2 * File...........: linux/include/asm-s390x/idals.h
3 * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Bugreports.to..: <Linux390@de.ibm.com>
6 * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 2000a
10 * 05/04/02 code restructuring.
16 #include <linux/config.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
22 #include <asm/uaccess.h>
25 #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
27 #define IDA_SIZE_LOG 11 /* 11 for 2k , 12 for 4k */
29 #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
32 * Test if an address/length pair needs an idal list.
35 idal_is_needed(void *vaddr, unsigned int length)
38 return ((__pa(vaddr) + length - 1) >> 31) != 0;
46 * Return the number of idal words needed for an address/length pair.
48 static inline unsigned int
49 idal_nr_words(void *vaddr, unsigned int length)
52 if (idal_is_needed(vaddr, length))
53 return ((__pa(vaddr) & (IDA_BLOCK_SIZE-1)) + length +
54 (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
60 * Create the list of idal words for an address/length pair.
62 static inline unsigned long *
63 idal_create_words(unsigned long *idaws, void *vaddr, unsigned int length)
70 cidaw = ((paddr & (IDA_BLOCK_SIZE-1)) + length +
71 (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
73 paddr &= -IDA_BLOCK_SIZE;
75 paddr += IDA_BLOCK_SIZE;
83 * Sets the address of the data in CCW.
84 * If necessary it allocates an IDAL and sets the appropriate flags.
87 set_normalized_cda(struct ccw1 * ccw, void *vaddr)
93 if (ccw->flags & CCW_FLAG_IDA)
95 nridaws = idal_nr_words(vaddr, ccw->count);
97 idal = kmalloc(nridaws * sizeof(unsigned long),
98 GFP_ATOMIC | GFP_DMA );
101 idal_create_words(idal, vaddr, ccw->count);
102 ccw->flags |= CCW_FLAG_IDA;
106 ccw->cda = (__u32)(unsigned long) vaddr;
111 * Releases any allocated IDAL related to the CCW.
114 clear_normalized_cda(struct ccw1 * ccw)
117 if (ccw->flags & CCW_FLAG_IDA) {
118 kfree((void *)(unsigned long) ccw->cda);
119 ccw->flags &= ~CCW_FLAG_IDA;
126 * Idal buffer extension
135 * Allocate an idal buffer
137 static inline struct idal_buffer *
138 idal_buffer_alloc(size_t size, int page_order)
140 struct idal_buffer *ib;
141 int nr_chunks, nr_ptrs, i;
143 nr_ptrs = (size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
144 nr_chunks = (4096 << page_order) >> IDA_SIZE_LOG;
145 ib = kmalloc(sizeof(struct idal_buffer) + nr_ptrs*sizeof(void *),
146 GFP_DMA | GFP_KERNEL);
148 return ERR_PTR(-ENOMEM);
150 ib->page_order = page_order;
151 for (i = 0; i < nr_ptrs; i++) {
152 if ((i & (nr_chunks - 1)) != 0) {
153 ib->data[i] = ib->data[i-1] + IDA_BLOCK_SIZE;
156 ib->data[i] = (void *)
157 __get_free_pages(GFP_KERNEL, page_order);
158 if (ib->data[i] != NULL)
161 while (i >= nr_chunks) {
163 free_pages((unsigned long) ib->data[i],
167 return ERR_PTR(-ENOMEM);
173 * Free an idal buffer.
176 idal_buffer_free(struct idal_buffer *ib)
178 int nr_chunks, nr_ptrs, i;
180 nr_ptrs = (ib->size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
181 nr_chunks = (4096 << ib->page_order) >> IDA_SIZE_LOG;
182 for (i = 0; i < nr_ptrs; i += nr_chunks)
183 free_pages((unsigned long) ib->data[i], ib->page_order);
188 * Test if a idal list is really needed.
191 __idal_buffer_is_needed(struct idal_buffer *ib)
194 return ib->size > (4096ul << ib->page_order) ||
195 idal_is_needed(ib->data[0], ib->size);
197 return ib->size > (4096ul << ib->page_order);
202 * Set channel data address to idal buffer.
205 idal_buffer_set_cda(struct idal_buffer *ib, struct ccw1 *ccw)
207 if (__idal_buffer_is_needed(ib)) {
209 ccw->cda = (u32)(addr_t) ib->data;
210 ccw->flags |= CCW_FLAG_IDA;
212 // we do not need idals - use direct addressing
213 ccw->cda = (u32)(addr_t) ib->data[0];
214 ccw->count = ib->size;
218 * Copy count bytes from an idal buffer to user memory
221 idal_buffer_to_user(struct idal_buffer *ib, void __user *to, size_t count)
226 BUG_ON(count > ib->size);
227 for (i = 0; count > IDA_BLOCK_SIZE; i++) {
228 left = copy_to_user(to, ib->data[i], IDA_BLOCK_SIZE);
230 return left + count - IDA_BLOCK_SIZE;
231 to = (void __user *) to + IDA_BLOCK_SIZE;
232 count -= IDA_BLOCK_SIZE;
234 return copy_to_user(to, ib->data[i], count);
238 * Copy count bytes from user memory to an idal buffer
241 idal_buffer_from_user(struct idal_buffer *ib, const void __user *from, size_t count)
246 BUG_ON(count > ib->size);
247 for (i = 0; count > IDA_BLOCK_SIZE; i++) {
248 left = copy_from_user(ib->data[i], from, IDA_BLOCK_SIZE);
250 return left + count - IDA_BLOCK_SIZE;
251 from = (void __user *) from + IDA_BLOCK_SIZE;
252 count -= IDA_BLOCK_SIZE;
254 return copy_from_user(ib->data[i], from, count);