async_xor.c 9.8 KB
Newer Older
D
Dan Williams 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * xor offload engine api
 *
 * Copyright © 2006, Intel Corporation.
 *
 *      Dan Williams <dan.j.williams@intel.com>
 *
 *      with architecture considerations by:
 *      Neil Brown <neilb@suse.de>
 *      Jeff Garzik <jeff@garzik.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#include <linux/kernel.h>
#include <linux/interrupt.h>
28
#include <linux/module.h>
D
Dan Williams 已提交
29 30 31 32 33
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/raid/xor.h>
#include <linux/async_tx.h>

34 35
/* do_async_xor - dma map the pages and perform the xor with an engine */
static __async_inline struct dma_async_tx_descriptor *
36
do_async_xor(struct dma_chan *chan, struct dmaengine_unmap_data *unmap,
37
	     struct async_submit_ctl *submit)
D
Dan Williams 已提交
38
{
39 40
	struct dma_device *dma = chan->device;
	struct dma_async_tx_descriptor *tx = NULL;
41 42 43
	dma_async_tx_callback cb_fn_orig = submit->cb_fn;
	void *cb_param_orig = submit->cb_param;
	enum async_tx_flags flags_orig = submit->flags;
44
	enum dma_ctrl_flags dma_flags;
45 46 47 48
	int src_cnt = unmap->to_cnt;
	int xor_src_cnt;
	dma_addr_t dma_dest = unmap->addr[unmap->to_cnt];
	dma_addr_t *src_list = unmap->addr;
49

50
	while (src_cnt) {
51 52
		dma_addr_t tmp;

53
		submit->flags = flags_orig;
54
		xor_src_cnt = min(src_cnt, (int)dma->max_xor);
55 56
		/* if we are submitting additional xors, leave the chain open
		 * and clear the callback parameters
57
		 */
58
		dma_flags = DMA_COMPL_SKIP_SRC_UNMAP | DMA_COMPL_SKIP_DEST_UNMAP;
59
		if (src_cnt > xor_src_cnt) {
60
			submit->flags &= ~ASYNC_TX_ACK;
D
Dan Williams 已提交
61
			submit->flags |= ASYNC_TX_FENCE;
62 63
			submit->cb_fn = NULL;
			submit->cb_param = NULL;
64
		} else {
65 66
			submit->cb_fn = cb_fn_orig;
			submit->cb_param = cb_param_orig;
67
		}
68
		if (submit->cb_fn)
69
			dma_flags |= DMA_PREP_INTERRUPT;
D
Dan Williams 已提交
70 71
		if (submit->flags & ASYNC_TX_FENCE)
			dma_flags |= DMA_PREP_FENCE;
72 73 74

		/* Drivers force forward progress in case they can not provide a
		 * descriptor
75
		 */
76 77 78 79 80 81 82 83
		tmp = src_list[0];
		if (src_list > unmap->addr)
			src_list[0] = dma_dest;
		tx = dma->device_prep_dma_xor(chan, dma_dest, src_list,
					      xor_src_cnt, unmap->len,
					      dma_flags);
		src_list[0] = tmp;

84

85
		if (unlikely(!tx))
86
			async_tx_quiesce(&submit->depend_tx);
87

L
Lucas De Marchi 已提交
88
		/* spin wait for the preceding transactions to complete */
89 90
		while (unlikely(!tx)) {
			dma_async_issue_pending(chan);
91
			tx = dma->device_prep_dma_xor(chan, dma_dest,
92 93
						      src_list,
						      xor_src_cnt, unmap->len,
94
						      dma_flags);
95
		}
D
Dan Williams 已提交
96

97
		dma_set_unmap(tx, unmap);
98 99
		async_tx_submit(chan, tx, submit);
		submit->depend_tx = tx;
100 101 102 103 104 105

		if (src_cnt > xor_src_cnt) {
			/* drop completed sources */
			src_cnt -= xor_src_cnt;
			/* use the intermediate result a source */
			src_cnt++;
106
			src_list += xor_src_cnt - 1;
107 108 109
		} else
			break;
	}
110 111

	return tx;
D
Dan Williams 已提交
112 113 114 115
}

static void
do_sync_xor(struct page *dest, struct page **src_list, unsigned int offset,
116
	    int src_cnt, size_t len, struct async_submit_ctl *submit)
