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