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 };
90 static void ioat3_eh(struct ioat2_dma_chan *ioat);
92 static void xor_set_src(struct ioat_raw_descriptor *descs[2],
93 dma_addr_t addr, u32 offset, int idx)
95 struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
97 raw->field[xor_idx_to_field[idx]] = addr + offset;
100 static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
102 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
104 return raw->field[pq_idx_to_field[idx]];
107 static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
109 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
111 return raw->field[pq16_idx_to_field[idx]];
114 static void pq_set_src(struct ioat_raw_descriptor *descs[2],
115 dma_addr_t addr, u32 offset, u8 coef, int idx)
117 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
118 struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
120 raw->field[pq_idx_to_field[idx]] = addr + offset;
121 pq->coef[idx] = coef;
124 static bool is_jf_ioat(struct pci_dev *pdev)
126 switch (pdev->device) {
127 case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
128 case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
129 case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
130 case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
131 case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
132 case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
133 case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
134 case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
135 case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
136 case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
143 static bool is_snb_ioat(struct pci_dev *pdev)
145 switch (pdev->device) {
146 case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
147 case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
148 case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
149 case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
150 case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
151 case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
152 case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
153 case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
154 case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
155 case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
162 static bool is_ivb_ioat(struct pci_dev *pdev)
164 switch (pdev->device) {
165 case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
166 case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
167 case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
168 case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
169 case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
170 case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
171 case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
172 case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
173 case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
174 case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
182 static bool is_hsw_ioat(struct pci_dev *pdev)
184 switch (pdev->device) {
185 case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
186 case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
187 case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
188 case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
189 case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
190 case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
191 case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
192 case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
193 case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
194 case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
202 static bool is_xeon_cb32(struct pci_dev *pdev)
204 return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
208 static bool is_bwd_ioat(struct pci_dev *pdev)
210 switch (pdev->device) {
211 case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
212 case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
213 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
214 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
221 static bool is_bwd_noraid(struct pci_dev *pdev)
223 switch (pdev->device) {
224 case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
225 case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
233 static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
234 dma_addr_t addr, u32 offset, u8 coef, unsigned idx)
236 struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
237 struct ioat_pq16a_descriptor *pq16 =
238 (struct ioat_pq16a_descriptor *)desc[1];
239 struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
241 raw->field[pq16_idx_to_field[idx]] = addr + offset;
244 pq->coef[idx] = coef;
246 pq16->coef[idx - 8] = coef;
249 static struct ioat_sed_ent *
250 ioat3_alloc_sed(struct ioatdma_device *device, unsigned int hw_pool)
252 struct ioat_sed_ent *sed;
253 gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
255 sed = kmem_cache_alloc(device->sed_pool, flags);
259 sed->hw_pool = hw_pool;
260 sed->hw = dma_pool_alloc(device->sed_hw_pool[hw_pool],
263 kmem_cache_free(device->sed_pool, sed);
270 static void ioat3_free_sed(struct ioatdma_device *device, struct ioat_sed_ent *sed)
275 dma_pool_free(device->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
276 kmem_cache_free(device->sed_pool, sed);
279 static bool desc_has_ext(struct ioat_ring_ent *desc)
281 struct ioat_dma_descriptor *hw = desc->hw;
283 if (hw->ctl_f.op == IOAT_OP_XOR ||
284 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
285 struct ioat_xor_descriptor *xor = desc->xor;
287 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
289 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
290 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
291 struct ioat_pq_descriptor *pq = desc->pq;
293 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
300 static u64 ioat3_get_current_completion(struct ioat_chan_common *chan)
305 completion = *chan->completion;
306 phys_complete = ioat_chansts_to_addr(completion);
308 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
309 (unsigned long long) phys_complete);
311 return phys_complete;
314 static bool ioat3_cleanup_preamble(struct ioat_chan_common *chan,
317 *phys_complete = ioat3_get_current_completion(chan);
318 if (*phys_complete == chan->last_completion)
321 clear_bit(IOAT_COMPLETION_ACK, &chan->state);
322 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
328 desc_get_errstat(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc)
330 struct ioat_dma_descriptor *hw = desc->hw;
332 switch (hw->ctl_f.op) {
334 case IOAT_OP_PQ_VAL_16S:
336 struct ioat_pq_descriptor *pq = desc->pq;
338 /* check if there's error written */
339 if (!pq->dwbes_f.wbes)
342 /* need to set a chanerr var for checking to clear later */
344 if (pq->dwbes_f.p_val_err)
345 *desc->result |= SUM_CHECK_P_RESULT;
347 if (pq->dwbes_f.q_val_err)
348 *desc->result |= SUM_CHECK_Q_RESULT;
358 * __cleanup - reclaim used descriptors
359 * @ioat: channel (ring) to clean
361 * The difference from the dma_v2.c __cleanup() is that this routine
362 * handles extended descriptors and dma-unmapping raid operations.
364 static void __cleanup(struct ioat2_dma_chan *ioat, dma_addr_t phys_complete)
366 struct ioat_chan_common *chan = &ioat->base;
367 struct ioatdma_device *device = chan->device;
368 struct ioat_ring_ent *desc;
369 bool seen_current = false;
370 int idx = ioat->tail, i;
373 dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
374 __func__, ioat->head, ioat->tail, ioat->issued);
377 * At restart of the channel, the completion address and the
378 * channel status will be 0 due to starting a new chain. Since
379 * it's new chain and the first descriptor "fails", there is
380 * nothing to clean up. We do not want to reap the entire submitted
381 * chain due to this 0 address value and then BUG.
386 active = ioat2_ring_active(ioat);
387 for (i = 0; i < active && !seen_current; i++) {
388 struct dma_async_tx_descriptor *tx;
390 smp_read_barrier_depends();
391 prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
392 desc = ioat2_get_ring_ent(ioat, idx + i);
393 dump_desc_dbg(ioat, desc);
395 /* set err stat if we are using dwbes */
396 if (device->cap & IOAT_CAP_DWBES)
397 desc_get_errstat(ioat, desc);
401 dma_cookie_complete(tx);
402 dma_descriptor_unmap(tx);
404 tx->callback(tx->callback_param);
409 if (tx->phys == phys_complete)
412 /* skip extended descriptors */
413 if (desc_has_ext(desc)) {
414 BUG_ON(i + 1 >= active);
418 /* cleanup super extended descriptors */
420 ioat3_free_sed(device, desc->sed);
424 smp_mb(); /* finish all descriptor reads before incrementing tail */
425 ioat->tail = idx + i;
426 BUG_ON(active && !seen_current); /* no active descs have written a completion? */
427 chan->last_completion = phys_complete;
429 if (active - i == 0) {
430 dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
432 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
433 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
435 /* 5 microsecond delay per pending descriptor */
436 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
437 chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
440 static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
442 struct ioat_chan_common *chan = &ioat->base;
445 spin_lock_bh(&chan->cleanup_lock);
447 if (ioat3_cleanup_preamble(chan, &phys_complete))
448 __cleanup(ioat, phys_complete);
450 if (is_ioat_halted(*chan->completion)) {
451 u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
453 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
454 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
459 spin_unlock_bh(&chan->cleanup_lock);
462 static void ioat3_cleanup_event(unsigned long data)
464 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
467 writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
470 static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
472 struct ioat_chan_common *chan = &ioat->base;
475 ioat2_quiesce(chan, 0);
476 if (ioat3_cleanup_preamble(chan, &phys_complete))
477 __cleanup(ioat, phys_complete);
479 __ioat2_restart_chan(ioat);
482 static void ioat3_eh(struct ioat2_dma_chan *ioat)
484 struct ioat_chan_common *chan = &ioat->base;
485 struct pci_dev *pdev = to_pdev(chan);
486 struct ioat_dma_descriptor *hw;
488 struct ioat_ring_ent *desc;
493 /* cleanup so tail points to descriptor that caused the error */
494 if (ioat3_cleanup_preamble(chan, &phys_complete))
495 __cleanup(ioat, phys_complete);
497 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
498 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
500 dev_dbg(to_dev(chan), "%s: error = %x:%x\n",
501 __func__, chanerr, chanerr_int);
503 desc = ioat2_get_ring_ent(ioat, ioat->tail);
505 dump_desc_dbg(ioat, desc);
507 switch (hw->ctl_f.op) {
508 case IOAT_OP_XOR_VAL:
509 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
510 *desc->result |= SUM_CHECK_P_RESULT;
511 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
515 case IOAT_OP_PQ_VAL_16S:
516 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
517 *desc->result |= SUM_CHECK_P_RESULT;
518 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
520 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
521 *desc->result |= SUM_CHECK_Q_RESULT;
522 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
527 /* fault on unhandled error or spurious halt */
528 if (chanerr ^ err_handled || chanerr == 0) {
529 dev_err(to_dev(chan), "%s: fatal error (%x:%x)\n",
530 __func__, chanerr, err_handled);
534 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
535 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
537 /* mark faulting descriptor as complete */
538 *chan->completion = desc->txd.phys;
540 spin_lock_bh(&ioat->prep_lock);
541 ioat3_restart_channel(ioat);
542 spin_unlock_bh(&ioat->prep_lock);
545 static void check_active(struct ioat2_dma_chan *ioat)
547 struct ioat_chan_common *chan = &ioat->base;
549 if (ioat2_ring_active(ioat)) {
550 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
554 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &chan->state))
555 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
556 else if (ioat->alloc_order > ioat_get_alloc_order()) {
557 /* if the ring is idle, empty, and oversized try to step
560 reshape_ring(ioat, ioat->alloc_order - 1);
562 /* keep shrinking until we get back to our minimum
565 if (ioat->alloc_order > ioat_get_alloc_order())
566 mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
571 static void ioat3_timer_event(unsigned long data)
573 struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
574 struct ioat_chan_common *chan = &ioat->base;
575 dma_addr_t phys_complete;
578 status = ioat_chansts(chan);
580 /* when halted due to errors check for channel
581 * programming errors before advancing the completion state
583 if (is_ioat_halted(status)) {
586 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
587 dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
589 if (test_bit(IOAT_RUN, &chan->state))
590 BUG_ON(is_ioat_bug(chanerr));
591 else /* we never got off the ground */
595 /* if we haven't made progress and we have already
596 * acknowledged a pending completion once, then be more
597 * forceful with a restart
599 spin_lock_bh(&chan->cleanup_lock);
600 if (ioat_cleanup_preamble(chan, &phys_complete))
601 __cleanup(ioat, phys_complete);
602 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
603 spin_lock_bh(&ioat->prep_lock);
604 ioat3_restart_channel(ioat);
605 spin_unlock_bh(&ioat->prep_lock);
606 spin_unlock_bh(&chan->cleanup_lock);
609 set_bit(IOAT_COMPLETION_ACK, &chan->state);
610 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
614 if (ioat2_ring_active(ioat))
615 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
617 spin_lock_bh(&ioat->prep_lock);
619 spin_unlock_bh(&ioat->prep_lock);
621 spin_unlock_bh(&chan->cleanup_lock);
624 static enum dma_status
625 ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
626 struct dma_tx_state *txstate)
628 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
631 ret = dma_cookie_status(c, cookie, txstate);
632 if (ret == DMA_SUCCESS)
637 return dma_cookie_status(c, cookie, txstate);
640 static struct dma_async_tx_descriptor *
641 __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
642 dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
643 size_t len, unsigned long flags)
645 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
646 struct ioat_ring_ent *compl_desc;
647 struct ioat_ring_ent *desc;
648 struct ioat_ring_ent *ext;
649 size_t total_len = len;
650 struct ioat_xor_descriptor *xor;
651 struct ioat_xor_ext_descriptor *xor_ex = NULL;
652 struct ioat_dma_descriptor *hw;
653 int num_descs, with_ext, idx, i;
655 u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
659 num_descs = ioat2_xferlen_to_descs(ioat, len);
660 /* we need 2x the number of descriptors to cover greater than 5
669 /* completion writes from the raid engine may pass completion
670 * writes from the legacy engine, so we need one extra null
671 * (legacy) descriptor to ensure all completion writes arrive in
674 if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
680 struct ioat_raw_descriptor *descs[2];
681 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
684 desc = ioat2_get_ring_ent(ioat, idx + i);
687 /* save a branch by unconditionally retrieving the
688 * extended descriptor xor_set_src() knows to not write
689 * to it in the single descriptor case
691 ext = ioat2_get_ring_ent(ioat, idx + i + 1);
692 xor_ex = ext->xor_ex;
694 descs[0] = (struct ioat_raw_descriptor *) xor;
695 descs[1] = (struct ioat_raw_descriptor *) xor_ex;
696 for (s = 0; s < src_cnt; s++)
697 xor_set_src(descs, src[s], offset, s);
698 xor->size = xfer_size;
699 xor->dst_addr = dest + offset;
702 xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
706 dump_desc_dbg(ioat, desc);
707 } while ((i += 1 + with_ext) < num_descs);
709 /* last xor descriptor carries the unmap parameters and fence bit */
710 desc->txd.flags = flags;
711 desc->len = total_len;
713 desc->result = result;
714 xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
716 /* completion descriptor carries interrupt bit */
717 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
718 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
722 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
723 hw->ctl_f.compl_write = 1;
724 hw->size = NULL_DESC_BUFFER_SIZE;
725 dump_desc_dbg(ioat, compl_desc);
727 /* we leave the channel locked to ensure in order submission */
728 return &compl_desc->txd;
731 static struct dma_async_tx_descriptor *
732 ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
733 unsigned int src_cnt, size_t len, unsigned long flags)
735 return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
738 struct dma_async_tx_descriptor *
739 ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
740 unsigned int src_cnt, size_t len,
741 enum sum_check_flags *result, unsigned long flags)
743 /* the cleanup routine only sets bits on validate failure, it
744 * does not clear bits on validate success... so clear it here
748 return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
749 src_cnt - 1, len, flags);
753 dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
755 struct device *dev = to_dev(&ioat->base);
756 struct ioat_pq_descriptor *pq = desc->pq;
757 struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
758 struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
759 int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
762 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
763 " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
765 desc_id(desc), (unsigned long long) desc->txd.phys,
766 (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
767 desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
768 pq->ctl_f.compl_write,
769 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
771 for (i = 0; i < src_cnt; i++)
772 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
773 (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
774 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
775 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
776 dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
779 static void dump_pq16_desc_dbg(struct ioat2_dma_chan *ioat,
780 struct ioat_ring_ent *desc)
782 struct device *dev = to_dev(&ioat->base);
783 struct ioat_pq_descriptor *pq = desc->pq;
784 struct ioat_raw_descriptor *descs[] = { (void *)pq,
787 int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
791 descs[1] = (void *)desc->sed->hw;
792 descs[2] = (void *)desc->sed->hw + 64;
795 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
796 " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
798 desc_id(desc), (unsigned long long) desc->txd.phys,
799 (unsigned long long) pq->next,
800 desc->txd.flags, pq->size, pq->ctl,
801 pq->ctl_f.op, pq->ctl_f.int_en,
802 pq->ctl_f.compl_write,
803 pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
805 for (i = 0; i < src_cnt; i++) {
806 dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
807 (unsigned long long) pq16_get_src(descs, i),
810 dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
811 dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
814 static struct dma_async_tx_descriptor *
815 __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
816 const dma_addr_t *dst, const dma_addr_t *src,
817 unsigned int src_cnt, const unsigned char *scf,
818 size_t len, unsigned long flags)
820 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
821 struct ioat_chan_common *chan = &ioat->base;
822 struct ioatdma_device *device = chan->device;
823 struct ioat_ring_ent *compl_desc;
824 struct ioat_ring_ent *desc;
825 struct ioat_ring_ent *ext;
826 size_t total_len = len;
827 struct ioat_pq_descriptor *pq;
828 struct ioat_pq_ext_descriptor *pq_ex = NULL;
829 struct ioat_dma_descriptor *hw;
831 u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
832 int i, s, idx, with_ext, num_descs;
833 int cb32 = (device->version < IOAT_VER_3_3) ? 1 : 0;
835 dev_dbg(to_dev(chan), "%s\n", __func__);
836 /* the engine requires at least two sources (we provide
837 * at least 1 implied source in the DMA_PREP_CONTINUE case)
839 BUG_ON(src_cnt + dmaf_continue(flags) < 2);
841 num_descs = ioat2_xferlen_to_descs(ioat, len);
842 /* we need 2x the number of descriptors to cover greater than 3
843 * sources (we need 1 extra source in the q-only continuation
844 * case and 3 extra sources in the p+q continuation case.
846 if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
847 (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
853 /* completion writes from the raid engine may pass completion
854 * writes from the legacy engine, so we need one extra null
855 * (legacy) descriptor to ensure all completion writes arrive in
858 if (likely(num_descs) &&
859 ioat2_check_space_lock(ioat, num_descs + cb32) == 0)
865 struct ioat_raw_descriptor *descs[2];
866 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
868 desc = ioat2_get_ring_ent(ioat, idx + i);
871 /* save a branch by unconditionally retrieving the
872 * extended descriptor pq_set_src() knows to not write
873 * to it in the single descriptor case
875 ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
878 descs[0] = (struct ioat_raw_descriptor *) pq;
879 descs[1] = (struct ioat_raw_descriptor *) pq_ex;
881 for (s = 0; s < src_cnt; s++)
882 pq_set_src(descs, src[s], offset, scf[s], s);
884 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
885 if (dmaf_p_disabled_continue(flags))
886 pq_set_src(descs, dst[1], offset, 1, s++);
887 else if (dmaf_continue(flags)) {
888 pq_set_src(descs, dst[0], offset, 0, s++);
889 pq_set_src(descs, dst[1], offset, 1, s++);
890 pq_set_src(descs, dst[1], offset, 0, s++);
892 pq->size = xfer_size;
893 pq->p_addr = dst[0] + offset;
894 pq->q_addr = dst[1] + offset;
897 /* we turn on descriptor write back error status */
898 if (device->cap & IOAT_CAP_DWBES)
899 pq->ctl_f.wb_en = result ? 1 : 0;
900 pq->ctl_f.src_cnt = src_cnt_to_hw(s);
901 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
902 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
906 } while ((i += 1 + with_ext) < num_descs);
908 /* last pq descriptor carries the unmap parameters and fence bit */
909 desc->txd.flags = flags;
910 desc->len = total_len;
912 desc->result = result;
913 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
914 dump_pq_desc_dbg(ioat, desc, ext);
917 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
918 pq->ctl_f.compl_write = 1;
921 /* completion descriptor carries interrupt bit */
922 compl_desc = ioat2_get_ring_ent(ioat, idx + i);
923 compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
927 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
928 hw->ctl_f.compl_write = 1;
929 hw->size = NULL_DESC_BUFFER_SIZE;
930 dump_desc_dbg(ioat, compl_desc);
934 /* we leave the channel locked to ensure in order submission */
935 return &compl_desc->txd;
938 static struct dma_async_tx_descriptor *
939 __ioat3_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
940 const dma_addr_t *dst, const dma_addr_t *src,
941 unsigned int src_cnt, const unsigned char *scf,
942 size_t len, unsigned long flags)
944 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
945 struct ioat_chan_common *chan = &ioat->base;
946 struct ioatdma_device *device = chan->device;
947 struct ioat_ring_ent *desc;
948 size_t total_len = len;
949 struct ioat_pq_descriptor *pq;
952 int i, s, idx, num_descs;
954 /* this function is only called with 9-16 sources */
955 op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
957 dev_dbg(to_dev(chan), "%s\n", __func__);
959 num_descs = ioat2_xferlen_to_descs(ioat, len);
962 * 16 source pq is only available on cb3.3 and has no completion
965 if (num_descs && ioat2_check_space_lock(ioat, num_descs) == 0)
973 struct ioat_raw_descriptor *descs[4];
974 size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
976 desc = ioat2_get_ring_ent(ioat, idx + i);
979 descs[0] = (struct ioat_raw_descriptor *) pq;
981 desc->sed = ioat3_alloc_sed(device, (src_cnt-2) >> 3);
983 dev_err(to_dev(chan),
984 "%s: no free sed entries\n", __func__);
988 pq->sed_addr = desc->sed->dma;
989 desc->sed->parent = desc;
991 descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
992 descs[2] = (void *)descs[1] + 64;
994 for (s = 0; s < src_cnt; s++)
995 pq16_set_src(descs, src[s], offset, scf[s], s);
997 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
998 if (dmaf_p_disabled_continue(flags))
999 pq16_set_src(descs, dst[1], offset, 1, s++);
1000 else if (dmaf_continue(flags)) {
1001 pq16_set_src(descs, dst[0], offset, 0, s++);
1002 pq16_set_src(descs, dst[1], offset, 1, s++);
1003 pq16_set_src(descs, dst[1], offset, 0, s++);
1006 pq->size = xfer_size;
1007 pq->p_addr = dst[0] + offset;
1008 pq->q_addr = dst[1] + offset;
1011 pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
1012 /* we turn on descriptor write back error status */
1013 if (device->cap & IOAT_CAP_DWBES)
1014 pq->ctl_f.wb_en = result ? 1 : 0;
1015 pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
1016 pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
1019 offset += xfer_size;
1020 } while (++i < num_descs);
1022 /* last pq descriptor carries the unmap parameters and fence bit */
1023 desc->txd.flags = flags;
1024 desc->len = total_len;
1026 desc->result = result;
1027 pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1029 /* with cb3.3 we should be able to do completion w/o a null desc */
1030 pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
1031 pq->ctl_f.compl_write = 1;
1033 dump_pq16_desc_dbg(ioat, desc);
1035 /* we leave the channel locked to ensure in order submission */
1039 static int src_cnt_flags(unsigned int src_cnt, unsigned long flags)
1041 if (dmaf_p_disabled_continue(flags))
1043 else if (dmaf_continue(flags))
1049 static struct dma_async_tx_descriptor *
1050 ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
1051 unsigned int src_cnt, const unsigned char *scf, size_t len,
1052 unsigned long flags)
1054 /* specify valid address for disabled result */
1055 if (flags & DMA_PREP_PQ_DISABLE_P)
1057 if (flags & DMA_PREP_PQ_DISABLE_Q)
1060 /* handle the single source multiply case from the raid6
1063 if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
1064 dma_addr_t single_source[2];
1065 unsigned char single_source_coef[2];
1067 BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
1068 single_source[0] = src[0];
1069 single_source[1] = src[0];
1070 single_source_coef[0] = scf[0];
1071 single_source_coef[1] = 0;
1073 return src_cnt_flags(src_cnt, flags) > 8 ?
1074 __ioat3_prep_pq16_lock(chan, NULL, dst, single_source,
1075 2, single_source_coef, len,
1077 __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
1078 single_source_coef, len, flags);
1081 return src_cnt_flags(src_cnt, flags) > 8 ?
1082 __ioat3_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
1084 __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt,
1089 struct dma_async_tx_descriptor *
1090 ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
1091 unsigned int src_cnt, const unsigned char *scf, size_t len,
1092 enum sum_check_flags *pqres, unsigned long flags)
1094 /* specify valid address for disabled result */
1095 if (flags & DMA_PREP_PQ_DISABLE_P)
1097 if (flags & DMA_PREP_PQ_DISABLE_Q)
1100 /* the cleanup routine only sets bits on validate failure, it
1101 * does not clear bits on validate success... so clear it here
1105 return src_cnt_flags(src_cnt, flags) > 8 ?
1106 __ioat3_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
1108 __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
1112 static struct dma_async_tx_descriptor *
1113 ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
1114 unsigned int src_cnt, size_t len, unsigned long flags)
1116 unsigned char scf[src_cnt];
1119 memset(scf, 0, src_cnt);
1121 flags |= DMA_PREP_PQ_DISABLE_Q;
1122 pq[1] = dst; /* specify valid address for disabled result */
1124 return src_cnt_flags(src_cnt, flags) > 8 ?
1125 __ioat3_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
1127 __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
1131 struct dma_async_tx_descriptor *
1132 ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
1133 unsigned int src_cnt, size_t len,
1134 enum sum_check_flags *result, unsigned long flags)
1136 unsigned char scf[src_cnt];
1139 /* the cleanup routine only sets bits on validate failure, it
1140 * does not clear bits on validate success... so clear it here
1144 memset(scf, 0, src_cnt);
1146 flags |= DMA_PREP_PQ_DISABLE_Q;
1147 pq[1] = pq[0]; /* specify valid address for disabled result */
1149 return src_cnt_flags(src_cnt, flags) > 8 ?
1150 __ioat3_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
1152 __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
1156 static struct dma_async_tx_descriptor *
1157 ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
1159 struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
1160 struct ioat_ring_ent *desc;
1161 struct ioat_dma_descriptor *hw;
1163 if (ioat2_check_space_lock(ioat, 1) == 0)
1164 desc = ioat2_get_ring_ent(ioat, ioat->head);
1171 hw->ctl_f.int_en = 1;
1172 hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
1173 hw->ctl_f.compl_write = 1;
1174 hw->size = NULL_DESC_BUFFER_SIZE;
1178 desc->txd.flags = flags;
1181 dump_desc_dbg(ioat, desc);
1183 /* we leave the channel locked to ensure in order submission */
1187 static void ioat3_dma_test_callback(void *dma_async_param)
1189 struct completion *cmp = dma_async_param;
1194 #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
1195 static int ioat_xor_val_self_test(struct ioatdma_device *device)
1199 struct page *xor_srcs[IOAT_NUM_SRC_TEST];
1200 struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
1201 dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
1202 dma_addr_t dest_dma;
1203 struct dma_async_tx_descriptor *tx;
1204 struct dma_chan *dma_chan;
1205 dma_cookie_t cookie;
1210 struct completion cmp;
1212 struct device *dev = &device->pdev->dev;
1213 struct dma_device *dma = &device->common;
1216 dev_dbg(dev, "%s\n", __func__);
1218 if (!dma_has_cap(DMA_XOR, dma->cap_mask))
1221 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1222 xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
1223 if (!xor_srcs[src_idx]) {
1225 __free_page(xor_srcs[src_idx]);
1230 dest = alloc_page(GFP_KERNEL);
1233 __free_page(xor_srcs[src_idx]);
1237 /* Fill in src buffers */
1238 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
1239 u8 *ptr = page_address(xor_srcs[src_idx]);
1240 for (i = 0; i < PAGE_SIZE; i++)
1241 ptr[i] = (1 << src_idx);
1244 for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
1245 cmp_byte ^= (u8) (1 << src_idx);
1247 cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
1248 (cmp_byte << 8) | cmp_byte;
1250 memset(page_address(dest), 0, PAGE_SIZE);
1252 dma_chan = container_of(dma->channels.next, struct dma_chan,
1254 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1262 dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
1263 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1264 dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
1266 tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
1267 IOAT_NUM_SRC_TEST, PAGE_SIZE,
1268 DMA_PREP_INTERRUPT);
1271 dev_err(dev, "Self-test xor prep failed\n");
1277 init_completion(&cmp);
1278 tx->callback = ioat3_dma_test_callback;
1279 tx->callback_param = &cmp;
1280 cookie = tx->tx_submit(tx);
1282 dev_err(dev, "Self-test xor setup failed\n");
1286 dma->device_issue_pending(dma_chan);
1288 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1290 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1291 dev_err(dev, "Self-test xor timed out\n");
1296 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1297 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1298 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1300 dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1301 for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
1302 u32 *ptr = page_address(dest);
1303 if (ptr[i] != cmp_word) {
1304 dev_err(dev, "Self-test xor failed compare\n");
1306 goto free_resources;
1309 dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1311 /* skip validate if the capability is not present */
1312 if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
1313 goto free_resources;
1315 op = IOAT_OP_XOR_VAL;
1317 /* validate the sources with the destintation page */
1318 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1319 xor_val_srcs[i] = xor_srcs[i];
1320 xor_val_srcs[i] = dest;
1324 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1325 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1327 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1328 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
1329 &xor_val_result, DMA_PREP_INTERRUPT);
1331 dev_err(dev, "Self-test zero prep failed\n");
1337 init_completion(&cmp);
1338 tx->callback = ioat3_dma_test_callback;
1339 tx->callback_param = &cmp;
1340 cookie = tx->tx_submit(tx);
1342 dev_err(dev, "Self-test zero setup failed\n");
1346 dma->device_issue_pending(dma_chan);
1348 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1350 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1351 dev_err(dev, "Self-test validate timed out\n");
1356 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1357 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1359 if (xor_val_result != 0) {
1360 dev_err(dev, "Self-test validate failed compare\n");
1362 goto free_resources;
1365 memset(page_address(dest), 0, PAGE_SIZE);
1367 /* test for non-zero parity sum */
1368 op = IOAT_OP_XOR_VAL;
1371 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1372 dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
1374 tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
1375 IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
1376 &xor_val_result, DMA_PREP_INTERRUPT);
1378 dev_err(dev, "Self-test 2nd zero prep failed\n");
1384 init_completion(&cmp);
1385 tx->callback = ioat3_dma_test_callback;
1386 tx->callback_param = &cmp;
1387 cookie = tx->tx_submit(tx);
1389 dev_err(dev, "Self-test 2nd zero setup failed\n");
1393 dma->device_issue_pending(dma_chan);
1395 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
1397 if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
1398 dev_err(dev, "Self-test 2nd validate timed out\n");
1403 if (xor_val_result != SUM_CHECK_P_RESULT) {
1404 dev_err(dev, "Self-test validate failed compare\n");
1409 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1410 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
1412 goto free_resources;
1414 if (op == IOAT_OP_XOR) {
1415 dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
1416 for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
1417 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1419 } else if (op == IOAT_OP_XOR_VAL) {
1420 for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
1421 dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
1425 dma->device_free_chan_resources(dma_chan);
1427 src_idx = IOAT_NUM_SRC_TEST;
1429 __free_page(xor_srcs[src_idx]);
1434 static int ioat3_dma_self_test(struct ioatdma_device *device)
1436 int rc = ioat_dma_self_test(device);
1441 rc = ioat_xor_val_self_test(device);
1448 static int ioat3_irq_reinit(struct ioatdma_device *device)
1450 int msixcnt = device->common.chancnt;
1451 struct pci_dev *pdev = device->pdev;
1453 struct msix_entry *msix;
1454 struct ioat_chan_common *chan;
1457 switch (device->irq_mode) {
1460 for (i = 0; i < msixcnt; i++) {
1461 msix = &device->msix_entries[i];
1462 chan = ioat_chan_by_index(device, i);
1463 devm_free_irq(&pdev->dev, msix->vector, chan);
1466 pci_disable_msix(pdev);
1469 case IOAT_MSIX_SINGLE:
1470 msix = &device->msix_entries[0];
1471 chan = ioat_chan_by_index(device, 0);
1472 devm_free_irq(&pdev->dev, msix->vector, chan);
1473 pci_disable_msix(pdev);
1477 chan = ioat_chan_by_index(device, 0);
1478 devm_free_irq(&pdev->dev, pdev->irq, chan);
1479 pci_disable_msi(pdev);
1483 chan = ioat_chan_by_index(device, 0);
1484 devm_free_irq(&pdev->dev, pdev->irq, chan);
1491 device->irq_mode = IOAT_NOIRQ;
1493 err = ioat_dma_setup_interrupts(device);
1498 static int ioat3_reset_hw(struct ioat_chan_common *chan)
1500 /* throw away whatever the channel was doing and get it
1501 * initialized, with ioat3 specific workarounds
1503 struct ioatdma_device *device = chan->device;
1504 struct pci_dev *pdev = device->pdev;
1509 ioat2_quiesce(chan, msecs_to_jiffies(100));
1511 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
1512 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
1514 if (device->version < IOAT_VER_3_3) {
1515 /* clear any pending errors */
1516 err = pci_read_config_dword(pdev,
1517 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
1520 "channel error register unreachable\n");
1523 pci_write_config_dword(pdev,
1524 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
1526 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1527 * (workaround for spurious config parity error after restart)
1529 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1530 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
1531 pci_write_config_dword(pdev,
1532 IOAT_PCI_DMAUNCERRSTS_OFFSET,
1537 err = ioat2_reset_sync(chan, msecs_to_jiffies(200));
1539 dev_err(&pdev->dev, "Failed to reset!\n");
1543 if (device->irq_mode != IOAT_NOIRQ && is_bwd_ioat(pdev))
1544 err = ioat3_irq_reinit(device);
1549 static void ioat3_intr_quirk(struct ioatdma_device *device)
1551 struct dma_device *dma;
1553 struct ioat_chan_common *chan;
1556 dma = &device->common;
1559 * if we have descriptor write back error status, we mask the
1562 if (device->cap & IOAT_CAP_DWBES) {
1563 list_for_each_entry(c, &dma->channels, device_node) {
1564 chan = to_chan_common(c);
1565 errmask = readl(chan->reg_base +
1566 IOAT_CHANERR_MASK_OFFSET);
1567 errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
1568 IOAT_CHANERR_XOR_Q_ERR;
1569 writel(errmask, chan->reg_base +
1570 IOAT_CHANERR_MASK_OFFSET);
1575 int ioat3_dma_probe(struct ioatdma_device *device, int dca)
1577 struct pci_dev *pdev = device->pdev;
1578 int dca_en = system_has_dca_enabled(pdev);
1579 struct dma_device *dma;
1581 struct ioat_chan_common *chan;
1582 bool is_raid_device = false;
1585 device->enumerate_channels = ioat2_enumerate_channels;
1586 device->reset_hw = ioat3_reset_hw;
1587 device->self_test = ioat3_dma_self_test;
1588 device->intr_quirk = ioat3_intr_quirk;
1589 dma = &device->common;
1590 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
1591 dma->device_issue_pending = ioat2_issue_pending;
1592 dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
1593 dma->device_free_chan_resources = ioat2_free_chan_resources;
1595 dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
1596 dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
1598 device->cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
1600 if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
1601 device->cap &= ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
1603 /* dca is incompatible with raid operations */
1604 if (dca_en && (device->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
1605 device->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
1607 if (device->cap & IOAT_CAP_XOR) {
1608 is_raid_device = true;
1611 dma_cap_set(DMA_XOR, dma->cap_mask);
1612 dma->device_prep_dma_xor = ioat3_prep_xor;
1614 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1615 dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
1618 if (device->cap & IOAT_CAP_PQ) {
1619 is_raid_device = true;
1621 dma->device_prep_dma_pq = ioat3_prep_pq;
1622 dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
1623 dma_cap_set(DMA_PQ, dma->cap_mask);
1624 dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
1626 if (device->cap & IOAT_CAP_RAID16SS) {
1627 dma_set_maxpq(dma, 16, 0);
1629 dma_set_maxpq(dma, 8, 0);
1632 if (!(device->cap & IOAT_CAP_XOR)) {
1633 dma->device_prep_dma_xor = ioat3_prep_pqxor;
1634 dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
1635 dma_cap_set(DMA_XOR, dma->cap_mask);
1636 dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
1638 if (device->cap & IOAT_CAP_RAID16SS) {
1646 dma->device_tx_status = ioat3_tx_status;
1647 device->cleanup_fn = ioat3_cleanup_event;
1648 device->timer_fn = ioat3_timer_event;
1650 /* starting with CB3.3 super extended descriptors are supported */
1651 if (device->cap & IOAT_CAP_RAID16SS) {
1655 /* allocate sw descriptor pool for SED */
1656 device->sed_pool = kmem_cache_create("ioat_sed",
1657 sizeof(struct ioat_sed_ent), 0, 0, NULL);
1658 if (!device->sed_pool)
1661 for (i = 0; i < MAX_SED_POOLS; i++) {
1662 snprintf(pool_name, 14, "ioat_hw%d_sed", i);
1664 /* allocate SED DMA pool */
1665 device->sed_hw_pool[i] = dma_pool_create(pool_name,
1667 SED_SIZE * (i + 1), 64, 0);
1668 if (!device->sed_hw_pool[i])
1669 goto sed_pool_cleanup;
1674 err = ioat_probe(device);
1677 ioat_set_tcp_copy_break(262144);
1679 list_for_each_entry(c, &dma->channels, device_node) {
1680 chan = to_chan_common(c);
1681 writel(IOAT_DMA_DCA_ANY_CPU,
1682 chan->reg_base + IOAT_DCACTRL_OFFSET);
1685 err = ioat_register(device);
1689 ioat_kobject_add(device, &ioat2_ktype);
1692 device->dca = ioat3_dca_init(pdev, device->reg_base);
1697 if (device->sed_pool) {
1699 kmem_cache_destroy(device->sed_pool);
1701 for (i = 0; i < MAX_SED_POOLS; i++)
1702 if (device->sed_hw_pool[i])
1703 dma_pool_destroy(device->sed_hw_pool[i]);
1709 void ioat3_dma_remove(struct ioatdma_device *device)
1711 if (device->sed_pool) {
1713 kmem_cache_destroy(device->sed_pool);
1715 for (i = 0; i < MAX_SED_POOLS; i++)
1716 if (device->sed_hw_pool[i])
1717 dma_pool_destroy(device->sed_hw_pool[i]);