2 * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
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)
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 * The full GNU General Public License is included in this distribution in the
15 * file called COPYING.
20 #include <linux/dmaengine.h>
22 #include "registers.h"
23 #include <linux/init.h>
24 #include <linux/dmapool.h>
25 #include <linux/cache.h>
26 #include <linux/pci_ids.h>
29 #define IOAT_DMA_VERSION "4.00"
31 #define IOAT_DMA_DCA_ANY_CPU ~0
33 #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
34 #define to_dev(ioat_chan) (&(ioat_chan)->device->pdev->dev)
35 #define to_pdev(ioat_chan) ((ioat_chan)->device->pdev)
37 #define chan_num(ch) ((int)((ch)->reg_base - (ch)->device->reg_base) / 0x80)
40 * workaround for IOAT ver.3.0 null descriptor issue
41 * (channel returns error when size is 0)
43 #define NULL_DESC_BUFFER_SIZE 1
53 * struct ioatdma_device - internal representation of a IOAT device
54 * @pdev: PCI-Express device
55 * @reg_base: MMIO register space base address
56 * @dma_pool: for allocating DMA descriptors
57 * @common: embedded struct dma_device
58 * @version: version of ioatdma device
59 * @msix_entries: irq handlers
60 * @idx: per channel data
61 * @dca: direct cache access context
62 * @intr_quirk: interrupt setup quirk (for ioat_v1 devices)
63 * @enumerate_channels: hw version specific channel enumeration
64 * @reset_hw: hw version specific channel (re)initialization
65 * @cleanup_fn: select between the v2 and v3 cleanup routines
66 * @timer_fn: select between the v2 and v3 timer watchdog routines
67 * @self_test: hardware version specific self test for each supported op type
69 * Note: the v3 cleanup routine supports raid operations
71 struct ioatdma_device {
73 void __iomem *reg_base;
74 struct pci_pool *dma_pool;
75 struct pci_pool *completion_pool;
76 #define MAX_SED_POOLS 5
77 struct dma_pool *sed_hw_pool[MAX_SED_POOLS];
78 struct dma_device common;
80 struct msix_entry msix_entries[4];
81 struct ioatdma_chan *idx[4];
82 struct dca_provider *dca;
83 enum ioat_irq_mode irq_mode;
85 void (*intr_quirk)(struct ioatdma_device *device);
86 int (*enumerate_channels)(struct ioatdma_device *device);
87 int (*reset_hw)(struct ioatdma_chan *ioat_chan);
88 void (*cleanup_fn)(unsigned long data);
89 void (*timer_fn)(unsigned long data);
90 int (*self_test)(struct ioatdma_device *device);
94 struct dma_chan dma_chan;
95 void __iomem *reg_base;
96 dma_addr_t last_completion;
97 spinlock_t cleanup_lock;
99 #define IOAT_COMPLETION_PENDING 0
100 #define IOAT_COMPLETION_ACK 1
101 #define IOAT_RESET_PENDING 2
102 #define IOAT_KOBJ_INIT_FAIL 3
103 #define IOAT_RESHAPE_PENDING 4
105 #define IOAT_CHAN_ACTIVE 6
106 struct timer_list timer;
107 #define COMPLETION_TIMEOUT msecs_to_jiffies(100)
108 #define IDLE_TIMEOUT msecs_to_jiffies(2000)
109 #define RESET_DELAY msecs_to_jiffies(100)
110 struct ioatdma_device *device;
111 dma_addr_t completion_dma;
113 struct tasklet_struct cleanup_task;
116 /* ioat v2 / v3 channel attributes
117 * @xfercap_log; log2 of channel max transfer length (for fast division)
118 * @head: allocated index
119 * @issued: hardware notification point
120 * @tail: cleanup index
121 * @dmacount: identical to 'head' except for occasionally resetting to zero
122 * @alloc_order: log2 of the number of allocated descriptors
123 * @produce: number of descriptors to produce at submit time
124 * @ring: software ring buffer implementation of hardware ring
125 * @prep_lock: serializes descriptor preparation (producers)
134 struct ioat_ring_ent **ring;
135 spinlock_t prep_lock;
138 struct ioat_sysfs_entry {
139 struct attribute attr;
140 ssize_t (*show)(struct dma_chan *, char *);
144 * struct ioat_sed_ent - wrapper around super extended hardware descriptor
146 * @sed_dma: dma address for the SED
148 * @parent: point to the dma descriptor that's the parent
150 struct ioat_sed_ent {
151 struct ioat_sed_raw_descriptor *hw;
153 struct ioat_ring_ent *parent;
154 unsigned int hw_pool;
157 static inline struct ioatdma_chan *to_ioat_chan(struct dma_chan *c)
159 return container_of(c, struct ioatdma_chan, dma_chan);
164 /* wrapper around hardware descriptor format + additional software fields */
167 #define set_desc_id(desc, i) ((desc)->id = (i))
168 #define desc_id(desc) ((desc)->id)
170 #define set_desc_id(desc, i)
171 #define desc_id(desc) (0)
175 __dump_desc_dbg(struct ioatdma_chan *ioat_chan, struct ioat_dma_descriptor *hw,
176 struct dma_async_tx_descriptor *tx, int id)
178 struct device *dev = to_dev(ioat_chan);
180 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) cookie: %d flags: %#x"
181 " ctl: %#10.8x (op: %#x int_en: %d compl: %d)\n", id,
182 (unsigned long long) tx->phys,
183 (unsigned long long) hw->next, tx->cookie, tx->flags,
184 hw->ctl, hw->ctl_f.op, hw->ctl_f.int_en, hw->ctl_f.compl_write);
187 #define dump_desc_dbg(c, d) \
188 ({ if (d) __dump_desc_dbg(c, d->hw, &d->txd, desc_id(d)); 0; })
190 static inline struct ioatdma_chan *
191 ioat_chan_by_index(struct ioatdma_device *device, int index)
193 return device->idx[index];
196 static inline u64 ioat_chansts_32(struct ioatdma_chan *ioat_chan)
198 u8 ver = ioat_chan->device->version;
202 /* We need to read the low address first as this causes the
203 * chipset to latch the upper bits for the subsequent read
205 status_lo = readl(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET_LOW(ver));
206 status = readl(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET_HIGH(ver));
213 #if BITS_PER_LONG == 64
215 static inline u64 ioat_chansts(struct ioatdma_chan *ioat_chan)
217 u8 ver = ioat_chan->device->version;
220 /* With IOAT v3.3 the status register is 64bit. */
221 if (ver >= IOAT_VER_3_3)
222 status = readq(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET(ver));
224 status = ioat_chansts_32(ioat_chan);
230 #define ioat_chansts ioat_chansts_32
233 static inline u64 ioat_chansts_to_addr(u64 status)
235 return status & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
238 static inline u32 ioat_chanerr(struct ioatdma_chan *ioat_chan)
240 return readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
243 static inline void ioat_suspend(struct ioatdma_chan *ioat_chan)
245 u8 ver = ioat_chan->device->version;
247 writeb(IOAT_CHANCMD_SUSPEND,
248 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
251 static inline void ioat_reset(struct ioatdma_chan *ioat_chan)
253 u8 ver = ioat_chan->device->version;
255 writeb(IOAT_CHANCMD_RESET,
256 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
259 static inline bool ioat_reset_pending(struct ioatdma_chan *ioat_chan)
261 u8 ver = ioat_chan->device->version;
264 cmd = readb(ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
265 return (cmd & IOAT_CHANCMD_RESET) == IOAT_CHANCMD_RESET;
268 static inline bool is_ioat_active(unsigned long status)
270 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_ACTIVE);
273 static inline bool is_ioat_idle(unsigned long status)
275 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_DONE);
278 static inline bool is_ioat_halted(unsigned long status)
280 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_HALTED);
283 static inline bool is_ioat_suspended(unsigned long status)
285 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_SUSPENDED);
288 /* channel was fatally programmed */
289 static inline bool is_ioat_bug(unsigned long err)
294 int ioat_probe(struct ioatdma_device *device);
295 int ioat_register(struct ioatdma_device *device);
296 int ioat_dma_self_test(struct ioatdma_device *device);
297 void ioat_dma_remove(struct ioatdma_device *device);
298 struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase);
299 void ioat_init_channel(struct ioatdma_device *device,
300 struct ioatdma_chan *ioat_chan, int idx);
301 enum dma_status ioat_dma_tx_status(struct dma_chan *c, dma_cookie_t cookie,
302 struct dma_tx_state *txstate);
303 bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
304 dma_addr_t *phys_complete);
305 void ioat_kobject_add(struct ioatdma_device *device, struct kobj_type *type);
306 void ioat_kobject_del(struct ioatdma_device *device);
307 int ioat_dma_setup_interrupts(struct ioatdma_device *device);
308 void ioat_stop(struct ioatdma_chan *ioat_chan);
309 extern const struct sysfs_ops ioat_sysfs_ops;
310 extern struct ioat_sysfs_entry ioat_version_attr;
311 extern struct ioat_sysfs_entry ioat_cap_attr;
312 #endif /* IOATDMA_H */