File Index
Files and directories
Sign in
FILE

itcw.c

File preview with syntax highlighting.

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