Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 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 | |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 33 | /* the struct page *blocks[] parameter passed to async_gen_syndrome() |
| 34 | * and async_syndrome_val() contains the 'P' destination address at |
| 35 | * blocks[disks-2] and the 'Q' destination address at blocks[disks-1] |
| 36 | * |
| 37 | * note: these are macros as they are used as lvalues |
| 38 | */ |
| 39 | #define P(b, d) (b[d-2]) |
| 40 | #define Q(b, d) (b[d-1]) |
| 41 | |
| 42 | /** |
| 43 | * do_async_gen_syndrome - asynchronously calculate P and/or Q |
| 44 | */ |
| 45 | static __async_inline struct dma_async_tx_descriptor * |
| 46 | do_async_gen_syndrome(struct dma_chan *chan, struct page **blocks, |
| 47 | const unsigned char *scfs, unsigned int offset, int disks, |
| 48 | size_t len, dma_addr_t *dma_src, |
| 49 | struct async_submit_ctl *submit) |
| 50 | { |
| 51 | struct dma_async_tx_descriptor *tx = NULL; |
| 52 | struct dma_device *dma = chan->device; |
| 53 | enum dma_ctrl_flags dma_flags = 0; |
| 54 | enum async_tx_flags flags_orig = submit->flags; |
| 55 | dma_async_tx_callback cb_fn_orig = submit->cb_fn; |
| 56 | dma_async_tx_callback cb_param_orig = submit->cb_param; |
| 57 | int src_cnt = disks - 2; |
| 58 | unsigned char coefs[src_cnt]; |
| 59 | unsigned short pq_src_cnt; |
| 60 | dma_addr_t dma_dest[2]; |
| 61 | int src_off = 0; |
| 62 | int idx; |
| 63 | int i; |
| 64 | |
| 65 | /* DMAs use destinations as sources, so use BIDIRECTIONAL mapping */ |
| 66 | if (P(blocks, disks)) |
| 67 | dma_dest[0] = dma_map_page(dma->dev, P(blocks, disks), offset, |
| 68 | len, DMA_BIDIRECTIONAL); |
| 69 | else |
| 70 | dma_flags |= DMA_PREP_PQ_DISABLE_P; |
| 71 | if (Q(blocks, disks)) |
| 72 | dma_dest[1] = dma_map_page(dma->dev, Q(blocks, disks), offset, |
| 73 | len, DMA_BIDIRECTIONAL); |
| 74 | else |
| 75 | dma_flags |= DMA_PREP_PQ_DISABLE_Q; |
| 76 | |
| 77 | /* convert source addresses being careful to collapse 'empty' |
| 78 | * sources and update the coefficients accordingly |
| 79 | */ |
| 80 | for (i = 0, idx = 0; i < src_cnt; i++) { |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 81 | if (blocks[i] == NULL) |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 82 | continue; |
| 83 | dma_src[idx] = dma_map_page(dma->dev, blocks[i], offset, len, |
| 84 | DMA_TO_DEVICE); |
| 85 | coefs[idx] = scfs[i]; |
| 86 | idx++; |
| 87 | } |
| 88 | src_cnt = idx; |
| 89 | |
| 90 | while (src_cnt > 0) { |
| 91 | submit->flags = flags_orig; |
| 92 | pq_src_cnt = min(src_cnt, dma_maxpq(dma, dma_flags)); |
| 93 | /* if we are submitting additional pqs, leave the chain open, |
| 94 | * clear the callback parameters, and leave the destination |
| 95 | * buffers mapped |
| 96 | */ |
| 97 | if (src_cnt > pq_src_cnt) { |
| 98 | submit->flags &= ~ASYNC_TX_ACK; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 99 | submit->flags |= ASYNC_TX_FENCE; |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 100 | dma_flags |= DMA_COMPL_SKIP_DEST_UNMAP; |
| 101 | submit->cb_fn = NULL; |
| 102 | submit->cb_param = NULL; |
| 103 | } else { |
| 104 | dma_flags &= ~DMA_COMPL_SKIP_DEST_UNMAP; |
| 105 | submit->cb_fn = cb_fn_orig; |
| 106 | submit->cb_param = cb_param_orig; |
| 107 | if (cb_fn_orig) |
| 108 | dma_flags |= DMA_PREP_INTERRUPT; |
| 109 | } |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 110 | if (submit->flags & ASYNC_TX_FENCE) |
| 111 | dma_flags |= DMA_PREP_FENCE; |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /* Since we have clobbered the src_list we are committed |
| 114 | * to doing this asynchronously. Drivers force forward |
| 115 | * progress in case they can not provide a descriptor |
| 116 | */ |
| 117 | for (;;) { |
| 118 | tx = dma->device_prep_dma_pq(chan, dma_dest, |
| 119 | &dma_src[src_off], |
| 120 | pq_src_cnt, |
| 121 | &coefs[src_off], len, |
| 122 | dma_flags); |
| 123 | if (likely(tx)) |
| 124 | break; |
| 125 | async_tx_quiesce(&submit->depend_tx); |
| 126 | dma_async_issue_pending(chan); |
| 127 | } |
| 128 | |
| 129 | async_tx_submit(chan, tx, submit); |
| 130 | submit->depend_tx = tx; |
| 131 | |
| 132 | /* drop completed sources */ |
| 133 | src_cnt -= pq_src_cnt; |
| 134 | src_off += pq_src_cnt; |
| 135 | |
| 136 | dma_flags |= DMA_PREP_CONTINUE; |
| 137 | } |
| 138 | |
| 139 | return tx; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * do_sync_gen_syndrome - synchronously calculate a raid6 syndrome |
| 144 | */ |
| 145 | static void |
| 146 | do_sync_gen_syndrome(struct page **blocks, unsigned int offset, int disks, |
| 147 | size_t len, struct async_submit_ctl *submit) |
| 148 | { |
| 149 | void **srcs; |
| 150 | int i; |
| 151 | |
| 152 | if (submit->scribble) |
| 153 | srcs = submit->scribble; |
| 154 | else |
| 155 | srcs = (void **) blocks; |
| 156 | |
| 157 | for (i = 0; i < disks; i++) { |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 158 | if (blocks[i] == NULL) { |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 159 | BUG_ON(i > disks - 3); /* P or Q can't be zero */ |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 160 | srcs[i] = (void*)raid6_empty_zero_page; |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 161 | } else |
| 162 | srcs[i] = page_address(blocks[i]) + offset; |
| 163 | } |
| 164 | raid6_call.gen_syndrome(disks, len, srcs); |
| 165 | async_tx_sync_epilog(submit); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * async_gen_syndrome - asynchronously calculate a raid6 syndrome |
| 170 | * @blocks: source blocks from idx 0..disks-3, P @ disks-2 and Q @ disks-1 |
| 171 | * @offset: common offset into each block (src and dest) to start transaction |
| 172 | * @disks: number of blocks (including missing P or Q, see below) |
| 173 | * @len: length of operation in bytes |
| 174 | * @submit: submission/completion modifiers |
| 175 | * |
| 176 | * General note: This routine assumes a field of GF(2^8) with a |
| 177 | * primitive polynomial of 0x11d and a generator of {02}. |
| 178 | * |
| 179 | * 'disks' note: callers can optionally omit either P or Q (but not |
| 180 | * both) from the calculation by setting blocks[disks-2] or |
| 181 | * blocks[disks-1] to NULL. When P or Q is omitted 'len' must be <= |
| 182 | * PAGE_SIZE as a temporary buffer of this size is used in the |
| 183 | * synchronous path. 'disks' always accounts for both destination |
Dan Williams | 5676470 | 2009-10-19 18:09:32 -0700 | [diff] [blame^] | 184 | * buffers. If any source buffers (blocks[i] where i < disks - 2) are |
| 185 | * set to NULL those buffers will be replaced with the raid6_zero_page |
| 186 | * in the synchronous path and omitted in the hardware-asynchronous |
| 187 | * path. |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 188 | * |
| 189 | * 'blocks' note: if submit->scribble is NULL then the contents of |
Dan Williams | 5676470 | 2009-10-19 18:09:32 -0700 | [diff] [blame^] | 190 | * 'blocks' may be overwritten to perform address conversions |
| 191 | * (dma_map_page() or page_address()). |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 192 | */ |
| 193 | struct dma_async_tx_descriptor * |
| 194 | async_gen_syndrome(struct page **blocks, unsigned int offset, int disks, |
| 195 | size_t len, struct async_submit_ctl *submit) |
| 196 | { |
| 197 | int src_cnt = disks - 2; |
| 198 | struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ, |
| 199 | &P(blocks, disks), 2, |
| 200 | blocks, src_cnt, len); |
| 201 | struct dma_device *device = chan ? chan->device : NULL; |
| 202 | dma_addr_t *dma_src = NULL; |
| 203 | |
| 204 | BUG_ON(disks > 255 || !(P(blocks, disks) || Q(blocks, disks))); |
| 205 | |
| 206 | if (submit->scribble) |
| 207 | dma_src = submit->scribble; |
| 208 | else if (sizeof(dma_addr_t) <= sizeof(struct page *)) |
| 209 | dma_src = (dma_addr_t *) blocks; |
| 210 | |
| 211 | if (dma_src && device && |
| 212 | (src_cnt <= dma_maxpq(device, 0) || |
Dan Williams | 83544ae | 2009-09-08 17:42:53 -0700 | [diff] [blame] | 213 | dma_maxpq(device, DMA_PREP_CONTINUE) > 0) && |
| 214 | is_dma_pq_aligned(device, offset, 0, len)) { |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 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); |
| 220 | } |
| 221 | |
| 222 | /* run the pq synchronously */ |
| 223 | pr_debug("%s: (sync) disks: %d len: %zu\n", __func__, disks, len); |
| 224 | |
| 225 | /* wait for any prerequisite operations */ |
| 226 | async_tx_quiesce(&submit->depend_tx); |
| 227 | |
| 228 | if (!P(blocks, disks)) { |
| 229 | P(blocks, disks) = scribble; |
| 230 | BUG_ON(len + offset > PAGE_SIZE); |
| 231 | } |
| 232 | if (!Q(blocks, disks)) { |
| 233 | Q(blocks, disks) = scribble; |
| 234 | BUG_ON(len + offset > PAGE_SIZE); |
| 235 | } |
| 236 | do_sync_gen_syndrome(blocks, offset, disks, len, submit); |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | EXPORT_SYMBOL_GPL(async_gen_syndrome); |
| 241 | |
| 242 | /** |
| 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 |
| 251 | * |
| 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 |
| 255 | * specified. |
| 256 | */ |
| 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) |
| 261 | { |
| 262 | struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ_VAL, |
| 263 | NULL, 0, blocks, disks, |
| 264 | len); |
| 265 | struct dma_device *device = chan ? chan->device : NULL; |
| 266 | struct dma_async_tx_descriptor *tx; |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 267 | unsigned char coefs[disks-2]; |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 268 | enum dma_ctrl_flags dma_flags = submit->cb_fn ? DMA_PREP_INTERRUPT : 0; |
| 269 | dma_addr_t *dma_src = NULL; |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 270 | int src_cnt = 0; |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | BUG_ON(disks < 4); |
| 273 | |
| 274 | if (submit->scribble) |
| 275 | dma_src = submit->scribble; |
| 276 | else if (sizeof(dma_addr_t) <= sizeof(struct page *)) |
| 277 | dma_src = (dma_addr_t *) blocks; |
| 278 | |
Dan Williams | 83544ae | 2009-09-08 17:42:53 -0700 | [diff] [blame] | 279 | if (dma_src && device && disks <= dma_maxpq(device, 0) && |
| 280 | is_dma_pq_aligned(device, offset, 0, len)) { |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 281 | struct device *dev = device->dev; |
| 282 | dma_addr_t *pq = &dma_src[disks-2]; |
| 283 | int i; |
| 284 | |
| 285 | pr_debug("%s: (async) disks: %d len: %zu\n", |
| 286 | __func__, disks, len); |
| 287 | if (!P(blocks, disks)) |
| 288 | dma_flags |= DMA_PREP_PQ_DISABLE_P; |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 289 | else |
Dan Williams | 5676470 | 2009-10-19 18:09:32 -0700 | [diff] [blame^] | 290 | pq[0] = dma_map_page(dev, P(blocks, disks), |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 291 | offset, len, |
| 292 | DMA_TO_DEVICE); |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 293 | if (!Q(blocks, disks)) |
| 294 | dma_flags |= DMA_PREP_PQ_DISABLE_Q; |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 295 | else |
Dan Williams | 5676470 | 2009-10-19 18:09:32 -0700 | [diff] [blame^] | 296 | pq[1] = dma_map_page(dev, Q(blocks, disks), |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 297 | offset, len, |
| 298 | DMA_TO_DEVICE); |
| 299 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 300 | if (submit->flags & ASYNC_TX_FENCE) |
| 301 | dma_flags |= DMA_PREP_FENCE; |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 302 | for (i = 0; i < disks-2; i++) |
| 303 | if (likely(blocks[i])) { |
| 304 | dma_src[src_cnt] = dma_map_page(dev, blocks[i], |
| 305 | offset, len, |
| 306 | DMA_TO_DEVICE); |
| 307 | coefs[src_cnt] = raid6_gfexp[i]; |
| 308 | src_cnt++; |
| 309 | } |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | for (;;) { |
| 312 | tx = device->device_prep_dma_pq_val(chan, pq, dma_src, |
NeilBrown | b2141e6 | 2009-10-16 16:40:34 +1100 | [diff] [blame] | 313 | src_cnt, |
| 314 | coefs, |
Dan Williams | b2f46fd | 2009-07-14 12:20:36 -0700 | [diff] [blame] | 315 | len, pqres, |
| 316 | dma_flags); |
| 317 | if (likely(tx)) |
| 318 | break; |
| 319 | async_tx_quiesce(&submit->depend_tx); |
| 320 | dma_async_issue_pending(chan); |
| 321 | } |
| 322 | async_tx_submit(chan, tx, submit); |
| 323 | |
| 324 | return tx; |
| 325 | } else { |
| 326 | struct page *p_src = P(blocks, disks); |
| 327 | struct page *q_src = Q(blocks, disks); |
| 328 | enum async_tx_flags flags_orig = submit->flags; |
| 329 | dma_async_tx_callback cb_fn_orig = submit->cb_fn; |
| 330 | void *scribble = submit->scribble; |
| 331 | void *cb_param_orig = submit->cb_param; |
| 332 | void *p, *q, *s; |
| 333 | |
| 334 | pr_debug("%s: (sync) disks: %d len: %zu\n", |
| 335 | __func__, disks, len); |
| 336 | |
| 337 | /* caller must provide a temporary result buffer and |
| 338 | * allow the input parameters to be preserved |
| 339 | */ |
| 340 | BUG_ON(!spare || !scribble); |
| 341 | |
| 342 | /* wait for any prerequisite operations */ |
| 343 | async_tx_quiesce(&submit->depend_tx); |
| 344 | |
| 345 | /* recompute p and/or q into the temporary buffer and then |
| 346 | * check to see the result matches the current value |
| 347 | */ |
| 348 | tx = NULL; |
| 349 | *pqres = 0; |
| 350 | if (p_src) { |
| 351 | init_async_submit(submit, ASYNC_TX_XOR_ZERO_DST, NULL, |
| 352 | NULL, NULL, scribble); |
| 353 | tx = async_xor(spare, blocks, offset, disks-2, len, submit); |
| 354 | async_tx_quiesce(&tx); |
| 355 | p = page_address(p_src) + offset; |
| 356 | s = page_address(spare) + offset; |
| 357 | *pqres |= !!memcmp(p, s, len) << SUM_CHECK_P; |
| 358 | } |
| 359 | |
| 360 | if (q_src) { |
| 361 | P(blocks, disks) = NULL; |
| 362 | Q(blocks, disks) = spare; |
| 363 | init_async_submit(submit, 0, NULL, NULL, NULL, scribble); |
| 364 | tx = async_gen_syndrome(blocks, offset, disks, len, submit); |
| 365 | async_tx_quiesce(&tx); |
| 366 | q = page_address(q_src) + offset; |
| 367 | s = page_address(spare) + offset; |
| 368 | *pqres |= !!memcmp(q, s, len) << SUM_CHECK_Q; |
| 369 | } |
| 370 | |
| 371 | /* restore P, Q and submit */ |
| 372 | P(blocks, disks) = p_src; |
| 373 | Q(blocks, disks) = q_src; |
| 374 | |
| 375 | submit->cb_fn = cb_fn_orig; |
| 376 | submit->cb_param = cb_param_orig; |
| 377 | submit->flags = flags_orig; |
| 378 | async_tx_sync_epilog(submit); |
| 379 | |
| 380 | return NULL; |
| 381 | } |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(async_syndrome_val); |
| 384 | |
| 385 | static int __init async_pq_init(void) |
| 386 | { |
| 387 | scribble = alloc_page(GFP_KERNEL); |
| 388 | |
| 389 | if (scribble) |
| 390 | return 0; |
| 391 | |
| 392 | pr_err("%s: failed to allocate required spare page\n", __func__); |
| 393 | |
| 394 | return -ENOMEM; |
| 395 | } |
| 396 | |
| 397 | static void __exit async_pq_exit(void) |
| 398 | { |
| 399 | put_page(scribble); |
| 400 | } |
| 401 | |
| 402 | module_init(async_pq_init); |
| 403 | module_exit(async_pq_exit); |
| 404 | |
| 405 | MODULE_DESCRIPTION("asynchronous raid6 syndrome generation/validation"); |
| 406 | MODULE_LICENSE("GPL"); |