blob: 5428746f03fbb90504369ac81cdd8374a90651eb [file] [log] [blame]
Chris Leech0bbd5f42006-05-23 17:35:34 -07001/*
Shannon Nelson43d6e362007-10-16 01:27:39 -07002 * Intel I/OAT DMA Linux driver
Dave Jiang85596a12015-08-11 08:48:10 -07003 * Copyright(c) 2004 - 2015 Intel Corporation.
Chris Leech0bbd5f42006-05-23 17:35:34 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
Shannon Nelson43d6e362007-10-16 01:27:39 -07006 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
Chris Leech0bbd5f42006-05-23 17:35:34 -07008 *
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 *
Shannon Nelson43d6e362007-10-16 01:27:39 -070014 * The full GNU General Public License is included in this distribution in
15 * the file called "COPYING".
16 *
Chris Leech0bbd5f42006-05-23 17:35:34 -070017 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Chris Leech0bbd5f42006-05-23 17:35:34 -070027#include <linux/pci.h>
28#include <linux/interrupt.h>
29#include <linux/dmaengine.h>
30#include <linux/delay.h>
David S. Miller6b00c922006-05-23 17:37:58 -070031#include <linux/dma-mapping.h>
Maciej Sosnowski09177e82008-07-22 10:07:33 -070032#include <linux/workqueue.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040033#include <linux/prefetch.h>
Dave Jiangdd4645e2016-02-10 15:00:32 -070034#include <linux/sizes.h>
Dan Williams584ec222009-07-28 14:32:12 -070035#include "dma.h"
36#include "registers.h"
37#include "hw.h"
Chris Leech0bbd5f42006-05-23 17:35:34 -070038
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000039#include "../dmaengine.h"
40
Dave Jiang3372de52015-08-11 08:48:55 -070041static void ioat_eh(struct ioatdma_chan *ioat_chan);
42
Shannon Nelson3e037452007-10-16 01:27:40 -070043/**
44 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
45 * @irq: interrupt id
46 * @data: interrupt data
47 */
Dave Jiangc0f28ce2015-08-11 08:48:43 -070048irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
Shannon Nelson3e037452007-10-16 01:27:40 -070049{
50 struct ioatdma_device *instance = data;
Dave Jiang5a976882015-08-11 08:48:21 -070051 struct ioatdma_chan *ioat_chan;
Shannon Nelson3e037452007-10-16 01:27:40 -070052 unsigned long attnstatus;
53 int bit;
54 u8 intrctrl;
55
56 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
57
58 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
59 return IRQ_NONE;
60
61 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
62 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
63 return IRQ_NONE;
64 }
65
66 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
Akinobu Mita984b3f52010-03-05 13:41:37 -080067 for_each_set_bit(bit, &attnstatus, BITS_PER_LONG) {
Dave Jiang5a976882015-08-11 08:48:21 -070068 ioat_chan = ioat_chan_by_index(instance, bit);
69 if (test_bit(IOAT_RUN, &ioat_chan->state))
70 tasklet_schedule(&ioat_chan->cleanup_task);
Shannon Nelson3e037452007-10-16 01:27:40 -070071 }
72
73 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
74 return IRQ_HANDLED;
75}
76
77/**
78 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
79 * @irq: interrupt id
80 * @data: interrupt data
81 */
Dave Jiangc0f28ce2015-08-11 08:48:43 -070082irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
Shannon Nelson3e037452007-10-16 01:27:40 -070083{
Dave Jiang5a976882015-08-11 08:48:21 -070084 struct ioatdma_chan *ioat_chan = data;
Shannon Nelson3e037452007-10-16 01:27:40 -070085
Dave Jiang5a976882015-08-11 08:48:21 -070086 if (test_bit(IOAT_RUN, &ioat_chan->state))
87 tasklet_schedule(&ioat_chan->cleanup_task);
Shannon Nelson3e037452007-10-16 01:27:40 -070088
89 return IRQ_HANDLED;
90}
91
Dave Jiang5a976882015-08-11 08:48:21 -070092void ioat_stop(struct ioatdma_chan *ioat_chan)
Dan Williamsda87ca42014-02-19 16:19:35 -080093{
Dave Jiang55f878e2015-08-11 08:48:27 -070094 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
95 struct pci_dev *pdev = ioat_dma->pdev;
Dave Jiang5a976882015-08-11 08:48:21 -070096 int chan_id = chan_num(ioat_chan);
Dan Williamsda87ca42014-02-19 16:19:35 -080097 struct msix_entry *msix;
98
99 /* 1/ stop irq from firing tasklets
100 * 2/ stop the tasklet from re-arming irqs
101 */
Dave Jiang5a976882015-08-11 08:48:21 -0700102 clear_bit(IOAT_RUN, &ioat_chan->state);
Dan Williamsda87ca42014-02-19 16:19:35 -0800103
104 /* flush inflight interrupts */
Dave Jiang55f878e2015-08-11 08:48:27 -0700105 switch (ioat_dma->irq_mode) {
Dan Williamsda87ca42014-02-19 16:19:35 -0800106 case IOAT_MSIX:
Dave Jiang55f878e2015-08-11 08:48:27 -0700107 msix = &ioat_dma->msix_entries[chan_id];
Dan Williamsda87ca42014-02-19 16:19:35 -0800108 synchronize_irq(msix->vector);
109 break;
110 case IOAT_MSI:
111 case IOAT_INTX:
112 synchronize_irq(pdev->irq);
113 break;
114 default:
115 break;
116 }
117
118 /* flush inflight timers */
Dave Jiang5a976882015-08-11 08:48:21 -0700119 del_timer_sync(&ioat_chan->timer);
Dan Williamsda87ca42014-02-19 16:19:35 -0800120
121 /* flush inflight tasklet runs */
Dave Jiang5a976882015-08-11 08:48:21 -0700122 tasklet_kill(&ioat_chan->cleanup_task);
Dan Williamsda87ca42014-02-19 16:19:35 -0800123
124 /* final cleanup now that everything is quiesced and can't re-arm */
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700125 ioat_cleanup_event((unsigned long)&ioat_chan->dma_chan);
Dan Williamsda87ca42014-02-19 16:19:35 -0800126}
127
Dave Jiang3372de52015-08-11 08:48:55 -0700128static void __ioat_issue_pending(struct ioatdma_chan *ioat_chan)
Dave Jiang885b2012015-08-11 08:48:32 -0700129{
130 ioat_chan->dmacount += ioat_ring_pending(ioat_chan);
131 ioat_chan->issued = ioat_chan->head;
132 writew(ioat_chan->dmacount,
133 ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
134 dev_dbg(to_dev(ioat_chan),
135 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
136 __func__, ioat_chan->head, ioat_chan->tail,
137 ioat_chan->issued, ioat_chan->dmacount);
138}
139
140void ioat_issue_pending(struct dma_chan *c)
141{
142 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
143
144 if (ioat_ring_pending(ioat_chan)) {
145 spin_lock_bh(&ioat_chan->prep_lock);
146 __ioat_issue_pending(ioat_chan);
147 spin_unlock_bh(&ioat_chan->prep_lock);
148 }
149}
150
151/**
152 * ioat_update_pending - log pending descriptors
153 * @ioat: ioat+ channel
154 *
155 * Check if the number of unsubmitted descriptors has exceeded the
156 * watermark. Called with prep_lock held
157 */
158static void ioat_update_pending(struct ioatdma_chan *ioat_chan)
159{
160 if (ioat_ring_pending(ioat_chan) > ioat_pending_level)
161 __ioat_issue_pending(ioat_chan);
162}
163
164static void __ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
165{
166 struct ioat_ring_ent *desc;
167 struct ioat_dma_descriptor *hw;
168
169 if (ioat_ring_space(ioat_chan) < 1) {
170 dev_err(to_dev(ioat_chan),
171 "Unable to start null desc - ring full\n");
172 return;
173 }
174
175 dev_dbg(to_dev(ioat_chan),
176 "%s: head: %#x tail: %#x issued: %#x\n",
177 __func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
178 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head);
179
180 hw = desc->hw;
181 hw->ctl = 0;
182 hw->ctl_f.null = 1;
183 hw->ctl_f.int_en = 1;
184 hw->ctl_f.compl_write = 1;
185 /* set size to non-zero value (channel returns error when size is 0) */
186 hw->size = NULL_DESC_BUFFER_SIZE;
187 hw->src_addr = 0;
188 hw->dst_addr = 0;
189 async_tx_ack(&desc->txd);
190 ioat_set_chainaddr(ioat_chan, desc->txd.phys);
191 dump_desc_dbg(ioat_chan, desc);
192 /* make sure descriptors are written before we submit */
193 wmb();
194 ioat_chan->head += 1;
195 __ioat_issue_pending(ioat_chan);
196}
197
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700198void ioat_start_null_desc(struct ioatdma_chan *ioat_chan)
Dave Jiang885b2012015-08-11 08:48:32 -0700199{
200 spin_lock_bh(&ioat_chan->prep_lock);
Dave Jiangad4a7b52015-08-26 13:17:24 -0700201 if (!test_bit(IOAT_CHAN_DOWN, &ioat_chan->state))
202 __ioat_start_null_desc(ioat_chan);
Dave Jiang885b2012015-08-11 08:48:32 -0700203 spin_unlock_bh(&ioat_chan->prep_lock);
204}
205
Dave Jiang3372de52015-08-11 08:48:55 -0700206static void __ioat_restart_chan(struct ioatdma_chan *ioat_chan)
Dave Jiang885b2012015-08-11 08:48:32 -0700207{
208 /* set the tail to be re-issued */
209 ioat_chan->issued = ioat_chan->tail;
210 ioat_chan->dmacount = 0;
Dave Jiang885b2012015-08-11 08:48:32 -0700211 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
212
213 dev_dbg(to_dev(ioat_chan),
214 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
215 __func__, ioat_chan->head, ioat_chan->tail,
216 ioat_chan->issued, ioat_chan->dmacount);
217
218 if (ioat_ring_pending(ioat_chan)) {
219 struct ioat_ring_ent *desc;
220
221 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
222 ioat_set_chainaddr(ioat_chan, desc->txd.phys);
223 __ioat_issue_pending(ioat_chan);
224 } else
225 __ioat_start_null_desc(ioat_chan);
226}
227
Dave Jiang3372de52015-08-11 08:48:55 -0700228static int ioat_quiesce(struct ioatdma_chan *ioat_chan, unsigned long tmo)
Dave Jiang885b2012015-08-11 08:48:32 -0700229{
230 unsigned long end = jiffies + tmo;
231 int err = 0;
232 u32 status;
233
234 status = ioat_chansts(ioat_chan);
235 if (is_ioat_active(status) || is_ioat_idle(status))
236 ioat_suspend(ioat_chan);
237 while (is_ioat_active(status) || is_ioat_idle(status)) {
238 if (tmo && time_after(jiffies, end)) {
239 err = -ETIMEDOUT;
240 break;
241 }
242 status = ioat_chansts(ioat_chan);
243 cpu_relax();
244 }
245
246 return err;
247}
248
Dave Jiang3372de52015-08-11 08:48:55 -0700249static int ioat_reset_sync(struct ioatdma_chan *ioat_chan, unsigned long tmo)
Dave Jiang885b2012015-08-11 08:48:32 -0700250{
251 unsigned long end = jiffies + tmo;
252 int err = 0;
253
254 ioat_reset(ioat_chan);
255 while (ioat_reset_pending(ioat_chan)) {
256 if (end && time_after(jiffies, end)) {
257 err = -ETIMEDOUT;
258 break;
259 }
260 cpu_relax();
261 }
262
263 return err;
264}
265
Dave Jiang885b2012015-08-11 08:48:32 -0700266static dma_cookie_t ioat_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
Dave Jiang5c65cb92015-08-25 12:58:05 -0700267 __releases(&ioat_chan->prep_lock)
Dave Jiang885b2012015-08-11 08:48:32 -0700268{
269 struct dma_chan *c = tx->chan;
270 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
271 dma_cookie_t cookie;
272
273 cookie = dma_cookie_assign(tx);
274 dev_dbg(to_dev(ioat_chan), "%s: cookie: %d\n", __func__, cookie);
275
276 if (!test_and_set_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
277 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
278
279 /* make descriptor updates visible before advancing ioat->head,
280 * this is purposefully not smp_wmb() since we are also
281 * publishing the descriptor updates to a dma device
282 */
283 wmb();
284
285 ioat_chan->head += ioat_chan->produce;
286
287 ioat_update_pending(ioat_chan);
288 spin_unlock_bh(&ioat_chan->prep_lock);
289
290 return cookie;
291}
292
293static struct ioat_ring_ent *
Dave Jiangdd4645e2016-02-10 15:00:32 -0700294ioat_alloc_ring_ent(struct dma_chan *chan, int idx, gfp_t flags)
Dave Jiang885b2012015-08-11 08:48:32 -0700295{
296 struct ioat_dma_descriptor *hw;
297 struct ioat_ring_ent *desc;
298 struct ioatdma_device *ioat_dma;
Dave Jiangdd4645e2016-02-10 15:00:32 -0700299 struct ioatdma_chan *ioat_chan = to_ioat_chan(chan);
300 int chunk;
Dave Jiang885b2012015-08-11 08:48:32 -0700301 dma_addr_t phys;
Dave Jiangdd4645e2016-02-10 15:00:32 -0700302 u8 *pos;
303 off_t offs;
Dave Jiang885b2012015-08-11 08:48:32 -0700304
305 ioat_dma = to_ioatdma_device(chan->device);
Dave Jiangdd4645e2016-02-10 15:00:32 -0700306
307 chunk = idx / IOAT_DESCS_PER_2M;
308 idx &= (IOAT_DESCS_PER_2M - 1);
309 offs = idx * IOAT_DESC_SZ;
310 pos = (u8 *)ioat_chan->descs[chunk].virt + offs;
311 phys = ioat_chan->descs[chunk].hw + offs;
312 hw = (struct ioat_dma_descriptor *)pos;
Dave Jiang885b2012015-08-11 08:48:32 -0700313 memset(hw, 0, sizeof(*hw));
314
315 desc = kmem_cache_zalloc(ioat_cache, flags);
Dave Jiangdd4645e2016-02-10 15:00:32 -0700316 if (!desc)
Dave Jiang885b2012015-08-11 08:48:32 -0700317 return NULL;
Dave Jiang885b2012015-08-11 08:48:32 -0700318
319 dma_async_tx_descriptor_init(&desc->txd, chan);
320 desc->txd.tx_submit = ioat_tx_submit_unlock;
321 desc->hw = hw;
322 desc->txd.phys = phys;
323 return desc;
324}
325
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700326void ioat_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
Dave Jiang885b2012015-08-11 08:48:32 -0700327{
Dave Jiang885b2012015-08-11 08:48:32 -0700328 kmem_cache_free(ioat_cache, desc);
329}
330
Dave Jiangc0f28ce2015-08-11 08:48:43 -0700331struct ioat_ring_ent **
Dave Jiang885b2012015-08-11 08:48:32 -0700332ioat_alloc_ring(struct dma_chan *c, int order, gfp_t flags)
333{
Dave Jiangdd4645e2016-02-10 15:00:32 -0700334 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
Dave Jiang885b2012015-08-11 08:48:32 -0700335 struct ioat_ring_ent **ring;
Dave Jiangdd4645e2016-02-10 15:00:32 -0700336 int total_descs = 1 << order;
337 int i, chunks;
Dave Jiang885b2012015-08-11 08:48:32 -0700338
Dave Jiang885b2012015-08-11 08:48:32 -0700339 /* allocate the array to hold the software ring */
Dave Jiangdd4645e2016-02-10 15:00:32 -0700340 ring = kcalloc(total_descs, sizeof(*ring), flags);
Dave Jiang885b2012015-08-11 08:48:32 -0700341 if (!ring)
342 return NULL;
Dave Jiangdd4645e2016-02-10 15:00:32 -0700343
344 ioat_chan->desc_chunks = chunks = (total_descs * IOAT_DESC_SZ) / SZ_2M;
345
346 for (i = 0; i < chunks; i++) {
347 struct ioat_descs *descs = &ioat_chan->descs[i];
348
349 descs->virt = dma_alloc_coherent(to_dev(ioat_chan),
350 SZ_2M, &descs->hw, flags);
351 if (!descs->virt && (i > 0)) {
352 int idx;
353
354 for (idx = 0; idx < i; idx++) {
355 dma_free_coherent(to_dev(ioat_chan), SZ_2M,
356 descs->virt, descs->hw);
357 descs->virt = NULL;
358 descs->hw = 0;
359 }
360
361 ioat_chan->desc_chunks = 0;
362 kfree(ring);
363 return NULL;
364 }
365 }
366
367 for (i = 0; i < total_descs; i++) {
368 ring[i] = ioat_alloc_ring_ent(c, i, flags);
Dave Jiang885b2012015-08-11 08:48:32 -0700369 if (!ring[i]) {
Dave Jiangdd4645e2016-02-10 15:00:32 -0700370 int idx;
371
Dave Jiang885b2012015-08-11 08:48:32 -0700372 while (i--)
373 ioat_free_ring_ent(ring[i], c);
Dave Jiangdd4645e2016-02-10 15:00:32 -0700374
375 for (idx = 0; idx < ioat_chan->desc_chunks; idx++) {
376 dma_free_coherent(to_dev(ioat_chan),
377 SZ_2M,
378 ioat_chan->descs[idx].virt,
379 ioat_chan->descs[idx].hw);
380 ioat_chan->descs[idx].virt = NULL;
381 ioat_chan->descs[idx].hw = 0;
382 }
383
384 ioat_chan->desc_chunks = 0;
Dave Jiang885b2012015-08-11 08:48:32 -0700385 kfree(ring);
386 return NULL;
387 }
388 set_desc_id(ring[i], i);
389 }
390
391 /* link descs */
Dave Jiangdd4645e2016-02-10 15:00:32 -0700392 for (i = 0; i < total_descs-1; i++) {
Dave Jiang885b2012015-08-11 08:48:32 -0700393 struct ioat_ring_ent *next = ring[i+1];
394 struct ioat_dma_descriptor *hw = ring[i]->hw;
395
396 hw->next = next->txd.phys;
397 }
398 ring[i]->hw->next = ring[0]->txd.phys;
399
400 return ring;
401}
402
Dave Jiang885b2012015-08-11 08:48:32 -0700403/**
404 * ioat_check_space_lock - verify space and grab ring producer lock
405 * @ioat: ioat,3 channel (ring) to operate on
406 * @num_descs: allocation length
407 */
408int ioat_check_space_lock(struct ioatdma_chan *ioat_chan, int num_descs)
Dave Jiang5c65cb92015-08-25 12:58:05 -0700409 __acquires(&ioat_chan->prep_lock)
Dave Jiang885b2012015-08-11 08:48:32 -0700410{
Dave Jiang885b2012015-08-11 08:48:32 -0700411 spin_lock_bh(&ioat_chan->prep_lock);
412 /* never allow the last descriptor to be consumed, we need at
413 * least one free at all times to allow for on-the-fly ring
414 * resizing.
415 */
416 if (likely(ioat_ring_space(ioat_chan) > num_descs)) {
417 dev_dbg(to_dev(ioat_chan), "%s: num_descs: %d (%x:%x:%x)\n",
418 __func__, num_descs, ioat_chan->head,
419 ioat_chan->tail, ioat_chan->issued);
420 ioat_chan->produce = num_descs;
421 return 0; /* with ioat->prep_lock held */
422 }
Dave Jiang885b2012015-08-11 08:48:32 -0700423 spin_unlock_bh(&ioat_chan->prep_lock);
424
Dave Jiang885b2012015-08-11 08:48:32 -0700425 dev_dbg_ratelimited(to_dev(ioat_chan),
426 "%s: ring full! num_descs: %d (%x:%x:%x)\n",
427 __func__, num_descs, ioat_chan->head,
428 ioat_chan->tail, ioat_chan->issued);
429
430 /* progress reclaim in the allocation failure case we may be
431 * called under bh_disabled so we need to trigger the timer
432 * event directly
433 */
434 if (time_is_before_jiffies(ioat_chan->timer.expires)
435 && timer_pending(&ioat_chan->timer)) {
Dave Jiang885b2012015-08-11 08:48:32 -0700436 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
Dave Jiangef97bd0f2015-08-11 08:49:00 -0700437 ioat_timer_event((unsigned long)ioat_chan);
Dave Jiang885b2012015-08-11 08:48:32 -0700438 }
439
440 return -ENOMEM;
441}
Dave Jiang3372de52015-08-11 08:48:55 -0700442
443static bool desc_has_ext(struct ioat_ring_ent *desc)
444{
445 struct ioat_dma_descriptor *hw = desc->hw;
446
447 if (hw->ctl_f.op == IOAT_OP_XOR ||
448 hw->ctl_f.op == IOAT_OP_XOR_VAL) {
449 struct ioat_xor_descriptor *xor = desc->xor;
450
451 if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
452 return true;
453 } else if (hw->ctl_f.op == IOAT_OP_PQ ||
454 hw->ctl_f.op == IOAT_OP_PQ_VAL) {
455 struct ioat_pq_descriptor *pq = desc->pq;
456
457 if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
458 return true;
459 }
460
461 return false;
462}
463
464static void
465ioat_free_sed(struct ioatdma_device *ioat_dma, struct ioat_sed_ent *sed)
466{
467 if (!sed)
468 return;
469
470 dma_pool_free(ioat_dma->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
471 kmem_cache_free(ioat_sed_cache, sed);
472}
473
474static u64 ioat_get_current_completion(struct ioatdma_chan *ioat_chan)
475{
476 u64 phys_complete;
477 u64 completion;
478
479 completion = *ioat_chan->completion;
480 phys_complete = ioat_chansts_to_addr(completion);
481
482 dev_dbg(to_dev(ioat_chan), "%s: phys_complete: %#llx\n", __func__,
483 (unsigned long long) phys_complete);
484
485 return phys_complete;
486}
487
488static bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
489 u64 *phys_complete)
490{
491 *phys_complete = ioat_get_current_completion(ioat_chan);
492 if (*phys_complete == ioat_chan->last_completion)
493 return false;
494
495 clear_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
496 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
497
498 return true;
499}
500
501static void
502desc_get_errstat(struct ioatdma_chan *ioat_chan, struct ioat_ring_ent *desc)
503{
504 struct ioat_dma_descriptor *hw = desc->hw;
505
506 switch (hw->ctl_f.op) {
507 case IOAT_OP_PQ_VAL:
508 case IOAT_OP_PQ_VAL_16S:
509 {
510 struct ioat_pq_descriptor *pq = desc->pq;
511
512 /* check if there's error written */
513 if (!pq->dwbes_f.wbes)
514 return;
515
516 /* need to set a chanerr var for checking to clear later */
517
518 if (pq->dwbes_f.p_val_err)
519 *desc->result |= SUM_CHECK_P_RESULT;
520
521 if (pq->dwbes_f.q_val_err)
522 *desc->result |= SUM_CHECK_Q_RESULT;
523
524 return;
525 }
526 default:
527 return;
528 }
529}
530
531/**
532 * __cleanup - reclaim used descriptors
533 * @ioat: channel (ring) to clean
534 */
535static void __cleanup(struct ioatdma_chan *ioat_chan, dma_addr_t phys_complete)
536{
537 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
538 struct ioat_ring_ent *desc;
539 bool seen_current = false;
540 int idx = ioat_chan->tail, i;
541 u16 active;
542
543 dev_dbg(to_dev(ioat_chan), "%s: head: %#x tail: %#x issued: %#x\n",
544 __func__, ioat_chan->head, ioat_chan->tail, ioat_chan->issued);
545
546 /*
547 * At restart of the channel, the completion address and the
548 * channel status will be 0 due to starting a new chain. Since
549 * it's new chain and the first descriptor "fails", there is
550 * nothing to clean up. We do not want to reap the entire submitted
551 * chain due to this 0 address value and then BUG.
552 */
553 if (!phys_complete)
554 return;
555
556 active = ioat_ring_active(ioat_chan);
557 for (i = 0; i < active && !seen_current; i++) {
558 struct dma_async_tx_descriptor *tx;
559
560 smp_read_barrier_depends();
561 prefetch(ioat_get_ring_ent(ioat_chan, idx + i + 1));
562 desc = ioat_get_ring_ent(ioat_chan, idx + i);
563 dump_desc_dbg(ioat_chan, desc);
564
565 /* set err stat if we are using dwbes */
566 if (ioat_dma->cap & IOAT_CAP_DWBES)
567 desc_get_errstat(ioat_chan, desc);
568
569 tx = &desc->txd;
570 if (tx->cookie) {
571 dma_cookie_complete(tx);
572 dma_descriptor_unmap(tx);
573 if (tx->callback) {
574 tx->callback(tx->callback_param);
575 tx->callback = NULL;
576 }
577 }
578
579 if (tx->phys == phys_complete)
580 seen_current = true;
581
582 /* skip extended descriptors */
583 if (desc_has_ext(desc)) {
584 BUG_ON(i + 1 >= active);
585 i++;
586 }
587
588 /* cleanup super extended descriptors */
589 if (desc->sed) {
590 ioat_free_sed(ioat_dma, desc->sed);
591 desc->sed = NULL;
592 }
593 }
594
595 /* finish all descriptor reads before incrementing tail */
596 smp_mb();
597 ioat_chan->tail = idx + i;
598 /* no active descs have written a completion? */
599 BUG_ON(active && !seen_current);
600 ioat_chan->last_completion = phys_complete;
601
602 if (active - i == 0) {
603 dev_dbg(to_dev(ioat_chan), "%s: cancel completion timeout\n",
604 __func__);
Dave Jiang3372de52015-08-11 08:48:55 -0700605 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
606 }
607
608 /* 5 microsecond delay per pending descriptor */
609 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
610 ioat_chan->ioat_dma->reg_base + IOAT_INTRDELAY_OFFSET);
611}
612
613static void ioat_cleanup(struct ioatdma_chan *ioat_chan)
614{
615 u64 phys_complete;
616
617 spin_lock_bh(&ioat_chan->cleanup_lock);
618
619 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
620 __cleanup(ioat_chan, phys_complete);
621
622 if (is_ioat_halted(*ioat_chan->completion)) {
623 u32 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
624
625 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
626 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
627 ioat_eh(ioat_chan);
628 }
629 }
630
631 spin_unlock_bh(&ioat_chan->cleanup_lock);
632}
633
634void ioat_cleanup_event(unsigned long data)
635{
636 struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
637
638 ioat_cleanup(ioat_chan);
639 if (!test_bit(IOAT_RUN, &ioat_chan->state))
640 return;
641 writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
642}
643
644static void ioat_restart_channel(struct ioatdma_chan *ioat_chan)
645{
646 u64 phys_complete;
647
648 ioat_quiesce(ioat_chan, 0);
649 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
650 __cleanup(ioat_chan, phys_complete);
651
652 __ioat_restart_chan(ioat_chan);
653}
654
655static void ioat_eh(struct ioatdma_chan *ioat_chan)
656{
657 struct pci_dev *pdev = to_pdev(ioat_chan);
658 struct ioat_dma_descriptor *hw;
659 struct dma_async_tx_descriptor *tx;
660 u64 phys_complete;
661 struct ioat_ring_ent *desc;
662 u32 err_handled = 0;
663 u32 chanerr_int;
664 u32 chanerr;
665
666 /* cleanup so tail points to descriptor that caused the error */
667 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
668 __cleanup(ioat_chan, phys_complete);
669
670 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
671 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
672
673 dev_dbg(to_dev(ioat_chan), "%s: error = %x:%x\n",
674 __func__, chanerr, chanerr_int);
675
676 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
677 hw = desc->hw;
678 dump_desc_dbg(ioat_chan, desc);
679
680 switch (hw->ctl_f.op) {
681 case IOAT_OP_XOR_VAL:
682 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
683 *desc->result |= SUM_CHECK_P_RESULT;
684 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
685 }
686 break;
687 case IOAT_OP_PQ_VAL:
688 case IOAT_OP_PQ_VAL_16S:
689 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
690 *desc->result |= SUM_CHECK_P_RESULT;
691 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
692 }
693 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
694 *desc->result |= SUM_CHECK_Q_RESULT;
695 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
696 }
697 break;
698 }
699
700 /* fault on unhandled error or spurious halt */
701 if (chanerr ^ err_handled || chanerr == 0) {
702 dev_err(to_dev(ioat_chan), "%s: fatal error (%x:%x)\n",
703 __func__, chanerr, err_handled);
704 BUG();
705 } else { /* cleanup the faulty descriptor */
706 tx = &desc->txd;
707 if (tx->cookie) {
708 dma_cookie_complete(tx);
709 dma_descriptor_unmap(tx);
710 if (tx->callback) {
711 tx->callback(tx->callback_param);
712 tx->callback = NULL;
713 }
714 }
715 }
716
717 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
718 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
719
720 /* mark faulting descriptor as complete */
721 *ioat_chan->completion = desc->txd.phys;
722
723 spin_lock_bh(&ioat_chan->prep_lock);
724 ioat_restart_channel(ioat_chan);
725 spin_unlock_bh(&ioat_chan->prep_lock);
726}
727
728static void check_active(struct ioatdma_chan *ioat_chan)
729{
730 if (ioat_ring_active(ioat_chan)) {
731 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
732 return;
733 }
734
735 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
736 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
Dave Jiang3372de52015-08-11 08:48:55 -0700737}
738
739void ioat_timer_event(unsigned long data)
740{
741 struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
742 dma_addr_t phys_complete;
743 u64 status;
744
745 status = ioat_chansts(ioat_chan);
746
747 /* when halted due to errors check for channel
748 * programming errors before advancing the completion state
749 */
750 if (is_ioat_halted(status)) {
751 u32 chanerr;
752
753 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
754 dev_err(to_dev(ioat_chan), "%s: Channel halted (%x)\n",
755 __func__, chanerr);
756 if (test_bit(IOAT_RUN, &ioat_chan->state))
757 BUG_ON(is_ioat_bug(chanerr));
758 else /* we never got off the ground */
759 return;
760 }
761
762 /* if we haven't made progress and we have already
763 * acknowledged a pending completion once, then be more
764 * forceful with a restart
765 */
766 spin_lock_bh(&ioat_chan->cleanup_lock);
767 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
768 __cleanup(ioat_chan, phys_complete);
769 else if (test_bit(IOAT_COMPLETION_ACK, &ioat_chan->state)) {
770 spin_lock_bh(&ioat_chan->prep_lock);
771 ioat_restart_channel(ioat_chan);
772 spin_unlock_bh(&ioat_chan->prep_lock);
773 spin_unlock_bh(&ioat_chan->cleanup_lock);
774 return;
775 } else {
776 set_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
777 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
778 }
779
780
781 if (ioat_ring_active(ioat_chan))
782 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
783 else {
784 spin_lock_bh(&ioat_chan->prep_lock);
785 check_active(ioat_chan);
786 spin_unlock_bh(&ioat_chan->prep_lock);
787 }
788 spin_unlock_bh(&ioat_chan->cleanup_lock);
789}
790
791enum dma_status
792ioat_tx_status(struct dma_chan *c, dma_cookie_t cookie,
793 struct dma_tx_state *txstate)
794{
795 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
796 enum dma_status ret;
797
798 ret = dma_cookie_status(c, cookie, txstate);
799 if (ret == DMA_COMPLETE)
800 return ret;
801
802 ioat_cleanup(ioat_chan);
803
804 return dma_cookie_status(c, cookie, txstate);
805}
806
Dave Jiang3372de52015-08-11 08:48:55 -0700807int ioat_reset_hw(struct ioatdma_chan *ioat_chan)
808{
809 /* throw away whatever the channel was doing and get it
810 * initialized, with ioat3 specific workarounds
811 */
812 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
813 struct pci_dev *pdev = ioat_dma->pdev;
814 u32 chanerr;
815 u16 dev_id;
816 int err;
817
818 ioat_quiesce(ioat_chan, msecs_to_jiffies(100));
819
820 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
821 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
822
823 if (ioat_dma->version < IOAT_VER_3_3) {
824 /* clear any pending errors */
825 err = pci_read_config_dword(pdev,
826 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
827 if (err) {
828 dev_err(&pdev->dev,
829 "channel error register unreachable\n");
830 return err;
831 }
832 pci_write_config_dword(pdev,
833 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
834
835 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
836 * (workaround for spurious config parity error after restart)
837 */
838 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
839 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
840 pci_write_config_dword(pdev,
841 IOAT_PCI_DMAUNCERRSTS_OFFSET,
842 0x10);
843 }
844 }
845
Dave Jiangc997e302016-03-10 16:18:40 -0700846 if (is_bwd_ioat(pdev) && (ioat_dma->irq_mode == IOAT_MSIX)) {
847 ioat_dma->msixtba0 = readq(ioat_dma->reg_base + 0x1000);
848 ioat_dma->msixdata0 = readq(ioat_dma->reg_base + 0x1008);
849 ioat_dma->msixpba = readq(ioat_dma->reg_base + 0x1800);
850 }
851
852
Dave Jiang3372de52015-08-11 08:48:55 -0700853 err = ioat_reset_sync(ioat_chan, msecs_to_jiffies(200));
Dave Jiangc997e302016-03-10 16:18:40 -0700854 if (!err) {
855 if (is_bwd_ioat(pdev) && (ioat_dma->irq_mode == IOAT_MSIX)) {
856 writeq(ioat_dma->msixtba0, ioat_dma->reg_base + 0x1000);
857 writeq(ioat_dma->msixdata0, ioat_dma->reg_base + 0x1008);
858 writeq(ioat_dma->msixpba, ioat_dma->reg_base + 0x1800);
859 }
860 }
Dave Jiang3372de52015-08-11 08:48:55 -0700861
862 if (err)
863 dev_err(&pdev->dev, "Failed to reset: %d\n", err);
864
865 return err;
866}