Merge branch 'tracing-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / crypto / async_tx / async_pq.c
1 /*
2  * Copyright(c) 2007 Yuri Tikhonov <yur@emcraft.com>
3  * Copyright(c) 2009 Intel Corporation
4  *
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)
8  * any later version.
9  *
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
13  * more details.
14  *
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.
18  *
19  * The full GNU General Public License is included in this distribution in the
20  * file called COPYING.
21  */
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>
27
28 /**
29  * scribble - space to hold throwaway P buffer for synchronous gen_syndrome
30  */
31 static struct page *scribble;
32
33 static bool is_raid6_zero_block(struct page *p)
34 {
35         return p == (void *) raid6_empty_zero_page;
36 }
37
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]
41  *
42  * note: these are macros as they are used as lvalues
43  */
44 #define P(b, d) (b[d-2])
45 #define Q(b, d) (b[d-1])
46
47 /**
48  * do_async_gen_syndrome - asynchronously calculate P and/or Q
49  */
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)
55 {
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];
66         int src_off = 0;
67         int idx;
68         int i;
69
70         /* DMAs use destinations as sources, so use BIDIRECTIONAL mapping */
71         if (P(blocks, disks))
72                 dma_dest[0] = dma_map_page(dma->dev, P(blocks, disks), offset,
73                                            len, DMA_BIDIRECTIONAL);
74         else
75                 dma_flags |= DMA_PREP_PQ_DISABLE_P;
76         if (Q(blocks, disks))
77                 dma_dest[1] = dma_map_page(dma->dev, Q(blocks, disks), offset,
78                                            len, DMA_BIDIRECTIONAL);
79         else
80                 dma_flags |= DMA_PREP_PQ_DISABLE_Q;
81
82         /* convert source addresses being careful to collapse 'empty'
83          * sources and update the coefficients accordingly
84          */
85         for (i = 0, idx = 0; i < src_cnt; i++) {
86                 if (is_raid6_zero_block(blocks[i]))
87                         continue;
88                 dma_src[idx] = dma_map_page(dma->dev, blocks[i], offset, len,
89                                             DMA_TO_DEVICE);
90                 coefs[idx] = scfs[i];
91                 idx++;
92         }
93         src_cnt = idx;
94
95         while (src_cnt > 0) {
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
100                  * buffers mapped
101                  */
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;
108                 } else {
109                         dma_flags &= ~DMA_COMPL_SKIP_DEST_UNMAP;
110                         submit->cb_fn = cb_fn_orig;
111                         submit->cb_param = cb_param_orig;
112                         if (cb_fn_orig)
113                                 dma_flags |= DMA_PREP_INTERRUPT;
114                 }
115                 if (submit->flags & ASYNC_TX_FENCE)
116                         dma_flags |= DMA_PREP_FENCE;
117
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
121                  */
122                 for (;;) {
123                         tx = dma->device_prep_dma_pq(chan, dma_dest,
124                                                      &dma_src[src_off],
125                                                      pq_src_cnt,
126                                                      &coefs[src_off], len,
127                                                      dma_flags);
128                         if (likely(tx))
129                                 break;
130                         async_tx_quiesce(&submit->depend_tx);
131                         dma_async_issue_pending(chan);
132                 }
133
134                 async_tx_submit(chan, tx, submit);
135                 submit->depend_tx = tx;
136
137                 /* drop completed sources */
138                 src_cnt -= pq_src_cnt;
139                 src_off += pq_src_cnt;
140
141                 dma_flags |= DMA_PREP_CONTINUE;
142         }
143
144         return tx;
145 }
146
147 /**
148  * do_sync_gen_syndrome - synchronously calculate a raid6 syndrome
149  */
150 static void
151 do_sync_gen_syndrome(struct page **blocks, unsigned int offset, int disks,
152                      size_t len, struct async_submit_ctl *submit)
153 {
154         void **srcs;
155         int i;
156
157         if (submit->scribble)
158                 srcs = submit->scribble;
159         else
160                 srcs = (void **) blocks;
161
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 */
165                         srcs[i] = blocks[i];
166                 } else
167                         srcs[i] = page_address(blocks[i]) + offset;
168         }
169         raid6_call.gen_syndrome(disks, len, srcs);
170         async_tx_sync_epilog(submit);
171 }
172
173 /**
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
180  *
181  * General note: This routine assumes a field of GF(2^8) with a
182  * primitive polynomial of 0x11d and a generator of {02}.
183  *
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
189  * buffers.
190  *
191  * 'blocks' note: if submit->scribble is NULL then the contents of
192  * 'blocks' may be overridden
193  */
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)
197 {
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;
204
205         BUG_ON(disks > 255 || !(P(blocks, disks) || Q(blocks, disks)));
206
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;
211
212         if (dma_src && device &&
213             (src_cnt <= dma_maxpq(device, 0) ||
214              dma_maxpq(device, DMA_PREP_CONTINUE) > 0) &&
215             is_dma_pq_aligned(device, offset, 0, len)) {
216                 /* run the p+q asynchronously */
217                 pr_debug("%s: (async) disks: %d len: %zu\n",
218                          __func__, disks, len);
219                 return do_async_gen_syndrome(chan, blocks, raid6_gfexp, offset,
220                                              disks, len, dma_src, submit);
221         }
222
223         /* run the pq synchronously */
224         pr_debug("%s: (sync) disks: %d len: %zu\n", __func__, disks, len);
225
226         /* wait for any prerequisite operations */
227         async_tx_quiesce(&submit->depend_tx);
228
229         if (!P(blocks, disks)) {
230                 P(blocks, disks) = scribble;
231                 BUG_ON(len + offset > PAGE_SIZE);
232         }
233         if (!Q(blocks, disks)) {
234                 Q(blocks, disks) = scribble;
235                 BUG_ON(len + offset > PAGE_SIZE);
236         }
237         do_sync_gen_syndrome(blocks, offset, disks, len, submit);
238
239         return NULL;
240 }
241 EXPORT_SYMBOL_GPL(async_gen_syndrome);
242
243 /**
244  * async_syndrome_val - asynchronously validate a raid6 syndrome
245  * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1
246  * @offset: common offset into each block (src and dest) to start transaction
247  * @disks: number of blocks (including missing P or Q, see below)
248  * @len: length of operation in bytes
249  * @pqres: on val failure SUM_CHECK_P_RESULT and/or SUM_CHECK_Q_RESULT are set
250  * @spare: temporary result buffer for the synchronous case
251  * @submit: submission / completion modifiers
252  *
253  * The same notes from async_gen_syndrome apply to the 'blocks',
254  * and 'disks' parameters of this routine.  The synchronous path
255  * requires a temporary result buffer and submit->scribble to be
256  * specified.
257  */
258 struct dma_async_tx_descriptor *
259 async_syndrome_val(struct page **blocks, unsigned int offset, int disks,
260                    size_t len, enum sum_check_flags *pqres, struct page *spare,
261                    struct async_submit_ctl *submit)
262 {
263         struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ_VAL,
264                                                       NULL, 0,  blocks, disks,
265                                                       len);
266         struct dma_device *device = chan ? chan->device : NULL;
267         struct dma_async_tx_descriptor *tx;
268         enum dma_ctrl_flags dma_flags = submit->cb_fn ? DMA_PREP_INTERRUPT : 0;
269         dma_addr_t *dma_src = NULL;
270
271         BUG_ON(disks < 4);
272
273         if (submit->scribble)
274                 dma_src = submit->scribble;
275         else if (sizeof(dma_addr_t) <= sizeof(struct page *))
276                 dma_src = (dma_addr_t *) blocks;
277
278         if (dma_src && device && disks <= dma_maxpq(device, 0) &&
279             is_dma_pq_aligned(device, offset, 0, len)) {
280                 struct device *dev = device->dev;
281                 dma_addr_t *pq = &dma_src[disks-2];
282                 int i;
283
284                 pr_debug("%s: (async) disks: %d len: %zu\n",
285                          __func__, disks, len);
286                 if (!P(blocks, disks))
287                         dma_flags |= DMA_PREP_PQ_DISABLE_P;
288                 if (!Q(blocks, disks))
289                         dma_flags |= DMA_PREP_PQ_DISABLE_Q;
290                 if (submit->flags & ASYNC_TX_FENCE)
291                         dma_flags |= DMA_PREP_FENCE;
292                 for (i = 0; i < disks; i++)
293                         if (likely(blocks[i])) {
294                                 BUG_ON(is_raid6_zero_block(blocks[i]));
295                                 dma_src[i] = dma_map_page(dev, blocks[i],
296                                                           offset, len,
297                                                           DMA_TO_DEVICE);
298                         }
299
300                 for (;;) {
301                         tx = device->device_prep_dma_pq_val(chan, pq, dma_src,
302                                                             disks - 2,
303                                                             raid6_gfexp,
304                                                             len, pqres,
305                                                             dma_flags);
306                         if (likely(tx))
307                                 break;
308                         async_tx_quiesce(&submit->depend_tx);
309                         dma_async_issue_pending(chan);
310                 }
311                 async_tx_submit(chan, tx, submit);
312
313                 return tx;
314         } else {
315                 struct page *p_src = P(blocks, disks);
316                 struct page *q_src = Q(blocks, disks);
317                 enum async_tx_flags flags_orig = submit->flags;
318                 dma_async_tx_callback cb_fn_orig = submit->cb_fn;
319                 void *scribble = submit->scribble;
320                 void *cb_param_orig = submit->cb_param;
321                 void *p, *q, *s;
322
323                 pr_debug("%s: (sync) disks: %d len: %zu\n",
324                          __func__, disks, len);
325
326                 /* caller must provide a temporary result buffer and
327                  * allow the input parameters to be preserved
328                  */
329                 BUG_ON(!spare || !scribble);
330
331                 /* wait for any prerequisite operations */
332                 async_tx_quiesce(&submit->depend_tx);
333
334                 /* recompute p and/or q into the temporary buffer and then
335                  * check to see the result matches the current value
336                  */
337                 tx = NULL;
338                 *pqres = 0;
339                 if (p_src) {
340                         init_async_submit(submit, ASYNC_TX_XOR_ZERO_DST, NULL,
341                                           NULL, NULL, scribble);
342                         tx = async_xor(spare, blocks, offset, disks-2, len, submit);
343                         async_tx_quiesce(&tx);
344                         p = page_address(p_src) + offset;
345                         s = page_address(spare) + offset;
346                         *pqres |= !!memcmp(p, s, len) << SUM_CHECK_P;
347                 }
348
349                 if (q_src) {
350                         P(blocks, disks) = NULL;
351                         Q(blocks, disks) = spare;
352                         init_async_submit(submit, 0, NULL, NULL, NULL, scribble);
353                         tx = async_gen_syndrome(blocks, offset, disks, len, submit);
354                         async_tx_quiesce(&tx);
355                         q = page_address(q_src) + offset;
356                         s = page_address(spare) + offset;
357                         *pqres |= !!memcmp(q, s, len) << SUM_CHECK_Q;
358                 }
359
360                 /* restore P, Q and submit */
361                 P(blocks, disks) = p_src;
362                 Q(blocks, disks) = q_src;
363
364                 submit->cb_fn = cb_fn_orig;
365                 submit->cb_param = cb_param_orig;
366                 submit->flags = flags_orig;
367                 async_tx_sync_epilog(submit);
368
369                 return NULL;
370         }
371 }
372 EXPORT_SYMBOL_GPL(async_syndrome_val);
373
374 static int __init async_pq_init(void)
375 {
376         scribble = alloc_page(GFP_KERNEL);
377
378         if (scribble)
379                 return 0;
380
381         pr_err("%s: failed to allocate required spare page\n", __func__);
382
383         return -ENOMEM;
384 }
385
386 static void __exit async_pq_exit(void)
387 {
388         put_page(scribble);
389 }
390
391 module_init(async_pq_init);
392 module_exit(async_pq_exit);
393
394 MODULE_DESCRIPTION("asynchronous raid6 syndrome generation/validation");
395 MODULE_LICENSE("GPL");