Linux Audio

Check our new training course

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