1 /* 2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 */ 33 #include <linux/init.h> 34 #include <linux/slab.h> 35 #include <linux/errno.h> 36 37 #include <rdma/ib_user_verbs.h> 38 #include <rdma/ib_addr.h> 39 #include <rdma/uverbs_ioctl.h> 40 41 #include "usnic_abi.h" 42 #include "usnic_ib.h" 43 #include "usnic_common_util.h" 44 #include "usnic_ib_qp_grp.h" 45 #include "usnic_ib_verbs.h" 46 #include "usnic_fwd.h" 47 #include "usnic_log.h" 48 #include "usnic_uiom.h" 49 #include "usnic_transport.h" 50 51 #define USNIC_DEFAULT_TRANSPORT USNIC_TRANSPORT_ROCE_CUSTOM 52 53 const struct usnic_vnic_res_spec min_transport_spec[USNIC_TRANSPORT_MAX] = { 54 { /*USNIC_TRANSPORT_UNKNOWN*/ 55 .resources = { 56 {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,}, 57 }, 58 }, 59 { /*USNIC_TRANSPORT_ROCE_CUSTOM*/ 60 .resources = { 61 {.type = USNIC_VNIC_RES_TYPE_WQ, .cnt = 1,}, 62 {.type = USNIC_VNIC_RES_TYPE_RQ, .cnt = 1,}, 63 {.type = USNIC_VNIC_RES_TYPE_CQ, .cnt = 1,}, 64 {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,}, 65 }, 66 }, 67 { /*USNIC_TRANSPORT_IPV4_UDP*/ 68 .resources = { 69 {.type = USNIC_VNIC_RES_TYPE_WQ, .cnt = 1,}, 70 {.type = USNIC_VNIC_RES_TYPE_RQ, .cnt = 1,}, 71 {.type = USNIC_VNIC_RES_TYPE_CQ, .cnt = 1,}, 72 {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,}, 73 }, 74 }, 75 }; 76 77 static void usnic_ib_fw_string_to_u64(char *fw_ver_str, u64 *fw_ver) 78 { 79 *fw_ver = *((u64 *)fw_ver_str); 80 } 81 82 static int usnic_ib_fill_create_qp_resp(struct usnic_ib_qp_grp *qp_grp, 83 struct ib_udata *udata) 84 { 85 struct usnic_ib_dev *us_ibdev; 86 struct usnic_ib_create_qp_resp resp; 87 struct pci_dev *pdev; 88 struct vnic_dev_bar *bar; 89 struct usnic_vnic_res_chunk *chunk; 90 struct usnic_ib_qp_grp_flow *default_flow; 91 int i, err; 92 93 memset(&resp, 0, sizeof(resp)); 94 95 us_ibdev = qp_grp->vf->pf; 96 pdev = usnic_vnic_get_pdev(qp_grp->vf->vnic); 97 if (!pdev) { 98 usnic_err("Failed to get pdev of qp_grp %d\n", 99 qp_grp->grp_id); 100 return -EFAULT; 101 } 102 103 bar = usnic_vnic_get_bar(qp_grp->vf->vnic, 0); 104 if (!bar) { 105 usnic_err("Failed to get bar0 of qp_grp %d vf %s", 106 qp_grp->grp_id, pci_name(pdev)); 107 return -EFAULT; 108 } 109 110 resp.vfid = usnic_vnic_get_index(qp_grp->vf->vnic); 111 resp.bar_bus_addr = bar->bus_addr; 112 resp.bar_len = bar->len; 113 114 chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ); 115 if (IS_ERR(chunk)) { 116 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n", 117 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_RQ), 118 qp_grp->grp_id, 119 PTR_ERR(chunk)); 120 return PTR_ERR(chunk); 121 } 122 123 WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_RQ); 124 resp.rq_cnt = chunk->cnt; 125 for (i = 0; i < chunk->cnt; i++) 126 resp.rq_idx[i] = chunk->res[i]->vnic_idx; 127 128 chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_WQ); 129 if (IS_ERR(chunk)) { 130 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n", 131 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_WQ), 132 qp_grp->grp_id, 133 PTR_ERR(chunk)); 134 return PTR_ERR(chunk); 135 } 136 137 WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_WQ); 138 resp.wq_cnt = chunk->cnt; 139 for (i = 0; i < chunk->cnt; i++) 140 resp.wq_idx[i] = chunk->res[i]->vnic_idx; 141 142 chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_CQ); 143 if (IS_ERR(chunk)) { 144 usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n", 145 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_CQ), 146 qp_grp->grp_id, 147 PTR_ERR(chunk)); 148 return PTR_ERR(chunk); 149 } 150 151 WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_CQ); 152 resp.cq_cnt = chunk->cnt; 153 for (i = 0; i < chunk->cnt; i++) 154 resp.cq_idx[i] = chunk->res[i]->vnic_idx; 155 156 default_flow = list_first_entry(&qp_grp->flows_lst, 157 struct usnic_ib_qp_grp_flow, link); 158 resp.transport = default_flow->trans_type; 159 160 err = ib_copy_to_udata(udata, &resp, sizeof(resp)); 161 if (err) { 162 usnic_err("Failed to copy udata for %s", 163 dev_name(&us_ibdev->ib_dev.dev)); 164 return err; 165 } 166 167 return 0; 168 } 169 170 static int 171 find_free_vf_and_create_qp_grp(struct ib_qp *qp, 172 struct usnic_transport_spec *trans_spec, 173 struct usnic_vnic_res_spec *res_spec) 174 { 175 struct usnic_ib_dev *us_ibdev = to_usdev(qp->device); 176 struct usnic_ib_pd *pd = to_upd(qp->pd); 177 struct usnic_ib_vf *vf; 178 struct usnic_vnic *vnic; 179 struct usnic_ib_qp_grp *qp_grp = to_uqp_grp(qp); 180 struct device *dev, **dev_list; 181 int i, ret; 182 183 BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock)); 184 185 if (list_empty(&us_ibdev->vf_dev_list)) { 186 usnic_info("No vfs to allocate\n"); 187 return -ENOMEM; 188 } 189 190 if (usnic_ib_share_vf) { 191 /* Try to find resouces on a used vf which is in pd */ 192 dev_list = usnic_uiom_get_dev_list(pd->umem_pd); 193 if (IS_ERR(dev_list)) 194 return PTR_ERR(dev_list); 195 for (i = 0; dev_list[i]; i++) { 196 dev = dev_list[i]; 197 vf = dev_get_drvdata(dev); 198 mutex_lock(&vf->lock); 199 vnic = vf->vnic; 200 if (!usnic_vnic_check_room(vnic, res_spec)) { 201 usnic_dbg("Found used vnic %s from %s\n", 202 dev_name(&us_ibdev->ib_dev.dev), 203 pci_name(usnic_vnic_get_pdev( 204 vnic))); 205 ret = usnic_ib_qp_grp_create(qp_grp, 206 us_ibdev->ufdev, 207 vf, pd, res_spec, 208 trans_spec); 209 210 mutex_unlock(&vf->lock); 211 goto qp_grp_check; 212 } 213 mutex_unlock(&vf->lock); 214 215 } 216 usnic_uiom_free_dev_list(dev_list); 217 dev_list = NULL; 218 } 219 220 /* Try to find resources on an unused vf */ 221 list_for_each_entry(vf, &us_ibdev->vf_dev_list, link) { 222 mutex_lock(&vf->lock); 223 vnic = vf->vnic; 224 if (vf->qp_grp_ref_cnt == 0 && 225 usnic_vnic_check_room(vnic, res_spec) == 0) { 226 ret = usnic_ib_qp_grp_create(qp_grp, us_ibdev->ufdev, 227 vf, pd, res_spec, 228 trans_spec); 229 230 mutex_unlock(&vf->lock); 231 goto qp_grp_check; 232 } 233 mutex_unlock(&vf->lock); 234 } 235 236 usnic_info("No free qp grp found on %s\n", 237 dev_name(&us_ibdev->ib_dev.dev)); 238 return -ENOMEM; 239 240 qp_grp_check: 241 if (ret) { 242 usnic_err("Failed to allocate qp_grp\n"); 243 if (usnic_ib_share_vf) 244 usnic_uiom_free_dev_list(dev_list); 245 } 246 return ret; 247 } 248 249 static void qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp) 250 { 251 struct usnic_ib_vf *vf = qp_grp->vf; 252 253 WARN_ON(qp_grp->state != IB_QPS_RESET); 254 255 mutex_lock(&vf->lock); 256 usnic_ib_qp_grp_destroy(qp_grp); 257 mutex_unlock(&vf->lock); 258 } 259 260 static int create_qp_validate_user_data(struct usnic_ib_create_qp_cmd cmd) 261 { 262 if (cmd.spec.trans_type <= USNIC_TRANSPORT_UNKNOWN || 263 cmd.spec.trans_type >= USNIC_TRANSPORT_MAX) 264 return -EINVAL; 265 266 return 0; 267 } 268 269 /* Start of ib callback functions */ 270 271 enum rdma_link_layer usnic_ib_port_link_layer(struct ib_device *device, 272 u32 port_num) 273 { 274 return IB_LINK_LAYER_ETHERNET; 275 } 276 277 int usnic_ib_query_device(struct ib_device *ibdev, 278 struct ib_device_attr *props, 279 struct ib_udata *uhw) 280 { 281 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev); 282 union ib_gid gid; 283 struct ethtool_drvinfo info; 284 int qp_per_vf; 285 286 usnic_dbg("\n"); 287 if (uhw->inlen || uhw->outlen) 288 return -EINVAL; 289 290 mutex_lock(&us_ibdev->usdev_lock); 291 us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info); 292 memset(props, 0, sizeof(*props)); 293 usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr, 294 &gid.raw[0]); 295 memcpy(&props->sys_image_guid, &gid.global.interface_id, 296 sizeof(gid.global.interface_id)); 297 usnic_ib_fw_string_to_u64(&info.fw_version[0], &props->fw_ver); 298 props->max_mr_size = USNIC_UIOM_MAX_MR_SIZE; 299 props->page_size_cap = USNIC_UIOM_PAGE_SIZE; 300 props->vendor_id = PCI_VENDOR_ID_CISCO; 301 props->vendor_part_id = PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC; 302 props->hw_ver = us_ibdev->pdev->subsystem_device; 303 qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ], 304 us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]); 305 props->max_qp = qp_per_vf * 306 kref_read(&us_ibdev->vf_cnt); 307 props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT | 308 IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 309 props->max_cq = us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ] * 310 kref_read(&us_ibdev->vf_cnt); 311 props->max_pd = USNIC_UIOM_MAX_PD_CNT; 312 props->max_mr = USNIC_UIOM_MAX_MR_CNT; 313 props->local_ca_ack_delay = 0; 314 props->max_pkeys = 0; 315 props->atomic_cap = IB_ATOMIC_NONE; 316 props->masked_atomic_cap = props->atomic_cap; 317 props->max_qp_rd_atom = 0; 318 props->max_qp_init_rd_atom = 0; 319 props->max_res_rd_atom = 0; 320 props->max_srq = 0; 321 props->max_srq_wr = 0; 322 props->max_srq_sge = 0; 323 props->max_fast_reg_page_list_len = 0; 324 props->max_mcast_grp = 0; 325 props->max_mcast_qp_attach = 0; 326 props->max_total_mcast_qp_attach = 0; 327 /* Owned by Userspace 328 * max_qp_wr, max_sge, max_sge_rd, max_cqe */ 329 mutex_unlock(&us_ibdev->usdev_lock); 330 331 return 0; 332 } 333 334 int usnic_ib_query_port(struct ib_device *ibdev, u32 port, 335 struct ib_port_attr *props) 336 { 337 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev); 338 339 usnic_dbg("\n"); 340 341 if (ib_get_eth_speed(ibdev, port, &props->active_speed, 342 &props->active_width)) 343 return -EINVAL; 344 345 /* 346 * usdev_lock is acquired after (and not before) ib_get_eth_speed call 347 * because acquiring rtnl_lock in ib_get_eth_speed, while holding 348 * usdev_lock could lead to a deadlock. 349 */ 350 mutex_lock(&us_ibdev->usdev_lock); 351 /* props being zeroed by the caller, avoid zeroing it here */ 352 353 props->lid = 0; 354 props->lmc = 1; 355 props->sm_lid = 0; 356 props->sm_sl = 0; 357 358 if (!us_ibdev->ufdev->link_up) { 359 props->state = IB_PORT_DOWN; 360 props->phys_state = IB_PORT_PHYS_STATE_DISABLED; 361 } else if (!us_ibdev->ufdev->inaddr) { 362 props->state = IB_PORT_INIT; 363 props->phys_state = 364 IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING; 365 } else { 366 props->state = IB_PORT_ACTIVE; 367 props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 368 } 369 370 props->port_cap_flags = 0; 371 props->gid_tbl_len = 1; 372 props->bad_pkey_cntr = 0; 373 props->qkey_viol_cntr = 0; 374 props->max_mtu = IB_MTU_4096; 375 props->active_mtu = iboe_get_mtu(us_ibdev->ufdev->mtu); 376 /* Userspace will adjust for hdrs */ 377 props->max_msg_sz = us_ibdev->ufdev->mtu; 378 props->max_vl_num = 1; 379 mutex_unlock(&us_ibdev->usdev_lock); 380 381 return 0; 382 } 383 384 int usnic_ib_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr, 385 int qp_attr_mask, 386 struct ib_qp_init_attr *qp_init_attr) 387 { 388 struct usnic_ib_qp_grp *qp_grp; 389 struct usnic_ib_vf *vf; 390 int err; 391 392 usnic_dbg("\n"); 393 394 memset(qp_attr, 0, sizeof(*qp_attr)); 395 memset(qp_init_attr, 0, sizeof(*qp_init_attr)); 396 397 qp_grp = to_uqp_grp(qp); 398 vf = qp_grp->vf; 399 mutex_lock(&vf->pf->usdev_lock); 400 usnic_dbg("\n"); 401 qp_attr->qp_state = qp_grp->state; 402 qp_attr->cur_qp_state = qp_grp->state; 403 404 switch (qp_grp->ibqp.qp_type) { 405 case IB_QPT_UD: 406 qp_attr->qkey = 0; 407 break; 408 default: 409 usnic_err("Unexpected qp_type %d\n", qp_grp->ibqp.qp_type); 410 err = -EINVAL; 411 goto err_out; 412 } 413 414 mutex_unlock(&vf->pf->usdev_lock); 415 return 0; 416 417 err_out: 418 mutex_unlock(&vf->pf->usdev_lock); 419 return err; 420 } 421 422 int usnic_ib_query_gid(struct ib_device *ibdev, u32 port, int index, 423 union ib_gid *gid) 424 { 425 426 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev); 427 usnic_dbg("\n"); 428 429 if (index > 1) 430 return -EINVAL; 431 432 mutex_lock(&us_ibdev->usdev_lock); 433 memset(&(gid->raw[0]), 0, sizeof(gid->raw)); 434 usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr, 435 &gid->raw[0]); 436 mutex_unlock(&us_ibdev->usdev_lock); 437 438 return 0; 439 } 440 441 int usnic_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 442 { 443 struct usnic_ib_pd *pd = to_upd(ibpd); 444 445 pd->umem_pd = usnic_uiom_alloc_pd(); 446 if (IS_ERR(pd->umem_pd)) 447 return PTR_ERR(pd->umem_pd); 448 449 return 0; 450 } 451 452 int usnic_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) 453 { 454 usnic_uiom_dealloc_pd((to_upd(pd))->umem_pd); 455 return 0; 456 } 457 458 int usnic_ib_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr, 459 struct ib_udata *udata) 460 { 461 int err; 462 struct usnic_ib_dev *us_ibdev; 463 struct usnic_ib_qp_grp *qp_grp = to_uqp_grp(ibqp); 464 struct usnic_ib_ucontext *ucontext = rdma_udata_to_drv_context( 465 udata, struct usnic_ib_ucontext, ibucontext); 466 int cq_cnt; 467 struct usnic_vnic_res_spec res_spec; 468 struct usnic_ib_create_qp_cmd cmd; 469 struct usnic_transport_spec trans_spec; 470 471 usnic_dbg("\n"); 472 473 us_ibdev = to_usdev(ibqp->device); 474 475 if (init_attr->create_flags) 476 return -EOPNOTSUPP; 477 478 err = ib_copy_from_udata(&cmd, udata, sizeof(cmd)); 479 if (err) { 480 usnic_err("%s: cannot copy udata for create_qp\n", 481 dev_name(&us_ibdev->ib_dev.dev)); 482 return -EINVAL; 483 } 484 485 err = create_qp_validate_user_data(cmd); 486 if (err) { 487 usnic_err("%s: Failed to validate user data\n", 488 dev_name(&us_ibdev->ib_dev.dev)); 489 return -EINVAL; 490 } 491 492 if (init_attr->qp_type != IB_QPT_UD) { 493 usnic_err("%s asked to make a non-UD QP: %d\n", 494 dev_name(&us_ibdev->ib_dev.dev), init_attr->qp_type); 495 return -EOPNOTSUPP; 496 } 497 498 trans_spec = cmd.spec; 499 mutex_lock(&us_ibdev->usdev_lock); 500 cq_cnt = (init_attr->send_cq == init_attr->recv_cq) ? 1 : 2; 501 res_spec = min_transport_spec[trans_spec.trans_type]; 502 usnic_vnic_res_spec_update(&res_spec, USNIC_VNIC_RES_TYPE_CQ, cq_cnt); 503 err = find_free_vf_and_create_qp_grp(ibqp, &trans_spec, &res_spec); 504 if (err) 505 goto out_release_mutex; 506 507 err = usnic_ib_fill_create_qp_resp(qp_grp, udata); 508 if (err) { 509 err = -EBUSY; 510 goto out_release_qp_grp; 511 } 512 513 qp_grp->ctx = ucontext; 514 list_add_tail(&qp_grp->link, &ucontext->qp_grp_list); 515 usnic_ib_log_vf(qp_grp->vf); 516 mutex_unlock(&us_ibdev->usdev_lock); 517 return 0; 518 519 out_release_qp_grp: 520 qp_grp_destroy(qp_grp); 521 out_release_mutex: 522 mutex_unlock(&us_ibdev->usdev_lock); 523 return err; 524 } 525 526 int usnic_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata) 527 { 528 struct usnic_ib_qp_grp *qp_grp; 529 struct usnic_ib_vf *vf; 530 531 usnic_dbg("\n"); 532 533 qp_grp = to_uqp_grp(qp); 534 vf = qp_grp->vf; 535 mutex_lock(&vf->pf->usdev_lock); 536 if (usnic_ib_qp_grp_modify(qp_grp, IB_QPS_RESET, NULL)) { 537 usnic_err("Failed to move qp grp %u to reset\n", 538 qp_grp->grp_id); 539 } 540 541 list_del(&qp_grp->link); 542 qp_grp_destroy(qp_grp); 543 mutex_unlock(&vf->pf->usdev_lock); 544 545 return 0; 546 } 547 548 int usnic_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, 549 int attr_mask, struct ib_udata *udata) 550 { 551 struct usnic_ib_qp_grp *qp_grp; 552 int status; 553 usnic_dbg("\n"); 554 555 if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS) 556 return -EOPNOTSUPP; 557 558 qp_grp = to_uqp_grp(ibqp); 559 560 mutex_lock(&qp_grp->vf->pf->usdev_lock); 561 if ((attr_mask & IB_QP_PORT) && attr->port_num != 1) { 562 /* usnic devices only have one port */ 563 status = -EINVAL; 564 goto out_unlock; 565 } 566 if (attr_mask & IB_QP_STATE) { 567 status = usnic_ib_qp_grp_modify(qp_grp, attr->qp_state, NULL); 568 } else { 569 usnic_err("Unhandled request, attr_mask=0x%x\n", attr_mask); 570 status = -EINVAL; 571 } 572 573 out_unlock: 574 mutex_unlock(&qp_grp->vf->pf->usdev_lock); 575 return status; 576 } 577 578 int usnic_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, 579 struct ib_udata *udata) 580 { 581 if (attr->flags) 582 return -EOPNOTSUPP; 583 584 return 0; 585 } 586 587 int usnic_ib_destroy_cq(struct ib_cq *cq, struct ib_udata *udata) 588 { 589 return 0; 590 } 591 592 struct ib_mr *usnic_ib_reg_mr(struct ib_pd *pd, u64 start, u64 length, 593 u64 virt_addr, int access_flags, 594 struct ib_udata *udata) 595 { 596 struct usnic_ib_mr *mr; 597 int err; 598 599 usnic_dbg("start 0x%llx va 0x%llx length 0x%llx\n", start, 600 virt_addr, length); 601 602 mr = kzalloc(sizeof(*mr), GFP_KERNEL); 603 if (!mr) 604 return ERR_PTR(-ENOMEM); 605 606 mr->umem = usnic_uiom_reg_get(to_upd(pd)->umem_pd, start, length, 607 access_flags, 0); 608 if (IS_ERR_OR_NULL(mr->umem)) { 609 err = mr->umem ? PTR_ERR(mr->umem) : -EFAULT; 610 goto err_free; 611 } 612 613 mr->ibmr.lkey = mr->ibmr.rkey = 0; 614 return &mr->ibmr; 615 616 err_free: 617 kfree(mr); 618 return ERR_PTR(err); 619 } 620 621 int usnic_ib_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata) 622 { 623 struct usnic_ib_mr *mr = to_umr(ibmr); 624 625 usnic_dbg("va 0x%lx length 0x%zx\n", mr->umem->va, mr->umem->length); 626 627 usnic_uiom_reg_release(mr->umem); 628 kfree(mr); 629 return 0; 630 } 631 632 int usnic_ib_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata) 633 { 634 struct ib_device *ibdev = uctx->device; 635 struct usnic_ib_ucontext *context = to_ucontext(uctx); 636 struct usnic_ib_dev *us_ibdev = to_usdev(ibdev); 637 usnic_dbg("\n"); 638 639 INIT_LIST_HEAD(&context->qp_grp_list); 640 mutex_lock(&us_ibdev->usdev_lock); 641 list_add_tail(&context->link, &us_ibdev->ctx_list); 642 mutex_unlock(&us_ibdev->usdev_lock); 643 644 return 0; 645 } 646 647 void usnic_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 648 { 649 struct usnic_ib_ucontext *context = to_uucontext(ibcontext); 650 struct usnic_ib_dev *us_ibdev = to_usdev(ibcontext->device); 651 usnic_dbg("\n"); 652 653 mutex_lock(&us_ibdev->usdev_lock); 654 WARN_ON_ONCE(!list_empty(&context->qp_grp_list)); 655 list_del(&context->link); 656 mutex_unlock(&us_ibdev->usdev_lock); 657 } 658 659 int usnic_ib_mmap(struct ib_ucontext *context, 660 struct vm_area_struct *vma) 661 { 662 struct usnic_ib_ucontext *uctx = to_ucontext(context); 663 struct usnic_ib_dev *us_ibdev; 664 struct usnic_ib_qp_grp *qp_grp; 665 struct usnic_ib_vf *vf; 666 struct vnic_dev_bar *bar; 667 dma_addr_t bus_addr; 668 unsigned int len; 669 unsigned int vfid; 670 671 usnic_dbg("\n"); 672 673 us_ibdev = to_usdev(context->device); 674 vma->vm_flags |= VM_IO; 675 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 676 vfid = vma->vm_pgoff; 677 usnic_dbg("Page Offset %lu PAGE_SHIFT %u VFID %u\n", 678 vma->vm_pgoff, PAGE_SHIFT, vfid); 679 680 mutex_lock(&us_ibdev->usdev_lock); 681 list_for_each_entry(qp_grp, &uctx->qp_grp_list, link) { 682 vf = qp_grp->vf; 683 if (usnic_vnic_get_index(vf->vnic) == vfid) { 684 bar = usnic_vnic_get_bar(vf->vnic, 0); 685 if ((vma->vm_end - vma->vm_start) != bar->len) { 686 usnic_err("Bar0 Len %lu - Request map %lu\n", 687 bar->len, 688 vma->vm_end - vma->vm_start); 689 mutex_unlock(&us_ibdev->usdev_lock); 690 return -EINVAL; 691 } 692 bus_addr = bar->bus_addr; 693 len = bar->len; 694 usnic_dbg("bus: %pa vaddr: %p size: %ld\n", 695 &bus_addr, bar->vaddr, bar->len); 696 mutex_unlock(&us_ibdev->usdev_lock); 697 698 return remap_pfn_range(vma, 699 vma->vm_start, 700 bus_addr >> PAGE_SHIFT, 701 len, vma->vm_page_prot); 702 } 703 } 704 705 mutex_unlock(&us_ibdev->usdev_lock); 706 usnic_err("No VF %u found\n", vfid); 707 return -EINVAL; 708 } 709 710