async_raid6_recov.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Asynchronous RAID-6 recovery calculations ASYNC_TX API.
  3. * Copyright(c) 2009 Intel Corporation
  4. *
  5. * based on raid6recov.c:
  6. * Copyright 2002 H. Peter Anvin
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 51
  20. * Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/raid/pq.h>
  27. #include <linux/async_tx.h>
  28. static struct dma_async_tx_descriptor *
  29. async_sum_product(struct page *dest, struct page **srcs, unsigned char *coef,
  30. size_t len, struct async_submit_ctl *submit)
  31. {
  32. struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
  33. &dest, 1, srcs, 2, len);
  34. struct dma_device *dma = chan ? chan->device : NULL;
  35. const u8 *amul, *bmul;
  36. u8 ax, bx;
  37. u8 *a, *b, *c;
  38. if (dma) {
  39. dma_addr_t dma_dest[2];
  40. dma_addr_t dma_src[2];
  41. struct device *dev = dma->dev;
  42. struct dma_async_tx_descriptor *tx;
  43. enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;
  44. if (submit->flags & ASYNC_TX_FENCE)
  45. dma_flags |= DMA_PREP_FENCE;
  46. dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
  47. dma_src[0] = dma_map_page(dev, srcs[0], 0, len, DMA_TO_DEVICE);
  48. dma_src[1] = dma_map_page(dev, srcs[1], 0, len, DMA_TO_DEVICE);
  49. tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 2, coef,
  50. len, dma_flags);
  51. if (tx) {
  52. async_tx_submit(chan, tx, submit);
  53. return tx;
  54. }
  55. /* could not get a descriptor, unmap and fall through to
  56. * the synchronous path
  57. */
  58. dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);
  59. dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);
  60. dma_unmap_page(dev, dma_src[1], len, DMA_TO_DEVICE);
  61. }
  62. /* run the operation synchronously */
  63. async_tx_quiesce(&submit->depend_tx);
  64. amul = raid6_gfmul[coef[0]];
  65. bmul = raid6_gfmul[coef[1]];
  66. a = page_address(srcs[0]);
  67. b = page_address(srcs[1]);
  68. c = page_address(dest);
  69. while (len--) {
  70. ax = amul[*a++];
  71. bx = bmul[*b++];
  72. *c++ = ax ^ bx;
  73. }
  74. return NULL;
  75. }
  76. static struct dma_async_tx_descriptor *
  77. async_mult(struct page *dest, struct page *src, u8 coef, size_t len,
  78. struct async_submit_ctl *submit)
  79. {
  80. struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
  81. &dest, 1, &src, 1, len);
  82. struct dma_device *dma = chan ? chan->device : NULL;
  83. const u8 *qmul; /* Q multiplier table */
  84. u8 *d, *s;
  85. if (dma) {
  86. dma_addr_t dma_dest[2];
  87. dma_addr_t dma_src[1];
  88. struct device *dev = dma->dev;
  89. struct dma_async_tx_descriptor *tx;
  90. enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;
  91. if (submit->flags & ASYNC_TX_FENCE)
  92. dma_flags |= DMA_PREP_FENCE;
  93. dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
  94. dma_src[0] = dma_map_page(dev, src, 0, len, DMA_TO_DEVICE);
  95. tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 1, &coef,
  96. len, dma_flags);
  97. if (tx) {
  98. async_tx_submit(chan, tx, submit);
  99. return tx;
  100. }
  101. /* could not get a descriptor, unmap and fall through to
  102. * the synchronous path
  103. */
  104. dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);
  105. dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);
  106. }
  107. /* no channel available, or failed to allocate a descriptor, so
  108. * perform the operation synchronously
  109. */
  110. async_tx_quiesce(&submit->depend_tx);
  111. qmul = raid6_gfmul[coef];
  112. d = page_address(dest);
  113. s = page_address(src);
  114. while (len--)
  115. *d++ = qmul[*s++];
  116. return NULL;
  117. }
  118. static struct dma_async_tx_descriptor *
  119. __2data_recov_4(size_t bytes, int faila, int failb, struct page **blocks,
  120. struct async_submit_ctl *submit)
  121. {
  122. struct dma_async_tx_descriptor *tx = NULL;
  123. struct page *p, *q, *a, *b;
  124. struct page *srcs[2];
  125. unsigned char coef[2];
  126. enum async_tx_flags flags = submit->flags;
  127. dma_async_tx_callback cb_fn = submit->cb_fn;
  128. void *cb_param = submit->cb_param;
  129. void *scribble = submit->scribble;
  130. p = blocks[4-2];
  131. q = blocks[4-1];
  132. a = blocks[faila];
  133. b = blocks[failb];
  134. /* in the 4 disk case P + Pxy == P and Q + Qxy == Q */
  135. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  136. srcs[0] = p;
  137. srcs[1] = q;
  138. coef[0] = raid6_gfexi[failb-faila];
  139. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  140. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  141. tx = async_sum_product(b, srcs, coef, bytes, submit);
  142. /* Dy = P+Pxy+Dx */
  143. srcs[0] = p;
  144. srcs[1] = b;
  145. init_async_submit(submit, flags | ASYNC_TX_XOR_ZERO_DST, tx, cb_fn,
  146. cb_param, scribble);
  147. tx = async_xor(a, srcs, 0, 2, bytes, submit);
  148. return tx;
  149. }
  150. static struct dma_async_tx_descriptor *
  151. __2data_recov_5(size_t bytes, int faila, int failb, struct page **blocks,
  152. struct async_submit_ctl *submit)
  153. {
  154. struct dma_async_tx_descriptor *tx = NULL;
  155. struct page *p, *q, *g, *dp, *dq;
  156. struct page *srcs[2];
  157. unsigned char coef[2];
  158. enum async_tx_flags flags = submit->flags;
  159. dma_async_tx_callback cb_fn = submit->cb_fn;
  160. void *cb_param = submit->cb_param;
  161. void *scribble = submit->scribble;
  162. int uninitialized_var(good);
  163. int i;
  164. for (i = 0; i < 3; i++) {
  165. if (i == faila || i == failb)
  166. continue;
  167. else {
  168. good = i;
  169. break;
  170. }
  171. }
  172. BUG_ON(i >= 3);
  173. p = blocks[5-2];
  174. q = blocks[5-1];
  175. g = blocks[good];
  176. /* Compute syndrome with zero for the missing data pages
  177. * Use the dead data pages as temporary storage for delta p and
  178. * delta q
  179. */
  180. dp = blocks[faila];
  181. dq = blocks[failb];
  182. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  183. tx = async_memcpy(dp, g, 0, 0, bytes, submit);
  184. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  185. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  186. /* compute P + Pxy */
  187. srcs[0] = dp;
  188. srcs[1] = p;
  189. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  190. NULL, NULL, scribble);
  191. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  192. /* compute Q + Qxy */
  193. srcs[0] = dq;
  194. srcs[1] = q;
  195. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  196. NULL, NULL, scribble);
  197. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  198. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  199. srcs[0] = dp;
  200. srcs[1] = dq;
  201. coef[0] = raid6_gfexi[failb-faila];
  202. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  203. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  204. tx = async_sum_product(dq, srcs, coef, bytes, submit);
  205. /* Dy = P+Pxy+Dx */
  206. srcs[0] = dp;
  207. srcs[1] = dq;
  208. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  209. cb_param, scribble);
  210. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  211. return tx;
  212. }
  213. static struct dma_async_tx_descriptor *
  214. __2data_recov_n(int disks, size_t bytes, int faila, int failb,
  215. struct page **blocks, struct async_submit_ctl *submit)
  216. {
  217. struct dma_async_tx_descriptor *tx = NULL;
  218. struct page *p, *q, *dp, *dq;
  219. struct page *srcs[2];
  220. unsigned char coef[2];
  221. enum async_tx_flags flags = submit->flags;
  222. dma_async_tx_callback cb_fn = submit->cb_fn;
  223. void *cb_param = submit->cb_param;
  224. void *scribble = submit->scribble;
  225. p = blocks[disks-2];
  226. q = blocks[disks-1];
  227. /* Compute syndrome with zero for the missing data pages
  228. * Use the dead data pages as temporary storage for
  229. * delta p and delta q
  230. */
  231. dp = blocks[faila];
  232. blocks[faila] = (void *)raid6_empty_zero_page;
  233. blocks[disks-2] = dp;
  234. dq = blocks[failb];
  235. blocks[failb] = (void *)raid6_empty_zero_page;
  236. blocks[disks-1] = dq;
  237. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  238. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  239. /* Restore pointer table */
  240. blocks[faila] = dp;
  241. blocks[failb] = dq;
  242. blocks[disks-2] = p;
  243. blocks[disks-1] = q;
  244. /* compute P + Pxy */
  245. srcs[0] = dp;
  246. srcs[1] = p;
  247. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  248. NULL, NULL, scribble);
  249. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  250. /* compute Q + Qxy */
  251. srcs[0] = dq;
  252. srcs[1] = q;
  253. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  254. NULL, NULL, scribble);
  255. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  256. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  257. srcs[0] = dp;
  258. srcs[1] = dq;
  259. coef[0] = raid6_gfexi[failb-faila];
  260. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  261. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  262. tx = async_sum_product(dq, srcs, coef, bytes, submit);
  263. /* Dy = P+Pxy+Dx */
  264. srcs[0] = dp;
  265. srcs[1] = dq;
  266. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  267. cb_param, scribble);
  268. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  269. return tx;
  270. }
  271. /**
  272. * async_raid6_2data_recov - asynchronously calculate two missing data blocks
  273. * @disks: number of disks in the RAID-6 array
  274. * @bytes: block size
  275. * @faila: first failed drive index
  276. * @failb: second failed drive index
  277. * @blocks: array of source pointers where the last two entries are p and q
  278. * @submit: submission/completion modifiers
  279. */
  280. struct dma_async_tx_descriptor *
  281. async_raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
  282. struct page **blocks, struct async_submit_ctl *submit)
  283. {
  284. BUG_ON(faila == failb);
  285. if (failb < faila)
  286. swap(faila, failb);
  287. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  288. /* we need to preserve the contents of 'blocks' for the async
  289. * case, so punt to synchronous if a scribble buffer is not available
  290. */
  291. if (!submit->scribble) {
  292. void **ptrs = (void **) blocks;
  293. int i;
  294. async_tx_quiesce(&submit->depend_tx);
  295. for (i = 0; i < disks; i++)
  296. ptrs[i] = page_address(blocks[i]);
  297. raid6_2data_recov(disks, bytes, faila, failb, ptrs);
  298. async_tx_sync_epilog(submit);
  299. return NULL;
  300. }
  301. switch (disks) {
  302. case 4:
  303. /* dma devices do not uniformly understand a zero source pq
  304. * operation (in contrast to the synchronous case), so
  305. * explicitly handle the 4 disk special case
  306. */
  307. return __2data_recov_4(bytes, faila, failb, blocks, submit);
  308. case 5:
  309. /* dma devices do not uniformly understand a single
  310. * source pq operation (in contrast to the synchronous
  311. * case), so explicitly handle the 5 disk special case
  312. */
  313. return __2data_recov_5(bytes, faila, failb, blocks, submit);
  314. default:
  315. return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
  316. }
  317. }
  318. EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
  319. /**
  320. * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
  321. * @disks: number of disks in the RAID-6 array
  322. * @bytes: block size
  323. * @faila: failed drive index
  324. * @blocks: array of source pointers where the last two entries are p and q
  325. * @submit: submission/completion modifiers
  326. */
  327. struct dma_async_tx_descriptor *
  328. async_raid6_datap_recov(int disks, size_t bytes, int faila,
  329. struct page **blocks, struct async_submit_ctl *submit)
  330. {
  331. struct dma_async_tx_descriptor *tx = NULL;
  332. struct page *p, *q, *dq;
  333. u8 coef;
  334. enum async_tx_flags flags = submit->flags;
  335. dma_async_tx_callback cb_fn = submit->cb_fn;
  336. void *cb_param = submit->cb_param;
  337. void *scribble = submit->scribble;
  338. struct page *srcs[2];
  339. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  340. /* we need to preserve the contents of 'blocks' for the async
  341. * case, so punt to synchronous if a scribble buffer is not available
  342. */
  343. if (!scribble) {
  344. void **ptrs = (void **) blocks;
  345. int i;
  346. async_tx_quiesce(&submit->depend_tx);
  347. for (i = 0; i < disks; i++)
  348. ptrs[i] = page_address(blocks[i]);
  349. raid6_datap_recov(disks, bytes, faila, ptrs);
  350. async_tx_sync_epilog(submit);
  351. return NULL;
  352. }
  353. p = blocks[disks-2];
  354. q = blocks[disks-1];
  355. /* Compute syndrome with zero for the missing data page
  356. * Use the dead data page as temporary storage for delta q
  357. */
  358. dq = blocks[faila];
  359. blocks[faila] = (void *)raid6_empty_zero_page;
  360. blocks[disks-1] = dq;
  361. /* in the 4 disk case we only need to perform a single source
  362. * multiplication
  363. */
  364. if (disks == 4) {
  365. int good = faila == 0 ? 1 : 0;
  366. struct page *g = blocks[good];
  367. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  368. scribble);
  369. tx = async_memcpy(p, g, 0, 0, bytes, submit);
  370. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  371. scribble);
  372. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  373. } else {
  374. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  375. scribble);
  376. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  377. }
  378. /* Restore pointer table */
  379. blocks[faila] = dq;
  380. blocks[disks-1] = q;
  381. /* calculate g^{-faila} */
  382. coef = raid6_gfinv[raid6_gfexp[faila]];
  383. srcs[0] = dq;
  384. srcs[1] = q;
  385. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  386. NULL, NULL, scribble);
  387. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  388. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  389. tx = async_mult(dq, dq, coef, bytes, submit);
  390. srcs[0] = p;
  391. srcs[1] = dq;
  392. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  393. cb_param, scribble);
  394. tx = async_xor(p, srcs, 0, 2, bytes, submit);
  395. return tx;
  396. }
  397. EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
  398. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  399. MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
  400. MODULE_LICENSE("GPL");