1 /* SPDX-License-Identifier: GPL-2.0 */
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 * Copyright IBM Corp. 2000
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 <linux/uaccess.h>
23 #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
24 #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
27 * Test if an address/length pair needs an idal list.
30 idal_is_needed(void *vaddr
, unsigned int length
)
32 return ((__pa(vaddr
) + length
- 1) >> 31) != 0;
37 * Return the number of idal words needed for an address/length pair.
39 static inline unsigned int idal_nr_words(void *vaddr
, unsigned int length
)
41 return ((__pa(vaddr
) & (IDA_BLOCK_SIZE
-1)) + length
+
42 (IDA_BLOCK_SIZE
-1)) >> IDA_SIZE_LOG
;
46 * Create the list of idal words for an address/length pair.
48 static inline unsigned long *idal_create_words(unsigned long *idaws
,
49 void *vaddr
, unsigned int length
)
55 cidaw
= ((paddr
& (IDA_BLOCK_SIZE
-1)) + length
+
56 (IDA_BLOCK_SIZE
-1)) >> IDA_SIZE_LOG
;
58 paddr
&= -IDA_BLOCK_SIZE
;
60 paddr
+= IDA_BLOCK_SIZE
;
67 * Sets the address of the data in CCW.
68 * If necessary it allocates an IDAL and sets the appropriate flags.
71 set_normalized_cda(struct ccw1
* ccw
, void *vaddr
)
76 if (ccw
->flags
& CCW_FLAG_IDA
)
78 nridaws
= idal_nr_words(vaddr
, ccw
->count
);
80 idal
= kmalloc(nridaws
* sizeof(unsigned long),
81 GFP_ATOMIC
| GFP_DMA
);
84 idal_create_words(idal
, vaddr
, ccw
->count
);
85 ccw
->flags
|= CCW_FLAG_IDA
;
88 ccw
->cda
= (__u32
)(unsigned long) vaddr
;
93 * Releases any allocated IDAL related to the CCW.
96 clear_normalized_cda(struct ccw1
* ccw
)
98 if (ccw
->flags
& CCW_FLAG_IDA
) {
99 kfree((void *)(unsigned long) ccw
->cda
);
100 ccw
->flags
&= ~CCW_FLAG_IDA
;
106 * Idal buffer extension
115 * Allocate an idal buffer
117 static inline struct idal_buffer
*
118 idal_buffer_alloc(size_t size
, int page_order
)
120 struct idal_buffer
*ib
;
121 int nr_chunks
, nr_ptrs
, i
;
123 nr_ptrs
= (size
+ IDA_BLOCK_SIZE
- 1) >> IDA_SIZE_LOG
;
124 nr_chunks
= (4096 << page_order
) >> IDA_SIZE_LOG
;
125 ib
= kmalloc(struct_size(ib
, data
, nr_ptrs
), GFP_DMA
| GFP_KERNEL
);
127 return ERR_PTR(-ENOMEM
);
129 ib
->page_order
= page_order
;
130 for (i
= 0; i
< nr_ptrs
; i
++) {
131 if ((i
& (nr_chunks
- 1)) != 0) {
132 ib
->data
[i
] = ib
->data
[i
-1] + IDA_BLOCK_SIZE
;
135 ib
->data
[i
] = (void *)
136 __get_free_pages(GFP_KERNEL
, page_order
);
137 if (ib
->data
[i
] != NULL
)
140 while (i
>= nr_chunks
) {
142 free_pages((unsigned long) ib
->data
[i
],
146 return ERR_PTR(-ENOMEM
);
152 * Free an idal buffer.
155 idal_buffer_free(struct idal_buffer
*ib
)
157 int nr_chunks
, nr_ptrs
, i
;
159 nr_ptrs
= (ib
->size
+ IDA_BLOCK_SIZE
- 1) >> IDA_SIZE_LOG
;
160 nr_chunks
= (4096 << ib
->page_order
) >> IDA_SIZE_LOG
;
161 for (i
= 0; i
< nr_ptrs
; i
+= nr_chunks
)
162 free_pages((unsigned long) ib
->data
[i
], ib
->page_order
);
167 * Test if a idal list is really needed.
170 __idal_buffer_is_needed(struct idal_buffer
*ib
)
172 return ib
->size
> (4096ul << ib
->page_order
) ||
173 idal_is_needed(ib
->data
[0], ib
->size
);
177 * Set channel data address to idal buffer.
180 idal_buffer_set_cda(struct idal_buffer
*ib
, struct ccw1
*ccw
)
182 if (__idal_buffer_is_needed(ib
)) {
184 ccw
->cda
= (u32
)(addr_t
) ib
->data
;
185 ccw
->flags
|= CCW_FLAG_IDA
;
187 // we do not need idals - use direct addressing
188 ccw
->cda
= (u32
)(addr_t
) ib
->data
[0];
189 ccw
->count
= ib
->size
;
193 * Copy count bytes from an idal buffer to user memory
196 idal_buffer_to_user(struct idal_buffer
*ib
, void __user
*to
, size_t count
)
201 BUG_ON(count
> ib
->size
);
202 for (i
= 0; count
> IDA_BLOCK_SIZE
; i
++) {
203 left
= copy_to_user(to
, ib
->data
[i
], IDA_BLOCK_SIZE
);
205 return left
+ count
- IDA_BLOCK_SIZE
;
206 to
= (void __user
*) to
+ IDA_BLOCK_SIZE
;
207 count
-= IDA_BLOCK_SIZE
;
209 return copy_to_user(to
, ib
->data
[i
], count
);
213 * Copy count bytes from user memory to an idal buffer
216 idal_buffer_from_user(struct idal_buffer
*ib
, const void __user
*from
, size_t count
)
221 BUG_ON(count
> ib
->size
);
222 for (i
= 0; count
> IDA_BLOCK_SIZE
; i
++) {
223 left
= copy_from_user(ib
->data
[i
], from
, IDA_BLOCK_SIZE
);
225 return left
+ count
- IDA_BLOCK_SIZE
;
226 from
= (void __user
*) from
+ IDA_BLOCK_SIZE
;
227 count
-= IDA_BLOCK_SIZE
;
229 return copy_from_user(ib
->data
[i
], from
, count
);