xref: /openbmc/linux/drivers/s390/cio/itcw.c (revision dce54888)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Functions for incremental construction of fcx enabled I/O control blocks.
4  *
5  *    Copyright IBM Corp. 2008
6  *    Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
12 #include <linux/io.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <asm/fcx.h>
17 #include <asm/itcw.h>
18 
19 /*
20  * struct itcw - incremental tcw helper data type
21  *
22  * This structure serves as a handle for the incremental construction of a
23  * tcw and associated tccb, tsb, data tidaw-list plus an optional interrogate
24  * tcw and associated data. The data structures are contained inside a single
25  * contiguous buffer provided by the user.
26  *
27  * The itcw construction functions take care of overall data integrity:
28  * - reset unused fields to zero
29  * - fill in required pointers
30  * - ensure required alignment for data structures
31  * - prevent data structures to cross 4k-byte boundary where required
32  * - calculate tccb-related length fields
33  * - optionally provide ready-made interrogate tcw and associated structures
34  *
35  * Restrictions apply to the itcws created with these construction functions:
36  * - tida only supported for data address, not for tccb
37  * - only contiguous tidaw-lists (no ttic)
38  * - total number of bytes required per itcw may not exceed 4k bytes
39  * - either read or write operation (may not work with r=0 and w=0)
40  *
41  * Example:
42  * struct itcw *itcw;
43  * void *buffer;
44  * size_t size;
45  *
46  * size = itcw_calc_size(1, 2, 0);
47  * buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
48  * if (!buffer)
49  *	return -ENOMEM;
50  * itcw = itcw_init(buffer, size, ITCW_OP_READ, 1, 2, 0);
51  * if (IS_ERR(itcw))
52  *	return PTR_ER(itcw);
53  * itcw_add_dcw(itcw, 0x2, 0, NULL, 0, 72);
54  * itcw_add_tidaw(itcw, 0, 0x30000, 20);
55  * itcw_add_tidaw(itcw, 0, 0x40000, 52);
56  * itcw_finalize(itcw);
57  *
58  */
59 struct itcw {
60 	struct tcw *tcw;
61 	struct tcw *intrg_tcw;
62 	int num_tidaws;
63 	int max_tidaws;
64 	int intrg_num_tidaws;
65 	int intrg_max_tidaws;
66 };
67 
68 /**
69  * itcw_get_tcw - return pointer to tcw associated with the itcw
70  * @itcw: address of the itcw
71  *
72  * Return pointer to the tcw associated with the itcw.
73  */
74 struct tcw *itcw_get_tcw(struct itcw *itcw)
75 {
76 	return itcw->tcw;
77 }
78 EXPORT_SYMBOL(itcw_get_tcw);
79 
80 /**
81  * itcw_calc_size - return the size of an itcw with the given parameters
82  * @intrg: if non-zero, add an interrogate tcw
83  * @max_tidaws: maximum number of tidaws to be used for data addressing or zero
84  * if no tida is to be used.
85  * @intrg_max_tidaws: maximum number of tidaws to be used for data addressing
86  * by the interrogate tcw, if specified
87  *
88  * Calculate and return the number of bytes required to hold an itcw with the
89  * given parameters and assuming tccbs with maximum size.
90  *
91  * Note that the resulting size also contains bytes needed for alignment
92  * padding as well as padding to ensure that data structures don't cross a
93  * 4k-boundary where required.
94  */
95 size_t itcw_calc_size(int intrg, int max_tidaws, int intrg_max_tidaws)
96 {
97 	size_t len;
98 	int cross_count;
99 
100 	/* Main data. */
101 	len = sizeof(struct itcw);
102 	len += /* TCW */ sizeof(struct tcw) + /* TCCB */ TCCB_MAX_SIZE +
103 	       /* TSB */ sizeof(struct tsb) +
104 	       /* TIDAL */ max_tidaws * sizeof(struct tidaw);
105 	/* Interrogate data. */
106 	if (intrg) {
107 		len += /* TCW */ sizeof(struct tcw) + /* TCCB */ TCCB_MAX_SIZE +
108 		       /* TSB */ sizeof(struct tsb) +
109 		       /* TIDAL */ intrg_max_tidaws * sizeof(struct tidaw);
110 	}
111 
112 	/* Maximum required alignment padding. */
113 	len += /* Initial TCW */ 63 + /* Interrogate TCCB */ 7;
114 
115 	/* TIDAW lists may not cross a 4k boundary. To cross a
116 	 * boundary we need to add a TTIC TIDAW. We need to reserve
117 	 * one additional TIDAW for a TTIC that we may need to add due
118 	 * to the placement of the data chunk in memory, and a further
119 	 * TIDAW for each page boundary that the TIDAW list may cross
120 	 * due to it's own size.
121 	 */
122 	if (max_tidaws) {
123 		cross_count = 1 + ((max_tidaws * sizeof(struct tidaw) - 1)
124 				   >> PAGE_SHIFT);
125 		len += cross_count * sizeof(struct tidaw);
126 	}
127 	if (intrg_max_tidaws) {
128 		cross_count = 1 + ((intrg_max_tidaws * sizeof(struct tidaw) - 1)
129 				   >> PAGE_SHIFT);
130 		len += cross_count * sizeof(struct tidaw);
131 	}
132 	return len;
133 }
134 EXPORT_SYMBOL(itcw_calc_size);
135 
136 #define CROSS4K(x, l)	(((x) & ~4095) != ((x + l) & ~4095))
137 
138 static inline void *fit_chunk(addr_t *start, addr_t end, size_t len,
139 			      int align, int check_4k)
140 {
141 	addr_t addr;
142 
143 	addr = ALIGN(*start, align);
144 	if (check_4k && CROSS4K(addr, len)) {
145 		addr = ALIGN(addr, 4096);
146 		addr = ALIGN(addr, align);
147 	}
148 	if (addr + len > end)
149 		return ERR_PTR(-ENOSPC);
150 	*start = addr + len;
151 	return (void *) addr;
152 }
153 
154 /**
155  * itcw_init - initialize incremental tcw data structure
156  * @buffer: address of buffer to use for data structures
157  * @size: number of bytes in buffer
158  * @op: %ITCW_OP_READ for a read operation tcw, %ITCW_OP_WRITE for a write
159  * operation tcw
160  * @intrg: if non-zero, add and initialize an interrogate tcw
161  * @max_tidaws: maximum number of tidaws to be used for data addressing or zero
162  * if no tida is to be used.
163  * @intrg_max_tidaws: maximum number of tidaws to be used for data addressing
164  * by the interrogate tcw, if specified
165  *
166  * Prepare the specified buffer to be used as an incremental tcw, i.e. a
167  * helper data structure that can be used to construct a valid tcw by
168  * successive calls to other helper functions. Note: the buffer needs to be
169  * located below the 2G address limit. The resulting tcw has the following
170  * restrictions:
171  *  - no tccb tidal
172  *  - input/output tidal is contiguous (no ttic)
173  *  - total data should not exceed 4k
174  *  - tcw specifies either read or write operation
175  *
176  * On success, return pointer to the resulting incremental tcw data structure,
177  * ERR_PTR otherwise.
178  */
179 struct itcw *itcw_init(void *buffer, size_t size, int op, int intrg,
180 		       int max_tidaws, int intrg_max_tidaws)
181 {
182 	struct itcw *itcw;
183 	void *chunk;
184 	addr_t start;
185 	addr_t end;
186 	int cross_count;
187 
188 	/* Check for 2G limit. */
189 	start = (addr_t) buffer;
190 	end = start + size;
191 	if ((virt_to_phys(buffer) + size) > (1 << 31))
192 		return ERR_PTR(-EINVAL);
193 	memset(buffer, 0, size);
194 	/* ITCW. */
195 	chunk = fit_chunk(&start, end, sizeof(struct itcw), 1, 0);
196 	if (IS_ERR(chunk))
197 		return chunk;
198 	itcw = chunk;
199 	/* allow for TTIC tidaws that may be needed to cross a page boundary */
200 	cross_count = 0;
201 	if (max_tidaws)
202 		cross_count = 1 + ((max_tidaws * sizeof(struct tidaw) - 1)
203 				   >> PAGE_SHIFT);
204 	itcw->max_tidaws = max_tidaws + cross_count;
205 	cross_count = 0;
206 	if (intrg_max_tidaws)
207 		cross_count = 1 + ((intrg_max_tidaws * sizeof(struct tidaw) - 1)
208 				   >> PAGE_SHIFT);
209 	itcw->intrg_max_tidaws = intrg_max_tidaws + cross_count;
210 	/* Main TCW. */
211 	chunk = fit_chunk(&start, end, sizeof(struct tcw), 64, 0);
212 	if (IS_ERR(chunk))
213 		return chunk;
214 	itcw->tcw = chunk;
215 	tcw_init(itcw->tcw, (op == ITCW_OP_READ) ? 1 : 0,
216 		 (op == ITCW_OP_WRITE) ? 1 : 0);
217 	/* Interrogate TCW. */
218 	if (intrg) {
219 		chunk = fit_chunk(&start, end, sizeof(struct tcw), 64, 0);
220 		if (IS_ERR(chunk))
221 			return chunk;
222 		itcw->intrg_tcw = chunk;
223 		tcw_init(itcw->intrg_tcw, 1, 0);
224 		tcw_set_intrg(itcw->tcw, itcw->intrg_tcw);
225 	}
226 	/* Data TIDAL. */
227 	if (max_tidaws > 0) {
228 		chunk = fit_chunk(&start, end, sizeof(struct tidaw) *
229 				  itcw->max_tidaws, 16, 0);
230 		if (IS_ERR(chunk))
231 			return chunk;
232 		tcw_set_data(itcw->tcw, chunk, 1);
233 	}
234 	/* Interrogate data TIDAL. */
235 	if (intrg && (intrg_max_tidaws > 0)) {
236 		chunk = fit_chunk(&start, end, sizeof(struct tidaw) *
237 				  itcw->intrg_max_tidaws, 16, 0);
238 		if (IS_ERR(chunk))
239 			return chunk;
240 		tcw_set_data(itcw->intrg_tcw, chunk, 1);
241 	}
242 	/* TSB. */
243 	chunk = fit_chunk(&start, end, sizeof(struct tsb), 8, 0);
244 	if (IS_ERR(chunk))
245 		return chunk;
246 	tsb_init(chunk);
247 	tcw_set_tsb(itcw->tcw, chunk);
248 	/* Interrogate TSB. */
249 	if (intrg) {
250 		chunk = fit_chunk(&start, end, sizeof(struct tsb), 8, 0);
251 		if (IS_ERR(chunk))
252 			return chunk;
253 		tsb_init(chunk);
254 		tcw_set_tsb(itcw->intrg_tcw, chunk);
255 	}
256 	/* TCCB. */
257 	chunk = fit_chunk(&start, end, TCCB_MAX_SIZE, 8, 0);
258 	if (IS_ERR(chunk))
259 		return chunk;
260 	tccb_init(chunk, TCCB_MAX_SIZE, TCCB_SAC_DEFAULT);
261 	tcw_set_tccb(itcw->tcw, chunk);
262 	/* Interrogate TCCB. */
263 	if (intrg) {
264 		chunk = fit_chunk(&start, end, TCCB_MAX_SIZE, 8, 0);
265 		if (IS_ERR(chunk))
266 			return chunk;
267 		tccb_init(chunk, TCCB_MAX_SIZE, TCCB_SAC_INTRG);
268 		tcw_set_tccb(itcw->intrg_tcw, chunk);
269 		tccb_add_dcw(chunk, TCCB_MAX_SIZE, DCW_CMD_INTRG, 0, NULL,
270 			     sizeof(struct dcw_intrg_data), 0);
271 		tcw_finalize(itcw->intrg_tcw, 0);
272 	}
273 	return itcw;
274 }
275 EXPORT_SYMBOL(itcw_init);
276 
277 /**
278  * itcw_add_dcw - add a dcw to the itcw
279  * @itcw: address of the itcw
280  * @cmd: the dcw command
281  * @flags: flags for the dcw
282  * @cd: address of control data for this dcw or NULL if none is required
283  * @cd_count: number of control data bytes for this dcw
284  * @count: number of data bytes for this dcw
285  *
286  * Add a new dcw to the specified itcw by writing the dcw information specified
287  * by @cmd, @flags, @cd, @cd_count and @count to the tca of the tccb. Return
288  * a pointer to the newly added dcw on success or -%ENOSPC if the new dcw
289  * would exceed the available space.
290  *
291  * Note: the tcal field of the tccb header will be updated to reflect added
292  * content.
293  */
294 struct dcw *itcw_add_dcw(struct itcw *itcw, u8 cmd, u8 flags, void *cd,
295 			 u8 cd_count, u32 count)
296 {
297 	return tccb_add_dcw(tcw_get_tccb(itcw->tcw), TCCB_MAX_SIZE, cmd,
298 			    flags, cd, cd_count, count);
299 }
300 EXPORT_SYMBOL(itcw_add_dcw);
301 
302 /**
303  * itcw_add_tidaw - add a tidaw to the itcw
304  * @itcw: address of the itcw
305  * @flags: flags for the new tidaw
306  * @addr: address value for the new tidaw
307  * @count: count value for the new tidaw
308  *
309  * Add a new tidaw to the input/output data tidaw-list of the specified itcw
310  * (depending on the value of the r-flag and w-flag). Return a pointer to
311  * the new tidaw on success or -%ENOSPC if the new tidaw would exceed the
312  * available space.
313  *
314  * Note: TTIC tidaws are automatically added when needed, so explicitly calling
315  * this interface with the TTIC flag is not supported. The last-tidaw flag
316  * for the last tidaw in the list will be set by itcw_finalize.
317  */
318 struct tidaw *itcw_add_tidaw(struct itcw *itcw, u8 flags, void *addr, u32 count)
319 {
320 	struct tidaw *following;
321 
322 	if (itcw->num_tidaws >= itcw->max_tidaws)
323 		return ERR_PTR(-ENOSPC);
324 	/*
325 	 * Is the tidaw, which follows the one we are about to fill, on the next
326 	 * page? Then we have to insert a TTIC tidaw first, that points to the
327 	 * tidaw on the new page.
328 	 */
329 	following = ((struct tidaw *) tcw_get_data(itcw->tcw))
330 		+ itcw->num_tidaws + 1;
331 	if (itcw->num_tidaws && !((unsigned long) following & ~PAGE_MASK)) {
332 		tcw_add_tidaw(itcw->tcw, itcw->num_tidaws++,
333 			      TIDAW_FLAGS_TTIC, following, 0);
334 		if (itcw->num_tidaws >= itcw->max_tidaws)
335 			return ERR_PTR(-ENOSPC);
336 	}
337 	return tcw_add_tidaw(itcw->tcw, itcw->num_tidaws++, flags, addr, count);
338 }
339 EXPORT_SYMBOL(itcw_add_tidaw);
340 
341 /**
342  * itcw_set_data - set data address and tida flag of the itcw
343  * @itcw: address of the itcw
344  * @addr: the data address
345  * @use_tidal: zero of the data address specifies a contiguous block of data,
346  * non-zero if it specifies a list if tidaws.
347  *
348  * Set the input/output data address of the itcw (depending on the value of the
349  * r-flag and w-flag). If @use_tidal is non-zero, the corresponding tida flag
350  * is set as well.
351  */
352 void itcw_set_data(struct itcw *itcw, void *addr, int use_tidal)
353 {
354 	tcw_set_data(itcw->tcw, addr, use_tidal);
355 }
356 EXPORT_SYMBOL(itcw_set_data);
357 
358 /**
359  * itcw_finalize - calculate length and count fields of the itcw
360  * @itcw: address of the itcw
361  *
362  * Calculate tcw input-/output-count and tccbl fields and add a tcat the tccb.
363  * In case input- or output-tida is used, the tidaw-list must be stored in
364  * continuous storage (no ttic). The tcal field in the tccb must be
365  * up-to-date.
366  */
367 void itcw_finalize(struct itcw *itcw)
368 {
369 	tcw_finalize(itcw->tcw, itcw->num_tidaws);
370 }
371 EXPORT_SYMBOL(itcw_finalize);
372