xref: /freebsd/sys/dev/irdma/osdep.h (revision 777e472cd86b9394d07bf96c19dbafc2e1ff4fdc)
1 /*-
2  * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3  *
4  * Copyright (c) 2021 - 2022 Intel Corporation
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenFabrics.org BSD license below:
11  *
12  *   Redistribution and use in source and binary forms, with or
13  *   without modification, are permitted provided that the following
14  *   conditions are met:
15  *
16  *    - Redistributions of source code must retain the above
17  *	copyright notice, this list of conditions and the following
18  *	disclaimer.
19  *
20  *    - Redistributions in binary form must reproduce the above
21  *	copyright notice, this list of conditions and the following
22  *	disclaimer in the documentation and/or other materials
23  *	provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 /*$FreeBSD$*/
35 
36 #ifndef _ICRDMA_OSDEP_H_
37 #define _ICRDMA_OSDEP_H_
38 
39 #include <linux/dma-mapping.h>
40 #include <linux/etherdevice.h>
41 #include <linux/fs.h>
42 #include <linux/if_ether.h>
43 #include <linux/interrupt.h>
44 #include <linux/io.h>
45 #include <linux/jhash.h>
46 #include <linux/list.h>
47 #include <linux/log2.h>
48 #include <linux/mutex.h>
49 #include <linux/pci.h>
50 #include <linux/random.h>
51 #include <linux/spinlock.h>
52 #include <linux/timer.h>
53 #include <linux/workqueue.h>
54 
55 #include <sys/bus.h>
56 #include <machine/bus.h>
57 
58 #define ATOMIC atomic_t
59 #define IOMEM
60 #define IRDMA_NTOHS(a) ntohs(a)
61 #define MAKEMASK(m, s) ((m) << (s))
62 #define OS_TIMER timer_list
63 #define DECLARE_HASHTABLE(n, b) struct hlist_head (n)[1 << (b)]
64 #define HASH_MIN(v, b) (sizeof(v) <= 4 ? hash_32(v, b) : hash_long(v, b))
65 #define HASH_FOR_EACH_RCU(n, b, o, m) 	for ((b) = 0, o = NULL; o == NULL && (b) < ARRAY_SIZE(n);\
66 			(b)++)\
67 		hlist_for_each_entry_rcu(o, &n[(b)], m)
68 #define HASH_FOR_EACH_POSSIBLE_RCU(n, o, m, k)		\
69 	hlist_for_each_entry_rcu(o, &n[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(n)))],\
70 		m)
71 #define HASH_FOR_EACH_POSSIBLE(n, o, m, k)		\
72 	hlist_for_each_entry(o, &n[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(n)))],\
73 		m)
74 #define HASH_ADD_RCU(h, n, k) \
75 	hlist_add_head_rcu(n, &h[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(h)))])
76 #define HASH_DEL_RCU(tbl, node) hlist_del_rcu(node)
77 #define HASH_ADD(h, n, k) \
78 	hlist_add_head(n, &h[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(h)))])
79 #define HASH_DEL(tbl, node) hlist_del(node)
80 
81 #define WQ_UNBOUND_MAX_ACTIVE max_t(int, 512, num_possible_cpus() * 4)
82 #define if_addr_rlock(x)
83 #define if_addr_runlock(x)
84 
85 /* constants */
86 #define STATS_TIMER_DELAY 60000
87 
88 /* a couple of linux size defines */
89 #define SZ_128     128
90 #define SZ_2K     SZ_128 * 16
91 #define SZ_1G   (SZ_1K * SZ_1K * SZ_1K)
92 #define SPEED_1000     1000
93 #define SPEED_10000   10000
94 #define SPEED_20000   20000
95 #define SPEED_25000   25000
96 #define SPEED_40000   40000
97 #define SPEED_100000 100000
98 
99 #define irdma_mb()	mb()
100 #define irdma_wmb()	wmb()
101 #define irdma_get_virt_to_phy vtophys
102 
103 #define __aligned_u64 uint64_t __aligned(8)
104 
105 #define VLAN_PRIO_SHIFT 13
106 
107 /*
108  * debug definition section
109  */
110 #define irdma_print(S, ...) printf("%s:%d "S, __FUNCTION__, __LINE__, ##__VA_ARGS__)
111 #define irdma_debug_buf(dev, mask, desc, buf, size)							\
112 do {													\
113 	u32    i;											\
114 	if (!((mask) & (dev)->debug_mask)) {								\
115 		break;											\
116 	}												\
117 	irdma_debug(dev, mask, "%s\n", desc);								\
118 	irdma_debug(dev, mask, "starting address virt=%p phy=%lxh\n", buf, irdma_get_virt_to_phy(buf));	\
119 	for (i = 0; i < size ; i += 8)									\
120 		irdma_debug(dev, mask, "index %03d val: %016lx\n", i, ((unsigned long *)buf)[i / 8]);	\
121 } while(0)
122 
123 #define irdma_debug(h, m, s, ...)					\
124 do {									\
125 	if (!(h)) {							\
126 		if ((m) == IRDMA_DEBUG_INIT)				\
127 			printf("irdma INIT " s, ##__VA_ARGS__);	\
128 	} else if (((m) & (h)->debug_mask)) {				\
129 		printf("irdma " s, ##__VA_ARGS__);			\
130 	} 								\
131 } while (0)
132 #define irdma_dev_err(a, b, ...) printf(b, ##__VA_ARGS__)
133 #define irdma_dev_warn(a, b, ...) printf(b, ##__VA_ARGS__) /*dev_warn(a, b)*/
134 #define irdma_dev_info(a, b, ...) printf(b, ##__VA_ARGS__)
135 #define irdma_pr_warn printf
136 #define ibdev_err(ibdev, fmt, ...)  printf("%s:"fmt, (ibdev)->name, ##__VA_ARGS__)
137 
138 #define dump_struct(s, sz, name)	\
139 do {				\
140 	unsigned char *a;	\
141 	printf("%s %u", (name), (unsigned int)(sz));				\
142 	for (a = (unsigned char*)(s); a < (unsigned char *)(s) + (sz) ; a ++) {	\
143 		if ((u64)a % 8 == 0)		\
144 			printf("\n%p ", a);	\
145 		printf("%2x ", *a);		\
146 	}			\
147 	printf("\n");		\
148 }while(0)
149 
150 /*
151  * debug definition end
152  */
153 
154 typedef __be16 BE16;
155 typedef __be32 BE32;
156 typedef uintptr_t irdma_uintptr;
157 
158 struct irdma_hw;
159 struct irdma_pci_f;
160 struct irdma_sc_dev;
161 struct irdma_sc_qp;
162 struct irdma_sc_vsi;
163 
164 struct irdma_task_arg {
165 	struct irdma_device *iwdev;
166 	struct ice_rdma_peer *peer;
167 	atomic_t open_ongoing;
168 	atomic_t close_ongoing;
169 };
170 
171 struct irdma_dev_ctx {
172 	bus_space_tag_t mem_bus_space_tag;
173 	bus_space_handle_t mem_bus_space_handle;
174 	bus_size_t mem_bus_space_size;
175 	void *dev;
176 	struct irdma_task_arg task_arg;
177 };
178 
179 #define irdma_pr_info(fmt, args ...) printf("%s: WARN "fmt, __func__, ## args)
180 #define irdma_pr_err(fmt, args ...) printf("%s: ERR "fmt, __func__, ## args)
181 #define irdma_memcpy(a, b, c)  memcpy((a), (b), (c))
182 #define irdma_memset(a, b, c)  memset((a), (b), (c))
183 #define irdma_usec_delay(x) DELAY(x)
184 #define mdelay(x) DELAY((x) * 1000)
185 
186 #define rt_tos2priority(tos) (tos >> 5)
187 #define ah_attr_to_dmac(attr) ((attr).dmac)
188 #define kc_ib_modify_qp_is_ok(cur_state, next_state, type, mask, ll) \
189         ib_modify_qp_is_ok(cur_state, next_state, type, mask)
190 #define kc_rdma_gid_attr_network_type(sgid_attr, gid_type, gid) \
191         ib_gid_to_network_type(gid_type, gid)
192 #define irdma_del_timer_compat(tt) del_timer((tt))
193 #define IRDMA_TAILQ_FOREACH CK_STAILQ_FOREACH
194 #define IRDMA_TAILQ_FOREACH_SAFE CK_STAILQ_FOREACH_SAFE
195 #define between(a, b, c) (bool)(c-a >= b-a)
196 
197 #define rd32(a, reg)            irdma_rd32((a)->dev_context, (reg))
198 #define wr32(a, reg, value)     irdma_wr32((a)->dev_context, (reg), (value))
199 
200 #define rd64(a, reg)            irdma_rd64((a)->dev_context, (reg))
201 #define wr64(a, reg, value)     irdma_wr64((a)->dev_context, (reg), (value))
202 #define db_wr32(value, a)	writel((value), (a))
203 
204 void *hw_to_dev(struct irdma_hw *hw);
205 
206 struct irdma_dma_mem {
207 	void  *va;
208 	u64    pa;
209 	bus_dma_tag_t tag;
210 	bus_dmamap_t map;
211 	bus_dma_segment_t seg;
212 	bus_size_t size;
213 	int    nseg;
214 	int    flags;
215 };
216 
217 struct irdma_virt_mem {
218 	void  *va;
219 	u32    size;
220 };
221 
222 struct irdma_dma_info {
223 	dma_addr_t *dmaaddrs;
224 };
225 
226 struct list_head;
227 u32 irdma_rd32(struct irdma_dev_ctx *dev_ctx, u32 reg);
228 void irdma_wr32(struct irdma_dev_ctx *dev_ctx, u32 reg, u32 value);
229 u64 irdma_rd64(struct irdma_dev_ctx *dev_ctx, u32 reg);
230 void irdma_wr64(struct irdma_dev_ctx *dev_ctx, u32 reg, u64 value);
231 
232 void irdma_term_modify_qp(struct irdma_sc_qp *qp, u8 next_state, u8 term, u8 term_len);
233 void irdma_terminate_done(struct irdma_sc_qp *qp, int timeout_occurred);
234 void irdma_terminate_start_timer(struct irdma_sc_qp *qp);
235 void irdma_terminate_del_timer(struct irdma_sc_qp *qp);
236 
237 void irdma_hw_stats_start_timer(struct irdma_sc_vsi *vsi);
238 void irdma_hw_stats_stop_timer(struct irdma_sc_vsi *vsi);
239 void irdma_send_ieq_ack(struct irdma_sc_qp *qp);
240 
241 u8* irdma_get_hw_addr(void *par);
242 
243 void irdma_unmap_vm_page_list(struct irdma_hw *hw, u64 *pg_arr, u32 pg_cnt);
244 int irdma_map_vm_page_list(struct irdma_hw *hw, void *va,
245 			   u64 *pg_arr, u32 pg_cnt);
246 
247 struct ib_device *irdma_get_ibdev(struct irdma_sc_dev *dev);
248 
249 #endif /* _ICRDMA_OSDEP_H_ */
250