blob: 5173ba97ba317f4bdc76388d851faba3b98691b9 [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);
Dan Williamsbb320782009-09-08 12:01:14 -0700135 dma->chancnt &= 0x1f; /* bits [4:0] valid */
136 if (dma->chancnt > ARRAY_SIZE(device->idx)) {
137 dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
138 dma->chancnt, ARRAY_SIZE(device->idx));
139 dma->chancnt = ARRAY_SIZE(device->idx);
140 }
Chris Leeche3828812007-03-08 09:57:35 -0800141 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Dan Williamsbb320782009-09-08 12:01:14 -0700142 xfercap_scale &= 0x1f; /* bits [4:0] valid */
Chris Leech0bbd5f42006-05-23 17:35:34 -0700143 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
Dan Williams6df91832009-09-08 12:00:55 -0700144 dev_dbg(dev, "%s: xfercap = %d\n", __func__, xfercap);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700145
Venki Pallipadif371be62008-10-23 15:39:06 -0700146#ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
Dan Williamsf2427e22009-07-28 14:42:38 -0700147 if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
148 dma->chancnt--;
Andy Henroid27471fd2008-10-09 11:45:22 -0700149#endif
Dan Williamsf2427e22009-07-28 14:42:38 -0700150 for (i = 0; i < dma->chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700151 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
Dan Williams5cbafa62009-08-26 13:01:44 -0700152 if (!ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700153 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700154
Dan Williams5cbafa62009-08-26 13:01:44 -0700155 ioat_init_channel(device, &ioat->base, i,
156 ioat1_reset_part2,
157 ioat1_cleanup_tasklet,
158 (unsigned long) ioat);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700159 ioat->xfercap = xfercap;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700160 spin_lock_init(&ioat->desc_lock);
161 INIT_LIST_HEAD(&ioat->free_desc);
162 INIT_LIST_HEAD(&ioat->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700163 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700164 dma->chancnt = i;
165 return i;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700166}
167
Shannon Nelson711924b2007-12-17 16:20:08 -0800168/**
169 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
170 * descriptors to hw
171 * @chan: DMA channel handle
172 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700173static inline void
Dan Williamsdcbc8532009-07-28 14:44:50 -0700174__ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat)
Shannon Nelson711924b2007-12-17 16:20:08 -0800175{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700176 void __iomem *reg_base = ioat->base.reg_base;
177
Dan Williams6df91832009-09-08 12:00:55 -0700178 dev_dbg(to_dev(&ioat->base), "%s: pending: %d\n",
179 __func__, ioat->pending);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700180 ioat->pending = 0;
181 writeb(IOAT_CHANCMD_APPEND, reg_base + IOAT1_CHANCMD_OFFSET);
Shannon Nelson711924b2007-12-17 16:20:08 -0800182}
183
184static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
185{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700186 struct ioat_dma_chan *ioat = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800187
Dan Williamsdcbc8532009-07-28 14:44:50 -0700188 if (ioat->pending > 0) {
189 spin_lock_bh(&ioat->desc_lock);
190 __ioat1_dma_memcpy_issue_pending(ioat);
191 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800192 }
193}
194
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700195/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700196 * ioat1_reset_part2 - reinit the channel after a reset
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700197 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700198static void ioat1_reset_part2(struct work_struct *work)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700199{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700200 struct ioat_chan_common *chan;
201 struct ioat_dma_chan *ioat;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700202 struct ioat_desc_sw *desc;
Dan Williams5cbafa62009-08-26 13:01:44 -0700203 int dmacount;
204 bool start_null = false;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700205
Dan Williamsdcbc8532009-07-28 14:44:50 -0700206 chan = container_of(work, struct ioat_chan_common, work.work);
207 ioat = container_of(chan, struct ioat_dma_chan, base);
208 spin_lock_bh(&chan->cleanup_lock);
209 spin_lock_bh(&ioat->desc_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700210
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700211 *chan->completion = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700212 ioat->pending = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700213
Dan Williams5cbafa62009-08-26 13:01:44 -0700214 /* count the descriptors waiting */
215 dmacount = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700216 if (ioat->used_desc.prev) {
217 desc = to_ioat_desc(ioat->used_desc.prev);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700218 do {
Dan Williams5cbafa62009-08-26 13:01:44 -0700219 dmacount++;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700220 desc = to_ioat_desc(desc->node.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700221 } while (&desc->node != ioat->used_desc.next);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700222 }
223
Dan Williams5cbafa62009-08-26 13:01:44 -0700224 if (dmacount) {
225 /*
226 * write the new starting descriptor address
227 * this puts channel engine into ARMED state
228 */
229 desc = to_ioat_desc(ioat->used_desc.prev);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700230 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700231 chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700232 writel(((u64) desc->txd.phys) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700233 chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700234
Dan Williamsdcbc8532009-07-28 14:44:50 -0700235 writeb(IOAT_CHANCMD_START, chan->reg_base
236 + IOAT_CHANCMD_OFFSET(chan->device->version));
Dan Williams5cbafa62009-08-26 13:01:44 -0700237 } else
238 start_null = true;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700239 spin_unlock_bh(&ioat->desc_lock);
240 spin_unlock_bh(&chan->cleanup_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700241
242 dev_err(to_dev(chan),
243 "chan%d reset - %d descs waiting, %d total desc\n",
244 chan_num(chan), dmacount, ioat->desccount);
245
246 if (start_null)
247 ioat1_dma_start_null_desc(ioat);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700248}
249
250/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700251 * ioat1_reset_channel - restart a channel
Dan Williamsdcbc8532009-07-28 14:44:50 -0700252 * @ioat: IOAT DMA channel handle
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700253 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700254static void ioat1_reset_channel(struct ioat_dma_chan *ioat)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700255{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700256 struct ioat_chan_common *chan = &ioat->base;
257 void __iomem *reg_base = chan->reg_base;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700258 u32 chansts, chanerr;
259
Dan Williamsdcbc8532009-07-28 14:44:50 -0700260 if (!ioat->used_desc.prev)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700261 return;
262
Dan Williams6df91832009-09-08 12:00:55 -0700263 dev_dbg(to_dev(chan), "%s\n", __func__);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700264 chanerr = readl(reg_base + IOAT_CHANERR_OFFSET);
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700265 chansts = *chan->completion & IOAT_CHANSTS_DMA_TRANSFER_STATUS;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700266 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700267 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700268 "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700269 chan_num(chan), chansts, chanerr);
270 writel(chanerr, reg_base + IOAT_CHANERR_OFFSET);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700271 }
272
273 /*
274 * whack it upside the head with a reset
275 * and wait for things to settle out.
276 * force the pending count to a really big negative
277 * to make sure no one forces an issue_pending
278 * while we're waiting.
279 */
280
Dan Williamsdcbc8532009-07-28 14:44:50 -0700281 spin_lock_bh(&ioat->desc_lock);
282 ioat->pending = INT_MIN;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700283 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700284 reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
285 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700286
287 /* schedule the 2nd half instead of sleeping a long time */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700288 schedule_delayed_work(&chan->work, RESET_DELAY);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700289}
290
291/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700292 * ioat1_chan_watchdog - watch for stuck channels
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700293 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700294static void ioat1_chan_watchdog(struct work_struct *work)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700295{
296 struct ioatdma_device *device =
297 container_of(work, struct ioatdma_device, work.work);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700298 struct ioat_dma_chan *ioat;
299 struct ioat_chan_common *chan;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700300 int i;
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700301 u64 completion;
302 u32 completion_low;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700303 unsigned long compl_desc_addr_hw;
304
305 for (i = 0; i < device->common.chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700306 chan = ioat_chan_by_index(device, i);
307 ioat = container_of(chan, struct ioat_dma_chan, base);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700308
Dan Williams5cbafa62009-08-26 13:01:44 -0700309 if (/* have we started processing anything yet */
310 chan->last_completion
311 /* have we completed any since last watchdog cycle? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700312 && (chan->last_completion == chan->watchdog_completion)
Dan Williams5cbafa62009-08-26 13:01:44 -0700313 /* has TCP stuck on one cookie since last watchdog? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700314 && (chan->watchdog_tcp_cookie == chan->watchdog_last_tcp_cookie)
315 && (chan->watchdog_tcp_cookie != chan->completed_cookie)
Dan Williams5cbafa62009-08-26 13:01:44 -0700316 /* is there something in the chain to be processed? */
317 /* CB1 chain always has at least the last one processed */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700318 && (ioat->used_desc.prev != ioat->used_desc.next)
319 && ioat->pending == 0) {
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700320
321 /*
322 * check CHANSTS register for completed
323 * descriptor address.
324 * if it is different than completion writeback,
325 * it is not zero
326 * and it has changed since the last watchdog
327 * we can assume that channel
328 * is still working correctly
329 * and the problem is in completion writeback.
330 * update completion writeback
331 * with actual CHANSTS value
332 * else
333 * try resetting the channel
334 */
335
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700336 /* we need to read the low address first as this
337 * causes the chipset to latch the upper bits
338 * for the subsequent read
339 */
340 completion_low = readl(chan->reg_base +
Dan Williamsdcbc8532009-07-28 14:44:50 -0700341 IOAT_CHANSTS_OFFSET_LOW(chan->device->version));
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700342 completion = readl(chan->reg_base +
Dan Williamsdcbc8532009-07-28 14:44:50 -0700343 IOAT_CHANSTS_OFFSET_HIGH(chan->device->version));
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700344 completion <<= 32;
345 completion |= completion_low;
346 compl_desc_addr_hw = completion &
347 IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700348
349 if ((compl_desc_addr_hw != 0)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700350 && (compl_desc_addr_hw != chan->watchdog_completion)
351 && (compl_desc_addr_hw != chan->last_compl_desc_addr_hw)) {
352 chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700353 *chan->completion = completion;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700354 } else {
Dan Williams5cbafa62009-08-26 13:01:44 -0700355 ioat1_reset_channel(ioat);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700356 chan->watchdog_completion = 0;
357 chan->last_compl_desc_addr_hw = 0;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700358 }
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700359 } else {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700360 chan->last_compl_desc_addr_hw = 0;
361 chan->watchdog_completion = chan->last_completion;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700362 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700363
Dan Williamsdcbc8532009-07-28 14:44:50 -0700364 chan->watchdog_last_tcp_cookie = chan->watchdog_tcp_cookie;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700365 }
366
367 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
368}
369
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800370static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700371{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700372 struct dma_chan *c = tx->chan;
373 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700374 struct ioat_desc_sw *desc = tx_to_ioat_desc(tx);
375 struct ioat_desc_sw *first;
376 struct ioat_desc_sw *chain_tail;
Dan Williams7405f742007-01-02 11:10:43 -0700377 dma_cookie_t cookie;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700378
Dan Williamsdcbc8532009-07-28 14:44:50 -0700379 spin_lock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700380 /* cookie incr and addition to used_list must be atomic */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700381 cookie = c->cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700382 cookie++;
383 if (cookie < 0)
384 cookie = 1;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700385 c->cookie = cookie;
386 tx->cookie = cookie;
Dan Williams6df91832009-09-08 12:00:55 -0700387 dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
Dan Williams7405f742007-01-02 11:10:43 -0700388
389 /* write address into NextDescriptor field of last desc in chain */
Dan Williamsa0587bc2009-07-28 14:44:04 -0700390 first = to_ioat_desc(tx->tx_list.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700391 chain_tail = to_ioat_desc(ioat->used_desc.prev);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700392 /* make descriptor updates globally visible before chaining */
393 wmb();
394 chain_tail->hw->next = first->txd.phys;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700395 list_splice_tail_init(&tx->tx_list, &ioat->used_desc);
Dan Williams6df91832009-09-08 12:00:55 -0700396 dump_desc_dbg(ioat, chain_tail);
397 dump_desc_dbg(ioat, first);
Dan Williams7405f742007-01-02 11:10:43 -0700398
Dan Williamsdcbc8532009-07-28 14:44:50 -0700399 ioat->pending += desc->tx_cnt;
400 if (ioat->pending >= ioat_pending_level)
401 __ioat1_dma_memcpy_issue_pending(ioat);
402 spin_unlock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700403
Dan Williams7405f742007-01-02 11:10:43 -0700404 return cookie;
405}
406
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800407/**
408 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
Dan Williamsdcbc8532009-07-28 14:44:50 -0700409 * @ioat: the channel supplying the memory pool for the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800410 * @flags: allocation flags
411 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700412static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700413ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat, gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700414{
415 struct ioat_dma_descriptor *desc;
416 struct ioat_desc_sw *desc_sw;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700417 struct ioatdma_device *ioatdma_device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700418 dma_addr_t phys;
419
Dan Williamsdcbc8532009-07-28 14:44:50 -0700420 ioatdma_device = ioat->base.device;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700421 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700422 if (unlikely(!desc))
423 return NULL;
424
425 desc_sw = kzalloc(sizeof(*desc_sw), flags);
426 if (unlikely(!desc_sw)) {
Shannon Nelson8ab89562007-10-16 01:27:39 -0700427 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700428 return NULL;
429 }
430
431 memset(desc, 0, sizeof(*desc));
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800432
Dan Williams5cbafa62009-08-26 13:01:44 -0700433 dma_async_tx_descriptor_init(&desc_sw->txd, &ioat->base.common);
434 desc_sw->txd.tx_submit = ioat1_tx_submit;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700435 desc_sw->hw = desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700436 desc_sw->txd.phys = phys;
Dan Williams6df91832009-09-08 12:00:55 -0700437 set_desc_id(desc_sw, -1);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700438
439 return desc_sw;
440}
441
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800442static int ioat_initial_desc_count = 256;
443module_param(ioat_initial_desc_count, int, 0644);
444MODULE_PARM_DESC(ioat_initial_desc_count,
Dan Williams5cbafa62009-08-26 13:01:44 -0700445 "ioat1: initial descriptors per channel (default: 256)");
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800446/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700447 * ioat1_dma_alloc_chan_resources - returns the number of allocated descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800448 * @chan: the channel to be filled out
449 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700450static int ioat1_dma_alloc_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700451{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700452 struct ioat_dma_chan *ioat = to_ioat_chan(c);
453 struct ioat_chan_common *chan = &ioat->base;
Shannon Nelson711924b2007-12-17 16:20:08 -0800454 struct ioat_desc_sw *desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700455 u16 chanctrl;
456 u32 chanerr;
457 int i;
458 LIST_HEAD(tmp_list);
459
Shannon Nelsone4223972007-08-24 23:02:53 -0700460 /* have we already been set up? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700461 if (!list_empty(&ioat->free_desc))
462 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700463
Shannon Nelson43d6e362007-10-16 01:27:39 -0700464 /* Setup register to interrupt and write completion status on error */
Shannon Nelsone4223972007-08-24 23:02:53 -0700465 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
Chris Leech0bbd5f42006-05-23 17:35:34 -0700466 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
467 IOAT_CHANCTRL_ERR_COMPLETION_EN;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700468 writew(chanctrl, chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700469
Dan Williamsdcbc8532009-07-28 14:44:50 -0700470 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700471 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700472 dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
473 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700474 }
475
476 /* Allocate descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800477 for (i = 0; i < ioat_initial_desc_count; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700478 desc = ioat_dma_alloc_descriptor(ioat, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700479 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700480 dev_err(to_dev(chan), "Only %d initial descriptors\n", i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700481 break;
482 }
Dan Williams6df91832009-09-08 12:00:55 -0700483 set_desc_id(desc, i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700484 list_add_tail(&desc->node, &tmp_list);
485 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700486 spin_lock_bh(&ioat->desc_lock);
487 ioat->desccount = i;
488 list_splice(&tmp_list, &ioat->free_desc);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700489 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700490
491 /* allocate a completion writeback area */
492 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700493 chan->completion = pci_pool_alloc(chan->device->completion_pool,
494 GFP_KERNEL, &chan->completion_dma);
495 memset(chan->completion, 0, sizeof(*chan->completion));
496 writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700497 chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700498 writel(((u64) chan->completion_dma) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700499 chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700500
Dan Williamsdcbc8532009-07-28 14:44:50 -0700501 tasklet_enable(&chan->cleanup_task);
Dan Williams5cbafa62009-08-26 13:01:44 -0700502 ioat1_dma_start_null_desc(ioat); /* give chain to dma device */
Dan Williams6df91832009-09-08 12:00:55 -0700503 dev_dbg(to_dev(chan), "%s: allocated %d descriptors\n",
504 __func__, ioat->desccount);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700505 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700506}
507
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800508/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700509 * ioat1_dma_free_chan_resources - release all the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800510 * @chan: the channel to be cleaned
511 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700512static void ioat1_dma_free_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700513{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700514 struct ioat_dma_chan *ioat = to_ioat_chan(c);
515 struct ioat_chan_common *chan = &ioat->base;
516 struct ioatdma_device *ioatdma_device = chan->device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700517 struct ioat_desc_sw *desc, *_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700518 int in_use_descs = 0;
519
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000520 /* Before freeing channel resources first check
521 * if they have been previously allocated for this channel.
522 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700523 if (ioat->desccount == 0)
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000524 return;
525
Dan Williamsdcbc8532009-07-28 14:44:50 -0700526 tasklet_disable(&chan->cleanup_task);
Dan Williams5cbafa62009-08-26 13:01:44 -0700527 ioat1_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700528
Shannon Nelson3e037452007-10-16 01:27:40 -0700529 /* Delay 100ms after reset to allow internal DMA logic to quiesce
530 * before removing DMA descriptor resources.
531 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800532 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700533 chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
Shannon Nelson3e037452007-10-16 01:27:40 -0700534 mdelay(100);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700535
Dan Williamsdcbc8532009-07-28 14:44:50 -0700536 spin_lock_bh(&ioat->desc_lock);
Dan Williams6df91832009-09-08 12:00:55 -0700537 list_for_each_entry_safe(desc, _desc, &ioat->used_desc, node) {
538 dev_dbg(to_dev(chan), "%s: freeing %d from used list\n",
539 __func__, desc_id(desc));
540 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700541 in_use_descs++;
542 list_del(&desc->node);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700543 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700544 desc->txd.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700545 kfree(desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700546 }
547 list_for_each_entry_safe(desc, _desc,
548 &ioat->free_desc, node) {
549 list_del(&desc->node);
550 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
551 desc->txd.phys);
552 kfree(desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700553 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700554 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700555
Shannon Nelson8ab89562007-10-16 01:27:39 -0700556 pci_pool_free(ioatdma_device->completion_pool,
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700557 chan->completion,
558 chan->completion_dma);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700559
560 /* one is ok since we left it on there on purpose */
561 if (in_use_descs > 1)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700562 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700563 in_use_descs - 1);
564
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700565 chan->last_completion = 0;
566 chan->completion_dma = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700567 chan->watchdog_completion = 0;
568 chan->last_compl_desc_addr_hw = 0;
569 chan->watchdog_tcp_cookie = chan->watchdog_last_tcp_cookie = 0;
570 ioat->pending = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700571 ioat->desccount = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700572}
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700573
Shannon Nelson3e037452007-10-16 01:27:40 -0700574/**
Dan Williamsdcbc8532009-07-28 14:44:50 -0700575 * ioat1_dma_get_next_descriptor - return the next available descriptor
576 * @ioat: IOAT DMA channel handle
Shannon Nelson3e037452007-10-16 01:27:40 -0700577 *
578 * Gets the next descriptor from the chain, and must be called with the
579 * channel's desc_lock held. Allocates more descriptors if the channel
580 * has run out.
581 */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700582static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700583ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat)
Shannon Nelson3e037452007-10-16 01:27:40 -0700584{
Shannon Nelson711924b2007-12-17 16:20:08 -0800585 struct ioat_desc_sw *new;
Shannon Nelson3e037452007-10-16 01:27:40 -0700586
Dan Williamsdcbc8532009-07-28 14:44:50 -0700587 if (!list_empty(&ioat->free_desc)) {
588 new = to_ioat_desc(ioat->free_desc.next);
Shannon Nelson3e037452007-10-16 01:27:40 -0700589 list_del(&new->node);
590 } else {
591 /* try to get another desc */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700592 new = ioat_dma_alloc_descriptor(ioat, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800593 if (!new) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700594 dev_err(to_dev(&ioat->base), "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800595 return NULL;
596 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700597 }
Dan Williams6df91832009-09-08 12:00:55 -0700598 dev_dbg(to_dev(&ioat->base), "%s: allocated: %d\n",
599 __func__, desc_id(new));
Shannon Nelson3e037452007-10-16 01:27:40 -0700600 prefetch(new->hw);
601 return new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700602}
603
Dan Williamsbc3c7022009-07-28 14:33:42 -0700604static struct dma_async_tx_descriptor *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700605ioat1_dma_prep_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700606 dma_addr_t dma_src, size_t len, unsigned long flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700607{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700608 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700609 struct ioat_desc_sw *desc;
610 size_t copy;
611 LIST_HEAD(chain);
612 dma_addr_t src = dma_src;
613 dma_addr_t dest = dma_dest;
614 size_t total_len = len;
615 struct ioat_dma_descriptor *hw = NULL;
616 int tx_cnt = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700617
Dan Williamsdcbc8532009-07-28 14:44:50 -0700618 spin_lock_bh(&ioat->desc_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700619 desc = ioat1_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700620 do {
621 if (!desc)
622 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700623
Dan Williamsa0587bc2009-07-28 14:44:04 -0700624 tx_cnt++;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700625 copy = min_t(size_t, len, ioat->xfercap);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700626
627 hw = desc->hw;
628 hw->size = copy;
629 hw->ctl = 0;
630 hw->src_addr = src;
631 hw->dst_addr = dest;
632
633 list_add_tail(&desc->node, &chain);
634
635 len -= copy;
636 dest += copy;
637 src += copy;
638 if (len) {
639 struct ioat_desc_sw *next;
640
641 async_tx_ack(&desc->txd);
Dan Williams5cbafa62009-08-26 13:01:44 -0700642 next = ioat1_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700643 hw->next = next ? next->txd.phys : 0;
Dan Williams6df91832009-09-08 12:00:55 -0700644 dump_desc_dbg(ioat, desc);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700645 desc = next;
646 } else
647 hw->next = 0;
648 } while (len);
649
650 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700651 struct ioat_chan_common *chan = &ioat->base;
652
653 dev_err(to_dev(chan),
Dan Williams5cbafa62009-08-26 13:01:44 -0700654 "chan%d - get_next_desc failed\n", chan_num(chan));
Dan Williamsdcbc8532009-07-28 14:44:50 -0700655 list_splice(&chain, &ioat->free_desc);
656 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800657 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700658 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700659 spin_unlock_bh(&ioat->desc_lock);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700660
661 desc->txd.flags = flags;
662 desc->tx_cnt = tx_cnt;
Dan Williamsa0587bc2009-07-28 14:44:04 -0700663 desc->len = total_len;
664 list_splice(&chain, &desc->txd.tx_list);
665 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
666 hw->ctl_f.compl_write = 1;
Dan Williams6df91832009-09-08 12:00:55 -0700667 dump_desc_dbg(ioat, desc);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700668
669 return &desc->txd;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700670}
671
Dan Williams5cbafa62009-08-26 13:01:44 -0700672static void ioat1_cleanup_tasklet(unsigned long data)
Shannon Nelson3e037452007-10-16 01:27:40 -0700673{
674 struct ioat_dma_chan *chan = (void *)data;
Dan Williams5cbafa62009-08-26 13:01:44 -0700675 ioat1_cleanup(chan);
Shannon Nelson3e037452007-10-16 01:27:40 -0700676 writew(IOAT_CHANCTRL_INT_DISABLE,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700677 chan->base.reg_base + IOAT_CHANCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -0700678}
679
Dan Williams5cbafa62009-08-26 13:01:44 -0700680static void ioat_unmap(struct pci_dev *pdev, dma_addr_t addr, size_t len,
681 int direction, enum dma_ctrl_flags flags, bool dst)
Dan Williamse1d181e2008-07-04 00:13:40 -0700682{
Dan Williams5cbafa62009-08-26 13:01:44 -0700683 if ((dst && (flags & DMA_COMPL_DEST_UNMAP_SINGLE)) ||
684 (!dst && (flags & DMA_COMPL_SRC_UNMAP_SINGLE)))
685 pci_unmap_single(pdev, addr, len, direction);
686 else
687 pci_unmap_page(pdev, addr, len, direction);
Dan Williamse1d181e2008-07-04 00:13:40 -0700688}
689
Dan Williams5cbafa62009-08-26 13:01:44 -0700690
691void ioat_dma_unmap(struct ioat_chan_common *chan, enum dma_ctrl_flags flags,
692 size_t len, struct ioat_dma_descriptor *hw)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700693{
Dan Williams5cbafa62009-08-26 13:01:44 -0700694 struct pci_dev *pdev = chan->device->pdev;
695 size_t offset = len - hw->size;
696
697 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
698 ioat_unmap(pdev, hw->dst_addr - offset, len,
699 PCI_DMA_FROMDEVICE, flags, 1);
700
701 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP))
702 ioat_unmap(pdev, hw->src_addr - offset, len,
703 PCI_DMA_TODEVICE, flags, 0);
704}
705
706unsigned long ioat_get_current_completion(struct ioat_chan_common *chan)
707{
Chris Leech0bbd5f42006-05-23 17:35:34 -0700708 unsigned long phys_complete;
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700709 u64 completion;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700710
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700711 completion = *chan->completion;
712 phys_complete = completion & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700713
Dan Williams6df91832009-09-08 12:00:55 -0700714 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
715 (unsigned long long) phys_complete);
716
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700717 if ((completion & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
Shannon Nelson43d6e362007-10-16 01:27:39 -0700718 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700719 dev_err(to_dev(chan), "Channel halted, chanerr = %x\n",
720 readl(chan->reg_base + IOAT_CHANERR_OFFSET));
Chris Leech0bbd5f42006-05-23 17:35:34 -0700721
722 /* TODO do something to salvage the situation */
723 }
724
Dan Williams5cbafa62009-08-26 13:01:44 -0700725 return phys_complete;
726}
727
728/**
729 * ioat1_cleanup - cleanup up finished descriptors
730 * @chan: ioat channel to be cleaned up
731 */
732static void ioat1_cleanup(struct ioat_dma_chan *ioat)
733{
734 struct ioat_chan_common *chan = &ioat->base;
735 unsigned long phys_complete;
736 struct ioat_desc_sw *desc, *_desc;
737 dma_cookie_t cookie = 0;
738 struct dma_async_tx_descriptor *tx;
739
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700740 prefetch(chan->completion);
Dan Williams5cbafa62009-08-26 13:01:44 -0700741
742 if (!spin_trylock_bh(&chan->cleanup_lock))
743 return;
744
745 phys_complete = ioat_get_current_completion(chan);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700746 if (phys_complete == chan->last_completion) {
747 spin_unlock_bh(&chan->cleanup_lock);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700748 /*
749 * perhaps we're stuck so hard that the watchdog can't go off?
750 * try to catch it after 2 seconds
751 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700752 if (time_after(jiffies,
753 chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
754 ioat1_chan_watchdog(&(chan->device->work.work));
755 chan->last_completion_time = jiffies;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700756 }
757 return;
758 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700759 chan->last_completion_time = jiffies;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700760
761 cookie = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700762 if (!spin_trylock_bh(&ioat->desc_lock)) {
763 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700764 return;
765 }
766
Dan Williams6df91832009-09-08 12:00:55 -0700767 dev_dbg(to_dev(chan), "%s: phys_complete: %lx\n",
768 __func__, phys_complete);
Dan Williams5cbafa62009-08-26 13:01:44 -0700769 list_for_each_entry_safe(desc, _desc, &ioat->used_desc, node) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700770 tx = &desc->txd;
Dan Williams5cbafa62009-08-26 13:01:44 -0700771 /*
772 * Incoming DMA requests may use multiple descriptors,
773 * due to exceeding xfercap, perhaps. If so, only the
774 * last one will have a cookie, and require unmapping.
775 */
Dan Williams6df91832009-09-08 12:00:55 -0700776 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700777 if (tx->cookie) {
778 cookie = tx->cookie;
779 ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
780 if (tx->callback) {
781 tx->callback(tx->callback_param);
782 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800783 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700784 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700785
786 if (tx->phys != phys_complete) {
787 /*
788 * a completed entry, but not the last, so clean
789 * up if the client is done with the descriptor
790 */
791 if (async_tx_test_ack(tx))
792 list_move_tail(&desc->node, &ioat->free_desc);
793 else
794 tx->cookie = 0;
795 } else {
796 /*
797 * last used desc. Do not remove, so we can
798 * append from it, but don't look at it next
799 * time, either
800 */
801 tx->cookie = 0;
802
803 /* TODO check status bits? */
804 break;
805 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700806 }
807
Dan Williamsdcbc8532009-07-28 14:44:50 -0700808 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700809
Dan Williamsdcbc8532009-07-28 14:44:50 -0700810 chan->last_completion = phys_complete;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700811 if (cookie != 0)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700812 chan->completed_cookie = cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700813
Dan Williamsdcbc8532009-07-28 14:44:50 -0700814 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700815}
816
Dan Williamsbc3c7022009-07-28 14:33:42 -0700817static enum dma_status
Dan Williams5cbafa62009-08-26 13:01:44 -0700818ioat1_dma_is_complete(struct dma_chan *c, dma_cookie_t cookie,
819 dma_cookie_t *done, dma_cookie_t *used)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700820{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700821 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700822
Dan Williams5cbafa62009-08-26 13:01:44 -0700823 if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
824 return DMA_SUCCESS;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700825
Dan Williams5cbafa62009-08-26 13:01:44 -0700826 ioat1_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700827
Dan Williams5cbafa62009-08-26 13:01:44 -0700828 return ioat_is_complete(c, cookie, done, used);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700829}
830
Dan Williams5cbafa62009-08-26 13:01:44 -0700831static void ioat1_dma_start_null_desc(struct ioat_dma_chan *ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700832{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700833 struct ioat_chan_common *chan = &ioat->base;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700834 struct ioat_desc_sw *desc;
Dan Williamsc7984f42009-07-28 14:44:04 -0700835 struct ioat_dma_descriptor *hw;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700836
Dan Williamsdcbc8532009-07-28 14:44:50 -0700837 spin_lock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700838
Dan Williams5cbafa62009-08-26 13:01:44 -0700839 desc = ioat1_dma_get_next_descriptor(ioat);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700840
841 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700842 dev_err(to_dev(chan),
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700843 "Unable to start null desc - get next desc failed\n");
Dan Williamsdcbc8532009-07-28 14:44:50 -0700844 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700845 return;
846 }
847
Dan Williamsc7984f42009-07-28 14:44:04 -0700848 hw = desc->hw;
849 hw->ctl = 0;
850 hw->ctl_f.null = 1;
851 hw->ctl_f.int_en = 1;
852 hw->ctl_f.compl_write = 1;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700853 /* set size to non-zero value (channel returns error when size is 0) */
Dan Williamsc7984f42009-07-28 14:44:04 -0700854 hw->size = NULL_DESC_BUFFER_SIZE;
855 hw->src_addr = 0;
856 hw->dst_addr = 0;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700857 async_tx_ack(&desc->txd);
Dan Williams5cbafa62009-08-26 13:01:44 -0700858 hw->next = 0;
859 list_add_tail(&desc->node, &ioat->used_desc);
Dan Williams6df91832009-09-08 12:00:55 -0700860 dump_desc_dbg(ioat, desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700861
Dan Williams5cbafa62009-08-26 13:01:44 -0700862 writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
863 chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
864 writel(((u64) desc->txd.phys) >> 32,
865 chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800866
Dan Williams5cbafa62009-08-26 13:01:44 -0700867 writeb(IOAT_CHANCMD_START, chan->reg_base
868 + IOAT_CHANCMD_OFFSET(chan->device->version));
Dan Williamsdcbc8532009-07-28 14:44:50 -0700869 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700870}
871
872/*
873 * Perform a IOAT transaction to verify the HW works.
874 */
875#define IOAT_TEST_SIZE 2000
876
Shannon Nelson95218432007-10-18 03:07:15 -0700877static void ioat_dma_test_callback(void *dma_async_param)
878{
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700879 struct completion *cmp = dma_async_param;
880
881 complete(cmp);
Shannon Nelson95218432007-10-18 03:07:15 -0700882}
883
Shannon Nelson3e037452007-10-16 01:27:40 -0700884/**
885 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
886 * @device: device to be tested
887 */
888static int ioat_dma_self_test(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700889{
890 int i;
891 u8 *src;
892 u8 *dest;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700893 struct dma_device *dma = &device->common;
894 struct device *dev = &device->pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700895 struct dma_chan *dma_chan;
Shannon Nelson711924b2007-12-17 16:20:08 -0800896 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700897 dma_addr_t dma_dest, dma_src;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700898 dma_cookie_t cookie;
899 int err = 0;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700900 struct completion cmp;
Dan Williams0c33e1c2009-03-02 13:31:35 -0700901 unsigned long tmo;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200902 unsigned long flags;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700903
Christoph Lametere94b1762006-12-06 20:33:17 -0800904 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700905 if (!src)
906 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -0800907 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700908 if (!dest) {
909 kfree(src);
910 return -ENOMEM;
911 }
912
913 /* Fill in src buffer */
914 for (i = 0; i < IOAT_TEST_SIZE; i++)
915 src[i] = (u8)i;
916
917 /* Start copy, using first DMA channel */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700918 dma_chan = container_of(dma->channels.next, struct dma_chan,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700919 device_node);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700920 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
921 dev_err(dev, "selftest cannot allocate chan resource\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700922 err = -ENODEV;
923 goto out;
924 }
925
Dan Williamsbc3c7022009-07-28 14:33:42 -0700926 dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
927 dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
Dan Williamsa6a39ca2009-07-28 14:44:05 -0700928 flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE |
929 DMA_PREP_INTERRUPT;
Dan Williams00367312008-02-02 19:49:57 -0700930 tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200931 IOAT_TEST_SIZE, flags);
Shannon Nelson5149fd02007-10-18 03:07:13 -0700932 if (!tx) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700933 dev_err(dev, "Self-test prep failed, disabling\n");
Shannon Nelson5149fd02007-10-18 03:07:13 -0700934 err = -ENODEV;
935 goto free_resources;
936 }
937
Dan Williams7405f742007-01-02 11:10:43 -0700938 async_tx_ack(tx);
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700939 init_completion(&cmp);
Shannon Nelson95218432007-10-18 03:07:15 -0700940 tx->callback = ioat_dma_test_callback;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700941 tx->callback_param = &cmp;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800942 cookie = tx->tx_submit(tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700943 if (cookie < 0) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700944 dev_err(dev, "Self-test setup failed, disabling\n");
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700945 err = -ENODEV;
946 goto free_resources;
947 }
Dan Williamsbc3c7022009-07-28 14:33:42 -0700948 dma->device_issue_pending(dma_chan);
Dan Williams532d3b12008-12-03 17:16:55 -0700949
Dan Williams0c33e1c2009-03-02 13:31:35 -0700950 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
Chris Leech0bbd5f42006-05-23 17:35:34 -0700951
Dan Williams0c33e1c2009-03-02 13:31:35 -0700952 if (tmo == 0 ||
Dan Williamsbc3c7022009-07-28 14:33:42 -0700953 dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800954 != DMA_SUCCESS) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700955 dev_err(dev, "Self-test copy timed out, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700956 err = -ENODEV;
957 goto free_resources;
958 }
959 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700960 dev_err(dev, "Self-test copy failed compare, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700961 err = -ENODEV;
962 goto free_resources;
963 }
964
965free_resources:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700966 dma->device_free_chan_resources(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700967out:
968 kfree(src);
969 kfree(dest);
970 return err;
971}
972
Shannon Nelson3e037452007-10-16 01:27:40 -0700973static char ioat_interrupt_style[32] = "msix";
974module_param_string(ioat_interrupt_style, ioat_interrupt_style,
975 sizeof(ioat_interrupt_style), 0644);
976MODULE_PARM_DESC(ioat_interrupt_style,
977 "set ioat interrupt style: msix (default), "
978 "msix-single-vector, msi, intx)");
979
980/**
981 * ioat_dma_setup_interrupts - setup interrupt handler
982 * @device: ioat device
983 */
984static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
985{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700986 struct ioat_chan_common *chan;
Dan Williamse6c0b692009-09-08 17:29:44 -0700987 struct pci_dev *pdev = device->pdev;
988 struct device *dev = &pdev->dev;
989 struct msix_entry *msix;
990 int i, j, msixcnt;
991 int err = -EINVAL;
Shannon Nelson3e037452007-10-16 01:27:40 -0700992 u8 intrctrl = 0;
993
994 if (!strcmp(ioat_interrupt_style, "msix"))
995 goto msix;
996 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
997 goto msix_single_vector;
998 if (!strcmp(ioat_interrupt_style, "msi"))
999 goto msi;
1000 if (!strcmp(ioat_interrupt_style, "intx"))
1001 goto intx;
Dan Williamse6c0b692009-09-08 17:29:44 -07001002 dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
Shannon Nelson5149fd02007-10-18 03:07:13 -07001003 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001004
1005msix:
1006 /* The number of MSI-X vectors should equal the number of channels */
1007 msixcnt = device->common.chancnt;
1008 for (i = 0; i < msixcnt; i++)
1009 device->msix_entries[i].entry = i;
1010
Dan Williamse6c0b692009-09-08 17:29:44 -07001011 err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
Shannon Nelson3e037452007-10-16 01:27:40 -07001012 if (err < 0)
1013 goto msi;
1014 if (err > 0)
1015 goto msix_single_vector;
1016
1017 for (i = 0; i < msixcnt; i++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001018 msix = &device->msix_entries[i];
Dan Williamsdcbc8532009-07-28 14:44:50 -07001019 chan = ioat_chan_by_index(device, i);
Dan Williamse6c0b692009-09-08 17:29:44 -07001020 err = devm_request_irq(dev, msix->vector,
1021 ioat_dma_do_interrupt_msix, 0,
Dan Williamsdcbc8532009-07-28 14:44:50 -07001022 "ioat-msix", chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001023 if (err) {
1024 for (j = 0; j < i; j++) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001025 msix = &device->msix_entries[j];
Dan Williamsdcbc8532009-07-28 14:44:50 -07001026 chan = ioat_chan_by_index(device, j);
1027 devm_free_irq(dev, msix->vector, chan);
Shannon Nelson3e037452007-10-16 01:27:40 -07001028 }
1029 goto msix_single_vector;
1030 }
1031 }
1032 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
Shannon Nelson3e037452007-10-16 01:27:40 -07001033 goto done;
1034
1035msix_single_vector:
Dan Williamse6c0b692009-09-08 17:29:44 -07001036 msix = &device->msix_entries[0];
1037 msix->entry = 0;
1038 err = pci_enable_msix(pdev, device->msix_entries, 1);
Shannon Nelson3e037452007-10-16 01:27:40 -07001039 if (err)
1040 goto msi;
1041
Dan Williamse6c0b692009-09-08 17:29:44 -07001042 err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
1043 "ioat-msix", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001044 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001045 pci_disable_msix(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001046 goto msi;
1047 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001048 goto done;
1049
1050msi:
Dan Williamse6c0b692009-09-08 17:29:44 -07001051 err = pci_enable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001052 if (err)
1053 goto intx;
1054
Dan Williamse6c0b692009-09-08 17:29:44 -07001055 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
1056 "ioat-msi", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001057 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001058 pci_disable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -07001059 goto intx;
1060 }
Shannon Nelson3e037452007-10-16 01:27:40 -07001061 goto done;
1062
1063intx:
Dan Williamse6c0b692009-09-08 17:29:44 -07001064 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
1065 IRQF_SHARED, "ioat-intx", device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001066 if (err)
1067 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -07001068
1069done:
Dan Williamsf2427e22009-07-28 14:42:38 -07001070 if (device->intr_quirk)
1071 device->intr_quirk(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001072 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
1073 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
1074 return 0;
1075
1076err_no_irq:
1077 /* Disable all interrupt generation */
1078 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Dan Williamse6c0b692009-09-08 17:29:44 -07001079 dev_err(dev, "no usable interrupts\n");
1080 return err;
Shannon Nelson3e037452007-10-16 01:27:40 -07001081}
1082
Dan Williamse6c0b692009-09-08 17:29:44 -07001083static void ioat_disable_interrupts(struct ioatdma_device *device)
Shannon Nelson3e037452007-10-16 01:27:40 -07001084{
Shannon Nelson3e037452007-10-16 01:27:40 -07001085 /* Disable all interrupt generation */
1086 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -07001087}
1088
Dan Williams5cbafa62009-08-26 13:01:44 -07001089int ioat_probe(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001090{
Dan Williamsf2427e22009-07-28 14:42:38 -07001091 int err = -ENODEV;
1092 struct dma_device *dma = &device->common;
1093 struct pci_dev *pdev = device->pdev;
Dan Williamse6c0b692009-09-08 17:29:44 -07001094 struct device *dev = &pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001095
1096 /* DMA coherent memory pool for DMA descriptor allocations */
1097 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
Shannon Nelson8ab89562007-10-16 01:27:39 -07001098 sizeof(struct ioat_dma_descriptor),
1099 64, 0);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001100 if (!device->dma_pool) {
1101 err = -ENOMEM;
1102 goto err_dma_pool;
1103 }
1104
Shannon Nelson43d6e362007-10-16 01:27:39 -07001105 device->completion_pool = pci_pool_create("completion_pool", pdev,
1106 sizeof(u64), SMP_CACHE_BYTES,
1107 SMP_CACHE_BYTES);
Dan Williams5cbafa62009-08-26 13:01:44 -07001108
Chris Leech0bbd5f42006-05-23 17:35:34 -07001109 if (!device->completion_pool) {
1110 err = -ENOMEM;
1111 goto err_completion_pool;
1112 }
1113
Dan Williams5cbafa62009-08-26 13:01:44 -07001114 device->enumerate_channels(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001115
Dan Williamsf2427e22009-07-28 14:42:38 -07001116 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
Dan Williamsf2427e22009-07-28 14:42:38 -07001117 dma->dev = &pdev->dev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001118
Dan Williamse6c0b692009-09-08 17:29:44 -07001119 dev_err(dev, "Intel(R) I/OAT DMA Engine found,"
Shannon Nelson5149fd02007-10-18 03:07:13 -07001120 " %d channels, device version 0x%02x, driver version %s\n",
Dan Williamsbc3c7022009-07-28 14:33:42 -07001121 dma->chancnt, device->version, IOAT_DMA_VERSION);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001122
Dan Williamsbc3c7022009-07-28 14:33:42 -07001123 if (!dma->chancnt) {
Dan Williamse6c0b692009-09-08 17:29:44 -07001124 dev_err(dev, "Intel(R) I/OAT DMA Engine problem found: "
Maciej Sosnowski8b794b12009-02-26 11:04:54 +01001125 "zero channels detected\n");
1126 goto err_setup_interrupts;
1127 }
1128
Shannon Nelson3e037452007-10-16 01:27:40 -07001129 err = ioat_dma_setup_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001130 if (err)
Shannon Nelson3e037452007-10-16 01:27:40 -07001131 goto err_setup_interrupts;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001132
Shannon Nelson3e037452007-10-16 01:27:40 -07001133 err = ioat_dma_self_test(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001134 if (err)
1135 goto err_self_test;
1136
Dan Williamsf2427e22009-07-28 14:42:38 -07001137 return 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001138
1139err_self_test:
Dan Williamse6c0b692009-09-08 17:29:44 -07001140 ioat_disable_interrupts(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001141err_setup_interrupts:
Chris Leech0bbd5f42006-05-23 17:35:34 -07001142 pci_pool_destroy(device->completion_pool);
1143err_completion_pool:
1144 pci_pool_destroy(device->dma_pool);
1145err_dma_pool:
Dan Williamsf2427e22009-07-28 14:42:38 -07001146 return err;
1147}
1148
Dan Williams5cbafa62009-08-26 13:01:44 -07001149int ioat_register(struct ioatdma_device *device)
Dan Williamsf2427e22009-07-28 14:42:38 -07001150{
1151 int err = dma_async_device_register(&device->common);
1152
1153 if (err) {
1154 ioat_disable_interrupts(device);
1155 pci_pool_destroy(device->completion_pool);
1156 pci_pool_destroy(device->dma_pool);
1157 }
1158
1159 return err;
1160}
1161
1162/* ioat1_intr_quirk - fix up dma ctrl register to enable / disable msi */
1163static void ioat1_intr_quirk(struct ioatdma_device *device)
1164{
1165 struct pci_dev *pdev = device->pdev;
1166 u32 dmactrl;
1167
1168 pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
1169 if (pdev->msi_enabled)
1170 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
1171 else
1172 dmactrl &= ~IOAT_PCI_DMACTRL_MSI_EN;
1173 pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
1174}
1175
1176int ioat1_dma_probe(struct ioatdma_device *device, int dca)
1177{
1178 struct pci_dev *pdev = device->pdev;
1179 struct dma_device *dma;
1180 int err;
1181
1182 device->intr_quirk = ioat1_intr_quirk;
Dan Williams5cbafa62009-08-26 13:01:44 -07001183 device->enumerate_channels = ioat1_enumerate_channels;
Dan Williamsf2427e22009-07-28 14:42:38 -07001184 dma = &device->common;
1185 dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1186 dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
Dan Williams5cbafa62009-08-26 13:01:44 -07001187 dma->device_alloc_chan_resources = ioat1_dma_alloc_chan_resources;
1188 dma->device_free_chan_resources = ioat1_dma_free_chan_resources;
1189 dma->device_is_tx_complete = ioat1_dma_is_complete;
Dan Williamsf2427e22009-07-28 14:42:38 -07001190
1191 err = ioat_probe(device);
1192 if (err)
1193 return err;
1194 ioat_set_tcp_copy_break(4096);
1195 err = ioat_register(device);
1196 if (err)
1197 return err;
1198 if (dca)
1199 device->dca = ioat_dca_init(pdev, device->reg_base);
1200
Dan Williams5cbafa62009-08-26 13:01:44 -07001201 INIT_DELAYED_WORK(&device->work, ioat1_chan_watchdog);
Dan Williamsf2427e22009-07-28 14:42:38 -07001202 schedule_delayed_work(&device->work, WATCHDOG_DELAY);
1203
1204 return err;
1205}
1206
Shannon Nelson8ab89562007-10-16 01:27:39 -07001207void ioat_dma_remove(struct ioatdma_device *device)
Dan Aloni428ed602007-03-08 09:57:36 -08001208{
Dan Williamsbc3c7022009-07-28 14:33:42 -07001209 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001210
Maciej Sosnowski2b8a6bf2009-02-26 11:05:07 +01001211 if (device->version != IOAT_VER_3_0)
1212 cancel_delayed_work(&device->work);
1213
Dan Williamse6c0b692009-09-08 17:29:44 -07001214 ioat_disable_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001215
Dan Williamsbc3c7022009-07-28 14:33:42 -07001216 dma_async_device_unregister(dma);
Shannon Nelsondfe22992007-10-18 03:07:13 -07001217
Chris Leech0bbd5f42006-05-23 17:35:34 -07001218 pci_pool_destroy(device->dma_pool);
1219 pci_pool_destroy(device->completion_pool);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001220
Dan Williamsdcbc8532009-07-28 14:44:50 -07001221 INIT_LIST_HEAD(&dma->channels);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001222}