async_pq.c 12 KB

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