async_xor.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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/mm.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/raid/xor.h>
  31. #include <linux/async_tx.h>
  32. /* do_async_xor - dma map the pages and perform the xor with an engine.
  33. * This routine is marked __always_inline so it can be compiled away
  34. * when CONFIG_DMA_ENGINE=n
  35. */
  36. static __always_inline struct dma_async_tx_descriptor *
  37. do_async_xor(struct dma_chan *chan, struct page *dest, struct page **src_list,
  38. unsigned int offset, int src_cnt, size_t len,
  39. enum async_tx_flags flags,
  40. struct dma_async_tx_descriptor *depend_tx,
  41. dma_async_tx_callback cb_fn, void *cb_param)
  42. {
  43. struct dma_device *dma = chan->device;
  44. dma_addr_t *dma_src = (dma_addr_t *) src_list;
  45. struct dma_async_tx_descriptor *tx = NULL;
  46. int src_off = 0;
  47. int i;
  48. dma_async_tx_callback _cb_fn;
  49. void *_cb_param;
  50. enum async_tx_flags async_flags;
  51. enum dma_ctrl_flags dma_flags;
  52. int xor_src_cnt;
  53. dma_addr_t dma_dest;
  54. dma_dest = dma_map_page(dma->dev, dest, offset, len, DMA_FROM_DEVICE);
  55. for (i = 0; i < src_cnt; i++)
  56. dma_src[i] = dma_map_page(dma->dev, src_list[i], offset,
  57. len, DMA_TO_DEVICE);
  58. while (src_cnt) {
  59. async_flags = flags;
  60. dma_flags = 0;
  61. xor_src_cnt = min(src_cnt, dma->max_xor);
  62. /* if we are submitting additional xors, leave the chain open,
  63. * clear the callback parameters, and leave the destination
  64. * buffer mapped
  65. */
  66. if (src_cnt > xor_src_cnt) {
  67. async_flags &= ~ASYNC_TX_ACK;
  68. dma_flags = DMA_COMPL_SKIP_DEST_UNMAP;
  69. _cb_fn = NULL;
  70. _cb_param = NULL;
  71. } else {
  72. _cb_fn = cb_fn;
  73. _cb_param = cb_param;
  74. }
  75. if (_cb_fn)
  76. dma_flags |= DMA_PREP_INTERRUPT;
  77. /* Since we have clobbered the src_list we are committed
  78. * to doing this asynchronously. Drivers force forward progress
  79. * in case they can not provide a descriptor
  80. */
  81. tx = dma->device_prep_dma_xor(chan, dma_dest, &dma_src[src_off],
  82. xor_src_cnt, len, dma_flags);
  83. if (unlikely(!tx))
  84. async_tx_quiesce(&depend_tx);
  85. /* spin wait for the preceeding transactions to complete */
  86. while (unlikely(!tx)) {
  87. dma_async_issue_pending(chan);
  88. tx = dma->device_prep_dma_xor(chan, dma_dest,
  89. &dma_src[src_off],
  90. xor_src_cnt, len,
  91. dma_flags);
  92. }
  93. async_tx_submit(chan, tx, async_flags, depend_tx, _cb_fn,
  94. _cb_param);
  95. depend_tx = tx;
  96. flags |= ASYNC_TX_DEP_ACK;
  97. if (src_cnt > xor_src_cnt) {
  98. /* drop completed sources */
  99. src_cnt -= xor_src_cnt;
  100. src_off += xor_src_cnt;
  101. /* use the intermediate result a source */
  102. dma_src[--src_off] = dma_dest;
  103. src_cnt++;
  104. } else
  105. break;
  106. }
  107. return tx;
  108. }
  109. static void
  110. do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset,
  111. int src_cnt, size_t len, enum async_tx_flags flags,
  112. struct dma_async_tx_descriptor *depend_tx,
  113. dma_async_tx_callback cb_fn, void *cb_param)
  114. {
  115. int i;
  116. int xor_src_cnt;
  117. int src_off = 0;
  118. void *dest_buf;
  119. void **srcs = (void **) src_list;
  120. /* reuse the 'src_list' array to convert to buffer pointers */
  121. for (i = 0; i < src_cnt; i++)
  122. srcs[i] = page_address(src_list[i]) + offset;
  123. /* set destination address */
  124. dest_buf = page_address(dest) + offset;
  125. if (flags & ASYNC_TX_XOR_ZERO_DST)
  126. memset(dest_buf, 0, len);
  127. while (src_cnt > 0) {
  128. /* process up to 'MAX_XOR_BLOCKS' sources */
  129. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  130. xor_blocks(xor_src_cnt, len, dest_buf, &srcs[src_off]);
  131. /* drop completed sources */
  132. src_cnt -= xor_src_cnt;
  133. src_off += xor_src_cnt;
  134. }
  135. async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
  136. }
  137. /**
  138. * async_xor - attempt to xor a set of blocks with a dma engine.
  139. * xor_blocks always uses the dest as a source so the ASYNC_TX_XOR_ZERO_DST
  140. * flag must be set to not include dest data in the calculation. The
  141. * assumption with dma eninges is that they only use the destination
  142. * buffer as a source when it is explicity specified in the source list.
  143. * @dest: destination page
  144. * @src_list: array of source pages (if the dest is also a source it must be
  145. * at index zero). The contents of this array may be overwritten.
  146. * @offset: offset in pages to start transaction
  147. * @src_cnt: number of source pages
  148. * @len: length in bytes
  149. * @flags: ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DEST,
  150. * ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
  151. * @depend_tx: xor depends on the result of this transaction.
  152. * @cb_fn: function to call when the xor completes
  153. * @cb_param: parameter to pass to the callback routine
  154. */
  155. struct dma_async_tx_descriptor *
  156. async_xor(struct page *dest, struct page **src_list, unsigned int offset,
  157. int src_cnt, size_t len, enum async_tx_flags flags,
  158. struct dma_async_tx_descriptor *depend_tx,
  159. dma_async_tx_callback cb_fn, void *cb_param)
  160. {
  161. struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_XOR,
  162. &dest, 1, src_list,
  163. src_cnt, len);
  164. BUG_ON(src_cnt <= 1);
  165. if (chan) {
  166. /* run the xor asynchronously */
  167. pr_debug("%s (async): len: %zu\n", __func__, len);
  168. return do_async_xor(chan, dest, src_list, offset, src_cnt, len,
  169. flags, depend_tx, cb_fn, cb_param);
  170. } else {
  171. /* run the xor synchronously */
  172. pr_debug("%s (sync): len: %zu\n", __func__, len);
  173. /* in the sync case the dest is an implied source
  174. * (assumes the dest is the first source)
  175. */
  176. if (flags & ASYNC_TX_XOR_DROP_DST) {
  177. src_cnt--;
  178. src_list++;
  179. }
  180. /* wait for any prerequisite operations */
  181. async_tx_quiesce(&depend_tx);
  182. do_sync_xor(dest, src_list, offset, src_cnt, len,
  183. flags, depend_tx, cb_fn, cb_param);
  184. return NULL;
  185. }
  186. }
  187. EXPORT_SYMBOL_GPL(async_xor);
  188. static int page_is_zero(struct page *p, unsigned int offset, size_t len)
  189. {
  190. char *a = page_address(p) + offset;
  191. return ((*(u32 *) a) == 0 &&
  192. memcmp(a, a + 4, len - 4) == 0);
  193. }
  194. /**
  195. * async_xor_zero_sum - attempt a xor parity check with a dma engine.
  196. * @dest: destination page used if the xor is performed synchronously
  197. * @src_list: array of source pages. The dest page must be listed as a source
  198. * at index zero. The contents of this array may be overwritten.
  199. * @offset: offset in pages to start transaction
  200. * @src_cnt: number of source pages
  201. * @len: length in bytes
  202. * @result: 0 if sum == 0 else non-zero
  203. * @flags: ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
  204. * @depend_tx: xor depends on the result of this transaction.
  205. * @cb_fn: function to call when the xor completes
  206. * @cb_param: parameter to pass to the callback routine
  207. */
  208. struct dma_async_tx_descriptor *
  209. async_xor_zero_sum(struct page *dest, struct page **src_list,
  210. unsigned int offset, int src_cnt, size_t len,
  211. u32 *result, enum async_tx_flags flags,
  212. struct dma_async_tx_descriptor *depend_tx,
  213. dma_async_tx_callback cb_fn, void *cb_param)
  214. {
  215. struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_ZERO_SUM,
  216. &dest, 1, src_list,
  217. src_cnt, len);
  218. struct dma_device *device = chan ? chan->device : NULL;
  219. struct dma_async_tx_descriptor *tx = NULL;
  220. BUG_ON(src_cnt <= 1);
  221. if (device && src_cnt <= device->max_xor) {
  222. dma_addr_t *dma_src = (dma_addr_t *) src_list;
  223. unsigned long dma_prep_flags = cb_fn ? DMA_PREP_INTERRUPT : 0;
  224. int i;
  225. pr_debug("%s: (async) len: %zu\n", __func__, len);
  226. for (i = 0; i < src_cnt; i++)
  227. dma_src[i] = dma_map_page(device->dev, src_list[i],
  228. offset, len, DMA_TO_DEVICE);
  229. tx = device->device_prep_dma_zero_sum(chan, dma_src, src_cnt,
  230. len, result,
  231. dma_prep_flags);
  232. if (unlikely(!tx)) {
  233. async_tx_quiesce(&depend_tx);
  234. while (!tx)
  235. dma_async_issue_pending(chan);
  236. tx = device->device_prep_dma_zero_sum(chan,
  237. dma_src, src_cnt, len, result,
  238. dma_prep_flags);
  239. }
  240. async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
  241. } else {
  242. unsigned long xor_flags = flags;
  243. pr_debug("%s: (sync) len: %zu\n", __func__, len);
  244. xor_flags |= ASYNC_TX_XOR_DROP_DST;
  245. xor_flags &= ~ASYNC_TX_ACK;
  246. tx = async_xor(dest, src_list, offset, src_cnt, len, xor_flags,
  247. depend_tx, NULL, NULL);
  248. async_tx_quiesce(&tx);
  249. *result = page_is_zero(dest, offset, len) ? 0 : 1;
  250. async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
  251. }
  252. return tx;
  253. }
  254. EXPORT_SYMBOL_GPL(async_xor_zero_sum);
  255. static int __init async_xor_init(void)
  256. {
  257. #ifdef CONFIG_DMA_ENGINE
  258. /* To conserve stack space the input src_list (array of page pointers)
  259. * is reused to hold the array of dma addresses passed to the driver.
  260. * This conversion is only possible when dma_addr_t is less than the
  261. * the size of a pointer. HIGHMEM64G is known to violate this
  262. * assumption.
  263. */
  264. BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(struct page *));
  265. #endif
  266. return 0;
  267. }
  268. static void __exit async_xor_exit(void)
  269. {
  270. do { } while (0);
  271. }
  272. module_init(async_xor_init);
  273. module_exit(async_xor_exit);
  274. MODULE_AUTHOR("Intel Corporation");
  275. MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api");
  276. MODULE_LICENSE("GPL");