xref: /freebsd/sys/dev/cxgb/cxgb_osdep.h (revision b0b1dbdd)
1 /**************************************************************************
2 
3 Copyright (c) 2007, Chelsio Inc.
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11 
12  2. Neither the name of the Chelsio Corporation nor the names of its
13     contributors may be used to endorse or promote products derived from
14     this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 
28 
29 $FreeBSD$
30 
31 ***************************************************************************/
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ctype.h>
36 #include <sys/endian.h>
37 #include <sys/bus.h>
38 
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #include <sys/kdb.h>
43 
44 #include <dev/mii/mii.h>
45 
46 #ifndef _CXGB_OSDEP_H_
47 #define _CXGB_OSDEP_H_
48 
49 typedef struct adapter adapter_t;
50 typedef struct port_info pinfo_t;
51 struct sge_rspq;
52 
53 enum {
54 	TP_TMR_RES = 200,	/* TP timer resolution in usec */
55 	MAX_NPORTS = 4,		/* max # of ports */
56 	TP_SRAM_OFFSET = 4096,	/* TP SRAM content offset in eeprom */
57 	TP_SRAM_LEN = 2112,	/* TP SRAM content offset in eeprom */
58 };
59 
60 struct t3_mbuf_hdr {
61 	struct mbuf *mh_head;
62 	struct mbuf *mh_tail;
63 };
64 
65 #ifndef PANIC_IF
66 #define PANIC_IF(exp) do {                  \
67 	if (exp)                            \
68 		panic("BUG: %s", #exp);      \
69 } while (0)
70 #endif
71 
72 #if __FreeBSD_version < 800054
73 #if defined (__GNUC__)
74   #if #cpu(i386) || defined __i386 || defined i386 || defined __i386__ || #cpu(x86_64) || defined __x86_64__
75     #define mb()  __asm__ __volatile__ ("mfence;": : :"memory")
76     #define wmb()  __asm__ __volatile__ ("sfence;": : :"memory")
77     #define rmb()  __asm__ __volatile__ ("lfence;": : :"memory")
78   #elif #cpu(sparc64) || defined sparc64 || defined __sparcv9
79     #define mb()  __asm__ __volatile__ ("membar #MemIssue": : :"memory")
80     #define wmb() mb()
81     #define rmb() mb()
82   #elif #cpu(sparc) || defined sparc || defined __sparc__
83     #define mb()  __asm__ __volatile__ ("stbar;": : :"memory")
84     #define wmb() mb()
85     #define rmb() mb()
86 #else
87     #define wmb() mb()
88     #define rmb() mb()
89     #define mb() 	/* XXX just to make this compile */
90   #endif
91 #else
92   #error "unknown compiler"
93 #endif
94 #endif
95 
96 /*
97  * Workaround for weird Chelsio issue
98  */
99 #if __FreeBSD_version > 700029
100 #define PRIV_SUPPORTED
101 #endif
102 
103 #define CXGB_TX_CLEANUP_THRESHOLD        32
104 
105 #define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
106 #define TX_MAX_SEGS                      36     /* maximum supported by card     */
107 
108 #define TX_MAX_DESC                       4     /* max descriptors per packet    */
109 
110 
111 #define TX_START_MAX_DESC (TX_MAX_DESC << 2)    /* maximum number of descriptors
112 						 * call to start used per 	 */
113 
114 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4)    /* maximum tx descriptors
115 						 * to clean per iteration        */
116 #define TX_WR_SIZE_MAX    11*1024              /* the maximum total size of packets aggregated into a single
117 						* TX WR
118 						*/
119 #define TX_WR_COUNT_MAX         7              /* the maximum total number of packets that can be
120 						* aggregated into a single TX WR
121 						*/
122 #if defined(__i386__) || defined(__amd64__)
123 
124 static __inline
125 void prefetch(void *x)
126 {
127         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
128 }
129 
130 #define smp_mb() mb()
131 
132 #define L1_CACHE_BYTES 128
133 #define WARN_ON(condition) do { \
134 	if (__predict_false((condition)!=0)) {  \
135                 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
136                 kdb_backtrace(); \
137         } \
138 } while (0)
139 
140 #else
141 #define smp_mb()
142 #define prefetch(x)
143 #define L1_CACHE_BYTES 32
144 #endif
145 
146 #define DBG_RX          (1 << 0)
147 static const int debug_flags = DBG_RX;
148 
149 #ifdef DEBUG_PRINT
150 #define DBG(flag, msg) do {	\
151 	if ((flag & debug_flags))	\
152 		printf msg; \
153 } while (0)
154 #else
155 #define DBG(...)
156 #endif
157 
158 #include <sys/syslog.h>
159 
160 #define promisc_rx_mode(rm)  ((rm)->port->ifp->if_flags & IFF_PROMISC)
161 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
162 
163 #define CH_ERR(adap, fmt, ...) log(LOG_ERR, fmt, ##__VA_ARGS__)
164 #define CH_WARN(adap, fmt, ...)	log(LOG_WARNING, fmt, ##__VA_ARGS__)
165 #define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, fmt, ##__VA_ARGS__)
166 
167 #define t3_os_sleep(x) DELAY((x) * 1000)
168 
169 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | (1<<bit)), ((*(p)) & ~(1<<bit)))
170 
171 #define max_t(type, a, b) (type)max((a), (b))
172 #define cpu_to_be32(x)		htobe32(x)
173 
174 /* Standard PHY definitions */
175 #define BMCR_LOOPBACK		BMCR_LOOP
176 #define BMCR_ISOLATE		BMCR_ISO
177 #define BMCR_ANENABLE		BMCR_AUTOEN
178 #define BMCR_SPEED1000		BMCR_SPEED1
179 #define BMCR_SPEED100		BMCR_SPEED0
180 #define BMCR_ANRESTART		BMCR_STARTNEG
181 #define BMCR_FULLDPLX		BMCR_FDX
182 #define BMSR_LSTATUS		BMSR_LINK
183 #define BMSR_ANEGCOMPLETE	BMSR_ACOMP
184 
185 #define MII_LPA			MII_ANLPAR
186 #define MII_ADVERTISE		MII_ANAR
187 #define MII_CTRL1000		MII_100T2CR
188 
189 #define ADVERTISE_PAUSE_CAP	ANAR_FC
190 #define ADVERTISE_PAUSE_ASYM	0x800
191 #define ADVERTISE_PAUSE		ANAR_FC
192 #define ADVERTISE_1000HALF	0x100
193 #define ADVERTISE_1000FULL	0x200
194 #define ADVERTISE_10FULL	ANAR_10_FD
195 #define ADVERTISE_10HALF	ANAR_10
196 #define ADVERTISE_100FULL	ANAR_TX_FD
197 #define ADVERTISE_100HALF	ANAR_TX
198 
199 
200 #define ADVERTISE_1000XHALF	ANAR_X_HD
201 #define ADVERTISE_1000XFULL	ANAR_X_FD
202 #define ADVERTISE_1000XPSE_ASYM	ANAR_X_PAUSE_ASYM
203 #define ADVERTISE_1000XPAUSE	ANAR_X_PAUSE_SYM
204 
205 #define ADVERTISE_CSMA		ANAR_CSMA
206 #define ADVERTISE_NPAGE		ANAR_NP
207 
208 
209 /* Standard PCI Extended Capabilities definitions */
210 #define PCI_CAP_ID_VPD	PCIY_VPD
211 #define PCI_VPD_ADDR	PCIR_VPD_ADDR
212 #define PCI_VPD_ADDR_F	0x8000
213 #define PCI_VPD_DATA	PCIR_VPD_DATA
214 
215 #define PCI_CAP_ID_EXP		PCIY_EXPRESS
216 #define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
217 #define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
218 #define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
219 #define PCI_EXP_LNKCTL		PCIER_LINK_CTL
220 #define PCI_EXP_LNKSTA		PCIER_LINK_STA
221 
222 /*
223  * Linux compatibility macros
224  */
225 
226 /* Some simple translations */
227 #define __devinit
228 #define udelay(x) DELAY(x)
229 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
230 #define le32_to_cpu(x) le32toh(x)
231 #define le16_to_cpu(x) le16toh(x)
232 #define cpu_to_le32(x) htole32(x)
233 #define swab32(x) bswap32(x)
234 #ifndef simple_strtoul
235 #define simple_strtoul(...) strtoul(__VA_ARGS__)
236 #endif
237 
238 
239 #ifndef LINUX_TYPES_DEFINED
240 typedef uint8_t 	u8;
241 typedef uint16_t 	u16;
242 typedef uint32_t 	u32;
243 typedef uint64_t 	u64;
244 
245 typedef uint8_t		__u8;
246 typedef uint16_t	__u16;
247 typedef uint32_t	__u32;
248 typedef uint8_t		__be8;
249 typedef uint16_t	__be16;
250 typedef uint32_t	__be32;
251 typedef uint64_t	__be64;
252 #endif
253 
254 
255 #if BYTE_ORDER == BIG_ENDIAN
256 #define __BIG_ENDIAN_BITFIELD
257 #elif BYTE_ORDER == LITTLE_ENDIAN
258 #define __LITTLE_ENDIAN_BITFIELD
259 #else
260 #error "Must set BYTE_ORDER"
261 #endif
262 
263 /* Indicates what features are supported by the interface. */
264 #define SUPPORTED_10baseT_Half          (1 << 0)
265 #define SUPPORTED_10baseT_Full          (1 << 1)
266 #define SUPPORTED_100baseT_Half         (1 << 2)
267 #define SUPPORTED_100baseT_Full         (1 << 3)
268 #define SUPPORTED_1000baseT_Half        (1 << 4)
269 #define SUPPORTED_1000baseT_Full        (1 << 5)
270 #define SUPPORTED_Autoneg               (1 << 6)
271 #define SUPPORTED_TP                    (1 << 7)
272 #define SUPPORTED_AUI                   (1 << 8)
273 #define SUPPORTED_MII                   (1 << 9)
274 #define SUPPORTED_FIBRE                 (1 << 10)
275 #define SUPPORTED_BNC                   (1 << 11)
276 #define SUPPORTED_10000baseT_Full       (1 << 12)
277 #define SUPPORTED_Pause                 (1 << 13)
278 #define SUPPORTED_Asym_Pause            (1 << 14)
279 
280 /* Indicates what features are advertised by the interface. */
281 #define ADVERTISED_10baseT_Half         (1 << 0)
282 #define ADVERTISED_10baseT_Full         (1 << 1)
283 #define ADVERTISED_100baseT_Half        (1 << 2)
284 #define ADVERTISED_100baseT_Full        (1 << 3)
285 #define ADVERTISED_1000baseT_Half       (1 << 4)
286 #define ADVERTISED_1000baseT_Full       (1 << 5)
287 #define ADVERTISED_Autoneg              (1 << 6)
288 #define ADVERTISED_TP                   (1 << 7)
289 #define ADVERTISED_AUI                  (1 << 8)
290 #define ADVERTISED_MII                  (1 << 9)
291 #define ADVERTISED_FIBRE                (1 << 10)
292 #define ADVERTISED_BNC                  (1 << 11)
293 #define ADVERTISED_10000baseT_Full      (1 << 12)
294 #define ADVERTISED_Pause                (1 << 13)
295 #define ADVERTISED_Asym_Pause           (1 << 14)
296 
297 /* Enable or disable autonegotiation.  If this is set to enable,
298  * the forced link modes above are completely ignored.
299  */
300 #define AUTONEG_DISABLE         0x00
301 #define AUTONEG_ENABLE          0x01
302 
303 #define SPEED_10		10
304 #define SPEED_100		100
305 #define SPEED_1000		1000
306 #define SPEED_10000		10000
307 #define DUPLEX_HALF		0
308 #define DUPLEX_FULL		1
309 
310 #endif
311