Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / pci / cxgb_osdep.h
blob90b4aac385b8005d48904ecdee11af6afd79cf23
1 /**************************************************************************
3 Copyright (c) 2007, Chelsio Inc.
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
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.
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.
29 $FreeBSD: src/sys/dev/cxgb/cxgb_osdep.h,v 1.14 2007/09/10 00:59:51 kmacy Exp $
31 ***************************************************************************/
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #ifdef __FreeBSD__
36 #include <sys/ctype.h>
37 #endif
38 #include <sys/endian.h>
39 #ifdef __FreeBSD__
40 #include <sys/bus.h>
41 #endif
43 #include <dev/mii/mii.h>
45 #ifdef __FreeBSD__
46 #ifdef CONFIG_DEFINED
47 #include <common/cxgb_version.h>
48 #include <cxgb_config.h>
49 #else
50 #include <dev/cxgb/common/cxgb_version.h>
51 #include <dev/cxgb/cxgb_config.h>
52 #endif
53 #endif
55 #ifndef _CXGB_OSDEP_H_
56 #define _CXGB_OSDEP_H_
58 #ifdef __NetBSD__
59 typedef char *caddr_t;
60 #include <dev/pci/cxgb_version.h>
61 #include <dev/pci/cxgb_config.h>
62 #include <sys/mbuf.h>
63 #include <machine/bus.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
69 #include <sys/simplelock.h>
71 #include <sys/kthread.h>
72 #include <sys/workqueue.h>
74 #include <sys/atomic.h>
76 void pci_enable_busmaster(device_t dev);
78 struct cxgb_task
80 const char *name;
81 void (*func)(struct work *, void *);
82 struct workqueue *wq;
83 struct work w;
84 void *context;
87 void cxgb_make_task(void *);
89 void m_cljset(struct mbuf *m, void *cl, int type);
91 #define mtx simplelock
92 #define mtx_init(a, b, c, d) { (a)->lock_data = __SIMPLELOCK_UNLOCKED; }
93 #define mtx_destroy(a)
94 #define mtx_lock(a) simple_lock(a)
95 #define mtx_unlock(a) simple_unlock(a)
96 #define mtx_trylock(a) simple_lock_try(a)
97 #define MA_OWNED 1
98 #define MA_NOTOWNED 0
99 #define mtx_assert(a, w)
101 #if 0
102 #define RT_LOCK_INIT(_rt) \
103 mtx_init(&(_rt)->rt_mtx, "rtentry", NULL, MTX_DEF | MTX_DUPOK)
104 #define RT_LOCK(_rt) mtx_lock(&(_rt)->rt_mtx)
105 #define RT_UNLOCK(_rt) mtx_unlock(&(_rt)->rt_mtx)
106 #define RT_LOCK_DESTROY(_rt) mtx_destroy(&(_rt)->rt_mtx)
107 #define RT_LOCK_ASSERT(_rt) mtx_assert(&(_rt)->rt_mtx, MA_OWNED)
108 #else
109 #define RT_LOCK_INIT(_rt)
110 #define RT_LOCK(_rt)
111 #define RT_UNLOCK(_rt)
112 #define RT_LOCK_DESTROY(_rt)
113 #define RT_LOCK_ASSERT(_rt)
114 #endif
116 #define RT_ADDREF(_rt) do { \
117 RT_LOCK_ASSERT(_rt); \
118 KASSERT((_rt)->rt_refcnt >= 0); \
119 (_rt)->rt_refcnt++; \
120 } while (0)
121 #define RT_REMREF(_rt) do { \
122 RT_LOCK_ASSERT(_rt); \
123 KASSERT((_rt)->rt_refcnt > 0); \
124 (_rt)->rt_refcnt--; \
125 } while (0)
128 #define EVL_VLID_MASK 0x0FFF
130 static inline void critical_enter(void)
134 static inline void critical_exit(void)
138 static inline void device_printf(device_t d, ...)
142 int atomic_fetchadd_int(volatile int *p, int v);
143 #if 0
144 int atomic_add_int(volatile int *p, int v);
145 #endif
146 int atomic_load_acq_int(volatile int *p);
147 void atomic_store_rel_int(volatile int *p, int v);
149 u_short in_cksum_hdr(struct ip *ih);
151 #define if_drv_flags if_flags
152 #define IFF_DRV_RUNNING IFF_RUNNING
153 #define IFF_DRV_OACTIVE IFF_OACTIVE
155 #define MJUM16BYTES (16*1024)
156 #define MJUMPAGESIZE PAGE_SIZE
158 #if 0
159 #define rw_rlock(x) rw_enter(x, RW_READER)
160 #define rw_runlock(x) rw_exit(x)
161 #define rw_wlock(x) rw_enter(x, RW_WRITER)
162 #define rw_wunlock(x) rw_exit(x)
163 #endif
165 #define callout_drain(x) callout_stop(x)
167 static inline int atomic_cmpset_ptr(volatile long *dst, long exp, long src)
169 if (*dst == exp)
171 *dst = src;
172 return (1);
174 return (0);
176 #define atomic_cmpset_int(a, b, c) atomic_cmpset_ptr((volatile long *)a, (long)b, (long)c)
178 static inline int atomic_set_int(volatile int *dst, int val)
180 *dst = val;
182 return (val);
185 static inline void log(int x, ...)
189 struct cxgb_attach_args
191 int port;
194 #define INT3 __asm("int $3")
196 static inline struct mbuf *
197 m_defrag(struct mbuf *m0, int flags)
199 struct mbuf *m;
200 MGETHDR(m, flags, MT_DATA);
201 if (m == NULL)
202 return NULL;
204 M_COPY_PKTHDR(m, m0);
205 MCLGET(m, flags);
206 if ((m->m_flags & M_EXT) == 0) {
207 m_free(m);
208 return NULL;
210 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
211 m->m_len = m->m_pkthdr.len;
212 return m;
215 #endif
218 typedef struct adapter adapter_t;
219 struct sge_rspq;
221 struct t3_mbuf_hdr {
222 struct mbuf *mh_head;
223 struct mbuf *mh_tail;
227 #define PANIC_IF(exp) do { \
228 if (exp) \
229 panic("BUG: %s", exp); \
230 } while (0)
233 #define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
234 #define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
236 #if __FreeBSD_version > 700030
237 #define INTR_FILTERS
238 #define FIRMWARE_LATEST
239 #endif
241 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
242 #define FIRMWARE_LATEST
243 #endif
245 #if __FreeBSD_version > 700000
246 #define MSI_SUPPORTED
247 #define TSO_SUPPORTED
248 #define VLAN_SUPPORTED
249 #define TASKQUEUE_CURRENT
250 #else
251 #define if_name(ifp) (ifp)->if_xname
252 #define M_SANITY(m, n)
253 #endif
255 #define __read_mostly __section(".data.read_mostly")
258 * Workaround for weird Chelsio issue
260 #if __FreeBSD_version > 700029
261 #define PRIV_SUPPORTED
262 #endif
264 #define CXGB_TX_CLEANUP_THRESHOLD 32
266 #define LOG_WARNING 1
267 #define LOG_ERR 2
269 #ifdef DEBUG_PRINT
270 #define DPRINTF printf
271 #else
272 #define DPRINTF(...)
273 #endif
275 #define TX_MAX_SIZE (1 << 16) /* 64KB */
276 #define TX_MAX_SEGS 36 /* maximum supported by card */
277 #define TX_MAX_DESC 4 /* max descriptors per packet */
279 #define TX_START_MIN_DESC (TX_MAX_DESC << 2)
281 #if 0
282 #define TX_START_MAX_DESC (TX_ETH_Q_SIZE >> 2) /* maximum number of descriptors */
283 #endif
285 #define TX_START_MAX_DESC (TX_MAX_DESC << 3) /* maximum number of descriptors
286 * call to start used per */
288 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4) /* maximum tx descriptors
289 * to clean per iteration */
292 #if defined(__i386__) || defined(__amd64__)
293 #define mb() __asm volatile("mfence":::"memory")
294 #define rmb() __asm volatile("lfence":::"memory")
295 #define wmb() __asm volatile("sfence" ::: "memory")
296 #define smp_mb() mb()
298 #define L1_CACHE_BYTES 64
299 static __inline
300 void prefetch(void *x)
302 __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
305 extern void kdb_backtrace(void);
307 #define WARN_ON(condition) do { \
308 if (unlikely((condition)!=0)) { \
309 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __func__); \
310 kdb_backtrace(); \
312 } while (0)
315 #else /* !i386 && !amd64 */
316 #define mb()
317 #define rmb()
318 #define wmb()
319 #define smp_mb()
320 #define prefetch(x)
321 #define L1_CACHE_BYTES 32
322 #endif
323 #define DBG_RX (1 << 0)
324 static const int debug_flags = DBG_RX;
326 #ifdef DEBUG_PRINT
327 #define DBG(flag, msg) do { \
328 if ((flag & debug_flags)) \
329 printf msg; \
330 } while (0)
331 #else
332 #define DBG(...)
333 #endif
335 #define promisc_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_PROMISC)
336 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
338 #ifdef __FreeBSD__
339 #define CH_ERR(adap, fmt, ...)device_printf(adap->dev, fmt, ##__VA_ARGS__);
341 #define CH_WARN(adap, fmt, ...) device_printf(adap->dev, fmt, ##__VA_ARGS__)
342 #define CH_ALERT(adap, fmt, ...) device_printf(adap->dev, fmt, ##__VA_ARGS__)
343 #endif
344 #ifdef __NetBSD__
345 #define CH_ERR(adap, fmt, ...) { }
347 #define CH_WARN(adap, fmt, ...) { }
348 #define CH_ALERT(adap, fmt, ...) { }
349 #endif
351 #define t3_os_sleep(x) DELAY((x) * 1000)
353 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | bit), ((*(p)) & ~bit))
356 #define max_t(type, a, b) (type)max((a), (b))
357 #define net_device ifnet
358 #define cpu_to_be32 htobe32
362 /* Standard PHY definitions */
363 #define BMCR_LOOPBACK BMCR_LOOP
364 #define BMCR_ISOLATE BMCR_ISO
365 #define BMCR_ANENABLE BMCR_AUTOEN
366 #define BMCR_SPEED1000 BMCR_SPEED1
367 #define BMCR_SPEED100 BMCR_SPEED0
368 #define BMCR_ANRESTART BMCR_STARTNEG
369 #define BMCR_FULLDPLX BMCR_FDX
370 #define BMSR_LSTATUS BMSR_LINK
371 #define BMSR_ANEGCOMPLETE BMSR_ACOMP
373 #define MII_LPA MII_ANLPAR
374 #define MII_ADVERTISE MII_ANAR
375 #define MII_CTRL1000 MII_100T2CR
377 #define ADVERTISE_PAUSE_CAP ANAR_FC
378 #define ADVERTISE_PAUSE_ASYM 0x0800
379 #define ADVERTISE_1000HALF ANAR_X_HD
380 #define ADVERTISE_1000FULL ANAR_X_FD
381 #define ADVERTISE_10FULL ANAR_10_FD
382 #define ADVERTISE_10HALF ANAR_10
383 #define ADVERTISE_100FULL ANAR_TX_FD
384 #define ADVERTISE_100HALF ANAR_TX
386 /* Standard PCI Extended Capaibilities definitions */
387 #define PCI_CAP_ID_VPD 0x03
388 #define PCI_VPD_ADDR 2
389 #define PCI_VPD_ADDR_F 0x8000
390 #define PCI_VPD_DATA 4
392 #define PCI_CAP_ID_EXP 0x10
393 #define PCI_EXP_DEVCTL 8
394 #define PCI_EXP_DEVCTL_PAYLOAD 0x00e0
395 #define PCI_EXP_LNKCTL 16
396 #define PCI_EXP_LNKSTA 18
399 * Linux compatibility macros
402 /* Some simple translations */
403 #define __devinit
404 #define udelay(x) DELAY(x)
405 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
406 #define le32_to_cpu(x) le32toh(x)
407 #define cpu_to_le32(x) htole32(x)
408 #define swab32(x) bswap32(x)
409 #define simple_strtoul strtoul
411 /* More types and endian definitions */
412 typedef uint8_t u8;
413 typedef uint16_t u16;
414 typedef uint32_t u32;
415 typedef uint64_t u64;
417 typedef uint8_t __u8;
418 typedef uint16_t __u16;
419 typedef uint32_t __u32;
420 typedef uint8_t __be8;
421 typedef uint16_t __be16;
422 typedef uint32_t __be32;
423 typedef uint64_t __be64;
425 #if BYTE_ORDER == BIG_ENDIAN
426 #define __BIG_ENDIAN_BITFIELD
427 #elif BYTE_ORDER == LITTLE_ENDIAN
428 #define __LITTLE_ENDIAN_BITFIELD
429 #else
430 #error "Must set BYTE_ORDER"
431 #endif
433 /* Indicates what features are supported by the interface. */
434 #define SUPPORTED_10baseT_Half (1 << 0)
435 #define SUPPORTED_10baseT_Full (1 << 1)
436 #define SUPPORTED_100baseT_Half (1 << 2)
437 #define SUPPORTED_100baseT_Full (1 << 3)
438 #define SUPPORTED_1000baseT_Half (1 << 4)
439 #define SUPPORTED_1000baseT_Full (1 << 5)
440 #define SUPPORTED_Autoneg (1 << 6)
441 #define SUPPORTED_TP (1 << 7)
442 #define SUPPORTED_AUI (1 << 8)
443 #define SUPPORTED_MII (1 << 9)
444 #define SUPPORTED_FIBRE (1 << 10)
445 #define SUPPORTED_BNC (1 << 11)
446 #define SUPPORTED_10000baseT_Full (1 << 12)
447 #define SUPPORTED_Pause (1 << 13)
448 #define SUPPORTED_Asym_Pause (1 << 14)
450 /* Indicates what features are advertised by the interface. */
451 #define ADVERTISED_10baseT_Half (1 << 0)
452 #define ADVERTISED_10baseT_Full (1 << 1)
453 #define ADVERTISED_100baseT_Half (1 << 2)
454 #define ADVERTISED_100baseT_Full (1 << 3)
455 #define ADVERTISED_1000baseT_Half (1 << 4)
456 #define ADVERTISED_1000baseT_Full (1 << 5)
457 #define ADVERTISED_Autoneg (1 << 6)
458 #define ADVERTISED_TP (1 << 7)
459 #define ADVERTISED_AUI (1 << 8)
460 #define ADVERTISED_MII (1 << 9)
461 #define ADVERTISED_FIBRE (1 << 10)
462 #define ADVERTISED_BNC (1 << 11)
463 #define ADVERTISED_10000baseT_Full (1 << 12)
464 #define ADVERTISED_Pause (1 << 13)
465 #define ADVERTISED_Asym_Pause (1 << 14)
467 /* Enable or disable autonegotiation. If this is set to enable,
468 * the forced link modes above are completely ignored.
470 #define AUTONEG_DISABLE 0x00
471 #define AUTONEG_ENABLE 0x01
473 #define SPEED_10 10
474 #define SPEED_100 100
475 #define SPEED_1000 1000
476 #define SPEED_10000 10000
477 #define DUPLEX_HALF 0
478 #define DUPLEX_FULL 1
480 #endif