1218792Snp/*-
2218792Snp * Copyright (c) 2010 Chelsio Communications, Inc.
3218792Snp * All rights reserved.
4218792Snp * Written by: Navdeep Parhar <np@FreeBSD.org>
5218792Snp *
6218792Snp * Redistribution and use in source and binary forms, with or without
7218792Snp * modification, are permitted provided that the following conditions
8218792Snp * are met:
9218792Snp * 1. Redistributions of source code must retain the above copyright
10218792Snp *    notice, this list of conditions and the following disclaimer.
11218792Snp * 2. Redistributions in binary form must reproduce the above copyright
12218792Snp *    notice, this list of conditions and the following disclaimer in the
13218792Snp *    documentation and/or other materials provided with the distribution.
14218792Snp *
15218792Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16218792Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17218792Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18218792Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19218792Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20218792Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21218792Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22218792Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23218792Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24218792Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25218792Snp * SUCH DAMAGE.
26218792Snp *
27218792Snp * $FreeBSD$
28218792Snp *
29218792Snp */
30218792Snp
31218792Snp#ifndef __CXGBE_OSDEP_H_
32218792Snp#define __CXGBE_OSDEP_H_
33218792Snp
34218792Snp#include <sys/cdefs.h>
35218792Snp#include <sys/ctype.h>
36218792Snp#include <sys/types.h>
37218792Snp#include <sys/param.h>
38218792Snp#include <sys/endian.h>
39218792Snp#include <sys/systm.h>
40218792Snp#include <sys/syslog.h>
41218792Snp#include <dev/pci/pcireg.h>
42218792Snp
43218792Snp#define CH_ERR(adap, fmt, ...) log(LOG_ERR, fmt, ##__VA_ARGS__)
44218792Snp#define CH_WARN(adap, fmt, ...) log(LOG_WARNING, fmt, ##__VA_ARGS__)
45218792Snp#define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, fmt, ##__VA_ARGS__)
46218792Snp#define CH_WARN_RATELIMIT(adap, fmt, ...) log(LOG_WARNING, fmt, ##__VA_ARGS__)
47218792Snp
48218792Snptypedef int8_t  s8;
49218792Snptypedef int16_t s16;
50218792Snptypedef int32_t s32;
51218792Snptypedef int64_t s64;
52218792Snptypedef uint8_t  u8;
53218792Snptypedef uint16_t u16;
54218792Snptypedef uint32_t u32;
55218792Snptypedef uint64_t u64;
56218792Snptypedef uint8_t  __u8;
57218792Snptypedef uint16_t __u16;
58218792Snptypedef uint32_t __u32;
59218792Snptypedef uint64_t __u64;
60218792Snptypedef uint8_t  __be8;
61218792Snptypedef uint16_t __be16;
62218792Snptypedef uint32_t __be32;
63218792Snptypedef uint64_t __be64;
64218792Snp
65218792Snp#if BYTE_ORDER == BIG_ENDIAN
66218792Snp#define __BIG_ENDIAN_BITFIELD
67252495Snp#define htobe32_const(x) (x)
68218792Snp#elif BYTE_ORDER == LITTLE_ENDIAN
69218792Snp#define __LITTLE_ENDIAN_BITFIELD
70252495Snp#define htobe32_const(x) (((x) >> 24) | (((x) >> 8) & 0xff00) |	\
71252495Snp    ((((x) & 0xffffff) << 8) & 0xff0000) | ((((x) & 0xff) << 24) & 0xff000000))
72218792Snp#else
73218792Snp#error "Must set BYTE_ORDER"
74218792Snp#endif
75218792Snp
76229157Smdf#ifndef __bool_true_false_are_defined
77218792Snptypedef boolean_t bool;
78218792Snp#define false FALSE
79218792Snp#define true TRUE
80229157Smdf#endif
81218792Snp
82218792Snp#define mdelay(x) DELAY((x) * 1000)
83218792Snp#define udelay(x) DELAY(x)
84218792Snp
85218792Snp#define __devinit
86218792Snp#define simple_strtoul strtoul
87218792Snp#define DIV_ROUND_UP(x, y) howmany(x, y)
88218792Snp
89241467Snp#define ARRAY_SIZE(x) nitems(x)
90222509Snp#define container_of(p, s, f) ((s *)(((uint8_t *)(p)) - offsetof(s, f)))
91218792Snp
92218792Snp#define swab16(x) bswap16(x)
93218792Snp#define swab32(x) bswap32(x)
94218792Snp#define swab64(x) bswap64(x)
95218792Snp#define le16_to_cpu(x) le16toh(x)
96218792Snp#define le32_to_cpu(x) le32toh(x)
97218792Snp#define le64_to_cpu(x) le64toh(x)
98218792Snp#define cpu_to_le16(x) htole16(x)
99218792Snp#define cpu_to_le32(x) htole32(x)
100218792Snp#define cpu_to_le64(x) htole64(x)
101218792Snp#define be16_to_cpu(x) be16toh(x)
102218792Snp#define be32_to_cpu(x) be32toh(x)
103218792Snp#define be64_to_cpu(x) be64toh(x)
104218792Snp#define cpu_to_be16(x) htobe16(x)
105218792Snp#define cpu_to_be32(x) htobe32(x)
106218792Snp#define cpu_to_be64(x) htobe64(x)
107218792Snp
108218792Snp#define SPEED_10	10
109218792Snp#define SPEED_100	100
110218792Snp#define SPEED_1000	1000
111218792Snp#define SPEED_10000	10000
112252495Snp#define SPEED_40000	40000
113218792Snp#define DUPLEX_HALF	0
114218792Snp#define DUPLEX_FULL	1
115218792Snp#define AUTONEG_DISABLE	0
116218792Snp#define AUTONEG_ENABLE	1
117218792Snp
118237925Snp#define PCI_DEVICE_ID	PCIR_DEVICE
119218792Snp#define PCI_CAP_ID_VPD  PCIY_VPD
120218792Snp#define PCI_VPD_ADDR    PCIR_VPD_ADDR
121218792Snp#define PCI_VPD_ADDR_F  0x8000
122218792Snp#define PCI_VPD_DATA    PCIR_VPD_DATA
123218792Snp
124218792Snp#define PCI_CAP_ID_EXP		PCIY_EXPRESS
125242015Sgavin#define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
126242015Sgavin#define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
127242015Sgavin#define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
128242015Sgavin#define PCI_EXP_LNKCTL		PCIER_LINK_CTL
129242015Sgavin#define PCI_EXP_LNKSTA		PCIER_LINK_STA
130242015Sgavin#define PCI_EXP_LNKSTA_CLS	PCIEM_LINK_STA_SPEED
131242015Sgavin#define PCI_EXP_LNKSTA_NLW	PCIEM_LINK_STA_WIDTH
132252495Snp#define PCI_EXP_DEVCTL2		PCIER_DEVICE_CTL2
133218792Snp
134218792Snpstatic inline int
135218792Snpilog2(long x)
136218792Snp{
137218792Snp	KASSERT(x > 0 && powerof2(x), ("%s: invalid arg %ld", __func__, x));
138218792Snp
139218792Snp	return (flsl(x) - 1);
140218792Snp}
141218792Snp
142218792Snpstatic inline char *
143218792Snpstrstrip(char *s)
144218792Snp{
145218792Snp	char c, *r, *trim_at;
146218792Snp
147218792Snp	while (isspace(*s))
148218792Snp		s++;
149218792Snp	r = trim_at = s;
150218792Snp
151218792Snp	while ((c = *s++) != 0) {
152218792Snp		if (!isspace(c))
153218792Snp			trim_at = s;
154218792Snp	}
155218792Snp	*trim_at = 0;
156218792Snp
157218792Snp	return (r);
158218792Snp}
159218792Snp
160218792Snp#endif
161