ioat: cleanup completion status reads
[linux-flexiantxendom0.git] / drivers / dma / ioat / dma_v2.c
1 /*
2  * Intel I/OAT DMA Linux driver
3  * Copyright(c) 2004 - 2009 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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  */
22
23 /*
24  * This driver supports an Intel I/OAT DMA engine (versions >= 2), which
25  * does asynchronous data movement and checksumming operations.
26  */
27
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/dmaengine.h>
33 #include <linux/delay.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/workqueue.h>
36 #include <linux/i7300_idle.h>
37 #include "dma.h"
38 #include "dma_v2.h"
39 #include "registers.h"
40 #include "hw.h"
41
42 static int ioat_ring_alloc_order = 8;
43 module_param(ioat_ring_alloc_order, int, 0644);
44 MODULE_PARM_DESC(ioat_ring_alloc_order,
45                  "ioat2+: allocate 2^n descriptors per channel (default: n=8)");
46
47 static void __ioat2_issue_pending(struct ioat2_dma_chan *ioat)
48 {
49         void * __iomem reg_base = ioat->base.reg_base;
50
51         ioat->pending = 0;
52         ioat->dmacount += ioat2_ring_pending(ioat);
53         ioat->issued = ioat->head;
54         /* make descriptor updates globally visible before notifying channel */
55         wmb();
56         writew(ioat->dmacount, reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
57         dev_dbg(to_dev(&ioat->base),
58                 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
59                 __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
60 }
61
62 static void ioat2_issue_pending(struct dma_chan *chan)
63 {
64         struct ioat2_dma_chan *ioat = to_ioat2_chan(chan);
65
66         spin_lock_bh(&ioat->ring_lock);
67         if (ioat->pending == 1)
68                 __ioat2_issue_pending(ioat);
69         spin_unlock_bh(&ioat->ring_lock);
70 }
71
72 /**
73  * ioat2_update_pending - log pending descriptors
74  * @ioat: ioat2+ channel
75  *
76  * set pending to '1' unless pending is already set to '2', pending == 2
77  * indicates that submission is temporarily blocked due to an in-flight
78  * reset.  If we are already above the ioat_pending_level threshold then
79  * just issue pending.
80  *
81  * called with ring_lock held
82  */
83 static void ioat2_update_pending(struct ioat2_dma_chan *ioat)
84 {
85         if (unlikely(ioat->pending == 2))
86                 return;
87         else if (ioat2_ring_pending(ioat) > ioat_pending_level)
88                 __ioat2_issue_pending(ioat);
89         else
90                 ioat->pending = 1;
91 }
92
93 static void __ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
94 {
95         void __iomem *reg_base = ioat->base.reg_base;
96         struct ioat_ring_ent *desc;
97         struct ioat_dma_descriptor *hw;
98         int idx;
99
100         if (ioat2_ring_space(ioat) < 1) {
101                 dev_err(to_dev(&ioat->base),
102                         "Unable to start null desc - ring full\n");
103                 return;
104         }
105
106         dev_dbg(to_dev(&ioat->base), "%s: head: %#x tail: %#x issued: %#x\n",
107                 __func__, ioat->head, ioat->tail, ioat->issued);
108         idx = ioat2_desc_alloc(ioat, 1);
109         desc = ioat2_get_ring_ent(ioat, idx);
110
111         hw = desc->hw;
112         hw->ctl = 0;
113         hw->ctl_f.null = 1;
114         hw->ctl_f.int_en = 1;
115         hw->ctl_f.compl_write = 1;
116         /* set size to non-zero value (channel returns error when size is 0) */
117         hw->size = NULL_DESC_BUFFER_SIZE;
118         hw->src_addr = 0;
119         hw->dst_addr = 0;
120         async_tx_ack(&desc->txd);
121         writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
122                reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
123         writel(((u64) desc->txd.phys) >> 32,
124                reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
125         dump_desc_dbg(ioat, desc);
126         __ioat2_issue_pending(ioat);
127 }
128
129 static void ioat2_start_null_desc(struct ioat2_dma_chan *ioat)
130 {
131         spin_lock_bh(&ioat->ring_lock);
132         __ioat2_start_null_desc(ioat);
133         spin_unlock_bh(&ioat->ring_lock);
134 }
135
136 static void ioat2_cleanup(struct ioat2_dma_chan *ioat);
137
138 /**
139  * ioat2_reset_part2 - reinit the channel after a reset
140  */
141 static void ioat2_reset_part2(struct work_struct *work)
142 {
143         struct ioat_chan_common *chan;
144         struct ioat2_dma_chan *ioat;
145
146         chan = container_of(work, struct ioat_chan_common, work.work);
147         ioat = container_of(chan, struct ioat2_dma_chan, base);
148
149         /* ensure that ->tail points to the stalled descriptor
150          * (ioat->pending is set to 2 at this point so no new
151          * descriptors will be issued while we perform this cleanup)
152          */
153         ioat2_cleanup(ioat);
154
155         spin_lock_bh(&chan->cleanup_lock);
156         spin_lock_bh(&ioat->ring_lock);
157
158         /* set the tail to be re-issued */
159         ioat->issued = ioat->tail;
160         ioat->dmacount = 0;
161
162         dev_dbg(to_dev(&ioat->base),
163                 "%s: head: %#x tail: %#x issued: %#x count: %#x\n",
164                 __func__, ioat->head, ioat->tail, ioat->issued, ioat->dmacount);
165
166         if (ioat2_ring_pending(ioat)) {
167                 struct ioat_ring_ent *desc;
168
169                 desc = ioat2_get_ring_ent(ioat, ioat->tail);
170                 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
171                        chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
172                 writel(((u64) desc->txd.phys) >> 32,
173                        chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
174                 __ioat2_issue_pending(ioat);
175         } else
176                 __ioat2_start_null_desc(ioat);
177
178         spin_unlock_bh(&ioat->ring_lock);
179         spin_unlock_bh(&chan->cleanup_lock);
180
181         dev_info(to_dev(chan),
182                  "chan%d reset - %d descs waiting, %d total desc\n",
183                  chan_num(chan), ioat->dmacount, 1 << ioat->alloc_order);
184 }
185
186 /**
187  * ioat2_reset_channel - restart a channel
188  * @ioat: IOAT DMA channel handle
189  */
190 static void ioat2_reset_channel(struct ioat2_dma_chan *ioat)
191 {
192         u32 chansts, chanerr;
193         struct ioat_chan_common *chan = &ioat->base;
194         u16 active;
195
196         spin_lock_bh(&ioat->ring_lock);
197         active = ioat2_ring_active(ioat);
198         spin_unlock_bh(&ioat->ring_lock);
199         if (!active)
200                 return;
201
202         chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
203         chansts = *chan->completion & IOAT_CHANSTS_DMA_TRANSFER_STATUS;
204         if (chanerr) {
205                 dev_err(to_dev(chan),
206                         "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
207                         chan_num(chan), chansts, chanerr);
208                 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
209         }
210
211         spin_lock_bh(&ioat->ring_lock);
212         ioat->pending = 2;
213         writeb(IOAT_CHANCMD_RESET,
214                chan->reg_base
215                + IOAT_CHANCMD_OFFSET(chan->device->version));
216         spin_unlock_bh(&ioat->ring_lock);
217         schedule_delayed_work(&chan->work, RESET_DELAY);
218 }
219
220 /**
221  * ioat2_chan_watchdog - watch for stuck channels
222  */
223 static void ioat2_chan_watchdog(struct work_struct *work)
224 {
225         struct ioatdma_device *device =
226                 container_of(work, struct ioatdma_device, work.work);
227         struct ioat2_dma_chan *ioat;
228         struct ioat_chan_common *chan;
229         u16 active;
230         int i;
231
232         dev_dbg(&device->pdev->dev, "%s\n", __func__);
233
234         for (i = 0; i < device->common.chancnt; i++) {
235                 chan = ioat_chan_by_index(device, i);
236                 ioat = container_of(chan, struct ioat2_dma_chan, base);
237
238                 /*
239                  * for version 2.0 if there are descriptors yet to be processed
240                  * and the last completed hasn't changed since the last watchdog
241                  *      if they haven't hit the pending level
242                  *          issue the pending to push them through
243                  *      else
244                  *          try resetting the channel
245                  */
246                 spin_lock_bh(&ioat->ring_lock);
247                 active = ioat2_ring_active(ioat);
248                 spin_unlock_bh(&ioat->ring_lock);
249
250                 if (active &&
251                     chan->last_completion &&
252                     chan->last_completion == chan->watchdog_completion) {
253
254                         if (ioat->pending == 1)
255                                 ioat2_issue_pending(&chan->common);
256                         else {
257                                 ioat2_reset_channel(ioat);
258                                 chan->watchdog_completion = 0;
259                         }
260                 } else {
261                         chan->last_compl_desc_addr_hw = 0;
262                         chan->watchdog_completion = chan->last_completion;
263                 }
264                 chan->watchdog_last_tcp_cookie = chan->watchdog_tcp_cookie;
265         }
266         schedule_delayed_work(&device->work, WATCHDOG_DELAY);
267 }
268
269 /**
270  * ioat2_cleanup - clean finished descriptors (advance tail pointer)
271  * @chan: ioat channel to be cleaned up
272  */
273 static void ioat2_cleanup(struct ioat2_dma_chan *ioat)
274 {
275         struct ioat_chan_common *chan = &ioat->base;
276         unsigned long phys_complete;
277         struct ioat_ring_ent *desc;
278         bool seen_current = false;
279         u16 active;
280         int i;
281         struct dma_async_tx_descriptor *tx;
282
283         prefetch(chan->completion);
284
285         spin_lock_bh(&chan->cleanup_lock);
286         phys_complete = ioat_get_current_completion(chan);
287         if (phys_complete == chan->last_completion) {
288                 spin_unlock_bh(&chan->cleanup_lock);
289                 /*
290                  * perhaps we're stuck so hard that the watchdog can't go off?
291                  * try to catch it after WATCHDOG_DELAY seconds
292                  */
293                 if (chan->device->version < IOAT_VER_3_0) {
294                         unsigned long tmo;
295
296                         tmo = chan->last_completion_time + HZ*WATCHDOG_DELAY;
297                         if (time_after(jiffies, tmo)) {
298                                 ioat2_chan_watchdog(&(chan->device->work.work));
299                                 chan->last_completion_time = jiffies;
300                         }
301                 }
302                 return;
303         }
304         chan->last_completion_time = jiffies;
305
306         spin_lock_bh(&ioat->ring_lock);
307
308         dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
309                 __func__, ioat->head, ioat->tail, ioat->issued);
310
311         active = ioat2_ring_active(ioat);
312         for (i = 0; i < active && !seen_current; i++) {
313                 prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
314                 desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
315                 tx = &desc->txd;
316                 dump_desc_dbg(ioat, desc);
317                 if (tx->cookie) {
318                         ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
319                         chan->completed_cookie = tx->cookie;
320                         tx->cookie = 0;
321                         if (tx->callback) {
322                                 tx->callback(tx->callback_param);
323                                 tx->callback = NULL;
324                         }
325                 }
326
327                 if (tx->phys == phys_complete)
328                         seen_current = true;
329         }
330         ioat->tail += i;
331         BUG_ON(!seen_current); /* no active descs have written a completion? */
332         spin_unlock_bh(&ioat->ring_lock);
333
334         chan->last_completion = phys_complete;
335
336         spin_unlock_bh(&chan->cleanup_lock);
337 }
338
339 static void ioat2_cleanup_tasklet(unsigned long data)
340 {
341         struct ioat2_dma_chan *ioat = (void *) data;
342
343         ioat2_cleanup(ioat);
344         writew(IOAT_CHANCTRL_INT_DISABLE,
345                ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
346 }
347
348 /**
349  * ioat2_enumerate_channels - find and initialize the device's channels
350  * @device: the device to be enumerated
351  */
352 static int ioat2_enumerate_channels(struct ioatdma_device *device)
353 {
354         struct ioat2_dma_chan *ioat;
355         struct device *dev = &device->pdev->dev;
356         struct dma_device *dma = &device->common;
357         u8 xfercap_log;
358         int i;
359
360         INIT_LIST_HEAD(&dma->channels);
361         dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
362         xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
363         if (xfercap_log == 0)
364                 return 0;
365         dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
366
367         /* FIXME which i/oat version is i7300? */
368 #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
369         if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
370                 dma->chancnt--;
371 #endif
372         for (i = 0; i < dma->chancnt; i++) {
373                 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
374                 if (!ioat)
375                         break;
376
377                 ioat_init_channel(device, &ioat->base, i,
378                                   ioat2_reset_part2,
379                                   ioat2_cleanup_tasklet,
380                                   (unsigned long) ioat);
381                 ioat->xfercap_log = xfercap_log;
382                 spin_lock_init(&ioat->ring_lock);
383         }
384         dma->chancnt = i;
385         return i;
386 }
387
388 static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
389 {
390         struct dma_chan *c = tx->chan;
391         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
392         dma_cookie_t cookie = c->cookie;
393
394         cookie++;
395         if (cookie < 0)
396                 cookie = 1;
397         tx->cookie = cookie;
398         c->cookie = cookie;
399         dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
400
401         ioat2_update_pending(ioat);
402         spin_unlock_bh(&ioat->ring_lock);
403
404         return cookie;
405 }
406
407 static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan)
408 {
409         struct ioat_dma_descriptor *hw;
410         struct ioat_ring_ent *desc;
411         struct ioatdma_device *dma;
412         dma_addr_t phys;
413
414         dma = to_ioatdma_device(chan->device);
415         hw = pci_pool_alloc(dma->dma_pool, GFP_KERNEL, &phys);
416         if (!hw)
417                 return NULL;
418         memset(hw, 0, sizeof(*hw));
419
420         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
421         if (!desc) {
422                 pci_pool_free(dma->dma_pool, hw, phys);
423                 return NULL;
424         }
425
426         dma_async_tx_descriptor_init(&desc->txd, chan);
427         desc->txd.tx_submit = ioat2_tx_submit_unlock;
428         desc->hw = hw;
429         desc->txd.phys = phys;
430         return desc;
431 }
432
433 static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
434 {
435         struct ioatdma_device *dma;
436
437         dma = to_ioatdma_device(chan->device);
438         pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys);
439         kfree(desc);
440 }
441
442 /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
443  * @chan: channel to be initialized
444  */
445 static int ioat2_alloc_chan_resources(struct dma_chan *c)
446 {
447         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
448         struct ioat_chan_common *chan = &ioat->base;
449         struct ioat_ring_ent **ring;
450         u16 chanctrl;
451         u32 chanerr;
452         int descs;
453         int i;
454
455         /* have we already been set up? */
456         if (ioat->ring)
457                 return 1 << ioat->alloc_order;
458
459         /* Setup register to interrupt and write completion status on error */
460         chanctrl = IOAT_CHANCTRL_ERR_INT_EN | IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
461                    IOAT_CHANCTRL_ERR_COMPLETION_EN;
462         writew(chanctrl, chan->reg_base + IOAT_CHANCTRL_OFFSET);
463
464         chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
465         if (chanerr) {
466                 dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
467                 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
468         }
469
470         /* allocate a completion writeback area */
471         /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
472         chan->completion = pci_pool_alloc(chan->device->completion_pool,
473                                           GFP_KERNEL, &chan->completion_dma);
474         if (!chan->completion)
475                 return -ENOMEM;
476
477         memset(chan->completion, 0, sizeof(*chan->completion));
478         writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
479                chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
480         writel(((u64) chan->completion_dma) >> 32,
481                chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
482
483         ioat->alloc_order = ioat_get_alloc_order();
484         descs = 1 << ioat->alloc_order;
485
486         /* allocate the array to hold the software ring */
487         ring = kcalloc(descs, sizeof(*ring), GFP_KERNEL);
488         if (!ring)
489                 return -ENOMEM;
490         for (i = 0; i < descs; i++) {
491                 ring[i] = ioat2_alloc_ring_ent(c);
492                 if (!ring[i]) {
493                         while (i--)
494                                 ioat2_free_ring_ent(ring[i], c);
495                         kfree(ring);
496                         return -ENOMEM;
497                 }
498                 set_desc_id(ring[i], i);
499         }
500
501         /* link descs */
502         for (i = 0; i < descs-1; i++) {
503                 struct ioat_ring_ent *next = ring[i+1];
504                 struct ioat_dma_descriptor *hw = ring[i]->hw;
505
506                 hw->next = next->txd.phys;
507         }
508         ring[i]->hw->next = ring[0]->txd.phys;
509
510         spin_lock_bh(&ioat->ring_lock);
511         ioat->ring = ring;
512         ioat->head = 0;
513         ioat->issued = 0;
514         ioat->tail = 0;
515         ioat->pending = 0;
516         spin_unlock_bh(&ioat->ring_lock);
517
518         tasklet_enable(&chan->cleanup_task);
519         ioat2_start_null_desc(ioat);
520
521         return descs;
522 }
523
524 /**
525  * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
526  * @idx: gets starting descriptor index on successful allocation
527  * @ioat: ioat2,3 channel (ring) to operate on
528  * @num_descs: allocation length
529  */
530 static int ioat2_alloc_and_lock(u16 *idx, struct ioat2_dma_chan *ioat, int num_descs)
531 {
532         struct ioat_chan_common *chan = &ioat->base;
533
534         spin_lock_bh(&ioat->ring_lock);
535         if (unlikely(ioat2_ring_space(ioat) < num_descs)) {
536                 if (printk_ratelimit())
537                         dev_dbg(to_dev(chan),
538                                 "%s: ring full! num_descs: %d (%x:%x:%x)\n",
539                                 __func__, num_descs, ioat->head, ioat->tail,
540                                 ioat->issued);
541                 spin_unlock_bh(&ioat->ring_lock);
542
543                 /* do direct reclaim in the allocation failure case */
544                 ioat2_cleanup(ioat);
545
546                 return -ENOMEM;
547         }
548
549         dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n",
550                 __func__, num_descs, ioat->head, ioat->tail, ioat->issued);
551
552         *idx = ioat2_desc_alloc(ioat, num_descs);
553         return 0;  /* with ioat->ring_lock held */
554 }
555
556 static struct dma_async_tx_descriptor *
557 ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
558                            dma_addr_t dma_src, size_t len, unsigned long flags)
559 {
560         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
561         struct ioat_dma_descriptor *hw;
562         struct ioat_ring_ent *desc;
563         dma_addr_t dst = dma_dest;
564         dma_addr_t src = dma_src;
565         size_t total_len = len;
566         int num_descs;
567         u16 idx;
568         int i;
569
570         num_descs = ioat2_xferlen_to_descs(ioat, len);
571         if (likely(num_descs) &&
572             ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
573                 /* pass */;
574         else
575                 return NULL;
576         for (i = 0; i < num_descs; i++) {
577                 size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log);
578
579                 desc = ioat2_get_ring_ent(ioat, idx + i);
580                 hw = desc->hw;
581
582                 hw->size = copy;
583                 hw->ctl = 0;
584                 hw->src_addr = src;
585                 hw->dst_addr = dst;
586
587                 len -= copy;
588                 dst += copy;
589                 src += copy;
590                 dump_desc_dbg(ioat, desc);
591         }
592
593         desc->txd.flags = flags;
594         desc->len = total_len;
595         hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
596         hw->ctl_f.compl_write = 1;
597         dump_desc_dbg(ioat, desc);
598         /* we leave the channel locked to ensure in order submission */
599
600         return &desc->txd;
601 }
602
603 /**
604  * ioat2_free_chan_resources - release all the descriptors
605  * @chan: the channel to be cleaned
606  */
607 static void ioat2_free_chan_resources(struct dma_chan *c)
608 {
609         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
610         struct ioat_chan_common *chan = &ioat->base;
611         struct ioatdma_device *ioatdma_device = chan->device;
612         struct ioat_ring_ent *desc;
613         const u16 total_descs = 1 << ioat->alloc_order;
614         int descs;
615         int i;
616
617         /* Before freeing channel resources first check
618          * if they have been previously allocated for this channel.
619          */
620         if (!ioat->ring)
621                 return;
622
623         tasklet_disable(&chan->cleanup_task);
624         ioat2_cleanup(ioat);
625
626         /* Delay 100ms after reset to allow internal DMA logic to quiesce
627          * before removing DMA descriptor resources.
628          */
629         writeb(IOAT_CHANCMD_RESET,
630                chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
631         mdelay(100);
632
633         spin_lock_bh(&ioat->ring_lock);
634         descs = ioat2_ring_space(ioat);
635         dev_dbg(to_dev(chan), "freeing %d idle descriptors\n", descs);
636         for (i = 0; i < descs; i++) {
637                 desc = ioat2_get_ring_ent(ioat, ioat->head + i);
638                 ioat2_free_ring_ent(desc, c);
639         }
640
641         if (descs < total_descs)
642                 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
643                         total_descs - descs);
644
645         for (i = 0; i < total_descs - descs; i++) {
646                 desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
647                 dump_desc_dbg(ioat, desc);
648                 ioat2_free_ring_ent(desc, c);
649         }
650
651         kfree(ioat->ring);
652         ioat->ring = NULL;
653         ioat->alloc_order = 0;
654         pci_pool_free(ioatdma_device->completion_pool,
655                       chan->completion,
656                       chan->completion_dma);
657         spin_unlock_bh(&ioat->ring_lock);
658
659         chan->last_completion = 0;
660         chan->completion_dma = 0;
661         ioat->pending = 0;
662         ioat->dmacount = 0;
663         chan->watchdog_completion = 0;
664         chan->last_compl_desc_addr_hw = 0;
665         chan->watchdog_tcp_cookie = 0;
666         chan->watchdog_last_tcp_cookie = 0;
667 }
668
669 static enum dma_status
670 ioat2_is_complete(struct dma_chan *c, dma_cookie_t cookie,
671                      dma_cookie_t *done, dma_cookie_t *used)
672 {
673         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
674
675         if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
676                 return DMA_SUCCESS;
677
678         ioat2_cleanup(ioat);
679
680         return ioat_is_complete(c, cookie, done, used);
681 }
682
683 int ioat2_dma_probe(struct ioatdma_device *device, int dca)
684 {
685         struct pci_dev *pdev = device->pdev;
686         struct dma_device *dma;
687         struct dma_chan *c;
688         struct ioat_chan_common *chan;
689         int err;
690
691         device->enumerate_channels = ioat2_enumerate_channels;
692         dma = &device->common;
693         dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
694         dma->device_issue_pending = ioat2_issue_pending;
695         dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
696         dma->device_free_chan_resources = ioat2_free_chan_resources;
697         dma->device_is_tx_complete = ioat2_is_complete;
698
699         err = ioat_probe(device);
700         if (err)
701                 return err;
702         ioat_set_tcp_copy_break(2048);
703
704         list_for_each_entry(c, &dma->channels, device_node) {
705                 chan = to_chan_common(c);
706                 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
707                        chan->reg_base + IOAT_DCACTRL_OFFSET);
708         }
709
710         err = ioat_register(device);
711         if (err)
712                 return err;
713         if (dca)
714                 device->dca = ioat2_dca_init(pdev, device->reg_base);
715
716         INIT_DELAYED_WORK(&device->work, ioat2_chan_watchdog);
717         schedule_delayed_work(&device->work, WATCHDOG_DELAY);
718
719         return err;
720 }
721
722 int ioat3_dma_probe(struct ioatdma_device *device, int dca)
723 {
724         struct pci_dev *pdev = device->pdev;
725         struct dma_device *dma;
726         struct dma_chan *c;
727         struct ioat_chan_common *chan;
728         int err;
729         u16 dev_id;
730
731         device->enumerate_channels = ioat2_enumerate_channels;
732         dma = &device->common;
733         dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
734         dma->device_issue_pending = ioat2_issue_pending;
735         dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
736         dma->device_free_chan_resources = ioat2_free_chan_resources;
737         dma->device_is_tx_complete = ioat2_is_complete;
738
739         /* -= IOAT ver.3 workarounds =- */
740         /* Write CHANERRMSK_INT with 3E07h to mask out the errors
741          * that can cause stability issues for IOAT ver.3
742          */
743         pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
744
745         /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
746          * (workaround for spurious config parity error after restart)
747          */
748         pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
749         if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
750                 pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
751
752         err = ioat_probe(device);
753         if (err)
754                 return err;
755         ioat_set_tcp_copy_break(262144);
756
757         list_for_each_entry(c, &dma->channels, device_node) {
758                 chan = to_chan_common(c);
759                 writel(IOAT_DMA_DCA_ANY_CPU,
760                        chan->reg_base + IOAT_DCACTRL_OFFSET);
761         }
762
763         err = ioat_register(device);
764         if (err)
765                 return err;
766         if (dca)
767                 device->dca = ioat3_dca_init(pdev, device->reg_base);
768
769         return err;
770 }