xref: /freebsd/sys/dev/irdma/fbsd_kcompat.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 FBSD_KCOMPAT_H
37 #define FBSD_KCOMPAT_H
38 #include "ice_rdma.h"
39 
40 #define TASKLET_DATA_TYPE	unsigned long
41 #define TASKLET_FUNC_TYPE	void (*)(TASKLET_DATA_TYPE)
42 
43 #ifndef tasklet_setup
44 #define tasklet_setup(tasklet, callback)				\
45 	tasklet_init((tasklet), (TASKLET_FUNC_TYPE)(callback),		\
46 		      (TASKLET_DATA_TYPE)(tasklet))
47 #endif
48 #ifndef from_tasklet
49 #define from_tasklet(var, callback_tasklet, tasklet_fieldname) \
50 	container_of(callback_tasklet, typeof(*var), tasklet_fieldname)
51 #endif
52 
53 #if __FreeBSD_version >= 1400000
54 #define IRDMA_SET_RDMA_OBJ_SIZE(ib_struct, drv_struct, member)    \
55 	(sizeof(struct drv_struct) +                              \
56 	 BUILD_BUG_ON_ZERO(offsetof(struct drv_struct, member)) + \
57 	 BUILD_BUG_ON_ZERO(                                       \
58 		!__same_type(((struct drv_struct *)NULL)->member, \
59                                       struct ib_struct)))
60 #endif /* __FreeBSD_version > 1400000 */
61 
62 #define set_ibdev_dma_device(ibdev, dev) \
63 	ibdev.dma_device = (dev)
64 #define set_max_sge(props, rf)  \
65 	((props)->max_sge = (rf)->sc_dev.hw_attrs.uk_attrs.max_hw_wq_frags)
66 #define kc_set_props_ip_gid_caps(props) \
67 	((props)->port_cap_flags  |= IB_PORT_IP_BASED_GIDS)
68 #define rdma_query_gid(ibdev, port, index, gid) \
69 	ib_get_cached_gid(ibdev, port, index, gid, NULL)
70 #define kmap(pg) page_address(pg)
71 #define kmap_local_page(pg) page_address(pg)
72 #define kunmap(pg)
73 #define kunmap_local(pg)
74 #if __FreeBSD_version >= 1400026
75 #define kc_free_lsmm_dereg_mr(iwdev, iwqp) \
76 	((iwdev)->ibdev.dereg_mr((iwqp)->lsmm_mr, NULL))
77 #else
78 #define kc_free_lsmm_dereg_mr(iwdev, iwqp) \
79 	((iwdev)->ibdev.dereg_mr((iwqp)->lsmm_mr))
80 #endif
81 
82 #define IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION IB_CQ_FLAGS_TIMESTAMP_COMPLETION
83 #if __FreeBSD_version < 1400026
84 #define kc_irdma_destroy_qp(ibqp, udata) irdma_destroy_qp(ibqp)
85 #else
86 #define kc_irdma_destroy_qp(ibqp, udata) irdma_destroy_qp(ibqp, udata)
87 #endif
88 #ifndef IB_QP_ATTR_STANDARD_BITS
89 #define IB_QP_ATTR_STANDARD_BITS GENMASK(20, 0)
90 #endif
91 
92 #define IRDMA_QOS_MODE_VLAN 0x0
93 #define IRDMA_QOS_MODE_DSCP 0x1
94 
95 #define IRDMA_VER_LEN 24
96 
97 void kc_set_roce_uverbs_cmd_mask(struct irdma_device *iwdev);
98 void kc_set_rdma_uverbs_cmd_mask(struct irdma_device *iwdev);
99 
100 struct irdma_tunable_info {
101 	struct sysctl_ctx_list irdma_sysctl_ctx;
102 	struct sysctl_oid *irdma_sysctl_tree;
103 	char drv_ver[IRDMA_VER_LEN];
104 	u8 roce_ena;
105 };
106 
107 static inline int irdma_iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
108 				      u16 *pkey)
109 {
110 	*pkey = 0;
111 	return 0;
112 }
113 
114 static inline int cq_validate_flags(u32 flags, u8 hw_rev)
115 {
116 	/* GEN1 does not support CQ create flags */
117 	if (hw_rev == IRDMA_GEN_1)
118 		return flags ? -EOPNOTSUPP : 0;
119 
120 	return flags & ~IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION ? -EOPNOTSUPP : 0;
121 }
122 static inline u64 *irdma_next_pbl_addr(u64 *pbl, struct irdma_pble_info **pinfo,
123 				       u32 *idx)
124 {
125 	*idx += 1;
126 	if (!(*pinfo) || *idx != (*pinfo)->cnt)
127 		return ++pbl;
128 	*idx = 0;
129 	(*pinfo)++;
130 
131 	return (*pinfo)->addr;
132 }
133 #if __FreeBSD_version < 1400026
134 struct ib_cq *irdma_create_cq(struct ib_device *ibdev,
135 			      const struct ib_cq_init_attr *attr,
136 			      struct ib_ucontext *context,
137 			      struct ib_udata *udata);
138 #else
139 int irdma_create_cq(struct ib_cq *ibcq,
140 		    const struct ib_cq_init_attr *attr,
141 		    struct ib_udata *udata);
142 #endif
143 struct ib_qp *irdma_create_qp(struct ib_pd *ibpd,
144 			      struct ib_qp_init_attr *init_attr,
145 			      struct ib_udata *udata);
146 #if __FreeBSD_version >= 1400026
147 int irdma_create_ah(struct ib_ah *ib_ah,
148 		    struct ib_ah_attr *attr, u32 flags,
149 		    struct ib_udata *udata);
150 int irdma_create_ah_stub(struct ib_ah *ib_ah,
151 			 struct ib_ah_attr *attr, u32 flags,
152 			 struct ib_udata *udata);
153 #else
154 struct ib_ah *irdma_create_ah(struct ib_pd *ibpd,
155 			      struct ib_ah_attr *attr,
156 			      struct ib_udata *udata);
157 struct ib_ah *irdma_create_ah_stub(struct ib_pd *ibpd,
158 				   struct ib_ah_attr *attr,
159 				   struct ib_udata *udata);
160 #endif
161 void irdma_ether_copy(u8 *dmac, struct ib_ah_attr *attr);
162 
163 #if __FreeBSD_version >= 1400026
164 void irdma_destroy_ah(struct ib_ah *ibah, u32 flags);
165 void irdma_destroy_ah_stub(struct ib_ah *ibah, u32 flags);
166 #else
167 int irdma_destroy_ah(struct ib_ah *ibah);
168 int irdma_destroy_ah_stub(struct ib_ah *ibah);
169 #endif
170 #if __FreeBSD_version < 1400026
171 int irdma_destroy_qp(struct ib_qp *ibqp);
172 #else
173 int irdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata);
174 #endif
175 #if __FreeBSD_version < 1400026
176 int irdma_dereg_mr(struct ib_mr *ib_mr);
177 #else
178 int irdma_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata);
179 #endif
180 int ib_get_eth_speed(struct ib_device *dev, u32 port_num, u8 *speed, u8 *width);
181 enum rdma_link_layer irdma_get_link_layer(struct ib_device *ibdev,
182 					  u8 port_num);
183 int irdma_roce_port_immutable(struct ib_device *ibdev, u8 port_num,
184 			      struct ib_port_immutable *immutable);
185 int irdma_iw_port_immutable(struct ib_device *ibdev, u8 port_num,
186 			    struct ib_port_immutable *immutable);
187 int irdma_query_gid(struct ib_device *ibdev, u8 port, int index,
188 		    union ib_gid *gid);
189 int irdma_query_gid_roce(struct ib_device *ibdev, u8 port, int index,
190 			 union ib_gid *gid);
191 int irdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
192 		     u16 *pkey);
193 int irdma_query_port(struct ib_device *ibdev, u8 port,
194 		     struct ib_port_attr *props);
195 struct rdma_hw_stats *irdma_alloc_hw_stats(struct ib_device *ibdev, u8 port_num);
196 int irdma_get_hw_stats(struct ib_device *ibdev,
197 		       struct rdma_hw_stats *stats, u8 port_num,
198 		       int index);
199 
200 void irdma_request_reset(struct irdma_pci_f *rf);
201 int irdma_register_qset(struct irdma_sc_vsi *vsi,
202 			struct irdma_ws_node *tc_node);
203 void irdma_unregister_qset(struct irdma_sc_vsi *vsi,
204 			   struct irdma_ws_node *tc_node);
205 void ib_unregister_device(struct ib_device *ibdev);
206 #if __FreeBSD_version < 1400026
207 int rdma_user_mmap_io(struct ib_ucontext *ucontext, struct vm_area_struct *vma,
208 		      unsigned long pfn, unsigned long size, pgprot_t prot);
209 #endif
210 void irdma_disassociate_ucontext(struct ib_ucontext *context);
211 int kc_irdma_set_roce_cm_info(struct irdma_qp *iwqp,
212 			      struct ib_qp_attr *attr,
213 			      u16 *vlan_id);
214 struct irdma_device *kc_irdma_get_device(struct ifnet *netdev);
215 void kc_irdma_put_device(struct irdma_device *iwdev);
216 
217 void kc_set_loc_seq_num_mss(struct irdma_cm_node *cm_node);
218 u16 kc_rdma_get_udp_sport(u32 fl, u32 lqpn, u32 rqpn);
219 
220 void irdma_get_dev_fw_str(struct ib_device *dev, char *str, size_t str_len);
221 
222 int irdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
223 		      struct ib_port_modify *props);
224 int irdma_get_dst_mac(struct irdma_cm_node *cm_node, struct sockaddr *dst_sin,
225 		      u8 *dst_mac);
226 int irdma_resolve_neigh_lpb_chk(struct irdma_device *iwdev, struct irdma_cm_node *cm_node,
227 				struct irdma_cm_info *cm_info);
228 int irdma_addr_resolve_neigh(struct irdma_cm_node *cm_node, u32 dst_ip,
229 			     int arpindex);
230 int irdma_addr_resolve_neigh_ipv6(struct irdma_cm_node *cm_node, u32 *dest,
231 				  int arpindex);
232 void irdma_dcqcn_tunables_init(struct irdma_pci_f *rf);
233 u32 irdma_create_stag(struct irdma_device *iwdev);
234 void irdma_free_stag(struct irdma_device *iwdev, u32 stag);
235 
236 int irdma_hwdereg_mr(struct ib_mr *ib_mr);
237 int irdma_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start, u64 len,
238 			u64 virt, int new_access, struct ib_pd *new_pd,
239 			struct ib_udata *udata);
240 struct irdma_mr;
241 struct irdma_cq;
242 struct irdma_cq_buf;
243 #if __FreeBSD_version < 1400026
244 struct ib_mr *irdma_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
245 			     u32 max_num_sg);
246 #else
247 struct ib_mr *irdma_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
248 			     u32 max_num_sg, struct ib_udata *udata);
249 #endif
250 int irdma_hwreg_mr(struct irdma_device *iwdev, struct irdma_mr *iwmr,
251 		   u16 access);
252 struct ib_mr *irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len,
253 				   u64 virt, struct ib_udata *udata);
254 int irdma_hw_alloc_mw(struct irdma_device *iwdev, struct irdma_mr *iwmr);
255 struct ib_mw *irdma_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
256 			     struct ib_udata *udata);
257 int irdma_hw_alloc_stag(struct irdma_device *iwdev, struct irdma_mr *iwmr);
258 void irdma_cq_free_rsrc(struct irdma_pci_f *rf, struct irdma_cq *iwcq);
259 int irdma_validate_qp_attrs(struct ib_qp_init_attr *init_attr,
260 			    struct irdma_device *iwdev);
261 void irdma_setup_virt_qp(struct irdma_device *iwdev,
262                          struct irdma_qp *iwqp,
263                          struct irdma_qp_init_info *init_info);
264 int irdma_setup_kmode_qp(struct irdma_device *iwdev,
265 			 struct irdma_qp *iwqp,
266 			 struct irdma_qp_init_info *info,
267 			 struct ib_qp_init_attr *init_attr);
268 int irdma_setup_umode_qp(struct ib_udata *udata,
269 			 struct irdma_device *iwdev,
270 			 struct irdma_qp *iwqp,
271 			 struct irdma_qp_init_info *info,
272 			 struct ib_qp_init_attr *init_attr);
273 void irdma_roce_fill_and_set_qpctx_info(struct irdma_qp *iwqp,
274 					struct irdma_qp_host_ctx_info *ctx_info);
275 void irdma_iw_fill_and_set_qpctx_info(struct irdma_qp *iwqp,
276 				      struct irdma_qp_host_ctx_info *ctx_info);
277 int irdma_cqp_create_qp_cmd(struct irdma_qp *iwqp);
278 void irdma_dealloc_push_page(struct irdma_pci_f *rf,
279 			     struct irdma_sc_qp *qp);
280 int irdma_process_resize_list(struct irdma_cq *iwcq, struct irdma_device *iwdev,
281 			      struct irdma_cq_buf *lcqe_buf);
282 #if __FreeBSD_version < 1400026
283 int irdma_destroy_cq(struct ib_cq *ib_cq);
284 #else
285 void irdma_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata);
286 #endif
287 #if __FreeBSD_version < 1400026
288 struct ib_ucontext *irdma_alloc_ucontext(struct ib_device *, struct ib_udata *);
289 #else
290 int irdma_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata);
291 #endif
292 #if __FreeBSD_version < 1400026
293 int irdma_dealloc_ucontext(struct ib_ucontext *);
294 #else
295 void irdma_dealloc_ucontext(struct ib_ucontext *context);
296 #endif
297 #if __FreeBSD_version < 1400026
298 struct ib_pd *irdma_alloc_pd(struct ib_device *, struct ib_ucontext *,
299 			     struct ib_udata *);
300 #else
301 int irdma_alloc_pd(struct ib_pd *pd, struct ib_udata *udata);
302 #endif
303 #if __FreeBSD_version < 1400026
304 int irdma_dealloc_pd(struct ib_pd *);
305 #else
306 void irdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata);
307 #endif
308 int irdma_add_gid(struct ib_device *, u8, unsigned int, const union ib_gid *,
309 		  const struct ib_gid_attr *, void **);
310 int irdma_del_gid(struct ib_device *, u8, unsigned int, void **);
311 struct ib_device *ib_device_get_by_netdev(struct ifnet *ndev, int driver_id);
312 void ib_device_put(struct ib_device *device);
313 void ib_unregister_device_put(struct ib_device *device);
314 enum ib_mtu ib_mtu_int_to_enum(int mtu);
315 struct irdma_pbl *irdma_get_pbl(unsigned long va, struct list_head *pbl_list);
316 void irdma_clean_cqes(struct irdma_qp *iwqp, struct irdma_cq *iwcq);
317 void irdma_remove_push_mmap_entries(struct irdma_qp *iwqp);
318 
319 struct irdma_ucontext;
320 void irdma_del_memlist(struct irdma_mr *iwmr, struct irdma_ucontext *ucontext);
321 void irdma_copy_user_pgaddrs(struct irdma_mr *iwmr, u64 *pbl,
322 			     enum irdma_pble_level level);
323 void irdma_reg_ipaddr_event_cb(struct irdma_pci_f *rf);
324 void irdma_dereg_ipaddr_event_cb(struct irdma_pci_f *rf);
325 
326 /* Introduced in this series https://lore.kernel.org/linux-rdma/0-v2-270386b7e60b+28f4-umem_1_jgg@nvidia.com/
327  * An irdma version helper doing same for older functions with difference that iova is passed in
328  * as opposed to derived from umem->iova.
329  */
330 static inline size_t irdma_ib_umem_num_dma_blocks(struct ib_umem *umem, unsigned long pgsz, u64 iova)
331 {
332 	/* some older OFED distros do not have ALIGN_DOWN */
333 #ifndef ALIGN_DOWN
334 #define ALIGN_DOWN(x, a)	ALIGN((x) - ((a) - 1), (a))
335 #endif
336 
337 	return (size_t)((ALIGN(iova + umem->length, pgsz) -
338 			 ALIGN_DOWN(iova, pgsz))) / pgsz;
339 }
340 
341 static inline void addrconf_addr_eui48(u8 *deui, const char *const addr)
342 {
343 	memcpy(deui, addr, 3);
344 	deui[3] = 0xFF;
345 	deui[4] = 0xFE;
346 	memcpy(deui + 5, addr + 3, 3);
347 	deui[0] ^= 2;
348 }
349 
350 #endif /* FBSD_KCOMPAT_H */
351