xref: /freebsd/sys/dev/ena/ena.c (revision 9a0f2079ca900f9f37806e341790e2f3ed4cb19f)
19b8d05b8SZbigniew Bodek /*-
29b8d05b8SZbigniew Bodek  * BSD LICENSE
39b8d05b8SZbigniew Bodek  *
49d0073e4SMarcin Wojtas  * Copyright (c) 2015-2019 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 
764fa9e02dSMarcin Wojtas #include <vm/vm.h>
774fa9e02dSMarcin Wojtas #include <vm/pmap.h>
784fa9e02dSMarcin Wojtas 
7938c7b965SMarcin Wojtas #include "ena_datapath.h"
809b8d05b8SZbigniew Bodek #include "ena.h"
819b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
829b8d05b8SZbigniew Bodek 
83d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
84d17b7d87SMarcin Wojtas #include "ena_netmap.h"
85d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
86d17b7d87SMarcin Wojtas 
879b8d05b8SZbigniew Bodek /*********************************************************
889b8d05b8SZbigniew Bodek  *  Function prototypes
899b8d05b8SZbigniew Bodek  *********************************************************/
909b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
919b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
929b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
939b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
949b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
959b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
969b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
979b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
989b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
99cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
1009b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
1019b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
1029b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
1039b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
1049b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
1059b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
1069b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1079b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1089b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1099b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
1109b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1119b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1129b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1139b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1149b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1159b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1169b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1179b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1189b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1199b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1209b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1219b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1229b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1239b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1249b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1259b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1269b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1275cb9db07SMarcin Wojtas static int	ena_handle_msix(void *);
1289b8d05b8SZbigniew Bodek static int	ena_enable_msix(struct ena_adapter *);
1299b8d05b8SZbigniew Bodek static void	ena_setup_mgmnt_intr(struct ena_adapter *);
13077958fcdSMarcin Wojtas static int	ena_setup_io_intr(struct ena_adapter *);
1319b8d05b8SZbigniew Bodek static int	ena_request_mgmnt_irq(struct ena_adapter *);
1329b8d05b8SZbigniew Bodek static int	ena_request_io_irq(struct ena_adapter *);
1339b8d05b8SZbigniew Bodek static void	ena_free_mgmnt_irq(struct ena_adapter *);
1349b8d05b8SZbigniew Bodek static void	ena_free_io_irq(struct ena_adapter *);
1359b8d05b8SZbigniew Bodek static void	ena_free_irqs(struct ena_adapter*);
1369b8d05b8SZbigniew Bodek static void	ena_disable_msix(struct ena_adapter *);
1379b8d05b8SZbigniew Bodek static void	ena_unmask_all_io_irqs(struct ena_adapter *);
1389b8d05b8SZbigniew Bodek static int	ena_rss_configure(struct ena_adapter *);
1399b8d05b8SZbigniew Bodek static int	ena_up_complete(struct ena_adapter *);
1409b8d05b8SZbigniew Bodek static uint64_t	ena_get_counter(if_t, ift_counter);
1419b8d05b8SZbigniew Bodek static int	ena_media_change(if_t);
1429b8d05b8SZbigniew Bodek static void	ena_media_status(if_t, struct ifmediareq *);
1439b8d05b8SZbigniew Bodek static void	ena_init(void *);
1449b8d05b8SZbigniew Bodek static int	ena_ioctl(if_t, u_long, caddr_t);
1459b8d05b8SZbigniew Bodek static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
1469b8d05b8SZbigniew Bodek static void	ena_update_host_info(struct ena_admin_host_info *, if_t);
1479b8d05b8SZbigniew Bodek static void	ena_update_hwassist(struct ena_adapter *);
1489b8d05b8SZbigniew Bodek static int	ena_setup_ifnet(device_t, struct ena_adapter *,
1499b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1504fa9e02dSMarcin Wojtas static int	ena_enable_wc(struct resource *);
1514fa9e02dSMarcin Wojtas static int	ena_set_queues_placement_policy(device_t, struct ena_com_dev *,
1524fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *, struct ena_llq_configurations *);
1539b8d05b8SZbigniew Bodek static int	ena_calc_io_queue_num(struct ena_adapter *,
1549b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1556064f289SMarcin Wojtas static int	ena_calc_queue_size(struct ena_adapter *,
1566064f289SMarcin Wojtas     struct ena_calc_queue_size_ctx *);
1576064f289SMarcin Wojtas static int	ena_handle_updated_queues(struct ena_adapter *,
1586064f289SMarcin Wojtas     struct ena_com_dev_get_features_ctx *);
1599b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1609b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
16146021271SMarcin Wojtas static void	ena_config_host_info(struct ena_com_dev *, device_t);
1629b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1639b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1649b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1659b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
1669b8d05b8SZbigniew Bodek static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *,
1679b8d05b8SZbigniew Bodek     int);
1689b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1699b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1709b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1719b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1729b8d05b8SZbigniew Bodek 
1739b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1749b8d05b8SZbigniew Bodek 
1759b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
1769b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
1779b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
1789b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
1799b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
1809b8d05b8SZbigniew Bodek     /* Last entry */
1819b8d05b8SZbigniew Bodek     { 0, 0, 0 }
1829b8d05b8SZbigniew Bodek };
1839b8d05b8SZbigniew Bodek 
1849b8d05b8SZbigniew Bodek /*
1859b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
1869b8d05b8SZbigniew Bodek  */
1879b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
1889b8d05b8SZbigniew Bodek 
1899b8d05b8SZbigniew Bodek void
1909b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1919b8d05b8SZbigniew Bodek {
1920bdffe59SMarcin Wojtas 	if (error != 0)
1939b8d05b8SZbigniew Bodek 		return;
1949b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
1959b8d05b8SZbigniew Bodek }
1969b8d05b8SZbigniew Bodek 
1979b8d05b8SZbigniew Bodek int
1989b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
1999b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
2009b8d05b8SZbigniew Bodek {
2019b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2020bdffe59SMarcin Wojtas 	uint32_t maxsize;
2030bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2049b8d05b8SZbigniew Bodek 	int error;
2059b8d05b8SZbigniew Bodek 
2060bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2070bdffe59SMarcin Wojtas 
2080bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2093f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2109b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2110bdffe59SMarcin Wojtas 
2129b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2139b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2148a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2158a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2169b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2179b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2189b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2199b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2209b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2219b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2229b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2239b8d05b8SZbigniew Bodek 	    &dma->tag);
2243f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2254e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2269b8d05b8SZbigniew Bodek 		goto fail_tag;
2279b8d05b8SZbigniew Bodek 	}
2289b8d05b8SZbigniew Bodek 
2299b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2309b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2313f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2324e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2334e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2349b8d05b8SZbigniew Bodek 		goto fail_map_create;
2359b8d05b8SZbigniew Bodek 	}
2369b8d05b8SZbigniew Bodek 
2379b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2389b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2399b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2403f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
2414e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2429b8d05b8SZbigniew Bodek 		goto fail_map_load;
2439b8d05b8SZbigniew Bodek 	}
2449b8d05b8SZbigniew Bodek 
245e8073738SMarcin Wojtas 	bus_dmamap_sync(dma->tag, dma->map,
246e8073738SMarcin Wojtas 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
247e8073738SMarcin Wojtas 
2489b8d05b8SZbigniew Bodek 	return (0);
2499b8d05b8SZbigniew Bodek 
2509b8d05b8SZbigniew Bodek fail_map_load:
2519b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2527d2544e6SMarcin Wojtas fail_map_create:
2539b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2549b8d05b8SZbigniew Bodek fail_tag:
2559b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2565b14f92eSMarcin Wojtas 	dma->vaddr = NULL;
2575b14f92eSMarcin Wojtas 	dma->paddr = 0;
2589b8d05b8SZbigniew Bodek 
2599b8d05b8SZbigniew Bodek 	return (error);
2609b8d05b8SZbigniew Bodek }
2619b8d05b8SZbigniew Bodek 
2629b8d05b8SZbigniew Bodek static void
2639b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
2649b8d05b8SZbigniew Bodek {
2659b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2669b8d05b8SZbigniew Bodek 
2679b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
2689b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2699b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
2709b8d05b8SZbigniew Bodek 	}
2719b8d05b8SZbigniew Bodek 
2729b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
2739b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2749b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
2759b8d05b8SZbigniew Bodek 	}
2769b8d05b8SZbigniew Bodek }
2779b8d05b8SZbigniew Bodek 
2789b8d05b8SZbigniew Bodek static int
2799b8d05b8SZbigniew Bodek ena_probe(device_t dev)
2809b8d05b8SZbigniew Bodek {
2819b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
2829b8d05b8SZbigniew Bodek 	char		adapter_name[60];
2839b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
2849b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
2859b8d05b8SZbigniew Bodek 
2869b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
2879b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
2889b8d05b8SZbigniew Bodek 
2899b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
2909b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
2919b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
2929b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
29330425f93SMarcin Wojtas 			ena_trace(ENA_DBG, "vendor=%x device=%x\n",
2949b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
2959b8d05b8SZbigniew Bodek 
2969b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
2979b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
2989b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
2999b8d05b8SZbigniew Bodek 		}
3009b8d05b8SZbigniew Bodek 
3019b8d05b8SZbigniew Bodek 		ent++;
3029b8d05b8SZbigniew Bodek 
3039b8d05b8SZbigniew Bodek 	}
3049b8d05b8SZbigniew Bodek 
3059b8d05b8SZbigniew Bodek 	return (ENXIO);
3069b8d05b8SZbigniew Bodek }
3079b8d05b8SZbigniew Bodek 
3089b8d05b8SZbigniew Bodek static int
3099b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3109b8d05b8SZbigniew Bodek {
3119b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3123cfadb28SMarcin Wojtas 	int rc;
3139b8d05b8SZbigniew Bodek 
3143cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3159b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3163cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3173cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3183cfadb28SMarcin Wojtas 		return (EINVAL);
3199b8d05b8SZbigniew Bodek 	}
3209b8d05b8SZbigniew Bodek 
3219b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3223cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
3233cfadb28SMarcin Wojtas 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
3243cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3253cfadb28SMarcin Wojtas 	} else {
3263cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3273cfadb28SMarcin Wojtas 		    new_mtu);
3283cfadb28SMarcin Wojtas 	}
3299b8d05b8SZbigniew Bodek 
3303cfadb28SMarcin Wojtas 	return (rc);
3319b8d05b8SZbigniew Bodek }
3329b8d05b8SZbigniew Bodek 
3339b8d05b8SZbigniew Bodek static inline void
3349b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3359b8d05b8SZbigniew Bodek {
3369b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3379b8d05b8SZbigniew Bodek 
3389b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3399b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3409b8d05b8SZbigniew Bodek }
3419b8d05b8SZbigniew Bodek 
3429b8d05b8SZbigniew Bodek static inline void
3439b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3449b8d05b8SZbigniew Bodek {
3459b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3469b8d05b8SZbigniew Bodek 
3479b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3489b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3499b8d05b8SZbigniew Bodek }
3509b8d05b8SZbigniew Bodek 
3519b8d05b8SZbigniew Bodek static inline void
3529b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3539b8d05b8SZbigniew Bodek {
3549b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3559b8d05b8SZbigniew Bodek 
3569b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3579b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
3589b8d05b8SZbigniew Bodek }
3599b8d05b8SZbigniew Bodek 
3609b8d05b8SZbigniew Bodek static void
3619b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
3629b8d05b8SZbigniew Bodek     uint16_t qid)
3639b8d05b8SZbigniew Bodek {
3649b8d05b8SZbigniew Bodek 
3659b8d05b8SZbigniew Bodek 	ring->qid = qid;
3669b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
3679b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
368d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
369d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
3709b8d05b8SZbigniew Bodek }
3719b8d05b8SZbigniew Bodek 
372cd5d5804SMarcin Wojtas static void
3739b8d05b8SZbigniew Bodek ena_init_io_rings(struct ena_adapter *adapter)
3749b8d05b8SZbigniew Bodek {
3759b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
3769b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
3779b8d05b8SZbigniew Bodek 	struct ena_que *que;
3789b8d05b8SZbigniew Bodek 	int i;
3799b8d05b8SZbigniew Bodek 
3809b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
3819b8d05b8SZbigniew Bodek 
3829b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
3839b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
3849b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
3859b8d05b8SZbigniew Bodek 
3869b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
3879b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
3889b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
3899b8d05b8SZbigniew Bodek 
3909b8d05b8SZbigniew Bodek 		/* TX specific ring state */
3919b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
3929b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
3939b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
3949b8d05b8SZbigniew Bodek 		txr->smoothed_interval =
3959b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
3969b8d05b8SZbigniew Bodek 
3979b8d05b8SZbigniew Bodek 		/* Allocate a buf ring */
3986064f289SMarcin Wojtas 		txr->buf_ring_size = adapter->buf_ring_size;
3996064f289SMarcin Wojtas 		txr->br = buf_ring_alloc(txr->buf_ring_size, M_DEVBUF,
4009b8d05b8SZbigniew Bodek 		    M_WAITOK, &txr->ring_mtx);
4019b8d05b8SZbigniew Bodek 
4029b8d05b8SZbigniew Bodek 		/* Alloc TX statistics. */
4039b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4049b8d05b8SZbigniew Bodek 		    sizeof(txr->tx_stats));
4059b8d05b8SZbigniew Bodek 
4069b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4079b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4089b8d05b8SZbigniew Bodek 		rxr->smoothed_interval =
4099b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
4109b8d05b8SZbigniew Bodek 
4119b8d05b8SZbigniew Bodek 		/* Alloc RX statistics. */
4129b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4139b8d05b8SZbigniew Bodek 		    sizeof(rxr->rx_stats));
4149b8d05b8SZbigniew Bodek 
4159b8d05b8SZbigniew Bodek 		/* Initialize locks */
4169b8d05b8SZbigniew Bodek 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4179b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4189b8d05b8SZbigniew Bodek 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4199b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4209b8d05b8SZbigniew Bodek 
4219b8d05b8SZbigniew Bodek 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
4229b8d05b8SZbigniew Bodek 
4239b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4249b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4259b8d05b8SZbigniew Bodek 		que->id = i;
4269b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4279b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4289b8d05b8SZbigniew Bodek 
4299b8d05b8SZbigniew Bodek 		txr->que = que;
4309b8d05b8SZbigniew Bodek 		rxr->que = que;
431efe6ab18SMarcin Wojtas 
432efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4339b8d05b8SZbigniew Bodek 	}
4349b8d05b8SZbigniew Bodek }
4359b8d05b8SZbigniew Bodek 
4369b8d05b8SZbigniew Bodek static void
4379b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4389b8d05b8SZbigniew Bodek {
4399b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4409b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4419b8d05b8SZbigniew Bodek 
4429b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4439b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4449b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4459b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4469b8d05b8SZbigniew Bodek 
4477d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4487d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4497d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4507d2544e6SMarcin Wojtas 
4519b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
4529b8d05b8SZbigniew Bodek }
4539b8d05b8SZbigniew Bodek 
4549b8d05b8SZbigniew Bodek static void
4559b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
4569b8d05b8SZbigniew Bodek {
4579b8d05b8SZbigniew Bodek 	int i;
4589b8d05b8SZbigniew Bodek 
4599b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
4609b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
4619b8d05b8SZbigniew Bodek 
4629b8d05b8SZbigniew Bodek }
4639b8d05b8SZbigniew Bodek 
4649b8d05b8SZbigniew Bodek static int
4659b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
4669b8d05b8SZbigniew Bodek {
4679b8d05b8SZbigniew Bodek 	int ret;
4689b8d05b8SZbigniew Bodek 
4699b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
4709b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
4719b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
4728a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
4738a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
4749b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
4759b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
4768a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
4779b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
4789b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
4799b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
4809b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
4819b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
4829b8d05b8SZbigniew Bodek 
4839b8d05b8SZbigniew Bodek 	return (ret);
4849b8d05b8SZbigniew Bodek }
4859b8d05b8SZbigniew Bodek 
4869b8d05b8SZbigniew Bodek static int
4879b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
4889b8d05b8SZbigniew Bodek {
4899b8d05b8SZbigniew Bodek 	int ret;
4909b8d05b8SZbigniew Bodek 
4919b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
4929b8d05b8SZbigniew Bodek 
4933f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
4949b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
4959b8d05b8SZbigniew Bodek 
4969b8d05b8SZbigniew Bodek 	return (ret);
4979b8d05b8SZbigniew Bodek }
4989b8d05b8SZbigniew Bodek 
4999b8d05b8SZbigniew Bodek static int
5009b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5019b8d05b8SZbigniew Bodek {
5029b8d05b8SZbigniew Bodek 	int ret;
5039b8d05b8SZbigniew Bodek 
5049b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5059b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5069b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5078a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5088a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5099b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5109b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsize 		     */
5114727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
5129b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsegsize 	     */
5139b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5149b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5159b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5169b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5179b8d05b8SZbigniew Bodek 
5189b8d05b8SZbigniew Bodek 	return (ret);
5199b8d05b8SZbigniew Bodek }
5209b8d05b8SZbigniew Bodek 
5219b8d05b8SZbigniew Bodek static int
5229b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5239b8d05b8SZbigniew Bodek {
5249b8d05b8SZbigniew Bodek 	int ret;
5259b8d05b8SZbigniew Bodek 
5269b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5279b8d05b8SZbigniew Bodek 
5283f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5299b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5309b8d05b8SZbigniew Bodek 
5319b8d05b8SZbigniew Bodek 	return (ret);
5329b8d05b8SZbigniew Bodek }
5339b8d05b8SZbigniew Bodek 
5349b8d05b8SZbigniew Bodek /**
5359b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
5369b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
5379b8d05b8SZbigniew Bodek  * @qid: queue index
5389b8d05b8SZbigniew Bodek  *
5399b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
5409b8d05b8SZbigniew Bodek  **/
5419b8d05b8SZbigniew Bodek static int
5429b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
5439b8d05b8SZbigniew Bodek {
5449b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
5459b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
5469b8d05b8SZbigniew Bodek 	int size, i, err;
5479b8d05b8SZbigniew Bodek 
5489b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
5499b8d05b8SZbigniew Bodek 
5509b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5513f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
5527d2544e6SMarcin Wojtas 		return (ENOMEM);
5539b8d05b8SZbigniew Bodek 
5549b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
5559b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5563f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
5577d2544e6SMarcin Wojtas 		goto err_buf_info_free;
5589b8d05b8SZbigniew Bodek 
5594fa9e02dSMarcin Wojtas 	size = tx_ring->tx_max_header_size;
5604fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = malloc(size, M_DEVBUF,
5614fa9e02dSMarcin Wojtas 	    M_NOWAIT | M_ZERO);
5624fa9e02dSMarcin Wojtas 	if (unlikely(tx_ring->push_buf_intermediate_buf == NULL))
5634fa9e02dSMarcin Wojtas 		goto err_tx_ids_free;
5644fa9e02dSMarcin Wojtas 
5659b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
5669b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
5679b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
5689b8d05b8SZbigniew Bodek 
5699b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
5709b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
5719b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
5729b8d05b8SZbigniew Bodek 
5739b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
5749b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
575af66d7d0SMarcin Wojtas 	tx_ring->acum_pkts = 0;
5769b8d05b8SZbigniew Bodek 
5779b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
578b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
5799b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
580b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
5819b8d05b8SZbigniew Bodek 
5829b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
5839b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
5849b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
5854fa9e02dSMarcin Wojtas 		    &tx_ring->tx_buffer_info[i].map_head);
5863f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
5874e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
5884fa9e02dSMarcin Wojtas 			    "Unable to create Tx DMA map_head for buffer %d\n",
5894fa9e02dSMarcin Wojtas 			    i);
5907d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
5919b8d05b8SZbigniew Bodek 		}
5924fa9e02dSMarcin Wojtas 		tx_ring->tx_buffer_info[i].seg_mapped = false;
5934fa9e02dSMarcin Wojtas 
5944fa9e02dSMarcin Wojtas 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
5954fa9e02dSMarcin Wojtas 		    &tx_ring->tx_buffer_info[i].map_seg);
5964fa9e02dSMarcin Wojtas 		if (unlikely(err != 0)) {
5974fa9e02dSMarcin Wojtas 			ena_trace(ENA_ALERT,
5984fa9e02dSMarcin Wojtas 			    "Unable to create Tx DMA map_seg for buffer %d\n",
5994fa9e02dSMarcin Wojtas 			    i);
6004fa9e02dSMarcin Wojtas 			goto err_buf_info_head_unmap;
6014fa9e02dSMarcin Wojtas 		}
6024fa9e02dSMarcin Wojtas 		tx_ring->tx_buffer_info[i].head_mapped = false;
6039b8d05b8SZbigniew Bodek 	}
6049b8d05b8SZbigniew Bodek 
6059b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6069b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6079b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6089b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6093f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
6104e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT,
6119b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6129b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6137d2544e6SMarcin Wojtas 		goto err_buf_info_unmap;
6149b8d05b8SZbigniew Bodek 	}
6159b8d05b8SZbigniew Bodek 
6165cb9db07SMarcin Wojtas 	tx_ring->running = true;
6175cb9db07SMarcin Wojtas 
6189b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
6199b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
6209b8d05b8SZbigniew Bodek 
6219b8d05b8SZbigniew Bodek 	return (0);
6229b8d05b8SZbigniew Bodek 
6234fa9e02dSMarcin Wojtas err_buf_info_head_unmap:
6244fa9e02dSMarcin Wojtas 	bus_dmamap_destroy(adapter->tx_buf_tag,
6254fa9e02dSMarcin Wojtas 	    tx_ring->tx_buffer_info[i].map_head);
6267d2544e6SMarcin Wojtas err_buf_info_unmap:
6279b8d05b8SZbigniew Bodek 	while (i--) {
6289b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6294fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_head);
6304fa9e02dSMarcin Wojtas 		bus_dmamap_destroy(adapter->tx_buf_tag,
6314fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_seg);
6329b8d05b8SZbigniew Bodek 	}
6334fa9e02dSMarcin Wojtas 	free(tx_ring->push_buf_intermediate_buf, M_DEVBUF);
6344fa9e02dSMarcin Wojtas err_tx_ids_free:
635cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6367d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
6377d2544e6SMarcin Wojtas err_buf_info_free:
638cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6397d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
6407d2544e6SMarcin Wojtas 
6419b8d05b8SZbigniew Bodek 	return (ENOMEM);
6429b8d05b8SZbigniew Bodek }
6439b8d05b8SZbigniew Bodek 
6449b8d05b8SZbigniew Bodek /**
6459b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
6469b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6479b8d05b8SZbigniew Bodek  * @qid: queue index
6489b8d05b8SZbigniew Bodek  *
6499b8d05b8SZbigniew Bodek  * Free all transmit software resources
6509b8d05b8SZbigniew Bodek  **/
6519b8d05b8SZbigniew Bodek static void
6529b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
6539b8d05b8SZbigniew Bodek {
6549b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
6559b8d05b8SZbigniew Bodek 
6569b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
6579b8d05b8SZbigniew Bodek 	    NULL))
6589b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
6599b8d05b8SZbigniew Bodek 
6609b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
6619b8d05b8SZbigniew Bodek 
662b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6639b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
6649b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
6659b8d05b8SZbigniew Bodek 
6669b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
6679b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
6684fa9e02dSMarcin Wojtas 		if (tx_ring->tx_buffer_info[i].head_mapped == true) {
669e8073738SMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag,
6704fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_head,
6714fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
6729b8d05b8SZbigniew Bodek 			bus_dmamap_unload(adapter->tx_buf_tag,
6734fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_head);
6744fa9e02dSMarcin Wojtas 			tx_ring->tx_buffer_info[i].head_mapped = false;
6754fa9e02dSMarcin Wojtas 		}
6769b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6774fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_head);
6784fa9e02dSMarcin Wojtas 
6794fa9e02dSMarcin Wojtas 		if (tx_ring->tx_buffer_info[i].seg_mapped == true) {
6804fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag,
6814fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_seg,
6824fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
6834fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
6844fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_seg);
6854fa9e02dSMarcin Wojtas 			tx_ring->tx_buffer_info[i].seg_mapped = false;
6864fa9e02dSMarcin Wojtas 		}
6874fa9e02dSMarcin Wojtas 		bus_dmamap_destroy(adapter->tx_buf_tag,
6884fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_seg);
6894fa9e02dSMarcin Wojtas 
690e8073738SMarcin Wojtas 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
691e8073738SMarcin Wojtas 		tx_ring->tx_buffer_info[i].mbuf = NULL;
6929b8d05b8SZbigniew Bodek 	}
693416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6949b8d05b8SZbigniew Bodek 
6959b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
696cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6979b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
6989b8d05b8SZbigniew Bodek 
699cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7009b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
7014fa9e02dSMarcin Wojtas 
7024fa9e02dSMarcin Wojtas 	ENA_MEM_FREE(adapter->ena_dev->dmadev,
7034fa9e02dSMarcin Wojtas 	    tx_ring->push_buf_intermediate_buf);
7044fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = NULL;
7059b8d05b8SZbigniew Bodek }
7069b8d05b8SZbigniew Bodek 
7079b8d05b8SZbigniew Bodek /**
7089b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7099b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7109b8d05b8SZbigniew Bodek  *
7119b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7129b8d05b8SZbigniew Bodek  **/
7139b8d05b8SZbigniew Bodek static int
7149b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7159b8d05b8SZbigniew Bodek {
7169b8d05b8SZbigniew Bodek 	int i, rc;
7179b8d05b8SZbigniew Bodek 
7189b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
7199b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
7200bdffe59SMarcin Wojtas 		if (rc != 0) {
7219b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
7229b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
7239b8d05b8SZbigniew Bodek 			goto err_setup_tx;
7249b8d05b8SZbigniew Bodek 		}
7257d2544e6SMarcin Wojtas 	}
7269b8d05b8SZbigniew Bodek 
7279b8d05b8SZbigniew Bodek 	return (0);
7289b8d05b8SZbigniew Bodek 
7299b8d05b8SZbigniew Bodek err_setup_tx:
7309b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
7319b8d05b8SZbigniew Bodek 	while (i--)
7329b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7339b8d05b8SZbigniew Bodek 	return (rc);
7349b8d05b8SZbigniew Bodek }
7359b8d05b8SZbigniew Bodek 
7369b8d05b8SZbigniew Bodek /**
7379b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
7389b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7399b8d05b8SZbigniew Bodek  *
7409b8d05b8SZbigniew Bodek  * Free all transmit software resources
7419b8d05b8SZbigniew Bodek  **/
7429b8d05b8SZbigniew Bodek static void
7439b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
7449b8d05b8SZbigniew Bodek {
7459b8d05b8SZbigniew Bodek 	int i;
7469b8d05b8SZbigniew Bodek 
7479b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
7489b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7499b8d05b8SZbigniew Bodek }
7509b8d05b8SZbigniew Bodek 
7519b8d05b8SZbigniew Bodek /**
7529b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
7539b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7549b8d05b8SZbigniew Bodek  * @qid: queue index
7559b8d05b8SZbigniew Bodek  *
7569b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7579b8d05b8SZbigniew Bodek  **/
7589b8d05b8SZbigniew Bodek static int
7599b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
7609b8d05b8SZbigniew Bodek {
7619b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
7629b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
7639b8d05b8SZbigniew Bodek 	int size, err, i;
7649b8d05b8SZbigniew Bodek 
7659b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
7669b8d05b8SZbigniew Bodek 
767*9a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
768*9a0f2079SMarcin Wojtas 	ena_netmap_reset_rx_ring(adapter, qid);
769*9a0f2079SMarcin Wojtas 	rx_ring->initialized = false;
770*9a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
771*9a0f2079SMarcin Wojtas 
7729b8d05b8SZbigniew Bodek 	/*
7739b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
7749b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
7759b8d05b8SZbigniew Bodek 	 */
7769b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
7779b8d05b8SZbigniew Bodek 
778cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
7799b8d05b8SZbigniew Bodek 
78043fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
78143fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
78243fefd16SMarcin Wojtas 
78343fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
78443fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
78543fefd16SMarcin Wojtas 
7869b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
7879b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
7889b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
7899b8d05b8SZbigniew Bodek 
7909b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
7919b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
7929b8d05b8SZbigniew Bodek 
7939b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
7949b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
7959b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
7969b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
7979b8d05b8SZbigniew Bodek 		if (err != 0) {
7984e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
7999b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8007d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
8019b8d05b8SZbigniew Bodek 		}
8029b8d05b8SZbigniew Bodek 	}
8039b8d05b8SZbigniew Bodek 
8049b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8050bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8069b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8070bdffe59SMarcin Wojtas 		if (err != 0) {
8089b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8099b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8109b8d05b8SZbigniew Bodek 		} else {
8119b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8129b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8139b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8149b8d05b8SZbigniew Bodek 		}
8159b8d05b8SZbigniew Bodek 	}
8169b8d05b8SZbigniew Bodek 
8179b8d05b8SZbigniew Bodek 	return (0);
8189b8d05b8SZbigniew Bodek 
8197d2544e6SMarcin Wojtas err_buf_info_unmap:
8209b8d05b8SZbigniew Bodek 	while (i--) {
8219b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8229b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8239b8d05b8SZbigniew Bodek 	}
8249b8d05b8SZbigniew Bodek 
82543fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
82643fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
827cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8289b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8299b8d05b8SZbigniew Bodek 	return (ENOMEM);
8309b8d05b8SZbigniew Bodek }
8319b8d05b8SZbigniew Bodek 
8329b8d05b8SZbigniew Bodek /**
8339b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
8349b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8359b8d05b8SZbigniew Bodek  * @qid: queue index
8369b8d05b8SZbigniew Bodek  *
8379b8d05b8SZbigniew Bodek  * Free all receive software resources
8389b8d05b8SZbigniew Bodek  **/
8399b8d05b8SZbigniew Bodek static void
8409b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8419b8d05b8SZbigniew Bodek {
8429b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
8439b8d05b8SZbigniew Bodek 
8449b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
8459b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
846e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag,
847e8073738SMarcin Wojtas 		    rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD);
8489b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
8499b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
8509b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
8519b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8529b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8539b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8549b8d05b8SZbigniew Bodek 	}
8559b8d05b8SZbigniew Bodek 
8569b8d05b8SZbigniew Bodek 	/* free LRO resources, */
8579b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
8589b8d05b8SZbigniew Bodek 
8599b8d05b8SZbigniew Bodek 	/* free allocated memory */
860cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8619b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8629b8d05b8SZbigniew Bodek 
86343fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
86443fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
8659b8d05b8SZbigniew Bodek }
8669b8d05b8SZbigniew Bodek 
8679b8d05b8SZbigniew Bodek /**
8689b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
8699b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8709b8d05b8SZbigniew Bodek  *
8719b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8729b8d05b8SZbigniew Bodek  **/
8739b8d05b8SZbigniew Bodek static int
8749b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
8759b8d05b8SZbigniew Bodek {
8769b8d05b8SZbigniew Bodek 	int i, rc = 0;
8779b8d05b8SZbigniew Bodek 
8789b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
8799b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
8800bdffe59SMarcin Wojtas 		if (rc != 0) {
8819b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8829b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
8839b8d05b8SZbigniew Bodek 			goto err_setup_rx;
8849b8d05b8SZbigniew Bodek 		}
8857d2544e6SMarcin Wojtas 	}
8869b8d05b8SZbigniew Bodek 	return (0);
8879b8d05b8SZbigniew Bodek 
8889b8d05b8SZbigniew Bodek err_setup_rx:
8899b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
8909b8d05b8SZbigniew Bodek 	while (i--)
8919b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
8929b8d05b8SZbigniew Bodek 	return (rc);
8939b8d05b8SZbigniew Bodek }
8949b8d05b8SZbigniew Bodek 
8959b8d05b8SZbigniew Bodek /**
8969b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
8979b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8989b8d05b8SZbigniew Bodek  *
8999b8d05b8SZbigniew Bodek  * Free all receive software resources
9009b8d05b8SZbigniew Bodek  **/
9019b8d05b8SZbigniew Bodek static void
9029b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9039b8d05b8SZbigniew Bodek {
9049b8d05b8SZbigniew Bodek 	int i;
9059b8d05b8SZbigniew Bodek 
9069b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
9079b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9089b8d05b8SZbigniew Bodek }
9099b8d05b8SZbigniew Bodek 
9109b8d05b8SZbigniew Bodek static inline int
9119b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9129b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9139b8d05b8SZbigniew Bodek {
9149b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9159b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9169b8d05b8SZbigniew Bodek 	int nsegs, error;
9174727bda6SMarcin Wojtas 	int mlen;
9189b8d05b8SZbigniew Bodek 
9199b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
9203f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
9219b8d05b8SZbigniew Bodek 		return (0);
9229b8d05b8SZbigniew Bodek 
9239b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
9249b8d05b8SZbigniew Bodek 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
9259b8d05b8SZbigniew Bodek 
9263f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
9274727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
9284727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9294727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
9309b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
9319b8d05b8SZbigniew Bodek 			return (ENOMEM);
9329b8d05b8SZbigniew Bodek 		}
9334727bda6SMarcin Wojtas 		mlen = MCLBYTES;
9344727bda6SMarcin Wojtas 	} else {
9354727bda6SMarcin Wojtas 		mlen = MJUM16BYTES;
9364727bda6SMarcin Wojtas 	}
9379b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
9384727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
9399b8d05b8SZbigniew Bodek 
9409b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
9419b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
94230425f93SMarcin Wojtas 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d\n",
9439b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
9449b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
9459b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
9463f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
9474e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
9489b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
9499b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
9509b8d05b8SZbigniew Bodek 		goto exit;
9519b8d05b8SZbigniew Bodek 
9529b8d05b8SZbigniew Bodek 	}
9539b8d05b8SZbigniew Bodek 
9549b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
9559b8d05b8SZbigniew Bodek 
9569b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
9579b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
9584727bda6SMarcin Wojtas 	ena_buf->len = mlen;
9599b8d05b8SZbigniew Bodek 
9609b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9619b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
9629b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
9639b8d05b8SZbigniew Bodek 
9649b8d05b8SZbigniew Bodek 	return (0);
9659b8d05b8SZbigniew Bodek 
9669b8d05b8SZbigniew Bodek exit:
9679b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9689b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9699b8d05b8SZbigniew Bodek 	return (EFAULT);
9709b8d05b8SZbigniew Bodek }
9719b8d05b8SZbigniew Bodek 
9729b8d05b8SZbigniew Bodek static void
9739b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
9749b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
9759b8d05b8SZbigniew Bodek {
9769b8d05b8SZbigniew Bodek 
9774e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
9784e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
9799b8d05b8SZbigniew Bodek 		return;
9804e8acd84SMarcin Wojtas 	}
9819b8d05b8SZbigniew Bodek 
982e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
983e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
9849b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
9859b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9869b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9879b8d05b8SZbigniew Bodek }
9889b8d05b8SZbigniew Bodek 
9899b8d05b8SZbigniew Bodek /**
9909b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
9919b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
9929b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
9939b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
9949b8d05b8SZbigniew Bodek  **/
99538c7b965SMarcin Wojtas int
9969b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
9979b8d05b8SZbigniew Bodek {
9989b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
99943fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
10009b8d05b8SZbigniew Bodek 	uint32_t i;
10019b8d05b8SZbigniew Bodek 	int rc;
10029b8d05b8SZbigniew Bodek 
100330425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d\n",
10049b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10059b8d05b8SZbigniew Bodek 
10069b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10079b8d05b8SZbigniew Bodek 
10089b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
100943fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
101043fefd16SMarcin Wojtas 
10119b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
101230425f93SMarcin Wojtas 		    "RX buffer - next to use: %d\n", next_to_use);
10139b8d05b8SZbigniew Bodek 
101443fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
101543fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
1016*9a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
1017*9a0f2079SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP)
1018*9a0f2079SMarcin Wojtas 			rc = ena_netmap_alloc_rx_slot(adapter, rx_ring, rx_info);
1019*9a0f2079SMarcin Wojtas 		else
1020*9a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
10219b8d05b8SZbigniew Bodek 			rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
10223f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
10234e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10244e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
10254e8acd84SMarcin Wojtas 			    rx_ring->qid);
10269b8d05b8SZbigniew Bodek 			break;
10279b8d05b8SZbigniew Bodek 		}
10289b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
102943fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
10300bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
10314e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10329b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
10339b8d05b8SZbigniew Bodek 			    rx_ring->qid);
10349b8d05b8SZbigniew Bodek 			break;
10359b8d05b8SZbigniew Bodek 		}
10369b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
10379b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
10389b8d05b8SZbigniew Bodek 	}
10399b8d05b8SZbigniew Bodek 
10403f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
10419b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
10424e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING,
10434e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
10444e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
10459b8d05b8SZbigniew Bodek 	}
10469b8d05b8SZbigniew Bodek 
10473f9ed7abSMarcin Wojtas 	if (likely(i != 0)) {
10489b8d05b8SZbigniew Bodek 		wmb();
10499b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
10509b8d05b8SZbigniew Bodek 	}
10519b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
10529b8d05b8SZbigniew Bodek 	return (i);
10539b8d05b8SZbigniew Bodek }
10549b8d05b8SZbigniew Bodek 
10559b8d05b8SZbigniew Bodek static void
10569b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
10579b8d05b8SZbigniew Bodek {
10589b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
10599b8d05b8SZbigniew Bodek 	unsigned int i;
10609b8d05b8SZbigniew Bodek 
10619b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
10629b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
10639b8d05b8SZbigniew Bodek 
10640bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
10659b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
1066*9a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
1067*9a0f2079SMarcin Wojtas 		if (((if_getflags(adapter->ifp) & IFF_DYING) == 0) &&
1068*9a0f2079SMarcin Wojtas 		    (adapter->ifp->if_capenable & IFCAP_NETMAP)) {
1069*9a0f2079SMarcin Wojtas 			if (rx_info->netmap_buf_idx != 0)
1070*9a0f2079SMarcin Wojtas 				ena_netmap_free_rx_slot(adapter, rx_ring,
1071*9a0f2079SMarcin Wojtas 				    rx_info);
1072*9a0f2079SMarcin Wojtas 		}
1073*9a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
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 		if (unlikely(rc != bufs_num))
10934e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
10944e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
1095*9a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
1096*9a0f2079SMarcin Wojtas 		rx_ring->initialized = true;
1097*9a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
10989b8d05b8SZbigniew Bodek 	}
10999b8d05b8SZbigniew Bodek }
11009b8d05b8SZbigniew Bodek 
11019b8d05b8SZbigniew Bodek static void
11029b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
11039b8d05b8SZbigniew Bodek {
11049b8d05b8SZbigniew Bodek 	int i;
11059b8d05b8SZbigniew Bodek 
11069b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
11079b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
11089b8d05b8SZbigniew Bodek }
11099b8d05b8SZbigniew Bodek 
11109b8d05b8SZbigniew Bodek /**
11119b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
11129b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
11139b8d05b8SZbigniew Bodek  * @qid: queue index
11149b8d05b8SZbigniew Bodek  **/
11159b8d05b8SZbigniew Bodek static void
11169b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
11179b8d05b8SZbigniew Bodek {
11184e8acd84SMarcin Wojtas 	bool print_once = true;
11199b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
11209b8d05b8SZbigniew Bodek 
1121416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
11229b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
11239b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
11249b8d05b8SZbigniew Bodek 
11259b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
11269b8d05b8SZbigniew Bodek 			continue;
11279b8d05b8SZbigniew Bodek 
11284e8acd84SMarcin Wojtas 		if (print_once) {
11294e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
113030425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
11314e8acd84SMarcin Wojtas 			    qid, i);
11324e8acd84SMarcin Wojtas 			print_once = false;
11334e8acd84SMarcin Wojtas 		} else {
11344e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG,
113530425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
11364e8acd84SMarcin Wojtas 			     qid, i);
11374e8acd84SMarcin Wojtas 		}
11389b8d05b8SZbigniew Bodek 
11394fa9e02dSMarcin Wojtas 		if (tx_info->head_mapped == true) {
11404fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_head,
1141e8073738SMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
11424fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
11434fa9e02dSMarcin Wojtas 			    tx_info->map_head);
11444fa9e02dSMarcin Wojtas 			tx_info->head_mapped = false;
11454fa9e02dSMarcin Wojtas 		}
11464fa9e02dSMarcin Wojtas 
11474fa9e02dSMarcin Wojtas 		if (tx_info->seg_mapped == true) {
11484fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_seg,
11494fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
11504fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
11514fa9e02dSMarcin Wojtas 			    tx_info->map_seg);
11524fa9e02dSMarcin Wojtas 			tx_info->seg_mapped = false;
11534fa9e02dSMarcin Wojtas 		}
11544fa9e02dSMarcin Wojtas 
11559b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
11569b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
11579b8d05b8SZbigniew Bodek 	}
1158416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
11599b8d05b8SZbigniew Bodek }
11609b8d05b8SZbigniew Bodek 
11619b8d05b8SZbigniew Bodek static void
11629b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
11639b8d05b8SZbigniew Bodek {
11649b8d05b8SZbigniew Bodek 
11659b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++)
11669b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
11679b8d05b8SZbigniew Bodek }
11689b8d05b8SZbigniew Bodek 
11699b8d05b8SZbigniew Bodek static void
11709b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
11719b8d05b8SZbigniew Bodek {
11729b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11739b8d05b8SZbigniew Bodek 	int i;
11749b8d05b8SZbigniew Bodek 
11759b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11769b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11779b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11789b8d05b8SZbigniew Bodek 	}
11799b8d05b8SZbigniew Bodek }
11809b8d05b8SZbigniew Bodek 
11819b8d05b8SZbigniew Bodek static void
11829b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
11839b8d05b8SZbigniew Bodek {
11849b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11859b8d05b8SZbigniew Bodek 	int i;
11869b8d05b8SZbigniew Bodek 
11879b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11889b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
11899b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11909b8d05b8SZbigniew Bodek 	}
11919b8d05b8SZbigniew Bodek }
11929b8d05b8SZbigniew Bodek 
11939b8d05b8SZbigniew Bodek static void
11949b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
11959b8d05b8SZbigniew Bodek {
11965cb9db07SMarcin Wojtas 	struct ena_que *queue;
11975cb9db07SMarcin Wojtas 	int i;
11985cb9db07SMarcin Wojtas 
11995cb9db07SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
12005cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
12015cb9db07SMarcin Wojtas 		while (taskqueue_cancel(queue->cleanup_tq,
12025cb9db07SMarcin Wojtas 		    &queue->cleanup_task, NULL))
12035cb9db07SMarcin Wojtas 			taskqueue_drain(queue->cleanup_tq,
12045cb9db07SMarcin Wojtas 			    &queue->cleanup_task);
12055cb9db07SMarcin Wojtas 		taskqueue_free(queue->cleanup_tq);
12065cb9db07SMarcin Wojtas 	}
12075cb9db07SMarcin Wojtas 
12089b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
12099b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
12109b8d05b8SZbigniew Bodek }
12119b8d05b8SZbigniew Bodek 
12129b8d05b8SZbigniew Bodek static int
12139b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
12149b8d05b8SZbigniew Bodek {
12159b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
12169b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
12179b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
12185cb9db07SMarcin Wojtas 	struct ena_que *queue;
12199b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12209b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
12219b8d05b8SZbigniew Bodek 	int rc, i;
12229b8d05b8SZbigniew Bodek 
12239b8d05b8SZbigniew Bodek 	/* Create TX queues */
12249b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12259b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12269b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
12279b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
12289b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
12299b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
12309b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12319b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12329b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12330bdffe59SMarcin Wojtas 		if (rc != 0) {
12349b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12359b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
12369b8d05b8SZbigniew Bodek 			goto err_tx;
12379b8d05b8SZbigniew Bodek 		}
12389b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
12399b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12409b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12419b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12420bdffe59SMarcin Wojtas 		if (rc != 0) {
12439b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12449b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
12459b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12469b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12479b8d05b8SZbigniew Bodek 			goto err_tx;
12489b8d05b8SZbigniew Bodek 		}
12499b8d05b8SZbigniew Bodek 	}
12509b8d05b8SZbigniew Bodek 
12519b8d05b8SZbigniew Bodek 	/* Create RX queues */
12529b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12539b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12549b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12559b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
12569b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
12579b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
12589b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12599b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12609b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12613f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12629b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12639b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
12649b8d05b8SZbigniew Bodek 			goto err_rx;
12659b8d05b8SZbigniew Bodek 		}
12669b8d05b8SZbigniew Bodek 
12679b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
12689b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12699b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12709b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12713f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12729b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12739b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
12749b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12759b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12769b8d05b8SZbigniew Bodek 			goto err_rx;
12779b8d05b8SZbigniew Bodek 		}
12789b8d05b8SZbigniew Bodek 	}
12799b8d05b8SZbigniew Bodek 
12805cb9db07SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
12815cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
12825cb9db07SMarcin Wojtas 
12835cb9db07SMarcin Wojtas 		TASK_INIT(&queue->cleanup_task, 0, ena_cleanup, queue);
12845cb9db07SMarcin Wojtas 		queue->cleanup_tq = taskqueue_create_fast("ena cleanup",
12855cb9db07SMarcin Wojtas 		    M_WAITOK, taskqueue_thread_enqueue, &queue->cleanup_tq);
12865cb9db07SMarcin Wojtas 
12875cb9db07SMarcin Wojtas 		taskqueue_start_threads(&queue->cleanup_tq, 1, PI_NET,
12885cb9db07SMarcin Wojtas 		    "%s queue %d cleanup",
12895cb9db07SMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
12905cb9db07SMarcin Wojtas 	}
12915cb9db07SMarcin Wojtas 
12929b8d05b8SZbigniew Bodek 	return (0);
12939b8d05b8SZbigniew Bodek 
12949b8d05b8SZbigniew Bodek err_rx:
12959b8d05b8SZbigniew Bodek 	while (i--)
12969b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
12979b8d05b8SZbigniew Bodek 	i = adapter->num_queues;
12989b8d05b8SZbigniew Bodek err_tx:
12999b8d05b8SZbigniew Bodek 	while (i--)
13009b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
13019b8d05b8SZbigniew Bodek 
13029b8d05b8SZbigniew Bodek 	return (ENXIO);
13039b8d05b8SZbigniew Bodek }
13049b8d05b8SZbigniew Bodek 
13059b8d05b8SZbigniew Bodek /*********************************************************************
13069b8d05b8SZbigniew Bodek  *
13079b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
13089b8d05b8SZbigniew Bodek  *
13099b8d05b8SZbigniew Bodek  **********************************************************************/
13109b8d05b8SZbigniew Bodek 
13119b8d05b8SZbigniew Bodek /**
13129b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
13139b8d05b8SZbigniew Bodek  * @arg: interrupt number
13149b8d05b8SZbigniew Bodek  **/
13159b8d05b8SZbigniew Bodek static void
13169b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
13179b8d05b8SZbigniew Bodek {
13189b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
13199b8d05b8SZbigniew Bodek 
13209b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1321fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)))
13229b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
13239b8d05b8SZbigniew Bodek }
13249b8d05b8SZbigniew Bodek 
13255cb9db07SMarcin Wojtas /**
13265cb9db07SMarcin Wojtas  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
13275cb9db07SMarcin Wojtas  * @arg: queue
13285cb9db07SMarcin Wojtas  **/
13295cb9db07SMarcin Wojtas static int
13305cb9db07SMarcin Wojtas ena_handle_msix(void *arg)
13315cb9db07SMarcin Wojtas {
13325cb9db07SMarcin Wojtas 	struct ena_que *queue = arg;
13335cb9db07SMarcin Wojtas 	struct ena_adapter *adapter = queue->adapter;
13345cb9db07SMarcin Wojtas 	if_t ifp = adapter->ifp;
13355cb9db07SMarcin Wojtas 
13365cb9db07SMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
13375cb9db07SMarcin Wojtas 		return (FILTER_STRAY);
13385cb9db07SMarcin Wojtas 
13395cb9db07SMarcin Wojtas 	taskqueue_enqueue(queue->cleanup_tq, &queue->cleanup_task);
13405cb9db07SMarcin Wojtas 
13415cb9db07SMarcin Wojtas 	return (FILTER_HANDLED);
13425cb9db07SMarcin Wojtas }
13435cb9db07SMarcin Wojtas 
13449b8d05b8SZbigniew Bodek static int
13459b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
13469b8d05b8SZbigniew Bodek {
13479b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
13488805021aSMarcin Wojtas 	int msix_vecs, msix_req;
13498805021aSMarcin Wojtas 	int i, rc = 0;
13509b8d05b8SZbigniew Bodek 
1351fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1352fd43fd2aSMarcin Wojtas 		device_printf(dev, "Error, MSI-X is already enabled\n");
1353fd43fd2aSMarcin Wojtas 		return (EINVAL);
1354fd43fd2aSMarcin Wojtas 	}
1355fd43fd2aSMarcin Wojtas 
13569b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
13579b8d05b8SZbigniew Bodek 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues);
13589b8d05b8SZbigniew Bodek 
1359cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1360cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1361cd5d5804SMarcin Wojtas 
136230425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d\n", msix_vecs);
13639b8d05b8SZbigniew Bodek 
13649b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
13659b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
13669b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
13679b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
13689b8d05b8SZbigniew Bodek 	}
13699b8d05b8SZbigniew Bodek 
13708805021aSMarcin Wojtas 	msix_req = msix_vecs;
13719b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
13723f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
13739b8d05b8SZbigniew Bodek 		device_printf(dev,
13749b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
13757d2544e6SMarcin Wojtas 
13769b8d05b8SZbigniew Bodek 		rc = ENOSPC;
13777d2544e6SMarcin Wojtas 		goto err_msix_free;
13789b8d05b8SZbigniew Bodek 	}
13799b8d05b8SZbigniew Bodek 
13808805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
13812b5b60feSMarcin Wojtas 		if (msix_vecs == ENA_ADMIN_MSIX_VEC) {
13822b5b60feSMarcin Wojtas 			device_printf(dev,
13832b5b60feSMarcin Wojtas 			    "Not enough number of MSI-x allocated: %d\n",
13842b5b60feSMarcin Wojtas 			    msix_vecs);
13852b5b60feSMarcin Wojtas 			pci_release_msi(dev);
13862b5b60feSMarcin Wojtas 			rc = ENOSPC;
13872b5b60feSMarcin Wojtas 			goto err_msix_free;
13882b5b60feSMarcin Wojtas 		}
13898805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
13908805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
13918805021aSMarcin Wojtas 		adapter->num_queues = msix_vecs - ENA_ADMIN_MSIX_VEC;
13928805021aSMarcin Wojtas 	}
13938805021aSMarcin Wojtas 
13949b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
1395fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
13969b8d05b8SZbigniew Bodek 
13977d2544e6SMarcin Wojtas 	return (0);
13987d2544e6SMarcin Wojtas 
13997d2544e6SMarcin Wojtas err_msix_free:
14007d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
14017d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
14027d2544e6SMarcin Wojtas 
14039b8d05b8SZbigniew Bodek 	return (rc);
14049b8d05b8SZbigniew Bodek }
14059b8d05b8SZbigniew Bodek 
14069b8d05b8SZbigniew Bodek static void
14079b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
14089b8d05b8SZbigniew Bodek {
14099b8d05b8SZbigniew Bodek 
14109b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
14119b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
14129b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
14139b8d05b8SZbigniew Bodek 	/*
14149b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
14159b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
14169b8d05b8SZbigniew Bodek 	 */
14179b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
14189b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
14199b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
14209b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
14219b8d05b8SZbigniew Bodek }
14229b8d05b8SZbigniew Bodek 
142377958fcdSMarcin Wojtas static int
14249b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
14259b8d05b8SZbigniew Bodek {
14269b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
14279b8d05b8SZbigniew Bodek 	int irq_idx;
14289b8d05b8SZbigniew Bodek 
142977958fcdSMarcin Wojtas 	if (adapter->msix_entries == NULL)
143077958fcdSMarcin Wojtas 		return (EINVAL);
143177958fcdSMarcin Wojtas 
14329b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++) {
14339b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
14349b8d05b8SZbigniew Bodek 
14359b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
14369b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
14379b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
14389b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
14399b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
14409b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
14419b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
14429b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
1443277f11c4SMarcin Wojtas 
14449b8d05b8SZbigniew Bodek 		/*
1445277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
14469b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
14479b8d05b8SZbigniew Bodek 		 */
14483f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
14499b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
14509b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
14519b8d05b8SZbigniew Bodek 		    last_bind_cpu;
14529b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
14539b8d05b8SZbigniew Bodek 	}
145477958fcdSMarcin Wojtas 
145577958fcdSMarcin Wojtas 	return (0);
14569b8d05b8SZbigniew Bodek }
14579b8d05b8SZbigniew Bodek 
14589b8d05b8SZbigniew Bodek static int
14599b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
14609b8d05b8SZbigniew Bodek {
14619b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
14629b8d05b8SZbigniew Bodek 	unsigned long flags;
14639b8d05b8SZbigniew Bodek 	int rc, rcc;
14649b8d05b8SZbigniew Bodek 
14659b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
14669b8d05b8SZbigniew Bodek 
14679b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
14689b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
14699b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
14709b8d05b8SZbigniew Bodek 
14713f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
14729b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
14739b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
14747d2544e6SMarcin Wojtas 		return (ENXIO);
14759b8d05b8SZbigniew Bodek 	}
14769b8d05b8SZbigniew Bodek 
14770bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
14780bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
14790bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
14803f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
14819b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
14829b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
14839b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
14847d2544e6SMarcin Wojtas 		goto err_res_free;
14859b8d05b8SZbigniew Bodek 	}
14869b8d05b8SZbigniew Bodek 	irq->requested = true;
14879b8d05b8SZbigniew Bodek 
14889b8d05b8SZbigniew Bodek 	return (rc);
14899b8d05b8SZbigniew Bodek 
14907d2544e6SMarcin Wojtas err_res_free:
14914e8acd84SMarcin Wojtas 	ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
14927d2544e6SMarcin Wojtas 	    irq->vector);
14939b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
14949b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
14953f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
14969b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
14979b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
14989b8d05b8SZbigniew Bodek 	irq->res = NULL;
14999b8d05b8SZbigniew Bodek 
15009b8d05b8SZbigniew Bodek 	return (rc);
15019b8d05b8SZbigniew Bodek }
15029b8d05b8SZbigniew Bodek 
15039b8d05b8SZbigniew Bodek static int
15049b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
15059b8d05b8SZbigniew Bodek {
15069b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
15079b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
15089b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
15099b8d05b8SZbigniew Bodek 
1510fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter))) {
15114e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
15124e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
15139b8d05b8SZbigniew Bodek 		return (EINVAL);
15149b8d05b8SZbigniew Bodek 	} else {
15159b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
15169b8d05b8SZbigniew Bodek 	}
15179b8d05b8SZbigniew Bodek 
15189b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
15199b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
15209b8d05b8SZbigniew Bodek 
15213f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
15229b8d05b8SZbigniew Bodek 			continue;
15239b8d05b8SZbigniew Bodek 
15249b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
15259b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
15263f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
1527469a8407SMarcin Wojtas 			rc = ENOMEM;
15289b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
15299b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
15309b8d05b8SZbigniew Bodek 			goto err;
15319b8d05b8SZbigniew Bodek 		}
15329b8d05b8SZbigniew Bodek 
15330bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
15345cb9db07SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, irq->handler, NULL,
15355cb9db07SMarcin Wojtas 		    irq->data, &irq->cookie);
15363f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
15379b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
15389b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
15399b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
15409b8d05b8SZbigniew Bodek 			goto err;
15419b8d05b8SZbigniew Bodek 		}
15429b8d05b8SZbigniew Bodek 		irq->requested = true;
15439b8d05b8SZbigniew Bodek 
15444e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
15459b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
15469b8d05b8SZbigniew Bodek 	}
15479b8d05b8SZbigniew Bodek 
15489b8d05b8SZbigniew Bodek 	return (rc);
15499b8d05b8SZbigniew Bodek 
15509b8d05b8SZbigniew Bodek err:
15519b8d05b8SZbigniew Bodek 
15529b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
15539b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
15549b8d05b8SZbigniew Bodek 		rcc = 0;
15559b8d05b8SZbigniew Bodek 
15569b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
15579b8d05b8SZbigniew Bodek 		   free both intr and resources */
15580bdffe59SMarcin Wojtas 		if (irq->requested)
15599b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
15603f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
15619b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
15629b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
15639b8d05b8SZbigniew Bodek 
15649b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
15659b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
15669b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
15679b8d05b8SZbigniew Bodek 		   this iteration */
15689b8d05b8SZbigniew Bodek 		rcc = 0;
15699b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
15709b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
15719b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
15729b8d05b8SZbigniew Bodek 		}
15733f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
15749b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
15759b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
15769b8d05b8SZbigniew Bodek 		irq->requested = false;
15779b8d05b8SZbigniew Bodek 		irq->res = NULL;
15789b8d05b8SZbigniew Bodek 	}
15799b8d05b8SZbigniew Bodek 
15809b8d05b8SZbigniew Bodek 	return (rc);
15819b8d05b8SZbigniew Bodek }
15829b8d05b8SZbigniew Bodek 
15839b8d05b8SZbigniew Bodek static void
15849b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
15859b8d05b8SZbigniew Bodek {
15869b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
15879b8d05b8SZbigniew Bodek 	int rc;
15889b8d05b8SZbigniew Bodek 
15899b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
15909b8d05b8SZbigniew Bodek 	if (irq->requested) {
15919b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
15929b8d05b8SZbigniew Bodek 		    irq->vector);
15939b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
15943f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
15959b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
15969b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
15979b8d05b8SZbigniew Bodek 		irq->requested = 0;
15989b8d05b8SZbigniew Bodek 	}
15999b8d05b8SZbigniew Bodek 
16009b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
16019b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
16029b8d05b8SZbigniew Bodek 		    irq->vector);
16039b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
16049b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
16059b8d05b8SZbigniew Bodek 		irq->res = NULL;
16063f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
16079b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
16089b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
16099b8d05b8SZbigniew Bodek 	}
16109b8d05b8SZbigniew Bodek }
16119b8d05b8SZbigniew Bodek 
16129b8d05b8SZbigniew Bodek static void
16139b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
16149b8d05b8SZbigniew Bodek {
16159b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
16169b8d05b8SZbigniew Bodek 	int rc;
16179b8d05b8SZbigniew Bodek 
16189b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
16199b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
16209b8d05b8SZbigniew Bodek 		if (irq->requested) {
16219b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
16229b8d05b8SZbigniew Bodek 			    irq->vector);
16239b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
16249b8d05b8SZbigniew Bodek 			    irq->cookie);
16253f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
16269b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
16279b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
16289b8d05b8SZbigniew Bodek 			}
16299b8d05b8SZbigniew Bodek 			irq->requested = 0;
16309b8d05b8SZbigniew Bodek 		}
16319b8d05b8SZbigniew Bodek 
16329b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
16339b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
16349b8d05b8SZbigniew Bodek 			    irq->vector);
16359b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
16369b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
16379b8d05b8SZbigniew Bodek 			irq->res = NULL;
16383f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
16399b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
16409b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
16419b8d05b8SZbigniew Bodek 				    irq->vector);
16429b8d05b8SZbigniew Bodek 			}
16439b8d05b8SZbigniew Bodek 		}
16449b8d05b8SZbigniew Bodek 	}
16459b8d05b8SZbigniew Bodek }
16469b8d05b8SZbigniew Bodek 
16479b8d05b8SZbigniew Bodek static void
16489b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
16499b8d05b8SZbigniew Bodek {
16509b8d05b8SZbigniew Bodek 
16519b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
16529b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
16539b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
16549b8d05b8SZbigniew Bodek }
16559b8d05b8SZbigniew Bodek 
16569b8d05b8SZbigniew Bodek static void
16579b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
16589b8d05b8SZbigniew Bodek {
16599b8d05b8SZbigniew Bodek 
1660fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1661fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
16629b8d05b8SZbigniew Bodek 		pci_release_msi(adapter->pdev);
1663fd43fd2aSMarcin Wojtas 	}
16649b8d05b8SZbigniew Bodek 
16659b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
1666fd43fd2aSMarcin Wojtas 	if (adapter->msix_entries != NULL)
1667cd5d5804SMarcin Wojtas 		free(adapter->msix_entries, M_DEVBUF);
16689b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
16699b8d05b8SZbigniew Bodek }
16709b8d05b8SZbigniew Bodek 
16719b8d05b8SZbigniew Bodek static void
16729b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
16739b8d05b8SZbigniew Bodek {
16749b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
16759b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
16769b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
16779b8d05b8SZbigniew Bodek 	int i;
16789b8d05b8SZbigniew Bodek 
16799b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
16809b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
16819b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
16829b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
16839b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
16849b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
16859b8d05b8SZbigniew Bodek 	}
16869b8d05b8SZbigniew Bodek }
16879b8d05b8SZbigniew Bodek 
16889b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
16890bdffe59SMarcin Wojtas static int
16900bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
16919b8d05b8SZbigniew Bodek {
16929b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
16939b8d05b8SZbigniew Bodek 	int rc;
16949b8d05b8SZbigniew Bodek 
16959b8d05b8SZbigniew Bodek 	/* Set indirect table */
16969b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
16970bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
16980bdffe59SMarcin Wojtas 		return (rc);
16999b8d05b8SZbigniew Bodek 
17009b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
17019b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
17020bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
17030bdffe59SMarcin Wojtas 		return (rc);
17049b8d05b8SZbigniew Bodek 
17059b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
17069b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
17070bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
17080bdffe59SMarcin Wojtas 		return (rc);
17099b8d05b8SZbigniew Bodek 
17100bdffe59SMarcin Wojtas 	return (0);
17119b8d05b8SZbigniew Bodek }
17129b8d05b8SZbigniew Bodek 
17139b8d05b8SZbigniew Bodek static int
17149b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
17159b8d05b8SZbigniew Bodek {
17169b8d05b8SZbigniew Bodek 	int rc;
17179b8d05b8SZbigniew Bodek 
1718fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) {
17199b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
17200bdffe59SMarcin Wojtas 		if (rc != 0)
17219b8d05b8SZbigniew Bodek 			return (rc);
17229b8d05b8SZbigniew Bodek 	}
17239b8d05b8SZbigniew Bodek 
17247d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
17253f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
17267d2544e6SMarcin Wojtas 		return (rc);
17277d2544e6SMarcin Wojtas 
17289b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
172930217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
173030217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
17319b8d05b8SZbigniew Bodek 
17329b8d05b8SZbigniew Bodek 	return (0);
17339b8d05b8SZbigniew Bodek }
17349b8d05b8SZbigniew Bodek 
173538c7b965SMarcin Wojtas int
17369b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
17379b8d05b8SZbigniew Bodek {
17389b8d05b8SZbigniew Bodek 	int rc = 0;
17399b8d05b8SZbigniew Bodek 
17403f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
17419b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
17429b8d05b8SZbigniew Bodek 		return (ENXIO);
17439b8d05b8SZbigniew Bodek 	}
17449b8d05b8SZbigniew Bodek 
1745fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
17469b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going UP\n");
17479b8d05b8SZbigniew Bodek 
17489b8d05b8SZbigniew Bodek 		/* setup interrupts for IO queues */
174977958fcdSMarcin Wojtas 		rc = ena_setup_io_intr(adapter);
175077958fcdSMarcin Wojtas 		if (unlikely(rc != 0)) {
175177958fcdSMarcin Wojtas 			ena_trace(ENA_ALERT, "error setting up IO interrupt\n");
175277958fcdSMarcin Wojtas 			goto error;
175377958fcdSMarcin Wojtas 		}
17549b8d05b8SZbigniew Bodek 		rc = ena_request_io_irq(adapter);
17553f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
175630425f93SMarcin Wojtas 			ena_trace(ENA_ALERT, "err_req_irq\n");
175777958fcdSMarcin Wojtas 			goto error;
17589b8d05b8SZbigniew Bodek 		}
17599b8d05b8SZbigniew Bodek 
17609b8d05b8SZbigniew Bodek 		/* allocate transmit descriptors */
17619b8d05b8SZbigniew Bodek 		rc = ena_setup_all_tx_resources(adapter);
17623f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
176330425f93SMarcin Wojtas 			ena_trace(ENA_ALERT, "err_setup_tx\n");
17649b8d05b8SZbigniew Bodek 			goto err_setup_tx;
17659b8d05b8SZbigniew Bodek 		}
17669b8d05b8SZbigniew Bodek 
17679b8d05b8SZbigniew Bodek 		/* allocate receive descriptors */
17689b8d05b8SZbigniew Bodek 		rc = ena_setup_all_rx_resources(adapter);
17693f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
177030425f93SMarcin Wojtas 			ena_trace(ENA_ALERT, "err_setup_rx\n");
17719b8d05b8SZbigniew Bodek 			goto err_setup_rx;
17729b8d05b8SZbigniew Bodek 		}
17739b8d05b8SZbigniew Bodek 
17749b8d05b8SZbigniew Bodek 		/* create IO queues for Rx & Tx */
17759b8d05b8SZbigniew Bodek 		rc = ena_create_io_queues(adapter);
17763f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
17779b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT,
177830425f93SMarcin Wojtas 			    "create IO queues failed\n");
17799b8d05b8SZbigniew Bodek 			goto err_io_que;
17809b8d05b8SZbigniew Bodek 		}
17819b8d05b8SZbigniew Bodek 
1782fd43fd2aSMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
17839b8d05b8SZbigniew Bodek 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
17849b8d05b8SZbigniew Bodek 
17859b8d05b8SZbigniew Bodek 		rc = ena_up_complete(adapter);
17863f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
17879b8d05b8SZbigniew Bodek 			goto err_up_complete;
17889b8d05b8SZbigniew Bodek 
17899b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_up, 1);
17909b8d05b8SZbigniew Bodek 
17919b8d05b8SZbigniew Bodek 		ena_update_hwassist(adapter);
17929b8d05b8SZbigniew Bodek 
17939b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
17949b8d05b8SZbigniew Bodek 		    IFF_DRV_OACTIVE);
17959b8d05b8SZbigniew Bodek 
179624392281SMarcin Wojtas 		/* Activate timer service only if the device is running.
179724392281SMarcin Wojtas 		 * If this flag is not set, it means that the driver is being
179824392281SMarcin Wojtas 		 * reset and timer service will be activated afterwards.
179924392281SMarcin Wojtas 		 */
180024392281SMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)) {
180124392281SMarcin Wojtas 			callout_reset_sbt(&adapter->timer_service, SBT_1S,
180224392281SMarcin Wojtas 			    SBT_1S, ena_timer_service, (void *)adapter, 0);
180324392281SMarcin Wojtas 		}
18049b8d05b8SZbigniew Bodek 
1805fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP, adapter);
180693471047SZbigniew Bodek 
180793471047SZbigniew Bodek 		ena_unmask_all_io_irqs(adapter);
18089b8d05b8SZbigniew Bodek 	}
18099b8d05b8SZbigniew Bodek 
18109b8d05b8SZbigniew Bodek 	return (0);
18119b8d05b8SZbigniew Bodek 
18129b8d05b8SZbigniew Bodek err_up_complete:
18139b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
18149b8d05b8SZbigniew Bodek err_io_que:
18159b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
18169b8d05b8SZbigniew Bodek err_setup_rx:
18179b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
18189b8d05b8SZbigniew Bodek err_setup_tx:
18199b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
182077958fcdSMarcin Wojtas error:
18219b8d05b8SZbigniew Bodek 	return (rc);
18229b8d05b8SZbigniew Bodek }
18239b8d05b8SZbigniew Bodek 
18249b8d05b8SZbigniew Bodek static uint64_t
18259b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
18269b8d05b8SZbigniew Bodek {
18279b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
18289b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
18299b8d05b8SZbigniew Bodek 
18309b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
18319b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
18329b8d05b8SZbigniew Bodek 
18339b8d05b8SZbigniew Bodek 	switch (cnt) {
18349b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
183530217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
18369b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
183730217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
18389b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
183930217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
18409b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
184130217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
18429b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
184330217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
18449b8d05b8SZbigniew Bodek 	default:
18459b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
18469b8d05b8SZbigniew Bodek 	}
18479b8d05b8SZbigniew Bodek }
18489b8d05b8SZbigniew Bodek 
18499b8d05b8SZbigniew Bodek static int
18509b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
18519b8d05b8SZbigniew Bodek {
18529b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
18539b8d05b8SZbigniew Bodek 	return (0);
18549b8d05b8SZbigniew Bodek }
18559b8d05b8SZbigniew Bodek 
18569b8d05b8SZbigniew Bodek static void
18579b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
18589b8d05b8SZbigniew Bodek {
18599b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
186030425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "enter\n");
18619b8d05b8SZbigniew Bodek 
18625a990212SMarcin Wojtas 	mtx_lock(&adapter->global_mtx);
18639b8d05b8SZbigniew Bodek 
18649b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
18659b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
18669b8d05b8SZbigniew Bodek 
1867fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) {
18685a990212SMarcin Wojtas 		mtx_unlock(&adapter->global_mtx);
1869fd43fd2aSMarcin Wojtas 		ena_trace(ENA_INFO, "Link is down\n");
18709b8d05b8SZbigniew Bodek 		return;
18719b8d05b8SZbigniew Bodek 	}
18729b8d05b8SZbigniew Bodek 
18739b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
1874b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
18759b8d05b8SZbigniew Bodek 
18765a990212SMarcin Wojtas 	mtx_unlock(&adapter->global_mtx);
18779b8d05b8SZbigniew Bodek }
18789b8d05b8SZbigniew Bodek 
18799b8d05b8SZbigniew Bodek static void
18809b8d05b8SZbigniew Bodek ena_init(void *arg)
18819b8d05b8SZbigniew Bodek {
18829b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
18839b8d05b8SZbigniew Bodek 
1884fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
18853d3a90f9SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
18869b8d05b8SZbigniew Bodek 		ena_up(adapter);
18873d3a90f9SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
18883d3a90f9SZbigniew Bodek 	}
18899b8d05b8SZbigniew Bodek }
18909b8d05b8SZbigniew Bodek 
18919b8d05b8SZbigniew Bodek static int
18929b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
18939b8d05b8SZbigniew Bodek {
18949b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
18959b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
18969b8d05b8SZbigniew Bodek 	int rc;
18979b8d05b8SZbigniew Bodek 
18989b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
18999b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
19009b8d05b8SZbigniew Bodek 
19019b8d05b8SZbigniew Bodek 	/*
19029b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
19039b8d05b8SZbigniew Bodek 	 */
19049b8d05b8SZbigniew Bodek 	rc = 0;
19059b8d05b8SZbigniew Bodek 	switch (command) {
19069b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
1907dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
1908dbf2eb54SMarcin Wojtas 			break;
1909e67c6554SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
19109b8d05b8SZbigniew Bodek 		ena_down(adapter);
19119b8d05b8SZbigniew Bodek 
19129b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
19139b8d05b8SZbigniew Bodek 
19149b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
1915e67c6554SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
19169b8d05b8SZbigniew Bodek 		break;
19179b8d05b8SZbigniew Bodek 
19189b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
19190bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
19200bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
19210bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
19220bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
19239b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
19249b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
19259b8d05b8SZbigniew Bodek 				}
19269b8d05b8SZbigniew Bodek 			} else {
1927e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
19289b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
1929e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
19309b8d05b8SZbigniew Bodek 			}
19319b8d05b8SZbigniew Bodek 		} else {
19320bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1933e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
19349b8d05b8SZbigniew Bodek 				ena_down(adapter);
1935e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
1936e67c6554SZbigniew Bodek 			}
19379b8d05b8SZbigniew Bodek 		}
19389b8d05b8SZbigniew Bodek 		break;
19399b8d05b8SZbigniew Bodek 
19409b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
19419b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
19429b8d05b8SZbigniew Bodek 		break;
19439b8d05b8SZbigniew Bodek 
19449b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
19459b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
19469b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
19479b8d05b8SZbigniew Bodek 		break;
19489b8d05b8SZbigniew Bodek 
19499b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
19509b8d05b8SZbigniew Bodek 		{
19519b8d05b8SZbigniew Bodek 			int reinit = 0;
19529b8d05b8SZbigniew Bodek 
19539b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
19549b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
19559b8d05b8SZbigniew Bodek 				reinit = 1;
19569b8d05b8SZbigniew Bodek 			}
19579b8d05b8SZbigniew Bodek 
19580bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
19590bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
1960e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
19619b8d05b8SZbigniew Bodek 				ena_down(adapter);
19629b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
1963e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
19649b8d05b8SZbigniew Bodek 			}
19659b8d05b8SZbigniew Bodek 		}
19669b8d05b8SZbigniew Bodek 
19679b8d05b8SZbigniew Bodek 		break;
19689b8d05b8SZbigniew Bodek 	default:
19699b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
19709b8d05b8SZbigniew Bodek 		break;
19719b8d05b8SZbigniew Bodek 	}
19729b8d05b8SZbigniew Bodek 
19739b8d05b8SZbigniew Bodek 	return (rc);
19749b8d05b8SZbigniew Bodek }
19759b8d05b8SZbigniew Bodek 
19769b8d05b8SZbigniew Bodek static int
19779b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
19789b8d05b8SZbigniew Bodek {
19799b8d05b8SZbigniew Bodek 	int caps = 0;
19809b8d05b8SZbigniew Bodek 
19810bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
19829b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
19839b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
19840bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
19859b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
19869b8d05b8SZbigniew Bodek 
19870bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
19889b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
19890bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
19909b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
19919b8d05b8SZbigniew Bodek 
19920bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
19930bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
19949b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
19959b8d05b8SZbigniew Bodek 
19960bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
19970bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
19989b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
19999b8d05b8SZbigniew Bodek 
20000bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
20019b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
20020bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
20039b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
20049b8d05b8SZbigniew Bodek 
20050bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
20060bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
20079b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
20089b8d05b8SZbigniew Bodek 
20099b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
20109b8d05b8SZbigniew Bodek 
20119b8d05b8SZbigniew Bodek 	return (caps);
20129b8d05b8SZbigniew Bodek }
20139b8d05b8SZbigniew Bodek 
20149b8d05b8SZbigniew Bodek static void
20159b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
20169b8d05b8SZbigniew Bodek {
20179b8d05b8SZbigniew Bodek 
20189b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
20199b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
20209b8d05b8SZbigniew Bodek }
20219b8d05b8SZbigniew Bodek 
20229b8d05b8SZbigniew Bodek static void
20239b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
20249b8d05b8SZbigniew Bodek {
20259b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
20269b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
20279b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
20289b8d05b8SZbigniew Bodek 	int flags = 0;
20299b8d05b8SZbigniew Bodek 
20309b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
20319b8d05b8SZbigniew Bodek 
20320bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
20330bdffe59SMarcin Wojtas 		if ((feat &
20340bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
20359b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
20360bdffe59SMarcin Wojtas 		if ((feat &
20379b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
20380bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
20399b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
20409b8d05b8SZbigniew Bodek 	}
20419b8d05b8SZbigniew Bodek 
20420bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
20439b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
20449b8d05b8SZbigniew Bodek 
20450bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
20469b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
20479b8d05b8SZbigniew Bodek 
20480bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
20499b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
20509b8d05b8SZbigniew Bodek 
20519b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
20529b8d05b8SZbigniew Bodek }
20539b8d05b8SZbigniew Bodek 
20549b8d05b8SZbigniew Bodek static int
20559b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
20569b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
20579b8d05b8SZbigniew Bodek {
20589b8d05b8SZbigniew Bodek 	if_t ifp;
20599b8d05b8SZbigniew Bodek 	int caps = 0;
20609b8d05b8SZbigniew Bodek 
20619b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
20623f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
20634e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
20649b8d05b8SZbigniew Bodek 		return (ENXIO);
20659b8d05b8SZbigniew Bodek 	}
20669b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
20679b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
20689b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
20699b8d05b8SZbigniew Bodek 
20709b8d05b8SZbigniew Bodek 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
20719b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
20729b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
20739b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
20749b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
20759b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
20769b8d05b8SZbigniew Bodek 
20779b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
20789b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
20799b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
20809b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
20819b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
20829b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
20839b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
20849b8d05b8SZbigniew Bodek 	/* check hardware support */
20859b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
20869b8d05b8SZbigniew Bodek 	/* ... and set them */
20879b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
20889b8d05b8SZbigniew Bodek 
20899b8d05b8SZbigniew Bodek 	/* TSO parameters */
20908a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
20918a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
20928a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
20938a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
20949b8d05b8SZbigniew Bodek 
20959b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
20969b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
20979b8d05b8SZbigniew Bodek 
20989b8d05b8SZbigniew Bodek 	/*
20999b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
21009b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
21019b8d05b8SZbigniew Bodek 	 */
21029b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
21039b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
21049b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
21059b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
21069b8d05b8SZbigniew Bodek 
21079b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
21089b8d05b8SZbigniew Bodek 
21099b8d05b8SZbigniew Bodek 	return (0);
21109b8d05b8SZbigniew Bodek }
21119b8d05b8SZbigniew Bodek 
211238c7b965SMarcin Wojtas void
21139b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
21149b8d05b8SZbigniew Bodek {
2115a195fab0SMarcin Wojtas 	int rc;
21169b8d05b8SZbigniew Bodek 
2117fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
21189b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going DOWN\n");
21199b8d05b8SZbigniew Bodek 
21209b8d05b8SZbigniew Bodek 		callout_drain(&adapter->timer_service);
21219b8d05b8SZbigniew Bodek 
2122fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP, adapter);
21239b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
21249b8d05b8SZbigniew Bodek 		    IFF_DRV_RUNNING);
21259b8d05b8SZbigniew Bodek 
21269b8d05b8SZbigniew Bodek 		ena_free_io_irq(adapter);
21279b8d05b8SZbigniew Bodek 
2128fd43fd2aSMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) {
2129a195fab0SMarcin Wojtas 			rc = ena_com_dev_reset(adapter->ena_dev,
2130a195fab0SMarcin Wojtas 			    adapter->reset_reason);
21313f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0))
2132a195fab0SMarcin Wojtas 				device_printf(adapter->pdev,
2133a195fab0SMarcin Wojtas 				    "Device reset failed\n");
2134a195fab0SMarcin Wojtas 		}
2135a195fab0SMarcin Wojtas 
21369b8d05b8SZbigniew Bodek 		ena_destroy_all_io_queues(adapter);
21379b8d05b8SZbigniew Bodek 
21389b8d05b8SZbigniew Bodek 		ena_free_all_tx_bufs(adapter);
21399b8d05b8SZbigniew Bodek 		ena_free_all_rx_bufs(adapter);
21409b8d05b8SZbigniew Bodek 		ena_free_all_tx_resources(adapter);
21419b8d05b8SZbigniew Bodek 		ena_free_all_rx_resources(adapter);
21429b8d05b8SZbigniew Bodek 
21439b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_down, 1);
21449b8d05b8SZbigniew Bodek 	}
21459b8d05b8SZbigniew Bodek }
21469b8d05b8SZbigniew Bodek 
21470bdffe59SMarcin Wojtas static int
21480bdffe59SMarcin Wojtas ena_calc_io_queue_num(struct ena_adapter *adapter,
21499b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
21509b8d05b8SZbigniew Bodek {
21516064f289SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
21524fa9e02dSMarcin Wojtas 	int io_tx_sq_num, io_tx_cq_num, io_rx_num, io_queue_num;
21539b8d05b8SZbigniew Bodek 
21546064f289SMarcin Wojtas 	/* Regular queues capabilities */
21556064f289SMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
21566064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
21576064f289SMarcin Wojtas 		    &get_feat_ctx->max_queue_ext.max_queue_ext;
21584fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, max_queue_ext->max_rx_sq_num,
21594fa9e02dSMarcin Wojtas 			max_queue_ext->max_rx_cq_num);
21606064f289SMarcin Wojtas 
21614fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queue_ext->max_tx_sq_num;
21624fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queue_ext->max_tx_cq_num;
21636064f289SMarcin Wojtas 	} else {
21646064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
21656064f289SMarcin Wojtas 		    &get_feat_ctx->max_queues;
21664fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queues->max_sq_num;
21674fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queues->max_cq_num;
21684fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, io_tx_sq_num, io_tx_cq_num);
21696064f289SMarcin Wojtas 	}
21709b8d05b8SZbigniew Bodek 
21714fa9e02dSMarcin Wojtas 	/* In case of LLQ use the llq fields for the tx SQ/CQ */
21724fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
21734fa9e02dSMarcin Wojtas 		io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
21744fa9e02dSMarcin Wojtas 
21759b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
21764fa9e02dSMarcin Wojtas 	io_queue_num = min_t(int, io_queue_num, io_rx_num);
21774fa9e02dSMarcin Wojtas 	io_queue_num = min_t(int, io_queue_num, io_tx_sq_num);
21784fa9e02dSMarcin Wojtas 	io_queue_num = min_t(int, io_queue_num, io_tx_cq_num);
21799b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
21809b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num,
21819b8d05b8SZbigniew Bodek 	    pci_msix_count(adapter->pdev) - 1);
21829b8d05b8SZbigniew Bodek 
21830bdffe59SMarcin Wojtas 	return (io_queue_num);
21849b8d05b8SZbigniew Bodek }
21859b8d05b8SZbigniew Bodek 
21860bdffe59SMarcin Wojtas static int
21874fa9e02dSMarcin Wojtas ena_enable_wc(struct resource *res)
21884fa9e02dSMarcin Wojtas {
2189472d4784SMarcin Wojtas #if defined(__i386) || defined(__amd64) || defined(__aarch64__)
21904fa9e02dSMarcin Wojtas 	vm_offset_t va;
21914fa9e02dSMarcin Wojtas 	vm_size_t len;
21924fa9e02dSMarcin Wojtas 	int rc;
21934fa9e02dSMarcin Wojtas 
21944fa9e02dSMarcin Wojtas 	va = (vm_offset_t)rman_get_virtual(res);
21954fa9e02dSMarcin Wojtas 	len = rman_get_size(res);
21964fa9e02dSMarcin Wojtas 	/* Enable write combining */
2197472d4784SMarcin Wojtas 	rc = pmap_change_attr(va, len, VM_MEMATTR_WRITE_COMBINING);
21984fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
21994fa9e02dSMarcin Wojtas 		ena_trace(ENA_ALERT, "pmap_change_attr failed, %d\n", rc);
22004fa9e02dSMarcin Wojtas 		return (rc);
22014fa9e02dSMarcin Wojtas 	}
22024fa9e02dSMarcin Wojtas 
22034fa9e02dSMarcin Wojtas 	return (0);
22044fa9e02dSMarcin Wojtas #endif
22054fa9e02dSMarcin Wojtas 	return (EOPNOTSUPP);
22064fa9e02dSMarcin Wojtas }
22074fa9e02dSMarcin Wojtas 
22084fa9e02dSMarcin Wojtas static int
22094fa9e02dSMarcin Wojtas ena_set_queues_placement_policy(device_t pdev, struct ena_com_dev *ena_dev,
22104fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *llq,
22114fa9e02dSMarcin Wojtas     struct ena_llq_configurations *llq_default_configurations)
22124fa9e02dSMarcin Wojtas {
22134fa9e02dSMarcin Wojtas 	struct ena_adapter *adapter = device_get_softc(pdev);
22144fa9e02dSMarcin Wojtas 	int rc, rid;
22154fa9e02dSMarcin Wojtas 	uint32_t llq_feature_mask;
22164fa9e02dSMarcin Wojtas 
22174fa9e02dSMarcin Wojtas 	llq_feature_mask = 1 << ENA_ADMIN_LLQ;
22184fa9e02dSMarcin Wojtas 	if (!(ena_dev->supported_features & llq_feature_mask)) {
22194fa9e02dSMarcin Wojtas 		device_printf(pdev,
22204fa9e02dSMarcin Wojtas 		    "LLQ is not supported. Fallback to host mode policy.\n");
22214fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
22224fa9e02dSMarcin Wojtas 		return (0);
22234fa9e02dSMarcin Wojtas 	}
22244fa9e02dSMarcin Wojtas 
22254fa9e02dSMarcin Wojtas 	rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
22264fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
22274fa9e02dSMarcin Wojtas 		device_printf(pdev, "Failed to configure the device mode. "
22284fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
22294fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
22304fa9e02dSMarcin Wojtas 		return (0);
22314fa9e02dSMarcin Wojtas 	}
22324fa9e02dSMarcin Wojtas 
22334fa9e02dSMarcin Wojtas 	/* Nothing to config, exit */
22344fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
22354fa9e02dSMarcin Wojtas 		return (0);
22364fa9e02dSMarcin Wojtas 
22374fa9e02dSMarcin Wojtas 	/* Try to allocate resources for LLQ bar */
22384fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_MEM_BAR);
22394fa9e02dSMarcin Wojtas 	adapter->memory = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
22404fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
22414fa9e02dSMarcin Wojtas 	if (unlikely(adapter->memory == NULL)) {
22424fa9e02dSMarcin Wojtas 		device_printf(pdev, "unable to allocate LLQ bar resource. "
22434fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
22444fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
22454fa9e02dSMarcin Wojtas 		return (0);
22464fa9e02dSMarcin Wojtas 	}
22474fa9e02dSMarcin Wojtas 
22484fa9e02dSMarcin Wojtas 	/* Enable write combining for better LLQ performance */
22494fa9e02dSMarcin Wojtas 	rc = ena_enable_wc(adapter->memory);
22504fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
22514fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to enable write combining.\n");
22524fa9e02dSMarcin Wojtas 		return (rc);
22534fa9e02dSMarcin Wojtas 	}
22544fa9e02dSMarcin Wojtas 
22554fa9e02dSMarcin Wojtas 	/*
22564fa9e02dSMarcin Wojtas 	 * Save virtual address of the device's memory region
22574fa9e02dSMarcin Wojtas 	 * for the ena_com layer.
22584fa9e02dSMarcin Wojtas 	 */
22594fa9e02dSMarcin Wojtas 	ena_dev->mem_bar = rman_get_virtual(adapter->memory);
22604fa9e02dSMarcin Wojtas 
22614fa9e02dSMarcin Wojtas 	return (0);
22624fa9e02dSMarcin Wojtas }
22634fa9e02dSMarcin Wojtas 
22644fa9e02dSMarcin Wojtas static inline
22654fa9e02dSMarcin Wojtas void set_default_llq_configurations(struct ena_llq_configurations *llq_config)
22664fa9e02dSMarcin Wojtas {
22674fa9e02dSMarcin Wojtas 	llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
22684fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
22694fa9e02dSMarcin Wojtas 	llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
22704fa9e02dSMarcin Wojtas 	llq_config->llq_num_decs_before_header =
22714fa9e02dSMarcin Wojtas 	    ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
22724fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size_value = 128;
22734fa9e02dSMarcin Wojtas }
22744fa9e02dSMarcin Wojtas 
22754fa9e02dSMarcin Wojtas static int
22766064f289SMarcin Wojtas ena_calc_queue_size(struct ena_adapter *adapter,
22776064f289SMarcin Wojtas     struct ena_calc_queue_size_ctx *ctx)
22789b8d05b8SZbigniew Bodek {
22794fa9e02dSMarcin Wojtas 	struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
22804fa9e02dSMarcin Wojtas 	struct ena_com_dev *ena_dev = ctx->ena_dev;
22816064f289SMarcin Wojtas 	uint32_t tx_queue_size = ENA_DEFAULT_RING_SIZE;
22826064f289SMarcin Wojtas 	uint32_t rx_queue_size = adapter->rx_ring_size;
22839b8d05b8SZbigniew Bodek 
22844fa9e02dSMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
22856064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
22866064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
22876064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
22886064f289SMarcin Wojtas 		    max_queue_ext->max_rx_cq_depth);
22896064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
22906064f289SMarcin Wojtas 		    max_queue_ext->max_rx_sq_depth);
22916064f289SMarcin Wojtas 		tx_queue_size = min_t(uint32_t, tx_queue_size,
22926064f289SMarcin Wojtas 		    max_queue_ext->max_tx_cq_depth);
22934fa9e02dSMarcin Wojtas 
22944fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
22954fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
22964fa9e02dSMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
22974fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
22984fa9e02dSMarcin Wojtas 		else
22996064f289SMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
23006064f289SMarcin Wojtas 			    max_queue_ext->max_tx_sq_depth);
23014fa9e02dSMarcin Wojtas 
23026064f289SMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
23036064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_rx_descs);
23046064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
23056064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_tx_descs);
23066064f289SMarcin Wojtas 	} else {
23076064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
23086064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queues;
23096064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
23106064f289SMarcin Wojtas 		    max_queues->max_cq_depth);
23116064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
23126064f289SMarcin Wojtas 		    max_queues->max_sq_depth);
23134fa9e02dSMarcin Wojtas 		tx_queue_size = min_t(uint32_t, tx_queue_size,
23144fa9e02dSMarcin Wojtas 		    max_queues->max_cq_depth);
23154fa9e02dSMarcin Wojtas 
23164fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
23174fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
23184fa9e02dSMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
23194fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
23204fa9e02dSMarcin Wojtas 		else
23214fa9e02dSMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
23224fa9e02dSMarcin Wojtas 			    max_queues->max_sq_depth);
23234fa9e02dSMarcin Wojtas 
23246064f289SMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
23256064f289SMarcin Wojtas 		    max_queues->max_packet_tx_descs);
23266064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
23276064f289SMarcin Wojtas 		    max_queues->max_packet_rx_descs);
23286064f289SMarcin Wojtas 	}
23299b8d05b8SZbigniew Bodek 
23309b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
23316064f289SMarcin Wojtas 	rx_queue_size = 1 << (fls(rx_queue_size) - 1);
23326064f289SMarcin Wojtas 	tx_queue_size = 1 << (fls(tx_queue_size) - 1);
23336064f289SMarcin Wojtas 
23346064f289SMarcin Wojtas 	if (unlikely(rx_queue_size == 0 || tx_queue_size == 0)) {
23356064f289SMarcin Wojtas 		device_printf(ctx->pdev, "Invalid queue size\n");
23366064f289SMarcin Wojtas 		return (EFAULT);
23379b8d05b8SZbigniew Bodek 	}
23389b8d05b8SZbigniew Bodek 
23396064f289SMarcin Wojtas 	ctx->rx_queue_size = rx_queue_size;
23406064f289SMarcin Wojtas 	ctx->tx_queue_size = tx_queue_size;
23416064f289SMarcin Wojtas 
23426064f289SMarcin Wojtas 	return (0);
23439b8d05b8SZbigniew Bodek }
23449b8d05b8SZbigniew Bodek 
23456064f289SMarcin Wojtas static int
23466064f289SMarcin Wojtas ena_handle_updated_queues(struct ena_adapter *adapter,
23476064f289SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
23486064f289SMarcin Wojtas {
23496064f289SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
23506064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
23516064f289SMarcin Wojtas 	device_t pdev = adapter->pdev;
23526064f289SMarcin Wojtas 	bool are_queues_changed = false;
23536064f289SMarcin Wojtas 	int io_queue_num, rc;
23549b8d05b8SZbigniew Bodek 
23556064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
23566064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = get_feat_ctx;
23576064f289SMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
23586064f289SMarcin Wojtas 
23596064f289SMarcin Wojtas 	io_queue_num = ena_calc_io_queue_num(adapter, get_feat_ctx);
23606064f289SMarcin Wojtas 	rc = ena_calc_queue_size(adapter, &calc_queue_ctx);
23616064f289SMarcin Wojtas 	if (unlikely(rc != 0 || io_queue_num <= 0))
23626064f289SMarcin Wojtas 		return EFAULT;
23636064f289SMarcin Wojtas 
23646064f289SMarcin Wojtas 	if (adapter->tx_ring->buf_ring_size != adapter->buf_ring_size)
23656064f289SMarcin Wojtas 		are_queues_changed = true;
23666064f289SMarcin Wojtas 
23676064f289SMarcin Wojtas 	if (unlikely(adapter->tx_ring_size > calc_queue_ctx.tx_queue_size ||
23686064f289SMarcin Wojtas 	    adapter->rx_ring_size > calc_queue_ctx.rx_queue_size)) {
23696064f289SMarcin Wojtas 		device_printf(pdev,
23706064f289SMarcin Wojtas 		    "Not enough resources to allocate requested queue sizes "
23716064f289SMarcin Wojtas 		    "(TX,RX)=(%d,%d), falling back to queue sizes "
23726064f289SMarcin Wojtas 		    "(TX,RX)=(%d,%d)\n",
23736064f289SMarcin Wojtas 		    adapter->tx_ring_size,
23746064f289SMarcin Wojtas 		    adapter->rx_ring_size,
23756064f289SMarcin Wojtas 		    calc_queue_ctx.tx_queue_size,
23766064f289SMarcin Wojtas 		    calc_queue_ctx.rx_queue_size);
23776064f289SMarcin Wojtas 		adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
23786064f289SMarcin Wojtas 		adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
23796064f289SMarcin Wojtas 		adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
23806064f289SMarcin Wojtas 		adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
23816064f289SMarcin Wojtas 		are_queues_changed = true;
23826064f289SMarcin Wojtas 	}
23836064f289SMarcin Wojtas 
23846064f289SMarcin Wojtas 	if (unlikely(adapter->num_queues > io_queue_num)) {
23856064f289SMarcin Wojtas 		device_printf(pdev,
23866064f289SMarcin Wojtas 		    "Not enough resources to allocate %d queues, "
23876064f289SMarcin Wojtas 		    "falling back to %d queues\n",
23886064f289SMarcin Wojtas 		    adapter->num_queues, io_queue_num);
23896064f289SMarcin Wojtas 		adapter->num_queues = io_queue_num;
2390fd43fd2aSMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter)) {
23916064f289SMarcin Wojtas 			ena_com_rss_destroy(ena_dev);
23926064f289SMarcin Wojtas 			rc = ena_rss_init_default(adapter);
23936064f289SMarcin Wojtas 			if (unlikely(rc != 0) && (rc != EOPNOTSUPP)) {
23946064f289SMarcin Wojtas 				device_printf(pdev, "Cannot init RSS rc: %d\n",
23956064f289SMarcin Wojtas 				    rc);
23966064f289SMarcin Wojtas 				return (rc);
23976064f289SMarcin Wojtas 			}
23986064f289SMarcin Wojtas 		}
23996064f289SMarcin Wojtas 		are_queues_changed = true;
24006064f289SMarcin Wojtas 	}
24016064f289SMarcin Wojtas 
24026064f289SMarcin Wojtas 	if (unlikely(are_queues_changed)) {
24036064f289SMarcin Wojtas 		ena_free_all_io_rings_resources(adapter);
24046064f289SMarcin Wojtas 		ena_init_io_rings(adapter);
24056064f289SMarcin Wojtas 	}
24066064f289SMarcin Wojtas 
24076064f289SMarcin Wojtas 	return (0);
24089b8d05b8SZbigniew Bodek }
24099b8d05b8SZbigniew Bodek 
24100bdffe59SMarcin Wojtas static int
24110bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
24129b8d05b8SZbigniew Bodek {
24139b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
24149b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
24159b8d05b8SZbigniew Bodek 	int qid, rc, i;
24169b8d05b8SZbigniew Bodek 
24179b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
24180bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
24194e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
24207d2544e6SMarcin Wojtas 		return (rc);
24219b8d05b8SZbigniew Bodek 	}
24229b8d05b8SZbigniew Bodek 
24239b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
24249b8d05b8SZbigniew Bodek 		qid = i % adapter->num_queues;
24259b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
24269b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
24270bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
24289b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
24297d2544e6SMarcin Wojtas 			goto err_rss_destroy;
24309b8d05b8SZbigniew Bodek 		}
24319b8d05b8SZbigniew Bodek 	}
24329b8d05b8SZbigniew Bodek 
24339b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
24349b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
24350bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
24369b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
24377d2544e6SMarcin Wojtas 		goto err_rss_destroy;
24389b8d05b8SZbigniew Bodek 	}
24399b8d05b8SZbigniew Bodek 
24409b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
24410bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
24429b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
24437d2544e6SMarcin Wojtas 		goto err_rss_destroy;
24449b8d05b8SZbigniew Bodek 	}
24459b8d05b8SZbigniew Bodek 
24469b8d05b8SZbigniew Bodek 	return (0);
24479b8d05b8SZbigniew Bodek 
24487d2544e6SMarcin Wojtas err_rss_destroy:
24499b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
24509b8d05b8SZbigniew Bodek 	return (rc);
24519b8d05b8SZbigniew Bodek }
24529b8d05b8SZbigniew Bodek 
24539b8d05b8SZbigniew Bodek static void
24549b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
24559b8d05b8SZbigniew Bodek {
24569b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
24579b8d05b8SZbigniew Bodek 	devclass_t dc;
24589b8d05b8SZbigniew Bodek 	int max;
24599b8d05b8SZbigniew Bodek 	int rc;
24609b8d05b8SZbigniew Bodek 
24619b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
24623f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
24634e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "No devclass ena\n");
24649b8d05b8SZbigniew Bodek 		return;
24659b8d05b8SZbigniew Bodek 	}
24669b8d05b8SZbigniew Bodek 
24679b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
24689b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
24699b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
24709b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
24719b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
2472fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
24733f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
24749b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
24759b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
24760bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
2477fd43fd2aSMarcin Wojtas 				ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
24789b8d05b8SZbigniew Bodek 			}
24799b8d05b8SZbigniew Bodek 		}
24809b8d05b8SZbigniew Bodek 	}
24819b8d05b8SZbigniew Bodek }
24829b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
24839b8d05b8SZbigniew Bodek 
24840bdffe59SMarcin Wojtas static void
248546021271SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev, device_t dev)
24869b8d05b8SZbigniew Bodek {
24879b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
248846021271SMarcin Wojtas 	uintptr_t rid;
24899b8d05b8SZbigniew Bodek 	int rc;
24909b8d05b8SZbigniew Bodek 
24919b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
24929b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
24933f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
24949b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
24959b8d05b8SZbigniew Bodek 		return;
24969b8d05b8SZbigniew Bodek 	}
24979b8d05b8SZbigniew Bodek 
24989b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
24999b8d05b8SZbigniew Bodek 
250046021271SMarcin Wojtas 	if (pci_get_id(dev, PCI_ID_RID, &rid) == 0)
250146021271SMarcin Wojtas 		host_info->bdf = rid;
25029b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
25039b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
25049b8d05b8SZbigniew Bodek 
25059b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
25069b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
25079b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
25089b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
25099b8d05b8SZbigniew Bodek 
25109b8d05b8SZbigniew Bodek 	host_info->driver_version =
25119b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
25129b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
25139b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
25148ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
25159b8d05b8SZbigniew Bodek 
25169b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
25173f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2518a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
25199b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
25209b8d05b8SZbigniew Bodek 		else
25219b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
25229b8d05b8SZbigniew Bodek 
25239b8d05b8SZbigniew Bodek 		goto err;
25249b8d05b8SZbigniew Bodek 	}
25259b8d05b8SZbigniew Bodek 
25269b8d05b8SZbigniew Bodek 	return;
25279b8d05b8SZbigniew Bodek 
25289b8d05b8SZbigniew Bodek err:
25299b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
25309b8d05b8SZbigniew Bodek }
25319b8d05b8SZbigniew Bodek 
25329b8d05b8SZbigniew Bodek static int
25339b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
25349b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
25359b8d05b8SZbigniew Bodek {
25369b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
25379b8d05b8SZbigniew Bodek 	bool readless_supported;
25389b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
25399b8d05b8SZbigniew Bodek 	int dma_width;
25409b8d05b8SZbigniew Bodek 	int rc;
25419b8d05b8SZbigniew Bodek 
25429b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
25433f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
25449b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
25450bdffe59SMarcin Wojtas 		return (rc);
25469b8d05b8SZbigniew Bodek 	}
25479b8d05b8SZbigniew Bodek 
25489b8d05b8SZbigniew Bodek 	/*
25499b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
25509b8d05b8SZbigniew Bodek 	 * read is disabled
25519b8d05b8SZbigniew Bodek 	 */
25529b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
25539b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
25549b8d05b8SZbigniew Bodek 
2555a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
25563f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
25579b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
25589b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
25599b8d05b8SZbigniew Bodek 	}
25609b8d05b8SZbigniew Bodek 
25619b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
25623f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
25639b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
25649b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
25659b8d05b8SZbigniew Bodek 	}
25669b8d05b8SZbigniew Bodek 
25679b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
25683f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
25699b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
25709b8d05b8SZbigniew Bodek 		rc = dma_width;
25719b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
25729b8d05b8SZbigniew Bodek 	}
25739b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
25749b8d05b8SZbigniew Bodek 
25759b8d05b8SZbigniew Bodek 	/* ENA admin level init */
257667ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
25773f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
25789b8d05b8SZbigniew Bodek 		device_printf(pdev,
25799b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
25809b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
25819b8d05b8SZbigniew Bodek 	}
25829b8d05b8SZbigniew Bodek 
25839b8d05b8SZbigniew Bodek 	/*
25849b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
25859b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
25869b8d05b8SZbigniew Bodek 	 * information
25879b8d05b8SZbigniew Bodek 	 */
25889b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
25899b8d05b8SZbigniew Bodek 
259046021271SMarcin Wojtas 	ena_config_host_info(ena_dev, pdev);
25919b8d05b8SZbigniew Bodek 
25929b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
25939b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
25943f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
25959b8d05b8SZbigniew Bodek 		device_printf(pdev,
25969b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
25979b8d05b8SZbigniew Bodek 		goto err_admin_init;
25989b8d05b8SZbigniew Bodek 	}
25999b8d05b8SZbigniew Bodek 
2600e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
2601e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
2602e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
260340621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
2604e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
26059b8d05b8SZbigniew Bodek 
26069b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
26079b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
26083f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
26099b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
26109b8d05b8SZbigniew Bodek 		goto err_admin_init;
26119b8d05b8SZbigniew Bodek 	}
26129b8d05b8SZbigniew Bodek 
26139b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
26149b8d05b8SZbigniew Bodek 
26150bdffe59SMarcin Wojtas 	return (0);
26169b8d05b8SZbigniew Bodek 
26179b8d05b8SZbigniew Bodek err_admin_init:
26189b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
26199b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
26209b8d05b8SZbigniew Bodek err_mmio_read_less:
26219b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
26229b8d05b8SZbigniew Bodek 
26230bdffe59SMarcin Wojtas 	return (rc);
26249b8d05b8SZbigniew Bodek }
26259b8d05b8SZbigniew Bodek 
26269b8d05b8SZbigniew Bodek static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
26279b8d05b8SZbigniew Bodek     int io_vectors)
26289b8d05b8SZbigniew Bodek {
26299b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
26309b8d05b8SZbigniew Bodek 	int rc;
26319b8d05b8SZbigniew Bodek 
26329b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
26333f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
26349b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
26350bdffe59SMarcin Wojtas 		return (rc);
26369b8d05b8SZbigniew Bodek 	}
26379b8d05b8SZbigniew Bodek 
26389b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
26399b8d05b8SZbigniew Bodek 
26409b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
26413f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
26429b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
26439b8d05b8SZbigniew Bodek 		goto err_disable_msix;
26449b8d05b8SZbigniew Bodek 	}
26459b8d05b8SZbigniew Bodek 
26469b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
26479b8d05b8SZbigniew Bodek 
26489b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
26499b8d05b8SZbigniew Bodek 
26500bdffe59SMarcin Wojtas 	return (0);
26519b8d05b8SZbigniew Bodek 
26529b8d05b8SZbigniew Bodek err_disable_msix:
26539b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
26549b8d05b8SZbigniew Bodek 
26550bdffe59SMarcin Wojtas 	return (rc);
26569b8d05b8SZbigniew Bodek }
26579b8d05b8SZbigniew Bodek 
26589b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
26599b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
26609b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
26619b8d05b8SZbigniew Bodek {
26629b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
266330217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
26649b8d05b8SZbigniew Bodek 	sbintime_t stime;
266530217e2dSMarcin Wojtas 	uint64_t rx_drops;
266630217e2dSMarcin Wojtas 
266730217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
266830217e2dSMarcin Wojtas 
266930217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
267030217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
267130217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
26729b8d05b8SZbigniew Bodek 
26739b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
26749b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
26759b8d05b8SZbigniew Bodek }
26769b8d05b8SZbigniew Bodek 
26779b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
26789b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
26799b8d05b8SZbigniew Bodek {
26809b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
26819b8d05b8SZbigniew Bodek 
26829b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
26839b8d05b8SZbigniew Bodek 		return;
26849b8d05b8SZbigniew Bodek 
268540621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
26869b8d05b8SZbigniew Bodek 		return;
26879b8d05b8SZbigniew Bodek 
26889b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
26899b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
26909b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
26919b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
26929b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
26939b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
2694858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
2695a195fab0SMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
2696fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
26979b8d05b8SZbigniew Bodek 		}
26989b8d05b8SZbigniew Bodek 	}
2699858659f7SMarcin Wojtas }
27009b8d05b8SZbigniew Bodek 
27019b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
27029b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
27039b8d05b8SZbigniew Bodek {
27040bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
27050bdffe59SMarcin Wojtas 	    false)) {
27069b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
27079b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
27089b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
2709858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
2710a195fab0SMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
2711fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
27129b8d05b8SZbigniew Bodek 		}
27139b8d05b8SZbigniew Bodek 	}
2714858659f7SMarcin Wojtas }
27159b8d05b8SZbigniew Bodek 
271674dba3adSMarcin Wojtas static int
2717d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
2718d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
2719d12f7bfcSMarcin Wojtas {
2720d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
2721d12f7bfcSMarcin Wojtas 		return (0);
2722d12f7bfcSMarcin Wojtas 
2723d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
2724d12f7bfcSMarcin Wojtas 		return (0);
2725d12f7bfcSMarcin Wojtas 
2726d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
2727d12f7bfcSMarcin Wojtas 
2728d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
2729d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
2730d12f7bfcSMarcin Wojtas 		    "Queue = %d. Reset the device\n", rx_ring->qid);
2731858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
2732d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
2733fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
2734858659f7SMarcin Wojtas 		}
2735d12f7bfcSMarcin Wojtas 		return (EIO);
2736d12f7bfcSMarcin Wojtas 	}
2737d12f7bfcSMarcin Wojtas 
2738d12f7bfcSMarcin Wojtas 	return (0);
2739d12f7bfcSMarcin Wojtas }
2740d12f7bfcSMarcin Wojtas 
2741d12f7bfcSMarcin Wojtas static int
2742d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
274374dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
274474dba3adSMarcin Wojtas {
274574dba3adSMarcin Wojtas 	struct bintime curtime, time;
274674dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
2747d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
274874dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
2749d12f7bfcSMarcin Wojtas 	int i, rc = 0;
275074dba3adSMarcin Wojtas 
275174dba3adSMarcin Wojtas 	getbinuptime(&curtime);
275274dba3adSMarcin Wojtas 
275374dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
275474dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
275574dba3adSMarcin Wojtas 
27560bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
275774dba3adSMarcin Wojtas 			continue;
275874dba3adSMarcin Wojtas 
275974dba3adSMarcin Wojtas 		time = curtime;
276074dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
2761d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
2762d12f7bfcSMarcin Wojtas 
2763d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
2764d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
2765d12f7bfcSMarcin Wojtas 			/*
2766d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
2767d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
2768d12f7bfcSMarcin Wojtas 			 */
2769d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
2770d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
2771d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
2772858659f7SMarcin Wojtas 			if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET,
2773858659f7SMarcin Wojtas 			    adapter))) {
2774858659f7SMarcin Wojtas 				adapter->reset_reason =
2775858659f7SMarcin Wojtas 				    ENA_REGS_RESET_MISS_INTERRUPT;
2776858659f7SMarcin Wojtas 				ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET,
2777858659f7SMarcin Wojtas 				    adapter);
2778858659f7SMarcin Wojtas 			}
2779d12f7bfcSMarcin Wojtas 			return (EIO);
2780d12f7bfcSMarcin Wojtas 		}
278174dba3adSMarcin Wojtas 
278274dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
2783d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
278474dba3adSMarcin Wojtas 
278574dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
278674dba3adSMarcin Wojtas 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
278774dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
278874dba3adSMarcin Wojtas 				    tx_ring->qid, i);
278974dba3adSMarcin Wojtas 
279074dba3adSMarcin Wojtas 			tx_buf->print_once = true;
279174dba3adSMarcin Wojtas 			missed_tx++;
2792d12f7bfcSMarcin Wojtas 		}
2793d12f7bfcSMarcin Wojtas 	}
279474dba3adSMarcin Wojtas 
2795d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
279674dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
2797d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
2798d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
27994e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
2800858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
2801d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_TX_CMPL;
2802fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
2803858659f7SMarcin Wojtas 		}
2804d12f7bfcSMarcin Wojtas 		rc = EIO;
280574dba3adSMarcin Wojtas 	}
280674dba3adSMarcin Wojtas 
2807d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
2808d12f7bfcSMarcin Wojtas 
2809d12f7bfcSMarcin Wojtas 	return (rc);
281074dba3adSMarcin Wojtas }
281174dba3adSMarcin Wojtas 
28129b8d05b8SZbigniew Bodek /*
28139b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
28149b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
28159b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
28169b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
28179b8d05b8SZbigniew Bodek  */
28180bdffe59SMarcin Wojtas static void
2819d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
28209b8d05b8SZbigniew Bodek {
28219b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
2822d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
282374dba3adSMarcin Wojtas 	int i, budget, rc;
28249b8d05b8SZbigniew Bodek 
28259b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
28269b8d05b8SZbigniew Bodek 	rmb();
28279b8d05b8SZbigniew Bodek 
2828fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
28299b8d05b8SZbigniew Bodek 		return;
28309b8d05b8SZbigniew Bodek 
2831fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
28329b8d05b8SZbigniew Bodek 		return;
28339b8d05b8SZbigniew Bodek 
283440621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
28359b8d05b8SZbigniew Bodek 		return;
28369b8d05b8SZbigniew Bodek 
28379b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
28389b8d05b8SZbigniew Bodek 
28399b8d05b8SZbigniew Bodek 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
28409b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
2841d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
28429b8d05b8SZbigniew Bodek 
2843d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
2844d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
2845d12f7bfcSMarcin Wojtas 			return;
2846d12f7bfcSMarcin Wojtas 
2847d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
28480bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
28499b8d05b8SZbigniew Bodek 			return;
28509b8d05b8SZbigniew Bodek 
28519b8d05b8SZbigniew Bodek 		budget--;
2852cd5d5804SMarcin Wojtas 		if (budget == 0) {
28539b8d05b8SZbigniew Bodek 			i++;
28549b8d05b8SZbigniew Bodek 			break;
28559b8d05b8SZbigniew Bodek 		}
28569b8d05b8SZbigniew Bodek 	}
28579b8d05b8SZbigniew Bodek 
28589b8d05b8SZbigniew Bodek 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
28599b8d05b8SZbigniew Bodek }
28609b8d05b8SZbigniew Bodek 
28615cb9db07SMarcin Wojtas /* trigger rx cleanup after 2 consecutive detections */
2862efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
2863efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
2864efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
2865efe6ab18SMarcin Wojtas  * for example).
2866efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
2867efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
2868efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
2869efe6ab18SMarcin Wojtas  * able to send new packets.
2870efe6ab18SMarcin Wojtas  *
2871efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
2872efe6ab18SMarcin Wojtas  */
2873efe6ab18SMarcin Wojtas static void
2874efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
2875efe6ab18SMarcin Wojtas {
2876efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
2877efe6ab18SMarcin Wojtas 	int i, refill_required;
2878efe6ab18SMarcin Wojtas 
2879fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
2880efe6ab18SMarcin Wojtas 		return;
2881efe6ab18SMarcin Wojtas 
2882fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
2883efe6ab18SMarcin Wojtas 		return;
2884efe6ab18SMarcin Wojtas 
2885efe6ab18SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
2886efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
2887efe6ab18SMarcin Wojtas 
2888efe6ab18SMarcin Wojtas 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
2889efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
2890efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
2891efe6ab18SMarcin Wojtas 
2892efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
2893efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
2894efe6ab18SMarcin Wojtas 				    1);
2895efe6ab18SMarcin Wojtas 
2896efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
2897efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
2898efe6ab18SMarcin Wojtas 
28995cb9db07SMarcin Wojtas 				taskqueue_enqueue(rx_ring->que->cleanup_tq,
29005cb9db07SMarcin Wojtas 				    &rx_ring->que->cleanup_task);
2901efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
2902efe6ab18SMarcin Wojtas 			}
2903efe6ab18SMarcin Wojtas 		} else {
2904efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
2905efe6ab18SMarcin Wojtas 		}
2906efe6ab18SMarcin Wojtas 	}
2907efe6ab18SMarcin Wojtas }
29089b8d05b8SZbigniew Bodek 
290940621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
291040621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
291140621d71SMarcin Wojtas {
291240621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
291340621d71SMarcin Wojtas 
291440621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
291540621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
291640621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
291740621d71SMarcin Wojtas 
291840621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
291940621d71SMarcin Wojtas 		/* convert to usec */
292040621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
292140621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
292240621d71SMarcin Wojtas 
292340621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
292440621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
292540621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
292640621d71SMarcin Wojtas 
292740621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
292840621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
292940621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
293040621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
293140621d71SMarcin Wojtas 		else
293240621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
293340621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
293440621d71SMarcin Wojtas 	}
293540621d71SMarcin Wojtas 
293640621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
293740621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
293840621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
293940621d71SMarcin Wojtas 		else
294040621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
294140621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
294240621d71SMarcin Wojtas 	}
294340621d71SMarcin Wojtas }
294440621d71SMarcin Wojtas 
29459b8d05b8SZbigniew Bodek static void
29469b8d05b8SZbigniew Bodek ena_timer_service(void *data)
29479b8d05b8SZbigniew Bodek {
29489b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
29499b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
29509b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
29519b8d05b8SZbigniew Bodek 
29529b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
29539b8d05b8SZbigniew Bodek 
29549b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
29559b8d05b8SZbigniew Bodek 
2956d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
29579b8d05b8SZbigniew Bodek 
2958efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
2959efe6ab18SMarcin Wojtas 
29600bdffe59SMarcin Wojtas 	if (host_info != NULL)
29619b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
29629b8d05b8SZbigniew Bodek 
2963fd43fd2aSMarcin Wojtas 	if (unlikely(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
29649b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
29659b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
29669b8d05b8SZbigniew Bodek 		return;
29679b8d05b8SZbigniew Bodek 	}
29689b8d05b8SZbigniew Bodek 
29699b8d05b8SZbigniew Bodek 	/*
29709b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
29719b8d05b8SZbigniew Bodek 	 */
29729b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
29739b8d05b8SZbigniew Bodek }
29749b8d05b8SZbigniew Bodek 
297538c7b965SMarcin Wojtas void
297632f63fa7SMarcin Wojtas ena_destroy_device(struct ena_adapter *adapter, bool graceful)
29779b8d05b8SZbigniew Bodek {
297832f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
29799b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
29809b8d05b8SZbigniew Bodek 	bool dev_up;
298132f63fa7SMarcin Wojtas 
298232f63fa7SMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter))
298332f63fa7SMarcin Wojtas 		return;
298432f63fa7SMarcin Wojtas 
298532f63fa7SMarcin Wojtas 	if_link_state_change(ifp, LINK_STATE_DOWN);
298632f63fa7SMarcin Wojtas 
298732f63fa7SMarcin Wojtas 	callout_drain(&adapter->timer_service);
298832f63fa7SMarcin Wojtas 
298932f63fa7SMarcin Wojtas 	dev_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
299032f63fa7SMarcin Wojtas 	if (dev_up)
299132f63fa7SMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
299232f63fa7SMarcin Wojtas 	else
299332f63fa7SMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
299432f63fa7SMarcin Wojtas 
299532f63fa7SMarcin Wojtas 	if (!graceful)
299632f63fa7SMarcin Wojtas 		ena_com_set_admin_running_state(ena_dev, false);
299732f63fa7SMarcin Wojtas 
299832f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
299932f63fa7SMarcin Wojtas 		ena_down(adapter);
300032f63fa7SMarcin Wojtas 
300132f63fa7SMarcin Wojtas 	/*
300232f63fa7SMarcin Wojtas 	 * Stop the device from sending AENQ events (if the device was up, and
300332f63fa7SMarcin Wojtas 	 * the trigger reset was on, ena_down already performs device reset)
300432f63fa7SMarcin Wojtas 	 */
300532f63fa7SMarcin Wojtas 	if (!(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter) && dev_up))
300632f63fa7SMarcin Wojtas 		ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
300732f63fa7SMarcin Wojtas 
300832f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
300932f63fa7SMarcin Wojtas 
301032f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
301132f63fa7SMarcin Wojtas 
301232f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
301332f63fa7SMarcin Wojtas 
301432f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
301532f63fa7SMarcin Wojtas 
301632f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
301732f63fa7SMarcin Wojtas 
301832f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
301932f63fa7SMarcin Wojtas 
302032f63fa7SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
302132f63fa7SMarcin Wojtas 
302232f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
302332f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
302432f63fa7SMarcin Wojtas }
302532f63fa7SMarcin Wojtas 
302632f63fa7SMarcin Wojtas static int
302732f63fa7SMarcin Wojtas ena_device_validate_params(struct ena_adapter *adapter,
302832f63fa7SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
302932f63fa7SMarcin Wojtas {
303032f63fa7SMarcin Wojtas 
303132f63fa7SMarcin Wojtas 	if (memcmp(get_feat_ctx->dev_attr.mac_addr, adapter->mac_addr,
303232f63fa7SMarcin Wojtas 	    ETHER_ADDR_LEN) != 0) {
303332f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
303432f63fa7SMarcin Wojtas 		    "Error, mac address are different\n");
303532f63fa7SMarcin Wojtas 		return (EINVAL);
303632f63fa7SMarcin Wojtas 	}
303732f63fa7SMarcin Wojtas 
303832f63fa7SMarcin Wojtas 	if (get_feat_ctx->dev_attr.max_mtu < if_getmtu(adapter->ifp)) {
303932f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
304032f63fa7SMarcin Wojtas 		    "Error, device max mtu is smaller than ifp MTU\n");
304132f63fa7SMarcin Wojtas 		return (EINVAL);
304232f63fa7SMarcin Wojtas 	}
304332f63fa7SMarcin Wojtas 
304432f63fa7SMarcin Wojtas 	return 0;
304532f63fa7SMarcin Wojtas }
304632f63fa7SMarcin Wojtas 
304738c7b965SMarcin Wojtas int
304832f63fa7SMarcin Wojtas ena_restore_device(struct ena_adapter *adapter)
304932f63fa7SMarcin Wojtas {
305032f63fa7SMarcin Wojtas 	struct ena_com_dev_get_features_ctx get_feat_ctx;
305132f63fa7SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
305232f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
305332f63fa7SMarcin Wojtas 	device_t dev = adapter->pdev;
305432f63fa7SMarcin Wojtas 	int wd_active;
30559b8d05b8SZbigniew Bodek 	int rc;
30569b8d05b8SZbigniew Bodek 
305732f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
305832f63fa7SMarcin Wojtas 
305932f63fa7SMarcin Wojtas 	rc = ena_device_init(adapter, dev, &get_feat_ctx, &wd_active);
306032f63fa7SMarcin Wojtas 	if (rc != 0) {
306132f63fa7SMarcin Wojtas 		device_printf(dev, "Cannot initialize device\n");
306232f63fa7SMarcin Wojtas 		goto err;
306332f63fa7SMarcin Wojtas 	}
306432f63fa7SMarcin Wojtas 	/*
306532f63fa7SMarcin Wojtas 	 * Only enable WD if it was enabled before reset, so it won't override
306632f63fa7SMarcin Wojtas 	 * value set by the user by the sysctl.
306732f63fa7SMarcin Wojtas 	 */
306832f63fa7SMarcin Wojtas 	if (adapter->wd_active != 0)
306932f63fa7SMarcin Wojtas 		adapter->wd_active = wd_active;
307032f63fa7SMarcin Wojtas 
307132f63fa7SMarcin Wojtas 	rc = ena_device_validate_params(adapter, &get_feat_ctx);
307232f63fa7SMarcin Wojtas 	if (rc != 0) {
307332f63fa7SMarcin Wojtas 		device_printf(dev, "Validation of device parameters failed\n");
307432f63fa7SMarcin Wojtas 		goto err_device_destroy;
307532f63fa7SMarcin Wojtas 	}
307632f63fa7SMarcin Wojtas 
307732f63fa7SMarcin Wojtas 	rc = ena_handle_updated_queues(adapter, &get_feat_ctx);
307832f63fa7SMarcin Wojtas 	if (rc != 0)
307932f63fa7SMarcin Wojtas 		goto err_device_destroy;
308032f63fa7SMarcin Wojtas 
308132f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
308232f63fa7SMarcin Wojtas 	/* Make sure we don't have a race with AENQ Links state handler */
308332f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
308432f63fa7SMarcin Wojtas 		if_link_state_change(ifp, LINK_STATE_UP);
308532f63fa7SMarcin Wojtas 
308632f63fa7SMarcin Wojtas 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
308732f63fa7SMarcin Wojtas 	    adapter->num_queues);
308832f63fa7SMarcin Wojtas 	if (rc != 0) {
308932f63fa7SMarcin Wojtas 		device_printf(dev, "Enable MSI-X failed\n");
309032f63fa7SMarcin Wojtas 		goto err_device_destroy;
309132f63fa7SMarcin Wojtas 	}
309232f63fa7SMarcin Wojtas 
309332f63fa7SMarcin Wojtas 	/* If the interface was up before the reset bring it up */
309432f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
309532f63fa7SMarcin Wojtas 		rc = ena_up(adapter);
309632f63fa7SMarcin Wojtas 		if (rc != 0) {
309732f63fa7SMarcin Wojtas 			device_printf(dev, "Failed to create I/O queues\n");
309832f63fa7SMarcin Wojtas 			goto err_disable_msix;
309932f63fa7SMarcin Wojtas 		}
310032f63fa7SMarcin Wojtas 	}
310132f63fa7SMarcin Wojtas 
310224392281SMarcin Wojtas 	/* Indicate that device is running again and ready to work */
310332f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
310424392281SMarcin Wojtas 
310524392281SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
310624392281SMarcin Wojtas 		/*
310724392281SMarcin Wojtas 		 * As the AENQ handlers weren't executed during reset because
310824392281SMarcin Wojtas 		 * the flag ENA_FLAG_DEVICE_RUNNING was turned off, the
310924392281SMarcin Wojtas 		 * timestamp must be updated again That will prevent next reset
311024392281SMarcin Wojtas 		 * caused by missing keep alive.
311124392281SMarcin Wojtas 		 */
311224392281SMarcin Wojtas 		adapter->keep_alive_timestamp = getsbinuptime();
311332f63fa7SMarcin Wojtas 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
311432f63fa7SMarcin Wojtas 		    ena_timer_service, (void *)adapter, 0);
311524392281SMarcin Wojtas 	}
311632f63fa7SMarcin Wojtas 
311732f63fa7SMarcin Wojtas 	device_printf(dev,
311832f63fa7SMarcin Wojtas 	    "Device reset completed successfully, Driver info: %s\n", ena_version);
311932f63fa7SMarcin Wojtas 
312032f63fa7SMarcin Wojtas 	return (rc);
312132f63fa7SMarcin Wojtas 
312232f63fa7SMarcin Wojtas err_disable_msix:
312332f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
312432f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
312532f63fa7SMarcin Wojtas err_device_destroy:
312632f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
312732f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
312832f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
312932f63fa7SMarcin Wojtas 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
313032f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
313132f63fa7SMarcin Wojtas err:
313232f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
313332f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
313432f63fa7SMarcin Wojtas 	device_printf(dev, "Reset attempt failed. Can not reset the device\n");
313532f63fa7SMarcin Wojtas 
313632f63fa7SMarcin Wojtas 	return (rc);
313732f63fa7SMarcin Wojtas }
313832f63fa7SMarcin Wojtas 
313932f63fa7SMarcin Wojtas static void
314032f63fa7SMarcin Wojtas ena_reset_task(void *arg, int pending)
314132f63fa7SMarcin Wojtas {
314232f63fa7SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
314332f63fa7SMarcin Wojtas 
3144fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
31459b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
31469b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
31479b8d05b8SZbigniew Bodek 		return;
31489b8d05b8SZbigniew Bodek 	}
31499b8d05b8SZbigniew Bodek 
31509b8d05b8SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
315132f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, false);
315232f63fa7SMarcin Wojtas 	ena_restore_device(adapter);
31539b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
31549b8d05b8SZbigniew Bodek }
31559b8d05b8SZbigniew Bodek 
31569b8d05b8SZbigniew Bodek /**
31579b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
31589b8d05b8SZbigniew Bodek  * @pdev: device information struct
31599b8d05b8SZbigniew Bodek  *
31609b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
31619b8d05b8SZbigniew Bodek  *
31629b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
31639b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
31649b8d05b8SZbigniew Bodek  * and a hardware reset occur.
31659b8d05b8SZbigniew Bodek  **/
31669b8d05b8SZbigniew Bodek static int
31679b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
31689b8d05b8SZbigniew Bodek {
31699b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
31704fa9e02dSMarcin Wojtas 	struct ena_llq_configurations llq_config;
31716064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
31729b8d05b8SZbigniew Bodek 	static int version_printed;
31739b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
31749b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
31754fa9e02dSMarcin Wojtas 	const char *queue_type_str;
31769b8d05b8SZbigniew Bodek 	int io_queue_num;
31774fa9e02dSMarcin Wojtas 	int rid, rc;
31784fa9e02dSMarcin Wojtas 
31799b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
31809b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
31819b8d05b8SZbigniew Bodek 
31829b8d05b8SZbigniew Bodek 	mtx_init(&adapter->global_mtx, "ENA global mtx", NULL, MTX_DEF);
31839b8d05b8SZbigniew Bodek 	sx_init(&adapter->ioctl_sx, "ENA ioctl sx");
31849b8d05b8SZbigniew Bodek 
31859b8d05b8SZbigniew Bodek 	/* Set up the timer service */
31869b8d05b8SZbigniew Bodek 	callout_init_mtx(&adapter->timer_service, &adapter->global_mtx, 0);
31879b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
31889b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
31899b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
31909b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
31919b8d05b8SZbigniew Bodek 
31929b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
31939b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
31949b8d05b8SZbigniew Bodek 
31959b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3196cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3197cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
31989b8d05b8SZbigniew Bodek 
31999b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
32009b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
32014fa9e02dSMarcin Wojtas 
32024fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_REG_BAR);
32034fa9e02dSMarcin Wojtas 	adapter->memory = NULL;
32044fa9e02dSMarcin Wojtas 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
32054fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
32064fa9e02dSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
32074fa9e02dSMarcin Wojtas 		device_printf(pdev,
32084fa9e02dSMarcin Wojtas 		    "unable to allocate bus resource: registers!\n");
32094fa9e02dSMarcin Wojtas 		rc = ENOMEM;
32104fa9e02dSMarcin Wojtas 		goto err_dev_free;
32114fa9e02dSMarcin Wojtas 	}
32124fa9e02dSMarcin Wojtas 
32139b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
32149b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
32159b8d05b8SZbigniew Bodek 
32169b8d05b8SZbigniew Bodek 	/* Store register resources */
32179b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
32189b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
32199b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
32209b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
32219b8d05b8SZbigniew Bodek 
32223f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
32239b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
32249b8d05b8SZbigniew Bodek 		rc = ENXIO;
3225cd5d5804SMarcin Wojtas 		goto err_bus_free;
32269b8d05b8SZbigniew Bodek 	}
32279b8d05b8SZbigniew Bodek 
32289b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
32299b8d05b8SZbigniew Bodek 
3230fd43fd2aSMarcin Wojtas 	/* Initially clear all the flags */
3231fd43fd2aSMarcin Wojtas 	ENA_FLAG_ZERO(adapter);
3232fd43fd2aSMarcin Wojtas 
32339b8d05b8SZbigniew Bodek 	/* Device initialization */
32349b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
32353f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32369b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
32379b8d05b8SZbigniew Bodek 		rc = ENXIO;
32389b8d05b8SZbigniew Bodek 		goto err_bus_free;
32399b8d05b8SZbigniew Bodek 	}
32409b8d05b8SZbigniew Bodek 
32414fa9e02dSMarcin Wojtas 	set_default_llq_configurations(&llq_config);
32424fa9e02dSMarcin Wojtas 
32434fa9e02dSMarcin Wojtas 	rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx.llq,
32444fa9e02dSMarcin Wojtas 	     &llq_config);
32454fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
32464fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to set placement policy\n");
32474fa9e02dSMarcin Wojtas 		goto err_com_free;
32484fa9e02dSMarcin Wojtas 	}
32494fa9e02dSMarcin Wojtas 
32504fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
32514fa9e02dSMarcin Wojtas 		queue_type_str = "Regular";
32524fa9e02dSMarcin Wojtas 	else
32534fa9e02dSMarcin Wojtas 		queue_type_str = "Low Latency";
32544fa9e02dSMarcin Wojtas 	device_printf(pdev, "Placement policy: %s\n", queue_type_str);
32554fa9e02dSMarcin Wojtas 
32569b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
32579b8d05b8SZbigniew Bodek 
32589b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
32599b8d05b8SZbigniew Bodek 
32609b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
32619b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
32629b8d05b8SZbigniew Bodek 
32636064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
32646064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
32656064f289SMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
32666064f289SMarcin Wojtas 
32679b8d05b8SZbigniew Bodek 	/* calculate IO queue number to create */
32689b8d05b8SZbigniew Bodek 	io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx);
32699b8d05b8SZbigniew Bodek 
32709b8d05b8SZbigniew Bodek 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
32719b8d05b8SZbigniew Bodek 	    io_queue_num);
32729b8d05b8SZbigniew Bodek 	adapter->num_queues = io_queue_num;
32739b8d05b8SZbigniew Bodek 
32743cfadb28SMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
32756064f289SMarcin Wojtas 	// Set the requested Rx ring size
32766064f289SMarcin Wojtas 	adapter->rx_ring_size = ENA_DEFAULT_RING_SIZE;
32779b8d05b8SZbigniew Bodek 	/* calculatre ring sizes */
32786064f289SMarcin Wojtas 	rc = ena_calc_queue_size(adapter, &calc_queue_ctx);
32796064f289SMarcin Wojtas 	if (unlikely((rc != 0) || (io_queue_num <= 0))) {
32806064f289SMarcin Wojtas 		rc = EFAULT;
32819b8d05b8SZbigniew Bodek 		goto err_com_free;
32829b8d05b8SZbigniew Bodek 	}
32839b8d05b8SZbigniew Bodek 
3284a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3285a195fab0SMarcin Wojtas 
32866064f289SMarcin Wojtas 	adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
32876064f289SMarcin Wojtas 	adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
32889b8d05b8SZbigniew Bodek 
32896064f289SMarcin Wojtas 	adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
32906064f289SMarcin Wojtas 	adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
32916064f289SMarcin Wojtas 
32926064f289SMarcin Wojtas 	adapter->buf_ring_size = ENA_DEFAULT_BUF_RING_SIZE;
32939b8d05b8SZbigniew Bodek 
32949b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
32959b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
32964e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
32974e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
3298cd5d5804SMarcin Wojtas 		goto err_com_free;
32994e8acd84SMarcin Wojtas 	}
33009b8d05b8SZbigniew Bodek 
33019b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
33024e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
33034e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
3304cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
33054e8acd84SMarcin Wojtas 	}
33069b8d05b8SZbigniew Bodek 
33079b8d05b8SZbigniew Bodek 	/* initialize rings basic information */
33086064f289SMarcin Wojtas 	device_printf(pdev,
33096064f289SMarcin Wojtas 	    "Creating %d io queues. Rx queue size: %d, Tx queue size: %d\n",
33106064f289SMarcin Wojtas 	    io_queue_num,
33116064f289SMarcin Wojtas 	    calc_queue_ctx.rx_queue_size,
33126064f289SMarcin Wojtas 	    calc_queue_ctx.tx_queue_size);
3313cd5d5804SMarcin Wojtas 	ena_init_io_rings(adapter);
33149b8d05b8SZbigniew Bodek 
33159b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
33163f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33179b8d05b8SZbigniew Bodek 		device_printf(pdev,
33189b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
3319c115a1e2SMarcin Wojtas 		goto err_io_free;
3320c115a1e2SMarcin Wojtas 	}
3321c115a1e2SMarcin Wojtas 
3322c115a1e2SMarcin Wojtas 	/* setup network interface */
3323c115a1e2SMarcin Wojtas 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
3324c115a1e2SMarcin Wojtas 	if (unlikely(rc != 0)) {
3325c115a1e2SMarcin Wojtas 		device_printf(pdev, "Error with network interface setup\n");
3326c115a1e2SMarcin Wojtas 		goto err_msix_free;
33279b8d05b8SZbigniew Bodek 	}
33289b8d05b8SZbigniew Bodek 
3329081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3330081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3331081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3332081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3333081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3334081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3335081169f2SZbigniew Bodek 
33369b8d05b8SZbigniew Bodek 	/* Initialize statistics */
33379b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
33389b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
333930217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
334030217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
33419b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
33429b8d05b8SZbigniew Bodek 
3343d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3344d17b7d87SMarcin Wojtas 	rc = ena_netmap_attach(adapter);
3345d17b7d87SMarcin Wojtas 	if (rc != 0) {
3346d17b7d87SMarcin Wojtas 		device_printf(pdev, "netmap attach failed: %d\n", rc);
3347d17b7d87SMarcin Wojtas 		goto err_detach;
3348d17b7d87SMarcin Wojtas 	}
3349d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3350d17b7d87SMarcin Wojtas 
33519b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
33529b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
3353fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
33549b8d05b8SZbigniew Bodek 
33559b8d05b8SZbigniew Bodek 	return (0);
33569b8d05b8SZbigniew Bodek 
3357d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3358d17b7d87SMarcin Wojtas err_detach:
3359d17b7d87SMarcin Wojtas 	ether_ifdetach(adapter->ifp);
3360d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3361c115a1e2SMarcin Wojtas err_msix_free:
3362c115a1e2SMarcin Wojtas 	ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR);
3363c115a1e2SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
3364c115a1e2SMarcin Wojtas 	ena_disable_msix(adapter);
3365cd5d5804SMarcin Wojtas err_io_free:
33669b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
33679b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3368cd5d5804SMarcin Wojtas err_tx_tag_free:
33699b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3370cd5d5804SMarcin Wojtas err_com_free:
33719b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
33729b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
3373cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
33749b8d05b8SZbigniew Bodek err_bus_free:
33759b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
33769b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
33774fa9e02dSMarcin Wojtas err_dev_free:
33784fa9e02dSMarcin Wojtas 	free(ena_dev, M_DEVBUF);
3379cd5d5804SMarcin Wojtas 
33809b8d05b8SZbigniew Bodek 	return (rc);
33819b8d05b8SZbigniew Bodek }
33829b8d05b8SZbigniew Bodek 
33839b8d05b8SZbigniew Bodek /**
33849b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
33859b8d05b8SZbigniew Bodek  * @pdev: device information struct
33869b8d05b8SZbigniew Bodek  *
33879b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
33889b8d05b8SZbigniew Bodek  * that it should release a PCI device.
33899b8d05b8SZbigniew Bodek  **/
33909b8d05b8SZbigniew Bodek static int
33919b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
33929b8d05b8SZbigniew Bodek {
33939b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
33949b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
33959b8d05b8SZbigniew Bodek 	int rc;
33969b8d05b8SZbigniew Bodek 
33979b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
33989b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
33999b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
34009b8d05b8SZbigniew Bodek 		return (EBUSY);
34019b8d05b8SZbigniew Bodek 	}
34029b8d05b8SZbigniew Bodek 
34039151c55dSMarcin Wojtas 	ether_ifdetach(adapter->ifp);
34049151c55dSMarcin Wojtas 
34059b8d05b8SZbigniew Bodek 	/* Free reset task and callout */
34069b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
34079b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
34089b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
34099b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
34109b8d05b8SZbigniew Bodek 
3411e67c6554SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
34129b8d05b8SZbigniew Bodek 	ena_down(adapter);
341332f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, true);
3414e67c6554SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
34159b8d05b8SZbigniew Bodek 
3416d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3417d17b7d87SMarcin Wojtas 	netmap_detach(adapter->ifp);
3418d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3419d17b7d87SMarcin Wojtas 
34209b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
34219b8d05b8SZbigniew Bodek 
342230217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
342330217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
34249b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
34259b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
34269b8d05b8SZbigniew Bodek 
34279b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
34283f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
34299b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
34309b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
34319b8d05b8SZbigniew Bodek 
34329b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
34333f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
34349b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
34359b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
34369b8d05b8SZbigniew Bodek 
34379b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
34389b8d05b8SZbigniew Bodek 
34399b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
34409b8d05b8SZbigniew Bodek 
344132f63fa7SMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter)))
344232f63fa7SMarcin Wojtas 		ena_com_rss_destroy(ena_dev);
344332f63fa7SMarcin Wojtas 
344432f63fa7SMarcin Wojtas 	ena_com_delete_host_info(ena_dev);
344532f63fa7SMarcin Wojtas 
34469b8d05b8SZbigniew Bodek 	mtx_destroy(&adapter->global_mtx);
34479b8d05b8SZbigniew Bodek 	sx_destroy(&adapter->ioctl_sx);
34489b8d05b8SZbigniew Bodek 
34499151c55dSMarcin Wojtas 	if_free(adapter->ifp);
34509151c55dSMarcin Wojtas 
34519b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
34529b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
34539b8d05b8SZbigniew Bodek 
34549b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
34559b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
34569b8d05b8SZbigniew Bodek 
34579b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
34589b8d05b8SZbigniew Bodek }
34599b8d05b8SZbigniew Bodek 
34609b8d05b8SZbigniew Bodek /******************************************************************************
34619b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
34629b8d05b8SZbigniew Bodek  *****************************************************************************/
34639b8d05b8SZbigniew Bodek /**
34649b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
34659b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
34669b8d05b8SZbigniew Bodek  **/
34679b8d05b8SZbigniew Bodek static void
34689b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
34699b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
34709b8d05b8SZbigniew Bodek {
34719b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
34729b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
34739b8d05b8SZbigniew Bodek 	int status;
34749b8d05b8SZbigniew Bodek 	if_t ifp;
34759b8d05b8SZbigniew Bodek 
34769b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
34779b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
34789b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
34799b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
34809b8d05b8SZbigniew Bodek 
34819b8d05b8SZbigniew Bodek 	if (status != 0) {
34829b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
3483fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_LINK_UP, adapter);
348432f63fa7SMarcin Wojtas 		if (!ENA_FLAG_ISSET(ENA_FLAG_ONGOING_RESET, adapter))
348532f63fa7SMarcin Wojtas 			if_link_state_change(ifp, LINK_STATE_UP);
348632f63fa7SMarcin Wojtas 	} else {
34879b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
34889b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
3489fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_LINK_UP, adapter);
34909b8d05b8SZbigniew Bodek 	}
34919b8d05b8SZbigniew Bodek }
34929b8d05b8SZbigniew Bodek 
349340621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
349440621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
349540621d71SMarcin Wojtas {
349640621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
349740621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
349840621d71SMarcin Wojtas 
349940621d71SMarcin Wojtas 	ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
350040621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
350140621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
350240621d71SMarcin Wojtas 
350340621d71SMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrom) {
350440621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
350540621d71SMarcin Wojtas 		hints =
350640621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
350740621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
350840621d71SMarcin Wojtas 		break;
350940621d71SMarcin Wojtas 	default:
351040621d71SMarcin Wojtas 		device_printf(adapter->pdev,
351140621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
351240621d71SMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrom);
351340621d71SMarcin Wojtas 	}
351440621d71SMarcin Wojtas }
351540621d71SMarcin Wojtas 
35169b8d05b8SZbigniew Bodek /**
35179b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
35189b8d05b8SZbigniew Bodek  **/
35199b8d05b8SZbigniew Bodek static void
3520e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
35219b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
35229b8d05b8SZbigniew Bodek {
3523e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3524e6de9a83SMarcin Wojtas 
3525e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
3526e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
35279b8d05b8SZbigniew Bodek }
35289b8d05b8SZbigniew Bodek 
35299b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
35309b8d05b8SZbigniew Bodek     .handlers = {
35319b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
353240621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
35339b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
35349b8d05b8SZbigniew Bodek     },
35359b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
35369b8d05b8SZbigniew Bodek };
35379b8d05b8SZbigniew Bodek 
35389b8d05b8SZbigniew Bodek /*********************************************************************
35399b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
35409b8d05b8SZbigniew Bodek  *********************************************************************/
35419b8d05b8SZbigniew Bodek 
35429b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
35439b8d05b8SZbigniew Bodek     /* Device interface */
35449b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
35459b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
35469b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
35479b8d05b8SZbigniew Bodek     DEVMETHOD_END
35489b8d05b8SZbigniew Bodek };
35499b8d05b8SZbigniew Bodek 
35509b8d05b8SZbigniew Bodek static driver_t ena_driver = {
35519b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
35529b8d05b8SZbigniew Bodek };
35539b8d05b8SZbigniew Bodek 
35549b8d05b8SZbigniew Bodek devclass_t ena_devclass;
35559b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
355640abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
3557329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
35589b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
35599b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
3560d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3561d17b7d87SMarcin Wojtas MODULE_DEPEND(ena, netmap, 1, 1, 1);
3562d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
35639b8d05b8SZbigniew Bodek 
35649b8d05b8SZbigniew Bodek /*********************************************************************/
3565