1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2022, Microsoft Corporation. All rights reserved. 4 */ 5 6 #include "mana_ib.h" 7 #include "linux/pci.h" 8 9 void mana_ib_uncfg_vport(struct mana_ib_dev *dev, struct mana_ib_pd *pd, 10 u32 port) 11 { 12 struct mana_port_context *mpc; 13 struct net_device *ndev; 14 15 ndev = mana_ib_get_netdev(&dev->ib_dev, port); 16 mpc = netdev_priv(ndev); 17 18 mutex_lock(&pd->vport_mutex); 19 20 pd->vport_use_count--; 21 WARN_ON(pd->vport_use_count < 0); 22 23 if (!pd->vport_use_count) { 24 mana_destroy_eq(mpc); 25 mana_uncfg_vport(mpc); 26 } 27 28 mutex_unlock(&pd->vport_mutex); 29 } 30 31 int mana_ib_cfg_vport(struct mana_ib_dev *dev, u32 port, struct mana_ib_pd *pd, 32 u32 doorbell_id) 33 { 34 struct mana_port_context *mpc; 35 struct net_device *ndev; 36 int err; 37 38 ndev = mana_ib_get_netdev(&dev->ib_dev, port); 39 mpc = netdev_priv(ndev); 40 41 mutex_lock(&pd->vport_mutex); 42 43 pd->vport_use_count++; 44 if (pd->vport_use_count > 1) { 45 /* Reject cross-port PD sharing. EQs and vport config 46 * are per-port, so the PD must stay bound to the port 47 * that was configured on the first raw QP creation. 48 */ 49 if (pd->vport_port != port) { 50 pd->vport_use_count--; 51 mutex_unlock(&pd->vport_mutex); 52 ibdev_dbg(&dev->ib_dev, 53 "PD already bound to port %u\n", 54 pd->vport_port); 55 return -EINVAL; 56 } 57 ibdev_dbg(&dev->ib_dev, 58 "Skip as this PD is already configured vport\n"); 59 mutex_unlock(&pd->vport_mutex); 60 return 0; 61 } 62 63 pd->vport_port = port; 64 65 err = mana_cfg_vport(mpc, pd->pdn, doorbell_id, true); 66 if (err) { 67 pd->vport_use_count--; 68 mutex_unlock(&pd->vport_mutex); 69 70 ibdev_dbg(&dev->ib_dev, "Failed to configure vPort %d\n", err); 71 return err; 72 } 73 74 75 err = mana_create_eq(mpc); 76 if (err) { 77 mana_uncfg_vport(mpc); 78 pd->vport_use_count--; 79 } else { 80 pd->tx_shortform_allowed = mpc->tx_shortform_allowed; 81 pd->tx_vp_offset = mpc->tx_vp_offset; 82 } 83 84 mutex_unlock(&pd->vport_mutex); 85 86 if (!err) 87 ibdev_dbg(&dev->ib_dev, "vport handle %llx pdid %x doorbell_id %x\n", 88 mpc->port_handle, pd->pdn, doorbell_id); 89 90 return err; 91 } 92 93 int mana_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 94 { 95 struct mana_ib_pd *pd = container_of(ibpd, struct mana_ib_pd, ibpd); 96 struct ib_device *ibdev = ibpd->device; 97 struct gdma_create_pd_resp resp = {}; 98 struct gdma_create_pd_req req = {}; 99 enum gdma_pd_flags flags = 0; 100 struct mana_ib_dev *dev; 101 struct gdma_context *gc; 102 int err; 103 104 dev = container_of(ibdev, struct mana_ib_dev, ib_dev); 105 gc = mdev_to_gc(dev); 106 107 mana_gd_init_req_hdr(&req.hdr, GDMA_CREATE_PD, sizeof(req), 108 sizeof(resp)); 109 110 if (!udata) 111 flags |= GDMA_PD_FLAG_ALLOW_GPA_MR; 112 113 req.flags = flags; 114 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 115 if (err) 116 return err; 117 118 pd->pd_handle = resp.pd_handle; 119 pd->pdn = resp.pd_id; 120 ibdev_dbg(&dev->ib_dev, "pd_handle 0x%llx pd_id %d\n", 121 pd->pd_handle, pd->pdn); 122 123 mutex_init(&pd->vport_mutex); 124 pd->vport_use_count = 0; 125 return 0; 126 } 127 128 int mana_ib_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 129 { 130 struct mana_ib_pd *pd = container_of(ibpd, struct mana_ib_pd, ibpd); 131 struct ib_device *ibdev = ibpd->device; 132 struct gdma_destory_pd_resp resp = {}; 133 struct gdma_destroy_pd_req req = {}; 134 struct mana_ib_dev *dev; 135 struct gdma_context *gc; 136 137 dev = container_of(ibdev, struct mana_ib_dev, ib_dev); 138 gc = mdev_to_gc(dev); 139 140 mana_gd_init_req_hdr(&req.hdr, GDMA_DESTROY_PD, sizeof(req), 141 sizeof(resp)); 142 143 req.pd_handle = pd->pd_handle; 144 145 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 146 } 147 148 static int mana_gd_destroy_doorbell_page(struct gdma_context *gc, 149 int doorbell_page) 150 { 151 struct gdma_destroy_resource_range_req req = {}; 152 struct gdma_resp_hdr resp = {}; 153 154 mana_gd_init_req_hdr(&req.hdr, GDMA_DESTROY_RESOURCE_RANGE, 155 sizeof(req), sizeof(resp)); 156 157 req.resource_type = GDMA_RESOURCE_DOORBELL_PAGE; 158 req.num_resources = 1; 159 req.allocated_resources = doorbell_page; 160 161 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 162 } 163 164 static int mana_gd_allocate_doorbell_page(struct gdma_context *gc, 165 int *doorbell_page) 166 { 167 struct gdma_allocate_resource_range_req req = {}; 168 struct gdma_allocate_resource_range_resp resp = {}; 169 int err; 170 171 mana_gd_init_req_hdr(&req.hdr, GDMA_ALLOCATE_RESOURCE_RANGE, 172 sizeof(req), sizeof(resp)); 173 174 req.resource_type = GDMA_RESOURCE_DOORBELL_PAGE; 175 req.num_resources = 1; 176 req.alignment = PAGE_SIZE / MANA_PAGE_SIZE; 177 178 /* Have GDMA start searching from 0 */ 179 req.allocated_resources = 0; 180 181 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 182 if (err) 183 return err; 184 185 *doorbell_page = resp.allocated_resources; 186 187 return 0; 188 } 189 190 int mana_ib_alloc_ucontext(struct ib_ucontext *ibcontext, 191 struct ib_udata *udata) 192 { 193 struct mana_ib_ucontext *ucontext = 194 container_of(ibcontext, struct mana_ib_ucontext, ibucontext); 195 struct ib_device *ibdev = ibcontext->device; 196 struct mana_ib_dev *mdev; 197 struct gdma_context *gc; 198 int doorbell_page; 199 int ret; 200 201 mdev = container_of(ibdev, struct mana_ib_dev, ib_dev); 202 gc = mdev_to_gc(mdev); 203 204 /* Allocate a doorbell page index */ 205 ret = mana_gd_allocate_doorbell_page(gc, &doorbell_page); 206 if (ret) { 207 ibdev_dbg(ibdev, "Failed to allocate doorbell page %d\n", ret); 208 return ret; 209 } 210 211 ibdev_dbg(ibdev, "Doorbell page allocated %d\n", doorbell_page); 212 213 ucontext->doorbell = doorbell_page; 214 215 return 0; 216 } 217 218 void mana_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 219 { 220 struct mana_ib_ucontext *mana_ucontext = 221 container_of(ibcontext, struct mana_ib_ucontext, ibucontext); 222 struct ib_device *ibdev = ibcontext->device; 223 struct mana_ib_dev *mdev; 224 struct gdma_context *gc; 225 int ret; 226 227 mdev = container_of(ibdev, struct mana_ib_dev, ib_dev); 228 gc = mdev_to_gc(mdev); 229 230 ret = mana_gd_destroy_doorbell_page(gc, mana_ucontext->doorbell); 231 if (ret) 232 ibdev_dbg(ibdev, "Failed to destroy doorbell page %d\n", ret); 233 } 234 235 int mana_ib_create_kernel_queue(struct mana_ib_dev *mdev, u32 size, enum gdma_queue_type type, 236 struct mana_ib_queue *queue) 237 { 238 struct gdma_queue_spec spec = {}; 239 int err; 240 241 queue->id = INVALID_QUEUE_ID; 242 queue->gdma_region = GDMA_INVALID_DMA_REGION; 243 spec.type = type; 244 spec.monitor_avl_buf = false; 245 spec.queue_size = size; 246 err = mana_gd_create_mana_wq_cq(mdev->gdma_dev, &spec, &queue->kmem); 247 if (err) 248 return err; 249 /* take ownership into mana_ib from mana */ 250 queue->gdma_region = queue->kmem->mem_info.dma_region_handle; 251 queue->kmem->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 252 return 0; 253 } 254 255 int mana_ib_create_queue(struct mana_ib_dev *mdev, u64 addr, u32 size, 256 struct mana_ib_queue *queue) 257 { 258 struct ib_umem *umem; 259 int err; 260 261 queue->umem = NULL; 262 queue->id = INVALID_QUEUE_ID; 263 queue->gdma_region = GDMA_INVALID_DMA_REGION; 264 265 umem = ib_umem_get_va(&mdev->ib_dev, addr, size, IB_ACCESS_LOCAL_WRITE); 266 if (IS_ERR(umem)) { 267 ibdev_dbg(&mdev->ib_dev, "Failed to get umem, %pe\n", umem); 268 return PTR_ERR(umem); 269 } 270 271 err = mana_ib_create_zero_offset_dma_region(mdev, umem, &queue->gdma_region); 272 if (err) { 273 ibdev_dbg(&mdev->ib_dev, "Failed to create dma region, %d\n", err); 274 goto free_umem; 275 } 276 queue->umem = umem; 277 278 ibdev_dbg(&mdev->ib_dev, "created dma region 0x%llx\n", queue->gdma_region); 279 280 return 0; 281 free_umem: 282 ib_umem_release(umem); 283 return err; 284 } 285 286 void mana_ib_destroy_queue(struct mana_ib_dev *mdev, struct mana_ib_queue *queue) 287 { 288 /* Ignore return code as there is not much we can do about it. 289 * The error message is printed inside. 290 */ 291 mana_ib_gd_destroy_dma_region(mdev, queue->gdma_region); 292 ib_umem_release(queue->umem); 293 if (queue->kmem) 294 mana_gd_destroy_queue(mdev_to_gc(mdev), queue->kmem); 295 } 296 297 static int 298 mana_ib_gd_first_dma_region(struct mana_ib_dev *dev, 299 struct gdma_context *gc, 300 struct gdma_create_dma_region_req *create_req, 301 size_t num_pages, mana_handle_t *gdma_region, 302 u32 expected_status) 303 { 304 struct gdma_create_dma_region_resp create_resp = {}; 305 unsigned int create_req_msg_size; 306 int err; 307 308 create_req_msg_size = 309 struct_size(create_req, page_addr_list, num_pages); 310 create_req->page_addr_list_len = num_pages; 311 312 err = mana_gd_send_request(gc, create_req_msg_size, create_req, 313 sizeof(create_resp), &create_resp); 314 if (err || create_resp.hdr.status != expected_status) { 315 ibdev_dbg(&dev->ib_dev, 316 "Failed to create DMA region: %d, 0x%x\n", 317 err, create_resp.hdr.status); 318 if (!err) 319 err = -EPROTO; 320 321 return err; 322 } 323 324 *gdma_region = create_resp.dma_region_handle; 325 ibdev_dbg(&dev->ib_dev, "Created DMA region handle 0x%llx\n", 326 *gdma_region); 327 328 return 0; 329 } 330 331 static int 332 mana_ib_gd_add_dma_region(struct mana_ib_dev *dev, struct gdma_context *gc, 333 struct gdma_dma_region_add_pages_req *add_req, 334 unsigned int num_pages, u32 expected_status) 335 { 336 unsigned int add_req_msg_size = 337 struct_size(add_req, page_addr_list, num_pages); 338 struct gdma_general_resp add_resp = {}; 339 int err; 340 341 mana_gd_init_req_hdr(&add_req->hdr, GDMA_DMA_REGION_ADD_PAGES, 342 add_req_msg_size, sizeof(add_resp)); 343 add_req->page_addr_list_len = num_pages; 344 345 err = mana_gd_send_request(gc, add_req_msg_size, add_req, 346 sizeof(add_resp), &add_resp); 347 if (err || add_resp.hdr.status != expected_status) { 348 ibdev_dbg(&dev->ib_dev, 349 "Failed to create DMA region: %d, 0x%x\n", 350 err, add_resp.hdr.status); 351 352 if (!err) 353 err = -EPROTO; 354 355 return err; 356 } 357 358 return 0; 359 } 360 361 static int mana_ib_gd_create_dma_region(struct mana_ib_dev *dev, struct ib_umem *umem, 362 mana_handle_t *gdma_region, unsigned long page_sz) 363 { 364 struct gdma_dma_region_add_pages_req *add_req = NULL; 365 size_t num_pages_processed = 0, num_pages_to_handle; 366 struct gdma_create_dma_region_req *create_req; 367 unsigned int create_req_msg_size; 368 struct hw_channel_context *hwc; 369 struct ib_block_iter biter; 370 size_t max_pgs_add_cmd = 0; 371 size_t max_pgs_create_cmd; 372 struct gdma_context *gc; 373 size_t num_pages_total; 374 unsigned int tail = 0; 375 u64 *page_addr_list; 376 void *request_buf; 377 int err = 0; 378 379 gc = mdev_to_gc(dev); 380 hwc = gc->hwc.driver_data; 381 382 num_pages_total = ib_umem_num_dma_blocks(umem, page_sz); 383 384 max_pgs_create_cmd = 385 (hwc->max_req_msg_size - sizeof(*create_req)) / sizeof(u64); 386 num_pages_to_handle = 387 min_t(size_t, num_pages_total, max_pgs_create_cmd); 388 create_req_msg_size = 389 struct_size(create_req, page_addr_list, num_pages_to_handle); 390 391 request_buf = kzalloc(hwc->max_req_msg_size, GFP_KERNEL); 392 if (!request_buf) 393 return -ENOMEM; 394 395 create_req = request_buf; 396 mana_gd_init_req_hdr(&create_req->hdr, GDMA_CREATE_DMA_REGION, 397 create_req_msg_size, 398 sizeof(struct gdma_create_dma_region_resp)); 399 400 create_req->length = umem->length; 401 create_req->offset_in_page = ib_umem_dma_offset(umem, page_sz); 402 create_req->gdma_page_type = order_base_2(page_sz) - MANA_PAGE_SHIFT; 403 create_req->page_count = num_pages_total; 404 405 ibdev_dbg(&dev->ib_dev, "size_dma_region %lu num_pages_total %lu\n", 406 umem->length, num_pages_total); 407 408 ibdev_dbg(&dev->ib_dev, "page_sz %lu offset_in_page %u\n", 409 page_sz, create_req->offset_in_page); 410 411 ibdev_dbg(&dev->ib_dev, "num_pages_to_handle %lu, gdma_page_type %u", 412 num_pages_to_handle, create_req->gdma_page_type); 413 414 page_addr_list = create_req->page_addr_list; 415 rdma_umem_for_each_dma_block(umem, &biter, page_sz) { 416 u32 expected_status = 0; 417 418 page_addr_list[tail++] = rdma_block_iter_dma_address(&biter); 419 if (tail < num_pages_to_handle) 420 continue; 421 422 if (num_pages_processed + num_pages_to_handle < 423 num_pages_total) 424 expected_status = GDMA_STATUS_MORE_ENTRIES; 425 426 if (!num_pages_processed) { 427 /* First create message */ 428 err = mana_ib_gd_first_dma_region(dev, gc, create_req, 429 tail, gdma_region, 430 expected_status); 431 if (err) 432 goto out; 433 434 max_pgs_add_cmd = (hwc->max_req_msg_size - 435 sizeof(*add_req)) / sizeof(u64); 436 437 add_req = request_buf; 438 add_req->dma_region_handle = *gdma_region; 439 add_req->reserved3 = 0; 440 page_addr_list = add_req->page_addr_list; 441 } else { 442 /* Subsequent create messages */ 443 err = mana_ib_gd_add_dma_region(dev, gc, add_req, tail, 444 expected_status); 445 if (err) 446 break; 447 } 448 449 num_pages_processed += tail; 450 tail = 0; 451 452 /* The remaining pages to create */ 453 num_pages_to_handle = 454 min_t(size_t, 455 num_pages_total - num_pages_processed, 456 max_pgs_add_cmd); 457 } 458 459 if (err) 460 mana_ib_gd_destroy_dma_region(dev, *gdma_region); 461 462 out: 463 kfree(request_buf); 464 return err; 465 } 466 467 int mana_ib_create_dma_region(struct mana_ib_dev *dev, struct ib_umem *umem, 468 mana_handle_t *gdma_region, u64 virt) 469 { 470 unsigned long page_sz; 471 472 page_sz = ib_umem_find_best_pgsz(umem, dev->adapter_caps.page_size_cap, virt); 473 if (!page_sz) { 474 ibdev_dbg(&dev->ib_dev, "Failed to find page size.\n"); 475 return -EINVAL; 476 } 477 478 return mana_ib_gd_create_dma_region(dev, umem, gdma_region, page_sz); 479 } 480 481 int mana_ib_create_zero_offset_dma_region(struct mana_ib_dev *dev, struct ib_umem *umem, 482 mana_handle_t *gdma_region) 483 { 484 unsigned long page_sz; 485 486 /* Hardware requires dma region to align to chosen page size */ 487 page_sz = ib_umem_find_best_pgoff(umem, dev->adapter_caps.page_size_cap, 0); 488 if (!page_sz) { 489 ibdev_dbg(&dev->ib_dev, "Failed to find page size.\n"); 490 return -EINVAL; 491 } 492 493 return mana_ib_gd_create_dma_region(dev, umem, gdma_region, page_sz); 494 } 495 496 int mana_ib_gd_destroy_dma_region(struct mana_ib_dev *dev, u64 gdma_region) 497 { 498 struct gdma_context *gc = mdev_to_gc(dev); 499 500 ibdev_dbg(&dev->ib_dev, "destroy dma region 0x%llx\n", gdma_region); 501 502 return mana_gd_destroy_dma_region(gc, gdma_region); 503 } 504 505 int mana_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 506 { 507 struct mana_ib_ucontext *mana_ucontext = 508 container_of(ibcontext, struct mana_ib_ucontext, ibucontext); 509 struct ib_device *ibdev = ibcontext->device; 510 struct mana_ib_dev *mdev; 511 struct gdma_context *gc; 512 phys_addr_t pfn; 513 pgprot_t prot; 514 int ret; 515 516 mdev = container_of(ibdev, struct mana_ib_dev, ib_dev); 517 gc = mdev_to_gc(mdev); 518 519 if (vma->vm_pgoff != 0) { 520 ibdev_dbg(ibdev, "Unexpected vm_pgoff %lu\n", vma->vm_pgoff); 521 return -EINVAL; 522 } 523 524 /* Map to the page indexed by ucontext->doorbell */ 525 pfn = (gc->phys_db_page_base + 526 gc->db_page_size * mana_ucontext->doorbell) >> 527 PAGE_SHIFT; 528 prot = pgprot_writecombine(vma->vm_page_prot); 529 530 ret = rdma_user_mmap_io(ibcontext, vma, pfn, PAGE_SIZE, prot, 531 NULL); 532 if (ret) 533 ibdev_dbg(ibdev, "can't rdma_user_mmap_io ret %d\n", ret); 534 else 535 ibdev_dbg(ibdev, "mapped I/O pfn 0x%llx page_size %lu, ret %d\n", 536 pfn, PAGE_SIZE, ret); 537 538 return ret; 539 } 540 541 int mana_ib_get_port_immutable(struct ib_device *ibdev, u32 port_num, 542 struct ib_port_immutable *immutable) 543 { 544 struct mana_ib_dev *dev = container_of(ibdev, struct mana_ib_dev, ib_dev); 545 struct ib_port_attr attr; 546 int err; 547 548 err = ib_query_port(ibdev, port_num, &attr); 549 if (err) 550 return err; 551 552 immutable->pkey_tbl_len = attr.pkey_tbl_len; 553 immutable->gid_tbl_len = attr.gid_tbl_len; 554 555 if (mana_ib_is_rnic(dev)) { 556 if (port_num == 1) { 557 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 558 immutable->max_mad_size = IB_MGMT_MAD_SIZE; 559 } else { 560 immutable->core_cap_flags = RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP 561 | RDMA_CORE_CAP_ETH_AH; 562 immutable->max_mad_size = 0; 563 } 564 } else { 565 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 566 } 567 568 return 0; 569 } 570 571 int mana_ib_query_device(struct ib_device *ibdev, struct ib_device_attr *props, 572 struct ib_udata *uhw) 573 { 574 struct mana_ib_dev *dev = container_of(ibdev, struct mana_ib_dev, ib_dev); 575 struct pci_dev *pdev = to_pci_dev(mdev_to_gc(dev)->dev); 576 int err; 577 578 err = ib_is_udata_in_empty(uhw); 579 if (err) 580 return err; 581 582 memset(props, 0, sizeof(*props)); 583 props->vendor_id = pdev->vendor; 584 props->vendor_part_id = dev->gdma_dev->dev_id.type; 585 props->max_mr_size = MANA_IB_MAX_MR_SIZE; 586 props->page_size_cap = dev->adapter_caps.page_size_cap; 587 props->max_qp = dev->adapter_caps.max_qp_count; 588 props->max_qp_wr = dev->adapter_caps.max_qp_wr; 589 props->device_cap_flags = IB_DEVICE_RC_RNR_NAK_GEN; 590 props->max_send_sge = dev->adapter_caps.max_send_sge_count; 591 props->max_recv_sge = dev->adapter_caps.max_recv_sge_count; 592 props->max_sge_rd = dev->adapter_caps.max_recv_sge_count; 593 props->max_cq = dev->adapter_caps.max_cq_count; 594 props->max_cqe = dev->adapter_caps.max_qp_wr; 595 props->max_mr = dev->adapter_caps.max_mr_count; 596 props->max_pd = dev->adapter_caps.max_pd_count; 597 props->max_qp_rd_atom = dev->adapter_caps.max_inbound_read_limit; 598 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 599 props->max_qp_init_rd_atom = dev->adapter_caps.max_outbound_read_limit; 600 props->atomic_cap = IB_ATOMIC_NONE; 601 props->masked_atomic_cap = IB_ATOMIC_NONE; 602 props->max_ah = INT_MAX; 603 props->max_pkeys = 1; 604 props->local_ca_ack_delay = MANA_CA_ACK_DELAY; 605 if (!mana_ib_is_rnic(dev)) 606 props->raw_packet_caps = IB_RAW_PACKET_CAP_IP_CSUM; 607 608 return ib_respond_empty_udata(uhw); 609 } 610 611 int mana_ib_query_port(struct ib_device *ibdev, u32 port, 612 struct ib_port_attr *props) 613 { 614 struct mana_ib_dev *dev = container_of(ibdev, struct mana_ib_dev, ib_dev); 615 struct net_device *ndev = mana_ib_get_netdev(ibdev, port); 616 617 if (!ndev) 618 return -EINVAL; 619 620 memset(props, 0, sizeof(*props)); 621 props->max_mtu = IB_MTU_4096; 622 props->active_mtu = ib_mtu_int_to_enum(ndev->mtu); 623 624 if (netif_carrier_ok(ndev) && netif_running(ndev)) { 625 props->state = IB_PORT_ACTIVE; 626 props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 627 } else { 628 props->state = IB_PORT_DOWN; 629 props->phys_state = IB_PORT_PHYS_STATE_DISABLED; 630 } 631 632 ib_get_eth_speed(ibdev, port, &props->active_speed, &props->active_width); 633 props->pkey_tbl_len = 1; 634 if (mana_ib_is_rnic(dev)) { 635 props->gid_tbl_len = 16; 636 props->ip_gids = true; 637 props->max_msg_sz = SZ_16M; 638 if (port == 1) 639 props->port_cap_flags = IB_PORT_CM_SUP; 640 } 641 642 return 0; 643 } 644 645 enum rdma_link_layer mana_ib_get_link_layer(struct ib_device *device, u32 port_num) 646 { 647 return IB_LINK_LAYER_ETHERNET; 648 } 649 650 int mana_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index, u16 *pkey) 651 { 652 if (index != 0) 653 return -EINVAL; 654 *pkey = IB_DEFAULT_PKEY_FULL; 655 return 0; 656 } 657 658 int mana_ib_query_gid(struct ib_device *ibdev, u32 port, int index, 659 union ib_gid *gid) 660 { 661 /* This version doesn't return GID properties */ 662 return 0; 663 } 664 665 void mana_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 666 { 667 } 668 669 int mana_ib_gd_query_adapter_caps(struct mana_ib_dev *dev) 670 { 671 struct mana_ib_adapter_caps *caps = &dev->adapter_caps; 672 struct mana_ib_query_adapter_caps_resp resp = {}; 673 struct mana_ib_query_adapter_caps_req req = {}; 674 int err; 675 676 mana_gd_init_req_hdr(&req.hdr, MANA_IB_GET_ADAPTER_CAP, sizeof(req), 677 sizeof(resp)); 678 req.hdr.resp.msg_version = GDMA_MESSAGE_V4; 679 req.hdr.dev_id = dev->gdma_dev->dev_id; 680 681 err = mana_gd_send_request(mdev_to_gc(dev), sizeof(req), &req, 682 sizeof(resp), &resp); 683 if (err) 684 return err; 685 686 caps->max_sq_id = resp.max_sq_id; 687 caps->max_rq_id = resp.max_rq_id; 688 caps->max_cq_id = resp.max_cq_id; 689 caps->max_qp_count = resp.max_qp_count; 690 caps->max_cq_count = resp.max_cq_count; 691 caps->max_mr_count = resp.max_mr_count; 692 caps->max_pd_count = resp.max_pd_count; 693 caps->max_inbound_read_limit = resp.max_inbound_read_limit; 694 caps->max_outbound_read_limit = resp.max_outbound_read_limit; 695 caps->mw_count = resp.mw_count; 696 caps->max_srq_count = resp.max_srq_count; 697 caps->max_qp_wr = min_t(u32, 698 resp.max_requester_sq_size / GDMA_MAX_SQE_SIZE, 699 resp.max_requester_rq_size / GDMA_MAX_RQE_SIZE); 700 caps->max_inline_data_size = resp.max_inline_data_size; 701 caps->max_send_sge_count = resp.max_send_sge_count; 702 caps->max_recv_sge_count = resp.max_recv_sge_count; 703 caps->feature_flags = resp.feature_flags; 704 705 caps->page_size_cap = PAGE_SZ_BM; 706 if (mdev_to_gc(dev)->pf_cap_flags1 & GDMA_DRV_CAP_FLAG_1_GDMA_PAGES_4MB_1GB_2GB) 707 caps->page_size_cap |= (SZ_4M | SZ_1G | SZ_2G); 708 709 return 0; 710 } 711 712 int mana_eth_query_adapter_caps(struct mana_ib_dev *dev) 713 { 714 struct mana_ib_adapter_caps *caps = &dev->adapter_caps; 715 struct gdma_query_max_resources_resp resp = {}; 716 struct gdma_general_req req = {}; 717 int err; 718 719 mana_gd_init_req_hdr(&req.hdr, GDMA_QUERY_MAX_RESOURCES, 720 sizeof(req), sizeof(resp)); 721 722 err = mana_gd_send_request(mdev_to_gc(dev), sizeof(req), &req, 723 sizeof(resp), &resp); 724 if (err) 725 return err; 726 727 caps->max_qp_count = min_t(u32, resp.max_sq, resp.max_rq); 728 caps->max_cq_count = resp.max_cq; 729 caps->max_mr_count = resp.max_mst; 730 caps->max_pd_count = 0x6000; 731 caps->max_qp_wr = min_t(u32, 732 0x100000 / GDMA_MAX_SQE_SIZE, 733 0x100000 / GDMA_MAX_RQE_SIZE); 734 caps->max_send_sge_count = 30; 735 caps->max_recv_sge_count = 15; 736 caps->page_size_cap = PAGE_SZ_BM; 737 738 return 0; 739 } 740 741 static void 742 mana_ib_event_handler(void *ctx, struct gdma_queue *q, struct gdma_event *event) 743 { 744 struct mana_ib_dev *mdev = (struct mana_ib_dev *)ctx; 745 struct mana_ib_qp *qp; 746 struct ib_event ev; 747 u32 qpn; 748 749 switch (event->type) { 750 case GDMA_EQE_RNIC_QP_FATAL: 751 qpn = event->details[0]; 752 qp = mana_get_qp_ref(mdev, qpn, false); 753 if (!qp) 754 break; 755 if (qp->ibqp.event_handler) { 756 ev.device = qp->ibqp.device; 757 ev.element.qp = &qp->ibqp; 758 ev.event = IB_EVENT_QP_FATAL; 759 qp->ibqp.event_handler(&ev, qp->ibqp.qp_context); 760 } 761 mana_put_qp_ref(qp); 762 break; 763 default: 764 break; 765 } 766 } 767 768 int mana_ib_create_eqs(struct mana_ib_dev *mdev) 769 { 770 struct gdma_context *gc = mdev_to_gc(mdev); 771 struct gdma_queue_spec spec = {}; 772 struct gdma_irq_context *gic; 773 int err, i, msi; 774 775 spec.type = GDMA_EQ; 776 spec.monitor_avl_buf = false; 777 spec.queue_size = EQ_SIZE; 778 spec.eq.callback = mana_ib_event_handler; 779 spec.eq.context = mdev; 780 spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE; 781 782 msi = 0; 783 gic = mana_gd_get_gic(gc, false, &msi); 784 if (IS_ERR(gic)) 785 return PTR_ERR(gic); 786 spec.eq.msix_index = msi; 787 788 err = mana_gd_create_mana_eq(mdev->gdma_dev, &spec, &mdev->fatal_err_eq); 789 if (err) { 790 mana_gd_put_gic(gc, false, 0); 791 return err; 792 } 793 mdev->fatal_err_eq->eq.irq = gic->irq; 794 795 mdev->eqs = kzalloc_objs(struct gdma_queue *, 796 mdev->ib_dev.num_comp_vectors); 797 if (!mdev->eqs) { 798 err = -ENOMEM; 799 goto destroy_fatal_eq; 800 } 801 spec.eq.callback = NULL; 802 for (i = 0; i < mdev->ib_dev.num_comp_vectors; i++) { 803 msi = (i + 1) % gc->num_msix_usable; 804 805 gic = mana_gd_get_gic(gc, false, &msi); 806 if (IS_ERR(gic)) { 807 err = PTR_ERR(gic); 808 goto destroy_eqs; 809 } 810 spec.eq.msix_index = msi; 811 812 err = mana_gd_create_mana_eq(mdev->gdma_dev, &spec, &mdev->eqs[i]); 813 if (err) { 814 mana_gd_put_gic(gc, false, msi); 815 goto destroy_eqs; 816 } 817 mdev->eqs[i]->eq.irq = gic->irq; 818 } 819 820 return 0; 821 822 destroy_eqs: 823 while (i-- > 0) { 824 mana_gd_destroy_queue(gc, mdev->eqs[i]); 825 mana_gd_put_gic(gc, false, (i + 1) % gc->num_msix_usable); 826 } 827 kfree(mdev->eqs); 828 destroy_fatal_eq: 829 mana_gd_destroy_queue(gc, mdev->fatal_err_eq); 830 mana_gd_put_gic(gc, false, 0); 831 return err; 832 } 833 834 void mana_ib_destroy_eqs(struct mana_ib_dev *mdev) 835 { 836 struct gdma_context *gc = mdev_to_gc(mdev); 837 int i, msi; 838 839 mana_gd_destroy_queue(gc, mdev->fatal_err_eq); 840 mana_gd_put_gic(gc, false, 0); 841 842 for (i = 0; i < mdev->ib_dev.num_comp_vectors; i++) { 843 mana_gd_destroy_queue(gc, mdev->eqs[i]); 844 msi = (i + 1) % gc->num_msix_usable; 845 mana_gd_put_gic(gc, false, msi); 846 } 847 848 kfree(mdev->eqs); 849 } 850 851 int mana_ib_gd_create_rnic_adapter(struct mana_ib_dev *mdev) 852 { 853 struct mana_rnic_create_adapter_resp resp = {}; 854 struct mana_rnic_create_adapter_req req = {}; 855 struct gdma_context *gc = mdev_to_gc(mdev); 856 int err; 857 858 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CREATE_ADAPTER, sizeof(req), sizeof(resp)); 859 req.hdr.req.msg_version = GDMA_MESSAGE_V2; 860 req.hdr.dev_id = mdev->gdma_dev->dev_id; 861 req.notify_eq_id = mdev->fatal_err_eq->id; 862 863 if (mdev->adapter_caps.feature_flags & MANA_IB_FEATURE_CLIENT_ERROR_CQE_SUPPORT) 864 req.feature_flags |= MANA_IB_FEATURE_CLIENT_ERROR_CQE_REQUEST; 865 866 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 867 if (err) 868 return err; 869 mdev->adapter_handle = resp.adapter; 870 871 return 0; 872 } 873 874 int mana_ib_gd_destroy_rnic_adapter(struct mana_ib_dev *mdev) 875 { 876 struct mana_rnic_destroy_adapter_resp resp = {}; 877 struct mana_rnic_destroy_adapter_req req = {}; 878 struct gdma_context *gc; 879 880 gc = mdev_to_gc(mdev); 881 mana_gd_init_req_hdr(&req.hdr, MANA_IB_DESTROY_ADAPTER, sizeof(req), sizeof(resp)); 882 req.hdr.dev_id = mdev->gdma_dev->dev_id; 883 req.adapter = mdev->adapter_handle; 884 885 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 886 } 887 888 int mana_ib_gd_add_gid(const struct ib_gid_attr *attr, void **context) 889 { 890 struct mana_ib_dev *mdev = container_of(attr->device, struct mana_ib_dev, ib_dev); 891 enum rdma_network_type ntype = rdma_gid_attr_network_type(attr); 892 struct mana_rnic_config_addr_resp resp = {}; 893 struct gdma_context *gc = mdev_to_gc(mdev); 894 struct mana_rnic_config_addr_req req = {}; 895 896 if (ntype != RDMA_NETWORK_IPV4 && ntype != RDMA_NETWORK_IPV6) { 897 ibdev_dbg(&mdev->ib_dev, "Unsupported rdma network type %d", ntype); 898 return -EINVAL; 899 } 900 901 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CONFIG_IP_ADDR, sizeof(req), sizeof(resp)); 902 req.hdr.dev_id = mdev->gdma_dev->dev_id; 903 req.adapter = mdev->adapter_handle; 904 req.op = ADDR_OP_ADD; 905 req.sgid_type = (ntype == RDMA_NETWORK_IPV6) ? SGID_TYPE_IPV6 : SGID_TYPE_IPV4; 906 copy_in_reverse(req.ip_addr, attr->gid.raw, sizeof(union ib_gid)); 907 908 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 909 } 910 911 int mana_ib_gd_del_gid(const struct ib_gid_attr *attr, void **context) 912 { 913 struct mana_ib_dev *mdev = container_of(attr->device, struct mana_ib_dev, ib_dev); 914 enum rdma_network_type ntype = rdma_gid_attr_network_type(attr); 915 struct mana_rnic_config_addr_resp resp = {}; 916 struct gdma_context *gc = mdev_to_gc(mdev); 917 struct mana_rnic_config_addr_req req = {}; 918 919 if (ntype != RDMA_NETWORK_IPV4 && ntype != RDMA_NETWORK_IPV6) { 920 ibdev_dbg(&mdev->ib_dev, "Unsupported rdma network type %d", ntype); 921 return -EINVAL; 922 } 923 924 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CONFIG_IP_ADDR, sizeof(req), sizeof(resp)); 925 req.hdr.dev_id = mdev->gdma_dev->dev_id; 926 req.adapter = mdev->adapter_handle; 927 req.op = ADDR_OP_REMOVE; 928 req.sgid_type = (ntype == RDMA_NETWORK_IPV6) ? SGID_TYPE_IPV6 : SGID_TYPE_IPV4; 929 copy_in_reverse(req.ip_addr, attr->gid.raw, sizeof(union ib_gid)); 930 931 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 932 } 933 934 int mana_ib_gd_config_mac(struct mana_ib_dev *mdev, enum mana_ib_addr_op op, u8 *mac) 935 { 936 struct mana_rnic_config_mac_addr_resp resp = {}; 937 struct mana_rnic_config_mac_addr_req req = {}; 938 struct gdma_context *gc = mdev_to_gc(mdev); 939 940 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CONFIG_MAC_ADDR, sizeof(req), sizeof(resp)); 941 req.hdr.dev_id = mdev->gdma_dev->dev_id; 942 req.adapter = mdev->adapter_handle; 943 req.op = op; 944 copy_in_reverse(req.mac_addr, mac, ETH_ALEN); 945 946 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 947 } 948 949 int mana_ib_gd_create_cq(struct mana_ib_dev *mdev, struct mana_ib_cq *cq, u32 doorbell) 950 { 951 struct gdma_context *gc = mdev_to_gc(mdev); 952 struct mana_rnic_create_cq_resp resp = {}; 953 struct mana_rnic_create_cq_req req = {}; 954 int err; 955 956 if (!mdev->eqs) 957 return -EINVAL; 958 959 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CREATE_CQ, sizeof(req), sizeof(resp)); 960 req.hdr.dev_id = mdev->gdma_dev->dev_id; 961 req.adapter = mdev->adapter_handle; 962 req.gdma_region = cq->queue.gdma_region; 963 req.eq_id = mdev->eqs[cq->comp_vector]->id; 964 req.doorbell_page = doorbell; 965 966 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 967 968 if (err) { 969 ibdev_err(&mdev->ib_dev, "Failed to create cq err %d", err); 970 return err; 971 } 972 973 cq->queue.id = resp.cq_id; 974 cq->cq_handle = resp.cq_handle; 975 /* The GDMA region is now owned by the CQ handle */ 976 cq->queue.gdma_region = GDMA_INVALID_DMA_REGION; 977 978 return 0; 979 } 980 981 int mana_ib_gd_destroy_cq(struct mana_ib_dev *mdev, struct mana_ib_cq *cq) 982 { 983 struct gdma_context *gc = mdev_to_gc(mdev); 984 struct mana_rnic_destroy_cq_resp resp = {}; 985 struct mana_rnic_destroy_cq_req req = {}; 986 987 if (cq->cq_handle == INVALID_MANA_HANDLE) 988 return 0; 989 990 mana_gd_init_req_hdr(&req.hdr, MANA_IB_DESTROY_CQ, sizeof(req), sizeof(resp)); 991 req.hdr.dev_id = mdev->gdma_dev->dev_id; 992 req.adapter = mdev->adapter_handle; 993 req.cq_handle = cq->cq_handle; 994 995 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 996 } 997 998 int mana_ib_gd_create_rc_qp(struct mana_ib_dev *mdev, struct mana_ib_qp *qp, 999 struct ib_qp_init_attr *attr, u32 doorbell, u64 flags) 1000 { 1001 struct mana_ib_cq *send_cq = container_of(qp->ibqp.send_cq, struct mana_ib_cq, ibcq); 1002 struct mana_ib_cq *recv_cq = container_of(qp->ibqp.recv_cq, struct mana_ib_cq, ibcq); 1003 struct mana_ib_pd *pd = container_of(qp->ibqp.pd, struct mana_ib_pd, ibpd); 1004 struct gdma_context *gc = mdev_to_gc(mdev); 1005 struct mana_rnic_create_qp_resp resp = {}; 1006 struct mana_rnic_create_qp_req req = {}; 1007 int err, i; 1008 1009 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CREATE_RC_QP, sizeof(req), sizeof(resp)); 1010 req.hdr.dev_id = mdev->gdma_dev->dev_id; 1011 req.adapter = mdev->adapter_handle; 1012 req.pd_handle = pd->pd_handle; 1013 req.send_cq_handle = send_cq->cq_handle; 1014 req.recv_cq_handle = recv_cq->cq_handle; 1015 for (i = 0; i < MANA_RC_QUEUE_TYPE_MAX; i++) 1016 req.dma_region[i] = qp->rc_qp.queues[i].gdma_region; 1017 req.doorbell_page = doorbell; 1018 req.max_send_wr = attr->cap.max_send_wr; 1019 req.max_recv_wr = attr->cap.max_recv_wr; 1020 req.max_send_sge = attr->cap.max_send_sge; 1021 req.max_recv_sge = attr->cap.max_recv_sge; 1022 req.flags = flags; 1023 1024 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 1025 if (err) 1026 return err; 1027 1028 qp->qp_handle = resp.rc_qp_handle; 1029 for (i = 0; i < MANA_RC_QUEUE_TYPE_MAX; i++) { 1030 qp->rc_qp.queues[i].id = resp.queue_ids[i]; 1031 /* The GDMA regions are now owned by the RNIC QP handle */ 1032 qp->rc_qp.queues[i].gdma_region = GDMA_INVALID_DMA_REGION; 1033 } 1034 return 0; 1035 } 1036 1037 int mana_ib_gd_destroy_rc_qp(struct mana_ib_dev *mdev, struct mana_ib_qp *qp) 1038 { 1039 struct mana_rnic_destroy_rc_qp_resp resp = {0}; 1040 struct mana_rnic_destroy_rc_qp_req req = {0}; 1041 struct gdma_context *gc = mdev_to_gc(mdev); 1042 1043 mana_gd_init_req_hdr(&req.hdr, MANA_IB_DESTROY_RC_QP, sizeof(req), sizeof(resp)); 1044 req.hdr.dev_id = mdev->gdma_dev->dev_id; 1045 req.adapter = mdev->adapter_handle; 1046 req.rc_qp_handle = qp->qp_handle; 1047 1048 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 1049 } 1050 1051 int mana_ib_gd_create_ud_qp(struct mana_ib_dev *mdev, struct mana_ib_qp *qp, 1052 struct ib_qp_init_attr *attr, u32 doorbell, u32 type) 1053 { 1054 struct mana_ib_cq *send_cq = container_of(qp->ibqp.send_cq, struct mana_ib_cq, ibcq); 1055 struct mana_ib_cq *recv_cq = container_of(qp->ibqp.recv_cq, struct mana_ib_cq, ibcq); 1056 struct mana_ib_pd *pd = container_of(qp->ibqp.pd, struct mana_ib_pd, ibpd); 1057 struct gdma_context *gc = mdev_to_gc(mdev); 1058 struct mana_rnic_create_udqp_resp resp = {}; 1059 struct mana_rnic_create_udqp_req req = {}; 1060 int err, i; 1061 1062 mana_gd_init_req_hdr(&req.hdr, MANA_IB_CREATE_UD_QP, sizeof(req), sizeof(resp)); 1063 req.hdr.dev_id = mdev->gdma_dev->dev_id; 1064 req.adapter = mdev->adapter_handle; 1065 req.pd_handle = pd->pd_handle; 1066 req.send_cq_handle = send_cq->cq_handle; 1067 req.recv_cq_handle = recv_cq->cq_handle; 1068 for (i = 0; i < MANA_UD_QUEUE_TYPE_MAX; i++) 1069 req.dma_region[i] = qp->ud_qp.queues[i].gdma_region; 1070 req.doorbell_page = doorbell; 1071 req.max_send_wr = attr->cap.max_send_wr; 1072 req.max_recv_wr = attr->cap.max_recv_wr; 1073 req.max_send_sge = attr->cap.max_send_sge; 1074 req.max_recv_sge = attr->cap.max_recv_sge; 1075 req.qp_type = type; 1076 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 1077 if (err) 1078 return err; 1079 1080 qp->qp_handle = resp.qp_handle; 1081 for (i = 0; i < MANA_UD_QUEUE_TYPE_MAX; i++) { 1082 qp->ud_qp.queues[i].id = resp.queue_ids[i]; 1083 /* The GDMA regions are now owned by the RNIC QP handle */ 1084 qp->ud_qp.queues[i].gdma_region = GDMA_INVALID_DMA_REGION; 1085 } 1086 return 0; 1087 } 1088 1089 int mana_ib_gd_destroy_ud_qp(struct mana_ib_dev *mdev, struct mana_ib_qp *qp) 1090 { 1091 struct mana_rnic_destroy_udqp_resp resp = {0}; 1092 struct mana_rnic_destroy_udqp_req req = {0}; 1093 struct gdma_context *gc = mdev_to_gc(mdev); 1094 1095 mana_gd_init_req_hdr(&req.hdr, MANA_IB_DESTROY_UD_QP, sizeof(req), sizeof(resp)); 1096 req.hdr.dev_id = mdev->gdma_dev->dev_id; 1097 req.adapter = mdev->adapter_handle; 1098 req.qp_handle = qp->qp_handle; 1099 1100 return mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 1101 } 1102