1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions for assembling 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 "cio.h" 19 20 /** 21 * tcw_get_intrg - return pointer to associated interrogate tcw 22 * @tcw: pointer to the original tcw 23 * 24 * Return a pointer to the interrogate tcw associated with the specified tcw 25 * or %NULL if there is no associated interrogate tcw. 26 */ 27 struct tcw *tcw_get_intrg(struct tcw *tcw) 28 { 29 return dma32_to_virt(tcw->intrg); 30 } 31 EXPORT_SYMBOL(tcw_get_intrg); 32 33 /** 34 * tcw_get_data - return pointer to input/output data associated with tcw 35 * @tcw: pointer to the tcw 36 * 37 * Return the input or output data address specified in the tcw depending 38 * on whether the r-bit or the w-bit is set. If neither bit is set, return 39 * %NULL. 40 */ 41 void *tcw_get_data(struct tcw *tcw) 42 { 43 if (tcw->r) 44 return dma64_to_virt(tcw->input); 45 if (tcw->w) 46 return dma64_to_virt(tcw->output); 47 return NULL; 48 } 49 EXPORT_SYMBOL(tcw_get_data); 50 51 /** 52 * tcw_get_tccb - return pointer to tccb associated with tcw 53 * @tcw: pointer to the tcw 54 * 55 * Return pointer to the tccb associated with this tcw. 56 */ 57 struct tccb *tcw_get_tccb(struct tcw *tcw) 58 { 59 return dma64_to_virt(tcw->tccb); 60 } 61 EXPORT_SYMBOL(tcw_get_tccb); 62 63 /** 64 * tcw_get_tsb - return pointer to tsb associated with tcw 65 * @tcw: pointer to the tcw 66 * 67 * Return pointer to the tsb associated with this tcw. 68 */ 69 struct tsb *tcw_get_tsb(struct tcw *tcw) 70 { 71 return dma64_to_virt(tcw->tsb); 72 } 73 EXPORT_SYMBOL(tcw_get_tsb); 74 75 /** 76 * tcw_init - initialize tcw data structure 77 * @tcw: pointer to the tcw to be initialized 78 * @r: initial value of the r-bit 79 * @w: initial value of the w-bit 80 * 81 * Initialize all fields of the specified tcw data structure with zero and 82 * fill in the format, flags, r and w fields. 83 */ 84 void tcw_init(struct tcw *tcw, int r, int w) 85 { 86 memset(tcw, 0, sizeof(struct tcw)); 87 tcw->format = TCW_FORMAT_DEFAULT; 88 tcw->flags = TCW_FLAGS_TIDAW_FORMAT(TCW_TIDAW_FORMAT_DEFAULT); 89 if (r) 90 tcw->r = 1; 91 if (w) 92 tcw->w = 1; 93 } 94 EXPORT_SYMBOL(tcw_init); 95 96 static inline size_t tca_size(struct tccb *tccb) 97 { 98 return tccb->tcah.tcal - 12; 99 } 100 101 static u32 calc_dcw_count(struct tccb *tccb) 102 { 103 int offset; 104 struct dcw *dcw; 105 u32 count = 0; 106 size_t size; 107 108 size = tca_size(tccb); 109 for (offset = 0; offset < size;) { 110 dcw = (struct dcw *) &tccb->tca[offset]; 111 count += dcw->count; 112 if (!(dcw->flags & DCW_FLAGS_CC)) 113 break; 114 offset += sizeof(struct dcw) + ALIGN((int) dcw->cd_count, 4); 115 } 116 return count; 117 } 118 119 static u32 calc_cbc_size(struct tidaw *tidaw, int num) 120 { 121 int i; 122 u32 cbc_data; 123 u32 cbc_count = 0; 124 u64 data_count = 0; 125 126 for (i = 0; i < num; i++) { 127 if (tidaw[i].flags & TIDAW_FLAGS_LAST) 128 break; 129 /* TODO: find out if padding applies to total of data 130 * transferred or data transferred by this tidaw. Assumption: 131 * applies to total. */ 132 data_count += tidaw[i].count; 133 if (tidaw[i].flags & TIDAW_FLAGS_INSERT_CBC) { 134 cbc_data = 4 + ALIGN(data_count, 4) - data_count; 135 cbc_count += cbc_data; 136 data_count += cbc_data; 137 } 138 } 139 return cbc_count; 140 } 141 142 /** 143 * tcw_finalize - finalize tcw length fields and tidaw list 144 * @tcw: pointer to the tcw 145 * @num_tidaws: the number of tidaws used to address input/output data or zero 146 * if no tida is used 147 * 148 * Calculate the input-/output-count and tccbl field in the tcw, add a 149 * tcat the tccb and terminate the data tidaw list if used. 150 * 151 * Note: in case input- or output-tida is used, the tidaw-list must be stored 152 * in contiguous storage (no ttic). The tcal field in the tccb must be 153 * up-to-date. 154 */ 155 void tcw_finalize(struct tcw *tcw, int num_tidaws) 156 { 157 struct tidaw *tidaw; 158 struct tccb *tccb; 159 struct tccb_tcat *tcat; 160 u32 count; 161 162 /* Terminate tidaw list. */ 163 tidaw = tcw_get_data(tcw); 164 if (num_tidaws > 0) 165 tidaw[num_tidaws - 1].flags |= TIDAW_FLAGS_LAST; 166 /* Add tcat to tccb. */ 167 tccb = tcw_get_tccb(tcw); 168 tcat = (struct tccb_tcat *) &tccb->tca[tca_size(tccb)]; 169 memset(tcat, 0, sizeof(*tcat)); 170 /* Calculate tcw input/output count and tcat transport count. */ 171 count = calc_dcw_count(tccb); 172 if (tcw->w && (tcw->flags & TCW_FLAGS_OUTPUT_TIDA)) 173 count += calc_cbc_size(tidaw, num_tidaws); 174 if (tcw->r) 175 tcw->input_count = count; 176 else if (tcw->w) 177 tcw->output_count = count; 178 tcat->count = ALIGN(count, 4) + 4; 179 /* Calculate tccbl. */ 180 tcw->tccbl = (sizeof(struct tccb) + tca_size(tccb) + 181 sizeof(struct tccb_tcat) - 20) >> 2; 182 } 183 EXPORT_SYMBOL(tcw_finalize); 184 185 /** 186 * tcw_set_intrg - set the interrogate tcw address of a tcw 187 * @tcw: the tcw address 188 * @intrg_tcw: the address of the interrogate tcw 189 * 190 * Set the address of the interrogate tcw in the specified tcw. 191 */ 192 void tcw_set_intrg(struct tcw *tcw, struct tcw *intrg_tcw) 193 { 194 tcw->intrg = virt_to_dma32(intrg_tcw); 195 } 196 EXPORT_SYMBOL(tcw_set_intrg); 197 198 /** 199 * tcw_set_data - set data address and tida flag of a tcw 200 * @tcw: the tcw address 201 * @data: the data address 202 * @use_tidal: zero of the data address specifies a contiguous block of data, 203 * non-zero if it specifies a list if tidaws. 204 * 205 * Set the input/output data address of a tcw (depending on the value of the 206 * r-flag and w-flag). If @use_tidal is non-zero, the corresponding tida flag 207 * is set as well. 208 */ 209 void tcw_set_data(struct tcw *tcw, void *data, int use_tidal) 210 { 211 if (tcw->r) { 212 tcw->input = virt_to_dma64(data); 213 if (use_tidal) 214 tcw->flags |= TCW_FLAGS_INPUT_TIDA; 215 } else if (tcw->w) { 216 tcw->output = virt_to_dma64(data); 217 if (use_tidal) 218 tcw->flags |= TCW_FLAGS_OUTPUT_TIDA; 219 } 220 } 221 EXPORT_SYMBOL(tcw_set_data); 222 223 /** 224 * tcw_set_tccb - set tccb address of a tcw 225 * @tcw: the tcw address 226 * @tccb: the tccb address 227 * 228 * Set the address of the tccb in the specified tcw. 229 */ 230 void tcw_set_tccb(struct tcw *tcw, struct tccb *tccb) 231 { 232 tcw->tccb = virt_to_dma64(tccb); 233 } 234 EXPORT_SYMBOL(tcw_set_tccb); 235 236 /** 237 * tcw_set_tsb - set tsb address of a tcw 238 * @tcw: the tcw address 239 * @tsb: the tsb address 240 * 241 * Set the address of the tsb in the specified tcw. 242 */ 243 void tcw_set_tsb(struct tcw *tcw, struct tsb *tsb) 244 { 245 tcw->tsb = virt_to_dma64(tsb); 246 } 247 EXPORT_SYMBOL(tcw_set_tsb); 248 249 /** 250 * tccb_init - initialize tccb 251 * @tccb: the tccb address 252 * @size: the maximum size of the tccb 253 * @sac: the service-action-code to be user 254 * 255 * Initialize the header of the specified tccb by resetting all values to zero 256 * and filling in defaults for format, sac and initial tcal fields. 257 */ 258 void tccb_init(struct tccb *tccb, size_t size, u32 sac) 259 { 260 memset(tccb, 0, size); 261 tccb->tcah.format = TCCB_FORMAT_DEFAULT; 262 tccb->tcah.sac = sac; 263 tccb->tcah.tcal = 12; 264 } 265 EXPORT_SYMBOL(tccb_init); 266 267 /** 268 * tsb_init - initialize tsb 269 * @tsb: the tsb address 270 * 271 * Initialize the specified tsb by resetting all values to zero. 272 */ 273 void tsb_init(struct tsb *tsb) 274 { 275 memset(tsb, 0, sizeof(*tsb)); 276 } 277 EXPORT_SYMBOL(tsb_init); 278 279 /** 280 * tccb_add_dcw - add a dcw to the tccb 281 * @tccb: the tccb address 282 * @tccb_size: the maximum tccb size 283 * @cmd: the dcw command 284 * @flags: flags for the dcw 285 * @cd: pointer to control data for this dcw or NULL if none is required 286 * @cd_count: number of control data bytes for this dcw 287 * @count: number of data bytes for this dcw 288 * 289 * Add a new dcw to the specified tccb by writing the dcw information specified 290 * by @cmd, @flags, @cd, @cd_count and @count to the tca of the tccb. Return 291 * a pointer to the newly added dcw on success or -%ENOSPC if the new dcw 292 * would exceed the available space as defined by @tccb_size. 293 * 294 * Note: the tcal field of the tccb header will be updates to reflect added 295 * content. 296 */ 297 struct dcw *tccb_add_dcw(struct tccb *tccb, size_t tccb_size, u8 cmd, u8 flags, 298 void *cd, u8 cd_count, u32 count) 299 { 300 struct dcw *dcw; 301 int size; 302 int tca_offset; 303 304 /* Check for space. */ 305 tca_offset = tca_size(tccb); 306 size = ALIGN(sizeof(struct dcw) + cd_count, 4); 307 if (sizeof(struct tccb_tcah) + tca_offset + size + 308 sizeof(struct tccb_tcat) > tccb_size) 309 return ERR_PTR(-ENOSPC); 310 /* Add dcw to tca. */ 311 dcw = (struct dcw *) &tccb->tca[tca_offset]; 312 memset(dcw, 0, size); 313 dcw->cmd = cmd; 314 dcw->flags = flags; 315 dcw->count = count; 316 dcw->cd_count = cd_count; 317 if (cd) 318 memcpy(&dcw->cd[0], cd, cd_count); 319 tccb->tcah.tcal += size; 320 return dcw; 321 } 322 EXPORT_SYMBOL(tccb_add_dcw); 323 324 /** 325 * tcw_add_tidaw - add a tidaw to a tcw 326 * @tcw: the tcw address 327 * @num_tidaws: the current number of tidaws 328 * @flags: flags for the new tidaw 329 * @addr: address value for the new tidaw 330 * @count: count value for the new tidaw 331 * 332 * Add a new tidaw to the input/output data tidaw-list of the specified tcw 333 * (depending on the value of the r-flag and w-flag) and return a pointer to 334 * the new tidaw. 335 * 336 * Note: the tidaw-list is assumed to be contiguous with no ttics. The caller 337 * must ensure that there is enough space for the new tidaw. The last-tidaw 338 * flag for the last tidaw in the list will be set by tcw_finalize. 339 */ 340 struct tidaw *tcw_add_tidaw(struct tcw *tcw, int num_tidaws, u8 flags, 341 void *addr, u32 count) 342 { 343 struct tidaw *tidaw; 344 345 /* Add tidaw to tidaw-list. */ 346 tidaw = ((struct tidaw *) tcw_get_data(tcw)) + num_tidaws; 347 memset(tidaw, 0, sizeof(struct tidaw)); 348 tidaw->flags = flags; 349 tidaw->count = count; 350 tidaw->addr = virt_to_dma64(addr); 351 return tidaw; 352 } 353 EXPORT_SYMBOL(tcw_add_tidaw); 354