1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2010 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.
15 #include <linux/spinlock.h>
17 /**************************************************************************
21 **************************************************************************
23 * Notes on locking strategy:
25 * Most CSRs are 128-bit (oword) and therefore cannot be read or
26 * written atomically. Access from the host is buffered by the Bus
27 * Interface Unit (BIU). Whenever the host reads from the lowest
28 * address of such a register, or from the address of a different such
29 * register, the BIU latches the register's value. Subsequent reads
30 * from higher addresses of the same register will read the latched
31 * value. Whenever the host writes part of such a register, the BIU
32 * collects the written value and does not write to the underlying
33 * register until all 4 dwords have been written. A similar buffering
34 * scheme applies to host access to the NIC's 64-bit SRAM.
36 * Access to different CSRs and 64-bit SRAM words must be serialised,
37 * since interleaved access can result in lost writes or lost
38 * information from read-to-clear fields. We use efx_nic::biu_lock
39 * for this. (We could use separate locks for read and write, but
40 * this is not normally a performance bottleneck.)
42 * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
43 * 128-bit but are special-cased in the BIU to avoid the need for
44 * locking in the host:
46 * - They are write-only.
47 * - The semantics of writing to these registers are such that
48 * replacing the low 96 bits with zero does not affect functionality.
49 * - If the host writes to the last dword address of such a register
50 * (i.e. the high 32 bits) the underlying register will always be
51 * written. If the collector and the current write together do not
52 * provide values for all 128 bits of the register, the low 96 bits
53 * will be written as zero.
54 * - If the host writes to the address of any other part of such a
55 * register while the collector already holds values for some other
56 * register, the write is discarded and the collector maintains its
60 #if BITS_PER_LONG == 64
61 #define EFX_USE_QWORD_IO 1
64 #ifdef EFX_USE_QWORD_IO
65 static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
68 __raw_writeq((__force u64)value, efx->membase + reg);
70 static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
72 return (__force __le64)__raw_readq(efx->membase + reg);
76 static inline void _efx_writed(struct efx_nic *efx, __le32 value,
79 __raw_writel((__force u32)value, efx->membase + reg);
81 static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
83 return (__force __le32)__raw_readl(efx->membase + reg);
86 /* Write a normal 128-bit CSR, locking as appropriate. */
87 static inline void efx_writeo(struct efx_nic *efx, efx_oword_t *value,
90 unsigned long flags __attribute__ ((unused));
92 netif_vdbg(efx, hw, efx->net_dev,
93 "writing register %x with " EFX_OWORD_FMT "\n", reg,
94 EFX_OWORD_VAL(*value));
96 spin_lock_irqsave(&efx->biu_lock, flags);
97 #ifdef EFX_USE_QWORD_IO
98 _efx_writeq(efx, value->u64[0], reg + 0);
99 _efx_writeq(efx, value->u64[1], reg + 8);
101 _efx_writed(efx, value->u32[0], reg + 0);
102 _efx_writed(efx, value->u32[1], reg + 4);
103 _efx_writed(efx, value->u32[2], reg + 8);
104 _efx_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 efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
113 efx_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 " EFX_QWORD_FMT "\n",
120 addr, EFX_QWORD_VAL(*value));
122 spin_lock_irqsave(&efx->biu_lock, flags);
123 #ifdef EFX_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);
131 spin_unlock_irqrestore(&efx->biu_lock, flags);
134 /* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
135 static inline void efx_writed(struct efx_nic *efx, efx_dword_t *value,
138 netif_vdbg(efx, hw, efx->net_dev,
139 "writing register %x with "EFX_DWORD_FMT"\n",
140 reg, EFX_DWORD_VAL(*value));
142 /* No lock required */
143 _efx_writed(efx, value->u32[0], reg);
147 /* Read a 128-bit CSR, locking as appropriate. */
148 static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
151 unsigned long flags __attribute__ ((unused));
153 spin_lock_irqsave(&efx->biu_lock, flags);
154 value->u32[0] = _efx_readd(efx, reg + 0);
155 value->u32[1] = _efx_readd(efx, reg + 4);
156 value->u32[2] = _efx_readd(efx, reg + 8);
157 value->u32[3] = _efx_readd(efx, reg + 12);
158 spin_unlock_irqrestore(&efx->biu_lock, flags);
160 netif_vdbg(efx, hw, efx->net_dev,
161 "read from register %x, got " EFX_OWORD_FMT "\n", reg,
162 EFX_OWORD_VAL(*value));
165 /* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
166 static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
167 efx_qword_t *value, unsigned int index)
169 unsigned int addr = index * sizeof(*value);
170 unsigned long flags __attribute__ ((unused));
172 spin_lock_irqsave(&efx->biu_lock, flags);
173 #ifdef EFX_USE_QWORD_IO
174 value->u64[0] = (__force __le64)__raw_readq(membase + addr);
176 value->u32[0] = (__force __le32)__raw_readl(membase + addr);
177 value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
179 spin_unlock_irqrestore(&efx->biu_lock, flags);
181 netif_vdbg(efx, hw, efx->net_dev,
182 "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
183 addr, EFX_QWORD_VAL(*value));
186 /* Read a 32-bit CSR or SRAM */
187 static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
190 value->u32[0] = _efx_readd(efx, reg);
191 netif_vdbg(efx, hw, efx->net_dev,
192 "read from register %x, got "EFX_DWORD_FMT"\n",
193 reg, EFX_DWORD_VAL(*value));
196 /* Write a 128-bit CSR forming part of a table */
197 static inline void efx_writeo_table(struct efx_nic *efx, efx_oword_t *value,
198 unsigned int reg, unsigned int index)
200 efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
203 /* Read a 128-bit CSR forming part of a table */
204 static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
205 unsigned int reg, unsigned int index)
207 efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
210 /* Write a 32-bit CSR forming part of a table, or 32-bit SRAM */
211 static inline void efx_writed_table(struct efx_nic *efx, efx_dword_t *value,
212 unsigned int reg, unsigned int index)
214 efx_writed(efx, value, reg + index * sizeof(efx_oword_t));
217 /* Read a 32-bit CSR forming part of a table, or 32-bit SRAM */
218 static inline void efx_readd_table(struct efx_nic *efx, efx_dword_t *value,
219 unsigned int reg, unsigned int index)
221 efx_readd(efx, value, reg + index * sizeof(efx_dword_t));
224 /* Page-mapped register block size */
225 #define EFX_PAGE_BLOCK_SIZE 0x2000
227 /* Calculate offset to page-mapped register block */
228 #define EFX_PAGED_REG(page, reg) \
229 ((page) * EFX_PAGE_BLOCK_SIZE + (reg))
231 /* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
232 static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
233 unsigned int reg, unsigned int page)
235 reg = EFX_PAGED_REG(page, reg);
237 netif_vdbg(efx, hw, efx->net_dev,
238 "writing register %x with " EFX_OWORD_FMT "\n", reg,
239 EFX_OWORD_VAL(*value));
241 #ifdef EFX_USE_QWORD_IO
242 _efx_writeq(efx, value->u64[0], reg + 0);
243 _efx_writeq(efx, value->u64[1], reg + 8);
245 _efx_writed(efx, value->u32[0], reg + 0);
246 _efx_writed(efx, value->u32[1], reg + 4);
247 _efx_writed(efx, value->u32[2], reg + 8);
248 _efx_writed(efx, value->u32[3], reg + 12);
252 #define efx_writeo_page(efx, value, reg, page) \
253 _efx_writeo_page(efx, value, \
255 BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
258 /* Write a page-mapped 32-bit CSR (EVQ_RPTR or the high bits of
259 * RX_DESC_UPD or TX_DESC_UPD)
261 static inline void _efx_writed_page(struct efx_nic *efx, efx_dword_t *value,
262 unsigned int reg, unsigned int page)
264 efx_writed(efx, value, EFX_PAGED_REG(page, reg));
266 #define efx_writed_page(efx, value, reg, page) \
267 _efx_writed_page(efx, value, \
269 BUILD_BUG_ON_ZERO((reg) != 0x400 && (reg) != 0x83c \
270 && (reg) != 0xa1c), \
273 /* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
274 * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
275 * collector register.
277 static inline void _efx_writed_page_locked(struct efx_nic *efx,
282 unsigned long flags __attribute__ ((unused));
285 spin_lock_irqsave(&efx->biu_lock, flags);
286 efx_writed(efx, value, EFX_PAGED_REG(page, reg));
287 spin_unlock_irqrestore(&efx->biu_lock, flags);
289 efx_writed(efx, value, EFX_PAGED_REG(page, reg));
292 #define efx_writed_page_locked(efx, value, reg, page) \
293 _efx_writed_page_locked(efx, value, \
294 reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
297 #endif /* EFX_IO_H */