xref: /freebsd/contrib/ofed/libirdma/irdma_umain.c (revision e3aa18ad)
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 
37 #include <sys/mman.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include "irdma_umain.h"
41 #include "irdma-abi.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[] = "0.0.51-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 static int
122 irdma_init_context(struct verbs_device *vdev,
123 		   struct ibv_context *ctx, int cmd_fd)
124 {
125 	struct irdma_uvcontext *iwvctx;
126 	struct irdma_get_context cmd = {};
127 	struct irdma_get_context_resp resp = {};
128 	struct ibv_pd *ibv_pd;
129 	u64 mmap_key;
130 
131 	iwvctx = container_of(ctx, struct irdma_uvcontext, ibv_ctx);
132 	iwvctx->ibv_ctx.cmd_fd = cmd_fd;
133 	cmd.userspace_ver = IRDMA_ABI_VER;
134 	if (ibv_cmd_get_context(&iwvctx->ibv_ctx, &cmd.ibv_cmd, sizeof(cmd),
135 				&resp.ibv_resp, sizeof(resp))) {
136 		/* failed first attempt */
137 		printf("%s %s get context failure\n", __FILE__, __func__);
138 		return -1;
139 	}
140 	iwvctx->uk_attrs.feature_flags = resp.feature_flags;
141 	iwvctx->uk_attrs.hw_rev = resp.hw_rev;
142 	iwvctx->uk_attrs.max_hw_wq_frags = resp.max_hw_wq_frags;
143 	iwvctx->uk_attrs.max_hw_read_sges = resp.max_hw_read_sges;
144 	iwvctx->uk_attrs.max_hw_inline = resp.max_hw_inline;
145 	iwvctx->uk_attrs.max_hw_rq_quanta = resp.max_hw_rq_quanta;
146 	iwvctx->uk_attrs.max_hw_wq_quanta = resp.max_hw_wq_quanta;
147 	iwvctx->uk_attrs.max_hw_sq_chunk = resp.max_hw_sq_chunk;
148 	iwvctx->uk_attrs.max_hw_cq_size = resp.max_hw_cq_size;
149 	iwvctx->uk_attrs.min_hw_cq_size = resp.min_hw_cq_size;
150 	iwvctx->abi_ver = IRDMA_ABI_VER;
151 	mmap_key = resp.db_mmap_key;
152 
153 	iwvctx->db = mmap(NULL, IRDMA_HW_PAGE_SIZE, PROT_WRITE | PROT_READ,
154 			  MAP_SHARED, cmd_fd, mmap_key);
155 	if (iwvctx->db == MAP_FAILED)
156 		goto err_free;
157 
158 	iwvctx->ibv_ctx.ops = irdma_ctx_ops;
159 
160 	ibv_pd = irdma_ualloc_pd(&iwvctx->ibv_ctx);
161 	if (!ibv_pd) {
162 		munmap(iwvctx->db, IRDMA_HW_PAGE_SIZE);
163 		goto err_free;
164 	}
165 
166 	ibv_pd->context = &iwvctx->ibv_ctx;
167 	iwvctx->iwupd = container_of(ibv_pd, struct irdma_upd, ibv_pd);
168 
169 	return 0;
170 
171 err_free:
172 
173 	printf("%s %s failure\n", __FILE__, __func__);
174 	return -1;
175 }
176 
177 static void
178 irdma_cleanup_context(struct verbs_device *device,
179 		      struct ibv_context *ibctx)
180 {
181 	struct irdma_uvcontext *iwvctx;
182 
183 	printf("%s %s CALL\n", __FILE__, __func__);
184 
185 	iwvctx = container_of(ibctx, struct irdma_uvcontext, ibv_ctx);
186 	irdma_ufree_pd(&iwvctx->iwupd->ibv_pd);
187 	munmap(iwvctx->db, IRDMA_HW_PAGE_SIZE);
188 
189 }
190 
191 static struct verbs_device_ops irdma_dev_ops = {
192 	.init_context = irdma_init_context,
193 	.uninit_context = irdma_cleanup_context,
194 };
195 
196 static struct verbs_device *
197 irdma_driver_init(const char *uverbs_sys_path,
198 		  int abi_version)
199 {
200 	struct irdma_udevice *dev;
201 	int i = 0;
202 	unsigned int device_found = 0;
203 	unsigned vendor_id, device_id;
204 	unsigned hca_size;
205 	char buf[8];
206 
207 	if (ibv_read_sysfs_file(uverbs_sys_path, "device/vendor",
208 				buf, sizeof(buf)) < 0)
209 		return NULL;
210 	sscanf(buf, "%i", &vendor_id);
211 	if (vendor_id != PCI_VENDOR_ID_INTEL)
212 		return NULL;
213 
214 	if (ibv_read_sysfs_file(uverbs_sys_path, "device/device",
215 				buf, sizeof(buf)) < 0)
216 		return NULL;
217 	sscanf(buf, "%i", &device_id);
218 
219 	hca_size = sizeof(hca_table) / sizeof(struct hca_info);
220 	while (i < hca_size && !device_found) {
221 		if (device_id != hca_table[i].device)
222 			device_found = 1;
223 		++i;
224 	}
225 
226 	if (!device_found)
227 		return NULL;
228 
229 	if (abi_version < IRDMA_MIN_ABI_VERSION ||
230 	    abi_version > IRDMA_MAX_ABI_VERSION) {
231 		printf("Invalid ABI version: %d of %s\n",
232 		       abi_version, uverbs_sys_path);
233 		return NULL;
234 	}
235 
236 	dev = calloc(1, sizeof(struct irdma_udevice));
237 	if (!dev) {
238 		printf("Device creation for %s failed\n", uverbs_sys_path);
239 		return NULL;
240 	}
241 
242 	dev->ibv_dev.ops = &irdma_dev_ops;
243 	dev->ibv_dev.sz = sizeof(*dev);
244 	dev->ibv_dev.size_of_context = sizeof(struct irdma_uvcontext) -
245 	    sizeof(struct ibv_context);
246 
247 	return &dev->ibv_dev;
248 }
249 
250 static __attribute__((constructor))
251 void
252 irdma_register_driver(void)
253 {
254 	verbs_register_driver("irdma", irdma_driver_init);
255 }
256