1 /* 2 * Copyright (c) 2012-2016 VMware, Inc. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of EITHER the GNU General Public License 6 * version 2 as published by the Free Software Foundation or the BSD 7 * 2-Clause License. This program is distributed in the hope that it 8 * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED 9 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 10 * See the GNU General Public License version 2 for more details at 11 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program available in the file COPYING in the main 15 * directory of this source tree. 16 * 17 * The BSD 2-Clause License 18 * 19 * Redistribution and use in source and binary forms, with or 20 * without modification, are permitted provided that the following 21 * conditions are met: 22 * 23 * - Redistributions of source code must retain the above 24 * copyright notice, this list of conditions and the following 25 * disclaimer. 26 * 27 * - Redistributions in binary form must reproduce the above 28 * copyright notice, this list of conditions and the following 29 * disclaimer in the documentation and/or other materials 30 * provided with the distribution. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 36 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 43 * OF THE POSSIBILITY OF SUCH DAMAGE. 44 */ 45 46 #include <asm/page.h> 47 #include <linux/inet.h> 48 #include <linux/io.h> 49 #include <rdma/ib_addr.h> 50 #include <rdma/ib_smi.h> 51 #include <rdma/ib_user_verbs.h> 52 #include <rdma/vmw_pvrdma-abi.h> 53 54 #include "pvrdma.h" 55 56 /** 57 * pvrdma_query_device - query device 58 * @ibdev: the device to query 59 * @props: the device properties 60 * @uhw: user data 61 * 62 * @return: 0 on success, otherwise negative errno 63 */ 64 int pvrdma_query_device(struct ib_device *ibdev, 65 struct ib_device_attr *props, 66 struct ib_udata *uhw) 67 { 68 struct pvrdma_dev *dev = to_vdev(ibdev); 69 70 if (uhw->inlen || uhw->outlen) 71 return -EINVAL; 72 73 memset(props, 0, sizeof(*props)); 74 75 props->fw_ver = dev->dsr->caps.fw_ver; 76 props->sys_image_guid = dev->dsr->caps.sys_image_guid; 77 props->max_mr_size = dev->dsr->caps.max_mr_size; 78 props->page_size_cap = dev->dsr->caps.page_size_cap; 79 props->vendor_id = dev->dsr->caps.vendor_id; 80 props->vendor_part_id = dev->pdev->device; 81 props->hw_ver = dev->dsr->caps.hw_ver; 82 props->max_qp = dev->dsr->caps.max_qp; 83 props->max_qp_wr = dev->dsr->caps.max_qp_wr; 84 props->device_cap_flags = dev->dsr->caps.device_cap_flags; 85 props->max_send_sge = dev->dsr->caps.max_sge; 86 props->max_recv_sge = dev->dsr->caps.max_sge; 87 props->max_sge_rd = PVRDMA_GET_CAP(dev, dev->dsr->caps.max_sge, 88 dev->dsr->caps.max_sge_rd); 89 props->max_srq = dev->dsr->caps.max_srq; 90 props->max_srq_wr = dev->dsr->caps.max_srq_wr; 91 props->max_srq_sge = dev->dsr->caps.max_srq_sge; 92 props->max_cq = dev->dsr->caps.max_cq; 93 props->max_cqe = dev->dsr->caps.max_cqe; 94 props->max_mr = dev->dsr->caps.max_mr; 95 props->max_pd = dev->dsr->caps.max_pd; 96 props->max_qp_rd_atom = dev->dsr->caps.max_qp_rd_atom; 97 props->max_qp_init_rd_atom = dev->dsr->caps.max_qp_init_rd_atom; 98 props->atomic_cap = 99 dev->dsr->caps.atomic_ops & 100 (PVRDMA_ATOMIC_OP_COMP_SWAP | PVRDMA_ATOMIC_OP_FETCH_ADD) ? 101 IB_ATOMIC_HCA : IB_ATOMIC_NONE; 102 props->masked_atomic_cap = props->atomic_cap; 103 props->max_ah = dev->dsr->caps.max_ah; 104 props->max_pkeys = dev->dsr->caps.max_pkeys; 105 props->local_ca_ack_delay = dev->dsr->caps.local_ca_ack_delay; 106 if ((dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_LOCAL_INV) && 107 (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_REMOTE_INV) && 108 (dev->dsr->caps.bmme_flags & PVRDMA_BMME_FLAG_FAST_REG_WR)) { 109 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 110 props->max_fast_reg_page_list_len = PVRDMA_GET_CAP(dev, 111 PVRDMA_MAX_FAST_REG_PAGES, 112 dev->dsr->caps.max_fast_reg_page_list_len); 113 } 114 115 props->device_cap_flags |= IB_DEVICE_PORT_ACTIVE_EVENT | 116 IB_DEVICE_RC_RNR_NAK_GEN; 117 118 return 0; 119 } 120 121 /** 122 * pvrdma_query_port - query device port attributes 123 * @ibdev: the device to query 124 * @port: the port number 125 * @props: the device properties 126 * 127 * @return: 0 on success, otherwise negative errno 128 */ 129 int pvrdma_query_port(struct ib_device *ibdev, u8 port, 130 struct ib_port_attr *props) 131 { 132 struct pvrdma_dev *dev = to_vdev(ibdev); 133 union pvrdma_cmd_req req; 134 union pvrdma_cmd_resp rsp; 135 struct pvrdma_cmd_query_port *cmd = &req.query_port; 136 struct pvrdma_cmd_query_port_resp *resp = &rsp.query_port_resp; 137 int err; 138 139 memset(cmd, 0, sizeof(*cmd)); 140 cmd->hdr.cmd = PVRDMA_CMD_QUERY_PORT; 141 cmd->port_num = port; 142 143 err = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_PORT_RESP); 144 if (err < 0) { 145 dev_warn(&dev->pdev->dev, 146 "could not query port, error: %d\n", err); 147 return err; 148 } 149 150 /* props being zeroed by the caller, avoid zeroing it here */ 151 152 props->state = pvrdma_port_state_to_ib(resp->attrs.state); 153 props->max_mtu = pvrdma_mtu_to_ib(resp->attrs.max_mtu); 154 props->active_mtu = pvrdma_mtu_to_ib(resp->attrs.active_mtu); 155 props->gid_tbl_len = resp->attrs.gid_tbl_len; 156 props->port_cap_flags = 157 pvrdma_port_cap_flags_to_ib(resp->attrs.port_cap_flags); 158 props->port_cap_flags |= IB_PORT_CM_SUP; 159 props->ip_gids = true; 160 props->max_msg_sz = resp->attrs.max_msg_sz; 161 props->bad_pkey_cntr = resp->attrs.bad_pkey_cntr; 162 props->qkey_viol_cntr = resp->attrs.qkey_viol_cntr; 163 props->pkey_tbl_len = resp->attrs.pkey_tbl_len; 164 props->lid = resp->attrs.lid; 165 props->sm_lid = resp->attrs.sm_lid; 166 props->lmc = resp->attrs.lmc; 167 props->max_vl_num = resp->attrs.max_vl_num; 168 props->sm_sl = resp->attrs.sm_sl; 169 props->subnet_timeout = resp->attrs.subnet_timeout; 170 props->init_type_reply = resp->attrs.init_type_reply; 171 props->active_width = pvrdma_port_width_to_ib(resp->attrs.active_width); 172 props->active_speed = pvrdma_port_speed_to_ib(resp->attrs.active_speed); 173 props->phys_state = resp->attrs.phys_state; 174 175 return 0; 176 } 177 178 /** 179 * pvrdma_query_gid - query device gid 180 * @ibdev: the device to query 181 * @port: the port number 182 * @index: the index 183 * @gid: the device gid value 184 * 185 * @return: 0 on success, otherwise negative errno 186 */ 187 int pvrdma_query_gid(struct ib_device *ibdev, u8 port, int index, 188 union ib_gid *gid) 189 { 190 struct pvrdma_dev *dev = to_vdev(ibdev); 191 192 if (index >= dev->dsr->caps.gid_tbl_len) 193 return -EINVAL; 194 195 memcpy(gid, &dev->sgid_tbl[index], sizeof(union ib_gid)); 196 197 return 0; 198 } 199 200 /** 201 * pvrdma_query_pkey - query device port's P_Key table 202 * @ibdev: the device to query 203 * @port: the port number 204 * @index: the index 205 * @pkey: the device P_Key value 206 * 207 * @return: 0 on success, otherwise negative errno 208 */ 209 int pvrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 210 u16 *pkey) 211 { 212 int err = 0; 213 union pvrdma_cmd_req req; 214 union pvrdma_cmd_resp rsp; 215 struct pvrdma_cmd_query_pkey *cmd = &req.query_pkey; 216 217 memset(cmd, 0, sizeof(*cmd)); 218 cmd->hdr.cmd = PVRDMA_CMD_QUERY_PKEY; 219 cmd->port_num = port; 220 cmd->index = index; 221 222 err = pvrdma_cmd_post(to_vdev(ibdev), &req, &rsp, 223 PVRDMA_CMD_QUERY_PKEY_RESP); 224 if (err < 0) { 225 dev_warn(&to_vdev(ibdev)->pdev->dev, 226 "could not query pkey, error: %d\n", err); 227 return err; 228 } 229 230 *pkey = rsp.query_pkey_resp.pkey; 231 232 return 0; 233 } 234 235 enum rdma_link_layer pvrdma_port_link_layer(struct ib_device *ibdev, 236 u8 port) 237 { 238 return IB_LINK_LAYER_ETHERNET; 239 } 240 241 int pvrdma_modify_device(struct ib_device *ibdev, int mask, 242 struct ib_device_modify *props) 243 { 244 unsigned long flags; 245 246 if (mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID | 247 IB_DEVICE_MODIFY_NODE_DESC)) { 248 dev_warn(&to_vdev(ibdev)->pdev->dev, 249 "unsupported device modify mask %#x\n", mask); 250 return -EOPNOTSUPP; 251 } 252 253 if (mask & IB_DEVICE_MODIFY_NODE_DESC) { 254 spin_lock_irqsave(&to_vdev(ibdev)->desc_lock, flags); 255 memcpy(ibdev->node_desc, props->node_desc, 64); 256 spin_unlock_irqrestore(&to_vdev(ibdev)->desc_lock, flags); 257 } 258 259 if (mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) { 260 mutex_lock(&to_vdev(ibdev)->port_mutex); 261 to_vdev(ibdev)->sys_image_guid = 262 cpu_to_be64(props->sys_image_guid); 263 mutex_unlock(&to_vdev(ibdev)->port_mutex); 264 } 265 266 return 0; 267 } 268 269 /** 270 * pvrdma_modify_port - modify device port attributes 271 * @ibdev: the device to modify 272 * @port: the port number 273 * @mask: attributes to modify 274 * @props: the device properties 275 * 276 * @return: 0 on success, otherwise negative errno 277 */ 278 int pvrdma_modify_port(struct ib_device *ibdev, u8 port, int mask, 279 struct ib_port_modify *props) 280 { 281 struct ib_port_attr attr; 282 struct pvrdma_dev *vdev = to_vdev(ibdev); 283 int ret; 284 285 if (mask & ~IB_PORT_SHUTDOWN) { 286 dev_warn(&vdev->pdev->dev, 287 "unsupported port modify mask %#x\n", mask); 288 return -EOPNOTSUPP; 289 } 290 291 mutex_lock(&vdev->port_mutex); 292 ret = ib_query_port(ibdev, port, &attr); 293 if (ret) 294 goto out; 295 296 vdev->port_cap_mask |= props->set_port_cap_mask; 297 vdev->port_cap_mask &= ~props->clr_port_cap_mask; 298 299 if (mask & IB_PORT_SHUTDOWN) 300 vdev->ib_active = false; 301 302 out: 303 mutex_unlock(&vdev->port_mutex); 304 return ret; 305 } 306 307 /** 308 * pvrdma_alloc_ucontext - allocate ucontext 309 * @uctx: the uverbs countext 310 * @udata: user data 311 * 312 * @return: zero on success, otherwise errno. 313 */ 314 int pvrdma_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata) 315 { 316 struct ib_device *ibdev = uctx->device; 317 struct pvrdma_dev *vdev = to_vdev(ibdev); 318 struct pvrdma_ucontext *context = to_vucontext(uctx); 319 union pvrdma_cmd_req req = {}; 320 union pvrdma_cmd_resp rsp = {}; 321 struct pvrdma_cmd_create_uc *cmd = &req.create_uc; 322 struct pvrdma_cmd_create_uc_resp *resp = &rsp.create_uc_resp; 323 struct pvrdma_alloc_ucontext_resp uresp = {}; 324 int ret; 325 326 if (!vdev->ib_active) 327 return -EAGAIN; 328 329 context->dev = vdev; 330 ret = pvrdma_uar_alloc(vdev, &context->uar); 331 if (ret) 332 return -ENOMEM; 333 334 /* get ctx_handle from host */ 335 if (vdev->dsr_version < PVRDMA_PPN64_VERSION) 336 cmd->pfn = context->uar.pfn; 337 else 338 cmd->pfn64 = context->uar.pfn; 339 340 cmd->hdr.cmd = PVRDMA_CMD_CREATE_UC; 341 ret = pvrdma_cmd_post(vdev, &req, &rsp, PVRDMA_CMD_CREATE_UC_RESP); 342 if (ret < 0) { 343 dev_warn(&vdev->pdev->dev, 344 "could not create ucontext, error: %d\n", ret); 345 goto err; 346 } 347 348 context->ctx_handle = resp->ctx_handle; 349 350 /* copy back to user */ 351 uresp.qp_tab_size = vdev->dsr->caps.max_qp; 352 ret = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); 353 if (ret) { 354 pvrdma_uar_free(vdev, &context->uar); 355 pvrdma_dealloc_ucontext(&context->ibucontext); 356 return -EFAULT; 357 } 358 359 return 0; 360 361 err: 362 pvrdma_uar_free(vdev, &context->uar); 363 return ret; 364 } 365 366 /** 367 * pvrdma_dealloc_ucontext - deallocate ucontext 368 * @ibcontext: the ucontext 369 */ 370 void pvrdma_dealloc_ucontext(struct ib_ucontext *ibcontext) 371 { 372 struct pvrdma_ucontext *context = to_vucontext(ibcontext); 373 union pvrdma_cmd_req req = {}; 374 struct pvrdma_cmd_destroy_uc *cmd = &req.destroy_uc; 375 int ret; 376 377 cmd->hdr.cmd = PVRDMA_CMD_DESTROY_UC; 378 cmd->ctx_handle = context->ctx_handle; 379 380 ret = pvrdma_cmd_post(context->dev, &req, NULL, 0); 381 if (ret < 0) 382 dev_warn(&context->dev->pdev->dev, 383 "destroy ucontext failed, error: %d\n", ret); 384 385 /* Free the UAR even if the device command failed */ 386 pvrdma_uar_free(to_vdev(ibcontext->device), &context->uar); 387 } 388 389 /** 390 * pvrdma_mmap - create mmap region 391 * @ibcontext: the user context 392 * @vma: the VMA 393 * 394 * @return: 0 on success, otherwise errno. 395 */ 396 int pvrdma_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 397 { 398 struct pvrdma_ucontext *context = to_vucontext(ibcontext); 399 unsigned long start = vma->vm_start; 400 unsigned long size = vma->vm_end - vma->vm_start; 401 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; 402 403 dev_dbg(&context->dev->pdev->dev, "create mmap region\n"); 404 405 if ((size != PAGE_SIZE) || (offset & ~PAGE_MASK)) { 406 dev_warn(&context->dev->pdev->dev, 407 "invalid params for mmap region\n"); 408 return -EINVAL; 409 } 410 411 /* Map UAR to kernel space, VM_LOCKED? */ 412 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND; 413 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 414 if (io_remap_pfn_range(vma, start, context->uar.pfn, size, 415 vma->vm_page_prot)) 416 return -EAGAIN; 417 418 return 0; 419 } 420 421 /** 422 * pvrdma_alloc_pd - allocate protection domain 423 * @ibpd: PD pointer 424 * @context: user context 425 * @udata: user data 426 * 427 * @return: the ib_pd protection domain pointer on success, otherwise errno. 428 */ 429 int pvrdma_alloc_pd(struct ib_pd *ibpd, struct ib_ucontext *context, 430 struct ib_udata *udata) 431 { 432 struct ib_device *ibdev = ibpd->device; 433 struct pvrdma_pd *pd = to_vpd(ibpd); 434 struct pvrdma_dev *dev = to_vdev(ibdev); 435 union pvrdma_cmd_req req = {}; 436 union pvrdma_cmd_resp rsp = {}; 437 struct pvrdma_cmd_create_pd *cmd = &req.create_pd; 438 struct pvrdma_cmd_create_pd_resp *resp = &rsp.create_pd_resp; 439 struct pvrdma_alloc_pd_resp pd_resp = {0}; 440 int ret; 441 442 /* Check allowed max pds */ 443 if (!atomic_add_unless(&dev->num_pds, 1, dev->dsr->caps.max_pd)) 444 return -ENOMEM; 445 446 cmd->hdr.cmd = PVRDMA_CMD_CREATE_PD; 447 cmd->ctx_handle = (context) ? to_vucontext(context)->ctx_handle : 0; 448 ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_PD_RESP); 449 if (ret < 0) { 450 dev_warn(&dev->pdev->dev, 451 "failed to allocate protection domain, error: %d\n", 452 ret); 453 goto err; 454 } 455 456 pd->privileged = !context; 457 pd->pd_handle = resp->pd_handle; 458 pd->pdn = resp->pd_handle; 459 pd_resp.pdn = resp->pd_handle; 460 461 if (context) { 462 if (ib_copy_to_udata(udata, &pd_resp, sizeof(pd_resp))) { 463 dev_warn(&dev->pdev->dev, 464 "failed to copy back protection domain\n"); 465 pvrdma_dealloc_pd(&pd->ibpd); 466 return -EFAULT; 467 } 468 } 469 470 /* u32 pd handle */ 471 return 0; 472 473 err: 474 atomic_dec(&dev->num_pds); 475 return ret; 476 } 477 478 /** 479 * pvrdma_dealloc_pd - deallocate protection domain 480 * @pd: the protection domain to be released 481 * 482 * @return: 0 on success, otherwise errno. 483 */ 484 void pvrdma_dealloc_pd(struct ib_pd *pd) 485 { 486 struct pvrdma_dev *dev = to_vdev(pd->device); 487 union pvrdma_cmd_req req = {}; 488 struct pvrdma_cmd_destroy_pd *cmd = &req.destroy_pd; 489 int ret; 490 491 cmd->hdr.cmd = PVRDMA_CMD_DESTROY_PD; 492 cmd->pd_handle = to_vpd(pd)->pd_handle; 493 494 ret = pvrdma_cmd_post(dev, &req, NULL, 0); 495 if (ret) 496 dev_warn(&dev->pdev->dev, 497 "could not dealloc protection domain, error: %d\n", 498 ret); 499 500 atomic_dec(&dev->num_pds); 501 } 502 503 /** 504 * pvrdma_create_ah - create an address handle 505 * @pd: the protection domain 506 * @ah_attr: the attributes of the AH 507 * @udata: user data blob 508 * @flags: create address handle flags (see enum rdma_create_ah_flags) 509 * 510 * @return: the ib_ah pointer on success, otherwise errno. 511 */ 512 struct ib_ah *pvrdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr, 513 u32 flags, struct ib_udata *udata) 514 { 515 struct pvrdma_dev *dev = to_vdev(pd->device); 516 struct pvrdma_ah *ah; 517 const struct ib_global_route *grh; 518 u8 port_num = rdma_ah_get_port_num(ah_attr); 519 520 if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) 521 return ERR_PTR(-EINVAL); 522 523 grh = rdma_ah_read_grh(ah_attr); 524 if ((ah_attr->type != RDMA_AH_ATTR_TYPE_ROCE) || 525 rdma_is_multicast_addr((struct in6_addr *)grh->dgid.raw)) 526 return ERR_PTR(-EINVAL); 527 528 if (!atomic_add_unless(&dev->num_ahs, 1, dev->dsr->caps.max_ah)) 529 return ERR_PTR(-ENOMEM); 530 531 ah = kzalloc(sizeof(*ah), GFP_ATOMIC); 532 if (!ah) { 533 atomic_dec(&dev->num_ahs); 534 return ERR_PTR(-ENOMEM); 535 } 536 537 ah->av.port_pd = to_vpd(pd)->pd_handle | (port_num << 24); 538 ah->av.src_path_bits = rdma_ah_get_path_bits(ah_attr); 539 ah->av.src_path_bits |= 0x80; 540 ah->av.gid_index = grh->sgid_index; 541 ah->av.hop_limit = grh->hop_limit; 542 ah->av.sl_tclass_flowlabel = (grh->traffic_class << 20) | 543 grh->flow_label; 544 memcpy(ah->av.dgid, grh->dgid.raw, 16); 545 memcpy(ah->av.dmac, ah_attr->roce.dmac, ETH_ALEN); 546 547 ah->ibah.device = pd->device; 548 ah->ibah.pd = pd; 549 ah->ibah.uobject = NULL; 550 551 return &ah->ibah; 552 } 553 554 /** 555 * pvrdma_destroy_ah - destroy an address handle 556 * @ah: the address handle to destroyed 557 * @flags: destroy address handle flags (see enum rdma_destroy_ah_flags) 558 * 559 * @return: 0 on success. 560 */ 561 int pvrdma_destroy_ah(struct ib_ah *ah, u32 flags) 562 { 563 struct pvrdma_dev *dev = to_vdev(ah->device); 564 565 kfree(to_vah(ah)); 566 atomic_dec(&dev->num_ahs); 567 568 return 0; 569 } 570