async_raid6_recov.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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(int disks, size_t bytes, int faila, int failb,
  120. struct page **blocks, 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[disks-2];
  131. q = blocks[disks-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(int disks, size_t bytes, int faila, int failb,
  152. struct page **blocks, 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 good_srcs, good, i;
  163. good_srcs = 0;
  164. good = -1;
  165. for (i = 0; i < disks-2; i++) {
  166. if (blocks[i] == NULL)
  167. continue;
  168. if (i == faila || i == failb)
  169. continue;
  170. good = i;
  171. good_srcs++;
  172. }
  173. BUG_ON(good_srcs > 1);
  174. p = blocks[disks-2];
  175. q = blocks[disks-1];
  176. g = blocks[good];
  177. /* Compute syndrome with zero for the missing data pages
  178. * Use the dead data pages as temporary storage for delta p and
  179. * delta q
  180. */
  181. dp = blocks[faila];
  182. dq = blocks[failb];
  183. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  184. tx = async_memcpy(dp, g, 0, 0, bytes, submit);
  185. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  186. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  187. /* compute P + Pxy */
  188. srcs[0] = dp;
  189. srcs[1] = p;
  190. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  191. NULL, NULL, scribble);
  192. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  193. /* compute Q + Qxy */
  194. srcs[0] = dq;
  195. srcs[1] = q;
  196. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  197. NULL, NULL, scribble);
  198. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  199. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  200. srcs[0] = dp;
  201. srcs[1] = dq;
  202. coef[0] = raid6_gfexi[failb-faila];
  203. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  204. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  205. tx = async_sum_product(dq, srcs, coef, bytes, submit);
  206. /* Dy = P+Pxy+Dx */
  207. srcs[0] = dp;
  208. srcs[1] = dq;
  209. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  210. cb_param, scribble);
  211. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  212. return tx;
  213. }
  214. static struct dma_async_tx_descriptor *
  215. __2data_recov_n(int disks, size_t bytes, int faila, int failb,
  216. struct page **blocks, struct async_submit_ctl *submit)
  217. {
  218. struct dma_async_tx_descriptor *tx = NULL;
  219. struct page *p, *q, *dp, *dq;
  220. struct page *srcs[2];
  221. unsigned char coef[2];
  222. enum async_tx_flags flags = submit->flags;
  223. dma_async_tx_callback cb_fn = submit->cb_fn;
  224. void *cb_param = submit->cb_param;
  225. void *scribble = submit->scribble;
  226. p = blocks[disks-2];
  227. q = blocks[disks-1];
  228. /* Compute syndrome with zero for the missing data pages
  229. * Use the dead data pages as temporary storage for
  230. * delta p and delta q
  231. */
  232. dp = blocks[faila];
  233. blocks[faila] = NULL;
  234. blocks[disks-2] = dp;
  235. dq = blocks[failb];
  236. blocks[failb] = NULL;
  237. blocks[disks-1] = dq;
  238. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  239. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  240. /* Restore pointer table */
  241. blocks[faila] = dp;
  242. blocks[failb] = dq;
  243. blocks[disks-2] = p;
  244. blocks[disks-1] = q;
  245. /* compute P + Pxy */
  246. srcs[0] = dp;
  247. srcs[1] = p;
  248. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  249. NULL, NULL, scribble);
  250. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  251. /* compute Q + Qxy */
  252. srcs[0] = dq;
  253. srcs[1] = q;
  254. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  255. NULL, NULL, scribble);
  256. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  257. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  258. srcs[0] = dp;
  259. srcs[1] = dq;
  260. coef[0] = raid6_gfexi[failb-faila];
  261. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  262. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  263. tx = async_sum_product(dq, srcs, coef, bytes, submit);
  264. /* Dy = P+Pxy+Dx */
  265. srcs[0] = dp;
  266. srcs[1] = dq;
  267. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  268. cb_param, scribble);
  269. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  270. return tx;
  271. }
  272. /**
  273. * async_raid6_2data_recov - asynchronously calculate two missing data blocks
  274. * @disks: number of disks in the RAID-6 array
  275. * @bytes: block size
  276. * @faila: first failed drive index
  277. * @failb: second failed drive index
  278. * @blocks: array of source pointers where the last two entries are p and q
  279. * @submit: submission/completion modifiers
  280. */
  281. struct dma_async_tx_descriptor *
  282. async_raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
  283. struct page **blocks, struct async_submit_ctl *submit)
  284. {
  285. int non_zero_srcs, i;
  286. BUG_ON(faila == failb);
  287. if (failb < faila)
  288. swap(faila, failb);
  289. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  290. /* we need to preserve the contents of 'blocks' for the async
  291. * case, so punt to synchronous if a scribble buffer is not available
  292. */
  293. if (!submit->scribble) {
  294. void **ptrs = (void **) blocks;
  295. async_tx_quiesce(&submit->depend_tx);
  296. for (i = 0; i < disks; i++)
  297. if (blocks[i] == NULL)
  298. ptrs[i] = (void *) raid6_empty_zero_page;
  299. else
  300. ptrs[i] = page_address(blocks[i]);
  301. raid6_2data_recov(disks, bytes, faila, failb, ptrs);
  302. async_tx_sync_epilog(submit);
  303. return NULL;
  304. }
  305. non_zero_srcs = 0;
  306. for (i = 0; i < disks-2 && non_zero_srcs < 4; i++)
  307. if (blocks[i])
  308. non_zero_srcs++;
  309. switch (non_zero_srcs) {
  310. case 0:
  311. case 1:
  312. /* There must be at least 2 sources - the failed devices. */
  313. BUG();
  314. case 2:
  315. /* dma devices do not uniformly understand a zero source pq
  316. * operation (in contrast to the synchronous case), so
  317. * explicitly handle the special case of a 4 disk array with
  318. * both data disks missing.
  319. */
  320. return __2data_recov_4(disks, bytes, faila, failb, blocks, submit);
  321. case 3:
  322. /* dma devices do not uniformly understand a single
  323. * source pq operation (in contrast to the synchronous
  324. * case), so explicitly handle the special case of a 5 disk
  325. * array with 2 of 3 data disks missing.
  326. */
  327. return __2data_recov_5(disks, bytes, faila, failb, blocks, submit);
  328. default:
  329. return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
  330. }
  331. }
  332. EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
  333. /**
  334. * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
  335. * @disks: number of disks in the RAID-6 array
  336. * @bytes: block size
  337. * @faila: failed drive index
  338. * @blocks: array of source pointers where the last two entries are p and q
  339. * @submit: submission/completion modifiers
  340. */
  341. struct dma_async_tx_descriptor *
  342. async_raid6_datap_recov(int disks, size_t bytes, int faila,
  343. struct page **blocks, struct async_submit_ctl *submit)
  344. {
  345. struct dma_async_tx_descriptor *tx = NULL;
  346. struct page *p, *q, *dq;
  347. u8 coef;
  348. enum async_tx_flags flags = submit->flags;
  349. dma_async_tx_callback cb_fn = submit->cb_fn;
  350. void *cb_param = submit->cb_param;
  351. void *scribble = submit->scribble;
  352. int good_srcs, good, i;
  353. struct page *srcs[2];
  354. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  355. /* we need to preserve the contents of 'blocks' for the async
  356. * case, so punt to synchronous if a scribble buffer is not available
  357. */
  358. if (!scribble) {
  359. void **ptrs = (void **) blocks;
  360. async_tx_quiesce(&submit->depend_tx);
  361. for (i = 0; i < disks; i++)
  362. if (blocks[i] == NULL)
  363. ptrs[i] = (void*)raid6_empty_zero_page;
  364. else
  365. ptrs[i] = page_address(blocks[i]);
  366. raid6_datap_recov(disks, bytes, faila, ptrs);
  367. async_tx_sync_epilog(submit);
  368. return NULL;
  369. }
  370. good_srcs = 0;
  371. good = -1;
  372. for (i = 0; i < disks-2; i++) {
  373. if (i == faila)
  374. continue;
  375. if (blocks[i]) {
  376. good = i;
  377. good_srcs++;
  378. if (good_srcs > 1)
  379. break;
  380. }
  381. }
  382. BUG_ON(good_srcs == 0);
  383. p = blocks[disks-2];
  384. q = blocks[disks-1];
  385. /* Compute syndrome with zero for the missing data page
  386. * Use the dead data page as temporary storage for delta q
  387. */
  388. dq = blocks[faila];
  389. blocks[faila] = NULL;
  390. blocks[disks-1] = dq;
  391. /* in the 4-disk case we only need to perform a single source
  392. * multiplication with the one good data block.
  393. */
  394. if (good_srcs == 1) {
  395. struct page *g = blocks[good];
  396. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  397. scribble);
  398. tx = async_memcpy(p, g, 0, 0, bytes, submit);
  399. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  400. scribble);
  401. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  402. } else {
  403. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  404. scribble);
  405. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  406. }
  407. /* Restore pointer table */
  408. blocks[faila] = dq;
  409. blocks[disks-1] = q;
  410. /* calculate g^{-faila} */
  411. coef = raid6_gfinv[raid6_gfexp[faila]];
  412. srcs[0] = dq;
  413. srcs[1] = q;
  414. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  415. NULL, NULL, scribble);
  416. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  417. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  418. tx = async_mult(dq, dq, coef, bytes, submit);
  419. srcs[0] = p;
  420. srcs[1] = dq;
  421. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  422. cb_param, scribble);
  423. tx = async_xor(p, srcs, 0, 2, bytes, submit);
  424. return tx;
  425. }
  426. EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
  427. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  428. MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
  429. MODULE_LICENSE("GPL");