]> Pileus Git - ~andy/linux/blob - crypto/async_tx/async_raid6_recov.c
async_raid6_recov: convert to dmaengine_unmap_data
[~andy/linux] / crypto / async_tx / async_raid6_recov.c
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
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)
34 {
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;
40         u8 ax, bx;
41         u8 *a, *b, *c;
42
43         if (dma)
44                 unmap = dmaengine_get_unmap_data(dma->dev, 3, GFP_NOIO);
45
46         if (unmap) {
47                 struct device *dev = dma->dev;
48                 dma_addr_t pq[2];
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;
53
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);
58                 unmap->to_cnt = 2;
59
60                 unmap->addr[2] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
61                 unmap->bidi_cnt = 1;
62                 /* engine only looks at Q, but expects it to follow P */
63                 pq[1] = unmap->addr[2];
64
65                 unmap->len = len;
66                 tx = dma->device_prep_dma_pq(chan, pq, unmap->addr, 2, coef,
67                                              len, dma_flags);
68                 if (tx) {
69                         dma_set_unmap(tx, unmap);
70                         async_tx_submit(chan, tx, submit);
71                         dmaengine_unmap_put(unmap);
72                         return tx;
73                 }
74
75                 /* could not get a descriptor, unmap and fall through to
76                  * the synchronous path
77                  */
78                 dmaengine_unmap_put(unmap);
79         }
80
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);
88
89         while (len--) {
90                 ax    = amul[*a++];
91                 bx    = bmul[*b++];
92                 *c++ = ax ^ bx;
93         }
94
95         return NULL;
96 }
97
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)
101 {
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 */
107         u8 *d, *s;
108
109         if (dma)
110                 unmap = dmaengine_get_unmap_data(dma->dev, 3, GFP_NOIO);
111
112         if (unmap) {
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;
119
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);
123                 unmap->to_cnt++;
124                 unmap->addr[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
125                 dma_dest[1] = unmap->addr[1];
126                 unmap->bidi_cnt++;
127                 unmap->len = len;
128
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
132                  */
133                 tx = dma->device_prep_dma_pq(chan, dma_dest, unmap->addr,
134                                              1, &coef, len, dma_flags);
135
136                 if (tx) {
137                         dma_set_unmap(tx, unmap);
138                         dmaengine_unmap_put(unmap);
139                         async_tx_submit(chan, tx, submit);
140                         return tx;
141                 }
142
143                 /* could not get a descriptor, unmap and fall through to
144                  * the synchronous path
145                  */
146                 dmaengine_unmap_put(unmap);
147         }
148
149         /* no channel available, or failed to allocate a descriptor, so
150          * perform the operation synchronously
151          */
152         async_tx_quiesce(&submit->depend_tx);
153         qmul  = raid6_gfmul[coef];
154         d = page_address(dest);
155         s = page_address(src);
156
157         while (len--)
158                 *d++ = qmul[*s++];
159
160         return NULL;
161 }
162
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)
166 {
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;
175
176         p = blocks[disks-2];
177         q = blocks[disks-1];
178
179         a = blocks[faila];
180         b = blocks[failb];
181
182         /* in the 4 disk case P + Pxy == P and Q + Qxy == Q */
183         /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
184         srcs[0] = p;
185         srcs[1] = q;
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);
190
191         /* Dy = P+Pxy+Dx */
192         srcs[0] = p;
193         srcs[1] = b;
194         init_async_submit(submit, flags | ASYNC_TX_XOR_ZERO_DST, tx, cb_fn,
195                           cb_param, scribble);
196         tx = async_xor(a, srcs, 0, 2, bytes, submit);
197
198         return tx;
199
200 }
201
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)
205 {
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;
215
216         good_srcs = 0;
217         good = -1;
218         for (i = 0; i < disks-2; i++) {
219                 if (blocks[i] == NULL)
220                         continue;
221                 if (i == faila || i == failb)
222                         continue;
223                 good = i;
224                 good_srcs++;
225         }
226         BUG_ON(good_srcs > 1);
227
228         p = blocks[disks-2];
229         q = blocks[disks-1];
230         g = blocks[good];
231
232         /* Compute syndrome with zero for the missing data pages
233          * Use the dead data pages as temporary storage for delta p and
234          * delta q
235          */
236         dp = blocks[faila];
237         dq = blocks[failb];
238
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);
243
244         /* compute P + Pxy */
245         srcs[0] = dp;
246         srcs[1] = p;
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);
250
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
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
266         /* Dy = P+Pxy+Dx */
267         srcs[0] = dp;
268         srcs[1] = dq;
269         init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
270                           cb_param, scribble);
271         tx = async_xor(dp, srcs, 0, 2, bytes, submit);
272
273         return tx;
274 }
275
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)
279 {
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;
288
289         p = blocks[disks-2];
290         q = blocks[disks-1];
291
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
295          */
296         dp = blocks[faila];
297         blocks[faila] = NULL;
298         blocks[disks-2] = dp;
299         dq = blocks[failb];
300         blocks[failb] = NULL;
301         blocks[disks-1] = dq;
302
303         init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
304         tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
305
306         /* Restore pointer table */
307         blocks[faila]   = dp;
308         blocks[failb]   = dq;
309         blocks[disks-2] = p;
310         blocks[disks-1] = q;
311
312         /* compute P + Pxy */
313         srcs[0] = dp;
314         srcs[1] = p;
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);
318
319         /* compute Q + Qxy */
320         srcs[0] = dq;
321         srcs[1] = q;
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);
325
326         /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
327         srcs[0] = dp;
328         srcs[1] = dq;
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);
333
334         /* Dy = P+Pxy+Dx */
335         srcs[0] = dp;
336         srcs[1] = dq;
337         init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
338                           cb_param, scribble);
339         tx = async_xor(dp, srcs, 0, 2, bytes, submit);
340
341         return tx;
342 }
343
344 /**
345  * async_raid6_2data_recov - asynchronously calculate two missing data blocks
346  * @disks: number of disks in the RAID-6 array
347  * @bytes: block size
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
352  */
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)
356 {
357         void *scribble = submit->scribble;
358         int non_zero_srcs, i;
359
360         BUG_ON(faila == failb);
361         if (failb < faila)
362                 swap(faila, failb);
363
364         pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
365
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.
370          */
371         if (!async_dma_find_channel(DMA_PQ) || !scribble) {
372                 void **ptrs = scribble ? scribble : (void **) blocks;
373
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;
378                         else
379                                 ptrs[i] = page_address(blocks[i]);
380
381                 raid6_2data_recov(disks, bytes, faila, failb, ptrs);
382
383                 async_tx_sync_epilog(submit);
384
385                 return NULL;
386         }
387
388         non_zero_srcs = 0;
389         for (i = 0; i < disks-2 && non_zero_srcs < 4; i++)
390                 if (blocks[i])
391                         non_zero_srcs++;
392         switch (non_zero_srcs) {
393         case 0:
394         case 1:
395                 /* There must be at least 2 sources - the failed devices. */
396                 BUG();
397
398         case 2:
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.
403                  */
404                 return __2data_recov_4(disks, bytes, faila, failb, blocks, submit);
405         case 3:
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.
410                  */
411                 return __2data_recov_5(disks, bytes, faila, failb, blocks, submit);
412         default:
413                 return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
414         }
415 }
416 EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
417
418 /**
419  * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
420  * @disks: number of disks in the RAID-6 array
421  * @bytes: block size
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
425  */
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)
429 {
430         struct dma_async_tx_descriptor *tx = NULL;
431         struct page *p, *q, *dq;
432         u8 coef;
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];
439
440         pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
441
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.
446          */
447         if (!async_dma_find_channel(DMA_PQ) || !scribble) {
448                 void **ptrs = scribble ? scribble : (void **) blocks;
449
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;
454                         else
455                                 ptrs[i] = page_address(blocks[i]);
456
457                 raid6_datap_recov(disks, bytes, faila, ptrs);
458
459                 async_tx_sync_epilog(submit);
460
461                 return NULL;
462         }
463
464         good_srcs = 0;
465         good = -1;
466         for (i = 0; i < disks-2; i++) {
467                 if (i == faila)
468                         continue;
469                 if (blocks[i]) {
470                         good = i;
471                         good_srcs++;
472                         if (good_srcs > 1)
473                                 break;
474                 }
475         }
476         BUG_ON(good_srcs == 0);
477
478         p = blocks[disks-2];
479         q = blocks[disks-1];
480
481         /* Compute syndrome with zero for the missing data page
482          * Use the dead data page as temporary storage for delta q
483          */
484         dq = blocks[faila];
485         blocks[faila] = NULL;
486         blocks[disks-1] = dq;
487
488         /* in the 4-disk case we only need to perform a single source
489          * multiplication with the one good data block.
490          */
491         if (good_srcs == 1) {
492                 struct page *g = blocks[good];
493
494                 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
495                                   scribble);
496                 tx = async_memcpy(p, g, 0, 0, bytes, submit);
497
498                 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
499                                   scribble);
500                 tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
501         } else {
502                 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
503                                   scribble);
504                 tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
505         }
506
507         /* Restore pointer table */
508         blocks[faila]   = dq;
509         blocks[disks-1] = q;
510
511         /* calculate g^{-faila} */
512         coef = raid6_gfinv[raid6_gfexp[faila]];
513
514         srcs[0] = dq;
515         srcs[1] = q;
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);
519
520         init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
521         tx = async_mult(dq, dq, coef, bytes, submit);
522
523         srcs[0] = p;
524         srcs[1] = dq;
525         init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
526                           cb_param, scribble);
527         tx = async_xor(p, srcs, 0, 2, bytes, submit);
528
529         return tx;
530 }
531 EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
532
533 MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
534 MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
535 MODULE_LICENSE("GPL");