blob: c524d36d3c2e199db83e5b2a9a8a7b7bf1939a21 [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,
Dan Williams09c8a5b2009-09-08 12:01:49 -0700102 void (*timer_fn)(unsigned long),
103 void (*tasklet)(unsigned long),
104 unsigned long ioat)
Dan Williams5cbafa62009-08-26 13:01:44 -0700105{
106 struct dma_device *dma = &device->common;
107
108 chan->device = device;
109 chan->reg_base = device->reg_base + (0x80 * (idx + 1));
Dan Williams5cbafa62009-08-26 13:01:44 -0700110 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;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700114 init_timer(&chan->timer);
115 chan->timer.function = timer_fn;
116 chan->timer.data = ioat;
117 tasklet_init(&chan->cleanup_task, tasklet, ioat);
Dan Williams5cbafa62009-08-26 13:01:44 -0700118 tasklet_disable(&chan->cleanup_task);
119}
120
Dan Williams09c8a5b2009-09-08 12:01:49 -0700121static void ioat1_timer_event(unsigned long data);
Shannon Nelson3e037452007-10-16 01:27:40 -0700122
123/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700124 * ioat1_dma_enumerate_channels - find and initialize the device's channels
Shannon Nelson3e037452007-10-16 01:27:40 -0700125 * @device: the device to be enumerated
126 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700127static int ioat1_enumerate_channels(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700128{
129 u8 xfercap_scale;
130 u32 xfercap;
131 int i;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700132 struct ioat_dma_chan *ioat;
Dan Williamse6c0b692009-09-08 17:29:44 -0700133 struct device *dev = &device->pdev->dev;
Dan Williamsf2427e22009-07-28 14:42:38 -0700134 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700135
Dan Williamsf2427e22009-07-28 14:42:38 -0700136 INIT_LIST_HEAD(&dma->channels);
137 dma->chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
Dan Williamsbb320782009-09-08 12:01:14 -0700138 dma->chancnt &= 0x1f; /* bits [4:0] valid */
139 if (dma->chancnt > ARRAY_SIZE(device->idx)) {
140 dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
141 dma->chancnt, ARRAY_SIZE(device->idx));
142 dma->chancnt = ARRAY_SIZE(device->idx);
143 }
Chris Leeche3828812007-03-08 09:57:35 -0800144 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Dan Williamsbb320782009-09-08 12:01:14 -0700145 xfercap_scale &= 0x1f; /* bits [4:0] valid */
Chris Leech0bbd5f42006-05-23 17:35:34 -0700146 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
Dan Williams6df91832009-09-08 12:00:55 -0700147 dev_dbg(dev, "%s: xfercap = %d\n", __func__, xfercap);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700148
Venki Pallipadif371be62008-10-23 15:39:06 -0700149#ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
Dan Williamsf2427e22009-07-28 14:42:38 -0700150 if (i7300_idle_platform_probe(NULL, NULL, 1) == 0)
151 dma->chancnt--;
Andy Henroid27471fd2008-10-09 11:45:22 -0700152#endif
Dan Williamsf2427e22009-07-28 14:42:38 -0700153 for (i = 0; i < dma->chancnt; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700154 ioat = devm_kzalloc(dev, sizeof(*ioat), GFP_KERNEL);
Dan Williams5cbafa62009-08-26 13:01:44 -0700155 if (!ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700156 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700157
Dan Williams5cbafa62009-08-26 13:01:44 -0700158 ioat_init_channel(device, &ioat->base, i,
Dan Williams09c8a5b2009-09-08 12:01:49 -0700159 ioat1_timer_event,
Dan Williams5cbafa62009-08-26 13:01:44 -0700160 ioat1_cleanup_tasklet,
161 (unsigned long) ioat);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700162 ioat->xfercap = xfercap;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700163 spin_lock_init(&ioat->desc_lock);
164 INIT_LIST_HEAD(&ioat->free_desc);
165 INIT_LIST_HEAD(&ioat->used_desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700166 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700167 dma->chancnt = i;
168 return i;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700169}
170
Shannon Nelson711924b2007-12-17 16:20:08 -0800171/**
172 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
173 * descriptors to hw
174 * @chan: DMA channel handle
175 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700176static inline void
Dan Williamsdcbc8532009-07-28 14:44:50 -0700177__ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat)
Shannon Nelson711924b2007-12-17 16:20:08 -0800178{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700179 void __iomem *reg_base = ioat->base.reg_base;
180
Dan Williams6df91832009-09-08 12:00:55 -0700181 dev_dbg(to_dev(&ioat->base), "%s: pending: %d\n",
182 __func__, ioat->pending);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700183 ioat->pending = 0;
184 writeb(IOAT_CHANCMD_APPEND, reg_base + IOAT1_CHANCMD_OFFSET);
Shannon Nelson711924b2007-12-17 16:20:08 -0800185}
186
187static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
188{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700189 struct ioat_dma_chan *ioat = to_ioat_chan(chan);
Shannon Nelson711924b2007-12-17 16:20:08 -0800190
Dan Williamsdcbc8532009-07-28 14:44:50 -0700191 if (ioat->pending > 0) {
192 spin_lock_bh(&ioat->desc_lock);
193 __ioat1_dma_memcpy_issue_pending(ioat);
194 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800195 }
196}
197
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700198/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700199 * ioat1_reset_channel - restart a channel
Dan Williamsdcbc8532009-07-28 14:44:50 -0700200 * @ioat: IOAT DMA channel handle
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700201 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700202static void ioat1_reset_channel(struct ioat_dma_chan *ioat)
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700203{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700204 struct ioat_chan_common *chan = &ioat->base;
205 void __iomem *reg_base = chan->reg_base;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700206 u32 chansts, chanerr;
207
Dan Williams09c8a5b2009-09-08 12:01:49 -0700208 dev_warn(to_dev(chan), "reset\n");
Dan Williamsdcbc8532009-07-28 14:44:50 -0700209 chanerr = readl(reg_base + IOAT_CHANERR_OFFSET);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700210 chansts = *chan->completion & IOAT_CHANSTS_STATUS;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700211 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700212 dev_err(to_dev(chan),
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700213 "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
Dan Williamsdcbc8532009-07-28 14:44:50 -0700214 chan_num(chan), chansts, chanerr);
215 writel(chanerr, reg_base + IOAT_CHANERR_OFFSET);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700216 }
217
218 /*
219 * whack it upside the head with a reset
220 * and wait for things to settle out.
221 * force the pending count to a really big negative
222 * to make sure no one forces an issue_pending
223 * while we're waiting.
224 */
225
Dan Williamsdcbc8532009-07-28 14:44:50 -0700226 ioat->pending = INT_MIN;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700227 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700228 reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
Dan Williams09c8a5b2009-09-08 12:01:49 -0700229 set_bit(IOAT_RESET_PENDING, &chan->state);
230 mod_timer(&chan->timer, jiffies + RESET_DELAY);
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700231}
232
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800233static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
Dan Williams7405f742007-01-02 11:10:43 -0700234{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700235 struct dma_chan *c = tx->chan;
236 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700237 struct ioat_desc_sw *desc = tx_to_ioat_desc(tx);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700238 struct ioat_chan_common *chan = &ioat->base;
Dan Williamsa0587bc2009-07-28 14:44:04 -0700239 struct ioat_desc_sw *first;
240 struct ioat_desc_sw *chain_tail;
Dan Williams7405f742007-01-02 11:10:43 -0700241 dma_cookie_t cookie;
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700242
Dan Williamsdcbc8532009-07-28 14:44:50 -0700243 spin_lock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700244 /* cookie incr and addition to used_list must be atomic */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700245 cookie = c->cookie;
Dan Williams7405f742007-01-02 11:10:43 -0700246 cookie++;
247 if (cookie < 0)
248 cookie = 1;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700249 c->cookie = cookie;
250 tx->cookie = cookie;
Dan Williams6df91832009-09-08 12:00:55 -0700251 dev_dbg(to_dev(&ioat->base), "%s: cookie: %d\n", __func__, cookie);
Dan Williams7405f742007-01-02 11:10:43 -0700252
253 /* write address into NextDescriptor field of last desc in chain */
Dan Williamsea259682009-09-08 17:53:02 -0700254 first = to_ioat_desc(desc->tx_list.next);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700255 chain_tail = to_ioat_desc(ioat->used_desc.prev);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700256 /* make descriptor updates globally visible before chaining */
257 wmb();
258 chain_tail->hw->next = first->txd.phys;
Dan Williamsea259682009-09-08 17:53:02 -0700259 list_splice_tail_init(&desc->tx_list, &ioat->used_desc);
Dan Williams6df91832009-09-08 12:00:55 -0700260 dump_desc_dbg(ioat, chain_tail);
261 dump_desc_dbg(ioat, first);
Dan Williams7405f742007-01-02 11:10:43 -0700262
Dan Williams09c8a5b2009-09-08 12:01:49 -0700263 if (!test_and_set_bit(IOAT_COMPLETION_PENDING, &chan->state))
264 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
265
Dan Williams5669e312009-09-08 17:42:56 -0700266 ioat->active += desc->hw->tx_cnt;
Dan Williamsad643f52009-09-08 12:01:38 -0700267 ioat->pending += desc->hw->tx_cnt;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700268 if (ioat->pending >= ioat_pending_level)
269 __ioat1_dma_memcpy_issue_pending(ioat);
270 spin_unlock_bh(&ioat->desc_lock);
Dan Williams7405f742007-01-02 11:10:43 -0700271
Dan Williams7405f742007-01-02 11:10:43 -0700272 return cookie;
273}
274
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800275/**
276 * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
Dan Williamsdcbc8532009-07-28 14:44:50 -0700277 * @ioat: the channel supplying the memory pool for the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800278 * @flags: allocation flags
279 */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700280static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700281ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat, gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700282{
283 struct ioat_dma_descriptor *desc;
284 struct ioat_desc_sw *desc_sw;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700285 struct ioatdma_device *ioatdma_device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700286 dma_addr_t phys;
287
Dan Williamsdcbc8532009-07-28 14:44:50 -0700288 ioatdma_device = ioat->base.device;
Shannon Nelson8ab89562007-10-16 01:27:39 -0700289 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700290 if (unlikely(!desc))
291 return NULL;
292
293 desc_sw = kzalloc(sizeof(*desc_sw), flags);
294 if (unlikely(!desc_sw)) {
Shannon Nelson8ab89562007-10-16 01:27:39 -0700295 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700296 return NULL;
297 }
298
299 memset(desc, 0, sizeof(*desc));
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800300
Dan Williamsea259682009-09-08 17:53:02 -0700301 INIT_LIST_HEAD(&desc_sw->tx_list);
Dan Williams5cbafa62009-08-26 13:01:44 -0700302 dma_async_tx_descriptor_init(&desc_sw->txd, &ioat->base.common);
303 desc_sw->txd.tx_submit = ioat1_tx_submit;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700304 desc_sw->hw = desc;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700305 desc_sw->txd.phys = phys;
Dan Williams6df91832009-09-08 12:00:55 -0700306 set_desc_id(desc_sw, -1);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700307
308 return desc_sw;
309}
310
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800311static int ioat_initial_desc_count = 256;
312module_param(ioat_initial_desc_count, int, 0644);
313MODULE_PARM_DESC(ioat_initial_desc_count,
Dan Williams5cbafa62009-08-26 13:01:44 -0700314 "ioat1: initial descriptors per channel (default: 256)");
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800315/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700316 * ioat1_dma_alloc_chan_resources - returns the number of allocated descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800317 * @chan: the channel to be filled out
318 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700319static int ioat1_dma_alloc_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700320{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700321 struct ioat_dma_chan *ioat = to_ioat_chan(c);
322 struct ioat_chan_common *chan = &ioat->base;
Shannon Nelson711924b2007-12-17 16:20:08 -0800323 struct ioat_desc_sw *desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700324 u32 chanerr;
325 int i;
326 LIST_HEAD(tmp_list);
327
Shannon Nelsone4223972007-08-24 23:02:53 -0700328 /* have we already been set up? */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700329 if (!list_empty(&ioat->free_desc))
330 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700331
Shannon Nelson43d6e362007-10-16 01:27:39 -0700332 /* Setup register to interrupt and write completion status on error */
Dan Williamsf6ab95b2009-09-08 12:01:21 -0700333 writew(IOAT_CHANCTRL_RUN, chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700334
Dan Williamsdcbc8532009-07-28 14:44:50 -0700335 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700336 if (chanerr) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700337 dev_err(to_dev(chan), "CHANERR = %x, clearing\n", chanerr);
338 writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700339 }
340
341 /* Allocate descriptors */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800342 for (i = 0; i < ioat_initial_desc_count; i++) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700343 desc = ioat_dma_alloc_descriptor(ioat, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700344 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700345 dev_err(to_dev(chan), "Only %d initial descriptors\n", i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700346 break;
347 }
Dan Williams6df91832009-09-08 12:00:55 -0700348 set_desc_id(desc, i);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700349 list_add_tail(&desc->node, &tmp_list);
350 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700351 spin_lock_bh(&ioat->desc_lock);
352 ioat->desccount = i;
353 list_splice(&tmp_list, &ioat->free_desc);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700354 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700355
356 /* allocate a completion writeback area */
357 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700358 chan->completion = pci_pool_alloc(chan->device->completion_pool,
359 GFP_KERNEL, &chan->completion_dma);
360 memset(chan->completion, 0, sizeof(*chan->completion));
361 writel(((u64) chan->completion_dma) & 0x00000000FFFFFFFF,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700362 chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700363 writel(((u64) chan->completion_dma) >> 32,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700364 chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700365
Dan Williamsdcbc8532009-07-28 14:44:50 -0700366 tasklet_enable(&chan->cleanup_task);
Dan Williams5cbafa62009-08-26 13:01:44 -0700367 ioat1_dma_start_null_desc(ioat); /* give chain to dma device */
Dan Williams6df91832009-09-08 12:00:55 -0700368 dev_dbg(to_dev(chan), "%s: allocated %d descriptors\n",
369 __func__, ioat->desccount);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700370 return ioat->desccount;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700371}
372
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800373/**
Dan Williams5cbafa62009-08-26 13:01:44 -0700374 * ioat1_dma_free_chan_resources - release all the descriptors
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800375 * @chan: the channel to be cleaned
376 */
Dan Williams5cbafa62009-08-26 13:01:44 -0700377static void ioat1_dma_free_chan_resources(struct dma_chan *c)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700378{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700379 struct ioat_dma_chan *ioat = to_ioat_chan(c);
380 struct ioat_chan_common *chan = &ioat->base;
381 struct ioatdma_device *ioatdma_device = chan->device;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700382 struct ioat_desc_sw *desc, *_desc;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700383 int in_use_descs = 0;
384
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000385 /* Before freeing channel resources first check
386 * if they have been previously allocated for this channel.
387 */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700388 if (ioat->desccount == 0)
Maciej Sosnowskic3d4f442008-11-07 01:45:52 +0000389 return;
390
Dan Williamsdcbc8532009-07-28 14:44:50 -0700391 tasklet_disable(&chan->cleanup_task);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700392 del_timer_sync(&chan->timer);
Dan Williams5cbafa62009-08-26 13:01:44 -0700393 ioat1_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700394
Shannon Nelson3e037452007-10-16 01:27:40 -0700395 /* Delay 100ms after reset to allow internal DMA logic to quiesce
396 * before removing DMA descriptor resources.
397 */
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800398 writeb(IOAT_CHANCMD_RESET,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700399 chan->reg_base + IOAT_CHANCMD_OFFSET(chan->device->version));
Shannon Nelson3e037452007-10-16 01:27:40 -0700400 mdelay(100);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700401
Dan Williamsdcbc8532009-07-28 14:44:50 -0700402 spin_lock_bh(&ioat->desc_lock);
Dan Williams6df91832009-09-08 12:00:55 -0700403 list_for_each_entry_safe(desc, _desc, &ioat->used_desc, node) {
404 dev_dbg(to_dev(chan), "%s: freeing %d from used list\n",
405 __func__, desc_id(desc));
406 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700407 in_use_descs++;
408 list_del(&desc->node);
Shannon Nelson8ab89562007-10-16 01:27:39 -0700409 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700410 desc->txd.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700411 kfree(desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700412 }
413 list_for_each_entry_safe(desc, _desc,
414 &ioat->free_desc, node) {
415 list_del(&desc->node);
416 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
417 desc->txd.phys);
418 kfree(desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700419 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700420 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700421
Shannon Nelson8ab89562007-10-16 01:27:39 -0700422 pci_pool_free(ioatdma_device->completion_pool,
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700423 chan->completion,
424 chan->completion_dma);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700425
426 /* one is ok since we left it on there on purpose */
427 if (in_use_descs > 1)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700428 dev_err(to_dev(chan), "Freeing %d in use descriptors!\n",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700429 in_use_descs - 1);
430
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700431 chan->last_completion = 0;
432 chan->completion_dma = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700433 ioat->pending = 0;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700434 ioat->desccount = 0;
Shannon Nelson3e037452007-10-16 01:27:40 -0700435}
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700436
Shannon Nelson3e037452007-10-16 01:27:40 -0700437/**
Dan Williamsdcbc8532009-07-28 14:44:50 -0700438 * ioat1_dma_get_next_descriptor - return the next available descriptor
439 * @ioat: IOAT DMA channel handle
Shannon Nelson3e037452007-10-16 01:27:40 -0700440 *
441 * Gets the next descriptor from the chain, and must be called with the
442 * channel's desc_lock held. Allocates more descriptors if the channel
443 * has run out.
444 */
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700445static struct ioat_desc_sw *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700446ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat)
Shannon Nelson3e037452007-10-16 01:27:40 -0700447{
Shannon Nelson711924b2007-12-17 16:20:08 -0800448 struct ioat_desc_sw *new;
Shannon Nelson3e037452007-10-16 01:27:40 -0700449
Dan Williamsdcbc8532009-07-28 14:44:50 -0700450 if (!list_empty(&ioat->free_desc)) {
451 new = to_ioat_desc(ioat->free_desc.next);
Shannon Nelson3e037452007-10-16 01:27:40 -0700452 list_del(&new->node);
453 } else {
454 /* try to get another desc */
Dan Williamsdcbc8532009-07-28 14:44:50 -0700455 new = ioat_dma_alloc_descriptor(ioat, GFP_ATOMIC);
Shannon Nelson711924b2007-12-17 16:20:08 -0800456 if (!new) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700457 dev_err(to_dev(&ioat->base), "alloc failed\n");
Shannon Nelson711924b2007-12-17 16:20:08 -0800458 return NULL;
459 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700460 }
Dan Williams6df91832009-09-08 12:00:55 -0700461 dev_dbg(to_dev(&ioat->base), "%s: allocated: %d\n",
462 __func__, desc_id(new));
Shannon Nelson3e037452007-10-16 01:27:40 -0700463 prefetch(new->hw);
464 return new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700465}
466
Dan Williamsbc3c7022009-07-28 14:33:42 -0700467static struct dma_async_tx_descriptor *
Dan Williamsdcbc8532009-07-28 14:44:50 -0700468ioat1_dma_prep_memcpy(struct dma_chan *c, dma_addr_t dma_dest,
Dan Williamsbc3c7022009-07-28 14:33:42 -0700469 dma_addr_t dma_src, size_t len, unsigned long flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700470{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700471 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700472 struct ioat_desc_sw *desc;
473 size_t copy;
474 LIST_HEAD(chain);
475 dma_addr_t src = dma_src;
476 dma_addr_t dest = dma_dest;
477 size_t total_len = len;
478 struct ioat_dma_descriptor *hw = NULL;
479 int tx_cnt = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700480
Dan Williamsdcbc8532009-07-28 14:44:50 -0700481 spin_lock_bh(&ioat->desc_lock);
Dan Williams5cbafa62009-08-26 13:01:44 -0700482 desc = ioat1_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700483 do {
484 if (!desc)
485 break;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700486
Dan Williamsa0587bc2009-07-28 14:44:04 -0700487 tx_cnt++;
Dan Williamsdcbc8532009-07-28 14:44:50 -0700488 copy = min_t(size_t, len, ioat->xfercap);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700489
490 hw = desc->hw;
491 hw->size = copy;
492 hw->ctl = 0;
493 hw->src_addr = src;
494 hw->dst_addr = dest;
495
496 list_add_tail(&desc->node, &chain);
497
498 len -= copy;
499 dest += copy;
500 src += copy;
501 if (len) {
502 struct ioat_desc_sw *next;
503
504 async_tx_ack(&desc->txd);
Dan Williams5cbafa62009-08-26 13:01:44 -0700505 next = ioat1_dma_get_next_descriptor(ioat);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700506 hw->next = next ? next->txd.phys : 0;
Dan Williams6df91832009-09-08 12:00:55 -0700507 dump_desc_dbg(ioat, desc);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700508 desc = next;
509 } else
510 hw->next = 0;
511 } while (len);
512
513 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700514 struct ioat_chan_common *chan = &ioat->base;
515
516 dev_err(to_dev(chan),
Dan Williams5cbafa62009-08-26 13:01:44 -0700517 "chan%d - get_next_desc failed\n", chan_num(chan));
Dan Williamsdcbc8532009-07-28 14:44:50 -0700518 list_splice(&chain, &ioat->free_desc);
519 spin_unlock_bh(&ioat->desc_lock);
Shannon Nelson711924b2007-12-17 16:20:08 -0800520 return NULL;
Maciej Sosnowski09177e82008-07-22 10:07:33 -0700521 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700522 spin_unlock_bh(&ioat->desc_lock);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700523
524 desc->txd.flags = flags;
Dan Williamsa0587bc2009-07-28 14:44:04 -0700525 desc->len = total_len;
Dan Williamsea259682009-09-08 17:53:02 -0700526 list_splice(&chain, &desc->tx_list);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700527 hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
528 hw->ctl_f.compl_write = 1;
Dan Williamsad643f52009-09-08 12:01:38 -0700529 hw->tx_cnt = tx_cnt;
Dan Williams6df91832009-09-08 12:00:55 -0700530 dump_desc_dbg(ioat, desc);
Dan Williamsa0587bc2009-07-28 14:44:04 -0700531
532 return &desc->txd;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700533}
534
Dan Williams5cbafa62009-08-26 13:01:44 -0700535static void ioat1_cleanup_tasklet(unsigned long data)
Shannon Nelson3e037452007-10-16 01:27:40 -0700536{
537 struct ioat_dma_chan *chan = (void *)data;
Dan Williamsf6ab95b2009-09-08 12:01:21 -0700538
Dan Williams5cbafa62009-08-26 13:01:44 -0700539 ioat1_cleanup(chan);
Dan Williamsf6ab95b2009-09-08 12:01:21 -0700540 writew(IOAT_CHANCTRL_RUN, chan->base.reg_base + IOAT_CHANCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -0700541}
542
Dan Williams5cbafa62009-08-26 13:01:44 -0700543void ioat_dma_unmap(struct ioat_chan_common *chan, enum dma_ctrl_flags flags,
544 size_t len, struct ioat_dma_descriptor *hw)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700545{
Dan Williams5cbafa62009-08-26 13:01:44 -0700546 struct pci_dev *pdev = chan->device->pdev;
547 size_t offset = len - hw->size;
548
549 if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
550 ioat_unmap(pdev, hw->dst_addr - offset, len,
551 PCI_DMA_FROMDEVICE, flags, 1);
552
553 if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP))
554 ioat_unmap(pdev, hw->src_addr - offset, len,
555 PCI_DMA_TODEVICE, flags, 0);
556}
557
558unsigned long ioat_get_current_completion(struct ioat_chan_common *chan)
559{
Chris Leech0bbd5f42006-05-23 17:35:34 -0700560 unsigned long phys_complete;
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700561 u64 completion;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700562
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700563 completion = *chan->completion;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700564 phys_complete = ioat_chansts_to_addr(completion);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700565
Dan Williams6df91832009-09-08 12:00:55 -0700566 dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
567 (unsigned long long) phys_complete);
568
Dan Williams09c8a5b2009-09-08 12:01:49 -0700569 if (is_ioat_halted(completion)) {
570 u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700571 dev_err(to_dev(chan), "Channel halted, chanerr = %x\n",
Dan Williams09c8a5b2009-09-08 12:01:49 -0700572 chanerr);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700573
574 /* TODO do something to salvage the situation */
575 }
576
Dan Williams5cbafa62009-08-26 13:01:44 -0700577 return phys_complete;
578}
579
Dan Williams09c8a5b2009-09-08 12:01:49 -0700580bool ioat_cleanup_preamble(struct ioat_chan_common *chan,
581 unsigned long *phys_complete)
582{
583 *phys_complete = ioat_get_current_completion(chan);
584 if (*phys_complete == chan->last_completion)
585 return false;
586 clear_bit(IOAT_COMPLETION_ACK, &chan->state);
587 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
588
589 return true;
590}
591
592static void __cleanup(struct ioat_dma_chan *ioat, unsigned long phys_complete)
Dan Williams5cbafa62009-08-26 13:01:44 -0700593{
594 struct ioat_chan_common *chan = &ioat->base;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700595 struct list_head *_desc, *n;
Dan Williams5cbafa62009-08-26 13:01:44 -0700596 struct dma_async_tx_descriptor *tx;
597
Dan Williams6df91832009-09-08 12:00:55 -0700598 dev_dbg(to_dev(chan), "%s: phys_complete: %lx\n",
599 __func__, phys_complete);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700600 list_for_each_safe(_desc, n, &ioat->used_desc) {
601 struct ioat_desc_sw *desc;
602
603 prefetch(n);
604 desc = list_entry(_desc, typeof(*desc), node);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700605 tx = &desc->txd;
Dan Williams5cbafa62009-08-26 13:01:44 -0700606 /*
607 * Incoming DMA requests may use multiple descriptors,
608 * due to exceeding xfercap, perhaps. If so, only the
609 * last one will have a cookie, and require unmapping.
610 */
Dan Williams6df91832009-09-08 12:00:55 -0700611 dump_desc_dbg(ioat, desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700612 if (tx->cookie) {
Dan Williams09c8a5b2009-09-08 12:01:49 -0700613 chan->completed_cookie = tx->cookie;
614 tx->cookie = 0;
Dan Williams5cbafa62009-08-26 13:01:44 -0700615 ioat_dma_unmap(chan, tx->flags, desc->len, desc->hw);
Dan Williams5669e312009-09-08 17:42:56 -0700616 ioat->active -= desc->hw->tx_cnt;
Dan Williams5cbafa62009-08-26 13:01:44 -0700617 if (tx->callback) {
618 tx->callback(tx->callback_param);
619 tx->callback = NULL;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800620 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700621 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700622
623 if (tx->phys != phys_complete) {
624 /*
625 * a completed entry, but not the last, so clean
626 * up if the client is done with the descriptor
627 */
628 if (async_tx_test_ack(tx))
629 list_move_tail(&desc->node, &ioat->free_desc);
Dan Williams5cbafa62009-08-26 13:01:44 -0700630 } else {
631 /*
632 * last used desc. Do not remove, so we can
Dan Williams09c8a5b2009-09-08 12:01:49 -0700633 * append from it.
Dan Williams5cbafa62009-08-26 13:01:44 -0700634 */
Dan Williams09c8a5b2009-09-08 12:01:49 -0700635
636 /* if nothing else is pending, cancel the
637 * completion timeout
638 */
639 if (n == &ioat->used_desc) {
640 dev_dbg(to_dev(chan),
641 "%s cancel completion timeout\n",
642 __func__);
643 clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
644 }
Dan Williams5cbafa62009-08-26 13:01:44 -0700645
646 /* TODO check status bits? */
647 break;
648 }
Chris Leech0bbd5f42006-05-23 17:35:34 -0700649 }
650
Dan Williamsdcbc8532009-07-28 14:44:50 -0700651 chan->last_completion = phys_complete;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700652}
Chris Leech0bbd5f42006-05-23 17:35:34 -0700653
Dan Williams09c8a5b2009-09-08 12:01:49 -0700654/**
655 * ioat1_cleanup - cleanup up finished descriptors
656 * @chan: ioat channel to be cleaned up
657 *
658 * To prevent lock contention we defer cleanup when the locks are
659 * contended with a terminal timeout that forces cleanup and catches
660 * completion notification errors.
661 */
662static void ioat1_cleanup(struct ioat_dma_chan *ioat)
663{
664 struct ioat_chan_common *chan = &ioat->base;
665 unsigned long phys_complete;
666
667 prefetch(chan->completion);
668
669 if (!spin_trylock_bh(&chan->cleanup_lock))
670 return;
671
672 if (!ioat_cleanup_preamble(chan, &phys_complete)) {
673 spin_unlock_bh(&chan->cleanup_lock);
674 return;
675 }
676
677 if (!spin_trylock_bh(&ioat->desc_lock)) {
678 spin_unlock_bh(&chan->cleanup_lock);
679 return;
680 }
681
682 __cleanup(ioat, phys_complete);
683
684 spin_unlock_bh(&ioat->desc_lock);
685 spin_unlock_bh(&chan->cleanup_lock);
686}
687
688static void ioat1_timer_event(unsigned long data)
689{
690 struct ioat_dma_chan *ioat = (void *) data;
691 struct ioat_chan_common *chan = &ioat->base;
692
693 dev_dbg(to_dev(chan), "%s: state: %lx\n", __func__, chan->state);
694
695 spin_lock_bh(&chan->cleanup_lock);
696 if (test_and_clear_bit(IOAT_RESET_PENDING, &chan->state)) {
697 struct ioat_desc_sw *desc;
698
699 spin_lock_bh(&ioat->desc_lock);
700
701 /* restart active descriptors */
702 desc = to_ioat_desc(ioat->used_desc.prev);
703 ioat_set_chainaddr(ioat, desc->txd.phys);
704 ioat_start(chan);
705
706 ioat->pending = 0;
707 set_bit(IOAT_COMPLETION_PENDING, &chan->state);
708 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
709 spin_unlock_bh(&ioat->desc_lock);
710 } else if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
711 unsigned long phys_complete;
712
713 spin_lock_bh(&ioat->desc_lock);
714 /* if we haven't made progress and we have already
715 * acknowledged a pending completion once, then be more
716 * forceful with a restart
717 */
718 if (ioat_cleanup_preamble(chan, &phys_complete))
719 __cleanup(ioat, phys_complete);
720 else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
721 ioat1_reset_channel(ioat);
722 else {
723 u64 status = ioat_chansts(chan);
724
725 /* manually update the last completion address */
726 if (ioat_chansts_to_addr(status) != 0)
727 *chan->completion = status;
728
729 set_bit(IOAT_COMPLETION_ACK, &chan->state);
730 mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
731 }
732 spin_unlock_bh(&ioat->desc_lock);
733 }
Dan Williamsdcbc8532009-07-28 14:44:50 -0700734 spin_unlock_bh(&chan->cleanup_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700735}
736
Dan Williamsbc3c7022009-07-28 14:33:42 -0700737static enum dma_status
Dan Williams5cbafa62009-08-26 13:01:44 -0700738ioat1_dma_is_complete(struct dma_chan *c, dma_cookie_t cookie,
739 dma_cookie_t *done, dma_cookie_t *used)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700740{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700741 struct ioat_dma_chan *ioat = to_ioat_chan(c);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700742
Dan Williams5cbafa62009-08-26 13:01:44 -0700743 if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
744 return DMA_SUCCESS;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700745
Dan Williams5cbafa62009-08-26 13:01:44 -0700746 ioat1_cleanup(ioat);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700747
Dan Williams5cbafa62009-08-26 13:01:44 -0700748 return ioat_is_complete(c, cookie, done, used);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700749}
750
Dan Williams5cbafa62009-08-26 13:01:44 -0700751static void ioat1_dma_start_null_desc(struct ioat_dma_chan *ioat)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700752{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700753 struct ioat_chan_common *chan = &ioat->base;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700754 struct ioat_desc_sw *desc;
Dan Williamsc7984f42009-07-28 14:44:04 -0700755 struct ioat_dma_descriptor *hw;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700756
Dan Williamsdcbc8532009-07-28 14:44:50 -0700757 spin_lock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700758
Dan Williams5cbafa62009-08-26 13:01:44 -0700759 desc = ioat1_dma_get_next_descriptor(ioat);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700760
761 if (!desc) {
Dan Williamsdcbc8532009-07-28 14:44:50 -0700762 dev_err(to_dev(chan),
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700763 "Unable to start null desc - get next desc failed\n");
Dan Williamsdcbc8532009-07-28 14:44:50 -0700764 spin_unlock_bh(&ioat->desc_lock);
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700765 return;
766 }
767
Dan Williamsc7984f42009-07-28 14:44:04 -0700768 hw = desc->hw;
769 hw->ctl = 0;
770 hw->ctl_f.null = 1;
771 hw->ctl_f.int_en = 1;
772 hw->ctl_f.compl_write = 1;
Maciej Sosnowski7f1b3582008-07-22 17:30:57 -0700773 /* set size to non-zero value (channel returns error when size is 0) */
Dan Williamsc7984f42009-07-28 14:44:04 -0700774 hw->size = NULL_DESC_BUFFER_SIZE;
775 hw->src_addr = 0;
776 hw->dst_addr = 0;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700777 async_tx_ack(&desc->txd);
Dan Williams5cbafa62009-08-26 13:01:44 -0700778 hw->next = 0;
779 list_add_tail(&desc->node, &ioat->used_desc);
Dan Williams6df91832009-09-08 12:00:55 -0700780 dump_desc_dbg(ioat, desc);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700781
Dan Williams09c8a5b2009-09-08 12:01:49 -0700782 ioat_set_chainaddr(ioat, desc->txd.phys);
783 ioat_start(chan);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700784 spin_unlock_bh(&ioat->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700785}
786
787/*
788 * Perform a IOAT transaction to verify the HW works.
789 */
790#define IOAT_TEST_SIZE 2000
791
Dan Williams345d8522009-09-08 12:01:30 -0700792static void __devinit ioat_dma_test_callback(void *dma_async_param)
Shannon Nelson95218432007-10-18 03:07:15 -0700793{
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700794 struct completion *cmp = dma_async_param;
795
796 complete(cmp);
Shannon Nelson95218432007-10-18 03:07:15 -0700797}
798
Shannon Nelson3e037452007-10-16 01:27:40 -0700799/**
800 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
801 * @device: device to be tested
802 */
Dan Williams9de6fc72009-09-08 17:42:58 -0700803int __devinit ioat_dma_self_test(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700804{
805 int i;
806 u8 *src;
807 u8 *dest;
Dan Williamsbc3c7022009-07-28 14:33:42 -0700808 struct dma_device *dma = &device->common;
809 struct device *dev = &device->pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700810 struct dma_chan *dma_chan;
Shannon Nelson711924b2007-12-17 16:20:08 -0800811 struct dma_async_tx_descriptor *tx;
Dan Williams00367312008-02-02 19:49:57 -0700812 dma_addr_t dma_dest, dma_src;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700813 dma_cookie_t cookie;
814 int err = 0;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700815 struct completion cmp;
Dan Williams0c33e1c2009-03-02 13:31:35 -0700816 unsigned long tmo;
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200817 unsigned long flags;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700818
Christoph Lametere94b1762006-12-06 20:33:17 -0800819 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700820 if (!src)
821 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -0800822 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700823 if (!dest) {
824 kfree(src);
825 return -ENOMEM;
826 }
827
828 /* Fill in src buffer */
829 for (i = 0; i < IOAT_TEST_SIZE; i++)
830 src[i] = (u8)i;
831
832 /* Start copy, using first DMA channel */
Dan Williamsbc3c7022009-07-28 14:33:42 -0700833 dma_chan = container_of(dma->channels.next, struct dma_chan,
Shannon Nelson43d6e362007-10-16 01:27:39 -0700834 device_node);
Dan Williamsbc3c7022009-07-28 14:33:42 -0700835 if (dma->device_alloc_chan_resources(dma_chan) < 1) {
836 dev_err(dev, "selftest cannot allocate chan resource\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700837 err = -ENODEV;
838 goto out;
839 }
840
Dan Williamsbc3c7022009-07-28 14:33:42 -0700841 dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
842 dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
Dan Williamsa6a39ca2009-07-28 14:44:05 -0700843 flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE |
844 DMA_PREP_INTERRUPT;
Dan Williams00367312008-02-02 19:49:57 -0700845 tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
Maciej Sosnowski4f005db2009-04-23 12:31:51 +0200846 IOAT_TEST_SIZE, flags);
Shannon Nelson5149fd02007-10-18 03:07:13 -0700847 if (!tx) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700848 dev_err(dev, "Self-test prep failed, disabling\n");
Shannon Nelson5149fd02007-10-18 03:07:13 -0700849 err = -ENODEV;
850 goto free_resources;
851 }
852
Dan Williams7405f742007-01-02 11:10:43 -0700853 async_tx_ack(tx);
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700854 init_completion(&cmp);
Shannon Nelson95218432007-10-18 03:07:15 -0700855 tx->callback = ioat_dma_test_callback;
Dan Williamsb9bdcbb2009-01-06 11:38:22 -0700856 tx->callback_param = &cmp;
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800857 cookie = tx->tx_submit(tx);
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700858 if (cookie < 0) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700859 dev_err(dev, "Self-test setup failed, disabling\n");
Shannon Nelson7f2b2912007-10-18 03:07:14 -0700860 err = -ENODEV;
861 goto free_resources;
862 }
Dan Williamsbc3c7022009-07-28 14:33:42 -0700863 dma->device_issue_pending(dma_chan);
Dan Williams532d3b12008-12-03 17:16:55 -0700864
Dan Williams0c33e1c2009-03-02 13:31:35 -0700865 tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
Chris Leech0bbd5f42006-05-23 17:35:34 -0700866
Dan Williams0c33e1c2009-03-02 13:31:35 -0700867 if (tmo == 0 ||
Dan Williamsbc3c7022009-07-28 14:33:42 -0700868 dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
Shannon Nelson7bb67c12007-11-14 16:59:51 -0800869 != DMA_SUCCESS) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700870 dev_err(dev, "Self-test copy timed out, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700871 err = -ENODEV;
872 goto free_resources;
873 }
874 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
Dan Williamsbc3c7022009-07-28 14:33:42 -0700875 dev_err(dev, "Self-test copy failed compare, disabling\n");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700876 err = -ENODEV;
877 goto free_resources;
878 }
879
880free_resources:
Dan Williamsbc3c7022009-07-28 14:33:42 -0700881 dma->device_free_chan_resources(dma_chan);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700882out:
883 kfree(src);
884 kfree(dest);
885 return err;
886}
887
Shannon Nelson3e037452007-10-16 01:27:40 -0700888static char ioat_interrupt_style[32] = "msix";
889module_param_string(ioat_interrupt_style, ioat_interrupt_style,
890 sizeof(ioat_interrupt_style), 0644);
891MODULE_PARM_DESC(ioat_interrupt_style,
892 "set ioat interrupt style: msix (default), "
893 "msix-single-vector, msi, intx)");
894
895/**
896 * ioat_dma_setup_interrupts - setup interrupt handler
897 * @device: ioat device
898 */
899static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
900{
Dan Williamsdcbc8532009-07-28 14:44:50 -0700901 struct ioat_chan_common *chan;
Dan Williamse6c0b692009-09-08 17:29:44 -0700902 struct pci_dev *pdev = device->pdev;
903 struct device *dev = &pdev->dev;
904 struct msix_entry *msix;
905 int i, j, msixcnt;
906 int err = -EINVAL;
Shannon Nelson3e037452007-10-16 01:27:40 -0700907 u8 intrctrl = 0;
908
909 if (!strcmp(ioat_interrupt_style, "msix"))
910 goto msix;
911 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
912 goto msix_single_vector;
913 if (!strcmp(ioat_interrupt_style, "msi"))
914 goto msi;
915 if (!strcmp(ioat_interrupt_style, "intx"))
916 goto intx;
Dan Williamse6c0b692009-09-08 17:29:44 -0700917 dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
Shannon Nelson5149fd02007-10-18 03:07:13 -0700918 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -0700919
920msix:
921 /* The number of MSI-X vectors should equal the number of channels */
922 msixcnt = device->common.chancnt;
923 for (i = 0; i < msixcnt; i++)
924 device->msix_entries[i].entry = i;
925
Dan Williamse6c0b692009-09-08 17:29:44 -0700926 err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
Shannon Nelson3e037452007-10-16 01:27:40 -0700927 if (err < 0)
928 goto msi;
929 if (err > 0)
930 goto msix_single_vector;
931
932 for (i = 0; i < msixcnt; i++) {
Dan Williamse6c0b692009-09-08 17:29:44 -0700933 msix = &device->msix_entries[i];
Dan Williamsdcbc8532009-07-28 14:44:50 -0700934 chan = ioat_chan_by_index(device, i);
Dan Williamse6c0b692009-09-08 17:29:44 -0700935 err = devm_request_irq(dev, msix->vector,
936 ioat_dma_do_interrupt_msix, 0,
Dan Williamsdcbc8532009-07-28 14:44:50 -0700937 "ioat-msix", chan);
Shannon Nelson3e037452007-10-16 01:27:40 -0700938 if (err) {
939 for (j = 0; j < i; j++) {
Dan Williamse6c0b692009-09-08 17:29:44 -0700940 msix = &device->msix_entries[j];
Dan Williamsdcbc8532009-07-28 14:44:50 -0700941 chan = ioat_chan_by_index(device, j);
942 devm_free_irq(dev, msix->vector, chan);
Shannon Nelson3e037452007-10-16 01:27:40 -0700943 }
944 goto msix_single_vector;
945 }
946 }
947 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
Shannon Nelson3e037452007-10-16 01:27:40 -0700948 goto done;
949
950msix_single_vector:
Dan Williamse6c0b692009-09-08 17:29:44 -0700951 msix = &device->msix_entries[0];
952 msix->entry = 0;
953 err = pci_enable_msix(pdev, device->msix_entries, 1);
Shannon Nelson3e037452007-10-16 01:27:40 -0700954 if (err)
955 goto msi;
956
Dan Williamse6c0b692009-09-08 17:29:44 -0700957 err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
958 "ioat-msix", device);
Shannon Nelson3e037452007-10-16 01:27:40 -0700959 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -0700960 pci_disable_msix(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -0700961 goto msi;
962 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700963 goto done;
964
965msi:
Dan Williamse6c0b692009-09-08 17:29:44 -0700966 err = pci_enable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -0700967 if (err)
968 goto intx;
969
Dan Williamse6c0b692009-09-08 17:29:44 -0700970 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
971 "ioat-msi", device);
Shannon Nelson3e037452007-10-16 01:27:40 -0700972 if (err) {
Dan Williamse6c0b692009-09-08 17:29:44 -0700973 pci_disable_msi(pdev);
Shannon Nelson3e037452007-10-16 01:27:40 -0700974 goto intx;
975 }
Shannon Nelson3e037452007-10-16 01:27:40 -0700976 goto done;
977
978intx:
Dan Williamse6c0b692009-09-08 17:29:44 -0700979 err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
980 IRQF_SHARED, "ioat-intx", device);
Shannon Nelson3e037452007-10-16 01:27:40 -0700981 if (err)
982 goto err_no_irq;
Shannon Nelson3e037452007-10-16 01:27:40 -0700983
984done:
Dan Williamsf2427e22009-07-28 14:42:38 -0700985 if (device->intr_quirk)
986 device->intr_quirk(device);
Shannon Nelson3e037452007-10-16 01:27:40 -0700987 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
988 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
989 return 0;
990
991err_no_irq:
992 /* Disable all interrupt generation */
993 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Dan Williamse6c0b692009-09-08 17:29:44 -0700994 dev_err(dev, "no usable interrupts\n");
995 return err;
Shannon Nelson3e037452007-10-16 01:27:40 -0700996}
997
Dan Williamse6c0b692009-09-08 17:29:44 -0700998static void ioat_disable_interrupts(struct ioatdma_device *device)
Shannon Nelson3e037452007-10-16 01:27:40 -0700999{
Shannon Nelson3e037452007-10-16 01:27:40 -07001000 /* Disable all interrupt generation */
1001 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
Shannon Nelson3e037452007-10-16 01:27:40 -07001002}
1003
Dan Williams345d8522009-09-08 12:01:30 -07001004int __devinit ioat_probe(struct ioatdma_device *device)
Chris Leech0bbd5f42006-05-23 17:35:34 -07001005{
Dan Williamsf2427e22009-07-28 14:42:38 -07001006 int err = -ENODEV;
1007 struct dma_device *dma = &device->common;
1008 struct pci_dev *pdev = device->pdev;
Dan Williamse6c0b692009-09-08 17:29:44 -07001009 struct device *dev = &pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001010
1011 /* DMA coherent memory pool for DMA descriptor allocations */
1012 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
Shannon Nelson8ab89562007-10-16 01:27:39 -07001013 sizeof(struct ioat_dma_descriptor),
1014 64, 0);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001015 if (!device->dma_pool) {
1016 err = -ENOMEM;
1017 goto err_dma_pool;
1018 }
1019
Shannon Nelson43d6e362007-10-16 01:27:39 -07001020 device->completion_pool = pci_pool_create("completion_pool", pdev,
1021 sizeof(u64), SMP_CACHE_BYTES,
1022 SMP_CACHE_BYTES);
Dan Williams5cbafa62009-08-26 13:01:44 -07001023
Chris Leech0bbd5f42006-05-23 17:35:34 -07001024 if (!device->completion_pool) {
1025 err = -ENOMEM;
1026 goto err_completion_pool;
1027 }
1028
Dan Williams5cbafa62009-08-26 13:01:44 -07001029 device->enumerate_channels(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001030
Dan Williamsf2427e22009-07-28 14:42:38 -07001031 dma_cap_set(DMA_MEMCPY, dma->cap_mask);
Dan Williamsf2427e22009-07-28 14:42:38 -07001032 dma->dev = &pdev->dev;
Shannon Nelson7bb67c12007-11-14 16:59:51 -08001033
Dan Williamsbc3c7022009-07-28 14:33:42 -07001034 if (!dma->chancnt) {
Dan Williams5669e312009-09-08 17:42:56 -07001035 dev_err(dev, "zero channels detected\n");
Maciej Sosnowski8b794b12009-02-26 11:04:54 +01001036 goto err_setup_interrupts;
1037 }
1038
Shannon Nelson3e037452007-10-16 01:27:40 -07001039 err = ioat_dma_setup_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001040 if (err)
Shannon Nelson3e037452007-10-16 01:27:40 -07001041 goto err_setup_interrupts;
Shannon Nelson8ab89562007-10-16 01:27:39 -07001042
Dan Williams9de6fc72009-09-08 17:42:58 -07001043 err = device->self_test(device);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001044 if (err)
1045 goto err_self_test;
1046
Dan Williamsf2427e22009-07-28 14:42:38 -07001047 return 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001048
1049err_self_test:
Dan Williamse6c0b692009-09-08 17:29:44 -07001050 ioat_disable_interrupts(device);
Shannon Nelson3e037452007-10-16 01:27:40 -07001051err_setup_interrupts:
Chris Leech0bbd5f42006-05-23 17:35:34 -07001052 pci_pool_destroy(device->completion_pool);
1053err_completion_pool:
1054 pci_pool_destroy(device->dma_pool);
1055err_dma_pool:
Dan Williamsf2427e22009-07-28 14:42:38 -07001056 return err;
1057}
1058
Dan Williams345d8522009-09-08 12:01:30 -07001059int __devinit ioat_register(struct ioatdma_device *device)
Dan Williamsf2427e22009-07-28 14:42:38 -07001060{
1061 int err = dma_async_device_register(&device->common);
1062
1063 if (err) {
1064 ioat_disable_interrupts(device);
1065 pci_pool_destroy(device->completion_pool);
1066 pci_pool_destroy(device->dma_pool);
1067 }
1068
1069 return err;
1070}
1071
1072/* ioat1_intr_quirk - fix up dma ctrl register to enable / disable msi */
1073static void ioat1_intr_quirk(struct ioatdma_device *device)
1074{
1075 struct pci_dev *pdev = device->pdev;
1076 u32 dmactrl;
1077
1078 pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
1079 if (pdev->msi_enabled)
1080 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
1081 else
1082 dmactrl &= ~IOAT_PCI_DMACTRL_MSI_EN;
1083 pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
1084}
1085
Dan Williams5669e312009-09-08 17:42:56 -07001086static ssize_t ring_size_show(struct dma_chan *c, char *page)
1087{
1088 struct ioat_dma_chan *ioat = to_ioat_chan(c);
1089
1090 return sprintf(page, "%d\n", ioat->desccount);
1091}
1092static struct ioat_sysfs_entry ring_size_attr = __ATTR_RO(ring_size);
1093
1094static ssize_t ring_active_show(struct dma_chan *c, char *page)
1095{
1096 struct ioat_dma_chan *ioat = to_ioat_chan(c);
1097
1098 return sprintf(page, "%d\n", ioat->active);
1099}
1100static struct ioat_sysfs_entry ring_active_attr = __ATTR_RO(ring_active);
1101
1102static ssize_t cap_show(struct dma_chan *c, char *page)
1103{
1104 struct dma_device *dma = c->device;
1105
1106 return sprintf(page, "copy%s%s%s%s%s%s\n",
1107 dma_has_cap(DMA_PQ, dma->cap_mask) ? " pq" : "",
1108 dma_has_cap(DMA_PQ_VAL, dma->cap_mask) ? " pq_val" : "",
1109 dma_has_cap(DMA_XOR, dma->cap_mask) ? " xor" : "",
1110 dma_has_cap(DMA_XOR_VAL, dma->cap_mask) ? " xor_val" : "",
1111 dma_has_cap(DMA_MEMSET, dma->cap_mask) ? " fill" : "",
1112 dma_has_cap(DMA_INTERRUPT, dma->cap_mask) ? " intr" : "");
1113
1114}
1115struct ioat_sysfs_entry ioat_cap_attr = __ATTR_RO(cap);
1116
1117static ssize_t version_show(struct dma_chan *c, char *page)
1118{
1119 struct dma_device *dma = c->device;
1120 struct ioatdma_device *device = to_ioatdma_device(dma);
1121
1122 return sprintf(page, "%d.%d\n",
1123 device->version >> 4, device->version & 0xf);
1124}
1125struct ioat_sysfs_entry ioat_version_attr = __ATTR_RO(version);
1126
1127static struct attribute *ioat1_attrs[] = {
1128 &ring_size_attr.attr,
1129 &ring_active_attr.attr,
1130 &ioat_cap_attr.attr,
1131 &ioat_version_attr.attr,
1132 NULL,
1133};
1134
1135static ssize_t
1136ioat_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1137{
1138 struct ioat_sysfs_entry *entry;
1139 struct ioat_chan_common *chan;
1140
1141 entry = container_of(attr, struct ioat_sysfs_entry, attr);
1142 chan = container_of(kobj, struct ioat_chan_common, kobj);
1143
1144 if (!entry->show)
1145 return -EIO;
1146 return entry->show(&chan->common, page);
1147}
1148
1149struct sysfs_ops ioat_sysfs_ops = {
1150 .show = ioat_attr_show,
1151};
1152
1153static struct kobj_type ioat1_ktype = {
1154 .sysfs_ops = &ioat_sysfs_ops,
1155 .default_attrs = ioat1_attrs,
1156};
1157
1158void ioat_kobject_add(struct ioatdma_device *device, struct kobj_type *type)
1159{
1160 struct dma_device *dma = &device->common;
1161 struct dma_chan *c;
1162
1163 list_for_each_entry(c, &dma->channels, device_node) {
1164 struct ioat_chan_common *chan = to_chan_common(c);
1165 struct kobject *parent = &c->dev->device.kobj;
1166 int err;
1167
1168 err = kobject_init_and_add(&chan->kobj, type, parent, "quickdata");
1169 if (err) {
1170 dev_warn(to_dev(chan),
1171 "sysfs init error (%d), continuing...\n", err);
1172 kobject_put(&chan->kobj);
1173 set_bit(IOAT_KOBJ_INIT_FAIL, &chan->state);
1174 }
1175 }
1176}
1177
1178void ioat_kobject_del(struct ioatdma_device *device)
1179{
1180 struct dma_device *dma = &device->common;
1181 struct dma_chan *c;
1182
1183 list_for_each_entry(c, &dma->channels, device_node) {
1184 struct ioat_chan_common *chan = to_chan_common(c);
1185
1186 if (!test_bit(IOAT_KOBJ_INIT_FAIL, &chan->state)) {
1187 kobject_del(&chan->kobj);
1188 kobject_put(&chan->kobj);
1189 }
1190 }
1191}
1192
Dan Williams345d8522009-09-08 12:01:30 -07001193int __devinit ioat1_dma_probe(struct ioatdma_device *device, int dca)
Dan Williamsf2427e22009-07-28 14:42:38 -07001194{
1195 struct pci_dev *pdev = device->pdev;
1196 struct dma_device *dma;
1197 int err;
1198
1199 device->intr_quirk = ioat1_intr_quirk;
Dan Williams5cbafa62009-08-26 13:01:44 -07001200 device->enumerate_channels = ioat1_enumerate_channels;
Dan Williams9de6fc72009-09-08 17:42:58 -07001201 device->self_test = ioat_dma_self_test;
Dan Williamsf2427e22009-07-28 14:42:38 -07001202 dma = &device->common;
1203 dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
1204 dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
Dan Williams5cbafa62009-08-26 13:01:44 -07001205 dma->device_alloc_chan_resources = ioat1_dma_alloc_chan_resources;
1206 dma->device_free_chan_resources = ioat1_dma_free_chan_resources;
1207 dma->device_is_tx_complete = ioat1_dma_is_complete;
Dan Williamsf2427e22009-07-28 14:42:38 -07001208
1209 err = ioat_probe(device);
1210 if (err)
1211 return err;
1212 ioat_set_tcp_copy_break(4096);
1213 err = ioat_register(device);
1214 if (err)
1215 return err;
Dan Williams5669e312009-09-08 17:42:56 -07001216 ioat_kobject_add(device, &ioat1_ktype);
1217
Dan Williamsf2427e22009-07-28 14:42:38 -07001218 if (dca)
1219 device->dca = ioat_dca_init(pdev, device->reg_base);
1220
Dan Williamsf2427e22009-07-28 14:42:38 -07001221 return err;
1222}
1223
Dan Williams345d8522009-09-08 12:01:30 -07001224void __devexit ioat_dma_remove(struct ioatdma_device *device)
Dan Aloni428ed602007-03-08 09:57:36 -08001225{
Dan Williamsbc3c7022009-07-28 14:33:42 -07001226 struct dma_device *dma = &device->common;
Chris Leech0bbd5f42006-05-23 17:35:34 -07001227
Dan Williamse6c0b692009-09-08 17:29:44 -07001228 ioat_disable_interrupts(device);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001229
Dan Williams5669e312009-09-08 17:42:56 -07001230 ioat_kobject_del(device);
1231
Dan Williamsbc3c7022009-07-28 14:33:42 -07001232 dma_async_device_unregister(dma);
Shannon Nelsondfe22992007-10-18 03:07:13 -07001233
Chris Leech0bbd5f42006-05-23 17:35:34 -07001234 pci_pool_destroy(device->dma_pool);
1235 pci_pool_destroy(device->completion_pool);
Shannon Nelson8ab89562007-10-16 01:27:39 -07001236
Dan Williamsdcbc8532009-07-28 14:44:50 -07001237 INIT_LIST_HEAD(&dma->channels);
Chris Leech0bbd5f42006-05-23 17:35:34 -07001238}