async_xor.c 10 KB

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