2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
22 * The full GNU General Public License is included in this distribution in
23 * the file called "COPYING".
27 * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions are met:
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * * Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in
36 * the documentation and/or other materials provided with the
38 * * Neither the name of Intel Corporation nor the names of its
39 * contributors may be used to endorse or promote products derived
40 * from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
46 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 * POSSIBILITY OF SUCH DAMAGE.
56 * Support routines for v3+ hardware
58 #include <linux/module.h>
59 #include <linux/pci.h>
60 #include <linux/gfp.h>
61 #include <linux/dmaengine.h>
62 #include <linux/dma-mapping.h>
63 #include <linux/prefetch.h>
64 #include "../dmaengine.h"
65 #include "registers.h"
70 /* ioat hardware assumes at least two sources for raid operations */
71 #define src_cnt_to_sw(x) ((x) + 2)
72 #define src_cnt_to_hw(x) ((x) - 2)
73 #define ndest_to_sw(x) ((x) + 1)
74 #define ndest_to_hw(x) ((x) - 1)
75 #define src16_cnt_to_sw(x) ((x) + 9)
76 #define src16_cnt_to_hw(x) ((x) - 9)
78 /* provide a lookup table for setting the source address in the base or
79 * extended descriptor of an xor or pq descriptor
81 static const u8 xor_idx_to_desc = 0xe0;
82 static const u8 xor_idx_to_field[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
83 static const u8 pq_idx_to_desc = 0xf8;
84 static const u8 pq16_idx_to_desc[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
85 2, 2, 2, 2, 2, 2, 2 };
86 static const u8 pq_idx_to_field[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
87 static const u8 pq16_idx_to_field[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
88 0, 1, 2, 3, 4, 5, 6 };
91 * technically sources 1 and 2 do not require SED, but the op will have
92 * at least 9 descriptors so that's irrelevant.
94 static const u8 pq16_idx_to_sed[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 1, 1, 1, 1, 1, 1, 1 };
97 static void ioat3_eh(struct ioat2_dma_chan *ioat);
99 static dma_addr_t xor_get_src(struct ioat_raw_descriptor *descs[2], int idx)
101 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
103 return raw->field[xor_idx_to_field[idx]];
106 static void xor_set_src(struct ioat_raw_descriptor *descs[2],
107 dma_addr_t addr, u32 offset, int idx)
109 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
111 raw->field[xor_idx_to_field[idx]] = addr + offset;
114 static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
116 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
118 return raw->field[pq_idx_to_field[idx]];
121 static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
123 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
125 return raw->field[pq16_idx_to_field[idx]];
128 static void pq_set_src(struct ioat_raw_descriptor *descs[2],
129 dma_addr_t addr, u32 offset, u8 coef, int idx)
131 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
132 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
134 raw->field[pq_idx_to_field[idx]] = addr + offset;
135 pq->coef[idx] = coef;
138 static int sed_get_pq16_pool_idx(int src_cnt)
141 return pq16_idx_to_sed[src_cnt];
144 static bool is_jf_ioat(struct pci_dev *pdev)
146 switch (pdev->device) {
147 case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
148 case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
149 case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
150 case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
151 case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
152 case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
153 case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
154 case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
155 case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
156 case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
163 static bool is_snb_ioat(struct pci_dev *pdev)
165 switch (pdev->device) {
166 case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
167 case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
168 case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
169 case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
170 case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
171 case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
172 case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
173 case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
174 case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
175 case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
182 static bool is_ivb_ioat(struct pci_dev *pdev)
184 switch (pdev->device) {
185 case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
186 case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
187 case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
188 case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
189 case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
190 case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
191 case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
192 case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
193 case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
194 case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
202 static bool is_hsw_ioat(struct pci_dev *pdev)
204 switch (pdev->device) {
205 case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
206 case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
207 case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
208 case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
209 case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
210 case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
211 case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
212 case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
213 case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
214 case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
222 static bool is_xeon_cb32(struct pci_dev *pdev)
224 return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
228 static bool is_bwd_ioat(struct pci_dev *pdev)
230 switch (pdev->device) {
231 case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
232 case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
233 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
234 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
241 static bool is_bwd_noraid(struct pci_dev *pdev)
243 switch (pdev->device) {
244 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
245 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
253 static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
254 dma_addr_t addr, u32 offset, u8 coef, int idx)
256 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
257 struct ioat_pq16a_descriptor *pq16 =
258 (struct ioat_pq16a_descriptor *)desc[1];
259 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
261 raw->field[pq16_idx_to_field[idx]] = addr + offset;
264 pq->coef[idx] = coef;
266 pq16->coef[idx - 8] = coef;
269 static struct ioat_sed_ent *
270 ioat3_alloc_sed(struct ioatdma_device *device, unsigned int hw_pool)
272 struct ioat_sed_ent *sed;
273 gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
275 sed = kmem_cache_alloc(device->sed_pool, flags);
279 sed->hw_pool = hw_pool;
280 sed->hw = dma_pool_alloc(device->sed_hw_pool[hw_pool],
283 kmem_cache_free(device->sed_pool, sed);
290 static void ioat3_free_sed(struct ioatdma_device *device, struct ioat_sed_ent *sed)
295 dma_pool_free(device->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
296 kmem_cache_free(device->sed_pool, sed);
299 static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
300 struct ioat_ring_ent *desc, int idx)
302 struct ioat_chan_common *chan = &ioat->base;
303 struct pci_dev *pdev = chan->device->pdev;
304 size_t len = desc->len;
305 size_t offset = len - desc->hw->size;
306 struct dma_async_tx_descriptor *tx = &desc->txd;
307 enum dma_ctrl_flags flags = tx->flags;
309 switch (desc->hw->ctl_f.op) {
311 if (!desc->hw->ctl_f.null) /* skip 'interrupt' ops */
312 ioat_dma_unmap(chan, flags, len, desc->hw);
315 struct ioat_fill_descriptor *hw = desc->fill;
317 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
318 ioat_unmap(pdev, hw->dst_addr - offset, len,
319 PCI_DMA_FROMDEVICE, flags, 1);
322 case IOAT_OP_XOR_VAL:
324 struct ioat_xor_descriptor *xor = desc->xor;
325 struct ioat_ring_ent *ext;
326 struct ioat_xor_ext_descriptor *xor_ex = NULL;
327 int src_cnt = src_cnt_to_sw(xor->ctl_f.src_cnt);
328 struct ioat_raw_descriptor *descs[2];
332 ext = ioat2_get_ring_ent(ioat, idx + 1);
333 xor_ex = ext->xor_ex;
336 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
337 descs[0] = (struct ioat_raw_descriptor *) xor;
338 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
339 for (i = 0; i < src_cnt; i++) {
340 dma_addr_t src = xor_get_src(descs, i);
342 ioat_unmap(pdev, src - offset, len,
343 PCI_DMA_TODEVICE, flags, 0);
346 /* dest is a source in xor validate operations */
347 if (xor->ctl_f.op == IOAT_OP_XOR_VAL) {
348 ioat_unmap(pdev, xor->dst_addr - offset, len,
349 PCI_DMA_TODEVICE, flags, 1);
354 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
355 ioat_unmap(pdev, xor->dst_addr - offset, len,
356 PCI_DMA_FROMDEVICE, flags, 1);
361 struct ioat_pq_descriptor *pq = desc->pq;
362 struct ioat_ring_ent *ext;
363 struct ioat_pq_ext_descriptor *pq_ex = NULL;
364 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
365 struct ioat_raw_descriptor *descs[2];
369 ext = ioat2_get_ring_ent(ioat, idx + 1);
373 /* in the 'continue' case don't unmap the dests as sources */
374 if (dmaf_p_disabled_continue(flags))
376 else if (dmaf_continue(flags))
379 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
380 descs[0] = (struct ioat_raw_descriptor *) pq;
381 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
382 for (i = 0; i < src_cnt; i++) {
383 dma_addr_t src = pq_get_src(descs, i);
385 ioat_unmap(pdev, src - offset, len,
386 PCI_DMA_TODEVICE, flags, 0);
389 /* the dests are sources in pq validate operations */
390 if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
391 if (!(flags & DMA_PREP_PQ_DISABLE_P))
392 ioat_unmap(pdev, pq->p_addr - offset,
393 len, PCI_DMA_TODEVICE, flags, 0);
394 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
395 ioat_unmap(pdev, pq->q_addr - offset,
396 len, PCI_DMA_TODEVICE, flags, 0);
401 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
402 if (!(flags & DMA_PREP_PQ_DISABLE_P))
403 ioat_unmap(pdev, pq->p_addr - offset, len,
404 PCI_DMA_BIDIRECTIONAL, flags, 1);
405 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
406 ioat_unmap(pdev, pq->q_addr - offset, len,
407 PCI_DMA_BIDIRECTIONAL, flags, 1);
412 case IOAT_OP_PQ_VAL_16S: {
413 struct ioat_pq_descriptor *pq = desc->pq;
414 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
415 struct ioat_raw_descriptor *descs[4];
418 /* in the 'continue' case don't unmap the dests as sources */
419 if (dmaf_p_disabled_continue(flags))
421 else if (dmaf_continue(flags))
424 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
425 descs[0] = (struct ioat_raw_descriptor *)pq;
426 descs[1] = (struct ioat_raw_descriptor *)(desc->sed->hw);
427 descs[2] = (struct ioat_raw_descriptor *)(&desc->sed->hw->b[0]);
428 for (i = 0; i < src_cnt; i++) {
429 dma_addr_t src = pq16_get_src(descs, i);
431 ioat_unmap(pdev, src - offset, len,
432 PCI_DMA_TODEVICE, flags, 0);
435 /* the dests are sources in pq validate operations */
436 if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
437 if (!(flags & DMA_PREP_PQ_DISABLE_P))
438 ioat_unmap(pdev, pq->p_addr - offset,
439 len, PCI_DMA_TODEVICE,
441 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
442 ioat_unmap(pdev, pq->q_addr - offset,
443 len, PCI_DMA_TODEVICE,
449 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
450 if (!(flags & DMA_PREP_PQ_DISABLE_P))
451 ioat_unmap(pdev, pq->p_addr - offset, len,
452 PCI_DMA_BIDIRECTIONAL, flags, 1);
453 if (!(flags & DMA_PREP_PQ_DISABLE_Q))
454 ioat_unmap(pdev, pq->q_addr - offset, len,
455 PCI_DMA_BIDIRECTIONAL, flags, 1);
460 dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
461 __func__, desc->hw->ctl_f.op);
465 static bool desc_has_ext(struct ioat_ring_ent *desc)
467 struct ioat_dma_descriptor *hw = desc->hw;
469 if (hw->ctl_f.op == IOAT_OP_XOR ||
470 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
471 struct ioat_xor_descriptor *xor = desc->xor;
473 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
475 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
476 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
477 struct ioat_pq_descriptor *pq = desc->pq;
479 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
486 static u64 ioat3_get_current_completion(struct ioat_chan_common *chan)
491 completion = *chan->completion;
492 phys_complete = ioat_chansts_to_addr(completion);
494 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
495 (unsigned long long) phys_complete);
497 return phys_complete;
500 static bool ioat3_cleanup_preamble(struct ioat_chan_common *chan,
503 *phys_complete = ioat3_get_current_completion(chan);
504 if (*phys_complete == chan->last_completion)
507 clear_bit(IOAT_COMPLETION_ACK, &chan->state);
508 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
514 desc_get_errstat(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc)
516 struct ioat_dma_descriptor *hw = desc->hw;
518 switch (hw->ctl_f.op) {
520 case IOAT_OP_PQ_VAL_16S:
522 struct ioat_pq_descriptor *pq = desc->pq;
524 /* check if there's error written */
525 if (!pq->dwbes_f.wbes)
528 /* need to set a chanerr var for checking to clear later */
530 if (pq->dwbes_f.p_val_err)
531 *desc->result |= SUM_CHECK_P_RESULT;
533 if (pq->dwbes_f.q_val_err)
534 *desc->result |= SUM_CHECK_Q_RESULT;
544 * __cleanup - reclaim used descriptors
545 * @ioat: channel (ring) to clean
547 * The difference from the dma_v2.c __cleanup() is that this routine
548 * handles extended descriptors and dma-unmapping raid operations.
550 static void __cleanup(struct ioat2_dma_chan *ioat, dma_addr_t phys_complete)
552 struct ioat_chan_common *chan = &ioat->base;
553 struct ioatdma_device *device = chan->device;
554 struct ioat_ring_ent *desc;
555 bool seen_current = false;
556 int idx = ioat->tail, i;
559 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
560 __func__, ioat->head, ioat->tail, ioat->issued);
563 * At restart of the channel, the completion address and the
564 * channel status will be 0 due to starting a new chain. Since
565 * it's new chain and the first descriptor "fails", there is
566 * nothing to clean up. We do not want to reap the entire submitted
567 * chain due to this 0 address value and then BUG.
572 active = ioat2_ring_active(ioat);
573 for (i = 0; i < active && !seen_current; i++) {
574 struct dma_async_tx_descriptor *tx;
576 smp_read_barrier_depends();
577 prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
578 desc = ioat2_get_ring_ent(ioat, idx + i);
579 dump_desc_dbg(ioat, desc);
581 /* set err stat if we are using dwbes */
582 if (device->cap & IOAT_CAP_DWBES)
583 desc_get_errstat(ioat, desc);
587 dma_cookie_complete(tx);
588 ioat3_dma_unmap(ioat, desc, idx + i);
590 tx->callback(tx->callback_param);
595 if (tx->phys == phys_complete)
598 /* skip extended descriptors */
599 if (desc_has_ext(desc)) {
600 BUG_ON(i + 1 >= active);
604 /* cleanup super extended descriptors */
606 ioat3_free_sed(device, desc->sed);
610 smp_mb(); /* finish all descriptor reads before incrementing tail */
611 ioat->tail = idx + i;
612 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
613 chan->last_completion = phys_complete;
615 if (active - i == 0) {
616 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
618 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
619 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
621 /* 5 microsecond delay per pending descriptor */
622 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
623 chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
626 static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
628 struct ioat_chan_common *chan = &ioat->base;
631 spin_lock_bh(&chan->cleanup_lock);
633 if (ioat3_cleanup_preamble(chan, &phys_complete))
634 __cleanup(ioat, phys_complete);
636 if (is_ioat_halted(*chan->completion)) {
637 u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
639 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
640 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
645 spin_unlock_bh(&chan->cleanup_lock);
648 static void ioat3_cleanup_event(unsigned long data)
650 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
653 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
656 static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
658 struct ioat_chan_common *chan = &ioat->base;
661 ioat2_quiesce(chan, 0);
662 if (ioat3_cleanup_preamble(chan, &phys_complete))
663 __cleanup(ioat, phys_complete);
665 __ioat2_restart_chan(ioat);
668 static void ioat3_eh(struct ioat2_dma_chan *ioat)
670 struct ioat_chan_common *chan = &ioat->base;
671 struct pci_dev *pdev = to_pdev(chan);
672 struct ioat_dma_descriptor *hw;
674 struct ioat_ring_ent *desc;
679 /* cleanup so tail points to descriptor that caused the error */
680 if (ioat3_cleanup_preamble(chan, &phys_complete))
681 __cleanup(ioat, phys_complete);
683 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
684 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
686 dev_dbg(to_dev(chan), "%s: error = %x:%x\n",
687 __func__, chanerr, chanerr_int);
689 desc = ioat2_get_ring_ent(ioat, ioat->tail);
691 dump_desc_dbg(ioat, desc);
693 switch (hw->ctl_f.op) {
694 case IOAT_OP_XOR_VAL:
695 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
696 *desc->result |= SUM_CHECK_P_RESULT;
697 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
701 case IOAT_OP_PQ_VAL_16S:
702 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
703 *desc->result |= SUM_CHECK_P_RESULT;
704 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
706 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
707 *desc->result |= SUM_CHECK_Q_RESULT;
708 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
713 /* fault on unhandled error or spurious halt */
714 if (chanerr ^ err_handled || chanerr == 0) {
715 dev_err(to_dev(chan), "%s: fatal error (%x:%x)\n",
716 __func__, chanerr, err_handled);
720 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
721 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
723 /* mark faulting descriptor as complete */
724 *chan->completion = desc->txd.phys;
726 spin_lock_bh(&ioat->prep_lock);
727 ioat3_restart_channel(ioat);
728 spin_unlock_bh(&ioat->prep_lock);
731 static void check_active(struct ioat2_dma_chan *ioat)
733 struct ioat_chan_common *chan = &ioat->base;
735 if (ioat2_ring_active(ioat)) {
736 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
740 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &chan->state))
741 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
742 else if (ioat->alloc_order > ioat_get_alloc_order()) {
743 /* if the ring is idle, empty, and oversized try to step
746 reshape_ring(ioat, ioat->alloc_order - 1);
748 /* keep shrinking until we get back to our minimum
751 if (ioat->alloc_order > ioat_get_alloc_order())
752 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
757 static void ioat3_timer_event(unsigned long data)
759 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
760 struct ioat_chan_common *chan = &ioat->base;
761 dma_addr_t phys_complete;
764 status = ioat_chansts(chan);
766 /* when halted due to errors check for channel
767 * programming errors before advancing the completion state
769 if (is_ioat_halted(status)) {
772 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
773 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
775 if (test_bit(IOAT_RUN, &chan->state))
776 BUG_ON(is_ioat_bug(chanerr));
777 else /* we never got off the ground */
781 /* if we haven't made progress and we have already
782 * acknowledged a pending completion once, then be more
783 * forceful with a restart
785 spin_lock_bh(&chan->cleanup_lock);
786 if (ioat_cleanup_preamble(chan, &phys_complete))
787 __cleanup(ioat, phys_complete);
788 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
789 spin_lock_bh(&ioat->prep_lock);
790 ioat3_restart_channel(ioat);
791 spin_unlock_bh(&ioat->prep_lock);
792 spin_unlock_bh(&chan->cleanup_lock);
795 set_bit(IOAT_COMPLETION_ACK, &chan->state);
796 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
800 if (ioat2_ring_active(ioat))
801 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
803 spin_lock_bh(&ioat->prep_lock);
805 spin_unlock_bh(&ioat->prep_lock);
807 spin_unlock_bh(&chan->cleanup_lock);
810 static enum dma_status
811 ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
812 struct dma_tx_state *txstate)
814 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
817 ret = dma_cookie_status(c, cookie, txstate);
818 if (ret == DMA_SUCCESS)
823 return dma_cookie_status(c, cookie, txstate);
826 static struct dma_async_tx_descriptor *
827 ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
828 size_t len, unsigned long flags)
830 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
831 struct ioat_ring_ent *desc;
832 size_t total_len = len;
833 struct ioat_fill_descriptor *fill;
834 u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
835 int num_descs, idx, i;
837 num_descs = ioat2_xferlen_to_descs(ioat, len);
838 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs) == 0)
844 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
846 desc = ioat2_get_ring_ent(ioat, idx + i);
849 fill->size = xfer_size;
850 fill->src_data = src_data;
851 fill->dst_addr = dest;
853 fill->ctl_f.op = IOAT_OP_FILL;
857 dump_desc_dbg(ioat, desc);
858 } while (++i < num_descs);
860 desc->txd.flags = flags;
861 desc->len = total_len;
862 fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
863 fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
864 fill->ctl_f.compl_write = 1;
865 dump_desc_dbg(ioat, desc);
867 /* we leave the channel locked to ensure in order submission */
871 static struct dma_async_tx_descriptor *
872 __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
873 dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
874 size_t len, unsigned long flags)
876 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
877 struct ioat_ring_ent *compl_desc;
878 struct ioat_ring_ent *desc;
879 struct ioat_ring_ent *ext;
880 size_t total_len = len;
881 struct ioat_xor_descriptor *xor;
882 struct ioat_xor_ext_descriptor *xor_ex = NULL;
883 struct ioat_dma_descriptor *hw;
884 int num_descs, with_ext, idx, i;
886 u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
890 num_descs = ioat2_xferlen_to_descs(ioat, len);
891 /* we need 2x the number of descriptors to cover greater than 5
900 /* completion writes from the raid engine may pass completion
901 * writes from the legacy engine, so we need one extra null
902 * (legacy) descriptor to ensure all completion writes arrive in
905 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
911 struct ioat_raw_descriptor *descs[2];
912 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
915 desc = ioat2_get_ring_ent(ioat, idx + i);
918 /* save a branch by unconditionally retrieving the
919 * extended descriptor xor_set_src() knows to not write
920 * to it in the single descriptor case
922 ext = ioat2_get_ring_ent(ioat, idx + i + 1);
923 xor_ex = ext->xor_ex;
925 descs[0] = (struct ioat_raw_descriptor *) xor;
926 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
927 for (s = 0; s < src_cnt; s++)
928 xor_set_src(descs, src[s], offset, s);
929 xor->size = xfer_size;
930 xor->dst_addr = dest + offset;
933 xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
937 dump_desc_dbg(ioat, desc);
938 } while ((i += 1 + with_ext) < num_descs);
940 /* last xor descriptor carries the unmap parameters and fence bit */
941 desc->txd.flags = flags;
942 desc->len = total_len;
944 desc->result = result;
945 xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
947 /* completion descriptor carries interrupt bit */
948 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
949 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
953 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
954 hw->ctl_f.compl_write = 1;
955 hw->size = NULL_DESC_BUFFER_SIZE;
956 dump_desc_dbg(ioat, compl_desc);
958 /* we leave the channel locked to ensure in order submission */
959 return &compl_desc->txd;
962 static struct dma_async_tx_descriptor *
963 ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
964 unsigned int src_cnt, size_t len, unsigned long flags)
966 return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
969 struct dma_async_tx_descriptor *
970 ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
971 unsigned int src_cnt, size_t len,
972 enum sum_check_flags *result, unsigned long flags)
974 /* the cleanup routine only sets bits on validate failure, it
975 * does not clear bits on validate success... so clear it here
979 return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
980 src_cnt - 1, len, flags);
984 dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
986 struct device *dev = to_dev(&ioat->base);
987 struct ioat_pq_descriptor *pq = desc->pq;
988 struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
989 struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
990 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
993 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
994 " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
996 desc_id(desc), (unsigned long long) desc->txd.phys,
997 (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
998 desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
999 pq->ctl_f.compl_write,
1000 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
1002 for (i = 0; i < src_cnt; i++)
1003 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
1004 (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
1005 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
1006 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
1007 dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
1010 static void dump_pq16_desc_dbg(struct ioat2_dma_chan *ioat,
1011 struct ioat_ring_ent *desc)
1013 struct device *dev = to_dev(&ioat->base);
1014 struct ioat_pq_descriptor *pq = desc->pq;
1015 struct ioat_raw_descriptor *descs[] = { (void *)pq,
1018 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
1022 descs[1] = (void *)desc->sed->hw;
1023 descs[2] = (void *)desc->sed->hw + 64;
1026 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
1027 " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
1029 desc_id(desc), (unsigned long long) desc->txd.phys,
1030 (unsigned long long) pq->next,
1031 desc->txd.flags, pq->size, pq->ctl,
1032 pq->ctl_f.op, pq->ctl_f.int_en,
1033 pq->ctl_f.compl_write,
1034 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
1036 for (i = 0; i < src_cnt; i++) {
1037 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
1038 (unsigned long long) pq16_get_src(descs, i),
1041 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
1042 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
1045 static struct dma_async_tx_descriptor *
1046 __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
1047 const dma_addr_t *dst, const dma_addr_t *src,
1048 unsigned int src_cnt, const unsigned char *scf,
1049 size_t len, unsigned long flags)
1051 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1052 struct ioat_chan_common *chan = &ioat->base;
1053 struct ioatdma_device *device = chan->device;
1054 struct ioat_ring_ent *compl_desc;
1055 struct ioat_ring_ent *desc;
1056 struct ioat_ring_ent *ext;
1057 size_t total_len = len;
1058 struct ioat_pq_descriptor *pq;
1059 struct ioat_pq_ext_descriptor *pq_ex = NULL;
1060 struct ioat_dma_descriptor *hw;
1062 u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
1063 int i, s, idx, with_ext, num_descs;
1064 int cb32 = (device->version < IOAT_VER_3_3) ? 1 : 0;
1066 dev_dbg(to_dev(chan), "%s\n", __func__);
1067 /* the engine requires at least two sources (we provide
1068 * at least 1 implied source in the DMA_PREP_CONTINUE case)
1070 BUG_ON(src_cnt + dmaf_continue(flags) < 2);
1072 num_descs = ioat2_xferlen_to_descs(ioat, len);
1073 /* we need 2x the number of descriptors to cover greater than 3
1074 * sources (we need 1 extra source in the q-only continuation
1075 * case and 3 extra sources in the p+q continuation case.
1077 if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
1078 (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
1084 /* completion writes from the raid engine may pass completion
1085 * writes from the legacy engine, so we need one extra null
1086 * (legacy) descriptor to ensure all completion writes arrive in
1089 if (likely(num_descs) &&
1090 ioat2_check_space_lock(ioat, num_descs + cb32) == 0)
1096 struct ioat_raw_descriptor *descs[2];
1097 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
1099 desc = ioat2_get_ring_ent(ioat, idx + i);
1102 /* save a branch by unconditionally retrieving the
1103 * extended descriptor pq_set_src() knows to not write
1104 * to it in the single descriptor case
1106 ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
1109 descs[0] = (struct ioat_raw_descriptor *) pq;
1110 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
1112 for (s = 0; s < src_cnt; s++)
1113 pq_set_src(descs, src[s], offset, scf[s], s);
1115 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1116 if (dmaf_p_disabled_continue(flags))
1117 pq_set_src(descs, dst[1], offset, 1, s++);
1118 else if (dmaf_continue(flags)) {
1119 pq_set_src(descs, dst[0], offset, 0, s++);
1120 pq_set_src(descs, dst[1], offset, 1, s++);
1121 pq_set_src(descs, dst[1], offset, 0, s++);
1123 pq->size = xfer_size;
1124 pq->p_addr = dst[0] + offset;
1125 pq->q_addr = dst[1] + offset;
1128 /* we turn on descriptor write back error status */
1129 if (device->cap & IOAT_CAP_DWBES)
1130 pq->ctl_f.wb_en = result ? 1 : 0;
1131 pq->ctl_f.src_cnt = src_cnt_to_hw(s);
1132 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1133 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1136 offset += xfer_size;
1137 } while ((i += 1 + with_ext) < num_descs);
1139 /* last pq descriptor carries the unmap parameters and fence bit */
1140 desc->txd.flags = flags;
1141 desc->len = total_len;
1143 desc->result = result;
1144 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1145 dump_pq_desc_dbg(ioat, desc, ext);
1148 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1149 pq->ctl_f.compl_write = 1;
1152 /* completion descriptor carries interrupt bit */
1153 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
1154 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
1155 hw = compl_desc->hw;
1158 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1159 hw->ctl_f.compl_write = 1;
1160 hw->size = NULL_DESC_BUFFER_SIZE;
1161 dump_desc_dbg(ioat, compl_desc);
1165 /* we leave the channel locked to ensure in order submission */
1166 return &compl_desc->txd;
1169 static struct dma_async_tx_descriptor *
1170 __ioat3_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
1171 const dma_addr_t *dst, const dma_addr_t *src,
1172 unsigned int src_cnt, const unsigned char *scf,
1173 size_t len, unsigned long flags)
1175 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1176 struct ioat_chan_common *chan = &ioat->base;
1177 struct ioatdma_device *device = chan->device;
1178 struct ioat_ring_ent *desc;
1179 size_t total_len = len;
1180 struct ioat_pq_descriptor *pq;
1183 int i, s, idx, num_descs;
1185 /* this function only handles src_cnt 9 - 16 */
1186 BUG_ON(src_cnt < 9);
1188 /* this function is only called with 9-16 sources */
1189 op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
1191 dev_dbg(to_dev(chan), "%s\n", __func__);
1193 num_descs = ioat2_xferlen_to_descs(ioat, len);
1196 * 16 source pq is only available on cb3.3 and has no completion
1199 if (num_descs && ioat2_check_space_lock(ioat, num_descs) == 0)
1207 struct ioat_raw_descriptor *descs[4];
1208 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
1210 desc = ioat2_get_ring_ent(ioat, idx + i);
1213 descs[0] = (struct ioat_raw_descriptor *) pq;
1215 desc->sed = ioat3_alloc_sed(device,
1216 sed_get_pq16_pool_idx(src_cnt));
1218 dev_err(to_dev(chan),
1219 "%s: no free sed entries\n", __func__);
1223 pq->sed_addr = desc->sed->dma;
1224 desc->sed->parent = desc;
1226 descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
1227 descs[2] = (void *)descs[1] + 64;
1229 for (s = 0; s < src_cnt; s++)
1230 pq16_set_src(descs, src[s], offset, scf[s], s);
1232 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1233 if (dmaf_p_disabled_continue(flags))
1234 pq16_set_src(descs, dst[1], offset, 1, s++);
1235 else if (dmaf_continue(flags)) {
1236 pq16_set_src(descs, dst[0], offset, 0, s++);
1237 pq16_set_src(descs, dst[1], offset, 1, s++);
1238 pq16_set_src(descs, dst[1], offset, 0, s++);
1241 pq->size = xfer_size;
1242 pq->p_addr = dst[0] + offset;
1243 pq->q_addr = dst[1] + offset;
1246 pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
1247 /* we turn on descriptor write back error status */
1248 if (device->cap & IOAT_CAP_DWBES)
1249 pq->ctl_f.wb_en = result ? 1 : 0;
1250 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1251 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1254 offset += xfer_size;
1255 } while (++i < num_descs);
1257 /* last pq descriptor carries the unmap parameters and fence bit */
1258 desc->txd.flags = flags;
1259 desc->len = total_len;
1261 desc->result = result;
1262 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1264 /* with cb3.3 we should be able to do completion w/o a null desc */
1265 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1266 pq->ctl_f.compl_write = 1;
1268 dump_pq16_desc_dbg(ioat, desc);
1270 /* we leave the channel locked to ensure in order submission */
1274 static struct dma_async_tx_descriptor *
1275 ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
1276 unsigned int src_cnt, const unsigned char *scf, size_t len,
1277 unsigned long flags)
1279 struct dma_device *dma = chan->device;
1281 /* specify valid address for disabled result */
1282 if (flags & DMA_PREP_PQ_DISABLE_P)
1284 if (flags & DMA_PREP_PQ_DISABLE_Q)
1287 /* handle the single source multiply case from the raid6
1290 if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
1291 dma_addr_t single_source[2];
1292 unsigned char single_source_coef[2];
1294 BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
1295 single_source[0] = src[0];
1296 single_source[1] = src[0];
1297 single_source_coef[0] = scf[0];
1298 single_source_coef[1] = 0;
1300 return (src_cnt > 8) && (dma->max_pq > 8) ?
1301 __ioat3_prep_pq16_lock(chan, NULL, dst, single_source,
1302 2, single_source_coef, len,
1304 __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
1305 single_source_coef, len, flags);
1308 return (src_cnt > 8) && (dma->max_pq > 8) ?
1309 __ioat3_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
1311 __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt,
1316 struct dma_async_tx_descriptor *
1317 ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
1318 unsigned int src_cnt, const unsigned char *scf, size_t len,
1319 enum sum_check_flags *pqres, unsigned long flags)
1321 struct dma_device *dma = chan->device;
1323 /* specify valid address for disabled result */
1324 if (flags & DMA_PREP_PQ_DISABLE_P)
1326 if (flags & DMA_PREP_PQ_DISABLE_Q)
1329 /* the cleanup routine only sets bits on validate failure, it
1330 * does not clear bits on validate success... so clear it here
1334 return (src_cnt > 8) && (dma->max_pq > 8) ?
1335 __ioat3_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
1337 __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
1341 static struct dma_async_tx_descriptor *
1342 ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
1343 unsigned int src_cnt, size_t len, unsigned long flags)
1345 struct dma_device *dma = chan->device;
1346 unsigned char scf[src_cnt];
1349 memset(scf, 0, src_cnt);
1351 flags |= DMA_PREP_PQ_DISABLE_Q;
1352 pq[1] = dst; /* specify valid address for disabled result */
1354 return (src_cnt > 8) && (dma->max_pq > 8) ?
1355 __ioat3_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
1357 __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
1361 struct dma_async_tx_descriptor *
1362 ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
1363 unsigned int src_cnt, size_t len,
1364 enum sum_check_flags *result, unsigned long flags)
1366 struct dma_device *dma = chan->device;
1367 unsigned char scf[src_cnt];
1370 /* the cleanup routine only sets bits on validate failure, it
1371 * does not clear bits on validate success... so clear it here
1375 memset(scf, 0, src_cnt);
1377 flags |= DMA_PREP_PQ_DISABLE_Q;
1378 pq[1] = pq[0]; /* specify valid address for disabled result */
1381 return (src_cnt > 8) && (dma->max_pq > 8) ?
1382 __ioat3_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
1384 __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
1388 static struct dma_async_tx_descriptor *
1389 ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
1391 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1392 struct ioat_ring_ent *desc;
1393 struct ioat_dma_descriptor *hw;
1395 if (ioat2_check_space_lock(ioat, 1) == 0)
1396 desc = ioat2_get_ring_ent(ioat, ioat->head);
1403 hw->ctl_f.int_en = 1;
1404 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1405 hw->ctl_f.compl_write = 1;
1406 hw->size = NULL_DESC_BUFFER_SIZE;
1410 desc->txd.flags = flags;
1413 dump_desc_dbg(ioat, desc);
1415 /* we leave the channel locked to ensure in order submission */
1419 static void ioat3_dma_test_callback(void *dma_async_param)
1421 struct completion *cmp = dma_async_param;
1426 #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
1427 static int ioat_xor_val_self_test(struct ioatdma_device *device)
1431 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
1432 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
1433 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
1434 dma_addr_t dma_addr, dest_dma;
1435 struct dma_async_tx_descriptor *tx;
1436 struct dma_chan *dma_chan;
1437 dma_cookie_t cookie;
1442 struct completion cmp;
1444 struct device *dev = &device->pdev->dev;
1445 struct dma_device *dma = &device->common;
1448 dev_dbg(dev, "%s\n", __func__);
1450 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
1453 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1454 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
1455 if (!xor_srcs[src_idx]) {
1457 __free_page(xor_srcs[src_idx]);
1462 dest = alloc_page(GFP_KERNEL);
1465 __free_page(xor_srcs[src_idx]);
1469 /* Fill in src buffers */
1470 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1471 u8 *ptr = page_address(xor_srcs[src_idx]);
1472 for (i = 0; i < PAGE_SIZE; i++)
1473 ptr[i] = (1 << src_idx);
1476 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
1477 cmp_byte ^= (u8) (1 << src_idx);
1479 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
1480 (cmp_byte << 8) | cmp_byte;
1482 memset(page_address(dest), 0, PAGE_SIZE);
1484 dma_chan = container_of(dma->channels.next, struct dma_chan,
1486 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1494 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1495 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1496 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
1498 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
1499 IOAT_NUM_SRC_TEST, PAGE_SIZE,
1500 DMA_PREP_INTERRUPT |
1501 DMA_COMPL_SKIP_SRC_UNMAP |
1502 DMA_COMPL_SKIP_DEST_UNMAP);
1505 dev_err(dev, "Self-test xor prep failed\n");
1511 init_completion(&cmp);
1512 tx->callback = ioat3_dma_test_callback;
1513 tx->callback_param = &cmp;
1514 cookie = tx->tx_submit(tx);
1516 dev_err(dev, "Self-test xor setup failed\n");
1520 dma->device_issue_pending(dma_chan);
1522 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1524 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1525 dev_err(dev, "Self-test xor timed out\n");
1530 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1531 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1532 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1534 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1535 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1536 u32 *ptr = page_address(dest);
1537 if (ptr[i] != cmp_word) {
1538 dev_err(dev, "Self-test xor failed compare\n");
1540 goto free_resources;
1543 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1545 /* skip validate if the capability is not present */
1546 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
1547 goto free_resources;
1549 op = IOAT_OP_XOR_VAL;
1551 /* validate the sources with the destintation page */
1552 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1553 xor_val_srcs[i] = xor_srcs[i];
1554 xor_val_srcs[i] = dest;
1558 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1559 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1561 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1562 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
1563 &xor_val_result, DMA_PREP_INTERRUPT |
1564 DMA_COMPL_SKIP_SRC_UNMAP |
1565 DMA_COMPL_SKIP_DEST_UNMAP);
1567 dev_err(dev, "Self-test zero prep failed\n");
1573 init_completion(&cmp);
1574 tx->callback = ioat3_dma_test_callback;
1575 tx->callback_param = &cmp;
1576 cookie = tx->tx_submit(tx);
1578 dev_err(dev, "Self-test zero setup failed\n");
1582 dma->device_issue_pending(dma_chan);
1584 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1586 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1587 dev_err(dev, "Self-test validate timed out\n");
1592 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1593 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1595 if (xor_val_result != 0) {
1596 dev_err(dev, "Self-test validate failed compare\n");
1598 goto free_resources;
1601 /* skip memset if the capability is not present */
1602 if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
1603 goto free_resources;
1608 dma_addr = dma_map_page(dev, dest, 0,
1609 PAGE_SIZE, DMA_FROM_DEVICE);
1610 tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
1611 DMA_PREP_INTERRUPT |
1612 DMA_COMPL_SKIP_SRC_UNMAP |
1613 DMA_COMPL_SKIP_DEST_UNMAP);
1615 dev_err(dev, "Self-test memset prep failed\n");
1621 init_completion(&cmp);
1622 tx->callback = ioat3_dma_test_callback;
1623 tx->callback_param = &cmp;
1624 cookie = tx->tx_submit(tx);
1626 dev_err(dev, "Self-test memset setup failed\n");
1630 dma->device_issue_pending(dma_chan);
1632 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1634 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1635 dev_err(dev, "Self-test memset timed out\n");
1640 dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
1642 for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
1643 u32 *ptr = page_address(dest);
1645 dev_err(dev, "Self-test memset failed compare\n");
1647 goto free_resources;
1651 /* test for non-zero parity sum */
1652 op = IOAT_OP_XOR_VAL;
1655 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1656 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1658 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1659 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
1660 &xor_val_result, DMA_PREP_INTERRUPT |
1661 DMA_COMPL_SKIP_SRC_UNMAP |
1662 DMA_COMPL_SKIP_DEST_UNMAP);
1664 dev_err(dev, "Self-test 2nd zero prep failed\n");
1670 init_completion(&cmp);
1671 tx->callback = ioat3_dma_test_callback;
1672 tx->callback_param = &cmp;
1673 cookie = tx->tx_submit(tx);
1675 dev_err(dev, "Self-test 2nd zero setup failed\n");
1679 dma->device_issue_pending(dma_chan);
1681 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1683 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1684 dev_err(dev, "Self-test 2nd validate timed out\n");
1689 if (xor_val_result != SUM_CHECK_P_RESULT) {
1690 dev_err(dev, "Self-test validate failed compare\n");
1695 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1696 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1698 goto free_resources;
1700 if (op == IOAT_OP_XOR) {
1701 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1702 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1703 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1705 } else if (op == IOAT_OP_XOR_VAL) {
1706 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1707 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1709 } else if (op == IOAT_OP_FILL)
1710 dma_unmap_page(dev, dma_addr, PAGE_SIZE, DMA_FROM_DEVICE);
1712 dma->device_free_chan_resources(dma_chan);
1714 src_idx = IOAT_NUM_SRC_TEST;
1716 __free_page(xor_srcs[src_idx]);
1721 static int ioat3_dma_self_test(struct ioatdma_device *device)
1723 int rc = ioat_dma_self_test(device);
1728 rc = ioat_xor_val_self_test(device);
1735 static int ioat3_irq_reinit(struct ioatdma_device *device)
1737 int msixcnt = device->common.chancnt;
1738 struct pci_dev *pdev = device->pdev;
1740 struct msix_entry *msix;
1741 struct ioat_chan_common *chan;
1744 switch (device->irq_mode) {
1747 for (i = 0; i < msixcnt; i++) {
1748 msix = &device->msix_entries[i];
1749 chan = ioat_chan_by_index(device, i);
1750 devm_free_irq(&pdev->dev, msix->vector, chan);
1753 pci_disable_msix(pdev);
1756 case IOAT_MSIX_SINGLE:
1757 msix = &device->msix_entries[0];
1758 chan = ioat_chan_by_index(device, 0);
1759 devm_free_irq(&pdev->dev, msix->vector, chan);
1760 pci_disable_msix(pdev);
1764 chan = ioat_chan_by_index(device, 0);
1765 devm_free_irq(&pdev->dev, pdev->irq, chan);
1766 pci_disable_msi(pdev);
1770 chan = ioat_chan_by_index(device, 0);
1771 devm_free_irq(&pdev->dev, pdev->irq, chan);
1778 device->irq_mode = IOAT_NOIRQ;
1780 err = ioat_dma_setup_interrupts(device);
1785 static int ioat3_reset_hw(struct ioat_chan_common *chan)
1787 /* throw away whatever the channel was doing and get it
1788 * initialized, with ioat3 specific workarounds
1790 struct ioatdma_device *device = chan->device;
1791 struct pci_dev *pdev = device->pdev;
1796 ioat2_quiesce(chan, msecs_to_jiffies(100));
1798 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
1799 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
1801 if (device->version < IOAT_VER_3_3) {
1802 /* clear any pending errors */
1803 err = pci_read_config_dword(pdev,
1804 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
1807 "channel error register unreachable\n");
1810 pci_write_config_dword(pdev,
1811 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
1813 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1814 * (workaround for spurious config parity error after restart)
1816 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1817 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
1818 pci_write_config_dword(pdev,
1819 IOAT_PCI_DMAUNCERRSTS_OFFSET,
1824 err = ioat2_reset_sync(chan, msecs_to_jiffies(200));
1826 dev_err(&pdev->dev, "Failed to reset!\n");
1830 if (device->irq_mode != IOAT_NOIRQ && is_bwd_ioat(pdev))
1831 err = ioat3_irq_reinit(device);
1836 static void ioat3_intr_quirk(struct ioatdma_device *device)
1838 struct dma_device *dma;
1840 struct ioat_chan_common *chan;
1843 dma = &device->common;
1846 * if we have descriptor write back error status, we mask the
1849 if (device->cap & IOAT_CAP_DWBES) {
1850 list_for_each_entry(c, &dma->channels, device_node) {
1851 chan = to_chan_common(c);
1852 errmask = readl(chan->reg_base +
1853 IOAT_CHANERR_MASK_OFFSET);
1854 errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
1855 IOAT_CHANERR_XOR_Q_ERR;
1856 writel(errmask, chan->reg_base +
1857 IOAT_CHANERR_MASK_OFFSET);
1862 int ioat3_dma_probe(struct ioatdma_device *device, int dca)
1864 struct pci_dev *pdev = device->pdev;
1865 int dca_en = system_has_dca_enabled(pdev);
1866 struct dma_device *dma;
1868 struct ioat_chan_common *chan;
1869 bool is_raid_device = false;
1872 device->enumerate_channels = ioat2_enumerate_channels;
1873 device->reset_hw = ioat3_reset_hw;
1874 device->self_test = ioat3_dma_self_test;
1875 device->intr_quirk = ioat3_intr_quirk;
1876 dma = &device->common;
1877 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
1878 dma->device_issue_pending = ioat2_issue_pending;
1879 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
1880 dma->device_free_chan_resources = ioat2_free_chan_resources;
1882 if (is_xeon_cb32(pdev))
1883 dma->copy_align = 6;
1885 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1886 dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
1888 device->cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
1890 if (is_bwd_noraid(pdev))
1891 device->cap &= ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
1893 /* dca is incompatible with raid operations */
1894 if (dca_en && (device->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1895 device->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
1897 if (device->cap & IOAT_CAP_XOR) {
1898 is_raid_device = true;
1902 dma_cap_set(DMA_XOR, dma->cap_mask);
1903 dma->device_prep_dma_xor = ioat3_prep_xor;
1905 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1906 dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
1909 if (device->cap & IOAT_CAP_PQ) {
1910 is_raid_device = true;
1912 dma->device_prep_dma_pq = ioat3_prep_pq;
1913 dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
1914 dma_cap_set(DMA_PQ, dma->cap_mask);
1915 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1917 if (device->cap & IOAT_CAP_RAID16SS) {
1918 dma_set_maxpq(dma, 16, 0);
1921 dma_set_maxpq(dma, 8, 0);
1922 if (is_xeon_cb32(pdev))
1928 if (!(device->cap & IOAT_CAP_XOR)) {
1929 dma->device_prep_dma_xor = ioat3_prep_pqxor;
1930 dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
1931 dma_cap_set(DMA_XOR, dma->cap_mask);
1932 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1934 if (device->cap & IOAT_CAP_RAID16SS) {
1939 if (is_xeon_cb32(pdev))
1947 if (is_raid_device && (device->cap & IOAT_CAP_FILL_BLOCK)) {
1948 dma_cap_set(DMA_MEMSET, dma->cap_mask);
1949 dma->device_prep_dma_memset = ioat3_prep_memset_lock;
1953 dma->device_tx_status = ioat3_tx_status;
1954 device->cleanup_fn = ioat3_cleanup_event;
1955 device->timer_fn = ioat3_timer_event;
1957 if (is_xeon_cb32(pdev)) {
1958 dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
1959 dma->device_prep_dma_xor_val = NULL;
1961 dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
1962 dma->device_prep_dma_pq_val = NULL;
1965 /* starting with CB3.3 super extended descriptors are supported */
1966 if (device->cap & IOAT_CAP_RAID16SS) {
1970 /* allocate sw descriptor pool for SED */
1971 device->sed_pool = kmem_cache_create("ioat_sed",
1972 sizeof(struct ioat_sed_ent), 0, 0, NULL);
1973 if (!device->sed_pool)
1976 for (i = 0; i < MAX_SED_POOLS; i++) {
1977 snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1979 /* allocate SED DMA pool */
1980 device->sed_hw_pool[i] = dma_pool_create(pool_name,
1982 SED_SIZE * (i + 1), 64, 0);
1983 if (!device->sed_hw_pool[i])
1984 goto sed_pool_cleanup;
1989 err = ioat_probe(device);
1992 ioat_set_tcp_copy_break(262144);
1994 list_for_each_entry(c, &dma->channels, device_node) {
1995 chan = to_chan_common(c);
1996 writel(IOAT_DMA_DCA_ANY_CPU,
1997 chan->reg_base + IOAT_DCACTRL_OFFSET);
2000 err = ioat_register(device);
2004 ioat_kobject_add(device, &ioat2_ktype);
2007 device->dca = ioat3_dca_init(pdev, device->reg_base);
2012 if (device->sed_pool) {
2014 kmem_cache_destroy(device->sed_pool);
2016 for (i = 0; i < MAX_SED_POOLS; i++)
2017 if (device->sed_hw_pool[i])
2018 dma_pool_destroy(device->sed_hw_pool[i]);
2024 void ioat3_dma_remove(struct ioatdma_device *device)
2026 if (device->sed_pool) {
2028 kmem_cache_destroy(device->sed_pool);
2030 for (i = 0; i < MAX_SED_POOLS; i++)
2031 if (device->sed_hw_pool[i])
2032 dma_pool_destroy(device->sed_hw_pool[i]);