async_xor.c 9.8 KB

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