async_raid6_recov.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. void *scribble = submit->scribble;
  286. int non_zero_srcs, i;
  287. BUG_ON(faila == failb);
  288. if (failb < faila)
  289. swap(faila, failb);
  290. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  291. /* if a dma resource is not available or a scribble buffer is not
  292. * available punt to the synchronous path. In the 'dma not
  293. * available' case be sure to use the scribble buffer to
  294. * preserve the content of 'blocks' as the caller intended.
  295. */
  296. if (!async_dma_find_channel(DMA_PQ) || !scribble) {
  297. void **ptrs = scribble ? scribble : (void **) blocks;
  298. async_tx_quiesce(&submit->depend_tx);
  299. for (i = 0; i < disks; i++)
  300. if (blocks[i] == NULL)
  301. ptrs[i] = (void *) raid6_empty_zero_page;
  302. else
  303. ptrs[i] = page_address(blocks[i]);
  304. raid6_2data_recov(disks, bytes, faila, failb, ptrs);
  305. async_tx_sync_epilog(submit);
  306. return NULL;
  307. }
  308. non_zero_srcs = 0;
  309. for (i = 0; i < disks-2 && non_zero_srcs < 4; i++)
  310. if (blocks[i])
  311. non_zero_srcs++;
  312. switch (non_zero_srcs) {
  313. case 0:
  314. case 1:
  315. /* There must be at least 2 sources - the failed devices. */
  316. BUG();
  317. case 2:
  318. /* dma devices do not uniformly understand a zero source pq
  319. * operation (in contrast to the synchronous case), so
  320. * explicitly handle the special case of a 4 disk array with
  321. * both data disks missing.
  322. */
  323. return __2data_recov_4(disks, bytes, faila, failb, blocks, submit);
  324. case 3:
  325. /* dma devices do not uniformly understand a single
  326. * source pq operation (in contrast to the synchronous
  327. * case), so explicitly handle the special case of a 5 disk
  328. * array with 2 of 3 data disks missing.
  329. */
  330. return __2data_recov_5(disks, bytes, faila, failb, blocks, submit);
  331. default:
  332. return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
  333. }
  334. }
  335. EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
  336. /**
  337. * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
  338. * @disks: number of disks in the RAID-6 array
  339. * @bytes: block size
  340. * @faila: failed drive index
  341. * @blocks: array of source pointers where the last two entries are p and q
  342. * @submit: submission/completion modifiers
  343. */
  344. struct dma_async_tx_descriptor *
  345. async_raid6_datap_recov(int disks, size_t bytes, int faila,
  346. struct page **blocks, struct async_submit_ctl *submit)
  347. {
  348. struct dma_async_tx_descriptor *tx = NULL;
  349. struct page *p, *q, *dq;
  350. u8 coef;
  351. enum async_tx_flags flags = submit->flags;
  352. dma_async_tx_callback cb_fn = submit->cb_fn;
  353. void *cb_param = submit->cb_param;
  354. void *scribble = submit->scribble;
  355. int good_srcs, good, i;
  356. struct page *srcs[2];
  357. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  358. /* if a dma resource is not available or a scribble buffer is not
  359. * available punt to the synchronous path. In the 'dma not
  360. * available' case be sure to use the scribble buffer to
  361. * preserve the content of 'blocks' as the caller intended.
  362. */
  363. if (!async_dma_find_channel(DMA_PQ) || !scribble) {
  364. void **ptrs = scribble ? scribble : (void **) blocks;
  365. async_tx_quiesce(&submit->depend_tx);
  366. for (i = 0; i < disks; i++)
  367. if (blocks[i] == NULL)
  368. ptrs[i] = (void*)raid6_empty_zero_page;
  369. else
  370. ptrs[i] = page_address(blocks[i]);
  371. raid6_datap_recov(disks, bytes, faila, ptrs);
  372. async_tx_sync_epilog(submit);
  373. return NULL;
  374. }
  375. good_srcs = 0;
  376. good = -1;
  377. for (i = 0; i < disks-2; i++) {
  378. if (i == faila)
  379. continue;
  380. if (blocks[i]) {
  381. good = i;
  382. good_srcs++;
  383. if (good_srcs > 1)
  384. break;
  385. }
  386. }
  387. BUG_ON(good_srcs == 0);
  388. p = blocks[disks-2];
  389. q = blocks[disks-1];
  390. /* Compute syndrome with zero for the missing data page
  391. * Use the dead data page as temporary storage for delta q
  392. */
  393. dq = blocks[faila];
  394. blocks[faila] = NULL;
  395. blocks[disks-1] = dq;
  396. /* in the 4-disk case we only need to perform a single source
  397. * multiplication with the one good data block.
  398. */
  399. if (good_srcs == 1) {
  400. struct page *g = blocks[good];
  401. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  402. scribble);
  403. tx = async_memcpy(p, g, 0, 0, bytes, submit);
  404. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  405. scribble);
  406. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  407. } else {
  408. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  409. scribble);
  410. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  411. }
  412. /* Restore pointer table */
  413. blocks[faila] = dq;
  414. blocks[disks-1] = q;
  415. /* calculate g^{-faila} */
  416. coef = raid6_gfinv[raid6_gfexp[faila]];
  417. srcs[0] = dq;
  418. srcs[1] = q;
  419. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  420. NULL, NULL, scribble);
  421. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  422. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  423. tx = async_mult(dq, dq, coef, bytes, submit);
  424. srcs[0] = p;
  425. srcs[1] = dq;
  426. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  427. cb_param, scribble);
  428. tx = async_xor(p, srcs, 0, 2, bytes, submit);
  429. return tx;
  430. }
  431. EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
  432. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  433. MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
  434. MODULE_LICENSE("GPL");