1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #ifdef CONFIG_RFS_ACCEL
9 #include <linux/cpu_rmap.h>
10 #endif /* CONFIG_RFS_ACCEL */
11 #include <linux/ethtool.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/numa.h>
15 #include <linux/pci.h>
16 #include <linux/utsname.h>
17 #include <linux/version.h>
18 #include <linux/vmalloc.h>
21 #include "ena_netdev.h"
22 #include <linux/bpf_trace.h>
23 #include "ena_pci_id_tbl.h"
25 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
26 MODULE_DESCRIPTION(DEVICE_NAME);
27 MODULE_LICENSE("GPL");
29 /* Time in jiffies before concluding the transmitter is hung. */
30 #define TX_TIMEOUT (5 * HZ)
32 #define ENA_MAX_RINGS min_t(unsigned int, ENA_MAX_NUM_IO_QUEUES, num_possible_cpus())
34 #define ENA_NAPI_BUDGET 64
36 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \
37 NETIF_MSG_TX_DONE | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
39 static struct ena_aenq_handlers aenq_handlers;
41 static struct workqueue_struct *ena_wq;
43 MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
45 static int ena_rss_init_default(struct ena_adapter *adapter);
46 static void check_for_admin_com_state(struct ena_adapter *adapter);
47 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful);
48 static int ena_restore_device(struct ena_adapter *adapter);
50 static void ena_init_io_rings(struct ena_adapter *adapter,
51 int first_index, int count);
52 static void ena_init_napi_in_range(struct ena_adapter *adapter, int first_index,
54 static void ena_del_napi_in_range(struct ena_adapter *adapter, int first_index,
56 static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid);
57 static int ena_setup_tx_resources_in_range(struct ena_adapter *adapter,
60 static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid);
61 static void ena_free_tx_resources(struct ena_adapter *adapter, int qid);
62 static int ena_clean_xdp_irq(struct ena_ring *xdp_ring, u32 budget);
63 static void ena_destroy_all_tx_queues(struct ena_adapter *adapter);
64 static void ena_free_all_io_tx_resources(struct ena_adapter *adapter);
65 static void ena_napi_disable_in_range(struct ena_adapter *adapter,
66 int first_index, int count);
67 static void ena_napi_enable_in_range(struct ena_adapter *adapter,
68 int first_index, int count);
69 static int ena_up(struct ena_adapter *adapter);
70 static void ena_down(struct ena_adapter *adapter);
71 static void ena_unmask_interrupt(struct ena_ring *tx_ring,
72 struct ena_ring *rx_ring);
73 static void ena_update_ring_numa_node(struct ena_ring *tx_ring,
74 struct ena_ring *rx_ring);
75 static void ena_unmap_tx_buff(struct ena_ring *tx_ring,
76 struct ena_tx_buffer *tx_info);
77 static int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter,
78 int first_index, int count);
80 /* Increase a stat by cnt while holding syncp seqlock on 32bit machines */
81 static void ena_increase_stat(u64 *statp, u64 cnt,
82 struct u64_stats_sync *syncp)
84 u64_stats_update_begin(syncp);
86 u64_stats_update_end(syncp);
89 static void ena_ring_tx_doorbell(struct ena_ring *tx_ring)
91 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
92 ena_increase_stat(&tx_ring->tx_stats.doorbells, 1, &tx_ring->syncp);
95 static void ena_tx_timeout(struct net_device *dev, unsigned int txqueue)
97 struct ena_adapter *adapter = netdev_priv(dev);
99 /* Change the state of the device to trigger reset
100 * Check that we are not in the middle or a trigger already
103 if (test_and_set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
106 adapter->reset_reason = ENA_REGS_RESET_OS_NETDEV_WD;
107 ena_increase_stat(&adapter->dev_stats.tx_timeout, 1, &adapter->syncp);
109 netif_err(adapter, tx_err, dev, "Transmit time out\n");
112 static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
116 for (i = 0; i < adapter->num_io_queues; i++)
117 adapter->rx_ring[i].mtu = mtu;
120 static int ena_change_mtu(struct net_device *dev, int new_mtu)
122 struct ena_adapter *adapter = netdev_priv(dev);
125 ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
127 netif_dbg(adapter, drv, dev, "Set MTU to %d\n", new_mtu);
128 update_rx_ring_mtu(adapter, new_mtu);
131 netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
138 static int ena_xmit_common(struct net_device *dev,
139 struct ena_ring *ring,
140 struct ena_tx_buffer *tx_info,
141 struct ena_com_tx_ctx *ena_tx_ctx,
145 struct ena_adapter *adapter = netdev_priv(dev);
148 if (unlikely(ena_com_is_doorbell_needed(ring->ena_com_io_sq,
150 netif_dbg(adapter, tx_queued, dev,
151 "llq tx max burst size of queue %d achieved, writing doorbell to send burst\n",
153 ena_ring_tx_doorbell(ring);
156 /* prepare the packet's descriptors to dma engine */
157 rc = ena_com_prepare_tx(ring->ena_com_io_sq, ena_tx_ctx,
160 /* In case there isn't enough space in the queue for the packet,
161 * we simply drop it. All other failure reasons of
162 * ena_com_prepare_tx() are fatal and therefore require a device reset.
165 netif_err(adapter, tx_queued, dev,
166 "Failed to prepare tx bufs\n");
167 ena_increase_stat(&ring->tx_stats.prepare_ctx_err, 1,
170 adapter->reset_reason =
171 ENA_REGS_RESET_DRIVER_INVALID_STATE;
172 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
177 u64_stats_update_begin(&ring->syncp);
178 ring->tx_stats.cnt++;
179 ring->tx_stats.bytes += bytes;
180 u64_stats_update_end(&ring->syncp);
182 tx_info->tx_descs = nb_hw_desc;
183 tx_info->last_jiffies = jiffies;
184 tx_info->print_once = 0;
186 ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
191 /* This is the XDP napi callback. XDP queues use a separate napi callback
194 static int ena_xdp_io_poll(struct napi_struct *napi, int budget)
196 struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
197 u32 xdp_work_done, xdp_budget;
198 struct ena_ring *xdp_ring;
199 int napi_comp_call = 0;
202 xdp_ring = ena_napi->xdp_ring;
206 if (!test_bit(ENA_FLAG_DEV_UP, &xdp_ring->adapter->flags) ||
207 test_bit(ENA_FLAG_TRIGGER_RESET, &xdp_ring->adapter->flags)) {
208 napi_complete_done(napi, 0);
212 xdp_work_done = ena_clean_xdp_irq(xdp_ring, xdp_budget);
214 /* If the device is about to reset or down, avoid unmask
215 * the interrupt and return 0 so NAPI won't reschedule
217 if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &xdp_ring->adapter->flags))) {
218 napi_complete_done(napi, 0);
220 } else if (xdp_budget > xdp_work_done) {
222 if (napi_complete_done(napi, xdp_work_done))
223 ena_unmask_interrupt(xdp_ring, NULL);
224 ena_update_ring_numa_node(xdp_ring, NULL);
230 u64_stats_update_begin(&xdp_ring->syncp);
231 xdp_ring->tx_stats.napi_comp += napi_comp_call;
232 xdp_ring->tx_stats.tx_poll++;
233 u64_stats_update_end(&xdp_ring->syncp);
234 xdp_ring->tx_stats.last_napi_jiffies = jiffies;
239 static int ena_xdp_tx_map_frame(struct ena_ring *xdp_ring,
240 struct ena_tx_buffer *tx_info,
241 struct xdp_frame *xdpf,
242 struct ena_com_tx_ctx *ena_tx_ctx)
244 struct ena_adapter *adapter = xdp_ring->adapter;
245 struct ena_com_buf *ena_buf;
251 tx_info->xdpf = xdpf;
252 data = tx_info->xdpf->data;
253 size = tx_info->xdpf->len;
255 if (xdp_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
256 /* Designate part of the packet for LLQ */
257 push_len = min_t(u32, size, xdp_ring->tx_max_header_size);
259 ena_tx_ctx->push_header = data;
265 ena_tx_ctx->header_len = push_len;
268 dma = dma_map_single(xdp_ring->dev,
272 if (unlikely(dma_mapping_error(xdp_ring->dev, dma)))
273 goto error_report_dma_error;
275 tx_info->map_linear_data = 0;
277 ena_buf = tx_info->bufs;
278 ena_buf->paddr = dma;
281 ena_tx_ctx->ena_bufs = ena_buf;
282 ena_tx_ctx->num_bufs = tx_info->num_of_bufs = 1;
287 error_report_dma_error:
288 ena_increase_stat(&xdp_ring->tx_stats.dma_mapping_err, 1,
290 netif_warn(adapter, tx_queued, adapter->netdev, "Failed to map xdp buff\n");
295 static int ena_xdp_xmit_frame(struct ena_ring *xdp_ring,
296 struct net_device *dev,
297 struct xdp_frame *xdpf,
300 struct ena_com_tx_ctx ena_tx_ctx = {};
301 struct ena_tx_buffer *tx_info;
302 u16 next_to_use, req_id;
305 next_to_use = xdp_ring->next_to_use;
306 req_id = xdp_ring->free_ids[next_to_use];
307 tx_info = &xdp_ring->tx_buffer_info[req_id];
308 tx_info->num_of_bufs = 0;
310 rc = ena_xdp_tx_map_frame(xdp_ring, tx_info, xdpf, &ena_tx_ctx);
314 ena_tx_ctx.req_id = req_id;
316 rc = ena_xmit_common(dev,
323 goto error_unmap_dma;
325 /* trigger the dma engine. ena_ring_tx_doorbell()
326 * calls a memory barrier inside it.
328 if (flags & XDP_XMIT_FLUSH)
329 ena_ring_tx_doorbell(xdp_ring);
334 ena_unmap_tx_buff(xdp_ring, tx_info);
335 tx_info->xdpf = NULL;
339 static int ena_xdp_xmit(struct net_device *dev, int n,
340 struct xdp_frame **frames, u32 flags)
342 struct ena_adapter *adapter = netdev_priv(dev);
343 struct ena_ring *xdp_ring;
344 int qid, i, nxmit = 0;
346 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
349 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
352 /* We assume that all rings have the same XDP program */
353 if (!READ_ONCE(adapter->rx_ring->xdp_bpf_prog))
356 qid = smp_processor_id() % adapter->xdp_num_queues;
357 qid += adapter->xdp_first_ring;
358 xdp_ring = &adapter->tx_ring[qid];
360 /* Other CPU ids might try to send thorugh this queue */
361 spin_lock(&xdp_ring->xdp_tx_lock);
363 for (i = 0; i < n; i++) {
364 if (ena_xdp_xmit_frame(xdp_ring, dev, frames[i], 0))
369 /* Ring doorbell to make device aware of the packets */
370 if (flags & XDP_XMIT_FLUSH)
371 ena_ring_tx_doorbell(xdp_ring);
373 spin_unlock(&xdp_ring->xdp_tx_lock);
375 /* Return number of packets sent */
379 static int ena_xdp_execute(struct ena_ring *rx_ring, struct xdp_buff *xdp)
381 struct bpf_prog *xdp_prog;
382 struct ena_ring *xdp_ring;
383 u32 verdict = XDP_PASS;
384 struct xdp_frame *xdpf;
387 xdp_prog = READ_ONCE(rx_ring->xdp_bpf_prog);
392 verdict = bpf_prog_run_xdp(xdp_prog, xdp);
396 xdpf = xdp_convert_buff_to_frame(xdp);
397 if (unlikely(!xdpf)) {
398 trace_xdp_exception(rx_ring->netdev, xdp_prog, verdict);
399 xdp_stat = &rx_ring->rx_stats.xdp_aborted;
400 verdict = XDP_ABORTED;
404 /* Find xmit queue */
405 xdp_ring = rx_ring->xdp_ring;
407 /* The XDP queues are shared between XDP_TX and XDP_REDIRECT */
408 spin_lock(&xdp_ring->xdp_tx_lock);
410 if (ena_xdp_xmit_frame(xdp_ring, rx_ring->netdev, xdpf,
412 xdp_return_frame(xdpf);
414 spin_unlock(&xdp_ring->xdp_tx_lock);
415 xdp_stat = &rx_ring->rx_stats.xdp_tx;
418 if (likely(!xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog))) {
419 xdp_stat = &rx_ring->rx_stats.xdp_redirect;
422 trace_xdp_exception(rx_ring->netdev, xdp_prog, verdict);
423 xdp_stat = &rx_ring->rx_stats.xdp_aborted;
424 verdict = XDP_ABORTED;
427 trace_xdp_exception(rx_ring->netdev, xdp_prog, verdict);
428 xdp_stat = &rx_ring->rx_stats.xdp_aborted;
431 xdp_stat = &rx_ring->rx_stats.xdp_drop;
434 xdp_stat = &rx_ring->rx_stats.xdp_pass;
437 bpf_warn_invalid_xdp_action(verdict);
438 xdp_stat = &rx_ring->rx_stats.xdp_invalid;
441 ena_increase_stat(xdp_stat, 1, &rx_ring->syncp);
446 static void ena_init_all_xdp_queues(struct ena_adapter *adapter)
448 adapter->xdp_first_ring = adapter->num_io_queues;
449 adapter->xdp_num_queues = adapter->num_io_queues;
451 ena_init_io_rings(adapter,
452 adapter->xdp_first_ring,
453 adapter->xdp_num_queues);
456 static int ena_setup_and_create_all_xdp_queues(struct ena_adapter *adapter)
460 rc = ena_setup_tx_resources_in_range(adapter, adapter->xdp_first_ring,
461 adapter->xdp_num_queues);
465 rc = ena_create_io_tx_queues_in_range(adapter,
466 adapter->xdp_first_ring,
467 adapter->xdp_num_queues);
474 ena_free_all_io_tx_resources(adapter);
479 /* Provides a way for both kernel and bpf-prog to know
480 * more about the RX-queue a given XDP frame arrived on.
482 static int ena_xdp_register_rxq_info(struct ena_ring *rx_ring)
486 rc = xdp_rxq_info_reg(&rx_ring->xdp_rxq, rx_ring->netdev, rx_ring->qid, 0);
489 netif_err(rx_ring->adapter, ifup, rx_ring->netdev,
490 "Failed to register xdp rx queue info. RX queue num %d rc: %d\n",
495 rc = xdp_rxq_info_reg_mem_model(&rx_ring->xdp_rxq, MEM_TYPE_PAGE_SHARED,
499 netif_err(rx_ring->adapter, ifup, rx_ring->netdev,
500 "Failed to register xdp rx queue info memory model. RX queue num %d rc: %d\n",
502 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
509 static void ena_xdp_unregister_rxq_info(struct ena_ring *rx_ring)
511 xdp_rxq_info_unreg_mem_model(&rx_ring->xdp_rxq);
512 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
515 static void ena_xdp_exchange_program_rx_in_range(struct ena_adapter *adapter,
516 struct bpf_prog *prog,
517 int first, int count)
519 struct ena_ring *rx_ring;
522 for (i = first; i < count; i++) {
523 rx_ring = &adapter->rx_ring[i];
524 xchg(&rx_ring->xdp_bpf_prog, prog);
526 ena_xdp_register_rxq_info(rx_ring);
527 rx_ring->rx_headroom = XDP_PACKET_HEADROOM;
529 ena_xdp_unregister_rxq_info(rx_ring);
530 rx_ring->rx_headroom = NET_SKB_PAD;
535 static void ena_xdp_exchange_program(struct ena_adapter *adapter,
536 struct bpf_prog *prog)
538 struct bpf_prog *old_bpf_prog = xchg(&adapter->xdp_bpf_prog, prog);
540 ena_xdp_exchange_program_rx_in_range(adapter,
543 adapter->num_io_queues);
546 bpf_prog_put(old_bpf_prog);
549 static int ena_destroy_and_free_all_xdp_queues(struct ena_adapter *adapter)
554 was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
559 adapter->xdp_first_ring = 0;
560 adapter->xdp_num_queues = 0;
561 ena_xdp_exchange_program(adapter, NULL);
563 rc = ena_up(adapter);
570 static int ena_xdp_set(struct net_device *netdev, struct netdev_bpf *bpf)
572 struct ena_adapter *adapter = netdev_priv(netdev);
573 struct bpf_prog *prog = bpf->prog;
574 struct bpf_prog *old_bpf_prog;
578 is_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
579 rc = ena_xdp_allowed(adapter);
580 if (rc == ENA_XDP_ALLOWED) {
581 old_bpf_prog = adapter->xdp_bpf_prog;
584 ena_init_all_xdp_queues(adapter);
585 } else if (!old_bpf_prog) {
587 ena_init_all_xdp_queues(adapter);
589 ena_xdp_exchange_program(adapter, prog);
591 if (is_up && !old_bpf_prog) {
592 rc = ena_up(adapter);
596 } else if (old_bpf_prog) {
597 rc = ena_destroy_and_free_all_xdp_queues(adapter);
602 prev_mtu = netdev->max_mtu;
603 netdev->max_mtu = prog ? ENA_XDP_MAX_MTU : adapter->max_mtu;
606 netif_info(adapter, drv, adapter->netdev,
607 "XDP program is set, changing the max_mtu from %d to %d",
608 prev_mtu, netdev->max_mtu);
610 } else if (rc == ENA_XDP_CURRENT_MTU_TOO_LARGE) {
611 netif_err(adapter, drv, adapter->netdev,
612 "Failed to set xdp program, the current MTU (%d) is larger than the maximum allowed MTU (%lu) while xdp is on",
613 netdev->mtu, ENA_XDP_MAX_MTU);
614 NL_SET_ERR_MSG_MOD(bpf->extack,
615 "Failed to set xdp program, the current MTU is larger than the maximum allowed MTU. Check the dmesg for more info");
617 } else if (rc == ENA_XDP_NO_ENOUGH_QUEUES) {
618 netif_err(adapter, drv, adapter->netdev,
619 "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",
620 adapter->num_io_queues, adapter->max_num_io_queues);
621 NL_SET_ERR_MSG_MOD(bpf->extack,
622 "Failed to set xdp program, there is no enough space for allocating XDP queues, Check the dmesg for more info");
629 /* This is the main xdp callback, it's used by the kernel to set/unset the xdp
630 * program as well as to query the current xdp program id.
632 static int ena_xdp(struct net_device *netdev, struct netdev_bpf *bpf)
634 switch (bpf->command) {
636 return ena_xdp_set(netdev, bpf);
643 static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
645 #ifdef CONFIG_RFS_ACCEL
649 adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_io_queues);
650 if (!adapter->netdev->rx_cpu_rmap)
652 for (i = 0; i < adapter->num_io_queues; i++) {
653 int irq_idx = ENA_IO_IRQ_IDX(i);
655 rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
656 pci_irq_vector(adapter->pdev, irq_idx));
658 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
659 adapter->netdev->rx_cpu_rmap = NULL;
663 #endif /* CONFIG_RFS_ACCEL */
667 static void ena_init_io_rings_common(struct ena_adapter *adapter,
668 struct ena_ring *ring, u16 qid)
671 ring->pdev = adapter->pdev;
672 ring->dev = &adapter->pdev->dev;
673 ring->netdev = adapter->netdev;
674 ring->napi = &adapter->ena_napi[qid].napi;
675 ring->adapter = adapter;
676 ring->ena_dev = adapter->ena_dev;
677 ring->per_napi_packets = 0;
679 ring->no_interrupt_event_cnt = 0;
680 u64_stats_init(&ring->syncp);
683 static void ena_init_io_rings(struct ena_adapter *adapter,
684 int first_index, int count)
686 struct ena_com_dev *ena_dev;
687 struct ena_ring *txr, *rxr;
690 ena_dev = adapter->ena_dev;
692 for (i = first_index; i < first_index + count; i++) {
693 txr = &adapter->tx_ring[i];
694 rxr = &adapter->rx_ring[i];
696 /* TX common ring state */
697 ena_init_io_rings_common(adapter, txr, i);
699 /* TX specific ring state */
700 txr->ring_size = adapter->requested_tx_ring_size;
701 txr->tx_max_header_size = ena_dev->tx_max_header_size;
702 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
703 txr->sgl_size = adapter->max_tx_sgl_size;
704 txr->smoothed_interval =
705 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
706 txr->disable_meta_caching = adapter->disable_meta_caching;
707 spin_lock_init(&txr->xdp_tx_lock);
709 /* Don't init RX queues for xdp queues */
710 if (!ENA_IS_XDP_INDEX(adapter, i)) {
711 /* RX common ring state */
712 ena_init_io_rings_common(adapter, rxr, i);
714 /* RX specific ring state */
715 rxr->ring_size = adapter->requested_rx_ring_size;
716 rxr->rx_copybreak = adapter->rx_copybreak;
717 rxr->sgl_size = adapter->max_rx_sgl_size;
718 rxr->smoothed_interval =
719 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
720 rxr->empty_rx_queue = 0;
721 rxr->rx_headroom = NET_SKB_PAD;
722 adapter->ena_napi[i].dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
723 rxr->xdp_ring = &adapter->tx_ring[i + adapter->num_io_queues];
728 /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
729 * @adapter: network interface device structure
732 * Return 0 on success, negative on failure
734 static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
736 struct ena_ring *tx_ring = &adapter->tx_ring[qid];
737 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
740 if (tx_ring->tx_buffer_info) {
741 netif_err(adapter, ifup,
742 adapter->netdev, "tx_buffer_info info is not NULL");
746 size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
747 node = cpu_to_node(ena_irq->cpu);
749 tx_ring->tx_buffer_info = vzalloc_node(size, node);
750 if (!tx_ring->tx_buffer_info) {
751 tx_ring->tx_buffer_info = vzalloc(size);
752 if (!tx_ring->tx_buffer_info)
753 goto err_tx_buffer_info;
756 size = sizeof(u16) * tx_ring->ring_size;
757 tx_ring->free_ids = vzalloc_node(size, node);
758 if (!tx_ring->free_ids) {
759 tx_ring->free_ids = vzalloc(size);
760 if (!tx_ring->free_ids)
761 goto err_tx_free_ids;
764 size = tx_ring->tx_max_header_size;
765 tx_ring->push_buf_intermediate_buf = vzalloc_node(size, node);
766 if (!tx_ring->push_buf_intermediate_buf) {
767 tx_ring->push_buf_intermediate_buf = vzalloc(size);
768 if (!tx_ring->push_buf_intermediate_buf)
769 goto err_push_buf_intermediate_buf;
772 /* Req id ring for TX out of order completions */
773 for (i = 0; i < tx_ring->ring_size; i++)
774 tx_ring->free_ids[i] = i;
776 /* Reset tx statistics */
777 memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
779 tx_ring->next_to_use = 0;
780 tx_ring->next_to_clean = 0;
781 tx_ring->cpu = ena_irq->cpu;
784 err_push_buf_intermediate_buf:
785 vfree(tx_ring->free_ids);
786 tx_ring->free_ids = NULL;
788 vfree(tx_ring->tx_buffer_info);
789 tx_ring->tx_buffer_info = NULL;
794 /* ena_free_tx_resources - Free I/O Tx Resources per Queue
795 * @adapter: network interface device structure
798 * Free all transmit software resources
800 static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
802 struct ena_ring *tx_ring = &adapter->tx_ring[qid];
804 vfree(tx_ring->tx_buffer_info);
805 tx_ring->tx_buffer_info = NULL;
807 vfree(tx_ring->free_ids);
808 tx_ring->free_ids = NULL;
810 vfree(tx_ring->push_buf_intermediate_buf);
811 tx_ring->push_buf_intermediate_buf = NULL;
814 static int ena_setup_tx_resources_in_range(struct ena_adapter *adapter,
820 for (i = first_index; i < first_index + count; i++) {
821 rc = ena_setup_tx_resources(adapter, i);
830 netif_err(adapter, ifup, adapter->netdev,
831 "Tx queue %d: allocation failed\n", i);
833 /* rewind the index freeing the rings as we go */
834 while (first_index < i--)
835 ena_free_tx_resources(adapter, i);
839 static void ena_free_all_io_tx_resources_in_range(struct ena_adapter *adapter,
840 int first_index, int count)
844 for (i = first_index; i < first_index + count; i++)
845 ena_free_tx_resources(adapter, i);
848 /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
849 * @adapter: board private structure
851 * Free all transmit software resources
853 static void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
855 ena_free_all_io_tx_resources_in_range(adapter,
857 adapter->xdp_num_queues +
858 adapter->num_io_queues);
861 /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
862 * @adapter: network interface device structure
865 * Returns 0 on success, negative on failure
867 static int ena_setup_rx_resources(struct ena_adapter *adapter,
870 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
871 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
874 if (rx_ring->rx_buffer_info) {
875 netif_err(adapter, ifup, adapter->netdev,
876 "rx_buffer_info is not NULL");
880 /* alloc extra element so in rx path
881 * we can always prefetch rx_info + 1
883 size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
884 node = cpu_to_node(ena_irq->cpu);
886 rx_ring->rx_buffer_info = vzalloc_node(size, node);
887 if (!rx_ring->rx_buffer_info) {
888 rx_ring->rx_buffer_info = vzalloc(size);
889 if (!rx_ring->rx_buffer_info)
893 size = sizeof(u16) * rx_ring->ring_size;
894 rx_ring->free_ids = vzalloc_node(size, node);
895 if (!rx_ring->free_ids) {
896 rx_ring->free_ids = vzalloc(size);
897 if (!rx_ring->free_ids) {
898 vfree(rx_ring->rx_buffer_info);
899 rx_ring->rx_buffer_info = NULL;
904 /* Req id ring for receiving RX pkts out of order */
905 for (i = 0; i < rx_ring->ring_size; i++)
906 rx_ring->free_ids[i] = i;
908 /* Reset rx statistics */
909 memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
911 rx_ring->next_to_clean = 0;
912 rx_ring->next_to_use = 0;
913 rx_ring->cpu = ena_irq->cpu;
918 /* ena_free_rx_resources - Free I/O Rx Resources
919 * @adapter: network interface device structure
922 * Free all receive software resources
924 static void ena_free_rx_resources(struct ena_adapter *adapter,
927 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
929 vfree(rx_ring->rx_buffer_info);
930 rx_ring->rx_buffer_info = NULL;
932 vfree(rx_ring->free_ids);
933 rx_ring->free_ids = NULL;
936 /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
937 * @adapter: board private structure
939 * Return 0 on success, negative on failure
941 static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
945 for (i = 0; i < adapter->num_io_queues; i++) {
946 rc = ena_setup_rx_resources(adapter, i);
955 netif_err(adapter, ifup, adapter->netdev,
956 "Rx queue %d: allocation failed\n", i);
958 /* rewind the index freeing the rings as we go */
960 ena_free_rx_resources(adapter, i);
964 /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
965 * @adapter: board private structure
967 * Free all receive software resources
969 static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
973 for (i = 0; i < adapter->num_io_queues; i++)
974 ena_free_rx_resources(adapter, i);
977 static struct page *ena_alloc_map_page(struct ena_ring *rx_ring,
982 /* This would allocate the page on the same NUMA node the executing code
985 page = dev_alloc_page();
987 ena_increase_stat(&rx_ring->rx_stats.page_alloc_fail, 1,
989 return ERR_PTR(-ENOSPC);
992 /* To enable NIC-side port-mirroring, AKA SPAN port,
993 * we make the buffer readable from the nic as well
995 *dma = dma_map_page(rx_ring->dev, page, 0, ENA_PAGE_SIZE,
997 if (unlikely(dma_mapping_error(rx_ring->dev, *dma))) {
998 ena_increase_stat(&rx_ring->rx_stats.dma_mapping_err, 1,
1001 return ERR_PTR(-EIO);
1007 static int ena_alloc_rx_buffer(struct ena_ring *rx_ring,
1008 struct ena_rx_buffer *rx_info)
1010 int headroom = rx_ring->rx_headroom;
1011 struct ena_com_buf *ena_buf;
1016 /* restore page offset value in case it has been changed by device */
1017 rx_info->page_offset = headroom;
1019 /* if previous allocated page is not used */
1020 if (unlikely(rx_info->page))
1023 /* We handle DMA here */
1024 page = ena_alloc_map_page(rx_ring, &dma);
1025 if (unlikely(IS_ERR(page)))
1026 return PTR_ERR(page);
1028 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1029 "Allocate page %p, rx_info %p\n", page, rx_info);
1031 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1033 rx_info->page = page;
1034 ena_buf = &rx_info->ena_buf;
1035 ena_buf->paddr = dma + headroom;
1036 ena_buf->len = ENA_PAGE_SIZE - headroom - tailroom;
1041 static void ena_unmap_rx_buff(struct ena_ring *rx_ring,
1042 struct ena_rx_buffer *rx_info)
1044 struct ena_com_buf *ena_buf = &rx_info->ena_buf;
1046 dma_unmap_page(rx_ring->dev, ena_buf->paddr - rx_ring->rx_headroom,
1051 static void ena_free_rx_page(struct ena_ring *rx_ring,
1052 struct ena_rx_buffer *rx_info)
1054 struct page *page = rx_info->page;
1056 if (unlikely(!page)) {
1057 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
1058 "Trying to free unallocated buffer\n");
1062 ena_unmap_rx_buff(rx_ring, rx_info);
1065 rx_info->page = NULL;
1068 static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
1070 u16 next_to_use, req_id;
1074 next_to_use = rx_ring->next_to_use;
1076 for (i = 0; i < num; i++) {
1077 struct ena_rx_buffer *rx_info;
1079 req_id = rx_ring->free_ids[next_to_use];
1081 rx_info = &rx_ring->rx_buffer_info[req_id];
1083 rc = ena_alloc_rx_buffer(rx_ring, rx_info);
1084 if (unlikely(rc < 0)) {
1085 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
1086 "Failed to allocate buffer for rx queue %d\n",
1090 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
1094 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
1095 "Failed to add buffer for rx queue %d\n",
1099 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
1100 rx_ring->ring_size);
1103 if (unlikely(i < num)) {
1104 ena_increase_stat(&rx_ring->rx_stats.refil_partial, 1,
1106 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
1107 "Refilled rx qid %d with only %d buffers (from %d)\n",
1108 rx_ring->qid, i, num);
1111 /* ena_com_write_sq_doorbell issues a wmb() */
1113 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
1115 rx_ring->next_to_use = next_to_use;
1120 static void ena_free_rx_bufs(struct ena_adapter *adapter,
1123 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
1126 for (i = 0; i < rx_ring->ring_size; i++) {
1127 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
1130 ena_free_rx_page(rx_ring, rx_info);
1134 /* ena_refill_all_rx_bufs - allocate all queues Rx buffers
1135 * @adapter: board private structure
1137 static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
1139 struct ena_ring *rx_ring;
1140 int i, rc, bufs_num;
1142 for (i = 0; i < adapter->num_io_queues; i++) {
1143 rx_ring = &adapter->rx_ring[i];
1144 bufs_num = rx_ring->ring_size - 1;
1145 rc = ena_refill_rx_bufs(rx_ring, bufs_num);
1147 if (unlikely(rc != bufs_num))
1148 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
1149 "Refilling Queue %d failed. allocated %d buffers from: %d\n",
1154 static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
1158 for (i = 0; i < adapter->num_io_queues; i++)
1159 ena_free_rx_bufs(adapter, i);
1162 static void ena_unmap_tx_buff(struct ena_ring *tx_ring,
1163 struct ena_tx_buffer *tx_info)
1165 struct ena_com_buf *ena_buf;
1169 ena_buf = tx_info->bufs;
1170 cnt = tx_info->num_of_bufs;
1175 if (tx_info->map_linear_data) {
1176 dma_unmap_single(tx_ring->dev,
1177 dma_unmap_addr(ena_buf, paddr),
1178 dma_unmap_len(ena_buf, len),
1184 /* unmap remaining mapped pages */
1185 for (i = 0; i < cnt; i++) {
1186 dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
1187 dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
1192 /* ena_free_tx_bufs - Free Tx Buffers per Queue
1193 * @tx_ring: TX ring for which buffers be freed
1195 static void ena_free_tx_bufs(struct ena_ring *tx_ring)
1197 bool print_once = true;
1200 for (i = 0; i < tx_ring->ring_size; i++) {
1201 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
1207 netif_notice(tx_ring->adapter, ifdown, tx_ring->netdev,
1208 "Free uncompleted tx skb qid %d idx 0x%x\n",
1212 netif_dbg(tx_ring->adapter, ifdown, tx_ring->netdev,
1213 "Free uncompleted tx skb qid %d idx 0x%x\n",
1217 ena_unmap_tx_buff(tx_ring, tx_info);
1219 dev_kfree_skb_any(tx_info->skb);
1221 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
1225 static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
1227 struct ena_ring *tx_ring;
1230 for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
1231 tx_ring = &adapter->tx_ring[i];
1232 ena_free_tx_bufs(tx_ring);
1236 static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
1241 for (i = 0; i < adapter->num_io_queues + adapter->xdp_num_queues; i++) {
1242 ena_qid = ENA_IO_TXQ_IDX(i);
1243 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
1247 static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
1252 for (i = 0; i < adapter->num_io_queues; i++) {
1253 ena_qid = ENA_IO_RXQ_IDX(i);
1254 cancel_work_sync(&adapter->ena_napi[i].dim.work);
1255 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
1259 static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
1261 ena_destroy_all_tx_queues(adapter);
1262 ena_destroy_all_rx_queues(adapter);
1265 static int handle_invalid_req_id(struct ena_ring *ring, u16 req_id,
1266 struct ena_tx_buffer *tx_info, bool is_xdp)
1269 netif_err(ring->adapter,
1272 "tx_info doesn't have valid %s",
1273 is_xdp ? "xdp frame" : "skb");
1275 netif_err(ring->adapter,
1278 "Invalid req_id: %hu\n",
1281 ena_increase_stat(&ring->tx_stats.bad_req_id, 1, &ring->syncp);
1283 /* Trigger device reset */
1284 ring->adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
1285 set_bit(ENA_FLAG_TRIGGER_RESET, &ring->adapter->flags);
1289 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
1291 struct ena_tx_buffer *tx_info = NULL;
1293 if (likely(req_id < tx_ring->ring_size)) {
1294 tx_info = &tx_ring->tx_buffer_info[req_id];
1295 if (likely(tx_info->skb))
1299 return handle_invalid_req_id(tx_ring, req_id, tx_info, false);
1302 static int validate_xdp_req_id(struct ena_ring *xdp_ring, u16 req_id)
1304 struct ena_tx_buffer *tx_info = NULL;
1306 if (likely(req_id < xdp_ring->ring_size)) {
1307 tx_info = &xdp_ring->tx_buffer_info[req_id];
1308 if (likely(tx_info->xdpf))
1312 return handle_invalid_req_id(xdp_ring, req_id, tx_info, true);
1315 static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
1317 struct netdev_queue *txq;
1326 next_to_clean = tx_ring->next_to_clean;
1327 txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
1329 while (tx_pkts < budget) {
1330 struct ena_tx_buffer *tx_info;
1331 struct sk_buff *skb;
1333 rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
1338 rc = validate_tx_req_id(tx_ring, req_id);
1342 tx_info = &tx_ring->tx_buffer_info[req_id];
1345 /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
1346 prefetch(&skb->end);
1348 tx_info->skb = NULL;
1349 tx_info->last_jiffies = 0;
1351 ena_unmap_tx_buff(tx_ring, tx_info);
1353 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
1354 "tx_poll: q %d skb %p completed\n", tx_ring->qid,
1357 tx_bytes += skb->len;
1360 total_done += tx_info->tx_descs;
1362 tx_ring->free_ids[next_to_clean] = req_id;
1363 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
1364 tx_ring->ring_size);
1367 tx_ring->next_to_clean = next_to_clean;
1368 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
1369 ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
1371 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
1373 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
1374 "tx_poll: q %d done. total pkts: %d\n",
1375 tx_ring->qid, tx_pkts);
1377 /* need to make the rings circular update visible to
1378 * ena_start_xmit() before checking for netif_queue_stopped().
1382 above_thresh = ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
1383 ENA_TX_WAKEUP_THRESH);
1384 if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
1385 __netif_tx_lock(txq, smp_processor_id());
1387 ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
1388 ENA_TX_WAKEUP_THRESH);
1389 if (netif_tx_queue_stopped(txq) && above_thresh &&
1390 test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags)) {
1391 netif_tx_wake_queue(txq);
1392 ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1,
1395 __netif_tx_unlock(txq);
1401 static struct sk_buff *ena_alloc_skb(struct ena_ring *rx_ring, void *first_frag)
1403 struct sk_buff *skb;
1406 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
1407 rx_ring->rx_copybreak);
1409 skb = build_skb(first_frag, ENA_PAGE_SIZE);
1411 if (unlikely(!skb)) {
1412 ena_increase_stat(&rx_ring->rx_stats.skb_alloc_fail, 1,
1415 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
1416 "Failed to allocate skb. first_frag %s\n",
1417 first_frag ? "provided" : "not provided");
1424 static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
1425 struct ena_com_rx_buf_info *ena_bufs,
1429 struct ena_rx_buffer *rx_info;
1430 u16 len, req_id, buf = 0;
1431 struct sk_buff *skb;
1436 len = ena_bufs[buf].len;
1437 req_id = ena_bufs[buf].req_id;
1439 rx_info = &rx_ring->rx_buffer_info[req_id];
1441 if (unlikely(!rx_info->page)) {
1442 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
1447 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1448 "rx_info %p page %p\n",
1449 rx_info, rx_info->page);
1451 /* save virt address of first buffer */
1452 page_addr = page_address(rx_info->page);
1453 page_offset = rx_info->page_offset;
1454 data_addr = page_addr + page_offset;
1456 prefetch(data_addr);
1458 if (len <= rx_ring->rx_copybreak) {
1459 skb = ena_alloc_skb(rx_ring, NULL);
1463 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1464 "RX allocated small packet. len %d. data_len %d\n",
1465 skb->len, skb->data_len);
1467 /* sync this buffer for CPU use */
1468 dma_sync_single_for_cpu(rx_ring->dev,
1469 dma_unmap_addr(&rx_info->ena_buf, paddr),
1472 skb_copy_to_linear_data(skb, data_addr, len);
1473 dma_sync_single_for_device(rx_ring->dev,
1474 dma_unmap_addr(&rx_info->ena_buf, paddr),
1479 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1480 rx_ring->free_ids[*next_to_clean] = req_id;
1481 *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
1482 rx_ring->ring_size);
1486 ena_unmap_rx_buff(rx_ring, rx_info);
1488 skb = ena_alloc_skb(rx_ring, page_addr);
1492 /* Populate skb's linear part */
1493 skb_reserve(skb, page_offset);
1495 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1498 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1499 "RX skb updated. len %d. data_len %d\n",
1500 skb->len, skb->data_len);
1502 rx_info->page = NULL;
1504 rx_ring->free_ids[*next_to_clean] = req_id;
1506 ENA_RX_RING_IDX_NEXT(*next_to_clean,
1507 rx_ring->ring_size);
1508 if (likely(--descs == 0))
1512 len = ena_bufs[buf].len;
1513 req_id = ena_bufs[buf].req_id;
1515 rx_info = &rx_ring->rx_buffer_info[req_id];
1517 ena_unmap_rx_buff(rx_ring, rx_info);
1519 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
1520 rx_info->page_offset, len, ENA_PAGE_SIZE);
1527 /* ena_rx_checksum - indicate in skb if hw indicated a good cksum
1528 * @adapter: structure containing adapter specific data
1529 * @ena_rx_ctx: received packet context/metadata
1530 * @skb: skb currently being received and modified
1532 static void ena_rx_checksum(struct ena_ring *rx_ring,
1533 struct ena_com_rx_ctx *ena_rx_ctx,
1534 struct sk_buff *skb)
1536 /* Rx csum disabled */
1537 if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
1538 skb->ip_summed = CHECKSUM_NONE;
1542 /* For fragmented packets the checksum isn't valid */
1543 if (ena_rx_ctx->frag) {
1544 skb->ip_summed = CHECKSUM_NONE;
1548 /* if IP and error */
1549 if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
1550 (ena_rx_ctx->l3_csum_err))) {
1551 /* ipv4 checksum error */
1552 skb->ip_summed = CHECKSUM_NONE;
1553 ena_increase_stat(&rx_ring->rx_stats.bad_csum, 1,
1555 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
1556 "RX IPv4 header checksum error\n");
1561 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1562 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
1563 if (unlikely(ena_rx_ctx->l4_csum_err)) {
1564 /* TCP/UDP checksum error */
1565 ena_increase_stat(&rx_ring->rx_stats.bad_csum, 1,
1567 netif_dbg(rx_ring->adapter, rx_err, rx_ring->netdev,
1568 "RX L4 checksum error\n");
1569 skb->ip_summed = CHECKSUM_NONE;
1573 if (likely(ena_rx_ctx->l4_csum_checked)) {
1574 skb->ip_summed = CHECKSUM_UNNECESSARY;
1575 ena_increase_stat(&rx_ring->rx_stats.csum_good, 1,
1578 ena_increase_stat(&rx_ring->rx_stats.csum_unchecked, 1,
1580 skb->ip_summed = CHECKSUM_NONE;
1583 skb->ip_summed = CHECKSUM_NONE;
1589 static void ena_set_rx_hash(struct ena_ring *rx_ring,
1590 struct ena_com_rx_ctx *ena_rx_ctx,
1591 struct sk_buff *skb)
1593 enum pkt_hash_types hash_type;
1595 if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
1596 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
1597 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
1599 hash_type = PKT_HASH_TYPE_L4;
1601 hash_type = PKT_HASH_TYPE_NONE;
1603 /* Override hash type if the packet is fragmented */
1604 if (ena_rx_ctx->frag)
1605 hash_type = PKT_HASH_TYPE_NONE;
1607 skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
1611 static int ena_xdp_handle_buff(struct ena_ring *rx_ring, struct xdp_buff *xdp)
1613 struct ena_rx_buffer *rx_info;
1616 rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id];
1617 xdp_prepare_buff(xdp, page_address(rx_info->page),
1618 rx_info->page_offset,
1619 rx_ring->ena_bufs[0].len, false);
1620 /* If for some reason we received a bigger packet than
1621 * we expect, then we simply drop it
1623 if (unlikely(rx_ring->ena_bufs[0].len > ENA_XDP_MAX_MTU))
1626 ret = ena_xdp_execute(rx_ring, xdp);
1628 /* The xdp program might expand the headers */
1629 if (ret == XDP_PASS) {
1630 rx_info->page_offset = xdp->data - xdp->data_hard_start;
1631 rx_ring->ena_bufs[0].len = xdp->data_end - xdp->data;
1636 /* ena_clean_rx_irq - Cleanup RX irq
1637 * @rx_ring: RX ring to clean
1638 * @napi: napi handler
1639 * @budget: how many packets driver is allowed to clean
1641 * Returns the number of cleaned buffers.
1643 static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
1646 u16 next_to_clean = rx_ring->next_to_clean;
1647 struct ena_com_rx_ctx ena_rx_ctx;
1648 struct ena_rx_buffer *rx_info;
1649 struct ena_adapter *adapter;
1650 u32 res_budget, work_done;
1651 int rx_copybreak_pkt = 0;
1652 int refill_threshold;
1653 struct sk_buff *skb;
1654 int refill_required;
1655 struct xdp_buff xdp;
1662 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1663 "%s qid %d\n", __func__, rx_ring->qid);
1664 res_budget = budget;
1665 xdp_init_buff(&xdp, ENA_PAGE_SIZE, &rx_ring->xdp_rxq);
1668 xdp_verdict = XDP_PASS;
1670 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
1671 ena_rx_ctx.max_bufs = rx_ring->sgl_size;
1672 ena_rx_ctx.descs = 0;
1673 ena_rx_ctx.pkt_offset = 0;
1674 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
1675 rx_ring->ena_com_io_sq,
1680 if (unlikely(ena_rx_ctx.descs == 0))
1683 /* First descriptor might have an offset set by the device */
1684 rx_info = &rx_ring->rx_buffer_info[rx_ring->ena_bufs[0].req_id];
1685 rx_info->page_offset += ena_rx_ctx.pkt_offset;
1687 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
1688 "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
1689 rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
1690 ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
1692 if (ena_xdp_present_ring(rx_ring))
1693 xdp_verdict = ena_xdp_handle_buff(rx_ring, &xdp);
1695 /* allocate skb and fill it */
1696 if (xdp_verdict == XDP_PASS)
1697 skb = ena_rx_skb(rx_ring,
1702 if (unlikely(!skb)) {
1703 for (i = 0; i < ena_rx_ctx.descs; i++) {
1704 int req_id = rx_ring->ena_bufs[i].req_id;
1706 rx_ring->free_ids[next_to_clean] = req_id;
1708 ENA_RX_RING_IDX_NEXT(next_to_clean,
1709 rx_ring->ring_size);
1711 /* Packets was passed for transmission, unmap it
1714 if (xdp_verdict == XDP_TX || xdp_verdict == XDP_REDIRECT) {
1715 ena_unmap_rx_buff(rx_ring,
1716 &rx_ring->rx_buffer_info[req_id]);
1717 rx_ring->rx_buffer_info[req_id].page = NULL;
1720 if (xdp_verdict != XDP_PASS) {
1721 xdp_flags |= xdp_verdict;
1728 ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
1730 ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
1732 skb_record_rx_queue(skb, rx_ring->qid);
1734 if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak)
1737 total_len += skb->len;
1739 napi_gro_receive(napi, skb);
1742 } while (likely(res_budget));
1744 work_done = budget - res_budget;
1745 rx_ring->per_napi_packets += work_done;
1746 u64_stats_update_begin(&rx_ring->syncp);
1747 rx_ring->rx_stats.bytes += total_len;
1748 rx_ring->rx_stats.cnt += work_done;
1749 rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
1750 u64_stats_update_end(&rx_ring->syncp);
1752 rx_ring->next_to_clean = next_to_clean;
1754 refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
1756 min_t(int, rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER,
1757 ENA_RX_REFILL_THRESH_PACKET);
1759 /* Optimization, try to batch new rx buffers */
1760 if (refill_required > refill_threshold) {
1761 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
1762 ena_refill_rx_bufs(rx_ring, refill_required);
1765 if (xdp_flags & XDP_REDIRECT)
1771 adapter = netdev_priv(rx_ring->netdev);
1773 if (rc == -ENOSPC) {
1774 ena_increase_stat(&rx_ring->rx_stats.bad_desc_num, 1,
1776 adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
1778 ena_increase_stat(&rx_ring->rx_stats.bad_req_id, 1,
1780 adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
1783 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
1788 static void ena_dim_work(struct work_struct *w)
1790 struct dim *dim = container_of(w, struct dim, work);
1791 struct dim_cq_moder cur_moder =
1792 net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1793 struct ena_napi *ena_napi = container_of(dim, struct ena_napi, dim);
1795 ena_napi->rx_ring->smoothed_interval = cur_moder.usec;
1796 dim->state = DIM_START_MEASURE;
1799 static void ena_adjust_adaptive_rx_intr_moderation(struct ena_napi *ena_napi)
1801 struct dim_sample dim_sample;
1802 struct ena_ring *rx_ring = ena_napi->rx_ring;
1804 if (!rx_ring->per_napi_packets)
1807 rx_ring->non_empty_napi_events++;
1809 dim_update_sample(rx_ring->non_empty_napi_events,
1810 rx_ring->rx_stats.cnt,
1811 rx_ring->rx_stats.bytes,
1814 net_dim(&ena_napi->dim, dim_sample);
1816 rx_ring->per_napi_packets = 0;
1819 static void ena_unmask_interrupt(struct ena_ring *tx_ring,
1820 struct ena_ring *rx_ring)
1822 struct ena_eth_io_intr_reg intr_reg;
1823 u32 rx_interval = 0;
1824 /* Rx ring can be NULL when for XDP tx queues which don't have an
1825 * accompanying rx_ring pair.
1828 rx_interval = ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev) ?
1829 rx_ring->smoothed_interval :
1830 ena_com_get_nonadaptive_moderation_interval_rx(rx_ring->ena_dev);
1832 /* Update intr register: rx intr delay,
1833 * tx intr delay and interrupt unmask
1835 ena_com_update_intr_reg(&intr_reg,
1837 tx_ring->smoothed_interval,
1840 ena_increase_stat(&tx_ring->tx_stats.unmask_interrupt, 1,
1843 /* It is a shared MSI-X.
1844 * Tx and Rx CQ have pointer to it.
1845 * So we use one of them to reach the intr reg
1846 * The Tx ring is used because the rx_ring is NULL for XDP queues
1848 ena_com_unmask_intr(tx_ring->ena_com_io_cq, &intr_reg);
1851 static void ena_update_ring_numa_node(struct ena_ring *tx_ring,
1852 struct ena_ring *rx_ring)
1854 int cpu = get_cpu();
1857 /* Check only one ring since the 2 rings are running on the same cpu */
1858 if (likely(tx_ring->cpu == cpu))
1861 numa_node = cpu_to_node(cpu);
1864 if (numa_node != NUMA_NO_NODE) {
1865 ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
1867 ena_com_update_numa_node(rx_ring->ena_com_io_cq,
1880 static int ena_clean_xdp_irq(struct ena_ring *xdp_ring, u32 budget)
1889 if (unlikely(!xdp_ring))
1891 next_to_clean = xdp_ring->next_to_clean;
1893 while (tx_pkts < budget) {
1894 struct ena_tx_buffer *tx_info;
1895 struct xdp_frame *xdpf;
1897 rc = ena_com_tx_comp_req_id_get(xdp_ring->ena_com_io_cq,
1902 rc = validate_xdp_req_id(xdp_ring, req_id);
1906 tx_info = &xdp_ring->tx_buffer_info[req_id];
1907 xdpf = tx_info->xdpf;
1909 tx_info->xdpf = NULL;
1910 tx_info->last_jiffies = 0;
1911 ena_unmap_tx_buff(xdp_ring, tx_info);
1913 netif_dbg(xdp_ring->adapter, tx_done, xdp_ring->netdev,
1914 "tx_poll: q %d skb %p completed\n", xdp_ring->qid,
1917 tx_bytes += xdpf->len;
1919 total_done += tx_info->tx_descs;
1921 xdp_return_frame(xdpf);
1922 xdp_ring->free_ids[next_to_clean] = req_id;
1923 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
1924 xdp_ring->ring_size);
1927 xdp_ring->next_to_clean = next_to_clean;
1928 ena_com_comp_ack(xdp_ring->ena_com_io_sq, total_done);
1929 ena_com_update_dev_comp_head(xdp_ring->ena_com_io_cq);
1931 netif_dbg(xdp_ring->adapter, tx_done, xdp_ring->netdev,
1932 "tx_poll: q %d done. total pkts: %d\n",
1933 xdp_ring->qid, tx_pkts);
1938 static int ena_io_poll(struct napi_struct *napi, int budget)
1940 struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
1941 struct ena_ring *tx_ring, *rx_ring;
1943 int rx_work_done = 0;
1945 int napi_comp_call = 0;
1948 tx_ring = ena_napi->tx_ring;
1949 rx_ring = ena_napi->rx_ring;
1951 tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
1953 if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
1954 test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags)) {
1955 napi_complete_done(napi, 0);
1959 tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
1960 /* On netpoll the budget is zero and the handler should only clean the
1964 rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
1966 /* If the device is about to reset or down, avoid unmask
1967 * the interrupt and return 0 so NAPI won't reschedule
1969 if (unlikely(!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags) ||
1970 test_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags))) {
1971 napi_complete_done(napi, 0);
1974 } else if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
1977 /* Update numa and unmask the interrupt only when schedule
1978 * from the interrupt context (vs from sk_busy_loop)
1980 if (napi_complete_done(napi, rx_work_done) &&
1981 READ_ONCE(ena_napi->interrupts_masked)) {
1982 smp_rmb(); /* make sure interrupts_masked is read */
1983 WRITE_ONCE(ena_napi->interrupts_masked, false);
1984 /* We apply adaptive moderation on Rx path only.
1985 * Tx uses static interrupt moderation.
1987 if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
1988 ena_adjust_adaptive_rx_intr_moderation(ena_napi);
1990 ena_unmask_interrupt(tx_ring, rx_ring);
1993 ena_update_ring_numa_node(tx_ring, rx_ring);
2000 u64_stats_update_begin(&tx_ring->syncp);
2001 tx_ring->tx_stats.napi_comp += napi_comp_call;
2002 tx_ring->tx_stats.tx_poll++;
2003 u64_stats_update_end(&tx_ring->syncp);
2005 tx_ring->tx_stats.last_napi_jiffies = jiffies;
2010 static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
2012 struct ena_adapter *adapter = (struct ena_adapter *)data;
2014 ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
2016 /* Don't call the aenq handler before probe is done */
2017 if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
2018 ena_com_aenq_intr_handler(adapter->ena_dev, data);
2023 /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
2024 * @irq: interrupt number
2025 * @data: pointer to a network interface private napi device structure
2027 static irqreturn_t ena_intr_msix_io(int irq, void *data)
2029 struct ena_napi *ena_napi = data;
2031 /* Used to check HW health */
2032 WRITE_ONCE(ena_napi->first_interrupt, true);
2034 WRITE_ONCE(ena_napi->interrupts_masked, true);
2035 smp_wmb(); /* write interrupts_masked before calling napi */
2037 napi_schedule_irqoff(&ena_napi->napi);
2042 /* Reserve a single MSI-X vector for management (admin + aenq).
2043 * plus reserve one vector for each potential io queue.
2044 * the number of potential io queues is the minimum of what the device
2045 * supports and the number of vCPUs.
2047 static int ena_enable_msix(struct ena_adapter *adapter)
2049 int msix_vecs, irq_cnt;
2051 if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
2052 netif_err(adapter, probe, adapter->netdev,
2053 "Error, MSI-X is already enabled\n");
2057 /* Reserved the max msix vectors we might need */
2058 msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues);
2059 netif_dbg(adapter, probe, adapter->netdev,
2060 "Trying to enable MSI-X, vectors %d\n", msix_vecs);
2062 irq_cnt = pci_alloc_irq_vectors(adapter->pdev, ENA_MIN_MSIX_VEC,
2063 msix_vecs, PCI_IRQ_MSIX);
2066 netif_err(adapter, probe, adapter->netdev,
2067 "Failed to enable MSI-X. irq_cnt %d\n", irq_cnt);
2071 if (irq_cnt != msix_vecs) {
2072 netif_notice(adapter, probe, adapter->netdev,
2073 "Enable only %d MSI-X (out of %d), reduce the number of queues\n",
2074 irq_cnt, msix_vecs);
2075 adapter->num_io_queues = irq_cnt - ENA_ADMIN_MSIX_VEC;
2078 if (ena_init_rx_cpu_rmap(adapter))
2079 netif_warn(adapter, probe, adapter->netdev,
2080 "Failed to map IRQs to CPUs\n");
2082 adapter->msix_vecs = irq_cnt;
2083 set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
2088 static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
2092 snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
2093 ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
2094 pci_name(adapter->pdev));
2095 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
2096 ena_intr_msix_mgmnt;
2097 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
2098 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
2099 pci_irq_vector(adapter->pdev, ENA_MGMNT_IRQ_IDX);
2100 cpu = cpumask_first(cpu_online_mask);
2101 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
2102 cpumask_set_cpu(cpu,
2103 &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
2106 static void ena_setup_io_intr(struct ena_adapter *adapter)
2108 struct net_device *netdev;
2109 int irq_idx, i, cpu;
2112 netdev = adapter->netdev;
2113 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2115 for (i = 0; i < io_queue_count; i++) {
2116 irq_idx = ENA_IO_IRQ_IDX(i);
2117 cpu = i % num_online_cpus();
2119 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
2120 "%s-Tx-Rx-%d", netdev->name, i);
2121 adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
2122 adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
2123 adapter->irq_tbl[irq_idx].vector =
2124 pci_irq_vector(adapter->pdev, irq_idx);
2125 adapter->irq_tbl[irq_idx].cpu = cpu;
2127 cpumask_set_cpu(cpu,
2128 &adapter->irq_tbl[irq_idx].affinity_hint_mask);
2132 static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
2134 unsigned long flags = 0;
2135 struct ena_irq *irq;
2138 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
2139 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
2142 netif_err(adapter, probe, adapter->netdev,
2143 "Failed to request admin irq\n");
2147 netif_dbg(adapter, probe, adapter->netdev,
2148 "Set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
2149 irq->affinity_hint_mask.bits[0], irq->vector);
2151 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
2156 static int ena_request_io_irq(struct ena_adapter *adapter)
2158 u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2159 unsigned long flags = 0;
2160 struct ena_irq *irq;
2163 if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
2164 netif_err(adapter, ifup, adapter->netdev,
2165 "Failed to request I/O IRQ: MSI-X is not enabled\n");
2169 for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) {
2170 irq = &adapter->irq_tbl[i];
2171 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
2174 netif_err(adapter, ifup, adapter->netdev,
2175 "Failed to request I/O IRQ. index %d rc %d\n",
2180 netif_dbg(adapter, ifup, adapter->netdev,
2181 "Set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
2182 i, irq->affinity_hint_mask.bits[0], irq->vector);
2184 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
2190 for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
2191 irq = &adapter->irq_tbl[k];
2192 free_irq(irq->vector, irq->data);
2198 static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
2200 struct ena_irq *irq;
2202 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
2203 synchronize_irq(irq->vector);
2204 irq_set_affinity_hint(irq->vector, NULL);
2205 free_irq(irq->vector, irq->data);
2208 static void ena_free_io_irq(struct ena_adapter *adapter)
2210 u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2211 struct ena_irq *irq;
2214 #ifdef CONFIG_RFS_ACCEL
2215 if (adapter->msix_vecs >= 1) {
2216 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
2217 adapter->netdev->rx_cpu_rmap = NULL;
2219 #endif /* CONFIG_RFS_ACCEL */
2221 for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++) {
2222 irq = &adapter->irq_tbl[i];
2223 irq_set_affinity_hint(irq->vector, NULL);
2224 free_irq(irq->vector, irq->data);
2228 static void ena_disable_msix(struct ena_adapter *adapter)
2230 if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
2231 pci_free_irq_vectors(adapter->pdev);
2234 static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
2236 u32 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2239 if (!netif_running(adapter->netdev))
2242 for (i = ENA_IO_IRQ_FIRST_IDX; i < ENA_MAX_MSIX_VEC(io_queue_count); i++)
2243 synchronize_irq(adapter->irq_tbl[i].vector);
2246 static void ena_del_napi_in_range(struct ena_adapter *adapter,
2252 for (i = first_index; i < first_index + count; i++) {
2253 netif_napi_del(&adapter->ena_napi[i].napi);
2255 WARN_ON(!ENA_IS_XDP_INDEX(adapter, i) &&
2256 adapter->ena_napi[i].xdp_ring);
2260 static void ena_init_napi_in_range(struct ena_adapter *adapter,
2261 int first_index, int count)
2265 for (i = first_index; i < first_index + count; i++) {
2266 struct ena_napi *napi = &adapter->ena_napi[i];
2268 netif_napi_add(adapter->netdev,
2270 ENA_IS_XDP_INDEX(adapter, i) ? ena_xdp_io_poll : ena_io_poll,
2273 if (!ENA_IS_XDP_INDEX(adapter, i)) {
2274 napi->rx_ring = &adapter->rx_ring[i];
2275 napi->tx_ring = &adapter->tx_ring[i];
2277 napi->xdp_ring = &adapter->tx_ring[i];
2283 static void ena_napi_disable_in_range(struct ena_adapter *adapter,
2289 for (i = first_index; i < first_index + count; i++)
2290 napi_disable(&adapter->ena_napi[i].napi);
2293 static void ena_napi_enable_in_range(struct ena_adapter *adapter,
2299 for (i = first_index; i < first_index + count; i++)
2300 napi_enable(&adapter->ena_napi[i].napi);
2303 /* Configure the Rx forwarding */
2304 static int ena_rss_configure(struct ena_adapter *adapter)
2306 struct ena_com_dev *ena_dev = adapter->ena_dev;
2309 /* In case the RSS table wasn't initialized by probe */
2310 if (!ena_dev->rss.tbl_log_size) {
2311 rc = ena_rss_init_default(adapter);
2312 if (rc && (rc != -EOPNOTSUPP)) {
2313 netif_err(adapter, ifup, adapter->netdev,
2314 "Failed to init RSS rc: %d\n", rc);
2319 /* Set indirect table */
2320 rc = ena_com_indirect_table_set(ena_dev);
2321 if (unlikely(rc && rc != -EOPNOTSUPP))
2324 /* Configure hash function (if supported) */
2325 rc = ena_com_set_hash_function(ena_dev);
2326 if (unlikely(rc && (rc != -EOPNOTSUPP)))
2329 /* Configure hash inputs (if supported) */
2330 rc = ena_com_set_hash_ctrl(ena_dev);
2331 if (unlikely(rc && (rc != -EOPNOTSUPP)))
2337 static int ena_up_complete(struct ena_adapter *adapter)
2341 rc = ena_rss_configure(adapter);
2345 ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
2347 ena_refill_all_rx_bufs(adapter);
2349 /* enable transmits */
2350 netif_tx_start_all_queues(adapter->netdev);
2352 ena_napi_enable_in_range(adapter,
2354 adapter->xdp_num_queues + adapter->num_io_queues);
2359 static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
2361 struct ena_com_create_io_ctx ctx;
2362 struct ena_com_dev *ena_dev;
2363 struct ena_ring *tx_ring;
2368 ena_dev = adapter->ena_dev;
2370 tx_ring = &adapter->tx_ring[qid];
2371 msix_vector = ENA_IO_IRQ_IDX(qid);
2372 ena_qid = ENA_IO_TXQ_IDX(qid);
2374 memset(&ctx, 0x0, sizeof(ctx));
2376 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
2378 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
2379 ctx.msix_vector = msix_vector;
2380 ctx.queue_size = tx_ring->ring_size;
2381 ctx.numa_node = cpu_to_node(tx_ring->cpu);
2383 rc = ena_com_create_io_queue(ena_dev, &ctx);
2385 netif_err(adapter, ifup, adapter->netdev,
2386 "Failed to create I/O TX queue num %d rc: %d\n",
2391 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
2392 &tx_ring->ena_com_io_sq,
2393 &tx_ring->ena_com_io_cq);
2395 netif_err(adapter, ifup, adapter->netdev,
2396 "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
2398 ena_com_destroy_io_queue(ena_dev, ena_qid);
2402 ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
2406 static int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter,
2407 int first_index, int count)
2409 struct ena_com_dev *ena_dev = adapter->ena_dev;
2412 for (i = first_index; i < first_index + count; i++) {
2413 rc = ena_create_io_tx_queue(adapter, i);
2421 while (i-- > first_index)
2422 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
2427 static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
2429 struct ena_com_dev *ena_dev;
2430 struct ena_com_create_io_ctx ctx;
2431 struct ena_ring *rx_ring;
2436 ena_dev = adapter->ena_dev;
2438 rx_ring = &adapter->rx_ring[qid];
2439 msix_vector = ENA_IO_IRQ_IDX(qid);
2440 ena_qid = ENA_IO_RXQ_IDX(qid);
2442 memset(&ctx, 0x0, sizeof(ctx));
2445 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
2446 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
2447 ctx.msix_vector = msix_vector;
2448 ctx.queue_size = rx_ring->ring_size;
2449 ctx.numa_node = cpu_to_node(rx_ring->cpu);
2451 rc = ena_com_create_io_queue(ena_dev, &ctx);
2453 netif_err(adapter, ifup, adapter->netdev,
2454 "Failed to create I/O RX queue num %d rc: %d\n",
2459 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
2460 &rx_ring->ena_com_io_sq,
2461 &rx_ring->ena_com_io_cq);
2463 netif_err(adapter, ifup, adapter->netdev,
2464 "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
2469 ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
2473 ena_com_destroy_io_queue(ena_dev, ena_qid);
2477 static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
2479 struct ena_com_dev *ena_dev = adapter->ena_dev;
2482 for (i = 0; i < adapter->num_io_queues; i++) {
2483 rc = ena_create_io_rx_queue(adapter, i);
2486 INIT_WORK(&adapter->ena_napi[i].dim.work, ena_dim_work);
2493 cancel_work_sync(&adapter->ena_napi[i].dim.work);
2494 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
2500 static void set_io_rings_size(struct ena_adapter *adapter,
2506 for (i = 0; i < adapter->num_io_queues; i++) {
2507 adapter->tx_ring[i].ring_size = new_tx_size;
2508 adapter->rx_ring[i].ring_size = new_rx_size;
2512 /* This function allows queue allocation to backoff when the system is
2513 * low on memory. If there is not enough memory to allocate io queues
2514 * the driver will try to allocate smaller queues.
2516 * The backoff algorithm is as follows:
2517 * 1. Try to allocate TX and RX and if successful.
2518 * 1.1. return success
2520 * 2. Divide by 2 the size of the larger of RX and TX queues (or both if their size is the same).
2522 * 3. If TX or RX is smaller than 256
2523 * 3.1. return failure.
2525 * 4.1. go back to 1.
2527 static int create_queues_with_size_backoff(struct ena_adapter *adapter)
2529 int rc, cur_rx_ring_size, cur_tx_ring_size;
2530 int new_rx_ring_size, new_tx_ring_size;
2532 /* current queue sizes might be set to smaller than the requested
2533 * ones due to past queue allocation failures.
2535 set_io_rings_size(adapter, adapter->requested_tx_ring_size,
2536 adapter->requested_rx_ring_size);
2539 if (ena_xdp_present(adapter)) {
2540 rc = ena_setup_and_create_all_xdp_queues(adapter);
2545 rc = ena_setup_tx_resources_in_range(adapter,
2547 adapter->num_io_queues);
2551 rc = ena_create_io_tx_queues_in_range(adapter,
2553 adapter->num_io_queues);
2555 goto err_create_tx_queues;
2557 rc = ena_setup_all_rx_resources(adapter);
2561 rc = ena_create_all_io_rx_queues(adapter);
2563 goto err_create_rx_queues;
2567 err_create_rx_queues:
2568 ena_free_all_io_rx_resources(adapter);
2570 ena_destroy_all_tx_queues(adapter);
2571 err_create_tx_queues:
2572 ena_free_all_io_tx_resources(adapter);
2574 if (rc != -ENOMEM) {
2575 netif_err(adapter, ifup, adapter->netdev,
2576 "Queue creation failed with error code %d\n",
2581 cur_tx_ring_size = adapter->tx_ring[0].ring_size;
2582 cur_rx_ring_size = adapter->rx_ring[0].ring_size;
2584 netif_err(adapter, ifup, adapter->netdev,
2585 "Not enough memory to create queues with sizes TX=%d, RX=%d\n",
2586 cur_tx_ring_size, cur_rx_ring_size);
2588 new_tx_ring_size = cur_tx_ring_size;
2589 new_rx_ring_size = cur_rx_ring_size;
2591 /* Decrease the size of the larger queue, or
2592 * decrease both if they are the same size.
2594 if (cur_rx_ring_size <= cur_tx_ring_size)
2595 new_tx_ring_size = cur_tx_ring_size / 2;
2596 if (cur_rx_ring_size >= cur_tx_ring_size)
2597 new_rx_ring_size = cur_rx_ring_size / 2;
2599 if (new_tx_ring_size < ENA_MIN_RING_SIZE ||
2600 new_rx_ring_size < ENA_MIN_RING_SIZE) {
2601 netif_err(adapter, ifup, adapter->netdev,
2602 "Queue creation failed with the smallest possible queue size of %d for both queues. Not retrying with smaller queues\n",
2607 netif_err(adapter, ifup, adapter->netdev,
2608 "Retrying queue creation with sizes TX=%d, RX=%d\n",
2612 set_io_rings_size(adapter, new_tx_ring_size,
2617 static int ena_up(struct ena_adapter *adapter)
2619 int io_queue_count, rc, i;
2621 netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__);
2623 io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2624 ena_setup_io_intr(adapter);
2626 /* napi poll functions should be initialized before running
2627 * request_irq(), to handle a rare condition where there is a pending
2628 * interrupt, causing the ISR to fire immediately while the poll
2629 * function wasn't set yet, causing a null dereference
2631 ena_init_napi_in_range(adapter, 0, io_queue_count);
2633 rc = ena_request_io_irq(adapter);
2637 rc = create_queues_with_size_backoff(adapter);
2639 goto err_create_queues_with_backoff;
2641 rc = ena_up_complete(adapter);
2645 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
2646 netif_carrier_on(adapter->netdev);
2648 ena_increase_stat(&adapter->dev_stats.interface_up, 1,
2651 set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2653 /* Enable completion queues interrupt */
2654 for (i = 0; i < adapter->num_io_queues; i++)
2655 ena_unmask_interrupt(&adapter->tx_ring[i],
2656 &adapter->rx_ring[i]);
2658 /* schedule napi in case we had pending packets
2659 * from the last time we disable napi
2661 for (i = 0; i < io_queue_count; i++)
2662 napi_schedule(&adapter->ena_napi[i].napi);
2667 ena_destroy_all_tx_queues(adapter);
2668 ena_free_all_io_tx_resources(adapter);
2669 ena_destroy_all_rx_queues(adapter);
2670 ena_free_all_io_rx_resources(adapter);
2671 err_create_queues_with_backoff:
2672 ena_free_io_irq(adapter);
2674 ena_del_napi_in_range(adapter, 0, io_queue_count);
2679 static void ena_down(struct ena_adapter *adapter)
2681 int io_queue_count = adapter->num_io_queues + adapter->xdp_num_queues;
2683 netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__);
2685 clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2687 ena_increase_stat(&adapter->dev_stats.interface_down, 1,
2690 netif_carrier_off(adapter->netdev);
2691 netif_tx_disable(adapter->netdev);
2693 /* After this point the napi handler won't enable the tx queue */
2694 ena_napi_disable_in_range(adapter, 0, io_queue_count);
2696 /* After destroy the queue there won't be any new interrupts */
2698 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags)) {
2701 rc = ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
2703 netif_err(adapter, ifdown, adapter->netdev,
2704 "Device reset failed\n");
2705 /* stop submitting admin commands on a device that was reset */
2706 ena_com_set_admin_running_state(adapter->ena_dev, false);
2709 ena_destroy_all_io_queues(adapter);
2711 ena_disable_io_intr_sync(adapter);
2712 ena_free_io_irq(adapter);
2713 ena_del_napi_in_range(adapter, 0, io_queue_count);
2715 ena_free_all_tx_bufs(adapter);
2716 ena_free_all_rx_bufs(adapter);
2717 ena_free_all_io_tx_resources(adapter);
2718 ena_free_all_io_rx_resources(adapter);
2721 /* ena_open - Called when a network interface is made active
2722 * @netdev: network interface device structure
2724 * Returns 0 on success, negative value on failure
2726 * The open entry point is called when a network interface is made
2727 * active by the system (IFF_UP). At this point all resources needed
2728 * for transmit and receive operations are allocated, the interrupt
2729 * handler is registered with the OS, the watchdog timer is started,
2730 * and the stack is notified that the interface is ready.
2732 static int ena_open(struct net_device *netdev)
2734 struct ena_adapter *adapter = netdev_priv(netdev);
2737 /* Notify the stack of the actual queue counts. */
2738 rc = netif_set_real_num_tx_queues(netdev, adapter->num_io_queues);
2740 netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
2744 rc = netif_set_real_num_rx_queues(netdev, adapter->num_io_queues);
2746 netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
2750 rc = ena_up(adapter);
2757 /* ena_close - Disables a network interface
2758 * @netdev: network interface device structure
2760 * Returns 0, this is not allowed to fail
2762 * The close entry point is called when an interface is de-activated
2763 * by the OS. The hardware is still under the drivers control, but
2764 * needs to be disabled. A global MAC reset is issued to stop the
2765 * hardware, and all transmit and receive resources are freed.
2767 static int ena_close(struct net_device *netdev)
2769 struct ena_adapter *adapter = netdev_priv(netdev);
2771 netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
2773 if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
2776 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2779 /* Check for device status and issue reset if needed*/
2780 check_for_admin_com_state(adapter);
2781 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
2782 netif_err(adapter, ifdown, adapter->netdev,
2783 "Destroy failure, restarting device\n");
2784 ena_dump_stats_to_dmesg(adapter);
2785 /* rtnl lock already obtained in dev_ioctl() layer */
2786 ena_destroy_device(adapter, false);
2787 ena_restore_device(adapter);
2793 int ena_update_queue_sizes(struct ena_adapter *adapter,
2799 dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2800 ena_close(adapter->netdev);
2801 adapter->requested_tx_ring_size = new_tx_size;
2802 adapter->requested_rx_ring_size = new_rx_size;
2803 ena_init_io_rings(adapter,
2805 adapter->xdp_num_queues +
2806 adapter->num_io_queues);
2807 return dev_was_up ? ena_up(adapter) : 0;
2810 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count)
2812 struct ena_com_dev *ena_dev = adapter->ena_dev;
2813 int prev_channel_count;
2816 dev_was_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2817 ena_close(adapter->netdev);
2818 prev_channel_count = adapter->num_io_queues;
2819 adapter->num_io_queues = new_channel_count;
2820 if (ena_xdp_present(adapter) &&
2821 ena_xdp_allowed(adapter) == ENA_XDP_ALLOWED) {
2822 adapter->xdp_first_ring = new_channel_count;
2823 adapter->xdp_num_queues = new_channel_count;
2824 if (prev_channel_count > new_channel_count)
2825 ena_xdp_exchange_program_rx_in_range(adapter,
2828 prev_channel_count);
2830 ena_xdp_exchange_program_rx_in_range(adapter,
2831 adapter->xdp_bpf_prog,
2836 /* We need to destroy the rss table so that the indirection
2837 * table will be reinitialized by ena_up()
2839 ena_com_rss_destroy(ena_dev);
2840 ena_init_io_rings(adapter,
2842 adapter->xdp_num_queues +
2843 adapter->num_io_queues);
2844 return dev_was_up ? ena_open(adapter->netdev) : 0;
2847 static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx,
2848 struct sk_buff *skb,
2849 bool disable_meta_caching)
2851 u32 mss = skb_shinfo(skb)->gso_size;
2852 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
2855 if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
2856 ena_tx_ctx->l4_csum_enable = 1;
2858 ena_tx_ctx->tso_enable = 1;
2859 ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
2860 ena_tx_ctx->l4_csum_partial = 0;
2862 ena_tx_ctx->tso_enable = 0;
2863 ena_meta->l4_hdr_len = 0;
2864 ena_tx_ctx->l4_csum_partial = 1;
2867 switch (ip_hdr(skb)->version) {
2869 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2870 if (ip_hdr(skb)->frag_off & htons(IP_DF))
2873 ena_tx_ctx->l3_csum_enable = 1;
2874 l4_protocol = ip_hdr(skb)->protocol;
2877 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
2878 l4_protocol = ipv6_hdr(skb)->nexthdr;
2884 if (l4_protocol == IPPROTO_TCP)
2885 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
2887 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
2889 ena_meta->mss = mss;
2890 ena_meta->l3_hdr_len = skb_network_header_len(skb);
2891 ena_meta->l3_hdr_offset = skb_network_offset(skb);
2892 ena_tx_ctx->meta_valid = 1;
2893 } else if (disable_meta_caching) {
2894 memset(ena_meta, 0, sizeof(*ena_meta));
2895 ena_tx_ctx->meta_valid = 1;
2897 ena_tx_ctx->meta_valid = 0;
2901 static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
2902 struct sk_buff *skb)
2904 int num_frags, header_len, rc;
2906 num_frags = skb_shinfo(skb)->nr_frags;
2907 header_len = skb_headlen(skb);
2909 if (num_frags < tx_ring->sgl_size)
2912 if ((num_frags == tx_ring->sgl_size) &&
2913 (header_len < tx_ring->tx_max_header_size))
2916 ena_increase_stat(&tx_ring->tx_stats.linearize, 1, &tx_ring->syncp);
2918 rc = skb_linearize(skb);
2920 ena_increase_stat(&tx_ring->tx_stats.linearize_failed, 1,
2927 static int ena_tx_map_skb(struct ena_ring *tx_ring,
2928 struct ena_tx_buffer *tx_info,
2929 struct sk_buff *skb,
2933 struct ena_adapter *adapter = tx_ring->adapter;
2934 struct ena_com_buf *ena_buf;
2936 u32 skb_head_len, frag_len, last_frag;
2941 skb_head_len = skb_headlen(skb);
2943 ena_buf = tx_info->bufs;
2945 if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2946 /* When the device is LLQ mode, the driver will copy
2947 * the header into the device memory space.
2948 * the ena_com layer assume the header is in a linear
2950 * This assumption might be wrong since part of the header
2951 * can be in the fragmented buffers.
2952 * Use skb_header_pointer to make sure the header is in a
2953 * linear memory space.
2956 push_len = min_t(u32, skb->len, tx_ring->tx_max_header_size);
2957 *push_hdr = skb_header_pointer(skb, 0, push_len,
2958 tx_ring->push_buf_intermediate_buf);
2959 *header_len = push_len;
2960 if (unlikely(skb->data != *push_hdr)) {
2961 ena_increase_stat(&tx_ring->tx_stats.llq_buffer_copy, 1,
2964 delta = push_len - skb_head_len;
2968 *header_len = min_t(u32, skb_head_len,
2969 tx_ring->tx_max_header_size);
2972 netif_dbg(adapter, tx_queued, adapter->netdev,
2973 "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
2974 *push_hdr, push_len);
2976 if (skb_head_len > push_len) {
2977 dma = dma_map_single(tx_ring->dev, skb->data + push_len,
2978 skb_head_len - push_len, DMA_TO_DEVICE);
2979 if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
2980 goto error_report_dma_error;
2982 ena_buf->paddr = dma;
2983 ena_buf->len = skb_head_len - push_len;
2986 tx_info->num_of_bufs++;
2987 tx_info->map_linear_data = 1;
2989 tx_info->map_linear_data = 0;
2992 last_frag = skb_shinfo(skb)->nr_frags;
2994 for (i = 0; i < last_frag; i++) {
2995 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2997 frag_len = skb_frag_size(frag);
2999 if (unlikely(delta >= frag_len)) {
3004 dma = skb_frag_dma_map(tx_ring->dev, frag, delta,
3005 frag_len - delta, DMA_TO_DEVICE);
3006 if (unlikely(dma_mapping_error(tx_ring->dev, dma)))
3007 goto error_report_dma_error;
3009 ena_buf->paddr = dma;
3010 ena_buf->len = frag_len - delta;
3012 tx_info->num_of_bufs++;
3018 error_report_dma_error:
3019 ena_increase_stat(&tx_ring->tx_stats.dma_mapping_err, 1,
3021 netif_warn(adapter, tx_queued, adapter->netdev, "Failed to map skb\n");
3023 tx_info->skb = NULL;
3025 tx_info->num_of_bufs += i;
3026 ena_unmap_tx_buff(tx_ring, tx_info);
3031 /* Called with netif_tx_lock. */
3032 static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
3034 struct ena_adapter *adapter = netdev_priv(dev);
3035 struct ena_tx_buffer *tx_info;
3036 struct ena_com_tx_ctx ena_tx_ctx;
3037 struct ena_ring *tx_ring;
3038 struct netdev_queue *txq;
3040 u16 next_to_use, req_id, header_len;
3043 netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
3044 /* Determine which tx ring we will be placed on */
3045 qid = skb_get_queue_mapping(skb);
3046 tx_ring = &adapter->tx_ring[qid];
3047 txq = netdev_get_tx_queue(dev, qid);
3049 rc = ena_check_and_linearize_skb(tx_ring, skb);
3051 goto error_drop_packet;
3053 skb_tx_timestamp(skb);
3055 next_to_use = tx_ring->next_to_use;
3056 req_id = tx_ring->free_ids[next_to_use];
3057 tx_info = &tx_ring->tx_buffer_info[req_id];
3058 tx_info->num_of_bufs = 0;
3060 WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
3062 rc = ena_tx_map_skb(tx_ring, tx_info, skb, &push_hdr, &header_len);
3064 goto error_drop_packet;
3066 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
3067 ena_tx_ctx.ena_bufs = tx_info->bufs;
3068 ena_tx_ctx.push_header = push_hdr;
3069 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
3070 ena_tx_ctx.req_id = req_id;
3071 ena_tx_ctx.header_len = header_len;
3073 /* set flags and meta data */
3074 ena_tx_csum(&ena_tx_ctx, skb, tx_ring->disable_meta_caching);
3076 rc = ena_xmit_common(dev,
3083 goto error_unmap_dma;
3085 netdev_tx_sent_queue(txq, skb->len);
3087 /* stop the queue when no more space available, the packet can have up
3088 * to sgl_size + 2. one for the meta descriptor and one for header
3089 * (if the header is larger than tx_max_header_size).
3091 if (unlikely(!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
3092 tx_ring->sgl_size + 2))) {
3093 netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
3096 netif_tx_stop_queue(txq);
3097 ena_increase_stat(&tx_ring->tx_stats.queue_stop, 1,
3100 /* There is a rare condition where this function decide to
3101 * stop the queue but meanwhile clean_tx_irq updates
3102 * next_to_completion and terminates.
3103 * The queue will remain stopped forever.
3104 * To solve this issue add a mb() to make sure that
3105 * netif_tx_stop_queue() write is vissible before checking if
3106 * there is additional space in the queue.
3110 if (ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
3111 ENA_TX_WAKEUP_THRESH)) {
3112 netif_tx_wake_queue(txq);
3113 ena_increase_stat(&tx_ring->tx_stats.queue_wakeup, 1,
3118 if (netif_xmit_stopped(txq) || !netdev_xmit_more())
3119 /* trigger the dma engine. ena_ring_tx_doorbell()
3120 * calls a memory barrier inside it.
3122 ena_ring_tx_doorbell(tx_ring);
3124 return NETDEV_TX_OK;
3127 ena_unmap_tx_buff(tx_ring, tx_info);
3128 tx_info->skb = NULL;
3132 return NETDEV_TX_OK;
3135 static u16 ena_select_queue(struct net_device *dev, struct sk_buff *skb,
3136 struct net_device *sb_dev)
3139 /* we suspect that this is good for in--kernel network services that
3140 * want to loop incoming skb rx to tx in normal user generated traffic,
3141 * most probably we will not get to this
3143 if (skb_rx_queue_recorded(skb))
3144 qid = skb_get_rx_queue(skb);
3146 qid = netdev_pick_tx(dev, skb, NULL);
3151 static void ena_config_host_info(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
3153 struct device *dev = &pdev->dev;
3154 struct ena_admin_host_info *host_info;
3157 /* Allocate only the host info */
3158 rc = ena_com_allocate_host_info(ena_dev);
3160 dev_err(dev, "Cannot allocate host info\n");
3164 host_info = ena_dev->host_attr.host_info;
3166 host_info->bdf = (pdev->bus->number << 8) | pdev->devfn;
3167 host_info->os_type = ENA_ADMIN_OS_LINUX;
3168 host_info->kernel_ver = LINUX_VERSION_CODE;
3169 strlcpy(host_info->kernel_ver_str, utsname()->version,
3170 sizeof(host_info->kernel_ver_str) - 1);
3171 host_info->os_dist = 0;
3172 strncpy(host_info->os_dist_str, utsname()->release,
3173 sizeof(host_info->os_dist_str) - 1);
3174 host_info->driver_version =
3175 (DRV_MODULE_GEN_MAJOR) |
3176 (DRV_MODULE_GEN_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
3177 (DRV_MODULE_GEN_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT) |
3178 ("K"[0] << ENA_ADMIN_HOST_INFO_MODULE_TYPE_SHIFT);
3179 host_info->num_cpus = num_online_cpus();
3181 host_info->driver_supported_features =
3182 ENA_ADMIN_HOST_INFO_RX_OFFSET_MASK |
3183 ENA_ADMIN_HOST_INFO_INTERRUPT_MODERATION_MASK |
3184 ENA_ADMIN_HOST_INFO_RX_BUF_MIRRORING_MASK |
3185 ENA_ADMIN_HOST_INFO_RSS_CONFIGURABLE_FUNCTION_KEY_MASK;
3187 rc = ena_com_set_host_attributes(ena_dev);
3189 if (rc == -EOPNOTSUPP)
3190 dev_warn(dev, "Cannot set host attributes\n");
3192 dev_err(dev, "Cannot set host attributes\n");
3200 ena_com_delete_host_info(ena_dev);
3203 static void ena_config_debug_area(struct ena_adapter *adapter)
3205 u32 debug_area_size;
3208 ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
3209 if (ss_count <= 0) {
3210 netif_err(adapter, drv, adapter->netdev,
3211 "SS count is negative\n");
3215 /* allocate 32 bytes for each string and 64bit for the value */
3216 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
3218 rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
3220 netif_err(adapter, drv, adapter->netdev,
3221 "Cannot allocate debug area\n");
3225 rc = ena_com_set_host_attributes(adapter->ena_dev);
3227 if (rc == -EOPNOTSUPP)
3228 netif_warn(adapter, drv, adapter->netdev,
3229 "Cannot set host attributes\n");
3231 netif_err(adapter, drv, adapter->netdev,
3232 "Cannot set host attributes\n");
3238 ena_com_delete_debug_area(adapter->ena_dev);
3241 int ena_update_hw_stats(struct ena_adapter *adapter)
3245 rc = ena_com_get_eni_stats(adapter->ena_dev, &adapter->eni_stats);
3247 dev_info_once(&adapter->pdev->dev, "Failed to get ENI stats\n");
3254 static void ena_get_stats64(struct net_device *netdev,
3255 struct rtnl_link_stats64 *stats)
3257 struct ena_adapter *adapter = netdev_priv(netdev);
3258 struct ena_ring *rx_ring, *tx_ring;
3264 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3267 for (i = 0; i < adapter->num_io_queues; i++) {
3270 tx_ring = &adapter->tx_ring[i];
3273 start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
3274 packets = tx_ring->tx_stats.cnt;
3275 bytes = tx_ring->tx_stats.bytes;
3276 } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
3278 stats->tx_packets += packets;
3279 stats->tx_bytes += bytes;
3281 rx_ring = &adapter->rx_ring[i];
3284 start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
3285 packets = rx_ring->rx_stats.cnt;
3286 bytes = rx_ring->rx_stats.bytes;
3287 } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
3289 stats->rx_packets += packets;
3290 stats->rx_bytes += bytes;
3294 start = u64_stats_fetch_begin_irq(&adapter->syncp);
3295 rx_drops = adapter->dev_stats.rx_drops;
3296 tx_drops = adapter->dev_stats.tx_drops;
3297 } while (u64_stats_fetch_retry_irq(&adapter->syncp, start));
3299 stats->rx_dropped = rx_drops;
3300 stats->tx_dropped = tx_drops;
3302 stats->multicast = 0;
3303 stats->collisions = 0;
3305 stats->rx_length_errors = 0;
3306 stats->rx_crc_errors = 0;
3307 stats->rx_frame_errors = 0;
3308 stats->rx_fifo_errors = 0;
3309 stats->rx_missed_errors = 0;
3310 stats->tx_window_errors = 0;
3312 stats->rx_errors = 0;
3313 stats->tx_errors = 0;
3316 static const struct net_device_ops ena_netdev_ops = {
3317 .ndo_open = ena_open,
3318 .ndo_stop = ena_close,
3319 .ndo_start_xmit = ena_start_xmit,
3320 .ndo_select_queue = ena_select_queue,
3321 .ndo_get_stats64 = ena_get_stats64,
3322 .ndo_tx_timeout = ena_tx_timeout,
3323 .ndo_change_mtu = ena_change_mtu,
3324 .ndo_set_mac_address = NULL,
3325 .ndo_validate_addr = eth_validate_addr,
3327 .ndo_xdp_xmit = ena_xdp_xmit,
3330 static int ena_device_validate_params(struct ena_adapter *adapter,
3331 struct ena_com_dev_get_features_ctx *get_feat_ctx)
3333 struct net_device *netdev = adapter->netdev;
3336 rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
3339 netif_err(adapter, drv, netdev,
3340 "Error, mac address are different\n");
3344 if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
3345 netif_err(adapter, drv, netdev,
3346 "Error, device max mtu is smaller than netdev MTU\n");
3353 static void set_default_llq_configurations(struct ena_llq_configurations *llq_config)
3355 llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
3356 llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
3357 llq_config->llq_num_decs_before_header = ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
3358 llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
3359 llq_config->llq_ring_entry_size_value = 128;
3362 static int ena_set_queues_placement_policy(struct pci_dev *pdev,
3363 struct ena_com_dev *ena_dev,
3364 struct ena_admin_feature_llq_desc *llq,
3365 struct ena_llq_configurations *llq_default_configurations)
3368 u32 llq_feature_mask;
3370 llq_feature_mask = 1 << ENA_ADMIN_LLQ;
3371 if (!(ena_dev->supported_features & llq_feature_mask)) {
3372 dev_warn(&pdev->dev,
3373 "LLQ is not supported Fallback to host mode policy.\n");
3374 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3378 rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
3381 "Failed to configure the device mode. Fallback to host mode policy.\n");
3382 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3388 static int ena_map_llq_mem_bar(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
3391 bool has_mem_bar = !!(bars & BIT(ENA_MEM_BAR));
3394 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
3396 "ENA device does not expose LLQ bar. Fallback to host mode policy.\n");
3397 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
3403 ena_dev->mem_bar = devm_ioremap_wc(&pdev->dev,
3404 pci_resource_start(pdev, ENA_MEM_BAR),
3405 pci_resource_len(pdev, ENA_MEM_BAR));
3407 if (!ena_dev->mem_bar)
3413 static int ena_device_init(struct ena_com_dev *ena_dev, struct pci_dev *pdev,
3414 struct ena_com_dev_get_features_ctx *get_feat_ctx,
3417 struct ena_llq_configurations llq_config;
3418 struct device *dev = &pdev->dev;
3419 bool readless_supported;
3424 rc = ena_com_mmio_reg_read_request_init(ena_dev);
3426 dev_err(dev, "Failed to init mmio read less\n");
3430 /* The PCIe configuration space revision id indicate if mmio reg
3433 readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
3434 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
3436 rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
3438 dev_err(dev, "Can not reset device\n");
3439 goto err_mmio_read_less;
3442 rc = ena_com_validate_version(ena_dev);
3444 dev_err(dev, "Device version is too low\n");
3445 goto err_mmio_read_less;
3448 dma_width = ena_com_get_dma_width(ena_dev);
3449 if (dma_width < 0) {
3450 dev_err(dev, "Invalid dma width value %d", dma_width);
3452 goto err_mmio_read_less;
3455 rc = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(dma_width));
3457 dev_err(dev, "dma_set_mask_and_coherent failed %d\n", rc);
3458 goto err_mmio_read_less;
3461 /* ENA admin level init */
3462 rc = ena_com_admin_init(ena_dev, &aenq_handlers);
3465 "Can not initialize ena admin queue with device\n");
3466 goto err_mmio_read_less;
3469 /* To enable the msix interrupts the driver needs to know the number
3470 * of queues. So the driver uses polling mode to retrieve this
3473 ena_com_set_admin_polling_mode(ena_dev, true);
3475 ena_config_host_info(ena_dev, pdev);
3477 /* Get Device Attributes*/
3478 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
3480 dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
3481 goto err_admin_init;
3484 /* Try to turn all the available aenq groups */
3485 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
3486 BIT(ENA_ADMIN_FATAL_ERROR) |
3487 BIT(ENA_ADMIN_WARNING) |
3488 BIT(ENA_ADMIN_NOTIFICATION) |
3489 BIT(ENA_ADMIN_KEEP_ALIVE);
3491 aenq_groups &= get_feat_ctx->aenq.supported_groups;
3493 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
3495 dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
3496 goto err_admin_init;
3499 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
3501 set_default_llq_configurations(&llq_config);
3503 rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx->llq,
3506 dev_err(dev, "ENA device init failed\n");
3507 goto err_admin_init;
3513 ena_com_delete_host_info(ena_dev);
3514 ena_com_admin_destroy(ena_dev);
3516 ena_com_mmio_reg_read_request_destroy(ena_dev);
3521 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter)
3523 struct ena_com_dev *ena_dev = adapter->ena_dev;
3524 struct device *dev = &adapter->pdev->dev;
3527 rc = ena_enable_msix(adapter);
3529 dev_err(dev, "Can not reserve msix vectors\n");
3533 ena_setup_mgmnt_intr(adapter);
3535 rc = ena_request_mgmnt_irq(adapter);
3537 dev_err(dev, "Can not setup management interrupts\n");
3538 goto err_disable_msix;
3541 ena_com_set_admin_polling_mode(ena_dev, false);
3543 ena_com_admin_aenq_enable(ena_dev);
3548 ena_disable_msix(adapter);
3553 static void ena_destroy_device(struct ena_adapter *adapter, bool graceful)
3555 struct net_device *netdev = adapter->netdev;
3556 struct ena_com_dev *ena_dev = adapter->ena_dev;
3559 if (!test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags))
3562 netif_carrier_off(netdev);
3564 del_timer_sync(&adapter->timer_service);
3566 dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
3567 adapter->dev_up_before_reset = dev_up;
3569 ena_com_set_admin_running_state(ena_dev, false);
3571 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3574 /* Stop the device from sending AENQ events (in case reset flag is set
3575 * and device is up, ena_down() already reset the device.
3577 if (!(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags) && dev_up))
3578 ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
3580 ena_free_mgmnt_irq(adapter);
3582 ena_disable_msix(adapter);
3584 ena_com_abort_admin_commands(ena_dev);
3586 ena_com_wait_for_abort_completion(ena_dev);
3588 ena_com_admin_destroy(ena_dev);
3590 ena_com_mmio_reg_read_request_destroy(ena_dev);
3592 /* return reset reason to default value */
3593 adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3595 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3596 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3599 static int ena_restore_device(struct ena_adapter *adapter)
3601 struct ena_com_dev_get_features_ctx get_feat_ctx;
3602 struct ena_com_dev *ena_dev = adapter->ena_dev;
3603 struct pci_dev *pdev = adapter->pdev;
3607 set_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
3608 rc = ena_device_init(ena_dev, adapter->pdev, &get_feat_ctx, &wd_state);
3610 dev_err(&pdev->dev, "Can not initialize device\n");
3613 adapter->wd_state = wd_state;
3615 rc = ena_device_validate_params(adapter, &get_feat_ctx);
3617 dev_err(&pdev->dev, "Validation of device parameters failed\n");
3618 goto err_device_destroy;
3621 rc = ena_enable_msix_and_set_admin_interrupts(adapter);
3623 dev_err(&pdev->dev, "Enable MSI-X failed\n");
3624 goto err_device_destroy;
3626 /* If the interface was up before the reset bring it up */
3627 if (adapter->dev_up_before_reset) {
3628 rc = ena_up(adapter);
3630 dev_err(&pdev->dev, "Failed to create I/O queues\n");
3631 goto err_disable_msix;
3635 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3637 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
3638 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
3639 netif_carrier_on(adapter->netdev);
3641 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
3642 adapter->last_keep_alive_jiffies = jiffies;
3644 dev_err(&pdev->dev, "Device reset completed successfully\n");
3648 ena_free_mgmnt_irq(adapter);
3649 ena_disable_msix(adapter);
3651 ena_com_abort_admin_commands(ena_dev);
3652 ena_com_wait_for_abort_completion(ena_dev);
3653 ena_com_admin_destroy(ena_dev);
3654 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
3655 ena_com_mmio_reg_read_request_destroy(ena_dev);
3657 clear_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3658 clear_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags);
3660 "Reset attempt failed. Can not reset the device\n");
3665 static void ena_fw_reset_device(struct work_struct *work)
3667 struct ena_adapter *adapter =
3668 container_of(work, struct ena_adapter, reset_task);
3672 if (likely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
3673 ena_destroy_device(adapter, false);
3674 ena_restore_device(adapter);
3680 static int check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3681 struct ena_ring *rx_ring)
3683 struct ena_napi *ena_napi = container_of(rx_ring->napi, struct ena_napi, napi);
3685 if (likely(READ_ONCE(ena_napi->first_interrupt)))
3688 if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3691 rx_ring->no_interrupt_event_cnt++;
3693 if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3694 netif_err(adapter, rx_err, adapter->netdev,
3695 "Potential MSIX issue on Rx side Queue = %d. Reset the device\n",
3697 adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3698 smp_mb__before_atomic();
3699 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3706 static int check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
3707 struct ena_ring *tx_ring)
3709 struct ena_napi *ena_napi = container_of(tx_ring->napi, struct ena_napi, napi);
3710 unsigned int time_since_last_napi;
3711 unsigned int missing_tx_comp_to;
3712 bool is_tx_comp_time_expired;
3713 struct ena_tx_buffer *tx_buf;
3714 unsigned long last_jiffies;
3718 for (i = 0; i < tx_ring->ring_size; i++) {
3719 tx_buf = &tx_ring->tx_buffer_info[i];
3720 last_jiffies = tx_buf->last_jiffies;
3722 if (last_jiffies == 0)
3723 /* no pending Tx at this location */
3726 is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies +
3727 2 * adapter->missing_tx_completion_to);
3729 if (unlikely(!READ_ONCE(ena_napi->first_interrupt) && is_tx_comp_time_expired)) {
3730 /* If after graceful period interrupt is still not
3731 * received, we schedule a reset
3733 netif_err(adapter, tx_err, adapter->netdev,
3734 "Potential MSIX issue on Tx side Queue = %d. Reset the device\n",
3736 adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3737 smp_mb__before_atomic();
3738 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3742 is_tx_comp_time_expired = time_is_before_jiffies(last_jiffies +
3743 adapter->missing_tx_completion_to);
3745 if (unlikely(is_tx_comp_time_expired)) {
3746 if (!tx_buf->print_once) {
3747 time_since_last_napi = jiffies_to_usecs(jiffies - tx_ring->tx_stats.last_napi_jiffies);
3748 missing_tx_comp_to = jiffies_to_msecs(adapter->missing_tx_completion_to);
3749 netif_notice(adapter, tx_err, adapter->netdev,
3750 "Found a Tx that wasn't completed on time, qid %d, index %d. %u usecs have passed since last napi execution. Missing Tx timeout value %u msecs\n",
3751 tx_ring->qid, i, time_since_last_napi, missing_tx_comp_to);
3754 tx_buf->print_once = 1;
3759 if (unlikely(missed_tx > adapter->missing_tx_completion_threshold)) {
3760 netif_err(adapter, tx_err, adapter->netdev,
3761 "The number of lost tx completions is above the threshold (%d > %d). Reset the device\n",
3763 adapter->missing_tx_completion_threshold);
3764 adapter->reset_reason =
3765 ENA_REGS_RESET_MISS_TX_CMPL;
3766 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3770 ena_increase_stat(&tx_ring->tx_stats.missed_tx, missed_tx,
3776 static void check_for_missing_completions(struct ena_adapter *adapter)
3778 struct ena_ring *tx_ring;
3779 struct ena_ring *rx_ring;
3783 io_queue_count = adapter->xdp_num_queues + adapter->num_io_queues;
3784 /* Make sure the driver doesn't turn the device in other process */
3787 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3790 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
3793 if (adapter->missing_tx_completion_to == ENA_HW_HINTS_NO_TIMEOUT)
3796 budget = ENA_MONITORED_TX_QUEUES;
3798 for (i = adapter->last_monitored_tx_qid; i < io_queue_count; i++) {
3799 tx_ring = &adapter->tx_ring[i];
3800 rx_ring = &adapter->rx_ring[i];
3802 rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3806 rc = !ENA_IS_XDP_INDEX(adapter, i) ?
3807 check_for_rx_interrupt_queue(adapter, rx_ring) : 0;
3816 adapter->last_monitored_tx_qid = i % io_queue_count;
3819 /* trigger napi schedule after 2 consecutive detections */
3820 #define EMPTY_RX_REFILL 2
3821 /* For the rare case where the device runs out of Rx descriptors and the
3822 * napi handler failed to refill new Rx descriptors (due to a lack of memory
3824 * This case will lead to a deadlock:
3825 * The device won't send interrupts since all the new Rx packets will be dropped
3826 * The napi handler won't allocate new Rx descriptors so the device will be
3827 * able to send new packets.
3829 * This scenario can happen when the kernel's vm.min_free_kbytes is too small.
3830 * It is recommended to have at least 512MB, with a minimum of 128MB for
3831 * constrained environment).
3833 * When such a situation is detected - Reschedule napi
3835 static void check_for_empty_rx_ring(struct ena_adapter *adapter)
3837 struct ena_ring *rx_ring;
3838 int i, refill_required;
3840 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
3843 if (test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))
3846 for (i = 0; i < adapter->num_io_queues; i++) {
3847 rx_ring = &adapter->rx_ring[i];
3849 refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
3850 if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3851 rx_ring->empty_rx_queue++;
3853 if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL) {
3854 ena_increase_stat(&rx_ring->rx_stats.empty_rx_ring, 1,
3857 netif_err(adapter, drv, adapter->netdev,
3858 "Trigger refill for ring %d\n", i);
3860 napi_schedule(rx_ring->napi);
3861 rx_ring->empty_rx_queue = 0;
3864 rx_ring->empty_rx_queue = 0;
3869 /* Check for keep alive expiration */
3870 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
3872 unsigned long keep_alive_expired;
3874 if (!adapter->wd_state)
3877 if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
3880 keep_alive_expired = adapter->last_keep_alive_jiffies +
3881 adapter->keep_alive_timeout;
3882 if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
3883 netif_err(adapter, drv, adapter->netdev,
3884 "Keep alive watchdog timeout.\n");
3885 ena_increase_stat(&adapter->dev_stats.wd_expired, 1,
3887 adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
3888 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3892 static void check_for_admin_com_state(struct ena_adapter *adapter)
3894 if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
3895 netif_err(adapter, drv, adapter->netdev,
3896 "ENA admin queue is not in running state!\n");
3897 ena_increase_stat(&adapter->dev_stats.admin_q_pause, 1,
3899 adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
3900 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
3904 static void ena_update_hints(struct ena_adapter *adapter,
3905 struct ena_admin_ena_hw_hints *hints)
3907 struct net_device *netdev = adapter->netdev;
3909 if (hints->admin_completion_tx_timeout)
3910 adapter->ena_dev->admin_queue.completion_timeout =
3911 hints->admin_completion_tx_timeout * 1000;
3913 if (hints->mmio_read_timeout)
3914 /* convert to usec */
3915 adapter->ena_dev->mmio_read.reg_read_to =
3916 hints->mmio_read_timeout * 1000;
3918 if (hints->missed_tx_completion_count_threshold_to_reset)
3919 adapter->missing_tx_completion_threshold =
3920 hints->missed_tx_completion_count_threshold_to_reset;
3922 if (hints->missing_tx_completion_timeout) {
3923 if (hints->missing_tx_completion_timeout == ENA_HW_HINTS_NO_TIMEOUT)
3924 adapter->missing_tx_completion_to = ENA_HW_HINTS_NO_TIMEOUT;
3926 adapter->missing_tx_completion_to =
3927 msecs_to_jiffies(hints->missing_tx_completion_timeout);
3930 if (hints->netdev_wd_timeout)
3931 netdev->watchdog_timeo = msecs_to_jiffies(hints->netdev_wd_timeout);
3933 if (hints->driver_watchdog_timeout) {
3934 if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
3935 adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
3937 adapter->keep_alive_timeout =
3938 msecs_to_jiffies(hints->driver_watchdog_timeout);
3942 static void ena_update_host_info(struct ena_admin_host_info *host_info,
3943 struct net_device *netdev)
3945 host_info->supported_network_features[0] =
3946 netdev->features & GENMASK_ULL(31, 0);
3947 host_info->supported_network_features[1] =
3948 (netdev->features & GENMASK_ULL(63, 32)) >> 32;
3951 static void ena_timer_service(struct timer_list *t)
3953 struct ena_adapter *adapter = from_timer(adapter, t, timer_service);
3954 u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
3955 struct ena_admin_host_info *host_info =
3956 adapter->ena_dev->host_attr.host_info;
3958 check_for_missing_keep_alive(adapter);
3960 check_for_admin_com_state(adapter);
3962 check_for_missing_completions(adapter);
3964 check_for_empty_rx_ring(adapter);
3967 ena_dump_stats_to_buf(adapter, debug_area);
3970 ena_update_host_info(host_info, adapter->netdev);
3972 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
3973 netif_err(adapter, drv, adapter->netdev,
3974 "Trigger reset is on\n");
3975 ena_dump_stats_to_dmesg(adapter);
3976 queue_work(ena_wq, &adapter->reset_task);
3980 /* Reset the timer */
3981 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
3984 static u32 ena_calc_max_io_queue_num(struct pci_dev *pdev,
3985 struct ena_com_dev *ena_dev,
3986 struct ena_com_dev_get_features_ctx *get_feat_ctx)
3988 u32 io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
3990 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
3991 struct ena_admin_queue_ext_feature_fields *max_queue_ext =
3992 &get_feat_ctx->max_queue_ext.max_queue_ext;
3993 io_rx_num = min_t(u32, max_queue_ext->max_rx_sq_num,
3994 max_queue_ext->max_rx_cq_num);
3996 io_tx_sq_num = max_queue_ext->max_tx_sq_num;
3997 io_tx_cq_num = max_queue_ext->max_tx_cq_num;
3999 struct ena_admin_queue_feature_desc *max_queues =
4000 &get_feat_ctx->max_queues;
4001 io_tx_sq_num = max_queues->max_sq_num;
4002 io_tx_cq_num = max_queues->max_cq_num;
4003 io_rx_num = min_t(u32, io_tx_sq_num, io_tx_cq_num);
4006 /* In case of LLQ use the llq fields for the tx SQ/CQ */
4007 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
4008 io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
4010 max_num_io_queues = min_t(u32, num_online_cpus(), ENA_MAX_NUM_IO_QUEUES);
4011 max_num_io_queues = min_t(u32, max_num_io_queues, io_rx_num);
4012 max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_sq_num);
4013 max_num_io_queues = min_t(u32, max_num_io_queues, io_tx_cq_num);
4014 /* 1 IRQ for mgmnt and 1 IRQs for each IO direction */
4015 max_num_io_queues = min_t(u32, max_num_io_queues, pci_msix_vec_count(pdev) - 1);
4016 if (unlikely(!max_num_io_queues)) {
4017 dev_err(&pdev->dev, "The device doesn't have io queues\n");
4021 return max_num_io_queues;
4024 static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
4025 struct net_device *netdev)
4027 netdev_features_t dev_features = 0;
4029 /* Set offload features */
4030 if (feat->offload.tx &
4031 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
4032 dev_features |= NETIF_F_IP_CSUM;
4034 if (feat->offload.tx &
4035 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
4036 dev_features |= NETIF_F_IPV6_CSUM;
4038 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
4039 dev_features |= NETIF_F_TSO;
4041 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
4042 dev_features |= NETIF_F_TSO6;
4044 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
4045 dev_features |= NETIF_F_TSO_ECN;
4047 if (feat->offload.rx_supported &
4048 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
4049 dev_features |= NETIF_F_RXCSUM;
4051 if (feat->offload.rx_supported &
4052 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
4053 dev_features |= NETIF_F_RXCSUM;
4061 netdev->hw_features |= netdev->features;
4062 netdev->vlan_features |= netdev->features;
4065 static void ena_set_conf_feat_params(struct ena_adapter *adapter,
4066 struct ena_com_dev_get_features_ctx *feat)
4068 struct net_device *netdev = adapter->netdev;
4070 /* Copy mac address */
4071 if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
4072 eth_hw_addr_random(netdev);
4073 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
4075 ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
4076 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
4079 /* Set offload features */
4080 ena_set_dev_offloads(feat, netdev);
4082 adapter->max_mtu = feat->dev_attr.max_mtu;
4083 netdev->max_mtu = adapter->max_mtu;
4084 netdev->min_mtu = ENA_MIN_MTU;
4087 static int ena_rss_init_default(struct ena_adapter *adapter)
4089 struct ena_com_dev *ena_dev = adapter->ena_dev;
4090 struct device *dev = &adapter->pdev->dev;
4094 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
4096 dev_err(dev, "Cannot init indirect table\n");
4100 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
4101 val = ethtool_rxfh_indir_default(i, adapter->num_io_queues);
4102 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
4103 ENA_IO_RXQ_IDX(val));
4104 if (unlikely(rc && (rc != -EOPNOTSUPP))) {
4105 dev_err(dev, "Cannot fill indirect table\n");
4106 goto err_fill_indir;
4110 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_TOEPLITZ, NULL,
4111 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
4112 if (unlikely(rc && (rc != -EOPNOTSUPP))) {
4113 dev_err(dev, "Cannot fill hash function\n");
4114 goto err_fill_indir;
4117 rc = ena_com_set_default_hash_ctrl(ena_dev);
4118 if (unlikely(rc && (rc != -EOPNOTSUPP))) {
4119 dev_err(dev, "Cannot fill hash control\n");
4120 goto err_fill_indir;
4126 ena_com_rss_destroy(ena_dev);
4132 static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
4134 int release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
4136 pci_release_selected_regions(pdev, release_bars);
4140 static int ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx)
4142 struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
4143 struct ena_com_dev *ena_dev = ctx->ena_dev;
4144 u32 tx_queue_size = ENA_DEFAULT_RING_SIZE;
4145 u32 rx_queue_size = ENA_DEFAULT_RING_SIZE;
4146 u32 max_tx_queue_size;
4147 u32 max_rx_queue_size;
4149 if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
4150 struct ena_admin_queue_ext_feature_fields *max_queue_ext =
4151 &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
4152 max_rx_queue_size = min_t(u32, max_queue_ext->max_rx_cq_depth,
4153 max_queue_ext->max_rx_sq_depth);
4154 max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
4156 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
4157 max_tx_queue_size = min_t(u32, max_tx_queue_size,
4158 llq->max_llq_depth);
4160 max_tx_queue_size = min_t(u32, max_tx_queue_size,
4161 max_queue_ext->max_tx_sq_depth);
4163 ctx->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
4164 max_queue_ext->max_per_packet_tx_descs);
4165 ctx->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
4166 max_queue_ext->max_per_packet_rx_descs);
4168 struct ena_admin_queue_feature_desc *max_queues =
4169 &ctx->get_feat_ctx->max_queues;
4170 max_rx_queue_size = min_t(u32, max_queues->max_cq_depth,
4171 max_queues->max_sq_depth);
4172 max_tx_queue_size = max_queues->max_cq_depth;
4174 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
4175 max_tx_queue_size = min_t(u32, max_tx_queue_size,
4176 llq->max_llq_depth);
4178 max_tx_queue_size = min_t(u32, max_tx_queue_size,
4179 max_queues->max_sq_depth);
4181 ctx->max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
4182 max_queues->max_packet_tx_descs);
4183 ctx->max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
4184 max_queues->max_packet_rx_descs);
4187 max_tx_queue_size = rounddown_pow_of_two(max_tx_queue_size);
4188 max_rx_queue_size = rounddown_pow_of_two(max_rx_queue_size);
4190 tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE,
4192 rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE,
4195 tx_queue_size = rounddown_pow_of_two(tx_queue_size);
4196 rx_queue_size = rounddown_pow_of_two(rx_queue_size);
4198 ctx->max_tx_queue_size = max_tx_queue_size;
4199 ctx->max_rx_queue_size = max_rx_queue_size;
4200 ctx->tx_queue_size = tx_queue_size;
4201 ctx->rx_queue_size = rx_queue_size;
4206 /* ena_probe - Device Initialization Routine
4207 * @pdev: PCI device information struct
4208 * @ent: entry in ena_pci_tbl
4210 * Returns 0 on success, negative on failure
4212 * ena_probe initializes an adapter identified by a pci_dev structure.
4213 * The OS initialization, configuring of the adapter private structure,
4214 * and a hardware reset occur.
4216 static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4218 struct ena_calc_queue_size_ctx calc_queue_ctx = {};
4219 struct ena_com_dev_get_features_ctx get_feat_ctx;
4220 struct ena_com_dev *ena_dev = NULL;
4221 struct ena_adapter *adapter;
4222 struct net_device *netdev;
4223 static int adapters_found;
4224 u32 max_num_io_queues;
4228 dev_dbg(&pdev->dev, "%s\n", __func__);
4230 rc = pci_enable_device_mem(pdev);
4232 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
4236 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(ENA_MAX_PHYS_ADDR_SIZE_BITS));
4238 dev_err(&pdev->dev, "dma_set_mask_and_coherent failed %d\n", rc);
4239 goto err_disable_device;
4242 pci_set_master(pdev);
4244 ena_dev = vzalloc(sizeof(*ena_dev));
4247 goto err_disable_device;
4250 bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
4251 rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
4253 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
4255 goto err_free_ena_dev;
4258 ena_dev->reg_bar = devm_ioremap(&pdev->dev,
4259 pci_resource_start(pdev, ENA_REG_BAR),
4260 pci_resource_len(pdev, ENA_REG_BAR));
4261 if (!ena_dev->reg_bar) {
4262 dev_err(&pdev->dev, "Failed to remap regs bar\n");
4264 goto err_free_region;
4267 ena_dev->ena_min_poll_delay_us = ENA_ADMIN_POLL_DELAY_US;
4269 ena_dev->dmadev = &pdev->dev;
4271 netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), ENA_MAX_RINGS);
4273 dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
4275 goto err_free_region;
4278 SET_NETDEV_DEV(netdev, &pdev->dev);
4279 adapter = netdev_priv(netdev);
4280 adapter->ena_dev = ena_dev;
4281 adapter->netdev = netdev;
4282 adapter->pdev = pdev;
4283 adapter->msg_enable = DEFAULT_MSG_ENABLE;
4285 ena_dev->net_device = netdev;
4287 pci_set_drvdata(pdev, adapter);
4289 rc = ena_device_init(ena_dev, pdev, &get_feat_ctx, &wd_state);
4291 dev_err(&pdev->dev, "ENA device init failed\n");
4294 goto err_netdev_destroy;
4297 rc = ena_map_llq_mem_bar(pdev, ena_dev, bars);
4299 dev_err(&pdev->dev, "ENA llq bar mapping failed\n");
4300 goto err_device_destroy;
4303 calc_queue_ctx.ena_dev = ena_dev;
4304 calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
4305 calc_queue_ctx.pdev = pdev;
4307 /* Initial TX and RX interrupt delay. Assumes 1 usec granularity.
4308 * Updated during device initialization with the real granularity
4310 ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
4311 ena_dev->intr_moder_rx_interval = ENA_INTR_INITIAL_RX_INTERVAL_USECS;
4312 ena_dev->intr_delay_resolution = ENA_DEFAULT_INTR_DELAY_RESOLUTION;
4313 max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev, &get_feat_ctx);
4314 rc = ena_calc_io_queue_size(&calc_queue_ctx);
4315 if (rc || !max_num_io_queues) {
4317 goto err_device_destroy;
4320 ena_set_conf_feat_params(adapter, &get_feat_ctx);
4322 adapter->reset_reason = ENA_REGS_RESET_NORMAL;
4324 adapter->requested_tx_ring_size = calc_queue_ctx.tx_queue_size;
4325 adapter->requested_rx_ring_size = calc_queue_ctx.rx_queue_size;
4326 adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size;
4327 adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size;
4328 adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
4329 adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
4331 adapter->num_io_queues = max_num_io_queues;
4332 adapter->max_num_io_queues = max_num_io_queues;
4333 adapter->last_monitored_tx_qid = 0;
4335 adapter->xdp_first_ring = 0;
4336 adapter->xdp_num_queues = 0;
4338 adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
4339 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
4340 adapter->disable_meta_caching =
4341 !!(get_feat_ctx.llq.accel_mode.u.get.supported_flags &
4342 BIT(ENA_ADMIN_DISABLE_META_CACHING));
4344 adapter->wd_state = wd_state;
4346 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
4348 rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
4351 "Failed to query interrupt moderation feature\n");
4352 goto err_device_destroy;
4354 ena_init_io_rings(adapter,
4356 adapter->xdp_num_queues +
4357 adapter->num_io_queues);
4359 netdev->netdev_ops = &ena_netdev_ops;
4360 netdev->watchdog_timeo = TX_TIMEOUT;
4361 ena_set_ethtool_ops(netdev);
4363 netdev->priv_flags |= IFF_UNICAST_FLT;
4365 u64_stats_init(&adapter->syncp);
4367 rc = ena_enable_msix_and_set_admin_interrupts(adapter);
4370 "Failed to enable and set the admin interrupts\n");
4371 goto err_worker_destroy;
4373 rc = ena_rss_init_default(adapter);
4374 if (rc && (rc != -EOPNOTSUPP)) {
4375 dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
4379 ena_config_debug_area(adapter);
4381 if (!ena_update_hw_stats(adapter))
4382 adapter->eni_stats_supported = true;
4384 adapter->eni_stats_supported = false;
4386 memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
4388 netif_carrier_off(netdev);
4390 rc = register_netdev(netdev);
4392 dev_err(&pdev->dev, "Cannot register net device\n");
4396 INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
4398 adapter->last_keep_alive_jiffies = jiffies;
4399 adapter->keep_alive_timeout = ENA_DEVICE_KALIVE_TIMEOUT;
4400 adapter->missing_tx_completion_to = TX_TIMEOUT;
4401 adapter->missing_tx_completion_threshold = MAX_NUM_OF_TIMEOUTED_PACKETS;
4403 ena_update_hints(adapter, &get_feat_ctx.hw_hints);
4405 timer_setup(&adapter->timer_service, ena_timer_service, 0);
4406 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
4408 dev_info(&pdev->dev,
4409 "%s found at mem %lx, mac addr %pM\n",
4410 DEVICE_NAME, (long)pci_resource_start(pdev, 0),
4413 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
4420 ena_com_delete_debug_area(ena_dev);
4421 ena_com_rss_destroy(ena_dev);
4423 ena_com_dev_reset(ena_dev, ENA_REGS_RESET_INIT_ERR);
4424 /* stop submitting admin commands on a device that was reset */
4425 ena_com_set_admin_running_state(ena_dev, false);
4426 ena_free_mgmnt_irq(adapter);
4427 ena_disable_msix(adapter);
4429 del_timer(&adapter->timer_service);
4431 ena_com_delete_host_info(ena_dev);
4432 ena_com_admin_destroy(ena_dev);
4434 free_netdev(netdev);
4436 ena_release_bars(ena_dev, pdev);
4440 pci_disable_device(pdev);
4444 /*****************************************************************************/
4446 /* __ena_shutoff - Helper used in both PCI remove/shutdown routines
4447 * @pdev: PCI device information struct
4448 * @shutdown: Is it a shutdown operation? If false, means it is a removal
4450 * __ena_shutoff is a helper routine that does the real work on shutdown and
4451 * removal paths; the difference between those paths is with regards to whether
4452 * dettach or unregister the netdevice.
4454 static void __ena_shutoff(struct pci_dev *pdev, bool shutdown)
4456 struct ena_adapter *adapter = pci_get_drvdata(pdev);
4457 struct ena_com_dev *ena_dev;
4458 struct net_device *netdev;
4460 ena_dev = adapter->ena_dev;
4461 netdev = adapter->netdev;
4463 #ifdef CONFIG_RFS_ACCEL
4464 if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
4465 free_irq_cpu_rmap(netdev->rx_cpu_rmap);
4466 netdev->rx_cpu_rmap = NULL;
4468 #endif /* CONFIG_RFS_ACCEL */
4470 /* Make sure timer and reset routine won't be called after
4471 * freeing device resources.
4473 del_timer_sync(&adapter->timer_service);
4474 cancel_work_sync(&adapter->reset_task);
4476 rtnl_lock(); /* lock released inside the below if-else block */
4477 adapter->reset_reason = ENA_REGS_RESET_SHUTDOWN;
4478 ena_destroy_device(adapter, true);
4480 netif_device_detach(netdev);
4485 unregister_netdev(netdev);
4486 free_netdev(netdev);
4489 ena_com_rss_destroy(ena_dev);
4491 ena_com_delete_debug_area(ena_dev);
4493 ena_com_delete_host_info(ena_dev);
4495 ena_release_bars(ena_dev, pdev);
4497 pci_disable_device(pdev);
4502 /* ena_remove - Device Removal Routine
4503 * @pdev: PCI device information struct
4505 * ena_remove is called by the PCI subsystem to alert the driver
4506 * that it should release a PCI device.
4509 static void ena_remove(struct pci_dev *pdev)
4511 __ena_shutoff(pdev, false);
4514 /* ena_shutdown - Device Shutdown Routine
4515 * @pdev: PCI device information struct
4517 * ena_shutdown is called by the PCI subsystem to alert the driver that
4518 * a shutdown/reboot (or kexec) is happening and device must be disabled.
4521 static void ena_shutdown(struct pci_dev *pdev)
4523 __ena_shutoff(pdev, true);
4526 /* ena_suspend - PM suspend callback
4527 * @dev_d: Device information struct
4529 static int __maybe_unused ena_suspend(struct device *dev_d)
4531 struct pci_dev *pdev = to_pci_dev(dev_d);
4532 struct ena_adapter *adapter = pci_get_drvdata(pdev);
4534 ena_increase_stat(&adapter->dev_stats.suspend, 1, &adapter->syncp);
4537 if (unlikely(test_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
4539 "Ignoring device reset request as the device is being suspended\n");
4540 clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
4542 ena_destroy_device(adapter, true);
4547 /* ena_resume - PM resume callback
4548 * @dev_d: Device information struct
4550 static int __maybe_unused ena_resume(struct device *dev_d)
4552 struct ena_adapter *adapter = dev_get_drvdata(dev_d);
4555 ena_increase_stat(&adapter->dev_stats.resume, 1, &adapter->syncp);
4558 rc = ena_restore_device(adapter);
4563 static SIMPLE_DEV_PM_OPS(ena_pm_ops, ena_suspend, ena_resume);
4565 static struct pci_driver ena_pci_driver = {
4566 .name = DRV_MODULE_NAME,
4567 .id_table = ena_pci_tbl,
4569 .remove = ena_remove,
4570 .shutdown = ena_shutdown,
4571 .driver.pm = &ena_pm_ops,
4572 .sriov_configure = pci_sriov_configure_simple,
4575 static int __init ena_init(void)
4577 ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
4579 pr_err("Failed to create workqueue\n");
4583 return pci_register_driver(&ena_pci_driver);
4586 static void __exit ena_cleanup(void)
4588 pci_unregister_driver(&ena_pci_driver);
4591 destroy_workqueue(ena_wq);
4596 /******************************************************************************
4597 ******************************** AENQ Handlers *******************************
4598 *****************************************************************************/
4599 /* ena_update_on_link_change:
4600 * Notify the network interface about the change in link status
4602 static void ena_update_on_link_change(void *adapter_data,
4603 struct ena_admin_aenq_entry *aenq_e)
4605 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4606 struct ena_admin_aenq_link_change_desc *aenq_desc =
4607 (struct ena_admin_aenq_link_change_desc *)aenq_e;
4608 int status = aenq_desc->flags &
4609 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
4612 netif_dbg(adapter, ifup, adapter->netdev, "%s\n", __func__);
4613 set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
4614 if (!test_bit(ENA_FLAG_ONGOING_RESET, &adapter->flags))
4615 netif_carrier_on(adapter->netdev);
4617 clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
4618 netif_carrier_off(adapter->netdev);
4622 static void ena_keep_alive_wd(void *adapter_data,
4623 struct ena_admin_aenq_entry *aenq_e)
4625 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4626 struct ena_admin_aenq_keep_alive_desc *desc;
4630 desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
4631 adapter->last_keep_alive_jiffies = jiffies;
4633 rx_drops = ((u64)desc->rx_drops_high << 32) | desc->rx_drops_low;
4634 tx_drops = ((u64)desc->tx_drops_high << 32) | desc->tx_drops_low;
4636 u64_stats_update_begin(&adapter->syncp);
4637 /* These stats are accumulated by the device, so the counters indicate
4638 * all drops since last reset.
4640 adapter->dev_stats.rx_drops = rx_drops;
4641 adapter->dev_stats.tx_drops = tx_drops;
4642 u64_stats_update_end(&adapter->syncp);
4645 static void ena_notification(void *adapter_data,
4646 struct ena_admin_aenq_entry *aenq_e)
4648 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4649 struct ena_admin_ena_hw_hints *hints;
4651 WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
4652 "Invalid group(%x) expected %x\n",
4653 aenq_e->aenq_common_desc.group,
4654 ENA_ADMIN_NOTIFICATION);
4656 switch (aenq_e->aenq_common_desc.syndrome) {
4657 case ENA_ADMIN_UPDATE_HINTS:
4658 hints = (struct ena_admin_ena_hw_hints *)
4659 (&aenq_e->inline_data_w4);
4660 ena_update_hints(adapter, hints);
4663 netif_err(adapter, drv, adapter->netdev,
4664 "Invalid aenq notification link state %d\n",
4665 aenq_e->aenq_common_desc.syndrome);
4669 /* This handler will called for unknown event group or unimplemented handlers*/
4670 static void unimplemented_aenq_handler(void *data,
4671 struct ena_admin_aenq_entry *aenq_e)
4673 struct ena_adapter *adapter = (struct ena_adapter *)data;
4675 netif_err(adapter, drv, adapter->netdev,
4676 "Unknown event was received or event with unimplemented handler\n");
4679 static struct ena_aenq_handlers aenq_handlers = {
4681 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
4682 [ENA_ADMIN_NOTIFICATION] = ena_notification,
4683 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
4685 .unimplemented_handler = unimplemented_aenq_handler
4688 module_init(ena_init);
4689 module_exit(ena_cleanup);