async_pq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright(c) 2007 Yuri Tikhonov <[email protected]>
  4. * Copyright(c) 2009 Intel Corporation
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/raid/pq.h>
  11. #include <linux/async_tx.h>
  12. #include <linux/gfp.h>
  13. /**
  14. * pq_scribble_page - space to hold throwaway P or Q buffer for
  15. * synchronous gen_syndrome
  16. */
  17. static struct page *pq_scribble_page;
  18. /* the struct page *blocks[] parameter passed to async_gen_syndrome()
  19. * and async_syndrome_val() contains the 'P' destination address at
  20. * blocks[disks-2] and the 'Q' destination address at blocks[disks-1]
  21. *
  22. * note: these are macros as they are used as lvalues
  23. */
  24. #define P(b, d) (b[d-2])
  25. #define Q(b, d) (b[d-1])
  26. #define MAX_DISKS 255
  27. /**
  28. * do_async_gen_syndrome - asynchronously calculate P and/or Q
  29. */
  30. static __async_inline struct dma_async_tx_descriptor *
  31. do_async_gen_syndrome(struct dma_chan *chan,
  32. const unsigned char *scfs, int disks,
  33. struct dmaengine_unmap_data *unmap,
  34. enum dma_ctrl_flags dma_flags,
  35. struct async_submit_ctl *submit)
  36. {
  37. struct dma_async_tx_descriptor *tx = NULL;
  38. struct dma_device *dma = chan->device;
  39. enum async_tx_flags flags_orig = submit->flags;
  40. dma_async_tx_callback cb_fn_orig = submit->cb_fn;
  41. dma_async_tx_callback cb_param_orig = submit->cb_param;
  42. int src_cnt = disks - 2;
  43. unsigned short pq_src_cnt;
  44. dma_addr_t dma_dest[2];
  45. int src_off = 0;
  46. while (src_cnt > 0) {
  47. submit->flags = flags_orig;
  48. pq_src_cnt = min(src_cnt, dma_maxpq(dma, dma_flags));
  49. /* if we are submitting additional pqs, leave the chain open,
  50. * clear the callback parameters, and leave the destination
  51. * buffers mapped
  52. */
  53. if (src_cnt > pq_src_cnt) {
  54. submit->flags &= ~ASYNC_TX_ACK;
  55. submit->flags |= ASYNC_TX_FENCE;
  56. submit->cb_fn = NULL;
  57. submit->cb_param = NULL;
  58. } else {
  59. submit->cb_fn = cb_fn_orig;
  60. submit->cb_param = cb_param_orig;
  61. if (cb_fn_orig)
  62. dma_flags |= DMA_PREP_INTERRUPT;
  63. }
  64. if (submit->flags & ASYNC_TX_FENCE)
  65. dma_flags |= DMA_PREP_FENCE;
  66. /* Drivers force forward progress in case they can not provide
  67. * a descriptor
  68. */
  69. for (;;) {
  70. dma_dest[0] = unmap->addr[disks - 2];
  71. dma_dest[1] = unmap->addr[disks - 1];
  72. tx = dma->device_prep_dma_pq(chan, dma_dest,
  73. &unmap->addr[src_off],
  74. pq_src_cnt,
  75. &scfs[src_off], unmap->len,
  76. dma_flags);
  77. if (likely(tx))
  78. break;
  79. async_tx_quiesce(&submit->depend_tx);
  80. dma_async_issue_pending(chan);
  81. }
  82. dma_set_unmap(tx, unmap);
  83. async_tx_submit(chan, tx, submit);
  84. submit->depend_tx = tx;
  85. /* drop completed sources */
  86. src_cnt -= pq_src_cnt;
  87. src_off += pq_src_cnt;
  88. dma_flags |= DMA_PREP_CONTINUE;
  89. }
  90. return tx;
  91. }
  92. /**
  93. * do_sync_gen_syndrome - synchronously calculate a raid6 syndrome
  94. */
  95. static void
  96. do_sync_gen_syndrome(struct page **blocks, unsigned int *offsets, int disks,
  97. size_t len, struct async_submit_ctl *submit)
  98. {
  99. void **srcs;
  100. int i;
  101. int start = -1, stop = disks - 3;
  102. if (submit->scribble)
  103. srcs = submit->scribble;
  104. else
  105. srcs = (void **) blocks;
  106. for (i = 0; i < disks; i++) {
  107. if (blocks[i] == NULL) {
  108. BUG_ON(i > disks - 3); /* P or Q can't be zero */
  109. srcs[i] = (void*)raid6_empty_zero_page;
  110. } else {
  111. srcs[i] = page_address(blocks[i]) + offsets[i];
  112. if (i < disks - 2) {
  113. stop = i;
  114. if (start == -1)
  115. start = i;
  116. }
  117. }
  118. }
  119. if (submit->flags & ASYNC_TX_PQ_XOR_DST) {
  120. BUG_ON(!raid6_call.xor_syndrome);
  121. if (start >= 0)
  122. raid6_call.xor_syndrome(disks, start, stop, len, srcs);
  123. } else
  124. raid6_call.gen_syndrome(disks, len, srcs);
  125. async_tx_sync_epilog(submit);
  126. }
  127. static inline bool
  128. is_dma_pq_aligned_offs(struct dma_device *dev, unsigned int *offs,
  129. int src_cnt, size_t len)
  130. {
  131. int i;
  132. for (i = 0; i < src_cnt; i++) {
  133. if (!is_dma_pq_aligned(dev, offs[i], 0, len))
  134. return false;
  135. }
  136. return true;
  137. }
  138. /**
  139. * async_gen_syndrome - asynchronously calculate a raid6 syndrome
  140. * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
  141. * @offsets: offset array into each block (src and dest) to start transaction
  142. * @disks: number of blocks (including missing P or Q, see below)
  143. * @len: length of operation in bytes
  144. * @submit: submission/completion modifiers
  145. *
  146. * General note: This routine assumes a field of GF(2^8) with a
  147. * primitive polynomial of 0x11d and a generator of {02}.
  148. *
  149. * 'disks' note: callers can optionally omit either P or Q (but not
  150. * both) from the calculation by setting blocks[disks-2] or
  151. * blocks[disks-1] to NULL. When P or Q is omitted 'len' must be <=
  152. * PAGE_SIZE as a temporary buffer of this size is used in the
  153. * synchronous path. 'disks' always accounts for both destination
  154. * buffers. If any source buffers (blocks[i] where i < disks - 2) are
  155. * set to NULL those buffers will be replaced with the raid6_zero_page
  156. * in the synchronous path and omitted in the hardware-asynchronous
  157. * path.
  158. */
  159. struct dma_async_tx_descriptor *
  160. async_gen_syndrome(struct page **blocks, unsigned int *offsets, int disks,
  161. size_t len, struct async_submit_ctl *submit)
  162. {
  163. int src_cnt = disks - 2;
  164. struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
  165. &P(blocks, disks), 2,
  166. blocks, src_cnt, len);
  167. struct dma_device *device = chan ? chan->device : NULL;
  168. struct dmaengine_unmap_data *unmap = NULL;
  169. BUG_ON(disks > MAX_DISKS || !(P(blocks, disks) || Q(blocks, disks)));
  170. if (device)
  171. unmap = dmaengine_get_unmap_data(device->dev, disks, GFP_NOWAIT);
  172. /* XORing P/Q is only implemented in software */
  173. if (unmap && !(submit->flags & ASYNC_TX_PQ_XOR_DST) &&
  174. (src_cnt <= dma_maxpq(device, 0) ||
  175. dma_maxpq(device, DMA_PREP_CONTINUE) > 0) &&
  176. is_dma_pq_aligned_offs(device, offsets, disks, len)) {
  177. struct dma_async_tx_descriptor *tx;
  178. enum dma_ctrl_flags dma_flags = 0;
  179. unsigned char coefs[MAX_DISKS];
  180. int i, j;
  181. /* run the p+q asynchronously */
  182. pr_debug("%s: (async) disks: %d len: %zu\n",
  183. __func__, disks, len);
  184. /* convert source addresses being careful to collapse 'empty'
  185. * sources and update the coefficients accordingly
  186. */
  187. unmap->len = len;
  188. for (i = 0, j = 0; i < src_cnt; i++) {
  189. if (blocks[i] == NULL)
  190. continue;
  191. unmap->addr[j] = dma_map_page(device->dev, blocks[i],
  192. offsets[i], len, DMA_TO_DEVICE);
  193. coefs[j] = raid6_gfexp[i];
  194. unmap->to_cnt++;
  195. j++;
  196. }
  197. /*
  198. * DMAs use destinations as sources,
  199. * so use BIDIRECTIONAL mapping
  200. */
  201. unmap->bidi_cnt++;
  202. if (P(blocks, disks))
  203. unmap->addr[j++] = dma_map_page(device->dev, P(blocks, disks),
  204. P(offsets, disks),
  205. len, DMA_BIDIRECTIONAL);
  206. else {
  207. unmap->addr[j++] = 0;
  208. dma_flags |= DMA_PREP_PQ_DISABLE_P;
  209. }
  210. unmap->bidi_cnt++;
  211. if (Q(blocks, disks))
  212. unmap->addr[j++] = dma_map_page(device->dev, Q(blocks, disks),
  213. Q(offsets, disks),
  214. len, DMA_BIDIRECTIONAL);
  215. else {
  216. unmap->addr[j++] = 0;
  217. dma_flags |= DMA_PREP_PQ_DISABLE_Q;
  218. }
  219. tx = do_async_gen_syndrome(chan, coefs, j, unmap, dma_flags, submit);
  220. dmaengine_unmap_put(unmap);
  221. return tx;
  222. }
  223. dmaengine_unmap_put(unmap);
  224. /* run the pq synchronously */
  225. pr_debug("%s: (sync) disks: %d len: %zu\n", __func__, disks, len);
  226. /* wait for any prerequisite operations */
  227. async_tx_quiesce(&submit->depend_tx);
  228. if (!P(blocks, disks)) {
  229. P(blocks, disks) = pq_scribble_page;
  230. P(offsets, disks) = 0;
  231. }
  232. if (!Q(blocks, disks)) {
  233. Q(blocks, disks) = pq_scribble_page;
  234. Q(offsets, disks) = 0;
  235. }
  236. do_sync_gen_syndrome(blocks, offsets, disks, len, submit);
  237. return NULL;
  238. }
  239. EXPORT_SYMBOL_GPL(async_gen_syndrome);
  240. static inline struct dma_chan *
  241. pq_val_chan(struct async_submit_ctl *submit, struct page **blocks, int disks, size_t len)
  242. {
  243. #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  244. return NULL;
  245. #endif
  246. return async_tx_find_channel(submit, DMA_PQ_VAL, NULL, 0, blocks,
  247. disks, len);
  248. }
  249. /**
  250. * async_syndrome_val - asynchronously validate a raid6 syndrome
  251. * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
  252. * @offset: common offset into each block (src and dest) to start transaction
  253. * @disks: number of blocks (including missing P or Q, see below)
  254. * @len: length of operation in bytes
  255. * @pqres: on val failure SUM_CHECK_P_RESULT and/or SUM_CHECK_Q_RESULT are set
  256. * @spare: temporary result buffer for the synchronous case
  257. * @s_off: spare buffer page offset
  258. * @submit: submission / completion modifiers
  259. *
  260. * The same notes from async_gen_syndrome apply to the 'blocks',
  261. * and 'disks' parameters of this routine. The synchronous path
  262. * requires a temporary result buffer and submit->scribble to be
  263. * specified.
  264. */
  265. struct dma_async_tx_descriptor *
  266. async_syndrome_val(struct page **blocks, unsigned int *offsets, int disks,
  267. size_t len, enum sum_check_flags *pqres, struct page *spare,
  268. unsigned int s_off, struct async_submit_ctl *submit)
  269. {
  270. struct dma_chan *chan = pq_val_chan(submit, blocks, disks, len);
  271. struct dma_device *device = chan ? chan->device : NULL;
  272. struct dma_async_tx_descriptor *tx;
  273. unsigned char coefs[MAX_DISKS];
  274. enum dma_ctrl_flags dma_flags = submit->cb_fn ? DMA_PREP_INTERRUPT : 0;
  275. struct dmaengine_unmap_data *unmap = NULL;
  276. BUG_ON(disks < 4 || disks > MAX_DISKS);
  277. if (device)
  278. unmap = dmaengine_get_unmap_data(device->dev, disks, GFP_NOWAIT);
  279. if (unmap && disks <= dma_maxpq(device, 0) &&
  280. is_dma_pq_aligned_offs(device, offsets, disks, len)) {
  281. struct device *dev = device->dev;
  282. dma_addr_t pq[2];
  283. int i, j = 0, src_cnt = 0;
  284. pr_debug("%s: (async) disks: %d len: %zu\n",
  285. __func__, disks, len);
  286. unmap->len = len;
  287. for (i = 0; i < disks-2; i++)
  288. if (likely(blocks[i])) {
  289. unmap->addr[j] = dma_map_page(dev, blocks[i],
  290. offsets[i], len,
  291. DMA_TO_DEVICE);
  292. coefs[j] = raid6_gfexp[i];
  293. unmap->to_cnt++;
  294. src_cnt++;
  295. j++;
  296. }
  297. if (!P(blocks, disks)) {
  298. pq[0] = 0;
  299. dma_flags |= DMA_PREP_PQ_DISABLE_P;
  300. } else {
  301. pq[0] = dma_map_page(dev, P(blocks, disks),
  302. P(offsets, disks), len,
  303. DMA_TO_DEVICE);
  304. unmap->addr[j++] = pq[0];
  305. unmap->to_cnt++;
  306. }
  307. if (!Q(blocks, disks)) {
  308. pq[1] = 0;
  309. dma_flags |= DMA_PREP_PQ_DISABLE_Q;
  310. } else {
  311. pq[1] = dma_map_page(dev, Q(blocks, disks),
  312. Q(offsets, disks), len,
  313. DMA_TO_DEVICE);
  314. unmap->addr[j++] = pq[1];
  315. unmap->to_cnt++;
  316. }
  317. if (submit->flags & ASYNC_TX_FENCE)
  318. dma_flags |= DMA_PREP_FENCE;
  319. for (;;) {
  320. tx = device->device_prep_dma_pq_val(chan, pq,
  321. unmap->addr,
  322. src_cnt,
  323. coefs,
  324. len, pqres,
  325. dma_flags);
  326. if (likely(tx))
  327. break;
  328. async_tx_quiesce(&submit->depend_tx);
  329. dma_async_issue_pending(chan);
  330. }
  331. dma_set_unmap(tx, unmap);
  332. async_tx_submit(chan, tx, submit);
  333. } else {
  334. struct page *p_src = P(blocks, disks);
  335. unsigned int p_off = P(offsets, disks);
  336. struct page *q_src = Q(blocks, disks);
  337. unsigned int q_off = Q(offsets, disks);
  338. enum async_tx_flags flags_orig = submit->flags;
  339. dma_async_tx_callback cb_fn_orig = submit->cb_fn;
  340. void *scribble = submit->scribble;
  341. void *cb_param_orig = submit->cb_param;
  342. void *p, *q, *s;
  343. pr_debug("%s: (sync) disks: %d len: %zu\n",
  344. __func__, disks, len);
  345. /* caller must provide a temporary result buffer and
  346. * allow the input parameters to be preserved
  347. */
  348. BUG_ON(!spare || !scribble);
  349. /* wait for any prerequisite operations */
  350. async_tx_quiesce(&submit->depend_tx);
  351. /* recompute p and/or q into the temporary buffer and then
  352. * check to see the result matches the current value
  353. */
  354. tx = NULL;
  355. *pqres = 0;
  356. if (p_src) {
  357. init_async_submit(submit, ASYNC_TX_XOR_ZERO_DST, NULL,
  358. NULL, NULL, scribble);
  359. tx = async_xor_offs(spare, s_off,
  360. blocks, offsets, disks-2, len, submit);
  361. async_tx_quiesce(&tx);
  362. p = page_address(p_src) + p_off;
  363. s = page_address(spare) + s_off;
  364. *pqres |= !!memcmp(p, s, len) << SUM_CHECK_P;
  365. }
  366. if (q_src) {
  367. P(blocks, disks) = NULL;
  368. Q(blocks, disks) = spare;
  369. Q(offsets, disks) = s_off;
  370. init_async_submit(submit, 0, NULL, NULL, NULL, scribble);
  371. tx = async_gen_syndrome(blocks, offsets, disks,
  372. len, submit);
  373. async_tx_quiesce(&tx);
  374. q = page_address(q_src) + q_off;
  375. s = page_address(spare) + s_off;
  376. *pqres |= !!memcmp(q, s, len) << SUM_CHECK_Q;
  377. }
  378. /* restore P, Q and submit */
  379. P(blocks, disks) = p_src;
  380. P(offsets, disks) = p_off;
  381. Q(blocks, disks) = q_src;
  382. Q(offsets, disks) = q_off;
  383. submit->cb_fn = cb_fn_orig;
  384. submit->cb_param = cb_param_orig;
  385. submit->flags = flags_orig;
  386. async_tx_sync_epilog(submit);
  387. tx = NULL;
  388. }
  389. dmaengine_unmap_put(unmap);
  390. return tx;
  391. }
  392. EXPORT_SYMBOL_GPL(async_syndrome_val);
  393. static int __init async_pq_init(void)
  394. {
  395. pq_scribble_page = alloc_page(GFP_KERNEL);
  396. if (pq_scribble_page)
  397. return 0;
  398. pr_err("%s: failed to allocate required spare page\n", __func__);
  399. return -ENOMEM;
  400. }
  401. static void __exit async_pq_exit(void)
  402. {
  403. __free_page(pq_scribble_page);
  404. }
  405. module_init(async_pq_init);
  406. module_exit(async_pq_exit);
  407. MODULE_DESCRIPTION("asynchronous raid6 syndrome generation/validation");
  408. MODULE_LICENSE("GPL");