1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
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
7 *
8 * History of changes
9 * 07/24/00 new file
10 * 05/04/02 code restructuring.
11 */
12
13 #ifndef _S390_IDALS_H
14 #define _S390_IDALS_H
15
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <asm/cio.h>
21 #include <linux/uaccess.h>
22
23 #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
24 #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
25
26 #define IDA_2K_SIZE_LOG 11
27 #define IDA_2K_BLOCK_SIZE (1L << IDA_2K_SIZE_LOG)
28
29 /*
30 * Test if an address/length pair needs an idal list.
31 */
32 static inline int
idal_is_needed(void * vaddr,unsigned int length)33 idal_is_needed(void *vaddr, unsigned int length)
34 {
35 return ((__pa(vaddr) + length - 1) >> 31) != 0;
36 }
37
38
39 /*
40 * Return the number of idal words needed for an address/length pair.
41 */
idal_nr_words(void * vaddr,unsigned int length)42 static inline unsigned int idal_nr_words(void *vaddr, unsigned int length)
43 {
44 return ((__pa(vaddr) & (IDA_BLOCK_SIZE-1)) + length +
45 (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
46 }
47
48 /*
49 * Return the number of 2K IDA words needed for an address/length pair.
50 */
idal_2k_nr_words(void * vaddr,unsigned int length)51 static inline unsigned int idal_2k_nr_words(void *vaddr, unsigned int length)
52 {
53 return ((__pa(vaddr) & (IDA_2K_BLOCK_SIZE - 1)) + length +
54 (IDA_2K_BLOCK_SIZE - 1)) >> IDA_2K_SIZE_LOG;
55 }
56
57 /*
58 * Create the list of idal words for an address/length pair.
59 */
idal_create_words(unsigned long * idaws,void * vaddr,unsigned int length)60 static inline unsigned long *idal_create_words(unsigned long *idaws,
61 void *vaddr, unsigned int length)
62 {
63 unsigned long paddr;
64 unsigned int cidaw;
65
66 paddr = __pa(vaddr);
67 cidaw = ((paddr & (IDA_BLOCK_SIZE-1)) + length +
68 (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
69 *idaws++ = paddr;
70 paddr &= -IDA_BLOCK_SIZE;
71 while (--cidaw > 0) {
72 paddr += IDA_BLOCK_SIZE;
73 *idaws++ = paddr;
74 }
75 return idaws;
76 }
77
78 /*
79 * Sets the address of the data in CCW.
80 * If necessary it allocates an IDAL and sets the appropriate flags.
81 */
82 static inline int
set_normalized_cda(struct ccw1 * ccw,void * vaddr)83 set_normalized_cda(struct ccw1 * ccw, void *vaddr)
84 {
85 unsigned int nridaws;
86 unsigned long *idal;
87
88 if (ccw->flags & CCW_FLAG_IDA)
89 return -EINVAL;
90 nridaws = idal_nr_words(vaddr, ccw->count);
91 if (nridaws > 0) {
92 idal = kmalloc(nridaws * sizeof(unsigned long),
93 GFP_ATOMIC | GFP_DMA );
94 if (idal == NULL)
95 return -ENOMEM;
96 idal_create_words(idal, vaddr, ccw->count);
97 ccw->flags |= CCW_FLAG_IDA;
98 vaddr = idal;
99 }
100 ccw->cda = (__u32)(unsigned long) vaddr;
101 return 0;
102 }
103
104 /*
105 * Releases any allocated IDAL related to the CCW.
106 */
107 static inline void
clear_normalized_cda(struct ccw1 * ccw)108 clear_normalized_cda(struct ccw1 * ccw)
109 {
110 if (ccw->flags & CCW_FLAG_IDA) {
111 kfree((void *)(unsigned long) ccw->cda);
112 ccw->flags &= ~CCW_FLAG_IDA;
113 }
114 ccw->cda = 0;
115 }
116
117 /*
118 * Idal buffer extension
119 */
120 struct idal_buffer {
121 size_t size;
122 size_t page_order;
123 void *data[];
124 };
125
126 /*
127 * Allocate an idal buffer
128 */
129 static inline struct idal_buffer *
idal_buffer_alloc(size_t size,int page_order)130 idal_buffer_alloc(size_t size, int page_order)
131 {
132 struct idal_buffer *ib;
133 int nr_chunks, nr_ptrs, i;
134
135 nr_ptrs = (size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
136 nr_chunks = (4096 << page_order) >> IDA_SIZE_LOG;
137 ib = kmalloc(struct_size(ib, data, nr_ptrs), GFP_DMA | GFP_KERNEL);
138 if (ib == NULL)
139 return ERR_PTR(-ENOMEM);
140 ib->size = size;
141 ib->page_order = page_order;
142 for (i = 0; i < nr_ptrs; i++) {
143 if ((i & (nr_chunks - 1)) != 0) {
144 ib->data[i] = ib->data[i-1] + IDA_BLOCK_SIZE;
145 continue;
146 }
147 ib->data[i] = (void *)
148 __get_free_pages(GFP_KERNEL, page_order);
149 if (ib->data[i] != NULL)
150 continue;
151 // Not enough memory
152 while (i >= nr_chunks) {
153 i -= nr_chunks;
154 free_pages((unsigned long) ib->data[i],
155 ib->page_order);
156 }
157 kfree(ib);
158 return ERR_PTR(-ENOMEM);
159 }
160 return ib;
161 }
162
163 /*
164 * Free an idal buffer.
165 */
166 static inline void
idal_buffer_free(struct idal_buffer * ib)167 idal_buffer_free(struct idal_buffer *ib)
168 {
169 int nr_chunks, nr_ptrs, i;
170
171 nr_ptrs = (ib->size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
172 nr_chunks = (4096 << ib->page_order) >> IDA_SIZE_LOG;
173 for (i = 0; i < nr_ptrs; i += nr_chunks)
174 free_pages((unsigned long) ib->data[i], ib->page_order);
175 kfree(ib);
176 }
177
178 /*
179 * Test if a idal list is really needed.
180 */
181 static inline int
__idal_buffer_is_needed(struct idal_buffer * ib)182 __idal_buffer_is_needed(struct idal_buffer *ib)
183 {
184 return ib->size > (4096ul << ib->page_order) ||
185 idal_is_needed(ib->data[0], ib->size);
186 }
187
188 /*
189 * Set channel data address to idal buffer.
190 */
191 static inline void
idal_buffer_set_cda(struct idal_buffer * ib,struct ccw1 * ccw)192 idal_buffer_set_cda(struct idal_buffer *ib, struct ccw1 *ccw)
193 {
194 if (__idal_buffer_is_needed(ib)) {
195 // setup idals;
196 ccw->cda = (u32)(addr_t) ib->data;
197 ccw->flags |= CCW_FLAG_IDA;
198 } else
199 // we do not need idals - use direct addressing
200 ccw->cda = (u32)(addr_t) ib->data[0];
201 ccw->count = ib->size;
202 }
203
204 /*
205 * Copy count bytes from an idal buffer to user memory
206 */
207 static inline size_t
idal_buffer_to_user(struct idal_buffer * ib,void __user * to,size_t count)208 idal_buffer_to_user(struct idal_buffer *ib, void __user *to, size_t count)
209 {
210 size_t left;
211 int i;
212
213 BUG_ON(count > ib->size);
214 for (i = 0; count > IDA_BLOCK_SIZE; i++) {
215 left = copy_to_user(to, ib->data[i], IDA_BLOCK_SIZE);
216 if (left)
217 return left + count - IDA_BLOCK_SIZE;
218 to = (void __user *) to + IDA_BLOCK_SIZE;
219 count -= IDA_BLOCK_SIZE;
220 }
221 return copy_to_user(to, ib->data[i], count);
222 }
223
224 /*
225 * Copy count bytes from user memory to an idal buffer
226 */
227 static inline size_t
idal_buffer_from_user(struct idal_buffer * ib,const void __user * from,size_t count)228 idal_buffer_from_user(struct idal_buffer *ib, const void __user *from, size_t count)
229 {
230 size_t left;
231 int i;
232
233 BUG_ON(count > ib->size);
234 for (i = 0; count > IDA_BLOCK_SIZE; i++) {
235 left = copy_from_user(ib->data[i], from, IDA_BLOCK_SIZE);
236 if (left)
237 return left + count - IDA_BLOCK_SIZE;
238 from = (void __user *) from + IDA_BLOCK_SIZE;
239 count -= IDA_BLOCK_SIZE;
240 }
241 return copy_from_user(ib->data[i], from, count);
242 }
243
244 #endif
245