async_xor.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * xor offload engine api
  3. *
  4. * Copyright © 2006, Intel Corporation.
  5. *
  6. * Dan Williams <dan.j.williams@intel.com>
  7. *
  8. * with architecture considerations by:
  9. * Neil Brown <neilb@suse.de>
  10. * Jeff Garzik <jeff@garzik.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/module.h>
  29. #include <linux/mm.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/raid/xor.h>
  32. #include <linux/async_tx.h>
  33. /* do_async_xor - dma map the pages and perform the xor with an engine */
  34. static __async_inline struct dma_async_tx_descriptor *
  35. do_async_xor(struct dma_chan *chan, struct page *dest, struct page **src_list,
  36. unsigned int offset, int src_cnt, size_t len, dma_addr_t *dma_src,
  37. struct async_submit_ctl *submit)
  38. {
  39. struct dma_device *dma = chan->device;
  40. struct dma_async_tx_descriptor *tx = NULL;
  41. int src_off = 0;
  42. int i;
  43. dma_async_tx_callback cb_fn_orig = submit->cb_fn;
  44. void *cb_param_orig = submit->cb_param;
  45. enum async_tx_flags flags_orig = submit->flags;
  46. enum dma_ctrl_flags dma_flags;
  47. int xor_src_cnt = 0;
  48. dma_addr_t dma_dest;
  49. /* map the dest bidrectional in case it is re-used as a source */
  50. dma_dest = dma_map_page(dma->dev, dest, offset, len, DMA_BIDIRECTIONAL);
  51. for (i = 0; i < src_cnt; i++) {
  52. /* only map the dest once */
  53. if (!src_list[i])
  54. continue;
  55. if (unlikely(src_list[i] == dest)) {
  56. dma_src[xor_src_cnt++] = dma_dest;
  57. continue;
  58. }
  59. dma_src[xor_src_cnt++] = dma_map_page(dma->dev, src_list[i], offset,
  60. len, DMA_TO_DEVICE);
  61. }
  62. src_cnt = xor_src_cnt;
  63. while (src_cnt) {
  64. submit->flags = flags_orig;
  65. dma_flags = 0;
  66. xor_src_cnt = min(src_cnt, (int)dma->max_xor);
  67. /* if we are submitting additional xors, leave the chain open,
  68. * clear the callback parameters, and leave the destination
  69. * buffer mapped
  70. */
  71. if (src_cnt > xor_src_cnt) {
  72. submit->flags &= ~ASYNC_TX_ACK;
  73. submit->flags |= ASYNC_TX_FENCE;
  74. dma_flags = DMA_COMPL_SKIP_DEST_UNMAP;
  75. submit->cb_fn = NULL;
  76. submit->cb_param = NULL;
  77. } else {
  78. submit->cb_fn = cb_fn_orig;
  79. submit->cb_param = cb_param_orig;
  80. }
  81. if (submit->cb_fn)
  82. dma_flags |= DMA_PREP_INTERRUPT;
  83. if (submit->flags & ASYNC_TX_FENCE)
  84. dma_flags |= DMA_PREP_FENCE;
  85. /* Since we have clobbered the src_list we are committed
  86. * to doing this asynchronously. Drivers force forward progress
  87. * in case they can not provide a descriptor
  88. */
  89. tx = dma->device_prep_dma_xor(chan, dma_dest, &dma_src[src_off],
  90. xor_src_cnt, len, dma_flags);
  91. if (unlikely(!tx))
  92. async_tx_quiesce(&submit->depend_tx);
  93. /* spin wait for the preceding transactions to complete */
  94. while (unlikely(!tx)) {
  95. dma_async_issue_pending(chan);
  96. tx = dma->device_prep_dma_xor(chan, dma_dest,
  97. &dma_src[src_off],
  98. xor_src_cnt, len,
  99. dma_flags);
  100. }
  101. async_tx_submit(chan, tx, submit);
  102. submit->depend_tx = tx;
  103. if (src_cnt > xor_src_cnt) {
  104. /* drop completed sources */
  105. src_cnt -= xor_src_cnt;
  106. src_off += xor_src_cnt;
  107. /* use the intermediate result a source */
  108. dma_src[--src_off] = dma_dest;
  109. src_cnt++;
  110. } else
  111. break;
  112. }
  113. return tx;
  114. }
  115. static void
  116. do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset,
  117. int src_cnt, size_t len, struct async_submit_ctl *submit)
  118. {
  119. int i;
  120. int xor_src_cnt = 0;
  121. int src_off = 0;
  122. void *dest_buf;
  123. void **srcs;
  124. if (submit->scribble)
  125. srcs = submit->scribble;
  126. else
  127. srcs = (void **) src_list;
  128. /* convert to buffer pointers */
  129. for (i = 0; i < src_cnt; i++)
  130. if (src_list[i])
  131. srcs[xor_src_cnt++] = page_address(src_list[i]) + offset;
  132. src_cnt = xor_src_cnt;
  133. /* set destination address */
  134. dest_buf = page_address(dest) + offset;
  135. if (submit->flags & ASYNC_TX_XOR_ZERO_DST)
  136. memset(dest_buf, 0, len);
  137. while (src_cnt > 0) {
  138. /* process up to 'MAX_XOR_BLOCKS' sources */
  139. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  140. xor_blocks(xor_src_cnt, len, dest_buf, &srcs[src_off]);
  141. /* drop completed sources */
  142. src_cnt -= xor_src_cnt;
  143. src_off += xor_src_cnt;
  144. }
  145. async_tx_sync_epilog(submit);
  146. }
  147. /**
  148. * async_xor - attempt to xor a set of blocks with a dma engine.
  149. * @dest: destination page
  150. * @src_list: array of source pages
  151. * @offset: common src/dst offset to start transaction
  152. * @src_cnt: number of source pages
  153. * @len: length in bytes
  154. * @submit: submission / completion modifiers
  155. *
  156. * honored flags: ASYNC_TX_ACK, ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DST
  157. *
  158. * xor_blocks always uses the dest as a source so the
  159. * ASYNC_TX_XOR_ZERO_DST flag must be set to not include dest data in
  160. * the calculation. The assumption with dma eninges is that they only
  161. * use the destination buffer as a source when it is explicity specified
  162. * 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(struct page *dest, struct page **src_list, unsigned int offset,
  170. int src_cnt, size_t len, struct async_submit_ctl *submit)
  171. {
  172. struct dma_chan *chan = async_tx_find_channel(submit, DMA_XOR,
  173. &dest, 1, src_list,
  174. src_cnt, len);
  175. dma_addr_t *dma_src = NULL;
  176. BUG_ON(src_cnt <= 1);
  177. if (submit->scribble)
  178. dma_src = submit->scribble;
  179. else if (sizeof(dma_addr_t) <= sizeof(struct page *))
  180. dma_src = (dma_addr_t *) src_list;
  181. if (dma_src && chan && is_dma_xor_aligned(chan->device, offset, 0, len)) {
  182. /* run the xor asynchronously */
  183. pr_debug("%s (async): len: %zu\n", __func__, len);
  184. return do_async_xor(chan, dest, src_list, offset, src_cnt, len,
  185. dma_src, submit);
  186. } else {
  187. /* run the xor synchronously */
  188. pr_debug("%s (sync): len: %zu\n", __func__, len);
  189. WARN_ONCE(chan, "%s: no space for dma address conversion\n",
  190. __func__);
  191. /* in the sync case the dest is an implied source
  192. * (assumes the dest is the first source)
  193. */
  194. if (submit->flags & ASYNC_TX_XOR_DROP_DST) {
  195. src_cnt--;
  196. src_list++;
  197. }
  198. /* wait for any prerequisite operations */
  199. async_tx_quiesce(&submit->depend_tx);
  200. do_sync_xor(dest, src_list, offset, src_cnt, len, submit);
  201. return NULL;
  202. }
  203. }
  204. EXPORT_SYMBOL_GPL(async_xor);
  205. static int page_is_zero(struct page *p, unsigned int offset, size_t len)
  206. {
  207. return !memchr_inv(page_address(p) + offset, 0, len);
  208. }
  209. static inline struct dma_chan *
  210. xor_val_chan(struct async_submit_ctl *submit, struct page *dest,
  211. struct page **src_list, int src_cnt, size_t len)
  212. {
  213. #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  214. return NULL;
  215. #endif
  216. return async_tx_find_channel(submit, DMA_XOR_VAL, &dest, 1, src_list,
  217. src_cnt, len);
  218. }
  219. /**
  220. * async_xor_val - attempt a xor parity check with a dma engine.
  221. * @dest: destination page used if the xor is performed synchronously
  222. * @src_list: array of source pages
  223. * @offset: offset in pages to start transaction
  224. * @src_cnt: number of source pages
  225. * @len: length in bytes
  226. * @result: 0 if sum == 0 else non-zero
  227. * @submit: submission / completion modifiers
  228. *
  229. * honored flags: ASYNC_TX_ACK
  230. *
  231. * src_list note: if the dest is also a source it must be at index zero.
  232. * The contents of this array will be overwritten if a scribble region
  233. * is not specified.
  234. */
  235. struct dma_async_tx_descriptor *
  236. async_xor_val(struct page *dest, struct page **src_list, unsigned int offset,
  237. int src_cnt, size_t len, enum sum_check_flags *result,
  238. struct async_submit_ctl *submit)
  239. {
  240. struct dma_chan *chan = xor_val_chan(submit, dest, src_list, src_cnt, len);
  241. struct dma_device *device = chan ? chan->device : NULL;
  242. struct dma_async_tx_descriptor *tx = NULL;
  243. dma_addr_t *dma_src = NULL;
  244. BUG_ON(src_cnt <= 1);
  245. if (submit->scribble)
  246. dma_src = submit->scribble;
  247. else if (sizeof(dma_addr_t) <= sizeof(struct page *))
  248. dma_src = (dma_addr_t *) src_list;
  249. if (dma_src && device && src_cnt <= device->max_xor &&
  250. is_dma_xor_aligned(device, offset, 0, len)) {
  251. unsigned long dma_prep_flags = 0;
  252. int i;
  253. pr_debug("%s: (async) len: %zu\n", __func__, len);
  254. if (submit->cb_fn)
  255. dma_prep_flags |= DMA_PREP_INTERRUPT;
  256. if (submit->flags & ASYNC_TX_FENCE)
  257. dma_prep_flags |= DMA_PREP_FENCE;
  258. for (i = 0; i < src_cnt; i++)
  259. dma_src[i] = dma_map_page(device->dev, src_list[i],
  260. offset, len, DMA_TO_DEVICE);
  261. tx = device->device_prep_dma_xor_val(chan, dma_src, src_cnt,
  262. len, result,
  263. dma_prep_flags);
  264. if (unlikely(!tx)) {
  265. async_tx_quiesce(&submit->depend_tx);
  266. while (!tx) {
  267. dma_async_issue_pending(chan);
  268. tx = device->device_prep_dma_xor_val(chan,
  269. dma_src, src_cnt, len, result,
  270. dma_prep_flags);
  271. }
  272. }
  273. async_tx_submit(chan, tx, submit);
  274. } else {
  275. enum async_tx_flags flags_orig = submit->flags;
  276. pr_debug("%s: (sync) len: %zu\n", __func__, len);
  277. WARN_ONCE(device && src_cnt <= device->max_xor,
  278. "%s: no space for dma address conversion\n",
  279. __func__);
  280. submit->flags |= ASYNC_TX_XOR_DROP_DST;
  281. submit->flags &= ~ASYNC_TX_ACK;
  282. tx = async_xor(dest, src_list, offset, src_cnt, len, submit);
  283. async_tx_quiesce(&tx);
  284. *result = !page_is_zero(dest, offset, len) << SUM_CHECK_P;
  285. async_tx_sync_epilog(submit);
  286. submit->flags = flags_orig;
  287. }
  288. return tx;
  289. }
  290. EXPORT_SYMBOL_GPL(async_xor_val);
  291. MODULE_AUTHOR("Intel Corporation");
  292. MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api");
  293. MODULE_LICENSE("GPL");