async_pq.c 13 KB

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