blob: 171044930282ba88d1409a41ad718852bbb9bf38 [file] [log] [blame]
Chris Leech0bbd5f42006-05-23 17:35:34 -07001/*
2 * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
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
15 * this program; if not, write to the Free Software Foundation, Inc., 59
16 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called COPYING.
20 */
21
22/*
23 * This driver supports an Intel I/OAT DMA engine, which does asynchronous
24 * copy operations.
25 */
26
27#include <linux/init.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/interrupt.h>
31#include <linux/dmaengine.h>
32#include <linux/delay.h>
David S. Miller6b00c922006-05-23 17:37:58 -070033#include <linux/dma-mapping.h>
Chris Leech0bbd5f42006-05-23 17:35:34 -070034#include "ioatdma.h"
Chris Leech0bbd5f42006-05-23 17:35:34 -070035#include "ioatdma_registers.h"
36#include "ioatdma_hw.h"
37
38#define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
39#define to_ioat_device(dev) container_of(dev, struct ioat_device, common)
40#define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
Dan Williams7405f742007-01-02 11:10:43 -070041#define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
Chris Leech0bbd5f42006-05-23 17:35:34 -070042
43/* internal functions */
44static int __devinit ioat_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
Dan Aloni428ed602007-03-08 09:57:36 -080045static void ioat_shutdown(struct pci_dev *pdev);
Chris Leech0bbd5f42006-05-23 17:35:34 -070046static void __devexit ioat_remove(struct pci_dev *pdev);
47
48static int enumerate_dma_channels(struct ioat_device *device)
49{
50 u8 xfercap_scale;
51 u32 xfercap;
52 int i;
53 struct ioat_dma_chan *ioat_chan;
54
Chris Leeche3828812007-03-08 09:57:35 -080055 device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
56 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -070057 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
58
59 for (i = 0; i < device->common.chancnt; i++) {
60 ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
61 if (!ioat_chan) {
62 device->common.chancnt = i;
63 break;
64 }
65
66 ioat_chan->device = device;
67 ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
68 ioat_chan->xfercap = xfercap;
69 spin_lock_init(&ioat_chan->cleanup_lock);
70 spin_lock_init(&ioat_chan->desc_lock);
71 INIT_LIST_HEAD(&ioat_chan->free_desc);
72 INIT_LIST_HEAD(&ioat_chan->used_desc);
73 /* This should be made common somewhere in dmaengine.c */
74 ioat_chan->common.device = &device->common;
75 ioat_chan->common.client = NULL;
76 list_add_tail(&ioat_chan->common.device_node,
77 &device->common.channels);
78 }
79 return device->common.chancnt;
80}
81
Dan Williams7405f742007-01-02 11:10:43 -070082static void
83ioat_set_src(dma_addr_t addr, struct dma_async_tx_descriptor *tx, int index)
84{
85 struct ioat_desc_sw *iter, *desc = tx_to_ioat_desc(tx);
86 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
87
88 pci_unmap_addr_set(desc, src, addr);
89
90 list_for_each_entry(iter, &desc->async_tx.tx_list, node) {
91 iter->hw->src_addr = addr;
92 addr += ioat_chan->xfercap;
93 }
94
95}
96
97static void
98ioat_set_dest(dma_addr_t addr, struct dma_async_tx_descriptor *tx, int index)
99{
100 struct ioat_desc_sw *iter, *desc = tx_to_ioat_desc(tx);
101 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
102
103 pci_unmap_addr_set(desc, dst, addr);
104
105 list_for_each_entry(iter, &desc->async_tx.tx_list, node) {
106 iter->hw->dst_addr = addr;
107 addr += ioat_chan->xfercap;
108 }
109}
110
111static dma_cookie_t
112ioat_tx_submit(struct dma_async_tx_descriptor *tx)
113{
114 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
115 struct ioat_desc_sw *desc = tx_to_ioat_desc(tx);
116 int append = 0;
117 dma_cookie_t cookie;
118 struct ioat_desc_sw *group_start;
119
120 group_start = list_entry(desc->async_tx.tx_list.next,
121 struct ioat_desc_sw, node);
122 spin_lock_bh(&ioat_chan->desc_lock);
123 /* cookie incr and addition to used_list must be atomic */
124 cookie = ioat_chan->common.cookie;
125 cookie++;
126 if (cookie < 0)
127 cookie = 1;
128 ioat_chan->common.cookie = desc->async_tx.cookie = cookie;
129
130 /* write address into NextDescriptor field of last desc in chain */
131 to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
132 group_start->async_tx.phys;
133 list_splice_init(&desc->async_tx.tx_list, ioat_chan->used_desc.prev);
134
135 ioat_chan->pending += desc->tx_cnt;
136 if (ioat_chan->pending >= 4) {
137 append = 1;
138 ioat_chan->pending = 0;
139 }
140 spin_unlock_bh(&ioat_chan->desc_lock);
141
142 if (append)
143 writeb(IOAT_CHANCMD_APPEND,
144 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
145
146 return cookie;
147}
148
Chris Leech0bbd5f42006-05-23 17:35:34 -0700149static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
150 struct ioat_dma_chan *ioat_chan,
Al Viro47b16532006-10-10 22:45:47 +0100151 gfp_t flags)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700152{
153 struct ioat_dma_descriptor *desc;
154 struct ioat_desc_sw *desc_sw;
155 struct ioat_device *ioat_device;
156 dma_addr_t phys;
157
158 ioat_device = to_ioat_device(ioat_chan->common.device);
159 desc = pci_pool_alloc(ioat_device->dma_pool, flags, &phys);
160 if (unlikely(!desc))
161 return NULL;
162
163 desc_sw = kzalloc(sizeof(*desc_sw), flags);
164 if (unlikely(!desc_sw)) {
165 pci_pool_free(ioat_device->dma_pool, desc, phys);
166 return NULL;
167 }
168
169 memset(desc, 0, sizeof(*desc));
Dan Williams7405f742007-01-02 11:10:43 -0700170 dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
171 desc_sw->async_tx.tx_set_src = ioat_set_src;
172 desc_sw->async_tx.tx_set_dest = ioat_set_dest;
173 desc_sw->async_tx.tx_submit = ioat_tx_submit;
174 INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700175 desc_sw->hw = desc;
Dan Williams7405f742007-01-02 11:10:43 -0700176 desc_sw->async_tx.phys = phys;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700177
178 return desc_sw;
179}
180
181#define INITIAL_IOAT_DESC_COUNT 128
182
183static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan);
184
185/* returns the actual number of allocated descriptors */
186static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
187{
188 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
189 struct ioat_desc_sw *desc = NULL;
190 u16 chanctrl;
191 u32 chanerr;
192 int i;
193 LIST_HEAD(tmp_list);
194
195 /*
196 * In-use bit automatically set by reading chanctrl
197 * If 0, we got it, if 1, someone else did
198 */
Chris Leeche3828812007-03-08 09:57:35 -0800199 chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700200 if (chanctrl & IOAT_CHANCTRL_CHANNEL_IN_USE)
201 return -EBUSY;
202
203 /* Setup register to interrupt and write completion status on error */
204 chanctrl = IOAT_CHANCTRL_CHANNEL_IN_USE |
205 IOAT_CHANCTRL_ERR_INT_EN |
206 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
207 IOAT_CHANCTRL_ERR_COMPLETION_EN;
Chris Leeche3828812007-03-08 09:57:35 -0800208 writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700209
Chris Leeche3828812007-03-08 09:57:35 -0800210 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700211 if (chanerr) {
212 printk("IOAT: CHANERR = %x, clearing\n", chanerr);
Chris Leeche3828812007-03-08 09:57:35 -0800213 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700214 }
215
216 /* Allocate descriptors */
217 for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
218 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
219 if (!desc) {
220 printk(KERN_ERR "IOAT: Only %d initial descriptors\n", i);
221 break;
222 }
223 list_add_tail(&desc->node, &tmp_list);
224 }
225 spin_lock_bh(&ioat_chan->desc_lock);
226 list_splice(&tmp_list, &ioat_chan->free_desc);
227 spin_unlock_bh(&ioat_chan->desc_lock);
228
229 /* allocate a completion writeback area */
230 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
231 ioat_chan->completion_virt =
232 pci_pool_alloc(ioat_chan->device->completion_pool,
233 GFP_KERNEL,
234 &ioat_chan->completion_addr);
235 memset(ioat_chan->completion_virt, 0,
236 sizeof(*ioat_chan->completion_virt));
Chris Leeche3828812007-03-08 09:57:35 -0800237 writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
238 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
239 writel(((u64) ioat_chan->completion_addr) >> 32,
240 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700241
242 ioat_start_null_desc(ioat_chan);
243 return i;
244}
245
246static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
247
248static void ioat_dma_free_chan_resources(struct dma_chan *chan)
249{
250 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
251 struct ioat_device *ioat_device = to_ioat_device(chan->device);
252 struct ioat_desc_sw *desc, *_desc;
253 u16 chanctrl;
254 int in_use_descs = 0;
255
256 ioat_dma_memcpy_cleanup(ioat_chan);
257
Chris Leeche3828812007-03-08 09:57:35 -0800258 writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700259
260 spin_lock_bh(&ioat_chan->desc_lock);
261 list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
262 in_use_descs++;
263 list_del(&desc->node);
Dan Williams7405f742007-01-02 11:10:43 -0700264 pci_pool_free(ioat_device->dma_pool, desc->hw,
265 desc->async_tx.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700266 kfree(desc);
267 }
268 list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
269 list_del(&desc->node);
Dan Williams7405f742007-01-02 11:10:43 -0700270 pci_pool_free(ioat_device->dma_pool, desc->hw,
271 desc->async_tx.phys);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700272 kfree(desc);
273 }
274 spin_unlock_bh(&ioat_chan->desc_lock);
275
276 pci_pool_free(ioat_device->completion_pool,
277 ioat_chan->completion_virt,
278 ioat_chan->completion_addr);
279
280 /* one is ok since we left it on there on purpose */
281 if (in_use_descs > 1)
282 printk(KERN_ERR "IOAT: Freeing %d in use descriptors!\n",
283 in_use_descs - 1);
284
285 ioat_chan->last_completion = ioat_chan->completion_addr = 0;
286
287 /* Tell hw the chan is free */
Chris Leeche3828812007-03-08 09:57:35 -0800288 chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700289 chanctrl &= ~IOAT_CHANCTRL_CHANNEL_IN_USE;
Chris Leeche3828812007-03-08 09:57:35 -0800290 writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700291}
292
Dan Williams7405f742007-01-02 11:10:43 -0700293static struct dma_async_tx_descriptor *
294ioat_dma_prep_memcpy(struct dma_chan *chan, size_t len, int int_en)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700295{
Dan Williams7405f742007-01-02 11:10:43 -0700296 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
297 struct ioat_desc_sw *first, *prev, *new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700298 LIST_HEAD(new_chain);
299 u32 copy;
300 size_t orig_len;
Dan Williams7405f742007-01-02 11:10:43 -0700301 int desc_count = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700302
303 if (!len)
Dan Williams7405f742007-01-02 11:10:43 -0700304 return NULL;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700305
306 orig_len = len;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700307
308 first = NULL;
309 prev = NULL;
310
311 spin_lock_bh(&ioat_chan->desc_lock);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700312 while (len) {
313 if (!list_empty(&ioat_chan->free_desc)) {
314 new = to_ioat_desc(ioat_chan->free_desc.next);
315 list_del(&new->node);
316 } else {
317 /* try to get another desc */
318 new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
319 /* will this ever happen? */
320 /* TODO add upper limit on these */
321 BUG_ON(!new);
322 }
323
324 copy = min((u32) len, ioat_chan->xfercap);
325
326 new->hw->size = copy;
327 new->hw->ctl = 0;
Dan Williams7405f742007-01-02 11:10:43 -0700328 new->async_tx.cookie = 0;
329 new->async_tx.ack = 1;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700330
331 /* chain together the physical address list for the HW */
332 if (!first)
333 first = new;
334 else
Dan Williams7405f742007-01-02 11:10:43 -0700335 prev->hw->next = (u64) new->async_tx.phys;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700336
337 prev = new;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700338 len -= copy;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700339 list_add_tail(&new->node, &new_chain);
340 desc_count++;
341 }
Dan Williams7405f742007-01-02 11:10:43 -0700342
343 list_splice(&new_chain, &new->async_tx.tx_list);
344
Chris Leech0bbd5f42006-05-23 17:35:34 -0700345 new->hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
346 new->hw->next = 0;
Dan Williams7405f742007-01-02 11:10:43 -0700347 new->tx_cnt = desc_count;
348 new->async_tx.ack = 0; /* client is in control of this ack */
349 new->async_tx.cookie = -EBUSY;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700350
Chris Leech0bbd5f42006-05-23 17:35:34 -0700351 pci_unmap_len_set(new, src_len, orig_len);
352 pci_unmap_len_set(new, dst_len, orig_len);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700353 spin_unlock_bh(&ioat_chan->desc_lock);
354
Dan Williams7405f742007-01-02 11:10:43 -0700355 return new ? &new->async_tx : NULL;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700356}
357
Chris Leech0bbd5f42006-05-23 17:35:34 -0700358
359/**
Randy Dunlap65088712006-07-03 19:45:31 -0700360 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended descriptors to hw
Chris Leech0bbd5f42006-05-23 17:35:34 -0700361 * @chan: DMA channel handle
362 */
363
364static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
365{
366 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
367
368 if (ioat_chan->pending != 0) {
369 ioat_chan->pending = 0;
Chris Leeche3828812007-03-08 09:57:35 -0800370 writeb(IOAT_CHANCMD_APPEND,
371 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700372 }
373}
374
375static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *chan)
376{
377 unsigned long phys_complete;
378 struct ioat_desc_sw *desc, *_desc;
379 dma_cookie_t cookie = 0;
380
381 prefetch(chan->completion_virt);
382
383 if (!spin_trylock(&chan->cleanup_lock))
384 return;
385
386 /* The completion writeback can happen at any time,
387 so reads by the driver need to be atomic operations
388 The descriptor physical addresses are limited to 32-bits
389 when the CPU can only do a 32-bit mov */
390
391#if (BITS_PER_LONG == 64)
392 phys_complete =
393 chan->completion_virt->full & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
394#else
395 phys_complete = chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
396#endif
397
398 if ((chan->completion_virt->full & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
399 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
400 printk("IOAT: Channel halted, chanerr = %x\n",
Chris Leeche3828812007-03-08 09:57:35 -0800401 readl(chan->reg_base + IOAT_CHANERR_OFFSET));
Chris Leech0bbd5f42006-05-23 17:35:34 -0700402
403 /* TODO do something to salvage the situation */
404 }
405
406 if (phys_complete == chan->last_completion) {
407 spin_unlock(&chan->cleanup_lock);
408 return;
409 }
410
411 spin_lock_bh(&chan->desc_lock);
412 list_for_each_entry_safe(desc, _desc, &chan->used_desc, node) {
413
414 /*
415 * Incoming DMA requests may use multiple descriptors, due to
416 * exceeding xfercap, perhaps. If so, only the last one will
417 * have a cookie, and require unmapping.
418 */
Dan Williams7405f742007-01-02 11:10:43 -0700419 if (desc->async_tx.cookie) {
420 cookie = desc->async_tx.cookie;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700421
422 /* yes we are unmapping both _page and _single alloc'd
423 regions with unmap_page. Is this *really* that bad?
424 */
425 pci_unmap_page(chan->device->pdev,
426 pci_unmap_addr(desc, dst),
427 pci_unmap_len(desc, dst_len),
428 PCI_DMA_FROMDEVICE);
429 pci_unmap_page(chan->device->pdev,
430 pci_unmap_addr(desc, src),
431 pci_unmap_len(desc, src_len),
432 PCI_DMA_TODEVICE);
433 }
434
Dan Williams7405f742007-01-02 11:10:43 -0700435 if (desc->async_tx.phys != phys_complete) {
436 /* a completed entry, but not the last, so cleanup
437 * if the client is done with the descriptor
438 */
439 if (desc->async_tx.ack) {
440 list_del(&desc->node);
441 list_add_tail(&desc->node, &chan->free_desc);
442 } else
443 desc->async_tx.cookie = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700444 } else {
445 /* last used desc. Do not remove, so we can append from
446 it, but don't look at it next time, either */
Dan Williams7405f742007-01-02 11:10:43 -0700447 desc->async_tx.cookie = 0;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700448
449 /* TODO check status bits? */
450 break;
451 }
452 }
453
454 spin_unlock_bh(&chan->desc_lock);
455
456 chan->last_completion = phys_complete;
457 if (cookie != 0)
458 chan->completed_cookie = cookie;
459
460 spin_unlock(&chan->cleanup_lock);
461}
462
Dan Williams7405f742007-01-02 11:10:43 -0700463static void ioat_dma_dependency_added(struct dma_chan *chan)
464{
465 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
466 spin_lock_bh(&ioat_chan->desc_lock);
467 if (ioat_chan->pending == 0) {
468 spin_unlock_bh(&ioat_chan->desc_lock);
469 ioat_dma_memcpy_cleanup(ioat_chan);
470 } else
471 spin_unlock_bh(&ioat_chan->desc_lock);
472}
473
Chris Leech0bbd5f42006-05-23 17:35:34 -0700474/**
475 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
476 * @chan: IOAT DMA channel handle
477 * @cookie: DMA transaction identifier
Randy Dunlap65088712006-07-03 19:45:31 -0700478 * @done: if not %NULL, updated with last completed transaction
479 * @used: if not %NULL, updated with last used transaction
Chris Leech0bbd5f42006-05-23 17:35:34 -0700480 */
481
482static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
483 dma_cookie_t cookie,
484 dma_cookie_t *done,
485 dma_cookie_t *used)
486{
487 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
488 dma_cookie_t last_used;
489 dma_cookie_t last_complete;
490 enum dma_status ret;
491
492 last_used = chan->cookie;
493 last_complete = ioat_chan->completed_cookie;
494
495 if (done)
496 *done= last_complete;
497 if (used)
498 *used = last_used;
499
500 ret = dma_async_is_complete(cookie, last_complete, last_used);
501 if (ret == DMA_SUCCESS)
502 return ret;
503
504 ioat_dma_memcpy_cleanup(ioat_chan);
505
506 last_used = chan->cookie;
507 last_complete = ioat_chan->completed_cookie;
508
509 if (done)
510 *done= last_complete;
511 if (used)
512 *used = last_used;
513
514 return dma_async_is_complete(cookie, last_complete, last_used);
515}
516
517/* PCI API */
518
519static struct pci_device_id ioat_pci_tbl[] = {
520 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT) },
521 { 0, }
522};
523
Randy Dunlap92504f72007-06-27 14:09:56 -0700524static struct pci_driver ioat_pci_driver = {
Chris Leech0bbd5f42006-05-23 17:35:34 -0700525 .name = "ioatdma",
526 .id_table = ioat_pci_tbl,
527 .probe = ioat_probe,
Dan Aloni428ed602007-03-08 09:57:36 -0800528 .shutdown = ioat_shutdown,
Chris Leech0bbd5f42006-05-23 17:35:34 -0700529 .remove = __devexit_p(ioat_remove),
530};
531
David Howells7d12e782006-10-05 14:55:46 +0100532static irqreturn_t ioat_do_interrupt(int irq, void *data)
Chris Leech0bbd5f42006-05-23 17:35:34 -0700533{
534 struct ioat_device *instance = data;
535 unsigned long attnstatus;
536 u8 intrctrl;
537
Chris Leeche3828812007-03-08 09:57:35 -0800538 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700539
540 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
541 return IRQ_NONE;
542
543 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
Chris Leeche3828812007-03-08 09:57:35 -0800544 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700545 return IRQ_NONE;
546 }
547
Chris Leeche3828812007-03-08 09:57:35 -0800548 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700549
550 printk(KERN_ERR "ioatdma error: interrupt! status %lx\n", attnstatus);
551
Chris Leeche3828812007-03-08 09:57:35 -0800552 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700553 return IRQ_HANDLED;
554}
555
556static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan)
557{
558 struct ioat_desc_sw *desc;
559
560 spin_lock_bh(&ioat_chan->desc_lock);
561
562 if (!list_empty(&ioat_chan->free_desc)) {
563 desc = to_ioat_desc(ioat_chan->free_desc.next);
564 list_del(&desc->node);
565 } else {
566 /* try to get another desc */
567 spin_unlock_bh(&ioat_chan->desc_lock);
568 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
569 spin_lock_bh(&ioat_chan->desc_lock);
570 /* will this ever happen? */
571 BUG_ON(!desc);
572 }
573
574 desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
575 desc->hw->next = 0;
Dan Williams7405f742007-01-02 11:10:43 -0700576 desc->async_tx.ack = 1;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700577
578 list_add_tail(&desc->node, &ioat_chan->used_desc);
579 spin_unlock_bh(&ioat_chan->desc_lock);
580
Dan Williams7405f742007-01-02 11:10:43 -0700581 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
Chris Leeche3828812007-03-08 09:57:35 -0800582 ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
Dan Williams7405f742007-01-02 11:10:43 -0700583 writel(((u64) desc->async_tx.phys) >> 32,
Chris Leech70774b42007-03-08 09:57:35 -0800584 ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
585
Chris Leeche3828812007-03-08 09:57:35 -0800586 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700587}
588
589/*
590 * Perform a IOAT transaction to verify the HW works.
591 */
592#define IOAT_TEST_SIZE 2000
593
594static int ioat_self_test(struct ioat_device *device)
595{
596 int i;
597 u8 *src;
598 u8 *dest;
599 struct dma_chan *dma_chan;
Dan Williams7405f742007-01-02 11:10:43 -0700600 struct dma_async_tx_descriptor *tx;
601 dma_addr_t addr;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700602 dma_cookie_t cookie;
603 int err = 0;
604
Christoph Lametere94b1762006-12-06 20:33:17 -0800605 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700606 if (!src)
607 return -ENOMEM;
Christoph Lametere94b1762006-12-06 20:33:17 -0800608 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700609 if (!dest) {
610 kfree(src);
611 return -ENOMEM;
612 }
613
614 /* Fill in src buffer */
615 for (i = 0; i < IOAT_TEST_SIZE; i++)
616 src[i] = (u8)i;
617
618 /* Start copy, using first DMA channel */
619 dma_chan = container_of(device->common.channels.next,
620 struct dma_chan,
621 device_node);
622 if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
623 err = -ENODEV;
624 goto out;
625 }
626
Dan Williams7405f742007-01-02 11:10:43 -0700627 tx = ioat_dma_prep_memcpy(dma_chan, IOAT_TEST_SIZE, 0);
628 async_tx_ack(tx);
629 addr = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
630 DMA_TO_DEVICE);
631 ioat_set_src(addr, tx, 0);
632 addr = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
633 DMA_FROM_DEVICE);
634 ioat_set_dest(addr, tx, 0);
635 cookie = ioat_tx_submit(tx);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700636 ioat_dma_memcpy_issue_pending(dma_chan);
637 msleep(1);
638
639 if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
640 printk(KERN_ERR "ioatdma: Self-test copy timed out, disabling\n");
641 err = -ENODEV;
642 goto free_resources;
643 }
644 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
645 printk(KERN_ERR "ioatdma: Self-test copy failed compare, disabling\n");
646 err = -ENODEV;
647 goto free_resources;
648 }
649
650free_resources:
651 ioat_dma_free_chan_resources(dma_chan);
652out:
653 kfree(src);
654 kfree(dest);
655 return err;
656}
657
658static int __devinit ioat_probe(struct pci_dev *pdev,
659 const struct pci_device_id *ent)
660{
661 int err;
662 unsigned long mmio_start, mmio_len;
Al Viro47b16532006-10-10 22:45:47 +0100663 void __iomem *reg_base;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700664 struct ioat_device *device;
665
666 err = pci_enable_device(pdev);
667 if (err)
668 goto err_enable_device;
669
670 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
671 if (err)
672 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
673 if (err)
674 goto err_set_dma_mask;
675
Randy Dunlap92504f72007-06-27 14:09:56 -0700676 err = pci_request_regions(pdev, ioat_pci_driver.name);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700677 if (err)
678 goto err_request_regions;
679
680 mmio_start = pci_resource_start(pdev, 0);
681 mmio_len = pci_resource_len(pdev, 0);
682
683 reg_base = ioremap(mmio_start, mmio_len);
684 if (!reg_base) {
685 err = -ENOMEM;
686 goto err_ioremap;
687 }
688
689 device = kzalloc(sizeof(*device), GFP_KERNEL);
690 if (!device) {
691 err = -ENOMEM;
692 goto err_kzalloc;
693 }
694
695 /* DMA coherent memory pool for DMA descriptor allocations */
696 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
697 sizeof(struct ioat_dma_descriptor), 64, 0);
698 if (!device->dma_pool) {
699 err = -ENOMEM;
700 goto err_dma_pool;
701 }
702
703 device->completion_pool = pci_pool_create("completion_pool", pdev, sizeof(u64), SMP_CACHE_BYTES, SMP_CACHE_BYTES);
704 if (!device->completion_pool) {
705 err = -ENOMEM;
706 goto err_completion_pool;
707 }
708
709 device->pdev = pdev;
710 pci_set_drvdata(pdev, device);
711#ifdef CONFIG_PCI_MSI
712 if (pci_enable_msi(pdev) == 0) {
713 device->msi = 1;
714 } else {
715 device->msi = 0;
716 }
717#endif
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700718 err = request_irq(pdev->irq, &ioat_do_interrupt, IRQF_SHARED, "ioat",
Chris Leech0bbd5f42006-05-23 17:35:34 -0700719 device);
720 if (err)
721 goto err_irq;
722
723 device->reg_base = reg_base;
724
Chris Leeche3828812007-03-08 09:57:35 -0800725 writeb(IOAT_INTRCTRL_MASTER_INT_EN, device->reg_base + IOAT_INTRCTRL_OFFSET);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700726 pci_set_master(pdev);
727
728 INIT_LIST_HEAD(&device->common.channels);
729 enumerate_dma_channels(device);
730
Dan Williams7405f742007-01-02 11:10:43 -0700731 dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700732 device->common.device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
733 device->common.device_free_chan_resources = ioat_dma_free_chan_resources;
Dan Williams7405f742007-01-02 11:10:43 -0700734 device->common.device_prep_dma_memcpy = ioat_dma_prep_memcpy;
735 device->common.device_is_tx_complete = ioat_dma_is_complete;
736 device->common.device_issue_pending = ioat_dma_memcpy_issue_pending;
737 device->common.device_dependency_added = ioat_dma_dependency_added;
738 device->common.dev = &pdev->dev;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700739 printk(KERN_INFO "Intel(R) I/OAT DMA Engine found, %d channels\n",
740 device->common.chancnt);
741
742 err = ioat_self_test(device);
743 if (err)
744 goto err_self_test;
745
746 dma_async_device_register(&device->common);
747
748 return 0;
749
750err_self_test:
751err_irq:
752 pci_pool_destroy(device->completion_pool);
753err_completion_pool:
754 pci_pool_destroy(device->dma_pool);
755err_dma_pool:
756 kfree(device);
757err_kzalloc:
758 iounmap(reg_base);
759err_ioremap:
760 pci_release_regions(pdev);
761err_request_regions:
762err_set_dma_mask:
763 pci_disable_device(pdev);
764err_enable_device:
Dan Aloni428ed602007-03-08 09:57:36 -0800765
766 printk(KERN_ERR "Intel(R) I/OAT DMA Engine initialization failed\n");
767
Chris Leech0bbd5f42006-05-23 17:35:34 -0700768 return err;
769}
770
Dan Aloni428ed602007-03-08 09:57:36 -0800771static void ioat_shutdown(struct pci_dev *pdev)
772{
773 struct ioat_device *device;
774 device = pci_get_drvdata(pdev);
775
776 dma_async_device_unregister(&device->common);
777}
778
Chris Leech0bbd5f42006-05-23 17:35:34 -0700779static void __devexit ioat_remove(struct pci_dev *pdev)
780{
781 struct ioat_device *device;
782 struct dma_chan *chan, *_chan;
783 struct ioat_dma_chan *ioat_chan;
784
785 device = pci_get_drvdata(pdev);
786 dma_async_device_unregister(&device->common);
787
788 free_irq(device->pdev->irq, device);
789#ifdef CONFIG_PCI_MSI
790 if (device->msi)
791 pci_disable_msi(device->pdev);
792#endif
793 pci_pool_destroy(device->dma_pool);
794 pci_pool_destroy(device->completion_pool);
795 iounmap(device->reg_base);
796 pci_release_regions(pdev);
797 pci_disable_device(pdev);
798 list_for_each_entry_safe(chan, _chan, &device->common.channels, device_node) {
799 ioat_chan = to_ioat_chan(chan);
800 list_del(&chan->device_node);
801 kfree(ioat_chan);
802 }
803 kfree(device);
804}
805
806/* MODULE API */
Chris Leech000725d2007-03-08 09:57:33 -0800807MODULE_VERSION("1.9");
Chris Leech0bbd5f42006-05-23 17:35:34 -0700808MODULE_LICENSE("GPL");
809MODULE_AUTHOR("Intel Corporation");
810
811static int __init ioat_init_module(void)
812{
813 /* it's currently unsafe to unload this module */
814 /* if forced, worst case is that rmmod hangs */
David S. Miller8070b2b2006-06-26 00:10:46 -0700815 __unsafe(THIS_MODULE);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700816
Randy Dunlap92504f72007-06-27 14:09:56 -0700817 return pci_register_driver(&ioat_pci_driver);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700818}
819
820module_init(ioat_init_module);
821
822static void __exit ioat_exit_module(void)
823{
Randy Dunlap92504f72007-06-27 14:09:56 -0700824 pci_unregister_driver(&ioat_pci_driver);
Chris Leech0bbd5f42006-05-23 17:35:34 -0700825}
826
827module_exit(ioat_exit_module);