async_raid6_recov.c 15 KB

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