async_pq.c 12 KB

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