xref: /freebsd/sys/dev/cxgb/cxgb_osdep.h (revision 94942af266ac119ede0ca836f9aa5a5ac0582938)
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. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15 
16  3. Neither the name of the Chelsio Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31 
32 
33 $FreeBSD$
34 
35 ***************************************************************************/
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/ctype.h>
40 #include <sys/endian.h>
41 #include <sys/bus.h>
42 
43 #include <dev/mii/mii.h>
44 
45 #include <dev/cxgb/common/cxgb_version.h>
46 #include <dev/cxgb/cxgb_config.h>
47 
48 #ifndef _CXGB_OSDEP_H_
49 #define _CXGB_OSDEP_H_
50 
51 typedef struct adapter adapter_t;
52 
53 struct sge_rspq;
54 
55 #if __FreeBSD_version > 700030
56 #define INTR_FILTERS
57 #define FIRMWARE_LATEST
58 #endif
59 
60 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
61 #define FIRMWARE_LATEST
62 #endif
63 
64 #if __FreeBSD_version > 700000
65 #define MSI_SUPPORTED
66 #define TSO_SUPPORTED
67 #define VLAN_SUPPORTED
68 #define TASKQUEUE_CURRENT
69 #endif
70 
71 /*
72  * Workaround for weird Chelsio issue
73  */
74 #if __FreeBSD_version > 700029
75 #define PRIV_SUPPORTED
76 #endif
77 
78 #define CXGB_TX_CLEANUP_THRESHOLD 32
79 
80 #define LOG_WARNING 1
81 
82 #ifdef DEBUG_PRINT
83 #define DPRINTF printf
84 #else
85 #define DPRINTF(...)
86 #endif
87 
88 #define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
89 #define TX_MAX_SEGS                      36     /* maximum supported by card     */
90 #define TX_MAX_DESC                       4     /* max descriptors per packet    */
91 #define TX_START_MAX_DESC (TX_MAX_DESC << 1)    /* maximum number of descriptors
92 						 * call to start used per 	 */
93 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 2)    /* maximum tx descriptors
94 						 * to clean per iteration        */
95 
96 
97 #if defined(__i386__) || defined(__amd64__)
98 #define mb()    __asm volatile("mfence":::"memory")
99 #define rmb()   __asm volatile("lfence":::"memory")
100 #define wmb()   __asm volatile("sfence" ::: "memory")
101 #define smp_mb() mb()
102 
103 #define L1_CACHE_BYTES 32
104 static __inline
105 void prefetch(void *x)
106 {
107         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
108 }
109 
110 #else /* !i386 && !amd64 */
111 #define mb()
112 #define rmb()
113 #define wmb()
114 #define smp_mb()
115 #define prefetch(x)
116 #define L1_CACHE_BYTES 32
117 #endif
118 #define DBG_RX          (1 << 0)
119 static const int debug_flags = DBG_RX;
120 
121 #ifdef DEBUG_PRINT
122 #define DBG(flag, msg) do {	\
123 	if ((flag & debug_flags))	\
124 		printf msg; \
125 } while (0)
126 #else
127 #define DBG(...)
128 #endif
129 
130 #define promisc_rx_mode(rm)  ((rm)->port->ifp->if_flags & IFF_PROMISC)
131 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
132 
133 #define CH_ERR(adap, fmt, ...)device_printf(adap->dev, fmt, ##__VA_ARGS__);
134 
135 #define CH_WARN(adap, fmt, ...)	device_printf(adap->dev, fmt, ##__VA_ARGS__)
136 #define CH_ALERT(adap, fmt, ...) device_printf(adap->dev, fmt, ##__VA_ARGS__)
137 
138 #define t3_os_sleep(x) DELAY((x) * 1000)
139 
140 #define max_t(type, a, b) (type)max((a), (b))
141 
142 /* Standard PHY definitions */
143 #define BMCR_LOOPBACK		BMCR_LOOP
144 #define BMCR_ISOLATE		BMCR_ISO
145 #define BMCR_ANENABLE		BMCR_AUTOEN
146 #define BMCR_SPEED1000		BMCR_SPEED1
147 #define BMCR_SPEED100		BMCR_SPEED0
148 #define BMCR_ANRESTART		BMCR_STARTNEG
149 #define BMCR_FULLDPLX		BMCR_FDX
150 #define BMSR_LSTATUS		BMSR_LINK
151 #define BMSR_ANEGCOMPLETE	BMSR_ACOMP
152 
153 #define MII_LPA			MII_ANLPAR
154 #define MII_ADVERTISE		MII_ANAR
155 #define MII_CTRL1000		MII_100T2CR
156 
157 #define ADVERTISE_PAUSE_CAP	ANAR_FC
158 #define ADVERTISE_PAUSE_ASYM	0x0800
159 #define ADVERTISE_1000HALF	ANAR_X_HD
160 #define ADVERTISE_1000FULL	ANAR_X_FD
161 #define ADVERTISE_10FULL	ANAR_10_FD
162 #define ADVERTISE_10HALF	ANAR_10
163 #define ADVERTISE_100FULL	ANAR_TX_FD
164 #define ADVERTISE_100HALF	ANAR_TX
165 
166 /* Standard PCI Extended Capaibilities definitions */
167 #define PCI_CAP_ID_VPD	0x03
168 #define PCI_VPD_ADDR	2
169 #define PCI_VPD_ADDR_F	0x8000
170 #define PCI_VPD_DATA	4
171 
172 #define PCI_CAP_ID_EXP	0x10
173 #define PCI_EXP_DEVCTL	8
174 #define PCI_EXP_DEVCTL_PAYLOAD 0x00e0
175 #define PCI_EXP_LNKCTL	16
176 #define PCI_EXP_LNKSTA	18
177 
178 /*
179  * Linux compatibility macros
180  */
181 
182 /* Some simple translations */
183 #define __devinit
184 #define udelay(x) DELAY(x)
185 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
186 #define le32_to_cpu(x) le32toh(x)
187 #define cpu_to_le32(x) htole32(x)
188 #define swab32(x) bswap32(x)
189 #define simple_strtoul strtoul
190 
191 /* More types and endian definitions */
192 typedef uint8_t u8;
193 typedef uint16_t u16;
194 typedef uint32_t u32;
195 typedef uint64_t u64;
196 
197 typedef uint8_t	__u8;
198 typedef uint16_t __u16;
199 typedef uint32_t __u32;
200 typedef uint8_t __be8;
201 typedef uint16_t __be16;
202 typedef uint32_t __be32;
203 typedef uint64_t __be64;
204 
205 #if BYTE_ORDER == BIG_ENDIAN
206 #define __BIG_ENDIAN_BITFIELD
207 #elif BYTE_ORDER == LITTLE_ENDIAN
208 #define __LITTLE_ENDIAN_BITFIELD
209 #else
210 #error "Must set BYTE_ORDER"
211 #endif
212 
213 /* Indicates what features are supported by the interface. */
214 #define SUPPORTED_10baseT_Half          (1 << 0)
215 #define SUPPORTED_10baseT_Full          (1 << 1)
216 #define SUPPORTED_100baseT_Half         (1 << 2)
217 #define SUPPORTED_100baseT_Full         (1 << 3)
218 #define SUPPORTED_1000baseT_Half        (1 << 4)
219 #define SUPPORTED_1000baseT_Full        (1 << 5)
220 #define SUPPORTED_Autoneg               (1 << 6)
221 #define SUPPORTED_TP                    (1 << 7)
222 #define SUPPORTED_AUI                   (1 << 8)
223 #define SUPPORTED_MII                   (1 << 9)
224 #define SUPPORTED_FIBRE                 (1 << 10)
225 #define SUPPORTED_BNC                   (1 << 11)
226 #define SUPPORTED_10000baseT_Full       (1 << 12)
227 #define SUPPORTED_Pause                 (1 << 13)
228 #define SUPPORTED_Asym_Pause            (1 << 14)
229 
230 /* Indicates what features are advertised by the interface. */
231 #define ADVERTISED_10baseT_Half         (1 << 0)
232 #define ADVERTISED_10baseT_Full         (1 << 1)
233 #define ADVERTISED_100baseT_Half        (1 << 2)
234 #define ADVERTISED_100baseT_Full        (1 << 3)
235 #define ADVERTISED_1000baseT_Half       (1 << 4)
236 #define ADVERTISED_1000baseT_Full       (1 << 5)
237 #define ADVERTISED_Autoneg              (1 << 6)
238 #define ADVERTISED_TP                   (1 << 7)
239 #define ADVERTISED_AUI                  (1 << 8)
240 #define ADVERTISED_MII                  (1 << 9)
241 #define ADVERTISED_FIBRE                (1 << 10)
242 #define ADVERTISED_BNC                  (1 << 11)
243 #define ADVERTISED_10000baseT_Full      (1 << 12)
244 #define ADVERTISED_Pause                (1 << 13)
245 #define ADVERTISED_Asym_Pause           (1 << 14)
246 
247 /* Enable or disable autonegotiation.  If this is set to enable,
248  * the forced link modes above are completely ignored.
249  */
250 #define AUTONEG_DISABLE         0x00
251 #define AUTONEG_ENABLE          0x01
252 
253 #define SPEED_10		10
254 #define SPEED_100		100
255 #define SPEED_1000		1000
256 #define SPEED_10000		10000
257 #define DUPLEX_HALF		0
258 #define DUPLEX_FULL		1
259 
260 #endif
261