xref: /freebsd/sys/dev/cxgb/cxgb_osdep.h (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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 #include <common/cxgb_version.h>
45 #include <cxgb_config.h>
46 
47 #ifndef _CXGB_OSDEP_H_
48 #define _CXGB_OSDEP_H_
49 
50 typedef struct adapter adapter_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 #define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
73 #define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
74 #define m_set_sgl(m, sgl) ((m)->m_pkthdr.header = (sgl))
75 #define m_get_sgl(m) ((bus_dma_segment_t *)(m)->m_pkthdr.header)
76 #define m_set_sgllen(m, len) ((m)->m_pkthdr.ether_vtag = len)
77 #define m_get_sgllen(m) ((m)->m_pkthdr.ether_vtag)
78 
79 /*
80  * XXX FIXME
81  */
82 #define m_set_toep(m, a) ((m)->m_pkthdr.header = (a))
83 #define m_get_toep(m) ((m)->m_pkthdr.header)
84 #define m_set_handler(m, handler) ((m)->m_pkthdr.header = (handler))
85 
86 #define m_set_socket(m, a) ((m)->m_pkthdr.header = (a))
87 #define m_get_socket(m) ((m)->m_pkthdr.header)
88 
89 #define	KTR_CXGB	KTR_SPARE2
90 
91 #define MT_DONTFREE  128
92 
93 #if __FreeBSD_version > 700030
94 #define INTR_FILTERS
95 #define FIRMWARE_LATEST
96 #endif
97 
98 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
99 #define FIRMWARE_LATEST
100 #endif
101 
102 #if __FreeBSD_version > 700000
103 #define MSI_SUPPORTED
104 #define TSO_SUPPORTED
105 #define VLAN_SUPPORTED
106 #define TASKQUEUE_CURRENT
107 #else
108 #define if_name(ifp) (ifp)->if_xname
109 #define M_SANITY(m, n)
110 #endif
111 
112 #if __FreeBSD_version >= 701000
113 #define LRO_SUPPORTED
114 #define TOE_SUPPORTED
115 #endif
116 
117 #define __read_mostly __attribute__((__section__(".data.read_mostly")))
118 
119 /*
120  * Workaround for weird Chelsio issue
121  */
122 #if __FreeBSD_version > 700029
123 #define PRIV_SUPPORTED
124 #endif
125 
126 #define CXGB_TX_CLEANUP_THRESHOLD        32
127 
128 
129 #ifdef DEBUG_PRINT
130 #define DPRINTF printf
131 #else
132 #define DPRINTF(...)
133 #endif
134 
135 #define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
136 #define TX_MAX_SEGS                      36     /* maximum supported by card     */
137 
138 #define TX_MAX_DESC                       4     /* max descriptors per packet    */
139 
140 
141 #define TX_START_MIN_DESC  (TX_MAX_DESC << 2)
142 #define TX_START_MAX_DESC (TX_MAX_DESC << 3)    /* maximum number of descriptors
143 						 * call to start used per 	 */
144 
145 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4)    /* maximum tx descriptors
146 						 * to clean per iteration        */
147 #define TX_WR_SIZE_MAX    11*1024              /* the maximum total size of packets aggregated into a single
148 						* TX WR
149 						*/
150 #define TX_WR_COUNT_MAX         7              /* the maximum total number of packets that can be
151 						* aggregated into a single TX WR
152 						*/
153 
154 
155 #if defined(__i386__) || defined(__amd64__)
156 #define mb()    __asm volatile("mfence":::"memory")
157 #define rmb()   __asm volatile("lfence":::"memory")
158 #define wmb()   __asm volatile("sfence" ::: "memory")
159 #define smp_mb() mb()
160 
161 #define L1_CACHE_BYTES 128
162 static __inline
163 void prefetch(void *x)
164 {
165         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
166 }
167 
168 extern void kdb_backtrace(void);
169 
170 #define WARN_ON(condition) do { \
171        if (__predict_false((condition)!=0)) {  \
172                 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
173                 kdb_backtrace(); \
174         } \
175 } while (0)
176 
177 
178 #else /* !i386 && !amd64 */
179 #define mb()
180 #define rmb()
181 #define wmb()
182 #define smp_mb()
183 #define prefetch(x)
184 #define L1_CACHE_BYTES 32
185 #endif
186 
187 struct buf_ring {
188 	caddr_t          *br_ring;
189 	volatile uint32_t br_cons;
190 	volatile uint32_t br_prod;
191 	int               br_size;
192 	struct mtx        br_lock;
193 };
194 
195 struct buf_ring *buf_ring_alloc(int count, int flags);
196 void buf_ring_free(struct buf_ring *);
197 
198 static __inline int
199 buf_ring_count(struct buf_ring *mr)
200 {
201 	int size = mr->br_size;
202 	uint32_t mask = size - 1;
203 
204 	return ((size + mr->br_prod - mr->br_cons) & mask);
205 }
206 
207 static __inline int
208 buf_ring_empty(struct buf_ring *mr)
209 {
210 	return (mr->br_cons == mr->br_prod);
211 }
212 
213 static __inline int
214 buf_ring_full(struct buf_ring *mr)
215 {
216 	uint32_t mask;
217 
218 	mask = mr->br_size - 1;
219 	return (mr->br_cons == ((mr->br_prod + 1) & mask));
220 }
221 
222 /*
223  * The producer and consumer are independently locked
224  * this relies on the consumer providing his own serialization
225  *
226  */
227 static __inline void *
228 buf_ring_dequeue(struct buf_ring *mr)
229 {
230 	uint32_t prod, cons, mask;
231 	caddr_t *ring, m;
232 
233 	ring = (caddr_t *)mr->br_ring;
234 	mask = mr->br_size - 1;
235 	cons = mr->br_cons;
236 	mb();
237 	prod = mr->br_prod;
238 	m = NULL;
239 	if (cons != prod) {
240 		m = ring[cons];
241 		ring[cons] = NULL;
242 		mr->br_cons = (cons + 1) & mask;
243 		mb();
244 	}
245 	return (m);
246 }
247 
248 #ifdef DEBUG_BUFRING
249 static __inline void
250 __buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
251 {
252 	int i;
253 
254 	for (i = 0; i < mr->br_size; i++)
255 		if (m == mr->br_ring[i])
256 			panic("%s:%d m=%p present prod=%d cons=%d idx=%d", file,
257 			    line, m, mr->br_prod, mr->br_cons, i);
258 }
259 
260 static __inline void
261 buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
262 {
263 	mtx_lock(&mr->br_lock);
264 	__buf_ring_scan(mr, m, file, line);
265 	mtx_unlock(&mr->br_lock);
266 }
267 
268 #else
269 static __inline void
270 __buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
271 {
272 }
273 
274 static __inline void
275 buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
276 {
277 }
278 #endif
279 
280 static __inline int
281 __buf_ring_enqueue(struct buf_ring *mr, void *m, char *file, int line)
282 {
283 
284 	uint32_t prod, cons, mask;
285 	int err;
286 
287 	mask = mr->br_size - 1;
288 	prod = mr->br_prod;
289 	mb();
290 	cons = mr->br_cons;
291 	__buf_ring_scan(mr, m, file, line);
292 	if (((prod + 1) & mask) != cons) {
293 		KASSERT(mr->br_ring[prod] == NULL, ("overwriting entry"));
294 		mr->br_ring[prod] = m;
295 		mb();
296 		mr->br_prod = (prod + 1) & mask;
297 		err = 0;
298 	} else
299 		err = ENOBUFS;
300 
301 	return (err);
302 }
303 
304 static __inline int
305 buf_ring_enqueue_(struct buf_ring *mr, void *m, char *file, int line)
306 {
307 	int err;
308 
309 	mtx_lock(&mr->br_lock);
310 	err = __buf_ring_enqueue(mr, m, file, line);
311 	mtx_unlock(&mr->br_lock);
312 
313 	return (err);
314 }
315 
316 #define buf_ring_enqueue(mr, m) buf_ring_enqueue_((mr), (m), __FILE__, __LINE__)
317 
318 
319 static __inline void *
320 buf_ring_peek(struct buf_ring *mr)
321 {
322 	int prod, cons, mask;
323 	caddr_t *ring, m;
324 
325 	ring = (caddr_t *)mr->br_ring;
326 	mask = mr->br_size - 1;
327 	cons = mr->br_cons;
328 	prod = mr->br_prod;
329 	m = NULL;
330 	if (cons != prod)
331 		m = ring[cons];
332 
333 	return (m);
334 }
335 
336 #define DBG_RX          (1 << 0)
337 static const int debug_flags = DBG_RX;
338 
339 #ifdef DEBUG_PRINT
340 #define DBG(flag, msg) do {	\
341 	if ((flag & debug_flags))	\
342 		printf msg; \
343 } while (0)
344 #else
345 #define DBG(...)
346 #endif
347 
348 #include <sys/syslog.h>
349 
350 #define promisc_rx_mode(rm)  ((rm)->port->ifp->if_flags & IFF_PROMISC)
351 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
352 
353 #define CH_ERR(adap, fmt, ...) log(LOG_ERR, fmt, ##__VA_ARGS__)
354 #define CH_WARN(adap, fmt, ...)	log(LOG_WARNING, fmt, ##__VA_ARGS__)
355 #define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, fmt, ##__VA_ARGS__)
356 
357 #define t3_os_sleep(x) DELAY((x) * 1000)
358 
359 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | (1<<bit)), ((*(p)) & ~(1<<bit)))
360 
361 #define max_t(type, a, b) (type)max((a), (b))
362 #define net_device ifnet
363 #define cpu_to_be32            htobe32
364 
365 /* Standard PHY definitions */
366 #define BMCR_LOOPBACK		BMCR_LOOP
367 #define BMCR_ISOLATE		BMCR_ISO
368 #define BMCR_ANENABLE		BMCR_AUTOEN
369 #define BMCR_SPEED1000		BMCR_SPEED1
370 #define BMCR_SPEED100		BMCR_SPEED0
371 #define BMCR_ANRESTART		BMCR_STARTNEG
372 #define BMCR_FULLDPLX		BMCR_FDX
373 #define BMSR_LSTATUS		BMSR_LINK
374 #define BMSR_ANEGCOMPLETE	BMSR_ACOMP
375 
376 #define MII_LPA			MII_ANLPAR
377 #define MII_ADVERTISE		MII_ANAR
378 #define MII_CTRL1000		MII_100T2CR
379 
380 #define ADVERTISE_PAUSE_CAP	ANAR_FC
381 #define ADVERTISE_PAUSE_ASYM	ANAR_X_PAUSE_ASYM
382 #define ADVERTISE_PAUSE		ANAR_X_PAUSE_SYM
383 #define ADVERTISE_1000HALF	ANAR_X_HD
384 #define ADVERTISE_1000FULL	ANAR_X_FD
385 #define ADVERTISE_10FULL	ANAR_10_FD
386 #define ADVERTISE_10HALF	ANAR_10
387 #define ADVERTISE_100FULL	ANAR_TX_FD
388 #define ADVERTISE_100HALF	ANAR_TX
389 
390 
391 #define ADVERTISE_1000XHALF	ANAR_X_HD
392 #define ADVERTISE_1000XFULL	ANAR_X_FD
393 #define ADVERTISE_1000XPSE_ASYM	ANAR_X_PAUSE_ASYM
394 #define ADVERTISE_1000XPAUSE	ANAR_X_PAUSE_SYM
395 
396 #define ADVERTISE_CSMA		ANAR_CSMA
397 #define ADVERTISE_NPAGE		ANAR_NP
398 
399 
400 /* Standard PCI Extended Capaibilities definitions */
401 #define PCI_CAP_ID_VPD	0x03
402 #define PCI_VPD_ADDR	2
403 #define PCI_VPD_ADDR_F	0x8000
404 #define PCI_VPD_DATA	4
405 
406 #define PCI_CAP_ID_EXP	0x10
407 #define PCI_EXP_DEVCTL	8
408 #define PCI_EXP_DEVCTL_PAYLOAD 0x00e0
409 #define PCI_EXP_LNKCTL	16
410 #define PCI_EXP_LNKSTA	18
411 
412 /*
413  * Linux compatibility macros
414  */
415 
416 /* Some simple translations */
417 #define __devinit
418 #define udelay(x) DELAY(x)
419 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
420 #define le32_to_cpu(x) le32toh(x)
421 #define le16_to_cpu(x) le16toh(x)
422 #define cpu_to_le32(x) htole32(x)
423 #define swab32(x) bswap32(x)
424 #define simple_strtoul strtoul
425 
426 
427 #ifndef LINUX_TYPES_DEFINED
428 typedef uint8_t 	u8;
429 typedef uint16_t 	u16;
430 typedef uint32_t 	u32;
431 typedef uint64_t 	u64;
432 
433 typedef uint8_t		__u8;
434 typedef uint16_t	__u16;
435 typedef uint32_t	__u32;
436 typedef uint8_t		__be8;
437 typedef uint16_t	__be16;
438 typedef uint32_t	__be32;
439 typedef uint64_t	__be64;
440 #endif
441 
442 
443 #if BYTE_ORDER == BIG_ENDIAN
444 #define __BIG_ENDIAN_BITFIELD
445 #elif BYTE_ORDER == LITTLE_ENDIAN
446 #define __LITTLE_ENDIAN_BITFIELD
447 #else
448 #error "Must set BYTE_ORDER"
449 #endif
450 
451 /* Indicates what features are supported by the interface. */
452 #define SUPPORTED_10baseT_Half          (1 << 0)
453 #define SUPPORTED_10baseT_Full          (1 << 1)
454 #define SUPPORTED_100baseT_Half         (1 << 2)
455 #define SUPPORTED_100baseT_Full         (1 << 3)
456 #define SUPPORTED_1000baseT_Half        (1 << 4)
457 #define SUPPORTED_1000baseT_Full        (1 << 5)
458 #define SUPPORTED_Autoneg               (1 << 6)
459 #define SUPPORTED_TP                    (1 << 7)
460 #define SUPPORTED_AUI                   (1 << 8)
461 #define SUPPORTED_MII                   (1 << 9)
462 #define SUPPORTED_FIBRE                 (1 << 10)
463 #define SUPPORTED_BNC                   (1 << 11)
464 #define SUPPORTED_10000baseT_Full       (1 << 12)
465 #define SUPPORTED_Pause                 (1 << 13)
466 #define SUPPORTED_Asym_Pause            (1 << 14)
467 
468 /* Indicates what features are advertised by the interface. */
469 #define ADVERTISED_10baseT_Half         (1 << 0)
470 #define ADVERTISED_10baseT_Full         (1 << 1)
471 #define ADVERTISED_100baseT_Half        (1 << 2)
472 #define ADVERTISED_100baseT_Full        (1 << 3)
473 #define ADVERTISED_1000baseT_Half       (1 << 4)
474 #define ADVERTISED_1000baseT_Full       (1 << 5)
475 #define ADVERTISED_Autoneg              (1 << 6)
476 #define ADVERTISED_TP                   (1 << 7)
477 #define ADVERTISED_AUI                  (1 << 8)
478 #define ADVERTISED_MII                  (1 << 9)
479 #define ADVERTISED_FIBRE                (1 << 10)
480 #define ADVERTISED_BNC                  (1 << 11)
481 #define ADVERTISED_10000baseT_Full      (1 << 12)
482 #define ADVERTISED_Pause                (1 << 13)
483 #define ADVERTISED_Asym_Pause           (1 << 14)
484 
485 /* Enable or disable autonegotiation.  If this is set to enable,
486  * the forced link modes above are completely ignored.
487  */
488 #define AUTONEG_DISABLE         0x00
489 #define AUTONEG_ENABLE          0x01
490 
491 #define SPEED_10		10
492 #define SPEED_100		100
493 #define SPEED_1000		1000
494 #define SPEED_10000		10000
495 #define DUPLEX_HALF		0
496 #define DUPLEX_FULL		1
497 
498 #endif
499