sfc: Optimise falcon_writel_page_locked() for page > 0
[linux-2.6/verdex.git] / drivers / net / sfc / falcon_io.h
blob8883092dae9768ee0d0516b518b5b3cd491af251
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.
9 */
11 #ifndef EFX_FALCON_IO_H
12 #define EFX_FALCON_IO_H
14 #include <linux/io.h>
15 #include <linux/spinlock.h>
17 /**************************************************************************
19 * Falcon hardware access
21 **************************************************************************
23 * Notes on locking strategy:
25 * Most Falcon registers require 16-byte (or 8-byte, for SRAM
26 * registers) atomic writes which necessitates locking.
27 * Under normal operation few writes to the Falcon BAR are made and these
28 * registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and TX_DESC_UPD_REG) are special
29 * cased to allow 4-byte (hence lockless) accesses.
31 * It *is* safe to write to these 4-byte registers in the middle of an
32 * access to an 8-byte or 16-byte register. We therefore use a
33 * spinlock to protect accesses to the larger registers, but no locks
34 * for the 4-byte registers.
36 * A write barrier is needed to ensure that DW3 is written after DW0/1/2
37 * due to the way the 16byte registers are "collected" in the Falcon BIU
39 * We also lock when carrying out reads, to ensure consistency of the
40 * data (made possible since the BIU reads all 128 bits into a cache).
41 * Reads are very rare, so this isn't a significant performance
42 * impact. (Most data transferred from NIC to host is DMAed directly
43 * into host memory).
45 * I/O BAR access uses locks for both reads and writes (but is only provided
46 * for testing purposes).
49 /* Special buffer descriptors (Falcon SRAM) */
50 #define BUF_TBL_KER_A1 0x18000
51 #define BUF_TBL_KER_B0 0x800000
54 #if BITS_PER_LONG == 64
55 #define FALCON_USE_QWORD_IO 1
56 #endif
58 #ifdef FALCON_USE_QWORD_IO
59 static inline void _falcon_writeq(struct efx_nic *efx, __le64 value,
60 unsigned int reg)
62 __raw_writeq((__force u64)value, efx->membase + reg);
64 static inline __le64 _falcon_readq(struct efx_nic *efx, unsigned int reg)
66 return (__force __le64)__raw_readq(efx->membase + reg);
68 #endif
70 static inline void _falcon_writel(struct efx_nic *efx, __le32 value,
71 unsigned int reg)
73 __raw_writel((__force u32)value, efx->membase + reg);
75 static inline __le32 _falcon_readl(struct efx_nic *efx, unsigned int reg)
77 return (__force __le32)__raw_readl(efx->membase + reg);
80 /* Writes to a normal 16-byte Falcon register, locking as appropriate. */
81 static inline void falcon_write(struct efx_nic *efx, efx_oword_t *value,
82 unsigned int reg)
84 unsigned long flags;
86 EFX_REGDUMP(efx, "writing register %x with " EFX_OWORD_FMT "\n", reg,
87 EFX_OWORD_VAL(*value));
89 spin_lock_irqsave(&efx->biu_lock, flags);
90 #ifdef FALCON_USE_QWORD_IO
91 _falcon_writeq(efx, value->u64[0], reg + 0);
92 wmb();
93 _falcon_writeq(efx, value->u64[1], reg + 8);
94 #else
95 _falcon_writel(efx, value->u32[0], reg + 0);
96 _falcon_writel(efx, value->u32[1], reg + 4);
97 _falcon_writel(efx, value->u32[2], reg + 8);
98 wmb();
99 _falcon_writel(efx, value->u32[3], reg + 12);
100 #endif
101 mmiowb();
102 spin_unlock_irqrestore(&efx->biu_lock, flags);
105 /* Writes to an 8-byte Falcon SRAM register, locking as appropriate. */
106 static inline void falcon_write_sram(struct efx_nic *efx, efx_qword_t *value,
107 unsigned int index)
109 unsigned int reg = efx->type->buf_tbl_base + (index * sizeof(*value));
110 unsigned long flags;
112 EFX_REGDUMP(efx, "writing SRAM register %x with " EFX_QWORD_FMT "\n",
113 reg, EFX_QWORD_VAL(*value));
115 spin_lock_irqsave(&efx->biu_lock, flags);
116 #ifdef FALCON_USE_QWORD_IO
117 _falcon_writeq(efx, value->u64[0], reg + 0);
118 #else
119 _falcon_writel(efx, value->u32[0], reg + 0);
120 wmb();
121 _falcon_writel(efx, value->u32[1], reg + 4);
122 #endif
123 mmiowb();
124 spin_unlock_irqrestore(&efx->biu_lock, flags);
127 /* Write dword to Falcon register that allows partial writes
129 * Some Falcon registers (EVQ_RPTR_REG, RX_DESC_UPD_REG and
130 * TX_DESC_UPD_REG) can be written to as a single dword. This allows
131 * for lockless writes.
133 static inline void falcon_writel(struct efx_nic *efx, efx_dword_t *value,
134 unsigned int reg)
136 EFX_REGDUMP(efx, "writing partial register %x with "EFX_DWORD_FMT"\n",
137 reg, EFX_DWORD_VAL(*value));
139 /* No lock required */
140 _falcon_writel(efx, value->u32[0], reg);
143 /* Read from a Falcon register
145 * This reads an entire 16-byte Falcon register in one go, locking as
146 * appropriate. It is essential to read the first dword first, as this
147 * prompts Falcon to load the current value into the shadow register.
149 static inline void falcon_read(struct efx_nic *efx, efx_oword_t *value,
150 unsigned int reg)
152 unsigned long flags;
154 spin_lock_irqsave(&efx->biu_lock, flags);
155 value->u32[0] = _falcon_readl(efx, reg + 0);
156 rmb();
157 value->u32[1] = _falcon_readl(efx, reg + 4);
158 value->u32[2] = _falcon_readl(efx, reg + 8);
159 value->u32[3] = _falcon_readl(efx, reg + 12);
160 spin_unlock_irqrestore(&efx->biu_lock, flags);
162 EFX_REGDUMP(efx, "read from register %x, got " EFX_OWORD_FMT "\n", reg,
163 EFX_OWORD_VAL(*value));
166 /* This reads an 8-byte Falcon SRAM entry in one go. */
167 static inline void falcon_read_sram(struct efx_nic *efx, efx_qword_t *value,
168 unsigned int index)
170 unsigned int reg = efx->type->buf_tbl_base + (index * sizeof(*value));
171 unsigned long flags;
173 spin_lock_irqsave(&efx->biu_lock, flags);
174 #ifdef FALCON_USE_QWORD_IO
175 value->u64[0] = _falcon_readq(efx, reg + 0);
176 #else
177 value->u32[0] = _falcon_readl(efx, reg + 0);
178 rmb();
179 value->u32[1] = _falcon_readl(efx, reg + 4);
180 #endif
181 spin_unlock_irqrestore(&efx->biu_lock, flags);
183 EFX_REGDUMP(efx, "read from SRAM register %x, got "EFX_QWORD_FMT"\n",
184 reg, EFX_QWORD_VAL(*value));
187 /* Read dword from Falcon register that allows partial writes (sic) */
188 static inline void falcon_readl(struct efx_nic *efx, efx_dword_t *value,
189 unsigned int reg)
191 value->u32[0] = _falcon_readl(efx, reg);
192 EFX_REGDUMP(efx, "read from register %x, got "EFX_DWORD_FMT"\n",
193 reg, EFX_DWORD_VAL(*value));
196 /* Write to a register forming part of a table */
197 static inline void falcon_write_table(struct efx_nic *efx, efx_oword_t *value,
198 unsigned int reg, unsigned int index)
200 falcon_write(efx, value, reg + index * sizeof(efx_oword_t));
203 /* Read to a register forming part of a table */
204 static inline void falcon_read_table(struct efx_nic *efx, efx_oword_t *value,
205 unsigned int reg, unsigned int index)
207 falcon_read(efx, value, reg + index * sizeof(efx_oword_t));
210 /* Write to a dword register forming part of a table */
211 static inline void falcon_writel_table(struct efx_nic *efx, efx_dword_t *value,
212 unsigned int reg, unsigned int index)
214 falcon_writel(efx, value, reg + index * sizeof(efx_oword_t));
217 /* Page-mapped register block size */
218 #define FALCON_PAGE_BLOCK_SIZE 0x2000
220 /* Calculate offset to page-mapped register block */
221 #define FALCON_PAGED_REG(page, reg) \
222 ((page) * FALCON_PAGE_BLOCK_SIZE + (reg))
224 /* As for falcon_write(), but for a page-mapped register. */
225 static inline void falcon_write_page(struct efx_nic *efx, efx_oword_t *value,
226 unsigned int reg, unsigned int page)
228 falcon_write(efx, value, FALCON_PAGED_REG(page, reg));
231 /* As for falcon_writel(), but for a page-mapped register. */
232 static inline void falcon_writel_page(struct efx_nic *efx, efx_dword_t *value,
233 unsigned int reg, unsigned int page)
235 falcon_writel(efx, value, FALCON_PAGED_REG(page, reg));
238 /* Write dword to Falcon page-mapped register with an extra lock.
240 * As for falcon_writel_page(), but for a register that suffers from
241 * SFC bug 3181. If writing to page 0, take out a lock so the BIU
242 * collector cannot be confused.
244 static inline void falcon_writel_page_locked(struct efx_nic *efx,
245 efx_dword_t *value,
246 unsigned int reg,
247 unsigned int page)
249 unsigned long flags = 0;
251 if (page == 0)
252 spin_lock_irqsave(&efx->biu_lock, flags);
253 falcon_writel(efx, value, FALCON_PAGED_REG(page, reg));
254 if (page == 0)
255 spin_unlock_irqrestore(&efx->biu_lock, flags);
258 #endif /* EFX_FALCON_IO_H */