2 * Asynchronous RAID-6 recovery calculations ASYNC_TX API.
3 * Copyright(c) 2009 Intel Corporation
5 * based on raid6recov.c:
6 * Copyright 2002 H. Peter Anvin
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)
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
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.
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>
31 static struct dma_async_tx_descriptor *
32 async_sum_product(struct page *dest, struct page **srcs, unsigned char *coef,
33 size_t len, struct async_submit_ctl *submit)
35 struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
36 &dest, 1, srcs, 2, len);
37 struct dma_device *dma = chan ? chan->device : NULL;
38 struct dmaengine_unmap_data *unmap = NULL;
39 const u8 *amul, *bmul;
44 unmap = dmaengine_get_unmap_data(dma->dev, 3, GFP_NOIO);
47 struct device *dev = dma->dev;
49 struct dma_async_tx_descriptor *tx;
50 enum dma_ctrl_flags dma_flags = DMA_COMPL_SKIP_SRC_UNMAP |
51 DMA_COMPL_SKIP_DEST_UNMAP |
52 DMA_PREP_PQ_DISABLE_P;
54 if (submit->flags & ASYNC_TX_FENCE)
55 dma_flags |= DMA_PREP_FENCE;
56 unmap->addr[0] = dma_map_page(dev, srcs[0], 0, len, DMA_TO_DEVICE);
57 unmap->addr[1] = dma_map_page(dev, srcs[1], 0, len, DMA_TO_DEVICE);
60 unmap->addr[2] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
62 /* engine only looks at Q, but expects it to follow P */
63 pq[1] = unmap->addr[2];
66 tx = dma->device_prep_dma_pq(chan, pq, unmap->addr, 2, coef,
69 dma_set_unmap(tx, unmap);
70 async_tx_submit(chan, tx, submit);
71 dmaengine_unmap_put(unmap);
75 /* could not get a descriptor, unmap and fall through to
76 * the synchronous path
78 dmaengine_unmap_put(unmap);
81 /* run the operation synchronously */
82 async_tx_quiesce(&submit->depend_tx);
83 amul = raid6_gfmul[coef[0]];
84 bmul = raid6_gfmul[coef[1]];
85 a = page_address(srcs[0]);
86 b = page_address(srcs[1]);
87 c = page_address(dest);
98 static struct dma_async_tx_descriptor *
99 async_mult(struct page *dest, struct page *src, u8 coef, size_t len,
100 struct async_submit_ctl *submit)
102 struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
103 &dest, 1, &src, 1, len);
104 struct dma_device *dma = chan ? chan->device : NULL;
105 struct dmaengine_unmap_data *unmap = NULL;
106 const u8 *qmul; /* Q multiplier table */
110 unmap = dmaengine_get_unmap_data(dma->dev, 3, GFP_NOIO);
113 dma_addr_t dma_dest[2];
114 struct device *dev = dma->dev;
115 struct dma_async_tx_descriptor *tx;
116 enum dma_ctrl_flags dma_flags = DMA_COMPL_SKIP_SRC_UNMAP |
117 DMA_COMPL_SKIP_DEST_UNMAP |
118 DMA_PREP_PQ_DISABLE_P;
120 if (submit->flags & ASYNC_TX_FENCE)
121 dma_flags |= DMA_PREP_FENCE;
122 unmap->addr[0] = dma_map_page(dev, src, 0, len, DMA_TO_DEVICE);
124 unmap->addr[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
125 dma_dest[1] = unmap->addr[1];
129 /* this looks funny, but the engine looks for Q at
130 * dma_dest[1] and ignores dma_dest[0] as a dest
131 * due to DMA_PREP_PQ_DISABLE_P
133 tx = dma->device_prep_dma_pq(chan, dma_dest, unmap->addr,
134 1, &coef, len, dma_flags);
137 dma_set_unmap(tx, unmap);
138 dmaengine_unmap_put(unmap);
139 async_tx_submit(chan, tx, submit);
143 /* could not get a descriptor, unmap and fall through to
144 * the synchronous path
146 dmaengine_unmap_put(unmap);
149 /* no channel available, or failed to allocate a descriptor, so
150 * perform the operation synchronously
152 async_tx_quiesce(&submit->depend_tx);
153 qmul = raid6_gfmul[coef];
154 d = page_address(dest);
155 s = page_address(src);
163 static struct dma_async_tx_descriptor *
164 __2data_recov_4(int disks, size_t bytes, int faila, int failb,
165 struct page **blocks, struct async_submit_ctl *submit)
167 struct dma_async_tx_descriptor *tx = NULL;
168 struct page *p, *q, *a, *b;
169 struct page *srcs[2];
170 unsigned char coef[2];
171 enum async_tx_flags flags = submit->flags;
172 dma_async_tx_callback cb_fn = submit->cb_fn;
173 void *cb_param = submit->cb_param;
174 void *scribble = submit->scribble;
182 /* in the 4 disk case P + Pxy == P and Q + Qxy == Q */
183 /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
186 coef[0] = raid6_gfexi[failb-faila];
187 coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
188 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
189 tx = async_sum_product(b, srcs, coef, bytes, submit);
194 init_async_submit(submit, flags | ASYNC_TX_XOR_ZERO_DST, tx, cb_fn,
196 tx = async_xor(a, srcs, 0, 2, bytes, submit);
202 static struct dma_async_tx_descriptor *
203 __2data_recov_5(int disks, size_t bytes, int faila, int failb,
204 struct page **blocks, struct async_submit_ctl *submit)
206 struct dma_async_tx_descriptor *tx = NULL;
207 struct page *p, *q, *g, *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 int good_srcs, good, i;
218 for (i = 0; i < disks-2; i++) {
219 if (blocks[i] == NULL)
221 if (i == faila || i == failb)
226 BUG_ON(good_srcs > 1);
232 /* Compute syndrome with zero for the missing data pages
233 * Use the dead data pages as temporary storage for delta p and
239 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
240 tx = async_memcpy(dp, g, 0, 0, bytes, submit);
241 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
242 tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
244 /* compute P + Pxy */
247 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
248 NULL, NULL, scribble);
249 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
251 /* compute Q + Qxy */
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);
258 /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
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);
269 init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
271 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
276 static struct dma_async_tx_descriptor *
277 __2data_recov_n(int disks, size_t bytes, int faila, int failb,
278 struct page **blocks, struct async_submit_ctl *submit)
280 struct dma_async_tx_descriptor *tx = NULL;
281 struct page *p, *q, *dp, *dq;
282 struct page *srcs[2];
283 unsigned char coef[2];
284 enum async_tx_flags flags = submit->flags;
285 dma_async_tx_callback cb_fn = submit->cb_fn;
286 void *cb_param = submit->cb_param;
287 void *scribble = submit->scribble;
292 /* Compute syndrome with zero for the missing data pages
293 * Use the dead data pages as temporary storage for
294 * delta p and delta q
297 blocks[faila] = NULL;
298 blocks[disks-2] = dp;
300 blocks[failb] = NULL;
301 blocks[disks-1] = dq;
303 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
304 tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
306 /* Restore pointer table */
312 /* compute P + Pxy */
315 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
316 NULL, NULL, scribble);
317 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
319 /* compute Q + Qxy */
322 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
323 NULL, NULL, scribble);
324 tx = async_xor(dq, srcs, 0, 2, bytes, submit);
326 /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
329 coef[0] = raid6_gfexi[failb-faila];
330 coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
331 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
332 tx = async_sum_product(dq, srcs, coef, bytes, submit);
337 init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
339 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
345 * async_raid6_2data_recov - asynchronously calculate two missing data blocks
346 * @disks: number of disks in the RAID-6 array
348 * @faila: first failed drive index
349 * @failb: second failed drive index
350 * @blocks: array of source pointers where the last two entries are p and q
351 * @submit: submission/completion modifiers
353 struct dma_async_tx_descriptor *
354 async_raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
355 struct page **blocks, struct async_submit_ctl *submit)
357 void *scribble = submit->scribble;
358 int non_zero_srcs, i;
360 BUG_ON(faila == failb);
364 pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
366 /* if a dma resource is not available or a scribble buffer is not
367 * available punt to the synchronous path. In the 'dma not
368 * available' case be sure to use the scribble buffer to
369 * preserve the content of 'blocks' as the caller intended.
371 if (!async_dma_find_channel(DMA_PQ) || !scribble) {
372 void **ptrs = scribble ? scribble : (void **) blocks;
374 async_tx_quiesce(&submit->depend_tx);
375 for (i = 0; i < disks; i++)
376 if (blocks[i] == NULL)
377 ptrs[i] = (void *) raid6_empty_zero_page;
379 ptrs[i] = page_address(blocks[i]);
381 raid6_2data_recov(disks, bytes, faila, failb, ptrs);
383 async_tx_sync_epilog(submit);
389 for (i = 0; i < disks-2 && non_zero_srcs < 4; i++)
392 switch (non_zero_srcs) {
395 /* There must be at least 2 sources - the failed devices. */
399 /* dma devices do not uniformly understand a zero source pq
400 * operation (in contrast to the synchronous case), so
401 * explicitly handle the special case of a 4 disk array with
402 * both data disks missing.
404 return __2data_recov_4(disks, bytes, faila, failb, blocks, submit);
406 /* dma devices do not uniformly understand a single
407 * source pq operation (in contrast to the synchronous
408 * case), so explicitly handle the special case of a 5 disk
409 * array with 2 of 3 data disks missing.
411 return __2data_recov_5(disks, bytes, faila, failb, blocks, submit);
413 return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
416 EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
419 * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
420 * @disks: number of disks in the RAID-6 array
422 * @faila: failed drive index
423 * @blocks: array of source pointers where the last two entries are p and q
424 * @submit: submission/completion modifiers
426 struct dma_async_tx_descriptor *
427 async_raid6_datap_recov(int disks, size_t bytes, int faila,
428 struct page **blocks, struct async_submit_ctl *submit)
430 struct dma_async_tx_descriptor *tx = NULL;
431 struct page *p, *q, *dq;
433 enum async_tx_flags flags = submit->flags;
434 dma_async_tx_callback cb_fn = submit->cb_fn;
435 void *cb_param = submit->cb_param;
436 void *scribble = submit->scribble;
437 int good_srcs, good, i;
438 struct page *srcs[2];
440 pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
442 /* if a dma resource is not available or a scribble buffer is not
443 * available punt to the synchronous path. In the 'dma not
444 * available' case be sure to use the scribble buffer to
445 * preserve the content of 'blocks' as the caller intended.
447 if (!async_dma_find_channel(DMA_PQ) || !scribble) {
448 void **ptrs = scribble ? scribble : (void **) blocks;
450 async_tx_quiesce(&submit->depend_tx);
451 for (i = 0; i < disks; i++)
452 if (blocks[i] == NULL)
453 ptrs[i] = (void*)raid6_empty_zero_page;
455 ptrs[i] = page_address(blocks[i]);
457 raid6_datap_recov(disks, bytes, faila, ptrs);
459 async_tx_sync_epilog(submit);
466 for (i = 0; i < disks-2; i++) {
476 BUG_ON(good_srcs == 0);
481 /* Compute syndrome with zero for the missing data page
482 * Use the dead data page as temporary storage for delta q
485 blocks[faila] = NULL;
486 blocks[disks-1] = dq;
488 /* in the 4-disk case we only need to perform a single source
489 * multiplication with the one good data block.
491 if (good_srcs == 1) {
492 struct page *g = blocks[good];
494 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
496 tx = async_memcpy(p, g, 0, 0, bytes, submit);
498 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
500 tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
502 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
504 tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
507 /* Restore pointer table */
511 /* calculate g^{-faila} */
512 coef = raid6_gfinv[raid6_gfexp[faila]];
516 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
517 NULL, NULL, scribble);
518 tx = async_xor(dq, srcs, 0, 2, bytes, submit);
520 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
521 tx = async_mult(dq, dq, coef, bytes, submit);
525 init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
527 tx = async_xor(p, srcs, 0, 2, bytes, submit);
531 EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
533 MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
534 MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
535 MODULE_LICENSE("GPL");