1 /*-
2 * SPDX-License-Identifier: GPL-2.0 or Linux-OpenIB
3 *
4 * Copyright (c) 2021 - 2026 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
35 #ifndef FBSD_KCOMPAT_H
36 #define FBSD_KCOMPAT_H
37 #include "ice_rdma.h"
38 #include "irdma-abi.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 #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
59 #define set_ibdev_dma_device(ibdev, dev) \
60 ibdev.dma_device = (dev)
61 #define set_max_sge(props, rf) \
62 ((props)->max_sge = (rf)->sc_dev.hw_attrs.uk_attrs.max_hw_wq_frags)
63 #define kmap(pg) page_address(pg)
64 #define kmap_local_page(pg) page_address(pg)
65 #define kunmap(pg)
66 #define kunmap_local(pg)
67
68 #define IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION IB_CQ_FLAGS_TIMESTAMP_COMPLETION
69 #ifndef IB_QP_ATTR_STANDARD_BITS
70 #define IB_QP_ATTR_STANDARD_BITS GENMASK(20, 0)
71 #endif
72
73 #define IRDMA_QOS_MODE_VLAN 0x0
74 #define IRDMA_QOS_MODE_DSCP 0x1
75
76 #define IRDMA_VER_LEN 24
77
78 void kc_set_roce_uverbs_cmd_mask(struct irdma_device *iwdev);
79 void kc_set_rdma_uverbs_cmd_mask(struct irdma_device *iwdev);
80
81 extern u8 irdma_sysctl_max_ord;
82 extern u8 irdma_sysctl_max_ird;
83
84 struct irdma_tunable_info {
85 struct sysctl_ctx_list irdma_sysctl_ctx;
86 struct sysctl_oid *irdma_sysctl_tree;
87 struct sysctl_oid *qos_sysctl_tree;
88 struct sysctl_oid *sws_sysctl_tree;
89 char drv_ver[IRDMA_VER_LEN];
90 u8 roce_ena;
91 };
92
irdma_iw_query_pkey(struct ib_device * ibdev,u8 port,u16 index,u16 * pkey)93 static inline int irdma_iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
94 u16 *pkey)
95 {
96 *pkey = 0;
97 return 0;
98 }
99
cq_validate_flags(u32 flags,u8 hw_rev)100 static inline int cq_validate_flags(u32 flags, u8 hw_rev)
101 {
102 /* GEN1 does not support CQ create flags */
103 if (hw_rev == IRDMA_GEN_1)
104 return flags ? -EOPNOTSUPP : 0;
105
106 return flags & ~IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION ? -EOPNOTSUPP : 0;
107 }
irdma_next_pbl_addr(u64 * pbl,struct irdma_pble_info ** pinfo,u32 * idx)108 static inline u64 *irdma_next_pbl_addr(u64 *pbl, struct irdma_pble_info **pinfo,
109 u32 *idx)
110 {
111 *idx += 1;
112 if (!(*pinfo) || *idx != (*pinfo)->cnt)
113 return ++pbl;
114 *idx = 0;
115 (*pinfo)++;
116
117 return (*pinfo)->addr;
118 }
119
120 static inline struct vnet *
irdma_cmid_to_vnet(struct iw_cm_id * cm_id)121 irdma_cmid_to_vnet(struct iw_cm_id *cm_id)
122 {
123 struct rdma_cm_id *rdma_id;
124
125 if (!cm_id)
126 return &init_net;
127
128 rdma_id = (struct rdma_cm_id *)cm_id->context;
129
130 return rdma_id->route.addr.dev_addr.net;
131 }
132
133 int irdma_create_cq(struct ib_cq *ibcq,
134 const struct ib_cq_init_attr *attr,
135 struct ib_udata *udata);
136 struct ib_qp *irdma_create_qp(struct ib_pd *ibpd,
137 struct ib_qp_init_attr *init_attr,
138 struct ib_udata *udata);
139 int irdma_create_ah(struct ib_ah *ib_ah,
140 struct ib_ah_attr *attr, u32 flags,
141 struct ib_udata *udata);
142 int irdma_create_ah_stub(struct ib_ah *ib_ah,
143 struct ib_ah_attr *attr, u32 flags,
144 struct ib_udata *udata);
145 void irdma_ether_copy(u8 *dmac, struct ib_ah_attr *attr);
146 void irdma_destroy_ah(struct ib_ah *ibah, u32 flags);
147 void irdma_destroy_ah_stub(struct ib_ah *ibah, u32 flags);
148 int irdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata);
149 int irdma_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata);
150 int ib_get_eth_speed(struct ib_device *dev, u32 port_num, u16 *speed, u8 *width);
151 enum rdma_link_layer irdma_get_link_layer(struct ib_device *ibdev,
152 u8 port_num);
153 int irdma_roce_port_immutable(struct ib_device *ibdev, u8 port_num,
154 struct ib_port_immutable *immutable);
155 int irdma_iw_port_immutable(struct ib_device *ibdev, u8 port_num,
156 struct ib_port_immutable *immutable);
157 int irdma_query_gid(struct ib_device *ibdev, u8 port, int index,
158 union ib_gid *gid);
159 int irdma_query_gid_roce(struct ib_device *ibdev, u8 port, int index,
160 union ib_gid *gid);
161 int irdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
162 u16 *pkey);
163 int irdma_query_port(struct ib_device *ibdev, u8 port,
164 struct ib_port_attr *props);
165 struct rdma_hw_stats *irdma_alloc_hw_stats(struct ib_device *ibdev, u8 port_num);
166 int irdma_get_hw_stats(struct ib_device *ibdev,
167 struct rdma_hw_stats *stats, u8 port_num,
168 int index);
169
170 void irdma_request_reset(struct irdma_pci_f *rf);
171 int irdma_register_qset(struct irdma_sc_vsi *vsi,
172 struct irdma_ws_node *tc_node);
173 void irdma_unregister_qset(struct irdma_sc_vsi *vsi,
174 struct irdma_ws_node *tc_node);
175 void ib_unregister_device(struct ib_device *ibdev);
176 void irdma_disassociate_ucontext(struct ib_ucontext *context);
177 int kc_irdma_set_roce_cm_info(struct irdma_qp *iwqp,
178 struct ib_qp_attr *attr,
179 u16 *vlan_id);
180
181 void kc_set_loc_seq_num_mss(struct irdma_cm_node *cm_node);
182 u16 kc_rdma_get_udp_sport(u32 fl, u32 lqpn, u32 rqpn);
183
184 void irdma_get_dev_fw_str(struct ib_device *dev, char *str, size_t str_len);
185
186 int irdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
187 struct ib_port_modify *props);
188 int irdma_get_dst_mac(struct irdma_cm_node *cm_node, struct sockaddr *dst_sin,
189 u8 *dst_mac);
190 int irdma_resolve_neigh_lpb_chk(struct irdma_device *iwdev, struct irdma_cm_node *cm_node,
191 struct irdma_cm_info *cm_info);
192 int irdma_addr_resolve_neigh(struct irdma_cm_node *cm_node, u32 dst_ip,
193 int arpindex);
194 int irdma_addr_resolve_neigh_ipv6(struct irdma_cm_node *cm_node, u32 *dest,
195 int arpindex);
196 void irdma_dcqcn_tunables_init(struct irdma_pci_f *rf);
197 void irdma_sysctl_settings(struct irdma_pci_f *rf);
198 void irdma_qos_info_tunables_init(struct irdma_pci_f *rf);
199 void irdma_sw_stats_tunables_init(struct irdma_pci_f *rf);
200 u32 irdma_create_stag(struct irdma_device *iwdev);
201 void irdma_free_stag(struct irdma_device *iwdev, u32 stag);
202
203 int irdma_hwdereg_mr(struct ib_mr *ib_mr);
204 int irdma_rereg_user_mr(struct ib_mr *ib_mr, int flags, u64 start, u64 len,
205 u64 virt, int new_access, struct ib_pd *new_pd,
206 struct ib_udata *udata);
207 struct irdma_mr;
208 struct irdma_cq;
209 struct irdma_cq_buf;
210 int irdma_reg_user_mr_type_qp(struct irdma_mem_reg_req req,
211 struct ib_udata *udata,
212 struct irdma_mr *iwmr);
213 int irdma_reg_user_mr_type_cq(struct irdma_mem_reg_req req,
214 struct ib_udata *udata,
215 struct irdma_mr *iwmr);
216 struct ib_mr *irdma_reg_user_mr(struct ib_pd *pd, u64 start, u64 len,
217 u64 virt, int access,
218 struct ib_udata *udata);
219 struct ib_mr *irdma_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
220 u32 max_num_sg, struct ib_udata *udata);
221 int irdma_hwreg_mr(struct irdma_device *iwdev, struct irdma_mr *iwmr,
222 u16 access);
223 struct ib_mr *irdma_rereg_mr_trans(struct irdma_mr *iwmr, u64 start, u64 len,
224 u64 virt, struct ib_udata *udata);
225 int irdma_hw_alloc_mw(struct irdma_device *iwdev, struct irdma_mr *iwmr);
226 struct ib_mw *irdma_alloc_mw(struct ib_pd *pd, enum ib_mw_type type,
227 struct ib_udata *udata);
228 int irdma_hw_alloc_stag(struct irdma_device *iwdev, struct irdma_mr *iwmr);
229 void irdma_cq_free_rsrc(struct irdma_pci_f *rf, struct irdma_cq *iwcq);
230 int irdma_validate_qp_attrs(struct ib_qp_init_attr *init_attr,
231 struct irdma_device *iwdev);
232 void irdma_setup_virt_qp(struct irdma_device *iwdev,
233 struct irdma_qp *iwqp,
234 struct irdma_qp_init_info *init_info);
235 int irdma_setup_kmode_qp(struct irdma_device *iwdev,
236 struct irdma_qp *iwqp,
237 struct irdma_qp_init_info *info,
238 struct ib_qp_init_attr *init_attr);
239 int irdma_setup_umode_qp(struct ib_udata *udata,
240 struct irdma_device *iwdev,
241 struct irdma_qp *iwqp,
242 struct irdma_qp_init_info *info,
243 struct ib_qp_init_attr *init_attr);
244 void irdma_roce_fill_and_set_qpctx_info(struct irdma_qp *iwqp,
245 struct irdma_qp_host_ctx_info *ctx_info);
246 void irdma_iw_fill_and_set_qpctx_info(struct irdma_qp *iwqp,
247 struct irdma_qp_host_ctx_info *ctx_info);
248 int irdma_cqp_create_qp_cmd(struct irdma_qp *iwqp);
249 void irdma_dealloc_push_page(struct irdma_pci_f *rf,
250 struct irdma_qp *iwqp);
251 int irdma_process_resize_list(struct irdma_cq *iwcq, struct irdma_device *iwdev,
252 struct irdma_cq_buf *lcqe_buf);
253 void irdma_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata);
254 int irdma_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata);
255 void irdma_dealloc_ucontext(struct ib_ucontext *context);
256 int irdma_alloc_pd(struct ib_pd *pd, struct ib_udata *udata);
257 void irdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata);
258 int irdma_add_gid(struct ib_device *, u8, unsigned int, const union ib_gid *,
259 const struct ib_gid_attr *, void **);
260 int irdma_del_gid(struct ib_device *, u8, unsigned int, void **);
261 struct ib_device *ib_device_get_by_netdev(struct ifnet *ndev, int driver_id);
262 void ib_device_put(struct ib_device *device);
263 void ib_unregister_device_put(struct ib_device *device);
264 enum ib_mtu ib_mtu_int_to_enum(int mtu);
265 struct irdma_pbl *irdma_get_pbl(unsigned long va, struct list_head *pbl_list);
266 void irdma_clean_cqes(struct irdma_qp *iwqp, struct irdma_cq *iwcq);
267 void irdma_remove_push_mmap_entries(struct irdma_qp *iwqp);
268
269 struct irdma_ucontext;
270 void irdma_del_memlist(struct irdma_mr *iwmr, struct irdma_ucontext *ucontext);
271 void irdma_copy_user_pgaddrs(struct irdma_mr *iwmr, u64 *pbl,
272 enum irdma_pble_level level);
273 void irdma_reg_ipaddr_event_cb(struct irdma_pci_f *rf);
274 void irdma_dereg_ipaddr_event_cb(struct irdma_pci_f *rf);
275
276 /* Introduced in this series https://lore.kernel.org/linux-rdma/0-v2-270386b7e60b+28f4-umem_1_jgg@nvidia.com/
277 * An irdma version helper doing same for older functions with difference that iova is passed in
278 * as opposed to derived from umem->iova.
279 */
irdma_ib_umem_num_dma_blocks(struct ib_umem * umem,unsigned long pgsz,u64 iova)280 static inline size_t irdma_ib_umem_num_dma_blocks(struct ib_umem *umem, unsigned long pgsz, u64 iova)
281 {
282 /* some older OFED distros do not have ALIGN_DOWN */
283 #ifndef ALIGN_DOWN
284 #define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
285 #endif
286
287 return (size_t)((ALIGN(iova + umem->length, pgsz) -
288 ALIGN_DOWN(iova, pgsz))) / pgsz;
289 }
290
addrconf_addr_eui48(u8 * deui,const char * const addr)291 static inline void addrconf_addr_eui48(u8 *deui, const char *const addr)
292 {
293 memcpy(deui, addr, 3);
294 deui[3] = 0xFF;
295 deui[4] = 0xFE;
296 memcpy(deui + 5, addr + 3, 3);
297 deui[0] ^= 2;
298 }
299
300 #endif /* FBSD_KCOMPAT_H */
301