xref: /freebsd/sys/dev/ena/ena.c (revision d12f7bfc1746d03ffa1f24efd5fb95d38cc42bab)
19b8d05b8SZbigniew Bodek /*-
29b8d05b8SZbigniew Bodek  * BSD LICENSE
39b8d05b8SZbigniew Bodek  *
49b8d05b8SZbigniew Bodek  * Copyright (c) 2015-2017 Amazon.com, Inc. or its affiliates.
59b8d05b8SZbigniew Bodek  * All rights reserved.
69b8d05b8SZbigniew Bodek  *
79b8d05b8SZbigniew Bodek  * Redistribution and use in source and binary forms, with or without
89b8d05b8SZbigniew Bodek  * modification, are permitted provided that the following conditions
99b8d05b8SZbigniew Bodek  * are met:
109b8d05b8SZbigniew Bodek  *
119b8d05b8SZbigniew Bodek  * 1. Redistributions of source code must retain the above copyright
129b8d05b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer.
139b8d05b8SZbigniew Bodek  *
149b8d05b8SZbigniew Bodek  * 2. Redistributions in binary form must reproduce the above copyright
159b8d05b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer in the
169b8d05b8SZbigniew Bodek  *    documentation and/or other materials provided with the distribution.
179b8d05b8SZbigniew Bodek  *
189b8d05b8SZbigniew Bodek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199b8d05b8SZbigniew Bodek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
209b8d05b8SZbigniew Bodek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
219b8d05b8SZbigniew Bodek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
229b8d05b8SZbigniew Bodek  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239b8d05b8SZbigniew Bodek  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
249b8d05b8SZbigniew Bodek  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
259b8d05b8SZbigniew Bodek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
269b8d05b8SZbigniew Bodek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279b8d05b8SZbigniew Bodek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
289b8d05b8SZbigniew Bodek  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299b8d05b8SZbigniew Bodek  */
309b8d05b8SZbigniew Bodek #include <sys/cdefs.h>
319b8d05b8SZbigniew Bodek __FBSDID("$FreeBSD$");
329b8d05b8SZbigniew Bodek 
339b8d05b8SZbigniew Bodek #include <sys/param.h>
349b8d05b8SZbigniew Bodek #include <sys/systm.h>
359b8d05b8SZbigniew Bodek #include <sys/bus.h>
369b8d05b8SZbigniew Bodek #include <sys/endian.h>
379b8d05b8SZbigniew Bodek #include <sys/kernel.h>
389b8d05b8SZbigniew Bodek #include <sys/kthread.h>
399b8d05b8SZbigniew Bodek #include <sys/malloc.h>
409b8d05b8SZbigniew Bodek #include <sys/mbuf.h>
419b8d05b8SZbigniew Bodek #include <sys/module.h>
429b8d05b8SZbigniew Bodek #include <sys/rman.h>
439b8d05b8SZbigniew Bodek #include <sys/smp.h>
449b8d05b8SZbigniew Bodek #include <sys/socket.h>
459b8d05b8SZbigniew Bodek #include <sys/sockio.h>
469b8d05b8SZbigniew Bodek #include <sys/sysctl.h>
479b8d05b8SZbigniew Bodek #include <sys/taskqueue.h>
489b8d05b8SZbigniew Bodek #include <sys/time.h>
499b8d05b8SZbigniew Bodek #include <sys/eventhandler.h>
509b8d05b8SZbigniew Bodek 
519b8d05b8SZbigniew Bodek #include <machine/bus.h>
529b8d05b8SZbigniew Bodek #include <machine/resource.h>
539b8d05b8SZbigniew Bodek #include <machine/in_cksum.h>
549b8d05b8SZbigniew Bodek 
559b8d05b8SZbigniew Bodek #include <net/bpf.h>
569b8d05b8SZbigniew Bodek #include <net/ethernet.h>
579b8d05b8SZbigniew Bodek #include <net/if.h>
589b8d05b8SZbigniew Bodek #include <net/if_var.h>
599b8d05b8SZbigniew Bodek #include <net/if_arp.h>
609b8d05b8SZbigniew Bodek #include <net/if_dl.h>
619b8d05b8SZbigniew Bodek #include <net/if_media.h>
629b8d05b8SZbigniew Bodek #include <net/if_types.h>
639b8d05b8SZbigniew Bodek #include <net/if_vlan_var.h>
649b8d05b8SZbigniew Bodek 
659b8d05b8SZbigniew Bodek #include <netinet/in_systm.h>
669b8d05b8SZbigniew Bodek #include <netinet/in.h>
679b8d05b8SZbigniew Bodek #include <netinet/if_ether.h>
689b8d05b8SZbigniew Bodek #include <netinet/ip.h>
699b8d05b8SZbigniew Bodek #include <netinet/ip6.h>
709b8d05b8SZbigniew Bodek #include <netinet/tcp.h>
719b8d05b8SZbigniew Bodek #include <netinet/udp.h>
729b8d05b8SZbigniew Bodek 
739b8d05b8SZbigniew Bodek #include <dev/pci/pcivar.h>
749b8d05b8SZbigniew Bodek #include <dev/pci/pcireg.h>
759b8d05b8SZbigniew Bodek 
769b8d05b8SZbigniew Bodek #include "ena.h"
779b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
789b8d05b8SZbigniew Bodek 
799b8d05b8SZbigniew Bodek /*********************************************************
809b8d05b8SZbigniew Bodek  *  Function prototypes
819b8d05b8SZbigniew Bodek  *********************************************************/
829b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
839b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
849b8d05b8SZbigniew Bodek static int	ena_allocate_pci_resources(struct ena_adapter*);
859b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
869b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
879b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
889b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
899b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
909b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
919b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
92cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
939b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
949b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
959b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
969b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
979b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
989b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
999b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1009b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1019b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1029b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
10343fefd16SMarcin Wojtas static inline int validate_rx_req_id(struct ena_ring *, uint16_t);
1049b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1059b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1069b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1079b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1089b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1099b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1109b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1119b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1129b8d05b8SZbigniew Bodek static int	ena_refill_rx_bufs(struct ena_ring *, uint32_t);
1139b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1149b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1159b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1169b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1179b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1189b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1199b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1209b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1219b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1229b8d05b8SZbigniew Bodek static int	ena_tx_cleanup(struct ena_ring *);
123efe6ab18SMarcin Wojtas static void	ena_deferred_rx_cleanup(void *, int);
1249b8d05b8SZbigniew Bodek static int	ena_rx_cleanup(struct ena_ring *);
125fceb9387SMarcin Wojtas static inline int validate_tx_req_id(struct ena_ring *, uint16_t);
1269b8d05b8SZbigniew Bodek static void	ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *,
1279b8d05b8SZbigniew Bodek     struct mbuf *);
1289b8d05b8SZbigniew Bodek static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *,
1299b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *, uint16_t *);
1309b8d05b8SZbigniew Bodek static inline void ena_rx_checksum(struct ena_ring *, struct ena_com_rx_ctx *,
1319b8d05b8SZbigniew Bodek     struct mbuf *);
1329b8d05b8SZbigniew Bodek static void	ena_handle_msix(void *);
1339b8d05b8SZbigniew Bodek static int	ena_enable_msix(struct ena_adapter *);
1349b8d05b8SZbigniew Bodek static void	ena_setup_mgmnt_intr(struct ena_adapter *);
1359b8d05b8SZbigniew Bodek static void	ena_setup_io_intr(struct ena_adapter *);
1369b8d05b8SZbigniew Bodek static int	ena_request_mgmnt_irq(struct ena_adapter *);
1379b8d05b8SZbigniew Bodek static int	ena_request_io_irq(struct ena_adapter *);
1389b8d05b8SZbigniew Bodek static void	ena_free_mgmnt_irq(struct ena_adapter *);
1399b8d05b8SZbigniew Bodek static void	ena_free_io_irq(struct ena_adapter *);
1409b8d05b8SZbigniew Bodek static void	ena_free_irqs(struct ena_adapter*);
1419b8d05b8SZbigniew Bodek static void	ena_disable_msix(struct ena_adapter *);
1429b8d05b8SZbigniew Bodek static void	ena_unmask_all_io_irqs(struct ena_adapter *);
1439b8d05b8SZbigniew Bodek static int	ena_rss_configure(struct ena_adapter *);
1449b8d05b8SZbigniew Bodek static int	ena_up_complete(struct ena_adapter *);
1459b8d05b8SZbigniew Bodek static int	ena_up(struct ena_adapter *);
1469b8d05b8SZbigniew Bodek static void	ena_down(struct ena_adapter *);
1479b8d05b8SZbigniew Bodek static uint64_t	ena_get_counter(if_t, ift_counter);
1489b8d05b8SZbigniew Bodek static int	ena_media_change(if_t);
1499b8d05b8SZbigniew Bodek static void	ena_media_status(if_t, struct ifmediareq *);
1509b8d05b8SZbigniew Bodek static void	ena_init(void *);
1519b8d05b8SZbigniew Bodek static int	ena_ioctl(if_t, u_long, caddr_t);
1529b8d05b8SZbigniew Bodek static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
1539b8d05b8SZbigniew Bodek static void	ena_update_host_info(struct ena_admin_host_info *, if_t);
1549b8d05b8SZbigniew Bodek static void	ena_update_hwassist(struct ena_adapter *);
1559b8d05b8SZbigniew Bodek static int	ena_setup_ifnet(device_t, struct ena_adapter *,
1569b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1579b8d05b8SZbigniew Bodek static void	ena_tx_csum(struct ena_com_tx_ctx *, struct mbuf *);
1581b069f1cSZbigniew Bodek static int	ena_check_and_collapse_mbuf(struct ena_ring *tx_ring,
1591b069f1cSZbigniew Bodek     struct mbuf **mbuf);
1601e9fb899SZbigniew Bodek static int	ena_xmit_mbuf(struct ena_ring *, struct mbuf **);
1619b8d05b8SZbigniew Bodek static void	ena_start_xmit(struct ena_ring *);
1629b8d05b8SZbigniew Bodek static int	ena_mq_start(if_t, struct mbuf *);
1639b8d05b8SZbigniew Bodek static void	ena_deferred_mq_start(void *, int);
1649b8d05b8SZbigniew Bodek static void	ena_qflush(if_t);
1659b8d05b8SZbigniew Bodek static int	ena_calc_io_queue_num(struct ena_adapter *,
1669b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1679b8d05b8SZbigniew Bodek static int	ena_calc_queue_size(struct ena_adapter *, uint16_t *,
1689b8d05b8SZbigniew Bodek     uint16_t *, struct ena_com_dev_get_features_ctx *);
1699b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1709b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
1719b8d05b8SZbigniew Bodek static void	ena_config_host_info(struct ena_com_dev *);
1729b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1739b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1749b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1759b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
1769b8d05b8SZbigniew Bodek static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *,
1779b8d05b8SZbigniew Bodek     int);
1789b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1799b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1809b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1819b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1829b8d05b8SZbigniew Bodek 
1839b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1849b8d05b8SZbigniew Bodek 
1859b8d05b8SZbigniew Bodek static SYSCTL_NODE(_hw, OID_AUTO, ena, CTLFLAG_RD, 0, "ENA driver parameters");
1869b8d05b8SZbigniew Bodek 
1879b8d05b8SZbigniew Bodek /*
1889b8d05b8SZbigniew Bodek  * Tuneable number of buffers in the buf-ring (drbr)
1899b8d05b8SZbigniew Bodek  */
1909b8d05b8SZbigniew Bodek static int ena_buf_ring_size = 4096;
1919b8d05b8SZbigniew Bodek SYSCTL_INT(_hw_ena, OID_AUTO, buf_ring_size, CTLFLAG_RWTUN,
1929b8d05b8SZbigniew Bodek     &ena_buf_ring_size, 0, "Size of the bufring");
1939b8d05b8SZbigniew Bodek 
1944e8acd84SMarcin Wojtas /*
1954e8acd84SMarcin Wojtas  * Logging level for changing verbosity of the output
1964e8acd84SMarcin Wojtas  */
1974e8acd84SMarcin Wojtas int ena_log_level = ENA_ALERT | ENA_WARNING;
1984e8acd84SMarcin Wojtas SYSCTL_INT(_hw_ena, OID_AUTO, log_level, CTLFLAG_RWTUN,
1994e8acd84SMarcin Wojtas     &ena_log_level, 0, "Logging level indicating verbosity of the logs");
2004e8acd84SMarcin Wojtas 
2019b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
2029b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
2039b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
2049b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
2059b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
2069b8d05b8SZbigniew Bodek     /* Last entry */
2079b8d05b8SZbigniew Bodek     { 0, 0, 0 }
2089b8d05b8SZbigniew Bodek };
2099b8d05b8SZbigniew Bodek 
2109b8d05b8SZbigniew Bodek /*
2119b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
2129b8d05b8SZbigniew Bodek  */
2139b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
2149b8d05b8SZbigniew Bodek 
2159b8d05b8SZbigniew Bodek void
2169b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2179b8d05b8SZbigniew Bodek {
2180bdffe59SMarcin Wojtas 	if (error != 0)
2199b8d05b8SZbigniew Bodek 		return;
2209b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
2219b8d05b8SZbigniew Bodek }
2229b8d05b8SZbigniew Bodek 
2239b8d05b8SZbigniew Bodek int
2249b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
2259b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
2269b8d05b8SZbigniew Bodek {
2279b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2280bdffe59SMarcin Wojtas 	uint32_t maxsize;
2290bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2309b8d05b8SZbigniew Bodek 	int error;
2319b8d05b8SZbigniew Bodek 
2320bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2330bdffe59SMarcin Wojtas 
2340bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2353f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2369b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2370bdffe59SMarcin Wojtas 
2389b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2399b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2408a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2418a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2429b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2439b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2449b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2459b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2469b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2479b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2489b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2499b8d05b8SZbigniew Bodek 	    &dma->tag);
2503f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2514e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2529b8d05b8SZbigniew Bodek 		goto fail_tag;
2539b8d05b8SZbigniew Bodek 	}
2549b8d05b8SZbigniew Bodek 
2559b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2569b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2573f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2584e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2594e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2609b8d05b8SZbigniew Bodek 		goto fail_map_create;
2619b8d05b8SZbigniew Bodek 	}
2629b8d05b8SZbigniew Bodek 
2639b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2649b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2659b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2663f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
2674e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2689b8d05b8SZbigniew Bodek 		goto fail_map_load;
2699b8d05b8SZbigniew Bodek 	}
2709b8d05b8SZbigniew Bodek 
2719b8d05b8SZbigniew Bodek 	return (0);
2729b8d05b8SZbigniew Bodek 
2739b8d05b8SZbigniew Bodek fail_map_load:
2749b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2757d2544e6SMarcin Wojtas fail_map_create:
2769b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2779b8d05b8SZbigniew Bodek fail_tag:
2789b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2799b8d05b8SZbigniew Bodek 
2809b8d05b8SZbigniew Bodek 	return (error);
2819b8d05b8SZbigniew Bodek }
2829b8d05b8SZbigniew Bodek 
2839b8d05b8SZbigniew Bodek static int
2849b8d05b8SZbigniew Bodek ena_allocate_pci_resources(struct ena_adapter* adapter)
2859b8d05b8SZbigniew Bodek {
2869b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2879b8d05b8SZbigniew Bodek 	int rid;
2889b8d05b8SZbigniew Bodek 
2899b8d05b8SZbigniew Bodek 	rid = PCIR_BAR(ENA_REG_BAR);
2909b8d05b8SZbigniew Bodek 	adapter->memory = NULL;
2919b8d05b8SZbigniew Bodek 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
2929b8d05b8SZbigniew Bodek 	    &rid, RF_ACTIVE);
2933f9ed7abSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
2949b8d05b8SZbigniew Bodek 		device_printf(pdev, "Unable to allocate bus resource: "
2959b8d05b8SZbigniew Bodek 		    "registers\n");
2969b8d05b8SZbigniew Bodek 		return (ENXIO);
2979b8d05b8SZbigniew Bodek 	}
2989b8d05b8SZbigniew Bodek 
2999b8d05b8SZbigniew Bodek 	return (0);
3009b8d05b8SZbigniew Bodek }
3019b8d05b8SZbigniew Bodek 
3029b8d05b8SZbigniew Bodek static void
3039b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
3049b8d05b8SZbigniew Bodek {
3059b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
3069b8d05b8SZbigniew Bodek 
3079b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
3089b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3099b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
3109b8d05b8SZbigniew Bodek 	}
3119b8d05b8SZbigniew Bodek 
3129b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
3139b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3149b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
3159b8d05b8SZbigniew Bodek 	}
3169b8d05b8SZbigniew Bodek }
3179b8d05b8SZbigniew Bodek 
3189b8d05b8SZbigniew Bodek static int
3199b8d05b8SZbigniew Bodek ena_probe(device_t dev)
3209b8d05b8SZbigniew Bodek {
3219b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3229b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3239b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3249b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3259b8d05b8SZbigniew Bodek 
3269b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3279b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3289b8d05b8SZbigniew Bodek 
3299b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3309b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3319b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3329b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
3339b8d05b8SZbigniew Bodek 			ena_trace(ENA_DBG, "vendor=%x device=%x ",
3349b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3359b8d05b8SZbigniew Bodek 
3369b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3379b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3389b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3399b8d05b8SZbigniew Bodek 		}
3409b8d05b8SZbigniew Bodek 
3419b8d05b8SZbigniew Bodek 		ent++;
3429b8d05b8SZbigniew Bodek 
3439b8d05b8SZbigniew Bodek 	}
3449b8d05b8SZbigniew Bodek 
3459b8d05b8SZbigniew Bodek 	return (ENXIO);
3469b8d05b8SZbigniew Bodek }
3479b8d05b8SZbigniew Bodek 
3489b8d05b8SZbigniew Bodek static int
3499b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3509b8d05b8SZbigniew Bodek {
3519b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3523cfadb28SMarcin Wojtas 	int rc;
3539b8d05b8SZbigniew Bodek 
3543cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3559b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3563cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3573cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3583cfadb28SMarcin Wojtas 		return (EINVAL);
3599b8d05b8SZbigniew Bodek 	}
3609b8d05b8SZbigniew Bodek 
3619b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3623cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
3633cfadb28SMarcin Wojtas 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
3643cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3653cfadb28SMarcin Wojtas 	} else {
3663cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3673cfadb28SMarcin Wojtas 		    new_mtu);
3683cfadb28SMarcin Wojtas 	}
3699b8d05b8SZbigniew Bodek 
3703cfadb28SMarcin Wojtas 	return (rc);
3719b8d05b8SZbigniew Bodek }
3729b8d05b8SZbigniew Bodek 
3739b8d05b8SZbigniew Bodek static inline void
3749b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3759b8d05b8SZbigniew Bodek {
3769b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3779b8d05b8SZbigniew Bodek 
3789b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3799b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3809b8d05b8SZbigniew Bodek }
3819b8d05b8SZbigniew Bodek 
3829b8d05b8SZbigniew Bodek static inline void
3839b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3849b8d05b8SZbigniew Bodek {
3859b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3869b8d05b8SZbigniew Bodek 
3879b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3889b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3899b8d05b8SZbigniew Bodek }
3909b8d05b8SZbigniew Bodek 
3919b8d05b8SZbigniew Bodek static inline void
3929b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3939b8d05b8SZbigniew Bodek {
3949b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3959b8d05b8SZbigniew Bodek 
3969b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3979b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
3989b8d05b8SZbigniew Bodek }
3999b8d05b8SZbigniew Bodek 
4009b8d05b8SZbigniew Bodek static void
4019b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
4029b8d05b8SZbigniew Bodek     uint16_t qid)
4039b8d05b8SZbigniew Bodek {
4049b8d05b8SZbigniew Bodek 
4059b8d05b8SZbigniew Bodek 	ring->qid = qid;
4069b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
4079b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
408*d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
409*d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
4109b8d05b8SZbigniew Bodek }
4119b8d05b8SZbigniew Bodek 
412cd5d5804SMarcin Wojtas static void
4139b8d05b8SZbigniew Bodek ena_init_io_rings(struct ena_adapter *adapter)
4149b8d05b8SZbigniew Bodek {
4159b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
4169b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
4179b8d05b8SZbigniew Bodek 	struct ena_que *que;
4189b8d05b8SZbigniew Bodek 	int i;
4199b8d05b8SZbigniew Bodek 
4209b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
4219b8d05b8SZbigniew Bodek 
4229b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
4239b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4249b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4259b8d05b8SZbigniew Bodek 
4269b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4279b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4289b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4299b8d05b8SZbigniew Bodek 
4309b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4319b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
4329b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4339b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4349b8d05b8SZbigniew Bodek 		txr->smoothed_interval =
4359b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
4369b8d05b8SZbigniew Bodek 
4379b8d05b8SZbigniew Bodek 		/* Allocate a buf ring */
4389b8d05b8SZbigniew Bodek 		txr->br = buf_ring_alloc(ena_buf_ring_size, M_DEVBUF,
4399b8d05b8SZbigniew Bodek 		    M_WAITOK, &txr->ring_mtx);
4409b8d05b8SZbigniew Bodek 
4419b8d05b8SZbigniew Bodek 		/* Alloc TX statistics. */
4429b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4439b8d05b8SZbigniew Bodek 		    sizeof(txr->tx_stats));
4449b8d05b8SZbigniew Bodek 
4459b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4469b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4479b8d05b8SZbigniew Bodek 		rxr->smoothed_interval =
4489b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
4499b8d05b8SZbigniew Bodek 
4509b8d05b8SZbigniew Bodek 		/* Alloc RX statistics. */
4519b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4529b8d05b8SZbigniew Bodek 		    sizeof(rxr->rx_stats));
4539b8d05b8SZbigniew Bodek 
4549b8d05b8SZbigniew Bodek 		/* Initialize locks */
4559b8d05b8SZbigniew Bodek 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4569b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4579b8d05b8SZbigniew Bodek 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4589b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4599b8d05b8SZbigniew Bodek 
4609b8d05b8SZbigniew Bodek 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
461efe6ab18SMarcin Wojtas 		mtx_init(&rxr->ring_mtx, rxr->mtx_name, NULL, MTX_DEF);
4629b8d05b8SZbigniew Bodek 
4639b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4649b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4659b8d05b8SZbigniew Bodek 		que->id = i;
4669b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4679b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4689b8d05b8SZbigniew Bodek 
4699b8d05b8SZbigniew Bodek 		txr->que = que;
4709b8d05b8SZbigniew Bodek 		rxr->que = que;
471efe6ab18SMarcin Wojtas 
472efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4739b8d05b8SZbigniew Bodek 	}
4749b8d05b8SZbigniew Bodek }
4759b8d05b8SZbigniew Bodek 
4769b8d05b8SZbigniew Bodek static void
4779b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4789b8d05b8SZbigniew Bodek {
4799b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4809b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4819b8d05b8SZbigniew Bodek 
4829b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4839b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4849b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4859b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4869b8d05b8SZbigniew Bodek 
4877d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4887d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4897d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4907d2544e6SMarcin Wojtas 
4919b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
492efe6ab18SMarcin Wojtas 	mtx_destroy(&rxr->ring_mtx);
4939b8d05b8SZbigniew Bodek }
4949b8d05b8SZbigniew Bodek 
4959b8d05b8SZbigniew Bodek static void
4969b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
4979b8d05b8SZbigniew Bodek {
4989b8d05b8SZbigniew Bodek 	int i;
4999b8d05b8SZbigniew Bodek 
5009b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
5019b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
5029b8d05b8SZbigniew Bodek 
5039b8d05b8SZbigniew Bodek }
5049b8d05b8SZbigniew Bodek 
5059b8d05b8SZbigniew Bodek static int
5069b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
5079b8d05b8SZbigniew Bodek {
5089b8d05b8SZbigniew Bodek 	int ret;
5099b8d05b8SZbigniew Bodek 
5109b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
5119b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
5129b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5138a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5148a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5159b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5169b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
5178a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
5189b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
5199b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5209b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5219b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
5229b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5239b8d05b8SZbigniew Bodek 
5249b8d05b8SZbigniew Bodek 	return (ret);
5259b8d05b8SZbigniew Bodek }
5269b8d05b8SZbigniew Bodek 
5279b8d05b8SZbigniew Bodek static int
5289b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5299b8d05b8SZbigniew Bodek {
5309b8d05b8SZbigniew Bodek 	int ret;
5319b8d05b8SZbigniew Bodek 
5329b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5339b8d05b8SZbigniew Bodek 
5343f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5359b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5369b8d05b8SZbigniew Bodek 
5379b8d05b8SZbigniew Bodek 	return (ret);
5389b8d05b8SZbigniew Bodek }
5399b8d05b8SZbigniew Bodek 
5409b8d05b8SZbigniew Bodek static int
5419b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5429b8d05b8SZbigniew Bodek {
5439b8d05b8SZbigniew Bodek 	int ret;
5449b8d05b8SZbigniew Bodek 
5459b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5469b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5479b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5488a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5498a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5509b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5519b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsize 		     */
5524727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
5539b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsegsize 	     */
5549b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5559b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5569b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5579b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5589b8d05b8SZbigniew Bodek 
5599b8d05b8SZbigniew Bodek 	return (ret);
5609b8d05b8SZbigniew Bodek }
5619b8d05b8SZbigniew Bodek 
5629b8d05b8SZbigniew Bodek static int
5639b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5649b8d05b8SZbigniew Bodek {
5659b8d05b8SZbigniew Bodek 	int ret;
5669b8d05b8SZbigniew Bodek 
5679b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5689b8d05b8SZbigniew Bodek 
5693f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5709b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5719b8d05b8SZbigniew Bodek 
5729b8d05b8SZbigniew Bodek 	return (ret);
5739b8d05b8SZbigniew Bodek }
5749b8d05b8SZbigniew Bodek 
5759b8d05b8SZbigniew Bodek /**
5769b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
5779b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
5789b8d05b8SZbigniew Bodek  * @qid: queue index
5799b8d05b8SZbigniew Bodek  *
5809b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
5819b8d05b8SZbigniew Bodek  **/
5829b8d05b8SZbigniew Bodek static int
5839b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
5849b8d05b8SZbigniew Bodek {
5859b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
5869b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
5879b8d05b8SZbigniew Bodek 	int size, i, err;
5889b8d05b8SZbigniew Bodek 
5899b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
5909b8d05b8SZbigniew Bodek 
5919b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5923f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
5937d2544e6SMarcin Wojtas 		return (ENOMEM);
5949b8d05b8SZbigniew Bodek 
5959b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
5969b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5973f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
5987d2544e6SMarcin Wojtas 		goto err_buf_info_free;
5999b8d05b8SZbigniew Bodek 
6009b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
6019b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
6029b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
6039b8d05b8SZbigniew Bodek 
6049b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
6059b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
6069b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
6079b8d05b8SZbigniew Bodek 
6089b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
6099b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
6109b8d05b8SZbigniew Bodek 
6119b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
612b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6139b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
614b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6159b8d05b8SZbigniew Bodek 
6169b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6179b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6189b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6199b8d05b8SZbigniew Bodek 		    &tx_ring->tx_buffer_info[i].map);
6203f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
6214e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
6229b8d05b8SZbigniew Bodek 			     "Unable to create Tx DMA map for buffer %d\n", i);
6237d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
6249b8d05b8SZbigniew Bodek 		}
6259b8d05b8SZbigniew Bodek 	}
6269b8d05b8SZbigniew Bodek 
6279b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6289b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6299b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6309b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6313f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
6324e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT,
6339b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6349b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6357d2544e6SMarcin Wojtas 		goto err_buf_info_unmap;
6369b8d05b8SZbigniew Bodek 	}
6379b8d05b8SZbigniew Bodek 
6389b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
6399b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
6409b8d05b8SZbigniew Bodek 
6419b8d05b8SZbigniew Bodek 	return (0);
6429b8d05b8SZbigniew Bodek 
6437d2544e6SMarcin Wojtas err_buf_info_unmap:
6449b8d05b8SZbigniew Bodek 	while (i--) {
6459b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6469b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6479b8d05b8SZbigniew Bodek 	}
648cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6497d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
6507d2544e6SMarcin Wojtas err_buf_info_free:
651cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6527d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
6537d2544e6SMarcin Wojtas 
6549b8d05b8SZbigniew Bodek 	return (ENOMEM);
6559b8d05b8SZbigniew Bodek }
6569b8d05b8SZbigniew Bodek 
6579b8d05b8SZbigniew Bodek /**
6589b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
6599b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6609b8d05b8SZbigniew Bodek  * @qid: queue index
6619b8d05b8SZbigniew Bodek  *
6629b8d05b8SZbigniew Bodek  * Free all transmit software resources
6639b8d05b8SZbigniew Bodek  **/
6649b8d05b8SZbigniew Bodek static void
6659b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
6669b8d05b8SZbigniew Bodek {
6679b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
6689b8d05b8SZbigniew Bodek 
6699b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
6709b8d05b8SZbigniew Bodek 	    NULL))
6719b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
6729b8d05b8SZbigniew Bodek 
6739b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
6749b8d05b8SZbigniew Bodek 
675b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6769b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
6779b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
6789b8d05b8SZbigniew Bodek 
6799b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
6809b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
6819b8d05b8SZbigniew Bodek 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
6829b8d05b8SZbigniew Bodek 		tx_ring->tx_buffer_info[i].mbuf = NULL;
6839b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag,
6849b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6859b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6869b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6879b8d05b8SZbigniew Bodek 	}
688416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6899b8d05b8SZbigniew Bodek 
6909b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
691cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6929b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
6939b8d05b8SZbigniew Bodek 
694cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6959b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
6969b8d05b8SZbigniew Bodek }
6979b8d05b8SZbigniew Bodek 
6989b8d05b8SZbigniew Bodek /**
6999b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7009b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7019b8d05b8SZbigniew Bodek  *
7029b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7039b8d05b8SZbigniew Bodek  **/
7049b8d05b8SZbigniew Bodek static int
7059b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7069b8d05b8SZbigniew Bodek {
7079b8d05b8SZbigniew Bodek 	int i, rc;
7089b8d05b8SZbigniew Bodek 
7099b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
7109b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
7110bdffe59SMarcin Wojtas 		if (rc != 0) {
7129b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
7139b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
7149b8d05b8SZbigniew Bodek 			goto err_setup_tx;
7159b8d05b8SZbigniew Bodek 		}
7167d2544e6SMarcin Wojtas 	}
7179b8d05b8SZbigniew Bodek 
7189b8d05b8SZbigniew Bodek 	return (0);
7199b8d05b8SZbigniew Bodek 
7209b8d05b8SZbigniew Bodek err_setup_tx:
7219b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
7229b8d05b8SZbigniew Bodek 	while (i--)
7239b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7249b8d05b8SZbigniew Bodek 	return (rc);
7259b8d05b8SZbigniew Bodek }
7269b8d05b8SZbigniew Bodek 
7279b8d05b8SZbigniew Bodek /**
7289b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
7299b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7309b8d05b8SZbigniew Bodek  *
7319b8d05b8SZbigniew Bodek  * Free all transmit software resources
7329b8d05b8SZbigniew Bodek  **/
7339b8d05b8SZbigniew Bodek static void
7349b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
7359b8d05b8SZbigniew Bodek {
7369b8d05b8SZbigniew Bodek 	int i;
7379b8d05b8SZbigniew Bodek 
7389b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
7399b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7409b8d05b8SZbigniew Bodek }
7419b8d05b8SZbigniew Bodek 
74243fefd16SMarcin Wojtas static inline int
74343fefd16SMarcin Wojtas validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
74443fefd16SMarcin Wojtas {
74543fefd16SMarcin Wojtas 	if (likely(req_id < rx_ring->ring_size))
74643fefd16SMarcin Wojtas 		return (0);
74743fefd16SMarcin Wojtas 
74843fefd16SMarcin Wojtas 	device_printf(rx_ring->adapter->pdev, "Invalid rx req_id: %hu\n",
74943fefd16SMarcin Wojtas 	    req_id);
75043fefd16SMarcin Wojtas 	counter_u64_add(rx_ring->rx_stats.bad_req_id, 1);
75143fefd16SMarcin Wojtas 
75243fefd16SMarcin Wojtas 	/* Trigger device reset */
75343fefd16SMarcin Wojtas 	rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
75443fefd16SMarcin Wojtas 	rx_ring->adapter->trigger_reset = true;
75543fefd16SMarcin Wojtas 
75643fefd16SMarcin Wojtas 	return (EFAULT);
75743fefd16SMarcin Wojtas }
75843fefd16SMarcin Wojtas 
7599b8d05b8SZbigniew Bodek /**
7609b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
7619b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7629b8d05b8SZbigniew Bodek  * @qid: queue index
7639b8d05b8SZbigniew Bodek  *
7649b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7659b8d05b8SZbigniew Bodek  **/
7669b8d05b8SZbigniew Bodek static int
7679b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
7689b8d05b8SZbigniew Bodek {
7699b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
7709b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
7719b8d05b8SZbigniew Bodek 	int size, err, i;
7729b8d05b8SZbigniew Bodek 
7739b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
7749b8d05b8SZbigniew Bodek 
7759b8d05b8SZbigniew Bodek 	/*
7769b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
7779b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
7789b8d05b8SZbigniew Bodek 	 */
7799b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
7809b8d05b8SZbigniew Bodek 
781cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
7829b8d05b8SZbigniew Bodek 
78343fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
78443fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
78543fefd16SMarcin Wojtas 
78643fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
78743fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
78843fefd16SMarcin Wojtas 
7899b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
7909b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
7919b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
7929b8d05b8SZbigniew Bodek 
7939b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
7949b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
7959b8d05b8SZbigniew Bodek 
7969b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
7979b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
7989b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
7999b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
8009b8d05b8SZbigniew Bodek 		if (err != 0) {
8014e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
8029b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8037d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
8049b8d05b8SZbigniew Bodek 		}
8059b8d05b8SZbigniew Bodek 	}
8069b8d05b8SZbigniew Bodek 
8079b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8080bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8099b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8100bdffe59SMarcin Wojtas 		if (err != 0) {
8119b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8129b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8139b8d05b8SZbigniew Bodek 		} else {
8149b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8159b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8169b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8179b8d05b8SZbigniew Bodek 		}
8189b8d05b8SZbigniew Bodek 	}
8199b8d05b8SZbigniew Bodek 
820efe6ab18SMarcin Wojtas 	/* Allocate taskqueues */
821efe6ab18SMarcin Wojtas 	TASK_INIT(&rx_ring->cmpl_task, 0, ena_deferred_rx_cleanup, rx_ring);
822efe6ab18SMarcin Wojtas 	rx_ring->cmpl_tq = taskqueue_create_fast("ena RX completion", M_WAITOK,
823efe6ab18SMarcin Wojtas 	    taskqueue_thread_enqueue, &rx_ring->cmpl_tq);
824efe6ab18SMarcin Wojtas 
825efe6ab18SMarcin Wojtas 	taskqueue_start_threads(&rx_ring->cmpl_tq, 1, PI_NET,
826efe6ab18SMarcin Wojtas 	    "%s rx_ring cmpl %d", device_get_nameunit(adapter->pdev), que->cpu);
827efe6ab18SMarcin Wojtas 
8289b8d05b8SZbigniew Bodek 	return (0);
8299b8d05b8SZbigniew Bodek 
8307d2544e6SMarcin Wojtas err_buf_info_unmap:
8319b8d05b8SZbigniew Bodek 	while (i--) {
8329b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8339b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8349b8d05b8SZbigniew Bodek 	}
8359b8d05b8SZbigniew Bodek 
83643fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
83743fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
838cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8399b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8409b8d05b8SZbigniew Bodek 	return (ENOMEM);
8419b8d05b8SZbigniew Bodek }
8429b8d05b8SZbigniew Bodek 
8439b8d05b8SZbigniew Bodek /**
8449b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
8459b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8469b8d05b8SZbigniew Bodek  * @qid: queue index
8479b8d05b8SZbigniew Bodek  *
8489b8d05b8SZbigniew Bodek  * Free all receive software resources
8499b8d05b8SZbigniew Bodek  **/
8509b8d05b8SZbigniew Bodek static void
8519b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8529b8d05b8SZbigniew Bodek {
8539b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
8549b8d05b8SZbigniew Bodek 
855efe6ab18SMarcin Wojtas 	while (taskqueue_cancel(rx_ring->cmpl_tq, &rx_ring->cmpl_task, NULL) != 0)
856efe6ab18SMarcin Wojtas 		taskqueue_drain(rx_ring->cmpl_tq, &rx_ring->cmpl_task);
857efe6ab18SMarcin Wojtas 
858efe6ab18SMarcin Wojtas 	taskqueue_free(rx_ring->cmpl_tq);
859efe6ab18SMarcin Wojtas 
8609b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
8619b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
8629b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
8639b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
8649b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
8659b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8669b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8679b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8689b8d05b8SZbigniew Bodek 	}
8699b8d05b8SZbigniew Bodek 
8709b8d05b8SZbigniew Bodek 	/* free LRO resources, */
8719b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
8729b8d05b8SZbigniew Bodek 
8739b8d05b8SZbigniew Bodek 	/* free allocated memory */
874cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8759b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8769b8d05b8SZbigniew Bodek 
87743fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
87843fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
8799b8d05b8SZbigniew Bodek }
8809b8d05b8SZbigniew Bodek 
8819b8d05b8SZbigniew Bodek /**
8829b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
8839b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8849b8d05b8SZbigniew Bodek  *
8859b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8869b8d05b8SZbigniew Bodek  **/
8879b8d05b8SZbigniew Bodek static int
8889b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
8899b8d05b8SZbigniew Bodek {
8909b8d05b8SZbigniew Bodek 	int i, rc = 0;
8919b8d05b8SZbigniew Bodek 
8929b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
8939b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
8940bdffe59SMarcin Wojtas 		if (rc != 0) {
8959b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8969b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
8979b8d05b8SZbigniew Bodek 			goto err_setup_rx;
8989b8d05b8SZbigniew Bodek 		}
8997d2544e6SMarcin Wojtas 	}
9009b8d05b8SZbigniew Bodek 	return (0);
9019b8d05b8SZbigniew Bodek 
9029b8d05b8SZbigniew Bodek err_setup_rx:
9039b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
9049b8d05b8SZbigniew Bodek 	while (i--)
9059b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9069b8d05b8SZbigniew Bodek 	return (rc);
9079b8d05b8SZbigniew Bodek }
9089b8d05b8SZbigniew Bodek 
9099b8d05b8SZbigniew Bodek /**
9109b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
9119b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9129b8d05b8SZbigniew Bodek  *
9139b8d05b8SZbigniew Bodek  * Free all receive software resources
9149b8d05b8SZbigniew Bodek  **/
9159b8d05b8SZbigniew Bodek static void
9169b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9179b8d05b8SZbigniew Bodek {
9189b8d05b8SZbigniew Bodek 	int i;
9199b8d05b8SZbigniew Bodek 
9209b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
9219b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9229b8d05b8SZbigniew Bodek }
9239b8d05b8SZbigniew Bodek 
9249b8d05b8SZbigniew Bodek static inline int
9259b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9269b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9279b8d05b8SZbigniew Bodek {
9289b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9299b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9309b8d05b8SZbigniew Bodek 	int nsegs, error;
9314727bda6SMarcin Wojtas 	int mlen;
9329b8d05b8SZbigniew Bodek 
9339b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
9343f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
9359b8d05b8SZbigniew Bodek 		return (0);
9369b8d05b8SZbigniew Bodek 
9379b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
9389b8d05b8SZbigniew Bodek 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
9399b8d05b8SZbigniew Bodek 
9403f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
9414727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
9424727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9434727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
9449b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
9459b8d05b8SZbigniew Bodek 			return (ENOMEM);
9469b8d05b8SZbigniew Bodek 		}
9474727bda6SMarcin Wojtas 		mlen = MCLBYTES;
9484727bda6SMarcin Wojtas 	} else {
9494727bda6SMarcin Wojtas 		mlen = MJUM16BYTES;
9504727bda6SMarcin Wojtas 	}
9519b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
9524727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
9539b8d05b8SZbigniew Bodek 
9549b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
9559b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9569b8d05b8SZbigniew Bodek 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d",
9579b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
9589b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
9599b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
9603f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
9614e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
9629b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
9639b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
9649b8d05b8SZbigniew Bodek 		goto exit;
9659b8d05b8SZbigniew Bodek 
9669b8d05b8SZbigniew Bodek 	}
9679b8d05b8SZbigniew Bodek 
9689b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
9699b8d05b8SZbigniew Bodek 
9709b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
9719b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
9724727bda6SMarcin Wojtas 	ena_buf->len = mlen;
9739b8d05b8SZbigniew Bodek 
9749b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9759b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
9769b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
9779b8d05b8SZbigniew Bodek 
9789b8d05b8SZbigniew Bodek 	return (0);
9799b8d05b8SZbigniew Bodek 
9809b8d05b8SZbigniew Bodek exit:
9819b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9829b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9839b8d05b8SZbigniew Bodek 	return (EFAULT);
9849b8d05b8SZbigniew Bodek }
9859b8d05b8SZbigniew Bodek 
9869b8d05b8SZbigniew Bodek static void
9879b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
9889b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
9899b8d05b8SZbigniew Bodek {
9909b8d05b8SZbigniew Bodek 
9914e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
9924e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
9939b8d05b8SZbigniew Bodek 		return;
9944e8acd84SMarcin Wojtas 	}
9959b8d05b8SZbigniew Bodek 
9969b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
9979b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9989b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9999b8d05b8SZbigniew Bodek }
10009b8d05b8SZbigniew Bodek 
10019b8d05b8SZbigniew Bodek /**
10029b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
10039b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
10049b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
10059b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
10069b8d05b8SZbigniew Bodek  **/
10079b8d05b8SZbigniew Bodek static int
10089b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
10099b8d05b8SZbigniew Bodek {
10109b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
101143fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
10129b8d05b8SZbigniew Bodek 	uint32_t i;
10139b8d05b8SZbigniew Bodek 	int rc;
10149b8d05b8SZbigniew Bodek 
10159b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d",
10169b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10179b8d05b8SZbigniew Bodek 
10189b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10199b8d05b8SZbigniew Bodek 
10209b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
102143fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
102243fefd16SMarcin Wojtas 
10239b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
10249b8d05b8SZbigniew Bodek 		    "RX buffer - next to use: %d", next_to_use);
10259b8d05b8SZbigniew Bodek 
102643fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
102743fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
10289b8d05b8SZbigniew Bodek 
10299b8d05b8SZbigniew Bodek 		rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
10303f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
10314e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10324e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
10334e8acd84SMarcin Wojtas 			    rx_ring->qid);
10349b8d05b8SZbigniew Bodek 			break;
10359b8d05b8SZbigniew Bodek 		}
10369b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
103743fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
10380bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
10394e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10409b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
10419b8d05b8SZbigniew Bodek 			    rx_ring->qid);
10429b8d05b8SZbigniew Bodek 			break;
10439b8d05b8SZbigniew Bodek 		}
10449b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
10459b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
10469b8d05b8SZbigniew Bodek 	}
10479b8d05b8SZbigniew Bodek 
10483f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
10499b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
10504e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING,
10514e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
10524e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
10539b8d05b8SZbigniew Bodek 	}
10549b8d05b8SZbigniew Bodek 
10553f9ed7abSMarcin Wojtas 	if (likely(i != 0)) {
10569b8d05b8SZbigniew Bodek 		wmb();
10579b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
10589b8d05b8SZbigniew Bodek 	}
10599b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
10609b8d05b8SZbigniew Bodek 	return (i);
10619b8d05b8SZbigniew Bodek }
10629b8d05b8SZbigniew Bodek 
10639b8d05b8SZbigniew Bodek static void
10649b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
10659b8d05b8SZbigniew Bodek {
10669b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
10679b8d05b8SZbigniew Bodek 	unsigned int i;
10689b8d05b8SZbigniew Bodek 
10699b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
10709b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
10719b8d05b8SZbigniew Bodek 
10720bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
10739b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
10749b8d05b8SZbigniew Bodek 	}
10759b8d05b8SZbigniew Bodek }
10769b8d05b8SZbigniew Bodek 
10779b8d05b8SZbigniew Bodek /**
10789b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
10799b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
10809b8d05b8SZbigniew Bodek  *
10819b8d05b8SZbigniew Bodek  */
10829b8d05b8SZbigniew Bodek static void
10839b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
10849b8d05b8SZbigniew Bodek {
10859b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
10869b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
10879b8d05b8SZbigniew Bodek 
10889b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
10899b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
10909b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
10919b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
10929b8d05b8SZbigniew Bodek 
10939b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
10944e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
10954e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
10969b8d05b8SZbigniew Bodek 	}
10979b8d05b8SZbigniew Bodek }
10989b8d05b8SZbigniew Bodek 
10999b8d05b8SZbigniew Bodek static void
11009b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
11019b8d05b8SZbigniew Bodek {
11029b8d05b8SZbigniew Bodek 	int i;
11039b8d05b8SZbigniew Bodek 
11049b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
11059b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
11069b8d05b8SZbigniew Bodek }
11079b8d05b8SZbigniew Bodek 
11089b8d05b8SZbigniew Bodek /**
11099b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
11109b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
11119b8d05b8SZbigniew Bodek  * @qid: queue index
11129b8d05b8SZbigniew Bodek  **/
11139b8d05b8SZbigniew Bodek static void
11149b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
11159b8d05b8SZbigniew Bodek {
11164e8acd84SMarcin Wojtas 	bool print_once = true;
11179b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
11189b8d05b8SZbigniew Bodek 
1119416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
11209b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
11219b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
11229b8d05b8SZbigniew Bodek 
11239b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
11249b8d05b8SZbigniew Bodek 			continue;
11259b8d05b8SZbigniew Bodek 
11264e8acd84SMarcin Wojtas 		if (print_once) {
11274e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
11284e8acd84SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x",
11294e8acd84SMarcin Wojtas 			    qid, i);
11304e8acd84SMarcin Wojtas 			print_once = false;
11314e8acd84SMarcin Wojtas 		} else {
11324e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG,
11334e8acd84SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x",
11344e8acd84SMarcin Wojtas 			     qid, i);
11354e8acd84SMarcin Wojtas 		}
11369b8d05b8SZbigniew Bodek 
11379b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
11389b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
11399b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
11409b8d05b8SZbigniew Bodek 	}
1141416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
11429b8d05b8SZbigniew Bodek }
11439b8d05b8SZbigniew Bodek 
11449b8d05b8SZbigniew Bodek static void
11459b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
11469b8d05b8SZbigniew Bodek {
11479b8d05b8SZbigniew Bodek 
11489b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++)
11499b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
11509b8d05b8SZbigniew Bodek }
11519b8d05b8SZbigniew Bodek 
11529b8d05b8SZbigniew Bodek static void
11539b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
11549b8d05b8SZbigniew Bodek {
11559b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11569b8d05b8SZbigniew Bodek 	int i;
11579b8d05b8SZbigniew Bodek 
11589b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11599b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11609b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11619b8d05b8SZbigniew Bodek 	}
11629b8d05b8SZbigniew Bodek }
11639b8d05b8SZbigniew Bodek 
11649b8d05b8SZbigniew Bodek static void
11659b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
11669b8d05b8SZbigniew Bodek {
11679b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11689b8d05b8SZbigniew Bodek 	int i;
11699b8d05b8SZbigniew Bodek 
11709b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11719b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
11729b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11739b8d05b8SZbigniew Bodek 	}
11749b8d05b8SZbigniew Bodek }
11759b8d05b8SZbigniew Bodek 
11769b8d05b8SZbigniew Bodek static void
11779b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
11789b8d05b8SZbigniew Bodek {
11799b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
11809b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
11819b8d05b8SZbigniew Bodek }
11829b8d05b8SZbigniew Bodek 
1183fceb9387SMarcin Wojtas static inline int
11849b8d05b8SZbigniew Bodek validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
11859b8d05b8SZbigniew Bodek {
11864e8acd84SMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
11879b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info = NULL;
11889b8d05b8SZbigniew Bodek 
11899b8d05b8SZbigniew Bodek 	if (likely(req_id < tx_ring->ring_size)) {
11909b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
11910bdffe59SMarcin Wojtas 		if (tx_info->mbuf != NULL)
11920bdffe59SMarcin Wojtas 			return (0);
11939b8d05b8SZbigniew Bodek 	}
11949b8d05b8SZbigniew Bodek 
11954e8acd84SMarcin Wojtas 	if (tx_info->mbuf == NULL)
11964e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
11974e8acd84SMarcin Wojtas 		    "tx_info doesn't have valid mbuf\n");
11984e8acd84SMarcin Wojtas 	else
11994e8acd84SMarcin Wojtas 		device_printf(adapter->pdev, "Invalid req_id: %hu\n", req_id);
12004e8acd84SMarcin Wojtas 
12019b8d05b8SZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
12029b8d05b8SZbigniew Bodek 
12039b8d05b8SZbigniew Bodek 	return (EFAULT);
12049b8d05b8SZbigniew Bodek }
12059b8d05b8SZbigniew Bodek 
12069b8d05b8SZbigniew Bodek static int
12079b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
12089b8d05b8SZbigniew Bodek {
12099b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
12109b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
12119b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
12129b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12139b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
12149b8d05b8SZbigniew Bodek 	int rc, i;
12159b8d05b8SZbigniew Bodek 
12169b8d05b8SZbigniew Bodek 	/* Create TX queues */
12179b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12189b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12199b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
12209b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
12219b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
12229b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
12239b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12249b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12259b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12260bdffe59SMarcin Wojtas 		if (rc != 0) {
12279b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12289b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
12299b8d05b8SZbigniew Bodek 			goto err_tx;
12309b8d05b8SZbigniew Bodek 		}
12319b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
12329b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12339b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12349b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12350bdffe59SMarcin Wojtas 		if (rc != 0) {
12369b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12379b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
12389b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12399b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12409b8d05b8SZbigniew Bodek 			goto err_tx;
12419b8d05b8SZbigniew Bodek 		}
12429b8d05b8SZbigniew Bodek 	}
12439b8d05b8SZbigniew Bodek 
12449b8d05b8SZbigniew Bodek 	/* Create RX queues */
12459b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12469b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12479b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12489b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
12499b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
12509b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
12519b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12529b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12539b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12543f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12559b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12569b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
12579b8d05b8SZbigniew Bodek 			goto err_rx;
12589b8d05b8SZbigniew Bodek 		}
12599b8d05b8SZbigniew Bodek 
12609b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
12619b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12629b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12639b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12643f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12659b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12669b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
12679b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12689b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12699b8d05b8SZbigniew Bodek 			goto err_rx;
12709b8d05b8SZbigniew Bodek 		}
12719b8d05b8SZbigniew Bodek 	}
12729b8d05b8SZbigniew Bodek 
12739b8d05b8SZbigniew Bodek 	return (0);
12749b8d05b8SZbigniew Bodek 
12759b8d05b8SZbigniew Bodek err_rx:
12769b8d05b8SZbigniew Bodek 	while (i--)
12779b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
12789b8d05b8SZbigniew Bodek 	i = adapter->num_queues;
12799b8d05b8SZbigniew Bodek err_tx:
12809b8d05b8SZbigniew Bodek 	while (i--)
12819b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
12829b8d05b8SZbigniew Bodek 
12839b8d05b8SZbigniew Bodek 	return (ENXIO);
12849b8d05b8SZbigniew Bodek }
12859b8d05b8SZbigniew Bodek 
12869b8d05b8SZbigniew Bodek /**
12879b8d05b8SZbigniew Bodek  * ena_tx_cleanup - clear sent packets and corresponding descriptors
12889b8d05b8SZbigniew Bodek  * @tx_ring: ring for which we want to clean packets
12899b8d05b8SZbigniew Bodek  *
12909b8d05b8SZbigniew Bodek  * Once packets are sent, we ask the device in a loop for no longer used
12919b8d05b8SZbigniew Bodek  * descriptors. We find the related mbuf chain in a map (index in an array)
12929b8d05b8SZbigniew Bodek  * and free it, then update ring state.
12939b8d05b8SZbigniew Bodek  * This is performed in "endless" loop, updating ring pointers every
12949b8d05b8SZbigniew Bodek  * TX_COMMIT. The first check of free descriptor is performed before the actual
12959b8d05b8SZbigniew Bodek  * loop, then repeated at the loop end.
12969b8d05b8SZbigniew Bodek  **/
12979b8d05b8SZbigniew Bodek static int
12989b8d05b8SZbigniew Bodek ena_tx_cleanup(struct ena_ring *tx_ring)
12999b8d05b8SZbigniew Bodek {
13009b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
13019b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
13029b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
13039b8d05b8SZbigniew Bodek 	uint16_t req_id;
13049b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
13059b8d05b8SZbigniew Bodek 	unsigned int total_done = 0;
13069b8d05b8SZbigniew Bodek 	int rc;
13079b8d05b8SZbigniew Bodek 	int commit = TX_COMMIT;
13089b8d05b8SZbigniew Bodek 	int budget = TX_BUDGET;
13099b8d05b8SZbigniew Bodek 	int work_done;
13109b8d05b8SZbigniew Bodek 
13119b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
13129b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
13139b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
13149b8d05b8SZbigniew Bodek 	next_to_clean = tx_ring->next_to_clean;
13159b8d05b8SZbigniew Bodek 
13169b8d05b8SZbigniew Bodek 	do {
13179b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info;
13189b8d05b8SZbigniew Bodek 		struct mbuf *mbuf;
13199b8d05b8SZbigniew Bodek 
13209b8d05b8SZbigniew Bodek 		rc = ena_com_tx_comp_req_id_get(io_cq, &req_id);
13213f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
13229b8d05b8SZbigniew Bodek 			break;
13239b8d05b8SZbigniew Bodek 
13249b8d05b8SZbigniew Bodek 		rc = validate_tx_req_id(tx_ring, req_id);
13253f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
13269b8d05b8SZbigniew Bodek 			break;
13279b8d05b8SZbigniew Bodek 
13289b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
13299b8d05b8SZbigniew Bodek 
13309b8d05b8SZbigniew Bodek 		mbuf = tx_info->mbuf;
13319b8d05b8SZbigniew Bodek 
13329b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
13339b8d05b8SZbigniew Bodek 		bintime_clear(&tx_info->timestamp);
13349b8d05b8SZbigniew Bodek 
13353f9ed7abSMarcin Wojtas 		if (likely(tx_info->num_of_bufs != 0)) {
13369b8d05b8SZbigniew Bodek 			/* Map is no longer required */
13379b8d05b8SZbigniew Bodek 			bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
13389b8d05b8SZbigniew Bodek 		}
13399b8d05b8SZbigniew Bodek 
13404e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d mbuf %p completed",
13414e8acd84SMarcin Wojtas 		    tx_ring->qid, mbuf);
13424e8acd84SMarcin Wojtas 
13439b8d05b8SZbigniew Bodek 		m_freem(mbuf);
13449b8d05b8SZbigniew Bodek 
13459b8d05b8SZbigniew Bodek 		total_done += tx_info->tx_descs;
13469b8d05b8SZbigniew Bodek 
13479b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[next_to_clean] = req_id;
13489b8d05b8SZbigniew Bodek 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
13499b8d05b8SZbigniew Bodek 		    tx_ring->ring_size);
13509b8d05b8SZbigniew Bodek 
13513f9ed7abSMarcin Wojtas 		if (unlikely(--commit == 0)) {
13529b8d05b8SZbigniew Bodek 			commit = TX_COMMIT;
13539b8d05b8SZbigniew Bodek 			/* update ring state every TX_COMMIT descriptor */
13549b8d05b8SZbigniew Bodek 			tx_ring->next_to_clean = next_to_clean;
13550bdffe59SMarcin Wojtas 			ena_com_comp_ack(
13560bdffe59SMarcin Wojtas 			    &adapter->ena_dev->io_sq_queues[ena_qid],
13570bdffe59SMarcin Wojtas 			    total_done);
13589b8d05b8SZbigniew Bodek 			ena_com_update_dev_comp_head(io_cq);
13599b8d05b8SZbigniew Bodek 			total_done = 0;
13609b8d05b8SZbigniew Bodek 		}
13613f9ed7abSMarcin Wojtas 	} while (likely(--budget));
13629b8d05b8SZbigniew Bodek 
13639b8d05b8SZbigniew Bodek 	work_done = TX_BUDGET - budget;
13649b8d05b8SZbigniew Bodek 
13654e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d done. total pkts: %d",
13664e8acd84SMarcin Wojtas 	tx_ring->qid, work_done);
13674e8acd84SMarcin Wojtas 
13689b8d05b8SZbigniew Bodek 	/* If there is still something to commit update ring state */
13693f9ed7abSMarcin Wojtas 	if (likely(commit != TX_COMMIT)) {
13709b8d05b8SZbigniew Bodek 		tx_ring->next_to_clean = next_to_clean;
13710bdffe59SMarcin Wojtas 		ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid],
13720bdffe59SMarcin Wojtas 		    total_done);
13739b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(io_cq);
13749b8d05b8SZbigniew Bodek 	}
13759b8d05b8SZbigniew Bodek 
13769b8d05b8SZbigniew Bodek 	taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
13779b8d05b8SZbigniew Bodek 
13789b8d05b8SZbigniew Bodek 	return (work_done);
13799b8d05b8SZbigniew Bodek }
13809b8d05b8SZbigniew Bodek 
13819b8d05b8SZbigniew Bodek static void
13829b8d05b8SZbigniew Bodek ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
13839b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
13849b8d05b8SZbigniew Bodek {
13859b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
13869b8d05b8SZbigniew Bodek 
13873f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support)) {
13889b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
13899b8d05b8SZbigniew Bodek 
13909b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->frag &&
1391bfea0e93SMarcin Wojtas 		    (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
13929b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
13939b8d05b8SZbigniew Bodek 			return;
13949b8d05b8SZbigniew Bodek 		}
13959b8d05b8SZbigniew Bodek 
13969b8d05b8SZbigniew Bodek 		switch (ena_rx_ctx->l3_proto) {
13979b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV4:
13989b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
13999b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14009b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
14019b8d05b8SZbigniew Bodek 				break;
14029b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14039b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
14049b8d05b8SZbigniew Bodek 				break;
14059b8d05b8SZbigniew Bodek 			default:
14069b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
14079b8d05b8SZbigniew Bodek 			}
14089b8d05b8SZbigniew Bodek 			break;
14099b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV6:
14109b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
14119b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14129b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
14139b8d05b8SZbigniew Bodek 				break;
14149b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14159b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
14169b8d05b8SZbigniew Bodek 				break;
14179b8d05b8SZbigniew Bodek 			default:
14189b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
14199b8d05b8SZbigniew Bodek 			}
14209b8d05b8SZbigniew Bodek 			break;
14219b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
14229b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
14239b8d05b8SZbigniew Bodek 			break;
14249b8d05b8SZbigniew Bodek 		default:
14259b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
14269b8d05b8SZbigniew Bodek 		}
14279b8d05b8SZbigniew Bodek 	} else {
14289b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = rx_ring->qid;
14299b8d05b8SZbigniew Bodek 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
14309b8d05b8SZbigniew Bodek 	}
14319b8d05b8SZbigniew Bodek }
14329b8d05b8SZbigniew Bodek 
14339b8d05b8SZbigniew Bodek /**
14349b8d05b8SZbigniew Bodek  * ena_rx_mbuf - assemble mbuf from descriptors
14359b8d05b8SZbigniew Bodek  * @rx_ring: ring for which we want to clean packets
14369b8d05b8SZbigniew Bodek  * @ena_bufs: buffer info
14379b8d05b8SZbigniew Bodek  * @ena_rx_ctx: metadata for this packet(s)
143843fefd16SMarcin Wojtas  * @next_to_clean: ring pointer, will be updated only upon success
14399b8d05b8SZbigniew Bodek  *
14409b8d05b8SZbigniew Bodek  **/
14419b8d05b8SZbigniew Bodek static struct mbuf*
14429b8d05b8SZbigniew Bodek ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
14439b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
14449b8d05b8SZbigniew Bodek {
14459b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
14469b8d05b8SZbigniew Bodek 	struct ena_rx_buffer *rx_info;
14479b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
14489b8d05b8SZbigniew Bodek 	unsigned int descs = ena_rx_ctx->descs;
1449c51a229cSMarcin Wojtas 	int rc;
145043fefd16SMarcin Wojtas 	uint16_t ntc, len, req_id, buf = 0;
14519b8d05b8SZbigniew Bodek 
145243fefd16SMarcin Wojtas 	ntc = *next_to_clean;
14539b8d05b8SZbigniew Bodek 	adapter = rx_ring->adapter;
14549b8d05b8SZbigniew Bodek 
145543fefd16SMarcin Wojtas 	len = ena_bufs[buf].len;
145643fefd16SMarcin Wojtas 	req_id = ena_bufs[buf].req_id;
1457c51a229cSMarcin Wojtas 	rc = validate_rx_req_id(rx_ring, req_id);
1458c51a229cSMarcin Wojtas 	if (unlikely(rc != 0))
1459c51a229cSMarcin Wojtas 		return (NULL);
1460c51a229cSMarcin Wojtas 
146143fefd16SMarcin Wojtas 	rx_info = &rx_ring->rx_buffer_info[req_id];
14621d65b4c0SMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
14631d65b4c0SMarcin Wojtas 		device_printf(adapter->pdev, "NULL mbuf in rx_info");
14641d65b4c0SMarcin Wojtas 		return (NULL);
14651d65b4c0SMarcin Wojtas 	}
146643fefd16SMarcin Wojtas 
14679b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
14689b8d05b8SZbigniew Bodek 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
14699b8d05b8SZbigniew Bodek 
14709b8d05b8SZbigniew Bodek 	mbuf = rx_info->mbuf;
14719b8d05b8SZbigniew Bodek 	mbuf->m_flags |= M_PKTHDR;
14729b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.len = len;
14739b8d05b8SZbigniew Bodek 	mbuf->m_len = len;
14749b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.rcvif = rx_ring->que->adapter->ifp;
14759b8d05b8SZbigniew Bodek 
14769b8d05b8SZbigniew Bodek 	/* Fill mbuf with hash key and it's interpretation for optimization */
14779b8d05b8SZbigniew Bodek 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
14789b8d05b8SZbigniew Bodek 
14799b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
14809b8d05b8SZbigniew Bodek 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
14819b8d05b8SZbigniew Bodek 
14829b8d05b8SZbigniew Bodek 	/* DMA address is not needed anymore, unmap it */
14839b8d05b8SZbigniew Bodek 	bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
14849b8d05b8SZbigniew Bodek 
14859b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
148643fefd16SMarcin Wojtas 	rx_ring->free_rx_ids[ntc] = req_id;
148743fefd16SMarcin Wojtas 	ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
14889b8d05b8SZbigniew Bodek 
14899b8d05b8SZbigniew Bodek 	/*
14909b8d05b8SZbigniew Bodek 	 * While we have more than 1 descriptors for one rcvd packet, append
14919b8d05b8SZbigniew Bodek 	 * other mbufs to the main one
14929b8d05b8SZbigniew Bodek 	 */
14939b8d05b8SZbigniew Bodek 	while (--descs) {
149443fefd16SMarcin Wojtas 		++buf;
149543fefd16SMarcin Wojtas 		len = ena_bufs[buf].len;
149643fefd16SMarcin Wojtas 		req_id = ena_bufs[buf].req_id;
1497c51a229cSMarcin Wojtas 		rc = validate_rx_req_id(rx_ring, req_id);
1498c51a229cSMarcin Wojtas 		if (unlikely(rc != 0)) {
1499c51a229cSMarcin Wojtas 			/*
1500c51a229cSMarcin Wojtas 			 * If the req_id is invalid, then the device will be
1501c51a229cSMarcin Wojtas 			 * reset. In that case we must free all mbufs that
1502c51a229cSMarcin Wojtas 			 * were already gathered.
1503c51a229cSMarcin Wojtas 			 */
1504c51a229cSMarcin Wojtas 			m_freem(mbuf);
1505c51a229cSMarcin Wojtas 			return (NULL);
1506c51a229cSMarcin Wojtas 		}
150743fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
15089b8d05b8SZbigniew Bodek 
150943fefd16SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
151043fefd16SMarcin Wojtas 			device_printf(adapter->pdev, "NULL mbuf in rx_info");
151143fefd16SMarcin Wojtas 			/*
151243fefd16SMarcin Wojtas 			 * If one of the required mbufs was not allocated yet,
151343fefd16SMarcin Wojtas 			 * we can break there.
151443fefd16SMarcin Wojtas 			 * All earlier used descriptors will be reallocated
151543fefd16SMarcin Wojtas 			 * later and not used mbufs can be reused.
151643fefd16SMarcin Wojtas 			 * The next_to_clean pointer will not be updated in case
151743fefd16SMarcin Wojtas 			 * of an error, so caller should advance it manually
151843fefd16SMarcin Wojtas 			 * in error handling routine to keep it up to date
151943fefd16SMarcin Wojtas 			 * with hw ring.
152043fefd16SMarcin Wojtas 			 */
152143fefd16SMarcin Wojtas 			m_freem(mbuf);
152243fefd16SMarcin Wojtas 			return (NULL);
152343fefd16SMarcin Wojtas 		}
152443fefd16SMarcin Wojtas 
15253f9ed7abSMarcin Wojtas 		if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
15269b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
15279b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
15289b8d05b8SZbigniew Bodek 			    mbuf);
15299b8d05b8SZbigniew Bodek 		}
15304e8acd84SMarcin Wojtas 
15314e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH,
15324e8acd84SMarcin Wojtas 		    "rx mbuf updated. len %d", mbuf->m_pkthdr.len);
15334e8acd84SMarcin Wojtas 
15349b8d05b8SZbigniew Bodek 		/* Free already appended mbuf, it won't be useful anymore */
15359b8d05b8SZbigniew Bodek 		bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
15369b8d05b8SZbigniew Bodek 		m_freem(rx_info->mbuf);
15379b8d05b8SZbigniew Bodek 		rx_info->mbuf = NULL;
15389b8d05b8SZbigniew Bodek 
153943fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[ntc] = req_id;
154043fefd16SMarcin Wojtas 		ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
15419b8d05b8SZbigniew Bodek 	}
15429b8d05b8SZbigniew Bodek 
154343fefd16SMarcin Wojtas 	*next_to_clean = ntc;
154443fefd16SMarcin Wojtas 
15459b8d05b8SZbigniew Bodek 	return (mbuf);
15469b8d05b8SZbigniew Bodek }
15479b8d05b8SZbigniew Bodek 
15489b8d05b8SZbigniew Bodek /**
15499b8d05b8SZbigniew Bodek  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
15509b8d05b8SZbigniew Bodek  **/
15519b8d05b8SZbigniew Bodek static inline void
15529b8d05b8SZbigniew Bodek ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
15539b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
15549b8d05b8SZbigniew Bodek {
15559b8d05b8SZbigniew Bodek 
15569b8d05b8SZbigniew Bodek 	/* if IP and error */
15573f9ed7abSMarcin Wojtas 	if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
15583f9ed7abSMarcin Wojtas 	    ena_rx_ctx->l3_csum_err)) {
15599b8d05b8SZbigniew Bodek 		/* ipv4 checksum error */
15609b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.csum_flags = 0;
15619b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
15624e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG, "RX IPv4 header checksum error");
15639b8d05b8SZbigniew Bodek 		return;
15649b8d05b8SZbigniew Bodek 	}
15659b8d05b8SZbigniew Bodek 
15669b8d05b8SZbigniew Bodek 	/* if TCP/UDP */
15679b8d05b8SZbigniew Bodek 	if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
15689b8d05b8SZbigniew Bodek 	    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
15699b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->l4_csum_err) {
15709b8d05b8SZbigniew Bodek 			/* TCP/UDP checksum error */
15719b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = 0;
15729b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
15734e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG, "RX L4 checksum error");
15749b8d05b8SZbigniew Bodek 		} else {
15759b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
15769b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
15779b8d05b8SZbigniew Bodek 		}
15789b8d05b8SZbigniew Bodek 	}
15799b8d05b8SZbigniew Bodek }
15809b8d05b8SZbigniew Bodek 
1581efe6ab18SMarcin Wojtas static void
1582efe6ab18SMarcin Wojtas ena_deferred_rx_cleanup(void *arg, int pending)
1583efe6ab18SMarcin Wojtas {
1584efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring = arg;
1585efe6ab18SMarcin Wojtas 	int budget = CLEAN_BUDGET;
1586efe6ab18SMarcin Wojtas 
1587efe6ab18SMarcin Wojtas 	ENA_RING_MTX_LOCK(rx_ring);
1588efe6ab18SMarcin Wojtas 	/*
1589efe6ab18SMarcin Wojtas 	 * If deferred task was executed, perform cleanup of all awaiting
1590efe6ab18SMarcin Wojtas 	 * descs (or until given budget is depleted to avoid infinite loop).
1591efe6ab18SMarcin Wojtas 	 */
15923f9ed7abSMarcin Wojtas 	while (likely(budget--)) {
1593efe6ab18SMarcin Wojtas 		if (ena_rx_cleanup(rx_ring) == 0)
1594efe6ab18SMarcin Wojtas 			break;
1595efe6ab18SMarcin Wojtas 	}
1596efe6ab18SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(rx_ring);
1597efe6ab18SMarcin Wojtas }
1598efe6ab18SMarcin Wojtas 
15999b8d05b8SZbigniew Bodek /**
16009b8d05b8SZbigniew Bodek  * ena_rx_cleanup - handle rx irq
16019b8d05b8SZbigniew Bodek  * @arg: ring for which irq is being handled
16029b8d05b8SZbigniew Bodek  **/
16039b8d05b8SZbigniew Bodek static int
16049b8d05b8SZbigniew Bodek ena_rx_cleanup(struct ena_ring *rx_ring)
16059b8d05b8SZbigniew Bodek {
16069b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
16079b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
16089b8d05b8SZbigniew Bodek 	struct ena_com_rx_ctx ena_rx_ctx;
16099b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
16109b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
16119b8d05b8SZbigniew Bodek 	if_t ifp;
16129b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
16139b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
16149b8d05b8SZbigniew Bodek 	uint32_t refill_required;
16159b8d05b8SZbigniew Bodek 	uint32_t refill_threshold;
16169b8d05b8SZbigniew Bodek 	uint32_t do_if_input = 0;
16179b8d05b8SZbigniew Bodek 	unsigned int qid;
161843fefd16SMarcin Wojtas 	int rc, i;
16199b8d05b8SZbigniew Bodek 	int budget = RX_BUDGET;
16209b8d05b8SZbigniew Bodek 
16219b8d05b8SZbigniew Bodek 	adapter = rx_ring->que->adapter;
16229b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
16239b8d05b8SZbigniew Bodek 	qid = rx_ring->que->id;
16249b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_RXQ_IDX(qid);
16259b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
16269b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
16279b8d05b8SZbigniew Bodek 	next_to_clean = rx_ring->next_to_clean;
16289b8d05b8SZbigniew Bodek 
16294e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG, "rx: qid %d", qid);
16304e8acd84SMarcin Wojtas 
16319b8d05b8SZbigniew Bodek 	do {
16329b8d05b8SZbigniew Bodek 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
16339b8d05b8SZbigniew Bodek 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
16349b8d05b8SZbigniew Bodek 		ena_rx_ctx.descs = 0;
16359b8d05b8SZbigniew Bodek 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
16369b8d05b8SZbigniew Bodek 
16370bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
16389b8d05b8SZbigniew Bodek 			goto error;
16399b8d05b8SZbigniew Bodek 
16409b8d05b8SZbigniew Bodek 		if (unlikely(ena_rx_ctx.descs == 0))
16419b8d05b8SZbigniew Bodek 			break;
16429b8d05b8SZbigniew Bodek 
16434e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
16444e8acd84SMarcin Wojtas 		    "descs #: %d l3 proto %d l4 proto %d hash: %x",
16454e8acd84SMarcin Wojtas 		    rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
16464e8acd84SMarcin Wojtas 		    ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
16474e8acd84SMarcin Wojtas 
16489b8d05b8SZbigniew Bodek 		/* Receive mbuf from the ring */
16499b8d05b8SZbigniew Bodek 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
16509b8d05b8SZbigniew Bodek 		    &ena_rx_ctx, &next_to_clean);
16519b8d05b8SZbigniew Bodek 
16529b8d05b8SZbigniew Bodek 		/* Exit if we failed to retrieve a buffer */
16530bdffe59SMarcin Wojtas 		if (unlikely(mbuf == NULL)) {
165443fefd16SMarcin Wojtas 			for (i = 0; i < ena_rx_ctx.descs; ++i) {
165543fefd16SMarcin Wojtas 				rx_ring->free_rx_ids[next_to_clean] =
165643fefd16SMarcin Wojtas 				    rx_ring->ena_bufs[i].req_id;
165743fefd16SMarcin Wojtas 				next_to_clean =
165843fefd16SMarcin Wojtas 				    ENA_RX_RING_IDX_NEXT(next_to_clean,
165943fefd16SMarcin Wojtas 				    rx_ring->ring_size);
166043fefd16SMarcin Wojtas 
166143fefd16SMarcin Wojtas 			}
16629b8d05b8SZbigniew Bodek 			break;
16639b8d05b8SZbigniew Bodek 		}
16649b8d05b8SZbigniew Bodek 
16650bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_RXCSUM) != 0) ||
16660bdffe59SMarcin Wojtas 		    ((ifp->if_capenable & IFCAP_RXCSUM_IPV6) != 0)) {
16679b8d05b8SZbigniew Bodek 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
16689b8d05b8SZbigniew Bodek 		}
16699b8d05b8SZbigniew Bodek 
167030217e2dSMarcin Wojtas 		counter_enter();
167130217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
167230217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
167330217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
167430217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
167530217e2dSMarcin Wojtas 		counter_exit();
16769b8d05b8SZbigniew Bodek 		/*
16779b8d05b8SZbigniew Bodek 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
16789b8d05b8SZbigniew Bodek 		 * should be computed by hardware.
16799b8d05b8SZbigniew Bodek 		 */
16809b8d05b8SZbigniew Bodek 		do_if_input = 1;
16810bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_LRO) != 0)  &&
16820bdffe59SMarcin Wojtas 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
16830bdffe59SMarcin Wojtas 		    (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
16849b8d05b8SZbigniew Bodek 			/*
16859b8d05b8SZbigniew Bodek 			 * Send to the stack if:
16869b8d05b8SZbigniew Bodek 			 *  - LRO not enabled, or
16879b8d05b8SZbigniew Bodek 			 *  - no LRO resources, or
16889b8d05b8SZbigniew Bodek 			 *  - lro enqueue fails
16899b8d05b8SZbigniew Bodek 			 */
16900bdffe59SMarcin Wojtas 			if ((rx_ring->lro.lro_cnt != 0) &&
16910bdffe59SMarcin Wojtas 			    (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
16929b8d05b8SZbigniew Bodek 					do_if_input = 0;
16939b8d05b8SZbigniew Bodek 		}
16940bdffe59SMarcin Wojtas 		if (do_if_input != 0) {
16950bdffe59SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_RXPTH,
16960bdffe59SMarcin Wojtas 			    "calling if_input() with mbuf %p", mbuf);
16979b8d05b8SZbigniew Bodek 			(*ifp->if_input)(ifp, mbuf);
16989b8d05b8SZbigniew Bodek 		}
16999b8d05b8SZbigniew Bodek 
170030217e2dSMarcin Wojtas 		counter_enter();
170130217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
170230217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
170330217e2dSMarcin Wojtas 		counter_exit();
17049b8d05b8SZbigniew Bodek 	} while (--budget);
17059b8d05b8SZbigniew Bodek 
17069b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = next_to_clean;
17079b8d05b8SZbigniew Bodek 
1708a195fab0SMarcin Wojtas 	refill_required = ena_com_free_desc(io_sq);
17095a990212SMarcin Wojtas 	refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
17109b8d05b8SZbigniew Bodek 
17119b8d05b8SZbigniew Bodek 	if (refill_required > refill_threshold) {
17129b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
17139b8d05b8SZbigniew Bodek 		ena_refill_rx_bufs(rx_ring, refill_required);
17149b8d05b8SZbigniew Bodek 	}
17159b8d05b8SZbigniew Bodek 
17169b8d05b8SZbigniew Bodek 	tcp_lro_flush_all(&rx_ring->lro);
17179b8d05b8SZbigniew Bodek 
17189b8d05b8SZbigniew Bodek 	return (RX_BUDGET - budget);
17199b8d05b8SZbigniew Bodek 
17209b8d05b8SZbigniew Bodek error:
17219b8d05b8SZbigniew Bodek 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
1722c9b099ecSMarcin Wojtas 
1723c9b099ecSMarcin Wojtas 	/* Too many desc from the device. Trigger reset */
1724c9b099ecSMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
1725c9b099ecSMarcin Wojtas 	adapter->trigger_reset = true;
1726c9b099ecSMarcin Wojtas 
1727c9b099ecSMarcin Wojtas 	return (0);
17289b8d05b8SZbigniew Bodek }
17299b8d05b8SZbigniew Bodek 
17309b8d05b8SZbigniew Bodek /*********************************************************************
17319b8d05b8SZbigniew Bodek  *
17329b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
17339b8d05b8SZbigniew Bodek  *
17349b8d05b8SZbigniew Bodek  **********************************************************************/
17359b8d05b8SZbigniew Bodek 
17369b8d05b8SZbigniew Bodek /**
17379b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
17389b8d05b8SZbigniew Bodek  * @arg: interrupt number
17399b8d05b8SZbigniew Bodek  **/
17409b8d05b8SZbigniew Bodek static void
17419b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
17429b8d05b8SZbigniew Bodek {
17439b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
17449b8d05b8SZbigniew Bodek 
17459b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
17469b8d05b8SZbigniew Bodek 	if (likely(adapter->running))
17479b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
17489b8d05b8SZbigniew Bodek }
17499b8d05b8SZbigniew Bodek 
17509b8d05b8SZbigniew Bodek /**
17519b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
17529b8d05b8SZbigniew Bodek  * @arg: interrupt number
17539b8d05b8SZbigniew Bodek  **/
17549b8d05b8SZbigniew Bodek static void
17559b8d05b8SZbigniew Bodek ena_handle_msix(void *arg)
17569b8d05b8SZbigniew Bodek {
17579b8d05b8SZbigniew Bodek 	struct ena_que	*que = arg;
17589b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = que->adapter;
17599b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
17609b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
17619b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
17629b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
17639b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
17649b8d05b8SZbigniew Bodek 	int qid, ena_qid;
17659b8d05b8SZbigniew Bodek 	int txc, rxc, i;
17669b8d05b8SZbigniew Bodek 
17673f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
17689b8d05b8SZbigniew Bodek 		return;
17699b8d05b8SZbigniew Bodek 
17709b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "MSI-X TX/RX routine");
17719b8d05b8SZbigniew Bodek 
17729b8d05b8SZbigniew Bodek 	tx_ring = que->tx_ring;
17739b8d05b8SZbigniew Bodek 	rx_ring = que->rx_ring;
17749b8d05b8SZbigniew Bodek 	qid = que->id;
17759b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(qid);
17769b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
17779b8d05b8SZbigniew Bodek 
1778*d12f7bfcSMarcin Wojtas 	tx_ring->first_interrupt = true;
1779*d12f7bfcSMarcin Wojtas 	rx_ring->first_interrupt = true;
1780*d12f7bfcSMarcin Wojtas 
17819b8d05b8SZbigniew Bodek 	for (i = 0; i < CLEAN_BUDGET; ++i) {
1782efe6ab18SMarcin Wojtas 		/*
1783efe6ab18SMarcin Wojtas 		 * If lock cannot be acquired, then deferred cleanup task was
1784efe6ab18SMarcin Wojtas 		 * being executed and rx ring is being cleaned up in
1785efe6ab18SMarcin Wojtas 		 * another thread.
1786efe6ab18SMarcin Wojtas 		 */
17873f9ed7abSMarcin Wojtas 		if (likely(ENA_RING_MTX_TRYLOCK(rx_ring) != 0)) {
17889b8d05b8SZbigniew Bodek 			rxc = ena_rx_cleanup(rx_ring);
1789efe6ab18SMarcin Wojtas 			ENA_RING_MTX_UNLOCK(rx_ring);
1790efe6ab18SMarcin Wojtas 		} else {
1791efe6ab18SMarcin Wojtas 			rxc = 0;
1792efe6ab18SMarcin Wojtas 		}
17939b8d05b8SZbigniew Bodek 
17949b8d05b8SZbigniew Bodek 		/* Protection from calling ena_tx_cleanup from ena_start_xmit */
17959b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
17969b8d05b8SZbigniew Bodek 		txc = ena_tx_cleanup(tx_ring);
17979b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
17989b8d05b8SZbigniew Bodek 
17993f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
18009b8d05b8SZbigniew Bodek 			return;
18019b8d05b8SZbigniew Bodek 
18020bdffe59SMarcin Wojtas 		if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
18039b8d05b8SZbigniew Bodek 		       break;
18049b8d05b8SZbigniew Bodek 	}
18059b8d05b8SZbigniew Bodek 
18069b8d05b8SZbigniew Bodek 	/* Signal that work is done and unmask interrupt */
18079b8d05b8SZbigniew Bodek 	ena_com_update_intr_reg(&intr_reg,
18089b8d05b8SZbigniew Bodek 	    RX_IRQ_INTERVAL,
18099b8d05b8SZbigniew Bodek 	    TX_IRQ_INTERVAL,
18109b8d05b8SZbigniew Bodek 	    true);
18119b8d05b8SZbigniew Bodek 	ena_com_unmask_intr(io_cq, &intr_reg);
18129b8d05b8SZbigniew Bodek }
18139b8d05b8SZbigniew Bodek 
18149b8d05b8SZbigniew Bodek static int
18159b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
18169b8d05b8SZbigniew Bodek {
18179b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
18188805021aSMarcin Wojtas 	int msix_vecs, msix_req;
18198805021aSMarcin Wojtas 	int i, rc = 0;
18209b8d05b8SZbigniew Bodek 
18219b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
18229b8d05b8SZbigniew Bodek 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues);
18239b8d05b8SZbigniew Bodek 
1824cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1825cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1826cd5d5804SMarcin Wojtas 
18274e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d", msix_vecs);
18289b8d05b8SZbigniew Bodek 
18299b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
18309b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
18319b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
18329b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
18339b8d05b8SZbigniew Bodek 	}
18349b8d05b8SZbigniew Bodek 
18358805021aSMarcin Wojtas 	msix_req = msix_vecs;
18369b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
18373f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
18389b8d05b8SZbigniew Bodek 		device_printf(dev,
18399b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
18407d2544e6SMarcin Wojtas 
18419b8d05b8SZbigniew Bodek 		rc = ENOSPC;
18427d2544e6SMarcin Wojtas 		goto err_msix_free;
18439b8d05b8SZbigniew Bodek 	}
18449b8d05b8SZbigniew Bodek 
18458805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
18468805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
18478805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
18488805021aSMarcin Wojtas 		adapter->num_queues = msix_vecs - ENA_ADMIN_MSIX_VEC;
18498805021aSMarcin Wojtas 	}
18508805021aSMarcin Wojtas 
18519b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
18529b8d05b8SZbigniew Bodek 	adapter->msix_enabled = true;
18539b8d05b8SZbigniew Bodek 
18547d2544e6SMarcin Wojtas 	return (0);
18557d2544e6SMarcin Wojtas 
18567d2544e6SMarcin Wojtas err_msix_free:
18577d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
18587d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
18597d2544e6SMarcin Wojtas 
18609b8d05b8SZbigniew Bodek 	return (rc);
18619b8d05b8SZbigniew Bodek }
18629b8d05b8SZbigniew Bodek 
18639b8d05b8SZbigniew Bodek static void
18649b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
18659b8d05b8SZbigniew Bodek {
18669b8d05b8SZbigniew Bodek 
18679b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
18689b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
18699b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
18709b8d05b8SZbigniew Bodek 	/*
18719b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
18729b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
18739b8d05b8SZbigniew Bodek 	 */
18749b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
18759b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
18769b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
18779b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
18789b8d05b8SZbigniew Bodek }
18799b8d05b8SZbigniew Bodek 
18809b8d05b8SZbigniew Bodek static void
18819b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
18829b8d05b8SZbigniew Bodek {
18839b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
18849b8d05b8SZbigniew Bodek 	int irq_idx;
18859b8d05b8SZbigniew Bodek 
18869b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++) {
18879b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
18889b8d05b8SZbigniew Bodek 
18899b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
18909b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
18919b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
18929b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
18939b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
18949b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
18959b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
18969b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
1897277f11c4SMarcin Wojtas 
18989b8d05b8SZbigniew Bodek 		/*
1899277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
19009b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
19019b8d05b8SZbigniew Bodek 		 */
19023f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
19039b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
19049b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
19059b8d05b8SZbigniew Bodek 		    last_bind_cpu;
19069b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
19079b8d05b8SZbigniew Bodek 	}
19089b8d05b8SZbigniew Bodek }
19099b8d05b8SZbigniew Bodek 
19109b8d05b8SZbigniew Bodek static int
19119b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
19129b8d05b8SZbigniew Bodek {
19139b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19149b8d05b8SZbigniew Bodek 	unsigned long flags;
19159b8d05b8SZbigniew Bodek 	int rc, rcc;
19169b8d05b8SZbigniew Bodek 
19179b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
19189b8d05b8SZbigniew Bodek 
19199b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
19209b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
19219b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
19229b8d05b8SZbigniew Bodek 
19233f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
19249b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
19259b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
19267d2544e6SMarcin Wojtas 		return (ENXIO);
19279b8d05b8SZbigniew Bodek 	}
19289b8d05b8SZbigniew Bodek 
19290bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
19300bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
19310bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
19323f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
19339b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
19349b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
19359b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
19367d2544e6SMarcin Wojtas 		goto err_res_free;
19379b8d05b8SZbigniew Bodek 	}
19389b8d05b8SZbigniew Bodek 	irq->requested = true;
19399b8d05b8SZbigniew Bodek 
19409b8d05b8SZbigniew Bodek 	return (rc);
19419b8d05b8SZbigniew Bodek 
19427d2544e6SMarcin Wojtas err_res_free:
19434e8acd84SMarcin Wojtas 	ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
19447d2544e6SMarcin Wojtas 	    irq->vector);
19459b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
19469b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
19473f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
19489b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
19499b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
19509b8d05b8SZbigniew Bodek 	irq->res = NULL;
19519b8d05b8SZbigniew Bodek 
19529b8d05b8SZbigniew Bodek 	return (rc);
19539b8d05b8SZbigniew Bodek }
19549b8d05b8SZbigniew Bodek 
19559b8d05b8SZbigniew Bodek static int
19569b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
19579b8d05b8SZbigniew Bodek {
19589b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19599b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
19609b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
19619b8d05b8SZbigniew Bodek 
19623f9ed7abSMarcin Wojtas 	if (unlikely(adapter->msix_enabled == 0)) {
19634e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
19644e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
19659b8d05b8SZbigniew Bodek 		return (EINVAL);
19669b8d05b8SZbigniew Bodek 	} else {
19679b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
19689b8d05b8SZbigniew Bodek 	}
19699b8d05b8SZbigniew Bodek 
19709b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
19719b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
19729b8d05b8SZbigniew Bodek 
19733f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
19749b8d05b8SZbigniew Bodek 			continue;
19759b8d05b8SZbigniew Bodek 
19769b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
19779b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
19783f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
19799b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
19809b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
19819b8d05b8SZbigniew Bodek 			goto err;
19829b8d05b8SZbigniew Bodek 		}
19839b8d05b8SZbigniew Bodek 
19840bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
19850bdffe59SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, NULL,
19860bdffe59SMarcin Wojtas 		    irq->handler, irq->data, &irq->cookie);
19873f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
19889b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
19899b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
19909b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
19919b8d05b8SZbigniew Bodek 			goto err;
19929b8d05b8SZbigniew Bodek 		}
19939b8d05b8SZbigniew Bodek 		irq->requested = true;
19949b8d05b8SZbigniew Bodek 
19954e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
19969b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
19979b8d05b8SZbigniew Bodek 	}
19989b8d05b8SZbigniew Bodek 
19999b8d05b8SZbigniew Bodek 	return (rc);
20009b8d05b8SZbigniew Bodek 
20019b8d05b8SZbigniew Bodek err:
20029b8d05b8SZbigniew Bodek 
20039b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
20049b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20059b8d05b8SZbigniew Bodek 		rcc = 0;
20069b8d05b8SZbigniew Bodek 
20079b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
20089b8d05b8SZbigniew Bodek 		   free both intr and resources */
20090bdffe59SMarcin Wojtas 		if (irq->requested)
20109b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
20113f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
20129b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
20139b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
20149b8d05b8SZbigniew Bodek 
20159b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
20169b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
20179b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
20189b8d05b8SZbigniew Bodek 		   this iteration */
20199b8d05b8SZbigniew Bodek 		rcc = 0;
20209b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
20219b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20229b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
20239b8d05b8SZbigniew Bodek 		}
20243f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
20259b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
20269b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
20279b8d05b8SZbigniew Bodek 		irq->requested = false;
20289b8d05b8SZbigniew Bodek 		irq->res = NULL;
20299b8d05b8SZbigniew Bodek 	}
20309b8d05b8SZbigniew Bodek 
20319b8d05b8SZbigniew Bodek 	return (rc);
20329b8d05b8SZbigniew Bodek }
20339b8d05b8SZbigniew Bodek 
20349b8d05b8SZbigniew Bodek static void
20359b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
20369b8d05b8SZbigniew Bodek {
20379b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20389b8d05b8SZbigniew Bodek 	int rc;
20399b8d05b8SZbigniew Bodek 
20409b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
20419b8d05b8SZbigniew Bodek 	if (irq->requested) {
20429b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
20439b8d05b8SZbigniew Bodek 		    irq->vector);
20449b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
20453f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
20469b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
20479b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
20489b8d05b8SZbigniew Bodek 		irq->requested = 0;
20499b8d05b8SZbigniew Bodek 	}
20509b8d05b8SZbigniew Bodek 
20519b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
20529b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
20539b8d05b8SZbigniew Bodek 		    irq->vector);
20549b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20559b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
20569b8d05b8SZbigniew Bodek 		irq->res = NULL;
20573f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
20589b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
20599b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
20609b8d05b8SZbigniew Bodek 	}
20619b8d05b8SZbigniew Bodek }
20629b8d05b8SZbigniew Bodek 
20639b8d05b8SZbigniew Bodek static void
20649b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
20659b8d05b8SZbigniew Bodek {
20669b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20679b8d05b8SZbigniew Bodek 	int rc;
20689b8d05b8SZbigniew Bodek 
20699b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
20709b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20719b8d05b8SZbigniew Bodek 		if (irq->requested) {
20729b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
20739b8d05b8SZbigniew Bodek 			    irq->vector);
20749b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
20759b8d05b8SZbigniew Bodek 			    irq->cookie);
20763f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
20779b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
20789b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
20799b8d05b8SZbigniew Bodek 			}
20809b8d05b8SZbigniew Bodek 			irq->requested = 0;
20819b8d05b8SZbigniew Bodek 		}
20829b8d05b8SZbigniew Bodek 
20839b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
20849b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
20859b8d05b8SZbigniew Bodek 			    irq->vector);
20869b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20879b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
20889b8d05b8SZbigniew Bodek 			irq->res = NULL;
20893f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
20909b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
20919b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
20929b8d05b8SZbigniew Bodek 				    irq->vector);
20939b8d05b8SZbigniew Bodek 			}
20949b8d05b8SZbigniew Bodek 		}
20959b8d05b8SZbigniew Bodek 	}
20969b8d05b8SZbigniew Bodek }
20979b8d05b8SZbigniew Bodek 
20989b8d05b8SZbigniew Bodek static void
20999b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
21009b8d05b8SZbigniew Bodek {
21019b8d05b8SZbigniew Bodek 
21029b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
21039b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
21049b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
21059b8d05b8SZbigniew Bodek }
21069b8d05b8SZbigniew Bodek 
21079b8d05b8SZbigniew Bodek static void
21089b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
21099b8d05b8SZbigniew Bodek {
21109b8d05b8SZbigniew Bodek 
21119b8d05b8SZbigniew Bodek 	pci_release_msi(adapter->pdev);
21129b8d05b8SZbigniew Bodek 
21139b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
2114cd5d5804SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
21159b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
21169b8d05b8SZbigniew Bodek }
21179b8d05b8SZbigniew Bodek 
21189b8d05b8SZbigniew Bodek static void
21199b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
21209b8d05b8SZbigniew Bodek {
21219b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
21229b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
21239b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
21249b8d05b8SZbigniew Bodek 	int i;
21259b8d05b8SZbigniew Bodek 
21269b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
21279b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
21289b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
21299b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
21309b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
21319b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
21329b8d05b8SZbigniew Bodek 	}
21339b8d05b8SZbigniew Bodek }
21349b8d05b8SZbigniew Bodek 
21359b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
21360bdffe59SMarcin Wojtas static int
21370bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
21389b8d05b8SZbigniew Bodek {
21399b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
21409b8d05b8SZbigniew Bodek 	int rc;
21419b8d05b8SZbigniew Bodek 
21429b8d05b8SZbigniew Bodek 	/* Set indirect table */
21439b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
21440bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21450bdffe59SMarcin Wojtas 		return (rc);
21469b8d05b8SZbigniew Bodek 
21479b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
21489b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
21490bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21500bdffe59SMarcin Wojtas 		return (rc);
21519b8d05b8SZbigniew Bodek 
21529b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
21539b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
21540bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21550bdffe59SMarcin Wojtas 		return (rc);
21569b8d05b8SZbigniew Bodek 
21570bdffe59SMarcin Wojtas 	return (0);
21589b8d05b8SZbigniew Bodek }
21599b8d05b8SZbigniew Bodek 
21609b8d05b8SZbigniew Bodek static int
21619b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
21629b8d05b8SZbigniew Bodek {
21639b8d05b8SZbigniew Bodek 	int rc;
21649b8d05b8SZbigniew Bodek 
21653f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support)) {
21669b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
21670bdffe59SMarcin Wojtas 		if (rc != 0)
21689b8d05b8SZbigniew Bodek 			return (rc);
21699b8d05b8SZbigniew Bodek 	}
21709b8d05b8SZbigniew Bodek 
21717d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
21723f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
21737d2544e6SMarcin Wojtas 		return (rc);
21747d2544e6SMarcin Wojtas 
21759b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
217630217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
217730217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
21789b8d05b8SZbigniew Bodek 
21799b8d05b8SZbigniew Bodek 	return (0);
21809b8d05b8SZbigniew Bodek }
21819b8d05b8SZbigniew Bodek 
21829b8d05b8SZbigniew Bodek static int
21839b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
21849b8d05b8SZbigniew Bodek {
21859b8d05b8SZbigniew Bodek 	int rc = 0;
21869b8d05b8SZbigniew Bodek 
21873f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
21889b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
21899b8d05b8SZbigniew Bodek 		return (ENXIO);
21909b8d05b8SZbigniew Bodek 	}
21919b8d05b8SZbigniew Bodek 
21923f9ed7abSMarcin Wojtas 	if (unlikely(!adapter->running)) {
21939b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not running!\n");
21949b8d05b8SZbigniew Bodek 		return (ENXIO);
21959b8d05b8SZbigniew Bodek 	}
21969b8d05b8SZbigniew Bodek 
21979b8d05b8SZbigniew Bodek 	if (!adapter->up) {
21989b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going UP\n");
21999b8d05b8SZbigniew Bodek 
22009b8d05b8SZbigniew Bodek 		/* setup interrupts for IO queues */
22019b8d05b8SZbigniew Bodek 		ena_setup_io_intr(adapter);
22029b8d05b8SZbigniew Bodek 		rc = ena_request_io_irq(adapter);
22033f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22049b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_req_irq");
22059b8d05b8SZbigniew Bodek 			goto err_req_irq;
22069b8d05b8SZbigniew Bodek 		}
22079b8d05b8SZbigniew Bodek 
22089b8d05b8SZbigniew Bodek 		/* allocate transmit descriptors */
22099b8d05b8SZbigniew Bodek 		rc = ena_setup_all_tx_resources(adapter);
22103f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22119b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_tx");
22129b8d05b8SZbigniew Bodek 			goto err_setup_tx;
22139b8d05b8SZbigniew Bodek 		}
22149b8d05b8SZbigniew Bodek 
22159b8d05b8SZbigniew Bodek 		/* allocate receive descriptors */
22169b8d05b8SZbigniew Bodek 		rc = ena_setup_all_rx_resources(adapter);
22173f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22189b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_rx");
22199b8d05b8SZbigniew Bodek 			goto err_setup_rx;
22209b8d05b8SZbigniew Bodek 		}
22219b8d05b8SZbigniew Bodek 
22229b8d05b8SZbigniew Bodek 		/* create IO queues for Rx & Tx */
22239b8d05b8SZbigniew Bodek 		rc = ena_create_io_queues(adapter);
22243f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22259b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT,
22269b8d05b8SZbigniew Bodek 			    "create IO queues failed");
22279b8d05b8SZbigniew Bodek 			goto err_io_que;
22289b8d05b8SZbigniew Bodek 		}
22299b8d05b8SZbigniew Bodek 
22303f9ed7abSMarcin Wojtas 		if (unlikely(adapter->link_status))
22319b8d05b8SZbigniew Bodek 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
22329b8d05b8SZbigniew Bodek 
22339b8d05b8SZbigniew Bodek 		rc = ena_up_complete(adapter);
22343f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
22359b8d05b8SZbigniew Bodek 			goto err_up_complete;
22369b8d05b8SZbigniew Bodek 
22379b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_up, 1);
22389b8d05b8SZbigniew Bodek 
22399b8d05b8SZbigniew Bodek 		ena_update_hwassist(adapter);
22409b8d05b8SZbigniew Bodek 
22419b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
22429b8d05b8SZbigniew Bodek 		    IFF_DRV_OACTIVE);
22439b8d05b8SZbigniew Bodek 
22449b8d05b8SZbigniew Bodek 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
22459b8d05b8SZbigniew Bodek 		    ena_timer_service, (void *)adapter, 0);
22469b8d05b8SZbigniew Bodek 
22479b8d05b8SZbigniew Bodek 		adapter->up = true;
224893471047SZbigniew Bodek 
224993471047SZbigniew Bodek 		ena_unmask_all_io_irqs(adapter);
22509b8d05b8SZbigniew Bodek 	}
22519b8d05b8SZbigniew Bodek 
22529b8d05b8SZbigniew Bodek 	return (0);
22539b8d05b8SZbigniew Bodek 
22549b8d05b8SZbigniew Bodek err_up_complete:
22559b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
22569b8d05b8SZbigniew Bodek err_io_que:
22579b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
22589b8d05b8SZbigniew Bodek err_setup_rx:
22599b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
22609b8d05b8SZbigniew Bodek err_setup_tx:
22619b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
22629b8d05b8SZbigniew Bodek err_req_irq:
22639b8d05b8SZbigniew Bodek 	return (rc);
22649b8d05b8SZbigniew Bodek }
22659b8d05b8SZbigniew Bodek 
22669b8d05b8SZbigniew Bodek static uint64_t
22679b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
22689b8d05b8SZbigniew Bodek {
22699b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
22709b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
22719b8d05b8SZbigniew Bodek 
22729b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
22739b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
22749b8d05b8SZbigniew Bodek 
22759b8d05b8SZbigniew Bodek 	switch (cnt) {
22769b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
227730217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
22789b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
227930217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
22809b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
228130217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
22829b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
228330217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
22849b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
228530217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
22869b8d05b8SZbigniew Bodek 	default:
22879b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
22889b8d05b8SZbigniew Bodek 	}
22899b8d05b8SZbigniew Bodek }
22909b8d05b8SZbigniew Bodek 
22919b8d05b8SZbigniew Bodek static int
22929b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
22939b8d05b8SZbigniew Bodek {
22949b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
22959b8d05b8SZbigniew Bodek 	return (0);
22969b8d05b8SZbigniew Bodek }
22979b8d05b8SZbigniew Bodek 
22989b8d05b8SZbigniew Bodek static void
22999b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
23009b8d05b8SZbigniew Bodek {
23019b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
23029b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "enter");
23039b8d05b8SZbigniew Bodek 
23045a990212SMarcin Wojtas 	mtx_lock(&adapter->global_mtx);
23059b8d05b8SZbigniew Bodek 
23069b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
23079b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
23089b8d05b8SZbigniew Bodek 
23099b8d05b8SZbigniew Bodek 	if (!adapter->link_status) {
23105a990212SMarcin Wojtas 		mtx_unlock(&adapter->global_mtx);
23114e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "link_status = false");
23129b8d05b8SZbigniew Bodek 		return;
23139b8d05b8SZbigniew Bodek 	}
23149b8d05b8SZbigniew Bodek 
23159b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
2316b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
23179b8d05b8SZbigniew Bodek 
23185a990212SMarcin Wojtas 	mtx_unlock(&adapter->global_mtx);
23199b8d05b8SZbigniew Bodek }
23209b8d05b8SZbigniew Bodek 
23219b8d05b8SZbigniew Bodek static void
23229b8d05b8SZbigniew Bodek ena_init(void *arg)
23239b8d05b8SZbigniew Bodek {
23249b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
23259b8d05b8SZbigniew Bodek 
23260bdffe59SMarcin Wojtas 	if (!adapter->up) {
23273d3a90f9SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
23289b8d05b8SZbigniew Bodek 		ena_up(adapter);
23293d3a90f9SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
23303d3a90f9SZbigniew Bodek 	}
23319b8d05b8SZbigniew Bodek }
23329b8d05b8SZbigniew Bodek 
23339b8d05b8SZbigniew Bodek static int
23349b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
23359b8d05b8SZbigniew Bodek {
23369b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
23379b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
23389b8d05b8SZbigniew Bodek 	int rc;
23399b8d05b8SZbigniew Bodek 
23409b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
23419b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
23429b8d05b8SZbigniew Bodek 
23439b8d05b8SZbigniew Bodek 	/*
23449b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
23459b8d05b8SZbigniew Bodek 	 */
23469b8d05b8SZbigniew Bodek 	rc = 0;
23479b8d05b8SZbigniew Bodek 	switch (command) {
23489b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2349dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
2350dbf2eb54SMarcin Wojtas 			break;
2351e67c6554SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
23529b8d05b8SZbigniew Bodek 		ena_down(adapter);
23539b8d05b8SZbigniew Bodek 
23549b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
23559b8d05b8SZbigniew Bodek 
23569b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
2357e67c6554SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
23589b8d05b8SZbigniew Bodek 		break;
23599b8d05b8SZbigniew Bodek 
23609b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
23610bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
23620bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
23630bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
23640bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
23659b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
23669b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
23679b8d05b8SZbigniew Bodek 				}
23689b8d05b8SZbigniew Bodek 			} else {
2369e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
23709b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2371e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
23729b8d05b8SZbigniew Bodek 			}
23739b8d05b8SZbigniew Bodek 		} else {
23740bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2375e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
23769b8d05b8SZbigniew Bodek 				ena_down(adapter);
2377e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
2378e67c6554SZbigniew Bodek 			}
23799b8d05b8SZbigniew Bodek 		}
23809b8d05b8SZbigniew Bodek 		break;
23819b8d05b8SZbigniew Bodek 
23829b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
23839b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
23849b8d05b8SZbigniew Bodek 		break;
23859b8d05b8SZbigniew Bodek 
23869b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
23879b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
23889b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
23899b8d05b8SZbigniew Bodek 		break;
23909b8d05b8SZbigniew Bodek 
23919b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
23929b8d05b8SZbigniew Bodek 		{
23939b8d05b8SZbigniew Bodek 			int reinit = 0;
23949b8d05b8SZbigniew Bodek 
23959b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
23969b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
23979b8d05b8SZbigniew Bodek 				reinit = 1;
23989b8d05b8SZbigniew Bodek 			}
23999b8d05b8SZbigniew Bodek 
24000bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
24010bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
2402e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24039b8d05b8SZbigniew Bodek 				ena_down(adapter);
24049b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2405e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
24069b8d05b8SZbigniew Bodek 			}
24079b8d05b8SZbigniew Bodek 		}
24089b8d05b8SZbigniew Bodek 
24099b8d05b8SZbigniew Bodek 		break;
24109b8d05b8SZbigniew Bodek 	default:
24119b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
24129b8d05b8SZbigniew Bodek 		break;
24139b8d05b8SZbigniew Bodek 	}
24149b8d05b8SZbigniew Bodek 
24159b8d05b8SZbigniew Bodek 	return (rc);
24169b8d05b8SZbigniew Bodek }
24179b8d05b8SZbigniew Bodek 
24189b8d05b8SZbigniew Bodek static int
24199b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
24209b8d05b8SZbigniew Bodek {
24219b8d05b8SZbigniew Bodek 	int caps = 0;
24229b8d05b8SZbigniew Bodek 
24230bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24249b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
24259b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
24260bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
24279b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
24289b8d05b8SZbigniew Bodek 
24290bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24309b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
24310bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
24329b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
24339b8d05b8SZbigniew Bodek 
24340bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24350bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
24369b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
24379b8d05b8SZbigniew Bodek 
24380bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24390bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
24409b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
24419b8d05b8SZbigniew Bodek 
24420bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
24439b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
24440bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
24459b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
24469b8d05b8SZbigniew Bodek 
24470bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
24480bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
24499b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
24509b8d05b8SZbigniew Bodek 
24519b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
24529b8d05b8SZbigniew Bodek 
24539b8d05b8SZbigniew Bodek 	return (caps);
24549b8d05b8SZbigniew Bodek }
24559b8d05b8SZbigniew Bodek 
24569b8d05b8SZbigniew Bodek static void
24579b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
24589b8d05b8SZbigniew Bodek {
24599b8d05b8SZbigniew Bodek 
24609b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
24619b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
24629b8d05b8SZbigniew Bodek }
24639b8d05b8SZbigniew Bodek 
24649b8d05b8SZbigniew Bodek static void
24659b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
24669b8d05b8SZbigniew Bodek {
24679b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
24689b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
24699b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
24709b8d05b8SZbigniew Bodek 	int flags = 0;
24719b8d05b8SZbigniew Bodek 
24729b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
24739b8d05b8SZbigniew Bodek 
24740bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
24750bdffe59SMarcin Wojtas 		if ((feat &
24760bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
24779b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
24780bdffe59SMarcin Wojtas 		if ((feat &
24799b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
24800bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
24819b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
24829b8d05b8SZbigniew Bodek 	}
24839b8d05b8SZbigniew Bodek 
24840bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
24859b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
24869b8d05b8SZbigniew Bodek 
24870bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
24889b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
24899b8d05b8SZbigniew Bodek 
24900bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
24919b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
24929b8d05b8SZbigniew Bodek 
24939b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
24949b8d05b8SZbigniew Bodek }
24959b8d05b8SZbigniew Bodek 
24969b8d05b8SZbigniew Bodek static int
24979b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
24989b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
24999b8d05b8SZbigniew Bodek {
25009b8d05b8SZbigniew Bodek 	if_t ifp;
25019b8d05b8SZbigniew Bodek 	int caps = 0;
25029b8d05b8SZbigniew Bodek 
25039b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
25043f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
25054e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
25069b8d05b8SZbigniew Bodek 		return (ENXIO);
25079b8d05b8SZbigniew Bodek 	}
25089b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
25099b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
25109b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
25119b8d05b8SZbigniew Bodek 
25129b8d05b8SZbigniew Bodek 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
25139b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
25149b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
25159b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
25169b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
25179b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
25189b8d05b8SZbigniew Bodek 
25199b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
25209b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
25219b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
25229b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
25239b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
25249b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
25259b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
25269b8d05b8SZbigniew Bodek 	/* check hardware support */
25279b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
25289b8d05b8SZbigniew Bodek 	/* ... and set them */
25299b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
25309b8d05b8SZbigniew Bodek 
25319b8d05b8SZbigniew Bodek 	/* TSO parameters */
25328a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
25338a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
25348a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
25358a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
25369b8d05b8SZbigniew Bodek 
25379b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
25389b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
25399b8d05b8SZbigniew Bodek 
25409b8d05b8SZbigniew Bodek 	/*
25419b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
25429b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
25439b8d05b8SZbigniew Bodek 	 */
25449b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
25459b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
25469b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
25479b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
25489b8d05b8SZbigniew Bodek 
25499b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
25509b8d05b8SZbigniew Bodek 
25519b8d05b8SZbigniew Bodek 	return (0);
25529b8d05b8SZbigniew Bodek }
25539b8d05b8SZbigniew Bodek 
25549b8d05b8SZbigniew Bodek static void
25559b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
25569b8d05b8SZbigniew Bodek {
2557a195fab0SMarcin Wojtas 	int rc;
25589b8d05b8SZbigniew Bodek 
25599b8d05b8SZbigniew Bodek 	if (adapter->up) {
25609b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going DOWN\n");
25619b8d05b8SZbigniew Bodek 
25629b8d05b8SZbigniew Bodek 		callout_drain(&adapter->timer_service);
25639b8d05b8SZbigniew Bodek 
25649b8d05b8SZbigniew Bodek 		adapter->up = false;
25659b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
25669b8d05b8SZbigniew Bodek 		    IFF_DRV_RUNNING);
25679b8d05b8SZbigniew Bodek 
25689b8d05b8SZbigniew Bodek 		ena_free_io_irq(adapter);
25699b8d05b8SZbigniew Bodek 
2570a195fab0SMarcin Wojtas 		if (adapter->trigger_reset) {
2571a195fab0SMarcin Wojtas 			rc = ena_com_dev_reset(adapter->ena_dev,
2572a195fab0SMarcin Wojtas 			    adapter->reset_reason);
25733f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0))
2574a195fab0SMarcin Wojtas 				device_printf(adapter->pdev,
2575a195fab0SMarcin Wojtas 				    "Device reset failed\n");
2576a195fab0SMarcin Wojtas 		}
2577a195fab0SMarcin Wojtas 
25789b8d05b8SZbigniew Bodek 		ena_destroy_all_io_queues(adapter);
25799b8d05b8SZbigniew Bodek 
25809b8d05b8SZbigniew Bodek 		ena_free_all_tx_bufs(adapter);
25819b8d05b8SZbigniew Bodek 		ena_free_all_rx_bufs(adapter);
25829b8d05b8SZbigniew Bodek 		ena_free_all_tx_resources(adapter);
25839b8d05b8SZbigniew Bodek 		ena_free_all_rx_resources(adapter);
25849b8d05b8SZbigniew Bodek 
25859b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_down, 1);
25869b8d05b8SZbigniew Bodek 	}
25879b8d05b8SZbigniew Bodek }
25889b8d05b8SZbigniew Bodek 
25899b8d05b8SZbigniew Bodek static void
25909b8d05b8SZbigniew Bodek ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
25919b8d05b8SZbigniew Bodek {
25929b8d05b8SZbigniew Bodek 	struct ena_com_tx_meta *ena_meta;
25939b8d05b8SZbigniew Bodek 	struct ether_vlan_header *eh;
25949b8d05b8SZbigniew Bodek 	u32 mss;
25959b8d05b8SZbigniew Bodek 	bool offload;
25969b8d05b8SZbigniew Bodek 	uint16_t etype;
25979b8d05b8SZbigniew Bodek 	int ehdrlen;
25989b8d05b8SZbigniew Bodek 	struct ip *ip;
25999b8d05b8SZbigniew Bodek 	int iphlen;
26009b8d05b8SZbigniew Bodek 	struct tcphdr *th;
26019b8d05b8SZbigniew Bodek 
26029b8d05b8SZbigniew Bodek 	offload = false;
26039b8d05b8SZbigniew Bodek 	ena_meta = &ena_tx_ctx->ena_meta;
26049b8d05b8SZbigniew Bodek 	mss = mbuf->m_pkthdr.tso_segsz;
26059b8d05b8SZbigniew Bodek 
26069b8d05b8SZbigniew Bodek 	if (mss != 0)
26079b8d05b8SZbigniew Bodek 		offload = true;
26089b8d05b8SZbigniew Bodek 
26099b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0)
26109b8d05b8SZbigniew Bodek 		offload = true;
26119b8d05b8SZbigniew Bodek 
26129b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
26139b8d05b8SZbigniew Bodek 		offload = true;
26149b8d05b8SZbigniew Bodek 
26150bdffe59SMarcin Wojtas 	if (!offload) {
26169b8d05b8SZbigniew Bodek 		ena_tx_ctx->meta_valid = 0;
26179b8d05b8SZbigniew Bodek 		return;
26189b8d05b8SZbigniew Bodek 	}
26199b8d05b8SZbigniew Bodek 
26209b8d05b8SZbigniew Bodek 	/* Determine where frame payload starts. */
26219b8d05b8SZbigniew Bodek 	eh = mtod(mbuf, struct ether_vlan_header *);
26229b8d05b8SZbigniew Bodek 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
26239b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_proto);
26249b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
26259b8d05b8SZbigniew Bodek 	} else {
26269b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_encap_proto);
26279b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN;
26289b8d05b8SZbigniew Bodek 	}
26299b8d05b8SZbigniew Bodek 
26309b8d05b8SZbigniew Bodek 	ip = (struct ip *)(mbuf->m_data + ehdrlen);
26319b8d05b8SZbigniew Bodek 	iphlen = ip->ip_hl << 2;
26329b8d05b8SZbigniew Bodek 	th = (struct tcphdr *)((caddr_t)ip + iphlen);
26339b8d05b8SZbigniew Bodek 
26349b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
26359b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_csum_enable = 1;
26369b8d05b8SZbigniew Bodek 	}
26379b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
26389b8d05b8SZbigniew Bodek 		ena_tx_ctx->tso_enable = 1;
26399b8d05b8SZbigniew Bodek 		ena_meta->l4_hdr_len = (th->th_off);
26409b8d05b8SZbigniew Bodek 	}
26419b8d05b8SZbigniew Bodek 
26429b8d05b8SZbigniew Bodek 	switch (etype) {
26439b8d05b8SZbigniew Bodek 	case ETHERTYPE_IP:
26449b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2645cd433385SMarcin Wojtas 		if ((ip->ip_off & htons(IP_DF)) != 0)
26469b8d05b8SZbigniew Bodek 			ena_tx_ctx->df = 1;
26479b8d05b8SZbigniew Bodek 		break;
26489b8d05b8SZbigniew Bodek 	case ETHERTYPE_IPV6:
26499b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
26509b8d05b8SZbigniew Bodek 
26519b8d05b8SZbigniew Bodek 	default:
26529b8d05b8SZbigniew Bodek 		break;
26539b8d05b8SZbigniew Bodek 	}
26549b8d05b8SZbigniew Bodek 
26559b8d05b8SZbigniew Bodek 	if (ip->ip_p == IPPROTO_TCP) {
26569b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
26570bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
26580bdffe59SMarcin Wojtas 		    (CSUM_IP_TCP | CSUM_IP6_TCP)) != 0)
26599b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
26609b8d05b8SZbigniew Bodek 		else
26619b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
26629b8d05b8SZbigniew Bodek 	} else if (ip->ip_p == IPPROTO_UDP) {
26639b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
26640bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
26650bdffe59SMarcin Wojtas 		    (CSUM_IP_UDP | CSUM_IP6_UDP)) != 0)
26669b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
26679b8d05b8SZbigniew Bodek 		else
26689b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
26699b8d05b8SZbigniew Bodek 	} else {
26709b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
26719b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_csum_enable = 0;
26729b8d05b8SZbigniew Bodek 	}
26739b8d05b8SZbigniew Bodek 
26749b8d05b8SZbigniew Bodek 	ena_meta->mss = mss;
26759b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_len = iphlen;
26769b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_offset = ehdrlen;
26779b8d05b8SZbigniew Bodek 	ena_tx_ctx->meta_valid = 1;
26789b8d05b8SZbigniew Bodek }
26799b8d05b8SZbigniew Bodek 
26809b8d05b8SZbigniew Bodek static int
26811b069f1cSZbigniew Bodek ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
26821e9fb899SZbigniew Bodek {
26831e9fb899SZbigniew Bodek 	struct ena_adapter *adapter;
26841b069f1cSZbigniew Bodek 	struct mbuf *collapsed_mbuf;
26851e9fb899SZbigniew Bodek 	int num_frags;
26861e9fb899SZbigniew Bodek 
26871e9fb899SZbigniew Bodek 	adapter = tx_ring->adapter;
26881e9fb899SZbigniew Bodek 	num_frags = ena_mbuf_count(*mbuf);
26891e9fb899SZbigniew Bodek 
26901e9fb899SZbigniew Bodek 	/* One segment must be reserved for configuration descriptor. */
26911e9fb899SZbigniew Bodek 	if (num_frags < adapter->max_tx_sgl_size)
26921e9fb899SZbigniew Bodek 		return (0);
26931b069f1cSZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
26941e9fb899SZbigniew Bodek 
26951b069f1cSZbigniew Bodek 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
26961b069f1cSZbigniew Bodek 	    adapter->max_tx_sgl_size - 1);
26973f9ed7abSMarcin Wojtas 	if (unlikely(collapsed_mbuf == NULL)) {
26981b069f1cSZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
26991e9fb899SZbigniew Bodek 		return (ENOMEM);
27001e9fb899SZbigniew Bodek 	}
27011e9fb899SZbigniew Bodek 
27021b069f1cSZbigniew Bodek 	/* If mbuf was collapsed succesfully, original mbuf is released. */
27031b069f1cSZbigniew Bodek 	*mbuf = collapsed_mbuf;
27041e9fb899SZbigniew Bodek 
27051e9fb899SZbigniew Bodek 	return (0);
27061e9fb899SZbigniew Bodek }
27071e9fb899SZbigniew Bodek 
27081e9fb899SZbigniew Bodek static int
27091e9fb899SZbigniew Bodek ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
27109b8d05b8SZbigniew Bodek {
27119b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
27129b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info;
27139b8d05b8SZbigniew Bodek 	struct ena_com_tx_ctx ena_tx_ctx;
27149b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
27159b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
27169b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
27179b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[ENA_BUS_DMA_SEGS];
27189b8d05b8SZbigniew Bodek 	void *push_hdr;
27199b8d05b8SZbigniew Bodek 	uint16_t next_to_use;
27209b8d05b8SZbigniew Bodek 	uint16_t req_id;
27219b8d05b8SZbigniew Bodek 	uint16_t push_len;
27229b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
27232339f28cSMarcin Wojtas 	uint32_t nsegs, header_len;
27249b8d05b8SZbigniew Bodek 	int i, rc;
27251e9fb899SZbigniew Bodek 	int nb_hw_desc;
27269b8d05b8SZbigniew Bodek 
27279b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
27289b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
27299b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
273090f4da8bSMarcin Wojtas 	io_sq = &ena_dev->io_sq_queues[ena_qid];
27319b8d05b8SZbigniew Bodek 
27321b069f1cSZbigniew Bodek 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
27333f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27341e9fb899SZbigniew Bodek 		ena_trace(ENA_WARNING,
27351b069f1cSZbigniew Bodek 		    "Failed to collapse mbuf! err: %d", rc);
27361e9fb899SZbigniew Bodek 		return (rc);
27379b8d05b8SZbigniew Bodek 	}
27389b8d05b8SZbigniew Bodek 
27399b8d05b8SZbigniew Bodek 	next_to_use = tx_ring->next_to_use;
27409b8d05b8SZbigniew Bodek 	req_id = tx_ring->free_tx_ids[next_to_use];
27419b8d05b8SZbigniew Bodek 	tx_info = &tx_ring->tx_buffer_info[req_id];
27429b8d05b8SZbigniew Bodek 
27431e9fb899SZbigniew Bodek 	tx_info->mbuf = *mbuf;
27449b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = 0;
27459b8d05b8SZbigniew Bodek 
27469b8d05b8SZbigniew Bodek 	ena_buf = tx_info->bufs;
27479b8d05b8SZbigniew Bodek 
27481e9fb899SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
27499b8d05b8SZbigniew Bodek 
27509b8d05b8SZbigniew Bodek 	push_len = 0;
27512339f28cSMarcin Wojtas 	/*
27522339f28cSMarcin Wojtas 	 * header_len is just a hint for the device. Because FreeBSD is not
27532339f28cSMarcin Wojtas 	 * giving us information about packet header length and it is not
27542339f28cSMarcin Wojtas 	 * guaranteed that all packet headers will be in the 1st mbuf, setting
27552339f28cSMarcin Wojtas 	 * header_len to 0 is making the device ignore this value and resolve
27562339f28cSMarcin Wojtas 	 * header on it's own.
27572339f28cSMarcin Wojtas 	 */
27582339f28cSMarcin Wojtas 	header_len = 0;
27599b8d05b8SZbigniew Bodek 	push_hdr = NULL;
27609b8d05b8SZbigniew Bodek 
27619b8d05b8SZbigniew Bodek 	rc = bus_dmamap_load_mbuf_sg(adapter->tx_buf_tag, tx_info->map,
27621e9fb899SZbigniew Bodek 	    *mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
27639b8d05b8SZbigniew Bodek 
27643f9ed7abSMarcin Wojtas 	if (unlikely((rc != 0) || (nsegs == 0))) {
27659b8d05b8SZbigniew Bodek 		ena_trace(ENA_WARNING,
27669b8d05b8SZbigniew Bodek 		    "dmamap load failed! err: %d nsegs: %d", rc, nsegs);
27679b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
27689b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
27699b8d05b8SZbigniew Bodek 		if (rc == ENOMEM)
27709b8d05b8SZbigniew Bodek 			return (ENA_COM_NO_MEM);
27719b8d05b8SZbigniew Bodek 		else
27729b8d05b8SZbigniew Bodek 			return (ENA_COM_INVAL);
27739b8d05b8SZbigniew Bodek 	}
27749b8d05b8SZbigniew Bodek 
27759b8d05b8SZbigniew Bodek 	for (i = 0; i < nsegs; i++) {
27769b8d05b8SZbigniew Bodek 		ena_buf->len = segs[i].ds_len;
27779b8d05b8SZbigniew Bodek 		ena_buf->paddr = segs[i].ds_addr;
27789b8d05b8SZbigniew Bodek 		ena_buf++;
27799b8d05b8SZbigniew Bodek 	}
27809b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = nsegs;
27819b8d05b8SZbigniew Bodek 
27829b8d05b8SZbigniew Bodek 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
27839b8d05b8SZbigniew Bodek 	ena_tx_ctx.ena_bufs = tx_info->bufs;
27849b8d05b8SZbigniew Bodek 	ena_tx_ctx.push_header = push_hdr;
27859b8d05b8SZbigniew Bodek 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
27869b8d05b8SZbigniew Bodek 	ena_tx_ctx.req_id = req_id;
27879b8d05b8SZbigniew Bodek 	ena_tx_ctx.header_len = header_len;
27889b8d05b8SZbigniew Bodek 
27899b8d05b8SZbigniew Bodek 	/* Set flags and meta data */
27901e9fb899SZbigniew Bodek 	ena_tx_csum(&ena_tx_ctx, *mbuf);
27919b8d05b8SZbigniew Bodek 	/* Prepare the packet's descriptors and send them to device */
27929b8d05b8SZbigniew Bodek 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
27933f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2794e3cecf70SMarcin Wojtas 		if (likely(rc == ENA_COM_NO_MEM)) {
2795e3cecf70SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_TXPTH,
2796e3cecf70SMarcin Wojtas 			    "tx ring[%d] if out of space\n", tx_ring->que->id);
2797e3cecf70SMarcin Wojtas 		} else {
2798e3cecf70SMarcin Wojtas 			device_printf(adapter->pdev,
2799e3cecf70SMarcin Wojtas 			    "failed to prepare tx bufs\n");
2800e3cecf70SMarcin Wojtas 		}
28010052f3b5SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
28029b8d05b8SZbigniew Bodek 		goto dma_error;
28039b8d05b8SZbigniew Bodek 	}
28049b8d05b8SZbigniew Bodek 
28059b8d05b8SZbigniew Bodek 	counter_enter();
28069b8d05b8SZbigniew Bodek 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
28070bdffe59SMarcin Wojtas 	counter_u64_add_protected(tx_ring->tx_stats.bytes,
28080bdffe59SMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
280930217e2dSMarcin Wojtas 
281030217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
281130217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
281230217e2dSMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
28139b8d05b8SZbigniew Bodek 	counter_exit();
28149b8d05b8SZbigniew Bodek 
28159b8d05b8SZbigniew Bodek 	tx_info->tx_descs = nb_hw_desc;
28169b8d05b8SZbigniew Bodek 	getbinuptime(&tx_info->timestamp);
28179b8d05b8SZbigniew Bodek 	tx_info->print_once = true;
28189b8d05b8SZbigniew Bodek 
28199b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
28209b8d05b8SZbigniew Bodek 	    tx_ring->ring_size);
28219b8d05b8SZbigniew Bodek 
28220bdffe59SMarcin Wojtas 	bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map,
28230bdffe59SMarcin Wojtas 	    BUS_DMASYNC_PREWRITE);
28249b8d05b8SZbigniew Bodek 
28259b8d05b8SZbigniew Bodek 	return (0);
28269b8d05b8SZbigniew Bodek 
28279b8d05b8SZbigniew Bodek dma_error:
28289b8d05b8SZbigniew Bodek 	tx_info->mbuf = NULL;
28299b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
28309b8d05b8SZbigniew Bodek 
28319b8d05b8SZbigniew Bodek 	return (rc);
28329b8d05b8SZbigniew Bodek }
28339b8d05b8SZbigniew Bodek 
28349b8d05b8SZbigniew Bodek static void
28359b8d05b8SZbigniew Bodek ena_start_xmit(struct ena_ring *tx_ring)
28369b8d05b8SZbigniew Bodek {
28379b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
28389b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = tx_ring->adapter;
28399b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
28409b8d05b8SZbigniew Bodek 	int ena_qid;
28419b8d05b8SZbigniew Bodek 	int acum_pkts = 0;
28429b8d05b8SZbigniew Bodek 	int ret = 0;
28439b8d05b8SZbigniew Bodek 
28443f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
28459b8d05b8SZbigniew Bodek 		return;
28469b8d05b8SZbigniew Bodek 
28473f9ed7abSMarcin Wojtas 	if (unlikely(!adapter->link_status))
28489b8d05b8SZbigniew Bodek 		return;
28499b8d05b8SZbigniew Bodek 
28509b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
28519b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
28529b8d05b8SZbigniew Bodek 
28539b8d05b8SZbigniew Bodek 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
28549b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
28559b8d05b8SZbigniew Bodek 		    " header csum flags %#jx",
28564e8acd84SMarcin Wojtas 		    mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
28579b8d05b8SZbigniew Bodek 
285867ec48bbSMarcin Wojtas 		if (unlikely(ena_com_free_desc(io_sq) <=
285967ec48bbSMarcin Wojtas 		    ENA_TX_CLEANUP_THRESHOLD))
28609b8d05b8SZbigniew Bodek 			ena_tx_cleanup(tx_ring);
28619b8d05b8SZbigniew Bodek 
28623f9ed7abSMarcin Wojtas 		if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
28639b8d05b8SZbigniew Bodek 			if (ret == ENA_COM_NO_MEM) {
28649b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
28659b8d05b8SZbigniew Bodek 			} else if (ret == ENA_COM_NO_SPACE) {
28669b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
28679b8d05b8SZbigniew Bodek 			} else {
28689b8d05b8SZbigniew Bodek 				m_freem(mbuf);
28699b8d05b8SZbigniew Bodek 				drbr_advance(adapter->ifp, tx_ring->br);
28709b8d05b8SZbigniew Bodek 			}
28719b8d05b8SZbigniew Bodek 
28729b8d05b8SZbigniew Bodek 			break;
28739b8d05b8SZbigniew Bodek 		}
28749b8d05b8SZbigniew Bodek 
2875b4b29032SZbigniew Bodek 		drbr_advance(adapter->ifp, tx_ring->br);
2876b4b29032SZbigniew Bodek 
28773f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(adapter->ifp) &
28783f9ed7abSMarcin Wojtas 		    IFF_DRV_RUNNING) == 0))
28799b8d05b8SZbigniew Bodek 			return;
28809b8d05b8SZbigniew Bodek 
28819b8d05b8SZbigniew Bodek 		acum_pkts++;
28829b8d05b8SZbigniew Bodek 
28839b8d05b8SZbigniew Bodek 		BPF_MTAP(adapter->ifp, mbuf);
28849b8d05b8SZbigniew Bodek 
28853f9ed7abSMarcin Wojtas 		if (unlikely(acum_pkts == DB_THRESHOLD)) {
28869b8d05b8SZbigniew Bodek 			acum_pkts = 0;
28879b8d05b8SZbigniew Bodek 			wmb();
28889b8d05b8SZbigniew Bodek 			/* Trigger the dma engine */
28899b8d05b8SZbigniew Bodek 			ena_com_write_sq_doorbell(io_sq);
28909b8d05b8SZbigniew Bodek 			counter_u64_add(tx_ring->tx_stats.doorbells, 1);
28919b8d05b8SZbigniew Bodek 		}
28929b8d05b8SZbigniew Bodek 
28939b8d05b8SZbigniew Bodek 	}
28949b8d05b8SZbigniew Bodek 
28953f9ed7abSMarcin Wojtas 	if (likely(acum_pkts != 0)) {
28969b8d05b8SZbigniew Bodek 		wmb();
28979b8d05b8SZbigniew Bodek 		/* Trigger the dma engine */
28989b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(io_sq);
28999b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
29009b8d05b8SZbigniew Bodek 	}
29019b8d05b8SZbigniew Bodek 
290267ec48bbSMarcin Wojtas 	if (ena_com_free_desc(io_sq) <= ENA_TX_CLEANUP_THRESHOLD)
29039b8d05b8SZbigniew Bodek 		ena_tx_cleanup(tx_ring);
29049b8d05b8SZbigniew Bodek }
29059b8d05b8SZbigniew Bodek 
29069b8d05b8SZbigniew Bodek static void
29079b8d05b8SZbigniew Bodek ena_deferred_mq_start(void *arg, int pending)
29089b8d05b8SZbigniew Bodek {
29099b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
29109b8d05b8SZbigniew Bodek 	struct ifnet *ifp = tx_ring->adapter->ifp;
29119b8d05b8SZbigniew Bodek 
29120bdffe59SMarcin Wojtas 	while (!drbr_empty(ifp, tx_ring->br) &&
29130bdffe59SMarcin Wojtas 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
29149b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
29159b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
29169b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
29179b8d05b8SZbigniew Bodek 	}
29189b8d05b8SZbigniew Bodek }
29199b8d05b8SZbigniew Bodek 
29209b8d05b8SZbigniew Bodek static int
29219b8d05b8SZbigniew Bodek ena_mq_start(if_t ifp, struct mbuf *m)
29229b8d05b8SZbigniew Bodek {
29239b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
29249b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
29259b8d05b8SZbigniew Bodek 	int ret, is_drbr_empty;
29269b8d05b8SZbigniew Bodek 	uint32_t i;
29279b8d05b8SZbigniew Bodek 
29283f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
29299b8d05b8SZbigniew Bodek 		return (ENODEV);
29309b8d05b8SZbigniew Bodek 
29319b8d05b8SZbigniew Bodek 	/* Which queue to use */
29329b8d05b8SZbigniew Bodek 	/*
29339b8d05b8SZbigniew Bodek 	 * If everything is setup correctly, it should be the
29349b8d05b8SZbigniew Bodek 	 * same bucket that the current CPU we're on is.
29359b8d05b8SZbigniew Bodek 	 * It should improve performance.
29369b8d05b8SZbigniew Bodek 	 */
29379b8d05b8SZbigniew Bodek 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
29389b8d05b8SZbigniew Bodek 		i = m->m_pkthdr.flowid % adapter->num_queues;
29399b8d05b8SZbigniew Bodek 	} else {
29409b8d05b8SZbigniew Bodek 		i = curcpu % adapter->num_queues;
29419b8d05b8SZbigniew Bodek 	}
29429b8d05b8SZbigniew Bodek 	tx_ring = &adapter->tx_ring[i];
29439b8d05b8SZbigniew Bodek 
29449b8d05b8SZbigniew Bodek 	/* Check if drbr is empty before putting packet */
29459b8d05b8SZbigniew Bodek 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
29469b8d05b8SZbigniew Bodek 	ret = drbr_enqueue(ifp, tx_ring->br, m);
29473f9ed7abSMarcin Wojtas 	if (unlikely(ret != 0)) {
29489b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
29499b8d05b8SZbigniew Bodek 		return (ret);
29509b8d05b8SZbigniew Bodek 	}
29519b8d05b8SZbigniew Bodek 
295267ec48bbSMarcin Wojtas 	if (is_drbr_empty && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
29539b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
29549b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
29559b8d05b8SZbigniew Bodek 	} else {
29569b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
29579b8d05b8SZbigniew Bodek 	}
29589b8d05b8SZbigniew Bodek 
29599b8d05b8SZbigniew Bodek 	return (0);
29609b8d05b8SZbigniew Bodek }
29619b8d05b8SZbigniew Bodek 
29629b8d05b8SZbigniew Bodek static void
29639b8d05b8SZbigniew Bodek ena_qflush(if_t ifp)
29649b8d05b8SZbigniew Bodek {
29659b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
29669b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = adapter->tx_ring;
29679b8d05b8SZbigniew Bodek 	int i;
29689b8d05b8SZbigniew Bodek 
29699b8d05b8SZbigniew Bodek 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
29700bdffe59SMarcin Wojtas 		if (!drbr_empty(ifp, tx_ring->br)) {
29719b8d05b8SZbigniew Bodek 			ENA_RING_MTX_LOCK(tx_ring);
29729b8d05b8SZbigniew Bodek 			drbr_flush(ifp, tx_ring->br);
29739b8d05b8SZbigniew Bodek 			ENA_RING_MTX_UNLOCK(tx_ring);
29749b8d05b8SZbigniew Bodek 		}
29759b8d05b8SZbigniew Bodek 
29769b8d05b8SZbigniew Bodek 	if_qflush(ifp);
29779b8d05b8SZbigniew Bodek }
29789b8d05b8SZbigniew Bodek 
29790bdffe59SMarcin Wojtas static int
29800bdffe59SMarcin Wojtas ena_calc_io_queue_num(struct ena_adapter *adapter,
29819b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
29829b8d05b8SZbigniew Bodek {
29839b8d05b8SZbigniew Bodek 	int io_sq_num, io_cq_num, io_queue_num;
29849b8d05b8SZbigniew Bodek 
29859b8d05b8SZbigniew Bodek 	io_sq_num = get_feat_ctx->max_queues.max_sq_num;
2986ad5a5afaSMarcin Wojtas 	io_cq_num = get_feat_ctx->max_queues.max_cq_num;
29879b8d05b8SZbigniew Bodek 
29889b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
29899b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_sq_num);
29909b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_cq_num);
29919b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
29929b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num,
29939b8d05b8SZbigniew Bodek 	    pci_msix_count(adapter->pdev) - 1);
29949b8d05b8SZbigniew Bodek 
29950bdffe59SMarcin Wojtas 	return (io_queue_num);
29969b8d05b8SZbigniew Bodek }
29979b8d05b8SZbigniew Bodek 
29980bdffe59SMarcin Wojtas static int
29990bdffe59SMarcin Wojtas ena_calc_queue_size(struct ena_adapter *adapter, uint16_t *max_tx_sgl_size,
30000bdffe59SMarcin Wojtas     uint16_t *max_rx_sgl_size, struct ena_com_dev_get_features_ctx *feat)
30019b8d05b8SZbigniew Bodek {
30029b8d05b8SZbigniew Bodek 	uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
30039b8d05b8SZbigniew Bodek 	uint32_t v;
30049b8d05b8SZbigniew Bodek 	uint32_t q;
30059b8d05b8SZbigniew Bodek 
30069b8d05b8SZbigniew Bodek 	queue_size = min_t(uint32_t, queue_size,
30079b8d05b8SZbigniew Bodek 	    feat->max_queues.max_cq_depth);
30089b8d05b8SZbigniew Bodek 	queue_size = min_t(uint32_t, queue_size,
30099b8d05b8SZbigniew Bodek 	    feat->max_queues.max_sq_depth);
30109b8d05b8SZbigniew Bodek 
30119b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
30129b8d05b8SZbigniew Bodek 	v = queue_size;
30139b8d05b8SZbigniew Bodek 	while (v != 0) {
30140bdffe59SMarcin Wojtas 		if (powerof2(queue_size) != 0)
30159b8d05b8SZbigniew Bodek 			break;
30169b8d05b8SZbigniew Bodek 		v /= 2;
30179b8d05b8SZbigniew Bodek 		q = rounddown2(queue_size, v);
30189b8d05b8SZbigniew Bodek 		if (q != 0) {
30199b8d05b8SZbigniew Bodek 			queue_size = q;
30209b8d05b8SZbigniew Bodek 			break;
30219b8d05b8SZbigniew Bodek 		}
30229b8d05b8SZbigniew Bodek 	}
30239b8d05b8SZbigniew Bodek 
30240bdffe59SMarcin Wojtas 	if (unlikely(queue_size == 0)) {
30259b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid queue size\n");
30260bdffe59SMarcin Wojtas 		return (ENA_COM_FAULT);
30279b8d05b8SZbigniew Bodek 	}
30289b8d05b8SZbigniew Bodek 
30299b8d05b8SZbigniew Bodek 	*max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
30309b8d05b8SZbigniew Bodek 	    feat->max_queues.max_packet_tx_descs);
30319b8d05b8SZbigniew Bodek 	*max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
30329b8d05b8SZbigniew Bodek 	    feat->max_queues.max_packet_rx_descs);
30339b8d05b8SZbigniew Bodek 
30340bdffe59SMarcin Wojtas 	return (queue_size);
30359b8d05b8SZbigniew Bodek }
30369b8d05b8SZbigniew Bodek 
30370bdffe59SMarcin Wojtas static int
30380bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
30399b8d05b8SZbigniew Bodek {
30409b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
30419b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
30429b8d05b8SZbigniew Bodek 	int qid, rc, i;
30439b8d05b8SZbigniew Bodek 
30449b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
30450bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
30464e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
30477d2544e6SMarcin Wojtas 		return (rc);
30489b8d05b8SZbigniew Bodek 	}
30499b8d05b8SZbigniew Bodek 
30509b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
30519b8d05b8SZbigniew Bodek 		qid = i % adapter->num_queues;
30529b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
30539b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
30540bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
30559b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
30567d2544e6SMarcin Wojtas 			goto err_rss_destroy;
30579b8d05b8SZbigniew Bodek 		}
30589b8d05b8SZbigniew Bodek 	}
30599b8d05b8SZbigniew Bodek 
30609b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
30619b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
30620bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
30639b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
30647d2544e6SMarcin Wojtas 		goto err_rss_destroy;
30659b8d05b8SZbigniew Bodek 	}
30669b8d05b8SZbigniew Bodek 
30679b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
30680bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
30699b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
30707d2544e6SMarcin Wojtas 		goto err_rss_destroy;
30719b8d05b8SZbigniew Bodek 	}
30729b8d05b8SZbigniew Bodek 
30739b8d05b8SZbigniew Bodek 	return (0);
30749b8d05b8SZbigniew Bodek 
30757d2544e6SMarcin Wojtas err_rss_destroy:
30769b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
30779b8d05b8SZbigniew Bodek 	return (rc);
30789b8d05b8SZbigniew Bodek }
30799b8d05b8SZbigniew Bodek 
30809b8d05b8SZbigniew Bodek static void
30819b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
30829b8d05b8SZbigniew Bodek {
30839b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
30849b8d05b8SZbigniew Bodek 	devclass_t dc;
30859b8d05b8SZbigniew Bodek 	int max;
30869b8d05b8SZbigniew Bodek 	int rc;
30879b8d05b8SZbigniew Bodek 
30889b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
30893f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
30904e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "No devclass ena\n");
30919b8d05b8SZbigniew Bodek 		return;
30929b8d05b8SZbigniew Bodek 	}
30939b8d05b8SZbigniew Bodek 
30949b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
30959b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
30969b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
30979b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
30989b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
30999b8d05b8SZbigniew Bodek 			adapter->rss_support = true;
31003f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
31019b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
31029b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
31030bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
31049b8d05b8SZbigniew Bodek 				adapter->rss_support = false;
31059b8d05b8SZbigniew Bodek 			}
31069b8d05b8SZbigniew Bodek 		}
31079b8d05b8SZbigniew Bodek 	}
31089b8d05b8SZbigniew Bodek }
31099b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
31109b8d05b8SZbigniew Bodek 
31110bdffe59SMarcin Wojtas static void
31120bdffe59SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev)
31139b8d05b8SZbigniew Bodek {
31149b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
31159b8d05b8SZbigniew Bodek 	int rc;
31169b8d05b8SZbigniew Bodek 
31179b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
31189b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
31193f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
31209b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
31219b8d05b8SZbigniew Bodek 		return;
31229b8d05b8SZbigniew Bodek 	}
31239b8d05b8SZbigniew Bodek 
31249b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
31259b8d05b8SZbigniew Bodek 
31269b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
31279b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
31289b8d05b8SZbigniew Bodek 
31299b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
31309b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
31319b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
31329b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
31339b8d05b8SZbigniew Bodek 
31349b8d05b8SZbigniew Bodek 	host_info->driver_version =
31359b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
31369b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
31379b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
31388ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
31399b8d05b8SZbigniew Bodek 
31409b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
31413f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
3142a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
31439b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
31449b8d05b8SZbigniew Bodek 		else
31459b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
31469b8d05b8SZbigniew Bodek 
31479b8d05b8SZbigniew Bodek 		goto err;
31489b8d05b8SZbigniew Bodek 	}
31499b8d05b8SZbigniew Bodek 
31509b8d05b8SZbigniew Bodek 	return;
31519b8d05b8SZbigniew Bodek 
31529b8d05b8SZbigniew Bodek err:
31539b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
31549b8d05b8SZbigniew Bodek }
31559b8d05b8SZbigniew Bodek 
31569b8d05b8SZbigniew Bodek static int
31579b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
31589b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
31599b8d05b8SZbigniew Bodek {
31609b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
31619b8d05b8SZbigniew Bodek 	bool readless_supported;
31629b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
31639b8d05b8SZbigniew Bodek 	int dma_width;
31649b8d05b8SZbigniew Bodek 	int rc;
31659b8d05b8SZbigniew Bodek 
31669b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
31673f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
31689b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
31690bdffe59SMarcin Wojtas 		return (rc);
31709b8d05b8SZbigniew Bodek 	}
31719b8d05b8SZbigniew Bodek 
31729b8d05b8SZbigniew Bodek 	/*
31739b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
31749b8d05b8SZbigniew Bodek 	 * read is disabled
31759b8d05b8SZbigniew Bodek 	 */
31769b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
31779b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
31789b8d05b8SZbigniew Bodek 
3179a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
31803f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
31819b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
31829b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
31839b8d05b8SZbigniew Bodek 	}
31849b8d05b8SZbigniew Bodek 
31859b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
31863f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
31879b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
31889b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
31899b8d05b8SZbigniew Bodek 	}
31909b8d05b8SZbigniew Bodek 
31919b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
31923f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
31939b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
31949b8d05b8SZbigniew Bodek 		rc = dma_width;
31959b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
31969b8d05b8SZbigniew Bodek 	}
31979b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
31989b8d05b8SZbigniew Bodek 
31999b8d05b8SZbigniew Bodek 	/* ENA admin level init */
320067ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
32013f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32029b8d05b8SZbigniew Bodek 		device_printf(pdev,
32039b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
32049b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
32059b8d05b8SZbigniew Bodek 	}
32069b8d05b8SZbigniew Bodek 
32079b8d05b8SZbigniew Bodek 	/*
32089b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
32099b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
32109b8d05b8SZbigniew Bodek 	 * information
32119b8d05b8SZbigniew Bodek 	 */
32129b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
32139b8d05b8SZbigniew Bodek 
32149b8d05b8SZbigniew Bodek 	ena_config_host_info(ena_dev);
32159b8d05b8SZbigniew Bodek 
32169b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
32179b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
32183f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32199b8d05b8SZbigniew Bodek 		device_printf(pdev,
32209b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
32219b8d05b8SZbigniew Bodek 		goto err_admin_init;
32229b8d05b8SZbigniew Bodek 	}
32239b8d05b8SZbigniew Bodek 
3224e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
3225e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
3226e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
322740621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
3228e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
32299b8d05b8SZbigniew Bodek 
32309b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
32319b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
32323f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32339b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
32349b8d05b8SZbigniew Bodek 		goto err_admin_init;
32359b8d05b8SZbigniew Bodek 	}
32369b8d05b8SZbigniew Bodek 
32379b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
32389b8d05b8SZbigniew Bodek 
32390bdffe59SMarcin Wojtas 	return (0);
32409b8d05b8SZbigniew Bodek 
32419b8d05b8SZbigniew Bodek err_admin_init:
32429b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
32439b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
32449b8d05b8SZbigniew Bodek err_mmio_read_less:
32459b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
32469b8d05b8SZbigniew Bodek 
32470bdffe59SMarcin Wojtas 	return (rc);
32489b8d05b8SZbigniew Bodek }
32499b8d05b8SZbigniew Bodek 
32509b8d05b8SZbigniew Bodek static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
32519b8d05b8SZbigniew Bodek     int io_vectors)
32529b8d05b8SZbigniew Bodek {
32539b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
32549b8d05b8SZbigniew Bodek 	int rc;
32559b8d05b8SZbigniew Bodek 
32569b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
32573f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32589b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
32590bdffe59SMarcin Wojtas 		return (rc);
32609b8d05b8SZbigniew Bodek 	}
32619b8d05b8SZbigniew Bodek 
32629b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
32639b8d05b8SZbigniew Bodek 
32649b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
32653f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32669b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
32679b8d05b8SZbigniew Bodek 		goto err_disable_msix;
32689b8d05b8SZbigniew Bodek 	}
32699b8d05b8SZbigniew Bodek 
32709b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
32719b8d05b8SZbigniew Bodek 
32729b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
32739b8d05b8SZbigniew Bodek 
32740bdffe59SMarcin Wojtas 	return (0);
32759b8d05b8SZbigniew Bodek 
32769b8d05b8SZbigniew Bodek err_disable_msix:
32779b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
32789b8d05b8SZbigniew Bodek 
32790bdffe59SMarcin Wojtas 	return (rc);
32809b8d05b8SZbigniew Bodek }
32819b8d05b8SZbigniew Bodek 
32829b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
32839b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
32849b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
32859b8d05b8SZbigniew Bodek {
32869b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
328730217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
32889b8d05b8SZbigniew Bodek 	sbintime_t stime;
328930217e2dSMarcin Wojtas 	uint64_t rx_drops;
329030217e2dSMarcin Wojtas 
329130217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
329230217e2dSMarcin Wojtas 
329330217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
329430217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
329530217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
32969b8d05b8SZbigniew Bodek 
32979b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
32989b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
32999b8d05b8SZbigniew Bodek }
33009b8d05b8SZbigniew Bodek 
33019b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
33029b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
33039b8d05b8SZbigniew Bodek {
33049b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
33059b8d05b8SZbigniew Bodek 
33069b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
33079b8d05b8SZbigniew Bodek 		return;
33089b8d05b8SZbigniew Bodek 
330940621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
33109b8d05b8SZbigniew Bodek 		return;
33119b8d05b8SZbigniew Bodek 
33129b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
33139b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
33149b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
33159b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33169b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
33179b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
3318a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
33199b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
33209b8d05b8SZbigniew Bodek 	}
33219b8d05b8SZbigniew Bodek }
33229b8d05b8SZbigniew Bodek 
33239b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
33249b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
33259b8d05b8SZbigniew Bodek {
33260bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
33270bdffe59SMarcin Wojtas 	    false)) {
33289b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33299b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
33309b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
3331a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
33329b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
33339b8d05b8SZbigniew Bodek 	}
33349b8d05b8SZbigniew Bodek }
33359b8d05b8SZbigniew Bodek 
333674dba3adSMarcin Wojtas static int
3337*d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3338*d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
3339*d12f7bfcSMarcin Wojtas {
3340*d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
3341*d12f7bfcSMarcin Wojtas 		return (0);
3342*d12f7bfcSMarcin Wojtas 
3343*d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3344*d12f7bfcSMarcin Wojtas 		return (0);
3345*d12f7bfcSMarcin Wojtas 
3346*d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
3347*d12f7bfcSMarcin Wojtas 
3348*d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3349*d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
3350*d12f7bfcSMarcin Wojtas 		   "Queue = %d. Reset the device\n", rx_ring->qid);
3351*d12f7bfcSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3352*d12f7bfcSMarcin Wojtas 		adapter->trigger_reset = true;
3353*d12f7bfcSMarcin Wojtas 		return (EIO);
3354*d12f7bfcSMarcin Wojtas 	}
3355*d12f7bfcSMarcin Wojtas 
3356*d12f7bfcSMarcin Wojtas 	return (0);
3357*d12f7bfcSMarcin Wojtas }
3358*d12f7bfcSMarcin Wojtas 
3359*d12f7bfcSMarcin Wojtas static int
3360*d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
336174dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
336274dba3adSMarcin Wojtas {
336374dba3adSMarcin Wojtas 	struct bintime curtime, time;
336474dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
3365*d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
336674dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
3367*d12f7bfcSMarcin Wojtas 	int i, rc = 0;
336874dba3adSMarcin Wojtas 
336974dba3adSMarcin Wojtas 	getbinuptime(&curtime);
337074dba3adSMarcin Wojtas 
337174dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
337274dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
337374dba3adSMarcin Wojtas 
33740bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
337574dba3adSMarcin Wojtas 			continue;
337674dba3adSMarcin Wojtas 
337774dba3adSMarcin Wojtas 		time = curtime;
337874dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
3379*d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
3380*d12f7bfcSMarcin Wojtas 
3381*d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
3382*d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
3383*d12f7bfcSMarcin Wojtas 			/*
3384*d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
3385*d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
3386*d12f7bfcSMarcin Wojtas 			 */
3387*d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
3388*d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
3389*d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
3390*d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3391*d12f7bfcSMarcin Wojtas 			adapter->trigger_reset = true;
3392*d12f7bfcSMarcin Wojtas 			return (EIO);
3393*d12f7bfcSMarcin Wojtas 		}
339474dba3adSMarcin Wojtas 
339574dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
3396*d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
339774dba3adSMarcin Wojtas 
339874dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
339974dba3adSMarcin Wojtas 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
340074dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
340174dba3adSMarcin Wojtas 				    tx_ring->qid, i);
340274dba3adSMarcin Wojtas 
340374dba3adSMarcin Wojtas 			tx_buf->print_once = true;
340474dba3adSMarcin Wojtas 			missed_tx++;
3405*d12f7bfcSMarcin Wojtas 		}
3406*d12f7bfcSMarcin Wojtas 	}
340774dba3adSMarcin Wojtas 
3408*d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
340974dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
3410*d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
3411*d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
34124e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
3413*d12f7bfcSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_MISS_TX_CMPL;
341474dba3adSMarcin Wojtas 		adapter->trigger_reset = true;
3415*d12f7bfcSMarcin Wojtas 		rc = EIO;
341674dba3adSMarcin Wojtas 	}
341774dba3adSMarcin Wojtas 
3418*d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
3419*d12f7bfcSMarcin Wojtas 
3420*d12f7bfcSMarcin Wojtas 	return (rc);
342174dba3adSMarcin Wojtas }
342274dba3adSMarcin Wojtas 
34239b8d05b8SZbigniew Bodek /*
34249b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
34259b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
34269b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
34279b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
34289b8d05b8SZbigniew Bodek  */
34290bdffe59SMarcin Wojtas static void
3430*d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
34319b8d05b8SZbigniew Bodek {
34329b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
3433*d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
343474dba3adSMarcin Wojtas 	int i, budget, rc;
34359b8d05b8SZbigniew Bodek 
34369b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
34379b8d05b8SZbigniew Bodek 	rmb();
34389b8d05b8SZbigniew Bodek 
34399b8d05b8SZbigniew Bodek 	if (!adapter->up)
34409b8d05b8SZbigniew Bodek 		return;
34419b8d05b8SZbigniew Bodek 
34429b8d05b8SZbigniew Bodek 	if (adapter->trigger_reset)
34439b8d05b8SZbigniew Bodek 		return;
34449b8d05b8SZbigniew Bodek 
344540621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
34469b8d05b8SZbigniew Bodek 		return;
34479b8d05b8SZbigniew Bodek 
34489b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
34499b8d05b8SZbigniew Bodek 
34509b8d05b8SZbigniew Bodek 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
34519b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
3452*d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
34539b8d05b8SZbigniew Bodek 
3454*d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3455*d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
3456*d12f7bfcSMarcin Wojtas 			return;
3457*d12f7bfcSMarcin Wojtas 
3458*d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
34590bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
34609b8d05b8SZbigniew Bodek 			return;
34619b8d05b8SZbigniew Bodek 
34629b8d05b8SZbigniew Bodek 		budget--;
3463cd5d5804SMarcin Wojtas 		if (budget == 0) {
34649b8d05b8SZbigniew Bodek 			i++;
34659b8d05b8SZbigniew Bodek 			break;
34669b8d05b8SZbigniew Bodek 		}
34679b8d05b8SZbigniew Bodek 	}
34689b8d05b8SZbigniew Bodek 
34699b8d05b8SZbigniew Bodek 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
34709b8d05b8SZbigniew Bodek }
34719b8d05b8SZbigniew Bodek 
3472efe6ab18SMarcin Wojtas /* trigger deferred rx cleanup after 2 consecutive detections */
3473efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
3474efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
3475efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
3476efe6ab18SMarcin Wojtas  * for example).
3477efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
3478efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
3479efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
3480efe6ab18SMarcin Wojtas  * able to send new packets.
3481efe6ab18SMarcin Wojtas  *
3482efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
3483efe6ab18SMarcin Wojtas  */
3484efe6ab18SMarcin Wojtas static void
3485efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
3486efe6ab18SMarcin Wojtas {
3487efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
3488efe6ab18SMarcin Wojtas 	int i, refill_required;
3489efe6ab18SMarcin Wojtas 
3490efe6ab18SMarcin Wojtas 	if (!adapter->up)
3491efe6ab18SMarcin Wojtas 		return;
3492efe6ab18SMarcin Wojtas 
3493efe6ab18SMarcin Wojtas 	if (adapter->trigger_reset)
3494efe6ab18SMarcin Wojtas 		return;
3495efe6ab18SMarcin Wojtas 
3496efe6ab18SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
3497efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
3498efe6ab18SMarcin Wojtas 
3499efe6ab18SMarcin Wojtas 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
3500efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3501efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
3502efe6ab18SMarcin Wojtas 
3503efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
3504efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
3505efe6ab18SMarcin Wojtas 				    1);
3506efe6ab18SMarcin Wojtas 
3507efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
3508efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
3509efe6ab18SMarcin Wojtas 
3510efe6ab18SMarcin Wojtas 				taskqueue_enqueue(rx_ring->cmpl_tq,
3511efe6ab18SMarcin Wojtas 				    &rx_ring->cmpl_task);
3512efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
3513efe6ab18SMarcin Wojtas 			}
3514efe6ab18SMarcin Wojtas 		} else {
3515efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
3516efe6ab18SMarcin Wojtas 		}
3517efe6ab18SMarcin Wojtas 	}
3518efe6ab18SMarcin Wojtas }
35199b8d05b8SZbigniew Bodek 
352040621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
352140621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
352240621d71SMarcin Wojtas {
352340621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
352440621d71SMarcin Wojtas 
352540621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
352640621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
352740621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
352840621d71SMarcin Wojtas 
352940621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
353040621d71SMarcin Wojtas 		/* convert to usec */
353140621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
353240621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
353340621d71SMarcin Wojtas 
353440621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
353540621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
353640621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
353740621d71SMarcin Wojtas 
353840621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
353940621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
354040621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
354140621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
354240621d71SMarcin Wojtas 		else
354340621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
354440621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
354540621d71SMarcin Wojtas 	}
354640621d71SMarcin Wojtas 
354740621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
354840621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
354940621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
355040621d71SMarcin Wojtas 		else
355140621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
355240621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
355340621d71SMarcin Wojtas 	}
355440621d71SMarcin Wojtas }
355540621d71SMarcin Wojtas 
35569b8d05b8SZbigniew Bodek static void
35579b8d05b8SZbigniew Bodek ena_timer_service(void *data)
35589b8d05b8SZbigniew Bodek {
35599b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
35609b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
35619b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
35629b8d05b8SZbigniew Bodek 
35639b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
35649b8d05b8SZbigniew Bodek 
35659b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
35669b8d05b8SZbigniew Bodek 
3567*d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
35689b8d05b8SZbigniew Bodek 
3569efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
3570efe6ab18SMarcin Wojtas 
35710bdffe59SMarcin Wojtas 	if (host_info != NULL)
35729b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
35739b8d05b8SZbigniew Bodek 
35749b8d05b8SZbigniew Bodek 	if (unlikely(adapter->trigger_reset)) {
35759b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
35769b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
35779b8d05b8SZbigniew Bodek 		return;
35789b8d05b8SZbigniew Bodek 	}
35799b8d05b8SZbigniew Bodek 
35809b8d05b8SZbigniew Bodek 	/*
35819b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
35829b8d05b8SZbigniew Bodek 	 */
35839b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
35849b8d05b8SZbigniew Bodek }
35859b8d05b8SZbigniew Bodek 
35869b8d05b8SZbigniew Bodek static void
35879b8d05b8SZbigniew Bodek ena_reset_task(void *arg, int pending)
35889b8d05b8SZbigniew Bodek {
35899b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
35909b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
35919b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
35929b8d05b8SZbigniew Bodek 	bool dev_up;
35939b8d05b8SZbigniew Bodek 	int rc;
35949b8d05b8SZbigniew Bodek 
35959b8d05b8SZbigniew Bodek 	if (unlikely(!adapter->trigger_reset)) {
35969b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
35979b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
35989b8d05b8SZbigniew Bodek 		return;
35999b8d05b8SZbigniew Bodek 	}
36009b8d05b8SZbigniew Bodek 
36019b8d05b8SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
36029b8d05b8SZbigniew Bodek 
36039b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
36049b8d05b8SZbigniew Bodek 
36059b8d05b8SZbigniew Bodek 	dev_up = adapter->up;
36069b8d05b8SZbigniew Bodek 
36079b8d05b8SZbigniew Bodek 	ena_com_set_admin_running_state(ena_dev, false);
36089b8d05b8SZbigniew Bodek 	ena_down(adapter);
3609a195fab0SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
36109b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
36119b8d05b8SZbigniew Bodek 	ena_com_abort_admin_commands(ena_dev);
36129b8d05b8SZbigniew Bodek 	ena_com_wait_for_abort_completion(ena_dev);
36139b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36149b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
36159b8d05b8SZbigniew Bodek 
3616a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
36179b8d05b8SZbigniew Bodek 	adapter->trigger_reset = false;
36189b8d05b8SZbigniew Bodek 
36199b8d05b8SZbigniew Bodek 	/* Finished destroy part. Restart the device */
36209b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
36219b8d05b8SZbigniew Bodek 	    &adapter->wd_active);
36223f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36239b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36249b8d05b8SZbigniew Bodek 		    "ENA device init failed! (err: %d)\n", rc);
36259b8d05b8SZbigniew Bodek 		goto err_dev_free;
36269b8d05b8SZbigniew Bodek 	}
36279b8d05b8SZbigniew Bodek 
36289b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
36299b8d05b8SZbigniew Bodek 	    adapter->num_queues);
36303f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36319b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Enable MSI-X failed\n");
36329b8d05b8SZbigniew Bodek 		goto err_com_free;
36339b8d05b8SZbigniew Bodek 	}
36349b8d05b8SZbigniew Bodek 
36359b8d05b8SZbigniew Bodek 	/* If the interface was up before the reset bring it up */
36369b8d05b8SZbigniew Bodek 	if (dev_up) {
36379b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
36383f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
36399b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
36409b8d05b8SZbigniew Bodek 			    "Failed to create I/O queues\n");
36419b8d05b8SZbigniew Bodek 			goto err_msix_free;
36429b8d05b8SZbigniew Bodek 		}
36439b8d05b8SZbigniew Bodek 	}
36449b8d05b8SZbigniew Bodek 
36459b8d05b8SZbigniew Bodek 	callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
36469b8d05b8SZbigniew Bodek 	    ena_timer_service, (void *)adapter, 0);
36479b8d05b8SZbigniew Bodek 
36489b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
36499b8d05b8SZbigniew Bodek 
36509b8d05b8SZbigniew Bodek 	return;
36519b8d05b8SZbigniew Bodek 
36529b8d05b8SZbigniew Bodek err_msix_free:
36539b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
36549b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
36559b8d05b8SZbigniew Bodek err_com_free:
36569b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36579b8d05b8SZbigniew Bodek err_dev_free:
36589b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "ENA reset failed!\n");
36599b8d05b8SZbigniew Bodek 	adapter->running = false;
36609b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
36619b8d05b8SZbigniew Bodek }
36629b8d05b8SZbigniew Bodek 
36639b8d05b8SZbigniew Bodek /**
36649b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
36659b8d05b8SZbigniew Bodek  * @pdev: device information struct
36669b8d05b8SZbigniew Bodek  *
36679b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
36689b8d05b8SZbigniew Bodek  *
36699b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
36709b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
36719b8d05b8SZbigniew Bodek  * and a hardware reset occur.
36729b8d05b8SZbigniew Bodek  **/
36739b8d05b8SZbigniew Bodek static int
36749b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
36759b8d05b8SZbigniew Bodek {
36769b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
36779b8d05b8SZbigniew Bodek 	static int version_printed;
36789b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
36799b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
36809b8d05b8SZbigniew Bodek 	uint16_t tx_sgl_size = 0;
36819b8d05b8SZbigniew Bodek 	uint16_t rx_sgl_size = 0;
36829b8d05b8SZbigniew Bodek 	int io_queue_num;
36839b8d05b8SZbigniew Bodek 	int queue_size;
36849b8d05b8SZbigniew Bodek 	int rc;
36859b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
36869b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
36879b8d05b8SZbigniew Bodek 
36889b8d05b8SZbigniew Bodek 	mtx_init(&adapter->global_mtx, "ENA global mtx", NULL, MTX_DEF);
36899b8d05b8SZbigniew Bodek 	sx_init(&adapter->ioctl_sx, "ENA ioctl sx");
36909b8d05b8SZbigniew Bodek 
36919b8d05b8SZbigniew Bodek 	/* Set up the timer service */
36929b8d05b8SZbigniew Bodek 	callout_init_mtx(&adapter->timer_service, &adapter->global_mtx, 0);
36939b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
36949b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
36959b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
36969b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
36979b8d05b8SZbigniew Bodek 
36989b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
36999b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
37009b8d05b8SZbigniew Bodek 
37019b8d05b8SZbigniew Bodek 	rc = ena_allocate_pci_resources(adapter);
37023f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37039b8d05b8SZbigniew Bodek 		device_printf(pdev, "PCI resource allocation failed!\n");
37049b8d05b8SZbigniew Bodek 		ena_free_pci_resources(adapter);
3705cd5d5804SMarcin Wojtas 		return (rc);
37069b8d05b8SZbigniew Bodek 	}
37079b8d05b8SZbigniew Bodek 
37089b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3709cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3710cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
37119b8d05b8SZbigniew Bodek 
37129b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
37139b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
37149b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
37159b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
37169b8d05b8SZbigniew Bodek 
37179b8d05b8SZbigniew Bodek 	/* Store register resources */
37189b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
37199b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
37209b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
37219b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
37229b8d05b8SZbigniew Bodek 
37233f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
37249b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
37259b8d05b8SZbigniew Bodek 		rc = ENXIO;
3726cd5d5804SMarcin Wojtas 		goto err_bus_free;
37279b8d05b8SZbigniew Bodek 	}
37289b8d05b8SZbigniew Bodek 
37299b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
37309b8d05b8SZbigniew Bodek 
37319b8d05b8SZbigniew Bodek 	/* Device initialization */
37329b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
37333f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37349b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
37359b8d05b8SZbigniew Bodek 		rc = ENXIO;
37369b8d05b8SZbigniew Bodek 		goto err_bus_free;
37379b8d05b8SZbigniew Bodek 	}
37389b8d05b8SZbigniew Bodek 
37399b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
37409b8d05b8SZbigniew Bodek 
37419b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
37429b8d05b8SZbigniew Bodek 
37439b8d05b8SZbigniew Bodek 	/* Set for sure that interface is not up */
37449b8d05b8SZbigniew Bodek 	adapter->up = false;
37459b8d05b8SZbigniew Bodek 
37469b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
37479b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
37489b8d05b8SZbigniew Bodek 
37499b8d05b8SZbigniew Bodek 	/* calculate IO queue number to create */
37509b8d05b8SZbigniew Bodek 	io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx);
37519b8d05b8SZbigniew Bodek 
37529b8d05b8SZbigniew Bodek 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
37539b8d05b8SZbigniew Bodek 	    io_queue_num);
37549b8d05b8SZbigniew Bodek 	adapter->num_queues = io_queue_num;
37559b8d05b8SZbigniew Bodek 
37563cfadb28SMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
37573cfadb28SMarcin Wojtas 
37589b8d05b8SZbigniew Bodek 	/* calculatre ring sizes */
37599b8d05b8SZbigniew Bodek 	queue_size = ena_calc_queue_size(adapter,&tx_sgl_size,
37609b8d05b8SZbigniew Bodek 	    &rx_sgl_size, &get_feat_ctx);
37613f9ed7abSMarcin Wojtas 	if (unlikely((queue_size <= 0) || (io_queue_num <= 0))) {
37629b8d05b8SZbigniew Bodek 		rc = ENA_COM_FAULT;
37639b8d05b8SZbigniew Bodek 		goto err_com_free;
37649b8d05b8SZbigniew Bodek 	}
37659b8d05b8SZbigniew Bodek 
3766a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3767a195fab0SMarcin Wojtas 
37689b8d05b8SZbigniew Bodek 	adapter->tx_ring_size = queue_size;
37699b8d05b8SZbigniew Bodek 	adapter->rx_ring_size = queue_size;
37709b8d05b8SZbigniew Bodek 
37719b8d05b8SZbigniew Bodek 	adapter->max_tx_sgl_size = tx_sgl_size;
37729b8d05b8SZbigniew Bodek 	adapter->max_rx_sgl_size = rx_sgl_size;
37739b8d05b8SZbigniew Bodek 
37749b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
37759b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
37764e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
37774e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
3778cd5d5804SMarcin Wojtas 		goto err_com_free;
37794e8acd84SMarcin Wojtas 	}
37809b8d05b8SZbigniew Bodek 
37819b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
37824e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
37834e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
3784cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
37854e8acd84SMarcin Wojtas 	}
37869b8d05b8SZbigniew Bodek 
37879b8d05b8SZbigniew Bodek 	/* initialize rings basic information */
37889b8d05b8SZbigniew Bodek 	device_printf(pdev, "initalize %d io queues\n", io_queue_num);
3789cd5d5804SMarcin Wojtas 	ena_init_io_rings(adapter);
37909b8d05b8SZbigniew Bodek 
37919b8d05b8SZbigniew Bodek 	/* setup network interface */
37929b8d05b8SZbigniew Bodek 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
37933f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37949b8d05b8SZbigniew Bodek 		device_printf(pdev, "Error with network interface setup\n");
3795cd5d5804SMarcin Wojtas 		goto err_io_free;
37969b8d05b8SZbigniew Bodek 	}
37979b8d05b8SZbigniew Bodek 
37989b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
37993f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
38009b8d05b8SZbigniew Bodek 		device_printf(pdev,
38019b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
38029b8d05b8SZbigniew Bodek 		goto err_ifp_free;
38039b8d05b8SZbigniew Bodek 	}
38049b8d05b8SZbigniew Bodek 
3805081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3806081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3807081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3808081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3809081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3810081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3811081169f2SZbigniew Bodek 
38129b8d05b8SZbigniew Bodek 	/* Initialize statistics */
38139b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
38149b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
381530217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
381630217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
38179b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
38189b8d05b8SZbigniew Bodek 
38199b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
38209b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
38219b8d05b8SZbigniew Bodek 
38229b8d05b8SZbigniew Bodek 	adapter->running = true;
38239b8d05b8SZbigniew Bodek 	return (0);
38249b8d05b8SZbigniew Bodek 
38259b8d05b8SZbigniew Bodek err_ifp_free:
38269b8d05b8SZbigniew Bodek 	if_detach(adapter->ifp);
38279b8d05b8SZbigniew Bodek 	if_free(adapter->ifp);
3828cd5d5804SMarcin Wojtas err_io_free:
38299b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
38309b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3831cd5d5804SMarcin Wojtas err_tx_tag_free:
38329b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3833cd5d5804SMarcin Wojtas err_com_free:
38349b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
38359b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
3836cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
38379b8d05b8SZbigniew Bodek err_bus_free:
38389b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
38399b8d05b8SZbigniew Bodek 	free(ena_dev, M_DEVBUF);
38409b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
3841cd5d5804SMarcin Wojtas 
38429b8d05b8SZbigniew Bodek 	return (rc);
38439b8d05b8SZbigniew Bodek }
38449b8d05b8SZbigniew Bodek 
38459b8d05b8SZbigniew Bodek /**
38469b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
38479b8d05b8SZbigniew Bodek  * @pdev: device information struct
38489b8d05b8SZbigniew Bodek  *
38499b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
38509b8d05b8SZbigniew Bodek  * that it should release a PCI device.
38519b8d05b8SZbigniew Bodek  **/
38529b8d05b8SZbigniew Bodek static int
38539b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
38549b8d05b8SZbigniew Bodek {
38559b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
38569b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
38579b8d05b8SZbigniew Bodek 	int rc;
38589b8d05b8SZbigniew Bodek 
38599b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
38609b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
38619b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
38629b8d05b8SZbigniew Bodek 		return (EBUSY);
38639b8d05b8SZbigniew Bodek 	}
38649b8d05b8SZbigniew Bodek 
38659b8d05b8SZbigniew Bodek 	/* Free reset task and callout */
38669b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
38679b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
38689b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
38699b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
38709b8d05b8SZbigniew Bodek 
3871e67c6554SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
38729b8d05b8SZbigniew Bodek 	ena_down(adapter);
3873e67c6554SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
38749b8d05b8SZbigniew Bodek 
38759b8d05b8SZbigniew Bodek 	if (adapter->ifp != NULL) {
38769b8d05b8SZbigniew Bodek 		ether_ifdetach(adapter->ifp);
38779b8d05b8SZbigniew Bodek 		if_free(adapter->ifp);
38789b8d05b8SZbigniew Bodek 	}
38799b8d05b8SZbigniew Bodek 
38809b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
38819b8d05b8SZbigniew Bodek 
388230217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
388330217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
38849b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
38859b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
38869b8d05b8SZbigniew Bodek 
38873f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support))
38889b8d05b8SZbigniew Bodek 		ena_com_rss_destroy(ena_dev);
38899b8d05b8SZbigniew Bodek 
38909b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
38913f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
38929b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
38939b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
38949b8d05b8SZbigniew Bodek 
38959b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
38963f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
38979b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
38989b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
38999b8d05b8SZbigniew Bodek 
39009b8d05b8SZbigniew Bodek 	/* Reset the device only if the device is running. */
39019b8d05b8SZbigniew Bodek 	if (adapter->running)
3902a195fab0SMarcin Wojtas 		ena_com_dev_reset(ena_dev, adapter->reset_reason);
39039b8d05b8SZbigniew Bodek 
39049b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
39059b8d05b8SZbigniew Bodek 
39069b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
39079b8d05b8SZbigniew Bodek 
3908197f0284SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
3909197f0284SMarcin Wojtas 
3910197f0284SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
3911197f0284SMarcin Wojtas 
3912197f0284SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
3913197f0284SMarcin Wojtas 
39149b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
39159b8d05b8SZbigniew Bodek 
39169b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
39179b8d05b8SZbigniew Bodek 
39189b8d05b8SZbigniew Bodek 	mtx_destroy(&adapter->global_mtx);
39199b8d05b8SZbigniew Bodek 	sx_destroy(&adapter->ioctl_sx);
39209b8d05b8SZbigniew Bodek 
39219b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
39229b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
39239b8d05b8SZbigniew Bodek 
39249b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
39259b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
39269b8d05b8SZbigniew Bodek 
39279b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
39289b8d05b8SZbigniew Bodek }
39299b8d05b8SZbigniew Bodek 
39309b8d05b8SZbigniew Bodek /******************************************************************************
39319b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
39329b8d05b8SZbigniew Bodek  *****************************************************************************/
39339b8d05b8SZbigniew Bodek /**
39349b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
39359b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
39369b8d05b8SZbigniew Bodek  **/
39379b8d05b8SZbigniew Bodek static void
39389b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
39399b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
39409b8d05b8SZbigniew Bodek {
39419b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
39429b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
39439b8d05b8SZbigniew Bodek 	int status;
39449b8d05b8SZbigniew Bodek 	if_t ifp;
39459b8d05b8SZbigniew Bodek 
39469b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
39479b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
39489b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
39499b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
39509b8d05b8SZbigniew Bodek 
39519b8d05b8SZbigniew Bodek 	if (status != 0) {
39529b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
39539b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_UP);
39549b8d05b8SZbigniew Bodek 	} else if (status == 0) {
39559b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
39569b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
39579b8d05b8SZbigniew Bodek 	} else {
39589b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "invalid value recvd\n");
39599b8d05b8SZbigniew Bodek 		BUG();
39609b8d05b8SZbigniew Bodek 	}
39619b8d05b8SZbigniew Bodek 
39629b8d05b8SZbigniew Bodek 	adapter->link_status = status;
39639b8d05b8SZbigniew Bodek }
39649b8d05b8SZbigniew Bodek 
396540621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
396640621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
396740621d71SMarcin Wojtas {
396840621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
396940621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
397040621d71SMarcin Wojtas 
397140621d71SMarcin Wojtas 	ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
397240621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
397340621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
397440621d71SMarcin Wojtas 
397540621d71SMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrom) {
397640621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
397740621d71SMarcin Wojtas 		hints =
397840621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
397940621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
398040621d71SMarcin Wojtas 		break;
398140621d71SMarcin Wojtas 	default:
398240621d71SMarcin Wojtas 		device_printf(adapter->pdev,
398340621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
398440621d71SMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrom);
398540621d71SMarcin Wojtas 	}
398640621d71SMarcin Wojtas }
398740621d71SMarcin Wojtas 
39889b8d05b8SZbigniew Bodek /**
39899b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
39909b8d05b8SZbigniew Bodek  **/
39919b8d05b8SZbigniew Bodek static void
3992e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
39939b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
39949b8d05b8SZbigniew Bodek {
3995e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3996e6de9a83SMarcin Wojtas 
3997e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
3998e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
39999b8d05b8SZbigniew Bodek }
40009b8d05b8SZbigniew Bodek 
40019b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
40029b8d05b8SZbigniew Bodek     .handlers = {
40039b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
400440621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
40059b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
40069b8d05b8SZbigniew Bodek     },
40079b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
40089b8d05b8SZbigniew Bodek };
40099b8d05b8SZbigniew Bodek 
40109b8d05b8SZbigniew Bodek /*********************************************************************
40119b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
40129b8d05b8SZbigniew Bodek  *********************************************************************/
40139b8d05b8SZbigniew Bodek 
40149b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
40159b8d05b8SZbigniew Bodek     /* Device interface */
40169b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
40179b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
40189b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
40199b8d05b8SZbigniew Bodek     DEVMETHOD_END
40209b8d05b8SZbigniew Bodek };
40219b8d05b8SZbigniew Bodek 
40229b8d05b8SZbigniew Bodek static driver_t ena_driver = {
40239b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
40249b8d05b8SZbigniew Bodek };
40259b8d05b8SZbigniew Bodek 
40269b8d05b8SZbigniew Bodek devclass_t ena_devclass;
40279b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
402840abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
4029329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
40309b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
40319b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
40329b8d05b8SZbigniew Bodek 
40339b8d05b8SZbigniew Bodek /*********************************************************************/
4034