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 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
23 * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions are met:
28 * * Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * * Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
34 * * Neither the name of Intel Corporation nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
42 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
52 * Support routines for v3+ hardware
54 #include <linux/module.h>
55 #include <linux/pci.h>
56 #include <linux/gfp.h>
57 #include <linux/dmaengine.h>
58 #include <linux/dma-mapping.h>
59 #include <linux/prefetch.h>
60 #include "../dmaengine.h"
61 #include "registers.h"
66 extern struct kmem_cache
*ioat3_sed_cache
;
68 /* ioat hardware assumes at least two sources for raid operations */
69 #define src_cnt_to_sw(x) ((x) + 2)
70 #define src_cnt_to_hw(x) ((x) - 2)
71 #define ndest_to_sw(x) ((x) + 1)
72 #define ndest_to_hw(x) ((x) - 1)
73 #define src16_cnt_to_sw(x) ((x) + 9)
74 #define src16_cnt_to_hw(x) ((x) - 9)
76 /* provide a lookup table for setting the source address in the base or
77 * extended descriptor of an xor or pq descriptor
79 static const u8 xor_idx_to_desc
= 0xe0;
80 static const u8 xor_idx_to_field
[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
81 static const u8 pq_idx_to_desc
= 0xf8;
82 static const u8 pq16_idx_to_desc
[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
83 2, 2, 2, 2, 2, 2, 2 };
84 static const u8 pq_idx_to_field
[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
85 static const u8 pq16_idx_to_field
[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
86 0, 1, 2, 3, 4, 5, 6 };
88 static void ioat3_eh(struct ioat2_dma_chan
*ioat
);
90 static void xor_set_src(struct ioat_raw_descriptor
*descs
[2],
91 dma_addr_t addr
, u32 offset
, int idx
)
93 struct ioat_raw_descriptor
*raw
= descs
[xor_idx_to_desc
>> idx
& 1];
95 raw
->field
[xor_idx_to_field
[idx
]] = addr
+ offset
;
98 static dma_addr_t
pq_get_src(struct ioat_raw_descriptor
*descs
[2], int idx
)
100 struct ioat_raw_descriptor
*raw
= descs
[pq_idx_to_desc
>> idx
& 1];
102 return raw
->field
[pq_idx_to_field
[idx
]];
105 static dma_addr_t
pq16_get_src(struct ioat_raw_descriptor
*desc
[3], int idx
)
107 struct ioat_raw_descriptor
*raw
= desc
[pq16_idx_to_desc
[idx
]];
109 return raw
->field
[pq16_idx_to_field
[idx
]];
112 static void pq_set_src(struct ioat_raw_descriptor
*descs
[2],
113 dma_addr_t addr
, u32 offset
, u8 coef
, int idx
)
115 struct ioat_pq_descriptor
*pq
= (struct ioat_pq_descriptor
*) descs
[0];
116 struct ioat_raw_descriptor
*raw
= descs
[pq_idx_to_desc
>> idx
& 1];
118 raw
->field
[pq_idx_to_field
[idx
]] = addr
+ offset
;
119 pq
->coef
[idx
] = coef
;
122 static bool is_jf_ioat(struct pci_dev
*pdev
)
124 switch (pdev
->device
) {
125 case PCI_DEVICE_ID_INTEL_IOAT_JSF0
:
126 case PCI_DEVICE_ID_INTEL_IOAT_JSF1
:
127 case PCI_DEVICE_ID_INTEL_IOAT_JSF2
:
128 case PCI_DEVICE_ID_INTEL_IOAT_JSF3
:
129 case PCI_DEVICE_ID_INTEL_IOAT_JSF4
:
130 case PCI_DEVICE_ID_INTEL_IOAT_JSF5
:
131 case PCI_DEVICE_ID_INTEL_IOAT_JSF6
:
132 case PCI_DEVICE_ID_INTEL_IOAT_JSF7
:
133 case PCI_DEVICE_ID_INTEL_IOAT_JSF8
:
134 case PCI_DEVICE_ID_INTEL_IOAT_JSF9
:
141 static bool is_snb_ioat(struct pci_dev
*pdev
)
143 switch (pdev
->device
) {
144 case PCI_DEVICE_ID_INTEL_IOAT_SNB0
:
145 case PCI_DEVICE_ID_INTEL_IOAT_SNB1
:
146 case PCI_DEVICE_ID_INTEL_IOAT_SNB2
:
147 case PCI_DEVICE_ID_INTEL_IOAT_SNB3
:
148 case PCI_DEVICE_ID_INTEL_IOAT_SNB4
:
149 case PCI_DEVICE_ID_INTEL_IOAT_SNB5
:
150 case PCI_DEVICE_ID_INTEL_IOAT_SNB6
:
151 case PCI_DEVICE_ID_INTEL_IOAT_SNB7
:
152 case PCI_DEVICE_ID_INTEL_IOAT_SNB8
:
153 case PCI_DEVICE_ID_INTEL_IOAT_SNB9
:
160 static bool is_ivb_ioat(struct pci_dev
*pdev
)
162 switch (pdev
->device
) {
163 case PCI_DEVICE_ID_INTEL_IOAT_IVB0
:
164 case PCI_DEVICE_ID_INTEL_IOAT_IVB1
:
165 case PCI_DEVICE_ID_INTEL_IOAT_IVB2
:
166 case PCI_DEVICE_ID_INTEL_IOAT_IVB3
:
167 case PCI_DEVICE_ID_INTEL_IOAT_IVB4
:
168 case PCI_DEVICE_ID_INTEL_IOAT_IVB5
:
169 case PCI_DEVICE_ID_INTEL_IOAT_IVB6
:
170 case PCI_DEVICE_ID_INTEL_IOAT_IVB7
:
171 case PCI_DEVICE_ID_INTEL_IOAT_IVB8
:
172 case PCI_DEVICE_ID_INTEL_IOAT_IVB9
:
180 static bool is_hsw_ioat(struct pci_dev
*pdev
)
182 switch (pdev
->device
) {
183 case PCI_DEVICE_ID_INTEL_IOAT_HSW0
:
184 case PCI_DEVICE_ID_INTEL_IOAT_HSW1
:
185 case PCI_DEVICE_ID_INTEL_IOAT_HSW2
:
186 case PCI_DEVICE_ID_INTEL_IOAT_HSW3
:
187 case PCI_DEVICE_ID_INTEL_IOAT_HSW4
:
188 case PCI_DEVICE_ID_INTEL_IOAT_HSW5
:
189 case PCI_DEVICE_ID_INTEL_IOAT_HSW6
:
190 case PCI_DEVICE_ID_INTEL_IOAT_HSW7
:
191 case PCI_DEVICE_ID_INTEL_IOAT_HSW8
:
192 case PCI_DEVICE_ID_INTEL_IOAT_HSW9
:
200 static bool is_xeon_cb32(struct pci_dev
*pdev
)
202 return is_jf_ioat(pdev
) || is_snb_ioat(pdev
) || is_ivb_ioat(pdev
) ||
206 static bool is_bwd_ioat(struct pci_dev
*pdev
)
208 switch (pdev
->device
) {
209 case PCI_DEVICE_ID_INTEL_IOAT_BWD0
:
210 case PCI_DEVICE_ID_INTEL_IOAT_BWD1
:
211 case PCI_DEVICE_ID_INTEL_IOAT_BWD2
:
212 case PCI_DEVICE_ID_INTEL_IOAT_BWD3
:
213 /* even though not Atom, BDX-DE has same DMA silicon */
214 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0
:
215 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1
:
216 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2
:
217 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3
:
224 static bool is_bwd_noraid(struct pci_dev
*pdev
)
226 switch (pdev
->device
) {
227 case PCI_DEVICE_ID_INTEL_IOAT_BWD2
:
228 case PCI_DEVICE_ID_INTEL_IOAT_BWD3
:
229 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0
:
230 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1
:
231 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2
:
232 case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3
:
240 static void pq16_set_src(struct ioat_raw_descriptor
*desc
[3],
241 dma_addr_t addr
, u32 offset
, u8 coef
, unsigned idx
)
243 struct ioat_pq_descriptor
*pq
= (struct ioat_pq_descriptor
*)desc
[0];
244 struct ioat_pq16a_descriptor
*pq16
=
245 (struct ioat_pq16a_descriptor
*)desc
[1];
246 struct ioat_raw_descriptor
*raw
= desc
[pq16_idx_to_desc
[idx
]];
248 raw
->field
[pq16_idx_to_field
[idx
]] = addr
+ offset
;
251 pq
->coef
[idx
] = coef
;
253 pq16
->coef
[idx
- 8] = coef
;
256 static struct ioat_sed_ent
*
257 ioat3_alloc_sed(struct ioatdma_device
*device
, unsigned int hw_pool
)
259 struct ioat_sed_ent
*sed
;
260 gfp_t flags
= __GFP_ZERO
| GFP_ATOMIC
;
262 sed
= kmem_cache_alloc(ioat3_sed_cache
, flags
);
266 sed
->hw_pool
= hw_pool
;
267 sed
->hw
= dma_pool_alloc(device
->sed_hw_pool
[hw_pool
],
270 kmem_cache_free(ioat3_sed_cache
, sed
);
277 static void ioat3_free_sed(struct ioatdma_device
*device
, struct ioat_sed_ent
*sed
)
282 dma_pool_free(device
->sed_hw_pool
[sed
->hw_pool
], sed
->hw
, sed
->dma
);
283 kmem_cache_free(ioat3_sed_cache
, sed
);
286 static bool desc_has_ext(struct ioat_ring_ent
*desc
)
288 struct ioat_dma_descriptor
*hw
= desc
->hw
;
290 if (hw
->ctl_f
.op
== IOAT_OP_XOR
||
291 hw
->ctl_f
.op
== IOAT_OP_XOR_VAL
) {
292 struct ioat_xor_descriptor
*xor = desc
->xor;
294 if (src_cnt_to_sw(xor->ctl_f
.src_cnt
) > 5)
296 } else if (hw
->ctl_f
.op
== IOAT_OP_PQ
||
297 hw
->ctl_f
.op
== IOAT_OP_PQ_VAL
) {
298 struct ioat_pq_descriptor
*pq
= desc
->pq
;
300 if (src_cnt_to_sw(pq
->ctl_f
.src_cnt
) > 3)
307 static u64
ioat3_get_current_completion(struct ioat_chan_common
*chan
)
312 completion
= *chan
->completion
;
313 phys_complete
= ioat_chansts_to_addr(completion
);
315 dev_dbg(to_dev(chan
), "%s: phys_complete: %#llx\n", __func__
,
316 (unsigned long long) phys_complete
);
318 return phys_complete
;
321 static bool ioat3_cleanup_preamble(struct ioat_chan_common
*chan
,
324 *phys_complete
= ioat3_get_current_completion(chan
);
325 if (*phys_complete
== chan
->last_completion
)
328 clear_bit(IOAT_COMPLETION_ACK
, &chan
->state
);
329 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
335 desc_get_errstat(struct ioat2_dma_chan
*ioat
, struct ioat_ring_ent
*desc
)
337 struct ioat_dma_descriptor
*hw
= desc
->hw
;
339 switch (hw
->ctl_f
.op
) {
341 case IOAT_OP_PQ_VAL_16S
:
343 struct ioat_pq_descriptor
*pq
= desc
->pq
;
345 /* check if there's error written */
346 if (!pq
->dwbes_f
.wbes
)
349 /* need to set a chanerr var for checking to clear later */
351 if (pq
->dwbes_f
.p_val_err
)
352 *desc
->result
|= SUM_CHECK_P_RESULT
;
354 if (pq
->dwbes_f
.q_val_err
)
355 *desc
->result
|= SUM_CHECK_Q_RESULT
;
365 * __cleanup - reclaim used descriptors
366 * @ioat: channel (ring) to clean
368 * The difference from the dma_v2.c __cleanup() is that this routine
369 * handles extended descriptors and dma-unmapping raid operations.
371 static void __cleanup(struct ioat2_dma_chan
*ioat
, dma_addr_t phys_complete
)
373 struct ioat_chan_common
*chan
= &ioat
->base
;
374 struct ioatdma_device
*device
= chan
->device
;
375 struct ioat_ring_ent
*desc
;
376 bool seen_current
= false;
377 int idx
= ioat
->tail
, i
;
380 dev_dbg(to_dev(chan
), "%s: head: %#x tail: %#x issued: %#x\n",
381 __func__
, ioat
->head
, ioat
->tail
, ioat
->issued
);
384 * At restart of the channel, the completion address and the
385 * channel status will be 0 due to starting a new chain. Since
386 * it's new chain and the first descriptor "fails", there is
387 * nothing to clean up. We do not want to reap the entire submitted
388 * chain due to this 0 address value and then BUG.
393 active
= ioat2_ring_active(ioat
);
394 for (i
= 0; i
< active
&& !seen_current
; i
++) {
395 struct dma_async_tx_descriptor
*tx
;
397 smp_read_barrier_depends();
398 prefetch(ioat2_get_ring_ent(ioat
, idx
+ i
+ 1));
399 desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
400 dump_desc_dbg(ioat
, desc
);
402 /* set err stat if we are using dwbes */
403 if (device
->cap
& IOAT_CAP_DWBES
)
404 desc_get_errstat(ioat
, desc
);
408 dma_cookie_complete(tx
);
409 dma_descriptor_unmap(tx
);
411 tx
->callback(tx
->callback_param
);
416 if (tx
->phys
== phys_complete
)
419 /* skip extended descriptors */
420 if (desc_has_ext(desc
)) {
421 BUG_ON(i
+ 1 >= active
);
425 /* cleanup super extended descriptors */
427 ioat3_free_sed(device
, desc
->sed
);
431 smp_mb(); /* finish all descriptor reads before incrementing tail */
432 ioat
->tail
= idx
+ i
;
433 BUG_ON(active
&& !seen_current
); /* no active descs have written a completion? */
434 chan
->last_completion
= phys_complete
;
436 if (active
- i
== 0) {
437 dev_dbg(to_dev(chan
), "%s: cancel completion timeout\n",
439 clear_bit(IOAT_COMPLETION_PENDING
, &chan
->state
);
440 mod_timer(&chan
->timer
, jiffies
+ IDLE_TIMEOUT
);
442 /* 5 microsecond delay per pending descriptor */
443 writew(min((5 * (active
- i
)), IOAT_INTRDELAY_MASK
),
444 chan
->device
->reg_base
+ IOAT_INTRDELAY_OFFSET
);
447 static void ioat3_cleanup(struct ioat2_dma_chan
*ioat
)
449 struct ioat_chan_common
*chan
= &ioat
->base
;
452 spin_lock_bh(&chan
->cleanup_lock
);
454 if (ioat3_cleanup_preamble(chan
, &phys_complete
))
455 __cleanup(ioat
, phys_complete
);
457 if (is_ioat_halted(*chan
->completion
)) {
458 u32 chanerr
= readl(chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
460 if (chanerr
& IOAT_CHANERR_HANDLE_MASK
) {
461 mod_timer(&chan
->timer
, jiffies
+ IDLE_TIMEOUT
);
466 spin_unlock_bh(&chan
->cleanup_lock
);
469 static void ioat3_cleanup_event(unsigned long data
)
471 struct ioat2_dma_chan
*ioat
= to_ioat2_chan((void *) data
);
472 struct ioat_chan_common
*chan
= &ioat
->base
;
475 if (!test_bit(IOAT_RUN
, &chan
->state
))
477 writew(IOAT_CHANCTRL_RUN
, ioat
->base
.reg_base
+ IOAT_CHANCTRL_OFFSET
);
480 static void ioat3_restart_channel(struct ioat2_dma_chan
*ioat
)
482 struct ioat_chan_common
*chan
= &ioat
->base
;
485 ioat2_quiesce(chan
, 0);
486 if (ioat3_cleanup_preamble(chan
, &phys_complete
))
487 __cleanup(ioat
, phys_complete
);
489 __ioat2_restart_chan(ioat
);
492 static void ioat3_eh(struct ioat2_dma_chan
*ioat
)
494 struct ioat_chan_common
*chan
= &ioat
->base
;
495 struct pci_dev
*pdev
= to_pdev(chan
);
496 struct ioat_dma_descriptor
*hw
;
497 struct dma_async_tx_descriptor
*tx
;
499 struct ioat_ring_ent
*desc
;
504 /* cleanup so tail points to descriptor that caused the error */
505 if (ioat3_cleanup_preamble(chan
, &phys_complete
))
506 __cleanup(ioat
, phys_complete
);
508 chanerr
= readl(chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
509 pci_read_config_dword(pdev
, IOAT_PCI_CHANERR_INT_OFFSET
, &chanerr_int
);
511 dev_dbg(to_dev(chan
), "%s: error = %x:%x\n",
512 __func__
, chanerr
, chanerr_int
);
514 desc
= ioat2_get_ring_ent(ioat
, ioat
->tail
);
516 dump_desc_dbg(ioat
, desc
);
518 switch (hw
->ctl_f
.op
) {
519 case IOAT_OP_XOR_VAL
:
520 if (chanerr
& IOAT_CHANERR_XOR_P_OR_CRC_ERR
) {
521 *desc
->result
|= SUM_CHECK_P_RESULT
;
522 err_handled
|= IOAT_CHANERR_XOR_P_OR_CRC_ERR
;
526 case IOAT_OP_PQ_VAL_16S
:
527 if (chanerr
& IOAT_CHANERR_XOR_P_OR_CRC_ERR
) {
528 *desc
->result
|= SUM_CHECK_P_RESULT
;
529 err_handled
|= IOAT_CHANERR_XOR_P_OR_CRC_ERR
;
531 if (chanerr
& IOAT_CHANERR_XOR_Q_ERR
) {
532 *desc
->result
|= SUM_CHECK_Q_RESULT
;
533 err_handled
|= IOAT_CHANERR_XOR_Q_ERR
;
538 /* fault on unhandled error or spurious halt */
539 if (chanerr
^ err_handled
|| chanerr
== 0) {
540 dev_err(to_dev(chan
), "%s: fatal error (%x:%x)\n",
541 __func__
, chanerr
, err_handled
);
543 } else { /* cleanup the faulty descriptor */
546 dma_cookie_complete(tx
);
547 dma_descriptor_unmap(tx
);
549 tx
->callback(tx
->callback_param
);
555 writel(chanerr
, chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
556 pci_write_config_dword(pdev
, IOAT_PCI_CHANERR_INT_OFFSET
, chanerr_int
);
558 /* mark faulting descriptor as complete */
559 *chan
->completion
= desc
->txd
.phys
;
561 spin_lock_bh(&ioat
->prep_lock
);
562 ioat3_restart_channel(ioat
);
563 spin_unlock_bh(&ioat
->prep_lock
);
566 static void check_active(struct ioat2_dma_chan
*ioat
)
568 struct ioat_chan_common
*chan
= &ioat
->base
;
570 if (ioat2_ring_active(ioat
)) {
571 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
575 if (test_and_clear_bit(IOAT_CHAN_ACTIVE
, &chan
->state
))
576 mod_timer(&chan
->timer
, jiffies
+ IDLE_TIMEOUT
);
577 else if (ioat
->alloc_order
> ioat_get_alloc_order()) {
578 /* if the ring is idle, empty, and oversized try to step
581 reshape_ring(ioat
, ioat
->alloc_order
- 1);
583 /* keep shrinking until we get back to our minimum
586 if (ioat
->alloc_order
> ioat_get_alloc_order())
587 mod_timer(&chan
->timer
, jiffies
+ IDLE_TIMEOUT
);
592 static void ioat3_timer_event(unsigned long data
)
594 struct ioat2_dma_chan
*ioat
= to_ioat2_chan((void *) data
);
595 struct ioat_chan_common
*chan
= &ioat
->base
;
596 dma_addr_t phys_complete
;
599 status
= ioat_chansts(chan
);
601 /* when halted due to errors check for channel
602 * programming errors before advancing the completion state
604 if (is_ioat_halted(status
)) {
607 chanerr
= readl(chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
608 dev_err(to_dev(chan
), "%s: Channel halted (%x)\n",
610 if (test_bit(IOAT_RUN
, &chan
->state
))
611 BUG_ON(is_ioat_bug(chanerr
));
612 else /* we never got off the ground */
616 /* if we haven't made progress and we have already
617 * acknowledged a pending completion once, then be more
618 * forceful with a restart
620 spin_lock_bh(&chan
->cleanup_lock
);
621 if (ioat_cleanup_preamble(chan
, &phys_complete
))
622 __cleanup(ioat
, phys_complete
);
623 else if (test_bit(IOAT_COMPLETION_ACK
, &chan
->state
)) {
624 spin_lock_bh(&ioat
->prep_lock
);
625 ioat3_restart_channel(ioat
);
626 spin_unlock_bh(&ioat
->prep_lock
);
627 spin_unlock_bh(&chan
->cleanup_lock
);
630 set_bit(IOAT_COMPLETION_ACK
, &chan
->state
);
631 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
635 if (ioat2_ring_active(ioat
))
636 mod_timer(&chan
->timer
, jiffies
+ COMPLETION_TIMEOUT
);
638 spin_lock_bh(&ioat
->prep_lock
);
640 spin_unlock_bh(&ioat
->prep_lock
);
642 spin_unlock_bh(&chan
->cleanup_lock
);
645 static enum dma_status
646 ioat3_tx_status(struct dma_chan
*c
, dma_cookie_t cookie
,
647 struct dma_tx_state
*txstate
)
649 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
652 ret
= dma_cookie_status(c
, cookie
, txstate
);
653 if (ret
== DMA_COMPLETE
)
658 return dma_cookie_status(c
, cookie
, txstate
);
661 static struct dma_async_tx_descriptor
*
662 __ioat3_prep_xor_lock(struct dma_chan
*c
, enum sum_check_flags
*result
,
663 dma_addr_t dest
, dma_addr_t
*src
, unsigned int src_cnt
,
664 size_t len
, unsigned long flags
)
666 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
667 struct ioat_ring_ent
*compl_desc
;
668 struct ioat_ring_ent
*desc
;
669 struct ioat_ring_ent
*ext
;
670 size_t total_len
= len
;
671 struct ioat_xor_descriptor
*xor;
672 struct ioat_xor_ext_descriptor
*xor_ex
= NULL
;
673 struct ioat_dma_descriptor
*hw
;
674 int num_descs
, with_ext
, idx
, i
;
676 u8 op
= result
? IOAT_OP_XOR_VAL
: IOAT_OP_XOR
;
680 num_descs
= ioat2_xferlen_to_descs(ioat
, len
);
681 /* we need 2x the number of descriptors to cover greater than 5
690 /* completion writes from the raid engine may pass completion
691 * writes from the legacy engine, so we need one extra null
692 * (legacy) descriptor to ensure all completion writes arrive in
695 if (likely(num_descs
) && ioat2_check_space_lock(ioat
, num_descs
+1) == 0)
701 struct ioat_raw_descriptor
*descs
[2];
702 size_t xfer_size
= min_t(size_t, len
, 1 << ioat
->xfercap_log
);
705 desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
708 /* save a branch by unconditionally retrieving the
709 * extended descriptor xor_set_src() knows to not write
710 * to it in the single descriptor case
712 ext
= ioat2_get_ring_ent(ioat
, idx
+ i
+ 1);
713 xor_ex
= ext
->xor_ex
;
715 descs
[0] = (struct ioat_raw_descriptor
*) xor;
716 descs
[1] = (struct ioat_raw_descriptor
*) xor_ex
;
717 for (s
= 0; s
< src_cnt
; s
++)
718 xor_set_src(descs
, src
[s
], offset
, s
);
719 xor->size
= xfer_size
;
720 xor->dst_addr
= dest
+ offset
;
723 xor->ctl_f
.src_cnt
= src_cnt_to_hw(src_cnt
);
727 dump_desc_dbg(ioat
, desc
);
728 } while ((i
+= 1 + with_ext
) < num_descs
);
730 /* last xor descriptor carries the unmap parameters and fence bit */
731 desc
->txd
.flags
= flags
;
732 desc
->len
= total_len
;
734 desc
->result
= result
;
735 xor->ctl_f
.fence
= !!(flags
& DMA_PREP_FENCE
);
737 /* completion descriptor carries interrupt bit */
738 compl_desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
739 compl_desc
->txd
.flags
= flags
& DMA_PREP_INTERRUPT
;
743 hw
->ctl_f
.int_en
= !!(flags
& DMA_PREP_INTERRUPT
);
744 hw
->ctl_f
.compl_write
= 1;
745 hw
->size
= NULL_DESC_BUFFER_SIZE
;
746 dump_desc_dbg(ioat
, compl_desc
);
748 /* we leave the channel locked to ensure in order submission */
749 return &compl_desc
->txd
;
752 static struct dma_async_tx_descriptor
*
753 ioat3_prep_xor(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t
*src
,
754 unsigned int src_cnt
, size_t len
, unsigned long flags
)
756 return __ioat3_prep_xor_lock(chan
, NULL
, dest
, src
, src_cnt
, len
, flags
);
759 static struct dma_async_tx_descriptor
*
760 ioat3_prep_xor_val(struct dma_chan
*chan
, dma_addr_t
*src
,
761 unsigned int src_cnt
, size_t len
,
762 enum sum_check_flags
*result
, unsigned long flags
)
764 /* the cleanup routine only sets bits on validate failure, it
765 * does not clear bits on validate success... so clear it here
769 return __ioat3_prep_xor_lock(chan
, result
, src
[0], &src
[1],
770 src_cnt
- 1, len
, flags
);
774 dump_pq_desc_dbg(struct ioat2_dma_chan
*ioat
, struct ioat_ring_ent
*desc
, struct ioat_ring_ent
*ext
)
776 struct device
*dev
= to_dev(&ioat
->base
);
777 struct ioat_pq_descriptor
*pq
= desc
->pq
;
778 struct ioat_pq_ext_descriptor
*pq_ex
= ext
? ext
->pq_ex
: NULL
;
779 struct ioat_raw_descriptor
*descs
[] = { (void *) pq
, (void *) pq_ex
};
780 int src_cnt
= src_cnt_to_sw(pq
->ctl_f
.src_cnt
);
783 dev_dbg(dev
, "desc[%d]: (%#llx->%#llx) flags: %#x"
784 " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
786 desc_id(desc
), (unsigned long long) desc
->txd
.phys
,
787 (unsigned long long) (pq_ex
? pq_ex
->next
: pq
->next
),
788 desc
->txd
.flags
, pq
->size
, pq
->ctl
, pq
->ctl_f
.op
, pq
->ctl_f
.int_en
,
789 pq
->ctl_f
.compl_write
,
790 pq
->ctl_f
.p_disable
? "" : "p", pq
->ctl_f
.q_disable
? "" : "q",
792 for (i
= 0; i
< src_cnt
; i
++)
793 dev_dbg(dev
, "\tsrc[%d]: %#llx coef: %#x\n", i
,
794 (unsigned long long) pq_get_src(descs
, i
), pq
->coef
[i
]);
795 dev_dbg(dev
, "\tP: %#llx\n", pq
->p_addr
);
796 dev_dbg(dev
, "\tQ: %#llx\n", pq
->q_addr
);
797 dev_dbg(dev
, "\tNEXT: %#llx\n", pq
->next
);
800 static void dump_pq16_desc_dbg(struct ioat2_dma_chan
*ioat
,
801 struct ioat_ring_ent
*desc
)
803 struct device
*dev
= to_dev(&ioat
->base
);
804 struct ioat_pq_descriptor
*pq
= desc
->pq
;
805 struct ioat_raw_descriptor
*descs
[] = { (void *)pq
,
808 int src_cnt
= src16_cnt_to_sw(pq
->ctl_f
.src_cnt
);
812 descs
[1] = (void *)desc
->sed
->hw
;
813 descs
[2] = (void *)desc
->sed
->hw
+ 64;
816 dev_dbg(dev
, "desc[%d]: (%#llx->%#llx) flags: %#x"
817 " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
819 desc_id(desc
), (unsigned long long) desc
->txd
.phys
,
820 (unsigned long long) pq
->next
,
821 desc
->txd
.flags
, pq
->size
, pq
->ctl
,
822 pq
->ctl_f
.op
, pq
->ctl_f
.int_en
,
823 pq
->ctl_f
.compl_write
,
824 pq
->ctl_f
.p_disable
? "" : "p", pq
->ctl_f
.q_disable
? "" : "q",
826 for (i
= 0; i
< src_cnt
; i
++) {
827 dev_dbg(dev
, "\tsrc[%d]: %#llx coef: %#x\n", i
,
828 (unsigned long long) pq16_get_src(descs
, i
),
831 dev_dbg(dev
, "\tP: %#llx\n", pq
->p_addr
);
832 dev_dbg(dev
, "\tQ: %#llx\n", pq
->q_addr
);
835 static struct dma_async_tx_descriptor
*
836 __ioat3_prep_pq_lock(struct dma_chan
*c
, enum sum_check_flags
*result
,
837 const dma_addr_t
*dst
, const dma_addr_t
*src
,
838 unsigned int src_cnt
, const unsigned char *scf
,
839 size_t len
, unsigned long flags
)
841 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
842 struct ioat_chan_common
*chan
= &ioat
->base
;
843 struct ioatdma_device
*device
= chan
->device
;
844 struct ioat_ring_ent
*compl_desc
;
845 struct ioat_ring_ent
*desc
;
846 struct ioat_ring_ent
*ext
;
847 size_t total_len
= len
;
848 struct ioat_pq_descriptor
*pq
;
849 struct ioat_pq_ext_descriptor
*pq_ex
= NULL
;
850 struct ioat_dma_descriptor
*hw
;
852 u8 op
= result
? IOAT_OP_PQ_VAL
: IOAT_OP_PQ
;
853 int i
, s
, idx
, with_ext
, num_descs
;
854 int cb32
= (device
->version
< IOAT_VER_3_3
) ? 1 : 0;
856 dev_dbg(to_dev(chan
), "%s\n", __func__
);
857 /* the engine requires at least two sources (we provide
858 * at least 1 implied source in the DMA_PREP_CONTINUE case)
860 BUG_ON(src_cnt
+ dmaf_continue(flags
) < 2);
862 num_descs
= ioat2_xferlen_to_descs(ioat
, len
);
863 /* we need 2x the number of descriptors to cover greater than 3
864 * sources (we need 1 extra source in the q-only continuation
865 * case and 3 extra sources in the p+q continuation case.
867 if (src_cnt
+ dmaf_p_disabled_continue(flags
) > 3 ||
868 (dmaf_continue(flags
) && !dmaf_p_disabled_continue(flags
))) {
874 /* completion writes from the raid engine may pass completion
875 * writes from the legacy engine, so we need one extra null
876 * (legacy) descriptor to ensure all completion writes arrive in
879 if (likely(num_descs
) &&
880 ioat2_check_space_lock(ioat
, num_descs
+ cb32
) == 0)
886 struct ioat_raw_descriptor
*descs
[2];
887 size_t xfer_size
= min_t(size_t, len
, 1 << ioat
->xfercap_log
);
889 desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
892 /* save a branch by unconditionally retrieving the
893 * extended descriptor pq_set_src() knows to not write
894 * to it in the single descriptor case
896 ext
= ioat2_get_ring_ent(ioat
, idx
+ i
+ with_ext
);
899 descs
[0] = (struct ioat_raw_descriptor
*) pq
;
900 descs
[1] = (struct ioat_raw_descriptor
*) pq_ex
;
902 for (s
= 0; s
< src_cnt
; s
++)
903 pq_set_src(descs
, src
[s
], offset
, scf
[s
], s
);
905 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
906 if (dmaf_p_disabled_continue(flags
))
907 pq_set_src(descs
, dst
[1], offset
, 1, s
++);
908 else if (dmaf_continue(flags
)) {
909 pq_set_src(descs
, dst
[0], offset
, 0, s
++);
910 pq_set_src(descs
, dst
[1], offset
, 1, s
++);
911 pq_set_src(descs
, dst
[1], offset
, 0, s
++);
913 pq
->size
= xfer_size
;
914 pq
->p_addr
= dst
[0] + offset
;
915 pq
->q_addr
= dst
[1] + offset
;
918 /* we turn on descriptor write back error status */
919 if (device
->cap
& IOAT_CAP_DWBES
)
920 pq
->ctl_f
.wb_en
= result
? 1 : 0;
921 pq
->ctl_f
.src_cnt
= src_cnt_to_hw(s
);
922 pq
->ctl_f
.p_disable
= !!(flags
& DMA_PREP_PQ_DISABLE_P
);
923 pq
->ctl_f
.q_disable
= !!(flags
& DMA_PREP_PQ_DISABLE_Q
);
927 } while ((i
+= 1 + with_ext
) < num_descs
);
929 /* last pq descriptor carries the unmap parameters and fence bit */
930 desc
->txd
.flags
= flags
;
931 desc
->len
= total_len
;
933 desc
->result
= result
;
934 pq
->ctl_f
.fence
= !!(flags
& DMA_PREP_FENCE
);
935 dump_pq_desc_dbg(ioat
, desc
, ext
);
938 pq
->ctl_f
.int_en
= !!(flags
& DMA_PREP_INTERRUPT
);
939 pq
->ctl_f
.compl_write
= 1;
942 /* completion descriptor carries interrupt bit */
943 compl_desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
944 compl_desc
->txd
.flags
= flags
& DMA_PREP_INTERRUPT
;
948 hw
->ctl_f
.int_en
= !!(flags
& DMA_PREP_INTERRUPT
);
949 hw
->ctl_f
.compl_write
= 1;
950 hw
->size
= NULL_DESC_BUFFER_SIZE
;
951 dump_desc_dbg(ioat
, compl_desc
);
955 /* we leave the channel locked to ensure in order submission */
956 return &compl_desc
->txd
;
959 static struct dma_async_tx_descriptor
*
960 __ioat3_prep_pq16_lock(struct dma_chan
*c
, enum sum_check_flags
*result
,
961 const dma_addr_t
*dst
, const dma_addr_t
*src
,
962 unsigned int src_cnt
, const unsigned char *scf
,
963 size_t len
, unsigned long flags
)
965 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
966 struct ioat_chan_common
*chan
= &ioat
->base
;
967 struct ioatdma_device
*device
= chan
->device
;
968 struct ioat_ring_ent
*desc
;
969 size_t total_len
= len
;
970 struct ioat_pq_descriptor
*pq
;
973 int i
, s
, idx
, num_descs
;
975 /* this function is only called with 9-16 sources */
976 op
= result
? IOAT_OP_PQ_VAL_16S
: IOAT_OP_PQ_16S
;
978 dev_dbg(to_dev(chan
), "%s\n", __func__
);
980 num_descs
= ioat2_xferlen_to_descs(ioat
, len
);
983 * 16 source pq is only available on cb3.3 and has no completion
986 if (num_descs
&& ioat2_check_space_lock(ioat
, num_descs
) == 0)
994 struct ioat_raw_descriptor
*descs
[4];
995 size_t xfer_size
= min_t(size_t, len
, 1 << ioat
->xfercap_log
);
997 desc
= ioat2_get_ring_ent(ioat
, idx
+ i
);
1000 descs
[0] = (struct ioat_raw_descriptor
*) pq
;
1002 desc
->sed
= ioat3_alloc_sed(device
, (src_cnt
-2) >> 3);
1004 dev_err(to_dev(chan
),
1005 "%s: no free sed entries\n", __func__
);
1009 pq
->sed_addr
= desc
->sed
->dma
;
1010 desc
->sed
->parent
= desc
;
1012 descs
[1] = (struct ioat_raw_descriptor
*)desc
->sed
->hw
;
1013 descs
[2] = (void *)descs
[1] + 64;
1015 for (s
= 0; s
< src_cnt
; s
++)
1016 pq16_set_src(descs
, src
[s
], offset
, scf
[s
], s
);
1018 /* see the comment for dma_maxpq in include/linux/dmaengine.h */
1019 if (dmaf_p_disabled_continue(flags
))
1020 pq16_set_src(descs
, dst
[1], offset
, 1, s
++);
1021 else if (dmaf_continue(flags
)) {
1022 pq16_set_src(descs
, dst
[0], offset
, 0, s
++);
1023 pq16_set_src(descs
, dst
[1], offset
, 1, s
++);
1024 pq16_set_src(descs
, dst
[1], offset
, 0, s
++);
1027 pq
->size
= xfer_size
;
1028 pq
->p_addr
= dst
[0] + offset
;
1029 pq
->q_addr
= dst
[1] + offset
;
1032 pq
->ctl_f
.src_cnt
= src16_cnt_to_hw(s
);
1033 /* we turn on descriptor write back error status */
1034 if (device
->cap
& IOAT_CAP_DWBES
)
1035 pq
->ctl_f
.wb_en
= result
? 1 : 0;
1036 pq
->ctl_f
.p_disable
= !!(flags
& DMA_PREP_PQ_DISABLE_P
);
1037 pq
->ctl_f
.q_disable
= !!(flags
& DMA_PREP_PQ_DISABLE_Q
);
1040 offset
+= xfer_size
;
1041 } while (++i
< num_descs
);
1043 /* last pq descriptor carries the unmap parameters and fence bit */
1044 desc
->txd
.flags
= flags
;
1045 desc
->len
= total_len
;
1047 desc
->result
= result
;
1048 pq
->ctl_f
.fence
= !!(flags
& DMA_PREP_FENCE
);
1050 /* with cb3.3 we should be able to do completion w/o a null desc */
1051 pq
->ctl_f
.int_en
= !!(flags
& DMA_PREP_INTERRUPT
);
1052 pq
->ctl_f
.compl_write
= 1;
1054 dump_pq16_desc_dbg(ioat
, desc
);
1056 /* we leave the channel locked to ensure in order submission */
1060 static int src_cnt_flags(unsigned int src_cnt
, unsigned long flags
)
1062 if (dmaf_p_disabled_continue(flags
))
1064 else if (dmaf_continue(flags
))
1070 static struct dma_async_tx_descriptor
*
1071 ioat3_prep_pq(struct dma_chan
*chan
, dma_addr_t
*dst
, dma_addr_t
*src
,
1072 unsigned int src_cnt
, const unsigned char *scf
, size_t len
,
1073 unsigned long flags
)
1075 /* specify valid address for disabled result */
1076 if (flags
& DMA_PREP_PQ_DISABLE_P
)
1078 if (flags
& DMA_PREP_PQ_DISABLE_Q
)
1081 /* handle the single source multiply case from the raid6
1084 if ((flags
& DMA_PREP_PQ_DISABLE_P
) && src_cnt
== 1) {
1085 dma_addr_t single_source
[2];
1086 unsigned char single_source_coef
[2];
1088 BUG_ON(flags
& DMA_PREP_PQ_DISABLE_Q
);
1089 single_source
[0] = src
[0];
1090 single_source
[1] = src
[0];
1091 single_source_coef
[0] = scf
[0];
1092 single_source_coef
[1] = 0;
1094 return src_cnt_flags(src_cnt
, flags
) > 8 ?
1095 __ioat3_prep_pq16_lock(chan
, NULL
, dst
, single_source
,
1096 2, single_source_coef
, len
,
1098 __ioat3_prep_pq_lock(chan
, NULL
, dst
, single_source
, 2,
1099 single_source_coef
, len
, flags
);
1102 return src_cnt_flags(src_cnt
, flags
) > 8 ?
1103 __ioat3_prep_pq16_lock(chan
, NULL
, dst
, src
, src_cnt
,
1105 __ioat3_prep_pq_lock(chan
, NULL
, dst
, src
, src_cnt
,
1110 static struct dma_async_tx_descriptor
*
1111 ioat3_prep_pq_val(struct dma_chan
*chan
, dma_addr_t
*pq
, dma_addr_t
*src
,
1112 unsigned int src_cnt
, const unsigned char *scf
, size_t len
,
1113 enum sum_check_flags
*pqres
, unsigned long flags
)
1115 /* specify valid address for disabled result */
1116 if (flags
& DMA_PREP_PQ_DISABLE_P
)
1118 if (flags
& DMA_PREP_PQ_DISABLE_Q
)
1121 /* the cleanup routine only sets bits on validate failure, it
1122 * does not clear bits on validate success... so clear it here
1126 return src_cnt_flags(src_cnt
, flags
) > 8 ?
1127 __ioat3_prep_pq16_lock(chan
, pqres
, pq
, src
, src_cnt
, scf
, len
,
1129 __ioat3_prep_pq_lock(chan
, pqres
, pq
, src
, src_cnt
, scf
, len
,
1133 static struct dma_async_tx_descriptor
*
1134 ioat3_prep_pqxor(struct dma_chan
*chan
, dma_addr_t dst
, dma_addr_t
*src
,
1135 unsigned int src_cnt
, size_t len
, unsigned long flags
)
1137 unsigned char scf
[src_cnt
];
1140 memset(scf
, 0, src_cnt
);
1142 flags
|= DMA_PREP_PQ_DISABLE_Q
;
1143 pq
[1] = dst
; /* specify valid address for disabled result */
1145 return src_cnt_flags(src_cnt
, flags
) > 8 ?
1146 __ioat3_prep_pq16_lock(chan
, NULL
, pq
, src
, src_cnt
, scf
, len
,
1148 __ioat3_prep_pq_lock(chan
, NULL
, pq
, src
, src_cnt
, scf
, len
,
1152 static struct dma_async_tx_descriptor
*
1153 ioat3_prep_pqxor_val(struct dma_chan
*chan
, dma_addr_t
*src
,
1154 unsigned int src_cnt
, size_t len
,
1155 enum sum_check_flags
*result
, unsigned long flags
)
1157 unsigned char scf
[src_cnt
];
1160 /* the cleanup routine only sets bits on validate failure, it
1161 * does not clear bits on validate success... so clear it here
1165 memset(scf
, 0, src_cnt
);
1167 flags
|= DMA_PREP_PQ_DISABLE_Q
;
1168 pq
[1] = pq
[0]; /* specify valid address for disabled result */
1170 return src_cnt_flags(src_cnt
, flags
) > 8 ?
1171 __ioat3_prep_pq16_lock(chan
, result
, pq
, &src
[1], src_cnt
- 1,
1173 __ioat3_prep_pq_lock(chan
, result
, pq
, &src
[1], src_cnt
- 1,
1177 static struct dma_async_tx_descriptor
*
1178 ioat3_prep_interrupt_lock(struct dma_chan
*c
, unsigned long flags
)
1180 struct ioat2_dma_chan
*ioat
= to_ioat2_chan(c
);
1181 struct ioat_ring_ent
*desc
;
1182 struct ioat_dma_descriptor
*hw
;
1184 if (ioat2_check_space_lock(ioat
, 1) == 0)
1185 desc
= ioat2_get_ring_ent(ioat
, ioat
->head
);
1192 hw
->ctl_f
.int_en
= 1;
1193 hw
->ctl_f
.fence
= !!(flags
& DMA_PREP_FENCE
);
1194 hw
->ctl_f
.compl_write
= 1;
1195 hw
->size
= NULL_DESC_BUFFER_SIZE
;
1199 desc
->txd
.flags
= flags
;
1202 dump_desc_dbg(ioat
, desc
);
1204 /* we leave the channel locked to ensure in order submission */
1208 static void ioat3_dma_test_callback(void *dma_async_param
)
1210 struct completion
*cmp
= dma_async_param
;
1215 #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
1216 static int ioat_xor_val_self_test(struct ioatdma_device
*device
)
1220 struct page
*xor_srcs
[IOAT_NUM_SRC_TEST
];
1221 struct page
*xor_val_srcs
[IOAT_NUM_SRC_TEST
+ 1];
1222 dma_addr_t dma_srcs
[IOAT_NUM_SRC_TEST
+ 1];
1223 dma_addr_t dest_dma
;
1224 struct dma_async_tx_descriptor
*tx
;
1225 struct dma_chan
*dma_chan
;
1226 dma_cookie_t cookie
;
1231 struct completion cmp
;
1233 struct device
*dev
= &device
->pdev
->dev
;
1234 struct dma_device
*dma
= &device
->common
;
1237 dev_dbg(dev
, "%s\n", __func__
);
1239 if (!dma_has_cap(DMA_XOR
, dma
->cap_mask
))
1242 for (src_idx
= 0; src_idx
< IOAT_NUM_SRC_TEST
; src_idx
++) {
1243 xor_srcs
[src_idx
] = alloc_page(GFP_KERNEL
);
1244 if (!xor_srcs
[src_idx
]) {
1246 __free_page(xor_srcs
[src_idx
]);
1251 dest
= alloc_page(GFP_KERNEL
);
1254 __free_page(xor_srcs
[src_idx
]);
1258 /* Fill in src buffers */
1259 for (src_idx
= 0; src_idx
< IOAT_NUM_SRC_TEST
; src_idx
++) {
1260 u8
*ptr
= page_address(xor_srcs
[src_idx
]);
1261 for (i
= 0; i
< PAGE_SIZE
; i
++)
1262 ptr
[i
] = (1 << src_idx
);
1265 for (src_idx
= 0; src_idx
< IOAT_NUM_SRC_TEST
; src_idx
++)
1266 cmp_byte
^= (u8
) (1 << src_idx
);
1268 cmp_word
= (cmp_byte
<< 24) | (cmp_byte
<< 16) |
1269 (cmp_byte
<< 8) | cmp_byte
;
1271 memset(page_address(dest
), 0, PAGE_SIZE
);
1273 dma_chan
= container_of(dma
->channels
.next
, struct dma_chan
,
1275 if (dma
->device_alloc_chan_resources(dma_chan
) < 1) {
1283 dest_dma
= dma_map_page(dev
, dest
, 0, PAGE_SIZE
, DMA_FROM_DEVICE
);
1284 if (dma_mapping_error(dev
, dest_dma
))
1287 for (i
= 0; i
< IOAT_NUM_SRC_TEST
; i
++)
1288 dma_srcs
[i
] = DMA_ERROR_CODE
;
1289 for (i
= 0; i
< IOAT_NUM_SRC_TEST
; i
++) {
1290 dma_srcs
[i
] = dma_map_page(dev
, xor_srcs
[i
], 0, PAGE_SIZE
,
1292 if (dma_mapping_error(dev
, dma_srcs
[i
]))
1295 tx
= dma
->device_prep_dma_xor(dma_chan
, dest_dma
, dma_srcs
,
1296 IOAT_NUM_SRC_TEST
, PAGE_SIZE
,
1297 DMA_PREP_INTERRUPT
);
1300 dev_err(dev
, "Self-test xor prep failed\n");
1306 init_completion(&cmp
);
1307 tx
->callback
= ioat3_dma_test_callback
;
1308 tx
->callback_param
= &cmp
;
1309 cookie
= tx
->tx_submit(tx
);
1311 dev_err(dev
, "Self-test xor setup failed\n");
1315 dma
->device_issue_pending(dma_chan
);
1317 tmo
= wait_for_completion_timeout(&cmp
, msecs_to_jiffies(3000));
1320 dma
->device_tx_status(dma_chan
, cookie
, NULL
) != DMA_COMPLETE
) {
1321 dev_err(dev
, "Self-test xor timed out\n");
1326 for (i
= 0; i
< IOAT_NUM_SRC_TEST
; i
++)
1327 dma_unmap_page(dev
, dma_srcs
[i
], PAGE_SIZE
, DMA_TO_DEVICE
);
1329 dma_sync_single_for_cpu(dev
, dest_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
1330 for (i
= 0; i
< (PAGE_SIZE
/ sizeof(u32
)); i
++) {
1331 u32
*ptr
= page_address(dest
);
1332 if (ptr
[i
] != cmp_word
) {
1333 dev_err(dev
, "Self-test xor failed compare\n");
1335 goto free_resources
;
1338 dma_sync_single_for_device(dev
, dest_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
1340 dma_unmap_page(dev
, dest_dma
, PAGE_SIZE
, DMA_FROM_DEVICE
);
1342 /* skip validate if the capability is not present */
1343 if (!dma_has_cap(DMA_XOR_VAL
, dma_chan
->device
->cap_mask
))
1344 goto free_resources
;
1346 op
= IOAT_OP_XOR_VAL
;
1348 /* validate the sources with the destintation page */
1349 for (i
= 0; i
< IOAT_NUM_SRC_TEST
; i
++)
1350 xor_val_srcs
[i
] = xor_srcs
[i
];
1351 xor_val_srcs
[i
] = dest
;
1355 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++)
1356 dma_srcs
[i
] = DMA_ERROR_CODE
;
1357 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++) {
1358 dma_srcs
[i
] = dma_map_page(dev
, xor_val_srcs
[i
], 0, PAGE_SIZE
,
1360 if (dma_mapping_error(dev
, dma_srcs
[i
]))
1363 tx
= dma
->device_prep_dma_xor_val(dma_chan
, dma_srcs
,
1364 IOAT_NUM_SRC_TEST
+ 1, PAGE_SIZE
,
1365 &xor_val_result
, DMA_PREP_INTERRUPT
);
1367 dev_err(dev
, "Self-test zero prep failed\n");
1373 init_completion(&cmp
);
1374 tx
->callback
= ioat3_dma_test_callback
;
1375 tx
->callback_param
= &cmp
;
1376 cookie
= tx
->tx_submit(tx
);
1378 dev_err(dev
, "Self-test zero setup failed\n");
1382 dma
->device_issue_pending(dma_chan
);
1384 tmo
= wait_for_completion_timeout(&cmp
, msecs_to_jiffies(3000));
1387 dma
->device_tx_status(dma_chan
, cookie
, NULL
) != DMA_COMPLETE
) {
1388 dev_err(dev
, "Self-test validate timed out\n");
1393 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++)
1394 dma_unmap_page(dev
, dma_srcs
[i
], PAGE_SIZE
, DMA_TO_DEVICE
);
1396 if (xor_val_result
!= 0) {
1397 dev_err(dev
, "Self-test validate failed compare\n");
1399 goto free_resources
;
1402 memset(page_address(dest
), 0, PAGE_SIZE
);
1404 /* test for non-zero parity sum */
1405 op
= IOAT_OP_XOR_VAL
;
1408 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++)
1409 dma_srcs
[i
] = DMA_ERROR_CODE
;
1410 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++) {
1411 dma_srcs
[i
] = dma_map_page(dev
, xor_val_srcs
[i
], 0, PAGE_SIZE
,
1413 if (dma_mapping_error(dev
, dma_srcs
[i
]))
1416 tx
= dma
->device_prep_dma_xor_val(dma_chan
, dma_srcs
,
1417 IOAT_NUM_SRC_TEST
+ 1, PAGE_SIZE
,
1418 &xor_val_result
, DMA_PREP_INTERRUPT
);
1420 dev_err(dev
, "Self-test 2nd zero prep failed\n");
1426 init_completion(&cmp
);
1427 tx
->callback
= ioat3_dma_test_callback
;
1428 tx
->callback_param
= &cmp
;
1429 cookie
= tx
->tx_submit(tx
);
1431 dev_err(dev
, "Self-test 2nd zero setup failed\n");
1435 dma
->device_issue_pending(dma_chan
);
1437 tmo
= wait_for_completion_timeout(&cmp
, msecs_to_jiffies(3000));
1440 dma
->device_tx_status(dma_chan
, cookie
, NULL
) != DMA_COMPLETE
) {
1441 dev_err(dev
, "Self-test 2nd validate timed out\n");
1446 if (xor_val_result
!= SUM_CHECK_P_RESULT
) {
1447 dev_err(dev
, "Self-test validate failed compare\n");
1452 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++)
1453 dma_unmap_page(dev
, dma_srcs
[i
], PAGE_SIZE
, DMA_TO_DEVICE
);
1455 goto free_resources
;
1457 if (op
== IOAT_OP_XOR
) {
1458 if (dest_dma
!= DMA_ERROR_CODE
)
1459 dma_unmap_page(dev
, dest_dma
, PAGE_SIZE
,
1461 for (i
= 0; i
< IOAT_NUM_SRC_TEST
; i
++)
1462 if (dma_srcs
[i
] != DMA_ERROR_CODE
)
1463 dma_unmap_page(dev
, dma_srcs
[i
], PAGE_SIZE
,
1465 } else if (op
== IOAT_OP_XOR_VAL
) {
1466 for (i
= 0; i
< IOAT_NUM_SRC_TEST
+ 1; i
++)
1467 if (dma_srcs
[i
] != DMA_ERROR_CODE
)
1468 dma_unmap_page(dev
, dma_srcs
[i
], PAGE_SIZE
,
1472 dma
->device_free_chan_resources(dma_chan
);
1474 src_idx
= IOAT_NUM_SRC_TEST
;
1476 __free_page(xor_srcs
[src_idx
]);
1481 static int ioat3_dma_self_test(struct ioatdma_device
*device
)
1483 int rc
= ioat_dma_self_test(device
);
1488 rc
= ioat_xor_val_self_test(device
);
1495 static int ioat3_irq_reinit(struct ioatdma_device
*device
)
1497 struct pci_dev
*pdev
= device
->pdev
;
1498 int irq
= pdev
->irq
, i
;
1500 if (!is_bwd_ioat(pdev
))
1503 switch (device
->irq_mode
) {
1505 for (i
= 0; i
< device
->common
.chancnt
; i
++) {
1506 struct msix_entry
*msix
= &device
->msix_entries
[i
];
1507 struct ioat_chan_common
*chan
;
1509 chan
= ioat_chan_by_index(device
, i
);
1510 devm_free_irq(&pdev
->dev
, msix
->vector
, chan
);
1513 pci_disable_msix(pdev
);
1516 pci_disable_msi(pdev
);
1519 devm_free_irq(&pdev
->dev
, irq
, device
);
1524 device
->irq_mode
= IOAT_NOIRQ
;
1526 return ioat_dma_setup_interrupts(device
);
1529 static int ioat3_reset_hw(struct ioat_chan_common
*chan
)
1531 /* throw away whatever the channel was doing and get it
1532 * initialized, with ioat3 specific workarounds
1534 struct ioatdma_device
*device
= chan
->device
;
1535 struct pci_dev
*pdev
= device
->pdev
;
1540 ioat2_quiesce(chan
, msecs_to_jiffies(100));
1542 chanerr
= readl(chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
1543 writel(chanerr
, chan
->reg_base
+ IOAT_CHANERR_OFFSET
);
1545 if (device
->version
< IOAT_VER_3_3
) {
1546 /* clear any pending errors */
1547 err
= pci_read_config_dword(pdev
,
1548 IOAT_PCI_CHANERR_INT_OFFSET
, &chanerr
);
1551 "channel error register unreachable\n");
1554 pci_write_config_dword(pdev
,
1555 IOAT_PCI_CHANERR_INT_OFFSET
, chanerr
);
1557 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1558 * (workaround for spurious config parity error after restart)
1560 pci_read_config_word(pdev
, IOAT_PCI_DEVICE_ID_OFFSET
, &dev_id
);
1561 if (dev_id
== PCI_DEVICE_ID_INTEL_IOAT_TBG0
) {
1562 pci_write_config_dword(pdev
,
1563 IOAT_PCI_DMAUNCERRSTS_OFFSET
,
1568 err
= ioat2_reset_sync(chan
, msecs_to_jiffies(200));
1570 err
= ioat3_irq_reinit(device
);
1573 dev_err(&pdev
->dev
, "Failed to reset: %d\n", err
);
1578 static void ioat3_intr_quirk(struct ioatdma_device
*device
)
1580 struct dma_device
*dma
;
1582 struct ioat_chan_common
*chan
;
1585 dma
= &device
->common
;
1588 * if we have descriptor write back error status, we mask the
1591 if (device
->cap
& IOAT_CAP_DWBES
) {
1592 list_for_each_entry(c
, &dma
->channels
, device_node
) {
1593 chan
= to_chan_common(c
);
1594 errmask
= readl(chan
->reg_base
+
1595 IOAT_CHANERR_MASK_OFFSET
);
1596 errmask
|= IOAT_CHANERR_XOR_P_OR_CRC_ERR
|
1597 IOAT_CHANERR_XOR_Q_ERR
;
1598 writel(errmask
, chan
->reg_base
+
1599 IOAT_CHANERR_MASK_OFFSET
);
1604 int ioat3_dma_probe(struct ioatdma_device
*device
, int dca
)
1606 struct pci_dev
*pdev
= device
->pdev
;
1607 int dca_en
= system_has_dca_enabled(pdev
);
1608 struct dma_device
*dma
;
1610 struct ioat_chan_common
*chan
;
1611 bool is_raid_device
= false;
1614 device
->enumerate_channels
= ioat2_enumerate_channels
;
1615 device
->reset_hw
= ioat3_reset_hw
;
1616 device
->self_test
= ioat3_dma_self_test
;
1617 device
->intr_quirk
= ioat3_intr_quirk
;
1618 dma
= &device
->common
;
1619 dma
->device_prep_dma_memcpy
= ioat2_dma_prep_memcpy_lock
;
1620 dma
->device_issue_pending
= ioat2_issue_pending
;
1621 dma
->device_alloc_chan_resources
= ioat2_alloc_chan_resources
;
1622 dma
->device_free_chan_resources
= ioat2_free_chan_resources
;
1624 dma_cap_set(DMA_INTERRUPT
, dma
->cap_mask
);
1625 dma
->device_prep_dma_interrupt
= ioat3_prep_interrupt_lock
;
1627 device
->cap
= readl(device
->reg_base
+ IOAT_DMA_CAP_OFFSET
);
1629 if (is_xeon_cb32(pdev
) || is_bwd_noraid(pdev
))
1630 device
->cap
&= ~(IOAT_CAP_XOR
| IOAT_CAP_PQ
| IOAT_CAP_RAID16SS
);
1632 /* dca is incompatible with raid operations */
1633 if (dca_en
&& (device
->cap
& (IOAT_CAP_XOR
|IOAT_CAP_PQ
)))
1634 device
->cap
&= ~(IOAT_CAP_XOR
|IOAT_CAP_PQ
);
1636 if (device
->cap
& IOAT_CAP_XOR
) {
1637 is_raid_device
= true;
1640 dma_cap_set(DMA_XOR
, dma
->cap_mask
);
1641 dma
->device_prep_dma_xor
= ioat3_prep_xor
;
1643 dma_cap_set(DMA_XOR_VAL
, dma
->cap_mask
);
1644 dma
->device_prep_dma_xor_val
= ioat3_prep_xor_val
;
1647 if (device
->cap
& IOAT_CAP_PQ
) {
1648 is_raid_device
= true;
1650 dma
->device_prep_dma_pq
= ioat3_prep_pq
;
1651 dma
->device_prep_dma_pq_val
= ioat3_prep_pq_val
;
1652 dma_cap_set(DMA_PQ
, dma
->cap_mask
);
1653 dma_cap_set(DMA_PQ_VAL
, dma
->cap_mask
);
1655 if (device
->cap
& IOAT_CAP_RAID16SS
) {
1656 dma_set_maxpq(dma
, 16, 0);
1658 dma_set_maxpq(dma
, 8, 0);
1661 if (!(device
->cap
& IOAT_CAP_XOR
)) {
1662 dma
->device_prep_dma_xor
= ioat3_prep_pqxor
;
1663 dma
->device_prep_dma_xor_val
= ioat3_prep_pqxor_val
;
1664 dma_cap_set(DMA_XOR
, dma
->cap_mask
);
1665 dma_cap_set(DMA_XOR_VAL
, dma
->cap_mask
);
1667 if (device
->cap
& IOAT_CAP_RAID16SS
) {
1675 dma
->device_tx_status
= ioat3_tx_status
;
1676 device
->cleanup_fn
= ioat3_cleanup_event
;
1677 device
->timer_fn
= ioat3_timer_event
;
1679 /* starting with CB3.3 super extended descriptors are supported */
1680 if (device
->cap
& IOAT_CAP_RAID16SS
) {
1684 for (i
= 0; i
< MAX_SED_POOLS
; i
++) {
1685 snprintf(pool_name
, 14, "ioat_hw%d_sed", i
);
1687 /* allocate SED DMA pool */
1688 device
->sed_hw_pool
[i
] = dmam_pool_create(pool_name
,
1690 SED_SIZE
* (i
+ 1), 64, 0);
1691 if (!device
->sed_hw_pool
[i
])
1697 err
= ioat_probe(device
);
1701 list_for_each_entry(c
, &dma
->channels
, device_node
) {
1702 chan
= to_chan_common(c
);
1703 writel(IOAT_DMA_DCA_ANY_CPU
,
1704 chan
->reg_base
+ IOAT_DCACTRL_OFFSET
);
1707 err
= ioat_register(device
);
1711 ioat_kobject_add(device
, &ioat2_ktype
);
1714 device
->dca
= ioat3_dca_init(pdev
, device
->reg_base
);