async_xor.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. static void
  33. do_async_xor(struct dma_async_tx_descriptor *tx, struct dma_device *device,
  34. struct dma_chan *chan, struct page *dest, struct page **src_list,
  35. unsigned int offset, unsigned int src_cnt, size_t len,
  36. enum async_tx_flags flags, struct dma_async_tx_descriptor *depend_tx,
  37. dma_async_tx_callback cb_fn, void *cb_param)
  38. {
  39. dma_addr_t dma_addr;
  40. enum dma_data_direction dir;
  41. int i;
  42. pr_debug("%s: len: %zu\n", __FUNCTION__, len);
  43. dir = (flags & ASYNC_TX_ASSUME_COHERENT) ?
  44. DMA_NONE : DMA_FROM_DEVICE;
  45. dma_addr = dma_map_page(device->dev, dest, offset, len, dir);
  46. tx->tx_set_dest(dma_addr, tx, 0);
  47. dir = (flags & ASYNC_TX_ASSUME_COHERENT) ?
  48. DMA_NONE : DMA_TO_DEVICE;
  49. for (i = 0; i < src_cnt; i++) {
  50. dma_addr = dma_map_page(device->dev, src_list[i],
  51. offset, len, dir);
  52. tx->tx_set_src(dma_addr, tx, i);
  53. }
  54. async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
  55. }
  56. static void
  57. do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset,
  58. unsigned int src_cnt, size_t len, enum async_tx_flags flags,
  59. struct dma_async_tx_descriptor *depend_tx,
  60. dma_async_tx_callback cb_fn, void *cb_param)
  61. {
  62. void *_dest;
  63. int i;
  64. pr_debug("%s: len: %zu\n", __FUNCTION__, len);
  65. /* reuse the 'src_list' array to convert to buffer pointers */
  66. for (i = 0; i < src_cnt; i++)
  67. src_list[i] = (struct page *)
  68. (page_address(src_list[i]) + offset);
  69. /* set destination address */
  70. _dest = page_address(dest) + offset;
  71. if (flags & ASYNC_TX_XOR_ZERO_DST)
  72. memset(_dest, 0, len);
  73. xor_blocks(src_cnt, len, _dest,
  74. (void **) src_list);
  75. async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
  76. }
  77. /**
  78. * async_xor - attempt to xor a set of blocks with a dma engine.
  79. * xor_blocks always uses the dest as a source so the ASYNC_TX_XOR_ZERO_DST
  80. * flag must be set to not include dest data in the calculation. The
  81. * assumption with dma eninges is that they only use the destination
  82. * buffer as a source when it is explicity specified in the source list.
  83. * @dest: destination page
  84. * @src_list: array of source pages (if the dest is also a source it must be
  85. * at index zero). The contents of this array may be overwritten.
  86. * @offset: offset in pages to start transaction
  87. * @src_cnt: number of source pages
  88. * @len: length in bytes
  89. * @flags: ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DEST,
  90. * ASYNC_TX_ASSUME_COHERENT, ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
  91. * @depend_tx: xor depends on the result of this transaction.
  92. * @cb_fn: function to call when the xor completes
  93. * @cb_param: parameter to pass to the callback routine
  94. */
  95. struct dma_async_tx_descriptor *
  96. async_xor(struct page *dest, struct page **src_list, unsigned int offset,
  97. int src_cnt, size_t len, enum async_tx_flags flags,
  98. struct dma_async_tx_descriptor *depend_tx,
  99. dma_async_tx_callback cb_fn, void *cb_param)
  100. {
  101. struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_XOR);
  102. struct dma_device *device = chan ? chan->device : NULL;
  103. struct dma_async_tx_descriptor *tx = NULL;
  104. dma_async_tx_callback _cb_fn;
  105. void *_cb_param;
  106. unsigned long local_flags;
  107. int xor_src_cnt;
  108. int i = 0, src_off = 0, int_en;
  109. BUG_ON(src_cnt <= 1);
  110. while (src_cnt) {
  111. local_flags = flags;
  112. if (device) { /* run the xor asynchronously */
  113. xor_src_cnt = min(src_cnt, device->max_xor);
  114. /* if we are submitting additional xors
  115. * only set the callback on the last transaction
  116. */
  117. if (src_cnt > xor_src_cnt) {
  118. local_flags &= ~ASYNC_TX_ACK;
  119. _cb_fn = NULL;
  120. _cb_param = NULL;
  121. } else {
  122. _cb_fn = cb_fn;
  123. _cb_param = cb_param;
  124. }
  125. int_en = _cb_fn ? 1 : 0;
  126. tx = device->device_prep_dma_xor(
  127. chan, xor_src_cnt, len, int_en);
  128. if (tx) {
  129. do_async_xor(tx, device, chan, dest,
  130. &src_list[src_off], offset, xor_src_cnt, len,
  131. local_flags, depend_tx, _cb_fn,
  132. _cb_param);
  133. } else /* fall through */
  134. goto xor_sync;
  135. } else { /* run the xor synchronously */
  136. xor_sync:
  137. /* in the sync case the dest is an implied source
  138. * (assumes the dest is at the src_off index)
  139. */
  140. if (flags & ASYNC_TX_XOR_DROP_DST) {
  141. src_cnt--;
  142. src_off++;
  143. }
  144. /* process up to 'MAX_XOR_BLOCKS' sources */
  145. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  146. /* if we are submitting additional xors
  147. * only set the callback on the last transaction
  148. */
  149. if (src_cnt > xor_src_cnt) {
  150. local_flags &= ~ASYNC_TX_ACK;
  151. _cb_fn = NULL;
  152. _cb_param = NULL;
  153. } else {
  154. _cb_fn = cb_fn;
  155. _cb_param = cb_param;
  156. }
  157. /* wait for any prerequisite operations */
  158. if (depend_tx) {
  159. /* if ack is already set then we cannot be sure
  160. * we are referring to the correct operation
  161. */
  162. BUG_ON(depend_tx->ack);
  163. if (dma_wait_for_async_tx(depend_tx) ==
  164. DMA_ERROR)
  165. panic("%s: DMA_ERROR waiting for "
  166. "depend_tx\n",
  167. __FUNCTION__);
  168. }
  169. do_sync_xor(dest, &src_list[src_off], offset,
  170. xor_src_cnt, len, local_flags, depend_tx,
  171. _cb_fn, _cb_param);
  172. }
  173. /* the previous tx is hidden from the client,
  174. * so ack it
  175. */
  176. if (i && depend_tx)
  177. async_tx_ack(depend_tx);
  178. depend_tx = tx;
  179. if (src_cnt > xor_src_cnt) {
  180. /* drop completed sources */
  181. src_cnt -= xor_src_cnt;
  182. src_off += xor_src_cnt;
  183. /* unconditionally preserve the destination */
  184. flags &= ~ASYNC_TX_XOR_ZERO_DST;
  185. /* use the intermediate result a source, but remember
  186. * it's dropped, because it's implied, in the sync case
  187. */
  188. src_list[--src_off] = dest;
  189. src_cnt++;
  190. flags |= ASYNC_TX_XOR_DROP_DST;
  191. } else
  192. src_cnt = 0;
  193. i++;
  194. }
  195. return tx;
  196. }
  197. EXPORT_SYMBOL_GPL(async_xor);
  198. static int page_is_zero(struct page *p, unsigned int offset, size_t len)
  199. {
  200. char *a = page_address(p) + offset;
  201. return ((*(u32 *) a) == 0 &&
  202. memcmp(a, a + 4, len - 4) == 0);
  203. }
  204. /**
  205. * async_xor_zero_sum - attempt a xor parity check with a dma engine.
  206. * @dest: destination page used if the xor is performed synchronously
  207. * @src_list: array of source pages. The dest page must be listed as a source
  208. * at index zero. The contents of this array may be overwritten.
  209. * @offset: offset in pages to start transaction
  210. * @src_cnt: number of source pages
  211. * @len: length in bytes
  212. * @result: 0 if sum == 0 else non-zero
  213. * @flags: ASYNC_TX_ASSUME_COHERENT, ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
  214. * @depend_tx: xor depends on the result of this transaction.
  215. * @cb_fn: function to call when the xor completes
  216. * @cb_param: parameter to pass to the callback routine
  217. */
  218. struct dma_async_tx_descriptor *
  219. async_xor_zero_sum(struct page *dest, struct page **src_list,
  220. unsigned int offset, int src_cnt, size_t len,
  221. u32 *result, enum async_tx_flags flags,
  222. struct dma_async_tx_descriptor *depend_tx,
  223. dma_async_tx_callback cb_fn, void *cb_param)
  224. {
  225. struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_ZERO_SUM);
  226. struct dma_device *device = chan ? chan->device : NULL;
  227. int int_en = cb_fn ? 1 : 0;
  228. struct dma_async_tx_descriptor *tx = device ?
  229. device->device_prep_dma_zero_sum(chan, src_cnt, len, result,
  230. int_en) : NULL;
  231. int i;
  232. BUG_ON(src_cnt <= 1);
  233. if (tx) {
  234. dma_addr_t dma_addr;
  235. enum dma_data_direction dir;
  236. pr_debug("%s: (async) len: %zu\n", __FUNCTION__, len);
  237. dir = (flags & ASYNC_TX_ASSUME_COHERENT) ?
  238. DMA_NONE : DMA_TO_DEVICE;
  239. for (i = 0; i < src_cnt; i++) {
  240. dma_addr = dma_map_page(device->dev, src_list[i],
  241. offset, len, dir);
  242. tx->tx_set_src(dma_addr, tx, i);
  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", __FUNCTION__, 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. if (tx) {
  253. if (dma_wait_for_async_tx(tx) == DMA_ERROR)
  254. panic("%s: DMA_ERROR waiting for tx\n",
  255. __FUNCTION__);
  256. async_tx_ack(tx);
  257. }
  258. *result = page_is_zero(dest, offset, len) ? 0 : 1;
  259. tx = NULL;
  260. async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
  261. }
  262. return tx;
  263. }
  264. EXPORT_SYMBOL_GPL(async_xor_zero_sum);
  265. static int __init async_xor_init(void)
  266. {
  267. return 0;
  268. }
  269. static void __exit async_xor_exit(void)
  270. {
  271. do { } while (0);
  272. }
  273. module_init(async_xor_init);
  274. module_exit(async_xor_exit);
  275. MODULE_AUTHOR("Intel Corporation");
  276. MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api");
  277. MODULE_LICENSE("GPL");