1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2 /* 3 * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #ifdef CONFIG_RFS_ACCEL 9 #include <linux/cpu_rmap.h> 10 #endif /* CONFIG_RFS_ACCEL */ 11 #include <linux/ethtool.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/numa.h> 15 #include <linux/pci.h> 16 #include <linux/utsname.h> 17 #include <linux/version.h> 18 #include <linux/vmalloc.h> 19 #include <net/ip.h> 20 21 #include "ena_netdev.h" 22 #include "ena_pci_id_tbl.h" 23 #include "ena_xdp.h" 24 25 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates"); 26 MODULE_DESCRIPTION(DEVICE_NAME); 27 MODULE_LICENSE("GPL"); 28 29 /* Time in jiffies before concluding the transmitter is hung. */ 30 #define TX_TIMEOUT (5 * HZ) 31 32 #define ENA_MAX_RINGS min_t(unsigned int, ENA_MAX_NUM_IO_QUEUES, num_possible_cpus()) 33 34 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \ 35 NETIF_MSG_TX_DONE | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR) 36 37 static struct ena_aenq_handlers aenq_handlers; 38 39 static struct workqueue_struct *ena_wq; 40 41 MODULE_DEVICE_TABLE(pci, ena_pci_tbl); 42 43 static int ena_rss_init_default(struct ena_adapter *adapter); 44 static void check_for_admin_com_state(struct ena_adapter *adapter); 45 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful); 46 static int ena_restore_device(struct ena_adapter *adapter); 47 48 static void ena_tx_timeout(struct net_device *dev, unsigned int txqueue) 49 { 50 struct ena_adapter *adapter = netdev_priv(dev); 51 52 /* Change the state of the device to trigger reset 53 * Check that we are not in the middle or a trigger already 54 */ 55 56 if (test_and_set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) 57 return; 58 59 ena_reset_device(adapter, ENA_REGS_RESET_OS_NETDEV_WD); 60 ena_increase_stat(&adapter->dev_stats.tx_timeout, 1, &adapter->syncp); 61 62 netif_err(adapter, tx_err, dev, "Transmit time out\n"); 63 } 64 65 static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu) 66 { 67 int i; 68 69 for (i = 0; i < adapter->num_io_queues; i++) 70 adapter->rx_ring[i].mtu = mtu; 71 } 72 73 static int ena_change_mtu(struct net_device *dev, int new_mtu) 74 { 75 struct ena_adapter *adapter = netdev_priv(dev); 76 int ret; 77 78 ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu); 79 if (!ret) { 80 netif_dbg(adapter, drv, dev, "Set MTU to %d\n", new_mtu); 81 update_rx_ring_mtu(adapter, new_mtu); 82 dev->mtu = new_mtu; 83 } else { 84 netif_err(adapter, drv, dev, "Failed to set MTU to %d\n", 85 new_mtu); 86 } 87 88 return ret; 89 } 90 91 int ena_xmit_common(struct ena_adapter *adapter, 92 struct ena_ring *ring, 93 struct ena_tx_buffer *tx_info, 94 struct ena_com_tx_ctx *ena_tx_ctx, 95 u16 next_to_use, 96 u32 bytes) 97 { 98 int rc, nb_hw_desc; 99 100 if (unlikely(ena_com_is_doorbell_needed(ring->ena_com_io_sq, 101 ena_tx_ctx))) { 102 netif_dbg(adapter, tx_queued, adapter->netdev, 103 "llq tx max burst size of queue %d achieved, writing doorbell to send burst\n", 104 ring->qid); 105 ena_ring_tx_doorbell(ring); 106 } 107 108 /* prepare the packet's descriptors to dma engine */ 109 rc = ena_com_prepare_tx(ring->ena_com_io_sq, ena_tx_ctx, 110 &nb_hw_desc); 111 112 /* In case there isn't enough space in the queue for the packet, 113 * we simply drop it. All other failure reasons of 114 * ena_com_prepare_tx() are fatal and therefore require a device reset. 115 */ 116 if (unlikely(rc)) { 117 netif_err(adapter, tx_queued, adapter->netdev, 118 "Failed to prepare tx bufs\n"); 119 ena_increase_stat(&ring->tx_stats.prepare_ctx_err, 1, 120 &ring->syncp); 121 if (rc != -ENOMEM) 122 ena_reset_device(adapter, 123 ENA_REGS_RESET_DRIVER_INVALID_STATE); 124 return rc; 125 } 126 127 u64_stats_update_begin(&ring->syncp); 128 ring->tx_stats.cnt++; 129 ring->tx_stats.bytes += bytes; 130 u64_stats_update_end(&ring->syncp); 131 132 tx_info->tx_descs = nb_hw_desc; 133 tx_info->last_jiffies = jiffies; 134 tx_info->print_once = 0; 135 136 ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use, 137 ring->ring_size); 138 return 0; 139 } 140 141 static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter) 142 { 143 #ifdef CONFIG_RFS_ACCEL 144 u32 i; 145 int rc; 146 147 adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_io_queues); 148 if (!adapter->netdev->rx_cpu_rmap) 149 return -ENOMEM; 150 for (i = 0; i < adapter->num_io_queues; i++) { 151 int irq_idx = ENA_IO_IRQ_IDX(i); 152 153 rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap, 154 pci_irq_vector(adapter->pdev, irq_idx)); 155 if (rc) { 156 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap); 157 adapter->netdev->rx_cpu_rmap = NULL; 158 return rc; 159 } 160 } 161 #endif /* CONFIG_RFS_ACCEL */ 162 return 0; 163 } 164 165 static void ena_init_io_rings_common(struct ena_adapter *adapter, 166 struct ena_ring *ring, u16 qid) 167 { 168 ring->qid = qid; 169 ring->pdev = adapter->pdev; 170 ring->dev = &adapter->pdev->dev; 171 ring->netdev = adapter->netdev; 172 ring->napi = &adapter->ena_napi[qid].napi; 173 ring->adapter = adapter; 174 ring->ena_dev = adapter->ena_dev; 175 ring->per_napi_packets = 0; 176 ring->cpu = 0; 177 ring->numa_node = 0; 178 ring->no_interrupt_event_cnt = 0; 179 u64_stats_init(&ring->syncp); 180 } 181 182 void ena_init_io_rings(struct ena_adapter *adapter, 183 int first_index, int count) 184 { 185 struct ena_com_dev *ena_dev; 186 struct ena_ring *txr, *rxr; 187 int i; 188 189 ena_dev = adapter->ena_dev; 190 191 for (i = first_index; i < first_index + count; i++) { 192 txr = &adapter->tx_ring[i]; 193 rxr = &adapter->rx_ring[i]; 194 195 /* TX common ring state */ 196 ena_init_io_rings_common(adapter, txr, i); 197 198 /* TX specific ring state */ 199 txr->ring_size = adapter->requested_tx_ring_size; 200 txr->tx_max_header_size = ena_dev->tx_max_header_size; 201 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type; 202 txr->sgl_size = adapter->max_tx_sgl_size; 203 txr->smoothed_interval = 204 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev); 205 txr->disable_meta_caching = adapter->disable_meta_caching; 206 spin_lock_init(&txr->xdp_tx_lock); 207 208 /* Don't init RX queues for xdp queues */ 209 if (!ENA_IS_XDP_INDEX(adapter, i)) { 210 /* RX common ring state */ 211 ena_init_io_rings_common(adapter, rxr, i); 212 213 /* RX specific ring state */ 214 rxr->ring_size = adapter->requested_rx_ring_size; 215 rxr->rx_copybreak = adapter->rx_copybreak; 216 rxr->sgl_size = adapter->max_rx_sgl_size; 217 rxr->smoothed_interval = 218 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev); 219 rxr->empty_rx_queue = 0; 220 rxr->rx_headroom = NET_SKB_PAD; 221 adapter->ena_napi[i].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE; 222 rxr->xdp_ring = &adapter->tx_ring[i + adapter->num_io_queues]; 223 } 224 } 225 } 226 227 /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors) 228 * @adapter: network interface device structure 229 * @qid: queue index 230 * 231 * Return 0 on success, negative on failure 232 */ 233 static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid) 234 { 235 struct ena_ring *tx_ring = &adapter->tx_ring[qid]; 236 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)]; 237 int size, i, node; 238 239 if (tx_ring->tx_buffer_info) { 240 netif_err(adapter, ifup, 241 adapter->netdev, "tx_buffer_info info is not NULL"); 242 return -EEXIST; 243 } 244 245 size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size; 246 node = cpu_to_node(ena_irq->cpu); 247 248 tx_ring->tx_buffer_info = vzalloc_node(size, node); 249 if (!tx_ring->tx_buffer_info) { 250 tx_ring->tx_buffer_info = vzalloc(size); 251 if (!tx_ring->tx_buffer_info) 252 goto err_tx_buffer_info; 253 } 254 255 size = sizeof(u16) * tx_ring->ring_size; 256 tx_ring->free_ids = vzalloc_node(size, node); 257 if (!tx_ring->free_ids) { 258 tx_ring->free_ids = vzalloc(size); 259 if (!tx_ring->free_ids) 260 goto err_tx_free_ids; 261 } 262 263 size = tx_ring->tx_max_header_size; 264 tx_ring->push_buf_intermediate_buf = vzalloc_node(size, node); 265 if (!tx_ring->push_buf_intermediate_buf) { 266 tx_ring->push_buf_intermediate_buf = vzalloc(size); 267 if (!tx_ring->push_buf_intermediate_buf) 268 goto err_push_buf_intermediate_buf; 269 } 270 271 /* Req id ring for TX out of order completions */ 272 for (i = 0; i < tx_ring->ring_size; i++) 273 tx_ring->free_ids[i] = i; 274 275 /* Reset tx statistics */ 276 memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats)); 277 278 tx_ring->next_to_use = 0; 279 tx_ring->next_to_clean = 0; 280 tx_ring->cpu = ena_irq->cpu; 281 tx_ring->numa_node = node; 282 return 0; 283 284 err_push_buf_intermediate_buf: 285 vfree(tx_ring->free_ids); 286 tx_ring->free_ids = NULL; 287 err_tx_free_ids: 288 vfree(tx_ring->tx_buffer_info); 289 tx_ring->tx_buffer_info = NULL; 290 err_tx_buffer_info: 291 return -ENOMEM; 292 } 293 294 /* ena_free_tx_resources - Free I/O Tx Resources per Queue 295 * @adapter: network interface device structure 296 * @qid: queue index 297 * 298 * Free all transmit software resources 299 */ 300 static void ena_free_tx_resources(struct ena_adapter *adapter, int qid) 301 { 302 struct ena_ring *tx_ring = &adapter->tx_ring[qid]; 303 304 vfree(tx_ring->tx_buffer_info); 305 tx_ring->tx_buffer_info = NULL; 306 307 vfree(tx_ring->free_ids); 308 tx_ring->free_ids = NULL; 309 310 vfree(tx_ring->push_buf_intermediate_buf); 311 tx_ring->push_buf_intermediate_buf = NULL; 312 } 313 314 int ena_setup_tx_resources_in_range(struct ena_adapter *adapter, 315 int first_index, int count) 316 { 317 int i, rc = 0; 318 319 for (i = first_index; i < first_index + count; i++) { 320 rc = ena_setup_tx_resources(adapter, i); 321 if (rc) 322 goto err_setup_tx; 323 } 324 325 return 0; 326 327 err_setup_tx: 328 329 netif_err(adapter, ifup, adapter->netdev, 330 "Tx queue %d: allocation failed\n", i); 331 332 /* rewind the index freeing the rings as we go */ 333 while (first_index < i--) 334 ena_free_tx_resources(adapter, i); 335 return rc; 336 } 337 338 void ena_free_all_io_tx_resources_in_range(struct ena_adapter *adapter, 339 int first_index, int count) 340 { 341 int i; 342 343 for (i = first_index; i < first_index + count; i++) 344 ena_free_tx_resources(adapter, i); 345 } 346 347 /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues 348 * @adapter: board private structure 349 * 350 * Free all transmit software resources 351 */ 352 void ena_free_all_io_tx_resources(struct ena_adapter *adapter) 353 { 354 ena_free_all_io_tx_resources_in_range(adapter, 355 0, 356 adapter->xdp_num_queues + 357 adapter->num_io_queues); 358 } 359 360 /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors) 361 * @adapter: network interface device structure 362 * @qid: queue index 363 * 364 * Returns 0 on success, negative on failure 365 */ 366 static int ena_setup_rx_resources(struct ena_adapter *adapter, 367 u32 qid) 368 { 369 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 370 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)]; 371 int size, node, i; 372 373 if (rx_ring->rx_buffer_info) { 374 netif_err(adapter, ifup, adapter->netdev, 375 "rx_buffer_info is not NULL"); 376 return -EEXIST; 377 } 378 379 /* alloc extra element so in rx path 380 * we can always prefetch rx_info + 1 381 */ 382 size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1); 383 node = cpu_to_node(ena_irq->cpu); 384 385 rx_ring->rx_buffer_info = vzalloc_node(size, node); 386 if (!rx_ring->rx_buffer_info) { 387 rx_ring->rx_buffer_info = vzalloc(size); 388 if (!rx_ring->rx_buffer_info) 389 return -ENOMEM; 390 } 391 392 size = sizeof(u16) * rx_ring->ring_size; 393 rx_ring->free_ids = vzalloc_node(size, node); 394 if (!rx_ring->free_ids) { 395 rx_ring->free_ids = vzalloc(size); 396 if (!rx_ring->free_ids) { 397 vfree(rx_ring->rx_buffer_info); 398 rx_ring->rx_buffer_info = NULL; 399 return -ENOMEM; 400 } 401 } 402 403 /* Req id ring for receiving RX pkts out of order */ 404 for (i = 0; i < rx_ring->ring_size; i++) 405 rx_ring->free_ids[i] = i; 406 407 /* Reset rx statistics */ 408 memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats)); 409 410 rx_ring->next_to_clean = 0; 411 rx_ring->next_to_use = 0; 412 rx_ring->cpu = ena_irq->cpu; 413 rx_ring->numa_node = node; 414 415 return 0; 416 } 417 418 /* ena_free_rx_resources - Free I/O Rx Resources 419 * @adapter: network interface device structure 420 * @qid: queue index 421 * 422 * Free all receive software resources 423 */ 424 static void ena_free_rx_resources(struct ena_adapter *adapter, 425 u32 qid) 426 { 427 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 428 429 vfree(rx_ring->rx_buffer_info); 430 rx_ring->rx_buffer_info = NULL; 431 432 vfree(rx_ring->free_ids); 433 rx_ring->free_ids = NULL; 434 } 435 436 /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues 437 * @adapter: board private structure 438 * 439 * Return 0 on success, negative on failure 440 */ 441 static int ena_setup_all_rx_resources(struct ena_adapter *adapter) 442 { 443 int i, rc = 0; 444 445 for (i = 0; i < adapter->num_io_queues; i++) { 446 rc = ena_setup_rx_resources(adapter, i); 447 if (rc) 448 goto err_setup_rx; 449 } 450 451 return 0; 452 453 err_setup_rx: 454 455 netif_err(adapter, ifup, adapter->netdev, 456 "Rx queue %d: allocation failed\n", i); 457 458 /* rewind the index freeing the rings as we go */ 459 while (i--) 460 ena_free_rx_resources(adapter, i); 461 return rc; 462 } 463 464 /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues 465 * @adapter: board private structure 466 * 467 * Free all receive software resources 468 */ 469 static void ena_free_all_io_rx_resources(struct ena_adapter *adapter) 470 { 471 int i; 472 473 for (i = 0; i < adapter->num_io_queues; i++) 474 ena_free_rx_resources(adapter, i); 475 } 476 477 static struct page *ena_alloc_map_page(struct ena_ring *rx_ring, 478 dma_addr_t *dma) 479 { 480 struct page *page; 481 482 /* This would allocate the page on the same NUMA node the executing code 483 * is running on. 484 */ 485 page = dev_alloc_page(); 486 if (!page) { 487 ena_increase_stat(&rx_ring->rx_stats.page_alloc_fail, 1, 488 &rx_ring->syncp); 489 return ERR_PTR(-ENOSPC); 490 } 491 492 /* To enable NIC-side port-mirroring, AKA SPAN port, 493 * we make the buffer readable from the nic as well 494 */ 495 *dma = dma_map_page(rx_ring->dev, page, 0, ENA_PAGE_SIZE, 496 DMA_BIDIRECTIONAL); 497 if (unlikely(dma_mapping_error(rx_ring->dev, *dma))) { 498 ena_increase_stat(&rx_ring->rx_stats.dma_mapping_err, 1, 499 &rx_ring->syncp); 500 __free_page(page); 501 return ERR_PTR(-EIO); 502 } 503 504 return page; 505 } 506 507 static int ena_alloc_rx_buffer(struct ena_ring *rx_ring, 508 struct ena_rx_buffer *rx_info) 509 { 510 int headroom = rx_ring->rx_headroom; 511 struct ena_com_buf *ena_buf; 512 struct page *page; 513 dma_addr_t dma; 514 int tailroom; 515 516 /* restore page offset value in case it has been changed by device */ 517 rx_info->buf_offset = headroom; 518 519 /* if previous allocated page is not used */ 520 if (unlikely(rx_info->page)) 521 return 0; 522 523 /* We handle DMA here */ 524 page = ena_alloc_map_page(rx_ring, &dma); 525 if (unlikely(IS_ERR(page))) 526 return PTR_ERR(page); 527 528 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev, 529 "Allocate page %p, rx_info %p\n", page, rx_info); 530 531 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 532 533 rx_info->page = page; 534 rx_info->dma_addr = dma; 535 rx_info->page_offset = 0; 536 ena_buf = &rx_info->ena_buf; 537 ena_buf->paddr = dma + headroom; 538 ena_buf->len = ENA_PAGE_SIZE - headroom - tailroom; 539 540 return 0; 541 } 542 543 static void ena_unmap_rx_buff_attrs(struct ena_ring *rx_ring, 544 struct ena_rx_buffer *rx_info, 545 unsigned long attrs) 546 { 547 dma_unmap_page_attrs(rx_ring->dev, rx_info->dma_addr, ENA_PAGE_SIZE, 548 DMA_BIDIRECTIONAL, attrs); 549 } 550 551 static void ena_free_rx_page(struct ena_ring *rx_ring, 552 struct ena_rx_buffer *rx_info) 553 { 554 struct page *page = rx_info->page; 555 556 if (unlikely(!page)) { 557 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev, 558 "Trying to free unallocated buffer\n"); 559 return; 560 } 561 562 ena_unmap_rx_buff_attrs(rx_ring, rx_info, 0); 563 564 __free_page(page); 565 rx_info->page = NULL; 566 } 567 568 static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num) 569 { 570 u16 next_to_use, req_id; 571 u32 i; 572 int rc; 573 574 next_to_use = rx_ring->next_to_use; 575 576 for (i = 0; i < num; i++) { 577 struct ena_rx_buffer *rx_info; 578 579 req_id = rx_ring->free_ids[next_to_use]; 580 581 rx_info = &rx_ring->rx_buffer_info[req_id]; 582 583 rc = ena_alloc_rx_buffer(rx_ring, rx_info); 584 if (unlikely(rc < 0)) { 585 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev, 586 "Failed to allocate buffer for rx queue %d\n", 587 rx_ring->qid); 588 break; 589 } 590 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq, 591 &rx_info->ena_buf, 592 req_id); 593 if (unlikely(rc)) { 594 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev, 595 "Failed to add buffer for rx queue %d\n", 596 rx_ring->qid); 597 break; 598 } 599 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use, 600 rx_ring->ring_size); 601 } 602 603 if (unlikely(i < num)) { 604 ena_increase_stat(&rx_ring->rx_stats.refil_partial, 1, 605 &rx_ring->syncp); 606 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev, 607 "Refilled rx qid %d with only %d buffers (from %d)\n", 608 rx_ring->qid, i, num); 609 } 610 611 /* ena_com_write_sq_doorbell issues a wmb() */ 612 if (likely(i)) 613 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq); 614 615 rx_ring->next_to_use = next_to_use; 616 617 return i; 618 } 619 620 static void ena_free_rx_bufs(struct ena_adapter *adapter, 621 u32 qid) 622 { 623 struct ena_ring *rx_ring = &adapter->rx_ring[qid]; 624 u32 i; 625 626 for (i = 0; i < rx_ring->ring_size; i++) { 627 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i]; 628 629 if (rx_info->page) 630 ena_free_rx_page(rx_ring, rx_info); 631 } 632 } 633 634 /* ena_refill_all_rx_bufs - allocate all queues Rx buffers 635 * @adapter: board private structure 636 */ 637 static void ena_refill_all_rx_bufs(struct ena_adapter *adapter) 638 { 639 struct ena_ring *rx_ring; 640 int i, rc, bufs_num; 641 642 for (i = 0; i < adapter->num_io_queues; i++) { 643 rx_ring = &adapter->rx_ring[i]; 644 bufs_num = rx_ring->ring_size - 1; 645 rc = ena_refill_rx_bufs(rx_ring, bufs_num); 646 647 if (unlikely(rc != bufs_num)) 648 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev, 649 "Refilling Queue %d failed. allocated %d buffers from: %d\n", 650 i, rc, bufs_num); 651 } 652 } 653 654 static void ena_free_all_rx_bufs(struct ena_adapter *adapter) 655 { 656 int i; 657 658 for (i = 0; i < adapter->num_io_queues; i++) 659 ena_free_rx_bufs(adapter, i); 660 } 661 662 void ena_unmap_tx_buff(struct ena_ring *tx_ring, 663 struct ena_tx_buffer *tx_info) 664 { 665 struct ena_com_buf *ena_buf; 666 u32 cnt; 667 int i; 668 669 ena_buf = tx_info->bufs; 670 cnt = tx_info->num_of_bufs; 671 672 if (unlikely(!cnt)) 673 return; 674 675 if (tx_info->map_linear_data) { 676 dma_unmap_single(tx_ring->dev, 677 dma_unmap_addr(ena_buf, paddr), 678 dma_unmap_len(ena_buf, len), 679 DMA_TO_DEVICE); 680 ena_buf++; 681 cnt--; 682 } 683 684 /* unmap remaining mapped pages */ 685 for (i = 0; i < cnt; i++) { 686 dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr), 687 dma_unmap_len(ena_buf, len), DMA_TO_DEVICE); 688 ena_buf++; 689 } 690 } 691 692 /* ena_free_tx_bufs - Free Tx Buffers per Queue 693 * @tx_ring: TX ring for which buffers be freed 694 */ 695 static void ena_free_tx_bufs(struct ena_ring *tx_ring) 696 { 697 bool print_once = true; 698 bool is_xdp_ring; 699 u32 i; 700 701 is_xdp_ring = ENA_IS_XDP_INDEX(tx_ring->adapter, tx_ring->qid); 702 703 for (i = 0; i < tx_ring->ring_size; i++) { 704 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i]; 705 706 if (!tx_info->skb) 707 continue; 708 709 if (print_once) { 710 netif_notice(tx_ring->adapter, ifdown, tx_ring->netdev, 711 "Free uncompleted tx skb qid %d idx 0x%x\n", 712 tx_ring->qid, i); 713 print_once = false; 714 } else { 715 netif_dbg(tx_ring->adapter, ifdown, tx_ring->netdev, 716 "Free uncompleted tx skb qid %d idx 0x%x\n", 717 tx_ring->qid, i); 718 } 719 720 ena_unmap_tx_buff(tx_ring, tx_info); 721 722 if (is_xdp_ring) 723 xdp_return_frame(tx_info->xdpf); 724 else 725 dev_kfree_skb_any(tx_info->skb); 726 } 727 728 if (!is_xdp_ring) 729 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev, 730 tx_ring->qid)); 731 } 732 733 static void ena_free_all_tx_bufs(struct ena_adapter *adapter) 734 { 735 struct ena_ring *tx_ring; 736 int i; 737 738 for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) { 739 tx_ring = &adapter->tx_ring[i]; 740 ena_free_tx_bufs(tx_ring); 741 } 742 } 743 744 static void ena_destroy_all_tx_queues(struct ena_adapter *adapter) 745 { 746 u16 ena_qid; 747 int i; 748 749 for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) { 750 ena_qid = ENA_IO_TXQ_IDX(i); 751 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid); 752 } 753 } 754 755 static void ena_destroy_all_rx_queues(struct ena_adapter *adapter) 756 { 757 u16 ena_qid; 758 int i; 759 760 for (i = 0; i < adapter->num_io_queues; i++) { 761 ena_qid = ENA_IO_RXQ_IDX(i); 762 cancel_work_sync(&adapter->ena_napi[i].dim.work); 763 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid); 764 } 765 } 766 767 static void ena_destroy_all_io_queues(struct ena_adapter *adapter) 768 { 769 ena_destroy_all_tx_queues(adapter); 770 ena_destroy_all_rx_queues(adapter); 771 } 772 773 int handle_invalid_req_id(struct ena_ring *ring, u16 req_id, 774 struct ena_tx_buffer *tx_info, bool is_xdp) 775 { 776 if (tx_info) 777 netif_err(ring->adapter, 778 tx_done, 779 ring->netdev, 780 "tx_info doesn't have valid %s. qid %u req_id %u", 781 is_xdp ? "xdp frame" : "skb", ring->qid, req_id); 782 else 783 netif_err(ring->adapter, 784 tx_done, 785 ring->netdev, 786 "Invalid req_id %u in qid %u\n", 787 req_id, ring->qid); 788 789 ena_increase_stat(&ring->tx_stats.bad_req_id, 1, &ring->syncp); 790 ena_reset_device(ring->adapter, ENA_REGS_RESET_INV_TX_REQ_ID); 791 792 return -EFAULT; 793 } 794 795 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id) 796 { 797 struct ena_tx_buffer *tx_info; 798 799 tx_info = &tx_ring->tx_buffer_info[req_id]; 800 if (likely(tx_info->skb)) 801 return 0; 802 803 return handle_invalid_req_id(tx_ring, req_id, tx_info, false); 804 } 805 806 static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget) 807 { 808 struct netdev_queue *txq; 809 bool above_thresh; 810 u32 tx_bytes = 0; 811 u32 total_done = 0; 812 u16 next_to_clean; 813 u16 req_id; 814 int tx_pkts = 0; 815 int rc; 816 817 next_to_clean = tx_ring->next_to_clean; 818 txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid); 819 820 while (tx_pkts < budget) { 821 struct ena_tx_buffer *tx_info; 822 struct sk_buff *skb; 823 824 rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq, 825 &req_id); 826 if (rc) { 827 if (unlikely(rc == -EINVAL)) 828 handle_invalid_req_id(tx_ring, req_id, NULL, 829 false); 830 break; 831 } 832 833 /* validate that the request id points to a valid skb */ 834 rc = validate_tx_req_id(tx_ring, req_id); 835 if (rc) 836 break; 837 838 tx_info = &tx_ring->tx_buffer_info[req_id]; 839 skb = tx_info->skb; 840 841 /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */ 842 prefetch(&skb->end); 843 844 tx_info->skb = NULL; 845 tx_info->last_jiffies = 0; 846 847 ena_unmap_tx_buff(tx_ring, tx_info); 848 849 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev, 850 "tx_poll: q %d skb %p completed\n", tx_ring->qid, 851 skb); 852 853 tx_bytes += skb->len; 854 dev_kfree_skb(skb); 855 tx_pkts++; 856 total_done += tx_info->tx_descs; 857 858 tx_ring->free_ids[next_to_clean] = req_id; 859 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean, 860 tx_ring->ring_size); 861 } 862 863 tx_ring->next_to_clean = next_to_clean; 864 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done); 865 ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq); 866 867 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes); 868 869 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev, 870 "tx_poll: q %d done. total pkts: %d\n", 871 tx_ring->qid, tx_pkts); 872 873 /* need to make the rings circular update visible to 874 * ena_start_xmit() before checking for netif_queue_stopped(). 875 */ 876 smp_mb(); 877 878 above_thresh = ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq, 879 ENA_TX_WAKEUP_THRESH); 880 if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) { 881 __netif_tx_lock(txq, smp_processor_id()); 882 above_thresh = 883 ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq, 884 ENA_TX_WAKEUP_THRESH); 885 if (netif_tx_queue_stopped(txq) && above_thresh && 886 test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags)) { 887 netif_tx_wake_queue(txq); 888 ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1, 889 &tx_ring->syncp); 890 } 891 __netif_tx_unlock(txq); 892 } 893 894 return tx_pkts; 895 } 896 897 static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag, u16 len) 898 { 899 struct sk_buff *skb; 900 901 if (!first_frag) 902 skb = napi_alloc_skb(rx_ring->napi, len); 903 else 904 skb = napi_build_skb(first_frag, len); 905 906 if (unlikely(!skb)) { 907 ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1, 908 &rx_ring->syncp); 909 910 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev, 911 "Failed to allocate skb. first_frag %s\n", 912 first_frag ? "provided" : "not provided"); 913 } 914 915 return skb; 916 } 917 918 static bool ena_try_rx_buf_page_reuse(struct ena_rx_buffer *rx_info, u16 buf_len, 919 u16 len, int pkt_offset) 920 { 921 struct ena_com_buf *ena_buf = &rx_info->ena_buf; 922 923 /* More than ENA_MIN_RX_BUF_SIZE left in the reused buffer 924 * for data + headroom + tailroom. 925 */ 926 if (SKB_DATA_ALIGN(len + pkt_offset) + ENA_MIN_RX_BUF_SIZE <= ena_buf->len) { 927 page_ref_inc(rx_info->page); 928 rx_info->page_offset += buf_len; 929 ena_buf->paddr += buf_len; 930 ena_buf->len -= buf_len; 931 return true; 932 } 933 934 return false; 935 } 936 937 static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring, 938 struct ena_com_rx_buf_info *ena_bufs, 939 u32 descs, 940 u16 *next_to_clean) 941 { 942 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 943 bool is_xdp_loaded = ena_xdp_present_ring(rx_ring); 944 struct ena_rx_buffer *rx_info; 945 struct ena_adapter *adapter; 946 int page_offset, pkt_offset; 947 dma_addr_t pre_reuse_paddr; 948 u16 len, req_id, buf = 0; 949 bool reuse_rx_buf_page; 950 struct sk_buff *skb; 951 void *buf_addr; 952 int buf_offset; 953 u16 buf_len; 954 955 len = ena_bufs[buf].len; 956 req_id = ena_bufs[buf].req_id; 957 958 rx_info = &rx_ring->rx_buffer_info[req_id]; 959 960 if (unlikely(!rx_info->page)) { 961 adapter = rx_ring->adapter; 962 netif_err(adapter, rx_err, rx_ring->netdev, 963 "Page is NULL. qid %u req_id %u\n", rx_ring->qid, req_id); 964 ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1, &rx_ring->syncp); 965 ena_reset_device(adapter, ENA_REGS_RESET_INV_RX_REQ_ID); 966 return NULL; 967 } 968 969 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev, 970 "rx_info %p page %p\n", 971 rx_info, rx_info->page); 972 973 buf_offset = rx_info->buf_offset; 974 pkt_offset = buf_offset - rx_ring->rx_headroom; 975 page_offset = rx_info->page_offset; 976 buf_addr = page_address(rx_info->page) + page_offset; 977 978 if (len <= rx_ring->rx_copybreak) { 979 skb = ena_alloc_skb(rx_ring, NULL, len); 980 if (unlikely(!skb)) 981 return NULL; 982 983 skb_copy_to_linear_data(skb, buf_addr + buf_offset, len); 984 dma_sync_single_for_device(rx_ring->dev, 985 dma_unmap_addr(&rx_info->ena_buf, paddr) + pkt_offset, 986 len, 987 DMA_FROM_DEVICE); 988 989 skb_put(skb, len); 990 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev, 991 "RX allocated small packet. len %d.\n", skb->len); 992 skb->protocol = eth_type_trans(skb, rx_ring->netdev); 993 rx_ring->free_ids[*next_to_clean] = req_id; 994 *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs, 995 rx_ring->ring_size); 996 return skb; 997 } 998 999 buf_len = SKB_DATA_ALIGN(len + buf_offset + tailroom); 1000 1001 /* If XDP isn't loaded try to reuse part of the RX buffer */ 1002 reuse_rx_buf_page = !is_xdp_loaded && 1003 ena_try_rx_buf_page_reuse(rx_info, buf_len, len, pkt_offset); 1004 1005 if (!reuse_rx_buf_page) 1006 ena_unmap_rx_buff_attrs(rx_ring, rx_info, DMA_ATTR_SKIP_CPU_SYNC); 1007 1008 skb = ena_alloc_skb(rx_ring, buf_addr, buf_len); 1009 if (unlikely(!skb)) 1010 return NULL; 1011 1012 /* Populate skb's linear part */ 1013 skb_reserve(skb, buf_offset); 1014 skb_put(skb, len); 1015 skb->protocol = eth_type_trans(skb, rx_ring->netdev); 1016 1017 do { 1018 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev, 1019 "RX skb updated. len %d. data_len %d\n", 1020 skb->len, skb->data_len); 1021 1022 if (!reuse_rx_buf_page) 1023 rx_info->page = NULL; 1024 1025 rx_ring->free_ids[*next_to_clean] = req_id; 1026 *next_to_clean = 1027 ENA_RX_RING_IDX_NEXT(*next_to_clean, 1028 rx_ring->ring_size); 1029 if (likely(--descs == 0)) 1030 break; 1031 1032 buf++; 1033 len = ena_bufs[buf].len; 1034 req_id = ena_bufs[buf].req_id; 1035 1036 rx_info = &rx_ring->rx_buffer_info[req_id]; 1037 1038 /* rx_info->buf_offset includes rx_ring->rx_headroom */ 1039 buf_offset = rx_info->buf_offset; 1040 pkt_offset = buf_offset - rx_ring->rx_headroom; 1041 buf_len = SKB_DATA_ALIGN(len + buf_offset + tailroom); 1042 page_offset = rx_info->page_offset; 1043 1044 pre_reuse_paddr = dma_unmap_addr(&rx_info->ena_buf, paddr); 1045 1046 reuse_rx_buf_page = !is_xdp_loaded && 1047 ena_try_rx_buf_page_reuse(rx_info, buf_len, len, pkt_offset); 1048 1049 dma_sync_single_for_cpu(rx_ring->dev, 1050 pre_reuse_paddr + pkt_offset, 1051 len, 1052 DMA_FROM_DEVICE); 1053 1054 if (!reuse_rx_buf_page) 1055 ena_unmap_rx_buff_attrs(rx_ring, rx_info, 1056 DMA_ATTR_SKIP_CPU_SYNC); 1057 1058 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page, 1059 page_offset + buf_offset, len, buf_len); 1060 1061 } while (1); 1062 1063 return skb; 1064 } 1065 1066 /* ena_rx_checksum - indicate in skb if hw indicated a good cksum 1067 * @adapter: structure containing adapter specific data 1068 * @ena_rx_ctx: received packet context/metadata 1069 * @skb: skb currently being received and modified 1070 */ 1071 static void ena_rx_checksum(struct ena_ring *rx_ring, 1072 struct ena_com_rx_ctx *ena_rx_ctx, 1073 struct sk_buff *skb) 1074 { 1075 /* Rx csum disabled */ 1076 if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) { 1077 skb->ip_summed = CHECKSUM_NONE; 1078 return; 1079 } 1080 1081 /* For fragmented packets the checksum isn't valid */ 1082 if (ena_rx_ctx->frag) { 1083 skb->ip_summed = CHECKSUM_NONE; 1084 return; 1085 } 1086 1087 /* if IP and error */ 1088 if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) && 1089 (ena_rx_ctx->l3_csum_err))) { 1090 /* ipv4 checksum error */ 1091 skb->ip_summed = CHECKSUM_NONE; 1092 ena_increase_stat(&rx_ring->rx_stats.csum_bad, 1, 1093 &rx_ring->syncp); 1094 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev, 1095 "RX IPv4 header checksum error\n"); 1096 return; 1097 } 1098 1099 /* if TCP/UDP */ 1100 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) || 1101 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) { 1102 if (unlikely(ena_rx_ctx->l4_csum_err)) { 1103 /* TCP/UDP checksum error */ 1104 ena_increase_stat(&rx_ring->rx_stats.csum_bad, 1, 1105 &rx_ring->syncp); 1106 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev, 1107 "RX L4 checksum error\n"); 1108 skb->ip_summed = CHECKSUM_NONE; 1109 return; 1110 } 1111 1112 if (likely(ena_rx_ctx->l4_csum_checked)) { 1113 skb->ip_summed = CHECKSUM_UNNECESSARY; 1114 ena_increase_stat(&rx_ring->rx_stats.csum_good, 1, 1115 &rx_ring->syncp); 1116 } else { 1117 ena_increase_stat(&rx_ring->rx_stats.csum_unchecked, 1, 1118 &rx_ring->syncp); 1119 skb->ip_summed = CHECKSUM_NONE; 1120 } 1121 } else { 1122 skb->ip_summed = CHECKSUM_NONE; 1123 return; 1124 } 1125 1126 } 1127 1128 static void ena_set_rx_hash(struct ena_ring *rx_ring, 1129 struct ena_com_rx_ctx *ena_rx_ctx, 1130 struct sk_buff *skb) 1131 { 1132 enum pkt_hash_types hash_type; 1133 1134 if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) { 1135 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) || 1136 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) 1137 1138 hash_type = PKT_HASH_TYPE_L4; 1139 else 1140 hash_type = PKT_HASH_TYPE_NONE; 1141 1142 /* Override hash type if the packet is fragmented */ 1143 if (ena_rx_ctx->frag) 1144 hash_type = PKT_HASH_TYPE_NONE; 1145 1146 skb_set_hash(skb, ena_rx_ctx->hash, hash_type); 1147 } 1148 } 1149 1150 static int ena_xdp_handle_buff(struct ena_ring *rx_ring, struct xdp_buff *xdp, u16 num_descs) 1151 { 1152 struct ena_rx_buffer *rx_info; 1153 int ret; 1154 1155 /* XDP multi-buffer packets not supported */ 1156 if (unlikely(num_descs > 1)) { 1157 netdev_err_once(rx_ring->adapter->netdev, 1158 "xdp: dropped unsupported multi-buffer packets\n"); 1159 ena_increase_stat(&rx_ring->rx_stats.xdp_drop, 1, &rx_ring->syncp); 1160 return ENA_XDP_DROP; 1161 } 1162 1163 rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id]; 1164 xdp_prepare_buff(xdp, page_address(rx_info->page), 1165 rx_info->buf_offset, 1166 rx_ring->ena_bufs[0].len, false); 1167 1168 ret = ena_xdp_execute(rx_ring, xdp); 1169 1170 /* The xdp program might expand the headers */ 1171 if (ret == ENA_XDP_PASS) { 1172 rx_info->buf_offset = xdp->data - xdp->data_hard_start; 1173 rx_ring->ena_bufs[0].len = xdp->data_end - xdp->data; 1174 } 1175 1176 return ret; 1177 } 1178 1179 /* ena_clean_rx_irq - Cleanup RX irq 1180 * @rx_ring: RX ring to clean 1181 * @napi: napi handler 1182 * @budget: how many packets driver is allowed to clean 1183 * 1184 * Returns the number of cleaned buffers. 1185 */ 1186 static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi, 1187 u32 budget) 1188 { 1189 u16 next_to_clean = rx_ring->next_to_clean; 1190 struct ena_com_rx_ctx ena_rx_ctx; 1191 struct ena_rx_buffer *rx_info; 1192 struct ena_adapter *adapter; 1193 u32 res_budget, work_done; 1194 int rx_copybreak_pkt = 0; 1195 int refill_threshold; 1196 struct sk_buff *skb; 1197 int refill_required; 1198 struct xdp_buff xdp; 1199 int xdp_flags = 0; 1200 int total_len = 0; 1201 int xdp_verdict; 1202 u8 pkt_offset; 1203 int rc = 0; 1204 int i; 1205 1206 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev, 1207 "%s qid %d\n", __func__, rx_ring->qid); 1208 res_budget = budget; 1209 xdp_init_buff(&xdp, ENA_PAGE_SIZE, &rx_ring->xdp_rxq); 1210 1211 do { 1212 xdp_verdict = ENA_XDP_PASS; 1213 skb = NULL; 1214 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs; 1215 ena_rx_ctx.max_bufs = rx_ring->sgl_size; 1216 ena_rx_ctx.descs = 0; 1217 ena_rx_ctx.pkt_offset = 0; 1218 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq, 1219 rx_ring->ena_com_io_sq, 1220 &ena_rx_ctx); 1221 if (unlikely(rc)) 1222 goto error; 1223 1224 if (unlikely(ena_rx_ctx.descs == 0)) 1225 break; 1226 1227 /* First descriptor might have an offset set by the device */ 1228 rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id]; 1229 pkt_offset = ena_rx_ctx.pkt_offset; 1230 rx_info->buf_offset += pkt_offset; 1231 1232 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev, 1233 "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n", 1234 rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto, 1235 ena_rx_ctx.l4_proto, ena_rx_ctx.hash); 1236 1237 dma_sync_single_for_cpu(rx_ring->dev, 1238 dma_unmap_addr(&rx_info->ena_buf, paddr) + pkt_offset, 1239 rx_ring->ena_bufs[0].len, 1240 DMA_FROM_DEVICE); 1241 1242 if (ena_xdp_present_ring(rx_ring)) 1243 xdp_verdict = ena_xdp_handle_buff(rx_ring, &xdp, ena_rx_ctx.descs); 1244 1245 /* allocate skb and fill it */ 1246 if (xdp_verdict == ENA_XDP_PASS) 1247 skb = ena_rx_skb(rx_ring, 1248 rx_ring->ena_bufs, 1249 ena_rx_ctx.descs, 1250 &next_to_clean); 1251 1252 if (unlikely(!skb)) { 1253 for (i = 0; i < ena_rx_ctx.descs; i++) { 1254 int req_id = rx_ring->ena_bufs[i].req_id; 1255 1256 rx_ring->free_ids[next_to_clean] = req_id; 1257 next_to_clean = 1258 ENA_RX_RING_IDX_NEXT(next_to_clean, 1259 rx_ring->ring_size); 1260 1261 /* Packets was passed for transmission, unmap it 1262 * from RX side. 1263 */ 1264 if (xdp_verdict & ENA_XDP_FORWARDED) { 1265 ena_unmap_rx_buff_attrs(rx_ring, 1266 &rx_ring->rx_buffer_info[req_id], 1267 DMA_ATTR_SKIP_CPU_SYNC); 1268 rx_ring->rx_buffer_info[req_id].page = NULL; 1269 } 1270 } 1271 if (xdp_verdict != ENA_XDP_PASS) { 1272 xdp_flags |= xdp_verdict; 1273 total_len += ena_rx_ctx.ena_bufs[0].len; 1274 res_budget--; 1275 continue; 1276 } 1277 break; 1278 } 1279 1280 ena_rx_checksum(rx_ring, &ena_rx_ctx, skb); 1281 1282 ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb); 1283 1284 skb_record_rx_queue(skb, rx_ring->qid); 1285 1286 if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak) 1287 rx_copybreak_pkt++; 1288 1289 total_len += skb->len; 1290 1291 napi_gro_receive(napi, skb); 1292 1293 res_budget--; 1294 } while (likely(res_budget)); 1295 1296 work_done = budget - res_budget; 1297 rx_ring->per_napi_packets += work_done; 1298 u64_stats_update_begin(&rx_ring->syncp); 1299 rx_ring->rx_stats.bytes += total_len; 1300 rx_ring->rx_stats.cnt += work_done; 1301 rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt; 1302 u64_stats_update_end(&rx_ring->syncp); 1303 1304 rx_ring->next_to_clean = next_to_clean; 1305 1306 refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq); 1307 refill_threshold = 1308 min_t(int, rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER, 1309 ENA_RX_REFILL_THRESH_PACKET); 1310 1311 /* Optimization, try to batch new rx buffers */ 1312 if (refill_required > refill_threshold) { 1313 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq); 1314 ena_refill_rx_bufs(rx_ring, refill_required); 1315 } 1316 1317 if (xdp_flags & ENA_XDP_REDIRECT) 1318 xdp_do_flush_map(); 1319 1320 return work_done; 1321 1322 error: 1323 if (xdp_flags & ENA_XDP_REDIRECT) 1324 xdp_do_flush(); 1325 1326 adapter = netdev_priv(rx_ring->netdev); 1327 1328 if (rc == -ENOSPC) { 1329 ena_increase_stat(&rx_ring->rx_stats.bad_desc_num, 1, 1330 &rx_ring->syncp); 1331 ena_reset_device(adapter, ENA_REGS_RESET_TOO_MANY_RX_DESCS); 1332 } else { 1333 ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1, 1334 &rx_ring->syncp); 1335 ena_reset_device(adapter, ENA_REGS_RESET_INV_RX_REQ_ID); 1336 } 1337 return 0; 1338 } 1339 1340 static void ena_dim_work(struct work_struct *w) 1341 { 1342 struct dim *dim = container_of(w, struct dim, work); 1343 struct dim_cq_moder cur_moder = 1344 net_dim_get_rx_moderation(dim->mode, dim->profile_ix); 1345 struct ena_napi *ena_napi = container_of(dim, struct ena_napi, dim); 1346 1347 ena_napi->rx_ring->smoothed_interval = cur_moder.usec; 1348 dim->state = DIM_START_MEASURE; 1349 } 1350 1351 static void ena_adjust_adaptive_rx_intr_moderation(struct ena_napi *ena_napi) 1352 { 1353 struct dim_sample dim_sample; 1354 struct ena_ring *rx_ring = ena_napi->rx_ring; 1355 1356 if (!rx_ring->per_napi_packets) 1357 return; 1358 1359 rx_ring->non_empty_napi_events++; 1360 1361 dim_update_sample(rx_ring->non_empty_napi_events, 1362 rx_ring->rx_stats.cnt, 1363 rx_ring->rx_stats.bytes, 1364 &dim_sample); 1365 1366 net_dim(&ena_napi->dim, dim_sample); 1367 1368 rx_ring->per_napi_packets = 0; 1369 } 1370 1371 void ena_unmask_interrupt(struct ena_ring *tx_ring, 1372 struct ena_ring *rx_ring) 1373 { 1374 u32 rx_interval = tx_ring->smoothed_interval; 1375 struct ena_eth_io_intr_reg intr_reg; 1376 1377 /* Rx ring can be NULL when for XDP tx queues which don't have an 1378 * accompanying rx_ring pair. 1379 */ 1380 if (rx_ring) 1381 rx_interval = ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev) ? 1382 rx_ring->smoothed_interval : 1383 ena_com_get_nonadaptive_moderation_interval_rx(rx_ring->ena_dev); 1384 1385 /* Update intr register: rx intr delay, 1386 * tx intr delay and interrupt unmask 1387 */ 1388 ena_com_update_intr_reg(&intr_reg, 1389 rx_interval, 1390 tx_ring->smoothed_interval, 1391 true); 1392 1393 ena_increase_stat(&tx_ring->tx_stats.unmask_interrupt, 1, 1394 &tx_ring->syncp); 1395 1396 /* It is a shared MSI-X. 1397 * Tx and Rx CQ have pointer to it. 1398 * So we use one of them to reach the intr reg 1399 * The Tx ring is used because the rx_ring is NULL for XDP queues 1400 */ 1401 ena_com_unmask_intr(tx_ring->ena_com_io_cq, &intr_reg); 1402 } 1403 1404 void ena_update_ring_numa_node(struct ena_ring *tx_ring, 1405 struct ena_ring *rx_ring) 1406 { 1407 int cpu = get_cpu(); 1408 int numa_node; 1409 1410 /* Check only one ring since the 2 rings are running on the same cpu */ 1411 if (likely(tx_ring->cpu == cpu)) 1412 goto out; 1413 1414 tx_ring->cpu = cpu; 1415 if (rx_ring) 1416 rx_ring->cpu = cpu; 1417 1418 numa_node = cpu_to_node(cpu); 1419 1420 if (likely(tx_ring->numa_node == numa_node)) 1421 goto out; 1422 1423 put_cpu(); 1424 1425 if (numa_node != NUMA_NO_NODE) { 1426 ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node); 1427 tx_ring->numa_node = numa_node; 1428 if (rx_ring) { 1429 rx_ring->numa_node = numa_node; 1430 ena_com_update_numa_node(rx_ring->ena_com_io_cq, 1431 numa_node); 1432 } 1433 } 1434 1435 return; 1436 out: 1437 put_cpu(); 1438 } 1439 1440 static int ena_io_poll(struct napi_struct *napi, int budget) 1441 { 1442 struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi); 1443 struct ena_ring *tx_ring, *rx_ring; 1444 int tx_work_done; 1445 int rx_work_done = 0; 1446 int tx_budget; 1447 int napi_comp_call = 0; 1448 int ret; 1449 1450 tx_ring = ena_napi->tx_ring; 1451 rx_ring = ena_napi->rx_ring; 1452 1453 tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER; 1454 1455 if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) || 1456 test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) { 1457 napi_complete_done(napi, 0); 1458 return 0; 1459 } 1460 1461 tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget); 1462 /* On netpoll the budget is zero and the handler should only clean the 1463 * tx completions. 1464 */ 1465 if (likely(budget)) 1466 rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget); 1467 1468 /* If the device is about to reset or down, avoid unmask 1469 * the interrupt and return 0 so NAPI won't reschedule 1470 */ 1471 if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) || 1472 test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags))) { 1473 napi_complete_done(napi, 0); 1474 ret = 0; 1475 1476 } else if ((budget > rx_work_done) && (tx_budget > tx_work_done)) { 1477 napi_comp_call = 1; 1478 1479 /* Update numa and unmask the interrupt only when schedule 1480 * from the interrupt context (vs from sk_busy_loop) 1481 */ 1482 if (napi_complete_done(napi, rx_work_done) && 1483 READ_ONCE(ena_napi->interrupts_masked)) { 1484 smp_rmb(); /* make sure interrupts_masked is read */ 1485 WRITE_ONCE(ena_napi->interrupts_masked, false); 1486 /* We apply adaptive moderation on Rx path only. 1487 * Tx uses static interrupt moderation. 1488 */ 1489 if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev)) 1490 ena_adjust_adaptive_rx_intr_moderation(ena_napi); 1491 1492 ena_update_ring_numa_node(tx_ring, rx_ring); 1493 ena_unmask_interrupt(tx_ring, rx_ring); 1494 } 1495 1496 ret = rx_work_done; 1497 } else { 1498 ret = budget; 1499 } 1500 1501 u64_stats_update_begin(&tx_ring->syncp); 1502 tx_ring->tx_stats.napi_comp += napi_comp_call; 1503 tx_ring->tx_stats.tx_poll++; 1504 u64_stats_update_end(&tx_ring->syncp); 1505 1506 tx_ring->tx_stats.last_napi_jiffies = jiffies; 1507 1508 return ret; 1509 } 1510 1511 static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data) 1512 { 1513 struct ena_adapter *adapter = (struct ena_adapter *)data; 1514 1515 ena_com_admin_q_comp_intr_handler(adapter->ena_dev); 1516 1517 /* Don't call the aenq handler before probe is done */ 1518 if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))) 1519 ena_com_aenq_intr_handler(adapter->ena_dev, data); 1520 1521 return IRQ_HANDLED; 1522 } 1523 1524 /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx 1525 * @irq: interrupt number 1526 * @data: pointer to a network interface private napi device structure 1527 */ 1528 static irqreturn_t ena_intr_msix_io(int irq, void *data) 1529 { 1530 struct ena_napi *ena_napi = data; 1531 1532 /* Used to check HW health */ 1533 WRITE_ONCE(ena_napi->first_interrupt, true); 1534 1535 WRITE_ONCE(ena_napi->interrupts_masked, true); 1536 smp_wmb(); /* write interrupts_masked before calling napi */ 1537 1538 napi_schedule_irqoff(&ena_napi->napi); 1539 1540 return IRQ_HANDLED; 1541 } 1542 1543 /* Reserve a single MSI-X vector for management (admin + aenq). 1544 * plus reserve one vector for each potential io queue. 1545 * the number of potential io queues is the minimum of what the device 1546 * supports and the number of vCPUs. 1547 */ 1548 static int ena_enable_msix(struct ena_adapter *adapter) 1549 { 1550 int msix_vecs, irq_cnt; 1551 1552 if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) { 1553 netif_err(adapter, probe, adapter->netdev, 1554 "Error, MSI-X is already enabled\n"); 1555 return -EPERM; 1556 } 1557 1558 /* Reserved the max msix vectors we might need */ 1559 msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues); 1560 netif_dbg(adapter, probe, adapter->netdev, 1561 "Trying to enable MSI-X, vectors %d\n", msix_vecs); 1562 1563 irq_cnt = pci_alloc_irq_vectors(adapter->pdev, ENA_MIN_MSIX_VEC, 1564 msix_vecs, PCI_IRQ_MSIX); 1565 1566 if (irq_cnt < 0) { 1567 netif_err(adapter, probe, adapter->netdev, 1568 "Failed to enable MSI-X. irq_cnt %d\n", irq_cnt); 1569 return -ENOSPC; 1570 } 1571 1572 if (irq_cnt != msix_vecs) { 1573 netif_notice(adapter, probe, adapter->netdev, 1574 "Enable only %d MSI-X (out of %d), reduce the number of queues\n", 1575 irq_cnt, msix_vecs); 1576 adapter->num_io_queues = irq_cnt - ENA_ADMIN_MSIX_VEC; 1577 } 1578 1579 if (ena_init_rx_cpu_rmap(adapter)) 1580 netif_warn(adapter, probe, adapter->netdev, 1581 "Failed to map IRQs to CPUs\n"); 1582 1583 adapter->msix_vecs = irq_cnt; 1584 set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags); 1585 1586 return 0; 1587 } 1588 1589 static void ena_setup_mgmnt_intr(struct ena_adapter *adapter) 1590 { 1591 u32 cpu; 1592 1593 snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name, 1594 ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s", 1595 pci_name(adapter->pdev)); 1596 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = 1597 ena_intr_msix_mgmnt; 1598 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter; 1599 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector = 1600 pci_irq_vector(adapter->pdev, ENA_MGMNT_IRQ_IDX); 1601 cpu = cpumask_first(cpu_online_mask); 1602 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu; 1603 cpumask_set_cpu(cpu, 1604 &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask); 1605 } 1606 1607 static void ena_setup_io_intr(struct ena_adapter *adapter) 1608 { 1609 struct net_device *netdev; 1610 int irq_idx, i, cpu; 1611 int io_queue_count; 1612 1613 netdev = adapter->netdev; 1614 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues; 1615 1616 for (i = 0; i < io_queue_count; i++) { 1617 irq_idx = ENA_IO_IRQ_IDX(i); 1618 cpu = i % num_online_cpus(); 1619 1620 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE, 1621 "%s-Tx-Rx-%d", netdev->name, i); 1622 adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io; 1623 adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i]; 1624 adapter->irq_tbl[irq_idx].vector = 1625 pci_irq_vector(adapter->pdev, irq_idx); 1626 adapter->irq_tbl[irq_idx].cpu = cpu; 1627 1628 cpumask_set_cpu(cpu, 1629 &adapter->irq_tbl[irq_idx].affinity_hint_mask); 1630 } 1631 } 1632 1633 static int ena_request_mgmnt_irq(struct ena_adapter *adapter) 1634 { 1635 unsigned long flags = 0; 1636 struct ena_irq *irq; 1637 int rc; 1638 1639 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX]; 1640 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 1641 irq->data); 1642 if (rc) { 1643 netif_err(adapter, probe, adapter->netdev, 1644 "Failed to request admin irq\n"); 1645 return rc; 1646 } 1647 1648 netif_dbg(adapter, probe, adapter->netdev, 1649 "Set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n", 1650 irq->affinity_hint_mask.bits[0], irq->vector); 1651 1652 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask); 1653 1654 return rc; 1655 } 1656 1657 static int ena_request_io_irq(struct ena_adapter *adapter) 1658 { 1659 u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues; 1660 unsigned long flags = 0; 1661 struct ena_irq *irq; 1662 int rc = 0, i, k; 1663 1664 if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) { 1665 netif_err(adapter, ifup, adapter->netdev, 1666 "Failed to request I/O IRQ: MSI-X is not enabled\n"); 1667 return -EINVAL; 1668 } 1669 1670 for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) { 1671 irq = &adapter->irq_tbl[i]; 1672 rc = request_irq(irq->vector, irq->handler, flags, irq->name, 1673 irq->data); 1674 if (rc) { 1675 netif_err(adapter, ifup, adapter->netdev, 1676 "Failed to request I/O IRQ. index %d rc %d\n", 1677 i, rc); 1678 goto err; 1679 } 1680 1681 netif_dbg(adapter, ifup, adapter->netdev, 1682 "Set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n", 1683 i, irq->affinity_hint_mask.bits[0], irq->vector); 1684 1685 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask); 1686 } 1687 1688 return rc; 1689 1690 err: 1691 for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) { 1692 irq = &adapter->irq_tbl[k]; 1693 free_irq(irq->vector, irq->data); 1694 } 1695 1696 return rc; 1697 } 1698 1699 static void ena_free_mgmnt_irq(struct ena_adapter *adapter) 1700 { 1701 struct ena_irq *irq; 1702 1703 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX]; 1704 synchronize_irq(irq->vector); 1705 irq_set_affinity_hint(irq->vector, NULL); 1706 free_irq(irq->vector, irq->data); 1707 } 1708 1709 static void ena_free_io_irq(struct ena_adapter *adapter) 1710 { 1711 u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues; 1712 struct ena_irq *irq; 1713 int i; 1714 1715 #ifdef CONFIG_RFS_ACCEL 1716 if (adapter->msix_vecs >= 1) { 1717 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap); 1718 adapter->netdev->rx_cpu_rmap = NULL; 1719 } 1720 #endif /* CONFIG_RFS_ACCEL */ 1721 1722 for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) { 1723 irq = &adapter->irq_tbl[i]; 1724 irq_set_affinity_hint(irq->vector, NULL); 1725 free_irq(irq->vector, irq->data); 1726 } 1727 } 1728 1729 static void ena_disable_msix(struct ena_adapter *adapter) 1730 { 1731 if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) 1732 pci_free_irq_vectors(adapter->pdev); 1733 } 1734 1735 static void ena_disable_io_intr_sync(struct ena_adapter *adapter) 1736 { 1737 u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues; 1738 int i; 1739 1740 if (!netif_running(adapter->netdev)) 1741 return; 1742 1743 for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) 1744 synchronize_irq(adapter->irq_tbl[i].vector); 1745 } 1746 1747 static void ena_del_napi_in_range(struct ena_adapter *adapter, 1748 int first_index, 1749 int count) 1750 { 1751 int i; 1752 1753 for (i = first_index; i < first_index + count; i++) { 1754 netif_napi_del(&adapter->ena_napi[i].napi); 1755 1756 WARN_ON(ENA_IS_XDP_INDEX(adapter, i) && 1757 adapter->ena_napi[i].rx_ring); 1758 } 1759 } 1760 1761 static void ena_init_napi_in_range(struct ena_adapter *adapter, 1762 int first_index, int count) 1763 { 1764 int i; 1765 1766 for (i = first_index; i < first_index + count; i++) { 1767 struct ena_napi *napi = &adapter->ena_napi[i]; 1768 1769 netif_napi_add(adapter->netdev, &napi->napi, 1770 ENA_IS_XDP_INDEX(adapter, i) ? ena_xdp_io_poll : ena_io_poll); 1771 1772 if (!ENA_IS_XDP_INDEX(adapter, i)) 1773 napi->rx_ring = &adapter->rx_ring[i]; 1774 1775 napi->tx_ring = &adapter->tx_ring[i]; 1776 napi->qid = i; 1777 } 1778 } 1779 1780 static void ena_napi_disable_in_range(struct ena_adapter *adapter, 1781 int first_index, 1782 int count) 1783 { 1784 int i; 1785 1786 for (i = first_index; i < first_index + count; i++) 1787 napi_disable(&adapter->ena_napi[i].napi); 1788 } 1789 1790 static void ena_napi_enable_in_range(struct ena_adapter *adapter, 1791 int first_index, 1792 int count) 1793 { 1794 int i; 1795 1796 for (i = first_index; i < first_index + count; i++) 1797 napi_enable(&adapter->ena_napi[i].napi); 1798 } 1799 1800 /* Configure the Rx forwarding */ 1801 static int ena_rss_configure(struct ena_adapter *adapter) 1802 { 1803 struct ena_com_dev *ena_dev = adapter->ena_dev; 1804 int rc; 1805 1806 /* In case the RSS table wasn't initialized by probe */ 1807 if (!ena_dev->rss.tbl_log_size) { 1808 rc = ena_rss_init_default(adapter); 1809 if (rc && (rc != -EOPNOTSUPP)) { 1810 netif_err(adapter, ifup, adapter->netdev, 1811 "Failed to init RSS rc: %d\n", rc); 1812 return rc; 1813 } 1814 } 1815 1816 /* Set indirect table */ 1817 rc = ena_com_indirect_table_set(ena_dev); 1818 if (unlikely(rc && rc != -EOPNOTSUPP)) 1819 return rc; 1820 1821 /* Configure hash function (if supported) */ 1822 rc = ena_com_set_hash_function(ena_dev); 1823 if (unlikely(rc && (rc != -EOPNOTSUPP))) 1824 return rc; 1825 1826 /* Configure hash inputs (if supported) */ 1827 rc = ena_com_set_hash_ctrl(ena_dev); 1828 if (unlikely(rc && (rc != -EOPNOTSUPP))) 1829 return rc; 1830 1831 return 0; 1832 } 1833 1834 static int ena_up_complete(struct ena_adapter *adapter) 1835 { 1836 int rc; 1837 1838 rc = ena_rss_configure(adapter); 1839 if (rc) 1840 return rc; 1841 1842 ena_change_mtu(adapter->netdev, adapter->netdev->mtu); 1843 1844 ena_refill_all_rx_bufs(adapter); 1845 1846 /* enable transmits */ 1847 netif_tx_start_all_queues(adapter->netdev); 1848 1849 ena_napi_enable_in_range(adapter, 1850 0, 1851 adapter->xdp_num_queues + adapter->num_io_queues); 1852 1853 return 0; 1854 } 1855 1856 static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid) 1857 { 1858 struct ena_com_create_io_ctx ctx; 1859 struct ena_com_dev *ena_dev; 1860 struct ena_ring *tx_ring; 1861 u32 msix_vector; 1862 u16 ena_qid; 1863 int rc; 1864 1865 ena_dev = adapter->ena_dev; 1866 1867 tx_ring = &adapter->tx_ring[qid]; 1868 msix_vector = ENA_IO_IRQ_IDX(qid); 1869 ena_qid = ENA_IO_TXQ_IDX(qid); 1870 1871 memset(&ctx, 0x0, sizeof(ctx)); 1872 1873 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX; 1874 ctx.qid = ena_qid; 1875 ctx.mem_queue_type = ena_dev->tx_mem_queue_type; 1876 ctx.msix_vector = msix_vector; 1877 ctx.queue_size = tx_ring->ring_size; 1878 ctx.numa_node = tx_ring->numa_node; 1879 1880 rc = ena_com_create_io_queue(ena_dev, &ctx); 1881 if (rc) { 1882 netif_err(adapter, ifup, adapter->netdev, 1883 "Failed to create I/O TX queue num %d rc: %d\n", 1884 qid, rc); 1885 return rc; 1886 } 1887 1888 rc = ena_com_get_io_handlers(ena_dev, ena_qid, 1889 &tx_ring->ena_com_io_sq, 1890 &tx_ring->ena_com_io_cq); 1891 if (rc) { 1892 netif_err(adapter, ifup, adapter->netdev, 1893 "Failed to get TX queue handlers. TX queue num %d rc: %d\n", 1894 qid, rc); 1895 ena_com_destroy_io_queue(ena_dev, ena_qid); 1896 return rc; 1897 } 1898 1899 ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node); 1900 return rc; 1901 } 1902 1903 int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter, 1904 int first_index, int count) 1905 { 1906 struct ena_com_dev *ena_dev = adapter->ena_dev; 1907 int rc, i; 1908 1909 for (i = first_index; i < first_index + count; i++) { 1910 rc = ena_create_io_tx_queue(adapter, i); 1911 if (rc) 1912 goto create_err; 1913 } 1914 1915 return 0; 1916 1917 create_err: 1918 while (i-- > first_index) 1919 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i)); 1920 1921 return rc; 1922 } 1923 1924 static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid) 1925 { 1926 struct ena_com_dev *ena_dev; 1927 struct ena_com_create_io_ctx ctx; 1928 struct ena_ring *rx_ring; 1929 u32 msix_vector; 1930 u16 ena_qid; 1931 int rc; 1932 1933 ena_dev = adapter->ena_dev; 1934 1935 rx_ring = &adapter->rx_ring[qid]; 1936 msix_vector = ENA_IO_IRQ_IDX(qid); 1937 ena_qid = ENA_IO_RXQ_IDX(qid); 1938 1939 memset(&ctx, 0x0, sizeof(ctx)); 1940 1941 ctx.qid = ena_qid; 1942 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX; 1943 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 1944 ctx.msix_vector = msix_vector; 1945 ctx.queue_size = rx_ring->ring_size; 1946 ctx.numa_node = rx_ring->numa_node; 1947 1948 rc = ena_com_create_io_queue(ena_dev, &ctx); 1949 if (rc) { 1950 netif_err(adapter, ifup, adapter->netdev, 1951 "Failed to create I/O RX queue num %d rc: %d\n", 1952 qid, rc); 1953 return rc; 1954 } 1955 1956 rc = ena_com_get_io_handlers(ena_dev, ena_qid, 1957 &rx_ring->ena_com_io_sq, 1958 &rx_ring->ena_com_io_cq); 1959 if (rc) { 1960 netif_err(adapter, ifup, adapter->netdev, 1961 "Failed to get RX queue handlers. RX queue num %d rc: %d\n", 1962 qid, rc); 1963 goto err; 1964 } 1965 1966 ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node); 1967 1968 return rc; 1969 err: 1970 ena_com_destroy_io_queue(ena_dev, ena_qid); 1971 return rc; 1972 } 1973 1974 static int ena_create_all_io_rx_queues(struct ena_adapter *adapter) 1975 { 1976 struct ena_com_dev *ena_dev = adapter->ena_dev; 1977 int rc, i; 1978 1979 for (i = 0; i < adapter->num_io_queues; i++) { 1980 rc = ena_create_io_rx_queue(adapter, i); 1981 if (rc) 1982 goto create_err; 1983 INIT_WORK(&adapter->ena_napi[i].dim.work, ena_dim_work); 1984 } 1985 1986 return 0; 1987 1988 create_err: 1989 while (i--) { 1990 cancel_work_sync(&adapter->ena_napi[i].dim.work); 1991 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i)); 1992 } 1993 1994 return rc; 1995 } 1996 1997 static void set_io_rings_size(struct ena_adapter *adapter, 1998 int new_tx_size, 1999 int new_rx_size) 2000 { 2001 int i; 2002 2003 for (i = 0; i < adapter->num_io_queues; i++) { 2004 adapter->tx_ring[i].ring_size = new_tx_size; 2005 adapter->rx_ring[i].ring_size = new_rx_size; 2006 } 2007 } 2008 2009 /* This function allows queue allocation to backoff when the system is 2010 * low on memory. If there is not enough memory to allocate io queues 2011 * the driver will try to allocate smaller queues. 2012 * 2013 * The backoff algorithm is as follows: 2014 * 1. Try to allocate TX and RX and if successful. 2015 * 1.1. return success 2016 * 2017 * 2. Divide by 2 the size of the larger of RX and TX queues (or both if their size is the same). 2018 * 2019 * 3. If TX or RX is smaller than 256 2020 * 3.1. return failure. 2021 * 4. else 2022 * 4.1. go back to 1. 2023 */ 2024 static int create_queues_with_size_backoff(struct ena_adapter *adapter) 2025 { 2026 int rc, cur_rx_ring_size, cur_tx_ring_size; 2027 int new_rx_ring_size, new_tx_ring_size; 2028 2029 /* current queue sizes might be set to smaller than the requested 2030 * ones due to past queue allocation failures. 2031 */ 2032 set_io_rings_size(adapter, adapter->requested_tx_ring_size, 2033 adapter->requested_rx_ring_size); 2034 2035 while (1) { 2036 if (ena_xdp_present(adapter)) { 2037 rc = ena_setup_and_create_all_xdp_queues(adapter); 2038 2039 if (rc) 2040 goto err_setup_tx; 2041 } 2042 rc = ena_setup_tx_resources_in_range(adapter, 2043 0, 2044 adapter->num_io_queues); 2045 if (rc) 2046 goto err_setup_tx; 2047 2048 rc = ena_create_io_tx_queues_in_range(adapter, 2049 0, 2050 adapter->num_io_queues); 2051 if (rc) 2052 goto err_create_tx_queues; 2053 2054 rc = ena_setup_all_rx_resources(adapter); 2055 if (rc) 2056 goto err_setup_rx; 2057 2058 rc = ena_create_all_io_rx_queues(adapter); 2059 if (rc) 2060 goto err_create_rx_queues; 2061 2062 return 0; 2063 2064 err_create_rx_queues: 2065 ena_free_all_io_rx_resources(adapter); 2066 err_setup_rx: 2067 ena_destroy_all_tx_queues(adapter); 2068 err_create_tx_queues: 2069 ena_free_all_io_tx_resources(adapter); 2070 err_setup_tx: 2071 if (rc != -ENOMEM) { 2072 netif_err(adapter, ifup, adapter->netdev, 2073 "Queue creation failed with error code %d\n", 2074 rc); 2075 return rc; 2076 } 2077 2078 cur_tx_ring_size = adapter->tx_ring[0].ring_size; 2079 cur_rx_ring_size = adapter->rx_ring[0].ring_size; 2080 2081 netif_err(adapter, ifup, adapter->netdev, 2082 "Not enough memory to create queues with sizes TX=%d, RX=%d\n", 2083 cur_tx_ring_size, cur_rx_ring_size); 2084 2085 new_tx_ring_size = cur_tx_ring_size; 2086 new_rx_ring_size = cur_rx_ring_size; 2087 2088 /* Decrease the size of the larger queue, or 2089 * decrease both if they are the same size. 2090 */ 2091 if (cur_rx_ring_size <= cur_tx_ring_size) 2092 new_tx_ring_size = cur_tx_ring_size / 2; 2093 if (cur_rx_ring_size >= cur_tx_ring_size) 2094 new_rx_ring_size = cur_rx_ring_size / 2; 2095 2096 if (new_tx_ring_size < ENA_MIN_RING_SIZE || 2097 new_rx_ring_size < ENA_MIN_RING_SIZE) { 2098 netif_err(adapter, ifup, adapter->netdev, 2099 "Queue creation failed with the smallest possible queue size of %d for both queues. Not retrying with smaller queues\n", 2100 ENA_MIN_RING_SIZE); 2101 return rc; 2102 } 2103 2104 netif_err(adapter, ifup, adapter->netdev, 2105 "Retrying queue creation with sizes TX=%d, RX=%d\n", 2106 new_tx_ring_size, 2107 new_rx_ring_size); 2108 2109 set_io_rings_size(adapter, new_tx_ring_size, 2110 new_rx_ring_size); 2111 } 2112 } 2113 2114 int ena_up(struct ena_adapter *adapter) 2115 { 2116 int io_queue_count, rc, i; 2117 2118 netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__); 2119 2120 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues; 2121 ena_setup_io_intr(adapter); 2122 2123 /* napi poll functions should be initialized before running 2124 * request_irq(), to handle a rare condition where there is a pending 2125 * interrupt, causing the ISR to fire immediately while the poll 2126 * function wasn't set yet, causing a null dereference 2127 */ 2128 ena_init_napi_in_range(adapter, 0, io_queue_count); 2129 2130 rc = ena_request_io_irq(adapter); 2131 if (rc) 2132 goto err_req_irq; 2133 2134 rc = create_queues_with_size_backoff(adapter); 2135 if (rc) 2136 goto err_create_queues_with_backoff; 2137 2138 rc = ena_up_complete(adapter); 2139 if (rc) 2140 goto err_up; 2141 2142 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags)) 2143 netif_carrier_on(adapter->netdev); 2144 2145 ena_increase_stat(&adapter->dev_stats.interface_up, 1, 2146 &adapter->syncp); 2147 2148 set_bit(ENA_FLAG_DEV_UP, &adapter->flags); 2149 2150 /* Enable completion queues interrupt */ 2151 for (i = 0; i < adapter->num_io_queues; i++) 2152 ena_unmask_interrupt(&adapter->tx_ring[i], 2153 &adapter->rx_ring[i]); 2154 2155 /* schedule napi in case we had pending packets 2156 * from the last time we disable napi 2157 */ 2158 for (i = 0; i < io_queue_count; i++) 2159 napi_schedule(&adapter->ena_napi[i].napi); 2160 2161 return rc; 2162 2163 err_up: 2164 ena_destroy_all_tx_queues(adapter); 2165 ena_free_all_io_tx_resources(adapter); 2166 ena_destroy_all_rx_queues(adapter); 2167 ena_free_all_io_rx_resources(adapter); 2168 err_create_queues_with_backoff: 2169 ena_free_io_irq(adapter); 2170 err_req_irq: 2171 ena_del_napi_in_range(adapter, 0, io_queue_count); 2172 2173 return rc; 2174 } 2175 2176 void ena_down(struct ena_adapter *adapter) 2177 { 2178 int io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues; 2179 2180 netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__); 2181 2182 clear_bit(ENA_FLAG_DEV_UP, &adapter->flags); 2183 2184 ena_increase_stat(&adapter->dev_stats.interface_down, 1, 2185 &adapter->syncp); 2186 2187 netif_carrier_off(adapter->netdev); 2188 netif_tx_disable(adapter->netdev); 2189 2190 /* After this point the napi handler won't enable the tx queue */ 2191 ena_napi_disable_in_range(adapter, 0, io_queue_count); 2192 2193 /* After destroy the queue there won't be any new interrupts */ 2194 2195 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) { 2196 int rc; 2197 2198 rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason); 2199 if (rc) 2200 netif_err(adapter, ifdown, adapter->netdev, 2201 "Device reset failed\n"); 2202 /* stop submitting admin commands on a device that was reset */ 2203 ena_com_set_admin_running_state(adapter->ena_dev, false); 2204 } 2205 2206 ena_destroy_all_io_queues(adapter); 2207 2208 ena_disable_io_intr_sync(adapter); 2209 ena_free_io_irq(adapter); 2210 ena_del_napi_in_range(adapter, 0, io_queue_count); 2211 2212 ena_free_all_tx_bufs(adapter); 2213 ena_free_all_rx_bufs(adapter); 2214 ena_free_all_io_tx_resources(adapter); 2215 ena_free_all_io_rx_resources(adapter); 2216 } 2217 2218 /* ena_open - Called when a network interface is made active 2219 * @netdev: network interface device structure 2220 * 2221 * Returns 0 on success, negative value on failure 2222 * 2223 * The open entry point is called when a network interface is made 2224 * active by the system (IFF_UP). At this point all resources needed 2225 * for transmit and receive operations are allocated, the interrupt 2226 * handler is registered with the OS, the watchdog timer is started, 2227 * and the stack is notified that the interface is ready. 2228 */ 2229 static int ena_open(struct net_device *netdev) 2230 { 2231 struct ena_adapter *adapter = netdev_priv(netdev); 2232 int rc; 2233 2234 /* Notify the stack of the actual queue counts. */ 2235 rc = netif_set_real_num_tx_queues(netdev, adapter->num_io_queues); 2236 if (rc) { 2237 netif_err(adapter, ifup, netdev, "Can't set num tx queues\n"); 2238 return rc; 2239 } 2240 2241 rc = netif_set_real_num_rx_queues(netdev, adapter->num_io_queues); 2242 if (rc) { 2243 netif_err(adapter, ifup, netdev, "Can't set num rx queues\n"); 2244 return rc; 2245 } 2246 2247 rc = ena_up(adapter); 2248 if (rc) 2249 return rc; 2250 2251 return rc; 2252 } 2253 2254 /* ena_close - Disables a network interface 2255 * @netdev: network interface device structure 2256 * 2257 * Returns 0, this is not allowed to fail 2258 * 2259 * The close entry point is called when an interface is de-activated 2260 * by the OS. The hardware is still under the drivers control, but 2261 * needs to be disabled. A global MAC reset is issued to stop the 2262 * hardware, and all transmit and receive resources are freed. 2263 */ 2264 static int ena_close(struct net_device *netdev) 2265 { 2266 struct ena_adapter *adapter = netdev_priv(netdev); 2267 2268 netif_dbg(adapter, ifdown, netdev, "%s\n", __func__); 2269 2270 if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)) 2271 return 0; 2272 2273 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 2274 ena_down(adapter); 2275 2276 /* Check for device status and issue reset if needed*/ 2277 check_for_admin_com_state(adapter); 2278 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 2279 netif_err(adapter, ifdown, adapter->netdev, 2280 "Destroy failure, restarting device\n"); 2281 ena_dump_stats_to_dmesg(adapter); 2282 /* rtnl lock already obtained in dev_ioctl() layer */ 2283 ena_destroy_device(adapter, false); 2284 ena_restore_device(adapter); 2285 } 2286 2287 return 0; 2288 } 2289 2290 int ena_update_queue_params(struct ena_adapter *adapter, 2291 u32 new_tx_size, 2292 u32 new_rx_size, 2293 u32 new_llq_header_len) 2294 { 2295 bool dev_was_up, large_llq_changed = false; 2296 int rc = 0; 2297 2298 dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags); 2299 ena_close(adapter->netdev); 2300 adapter->requested_tx_ring_size = new_tx_size; 2301 adapter->requested_rx_ring_size = new_rx_size; 2302 ena_init_io_rings(adapter, 2303 0, 2304 adapter->xdp_num_queues + 2305 adapter->num_io_queues); 2306 2307 large_llq_changed = adapter->ena_dev->tx_mem_queue_type == 2308 ENA_ADMIN_PLACEMENT_POLICY_DEV; 2309 large_llq_changed &= 2310 new_llq_header_len != adapter->ena_dev->tx_max_header_size; 2311 2312 /* a check that the configuration is valid is done by caller */ 2313 if (large_llq_changed) { 2314 adapter->large_llq_header_enabled = !adapter->large_llq_header_enabled; 2315 2316 ena_destroy_device(adapter, false); 2317 rc = ena_restore_device(adapter); 2318 } 2319 2320 return dev_was_up && !rc ? ena_up(adapter) : rc; 2321 } 2322 2323 int ena_set_rx_copybreak(struct ena_adapter *adapter, u32 rx_copybreak) 2324 { 2325 struct ena_ring *rx_ring; 2326 int i; 2327 2328 if (rx_copybreak > min_t(u16, adapter->netdev->mtu, ENA_PAGE_SIZE)) 2329 return -EINVAL; 2330 2331 adapter->rx_copybreak = rx_copybreak; 2332 2333 for (i = 0; i < adapter->num_io_queues; i++) { 2334 rx_ring = &adapter->rx_ring[i]; 2335 rx_ring->rx_copybreak = rx_copybreak; 2336 } 2337 2338 return 0; 2339 } 2340 2341 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count) 2342 { 2343 struct ena_com_dev *ena_dev = adapter->ena_dev; 2344 int prev_channel_count; 2345 bool dev_was_up; 2346 2347 dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags); 2348 ena_close(adapter->netdev); 2349 prev_channel_count = adapter->num_io_queues; 2350 adapter->num_io_queues = new_channel_count; 2351 if (ena_xdp_present(adapter) && 2352 ena_xdp_allowed(adapter) == ENA_XDP_ALLOWED) { 2353 adapter->xdp_first_ring = new_channel_count; 2354 adapter->xdp_num_queues = new_channel_count; 2355 if (prev_channel_count > new_channel_count) 2356 ena_xdp_exchange_program_rx_in_range(adapter, 2357 NULL, 2358 new_channel_count, 2359 prev_channel_count); 2360 else 2361 ena_xdp_exchange_program_rx_in_range(adapter, 2362 adapter->xdp_bpf_prog, 2363 prev_channel_count, 2364 new_channel_count); 2365 } 2366 2367 /* We need to destroy the rss table so that the indirection 2368 * table will be reinitialized by ena_up() 2369 */ 2370 ena_com_rss_destroy(ena_dev); 2371 ena_init_io_rings(adapter, 2372 0, 2373 adapter->xdp_num_queues + 2374 adapter->num_io_queues); 2375 return dev_was_up ? ena_open(adapter->netdev) : 0; 2376 } 2377 2378 static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, 2379 struct sk_buff *skb, 2380 bool disable_meta_caching) 2381 { 2382 u32 mss = skb_shinfo(skb)->gso_size; 2383 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta; 2384 u8 l4_protocol = 0; 2385 2386 if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) { 2387 ena_tx_ctx->l4_csum_enable = 1; 2388 if (mss) { 2389 ena_tx_ctx->tso_enable = 1; 2390 ena_meta->l4_hdr_len = tcp_hdr(skb)->doff; 2391 ena_tx_ctx->l4_csum_partial = 0; 2392 } else { 2393 ena_tx_ctx->tso_enable = 0; 2394 ena_meta->l4_hdr_len = 0; 2395 ena_tx_ctx->l4_csum_partial = 1; 2396 } 2397 2398 switch (ip_hdr(skb)->version) { 2399 case IPVERSION: 2400 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4; 2401 if (ip_hdr(skb)->frag_off & htons(IP_DF)) 2402 ena_tx_ctx->df = 1; 2403 if (mss) 2404 ena_tx_ctx->l3_csum_enable = 1; 2405 l4_protocol = ip_hdr(skb)->protocol; 2406 break; 2407 case 6: 2408 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6; 2409 l4_protocol = ipv6_hdr(skb)->nexthdr; 2410 break; 2411 default: 2412 break; 2413 } 2414 2415 if (l4_protocol == IPPROTO_TCP) 2416 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP; 2417 else 2418 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP; 2419 2420 ena_meta->mss = mss; 2421 ena_meta->l3_hdr_len = skb_network_header_len(skb); 2422 ena_meta->l3_hdr_offset = skb_network_offset(skb); 2423 ena_tx_ctx->meta_valid = 1; 2424 } else if (disable_meta_caching) { 2425 memset(ena_meta, 0, sizeof(*ena_meta)); 2426 ena_tx_ctx->meta_valid = 1; 2427 } else { 2428 ena_tx_ctx->meta_valid = 0; 2429 } 2430 } 2431 2432 static int ena_check_and_linearize_skb(struct ena_ring *tx_ring, 2433 struct sk_buff *skb) 2434 { 2435 int num_frags, header_len, rc; 2436 2437 num_frags = skb_shinfo(skb)->nr_frags; 2438 header_len = skb_headlen(skb); 2439 2440 if (num_frags < tx_ring->sgl_size) 2441 return 0; 2442 2443 if ((num_frags == tx_ring->sgl_size) && 2444 (header_len < tx_ring->tx_max_header_size)) 2445 return 0; 2446 2447 ena_increase_stat(&tx_ring->tx_stats.linearize, 1, &tx_ring->syncp); 2448 2449 rc = skb_linearize(skb); 2450 if (unlikely(rc)) { 2451 ena_increase_stat(&tx_ring->tx_stats.linearize_failed, 1, 2452 &tx_ring->syncp); 2453 } 2454 2455 return rc; 2456 } 2457 2458 static int ena_tx_map_skb(struct ena_ring *tx_ring, 2459 struct ena_tx_buffer *tx_info, 2460 struct sk_buff *skb, 2461 void **push_hdr, 2462 u16 *header_len) 2463 { 2464 struct ena_adapter *adapter = tx_ring->adapter; 2465 struct ena_com_buf *ena_buf; 2466 dma_addr_t dma; 2467 u32 skb_head_len, frag_len, last_frag; 2468 u16 push_len = 0; 2469 u16 delta = 0; 2470 int i = 0; 2471 2472 skb_head_len = skb_headlen(skb); 2473 tx_info->skb = skb; 2474 ena_buf = tx_info->bufs; 2475 2476 if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) { 2477 /* When the device is LLQ mode, the driver will copy 2478 * the header into the device memory space. 2479 * the ena_com layer assume the header is in a linear 2480 * memory space. 2481 * This assumption might be wrong since part of the header 2482 * can be in the fragmented buffers. 2483 * Use skb_header_pointer to make sure the header is in a 2484 * linear memory space. 2485 */ 2486 2487 push_len = min_t(u32, skb->len, tx_ring->tx_max_header_size); 2488 *push_hdr = skb_header_pointer(skb, 0, push_len, 2489 tx_ring->push_buf_intermediate_buf); 2490 *header_len = push_len; 2491 if (unlikely(skb->data != *push_hdr)) { 2492 ena_increase_stat(&tx_ring->tx_stats.llq_buffer_copy, 1, 2493 &tx_ring->syncp); 2494 2495 delta = push_len - skb_head_len; 2496 } 2497 } else { 2498 *push_hdr = NULL; 2499 *header_len = min_t(u32, skb_head_len, 2500 tx_ring->tx_max_header_size); 2501 } 2502 2503 netif_dbg(adapter, tx_queued, adapter->netdev, 2504 "skb: %p header_buf->vaddr: %p push_len: %d\n", skb, 2505 *push_hdr, push_len); 2506 2507 if (skb_head_len > push_len) { 2508 dma = dma_map_single(tx_ring->dev, skb->data + push_len, 2509 skb_head_len - push_len, DMA_TO_DEVICE); 2510 if (unlikely(dma_mapping_error(tx_ring->dev, dma))) 2511 goto error_report_dma_error; 2512 2513 ena_buf->paddr = dma; 2514 ena_buf->len = skb_head_len - push_len; 2515 2516 ena_buf++; 2517 tx_info->num_of_bufs++; 2518 tx_info->map_linear_data = 1; 2519 } else { 2520 tx_info->map_linear_data = 0; 2521 } 2522 2523 last_frag = skb_shinfo(skb)->nr_frags; 2524 2525 for (i = 0; i < last_frag; i++) { 2526 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2527 2528 frag_len = skb_frag_size(frag); 2529 2530 if (unlikely(delta >= frag_len)) { 2531 delta -= frag_len; 2532 continue; 2533 } 2534 2535 dma = skb_frag_dma_map(tx_ring->dev, frag, delta, 2536 frag_len - delta, DMA_TO_DEVICE); 2537 if (unlikely(dma_mapping_error(tx_ring->dev, dma))) 2538 goto error_report_dma_error; 2539 2540 ena_buf->paddr = dma; 2541 ena_buf->len = frag_len - delta; 2542 ena_buf++; 2543 tx_info->num_of_bufs++; 2544 delta = 0; 2545 } 2546 2547 return 0; 2548 2549 error_report_dma_error: 2550 ena_increase_stat(&tx_ring->tx_stats.dma_mapping_err, 1, 2551 &tx_ring->syncp); 2552 netif_warn(adapter, tx_queued, adapter->netdev, "Failed to map skb\n"); 2553 2554 tx_info->skb = NULL; 2555 2556 tx_info->num_of_bufs += i; 2557 ena_unmap_tx_buff(tx_ring, tx_info); 2558 2559 return -EINVAL; 2560 } 2561 2562 /* Called with netif_tx_lock. */ 2563 static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev) 2564 { 2565 struct ena_adapter *adapter = netdev_priv(dev); 2566 struct ena_tx_buffer *tx_info; 2567 struct ena_com_tx_ctx ena_tx_ctx; 2568 struct ena_ring *tx_ring; 2569 struct netdev_queue *txq; 2570 void *push_hdr; 2571 u16 next_to_use, req_id, header_len; 2572 int qid, rc; 2573 2574 netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb); 2575 /* Determine which tx ring we will be placed on */ 2576 qid = skb_get_queue_mapping(skb); 2577 tx_ring = &adapter->tx_ring[qid]; 2578 txq = netdev_get_tx_queue(dev, qid); 2579 2580 rc = ena_check_and_linearize_skb(tx_ring, skb); 2581 if (unlikely(rc)) 2582 goto error_drop_packet; 2583 2584 skb_tx_timestamp(skb); 2585 2586 next_to_use = tx_ring->next_to_use; 2587 req_id = tx_ring->free_ids[next_to_use]; 2588 tx_info = &tx_ring->tx_buffer_info[req_id]; 2589 tx_info->num_of_bufs = 0; 2590 2591 WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id); 2592 2593 rc = ena_tx_map_skb(tx_ring, tx_info, skb, &push_hdr, &header_len); 2594 if (unlikely(rc)) 2595 goto error_drop_packet; 2596 2597 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx)); 2598 ena_tx_ctx.ena_bufs = tx_info->bufs; 2599 ena_tx_ctx.push_header = push_hdr; 2600 ena_tx_ctx.num_bufs = tx_info->num_of_bufs; 2601 ena_tx_ctx.req_id = req_id; 2602 ena_tx_ctx.header_len = header_len; 2603 2604 /* set flags and meta data */ 2605 ena_tx_csum(&ena_tx_ctx, skb, tx_ring->disable_meta_caching); 2606 2607 rc = ena_xmit_common(adapter, 2608 tx_ring, 2609 tx_info, 2610 &ena_tx_ctx, 2611 next_to_use, 2612 skb->len); 2613 if (rc) 2614 goto error_unmap_dma; 2615 2616 netdev_tx_sent_queue(txq, skb->len); 2617 2618 /* stop the queue when no more space available, the packet can have up 2619 * to sgl_size + 2. one for the meta descriptor and one for header 2620 * (if the header is larger than tx_max_header_size). 2621 */ 2622 if (unlikely(!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq, 2623 tx_ring->sgl_size + 2))) { 2624 netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n", 2625 __func__, qid); 2626 2627 netif_tx_stop_queue(txq); 2628 ena_increase_stat(&tx_ring->tx_stats.queue_stop, 1, 2629 &tx_ring->syncp); 2630 2631 /* There is a rare condition where this function decide to 2632 * stop the queue but meanwhile clean_tx_irq updates 2633 * next_to_completion and terminates. 2634 * The queue will remain stopped forever. 2635 * To solve this issue add a mb() to make sure that 2636 * netif_tx_stop_queue() write is vissible before checking if 2637 * there is additional space in the queue. 2638 */ 2639 smp_mb(); 2640 2641 if (ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq, 2642 ENA_TX_WAKEUP_THRESH)) { 2643 netif_tx_wake_queue(txq); 2644 ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1, 2645 &tx_ring->syncp); 2646 } 2647 } 2648 2649 if (netif_xmit_stopped(txq) || !netdev_xmit_more()) 2650 /* trigger the dma engine. ena_ring_tx_doorbell() 2651 * calls a memory barrier inside it. 2652 */ 2653 ena_ring_tx_doorbell(tx_ring); 2654 2655 return NETDEV_TX_OK; 2656 2657 error_unmap_dma: 2658 ena_unmap_tx_buff(tx_ring, tx_info); 2659 tx_info->skb = NULL; 2660 2661 error_drop_packet: 2662 dev_kfree_skb(skb); 2663 return NETDEV_TX_OK; 2664 } 2665 2666 static void ena_config_host_info(struct ena_com_dev *ena_dev, struct pci_dev *pdev) 2667 { 2668 struct device *dev = &pdev->dev; 2669 struct ena_admin_host_info *host_info; 2670 int rc; 2671 2672 /* Allocate only the host info */ 2673 rc = ena_com_allocate_host_info(ena_dev); 2674 if (rc) { 2675 dev_err(dev, "Cannot allocate host info\n"); 2676 return; 2677 } 2678 2679 host_info = ena_dev->host_attr.host_info; 2680 2681 host_info->bdf = pci_dev_id(pdev); 2682 host_info->os_type = ENA_ADMIN_OS_LINUX; 2683 host_info->kernel_ver = LINUX_VERSION_CODE; 2684 strscpy(host_info->kernel_ver_str, utsname()->version, 2685 sizeof(host_info->kernel_ver_str) - 1); 2686 host_info->os_dist = 0; 2687 strncpy(host_info->os_dist_str, utsname()->release, 2688 sizeof(host_info->os_dist_str) - 1); 2689 host_info->driver_version = 2690 (DRV_MODULE_GEN_MAJOR) | 2691 (DRV_MODULE_GEN_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) | 2692 (DRV_MODULE_GEN_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT) | 2693 ("K"[0] << ENA_ADMIN_HOST_INFO_MODULE_TYPE_SHIFT); 2694 host_info->num_cpus = num_online_cpus(); 2695 2696 host_info->driver_supported_features = 2697 ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK | 2698 ENA_ADMIN_HOST_INFO_INTERRUPT_MODERATION_MASK | 2699 ENA_ADMIN_HOST_INFO_RX_BUF_MIRRORING_MASK | 2700 ENA_ADMIN_HOST_INFO_RSS_CONFIGURABLE_FUNCTION_KEY_MASK | 2701 ENA_ADMIN_HOST_INFO_RX_PAGE_REUSE_MASK; 2702 2703 rc = ena_com_set_host_attributes(ena_dev); 2704 if (rc) { 2705 if (rc == -EOPNOTSUPP) 2706 dev_warn(dev, "Cannot set host attributes\n"); 2707 else 2708 dev_err(dev, "Cannot set host attributes\n"); 2709 2710 goto err; 2711 } 2712 2713 return; 2714 2715 err: 2716 ena_com_delete_host_info(ena_dev); 2717 } 2718 2719 static void ena_config_debug_area(struct ena_adapter *adapter) 2720 { 2721 u32 debug_area_size; 2722 int rc, ss_count; 2723 2724 ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS); 2725 if (ss_count <= 0) { 2726 netif_err(adapter, drv, adapter->netdev, 2727 "SS count is negative\n"); 2728 return; 2729 } 2730 2731 /* allocate 32 bytes for each string and 64bit for the value */ 2732 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count; 2733 2734 rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size); 2735 if (rc) { 2736 netif_err(adapter, drv, adapter->netdev, 2737 "Cannot allocate debug area\n"); 2738 return; 2739 } 2740 2741 rc = ena_com_set_host_attributes(adapter->ena_dev); 2742 if (rc) { 2743 if (rc == -EOPNOTSUPP) 2744 netif_warn(adapter, drv, adapter->netdev, 2745 "Cannot set host attributes\n"); 2746 else 2747 netif_err(adapter, drv, adapter->netdev, 2748 "Cannot set host attributes\n"); 2749 goto err; 2750 } 2751 2752 return; 2753 err: 2754 ena_com_delete_debug_area(adapter->ena_dev); 2755 } 2756 2757 int ena_update_hw_stats(struct ena_adapter *adapter) 2758 { 2759 int rc; 2760 2761 rc = ena_com_get_eni_stats(adapter->ena_dev, &adapter->eni_stats); 2762 if (rc) { 2763 netdev_err(adapter->netdev, "Failed to get ENI stats\n"); 2764 return rc; 2765 } 2766 2767 return 0; 2768 } 2769 2770 static void ena_get_stats64(struct net_device *netdev, 2771 struct rtnl_link_stats64 *stats) 2772 { 2773 struct ena_adapter *adapter = netdev_priv(netdev); 2774 struct ena_ring *rx_ring, *tx_ring; 2775 unsigned int start; 2776 u64 rx_drops; 2777 u64 tx_drops; 2778 int i; 2779 2780 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 2781 return; 2782 2783 for (i = 0; i < adapter->num_io_queues; i++) { 2784 u64 bytes, packets; 2785 2786 tx_ring = &adapter->tx_ring[i]; 2787 2788 do { 2789 start = u64_stats_fetch_begin(&tx_ring->syncp); 2790 packets = tx_ring->tx_stats.cnt; 2791 bytes = tx_ring->tx_stats.bytes; 2792 } while (u64_stats_fetch_retry(&tx_ring->syncp, start)); 2793 2794 stats->tx_packets += packets; 2795 stats->tx_bytes += bytes; 2796 2797 rx_ring = &adapter->rx_ring[i]; 2798 2799 do { 2800 start = u64_stats_fetch_begin(&rx_ring->syncp); 2801 packets = rx_ring->rx_stats.cnt; 2802 bytes = rx_ring->rx_stats.bytes; 2803 } while (u64_stats_fetch_retry(&rx_ring->syncp, start)); 2804 2805 stats->rx_packets += packets; 2806 stats->rx_bytes += bytes; 2807 } 2808 2809 do { 2810 start = u64_stats_fetch_begin(&adapter->syncp); 2811 rx_drops = adapter->dev_stats.rx_drops; 2812 tx_drops = adapter->dev_stats.tx_drops; 2813 } while (u64_stats_fetch_retry(&adapter->syncp, start)); 2814 2815 stats->rx_dropped = rx_drops; 2816 stats->tx_dropped = tx_drops; 2817 2818 stats->multicast = 0; 2819 stats->collisions = 0; 2820 2821 stats->rx_length_errors = 0; 2822 stats->rx_crc_errors = 0; 2823 stats->rx_frame_errors = 0; 2824 stats->rx_fifo_errors = 0; 2825 stats->rx_missed_errors = 0; 2826 stats->tx_window_errors = 0; 2827 2828 stats->rx_errors = 0; 2829 stats->tx_errors = 0; 2830 } 2831 2832 static const struct net_device_ops ena_netdev_ops = { 2833 .ndo_open = ena_open, 2834 .ndo_stop = ena_close, 2835 .ndo_start_xmit = ena_start_xmit, 2836 .ndo_get_stats64 = ena_get_stats64, 2837 .ndo_tx_timeout = ena_tx_timeout, 2838 .ndo_change_mtu = ena_change_mtu, 2839 .ndo_set_mac_address = NULL, 2840 .ndo_validate_addr = eth_validate_addr, 2841 .ndo_bpf = ena_xdp, 2842 .ndo_xdp_xmit = ena_xdp_xmit, 2843 }; 2844 2845 static void ena_calc_io_queue_size(struct ena_adapter *adapter, 2846 struct ena_com_dev_get_features_ctx *get_feat_ctx) 2847 { 2848 struct ena_admin_feature_llq_desc *llq = &get_feat_ctx->llq; 2849 struct ena_com_dev *ena_dev = adapter->ena_dev; 2850 u32 tx_queue_size = ENA_DEFAULT_RING_SIZE; 2851 u32 rx_queue_size = ENA_DEFAULT_RING_SIZE; 2852 u32 max_tx_queue_size; 2853 u32 max_rx_queue_size; 2854 2855 /* If this function is called after driver load, the ring sizes have already 2856 * been configured. Take it into account when recalculating ring size. 2857 */ 2858 if (adapter->tx_ring->ring_size) 2859 tx_queue_size = adapter->tx_ring->ring_size; 2860 2861 if (adapter->rx_ring->ring_size) 2862 rx_queue_size = adapter->rx_ring->ring_size; 2863 2864 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) { 2865 struct ena_admin_queue_ext_feature_fields *max_queue_ext = 2866 &get_feat_ctx->max_queue_ext.max_queue_ext; 2867 max_rx_queue_size = min_t(u32, max_queue_ext->max_rx_cq_depth, 2868 max_queue_ext->max_rx_sq_depth); 2869 max_tx_queue_size = max_queue_ext->max_tx_cq_depth; 2870 2871 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 2872 max_tx_queue_size = min_t(u32, max_tx_queue_size, 2873 llq->max_llq_depth); 2874 else 2875 max_tx_queue_size = min_t(u32, max_tx_queue_size, 2876 max_queue_ext->max_tx_sq_depth); 2877 2878 adapter->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 2879 max_queue_ext->max_per_packet_tx_descs); 2880 adapter->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 2881 max_queue_ext->max_per_packet_rx_descs); 2882 } else { 2883 struct ena_admin_queue_feature_desc *max_queues = 2884 &get_feat_ctx->max_queues; 2885 max_rx_queue_size = min_t(u32, max_queues->max_cq_depth, 2886 max_queues->max_sq_depth); 2887 max_tx_queue_size = max_queues->max_cq_depth; 2888 2889 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 2890 max_tx_queue_size = min_t(u32, max_tx_queue_size, 2891 llq->max_llq_depth); 2892 else 2893 max_tx_queue_size = min_t(u32, max_tx_queue_size, 2894 max_queues->max_sq_depth); 2895 2896 adapter->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 2897 max_queues->max_packet_tx_descs); 2898 adapter->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 2899 max_queues->max_packet_rx_descs); 2900 } 2901 2902 max_tx_queue_size = rounddown_pow_of_two(max_tx_queue_size); 2903 max_rx_queue_size = rounddown_pow_of_two(max_rx_queue_size); 2904 2905 /* When forcing large headers, we multiply the entry size by 2, and therefore divide 2906 * the queue size by 2, leaving the amount of memory used by the queues unchanged. 2907 */ 2908 if (adapter->large_llq_header_enabled) { 2909 if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) && 2910 ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) { 2911 max_tx_queue_size /= 2; 2912 dev_info(&adapter->pdev->dev, 2913 "Forcing large headers and decreasing maximum TX queue size to %d\n", 2914 max_tx_queue_size); 2915 } else { 2916 dev_err(&adapter->pdev->dev, 2917 "Forcing large headers failed: LLQ is disabled or device does not support large headers\n"); 2918 2919 adapter->large_llq_header_enabled = false; 2920 } 2921 } 2922 2923 tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE, 2924 max_tx_queue_size); 2925 rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE, 2926 max_rx_queue_size); 2927 2928 tx_queue_size = rounddown_pow_of_two(tx_queue_size); 2929 rx_queue_size = rounddown_pow_of_two(rx_queue_size); 2930 2931 adapter->max_tx_ring_size = max_tx_queue_size; 2932 adapter->max_rx_ring_size = max_rx_queue_size; 2933 adapter->requested_tx_ring_size = tx_queue_size; 2934 adapter->requested_rx_ring_size = rx_queue_size; 2935 } 2936 2937 static int ena_device_validate_params(struct ena_adapter *adapter, 2938 struct ena_com_dev_get_features_ctx *get_feat_ctx) 2939 { 2940 struct net_device *netdev = adapter->netdev; 2941 int rc; 2942 2943 rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr, 2944 adapter->mac_addr); 2945 if (!rc) { 2946 netif_err(adapter, drv, netdev, 2947 "Error, mac address are different\n"); 2948 return -EINVAL; 2949 } 2950 2951 if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) { 2952 netif_err(adapter, drv, netdev, 2953 "Error, device max mtu is smaller than netdev MTU\n"); 2954 return -EINVAL; 2955 } 2956 2957 return 0; 2958 } 2959 2960 static void set_default_llq_configurations(struct ena_adapter *adapter, 2961 struct ena_llq_configurations *llq_config, 2962 struct ena_admin_feature_llq_desc *llq) 2963 { 2964 struct ena_com_dev *ena_dev = adapter->ena_dev; 2965 2966 llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER; 2967 llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY; 2968 llq_config->llq_num_decs_before_header = ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2; 2969 2970 adapter->large_llq_header_supported = 2971 !!(ena_dev->supported_features & BIT(ENA_ADMIN_LLQ)); 2972 adapter->large_llq_header_supported &= 2973 !!(llq->entry_size_ctrl_supported & 2974 ENA_ADMIN_LIST_ENTRY_SIZE_256B); 2975 2976 if ((llq->entry_size_ctrl_supported & ENA_ADMIN_LIST_ENTRY_SIZE_256B) && 2977 adapter->large_llq_header_enabled) { 2978 llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_256B; 2979 llq_config->llq_ring_entry_size_value = 256; 2980 } else { 2981 llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B; 2982 llq_config->llq_ring_entry_size_value = 128; 2983 } 2984 } 2985 2986 static int ena_set_queues_placement_policy(struct pci_dev *pdev, 2987 struct ena_com_dev *ena_dev, 2988 struct ena_admin_feature_llq_desc *llq, 2989 struct ena_llq_configurations *llq_default_configurations) 2990 { 2991 int rc; 2992 u32 llq_feature_mask; 2993 2994 llq_feature_mask = 1 << ENA_ADMIN_LLQ; 2995 if (!(ena_dev->supported_features & llq_feature_mask)) { 2996 dev_warn(&pdev->dev, 2997 "LLQ is not supported Fallback to host mode policy.\n"); 2998 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 2999 return 0; 3000 } 3001 3002 if (!ena_dev->mem_bar) { 3003 netdev_err(ena_dev->net_device, 3004 "LLQ is advertised as supported but device doesn't expose mem bar\n"); 3005 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 3006 return 0; 3007 } 3008 3009 rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations); 3010 if (unlikely(rc)) { 3011 dev_err(&pdev->dev, 3012 "Failed to configure the device mode. Fallback to host mode policy.\n"); 3013 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 3014 } 3015 3016 return 0; 3017 } 3018 3019 static int ena_map_llq_mem_bar(struct pci_dev *pdev, struct ena_com_dev *ena_dev, 3020 int bars) 3021 { 3022 bool has_mem_bar = !!(bars & BIT(ENA_MEM_BAR)); 3023 3024 if (!has_mem_bar) 3025 return 0; 3026 3027 ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev, 3028 pci_resource_start(pdev, ENA_MEM_BAR), 3029 pci_resource_len(pdev, ENA_MEM_BAR)); 3030 3031 if (!ena_dev->mem_bar) 3032 return -EFAULT; 3033 3034 return 0; 3035 } 3036 3037 static int ena_device_init(struct ena_adapter *adapter, struct pci_dev *pdev, 3038 struct ena_com_dev_get_features_ctx *get_feat_ctx, 3039 bool *wd_state) 3040 { 3041 struct ena_com_dev *ena_dev = adapter->ena_dev; 3042 struct ena_llq_configurations llq_config; 3043 struct device *dev = &pdev->dev; 3044 bool readless_supported; 3045 u32 aenq_groups; 3046 int dma_width; 3047 int rc; 3048 3049 rc = ena_com_mmio_reg_read_request_init(ena_dev); 3050 if (rc) { 3051 dev_err(dev, "Failed to init mmio read less\n"); 3052 return rc; 3053 } 3054 3055 /* The PCIe configuration space revision id indicate if mmio reg 3056 * read is disabled 3057 */ 3058 readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ); 3059 ena_com_set_mmio_read_mode(ena_dev, readless_supported); 3060 3061 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL); 3062 if (rc) { 3063 dev_err(dev, "Can not reset device\n"); 3064 goto err_mmio_read_less; 3065 } 3066 3067 rc = ena_com_validate_version(ena_dev); 3068 if (rc) { 3069 dev_err(dev, "Device version is too low\n"); 3070 goto err_mmio_read_less; 3071 } 3072 3073 dma_width = ena_com_get_dma_width(ena_dev); 3074 if (dma_width < 0) { 3075 dev_err(dev, "Invalid dma width value %d", dma_width); 3076 rc = dma_width; 3077 goto err_mmio_read_less; 3078 } 3079 3080 rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_width)); 3081 if (rc) { 3082 dev_err(dev, "dma_set_mask_and_coherent failed %d\n", rc); 3083 goto err_mmio_read_less; 3084 } 3085 3086 /* ENA admin level init */ 3087 rc = ena_com_admin_init(ena_dev, &aenq_handlers); 3088 if (rc) { 3089 dev_err(dev, 3090 "Can not initialize ena admin queue with device\n"); 3091 goto err_mmio_read_less; 3092 } 3093 3094 /* To enable the msix interrupts the driver needs to know the number 3095 * of queues. So the driver uses polling mode to retrieve this 3096 * information 3097 */ 3098 ena_com_set_admin_polling_mode(ena_dev, true); 3099 3100 ena_config_host_info(ena_dev, pdev); 3101 3102 /* Get Device Attributes*/ 3103 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx); 3104 if (rc) { 3105 dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc); 3106 goto err_admin_init; 3107 } 3108 3109 /* Try to turn all the available aenq groups */ 3110 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | 3111 BIT(ENA_ADMIN_FATAL_ERROR) | 3112 BIT(ENA_ADMIN_WARNING) | 3113 BIT(ENA_ADMIN_NOTIFICATION) | 3114 BIT(ENA_ADMIN_KEEP_ALIVE); 3115 3116 aenq_groups &= get_feat_ctx->aenq.supported_groups; 3117 3118 rc = ena_com_set_aenq_config(ena_dev, aenq_groups); 3119 if (rc) { 3120 dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc); 3121 goto err_admin_init; 3122 } 3123 3124 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE)); 3125 3126 set_default_llq_configurations(adapter, &llq_config, &get_feat_ctx->llq); 3127 3128 rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx->llq, 3129 &llq_config); 3130 if (rc) { 3131 dev_err(dev, "ENA device init failed\n"); 3132 goto err_admin_init; 3133 } 3134 3135 ena_calc_io_queue_size(adapter, get_feat_ctx); 3136 3137 return 0; 3138 3139 err_admin_init: 3140 ena_com_delete_host_info(ena_dev); 3141 ena_com_admin_destroy(ena_dev); 3142 err_mmio_read_less: 3143 ena_com_mmio_reg_read_request_destroy(ena_dev); 3144 3145 return rc; 3146 } 3147 3148 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter) 3149 { 3150 struct ena_com_dev *ena_dev = adapter->ena_dev; 3151 struct device *dev = &adapter->pdev->dev; 3152 int rc; 3153 3154 rc = ena_enable_msix(adapter); 3155 if (rc) { 3156 dev_err(dev, "Can not reserve msix vectors\n"); 3157 return rc; 3158 } 3159 3160 ena_setup_mgmnt_intr(adapter); 3161 3162 rc = ena_request_mgmnt_irq(adapter); 3163 if (rc) { 3164 dev_err(dev, "Can not setup management interrupts\n"); 3165 goto err_disable_msix; 3166 } 3167 3168 ena_com_set_admin_polling_mode(ena_dev, false); 3169 3170 ena_com_admin_aenq_enable(ena_dev); 3171 3172 return 0; 3173 3174 err_disable_msix: 3175 ena_disable_msix(adapter); 3176 3177 return rc; 3178 } 3179 3180 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful) 3181 { 3182 struct net_device *netdev = adapter->netdev; 3183 struct ena_com_dev *ena_dev = adapter->ena_dev; 3184 bool dev_up; 3185 3186 if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)) 3187 return; 3188 3189 netif_carrier_off(netdev); 3190 3191 del_timer_sync(&adapter->timer_service); 3192 3193 dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags); 3194 adapter->dev_up_before_reset = dev_up; 3195 if (!graceful) 3196 ena_com_set_admin_running_state(ena_dev, false); 3197 3198 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3199 ena_down(adapter); 3200 3201 /* Stop the device from sending AENQ events (in case reset flag is set 3202 * and device is up, ena_down() already reset the device. 3203 */ 3204 if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up)) 3205 ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason); 3206 3207 ena_free_mgmnt_irq(adapter); 3208 3209 ena_disable_msix(adapter); 3210 3211 ena_com_abort_admin_commands(ena_dev); 3212 3213 ena_com_wait_for_abort_completion(ena_dev); 3214 3215 ena_com_admin_destroy(ena_dev); 3216 3217 ena_com_mmio_reg_read_request_destroy(ena_dev); 3218 3219 /* return reset reason to default value */ 3220 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3221 3222 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3223 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3224 } 3225 3226 static int ena_restore_device(struct ena_adapter *adapter) 3227 { 3228 struct ena_com_dev_get_features_ctx get_feat_ctx; 3229 struct ena_com_dev *ena_dev = adapter->ena_dev; 3230 struct pci_dev *pdev = adapter->pdev; 3231 struct ena_ring *txr; 3232 int rc, count, i; 3233 bool wd_state; 3234 3235 set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags); 3236 rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx, &wd_state); 3237 if (rc) { 3238 dev_err(&pdev->dev, "Can not initialize device\n"); 3239 goto err; 3240 } 3241 adapter->wd_state = wd_state; 3242 3243 count = adapter->xdp_num_queues + adapter->num_io_queues; 3244 for (i = 0 ; i < count; i++) { 3245 txr = &adapter->tx_ring[i]; 3246 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type; 3247 txr->tx_max_header_size = ena_dev->tx_max_header_size; 3248 } 3249 3250 rc = ena_device_validate_params(adapter, &get_feat_ctx); 3251 if (rc) { 3252 dev_err(&pdev->dev, "Validation of device parameters failed\n"); 3253 goto err_device_destroy; 3254 } 3255 3256 rc = ena_enable_msix_and_set_admin_interrupts(adapter); 3257 if (rc) { 3258 dev_err(&pdev->dev, "Enable MSI-X failed\n"); 3259 goto err_device_destroy; 3260 } 3261 /* If the interface was up before the reset bring it up */ 3262 if (adapter->dev_up_before_reset) { 3263 rc = ena_up(adapter); 3264 if (rc) { 3265 dev_err(&pdev->dev, "Failed to create I/O queues\n"); 3266 goto err_disable_msix; 3267 } 3268 } 3269 3270 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3271 3272 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags); 3273 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags)) 3274 netif_carrier_on(adapter->netdev); 3275 3276 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ)); 3277 adapter->last_keep_alive_jiffies = jiffies; 3278 3279 return rc; 3280 err_disable_msix: 3281 ena_free_mgmnt_irq(adapter); 3282 ena_disable_msix(adapter); 3283 err_device_destroy: 3284 ena_com_abort_admin_commands(ena_dev); 3285 ena_com_wait_for_abort_completion(ena_dev); 3286 ena_com_admin_destroy(ena_dev); 3287 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE); 3288 ena_com_mmio_reg_read_request_destroy(ena_dev); 3289 err: 3290 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3291 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags); 3292 dev_err(&pdev->dev, 3293 "Reset attempt failed. Can not reset the device\n"); 3294 3295 return rc; 3296 } 3297 3298 static void ena_fw_reset_device(struct work_struct *work) 3299 { 3300 struct ena_adapter *adapter = 3301 container_of(work, struct ena_adapter, reset_task); 3302 3303 rtnl_lock(); 3304 3305 if (likely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 3306 ena_destroy_device(adapter, false); 3307 ena_restore_device(adapter); 3308 3309 dev_err(&adapter->pdev->dev, "Device reset completed successfully\n"); 3310 } 3311 3312 rtnl_unlock(); 3313 } 3314 3315 static int check_for_rx_interrupt_queue(struct ena_adapter *adapter, 3316 struct ena_ring *rx_ring) 3317 { 3318 struct ena_napi *ena_napi = container_of(rx_ring->napi, struct ena_napi, napi); 3319 3320 if (likely(READ_ONCE(ena_napi->first_interrupt))) 3321 return 0; 3322 3323 if (ena_com_cq_empty(rx_ring->ena_com_io_cq)) 3324 return 0; 3325 3326 rx_ring->no_interrupt_event_cnt++; 3327 3328 if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) { 3329 netif_err(adapter, rx_err, adapter->netdev, 3330 "Potential MSIX issue on Rx side Queue = %d. Reset the device\n", 3331 rx_ring->qid); 3332 3333 ena_reset_device(adapter, ENA_REGS_RESET_MISS_INTERRUPT); 3334 return -EIO; 3335 } 3336 3337 return 0; 3338 } 3339 3340 static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter, 3341 struct ena_ring *tx_ring) 3342 { 3343 struct ena_napi *ena_napi = container_of(tx_ring->napi, struct ena_napi, napi); 3344 unsigned int time_since_last_napi; 3345 unsigned int missing_tx_comp_to; 3346 bool is_tx_comp_time_expired; 3347 struct ena_tx_buffer *tx_buf; 3348 unsigned long last_jiffies; 3349 u32 missed_tx = 0; 3350 int i, rc = 0; 3351 3352 for (i = 0; i < tx_ring->ring_size; i++) { 3353 tx_buf = &tx_ring->tx_buffer_info[i]; 3354 last_jiffies = tx_buf->last_jiffies; 3355 3356 if (last_jiffies == 0) 3357 /* no pending Tx at this location */ 3358 continue; 3359 3360 is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies + 3361 2 * adapter->missing_tx_completion_to); 3362 3363 if (unlikely(!READ_ONCE(ena_napi->first_interrupt) && is_tx_comp_time_expired)) { 3364 /* If after graceful period interrupt is still not 3365 * received, we schedule a reset 3366 */ 3367 netif_err(adapter, tx_err, adapter->netdev, 3368 "Potential MSIX issue on Tx side Queue = %d. Reset the device\n", 3369 tx_ring->qid); 3370 ena_reset_device(adapter, ENA_REGS_RESET_MISS_INTERRUPT); 3371 return -EIO; 3372 } 3373 3374 is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies + 3375 adapter->missing_tx_completion_to); 3376 3377 if (unlikely(is_tx_comp_time_expired)) { 3378 if (!tx_buf->print_once) { 3379 time_since_last_napi = jiffies_to_usecs(jiffies - tx_ring->tx_stats.last_napi_jiffies); 3380 missing_tx_comp_to = jiffies_to_msecs(adapter->missing_tx_completion_to); 3381 netif_notice(adapter, tx_err, adapter->netdev, 3382 "Found a Tx that wasn't completed on time, qid %d, index %d. %u usecs have passed since last napi execution. Missing Tx timeout value %u msecs\n", 3383 tx_ring->qid, i, time_since_last_napi, missing_tx_comp_to); 3384 } 3385 3386 tx_buf->print_once = 1; 3387 missed_tx++; 3388 } 3389 } 3390 3391 if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) { 3392 netif_err(adapter, tx_err, adapter->netdev, 3393 "The number of lost tx completions is above the threshold (%d > %d). Reset the device\n", 3394 missed_tx, 3395 adapter->missing_tx_completion_threshold); 3396 ena_reset_device(adapter, ENA_REGS_RESET_MISS_TX_CMPL); 3397 rc = -EIO; 3398 } 3399 3400 ena_increase_stat(&tx_ring->tx_stats.missed_tx, missed_tx, 3401 &tx_ring->syncp); 3402 3403 return rc; 3404 } 3405 3406 static void check_for_missing_completions(struct ena_adapter *adapter) 3407 { 3408 struct ena_ring *tx_ring; 3409 struct ena_ring *rx_ring; 3410 int qid, budget, rc; 3411 int io_queue_count; 3412 3413 io_queue_count = adapter->xdp_num_queues + adapter->num_io_queues; 3414 3415 /* Make sure the driver doesn't turn the device in other process */ 3416 smp_rmb(); 3417 3418 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3419 return; 3420 3421 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) 3422 return; 3423 3424 if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT) 3425 return; 3426 3427 budget = min_t(u32, io_queue_count, ENA_MONITORED_TX_QUEUES); 3428 3429 qid = adapter->last_monitored_tx_qid; 3430 3431 while (budget) { 3432 qid = (qid + 1) % io_queue_count; 3433 3434 tx_ring = &adapter->tx_ring[qid]; 3435 rx_ring = &adapter->rx_ring[qid]; 3436 3437 rc = check_missing_comp_in_tx_queue(adapter, tx_ring); 3438 if (unlikely(rc)) 3439 return; 3440 3441 rc = !ENA_IS_XDP_INDEX(adapter, qid) ? 3442 check_for_rx_interrupt_queue(adapter, rx_ring) : 0; 3443 if (unlikely(rc)) 3444 return; 3445 3446 budget--; 3447 } 3448 3449 adapter->last_monitored_tx_qid = qid; 3450 } 3451 3452 /* trigger napi schedule after 2 consecutive detections */ 3453 #define EMPTY_RX_REFILL 2 3454 /* For the rare case where the device runs out of Rx descriptors and the 3455 * napi handler failed to refill new Rx descriptors (due to a lack of memory 3456 * for example). 3457 * This case will lead to a deadlock: 3458 * The device won't send interrupts since all the new Rx packets will be dropped 3459 * The napi handler won't allocate new Rx descriptors so the device will be 3460 * able to send new packets. 3461 * 3462 * This scenario can happen when the kernel's vm.min_free_kbytes is too small. 3463 * It is recommended to have at least 512MB, with a minimum of 128MB for 3464 * constrained environment). 3465 * 3466 * When such a situation is detected - Reschedule napi 3467 */ 3468 static void check_for_empty_rx_ring(struct ena_adapter *adapter) 3469 { 3470 struct ena_ring *rx_ring; 3471 int i, refill_required; 3472 3473 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3474 return; 3475 3476 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) 3477 return; 3478 3479 for (i = 0; i < adapter->num_io_queues; i++) { 3480 rx_ring = &adapter->rx_ring[i]; 3481 3482 refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq); 3483 if (unlikely(refill_required == (rx_ring->ring_size - 1))) { 3484 rx_ring->empty_rx_queue++; 3485 3486 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) { 3487 ena_increase_stat(&rx_ring->rx_stats.empty_rx_ring, 1, 3488 &rx_ring->syncp); 3489 3490 netif_err(adapter, drv, adapter->netdev, 3491 "Trigger refill for ring %d\n", i); 3492 3493 napi_schedule(rx_ring->napi); 3494 rx_ring->empty_rx_queue = 0; 3495 } 3496 } else { 3497 rx_ring->empty_rx_queue = 0; 3498 } 3499 } 3500 } 3501 3502 /* Check for keep alive expiration */ 3503 static void check_for_missing_keep_alive(struct ena_adapter *adapter) 3504 { 3505 unsigned long keep_alive_expired; 3506 3507 if (!adapter->wd_state) 3508 return; 3509 3510 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3511 return; 3512 3513 keep_alive_expired = adapter->last_keep_alive_jiffies + 3514 adapter->keep_alive_timeout; 3515 if (unlikely(time_is_before_jiffies(keep_alive_expired))) { 3516 netif_err(adapter, drv, adapter->netdev, 3517 "Keep alive watchdog timeout.\n"); 3518 ena_increase_stat(&adapter->dev_stats.wd_expired, 1, 3519 &adapter->syncp); 3520 ena_reset_device(adapter, ENA_REGS_RESET_KEEP_ALIVE_TO); 3521 } 3522 } 3523 3524 static void check_for_admin_com_state(struct ena_adapter *adapter) 3525 { 3526 if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) { 3527 netif_err(adapter, drv, adapter->netdev, 3528 "ENA admin queue is not in running state!\n"); 3529 ena_increase_stat(&adapter->dev_stats.admin_q_pause, 1, 3530 &adapter->syncp); 3531 ena_reset_device(adapter, ENA_REGS_RESET_ADMIN_TO); 3532 } 3533 } 3534 3535 static void ena_update_hints(struct ena_adapter *adapter, 3536 struct ena_admin_ena_hw_hints *hints) 3537 { 3538 struct net_device *netdev = adapter->netdev; 3539 3540 if (hints->admin_completion_tx_timeout) 3541 adapter->ena_dev->admin_queue.completion_timeout = 3542 hints->admin_completion_tx_timeout * 1000; 3543 3544 if (hints->mmio_read_timeout) 3545 /* convert to usec */ 3546 adapter->ena_dev->mmio_read.reg_read_to = 3547 hints->mmio_read_timeout * 1000; 3548 3549 if (hints->missed_tx_completion_count_threshold_to_reset) 3550 adapter->missing_tx_completion_threshold = 3551 hints->missed_tx_completion_count_threshold_to_reset; 3552 3553 if (hints->missing_tx_completion_timeout) { 3554 if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3555 adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT; 3556 else 3557 adapter->missing_tx_completion_to = 3558 msecs_to_jiffies(hints->missing_tx_completion_timeout); 3559 } 3560 3561 if (hints->netdev_wd_timeout) 3562 netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout); 3563 3564 if (hints->driver_watchdog_timeout) { 3565 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3566 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT; 3567 else 3568 adapter->keep_alive_timeout = 3569 msecs_to_jiffies(hints->driver_watchdog_timeout); 3570 } 3571 } 3572 3573 static void ena_update_host_info(struct ena_admin_host_info *host_info, 3574 struct net_device *netdev) 3575 { 3576 host_info->supported_network_features[0] = 3577 netdev->features & GENMASK_ULL(31, 0); 3578 host_info->supported_network_features[1] = 3579 (netdev->features & GENMASK_ULL(63, 32)) >> 32; 3580 } 3581 3582 static void ena_timer_service(struct timer_list *t) 3583 { 3584 struct ena_adapter *adapter = from_timer(adapter, t, timer_service); 3585 u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr; 3586 struct ena_admin_host_info *host_info = 3587 adapter->ena_dev->host_attr.host_info; 3588 3589 check_for_missing_keep_alive(adapter); 3590 3591 check_for_admin_com_state(adapter); 3592 3593 check_for_missing_completions(adapter); 3594 3595 check_for_empty_rx_ring(adapter); 3596 3597 if (debug_area) 3598 ena_dump_stats_to_buf(adapter, debug_area); 3599 3600 if (host_info) 3601 ena_update_host_info(host_info, adapter->netdev); 3602 3603 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 3604 netif_err(adapter, drv, adapter->netdev, 3605 "Trigger reset is on\n"); 3606 ena_dump_stats_to_dmesg(adapter); 3607 queue_work(ena_wq, &adapter->reset_task); 3608 return; 3609 } 3610 3611 /* Reset the timer */ 3612 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ)); 3613 } 3614 3615 static u32 ena_calc_max_io_queue_num(struct pci_dev *pdev, 3616 struct ena_com_dev *ena_dev, 3617 struct ena_com_dev_get_features_ctx *get_feat_ctx) 3618 { 3619 u32 io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues; 3620 3621 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) { 3622 struct ena_admin_queue_ext_feature_fields *max_queue_ext = 3623 &get_feat_ctx->max_queue_ext.max_queue_ext; 3624 io_rx_num = min_t(u32, max_queue_ext->max_rx_sq_num, 3625 max_queue_ext->max_rx_cq_num); 3626 3627 io_tx_sq_num = max_queue_ext->max_tx_sq_num; 3628 io_tx_cq_num = max_queue_ext->max_tx_cq_num; 3629 } else { 3630 struct ena_admin_queue_feature_desc *max_queues = 3631 &get_feat_ctx->max_queues; 3632 io_tx_sq_num = max_queues->max_sq_num; 3633 io_tx_cq_num = max_queues->max_cq_num; 3634 io_rx_num = min_t(u32, io_tx_sq_num, io_tx_cq_num); 3635 } 3636 3637 /* In case of LLQ use the llq fields for the tx SQ/CQ */ 3638 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 3639 io_tx_sq_num = get_feat_ctx->llq.max_llq_num; 3640 3641 max_num_io_queues = min_t(u32, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES); 3642 max_num_io_queues = min_t(u32, max_num_io_queues, io_rx_num); 3643 max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_sq_num); 3644 max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_cq_num); 3645 /* 1 IRQ for mgmnt and 1 IRQs for each IO direction */ 3646 max_num_io_queues = min_t(u32, max_num_io_queues, pci_msix_vec_count(pdev) - 1); 3647 3648 return max_num_io_queues; 3649 } 3650 3651 static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat, 3652 struct net_device *netdev) 3653 { 3654 netdev_features_t dev_features = 0; 3655 3656 /* Set offload features */ 3657 if (feat->offload.tx & 3658 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK) 3659 dev_features |= NETIF_F_IP_CSUM; 3660 3661 if (feat->offload.tx & 3662 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK) 3663 dev_features |= NETIF_F_IPV6_CSUM; 3664 3665 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) 3666 dev_features |= NETIF_F_TSO; 3667 3668 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) 3669 dev_features |= NETIF_F_TSO6; 3670 3671 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK) 3672 dev_features |= NETIF_F_TSO_ECN; 3673 3674 if (feat->offload.rx_supported & 3675 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK) 3676 dev_features |= NETIF_F_RXCSUM; 3677 3678 if (feat->offload.rx_supported & 3679 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) 3680 dev_features |= NETIF_F_RXCSUM; 3681 3682 netdev->features = 3683 dev_features | 3684 NETIF_F_SG | 3685 NETIF_F_RXHASH | 3686 NETIF_F_HIGHDMA; 3687 3688 netdev->hw_features |= netdev->features; 3689 netdev->vlan_features |= netdev->features; 3690 } 3691 3692 static void ena_set_conf_feat_params(struct ena_adapter *adapter, 3693 struct ena_com_dev_get_features_ctx *feat) 3694 { 3695 struct net_device *netdev = adapter->netdev; 3696 3697 /* Copy mac address */ 3698 if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) { 3699 eth_hw_addr_random(netdev); 3700 ether_addr_copy(adapter->mac_addr, netdev->dev_addr); 3701 } else { 3702 ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr); 3703 eth_hw_addr_set(netdev, adapter->mac_addr); 3704 } 3705 3706 /* Set offload features */ 3707 ena_set_dev_offloads(feat, netdev); 3708 3709 adapter->max_mtu = feat->dev_attr.max_mtu; 3710 netdev->max_mtu = adapter->max_mtu; 3711 netdev->min_mtu = ENA_MIN_MTU; 3712 } 3713 3714 static int ena_rss_init_default(struct ena_adapter *adapter) 3715 { 3716 struct ena_com_dev *ena_dev = adapter->ena_dev; 3717 struct device *dev = &adapter->pdev->dev; 3718 int rc, i; 3719 u32 val; 3720 3721 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE); 3722 if (unlikely(rc)) { 3723 dev_err(dev, "Cannot init indirect table\n"); 3724 goto err_rss_init; 3725 } 3726 3727 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) { 3728 val = ethtool_rxfh_indir_default(i, adapter->num_io_queues); 3729 rc = ena_com_indirect_table_fill_entry(ena_dev, i, 3730 ENA_IO_RXQ_IDX(val)); 3731 if (unlikely(rc)) { 3732 dev_err(dev, "Cannot fill indirect table\n"); 3733 goto err_fill_indir; 3734 } 3735 } 3736 3737 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_TOEPLITZ, NULL, 3738 ENA_HASH_KEY_SIZE, 0xFFFFFFFF); 3739 if (unlikely(rc && (rc != -EOPNOTSUPP))) { 3740 dev_err(dev, "Cannot fill hash function\n"); 3741 goto err_fill_indir; 3742 } 3743 3744 rc = ena_com_set_default_hash_ctrl(ena_dev); 3745 if (unlikely(rc && (rc != -EOPNOTSUPP))) { 3746 dev_err(dev, "Cannot fill hash control\n"); 3747 goto err_fill_indir; 3748 } 3749 3750 return 0; 3751 3752 err_fill_indir: 3753 ena_com_rss_destroy(ena_dev); 3754 err_rss_init: 3755 3756 return rc; 3757 } 3758 3759 static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev) 3760 { 3761 int release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK; 3762 3763 pci_release_selected_regions(pdev, release_bars); 3764 } 3765 3766 /* ena_probe - Device Initialization Routine 3767 * @pdev: PCI device information struct 3768 * @ent: entry in ena_pci_tbl 3769 * 3770 * Returns 0 on success, negative on failure 3771 * 3772 * ena_probe initializes an adapter identified by a pci_dev structure. 3773 * The OS initialization, configuring of the adapter private structure, 3774 * and a hardware reset occur. 3775 */ 3776 static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 3777 { 3778 struct ena_com_dev_get_features_ctx get_feat_ctx; 3779 struct ena_com_dev *ena_dev = NULL; 3780 struct ena_adapter *adapter; 3781 struct net_device *netdev; 3782 static int adapters_found; 3783 u32 max_num_io_queues; 3784 bool wd_state; 3785 int bars, rc; 3786 3787 dev_dbg(&pdev->dev, "%s\n", __func__); 3788 3789 rc = pci_enable_device_mem(pdev); 3790 if (rc) { 3791 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n"); 3792 return rc; 3793 } 3794 3795 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(ENA_MAX_PHYS_ADDR_SIZE_BITS)); 3796 if (rc) { 3797 dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", rc); 3798 goto err_disable_device; 3799 } 3800 3801 pci_set_master(pdev); 3802 3803 ena_dev = vzalloc(sizeof(*ena_dev)); 3804 if (!ena_dev) { 3805 rc = -ENOMEM; 3806 goto err_disable_device; 3807 } 3808 3809 bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK; 3810 rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME); 3811 if (rc) { 3812 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n", 3813 rc); 3814 goto err_free_ena_dev; 3815 } 3816 3817 ena_dev->reg_bar = devm_ioremap(&pdev->dev, 3818 pci_resource_start(pdev, ENA_REG_BAR), 3819 pci_resource_len(pdev, ENA_REG_BAR)); 3820 if (!ena_dev->reg_bar) { 3821 dev_err(&pdev->dev, "Failed to remap regs bar\n"); 3822 rc = -EFAULT; 3823 goto err_free_region; 3824 } 3825 3826 ena_dev->ena_min_poll_delay_us = ENA_ADMIN_POLL_DELAY_US; 3827 3828 ena_dev->dmadev = &pdev->dev; 3829 3830 netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), ENA_MAX_RINGS); 3831 if (!netdev) { 3832 dev_err(&pdev->dev, "alloc_etherdev_mq failed\n"); 3833 rc = -ENOMEM; 3834 goto err_free_region; 3835 } 3836 3837 SET_NETDEV_DEV(netdev, &pdev->dev); 3838 adapter = netdev_priv(netdev); 3839 adapter->ena_dev = ena_dev; 3840 adapter->netdev = netdev; 3841 adapter->pdev = pdev; 3842 adapter->msg_enable = DEFAULT_MSG_ENABLE; 3843 3844 ena_dev->net_device = netdev; 3845 3846 pci_set_drvdata(pdev, adapter); 3847 3848 rc = ena_map_llq_mem_bar(pdev, ena_dev, bars); 3849 if (rc) { 3850 dev_err(&pdev->dev, "ENA LLQ bar mapping failed\n"); 3851 goto err_netdev_destroy; 3852 } 3853 3854 rc = ena_device_init(adapter, pdev, &get_feat_ctx, &wd_state); 3855 if (rc) { 3856 dev_err(&pdev->dev, "ENA device init failed\n"); 3857 if (rc == -ETIME) 3858 rc = -EPROBE_DEFER; 3859 goto err_netdev_destroy; 3860 } 3861 3862 /* Initial TX and RX interrupt delay. Assumes 1 usec granularity. 3863 * Updated during device initialization with the real granularity 3864 */ 3865 ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS; 3866 ena_dev->intr_moder_rx_interval = ENA_INTR_INITIAL_RX_INTERVAL_USECS; 3867 ena_dev->intr_delay_resolution = ENA_DEFAULT_INTR_DELAY_RESOLUTION; 3868 max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev, &get_feat_ctx); 3869 if (unlikely(!max_num_io_queues)) { 3870 rc = -EFAULT; 3871 goto err_device_destroy; 3872 } 3873 3874 ena_set_conf_feat_params(adapter, &get_feat_ctx); 3875 3876 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3877 3878 adapter->num_io_queues = max_num_io_queues; 3879 adapter->max_num_io_queues = max_num_io_queues; 3880 adapter->last_monitored_tx_qid = 0; 3881 3882 adapter->xdp_first_ring = 0; 3883 adapter->xdp_num_queues = 0; 3884 3885 adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK; 3886 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 3887 adapter->disable_meta_caching = 3888 !!(get_feat_ctx.llq.accel_mode.u.get.supported_flags & 3889 BIT(ENA_ADMIN_DISABLE_META_CACHING)); 3890 3891 adapter->wd_state = wd_state; 3892 3893 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found); 3894 3895 rc = ena_com_init_interrupt_moderation(adapter->ena_dev); 3896 if (rc) { 3897 dev_err(&pdev->dev, 3898 "Failed to query interrupt moderation feature\n"); 3899 goto err_device_destroy; 3900 } 3901 3902 ena_init_io_rings(adapter, 3903 0, 3904 adapter->xdp_num_queues + 3905 adapter->num_io_queues); 3906 3907 netdev->netdev_ops = &ena_netdev_ops; 3908 netdev->watchdog_timeo = TX_TIMEOUT; 3909 ena_set_ethtool_ops(netdev); 3910 3911 netdev->priv_flags |= IFF_UNICAST_FLT; 3912 3913 u64_stats_init(&adapter->syncp); 3914 3915 rc = ena_enable_msix_and_set_admin_interrupts(adapter); 3916 if (rc) { 3917 dev_err(&pdev->dev, 3918 "Failed to enable and set the admin interrupts\n"); 3919 goto err_worker_destroy; 3920 } 3921 rc = ena_rss_init_default(adapter); 3922 if (rc && (rc != -EOPNOTSUPP)) { 3923 dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc); 3924 goto err_free_msix; 3925 } 3926 3927 ena_config_debug_area(adapter); 3928 3929 if (ena_xdp_legal_queue_count(adapter, adapter->num_io_queues)) 3930 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | 3931 NETDEV_XDP_ACT_REDIRECT; 3932 3933 memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len); 3934 3935 netif_carrier_off(netdev); 3936 3937 rc = register_netdev(netdev); 3938 if (rc) { 3939 dev_err(&pdev->dev, "Cannot register net device\n"); 3940 goto err_rss; 3941 } 3942 3943 INIT_WORK(&adapter->reset_task, ena_fw_reset_device); 3944 3945 adapter->last_keep_alive_jiffies = jiffies; 3946 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT; 3947 adapter->missing_tx_completion_to = TX_TIMEOUT; 3948 adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS; 3949 3950 ena_update_hints(adapter, &get_feat_ctx.hw_hints); 3951 3952 timer_setup(&adapter->timer_service, ena_timer_service, 0); 3953 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ)); 3954 3955 dev_info(&pdev->dev, 3956 "%s found at mem %lx, mac addr %pM\n", 3957 DEVICE_NAME, (long)pci_resource_start(pdev, 0), 3958 netdev->dev_addr); 3959 3960 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3961 3962 adapters_found++; 3963 3964 return 0; 3965 3966 err_rss: 3967 ena_com_delete_debug_area(ena_dev); 3968 ena_com_rss_destroy(ena_dev); 3969 err_free_msix: 3970 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR); 3971 /* stop submitting admin commands on a device that was reset */ 3972 ena_com_set_admin_running_state(ena_dev, false); 3973 ena_free_mgmnt_irq(adapter); 3974 ena_disable_msix(adapter); 3975 err_worker_destroy: 3976 del_timer(&adapter->timer_service); 3977 err_device_destroy: 3978 ena_com_delete_host_info(ena_dev); 3979 ena_com_admin_destroy(ena_dev); 3980 err_netdev_destroy: 3981 free_netdev(netdev); 3982 err_free_region: 3983 ena_release_bars(ena_dev, pdev); 3984 err_free_ena_dev: 3985 vfree(ena_dev); 3986 err_disable_device: 3987 pci_disable_device(pdev); 3988 return rc; 3989 } 3990 3991 /*****************************************************************************/ 3992 3993 /* __ena_shutoff - Helper used in both PCI remove/shutdown routines 3994 * @pdev: PCI device information struct 3995 * @shutdown: Is it a shutdown operation? If false, means it is a removal 3996 * 3997 * __ena_shutoff is a helper routine that does the real work on shutdown and 3998 * removal paths; the difference between those paths is with regards to whether 3999 * dettach or unregister the netdevice. 4000 */ 4001 static void __ena_shutoff(struct pci_dev *pdev, bool shutdown) 4002 { 4003 struct ena_adapter *adapter = pci_get_drvdata(pdev); 4004 struct ena_com_dev *ena_dev; 4005 struct net_device *netdev; 4006 4007 ena_dev = adapter->ena_dev; 4008 netdev = adapter->netdev; 4009 4010 #ifdef CONFIG_RFS_ACCEL 4011 if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) { 4012 free_irq_cpu_rmap(netdev->rx_cpu_rmap); 4013 netdev->rx_cpu_rmap = NULL; 4014 } 4015 #endif /* CONFIG_RFS_ACCEL */ 4016 4017 /* Make sure timer and reset routine won't be called after 4018 * freeing device resources. 4019 */ 4020 del_timer_sync(&adapter->timer_service); 4021 cancel_work_sync(&adapter->reset_task); 4022 4023 rtnl_lock(); /* lock released inside the below if-else block */ 4024 adapter->reset_reason = ENA_REGS_RESET_SHUTDOWN; 4025 ena_destroy_device(adapter, true); 4026 4027 if (shutdown) { 4028 netif_device_detach(netdev); 4029 dev_close(netdev); 4030 rtnl_unlock(); 4031 } else { 4032 rtnl_unlock(); 4033 unregister_netdev(netdev); 4034 free_netdev(netdev); 4035 } 4036 4037 ena_com_rss_destroy(ena_dev); 4038 4039 ena_com_delete_debug_area(ena_dev); 4040 4041 ena_com_delete_host_info(ena_dev); 4042 4043 ena_release_bars(ena_dev, pdev); 4044 4045 pci_disable_device(pdev); 4046 4047 vfree(ena_dev); 4048 } 4049 4050 /* ena_remove - Device Removal Routine 4051 * @pdev: PCI device information struct 4052 * 4053 * ena_remove is called by the PCI subsystem to alert the driver 4054 * that it should release a PCI device. 4055 */ 4056 4057 static void ena_remove(struct pci_dev *pdev) 4058 { 4059 __ena_shutoff(pdev, false); 4060 } 4061 4062 /* ena_shutdown - Device Shutdown Routine 4063 * @pdev: PCI device information struct 4064 * 4065 * ena_shutdown is called by the PCI subsystem to alert the driver that 4066 * a shutdown/reboot (or kexec) is happening and device must be disabled. 4067 */ 4068 4069 static void ena_shutdown(struct pci_dev *pdev) 4070 { 4071 __ena_shutoff(pdev, true); 4072 } 4073 4074 /* ena_suspend - PM suspend callback 4075 * @dev_d: Device information struct 4076 */ 4077 static int __maybe_unused ena_suspend(struct device *dev_d) 4078 { 4079 struct pci_dev *pdev = to_pci_dev(dev_d); 4080 struct ena_adapter *adapter = pci_get_drvdata(pdev); 4081 4082 ena_increase_stat(&adapter->dev_stats.suspend, 1, &adapter->syncp); 4083 4084 rtnl_lock(); 4085 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 4086 dev_err(&pdev->dev, 4087 "Ignoring device reset request as the device is being suspended\n"); 4088 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 4089 } 4090 ena_destroy_device(adapter, true); 4091 rtnl_unlock(); 4092 return 0; 4093 } 4094 4095 /* ena_resume - PM resume callback 4096 * @dev_d: Device information struct 4097 */ 4098 static int __maybe_unused ena_resume(struct device *dev_d) 4099 { 4100 struct ena_adapter *adapter = dev_get_drvdata(dev_d); 4101 int rc; 4102 4103 ena_increase_stat(&adapter->dev_stats.resume, 1, &adapter->syncp); 4104 4105 rtnl_lock(); 4106 rc = ena_restore_device(adapter); 4107 rtnl_unlock(); 4108 return rc; 4109 } 4110 4111 static SIMPLE_DEV_PM_OPS(ena_pm_ops, ena_suspend, ena_resume); 4112 4113 static struct pci_driver ena_pci_driver = { 4114 .name = DRV_MODULE_NAME, 4115 .id_table = ena_pci_tbl, 4116 .probe = ena_probe, 4117 .remove = ena_remove, 4118 .shutdown = ena_shutdown, 4119 .driver.pm = &ena_pm_ops, 4120 .sriov_configure = pci_sriov_configure_simple, 4121 }; 4122 4123 static int __init ena_init(void) 4124 { 4125 int ret; 4126 4127 ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME); 4128 if (!ena_wq) { 4129 pr_err("Failed to create workqueue\n"); 4130 return -ENOMEM; 4131 } 4132 4133 ret = pci_register_driver(&ena_pci_driver); 4134 if (ret) 4135 destroy_workqueue(ena_wq); 4136 4137 return ret; 4138 } 4139 4140 static void __exit ena_cleanup(void) 4141 { 4142 pci_unregister_driver(&ena_pci_driver); 4143 4144 if (ena_wq) { 4145 destroy_workqueue(ena_wq); 4146 ena_wq = NULL; 4147 } 4148 } 4149 4150 /****************************************************************************** 4151 ******************************** AENQ Handlers ******************************* 4152 *****************************************************************************/ 4153 /* ena_update_on_link_change: 4154 * Notify the network interface about the change in link status 4155 */ 4156 static void ena_update_on_link_change(void *adapter_data, 4157 struct ena_admin_aenq_entry *aenq_e) 4158 { 4159 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4160 struct ena_admin_aenq_link_change_desc *aenq_desc = 4161 (struct ena_admin_aenq_link_change_desc *)aenq_e; 4162 int status = aenq_desc->flags & 4163 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK; 4164 4165 if (status) { 4166 netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__); 4167 set_bit(ENA_FLAG_LINK_UP, &adapter->flags); 4168 if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags)) 4169 netif_carrier_on(adapter->netdev); 4170 } else { 4171 clear_bit(ENA_FLAG_LINK_UP, &adapter->flags); 4172 netif_carrier_off(adapter->netdev); 4173 } 4174 } 4175 4176 static void ena_keep_alive_wd(void *adapter_data, 4177 struct ena_admin_aenq_entry *aenq_e) 4178 { 4179 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4180 struct ena_admin_aenq_keep_alive_desc *desc; 4181 u64 rx_drops; 4182 u64 tx_drops; 4183 4184 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e; 4185 adapter->last_keep_alive_jiffies = jiffies; 4186 4187 rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low; 4188 tx_drops = ((u64)desc->tx_drops_high << 32) | desc->tx_drops_low; 4189 4190 u64_stats_update_begin(&adapter->syncp); 4191 /* These stats are accumulated by the device, so the counters indicate 4192 * all drops since last reset. 4193 */ 4194 adapter->dev_stats.rx_drops = rx_drops; 4195 adapter->dev_stats.tx_drops = tx_drops; 4196 u64_stats_update_end(&adapter->syncp); 4197 } 4198 4199 static void ena_notification(void *adapter_data, 4200 struct ena_admin_aenq_entry *aenq_e) 4201 { 4202 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4203 struct ena_admin_ena_hw_hints *hints; 4204 4205 WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION, 4206 "Invalid group(%x) expected %x\n", 4207 aenq_e->aenq_common_desc.group, 4208 ENA_ADMIN_NOTIFICATION); 4209 4210 switch (aenq_e->aenq_common_desc.syndrome) { 4211 case ENA_ADMIN_UPDATE_HINTS: 4212 hints = (struct ena_admin_ena_hw_hints *) 4213 (&aenq_e->inline_data_w4); 4214 ena_update_hints(adapter, hints); 4215 break; 4216 default: 4217 netif_err(adapter, drv, adapter->netdev, 4218 "Invalid aenq notification link state %d\n", 4219 aenq_e->aenq_common_desc.syndrome); 4220 } 4221 } 4222 4223 /* This handler will called for unknown event group or unimplemented handlers*/ 4224 static void unimplemented_aenq_handler(void *data, 4225 struct ena_admin_aenq_entry *aenq_e) 4226 { 4227 struct ena_adapter *adapter = (struct ena_adapter *)data; 4228 4229 netif_err(adapter, drv, adapter->netdev, 4230 "Unknown event was received or event with unimplemented handler\n"); 4231 } 4232 4233 static struct ena_aenq_handlers aenq_handlers = { 4234 .handlers = { 4235 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change, 4236 [ENA_ADMIN_NOTIFICATION] = ena_notification, 4237 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd, 4238 }, 4239 .unimplemented_handler = unimplemented_aenq_handler 4240 }; 4241 4242 module_init(ena_init); 4243 module_exit(ena_cleanup); 4244