D
Dan Williams 已提交
117 118
{
	int i;
119
	int xor_src_cnt = 0;
120 121
	int src_off = 0;
	void *dest_buf;
122
	void **srcs;
D
Dan Williams 已提交
123

124 125 126 127 128 129
	if (submit->scribble)
		srcs = submit->scribble;
	else
		srcs = (void **) src_list;

	/* convert to buffer pointers */
D
Dan Williams 已提交
130
	for (i = 0; i < src_cnt; i++)
131 132 133
		if (src_list[i])
			srcs[xor_src_cnt++] = page_address(src_list[i]) + offset;
	src_cnt = xor_src_cnt;
D
Dan Williams 已提交
134
	/* set destination address */
135
	dest_buf = page_address(dest) + offset;
D
Dan Williams 已提交
136

137
	if (submit->flags & ASYNC_TX_XOR_ZERO_DST)
138
		memset(dest_buf, 0, len);
D
Dan Williams 已提交
139

140 141 142 143 144 145 146 147 148
	while (src_cnt > 0) {
		/* process up to 'MAX_XOR_BLOCKS' sources */
		xor_src_cnt = min(src_cnt, MAX_XOR_BLOCKS);
		xor_blocks(xor_src_cnt, len, dest_buf, &srcs[src_off]);

		/* drop completed sources */
		src_cnt -= xor_src_cnt;
		src_off += xor_src_cnt;
	}
D
Dan Williams 已提交
149

150
	async_tx_sync_epilog(submit);
D
Dan Williams 已提交
151 152 153 154 155
}

/**
 * async_xor - attempt to xor a set of blocks with a dma engine.
 * @dest: destination page
156 157
 * @src_list: array of source pages
 * @offset: common src/dst offset to start transaction
D
Dan Williams 已提交
158 159
 * @src_cnt: number of source pages
 * @len: length in bytes
160 161 162 163 164 165 166 167 168 169 170 171 172
 * @submit: submission / completion modifiers
 *
 * honored flags: ASYNC_TX_ACK, ASYNC_TX_XOR_ZERO_DST, ASYNC_TX_XOR_DROP_DST
 *
 * xor_blocks always uses the dest as a source so the
 * ASYNC_TX_XOR_ZERO_DST flag must be set to not include dest data in
 * the calculation.  The assumption with dma eninges is that they only
 * use the destination buffer as a source when it is explicity specified
 * in the source list.
 *
 * src_list note: if the dest is also a source it must be at index zero.
 * The contents of this array will be overwritten if a scribble region
 * is not specified.
D
Dan Williams 已提交
173 174 175
 */
struct dma_async_tx_descriptor *
async_xor(struct page *dest, struct page **src_list, unsigned int offset,
176
	  int src_cnt, size_t len, struct async_submit_ctl *submit)
D
Dan Williams 已提交
177
{
178
	struct dma_chan *chan = async_tx_find_channel(submit, DMA_XOR,
179 180
						      &dest, 1, src_list,
						      src_cnt, len);
181 182
	struct dma_device *device = chan ? chan->device : NULL;
	struct dmaengine_unmap_data *unmap = NULL;
183

D
Dan Williams 已提交
184 185
	BUG_ON(src_cnt <= 1);

186 187 188 189 190 191
	if (device)
		unmap = dmaengine_get_unmap_data(device->dev, src_cnt+1, GFP_NOIO);

	if (unmap && is_dma_xor_aligned(device, offset, 0, len)) {
		struct dma_async_tx_descriptor *tx;
		int i, j;
192

193 194
		/* run the xor asynchronously */
		pr_debug("%s (async): len: %zu\n", __func__, len);
D
Dan Williams 已提交
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		unmap->len = len;
		for (i = 0, j = 0; i < src_cnt; i++) {
			if (!src_list[i])
				continue;
			unmap->to_cnt++;
			unmap->addr[j++] = dma_map_page(device->dev, src_list[i],
							offset, len, DMA_TO_DEVICE);
		}

		/* map it bidirectional as it may be re-used as a source */
		unmap->addr[j] = dma_map_page(device->dev, dest, offset, len,
					      DMA_BIDIRECTIONAL);
		unmap->bidi_cnt = 1;

		tx = do_async_xor(chan, unmap, submit);
		dmaengine_unmap_put(unmap);
		return tx;
213
	} else {
214
		dmaengine_unmap_put(unmap);
215 216
		/* run the xor synchronously */
		pr_debug("%s (sync): len: %zu\n", __func__, len);
217 218
		WARN_ONCE(chan, "%s: no space for dma address conversion\n",
			  __func__);
D
Dan Williams 已提交
219

220 221
		/* in the sync case the dest is an implied source
		 * (assumes the dest is the first source)
D
Dan Williams 已提交
222
		 */
223
		if (submit->flags & ASYNC_TX_XOR_DROP_DST) {
224 225 226
			src_cnt--;
			src_list++;
		}
D
Dan Williams 已提交
227

228
		/* wait for any prerequisite operations */
229
		async_tx_quiesce(&submit->depend_tx);
D
Dan Williams 已提交
230

231
		do_sync_xor(dest, src_list, offset, src_cnt, len, submit);
D
Dan Williams 已提交
232

233
		return NULL;
D
Dan Williams 已提交
234 235 236 237 238 239
	}
}
EXPORT_SYMBOL_GPL(async_xor);

static int page_is_zero(struct page *p, unsigned int offset, size_t len)
{
A
Akinobu Mita 已提交
240
	return !memchr_inv(page_address(p) + offset, 0, len);
D
Dan Williams 已提交
241 242
}

