async_raid6_recov.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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/module.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/raid/pq.h>
  28. #include <linux/async_tx.h>
  29. static struct dma_async_tx_descriptor *
  30. async_sum_product(struct page *dest, struct page **srcs, unsigned char *coef,
  31. size_t len, struct async_submit_ctl *submit)
  32. {
  33. struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
  34. &dest, 1, srcs, 2, len);
  35. struct dma_device *dma = chan ? chan->device : NULL;
  36. const u8 *amul, *bmul;
  37. u8 ax, bx;
  38. u8 *a, *b, *c;
  39. if (dma) {
  40. dma_addr_t dma_dest[2];
  41. dma_addr_t dma_src[2];
  42. struct device *dev = dma->dev;
  43. struct dma_async_tx_descriptor *tx;
  44. enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;
  45. if (submit->flags & ASYNC_TX_FENCE)
  46. dma_flags |= DMA_PREP_FENCE;
  47. dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
  48. dma_src[0] = dma_map_page(dev, srcs[0], 0, len, DMA_TO_DEVICE);
  49. dma_src[1] = dma_map_page(dev, srcs[1], 0, len, DMA_TO_DEVICE);
  50. tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 2, coef,
  51. len, dma_flags);
  52. if (tx) {
  53. async_tx_submit(chan, tx, submit);
  54. return tx;
  55. }
  56. /* could not get a descriptor, unmap and fall through to
  57. * the synchronous path
  58. */
  59. dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);
  60. dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);
  61. dma_unmap_page(dev, dma_src[1], len, DMA_TO_DEVICE);
  62. }
  63. /* run the operation synchronously */
  64. async_tx_quiesce(&submit->depend_tx);
  65. amul = raid6_gfmul[coef[0]];
  66. bmul = raid6_gfmul[coef[1]];
  67. a = page_address(srcs[0]);
  68. b = page_address(srcs[1]);
  69. c = page_address(dest);
  70. while (len--) {
  71. ax = amul[*a++];
  72. bx = bmul[*b++];
  73. *c++ = ax ^ bx;
  74. }
  75. return NULL;
  76. }
  77. static struct dma_async_tx_descriptor *
  78. async_mult(struct page *dest, struct page *src, u8 coef, size_t len,
  79. struct async_submit_ctl *submit)
  80. {
  81. struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
  82. &dest, 1, &src, 1, len);
  83. struct dma_device *dma = chan ? chan->device : NULL;
  84. const u8 *qmul; /* Q multiplier table */
  85. u8 *d, *s;
  86. if (dma) {
  87. dma_addr_t dma_dest[2];
  88. dma_addr_t dma_src[1];
  89. struct device *dev = dma->dev;
  90. struct dma_async_tx_descriptor *tx;
  91. enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;
  92. if (submit->flags & ASYNC_TX_FENCE)
  93. dma_flags |= DMA_PREP_FENCE;
  94. dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
  95. dma_src[0] = dma_map_page(dev, src, 0, len, DMA_TO_DEVICE);
  96. tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 1, &coef,
  97. len, dma_flags);
  98. if (tx) {
  99. async_tx_submit(chan, tx, submit);
  100. return tx;
  101. }
  102. /* could not get a descriptor, unmap and fall through to
  103. * the synchronous path
  104. */
  105. dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);
  106. dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);
  107. }
  108. /* no channel available, or failed to allocate a descriptor, so
  109. * perform the operation synchronously
  110. */
  111. async_tx_quiesce(&submit->depend_tx);
  112. qmul = raid6_gfmul[coef];
  113. d = page_address(dest);
  114. s = page_address(src);
  115. while (len--)
  116. *d++ = qmul[*s++];
  117. return NULL;
  118. }
  119. static struct dma_async_tx_descriptor *
  120. __2data_recov_4(int disks, size_t bytes, int faila, int failb,
  121. struct page **blocks, struct async_submit_ctl *submit)
  122. {
  123. struct dma_async_tx_descriptor *tx = NULL;
  124. struct page *p, *q, *a, *b;
  125. struct page *srcs[2];
  126. unsigned char coef[2];
  127. enum async_tx_flags flags = submit->flags;
  128. dma_async_tx_callback cb_fn = submit->cb_fn;
  129. void *cb_param = submit->cb_param;
  130. void *scribble = submit->scribble;
  131. p = blocks[disks-2];
  132. q = blocks[disks-1];
  133. a = blocks[faila];
  134. b = blocks[failb];
  135. /* in the 4 disk case P + Pxy == P and Q + Qxy == Q */
  136. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  137. srcs[0] = p;
  138. srcs[1] = q;
  139. coef[0] = raid6_gfexi[failb-faila];
  140. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  141. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  142. tx = async_sum_product(b, srcs, coef, bytes, submit);
  143. /* Dy = P+Pxy+Dx */
  144. srcs[0] = p;
  145. srcs[1] = b;
  146. init_async_submit(submit, flags | ASYNC_TX_XOR_ZERO_DST, tx, cb_fn,
  147. cb_param, scribble);
  148. tx = async_xor(a, srcs, 0, 2, bytes, submit);
  149. return tx;
  150. }
  151. static struct dma_async_tx_descriptor *
  152. __2data_recov_5(int disks, size_t bytes, int faila, int failb,
  153. struct page **blocks, struct async_submit_ctl *submit)
  154. {
  155. struct dma_async_tx_descriptor *tx = NULL;
  156. struct page *p, *q, *g, *dp, *dq;
  157. struct page *srcs[2];
  158. unsigned char coef[2];
  159. enum async_tx_flags flags = submit->flags;
  160. dma_async_tx_callback cb_fn = submit->cb_fn;
  161. void *cb_param = submit->cb_param;
  162. void *scribble = submit->scribble;
  163. int good_srcs, good, i;
  164. good_srcs = 0;
  165. good = -1;
  166. for (i = 0; i < disks-2; i++) {
  167. if (blocks[i] == NULL)
  168. continue;
  169. if (i == faila || i == failb)
  170. continue;
  171. good = i;
  172. good_srcs++;
  173. }
  174. BUG_ON(good_srcs > 1);
  175. p = blocks[disks-2];
  176. q = blocks[disks-1];
  177. g = blocks[good];
  178. /* Compute syndrome with zero for the missing data pages
  179. * Use the dead data pages as temporary storage for delta p and
  180. * delta q
  181. */
  182. dp = blocks[faila];
  183. dq = blocks[failb];
  184. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  185. tx = async_memcpy(dp, g, 0, 0, bytes, submit);
  186. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  187. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  188. /* compute P + Pxy */
  189. srcs[0] = dp;
  190. srcs[1] = p;
  191. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  192. NULL, NULL, scribble);
  193. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  194. /* compute Q + Qxy */
  195. srcs[0] = dq;
  196. srcs[1] = q;
  197. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  198. NULL, NULL, scribble);
  199. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  200. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  201. srcs[0] = dp;
  202. srcs[1] = dq;
  203. coef[0] = raid6_gfexi[failb-faila];
  204. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  205. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  206. tx = async_sum_product(dq, srcs, coef, bytes, submit);
  207. /* Dy = P+Pxy+Dx */
  208. srcs[0] = dp;
  209. srcs[1] = dq;
  210. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  211. cb_param, scribble);
  212. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  213. return tx;
  214. }
  215. static struct dma_async_tx_descriptor *
  216. __2data_recov_n(int disks, size_t bytes, int faila, int failb,
  217. struct page **blocks, struct async_submit_ctl *submit)
  218. {
  219. struct dma_async_tx_descriptor *tx = NULL;
  220. struct page *p, *q, *dp, *dq;
  221. struct page *srcs[2];
  222. unsigned char coef[2];
  223. enum async_tx_flags flags = submit->flags;
  224. dma_async_tx_callback cb_fn = submit->cb_fn;
  225. void *cb_param = submit->cb_param;
  226. void *scribble = submit->scribble;
  227. p = blocks[disks-2];
  228. q = blocks[disks-1];
  229. /* Compute syndrome with zero for the missing data pages
  230. * Use the dead data pages as temporary storage for
  231. * delta p and delta q
  232. */
  233. dp = blocks[faila];
  234. blocks[faila] = NULL;
  235. blocks[disks-2] = dp;
  236. dq = blocks[failb];
  237. blocks[failb] = NULL;
  238. blocks[disks-1] = dq;
  239. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  240. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  241. /* Restore pointer table */
  242. blocks[faila] = dp;
  243. blocks[failb] = dq;
  244. blocks[disks-2] = p;
  245. blocks[disks-1] = q;
  246. /* compute P + Pxy */
  247. srcs[0] = dp;
  248. srcs[1] = p;
  249. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  250. NULL, NULL, scribble);
  251. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  252. /* compute Q + Qxy */
  253. srcs[0] = dq;
  254. srcs[1] = q;
  255. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  256. NULL, NULL, scribble);
  257. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  258. /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
  259. srcs[0] = dp;
  260. srcs[1] = dq;
  261. coef[0] = raid6_gfexi[failb-faila];
  262. coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
  263. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  264. tx = async_sum_product(dq, srcs, coef, bytes, submit);
  265. /* Dy = P+Pxy+Dx */
  266. srcs[0] = dp;
  267. srcs[1] = dq;
  268. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  269. cb_param, scribble);
  270. tx = async_xor(dp, srcs, 0, 2, bytes, submit);
  271. return tx;
  272. }
  273. /**
  274. * async_raid6_2data_recov - asynchronously calculate two missing data blocks
  275. * @disks: number of disks in the RAID-6 array
  276. * @bytes: block size
  277. * @faila: first failed drive index
  278. * @failb: second failed drive index
  279. * @blocks: array of source pointers where the last two entries are p and q
  280. * @submit: submission/completion modifiers
  281. */
  282. struct dma_async_tx_descriptor *
  283. async_raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
  284. struct page **blocks, struct async_submit_ctl *submit)
  285. {
  286. void *scribble = submit->scribble;
  287. int non_zero_srcs, i;
  288. BUG_ON(faila == failb);
  289. if (failb < faila)
  290. swap(faila, failb);
  291. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  292. /* if a dma resource is not available or a scribble buffer is not
  293. * available punt to the synchronous path. In the 'dma not
  294. * available' case be sure to use the scribble buffer to
  295. * preserve the content of 'blocks' as the caller intended.
  296. */
  297. if (!async_dma_find_channel(DMA_PQ) || !scribble) {
  298. void **ptrs = scribble ? scribble : (void **) blocks;
  299. async_tx_quiesce(&submit->depend_tx);
  300. for (i = 0; i < disks; i++)
  301. if (blocks[i] == NULL)
  302. ptrs[i] = (void *) raid6_empty_zero_page;
  303. else
  304. ptrs[i] = page_address(blocks[i]);
  305. raid6_2data_recov(disks, bytes, faila, failb, ptrs);
  306. async_tx_sync_epilog(submit);
  307. return NULL;
  308. }
  309. non_zero_srcs = 0;
  310. for (i = 0; i < disks-2 && non_zero_srcs < 4; i++)
  311. if (blocks[i])
  312. non_zero_srcs++;
  313. switch (non_zero_srcs) {
  314. case 0:
  315. case 1:
  316. /* There must be at least 2 sources - the failed devices. */
  317. BUG();
  318. case 2:
  319. /* dma devices do not uniformly understand a zero source pq
  320. * operation (in contrast to the synchronous case), so
  321. * explicitly handle the special case of a 4 disk array with
  322. * both data disks missing.
  323. */
  324. return __2data_recov_4(disks, bytes, faila, failb, blocks, submit);
  325. case 3:
  326. /* dma devices do not uniformly understand a single
  327. * source pq operation (in contrast to the synchronous
  328. * case), so explicitly handle the special case of a 5 disk
  329. * array with 2 of 3 data disks missing.
  330. */
  331. return __2data_recov_5(disks, bytes, faila, failb, blocks, submit);
  332. default:
  333. return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
  334. }
  335. }
  336. EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
  337. /**
  338. * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
  339. * @disks: number of disks in the RAID-6 array
  340. * @bytes: block size
  341. * @faila: failed drive index
  342. * @blocks: array of source pointers where the last two entries are p and q
  343. * @submit: submission/completion modifiers
  344. */
  345. struct dma_async_tx_descriptor *
  346. async_raid6_datap_recov(int disks, size_t bytes, int faila,
  347. struct page **blocks, struct async_submit_ctl *submit)
  348. {
  349. struct dma_async_tx_descriptor *tx = NULL;
  350. struct page *p, *q, *dq;
  351. u8 coef;
  352. enum async_tx_flags flags = submit->flags;
  353. dma_async_tx_callback cb_fn = submit->cb_fn;
  354. void *cb_param = submit->cb_param;
  355. void *scribble = submit->scribble;
  356. int good_srcs, good, i;
  357. struct page *srcs[2];
  358. pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
  359. /* if a dma resource is not available or a scribble buffer is not
  360. * available punt to the synchronous path. In the 'dma not
  361. * available' case be sure to use the scribble buffer to
  362. * preserve the content of 'blocks' as the caller intended.
  363. */
  364. if (!async_dma_find_channel(DMA_PQ) || !scribble) {
  365. void **ptrs = scribble ? scribble : (void **) blocks;
  366. async_tx_quiesce(&submit->depend_tx);
  367. for (i = 0; i < disks; i++)
  368. if (blocks[i] == NULL)
  369. ptrs[i] = (void*)raid6_empty_zero_page;
  370. else
  371. ptrs[i] = page_address(blocks[i]);
  372. raid6_datap_recov(disks, bytes, faila, ptrs);
  373. async_tx_sync_epilog(submit);
  374. return NULL;
  375. }
  376. good_srcs = 0;
  377. good = -1;
  378. for (i = 0; i < disks-2; i++) {
  379. if (i == faila)
  380. continue;
  381. if (blocks[i]) {
  382. good = i;
  383. good_srcs++;
  384. if (good_srcs > 1)
  385. break;
  386. }
  387. }
  388. BUG_ON(good_srcs == 0);
  389. p = blocks[disks-2];
  390. q = blocks[disks-1];
  391. /* Compute syndrome with zero for the missing data page
  392. * Use the dead data page as temporary storage for delta q
  393. */
  394. dq = blocks[faila];
  395. blocks[faila] = NULL;
  396. blocks[disks-1] = dq;
  397. /* in the 4-disk case we only need to perform a single source
  398. * multiplication with the one good data block.
  399. */
  400. if (good_srcs == 1) {
  401. struct page *g = blocks[good];
  402. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  403. scribble);
  404. tx = async_memcpy(p, g, 0, 0, bytes, submit);
  405. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  406. scribble);
  407. tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
  408. } else {
  409. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
  410. scribble);
  411. tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
  412. }
  413. /* Restore pointer table */
  414. blocks[faila] = dq;
  415. blocks[disks-1] = q;
  416. /* calculate g^{-faila} */
  417. coef = raid6_gfinv[raid6_gfexp[faila]];
  418. srcs[0] = dq;
  419. srcs[1] = q;
  420. init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
  421. NULL, NULL, scribble);
  422. tx = async_xor(dq, srcs, 0, 2, bytes, submit);
  423. init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
  424. tx = async_mult(dq, dq, coef, bytes, submit);
  425. srcs[0] = p;
  426. srcs[1] = dq;
  427. init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
  428. cb_param, scribble);
  429. tx = async_xor(p, srcs, 0, 2, bytes, submit);
  430. return tx;
  431. }
  432. EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
  433. MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
  434. MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
  435. MODULE_LICENSE("GPL");