1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright (c) 2021, Microsoft Corporation. */ 3 4 #include <linux/module.h> 5 #include <linux/pci.h> 6 #include <linux/utsname.h> 7 #include <linux/version.h> 8 9 #include <net/mana/mana.h> 10 11 static u32 mana_gd_r32(struct gdma_context *g, u64 offset) 12 { 13 return readl(g->bar0_va + offset); 14 } 15 16 static u64 mana_gd_r64(struct gdma_context *g, u64 offset) 17 { 18 return readq(g->bar0_va + offset); 19 } 20 21 static void mana_gd_init_pf_regs(struct pci_dev *pdev) 22 { 23 struct gdma_context *gc = pci_get_drvdata(pdev); 24 void __iomem *sriov_base_va; 25 u64 sriov_base_off; 26 27 gc->db_page_size = mana_gd_r32(gc, GDMA_PF_REG_DB_PAGE_SIZE) & 0xFFFF; 28 gc->db_page_base = gc->bar0_va + 29 mana_gd_r64(gc, GDMA_PF_REG_DB_PAGE_OFF); 30 31 sriov_base_off = mana_gd_r64(gc, GDMA_SRIOV_REG_CFG_BASE_OFF); 32 33 sriov_base_va = gc->bar0_va + sriov_base_off; 34 gc->shm_base = sriov_base_va + 35 mana_gd_r64(gc, sriov_base_off + GDMA_PF_REG_SHM_OFF); 36 } 37 38 static void mana_gd_init_vf_regs(struct pci_dev *pdev) 39 { 40 struct gdma_context *gc = pci_get_drvdata(pdev); 41 42 gc->db_page_size = mana_gd_r32(gc, GDMA_REG_DB_PAGE_SIZE) & 0xFFFF; 43 44 gc->db_page_base = gc->bar0_va + 45 mana_gd_r64(gc, GDMA_REG_DB_PAGE_OFFSET); 46 47 gc->phys_db_page_base = gc->bar0_pa + 48 mana_gd_r64(gc, GDMA_REG_DB_PAGE_OFFSET); 49 50 gc->shm_base = gc->bar0_va + mana_gd_r64(gc, GDMA_REG_SHM_OFFSET); 51 } 52 53 static void mana_gd_init_registers(struct pci_dev *pdev) 54 { 55 struct gdma_context *gc = pci_get_drvdata(pdev); 56 57 if (gc->is_pf) 58 mana_gd_init_pf_regs(pdev); 59 else 60 mana_gd_init_vf_regs(pdev); 61 } 62 63 static int mana_gd_query_max_resources(struct pci_dev *pdev) 64 { 65 struct gdma_context *gc = pci_get_drvdata(pdev); 66 struct gdma_query_max_resources_resp resp = {}; 67 struct gdma_general_req req = {}; 68 int err; 69 70 mana_gd_init_req_hdr(&req.hdr, GDMA_QUERY_MAX_RESOURCES, 71 sizeof(req), sizeof(resp)); 72 73 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 74 if (err || resp.hdr.status) { 75 dev_err(gc->dev, "Failed to query resource info: %d, 0x%x\n", 76 err, resp.hdr.status); 77 return err ? err : -EPROTO; 78 } 79 80 if (gc->num_msix_usable > resp.max_msix) 81 gc->num_msix_usable = resp.max_msix; 82 83 if (gc->num_msix_usable <= 1) 84 return -ENOSPC; 85 86 gc->max_num_queues = num_online_cpus(); 87 if (gc->max_num_queues > MANA_MAX_NUM_QUEUES) 88 gc->max_num_queues = MANA_MAX_NUM_QUEUES; 89 90 if (gc->max_num_queues > resp.max_eq) 91 gc->max_num_queues = resp.max_eq; 92 93 if (gc->max_num_queues > resp.max_cq) 94 gc->max_num_queues = resp.max_cq; 95 96 if (gc->max_num_queues > resp.max_sq) 97 gc->max_num_queues = resp.max_sq; 98 99 if (gc->max_num_queues > resp.max_rq) 100 gc->max_num_queues = resp.max_rq; 101 102 /* The Hardware Channel (HWC) used 1 MSI-X */ 103 if (gc->max_num_queues > gc->num_msix_usable - 1) 104 gc->max_num_queues = gc->num_msix_usable - 1; 105 106 return 0; 107 } 108 109 static int mana_gd_detect_devices(struct pci_dev *pdev) 110 { 111 struct gdma_context *gc = pci_get_drvdata(pdev); 112 struct gdma_list_devices_resp resp = {}; 113 struct gdma_general_req req = {}; 114 struct gdma_dev_id dev; 115 u32 i, max_num_devs; 116 u16 dev_type; 117 int err; 118 119 mana_gd_init_req_hdr(&req.hdr, GDMA_LIST_DEVICES, sizeof(req), 120 sizeof(resp)); 121 122 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 123 if (err || resp.hdr.status) { 124 dev_err(gc->dev, "Failed to detect devices: %d, 0x%x\n", err, 125 resp.hdr.status); 126 return err ? err : -EPROTO; 127 } 128 129 max_num_devs = min_t(u32, MAX_NUM_GDMA_DEVICES, resp.num_of_devs); 130 131 for (i = 0; i < max_num_devs; i++) { 132 dev = resp.devs[i]; 133 dev_type = dev.type; 134 135 /* HWC is already detected in mana_hwc_create_channel(). */ 136 if (dev_type == GDMA_DEVICE_HWC) 137 continue; 138 139 if (dev_type == GDMA_DEVICE_MANA) { 140 gc->mana.gdma_context = gc; 141 gc->mana.dev_id = dev; 142 } 143 } 144 145 return gc->mana.dev_id.type == 0 ? -ENODEV : 0; 146 } 147 148 int mana_gd_send_request(struct gdma_context *gc, u32 req_len, const void *req, 149 u32 resp_len, void *resp) 150 { 151 struct hw_channel_context *hwc = gc->hwc.driver_data; 152 153 return mana_hwc_send_request(hwc, req_len, req, resp_len, resp); 154 } 155 EXPORT_SYMBOL_NS(mana_gd_send_request, NET_MANA); 156 157 int mana_gd_alloc_memory(struct gdma_context *gc, unsigned int length, 158 struct gdma_mem_info *gmi) 159 { 160 dma_addr_t dma_handle; 161 void *buf; 162 163 if (length < PAGE_SIZE || !is_power_of_2(length)) 164 return -EINVAL; 165 166 gmi->dev = gc->dev; 167 buf = dma_alloc_coherent(gmi->dev, length, &dma_handle, GFP_KERNEL); 168 if (!buf) 169 return -ENOMEM; 170 171 gmi->dma_handle = dma_handle; 172 gmi->virt_addr = buf; 173 gmi->length = length; 174 175 return 0; 176 } 177 178 void mana_gd_free_memory(struct gdma_mem_info *gmi) 179 { 180 dma_free_coherent(gmi->dev, gmi->length, gmi->virt_addr, 181 gmi->dma_handle); 182 } 183 184 static int mana_gd_create_hw_eq(struct gdma_context *gc, 185 struct gdma_queue *queue) 186 { 187 struct gdma_create_queue_resp resp = {}; 188 struct gdma_create_queue_req req = {}; 189 int err; 190 191 if (queue->type != GDMA_EQ) 192 return -EINVAL; 193 194 mana_gd_init_req_hdr(&req.hdr, GDMA_CREATE_QUEUE, 195 sizeof(req), sizeof(resp)); 196 197 req.hdr.dev_id = queue->gdma_dev->dev_id; 198 req.type = queue->type; 199 req.pdid = queue->gdma_dev->pdid; 200 req.doolbell_id = queue->gdma_dev->doorbell; 201 req.gdma_region = queue->mem_info.dma_region_handle; 202 req.queue_size = queue->queue_size; 203 req.log2_throttle_limit = queue->eq.log2_throttle_limit; 204 req.eq_pci_msix_index = queue->eq.msix_index; 205 206 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 207 if (err || resp.hdr.status) { 208 dev_err(gc->dev, "Failed to create queue: %d, 0x%x\n", err, 209 resp.hdr.status); 210 return err ? err : -EPROTO; 211 } 212 213 queue->id = resp.queue_index; 214 queue->eq.disable_needed = true; 215 queue->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 216 return 0; 217 } 218 219 static int mana_gd_disable_queue(struct gdma_queue *queue) 220 { 221 struct gdma_context *gc = queue->gdma_dev->gdma_context; 222 struct gdma_disable_queue_req req = {}; 223 struct gdma_general_resp resp = {}; 224 int err; 225 226 WARN_ON(queue->type != GDMA_EQ); 227 228 mana_gd_init_req_hdr(&req.hdr, GDMA_DISABLE_QUEUE, 229 sizeof(req), sizeof(resp)); 230 231 req.hdr.dev_id = queue->gdma_dev->dev_id; 232 req.type = queue->type; 233 req.queue_index = queue->id; 234 req.alloc_res_id_on_creation = 1; 235 236 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 237 if (err || resp.hdr.status) { 238 dev_err(gc->dev, "Failed to disable queue: %d, 0x%x\n", err, 239 resp.hdr.status); 240 return err ? err : -EPROTO; 241 } 242 243 return 0; 244 } 245 246 #define DOORBELL_OFFSET_SQ 0x0 247 #define DOORBELL_OFFSET_RQ 0x400 248 #define DOORBELL_OFFSET_CQ 0x800 249 #define DOORBELL_OFFSET_EQ 0xFF8 250 251 static void mana_gd_ring_doorbell(struct gdma_context *gc, u32 db_index, 252 enum gdma_queue_type q_type, u32 qid, 253 u32 tail_ptr, u8 num_req) 254 { 255 void __iomem *addr = gc->db_page_base + gc->db_page_size * db_index; 256 union gdma_doorbell_entry e = {}; 257 258 switch (q_type) { 259 case GDMA_EQ: 260 e.eq.id = qid; 261 e.eq.tail_ptr = tail_ptr; 262 e.eq.arm = num_req; 263 264 addr += DOORBELL_OFFSET_EQ; 265 break; 266 267 case GDMA_CQ: 268 e.cq.id = qid; 269 e.cq.tail_ptr = tail_ptr; 270 e.cq.arm = num_req; 271 272 addr += DOORBELL_OFFSET_CQ; 273 break; 274 275 case GDMA_RQ: 276 e.rq.id = qid; 277 e.rq.tail_ptr = tail_ptr; 278 e.rq.wqe_cnt = num_req; 279 280 addr += DOORBELL_OFFSET_RQ; 281 break; 282 283 case GDMA_SQ: 284 e.sq.id = qid; 285 e.sq.tail_ptr = tail_ptr; 286 287 addr += DOORBELL_OFFSET_SQ; 288 break; 289 290 default: 291 WARN_ON(1); 292 return; 293 } 294 295 /* Ensure all writes are done before ring doorbell */ 296 wmb(); 297 298 writeq(e.as_uint64, addr); 299 } 300 301 void mana_gd_wq_ring_doorbell(struct gdma_context *gc, struct gdma_queue *queue) 302 { 303 mana_gd_ring_doorbell(gc, queue->gdma_dev->doorbell, queue->type, 304 queue->id, queue->head * GDMA_WQE_BU_SIZE, 1); 305 } 306 307 void mana_gd_ring_cq(struct gdma_queue *cq, u8 arm_bit) 308 { 309 struct gdma_context *gc = cq->gdma_dev->gdma_context; 310 311 u32 num_cqe = cq->queue_size / GDMA_CQE_SIZE; 312 313 u32 head = cq->head % (num_cqe << GDMA_CQE_OWNER_BITS); 314 315 mana_gd_ring_doorbell(gc, cq->gdma_dev->doorbell, cq->type, cq->id, 316 head, arm_bit); 317 } 318 319 static void mana_gd_process_eqe(struct gdma_queue *eq) 320 { 321 u32 head = eq->head % (eq->queue_size / GDMA_EQE_SIZE); 322 struct gdma_context *gc = eq->gdma_dev->gdma_context; 323 struct gdma_eqe *eq_eqe_ptr = eq->queue_mem_ptr; 324 union gdma_eqe_info eqe_info; 325 enum gdma_eqe_type type; 326 struct gdma_event event; 327 struct gdma_queue *cq; 328 struct gdma_eqe *eqe; 329 u32 cq_id; 330 331 eqe = &eq_eqe_ptr[head]; 332 eqe_info.as_uint32 = eqe->eqe_info; 333 type = eqe_info.type; 334 335 switch (type) { 336 case GDMA_EQE_COMPLETION: 337 cq_id = eqe->details[0] & 0xFFFFFF; 338 if (WARN_ON_ONCE(cq_id >= gc->max_num_cqs)) 339 break; 340 341 cq = gc->cq_table[cq_id]; 342 if (WARN_ON_ONCE(!cq || cq->type != GDMA_CQ || cq->id != cq_id)) 343 break; 344 345 if (cq->cq.callback) 346 cq->cq.callback(cq->cq.context, cq); 347 348 break; 349 350 case GDMA_EQE_TEST_EVENT: 351 gc->test_event_eq_id = eq->id; 352 complete(&gc->eq_test_event); 353 break; 354 355 case GDMA_EQE_HWC_INIT_EQ_ID_DB: 356 case GDMA_EQE_HWC_INIT_DATA: 357 case GDMA_EQE_HWC_INIT_DONE: 358 if (!eq->eq.callback) 359 break; 360 361 event.type = type; 362 memcpy(&event.details, &eqe->details, GDMA_EVENT_DATA_SIZE); 363 eq->eq.callback(eq->eq.context, eq, &event); 364 break; 365 366 default: 367 break; 368 } 369 } 370 371 static void mana_gd_process_eq_events(void *arg) 372 { 373 u32 owner_bits, new_bits, old_bits; 374 union gdma_eqe_info eqe_info; 375 struct gdma_eqe *eq_eqe_ptr; 376 struct gdma_queue *eq = arg; 377 struct gdma_context *gc; 378 struct gdma_eqe *eqe; 379 u32 head, num_eqe; 380 int i; 381 382 gc = eq->gdma_dev->gdma_context; 383 384 num_eqe = eq->queue_size / GDMA_EQE_SIZE; 385 eq_eqe_ptr = eq->queue_mem_ptr; 386 387 /* Process up to 5 EQEs at a time, and update the HW head. */ 388 for (i = 0; i < 5; i++) { 389 eqe = &eq_eqe_ptr[eq->head % num_eqe]; 390 eqe_info.as_uint32 = eqe->eqe_info; 391 owner_bits = eqe_info.owner_bits; 392 393 old_bits = (eq->head / num_eqe - 1) & GDMA_EQE_OWNER_MASK; 394 /* No more entries */ 395 if (owner_bits == old_bits) 396 break; 397 398 new_bits = (eq->head / num_eqe) & GDMA_EQE_OWNER_MASK; 399 if (owner_bits != new_bits) { 400 dev_err(gc->dev, "EQ %d: overflow detected\n", eq->id); 401 break; 402 } 403 404 /* Per GDMA spec, rmb is necessary after checking owner_bits, before 405 * reading eqe. 406 */ 407 rmb(); 408 409 mana_gd_process_eqe(eq); 410 411 eq->head++; 412 } 413 414 head = eq->head % (num_eqe << GDMA_EQE_OWNER_BITS); 415 416 mana_gd_ring_doorbell(gc, eq->gdma_dev->doorbell, eq->type, eq->id, 417 head, SET_ARM_BIT); 418 } 419 420 static int mana_gd_register_irq(struct gdma_queue *queue, 421 const struct gdma_queue_spec *spec) 422 { 423 struct gdma_dev *gd = queue->gdma_dev; 424 struct gdma_irq_context *gic; 425 struct gdma_context *gc; 426 struct gdma_resource *r; 427 unsigned int msi_index; 428 unsigned long flags; 429 struct device *dev; 430 int err = 0; 431 432 gc = gd->gdma_context; 433 r = &gc->msix_resource; 434 dev = gc->dev; 435 436 spin_lock_irqsave(&r->lock, flags); 437 438 msi_index = find_first_zero_bit(r->map, r->size); 439 if (msi_index >= r->size || msi_index >= gc->num_msix_usable) { 440 err = -ENOSPC; 441 } else { 442 bitmap_set(r->map, msi_index, 1); 443 queue->eq.msix_index = msi_index; 444 } 445 446 spin_unlock_irqrestore(&r->lock, flags); 447 448 if (err) { 449 dev_err(dev, "Register IRQ err:%d, msi:%u rsize:%u, nMSI:%u", 450 err, msi_index, r->size, gc->num_msix_usable); 451 452 return err; 453 } 454 455 gic = &gc->irq_contexts[msi_index]; 456 457 WARN_ON(gic->handler || gic->arg); 458 459 gic->arg = queue; 460 461 gic->handler = mana_gd_process_eq_events; 462 463 return 0; 464 } 465 466 static void mana_gd_deregiser_irq(struct gdma_queue *queue) 467 { 468 struct gdma_dev *gd = queue->gdma_dev; 469 struct gdma_irq_context *gic; 470 struct gdma_context *gc; 471 struct gdma_resource *r; 472 unsigned int msix_index; 473 unsigned long flags; 474 475 gc = gd->gdma_context; 476 r = &gc->msix_resource; 477 478 /* At most num_online_cpus() + 1 interrupts are used. */ 479 msix_index = queue->eq.msix_index; 480 if (WARN_ON(msix_index >= gc->num_msix_usable)) 481 return; 482 483 gic = &gc->irq_contexts[msix_index]; 484 gic->handler = NULL; 485 gic->arg = NULL; 486 487 spin_lock_irqsave(&r->lock, flags); 488 bitmap_clear(r->map, msix_index, 1); 489 spin_unlock_irqrestore(&r->lock, flags); 490 491 queue->eq.msix_index = INVALID_PCI_MSIX_INDEX; 492 } 493 494 int mana_gd_test_eq(struct gdma_context *gc, struct gdma_queue *eq) 495 { 496 struct gdma_generate_test_event_req req = {}; 497 struct gdma_general_resp resp = {}; 498 struct device *dev = gc->dev; 499 int err; 500 501 mutex_lock(&gc->eq_test_event_mutex); 502 503 init_completion(&gc->eq_test_event); 504 gc->test_event_eq_id = INVALID_QUEUE_ID; 505 506 mana_gd_init_req_hdr(&req.hdr, GDMA_GENERATE_TEST_EQE, 507 sizeof(req), sizeof(resp)); 508 509 req.hdr.dev_id = eq->gdma_dev->dev_id; 510 req.queue_index = eq->id; 511 512 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 513 if (err) { 514 dev_err(dev, "test_eq failed: %d\n", err); 515 goto out; 516 } 517 518 err = -EPROTO; 519 520 if (resp.hdr.status) { 521 dev_err(dev, "test_eq failed: 0x%x\n", resp.hdr.status); 522 goto out; 523 } 524 525 if (!wait_for_completion_timeout(&gc->eq_test_event, 30 * HZ)) { 526 dev_err(dev, "test_eq timed out on queue %d\n", eq->id); 527 goto out; 528 } 529 530 if (eq->id != gc->test_event_eq_id) { 531 dev_err(dev, "test_eq got an event on wrong queue %d (%d)\n", 532 gc->test_event_eq_id, eq->id); 533 goto out; 534 } 535 536 err = 0; 537 out: 538 mutex_unlock(&gc->eq_test_event_mutex); 539 return err; 540 } 541 542 static void mana_gd_destroy_eq(struct gdma_context *gc, bool flush_evenets, 543 struct gdma_queue *queue) 544 { 545 int err; 546 547 if (flush_evenets) { 548 err = mana_gd_test_eq(gc, queue); 549 if (err) 550 dev_warn(gc->dev, "Failed to flush EQ: %d\n", err); 551 } 552 553 mana_gd_deregiser_irq(queue); 554 555 if (queue->eq.disable_needed) 556 mana_gd_disable_queue(queue); 557 } 558 559 static int mana_gd_create_eq(struct gdma_dev *gd, 560 const struct gdma_queue_spec *spec, 561 bool create_hwq, struct gdma_queue *queue) 562 { 563 struct gdma_context *gc = gd->gdma_context; 564 struct device *dev = gc->dev; 565 u32 log2_num_entries; 566 int err; 567 568 queue->eq.msix_index = INVALID_PCI_MSIX_INDEX; 569 570 log2_num_entries = ilog2(queue->queue_size / GDMA_EQE_SIZE); 571 572 if (spec->eq.log2_throttle_limit > log2_num_entries) { 573 dev_err(dev, "EQ throttling limit (%lu) > maximum EQE (%u)\n", 574 spec->eq.log2_throttle_limit, log2_num_entries); 575 return -EINVAL; 576 } 577 578 err = mana_gd_register_irq(queue, spec); 579 if (err) { 580 dev_err(dev, "Failed to register irq: %d\n", err); 581 return err; 582 } 583 584 queue->eq.callback = spec->eq.callback; 585 queue->eq.context = spec->eq.context; 586 queue->head |= INITIALIZED_OWNER_BIT(log2_num_entries); 587 queue->eq.log2_throttle_limit = spec->eq.log2_throttle_limit ?: 1; 588 589 if (create_hwq) { 590 err = mana_gd_create_hw_eq(gc, queue); 591 if (err) 592 goto out; 593 594 err = mana_gd_test_eq(gc, queue); 595 if (err) 596 goto out; 597 } 598 599 return 0; 600 out: 601 dev_err(dev, "Failed to create EQ: %d\n", err); 602 mana_gd_destroy_eq(gc, false, queue); 603 return err; 604 } 605 606 static void mana_gd_create_cq(const struct gdma_queue_spec *spec, 607 struct gdma_queue *queue) 608 { 609 u32 log2_num_entries = ilog2(spec->queue_size / GDMA_CQE_SIZE); 610 611 queue->head |= INITIALIZED_OWNER_BIT(log2_num_entries); 612 queue->cq.parent = spec->cq.parent_eq; 613 queue->cq.context = spec->cq.context; 614 queue->cq.callback = spec->cq.callback; 615 } 616 617 static void mana_gd_destroy_cq(struct gdma_context *gc, 618 struct gdma_queue *queue) 619 { 620 u32 id = queue->id; 621 622 if (id >= gc->max_num_cqs) 623 return; 624 625 if (!gc->cq_table[id]) 626 return; 627 628 gc->cq_table[id] = NULL; 629 } 630 631 int mana_gd_create_hwc_queue(struct gdma_dev *gd, 632 const struct gdma_queue_spec *spec, 633 struct gdma_queue **queue_ptr) 634 { 635 struct gdma_context *gc = gd->gdma_context; 636 struct gdma_mem_info *gmi; 637 struct gdma_queue *queue; 638 int err; 639 640 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 641 if (!queue) 642 return -ENOMEM; 643 644 gmi = &queue->mem_info; 645 err = mana_gd_alloc_memory(gc, spec->queue_size, gmi); 646 if (err) 647 goto free_q; 648 649 queue->head = 0; 650 queue->tail = 0; 651 queue->queue_mem_ptr = gmi->virt_addr; 652 queue->queue_size = spec->queue_size; 653 queue->monitor_avl_buf = spec->monitor_avl_buf; 654 queue->type = spec->type; 655 queue->gdma_dev = gd; 656 657 if (spec->type == GDMA_EQ) 658 err = mana_gd_create_eq(gd, spec, false, queue); 659 else if (spec->type == GDMA_CQ) 660 mana_gd_create_cq(spec, queue); 661 662 if (err) 663 goto out; 664 665 *queue_ptr = queue; 666 return 0; 667 out: 668 mana_gd_free_memory(gmi); 669 free_q: 670 kfree(queue); 671 return err; 672 } 673 674 int mana_gd_destroy_dma_region(struct gdma_context *gc, 675 gdma_obj_handle_t dma_region_handle) 676 { 677 struct gdma_destroy_dma_region_req req = {}; 678 struct gdma_general_resp resp = {}; 679 int err; 680 681 if (dma_region_handle == GDMA_INVALID_DMA_REGION) 682 return 0; 683 684 mana_gd_init_req_hdr(&req.hdr, GDMA_DESTROY_DMA_REGION, sizeof(req), 685 sizeof(resp)); 686 req.dma_region_handle = dma_region_handle; 687 688 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 689 if (err || resp.hdr.status) { 690 dev_err(gc->dev, "Failed to destroy DMA region: %d, 0x%x\n", 691 err, resp.hdr.status); 692 return -EPROTO; 693 } 694 695 return 0; 696 } 697 EXPORT_SYMBOL_NS(mana_gd_destroy_dma_region, NET_MANA); 698 699 static int mana_gd_create_dma_region(struct gdma_dev *gd, 700 struct gdma_mem_info *gmi) 701 { 702 unsigned int num_page = gmi->length / PAGE_SIZE; 703 struct gdma_create_dma_region_req *req = NULL; 704 struct gdma_create_dma_region_resp resp = {}; 705 struct gdma_context *gc = gd->gdma_context; 706 struct hw_channel_context *hwc; 707 u32 length = gmi->length; 708 size_t req_msg_size; 709 int err; 710 int i; 711 712 if (length < PAGE_SIZE || !is_power_of_2(length)) 713 return -EINVAL; 714 715 if (offset_in_page(gmi->virt_addr) != 0) 716 return -EINVAL; 717 718 hwc = gc->hwc.driver_data; 719 req_msg_size = struct_size(req, page_addr_list, num_page); 720 if (req_msg_size > hwc->max_req_msg_size) 721 return -EINVAL; 722 723 req = kzalloc(req_msg_size, GFP_KERNEL); 724 if (!req) 725 return -ENOMEM; 726 727 mana_gd_init_req_hdr(&req->hdr, GDMA_CREATE_DMA_REGION, 728 req_msg_size, sizeof(resp)); 729 req->length = length; 730 req->offset_in_page = 0; 731 req->gdma_page_type = GDMA_PAGE_TYPE_4K; 732 req->page_count = num_page; 733 req->page_addr_list_len = num_page; 734 735 for (i = 0; i < num_page; i++) 736 req->page_addr_list[i] = gmi->dma_handle + i * PAGE_SIZE; 737 738 err = mana_gd_send_request(gc, req_msg_size, req, sizeof(resp), &resp); 739 if (err) 740 goto out; 741 742 if (resp.hdr.status || 743 resp.dma_region_handle == GDMA_INVALID_DMA_REGION) { 744 dev_err(gc->dev, "Failed to create DMA region: 0x%x\n", 745 resp.hdr.status); 746 err = -EPROTO; 747 goto out; 748 } 749 750 gmi->dma_region_handle = resp.dma_region_handle; 751 out: 752 kfree(req); 753 return err; 754 } 755 756 int mana_gd_create_mana_eq(struct gdma_dev *gd, 757 const struct gdma_queue_spec *spec, 758 struct gdma_queue **queue_ptr) 759 { 760 struct gdma_context *gc = gd->gdma_context; 761 struct gdma_mem_info *gmi; 762 struct gdma_queue *queue; 763 int err; 764 765 if (spec->type != GDMA_EQ) 766 return -EINVAL; 767 768 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 769 if (!queue) 770 return -ENOMEM; 771 772 gmi = &queue->mem_info; 773 err = mana_gd_alloc_memory(gc, spec->queue_size, gmi); 774 if (err) 775 goto free_q; 776 777 err = mana_gd_create_dma_region(gd, gmi); 778 if (err) 779 goto out; 780 781 queue->head = 0; 782 queue->tail = 0; 783 queue->queue_mem_ptr = gmi->virt_addr; 784 queue->queue_size = spec->queue_size; 785 queue->monitor_avl_buf = spec->monitor_avl_buf; 786 queue->type = spec->type; 787 queue->gdma_dev = gd; 788 789 err = mana_gd_create_eq(gd, spec, true, queue); 790 if (err) 791 goto out; 792 793 *queue_ptr = queue; 794 return 0; 795 out: 796 mana_gd_free_memory(gmi); 797 free_q: 798 kfree(queue); 799 return err; 800 } 801 802 int mana_gd_create_mana_wq_cq(struct gdma_dev *gd, 803 const struct gdma_queue_spec *spec, 804 struct gdma_queue **queue_ptr) 805 { 806 struct gdma_context *gc = gd->gdma_context; 807 struct gdma_mem_info *gmi; 808 struct gdma_queue *queue; 809 int err; 810 811 if (spec->type != GDMA_CQ && spec->type != GDMA_SQ && 812 spec->type != GDMA_RQ) 813 return -EINVAL; 814 815 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 816 if (!queue) 817 return -ENOMEM; 818 819 gmi = &queue->mem_info; 820 err = mana_gd_alloc_memory(gc, spec->queue_size, gmi); 821 if (err) 822 goto free_q; 823 824 err = mana_gd_create_dma_region(gd, gmi); 825 if (err) 826 goto out; 827 828 queue->head = 0; 829 queue->tail = 0; 830 queue->queue_mem_ptr = gmi->virt_addr; 831 queue->queue_size = spec->queue_size; 832 queue->monitor_avl_buf = spec->monitor_avl_buf; 833 queue->type = spec->type; 834 queue->gdma_dev = gd; 835 836 if (spec->type == GDMA_CQ) 837 mana_gd_create_cq(spec, queue); 838 839 *queue_ptr = queue; 840 return 0; 841 out: 842 mana_gd_free_memory(gmi); 843 free_q: 844 kfree(queue); 845 return err; 846 } 847 848 void mana_gd_destroy_queue(struct gdma_context *gc, struct gdma_queue *queue) 849 { 850 struct gdma_mem_info *gmi = &queue->mem_info; 851 852 switch (queue->type) { 853 case GDMA_EQ: 854 mana_gd_destroy_eq(gc, queue->eq.disable_needed, queue); 855 break; 856 857 case GDMA_CQ: 858 mana_gd_destroy_cq(gc, queue); 859 break; 860 861 case GDMA_RQ: 862 break; 863 864 case GDMA_SQ: 865 break; 866 867 default: 868 dev_err(gc->dev, "Can't destroy unknown queue: type=%d\n", 869 queue->type); 870 return; 871 } 872 873 mana_gd_destroy_dma_region(gc, gmi->dma_region_handle); 874 mana_gd_free_memory(gmi); 875 kfree(queue); 876 } 877 878 int mana_gd_verify_vf_version(struct pci_dev *pdev) 879 { 880 struct gdma_context *gc = pci_get_drvdata(pdev); 881 struct gdma_verify_ver_resp resp = {}; 882 struct gdma_verify_ver_req req = {}; 883 int err; 884 885 mana_gd_init_req_hdr(&req.hdr, GDMA_VERIFY_VF_DRIVER_VERSION, 886 sizeof(req), sizeof(resp)); 887 888 req.protocol_ver_min = GDMA_PROTOCOL_FIRST; 889 req.protocol_ver_max = GDMA_PROTOCOL_LAST; 890 891 req.gd_drv_cap_flags1 = GDMA_DRV_CAP_FLAGS1; 892 req.gd_drv_cap_flags2 = GDMA_DRV_CAP_FLAGS2; 893 req.gd_drv_cap_flags3 = GDMA_DRV_CAP_FLAGS3; 894 req.gd_drv_cap_flags4 = GDMA_DRV_CAP_FLAGS4; 895 896 req.drv_ver = 0; /* Unused*/ 897 req.os_type = 0x10; /* Linux */ 898 req.os_ver_major = LINUX_VERSION_MAJOR; 899 req.os_ver_minor = LINUX_VERSION_PATCHLEVEL; 900 req.os_ver_build = LINUX_VERSION_SUBLEVEL; 901 strscpy(req.os_ver_str1, utsname()->sysname, sizeof(req.os_ver_str1)); 902 strscpy(req.os_ver_str2, utsname()->release, sizeof(req.os_ver_str2)); 903 strscpy(req.os_ver_str3, utsname()->version, sizeof(req.os_ver_str3)); 904 905 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 906 if (err || resp.hdr.status) { 907 dev_err(gc->dev, "VfVerifyVersionOutput: %d, status=0x%x\n", 908 err, resp.hdr.status); 909 return err ? err : -EPROTO; 910 } 911 912 return 0; 913 } 914 915 int mana_gd_register_device(struct gdma_dev *gd) 916 { 917 struct gdma_context *gc = gd->gdma_context; 918 struct gdma_register_device_resp resp = {}; 919 struct gdma_general_req req = {}; 920 int err; 921 922 gd->pdid = INVALID_PDID; 923 gd->doorbell = INVALID_DOORBELL; 924 gd->gpa_mkey = INVALID_MEM_KEY; 925 926 mana_gd_init_req_hdr(&req.hdr, GDMA_REGISTER_DEVICE, sizeof(req), 927 sizeof(resp)); 928 929 req.hdr.dev_id = gd->dev_id; 930 931 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 932 if (err || resp.hdr.status) { 933 dev_err(gc->dev, "gdma_register_device_resp failed: %d, 0x%x\n", 934 err, resp.hdr.status); 935 return err ? err : -EPROTO; 936 } 937 938 gd->pdid = resp.pdid; 939 gd->gpa_mkey = resp.gpa_mkey; 940 gd->doorbell = resp.db_id; 941 942 return 0; 943 } 944 945 int mana_gd_deregister_device(struct gdma_dev *gd) 946 { 947 struct gdma_context *gc = gd->gdma_context; 948 struct gdma_general_resp resp = {}; 949 struct gdma_general_req req = {}; 950 int err; 951 952 if (gd->pdid == INVALID_PDID) 953 return -EINVAL; 954 955 mana_gd_init_req_hdr(&req.hdr, GDMA_DEREGISTER_DEVICE, sizeof(req), 956 sizeof(resp)); 957 958 req.hdr.dev_id = gd->dev_id; 959 960 err = mana_gd_send_request(gc, sizeof(req), &req, sizeof(resp), &resp); 961 if (err || resp.hdr.status) { 962 dev_err(gc->dev, "Failed to deregister device: %d, 0x%x\n", 963 err, resp.hdr.status); 964 if (!err) 965 err = -EPROTO; 966 } 967 968 gd->pdid = INVALID_PDID; 969 gd->doorbell = INVALID_DOORBELL; 970 gd->gpa_mkey = INVALID_MEM_KEY; 971 972 return err; 973 } 974 975 u32 mana_gd_wq_avail_space(struct gdma_queue *wq) 976 { 977 u32 used_space = (wq->head - wq->tail) * GDMA_WQE_BU_SIZE; 978 u32 wq_size = wq->queue_size; 979 980 WARN_ON_ONCE(used_space > wq_size); 981 982 return wq_size - used_space; 983 } 984 985 u8 *mana_gd_get_wqe_ptr(const struct gdma_queue *wq, u32 wqe_offset) 986 { 987 u32 offset = (wqe_offset * GDMA_WQE_BU_SIZE) & (wq->queue_size - 1); 988 989 WARN_ON_ONCE((offset + GDMA_WQE_BU_SIZE) > wq->queue_size); 990 991 return wq->queue_mem_ptr + offset; 992 } 993 994 static u32 mana_gd_write_client_oob(const struct gdma_wqe_request *wqe_req, 995 enum gdma_queue_type q_type, 996 u32 client_oob_size, u32 sgl_data_size, 997 u8 *wqe_ptr) 998 { 999 bool oob_in_sgl = !!(wqe_req->flags & GDMA_WR_OOB_IN_SGL); 1000 bool pad_data = !!(wqe_req->flags & GDMA_WR_PAD_BY_SGE0); 1001 struct gdma_wqe *header = (struct gdma_wqe *)wqe_ptr; 1002 u8 *ptr; 1003 1004 memset(header, 0, sizeof(struct gdma_wqe)); 1005 header->num_sge = wqe_req->num_sge; 1006 header->inline_oob_size_div4 = client_oob_size / sizeof(u32); 1007 1008 if (oob_in_sgl) { 1009 WARN_ON_ONCE(!pad_data || wqe_req->num_sge < 2); 1010 1011 header->client_oob_in_sgl = 1; 1012 1013 if (pad_data) 1014 header->last_vbytes = wqe_req->sgl[0].size; 1015 } 1016 1017 if (q_type == GDMA_SQ) 1018 header->client_data_unit = wqe_req->client_data_unit; 1019 1020 /* The size of gdma_wqe + client_oob_size must be less than or equal 1021 * to one Basic Unit (i.e. 32 bytes), so the pointer can't go beyond 1022 * the queue memory buffer boundary. 1023 */ 1024 ptr = wqe_ptr + sizeof(header); 1025 1026 if (wqe_req->inline_oob_data && wqe_req->inline_oob_size > 0) { 1027 memcpy(ptr, wqe_req->inline_oob_data, wqe_req->inline_oob_size); 1028 1029 if (client_oob_size > wqe_req->inline_oob_size) 1030 memset(ptr + wqe_req->inline_oob_size, 0, 1031 client_oob_size - wqe_req->inline_oob_size); 1032 } 1033 1034 return sizeof(header) + client_oob_size; 1035 } 1036 1037 static void mana_gd_write_sgl(struct gdma_queue *wq, u8 *wqe_ptr, 1038 const struct gdma_wqe_request *wqe_req) 1039 { 1040 u32 sgl_size = sizeof(struct gdma_sge) * wqe_req->num_sge; 1041 const u8 *address = (u8 *)wqe_req->sgl; 1042 u8 *base_ptr, *end_ptr; 1043 u32 size_to_end; 1044 1045 base_ptr = wq->queue_mem_ptr; 1046 end_ptr = base_ptr + wq->queue_size; 1047 size_to_end = (u32)(end_ptr - wqe_ptr); 1048 1049 if (size_to_end < sgl_size) { 1050 memcpy(wqe_ptr, address, size_to_end); 1051 1052 wqe_ptr = base_ptr; 1053 address += size_to_end; 1054 sgl_size -= size_to_end; 1055 } 1056 1057 memcpy(wqe_ptr, address, sgl_size); 1058 } 1059 1060 int mana_gd_post_work_request(struct gdma_queue *wq, 1061 const struct gdma_wqe_request *wqe_req, 1062 struct gdma_posted_wqe_info *wqe_info) 1063 { 1064 u32 client_oob_size = wqe_req->inline_oob_size; 1065 struct gdma_context *gc; 1066 u32 sgl_data_size; 1067 u32 max_wqe_size; 1068 u32 wqe_size; 1069 u8 *wqe_ptr; 1070 1071 if (wqe_req->num_sge == 0) 1072 return -EINVAL; 1073 1074 if (wq->type == GDMA_RQ) { 1075 if (client_oob_size != 0) 1076 return -EINVAL; 1077 1078 client_oob_size = INLINE_OOB_SMALL_SIZE; 1079 1080 max_wqe_size = GDMA_MAX_RQE_SIZE; 1081 } else { 1082 if (client_oob_size != INLINE_OOB_SMALL_SIZE && 1083 client_oob_size != INLINE_OOB_LARGE_SIZE) 1084 return -EINVAL; 1085 1086 max_wqe_size = GDMA_MAX_SQE_SIZE; 1087 } 1088 1089 sgl_data_size = sizeof(struct gdma_sge) * wqe_req->num_sge; 1090 wqe_size = ALIGN(sizeof(struct gdma_wqe) + client_oob_size + 1091 sgl_data_size, GDMA_WQE_BU_SIZE); 1092 if (wqe_size > max_wqe_size) 1093 return -EINVAL; 1094 1095 if (wq->monitor_avl_buf && wqe_size > mana_gd_wq_avail_space(wq)) { 1096 gc = wq->gdma_dev->gdma_context; 1097 dev_err(gc->dev, "unsuccessful flow control!\n"); 1098 return -ENOSPC; 1099 } 1100 1101 if (wqe_info) 1102 wqe_info->wqe_size_in_bu = wqe_size / GDMA_WQE_BU_SIZE; 1103 1104 wqe_ptr = mana_gd_get_wqe_ptr(wq, wq->head); 1105 wqe_ptr += mana_gd_write_client_oob(wqe_req, wq->type, client_oob_size, 1106 sgl_data_size, wqe_ptr); 1107 if (wqe_ptr >= (u8 *)wq->queue_mem_ptr + wq->queue_size) 1108 wqe_ptr -= wq->queue_size; 1109 1110 mana_gd_write_sgl(wq, wqe_ptr, wqe_req); 1111 1112 wq->head += wqe_size / GDMA_WQE_BU_SIZE; 1113 1114 return 0; 1115 } 1116 1117 int mana_gd_post_and_ring(struct gdma_queue *queue, 1118 const struct gdma_wqe_request *wqe_req, 1119 struct gdma_posted_wqe_info *wqe_info) 1120 { 1121 struct gdma_context *gc = queue->gdma_dev->gdma_context; 1122 int err; 1123 1124 err = mana_gd_post_work_request(queue, wqe_req, wqe_info); 1125 if (err) 1126 return err; 1127 1128 mana_gd_wq_ring_doorbell(gc, queue); 1129 1130 return 0; 1131 } 1132 1133 static int mana_gd_read_cqe(struct gdma_queue *cq, struct gdma_comp *comp) 1134 { 1135 unsigned int num_cqe = cq->queue_size / sizeof(struct gdma_cqe); 1136 struct gdma_cqe *cq_cqe = cq->queue_mem_ptr; 1137 u32 owner_bits, new_bits, old_bits; 1138 struct gdma_cqe *cqe; 1139 1140 cqe = &cq_cqe[cq->head % num_cqe]; 1141 owner_bits = cqe->cqe_info.owner_bits; 1142 1143 old_bits = (cq->head / num_cqe - 1) & GDMA_CQE_OWNER_MASK; 1144 /* Return 0 if no more entries. */ 1145 if (owner_bits == old_bits) 1146 return 0; 1147 1148 new_bits = (cq->head / num_cqe) & GDMA_CQE_OWNER_MASK; 1149 /* Return -1 if overflow detected. */ 1150 if (WARN_ON_ONCE(owner_bits != new_bits)) 1151 return -1; 1152 1153 /* Per GDMA spec, rmb is necessary after checking owner_bits, before 1154 * reading completion info 1155 */ 1156 rmb(); 1157 1158 comp->wq_num = cqe->cqe_info.wq_num; 1159 comp->is_sq = cqe->cqe_info.is_sq; 1160 memcpy(comp->cqe_data, cqe->cqe_data, GDMA_COMP_DATA_SIZE); 1161 1162 return 1; 1163 } 1164 1165 int mana_gd_poll_cq(struct gdma_queue *cq, struct gdma_comp *comp, int num_cqe) 1166 { 1167 int cqe_idx; 1168 int ret; 1169 1170 for (cqe_idx = 0; cqe_idx < num_cqe; cqe_idx++) { 1171 ret = mana_gd_read_cqe(cq, &comp[cqe_idx]); 1172 1173 if (ret < 0) { 1174 cq->head -= cqe_idx; 1175 return ret; 1176 } 1177 1178 if (ret == 0) 1179 break; 1180 1181 cq->head++; 1182 } 1183 1184 return cqe_idx; 1185 } 1186 1187 static irqreturn_t mana_gd_intr(int irq, void *arg) 1188 { 1189 struct gdma_irq_context *gic = arg; 1190 1191 if (gic->handler) 1192 gic->handler(gic->arg); 1193 1194 return IRQ_HANDLED; 1195 } 1196 1197 int mana_gd_alloc_res_map(u32 res_avail, struct gdma_resource *r) 1198 { 1199 r->map = bitmap_zalloc(res_avail, GFP_KERNEL); 1200 if (!r->map) 1201 return -ENOMEM; 1202 1203 r->size = res_avail; 1204 spin_lock_init(&r->lock); 1205 1206 return 0; 1207 } 1208 1209 void mana_gd_free_res_map(struct gdma_resource *r) 1210 { 1211 bitmap_free(r->map); 1212 r->map = NULL; 1213 r->size = 0; 1214 } 1215 1216 static int mana_gd_setup_irqs(struct pci_dev *pdev) 1217 { 1218 unsigned int max_queues_per_port = num_online_cpus(); 1219 struct gdma_context *gc = pci_get_drvdata(pdev); 1220 struct gdma_irq_context *gic; 1221 unsigned int max_irqs; 1222 u16 *cpus; 1223 cpumask_var_t req_mask; 1224 int nvec, irq; 1225 int err, i = 0, j; 1226 1227 if (max_queues_per_port > MANA_MAX_NUM_QUEUES) 1228 max_queues_per_port = MANA_MAX_NUM_QUEUES; 1229 1230 /* Need 1 interrupt for the Hardware communication Channel (HWC) */ 1231 max_irqs = max_queues_per_port + 1; 1232 1233 nvec = pci_alloc_irq_vectors(pdev, 2, max_irqs, PCI_IRQ_MSIX); 1234 if (nvec < 0) 1235 return nvec; 1236 1237 gc->irq_contexts = kcalloc(nvec, sizeof(struct gdma_irq_context), 1238 GFP_KERNEL); 1239 if (!gc->irq_contexts) { 1240 err = -ENOMEM; 1241 goto free_irq_vector; 1242 } 1243 1244 if (!zalloc_cpumask_var(&req_mask, GFP_KERNEL)) { 1245 err = -ENOMEM; 1246 goto free_irq; 1247 } 1248 1249 cpus = kcalloc(nvec, sizeof(*cpus), GFP_KERNEL); 1250 if (!cpus) { 1251 err = -ENOMEM; 1252 goto free_mask; 1253 } 1254 for (i = 0; i < nvec; i++) 1255 cpus[i] = cpumask_local_spread(i, gc->numa_node); 1256 1257 for (i = 0; i < nvec; i++) { 1258 cpumask_set_cpu(cpus[i], req_mask); 1259 gic = &gc->irq_contexts[i]; 1260 gic->handler = NULL; 1261 gic->arg = NULL; 1262 1263 irq = pci_irq_vector(pdev, i); 1264 if (irq < 0) { 1265 err = irq; 1266 goto free_mask; 1267 } 1268 1269 err = request_irq(irq, mana_gd_intr, 0, "mana_intr", gic); 1270 if (err) 1271 goto free_mask; 1272 irq_set_affinity_and_hint(irq, req_mask); 1273 cpumask_clear(req_mask); 1274 } 1275 free_cpumask_var(req_mask); 1276 kfree(cpus); 1277 1278 err = mana_gd_alloc_res_map(nvec, &gc->msix_resource); 1279 if (err) 1280 goto free_irq; 1281 1282 gc->max_num_msix = nvec; 1283 gc->num_msix_usable = nvec; 1284 1285 return 0; 1286 1287 free_mask: 1288 free_cpumask_var(req_mask); 1289 kfree(cpus); 1290 free_irq: 1291 for (j = i - 1; j >= 0; j--) { 1292 irq = pci_irq_vector(pdev, j); 1293 gic = &gc->irq_contexts[j]; 1294 free_irq(irq, gic); 1295 } 1296 1297 kfree(gc->irq_contexts); 1298 gc->irq_contexts = NULL; 1299 free_irq_vector: 1300 pci_free_irq_vectors(pdev); 1301 return err; 1302 } 1303 1304 static void mana_gd_remove_irqs(struct pci_dev *pdev) 1305 { 1306 struct gdma_context *gc = pci_get_drvdata(pdev); 1307 struct gdma_irq_context *gic; 1308 int irq, i; 1309 1310 if (gc->max_num_msix < 1) 1311 return; 1312 1313 mana_gd_free_res_map(&gc->msix_resource); 1314 1315 for (i = 0; i < gc->max_num_msix; i++) { 1316 irq = pci_irq_vector(pdev, i); 1317 if (irq < 0) 1318 continue; 1319 1320 gic = &gc->irq_contexts[i]; 1321 free_irq(irq, gic); 1322 } 1323 1324 pci_free_irq_vectors(pdev); 1325 1326 gc->max_num_msix = 0; 1327 gc->num_msix_usable = 0; 1328 kfree(gc->irq_contexts); 1329 gc->irq_contexts = NULL; 1330 } 1331 1332 static int mana_gd_setup(struct pci_dev *pdev) 1333 { 1334 struct gdma_context *gc = pci_get_drvdata(pdev); 1335 int err; 1336 1337 mana_gd_init_registers(pdev); 1338 mana_smc_init(&gc->shm_channel, gc->dev, gc->shm_base); 1339 1340 err = mana_gd_setup_irqs(pdev); 1341 if (err) 1342 return err; 1343 1344 err = mana_hwc_create_channel(gc); 1345 if (err) 1346 goto remove_irq; 1347 1348 err = mana_gd_verify_vf_version(pdev); 1349 if (err) 1350 goto destroy_hwc; 1351 1352 err = mana_gd_query_max_resources(pdev); 1353 if (err) 1354 goto destroy_hwc; 1355 1356 err = mana_gd_detect_devices(pdev); 1357 if (err) 1358 goto destroy_hwc; 1359 1360 return 0; 1361 1362 destroy_hwc: 1363 mana_hwc_destroy_channel(gc); 1364 remove_irq: 1365 mana_gd_remove_irqs(pdev); 1366 return err; 1367 } 1368 1369 static void mana_gd_cleanup(struct pci_dev *pdev) 1370 { 1371 struct gdma_context *gc = pci_get_drvdata(pdev); 1372 1373 mana_hwc_destroy_channel(gc); 1374 1375 mana_gd_remove_irqs(pdev); 1376 } 1377 1378 static bool mana_is_pf(unsigned short dev_id) 1379 { 1380 return dev_id == MANA_PF_DEVICE_ID; 1381 } 1382 1383 static int mana_gd_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1384 { 1385 struct gdma_context *gc; 1386 void __iomem *bar0_va; 1387 int bar = 0; 1388 int err; 1389 1390 /* Each port has 2 CQs, each CQ has at most 1 EQE at a time */ 1391 BUILD_BUG_ON(2 * MAX_PORTS_IN_MANA_DEV * GDMA_EQE_SIZE > EQ_SIZE); 1392 1393 err = pci_enable_device(pdev); 1394 if (err) 1395 return -ENXIO; 1396 1397 pci_set_master(pdev); 1398 1399 err = pci_request_regions(pdev, "mana"); 1400 if (err) 1401 goto disable_dev; 1402 1403 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 1404 if (err) 1405 goto release_region; 1406 1407 err = dma_set_max_seg_size(&pdev->dev, UINT_MAX); 1408 if (err) { 1409 dev_err(&pdev->dev, "Failed to set dma device segment size\n"); 1410 goto release_region; 1411 } 1412 1413 err = -ENOMEM; 1414 gc = vzalloc(sizeof(*gc)); 1415 if (!gc) 1416 goto release_region; 1417 1418 mutex_init(&gc->eq_test_event_mutex); 1419 pci_set_drvdata(pdev, gc); 1420 gc->bar0_pa = pci_resource_start(pdev, 0); 1421 1422 bar0_va = pci_iomap(pdev, bar, 0); 1423 if (!bar0_va) 1424 goto free_gc; 1425 1426 gc->numa_node = dev_to_node(&pdev->dev); 1427 gc->is_pf = mana_is_pf(pdev->device); 1428 gc->bar0_va = bar0_va; 1429 gc->dev = &pdev->dev; 1430 1431 err = mana_gd_setup(pdev); 1432 if (err) 1433 goto unmap_bar; 1434 1435 err = mana_probe(&gc->mana, false); 1436 if (err) 1437 goto cleanup_gd; 1438 1439 return 0; 1440 1441 cleanup_gd: 1442 mana_gd_cleanup(pdev); 1443 unmap_bar: 1444 pci_iounmap(pdev, bar0_va); 1445 free_gc: 1446 pci_set_drvdata(pdev, NULL); 1447 vfree(gc); 1448 release_region: 1449 pci_release_regions(pdev); 1450 disable_dev: 1451 pci_clear_master(pdev); 1452 pci_disable_device(pdev); 1453 dev_err(&pdev->dev, "gdma probe failed: err = %d\n", err); 1454 return err; 1455 } 1456 1457 static void mana_gd_remove(struct pci_dev *pdev) 1458 { 1459 struct gdma_context *gc = pci_get_drvdata(pdev); 1460 1461 mana_remove(&gc->mana, false); 1462 1463 mana_gd_cleanup(pdev); 1464 1465 pci_iounmap(pdev, gc->bar0_va); 1466 1467 vfree(gc); 1468 1469 pci_release_regions(pdev); 1470 pci_clear_master(pdev); 1471 pci_disable_device(pdev); 1472 } 1473 1474 /* The 'state' parameter is not used. */ 1475 static int mana_gd_suspend(struct pci_dev *pdev, pm_message_t state) 1476 { 1477 struct gdma_context *gc = pci_get_drvdata(pdev); 1478 1479 mana_remove(&gc->mana, true); 1480 1481 mana_gd_cleanup(pdev); 1482 1483 return 0; 1484 } 1485 1486 /* In case the NIC hardware stops working, the suspend and resume callbacks will 1487 * fail -- if this happens, it's safer to just report an error than try to undo 1488 * what has been done. 1489 */ 1490 static int mana_gd_resume(struct pci_dev *pdev) 1491 { 1492 struct gdma_context *gc = pci_get_drvdata(pdev); 1493 int err; 1494 1495 err = mana_gd_setup(pdev); 1496 if (err) 1497 return err; 1498 1499 err = mana_probe(&gc->mana, true); 1500 if (err) 1501 return err; 1502 1503 return 0; 1504 } 1505 1506 /* Quiesce the device for kexec. This is also called upon reboot/shutdown. */ 1507 static void mana_gd_shutdown(struct pci_dev *pdev) 1508 { 1509 struct gdma_context *gc = pci_get_drvdata(pdev); 1510 1511 dev_info(&pdev->dev, "Shutdown was called\n"); 1512 1513 mana_remove(&gc->mana, true); 1514 1515 mana_gd_cleanup(pdev); 1516 1517 pci_disable_device(pdev); 1518 } 1519 1520 static const struct pci_device_id mana_id_table[] = { 1521 { PCI_DEVICE(PCI_VENDOR_ID_MICROSOFT, MANA_PF_DEVICE_ID) }, 1522 { PCI_DEVICE(PCI_VENDOR_ID_MICROSOFT, MANA_VF_DEVICE_ID) }, 1523 { } 1524 }; 1525 1526 static struct pci_driver mana_driver = { 1527 .name = "mana", 1528 .id_table = mana_id_table, 1529 .probe = mana_gd_probe, 1530 .remove = mana_gd_remove, 1531 .suspend = mana_gd_suspend, 1532 .resume = mana_gd_resume, 1533 .shutdown = mana_gd_shutdown, 1534 }; 1535 1536 module_pci_driver(mana_driver); 1537 1538 MODULE_DEVICE_TABLE(pci, mana_id_table); 1539 1540 MODULE_LICENSE("Dual BSD/GPL"); 1541 MODULE_DESCRIPTION("Microsoft Azure Network Adapter driver"); 1542