xref: /freebsd/sys/dev/cxgb/cxgb_osdep.h (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
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 <dev/mii/mii.h>
40 
41 #ifdef CONFIG_DEFINED
42 #include <common/cxgb_version.h>
43 #include <cxgb_config.h>
44 #else
45 #include <dev/cxgb/common/cxgb_version.h>
46 #include <dev/cxgb/cxgb_config.h>
47 #endif
48 
49 #ifndef _CXGB_OSDEP_H_
50 #define _CXGB_OSDEP_H_
51 
52 typedef struct adapter adapter_t;
53 struct sge_rspq;
54 
55 #define PANIC_IF(exp) do {                  \
56 	if (exp)                            \
57 		panic("BUG: %s", exp);      \
58 } while (0)
59 
60 
61 #define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
62 #define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
63 
64 #if __FreeBSD_version > 700030
65 #define INTR_FILTERS
66 #define FIRMWARE_LATEST
67 #endif
68 
69 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
70 #define FIRMWARE_LATEST
71 #endif
72 
73 #if __FreeBSD_version > 700000
74 #define MSI_SUPPORTED
75 #define TSO_SUPPORTED
76 #define VLAN_SUPPORTED
77 #define TASKQUEUE_CURRENT
78 #endif
79 
80 #define __read_mostly __attribute__((__section__(".data.read_mostly")))
81 
82 /*
83  * Workaround for weird Chelsio issue
84  */
85 #if __FreeBSD_version > 700029
86 #define PRIV_SUPPORTED
87 #endif
88 
89 #define CXGB_TX_CLEANUP_THRESHOLD        32
90 
91 #define LOG_WARNING                       1
92 #define LOG_ERR                           2
93 
94 #ifdef DEBUG_PRINT
95 #define DPRINTF printf
96 #else
97 #define DPRINTF(...)
98 #endif
99 
100 #define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
101 #define TX_MAX_SEGS                      36     /* maximum supported by card     */
102 #define TX_MAX_DESC                       4     /* max descriptors per packet    */
103 
104 #define TX_START_MIN_DESC  (TX_MAX_DESC << 2)
105 
106 #if 0
107 #define TX_START_MAX_DESC (TX_ETH_Q_SIZE >> 2)  /* maximum number of descriptors */
108 #endif
109 
110 #define TX_START_MAX_DESC (TX_MAX_DESC << 3)    /* maximum number of descriptors
111 						 * call to start used per 	 */
112 
113 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4)    /* maximum tx descriptors
114 						 * to clean per iteration        */
115 
116 
117 #if defined(__i386__) || defined(__amd64__)
118 #define mb()    __asm volatile("mfence":::"memory")
119 #define rmb()   __asm volatile("lfence":::"memory")
120 #define wmb()   __asm volatile("sfence" ::: "memory")
121 #define smp_mb() mb()
122 
123 #define L1_CACHE_BYTES 64
124 static __inline
125 void prefetch(void *x)
126 {
127         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
128 }
129 
130 extern void kdb_backtrace(void);
131 
132 #define WARN_ON(condition) do { \
133         if (unlikely((condition)!=0)) { \
134                 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
135                 kdb_backtrace(); \
136         } \
137 } while (0)
138 
139 
140 #else /* !i386 && !amd64 */
141 #define mb()
142 #define rmb()
143 #define wmb()
144 #define smp_mb()
145 #define prefetch(x)
146 #define L1_CACHE_BYTES 32
147 #endif
148 #define DBG_RX          (1 << 0)
149 static const int debug_flags = DBG_RX;
150 
151 #ifdef DEBUG_PRINT
152 #define DBG(flag, msg) do {	\
153 	if ((flag & debug_flags))	\
154 		printf msg; \
155 } while (0)
156 #else
157 #define DBG(...)
158 #endif
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, ...)device_printf(adap->dev, fmt, ##__VA_ARGS__);
164 
165 #define CH_WARN(adap, fmt, ...)	device_printf(adap->dev, fmt, ##__VA_ARGS__)
166 #define CH_ALERT(adap, fmt, ...) device_printf(adap->dev, fmt, ##__VA_ARGS__)
167 
168 #define t3_os_sleep(x) DELAY((x) * 1000)
169 
170 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | bit), ((*(p)) & ~bit))
171 
172 
173 #define max_t(type, a, b) (type)max((a), (b))
174 #define net_device ifnet
175 #define cpu_to_be32            htobe32
176 
177 
178 
179 /* Standard PHY definitions */
180 #define BMCR_LOOPBACK		BMCR_LOOP
181 #define BMCR_ISOLATE		BMCR_ISO
182 #define BMCR_ANENABLE		BMCR_AUTOEN
183 #define BMCR_SPEED1000		BMCR_SPEED1
184 #define BMCR_SPEED100		BMCR_SPEED0
185 #define BMCR_ANRESTART		BMCR_STARTNEG
186 #define BMCR_FULLDPLX		BMCR_FDX
187 #define BMSR_LSTATUS		BMSR_LINK
188 #define BMSR_ANEGCOMPLETE	BMSR_ACOMP
189 
190 #define MII_LPA			MII_ANLPAR
191 #define MII_ADVERTISE		MII_ANAR
192 #define MII_CTRL1000		MII_100T2CR
193 
194 #define ADVERTISE_PAUSE_CAP	ANAR_FC
195 #define ADVERTISE_PAUSE_ASYM	0x0800
196 #define ADVERTISE_1000HALF	ANAR_X_HD
197 #define ADVERTISE_1000FULL	ANAR_X_FD
198 #define ADVERTISE_10FULL	ANAR_10_FD
199 #define ADVERTISE_10HALF	ANAR_10
200 #define ADVERTISE_100FULL	ANAR_TX_FD
201 #define ADVERTISE_100HALF	ANAR_TX
202 
203 /* Standard PCI Extended Capaibilities definitions */
204 #define PCI_CAP_ID_VPD	0x03
205 #define PCI_VPD_ADDR	2
206 #define PCI_VPD_ADDR_F	0x8000
207 #define PCI_VPD_DATA	4
208 
209 #define PCI_CAP_ID_EXP	0x10
210 #define PCI_EXP_DEVCTL	8
211 #define PCI_EXP_DEVCTL_PAYLOAD 0x00e0
212 #define PCI_EXP_LNKCTL	16
213 #define PCI_EXP_LNKSTA	18
214 
215 /*
216  * Linux compatibility macros
217  */
218 
219 /* Some simple translations */
220 #define __devinit
221 #define udelay(x) DELAY(x)
222 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
223 #define le32_to_cpu(x) le32toh(x)
224 #define cpu_to_le32(x) htole32(x)
225 #define swab32(x) bswap32(x)
226 #define simple_strtoul strtoul
227 
228 /* More types and endian definitions */
229 typedef uint8_t u8;
230 typedef uint16_t u16;
231 typedef uint32_t u32;
232 typedef uint64_t u64;
233 
234 typedef uint8_t	__u8;
235 typedef uint16_t __u16;
236 typedef uint32_t __u32;
237 typedef uint8_t __be8;
238 typedef uint16_t __be16;
239 typedef uint32_t __be32;
240 typedef uint64_t __be64;
241 
242 #if BYTE_ORDER == BIG_ENDIAN
243 #define __BIG_ENDIAN_BITFIELD
244 #elif BYTE_ORDER == LITTLE_ENDIAN
245 #define __LITTLE_ENDIAN_BITFIELD
246 #else
247 #error "Must set BYTE_ORDER"
248 #endif
249 
250 /* Indicates what features are supported by the interface. */
251 #define SUPPORTED_10baseT_Half          (1 << 0)
252 #define SUPPORTED_10baseT_Full          (1 << 1)
253 #define SUPPORTED_100baseT_Half         (1 << 2)
254 #define SUPPORTED_100baseT_Full         (1 << 3)
255 #define SUPPORTED_1000baseT_Half        (1 << 4)
256 #define SUPPORTED_1000baseT_Full        (1 << 5)
257 #define SUPPORTED_Autoneg               (1 << 6)
258 #define SUPPORTED_TP                    (1 << 7)
259 #define SUPPORTED_AUI                   (1 << 8)
260 #define SUPPORTED_MII                   (1 << 9)
261 #define SUPPORTED_FIBRE                 (1 << 10)
262 #define SUPPORTED_BNC                   (1 << 11)
263 #define SUPPORTED_10000baseT_Full       (1 << 12)
264 #define SUPPORTED_Pause                 (1 << 13)
265 #define SUPPORTED_Asym_Pause            (1 << 14)
266 
267 /* Indicates what features are advertised by the interface. */
268 #define ADVERTISED_10baseT_Half         (1 << 0)
269 #define ADVERTISED_10baseT_Full         (1 << 1)
270 #define ADVERTISED_100baseT_Half        (1 << 2)
271 #define ADVERTISED_100baseT_Full        (1 << 3)
272 #define ADVERTISED_1000baseT_Half       (1 << 4)
273 #define ADVERTISED_1000baseT_Full       (1 << 5)
274 #define ADVERTISED_Autoneg              (1 << 6)
275 #define ADVERTISED_TP                   (1 << 7)
276 #define ADVERTISED_AUI                  (1 << 8)
277 #define ADVERTISED_MII                  (1 << 9)
278 #define ADVERTISED_FIBRE                (1 << 10)
279 #define ADVERTISED_BNC                  (1 << 11)
280 #define ADVERTISED_10000baseT_Full      (1 << 12)
281 #define ADVERTISED_Pause                (1 << 13)
282 #define ADVERTISED_Asym_Pause           (1 << 14)
283 
284 /* Enable or disable autonegotiation.  If this is set to enable,
285  * the forced link modes above are completely ignored.
286  */
287 #define AUTONEG_DISABLE         0x00
288 #define AUTONEG_ENABLE          0x01
289 
290 #define SPEED_10		10
291 #define SPEED_100		100
292 #define SPEED_1000		1000
293 #define SPEED_10000		10000
294 #define DUPLEX_HALF		0
295 #define DUPLEX_FULL		1
296 
297 #endif
298