ioat: ignore reserved bits for chancnt and xfercap
[linux-2.6.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         dma->chancnt &= 0x1f; /* bits [4:0] valid */
363         if (dma->chancnt > ARRAY_SIZE(device->idx)) {
364                 dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
365                          dma->chancnt, ARRAY_SIZE(device->idx));
366                 dma->chancnt = ARRAY_SIZE(device->idx);
367         }
368         xfercap_log = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
369         xfercap_log &= 0x1f; /* bits [4:0] valid */
370         if (xfercap_log == 0)
371                 return 0;
372         dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
373
374         /* FIXME which i/oat version is i7300? */
375 #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
376         if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
377                 dma->chancnt--;
378 #endif
379         for (i = 0; i < dma->chancnt; i++) {
380                 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
381                 if (!ioat)
382                         break;
383
384                 ioat_init_channel(device, &ioat->base, i,
385                                   ioat2_reset_part2,
386                                   ioat2_cleanup_tasklet,
387                                   (unsigned long) ioat);
388                 ioat->xfercap_log = xfercap_log;
389                 spin_lock_init(&ioat->ring_lock);
390         }
391         dma->chancnt = i;
392         return i;
393 }
394
395 static dma_cookie_t ioat2_tx_submit_unlock(struct dma_async_tx_descriptor *tx)
396 {
397         struct dma_chan *c = tx->chan;
398         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
399         dma_cookie_t cookie = c->cookie;
400
401         cookie++;
402         if (cookie < 0)
403                 cookie = 1;
404         tx->cookie = cookie;
405         c->cookie = cookie;
406         dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
407
408         ioat2_update_pending(ioat);
409         spin_unlock_bh(&ioat->ring_lock);
410
411         return cookie;
412 }
413
414 static struct ioat_ring_ent *ioat2_alloc_ring_ent(struct dma_chan *chan)
415 {
416         struct ioat_dma_descriptor *hw;
417         struct ioat_ring_ent *desc;
418         struct ioatdma_device *dma;
419         dma_addr_t phys;
420
421         dma = to_ioatdma_device(chan->device);
422         hw = pci_pool_alloc(dma->dma_pool, GFP_KERNEL, &phys);
423         if (!hw)
424                 return NULL;
425         memset(hw, 0, sizeof(*hw));
426
427         desc = kzalloc(sizeof(*desc), GFP_KERNEL);
428         if (!desc) {
429                 pci_pool_free(dma->dma_pool, hw, phys);
430                 return NULL;
431         }
432
433         dma_async_tx_descriptor_init(&desc->txd, chan);
434         desc->txd.tx_submit = ioat2_tx_submit_unlock;
435         desc->hw = hw;
436         desc->txd.phys = phys;
437         return desc;
438 }
439
440 static void ioat2_free_ring_ent(struct ioat_ring_ent *desc, struct dma_chan *chan)
441 {
442         struct ioatdma_device *dma;
443
444         dma = to_ioatdma_device(chan->device);
445         pci_pool_free(dma->dma_pool, desc->hw, desc->txd.phys);
446         kfree(desc);
447 }
448
449 /* ioat2_alloc_chan_resources - allocate/initialize ioat2 descriptor ring
450  * @chan: channel to be initialized
451  */
452 static int ioat2_alloc_chan_resources(struct dma_chan *c)
453 {
454         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
455         struct ioat_chan_common *chan = &ioat->base;
456         struct ioat_ring_ent **ring;
457         u16 chanctrl;
458         u32 chanerr;
459         int descs;
460         int i;
461
462         /* have we already been set up? */
463         if (ioat->ring)
464                 return 1 << ioat->alloc_order;
465
466         /* Setup register to interrupt and write completion status on error */
467         chanctrl = IOAT_CHANCTRL_ERR_INT_EN | IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
468                    IOAT_CHANCTRL_ERR_COMPLETION_EN;
469         writew(chanctrl, chan->reg_base + IOAT_CHANCTRL_OFFSET);
470
471         chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
472         if (chanerr) {
473                 dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
474                 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
475         }
476
477         /* allocate a completion writeback area */
478         /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
479         chan->completion = pci_pool_alloc(chan->device->completion_pool,
480                                           GFP_KERNEL, &chan->completion_dma);
481         if (!chan->completion)
482                 return -ENOMEM;
483
484         memset(chan->completion, 0, sizeof(*chan->completion));
485         writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
486                chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
487         writel(((u64) chan->completion_dma) >> 32,
488                chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
489
490         ioat->alloc_order = ioat_get_alloc_order();
491         descs = 1 << ioat->alloc_order;
492
493         /* allocate the array to hold the software ring */
494         ring = kcalloc(descs, sizeof(*ring), GFP_KERNEL);
495         if (!ring)
496                 return -ENOMEM;
497         for (i = 0; i < descs; i++) {
498                 ring[i] = ioat2_alloc_ring_ent(c);
499                 if (!ring[i]) {
500                         while (i--)
501                                 ioat2_free_ring_ent(ring[i], c);
502                         kfree(ring);
503                         return -ENOMEM;
504                 }
505                 set_desc_id(ring[i], i);
506         }
507
508         /* link descs */
509         for (i = 0; i < descs-1; i++) {
510                 struct ioat_ring_ent *next = ring[i+1];
511                 struct ioat_dma_descriptor *hw = ring[i]->hw;
512
513                 hw->next = next->txd.phys;
514         }
515         ring[i]->hw->next = ring[0]->txd.phys;
516
517         spin_lock_bh(&ioat->ring_lock);
518         ioat->ring = ring;
519         ioat->head = 0;
520         ioat->issued = 0;
521         ioat->tail = 0;
522         ioat->pending = 0;
523         spin_unlock_bh(&ioat->ring_lock);
524
525         tasklet_enable(&chan->cleanup_task);
526         ioat2_start_null_desc(ioat);
527
528         return descs;
529 }
530
531 /**
532  * ioat2_alloc_and_lock - common descriptor alloc boilerplate for ioat2,3 ops
533  * @idx: gets starting descriptor index on successful allocation
534  * @ioat: ioat2,3 channel (ring) to operate on
535  * @num_descs: allocation length
536  */
537 static int ioat2_alloc_and_lock(u16 *idx, struct ioat2_dma_chan *ioat, int num_descs)
538 {
539         struct ioat_chan_common *chan = &ioat->base;
540
541         spin_lock_bh(&ioat->ring_lock);
542         if (unlikely(ioat2_ring_space(ioat) < num_descs)) {
543                 if (printk_ratelimit())
544                         dev_dbg(to_dev(chan),
545                                 "%s: ring full! num_descs: %d (%x:%x:%x)\n",
546                                 __func__, num_descs, ioat->head, ioat->tail,
547                                 ioat->issued);
548                 spin_unlock_bh(&ioat->ring_lock);
549
550                 /* do direct reclaim in the allocation failure case */
551                 ioat2_cleanup(ioat);
552
553                 return -ENOMEM;
554         }
555
556         dev_dbg(to_dev(chan), "%s: num_descs: %d (%x:%x:%x)\n",
557                 __func__, num_descs, ioat->head, ioat->tail, ioat->issued);
558
559         *idx = ioat2_desc_alloc(ioat, num_descs);
560         return 0;  /* with ioat->ring_lock held */
561 }
562
563 static struct dma_async_tx_descriptor *
564 ioat2_dma_prep_memcpy_lock(struct dma_chan *c, dma_addr_t dma_dest,
565                            dma_addr_t dma_src, size_t len, unsigned long flags)
566 {
567         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
568         struct ioat_dma_descriptor *hw;
569         struct ioat_ring_ent *desc;
570         dma_addr_t dst = dma_dest;
571         dma_addr_t src = dma_src;
572         size_t total_len = len;
573         int num_descs;
574         u16 idx;
575         int i;
576
577         num_descs = ioat2_xferlen_to_descs(ioat, len);
578         if (likely(num_descs) &&
579             ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
580                 /* pass */;
581         else
582                 return NULL;
583         for (i = 0; i < num_descs; i++) {
584                 size_t copy = min_t(size_t, len, 1 << ioat->xfercap_log);
585
586                 desc = ioat2_get_ring_ent(ioat, idx + i);
587                 hw = desc->hw;
588
589                 hw->size = copy;
590                 hw->ctl = 0;
591                 hw->src_addr = src;
592                 hw->dst_addr = dst;
593
594                 len -= copy;
595                 dst += copy;
596                 src += copy;
597                 dump_desc_dbg(ioat, desc);
598         }
599
600         desc->txd.flags = flags;
601         desc->len = total_len;
602         hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
603         hw->ctl_f.compl_write = 1;
604         dump_desc_dbg(ioat, desc);
605         /* we leave the channel locked to ensure in order submission */
606
607         return &desc->txd;
608 }
609
610 /**
611  * ioat2_free_chan_resources - release all the descriptors
612  * @chan: the channel to be cleaned
613  */
614 static void ioat2_free_chan_resources(struct dma_chan *c)
615 {
616         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
617         struct ioat_chan_common *chan = &ioat->base;
618         struct ioatdma_device *ioatdma_device = chan->device;
619         struct ioat_ring_ent *desc;
620         const u16 total_descs = 1 << ioat->alloc_order;
621         int descs;
622         int i;
623
624         /* Before freeing channel resources first check
625          * if they have been previously allocated for this channel.
626          */
627         if (!ioat->ring)
628                 return;
629
630         tasklet_disable(&chan->cleanup_task);
631         ioat2_cleanup(ioat);
632
633         /* Delay 100ms after reset to allow internal DMA logic to quiesce
634          * before removing DMA descriptor resources.
635          */
636         writeb(IOAT_CHANCMD_RESET,
637                chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
638         mdelay(100);
639
640         spin_lock_bh(&ioat->ring_lock);
641         descs = ioat2_ring_space(ioat);
642         dev_dbg(to_dev(chan), "freeing %d idle descriptors\n", descs);
643         for (i = 0; i < descs; i++) {
644                 desc = ioat2_get_ring_ent(ioat, ioat->head + i);
645                 ioat2_free_ring_ent(desc, c);
646         }
647
648         if (descs < total_descs)
649                 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
650                         total_descs - descs);
651
652         for (i = 0; i < total_descs - descs; i++) {
653                 desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
654                 dump_desc_dbg(ioat, desc);
655                 ioat2_free_ring_ent(desc, c);
656         }
657
658         kfree(ioat->ring);
659         ioat->ring = NULL;
660         ioat->alloc_order = 0;
661         pci_pool_free(ioatdma_device->completion_pool,
662                       chan->completion,
663                       chan->completion_dma);
664         spin_unlock_bh(&ioat->ring_lock);
665
666         chan->last_completion = 0;
667         chan->completion_dma = 0;
668         ioat->pending = 0;
669         ioat->dmacount = 0;
670         chan->watchdog_completion = 0;
671         chan->last_compl_desc_addr_hw = 0;
672         chan->watchdog_tcp_cookie = 0;
673         chan->watchdog_last_tcp_cookie = 0;
674 }
675
676 static enum dma_status
677 ioat2_is_complete(struct dma_chan *c, dma_cookie_t cookie,
678                      dma_cookie_t *done, dma_cookie_t *used)
679 {
680         struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
681
682         if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
683                 return DMA_SUCCESS;
684
685         ioat2_cleanup(ioat);
686
687         return ioat_is_complete(c, cookie, done, used);
688 }
689
690 int ioat2_dma_probe(struct ioatdma_device *device, int dca)
691 {
692         struct pci_dev *pdev = device->pdev;
693         struct dma_device *dma;
694         struct dma_chan *c;
695         struct ioat_chan_common *chan;
696         int err;
697
698         device->enumerate_channels = ioat2_enumerate_channels;
699         dma = &device->common;
700         dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
701         dma->device_issue_pending = ioat2_issue_pending;
702         dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
703         dma->device_free_chan_resources = ioat2_free_chan_resources;
704         dma->device_is_tx_complete = ioat2_is_complete;
705
706         err = ioat_probe(device);
707         if (err)
708                 return err;
709         ioat_set_tcp_copy_break(2048);
710
711         list_for_each_entry(c, &dma->channels, device_node) {
712                 chan = to_chan_common(c);
713                 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
714                        chan->reg_base + IOAT_DCACTRL_OFFSET);
715         }
716
717         err = ioat_register(device);
718         if (err)
719                 return err;
720         if (dca)
721                 device->dca = ioat2_dca_init(pdev, device->reg_base);
722
723         INIT_DELAYED_WORK(&device->work, ioat2_chan_watchdog);
724         schedule_delayed_work(&device->work, WATCHDOG_DELAY);
725
726         return err;
727 }
728
729 int ioat3_dma_probe(struct ioatdma_device *device, int dca)
730 {
731         struct pci_dev *pdev = device->pdev;
732         struct dma_device *dma;
733         struct dma_chan *c;
734         struct ioat_chan_common *chan;
735         int err;
736         u16 dev_id;
737
738         device->enumerate_channels = ioat2_enumerate_channels;
739         dma = &device->common;
740         dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
741         dma->device_issue_pending = ioat2_issue_pending;
742         dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
743         dma->device_free_chan_resources = ioat2_free_chan_resources;
744         dma->device_is_tx_complete = ioat2_is_complete;
745
746         /* -= IOAT ver.3 workarounds =- */
747         /* Write CHANERRMSK_INT with 3E07h to mask out the errors
748          * that can cause stability issues for IOAT ver.3
749          */
750         pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
751
752         /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
753          * (workaround for spurious config parity error after restart)
754          */
755         pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
756         if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
757                 pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
758
759         err = ioat_probe(device);
760         if (err)
761                 return err;
762         ioat_set_tcp_copy_break(262144);
763
764         list_for_each_entry(c, &dma->channels, device_node) {
765                 chan = to_chan_common(c);
766                 writel(IOAT_DMA_DCA_ANY_CPU,
767                        chan->reg_base + IOAT_DCACTRL_OFFSET);
768         }
769
770         err = ioat_register(device);
771         if (err)
772                 return err;
773         if (dca)
774                 device->dca = ioat3_dca_init(pdev, device->reg_base);
775
776         return err;
777 }