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