xref: /freebsd/sys/dev/irdma/fbsd_kcompat.h (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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 #define tasklet_setup(tasklet, callback)				\
44 	tasklet_init((tasklet), (TASKLET_FUNC_TYPE)(callback),		\
45 		      (TASKLET_DATA_TYPE)(tasklet))
46 
47 #ifndef from_tasklet
48 #define from_tasklet(var, callback_tasklet, tasklet_fieldname) \
49 	container_of(callback_tasklet, typeof(*var), tasklet_fieldname)
50 #endif
51 
52 #define IRDMA_SET_RDMA_OBJ_SIZE(ib_struct, drv_struct, member)    \
53 	(sizeof(struct drv_struct) +                              \
54 	 BUILD_BUG_ON_ZERO(offsetof(struct drv_struct, member)) + \
55 	 BUILD_BUG_ON_ZERO(                                       \
56 		!__same_type(((struct drv_struct *)NULL)->member, \
57                                       struct ib_struct)))
58 #define set_ibdev_dma_device(ibdev, dev) \
59 	ibdev.dma_device = (dev)
60 #define set_max_sge(props, rf)  \
61 	((props)->max_sge = (rf)->sc_dev.hw_attrs.uk_attrs.max_hw_wq_frags)
62 #define kc_set_props_ip_gid_caps(props) \
63 	((props)->port_cap_flags  |= IB_PORT_IP_BASED_GIDS)
64 #define rdma_query_gid(ibdev, port, index, gid) \
65 	ib_get_cached_gid(ibdev, port, index, gid, NULL)
66 #define kmap(pg) page_address(pg)
67 #define kmap_local_page(pg) page_address(pg)
68 #define kunmap(pg)
69 #define kunmap_local(pg)
70 #define kc_free_lsmm_dereg_mr(iwdev, iwqp) \
71 	((iwdev)->ibdev.dereg_mr((iwqp)->lsmm_mr, NULL))
72 
73 #define IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION IB_CQ_FLAGS_TIMESTAMP_COMPLETION
74 #define kc_irdma_destroy_qp(ibqp, udata) irdma_destroy_qp(ibqp, udata)
75 
76 #ifndef IB_QP_ATTR_STANDARD_BITS
77 #define IB_QP_ATTR_STANDARD_BITS GENMASK(20, 0)
78 #endif
79 
80 #define IRDMA_QOS_MODE_VLAN 0x0
81 #define IRDMA_QOS_MODE_DSCP 0x1
82 
83 void kc_set_roce_uverbs_cmd_mask(struct irdma_device *iwdev);
84 void kc_set_rdma_uverbs_cmd_mask(struct irdma_device *iwdev);
85 
86 struct irdma_tunable_info {
87 	struct sysctl_ctx_list irdma_sysctl_ctx;
88 	struct sysctl_oid *irdma_sysctl_tree;
89 	u8 roce_ena;
90 };
91 
92 static inline int irdma_iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
93 				      u16 *pkey)
94 {
95 	*pkey = 0;
96 	return 0;
97 }
98 
99 static inline int cq_validate_flags(u32 flags, u8 hw_rev)
100 {
101 	/* GEN1 does not support CQ create flags */
102 	if (hw_rev == IRDMA_GEN_1)
103 		return flags ? -EOPNOTSUPP : 0;
104 
105 	return flags & ~IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION ? -EOPNOTSUPP : 0;
106 }
107 static inline u64 *irdma_next_pbl_addr(u64 *pbl, struct irdma_pble_info **pinfo,
108 				       u32 *idx)
109 {
110 	*idx += 1;
111 	if (!(*pinfo) || *idx != (*pinfo)->cnt)
112 		return ++pbl;
113 	*idx = 0;
114 	(*pinfo)++;
115 
116 	return (*pinfo)->addr;
117 }
118 int irdma_create_cq(struct ib_cq *ibcq,
119 		    const struct ib_cq_init_attr *attr,
120 		    struct ib_udata *udata);
121 struct ib_qp *irdma_create_qp(struct ib_pd *ibpd,
122 			      struct ib_qp_init_attr *init_attr,
123 			      struct ib_udata *udata);
124 int irdma_create_ah(struct ib_ah *ib_ah,
125 		    struct ib_ah_attr *attr, u32 flags,
126 		    struct ib_udata *udata);
127 int irdma_create_ah_stub(struct ib_ah *ib_ah,
128 			 struct ib_ah_attr *attr, u32 flags,
129 			 struct ib_udata *udata);
130 void irdma_ether_copy(u8 *dmac, struct ib_ah_attr *attr);
131 
132 void irdma_destroy_ah(struct ib_ah *ibah, u32 flags);
133 void irdma_destroy_ah_stub(struct ib_ah *ibah, u32 flags);
134 int irdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata);
135 int irdma_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata);
136 void irdma_get_eth_speed_and_width(u32 link_speed, u8 *active_speed,
137 				   u8 *active_width);
138 enum rdma_link_layer irdma_get_link_layer(struct ib_device *ibdev,
139 					  u8 port_num);
140 int irdma_roce_port_immutable(struct ib_device *ibdev, u8 port_num,
141 			      struct ib_port_immutable *immutable);
142 int irdma_iw_port_immutable(struct ib_device *ibdev, u8 port_num,
143 			    struct ib_port_immutable *immutable);
144 int irdma_query_gid(struct ib_device *ibdev, u8 port, int index,
145 		    union ib_gid *gid);
146 int irdma_query_gid_roce(struct ib_device *ibdev, u8 port, int index,
147 			 union ib_gid *gid);
148 int irdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
149 		     u16 *pkey);
150 int irdma_query_port(struct ib_device *ibdev, u8 port,
151 		     struct ib_port_attr *props);
152 struct rdma_hw_stats *irdma_alloc_hw_stats(struct ib_device *ibdev, u8 port_num);
153 int irdma_get_hw_stats(struct ib_device *ibdev,
154 		       struct rdma_hw_stats *stats, u8 port_num,
155 		       int index);
156 
157 int irdma_register_qset(struct irdma_sc_vsi *vsi,
158 			struct irdma_ws_node *tc_node);
159 void irdma_unregister_qset(struct irdma_sc_vsi *vsi,
160 			   struct irdma_ws_node *tc_node);
161 void ib_unregister_device(struct ib_device *ibdev);
162 void irdma_disassociate_ucontext(struct ib_ucontext *context);
163 int kc_irdma_set_roce_cm_info(struct irdma_qp *iwqp,
164 			      struct ib_qp_attr *attr,
165 			      u16 *vlan_id);
166 struct irdma_device *kc_irdma_get_device(struct ifnet *netdev);
167 void kc_irdma_put_device(struct irdma_device *iwdev);
168 
169 void kc_set_loc_seq_num_mss(struct irdma_cm_node *cm_node);
170 
171 void irdma_get_dev_fw_str(struct ib_device *dev, char *str, size_t str_len);
172 
173 int irdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
174 		      struct ib_port_modify *props);
175 int irdma_get_dst_mac(struct irdma_cm_node *cm_node, struct sockaddr *dst_sin,
176 		      u8 *dst_mac);
177 int irdma_resolve_neigh_lpb_chk(struct irdma_device *iwdev, struct irdma_cm_node *cm_node,
178 				struct irdma_cm_info *cm_info);
179 int irdma_addr_resolve_neigh(struct irdma_cm_node *cm_node, u32 dst_ip,
180 			     int arpindex);
181 int irdma_addr_resolve_neigh_ipv6(struct irdma_cm_node *cm_node, u32 *dest,
182 				  int arpindex);
183 void irdma_dcqcn_tunables_init(struct irdma_pci_f *rf);
184 u32 irdma_create_stag(struct irdma_device *iwdev);
185 void irdma_free_stag(struct irdma_device *iwdev, u32 stag);
186 
187 struct irdma_mr;
188 struct irdma_cq;
189 struct irdma_cq_buf;
190 struct ib_mr *irdma_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
191 			     u32 max_num_sg, struct ib_udata *udata);
192 int irdma_hw_alloc_mw(struct irdma_device *iwdev, struct irdma_mr *iwmr);
193 struct ib_mw *irdma_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
194 			     struct ib_udata *udata);
195 int irdma_hw_alloc_stag(struct irdma_device *iwdev, struct irdma_mr *iwmr);
196 void irdma_cq_free_rsrc(struct irdma_pci_f *rf, struct irdma_cq *iwcq);
197 int irdma_validate_qp_attrs(struct ib_qp_init_attr *init_attr,
198 			    struct irdma_device *iwdev);
199 void irdma_setup_virt_qp(struct irdma_device *iwdev,
200                          struct irdma_qp *iwqp,
201                          struct irdma_qp_init_info *init_info);
202 int irdma_setup_kmode_qp(struct irdma_device *iwdev,
203 			 struct irdma_qp *iwqp,
204 			 struct irdma_qp_init_info *info,
205 			 struct ib_qp_init_attr *init_attr);
206 void irdma_roce_fill_and_set_qpctx_info(struct irdma_qp *iwqp,
207 					struct irdma_qp_host_ctx_info *ctx_info);
208 void irdma_iw_fill_and_set_qpctx_info(struct irdma_qp *iwqp,
209 				      struct irdma_qp_host_ctx_info *ctx_info);
210 int irdma_cqp_create_qp_cmd(struct irdma_qp *iwqp);
211 void irdma_dealloc_push_page(struct irdma_pci_f *rf,
212 			     struct irdma_sc_qp *qp);
213 int irdma_process_resize_list(struct irdma_cq *iwcq, struct irdma_device *iwdev,
214 			      struct irdma_cq_buf *lcqe_buf);
215 void irdma_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata);
216 int irdma_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata);
217 void irdma_dealloc_ucontext(struct ib_ucontext *context);
218 int irdma_alloc_pd(struct ib_pd *pd, struct ib_udata *udata);
219 void irdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata);
220 int irdma_add_gid(struct ib_device *, u8, unsigned int, const union ib_gid *,
221 		  const struct ib_gid_attr *, void **);
222 int irdma_del_gid(struct ib_device *, u8, unsigned int, void **);
223 struct ib_device *ib_device_get_by_netdev(struct ifnet *ndev, int driver_id);
224 void ib_device_put(struct ib_device *device);
225 void ib_unregister_device_put(struct ib_device *device);
226 enum ib_mtu ib_mtu_int_to_enum(int mtu);
227 struct irdma_pbl *irdma_get_pbl(unsigned long va, struct list_head *pbl_list);
228 void irdma_clean_cqes(struct irdma_qp *iwqp, struct irdma_cq *iwcq);
229 void irdma_remove_push_mmap_entries(struct irdma_qp *iwqp);
230 
231 struct irdma_ucontext;
232 void irdma_del_memlist(struct irdma_mr *iwmr, struct irdma_ucontext *ucontext);
233 void irdma_copy_user_pgaddrs(struct irdma_mr *iwmr, u64 *pbl,
234 			     enum irdma_pble_level level);
235 void irdma_reg_ipaddr_event_cb(struct irdma_pci_f *rf);
236 void irdma_dereg_ipaddr_event_cb(struct irdma_pci_f *rf);
237 
238 /* Introduced in this series https://lore.kernel.org/linux-rdma/0-v2-270386b7e60b+28f4-umem_1_jgg@nvidia.com/
239  * An irdma version helper doing same for older functions with difference that iova is passed in
240  * as opposed to derived from umem->iova.
241  */
242 static inline size_t irdma_ib_umem_num_dma_blocks(struct ib_umem *umem, unsigned long pgsz, u64 iova)
243 {
244 	/* some older OFED distros do not have ALIGN_DOWN */
245 #ifndef ALIGN_DOWN
246 #define ALIGN_DOWN(x, a)	ALIGN((x) - ((a) - 1), (a))
247 #endif
248 
249 	return (size_t)((ALIGN(iova + umem->length, pgsz) -
250 			 ALIGN_DOWN(iova, pgsz))) / pgsz;
251 }
252 
253 #endif /* FBSD_KCOMPAT_H */
254