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