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/errno.h>
17 #include <linux/err.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
21 #include <asm/uaccess.h>
24 #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
26 #define IDA_SIZE_LOG 11 /* 11 for 2k , 12 for 4k */
28 #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
31 * Test if an address/length pair needs an idal list.
34 idal_is_needed(void *vaddr
, unsigned int length
)
37 return ((__pa(vaddr
) + length
- 1) >> 31) != 0;
45 * Return the number of idal words needed for an address/length pair.
47 static inline unsigned int
48 idal_nr_words(void *vaddr
, unsigned int length
)
51 if (idal_is_needed(vaddr
, length
))
52 return ((__pa(vaddr
) & (IDA_BLOCK_SIZE
-1)) + length
+
53 (IDA_BLOCK_SIZE
-1)) >> IDA_SIZE_LOG
;
59 * Create the list of idal words for an address/length pair.
61 static inline unsigned long *
62 idal_create_words(unsigned long *idaws
, void *vaddr
, unsigned int length
)
69 cidaw
= ((paddr
& (IDA_BLOCK_SIZE
-1)) + length
+
70 (IDA_BLOCK_SIZE
-1)) >> IDA_SIZE_LOG
;
72 paddr
&= -IDA_BLOCK_SIZE
;
74 paddr
+= IDA_BLOCK_SIZE
;
82 * Sets the address of the data in CCW.
83 * If necessary it allocates an IDAL and sets the appropriate flags.
86 set_normalized_cda(struct ccw1
* ccw
, void *vaddr
)
92 if (ccw
->flags
& CCW_FLAG_IDA
)
94 nridaws
= idal_nr_words(vaddr
, ccw
->count
);
96 idal
= kmalloc(nridaws
* sizeof(unsigned long),
97 GFP_ATOMIC
| GFP_DMA
);
100 idal_create_words(idal
, vaddr
, ccw
->count
);
101 ccw
->flags
|= CCW_FLAG_IDA
;
105 ccw
->cda
= (__u32
)(unsigned long) vaddr
;
110 * Releases any allocated IDAL related to the CCW.
113 clear_normalized_cda(struct ccw1
* ccw
)
116 if (ccw
->flags
& CCW_FLAG_IDA
) {
117 kfree((void *)(unsigned long) ccw
->cda
);
118 ccw
->flags
&= ~CCW_FLAG_IDA
;
125 * Idal buffer extension
134 * Allocate an idal buffer
136 static inline struct idal_buffer
*
137 idal_buffer_alloc(size_t size
, int page_order
)
139 struct idal_buffer
*ib
;
140 int nr_chunks
, nr_ptrs
, i
;
142 nr_ptrs
= (size
+ IDA_BLOCK_SIZE
- 1) >> IDA_SIZE_LOG
;
143 nr_chunks
= (4096 << page_order
) >> IDA_SIZE_LOG
;
144 ib
= kmalloc(sizeof(struct idal_buffer
) + nr_ptrs
*sizeof(void *),
145 GFP_DMA
| GFP_KERNEL
);
147 return ERR_PTR(-ENOMEM
);
149 ib
->page_order
= page_order
;
150 for (i
= 0; i
< nr_ptrs
; i
++) {
151 if ((i
& (nr_chunks
- 1)) != 0) {
152 ib
->data
[i
] = ib
->data
[i
-1] + IDA_BLOCK_SIZE
;
155 ib
->data
[i
] = (void *)
156 __get_free_pages(GFP_KERNEL
, page_order
);
157 if (ib
->data
[i
] != NULL
)
160 while (i
>= nr_chunks
) {
162 free_pages((unsigned long) ib
->data
[i
],
166 return ERR_PTR(-ENOMEM
);
172 * Free an idal buffer.
175 idal_buffer_free(struct idal_buffer
*ib
)
177 int nr_chunks
, nr_ptrs
, i
;
179 nr_ptrs
= (ib
->size
+ IDA_BLOCK_SIZE
- 1) >> IDA_SIZE_LOG
;
180 nr_chunks
= (4096 << ib
->page_order
) >> IDA_SIZE_LOG
;
181 for (i
= 0; i
< nr_ptrs
; i
+= nr_chunks
)
182 free_pages((unsigned long) ib
->data
[i
], ib
->page_order
);
187 * Test if a idal list is really needed.
190 __idal_buffer_is_needed(struct idal_buffer
*ib
)
193 return ib
->size
> (4096ul << ib
->page_order
) ||
194 idal_is_needed(ib
->data
[0], ib
->size
);
196 return ib
->size
> (4096ul << ib
->page_order
);
201 * Set channel data address to idal buffer.
204 idal_buffer_set_cda(struct idal_buffer
*ib
, struct ccw1
*ccw
)
206 if (__idal_buffer_is_needed(ib
)) {
208 ccw
->cda
= (u32
)(addr_t
) ib
->data
;
209 ccw
->flags
|= CCW_FLAG_IDA
;
211 // we do not need idals - use direct addressing
212 ccw
->cda
= (u32
)(addr_t
) ib
->data
[0];
213 ccw
->count
= ib
->size
;
217 * Copy count bytes from an idal buffer to user memory
220 idal_buffer_to_user(struct idal_buffer
*ib
, void __user
*to
, size_t count
)
225 BUG_ON(count
> ib
->size
);
226 for (i
= 0; count
> IDA_BLOCK_SIZE
; i
++) {
227 left
= copy_to_user(to
, ib
->data
[i
], IDA_BLOCK_SIZE
);
229 return left
+ count
- IDA_BLOCK_SIZE
;
230 to
= (void __user
*) to
+ IDA_BLOCK_SIZE
;
231 count
-= IDA_BLOCK_SIZE
;
233 return copy_to_user(to
, ib
->data
[i
], count
);
237 * Copy count bytes from user memory to an idal buffer
240 idal_buffer_from_user(struct idal_buffer
*ib
, const void __user
*from
, size_t count
)
245 BUG_ON(count
> ib
->size
);
246 for (i
= 0; count
> IDA_BLOCK_SIZE
; i
++) {
247 left
= copy_from_user(ib
->data
[i
], from
, IDA_BLOCK_SIZE
);
249 return left
+ count
- IDA_BLOCK_SIZE
;
250 from
= (void __user
*) from
+ IDA_BLOCK_SIZE
;
251 count
-= IDA_BLOCK_SIZE
;
253 return copy_from_user(ib
->data
[i
], from
, count
);