2 * Intel I/OAT DMA Linux driver
3 * Copyright(c) 2004 - 2007 Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
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
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.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
24 * This driver supports an Intel I/OAT DMA engine, which does asynchronous
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>
34 #include <linux/dma-mapping.h>
36 #include "ioatdma_registers.h"
37 #include "ioatdma_hw.h"
39 #define INITIAL_IOAT_DESC_COUNT 128
41 #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
42 #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
43 #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
44 #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
46 /* internal functions */
47 static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
48 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
49 static struct ioat_desc_sw *
50 ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
52 static inline struct ioat_dma_chan *ioat_lookup_chan_by_index(
53 struct ioatdma_device *device,
56 return device->idx[index];
60 * ioat_dma_do_interrupt - handler used for single vector interrupt mode
62 * @data: interrupt data
64 static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
66 struct ioatdma_device *instance = data;
67 struct ioat_dma_chan *ioat_chan;
68 unsigned long attnstatus;
72 intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
74 if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
77 if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
78 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
82 attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
83 for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
84 ioat_chan = ioat_lookup_chan_by_index(instance, bit);
85 tasklet_schedule(&ioat_chan->cleanup_task);
88 writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
93 * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
95 * @data: interrupt data
97 static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
99 struct ioat_dma_chan *ioat_chan = data;
101 tasklet_schedule(&ioat_chan->cleanup_task);
106 static void ioat_dma_cleanup_tasklet(unsigned long data);
109 * ioat_dma_enumerate_channels - find and initialize the device's channels
110 * @device: the device to be enumerated
112 static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
117 struct ioat_dma_chan *ioat_chan;
119 device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
120 xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
121 xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
123 for (i = 0; i < device->common.chancnt; i++) {
124 ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
126 device->common.chancnt = i;
130 ioat_chan->device = device;
131 ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
132 ioat_chan->xfercap = xfercap;
133 spin_lock_init(&ioat_chan->cleanup_lock);
134 spin_lock_init(&ioat_chan->desc_lock);
135 INIT_LIST_HEAD(&ioat_chan->free_desc);
136 INIT_LIST_HEAD(&ioat_chan->used_desc);
137 /* This should be made common somewhere in dmaengine.c */
138 ioat_chan->common.device = &device->common;
139 list_add_tail(&ioat_chan->common.device_node,
140 &device->common.channels);
141 device->idx[i] = ioat_chan;
142 tasklet_init(&ioat_chan->cleanup_task,
143 ioat_dma_cleanup_tasklet,
144 (unsigned long) ioat_chan);
145 tasklet_disable(&ioat_chan->cleanup_task);
147 return device->common.chancnt;
150 static void ioat_set_src(dma_addr_t addr,
151 struct dma_async_tx_descriptor *tx,
154 tx_to_ioat_desc(tx)->src = addr;
157 static void ioat_set_dest(dma_addr_t addr,
158 struct dma_async_tx_descriptor *tx,
161 tx_to_ioat_desc(tx)->dst = addr;
164 static dma_cookie_t ioat_tx_submit(struct dma_async_tx_descriptor *tx)
166 struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
167 struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
168 struct ioat_desc_sw *prev, *new;
169 struct ioat_dma_descriptor *hw;
172 LIST_HEAD(new_chain);
177 unsigned int desc_count = 0;
179 /* src and dest and len are stored in the initial descriptor */
183 orig_ack = first->async_tx.ack;
186 spin_lock_bh(&ioat_chan->desc_lock);
187 prev = to_ioat_desc(ioat_chan->used_desc.prev);
190 copy = min((u32) len, ioat_chan->xfercap);
192 new->async_tx.ack = 1;
201 /* chain together the physical address list for the HW */
203 prev->hw->next = (u64) new->async_tx.phys;
209 list_add_tail(&new->node, &new_chain);
212 } while (len && (new = ioat_dma_get_next_descriptor(ioat_chan)));
214 hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
215 if (new->async_tx.callback) {
216 hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
218 /* move callback into to last desc */
219 new->async_tx.callback = first->async_tx.callback;
220 new->async_tx.callback_param
221 = first->async_tx.callback_param;
222 first->async_tx.callback = NULL;
223 first->async_tx.callback_param = NULL;
227 new->tx_cnt = desc_count;
228 new->async_tx.ack = orig_ack; /* client is in control of this ack */
230 /* store the original values for use in later cleanup */
232 new->src = first->src;
233 new->dst = first->dst;
234 new->len = first->len;
237 /* cookie incr and addition to used_list must be atomic */
238 cookie = ioat_chan->common.cookie;
242 ioat_chan->common.cookie = new->async_tx.cookie = cookie;
244 /* write address into NextDescriptor field of last desc in chain */
245 to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
246 first->async_tx.phys;
247 __list_splice(&new_chain, ioat_chan->used_desc.prev);
249 ioat_chan->pending += desc_count;
250 if (ioat_chan->pending >= 4) {
252 ioat_chan->pending = 0;
254 spin_unlock_bh(&ioat_chan->desc_lock);
257 writeb(IOAT_CHANCMD_APPEND,
258 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
263 static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
264 struct ioat_dma_chan *ioat_chan,
267 struct ioat_dma_descriptor *desc;
268 struct ioat_desc_sw *desc_sw;
269 struct ioatdma_device *ioatdma_device;
272 ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
273 desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
277 desc_sw = kzalloc(sizeof(*desc_sw), flags);
278 if (unlikely(!desc_sw)) {
279 pci_pool_free(ioatdma_device->dma_pool, desc, phys);
283 memset(desc, 0, sizeof(*desc));
284 dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
285 desc_sw->async_tx.tx_set_src = ioat_set_src;
286 desc_sw->async_tx.tx_set_dest = ioat_set_dest;
287 desc_sw->async_tx.tx_submit = ioat_tx_submit;
288 INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
290 desc_sw->async_tx.phys = phys;
295 /* returns the actual number of allocated descriptors */
296 static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
298 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
299 struct ioat_desc_sw *desc = NULL;
305 /* have we already been set up? */
306 if (!list_empty(&ioat_chan->free_desc))
307 return INITIAL_IOAT_DESC_COUNT;
309 /* Setup register to interrupt and write completion status on error */
310 chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
311 IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
312 IOAT_CHANCTRL_ERR_COMPLETION_EN;
313 writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
315 chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
317 dev_err(&ioat_chan->device->pdev->dev,
318 "CHANERR = %x, clearing\n", chanerr);
319 writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
322 /* Allocate descriptors */
323 for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
324 desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
326 dev_err(&ioat_chan->device->pdev->dev,
327 "Only %d initial descriptors\n", i);
330 list_add_tail(&desc->node, &tmp_list);
332 spin_lock_bh(&ioat_chan->desc_lock);
333 list_splice(&tmp_list, &ioat_chan->free_desc);
334 spin_unlock_bh(&ioat_chan->desc_lock);
336 /* allocate a completion writeback area */
337 /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
338 ioat_chan->completion_virt =
339 pci_pool_alloc(ioat_chan->device->completion_pool,
341 &ioat_chan->completion_addr);
342 memset(ioat_chan->completion_virt, 0,
343 sizeof(*ioat_chan->completion_virt));
344 writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
345 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
346 writel(((u64) ioat_chan->completion_addr) >> 32,
347 ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
349 tasklet_enable(&ioat_chan->cleanup_task);
350 ioat_dma_start_null_desc(ioat_chan);
354 static void ioat_dma_free_chan_resources(struct dma_chan *chan)
356 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
357 struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
358 struct ioat_desc_sw *desc, *_desc;
359 int in_use_descs = 0;
361 tasklet_disable(&ioat_chan->cleanup_task);
362 ioat_dma_memcpy_cleanup(ioat_chan);
364 /* Delay 100ms after reset to allow internal DMA logic to quiesce
365 * before removing DMA descriptor resources.
367 writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
370 spin_lock_bh(&ioat_chan->desc_lock);
371 list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
373 list_del(&desc->node);
374 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
375 desc->async_tx.phys);
378 list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
379 list_del(&desc->node);
380 pci_pool_free(ioatdma_device->dma_pool, desc->hw,
381 desc->async_tx.phys);
384 spin_unlock_bh(&ioat_chan->desc_lock);
386 pci_pool_free(ioatdma_device->completion_pool,
387 ioat_chan->completion_virt,
388 ioat_chan->completion_addr);
390 /* one is ok since we left it on there on purpose */
391 if (in_use_descs > 1)
392 dev_err(&ioat_chan->device->pdev->dev,
393 "Freeing %d in use descriptors!\n",
396 ioat_chan->last_completion = ioat_chan->completion_addr = 0;
397 ioat_chan->pending = 0;
401 * ioat_dma_get_next_descriptor - return the next available descriptor
402 * @ioat_chan: IOAT DMA channel handle
404 * Gets the next descriptor from the chain, and must be called with the
405 * channel's desc_lock held. Allocates more descriptors if the channel
408 static struct ioat_desc_sw *
409 ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
411 struct ioat_desc_sw *new = NULL;
413 if (!list_empty(&ioat_chan->free_desc)) {
414 new = to_ioat_desc(ioat_chan->free_desc.next);
415 list_del(&new->node);
417 /* try to get another desc */
418 new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
419 /* will this ever happen? */
420 /* TODO add upper limit on these */
428 static struct dma_async_tx_descriptor *ioat_dma_prep_memcpy(
429 struct dma_chan *chan,
433 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
434 struct ioat_desc_sw *new;
436 spin_lock_bh(&ioat_chan->desc_lock);
437 new = ioat_dma_get_next_descriptor(ioat_chan);
439 spin_unlock_bh(&ioat_chan->desc_lock);
441 return new ? &new->async_tx : NULL;
445 * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
447 * @chan: DMA channel handle
449 static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
451 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
453 if (ioat_chan->pending != 0) {
454 ioat_chan->pending = 0;
455 writeb(IOAT_CHANCMD_APPEND,
456 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
460 static void ioat_dma_cleanup_tasklet(unsigned long data)
462 struct ioat_dma_chan *chan = (void *)data;
463 ioat_dma_memcpy_cleanup(chan);
464 writew(IOAT_CHANCTRL_INT_DISABLE,
465 chan->reg_base + IOAT_CHANCTRL_OFFSET);
468 static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
470 unsigned long phys_complete;
471 struct ioat_desc_sw *desc, *_desc;
472 dma_cookie_t cookie = 0;
474 prefetch(ioat_chan->completion_virt);
476 if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
479 /* The completion writeback can happen at any time,
480 so reads by the driver need to be atomic operations
481 The descriptor physical addresses are limited to 32-bits
482 when the CPU can only do a 32-bit mov */
484 #if (BITS_PER_LONG == 64)
486 ioat_chan->completion_virt->full
487 & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
490 ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
493 if ((ioat_chan->completion_virt->full
494 & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
495 IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
496 dev_err(&ioat_chan->device->pdev->dev,
497 "Channel halted, chanerr = %x\n",
498 readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
500 /* TODO do something to salvage the situation */
503 if (phys_complete == ioat_chan->last_completion) {
504 spin_unlock_bh(&ioat_chan->cleanup_lock);
509 spin_lock_bh(&ioat_chan->desc_lock);
510 list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
513 * Incoming DMA requests may use multiple descriptors, due to
514 * exceeding xfercap, perhaps. If so, only the last one will
515 * have a cookie, and require unmapping.
517 if (desc->async_tx.cookie) {
518 cookie = desc->async_tx.cookie;
521 * yes we are unmapping both _page and _single alloc'd
522 * regions with unmap_page. Is this *really* that bad?
524 pci_unmap_page(ioat_chan->device->pdev,
525 pci_unmap_addr(desc, dst),
526 pci_unmap_len(desc, len),
528 pci_unmap_page(ioat_chan->device->pdev,
529 pci_unmap_addr(desc, src),
530 pci_unmap_len(desc, len),
532 if (desc->async_tx.callback) {
533 desc->async_tx.callback(
534 desc->async_tx.callback_param);
535 desc->async_tx.callback = NULL;
539 if (desc->async_tx.phys != phys_complete) {
541 * a completed entry, but not the last, so cleanup
542 * if the client is done with the descriptor
544 if (desc->async_tx.ack) {
545 list_del(&desc->node);
546 list_add_tail(&desc->node,
547 &ioat_chan->free_desc);
549 desc->async_tx.cookie = 0;
552 * last used desc. Do not remove, so we can append from
553 * it, but don't look at it next time, either
555 desc->async_tx.cookie = 0;
557 /* TODO check status bits? */
562 spin_unlock_bh(&ioat_chan->desc_lock);
564 ioat_chan->last_completion = phys_complete;
566 ioat_chan->completed_cookie = cookie;
568 spin_unlock_bh(&ioat_chan->cleanup_lock);
571 static void ioat_dma_dependency_added(struct dma_chan *chan)
573 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
574 spin_lock_bh(&ioat_chan->desc_lock);
575 if (ioat_chan->pending == 0) {
576 spin_unlock_bh(&ioat_chan->desc_lock);
577 ioat_dma_memcpy_cleanup(ioat_chan);
579 spin_unlock_bh(&ioat_chan->desc_lock);
583 * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
584 * @chan: IOAT DMA channel handle
585 * @cookie: DMA transaction identifier
586 * @done: if not %NULL, updated with last completed transaction
587 * @used: if not %NULL, updated with last used transaction
589 static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
594 struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
595 dma_cookie_t last_used;
596 dma_cookie_t last_complete;
599 last_used = chan->cookie;
600 last_complete = ioat_chan->completed_cookie;
603 *done = last_complete;
607 ret = dma_async_is_complete(cookie, last_complete, last_used);
608 if (ret == DMA_SUCCESS)
611 ioat_dma_memcpy_cleanup(ioat_chan);
613 last_used = chan->cookie;
614 last_complete = ioat_chan->completed_cookie;
617 *done = last_complete;
621 return dma_async_is_complete(cookie, last_complete, last_used);
626 static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
628 struct ioat_desc_sw *desc;
630 spin_lock_bh(&ioat_chan->desc_lock);
632 desc = ioat_dma_get_next_descriptor(ioat_chan);
633 desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
634 | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
635 | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
638 desc->hw->src_addr = 0;
639 desc->hw->dst_addr = 0;
640 desc->async_tx.ack = 1;
642 list_add_tail(&desc->node, &ioat_chan->used_desc);
643 spin_unlock_bh(&ioat_chan->desc_lock);
645 writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
646 ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
647 writel(((u64) desc->async_tx.phys) >> 32,
648 ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
650 writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
654 * Perform a IOAT transaction to verify the HW works.
656 #define IOAT_TEST_SIZE 2000
658 static void ioat_dma_test_callback(void *dma_async_param)
660 printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
665 * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
666 * @device: device to be tested
668 static int ioat_dma_self_test(struct ioatdma_device *device)
673 struct dma_chan *dma_chan;
674 struct dma_async_tx_descriptor *tx = NULL;
679 src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
682 dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
688 /* Fill in src buffer */
689 for (i = 0; i < IOAT_TEST_SIZE; i++)
692 /* Start copy, using first DMA channel */
693 dma_chan = container_of(device->common.channels.next,
696 if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
697 dev_err(&device->pdev->dev,
698 "selftest cannot allocate chan resource\n");
703 tx = ioat_dma_prep_memcpy(dma_chan, IOAT_TEST_SIZE, 0);
705 dev_err(&device->pdev->dev,
706 "Self-test prep failed, disabling\n");
712 addr = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
714 ioat_set_src(addr, tx, 0);
715 addr = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
717 ioat_set_dest(addr, tx, 0);
718 tx->callback = ioat_dma_test_callback;
719 tx->callback_param = (void *)0x8086;
720 cookie = ioat_tx_submit(tx);
722 dev_err(&device->pdev->dev,
723 "Self-test setup failed, disabling\n");
727 ioat_dma_memcpy_issue_pending(dma_chan);
730 if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
731 dev_err(&device->pdev->dev,
732 "Self-test copy timed out, disabling\n");
736 if (memcmp(src, dest, IOAT_TEST_SIZE)) {
737 dev_err(&device->pdev->dev,
738 "Self-test copy failed compare, disabling\n");
744 ioat_dma_free_chan_resources(dma_chan);
751 static char ioat_interrupt_style[32] = "msix";
752 module_param_string(ioat_interrupt_style, ioat_interrupt_style,
753 sizeof(ioat_interrupt_style), 0644);
754 MODULE_PARM_DESC(ioat_interrupt_style,
755 "set ioat interrupt style: msix (default), "
756 "msix-single-vector, msi, intx)");
759 * ioat_dma_setup_interrupts - setup interrupt handler
760 * @device: ioat device
762 static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
764 struct ioat_dma_chan *ioat_chan;
765 int err, i, j, msixcnt;
768 if (!strcmp(ioat_interrupt_style, "msix"))
770 if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
771 goto msix_single_vector;
772 if (!strcmp(ioat_interrupt_style, "msi"))
774 if (!strcmp(ioat_interrupt_style, "intx"))
776 dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
777 ioat_interrupt_style);
781 /* The number of MSI-X vectors should equal the number of channels */
782 msixcnt = device->common.chancnt;
783 for (i = 0; i < msixcnt; i++)
784 device->msix_entries[i].entry = i;
786 err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
790 goto msix_single_vector;
792 for (i = 0; i < msixcnt; i++) {
793 ioat_chan = ioat_lookup_chan_by_index(device, i);
794 err = request_irq(device->msix_entries[i].vector,
795 ioat_dma_do_interrupt_msix,
796 0, "ioat-msix", ioat_chan);
798 for (j = 0; j < i; j++) {
800 ioat_lookup_chan_by_index(device, j);
801 free_irq(device->msix_entries[j].vector,
804 goto msix_single_vector;
807 intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
808 device->irq_mode = msix_multi_vector;
812 device->msix_entries[0].entry = 0;
813 err = pci_enable_msix(device->pdev, device->msix_entries, 1);
817 err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
818 0, "ioat-msix", device);
820 pci_disable_msix(device->pdev);
823 device->irq_mode = msix_single_vector;
827 err = pci_enable_msi(device->pdev);
831 err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
832 0, "ioat-msi", device);
834 pci_disable_msi(device->pdev);
838 * CB 1.2 devices need a bit set in configuration space to enable MSI
840 if (device->version == IOAT_VER_1_2) {
842 pci_read_config_dword(device->pdev,
843 IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
844 dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
845 pci_write_config_dword(device->pdev,
846 IOAT_PCI_DMACTRL_OFFSET, dmactrl);
848 device->irq_mode = msi;
852 err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
853 IRQF_SHARED, "ioat-intx", device);
856 device->irq_mode = intx;
859 intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
860 writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
864 /* Disable all interrupt generation */
865 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
866 dev_err(&device->pdev->dev, "no usable interrupts\n");
867 device->irq_mode = none;
872 * ioat_dma_remove_interrupts - remove whatever interrupts were set
873 * @device: ioat device
875 static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
877 struct ioat_dma_chan *ioat_chan;
880 /* Disable all interrupt generation */
881 writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
883 switch (device->irq_mode) {
884 case msix_multi_vector:
885 for (i = 0; i < device->common.chancnt; i++) {
886 ioat_chan = ioat_lookup_chan_by_index(device, i);
887 free_irq(device->msix_entries[i].vector, ioat_chan);
889 pci_disable_msix(device->pdev);
891 case msix_single_vector:
892 free_irq(device->msix_entries[0].vector, device);
893 pci_disable_msix(device->pdev);
896 free_irq(device->pdev->irq, device);
897 pci_disable_msi(device->pdev);
900 free_irq(device->pdev->irq, device);
903 dev_warn(&device->pdev->dev,
904 "call to %s without interrupts setup\n", __func__);
906 device->irq_mode = none;
909 struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
910 void __iomem *iobase)
913 struct ioatdma_device *device;
915 device = kzalloc(sizeof(*device), GFP_KERNEL);
921 device->reg_base = iobase;
922 device->version = readb(device->reg_base + IOAT_VER_OFFSET);
924 /* DMA coherent memory pool for DMA descriptor allocations */
925 device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
926 sizeof(struct ioat_dma_descriptor),
928 if (!device->dma_pool) {
933 device->completion_pool = pci_pool_create("completion_pool", pdev,
934 sizeof(u64), SMP_CACHE_BYTES,
936 if (!device->completion_pool) {
938 goto err_completion_pool;
941 INIT_LIST_HEAD(&device->common.channels);
942 ioat_dma_enumerate_channels(device);
944 dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
945 device->common.device_alloc_chan_resources =
946 ioat_dma_alloc_chan_resources;
947 device->common.device_free_chan_resources =
948 ioat_dma_free_chan_resources;
949 device->common.device_prep_dma_memcpy = ioat_dma_prep_memcpy;
950 device->common.device_is_tx_complete = ioat_dma_is_complete;
951 device->common.device_issue_pending = ioat_dma_memcpy_issue_pending;
952 device->common.device_dependency_added = ioat_dma_dependency_added;
953 device->common.dev = &pdev->dev;
954 dev_err(&device->pdev->dev,
955 "Intel(R) I/OAT DMA Engine found,"
956 " %d channels, device version 0x%02x, driver version %s\n",
957 device->common.chancnt, device->version, IOAT_DMA_VERSION);
959 err = ioat_dma_setup_interrupts(device);
961 goto err_setup_interrupts;
963 err = ioat_dma_self_test(device);
967 dma_async_device_register(&device->common);
972 ioat_dma_remove_interrupts(device);
973 err_setup_interrupts:
974 pci_pool_destroy(device->completion_pool);
976 pci_pool_destroy(device->dma_pool);
980 dev_err(&device->pdev->dev,
981 "Intel(R) I/OAT DMA Engine initialization failed\n");
985 void ioat_dma_remove(struct ioatdma_device *device)
987 struct dma_chan *chan, *_chan;
988 struct ioat_dma_chan *ioat_chan;
990 ioat_dma_remove_interrupts(device);
992 dma_async_device_unregister(&device->common);
994 pci_pool_destroy(device->dma_pool);
995 pci_pool_destroy(device->completion_pool);
997 iounmap(device->reg_base);
998 pci_release_regions(device->pdev);
999 pci_disable_device(device->pdev);
1001 list_for_each_entry_safe(chan, _chan,
1002 &device->common.channels, device_node) {
1003 ioat_chan = to_ioat_chan(chan);
1004 list_del(&chan->device_node);