2 * Copyright 2015 Amazon.com, Inc. or its affiliates.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #ifdef CONFIG_RFS_ACCEL
36 #include <linux/cpu_rmap.h>
37 #endif /* CONFIG_RFS_ACCEL */
38 #include <linux/ethtool.h>
39 #include <linux/if_vlan.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/moduleparam.h>
43 #include <linux/numa.h>
44 #include <linux/pci.h>
45 #include <linux/utsname.h>
46 #include <linux/version.h>
47 #include <linux/vmalloc.h>
50 #include "ena_netdev.h"
51 #include "ena_pci_id_tbl.h"
53 static char version[] = DEVICE_NAME " v" DRV_MODULE_VERSION "\n";
55 MODULE_AUTHOR("Amazon.com, Inc. or its affiliates");
56 MODULE_DESCRIPTION(DEVICE_NAME);
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(DRV_MODULE_VERSION);
60 /* Time in jiffies before concluding the transmitter is hung. */
61 #define TX_TIMEOUT (5 * HZ)
63 #define ENA_NAPI_BUDGET 64
65 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | \
66 NETIF_MSG_TX_DONE | NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
67 static int debug = -1;
68 module_param(debug, int, 0);
69 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
71 static struct ena_aenq_handlers aenq_handlers;
73 static struct workqueue_struct *ena_wq;
75 MODULE_DEVICE_TABLE(pci, ena_pci_tbl);
77 static int ena_rss_init_default(struct ena_adapter *adapter);
79 static void ena_tx_timeout(struct net_device *dev)
81 struct ena_adapter *adapter = netdev_priv(dev);
83 u64_stats_update_begin(&adapter->syncp);
84 adapter->dev_stats.tx_timeout++;
85 u64_stats_update_end(&adapter->syncp);
87 netif_err(adapter, tx_err, dev, "Transmit time out\n");
89 /* Change the state of the device to trigger reset */
90 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
93 static void update_rx_ring_mtu(struct ena_adapter *adapter, int mtu)
97 for (i = 0; i < adapter->num_queues; i++)
98 adapter->rx_ring[i].mtu = mtu;
101 static int ena_change_mtu(struct net_device *dev, int new_mtu)
103 struct ena_adapter *adapter = netdev_priv(dev);
106 if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
107 netif_err(adapter, drv, dev,
108 "Invalid MTU setting. new_mtu: %d\n", new_mtu);
113 ret = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
115 netif_dbg(adapter, drv, dev, "set MTU to %d\n", new_mtu);
116 update_rx_ring_mtu(adapter, new_mtu);
119 netif_err(adapter, drv, dev, "Failed to set MTU to %d\n",
126 static int ena_init_rx_cpu_rmap(struct ena_adapter *adapter)
128 #ifdef CONFIG_RFS_ACCEL
132 adapter->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(adapter->num_queues);
133 if (!adapter->netdev->rx_cpu_rmap)
135 for (i = 0; i < adapter->num_queues; i++) {
136 int irq_idx = ENA_IO_IRQ_IDX(i);
138 rc = irq_cpu_rmap_add(adapter->netdev->rx_cpu_rmap,
139 adapter->msix_entries[irq_idx].vector);
141 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
142 adapter->netdev->rx_cpu_rmap = NULL;
146 #endif /* CONFIG_RFS_ACCEL */
150 static void ena_init_io_rings_common(struct ena_adapter *adapter,
151 struct ena_ring *ring, u16 qid)
154 ring->pdev = adapter->pdev;
155 ring->dev = &adapter->pdev->dev;
156 ring->netdev = adapter->netdev;
157 ring->napi = &adapter->ena_napi[qid].napi;
158 ring->adapter = adapter;
159 ring->ena_dev = adapter->ena_dev;
160 ring->per_napi_packets = 0;
161 ring->per_napi_bytes = 0;
163 u64_stats_init(&ring->syncp);
166 static void ena_init_io_rings(struct ena_adapter *adapter)
168 struct ena_com_dev *ena_dev;
169 struct ena_ring *txr, *rxr;
172 ena_dev = adapter->ena_dev;
174 for (i = 0; i < adapter->num_queues; i++) {
175 txr = &adapter->tx_ring[i];
176 rxr = &adapter->rx_ring[i];
178 /* TX/RX common ring state */
179 ena_init_io_rings_common(adapter, txr, i);
180 ena_init_io_rings_common(adapter, rxr, i);
182 /* TX specific ring state */
183 txr->ring_size = adapter->tx_ring_size;
184 txr->tx_max_header_size = ena_dev->tx_max_header_size;
185 txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
186 txr->sgl_size = adapter->max_tx_sgl_size;
187 txr->smoothed_interval =
188 ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
190 /* RX specific ring state */
191 rxr->ring_size = adapter->rx_ring_size;
192 rxr->rx_copybreak = adapter->rx_copybreak;
193 rxr->sgl_size = adapter->max_rx_sgl_size;
194 rxr->smoothed_interval =
195 ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
199 /* ena_setup_tx_resources - allocate I/O Tx resources (Descriptors)
200 * @adapter: network interface device structure
203 * Return 0 on success, negative on failure
205 static int ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
207 struct ena_ring *tx_ring = &adapter->tx_ring[qid];
208 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
211 if (tx_ring->tx_buffer_info) {
212 netif_err(adapter, ifup,
213 adapter->netdev, "tx_buffer_info info is not NULL");
217 size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
218 node = cpu_to_node(ena_irq->cpu);
220 tx_ring->tx_buffer_info = vzalloc_node(size, node);
221 if (!tx_ring->tx_buffer_info) {
222 tx_ring->tx_buffer_info = vzalloc(size);
223 if (!tx_ring->tx_buffer_info)
227 size = sizeof(u16) * tx_ring->ring_size;
228 tx_ring->free_tx_ids = vzalloc_node(size, node);
229 if (!tx_ring->free_tx_ids) {
230 tx_ring->free_tx_ids = vzalloc(size);
231 if (!tx_ring->free_tx_ids) {
232 vfree(tx_ring->tx_buffer_info);
237 /* Req id ring for TX out of order completions */
238 for (i = 0; i < tx_ring->ring_size; i++)
239 tx_ring->free_tx_ids[i] = i;
241 /* Reset tx statistics */
242 memset(&tx_ring->tx_stats, 0x0, sizeof(tx_ring->tx_stats));
244 tx_ring->next_to_use = 0;
245 tx_ring->next_to_clean = 0;
246 tx_ring->cpu = ena_irq->cpu;
250 /* ena_free_tx_resources - Free I/O Tx Resources per Queue
251 * @adapter: network interface device structure
254 * Free all transmit software resources
256 static void ena_free_tx_resources(struct ena_adapter *adapter, int qid)
258 struct ena_ring *tx_ring = &adapter->tx_ring[qid];
260 vfree(tx_ring->tx_buffer_info);
261 tx_ring->tx_buffer_info = NULL;
263 vfree(tx_ring->free_tx_ids);
264 tx_ring->free_tx_ids = NULL;
267 /* ena_setup_all_tx_resources - allocate I/O Tx queues resources for All queues
268 * @adapter: private structure
270 * Return 0 on success, negative on failure
272 static int ena_setup_all_tx_resources(struct ena_adapter *adapter)
276 for (i = 0; i < adapter->num_queues; i++) {
277 rc = ena_setup_tx_resources(adapter, i);
286 netif_err(adapter, ifup, adapter->netdev,
287 "Tx queue %d: allocation failed\n", i);
289 /* rewind the index freeing the rings as we go */
291 ena_free_tx_resources(adapter, i);
295 /* ena_free_all_io_tx_resources - Free I/O Tx Resources for All Queues
296 * @adapter: board private structure
298 * Free all transmit software resources
300 static void ena_free_all_io_tx_resources(struct ena_adapter *adapter)
304 for (i = 0; i < adapter->num_queues; i++)
305 ena_free_tx_resources(adapter, i);
308 /* ena_setup_rx_resources - allocate I/O Rx resources (Descriptors)
309 * @adapter: network interface device structure
312 * Returns 0 on success, negative on failure
314 static int ena_setup_rx_resources(struct ena_adapter *adapter,
317 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
318 struct ena_irq *ena_irq = &adapter->irq_tbl[ENA_IO_IRQ_IDX(qid)];
321 if (rx_ring->rx_buffer_info) {
322 netif_err(adapter, ifup, adapter->netdev,
323 "rx_buffer_info is not NULL");
327 /* alloc extra element so in rx path
328 * we can always prefetch rx_info + 1
330 size = sizeof(struct ena_rx_buffer) * (rx_ring->ring_size + 1);
331 node = cpu_to_node(ena_irq->cpu);
333 rx_ring->rx_buffer_info = vzalloc_node(size, node);
334 if (!rx_ring->rx_buffer_info) {
335 rx_ring->rx_buffer_info = vzalloc(size);
336 if (!rx_ring->rx_buffer_info)
340 /* Reset rx statistics */
341 memset(&rx_ring->rx_stats, 0x0, sizeof(rx_ring->rx_stats));
343 rx_ring->next_to_clean = 0;
344 rx_ring->next_to_use = 0;
345 rx_ring->cpu = ena_irq->cpu;
350 /* ena_free_rx_resources - Free I/O Rx Resources
351 * @adapter: network interface device structure
354 * Free all receive software resources
356 static void ena_free_rx_resources(struct ena_adapter *adapter,
359 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
361 vfree(rx_ring->rx_buffer_info);
362 rx_ring->rx_buffer_info = NULL;
365 /* ena_setup_all_rx_resources - allocate I/O Rx queues resources for all queues
366 * @adapter: board private structure
368 * Return 0 on success, negative on failure
370 static int ena_setup_all_rx_resources(struct ena_adapter *adapter)
374 for (i = 0; i < adapter->num_queues; i++) {
375 rc = ena_setup_rx_resources(adapter, i);
384 netif_err(adapter, ifup, adapter->netdev,
385 "Rx queue %d: allocation failed\n", i);
387 /* rewind the index freeing the rings as we go */
389 ena_free_rx_resources(adapter, i);
393 /* ena_free_all_io_rx_resources - Free I/O Rx Resources for All Queues
394 * @adapter: board private structure
396 * Free all receive software resources
398 static void ena_free_all_io_rx_resources(struct ena_adapter *adapter)
402 for (i = 0; i < adapter->num_queues; i++)
403 ena_free_rx_resources(adapter, i);
406 static inline int ena_alloc_rx_page(struct ena_ring *rx_ring,
407 struct ena_rx_buffer *rx_info, gfp_t gfp)
409 struct ena_com_buf *ena_buf;
413 /* if previous allocated page is not used */
414 if (unlikely(rx_info->page))
417 page = alloc_page(gfp);
418 if (unlikely(!page)) {
419 u64_stats_update_begin(&rx_ring->syncp);
420 rx_ring->rx_stats.page_alloc_fail++;
421 u64_stats_update_end(&rx_ring->syncp);
425 dma = dma_map_page(rx_ring->dev, page, 0, PAGE_SIZE,
427 if (unlikely(dma_mapping_error(rx_ring->dev, dma))) {
428 u64_stats_update_begin(&rx_ring->syncp);
429 rx_ring->rx_stats.dma_mapping_err++;
430 u64_stats_update_end(&rx_ring->syncp);
435 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
436 "alloc page %p, rx_info %p\n", page, rx_info);
438 rx_info->page = page;
439 rx_info->page_offset = 0;
440 ena_buf = &rx_info->ena_buf;
441 ena_buf->paddr = dma;
442 ena_buf->len = PAGE_SIZE;
447 static void ena_free_rx_page(struct ena_ring *rx_ring,
448 struct ena_rx_buffer *rx_info)
450 struct page *page = rx_info->page;
451 struct ena_com_buf *ena_buf = &rx_info->ena_buf;
453 if (unlikely(!page)) {
454 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
455 "Trying to free unallocated buffer\n");
459 dma_unmap_page(rx_ring->dev, ena_buf->paddr, PAGE_SIZE,
463 rx_info->page = NULL;
466 static int ena_refill_rx_bufs(struct ena_ring *rx_ring, u32 num)
472 next_to_use = rx_ring->next_to_use;
474 for (i = 0; i < num; i++) {
475 struct ena_rx_buffer *rx_info =
476 &rx_ring->rx_buffer_info[next_to_use];
478 rc = ena_alloc_rx_page(rx_ring, rx_info,
479 __GFP_COLD | GFP_ATOMIC | __GFP_COMP);
480 if (unlikely(rc < 0)) {
481 netif_warn(rx_ring->adapter, rx_err, rx_ring->netdev,
482 "failed to alloc buffer for rx queue %d\n",
486 rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
490 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
491 "failed to add buffer for rx queue %d\n",
495 next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
499 if (unlikely(i < num)) {
500 u64_stats_update_begin(&rx_ring->syncp);
501 rx_ring->rx_stats.refil_partial++;
502 u64_stats_update_end(&rx_ring->syncp);
503 netdev_warn(rx_ring->netdev,
504 "refilled rx qid %d with only %d buffers (from %d)\n",
505 rx_ring->qid, i, num);
509 /* Add memory barrier to make sure the desc were written before
513 ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
516 rx_ring->next_to_use = next_to_use;
521 static void ena_free_rx_bufs(struct ena_adapter *adapter,
524 struct ena_ring *rx_ring = &adapter->rx_ring[qid];
527 for (i = 0; i < rx_ring->ring_size; i++) {
528 struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
531 ena_free_rx_page(rx_ring, rx_info);
535 /* ena_refill_all_rx_bufs - allocate all queues Rx buffers
536 * @adapter: board private structure
539 static void ena_refill_all_rx_bufs(struct ena_adapter *adapter)
541 struct ena_ring *rx_ring;
544 for (i = 0; i < adapter->num_queues; i++) {
545 rx_ring = &adapter->rx_ring[i];
546 bufs_num = rx_ring->ring_size - 1;
547 rc = ena_refill_rx_bufs(rx_ring, bufs_num);
549 if (unlikely(rc != bufs_num))
550 netif_warn(rx_ring->adapter, rx_status, rx_ring->netdev,
551 "refilling Queue %d failed. allocated %d buffers from: %d\n",
556 static void ena_free_all_rx_bufs(struct ena_adapter *adapter)
560 for (i = 0; i < adapter->num_queues; i++)
561 ena_free_rx_bufs(adapter, i);
564 /* ena_free_tx_bufs - Free Tx Buffers per Queue
565 * @tx_ring: TX ring for which buffers be freed
567 static void ena_free_tx_bufs(struct ena_ring *tx_ring)
571 for (i = 0; i < tx_ring->ring_size; i++) {
572 struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
573 struct ena_com_buf *ena_buf;
580 netdev_notice(tx_ring->netdev,
581 "free uncompleted tx skb qid %d idx 0x%x\n",
584 ena_buf = tx_info->bufs;
585 dma_unmap_single(tx_ring->dev,
590 /* unmap remaining mapped pages */
591 nr_frags = tx_info->num_of_bufs - 1;
592 for (j = 0; j < nr_frags; j++) {
594 dma_unmap_page(tx_ring->dev,
600 dev_kfree_skb_any(tx_info->skb);
602 netdev_tx_reset_queue(netdev_get_tx_queue(tx_ring->netdev,
606 static void ena_free_all_tx_bufs(struct ena_adapter *adapter)
608 struct ena_ring *tx_ring;
611 for (i = 0; i < adapter->num_queues; i++) {
612 tx_ring = &adapter->tx_ring[i];
613 ena_free_tx_bufs(tx_ring);
617 static void ena_destroy_all_tx_queues(struct ena_adapter *adapter)
622 for (i = 0; i < adapter->num_queues; i++) {
623 ena_qid = ENA_IO_TXQ_IDX(i);
624 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
628 static void ena_destroy_all_rx_queues(struct ena_adapter *adapter)
633 for (i = 0; i < adapter->num_queues; i++) {
634 ena_qid = ENA_IO_RXQ_IDX(i);
635 ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
639 static void ena_destroy_all_io_queues(struct ena_adapter *adapter)
641 ena_destroy_all_tx_queues(adapter);
642 ena_destroy_all_rx_queues(adapter);
645 static int validate_tx_req_id(struct ena_ring *tx_ring, u16 req_id)
647 struct ena_tx_buffer *tx_info = NULL;
649 if (likely(req_id < tx_ring->ring_size)) {
650 tx_info = &tx_ring->tx_buffer_info[req_id];
651 if (likely(tx_info->skb))
656 netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
657 "tx_info doesn't have valid skb\n");
659 netif_err(tx_ring->adapter, tx_done, tx_ring->netdev,
660 "Invalid req_id: %hu\n", req_id);
662 u64_stats_update_begin(&tx_ring->syncp);
663 tx_ring->tx_stats.bad_req_id++;
664 u64_stats_update_end(&tx_ring->syncp);
666 /* Trigger device reset */
667 set_bit(ENA_FLAG_TRIGGER_RESET, &tx_ring->adapter->flags);
671 static int ena_clean_tx_irq(struct ena_ring *tx_ring, u32 budget)
673 struct netdev_queue *txq;
682 next_to_clean = tx_ring->next_to_clean;
683 txq = netdev_get_tx_queue(tx_ring->netdev, tx_ring->qid);
685 while (tx_pkts < budget) {
686 struct ena_tx_buffer *tx_info;
688 struct ena_com_buf *ena_buf;
691 rc = ena_com_tx_comp_req_id_get(tx_ring->ena_com_io_cq,
696 rc = validate_tx_req_id(tx_ring, req_id);
700 tx_info = &tx_ring->tx_buffer_info[req_id];
703 /* prefetch skb_end_pointer() to speedup skb_shinfo(skb) */
707 tx_info->last_jiffies = 0;
709 if (likely(tx_info->num_of_bufs != 0)) {
710 ena_buf = tx_info->bufs;
712 dma_unmap_single(tx_ring->dev,
713 dma_unmap_addr(ena_buf, paddr),
714 dma_unmap_len(ena_buf, len),
717 /* unmap remaining mapped pages */
718 nr_frags = tx_info->num_of_bufs - 1;
719 for (i = 0; i < nr_frags; i++) {
721 dma_unmap_page(tx_ring->dev,
722 dma_unmap_addr(ena_buf, paddr),
723 dma_unmap_len(ena_buf, len),
728 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
729 "tx_poll: q %d skb %p completed\n", tx_ring->qid,
732 tx_bytes += skb->len;
735 total_done += tx_info->tx_descs;
737 tx_ring->free_tx_ids[next_to_clean] = req_id;
738 next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
742 tx_ring->next_to_clean = next_to_clean;
743 ena_com_comp_ack(tx_ring->ena_com_io_sq, total_done);
744 ena_com_update_dev_comp_head(tx_ring->ena_com_io_cq);
746 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
748 netif_dbg(tx_ring->adapter, tx_done, tx_ring->netdev,
749 "tx_poll: q %d done. total pkts: %d\n",
750 tx_ring->qid, tx_pkts);
752 /* need to make the rings circular update visible to
753 * ena_start_xmit() before checking for netif_queue_stopped().
757 above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
758 ENA_TX_WAKEUP_THRESH;
759 if (unlikely(netif_tx_queue_stopped(txq) && above_thresh)) {
760 __netif_tx_lock(txq, smp_processor_id());
761 above_thresh = ena_com_sq_empty_space(tx_ring->ena_com_io_sq) >
762 ENA_TX_WAKEUP_THRESH;
763 if (netif_tx_queue_stopped(txq) && above_thresh) {
764 netif_tx_wake_queue(txq);
765 u64_stats_update_begin(&tx_ring->syncp);
766 tx_ring->tx_stats.queue_wakeup++;
767 u64_stats_update_end(&tx_ring->syncp);
769 __netif_tx_unlock(txq);
772 tx_ring->per_napi_bytes += tx_bytes;
773 tx_ring->per_napi_packets += tx_pkts;
778 static struct sk_buff *ena_rx_skb(struct ena_ring *rx_ring,
779 struct ena_com_rx_buf_info *ena_bufs,
784 struct ena_rx_buffer *rx_info =
785 &rx_ring->rx_buffer_info[*next_to_clean];
790 len = ena_bufs[0].len;
791 if (unlikely(!rx_info->page)) {
792 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
797 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
798 "rx_info %p page %p\n",
799 rx_info, rx_info->page);
801 /* save virt address of first buffer */
802 va = page_address(rx_info->page) + rx_info->page_offset;
803 prefetch(va + NET_IP_ALIGN);
805 if (len <= rx_ring->rx_copybreak) {
806 skb = netdev_alloc_skb_ip_align(rx_ring->netdev,
807 rx_ring->rx_copybreak);
808 if (unlikely(!skb)) {
809 u64_stats_update_begin(&rx_ring->syncp);
810 rx_ring->rx_stats.skb_alloc_fail++;
811 u64_stats_update_end(&rx_ring->syncp);
812 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
813 "Failed to allocate skb\n");
817 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
818 "rx allocated small packet. len %d. data_len %d\n",
819 skb->len, skb->data_len);
821 /* sync this buffer for CPU use */
822 dma_sync_single_for_cpu(rx_ring->dev,
823 dma_unmap_addr(&rx_info->ena_buf, paddr),
826 skb_copy_to_linear_data(skb, va, len);
827 dma_sync_single_for_device(rx_ring->dev,
828 dma_unmap_addr(&rx_info->ena_buf, paddr),
833 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
834 *next_to_clean = ENA_RX_RING_IDX_ADD(*next_to_clean, descs,
839 skb = napi_get_frags(rx_ring->napi);
840 if (unlikely(!skb)) {
841 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
842 "Failed allocating skb\n");
843 u64_stats_update_begin(&rx_ring->syncp);
844 rx_ring->rx_stats.skb_alloc_fail++;
845 u64_stats_update_end(&rx_ring->syncp);
850 dma_unmap_page(rx_ring->dev,
851 dma_unmap_addr(&rx_info->ena_buf, paddr),
852 PAGE_SIZE, DMA_FROM_DEVICE);
854 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_info->page,
855 rx_info->page_offset, len, PAGE_SIZE);
857 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
858 "rx skb updated. len %d. data_len %d\n",
859 skb->len, skb->data_len);
861 rx_info->page = NULL;
863 ENA_RX_RING_IDX_NEXT(*next_to_clean,
865 if (likely(--descs == 0))
867 rx_info = &rx_ring->rx_buffer_info[*next_to_clean];
868 len = ena_bufs[++buf].len;
874 /* ena_rx_checksum - indicate in skb if hw indicated a good cksum
875 * @adapter: structure containing adapter specific data
876 * @ena_rx_ctx: received packet context/metadata
877 * @skb: skb currently being received and modified
879 static inline void ena_rx_checksum(struct ena_ring *rx_ring,
880 struct ena_com_rx_ctx *ena_rx_ctx,
883 /* Rx csum disabled */
884 if (unlikely(!(rx_ring->netdev->features & NETIF_F_RXCSUM))) {
885 skb->ip_summed = CHECKSUM_NONE;
889 /* For fragmented packets the checksum isn't valid */
890 if (ena_rx_ctx->frag) {
891 skb->ip_summed = CHECKSUM_NONE;
895 /* if IP and error */
896 if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
897 (ena_rx_ctx->l3_csum_err))) {
898 /* ipv4 checksum error */
899 skb->ip_summed = CHECKSUM_NONE;
900 u64_stats_update_begin(&rx_ring->syncp);
901 rx_ring->rx_stats.bad_csum++;
902 u64_stats_update_end(&rx_ring->syncp);
903 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
904 "RX IPv4 header checksum error\n");
909 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
910 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP))) {
911 if (unlikely(ena_rx_ctx->l4_csum_err)) {
912 /* TCP/UDP checksum error */
913 u64_stats_update_begin(&rx_ring->syncp);
914 rx_ring->rx_stats.bad_csum++;
915 u64_stats_update_end(&rx_ring->syncp);
916 netif_err(rx_ring->adapter, rx_err, rx_ring->netdev,
917 "RX L4 checksum error\n");
918 skb->ip_summed = CHECKSUM_NONE;
922 skb->ip_summed = CHECKSUM_UNNECESSARY;
926 static void ena_set_rx_hash(struct ena_ring *rx_ring,
927 struct ena_com_rx_ctx *ena_rx_ctx,
930 enum pkt_hash_types hash_type;
932 if (likely(rx_ring->netdev->features & NETIF_F_RXHASH)) {
933 if (likely((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
934 (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)))
936 hash_type = PKT_HASH_TYPE_L4;
938 hash_type = PKT_HASH_TYPE_NONE;
940 /* Override hash type if the packet is fragmented */
941 if (ena_rx_ctx->frag)
942 hash_type = PKT_HASH_TYPE_NONE;
944 skb_set_hash(skb, ena_rx_ctx->hash, hash_type);
948 /* ena_clean_rx_irq - Cleanup RX irq
949 * @rx_ring: RX ring to clean
950 * @napi: napi handler
951 * @budget: how many packets driver is allowed to clean
953 * Returns the number of cleaned buffers.
955 static int ena_clean_rx_irq(struct ena_ring *rx_ring, struct napi_struct *napi,
958 u16 next_to_clean = rx_ring->next_to_clean;
959 u32 res_budget, work_done;
961 struct ena_com_rx_ctx ena_rx_ctx;
962 struct ena_adapter *adapter;
965 int refill_threshold;
968 int rx_copybreak_pkt = 0;
970 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
971 "%s qid %d\n", __func__, rx_ring->qid);
975 ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
976 ena_rx_ctx.max_bufs = rx_ring->sgl_size;
977 ena_rx_ctx.descs = 0;
978 rc = ena_com_rx_pkt(rx_ring->ena_com_io_cq,
979 rx_ring->ena_com_io_sq,
984 if (unlikely(ena_rx_ctx.descs == 0))
987 netif_dbg(rx_ring->adapter, rx_status, rx_ring->netdev,
988 "rx_poll: q %d got packet from ena. descs #: %d l3 proto %d l4 proto %d hash: %x\n",
989 rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
990 ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
992 /* allocate skb and fill it */
993 skb = ena_rx_skb(rx_ring, rx_ring->ena_bufs, ena_rx_ctx.descs,
996 /* exit if we failed to retrieve a buffer */
997 if (unlikely(!skb)) {
998 next_to_clean = ENA_RX_RING_IDX_ADD(next_to_clean,
1000 rx_ring->ring_size);
1004 ena_rx_checksum(rx_ring, &ena_rx_ctx, skb);
1006 ena_set_rx_hash(rx_ring, &ena_rx_ctx, skb);
1008 skb_record_rx_queue(skb, rx_ring->qid);
1010 if (rx_ring->ena_bufs[0].len <= rx_ring->rx_copybreak) {
1011 total_len += rx_ring->ena_bufs[0].len;
1013 napi_gro_receive(napi, skb);
1015 total_len += skb->len;
1016 napi_gro_frags(napi);
1020 } while (likely(res_budget));
1022 work_done = budget - res_budget;
1023 rx_ring->per_napi_bytes += total_len;
1024 rx_ring->per_napi_packets += work_done;
1025 u64_stats_update_begin(&rx_ring->syncp);
1026 rx_ring->rx_stats.bytes += total_len;
1027 rx_ring->rx_stats.cnt += work_done;
1028 rx_ring->rx_stats.rx_copybreak_pkt += rx_copybreak_pkt;
1029 u64_stats_update_end(&rx_ring->syncp);
1031 rx_ring->next_to_clean = next_to_clean;
1033 refill_required = ena_com_sq_empty_space(rx_ring->ena_com_io_sq);
1034 refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
1036 /* Optimization, try to batch new rx buffers */
1037 if (refill_required > refill_threshold) {
1038 ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
1039 ena_refill_rx_bufs(rx_ring, refill_required);
1045 adapter = netdev_priv(rx_ring->netdev);
1047 u64_stats_update_begin(&rx_ring->syncp);
1048 rx_ring->rx_stats.bad_desc_num++;
1049 u64_stats_update_end(&rx_ring->syncp);
1051 /* Too many desc from the device. Trigger reset */
1052 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
1057 inline void ena_adjust_intr_moderation(struct ena_ring *rx_ring,
1058 struct ena_ring *tx_ring)
1060 /* We apply adaptive moderation on Rx path only.
1061 * Tx uses static interrupt moderation.
1063 ena_com_calculate_interrupt_delay(rx_ring->ena_dev,
1064 rx_ring->per_napi_packets,
1065 rx_ring->per_napi_bytes,
1066 &rx_ring->smoothed_interval,
1067 &rx_ring->moder_tbl_idx);
1069 /* Reset per napi packets/bytes */
1070 tx_ring->per_napi_packets = 0;
1071 tx_ring->per_napi_bytes = 0;
1072 rx_ring->per_napi_packets = 0;
1073 rx_ring->per_napi_bytes = 0;
1076 static inline void ena_update_ring_numa_node(struct ena_ring *tx_ring,
1077 struct ena_ring *rx_ring)
1079 int cpu = get_cpu();
1082 /* Check only one ring since the 2 rings are running on the same cpu */
1083 if (likely(tx_ring->cpu == cpu))
1086 numa_node = cpu_to_node(cpu);
1089 if (numa_node != NUMA_NO_NODE) {
1090 ena_com_update_numa_node(tx_ring->ena_com_io_cq, numa_node);
1091 ena_com_update_numa_node(rx_ring->ena_com_io_cq, numa_node);
1102 static int ena_io_poll(struct napi_struct *napi, int budget)
1104 struct ena_napi *ena_napi = container_of(napi, struct ena_napi, napi);
1105 struct ena_ring *tx_ring, *rx_ring;
1106 struct ena_eth_io_intr_reg intr_reg;
1111 int napi_comp_call = 0;
1114 tx_ring = ena_napi->tx_ring;
1115 rx_ring = ena_napi->rx_ring;
1117 tx_budget = tx_ring->ring_size / ENA_TX_POLL_BUDGET_DIVIDER;
1119 if (!test_bit(ENA_FLAG_DEV_UP, &tx_ring->adapter->flags)) {
1120 napi_complete_done(napi, 0);
1124 tx_work_done = ena_clean_tx_irq(tx_ring, tx_budget);
1125 rx_work_done = ena_clean_rx_irq(rx_ring, napi, budget);
1127 if ((budget > rx_work_done) && (tx_budget > tx_work_done)) {
1128 napi_complete_done(napi, rx_work_done);
1131 /* Tx and Rx share the same interrupt vector */
1132 if (ena_com_get_adaptive_moderation_enabled(rx_ring->ena_dev))
1133 ena_adjust_intr_moderation(rx_ring, tx_ring);
1135 /* Update intr register: rx intr delay, tx intr delay and
1138 ena_com_update_intr_reg(&intr_reg,
1139 rx_ring->smoothed_interval,
1140 tx_ring->smoothed_interval,
1143 /* It is a shared MSI-X. Tx and Rx CQ have pointer to it.
1144 * So we use one of them to reach the intr reg
1146 ena_com_unmask_intr(rx_ring->ena_com_io_cq, &intr_reg);
1148 ena_update_ring_numa_node(tx_ring, rx_ring);
1155 u64_stats_update_begin(&tx_ring->syncp);
1156 tx_ring->tx_stats.napi_comp += napi_comp_call;
1157 tx_ring->tx_stats.tx_poll++;
1158 u64_stats_update_end(&tx_ring->syncp);
1163 static irqreturn_t ena_intr_msix_mgmnt(int irq, void *data)
1165 struct ena_adapter *adapter = (struct ena_adapter *)data;
1167 ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1169 /* Don't call the aenq handler before probe is done */
1170 if (likely(test_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags)))
1171 ena_com_aenq_intr_handler(adapter->ena_dev, data);
1176 /* ena_intr_msix_io - MSI-X Interrupt Handler for Tx/Rx
1177 * @irq: interrupt number
1178 * @data: pointer to a network interface private napi device structure
1180 static irqreturn_t ena_intr_msix_io(int irq, void *data)
1182 struct ena_napi *ena_napi = data;
1184 napi_schedule(&ena_napi->napi);
1189 static int ena_enable_msix(struct ena_adapter *adapter, int num_queues)
1191 int i, msix_vecs, rc;
1193 if (test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1194 netif_err(adapter, probe, adapter->netdev,
1195 "Error, MSI-X is already enabled\n");
1199 /* Reserved the max msix vectors we might need */
1200 msix_vecs = ENA_MAX_MSIX_VEC(num_queues);
1202 netif_dbg(adapter, probe, adapter->netdev,
1203 "trying to enable MSI-X, vectors %d\n", msix_vecs);
1205 adapter->msix_entries = vzalloc(msix_vecs * sizeof(struct msix_entry));
1207 if (!adapter->msix_entries)
1210 for (i = 0; i < msix_vecs; i++)
1211 adapter->msix_entries[i].entry = i;
1213 rc = pci_enable_msix(adapter->pdev, adapter->msix_entries, msix_vecs);
1215 netif_err(adapter, probe, adapter->netdev,
1216 "Failed to enable MSI-X, vectors %d rc %d\n",
1221 netif_dbg(adapter, probe, adapter->netdev, "enable MSI-X, vectors %d\n",
1224 if (msix_vecs >= 1) {
1225 if (ena_init_rx_cpu_rmap(adapter))
1226 netif_warn(adapter, probe, adapter->netdev,
1227 "Failed to map IRQs to CPUs\n");
1230 adapter->msix_vecs = msix_vecs;
1231 set_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags);
1236 static void ena_setup_mgmnt_intr(struct ena_adapter *adapter)
1240 snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
1241 ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
1242 pci_name(adapter->pdev));
1243 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler =
1244 ena_intr_msix_mgmnt;
1245 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
1246 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
1247 adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
1248 cpu = cpumask_first(cpu_online_mask);
1249 adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].cpu = cpu;
1250 cpumask_set_cpu(cpu,
1251 &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].affinity_hint_mask);
1254 static void ena_setup_io_intr(struct ena_adapter *adapter)
1256 struct net_device *netdev;
1257 int irq_idx, i, cpu;
1259 netdev = adapter->netdev;
1261 for (i = 0; i < adapter->num_queues; i++) {
1262 irq_idx = ENA_IO_IRQ_IDX(i);
1263 cpu = i % num_online_cpus();
1265 snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
1266 "%s-Tx-Rx-%d", netdev->name, i);
1267 adapter->irq_tbl[irq_idx].handler = ena_intr_msix_io;
1268 adapter->irq_tbl[irq_idx].data = &adapter->ena_napi[i];
1269 adapter->irq_tbl[irq_idx].vector =
1270 adapter->msix_entries[irq_idx].vector;
1271 adapter->irq_tbl[irq_idx].cpu = cpu;
1273 cpumask_set_cpu(cpu,
1274 &adapter->irq_tbl[irq_idx].affinity_hint_mask);
1278 static int ena_request_mgmnt_irq(struct ena_adapter *adapter)
1280 unsigned long flags = 0;
1281 struct ena_irq *irq;
1284 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1285 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1288 netif_err(adapter, probe, adapter->netdev,
1289 "failed to request admin irq\n");
1293 netif_dbg(adapter, probe, adapter->netdev,
1294 "set affinity hint of mgmnt irq.to 0x%lx (irq vector: %d)\n",
1295 irq->affinity_hint_mask.bits[0], irq->vector);
1297 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1302 static int ena_request_io_irq(struct ena_adapter *adapter)
1304 unsigned long flags = 0;
1305 struct ena_irq *irq;
1308 if (!test_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags)) {
1309 netif_err(adapter, ifup, adapter->netdev,
1310 "Failed to request I/O IRQ: MSI-X is not enabled\n");
1314 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
1315 irq = &adapter->irq_tbl[i];
1316 rc = request_irq(irq->vector, irq->handler, flags, irq->name,
1319 netif_err(adapter, ifup, adapter->netdev,
1320 "Failed to request I/O IRQ. index %d rc %d\n",
1325 netif_dbg(adapter, ifup, adapter->netdev,
1326 "set affinity hint of irq. index %d to 0x%lx (irq vector: %d)\n",
1327 i, irq->affinity_hint_mask.bits[0], irq->vector);
1329 irq_set_affinity_hint(irq->vector, &irq->affinity_hint_mask);
1335 for (k = ENA_IO_IRQ_FIRST_IDX; k < i; k++) {
1336 irq = &adapter->irq_tbl[k];
1337 free_irq(irq->vector, irq->data);
1343 static void ena_free_mgmnt_irq(struct ena_adapter *adapter)
1345 struct ena_irq *irq;
1347 irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
1348 synchronize_irq(irq->vector);
1349 irq_set_affinity_hint(irq->vector, NULL);
1350 free_irq(irq->vector, irq->data);
1353 static void ena_free_io_irq(struct ena_adapter *adapter)
1355 struct ena_irq *irq;
1358 #ifdef CONFIG_RFS_ACCEL
1359 if (adapter->msix_vecs >= 1) {
1360 free_irq_cpu_rmap(adapter->netdev->rx_cpu_rmap);
1361 adapter->netdev->rx_cpu_rmap = NULL;
1363 #endif /* CONFIG_RFS_ACCEL */
1365 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
1366 irq = &adapter->irq_tbl[i];
1367 irq_set_affinity_hint(irq->vector, NULL);
1368 free_irq(irq->vector, irq->data);
1372 static void ena_disable_msix(struct ena_adapter *adapter)
1374 if (test_and_clear_bit(ENA_FLAG_MSIX_ENABLED, &adapter->flags))
1375 pci_disable_msix(adapter->pdev);
1377 if (adapter->msix_entries)
1378 vfree(adapter->msix_entries);
1379 adapter->msix_entries = NULL;
1382 static void ena_disable_io_intr_sync(struct ena_adapter *adapter)
1386 if (!netif_running(adapter->netdev))
1389 for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++)
1390 synchronize_irq(adapter->irq_tbl[i].vector);
1393 static void ena_del_napi(struct ena_adapter *adapter)
1397 for (i = 0; i < adapter->num_queues; i++)
1398 netif_napi_del(&adapter->ena_napi[i].napi);
1401 static void ena_init_napi(struct ena_adapter *adapter)
1403 struct ena_napi *napi;
1406 for (i = 0; i < adapter->num_queues; i++) {
1407 napi = &adapter->ena_napi[i];
1409 netif_napi_add(adapter->netdev,
1410 &adapter->ena_napi[i].napi,
1413 napi->rx_ring = &adapter->rx_ring[i];
1414 napi->tx_ring = &adapter->tx_ring[i];
1419 static void ena_napi_disable_all(struct ena_adapter *adapter)
1423 for (i = 0; i < adapter->num_queues; i++)
1424 napi_disable(&adapter->ena_napi[i].napi);
1427 static void ena_napi_enable_all(struct ena_adapter *adapter)
1431 for (i = 0; i < adapter->num_queues; i++)
1432 napi_enable(&adapter->ena_napi[i].napi);
1435 static void ena_restore_ethtool_params(struct ena_adapter *adapter)
1437 adapter->tx_usecs = 0;
1438 adapter->rx_usecs = 0;
1439 adapter->tx_frames = 1;
1440 adapter->rx_frames = 1;
1443 /* Configure the Rx forwarding */
1444 static int ena_rss_configure(struct ena_adapter *adapter)
1446 struct ena_com_dev *ena_dev = adapter->ena_dev;
1449 /* In case the RSS table wasn't initialized by probe */
1450 if (!ena_dev->rss.tbl_log_size) {
1451 rc = ena_rss_init_default(adapter);
1452 if (rc && (rc != -EPERM)) {
1453 netif_err(adapter, ifup, adapter->netdev,
1454 "Failed to init RSS rc: %d\n", rc);
1459 /* Set indirect table */
1460 rc = ena_com_indirect_table_set(ena_dev);
1461 if (unlikely(rc && rc != -EPERM))
1464 /* Configure hash function (if supported) */
1465 rc = ena_com_set_hash_function(ena_dev);
1466 if (unlikely(rc && (rc != -EPERM)))
1469 /* Configure hash inputs (if supported) */
1470 rc = ena_com_set_hash_ctrl(ena_dev);
1471 if (unlikely(rc && (rc != -EPERM)))
1477 static int ena_up_complete(struct ena_adapter *adapter)
1481 rc = ena_rss_configure(adapter);
1485 ena_init_napi(adapter);
1487 ena_change_mtu(adapter->netdev, adapter->netdev->mtu);
1489 ena_refill_all_rx_bufs(adapter);
1491 /* enable transmits */
1492 netif_tx_start_all_queues(adapter->netdev);
1494 ena_restore_ethtool_params(adapter);
1496 ena_napi_enable_all(adapter);
1498 /* schedule napi in case we had pending packets
1499 * from the last time we disable napi
1501 for (i = 0; i < adapter->num_queues; i++)
1502 napi_schedule(&adapter->ena_napi[i].napi);
1507 static int ena_create_io_tx_queue(struct ena_adapter *adapter, int qid)
1509 struct ena_com_create_io_ctx ctx = { 0 };
1510 struct ena_com_dev *ena_dev;
1511 struct ena_ring *tx_ring;
1516 ena_dev = adapter->ena_dev;
1518 tx_ring = &adapter->tx_ring[qid];
1519 msix_vector = ENA_IO_IRQ_IDX(qid);
1520 ena_qid = ENA_IO_TXQ_IDX(qid);
1522 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
1524 ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
1525 ctx.msix_vector = msix_vector;
1526 ctx.queue_size = adapter->tx_ring_size;
1527 ctx.numa_node = cpu_to_node(tx_ring->cpu);
1529 rc = ena_com_create_io_queue(ena_dev, &ctx);
1531 netif_err(adapter, ifup, adapter->netdev,
1532 "Failed to create I/O TX queue num %d rc: %d\n",
1537 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1538 &tx_ring->ena_com_io_sq,
1539 &tx_ring->ena_com_io_cq);
1541 netif_err(adapter, ifup, adapter->netdev,
1542 "Failed to get TX queue handlers. TX queue num %d rc: %d\n",
1544 ena_com_destroy_io_queue(ena_dev, ena_qid);
1547 ena_com_update_numa_node(tx_ring->ena_com_io_cq, ctx.numa_node);
1551 static int ena_create_all_io_tx_queues(struct ena_adapter *adapter)
1553 struct ena_com_dev *ena_dev = adapter->ena_dev;
1556 for (i = 0; i < adapter->num_queues; i++) {
1557 rc = ena_create_io_tx_queue(adapter, i);
1566 ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
1571 static int ena_create_io_rx_queue(struct ena_adapter *adapter, int qid)
1573 struct ena_com_dev *ena_dev;
1574 struct ena_com_create_io_ctx ctx = { 0 };
1575 struct ena_ring *rx_ring;
1580 ena_dev = adapter->ena_dev;
1582 rx_ring = &adapter->rx_ring[qid];
1583 msix_vector = ENA_IO_IRQ_IDX(qid);
1584 ena_qid = ENA_IO_RXQ_IDX(qid);
1587 ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
1588 ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
1589 ctx.msix_vector = msix_vector;
1590 ctx.queue_size = adapter->rx_ring_size;
1591 ctx.numa_node = cpu_to_node(rx_ring->cpu);
1593 rc = ena_com_create_io_queue(ena_dev, &ctx);
1595 netif_err(adapter, ifup, adapter->netdev,
1596 "Failed to create I/O RX queue num %d rc: %d\n",
1601 rc = ena_com_get_io_handlers(ena_dev, ena_qid,
1602 &rx_ring->ena_com_io_sq,
1603 &rx_ring->ena_com_io_cq);
1605 netif_err(adapter, ifup, adapter->netdev,
1606 "Failed to get RX queue handlers. RX queue num %d rc: %d\n",
1608 ena_com_destroy_io_queue(ena_dev, ena_qid);
1611 ena_com_update_numa_node(rx_ring->ena_com_io_cq, ctx.numa_node);
1616 static int ena_create_all_io_rx_queues(struct ena_adapter *adapter)
1618 struct ena_com_dev *ena_dev = adapter->ena_dev;
1621 for (i = 0; i < adapter->num_queues; i++) {
1622 rc = ena_create_io_rx_queue(adapter, i);
1631 ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
1636 static int ena_up(struct ena_adapter *adapter)
1640 netdev_dbg(adapter->netdev, "%s\n", __func__);
1642 ena_setup_io_intr(adapter);
1644 rc = ena_request_io_irq(adapter);
1648 /* allocate transmit descriptors */
1649 rc = ena_setup_all_tx_resources(adapter);
1653 /* allocate receive descriptors */
1654 rc = ena_setup_all_rx_resources(adapter);
1658 /* Create TX queues */
1659 rc = ena_create_all_io_tx_queues(adapter);
1661 goto err_create_tx_queues;
1663 /* Create RX queues */
1664 rc = ena_create_all_io_rx_queues(adapter);
1666 goto err_create_rx_queues;
1668 rc = ena_up_complete(adapter);
1672 if (test_bit(ENA_FLAG_LINK_UP, &adapter->flags))
1673 netif_carrier_on(adapter->netdev);
1675 u64_stats_update_begin(&adapter->syncp);
1676 adapter->dev_stats.interface_up++;
1677 u64_stats_update_end(&adapter->syncp);
1679 set_bit(ENA_FLAG_DEV_UP, &adapter->flags);
1684 ena_destroy_all_rx_queues(adapter);
1685 err_create_rx_queues:
1686 ena_destroy_all_tx_queues(adapter);
1687 err_create_tx_queues:
1688 ena_free_all_io_rx_resources(adapter);
1690 ena_free_all_io_tx_resources(adapter);
1692 ena_free_io_irq(adapter);
1698 static void ena_down(struct ena_adapter *adapter)
1700 netif_info(adapter, ifdown, adapter->netdev, "%s\n", __func__);
1702 clear_bit(ENA_FLAG_DEV_UP, &adapter->flags);
1704 u64_stats_update_begin(&adapter->syncp);
1705 adapter->dev_stats.interface_down++;
1706 u64_stats_update_end(&adapter->syncp);
1708 /* After this point the napi handler won't enable the tx queue */
1709 ena_napi_disable_all(adapter);
1710 netif_carrier_off(adapter->netdev);
1711 netif_tx_disable(adapter->netdev);
1713 /* After destroy the queue there won't be any new interrupts */
1714 ena_destroy_all_io_queues(adapter);
1716 ena_disable_io_intr_sync(adapter);
1717 ena_free_io_irq(adapter);
1718 ena_del_napi(adapter);
1720 ena_free_all_tx_bufs(adapter);
1721 ena_free_all_rx_bufs(adapter);
1722 ena_free_all_io_tx_resources(adapter);
1723 ena_free_all_io_rx_resources(adapter);
1726 /* ena_open - Called when a network interface is made active
1727 * @netdev: network interface device structure
1729 * Returns 0 on success, negative value on failure
1731 * The open entry point is called when a network interface is made
1732 * active by the system (IFF_UP). At this point all resources needed
1733 * for transmit and receive operations are allocated, the interrupt
1734 * handler is registered with the OS, the watchdog timer is started,
1735 * and the stack is notified that the interface is ready.
1737 static int ena_open(struct net_device *netdev)
1739 struct ena_adapter *adapter = netdev_priv(netdev);
1742 /* Notify the stack of the actual queue counts. */
1743 rc = netif_set_real_num_tx_queues(netdev, adapter->num_queues);
1745 netif_err(adapter, ifup, netdev, "Can't set num tx queues\n");
1749 rc = netif_set_real_num_rx_queues(netdev, adapter->num_queues);
1751 netif_err(adapter, ifup, netdev, "Can't set num rx queues\n");
1755 rc = ena_up(adapter);
1762 /* ena_close - Disables a network interface
1763 * @netdev: network interface device structure
1765 * Returns 0, this is not allowed to fail
1767 * The close entry point is called when an interface is de-activated
1768 * by the OS. The hardware is still under the drivers control, but
1769 * needs to be disabled. A global MAC reset is issued to stop the
1770 * hardware, and all transmit and receive resources are freed.
1772 static int ena_close(struct net_device *netdev)
1774 struct ena_adapter *adapter = netdev_priv(netdev);
1776 netif_dbg(adapter, ifdown, netdev, "%s\n", __func__);
1778 if (test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
1784 static void ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct sk_buff *skb)
1786 u32 mss = skb_shinfo(skb)->gso_size;
1787 struct ena_com_tx_meta *ena_meta = &ena_tx_ctx->ena_meta;
1790 if ((skb->ip_summed == CHECKSUM_PARTIAL) || mss) {
1791 ena_tx_ctx->l4_csum_enable = 1;
1793 ena_tx_ctx->tso_enable = 1;
1794 ena_meta->l4_hdr_len = tcp_hdr(skb)->doff;
1795 ena_tx_ctx->l4_csum_partial = 0;
1797 ena_tx_ctx->tso_enable = 0;
1798 ena_meta->l4_hdr_len = 0;
1799 ena_tx_ctx->l4_csum_partial = 1;
1802 switch (ip_hdr(skb)->version) {
1804 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
1805 if (ip_hdr(skb)->frag_off & htons(IP_DF))
1808 ena_tx_ctx->l3_csum_enable = 1;
1809 l4_protocol = ip_hdr(skb)->protocol;
1812 ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
1813 l4_protocol = ipv6_hdr(skb)->nexthdr;
1819 if (l4_protocol == IPPROTO_TCP)
1820 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
1822 ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
1824 ena_meta->mss = mss;
1825 ena_meta->l3_hdr_len = skb_network_header_len(skb);
1826 ena_meta->l3_hdr_offset = skb_network_offset(skb);
1827 ena_tx_ctx->meta_valid = 1;
1830 ena_tx_ctx->meta_valid = 0;
1834 static int ena_check_and_linearize_skb(struct ena_ring *tx_ring,
1835 struct sk_buff *skb)
1837 int num_frags, header_len, rc;
1839 num_frags = skb_shinfo(skb)->nr_frags;
1840 header_len = skb_headlen(skb);
1842 if (num_frags < tx_ring->sgl_size)
1845 if ((num_frags == tx_ring->sgl_size) &&
1846 (header_len < tx_ring->tx_max_header_size))
1849 u64_stats_update_begin(&tx_ring->syncp);
1850 tx_ring->tx_stats.linearize++;
1851 u64_stats_update_end(&tx_ring->syncp);
1853 rc = skb_linearize(skb);
1855 u64_stats_update_begin(&tx_ring->syncp);
1856 tx_ring->tx_stats.linearize_failed++;
1857 u64_stats_update_end(&tx_ring->syncp);
1863 /* Called with netif_tx_lock. */
1864 static netdev_tx_t ena_start_xmit(struct sk_buff *skb, struct net_device *dev)
1866 struct ena_adapter *adapter = netdev_priv(dev);
1867 struct ena_tx_buffer *tx_info;
1868 struct ena_com_tx_ctx ena_tx_ctx;
1869 struct ena_ring *tx_ring;
1870 struct netdev_queue *txq;
1871 struct ena_com_buf *ena_buf;
1879 int qid, rc, nb_hw_desc;
1882 netif_dbg(adapter, tx_queued, dev, "%s skb %p\n", __func__, skb);
1883 /* Determine which tx ring we will be placed on */
1884 qid = skb_get_queue_mapping(skb);
1885 tx_ring = &adapter->tx_ring[qid];
1886 txq = netdev_get_tx_queue(dev, qid);
1888 rc = ena_check_and_linearize_skb(tx_ring, skb);
1890 goto error_drop_packet;
1892 skb_tx_timestamp(skb);
1893 len = skb_headlen(skb);
1895 next_to_use = tx_ring->next_to_use;
1896 req_id = tx_ring->free_tx_ids[next_to_use];
1897 tx_info = &tx_ring->tx_buffer_info[req_id];
1898 tx_info->num_of_bufs = 0;
1900 WARN(tx_info->skb, "SKB isn't NULL req_id %d\n", req_id);
1901 ena_buf = tx_info->bufs;
1904 if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
1905 /* prepared the push buffer */
1906 push_len = min_t(u32, len, tx_ring->tx_max_header_size);
1907 header_len = push_len;
1908 push_hdr = skb->data;
1911 header_len = min_t(u32, len, tx_ring->tx_max_header_size);
1915 netif_dbg(adapter, tx_queued, dev,
1916 "skb: %p header_buf->vaddr: %p push_len: %d\n", skb,
1917 push_hdr, push_len);
1919 if (len > push_len) {
1920 dma = dma_map_single(tx_ring->dev, skb->data + push_len,
1921 len - push_len, DMA_TO_DEVICE);
1922 if (dma_mapping_error(tx_ring->dev, dma))
1923 goto error_report_dma_error;
1925 ena_buf->paddr = dma;
1926 ena_buf->len = len - push_len;
1929 tx_info->num_of_bufs++;
1932 last_frag = skb_shinfo(skb)->nr_frags;
1934 for (i = 0; i < last_frag; i++) {
1935 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1937 len = skb_frag_size(frag);
1938 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, len,
1940 if (dma_mapping_error(tx_ring->dev, dma))
1941 goto error_report_dma_error;
1943 ena_buf->paddr = dma;
1948 tx_info->num_of_bufs += last_frag;
1950 memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
1951 ena_tx_ctx.ena_bufs = tx_info->bufs;
1952 ena_tx_ctx.push_header = push_hdr;
1953 ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
1954 ena_tx_ctx.req_id = req_id;
1955 ena_tx_ctx.header_len = header_len;
1957 /* set flags and meta data */
1958 ena_tx_csum(&ena_tx_ctx, skb);
1960 /* prepare the packet's descriptors to dma engine */
1961 rc = ena_com_prepare_tx(tx_ring->ena_com_io_sq, &ena_tx_ctx,
1965 netif_err(adapter, tx_queued, dev,
1966 "failed to prepare tx bufs\n");
1967 u64_stats_update_begin(&tx_ring->syncp);
1968 tx_ring->tx_stats.queue_stop++;
1969 tx_ring->tx_stats.prepare_ctx_err++;
1970 u64_stats_update_end(&tx_ring->syncp);
1971 netif_tx_stop_queue(txq);
1972 goto error_unmap_dma;
1975 netdev_tx_sent_queue(txq, skb->len);
1977 u64_stats_update_begin(&tx_ring->syncp);
1978 tx_ring->tx_stats.cnt++;
1979 tx_ring->tx_stats.bytes += skb->len;
1980 u64_stats_update_end(&tx_ring->syncp);
1982 tx_info->tx_descs = nb_hw_desc;
1983 tx_info->last_jiffies = jiffies;
1985 tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
1986 tx_ring->ring_size);
1988 /* This WMB is aimed to:
1989 * 1 - perform smp barrier before reading next_to_completion
1990 * 2 - make sure the desc were written before trigger DB
1994 /* stop the queue when no more space available, the packet can have up
1995 * to sgl_size + 2. one for the meta descriptor and one for header
1996 * (if the header is larger than tx_max_header_size).
1998 if (unlikely(ena_com_sq_empty_space(tx_ring->ena_com_io_sq) <
1999 (tx_ring->sgl_size + 2))) {
2000 netif_dbg(adapter, tx_queued, dev, "%s stop queue %d\n",
2003 netif_tx_stop_queue(txq);
2004 u64_stats_update_begin(&tx_ring->syncp);
2005 tx_ring->tx_stats.queue_stop++;
2006 u64_stats_update_end(&tx_ring->syncp);
2008 /* There is a rare condition where this function decide to
2009 * stop the queue but meanwhile clean_tx_irq updates
2010 * next_to_completion and terminates.
2011 * The queue will remain stopped forever.
2012 * To solve this issue this function perform rmb, check
2013 * the wakeup condition and wake up the queue if needed.
2017 if (ena_com_sq_empty_space(tx_ring->ena_com_io_sq)
2018 > ENA_TX_WAKEUP_THRESH) {
2019 netif_tx_wake_queue(txq);
2020 u64_stats_update_begin(&tx_ring->syncp);
2021 tx_ring->tx_stats.queue_wakeup++;
2022 u64_stats_update_end(&tx_ring->syncp);
2026 if (netif_xmit_stopped(txq) || !skb->xmit_more) {
2027 /* trigger the dma engine */
2028 ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
2029 u64_stats_update_begin(&tx_ring->syncp);
2030 tx_ring->tx_stats.doorbells++;
2031 u64_stats_update_end(&tx_ring->syncp);
2034 return NETDEV_TX_OK;
2036 error_report_dma_error:
2037 u64_stats_update_begin(&tx_ring->syncp);
2038 tx_ring->tx_stats.dma_mapping_err++;
2039 u64_stats_update_end(&tx_ring->syncp);
2040 netdev_warn(adapter->netdev, "failed to map skb\n");
2042 tx_info->skb = NULL;
2046 /* save value of frag that failed */
2049 /* start back at beginning and unmap skb */
2050 tx_info->skb = NULL;
2051 ena_buf = tx_info->bufs;
2052 dma_unmap_single(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
2053 dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
2055 /* unmap remaining mapped pages */
2056 for (i = 0; i < last_frag; i++) {
2058 dma_unmap_page(tx_ring->dev, dma_unmap_addr(ena_buf, paddr),
2059 dma_unmap_len(ena_buf, len), DMA_TO_DEVICE);
2066 return NETDEV_TX_OK;
2069 #ifdef CONFIG_NET_POLL_CONTROLLER
2070 static void ena_netpoll(struct net_device *netdev)
2072 struct ena_adapter *adapter = netdev_priv(netdev);
2075 for (i = 0; i < adapter->num_queues; i++)
2076 napi_schedule(&adapter->ena_napi[i].napi);
2078 #endif /* CONFIG_NET_POLL_CONTROLLER */
2080 static u16 ena_select_queue(struct net_device *dev, struct sk_buff *skb,
2081 void *accel_priv, select_queue_fallback_t fallback)
2084 /* we suspect that this is good for in--kernel network services that
2085 * want to loop incoming skb rx to tx in normal user generated traffic,
2086 * most probably we will not get to this
2088 if (skb_rx_queue_recorded(skb))
2089 qid = skb_get_rx_queue(skb);
2091 qid = fallback(dev, skb);
2096 static void ena_config_host_info(struct ena_com_dev *ena_dev)
2098 struct ena_admin_host_info *host_info;
2101 /* Allocate only the host info */
2102 rc = ena_com_allocate_host_info(ena_dev);
2104 pr_err("Cannot allocate host info\n");
2108 host_info = ena_dev->host_attr.host_info;
2110 host_info->os_type = ENA_ADMIN_OS_LINUX;
2111 host_info->kernel_ver = LINUX_VERSION_CODE;
2112 strncpy(host_info->kernel_ver_str, utsname()->version,
2113 sizeof(host_info->kernel_ver_str) - 1);
2114 host_info->os_dist = 0;
2115 strncpy(host_info->os_dist_str, utsname()->release,
2116 sizeof(host_info->os_dist_str) - 1);
2117 host_info->driver_version =
2118 (DRV_MODULE_VER_MAJOR) |
2119 (DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
2120 (DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
2122 rc = ena_com_set_host_attributes(ena_dev);
2125 pr_warn("Cannot set host attributes\n");
2127 pr_err("Cannot set host attributes\n");
2135 ena_com_delete_host_info(ena_dev);
2138 static void ena_config_debug_area(struct ena_adapter *adapter)
2140 u32 debug_area_size;
2143 ss_count = ena_get_sset_count(adapter->netdev, ETH_SS_STATS);
2144 if (ss_count <= 0) {
2145 netif_err(adapter, drv, adapter->netdev,
2146 "SS count is negative\n");
2150 /* allocate 32 bytes for each string and 64bit for the value */
2151 debug_area_size = ss_count * ETH_GSTRING_LEN + sizeof(u64) * ss_count;
2153 rc = ena_com_allocate_debug_area(adapter->ena_dev, debug_area_size);
2155 pr_err("Cannot allocate debug area\n");
2159 rc = ena_com_set_host_attributes(adapter->ena_dev);
2162 netif_warn(adapter, drv, adapter->netdev,
2163 "Cannot set host attributes\n");
2165 netif_err(adapter, drv, adapter->netdev,
2166 "Cannot set host attributes\n");
2172 ena_com_delete_debug_area(adapter->ena_dev);
2175 static struct rtnl_link_stats64 *ena_get_stats64(struct net_device *netdev,
2176 struct rtnl_link_stats64 *stats)
2178 struct ena_adapter *adapter = netdev_priv(netdev);
2179 struct ena_admin_basic_stats ena_stats;
2182 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2185 rc = ena_com_get_dev_basic_stats(adapter->ena_dev, &ena_stats);
2189 stats->tx_bytes = ((u64)ena_stats.tx_bytes_high << 32) |
2190 ena_stats.tx_bytes_low;
2191 stats->rx_bytes = ((u64)ena_stats.rx_bytes_high << 32) |
2192 ena_stats.rx_bytes_low;
2194 stats->rx_packets = ((u64)ena_stats.rx_pkts_high << 32) |
2195 ena_stats.rx_pkts_low;
2196 stats->tx_packets = ((u64)ena_stats.tx_pkts_high << 32) |
2197 ena_stats.tx_pkts_low;
2199 stats->rx_dropped = ((u64)ena_stats.rx_drops_high << 32) |
2200 ena_stats.rx_drops_low;
2202 stats->multicast = 0;
2203 stats->collisions = 0;
2205 stats->rx_length_errors = 0;
2206 stats->rx_crc_errors = 0;
2207 stats->rx_frame_errors = 0;
2208 stats->rx_fifo_errors = 0;
2209 stats->rx_missed_errors = 0;
2210 stats->tx_window_errors = 0;
2212 stats->rx_errors = 0;
2213 stats->tx_errors = 0;
2218 static const struct net_device_ops ena_netdev_ops = {
2219 .ndo_open = ena_open,
2220 .ndo_stop = ena_close,
2221 .ndo_start_xmit = ena_start_xmit,
2222 .ndo_select_queue = ena_select_queue,
2223 .ndo_get_stats64 = ena_get_stats64,
2224 .ndo_tx_timeout = ena_tx_timeout,
2225 .ndo_change_mtu = ena_change_mtu,
2226 .ndo_set_mac_address = NULL,
2227 .ndo_validate_addr = eth_validate_addr,
2228 #ifdef CONFIG_NET_POLL_CONTROLLER
2229 .ndo_poll_controller = ena_netpoll,
2230 #endif /* CONFIG_NET_POLL_CONTROLLER */
2233 static void ena_device_io_suspend(struct work_struct *work)
2235 struct ena_adapter *adapter =
2236 container_of(work, struct ena_adapter, suspend_io_task);
2237 struct net_device *netdev = adapter->netdev;
2239 /* ena_napi_disable_all disables only the IO handling.
2240 * We are still subject to AENQ keep alive watchdog.
2242 u64_stats_update_begin(&adapter->syncp);
2243 adapter->dev_stats.io_suspend++;
2244 u64_stats_update_begin(&adapter->syncp);
2245 ena_napi_disable_all(adapter);
2246 netif_tx_lock(netdev);
2247 netif_device_detach(netdev);
2248 netif_tx_unlock(netdev);
2251 static void ena_device_io_resume(struct work_struct *work)
2253 struct ena_adapter *adapter =
2254 container_of(work, struct ena_adapter, resume_io_task);
2255 struct net_device *netdev = adapter->netdev;
2257 u64_stats_update_begin(&adapter->syncp);
2258 adapter->dev_stats.io_resume++;
2259 u64_stats_update_end(&adapter->syncp);
2261 netif_device_attach(netdev);
2262 ena_napi_enable_all(adapter);
2265 static int ena_device_validate_params(struct ena_adapter *adapter,
2266 struct ena_com_dev_get_features_ctx *get_feat_ctx)
2268 struct net_device *netdev = adapter->netdev;
2271 rc = ether_addr_equal(get_feat_ctx->dev_attr.mac_addr,
2274 netif_err(adapter, drv, netdev,
2275 "Error, mac address are different\n");
2279 if ((get_feat_ctx->max_queues.max_cq_num < adapter->num_queues) ||
2280 (get_feat_ctx->max_queues.max_sq_num < adapter->num_queues)) {
2281 netif_err(adapter, drv, netdev,
2282 "Error, device doesn't support enough queues\n");
2286 if (get_feat_ctx->dev_attr.max_mtu < netdev->mtu) {
2287 netif_err(adapter, drv, netdev,
2288 "Error, device max mtu is smaller than netdev MTU\n");
2295 static int ena_device_init(struct ena_com_dev *ena_dev, struct pci_dev *pdev,
2296 struct ena_com_dev_get_features_ctx *get_feat_ctx,
2299 struct device *dev = &pdev->dev;
2300 bool readless_supported;
2305 rc = ena_com_mmio_reg_read_request_init(ena_dev);
2307 dev_err(dev, "failed to init mmio read less\n");
2311 /* The PCIe configuration space revision id indicate if mmio reg
2314 readless_supported = !(pdev->revision & ENA_MMIO_DISABLE_REG_READ);
2315 ena_com_set_mmio_read_mode(ena_dev, readless_supported);
2317 rc = ena_com_dev_reset(ena_dev);
2319 dev_err(dev, "Can not reset device\n");
2320 goto err_mmio_read_less;
2323 rc = ena_com_validate_version(ena_dev);
2325 dev_err(dev, "device version is too low\n");
2326 goto err_mmio_read_less;
2329 dma_width = ena_com_get_dma_width(ena_dev);
2330 if (dma_width < 0) {
2331 dev_err(dev, "Invalid dma width value %d", dma_width);
2333 goto err_mmio_read_less;
2336 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(dma_width));
2338 dev_err(dev, "pci_set_dma_mask failed 0x%x\n", rc);
2339 goto err_mmio_read_less;
2342 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(dma_width));
2344 dev_err(dev, "err_pci_set_consistent_dma_mask failed 0x%x\n",
2346 goto err_mmio_read_less;
2349 /* ENA admin level init */
2350 rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
2353 "Can not initialize ena admin queue with device\n");
2354 goto err_mmio_read_less;
2357 /* To enable the msix interrupts the driver needs to know the number
2358 * of queues. So the driver uses polling mode to retrieve this
2361 ena_com_set_admin_polling_mode(ena_dev, true);
2363 /* Get Device Attributes*/
2364 rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
2366 dev_err(dev, "Cannot get attribute for ena device rc=%d\n", rc);
2367 goto err_admin_init;
2370 /* Try to turn all the available aenq groups */
2371 aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
2372 BIT(ENA_ADMIN_FATAL_ERROR) |
2373 BIT(ENA_ADMIN_WARNING) |
2374 BIT(ENA_ADMIN_NOTIFICATION) |
2375 BIT(ENA_ADMIN_KEEP_ALIVE);
2377 aenq_groups &= get_feat_ctx->aenq.supported_groups;
2379 rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
2381 dev_err(dev, "Cannot configure aenq groups rc= %d\n", rc);
2382 goto err_admin_init;
2385 *wd_state = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
2387 ena_config_host_info(ena_dev);
2392 ena_com_admin_destroy(ena_dev);
2394 ena_com_mmio_reg_read_request_destroy(ena_dev);
2399 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
2402 struct ena_com_dev *ena_dev = adapter->ena_dev;
2403 struct device *dev = &adapter->pdev->dev;
2406 rc = ena_enable_msix(adapter, io_vectors);
2408 dev_err(dev, "Can not reserve msix vectors\n");
2412 ena_setup_mgmnt_intr(adapter);
2414 rc = ena_request_mgmnt_irq(adapter);
2416 dev_err(dev, "Can not setup management interrupts\n");
2417 goto err_disable_msix;
2420 ena_com_set_admin_polling_mode(ena_dev, false);
2422 ena_com_admin_aenq_enable(ena_dev);
2427 ena_disable_msix(adapter);
2432 static void ena_fw_reset_device(struct work_struct *work)
2434 struct ena_com_dev_get_features_ctx get_feat_ctx;
2435 struct ena_adapter *adapter =
2436 container_of(work, struct ena_adapter, reset_task);
2437 struct net_device *netdev = adapter->netdev;
2438 struct ena_com_dev *ena_dev = adapter->ena_dev;
2439 struct pci_dev *pdev = adapter->pdev;
2440 bool dev_up, wd_state;
2443 del_timer_sync(&adapter->timer_service);
2447 dev_up = test_bit(ENA_FLAG_DEV_UP, &adapter->flags);
2448 ena_com_set_admin_running_state(ena_dev, false);
2450 /* After calling ena_close the tx queues and the napi
2451 * are disabled so no one can interfere or touch the
2456 rc = ena_com_dev_reset(ena_dev);
2458 dev_err(&pdev->dev, "Device reset failed\n");
2462 ena_free_mgmnt_irq(adapter);
2464 ena_disable_msix(adapter);
2466 ena_com_abort_admin_commands(ena_dev);
2468 ena_com_wait_for_abort_completion(ena_dev);
2470 ena_com_admin_destroy(ena_dev);
2472 ena_com_mmio_reg_read_request_destroy(ena_dev);
2474 /* Finish with the destroy part. Start the init part */
2476 rc = ena_device_init(ena_dev, adapter->pdev, &get_feat_ctx, &wd_state);
2478 dev_err(&pdev->dev, "Can not initialize device\n");
2481 adapter->wd_state = wd_state;
2483 rc = ena_device_validate_params(adapter, &get_feat_ctx);
2485 dev_err(&pdev->dev, "Validation of device parameters failed\n");
2486 goto err_device_destroy;
2489 rc = ena_enable_msix_and_set_admin_interrupts(adapter,
2490 adapter->num_queues);
2492 dev_err(&pdev->dev, "Enable MSI-X failed\n");
2493 goto err_device_destroy;
2495 /* If the interface was up before the reset bring it up */
2497 rc = ena_up(adapter);
2499 dev_err(&pdev->dev, "Failed to create I/O queues\n");
2500 goto err_disable_msix;
2504 mod_timer(&adapter->timer_service, round_jiffies(jiffies + HZ));
2508 dev_err(&pdev->dev, "Device reset completed successfully\n");
2512 ena_free_mgmnt_irq(adapter);
2513 ena_disable_msix(adapter);
2515 ena_com_admin_destroy(ena_dev);
2520 "Reset attempt failed. Can not reset the device\n");
2523 static void check_for_missing_tx_completions(struct ena_adapter *adapter)
2525 struct ena_tx_buffer *tx_buf;
2526 unsigned long last_jiffies;
2527 struct ena_ring *tx_ring;
2531 /* Make sure the driver doesn't turn the device in other process */
2534 if (!test_bit(ENA_FLAG_DEV_UP, &adapter->flags))
2537 budget = ENA_MONITORED_TX_QUEUES;
2539 for (i = adapter->last_monitored_tx_qid; i < adapter->num_queues; i++) {
2540 tx_ring = &adapter->tx_ring[i];
2542 for (j = 0; j < tx_ring->ring_size; j++) {
2543 tx_buf = &tx_ring->tx_buffer_info[j];
2544 last_jiffies = tx_buf->last_jiffies;
2545 if (unlikely(last_jiffies && time_is_before_jiffies(last_jiffies + TX_TIMEOUT))) {
2546 netif_notice(adapter, tx_err, adapter->netdev,
2547 "Found a Tx that wasn't completed on time, qid %d, index %d.\n",
2550 u64_stats_update_begin(&tx_ring->syncp);
2551 missed_tx = tx_ring->tx_stats.missing_tx_comp++;
2552 u64_stats_update_end(&tx_ring->syncp);
2554 /* Clear last jiffies so the lost buffer won't
2557 tx_buf->last_jiffies = 0;
2559 if (unlikely(missed_tx > MAX_NUM_OF_TIMEOUTED_PACKETS)) {
2560 netif_err(adapter, tx_err, adapter->netdev,
2561 "The number of lost tx completion is above the threshold (%d > %d). Reset the device\n",
2562 missed_tx, MAX_NUM_OF_TIMEOUTED_PACKETS);
2563 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2573 adapter->last_monitored_tx_qid = i % adapter->num_queues;
2576 /* Check for keep alive expiration */
2577 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
2579 unsigned long keep_alive_expired;
2581 if (!adapter->wd_state)
2584 keep_alive_expired = round_jiffies(adapter->last_keep_alive_jiffies
2585 + ENA_DEVICE_KALIVE_TIMEOUT);
2586 if (unlikely(time_is_before_jiffies(keep_alive_expired))) {
2587 netif_err(adapter, drv, adapter->netdev,
2588 "Keep alive watchdog timeout.\n");
2589 u64_stats_update_begin(&adapter->syncp);
2590 adapter->dev_stats.wd_expired++;
2591 u64_stats_update_end(&adapter->syncp);
2592 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2596 static void check_for_admin_com_state(struct ena_adapter *adapter)
2598 if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
2599 netif_err(adapter, drv, adapter->netdev,
2600 "ENA admin queue is not in running state!\n");
2601 u64_stats_update_begin(&adapter->syncp);
2602 adapter->dev_stats.admin_q_pause++;
2603 u64_stats_update_end(&adapter->syncp);
2604 set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
2608 static void ena_update_host_info(struct ena_admin_host_info *host_info,
2609 struct net_device *netdev)
2611 host_info->supported_network_features[0] =
2612 netdev->features & GENMASK_ULL(31, 0);
2613 host_info->supported_network_features[1] =
2614 (netdev->features & GENMASK_ULL(63, 32)) >> 32;
2617 static void ena_timer_service(unsigned long data)
2619 struct ena_adapter *adapter = (struct ena_adapter *)data;
2620 u8 *debug_area = adapter->ena_dev->host_attr.debug_area_virt_addr;
2621 struct ena_admin_host_info *host_info =
2622 adapter->ena_dev->host_attr.host_info;
2624 check_for_missing_keep_alive(adapter);
2626 check_for_admin_com_state(adapter);
2628 check_for_missing_tx_completions(adapter);
2631 ena_dump_stats_to_buf(adapter, debug_area);
2634 ena_update_host_info(host_info, adapter->netdev);
2636 if (unlikely(test_and_clear_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags))) {
2637 netif_err(adapter, drv, adapter->netdev,
2638 "Trigger reset is on\n");
2639 ena_dump_stats_to_dmesg(adapter);
2640 queue_work(ena_wq, &adapter->reset_task);
2644 /* Reset the timer */
2645 mod_timer(&adapter->timer_service, jiffies + HZ);
2648 static int ena_calc_io_queue_num(struct pci_dev *pdev,
2649 struct ena_com_dev *ena_dev,
2650 struct ena_com_dev_get_features_ctx *get_feat_ctx)
2652 int io_sq_num, io_queue_num;
2654 /* In case of LLQ use the llq number in the get feature cmd */
2655 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2656 io_sq_num = get_feat_ctx->max_queues.max_llq_num;
2658 if (io_sq_num == 0) {
2660 "Trying to use LLQ but llq_num is 0. Fall back into regular queues\n");
2662 ena_dev->tx_mem_queue_type =
2663 ENA_ADMIN_PLACEMENT_POLICY_HOST;
2664 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2667 io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2670 io_queue_num = min_t(int, num_possible_cpus(), ENA_MAX_NUM_IO_QUEUES);
2671 io_queue_num = min_t(int, io_queue_num, io_sq_num);
2672 io_queue_num = min_t(int, io_queue_num,
2673 get_feat_ctx->max_queues.max_cq_num);
2674 /* 1 IRQ for for mgmnt and 1 IRQs for each IO direction */
2675 io_queue_num = min_t(int, io_queue_num, pci_msix_vec_count(pdev) - 1);
2676 if (unlikely(!io_queue_num)) {
2677 dev_err(&pdev->dev, "The device doesn't have io queues\n");
2681 return io_queue_num;
2684 static void ena_set_push_mode(struct pci_dev *pdev, struct ena_com_dev *ena_dev,
2685 struct ena_com_dev_get_features_ctx *get_feat_ctx)
2689 has_mem_bar = pci_select_bars(pdev, IORESOURCE_MEM) & BIT(ENA_MEM_BAR);
2691 /* Enable push mode if device supports LLQ */
2692 if (has_mem_bar && (get_feat_ctx->max_queues.max_llq_num > 0))
2693 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_DEV;
2695 ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
2698 static void ena_set_dev_offloads(struct ena_com_dev_get_features_ctx *feat,
2699 struct net_device *netdev)
2701 netdev_features_t dev_features = 0;
2703 /* Set offload features */
2704 if (feat->offload.tx &
2705 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)
2706 dev_features |= NETIF_F_IP_CSUM;
2708 if (feat->offload.tx &
2709 ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)
2710 dev_features |= NETIF_F_IPV6_CSUM;
2712 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
2713 dev_features |= NETIF_F_TSO;
2715 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
2716 dev_features |= NETIF_F_TSO6;
2718 if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_ECN_MASK)
2719 dev_features |= NETIF_F_TSO_ECN;
2721 if (feat->offload.rx_supported &
2722 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK)
2723 dev_features |= NETIF_F_RXCSUM;
2725 if (feat->offload.rx_supported &
2726 ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
2727 dev_features |= NETIF_F_RXCSUM;
2736 netdev->hw_features |= netdev->features;
2737 netdev->vlan_features |= netdev->features;
2740 static void ena_set_conf_feat_params(struct ena_adapter *adapter,
2741 struct ena_com_dev_get_features_ctx *feat)
2743 struct net_device *netdev = adapter->netdev;
2745 /* Copy mac address */
2746 if (!is_valid_ether_addr(feat->dev_attr.mac_addr)) {
2747 eth_hw_addr_random(netdev);
2748 ether_addr_copy(adapter->mac_addr, netdev->dev_addr);
2750 ether_addr_copy(adapter->mac_addr, feat->dev_attr.mac_addr);
2751 ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
2754 /* Set offload features */
2755 ena_set_dev_offloads(feat, netdev);
2757 adapter->max_mtu = feat->dev_attr.max_mtu;
2760 static int ena_rss_init_default(struct ena_adapter *adapter)
2762 struct ena_com_dev *ena_dev = adapter->ena_dev;
2763 struct device *dev = &adapter->pdev->dev;
2767 rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
2769 dev_err(dev, "Cannot init indirect table\n");
2773 for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
2774 val = ethtool_rxfh_indir_default(i, adapter->num_queues);
2775 rc = ena_com_indirect_table_fill_entry(ena_dev, i,
2776 ENA_IO_RXQ_IDX(val));
2777 if (unlikely(rc && (rc != -EPERM))) {
2778 dev_err(dev, "Cannot fill indirect table\n");
2779 goto err_fill_indir;
2783 rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
2784 ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
2785 if (unlikely(rc && (rc != -EPERM))) {
2786 dev_err(dev, "Cannot fill hash function\n");
2787 goto err_fill_indir;
2790 rc = ena_com_set_default_hash_ctrl(ena_dev);
2791 if (unlikely(rc && (rc != -EPERM))) {
2792 dev_err(dev, "Cannot fill hash control\n");
2793 goto err_fill_indir;
2799 ena_com_rss_destroy(ena_dev);
2805 static void ena_release_bars(struct ena_com_dev *ena_dev, struct pci_dev *pdev)
2809 release_bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
2810 pci_release_selected_regions(pdev, release_bars);
2813 static int ena_calc_queue_size(struct pci_dev *pdev,
2814 struct ena_com_dev *ena_dev,
2815 u16 *max_tx_sgl_size,
2816 u16 *max_rx_sgl_size,
2817 struct ena_com_dev_get_features_ctx *get_feat_ctx)
2819 u32 queue_size = ENA_DEFAULT_RING_SIZE;
2821 queue_size = min_t(u32, queue_size,
2822 get_feat_ctx->max_queues.max_cq_depth);
2823 queue_size = min_t(u32, queue_size,
2824 get_feat_ctx->max_queues.max_sq_depth);
2826 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
2827 queue_size = min_t(u32, queue_size,
2828 get_feat_ctx->max_queues.max_llq_depth);
2830 queue_size = rounddown_pow_of_two(queue_size);
2832 if (unlikely(!queue_size)) {
2833 dev_err(&pdev->dev, "Invalid queue size\n");
2837 *max_tx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2838 get_feat_ctx->max_queues.max_packet_tx_descs);
2839 *max_rx_sgl_size = min_t(u16, ENA_PKT_MAX_BUFS,
2840 get_feat_ctx->max_queues.max_packet_rx_descs);
2845 /* ena_probe - Device Initialization Routine
2846 * @pdev: PCI device information struct
2847 * @ent: entry in ena_pci_tbl
2849 * Returns 0 on success, negative on failure
2851 * ena_probe initializes an adapter identified by a pci_dev structure.
2852 * The OS initialization, configuring of the adapter private structure,
2853 * and a hardware reset occur.
2855 static int ena_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2857 struct ena_com_dev_get_features_ctx get_feat_ctx;
2858 static int version_printed;
2859 struct net_device *netdev;
2860 struct ena_adapter *adapter;
2861 struct ena_com_dev *ena_dev = NULL;
2862 static int adapters_found;
2863 int io_queue_num, bars, rc;
2865 u16 tx_sgl_size = 0;
2866 u16 rx_sgl_size = 0;
2869 dev_dbg(&pdev->dev, "%s\n", __func__);
2871 if (version_printed++ == 0)
2872 dev_info(&pdev->dev, "%s", version);
2874 rc = pci_enable_device_mem(pdev);
2876 dev_err(&pdev->dev, "pci_enable_device_mem() failed!\n");
2880 pci_set_master(pdev);
2882 ena_dev = vzalloc(sizeof(*ena_dev));
2885 goto err_disable_device;
2888 bars = pci_select_bars(pdev, IORESOURCE_MEM) & ENA_BAR_MASK;
2889 rc = pci_request_selected_regions(pdev, bars, DRV_MODULE_NAME);
2891 dev_err(&pdev->dev, "pci_request_selected_regions failed %d\n",
2893 goto err_free_ena_dev;
2896 ena_dev->reg_bar = ioremap(pci_resource_start(pdev, ENA_REG_BAR),
2897 pci_resource_len(pdev, ENA_REG_BAR));
2898 if (!ena_dev->reg_bar) {
2899 dev_err(&pdev->dev, "failed to remap regs bar\n");
2901 goto err_free_region;
2904 ena_dev->dmadev = &pdev->dev;
2906 rc = ena_device_init(ena_dev, pdev, &get_feat_ctx, &wd_state);
2908 dev_err(&pdev->dev, "ena device init failed\n");
2911 goto err_free_region;
2914 ena_set_push_mode(pdev, ena_dev, &get_feat_ctx);
2916 if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
2917 ena_dev->mem_bar = ioremap_wc(pci_resource_start(pdev, ENA_MEM_BAR),
2918 pci_resource_len(pdev, ENA_MEM_BAR));
2919 if (!ena_dev->mem_bar) {
2921 goto err_device_destroy;
2925 /* initial Tx interrupt delay, Assumes 1 usec granularity.
2926 * Updated during device initialization with the real granularity
2928 ena_dev->intr_moder_tx_interval = ENA_INTR_INITIAL_TX_INTERVAL_USECS;
2929 io_queue_num = ena_calc_io_queue_num(pdev, ena_dev, &get_feat_ctx);
2930 queue_size = ena_calc_queue_size(pdev, ena_dev, &tx_sgl_size,
2931 &rx_sgl_size, &get_feat_ctx);
2932 if ((queue_size <= 0) || (io_queue_num <= 0)) {
2934 goto err_device_destroy;
2937 dev_info(&pdev->dev, "creating %d io queues. queue size: %d\n",
2938 io_queue_num, queue_size);
2940 /* dev zeroed in init_etherdev */
2941 netdev = alloc_etherdev_mq(sizeof(struct ena_adapter), io_queue_num);
2943 dev_err(&pdev->dev, "alloc_etherdev_mq failed\n");
2945 goto err_device_destroy;
2948 SET_NETDEV_DEV(netdev, &pdev->dev);
2950 adapter = netdev_priv(netdev);
2951 pci_set_drvdata(pdev, adapter);
2953 adapter->ena_dev = ena_dev;
2954 adapter->netdev = netdev;
2955 adapter->pdev = pdev;
2957 ena_set_conf_feat_params(adapter, &get_feat_ctx);
2959 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
2961 adapter->tx_ring_size = queue_size;
2962 adapter->rx_ring_size = queue_size;
2964 adapter->max_tx_sgl_size = tx_sgl_size;
2965 adapter->max_rx_sgl_size = rx_sgl_size;
2967 adapter->num_queues = io_queue_num;
2968 adapter->last_monitored_tx_qid = 0;
2970 adapter->rx_copybreak = ENA_DEFAULT_RX_COPYBREAK;
2971 adapter->wd_state = wd_state;
2973 snprintf(adapter->name, ENA_NAME_MAX_LEN, "ena_%d", adapters_found);
2975 rc = ena_com_init_interrupt_moderation(adapter->ena_dev);
2978 "Failed to query interrupt moderation feature\n");
2979 goto err_netdev_destroy;
2981 ena_init_io_rings(adapter);
2983 netdev->netdev_ops = &ena_netdev_ops;
2984 netdev->watchdog_timeo = TX_TIMEOUT;
2985 ena_set_ethtool_ops(netdev);
2987 netdev->priv_flags |= IFF_UNICAST_FLT;
2989 u64_stats_init(&adapter->syncp);
2991 rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
2994 "Failed to enable and set the admin interrupts\n");
2995 goto err_worker_destroy;
2997 rc = ena_rss_init_default(adapter);
2998 if (rc && (rc != -EPERM)) {
2999 dev_err(&pdev->dev, "Cannot init RSS rc: %d\n", rc);
3003 ena_config_debug_area(adapter);
3005 memcpy(adapter->netdev->perm_addr, adapter->mac_addr, netdev->addr_len);
3007 netif_carrier_off(netdev);
3009 rc = register_netdev(netdev);
3011 dev_err(&pdev->dev, "Cannot register net device\n");
3015 INIT_WORK(&adapter->suspend_io_task, ena_device_io_suspend);
3016 INIT_WORK(&adapter->resume_io_task, ena_device_io_resume);
3017 INIT_WORK(&adapter->reset_task, ena_fw_reset_device);
3019 adapter->last_keep_alive_jiffies = jiffies;
3021 init_timer(&adapter->timer_service);
3022 adapter->timer_service.expires = round_jiffies(jiffies + HZ);
3023 adapter->timer_service.function = ena_timer_service;
3024 adapter->timer_service.data = (unsigned long)adapter;
3026 add_timer(&adapter->timer_service);
3028 dev_info(&pdev->dev, "%s found at mem %lx, mac addr %pM Queues %d\n",
3029 DEVICE_NAME, (long)pci_resource_start(pdev, 0),
3030 netdev->dev_addr, io_queue_num);
3032 set_bit(ENA_FLAG_DEVICE_RUNNING, &adapter->flags);
3039 ena_com_delete_debug_area(ena_dev);
3040 ena_com_rss_destroy(ena_dev);
3042 ena_com_dev_reset(ena_dev);
3043 ena_free_mgmnt_irq(adapter);
3044 ena_disable_msix(adapter);
3046 ena_com_destroy_interrupt_moderation(ena_dev);
3047 del_timer(&adapter->timer_service);
3048 cancel_work_sync(&adapter->suspend_io_task);
3049 cancel_work_sync(&adapter->resume_io_task);
3051 free_netdev(netdev);
3053 ena_com_delete_host_info(ena_dev);
3054 ena_com_admin_destroy(ena_dev);
3056 ena_release_bars(ena_dev, pdev);
3060 pci_disable_device(pdev);
3064 /*****************************************************************************/
3065 static int ena_sriov_configure(struct pci_dev *dev, int numvfs)
3070 rc = pci_enable_sriov(dev, numvfs);
3073 "pci_enable_sriov failed to enable: %d vfs with the error: %d\n",
3082 pci_disable_sriov(dev);
3089 /*****************************************************************************/
3090 /*****************************************************************************/
3092 /* ena_remove - Device Removal Routine
3093 * @pdev: PCI device information struct
3095 * ena_remove is called by the PCI subsystem to alert the driver
3096 * that it should release a PCI device.
3098 static void ena_remove(struct pci_dev *pdev)
3100 struct ena_adapter *adapter = pci_get_drvdata(pdev);
3101 struct ena_com_dev *ena_dev;
3102 struct net_device *netdev;
3105 /* This device didn't load properly and it's resources
3106 * already released, nothing to do
3110 ena_dev = adapter->ena_dev;
3111 netdev = adapter->netdev;
3113 #ifdef CONFIG_RFS_ACCEL
3114 if ((adapter->msix_vecs >= 1) && (netdev->rx_cpu_rmap)) {
3115 free_irq_cpu_rmap(netdev->rx_cpu_rmap);
3116 netdev->rx_cpu_rmap = NULL;
3118 #endif /* CONFIG_RFS_ACCEL */
3120 unregister_netdev(netdev);
3121 del_timer_sync(&adapter->timer_service);
3123 cancel_work_sync(&adapter->reset_task);
3125 cancel_work_sync(&adapter->suspend_io_task);
3127 cancel_work_sync(&adapter->resume_io_task);
3129 ena_com_dev_reset(ena_dev);
3131 ena_free_mgmnt_irq(adapter);
3133 ena_disable_msix(adapter);
3135 free_netdev(netdev);
3137 ena_com_mmio_reg_read_request_destroy(ena_dev);
3139 ena_com_abort_admin_commands(ena_dev);
3141 ena_com_wait_for_abort_completion(ena_dev);
3143 ena_com_admin_destroy(ena_dev);
3145 ena_com_rss_destroy(ena_dev);
3147 ena_com_delete_debug_area(ena_dev);
3149 ena_com_delete_host_info(ena_dev);
3151 ena_release_bars(ena_dev, pdev);
3153 pci_disable_device(pdev);
3155 ena_com_destroy_interrupt_moderation(ena_dev);
3160 static struct pci_driver ena_pci_driver = {
3161 .name = DRV_MODULE_NAME,
3162 .id_table = ena_pci_tbl,
3164 .remove = ena_remove,
3165 .sriov_configure = ena_sriov_configure,
3168 static int __init ena_init(void)
3170 pr_info("%s", version);
3172 ena_wq = create_singlethread_workqueue(DRV_MODULE_NAME);
3174 pr_err("Failed to create workqueue\n");
3178 return pci_register_driver(&ena_pci_driver);
3181 static void __exit ena_cleanup(void)
3183 pci_unregister_driver(&ena_pci_driver);
3186 destroy_workqueue(ena_wq);
3191 /******************************************************************************
3192 ******************************** AENQ Handlers *******************************
3193 *****************************************************************************/
3194 /* ena_update_on_link_change:
3195 * Notify the network interface about the change in link status
3197 static void ena_update_on_link_change(void *adapter_data,
3198 struct ena_admin_aenq_entry *aenq_e)
3200 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3201 struct ena_admin_aenq_link_change_desc *aenq_desc =
3202 (struct ena_admin_aenq_link_change_desc *)aenq_e;
3203 int status = aenq_desc->flags &
3204 ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
3207 netdev_dbg(adapter->netdev, "%s\n", __func__);
3208 set_bit(ENA_FLAG_LINK_UP, &adapter->flags);
3209 netif_carrier_on(adapter->netdev);
3211 clear_bit(ENA_FLAG_LINK_UP, &adapter->flags);
3212 netif_carrier_off(adapter->netdev);
3216 static void ena_keep_alive_wd(void *adapter_data,
3217 struct ena_admin_aenq_entry *aenq_e)
3219 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3221 adapter->last_keep_alive_jiffies = jiffies;
3224 static void ena_notification(void *adapter_data,
3225 struct ena_admin_aenq_entry *aenq_e)
3227 struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3229 WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
3230 "Invalid group(%x) expected %x\n",
3231 aenq_e->aenq_common_desc.group,
3232 ENA_ADMIN_NOTIFICATION);
3234 switch (aenq_e->aenq_common_desc.syndrom) {
3235 case ENA_ADMIN_SUSPEND:
3236 /* Suspend just the IO queues.
3237 * We deliberately don't suspend admin so the timer and
3238 * the keep_alive events should remain.
3240 queue_work(ena_wq, &adapter->suspend_io_task);
3242 case ENA_ADMIN_RESUME:
3243 queue_work(ena_wq, &adapter->resume_io_task);
3246 netif_err(adapter, drv, adapter->netdev,
3247 "Invalid aenq notification link state %d\n",
3248 aenq_e->aenq_common_desc.syndrom);
3252 /* This handler will called for unknown event group or unimplemented handlers*/
3253 static void unimplemented_aenq_handler(void *data,
3254 struct ena_admin_aenq_entry *aenq_e)
3256 struct ena_adapter *adapter = (struct ena_adapter *)data;
3258 netif_err(adapter, drv, adapter->netdev,
3259 "Unknown event was received or event with unimplemented handler\n");
3262 static struct ena_aenq_handlers aenq_handlers = {
3264 [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
3265 [ENA_ADMIN_NOTIFICATION] = ena_notification,
3266 [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
3268 .unimplemented_handler = unimplemented_aenq_handler
3271 module_init(ena_init);
3272 module_exit(ena_cleanup);