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