1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * xor offload engine api 4 * 5 * Copyright © 2006, Intel Corporation. 6 * 7 * Dan Williams <dan.j.williams@intel.com> 8 * 9 * with architecture considerations by: 10 * Neil Brown <neilb@suse.de> 11 * Jeff Garzik <jeff@garzik.org> 12 */ 13 #include <linux/kernel.h> 14 #include <linux/interrupt.h> 15 #include <linux/module.h> 16 #include <linux/mm.h> 17 #include <linux/dma-mapping.h> 18 #include <linux/raid/xor.h> 19 #include <linux/async_tx.h> 20 21 /* do_async_xor - dma map the pages and perform the xor with an engine */ 22 static __async_inline struct dma_async_tx_descriptor * 23 do_async_xor(struct dma_chan *chan, struct dmaengine_unmap_data *unmap, 24 struct async_submit_ctl *submit) 25 { 26 struct dma_device *dma = chan->device; 27 struct dma_async_tx_descriptor *tx = NULL; 28 dma_async_tx_callback cb_fn_orig = submit->cb_fn; 29 void *cb_param_orig = submit->cb_param; 30 enum async_tx_flags flags_orig = submit->flags; 31 enum dma_ctrl_flags dma_flags = 0; 32 int src_cnt = unmap->to_cnt; 33 int xor_src_cnt; 34 dma_addr_t dma_dest = unmap->addr[unmap->to_cnt]; 35 dma_addr_t *src_list = unmap->addr; 36 37 while (src_cnt) { 38 dma_addr_t tmp; 39 40 submit->flags = flags_orig; 41 xor_src_cnt = min(src_cnt, (int)dma->max_xor); 42 /* if we are submitting additional xors, leave the chain open 43 * and clear the callback parameters 44 */ 45 if (src_cnt > xor_src_cnt) { 46 submit->flags &= ~ASYNC_TX_ACK; 47 submit->flags |= ASYNC_TX_FENCE; 48 submit->cb_fn = NULL; 49 submit->cb_param = NULL; 50 } else { 51 submit->cb_fn = cb_fn_orig; 52 submit->cb_param = cb_param_orig; 53 } 54 if (submit->cb_fn) 55 dma_flags |= DMA_PREP_INTERRUPT; 56 if (submit->flags & ASYNC_TX_FENCE) 57 dma_flags |= DMA_PREP_FENCE; 58 59 /* Drivers force forward progress in case they can not provide a 60 * descriptor 61 */ 62 tmp = src_list[0]; 63 if (src_list > unmap->addr) 64 src_list[0] = dma_dest; 65 tx = dma->device_prep_dma_xor(chan, dma_dest, src_list, 66 xor_src_cnt, unmap->len, 67 dma_flags); 68 69 if (unlikely(!tx)) 70 async_tx_quiesce(&submit->depend_tx); 71 72 /* spin wait for the preceding transactions to complete */ 73 while (unlikely(!tx)) { 74 dma_async_issue_pending(chan); 75 tx = dma->device_prep_dma_xor(chan, dma_dest, 76 src_list, 77 xor_src_cnt, unmap->len, 78 dma_flags); 79 } 80 src_list[0] = tmp; 81 82 dma_set_unmap(tx, unmap); 83 async_tx_submit(chan, tx, submit); 84 submit->depend_tx = tx; 85 86 if (src_cnt > xor_src_cnt) { 87 /* drop completed sources */ 88 src_cnt -= xor_src_cnt; 89 /* use the intermediate result a source */ 90 src_cnt++; 91 src_list += xor_src_cnt - 1; 92 } else 93 break; 94 } 95 96 return tx; 97 } 98 99 static void 100 do_sync_xor_offs(struct page *dest, unsigned int offset, 101 struct page **src_list, unsigned int *src_offs, 102 int src_cnt, size_t len, struct async_submit_ctl *submit) 103 { 104 int i; 105 int xor_src_cnt = 0; 106 void *dest_buf; 107 void **srcs; 108 109 if (submit->scribble) 110 srcs = submit->scribble; 111 else 112 srcs = (void **) src_list; 113 114 /* convert to buffer pointers */ 115 for (i = 0; i < src_cnt; i++) 116 if (src_list[i]) 117 srcs[xor_src_cnt++] = page_address(src_list[i]) + 118 (src_offs ? src_offs[i] : offset); 119 120 /* set destination address */ 121 dest_buf = page_address(dest) + offset; 122 if (submit->flags & ASYNC_TX_XOR_ZERO_DST) 123 memset(dest_buf, 0, len); 124 xor_gen(dest_buf, srcs, xor_src_cnt, len); 125 async_tx_sync_epilog(submit); 126 } 127 128 static inline bool 129 dma_xor_aligned_offsets(struct dma_device *device, unsigned int offset, 130 unsigned int *src_offs, int src_cnt, int len) 131 { 132 int i; 133 134 if (!is_dma_xor_aligned(device, offset, 0, len)) 135 return false; 136 137 if (!src_offs) 138 return true; 139 140 for (i = 0; i < src_cnt; i++) { 141 if (!is_dma_xor_aligned(device, src_offs[i], 0, len)) 142 return false; 143 } 144 return true; 145 } 146 147 /** 148 * async_xor_offs - attempt to xor a set of blocks with a dma engine. 149 * @dest: destination page 150 * @offset: dst offset to start transaction 151 * @src_list: array of source pages 152 * @src_offs: array of source pages offset, NULL means common src/dst offset 153 * @src_cnt: number of source pages 154 * @len: length in bytes 155 * @submit: submission / completion modifiers 156 * 157 * honored flags: ASYNC_TX_ACK, ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DST 158 * 159 * xor_gen always uses the dest as a source so the ASYNC_TX_XOR_ZERO_DST flag 160 * must be set to not include dest data in the calculation. The assumption with 161 * dma engines is that they only use the destination buffer as a source when it 162 * is explicitly specified in the source list. 163 * 164 * src_list note: if the dest is also a source it must be at index zero. 165 * The contents of this array will be overwritten if a scribble region 166 * is not specified. 167 */ 168 struct dma_async_tx_descriptor * 169 async_xor_offs(struct page *dest, unsigned int offset, 170 struct page **src_list, unsigned int *src_offs, 171 int src_cnt, size_t len, struct async_submit_ctl *submit) 172 { 173 struct dma_chan *chan = async_tx_find_channel(submit, DMA_XOR, 174 &dest, 1, src_list, 175 src_cnt, len); 176 struct dma_device *device = chan ? chan->device : NULL; 177 struct dmaengine_unmap_data *unmap = NULL; 178 179 BUG_ON(src_cnt <= 1); 180 181 if (device) 182 unmap = dmaengine_get_unmap_data(device->dev, src_cnt+1, GFP_NOWAIT); 183 184 if (unmap && dma_xor_aligned_offsets(device, offset, 185 src_offs, src_cnt, len)) { 186 struct dma_async_tx_descriptor *tx; 187 int i, j; 188 189 /* run the xor asynchronously */ 190 pr_debug("%s (async): len: %zu\n", __func__, len); 191 192 unmap->len = len; 193 for (i = 0, j = 0; i < src_cnt; i++) { 194 if (!src_list[i]) 195 continue; 196 unmap->to_cnt++; 197 unmap->addr[j++] = dma_map_page(device->dev, src_list[i], 198 src_offs ? src_offs[i] : offset, 199 len, DMA_TO_DEVICE); 200 } 201 202 /* map it bidirectional as it may be re-used as a source */ 203 unmap->addr[j] = dma_map_page(device->dev, dest, offset, len, 204 DMA_BIDIRECTIONAL); 205 unmap->bidi_cnt = 1; 206 207 tx = do_async_xor(chan, unmap, submit); 208 dmaengine_unmap_put(unmap); 209 return tx; 210 } else { 211 dmaengine_unmap_put(unmap); 212 /* run the xor synchronously */ 213 pr_debug("%s (sync): len: %zu\n", __func__, len); 214 WARN_ONCE(chan, "%s: no space for dma address conversion\n", 215 __func__); 216 217 /* in the sync case the dest is an implied source 218 * (assumes the dest is the first source) 219 */ 220 if (submit->flags & ASYNC_TX_XOR_DROP_DST) { 221 src_cnt--; 222 src_list++; 223 if (src_offs) 224 src_offs++; 225 } 226 227 /* wait for any prerequisite operations */ 228 async_tx_quiesce(&submit->depend_tx); 229 230 do_sync_xor_offs(dest, offset, src_list, src_offs, 231 src_cnt, len, submit); 232 233 return NULL; 234 } 235 } 236 EXPORT_SYMBOL_GPL(async_xor_offs); 237 238 /** 239 * async_xor - attempt to xor a set of blocks with a dma engine. 240 * @dest: destination page 241 * @src_list: array of source pages 242 * @offset: common src/dst offset to start transaction 243 * @src_cnt: number of source pages 244 * @len: length in bytes 245 * @submit: submission / completion modifiers 246 * 247 * honored flags: ASYNC_TX_ACK, ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DST 248 * 249 * xor_gen always uses the dest as a source so the ASYNC_TX_XOR_ZERO_DST flag 250 * must be set to not include dest data in the calculation. The assumption with 251 * dma engines is that they only use the destination buffer as a source when it 252 * is explicitly specified in the source list. 253 * 254 * src_list note: if the dest is also a source it must be at index zero. 255 * The contents of this array will be overwritten if a scribble region 256 * is not specified. 257 */ 258 struct dma_async_tx_descriptor * 259 async_xor(struct page *dest, struct page **src_list, unsigned int offset, 260 int src_cnt, size_t len, struct async_submit_ctl *submit) 261 { 262 return async_xor_offs(dest, offset, src_list, NULL, 263 src_cnt, len, submit); 264 } 265 EXPORT_SYMBOL_GPL(async_xor); 266 267 static int page_is_zero(struct page *p, unsigned int offset, size_t len) 268 { 269 return !memchr_inv(page_address(p) + offset, 0, len); 270 } 271 272 static inline struct dma_chan * 273 xor_val_chan(struct async_submit_ctl *submit, struct page *dest, 274 struct page **src_list, int src_cnt, size_t len) 275 { 276 #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA 277 return NULL; 278 #endif 279 return async_tx_find_channel(submit, DMA_XOR_VAL, &dest, 1, src_list, 280 src_cnt, len); 281 } 282 283 /** 284 * async_xor_val_offs - attempt a xor parity check with a dma engine. 285 * @dest: destination page used if the xor is performed synchronously 286 * @offset: des offset in pages to start transaction 287 * @src_list: array of source pages 288 * @src_offs: array of source pages offset, NULL means common src/det offset 289 * @src_cnt: number of source pages 290 * @len: length in bytes 291 * @result: 0 if sum == 0 else non-zero 292 * @submit: submission / completion modifiers 293 * 294 * honored flags: ASYNC_TX_ACK 295 * 296 * src_list note: if the dest is also a source it must be at index zero. 297 * The contents of this array will be overwritten if a scribble region 298 * is not specified. 299 */ 300 struct dma_async_tx_descriptor * 301 async_xor_val_offs(struct page *dest, unsigned int offset, 302 struct page **src_list, unsigned int *src_offs, 303 int src_cnt, size_t len, enum sum_check_flags *result, 304 struct async_submit_ctl *submit) 305 { 306 struct dma_chan *chan = xor_val_chan(submit, dest, src_list, src_cnt, len); 307 struct dma_device *device = chan ? chan->device : NULL; 308 struct dma_async_tx_descriptor *tx = NULL; 309 struct dmaengine_unmap_data *unmap = NULL; 310 311 BUG_ON(src_cnt <= 1); 312 313 if (device) 314 unmap = dmaengine_get_unmap_data(device->dev, src_cnt, GFP_NOWAIT); 315 316 if (unmap && src_cnt <= device->max_xor && 317 dma_xor_aligned_offsets(device, offset, src_offs, src_cnt, len)) { 318 unsigned long dma_prep_flags = 0; 319 int i; 320 321 pr_debug("%s: (async) len: %zu\n", __func__, len); 322 323 if (submit->cb_fn) 324 dma_prep_flags |= DMA_PREP_INTERRUPT; 325 if (submit->flags & ASYNC_TX_FENCE) 326 dma_prep_flags |= DMA_PREP_FENCE; 327 328 for (i = 0; i < src_cnt; i++) { 329 unmap->addr[i] = dma_map_page(device->dev, src_list[i], 330 src_offs ? src_offs[i] : offset, 331 len, DMA_TO_DEVICE); 332 unmap->to_cnt++; 333 } 334 unmap->len = len; 335 336 tx = device->device_prep_dma_xor_val(chan, unmap->addr, src_cnt, 337 len, result, 338 dma_prep_flags); 339 if (unlikely(!tx)) { 340 async_tx_quiesce(&submit->depend_tx); 341 342 while (!tx) { 343 dma_async_issue_pending(chan); 344 tx = device->device_prep_dma_xor_val(chan, 345 unmap->addr, src_cnt, len, result, 346 dma_prep_flags); 347 } 348 } 349 dma_set_unmap(tx, unmap); 350 async_tx_submit(chan, tx, submit); 351 } else { 352 enum async_tx_flags flags_orig = submit->flags; 353 354 pr_debug("%s: (sync) len: %zu\n", __func__, len); 355 WARN_ONCE(device && src_cnt <= device->max_xor, 356 "%s: no space for dma address conversion\n", 357 __func__); 358 359 submit->flags |= ASYNC_TX_XOR_DROP_DST; 360 submit->flags &= ~ASYNC_TX_ACK; 361 362 tx = async_xor_offs(dest, offset, src_list, src_offs, 363 src_cnt, len, submit); 364 365 async_tx_quiesce(&tx); 366 367 *result = !page_is_zero(dest, offset, len) << SUM_CHECK_P; 368 369 async_tx_sync_epilog(submit); 370 submit->flags = flags_orig; 371 } 372 dmaengine_unmap_put(unmap); 373 374 return tx; 375 } 376 EXPORT_SYMBOL_GPL(async_xor_val_offs); 377 378 MODULE_AUTHOR("Intel Corporation"); 379 MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api"); 380 MODULE_LICENSE("GPL"); 381