async_xor.c 9.8 KB

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