xref: /freebsd/sys/dev/cxgb/cxgb_osdep.h (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
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 <dev/mii/mii.h>
43 
44 #define	CONFIG_CHELSIO_T3_CORE
45 #include <common/cxgb_version.h>
46 
47 #ifndef _CXGB_OSDEP_H_
48 #define _CXGB_OSDEP_H_
49 
50 typedef struct adapter adapter_t;
51 typedef struct port_info pinfo_t;
52 struct sge_rspq;
53 
54 enum {
55 	TP_TMR_RES = 200,	/* TP timer resolution in usec */
56 	MAX_NPORTS = 4,		/* max # of ports */
57 	TP_SRAM_OFFSET = 4096,	/* TP SRAM content offset in eeprom */
58 	TP_SRAM_LEN = 2112,	/* TP SRAM content offset in eeprom */
59 };
60 
61 struct t3_mbuf_hdr {
62 	struct mbuf *mh_head;
63 	struct mbuf *mh_tail;
64 };
65 
66 #ifndef PANIC_IF
67 #define PANIC_IF(exp) do {                  \
68 	if (exp)                            \
69 		panic("BUG: %s", #exp);      \
70 } while (0)
71 #endif
72 
73 #define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
74 #define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
75 #define m_set_sgl(m, sgl) ((m)->m_pkthdr.header = (sgl))
76 #define m_get_sgl(m) ((bus_dma_segment_t *)(m)->m_pkthdr.header)
77 #define m_set_sgllen(m, len) ((m)->m_pkthdr.ether_vtag = len)
78 #define m_get_sgllen(m) ((m)->m_pkthdr.ether_vtag)
79 
80 /*
81  * XXX FIXME
82  */
83 #define m_set_toep(m, a) ((m)->m_pkthdr.header = (a))
84 #define m_get_toep(m) ((m)->m_pkthdr.header)
85 #define m_set_handler(m, handler) ((m)->m_pkthdr.header = (handler))
86 
87 #define m_set_socket(m, a) ((m)->m_pkthdr.header = (a))
88 #define m_get_socket(m) ((m)->m_pkthdr.header)
89 
90 #define	KTR_CXGB	KTR_SPARE2
91 
92 #define MT_DONTFREE  128
93 
94 #if __FreeBSD_version > 700030
95 #define INTR_FILTERS
96 #define FIRMWARE_LATEST
97 #endif
98 
99 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
100 #define FIRMWARE_LATEST
101 #endif
102 
103 #if __FreeBSD_version > 700000
104 #define MSI_SUPPORTED
105 #define TSO_SUPPORTED
106 #define VLAN_SUPPORTED
107 #define TASKQUEUE_CURRENT
108 #else
109 #define if_name(ifp) (ifp)->if_xname
110 #define M_SANITY(m, n)
111 #endif
112 
113 #if __FreeBSD_version >= 701000
114 #include "opt_inet.h"
115 #ifdef INET
116 #define LRO_SUPPORTED
117 #define TOE_SUPPORTED
118 #endif
119 #endif
120 
121 #if __FreeBSD_version < 800054
122 #if defined (__GNUC__)
123   #if #cpu(i386) || defined __i386 || defined i386 || defined __i386__ || #cpu(x86_64) || defined __x86_64__
124     #define mb()  __asm__ __volatile__ ("mfence;": : :"memory")
125     #define wmb()  __asm__ __volatile__ ("sfence;": : :"memory")
126     #define rmb()  __asm__ __volatile__ ("lfence;": : :"memory")
127   #elif #cpu(sparc64) || defined sparc64 || defined __sparcv9
128     #define mb()  __asm__ __volatile__ ("membar #MemIssue": : :"memory")
129     #define wmb() mb()
130     #define rmb() mb()
131   #elif #cpu(sparc) || defined sparc || defined __sparc__
132     #define mb()  __asm__ __volatile__ ("stbar;": : :"memory")
133     #define wmb() mb()
134     #define rmb() mb()
135 #else
136     #define wmb() mb()
137     #define rmb() mb()
138     #define mb() 	/* XXX just to make this compile */
139   #endif
140 #else
141   #error "unknown compiler"
142 #endif
143 #endif
144 
145 #define __read_mostly __attribute__((__section__(".data.read_mostly")))
146 
147 /*
148  * Workaround for weird Chelsio issue
149  */
150 #if __FreeBSD_version > 700029
151 #define PRIV_SUPPORTED
152 #endif
153 
154 #define CXGB_TX_CLEANUP_THRESHOLD        32
155 
156 
157 #ifdef DEBUG_PRINT
158 #define DPRINTF printf
159 #else
160 #define DPRINTF(...)
161 #endif
162 
163 #define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
164 #define TX_MAX_SEGS                      36     /* maximum supported by card     */
165 
166 #define TX_MAX_DESC                       4     /* max descriptors per packet    */
167 
168 
169 #define TX_START_MAX_DESC (TX_MAX_DESC << 2)    /* maximum number of descriptors
170 						 * call to start used per 	 */
171 
172 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4)    /* maximum tx descriptors
173 						 * to clean per iteration        */
174 #define TX_WR_SIZE_MAX    11*1024              /* the maximum total size of packets aggregated into a single
175 						* TX WR
176 						*/
177 #define TX_WR_COUNT_MAX         7              /* the maximum total number of packets that can be
178 						* aggregated into a single TX WR
179 						*/
180 #if defined(__i386__) || defined(__amd64__)
181 
182 static __inline
183 void prefetch(void *x)
184 {
185         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
186 }
187 
188 #define smp_mb() mb()
189 
190 #define L1_CACHE_BYTES 128
191 extern void kdb_backtrace(void);
192 
193 #define WARN_ON(condition) do { \
194        if (__predict_false((condition)!=0)) {  \
195                 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
196                 kdb_backtrace(); \
197         } \
198 } while (0)
199 
200 #else
201 #define smp_mb()
202 #define prefetch(x)
203 #define L1_CACHE_BYTES 32
204 #endif
205 
206 #define DBG_RX          (1 << 0)
207 static const int debug_flags = DBG_RX;
208 
209 #ifdef DEBUG_PRINT
210 #define DBG(flag, msg) do {	\
211 	if ((flag & debug_flags))	\
212 		printf msg; \
213 } while (0)
214 #else
215 #define DBG(...)
216 #endif
217 
218 #include <sys/syslog.h>
219 
220 #define promisc_rx_mode(rm)  ((rm)->port->ifp->if_flags & IFF_PROMISC)
221 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
222 
223 #define CH_ERR(adap, fmt, ...) log(LOG_ERR, fmt, ##__VA_ARGS__)
224 #define CH_WARN(adap, fmt, ...)	log(LOG_WARNING, fmt, ##__VA_ARGS__)
225 #define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, fmt, ##__VA_ARGS__)
226 
227 #define t3_os_sleep(x) DELAY((x) * 1000)
228 
229 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | (1<<bit)), ((*(p)) & ~(1<<bit)))
230 
231 #define max_t(type, a, b) (type)max((a), (b))
232 #define net_device ifnet
233 #define cpu_to_be32            htobe32
234 
235 /* Standard PHY definitions */
236 #define BMCR_LOOPBACK		BMCR_LOOP
237 #define BMCR_ISOLATE		BMCR_ISO
238 #define BMCR_ANENABLE		BMCR_AUTOEN
239 #define BMCR_SPEED1000		BMCR_SPEED1
240 #define BMCR_SPEED100		BMCR_SPEED0
241 #define BMCR_ANRESTART		BMCR_STARTNEG
242 #define BMCR_FULLDPLX		BMCR_FDX
243 #define BMSR_LSTATUS		BMSR_LINK
244 #define BMSR_ANEGCOMPLETE	BMSR_ACOMP
245 
246 #define MII_LPA			MII_ANLPAR
247 #define MII_ADVERTISE		MII_ANAR
248 #define MII_CTRL1000		MII_100T2CR
249 
250 #define ADVERTISE_PAUSE_CAP	ANAR_FC
251 #define ADVERTISE_PAUSE_ASYM	0x800
252 #define ADVERTISE_PAUSE		ANAR_FC
253 #define ADVERTISE_1000HALF	0x100
254 #define ADVERTISE_1000FULL	0x200
255 #define ADVERTISE_10FULL	ANAR_10_FD
256 #define ADVERTISE_10HALF	ANAR_10
257 #define ADVERTISE_100FULL	ANAR_TX_FD
258 #define ADVERTISE_100HALF	ANAR_TX
259 
260 
261 #define ADVERTISE_1000XHALF	ANAR_X_HD
262 #define ADVERTISE_1000XFULL	ANAR_X_FD
263 #define ADVERTISE_1000XPSE_ASYM	ANAR_X_PAUSE_ASYM
264 #define ADVERTISE_1000XPAUSE	ANAR_X_PAUSE_SYM
265 
266 #define ADVERTISE_CSMA		ANAR_CSMA
267 #define ADVERTISE_NPAGE		ANAR_NP
268 
269 
270 /* Standard PCI Extended Capabilities definitions */
271 #define PCI_CAP_ID_VPD	PCIY_VPD
272 #define PCI_VPD_ADDR	PCIR_VPD_ADDR
273 #define PCI_VPD_ADDR_F	0x8000
274 #define PCI_VPD_DATA	PCIR_VPD_DATA
275 
276 #define PCI_CAP_ID_EXP		PCIY_EXPRESS
277 #define PCI_EXP_DEVCTL		PCIR_EXPRESS_DEVICE_CTL
278 #define PCI_EXP_DEVCTL_PAYLOAD	PCIM_EXP_CTL_MAX_PAYLOAD
279 #define PCI_EXP_DEVCTL_READRQ	PCIM_EXP_CTL_MAX_READ_REQUEST
280 #define PCI_EXP_LNKCTL		PCIR_EXPRESS_LINK_CTL
281 #define PCI_EXP_LNKSTA		PCIR_EXPRESS_LINK_STA
282 
283 /*
284  * Linux compatibility macros
285  */
286 
287 /* Some simple translations */
288 #define __devinit
289 #define udelay(x) DELAY(x)
290 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
291 #define le32_to_cpu(x) le32toh(x)
292 #define le16_to_cpu(x) le16toh(x)
293 #define cpu_to_le32(x) htole32(x)
294 #define swab32(x) bswap32(x)
295 #define simple_strtoul strtoul
296 
297 
298 #ifndef LINUX_TYPES_DEFINED
299 typedef uint8_t 	u8;
300 typedef uint16_t 	u16;
301 typedef uint32_t 	u32;
302 typedef uint64_t 	u64;
303 
304 typedef uint8_t		__u8;
305 typedef uint16_t	__u16;
306 typedef uint32_t	__u32;
307 typedef uint8_t		__be8;
308 typedef uint16_t	__be16;
309 typedef uint32_t	__be32;
310 typedef uint64_t	__be64;
311 #endif
312 
313 
314 #if BYTE_ORDER == BIG_ENDIAN
315 #define __BIG_ENDIAN_BITFIELD
316 #elif BYTE_ORDER == LITTLE_ENDIAN
317 #define __LITTLE_ENDIAN_BITFIELD
318 #else
319 #error "Must set BYTE_ORDER"
320 #endif
321 
322 /* Indicates what features are supported by the interface. */
323 #define SUPPORTED_10baseT_Half          (1 << 0)
324 #define SUPPORTED_10baseT_Full          (1 << 1)
325 #define SUPPORTED_100baseT_Half         (1 << 2)
326 #define SUPPORTED_100baseT_Full         (1 << 3)
327 #define SUPPORTED_1000baseT_Half        (1 << 4)
328 #define SUPPORTED_1000baseT_Full        (1 << 5)
329 #define SUPPORTED_Autoneg               (1 << 6)
330 #define SUPPORTED_TP                    (1 << 7)
331 #define SUPPORTED_AUI                   (1 << 8)
332 #define SUPPORTED_MII                   (1 << 9)
333 #define SUPPORTED_FIBRE                 (1 << 10)
334 #define SUPPORTED_BNC                   (1 << 11)
335 #define SUPPORTED_10000baseT_Full       (1 << 12)
336 #define SUPPORTED_Pause                 (1 << 13)
337 #define SUPPORTED_Asym_Pause            (1 << 14)
338 
339 /* Indicates what features are advertised by the interface. */
340 #define ADVERTISED_10baseT_Half         (1 << 0)
341 #define ADVERTISED_10baseT_Full         (1 << 1)
342 #define ADVERTISED_100baseT_Half        (1 << 2)
343 #define ADVERTISED_100baseT_Full        (1 << 3)
344 #define ADVERTISED_1000baseT_Half       (1 << 4)
345 #define ADVERTISED_1000baseT_Full       (1 << 5)
346 #define ADVERTISED_Autoneg              (1 << 6)
347 #define ADVERTISED_TP                   (1 << 7)
348 #define ADVERTISED_AUI                  (1 << 8)
349 #define ADVERTISED_MII                  (1 << 9)
350 #define ADVERTISED_FIBRE                (1 << 10)
351 #define ADVERTISED_BNC                  (1 << 11)
352 #define ADVERTISED_10000baseT_Full      (1 << 12)
353 #define ADVERTISED_Pause                (1 << 13)
354 #define ADVERTISED_Asym_Pause           (1 << 14)
355 
356 /* Enable or disable autonegotiation.  If this is set to enable,
357  * the forced link modes above are completely ignored.
358  */
359 #define AUTONEG_DISABLE         0x00
360 #define AUTONEG_ENABLE          0x01
361 
362 #define SPEED_10		10
363 #define SPEED_100		100
364 #define SPEED_1000		1000
365 #define SPEED_10000		10000
366 #define DUPLEX_HALF		0
367 #define DUPLEX_FULL		1
368 
369 #endif
370