1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #ifndef EFX_FALCON_IO_H
12 #define EFX_FALCON_IO_H
15 #include <linux/spinlock.h>
16 #include "net_driver.h"
18 /**************************************************************************
20 * Falcon hardware access
22 **************************************************************************
24 * Notes on locking strategy:
26 * Most Falcon registers require 16-byte (or 8-byte, for SRAM
27 * registers) atomic writes which necessitates locking.
28 * Under normal operation few writes to the Falcon BAR are made and these
29 * registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and TX_DESC_UPD_REG) are special
30 * cased to allow 4-byte (hence lockless) accesses.
32 * It *is* safe to write to these 4-byte registers in the middle of an
33 * access to an 8-byte or 16-byte register. We therefore use a
34 * spinlock to protect accesses to the larger registers, but no locks
35 * for the 4-byte registers.
37 * A write barrier is needed to ensure that DW3 is written after DW0/1/2
38 * due to the way the 16byte registers are "collected" in the Falcon BIU
40 * We also lock when carrying out reads, to ensure consistency of the
41 * data (made possible since the BIU reads all 128 bits into a cache).
42 * Reads are very rare, so this isn't a significant performance
43 * impact. (Most data transferred from NIC to host is DMAed directly
46 * I/O BAR access uses locks for both reads and writes (but is only provided
47 * for testing purposes).
50 /* Special buffer descriptors (Falcon SRAM) */
51 #define BUF_TBL_KER_A1 0x18000
52 #define BUF_TBL_KER_B0 0x800000
55 #if BITS_PER_LONG == 64
56 #define FALCON_USE_QWORD_IO 1
59 #ifdef FALCON_USE_QWORD_IO
60 static inline void _falcon_writeq(struct efx_nic
*efx
, __le64 value
,
63 __raw_writeq((__force u64
)value
, efx
->membase
+ reg
);
65 static inline __le64
_falcon_readq(struct efx_nic
*efx
, unsigned int reg
)
67 return (__force __le64
)__raw_readq(efx
->membase
+ reg
);
71 static inline void _falcon_writel(struct efx_nic
*efx
, __le32 value
,
74 __raw_writel((__force u32
)value
, efx
->membase
+ reg
);
76 static inline __le32
_falcon_readl(struct efx_nic
*efx
, unsigned int reg
)
78 return (__force __le32
)__raw_readl(efx
->membase
+ reg
);
81 /* Writes to a normal 16-byte Falcon register, locking as appropriate. */
82 static inline void falcon_write(struct efx_nic
*efx
, efx_oword_t
*value
,
87 EFX_REGDUMP(efx
, "writing register %x with " EFX_OWORD_FMT
"\n", reg
,
88 EFX_OWORD_VAL(*value
));
90 spin_lock_irqsave(&efx
->biu_lock
, flags
);
91 #ifdef FALCON_USE_QWORD_IO
92 _falcon_writeq(efx
, value
->u64
[0], reg
+ 0);
94 _falcon_writeq(efx
, value
->u64
[1], reg
+ 8);
96 _falcon_writel(efx
, value
->u32
[0], reg
+ 0);
97 _falcon_writel(efx
, value
->u32
[1], reg
+ 4);
98 _falcon_writel(efx
, value
->u32
[2], reg
+ 8);
100 _falcon_writel(efx
, value
->u32
[3], reg
+ 12);
103 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
106 /* Writes to an 8-byte Falcon SRAM register, locking as appropriate. */
107 static inline void falcon_write_sram(struct efx_nic
*efx
, efx_qword_t
*value
,
110 unsigned int reg
= efx
->type
->buf_tbl_base
+ (index
* sizeof(*value
));
113 EFX_REGDUMP(efx
, "writing SRAM register %x with " EFX_QWORD_FMT
"\n",
114 reg
, EFX_QWORD_VAL(*value
));
116 spin_lock_irqsave(&efx
->biu_lock
, flags
);
117 #ifdef FALCON_USE_QWORD_IO
118 _falcon_writeq(efx
, value
->u64
[0], reg
+ 0);
120 _falcon_writel(efx
, value
->u32
[0], reg
+ 0);
122 _falcon_writel(efx
, value
->u32
[1], reg
+ 4);
125 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
128 /* Write dword to Falcon register that allows partial writes
130 * Some Falcon registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and
131 * TX_DESC_UPD_REG) can be written to as a single dword. This allows
132 * for lockless writes.
134 static inline void falcon_writel(struct efx_nic
*efx
, efx_dword_t
*value
,
137 EFX_REGDUMP(efx
, "writing partial register %x with "EFX_DWORD_FMT
"\n",
138 reg
, EFX_DWORD_VAL(*value
));
140 /* No lock required */
141 _falcon_writel(efx
, value
->u32
[0], reg
);
144 /* Read from a Falcon register
146 * This reads an entire 16-byte Falcon register in one go, locking as
147 * appropriate. It is essential to read the first dword first, as this
148 * prompts Falcon to load the current value into the shadow register.
150 static inline void falcon_read(struct efx_nic
*efx
, efx_oword_t
*value
,
155 spin_lock_irqsave(&efx
->biu_lock
, flags
);
156 value
->u32
[0] = _falcon_readl(efx
, reg
+ 0);
158 value
->u32
[1] = _falcon_readl(efx
, reg
+ 4);
159 value
->u32
[2] = _falcon_readl(efx
, reg
+ 8);
160 value
->u32
[3] = _falcon_readl(efx
, reg
+ 12);
161 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
163 EFX_REGDUMP(efx
, "read from register %x, got " EFX_OWORD_FMT
"\n", reg
,
164 EFX_OWORD_VAL(*value
));
167 /* This reads an 8-byte Falcon SRAM entry in one go. */
168 static inline void falcon_read_sram(struct efx_nic
*efx
, efx_qword_t
*value
,
171 unsigned int reg
= efx
->type
->buf_tbl_base
+ (index
* sizeof(*value
));
174 spin_lock_irqsave(&efx
->biu_lock
, flags
);
175 #ifdef FALCON_USE_QWORD_IO
176 value
->u64
[0] = _falcon_readq(efx
, reg
+ 0);
178 value
->u32
[0] = _falcon_readl(efx
, reg
+ 0);
180 value
->u32
[1] = _falcon_readl(efx
, reg
+ 4);
182 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
184 EFX_REGDUMP(efx
, "read from SRAM register %x, got "EFX_QWORD_FMT
"\n",
185 reg
, EFX_QWORD_VAL(*value
));
188 /* Read dword from Falcon register that allows partial writes (sic) */
189 static inline void falcon_readl(struct efx_nic
*efx
, efx_dword_t
*value
,
192 value
->u32
[0] = _falcon_readl(efx
, reg
);
193 EFX_REGDUMP(efx
, "read from register %x, got "EFX_DWORD_FMT
"\n",
194 reg
, EFX_DWORD_VAL(*value
));
197 /* Write to a register forming part of a table */
198 static inline void falcon_write_table(struct efx_nic
*efx
, efx_oword_t
*value
,
199 unsigned int reg
, unsigned int index
)
201 falcon_write(efx
, value
, reg
+ index
* sizeof(efx_oword_t
));
204 /* Read to a register forming part of a table */
205 static inline void falcon_read_table(struct efx_nic
*efx
, efx_oword_t
*value
,
206 unsigned int reg
, unsigned int index
)
208 falcon_read(efx
, value
, reg
+ index
* sizeof(efx_oword_t
));
211 /* Write to a dword register forming part of a table */
212 static inline void falcon_writel_table(struct efx_nic
*efx
, efx_dword_t
*value
,
213 unsigned int reg
, unsigned int index
)
215 falcon_writel(efx
, value
, reg
+ index
* sizeof(efx_oword_t
));
218 /* Page-mapped register block size */
219 #define FALCON_PAGE_BLOCK_SIZE 0x2000
221 /* Calculate offset to page-mapped register block */
222 #define FALCON_PAGED_REG(page, reg) \
223 ((page) * FALCON_PAGE_BLOCK_SIZE + (reg))
225 /* As for falcon_write(), but for a page-mapped register. */
226 static inline void falcon_write_page(struct efx_nic
*efx
, efx_oword_t
*value
,
227 unsigned int reg
, unsigned int page
)
229 falcon_write(efx
, value
, FALCON_PAGED_REG(page
, reg
));
232 /* As for falcon_writel(), but for a page-mapped register. */
233 static inline void falcon_writel_page(struct efx_nic
*efx
, efx_dword_t
*value
,
234 unsigned int reg
, unsigned int page
)
236 falcon_writel(efx
, value
, FALCON_PAGED_REG(page
, reg
));
239 /* Write dword to Falcon page-mapped register with an extra lock.
241 * As for falcon_writel_page(), but for a register that suffers from
242 * SFC bug 3181. Take out a lock so the BIU collector cannot be
244 static inline void falcon_writel_page_locked(struct efx_nic
*efx
,
251 spin_lock_irqsave(&efx
->biu_lock
, flags
);
252 falcon_writel(efx
, value
, FALCON_PAGED_REG(page
, reg
));
253 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
256 #endif /* EFX_FALCON_IO_H */