osdep.h revision 237436
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: head/sys/dev/cxgbe/osdep.h 237436 2012-06-22 07:51:15Z np $
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
67218792Snp#elif BYTE_ORDER == LITTLE_ENDIAN
68218792Snp#define __LITTLE_ENDIAN_BITFIELD
69218792Snp#else
70218792Snp#error "Must set BYTE_ORDER"
71218792Snp#endif
72218792Snp
73228443Smdf#ifndef __bool_true_false_are_defined
74218792Snptypedef boolean_t bool;
75218792Snp#define false FALSE
76218792Snp#define true TRUE
77228443Smdf#endif
78218792Snp
79218792Snp#define mdelay(x) DELAY((x) * 1000)
80218792Snp#define udelay(x) DELAY(x)
81218792Snp
82218792Snp#define __devinit
83218792Snp#define simple_strtoul strtoul
84218792Snp#define DIV_ROUND_UP(x, y) howmany(x, y)
85218792Snp
86218792Snp#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
87222509Snp#define container_of(p, s, f) ((s *)(((uint8_t *)(p)) - offsetof(s, f)))
88218792Snp
89218792Snp#define swab16(x) bswap16(x)
90218792Snp#define swab32(x) bswap32(x)
91218792Snp#define swab64(x) bswap64(x)
92218792Snp#define le16_to_cpu(x) le16toh(x)
93218792Snp#define le32_to_cpu(x) le32toh(x)
94218792Snp#define le64_to_cpu(x) le64toh(x)
95218792Snp#define cpu_to_le16(x) htole16(x)
96218792Snp#define cpu_to_le32(x) htole32(x)
97218792Snp#define cpu_to_le64(x) htole64(x)
98218792Snp#define be16_to_cpu(x) be16toh(x)
99218792Snp#define be32_to_cpu(x) be32toh(x)
100218792Snp#define be64_to_cpu(x) be64toh(x)
101218792Snp#define cpu_to_be16(x) htobe16(x)
102218792Snp#define cpu_to_be32(x) htobe32(x)
103218792Snp#define cpu_to_be64(x) htobe64(x)
104218792Snp
105218792Snp#define SPEED_10	10
106218792Snp#define SPEED_100	100
107218792Snp#define SPEED_1000	1000
108218792Snp#define SPEED_10000	10000
109218792Snp#define DUPLEX_HALF	0
110218792Snp#define DUPLEX_FULL	1
111218792Snp#define AUTONEG_DISABLE	0
112218792Snp#define AUTONEG_ENABLE	1
113218792Snp
114237436Snp#define PCI_DEVICE_ID	PCIR_DEVICE
115218792Snp#define PCI_CAP_ID_VPD  PCIY_VPD
116218792Snp#define PCI_VPD_ADDR    PCIR_VPD_ADDR
117218792Snp#define PCI_VPD_ADDR_F  0x8000
118218792Snp#define PCI_VPD_DATA    PCIR_VPD_DATA
119218792Snp
120218792Snp#define PCI_CAP_ID_EXP		PCIY_EXPRESS
121218792Snp#define PCI_EXP_DEVCTL		PCIR_EXPRESS_DEVICE_CTL
122218792Snp#define PCI_EXP_DEVCTL_PAYLOAD	PCIM_EXP_CTL_MAX_PAYLOAD
123218792Snp#define PCI_EXP_DEVCTL_READRQ	PCIM_EXP_CTL_MAX_READ_REQUEST
124218792Snp#define PCI_EXP_LNKCTL		PCIR_EXPRESS_LINK_CTL
125218792Snp#define PCI_EXP_LNKSTA		PCIR_EXPRESS_LINK_STA
126218792Snp#define PCI_EXP_LNKSTA_CLS	PCIM_LINK_STA_SPEED
127218792Snp#define PCI_EXP_LNKSTA_NLW	PCIM_LINK_STA_WIDTH
128228561Snp#define PCI_EXP_DEVCTL2		0x28
129218792Snp
130218792Snpstatic inline int
131218792Snpilog2(long x)
132218792Snp{
133218792Snp	KASSERT(x > 0 && powerof2(x), ("%s: invalid arg %ld", __func__, x));
134218792Snp
135218792Snp	return (flsl(x) - 1);
136218792Snp}
137218792Snp
138218792Snpstatic inline char *
139218792Snpstrstrip(char *s)
140218792Snp{
141218792Snp	char c, *r, *trim_at;
142218792Snp
143218792Snp	while (isspace(*s))
144218792Snp		s++;
145218792Snp	r = trim_at = s;
146218792Snp
147218792Snp	while ((c = *s++) != 0) {
148218792Snp		if (!isspace(c))
149218792Snp			trim_at = s;
150218792Snp	}
151218792Snp	*trim_at = 0;
152218792Snp
153218792Snp	return (r);
154218792Snp}
155218792Snp
156218792Snp#endif
157