xref: /linux/drivers/dma/ioat/dma.c (revision cd60cd96137f6cb3ea82cace9225626619e7a52d)
1 /*
2  * Intel I/OAT DMA Linux driver
3  * Copyright(c) 2004 - 2015 Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * The full GNU General Public License is included in this distribution in
15  * the file called "COPYING".
16  *
17  */
18 
19 /*
20  * This driver supports an Intel I/OAT DMA engine, which does asynchronous
21  * copy operations.
22  */
23 
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"
37 
38 #include "../dmaengine.h"
39 
40 static void ioat_eh(struct ioatdma_chan *ioat_chan);
41 
42 /**
43  * ioat_dma_do_interrupt - handler used for single vector interrupt mode
44  * @irq: interrupt id
45  * @data: interrupt data
46  */
47 irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
48 {
49 	struct ioatdma_device *instance = data;
50 	struct ioatdma_chan *ioat_chan;
51 	unsigned long attnstatus;
52 	int bit;
53 	u8 intrctrl;
54 
55 	intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
56 
57 	if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
58 		return IRQ_NONE;
59 
60 	if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
61 		writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
62 		return IRQ_NONE;
63 	}
64 
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);
70 	}
71 
72 	writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
73 	return IRQ_HANDLED;
74 }
75 
76 /**
77  * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
78  * @irq: interrupt id
79  * @data: interrupt data
80  */
81 irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
82 {
83 	struct ioatdma_chan *ioat_chan = data;
84 
85 	if (test_bit(IOAT_RUN, &ioat_chan->state))
86 		tasklet_schedule(&ioat_chan->cleanup_task);
87 
88 	return IRQ_HANDLED;
89 }
90 
91 void ioat_stop(struct ioatdma_chan *ioat_chan)
92 {
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;
97 
98 	/* 1/ stop irq from firing tasklets
99 	 * 2/ stop the tasklet from re-arming irqs
100 	 */
101 	clear_bit(IOAT_RUN, &ioat_chan->state);
102 
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;
115 	}
116 
117 	/* flush inflight timers */
118 	del_timer_sync(&ioat_chan->timer);
119 
120 	/* flush inflight tasklet runs */
121 	tasklet_kill(&ioat_chan->cleanup_task);
122 
123 	/* final cleanup now that everything is quiesced and can't re-arm */
124 	ioat_cleanup_event((unsigned long)&ioat_chan->dma_chan);
125 }
126 
127 static void __ioat_issue_pending(struct ioatdma_chan *ioat_chan)
128 {
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);
137 }
138 
139 void ioat_issue_pending(struct dma_chan *c)
140 {
141 	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
142 
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);
147 	}
148 }
149 
150 /**
151  * ioat_update_pending - log pending descriptors
152  * @ioat: ioat+ channel
153  *
154  * Check if the number of unsubmitted descriptors has exceeded the
155  * watermark.  Called with prep_lock held
156  */
157 static void ioat_update_pending(struct ioatdma_chan *ioat_chan)
158 {
159 	if (ioat_ring_pending(ioat_chan) > ioat_pending_level)
160 		__ioat_issue_pending(ioat_chan);
161 }
162 
163 static void __ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
164 {
165 	struct ioat_ring_ent *desc;
166 	struct ioat_dma_descriptor *hw;
167 
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;
172 	}
173 
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);
178 
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);
195 }
196 
197 void ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
198 {
199 	spin_lock_bh(&ioat_chan->prep_lock);
200 	if (!test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
201 		__ioat_start_null_desc(ioat_chan);
202 	spin_unlock_bh(&ioat_chan->prep_lock);
203 }
204 
205 static void __ioat_restart_chan(struct ioatdma_chan *ioat_chan)
206 {
207 	/* set the tail to be re-issued */
208 	ioat_chan->issued = ioat_chan->tail;
209 	ioat_chan->dmacount = 0;
210 	mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
211 
212 	dev_dbg(to_dev(ioat_chan),
213 		"%s: head: %#x tail: %#x issued: %#x count: %#x\n",
214 		__func__, ioat_chan->head, ioat_chan->tail,
215 		ioat_chan->issued, ioat_chan->dmacount);
216 
217 	if (ioat_ring_pending(ioat_chan)) {
218 		struct ioat_ring_ent *desc;
219 
220 		desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
221 		ioat_set_chainaddr(ioat_chan, desc->txd.phys);
222 		__ioat_issue_pending(ioat_chan);
223 	} else
224 		__ioat_start_null_desc(ioat_chan);
225 }
226 
227 static int ioat_quiesce(struct ioatdma_chan *ioat_chan, unsigned long tmo)
228 {
229 	unsigned long end = jiffies + tmo;
230 	int err = 0;
231 	u32 status;
232 
233 	status = ioat_chansts(ioat_chan);
234 	if (is_ioat_active(status) || is_ioat_idle(status))
235 		ioat_suspend(ioat_chan);
236 	while (is_ioat_active(status) || is_ioat_idle(status)) {
237 		if (tmo && time_after(jiffies, end)) {
238 			err = -ETIMEDOUT;
239 			break;
240 		}
241 		status = ioat_chansts(ioat_chan);
242 		cpu_relax();
243 	}
244 
245 	return err;
246 }
247 
248 static int ioat_reset_sync(struct ioatdma_chan *ioat_chan, unsigned long tmo)
249 {
250 	unsigned long end = jiffies + tmo;
251 	int err = 0;
252 
253 	ioat_reset(ioat_chan);
254 	while (ioat_reset_pending(ioat_chan)) {
255 		if (end && time_after(jiffies, end)) {
256 			err = -ETIMEDOUT;
257 			break;
258 		}
259 		cpu_relax();
260 	}
261 
262 	return err;
263 }
264 
265 static dma_cookie_t ioat_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
266 	__releases(&ioat_chan->prep_lock)
267 {
268 	struct dma_chan *c = tx->chan;
269 	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
270 	dma_cookie_t cookie;
271 
272 	cookie = dma_cookie_assign(tx);
273 	dev_dbg(to_dev(ioat_chan), "%s: cookie: %d\n", __func__, cookie);
274 
275 	if (!test_and_set_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
276 		mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
277 
278 	/* make descriptor updates visible before advancing ioat->head,
279 	 * this is purposefully not smp_wmb() since we are also
280 	 * publishing the descriptor updates to a dma device
281 	 */
282 	wmb();
283 
284 	ioat_chan->head += ioat_chan->produce;
285 
286 	ioat_update_pending(ioat_chan);
287 	spin_unlock_bh(&ioat_chan->prep_lock);
288 
289 	return cookie;
290 }
291 
292 static struct ioat_ring_ent *
293 ioat_alloc_ring_ent(struct dma_chan *chan, gfp_t flags)
294 {
295 	struct ioat_dma_descriptor *hw;
296 	struct ioat_ring_ent *desc;
297 	struct ioatdma_device *ioat_dma;
298 	dma_addr_t phys;
299 
300 	ioat_dma = to_ioatdma_device(chan->device);
301 	hw = dma_pool_alloc(ioat_dma->dma_pool, flags, &phys);
302 	if (!hw)
303 		return NULL;
304 	memset(hw, 0, sizeof(*hw));
305 
306 	desc = kmem_cache_zalloc(ioat_cache, flags);
307 	if (!desc) {
308 		dma_pool_free(ioat_dma->dma_pool, hw, phys);
309 		return NULL;
310 	}
311 
312 	dma_async_tx_descriptor_init(&desc->txd, chan);
313 	desc->txd.tx_submit = ioat_tx_submit_unlock;
314 	desc->hw = hw;
315 	desc->txd.phys = phys;
316 	return desc;
317 }
318 
319 void ioat_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
320 {
321 	struct ioatdma_device *ioat_dma;
322 
323 	ioat_dma = to_ioatdma_device(chan->device);
324 	dma_pool_free(ioat_dma->dma_pool, desc->hw, desc->txd.phys);
325 	kmem_cache_free(ioat_cache, desc);
326 }
327 
328 struct ioat_ring_ent **
329 ioat_alloc_ring(struct dma_chan *c, int order, gfp_t flags)
330 {
331 	struct ioat_ring_ent **ring;
332 	int descs = 1 << order;
333 	int i;
334 
335 	/* allocate the array to hold the software ring */
336 	ring = kcalloc(descs, sizeof(*ring), flags);
337 	if (!ring)
338 		return NULL;
339 	for (i = 0; i < descs; i++) {
340 		ring[i] = ioat_alloc_ring_ent(c, flags);
341 		if (!ring[i]) {
342 			while (i--)
343 				ioat_free_ring_ent(ring[i], c);
344 			kfree(ring);
345 			return NULL;
346 		}
347 		set_desc_id(ring[i], i);
348 	}
349 
350 	/* link descs */
351 	for (i = 0; i < descs-1; i++) {
352 		struct ioat_ring_ent *next = ring[i+1];
353 		struct ioat_dma_descriptor *hw = ring[i]->hw;
354 
355 		hw->next = next->txd.phys;
356 	}
357 	ring[i]->hw->next = ring[0]->txd.phys;
358 
359 	return ring;
360 }
361 
362 /**
363  * ioat_check_space_lock - verify space and grab ring producer lock
364  * @ioat: ioat,3 channel (ring) to operate on
365  * @num_descs: allocation length
366  */
367 int ioat_check_space_lock(struct ioatdma_chan *ioat_chan, int num_descs)
368 	__acquires(&ioat_chan->prep_lock)
369 {
370 	spin_lock_bh(&ioat_chan->prep_lock);
371 	/* never allow the last descriptor to be consumed, we need at
372 	 * least one free at all times to allow for on-the-fly ring
373 	 * resizing.
374 	 */
375 	if (likely(ioat_ring_space(ioat_chan) > num_descs)) {
376 		dev_dbg(to_dev(ioat_chan), "%s: num_descs: %d (%x:%x:%x)\n",
377 			__func__, num_descs, ioat_chan->head,
378 			ioat_chan->tail, ioat_chan->issued);
379 		ioat_chan->produce = num_descs;
380 		return 0;  /* with ioat->prep_lock held */
381 	}
382 	spin_unlock_bh(&ioat_chan->prep_lock);
383 
384 	dev_dbg_ratelimited(to_dev(ioat_chan),
385 			    "%s: ring full! num_descs: %d (%x:%x:%x)\n",
386 			    __func__, num_descs, ioat_chan->head,
387 			    ioat_chan->tail, ioat_chan->issued);
388 
389 	/* progress reclaim in the allocation failure case we may be
390 	 * called under bh_disabled so we need to trigger the timer
391 	 * event directly
392 	 */
393 	if (time_is_before_jiffies(ioat_chan->timer.expires)
394 	    && timer_pending(&ioat_chan->timer)) {
395 		mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
396 		ioat_timer_event((unsigned long)ioat_chan);
397 	}
398 
399 	return -ENOMEM;
400 }
401 
402 static bool desc_has_ext(struct ioat_ring_ent *desc)
403 {
404 	struct ioat_dma_descriptor *hw = desc->hw;
405 
406 	if (hw->ctl_f.op == IOAT_OP_XOR ||
407 	    hw->ctl_f.op == IOAT_OP_XOR_VAL) {
408 		struct ioat_xor_descriptor *xor = desc->xor;
409 
410 		if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
411 			return true;
412 	} else if (hw->ctl_f.op == IOAT_OP_PQ ||
413 		   hw->ctl_f.op == IOAT_OP_PQ_VAL) {
414 		struct ioat_pq_descriptor *pq = desc->pq;
415 
416 		if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
417 			return true;
418 	}
419 
420 	return false;
421 }
422 
423 static void
424 ioat_free_sed(struct ioatdma_device *ioat_dma, struct ioat_sed_ent *sed)
425 {
426 	if (!sed)
427 		return;
428 
429 	dma_pool_free(ioat_dma->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
430 	kmem_cache_free(ioat_sed_cache, sed);
431 }
432 
433 static u64 ioat_get_current_completion(struct ioatdma_chan *ioat_chan)
434 {
435 	u64 phys_complete;
436 	u64 completion;
437 
438 	completion = *ioat_chan->completion;
439 	phys_complete = ioat_chansts_to_addr(completion);
440 
441 	dev_dbg(to_dev(ioat_chan), "%s: phys_complete: %#llx\n", __func__,
442 		(unsigned long long) phys_complete);
443 
444 	return phys_complete;
445 }
446 
447 static bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
448 				   u64 *phys_complete)
449 {
450 	*phys_complete = ioat_get_current_completion(ioat_chan);
451 	if (*phys_complete == ioat_chan->last_completion)
452 		return false;
453 
454 	clear_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
455 	mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
456 
457 	return true;
458 }
459 
460 static void
461 desc_get_errstat(struct ioatdma_chan *ioat_chan, struct ioat_ring_ent *desc)
462 {
463 	struct ioat_dma_descriptor *hw = desc->hw;
464 
465 	switch (hw->ctl_f.op) {
466 	case IOAT_OP_PQ_VAL:
467 	case IOAT_OP_PQ_VAL_16S:
468 	{
469 		struct ioat_pq_descriptor *pq = desc->pq;
470 
471 		/* check if there's error written */
472 		if (!pq->dwbes_f.wbes)
473 			return;
474 
475 		/* need to set a chanerr var for checking to clear later */
476 
477 		if (pq->dwbes_f.p_val_err)
478 			*desc->result |= SUM_CHECK_P_RESULT;
479 
480 		if (pq->dwbes_f.q_val_err)
481 			*desc->result |= SUM_CHECK_Q_RESULT;
482 
483 		return;
484 	}
485 	default:
486 		return;
487 	}
488 }
489 
490 /**
491  * __cleanup - reclaim used descriptors
492  * @ioat: channel (ring) to clean
493  */
494 static void __cleanup(struct ioatdma_chan *ioat_chan, dma_addr_t phys_complete)
495 {
496 	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
497 	struct ioat_ring_ent *desc;
498 	bool seen_current = false;
499 	int idx = ioat_chan->tail, i;
500 	u16 active;
501 
502 	dev_dbg(to_dev(ioat_chan), "%s: head: %#x tail: %#x issued: %#x\n",
503 		__func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
504 
505 	/*
506 	 * At restart of the channel, the completion address and the
507 	 * channel status will be 0 due to starting a new chain. Since
508 	 * it's new chain and the first descriptor "fails", there is
509 	 * nothing to clean up. We do not want to reap the entire submitted
510 	 * chain due to this 0 address value and then BUG.
511 	 */
512 	if (!phys_complete)
513 		return;
514 
515 	active = ioat_ring_active(ioat_chan);
516 	for (i = 0; i < active && !seen_current; i++) {
517 		struct dma_async_tx_descriptor *tx;
518 
519 		smp_read_barrier_depends();
520 		prefetch(ioat_get_ring_ent(ioat_chan, idx + i + 1));
521 		desc = ioat_get_ring_ent(ioat_chan, idx + i);
522 		dump_desc_dbg(ioat_chan, desc);
523 
524 		/* set err stat if we are using dwbes */
525 		if (ioat_dma->cap & IOAT_CAP_DWBES)
526 			desc_get_errstat(ioat_chan, desc);
527 
528 		tx = &desc->txd;
529 		if (tx->cookie) {
530 			dma_cookie_complete(tx);
531 			dma_descriptor_unmap(tx);
532 			if (tx->callback) {
533 				tx->callback(tx->callback_param);
534 				tx->callback = NULL;
535 			}
536 		}
537 
538 		if (tx->phys == phys_complete)
539 			seen_current = true;
540 
541 		/* skip extended descriptors */
542 		if (desc_has_ext(desc)) {
543 			BUG_ON(i + 1 >= active);
544 			i++;
545 		}
546 
547 		/* cleanup super extended descriptors */
548 		if (desc->sed) {
549 			ioat_free_sed(ioat_dma, desc->sed);
550 			desc->sed = NULL;
551 		}
552 	}
553 
554 	/* finish all descriptor reads before incrementing tail */
555 	smp_mb();
556 	ioat_chan->tail = idx + i;
557 	/* no active descs have written a completion? */
558 	BUG_ON(active && !seen_current);
559 	ioat_chan->last_completion = phys_complete;
560 
561 	if (active - i == 0) {
562 		dev_dbg(to_dev(ioat_chan), "%s: cancel completion timeout\n",
563 			__func__);
564 		mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
565 	}
566 
567 	/* 5 microsecond delay per pending descriptor */
568 	writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
569 	       ioat_chan->ioat_dma->reg_base + IOAT_INTRDELAY_OFFSET);
570 }
571 
572 static void ioat_cleanup(struct ioatdma_chan *ioat_chan)
573 {
574 	u64 phys_complete;
575 
576 	spin_lock_bh(&ioat_chan->cleanup_lock);
577 
578 	if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
579 		__cleanup(ioat_chan, phys_complete);
580 
581 	if (is_ioat_halted(*ioat_chan->completion)) {
582 		u32 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
583 
584 		if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
585 			mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
586 			ioat_eh(ioat_chan);
587 		}
588 	}
589 
590 	spin_unlock_bh(&ioat_chan->cleanup_lock);
591 }
592 
593 void ioat_cleanup_event(unsigned long data)
594 {
595 	struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
596 
597 	ioat_cleanup(ioat_chan);
598 	if (!test_bit(IOAT_RUN, &ioat_chan->state))
599 		return;
600 	writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
601 }
602 
603 static void ioat_restart_channel(struct ioatdma_chan *ioat_chan)
604 {
605 	u64 phys_complete;
606 
607 	ioat_quiesce(ioat_chan, 0);
608 	if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
609 		__cleanup(ioat_chan, phys_complete);
610 
611 	__ioat_restart_chan(ioat_chan);
612 }
613 
614 static void ioat_eh(struct ioatdma_chan *ioat_chan)
615 {
616 	struct pci_dev *pdev = to_pdev(ioat_chan);
617 	struct ioat_dma_descriptor *hw;
618 	struct dma_async_tx_descriptor *tx;
619 	u64 phys_complete;
620 	struct ioat_ring_ent *desc;
621 	u32 err_handled = 0;
622 	u32 chanerr_int;
623 	u32 chanerr;
624 
625 	/* cleanup so tail points to descriptor that caused the error */
626 	if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
627 		__cleanup(ioat_chan, phys_complete);
628 
629 	chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
630 	pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
631 
632 	dev_dbg(to_dev(ioat_chan), "%s: error = %x:%x\n",
633 		__func__, chanerr, chanerr_int);
634 
635 	desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
636 	hw = desc->hw;
637 	dump_desc_dbg(ioat_chan, desc);
638 
639 	switch (hw->ctl_f.op) {
640 	case IOAT_OP_XOR_VAL:
641 		if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
642 			*desc->result |= SUM_CHECK_P_RESULT;
643 			err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
644 		}
645 		break;
646 	case IOAT_OP_PQ_VAL:
647 	case IOAT_OP_PQ_VAL_16S:
648 		if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
649 			*desc->result |= SUM_CHECK_P_RESULT;
650 			err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
651 		}
652 		if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
653 			*desc->result |= SUM_CHECK_Q_RESULT;
654 			err_handled |= IOAT_CHANERR_XOR_Q_ERR;
655 		}
656 		break;
657 	}
658 
659 	/* fault on unhandled error or spurious halt */
660 	if (chanerr ^ err_handled || chanerr == 0) {
661 		dev_err(to_dev(ioat_chan), "%s: fatal error (%x:%x)\n",
662 			__func__, chanerr, err_handled);
663 		BUG();
664 	} else { /* cleanup the faulty descriptor */
665 		tx = &desc->txd;
666 		if (tx->cookie) {
667 			dma_cookie_complete(tx);
668 			dma_descriptor_unmap(tx);
669 			if (tx->callback) {
670 				tx->callback(tx->callback_param);
671 				tx->callback = NULL;
672 			}
673 		}
674 	}
675 
676 	writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
677 	pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
678 
679 	/* mark faulting descriptor as complete */
680 	*ioat_chan->completion = desc->txd.phys;
681 
682 	spin_lock_bh(&ioat_chan->prep_lock);
683 	ioat_restart_channel(ioat_chan);
684 	spin_unlock_bh(&ioat_chan->prep_lock);
685 }
686 
687 static void check_active(struct ioatdma_chan *ioat_chan)
688 {
689 	if (ioat_ring_active(ioat_chan)) {
690 		mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
691 		return;
692 	}
693 
694 	if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
695 		mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
696 }
697 
698 void ioat_timer_event(unsigned long data)
699 {
700 	struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
701 	dma_addr_t phys_complete;
702 	u64 status;
703 
704 	status = ioat_chansts(ioat_chan);
705 
706 	/* when halted due to errors check for channel
707 	 * programming errors before advancing the completion state
708 	 */
709 	if (is_ioat_halted(status)) {
710 		u32 chanerr;
711 
712 		chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
713 		dev_err(to_dev(ioat_chan), "%s: Channel halted (%x)\n",
714 			__func__, chanerr);
715 		if (test_bit(IOAT_RUN, &ioat_chan->state))
716 			BUG_ON(is_ioat_bug(chanerr));
717 		else /* we never got off the ground */
718 			return;
719 	}
720 
721 	/* if we haven't made progress and we have already
722 	 * acknowledged a pending completion once, then be more
723 	 * forceful with a restart
724 	 */
725 	spin_lock_bh(&ioat_chan->cleanup_lock);
726 	if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
727 		__cleanup(ioat_chan, phys_complete);
728 	else if (test_bit(IOAT_COMPLETION_ACK, &ioat_chan->state)) {
729 		spin_lock_bh(&ioat_chan->prep_lock);
730 		ioat_restart_channel(ioat_chan);
731 		spin_unlock_bh(&ioat_chan->prep_lock);
732 		spin_unlock_bh(&ioat_chan->cleanup_lock);
733 		return;
734 	} else {
735 		set_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
736 		mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
737 	}
738 
739 
740 	if (ioat_ring_active(ioat_chan))
741 		mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
742 	else {
743 		spin_lock_bh(&ioat_chan->prep_lock);
744 		check_active(ioat_chan);
745 		spin_unlock_bh(&ioat_chan->prep_lock);
746 	}
747 	spin_unlock_bh(&ioat_chan->cleanup_lock);
748 }
749 
750 enum dma_status
751 ioat_tx_status(struct dma_chan *c, dma_cookie_t cookie,
752 		struct dma_tx_state *txstate)
753 {
754 	struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
755 	enum dma_status ret;
756 
757 	ret = dma_cookie_status(c, cookie, txstate);
758 	if (ret == DMA_COMPLETE)
759 		return ret;
760 
761 	ioat_cleanup(ioat_chan);
762 
763 	return dma_cookie_status(c, cookie, txstate);
764 }
765 
766 static int ioat_irq_reinit(struct ioatdma_device *ioat_dma)
767 {
768 	struct pci_dev *pdev = ioat_dma->pdev;
769 	int irq = pdev->irq, i;
770 
771 	if (!is_bwd_ioat(pdev))
772 		return 0;
773 
774 	switch (ioat_dma->irq_mode) {
775 	case IOAT_MSIX:
776 		for (i = 0; i < ioat_dma->dma_dev.chancnt; i++) {
777 			struct msix_entry *msix = &ioat_dma->msix_entries[i];
778 			struct ioatdma_chan *ioat_chan;
779 
780 			ioat_chan = ioat_chan_by_index(ioat_dma, i);
781 			devm_free_irq(&pdev->dev, msix->vector, ioat_chan);
782 		}
783 
784 		pci_disable_msix(pdev);
785 		break;
786 	case IOAT_MSI:
787 		pci_disable_msi(pdev);
788 		/* fall through */
789 	case IOAT_INTX:
790 		devm_free_irq(&pdev->dev, irq, ioat_dma);
791 		break;
792 	default:
793 		return 0;
794 	}
795 	ioat_dma->irq_mode = IOAT_NOIRQ;
796 
797 	return ioat_dma_setup_interrupts(ioat_dma);
798 }
799 
800 int ioat_reset_hw(struct ioatdma_chan *ioat_chan)
801 {
802 	/* throw away whatever the channel was doing and get it
803 	 * initialized, with ioat3 specific workarounds
804 	 */
805 	struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
806 	struct pci_dev *pdev = ioat_dma->pdev;
807 	u32 chanerr;
808 	u16 dev_id;
809 	int err;
810 
811 	ioat_quiesce(ioat_chan, msecs_to_jiffies(100));
812 
813 	chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
814 	writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
815 
816 	if (ioat_dma->version < IOAT_VER_3_3) {
817 		/* clear any pending errors */
818 		err = pci_read_config_dword(pdev,
819 				IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
820 		if (err) {
821 			dev_err(&pdev->dev,
822 				"channel error register unreachable\n");
823 			return err;
824 		}
825 		pci_write_config_dword(pdev,
826 				IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
827 
828 		/* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
829 		 * (workaround for spurious config parity error after restart)
830 		 */
831 		pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
832 		if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
833 			pci_write_config_dword(pdev,
834 					       IOAT_PCI_DMAUNCERRSTS_OFFSET,
835 					       0x10);
836 		}
837 	}
838 
839 	err = ioat_reset_sync(ioat_chan, msecs_to_jiffies(200));
840 	if (!err)
841 		err = ioat_irq_reinit(ioat_dma);
842 
843 	if (err)
844 		dev_err(&pdev->dev, "Failed to reset: %d\n", err);
845 
846 	return err;
847 }
848