blob: 2e81e0c76e61ce8ab5145eb4cd8fafb862d9937f [file] [log] [blame]
Chris Leech0bbd5f42006-05-23 17:35:34 -07001/*
Shannon Nelson43d6e362007-10-16 01:27:39 -07002 * Intel I/OAT DMA Linux driver
Maciej Sosnowski211a22c2009-02-26 11:05:43 +01003 * Copyright(c) 2004 - 2009 Intel Corporation.
Chris Leech0bbd5f42006-05-23 17:35:34 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
Shannon Nelson43d6e362007-10-16 01:27:39 -07006 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
Chris Leech0bbd5f42006-05-23 17:35:34 -07008 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
Shannon Nelson43d6e362007-10-16 01:27:39 -070015 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
Chris Leech0bbd5f42006-05-23 17:35:34 -070017 *
Shannon Nelson43d6e362007-10-16 01:27:39 -070018 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
Chris Leech0bbd5f42006-05-23 17:35:34 -070021 */
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>
David S. Miller6b00c922006-05-23 17:37:58 -070034#include <linux/dma-mapping.h>
Maciej Sosnowski09177e82008-07-22 10:07:33 -070035#include <linux/workqueue.h>
Venki Pallipadi3ad0b022008-10-22 16:34:52 -070036#include <linux/i7300_idle.h>
Dan Williams584ec222009-07-28 14:32:12 -070037#include "dma.h"
38#include "registers.h"
39#include "hw.h"
Chris Leech0bbd5f42006-05-23 17:35:34 -070040
Shannon Nelson7bb67c12007-11-14 16:59:51 -080041static int ioat_pending_level = 4;
42module_param(ioat_pending_level, int, 0644);
43MODULE_PARM_DESC(ioat_pending_level,
44 "high-water mark for pushing ioat descriptors (default: 4)");
45
Maciej Sosnowski09177e82008-07-22 10:07:33 -070046static void ioat_dma_chan_reset_part2(struct work_struct *work);
47static void ioat_dma_chan_watchdog(struct work_struct *work);
48
Chris Leech0bbd5f42006-05-23 17:35:34 -070049/* internal functions */
Dan Williamsdcbc8532009-07-28 14:44:50 -070050static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat);
51static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat);
Shannon Nelson7bb67c12007-11-14 16:59:51 -080052
Shannon Nelson7f2b2912007-10-18 03:07:14 -070053static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -070054ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat);
Shannon Nelson7bb67c12007-11-14 16:59:51 -080055static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -070056ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -070057
Dan Williamsdcbc8532009-07-28 14:44:50 -070058static inline struct ioat_chan_common *
Dan Williamsbc3c7022009-07-28 14:33:42 -070059ioat_chan_by_index(struct ioatdma_device *device, int index)
Shannon Nelson3e037452007-10-16 01:27:40 -070060{
61 return device->idx[index];
62}
63
64/**
65 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
66 * @irq: interrupt id
67 * @data: interrupt data
68 */
69static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
70{
71 struct ioatdma_device *instance = data;
Dan Williamsdcbc8532009-07-28 14:44:50 -070072 struct ioat_chan_common *chan;
Shannon Nelson3e037452007-10-16 01:27:40 -070073 unsigned long attnstatus;
74 int bit;
75 u8 intrctrl;
76
77 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
78
79 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
80 return IRQ_NONE;
81
82 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
83 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
84 return IRQ_NONE;
85 }
86
87 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
88 for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
Dan Williamsdcbc8532009-07-28 14:44:50 -070089 chan = ioat_chan_by_index(instance, bit);
90 tasklet_schedule(&chan->cleanup_task);
Shannon Nelson3e037452007-10-16 01:27:40 -070091 }
92
93 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
94 return IRQ_HANDLED;
95}
96
97/**
98 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
99 * @irq: interrupt id
100 * @data: interrupt data
101 */
102static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
103{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700104 struct ioat_chan_common *chan = data;
Shannon Nelson3e037452007-10-16 01:27:40 -0700105
Dan Williamsdcbc8532009-07-28 14:44:50 -0700106 tasklet_schedule(&chan->cleanup_task);
Shannon Nelson3e037452007-10-16 01:27:40 -0700107
108 return IRQ_HANDLED;
109}
110
111static void ioat_dma_cleanup_tasklet(unsigned long data);
112
113/**
114 * ioat_dma_enumerate_channels - find and initialize the device's channels
115 * @device: the device to be enumerated
116 */
Shannon Nelson8ab89562007-10-16 01:27:39 -0700117static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700118{
119 u8 xfercap_scale;
120 u32 xfercap;
121 int i;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700122 struct ioat_chan_common *chan;
123 struct ioat_dma_chan *ioat;
Dan Williamse6c0b692009-09-08 17:29:44 -0700124 struct device *dev = &device->pdev->dev;
Dan Williamsf2427e22009-07-28 14:42:38 -0700125 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700126
Dan Williamsf2427e22009-07-28 14:42:38 -0700127 INIT_LIST_HEAD(&dma->channels);
128 dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
Chris Leeche3828812007-03-08 09:57:35 -0800129 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700130 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
131
Venki Pallipadif371be62008-10-23 15:39:06 -0700132#ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
Dan Williamsf2427e22009-07-28 14:42:38 -0700133 if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
134 dma->chancnt--;
Andy Henroid27471fd2008-10-09 11:45:22 -0700135#endif
Dan Williamsf2427e22009-07-28 14:42:38 -0700136 for (i = 0; i < dma->chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700137 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
138 if (!ioat) {
Dan Williamsf2427e22009-07-28 14:42:38 -0700139 dma->chancnt = i;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700140 break;
141 }
142
Dan Williamsdcbc8532009-07-28 14:44:50 -0700143 chan = &ioat->base;
144 chan->device = device;
145 chan->reg_base = device->reg_base + (0x80 * (i + 1));
146 ioat->xfercap = xfercap;
147 ioat->desccount = 0;
148 INIT_DELAYED_WORK(&chan->work, ioat_dma_chan_reset_part2);
149 spin_lock_init(&chan->cleanup_lock);
150 spin_lock_init(&ioat->desc_lock);
151 INIT_LIST_HEAD(&ioat->free_desc);
152 INIT_LIST_HEAD(&ioat->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700153 /* This should be made common somewhere in dmaengine.c */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700154 chan->common.device = &device->common;
155 list_add_tail(&chan->common.device_node, &dma->channels);
156 device->idx[i] = chan;
157 tasklet_init(&chan->cleanup_task,
Shannon Nelson3e037452007-10-16 01:27:40 -0700158 ioat_dma_cleanup_tasklet,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700159 (unsigned long) ioat);
160 tasklet_disable(&chan->cleanup_task);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700161 }
Dan Williamsf2427e22009-07-28 14:42:38 -0700162 return dma->chancnt;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700163}
164
Shannon Nelson711924b2007-12-17 16:20:08 -0800165/**
166 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
167 * descriptors to hw
168 * @chan: DMA channel handle
169 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700170static inline void
Dan Williamsdcbc8532009-07-28 14:44:50 -0700171__ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat)
Shannon Nelson711924b2007-12-17 16:20:08 -0800172{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700173 void __iomem *reg_base = ioat->base.reg_base;
174
175 ioat->pending = 0;
176 writeb(IOAT_CHANCMD_APPEND, reg_base + IOAT1_CHANCMD_OFFSET);
Shannon Nelson711924b2007-12-17 16:20:08 -0800177}
178
179static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
180{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700181 struct ioat_dma_chan *ioat = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800182
Dan Williamsdcbc8532009-07-28 14:44:50 -0700183 if (ioat->pending > 0) {
184 spin_lock_bh(&ioat->desc_lock);
185 __ioat1_dma_memcpy_issue_pending(ioat);
186 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800187 }
188}
189
Dan Williamsbc3c7022009-07-28 14:33:42 -0700190static inline void
Dan Williamsdcbc8532009-07-28 14:44:50 -0700191__ioat2_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat)
Shannon Nelson711924b2007-12-17 16:20:08 -0800192{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700193 void __iomem *reg_base = ioat->base.reg_base;
194
195 ioat->pending = 0;
196 writew(ioat->dmacount, reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
Shannon Nelson711924b2007-12-17 16:20:08 -0800197}
198
199static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
200{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700201 struct ioat_dma_chan *ioat = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800202
Dan Williamsdcbc8532009-07-28 14:44:50 -0700203 if (ioat->pending > 0) {
204 spin_lock_bh(&ioat->desc_lock);
205 __ioat2_dma_memcpy_issue_pending(ioat);
206 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800207 }
208}
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800209
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700210
211/**
212 * ioat_dma_chan_reset_part2 - reinit the channel after a reset
213 */
214static void ioat_dma_chan_reset_part2(struct work_struct *work)
215{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700216 struct ioat_chan_common *chan;
217 struct ioat_dma_chan *ioat;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700218 struct ioat_desc_sw *desc;
219
Dan Williamsdcbc8532009-07-28 14:44:50 -0700220 chan = container_of(work, struct ioat_chan_common, work.work);
221 ioat = container_of(chan, struct ioat_dma_chan, base);
222 spin_lock_bh(&chan->cleanup_lock);
223 spin_lock_bh(&ioat->desc_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700224
Dan Williamsdcbc8532009-07-28 14:44:50 -0700225 chan->completion_virt->low = 0;
226 chan->completion_virt->high = 0;
227 ioat->pending = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700228
229 /*
230 * count the descriptors waiting, and be sure to do it
231 * right for both the CB1 line and the CB2 ring
232 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700233 ioat->dmacount = 0;
234 if (ioat->used_desc.prev) {
235 desc = to_ioat_desc(ioat->used_desc.prev);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700236 do {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700237 ioat->dmacount++;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700238 desc = to_ioat_desc(desc->node.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700239 } while (&desc->node != ioat->used_desc.next);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700240 }
241
242 /*
243 * write the new starting descriptor address
244 * this puts channel engine into ARMED state
245 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700246 desc = to_ioat_desc(ioat->used_desc.prev);
247 switch (chan->device->version) {
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700248 case IOAT_VER_1_2:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700249 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700250 chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700251 writel(((u64) desc->txd.phys) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700252 chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700253
Dan Williamsdcbc8532009-07-28 14:44:50 -0700254 writeb(IOAT_CHANCMD_START, chan->reg_base
255 + IOAT_CHANCMD_OFFSET(chan->device->version));
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700256 break;
257 case IOAT_VER_2_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700258 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700259 chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700260 writel(((u64) desc->txd.phys) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700261 chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700262
263 /* tell the engine to go with what's left to be done */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700264 writew(ioat->dmacount,
265 chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700266
267 break;
268 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700269 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700270 "chan%d reset - %d descs waiting, %d total desc\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700271 chan_num(chan), ioat->dmacount, ioat->desccount);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700272
Dan Williamsdcbc8532009-07-28 14:44:50 -0700273 spin_unlock_bh(&ioat->desc_lock);
274 spin_unlock_bh(&chan->cleanup_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700275}
276
277/**
278 * ioat_dma_reset_channel - restart a channel
Dan Williamsdcbc8532009-07-28 14:44:50 -0700279 * @ioat: IOAT DMA channel handle
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700280 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700281static void ioat_dma_reset_channel(struct ioat_dma_chan *ioat)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700282{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700283 struct ioat_chan_common *chan = &ioat->base;
284 void __iomem *reg_base = chan->reg_base;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700285 u32 chansts, chanerr;
286
Dan Williamsdcbc8532009-07-28 14:44:50 -0700287 if (!ioat->used_desc.prev)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700288 return;
289
Dan Williamsdcbc8532009-07-28 14:44:50 -0700290 chanerr = readl(reg_base + IOAT_CHANERR_OFFSET);
291 chansts = (chan->completion_virt->low
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700292 & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
293 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700294 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700295 "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700296 chan_num(chan), chansts, chanerr);
297 writel(chanerr, reg_base + IOAT_CHANERR_OFFSET);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700298 }
299
300 /*
301 * whack it upside the head with a reset
302 * and wait for things to settle out.
303 * force the pending count to a really big negative
304 * to make sure no one forces an issue_pending
305 * while we're waiting.
306 */
307
Dan Williamsdcbc8532009-07-28 14:44:50 -0700308 spin_lock_bh(&ioat->desc_lock);
309 ioat->pending = INT_MIN;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700310 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700311 reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
312 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700313
314 /* schedule the 2nd half instead of sleeping a long time */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700315 schedule_delayed_work(&chan->work, RESET_DELAY);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700316}
317
318/**
319 * ioat_dma_chan_watchdog - watch for stuck channels
320 */
321static void ioat_dma_chan_watchdog(struct work_struct *work)
322{
323 struct ioatdma_device *device =
324 container_of(work, struct ioatdma_device, work.work);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700325 struct ioat_dma_chan *ioat;
326 struct ioat_chan_common *chan;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700327 int i;
328
329 union {
330 u64 full;
331 struct {
332 u32 low;
333 u32 high;
334 };
335 } completion_hw;
336 unsigned long compl_desc_addr_hw;
337
338 for (i = 0; i < device->common.chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700339 chan = ioat_chan_by_index(device, i);
340 ioat = container_of(chan, struct ioat_dma_chan, base);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700341
Dan Williamsdcbc8532009-07-28 14:44:50 -0700342 if (chan->device->version == IOAT_VER_1_2
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700343 /* have we started processing anything yet */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700344 && chan->last_completion
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700345 /* have we completed any since last watchdog cycle? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700346 && (chan->last_completion == chan->watchdog_completion)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700347 /* has TCP stuck on one cookie since last watchdog? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700348 && (chan->watchdog_tcp_cookie == chan->watchdog_last_tcp_cookie)
349 && (chan->watchdog_tcp_cookie != chan->completed_cookie)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700350 /* is there something in the chain to be processed? */
351 /* CB1 chain always has at least the last one processed */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700352 && (ioat->used_desc.prev != ioat->used_desc.next)
353 && ioat->pending == 0) {
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700354
355 /*
356 * check CHANSTS register for completed
357 * descriptor address.
358 * if it is different than completion writeback,
359 * it is not zero
360 * and it has changed since the last watchdog
361 * we can assume that channel
362 * is still working correctly
363 * and the problem is in completion writeback.
364 * update completion writeback
365 * with actual CHANSTS value
366 * else
367 * try resetting the channel
368 */
369
Dan Williamsdcbc8532009-07-28 14:44:50 -0700370 completion_hw.low = readl(chan->reg_base +
371 IOAT_CHANSTS_OFFSET_LOW(chan->device->version));
372 completion_hw.high = readl(chan->reg_base +
373 IOAT_CHANSTS_OFFSET_HIGH(chan->device->version));
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700374#if (BITS_PER_LONG == 64)
375 compl_desc_addr_hw =
376 completion_hw.full
377 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
378#else
379 compl_desc_addr_hw =
380 completion_hw.low & IOAT_LOW_COMPLETION_MASK;
381#endif
382
383 if ((compl_desc_addr_hw != 0)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700384 && (compl_desc_addr_hw != chan->watchdog_completion)
385 && (compl_desc_addr_hw != chan->last_compl_desc_addr_hw)) {
386 chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
387 chan->completion_virt->low = completion_hw.low;
388 chan->completion_virt->high = completion_hw.high;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700389 } else {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700390 ioat_dma_reset_channel(ioat);
391 chan->watchdog_completion = 0;
392 chan->last_compl_desc_addr_hw = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700393 }
394
395 /*
396 * for version 2.0 if there are descriptors yet to be processed
397 * and the last completed hasn't changed since the last watchdog
398 * if they haven't hit the pending level
399 * issue the pending to push them through
400 * else
401 * try resetting the channel
402 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700403 } else if (chan->device->version == IOAT_VER_2_0
404 && ioat->used_desc.prev
405 && chan->last_completion
406 && chan->last_completion == chan->watchdog_completion) {
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700407
Dan Williamsdcbc8532009-07-28 14:44:50 -0700408 if (ioat->pending < ioat_pending_level)
409 ioat2_dma_memcpy_issue_pending(&chan->common);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700410 else {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700411 ioat_dma_reset_channel(ioat);
412 chan->watchdog_completion = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700413 }
414 } else {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700415 chan->last_compl_desc_addr_hw = 0;
416 chan->watchdog_completion = chan->last_completion;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700417 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700418 chan->watchdog_last_tcp_cookie = chan->watchdog_tcp_cookie;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700419 }
420
421 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
422}
423
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800424static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700425{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700426 struct dma_chan *c = tx->chan;
427 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700428 struct ioat_desc_sw *desc = tx_to_ioat_desc(tx);
429 struct ioat_desc_sw *first;
430 struct ioat_desc_sw *chain_tail;
Dan Williams7405f742007-01-02 11:10:43 -0700431 dma_cookie_t cookie;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700432
Dan Williamsdcbc8532009-07-28 14:44:50 -0700433 spin_lock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700434 /* cookie incr and addition to used_list must be atomic */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700435 cookie = c->cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700436 cookie++;
437 if (cookie < 0)
438 cookie = 1;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700439 c->cookie = cookie;
440 tx->cookie = cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700441
442 /* write address into NextDescriptor field of last desc in chain */
Dan Williamsa0587bc2009-07-28 14:44:04 -0700443 first = to_ioat_desc(tx->tx_list.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700444 chain_tail = to_ioat_desc(ioat->used_desc.prev);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700445 /* make descriptor updates globally visible before chaining */
446 wmb();
447 chain_tail->hw->next = first->txd.phys;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700448 list_splice_tail_init(&tx->tx_list, &ioat->used_desc);
Dan Williams7405f742007-01-02 11:10:43 -0700449
Dan Williamsdcbc8532009-07-28 14:44:50 -0700450 ioat->dmacount += desc->tx_cnt;
451 ioat->pending += desc->tx_cnt;
452 if (ioat->pending >= ioat_pending_level)
453 __ioat1_dma_memcpy_issue_pending(ioat);
454 spin_unlock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700455
Dan Williams7405f742007-01-02 11:10:43 -0700456 return cookie;
457}
458
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800459static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
460{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700461 struct ioat_dma_chan *ioat = to_ioat_chan(tx->chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800462 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
463 struct ioat_desc_sw *new;
464 struct ioat_dma_descriptor *hw;
465 dma_cookie_t cookie;
466 u32 copy;
467 size_t len;
468 dma_addr_t src, dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700469 unsigned long orig_flags;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800470 unsigned int desc_count = 0;
471
472 /* src and dest and len are stored in the initial descriptor */
473 len = first->len;
474 src = first->src;
475 dst = first->dst;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700476 orig_flags = first->txd.flags;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800477 new = first;
478
Shannon Nelson711924b2007-12-17 16:20:08 -0800479 /*
Dan Williamsdcbc8532009-07-28 14:44:50 -0700480 * ioat->desc_lock is still in force in version 2 path
Shannon Nelson711924b2007-12-17 16:20:08 -0800481 * it gets unlocked at end of this function
482 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800483 do {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700484 copy = min_t(size_t, len, ioat->xfercap);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800485
Dan Williamsbc3c7022009-07-28 14:33:42 -0700486 async_tx_ack(&new->txd);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800487
488 hw = new->hw;
489 hw->size = copy;
490 hw->ctl = 0;
491 hw->src_addr = src;
492 hw->dst_addr = dst;
493
494 len -= copy;
495 dst += copy;
496 src += copy;
497 desc_count++;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700498 } while (len && (new = ioat2_dma_get_next_descriptor(ioat)));
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800499
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700500 if (!new) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700501 dev_err(to_dev(&ioat->base), "tx submit failed\n");
502 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700503 return -ENOMEM;
504 }
505
Dan Williamsc7984f42009-07-28 14:44:04 -0700506 hw->ctl_f.compl_write = 1;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700507 if (first->txd.callback) {
Dan Williamsc7984f42009-07-28 14:44:04 -0700508 hw->ctl_f.int_en = 1;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800509 if (first != new) {
510 /* move callback into to last desc */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700511 new->txd.callback = first->txd.callback;
512 new->txd.callback_param
513 = first->txd.callback_param;
514 first->txd.callback = NULL;
515 first->txd.callback_param = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800516 }
517 }
518
519 new->tx_cnt = desc_count;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700520 new->txd.flags = orig_flags; /* client is in control of this ack */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800521
522 /* store the original values for use in later cleanup */
523 if (new != first) {
524 new->src = first->src;
525 new->dst = first->dst;
526 new->len = first->len;
527 }
528
529 /* cookie incr and addition to used_list must be atomic */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700530 cookie = ioat->base.common.cookie;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800531 cookie++;
532 if (cookie < 0)
533 cookie = 1;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700534 ioat->base.common.cookie = new->txd.cookie = cookie;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800535
Dan Williamsdcbc8532009-07-28 14:44:50 -0700536 ioat->dmacount += desc_count;
537 ioat->pending += desc_count;
538 if (ioat->pending >= ioat_pending_level)
539 __ioat2_dma_memcpy_issue_pending(ioat);
540 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800541
542 return cookie;
543}
544
545/**
546 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
Dan Williamsdcbc8532009-07-28 14:44:50 -0700547 * @ioat: the channel supplying the memory pool for the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800548 * @flags: allocation flags
549 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700550static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700551ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat, gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700552{
553 struct ioat_dma_descriptor *desc;
554 struct ioat_desc_sw *desc_sw;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700555 struct ioatdma_device *ioatdma_device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700556 dma_addr_t phys;
557
Dan Williamsdcbc8532009-07-28 14:44:50 -0700558 ioatdma_device = ioat->base.device;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700559 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700560 if (unlikely(!desc))
561 return NULL;
562
563 desc_sw = kzalloc(sizeof(*desc_sw), flags);
564 if (unlikely(!desc_sw)) {
Shannon Nelson8ab89562007-10-16 01:27:39 -0700565 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700566 return NULL;
567 }
568
569 memset(desc, 0, sizeof(*desc));
Dan Williamsdcbc8532009-07-28 14:44:50 -0700570 dma_async_tx_descriptor_init(&desc_sw->txd, &ioat->base.common);
571 switch (ioatdma_device->version) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800572 case IOAT_VER_1_2:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700573 desc_sw->txd.tx_submit = ioat1_tx_submit;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800574 break;
575 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700576 case IOAT_VER_3_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700577 desc_sw->txd.tx_submit = ioat2_tx_submit;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800578 break;
579 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800580
Chris Leech0bbd5f42006-05-23 17:35:34 -0700581 desc_sw->hw = desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700582 desc_sw->txd.phys = phys;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700583
584 return desc_sw;
585}
586
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800587static int ioat_initial_desc_count = 256;
588module_param(ioat_initial_desc_count, int, 0644);
589MODULE_PARM_DESC(ioat_initial_desc_count,
590 "initial descriptors per channel (default: 256)");
591
592/**
593 * ioat2_dma_massage_chan_desc - link the descriptors into a circle
Dan Williamsdcbc8532009-07-28 14:44:50 -0700594 * @ioat: the channel to be massaged
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800595 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700596static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800597{
598 struct ioat_desc_sw *desc, *_desc;
599
600 /* setup used_desc */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700601 ioat->used_desc.next = ioat->free_desc.next;
602 ioat->used_desc.prev = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800603
604 /* pull free_desc out of the circle so that every node is a hw
605 * descriptor, but leave it pointing to the list
606 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700607 ioat->free_desc.prev->next = ioat->free_desc.next;
608 ioat->free_desc.next->prev = ioat->free_desc.prev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800609
610 /* circle link the hw descriptors */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700611 desc = to_ioat_desc(ioat->free_desc.next);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700612 desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700613 list_for_each_entry_safe(desc, _desc, ioat->free_desc.next, node) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700614 desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800615 }
616}
617
618/**
619 * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
620 * @chan: the channel to be filled out
621 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700622static int ioat_dma_alloc_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700623{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700624 struct ioat_dma_chan *ioat = to_ioat_chan(c);
625 struct ioat_chan_common *chan = &ioat->base;
Shannon Nelson711924b2007-12-17 16:20:08 -0800626 struct ioat_desc_sw *desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700627 u16 chanctrl;
628 u32 chanerr;
629 int i;
630 LIST_HEAD(tmp_list);
631
Shannon Nelsone4223972007-08-24 23:02:53 -0700632 /* have we already been set up? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700633 if (!list_empty(&ioat->free_desc))
634 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700635
Shannon Nelson43d6e362007-10-16 01:27:39 -0700636 /* Setup register to interrupt and write completion status on error */
Shannon Nelsone4223972007-08-24 23:02:53 -0700637 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
Chris Leech0bbd5f42006-05-23 17:35:34 -0700638 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
639 IOAT_CHANCTRL_ERR_COMPLETION_EN;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700640 writew(chanctrl, chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700641
Dan Williamsdcbc8532009-07-28 14:44:50 -0700642 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700643 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700644 dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
645 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700646 }
647
648 /* Allocate descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800649 for (i = 0; i < ioat_initial_desc_count; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700650 desc = ioat_dma_alloc_descriptor(ioat, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700651 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700652 dev_err(to_dev(chan), "Only %d initial descriptors\n", i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700653 break;
654 }
655 list_add_tail(&desc->node, &tmp_list);
656 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700657 spin_lock_bh(&ioat->desc_lock);
658 ioat->desccount = i;
659 list_splice(&tmp_list, &ioat->free_desc);
660 if (chan->device->version != IOAT_VER_1_2)
661 ioat2_dma_massage_chan_desc(ioat);
662 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700663
664 /* allocate a completion writeback area */
665 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700666 chan->completion_virt = pci_pool_alloc(chan->device->completion_pool,
667 GFP_KERNEL,
668 &chan->completion_addr);
669 memset(chan->completion_virt, 0,
670 sizeof(*chan->completion_virt));
671 writel(((u64) chan->completion_addr) & 0x00000000FFFFFFFF,
672 chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
673 writel(((u64) chan->completion_addr) >> 32,
674 chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700675
Dan Williamsdcbc8532009-07-28 14:44:50 -0700676 tasklet_enable(&chan->cleanup_task);
677 ioat_dma_start_null_desc(ioat); /* give chain to dma device */
678 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700679}
680
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800681/**
682 * ioat_dma_free_chan_resources - release all the descriptors
683 * @chan: the channel to be cleaned
684 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700685static void ioat_dma_free_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700686{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700687 struct ioat_dma_chan *ioat = to_ioat_chan(c);
688 struct ioat_chan_common *chan = &ioat->base;
689 struct ioatdma_device *ioatdma_device = chan->device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700690 struct ioat_desc_sw *desc, *_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700691 int in_use_descs = 0;
692
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000693 /* Before freeing channel resources first check
694 * if they have been previously allocated for this channel.
695 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700696 if (ioat->desccount == 0)
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000697 return;
698
Dan Williamsdcbc8532009-07-28 14:44:50 -0700699 tasklet_disable(&chan->cleanup_task);
700 ioat_dma_memcpy_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700701
Shannon Nelson3e037452007-10-16 01:27:40 -0700702 /* Delay 100ms after reset to allow internal DMA logic to quiesce
703 * before removing DMA descriptor resources.
704 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800705 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700706 chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
Shannon Nelson3e037452007-10-16 01:27:40 -0700707 mdelay(100);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700708
Dan Williamsdcbc8532009-07-28 14:44:50 -0700709 spin_lock_bh(&ioat->desc_lock);
710 switch (chan->device->version) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800711 case IOAT_VER_1_2:
712 list_for_each_entry_safe(desc, _desc,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700713 &ioat->used_desc, node) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800714 in_use_descs++;
715 list_del(&desc->node);
716 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700717 desc->txd.phys);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800718 kfree(desc);
719 }
720 list_for_each_entry_safe(desc, _desc,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700721 &ioat->free_desc, node) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800722 list_del(&desc->node);
723 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700724 desc->txd.phys);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800725 kfree(desc);
726 }
727 break;
728 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700729 case IOAT_VER_3_0:
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800730 list_for_each_entry_safe(desc, _desc,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700731 ioat->free_desc.next, node) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800732 list_del(&desc->node);
733 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700734 desc->txd.phys);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800735 kfree(desc);
736 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700737 desc = to_ioat_desc(ioat->free_desc.next);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700738 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700739 desc->txd.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700740 kfree(desc);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700741 INIT_LIST_HEAD(&ioat->free_desc);
742 INIT_LIST_HEAD(&ioat->used_desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800743 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700744 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700745 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700746
Shannon Nelson8ab89562007-10-16 01:27:39 -0700747 pci_pool_free(ioatdma_device->completion_pool,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700748 chan->completion_virt,
749 chan->completion_addr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700750
751 /* one is ok since we left it on there on purpose */
752 if (in_use_descs > 1)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700753 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700754 in_use_descs - 1);
755
Dan Williamsdcbc8532009-07-28 14:44:50 -0700756 chan->last_completion = chan->completion_addr = 0;
757 chan->watchdog_completion = 0;
758 chan->last_compl_desc_addr_hw = 0;
759 chan->watchdog_tcp_cookie = chan->watchdog_last_tcp_cookie = 0;
760 ioat->pending = 0;
761 ioat->dmacount = 0;
762 ioat->desccount = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700763}
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700764
Shannon Nelson3e037452007-10-16 01:27:40 -0700765/**
Dan Williamsdcbc8532009-07-28 14:44:50 -0700766 * ioat1_dma_get_next_descriptor - return the next available descriptor
767 * @ioat: IOAT DMA channel handle
Shannon Nelson3e037452007-10-16 01:27:40 -0700768 *
769 * Gets the next descriptor from the chain, and must be called with the
770 * channel's desc_lock held. Allocates more descriptors if the channel
771 * has run out.
772 */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700773static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700774ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat)
Shannon Nelson3e037452007-10-16 01:27:40 -0700775{
Shannon Nelson711924b2007-12-17 16:20:08 -0800776 struct ioat_desc_sw *new;
Shannon Nelson3e037452007-10-16 01:27:40 -0700777
Dan Williamsdcbc8532009-07-28 14:44:50 -0700778 if (!list_empty(&ioat->free_desc)) {
779 new = to_ioat_desc(ioat->free_desc.next);
Shannon Nelson3e037452007-10-16 01:27:40 -0700780 list_del(&new->node);
781 } else {
782 /* try to get another desc */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700783 new = ioat_dma_alloc_descriptor(ioat, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800784 if (!new) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700785 dev_err(to_dev(&ioat->base), "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800786 return NULL;
787 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700788 }
789
790 prefetch(new->hw);
791 return new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700792}
793
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800794static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700795ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800796{
Shannon Nelson711924b2007-12-17 16:20:08 -0800797 struct ioat_desc_sw *new;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800798
799 /*
800 * used.prev points to where to start processing
801 * used.next points to next free descriptor
802 * if used.prev == NULL, there are none waiting to be processed
803 * if used.next == used.prev.prev, there is only one free descriptor,
804 * and we need to use it to as a noop descriptor before
805 * linking in a new set of descriptors, since the device
806 * has probably already read the pointer to it
807 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700808 if (ioat->used_desc.prev &&
809 ioat->used_desc.next == ioat->used_desc.prev->prev) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800810
Shannon Nelson711924b2007-12-17 16:20:08 -0800811 struct ioat_desc_sw *desc;
812 struct ioat_desc_sw *noop_desc;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800813 int i;
814
815 /* set up the noop descriptor */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700816 noop_desc = to_ioat_desc(ioat->used_desc.next);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700817 /* set size to non-zero value (channel returns error when size is 0) */
818 noop_desc->hw->size = NULL_DESC_BUFFER_SIZE;
Dan Williamsc7984f42009-07-28 14:44:04 -0700819 noop_desc->hw->ctl = 0;
820 noop_desc->hw->ctl_f.null = 1;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800821 noop_desc->hw->src_addr = 0;
822 noop_desc->hw->dst_addr = 0;
823
Dan Williamsdcbc8532009-07-28 14:44:50 -0700824 ioat->used_desc.next = ioat->used_desc.next->next;
825 ioat->pending++;
826 ioat->dmacount++;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800827
Shannon Nelson711924b2007-12-17 16:20:08 -0800828 /* try to get a few more descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800829 for (i = 16; i; i--) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700830 desc = ioat_dma_alloc_descriptor(ioat, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800831 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700832 dev_err(to_dev(&ioat->base),
833 "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800834 break;
835 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700836 list_add_tail(&desc->node, ioat->used_desc.next);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800837
838 desc->hw->next
Dan Williamsbc3c7022009-07-28 14:33:42 -0700839 = to_ioat_desc(desc->node.next)->txd.phys;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800840 to_ioat_desc(desc->node.prev)->hw->next
Dan Williamsbc3c7022009-07-28 14:33:42 -0700841 = desc->txd.phys;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700842 ioat->desccount++;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800843 }
844
Dan Williamsdcbc8532009-07-28 14:44:50 -0700845 ioat->used_desc.next = noop_desc->node.next;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800846 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700847 new = to_ioat_desc(ioat->used_desc.next);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800848 prefetch(new);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700849 ioat->used_desc.next = new->node.next;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800850
Dan Williamsdcbc8532009-07-28 14:44:50 -0700851 if (ioat->used_desc.prev == NULL)
852 ioat->used_desc.prev = &new->node;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800853
854 prefetch(new->hw);
855 return new;
856}
857
Dan Williamsbc3c7022009-07-28 14:33:42 -0700858static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700859ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800860{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700861 if (!ioat)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800862 return NULL;
863
Dan Williamsdcbc8532009-07-28 14:44:50 -0700864 switch (ioat->base.device->version) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800865 case IOAT_VER_1_2:
Dan Williamsdcbc8532009-07-28 14:44:50 -0700866 return ioat1_dma_get_next_descriptor(ioat);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800867 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700868 case IOAT_VER_3_0:
Dan Williamsdcbc8532009-07-28 14:44:50 -0700869 return ioat2_dma_get_next_descriptor(ioat);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800870 }
871 return NULL;
872}
873
Dan Williamsbc3c7022009-07-28 14:33:42 -0700874static struct dma_async_tx_descriptor *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700875ioat1_dma_prep_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700876 dma_addr_t dma_src, size_t len, unsigned long flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700877{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700878 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700879 struct ioat_desc_sw *desc;
880 size_t copy;
881 LIST_HEAD(chain);
882 dma_addr_t src = dma_src;
883 dma_addr_t dest = dma_dest;
884 size_t total_len = len;
885 struct ioat_dma_descriptor *hw = NULL;
886 int tx_cnt = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700887
Dan Williamsdcbc8532009-07-28 14:44:50 -0700888 spin_lock_bh(&ioat->desc_lock);
889 desc = ioat_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700890 do {
891 if (!desc)
892 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700893
Dan Williamsa0587bc2009-07-28 14:44:04 -0700894 tx_cnt++;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700895 copy = min_t(size_t, len, ioat->xfercap);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700896
897 hw = desc->hw;
898 hw->size = copy;
899 hw->ctl = 0;
900 hw->src_addr = src;
901 hw->dst_addr = dest;
902
903 list_add_tail(&desc->node, &chain);
904
905 len -= copy;
906 dest += copy;
907 src += copy;
908 if (len) {
909 struct ioat_desc_sw *next;
910
911 async_tx_ack(&desc->txd);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700912 next = ioat_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700913 hw->next = next ? next->txd.phys : 0;
914 desc = next;
915 } else
916 hw->next = 0;
917 } while (len);
918
919 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700920 struct ioat_chan_common *chan = &ioat->base;
921
922 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700923 "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700924 chan_num(chan), ioat->dmacount, ioat->desccount);
925 list_splice(&chain, &ioat->free_desc);
926 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800927 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700928 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700929 spin_unlock_bh(&ioat->desc_lock);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700930
931 desc->txd.flags = flags;
932 desc->tx_cnt = tx_cnt;
933 desc->src = dma_src;
934 desc->dst = dma_dest;
935 desc->len = total_len;
936 list_splice(&chain, &desc->txd.tx_list);
937 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
938 hw->ctl_f.compl_write = 1;
939
940 return &desc->txd;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700941}
942
Dan Williamsbc3c7022009-07-28 14:33:42 -0700943static struct dma_async_tx_descriptor *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700944ioat2_dma_prep_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700945 dma_addr_t dma_src, size_t len, unsigned long flags)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800946{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700947 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800948 struct ioat_desc_sw *new;
949
Dan Williamsdcbc8532009-07-28 14:44:50 -0700950 spin_lock_bh(&ioat->desc_lock);
951 new = ioat2_dma_get_next_descriptor(ioat);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800952
Shannon Nelson711924b2007-12-17 16:20:08 -0800953 /*
Dan Williamsdcbc8532009-07-28 14:44:50 -0700954 * leave ioat->desc_lock set in ioat 2 path
Shannon Nelson711924b2007-12-17 16:20:08 -0800955 * it will get unlocked at end of tx_submit
956 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800957
Shannon Nelson711924b2007-12-17 16:20:08 -0800958 if (new) {
959 new->len = len;
Dan Williams00367312008-02-02 19:49:57 -0700960 new->dst = dma_dest;
961 new->src = dma_src;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700962 new->txd.flags = flags;
963 return &new->txd;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700964 } else {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700965 struct ioat_chan_common *chan = &ioat->base;
966
967 spin_unlock_bh(&ioat->desc_lock);
968 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700969 "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700970 chan_num(chan), ioat->dmacount, ioat->desccount);
Shannon Nelson711924b2007-12-17 16:20:08 -0800971 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700972 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700973}
974
Shannon Nelson3e037452007-10-16 01:27:40 -0700975static void ioat_dma_cleanup_tasklet(unsigned long data)
976{
977 struct ioat_dma_chan *chan = (void *)data;
978 ioat_dma_memcpy_cleanup(chan);
979 writew(IOAT_CHANCTRL_INT_DISABLE,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700980 chan->base.reg_base + IOAT_CHANCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -0700981}
982
Dan Williamse1d181e2008-07-04 00:13:40 -0700983static void
Dan Williamsdcbc8532009-07-28 14:44:50 -0700984ioat_dma_unmap(struct ioat_chan_common *chan, struct ioat_desc_sw *desc)
Dan Williamse1d181e2008-07-04 00:13:40 -0700985{
Dan Williamsbc3c7022009-07-28 14:33:42 -0700986 if (!(desc->txd.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
987 if (desc->txd.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700988 pci_unmap_single(chan->device->pdev,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200989 pci_unmap_addr(desc, dst),
990 pci_unmap_len(desc, len),
991 PCI_DMA_FROMDEVICE);
992 else
Dan Williamsdcbc8532009-07-28 14:44:50 -0700993 pci_unmap_page(chan->device->pdev,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200994 pci_unmap_addr(desc, dst),
995 pci_unmap_len(desc, len),
996 PCI_DMA_FROMDEVICE);
997 }
Dan Williamse1d181e2008-07-04 00:13:40 -0700998
Dan Williamsbc3c7022009-07-28 14:33:42 -0700999 if (!(desc->txd.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
1000 if (desc->txd.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
Dan Williamsdcbc8532009-07-28 14:44:50 -07001001 pci_unmap_single(chan->device->pdev,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001002 pci_unmap_addr(desc, src),
1003 pci_unmap_len(desc, len),
1004 PCI_DMA_TODEVICE);
1005 else
Dan Williamsdcbc8532009-07-28 14:44:50 -07001006 pci_unmap_page(chan->device->pdev,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001007 pci_unmap_addr(desc, src),
1008 pci_unmap_len(desc, len),
1009 PCI_DMA_TODEVICE);
1010 }
Dan Williamse1d181e2008-07-04 00:13:40 -07001011}
1012
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001013/**
1014 * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
1015 * @chan: ioat channel to be cleaned up
1016 */
Dan Williamsdcbc8532009-07-28 14:44:50 -07001017static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001018{
Dan Williamsdcbc8532009-07-28 14:44:50 -07001019 struct ioat_chan_common *chan = &ioat->base;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001020 unsigned long phys_complete;
1021 struct ioat_desc_sw *desc, *_desc;
1022 dma_cookie_t cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001023 unsigned long desc_phys;
1024 struct ioat_desc_sw *latest_desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001025 struct dma_async_tx_descriptor *tx;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001026
Dan Williamsdcbc8532009-07-28 14:44:50 -07001027 prefetch(chan->completion_virt);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001028
Dan Williamsdcbc8532009-07-28 14:44:50 -07001029 if (!spin_trylock_bh(&chan->cleanup_lock))
Chris Leech0bbd5f42006-05-23 17:35:34 -07001030 return;
1031
1032 /* The completion writeback can happen at any time,
1033 so reads by the driver need to be atomic operations
1034 The descriptor physical addresses are limited to 32-bits
1035 when the CPU can only do a 32-bit mov */
1036
1037#if (BITS_PER_LONG == 64)
1038 phys_complete =
Dan Williamsdcbc8532009-07-28 14:44:50 -07001039 chan->completion_virt->full
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001040 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001041#else
Dan Williamsdcbc8532009-07-28 14:44:50 -07001042 phys_complete = chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001043#endif
1044
Dan Williamsdcbc8532009-07-28 14:44:50 -07001045 if ((chan->completion_virt->full
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001046 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
Shannon Nelson43d6e362007-10-16 01:27:39 -07001047 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
Dan Williamsdcbc8532009-07-28 14:44:50 -07001048 dev_err(to_dev(chan), "Channel halted, chanerr = %x\n",
1049 readl(chan->reg_base + IOAT_CHANERR_OFFSET));
Chris Leech0bbd5f42006-05-23 17:35:34 -07001050
1051 /* TODO do something to salvage the situation */
1052 }
1053
Dan Williamsdcbc8532009-07-28 14:44:50 -07001054 if (phys_complete == chan->last_completion) {
1055 spin_unlock_bh(&chan->cleanup_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001056 /*
1057 * perhaps we're stuck so hard that the watchdog can't go off?
1058 * try to catch it after 2 seconds
1059 */
Dan Williamsdcbc8532009-07-28 14:44:50 -07001060 if (chan->device->version != IOAT_VER_3_0) {
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001061 if (time_after(jiffies,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001062 chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
1063 ioat_dma_chan_watchdog(&(chan->device->work.work));
1064 chan->last_completion_time = jiffies;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001065 }
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001066 }
1067 return;
1068 }
Dan Williamsdcbc8532009-07-28 14:44:50 -07001069 chan->last_completion_time = jiffies;
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001070
1071 cookie = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -07001072 if (!spin_trylock_bh(&ioat->desc_lock)) {
1073 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001074 return;
1075 }
1076
Dan Williamsdcbc8532009-07-28 14:44:50 -07001077 switch (chan->device->version) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001078 case IOAT_VER_1_2:
Dan Williamsdcbc8532009-07-28 14:44:50 -07001079 list_for_each_entry_safe(desc, _desc, &ioat->used_desc, node) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001080 tx = &desc->txd;
Shannon Nelson43d6e362007-10-16 01:27:39 -07001081 /*
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001082 * Incoming DMA requests may use multiple descriptors,
1083 * due to exceeding xfercap, perhaps. If so, only the
1084 * last one will have a cookie, and require unmapping.
Shannon Nelson43d6e362007-10-16 01:27:39 -07001085 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001086 if (tx->cookie) {
1087 cookie = tx->cookie;
Dan Williamsdcbc8532009-07-28 14:44:50 -07001088 ioat_dma_unmap(chan, desc);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001089 if (tx->callback) {
1090 tx->callback(tx->callback_param);
1091 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001092 }
1093 }
1094
Dan Williamsbc3c7022009-07-28 14:33:42 -07001095 if (tx->phys != phys_complete) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001096 /*
1097 * a completed entry, but not the last, so clean
1098 * up if the client is done with the descriptor
1099 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001100 if (async_tx_test_ack(tx)) {
Eric Sesterhennaa2d0b82009-02-26 11:05:30 +01001101 list_move_tail(&desc->node,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001102 &ioat->free_desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001103 } else
Dan Williamsbc3c7022009-07-28 14:33:42 -07001104 tx->cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001105 } else {
1106 /*
1107 * last used desc. Do not remove, so we can
1108 * append from it, but don't look at it next
1109 * time, either
1110 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001111 tx->cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001112
1113 /* TODO check status bits? */
1114 break;
Shannon Nelson95218432007-10-18 03:07:15 -07001115 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001116 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001117 break;
1118 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001119 case IOAT_VER_3_0:
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001120 /* has some other thread has already cleaned up? */
Dan Williamsdcbc8532009-07-28 14:44:50 -07001121 if (ioat->used_desc.prev == NULL)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001122 break;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001123
1124 /* work backwards to find latest finished desc */
Dan Williamsdcbc8532009-07-28 14:44:50 -07001125 desc = to_ioat_desc(ioat->used_desc.next);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001126 tx = &desc->txd;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001127 latest_desc = NULL;
1128 do {
1129 desc = to_ioat_desc(desc->node.prev);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001130 desc_phys = (unsigned long)tx->phys
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001131 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
1132 if (desc_phys == phys_complete) {
1133 latest_desc = desc;
1134 break;
1135 }
Dan Williamsdcbc8532009-07-28 14:44:50 -07001136 } while (&desc->node != ioat->used_desc.prev);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001137
1138 if (latest_desc != NULL) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001139 /* work forwards to clear finished descriptors */
Dan Williamsdcbc8532009-07-28 14:44:50 -07001140 for (desc = to_ioat_desc(ioat->used_desc.prev);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001141 &desc->node != latest_desc->node.next &&
Dan Williamsdcbc8532009-07-28 14:44:50 -07001142 &desc->node != ioat->used_desc.next;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001143 desc = to_ioat_desc(desc->node.next)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001144 if (tx->cookie) {
1145 cookie = tx->cookie;
1146 tx->cookie = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -07001147 ioat_dma_unmap(chan, desc);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001148 if (tx->callback) {
1149 tx->callback(tx->callback_param);
1150 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001151 }
1152 }
1153 }
1154
1155 /* move used.prev up beyond those that are finished */
Dan Williamsdcbc8532009-07-28 14:44:50 -07001156 if (&desc->node == ioat->used_desc.next)
1157 ioat->used_desc.prev = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001158 else
Dan Williamsdcbc8532009-07-28 14:44:50 -07001159 ioat->used_desc.prev = &desc->node;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001160 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001161 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001162 }
1163
Dan Williamsdcbc8532009-07-28 14:44:50 -07001164 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001165
Dan Williamsdcbc8532009-07-28 14:44:50 -07001166 chan->last_completion = phys_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001167 if (cookie != 0)
Dan Williamsdcbc8532009-07-28 14:44:50 -07001168 chan->completed_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001169
Dan Williamsdcbc8532009-07-28 14:44:50 -07001170 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001171}
1172
1173/**
1174 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
1175 * @chan: IOAT DMA channel handle
1176 * @cookie: DMA transaction identifier
Randy Dunlap65088712006-07-03 19:45:31 -07001177 * @done: if not %NULL, updated with last completed transaction
1178 * @used: if not %NULL, updated with last used transaction
Chris Leech0bbd5f42006-05-23 17:35:34 -07001179 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001180static enum dma_status
Dan Williamsdcbc8532009-07-28 14:44:50 -07001181ioat_dma_is_complete(struct dma_chan *c, dma_cookie_t cookie,
Dan Williamsbc3c7022009-07-28 14:33:42 -07001182 dma_cookie_t *done, dma_cookie_t *used)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001183{
Dan Williamsdcbc8532009-07-28 14:44:50 -07001184 struct ioat_dma_chan *ioat = to_ioat_chan(c);
1185 struct ioat_chan_common *chan = &ioat->base;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001186 dma_cookie_t last_used;
1187 dma_cookie_t last_complete;
1188 enum dma_status ret;
1189
Dan Williamsdcbc8532009-07-28 14:44:50 -07001190 last_used = c->cookie;
1191 last_complete = chan->completed_cookie;
1192 chan->watchdog_tcp_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001193
1194 if (done)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001195 *done = last_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001196 if (used)
1197 *used = last_used;
1198
1199 ret = dma_async_is_complete(cookie, last_complete, last_used);
1200 if (ret == DMA_SUCCESS)
1201 return ret;
1202
Dan Williamsdcbc8532009-07-28 14:44:50 -07001203 ioat_dma_memcpy_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001204
Dan Williamsdcbc8532009-07-28 14:44:50 -07001205 last_used = c->cookie;
1206 last_complete = chan->completed_cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001207
1208 if (done)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001209 *done = last_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001210 if (used)
1211 *used = last_used;
1212
1213 return dma_async_is_complete(cookie, last_complete, last_used);
1214}
1215
Dan Williamsdcbc8532009-07-28 14:44:50 -07001216static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001217{
Dan Williamsdcbc8532009-07-28 14:44:50 -07001218 struct ioat_chan_common *chan = &ioat->base;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001219 struct ioat_desc_sw *desc;
Dan Williamsc7984f42009-07-28 14:44:04 -07001220 struct ioat_dma_descriptor *hw;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001221
Dan Williamsdcbc8532009-07-28 14:44:50 -07001222 spin_lock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001223
Dan Williamsdcbc8532009-07-28 14:44:50 -07001224 desc = ioat_dma_get_next_descriptor(ioat);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001225
1226 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -07001227 dev_err(to_dev(chan),
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001228 "Unable to start null desc - get next desc failed\n");
Dan Williamsdcbc8532009-07-28 14:44:50 -07001229 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001230 return;
1231 }
1232
Dan Williamsc7984f42009-07-28 14:44:04 -07001233 hw = desc->hw;
1234 hw->ctl = 0;
1235 hw->ctl_f.null = 1;
1236 hw->ctl_f.int_en = 1;
1237 hw->ctl_f.compl_write = 1;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001238 /* set size to non-zero value (channel returns error when size is 0) */
Dan Williamsc7984f42009-07-28 14:44:04 -07001239 hw->size = NULL_DESC_BUFFER_SIZE;
1240 hw->src_addr = 0;
1241 hw->dst_addr = 0;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001242 async_tx_ack(&desc->txd);
Dan Williamsdcbc8532009-07-28 14:44:50 -07001243 switch (chan->device->version) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001244 case IOAT_VER_1_2:
Dan Williamsc7984f42009-07-28 14:44:04 -07001245 hw->next = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -07001246 list_add_tail(&desc->node, &ioat->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001247
Dan Williamsbc3c7022009-07-28 14:33:42 -07001248 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001249 chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001250 writel(((u64) desc->txd.phys) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001251 chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001252
Dan Williamsdcbc8532009-07-28 14:44:50 -07001253 writeb(IOAT_CHANCMD_START, chan->reg_base
1254 + IOAT_CHANCMD_OFFSET(chan->device->version));
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001255 break;
1256 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001257 case IOAT_VER_3_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -07001258 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001259 chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001260 writel(((u64) desc->txd.phys) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001261 chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001262
Dan Williamsdcbc8532009-07-28 14:44:50 -07001263 ioat->dmacount++;
1264 __ioat2_dma_memcpy_issue_pending(ioat);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001265 break;
1266 }
Dan Williamsdcbc8532009-07-28 14:44:50 -07001267 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001268}
1269
1270/*
1271 * Perform a IOAT transaction to verify the HW works.
1272 */
1273#define IOAT_TEST_SIZE 2000
1274
Shannon Nelson95218432007-10-18 03:07:15 -07001275static void ioat_dma_test_callback(void *dma_async_param)
1276{
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001277 struct completion *cmp = dma_async_param;
1278
1279 complete(cmp);
Shannon Nelson95218432007-10-18 03:07:15 -07001280}
1281
Shannon Nelson3e037452007-10-16 01:27:40 -07001282/**
1283 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
1284 * @device: device to be tested
1285 */
1286static int ioat_dma_self_test(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001287{
1288 int i;
1289 u8 *src;
1290 u8 *dest;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001291 struct dma_device *dma = &device->common;
1292 struct device *dev = &device->pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001293 struct dma_chan *dma_chan;
Shannon Nelson711924b2007-12-17 16:20:08 -08001294 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -07001295 dma_addr_t dma_dest, dma_src;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001296 dma_cookie_t cookie;
1297 int err = 0;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001298 struct completion cmp;
Dan Williams0c33e1c2009-03-02 13:31:35 -07001299 unsigned long tmo;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001300 unsigned long flags;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001301
Christoph Lametere94b1762006-12-06 20:33:17 -08001302 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001303 if (!src)
1304 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -08001305 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001306 if (!dest) {
1307 kfree(src);
1308 return -ENOMEM;
1309 }
1310
1311 /* Fill in src buffer */
1312 for (i = 0; i < IOAT_TEST_SIZE; i++)
1313 src[i] = (u8)i;
1314
1315 /* Start copy, using first DMA channel */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001316 dma_chan = container_of(dma->channels.next, struct dma_chan,
Shannon Nelson43d6e362007-10-16 01:27:39 -07001317 device_node);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001318 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1319 dev_err(dev, "selftest cannot allocate chan resource\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001320 err = -ENODEV;
1321 goto out;
1322 }
1323
Dan Williamsbc3c7022009-07-28 14:33:42 -07001324 dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
1325 dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
Dan Williamsa6a39ca2009-07-28 14:44:05 -07001326 flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE |
1327 DMA_PREP_INTERRUPT;
Dan Williams00367312008-02-02 19:49:57 -07001328 tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001329 IOAT_TEST_SIZE, flags);
Shannon Nelson5149fd02007-10-18 03:07:13 -07001330 if (!tx) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001331 dev_err(dev, "Self-test prep failed, disabling\n");
Shannon Nelson5149fd02007-10-18 03:07:13 -07001332 err = -ENODEV;
1333 goto free_resources;
1334 }
1335
Dan Williams7405f742007-01-02 11:10:43 -07001336 async_tx_ack(tx);
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001337 init_completion(&cmp);
Shannon Nelson95218432007-10-18 03:07:15 -07001338 tx->callback = ioat_dma_test_callback;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001339 tx->callback_param = &cmp;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001340 cookie = tx->tx_submit(tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001341 if (cookie < 0) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001342 dev_err(dev, "Self-test setup failed, disabling\n");
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001343 err = -ENODEV;
1344 goto free_resources;
1345 }
Dan Williamsbc3c7022009-07-28 14:33:42 -07001346 dma->device_issue_pending(dma_chan);
Dan Williams532d3b12008-12-03 17:16:55 -07001347
Dan Williams0c33e1c2009-03-02 13:31:35 -07001348 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
Chris Leech0bbd5f42006-05-23 17:35:34 -07001349
Dan Williams0c33e1c2009-03-02 13:31:35 -07001350 if (tmo == 0 ||
Dan Williamsbc3c7022009-07-28 14:33:42 -07001351 dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001352 != DMA_SUCCESS) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001353 dev_err(dev, "Self-test copy timed out, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001354 err = -ENODEV;
1355 goto free_resources;
1356 }
1357 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001358 dev_err(dev, "Self-test copy failed compare, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001359 err = -ENODEV;
1360 goto free_resources;
1361 }
1362
1363free_resources:
Dan Williamsbc3c7022009-07-28 14:33:42 -07001364 dma->device_free_chan_resources(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001365out:
1366 kfree(src);
1367 kfree(dest);
1368 return err;
1369}
1370
Shannon Nelson3e037452007-10-16 01:27:40 -07001371static char ioat_interrupt_style[32] = "msix";
1372module_param_string(ioat_interrupt_style, ioat_interrupt_style,
1373 sizeof(ioat_interrupt_style), 0644);
1374MODULE_PARM_DESC(ioat_interrupt_style,
1375 "set ioat interrupt style: msix (default), "
1376 "msix-single-vector, msi, intx)");
1377
1378/**
1379 * ioat_dma_setup_interrupts - setup interrupt handler
1380 * @device: ioat device
1381 */
1382static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
1383{
Dan Williamsdcbc8532009-07-28 14:44:50 -07001384 struct ioat_chan_common *chan;
Dan Williamse6c0b692009-09-08 17:29:44 -07001385 struct pci_dev *pdev = device->pdev;
1386 struct device *dev = &pdev->dev;
1387 struct msix_entry *msix;
1388 int i, j, msixcnt;
1389 int err = -EINVAL;
Shannon Nelson3e037452007-10-16 01:27:40 -07001390 u8 intrctrl = 0;
1391
1392 if (!strcmp(ioat_interrupt_style, "msix"))
1393 goto msix;
1394 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
1395 goto msix_single_vector;
1396 if (!strcmp(ioat_interrupt_style, "msi"))
1397 goto msi;
1398 if (!strcmp(ioat_interrupt_style, "intx"))
1399 goto intx;
Dan Williamse6c0b692009-09-08 17:29:44 -07001400 dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
Shannon Nelson5149fd02007-10-18 03:07:13 -07001401 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001402
1403msix:
1404 /* The number of MSI-X vectors should equal the number of channels */
1405 msixcnt = device->common.chancnt;
1406 for (i = 0; i < msixcnt; i++)
1407 device->msix_entries[i].entry = i;
1408
Dan Williamse6c0b692009-09-08 17:29:44 -07001409 err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
Shannon Nelson3e037452007-10-16 01:27:40 -07001410 if (err < 0)
1411 goto msi;
1412 if (err > 0)
1413 goto msix_single_vector;
1414
1415 for (i = 0; i < msixcnt; i++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001416 msix = &device->msix_entries[i];
Dan Williamsdcbc8532009-07-28 14:44:50 -07001417 chan = ioat_chan_by_index(device, i);
Dan Williamse6c0b692009-09-08 17:29:44 -07001418 err = devm_request_irq(dev, msix->vector,
1419 ioat_dma_do_interrupt_msix, 0,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001420 "ioat-msix", chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001421 if (err) {
1422 for (j = 0; j < i; j++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001423 msix = &device->msix_entries[j];
Dan Williamsdcbc8532009-07-28 14:44:50 -07001424 chan = ioat_chan_by_index(device, j);
1425 devm_free_irq(dev, msix->vector, chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001426 }
1427 goto msix_single_vector;
1428 }
1429 }
1430 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
Shannon Nelson3e037452007-10-16 01:27:40 -07001431 goto done;
1432
1433msix_single_vector:
Dan Williamse6c0b692009-09-08 17:29:44 -07001434 msix = &device->msix_entries[0];
1435 msix->entry = 0;
1436 err = pci_enable_msix(pdev, device->msix_entries, 1);
Shannon Nelson3e037452007-10-16 01:27:40 -07001437 if (err)
1438 goto msi;
1439
Dan Williamse6c0b692009-09-08 17:29:44 -07001440 err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
1441 "ioat-msix", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001442 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001443 pci_disable_msix(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001444 goto msi;
1445 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001446 goto done;
1447
1448msi:
Dan Williamse6c0b692009-09-08 17:29:44 -07001449 err = pci_enable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001450 if (err)
1451 goto intx;
1452
Dan Williamse6c0b692009-09-08 17:29:44 -07001453 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
1454 "ioat-msi", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001455 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001456 pci_disable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001457 goto intx;
1458 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001459 goto done;
1460
1461intx:
Dan Williamse6c0b692009-09-08 17:29:44 -07001462 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
1463 IRQF_SHARED, "ioat-intx", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001464 if (err)
1465 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001466
1467done:
Dan Williamsf2427e22009-07-28 14:42:38 -07001468 if (device->intr_quirk)
1469 device->intr_quirk(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001470 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
1471 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
1472 return 0;
1473
1474err_no_irq:
1475 /* Disable all interrupt generation */
1476 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Dan Williamse6c0b692009-09-08 17:29:44 -07001477 dev_err(dev, "no usable interrupts\n");
1478 return err;
Shannon Nelson3e037452007-10-16 01:27:40 -07001479}
1480
Dan Williamse6c0b692009-09-08 17:29:44 -07001481static void ioat_disable_interrupts(struct ioatdma_device *device)
Shannon Nelson3e037452007-10-16 01:27:40 -07001482{
Shannon Nelson3e037452007-10-16 01:27:40 -07001483 /* Disable all interrupt generation */
1484 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -07001485}
1486
Dan Williamsf2427e22009-07-28 14:42:38 -07001487static int ioat_probe(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001488{
Dan Williamsf2427e22009-07-28 14:42:38 -07001489 int err = -ENODEV;
1490 struct dma_device *dma = &device->common;
1491 struct pci_dev *pdev = device->pdev;
Dan Williamse6c0b692009-09-08 17:29:44 -07001492 struct device *dev = &pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001493
1494 /* DMA coherent memory pool for DMA descriptor allocations */
1495 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
Shannon Nelson8ab89562007-10-16 01:27:39 -07001496 sizeof(struct ioat_dma_descriptor),
1497 64, 0);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001498 if (!device->dma_pool) {
1499 err = -ENOMEM;
1500 goto err_dma_pool;
1501 }
1502
Shannon Nelson43d6e362007-10-16 01:27:39 -07001503 device->completion_pool = pci_pool_create("completion_pool", pdev,
1504 sizeof(u64), SMP_CACHE_BYTES,
1505 SMP_CACHE_BYTES);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001506 if (!device->completion_pool) {
1507 err = -ENOMEM;
1508 goto err_completion_pool;
1509 }
1510
Shannon Nelson43d6e362007-10-16 01:27:39 -07001511 ioat_dma_enumerate_channels(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001512
Dan Williamsf2427e22009-07-28 14:42:38 -07001513 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001514 dma->device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
1515 dma->device_free_chan_resources = ioat_dma_free_chan_resources;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001516 dma->device_is_tx_complete = ioat_dma_is_complete;
Dan Williamsf2427e22009-07-28 14:42:38 -07001517 dma->dev = &pdev->dev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001518
Dan Williamse6c0b692009-09-08 17:29:44 -07001519 dev_err(dev, "Intel(R) I/OAT DMA Engine found,"
Shannon Nelson5149fd02007-10-18 03:07:13 -07001520 " %d channels, device version 0x%02x, driver version %s\n",
Dan Williamsbc3c7022009-07-28 14:33:42 -07001521 dma->chancnt, device->version, IOAT_DMA_VERSION);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001522
Dan Williamsbc3c7022009-07-28 14:33:42 -07001523 if (!dma->chancnt) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001524 dev_err(dev, "Intel(R) I/OAT DMA Engine problem found: "
Maciej Sosnowski8b794b12009-02-26 11:04:54 +01001525 "zero channels detected\n");
1526 goto err_setup_interrupts;
1527 }
1528
Shannon Nelson3e037452007-10-16 01:27:40 -07001529 err = ioat_dma_setup_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001530 if (err)
Shannon Nelson3e037452007-10-16 01:27:40 -07001531 goto err_setup_interrupts;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001532
Shannon Nelson3e037452007-10-16 01:27:40 -07001533 err = ioat_dma_self_test(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001534 if (err)
1535 goto err_self_test;
1536
Dan Williamsf2427e22009-07-28 14:42:38 -07001537 return 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001538
1539err_self_test:
Dan Williamse6c0b692009-09-08 17:29:44 -07001540 ioat_disable_interrupts(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001541err_setup_interrupts:
Chris Leech0bbd5f42006-05-23 17:35:34 -07001542 pci_pool_destroy(device->completion_pool);
1543err_completion_pool:
1544 pci_pool_destroy(device->dma_pool);
1545err_dma_pool:
Dan Williamsf2427e22009-07-28 14:42:38 -07001546 return err;
1547}
1548
1549static int ioat_register(struct ioatdma_device *device)
1550{
1551 int err = dma_async_device_register(&device->common);
1552
1553 if (err) {
1554 ioat_disable_interrupts(device);
1555 pci_pool_destroy(device->completion_pool);
1556 pci_pool_destroy(device->dma_pool);
1557 }
1558
1559 return err;
1560}
1561
1562/* ioat1_intr_quirk - fix up dma ctrl register to enable / disable msi */
1563static void ioat1_intr_quirk(struct ioatdma_device *device)
1564{
1565 struct pci_dev *pdev = device->pdev;
1566 u32 dmactrl;
1567
1568 pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
1569 if (pdev->msi_enabled)
1570 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
1571 else
1572 dmactrl &= ~IOAT_PCI_DMACTRL_MSI_EN;
1573 pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
1574}
1575
1576int ioat1_dma_probe(struct ioatdma_device *device, int dca)
1577{
1578 struct pci_dev *pdev = device->pdev;
1579 struct dma_device *dma;
1580 int err;
1581
1582 device->intr_quirk = ioat1_intr_quirk;
1583 dma = &device->common;
1584 dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1585 dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
1586
1587 err = ioat_probe(device);
1588 if (err)
1589 return err;
1590 ioat_set_tcp_copy_break(4096);
1591 err = ioat_register(device);
1592 if (err)
1593 return err;
1594 if (dca)
1595 device->dca = ioat_dca_init(pdev, device->reg_base);
1596
1597 INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
1598 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
1599
1600 return err;
1601}
1602
1603int ioat2_dma_probe(struct ioatdma_device *device, int dca)
1604{
1605 struct pci_dev *pdev = device->pdev;
1606 struct dma_device *dma;
Dan Williamsdcbc8532009-07-28 14:44:50 -07001607 struct dma_chan *c;
1608 struct ioat_chan_common *chan;
Dan Williamsf2427e22009-07-28 14:42:38 -07001609 int err;
1610
1611 dma = &device->common;
1612 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
1613 dma->device_issue_pending = ioat2_dma_memcpy_issue_pending;
1614
1615 err = ioat_probe(device);
1616 if (err)
1617 return err;
1618 ioat_set_tcp_copy_break(2048);
1619
Dan Williamsdcbc8532009-07-28 14:44:50 -07001620 list_for_each_entry(c, &dma->channels, device_node) {
1621 chan = to_chan_common(c);
Dan Williamsf2427e22009-07-28 14:42:38 -07001622 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE | IOAT_DMA_DCA_ANY_CPU,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001623 chan->reg_base + IOAT_DCACTRL_OFFSET);
Dan Williamsf2427e22009-07-28 14:42:38 -07001624 }
1625
1626 err = ioat_register(device);
1627 if (err)
1628 return err;
1629 if (dca)
1630 device->dca = ioat2_dca_init(pdev, device->reg_base);
1631
1632 INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
1633 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
1634
1635 return err;
1636}
1637
1638int ioat3_dma_probe(struct ioatdma_device *device, int dca)
1639{
1640 struct pci_dev *pdev = device->pdev;
1641 struct dma_device *dma;
Dan Williamsdcbc8532009-07-28 14:44:50 -07001642 struct dma_chan *c;
1643 struct ioat_chan_common *chan;
Dan Williamsf2427e22009-07-28 14:42:38 -07001644 int err;
1645 u16 dev_id;
1646
1647 dma = &device->common;
1648 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
1649 dma->device_issue_pending = ioat2_dma_memcpy_issue_pending;
1650
1651 /* -= IOAT ver.3 workarounds =- */
1652 /* Write CHANERRMSK_INT with 3E07h to mask out the errors
1653 * that can cause stability issues for IOAT ver.3
1654 */
1655 pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
1656
1657 /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
1658 * (workaround for spurious config parity error after restart)
1659 */
1660 pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
1661 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
1662 pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
1663
1664 err = ioat_probe(device);
1665 if (err)
1666 return err;
1667 ioat_set_tcp_copy_break(262144);
1668
Dan Williamsdcbc8532009-07-28 14:44:50 -07001669 list_for_each_entry(c, &dma->channels, device_node) {
1670 chan = to_chan_common(c);
Dan Williamsf2427e22009-07-28 14:42:38 -07001671 writel(IOAT_DMA_DCA_ANY_CPU,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001672 chan->reg_base + IOAT_DCACTRL_OFFSET);
Dan Williamsf2427e22009-07-28 14:42:38 -07001673 }
1674
1675 err = ioat_register(device);
1676 if (err)
1677 return err;
1678 if (dca)
1679 device->dca = ioat3_dca_init(pdev, device->reg_base);
1680
1681 return err;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001682}
1683
Shannon Nelson8ab89562007-10-16 01:27:39 -07001684void ioat_dma_remove(struct ioatdma_device *device)
Dan Aloni428ed602007-03-08 09:57:36 -08001685{
Dan Williamsbc3c7022009-07-28 14:33:42 -07001686 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001687
Maciej Sosnowski2b8a6bf2009-02-26 11:05:07 +01001688 if (device->version != IOAT_VER_3_0)
1689 cancel_delayed_work(&device->work);
1690
Dan Williamse6c0b692009-09-08 17:29:44 -07001691 ioat_disable_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001692
Dan Williamsbc3c7022009-07-28 14:33:42 -07001693 dma_async_device_unregister(dma);
Shannon Nelsondfe22992007-10-18 03:07:13 -07001694
Chris Leech0bbd5f42006-05-23 17:35:34 -07001695 pci_pool_destroy(device->dma_pool);
1696 pci_pool_destroy(device->completion_pool);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001697
Dan Williamsdcbc8532009-07-28 14:44:50 -07001698 INIT_LIST_HEAD(&dma->channels);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001699}
1700