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