243 244 245 246 247 248 249 250 251 252 253
static inline struct dma_chan *
xor_val_chan(struct async_submit_ctl *submit, struct page *dest,
		 struct page **src_list, int src_cnt, size_t len)
{
	#ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
	return NULL;
	#endif
	return async_tx_find_channel(submit, DMA_XOR_VAL, &dest, 1, src_list,
				     src_cnt, len);
}

D
Dan Williams 已提交
254
/**
D
Dan Williams 已提交
255
 * async_xor_val - attempt a xor parity check with a dma engine.
D
Dan Williams 已提交
256
 * @dest: destination page used if the xor is performed synchronously
257
 * @src_list: array of source pages
D
Dan Williams 已提交
258 259 260 261
 * @offset: offset in pages to start transaction
 * @src_cnt: number of source pages
 * @len: length in bytes
 * @result: 0 if sum == 0 else non-zero
262 263 264 265 266 267 268
 * @submit: submission / completion modifiers
 *
 * honored flags: ASYNC_TX_ACK
 *
 * src_list note: if the dest is also a source it must be at index zero.
 * The contents of this array will be overwritten if a scribble region
 * is not specified.
D
Dan Williams 已提交
269 270
 */
struct dma_async_tx_descriptor *
271
async_xor_val(struct page *dest, struct page **src_list, unsigned int offset,
D
Dan Williams 已提交
272
	      int src_cnt, size_t len, enum sum_check_flags *result,
273
	      struct async_submit_ctl *submit)
D
Dan Williams 已提交
274
{
275
	struct dma_chan *chan = xor_val_chan(submit, dest, src_list, src_cnt, len);
D
Dan Williams 已提交
276
	struct dma_device *device = chan ? chan->device : NULL;
277
	struct dma_async_tx_descriptor *tx = NULL;
278
	dma_addr_t *dma_src = NULL;
D
Dan Williams 已提交
279 280 281

	BUG_ON(src_cnt <= 1);

282 283 284 285 286
	if (submit->scribble)
		dma_src = submit->scribble;
	else if (sizeof(dma_addr_t) <= sizeof(struct page *))
		dma_src = (dma_addr_t *) src_list;

287 288
	if (dma_src && device && src_cnt <= device->max_xor &&
	    is_dma_xor_aligned(device, offset, 0, len)) {
D
Dan Williams 已提交
289
		unsigned long dma_prep_flags = 0;
290
		int i;
D
Dan Williams 已提交
291

292
		pr_debug("%s: (async) len: %zu\n", __func__, len);
D
Dan Williams 已提交
293

D
Dan Williams 已提交
294 295 296 297
		if (submit->cb_fn)
			dma_prep_flags |= DMA_PREP_INTERRUPT;
		if (submit->flags & ASYNC_TX_FENCE)
			dma_prep_flags |= DMA_PREP_FENCE;
298 299 300 301
		for (i = 0; i < src_cnt; i++)
			dma_src[i] = dma_map_page(device->dev, src_list[i],
						  offset, len, DMA_TO_DEVICE);

D
Dan Williams 已提交
302 303 304
		tx = device->device_prep_dma_xor_val(chan, dma_src, src_cnt,
						     len, result,
						     dma_prep_flags);
305
		if (unlikely(!tx)) {
306
			async_tx_quiesce(&submit->depend_tx);
307

308
			while (!tx) {
309
				dma_async_issue_pending(chan);
D
Dan Williams 已提交
310
				tx = device->device_prep_dma_xor_val(chan,
311
					dma_src, src_cnt, len, result,
312
					dma_prep_flags);
313
			}
D
Dan Williams 已提交
314 315
		}

316
		async_tx_submit(chan, tx, submit);
D
Dan Williams 已提交
317
	} else {
318
		enum async_tx_flags flags_orig = submit->flags;
D
Dan Williams 已提交
319

320
		pr_debug("%s: (sync) len: %zu\n", __func__, len);
321 322 323
		WARN_ONCE(device && src_cnt <= device->max_xor,
			  "%s: no space for dma address conversion\n",
			  __func__);
D
Dan Williams 已提交
324

325 326
		submit->flags |= ASYNC_TX_XOR_DROP_DST;
		submit->flags &= ~ASYNC_TX_ACK;
D
Dan Williams 已提交
327

328
		tx = async_xor(dest, src_list, offset, src_cnt, len, submit);
D
Dan Williams 已提交
329

D
Dan Williams 已提交
330
		async_tx_quiesce(&tx);
D
Dan Williams 已提交
331

D
Dan Williams 已提交
332
		*result = !page_is_zero(dest, offset, len) << SUM_CHECK_P;
D
Dan Williams 已提交
333

334 335
		async_tx_sync_epilog(submit);
		submit->flags = flags_orig;
D
Dan Williams 已提交
336 337 338 339
	}

	return tx;
}
D
Dan Williams 已提交
340
EXPORT_SYMBOL_GPL(async_xor_val);
D
Dan Williams 已提交
341 342 343 344

MODULE_AUTHOR("Intel Corporation");
MODULE_DESCRIPTION("asynchronous xor/xor-zero-sum api");
MODULE_LICENSE("GPL");