Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Intel I/OAT DMA Linux driver
  4 * Copyright(c) 2004 - 2015 Intel Corporation.
 
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6#include <linux/module.h>
  7#include <linux/pci.h>
  8#include <linux/gfp.h>
  9#include <linux/dmaengine.h>
 10#include <linux/dma-mapping.h>
 11#include <linux/prefetch.h>
 12#include "../dmaengine.h"
 13#include "registers.h"
 14#include "hw.h"
 15#include "dma.h"
 16
 17#define MAX_SCF	256
 18
 19/* provide a lookup table for setting the source address in the base or
 20 * extended descriptor of an xor or pq descriptor
 21 */
 22static const u8 xor_idx_to_desc = 0xe0;
 23static const u8 xor_idx_to_field[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
 24static const u8 pq_idx_to_desc = 0xf8;
 25static const u8 pq16_idx_to_desc[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
 26				       2, 2, 2, 2, 2, 2, 2 };
 27static const u8 pq_idx_to_field[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
 28static const u8 pq16_idx_to_field[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
 29					0, 1, 2, 3, 4, 5, 6 };
 30
 31static void xor_set_src(struct ioat_raw_descriptor *descs[2],
 32			dma_addr_t addr, u32 offset, int idx)
 33{
 34	struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
 35
 36	raw->field[xor_idx_to_field[idx]] = addr + offset;
 37}
 38
 39static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
 40{
 41	struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
 42
 43	return raw->field[pq_idx_to_field[idx]];
 44}
 45
 46static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
 47{
 48	struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
 49
 50	return raw->field[pq16_idx_to_field[idx]];
 51}
 52
 53static void pq_set_src(struct ioat_raw_descriptor *descs[2],
 54		       dma_addr_t addr, u32 offset, u8 coef, int idx)
 55{
 56	struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
 57	struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
 58
 59	raw->field[pq_idx_to_field[idx]] = addr + offset;
 60	pq->coef[idx] = coef;
 61}
 62
 63static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
 64			dma_addr_t addr, u32 offset, u8 coef, unsigned idx)
 65{
 66	struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
 67	struct ioat_pq16a_descriptor *pq16 =
 68		(struct ioat_pq16a_descriptor *)desc[1];
 69	struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
 70
 71	raw->field[pq16_idx_to_field[idx]] = addr + offset;
 72
 73	if (idx < 8)
 74		pq->coef[idx] = coef;
 75	else
 76		pq16->coef[idx - 8] = coef;
 77}
 78
 79static struct ioat_sed_ent *
 80ioat3_alloc_sed(struct ioatdma_device *ioat_dma, unsigned int hw_pool)
 81{
 82	struct ioat_sed_ent *sed;
 83	gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
 84
 85	sed = kmem_cache_alloc(ioat_sed_cache, flags);
 86	if (!sed)
 87		return NULL;
 88
 89	sed->hw_pool = hw_pool;
 90	sed->hw = dma_pool_alloc(ioat_dma->sed_hw_pool[hw_pool],
 91				 flags, &sed->dma);
 92	if (!sed->hw) {
 93		kmem_cache_free(ioat_sed_cache, sed);
 94		return NULL;
 95	}
 96
 97	return sed;
 98}
 99
100struct dma_async_tx_descriptor *
101ioat_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
102			   dma_addr_t dma_src, size_t len, unsigned long flags)
103{
104	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
105	struct ioat_dma_descriptor *hw;
106	struct ioat_ring_ent *desc;
107	dma_addr_t dst = dma_dest;
108	dma_addr_t src = dma_src;
109	size_t total_len = len;
110	int num_descs, idx, i;
111
112	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
113		return NULL;
114
115	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
116	if (likely(num_descs) &&
117	    ioat_check_space_lock(ioat_chan, num_descs) == 0)
118		idx = ioat_chan->head;
119	else
120		return NULL;
121	i = 0;
122	do {
123		size_t copy = min_t(size_t, len, 1 << ioat_chan->xfercap_log);
124
125		desc = ioat_get_ring_ent(ioat_chan, idx + i);
126		hw = desc->hw;
127
128		hw->size = copy;
129		hw->ctl = 0;
130		hw->src_addr = src;
131		hw->dst_addr = dst;
132
133		len -= copy;
134		dst += copy;
135		src += copy;
136		dump_desc_dbg(ioat_chan, desc);
137	} while (++i < num_descs);
138
139	desc->txd.flags = flags;
140	desc->len = total_len;
141	hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
142	hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
143	hw->ctl_f.compl_write = 1;
144	dump_desc_dbg(ioat_chan, desc);
145	/* we leave the channel locked to ensure in order submission */
146
147	return &desc->txd;
148}
149
150
151static struct dma_async_tx_descriptor *
152__ioat_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
153		      dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
154		      size_t len, unsigned long flags)
155{
156	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
157	struct ioat_ring_ent *compl_desc;
158	struct ioat_ring_ent *desc;
159	struct ioat_ring_ent *ext;
160	size_t total_len = len;
161	struct ioat_xor_descriptor *xor;
162	struct ioat_xor_ext_descriptor *xor_ex = NULL;
163	struct ioat_dma_descriptor *hw;
164	int num_descs, with_ext, idx, i;
165	u32 offset = 0;
166	u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
167
168	BUG_ON(src_cnt < 2);
169
170	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
171	/* we need 2x the number of descriptors to cover greater than 5
172	 * sources
173	 */
174	if (src_cnt > 5) {
175		with_ext = 1;
176		num_descs *= 2;
177	} else
178		with_ext = 0;
179
180	/* completion writes from the raid engine may pass completion
181	 * writes from the legacy engine, so we need one extra null
182	 * (legacy) descriptor to ensure all completion writes arrive in
183	 * order.
184	 */
185	if (likely(num_descs) &&
186	    ioat_check_space_lock(ioat_chan, num_descs+1) == 0)
187		idx = ioat_chan->head;
188	else
189		return NULL;
190	i = 0;
191	do {
192		struct ioat_raw_descriptor *descs[2];
193		size_t xfer_size = min_t(size_t,
194					 len, 1 << ioat_chan->xfercap_log);
195		int s;
196
197		desc = ioat_get_ring_ent(ioat_chan, idx + i);
198		xor = desc->xor;
199
200		/* save a branch by unconditionally retrieving the
201		 * extended descriptor xor_set_src() knows to not write
202		 * to it in the single descriptor case
203		 */
204		ext = ioat_get_ring_ent(ioat_chan, idx + i + 1);
205		xor_ex = ext->xor_ex;
206
207		descs[0] = (struct ioat_raw_descriptor *) xor;
208		descs[1] = (struct ioat_raw_descriptor *) xor_ex;
209		for (s = 0; s < src_cnt; s++)
210			xor_set_src(descs, src[s], offset, s);
211		xor->size = xfer_size;
212		xor->dst_addr = dest + offset;
213		xor->ctl = 0;
214		xor->ctl_f.op = op;
215		xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
216
217		len -= xfer_size;
218		offset += xfer_size;
219		dump_desc_dbg(ioat_chan, desc);
220	} while ((i += 1 + with_ext) < num_descs);
221
222	/* last xor descriptor carries the unmap parameters and fence bit */
223	desc->txd.flags = flags;
224	desc->len = total_len;
225	if (result)
226		desc->result = result;
227	xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
228
229	/* completion descriptor carries interrupt bit */
230	compl_desc = ioat_get_ring_ent(ioat_chan, idx + i);
231	compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
232	hw = compl_desc->hw;
233	hw->ctl = 0;
234	hw->ctl_f.null = 1;
235	hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
236	hw->ctl_f.compl_write = 1;
237	hw->size = NULL_DESC_BUFFER_SIZE;
238	dump_desc_dbg(ioat_chan, compl_desc);
239
240	/* we leave the channel locked to ensure in order submission */
241	return &compl_desc->txd;
242}
243
244struct dma_async_tx_descriptor *
245ioat_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
246	       unsigned int src_cnt, size_t len, unsigned long flags)
247{
248	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
249
250	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
251		return NULL;
252
253	return __ioat_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
254}
255
256struct dma_async_tx_descriptor *
257ioat_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
258		    unsigned int src_cnt, size_t len,
259		    enum sum_check_flags *result, unsigned long flags)
260{
261	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
262
263	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
264		return NULL;
265
266	/* the cleanup routine only sets bits on validate failure, it
267	 * does not clear bits on validate success... so clear it here
268	 */
269	*result = 0;
270
271	return __ioat_prep_xor_lock(chan, result, src[0], &src[1],
272				     src_cnt - 1, len, flags);
273}
274
275static void
276dump_pq_desc_dbg(struct ioatdma_chan *ioat_chan, struct ioat_ring_ent *desc,
277		 struct ioat_ring_ent *ext)
278{
279	struct device *dev = to_dev(ioat_chan);
280	struct ioat_pq_descriptor *pq = desc->pq;
281	struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
282	struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
283	int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
284	int i;
285
286	dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
287		" sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
288		" src_cnt: %d)\n",
289		desc_id(desc), (unsigned long long) desc->txd.phys,
290		(unsigned long long) (pq_ex ? pq_ex->next : pq->next),
291		desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op,
292		pq->ctl_f.int_en, pq->ctl_f.compl_write,
293		pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
294		pq->ctl_f.src_cnt);
295	for (i = 0; i < src_cnt; i++)
296		dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
297			(unsigned long long) pq_get_src(descs, i), pq->coef[i]);
298	dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
299	dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
300	dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
301}
302
303static void dump_pq16_desc_dbg(struct ioatdma_chan *ioat_chan,
304			       struct ioat_ring_ent *desc)
305{
306	struct device *dev = to_dev(ioat_chan);
307	struct ioat_pq_descriptor *pq = desc->pq;
308	struct ioat_raw_descriptor *descs[] = { (void *)pq,
309						(void *)pq,
310						(void *)pq };
311	int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
312	int i;
313
314	if (desc->sed) {
315		descs[1] = (void *)desc->sed->hw;
316		descs[2] = (void *)desc->sed->hw + 64;
317	}
318
319	dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
320		" sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
321		" src_cnt: %d)\n",
322		desc_id(desc), (unsigned long long) desc->txd.phys,
323		(unsigned long long) pq->next,
324		desc->txd.flags, pq->size, pq->ctl,
325		pq->ctl_f.op, pq->ctl_f.int_en,
326		pq->ctl_f.compl_write,
327		pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
328		pq->ctl_f.src_cnt);
329	for (i = 0; i < src_cnt; i++) {
330		dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
331			(unsigned long long) pq16_get_src(descs, i),
332			pq->coef[i]);
333	}
334	dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
335	dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
336}
337
338static struct dma_async_tx_descriptor *
339__ioat_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
340		     const dma_addr_t *dst, const dma_addr_t *src,
341		     unsigned int src_cnt, const unsigned char *scf,
342		     size_t len, unsigned long flags)
343{
344	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
345	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
346	struct ioat_ring_ent *compl_desc;
347	struct ioat_ring_ent *desc;
348	struct ioat_ring_ent *ext;
349	size_t total_len = len;
350	struct ioat_pq_descriptor *pq;
351	struct ioat_pq_ext_descriptor *pq_ex = NULL;
352	struct ioat_dma_descriptor *hw;
353	u32 offset = 0;
354	u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
355	int i, s, idx, with_ext, num_descs;
356	int cb32 = (ioat_dma->version < IOAT_VER_3_3) ? 1 : 0;
357
358	dev_dbg(to_dev(ioat_chan), "%s\n", __func__);
359	/* the engine requires at least two sources (we provide
360	 * at least 1 implied source in the DMA_PREP_CONTINUE case)
361	 */
362	BUG_ON(src_cnt + dmaf_continue(flags) < 2);
363
364	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
365	/* we need 2x the number of descriptors to cover greater than 3
366	 * sources (we need 1 extra source in the q-only continuation
367	 * case and 3 extra sources in the p+q continuation case.
368	 */
369	if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
370	    (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
371		with_ext = 1;
372		num_descs *= 2;
373	} else
374		with_ext = 0;
375
376	/* completion writes from the raid engine may pass completion
377	 * writes from the legacy engine, so we need one extra null
378	 * (legacy) descriptor to ensure all completion writes arrive in
379	 * order.
380	 */
381	if (likely(num_descs) &&
382	    ioat_check_space_lock(ioat_chan, num_descs + cb32) == 0)
383		idx = ioat_chan->head;
384	else
385		return NULL;
386	i = 0;
387	do {
388		struct ioat_raw_descriptor *descs[2];
389		size_t xfer_size = min_t(size_t, len,
390					 1 << ioat_chan->xfercap_log);
391
392		desc = ioat_get_ring_ent(ioat_chan, idx + i);
393		pq = desc->pq;
394
395		/* save a branch by unconditionally retrieving the
396		 * extended descriptor pq_set_src() knows to not write
397		 * to it in the single descriptor case
398		 */
399		ext = ioat_get_ring_ent(ioat_chan, idx + i + with_ext);
400		pq_ex = ext->pq_ex;
401
402		descs[0] = (struct ioat_raw_descriptor *) pq;
403		descs[1] = (struct ioat_raw_descriptor *) pq_ex;
404
405		for (s = 0; s < src_cnt; s++)
406			pq_set_src(descs, src[s], offset, scf[s], s);
407
408		/* see the comment for dma_maxpq in include/linux/dmaengine.h */
409		if (dmaf_p_disabled_continue(flags))
410			pq_set_src(descs, dst[1], offset, 1, s++);
411		else if (dmaf_continue(flags)) {
412			pq_set_src(descs, dst[0], offset, 0, s++);
413			pq_set_src(descs, dst[1], offset, 1, s++);
414			pq_set_src(descs, dst[1], offset, 0, s++);
415		}
416		pq->size = xfer_size;
417		pq->p_addr = dst[0] + offset;
418		pq->q_addr = dst[1] + offset;
419		pq->ctl = 0;
420		pq->ctl_f.op = op;
421		/* we turn on descriptor write back error status */
422		if (ioat_dma->cap & IOAT_CAP_DWBES)
423			pq->ctl_f.wb_en = result ? 1 : 0;
424		pq->ctl_f.src_cnt = src_cnt_to_hw(s);
425		pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
426		pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
427
428		len -= xfer_size;
429		offset += xfer_size;
430	} while ((i += 1 + with_ext) < num_descs);
431
432	/* last pq descriptor carries the unmap parameters and fence bit */
433	desc->txd.flags = flags;
434	desc->len = total_len;
435	if (result)
436		desc->result = result;
437	pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
438	dump_pq_desc_dbg(ioat_chan, desc, ext);
439
440	if (!cb32) {
441		pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
442		pq->ctl_f.compl_write = 1;
443		compl_desc = desc;
444	} else {
445		/* completion descriptor carries interrupt bit */
446		compl_desc = ioat_get_ring_ent(ioat_chan, idx + i);
447		compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
448		hw = compl_desc->hw;
449		hw->ctl = 0;
450		hw->ctl_f.null = 1;
451		hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
452		hw->ctl_f.compl_write = 1;
453		hw->size = NULL_DESC_BUFFER_SIZE;
454		dump_desc_dbg(ioat_chan, compl_desc);
455	}
456
457
458	/* we leave the channel locked to ensure in order submission */
459	return &compl_desc->txd;
460}
461
462static struct dma_async_tx_descriptor *
463__ioat_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
464		       const dma_addr_t *dst, const dma_addr_t *src,
465		       unsigned int src_cnt, const unsigned char *scf,
466		       size_t len, unsigned long flags)
467{
468	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
469	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
470	struct ioat_ring_ent *desc;
471	size_t total_len = len;
472	struct ioat_pq_descriptor *pq;
473	u32 offset = 0;
474	u8 op;
475	int i, s, idx, num_descs;
476
477	/* this function is only called with 9-16 sources */
478	op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
479
480	dev_dbg(to_dev(ioat_chan), "%s\n", __func__);
481
482	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
483
484	/*
485	 * 16 source pq is only available on cb3.3 and has no completion
486	 * write hw bug.
487	 */
488	if (num_descs && ioat_check_space_lock(ioat_chan, num_descs) == 0)
489		idx = ioat_chan->head;
490	else
491		return NULL;
492
493	i = 0;
494
495	do {
496		struct ioat_raw_descriptor *descs[4];
497		size_t xfer_size = min_t(size_t, len,
498					 1 << ioat_chan->xfercap_log);
499
500		desc = ioat_get_ring_ent(ioat_chan, idx + i);
501		pq = desc->pq;
502
503		descs[0] = (struct ioat_raw_descriptor *) pq;
504
505		desc->sed = ioat3_alloc_sed(ioat_dma, (src_cnt-2) >> 3);
506		if (!desc->sed) {
507			dev_err(to_dev(ioat_chan),
508				"%s: no free sed entries\n", __func__);
509			return NULL;
510		}
511
512		pq->sed_addr = desc->sed->dma;
513		desc->sed->parent = desc;
514
515		descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
516		descs[2] = (void *)descs[1] + 64;
517
518		for (s = 0; s < src_cnt; s++)
519			pq16_set_src(descs, src[s], offset, scf[s], s);
520
521		/* see the comment for dma_maxpq in include/linux/dmaengine.h */
522		if (dmaf_p_disabled_continue(flags))
523			pq16_set_src(descs, dst[1], offset, 1, s++);
524		else if (dmaf_continue(flags)) {
525			pq16_set_src(descs, dst[0], offset, 0, s++);
526			pq16_set_src(descs, dst[1], offset, 1, s++);
527			pq16_set_src(descs, dst[1], offset, 0, s++);
528		}
529
530		pq->size = xfer_size;
531		pq->p_addr = dst[0] + offset;
532		pq->q_addr = dst[1] + offset;
533		pq->ctl = 0;
534		pq->ctl_f.op = op;
535		pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
536		/* we turn on descriptor write back error status */
537		if (ioat_dma->cap & IOAT_CAP_DWBES)
538			pq->ctl_f.wb_en = result ? 1 : 0;
539		pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
540		pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
541
542		len -= xfer_size;
543		offset += xfer_size;
544	} while (++i < num_descs);
545
546	/* last pq descriptor carries the unmap parameters and fence bit */
547	desc->txd.flags = flags;
548	desc->len = total_len;
549	if (result)
550		desc->result = result;
551	pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
552
553	/* with cb3.3 we should be able to do completion w/o a null desc */
554	pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
555	pq->ctl_f.compl_write = 1;
556
557	dump_pq16_desc_dbg(ioat_chan, desc);
558
559	/* we leave the channel locked to ensure in order submission */
560	return &desc->txd;
561}
562
563static int src_cnt_flags(unsigned int src_cnt, unsigned long flags)
564{
565	if (dmaf_p_disabled_continue(flags))
566		return src_cnt + 1;
567	else if (dmaf_continue(flags))
568		return src_cnt + 3;
569	else
570		return src_cnt;
571}
572
573struct dma_async_tx_descriptor *
574ioat_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
575	      unsigned int src_cnt, const unsigned char *scf, size_t len,
576	      unsigned long flags)
577{
578	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
579
580	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
581		return NULL;
582
583	/* specify valid address for disabled result */
584	if (flags & DMA_PREP_PQ_DISABLE_P)
585		dst[0] = dst[1];
586	if (flags & DMA_PREP_PQ_DISABLE_Q)
587		dst[1] = dst[0];
588
589	/* handle the single source multiply case from the raid6
590	 * recovery path
591	 */
592	if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
593		dma_addr_t single_source[2];
594		unsigned char single_source_coef[2];
595
596		BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
597		single_source[0] = src[0];
598		single_source[1] = src[0];
599		single_source_coef[0] = scf[0];
600		single_source_coef[1] = 0;
601
602		return src_cnt_flags(src_cnt, flags) > 8 ?
603			__ioat_prep_pq16_lock(chan, NULL, dst, single_source,
604					       2, single_source_coef, len,
605					       flags) :
606			__ioat_prep_pq_lock(chan, NULL, dst, single_source, 2,
607					     single_source_coef, len, flags);
608
609	} else {
610		return src_cnt_flags(src_cnt, flags) > 8 ?
611			__ioat_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
612					       scf, len, flags) :
613			__ioat_prep_pq_lock(chan, NULL, dst, src, src_cnt,
614					     scf, len, flags);
615	}
616}
617
618struct dma_async_tx_descriptor *
619ioat_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
620		  unsigned int src_cnt, const unsigned char *scf, size_t len,
621		  enum sum_check_flags *pqres, unsigned long flags)
622{
623	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
624
625	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
626		return NULL;
627
628	/* specify valid address for disabled result */
629	if (flags & DMA_PREP_PQ_DISABLE_P)
630		pq[0] = pq[1];
631	if (flags & DMA_PREP_PQ_DISABLE_Q)
632		pq[1] = pq[0];
633
634	/* the cleanup routine only sets bits on validate failure, it
635	 * does not clear bits on validate success... so clear it here
636	 */
637	*pqres = 0;
638
639	return src_cnt_flags(src_cnt, flags) > 8 ?
640		__ioat_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
641				       flags) :
642		__ioat_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
643				     flags);
644}
645
646struct dma_async_tx_descriptor *
647ioat_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
648		 unsigned int src_cnt, size_t len, unsigned long flags)
649{
650	unsigned char scf[MAX_SCF];
651	dma_addr_t pq[2];
652	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
653
654	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
655		return NULL;
656
657	if (src_cnt > MAX_SCF)
658		return NULL;
659
660	memset(scf, 0, src_cnt);
661	pq[0] = dst;
662	flags |= DMA_PREP_PQ_DISABLE_Q;
663	pq[1] = dst; /* specify valid address for disabled result */
664
665	return src_cnt_flags(src_cnt, flags) > 8 ?
666		__ioat_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
667				       flags) :
668		__ioat_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
669				     flags);
670}
671
672struct dma_async_tx_descriptor *
673ioat_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
674		     unsigned int src_cnt, size_t len,
675		     enum sum_check_flags *result, unsigned long flags)
676{
677	unsigned char scf[MAX_SCF];
678	dma_addr_t pq[2];
679	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
680
681	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
682		return NULL;
683
684	if (src_cnt > MAX_SCF)
685		return NULL;
686
687	/* the cleanup routine only sets bits on validate failure, it
688	 * does not clear bits on validate success... so clear it here
689	 */
690	*result = 0;
691
692	memset(scf, 0, src_cnt);
693	pq[0] = src[0];
694	flags |= DMA_PREP_PQ_DISABLE_Q;
695	pq[1] = pq[0]; /* specify valid address for disabled result */
696
697	return src_cnt_flags(src_cnt, flags) > 8 ?
698		__ioat_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
699				       scf, len, flags) :
700		__ioat_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
701				     scf, len, flags);
702}
703
704struct dma_async_tx_descriptor *
705ioat_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
706{
707	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
708	struct ioat_ring_ent *desc;
709	struct ioat_dma_descriptor *hw;
710
711	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
712		return NULL;
713
714	if (ioat_check_space_lock(ioat_chan, 1) == 0)
715		desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head);
716	else
717		return NULL;
718
719	hw = desc->hw;
720	hw->ctl = 0;
721	hw->ctl_f.null = 1;
722	hw->ctl_f.int_en = 1;
723	hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
724	hw->ctl_f.compl_write = 1;
725	hw->size = NULL_DESC_BUFFER_SIZE;
726	hw->src_addr = 0;
727	hw->dst_addr = 0;
728
729	desc->txd.flags = flags;
730	desc->len = 1;
731
732	dump_desc_dbg(ioat_chan, desc);
733
734	/* we leave the channel locked to ensure in order submission */
735	return &desc->txd;
736}
737
v4.10.11
 
  1/*
  2 * Intel I/OAT DMA Linux driver
  3 * Copyright(c) 2004 - 2015 Intel Corporation.
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms and conditions of the GNU General Public License,
  7 * version 2, as published by the Free Software Foundation.
  8 *
  9 * This program is distributed in the hope that it will be useful, but WITHOUT
 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 12 * more details.
 13 *
 14 * The full GNU General Public License is included in this distribution in
 15 * the file called "COPYING".
 16 *
 17 */
 18#include <linux/module.h>
 19#include <linux/pci.h>
 20#include <linux/gfp.h>
 21#include <linux/dmaengine.h>
 22#include <linux/dma-mapping.h>
 23#include <linux/prefetch.h>
 24#include "../dmaengine.h"
 25#include "registers.h"
 26#include "hw.h"
 27#include "dma.h"
 28
 29#define MAX_SCF	256
 30
 31/* provide a lookup table for setting the source address in the base or
 32 * extended descriptor of an xor or pq descriptor
 33 */
 34static const u8 xor_idx_to_desc = 0xe0;
 35static const u8 xor_idx_to_field[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
 36static const u8 pq_idx_to_desc = 0xf8;
 37static const u8 pq16_idx_to_desc[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
 38				       2, 2, 2, 2, 2, 2, 2 };
 39static const u8 pq_idx_to_field[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
 40static const u8 pq16_idx_to_field[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
 41					0, 1, 2, 3, 4, 5, 6 };
 42
 43static void xor_set_src(struct ioat_raw_descriptor *descs[2],
 44			dma_addr_t addr, u32 offset, int idx)
 45{
 46	struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
 47
 48	raw->field[xor_idx_to_field[idx]] = addr + offset;
 49}
 50
 51static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
 52{
 53	struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
 54
 55	return raw->field[pq_idx_to_field[idx]];
 56}
 57
 58static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
 59{
 60	struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
 61
 62	return raw->field[pq16_idx_to_field[idx]];
 63}
 64
 65static void pq_set_src(struct ioat_raw_descriptor *descs[2],
 66		       dma_addr_t addr, u32 offset, u8 coef, int idx)
 67{
 68	struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
 69	struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
 70
 71	raw->field[pq_idx_to_field[idx]] = addr + offset;
 72	pq->coef[idx] = coef;
 73}
 74
 75static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
 76			dma_addr_t addr, u32 offset, u8 coef, unsigned idx)
 77{
 78	struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
 79	struct ioat_pq16a_descriptor *pq16 =
 80		(struct ioat_pq16a_descriptor *)desc[1];
 81	struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
 82
 83	raw->field[pq16_idx_to_field[idx]] = addr + offset;
 84
 85	if (idx < 8)
 86		pq->coef[idx] = coef;
 87	else
 88		pq16->coef[idx - 8] = coef;
 89}
 90
 91static struct ioat_sed_ent *
 92ioat3_alloc_sed(struct ioatdma_device *ioat_dma, unsigned int hw_pool)
 93{
 94	struct ioat_sed_ent *sed;
 95	gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
 96
 97	sed = kmem_cache_alloc(ioat_sed_cache, flags);
 98	if (!sed)
 99		return NULL;
100
101	sed->hw_pool = hw_pool;
102	sed->hw = dma_pool_alloc(ioat_dma->sed_hw_pool[hw_pool],
103				 flags, &sed->dma);
104	if (!sed->hw) {
105		kmem_cache_free(ioat_sed_cache, sed);
106		return NULL;
107	}
108
109	return sed;
110}
111
112struct dma_async_tx_descriptor *
113ioat_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
114			   dma_addr_t dma_src, size_t len, unsigned long flags)
115{
116	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
117	struct ioat_dma_descriptor *hw;
118	struct ioat_ring_ent *desc;
119	dma_addr_t dst = dma_dest;
120	dma_addr_t src = dma_src;
121	size_t total_len = len;
122	int num_descs, idx, i;
123
124	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
125		return NULL;
126
127	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
128	if (likely(num_descs) &&
129	    ioat_check_space_lock(ioat_chan, num_descs) == 0)
130		idx = ioat_chan->head;
131	else
132		return NULL;
133	i = 0;
134	do {
135		size_t copy = min_t(size_t, len, 1 << ioat_chan->xfercap_log);
136
137		desc = ioat_get_ring_ent(ioat_chan, idx + i);
138		hw = desc->hw;
139
140		hw->size = copy;
141		hw->ctl = 0;
142		hw->src_addr = src;
143		hw->dst_addr = dst;
144
145		len -= copy;
146		dst += copy;
147		src += copy;
148		dump_desc_dbg(ioat_chan, desc);
149	} while (++i < num_descs);
150
151	desc->txd.flags = flags;
152	desc->len = total_len;
153	hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
154	hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
155	hw->ctl_f.compl_write = 1;
156	dump_desc_dbg(ioat_chan, desc);
157	/* we leave the channel locked to ensure in order submission */
158
159	return &desc->txd;
160}
161
162
163static struct dma_async_tx_descriptor *
164__ioat_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
165		      dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
166		      size_t len, unsigned long flags)
167{
168	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
169	struct ioat_ring_ent *compl_desc;
170	struct ioat_ring_ent *desc;
171	struct ioat_ring_ent *ext;
172	size_t total_len = len;
173	struct ioat_xor_descriptor *xor;
174	struct ioat_xor_ext_descriptor *xor_ex = NULL;
175	struct ioat_dma_descriptor *hw;
176	int num_descs, with_ext, idx, i;
177	u32 offset = 0;
178	u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
179
180	BUG_ON(src_cnt < 2);
181
182	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
183	/* we need 2x the number of descriptors to cover greater than 5
184	 * sources
185	 */
186	if (src_cnt > 5) {
187		with_ext = 1;
188		num_descs *= 2;
189	} else
190		with_ext = 0;
191
192	/* completion writes from the raid engine may pass completion
193	 * writes from the legacy engine, so we need one extra null
194	 * (legacy) descriptor to ensure all completion writes arrive in
195	 * order.
196	 */
197	if (likely(num_descs) &&
198	    ioat_check_space_lock(ioat_chan, num_descs+1) == 0)
199		idx = ioat_chan->head;
200	else
201		return NULL;
202	i = 0;
203	do {
204		struct ioat_raw_descriptor *descs[2];
205		size_t xfer_size = min_t(size_t,
206					 len, 1 << ioat_chan->xfercap_log);
207		int s;
208
209		desc = ioat_get_ring_ent(ioat_chan, idx + i);
210		xor = desc->xor;
211
212		/* save a branch by unconditionally retrieving the
213		 * extended descriptor xor_set_src() knows to not write
214		 * to it in the single descriptor case
215		 */
216		ext = ioat_get_ring_ent(ioat_chan, idx + i + 1);
217		xor_ex = ext->xor_ex;
218
219		descs[0] = (struct ioat_raw_descriptor *) xor;
220		descs[1] = (struct ioat_raw_descriptor *) xor_ex;
221		for (s = 0; s < src_cnt; s++)
222			xor_set_src(descs, src[s], offset, s);
223		xor->size = xfer_size;
224		xor->dst_addr = dest + offset;
225		xor->ctl = 0;
226		xor->ctl_f.op = op;
227		xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
228
229		len -= xfer_size;
230		offset += xfer_size;
231		dump_desc_dbg(ioat_chan, desc);
232	} while ((i += 1 + with_ext) < num_descs);
233
234	/* last xor descriptor carries the unmap parameters and fence bit */
235	desc->txd.flags = flags;
236	desc->len = total_len;
237	if (result)
238		desc->result = result;
239	xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
240
241	/* completion descriptor carries interrupt bit */
242	compl_desc = ioat_get_ring_ent(ioat_chan, idx + i);
243	compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
244	hw = compl_desc->hw;
245	hw->ctl = 0;
246	hw->ctl_f.null = 1;
247	hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
248	hw->ctl_f.compl_write = 1;
249	hw->size = NULL_DESC_BUFFER_SIZE;
250	dump_desc_dbg(ioat_chan, compl_desc);
251
252	/* we leave the channel locked to ensure in order submission */
253	return &compl_desc->txd;
254}
255
256struct dma_async_tx_descriptor *
257ioat_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
258	       unsigned int src_cnt, size_t len, unsigned long flags)
259{
260	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
261
262	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
263		return NULL;
264
265	return __ioat_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
266}
267
268struct dma_async_tx_descriptor *
269ioat_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
270		    unsigned int src_cnt, size_t len,
271		    enum sum_check_flags *result, unsigned long flags)
272{
273	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
274
275	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
276		return NULL;
277
278	/* the cleanup routine only sets bits on validate failure, it
279	 * does not clear bits on validate success... so clear it here
280	 */
281	*result = 0;
282
283	return __ioat_prep_xor_lock(chan, result, src[0], &src[1],
284				     src_cnt - 1, len, flags);
285}
286
287static void
288dump_pq_desc_dbg(struct ioatdma_chan *ioat_chan, struct ioat_ring_ent *desc,
289		 struct ioat_ring_ent *ext)
290{
291	struct device *dev = to_dev(ioat_chan);
292	struct ioat_pq_descriptor *pq = desc->pq;
293	struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
294	struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
295	int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
296	int i;
297
298	dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
299		" sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
300		" src_cnt: %d)\n",
301		desc_id(desc), (unsigned long long) desc->txd.phys,
302		(unsigned long long) (pq_ex ? pq_ex->next : pq->next),
303		desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op,
304		pq->ctl_f.int_en, pq->ctl_f.compl_write,
305		pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
306		pq->ctl_f.src_cnt);
307	for (i = 0; i < src_cnt; i++)
308		dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
309			(unsigned long long) pq_get_src(descs, i), pq->coef[i]);
310	dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
311	dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
312	dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
313}
314
315static void dump_pq16_desc_dbg(struct ioatdma_chan *ioat_chan,
316			       struct ioat_ring_ent *desc)
317{
318	struct device *dev = to_dev(ioat_chan);
319	struct ioat_pq_descriptor *pq = desc->pq;
320	struct ioat_raw_descriptor *descs[] = { (void *)pq,
321						(void *)pq,
322						(void *)pq };
323	int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
324	int i;
325
326	if (desc->sed) {
327		descs[1] = (void *)desc->sed->hw;
328		descs[2] = (void *)desc->sed->hw + 64;
329	}
330
331	dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
332		" sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
333		" src_cnt: %d)\n",
334		desc_id(desc), (unsigned long long) desc->txd.phys,
335		(unsigned long long) pq->next,
336		desc->txd.flags, pq->size, pq->ctl,
337		pq->ctl_f.op, pq->ctl_f.int_en,
338		pq->ctl_f.compl_write,
339		pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
340		pq->ctl_f.src_cnt);
341	for (i = 0; i < src_cnt; i++) {
342		dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
343			(unsigned long long) pq16_get_src(descs, i),
344			pq->coef[i]);
345	}
346	dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
347	dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
348}
349
350static struct dma_async_tx_descriptor *
351__ioat_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
352		     const dma_addr_t *dst, const dma_addr_t *src,
353		     unsigned int src_cnt, const unsigned char *scf,
354		     size_t len, unsigned long flags)
355{
356	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
357	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
358	struct ioat_ring_ent *compl_desc;
359	struct ioat_ring_ent *desc;
360	struct ioat_ring_ent *ext;
361	size_t total_len = len;
362	struct ioat_pq_descriptor *pq;
363	struct ioat_pq_ext_descriptor *pq_ex = NULL;
364	struct ioat_dma_descriptor *hw;
365	u32 offset = 0;
366	u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
367	int i, s, idx, with_ext, num_descs;
368	int cb32 = (ioat_dma->version < IOAT_VER_3_3) ? 1 : 0;
369
370	dev_dbg(to_dev(ioat_chan), "%s\n", __func__);
371	/* the engine requires at least two sources (we provide
372	 * at least 1 implied source in the DMA_PREP_CONTINUE case)
373	 */
374	BUG_ON(src_cnt + dmaf_continue(flags) < 2);
375
376	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
377	/* we need 2x the number of descriptors to cover greater than 3
378	 * sources (we need 1 extra source in the q-only continuation
379	 * case and 3 extra sources in the p+q continuation case.
380	 */
381	if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
382	    (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
383		with_ext = 1;
384		num_descs *= 2;
385	} else
386		with_ext = 0;
387
388	/* completion writes from the raid engine may pass completion
389	 * writes from the legacy engine, so we need one extra null
390	 * (legacy) descriptor to ensure all completion writes arrive in
391	 * order.
392	 */
393	if (likely(num_descs) &&
394	    ioat_check_space_lock(ioat_chan, num_descs + cb32) == 0)
395		idx = ioat_chan->head;
396	else
397		return NULL;
398	i = 0;
399	do {
400		struct ioat_raw_descriptor *descs[2];
401		size_t xfer_size = min_t(size_t, len,
402					 1 << ioat_chan->xfercap_log);
403
404		desc = ioat_get_ring_ent(ioat_chan, idx + i);
405		pq = desc->pq;
406
407		/* save a branch by unconditionally retrieving the
408		 * extended descriptor pq_set_src() knows to not write
409		 * to it in the single descriptor case
410		 */
411		ext = ioat_get_ring_ent(ioat_chan, idx + i + with_ext);
412		pq_ex = ext->pq_ex;
413
414		descs[0] = (struct ioat_raw_descriptor *) pq;
415		descs[1] = (struct ioat_raw_descriptor *) pq_ex;
416
417		for (s = 0; s < src_cnt; s++)
418			pq_set_src(descs, src[s], offset, scf[s], s);
419
420		/* see the comment for dma_maxpq in include/linux/dmaengine.h */
421		if (dmaf_p_disabled_continue(flags))
422			pq_set_src(descs, dst[1], offset, 1, s++);
423		else if (dmaf_continue(flags)) {
424			pq_set_src(descs, dst[0], offset, 0, s++);
425			pq_set_src(descs, dst[1], offset, 1, s++);
426			pq_set_src(descs, dst[1], offset, 0, s++);
427		}
428		pq->size = xfer_size;
429		pq->p_addr = dst[0] + offset;
430		pq->q_addr = dst[1] + offset;
431		pq->ctl = 0;
432		pq->ctl_f.op = op;
433		/* we turn on descriptor write back error status */
434		if (ioat_dma->cap & IOAT_CAP_DWBES)
435			pq->ctl_f.wb_en = result ? 1 : 0;
436		pq->ctl_f.src_cnt = src_cnt_to_hw(s);
437		pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
438		pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
439
440		len -= xfer_size;
441		offset += xfer_size;
442	} while ((i += 1 + with_ext) < num_descs);
443
444	/* last pq descriptor carries the unmap parameters and fence bit */
445	desc->txd.flags = flags;
446	desc->len = total_len;
447	if (result)
448		desc->result = result;
449	pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
450	dump_pq_desc_dbg(ioat_chan, desc, ext);
451
452	if (!cb32) {
453		pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
454		pq->ctl_f.compl_write = 1;
455		compl_desc = desc;
456	} else {
457		/* completion descriptor carries interrupt bit */
458		compl_desc = ioat_get_ring_ent(ioat_chan, idx + i);
459		compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
460		hw = compl_desc->hw;
461		hw->ctl = 0;
462		hw->ctl_f.null = 1;
463		hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
464		hw->ctl_f.compl_write = 1;
465		hw->size = NULL_DESC_BUFFER_SIZE;
466		dump_desc_dbg(ioat_chan, compl_desc);
467	}
468
469
470	/* we leave the channel locked to ensure in order submission */
471	return &compl_desc->txd;
472}
473
474static struct dma_async_tx_descriptor *
475__ioat_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
476		       const dma_addr_t *dst, const dma_addr_t *src,
477		       unsigned int src_cnt, const unsigned char *scf,
478		       size_t len, unsigned long flags)
479{
480	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
481	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
482	struct ioat_ring_ent *desc;
483	size_t total_len = len;
484	struct ioat_pq_descriptor *pq;
485	u32 offset = 0;
486	u8 op;
487	int i, s, idx, num_descs;
488
489	/* this function is only called with 9-16 sources */
490	op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
491
492	dev_dbg(to_dev(ioat_chan), "%s\n", __func__);
493
494	num_descs = ioat_xferlen_to_descs(ioat_chan, len);
495
496	/*
497	 * 16 source pq is only available on cb3.3 and has no completion
498	 * write hw bug.
499	 */
500	if (num_descs && ioat_check_space_lock(ioat_chan, num_descs) == 0)
501		idx = ioat_chan->head;
502	else
503		return NULL;
504
505	i = 0;
506
507	do {
508		struct ioat_raw_descriptor *descs[4];
509		size_t xfer_size = min_t(size_t, len,
510					 1 << ioat_chan->xfercap_log);
511
512		desc = ioat_get_ring_ent(ioat_chan, idx + i);
513		pq = desc->pq;
514
515		descs[0] = (struct ioat_raw_descriptor *) pq;
516
517		desc->sed = ioat3_alloc_sed(ioat_dma, (src_cnt-2) >> 3);
518		if (!desc->sed) {
519			dev_err(to_dev(ioat_chan),
520				"%s: no free sed entries\n", __func__);
521			return NULL;
522		}
523
524		pq->sed_addr = desc->sed->dma;
525		desc->sed->parent = desc;
526
527		descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
528		descs[2] = (void *)descs[1] + 64;
529
530		for (s = 0; s < src_cnt; s++)
531			pq16_set_src(descs, src[s], offset, scf[s], s);
532
533		/* see the comment for dma_maxpq in include/linux/dmaengine.h */
534		if (dmaf_p_disabled_continue(flags))
535			pq16_set_src(descs, dst[1], offset, 1, s++);
536		else if (dmaf_continue(flags)) {
537			pq16_set_src(descs, dst[0], offset, 0, s++);
538			pq16_set_src(descs, dst[1], offset, 1, s++);
539			pq16_set_src(descs, dst[1], offset, 0, s++);
540		}
541
542		pq->size = xfer_size;
543		pq->p_addr = dst[0] + offset;
544		pq->q_addr = dst[1] + offset;
545		pq->ctl = 0;
546		pq->ctl_f.op = op;
547		pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
548		/* we turn on descriptor write back error status */
549		if (ioat_dma->cap & IOAT_CAP_DWBES)
550			pq->ctl_f.wb_en = result ? 1 : 0;
551		pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
552		pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
553
554		len -= xfer_size;
555		offset += xfer_size;
556	} while (++i < num_descs);
557
558	/* last pq descriptor carries the unmap parameters and fence bit */
559	desc->txd.flags = flags;
560	desc->len = total_len;
561	if (result)
562		desc->result = result;
563	pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
564
565	/* with cb3.3 we should be able to do completion w/o a null desc */
566	pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
567	pq->ctl_f.compl_write = 1;
568
569	dump_pq16_desc_dbg(ioat_chan, desc);
570
571	/* we leave the channel locked to ensure in order submission */
572	return &desc->txd;
573}
574
575static int src_cnt_flags(unsigned int src_cnt, unsigned long flags)
576{
577	if (dmaf_p_disabled_continue(flags))
578		return src_cnt + 1;
579	else if (dmaf_continue(flags))
580		return src_cnt + 3;
581	else
582		return src_cnt;
583}
584
585struct dma_async_tx_descriptor *
586ioat_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
587	      unsigned int src_cnt, const unsigned char *scf, size_t len,
588	      unsigned long flags)
589{
590	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
591
592	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
593		return NULL;
594
595	/* specify valid address for disabled result */
596	if (flags & DMA_PREP_PQ_DISABLE_P)
597		dst[0] = dst[1];
598	if (flags & DMA_PREP_PQ_DISABLE_Q)
599		dst[1] = dst[0];
600
601	/* handle the single source multiply case from the raid6
602	 * recovery path
603	 */
604	if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
605		dma_addr_t single_source[2];
606		unsigned char single_source_coef[2];
607
608		BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
609		single_source[0] = src[0];
610		single_source[1] = src[0];
611		single_source_coef[0] = scf[0];
612		single_source_coef[1] = 0;
613
614		return src_cnt_flags(src_cnt, flags) > 8 ?
615			__ioat_prep_pq16_lock(chan, NULL, dst, single_source,
616					       2, single_source_coef, len,
617					       flags) :
618			__ioat_prep_pq_lock(chan, NULL, dst, single_source, 2,
619					     single_source_coef, len, flags);
620
621	} else {
622		return src_cnt_flags(src_cnt, flags) > 8 ?
623			__ioat_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
624					       scf, len, flags) :
625			__ioat_prep_pq_lock(chan, NULL, dst, src, src_cnt,
626					     scf, len, flags);
627	}
628}
629
630struct dma_async_tx_descriptor *
631ioat_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
632		  unsigned int src_cnt, const unsigned char *scf, size_t len,
633		  enum sum_check_flags *pqres, unsigned long flags)
634{
635	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
636
637	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
638		return NULL;
639
640	/* specify valid address for disabled result */
641	if (flags & DMA_PREP_PQ_DISABLE_P)
642		pq[0] = pq[1];
643	if (flags & DMA_PREP_PQ_DISABLE_Q)
644		pq[1] = pq[0];
645
646	/* the cleanup routine only sets bits on validate failure, it
647	 * does not clear bits on validate success... so clear it here
648	 */
649	*pqres = 0;
650
651	return src_cnt_flags(src_cnt, flags) > 8 ?
652		__ioat_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
653				       flags) :
654		__ioat_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
655				     flags);
656}
657
658struct dma_async_tx_descriptor *
659ioat_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
660		 unsigned int src_cnt, size_t len, unsigned long flags)
661{
662	unsigned char scf[MAX_SCF];
663	dma_addr_t pq[2];
664	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
665
666	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
667		return NULL;
668
669	if (src_cnt > MAX_SCF)
670		return NULL;
671
672	memset(scf, 0, src_cnt);
673	pq[0] = dst;
674	flags |= DMA_PREP_PQ_DISABLE_Q;
675	pq[1] = dst; /* specify valid address for disabled result */
676
677	return src_cnt_flags(src_cnt, flags) > 8 ?
678		__ioat_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
679				       flags) :
680		__ioat_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
681				     flags);
682}
683
684struct dma_async_tx_descriptor *
685ioat_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
686		     unsigned int src_cnt, size_t len,
687		     enum sum_check_flags *result, unsigned long flags)
688{
689	unsigned char scf[MAX_SCF];
690	dma_addr_t pq[2];
691	struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
692
693	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
694		return NULL;
695
696	if (src_cnt > MAX_SCF)
697		return NULL;
698
699	/* the cleanup routine only sets bits on validate failure, it
700	 * does not clear bits on validate success... so clear it here
701	 */
702	*result = 0;
703
704	memset(scf, 0, src_cnt);
705	pq[0] = src[0];
706	flags |= DMA_PREP_PQ_DISABLE_Q;
707	pq[1] = pq[0]; /* specify valid address for disabled result */
708
709	return src_cnt_flags(src_cnt, flags) > 8 ?
710		__ioat_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
711				       scf, len, flags) :
712		__ioat_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
713				     scf, len, flags);
714}
715
716struct dma_async_tx_descriptor *
717ioat_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
718{
719	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
720	struct ioat_ring_ent *desc;
721	struct ioat_dma_descriptor *hw;
722
723	if (test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
724		return NULL;
725
726	if (ioat_check_space_lock(ioat_chan, 1) == 0)
727		desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head);
728	else
729		return NULL;
730
731	hw = desc->hw;
732	hw->ctl = 0;
733	hw->ctl_f.null = 1;
734	hw->ctl_f.int_en = 1;
735	hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
736	hw->ctl_f.compl_write = 1;
737	hw->size = NULL_DESC_BUFFER_SIZE;
738	hw->src_addr = 0;
739	hw->dst_addr = 0;
740
741	desc->txd.flags = flags;
742	desc->len = 1;
743
744	dump_desc_dbg(ioat_chan, desc);
745
746	/* we leave the channel locked to ensure in order submission */
747	return &desc->txd;
748}
749