async_xor.c 9.6 KB

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