1e96eaa29068d79dddd8dc1d00c8a642f9c3fd56
[firefly-linux-kernel-4.4.55.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 ioat_chan_common *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 ioat_chan_common *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 ioat_chan_common {
94         struct dma_chan common;
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
117 struct ioat_sysfs_entry {
118         struct attribute attr;
119         ssize_t (*show)(struct dma_chan *, char *);
120 };
121
122 /**
123  * struct ioat_sed_ent - wrapper around super extended hardware descriptor
124  * @hw: hardware SED
125  * @sed_dma: dma address for the SED
126  * @list: list member
127  * @parent: point to the dma descriptor that's the parent
128  */
129 struct ioat_sed_ent {
130         struct ioat_sed_raw_descriptor *hw;
131         dma_addr_t dma;
132         struct ioat_ring_ent *parent;
133         unsigned int hw_pool;
134 };
135
136 static inline struct ioat_chan_common *to_chan_common(struct dma_chan *c)
137 {
138         return container_of(c, struct ioat_chan_common, common);
139 }
140
141 /* wrapper around hardware descriptor format + additional software fields */
142
143 #ifdef DEBUG
144 #define set_desc_id(desc, i) ((desc)->id = (i))
145 #define desc_id(desc) ((desc)->id)
146 #else
147 #define set_desc_id(desc, i)
148 #define desc_id(desc) (0)
149 #endif
150
151 static inline void
152 __dump_desc_dbg(struct ioat_chan_common *chan, struct ioat_dma_descriptor *hw,
153                 struct dma_async_tx_descriptor *tx, int id)
154 {
155         struct device *dev = to_dev(chan);
156
157         dev_dbg(dev, "desc[%d]: (%#llx->%#llx) cookie: %d flags: %#x"
158                 " ctl: %#10.8x (op: %#x int_en: %d compl: %d)\n", id,
159                 (unsigned long long) tx->phys,
160                 (unsigned long long) hw->next, tx->cookie, tx->flags,
161                 hw->ctl, hw->ctl_f.op, hw->ctl_f.int_en, hw->ctl_f.compl_write);
162 }
163
164 #define dump_desc_dbg(c, d) \
165         ({ if (d) __dump_desc_dbg(&c->base, d->hw, &d->txd, desc_id(d)); 0; })
166
167 static inline struct ioat_chan_common *
168 ioat_chan_by_index(struct ioatdma_device *device, int index)
169 {
170         return device->idx[index];
171 }
172
173 static inline u64 ioat_chansts_32(struct ioat_chan_common *chan)
174 {
175         u8 ver = chan->device->version;
176         u64 status;
177         u32 status_lo;
178
179         /* We need to read the low address first as this causes the
180          * chipset to latch the upper bits for the subsequent read
181          */
182         status_lo = readl(chan->reg_base + IOAT_CHANSTS_OFFSET_LOW(ver));
183         status = readl(chan->reg_base + IOAT_CHANSTS_OFFSET_HIGH(ver));
184         status <<= 32;
185         status |= status_lo;
186
187         return status;
188 }
189
190 #if BITS_PER_LONG == 64
191
192 static inline u64 ioat_chansts(struct ioat_chan_common *chan)
193 {
194         u8 ver = chan->device->version;
195         u64 status;
196
197          /* With IOAT v3.3 the status register is 64bit.  */
198         if (ver >= IOAT_VER_3_3)
199                 status = readq(chan->reg_base + IOAT_CHANSTS_OFFSET(ver));
200         else
201                 status = ioat_chansts_32(chan);
202
203         return status;
204 }
205
206 #else
207 #define ioat_chansts ioat_chansts_32
208 #endif
209
210 static inline u64 ioat_chansts_to_addr(u64 status)
211 {
212         return status & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
213 }
214
215 static inline u32 ioat_chanerr(struct ioat_chan_common *chan)
216 {
217         return readl(chan->reg_base + IOAT_CHANERR_OFFSET);
218 }
219
220 static inline void ioat_suspend(struct ioat_chan_common *chan)
221 {
222         u8 ver = chan->device->version;
223
224         writeb(IOAT_CHANCMD_SUSPEND, chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
225 }
226
227 static inline void ioat_reset(struct ioat_chan_common *chan)
228 {
229         u8 ver = chan->device->version;
230
231         writeb(IOAT_CHANCMD_RESET, chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
232 }
233
234 static inline bool ioat_reset_pending(struct ioat_chan_common *chan)
235 {
236         u8 ver = chan->device->version;
237         u8 cmd;
238
239         cmd = readb(chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
240         return (cmd & IOAT_CHANCMD_RESET) == IOAT_CHANCMD_RESET;
241 }
242
243 static inline bool is_ioat_active(unsigned long status)
244 {
245         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_ACTIVE);
246 }
247
248 static inline bool is_ioat_idle(unsigned long status)
249 {
250         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_DONE);
251 }
252
253 static inline bool is_ioat_halted(unsigned long status)
254 {
255         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_HALTED);
256 }
257
258 static inline bool is_ioat_suspended(unsigned long status)
259 {
260         return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_SUSPENDED);
261 }
262
263 /* channel was fatally programmed */
264 static inline bool is_ioat_bug(unsigned long err)
265 {
266         return !!err;
267 }
268
269 int ioat_probe(struct ioatdma_device *device);
270 int ioat_register(struct ioatdma_device *device);
271 int ioat_dma_self_test(struct ioatdma_device *device);
272 void ioat_dma_remove(struct ioatdma_device *device);
273 struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase);
274 void ioat_init_channel(struct ioatdma_device *device,
275                        struct ioat_chan_common *chan, int idx);
276 enum dma_status ioat_dma_tx_status(struct dma_chan *c, dma_cookie_t cookie,
277                                    struct dma_tx_state *txstate);
278 bool ioat_cleanup_preamble(struct ioat_chan_common *chan,
279                            dma_addr_t *phys_complete);
280 void ioat_kobject_add(struct ioatdma_device *device, struct kobj_type *type);
281 void ioat_kobject_del(struct ioatdma_device *device);
282 int ioat_dma_setup_interrupts(struct ioatdma_device *device);
283 void ioat_stop(struct ioat_chan_common *chan);
284 extern const struct sysfs_ops ioat_sysfs_ops;
285 extern struct ioat_sysfs_entry ioat_version_attr;
286 extern struct ioat_sysfs_entry ioat_cap_attr;
287 #endif /* IOATDMA_H */