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/dma-mapping.h>
25 #include <linux/raid/pq.h>
26 #include <linux/async_tx.h>
29 * scribble - space to hold throwaway P buffer for synchronous gen_syndrome
31 static struct page *scribble;
33 static bool is_raid6_zero_block(struct page *p)
35 return p == (void *) raid6_empty_zero_page;
38 /* the struct page *blocks[] parameter passed to async_gen_syndrome()
39 * and async_syndrome_val() contains the 'P' destination address at
40 * blocks[disks-2] and the 'Q' destination address at blocks[disks-1]
42 * note: these are macros as they are used as lvalues
44 #define P(b, d) (b[d-2])
45 #define Q(b, d) (b[d-1])
48 * do_async_gen_syndrome - asynchronously calculate P and/or Q
50 static __async_inline struct dma_async_tx_descriptor *
51 do_async_gen_syndrome(struct dma_chan *chan, struct page **blocks,
52 const unsigned char *scfs, unsigned int offset, int disks,
53 size_t len, dma_addr_t *dma_src,
54 struct async_submit_ctl *submit)
56 struct dma_async_tx_descriptor *tx = NULL;
57 struct dma_device *dma = chan->device;
58 enum dma_ctrl_flags dma_flags = 0;
59 enum async_tx_flags flags_orig = submit->flags;
60 dma_async_tx_callback cb_fn_orig = submit->cb_fn;
61 dma_async_tx_callback cb_param_orig = submit->cb_param;
62 int src_cnt = disks - 2;
63 unsigned char coefs[src_cnt];
64 unsigned short pq_src_cnt;
65 dma_addr_t dma_dest[2];
70 /* DMAs use destinations as sources, so use BIDIRECTIONAL mapping */
72 dma_dest[0] = dma_map_page(dma->dev, P(blocks, disks), offset,
73 len, DMA_BIDIRECTIONAL);
75 dma_flags |= DMA_PREP_PQ_DISABLE_P;
77 dma_dest[1] = dma_map_page(dma->dev, Q(blocks, disks), offset,
78 len, DMA_BIDIRECTIONAL);
80 dma_flags |= DMA_PREP_PQ_DISABLE_Q;
82 /* convert source addresses being careful to collapse 'empty'
83 * sources and update the coefficients accordingly
85 for (i = 0, idx = 0; i < src_cnt; i++) {
86 if (is_raid6_zero_block(blocks[i]))
88 dma_src[idx] = dma_map_page(dma->dev, blocks[i], offset, len,
96 submit->flags = flags_orig;
97 pq_src_cnt = min(src_cnt, dma_maxpq(dma, dma_flags));
98 /* if we are submitting additional pqs, leave the chain open,
99 * clear the callback parameters, and leave the destination
102 if (src_cnt > pq_src_cnt) {
103 submit->flags &= ~ASYNC_TX_ACK;
104 submit->flags |= ASYNC_TX_FENCE;
105 dma_flags |= DMA_COMPL_SKIP_DEST_UNMAP;
106 submit->cb_fn = NULL;
107 submit->cb_param = NULL;
109 dma_flags &= ~DMA_COMPL_SKIP_DEST_UNMAP;
110 submit->cb_fn = cb_fn_orig;
111 submit->cb_param = cb_param_orig;
113 dma_flags |= DMA_PREP_INTERRUPT;
115 if (submit->flags & ASYNC_TX_FENCE)
116 dma_flags |= DMA_PREP_FENCE;
118 /* Since we have clobbered the src_list we are committed
119 * to doing this asynchronously. Drivers force forward
120 * progress in case they can not provide a descriptor
123 tx = dma->device_prep_dma_pq(chan, dma_dest,
126 &coefs[src_off], len,
130 async_tx_quiesce(&submit->depend_tx);
131 dma_async_issue_pending(chan);
134 async_tx_submit(chan, tx, submit);
135 submit->depend_tx = tx;
137 /* drop completed sources */
138 src_cnt -= pq_src_cnt;
139 src_off += pq_src_cnt;
141 dma_flags |= DMA_PREP_CONTINUE;
148 * do_sync_gen_syndrome - synchronously calculate a raid6 syndrome
151 do_sync_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
152 size_t len, struct async_submit_ctl *submit)
157 if (submit->scribble)
158 srcs = submit->scribble;
160 srcs = (void **) blocks;
162 for (i = 0; i < disks; i++) {
163 if (is_raid6_zero_block(blocks[i])) {
164 BUG_ON(i > disks - 3); /* P or Q can't be zero */
167 srcs[i] = page_address(blocks[i]) + offset;
169 raid6_call.gen_syndrome(disks, len, srcs);
170 async_tx_sync_epilog(submit);
174 * async_gen_syndrome - asynchronously calculate a raid6 syndrome
175 * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
176 * @offset: common offset into each block (src and dest) to start transaction
177 * @disks: number of blocks (including missing P or Q, see below)
178 * @len: length of operation in bytes
179 * @submit: submission/completion modifiers
181 * General note: This routine assumes a field of GF(2^8) with a
182 * primitive polynomial of 0x11d and a generator of {02}.
184 * 'disks' note: callers can optionally omit either P or Q (but not
185 * both) from the calculation by setting blocks[disks-2] or
186 * blocks[disks-1] to NULL. When P or Q is omitted 'len' must be <=
187 * PAGE_SIZE as a temporary buffer of this size is used in the
188 * synchronous path. 'disks' always accounts for both destination
191 * 'blocks' note: if submit->scribble is NULL then the contents of
192 * 'blocks' may be overridden
194 struct dma_async_tx_descriptor *
195 async_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
196 size_t len, struct async_submit_ctl *submit)
198 int src_cnt = disks - 2;
199 struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,
200 &P(blocks, disks), 2,
201 blocks, src_cnt, len);
202 struct dma_device *device = chan ? chan->device : NULL;
203 dma_addr_t *dma_src = NULL;
205 BUG_ON(disks > 255 || !(P(blocks, disks) || Q(blocks, disks)));
207 if (submit->scribble)
208 dma_src = submit->scribble;
209 else if (sizeof(dma_addr_t) <= sizeof(struct page *))
210 dma_src = (dma_addr_t *) blocks;
212 if (dma_src && device &&
213 (src_cnt <= dma_maxpq(device, 0) ||
214 dma_maxpq(device, DMA_PREP_CONTINUE) > 0)) {
215 /* run the p+q asynchronously */
216 pr_debug("%s: (async) disks: %d len: %zu\n",
217 __func__, disks, len);
218 return do_async_gen_syndrome(chan, blocks, raid6_gfexp, offset,
219 disks, len, dma_src, submit);
222 /* run the pq synchronously */
223 pr_debug("%s: (sync) disks: %d len: %zu\n", __func__, disks, len);
225 /* wait for any prerequisite operations */
226 async_tx_quiesce(&submit->depend_tx);
228 if (!P(blocks, disks)) {
229 P(blocks, disks) = scribble;
230 BUG_ON(len + offset > PAGE_SIZE);
232 if (!Q(blocks, disks)) {
233 Q(blocks, disks) = scribble;
234 BUG_ON(len + offset > PAGE_SIZE);
236 do_sync_gen_syndrome(blocks, offset, disks, len, submit);
240 EXPORT_SYMBOL_GPL(async_gen_syndrome);
243 * async_syndrome_val - asynchronously validate a raid6 syndrome
244 * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
245 * @offset: common offset into each block (src and dest) to start transaction
246 * @disks: number of blocks (including missing P or Q, see below)
247 * @len: length of operation in bytes
248 * @pqres: on val failure SUM_CHECK_P_RESULT and/or SUM_CHECK_Q_RESULT are set
249 * @spare: temporary result buffer for the synchronous case
250 * @submit: submission / completion modifiers
252 * The same notes from async_gen_syndrome apply to the 'blocks',
253 * and 'disks' parameters of this routine. The synchronous path
254 * requires a temporary result buffer and submit->scribble to be
257 struct dma_async_tx_descriptor *
258 async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
259 size_t len, enum sum_check_flags *pqres, struct page *spare,
260 struct async_submit_ctl *submit)
262 struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ_VAL,
263 NULL, 0, blocks, disks,
265 struct dma_device *device = chan ? chan->device : NULL;
266 struct dma_async_tx_descriptor *tx;
267 enum dma_ctrl_flags dma_flags = submit->cb_fn ? DMA_PREP_INTERRUPT : 0;
268 dma_addr_t *dma_src = NULL;
272 if (submit->scribble)
273 dma_src = submit->scribble;
274 else if (sizeof(dma_addr_t) <= sizeof(struct page *))
275 dma_src = (dma_addr_t *) blocks;
277 if (dma_src && device && disks <= dma_maxpq(device, 0)) {
278 struct device *dev = device->dev;
279 dma_addr_t *pq = &dma_src[disks-2];
282 pr_debug("%s: (async) disks: %d len: %zu\n",
283 __func__, disks, len);
284 if (!P(blocks, disks))
285 dma_flags |= DMA_PREP_PQ_DISABLE_P;
286 if (!Q(blocks, disks))
287 dma_flags |= DMA_PREP_PQ_DISABLE_Q;
288 if (submit->flags & ASYNC_TX_FENCE)
289 dma_flags |= DMA_PREP_FENCE;
290 for (i = 0; i < disks; i++)
291 if (likely(blocks[i])) {
292 BUG_ON(is_raid6_zero_block(blocks[i]));
293 dma_src[i] = dma_map_page(dev, blocks[i],
299 tx = device->device_prep_dma_pq_val(chan, pq, dma_src,
306 async_tx_quiesce(&submit->depend_tx);
307 dma_async_issue_pending(chan);
309 async_tx_submit(chan, tx, submit);
313 struct page *p_src = P(blocks, disks);
314 struct page *q_src = Q(blocks, disks);
315 enum async_tx_flags flags_orig = submit->flags;
316 dma_async_tx_callback cb_fn_orig = submit->cb_fn;
317 void *scribble = submit->scribble;
318 void *cb_param_orig = submit->cb_param;
321 pr_debug("%s: (sync) disks: %d len: %zu\n",
322 __func__, disks, len);
324 /* caller must provide a temporary result buffer and
325 * allow the input parameters to be preserved
327 BUG_ON(!spare || !scribble);
329 /* wait for any prerequisite operations */
330 async_tx_quiesce(&submit->depend_tx);
332 /* recompute p and/or q into the temporary buffer and then
333 * check to see the result matches the current value
338 init_async_submit(submit, ASYNC_TX_XOR_ZERO_DST, NULL,
339 NULL, NULL, scribble);
340 tx = async_xor(spare, blocks, offset, disks-2, len, submit);
341 async_tx_quiesce(&tx);
342 p = page_address(p_src) + offset;
343 s = page_address(spare) + offset;
344 *pqres |= !!memcmp(p, s, len) << SUM_CHECK_P;
348 P(blocks, disks) = NULL;
349 Q(blocks, disks) = spare;
350 init_async_submit(submit, 0, NULL, NULL, NULL, scribble);
351 tx = async_gen_syndrome(blocks, offset, disks, len, submit);
352 async_tx_quiesce(&tx);
353 q = page_address(q_src) + offset;
354 s = page_address(spare) + offset;
355 *pqres |= !!memcmp(q, s, len) << SUM_CHECK_Q;
358 /* restore P, Q and submit */
359 P(blocks, disks) = p_src;
360 Q(blocks, disks) = q_src;
362 submit->cb_fn = cb_fn_orig;
363 submit->cb_param = cb_param_orig;
364 submit->flags = flags_orig;
365 async_tx_sync_epilog(submit);
370 EXPORT_SYMBOL_GPL(async_syndrome_val);
372 static int __init async_pq_init(void)
374 scribble = alloc_page(GFP_KERNEL);
379 pr_err("%s: failed to allocate required spare page\n", __func__);
384 static void __exit async_pq_exit(void)
389 module_init(async_pq_init);
390 module_exit(async_pq_exit);
392 MODULE_DESCRIPTION("asynchronous raid6 syndrome generation/validation");
393 MODULE_LICENSE("GPL");