async_xor.c 9.1 KB

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