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