blob: 462dae627191c0e514263cbd5df020c3bfa84a85 [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 */
Shannon Nelson43d6e362007-10-16 01:27:39 -070050static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
51static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -080052
Shannon Nelson7f2b2912007-10-18 03:07:14 -070053static struct ioat_desc_sw *
Shannon Nelson7bb67c12007-11-14 16:59:51 -080054ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
55static struct ioat_desc_sw *
56ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -070057
Dan Williamsbc3c7022009-07-28 14:33:42 -070058static inline struct ioat_dma_chan *
59ioat_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;
72 struct ioat_dma_chan *ioat_chan;
73 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 Williamsbc3c7022009-07-28 14:33:42 -070089 ioat_chan = ioat_chan_by_index(instance, bit);
Shannon Nelson3e037452007-10-16 01:27:40 -070090 tasklet_schedule(&ioat_chan->cleanup_task);
91 }
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{
104 struct ioat_dma_chan *ioat_chan = data;
105
106 tasklet_schedule(&ioat_chan->cleanup_task);
107
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;
122 struct ioat_dma_chan *ioat_chan;
Dan Williamse6c0b692009-09-08 17:29:44 -0700123 struct device *dev = &device->pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700124
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700125 /*
126 * IOAT ver.3 workarounds
127 */
128 if (device->version == IOAT_VER_3_0) {
129 u32 chan_err_mask;
130 u16 dev_id;
131 u32 dmauncerrsts;
132
133 /*
134 * Write CHANERRMSK_INT with 3E07h to mask out the errors
135 * that can cause stability issues for IOAT ver.3
136 */
137 chan_err_mask = 0x3E07;
138 pci_write_config_dword(device->pdev,
139 IOAT_PCI_CHANERRMASK_INT_OFFSET,
140 chan_err_mask);
141
142 /*
143 * Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
144 * (workaround for spurious config parity error after restart)
145 */
146 pci_read_config_word(device->pdev,
147 IOAT_PCI_DEVICE_ID_OFFSET,
148 &dev_id);
149 if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
150 dmauncerrsts = 0x10;
151 pci_write_config_dword(device->pdev,
152 IOAT_PCI_DMAUNCERRSTS_OFFSET,
153 dmauncerrsts);
154 }
155 }
156
Chris Leeche3828812007-03-08 09:57:35 -0800157 device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
158 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700159 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
160
Venki Pallipadif371be62008-10-23 15:39:06 -0700161#ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
Len Brown2f102602009-05-27 23:59:58 -0400162 if (i7300_idle_platform_probe(NULL, NULL, 1) == 0) {
Venki Pallipadi3ad0b022008-10-22 16:34:52 -0700163 device->common.chancnt--;
164 }
Andy Henroid27471fd2008-10-09 11:45:22 -0700165#endif
Chris Leech0bbd5f42006-05-23 17:35:34 -0700166 for (i = 0; i < device->common.chancnt; i++) {
Dan Williamse6c0b692009-09-08 17:29:44 -0700167 ioat_chan = devm_kzalloc(dev, sizeof(*ioat_chan), GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700168 if (!ioat_chan) {
169 device->common.chancnt = i;
170 break;
171 }
172
173 ioat_chan->device = device;
174 ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
175 ioat_chan->xfercap = xfercap;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800176 ioat_chan->desccount = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700177 INIT_DELAYED_WORK(&ioat_chan->work, ioat_dma_chan_reset_part2);
Maciej Sosnowskiea9c7172009-02-26 11:04:38 +0100178 if (ioat_chan->device->version == IOAT_VER_2_0)
179 writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE |
180 IOAT_DMA_DCA_ANY_CPU,
181 ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
182 else if (ioat_chan->device->version == IOAT_VER_3_0)
183 writel(IOAT_DMA_DCA_ANY_CPU,
184 ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700185 spin_lock_init(&ioat_chan->cleanup_lock);
186 spin_lock_init(&ioat_chan->desc_lock);
187 INIT_LIST_HEAD(&ioat_chan->free_desc);
188 INIT_LIST_HEAD(&ioat_chan->used_desc);
189 /* This should be made common somewhere in dmaengine.c */
190 ioat_chan->common.device = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700191 list_add_tail(&ioat_chan->common.device_node,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700192 &device->common.channels);
Shannon Nelson3e037452007-10-16 01:27:40 -0700193 device->idx[i] = ioat_chan;
194 tasklet_init(&ioat_chan->cleanup_task,
195 ioat_dma_cleanup_tasklet,
196 (unsigned long) ioat_chan);
197 tasklet_disable(&ioat_chan->cleanup_task);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700198 }
199 return device->common.chancnt;
200}
201
Shannon Nelson711924b2007-12-17 16:20:08 -0800202/**
203 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
204 * descriptors to hw
205 * @chan: DMA channel handle
206 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700207static inline void
208__ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat_chan)
Shannon Nelson711924b2007-12-17 16:20:08 -0800209{
210 ioat_chan->pending = 0;
211 writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
212}
213
214static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
215{
216 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
217
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700218 if (ioat_chan->pending > 0) {
Shannon Nelson711924b2007-12-17 16:20:08 -0800219 spin_lock_bh(&ioat_chan->desc_lock);
220 __ioat1_dma_memcpy_issue_pending(ioat_chan);
221 spin_unlock_bh(&ioat_chan->desc_lock);
222 }
223}
224
Dan Williamsbc3c7022009-07-28 14:33:42 -0700225static inline void
226__ioat2_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat_chan)
Shannon Nelson711924b2007-12-17 16:20:08 -0800227{
228 ioat_chan->pending = 0;
229 writew(ioat_chan->dmacount,
230 ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
231}
232
233static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
234{
235 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
236
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700237 if (ioat_chan->pending > 0) {
Shannon Nelson711924b2007-12-17 16:20:08 -0800238 spin_lock_bh(&ioat_chan->desc_lock);
239 __ioat2_dma_memcpy_issue_pending(ioat_chan);
240 spin_unlock_bh(&ioat_chan->desc_lock);
241 }
242}
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800243
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700244
245/**
246 * ioat_dma_chan_reset_part2 - reinit the channel after a reset
247 */
248static void ioat_dma_chan_reset_part2(struct work_struct *work)
249{
250 struct ioat_dma_chan *ioat_chan =
251 container_of(work, struct ioat_dma_chan, work.work);
252 struct ioat_desc_sw *desc;
253
254 spin_lock_bh(&ioat_chan->cleanup_lock);
255 spin_lock_bh(&ioat_chan->desc_lock);
256
257 ioat_chan->completion_virt->low = 0;
258 ioat_chan->completion_virt->high = 0;
259 ioat_chan->pending = 0;
260
261 /*
262 * count the descriptors waiting, and be sure to do it
263 * right for both the CB1 line and the CB2 ring
264 */
265 ioat_chan->dmacount = 0;
266 if (ioat_chan->used_desc.prev) {
267 desc = to_ioat_desc(ioat_chan->used_desc.prev);
268 do {
269 ioat_chan->dmacount++;
270 desc = to_ioat_desc(desc->node.next);
271 } while (&desc->node != ioat_chan->used_desc.next);
272 }
273
274 /*
275 * write the new starting descriptor address
276 * this puts channel engine into ARMED state
277 */
278 desc = to_ioat_desc(ioat_chan->used_desc.prev);
279 switch (ioat_chan->device->version) {
280 case IOAT_VER_1_2:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700281 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700282 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700283 writel(((u64) desc->txd.phys) >> 32,
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700284 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
285
286 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
287 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
288 break;
289 case IOAT_VER_2_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700290 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700291 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700292 writel(((u64) desc->txd.phys) >> 32,
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700293 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
294
295 /* tell the engine to go with what's left to be done */
296 writew(ioat_chan->dmacount,
297 ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
298
299 break;
300 }
Dan Williamsbc3c7022009-07-28 14:33:42 -0700301 dev_err(to_dev(ioat_chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700302 "chan%d reset - %d descs waiting, %d total desc\n",
303 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
304
305 spin_unlock_bh(&ioat_chan->desc_lock);
306 spin_unlock_bh(&ioat_chan->cleanup_lock);
307}
308
309/**
310 * ioat_dma_reset_channel - restart a channel
311 * @ioat_chan: IOAT DMA channel handle
312 */
313static void ioat_dma_reset_channel(struct ioat_dma_chan *ioat_chan)
314{
315 u32 chansts, chanerr;
316
317 if (!ioat_chan->used_desc.prev)
318 return;
319
320 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
321 chansts = (ioat_chan->completion_virt->low
322 & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
323 if (chanerr) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700324 dev_err(to_dev(ioat_chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700325 "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
326 chan_num(ioat_chan), chansts, chanerr);
327 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
328 }
329
330 /*
331 * whack it upside the head with a reset
332 * and wait for things to settle out.
333 * force the pending count to a really big negative
334 * to make sure no one forces an issue_pending
335 * while we're waiting.
336 */
337
338 spin_lock_bh(&ioat_chan->desc_lock);
339 ioat_chan->pending = INT_MIN;
340 writeb(IOAT_CHANCMD_RESET,
341 ioat_chan->reg_base
342 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
343 spin_unlock_bh(&ioat_chan->desc_lock);
344
345 /* schedule the 2nd half instead of sleeping a long time */
346 schedule_delayed_work(&ioat_chan->work, RESET_DELAY);
347}
348
349/**
350 * ioat_dma_chan_watchdog - watch for stuck channels
351 */
352static void ioat_dma_chan_watchdog(struct work_struct *work)
353{
354 struct ioatdma_device *device =
355 container_of(work, struct ioatdma_device, work.work);
356 struct ioat_dma_chan *ioat_chan;
357 int i;
358
359 union {
360 u64 full;
361 struct {
362 u32 low;
363 u32 high;
364 };
365 } completion_hw;
366 unsigned long compl_desc_addr_hw;
367
368 for (i = 0; i < device->common.chancnt; i++) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700369 ioat_chan = ioat_chan_by_index(device, i);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700370
371 if (ioat_chan->device->version == IOAT_VER_1_2
372 /* have we started processing anything yet */
373 && ioat_chan->last_completion
374 /* have we completed any since last watchdog cycle? */
375 && (ioat_chan->last_completion ==
376 ioat_chan->watchdog_completion)
377 /* has TCP stuck on one cookie since last watchdog? */
378 && (ioat_chan->watchdog_tcp_cookie ==
379 ioat_chan->watchdog_last_tcp_cookie)
380 && (ioat_chan->watchdog_tcp_cookie !=
381 ioat_chan->completed_cookie)
382 /* is there something in the chain to be processed? */
383 /* CB1 chain always has at least the last one processed */
384 && (ioat_chan->used_desc.prev != ioat_chan->used_desc.next)
385 && ioat_chan->pending == 0) {
386
387 /*
388 * check CHANSTS register for completed
389 * descriptor address.
390 * if it is different than completion writeback,
391 * it is not zero
392 * and it has changed since the last watchdog
393 * we can assume that channel
394 * is still working correctly
395 * and the problem is in completion writeback.
396 * update completion writeback
397 * with actual CHANSTS value
398 * else
399 * try resetting the channel
400 */
401
402 completion_hw.low = readl(ioat_chan->reg_base +
403 IOAT_CHANSTS_OFFSET_LOW(ioat_chan->device->version));
404 completion_hw.high = readl(ioat_chan->reg_base +
405 IOAT_CHANSTS_OFFSET_HIGH(ioat_chan->device->version));
406#if (BITS_PER_LONG == 64)
407 compl_desc_addr_hw =
408 completion_hw.full
409 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
410#else
411 compl_desc_addr_hw =
412 completion_hw.low & IOAT_LOW_COMPLETION_MASK;
413#endif
414
415 if ((compl_desc_addr_hw != 0)
416 && (compl_desc_addr_hw != ioat_chan->watchdog_completion)
417 && (compl_desc_addr_hw != ioat_chan->last_compl_desc_addr_hw)) {
418 ioat_chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
419 ioat_chan->completion_virt->low = completion_hw.low;
420 ioat_chan->completion_virt->high = completion_hw.high;
421 } else {
422 ioat_dma_reset_channel(ioat_chan);
423 ioat_chan->watchdog_completion = 0;
424 ioat_chan->last_compl_desc_addr_hw = 0;
425 }
426
427 /*
428 * for version 2.0 if there are descriptors yet to be processed
429 * and the last completed hasn't changed since the last watchdog
430 * if they haven't hit the pending level
431 * issue the pending to push them through
432 * else
433 * try resetting the channel
434 */
435 } else if (ioat_chan->device->version == IOAT_VER_2_0
436 && ioat_chan->used_desc.prev
437 && ioat_chan->last_completion
438 && ioat_chan->last_completion == ioat_chan->watchdog_completion) {
439
440 if (ioat_chan->pending < ioat_pending_level)
441 ioat2_dma_memcpy_issue_pending(&ioat_chan->common);
442 else {
443 ioat_dma_reset_channel(ioat_chan);
444 ioat_chan->watchdog_completion = 0;
445 }
446 } else {
447 ioat_chan->last_compl_desc_addr_hw = 0;
448 ioat_chan->watchdog_completion
449 = ioat_chan->last_completion;
450 }
451
452 ioat_chan->watchdog_last_tcp_cookie =
453 ioat_chan->watchdog_tcp_cookie;
454 }
455
456 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
457}
458
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800459static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700460{
461 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700462 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
463 struct ioat_desc_sw *prev, *new;
464 struct ioat_dma_descriptor *hw;
Dan Williams7405f742007-01-02 11:10:43 -0700465 dma_cookie_t cookie;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700466 LIST_HEAD(new_chain);
467 u32 copy;
468 size_t len;
469 dma_addr_t src, dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700470 unsigned long orig_flags;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700471 unsigned int desc_count = 0;
Dan Williams7405f742007-01-02 11:10:43 -0700472
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700473 /* src and dest and len are stored in the initial descriptor */
474 len = first->len;
475 src = first->src;
476 dst = first->dst;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700477 orig_flags = first->txd.flags;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700478 new = first;
479
Dan Williams7405f742007-01-02 11:10:43 -0700480 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700481 prev = to_ioat_desc(ioat_chan->used_desc.prev);
482 prefetch(prev->hw);
483 do {
Shannon Nelson711924b2007-12-17 16:20:08 -0800484 copy = min_t(size_t, len, ioat_chan->xfercap);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700485
Dan Williamsbc3c7022009-07-28 14:33:42 -0700486 async_tx_ack(&new->txd);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700487
488 hw = new->hw;
489 hw->size = copy;
490 hw->ctl = 0;
491 hw->src_addr = src;
492 hw->dst_addr = dst;
493 hw->next = 0;
494
495 /* chain together the physical address list for the HW */
496 wmb();
Dan Williamsbc3c7022009-07-28 14:33:42 -0700497 prev->hw->next = (u64) new->txd.phys;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700498
499 len -= copy;
500 dst += copy;
501 src += copy;
502
503 list_add_tail(&new->node, &new_chain);
504 desc_count++;
505 prev = new;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800506 } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700507
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700508 if (!new) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700509 dev_err(to_dev(ioat_chan), "tx submit failed\n");
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700510 spin_unlock_bh(&ioat_chan->desc_lock);
511 return -ENOMEM;
512 }
513
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700514 hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700515 if (first->txd.callback) {
Shannon Nelson95218432007-10-18 03:07:15 -0700516 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
517 if (first != new) {
518 /* move callback into to last desc */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700519 new->txd.callback = first->txd.callback;
520 new->txd.callback_param
521 = first->txd.callback_param;
522 first->txd.callback = NULL;
523 first->txd.callback_param = NULL;
Shannon Nelson95218432007-10-18 03:07:15 -0700524 }
525 }
526
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700527 new->tx_cnt = desc_count;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700528 new->txd.flags = orig_flags; /* client is in control of this ack */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700529
530 /* store the original values for use in later cleanup */
531 if (new != first) {
532 new->src = first->src;
533 new->dst = first->dst;
534 new->len = first->len;
535 }
536
Dan Williams7405f742007-01-02 11:10:43 -0700537 /* cookie incr and addition to used_list must be atomic */
538 cookie = ioat_chan->common.cookie;
539 cookie++;
540 if (cookie < 0)
541 cookie = 1;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700542 ioat_chan->common.cookie = new->txd.cookie = cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700543
544 /* write address into NextDescriptor field of last desc in chain */
545 to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
Dan Williamsbc3c7022009-07-28 14:33:42 -0700546 first->txd.phys;
Luis R. Rodriguez7d283ae2008-08-06 15:21:26 -0700547 list_splice_tail(&new_chain, &ioat_chan->used_desc);
Dan Williams7405f742007-01-02 11:10:43 -0700548
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800549 ioat_chan->dmacount += desc_count;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700550 ioat_chan->pending += desc_count;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800551 if (ioat_chan->pending >= ioat_pending_level)
552 __ioat1_dma_memcpy_issue_pending(ioat_chan);
Dan Williams7405f742007-01-02 11:10:43 -0700553 spin_unlock_bh(&ioat_chan->desc_lock);
554
Dan Williams7405f742007-01-02 11:10:43 -0700555 return cookie;
556}
557
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800558static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
559{
560 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
561 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
562 struct ioat_desc_sw *new;
563 struct ioat_dma_descriptor *hw;
564 dma_cookie_t cookie;
565 u32 copy;
566 size_t len;
567 dma_addr_t src, dst;
Dan Williams636bdea2008-04-17 20:17:26 -0700568 unsigned long orig_flags;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800569 unsigned int desc_count = 0;
570
571 /* src and dest and len are stored in the initial descriptor */
572 len = first->len;
573 src = first->src;
574 dst = first->dst;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700575 orig_flags = first->txd.flags;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800576 new = first;
577
Shannon Nelson711924b2007-12-17 16:20:08 -0800578 /*
579 * ioat_chan->desc_lock is still in force in version 2 path
580 * it gets unlocked at end of this function
581 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800582 do {
Shannon Nelson711924b2007-12-17 16:20:08 -0800583 copy = min_t(size_t, len, ioat_chan->xfercap);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800584
Dan Williamsbc3c7022009-07-28 14:33:42 -0700585 async_tx_ack(&new->txd);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800586
587 hw = new->hw;
588 hw->size = copy;
589 hw->ctl = 0;
590 hw->src_addr = src;
591 hw->dst_addr = dst;
592
593 len -= copy;
594 dst += copy;
595 src += copy;
596 desc_count++;
597 } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
598
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700599 if (!new) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700600 dev_err(to_dev(ioat_chan), "tx submit failed\n");
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700601 spin_unlock_bh(&ioat_chan->desc_lock);
602 return -ENOMEM;
603 }
604
605 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700606 if (first->txd.callback) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800607 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
608 if (first != new) {
609 /* move callback into to last desc */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700610 new->txd.callback = first->txd.callback;
611 new->txd.callback_param
612 = first->txd.callback_param;
613 first->txd.callback = NULL;
614 first->txd.callback_param = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800615 }
616 }
617
618 new->tx_cnt = desc_count;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700619 new->txd.flags = orig_flags; /* client is in control of this ack */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800620
621 /* store the original values for use in later cleanup */
622 if (new != first) {
623 new->src = first->src;
624 new->dst = first->dst;
625 new->len = first->len;
626 }
627
628 /* cookie incr and addition to used_list must be atomic */
629 cookie = ioat_chan->common.cookie;
630 cookie++;
631 if (cookie < 0)
632 cookie = 1;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700633 ioat_chan->common.cookie = new->txd.cookie = cookie;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800634
635 ioat_chan->dmacount += desc_count;
636 ioat_chan->pending += desc_count;
637 if (ioat_chan->pending >= ioat_pending_level)
638 __ioat2_dma_memcpy_issue_pending(ioat_chan);
639 spin_unlock_bh(&ioat_chan->desc_lock);
640
641 return cookie;
642}
643
644/**
645 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
646 * @ioat_chan: the channel supplying the memory pool for the descriptors
647 * @flags: allocation flags
648 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700649static struct ioat_desc_sw *
650ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat_chan, gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700651{
652 struct ioat_dma_descriptor *desc;
653 struct ioat_desc_sw *desc_sw;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700654 struct ioatdma_device *ioatdma_device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700655 dma_addr_t phys;
656
Shannon Nelson8ab89562007-10-16 01:27:39 -0700657 ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
658 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700659 if (unlikely(!desc))
660 return NULL;
661
662 desc_sw = kzalloc(sizeof(*desc_sw), flags);
663 if (unlikely(!desc_sw)) {
Shannon Nelson8ab89562007-10-16 01:27:39 -0700664 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700665 return NULL;
666 }
667
668 memset(desc, 0, sizeof(*desc));
Dan Williamsbc3c7022009-07-28 14:33:42 -0700669 dma_async_tx_descriptor_init(&desc_sw->txd, &ioat_chan->common);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800670 switch (ioat_chan->device->version) {
671 case IOAT_VER_1_2:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700672 desc_sw->txd.tx_submit = ioat1_tx_submit;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800673 break;
674 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700675 case IOAT_VER_3_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700676 desc_sw->txd.tx_submit = ioat2_tx_submit;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800677 break;
678 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800679
Chris Leech0bbd5f42006-05-23 17:35:34 -0700680 desc_sw->hw = desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700681 desc_sw->txd.phys = phys;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700682
683 return desc_sw;
684}
685
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800686static int ioat_initial_desc_count = 256;
687module_param(ioat_initial_desc_count, int, 0644);
688MODULE_PARM_DESC(ioat_initial_desc_count,
689 "initial descriptors per channel (default: 256)");
690
691/**
692 * ioat2_dma_massage_chan_desc - link the descriptors into a circle
693 * @ioat_chan: the channel to be massaged
694 */
695static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
696{
697 struct ioat_desc_sw *desc, *_desc;
698
699 /* setup used_desc */
700 ioat_chan->used_desc.next = ioat_chan->free_desc.next;
701 ioat_chan->used_desc.prev = NULL;
702
703 /* pull free_desc out of the circle so that every node is a hw
704 * descriptor, but leave it pointing to the list
705 */
706 ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
707 ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
708
709 /* circle link the hw descriptors */
710 desc = to_ioat_desc(ioat_chan->free_desc.next);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700711 desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800712 list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700713 desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800714 }
715}
716
717/**
718 * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
719 * @chan: the channel to be filled out
720 */
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700721static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700722{
723 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800724 struct ioat_desc_sw *desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700725 u16 chanctrl;
726 u32 chanerr;
727 int i;
728 LIST_HEAD(tmp_list);
729
Shannon Nelsone4223972007-08-24 23:02:53 -0700730 /* have we already been set up? */
731 if (!list_empty(&ioat_chan->free_desc))
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800732 return ioat_chan->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700733
Shannon Nelson43d6e362007-10-16 01:27:39 -0700734 /* Setup register to interrupt and write completion status on error */
Shannon Nelsone4223972007-08-24 23:02:53 -0700735 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
Chris Leech0bbd5f42006-05-23 17:35:34 -0700736 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
737 IOAT_CHANCTRL_ERR_COMPLETION_EN;
Shannon Nelson43d6e362007-10-16 01:27:39 -0700738 writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700739
Chris Leeche3828812007-03-08 09:57:35 -0800740 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700741 if (chanerr) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700742 dev_err(to_dev(ioat_chan), "CHANERR = %x, clearing\n", chanerr);
Chris Leeche3828812007-03-08 09:57:35 -0800743 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700744 }
745
746 /* Allocate descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800747 for (i = 0; i < ioat_initial_desc_count; i++) {
Chris Leech0bbd5f42006-05-23 17:35:34 -0700748 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
749 if (!desc) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700750 dev_err(to_dev(ioat_chan),
Shannon Nelson5149fd02007-10-18 03:07:13 -0700751 "Only %d initial descriptors\n", i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700752 break;
753 }
754 list_add_tail(&desc->node, &tmp_list);
755 }
756 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800757 ioat_chan->desccount = i;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700758 list_splice(&tmp_list, &ioat_chan->free_desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800759 if (ioat_chan->device->version != IOAT_VER_1_2)
760 ioat2_dma_massage_chan_desc(ioat_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700761 spin_unlock_bh(&ioat_chan->desc_lock);
762
763 /* allocate a completion writeback area */
764 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
765 ioat_chan->completion_virt =
766 pci_pool_alloc(ioat_chan->device->completion_pool,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700767 GFP_KERNEL,
768 &ioat_chan->completion_addr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700769 memset(ioat_chan->completion_virt, 0,
770 sizeof(*ioat_chan->completion_virt));
Chris Leeche3828812007-03-08 09:57:35 -0800771 writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
772 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
773 writel(((u64) ioat_chan->completion_addr) >> 32,
774 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700775
Shannon Nelson3e037452007-10-16 01:27:40 -0700776 tasklet_enable(&ioat_chan->cleanup_task);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800777 ioat_dma_start_null_desc(ioat_chan); /* give chain to dma device */
778 return ioat_chan->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700779}
780
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800781/**
782 * ioat_dma_free_chan_resources - release all the descriptors
783 * @chan: the channel to be cleaned
784 */
Chris Leech0bbd5f42006-05-23 17:35:34 -0700785static void ioat_dma_free_chan_resources(struct dma_chan *chan)
786{
787 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700788 struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700789 struct ioat_desc_sw *desc, *_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700790 int in_use_descs = 0;
791
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000792 /* Before freeing channel resources first check
793 * if they have been previously allocated for this channel.
794 */
795 if (ioat_chan->desccount == 0)
796 return;
797
Shannon Nelson3e037452007-10-16 01:27:40 -0700798 tasklet_disable(&ioat_chan->cleanup_task);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700799 ioat_dma_memcpy_cleanup(ioat_chan);
800
Shannon Nelson3e037452007-10-16 01:27:40 -0700801 /* Delay 100ms after reset to allow internal DMA logic to quiesce
802 * before removing DMA descriptor resources.
803 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800804 writeb(IOAT_CHANCMD_RESET,
805 ioat_chan->reg_base
806 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
Shannon Nelson3e037452007-10-16 01:27:40 -0700807 mdelay(100);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700808
809 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800810 switch (ioat_chan->device->version) {
811 case IOAT_VER_1_2:
812 list_for_each_entry_safe(desc, _desc,
813 &ioat_chan->used_desc, node) {
814 in_use_descs++;
815 list_del(&desc->node);
816 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700817 desc->txd.phys);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800818 kfree(desc);
819 }
820 list_for_each_entry_safe(desc, _desc,
821 &ioat_chan->free_desc, node) {
822 list_del(&desc->node);
823 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700824 desc->txd.phys);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800825 kfree(desc);
826 }
827 break;
828 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700829 case IOAT_VER_3_0:
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800830 list_for_each_entry_safe(desc, _desc,
831 ioat_chan->free_desc.next, node) {
832 list_del(&desc->node);
833 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700834 desc->txd.phys);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800835 kfree(desc);
836 }
837 desc = to_ioat_desc(ioat_chan->free_desc.next);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700838 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700839 desc->txd.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700840 kfree(desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800841 INIT_LIST_HEAD(&ioat_chan->free_desc);
842 INIT_LIST_HEAD(&ioat_chan->used_desc);
843 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700844 }
845 spin_unlock_bh(&ioat_chan->desc_lock);
846
Shannon Nelson8ab89562007-10-16 01:27:39 -0700847 pci_pool_free(ioatdma_device->completion_pool,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700848 ioat_chan->completion_virt,
849 ioat_chan->completion_addr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700850
851 /* one is ok since we left it on there on purpose */
852 if (in_use_descs > 1)
Dan Williamsbc3c7022009-07-28 14:33:42 -0700853 dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700854 in_use_descs - 1);
855
856 ioat_chan->last_completion = ioat_chan->completion_addr = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700857 ioat_chan->pending = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800858 ioat_chan->dmacount = 0;
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000859 ioat_chan->desccount = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700860 ioat_chan->watchdog_completion = 0;
861 ioat_chan->last_compl_desc_addr_hw = 0;
862 ioat_chan->watchdog_tcp_cookie =
863 ioat_chan->watchdog_last_tcp_cookie = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700864}
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700865
Shannon Nelson3e037452007-10-16 01:27:40 -0700866/**
867 * ioat_dma_get_next_descriptor - return the next available descriptor
868 * @ioat_chan: IOAT DMA channel handle
869 *
870 * Gets the next descriptor from the chain, and must be called with the
871 * channel's desc_lock held. Allocates more descriptors if the channel
872 * has run out.
873 */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700874static struct ioat_desc_sw *
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800875ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
Shannon Nelson3e037452007-10-16 01:27:40 -0700876{
Shannon Nelson711924b2007-12-17 16:20:08 -0800877 struct ioat_desc_sw *new;
Shannon Nelson3e037452007-10-16 01:27:40 -0700878
879 if (!list_empty(&ioat_chan->free_desc)) {
880 new = to_ioat_desc(ioat_chan->free_desc.next);
881 list_del(&new->node);
882 } else {
883 /* try to get another desc */
884 new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800885 if (!new) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700886 dev_err(to_dev(ioat_chan), "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800887 return NULL;
888 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700889 }
890
891 prefetch(new->hw);
892 return new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700893}
894
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800895static struct ioat_desc_sw *
896ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
897{
Shannon Nelson711924b2007-12-17 16:20:08 -0800898 struct ioat_desc_sw *new;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800899
900 /*
901 * used.prev points to where to start processing
902 * used.next points to next free descriptor
903 * if used.prev == NULL, there are none waiting to be processed
904 * if used.next == used.prev.prev, there is only one free descriptor,
905 * and we need to use it to as a noop descriptor before
906 * linking in a new set of descriptors, since the device
907 * has probably already read the pointer to it
908 */
909 if (ioat_chan->used_desc.prev &&
910 ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
911
Shannon Nelson711924b2007-12-17 16:20:08 -0800912 struct ioat_desc_sw *desc;
913 struct ioat_desc_sw *noop_desc;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800914 int i;
915
916 /* set up the noop descriptor */
917 noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700918 /* set size to non-zero value (channel returns error when size is 0) */
919 noop_desc->hw->size = NULL_DESC_BUFFER_SIZE;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800920 noop_desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
921 noop_desc->hw->src_addr = 0;
922 noop_desc->hw->dst_addr = 0;
923
924 ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
925 ioat_chan->pending++;
926 ioat_chan->dmacount++;
927
Shannon Nelson711924b2007-12-17 16:20:08 -0800928 /* try to get a few more descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800929 for (i = 16; i; i--) {
930 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800931 if (!desc) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700932 dev_err(to_dev(ioat_chan), "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800933 break;
934 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800935 list_add_tail(&desc->node, ioat_chan->used_desc.next);
936
937 desc->hw->next
Dan Williamsbc3c7022009-07-28 14:33:42 -0700938 = to_ioat_desc(desc->node.next)->txd.phys;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800939 to_ioat_desc(desc->node.prev)->hw->next
Dan Williamsbc3c7022009-07-28 14:33:42 -0700940 = desc->txd.phys;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800941 ioat_chan->desccount++;
942 }
943
944 ioat_chan->used_desc.next = noop_desc->node.next;
945 }
946 new = to_ioat_desc(ioat_chan->used_desc.next);
947 prefetch(new);
948 ioat_chan->used_desc.next = new->node.next;
949
950 if (ioat_chan->used_desc.prev == NULL)
951 ioat_chan->used_desc.prev = &new->node;
952
953 prefetch(new->hw);
954 return new;
955}
956
Dan Williamsbc3c7022009-07-28 14:33:42 -0700957static struct ioat_desc_sw *
958ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800959{
960 if (!ioat_chan)
961 return NULL;
962
963 switch (ioat_chan->device->version) {
964 case IOAT_VER_1_2:
965 return ioat1_dma_get_next_descriptor(ioat_chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800966 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700967 case IOAT_VER_3_0:
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800968 return ioat2_dma_get_next_descriptor(ioat_chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800969 }
970 return NULL;
971}
972
Dan Williamsbc3c7022009-07-28 14:33:42 -0700973static struct dma_async_tx_descriptor *
974ioat1_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
975 dma_addr_t dma_src, size_t len, unsigned long flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700976{
Dan Williams7405f742007-01-02 11:10:43 -0700977 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700978 struct ioat_desc_sw *new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700979
980 spin_lock_bh(&ioat_chan->desc_lock);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700981 new = ioat_dma_get_next_descriptor(ioat_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700982 spin_unlock_bh(&ioat_chan->desc_lock);
983
Shannon Nelson711924b2007-12-17 16:20:08 -0800984 if (new) {
985 new->len = len;
Dan Williams00367312008-02-02 19:49:57 -0700986 new->dst = dma_dest;
987 new->src = dma_src;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700988 new->txd.flags = flags;
989 return &new->txd;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700990 } else {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700991 dev_err(to_dev(ioat_chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700992 "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
993 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
Shannon Nelson711924b2007-12-17 16:20:08 -0800994 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700995 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700996}
997
Dan Williamsbc3c7022009-07-28 14:33:42 -0700998static struct dma_async_tx_descriptor *
999ioat2_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
1000 dma_addr_t dma_src, size_t len, unsigned long flags)
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001001{
1002 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
1003 struct ioat_desc_sw *new;
1004
1005 spin_lock_bh(&ioat_chan->desc_lock);
1006 new = ioat2_dma_get_next_descriptor(ioat_chan);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001007
Shannon Nelson711924b2007-12-17 16:20:08 -08001008 /*
1009 * leave ioat_chan->desc_lock set in ioat 2 path
1010 * it will get unlocked at end of tx_submit
1011 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001012
Shannon Nelson711924b2007-12-17 16:20:08 -08001013 if (new) {
1014 new->len = len;
Dan Williams00367312008-02-02 19:49:57 -07001015 new->dst = dma_dest;
1016 new->src = dma_src;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001017 new->txd.flags = flags;
1018 return &new->txd;
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001019 } else {
1020 spin_unlock_bh(&ioat_chan->desc_lock);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001021 dev_err(to_dev(ioat_chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001022 "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
1023 chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
Shannon Nelson711924b2007-12-17 16:20:08 -08001024 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001025 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001026}
1027
Shannon Nelson3e037452007-10-16 01:27:40 -07001028static void ioat_dma_cleanup_tasklet(unsigned long data)
1029{
1030 struct ioat_dma_chan *chan = (void *)data;
1031 ioat_dma_memcpy_cleanup(chan);
1032 writew(IOAT_CHANCTRL_INT_DISABLE,
1033 chan->reg_base + IOAT_CHANCTRL_OFFSET);
1034}
1035
Dan Williamse1d181e2008-07-04 00:13:40 -07001036static void
1037ioat_dma_unmap(struct ioat_dma_chan *ioat_chan, struct ioat_desc_sw *desc)
1038{
Dan Williamsbc3c7022009-07-28 14:33:42 -07001039 if (!(desc->txd.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
1040 if (desc->txd.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001041 pci_unmap_single(ioat_chan->device->pdev,
1042 pci_unmap_addr(desc, dst),
1043 pci_unmap_len(desc, len),
1044 PCI_DMA_FROMDEVICE);
1045 else
1046 pci_unmap_page(ioat_chan->device->pdev,
1047 pci_unmap_addr(desc, dst),
1048 pci_unmap_len(desc, len),
1049 PCI_DMA_FROMDEVICE);
1050 }
Dan Williamse1d181e2008-07-04 00:13:40 -07001051
Dan Williamsbc3c7022009-07-28 14:33:42 -07001052 if (!(desc->txd.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
1053 if (desc->txd.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001054 pci_unmap_single(ioat_chan->device->pdev,
1055 pci_unmap_addr(desc, src),
1056 pci_unmap_len(desc, len),
1057 PCI_DMA_TODEVICE);
1058 else
1059 pci_unmap_page(ioat_chan->device->pdev,
1060 pci_unmap_addr(desc, src),
1061 pci_unmap_len(desc, len),
1062 PCI_DMA_TODEVICE);
1063 }
Dan Williamse1d181e2008-07-04 00:13:40 -07001064}
1065
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001066/**
1067 * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
1068 * @chan: ioat channel to be cleaned up
1069 */
Shannon Nelson43d6e362007-10-16 01:27:39 -07001070static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001071{
1072 unsigned long phys_complete;
1073 struct ioat_desc_sw *desc, *_desc;
1074 dma_cookie_t cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001075 unsigned long desc_phys;
1076 struct ioat_desc_sw *latest_desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001077 struct dma_async_tx_descriptor *tx;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001078
Shannon Nelson43d6e362007-10-16 01:27:39 -07001079 prefetch(ioat_chan->completion_virt);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001080
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001081 if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
Chris Leech0bbd5f42006-05-23 17:35:34 -07001082 return;
1083
1084 /* The completion writeback can happen at any time,
1085 so reads by the driver need to be atomic operations
1086 The descriptor physical addresses are limited to 32-bits
1087 when the CPU can only do a 32-bit mov */
1088
1089#if (BITS_PER_LONG == 64)
1090 phys_complete =
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001091 ioat_chan->completion_virt->full
1092 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001093#else
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001094 phys_complete =
1095 ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001096#endif
1097
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001098 if ((ioat_chan->completion_virt->full
1099 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
Shannon Nelson43d6e362007-10-16 01:27:39 -07001100 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001101 dev_err(to_dev(ioat_chan), "Channel halted, chanerr = %x\n",
Shannon Nelson43d6e362007-10-16 01:27:39 -07001102 readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
Chris Leech0bbd5f42006-05-23 17:35:34 -07001103
1104 /* TODO do something to salvage the situation */
1105 }
1106
Shannon Nelson43d6e362007-10-16 01:27:39 -07001107 if (phys_complete == ioat_chan->last_completion) {
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001108 spin_unlock_bh(&ioat_chan->cleanup_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001109 /*
1110 * perhaps we're stuck so hard that the watchdog can't go off?
1111 * try to catch it after 2 seconds
1112 */
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001113 if (ioat_chan->device->version != IOAT_VER_3_0) {
1114 if (time_after(jiffies,
1115 ioat_chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
1116 ioat_dma_chan_watchdog(&(ioat_chan->device->work.work));
1117 ioat_chan->last_completion_time = jiffies;
1118 }
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001119 }
1120 return;
1121 }
1122 ioat_chan->last_completion_time = jiffies;
1123
1124 cookie = 0;
1125 if (!spin_trylock_bh(&ioat_chan->desc_lock)) {
1126 spin_unlock_bh(&ioat_chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001127 return;
1128 }
1129
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001130 switch (ioat_chan->device->version) {
1131 case IOAT_VER_1_2:
1132 list_for_each_entry_safe(desc, _desc,
1133 &ioat_chan->used_desc, node) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001134 tx = &desc->txd;
Shannon Nelson43d6e362007-10-16 01:27:39 -07001135 /*
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001136 * Incoming DMA requests may use multiple descriptors,
1137 * due to exceeding xfercap, perhaps. If so, only the
1138 * last one will have a cookie, and require unmapping.
Shannon Nelson43d6e362007-10-16 01:27:39 -07001139 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001140 if (tx->cookie) {
1141 cookie = tx->cookie;
Dan Williamse1d181e2008-07-04 00:13:40 -07001142 ioat_dma_unmap(ioat_chan, desc);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001143 if (tx->callback) {
1144 tx->callback(tx->callback_param);
1145 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001146 }
1147 }
1148
Dan Williamsbc3c7022009-07-28 14:33:42 -07001149 if (tx->phys != phys_complete) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001150 /*
1151 * a completed entry, but not the last, so clean
1152 * up if the client is done with the descriptor
1153 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001154 if (async_tx_test_ack(tx)) {
Eric Sesterhennaa2d0b82009-02-26 11:05:30 +01001155 list_move_tail(&desc->node,
1156 &ioat_chan->free_desc);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001157 } else
Dan Williamsbc3c7022009-07-28 14:33:42 -07001158 tx->cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001159 } else {
1160 /*
1161 * last used desc. Do not remove, so we can
1162 * append from it, but don't look at it next
1163 * time, either
1164 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001165 tx->cookie = 0;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001166
1167 /* TODO check status bits? */
1168 break;
Shannon Nelson95218432007-10-18 03:07:15 -07001169 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001170 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001171 break;
1172 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001173 case IOAT_VER_3_0:
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001174 /* has some other thread has already cleaned up? */
1175 if (ioat_chan->used_desc.prev == NULL)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001176 break;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001177
1178 /* work backwards to find latest finished desc */
1179 desc = to_ioat_desc(ioat_chan->used_desc.next);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001180 tx = &desc->txd;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001181 latest_desc = NULL;
1182 do {
1183 desc = to_ioat_desc(desc->node.prev);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001184 desc_phys = (unsigned long)tx->phys
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001185 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
1186 if (desc_phys == phys_complete) {
1187 latest_desc = desc;
1188 break;
1189 }
1190 } while (&desc->node != ioat_chan->used_desc.prev);
1191
1192 if (latest_desc != NULL) {
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001193 /* work forwards to clear finished descriptors */
1194 for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
1195 &desc->node != latest_desc->node.next &&
1196 &desc->node != ioat_chan->used_desc.next;
1197 desc = to_ioat_desc(desc->node.next)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001198 if (tx->cookie) {
1199 cookie = tx->cookie;
1200 tx->cookie = 0;
Dan Williamse1d181e2008-07-04 00:13:40 -07001201 ioat_dma_unmap(ioat_chan, desc);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001202 if (tx->callback) {
1203 tx->callback(tx->callback_param);
1204 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001205 }
1206 }
1207 }
1208
1209 /* move used.prev up beyond those that are finished */
1210 if (&desc->node == ioat_chan->used_desc.next)
1211 ioat_chan->used_desc.prev = NULL;
1212 else
1213 ioat_chan->used_desc.prev = &desc->node;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001214 }
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001215 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001216 }
1217
Shannon Nelson43d6e362007-10-16 01:27:39 -07001218 spin_unlock_bh(&ioat_chan->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001219
Shannon Nelson43d6e362007-10-16 01:27:39 -07001220 ioat_chan->last_completion = phys_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001221 if (cookie != 0)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001222 ioat_chan->completed_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001223
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001224 spin_unlock_bh(&ioat_chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001225}
1226
1227/**
1228 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
1229 * @chan: IOAT DMA channel handle
1230 * @cookie: DMA transaction identifier
Randy Dunlap65088712006-07-03 19:45:31 -07001231 * @done: if not %NULL, updated with last completed transaction
1232 * @used: if not %NULL, updated with last used transaction
Chris Leech0bbd5f42006-05-23 17:35:34 -07001233 */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001234static enum dma_status
1235ioat_dma_is_complete(struct dma_chan *chan, dma_cookie_t cookie,
1236 dma_cookie_t *done, dma_cookie_t *used)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001237{
1238 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
1239 dma_cookie_t last_used;
1240 dma_cookie_t last_complete;
1241 enum dma_status ret;
1242
1243 last_used = chan->cookie;
1244 last_complete = ioat_chan->completed_cookie;
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001245 ioat_chan->watchdog_tcp_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001246
1247 if (done)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001248 *done = last_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001249 if (used)
1250 *used = last_used;
1251
1252 ret = dma_async_is_complete(cookie, last_complete, last_used);
1253 if (ret == DMA_SUCCESS)
1254 return ret;
1255
1256 ioat_dma_memcpy_cleanup(ioat_chan);
1257
1258 last_used = chan->cookie;
1259 last_complete = ioat_chan->completed_cookie;
1260
1261 if (done)
Shannon Nelson43d6e362007-10-16 01:27:39 -07001262 *done = last_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001263 if (used)
1264 *used = last_used;
1265
1266 return dma_async_is_complete(cookie, last_complete, last_used);
1267}
1268
Shannon Nelson43d6e362007-10-16 01:27:39 -07001269static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001270{
1271 struct ioat_desc_sw *desc;
1272
1273 spin_lock_bh(&ioat_chan->desc_lock);
1274
Shannon Nelson3e037452007-10-16 01:27:40 -07001275 desc = ioat_dma_get_next_descriptor(ioat_chan);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001276
1277 if (!desc) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001278 dev_err(to_dev(ioat_chan),
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001279 "Unable to start null desc - get next desc failed\n");
1280 spin_unlock_bh(&ioat_chan->desc_lock);
1281 return;
1282 }
1283
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001284 desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
1285 | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
1286 | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001287 /* set size to non-zero value (channel returns error when size is 0) */
1288 desc->hw->size = NULL_DESC_BUFFER_SIZE;
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001289 desc->hw->src_addr = 0;
1290 desc->hw->dst_addr = 0;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001291 async_tx_ack(&desc->txd);
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001292 switch (ioat_chan->device->version) {
1293 case IOAT_VER_1_2:
1294 desc->hw->next = 0;
1295 list_add_tail(&desc->node, &ioat_chan->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001296
Dan Williamsbc3c7022009-07-28 14:33:42 -07001297 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001298 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001299 writel(((u64) desc->txd.phys) >> 32,
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001300 ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
1301
1302 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
1303 + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
1304 break;
1305 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001306 case IOAT_VER_3_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -07001307 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001308 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001309 writel(((u64) desc->txd.phys) >> 32,
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001310 ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
1311
1312 ioat_chan->dmacount++;
1313 __ioat2_dma_memcpy_issue_pending(ioat_chan);
1314 break;
1315 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001316 spin_unlock_bh(&ioat_chan->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001317}
1318
1319/*
1320 * Perform a IOAT transaction to verify the HW works.
1321 */
1322#define IOAT_TEST_SIZE 2000
1323
Shannon Nelson95218432007-10-18 03:07:15 -07001324static void ioat_dma_test_callback(void *dma_async_param)
1325{
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001326 struct completion *cmp = dma_async_param;
1327
1328 complete(cmp);
Shannon Nelson95218432007-10-18 03:07:15 -07001329}
1330
Shannon Nelson3e037452007-10-16 01:27:40 -07001331/**
1332 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
1333 * @device: device to be tested
1334 */
1335static int ioat_dma_self_test(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001336{
1337 int i;
1338 u8 *src;
1339 u8 *dest;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001340 struct dma_device *dma = &device->common;
1341 struct device *dev = &device->pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001342 struct dma_chan *dma_chan;
Shannon Nelson711924b2007-12-17 16:20:08 -08001343 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -07001344 dma_addr_t dma_dest, dma_src;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001345 dma_cookie_t cookie;
1346 int err = 0;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001347 struct completion cmp;
Dan Williams0c33e1c2009-03-02 13:31:35 -07001348 unsigned long tmo;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001349 unsigned long flags;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001350
Christoph Lametere94b1762006-12-06 20:33:17 -08001351 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001352 if (!src)
1353 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -08001354 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001355 if (!dest) {
1356 kfree(src);
1357 return -ENOMEM;
1358 }
1359
1360 /* Fill in src buffer */
1361 for (i = 0; i < IOAT_TEST_SIZE; i++)
1362 src[i] = (u8)i;
1363
1364 /* Start copy, using first DMA channel */
Dan Williamsbc3c7022009-07-28 14:33:42 -07001365 dma_chan = container_of(dma->channels.next, struct dma_chan,
Shannon Nelson43d6e362007-10-16 01:27:39 -07001366 device_node);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001367 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
1368 dev_err(dev, "selftest cannot allocate chan resource\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001369 err = -ENODEV;
1370 goto out;
1371 }
1372
Dan Williamsbc3c7022009-07-28 14:33:42 -07001373 dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
1374 dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001375 flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE;
Dan Williams00367312008-02-02 19:49:57 -07001376 tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +02001377 IOAT_TEST_SIZE, flags);
Shannon Nelson5149fd02007-10-18 03:07:13 -07001378 if (!tx) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001379 dev_err(dev, "Self-test prep failed, disabling\n");
Shannon Nelson5149fd02007-10-18 03:07:13 -07001380 err = -ENODEV;
1381 goto free_resources;
1382 }
1383
Dan Williams7405f742007-01-02 11:10:43 -07001384 async_tx_ack(tx);
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001385 init_completion(&cmp);
Shannon Nelson95218432007-10-18 03:07:15 -07001386 tx->callback = ioat_dma_test_callback;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -07001387 tx->callback_param = &cmp;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001388 cookie = tx->tx_submit(tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001389 if (cookie < 0) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001390 dev_err(dev, "Self-test setup failed, disabling\n");
Shannon Nelson7f2b2912007-10-18 03:07:14 -07001391 err = -ENODEV;
1392 goto free_resources;
1393 }
Dan Williamsbc3c7022009-07-28 14:33:42 -07001394 dma->device_issue_pending(dma_chan);
Dan Williams532d3b12008-12-03 17:16:55 -07001395
Dan Williams0c33e1c2009-03-02 13:31:35 -07001396 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
Chris Leech0bbd5f42006-05-23 17:35:34 -07001397
Dan Williams0c33e1c2009-03-02 13:31:35 -07001398 if (tmo == 0 ||
Dan Williamsbc3c7022009-07-28 14:33:42 -07001399 dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001400 != DMA_SUCCESS) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001401 dev_err(dev, "Self-test copy timed out, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001402 err = -ENODEV;
1403 goto free_resources;
1404 }
1405 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -07001406 dev_err(dev, "Self-test copy failed compare, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -07001407 err = -ENODEV;
1408 goto free_resources;
1409 }
1410
1411free_resources:
Dan Williamsbc3c7022009-07-28 14:33:42 -07001412 dma->device_free_chan_resources(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001413out:
1414 kfree(src);
1415 kfree(dest);
1416 return err;
1417}
1418
Shannon Nelson3e037452007-10-16 01:27:40 -07001419static char ioat_interrupt_style[32] = "msix";
1420module_param_string(ioat_interrupt_style, ioat_interrupt_style,
1421 sizeof(ioat_interrupt_style), 0644);
1422MODULE_PARM_DESC(ioat_interrupt_style,
1423 "set ioat interrupt style: msix (default), "
1424 "msix-single-vector, msi, intx)");
1425
1426/**
1427 * ioat_dma_setup_interrupts - setup interrupt handler
1428 * @device: ioat device
1429 */
1430static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
1431{
1432 struct ioat_dma_chan *ioat_chan;
Dan Williamse6c0b692009-09-08 17:29:44 -07001433 struct pci_dev *pdev = device->pdev;
1434 struct device *dev = &pdev->dev;
1435 struct msix_entry *msix;
1436 int i, j, msixcnt;
1437 int err = -EINVAL;
Shannon Nelson3e037452007-10-16 01:27:40 -07001438 u8 intrctrl = 0;
1439
1440 if (!strcmp(ioat_interrupt_style, "msix"))
1441 goto msix;
1442 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
1443 goto msix_single_vector;
1444 if (!strcmp(ioat_interrupt_style, "msi"))
1445 goto msi;
1446 if (!strcmp(ioat_interrupt_style, "intx"))
1447 goto intx;
Dan Williamse6c0b692009-09-08 17:29:44 -07001448 dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
Shannon Nelson5149fd02007-10-18 03:07:13 -07001449 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001450
1451msix:
1452 /* The number of MSI-X vectors should equal the number of channels */
1453 msixcnt = device->common.chancnt;
1454 for (i = 0; i < msixcnt; i++)
1455 device->msix_entries[i].entry = i;
1456
Dan Williamse6c0b692009-09-08 17:29:44 -07001457 err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
Shannon Nelson3e037452007-10-16 01:27:40 -07001458 if (err < 0)
1459 goto msi;
1460 if (err > 0)
1461 goto msix_single_vector;
1462
1463 for (i = 0; i < msixcnt; i++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001464 msix = &device->msix_entries[i];
Dan Williamsbc3c7022009-07-28 14:33:42 -07001465 ioat_chan = ioat_chan_by_index(device, i);
Dan Williamse6c0b692009-09-08 17:29:44 -07001466 err = devm_request_irq(dev, msix->vector,
1467 ioat_dma_do_interrupt_msix, 0,
1468 "ioat-msix", ioat_chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001469 if (err) {
1470 for (j = 0; j < i; j++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001471 msix = &device->msix_entries[j];
Dan Williamsbc3c7022009-07-28 14:33:42 -07001472 ioat_chan = ioat_chan_by_index(device, j);
Dan Williamse6c0b692009-09-08 17:29:44 -07001473 devm_free_irq(dev, msix->vector, ioat_chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001474 }
1475 goto msix_single_vector;
1476 }
1477 }
1478 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
Shannon Nelson3e037452007-10-16 01:27:40 -07001479 goto done;
1480
1481msix_single_vector:
Dan Williamse6c0b692009-09-08 17:29:44 -07001482 msix = &device->msix_entries[0];
1483 msix->entry = 0;
1484 err = pci_enable_msix(pdev, device->msix_entries, 1);
Shannon Nelson3e037452007-10-16 01:27:40 -07001485 if (err)
1486 goto msi;
1487
Dan Williamse6c0b692009-09-08 17:29:44 -07001488 err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
1489 "ioat-msix", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001490 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001491 pci_disable_msix(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001492 goto msi;
1493 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001494 goto done;
1495
1496msi:
Dan Williamse6c0b692009-09-08 17:29:44 -07001497 err = pci_enable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001498 if (err)
1499 goto intx;
1500
Dan Williamse6c0b692009-09-08 17:29:44 -07001501 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
1502 "ioat-msi", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001503 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001504 pci_disable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001505 goto intx;
1506 }
1507 /*
1508 * CB 1.2 devices need a bit set in configuration space to enable MSI
1509 */
1510 if (device->version == IOAT_VER_1_2) {
1511 u32 dmactrl;
Dan Williamse6c0b692009-09-08 17:29:44 -07001512 pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
Shannon Nelson3e037452007-10-16 01:27:40 -07001513 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
Dan Williamse6c0b692009-09-08 17:29:44 -07001514 pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
Shannon Nelson3e037452007-10-16 01:27:40 -07001515 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001516 goto done;
1517
1518intx:
Dan Williamse6c0b692009-09-08 17:29:44 -07001519 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
1520 IRQF_SHARED, "ioat-intx", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001521 if (err)
1522 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001523
1524done:
1525 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
1526 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
1527 return 0;
1528
1529err_no_irq:
1530 /* Disable all interrupt generation */
1531 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Dan Williamse6c0b692009-09-08 17:29:44 -07001532 dev_err(dev, "no usable interrupts\n");
1533 return err;
Shannon Nelson3e037452007-10-16 01:27:40 -07001534}
1535
Dan Williamse6c0b692009-09-08 17:29:44 -07001536static void ioat_disable_interrupts(struct ioatdma_device *device)
Shannon Nelson3e037452007-10-16 01:27:40 -07001537{
Shannon Nelson3e037452007-10-16 01:27:40 -07001538 /* Disable all interrupt generation */
1539 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -07001540}
1541
Dan Williamsbc3c7022009-07-28 14:33:42 -07001542struct ioatdma_device *
1543ioat_dma_probe(struct pci_dev *pdev, void __iomem *iobase)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001544{
1545 int err;
Dan Williamse6c0b692009-09-08 17:29:44 -07001546 struct device *dev = &pdev->dev;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001547 struct ioatdma_device *device;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001548 struct dma_device *dma;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001549
Dan Williamse6c0b692009-09-08 17:29:44 -07001550 device = devm_kzalloc(dev, sizeof(*device), GFP_KERNEL);
1551 if (!device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001552 err = -ENOMEM;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001553 device->pdev = pdev;
1554 device->reg_base = iobase;
1555 device->version = readb(device->reg_base + IOAT_VER_OFFSET);
Dan Williamsbc3c7022009-07-28 14:33:42 -07001556 dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001557
1558 /* DMA coherent memory pool for DMA descriptor allocations */
1559 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
Shannon Nelson8ab89562007-10-16 01:27:39 -07001560 sizeof(struct ioat_dma_descriptor),
1561 64, 0);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001562 if (!device->dma_pool) {
1563 err = -ENOMEM;
1564 goto err_dma_pool;
1565 }
1566
Shannon Nelson43d6e362007-10-16 01:27:39 -07001567 device->completion_pool = pci_pool_create("completion_pool", pdev,
1568 sizeof(u64), SMP_CACHE_BYTES,
1569 SMP_CACHE_BYTES);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001570 if (!device->completion_pool) {
1571 err = -ENOMEM;
1572 goto err_completion_pool;
1573 }
1574
Dan Williamsbc3c7022009-07-28 14:33:42 -07001575 INIT_LIST_HEAD(&dma->channels);
Shannon Nelson43d6e362007-10-16 01:27:39 -07001576 ioat_dma_enumerate_channels(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001577
Dan Williamsbc3c7022009-07-28 14:33:42 -07001578 dma->device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
1579 dma->device_free_chan_resources = ioat_dma_free_chan_resources;
1580 dma->dev = &pdev->dev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001581
Dan Williamsbc3c7022009-07-28 14:33:42 -07001582 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
1583 dma->device_is_tx_complete = ioat_dma_is_complete;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001584 switch (device->version) {
1585 case IOAT_VER_1_2:
Dan Williamsbc3c7022009-07-28 14:33:42 -07001586 dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1587 dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001588 break;
1589 case IOAT_VER_2_0:
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001590 case IOAT_VER_3_0:
Dan Williamsbc3c7022009-07-28 14:33:42 -07001591 dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
1592 dma->device_issue_pending = ioat2_dma_memcpy_issue_pending;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001593 break;
1594 }
1595
Dan Williamse6c0b692009-09-08 17:29:44 -07001596 dev_err(dev, "Intel(R) I/OAT DMA Engine found,"
Shannon Nelson5149fd02007-10-18 03:07:13 -07001597 " %d channels, device version 0x%02x, driver version %s\n",
Dan Williamsbc3c7022009-07-28 14:33:42 -07001598 dma->chancnt, device->version, IOAT_DMA_VERSION);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001599
Dan Williamsbc3c7022009-07-28 14:33:42 -07001600 if (!dma->chancnt) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001601 dev_err(dev, "Intel(R) I/OAT DMA Engine problem found: "
Maciej Sosnowski8b794b12009-02-26 11:04:54 +01001602 "zero channels detected\n");
1603 goto err_setup_interrupts;
1604 }
1605
Shannon Nelson3e037452007-10-16 01:27:40 -07001606 err = ioat_dma_setup_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001607 if (err)
Shannon Nelson3e037452007-10-16 01:27:40 -07001608 goto err_setup_interrupts;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001609
Shannon Nelson3e037452007-10-16 01:27:40 -07001610 err = ioat_dma_self_test(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001611 if (err)
1612 goto err_self_test;
1613
Dan Williamsbc3c7022009-07-28 14:33:42 -07001614 err = dma_async_device_register(dma);
Dan Williamse6c0b692009-09-08 17:29:44 -07001615 if (err)
1616 goto err_self_test;
Maciej Sosnowski16a37ac2008-07-22 17:30:57 -07001617
Dan Williamse6c0b692009-09-08 17:29:44 -07001618 ioat_set_tcp_copy_break(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001619
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -07001620 if (device->version != IOAT_VER_3_0) {
1621 INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
1622 schedule_delayed_work(&device->work,
1623 WATCHDOG_DELAY);
1624 }
Maciej Sosnowski09177e82008-07-22 10:07:33 -07001625
Shannon Nelson8ab89562007-10-16 01:27:39 -07001626 return device;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001627
1628err_self_test:
Dan Williamse6c0b692009-09-08 17:29:44 -07001629 ioat_disable_interrupts(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001630err_setup_interrupts:
Chris Leech0bbd5f42006-05-23 17:35:34 -07001631 pci_pool_destroy(device->completion_pool);
1632err_completion_pool:
1633 pci_pool_destroy(device->dma_pool);
1634err_dma_pool:
Shannon Nelson8ab89562007-10-16 01:27:39 -07001635 return NULL;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001636}
1637
Shannon Nelson8ab89562007-10-16 01:27:39 -07001638void ioat_dma_remove(struct ioatdma_device *device)
Dan Aloni428ed602007-03-08 09:57:36 -08001639{
Chris Leech0bbd5f42006-05-23 17:35:34 -07001640 struct dma_chan *chan, *_chan;
1641 struct ioat_dma_chan *ioat_chan;
Dan Williamsbc3c7022009-07-28 14:33:42 -07001642 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001643
Maciej Sosnowski2b8a6bf2009-02-26 11:05:07 +01001644 if (device->version != IOAT_VER_3_0)
1645 cancel_delayed_work(&device->work);
1646
Dan Williamse6c0b692009-09-08 17:29:44 -07001647 ioat_disable_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001648
Dan Williamsbc3c7022009-07-28 14:33:42 -07001649 dma_async_device_unregister(dma);
Shannon Nelsondfe22992007-10-18 03:07:13 -07001650
Chris Leech0bbd5f42006-05-23 17:35:34 -07001651 pci_pool_destroy(device->dma_pool);
1652 pci_pool_destroy(device->completion_pool);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001653
Dan Williamsbc3c7022009-07-28 14:33:42 -07001654 list_for_each_entry_safe(chan, _chan, &dma->channels, device_node) {
Chris Leech0bbd5f42006-05-23 17:35:34 -07001655 ioat_chan = to_ioat_chan(chan);
1656 list_del(&chan->device_node);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001657 }
Chris Leech0bbd5f42006-05-23 17:35:34 -07001658}
1659