dmaengine: ioatdma: Clean up IOAT_COMPLETION_PENDING flag
[linux-2.6/btrfs-unstable.git] / drivers / dma / ioat / dma.c
blob50d0112b602a12363c47516d986f014ae7c9cca7
1 /*
2 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2004 - 2015 Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * The full GNU General Public License is included in this distribution in
15 * the file called "COPYING".
20 * This driver supports an Intel I/OAT DMA engine, which does asynchronous
21 * copy operations.
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/interrupt.h>
29 #include <linux/dmaengine.h>
30 #include <linux/delay.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/workqueue.h>
33 #include <linux/prefetch.h>
34 #include "dma.h"
35 #include "registers.h"
36 #include "hw.h"
38 #include "../dmaengine.h"
40 static void ioat_eh(struct ioatdma_chan *ioat_chan);
42 /**
43 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
44 * @irq: interrupt id
45 * @data: interrupt data
47 irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
49 struct ioatdma_device *instance = data;
50 struct ioatdma_chan *ioat_chan;
51 unsigned long attnstatus;
52 int bit;
53 u8 intrctrl;
55 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
57 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
58 return IRQ_NONE;
60 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
61 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
62 return IRQ_NONE;
65 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
66 for_each_set_bit(bit, &attnstatus, BITS_PER_LONG) {
67 ioat_chan = ioat_chan_by_index(instance, bit);
68 if (test_bit(IOAT_RUN, &ioat_chan->state))
69 tasklet_schedule(&ioat_chan->cleanup_task);
72 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
73 return IRQ_HANDLED;
76 /**
77 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
78 * @irq: interrupt id
79 * @data: interrupt data
81 irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
83 struct ioatdma_chan *ioat_chan = data;
85 if (test_bit(IOAT_RUN, &ioat_chan->state))
86 tasklet_schedule(&ioat_chan->cleanup_task);
88 return IRQ_HANDLED;
91 void ioat_stop(struct ioatdma_chan *ioat_chan)
93 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
94 struct pci_dev *pdev = ioat_dma->pdev;
95 int chan_id = chan_num(ioat_chan);
96 struct msix_entry *msix;
98 /* 1/ stop irq from firing tasklets
99 * 2/ stop the tasklet from re-arming irqs
101 clear_bit(IOAT_RUN, &ioat_chan->state);
103 /* flush inflight interrupts */
104 switch (ioat_dma->irq_mode) {
105 case IOAT_MSIX:
106 msix = &ioat_dma->msix_entries[chan_id];
107 synchronize_irq(msix->vector);
108 break;
109 case IOAT_MSI:
110 case IOAT_INTX:
111 synchronize_irq(pdev->irq);
112 break;
113 default:
114 break;
117 /* flush inflight timers */
118 del_timer_sync(&ioat_chan->timer);
120 /* flush inflight tasklet runs */
121 tasklet_kill(&ioat_chan->cleanup_task);
123 /* final cleanup now that everything is quiesced and can't re-arm */
124 ioat_cleanup_event((unsigned long)&ioat_chan->dma_chan);
127 static void __ioat_issue_pending(struct ioatdma_chan *ioat_chan)
129 ioat_chan->dmacount += ioat_ring_pending(ioat_chan);
130 ioat_chan->issued = ioat_chan->head;
131 writew(ioat_chan->dmacount,
132 ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
133 dev_dbg(to_dev(ioat_chan),
134 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
135 __func__, ioat_chan->head, ioat_chan->tail,
136 ioat_chan->issued, ioat_chan->dmacount);
139 void ioat_issue_pending(struct dma_chan *c)
141 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
143 if (ioat_ring_pending(ioat_chan)) {
144 spin_lock_bh(&ioat_chan->prep_lock);
145 __ioat_issue_pending(ioat_chan);
146 spin_unlock_bh(&ioat_chan->prep_lock);
151 * ioat_update_pending - log pending descriptors
152 * @ioat: ioat+ channel
154 * Check if the number of unsubmitted descriptors has exceeded the
155 * watermark. Called with prep_lock held
157 static void ioat_update_pending(struct ioatdma_chan *ioat_chan)
159 if (ioat_ring_pending(ioat_chan) > ioat_pending_level)
160 __ioat_issue_pending(ioat_chan);
163 static void __ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
165 struct ioat_ring_ent *desc;
166 struct ioat_dma_descriptor *hw;
168 if (ioat_ring_space(ioat_chan) < 1) {
169 dev_err(to_dev(ioat_chan),
170 "Unable to start null desc - ring full\n");
171 return;
174 dev_dbg(to_dev(ioat_chan),
175 "%s: head: %#x tail: %#x issued: %#x\n",
176 __func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
177 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head);
179 hw = desc->hw;
180 hw->ctl = 0;
181 hw->ctl_f.null = 1;
182 hw->ctl_f.int_en = 1;
183 hw->ctl_f.compl_write = 1;
184 /* set size to non-zero value (channel returns error when size is 0) */
185 hw->size = NULL_DESC_BUFFER_SIZE;
186 hw->src_addr = 0;
187 hw->dst_addr = 0;
188 async_tx_ack(&desc->txd);
189 ioat_set_chainaddr(ioat_chan, desc->txd.phys);
190 dump_desc_dbg(ioat_chan, desc);
191 /* make sure descriptors are written before we submit */
192 wmb();
193 ioat_chan->head += 1;
194 __ioat_issue_pending(ioat_chan);
197 void ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
199 spin_lock_bh(&ioat_chan->prep_lock);
200 __ioat_start_null_desc(ioat_chan);
201 spin_unlock_bh(&ioat_chan->prep_lock);
204 static void __ioat_restart_chan(struct ioatdma_chan *ioat_chan)
206 /* set the tail to be re-issued */
207 ioat_chan->issued = ioat_chan->tail;
208 ioat_chan->dmacount = 0;
209 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
211 dev_dbg(to_dev(ioat_chan),
212 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
213 __func__, ioat_chan->head, ioat_chan->tail,
214 ioat_chan->issued, ioat_chan->dmacount);
216 if (ioat_ring_pending(ioat_chan)) {
217 struct ioat_ring_ent *desc;
219 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
220 ioat_set_chainaddr(ioat_chan, desc->txd.phys);
221 __ioat_issue_pending(ioat_chan);
222 } else
223 __ioat_start_null_desc(ioat_chan);
226 static int ioat_quiesce(struct ioatdma_chan *ioat_chan, unsigned long tmo)
228 unsigned long end = jiffies + tmo;
229 int err = 0;
230 u32 status;
232 status = ioat_chansts(ioat_chan);
233 if (is_ioat_active(status) || is_ioat_idle(status))
234 ioat_suspend(ioat_chan);
235 while (is_ioat_active(status) || is_ioat_idle(status)) {
236 if (tmo && time_after(jiffies, end)) {
237 err = -ETIMEDOUT;
238 break;
240 status = ioat_chansts(ioat_chan);
241 cpu_relax();
244 return err;
247 static int ioat_reset_sync(struct ioatdma_chan *ioat_chan, unsigned long tmo)
249 unsigned long end = jiffies + tmo;
250 int err = 0;
252 ioat_reset(ioat_chan);
253 while (ioat_reset_pending(ioat_chan)) {
254 if (end && time_after(jiffies, end)) {
255 err = -ETIMEDOUT;
256 break;
258 cpu_relax();
261 return err;
264 static dma_cookie_t ioat_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
266 struct dma_chan *c = tx->chan;
267 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
268 dma_cookie_t cookie;
270 cookie = dma_cookie_assign(tx);
271 dev_dbg(to_dev(ioat_chan), "%s: cookie: %d\n", __func__, cookie);
273 if (!test_and_set_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
274 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
276 /* make descriptor updates visible before advancing ioat->head,
277 * this is purposefully not smp_wmb() since we are also
278 * publishing the descriptor updates to a dma device
280 wmb();
282 ioat_chan->head += ioat_chan->produce;
284 ioat_update_pending(ioat_chan);
285 spin_unlock_bh(&ioat_chan->prep_lock);
287 return cookie;
290 static struct ioat_ring_ent *
291 ioat_alloc_ring_ent(struct dma_chan *chan, gfp_t flags)
293 struct ioat_dma_descriptor *hw;
294 struct ioat_ring_ent *desc;
295 struct ioatdma_device *ioat_dma;
296 dma_addr_t phys;
298 ioat_dma = to_ioatdma_device(chan->device);
299 hw = pci_pool_alloc(ioat_dma->dma_pool, flags, &phys);
300 if (!hw)
301 return NULL;
302 memset(hw, 0, sizeof(*hw));
304 desc = kmem_cache_zalloc(ioat_cache, flags);
305 if (!desc) {
306 pci_pool_free(ioat_dma->dma_pool, hw, phys);
307 return NULL;
310 dma_async_tx_descriptor_init(&desc->txd, chan);
311 desc->txd.tx_submit = ioat_tx_submit_unlock;
312 desc->hw = hw;
313 desc->txd.phys = phys;
314 return desc;
317 void ioat_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
319 struct ioatdma_device *ioat_dma;
321 ioat_dma = to_ioatdma_device(chan->device);
322 pci_pool_free(ioat_dma->dma_pool, desc->hw, desc->txd.phys);
323 kmem_cache_free(ioat_cache, desc);
326 struct ioat_ring_ent **
327 ioat_alloc_ring(struct dma_chan *c, int order, gfp_t flags)
329 struct ioat_ring_ent **ring;
330 int descs = 1 << order;
331 int i;
333 if (order > ioat_get_max_alloc_order())
334 return NULL;
336 /* allocate the array to hold the software ring */
337 ring = kcalloc(descs, sizeof(*ring), flags);
338 if (!ring)
339 return NULL;
340 for (i = 0; i < descs; i++) {
341 ring[i] = ioat_alloc_ring_ent(c, flags);
342 if (!ring[i]) {
343 while (i--)
344 ioat_free_ring_ent(ring[i], c);
345 kfree(ring);
346 return NULL;
348 set_desc_id(ring[i], i);
351 /* link descs */
352 for (i = 0; i < descs-1; i++) {
353 struct ioat_ring_ent *next = ring[i+1];
354 struct ioat_dma_descriptor *hw = ring[i]->hw;
356 hw->next = next->txd.phys;
358 ring[i]->hw->next = ring[0]->txd.phys;
360 return ring;
363 static bool reshape_ring(struct ioatdma_chan *ioat_chan, int order)
365 /* reshape differs from normal ring allocation in that we want
366 * to allocate a new software ring while only
367 * extending/truncating the hardware ring
369 struct dma_chan *c = &ioat_chan->dma_chan;
370 const u32 curr_size = ioat_ring_size(ioat_chan);
371 const u16 active = ioat_ring_active(ioat_chan);
372 const u32 new_size = 1 << order;
373 struct ioat_ring_ent **ring;
374 u32 i;
376 if (order > ioat_get_max_alloc_order())
377 return false;
379 /* double check that we have at least 1 free descriptor */
380 if (active == curr_size)
381 return false;
383 /* when shrinking, verify that we can hold the current active
384 * set in the new ring
386 if (active >= new_size)
387 return false;
389 /* allocate the array to hold the software ring */
390 ring = kcalloc(new_size, sizeof(*ring), GFP_NOWAIT);
391 if (!ring)
392 return false;
394 /* allocate/trim descriptors as needed */
395 if (new_size > curr_size) {
396 /* copy current descriptors to the new ring */
397 for (i = 0; i < curr_size; i++) {
398 u16 curr_idx = (ioat_chan->tail+i) & (curr_size-1);
399 u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
401 ring[new_idx] = ioat_chan->ring[curr_idx];
402 set_desc_id(ring[new_idx], new_idx);
405 /* add new descriptors to the ring */
406 for (i = curr_size; i < new_size; i++) {
407 u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
409 ring[new_idx] = ioat_alloc_ring_ent(c, GFP_NOWAIT);
410 if (!ring[new_idx]) {
411 while (i--) {
412 u16 new_idx = (ioat_chan->tail+i) &
413 (new_size-1);
415 ioat_free_ring_ent(ring[new_idx], c);
417 kfree(ring);
418 return false;
420 set_desc_id(ring[new_idx], new_idx);
423 /* hw link new descriptors */
424 for (i = curr_size-1; i < new_size; i++) {
425 u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
426 struct ioat_ring_ent *next =
427 ring[(new_idx+1) & (new_size-1)];
428 struct ioat_dma_descriptor *hw = ring[new_idx]->hw;
430 hw->next = next->txd.phys;
432 } else {
433 struct ioat_dma_descriptor *hw;
434 struct ioat_ring_ent *next;
436 /* copy current descriptors to the new ring, dropping the
437 * removed descriptors
439 for (i = 0; i < new_size; i++) {
440 u16 curr_idx = (ioat_chan->tail+i) & (curr_size-1);
441 u16 new_idx = (ioat_chan->tail+i) & (new_size-1);
443 ring[new_idx] = ioat_chan->ring[curr_idx];
444 set_desc_id(ring[new_idx], new_idx);
447 /* free deleted descriptors */
448 for (i = new_size; i < curr_size; i++) {
449 struct ioat_ring_ent *ent;
451 ent = ioat_get_ring_ent(ioat_chan, ioat_chan->tail+i);
452 ioat_free_ring_ent(ent, c);
455 /* fix up hardware ring */
456 hw = ring[(ioat_chan->tail+new_size-1) & (new_size-1)]->hw;
457 next = ring[(ioat_chan->tail+new_size) & (new_size-1)];
458 hw->next = next->txd.phys;
461 dev_dbg(to_dev(ioat_chan), "%s: allocated %d descriptors\n",
462 __func__, new_size);
464 kfree(ioat_chan->ring);
465 ioat_chan->ring = ring;
466 ioat_chan->alloc_order = order;
468 return true;
472 * ioat_check_space_lock - verify space and grab ring producer lock
473 * @ioat: ioat,3 channel (ring) to operate on
474 * @num_descs: allocation length
476 int ioat_check_space_lock(struct ioatdma_chan *ioat_chan, int num_descs)
478 bool retry;
480 retry:
481 spin_lock_bh(&ioat_chan->prep_lock);
482 /* never allow the last descriptor to be consumed, we need at
483 * least one free at all times to allow for on-the-fly ring
484 * resizing.
486 if (likely(ioat_ring_space(ioat_chan) > num_descs)) {
487 dev_dbg(to_dev(ioat_chan), "%s: num_descs: %d (%x:%x:%x)\n",
488 __func__, num_descs, ioat_chan->head,
489 ioat_chan->tail, ioat_chan->issued);
490 ioat_chan->produce = num_descs;
491 return 0; /* with ioat->prep_lock held */
493 retry = test_and_set_bit(IOAT_RESHAPE_PENDING, &ioat_chan->state);
494 spin_unlock_bh(&ioat_chan->prep_lock);
496 /* is another cpu already trying to expand the ring? */
497 if (retry)
498 goto retry;
500 spin_lock_bh(&ioat_chan->cleanup_lock);
501 spin_lock_bh(&ioat_chan->prep_lock);
502 retry = reshape_ring(ioat_chan, ioat_chan->alloc_order + 1);
503 clear_bit(IOAT_RESHAPE_PENDING, &ioat_chan->state);
504 spin_unlock_bh(&ioat_chan->prep_lock);
505 spin_unlock_bh(&ioat_chan->cleanup_lock);
507 /* if we were able to expand the ring retry the allocation */
508 if (retry)
509 goto retry;
511 dev_dbg_ratelimited(to_dev(ioat_chan),
512 "%s: ring full! num_descs: %d (%x:%x:%x)\n",
513 __func__, num_descs, ioat_chan->head,
514 ioat_chan->tail, ioat_chan->issued);
516 /* progress reclaim in the allocation failure case we may be
517 * called under bh_disabled so we need to trigger the timer
518 * event directly
520 if (time_is_before_jiffies(ioat_chan->timer.expires)
521 && timer_pending(&ioat_chan->timer)) {
522 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
523 ioat_timer_event((unsigned long)ioat_chan);
526 return -ENOMEM;
529 static bool desc_has_ext(struct ioat_ring_ent *desc)
531 struct ioat_dma_descriptor *hw = desc->hw;
533 if (hw->ctl_f.op == IOAT_OP_XOR ||
534 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
535 struct ioat_xor_descriptor *xor = desc->xor;
537 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
538 return true;
539 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
540 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
541 struct ioat_pq_descriptor *pq = desc->pq;
543 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
544 return true;
547 return false;
550 static void
551 ioat_free_sed(struct ioatdma_device *ioat_dma, struct ioat_sed_ent *sed)
553 if (!sed)
554 return;
556 dma_pool_free(ioat_dma->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
557 kmem_cache_free(ioat_sed_cache, sed);
560 static u64 ioat_get_current_completion(struct ioatdma_chan *ioat_chan)
562 u64 phys_complete;
563 u64 completion;
565 completion = *ioat_chan->completion;
566 phys_complete = ioat_chansts_to_addr(completion);
568 dev_dbg(to_dev(ioat_chan), "%s: phys_complete: %#llx\n", __func__,
569 (unsigned long long) phys_complete);
571 return phys_complete;
574 static bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
575 u64 *phys_complete)
577 *phys_complete = ioat_get_current_completion(ioat_chan);
578 if (*phys_complete == ioat_chan->last_completion)
579 return false;
581 clear_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
582 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
584 return true;
587 static void
588 desc_get_errstat(struct ioatdma_chan *ioat_chan, struct ioat_ring_ent *desc)
590 struct ioat_dma_descriptor *hw = desc->hw;
592 switch (hw->ctl_f.op) {
593 case IOAT_OP_PQ_VAL:
594 case IOAT_OP_PQ_VAL_16S:
596 struct ioat_pq_descriptor *pq = desc->pq;
598 /* check if there's error written */
599 if (!pq->dwbes_f.wbes)
600 return;
602 /* need to set a chanerr var for checking to clear later */
604 if (pq->dwbes_f.p_val_err)
605 *desc->result |= SUM_CHECK_P_RESULT;
607 if (pq->dwbes_f.q_val_err)
608 *desc->result |= SUM_CHECK_Q_RESULT;
610 return;
612 default:
613 return;
618 * __cleanup - reclaim used descriptors
619 * @ioat: channel (ring) to clean
621 static void __cleanup(struct ioatdma_chan *ioat_chan, dma_addr_t phys_complete)
623 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
624 struct ioat_ring_ent *desc;
625 bool seen_current = false;
626 int idx = ioat_chan->tail, i;
627 u16 active;
629 dev_dbg(to_dev(ioat_chan), "%s: head: %#x tail: %#x issued: %#x\n",
630 __func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
633 * At restart of the channel, the completion address and the
634 * channel status will be 0 due to starting a new chain. Since
635 * it's new chain and the first descriptor "fails", there is
636 * nothing to clean up. We do not want to reap the entire submitted
637 * chain due to this 0 address value and then BUG.
639 if (!phys_complete)
640 return;
642 active = ioat_ring_active(ioat_chan);
643 for (i = 0; i < active && !seen_current; i++) {
644 struct dma_async_tx_descriptor *tx;
646 smp_read_barrier_depends();
647 prefetch(ioat_get_ring_ent(ioat_chan, idx + i + 1));
648 desc = ioat_get_ring_ent(ioat_chan, idx + i);
649 dump_desc_dbg(ioat_chan, desc);
651 /* set err stat if we are using dwbes */
652 if (ioat_dma->cap & IOAT_CAP_DWBES)
653 desc_get_errstat(ioat_chan, desc);
655 tx = &desc->txd;
656 if (tx->cookie) {
657 dma_cookie_complete(tx);
658 dma_descriptor_unmap(tx);
659 if (tx->callback) {
660 tx->callback(tx->callback_param);
661 tx->callback = NULL;
665 if (tx->phys == phys_complete)
666 seen_current = true;
668 /* skip extended descriptors */
669 if (desc_has_ext(desc)) {
670 BUG_ON(i + 1 >= active);
671 i++;
674 /* cleanup super extended descriptors */
675 if (desc->sed) {
676 ioat_free_sed(ioat_dma, desc->sed);
677 desc->sed = NULL;
681 /* finish all descriptor reads before incrementing tail */
682 smp_mb();
683 ioat_chan->tail = idx + i;
684 /* no active descs have written a completion? */
685 BUG_ON(active && !seen_current);
686 ioat_chan->last_completion = phys_complete;
688 if (active - i == 0) {
689 dev_dbg(to_dev(ioat_chan), "%s: cancel completion timeout\n",
690 __func__);
691 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
694 /* 5 microsecond delay per pending descriptor */
695 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
696 ioat_chan->ioat_dma->reg_base + IOAT_INTRDELAY_OFFSET);
699 static void ioat_cleanup(struct ioatdma_chan *ioat_chan)
701 u64 phys_complete;
703 spin_lock_bh(&ioat_chan->cleanup_lock);
705 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
706 __cleanup(ioat_chan, phys_complete);
708 if (is_ioat_halted(*ioat_chan->completion)) {
709 u32 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
711 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
712 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
713 ioat_eh(ioat_chan);
717 spin_unlock_bh(&ioat_chan->cleanup_lock);
720 void ioat_cleanup_event(unsigned long data)
722 struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
724 ioat_cleanup(ioat_chan);
725 if (!test_bit(IOAT_RUN, &ioat_chan->state))
726 return;
727 writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
730 static void ioat_restart_channel(struct ioatdma_chan *ioat_chan)
732 u64 phys_complete;
734 ioat_quiesce(ioat_chan, 0);
735 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
736 __cleanup(ioat_chan, phys_complete);
738 __ioat_restart_chan(ioat_chan);
741 static void ioat_eh(struct ioatdma_chan *ioat_chan)
743 struct pci_dev *pdev = to_pdev(ioat_chan);
744 struct ioat_dma_descriptor *hw;
745 struct dma_async_tx_descriptor *tx;
746 u64 phys_complete;
747 struct ioat_ring_ent *desc;
748 u32 err_handled = 0;
749 u32 chanerr_int;
750 u32 chanerr;
752 /* cleanup so tail points to descriptor that caused the error */
753 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
754 __cleanup(ioat_chan, phys_complete);
756 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
757 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
759 dev_dbg(to_dev(ioat_chan), "%s: error = %x:%x\n",
760 __func__, chanerr, chanerr_int);
762 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
763 hw = desc->hw;
764 dump_desc_dbg(ioat_chan, desc);
766 switch (hw->ctl_f.op) {
767 case IOAT_OP_XOR_VAL:
768 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
769 *desc->result |= SUM_CHECK_P_RESULT;
770 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
772 break;
773 case IOAT_OP_PQ_VAL:
774 case IOAT_OP_PQ_VAL_16S:
775 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
776 *desc->result |= SUM_CHECK_P_RESULT;
777 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
779 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
780 *desc->result |= SUM_CHECK_Q_RESULT;
781 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
783 break;
786 /* fault on unhandled error or spurious halt */
787 if (chanerr ^ err_handled || chanerr == 0) {
788 dev_err(to_dev(ioat_chan), "%s: fatal error (%x:%x)\n",
789 __func__, chanerr, err_handled);
790 BUG();
791 } else { /* cleanup the faulty descriptor */
792 tx = &desc->txd;
793 if (tx->cookie) {
794 dma_cookie_complete(tx);
795 dma_descriptor_unmap(tx);
796 if (tx->callback) {
797 tx->callback(tx->callback_param);
798 tx->callback = NULL;
803 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
804 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
806 /* mark faulting descriptor as complete */
807 *ioat_chan->completion = desc->txd.phys;
809 spin_lock_bh(&ioat_chan->prep_lock);
810 ioat_restart_channel(ioat_chan);
811 spin_unlock_bh(&ioat_chan->prep_lock);
814 static void check_active(struct ioatdma_chan *ioat_chan)
816 if (ioat_ring_active(ioat_chan)) {
817 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
818 return;
821 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
822 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
823 else if (ioat_chan->alloc_order > ioat_get_alloc_order()) {
824 /* if the ring is idle, empty, and oversized try to step
825 * down the size
827 reshape_ring(ioat_chan, ioat_chan->alloc_order - 1);
829 /* keep shrinking until we get back to our minimum
830 * default size
832 if (ioat_chan->alloc_order > ioat_get_alloc_order())
833 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
838 void ioat_timer_event(unsigned long data)
840 struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
841 dma_addr_t phys_complete;
842 u64 status;
844 status = ioat_chansts(ioat_chan);
846 /* when halted due to errors check for channel
847 * programming errors before advancing the completion state
849 if (is_ioat_halted(status)) {
850 u32 chanerr;
852 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
853 dev_err(to_dev(ioat_chan), "%s: Channel halted (%x)\n",
854 __func__, chanerr);
855 if (test_bit(IOAT_RUN, &ioat_chan->state))
856 BUG_ON(is_ioat_bug(chanerr));
857 else /* we never got off the ground */
858 return;
861 /* if we haven't made progress and we have already
862 * acknowledged a pending completion once, then be more
863 * forceful with a restart
865 spin_lock_bh(&ioat_chan->cleanup_lock);
866 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
867 __cleanup(ioat_chan, phys_complete);
868 else if (test_bit(IOAT_COMPLETION_ACK, &ioat_chan->state)) {
869 spin_lock_bh(&ioat_chan->prep_lock);
870 ioat_restart_channel(ioat_chan);
871 spin_unlock_bh(&ioat_chan->prep_lock);
872 spin_unlock_bh(&ioat_chan->cleanup_lock);
873 return;
874 } else {
875 set_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
876 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
880 if (ioat_ring_active(ioat_chan))
881 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
882 else {
883 spin_lock_bh(&ioat_chan->prep_lock);
884 check_active(ioat_chan);
885 spin_unlock_bh(&ioat_chan->prep_lock);
887 spin_unlock_bh(&ioat_chan->cleanup_lock);
890 enum dma_status
891 ioat_tx_status(struct dma_chan *c, dma_cookie_t cookie,
892 struct dma_tx_state *txstate)
894 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
895 enum dma_status ret;
897 ret = dma_cookie_status(c, cookie, txstate);
898 if (ret == DMA_COMPLETE)
899 return ret;
901 ioat_cleanup(ioat_chan);
903 return dma_cookie_status(c, cookie, txstate);
906 static int ioat_irq_reinit(struct ioatdma_device *ioat_dma)
908 struct pci_dev *pdev = ioat_dma->pdev;
909 int irq = pdev->irq, i;
911 if (!is_bwd_ioat(pdev))
912 return 0;
914 switch (ioat_dma->irq_mode) {
915 case IOAT_MSIX:
916 for (i = 0; i < ioat_dma->dma_dev.chancnt; i++) {
917 struct msix_entry *msix = &ioat_dma->msix_entries[i];
918 struct ioatdma_chan *ioat_chan;
920 ioat_chan = ioat_chan_by_index(ioat_dma, i);
921 devm_free_irq(&pdev->dev, msix->vector, ioat_chan);
924 pci_disable_msix(pdev);
925 break;
926 case IOAT_MSI:
927 pci_disable_msi(pdev);
928 /* fall through */
929 case IOAT_INTX:
930 devm_free_irq(&pdev->dev, irq, ioat_dma);
931 break;
932 default:
933 return 0;
935 ioat_dma->irq_mode = IOAT_NOIRQ;
937 return ioat_dma_setup_interrupts(ioat_dma);
940 int ioat_reset_hw(struct ioatdma_chan *ioat_chan)
942 /* throw away whatever the channel was doing and get it
943 * initialized, with ioat3 specific workarounds
945 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
946 struct pci_dev *pdev = ioat_dma->pdev;
947 u32 chanerr;
948 u16 dev_id;
949 int err;
951 ioat_quiesce(ioat_chan, msecs_to_jiffies(100));
953 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
954 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
956 if (ioat_dma->version < IOAT_VER_3_3) {
957 /* clear any pending errors */
958 err = pci_read_config_dword(pdev,
959 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
960 if (err) {
961 dev_err(&pdev->dev,
962 "channel error register unreachable\n");
963 return err;
965 pci_write_config_dword(pdev,
966 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
968 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
969 * (workaround for spurious config parity error after restart)
971 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
972 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
973 pci_write_config_dword(pdev,
974 IOAT_PCI_DMAUNCERRSTS_OFFSET,
975 0x10);
979 err = ioat_reset_sync(ioat_chan, msecs_to_jiffies(200));
980 if (!err)
981 err = ioat_irq_reinit(ioat_dma);
983 if (err)
984 dev_err(&pdev->dev, "Failed to reset: %d\n", err);
986 return err;