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