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