dmaengine: ioatdma: clean up local dma channel data structure
[cascardo/linux.git] / drivers / dma / ioat / dma.h
1 /*
2  * Copyright(c) 2004 - 2009 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  * The full GNU General Public License is included in this distribution in the
15  * file called COPYING.
16  */
17 #ifndef IOATDMA_H
18 #define IOATDMA_H
19
20 #include <linux/dmaengine.h>
21 #include "hw.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>
27 #include <net/tcp.h>
28
29 #define IOAT_DMA_VERSION  "4.00"
30
31 #define IOAT_DMA_DCA_ANY_CPU            ~0
32
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)
36
37 #define chan_num(ch) ((int)((ch)->reg_base - (ch)->device->reg_base) / 0x80)
38
39 /*
40  * workaround for IOAT ver.3.0 null descriptor issue
41  * (channel returns error when size is 0)
42  */
43 #define NULL_DESC_BUFFER_SIZE 1
44
45 enum ioat_irq_mode {
46         IOAT_NOIRQ = 0,
47         IOAT_MSIX,
48         IOAT_MSI,
49         IOAT_INTX
50 };
51
52 /**
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
68  *
69  * Note: the v3 cleanup routine supports raid operations
70  */
71 struct ioatdma_device {
72         struct pci_dev *pdev;
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;
79         u8 version;
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;
84         u32 cap;
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);
91 };
92
93 struct ioatdma_chan {
94         struct dma_chan dma_chan;
95         void __iomem *reg_base;
96         dma_addr_t last_completion;
97         spinlock_t cleanup_lock;
98         unsigned long state;
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
104         #define IOAT_RUN 5
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;
112         u64 *completion;
113         struct tasklet_struct cleanup_task;
114         struct kobject kobj;
115
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)
126  */
127         size_t xfercap_log;
128         u16 head;
129         u16 issued;
130         u16 tail;
131         u16 dmacount;
132         u16 alloc_order;
133         u16 produce;
134         struct ioat_ring_ent **ring;
135         spinlock_t prep_lock;
136 };
137
138 struct ioat_sysfs_entry {
139         struct attribute attr;
140         ssize_t (*show)(struct dma_chan *, char *);
141 };
142
143 /**
144  * struct ioat_sed_ent - wrapper around super extended hardware descriptor
145  * @hw: hardware SED
146  * @sed_dma: dma address for the SED
147  * @list: list member
148  * @parent: point to the dma descriptor that's the parent
149  */
150 struct ioat_sed_ent {
151         struct ioat_sed_raw_descriptor *hw;
152         dma_addr_t dma;
153         struct ioat_ring_ent *parent;
154         unsigned int hw_pool;
155 };
156
157 static inline struct ioatdma_chan *to_ioat_chan(struct dma_chan *c)
158 {
159         return container_of(c, struct ioatdma_chan, dma_chan);
160 }
161
162
163
164 /* wrapper around hardware descriptor format + additional software fields */
165
166 #ifdef DEBUG
167 #define set_desc_id(desc, i) ((desc)->id = (i))
168 #define desc_id(desc) ((desc)->id)
169 #else
170 #define set_desc_id(desc, i)
171 #define desc_id(desc) (0)
172 #endif
173
174 static inline void
175 __dump_desc_dbg(struct ioatdma_chan *ioat_chan, struct ioat_dma_descriptor *hw,
176                 struct dma_async_tx_descriptor *tx, int id)
177 {
178         struct device *dev = to_dev(ioat_chan);
179
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);
185 }
186
187 #define dump_desc_dbg(c, d) \
188         ({ if (d) __dump_desc_dbg(c, d->hw, &d->txd, desc_id(d)); 0; })
189
190 static inline struct ioatdma_chan *
191 ioat_chan_by_index(struct ioatdma_device *device, int index)
192 {
193         return device->idx[index];
194 }
195
196 static inline u64 ioat_chansts_32(struct ioatdma_chan *ioat_chan)
197 {
198         u8 ver = ioat_chan->device->version;
199         u64 status;
200         u32 status_lo;
201
202         /* We need to read the low address first as this causes the
203          * chipset to latch the upper bits for the subsequent read
204          */
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));
207         status <<= 32;
208         status |= status_lo;
209
210         return status;
211 }
212
213 #if BITS_PER_LONG == 64
214
215 static inline u64 ioat_chansts(struct ioatdma_chan *ioat_chan)
216 {
217         u8 ver = ioat_chan->device->version;
218         u64 status;
219
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));
223         else
224                 status = ioat_chansts_32(ioat_chan);
225
226         return status;
227 }
228
229 #else
230 #define ioat_chansts ioat_chansts_32
231 #endif
232
233 static inline u64 ioat_chansts_to_addr(u64 status)
234 {
235         return status & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
236 }
237
238 static inline u32 ioat_chanerr(struct ioatdma_chan *ioat_chan)
239 {
240         return readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
241 }
242
243 static inline void ioat_suspend(struct ioatdma_chan *ioat_chan)
244 {
245         u8 ver = ioat_chan->device->version;
246
247         writeb(IOAT_CHANCMD_SUSPEND,
248                ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
249 }
250
251 static inline void ioat_reset(struct ioatdma_chan *ioat_chan)
252 {
253         u8 ver = ioat_chan->device->version;
254
255         writeb(IOAT_CHANCMD_RESET,
256                ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
257 }
258
259 static inline bool ioat_reset_pending(struct ioatdma_chan *ioat_chan)
260 {
261         u8 ver = ioat_chan->device->version;
262         u8 cmd;
263
264         cmd = readb(ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
265         return (cmd & IOAT_CHANCMD_RESET) == IOAT_CHANCMD_RESET;
266 }
267
268 static inline bool is_ioat_active(unsigned long status)
269 {
270         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_ACTIVE);
271 }
272
273 static inline bool is_ioat_idle(unsigned long status)
274 {
275         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_DONE);
276 }
277
278 static inline bool is_ioat_halted(unsigned long status)
279 {
280         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_HALTED);
281 }
282
283 static inline bool is_ioat_suspended(unsigned long status)
284 {
285         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_SUSPENDED);
286 }
287
288 /* channel was fatally programmed */
289 static inline bool is_ioat_bug(unsigned long err)
290 {
291         return !!err;
292 }
293
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 */