xref: /freebsd/contrib/ofed/libirdma/osdep.h (revision 61e21613)
1 /*-
2  * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3  *
4  * Copyright (c) 2021 - 2023 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 <stdatomic.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <infiniband/types.h>
43 #include <infiniband/verbs.h>
44 #include <infiniband/udma_barrier.h>
45 #include <sys/bus.h>
46 #include <sys/bus_dma.h>
47 #include <sys/endian.h>
48 
49 #define IOMEM
50 #define IRDMA_NTOHL(a) ntohl(a)
51 #define IRDMA_NTOHS(a) ntohs(a)
52 #define MAKEMASK(m, s) ((m) << (s))
53 #define OS_TIMER timer_list
54 #define OS_LIST_HEAD list_head
55 #define OS_LIST_ENTRY list_head
56 #define DECLARE_HASHTABLE(n, b) struct hlist_head (n)[1 << (b)]
57 #define HASH_MIN(v, b) (sizeof(v) <= 4 ? hash_32(v, b) : hash_long(v, b))
58 #define HASH_FOR_EACH_RCU(n, b, o, m) 	for ((b) = 0, o = NULL; o == NULL && (b) < ARRAY_SIZE(n);\
59 			(b)++)\
60 		hlist_for_each_entry_rcu(o, &n[(b)], m)
61 #define HASH_FOR_EACH_POSSIBLE_RCU(n, o, m, k)		\
62 	hlist_for_each_entry_rcu(o, &n[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(n)))],\
63 		m)
64 #define HASH_FOR_EACH_POSSIBLE(n, o, m, k)		\
65 	hlist_for_each_entry(o, &n[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(n)))],\
66 		m)
67 #define HASH_ADD_RCU(h, n, k) \
68 	hlist_add_head_rcu(n, &h[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(h)))])
69 #define HASH_DEL_RCU(tbl, node) hlist_del_rcu(node)
70 #define HASH_ADD(h, n, k) \
71 	hlist_add_head(n, &h[jhash(&k, sizeof(k), 0) >> (32 - ilog2(ARRAY_SIZE(h)))])
72 #define HASH_DEL(tbl, node) hlist_del(node)
73 
74 #define WQ_UNBOUND_MAX_ACTIVE max_t(int, 512, num_possible_cpus() * 4)
75 #define if_addr_rlock(x)
76 #define if_addr_runlock(x)
77 
78 /* constants */
79 #define STATS_TIMER_DELAY 60000
80 
81 /* a couple of linux size defines */
82 #define BIT_ULL(a) (1ULL << (a))
83 #define min(a, b) ((a) > (b) ? (b) : (a))
84 #ifndef likely
85 #define likely(x)  __builtin_expect((x), 1)
86 #endif
87 #ifndef unlikely
88 #define unlikely(x)  __builtin_expect((x), 0)
89 #endif
90 
91 #define __aligned_u64 uint64_t __aligned(8)
92 
93 #define VLAN_PRIO_SHIFT 13
94 #if __FreeBSD_version < 1400000
95 #define IB_USER_VERBS_EX_CMD_MODIFY_QP IB_USER_VERBS_CMD_MODIFY_QP
96 #endif
97 
98 /*
99  * debug definition section
100  */
101 #define irdma_print(S, ...) printf("%s:%d "S, __FUNCTION__, __LINE__, ##__VA_ARGS__)
102 #define irdma_debug_buf(dev, mask, desc, buf, size)							\
103 do {													\
104 	u32 i;												\
105 	if (!((mask) & (dev)->debug_mask)) {								\
106 		break;											\
107 	}												\
108 	irdma_debug(dev, mask, "%s\n", desc);								\
109 	irdma_debug(dev, mask, "starting address virt=%p phy=%lxh\n", buf, irdma_get_virt_to_phy(buf));	\
110 	for (i = 0; i < size ; i += 8)									\
111 		irdma_debug(dev, mask, "index %03d val: %016lx\n", i, ((unsigned long *)(buf))[i / 8]);	\
112 } while(0)
113 
114 #define irdma_debug(h, m, s, ...)				\
115 do {								\
116 	if (!(h)) {						\
117 		if ((m) == IRDMA_DEBUG_INIT)			\
118 			printf("irdma INIT " s, ##__VA_ARGS__);	\
119 	} else if (((m) & (h)->debug_mask)) {			\
120 		printf("irdma " s, ##__VA_ARGS__);		\
121 	} 							\
122 } while (0)
123 extern unsigned int irdma_dbg;
124 #define libirdma_debug(fmt, args...)				\
125 do {								\
126 	if (irdma_dbg)						\
127 		printf("libirdma-%s: " fmt, __func__, ##args);	\
128 } while (0)
129 #define irdma_dev_err(ibdev, fmt, ...) \
130 	pr_err("%s:%s:%d ERR "fmt, (ibdev)->name, __func__, __LINE__, ##__VA_ARGS__)
131 #define irdma_dev_warn(ibdev, fmt, ...) \
132 	pr_warn("%s:%s:%d WARN "fmt, (ibdev)->name, __func__, __LINE__, ##__VA_ARGS__)
133 #define irdma_dev_info(a, b, ...) printf(b, ##__VA_ARGS__)
134 #define irdma_pr_warn printf
135 
136 /*
137  * debug definition end
138  */
139 
140 typedef __be16 BE16;
141 typedef __be32 BE32;
142 typedef uintptr_t irdma_uintptr;
143 
144 struct irdma_hw;
145 struct irdma_pci_f;
146 struct irdma_sc_dev;
147 struct irdma_sc_qp;
148 struct irdma_sc_vsi;
149 
150 #define irdma_pr_info(fmt, args ...) printf("%s: WARN "fmt, __func__, ## args)
151 #define irdma_pr_err(fmt, args ...) printf("%s: ERR "fmt, __func__, ## args)
152 #define irdma_memcpy(a, b, c)  memcpy((a), (b), (c))
153 #define irdma_memset(a, b, c)  memset((a), (b), (c))
154 #define irdma_usec_delay(x) DELAY(x)
155 #define mdelay(x) DELAY((x) * 1000)
156 
157 #define rt_tos2priority(tos) (tos >> 5)
158 #define ah_attr_to_dmac(attr) ((attr).dmac)
159 #define irdma_del_timer_compat(tt) del_timer((tt))
160 #define IRDMA_TAILQ_FOREACH CK_STAILQ_FOREACH
161 #define IRDMA_TAILQ_FOREACH_SAFE CK_STAILQ_FOREACH_SAFE
162 #define between(a, b, c) (bool)(c-a >= b-a)
163 
164 static inline void db_wr32(__u32 val, __u32 *wqe_word)
165 {
166 	*wqe_word = val;
167 }
168 
169 void *hw_to_dev(struct irdma_hw *hw);
170 
171 struct irdma_dma_mem {
172 	void *va;
173 	u64 pa;
174 	bus_dma_tag_t tag;
175 	bus_dmamap_t map;
176 	bus_dma_segment_t seg;
177 	bus_size_t size;
178 	int nseg;
179 	int flags;
180 };
181 
182 struct irdma_virt_mem {
183 	void *va;
184 	u32 size;
185 };
186 
187 #ifndef verbs_mr
188 enum ibv_mr_type {
189 	IBV_MR_TYPE_MR,
190 	IBV_MR_TYPE_NULL_MR,
191 };
192 
193 struct verbs_mr {
194 	struct ibv_mr		ibv_mr;
195 	enum ibv_mr_type	mr_type;
196 	int 			access;
197 };
198 #define verbs_get_mr(mr) container_of((mr), struct verbs_mr, ibv_mr)
199 #endif
200 #endif /* _ICRDMA_OSDEP_H_ */
201