1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2006-2013 Solarflare Communications Inc.
12 #include <linux/spinlock.h>
14 /**************************************************************************
18 **************************************************************************
20 * Notes on locking strategy for the Falcon architecture:
22 * Many CSRs are very wide and cannot be read or written atomically.
23 * Writes from the host are buffered by the Bus Interface Unit (BIU)
24 * up to 128 bits. Whenever the host writes part of such a register,
25 * the BIU collects the written value and does not write to the
26 * underlying register until all 4 dwords have been written. A
27 * similar buffering scheme applies to host access to the NIC's 64-bit
30 * Writes to different CSRs and 64-bit SRAM words must be serialised,
31 * since interleaved access can result in lost writes. We use
32 * ef4_nic::biu_lock for this.
34 * We also serialise reads from 128-bit CSRs and SRAM with the same
35 * spinlock. This may not be necessary, but it doesn't really matter
36 * as there are no such reads on the fast path.
38 * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
39 * 128-bit but are special-cased in the BIU to avoid the need for
40 * locking in the host:
42 * - They are write-only.
43 * - The semantics of writing to these registers are such that
44 * replacing the low 96 bits with zero does not affect functionality.
45 * - If the host writes to the last dword address of such a register
46 * (i.e. the high 32 bits) the underlying register will always be
47 * written. If the collector and the current write together do not
48 * provide values for all 128 bits of the register, the low 96 bits
49 * will be written as zero.
50 * - If the host writes to the address of any other part of such a
51 * register while the collector already holds values for some other
52 * register, the write is discarded and the collector maintains its
55 * The EF10 architecture exposes very few registers to the host and
56 * most of them are only 32 bits wide. The only exceptions are the MC
57 * doorbell register pair, which has its own latching, and
58 * TX_DESC_UPD, which works in a similar way to the Falcon
62 #if BITS_PER_LONG == 64
63 #define EF4_USE_QWORD_IO 1
66 #ifdef EF4_USE_QWORD_IO
67 static inline void _ef4_writeq(struct ef4_nic
*efx
, __le64 value
,
70 __raw_writeq((__force u64
)value
, efx
->membase
+ reg
);
72 static inline __le64
_ef4_readq(struct ef4_nic
*efx
, unsigned int reg
)
74 return (__force __le64
)__raw_readq(efx
->membase
+ reg
);
78 static inline void _ef4_writed(struct ef4_nic
*efx
, __le32 value
,
81 __raw_writel((__force u32
)value
, efx
->membase
+ reg
);
83 static inline __le32
_ef4_readd(struct ef4_nic
*efx
, unsigned int reg
)
85 return (__force __le32
)__raw_readl(efx
->membase
+ reg
);
88 /* Write a normal 128-bit CSR, locking as appropriate. */
89 static inline void ef4_writeo(struct ef4_nic
*efx
, const ef4_oword_t
*value
,
92 unsigned long flags
__attribute__ ((unused
));
94 netif_vdbg(efx
, hw
, efx
->net_dev
,
95 "writing register %x with " EF4_OWORD_FMT
"\n", reg
,
96 EF4_OWORD_VAL(*value
));
98 spin_lock_irqsave(&efx
->biu_lock
, flags
);
99 #ifdef EF4_USE_QWORD_IO
100 _ef4_writeq(efx
, value
->u64
[0], reg
+ 0);
101 _ef4_writeq(efx
, value
->u64
[1], reg
+ 8);
103 _ef4_writed(efx
, value
->u32
[0], reg
+ 0);
104 _ef4_writed(efx
, value
->u32
[1], reg
+ 4);
105 _ef4_writed(efx
, value
->u32
[2], reg
+ 8);
106 _ef4_writed(efx
, value
->u32
[3], reg
+ 12);
108 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
111 /* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
112 static inline void ef4_sram_writeq(struct ef4_nic
*efx
, void __iomem
*membase
,
113 const ef4_qword_t
*value
, unsigned int index
)
115 unsigned int addr
= index
* sizeof(*value
);
116 unsigned long flags
__attribute__ ((unused
));
118 netif_vdbg(efx
, hw
, efx
->net_dev
,
119 "writing SRAM address %x with " EF4_QWORD_FMT
"\n",
120 addr
, EF4_QWORD_VAL(*value
));
122 spin_lock_irqsave(&efx
->biu_lock
, flags
);
123 #ifdef EF4_USE_QWORD_IO
124 __raw_writeq((__force u64
)value
->u64
[0], membase
+ addr
);
126 __raw_writel((__force u32
)value
->u32
[0], membase
+ addr
);
127 __raw_writel((__force u32
)value
->u32
[1], membase
+ addr
+ 4);
129 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
132 /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
133 static inline void ef4_writed(struct ef4_nic
*efx
, const ef4_dword_t
*value
,
136 netif_vdbg(efx
, hw
, efx
->net_dev
,
137 "writing register %x with "EF4_DWORD_FMT
"\n",
138 reg
, EF4_DWORD_VAL(*value
));
140 /* No lock required */
141 _ef4_writed(efx
, value
->u32
[0], reg
);
144 /* Read a 128-bit CSR, locking as appropriate. */
145 static inline void ef4_reado(struct ef4_nic
*efx
, ef4_oword_t
*value
,
148 unsigned long flags
__attribute__ ((unused
));
150 spin_lock_irqsave(&efx
->biu_lock
, flags
);
151 value
->u32
[0] = _ef4_readd(efx
, reg
+ 0);
152 value
->u32
[1] = _ef4_readd(efx
, reg
+ 4);
153 value
->u32
[2] = _ef4_readd(efx
, reg
+ 8);
154 value
->u32
[3] = _ef4_readd(efx
, reg
+ 12);
155 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
157 netif_vdbg(efx
, hw
, efx
->net_dev
,
158 "read from register %x, got " EF4_OWORD_FMT
"\n", reg
,
159 EF4_OWORD_VAL(*value
));
162 /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
163 static inline void ef4_sram_readq(struct ef4_nic
*efx
, void __iomem
*membase
,
164 ef4_qword_t
*value
, unsigned int index
)
166 unsigned int addr
= index
* sizeof(*value
);
167 unsigned long flags
__attribute__ ((unused
));
169 spin_lock_irqsave(&efx
->biu_lock
, flags
);
170 #ifdef EF4_USE_QWORD_IO
171 value
->u64
[0] = (__force __le64
)__raw_readq(membase
+ addr
);
173 value
->u32
[0] = (__force __le32
)__raw_readl(membase
+ addr
);
174 value
->u32
[1] = (__force __le32
)__raw_readl(membase
+ addr
+ 4);
176 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
178 netif_vdbg(efx
, hw
, efx
->net_dev
,
179 "read from SRAM address %x, got "EF4_QWORD_FMT
"\n",
180 addr
, EF4_QWORD_VAL(*value
));
183 /* Read a 32-bit CSR or SRAM */
184 static inline void ef4_readd(struct ef4_nic
*efx
, ef4_dword_t
*value
,
187 value
->u32
[0] = _ef4_readd(efx
, reg
);
188 netif_vdbg(efx
, hw
, efx
->net_dev
,
189 "read from register %x, got "EF4_DWORD_FMT
"\n",
190 reg
, EF4_DWORD_VAL(*value
));
193 /* Write a 128-bit CSR forming part of a table */
195 ef4_writeo_table(struct ef4_nic
*efx
, const ef4_oword_t
*value
,
196 unsigned int reg
, unsigned int index
)
198 ef4_writeo(efx
, value
, reg
+ index
* sizeof(ef4_oword_t
));
201 /* Read a 128-bit CSR forming part of a table */
202 static inline void ef4_reado_table(struct ef4_nic
*efx
, ef4_oword_t
*value
,
203 unsigned int reg
, unsigned int index
)
205 ef4_reado(efx
, value
, reg
+ index
* sizeof(ef4_oword_t
));
208 /* Page size used as step between per-VI registers */
209 #define EF4_VI_PAGE_SIZE 0x2000
211 /* Calculate offset to page-mapped register */
212 #define EF4_PAGED_REG(page, reg) \
213 ((page) * EF4_VI_PAGE_SIZE + (reg))
215 /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
216 static inline void _ef4_writeo_page(struct ef4_nic
*efx
, ef4_oword_t
*value
,
217 unsigned int reg
, unsigned int page
)
219 reg
= EF4_PAGED_REG(page
, reg
);
221 netif_vdbg(efx
, hw
, efx
->net_dev
,
222 "writing register %x with " EF4_OWORD_FMT
"\n", reg
,
223 EF4_OWORD_VAL(*value
));
225 #ifdef EF4_USE_QWORD_IO
226 _ef4_writeq(efx
, value
->u64
[0], reg
+ 0);
227 _ef4_writeq(efx
, value
->u64
[1], reg
+ 8);
229 _ef4_writed(efx
, value
->u32
[0], reg
+ 0);
230 _ef4_writed(efx
, value
->u32
[1], reg
+ 4);
231 _ef4_writed(efx
, value
->u32
[2], reg
+ 8);
232 _ef4_writed(efx
, value
->u32
[3], reg
+ 12);
235 #define ef4_writeo_page(efx, value, reg, page) \
236 _ef4_writeo_page(efx, value, \
238 BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
241 /* Write a page-mapped 32-bit CSR (EVQ_RPTR, EVQ_TMR (EF10), or the
242 * high bits of RX_DESC_UPD or TX_DESC_UPD)
245 _ef4_writed_page(struct ef4_nic
*efx
, const ef4_dword_t
*value
,
246 unsigned int reg
, unsigned int page
)
248 ef4_writed(efx
, value
, EF4_PAGED_REG(page
, reg
));
250 #define ef4_writed_page(efx, value, reg, page) \
251 _ef4_writed_page(efx, value, \
253 BUILD_BUG_ON_ZERO((reg) != 0x400 && \
261 /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
262 * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
263 * collector register.
265 static inline void _ef4_writed_page_locked(struct ef4_nic
*efx
,
266 const ef4_dword_t
*value
,
270 unsigned long flags
__attribute__ ((unused
));
273 spin_lock_irqsave(&efx
->biu_lock
, flags
);
274 ef4_writed(efx
, value
, EF4_PAGED_REG(page
, reg
));
275 spin_unlock_irqrestore(&efx
->biu_lock
, flags
);
277 ef4_writed(efx
, value
, EF4_PAGED_REG(page
, reg
));
280 #define ef4_writed_page_locked(efx, value, reg, page) \
281 _ef4_writed_page_locked(efx, value, \
282 reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
285 #endif /* EF4_IO_H */