xref: /freebsd/sys/dev/qlxgbe/ql_os.h (revision 4fe1295c964fa712dd763e3852187da8724ef79a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013-2016 Qlogic Corporation
5  * All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
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 COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  *  AND 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 COPYRIGHT OWNER OR CONTRIBUTORS BE
21  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  *  POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 /*
32  * File: ql_os.h
33  * Author : David C Somayajulu, Qlogic Corporation, Aliso Viejo, CA 92656.
34  */
35 
36 #ifndef _QL_OS_H_
37 #define _QL_OS_H_
38 
39 #include "opt_inet.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/kernel.h>
49 #include <sys/sockio.h>
50 #include <sys/types.h>
51 #include <machine/atomic.h>
52 #include <machine/_inttypes.h>
53 #include <sys/conf.h>
54 
55 #if __FreeBSD_version < 900044
56 #error FreeBSD Version not supported - use version >= 900044
57 #endif
58 
59 #include <net/if.h>
60 #include <net/if_var.h>
61 #include <net/if_arp.h>
62 #include <net/ethernet.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/bpf.h>
66 #include <net/if_types.h>
67 #include <net/if_vlan_var.h>
68 
69 #include <netinet/in_systm.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip6.h>
74 #include <netinet/tcp.h>
75 #include <netinet/udp.h>
76 #include <netinet/in_var.h>
77 #include <netinet/tcp_lro.h>
78 
79 #include <sys/bus.h>
80 #include <machine/bus.h>
81 #include <sys/rman.h>
82 #include <machine/resource.h>
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcivar.h>
85 #include <sys/mutex.h>
86 #include <sys/condvar.h>
87 #include <sys/proc.h>
88 #include <sys/sysctl.h>
89 #include <sys/endian.h>
90 #include <sys/taskqueue.h>
91 #include <sys/pcpu.h>
92 
93 #include <sys/unistd.h>
94 #include <sys/kthread.h>
95 
96 #define QLA_USEC_DELAY(usec)	DELAY(usec)
97 
98 static __inline int qla_ms_to_hz(int ms)
99 {
100 	int qla_hz;
101 
102 	struct timeval t;
103 
104 	t.tv_sec = ms / 1000;
105 	t.tv_usec = (ms % 1000) * 1000;
106 
107 	qla_hz = tvtohz(&t);
108 
109 	if (qla_hz < 0)
110 		qla_hz = 0x7fffffff;
111 	if (!qla_hz)
112 		qla_hz = 1;
113 
114 	return (qla_hz);
115 }
116 
117 static __inline int qla_sec_to_hz(int sec)
118 {
119 	struct timeval t;
120 
121 	t.tv_sec = sec;
122 	t.tv_usec = 0;
123 
124 	return (tvtohz(&t));
125 }
126 
127 static __inline uint64_t qla_get_usec_timestamp(void)
128 {
129 	struct timeval tv;
130 
131 	microuptime(&tv);
132 
133 	return ((uint64_t)(((uint64_t)tv.tv_sec) * 1000000 + tv.tv_usec));
134 }
135 
136 #define qla_host_to_le16(x)	htole16(x)
137 #define qla_host_to_le32(x)	htole32(x)
138 #define qla_host_to_le64(x)	htole64(x)
139 #define qla_host_to_be16(x)	htobe16(x)
140 #define qla_host_to_be32(x)	htobe32(x)
141 #define qla_host_to_be64(x)	htobe64(x)
142 
143 #define qla_le16_to_host(x)	le16toh(x)
144 #define qla_le32_to_host(x)	le32toh(x)
145 #define qla_le64_to_host(x)	le64toh(x)
146 #define qla_be16_to_host(x)	be16toh(x)
147 #define qla_be32_to_host(x)	be32toh(x)
148 #define qla_be64_to_host(x)	be64toh(x)
149 
150 MALLOC_DECLARE(M_QLA83XXBUF);
151 
152 #define qla_mdelay(fn, msecs)	\
153 	{\
154 		if (cold) \
155 			DELAY((msecs * 1000)); \
156 		else  \
157 			pause(fn, qla_ms_to_hz(msecs)); \
158 	}
159 
160 /*
161  * Locks
162  */
163 #define QLA_LOCK(ha, str, to_ms, no_sleep)	qla_lock(ha, str, to_ms, no_sleep)
164 #define QLA_UNLOCK(ha, str)			qla_unlock(ha, str)
165 
166 /*
167  * structure encapsulating a DMA buffer
168  */
169 struct qla_dma {
170         bus_size_t              alignment;
171         uint32_t                size;
172         void                    *dma_b;
173         bus_addr_t              dma_addr;
174         bus_dmamap_t            dma_map;
175         bus_dma_tag_t           dma_tag;
176 };
177 typedef struct qla_dma qla_dma_t;
178 
179 #endif /* #ifndef _QL_OS_H_ */
180