cxgb_osdep.h revision 175311
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 175311 2008-01-14 05:56:03Z 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#define PANIC_IF(exp) do {                  \
65	if (exp)                            \
66		panic("BUG: %s", #exp);      \
67} while (0)
68
69#define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
70#define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
71#define m_set_sgl(m, sgl) ((m)->m_pkthdr.header = (sgl))
72#define m_get_sgl(m) ((bus_dma_segment_t *)(m)->m_pkthdr.header)
73#define m_set_sgllen(m, len) ((m)->m_pkthdr.ether_vtag = len)
74#define m_get_sgllen(m) ((m)->m_pkthdr.ether_vtag)
75
76/*
77 * XXX FIXME
78 */
79#define m_set_toep(m, a) ((m)->m_pkthdr.header = (a))
80#define m_get_toep(m) ((m)->m_pkthdr.header)
81#define m_set_handler(m, handler) ((m)->m_pkthdr.header = (handler))
82
83#define m_set_socket(m, a) ((m)->m_pkthdr.header = (a))
84#define m_get_socket(m) ((m)->m_pkthdr.header)
85
86#define MT_DONTFREE  128
87
88#if __FreeBSD_version > 700030
89#define INTR_FILTERS
90#define FIRMWARE_LATEST
91#endif
92
93#if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
94#define FIRMWARE_LATEST
95#endif
96
97#if __FreeBSD_version > 700000
98#define MSI_SUPPORTED
99#define TSO_SUPPORTED
100#define VLAN_SUPPORTED
101#define TASKQUEUE_CURRENT
102#else
103#define if_name(ifp) (ifp)->if_xname
104#define M_SANITY(m, n)
105#endif
106
107#define __read_mostly __attribute__((__section__(".data.read_mostly")))
108
109/*
110 * Workaround for weird Chelsio issue
111 */
112#if __FreeBSD_version > 700029
113#define PRIV_SUPPORTED
114#endif
115
116#define CXGB_TX_CLEANUP_THRESHOLD        32
117
118
119#ifdef DEBUG_PRINT
120#define DPRINTF printf
121#else
122#define DPRINTF(...)
123#endif
124
125#define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
126#define TX_MAX_SEGS                      36     /* maximum supported by card     */
127
128#define TX_MAX_DESC                       4     /* max descriptors per packet    */
129
130
131#define TX_START_MIN_DESC  (TX_MAX_DESC << 2)
132
133
134
135#define TX_START_MAX_DESC (TX_MAX_DESC << 3)    /* maximum number of descriptors
136						 * call to start used per 	 */
137
138#define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4)    /* maximum tx descriptors
139						 * to clean per iteration        */
140#define TX_WR_SIZE_MAX    11*1024              /* the maximum total size of packets aggregated into a single
141						* TX WR
142						*/
143#define TX_WR_COUNT_MAX         7              /* the maximum total number of packets that can be
144						* aggregated into a single TX WR
145						*/
146
147
148#if defined(__i386__) || defined(__amd64__)
149#define mb()    __asm volatile("mfence":::"memory")
150#define rmb()   __asm volatile("lfence":::"memory")
151#define wmb()   __asm volatile("sfence" ::: "memory")
152#define smp_mb() mb()
153
154#define L1_CACHE_BYTES 128
155static __inline
156void prefetch(void *x)
157{
158        __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
159}
160
161extern void kdb_backtrace(void);
162
163#define WARN_ON(condition) do { \
164        if (unlikely((condition)!=0)) { \
165                log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
166                kdb_backtrace(); \
167        } \
168} while (0)
169
170
171#else /* !i386 && !amd64 */
172#define mb()
173#define rmb()
174#define wmb()
175#define smp_mb()
176#define prefetch(x)
177#define L1_CACHE_BYTES 32
178#endif
179
180struct buf_ring {
181	caddr_t          *br_ring;
182	volatile uint32_t br_cons;
183	volatile uint32_t br_prod;
184	int               br_size;
185	struct mtx        br_lock;
186};
187
188struct buf_ring *buf_ring_alloc(int count, int flags);
189void buf_ring_free(struct buf_ring *);
190
191static __inline int
192buf_ring_count(struct buf_ring *mr)
193{
194	int size = mr->br_size;
195	uint32_t mask = size - 1;
196
197	return ((size + mr->br_prod - mr->br_cons) & mask);
198}
199
200static __inline int
201buf_ring_empty(struct buf_ring *mr)
202{
203	return (mr->br_cons == mr->br_prod);
204}
205
206static __inline int
207buf_ring_full(struct buf_ring *mr)
208{
209	uint32_t mask;
210
211	mask = mr->br_size - 1;
212	return (mr->br_cons == ((mr->br_prod + 1) & mask));
213}
214
215/*
216 * The producer and consumer are independently locked
217 * this relies on the consumer providing his own serialization
218 *
219 */
220static __inline void *
221buf_ring_dequeue(struct buf_ring *mr)
222{
223	uint32_t prod, cons, mask;
224	caddr_t *ring, m;
225
226	ring = (caddr_t *)mr->br_ring;
227	mask = mr->br_size - 1;
228	mtx_lock(&mr->br_lock);	/* XXX temporary ? */
229	cons = mr->br_cons;
230	mb();
231	prod = mr->br_prod;
232	m = NULL;
233	if (cons != prod) {
234		m = ring[cons];
235		ring[cons] = NULL;
236		mr->br_cons = (cons + 1) & mask;
237		mb();
238	}
239	mtx_unlock(&mr->br_lock);	/* XXX temporary ? */
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#define promisc_rx_mode(rm)  ((rm)->port->ifp->if_flags & IFF_PROMISC)
344#define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
345
346#define CH_ERR(adap, fmt, ...)device_printf(adap->dev, fmt, ##__VA_ARGS__);
347
348#define CH_WARN(adap, fmt, ...)	device_printf(adap->dev, fmt, ##__VA_ARGS__)
349#define CH_ALERT(adap, fmt, ...) device_printf(adap->dev, fmt, ##__VA_ARGS__)
350
351#define t3_os_sleep(x) DELAY((x) * 1000)
352
353#define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | (1<<bit)), ((*(p)) & ~(1<<bit)))
354
355#define max_t(type, a, b) (type)max((a), (b))
356#define net_device ifnet
357#define cpu_to_be32            htobe32
358
359/* Standard PHY definitions */
360#define BMCR_LOOPBACK		BMCR_LOOP
361#define BMCR_ISOLATE		BMCR_ISO
362#define BMCR_ANENABLE		BMCR_AUTOEN
363#define BMCR_SPEED1000		BMCR_SPEED1
364#define BMCR_SPEED100		BMCR_SPEED0
365#define BMCR_ANRESTART		BMCR_STARTNEG
366#define BMCR_FULLDPLX		BMCR_FDX
367#define BMSR_LSTATUS		BMSR_LINK
368#define BMSR_ANEGCOMPLETE	BMSR_ACOMP
369
370#define MII_LPA			MII_ANLPAR
371#define MII_ADVERTISE		MII_ANAR
372#define MII_CTRL1000		MII_100T2CR
373
374#define ADVERTISE_PAUSE_CAP	ANAR_FC
375#define ADVERTISE_PAUSE_ASYM	0x0800
376#define ADVERTISE_1000HALF	ANAR_X_HD
377#define ADVERTISE_1000FULL	ANAR_X_FD
378#define ADVERTISE_10FULL	ANAR_10_FD
379#define ADVERTISE_10HALF	ANAR_10
380#define ADVERTISE_100FULL	ANAR_TX_FD
381#define ADVERTISE_100HALF	ANAR_TX
382
383/* Standard PCI Extended Capaibilities definitions */
384#define PCI_CAP_ID_VPD	0x03
385#define PCI_VPD_ADDR	2
386#define PCI_VPD_ADDR_F	0x8000
387#define PCI_VPD_DATA	4
388
389#define PCI_CAP_ID_EXP	0x10
390#define PCI_EXP_DEVCTL	8
391#define PCI_EXP_DEVCTL_PAYLOAD 0x00e0
392#define PCI_EXP_LNKCTL	16
393#define PCI_EXP_LNKSTA	18
394
395/*
396 * Linux compatibility macros
397 */
398
399/* Some simple translations */
400#define __devinit
401#define udelay(x) DELAY(x)
402#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
403#define le32_to_cpu(x) le32toh(x)
404#define cpu_to_le32(x) htole32(x)
405#define swab32(x) bswap32(x)
406#define simple_strtoul strtoul
407
408
409typedef uint8_t u8;
410typedef uint16_t u16;
411typedef uint32_t u32;
412typedef uint64_t u64;
413
414typedef uint8_t       __u8;
415typedef uint16_t __u16;
416typedef uint32_t __u32;
417typedef uint8_t __be8;
418typedef uint16_t __be16;
419typedef uint32_t __be32;
420typedef uint64_t __be64;
421
422
423#if BYTE_ORDER == BIG_ENDIAN
424#define __BIG_ENDIAN_BITFIELD
425#elif BYTE_ORDER == LITTLE_ENDIAN
426#define __LITTLE_ENDIAN_BITFIELD
427#else
428#error "Must set BYTE_ORDER"
429#endif
430
431/* Indicates what features are supported by the interface. */
432#define SUPPORTED_10baseT_Half          (1 << 0)
433#define SUPPORTED_10baseT_Full          (1 << 1)
434#define SUPPORTED_100baseT_Half         (1 << 2)
435#define SUPPORTED_100baseT_Full         (1 << 3)
436#define SUPPORTED_1000baseT_Half        (1 << 4)
437#define SUPPORTED_1000baseT_Full        (1 << 5)
438#define SUPPORTED_Autoneg               (1 << 6)
439#define SUPPORTED_TP                    (1 << 7)
440#define SUPPORTED_AUI                   (1 << 8)
441#define SUPPORTED_MII                   (1 << 9)
442#define SUPPORTED_FIBRE                 (1 << 10)
443#define SUPPORTED_BNC                   (1 << 11)
444#define SUPPORTED_10000baseT_Full       (1 << 12)
445#define SUPPORTED_Pause                 (1 << 13)
446#define SUPPORTED_Asym_Pause            (1 << 14)
447
448/* Indicates what features are advertised by the interface. */
449#define ADVERTISED_10baseT_Half         (1 << 0)
450#define ADVERTISED_10baseT_Full         (1 << 1)
451#define ADVERTISED_100baseT_Half        (1 << 2)
452#define ADVERTISED_100baseT_Full        (1 << 3)
453#define ADVERTISED_1000baseT_Half       (1 << 4)
454#define ADVERTISED_1000baseT_Full       (1 << 5)
455#define ADVERTISED_Autoneg              (1 << 6)
456#define ADVERTISED_TP                   (1 << 7)
457#define ADVERTISED_AUI                  (1 << 8)
458#define ADVERTISED_MII                  (1 << 9)
459#define ADVERTISED_FIBRE                (1 << 10)
460#define ADVERTISED_BNC                  (1 << 11)
461#define ADVERTISED_10000baseT_Full      (1 << 12)
462#define ADVERTISED_Pause                (1 << 13)
463#define ADVERTISED_Asym_Pause           (1 << 14)
464
465/* Enable or disable autonegotiation.  If this is set to enable,
466 * the forced link modes above are completely ignored.
467 */
468#define AUTONEG_DISABLE         0x00
469#define AUTONEG_ENABLE          0x01
470
471#define SPEED_10		10
472#define SPEED_100		100
473#define SPEED_1000		1000
474#define SPEED_10000		10000
475#define DUPLEX_HALF		0
476#define DUPLEX_FULL		1
477
478#endif
479