blob: 11bbcf27f86ff9fa281f3fe23e5bad1cd10e0862 [file] [log] [blame]
Chris Leech0bbd5f42006-05-23 17:35:34 -07001/*
Maciej Sosnowski211a22c2009-02-26 11:05:43 +01002 * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
Chris Leech0bbd5f42006-05-23 17:35:34 -07003 *
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 *
Chris Leech0bbd5f42006-05-23 17:35:34 -070014 * 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>
Dan Williams584ec222009-07-28 14:32:12 -070021#include "hw.h"
Dan Williams09c8a5b2009-09-08 12:01:49 -070022#include "registers.h"
Chris Leech0bbd5f42006-05-23 17:35:34 -070023#include <linux/init.h>
24#include <linux/dmapool.h>
25#include <linux/cache.h>
David S. Miller57c651f2006-05-23 17:39:49 -070026#include <linux/pci_ids.h>
Maciej Sosnowski16a37ac2008-07-22 17:30:57 -070027#include <net/tcp.h>
Chris Leech0bbd5f42006-05-23 17:35:34 -070028
Dan Williams3208ca52009-09-10 11:27:36 -070029#define IOAT_DMA_VERSION "4.00"
Shannon Nelson5149fd02007-10-18 03:07:13 -070030
Shannon Nelson7bb67c12007-11-14 16:59:51 -080031#define IOAT_DMA_DCA_ANY_CPU ~0
32
Dave Jiang55f878e2015-08-11 08:48:27 -070033#define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, dma_dev)
34#define to_dev(ioat_chan) (&(ioat_chan)->ioat_dma->pdev->dev)
35#define to_pdev(ioat_chan) ((ioat_chan)->ioat_dma->pdev)
Dan Williams1f27adc22009-09-08 17:29:02 -070036
Dave Jiang55f878e2015-08-11 08:48:27 -070037#define chan_num(ch) ((int)((ch)->reg_base - (ch)->ioat_dma->reg_base) / 0x80)
Dan Williams1f27adc22009-09-08 17:29:02 -070038
Dan Williams1f27adc22009-09-08 17:29:02 -070039/*
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
Dave Jiang8a52b9f2013-03-26 15:42:47 -070045enum ioat_irq_mode {
46 IOAT_NOIRQ = 0,
47 IOAT_MSIX,
Dave Jiang8a52b9f2013-03-26 15:42:47 -070048 IOAT_MSI,
49 IOAT_INTX
50};
51
Chris Leech0bbd5f42006-05-23 17:35:34 -070052/**
Shannon Nelson8ab89562007-10-16 01:27:39 -070053 * struct ioatdma_device - internal representation of a IOAT device
Chris Leech0bbd5f42006-05-23 17:35:34 -070054 * @pdev: PCI-Express device
55 * @reg_base: MMIO register space base address
56 * @dma_pool: for allocating DMA descriptors
Dave Jiang55f878e2015-08-11 08:48:27 -070057 * @dma_dev: embedded struct dma_device
Shannon Nelson8ab89562007-10-16 01:27:39 -070058 * @version: version of ioatdma device
Shannon Nelson7bb67c12007-11-14 16:59:51 -080059 * @msix_entries: irq handlers
60 * @idx: per channel data
Dan Williamsf2427e22009-07-28 14:42:38 -070061 * @dca: direct cache access context
62 * @intr_quirk: interrupt setup quirk (for ioat_v1 devices)
Dan Williams5cbafa62009-08-26 13:01:44 -070063 * @enumerate_channels: hw version specific channel enumeration
Dan Williamsa6d52d72009-12-19 15:36:02 -070064 * @reset_hw: hw version specific channel (re)initialization
Dan Williamsaa4d72a2010-03-03 21:21:13 -070065 * @cleanup_fn: select between the v2 and v3 cleanup routines
Dan Williamsbf40a682009-09-08 17:42:55 -070066 * @timer_fn: select between the v2 and v3 timer watchdog routines
Dan Williams9de6fc72009-09-08 17:42:58 -070067 * @self_test: hardware version specific self test for each supported op type
Dan Williamsbf40a682009-09-08 17:42:55 -070068 *
69 * Note: the v3 cleanup routine supports raid operations
Chris Leech0bbd5f42006-05-23 17:35:34 -070070 */
Shannon Nelson8ab89562007-10-16 01:27:39 -070071struct ioatdma_device {
Chris Leech0bbd5f42006-05-23 17:35:34 -070072 struct pci_dev *pdev;
Al Viro47b16532006-10-10 22:45:47 +010073 void __iomem *reg_base;
Chris Leech0bbd5f42006-05-23 17:35:34 -070074 struct pci_pool *dma_pool;
75 struct pci_pool *completion_pool;
Dave Jiang7727eaa2013-04-15 10:25:56 -070076#define MAX_SED_POOLS 5
77 struct dma_pool *sed_hw_pool[MAX_SED_POOLS];
Dave Jiang55f878e2015-08-11 08:48:27 -070078 struct dma_device dma_dev;
Shannon Nelson8ab89562007-10-16 01:27:39 -070079 u8 version;
Shannon Nelson3e037452007-10-16 01:27:40 -070080 struct msix_entry msix_entries[4];
Dave Jiang5a976882015-08-11 08:48:21 -070081 struct ioatdma_chan *idx[4];
Dan Williamsf2427e22009-07-28 14:42:38 -070082 struct dca_provider *dca;
Dave Jiang8a52b9f2013-03-26 15:42:47 -070083 enum ioat_irq_mode irq_mode;
Dave Jiang75c6f0a2013-04-10 16:44:39 -070084 u32 cap;
Dave Jiang55f878e2015-08-11 08:48:27 -070085 void (*intr_quirk)(struct ioatdma_device *ioat_dma);
86 int (*enumerate_channels)(struct ioatdma_device *ioat_dma);
Dave Jiang5a976882015-08-11 08:48:21 -070087 int (*reset_hw)(struct ioatdma_chan *ioat_chan);
Dan Williamsaa4d72a2010-03-03 21:21:13 -070088 void (*cleanup_fn)(unsigned long data);
Dan Williamsbf40a682009-09-08 17:42:55 -070089 void (*timer_fn)(unsigned long data);
Dave Jiang55f878e2015-08-11 08:48:27 -070090 int (*self_test)(struct ioatdma_device *ioat_dma);
Chris Leech0bbd5f42006-05-23 17:35:34 -070091};
92
Dave Jiang5a976882015-08-11 08:48:21 -070093struct ioatdma_chan {
94 struct dma_chan dma_chan;
Al Viro47b16532006-10-10 22:45:47 +010095 void __iomem *reg_base;
Dan Williams27502932012-03-23 13:36:42 -070096 dma_addr_t last_completion;
Chris Leech0bbd5f42006-05-23 17:35:34 -070097 spinlock_t cleanup_lock;
Dan Williams09c8a5b2009-09-08 12:01:49 -070098 unsigned long state;
99 #define IOAT_COMPLETION_PENDING 0
100 #define IOAT_COMPLETION_ACK 1
101 #define IOAT_RESET_PENDING 2
Dan Williams5669e312009-09-08 17:42:56 -0700102 #define IOAT_KOBJ_INIT_FAIL 3
Dan Williams074cc472010-05-01 15:22:55 -0700103 #define IOAT_RESHAPE_PENDING 4
Dan Williams556ab452010-07-23 15:47:56 -0700104 #define IOAT_RUN 5
Dave Jiang4dec23d2013-02-07 14:38:32 -0700105 #define IOAT_CHAN_ACTIVE 6
Dan Williams09c8a5b2009-09-08 12:01:49 -0700106 struct timer_list timer;
107 #define COMPLETION_TIMEOUT msecs_to_jiffies(100)
Dan Williamsa3092182009-09-08 12:02:01 -0700108 #define IDLE_TIMEOUT msecs_to_jiffies(2000)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700109 #define RESET_DELAY msecs_to_jiffies(100)
Dave Jiang55f878e2015-08-11 08:48:27 -0700110 struct ioatdma_device *ioat_dma;
Dan Williams4fb9b9e2009-09-08 12:01:04 -0700111 dma_addr_t completion_dma;
112 u64 *completion;
Shannon Nelson3e037452007-10-16 01:27:40 -0700113 struct tasklet_struct cleanup_task;
Dan Williams5669e312009-09-08 17:42:56 -0700114 struct kobject kobj;
Dave Jiang5a976882015-08-11 08:48:21 -0700115
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;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700136};
137
Dan Williams5669e312009-09-08 17:42:56 -0700138struct ioat_sysfs_entry {
139 struct attribute attr;
140 ssize_t (*show)(struct dma_chan *, char *);
141};
Dan Williams5cbafa62009-08-26 13:01:44 -0700142
Dan Williamsdcbc8532009-07-28 14:44:50 -0700143/**
Dave Jiang7727eaa2013-04-15 10:25:56 -0700144 * 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 */
150struct 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
Dave Jiang5a976882015-08-11 08:48:21 -0700157static inline struct ioatdma_chan *to_ioat_chan(struct dma_chan *c)
Dan Williamsdcbc8532009-07-28 14:44:50 -0700158{
Dave Jiang5a976882015-08-11 08:48:21 -0700159 return container_of(c, struct ioatdma_chan, dma_chan);
Dan Williamsdcbc8532009-07-28 14:44:50 -0700160}
161
Dave Jiang5a976882015-08-11 08:48:21 -0700162
163
Chris Leech0bbd5f42006-05-23 17:35:34 -0700164/* wrapper around hardware descriptor format + additional software fields */
165
Dan Williams6df91832009-09-08 12:00:55 -0700166#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
174static inline void
Dave Jiang5a976882015-08-11 08:48:21 -0700175__dump_desc_dbg(struct ioatdma_chan *ioat_chan, struct ioat_dma_descriptor *hw,
Dan Williams6df91832009-09-08 12:00:55 -0700176 struct dma_async_tx_descriptor *tx, int id)
177{
Dave Jiang5a976882015-08-11 08:48:21 -0700178 struct device *dev = to_dev(ioat_chan);
Dan Williams6df91832009-09-08 12:00:55 -0700179
180 dev_dbg(dev, "desc[%d]: (%#llx->%#llx) cookie: %d flags: %#x"
Dave Jiang50f9f972013-03-04 10:59:54 -0700181 " ctl: %#10.8x (op: %#x int_en: %d compl: %d)\n", id,
Dan Williams6df91832009-09-08 12:00:55 -0700182 (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) \
Dave Jiang5a976882015-08-11 08:48:21 -0700188 ({ if (d) __dump_desc_dbg(c, d->hw, &d->txd, desc_id(d)); 0; })
Dan Williams6df91832009-09-08 12:00:55 -0700189
Dave Jiang5a976882015-08-11 08:48:21 -0700190static inline struct ioatdma_chan *
Dave Jiang55f878e2015-08-11 08:48:27 -0700191ioat_chan_by_index(struct ioatdma_device *ioat_dma, int index)
Dan Williams5cbafa62009-08-26 13:01:44 -0700192{
Dave Jiang55f878e2015-08-11 08:48:27 -0700193 return ioat_dma->idx[index];
Dan Williams5cbafa62009-08-26 13:01:44 -0700194}
195
Dave Jiang5a976882015-08-11 08:48:21 -0700196static inline u64 ioat_chansts_32(struct ioatdma_chan *ioat_chan)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700197{
Dave Jiang55f878e2015-08-11 08:48:27 -0700198 u8 ver = ioat_chan->ioat_dma->version;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700199 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 */
Dave Jiang5a976882015-08-11 08:48:21 -0700205 status_lo = readl(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET_LOW(ver));
206 status = readl(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET_HIGH(ver));
Dan Williams09c8a5b2009-09-08 12:01:49 -0700207 status <<= 32;
208 status |= status_lo;
209
210 return status;
211}
212
Dave Jiangd92a8d72013-03-26 15:42:41 -0700213#if BITS_PER_LONG == 64
214
Dave Jiang5a976882015-08-11 08:48:21 -0700215static inline u64 ioat_chansts(struct ioatdma_chan *ioat_chan)
Dave Jiangd92a8d72013-03-26 15:42:41 -0700216{
Dave Jiang55f878e2015-08-11 08:48:27 -0700217 u8 ver = ioat_chan->ioat_dma->version;
Dave Jiangd92a8d72013-03-26 15:42:41 -0700218 u64 status;
219
220 /* With IOAT v3.3 the status register is 64bit. */
221 if (ver >= IOAT_VER_3_3)
Dave Jiang5a976882015-08-11 08:48:21 -0700222 status = readq(ioat_chan->reg_base + IOAT_CHANSTS_OFFSET(ver));
Dave Jiangd92a8d72013-03-26 15:42:41 -0700223 else
Dave Jiang5a976882015-08-11 08:48:21 -0700224 status = ioat_chansts_32(ioat_chan);
Dave Jiangd92a8d72013-03-26 15:42:41 -0700225
226 return status;
227}
228
229#else
230#define ioat_chansts ioat_chansts_32
231#endif
232
Dan Williams09c8a5b2009-09-08 12:01:49 -0700233static inline u64 ioat_chansts_to_addr(u64 status)
234{
235 return status & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
236}
237
Dave Jiang5a976882015-08-11 08:48:21 -0700238static inline u32 ioat_chanerr(struct ioatdma_chan *ioat_chan)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700239{
Dave Jiang5a976882015-08-11 08:48:21 -0700240 return readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
Dan Williams09c8a5b2009-09-08 12:01:49 -0700241}
242
Dave Jiang5a976882015-08-11 08:48:21 -0700243static inline void ioat_suspend(struct ioatdma_chan *ioat_chan)
Dan Williams09c8a5b2009-09-08 12:01:49 -0700244{
Dave Jiang55f878e2015-08-11 08:48:27 -0700245 u8 ver = ioat_chan->ioat_dma->version;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700246
Dave Jiang5a976882015-08-11 08:48:21 -0700247 writeb(IOAT_CHANCMD_SUSPEND,
248 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
Dan Williams09c8a5b2009-09-08 12:01:49 -0700249}
250
Dave Jiang5a976882015-08-11 08:48:21 -0700251static inline void ioat_reset(struct ioatdma_chan *ioat_chan)
Dan Williamsa6d52d72009-12-19 15:36:02 -0700252{
Dave Jiang55f878e2015-08-11 08:48:27 -0700253 u8 ver = ioat_chan->ioat_dma->version;
Dan Williamsa6d52d72009-12-19 15:36:02 -0700254
Dave Jiang5a976882015-08-11 08:48:21 -0700255 writeb(IOAT_CHANCMD_RESET,
256 ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
Dan Williamsa6d52d72009-12-19 15:36:02 -0700257}
258
Dave Jiang5a976882015-08-11 08:48:21 -0700259static inline bool ioat_reset_pending(struct ioatdma_chan *ioat_chan)
Dan Williamsa6d52d72009-12-19 15:36:02 -0700260{
Dave Jiang55f878e2015-08-11 08:48:27 -0700261 u8 ver = ioat_chan->ioat_dma->version;
Dan Williamsa6d52d72009-12-19 15:36:02 -0700262 u8 cmd;
263
Dave Jiang5a976882015-08-11 08:48:21 -0700264 cmd = readb(ioat_chan->reg_base + IOAT_CHANCMD_OFFSET(ver));
Dan Williamsa6d52d72009-12-19 15:36:02 -0700265 return (cmd & IOAT_CHANCMD_RESET) == IOAT_CHANCMD_RESET;
266}
267
Dan Williams09c8a5b2009-09-08 12:01:49 -0700268static inline bool is_ioat_active(unsigned long status)
269{
270 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_ACTIVE);
271}
272
273static inline bool is_ioat_idle(unsigned long status)
274{
275 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_DONE);
276}
277
278static inline bool is_ioat_halted(unsigned long status)
279{
280 return ((status & IOAT_CHANSTS_STATUS) == IOAT_CHANSTS_HALTED);
281}
282
283static 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 */
289static inline bool is_ioat_bug(unsigned long err)
290{
Dan Williamsb57014d2009-11-19 17:10:07 -0700291 return !!err;
Dan Williams09c8a5b2009-09-08 12:01:49 -0700292}
293
Dave Jiang55f878e2015-08-11 08:48:27 -0700294int ioat_probe(struct ioatdma_device *ioat_dma);
295int ioat_register(struct ioatdma_device *ioat_dma);
296int ioat_dma_self_test(struct ioatdma_device *ioat_dma);
297void ioat_dma_remove(struct ioatdma_device *ioat_dma);
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800298struct dca_provider *ioat_dca_init(struct pci_dev *pdev, void __iomem *iobase);
Dave Jiang55f878e2015-08-11 08:48:27 -0700299void ioat_init_channel(struct ioatdma_device *ioat_dma,
Dave Jiang5a976882015-08-11 08:48:21 -0700300 struct ioatdma_chan *ioat_chan, int idx);
Linus Walleij07934482010-03-26 16:50:49 -0700301enum dma_status ioat_dma_tx_status(struct dma_chan *c, dma_cookie_t cookie,
302 struct dma_tx_state *txstate);
Dave Jiang5a976882015-08-11 08:48:21 -0700303bool ioat_cleanup_preamble(struct ioatdma_chan *ioat_chan,
Dan Williams27502932012-03-23 13:36:42 -0700304 dma_addr_t *phys_complete);
Dave Jiang55f878e2015-08-11 08:48:27 -0700305void ioat_kobject_add(struct ioatdma_device *ioat_dma, struct kobj_type *type);
306void ioat_kobject_del(struct ioatdma_device *ioat_dma);
307int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma);
Dave Jiang5a976882015-08-11 08:48:21 -0700308void ioat_stop(struct ioatdma_chan *ioat_chan);
Emese Revfy52cf25d2010-01-19 02:58:23 +0100309extern const struct sysfs_ops ioat_sysfs_ops;
Dan Williams5669e312009-09-08 17:42:56 -0700310extern struct ioat_sysfs_entry ioat_version_attr;
311extern struct ioat_sysfs_entry ioat_cap_attr;
Chris Leech0bbd5f42006-05-23 17:35:34 -0700312#endif /* IOATDMA_H */