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