xref: /freebsd/sys/dev/cxgbe/osdep.h (revision 643ac419fafba89f5adda0e0ea75b538727453fb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  *
31  */
32 
33 #ifndef __CXGBE_OSDEP_H_
34 #define __CXGBE_OSDEP_H_
35 
36 #include <sys/cdefs.h>
37 #include <sys/ctype.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/systm.h>
42 #include <sys/syslog.h>
43 #include <dev/pci/pcireg.h>
44 
45 #define CH_ERR(adap, fmt, ...) log(LOG_ERR, "%s: " fmt, \
46     device_get_nameunit((adap)->dev), ##__VA_ARGS__)
47 #define CH_WARN(adap, fmt, ...) log(LOG_WARNING, "%s: " fmt, \
48     device_get_nameunit((adap)->dev), ##__VA_ARGS__)
49 #define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, "%s: " fmt, \
50     device_get_nameunit((adap)->dev), ##__VA_ARGS__)
51 #define CH_WARN_RATELIMIT(adap, fmt, ...) log(LOG_WARNING, "%s: " fmt, \
52     device_get_nameunit((adap)->dev), ##__VA_ARGS__)
53 
54 #ifndef LINUX_TYPES_DEFINED
55 typedef int8_t  s8;
56 typedef int16_t s16;
57 typedef int32_t s32;
58 typedef int64_t s64;
59 typedef uint8_t  u8;
60 typedef uint16_t u16;
61 typedef uint32_t u32;
62 typedef uint64_t u64;
63 typedef uint8_t  __u8;
64 typedef uint16_t __u16;
65 typedef uint32_t __u32;
66 typedef uint64_t __u64;
67 typedef uint8_t  __be8;
68 typedef uint16_t __be16;
69 typedef uint32_t __be32;
70 typedef uint64_t __be64;
71 
72 #if BYTE_ORDER == BIG_ENDIAN
73 #define __BIG_ENDIAN_BITFIELD
74 #elif BYTE_ORDER == LITTLE_ENDIAN
75 #define __LITTLE_ENDIAN_BITFIELD
76 #else
77 #error "Must set BYTE_ORDER"
78 #endif
79 
80 #ifndef __bool_true_false_are_defined
81 typedef boolean_t bool;
82 #define false FALSE
83 #define true TRUE
84 #endif
85 
86 #define __force
87 
88 #define mdelay(x) DELAY((x) * 1000)
89 #define udelay(x) DELAY(x)
90 
91 #define simple_strtoul strtoul
92 #define DIV_ROUND_UP(x, y) howmany(x, y)
93 
94 #define ARRAY_SIZE(x) nitems(x)
95 #define container_of(p, s, f) ((s *)(((uint8_t *)(p)) - offsetof(s, f)))
96 
97 #define swab16(x) bswap16(x)
98 #define swab32(x) bswap32(x)
99 #define swab64(x) bswap64(x)
100 #define le16_to_cpu(x) le16toh(x)
101 #define le32_to_cpu(x) le32toh(x)
102 #define le64_to_cpu(x) le64toh(x)
103 #define cpu_to_le16(x) htole16(x)
104 #define cpu_to_le32(x) htole32(x)
105 #define cpu_to_le64(x) htole64(x)
106 #define be16_to_cpu(x) be16toh(x)
107 #define be32_to_cpu(x) be32toh(x)
108 #define be64_to_cpu(x) be64toh(x)
109 #define cpu_to_be16(x) htobe16(x)
110 #define cpu_to_be32(x) htobe32(x)
111 #define cpu_to_be64(x) htobe64(x)
112 
113 #define DUPLEX_HALF	0
114 #define DUPLEX_FULL	1
115 #define AUTONEG_AUTO	(-1)
116 #define AUTONEG_DISABLE	0
117 #define AUTONEG_ENABLE	1
118 
119 #define PCI_DEVICE_ID	PCIR_DEVICE
120 #define PCI_CAP_ID_VPD  PCIY_VPD
121 #define PCI_VPD_ADDR    PCIR_VPD_ADDR
122 #define PCI_VPD_ADDR_F  0x8000
123 #define PCI_VPD_DATA    PCIR_VPD_DATA
124 
125 #define PCI_CAP_ID_EXP		PCIY_EXPRESS
126 #define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
127 #define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
128 #define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
129 #define PCI_EXP_LNKCTL		PCIER_LINK_CTL
130 #define PCI_EXP_LNKSTA		PCIER_LINK_STA
131 #define PCI_EXP_LNKSTA_CLS	PCIEM_LINK_STA_SPEED
132 #define PCI_EXP_LNKSTA_NLW	PCIEM_LINK_STA_WIDTH
133 #define PCI_EXP_DEVCTL2		PCIER_DEVICE_CTL2
134 
135 static inline int
136 ilog2(long x)
137 {
138 	KASSERT(x > 0 && powerof2(x), ("%s: invalid arg %ld", __func__, x));
139 
140 	return (flsl(x) - 1);
141 }
142 
143 static inline char *
144 strstrip(char *s)
145 {
146 	char c, *r, *trim_at;
147 
148 	while (isspace(*s))
149 		s++;
150 	r = trim_at = s;
151 
152 	while ((c = *s++) != 0) {
153 		if (!isspace(c))
154 			trim_at = s;
155 	}
156 	*trim_at = 0;
157 
158 	return (r);
159 }
160 #endif /* LINUX_TYPES_DEFINED */
161 
162 #endif
163