async_xor.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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, (int)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. submit->flags |= ASYNC_TX_FENCE;
  70. dma_flags = DMA_COMPL_SKIP_DEST_UNMAP;
  71. submit->cb_fn = NULL;
  72. submit->cb_param = NULL;
  73. } else {
  74. submit->cb_fn = cb_fn_orig;
  75. submit->cb_param = cb_param_orig;
  76. }
  77. if (submit->cb_fn)
  78. dma_flags |= DMA_PREP_INTERRUPT;
  79. if (submit->flags & ASYNC_TX_FENCE)
  80. dma_flags |= DMA_PREP_FENCE;
  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(&submit->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, submit);
  98. submit->depend_tx = tx;
  99. if (src_cnt > xor_src_cnt) {
  100. /* drop completed sources */
  101. src_cnt -= xor_src_cnt;
  102. src_off += xor_src_cnt;
  103. /* use the intermediate result a source */
  104. dma_src[--src_off] = dma_dest;
  105. src_cnt++;
  106. } else
  107. break;
  108. }
  109. return tx;
  110. }
  111. static void
  112. do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset,
  113. int src_cnt, size_t len, struct async_submit_ctl *submit)
  114. {
  115. int i;
  116. int xor_src_cnt;
  117. int src_off = 0;
  118. void *dest_buf;
  119. void **srcs;
  120. if (submit->scribble)
  121. srcs = submit->scribble;
  122. else
  123. srcs = (void **) src_list;
  124. /* convert to buffer pointers */
  125. for (i = 0; i < src_cnt; i++)
  126. srcs[i] = page_address(src_list[i]) + offset;
  127. /* set destination address */
  128. dest_buf = page_address(dest) + offset;
  129. if (submit->flags & ASYNC_TX_XOR_ZERO_DST)
  130. memset(dest_buf, 0, len);
  131. while (src_cnt > 0) {
  132. /* process up to 'MAX_XOR_BLOCKS' sources */
  133. xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
  134. xor_blocks(xor_src_cnt, len, dest_buf, &srcs[src_off]);
  135. /* drop completed sources */
  136. src_cnt -= xor_src_cnt;
  137. src_off += xor_src_cnt;
  138. }
  139. async_tx_sync_epilog(submit);
  140. }
  141. /**
  142. * async_xor - attempt to xor a set of blocks with a dma engine.
  143. * @dest: destination page
  144. * @src_list: array of source pages
  145. * @offset: common src/dst offset to start transaction
  146. * @src_cnt: number of source pages
  147. * @len: length in bytes
  148. * @submit: submission / completion modifiers
  149. *
  150. * honored flags: ASYNC_TX_ACK, ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DST
  151. *
  152. * xor_blocks always uses the dest as a source so the
  153. * ASYNC_TX_XOR_ZERO_DST flag must be set to not include dest data in
  154. * the calculation. The assumption with dma eninges is that they only
  155. * use the destination buffer as a source when it is explicity specified
  156. * in the source list.
  157. *
  158. * src_list note: if the dest is also a source it must be at index zero.
  159. * The contents of this array will be overwritten if a scribble region
  160. * is not specified.
  161. */
  162. struct dma_async_tx_descriptor *
  163. async_xor(struct page *dest, struct page **src_list, unsigned int offset,
  164. int src_cnt, size_t len, struct async_submit_ctl *submit)
  165. {
  166. struct dma_chan *chan = async_tx_find_channel(submit, DMA_XOR,
  167. &dest, 1, src_list,
  168. src_cnt, len);
  169. dma_addr_t *dma_src = NULL;
  170. BUG_ON(src_cnt <= 1);
  171. if (submit->scribble)
  172. dma_src = submit->scribble;
  173. else if (sizeof(dma_addr_t) <= sizeof(struct page *))
  174. dma_src = (dma_addr_t *) src_list;
  175. if (dma_src && chan && is_dma_xor_aligned(chan->device, offset, 0, len)) {
  176. /* run the xor asynchronously */
  177. pr_debug("%s (async): len: %zu\n", __func__, len);
  178. return do_async_xor(chan, dest, src_list, offset, src_cnt, len,
  179. dma_src, submit);
  180. } else {
  181. /* run the xor synchronously */
  182. pr_debug("%s (sync): len: %zu\n", __func__, len);
  183. WARN_ONCE(chan, "%s: no space for dma address conversion\n",
  184. __func__);
  185. /* in the sync case the dest is an implied source
  186. * (assumes the dest is the first source)
  187. */
  188. if (submit->flags & ASYNC_TX_XOR_DROP_DST) {
  189. src_cnt--;
  190. src_list++;
  191. }
  192. /* wait for any prerequisite operations */
  193. async_tx_quiesce(&submit->depend_tx);
  194. do_sync_xor(dest, src_list, offset, src_cnt, len, submit);
  195. return NULL;
  196. }
  197. }
  198. EXPORT_SYMBOL_GPL(async_xor);
  199. static int page_is_zero(struct page *p, unsigned int offset, size_t len)
  200. {
  201. char *a = page_address(p) + offset;
  202. return ((*(u32 *) a) == 0 &&
  203. memcmp(a, a + 4, len - 4) == 0);
  204. }
  205. /**
  206. * async_xor_val - attempt a xor parity check with a dma engine.
  207. * @dest: destination page used if the xor is performed synchronously
  208. * @src_list: array of source pages
  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. * @submit: submission / completion modifiers
  214. *
  215. * honored flags: ASYNC_TX_ACK
  216. *
  217. * src_list note: if the dest is also a source it must be at index zero.
  218. * The contents of this array will be overwritten if a scribble region
  219. * is not specified.
  220. */
  221. struct dma_async_tx_descriptor *
  222. async_xor_val(struct page *dest, struct page **src_list, unsigned int offset,
  223. int src_cnt, size_t len, enum sum_check_flags *result,
  224. struct async_submit_ctl *submit)
  225. {
  226. struct dma_chan *chan = async_tx_find_channel(submit, DMA_XOR_VAL,
  227. &dest, 1, src_list,
  228. src_cnt, len);
  229. struct dma_device *device = chan ? chan->device : NULL;
  230. struct dma_async_tx_descriptor *tx = NULL;
  231. dma_addr_t *dma_src = NULL;
  232. BUG_ON(src_cnt <= 1);
  233. if (submit->scribble)
  234. dma_src = submit->scribble;
  235. else if (sizeof(dma_addr_t) <= sizeof(struct page *))
  236. dma_src = (dma_addr_t *) src_list;
  237. if (dma_src && device && src_cnt <= device->max_xor &&
  238. is_dma_xor_aligned(device, offset, 0, len)) {
  239. unsigned long dma_prep_flags = 0;
  240. int i;
  241. pr_debug("%s: (async) len: %zu\n", __func__, len);
  242. if (submit->cb_fn)
  243. dma_prep_flags |= DMA_PREP_INTERRUPT;
  244. if (submit->flags & ASYNC_TX_FENCE)
  245. dma_prep_flags |= DMA_PREP_FENCE;
  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_xor_val(chan, dma_src, src_cnt,
  250. len, result,
  251. dma_prep_flags);
  252. if (unlikely(!tx)) {
  253. async_tx_quiesce(&submit->depend_tx);
  254. while (!tx) {
  255. dma_async_issue_pending(chan);
  256. tx = device->device_prep_dma_xor_val(chan,
  257. dma_src, src_cnt, len, result,
  258. dma_prep_flags);
  259. }
  260. }
  261. async_tx_submit(chan, tx, submit);
  262. } else {
  263. enum async_tx_flags flags_orig = submit->flags;
  264. pr_debug("%s: (sync) len: %zu\n", __func__, len);
  265. WARN_ONCE(device && src_cnt <= device->max_xor,
  266. "%s: no space for dma address conversion\n",
  267. __func__);
  268. submit->flags |= ASYNC_TX_XOR_DROP_DST;
  269. submit->flags &= ~ASYNC_TX_ACK;
  270. tx = async_xor(dest, src_list, offset, src_cnt, len, submit);
  271. async_tx_quiesce(&tx);
  272. *result = !page_is_zero(dest, offset, len) << SUM_CHECK_P;
  273. async_tx_sync_epilog(submit);
  274. submit->flags = flags_orig;
  275. }
  276. return tx;
  277. }
  278. EXPORT_SYMBOL_GPL(async_xor_val);
  279. MODULE_AUTHOR("Intel Corporation");
  280. MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api");
  281. MODULE_LICENSE("GPL");