async_xor.c 9.8 KB

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