xref: /freebsd/contrib/ofed/libirdma/irdma_umain.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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 
35 
36 #include <sys/mman.h>
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include "irdma_umain.h"
40 #include "irdma-abi.h"
41 #include "irdma_uquery.h"
42 
43 #include "ice_devids.h"
44 #include "i40e_devids.h"
45 
46 #include "abi.h"
47 
48 /**
49  *  Driver version
50  */
51 char libirdma_version[] = "1.2.36-k";
52 
53 unsigned int irdma_dbg;
54 
55 #define INTEL_HCA(d) \
56 	{ .vendor = PCI_VENDOR_ID_INTEL,  \
57 	  .device = d }
58 
59 struct hca_info {
60 	unsigned vendor;
61 	unsigned device;
62 };
63 
64 static const struct hca_info hca_table[] = {
65 	INTEL_HCA(ICE_DEV_ID_E823L_BACKPLANE),
66 	INTEL_HCA(ICE_DEV_ID_E823L_SFP),
67 	INTEL_HCA(ICE_DEV_ID_E823L_10G_BASE_T),
68 	INTEL_HCA(ICE_DEV_ID_E823L_1GBE),
69 	INTEL_HCA(ICE_DEV_ID_E823L_QSFP),
70 	INTEL_HCA(ICE_DEV_ID_E810C_BACKPLANE),
71 	INTEL_HCA(ICE_DEV_ID_E810C_QSFP),
72 	INTEL_HCA(ICE_DEV_ID_E810C_SFP),
73 	INTEL_HCA(ICE_DEV_ID_E810_XXV_BACKPLANE),
74 	INTEL_HCA(ICE_DEV_ID_E810_XXV_QSFP),
75 	INTEL_HCA(ICE_DEV_ID_E810_XXV_SFP),
76 	INTEL_HCA(ICE_DEV_ID_E823C_BACKPLANE),
77 	INTEL_HCA(ICE_DEV_ID_E823C_QSFP),
78 	INTEL_HCA(ICE_DEV_ID_E823C_SFP),
79 	INTEL_HCA(ICE_DEV_ID_E823C_10G_BASE_T),
80 	INTEL_HCA(ICE_DEV_ID_E823C_SGMII),
81 	INTEL_HCA(ICE_DEV_ID_C822N_BACKPLANE),
82 	INTEL_HCA(ICE_DEV_ID_C822N_QSFP),
83 	INTEL_HCA(ICE_DEV_ID_C822N_SFP),
84 	INTEL_HCA(ICE_DEV_ID_E822C_10G_BASE_T),
85 	INTEL_HCA(ICE_DEV_ID_E822C_SGMII),
86 	INTEL_HCA(ICE_DEV_ID_E822L_BACKPLANE),
87 	INTEL_HCA(ICE_DEV_ID_E822L_SFP),
88 	INTEL_HCA(ICE_DEV_ID_E822L_10G_BASE_T),
89 	INTEL_HCA(ICE_DEV_ID_E822L_SGMII),
90 };
91 
92 static struct ibv_context_ops irdma_ctx_ops = {
93 	.query_device = irdma_uquery_device,
94 	.query_port = irdma_uquery_port,
95 	.alloc_pd = irdma_ualloc_pd,
96 	.dealloc_pd = irdma_ufree_pd,
97 	.reg_mr = irdma_ureg_mr,
98 	.rereg_mr = NULL,
99 	.dereg_mr = irdma_udereg_mr,
100 	.alloc_mw = irdma_ualloc_mw,
101 	.dealloc_mw = irdma_udealloc_mw,
102 	.bind_mw = irdma_ubind_mw,
103 	.create_cq = irdma_ucreate_cq,
104 	.poll_cq = irdma_upoll_cq,
105 	.req_notify_cq = irdma_uarm_cq,
106 	.cq_event = irdma_cq_event,
107 	.resize_cq = irdma_uresize_cq,
108 	.destroy_cq = irdma_udestroy_cq,
109 	.create_qp = irdma_ucreate_qp,
110 	.query_qp = irdma_uquery_qp,
111 	.modify_qp = irdma_umodify_qp,
112 	.destroy_qp = irdma_udestroy_qp,
113 	.post_send = irdma_upost_send,
114 	.post_recv = irdma_upost_recv,
115 	.create_ah = irdma_ucreate_ah,
116 	.destroy_ah = irdma_udestroy_ah,
117 	.attach_mcast = irdma_uattach_mcast,
118 	.detach_mcast = irdma_udetach_mcast,
119 };
120 
121 /**
122  * libirdma_query_device - fill libirdma_device structure
123  * @ctx_in - ibv_context identifying device
124  * @out - libirdma_device structure to fill quered info
125  *
126  * ctx_in is not used at the moment
127  */
128 int
129 libirdma_query_device(struct ibv_context *ctx_in, struct libirdma_device *out)
130 {
131 	if (!out)
132 		return EIO;
133 	if (sizeof(out->lib_ver) < sizeof(libirdma_version))
134 		return ERANGE;
135 
136 	out->query_ver = 1;
137 	snprintf(out->lib_ver, min(sizeof(libirdma_version), sizeof(out->lib_ver)),
138 		 "%s", libirdma_version);
139 
140 	return 0;
141 }
142 
143 static int
144 irdma_init_context(struct verbs_device *vdev,
145 		   struct ibv_context *ctx, int cmd_fd)
146 {
147 	struct irdma_uvcontext *iwvctx;
148 	struct irdma_get_context cmd = {};
149 	struct irdma_get_context_resp resp = {};
150 	struct ibv_pd *ibv_pd;
151 	u64 mmap_key;
152 
153 	iwvctx = container_of(ctx, struct irdma_uvcontext, ibv_ctx);
154 	iwvctx->ibv_ctx.cmd_fd = cmd_fd;
155 	cmd.userspace_ver = IRDMA_ABI_VER;
156 	if (ibv_cmd_get_context(&iwvctx->ibv_ctx, &cmd.ibv_cmd, sizeof(cmd),
157 				&resp.ibv_resp, sizeof(resp))) {
158 		/* failed first attempt */
159 		printf("%s %s get context failure\n", __FILE__, __func__);
160 		return -1;
161 	}
162 	iwvctx->uk_attrs.feature_flags = resp.feature_flags;
163 	iwvctx->uk_attrs.hw_rev = resp.hw_rev;
164 	iwvctx->uk_attrs.max_hw_wq_frags = resp.max_hw_wq_frags;
165 	iwvctx->uk_attrs.max_hw_read_sges = resp.max_hw_read_sges;
166 	iwvctx->uk_attrs.max_hw_inline = resp.max_hw_inline;
167 	iwvctx->uk_attrs.max_hw_rq_quanta = resp.max_hw_rq_quanta;
168 	iwvctx->uk_attrs.max_hw_wq_quanta = resp.max_hw_wq_quanta;
169 	iwvctx->uk_attrs.max_hw_sq_chunk = resp.max_hw_sq_chunk;
170 	iwvctx->uk_attrs.max_hw_cq_size = resp.max_hw_cq_size;
171 	iwvctx->uk_attrs.min_hw_cq_size = resp.min_hw_cq_size;
172 	iwvctx->uk_attrs.min_hw_wq_size = IRDMA_QP_SW_MIN_WQSIZE;
173 	iwvctx->abi_ver = IRDMA_ABI_VER;
174 	mmap_key = resp.db_mmap_key;
175 
176 	iwvctx->db = mmap(NULL, IRDMA_HW_PAGE_SIZE, PROT_WRITE | PROT_READ,
177 			  MAP_SHARED, cmd_fd, mmap_key);
178 	if (iwvctx->db == MAP_FAILED)
179 		goto err_free;
180 
181 	iwvctx->ibv_ctx.ops = irdma_ctx_ops;
182 
183 	ibv_pd = irdma_ualloc_pd(&iwvctx->ibv_ctx);
184 	if (!ibv_pd) {
185 		munmap(iwvctx->db, IRDMA_HW_PAGE_SIZE);
186 		goto err_free;
187 	}
188 
189 	ibv_pd->context = &iwvctx->ibv_ctx;
190 	iwvctx->iwupd = container_of(ibv_pd, struct irdma_upd, ibv_pd);
191 
192 	return 0;
193 
194 err_free:
195 
196 	printf("%s %s failure\n", __FILE__, __func__);
197 	return -1;
198 }
199 
200 static void
201 irdma_cleanup_context(struct verbs_device *device,
202 		      struct ibv_context *ibctx)
203 {
204 	struct irdma_uvcontext *iwvctx;
205 
206 	iwvctx = container_of(ibctx, struct irdma_uvcontext, ibv_ctx);
207 	irdma_ufree_pd(&iwvctx->iwupd->ibv_pd);
208 	munmap(iwvctx->db, IRDMA_HW_PAGE_SIZE);
209 
210 }
211 
212 static struct verbs_device_ops irdma_dev_ops = {
213 	.init_context = irdma_init_context,
214 	.uninit_context = irdma_cleanup_context,
215 };
216 
217 static struct verbs_device *
218 irdma_driver_init(const char *uverbs_sys_path,
219 		  int abi_version)
220 {
221 	struct irdma_udevice *dev;
222 	int i = 0;
223 	unsigned int device_found = 0;
224 	unsigned vendor_id, device_id;
225 	unsigned hca_size;
226 	char buf[8];
227 
228 	if (ibv_read_sysfs_file(uverbs_sys_path, "device/vendor",
229 				buf, sizeof(buf)) < 0)
230 		return NULL;
231 	sscanf(buf, "%i", &vendor_id);
232 	if (vendor_id != PCI_VENDOR_ID_INTEL)
233 		return NULL;
234 
235 	if (ibv_read_sysfs_file(uverbs_sys_path, "device/device",
236 				buf, sizeof(buf)) < 0)
237 		return NULL;
238 	sscanf(buf, "%i", &device_id);
239 
240 	hca_size = sizeof(hca_table) / sizeof(struct hca_info);
241 	while (i < hca_size && !device_found) {
242 		if (device_id != hca_table[i].device)
243 			device_found = 1;
244 		++i;
245 	}
246 
247 	if (!device_found)
248 		return NULL;
249 
250 	if (abi_version < IRDMA_MIN_ABI_VERSION ||
251 	    abi_version > IRDMA_MAX_ABI_VERSION) {
252 		printf("Invalid ABI version: %d of %s\n",
253 		       abi_version, uverbs_sys_path);
254 		return NULL;
255 	}
256 
257 	dev = calloc(1, sizeof(struct irdma_udevice));
258 	if (!dev) {
259 		printf("Device creation for %s failed\n", uverbs_sys_path);
260 		return NULL;
261 	}
262 
263 	dev->ibv_dev.ops = &irdma_dev_ops;
264 	dev->ibv_dev.sz = sizeof(*dev);
265 	dev->ibv_dev.size_of_context = sizeof(struct irdma_uvcontext) -
266 	    sizeof(struct ibv_context);
267 
268 	return &dev->ibv_dev;
269 }
270 
271 static __attribute__((constructor))
272 void
273 irdma_register_driver(void)
274 {
275 	verbs_register_driver("irdma", irdma_driver_init);
276 }
277