Commit | Line | Data |
---|---|---|
9bc89cd8 DW |
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 | ||
1367a3d3 DW |
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 | */ | |
0036731c | 37 | static __always_inline struct dma_async_tx_descriptor * |
1e55db2d DW |
38 | do_async_xor(struct dma_chan *chan, struct page *dest, struct page **src_list, |
39 | unsigned int offset, int src_cnt, size_t len, | |
40 | enum async_tx_flags flags, | |
41 | struct dma_async_tx_descriptor *depend_tx, | |
42 | dma_async_tx_callback cb_fn, void *cb_param) | |
9bc89cd8 | 43 | { |
1e55db2d | 44 | struct dma_device *dma = chan->device; |
0036731c | 45 | dma_addr_t *dma_src = (dma_addr_t *) src_list; |
1e55db2d DW |
46 | struct dma_async_tx_descriptor *tx = NULL; |
47 | int src_off = 0; | |
9bc89cd8 | 48 | int i; |
1e55db2d DW |
49 | dma_async_tx_callback _cb_fn; |
50 | void *_cb_param; | |
51 | enum async_tx_flags async_flags; | |
52 | enum dma_ctrl_flags dma_flags; | |
53 | int xor_src_cnt; | |
54 | dma_addr_t dma_dest; | |
9bc89cd8 | 55 | |
1e55db2d | 56 | dma_dest = dma_map_page(dma->dev, dest, offset, len, DMA_FROM_DEVICE); |
0036731c | 57 | for (i = 0; i < src_cnt; i++) |
1e55db2d | 58 | dma_src[i] = dma_map_page(dma->dev, src_list[i], offset, |
0036731c DW |
59 | len, DMA_TO_DEVICE); |
60 | ||
1e55db2d DW |
61 | while (src_cnt) { |
62 | async_flags = flags; | |
63 | dma_flags = 0; | |
64 | xor_src_cnt = min(src_cnt, dma->max_xor); | |
65 | /* if we are submitting additional xors, leave the chain open, | |
66 | * clear the callback parameters, and leave the destination | |
67 | * buffer mapped | |
68 | */ | |
69 | if (src_cnt > xor_src_cnt) { | |
70 | async_flags &= ~ASYNC_TX_ACK; | |
71 | dma_flags = DMA_COMPL_SKIP_DEST_UNMAP; | |
72 | _cb_fn = NULL; | |
73 | _cb_param = NULL; | |
74 | } else { | |
75 | _cb_fn = cb_fn; | |
76 | _cb_param = cb_param; | |
77 | } | |
78 | if (_cb_fn) | |
79 | dma_flags |= DMA_PREP_INTERRUPT; | |
80 | ||
81 | /* Since we have clobbered the src_list we are committed | |
82 | * to doing this asynchronously. Drivers force forward progress | |
83 | * in case they can not provide a descriptor | |
84 | */ | |
85 | tx = dma->device_prep_dma_xor(chan, dma_dest, &dma_src[src_off], | |
86 | xor_src_cnt, len, dma_flags); | |
87 | ||
669ab0b2 DW |
88 | if (unlikely(!tx)) |
89 | async_tx_quiesce(&depend_tx); | |
0036731c | 90 | |
1e55db2d | 91 | /* spin wait for the preceeding transactions to complete */ |
669ab0b2 DW |
92 | while (unlikely(!tx)) { |
93 | dma_async_issue_pending(chan); | |
1e55db2d DW |
94 | tx = dma->device_prep_dma_xor(chan, dma_dest, |
95 | &dma_src[src_off], | |
96 | xor_src_cnt, len, | |
97 | dma_flags); | |
669ab0b2 | 98 | } |
9bc89cd8 | 99 | |
1e55db2d DW |
100 | async_tx_submit(chan, tx, async_flags, depend_tx, _cb_fn, |
101 | _cb_param); | |
102 | ||
103 | depend_tx = tx; | |
104 | flags |= ASYNC_TX_DEP_ACK; | |
105 | ||
106 | if (src_cnt > xor_src_cnt) { | |
107 | /* drop completed sources */ | |
108 | src_cnt -= xor_src_cnt; | |
109 | src_off += xor_src_cnt; | |
110 | ||
111 | /* use the intermediate result a source */ | |
112 | dma_src[--src_off] = dma_dest; | |
113 | src_cnt++; | |
114 | } else | |
115 | break; | |
116 | } | |
0036731c DW |
117 | |
118 | return tx; | |
9bc89cd8 DW |
119 | } |
120 | ||
121 | static void | |
122 | do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset, | |
1e55db2d DW |
123 | int src_cnt, size_t len, enum async_tx_flags flags, |
124 | struct dma_async_tx_descriptor *depend_tx, | |
125 | dma_async_tx_callback cb_fn, void *cb_param) | |
9bc89cd8 | 126 | { |
9bc89cd8 | 127 | int i; |
1e55db2d DW |
128 | int xor_src_cnt; |
129 | int src_off = 0; | |
130 | void *dest_buf; | |
131 | void **srcs = (void **) src_list; | |
9bc89cd8 DW |
132 | |
133 | /* reuse the 'src_list' array to convert to buffer pointers */ | |
134 | for (i = 0; i < src_cnt; i++) | |
1e55db2d | 135 | srcs[i] = page_address(src_list[i]) + offset; |
9bc89cd8 DW |
136 | |
137 | /* set destination address */ | |
1e55db2d | 138 | dest_buf = page_address(dest) + offset; |
9bc89cd8 DW |
139 | |
140 | if (flags & ASYNC_TX_XOR_ZERO_DST) | |
1e55db2d | 141 | memset(dest_buf, 0, len); |
9bc89cd8 | 142 | |
1e55db2d DW |
143 | while (src_cnt > 0) { |
144 | /* process up to 'MAX_XOR_BLOCKS' sources */ | |
145 | xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS); | |
146 | xor_blocks(xor_src_cnt, len, dest_buf, &srcs[src_off]); | |
147 | ||
148 | /* drop completed sources */ | |
149 | src_cnt -= xor_src_cnt; | |
150 | src_off += xor_src_cnt; | |
151 | } | |
9bc89cd8 DW |
152 | |
153 | async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param); | |
154 | } | |
155 | ||
156 | /** | |
157 | * async_xor - attempt to xor a set of blocks with a dma engine. | |
158 | * xor_blocks always uses the dest as a source so the ASYNC_TX_XOR_ZERO_DST | |
159 | * flag must be set to not include dest data in the calculation. The | |
160 | * assumption with dma eninges is that they only use the destination | |
161 | * buffer as a source when it is explicity specified in the source list. | |
162 | * @dest: destination page | |
163 | * @src_list: array of source pages (if the dest is also a source it must be | |
164 | * at index zero). The contents of this array may be overwritten. | |
165 | * @offset: offset in pages to start transaction | |
166 | * @src_cnt: number of source pages | |
167 | * @len: length in bytes | |
168 | * @flags: ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DEST, | |
d909b347 | 169 | * ASYNC_TX_ACK, ASYNC_TX_DEP_ACK |
9bc89cd8 DW |
170 | * @depend_tx: xor depends on the result of this transaction. |
171 | * @cb_fn: function to call when the xor completes | |
172 | * @cb_param: parameter to pass to the callback routine | |
173 | */ | |
174 | struct dma_async_tx_descriptor * | |
175 | async_xor(struct page *dest, struct page **src_list, unsigned int offset, | |
176 | int src_cnt, size_t len, enum async_tx_flags flags, | |
177 | struct dma_async_tx_descriptor *depend_tx, | |
178 | dma_async_tx_callback cb_fn, void *cb_param) | |
179 | { | |
47437b2c DW |
180 | struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_XOR, |
181 | &dest, 1, src_list, | |
182 | src_cnt, len); | |
9bc89cd8 DW |
183 | BUG_ON(src_cnt <= 1); |
184 | ||
1e55db2d DW |
185 | if (chan) { |
186 | /* run the xor asynchronously */ | |
187 | pr_debug("%s (async): len: %zu\n", __func__, len); | |
9bc89cd8 | 188 | |
1e55db2d DW |
189 | return do_async_xor(chan, dest, src_list, offset, src_cnt, len, |
190 | flags, depend_tx, cb_fn, cb_param); | |
191 | } else { | |
192 | /* run the xor synchronously */ | |
193 | pr_debug("%s (sync): len: %zu\n", __func__, len); | |
9bc89cd8 | 194 | |
1e55db2d DW |
195 | /* in the sync case the dest is an implied source |
196 | * (assumes the dest is the first source) | |
9bc89cd8 | 197 | */ |
1e55db2d DW |
198 | if (flags & ASYNC_TX_XOR_DROP_DST) { |
199 | src_cnt--; | |
200 | src_list++; | |
201 | } | |
9bc89cd8 | 202 | |
1e55db2d | 203 | /* wait for any prerequisite operations */ |
d2c52b79 | 204 | async_tx_quiesce(&depend_tx); |
9bc89cd8 | 205 | |
1e55db2d DW |
206 | do_sync_xor(dest, src_list, offset, src_cnt, len, |
207 | flags, depend_tx, cb_fn, cb_param); | |
9bc89cd8 | 208 | |
1e55db2d | 209 | return NULL; |
9bc89cd8 | 210 | } |
9bc89cd8 DW |
211 | } |
212 | EXPORT_SYMBOL_GPL(async_xor); | |
213 | ||
214 | static int page_is_zero(struct page *p, unsigned int offset, size_t len) | |
215 | { | |
216 | char *a = page_address(p) + offset; | |
217 | return ((*(u32 *) a) == 0 && | |
218 | memcmp(a, a + 4, len - 4) == 0); | |
219 | } | |
220 | ||
221 | /** | |
222 | * async_xor_zero_sum - attempt a xor parity check with a dma engine. | |
223 | * @dest: destination page used if the xor is performed synchronously | |
224 | * @src_list: array of source pages. The dest page must be listed as a source | |
225 | * at index zero. The contents of this array may be overwritten. | |
226 | * @offset: offset in pages to start transaction | |
227 | * @src_cnt: number of source pages | |
228 | * @len: length in bytes | |
229 | * @result: 0 if sum == 0 else non-zero | |
d909b347 | 230 | * @flags: ASYNC_TX_ACK, ASYNC_TX_DEP_ACK |
9bc89cd8 DW |
231 | * @depend_tx: xor depends on the result of this transaction. |
232 | * @cb_fn: function to call when the xor completes | |
233 | * @cb_param: parameter to pass to the callback routine | |
234 | */ | |
235 | struct dma_async_tx_descriptor * | |
236 | async_xor_zero_sum(struct page *dest, struct page **src_list, | |
237 | unsigned int offset, int src_cnt, size_t len, | |
238 | u32 *result, enum async_tx_flags flags, | |
239 | struct dma_async_tx_descriptor *depend_tx, | |
240 | dma_async_tx_callback cb_fn, void *cb_param) | |
241 | { | |
47437b2c DW |
242 | struct dma_chan *chan = async_tx_find_channel(depend_tx, DMA_ZERO_SUM, |
243 | &dest, 1, src_list, | |
244 | src_cnt, len); | |
9bc89cd8 | 245 | struct dma_device *device = chan ? chan->device : NULL; |
0036731c | 246 | struct dma_async_tx_descriptor *tx = NULL; |
9bc89cd8 DW |
247 | |
248 | BUG_ON(src_cnt <= 1); | |
249 | ||
8d8002f6 | 250 | if (device && src_cnt <= device->max_xor) { |
0036731c | 251 | dma_addr_t *dma_src = (dma_addr_t *) src_list; |
d4c56f97 | 252 | unsigned long dma_prep_flags = cb_fn ? DMA_PREP_INTERRUPT : 0; |
0036731c | 253 | int i; |
9bc89cd8 | 254 | |
3280ab3e | 255 | pr_debug("%s: (async) len: %zu\n", __func__, len); |
9bc89cd8 | 256 | |
0036731c DW |
257 | for (i = 0; i < src_cnt; i++) |
258 | dma_src[i] = dma_map_page(device->dev, src_list[i], | |
259 | offset, len, DMA_TO_DEVICE); | |
260 | ||
261 | tx = device->device_prep_dma_zero_sum(chan, dma_src, src_cnt, | |
262 | len, result, | |
d4c56f97 | 263 | dma_prep_flags); |
669ab0b2 DW |
264 | if (unlikely(!tx)) { |
265 | async_tx_quiesce(&depend_tx); | |
0036731c DW |
266 | |
267 | while (!tx) | |
669ab0b2 | 268 | dma_async_issue_pending(chan); |
0036731c DW |
269 | tx = device->device_prep_dma_zero_sum(chan, |
270 | dma_src, src_cnt, len, result, | |
d4c56f97 | 271 | dma_prep_flags); |
9bc89cd8 DW |
272 | } |
273 | ||
274 | async_tx_submit(chan, tx, flags, depend_tx, cb_fn, cb_param); | |
275 | } else { | |
276 | unsigned long xor_flags = flags; | |
277 | ||
3280ab3e | 278 | pr_debug("%s: (sync) len: %zu\n", __func__, len); |
9bc89cd8 DW |
279 | |
280 | xor_flags |= ASYNC_TX_XOR_DROP_DST; | |
281 | xor_flags &= ~ASYNC_TX_ACK; | |
282 | ||
283 | tx = async_xor(dest, src_list, offset, src_cnt, len, xor_flags, | |
284 | depend_tx, NULL, NULL); | |
285 | ||
d2c52b79 | 286 | async_tx_quiesce(&tx); |
9bc89cd8 DW |
287 | |
288 | *result = page_is_zero(dest, offset, len) ? 0 : 1; | |
289 | ||
9bc89cd8 DW |
290 | async_tx_sync_epilog(flags, depend_tx, cb_fn, cb_param); |
291 | } | |
292 | ||
293 | return tx; | |
294 | } | |
295 | EXPORT_SYMBOL_GPL(async_xor_zero_sum); | |
296 | ||
297 | static int __init async_xor_init(void) | |
298 | { | |
0036731c DW |
299 | #ifdef CONFIG_DMA_ENGINE |
300 | /* To conserve stack space the input src_list (array of page pointers) | |
301 | * is reused to hold the array of dma addresses passed to the driver. | |
302 | * This conversion is only possible when dma_addr_t is less than the | |
303 | * the size of a pointer. HIGHMEM64G is known to violate this | |
304 | * assumption. | |
305 | */ | |
306 | BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(struct page *)); | |
307 | #endif | |
308 | ||
9bc89cd8 DW |
309 | return 0; |
310 | } | |
311 | ||
312 | static void __exit async_xor_exit(void) | |
313 | { | |
314 | do { } while (0); | |
315 | } | |
316 | ||
317 | module_init(async_xor_init); | |
318 | module_exit(async_xor_exit); | |
319 | ||
320 | MODULE_AUTHOR("Intel Corporation"); | |
321 | MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api"); | |
322 | MODULE_LICENSE("GPL"); |