blob: 64b4d75059aa5cdc6d6219da6fb55015af7ae71c [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
Dan Williams5cbafa62009-08-26 13:01:44 -070041int ioat_pending_level = 4;
Shannon Nelson7bb67c12007-11-14 16:59:51 -080042module_param(ioat_pending_level, int, 0644);
43MODULE_PARM_DESC(ioat_pending_level,
44 "high-water mark for pushing ioat descriptors (default: 4)");
45
Chris Leech0bbd5f42006-05-23 17:35:34 -070046/* internal functions */
Dan Williams5cbafa62009-08-26 13:01:44 -070047static void ioat1_cleanup(struct ioat_dma_chan *ioat);
48static void ioat1_dma_start_null_desc(struct ioat_dma_chan *ioat);
Shannon Nelson3e037452007-10-16 01:27:40 -070049
50/**
51 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
52 * @irq: interrupt id
53 * @data: interrupt data
54 */
55static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
56{
57 struct ioatdma_device *instance = data;
Dan Williamsdcbc8532009-07-28 14:44:50 -070058 struct ioat_chan_common *chan;
Shannon Nelson3e037452007-10-16 01:27:40 -070059 unsigned long attnstatus;
60 int bit;
61 u8 intrctrl;
62
63 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
64
65 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
66 return IRQ_NONE;
67
68 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
69 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
70 return IRQ_NONE;
71 }
72
73 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
74 for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
Dan Williamsdcbc8532009-07-28 14:44:50 -070075 chan = ioat_chan_by_index(instance, bit);
76 tasklet_schedule(&chan->cleanup_task);
Shannon Nelson3e037452007-10-16 01:27:40 -070077 }
78
79 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
80 return IRQ_HANDLED;
81}
82
83/**
84 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
85 * @irq: interrupt id
86 * @data: interrupt data
87 */
88static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
89{
Dan Williamsdcbc8532009-07-28 14:44:50 -070090 struct ioat_chan_common *chan = data;
Shannon Nelson3e037452007-10-16 01:27:40 -070091
Dan Williamsdcbc8532009-07-28 14:44:50 -070092 tasklet_schedule(&chan->cleanup_task);
Shannon Nelson3e037452007-10-16 01:27:40 -070093
94 return IRQ_HANDLED;
95}
96
Dan Williams5cbafa62009-08-26 13:01:44 -070097static void ioat1_cleanup_tasklet(unsigned long data);
98
99/* common channel initialization */
100void ioat_init_channel(struct ioatdma_device *device,
101 struct ioat_chan_common *chan, int idx,
102 work_func_t work_fn, void (*tasklet)(unsigned long),
103 unsigned long tasklet_data)
104{
105 struct dma_device *dma = &device->common;
106
107 chan->device = device;
108 chan->reg_base = device->reg_base + (0x80 * (idx + 1));
109 INIT_DELAYED_WORK(&chan->work, work_fn);
110 spin_lock_init(&chan->cleanup_lock);
111 chan->common.device = dma;
112 list_add_tail(&chan->common.device_node, &dma->channels);
113 device->idx[idx] = chan;
114 tasklet_init(&chan->cleanup_task, tasklet, tasklet_data);
115 tasklet_disable(&chan->cleanup_task);
116}
117
118static void ioat1_reset_part2(struct work_struct *work);
Shannon Nelson3e037452007-10-16 01:27:40 -0700119
120/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700121 * ioat1_dma_enumerate_channels - find and initialize the device's channels
Shannon Nelson3e037452007-10-16 01:27:40 -0700122 * @device: the device to be enumerated
123 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700124static int ioat1_enumerate_channels(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700125{
126 u8 xfercap_scale;
127 u32 xfercap;
128 int i;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700129 struct ioat_dma_chan *ioat;
Dan Williamse6c0b692009-09-08 17:29:44 -0700130 struct device *dev = &device->pdev->dev;
Dan Williamsf2427e22009-07-28 14:42:38 -0700131 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700132
Dan Williamsf2427e22009-07-28 14:42:38 -0700133 INIT_LIST_HEAD(&dma->channels);
134 dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
Chris Leeche3828812007-03-08 09:57:35 -0800135 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700136 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
137
Venki Pallipadif371be62008-10-23 15:39:06 -0700138#ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
Dan Williamsf2427e22009-07-28 14:42:38 -0700139 if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
140 dma->chancnt--;
Andy Henroid27471fd2008-10-09 11:45:22 -0700141#endif
Dan Williamsf2427e22009-07-28 14:42:38 -0700142 for (i = 0; i < dma->chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700143 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
Dan Williams5cbafa62009-08-26 13:01:44 -0700144 if (!ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700145 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700146
Dan Williams5cbafa62009-08-26 13:01:44 -0700147 ioat_init_channel(device, &ioat->base, i,
148 ioat1_reset_part2,
149 ioat1_cleanup_tasklet,
150 (unsigned long) ioat);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700151 ioat->xfercap = xfercap;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700152 spin_lock_init(&ioat->desc_lock);
153 INIT_LIST_HEAD(&ioat->free_desc);
154 INIT_LIST_HEAD(&ioat->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700155 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700156 dma->chancnt = i;
157 return i;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700158}
159
Shannon Nelson711924b2007-12-17 16:20:08 -0800160/**
161 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
162 * descriptors to hw
163 * @chan: DMA channel handle
164 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700165static inline void
Dan Williamsdcbc8532009-07-28 14:44:50 -0700166__ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat)
Shannon Nelson711924b2007-12-17 16:20:08 -0800167{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700168 void __iomem *reg_base = ioat->base.reg_base;
169
170 ioat->pending = 0;
171 writeb(IOAT_CHANCMD_APPEND, reg_base + IOAT1_CHANCMD_OFFSET);
Shannon Nelson711924b2007-12-17 16:20:08 -0800172}
173
174static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
175{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700176 struct ioat_dma_chan *ioat = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800177
Dan Williamsdcbc8532009-07-28 14:44:50 -0700178 if (ioat->pending > 0) {
179 spin_lock_bh(&ioat->desc_lock);
180 __ioat1_dma_memcpy_issue_pending(ioat);
181 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800182 }
183}
184
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700185/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700186 * ioat1_reset_part2 - reinit the channel after a reset
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700187 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700188static void ioat1_reset_part2(struct work_struct *work)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700189{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700190 struct ioat_chan_common *chan;
191 struct ioat_dma_chan *ioat;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700192 struct ioat_desc_sw *desc;
Dan Williams5cbafa62009-08-26 13:01:44 -0700193 int dmacount;
194 bool start_null = false;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700195
Dan Williamsdcbc8532009-07-28 14:44:50 -0700196 chan = container_of(work, struct ioat_chan_common, work.work);
197 ioat = container_of(chan, struct ioat_dma_chan, base);
198 spin_lock_bh(&chan->cleanup_lock);
199 spin_lock_bh(&ioat->desc_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700200
Dan Williamsdcbc8532009-07-28 14:44:50 -0700201 chan->completion_virt->low = 0;
202 chan->completion_virt->high = 0;
203 ioat->pending = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700204
Dan Williams5cbafa62009-08-26 13:01:44 -0700205 /* count the descriptors waiting */
206 dmacount = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700207 if (ioat->used_desc.prev) {
208 desc = to_ioat_desc(ioat->used_desc.prev);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700209 do {
Dan Williams5cbafa62009-08-26 13:01:44 -0700210 dmacount++;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700211 desc = to_ioat_desc(desc->node.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700212 } while (&desc->node != ioat->used_desc.next);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700213 }
214
Dan Williams5cbafa62009-08-26 13:01:44 -0700215 if (dmacount) {
216 /*
217 * write the new starting descriptor address
218 * this puts channel engine into ARMED state
219 */
220 desc = to_ioat_desc(ioat->used_desc.prev);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700221 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700222 chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700223 writel(((u64) desc->txd.phys) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700224 chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700225
Dan Williamsdcbc8532009-07-28 14:44:50 -0700226 writeb(IOAT_CHANCMD_START, chan->reg_base
227 + IOAT_CHANCMD_OFFSET(chan->device->version));
Dan Williams5cbafa62009-08-26 13:01:44 -0700228 } else
229 start_null = true;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700230 spin_unlock_bh(&ioat->desc_lock);
231 spin_unlock_bh(&chan->cleanup_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700232
233 dev_err(to_dev(chan),
234 "chan%d reset - %d descs waiting, %d total desc\n",
235 chan_num(chan), dmacount, ioat->desccount);
236
237 if (start_null)
238 ioat1_dma_start_null_desc(ioat);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700239}
240
241/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700242 * ioat1_reset_channel - restart a channel
Dan Williamsdcbc8532009-07-28 14:44:50 -0700243 * @ioat: IOAT DMA channel handle
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700244 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700245static void ioat1_reset_channel(struct ioat_dma_chan *ioat)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700246{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700247 struct ioat_chan_common *chan = &ioat->base;
248 void __iomem *reg_base = chan->reg_base;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700249 u32 chansts, chanerr;
250
Dan Williamsdcbc8532009-07-28 14:44:50 -0700251 if (!ioat->used_desc.prev)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700252 return;
253
Dan Williamsdcbc8532009-07-28 14:44:50 -0700254 chanerr = readl(reg_base + IOAT_CHANERR_OFFSET);
255 chansts = (chan->completion_virt->low
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700256 & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
257 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700258 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700259 "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700260 chan_num(chan), chansts, chanerr);
261 writel(chanerr, reg_base + IOAT_CHANERR_OFFSET);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700262 }
263
264 /*
265 * whack it upside the head with a reset
266 * and wait for things to settle out.
267 * force the pending count to a really big negative
268 * to make sure no one forces an issue_pending
269 * while we're waiting.
270 */
271
Dan Williamsdcbc8532009-07-28 14:44:50 -0700272 spin_lock_bh(&ioat->desc_lock);
273 ioat->pending = INT_MIN;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700274 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700275 reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
276 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700277
278 /* schedule the 2nd half instead of sleeping a long time */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700279 schedule_delayed_work(&chan->work, RESET_DELAY);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700280}
281
282/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700283 * ioat1_chan_watchdog - watch for stuck channels
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700284 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700285static void ioat1_chan_watchdog(struct work_struct *work)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700286{
287 struct ioatdma_device *device =
288 container_of(work, struct ioatdma_device, work.work);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700289 struct ioat_dma_chan *ioat;
290 struct ioat_chan_common *chan;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700291 int i;
292
293 union {
294 u64 full;
295 struct {
296 u32 low;
297 u32 high;
298 };
299 } completion_hw;
300 unsigned long compl_desc_addr_hw;
301
302 for (i = 0; i < device->common.chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700303 chan = ioat_chan_by_index(device, i);
304 ioat = container_of(chan, struct ioat_dma_chan, base);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700305
Dan Williams5cbafa62009-08-26 13:01:44 -0700306 if (/* have we started processing anything yet */
307 chan->last_completion
308 /* have we completed any since last watchdog cycle? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700309 && (chan->last_completion == chan->watchdog_completion)
Dan Williams5cbafa62009-08-26 13:01:44 -0700310 /* has TCP stuck on one cookie since last watchdog? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700311 && (chan->watchdog_tcp_cookie == chan->watchdog_last_tcp_cookie)
312 && (chan->watchdog_tcp_cookie != chan->completed_cookie)
Dan Williams5cbafa62009-08-26 13:01:44 -0700313 /* is there something in the chain to be processed? */
314 /* CB1 chain always has at least the last one processed */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700315 && (ioat->used_desc.prev != ioat->used_desc.next)
316 && ioat->pending == 0) {
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700317
318 /*
319 * check CHANSTS register for completed
320 * descriptor address.
321 * if it is different than completion writeback,
322 * it is not zero
323 * and it has changed since the last watchdog
324 * we can assume that channel
325 * is still working correctly
326 * and the problem is in completion writeback.
327 * update completion writeback
328 * with actual CHANSTS value
329 * else
330 * try resetting the channel
331 */
332
Dan Williamsdcbc8532009-07-28 14:44:50 -0700333 completion_hw.low = readl(chan->reg_base +
334 IOAT_CHANSTS_OFFSET_LOW(chan->device->version));
335 completion_hw.high = readl(chan->reg_base +
336 IOAT_CHANSTS_OFFSET_HIGH(chan->device->version));
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700337#if (BITS_PER_LONG == 64)
338 compl_desc_addr_hw =
339 completion_hw.full
340 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
341#else
342 compl_desc_addr_hw =
343 completion_hw.low & IOAT_LOW_COMPLETION_MASK;
344#endif
345
346 if ((compl_desc_addr_hw != 0)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700347 && (compl_desc_addr_hw != chan->watchdog_completion)
348 && (compl_desc_addr_hw != chan->last_compl_desc_addr_hw)) {
349 chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
350 chan->completion_virt->low = completion_hw.low;
351 chan->completion_virt->high = completion_hw.high;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700352 } else {
Dan Williams5cbafa62009-08-26 13:01:44 -0700353 ioat1_reset_channel(ioat);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700354 chan->watchdog_completion = 0;
355 chan->last_compl_desc_addr_hw = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700356 }
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700357 } else {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700358 chan->last_compl_desc_addr_hw = 0;
359 chan->watchdog_completion = chan->last_completion;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700360 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700361
Dan Williamsdcbc8532009-07-28 14:44:50 -0700362 chan->watchdog_last_tcp_cookie = chan->watchdog_tcp_cookie;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700363 }
364
365 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
366}
367
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800368static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700369{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700370 struct dma_chan *c = tx->chan;
371 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700372 struct ioat_desc_sw *desc = tx_to_ioat_desc(tx);
373 struct ioat_desc_sw *first;
374 struct ioat_desc_sw *chain_tail;
Dan Williams7405f742007-01-02 11:10:43 -0700375 dma_cookie_t cookie;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700376
Dan Williamsdcbc8532009-07-28 14:44:50 -0700377 spin_lock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700378 /* cookie incr and addition to used_list must be atomic */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700379 cookie = c->cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700380 cookie++;
381 if (cookie < 0)
382 cookie = 1;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700383 c->cookie = cookie;
384 tx->cookie = cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700385
386 /* write address into NextDescriptor field of last desc in chain */
Dan Williamsa0587bc2009-07-28 14:44:04 -0700387 first = to_ioat_desc(tx->tx_list.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700388 chain_tail = to_ioat_desc(ioat->used_desc.prev);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700389 /* make descriptor updates globally visible before chaining */
390 wmb();
391 chain_tail->hw->next = first->txd.phys;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700392 list_splice_tail_init(&tx->tx_list, &ioat->used_desc);
Dan Williams7405f742007-01-02 11:10:43 -0700393
Dan Williamsdcbc8532009-07-28 14:44:50 -0700394 ioat->pending += desc->tx_cnt;
395 if (ioat->pending >= ioat_pending_level)
396 __ioat1_dma_memcpy_issue_pending(ioat);
397 spin_unlock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700398
Dan Williams7405f742007-01-02 11:10:43 -0700399 return cookie;
400}
401
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800402/**
403 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
Dan Williamsdcbc8532009-07-28 14:44:50 -0700404 * @ioat: the channel supplying the memory pool for the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800405 * @flags: allocation flags
406 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700407static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700408ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat, gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700409{
410 struct ioat_dma_descriptor *desc;
411 struct ioat_desc_sw *desc_sw;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700412 struct ioatdma_device *ioatdma_device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700413 dma_addr_t phys;
414
Dan Williamsdcbc8532009-07-28 14:44:50 -0700415 ioatdma_device = ioat->base.device;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700416 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700417 if (unlikely(!desc))
418 return NULL;
419
420 desc_sw = kzalloc(sizeof(*desc_sw), flags);
421 if (unlikely(!desc_sw)) {
Shannon Nelson8ab89562007-10-16 01:27:39 -0700422 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700423 return NULL;
424 }
425
426 memset(desc, 0, sizeof(*desc));
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800427
Dan Williams5cbafa62009-08-26 13:01:44 -0700428 dma_async_tx_descriptor_init(&desc_sw->txd, &ioat->base.common);
429 desc_sw->txd.tx_submit = ioat1_tx_submit;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700430 desc_sw->hw = desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700431 desc_sw->txd.phys = phys;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700432
433 return desc_sw;
434}
435
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800436static int ioat_initial_desc_count = 256;
437module_param(ioat_initial_desc_count, int, 0644);
438MODULE_PARM_DESC(ioat_initial_desc_count,
Dan Williams5cbafa62009-08-26 13:01:44 -0700439 "ioat1: initial descriptors per channel (default: 256)");
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800440/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700441 * ioat1_dma_alloc_chan_resources - returns the number of allocated descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800442 * @chan: the channel to be filled out
443 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700444static int ioat1_dma_alloc_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700445{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700446 struct ioat_dma_chan *ioat = to_ioat_chan(c);
447 struct ioat_chan_common *chan = &ioat->base;
Shannon Nelson711924b2007-12-17 16:20:08 -0800448 struct ioat_desc_sw *desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700449 u16 chanctrl;
450 u32 chanerr;
451 int i;
452 LIST_HEAD(tmp_list);
453
Shannon Nelsone4223972007-08-24 23:02:53 -0700454 /* have we already been set up? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700455 if (!list_empty(&ioat->free_desc))
456 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700457
Shannon Nelson43d6e362007-10-16 01:27:39 -0700458 /* Setup register to interrupt and write completion status on error */
Shannon Nelsone4223972007-08-24 23:02:53 -0700459 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
Chris Leech0bbd5f42006-05-23 17:35:34 -0700460 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
461 IOAT_CHANCTRL_ERR_COMPLETION_EN;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700462 writew(chanctrl, chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700463
Dan Williamsdcbc8532009-07-28 14:44:50 -0700464 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700465 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700466 dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
467 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700468 }
469
470 /* Allocate descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800471 for (i = 0; i < ioat_initial_desc_count; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700472 desc = ioat_dma_alloc_descriptor(ioat, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700473 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700474 dev_err(to_dev(chan), "Only %d initial descriptors\n", i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700475 break;
476 }
477 list_add_tail(&desc->node, &tmp_list);
478 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700479 spin_lock_bh(&ioat->desc_lock);
480 ioat->desccount = i;
481 list_splice(&tmp_list, &ioat->free_desc);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700482 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700483
484 /* allocate a completion writeback area */
485 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700486 chan->completion_virt = pci_pool_alloc(chan->device->completion_pool,
487 GFP_KERNEL,
488 &chan->completion_addr);
489 memset(chan->completion_virt, 0,
490 sizeof(*chan->completion_virt));
491 writel(((u64) chan->completion_addr) & 0x00000000FFFFFFFF,
492 chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
493 writel(((u64) chan->completion_addr) >> 32,
494 chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700495
Dan Williamsdcbc8532009-07-28 14:44:50 -0700496 tasklet_enable(&chan->cleanup_task);
Dan Williams5cbafa62009-08-26 13:01:44 -0700497 ioat1_dma_start_null_desc(ioat); /* give chain to dma device */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700498 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700499}
500
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800501/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700502 * ioat1_dma_free_chan_resources - release all the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800503 * @chan: the channel to be cleaned
504 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700505static void ioat1_dma_free_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700506{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700507 struct ioat_dma_chan *ioat = to_ioat_chan(c);
508 struct ioat_chan_common *chan = &ioat->base;
509 struct ioatdma_device *ioatdma_device = chan->device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700510 struct ioat_desc_sw *desc, *_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700511 int in_use_descs = 0;
512
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000513 /* Before freeing channel resources first check
514 * if they have been previously allocated for this channel.
515 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700516 if (ioat->desccount == 0)
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000517 return;
518
Dan Williamsdcbc8532009-07-28 14:44:50 -0700519 tasklet_disable(&chan->cleanup_task);
Dan Williams5cbafa62009-08-26 13:01:44 -0700520 ioat1_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700521
Shannon Nelson3e037452007-10-16 01:27:40 -0700522 /* Delay 100ms after reset to allow internal DMA logic to quiesce
523 * before removing DMA descriptor resources.
524 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800525 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700526 chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
Shannon Nelson3e037452007-10-16 01:27:40 -0700527 mdelay(100);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700528
Dan Williamsdcbc8532009-07-28 14:44:50 -0700529 spin_lock_bh(&ioat->desc_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700530 list_for_each_entry_safe(desc, _desc,
531 &ioat->used_desc, node) {
532 in_use_descs++;
533 list_del(&desc->node);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700534 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700535 desc->txd.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700536 kfree(desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700537 }
538 list_for_each_entry_safe(desc, _desc,
539 &ioat->free_desc, node) {
540 list_del(&desc->node);
541 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
542 desc->txd.phys);
543 kfree(desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700544 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700545 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700546
Shannon Nelson8ab89562007-10-16 01:27:39 -0700547 pci_pool_free(ioatdma_device->completion_pool,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700548 chan->completion_virt,
549 chan->completion_addr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700550
551 /* one is ok since we left it on there on purpose */
552 if (in_use_descs > 1)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700553 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700554 in_use_descs - 1);
555
Dan Williamsdcbc8532009-07-28 14:44:50 -0700556 chan->last_completion = chan->completion_addr = 0;
557 chan->watchdog_completion = 0;
558 chan->last_compl_desc_addr_hw = 0;
559 chan->watchdog_tcp_cookie = chan->watchdog_last_tcp_cookie = 0;
560 ioat->pending = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700561 ioat->desccount = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700562}
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700563
Shannon Nelson3e037452007-10-16 01:27:40 -0700564/**
Dan Williamsdcbc8532009-07-28 14:44:50 -0700565 * ioat1_dma_get_next_descriptor - return the next available descriptor
566 * @ioat: IOAT DMA channel handle
Shannon Nelson3e037452007-10-16 01:27:40 -0700567 *
568 * Gets the next descriptor from the chain, and must be called with the
569 * channel's desc_lock held. Allocates more descriptors if the channel
570 * has run out.
571 */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700572static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700573ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat)
Shannon Nelson3e037452007-10-16 01:27:40 -0700574{
Shannon Nelson711924b2007-12-17 16:20:08 -0800575 struct ioat_desc_sw *new;
Shannon Nelson3e037452007-10-16 01:27:40 -0700576
Dan Williamsdcbc8532009-07-28 14:44:50 -0700577 if (!list_empty(&ioat->free_desc)) {
578 new = to_ioat_desc(ioat->free_desc.next);
Shannon Nelson3e037452007-10-16 01:27:40 -0700579 list_del(&new->node);
580 } else {
581 /* try to get another desc */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700582 new = ioat_dma_alloc_descriptor(ioat, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800583 if (!new) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700584 dev_err(to_dev(&ioat->base), "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800585 return NULL;
586 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700587 }
588
589 prefetch(new->hw);
590 return new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700591}
592
Dan Williamsbc3c7022009-07-28 14:33:42 -0700593static struct dma_async_tx_descriptor *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700594ioat1_dma_prep_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700595 dma_addr_t dma_src, size_t len, unsigned long flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700596{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700597 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700598 struct ioat_desc_sw *desc;
599 size_t copy;
600 LIST_HEAD(chain);
601 dma_addr_t src = dma_src;
602 dma_addr_t dest = dma_dest;
603 size_t total_len = len;
604 struct ioat_dma_descriptor *hw = NULL;
605 int tx_cnt = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700606
Dan Williamsdcbc8532009-07-28 14:44:50 -0700607 spin_lock_bh(&ioat->desc_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700608 desc = ioat1_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700609 do {
610 if (!desc)
611 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700612
Dan Williamsa0587bc2009-07-28 14:44:04 -0700613 tx_cnt++;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700614 copy = min_t(size_t, len, ioat->xfercap);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700615
616 hw = desc->hw;
617 hw->size = copy;
618 hw->ctl = 0;
619 hw->src_addr = src;
620 hw->dst_addr = dest;
621
622 list_add_tail(&desc->node, &chain);
623
624 len -= copy;
625 dest += copy;
626 src += copy;
627 if (len) {
628 struct ioat_desc_sw *next;
629
630 async_tx_ack(&desc->txd);
Dan Williams5cbafa62009-08-26 13:01:44 -0700631 next = ioat1_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700632 hw->next = next ? next->txd.phys : 0;
633 desc = next;
634 } else
635 hw->next = 0;
636 } while (len);
637
638 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700639 struct ioat_chan_common *chan = &ioat->base;
640
641 dev_err(to_dev(chan),
Dan Williams5cbafa62009-08-26 13:01:44 -0700642 "chan%d - get_next_desc failed\n", chan_num(chan));
Dan Williamsdcbc8532009-07-28 14:44:50 -0700643 list_splice(&chain, &ioat->free_desc);
644 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800645 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700646 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700647 spin_unlock_bh(&ioat->desc_lock);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700648
649 desc->txd.flags = flags;
650 desc->tx_cnt = tx_cnt;
651 desc->src = dma_src;
652 desc->dst = dma_dest;
653 desc->len = total_len;
654 list_splice(&chain, &desc->txd.tx_list);
655 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
656 hw->ctl_f.compl_write = 1;
657
658 return &desc->txd;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700659}
660
Dan Williams5cbafa62009-08-26 13:01:44 -0700661static void ioat1_cleanup_tasklet(unsigned long data)
Shannon Nelson3e037452007-10-16 01:27:40 -0700662{
663 struct ioat_dma_chan *chan = (void *)data;
Dan Williams5cbafa62009-08-26 13:01:44 -0700664 ioat1_cleanup(chan);
Shannon Nelson3e037452007-10-16 01:27:40 -0700665 writew(IOAT_CHANCTRL_INT_DISABLE,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700666 chan->base.reg_base + IOAT_CHANCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -0700667}
668
Dan Williams5cbafa62009-08-26 13:01:44 -0700669static void ioat_unmap(struct pci_dev *pdev, dma_addr_t addr, size_t len,
670 int direction, enum dma_ctrl_flags flags, bool dst)
Dan Williamse1d181e2008-07-04 00:13:40 -0700671{
Dan Williams5cbafa62009-08-26 13:01:44 -0700672 if ((dst && (flags & DMA_COMPL_DEST_UNMAP_SINGLE)) ||
673 (!dst && (flags & DMA_COMPL_SRC_UNMAP_SINGLE)))
674 pci_unmap_single(pdev, addr, len, direction);
675 else
676 pci_unmap_page(pdev, addr, len, direction);
Dan Williamse1d181e2008-07-04 00:13:40 -0700677}
678
Dan Williams5cbafa62009-08-26 13:01:44 -0700679
680void ioat_dma_unmap(struct ioat_chan_common *chan, enum dma_ctrl_flags flags,
681 size_t len, struct ioat_dma_descriptor *hw)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700682{
Dan Williams5cbafa62009-08-26 13:01:44 -0700683 struct pci_dev *pdev = chan->device->pdev;
684 size_t offset = len - hw->size;
685
686 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
687 ioat_unmap(pdev, hw->dst_addr - offset, len,
688 PCI_DMA_FROMDEVICE, flags, 1);
689
690 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP))
691 ioat_unmap(pdev, hw->src_addr - offset, len,
692 PCI_DMA_TODEVICE, flags, 0);
693}
694
695unsigned long ioat_get_current_completion(struct ioat_chan_common *chan)
696{
Chris Leech0bbd5f42006-05-23 17:35:34 -0700697 unsigned long phys_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700698
699 /* The completion writeback can happen at any time,
700 so reads by the driver need to be atomic operations
701 The descriptor physical addresses are limited to 32-bits
702 when the CPU can only do a 32-bit mov */
703
704#if (BITS_PER_LONG == 64)
705 phys_complete =
Dan Williamsdcbc8532009-07-28 14:44:50 -0700706 chan->completion_virt->full
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700707 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700708#else
Dan Williamsdcbc8532009-07-28 14:44:50 -0700709 phys_complete = chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700710#endif
711
Dan Williamsdcbc8532009-07-28 14:44:50 -0700712 if ((chan->completion_virt->full
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700713 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
Shannon Nelson43d6e362007-10-16 01:27:39 -0700714 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700715 dev_err(to_dev(chan), "Channel halted, chanerr = %x\n",
716 readl(chan->reg_base + IOAT_CHANERR_OFFSET));
Chris Leech0bbd5f42006-05-23 17:35:34 -0700717
718 /* TODO do something to salvage the situation */
719 }
720
Dan Williams5cbafa62009-08-26 13:01:44 -0700721 return phys_complete;
722}
723
724/**
725 * ioat1_cleanup - cleanup up finished descriptors
726 * @chan: ioat channel to be cleaned up
727 */
728static void ioat1_cleanup(struct ioat_dma_chan *ioat)
729{
730 struct ioat_chan_common *chan = &ioat->base;
731 unsigned long phys_complete;
732 struct ioat_desc_sw *desc, *_desc;
733 dma_cookie_t cookie = 0;
734 struct dma_async_tx_descriptor *tx;
735
736 prefetch(chan->completion_virt);
737
738 if (!spin_trylock_bh(&chan->cleanup_lock))
739 return;
740
741 phys_complete = ioat_get_current_completion(chan);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700742 if (phys_complete == chan->last_completion) {
743 spin_unlock_bh(&chan->cleanup_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700744 /*
745 * perhaps we're stuck so hard that the watchdog can't go off?
746 * try to catch it after 2 seconds
747 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700748 if (time_after(jiffies,
749 chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
750 ioat1_chan_watchdog(&(chan->device->work.work));
751 chan->last_completion_time = jiffies;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700752 }
753 return;
754 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700755 chan->last_completion_time = jiffies;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700756
757 cookie = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700758 if (!spin_trylock_bh(&ioat->desc_lock)) {
759 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700760 return;
761 }
762
Dan Williams5cbafa62009-08-26 13:01:44 -0700763 list_for_each_entry_safe(desc, _desc, &ioat->used_desc, node) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700764 tx = &desc->txd;
Dan Williams5cbafa62009-08-26 13:01:44 -0700765 /*
766 * Incoming DMA requests may use multiple descriptors,
767 * due to exceeding xfercap, perhaps. If so, only the
768 * last one will have a cookie, and require unmapping.
769 */
770 if (tx->cookie) {
771 cookie = tx->cookie;
772 ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
773 if (tx->callback) {
774 tx->callback(tx->callback_param);
775 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800776 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700777 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700778
779 if (tx->phys != phys_complete) {
780 /*
781 * a completed entry, but not the last, so clean
782 * up if the client is done with the descriptor
783 */
784 if (async_tx_test_ack(tx))
785 list_move_tail(&desc->node, &ioat->free_desc);
786 else
787 tx->cookie = 0;
788 } else {
789 /*
790 * last used desc. Do not remove, so we can
791 * append from it, but don't look at it next
792 * time, either
793 */
794 tx->cookie = 0;
795
796 /* TODO check status bits? */
797 break;
798 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700799 }
800
Dan Williamsdcbc8532009-07-28 14:44:50 -0700801 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700802
Dan Williamsdcbc8532009-07-28 14:44:50 -0700803 chan->last_completion = phys_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700804 if (cookie != 0)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700805 chan->completed_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700806
Dan Williamsdcbc8532009-07-28 14:44:50 -0700807 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700808}
809
Dan Williamsbc3c7022009-07-28 14:33:42 -0700810static enum dma_status
Dan Williams5cbafa62009-08-26 13:01:44 -0700811ioat1_dma_is_complete(struct dma_chan *c, dma_cookie_t cookie,
812 dma_cookie_t *done, dma_cookie_t *used)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700813{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700814 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700815
Dan Williams5cbafa62009-08-26 13:01:44 -0700816 if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
817 return DMA_SUCCESS;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700818
Dan Williams5cbafa62009-08-26 13:01:44 -0700819 ioat1_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700820
Dan Williams5cbafa62009-08-26 13:01:44 -0700821 return ioat_is_complete(c, cookie, done, used);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700822}
823
Dan Williams5cbafa62009-08-26 13:01:44 -0700824static void ioat1_dma_start_null_desc(struct ioat_dma_chan *ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700825{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700826 struct ioat_chan_common *chan = &ioat->base;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700827 struct ioat_desc_sw *desc;
Dan Williamsc7984f42009-07-28 14:44:04 -0700828 struct ioat_dma_descriptor *hw;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700829
Dan Williamsdcbc8532009-07-28 14:44:50 -0700830 spin_lock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700831
Dan Williams5cbafa62009-08-26 13:01:44 -0700832 desc = ioat1_dma_get_next_descriptor(ioat);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700833
834 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700835 dev_err(to_dev(chan),
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700836 "Unable to start null desc - get next desc failed\n");
Dan Williamsdcbc8532009-07-28 14:44:50 -0700837 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700838 return;
839 }
840
Dan Williamsc7984f42009-07-28 14:44:04 -0700841 hw = desc->hw;
842 hw->ctl = 0;
843 hw->ctl_f.null = 1;
844 hw->ctl_f.int_en = 1;
845 hw->ctl_f.compl_write = 1;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700846 /* set size to non-zero value (channel returns error when size is 0) */
Dan Williamsc7984f42009-07-28 14:44:04 -0700847 hw->size = NULL_DESC_BUFFER_SIZE;
848 hw->src_addr = 0;
849 hw->dst_addr = 0;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700850 async_tx_ack(&desc->txd);
Dan Williams5cbafa62009-08-26 13:01:44 -0700851 hw->next = 0;
852 list_add_tail(&desc->node, &ioat->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700853
Dan Williams5cbafa62009-08-26 13:01:44 -0700854 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
855 chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
856 writel(((u64) desc->txd.phys) >> 32,
857 chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800858
Dan Williams5cbafa62009-08-26 13:01:44 -0700859 writeb(IOAT_CHANCMD_START, chan->reg_base
860 + IOAT_CHANCMD_OFFSET(chan->device->version));
Dan Williamsdcbc8532009-07-28 14:44:50 -0700861 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700862}
863
864/*
865 * Perform a IOAT transaction to verify the HW works.
866 */
867#define IOAT_TEST_SIZE 2000
868
Shannon Nelson95218432007-10-18 03:07:15 -0700869static void ioat_dma_test_callback(void *dma_async_param)
870{
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700871 struct completion *cmp = dma_async_param;
872
873 complete(cmp);
Shannon Nelson95218432007-10-18 03:07:15 -0700874}
875
Shannon Nelson3e037452007-10-16 01:27:40 -0700876/**
877 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
878 * @device: device to be tested
879 */
880static int ioat_dma_self_test(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700881{
882 int i;
883 u8 *src;
884 u8 *dest;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700885 struct dma_device *dma = &device->common;
886 struct device *dev = &device->pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700887 struct dma_chan *dma_chan;
Shannon Nelson711924b2007-12-17 16:20:08 -0800888 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700889 dma_addr_t dma_dest, dma_src;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700890 dma_cookie_t cookie;
891 int err = 0;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700892 struct completion cmp;
Dan Williams0c33e1c2009-03-02 13:31:35 -0700893 unsigned long tmo;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200894 unsigned long flags;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700895
Christoph Lametere94b1762006-12-06 20:33:17 -0800896 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700897 if (!src)
898 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -0800899 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700900 if (!dest) {
901 kfree(src);
902 return -ENOMEM;
903 }
904
905 /* Fill in src buffer */
906 for (i = 0; i < IOAT_TEST_SIZE; i++)
907 src[i] = (u8)i;
908
909 /* Start copy, using first DMA channel */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700910 dma_chan = container_of(dma->channels.next, struct dma_chan,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700911 device_node);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700912 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
913 dev_err(dev, "selftest cannot allocate chan resource\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700914 err = -ENODEV;
915 goto out;
916 }
917
Dan Williamsbc3c7022009-07-28 14:33:42 -0700918 dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
919 dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
Dan Williamsa6a39ca2009-07-28 14:44:05 -0700920 flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE |
921 DMA_PREP_INTERRUPT;
Dan Williams00367312008-02-02 19:49:57 -0700922 tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200923 IOAT_TEST_SIZE, flags);
Shannon Nelson5149fd02007-10-18 03:07:13 -0700924 if (!tx) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700925 dev_err(dev, "Self-test prep failed, disabling\n");
Shannon Nelson5149fd02007-10-18 03:07:13 -0700926 err = -ENODEV;
927 goto free_resources;
928 }
929
Dan Williams7405f742007-01-02 11:10:43 -0700930 async_tx_ack(tx);
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700931 init_completion(&cmp);
Shannon Nelson95218432007-10-18 03:07:15 -0700932 tx->callback = ioat_dma_test_callback;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700933 tx->callback_param = &cmp;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800934 cookie = tx->tx_submit(tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700935 if (cookie < 0) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700936 dev_err(dev, "Self-test setup failed, disabling\n");
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700937 err = -ENODEV;
938 goto free_resources;
939 }
Dan Williamsbc3c7022009-07-28 14:33:42 -0700940 dma->device_issue_pending(dma_chan);
Dan Williams532d3b12008-12-03 17:16:55 -0700941
Dan Williams0c33e1c2009-03-02 13:31:35 -0700942 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
Chris Leech0bbd5f42006-05-23 17:35:34 -0700943
Dan Williams0c33e1c2009-03-02 13:31:35 -0700944 if (tmo == 0 ||
Dan Williamsbc3c7022009-07-28 14:33:42 -0700945 dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800946 != DMA_SUCCESS) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700947 dev_err(dev, "Self-test copy timed out, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700948 err = -ENODEV;
949 goto free_resources;
950 }
951 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700952 dev_err(dev, "Self-test copy failed compare, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700953 err = -ENODEV;
954 goto free_resources;
955 }
956
957free_resources:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700958 dma->device_free_chan_resources(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700959out:
960 kfree(src);
961 kfree(dest);
962 return err;
963}
964
Shannon Nelson3e037452007-10-16 01:27:40 -0700965static char ioat_interrupt_style[32] = "msix";
966module_param_string(ioat_interrupt_style, ioat_interrupt_style,
967 sizeof(ioat_interrupt_style), 0644);
968MODULE_PARM_DESC(ioat_interrupt_style,
969 "set ioat interrupt style: msix (default), "
970 "msix-single-vector, msi, intx)");
971
972/**
973 * ioat_dma_setup_interrupts - setup interrupt handler
974 * @device: ioat device
975 */
976static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
977{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700978 struct ioat_chan_common *chan;
Dan Williamse6c0b692009-09-08 17:29:44 -0700979 struct pci_dev *pdev = device->pdev;
980 struct device *dev = &pdev->dev;
981 struct msix_entry *msix;
982 int i, j, msixcnt;
983 int err = -EINVAL;
Shannon Nelson3e037452007-10-16 01:27:40 -0700984 u8 intrctrl = 0;
985
986 if (!strcmp(ioat_interrupt_style, "msix"))
987 goto msix;
988 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
989 goto msix_single_vector;
990 if (!strcmp(ioat_interrupt_style, "msi"))
991 goto msi;
992 if (!strcmp(ioat_interrupt_style, "intx"))
993 goto intx;
Dan Williamse6c0b692009-09-08 17:29:44 -0700994 dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
Shannon Nelson5149fd02007-10-18 03:07:13 -0700995 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -0700996
997msix:
998 /* The number of MSI-X vectors should equal the number of channels */
999 msixcnt = device->common.chancnt;
1000 for (i = 0; i < msixcnt; i++)
1001 device->msix_entries[i].entry = i;
1002
Dan Williamse6c0b692009-09-08 17:29:44 -07001003 err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
Shannon Nelson3e037452007-10-16 01:27:40 -07001004 if (err < 0)
1005 goto msi;
1006 if (err > 0)
1007 goto msix_single_vector;
1008
1009 for (i = 0; i < msixcnt; i++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001010 msix = &device->msix_entries[i];
Dan Williamsdcbc8532009-07-28 14:44:50 -07001011 chan = ioat_chan_by_index(device, i);
Dan Williamse6c0b692009-09-08 17:29:44 -07001012 err = devm_request_irq(dev, msix->vector,
1013 ioat_dma_do_interrupt_msix, 0,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001014 "ioat-msix", chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001015 if (err) {
1016 for (j = 0; j < i; j++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001017 msix = &device->msix_entries[j];
Dan Williamsdcbc8532009-07-28 14:44:50 -07001018 chan = ioat_chan_by_index(device, j);
1019 devm_free_irq(dev, msix->vector, chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001020 }
1021 goto msix_single_vector;
1022 }
1023 }
1024 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
Shannon Nelson3e037452007-10-16 01:27:40 -07001025 goto done;
1026
1027msix_single_vector:
Dan Williamse6c0b692009-09-08 17:29:44 -07001028 msix = &device->msix_entries[0];
1029 msix->entry = 0;
1030 err = pci_enable_msix(pdev, device->msix_entries, 1);
Shannon Nelson3e037452007-10-16 01:27:40 -07001031 if (err)
1032 goto msi;
1033
Dan Williamse6c0b692009-09-08 17:29:44 -07001034 err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
1035 "ioat-msix", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001036 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001037 pci_disable_msix(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001038 goto msi;
1039 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001040 goto done;
1041
1042msi:
Dan Williamse6c0b692009-09-08 17:29:44 -07001043 err = pci_enable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001044 if (err)
1045 goto intx;
1046
Dan Williamse6c0b692009-09-08 17:29:44 -07001047 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
1048 "ioat-msi", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001049 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001050 pci_disable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001051 goto intx;
1052 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001053 goto done;
1054
1055intx:
Dan Williamse6c0b692009-09-08 17:29:44 -07001056 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
1057 IRQF_SHARED, "ioat-intx", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001058 if (err)
1059 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001060
1061done:
Dan Williamsf2427e22009-07-28 14:42:38 -07001062 if (device->intr_quirk)
1063 device->intr_quirk(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001064 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
1065 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
1066 return 0;
1067
1068err_no_irq:
1069 /* Disable all interrupt generation */
1070 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Dan Williamse6c0b692009-09-08 17:29:44 -07001071 dev_err(dev, "no usable interrupts\n");
1072 return err;
Shannon Nelson3e037452007-10-16 01:27:40 -07001073}
1074
Dan Williamse6c0b692009-09-08 17:29:44 -07001075static void ioat_disable_interrupts(struct ioatdma_device *device)
Shannon Nelson3e037452007-10-16 01:27:40 -07001076{
Shannon Nelson3e037452007-10-16 01:27:40 -07001077 /* Disable all interrupt generation */
1078 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -07001079}
1080
Dan Williams5cbafa62009-08-26 13:01:44 -07001081int ioat_probe(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001082{
Dan Williamsf2427e22009-07-28 14:42:38 -07001083 int err = -ENODEV;
1084 struct dma_device *dma = &device->common;
1085 struct pci_dev *pdev = device->pdev;
Dan Williamse6c0b692009-09-08 17:29:44 -07001086 struct device *dev = &pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001087
1088 /* DMA coherent memory pool for DMA descriptor allocations */
1089 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
Shannon Nelson8ab89562007-10-16 01:27:39 -07001090 sizeof(struct ioat_dma_descriptor),
1091 64, 0);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001092 if (!device->dma_pool) {
1093 err = -ENOMEM;
1094 goto err_dma_pool;
1095 }
1096
Shannon Nelson43d6e362007-10-16 01:27:39 -07001097 device->completion_pool = pci_pool_create("completion_pool", pdev,
1098 sizeof(u64), SMP_CACHE_BYTES,
1099 SMP_CACHE_BYTES);
Dan Williams5cbafa62009-08-26 13:01:44 -07001100
Chris Leech0bbd5f42006-05-23 17:35:34 -07001101 if (!device->completion_pool) {
1102 err = -ENOMEM;
1103 goto err_completion_pool;
1104 }
1105
Dan Williams5cbafa62009-08-26 13:01:44 -07001106 device->enumerate_channels(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001107
Dan Williamsf2427e22009-07-28 14:42:38 -07001108 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
Dan Williamsf2427e22009-07-28 14:42:38 -07001109 dma->dev = &pdev->dev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001110
Dan Williamse6c0b692009-09-08 17:29:44 -07001111 dev_err(dev, "Intel(R) I/OAT DMA Engine found,"
Shannon Nelson5149fd02007-10-18 03:07:13 -07001112 " %d channels, device version 0x%02x, driver version %s\n",
Dan Williamsbc3c7022009-07-28 14:33:42 -07001113 dma->chancnt, device->version, IOAT_DMA_VERSION);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001114
Dan Williamsbc3c7022009-07-28 14:33:42 -07001115 if (!dma->chancnt) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001116 dev_err(dev, "Intel(R) I/OAT DMA Engine problem found: "
Maciej Sosnowski8b794b12009-02-26 11:04:54 +01001117 "zero channels detected\n");
1118 goto err_setup_interrupts;
1119 }
1120
Shannon Nelson3e037452007-10-16 01:27:40 -07001121 err = ioat_dma_setup_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001122 if (err)
Shannon Nelson3e037452007-10-16 01:27:40 -07001123 goto err_setup_interrupts;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001124
Shannon Nelson3e037452007-10-16 01:27:40 -07001125 err = ioat_dma_self_test(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001126 if (err)
1127 goto err_self_test;
1128
Dan Williamsf2427e22009-07-28 14:42:38 -07001129 return 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001130
1131err_self_test:
Dan Williamse6c0b692009-09-08 17:29:44 -07001132 ioat_disable_interrupts(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001133err_setup_interrupts:
Chris Leech0bbd5f42006-05-23 17:35:34 -07001134 pci_pool_destroy(device->completion_pool);
1135err_completion_pool:
1136 pci_pool_destroy(device->dma_pool);
1137err_dma_pool:
Dan Williamsf2427e22009-07-28 14:42:38 -07001138 return err;
1139}
1140
Dan Williams5cbafa62009-08-26 13:01:44 -07001141int ioat_register(struct ioatdma_device *device)
Dan Williamsf2427e22009-07-28 14:42:38 -07001142{
1143 int err = dma_async_device_register(&device->common);
1144
1145 if (err) {
1146 ioat_disable_interrupts(device);
1147 pci_pool_destroy(device->completion_pool);
1148 pci_pool_destroy(device->dma_pool);
1149 }
1150
1151 return err;
1152}
1153
1154/* ioat1_intr_quirk - fix up dma ctrl register to enable / disable msi */
1155static void ioat1_intr_quirk(struct ioatdma_device *device)
1156{
1157 struct pci_dev *pdev = device->pdev;
1158 u32 dmactrl;
1159
1160 pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
1161 if (pdev->msi_enabled)
1162 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
1163 else
1164 dmactrl &= ~IOAT_PCI_DMACTRL_MSI_EN;
1165 pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
1166}
1167
1168int ioat1_dma_probe(struct ioatdma_device *device, int dca)
1169{
1170 struct pci_dev *pdev = device->pdev;
1171 struct dma_device *dma;
1172 int err;
1173
1174 device->intr_quirk = ioat1_intr_quirk;
Dan Williams5cbafa62009-08-26 13:01:44 -07001175 device->enumerate_channels = ioat1_enumerate_channels;
Dan Williamsf2427e22009-07-28 14:42:38 -07001176 dma = &device->common;
1177 dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1178 dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
Dan Williams5cbafa62009-08-26 13:01:44 -07001179 dma->device_alloc_chan_resources = ioat1_dma_alloc_chan_resources;
1180 dma->device_free_chan_resources = ioat1_dma_free_chan_resources;
1181 dma->device_is_tx_complete = ioat1_dma_is_complete;
Dan Williamsf2427e22009-07-28 14:42:38 -07001182
1183 err = ioat_probe(device);
1184 if (err)
1185 return err;
1186 ioat_set_tcp_copy_break(4096);
1187 err = ioat_register(device);
1188 if (err)
1189 return err;
1190 if (dca)
1191 device->dca = ioat_dca_init(pdev, device->reg_base);
1192
Dan Williams5cbafa62009-08-26 13:01:44 -07001193 INIT_DELAYED_WORK(&device->work, ioat1_chan_watchdog);
Dan Williamsf2427e22009-07-28 14:42:38 -07001194 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
1195
1196 return err;
1197}
1198
Shannon Nelson8ab89562007-10-16 01:27:39 -07001199void ioat_dma_remove(struct ioatdma_device *device)
Dan Aloni428ed602007-03-08 09:57:36 -08001200{
Dan Williamsbc3c7022009-07-28 14:33:42 -07001201 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001202
Maciej Sosnowski2b8a6bf2009-02-26 11:05:07 +01001203 if (device->version != IOAT_VER_3_0)
1204 cancel_delayed_work(&device->work);
1205
Dan Williamse6c0b692009-09-08 17:29:44 -07001206 ioat_disable_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001207
Dan Williamsbc3c7022009-07-28 14:33:42 -07001208 dma_async_device_unregister(dma);
Shannon Nelsondfe22992007-10-18 03:07:13 -07001209
Chris Leech0bbd5f42006-05-23 17:35:34 -07001210 pci_pool_destroy(device->dma_pool);
1211 pci_pool_destroy(device->completion_pool);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001212
Dan Williamsdcbc8532009-07-28 14:44:50 -07001213 INIT_LIST_HEAD(&dma->channels);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001214}