blob: 8e30b6ed078936d6e16e7a951882362115ee6bbd [file] [log] [blame]
Dan Williams0a82a622009-07-14 12:20:37 -07001/*
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
29static struct dma_async_tx_descriptor *
30async_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
40 if (dma) {
41 dma_addr_t dma_dest[2];
42 dma_addr_t dma_src[2];
43 struct device *dev = dma->dev;
44 struct dma_async_tx_descriptor *tx;
45 enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;
46
Dan Williams0403e382009-09-08 17:42:50 -070047 if (submit->flags & ASYNC_TX_FENCE)
48 dma_flags |= DMA_PREP_FENCE;
Dan Williams0a82a622009-07-14 12:20:37 -070049 dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
50 dma_src[0] = dma_map_page(dev, srcs[0], 0, len, DMA_TO_DEVICE);
51 dma_src[1] = dma_map_page(dev, srcs[1], 0, len, DMA_TO_DEVICE);
52 tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 2, coef,
53 len, dma_flags);
54 if (tx) {
55 async_tx_submit(chan, tx, submit);
56 return tx;
57 }
Dan Williams1f6672d2009-09-21 10:47:40 -070058
59 /* could not get a descriptor, unmap and fall through to
60 * the synchronous path
61 */
62 dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);
63 dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);
64 dma_unmap_page(dev, dma_src[1], len, DMA_TO_DEVICE);
Dan Williams0a82a622009-07-14 12:20:37 -070065 }
66
67 /* run the operation synchronously */
68 async_tx_quiesce(&submit->depend_tx);
69 amul = raid6_gfmul[coef[0]];
70 bmul = raid6_gfmul[coef[1]];
71 a = page_address(srcs[0]);
72 b = page_address(srcs[1]);
73 c = page_address(dest);
74
75 while (len--) {
76 ax = amul[*a++];
77 bx = bmul[*b++];
78 *c++ = ax ^ bx;
79 }
80
81 return NULL;
82}
83
84static struct dma_async_tx_descriptor *
85async_mult(struct page *dest, struct page *src, u8 coef, size_t len,
86 struct async_submit_ctl *submit)
87{
88 struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
89 &dest, 1, &src, 1, len);
90 struct dma_device *dma = chan ? chan->device : NULL;
91 const u8 *qmul; /* Q multiplier table */
92 u8 *d, *s;
93
94 if (dma) {
95 dma_addr_t dma_dest[2];
96 dma_addr_t dma_src[1];
97 struct device *dev = dma->dev;
98 struct dma_async_tx_descriptor *tx;
99 enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;
100
Dan Williams0403e382009-09-08 17:42:50 -0700101 if (submit->flags & ASYNC_TX_FENCE)
102 dma_flags |= DMA_PREP_FENCE;
Dan Williams0a82a622009-07-14 12:20:37 -0700103 dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);
104 dma_src[0] = dma_map_page(dev, src, 0, len, DMA_TO_DEVICE);
105 tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 1, &coef,
106 len, dma_flags);
107 if (tx) {
108 async_tx_submit(chan, tx, submit);
109 return tx;
110 }
Dan Williams1f6672d2009-09-21 10:47:40 -0700111
112 /* could not get a descriptor, unmap and fall through to
113 * the synchronous path
114 */
115 dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);
116 dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);
Dan Williams0a82a622009-07-14 12:20:37 -0700117 }
118
119 /* no channel available, or failed to allocate a descriptor, so
120 * perform the operation synchronously
121 */
122 async_tx_quiesce(&submit->depend_tx);
123 qmul = raid6_gfmul[coef];
124 d = page_address(dest);
125 s = page_address(src);
126
127 while (len--)
128 *d++ = qmul[*s++];
129
130 return NULL;
131}
132
133static struct dma_async_tx_descriptor *
134__2data_recov_4(size_t bytes, int faila, int failb, struct page **blocks,
135 struct async_submit_ctl *submit)
136{
137 struct dma_async_tx_descriptor *tx = NULL;
138 struct page *p, *q, *a, *b;
139 struct page *srcs[2];
140 unsigned char coef[2];
141 enum async_tx_flags flags = submit->flags;
142 dma_async_tx_callback cb_fn = submit->cb_fn;
143 void *cb_param = submit->cb_param;
144 void *scribble = submit->scribble;
145
146 p = blocks[4-2];
147 q = blocks[4-1];
148
149 a = blocks[faila];
150 b = blocks[failb];
151
152 /* in the 4 disk case P + Pxy == P and Q + Qxy == Q */
153 /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
154 srcs[0] = p;
155 srcs[1] = q;
156 coef[0] = raid6_gfexi[failb-faila];
157 coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
Dan Williams0403e382009-09-08 17:42:50 -0700158 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700159 tx = async_sum_product(b, srcs, coef, bytes, submit);
160
161 /* Dy = P+Pxy+Dx */
162 srcs[0] = p;
163 srcs[1] = b;
164 init_async_submit(submit, flags | ASYNC_TX_XOR_ZERO_DST, tx, cb_fn,
165 cb_param, scribble);
166 tx = async_xor(a, srcs, 0, 2, bytes, submit);
167
168 return tx;
169
170}
171
172static struct dma_async_tx_descriptor *
173__2data_recov_5(size_t bytes, int faila, int failb, struct page **blocks,
174 struct async_submit_ctl *submit)
175{
176 struct dma_async_tx_descriptor *tx = NULL;
177 struct page *p, *q, *g, *dp, *dq;
178 struct page *srcs[2];
179 unsigned char coef[2];
180 enum async_tx_flags flags = submit->flags;
181 dma_async_tx_callback cb_fn = submit->cb_fn;
182 void *cb_param = submit->cb_param;
183 void *scribble = submit->scribble;
184 int uninitialized_var(good);
185 int i;
186
187 for (i = 0; i < 3; i++) {
188 if (i == faila || i == failb)
189 continue;
190 else {
191 good = i;
192 break;
193 }
194 }
195 BUG_ON(i >= 3);
196
197 p = blocks[5-2];
198 q = blocks[5-1];
199 g = blocks[good];
200
201 /* Compute syndrome with zero for the missing data pages
202 * Use the dead data pages as temporary storage for delta p and
203 * delta q
204 */
205 dp = blocks[faila];
206 dq = blocks[failb];
207
Dan Williams0403e382009-09-08 17:42:50 -0700208 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700209 tx = async_memcpy(dp, g, 0, 0, bytes, submit);
Dan Williams0403e382009-09-08 17:42:50 -0700210 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700211 tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
212
213 /* compute P + Pxy */
214 srcs[0] = dp;
215 srcs[1] = p;
Dan Williams0403e382009-09-08 17:42:50 -0700216 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
217 NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700218 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
219
220 /* compute Q + Qxy */
221 srcs[0] = dq;
222 srcs[1] = q;
Dan Williams0403e382009-09-08 17:42:50 -0700223 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
224 NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700225 tx = async_xor(dq, srcs, 0, 2, bytes, submit);
226
227 /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
228 srcs[0] = dp;
229 srcs[1] = dq;
230 coef[0] = raid6_gfexi[failb-faila];
231 coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
Dan Williams0403e382009-09-08 17:42:50 -0700232 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700233 tx = async_sum_product(dq, srcs, coef, bytes, submit);
234
235 /* Dy = P+Pxy+Dx */
236 srcs[0] = dp;
237 srcs[1] = dq;
238 init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
239 cb_param, scribble);
240 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
241
242 return tx;
243}
244
245static struct dma_async_tx_descriptor *
246__2data_recov_n(int disks, size_t bytes, int faila, int failb,
247 struct page **blocks, struct async_submit_ctl *submit)
248{
249 struct dma_async_tx_descriptor *tx = NULL;
250 struct page *p, *q, *dp, *dq;
251 struct page *srcs[2];
252 unsigned char coef[2];
253 enum async_tx_flags flags = submit->flags;
254 dma_async_tx_callback cb_fn = submit->cb_fn;
255 void *cb_param = submit->cb_param;
256 void *scribble = submit->scribble;
257
258 p = blocks[disks-2];
259 q = blocks[disks-1];
260
261 /* Compute syndrome with zero for the missing data pages
262 * Use the dead data pages as temporary storage for
263 * delta p and delta q
264 */
265 dp = blocks[faila];
NeilBrown5dd33c92009-10-16 16:40:25 +1100266 blocks[faila] = NULL;
Dan Williams0a82a622009-07-14 12:20:37 -0700267 blocks[disks-2] = dp;
268 dq = blocks[failb];
NeilBrown5dd33c92009-10-16 16:40:25 +1100269 blocks[failb] = NULL;
Dan Williams0a82a622009-07-14 12:20:37 -0700270 blocks[disks-1] = dq;
271
Dan Williams0403e382009-09-08 17:42:50 -0700272 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700273 tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
274
275 /* Restore pointer table */
276 blocks[faila] = dp;
277 blocks[failb] = dq;
278 blocks[disks-2] = p;
279 blocks[disks-1] = q;
280
281 /* compute P + Pxy */
282 srcs[0] = dp;
283 srcs[1] = p;
Dan Williams0403e382009-09-08 17:42:50 -0700284 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
285 NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700286 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
287
288 /* compute Q + Qxy */
289 srcs[0] = dq;
290 srcs[1] = q;
Dan Williams0403e382009-09-08 17:42:50 -0700291 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
292 NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700293 tx = async_xor(dq, srcs, 0, 2, bytes, submit);
294
295 /* Dx = A*(P+Pxy) + B*(Q+Qxy) */
296 srcs[0] = dp;
297 srcs[1] = dq;
298 coef[0] = raid6_gfexi[failb-faila];
299 coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];
Dan Williams0403e382009-09-08 17:42:50 -0700300 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700301 tx = async_sum_product(dq, srcs, coef, bytes, submit);
302
303 /* Dy = P+Pxy+Dx */
304 srcs[0] = dp;
305 srcs[1] = dq;
306 init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
307 cb_param, scribble);
308 tx = async_xor(dp, srcs, 0, 2, bytes, submit);
309
310 return tx;
311}
312
313/**
314 * async_raid6_2data_recov - asynchronously calculate two missing data blocks
315 * @disks: number of disks in the RAID-6 array
316 * @bytes: block size
317 * @faila: first failed drive index
318 * @failb: second failed drive index
319 * @blocks: array of source pointers where the last two entries are p and q
320 * @submit: submission/completion modifiers
321 */
322struct dma_async_tx_descriptor *
323async_raid6_2data_recov(int disks, size_t bytes, int faila, int failb,
324 struct page **blocks, struct async_submit_ctl *submit)
325{
326 BUG_ON(faila == failb);
327 if (failb < faila)
328 swap(faila, failb);
329
330 pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
331
332 /* we need to preserve the contents of 'blocks' for the async
333 * case, so punt to synchronous if a scribble buffer is not available
334 */
335 if (!submit->scribble) {
336 void **ptrs = (void **) blocks;
337 int i;
338
339 async_tx_quiesce(&submit->depend_tx);
340 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100341 if (blocks[i] == NULL)
342 ptrs[i] = (void*)raid6_empty_zero_page;
343 else
344 ptrs[i] = page_address(blocks[i]);
Dan Williams0a82a622009-07-14 12:20:37 -0700345
346 raid6_2data_recov(disks, bytes, faila, failb, ptrs);
347
348 async_tx_sync_epilog(submit);
349
350 return NULL;
351 }
352
353 switch (disks) {
354 case 4:
355 /* dma devices do not uniformly understand a zero source pq
356 * operation (in contrast to the synchronous case), so
357 * explicitly handle the 4 disk special case
358 */
359 return __2data_recov_4(bytes, faila, failb, blocks, submit);
360 case 5:
361 /* dma devices do not uniformly understand a single
362 * source pq operation (in contrast to the synchronous
363 * case), so explicitly handle the 5 disk special case
364 */
365 return __2data_recov_5(bytes, faila, failb, blocks, submit);
366 default:
367 return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);
368 }
369}
370EXPORT_SYMBOL_GPL(async_raid6_2data_recov);
371
372/**
373 * async_raid6_datap_recov - asynchronously calculate a data and the 'p' block
374 * @disks: number of disks in the RAID-6 array
375 * @bytes: block size
376 * @faila: failed drive index
377 * @blocks: array of source pointers where the last two entries are p and q
378 * @submit: submission/completion modifiers
379 */
380struct dma_async_tx_descriptor *
381async_raid6_datap_recov(int disks, size_t bytes, int faila,
382 struct page **blocks, struct async_submit_ctl *submit)
383{
384 struct dma_async_tx_descriptor *tx = NULL;
385 struct page *p, *q, *dq;
386 u8 coef;
387 enum async_tx_flags flags = submit->flags;
388 dma_async_tx_callback cb_fn = submit->cb_fn;
389 void *cb_param = submit->cb_param;
390 void *scribble = submit->scribble;
391 struct page *srcs[2];
392
393 pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);
394
395 /* we need to preserve the contents of 'blocks' for the async
396 * case, so punt to synchronous if a scribble buffer is not available
397 */
398 if (!scribble) {
399 void **ptrs = (void **) blocks;
400 int i;
401
402 async_tx_quiesce(&submit->depend_tx);
403 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100404 if (blocks[i] == NULL)
405 ptrs[i] = (void*)raid6_empty_zero_page;
406 else
407 ptrs[i] = page_address(blocks[i]);
Dan Williams0a82a622009-07-14 12:20:37 -0700408
409 raid6_datap_recov(disks, bytes, faila, ptrs);
410
411 async_tx_sync_epilog(submit);
412
413 return NULL;
414 }
415
416 p = blocks[disks-2];
417 q = blocks[disks-1];
418
419 /* Compute syndrome with zero for the missing data page
420 * Use the dead data page as temporary storage for delta q
421 */
422 dq = blocks[faila];
NeilBrown5dd33c92009-10-16 16:40:25 +1100423 blocks[faila] = NULL;
Dan Williams0a82a622009-07-14 12:20:37 -0700424 blocks[disks-1] = dq;
425
426 /* in the 4 disk case we only need to perform a single source
427 * multiplication
428 */
429 if (disks == 4) {
430 int good = faila == 0 ? 1 : 0;
431 struct page *g = blocks[good];
432
Dan Williams0403e382009-09-08 17:42:50 -0700433 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
434 scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700435 tx = async_memcpy(p, g, 0, 0, bytes, submit);
436
Dan Williams0403e382009-09-08 17:42:50 -0700437 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
438 scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700439 tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);
440 } else {
Dan Williams0403e382009-09-08 17:42:50 -0700441 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,
442 scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700443 tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);
444 }
445
446 /* Restore pointer table */
447 blocks[faila] = dq;
448 blocks[disks-1] = q;
449
450 /* calculate g^{-faila} */
451 coef = raid6_gfinv[raid6_gfexp[faila]];
452
453 srcs[0] = dq;
454 srcs[1] = q;
Dan Williams0403e382009-09-08 17:42:50 -0700455 init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
456 NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700457 tx = async_xor(dq, srcs, 0, 2, bytes, submit);
458
Dan Williams0403e382009-09-08 17:42:50 -0700459 init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);
Dan Williams0a82a622009-07-14 12:20:37 -0700460 tx = async_mult(dq, dq, coef, bytes, submit);
461
462 srcs[0] = p;
463 srcs[1] = dq;
464 init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,
465 cb_param, scribble);
466 tx = async_xor(p, srcs, 0, 2, bytes, submit);
467
468 return tx;
469}
470EXPORT_SYMBOL_GPL(async_raid6_datap_recov);
471
472MODULE_AUTHOR("Dan Williams <dan.j.williams@intel.com>");
473MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");
474MODULE_LICENSE("GPL");