async_pq.c 13 KB

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