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 = 3094 (DRV_MODULE_GEN_MAJOR) | 3095 (DRV_MODULE_GEN_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) | 3096 (DRV_MODULE_GEN_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT) | 3097 ("K"[0] << ENA_ADMIN_HOST_INFO_MODULE_TYPE_SHIFT); 3098 host_info->num_cpus = num_online_cpus(); 3099 3100 host_info->driver_supported_features = 3101 ENA_ADMIN_HOST_INFO_INTERRUPT_MODERATION_MASK; 3102 3103 rc = ena_com_set_host_attributes(ena_dev); 3104 if (rc) { 3105 if (rc == -EOPNOTSUPP) 3106 pr_warn("Cannot set host attributes\n"); 3107 else 3108 pr_err("Cannot set host attributes\n"); 3109 3110 goto err; 3111 } 3112 3113 return; 3114 3115 err: 3116 ena_com_delete_host_info(ena_dev); 3117 } 3118 3119 static void ena_config_debug_area(struct ena_adapter *adapter) 3120 { 3121 u32 debug_area_size; 3122 int rc, ss_count; 3123 3124 ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS); 3125 if (ss_count <= 0) { 3126 netif_err(adapter, drv, adapter->netdev, 3127 "SS count is negative\n"); 3128 return; 3129 } 3130 3131 /* allocate 32 bytes for each string and 64bit for the value */ 3132 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count; 3133 3134 rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size); 3135 if (rc) { 3136 pr_err("Cannot allocate debug area\n"); 3137 return; 3138 } 3139 3140 rc = ena_com_set_host_attributes(adapter->ena_dev); 3141 if (rc) { 3142 if (rc == -EOPNOTSUPP) 3143 netif_warn(adapter, drv, adapter->netdev, 3144 "Cannot set host attributes\n"); 3145 else 3146 netif_err(adapter, drv, adapter->netdev, 3147 "Cannot set host attributes\n"); 3148 goto err; 3149 } 3150 3151 return; 3152 err: 3153 ena_com_delete_debug_area(adapter->ena_dev); 3154 } 3155 3156 static void ena_get_stats64(struct net_device *netdev, 3157 struct rtnl_link_stats64 *stats) 3158 { 3159 struct ena_adapter *adapter = netdev_priv(netdev); 3160 struct ena_ring *rx_ring, *tx_ring; 3161 unsigned int start; 3162 u64 rx_drops; 3163 int i; 3164 3165 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3166 return; 3167 3168 for (i = 0; i < adapter->num_io_queues; i++) { 3169 u64 bytes, packets; 3170 3171 tx_ring = &adapter->tx_ring[i]; 3172 3173 do { 3174 start = u64_stats_fetch_begin_irq(&tx_ring->syncp); 3175 packets = tx_ring->tx_stats.cnt; 3176 bytes = tx_ring->tx_stats.bytes; 3177 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start)); 3178 3179 stats->tx_packets += packets; 3180 stats->tx_bytes += bytes; 3181 3182 rx_ring = &adapter->rx_ring[i]; 3183 3184 do { 3185 start = u64_stats_fetch_begin_irq(&rx_ring->syncp); 3186 packets = rx_ring->rx_stats.cnt; 3187 bytes = rx_ring->rx_stats.bytes; 3188 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start)); 3189 3190 stats->rx_packets += packets; 3191 stats->rx_bytes += bytes; 3192 } 3193 3194 do { 3195 start = u64_stats_fetch_begin_irq(&adapter->syncp); 3196 rx_drops = adapter->dev_stats.rx_drops; 3197 } while (u64_stats_fetch_retry_irq(&adapter->syncp, start)); 3198 3199 stats->rx_dropped = rx_drops; 3200 3201 stats->multicast = 0; 3202 stats->collisions = 0; 3203 3204 stats->rx_length_errors = 0; 3205 stats->rx_crc_errors = 0; 3206 stats->rx_frame_errors = 0; 3207 stats->rx_fifo_errors = 0; 3208 stats->rx_missed_errors = 0; 3209 stats->tx_window_errors = 0; 3210 3211 stats->rx_errors = 0; 3212 stats->tx_errors = 0; 3213 } 3214 3215 static const struct net_device_ops ena_netdev_ops = { 3216 .ndo_open = ena_open, 3217 .ndo_stop = ena_close, 3218 .ndo_start_xmit = ena_start_xmit, 3219 .ndo_select_queue = ena_select_queue, 3220 .ndo_get_stats64 = ena_get_stats64, 3221 .ndo_tx_timeout = ena_tx_timeout, 3222 .ndo_change_mtu = ena_change_mtu, 3223 .ndo_set_mac_address = NULL, 3224 .ndo_validate_addr = eth_validate_addr, 3225 .ndo_bpf = ena_xdp, 3226 }; 3227 3228 static int ena_device_validate_params(struct ena_adapter *adapter, 3229 struct ena_com_dev_get_features_ctx *get_feat_ctx) 3230 { 3231 struct net_device *netdev = adapter->netdev; 3232 int rc; 3233 3234 rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr, 3235 adapter->mac_addr); 3236 if (!rc) { 3237 netif_err(adapter, drv, netdev, 3238 "Error, mac address are different\n"); 3239 return -EINVAL; 3240 } 3241 3242 if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) { 3243 netif_err(adapter, drv, netdev, 3244 "Error, device max mtu is smaller than netdev MTU\n"); 3245 return -EINVAL; 3246 } 3247 3248 return 0; 3249 } 3250 3251 static int ena_device_init(struct ena_com_dev *ena_dev, struct pci_dev *pdev, 3252 struct ena_com_dev_get_features_ctx *get_feat_ctx, 3253 bool *wd_state) 3254 { 3255 struct device *dev = &pdev->dev; 3256 bool readless_supported; 3257 u32 aenq_groups; 3258 int dma_width; 3259 int rc; 3260 3261 rc = ena_com_mmio_reg_read_request_init(ena_dev); 3262 if (rc) { 3263 dev_err(dev, "failed to init mmio read less\n"); 3264 return rc; 3265 } 3266 3267 /* The PCIe configuration space revision id indicate if mmio reg 3268 * read is disabled 3269 */ 3270 readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ); 3271 ena_com_set_mmio_read_mode(ena_dev, readless_supported); 3272 3273 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL); 3274 if (rc) { 3275 dev_err(dev, "Can not reset device\n"); 3276 goto err_mmio_read_less; 3277 } 3278 3279 rc = ena_com_validate_version(ena_dev); 3280 if (rc) { 3281 dev_err(dev, "device version is too low\n"); 3282 goto err_mmio_read_less; 3283 } 3284 3285 dma_width = ena_com_get_dma_width(ena_dev); 3286 if (dma_width < 0) { 3287 dev_err(dev, "Invalid dma width value %d", dma_width); 3288 rc = dma_width; 3289 goto err_mmio_read_less; 3290 } 3291 3292 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_width)); 3293 if (rc) { 3294 dev_err(dev, "pci_set_dma_mask failed 0x%x\n", rc); 3295 goto err_mmio_read_less; 3296 } 3297 3298 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(dma_width)); 3299 if (rc) { 3300 dev_err(dev, "err_pci_set_consistent_dma_mask failed 0x%x\n", 3301 rc); 3302 goto err_mmio_read_less; 3303 } 3304 3305 /* ENA admin level init */ 3306 rc = ena_com_admin_init(ena_dev, &aenq_handlers); 3307 if (rc) { 3308 dev_err(dev, 3309 "Can not initialize ena admin queue with device\n"); 3310 goto err_mmio_read_less; 3311 } 3312 3313 /* To enable the msix interrupts the driver needs to know the number 3314 * of queues. So the driver uses polling mode to retrieve this 3315 * information 3316 */ 3317 ena_com_set_admin_polling_mode(ena_dev, true); 3318 3319 ena_config_host_info(ena_dev, pdev); 3320 3321 /* Get Device Attributes*/ 3322 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx); 3323 if (rc) { 3324 dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc); 3325 goto err_admin_init; 3326 } 3327 3328 /* Try to turn all the available aenq groups */ 3329 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | 3330 BIT(ENA_ADMIN_FATAL_ERROR) | 3331 BIT(ENA_ADMIN_WARNING) | 3332 BIT(ENA_ADMIN_NOTIFICATION) | 3333 BIT(ENA_ADMIN_KEEP_ALIVE); 3334 3335 aenq_groups &= get_feat_ctx->aenq.supported_groups; 3336 3337 rc = ena_com_set_aenq_config(ena_dev, aenq_groups); 3338 if (rc) { 3339 dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc); 3340 goto err_admin_init; 3341 } 3342 3343 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE)); 3344 3345 return 0; 3346 3347 err_admin_init: 3348 ena_com_delete_host_info(ena_dev); 3349 ena_com_admin_destroy(ena_dev); 3350 err_mmio_read_less: 3351 ena_com_mmio_reg_read_request_destroy(ena_dev); 3352 3353 return rc; 3354 } 3355 3356 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter) 3357 { 3358 struct ena_com_dev *ena_dev = adapter->ena_dev; 3359 struct device *dev = &adapter->pdev->dev; 3360 int rc; 3361 3362 rc = ena_enable_msix(adapter); 3363 if (rc) { 3364 dev_err(dev, "Can not reserve msix vectors\n"); 3365 return rc; 3366 } 3367 3368 ena_setup_mgmnt_intr(adapter); 3369 3370 rc = ena_request_mgmnt_irq(adapter); 3371 if (rc) { 3372 dev_err(dev, "Can not setup management interrupts\n"); 3373 goto err_disable_msix; 3374 } 3375 3376 ena_com_set_admin_polling_mode(ena_dev, false); 3377 3378 ena_com_admin_aenq_enable(ena_dev); 3379 3380 return 0; 3381 3382 err_disable_msix: 3383 ena_disable_msix(adapter); 3384 3385 return rc; 3386 } 3387 3388 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful) 3389 { 3390 struct net_device *netdev = adapter->netdev; 3391 struct ena_com_dev *ena_dev = adapter->ena_dev; 3392 bool dev_up; 3393 3394 if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)) 3395 return; 3396 3397 netif_carrier_off(netdev); 3398 3399 del_timer_sync(&adapter->timer_service); 3400 3401 dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags); 3402 adapter->dev_up_before_reset = dev_up; 3403 if (!graceful) 3404 ena_com_set_admin_running_state(ena_dev, false); 3405 3406 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3407 ena_down(adapter); 3408 3409 /* Stop the device from sending AENQ events (in case reset flag is set 3410 * and device is up, ena_down() already reset the device. 3411 */ 3412 if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up)) 3413 ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason); 3414 3415 ena_free_mgmnt_irq(adapter); 3416 3417 ena_disable_msix(adapter); 3418 3419 ena_com_abort_admin_commands(ena_dev); 3420 3421 ena_com_wait_for_abort_completion(ena_dev); 3422 3423 ena_com_admin_destroy(ena_dev); 3424 3425 ena_com_mmio_reg_read_request_destroy(ena_dev); 3426 3427 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 3428 3429 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3430 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3431 } 3432 3433 static int ena_restore_device(struct ena_adapter *adapter) 3434 { 3435 struct ena_com_dev_get_features_ctx get_feat_ctx; 3436 struct ena_com_dev *ena_dev = adapter->ena_dev; 3437 struct pci_dev *pdev = adapter->pdev; 3438 bool wd_state; 3439 int rc; 3440 3441 set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags); 3442 rc = ena_device_init(ena_dev, adapter->pdev, &get_feat_ctx, &wd_state); 3443 if (rc) { 3444 dev_err(&pdev->dev, "Can not initialize device\n"); 3445 goto err; 3446 } 3447 adapter->wd_state = wd_state; 3448 3449 rc = ena_device_validate_params(adapter, &get_feat_ctx); 3450 if (rc) { 3451 dev_err(&pdev->dev, "Validation of device parameters failed\n"); 3452 goto err_device_destroy; 3453 } 3454 3455 rc = ena_enable_msix_and_set_admin_interrupts(adapter); 3456 if (rc) { 3457 dev_err(&pdev->dev, "Enable MSI-X failed\n"); 3458 goto err_device_destroy; 3459 } 3460 /* If the interface was up before the reset bring it up */ 3461 if (adapter->dev_up_before_reset) { 3462 rc = ena_up(adapter); 3463 if (rc) { 3464 dev_err(&pdev->dev, "Failed to create I/O queues\n"); 3465 goto err_disable_msix; 3466 } 3467 } 3468 3469 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3470 3471 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags); 3472 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags)) 3473 netif_carrier_on(adapter->netdev); 3474 3475 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ)); 3476 dev_err(&pdev->dev, "Device reset completed successfully\n"); 3477 3478 return rc; 3479 err_disable_msix: 3480 ena_free_mgmnt_irq(adapter); 3481 ena_disable_msix(adapter); 3482 err_device_destroy: 3483 ena_com_abort_admin_commands(ena_dev); 3484 ena_com_wait_for_abort_completion(ena_dev); 3485 ena_com_admin_destroy(ena_dev); 3486 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE); 3487 ena_com_mmio_reg_read_request_destroy(ena_dev); 3488 err: 3489 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 3490 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags); 3491 dev_err(&pdev->dev, 3492 "Reset attempt failed. Can not reset the device\n"); 3493 3494 return rc; 3495 } 3496 3497 static void ena_fw_reset_device(struct work_struct *work) 3498 { 3499 struct ena_adapter *adapter = 3500 container_of(work, struct ena_adapter, reset_task); 3501 struct pci_dev *pdev = adapter->pdev; 3502 3503 if (unlikely(!test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 3504 dev_err(&pdev->dev, 3505 "device reset schedule while reset bit is off\n"); 3506 return; 3507 } 3508 rtnl_lock(); 3509 ena_destroy_device(adapter, false); 3510 ena_restore_device(adapter); 3511 rtnl_unlock(); 3512 } 3513 3514 static int check_for_rx_interrupt_queue(struct ena_adapter *adapter, 3515 struct ena_ring *rx_ring) 3516 { 3517 if (likely(rx_ring->first_interrupt)) 3518 return 0; 3519 3520 if (ena_com_cq_empty(rx_ring->ena_com_io_cq)) 3521 return 0; 3522 3523 rx_ring->no_interrupt_event_cnt++; 3524 3525 if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) { 3526 netif_err(adapter, rx_err, adapter->netdev, 3527 "Potential MSIX issue on Rx side Queue = %d. Reset the device\n", 3528 rx_ring->qid); 3529 adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT; 3530 smp_mb__before_atomic(); 3531 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3532 return -EIO; 3533 } 3534 3535 return 0; 3536 } 3537 3538 static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter, 3539 struct ena_ring *tx_ring) 3540 { 3541 struct ena_tx_buffer *tx_buf; 3542 unsigned long last_jiffies; 3543 u32 missed_tx = 0; 3544 int i, rc = 0; 3545 3546 for (i = 0; i < tx_ring->ring_size; i++) { 3547 tx_buf = &tx_ring->tx_buffer_info[i]; 3548 last_jiffies = tx_buf->last_jiffies; 3549 3550 if (last_jiffies == 0) 3551 /* no pending Tx at this location */ 3552 continue; 3553 3554 if (unlikely(!tx_ring->first_interrupt && time_is_before_jiffies(last_jiffies + 3555 2 * adapter->missing_tx_completion_to))) { 3556 /* If after graceful period interrupt is still not 3557 * received, we schedule a reset 3558 */ 3559 netif_err(adapter, tx_err, adapter->netdev, 3560 "Potential MSIX issue on Tx side Queue = %d. Reset the device\n", 3561 tx_ring->qid); 3562 adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT; 3563 smp_mb__before_atomic(); 3564 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3565 return -EIO; 3566 } 3567 3568 if (unlikely(time_is_before_jiffies(last_jiffies + 3569 adapter->missing_tx_completion_to))) { 3570 if (!tx_buf->print_once) 3571 netif_notice(adapter, tx_err, adapter->netdev, 3572 "Found a Tx that wasn't completed on time, qid %d, index %d.\n", 3573 tx_ring->qid, i); 3574 3575 tx_buf->print_once = 1; 3576 missed_tx++; 3577 } 3578 } 3579 3580 if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) { 3581 netif_err(adapter, tx_err, adapter->netdev, 3582 "The number of lost tx completions is above the threshold (%d > %d). Reset the device\n", 3583 missed_tx, 3584 adapter->missing_tx_completion_threshold); 3585 adapter->reset_reason = 3586 ENA_REGS_RESET_MISS_TX_CMPL; 3587 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3588 rc = -EIO; 3589 } 3590 3591 u64_stats_update_begin(&tx_ring->syncp); 3592 tx_ring->tx_stats.missed_tx = missed_tx; 3593 u64_stats_update_end(&tx_ring->syncp); 3594 3595 return rc; 3596 } 3597 3598 static void check_for_missing_completions(struct ena_adapter *adapter) 3599 { 3600 struct ena_ring *tx_ring; 3601 struct ena_ring *rx_ring; 3602 int i, budget, rc; 3603 int io_queue_count; 3604 3605 io_queue_count = adapter->xdp_num_queues + adapter->num_io_queues; 3606 /* Make sure the driver doesn't turn the device in other process */ 3607 smp_rmb(); 3608 3609 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3610 return; 3611 3612 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) 3613 return; 3614 3615 if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT) 3616 return; 3617 3618 budget = ENA_MONITORED_TX_QUEUES; 3619 3620 for (i = adapter->last_monitored_tx_qid; i < io_queue_count; i++) { 3621 tx_ring = &adapter->tx_ring[i]; 3622 rx_ring = &adapter->rx_ring[i]; 3623 3624 rc = check_missing_comp_in_tx_queue(adapter, tx_ring); 3625 if (unlikely(rc)) 3626 return; 3627 3628 rc = !ENA_IS_XDP_INDEX(adapter, i) ? 3629 check_for_rx_interrupt_queue(adapter, rx_ring) : 0; 3630 if (unlikely(rc)) 3631 return; 3632 3633 budget--; 3634 if (!budget) 3635 break; 3636 } 3637 3638 adapter->last_monitored_tx_qid = i % io_queue_count; 3639 } 3640 3641 /* trigger napi schedule after 2 consecutive detections */ 3642 #define EMPTY_RX_REFILL 2 3643 /* For the rare case where the device runs out of Rx descriptors and the 3644 * napi handler failed to refill new Rx descriptors (due to a lack of memory 3645 * for example). 3646 * This case will lead to a deadlock: 3647 * The device won't send interrupts since all the new Rx packets will be dropped 3648 * The napi handler won't allocate new Rx descriptors so the device will be 3649 * able to send new packets. 3650 * 3651 * This scenario can happen when the kernel's vm.min_free_kbytes is too small. 3652 * It is recommended to have at least 512MB, with a minimum of 128MB for 3653 * constrained environment). 3654 * 3655 * When such a situation is detected - Reschedule napi 3656 */ 3657 static void check_for_empty_rx_ring(struct ena_adapter *adapter) 3658 { 3659 struct ena_ring *rx_ring; 3660 int i, refill_required; 3661 3662 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags)) 3663 return; 3664 3665 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) 3666 return; 3667 3668 for (i = 0; i < adapter->num_io_queues; i++) { 3669 rx_ring = &adapter->rx_ring[i]; 3670 3671 refill_required = 3672 ena_com_free_desc(rx_ring->ena_com_io_sq); 3673 if (unlikely(refill_required == (rx_ring->ring_size - 1))) { 3674 rx_ring->empty_rx_queue++; 3675 3676 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) { 3677 u64_stats_update_begin(&rx_ring->syncp); 3678 rx_ring->rx_stats.empty_rx_ring++; 3679 u64_stats_update_end(&rx_ring->syncp); 3680 3681 netif_err(adapter, drv, adapter->netdev, 3682 "trigger refill for ring %d\n", i); 3683 3684 napi_schedule(rx_ring->napi); 3685 rx_ring->empty_rx_queue = 0; 3686 } 3687 } else { 3688 rx_ring->empty_rx_queue = 0; 3689 } 3690 } 3691 } 3692 3693 /* Check for keep alive expiration */ 3694 static void check_for_missing_keep_alive(struct ena_adapter *adapter) 3695 { 3696 unsigned long keep_alive_expired; 3697 3698 if (!adapter->wd_state) 3699 return; 3700 3701 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3702 return; 3703 3704 keep_alive_expired = adapter->last_keep_alive_jiffies + 3705 adapter->keep_alive_timeout; 3706 if (unlikely(time_is_before_jiffies(keep_alive_expired))) { 3707 netif_err(adapter, drv, adapter->netdev, 3708 "Keep alive watchdog timeout.\n"); 3709 u64_stats_update_begin(&adapter->syncp); 3710 adapter->dev_stats.wd_expired++; 3711 u64_stats_update_end(&adapter->syncp); 3712 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO; 3713 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3714 } 3715 } 3716 3717 static void check_for_admin_com_state(struct ena_adapter *adapter) 3718 { 3719 if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) { 3720 netif_err(adapter, drv, adapter->netdev, 3721 "ENA admin queue is not in running state!\n"); 3722 u64_stats_update_begin(&adapter->syncp); 3723 adapter->dev_stats.admin_q_pause++; 3724 u64_stats_update_end(&adapter->syncp); 3725 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO; 3726 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 3727 } 3728 } 3729 3730 static void ena_update_hints(struct ena_adapter *adapter, 3731 struct ena_admin_ena_hw_hints *hints) 3732 { 3733 struct net_device *netdev = adapter->netdev; 3734 3735 if (hints->admin_completion_tx_timeout) 3736 adapter->ena_dev->admin_queue.completion_timeout = 3737 hints->admin_completion_tx_timeout * 1000; 3738 3739 if (hints->mmio_read_timeout) 3740 /* convert to usec */ 3741 adapter->ena_dev->mmio_read.reg_read_to = 3742 hints->mmio_read_timeout * 1000; 3743 3744 if (hints->missed_tx_completion_count_threshold_to_reset) 3745 adapter->missing_tx_completion_threshold = 3746 hints->missed_tx_completion_count_threshold_to_reset; 3747 3748 if (hints->missing_tx_completion_timeout) { 3749 if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3750 adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT; 3751 else 3752 adapter->missing_tx_completion_to = 3753 msecs_to_jiffies(hints->missing_tx_completion_timeout); 3754 } 3755 3756 if (hints->netdev_wd_timeout) 3757 netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout); 3758 3759 if (hints->driver_watchdog_timeout) { 3760 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT) 3761 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT; 3762 else 3763 adapter->keep_alive_timeout = 3764 msecs_to_jiffies(hints->driver_watchdog_timeout); 3765 } 3766 } 3767 3768 static void ena_update_host_info(struct ena_admin_host_info *host_info, 3769 struct net_device *netdev) 3770 { 3771 host_info->supported_network_features[0] = 3772 netdev->features & GENMASK_ULL(31, 0); 3773 host_info->supported_network_features[1] = 3774 (netdev->features & GENMASK_ULL(63, 32)) >> 32; 3775 } 3776 3777 static void ena_timer_service(struct timer_list *t) 3778 { 3779 struct ena_adapter *adapter = from_timer(adapter, t, timer_service); 3780 u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr; 3781 struct ena_admin_host_info *host_info = 3782 adapter->ena_dev->host_attr.host_info; 3783 3784 check_for_missing_keep_alive(adapter); 3785 3786 check_for_admin_com_state(adapter); 3787 3788 check_for_missing_completions(adapter); 3789 3790 check_for_empty_rx_ring(adapter); 3791 3792 if (debug_area) 3793 ena_dump_stats_to_buf(adapter, debug_area); 3794 3795 if (host_info) 3796 ena_update_host_info(host_info, adapter->netdev); 3797 3798 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 3799 netif_err(adapter, drv, adapter->netdev, 3800 "Trigger reset is on\n"); 3801 ena_dump_stats_to_dmesg(adapter); 3802 queue_work(ena_wq, &adapter->reset_task); 3803 return; 3804 } 3805 3806 /* Reset the timer */ 3807 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ)); 3808 } 3809 3810 static int ena_calc_max_io_queue_num(struct pci_dev *pdev, 3811 struct ena_com_dev *ena_dev, 3812 struct ena_com_dev_get_features_ctx *get_feat_ctx) 3813 { 3814 int io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues; 3815 3816 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) { 3817 struct ena_admin_queue_ext_feature_fields *max_queue_ext = 3818 &get_feat_ctx->max_queue_ext.max_queue_ext; 3819 io_rx_num = min_t(u32, max_queue_ext->max_rx_sq_num, 3820 max_queue_ext->max_rx_cq_num); 3821 3822 io_tx_sq_num = max_queue_ext->max_tx_sq_num; 3823 io_tx_cq_num = max_queue_ext->max_tx_cq_num; 3824 } else { 3825 struct ena_admin_queue_feature_desc *max_queues = 3826 &get_feat_ctx->max_queues; 3827 io_tx_sq_num = max_queues->max_sq_num; 3828 io_tx_cq_num = max_queues->max_cq_num; 3829 io_rx_num = min_t(u32, io_tx_sq_num, io_tx_cq_num); 3830 } 3831 3832 /* In case of LLQ use the llq fields for the tx SQ/CQ */ 3833 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 3834 io_tx_sq_num = get_feat_ctx->llq.max_llq_num; 3835 3836 max_num_io_queues = min_t(u32, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES); 3837 max_num_io_queues = min_t(u32, max_num_io_queues, io_rx_num); 3838 max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_sq_num); 3839 max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_cq_num); 3840 /* 1 IRQ for for mgmnt and 1 IRQs for each IO direction */ 3841 max_num_io_queues = min_t(u32, max_num_io_queues, pci_msix_vec_count(pdev) - 1); 3842 if (unlikely(!max_num_io_queues)) { 3843 dev_err(&pdev->dev, "The device doesn't have io queues\n"); 3844 return -EFAULT; 3845 } 3846 3847 return max_num_io_queues; 3848 } 3849 3850 static int ena_set_queues_placement_policy(struct pci_dev *pdev, 3851 struct ena_com_dev *ena_dev, 3852 struct ena_admin_feature_llq_desc *llq, 3853 struct ena_llq_configurations *llq_default_configurations) 3854 { 3855 bool has_mem_bar; 3856 int rc; 3857 u32 llq_feature_mask; 3858 3859 llq_feature_mask = 1 << ENA_ADMIN_LLQ; 3860 if (!(ena_dev->supported_features & llq_feature_mask)) { 3861 dev_err(&pdev->dev, 3862 "LLQ is not supported Fallback to host mode policy.\n"); 3863 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 3864 return 0; 3865 } 3866 3867 has_mem_bar = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(ENA_MEM_BAR); 3868 3869 rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations); 3870 if (unlikely(rc)) { 3871 dev_err(&pdev->dev, 3872 "Failed to configure the device mode. Fallback to host mode policy.\n"); 3873 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 3874 return 0; 3875 } 3876 3877 /* Nothing to config, exit */ 3878 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST) 3879 return 0; 3880 3881 if (!has_mem_bar) { 3882 dev_err(&pdev->dev, 3883 "ENA device does not expose LLQ bar. Fallback to host mode policy.\n"); 3884 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST; 3885 return 0; 3886 } 3887 3888 ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev, 3889 pci_resource_start(pdev, ENA_MEM_BAR), 3890 pci_resource_len(pdev, ENA_MEM_BAR)); 3891 3892 if (!ena_dev->mem_bar) 3893 return -EFAULT; 3894 3895 return 0; 3896 } 3897 3898 static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat, 3899 struct net_device *netdev) 3900 { 3901 netdev_features_t dev_features = 0; 3902 3903 /* Set offload features */ 3904 if (feat->offload.tx & 3905 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK) 3906 dev_features |= NETIF_F_IP_CSUM; 3907 3908 if (feat->offload.tx & 3909 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK) 3910 dev_features |= NETIF_F_IPV6_CSUM; 3911 3912 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) 3913 dev_features |= NETIF_F_TSO; 3914 3915 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) 3916 dev_features |= NETIF_F_TSO6; 3917 3918 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK) 3919 dev_features |= NETIF_F_TSO_ECN; 3920 3921 if (feat->offload.rx_supported & 3922 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK) 3923 dev_features |= NETIF_F_RXCSUM; 3924 3925 if (feat->offload.rx_supported & 3926 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) 3927 dev_features |= NETIF_F_RXCSUM; 3928 3929 netdev->features = 3930 dev_features | 3931 NETIF_F_SG | 3932 NETIF_F_RXHASH | 3933 NETIF_F_HIGHDMA; 3934 3935 netdev->hw_features |= netdev->features; 3936 netdev->vlan_features |= netdev->features; 3937 } 3938 3939 static void ena_set_conf_feat_params(struct ena_adapter *adapter, 3940 struct ena_com_dev_get_features_ctx *feat) 3941 { 3942 struct net_device *netdev = adapter->netdev; 3943 3944 /* Copy mac address */ 3945 if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) { 3946 eth_hw_addr_random(netdev); 3947 ether_addr_copy(adapter->mac_addr, netdev->dev_addr); 3948 } else { 3949 ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr); 3950 ether_addr_copy(netdev->dev_addr, adapter->mac_addr); 3951 } 3952 3953 /* Set offload features */ 3954 ena_set_dev_offloads(feat, netdev); 3955 3956 adapter->max_mtu = feat->dev_attr.max_mtu; 3957 netdev->max_mtu = adapter->max_mtu; 3958 netdev->min_mtu = ENA_MIN_MTU; 3959 } 3960 3961 static int ena_rss_init_default(struct ena_adapter *adapter) 3962 { 3963 struct ena_com_dev *ena_dev = adapter->ena_dev; 3964 struct device *dev = &adapter->pdev->dev; 3965 int rc, i; 3966 u32 val; 3967 3968 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE); 3969 if (unlikely(rc)) { 3970 dev_err(dev, "Cannot init indirect table\n"); 3971 goto err_rss_init; 3972 } 3973 3974 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) { 3975 val = ethtool_rxfh_indir_default(i, adapter->num_io_queues); 3976 rc = ena_com_indirect_table_fill_entry(ena_dev, i, 3977 ENA_IO_RXQ_IDX(val)); 3978 if (unlikely(rc && (rc != -EOPNOTSUPP))) { 3979 dev_err(dev, "Cannot fill indirect table\n"); 3980 goto err_fill_indir; 3981 } 3982 } 3983 3984 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL, 3985 ENA_HASH_KEY_SIZE, 0xFFFFFFFF); 3986 if (unlikely(rc && (rc != -EOPNOTSUPP))) { 3987 dev_err(dev, "Cannot fill hash function\n"); 3988 goto err_fill_indir; 3989 } 3990 3991 rc = ena_com_set_default_hash_ctrl(ena_dev); 3992 if (unlikely(rc && (rc != -EOPNOTSUPP))) { 3993 dev_err(dev, "Cannot fill hash control\n"); 3994 goto err_fill_indir; 3995 } 3996 3997 return 0; 3998 3999 err_fill_indir: 4000 ena_com_rss_destroy(ena_dev); 4001 err_rss_init: 4002 4003 return rc; 4004 } 4005 4006 static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev) 4007 { 4008 int release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK; 4009 4010 pci_release_selected_regions(pdev, release_bars); 4011 } 4012 4013 static void set_default_llq_configurations(struct ena_llq_configurations *llq_config) 4014 { 4015 llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER; 4016 llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B; 4017 llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY; 4018 llq_config->llq_num_decs_before_header = ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2; 4019 llq_config->llq_ring_entry_size_value = 128; 4020 } 4021 4022 static int ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx) 4023 { 4024 struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq; 4025 struct ena_com_dev *ena_dev = ctx->ena_dev; 4026 u32 tx_queue_size = ENA_DEFAULT_RING_SIZE; 4027 u32 rx_queue_size = ENA_DEFAULT_RING_SIZE; 4028 u32 max_tx_queue_size; 4029 u32 max_rx_queue_size; 4030 4031 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) { 4032 struct ena_admin_queue_ext_feature_fields *max_queue_ext = 4033 &ctx->get_feat_ctx->max_queue_ext.max_queue_ext; 4034 max_rx_queue_size = min_t(u32, max_queue_ext->max_rx_cq_depth, 4035 max_queue_ext->max_rx_sq_depth); 4036 max_tx_queue_size = max_queue_ext->max_tx_cq_depth; 4037 4038 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 4039 max_tx_queue_size = min_t(u32, max_tx_queue_size, 4040 llq->max_llq_depth); 4041 else 4042 max_tx_queue_size = min_t(u32, max_tx_queue_size, 4043 max_queue_ext->max_tx_sq_depth); 4044 4045 ctx->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 4046 max_queue_ext->max_per_packet_tx_descs); 4047 ctx->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 4048 max_queue_ext->max_per_packet_rx_descs); 4049 } else { 4050 struct ena_admin_queue_feature_desc *max_queues = 4051 &ctx->get_feat_ctx->max_queues; 4052 max_rx_queue_size = min_t(u32, max_queues->max_cq_depth, 4053 max_queues->max_sq_depth); 4054 max_tx_queue_size = max_queues->max_cq_depth; 4055 4056 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) 4057 max_tx_queue_size = min_t(u32, max_tx_queue_size, 4058 llq->max_llq_depth); 4059 else 4060 max_tx_queue_size = min_t(u32, max_tx_queue_size, 4061 max_queues->max_sq_depth); 4062 4063 ctx->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 4064 max_queues->max_packet_tx_descs); 4065 ctx->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS, 4066 max_queues->max_packet_rx_descs); 4067 } 4068 4069 max_tx_queue_size = rounddown_pow_of_two(max_tx_queue_size); 4070 max_rx_queue_size = rounddown_pow_of_two(max_rx_queue_size); 4071 4072 tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE, 4073 max_tx_queue_size); 4074 rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE, 4075 max_rx_queue_size); 4076 4077 tx_queue_size = rounddown_pow_of_two(tx_queue_size); 4078 rx_queue_size = rounddown_pow_of_two(rx_queue_size); 4079 4080 ctx->max_tx_queue_size = max_tx_queue_size; 4081 ctx->max_rx_queue_size = max_rx_queue_size; 4082 ctx->tx_queue_size = tx_queue_size; 4083 ctx->rx_queue_size = rx_queue_size; 4084 4085 return 0; 4086 } 4087 4088 /* ena_probe - Device Initialization Routine 4089 * @pdev: PCI device information struct 4090 * @ent: entry in ena_pci_tbl 4091 * 4092 * Returns 0 on success, negative on failure 4093 * 4094 * ena_probe initializes an adapter identified by a pci_dev structure. 4095 * The OS initialization, configuring of the adapter private structure, 4096 * and a hardware reset occur. 4097 */ 4098 static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 4099 { 4100 struct ena_com_dev_get_features_ctx get_feat_ctx; 4101 struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 }; 4102 struct ena_llq_configurations llq_config; 4103 struct ena_com_dev *ena_dev = NULL; 4104 struct ena_adapter *adapter; 4105 struct net_device *netdev; 4106 static int adapters_found; 4107 u32 max_num_io_queues; 4108 char *queue_type_str; 4109 bool wd_state; 4110 int bars, rc; 4111 4112 dev_dbg(&pdev->dev, "%s\n", __func__); 4113 4114 rc = pci_enable_device_mem(pdev); 4115 if (rc) { 4116 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n"); 4117 return rc; 4118 } 4119 4120 pci_set_master(pdev); 4121 4122 ena_dev = vzalloc(sizeof(*ena_dev)); 4123 if (!ena_dev) { 4124 rc = -ENOMEM; 4125 goto err_disable_device; 4126 } 4127 4128 bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK; 4129 rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME); 4130 if (rc) { 4131 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n", 4132 rc); 4133 goto err_free_ena_dev; 4134 } 4135 4136 ena_dev->reg_bar = devm_ioremap(&pdev->dev, 4137 pci_resource_start(pdev, ENA_REG_BAR), 4138 pci_resource_len(pdev, ENA_REG_BAR)); 4139 if (!ena_dev->reg_bar) { 4140 dev_err(&pdev->dev, "failed to remap regs bar\n"); 4141 rc = -EFAULT; 4142 goto err_free_region; 4143 } 4144 4145 ena_dev->dmadev = &pdev->dev; 4146 4147 rc = ena_device_init(ena_dev, pdev, &get_feat_ctx, &wd_state); 4148 if (rc) { 4149 dev_err(&pdev->dev, "ena device init failed\n"); 4150 if (rc == -ETIME) 4151 rc = -EPROBE_DEFER; 4152 goto err_free_region; 4153 } 4154 4155 set_default_llq_configurations(&llq_config); 4156 4157 rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx.llq, 4158 &llq_config); 4159 if (rc) { 4160 dev_err(&pdev->dev, "ena device init failed\n"); 4161 goto err_device_destroy; 4162 } 4163 4164 calc_queue_ctx.ena_dev = ena_dev; 4165 calc_queue_ctx.get_feat_ctx = &get_feat_ctx; 4166 calc_queue_ctx.pdev = pdev; 4167 4168 /* Initial Tx and RX interrupt delay. Assumes 1 usec granularity. 4169 * Updated during device initialization with the real granularity 4170 */ 4171 ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS; 4172 ena_dev->intr_moder_rx_interval = ENA_INTR_INITIAL_RX_INTERVAL_USECS; 4173 ena_dev->intr_delay_resolution = ENA_DEFAULT_INTR_DELAY_RESOLUTION; 4174 max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev, &get_feat_ctx); 4175 rc = ena_calc_io_queue_size(&calc_queue_ctx); 4176 if (rc || !max_num_io_queues) { 4177 rc = -EFAULT; 4178 goto err_device_destroy; 4179 } 4180 4181 /* dev zeroed in init_etherdev */ 4182 netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), max_num_io_queues); 4183 if (!netdev) { 4184 dev_err(&pdev->dev, "alloc_etherdev_mq failed\n"); 4185 rc = -ENOMEM; 4186 goto err_device_destroy; 4187 } 4188 4189 SET_NETDEV_DEV(netdev, &pdev->dev); 4190 4191 adapter = netdev_priv(netdev); 4192 pci_set_drvdata(pdev, adapter); 4193 4194 adapter->ena_dev = ena_dev; 4195 adapter->netdev = netdev; 4196 adapter->pdev = pdev; 4197 4198 ena_set_conf_feat_params(adapter, &get_feat_ctx); 4199 4200 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE); 4201 adapter->reset_reason = ENA_REGS_RESET_NORMAL; 4202 4203 adapter->requested_tx_ring_size = calc_queue_ctx.tx_queue_size; 4204 adapter->requested_rx_ring_size = calc_queue_ctx.rx_queue_size; 4205 adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size; 4206 adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size; 4207 adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size; 4208 adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size; 4209 4210 adapter->num_io_queues = max_num_io_queues; 4211 adapter->max_num_io_queues = max_num_io_queues; 4212 4213 adapter->xdp_first_ring = 0; 4214 adapter->xdp_num_queues = 0; 4215 4216 adapter->last_monitored_tx_qid = 0; 4217 4218 adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK; 4219 adapter->wd_state = wd_state; 4220 4221 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found); 4222 4223 rc = ena_com_init_interrupt_moderation(adapter->ena_dev); 4224 if (rc) { 4225 dev_err(&pdev->dev, 4226 "Failed to query interrupt moderation feature\n"); 4227 goto err_netdev_destroy; 4228 } 4229 ena_init_io_rings(adapter, 4230 0, 4231 adapter->xdp_num_queues + 4232 adapter->num_io_queues); 4233 4234 netdev->netdev_ops = &ena_netdev_ops; 4235 netdev->watchdog_timeo = TX_TIMEOUT; 4236 ena_set_ethtool_ops(netdev); 4237 4238 netdev->priv_flags |= IFF_UNICAST_FLT; 4239 4240 u64_stats_init(&adapter->syncp); 4241 4242 rc = ena_enable_msix_and_set_admin_interrupts(adapter); 4243 if (rc) { 4244 dev_err(&pdev->dev, 4245 "Failed to enable and set the admin interrupts\n"); 4246 goto err_worker_destroy; 4247 } 4248 rc = ena_rss_init_default(adapter); 4249 if (rc && (rc != -EOPNOTSUPP)) { 4250 dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc); 4251 goto err_free_msix; 4252 } 4253 4254 ena_config_debug_area(adapter); 4255 4256 memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len); 4257 4258 netif_carrier_off(netdev); 4259 4260 rc = register_netdev(netdev); 4261 if (rc) { 4262 dev_err(&pdev->dev, "Cannot register net device\n"); 4263 goto err_rss; 4264 } 4265 4266 INIT_WORK(&adapter->reset_task, ena_fw_reset_device); 4267 4268 adapter->last_keep_alive_jiffies = jiffies; 4269 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT; 4270 adapter->missing_tx_completion_to = TX_TIMEOUT; 4271 adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS; 4272 4273 ena_update_hints(adapter, &get_feat_ctx.hw_hints); 4274 4275 timer_setup(&adapter->timer_service, ena_timer_service, 0); 4276 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ)); 4277 4278 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST) 4279 queue_type_str = "Regular"; 4280 else 4281 queue_type_str = "Low Latency"; 4282 4283 dev_info(&pdev->dev, 4284 "%s found at mem %lx, mac addr %pM, Placement policy: %s\n", 4285 DEVICE_NAME, (long)pci_resource_start(pdev, 0), 4286 netdev->dev_addr, queue_type_str); 4287 4288 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags); 4289 4290 adapters_found++; 4291 4292 return 0; 4293 4294 err_rss: 4295 ena_com_delete_debug_area(ena_dev); 4296 ena_com_rss_destroy(ena_dev); 4297 err_free_msix: 4298 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR); 4299 /* stop submitting admin commands on a device that was reset */ 4300 ena_com_set_admin_running_state(ena_dev, false); 4301 ena_free_mgmnt_irq(adapter); 4302 ena_disable_msix(adapter); 4303 err_worker_destroy: 4304 del_timer(&adapter->timer_service); 4305 err_netdev_destroy: 4306 free_netdev(netdev); 4307 err_device_destroy: 4308 ena_com_delete_host_info(ena_dev); 4309 ena_com_admin_destroy(ena_dev); 4310 err_free_region: 4311 ena_release_bars(ena_dev, pdev); 4312 err_free_ena_dev: 4313 vfree(ena_dev); 4314 err_disable_device: 4315 pci_disable_device(pdev); 4316 return rc; 4317 } 4318 4319 /*****************************************************************************/ 4320 4321 /* ena_remove - Device Removal Routine 4322 * @pdev: PCI device information struct 4323 * 4324 * ena_remove is called by the PCI subsystem to alert the driver 4325 * that it should release a PCI device. 4326 */ 4327 static void ena_remove(struct pci_dev *pdev) 4328 { 4329 struct ena_adapter *adapter = pci_get_drvdata(pdev); 4330 struct ena_com_dev *ena_dev; 4331 struct net_device *netdev; 4332 4333 ena_dev = adapter->ena_dev; 4334 netdev = adapter->netdev; 4335 4336 #ifdef CONFIG_RFS_ACCEL 4337 if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) { 4338 free_irq_cpu_rmap(netdev->rx_cpu_rmap); 4339 netdev->rx_cpu_rmap = NULL; 4340 } 4341 #endif /* CONFIG_RFS_ACCEL */ 4342 del_timer_sync(&adapter->timer_service); 4343 4344 cancel_work_sync(&adapter->reset_task); 4345 4346 rtnl_lock(); 4347 ena_destroy_device(adapter, true); 4348 rtnl_unlock(); 4349 4350 unregister_netdev(netdev); 4351 4352 free_netdev(netdev); 4353 4354 ena_com_rss_destroy(ena_dev); 4355 4356 ena_com_delete_debug_area(ena_dev); 4357 4358 ena_com_delete_host_info(ena_dev); 4359 4360 ena_release_bars(ena_dev, pdev); 4361 4362 pci_disable_device(pdev); 4363 4364 vfree(ena_dev); 4365 } 4366 4367 #ifdef CONFIG_PM 4368 /* ena_suspend - PM suspend callback 4369 * @pdev: PCI device information struct 4370 * @state:power state 4371 */ 4372 static int ena_suspend(struct pci_dev *pdev, pm_message_t state) 4373 { 4374 struct ena_adapter *adapter = pci_get_drvdata(pdev); 4375 4376 u64_stats_update_begin(&adapter->syncp); 4377 adapter->dev_stats.suspend++; 4378 u64_stats_update_end(&adapter->syncp); 4379 4380 rtnl_lock(); 4381 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) { 4382 dev_err(&pdev->dev, 4383 "ignoring device reset request as the device is being suspended\n"); 4384 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags); 4385 } 4386 ena_destroy_device(adapter, true); 4387 rtnl_unlock(); 4388 return 0; 4389 } 4390 4391 /* ena_resume - PM resume callback 4392 * @pdev: PCI device information struct 4393 * 4394 */ 4395 static int ena_resume(struct pci_dev *pdev) 4396 { 4397 struct ena_adapter *adapter = pci_get_drvdata(pdev); 4398 int rc; 4399 4400 u64_stats_update_begin(&adapter->syncp); 4401 adapter->dev_stats.resume++; 4402 u64_stats_update_end(&adapter->syncp); 4403 4404 rtnl_lock(); 4405 rc = ena_restore_device(adapter); 4406 rtnl_unlock(); 4407 return rc; 4408 } 4409 #endif 4410 4411 static struct pci_driver ena_pci_driver = { 4412 .name = DRV_MODULE_NAME, 4413 .id_table = ena_pci_tbl, 4414 .probe = ena_probe, 4415 .remove = ena_remove, 4416 #ifdef CONFIG_PM 4417 .suspend = ena_suspend, 4418 .resume = ena_resume, 4419 #endif 4420 .sriov_configure = pci_sriov_configure_simple, 4421 }; 4422 4423 static int __init ena_init(void) 4424 { 4425 ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME); 4426 if (!ena_wq) { 4427 pr_err("Failed to create workqueue\n"); 4428 return -ENOMEM; 4429 } 4430 4431 return pci_register_driver(&ena_pci_driver); 4432 } 4433 4434 static void __exit ena_cleanup(void) 4435 { 4436 pci_unregister_driver(&ena_pci_driver); 4437 4438 if (ena_wq) { 4439 destroy_workqueue(ena_wq); 4440 ena_wq = NULL; 4441 } 4442 } 4443 4444 /****************************************************************************** 4445 ******************************** AENQ Handlers ******************************* 4446 *****************************************************************************/ 4447 /* ena_update_on_link_change: 4448 * Notify the network interface about the change in link status 4449 */ 4450 static void ena_update_on_link_change(void *adapter_data, 4451 struct ena_admin_aenq_entry *aenq_e) 4452 { 4453 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4454 struct ena_admin_aenq_link_change_desc *aenq_desc = 4455 (struct ena_admin_aenq_link_change_desc *)aenq_e; 4456 int status = aenq_desc->flags & 4457 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK; 4458 4459 if (status) { 4460 netdev_dbg(adapter->netdev, "%s\n", __func__); 4461 set_bit(ENA_FLAG_LINK_UP, &adapter->flags); 4462 if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags)) 4463 netif_carrier_on(adapter->netdev); 4464 } else { 4465 clear_bit(ENA_FLAG_LINK_UP, &adapter->flags); 4466 netif_carrier_off(adapter->netdev); 4467 } 4468 } 4469 4470 static void ena_keep_alive_wd(void *adapter_data, 4471 struct ena_admin_aenq_entry *aenq_e) 4472 { 4473 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4474 struct ena_admin_aenq_keep_alive_desc *desc; 4475 u64 rx_drops; 4476 4477 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e; 4478 adapter->last_keep_alive_jiffies = jiffies; 4479 4480 rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low; 4481 4482 u64_stats_update_begin(&adapter->syncp); 4483 adapter->dev_stats.rx_drops = rx_drops; 4484 u64_stats_update_end(&adapter->syncp); 4485 } 4486 4487 static void ena_notification(void *adapter_data, 4488 struct ena_admin_aenq_entry *aenq_e) 4489 { 4490 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data; 4491 struct ena_admin_ena_hw_hints *hints; 4492 4493 WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION, 4494 "Invalid group(%x) expected %x\n", 4495 aenq_e->aenq_common_desc.group, 4496 ENA_ADMIN_NOTIFICATION); 4497 4498 switch (aenq_e->aenq_common_desc.syndrom) { 4499 case ENA_ADMIN_UPDATE_HINTS: 4500 hints = (struct ena_admin_ena_hw_hints *) 4501 (&aenq_e->inline_data_w4); 4502 ena_update_hints(adapter, hints); 4503 break; 4504 default: 4505 netif_err(adapter, drv, adapter->netdev, 4506 "Invalid aenq notification link state %d\n", 4507 aenq_e->aenq_common_desc.syndrom); 4508 } 4509 } 4510 4511 /* This handler will called for unknown event group or unimplemented handlers*/ 4512 static void unimplemented_aenq_handler(void *data, 4513 struct ena_admin_aenq_entry *aenq_e) 4514 { 4515 struct ena_adapter *adapter = (struct ena_adapter *)data; 4516 4517 netif_err(adapter, drv, adapter->netdev, 4518 "Unknown event was received or event with unimplemented handler\n"); 4519 } 4520 4521 static struct ena_aenq_handlers aenq_handlers = { 4522 .handlers = { 4523 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change, 4524 [ENA_ADMIN_NOTIFICATION] = ena_notification, 4525 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd, 4526 }, 4527 .unimplemented_handler = unimplemented_aenq_handler 4528 }; 4529 4530 module_init(ena_init); 4531 module_exit(ena_cleanup); 4532