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