1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause 2 /* 3 * Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All rights reserved. 4 */ 5 6 #include <linux/module.h> 7 #include <linux/pci.h> 8 #include <linux/utsname.h> 9 #include <linux/version.h> 10 11 #include <rdma/ib_user_verbs.h> 12 13 #include "efa.h" 14 15 #define PCI_DEV_ID_EFA_VF 0xefa0 16 17 static const struct pci_device_id efa_pci_tbl[] = { 18 { PCI_VDEVICE(AMAZON, PCI_DEV_ID_EFA_VF) }, 19 { } 20 }; 21 22 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates"); 23 MODULE_LICENSE("Dual BSD/GPL"); 24 MODULE_DESCRIPTION(DEVICE_NAME); 25 MODULE_DEVICE_TABLE(pci, efa_pci_tbl); 26 27 #define EFA_REG_BAR 0 28 #define EFA_MEM_BAR 2 29 #define EFA_BASE_BAR_MASK (BIT(EFA_REG_BAR) | BIT(EFA_MEM_BAR)) 30 31 #define EFA_AENQ_ENABLED_GROUPS \ 32 (BIT(EFA_ADMIN_FATAL_ERROR) | BIT(EFA_ADMIN_WARNING) | \ 33 BIT(EFA_ADMIN_NOTIFICATION) | BIT(EFA_ADMIN_KEEP_ALIVE)) 34 35 /* This handler will called for unknown event group or unimplemented handlers */ 36 static void unimplemented_aenq_handler(void *data, 37 struct efa_admin_aenq_entry *aenq_e) 38 { 39 struct efa_dev *dev = (struct efa_dev *)data; 40 41 ibdev_err(&dev->ibdev, 42 "Unknown event was received or event with unimplemented handler\n"); 43 } 44 45 static void efa_keep_alive(void *data, struct efa_admin_aenq_entry *aenq_e) 46 { 47 struct efa_dev *dev = (struct efa_dev *)data; 48 49 atomic64_inc(&dev->stats.keep_alive_rcvd); 50 } 51 52 static struct efa_aenq_handlers aenq_handlers = { 53 .handlers = { 54 [EFA_ADMIN_KEEP_ALIVE] = efa_keep_alive, 55 }, 56 .unimplemented_handler = unimplemented_aenq_handler 57 }; 58 59 static void efa_release_bars(struct efa_dev *dev, int bars_mask) 60 { 61 struct pci_dev *pdev = dev->pdev; 62 int release_bars; 63 64 release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & bars_mask; 65 pci_release_selected_regions(pdev, release_bars); 66 } 67 68 static irqreturn_t efa_intr_msix_mgmnt(int irq, void *data) 69 { 70 struct efa_dev *dev = data; 71 72 efa_com_admin_q_comp_intr_handler(&dev->edev); 73 efa_com_aenq_intr_handler(&dev->edev, data); 74 75 return IRQ_HANDLED; 76 } 77 78 static int efa_request_mgmnt_irq(struct efa_dev *dev) 79 { 80 struct efa_irq *irq; 81 int err; 82 83 irq = &dev->admin_irq; 84 err = request_irq(irq->vector, irq->handler, 0, irq->name, 85 irq->data); 86 if (err) { 87 dev_err(&dev->pdev->dev, "Failed to request admin irq (%d)\n", 88 err); 89 return err; 90 } 91 92 dev_dbg(&dev->pdev->dev, "Set affinity hint of mgmnt irq to %*pbl (irq vector: %d)\n", 93 nr_cpumask_bits, &irq->affinity_hint_mask, irq->vector); 94 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask); 95 96 return 0; 97 } 98 99 static void efa_setup_mgmnt_irq(struct efa_dev *dev) 100 { 101 u32 cpu; 102 103 snprintf(dev->admin_irq.name, EFA_IRQNAME_SIZE, 104 "efa-mgmnt@pci:%s", pci_name(dev->pdev)); 105 dev->admin_irq.handler = efa_intr_msix_mgmnt; 106 dev->admin_irq.data = dev; 107 dev->admin_irq.vector = 108 pci_irq_vector(dev->pdev, dev->admin_msix_vector_idx); 109 cpu = cpumask_first(cpu_online_mask); 110 dev->admin_irq.cpu = cpu; 111 cpumask_set_cpu(cpu, 112 &dev->admin_irq.affinity_hint_mask); 113 dev_info(&dev->pdev->dev, "Setup irq:0x%p vector:%d name:%s\n", 114 &dev->admin_irq, 115 dev->admin_irq.vector, 116 dev->admin_irq.name); 117 } 118 119 static void efa_free_mgmnt_irq(struct efa_dev *dev) 120 { 121 struct efa_irq *irq; 122 123 irq = &dev->admin_irq; 124 irq_set_affinity_hint(irq->vector, NULL); 125 free_irq(irq->vector, irq->data); 126 } 127 128 static int efa_set_mgmnt_irq(struct efa_dev *dev) 129 { 130 efa_setup_mgmnt_irq(dev); 131 132 return efa_request_mgmnt_irq(dev); 133 } 134 135 static int efa_request_doorbell_bar(struct efa_dev *dev) 136 { 137 u8 db_bar_idx = dev->dev_attr.db_bar; 138 struct pci_dev *pdev = dev->pdev; 139 int bars; 140 int err; 141 142 if (!(BIT(db_bar_idx) & EFA_BASE_BAR_MASK)) { 143 bars = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(db_bar_idx); 144 145 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME); 146 if (err) { 147 dev_err(&dev->pdev->dev, 148 "pci_request_selected_regions for bar %d failed %d\n", 149 db_bar_idx, err); 150 return err; 151 } 152 } 153 154 dev->db_bar_addr = pci_resource_start(dev->pdev, db_bar_idx); 155 dev->db_bar_len = pci_resource_len(dev->pdev, db_bar_idx); 156 157 return 0; 158 } 159 160 static void efa_release_doorbell_bar(struct efa_dev *dev) 161 { 162 if (!(BIT(dev->dev_attr.db_bar) & EFA_BASE_BAR_MASK)) 163 efa_release_bars(dev, BIT(dev->dev_attr.db_bar)); 164 } 165 166 static void efa_update_hw_hints(struct efa_dev *dev, 167 struct efa_com_get_hw_hints_result *hw_hints) 168 { 169 struct efa_com_dev *edev = &dev->edev; 170 171 if (hw_hints->mmio_read_timeout) 172 edev->mmio_read.mmio_read_timeout = 173 hw_hints->mmio_read_timeout * 1000; 174 175 if (hw_hints->poll_interval) 176 edev->aq.poll_interval = hw_hints->poll_interval; 177 178 if (hw_hints->admin_completion_timeout) 179 edev->aq.completion_timeout = 180 hw_hints->admin_completion_timeout; 181 } 182 183 static void efa_stats_init(struct efa_dev *dev) 184 { 185 atomic64_t *s = (atomic64_t *)&dev->stats; 186 int i; 187 188 for (i = 0; i < sizeof(dev->stats) / sizeof(*s); i++, s++) 189 atomic64_set(s, 0); 190 } 191 192 static void efa_set_host_info(struct efa_dev *dev) 193 { 194 struct efa_admin_set_feature_resp resp = {}; 195 struct efa_admin_set_feature_cmd cmd = {}; 196 struct efa_admin_host_info *hinf; 197 u32 bufsz = sizeof(*hinf); 198 dma_addr_t hinf_dma; 199 200 if (!efa_com_check_supported_feature_id(&dev->edev, 201 EFA_ADMIN_HOST_INFO)) 202 return; 203 204 /* Failures in host info set shall not disturb probe */ 205 hinf = dma_alloc_coherent(&dev->pdev->dev, bufsz, &hinf_dma, 206 GFP_KERNEL); 207 if (!hinf) 208 return; 209 210 strlcpy(hinf->os_dist_str, utsname()->release, 211 min(sizeof(hinf->os_dist_str), sizeof(utsname()->release))); 212 hinf->os_type = EFA_ADMIN_OS_LINUX; 213 strlcpy(hinf->kernel_ver_str, utsname()->version, 214 min(sizeof(hinf->kernel_ver_str), sizeof(utsname()->version))); 215 hinf->kernel_ver = LINUX_VERSION_CODE; 216 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MAJOR, 0); 217 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MINOR, 0); 218 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_SUB_MINOR, 0); 219 EFA_SET(&hinf->driver_ver, EFA_ADMIN_HOST_INFO_DRIVER_MODULE_TYPE, 0); 220 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_BUS, dev->pdev->bus->number); 221 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_DEVICE, 222 PCI_SLOT(dev->pdev->devfn)); 223 EFA_SET(&hinf->bdf, EFA_ADMIN_HOST_INFO_FUNCTION, 224 PCI_FUNC(dev->pdev->devfn)); 225 EFA_SET(&hinf->spec_ver, EFA_ADMIN_HOST_INFO_SPEC_MAJOR, 226 EFA_COMMON_SPEC_VERSION_MAJOR); 227 EFA_SET(&hinf->spec_ver, EFA_ADMIN_HOST_INFO_SPEC_MINOR, 228 EFA_COMMON_SPEC_VERSION_MINOR); 229 EFA_SET(&hinf->flags, EFA_ADMIN_HOST_INFO_INTREE, 1); 230 EFA_SET(&hinf->flags, EFA_ADMIN_HOST_INFO_GDR, 0); 231 232 efa_com_set_feature_ex(&dev->edev, &resp, &cmd, EFA_ADMIN_HOST_INFO, 233 hinf_dma, bufsz); 234 235 dma_free_coherent(&dev->pdev->dev, bufsz, hinf, hinf_dma); 236 } 237 238 static const struct ib_device_ops efa_dev_ops = { 239 .owner = THIS_MODULE, 240 .driver_id = RDMA_DRIVER_EFA, 241 .uverbs_abi_ver = EFA_UVERBS_ABI_VERSION, 242 243 .alloc_hw_stats = efa_alloc_hw_stats, 244 .alloc_pd = efa_alloc_pd, 245 .alloc_ucontext = efa_alloc_ucontext, 246 .create_ah = efa_create_ah, 247 .create_cq = efa_create_cq, 248 .create_qp = efa_create_qp, 249 .dealloc_pd = efa_dealloc_pd, 250 .dealloc_ucontext = efa_dealloc_ucontext, 251 .dereg_mr = efa_dereg_mr, 252 .destroy_ah = efa_destroy_ah, 253 .destroy_cq = efa_destroy_cq, 254 .destroy_qp = efa_destroy_qp, 255 .get_hw_stats = efa_get_hw_stats, 256 .get_link_layer = efa_port_link_layer, 257 .get_port_immutable = efa_get_port_immutable, 258 .mmap = efa_mmap, 259 .mmap_free = efa_mmap_free, 260 .modify_qp = efa_modify_qp, 261 .query_device = efa_query_device, 262 .query_gid = efa_query_gid, 263 .query_pkey = efa_query_pkey, 264 .query_port = efa_query_port, 265 .query_qp = efa_query_qp, 266 .reg_user_mr = efa_reg_mr, 267 268 INIT_RDMA_OBJ_SIZE(ib_ah, efa_ah, ibah), 269 INIT_RDMA_OBJ_SIZE(ib_cq, efa_cq, ibcq), 270 INIT_RDMA_OBJ_SIZE(ib_pd, efa_pd, ibpd), 271 INIT_RDMA_OBJ_SIZE(ib_ucontext, efa_ucontext, ibucontext), 272 }; 273 274 static int efa_ib_device_add(struct efa_dev *dev) 275 { 276 struct efa_com_get_hw_hints_result hw_hints; 277 struct pci_dev *pdev = dev->pdev; 278 int err; 279 280 efa_stats_init(dev); 281 282 err = efa_com_get_device_attr(&dev->edev, &dev->dev_attr); 283 if (err) 284 return err; 285 286 dev_dbg(&dev->pdev->dev, "Doorbells bar (%d)\n", dev->dev_attr.db_bar); 287 err = efa_request_doorbell_bar(dev); 288 if (err) 289 return err; 290 291 err = efa_com_get_hw_hints(&dev->edev, &hw_hints); 292 if (err) 293 goto err_release_doorbell_bar; 294 295 efa_update_hw_hints(dev, &hw_hints); 296 297 /* Try to enable all the available aenq groups */ 298 err = efa_com_set_aenq_config(&dev->edev, EFA_AENQ_ENABLED_GROUPS); 299 if (err) 300 goto err_release_doorbell_bar; 301 302 efa_set_host_info(dev); 303 304 dev->ibdev.node_type = RDMA_NODE_UNSPECIFIED; 305 dev->ibdev.phys_port_cnt = 1; 306 dev->ibdev.num_comp_vectors = 1; 307 dev->ibdev.dev.parent = &pdev->dev; 308 309 dev->ibdev.uverbs_cmd_mask = 310 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 311 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 312 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 313 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 314 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 315 (1ull << IB_USER_VERBS_CMD_REG_MR) | 316 (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 317 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 318 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 319 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 320 (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 321 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 322 (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 323 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 324 (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 325 (1ull << IB_USER_VERBS_CMD_DESTROY_AH); 326 327 dev->ibdev.uverbs_ex_cmd_mask = 328 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE); 329 330 ib_set_device_ops(&dev->ibdev, &efa_dev_ops); 331 332 err = ib_register_device(&dev->ibdev, "efa_%d"); 333 if (err) 334 goto err_release_doorbell_bar; 335 336 ibdev_info(&dev->ibdev, "IB device registered\n"); 337 338 return 0; 339 340 err_release_doorbell_bar: 341 efa_release_doorbell_bar(dev); 342 return err; 343 } 344 345 static void efa_ib_device_remove(struct efa_dev *dev) 346 { 347 efa_com_dev_reset(&dev->edev, EFA_REGS_RESET_NORMAL); 348 ibdev_info(&dev->ibdev, "Unregister ib device\n"); 349 ib_unregister_device(&dev->ibdev); 350 efa_release_doorbell_bar(dev); 351 } 352 353 static void efa_disable_msix(struct efa_dev *dev) 354 { 355 pci_free_irq_vectors(dev->pdev); 356 } 357 358 static int efa_enable_msix(struct efa_dev *dev) 359 { 360 int msix_vecs, irq_num; 361 362 /* Reserve the max msix vectors we might need */ 363 msix_vecs = EFA_NUM_MSIX_VEC; 364 dev_dbg(&dev->pdev->dev, "Trying to enable MSI-X, vectors %d\n", 365 msix_vecs); 366 367 dev->admin_msix_vector_idx = EFA_MGMNT_MSIX_VEC_IDX; 368 irq_num = pci_alloc_irq_vectors(dev->pdev, msix_vecs, 369 msix_vecs, PCI_IRQ_MSIX); 370 371 if (irq_num < 0) { 372 dev_err(&dev->pdev->dev, "Failed to enable MSI-X. irq_num %d\n", 373 irq_num); 374 return -ENOSPC; 375 } 376 377 if (irq_num != msix_vecs) { 378 dev_err(&dev->pdev->dev, 379 "Allocated %d MSI-X (out of %d requested)\n", 380 irq_num, msix_vecs); 381 return -ENOSPC; 382 } 383 384 return 0; 385 } 386 387 static int efa_device_init(struct efa_com_dev *edev, struct pci_dev *pdev) 388 { 389 int dma_width; 390 int err; 391 392 err = efa_com_dev_reset(edev, EFA_REGS_RESET_NORMAL); 393 if (err) 394 return err; 395 396 err = efa_com_validate_version(edev); 397 if (err) 398 return err; 399 400 dma_width = efa_com_get_dma_width(edev); 401 if (dma_width < 0) { 402 err = dma_width; 403 return err; 404 } 405 406 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_width)); 407 if (err) { 408 dev_err(&pdev->dev, "pci_set_dma_mask failed %d\n", err); 409 return err; 410 } 411 412 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(dma_width)); 413 if (err) { 414 dev_err(&pdev->dev, 415 "err_pci_set_consistent_dma_mask failed %d\n", 416 err); 417 return err; 418 } 419 420 return 0; 421 } 422 423 static struct efa_dev *efa_probe_device(struct pci_dev *pdev) 424 { 425 struct efa_com_dev *edev; 426 struct efa_dev *dev; 427 int bars; 428 int err; 429 430 err = pci_enable_device_mem(pdev); 431 if (err) { 432 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n"); 433 return ERR_PTR(err); 434 } 435 436 pci_set_master(pdev); 437 438 dev = ib_alloc_device(efa_dev, ibdev); 439 if (!dev) { 440 dev_err(&pdev->dev, "Device alloc failed\n"); 441 err = -ENOMEM; 442 goto err_disable_device; 443 } 444 445 pci_set_drvdata(pdev, dev); 446 edev = &dev->edev; 447 edev->efa_dev = dev; 448 edev->dmadev = &pdev->dev; 449 dev->pdev = pdev; 450 451 bars = pci_select_bars(pdev, IORESOURCE_MEM) & EFA_BASE_BAR_MASK; 452 err = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME); 453 if (err) { 454 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n", 455 err); 456 goto err_ibdev_destroy; 457 } 458 459 dev->reg_bar_addr = pci_resource_start(pdev, EFA_REG_BAR); 460 dev->reg_bar_len = pci_resource_len(pdev, EFA_REG_BAR); 461 dev->mem_bar_addr = pci_resource_start(pdev, EFA_MEM_BAR); 462 dev->mem_bar_len = pci_resource_len(pdev, EFA_MEM_BAR); 463 464 edev->reg_bar = devm_ioremap(&pdev->dev, 465 dev->reg_bar_addr, 466 dev->reg_bar_len); 467 if (!edev->reg_bar) { 468 dev_err(&pdev->dev, "Failed to remap register bar\n"); 469 err = -EFAULT; 470 goto err_release_bars; 471 } 472 473 err = efa_com_mmio_reg_read_init(edev); 474 if (err) { 475 dev_err(&pdev->dev, "Failed to init readless MMIO\n"); 476 goto err_iounmap; 477 } 478 479 err = efa_device_init(edev, pdev); 480 if (err) { 481 dev_err(&pdev->dev, "EFA device init failed\n"); 482 if (err == -ETIME) 483 err = -EPROBE_DEFER; 484 goto err_reg_read_destroy; 485 } 486 487 err = efa_enable_msix(dev); 488 if (err) 489 goto err_reg_read_destroy; 490 491 edev->aq.msix_vector_idx = dev->admin_msix_vector_idx; 492 edev->aenq.msix_vector_idx = dev->admin_msix_vector_idx; 493 494 err = efa_set_mgmnt_irq(dev); 495 if (err) 496 goto err_disable_msix; 497 498 err = efa_com_admin_init(edev, &aenq_handlers); 499 if (err) 500 goto err_free_mgmnt_irq; 501 502 return dev; 503 504 err_free_mgmnt_irq: 505 efa_free_mgmnt_irq(dev); 506 err_disable_msix: 507 efa_disable_msix(dev); 508 err_reg_read_destroy: 509 efa_com_mmio_reg_read_destroy(edev); 510 err_iounmap: 511 devm_iounmap(&pdev->dev, edev->reg_bar); 512 err_release_bars: 513 efa_release_bars(dev, EFA_BASE_BAR_MASK); 514 err_ibdev_destroy: 515 ib_dealloc_device(&dev->ibdev); 516 err_disable_device: 517 pci_disable_device(pdev); 518 return ERR_PTR(err); 519 } 520 521 static void efa_remove_device(struct pci_dev *pdev) 522 { 523 struct efa_dev *dev = pci_get_drvdata(pdev); 524 struct efa_com_dev *edev; 525 526 edev = &dev->edev; 527 efa_com_admin_destroy(edev); 528 efa_free_mgmnt_irq(dev); 529 efa_disable_msix(dev); 530 efa_com_mmio_reg_read_destroy(edev); 531 devm_iounmap(&pdev->dev, edev->reg_bar); 532 efa_release_bars(dev, EFA_BASE_BAR_MASK); 533 ib_dealloc_device(&dev->ibdev); 534 pci_disable_device(pdev); 535 } 536 537 static int efa_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 538 { 539 struct efa_dev *dev; 540 int err; 541 542 dev = efa_probe_device(pdev); 543 if (IS_ERR(dev)) 544 return PTR_ERR(dev); 545 546 err = efa_ib_device_add(dev); 547 if (err) 548 goto err_remove_device; 549 550 return 0; 551 552 err_remove_device: 553 efa_remove_device(pdev); 554 return err; 555 } 556 557 static void efa_remove(struct pci_dev *pdev) 558 { 559 struct efa_dev *dev = pci_get_drvdata(pdev); 560 561 efa_ib_device_remove(dev); 562 efa_remove_device(pdev); 563 } 564 565 static struct pci_driver efa_pci_driver = { 566 .name = DRV_MODULE_NAME, 567 .id_table = efa_pci_tbl, 568 .probe = efa_probe, 569 .remove = efa_remove, 570 }; 571 572 module_pci_driver(efa_pci_driver); 573