2 * Copyright(c) 2007 Yuri Tikhonov <yur@emcraft.com>
3 * Copyright(c) 2009 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59
17 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The full GNU General Public License is included in this distribution in the
20 * file called COPYING.
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/raid/pq.h>
27 #include <linux/async_tx.h>
28 #include <linux/gfp.h>
31 * pq_scribble_page - space to hold throwaway P or Q buffer for
32 * synchronous gen_syndrome
34 static struct page
*pq_scribble_page
;
36 /* the struct page *blocks[] parameter passed to async_gen_syndrome()
37 * and async_syndrome_val() contains the 'P' destination address at
38 * blocks[disks-2] and the 'Q' destination address at blocks[disks-1]
40 * note: these are macros as they are used as lvalues
42 #define P(b, d) (b[d-2])
43 #define Q(b, d) (b[d-1])
46 * do_async_gen_syndrome - asynchronously calculate P and/or Q
48 static __async_inline
struct dma_async_tx_descriptor
*
49 do_async_gen_syndrome(struct dma_chan
*chan
,
50 const unsigned char *scfs
, int disks
,
51 struct dmaengine_unmap_data
*unmap
,
52 enum dma_ctrl_flags dma_flags
,
53 struct async_submit_ctl
*submit
)
55 struct dma_async_tx_descriptor
*tx
= NULL
;
56 struct dma_device
*dma
= chan
->device
;
57 enum async_tx_flags flags_orig
= submit
->flags
;
58 dma_async_tx_callback cb_fn_orig
= submit
->cb_fn
;
59 dma_async_tx_callback cb_param_orig
= submit
->cb_param
;
60 int src_cnt
= disks
- 2;
61 unsigned short pq_src_cnt
;
62 dma_addr_t dma_dest
[2];
65 dma_flags
|= DMA_COMPL_SKIP_SRC_UNMAP
| DMA_COMPL_SKIP_DEST_UNMAP
;
66 if (submit
->flags
& ASYNC_TX_FENCE
)
67 dma_flags
|= DMA_PREP_FENCE
;
70 submit
->flags
= flags_orig
;
71 pq_src_cnt
= min(src_cnt
, dma_maxpq(dma
, dma_flags
));
72 /* if we are submitting additional pqs, leave the chain open,
73 * clear the callback parameters, and leave the destination
76 if (src_cnt
> pq_src_cnt
) {
77 submit
->flags
&= ~ASYNC_TX_ACK
;
78 submit
->flags
|= ASYNC_TX_FENCE
;
80 submit
->cb_param
= NULL
;
82 submit
->cb_fn
= cb_fn_orig
;
83 submit
->cb_param
= cb_param_orig
;
85 dma_flags
|= DMA_PREP_INTERRUPT
;
88 /* Drivers force forward progress in case they can not provide
92 dma_dest
[0] = unmap
->addr
[disks
- 2];
93 dma_dest
[1] = unmap
->addr
[disks
- 1];
94 tx
= dma
->device_prep_dma_pq(chan
, dma_dest
,
95 &unmap
->addr
[src_off
],
97 &scfs
[src_off
], unmap
->len
,
101 async_tx_quiesce(&submit
->depend_tx
);
102 dma_async_issue_pending(chan
);
105 dma_set_unmap(tx
, unmap
);
106 async_tx_submit(chan
, tx
, submit
);
107 submit
->depend_tx
= tx
;
109 /* drop completed sources */
110 src_cnt
-= pq_src_cnt
;
111 src_off
+= pq_src_cnt
;
113 dma_flags
|= DMA_PREP_CONTINUE
;
120 * do_sync_gen_syndrome - synchronously calculate a raid6 syndrome
123 do_sync_gen_syndrome(struct page
**blocks
, unsigned int offset
, int disks
,
124 size_t len
, struct async_submit_ctl
*submit
)
129 if (submit
->scribble
)
130 srcs
= submit
->scribble
;
132 srcs
= (void **) blocks
;
134 for (i
= 0; i
< disks
; i
++) {
135 if (blocks
[i
] == NULL
) {
136 BUG_ON(i
> disks
- 3); /* P or Q can't be zero */
137 srcs
[i
] = (void*)raid6_empty_zero_page
;
139 srcs
[i
] = page_address(blocks
[i
]) + offset
;
141 raid6_call
.gen_syndrome(disks
, len
, srcs
);
142 async_tx_sync_epilog(submit
);
146 * async_gen_syndrome - asynchronously calculate a raid6 syndrome
147 * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
148 * @offset: common offset into each block (src and dest) to start transaction
149 * @disks: number of blocks (including missing P or Q, see below)
150 * @len: length of operation in bytes
151 * @submit: submission/completion modifiers
153 * General note: This routine assumes a field of GF(2^8) with a
154 * primitive polynomial of 0x11d and a generator of {02}.
156 * 'disks' note: callers can optionally omit either P or Q (but not
157 * both) from the calculation by setting blocks[disks-2] or
158 * blocks[disks-1] to NULL. When P or Q is omitted 'len' must be <=
159 * PAGE_SIZE as a temporary buffer of this size is used in the
160 * synchronous path. 'disks' always accounts for both destination
161 * buffers. If any source buffers (blocks[i] where i < disks - 2) are
162 * set to NULL those buffers will be replaced with the raid6_zero_page
163 * in the synchronous path and omitted in the hardware-asynchronous
166 struct dma_async_tx_descriptor
*
167 async_gen_syndrome(struct page
**blocks
, unsigned int offset
, int disks
,
168 size_t len
, struct async_submit_ctl
*submit
)
170 int src_cnt
= disks
- 2;
171 struct dma_chan
*chan
= async_tx_find_channel(submit
, DMA_PQ
,
172 &P(blocks
, disks
), 2,
173 blocks
, src_cnt
, len
);
174 struct dma_device
*device
= chan
? chan
->device
: NULL
;
175 struct dmaengine_unmap_data
*unmap
= NULL
;
177 BUG_ON(disks
> 255 || !(P(blocks
, disks
) || Q(blocks
, disks
)));
180 unmap
= dmaengine_get_unmap_data(device
->dev
, disks
, GFP_NOIO
);
183 (src_cnt
<= dma_maxpq(device
, 0) ||
184 dma_maxpq(device
, DMA_PREP_CONTINUE
) > 0) &&
185 is_dma_pq_aligned(device
, offset
, 0, len
)) {
186 struct dma_async_tx_descriptor
*tx
;
187 enum dma_ctrl_flags dma_flags
= 0;
188 unsigned char coefs
[src_cnt
];
191 /* run the p+q asynchronously */
192 pr_debug("%s: (async) disks: %d len: %zu\n",
193 __func__
, disks
, len
);
195 /* convert source addresses being careful to collapse 'empty'
196 * sources and update the coefficients accordingly
199 for (i
= 0, j
= 0; i
< src_cnt
; i
++) {
200 if (blocks
[i
] == NULL
)
202 unmap
->addr
[j
] = dma_map_page(device
->dev
, blocks
[i
], offset
,
204 coefs
[j
] = raid6_gfexp
[i
];
210 * DMAs use destinations as sources,
211 * so use BIDIRECTIONAL mapping
214 if (P(blocks
, disks
))
215 unmap
->addr
[j
++] = dma_map_page(device
->dev
, P(blocks
, disks
),
216 offset
, len
, DMA_BIDIRECTIONAL
);
218 unmap
->addr
[j
++] = 0;
219 dma_flags
|= DMA_PREP_PQ_DISABLE_P
;
223 if (Q(blocks
, disks
))
224 unmap
->addr
[j
++] = dma_map_page(device
->dev
, Q(blocks
, disks
),
225 offset
, len
, DMA_BIDIRECTIONAL
);
227 unmap
->addr
[j
++] = 0;
228 dma_flags
|= DMA_PREP_PQ_DISABLE_Q
;
231 tx
= do_async_gen_syndrome(chan
, coefs
, j
, unmap
, dma_flags
, submit
);
232 dmaengine_unmap_put(unmap
);
236 dmaengine_unmap_put(unmap
);
238 /* run the pq synchronously */
239 pr_debug("%s: (sync) disks: %d len: %zu\n", __func__
, disks
, len
);
241 /* wait for any prerequisite operations */
242 async_tx_quiesce(&submit
->depend_tx
);
244 if (!P(blocks
, disks
)) {
245 P(blocks
, disks
) = pq_scribble_page
;
246 BUG_ON(len
+ offset
> PAGE_SIZE
);
248 if (!Q(blocks
, disks
)) {
249 Q(blocks
, disks
) = pq_scribble_page
;
250 BUG_ON(len
+ offset
> PAGE_SIZE
);
252 do_sync_gen_syndrome(blocks
, offset
, disks
, len
, submit
);
256 EXPORT_SYMBOL_GPL(async_gen_syndrome
);
258 static inline struct dma_chan
*
259 pq_val_chan(struct async_submit_ctl
*submit
, struct page
**blocks
, int disks
, size_t len
)
261 #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
264 return async_tx_find_channel(submit
, DMA_PQ_VAL
, NULL
, 0, blocks
,
269 * async_syndrome_val - asynchronously validate a raid6 syndrome
270 * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
271 * @offset: common offset into each block (src and dest) to start transaction
272 * @disks: number of blocks (including missing P or Q, see below)
273 * @len: length of operation in bytes
274 * @pqres: on val failure SUM_CHECK_P_RESULT and/or SUM_CHECK_Q_RESULT are set
275 * @spare: temporary result buffer for the synchronous case
276 * @submit: submission / completion modifiers
278 * The same notes from async_gen_syndrome apply to the 'blocks',
279 * and 'disks' parameters of this routine. The synchronous path
280 * requires a temporary result buffer and submit->scribble to be
283 struct dma_async_tx_descriptor
*
284 async_syndrome_val(struct page
**blocks
, unsigned int offset
, int disks
,
285 size_t len
, enum sum_check_flags
*pqres
, struct page
*spare
,
286 struct async_submit_ctl
*submit
)
288 struct dma_chan
*chan
= pq_val_chan(submit
, blocks
, disks
, len
);
289 struct dma_device
*device
= chan
? chan
->device
: NULL
;
290 struct dma_async_tx_descriptor
*tx
;
291 unsigned char coefs
[disks
-2];
292 enum dma_ctrl_flags dma_flags
= submit
->cb_fn
? DMA_PREP_INTERRUPT
: 0;
293 dma_addr_t
*dma_src
= NULL
;
298 if (submit
->scribble
)
299 dma_src
= submit
->scribble
;
300 else if (sizeof(dma_addr_t
) <= sizeof(struct page
*))
301 dma_src
= (dma_addr_t
*) blocks
;
303 if (dma_src
&& device
&& disks
<= dma_maxpq(device
, 0) &&
304 is_dma_pq_aligned(device
, offset
, 0, len
)) {
305 struct device
*dev
= device
->dev
;
306 dma_addr_t
*pq
= &dma_src
[disks
-2];
309 pr_debug("%s: (async) disks: %d len: %zu\n",
310 __func__
, disks
, len
);
311 if (!P(blocks
, disks
))
312 dma_flags
|= DMA_PREP_PQ_DISABLE_P
;
314 pq
[0] = dma_map_page(dev
, P(blocks
, disks
),
317 if (!Q(blocks
, disks
))
318 dma_flags
|= DMA_PREP_PQ_DISABLE_Q
;
320 pq
[1] = dma_map_page(dev
, Q(blocks
, disks
),
324 if (submit
->flags
& ASYNC_TX_FENCE
)
325 dma_flags
|= DMA_PREP_FENCE
;
326 for (i
= 0; i
< disks
-2; i
++)
327 if (likely(blocks
[i
])) {
328 dma_src
[src_cnt
] = dma_map_page(dev
, blocks
[i
],
331 coefs
[src_cnt
] = raid6_gfexp
[i
];
336 tx
= device
->device_prep_dma_pq_val(chan
, pq
, dma_src
,
343 async_tx_quiesce(&submit
->depend_tx
);
344 dma_async_issue_pending(chan
);
346 async_tx_submit(chan
, tx
, submit
);
350 struct page
*p_src
= P(blocks
, disks
);
351 struct page
*q_src
= Q(blocks
, disks
);
352 enum async_tx_flags flags_orig
= submit
->flags
;
353 dma_async_tx_callback cb_fn_orig
= submit
->cb_fn
;
354 void *scribble
= submit
->scribble
;
355 void *cb_param_orig
= submit
->cb_param
;
358 pr_debug("%s: (sync) disks: %d len: %zu\n",
359 __func__
, disks
, len
);
361 /* caller must provide a temporary result buffer and
362 * allow the input parameters to be preserved
364 BUG_ON(!spare
|| !scribble
);
366 /* wait for any prerequisite operations */
367 async_tx_quiesce(&submit
->depend_tx
);
369 /* recompute p and/or q into the temporary buffer and then
370 * check to see the result matches the current value
375 init_async_submit(submit
, ASYNC_TX_XOR_ZERO_DST
, NULL
,
376 NULL
, NULL
, scribble
);
377 tx
= async_xor(spare
, blocks
, offset
, disks
-2, len
, submit
);
378 async_tx_quiesce(&tx
);
379 p
= page_address(p_src
) + offset
;
380 s
= page_address(spare
) + offset
;
381 *pqres
|= !!memcmp(p
, s
, len
) << SUM_CHECK_P
;
385 P(blocks
, disks
) = NULL
;
386 Q(blocks
, disks
) = spare
;
387 init_async_submit(submit
, 0, NULL
, NULL
, NULL
, scribble
);
388 tx
= async_gen_syndrome(blocks
, offset
, disks
, len
, submit
);
389 async_tx_quiesce(&tx
);
390 q
= page_address(q_src
) + offset
;
391 s
= page_address(spare
) + offset
;
392 *pqres
|= !!memcmp(q
, s
, len
) << SUM_CHECK_Q
;
395 /* restore P, Q and submit */
396 P(blocks
, disks
) = p_src
;
397 Q(blocks
, disks
) = q_src
;
399 submit
->cb_fn
= cb_fn_orig
;
400 submit
->cb_param
= cb_param_orig
;
401 submit
->flags
= flags_orig
;
402 async_tx_sync_epilog(submit
);
407 EXPORT_SYMBOL_GPL(async_syndrome_val
);
409 static int __init
async_pq_init(void)
411 pq_scribble_page
= alloc_page(GFP_KERNEL
);
413 if (pq_scribble_page
)
416 pr_err("%s: failed to allocate required spare page\n", __func__
);
421 static void __exit
async_pq_exit(void)
423 put_page(pq_scribble_page
);
426 module_init(async_pq_init
);
427 module_exit(async_pq_exit
);
429 MODULE_DESCRIPTION("asynchronous raid6 syndrome generation/validation");
430 MODULE_LICENSE("GPL");