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: stable/11/sys/dev/cxgbe/osdep.h 346962 2019-04-30 16:30:16Z 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
43346962Snp#define CH_ERR(adap, fmt, ...) log(LOG_ERR, "%s: " fmt, \
44346962Snp    device_get_nameunit(adap->dev), ##__VA_ARGS__)
45346962Snp#define CH_WARN(adap, fmt, ...) log(LOG_WARNING, "%s: " fmt, \
46346962Snp    device_get_nameunit(adap->dev), ##__VA_ARGS__)
47346962Snp#define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, "%s: " fmt, \
48346962Snp    device_get_nameunit(adap->dev), ##__VA_ARGS__)
49346962Snp#define CH_WARN_RATELIMIT(adap, fmt, ...) log(LOG_WARNING, "%s: " fmt, \
50346962Snp    device_get_nameunit(adap->dev), ##__VA_ARGS__)
51218792Snp
52255005Snp#ifndef LINUX_TYPES_DEFINED
53218792Snptypedef int8_t  s8;
54218792Snptypedef int16_t s16;
55218792Snptypedef int32_t s32;
56218792Snptypedef int64_t s64;
57218792Snptypedef uint8_t  u8;
58218792Snptypedef uint16_t u16;
59218792Snptypedef uint32_t u32;
60218792Snptypedef uint64_t u64;
61218792Snptypedef uint8_t  __u8;
62218792Snptypedef uint16_t __u16;
63218792Snptypedef uint32_t __u32;
64218792Snptypedef uint64_t __u64;
65218792Snptypedef uint8_t  __be8;
66218792Snptypedef uint16_t __be16;
67218792Snptypedef uint32_t __be32;
68218792Snptypedef uint64_t __be64;
69218792Snp
70218792Snp#if BYTE_ORDER == BIG_ENDIAN
71218792Snp#define __BIG_ENDIAN_BITFIELD
72218792Snp#elif BYTE_ORDER == LITTLE_ENDIAN
73218792Snp#define __LITTLE_ENDIAN_BITFIELD
74218792Snp#else
75218792Snp#error "Must set BYTE_ORDER"
76218792Snp#endif
77218792Snp
78228443Smdf#ifndef __bool_true_false_are_defined
79218792Snptypedef boolean_t bool;
80218792Snp#define false FALSE
81218792Snp#define true TRUE
82228443Smdf#endif
83218792Snp
84296489Snp#define __force
85296489Snp
86218792Snp#define mdelay(x) DELAY((x) * 1000)
87218792Snp#define udelay(x) DELAY(x)
88218792Snp
89218792Snp#define simple_strtoul strtoul
90218792Snp#define DIV_ROUND_UP(x, y) howmany(x, y)
91218792Snp
92240452Snp#define ARRAY_SIZE(x) nitems(x)
93222509Snp#define container_of(p, s, f) ((s *)(((uint8_t *)(p)) - offsetof(s, f)))
94218792Snp
95296490Snp#define swab16(x) bswap16(x)
96296490Snp#define swab32(x) bswap32(x)
97296490Snp#define swab64(x) bswap64(x)
98218792Snp#define le16_to_cpu(x) le16toh(x)
99218792Snp#define le32_to_cpu(x) le32toh(x)
100218792Snp#define le64_to_cpu(x) le64toh(x)
101218792Snp#define cpu_to_le16(x) htole16(x)
102218792Snp#define cpu_to_le32(x) htole32(x)
103218792Snp#define cpu_to_le64(x) htole64(x)
104218792Snp#define be16_to_cpu(x) be16toh(x)
105218792Snp#define be32_to_cpu(x) be32toh(x)
106218792Snp#define be64_to_cpu(x) be64toh(x)
107218792Snp#define cpu_to_be16(x) htobe16(x)
108218792Snp#define cpu_to_be32(x) htobe32(x)
109218792Snp#define cpu_to_be64(x) htobe64(x)
110218792Snp
111218792Snp#define DUPLEX_HALF	0
112218792Snp#define DUPLEX_FULL	1
113339399Snp#define AUTONEG_AUTO	(-1)
114218792Snp#define AUTONEG_DISABLE	0
115218792Snp#define AUTONEG_ENABLE	1
116218792Snp
117237436Snp#define PCI_DEVICE_ID	PCIR_DEVICE
118218792Snp#define PCI_CAP_ID_VPD  PCIY_VPD
119218792Snp#define PCI_VPD_ADDR    PCIR_VPD_ADDR
120218792Snp#define PCI_VPD_ADDR_F  0x8000
121218792Snp#define PCI_VPD_DATA    PCIR_VPD_DATA
122218792Snp
123218792Snp#define PCI_CAP_ID_EXP		PCIY_EXPRESS
124240680Sgavin#define PCI_EXP_DEVCTL		PCIER_DEVICE_CTL
125240680Sgavin#define PCI_EXP_DEVCTL_PAYLOAD	PCIEM_CTL_MAX_PAYLOAD
126240680Sgavin#define PCI_EXP_DEVCTL_READRQ	PCIEM_CTL_MAX_READ_REQUEST
127240680Sgavin#define PCI_EXP_LNKCTL		PCIER_LINK_CTL
128240680Sgavin#define PCI_EXP_LNKSTA		PCIER_LINK_STA
129240680Sgavin#define PCI_EXP_LNKSTA_CLS	PCIEM_LINK_STA_SPEED
130240680Sgavin#define PCI_EXP_LNKSTA_NLW	PCIEM_LINK_STA_WIDTH
131240693Sgavin#define PCI_EXP_DEVCTL2		PCIER_DEVICE_CTL2
132218792Snp
133218792Snpstatic inline int
134218792Snpilog2(long x)
135218792Snp{
136218792Snp	KASSERT(x > 0 && powerof2(x), ("%s: invalid arg %ld", __func__, x));
137218792Snp
138218792Snp	return (flsl(x) - 1);
139218792Snp}
140218792Snp
141218792Snpstatic inline char *
142218792Snpstrstrip(char *s)
143218792Snp{
144218792Snp	char c, *r, *trim_at;
145218792Snp
146218792Snp	while (isspace(*s))
147218792Snp		s++;
148218792Snp	r = trim_at = s;
149218792Snp
150218792Snp	while ((c = *s++) != 0) {
151218792Snp		if (!isspace(c))
152218792Snp			trim_at = s;
153218792Snp	}
154218792Snp	*trim_at = 0;
155218792Snp
156218792Snp	return (r);
157218792Snp}
158255005Snp#endif /* LINUX_TYPES_DEFINED */
159218792Snp
160218792Snp#endif
161