async_raid6_recov.c 13 KB

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