blob: 6499de4b8e799ca0d43c0657fed0f2c5c22021d3 [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
339 /* 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);
Dave Jiang63992862016-07-20 13:11:33 -0700573 dmaengine_desc_get_callback_invoke(tx, NULL);
574 tx->callback = NULL;
Dave Jiang3372de52015-08-11 08:48:55 -0700575 }
576
577 if (tx->phys == phys_complete)
578 seen_current = true;
579
580 /* skip extended descriptors */
581 if (desc_has_ext(desc)) {
582 BUG_ON(i + 1 >= active);
583 i++;
584 }
585
586 /* cleanup super extended descriptors */
587 if (desc->sed) {
588 ioat_free_sed(ioat_dma, desc->sed);
589 desc->sed = NULL;
590 }
591 }
592
593 /* finish all descriptor reads before incrementing tail */
594 smp_mb();
595 ioat_chan->tail = idx + i;
596 /* no active descs have written a completion? */
597 BUG_ON(active && !seen_current);
598 ioat_chan->last_completion = phys_complete;
599
600 if (active - i == 0) {
601 dev_dbg(to_dev(ioat_chan), "%s: cancel completion timeout\n",
602 __func__);
Dave Jiang3372de52015-08-11 08:48:55 -0700603 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
604 }
605
606 /* 5 microsecond delay per pending descriptor */
607 writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
608 ioat_chan->ioat_dma->reg_base + IOAT_INTRDELAY_OFFSET);
609}
610
611static void ioat_cleanup(struct ioatdma_chan *ioat_chan)
612{
613 u64 phys_complete;
614
615 spin_lock_bh(&ioat_chan->cleanup_lock);
616
617 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
618 __cleanup(ioat_chan, phys_complete);
619
620 if (is_ioat_halted(*ioat_chan->completion)) {
621 u32 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
622
623 if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
624 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
625 ioat_eh(ioat_chan);
626 }
627 }
628
629 spin_unlock_bh(&ioat_chan->cleanup_lock);
630}
631
632void ioat_cleanup_event(unsigned long data)
633{
634 struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
635
636 ioat_cleanup(ioat_chan);
637 if (!test_bit(IOAT_RUN, &ioat_chan->state))
638 return;
639 writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
640}
641
642static void ioat_restart_channel(struct ioatdma_chan *ioat_chan)
643{
644 u64 phys_complete;
645
646 ioat_quiesce(ioat_chan, 0);
647 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
648 __cleanup(ioat_chan, phys_complete);
649
650 __ioat_restart_chan(ioat_chan);
651}
652
653static void ioat_eh(struct ioatdma_chan *ioat_chan)
654{
655 struct pci_dev *pdev = to_pdev(ioat_chan);
656 struct ioat_dma_descriptor *hw;
657 struct dma_async_tx_descriptor *tx;
658 u64 phys_complete;
659 struct ioat_ring_ent *desc;
660 u32 err_handled = 0;
661 u32 chanerr_int;
662 u32 chanerr;
663
664 /* cleanup so tail points to descriptor that caused the error */
665 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
666 __cleanup(ioat_chan, phys_complete);
667
668 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
669 pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
670
671 dev_dbg(to_dev(ioat_chan), "%s: error = %x:%x\n",
672 __func__, chanerr, chanerr_int);
673
674 desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail);
675 hw = desc->hw;
676 dump_desc_dbg(ioat_chan, desc);
677
678 switch (hw->ctl_f.op) {
679 case IOAT_OP_XOR_VAL:
680 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
681 *desc->result |= SUM_CHECK_P_RESULT;
682 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
683 }
684 break;
685 case IOAT_OP_PQ_VAL:
686 case IOAT_OP_PQ_VAL_16S:
687 if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
688 *desc->result |= SUM_CHECK_P_RESULT;
689 err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
690 }
691 if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
692 *desc->result |= SUM_CHECK_Q_RESULT;
693 err_handled |= IOAT_CHANERR_XOR_Q_ERR;
694 }
695 break;
696 }
697
698 /* fault on unhandled error or spurious halt */
699 if (chanerr ^ err_handled || chanerr == 0) {
700 dev_err(to_dev(ioat_chan), "%s: fatal error (%x:%x)\n",
701 __func__, chanerr, err_handled);
702 BUG();
703 } else { /* cleanup the faulty descriptor */
704 tx = &desc->txd;
705 if (tx->cookie) {
706 dma_cookie_complete(tx);
707 dma_descriptor_unmap(tx);
Dave Jiang63992862016-07-20 13:11:33 -0700708 dmaengine_desc_get_callback_invoke(tx, NULL);
709 tx->callback = NULL;
Dave Jiang3372de52015-08-11 08:48:55 -0700710 }
711 }
712
713 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
714 pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
715
716 /* mark faulting descriptor as complete */
717 *ioat_chan->completion = desc->txd.phys;
718
719 spin_lock_bh(&ioat_chan->prep_lock);
720 ioat_restart_channel(ioat_chan);
721 spin_unlock_bh(&ioat_chan->prep_lock);
722}
723
724static void check_active(struct ioatdma_chan *ioat_chan)
725{
726 if (ioat_ring_active(ioat_chan)) {
727 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
728 return;
729 }
730
731 if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &ioat_chan->state))
732 mod_timer(&ioat_chan->timer, jiffies + IDLE_TIMEOUT);
Dave Jiang3372de52015-08-11 08:48:55 -0700733}
734
735void ioat_timer_event(unsigned long data)
736{
737 struct ioatdma_chan *ioat_chan = to_ioat_chan((void *)data);
738 dma_addr_t phys_complete;
739 u64 status;
740
741 status = ioat_chansts(ioat_chan);
742
743 /* when halted due to errors check for channel
744 * programming errors before advancing the completion state
745 */
746 if (is_ioat_halted(status)) {
747 u32 chanerr;
748
749 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
750 dev_err(to_dev(ioat_chan), "%s: Channel halted (%x)\n",
751 __func__, chanerr);
752 if (test_bit(IOAT_RUN, &ioat_chan->state))
753 BUG_ON(is_ioat_bug(chanerr));
754 else /* we never got off the ground */
755 return;
756 }
757
Dave Jiang8a695db2016-01-19 08:57:48 -0700758 spin_lock_bh(&ioat_chan->cleanup_lock);
759
760 /* handle the no-actives case */
761 if (!ioat_ring_active(ioat_chan)) {
762 spin_lock_bh(&ioat_chan->prep_lock);
763 check_active(ioat_chan);
764 spin_unlock_bh(&ioat_chan->prep_lock);
765 spin_unlock_bh(&ioat_chan->cleanup_lock);
766 return;
767 }
768
Dave Jiang3372de52015-08-11 08:48:55 -0700769 /* if we haven't made progress and we have already
770 * acknowledged a pending completion once, then be more
771 * forceful with a restart
772 */
Dave Jiang3372de52015-08-11 08:48:55 -0700773 if (ioat_cleanup_preamble(ioat_chan, &phys_complete))
774 __cleanup(ioat_chan, phys_complete);
775 else if (test_bit(IOAT_COMPLETION_ACK, &ioat_chan->state)) {
Dave Jiang8a695db2016-01-19 08:57:48 -0700776 u32 chanerr;
777
778 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
779 dev_warn(to_dev(ioat_chan), "Restarting channel...\n");
780 dev_warn(to_dev(ioat_chan), "CHANSTS: %#Lx CHANERR: %#x\n",
781 status, chanerr);
782 dev_warn(to_dev(ioat_chan), "Active descriptors: %d\n",
783 ioat_ring_active(ioat_chan));
784
Dave Jiang3372de52015-08-11 08:48:55 -0700785 spin_lock_bh(&ioat_chan->prep_lock);
786 ioat_restart_channel(ioat_chan);
787 spin_unlock_bh(&ioat_chan->prep_lock);
788 spin_unlock_bh(&ioat_chan->cleanup_lock);
789 return;
Dave Jiang8a695db2016-01-19 08:57:48 -0700790 } else
Dave Jiang3372de52015-08-11 08:48:55 -0700791 set_bit(IOAT_COMPLETION_ACK, &ioat_chan->state);
Dave Jiang3372de52015-08-11 08:48:55 -0700792
Dave Jiang8a695db2016-01-19 08:57:48 -0700793 mod_timer(&ioat_chan->timer, jiffies + COMPLETION_TIMEOUT);
Dave Jiang3372de52015-08-11 08:48:55 -0700794 spin_unlock_bh(&ioat_chan->cleanup_lock);
795}
796
797enum dma_status
798ioat_tx_status(struct dma_chan *c, dma_cookie_t cookie,
799 struct dma_tx_state *txstate)
800{
801 struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
802 enum dma_status ret;
803
804 ret = dma_cookie_status(c, cookie, txstate);
805 if (ret == DMA_COMPLETE)
806 return ret;
807
808 ioat_cleanup(ioat_chan);
809
810 return dma_cookie_status(c, cookie, txstate);
811}
812
Dave Jiang3372de52015-08-11 08:48:55 -0700813int ioat_reset_hw(struct ioatdma_chan *ioat_chan)
814{
815 /* throw away whatever the channel was doing and get it
816 * initialized, with ioat3 specific workarounds
817 */
818 struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
819 struct pci_dev *pdev = ioat_dma->pdev;
820 u32 chanerr;
821 u16 dev_id;
822 int err;
823
824 ioat_quiesce(ioat_chan, msecs_to_jiffies(100));
825
826 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
827 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
828
829 if (ioat_dma->version < IOAT_VER_3_3) {
830 /* clear any pending errors */
831 err = pci_read_config_dword(pdev,
832 IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
833 if (err) {
834 dev_err(&pdev->dev,
835 "channel error register unreachable\n");
836 return err;
837 }
838 pci_write_config_dword(pdev,
839 IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
840
841 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
842 * (workaround for spurious config parity error after restart)
843 */
844 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
845 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
846 pci_write_config_dword(pdev,
847 IOAT_PCI_DMAUNCERRSTS_OFFSET,
848 0x10);
849 }
850 }
851
Dave Jiangc997e302016-03-10 16:18:40 -0700852 if (is_bwd_ioat(pdev) && (ioat_dma->irq_mode == IOAT_MSIX)) {
853 ioat_dma->msixtba0 = readq(ioat_dma->reg_base + 0x1000);
854 ioat_dma->msixdata0 = readq(ioat_dma->reg_base + 0x1008);
855 ioat_dma->msixpba = readq(ioat_dma->reg_base + 0x1800);
856 }
857
858
Dave Jiang3372de52015-08-11 08:48:55 -0700859 err = ioat_reset_sync(ioat_chan, msecs_to_jiffies(200));
Dave Jiangc997e302016-03-10 16:18:40 -0700860 if (!err) {
861 if (is_bwd_ioat(pdev) && (ioat_dma->irq_mode == IOAT_MSIX)) {
862 writeq(ioat_dma->msixtba0, ioat_dma->reg_base + 0x1000);
863 writeq(ioat_dma->msixdata0, ioat_dma->reg_base + 0x1008);
864 writeq(ioat_dma->msixpba, ioat_dma->reg_base + 0x1800);
865 }
866 }
Dave Jiang3372de52015-08-11 08:48:55 -0700867
868 if (err)
869 dev_err(&pdev->dev, "Failed to reset: %d\n", err);
870
871 return err;
872}