async_xor.c 10 KB

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