Linux Audio

Check our new training course

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