]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/dma/ioat_dma.c
I/OAT: fix channel resources free for not allocated channels
[karo-tx-linux.git] / drivers / dma / ioat_dma.c
1 /*
2  * Intel I/OAT DMA Linux driver
3  * Copyright(c) 2004 - 2007 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, which does asynchronous
25  * copy 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 "ioatdma.h"
37 #include "ioatdma_registers.h"
38 #include "ioatdma_hw.h"
39
40 #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
41 #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
42 #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
43 #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
44
45 #define chan_num(ch) ((int)((ch)->reg_base - (ch)->device->reg_base) / 0x80)
46 static int ioat_pending_level = 4;
47 module_param(ioat_pending_level, int, 0644);
48 MODULE_PARM_DESC(ioat_pending_level,
49                  "high-water mark for pushing ioat descriptors (default: 4)");
50
51 #define RESET_DELAY  msecs_to_jiffies(100)
52 #define WATCHDOG_DELAY  round_jiffies(msecs_to_jiffies(2000))
53 static void ioat_dma_chan_reset_part2(struct work_struct *work);
54 static void ioat_dma_chan_watchdog(struct work_struct *work);
55
56 /*
57  * workaround for IOAT ver.3.0 null descriptor issue
58  * (channel returns error when size is 0)
59  */
60 #define NULL_DESC_BUFFER_SIZE 1
61
62 /* internal functions */
63 static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
64 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
65
66 static struct ioat_desc_sw *
67 ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
68 static struct ioat_desc_sw *
69 ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
70
71 static inline struct ioat_dma_chan *ioat_lookup_chan_by_index(
72                                                 struct ioatdma_device *device,
73                                                 int index)
74 {
75         return device->idx[index];
76 }
77
78 /**
79  * ioat_dma_do_interrupt - handler used for single vector interrupt mode
80  * @irq: interrupt id
81  * @data: interrupt data
82  */
83 static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
84 {
85         struct ioatdma_device *instance = data;
86         struct ioat_dma_chan *ioat_chan;
87         unsigned long attnstatus;
88         int bit;
89         u8 intrctrl;
90
91         intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
92
93         if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
94                 return IRQ_NONE;
95
96         if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
97                 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
98                 return IRQ_NONE;
99         }
100
101         attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
102         for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
103                 ioat_chan = ioat_lookup_chan_by_index(instance, bit);
104                 tasklet_schedule(&ioat_chan->cleanup_task);
105         }
106
107         writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
108         return IRQ_HANDLED;
109 }
110
111 /**
112  * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
113  * @irq: interrupt id
114  * @data: interrupt data
115  */
116 static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
117 {
118         struct ioat_dma_chan *ioat_chan = data;
119
120         tasklet_schedule(&ioat_chan->cleanup_task);
121
122         return IRQ_HANDLED;
123 }
124
125 static void ioat_dma_cleanup_tasklet(unsigned long data);
126
127 /**
128  * ioat_dma_enumerate_channels - find and initialize the device's channels
129  * @device: the device to be enumerated
130  */
131 static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
132 {
133         u8 xfercap_scale;
134         u32 xfercap;
135         int i;
136         struct ioat_dma_chan *ioat_chan;
137
138         /*
139          * IOAT ver.3 workarounds
140          */
141         if (device->version == IOAT_VER_3_0) {
142                 u32 chan_err_mask;
143                 u16 dev_id;
144                 u32 dmauncerrsts;
145
146                 /*
147                  * Write CHANERRMSK_INT with 3E07h to mask out the errors
148                  * that can cause stability issues for IOAT ver.3
149                  */
150                 chan_err_mask = 0x3E07;
151                 pci_write_config_dword(device->pdev,
152                         IOAT_PCI_CHANERRMASK_INT_OFFSET,
153                         chan_err_mask);
154
155                 /*
156                  * Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
157                  * (workaround for spurious config parity error after restart)
158                  */
159                 pci_read_config_word(device->pdev,
160                         IOAT_PCI_DEVICE_ID_OFFSET,
161                         &dev_id);
162                 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
163                         dmauncerrsts = 0x10;
164                         pci_write_config_dword(device->pdev,
165                                 IOAT_PCI_DMAUNCERRSTS_OFFSET,
166                                 dmauncerrsts);
167                 }
168         }
169
170         device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
171         xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
172         xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
173
174         for (i = 0; i < device->common.chancnt; i++) {
175                 ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
176                 if (!ioat_chan) {
177                         device->common.chancnt = i;
178                         break;
179                 }
180
181                 ioat_chan->device = device;
182                 ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
183                 ioat_chan->xfercap = xfercap;
184                 ioat_chan->desccount = 0;
185                 INIT_DELAYED_WORK(&ioat_chan->work, ioat_dma_chan_reset_part2);
186                 if (ioat_chan->device->version != IOAT_VER_1_2) {
187                         writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE
188                                         | IOAT_DMA_DCA_ANY_CPU,
189                                 ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
190                 }
191                 spin_lock_init(&ioat_chan->cleanup_lock);
192                 spin_lock_init(&ioat_chan->desc_lock);
193                 INIT_LIST_HEAD(&ioat_chan->free_desc);
194                 INIT_LIST_HEAD(&ioat_chan->used_desc);
195                 /* This should be made common somewhere in dmaengine.c */
196                 ioat_chan->common.device = &device->common;
197                 list_add_tail(&ioat_chan->common.device_node,
198                               &device->common.channels);
199                 device->idx[i] = ioat_chan;
200                 tasklet_init(&ioat_chan->cleanup_task,
201                              ioat_dma_cleanup_tasklet,
202                              (unsigned long) ioat_chan);
203                 tasklet_disable(&ioat_chan->cleanup_task);
204         }
205         return device->common.chancnt;
206 }
207
208 /**
209  * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
210  *                                 descriptors to hw
211  * @chan: DMA channel handle
212  */
213 static inline void __ioat1_dma_memcpy_issue_pending(
214                                                 struct ioat_dma_chan *ioat_chan)
215 {
216         ioat_chan->pending = 0;
217         writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
218 }
219
220 static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
221 {
222         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
223
224         if (ioat_chan->pending > 0) {
225                 spin_lock_bh(&ioat_chan->desc_lock);
226                 __ioat1_dma_memcpy_issue_pending(ioat_chan);
227                 spin_unlock_bh(&ioat_chan->desc_lock);
228         }
229 }
230
231 static inline void __ioat2_dma_memcpy_issue_pending(
232                                                 struct ioat_dma_chan *ioat_chan)
233 {
234         ioat_chan->pending = 0;
235         writew(ioat_chan->dmacount,
236                ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
237 }
238
239 static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
240 {
241         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
242
243         if (ioat_chan->pending > 0) {
244                 spin_lock_bh(&ioat_chan->desc_lock);
245                 __ioat2_dma_memcpy_issue_pending(ioat_chan);
246                 spin_unlock_bh(&ioat_chan->desc_lock);
247         }
248 }
249
250
251 /**
252  * ioat_dma_chan_reset_part2 - reinit the channel after a reset
253  */
254 static void ioat_dma_chan_reset_part2(struct work_struct *work)
255 {
256         struct ioat_dma_chan *ioat_chan =
257                 container_of(work, struct ioat_dma_chan, work.work);
258         struct ioat_desc_sw *desc;
259
260         spin_lock_bh(&ioat_chan->cleanup_lock);
261         spin_lock_bh(&ioat_chan->desc_lock);
262
263         ioat_chan->completion_virt->low = 0;
264         ioat_chan->completion_virt->high = 0;
265         ioat_chan->pending = 0;
266
267         /*
268          * count the descriptors waiting, and be sure to do it
269          * right for both the CB1 line and the CB2 ring
270          */
271         ioat_chan->dmacount = 0;
272         if (ioat_chan->used_desc.prev) {
273                 desc = to_ioat_desc(ioat_chan->used_desc.prev);
274                 do {
275                         ioat_chan->dmacount++;
276                         desc = to_ioat_desc(desc->node.next);
277                 } while (&desc->node != ioat_chan->used_desc.next);
278         }
279
280         /*
281          * write the new starting descriptor address
282          * this puts channel engine into ARMED state
283          */
284         desc = to_ioat_desc(ioat_chan->used_desc.prev);
285         switch (ioat_chan->device->version) {
286         case IOAT_VER_1_2:
287                 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
288                        ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
289                 writel(((u64) desc->async_tx.phys) >> 32,
290                        ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
291
292                 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
293                         + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
294                 break;
295         case IOAT_VER_2_0:
296                 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
297                        ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
298                 writel(((u64) desc->async_tx.phys) >> 32,
299                        ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
300
301                 /* tell the engine to go with what's left to be done */
302                 writew(ioat_chan->dmacount,
303                        ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
304
305                 break;
306         }
307         dev_err(&ioat_chan->device->pdev->dev,
308                 "chan%d reset - %d descs waiting, %d total desc\n",
309                 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
310
311         spin_unlock_bh(&ioat_chan->desc_lock);
312         spin_unlock_bh(&ioat_chan->cleanup_lock);
313 }
314
315 /**
316  * ioat_dma_reset_channel - restart a channel
317  * @ioat_chan: IOAT DMA channel handle
318  */
319 static void ioat_dma_reset_channel(struct ioat_dma_chan *ioat_chan)
320 {
321         u32 chansts, chanerr;
322
323         if (!ioat_chan->used_desc.prev)
324                 return;
325
326         chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
327         chansts = (ioat_chan->completion_virt->low
328                                         & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
329         if (chanerr) {
330                 dev_err(&ioat_chan->device->pdev->dev,
331                         "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
332                         chan_num(ioat_chan), chansts, chanerr);
333                 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
334         }
335
336         /*
337          * whack it upside the head with a reset
338          * and wait for things to settle out.
339          * force the pending count to a really big negative
340          * to make sure no one forces an issue_pending
341          * while we're waiting.
342          */
343
344         spin_lock_bh(&ioat_chan->desc_lock);
345         ioat_chan->pending = INT_MIN;
346         writeb(IOAT_CHANCMD_RESET,
347                ioat_chan->reg_base
348                + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
349         spin_unlock_bh(&ioat_chan->desc_lock);
350
351         /* schedule the 2nd half instead of sleeping a long time */
352         schedule_delayed_work(&ioat_chan->work, RESET_DELAY);
353 }
354
355 /**
356  * ioat_dma_chan_watchdog - watch for stuck channels
357  */
358 static void ioat_dma_chan_watchdog(struct work_struct *work)
359 {
360         struct ioatdma_device *device =
361                 container_of(work, struct ioatdma_device, work.work);
362         struct ioat_dma_chan *ioat_chan;
363         int i;
364
365         union {
366                 u64 full;
367                 struct {
368                         u32 low;
369                         u32 high;
370                 };
371         } completion_hw;
372         unsigned long compl_desc_addr_hw;
373
374         for (i = 0; i < device->common.chancnt; i++) {
375                 ioat_chan = ioat_lookup_chan_by_index(device, i);
376
377                 if (ioat_chan->device->version == IOAT_VER_1_2
378                         /* have we started processing anything yet */
379                     && ioat_chan->last_completion
380                         /* have we completed any since last watchdog cycle? */
381                     && (ioat_chan->last_completion ==
382                                 ioat_chan->watchdog_completion)
383                         /* has TCP stuck on one cookie since last watchdog? */
384                     && (ioat_chan->watchdog_tcp_cookie ==
385                                 ioat_chan->watchdog_last_tcp_cookie)
386                     && (ioat_chan->watchdog_tcp_cookie !=
387                                 ioat_chan->completed_cookie)
388                         /* is there something in the chain to be processed? */
389                         /* CB1 chain always has at least the last one processed */
390                     && (ioat_chan->used_desc.prev != ioat_chan->used_desc.next)
391                     && ioat_chan->pending == 0) {
392
393                         /*
394                          * check CHANSTS register for completed
395                          * descriptor address.
396                          * if it is different than completion writeback,
397                          * it is not zero
398                          * and it has changed since the last watchdog
399                          *     we can assume that channel
400                          *     is still working correctly
401                          *     and the problem is in completion writeback.
402                          *     update completion writeback
403                          *     with actual CHANSTS value
404                          * else
405                          *     try resetting the channel
406                          */
407
408                         completion_hw.low = readl(ioat_chan->reg_base +
409                                 IOAT_CHANSTS_OFFSET_LOW(ioat_chan->device->version));
410                         completion_hw.high = readl(ioat_chan->reg_base +
411                                 IOAT_CHANSTS_OFFSET_HIGH(ioat_chan->device->version));
412 #if (BITS_PER_LONG == 64)
413                         compl_desc_addr_hw =
414                                 completion_hw.full
415                                 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
416 #else
417                         compl_desc_addr_hw =
418                                 completion_hw.low & IOAT_LOW_COMPLETION_MASK;
419 #endif
420
421                         if ((compl_desc_addr_hw != 0)
422                            && (compl_desc_addr_hw != ioat_chan->watchdog_completion)
423                            && (compl_desc_addr_hw != ioat_chan->last_compl_desc_addr_hw)) {
424                                 ioat_chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
425                                 ioat_chan->completion_virt->low = completion_hw.low;
426                                 ioat_chan->completion_virt->high = completion_hw.high;
427                         } else {
428                                 ioat_dma_reset_channel(ioat_chan);
429                                 ioat_chan->watchdog_completion = 0;
430                                 ioat_chan->last_compl_desc_addr_hw = 0;
431                         }
432
433                 /*
434                  * for version 2.0 if there are descriptors yet to be processed
435                  * and the last completed hasn't changed since the last watchdog
436                  *      if they haven't hit the pending level
437                  *          issue the pending to push them through
438                  *      else
439                  *          try resetting the channel
440                  */
441                 } else if (ioat_chan->device->version == IOAT_VER_2_0
442                     && ioat_chan->used_desc.prev
443                     && ioat_chan->last_completion
444                     && ioat_chan->last_completion == ioat_chan->watchdog_completion) {
445
446                         if (ioat_chan->pending < ioat_pending_level)
447                                 ioat2_dma_memcpy_issue_pending(&ioat_chan->common);
448                         else {
449                                 ioat_dma_reset_channel(ioat_chan);
450                                 ioat_chan->watchdog_completion = 0;
451                         }
452                 } else {
453                         ioat_chan->last_compl_desc_addr_hw = 0;
454                         ioat_chan->watchdog_completion
455                                         = ioat_chan->last_completion;
456                 }
457
458                 ioat_chan->watchdog_last_tcp_cookie =
459                         ioat_chan->watchdog_tcp_cookie;
460         }
461
462         schedule_delayed_work(&device->work, WATCHDOG_DELAY);
463 }
464
465 static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
466 {
467         struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
468         struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
469         struct ioat_desc_sw *prev, *new;
470         struct ioat_dma_descriptor *hw;
471         dma_cookie_t cookie;
472         LIST_HEAD(new_chain);
473         u32 copy;
474         size_t len;
475         dma_addr_t src, dst;
476         unsigned long orig_flags;
477         unsigned int desc_count = 0;
478
479         /* src and dest and len are stored in the initial descriptor */
480         len = first->len;
481         src = first->src;
482         dst = first->dst;
483         orig_flags = first->async_tx.flags;
484         new = first;
485
486         spin_lock_bh(&ioat_chan->desc_lock);
487         prev = to_ioat_desc(ioat_chan->used_desc.prev);
488         prefetch(prev->hw);
489         do {
490                 copy = min_t(size_t, len, ioat_chan->xfercap);
491
492                 async_tx_ack(&new->async_tx);
493
494                 hw = new->hw;
495                 hw->size = copy;
496                 hw->ctl = 0;
497                 hw->src_addr = src;
498                 hw->dst_addr = dst;
499                 hw->next = 0;
500
501                 /* chain together the physical address list for the HW */
502                 wmb();
503                 prev->hw->next = (u64) new->async_tx.phys;
504
505                 len -= copy;
506                 dst += copy;
507                 src += copy;
508
509                 list_add_tail(&new->node, &new_chain);
510                 desc_count++;
511                 prev = new;
512         } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
513
514         if (!new) {
515                 dev_err(&ioat_chan->device->pdev->dev,
516                         "tx submit failed\n");
517                 spin_unlock_bh(&ioat_chan->desc_lock);
518                 return -ENOMEM;
519         }
520
521         hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
522         if (new->async_tx.callback) {
523                 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
524                 if (first != new) {
525                         /* move callback into to last desc */
526                         new->async_tx.callback = first->async_tx.callback;
527                         new->async_tx.callback_param
528                                         = first->async_tx.callback_param;
529                         first->async_tx.callback = NULL;
530                         first->async_tx.callback_param = NULL;
531                 }
532         }
533
534         new->tx_cnt = desc_count;
535         new->async_tx.flags = orig_flags; /* client is in control of this ack */
536
537         /* store the original values for use in later cleanup */
538         if (new != first) {
539                 new->src = first->src;
540                 new->dst = first->dst;
541                 new->len = first->len;
542         }
543
544         /* cookie incr and addition to used_list must be atomic */
545         cookie = ioat_chan->common.cookie;
546         cookie++;
547         if (cookie < 0)
548                 cookie = 1;
549         ioat_chan->common.cookie = new->async_tx.cookie = cookie;
550
551         /* write address into NextDescriptor field of last desc in chain */
552         to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
553                                                         first->async_tx.phys;
554         list_splice_tail(&new_chain, &ioat_chan->used_desc);
555
556         ioat_chan->dmacount += desc_count;
557         ioat_chan->pending += desc_count;
558         if (ioat_chan->pending >= ioat_pending_level)
559                 __ioat1_dma_memcpy_issue_pending(ioat_chan);
560         spin_unlock_bh(&ioat_chan->desc_lock);
561
562         return cookie;
563 }
564
565 static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
566 {
567         struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
568         struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
569         struct ioat_desc_sw *new;
570         struct ioat_dma_descriptor *hw;
571         dma_cookie_t cookie;
572         u32 copy;
573         size_t len;
574         dma_addr_t src, dst;
575         unsigned long orig_flags;
576         unsigned int desc_count = 0;
577
578         /* src and dest and len are stored in the initial descriptor */
579         len = first->len;
580         src = first->src;
581         dst = first->dst;
582         orig_flags = first->async_tx.flags;
583         new = first;
584
585         /*
586          * ioat_chan->desc_lock is still in force in version 2 path
587          * it gets unlocked at end of this function
588          */
589         do {
590                 copy = min_t(size_t, len, ioat_chan->xfercap);
591
592                 async_tx_ack(&new->async_tx);
593
594                 hw = new->hw;
595                 hw->size = copy;
596                 hw->ctl = 0;
597                 hw->src_addr = src;
598                 hw->dst_addr = dst;
599
600                 len -= copy;
601                 dst += copy;
602                 src += copy;
603                 desc_count++;
604         } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
605
606         if (!new) {
607                 dev_err(&ioat_chan->device->pdev->dev,
608                         "tx submit failed\n");
609                 spin_unlock_bh(&ioat_chan->desc_lock);
610                 return -ENOMEM;
611         }
612
613         hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
614         if (new->async_tx.callback) {
615                 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
616                 if (first != new) {
617                         /* move callback into to last desc */
618                         new->async_tx.callback = first->async_tx.callback;
619                         new->async_tx.callback_param
620                                         = first->async_tx.callback_param;
621                         first->async_tx.callback = NULL;
622                         first->async_tx.callback_param = NULL;
623                 }
624         }
625
626         new->tx_cnt = desc_count;
627         new->async_tx.flags = orig_flags; /* client is in control of this ack */
628
629         /* store the original values for use in later cleanup */
630         if (new != first) {
631                 new->src = first->src;
632                 new->dst = first->dst;
633                 new->len = first->len;
634         }
635
636         /* cookie incr and addition to used_list must be atomic */
637         cookie = ioat_chan->common.cookie;
638         cookie++;
639         if (cookie < 0)
640                 cookie = 1;
641         ioat_chan->common.cookie = new->async_tx.cookie = cookie;
642
643         ioat_chan->dmacount += desc_count;
644         ioat_chan->pending += desc_count;
645         if (ioat_chan->pending >= ioat_pending_level)
646                 __ioat2_dma_memcpy_issue_pending(ioat_chan);
647         spin_unlock_bh(&ioat_chan->desc_lock);
648
649         return cookie;
650 }
651
652 /**
653  * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
654  * @ioat_chan: the channel supplying the memory pool for the descriptors
655  * @flags: allocation flags
656  */
657 static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
658                                         struct ioat_dma_chan *ioat_chan,
659                                         gfp_t flags)
660 {
661         struct ioat_dma_descriptor *desc;
662         struct ioat_desc_sw *desc_sw;
663         struct ioatdma_device *ioatdma_device;
664         dma_addr_t phys;
665
666         ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
667         desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
668         if (unlikely(!desc))
669                 return NULL;
670
671         desc_sw = kzalloc(sizeof(*desc_sw), flags);
672         if (unlikely(!desc_sw)) {
673                 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
674                 return NULL;
675         }
676
677         memset(desc, 0, sizeof(*desc));
678         dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
679         switch (ioat_chan->device->version) {
680         case IOAT_VER_1_2:
681                 desc_sw->async_tx.tx_submit = ioat1_tx_submit;
682                 break;
683         case IOAT_VER_2_0:
684         case IOAT_VER_3_0:
685                 desc_sw->async_tx.tx_submit = ioat2_tx_submit;
686                 break;
687         }
688         INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
689
690         desc_sw->hw = desc;
691         desc_sw->async_tx.phys = phys;
692
693         return desc_sw;
694 }
695
696 static int ioat_initial_desc_count = 256;
697 module_param(ioat_initial_desc_count, int, 0644);
698 MODULE_PARM_DESC(ioat_initial_desc_count,
699                  "initial descriptors per channel (default: 256)");
700
701 /**
702  * ioat2_dma_massage_chan_desc - link the descriptors into a circle
703  * @ioat_chan: the channel to be massaged
704  */
705 static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
706 {
707         struct ioat_desc_sw *desc, *_desc;
708
709         /* setup used_desc */
710         ioat_chan->used_desc.next = ioat_chan->free_desc.next;
711         ioat_chan->used_desc.prev = NULL;
712
713         /* pull free_desc out of the circle so that every node is a hw
714          * descriptor, but leave it pointing to the list
715          */
716         ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
717         ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
718
719         /* circle link the hw descriptors */
720         desc = to_ioat_desc(ioat_chan->free_desc.next);
721         desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
722         list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
723                 desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
724         }
725 }
726
727 /**
728  * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
729  * @chan: the channel to be filled out
730  */
731 static int ioat_dma_alloc_chan_resources(struct dma_chan *chan,
732                                          struct dma_client *client)
733 {
734         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
735         struct ioat_desc_sw *desc;
736         u16 chanctrl;
737         u32 chanerr;
738         int i;
739         LIST_HEAD(tmp_list);
740
741         /* have we already been set up? */
742         if (!list_empty(&ioat_chan->free_desc))
743                 return ioat_chan->desccount;
744
745         /* Setup register to interrupt and write completion status on error */
746         chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
747                 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
748                 IOAT_CHANCTRL_ERR_COMPLETION_EN;
749         writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
750
751         chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
752         if (chanerr) {
753                 dev_err(&ioat_chan->device->pdev->dev,
754                         "CHANERR = %x, clearing\n", chanerr);
755                 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
756         }
757
758         /* Allocate descriptors */
759         for (i = 0; i < ioat_initial_desc_count; i++) {
760                 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
761                 if (!desc) {
762                         dev_err(&ioat_chan->device->pdev->dev,
763                                 "Only %d initial descriptors\n", i);
764                         break;
765                 }
766                 list_add_tail(&desc->node, &tmp_list);
767         }
768         spin_lock_bh(&ioat_chan->desc_lock);
769         ioat_chan->desccount = i;
770         list_splice(&tmp_list, &ioat_chan->free_desc);
771         if (ioat_chan->device->version != IOAT_VER_1_2)
772                 ioat2_dma_massage_chan_desc(ioat_chan);
773         spin_unlock_bh(&ioat_chan->desc_lock);
774
775         /* allocate a completion writeback area */
776         /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
777         ioat_chan->completion_virt =
778                 pci_pool_alloc(ioat_chan->device->completion_pool,
779                                GFP_KERNEL,
780                                &ioat_chan->completion_addr);
781         memset(ioat_chan->completion_virt, 0,
782                sizeof(*ioat_chan->completion_virt));
783         writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
784                ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
785         writel(((u64) ioat_chan->completion_addr) >> 32,
786                ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
787
788         tasklet_enable(&ioat_chan->cleanup_task);
789         ioat_dma_start_null_desc(ioat_chan);  /* give chain to dma device */
790         return ioat_chan->desccount;
791 }
792
793 /**
794  * ioat_dma_free_chan_resources - release all the descriptors
795  * @chan: the channel to be cleaned
796  */
797 static void ioat_dma_free_chan_resources(struct dma_chan *chan)
798 {
799         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
800         struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
801         struct ioat_desc_sw *desc, *_desc;
802         int in_use_descs = 0;
803
804         /* Before freeing channel resources first check
805          * if they have been previously allocated for this channel.
806          */
807         if (ioat_chan->desccount == 0)
808                 return;
809
810         tasklet_disable(&ioat_chan->cleanup_task);
811         ioat_dma_memcpy_cleanup(ioat_chan);
812
813         /* Delay 100ms after reset to allow internal DMA logic to quiesce
814          * before removing DMA descriptor resources.
815          */
816         writeb(IOAT_CHANCMD_RESET,
817                ioat_chan->reg_base
818                         + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
819         mdelay(100);
820
821         spin_lock_bh(&ioat_chan->desc_lock);
822         switch (ioat_chan->device->version) {
823         case IOAT_VER_1_2:
824                 list_for_each_entry_safe(desc, _desc,
825                                          &ioat_chan->used_desc, node) {
826                         in_use_descs++;
827                         list_del(&desc->node);
828                         pci_pool_free(ioatdma_device->dma_pool, desc->hw,
829                                       desc->async_tx.phys);
830                         kfree(desc);
831                 }
832                 list_for_each_entry_safe(desc, _desc,
833                                          &ioat_chan->free_desc, node) {
834                         list_del(&desc->node);
835                         pci_pool_free(ioatdma_device->dma_pool, desc->hw,
836                                       desc->async_tx.phys);
837                         kfree(desc);
838                 }
839                 break;
840         case IOAT_VER_2_0:
841         case IOAT_VER_3_0:
842                 list_for_each_entry_safe(desc, _desc,
843                                          ioat_chan->free_desc.next, node) {
844                         list_del(&desc->node);
845                         pci_pool_free(ioatdma_device->dma_pool, desc->hw,
846                                       desc->async_tx.phys);
847                         kfree(desc);
848                 }
849                 desc = to_ioat_desc(ioat_chan->free_desc.next);
850                 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
851                               desc->async_tx.phys);
852                 kfree(desc);
853                 INIT_LIST_HEAD(&ioat_chan->free_desc);
854                 INIT_LIST_HEAD(&ioat_chan->used_desc);
855                 break;
856         }
857         spin_unlock_bh(&ioat_chan->desc_lock);
858
859         pci_pool_free(ioatdma_device->completion_pool,
860                       ioat_chan->completion_virt,
861                       ioat_chan->completion_addr);
862
863         /* one is ok since we left it on there on purpose */
864         if (in_use_descs > 1)
865                 dev_err(&ioat_chan->device->pdev->dev,
866                         "Freeing %d in use descriptors!\n",
867                         in_use_descs - 1);
868
869         ioat_chan->last_completion = ioat_chan->completion_addr = 0;
870         ioat_chan->pending = 0;
871         ioat_chan->dmacount = 0;
872         ioat_chan->desccount = 0;
873         ioat_chan->watchdog_completion = 0;
874         ioat_chan->last_compl_desc_addr_hw = 0;
875         ioat_chan->watchdog_tcp_cookie =
876                 ioat_chan->watchdog_last_tcp_cookie = 0;
877 }
878
879 /**
880  * ioat_dma_get_next_descriptor - return the next available descriptor
881  * @ioat_chan: IOAT DMA channel handle
882  *
883  * Gets the next descriptor from the chain, and must be called with the
884  * channel's desc_lock held.  Allocates more descriptors if the channel
885  * has run out.
886  */
887 static struct ioat_desc_sw *
888 ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
889 {
890         struct ioat_desc_sw *new;
891
892         if (!list_empty(&ioat_chan->free_desc)) {
893                 new = to_ioat_desc(ioat_chan->free_desc.next);
894                 list_del(&new->node);
895         } else {
896                 /* try to get another desc */
897                 new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
898                 if (!new) {
899                         dev_err(&ioat_chan->device->pdev->dev,
900                                 "alloc failed\n");
901                         return NULL;
902                 }
903         }
904
905         prefetch(new->hw);
906         return new;
907 }
908
909 static struct ioat_desc_sw *
910 ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
911 {
912         struct ioat_desc_sw *new;
913
914         /*
915          * used.prev points to where to start processing
916          * used.next points to next free descriptor
917          * if used.prev == NULL, there are none waiting to be processed
918          * if used.next == used.prev.prev, there is only one free descriptor,
919          *      and we need to use it to as a noop descriptor before
920          *      linking in a new set of descriptors, since the device
921          *      has probably already read the pointer to it
922          */
923         if (ioat_chan->used_desc.prev &&
924             ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
925
926                 struct ioat_desc_sw *desc;
927                 struct ioat_desc_sw *noop_desc;
928                 int i;
929
930                 /* set up the noop descriptor */
931                 noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
932                 /* set size to non-zero value (channel returns error when size is 0) */
933                 noop_desc->hw->size = NULL_DESC_BUFFER_SIZE;
934                 noop_desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
935                 noop_desc->hw->src_addr = 0;
936                 noop_desc->hw->dst_addr = 0;
937
938                 ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
939                 ioat_chan->pending++;
940                 ioat_chan->dmacount++;
941
942                 /* try to get a few more descriptors */
943                 for (i = 16; i; i--) {
944                         desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
945                         if (!desc) {
946                                 dev_err(&ioat_chan->device->pdev->dev,
947                                         "alloc failed\n");
948                                 break;
949                         }
950                         list_add_tail(&desc->node, ioat_chan->used_desc.next);
951
952                         desc->hw->next
953                                 = to_ioat_desc(desc->node.next)->async_tx.phys;
954                         to_ioat_desc(desc->node.prev)->hw->next
955                                 = desc->async_tx.phys;
956                         ioat_chan->desccount++;
957                 }
958
959                 ioat_chan->used_desc.next = noop_desc->node.next;
960         }
961         new = to_ioat_desc(ioat_chan->used_desc.next);
962         prefetch(new);
963         ioat_chan->used_desc.next = new->node.next;
964
965         if (ioat_chan->used_desc.prev == NULL)
966                 ioat_chan->used_desc.prev = &new->node;
967
968         prefetch(new->hw);
969         return new;
970 }
971
972 static struct ioat_desc_sw *ioat_dma_get_next_descriptor(
973                                                 struct ioat_dma_chan *ioat_chan)
974 {
975         if (!ioat_chan)
976                 return NULL;
977
978         switch (ioat_chan->device->version) {
979         case IOAT_VER_1_2:
980                 return ioat1_dma_get_next_descriptor(ioat_chan);
981                 break;
982         case IOAT_VER_2_0:
983         case IOAT_VER_3_0:
984                 return ioat2_dma_get_next_descriptor(ioat_chan);
985                 break;
986         }
987         return NULL;
988 }
989
990 static struct dma_async_tx_descriptor *ioat1_dma_prep_memcpy(
991                                                 struct dma_chan *chan,
992                                                 dma_addr_t dma_dest,
993                                                 dma_addr_t dma_src,
994                                                 size_t len,
995                                                 unsigned long flags)
996 {
997         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
998         struct ioat_desc_sw *new;
999
1000         spin_lock_bh(&ioat_chan->desc_lock);
1001         new = ioat_dma_get_next_descriptor(ioat_chan);
1002         spin_unlock_bh(&ioat_chan->desc_lock);
1003
1004         if (new) {
1005                 new->len = len;
1006                 new->dst = dma_dest;
1007                 new->src = dma_src;
1008                 new->async_tx.flags = flags;
1009                 return &new->async_tx;
1010         } else {
1011                 dev_err(&ioat_chan->device->pdev->dev,
1012                         "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
1013                         chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
1014                 return NULL;
1015         }
1016 }
1017
1018 static struct dma_async_tx_descriptor *ioat2_dma_prep_memcpy(
1019                                                 struct dma_chan *chan,
1020                                                 dma_addr_t dma_dest,
1021                                                 dma_addr_t dma_src,
1022                                                 size_t len,
1023                                                 unsigned long flags)
1024 {
1025         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
1026         struct ioat_desc_sw *new;
1027
1028         spin_lock_bh(&ioat_chan->desc_lock);
1029         new = ioat2_dma_get_next_descriptor(ioat_chan);
1030
1031         /*
1032          * leave ioat_chan->desc_lock set in ioat 2 path
1033          * it will get unlocked at end of tx_submit
1034          */
1035
1036         if (new) {
1037                 new->len = len;
1038                 new->dst = dma_dest;
1039                 new->src = dma_src;
1040                 new->async_tx.flags = flags;
1041                 return &new->async_tx;
1042         } else {
1043                 spin_unlock_bh(&ioat_chan->desc_lock);
1044                 dev_err(&ioat_chan->device->pdev->dev,
1045                         "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
1046                         chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
1047                 return NULL;
1048         }
1049 }
1050
1051 static void ioat_dma_cleanup_tasklet(unsigned long data)
1052 {
1053         struct ioat_dma_chan *chan = (void *)data;
1054         ioat_dma_memcpy_cleanup(chan);
1055         writew(IOAT_CHANCTRL_INT_DISABLE,
1056                chan->reg_base + IOAT_CHANCTRL_OFFSET);
1057 }
1058
1059 static void
1060 ioat_dma_unmap(struct ioat_dma_chan *ioat_chan, struct ioat_desc_sw *desc)
1061 {
1062         /*
1063          * yes we are unmapping both _page and _single
1064          * alloc'd regions with unmap_page. Is this
1065          * *really* that bad?
1066          */
1067         if (!(desc->async_tx.flags & DMA_COMPL_SKIP_DEST_UNMAP))
1068                 pci_unmap_page(ioat_chan->device->pdev,
1069                                 pci_unmap_addr(desc, dst),
1070                                 pci_unmap_len(desc, len),
1071                                 PCI_DMA_FROMDEVICE);
1072
1073         if (!(desc->async_tx.flags & DMA_COMPL_SKIP_SRC_UNMAP))
1074                 pci_unmap_page(ioat_chan->device->pdev,
1075                                 pci_unmap_addr(desc, src),
1076                                 pci_unmap_len(desc, len),
1077                                 PCI_DMA_TODEVICE);
1078 }
1079
1080 /**
1081  * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
1082  * @chan: ioat channel to be cleaned up
1083  */
1084 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
1085 {
1086         unsigned long phys_complete;
1087         struct ioat_desc_sw *desc, *_desc;
1088         dma_cookie_t cookie = 0;
1089         unsigned long desc_phys;
1090         struct ioat_desc_sw *latest_desc;
1091
1092         prefetch(ioat_chan->completion_virt);
1093
1094         if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
1095                 return;
1096
1097         /* The completion writeback can happen at any time,
1098            so reads by the driver need to be atomic operations
1099            The descriptor physical addresses are limited to 32-bits
1100            when the CPU can only do a 32-bit mov */
1101
1102 #if (BITS_PER_LONG == 64)
1103         phys_complete =
1104                 ioat_chan->completion_virt->full
1105                 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
1106 #else
1107         phys_complete =
1108                 ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
1109 #endif
1110
1111         if ((ioat_chan->completion_virt->full
1112                 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
1113                                 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
1114                 dev_err(&ioat_chan->device->pdev->dev,
1115                         "Channel halted, chanerr = %x\n",
1116                         readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
1117
1118                 /* TODO do something to salvage the situation */
1119         }
1120
1121         if (phys_complete == ioat_chan->last_completion) {
1122                 spin_unlock_bh(&ioat_chan->cleanup_lock);
1123                 /*
1124                  * perhaps we're stuck so hard that the watchdog can't go off?
1125                  * try to catch it after 2 seconds
1126                  */
1127                 if (ioat_chan->device->version != IOAT_VER_3_0) {
1128                         if (time_after(jiffies,
1129                                        ioat_chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
1130                                 ioat_dma_chan_watchdog(&(ioat_chan->device->work.work));
1131                                 ioat_chan->last_completion_time = jiffies;
1132                         }
1133                 }
1134                 return;
1135         }
1136         ioat_chan->last_completion_time = jiffies;
1137
1138         cookie = 0;
1139         if (!spin_trylock_bh(&ioat_chan->desc_lock)) {
1140                 spin_unlock_bh(&ioat_chan->cleanup_lock);
1141                 return;
1142         }
1143
1144         switch (ioat_chan->device->version) {
1145         case IOAT_VER_1_2:
1146                 list_for_each_entry_safe(desc, _desc,
1147                                          &ioat_chan->used_desc, node) {
1148
1149                         /*
1150                          * Incoming DMA requests may use multiple descriptors,
1151                          * due to exceeding xfercap, perhaps. If so, only the
1152                          * last one will have a cookie, and require unmapping.
1153                          */
1154                         if (desc->async_tx.cookie) {
1155                                 cookie = desc->async_tx.cookie;
1156                                 ioat_dma_unmap(ioat_chan, desc);
1157                                 if (desc->async_tx.callback) {
1158                                         desc->async_tx.callback(desc->async_tx.callback_param);
1159                                         desc->async_tx.callback = NULL;
1160                                 }
1161                         }
1162
1163                         if (desc->async_tx.phys != phys_complete) {
1164                                 /*
1165                                  * a completed entry, but not the last, so clean
1166                                  * up if the client is done with the descriptor
1167                                  */
1168                                 if (async_tx_test_ack(&desc->async_tx)) {
1169                                         list_del(&desc->node);
1170                                         list_add_tail(&desc->node,
1171                                                       &ioat_chan->free_desc);
1172                                 } else
1173                                         desc->async_tx.cookie = 0;
1174                         } else {
1175                                 /*
1176                                  * last used desc. Do not remove, so we can
1177                                  * append from it, but don't look at it next
1178                                  * time, either
1179                                  */
1180                                 desc->async_tx.cookie = 0;
1181
1182                                 /* TODO check status bits? */
1183                                 break;
1184                         }
1185                 }
1186                 break;
1187         case IOAT_VER_2_0:
1188         case IOAT_VER_3_0:
1189                 /* has some other thread has already cleaned up? */
1190                 if (ioat_chan->used_desc.prev == NULL)
1191                         break;
1192
1193                 /* work backwards to find latest finished desc */
1194                 desc = to_ioat_desc(ioat_chan->used_desc.next);
1195                 latest_desc = NULL;
1196                 do {
1197                         desc = to_ioat_desc(desc->node.prev);
1198                         desc_phys = (unsigned long)desc->async_tx.phys
1199                                        & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
1200                         if (desc_phys == phys_complete) {
1201                                 latest_desc = desc;
1202                                 break;
1203                         }
1204                 } while (&desc->node != ioat_chan->used_desc.prev);
1205
1206                 if (latest_desc != NULL) {
1207
1208                         /* work forwards to clear finished descriptors */
1209                         for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
1210                              &desc->node != latest_desc->node.next &&
1211                              &desc->node != ioat_chan->used_desc.next;
1212                              desc = to_ioat_desc(desc->node.next)) {
1213                                 if (desc->async_tx.cookie) {
1214                                         cookie = desc->async_tx.cookie;
1215                                         desc->async_tx.cookie = 0;
1216                                         ioat_dma_unmap(ioat_chan, desc);
1217                                         if (desc->async_tx.callback) {
1218                                                 desc->async_tx.callback(desc->async_tx.callback_param);
1219                                                 desc->async_tx.callback = NULL;
1220                                         }
1221                                 }
1222                         }
1223
1224                         /* move used.prev up beyond those that are finished */
1225                         if (&desc->node == ioat_chan->used_desc.next)
1226                                 ioat_chan->used_desc.prev = NULL;
1227                         else
1228                                 ioat_chan->used_desc.prev = &desc->node;
1229                 }
1230                 break;
1231         }
1232
1233         spin_unlock_bh(&ioat_chan->desc_lock);
1234
1235         ioat_chan->last_completion = phys_complete;
1236         if (cookie != 0)
1237                 ioat_chan->completed_cookie = cookie;
1238
1239         spin_unlock_bh(&ioat_chan->cleanup_lock);
1240 }
1241
1242 /**
1243  * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
1244  * @chan: IOAT DMA channel handle
1245  * @cookie: DMA transaction identifier
1246  * @done: if not %NULL, updated with last completed transaction
1247  * @used: if not %NULL, updated with last used transaction
1248  */
1249 static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
1250                                             dma_cookie_t cookie,
1251                                             dma_cookie_t *done,
1252                                             dma_cookie_t *used)
1253 {
1254         struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
1255         dma_cookie_t last_used;
1256         dma_cookie_t last_complete;
1257         enum dma_status ret;
1258
1259         last_used = chan->cookie;
1260         last_complete = ioat_chan->completed_cookie;
1261         ioat_chan->watchdog_tcp_cookie = cookie;
1262
1263         if (done)
1264                 *done = last_complete;
1265         if (used)
1266                 *used = last_used;
1267
1268         ret = dma_async_is_complete(cookie, last_complete, last_used);
1269         if (ret == DMA_SUCCESS)
1270                 return ret;
1271
1272         ioat_dma_memcpy_cleanup(ioat_chan);
1273
1274         last_used = chan->cookie;
1275         last_complete = ioat_chan->completed_cookie;
1276
1277         if (done)
1278                 *done = last_complete;
1279         if (used)
1280                 *used = last_used;
1281
1282         return dma_async_is_complete(cookie, last_complete, last_used);
1283 }
1284
1285 static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
1286 {
1287         struct ioat_desc_sw *desc;
1288
1289         spin_lock_bh(&ioat_chan->desc_lock);
1290
1291         desc = ioat_dma_get_next_descriptor(ioat_chan);
1292
1293         if (!desc) {
1294                 dev_err(&ioat_chan->device->pdev->dev,
1295                         "Unable to start null desc - get next desc failed\n");
1296                 spin_unlock_bh(&ioat_chan->desc_lock);
1297                 return;
1298         }
1299
1300         desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
1301                                 | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
1302                                 | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
1303         /* set size to non-zero value (channel returns error when size is 0) */
1304         desc->hw->size = NULL_DESC_BUFFER_SIZE;
1305         desc->hw->src_addr = 0;
1306         desc->hw->dst_addr = 0;
1307         async_tx_ack(&desc->async_tx);
1308         switch (ioat_chan->device->version) {
1309         case IOAT_VER_1_2:
1310                 desc->hw->next = 0;
1311                 list_add_tail(&desc->node, &ioat_chan->used_desc);
1312
1313                 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
1314                        ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
1315                 writel(((u64) desc->async_tx.phys) >> 32,
1316                        ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
1317
1318                 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
1319                         + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
1320                 break;
1321         case IOAT_VER_2_0:
1322         case IOAT_VER_3_0:
1323                 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
1324                        ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
1325                 writel(((u64) desc->async_tx.phys) >> 32,
1326                        ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
1327
1328                 ioat_chan->dmacount++;
1329                 __ioat2_dma_memcpy_issue_pending(ioat_chan);
1330                 break;
1331         }
1332         spin_unlock_bh(&ioat_chan->desc_lock);
1333 }
1334
1335 /*
1336  * Perform a IOAT transaction to verify the HW works.
1337  */
1338 #define IOAT_TEST_SIZE 2000
1339
1340 static void ioat_dma_test_callback(void *dma_async_param)
1341 {
1342         printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
1343                 dma_async_param);
1344 }
1345
1346 /**
1347  * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
1348  * @device: device to be tested
1349  */
1350 static int ioat_dma_self_test(struct ioatdma_device *device)
1351 {
1352         int i;
1353         u8 *src;
1354         u8 *dest;
1355         struct dma_chan *dma_chan;
1356         struct dma_async_tx_descriptor *tx;
1357         dma_addr_t dma_dest, dma_src;
1358         dma_cookie_t cookie;
1359         int err = 0;
1360
1361         src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
1362         if (!src)
1363                 return -ENOMEM;
1364         dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
1365         if (!dest) {
1366                 kfree(src);
1367                 return -ENOMEM;
1368         }
1369
1370         /* Fill in src buffer */
1371         for (i = 0; i < IOAT_TEST_SIZE; i++)
1372                 src[i] = (u8)i;
1373
1374         /* Start copy, using first DMA channel */
1375         dma_chan = container_of(device->common.channels.next,
1376                                 struct dma_chan,
1377                                 device_node);
1378         if (device->common.device_alloc_chan_resources(dma_chan, NULL) < 1) {
1379                 dev_err(&device->pdev->dev,
1380                         "selftest cannot allocate chan resource\n");
1381                 err = -ENODEV;
1382                 goto out;
1383         }
1384
1385         dma_src = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
1386                                  DMA_TO_DEVICE);
1387         dma_dest = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
1388                                   DMA_FROM_DEVICE);
1389         tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
1390                                                    IOAT_TEST_SIZE, 0);
1391         if (!tx) {
1392                 dev_err(&device->pdev->dev,
1393                         "Self-test prep failed, disabling\n");
1394                 err = -ENODEV;
1395                 goto free_resources;
1396         }
1397
1398         async_tx_ack(tx);
1399         tx->callback = ioat_dma_test_callback;
1400         tx->callback_param = (void *)0x8086;
1401         cookie = tx->tx_submit(tx);
1402         if (cookie < 0) {
1403                 dev_err(&device->pdev->dev,
1404                         "Self-test setup failed, disabling\n");
1405                 err = -ENODEV;
1406                 goto free_resources;
1407         }
1408         device->common.device_issue_pending(dma_chan);
1409         msleep(1);
1410
1411         if (device->common.device_is_tx_complete(dma_chan, cookie, NULL, NULL)
1412                                         != DMA_SUCCESS) {
1413                 dev_err(&device->pdev->dev,
1414                         "Self-test copy timed out, disabling\n");
1415                 err = -ENODEV;
1416                 goto free_resources;
1417         }
1418         if (memcmp(src, dest, IOAT_TEST_SIZE)) {
1419                 dev_err(&device->pdev->dev,
1420                         "Self-test copy failed compare, disabling\n");
1421                 err = -ENODEV;
1422                 goto free_resources;
1423         }
1424
1425 free_resources:
1426         device->common.device_free_chan_resources(dma_chan);
1427 out:
1428         kfree(src);
1429         kfree(dest);
1430         return err;
1431 }
1432
1433 static char ioat_interrupt_style[32] = "msix";
1434 module_param_string(ioat_interrupt_style, ioat_interrupt_style,
1435                     sizeof(ioat_interrupt_style), 0644);
1436 MODULE_PARM_DESC(ioat_interrupt_style,
1437                  "set ioat interrupt style: msix (default), "
1438                  "msix-single-vector, msi, intx)");
1439
1440 /**
1441  * ioat_dma_setup_interrupts - setup interrupt handler
1442  * @device: ioat device
1443  */
1444 static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
1445 {
1446         struct ioat_dma_chan *ioat_chan;
1447         int err, i, j, msixcnt;
1448         u8 intrctrl = 0;
1449
1450         if (!strcmp(ioat_interrupt_style, "msix"))
1451                 goto msix;
1452         if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
1453                 goto msix_single_vector;
1454         if (!strcmp(ioat_interrupt_style, "msi"))
1455                 goto msi;
1456         if (!strcmp(ioat_interrupt_style, "intx"))
1457                 goto intx;
1458         dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
1459                 ioat_interrupt_style);
1460         goto err_no_irq;
1461
1462 msix:
1463         /* The number of MSI-X vectors should equal the number of channels */
1464         msixcnt = device->common.chancnt;
1465         for (i = 0; i < msixcnt; i++)
1466                 device->msix_entries[i].entry = i;
1467
1468         err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
1469         if (err < 0)
1470                 goto msi;
1471         if (err > 0)
1472                 goto msix_single_vector;
1473
1474         for (i = 0; i < msixcnt; i++) {
1475                 ioat_chan = ioat_lookup_chan_by_index(device, i);
1476                 err = request_irq(device->msix_entries[i].vector,
1477                                   ioat_dma_do_interrupt_msix,
1478                                   0, "ioat-msix", ioat_chan);
1479                 if (err) {
1480                         for (j = 0; j < i; j++) {
1481                                 ioat_chan =
1482                                         ioat_lookup_chan_by_index(device, j);
1483                                 free_irq(device->msix_entries[j].vector,
1484                                          ioat_chan);
1485                         }
1486                         goto msix_single_vector;
1487                 }
1488         }
1489         intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
1490         device->irq_mode = msix_multi_vector;
1491         goto done;
1492
1493 msix_single_vector:
1494         device->msix_entries[0].entry = 0;
1495         err = pci_enable_msix(device->pdev, device->msix_entries, 1);
1496         if (err)
1497                 goto msi;
1498
1499         err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
1500                           0, "ioat-msix", device);
1501         if (err) {
1502                 pci_disable_msix(device->pdev);
1503                 goto msi;
1504         }
1505         device->irq_mode = msix_single_vector;
1506         goto done;
1507
1508 msi:
1509         err = pci_enable_msi(device->pdev);
1510         if (err)
1511                 goto intx;
1512
1513         err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
1514                           0, "ioat-msi", device);
1515         if (err) {
1516                 pci_disable_msi(device->pdev);
1517                 goto intx;
1518         }
1519         /*
1520          * CB 1.2 devices need a bit set in configuration space to enable MSI
1521          */
1522         if (device->version == IOAT_VER_1_2) {
1523                 u32 dmactrl;
1524                 pci_read_config_dword(device->pdev,
1525                                       IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
1526                 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
1527                 pci_write_config_dword(device->pdev,
1528                                        IOAT_PCI_DMACTRL_OFFSET, dmactrl);
1529         }
1530         device->irq_mode = msi;
1531         goto done;
1532
1533 intx:
1534         err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
1535                           IRQF_SHARED, "ioat-intx", device);
1536         if (err)
1537                 goto err_no_irq;
1538         device->irq_mode = intx;
1539
1540 done:
1541         intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
1542         writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
1543         return 0;
1544
1545 err_no_irq:
1546         /* Disable all interrupt generation */
1547         writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
1548         dev_err(&device->pdev->dev, "no usable interrupts\n");
1549         device->irq_mode = none;
1550         return -1;
1551 }
1552
1553 /**
1554  * ioat_dma_remove_interrupts - remove whatever interrupts were set
1555  * @device: ioat device
1556  */
1557 static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
1558 {
1559         struct ioat_dma_chan *ioat_chan;
1560         int i;
1561
1562         /* Disable all interrupt generation */
1563         writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
1564
1565         switch (device->irq_mode) {
1566         case msix_multi_vector:
1567                 for (i = 0; i < device->common.chancnt; i++) {
1568                         ioat_chan = ioat_lookup_chan_by_index(device, i);
1569                         free_irq(device->msix_entries[i].vector, ioat_chan);
1570                 }
1571                 pci_disable_msix(device->pdev);
1572                 break;
1573         case msix_single_vector:
1574                 free_irq(device->msix_entries[0].vector, device);
1575                 pci_disable_msix(device->pdev);
1576                 break;
1577         case msi:
1578                 free_irq(device->pdev->irq, device);
1579                 pci_disable_msi(device->pdev);
1580                 break;
1581         case intx:
1582                 free_irq(device->pdev->irq, device);
1583                 break;
1584         case none:
1585                 dev_warn(&device->pdev->dev,
1586                          "call to %s without interrupts setup\n", __func__);
1587         }
1588         device->irq_mode = none;
1589 }
1590
1591 struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
1592                                       void __iomem *iobase)
1593 {
1594         int err;
1595         struct ioatdma_device *device;
1596
1597         device = kzalloc(sizeof(*device), GFP_KERNEL);
1598         if (!device) {
1599                 err = -ENOMEM;
1600                 goto err_kzalloc;
1601         }
1602         device->pdev = pdev;
1603         device->reg_base = iobase;
1604         device->version = readb(device->reg_base + IOAT_VER_OFFSET);
1605
1606         /* DMA coherent memory pool for DMA descriptor allocations */
1607         device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
1608                                            sizeof(struct ioat_dma_descriptor),
1609                                            64, 0);
1610         if (!device->dma_pool) {
1611                 err = -ENOMEM;
1612                 goto err_dma_pool;
1613         }
1614
1615         device->completion_pool = pci_pool_create("completion_pool", pdev,
1616                                                   sizeof(u64), SMP_CACHE_BYTES,
1617                                                   SMP_CACHE_BYTES);
1618         if (!device->completion_pool) {
1619                 err = -ENOMEM;
1620                 goto err_completion_pool;
1621         }
1622
1623         INIT_LIST_HEAD(&device->common.channels);
1624         ioat_dma_enumerate_channels(device);
1625
1626         device->common.device_alloc_chan_resources =
1627                                                 ioat_dma_alloc_chan_resources;
1628         device->common.device_free_chan_resources =
1629                                                 ioat_dma_free_chan_resources;
1630         device->common.dev = &pdev->dev;
1631
1632         dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
1633         device->common.device_is_tx_complete = ioat_dma_is_complete;
1634         switch (device->version) {
1635         case IOAT_VER_1_2:
1636                 device->common.device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1637                 device->common.device_issue_pending =
1638                                                 ioat1_dma_memcpy_issue_pending;
1639                 break;
1640         case IOAT_VER_2_0:
1641         case IOAT_VER_3_0:
1642                 device->common.device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
1643                 device->common.device_issue_pending =
1644                                                 ioat2_dma_memcpy_issue_pending;
1645                 break;
1646         }
1647
1648         dev_err(&device->pdev->dev,
1649                 "Intel(R) I/OAT DMA Engine found,"
1650                 " %d channels, device version 0x%02x, driver version %s\n",
1651                 device->common.chancnt, device->version, IOAT_DMA_VERSION);
1652
1653         err = ioat_dma_setup_interrupts(device);
1654         if (err)
1655                 goto err_setup_interrupts;
1656
1657         err = ioat_dma_self_test(device);
1658         if (err)
1659                 goto err_self_test;
1660
1661         ioat_set_tcp_copy_break(device);
1662
1663         dma_async_device_register(&device->common);
1664
1665         if (device->version != IOAT_VER_3_0) {
1666                 INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
1667                 schedule_delayed_work(&device->work,
1668                                       WATCHDOG_DELAY);
1669         }
1670
1671         return device;
1672
1673 err_self_test:
1674         ioat_dma_remove_interrupts(device);
1675 err_setup_interrupts:
1676         pci_pool_destroy(device->completion_pool);
1677 err_completion_pool:
1678         pci_pool_destroy(device->dma_pool);
1679 err_dma_pool:
1680         kfree(device);
1681 err_kzalloc:
1682         dev_err(&pdev->dev,
1683                 "Intel(R) I/OAT DMA Engine initialization failed\n");
1684         return NULL;
1685 }
1686
1687 void ioat_dma_remove(struct ioatdma_device *device)
1688 {
1689         struct dma_chan *chan, *_chan;
1690         struct ioat_dma_chan *ioat_chan;
1691
1692         ioat_dma_remove_interrupts(device);
1693
1694         dma_async_device_unregister(&device->common);
1695
1696         pci_pool_destroy(device->dma_pool);
1697         pci_pool_destroy(device->completion_pool);
1698
1699         iounmap(device->reg_base);
1700         pci_release_regions(device->pdev);
1701         pci_disable_device(device->pdev);
1702
1703         if (device->version != IOAT_VER_3_0) {
1704                 cancel_delayed_work(&device->work);
1705         }
1706
1707         list_for_each_entry_safe(chan, _chan,
1708                                  &device->common.channels, device_node) {
1709                 ioat_chan = to_ioat_chan(chan);
1710                 list_del(&chan->device_node);
1711                 kfree(ioat_chan);
1712         }
1713         kfree(device);
1714 }
1715