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