]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - crypto/async_tx/async_xor.c
cb41e6bbbc4dd506a72a9b76257ab5a6f1de6509
[linux-2.6-omap-h63xx.git] / crypto / async_tx / async_xor.c
1 /*
2  * xor offload engine api
3  *
4  * Copyright © 2006, Intel Corporation.
5  *
6  *      Dan Williams <dan.j.williams@intel.com>
7  *
8  *      with architecture considerations by:
9  *      Neil Brown <neilb@suse.de>
10  *      Jeff Garzik <jeff@garzik.org>
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms and conditions of the GNU General Public License,
14  * version 2, as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19  * more details.
20  *
21  * You should have received a copy of the GNU General Public License along with
22  * this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  */
26 #include <linux/kernel.h>
27 #include <linux/interrupt.h>
28 #include <linux/mm.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/raid/xor.h>
31 #include <linux/async_tx.h>
32
33 /* do_async_xor - dma map the pages and perform the xor with an engine.
34  *      This routine is marked __always_inline so it can be compiled away
35  *      when CONFIG_DMA_ENGINE=n
36  */
37 static __always_inline void
38 do_async_xor(struct dma_async_tx_descriptor *tx, struct dma_device *device,
39         struct dma_chan *chan, struct page *dest, struct page **src_list,
40         unsigned int offset, unsigned int src_cnt, size_t len,
41         enum async_tx_flags flags, struct dma_async_tx_descriptor *depend_tx,
42         dma_async_tx_callback cb_fn, void *cb_param)
43 {
44         dma_addr_t dma_addr;
45         int i;
46
47         pr_debug("%s: len: %zu\n", __FUNCTION__, len);
48
49         dma_addr = dma_map_page(device->dev, dest, offset, len,
50                                 DMA_FROM_DEVICE);
51         tx->tx_set_dest(dma_addr, tx, 0);
52
53         for (i = 0; i < src_cnt; i++) {
54                 dma_addr = dma_map_page(device->dev, src_list[i],
55                         offset, len, DMA_TO_DEVICE);
56                 tx->tx_set_src(dma_addr, tx, i);
57         }
58
59         async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
60 }
61
62 static void
63 do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset,
64         unsigned int src_cnt, size_t len, enum async_tx_flags flags,
65         struct dma_async_tx_descriptor *depend_tx,
66         dma_async_tx_callback cb_fn, void *cb_param)
67 {
68         void *_dest;
69         int i;
70
71         pr_debug("%s: len: %zu\n", __FUNCTION__, len);
72
73         /* reuse the 'src_list' array to convert to buffer pointers */
74         for (i = 0; i < src_cnt; i++)
75                 src_list[i] = (struct page *)
76                         (page_address(src_list[i]) + offset);
77
78         /* set destination address */
79         _dest = page_address(dest) + offset;
80
81         if (flags & ASYNC_TX_XOR_ZERO_DST)
82                 memset(_dest, 0, len);
83
84         xor_blocks(src_cnt, len, _dest,
85                 (void **) src_list);
86
87         async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
88 }
89
90 /**
91  * async_xor - attempt to xor a set of blocks with a dma engine.
92  *      xor_blocks always uses the dest as a source so the ASYNC_TX_XOR_ZERO_DST
93  *      flag must be set to not include dest data in the calculation.  The
94  *      assumption with dma eninges is that they only use the destination
95  *      buffer as a source when it is explicity specified in the source list.
96  * @dest: destination page
97  * @src_list: array of source pages (if the dest is also a source it must be
98  *      at index zero).  The contents of this array may be overwritten.
99  * @offset: offset in pages to start transaction
100  * @src_cnt: number of source pages
101  * @len: length in bytes
102  * @flags: ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DEST,
103  *      ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
104  * @depend_tx: xor depends on the result of this transaction.
105  * @cb_fn: function to call when the xor completes
106  * @cb_param: parameter to pass to the callback routine
107  */
108 struct dma_async_tx_descriptor *
109 async_xor(struct page *dest, struct page **src_list, unsigned int offset,
110         int src_cnt, size_t len, enum async_tx_flags flags,
111         struct dma_async_tx_descriptor *depend_tx,
112         dma_async_tx_callback cb_fn, void *cb_param)
113 {
114         struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_XOR);
115         struct dma_device *device = chan ? chan->device : NULL;
116         struct dma_async_tx_descriptor *tx = NULL;
117         dma_async_tx_callback _cb_fn;
118         void *_cb_param;
119         unsigned long local_flags;
120         int xor_src_cnt;
121         int i = 0, src_off = 0, int_en;
122
123         BUG_ON(src_cnt <= 1);
124
125         while (src_cnt) {
126                 local_flags = flags;
127                 if (device) { /* run the xor asynchronously */
128                         xor_src_cnt = min(src_cnt, device->max_xor);
129                         /* if we are submitting additional xors
130                          * only set the callback on the last transaction
131                          */
132                         if (src_cnt > xor_src_cnt) {
133                                 local_flags &= ~ASYNC_TX_ACK;
134                                 _cb_fn = NULL;
135                                 _cb_param = NULL;
136                         } else {
137                                 _cb_fn = cb_fn;
138                                 _cb_param = cb_param;
139                         }
140
141                         int_en = _cb_fn ? 1 : 0;
142
143                         tx = device->device_prep_dma_xor(
144                                 chan, xor_src_cnt, len, int_en);
145
146                         if (tx) {
147                                 do_async_xor(tx, device, chan, dest,
148                                 &src_list[src_off], offset, xor_src_cnt, len,
149                                 local_flags, depend_tx, _cb_fn,
150                                 _cb_param);
151                         } else /* fall through */
152                                 goto xor_sync;
153                 } else { /* run the xor synchronously */
154 xor_sync:
155                         /* in the sync case the dest is an implied source
156                          * (assumes the dest is at the src_off index)
157                          */
158                         if (flags & ASYNC_TX_XOR_DROP_DST) {
159                                 src_cnt--;
160                                 src_off++;
161                         }
162
163                         /* process up to 'MAX_XOR_BLOCKS' sources */
164                         xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
165
166                         /* if we are submitting additional xors
167                          * only set the callback on the last transaction
168                          */
169                         if (src_cnt > xor_src_cnt) {
170                                 local_flags &= ~ASYNC_TX_ACK;
171                                 _cb_fn = NULL;
172                                 _cb_param = NULL;
173                         } else {
174                                 _cb_fn = cb_fn;
175                                 _cb_param = cb_param;
176                         }
177
178                         /* wait for any prerequisite operations */
179                         if (depend_tx) {
180                                 /* if ack is already set then we cannot be sure
181                                  * we are referring to the correct operation
182                                  */
183                                 BUG_ON(depend_tx->ack);
184                                 if (dma_wait_for_async_tx(depend_tx) ==
185                                         DMA_ERROR)
186                                         panic("%s: DMA_ERROR waiting for "
187                                                 "depend_tx\n",
188                                                 __FUNCTION__);
189                         }
190
191                         do_sync_xor(dest, &src_list[src_off], offset,
192                                 xor_src_cnt, len, local_flags, depend_tx,
193                                 _cb_fn, _cb_param);
194                 }
195
196                 /* the previous tx is hidden from the client,
197                  * so ack it
198                  */
199                 if (i && depend_tx)
200                         async_tx_ack(depend_tx);
201
202                 depend_tx = tx;
203
204                 if (src_cnt > xor_src_cnt) {
205                         /* drop completed sources */
206                         src_cnt -= xor_src_cnt;
207                         src_off += xor_src_cnt;
208
209                         /* unconditionally preserve the destination */
210                         flags &= ~ASYNC_TX_XOR_ZERO_DST;
211
212                         /* use the intermediate result a source, but remember
213                          * it's dropped, because it's implied, in the sync case
214                          */
215                         src_list[--src_off] = dest;
216                         src_cnt++;
217                         flags |= ASYNC_TX_XOR_DROP_DST;
218                 } else
219                         src_cnt = 0;
220                 i++;
221         }
222
223         return tx;
224 }
225 EXPORT_SYMBOL_GPL(async_xor);
226
227 static int page_is_zero(struct page *p, unsigned int offset, size_t len)
228 {
229         char *a = page_address(p) + offset;
230         return ((*(u32 *) a) == 0 &&
231                 memcmp(a, a + 4, len - 4) == 0);
232 }
233
234 /**
235  * async_xor_zero_sum - attempt a xor parity check with a dma engine.
236  * @dest: destination page used if the xor is performed synchronously
237  * @src_list: array of source pages.  The dest page must be listed as a source
238  *      at index zero.  The contents of this array may be overwritten.
239  * @offset: offset in pages to start transaction
240  * @src_cnt: number of source pages
241  * @len: length in bytes
242  * @result: 0 if sum == 0 else non-zero
243  * @flags: ASYNC_TX_ACK, ASYNC_TX_DEP_ACK
244  * @depend_tx: xor depends on the result of this transaction.
245  * @cb_fn: function to call when the xor completes
246  * @cb_param: parameter to pass to the callback routine
247  */
248 struct dma_async_tx_descriptor *
249 async_xor_zero_sum(struct page *dest, struct page **src_list,
250         unsigned int offset, int src_cnt, size_t len,
251         u32 *result, enum async_tx_flags flags,
252         struct dma_async_tx_descriptor *depend_tx,
253         dma_async_tx_callback cb_fn, void *cb_param)
254 {
255         struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_ZERO_SUM);
256         struct dma_device *device = chan ? chan->device : NULL;
257         int int_en = cb_fn ? 1 : 0;
258         struct dma_async_tx_descriptor *tx = device ?
259                 device->device_prep_dma_zero_sum(chan, src_cnt, len, result,
260                         int_en) : NULL;
261         int i;
262
263         BUG_ON(src_cnt <= 1);
264
265         if (tx) {
266                 dma_addr_t dma_addr;
267
268                 pr_debug("%s: (async) len: %zu\n", __FUNCTION__, len);
269
270                 for (i = 0; i < src_cnt; i++) {
271                         dma_addr = dma_map_page(device->dev, src_list[i],
272                                 offset, len, DMA_TO_DEVICE);
273                         tx->tx_set_src(dma_addr, tx, i);
274                 }
275
276                 async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param);
277         } else {
278                 unsigned long xor_flags = flags;
279
280                 pr_debug("%s: (sync) len: %zu\n", __FUNCTION__, len);
281
282                 xor_flags |= ASYNC_TX_XOR_DROP_DST;
283                 xor_flags &= ~ASYNC_TX_ACK;
284
285                 tx = async_xor(dest, src_list, offset, src_cnt, len, xor_flags,
286                         depend_tx, NULL, NULL);
287
288                 if (tx) {
289                         if (dma_wait_for_async_tx(tx) == DMA_ERROR)
290                                 panic("%s: DMA_ERROR waiting for tx\n",
291                                         __FUNCTION__);
292                         async_tx_ack(tx);
293                 }
294
295                 *result = page_is_zero(dest, offset, len) ? 0 : 1;
296
297                 tx = NULL;
298
299                 async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param);
300         }
301
302         return tx;
303 }
304 EXPORT_SYMBOL_GPL(async_xor_zero_sum);
305
306 static int __init async_xor_init(void)
307 {
308         return 0;
309 }
310
311 static void __exit async_xor_exit(void)
312 {
313         do { } while (0);
314 }
315
316 module_init(async_xor_init);
317 module_exit(async_xor_exit);
318
319 MODULE_AUTHOR("Intel Corporation");
320 MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api");
321 MODULE_LICENSE("GPL");