async_xor.c 12 KB

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