xref: /freebsd/sys/dev/ena/ena.c (revision 9eb1615f33e9174fa5f1ca46954c35b3f026a98a)
19b8d05b8SZbigniew Bodek /*-
29b8d05b8SZbigniew Bodek  * BSD LICENSE
39b8d05b8SZbigniew Bodek  *
42287afd8SMarcin Wojtas  * Copyright (c) 2015-2020 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 
33b40dd828SAndriy Gapon #include "opt_rss.h"
34b40dd828SAndriy Gapon 
359b8d05b8SZbigniew Bodek #include <sys/param.h>
369b8d05b8SZbigniew Bodek #include <sys/systm.h>
379b8d05b8SZbigniew Bodek #include <sys/bus.h>
389b8d05b8SZbigniew Bodek #include <sys/endian.h>
399b8d05b8SZbigniew Bodek #include <sys/kernel.h>
409b8d05b8SZbigniew Bodek #include <sys/kthread.h>
419b8d05b8SZbigniew Bodek #include <sys/malloc.h>
429b8d05b8SZbigniew Bodek #include <sys/mbuf.h>
439b8d05b8SZbigniew Bodek #include <sys/module.h>
449b8d05b8SZbigniew Bodek #include <sys/rman.h>
459b8d05b8SZbigniew Bodek #include <sys/smp.h>
469b8d05b8SZbigniew Bodek #include <sys/socket.h>
479b8d05b8SZbigniew Bodek #include <sys/sockio.h>
489b8d05b8SZbigniew Bodek #include <sys/sysctl.h>
499b8d05b8SZbigniew Bodek #include <sys/taskqueue.h>
509b8d05b8SZbigniew Bodek #include <sys/time.h>
519b8d05b8SZbigniew Bodek #include <sys/eventhandler.h>
529b8d05b8SZbigniew Bodek 
539b8d05b8SZbigniew Bodek #include <machine/bus.h>
549b8d05b8SZbigniew Bodek #include <machine/resource.h>
559b8d05b8SZbigniew Bodek #include <machine/in_cksum.h>
569b8d05b8SZbigniew Bodek 
579b8d05b8SZbigniew Bodek #include <net/bpf.h>
589b8d05b8SZbigniew Bodek #include <net/ethernet.h>
599b8d05b8SZbigniew Bodek #include <net/if.h>
609b8d05b8SZbigniew Bodek #include <net/if_var.h>
619b8d05b8SZbigniew Bodek #include <net/if_arp.h>
629b8d05b8SZbigniew Bodek #include <net/if_dl.h>
639b8d05b8SZbigniew Bodek #include <net/if_media.h>
649b8d05b8SZbigniew Bodek #include <net/if_types.h>
659b8d05b8SZbigniew Bodek #include <net/if_vlan_var.h>
66b40dd828SAndriy Gapon #ifdef RSS
67b40dd828SAndriy Gapon #include <net/rss_config.h>
68b40dd828SAndriy Gapon #endif
699b8d05b8SZbigniew Bodek 
709b8d05b8SZbigniew Bodek #include <netinet/in_systm.h>
719b8d05b8SZbigniew Bodek #include <netinet/in.h>
729b8d05b8SZbigniew Bodek #include <netinet/if_ether.h>
739b8d05b8SZbigniew Bodek #include <netinet/ip.h>
749b8d05b8SZbigniew Bodek #include <netinet/ip6.h>
759b8d05b8SZbigniew Bodek #include <netinet/tcp.h>
769b8d05b8SZbigniew Bodek #include <netinet/udp.h>
779b8d05b8SZbigniew Bodek 
789b8d05b8SZbigniew Bodek #include <dev/pci/pcivar.h>
799b8d05b8SZbigniew Bodek #include <dev/pci/pcireg.h>
809b8d05b8SZbigniew Bodek 
814fa9e02dSMarcin Wojtas #include <vm/vm.h>
824fa9e02dSMarcin Wojtas #include <vm/pmap.h>
834fa9e02dSMarcin Wojtas 
8438c7b965SMarcin Wojtas #include "ena_datapath.h"
859b8d05b8SZbigniew Bodek #include "ena.h"
869b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
879b8d05b8SZbigniew Bodek 
88d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
89d17b7d87SMarcin Wojtas #include "ena_netmap.h"
90d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
91d17b7d87SMarcin Wojtas 
929b8d05b8SZbigniew Bodek /*********************************************************
939b8d05b8SZbigniew Bodek  *  Function prototypes
949b8d05b8SZbigniew Bodek  *********************************************************/
959b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
969b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
979b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
989b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
999b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
1009b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
1019b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
1029b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
1039b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
1047d8c4feeSMarcin Wojtas static void	ena_init_io_rings_basic(struct ena_adapter *);
1057d8c4feeSMarcin Wojtas static void	ena_init_io_rings_advanced(struct ena_adapter *);
106cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
1079b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
1089b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
1099b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
1109b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
1119b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
1129b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
1136f2128c7SMarcin Wojtas static void	ena_release_all_tx_dmamap(struct ena_ring *);
1149b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1159b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1169b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1179b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
1189b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1199b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1209b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1219b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1229b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1239b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1249b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1259b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1269b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1279b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1289b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1299b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1309b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1319b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1329b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1339b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1349b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1355cb9db07SMarcin Wojtas static int	ena_handle_msix(void *);
1369b8d05b8SZbigniew Bodek static int	ena_enable_msix(struct ena_adapter *);
1379b8d05b8SZbigniew Bodek static void	ena_setup_mgmnt_intr(struct ena_adapter *);
13877958fcdSMarcin Wojtas static int	ena_setup_io_intr(struct ena_adapter *);
1399b8d05b8SZbigniew Bodek static int	ena_request_mgmnt_irq(struct ena_adapter *);
1409b8d05b8SZbigniew Bodek static int	ena_request_io_irq(struct ena_adapter *);
1419b8d05b8SZbigniew Bodek static void	ena_free_mgmnt_irq(struct ena_adapter *);
1429b8d05b8SZbigniew Bodek static void	ena_free_io_irq(struct ena_adapter *);
1439b8d05b8SZbigniew Bodek static void	ena_free_irqs(struct ena_adapter*);
1449b8d05b8SZbigniew Bodek static void	ena_disable_msix(struct ena_adapter *);
1459b8d05b8SZbigniew Bodek static void	ena_unmask_all_io_irqs(struct ena_adapter *);
1469b8d05b8SZbigniew Bodek static int	ena_rss_configure(struct ena_adapter *);
1479b8d05b8SZbigniew Bodek static int	ena_up_complete(struct ena_adapter *);
1489b8d05b8SZbigniew Bodek static uint64_t	ena_get_counter(if_t, ift_counter);
1499b8d05b8SZbigniew Bodek static int	ena_media_change(if_t);
1509b8d05b8SZbigniew Bodek static void	ena_media_status(if_t, struct ifmediareq *);
1519b8d05b8SZbigniew Bodek static void	ena_init(void *);
1529b8d05b8SZbigniew Bodek static int	ena_ioctl(if_t, u_long, caddr_t);
1539b8d05b8SZbigniew Bodek static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
1549b8d05b8SZbigniew Bodek static void	ena_update_host_info(struct ena_admin_host_info *, if_t);
1559b8d05b8SZbigniew Bodek static void	ena_update_hwassist(struct ena_adapter *);
1569b8d05b8SZbigniew Bodek static int	ena_setup_ifnet(device_t, struct ena_adapter *,
1579b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1584fa9e02dSMarcin Wojtas static int	ena_enable_wc(struct resource *);
1594fa9e02dSMarcin Wojtas static int	ena_set_queues_placement_policy(device_t, struct ena_com_dev *,
1604fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *, struct ena_llq_configurations *);
1617d8c4feeSMarcin Wojtas static uint32_t	ena_calc_max_io_queue_num(device_t, struct ena_com_dev *,
1629b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1637d8c4feeSMarcin Wojtas static int	ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *);
1649b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1659b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
16646021271SMarcin Wojtas static void	ena_config_host_info(struct ena_com_dev *, device_t);
1679b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1689b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1699b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1709b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
171aa9c3226SMarcin Wojtas static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *);
1729b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1739b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1749b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1759b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1769b8d05b8SZbigniew Bodek 
1779b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1789b8d05b8SZbigniew Bodek 
1799b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
1809b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
1819b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
1829b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
1839b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
1849b8d05b8SZbigniew Bodek     /* Last entry */
1859b8d05b8SZbigniew Bodek     { 0, 0, 0 }
1869b8d05b8SZbigniew Bodek };
1879b8d05b8SZbigniew Bodek 
1889b8d05b8SZbigniew Bodek /*
1899b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
1909b8d05b8SZbigniew Bodek  */
1919b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
1929b8d05b8SZbigniew Bodek 
1939b8d05b8SZbigniew Bodek void
1949b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1959b8d05b8SZbigniew Bodek {
1960bdffe59SMarcin Wojtas 	if (error != 0)
1979b8d05b8SZbigniew Bodek 		return;
1989b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
1999b8d05b8SZbigniew Bodek }
2009b8d05b8SZbigniew Bodek 
2019b8d05b8SZbigniew Bodek int
2029b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
2034f8f476eSMarcin Wojtas     ena_mem_handle_t *dma, int mapflags, bus_size_t alignment)
2049b8d05b8SZbigniew Bodek {
2059b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2060bdffe59SMarcin Wojtas 	uint32_t maxsize;
2070bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2089b8d05b8SZbigniew Bodek 	int error;
2099b8d05b8SZbigniew Bodek 
2100bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2110bdffe59SMarcin Wojtas 
2120bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2133f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2149b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2150bdffe59SMarcin Wojtas 
2169b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2174f8f476eSMarcin Wojtas 	    alignment, 0,     /* alignment, bounds 		*/
2188a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2198a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2209b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2219b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2229b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2239b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2249b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2259b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2269b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2279b8d05b8SZbigniew Bodek 	    &dma->tag);
2283f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
229*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2309b8d05b8SZbigniew Bodek 		goto fail_tag;
2319b8d05b8SZbigniew Bodek 	}
2329b8d05b8SZbigniew Bodek 
2339b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2349b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2353f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
236*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2374e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2389b8d05b8SZbigniew Bodek 		goto fail_map_create;
2399b8d05b8SZbigniew Bodek 	}
2409b8d05b8SZbigniew Bodek 
2419b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2429b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2439b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2443f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
245*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2469b8d05b8SZbigniew Bodek 		goto fail_map_load;
2479b8d05b8SZbigniew Bodek 	}
2489b8d05b8SZbigniew Bodek 
249e8073738SMarcin Wojtas 	bus_dmamap_sync(dma->tag, dma->map,
250e8073738SMarcin Wojtas 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
251e8073738SMarcin Wojtas 
2529b8d05b8SZbigniew Bodek 	return (0);
2539b8d05b8SZbigniew Bodek 
2549b8d05b8SZbigniew Bodek fail_map_load:
2559b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2567d2544e6SMarcin Wojtas fail_map_create:
2579b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2589b8d05b8SZbigniew Bodek fail_tag:
2599b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2605b14f92eSMarcin Wojtas 	dma->vaddr = NULL;
2615b14f92eSMarcin Wojtas 	dma->paddr = 0;
2629b8d05b8SZbigniew Bodek 
2639b8d05b8SZbigniew Bodek 	return (error);
2649b8d05b8SZbigniew Bodek }
2659b8d05b8SZbigniew Bodek 
2668483b844SMarcin Wojtas /*
2678483b844SMarcin Wojtas  * This function should generate unique key for the whole driver.
2688483b844SMarcin Wojtas  * If the key was already genereated in the previous call (for example
2698483b844SMarcin Wojtas  * for another adapter), then it should be returned instead.
2708483b844SMarcin Wojtas  */
2718483b844SMarcin Wojtas void
2728483b844SMarcin Wojtas ena_rss_key_fill(void *key, size_t size)
2738483b844SMarcin Wojtas {
2748483b844SMarcin Wojtas 	static bool key_generated;
2758483b844SMarcin Wojtas 	static uint8_t default_key[ENA_HASH_KEY_SIZE];
2768483b844SMarcin Wojtas 
2778483b844SMarcin Wojtas 	KASSERT(size <= ENA_HASH_KEY_SIZE, ("Requested more bytes than ENA RSS key can hold"));
2788483b844SMarcin Wojtas 
2798483b844SMarcin Wojtas 	if (!key_generated) {
2808483b844SMarcin Wojtas 		arc4random_buf(default_key, ENA_HASH_KEY_SIZE);
2818483b844SMarcin Wojtas 		key_generated = true;
2828483b844SMarcin Wojtas 	}
2838483b844SMarcin Wojtas 
2848483b844SMarcin Wojtas 	memcpy(key, default_key, size);
2858483b844SMarcin Wojtas }
2868483b844SMarcin Wojtas 
2879b8d05b8SZbigniew Bodek static void
2889b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
2899b8d05b8SZbigniew Bodek {
2909b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2919b8d05b8SZbigniew Bodek 
2929b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
2939b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2949b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
2959b8d05b8SZbigniew Bodek 	}
2969b8d05b8SZbigniew Bodek 
2979b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
2989b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2999b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
3009b8d05b8SZbigniew Bodek 	}
3019b8d05b8SZbigniew Bodek }
3029b8d05b8SZbigniew Bodek 
3039b8d05b8SZbigniew Bodek static int
3049b8d05b8SZbigniew Bodek ena_probe(device_t dev)
3059b8d05b8SZbigniew Bodek {
3069b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3079b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3089b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3099b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3109b8d05b8SZbigniew Bodek 
3119b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3129b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3139b8d05b8SZbigniew Bodek 
3149b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3159b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3169b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3179b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
318*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_DBG, "vendor=%x device=%x\n",
3199b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3209b8d05b8SZbigniew Bodek 
3219b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3229b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3239b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3249b8d05b8SZbigniew Bodek 		}
3259b8d05b8SZbigniew Bodek 
3269b8d05b8SZbigniew Bodek 		ent++;
3279b8d05b8SZbigniew Bodek 
3289b8d05b8SZbigniew Bodek 	}
3299b8d05b8SZbigniew Bodek 
3309b8d05b8SZbigniew Bodek 	return (ENXIO);
3319b8d05b8SZbigniew Bodek }
3329b8d05b8SZbigniew Bodek 
3339b8d05b8SZbigniew Bodek static int
3349b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3359b8d05b8SZbigniew Bodek {
3369b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3373cfadb28SMarcin Wojtas 	int rc;
3389b8d05b8SZbigniew Bodek 
3393cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3409b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3413cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3423cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3433cfadb28SMarcin Wojtas 		return (EINVAL);
3449b8d05b8SZbigniew Bodek 	}
3459b8d05b8SZbigniew Bodek 
3469b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3473cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
348*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_DBG, "set MTU to %d\n", new_mtu);
3493cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3503cfadb28SMarcin Wojtas 	} else {
3513cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3523cfadb28SMarcin Wojtas 		    new_mtu);
3533cfadb28SMarcin Wojtas 	}
3549b8d05b8SZbigniew Bodek 
3553cfadb28SMarcin Wojtas 	return (rc);
3569b8d05b8SZbigniew Bodek }
3579b8d05b8SZbigniew Bodek 
3589b8d05b8SZbigniew Bodek static inline void
3599b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3609b8d05b8SZbigniew Bodek {
3619b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3629b8d05b8SZbigniew Bodek 
3639b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3649b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3659b8d05b8SZbigniew Bodek }
3669b8d05b8SZbigniew Bodek 
3679b8d05b8SZbigniew Bodek static inline void
3689b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3699b8d05b8SZbigniew Bodek {
3709b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3719b8d05b8SZbigniew Bodek 
3729b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3739b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3749b8d05b8SZbigniew Bodek }
3759b8d05b8SZbigniew Bodek 
3769b8d05b8SZbigniew Bodek static inline void
3779b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3789b8d05b8SZbigniew Bodek {
3799b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3809b8d05b8SZbigniew Bodek 
3819b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3829b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
3839b8d05b8SZbigniew Bodek }
3849b8d05b8SZbigniew Bodek 
3859b8d05b8SZbigniew Bodek static void
3869b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
3879b8d05b8SZbigniew Bodek     uint16_t qid)
3889b8d05b8SZbigniew Bodek {
3899b8d05b8SZbigniew Bodek 
3909b8d05b8SZbigniew Bodek 	ring->qid = qid;
3919b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
3929b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
393d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
394d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
3959b8d05b8SZbigniew Bodek }
3969b8d05b8SZbigniew Bodek 
397cd5d5804SMarcin Wojtas static void
3987d8c4feeSMarcin Wojtas ena_init_io_rings_basic(struct ena_adapter *adapter)
3999b8d05b8SZbigniew Bodek {
4009b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
4019b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
4029b8d05b8SZbigniew Bodek 	struct ena_que *que;
4039b8d05b8SZbigniew Bodek 	int i;
4049b8d05b8SZbigniew Bodek 
4059b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
4069b8d05b8SZbigniew Bodek 
4077d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
4089b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4099b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4109b8d05b8SZbigniew Bodek 
4119b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4129b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4139b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4149b8d05b8SZbigniew Bodek 
4159b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4169b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4179b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4189b8d05b8SZbigniew Bodek 
4199b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4209b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4219b8d05b8SZbigniew Bodek 		que->id = i;
4229b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4239b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4249b8d05b8SZbigniew Bodek 
4259b8d05b8SZbigniew Bodek 		txr->que = que;
4269b8d05b8SZbigniew Bodek 		rxr->que = que;
427efe6ab18SMarcin Wojtas 
428efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4297d8c4feeSMarcin Wojtas 		rxr->rx_mbuf_sz = ena_mbuf_sz;
4309b8d05b8SZbigniew Bodek 	}
4319b8d05b8SZbigniew Bodek }
4329b8d05b8SZbigniew Bodek 
4339b8d05b8SZbigniew Bodek static void
4347d8c4feeSMarcin Wojtas ena_init_io_rings_advanced(struct ena_adapter *adapter)
4357d8c4feeSMarcin Wojtas {
4367d8c4feeSMarcin Wojtas 	struct ena_ring *txr, *rxr;
4377d8c4feeSMarcin Wojtas 	int i;
4387d8c4feeSMarcin Wojtas 
4397d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
4407d8c4feeSMarcin Wojtas 		txr = &adapter->tx_ring[i];
4417d8c4feeSMarcin Wojtas 		rxr = &adapter->rx_ring[i];
4427d8c4feeSMarcin Wojtas 
4437d8c4feeSMarcin Wojtas 		/* Allocate a buf ring */
4447d8c4feeSMarcin Wojtas 		txr->buf_ring_size = adapter->buf_ring_size;
4457d8c4feeSMarcin Wojtas 		txr->br = buf_ring_alloc(txr->buf_ring_size, M_DEVBUF,
4467d8c4feeSMarcin Wojtas 		    M_WAITOK, &txr->ring_mtx);
4477d8c4feeSMarcin Wojtas 
4487d8c4feeSMarcin Wojtas 		/* Allocate Tx statistics. */
4497d8c4feeSMarcin Wojtas 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4507d8c4feeSMarcin Wojtas 		    sizeof(txr->tx_stats));
4517d8c4feeSMarcin Wojtas 
4527d8c4feeSMarcin Wojtas 		/* Allocate Rx statistics. */
4537d8c4feeSMarcin Wojtas 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4547d8c4feeSMarcin Wojtas 		    sizeof(rxr->rx_stats));
4557d8c4feeSMarcin Wojtas 
4567d8c4feeSMarcin Wojtas 		/* Initialize locks */
4577d8c4feeSMarcin Wojtas 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4587d8c4feeSMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
4597d8c4feeSMarcin Wojtas 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4607d8c4feeSMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
4617d8c4feeSMarcin Wojtas 
4627d8c4feeSMarcin Wojtas 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
4637d8c4feeSMarcin Wojtas 	}
4647d8c4feeSMarcin Wojtas }
4657d8c4feeSMarcin Wojtas 
4667d8c4feeSMarcin Wojtas static void
4677d8c4feeSMarcin Wojtas ena_init_io_rings(struct ena_adapter *adapter)
4687d8c4feeSMarcin Wojtas {
4697d8c4feeSMarcin Wojtas 	/*
4707d8c4feeSMarcin Wojtas 	 * IO rings initialization can be divided into the 2 steps:
4717d8c4feeSMarcin Wojtas 	 *   1. Initialize variables and fields with initial values and copy
4727d8c4feeSMarcin Wojtas 	 *      them from adapter/ena_dev (basic)
4737d8c4feeSMarcin Wojtas 	 *   2. Allocate mutex, counters and buf_ring (advanced)
4747d8c4feeSMarcin Wojtas 	 */
4757d8c4feeSMarcin Wojtas 	ena_init_io_rings_basic(adapter);
4767d8c4feeSMarcin Wojtas 	ena_init_io_rings_advanced(adapter);
4777d8c4feeSMarcin Wojtas }
4787d8c4feeSMarcin Wojtas 
4797d8c4feeSMarcin Wojtas static void
4809b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4819b8d05b8SZbigniew Bodek {
4829b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4839b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4849b8d05b8SZbigniew Bodek 
4859b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4869b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4879b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4889b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4899b8d05b8SZbigniew Bodek 
4907d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4917d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4927d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4937d2544e6SMarcin Wojtas 
4949b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
4959b8d05b8SZbigniew Bodek }
4969b8d05b8SZbigniew Bodek 
4979b8d05b8SZbigniew Bodek static void
4989b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
4999b8d05b8SZbigniew Bodek {
5009b8d05b8SZbigniew Bodek 	int i;
5019b8d05b8SZbigniew Bodek 
5027d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
5039b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
5049b8d05b8SZbigniew Bodek 
5059b8d05b8SZbigniew Bodek }
5069b8d05b8SZbigniew Bodek 
5079b8d05b8SZbigniew Bodek static int
5089b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
5099b8d05b8SZbigniew Bodek {
5109b8d05b8SZbigniew Bodek 	int ret;
5119b8d05b8SZbigniew Bodek 
5129b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
5139b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
5149b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5158a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5168a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5179b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5189b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
5198a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
5209b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
5219b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5229b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5239b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
5249b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5259b8d05b8SZbigniew Bodek 
5269b8d05b8SZbigniew Bodek 	return (ret);
5279b8d05b8SZbigniew Bodek }
5289b8d05b8SZbigniew Bodek 
5299b8d05b8SZbigniew Bodek static int
5309b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5319b8d05b8SZbigniew Bodek {
5329b8d05b8SZbigniew Bodek 	int ret;
5339b8d05b8SZbigniew Bodek 
5349b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5359b8d05b8SZbigniew Bodek 
5363f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5379b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5389b8d05b8SZbigniew Bodek 
5399b8d05b8SZbigniew Bodek 	return (ret);
5409b8d05b8SZbigniew Bodek }
5419b8d05b8SZbigniew Bodek 
5429b8d05b8SZbigniew Bodek static int
5439b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5449b8d05b8SZbigniew Bodek {
5459b8d05b8SZbigniew Bodek 	int ret;
5469b8d05b8SZbigniew Bodek 
5479b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5489b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5499b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5508a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5518a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5529b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
55304cf2b88SMarcin Wojtas 	    ena_mbuf_sz,			  /* maxsize 		     */
5544727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
55504cf2b88SMarcin Wojtas 	    ena_mbuf_sz,			  /* maxsegsize 	     */
5569b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5579b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5589b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5599b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5609b8d05b8SZbigniew Bodek 
5619b8d05b8SZbigniew Bodek 	return (ret);
5629b8d05b8SZbigniew Bodek }
5639b8d05b8SZbigniew Bodek 
5649b8d05b8SZbigniew Bodek static int
5659b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5669b8d05b8SZbigniew Bodek {
5679b8d05b8SZbigniew Bodek 	int ret;
5689b8d05b8SZbigniew Bodek 
5699b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5709b8d05b8SZbigniew Bodek 
5713f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5729b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5739b8d05b8SZbigniew Bodek 
5749b8d05b8SZbigniew Bodek 	return (ret);
5759b8d05b8SZbigniew Bodek }
5769b8d05b8SZbigniew Bodek 
5776f2128c7SMarcin Wojtas static void
5786f2128c7SMarcin Wojtas ena_release_all_tx_dmamap(struct ena_ring *tx_ring)
5796f2128c7SMarcin Wojtas {
5806f2128c7SMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
5816f2128c7SMarcin Wojtas 	struct ena_tx_buffer *tx_info;
5826f2128c7SMarcin Wojtas 	bus_dma_tag_t tx_tag = adapter->tx_buf_tag;;
5836f2128c7SMarcin Wojtas 	int i;
5846f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
5856f2128c7SMarcin Wojtas 	struct ena_netmap_tx_info *nm_info;
5866f2128c7SMarcin Wojtas 	int j;
5876f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
5886f2128c7SMarcin Wojtas 
5896f2128c7SMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; ++i) {
5906f2128c7SMarcin Wojtas 		tx_info = &tx_ring->tx_buffer_info[i];
5916f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
5926f2128c7SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP) {
5936f2128c7SMarcin Wojtas 			nm_info = &tx_info->nm_info;
5946f2128c7SMarcin Wojtas 			for (j = 0; j < ENA_PKT_MAX_BUFS; ++j) {
5956f2128c7SMarcin Wojtas 				if (nm_info->map_seg[j] != NULL) {
5966f2128c7SMarcin Wojtas 					bus_dmamap_destroy(tx_tag,
5976f2128c7SMarcin Wojtas 					    nm_info->map_seg[j]);
5986f2128c7SMarcin Wojtas 					nm_info->map_seg[j] = NULL;
5996f2128c7SMarcin Wojtas 				}
6006f2128c7SMarcin Wojtas 			}
6016f2128c7SMarcin Wojtas 		}
6026f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
603888810f0SMarcin Wojtas 		if (tx_info->dmamap != NULL) {
604888810f0SMarcin Wojtas 			bus_dmamap_destroy(tx_tag, tx_info->dmamap);
605888810f0SMarcin Wojtas 			tx_info->dmamap = NULL;
6066f2128c7SMarcin Wojtas 		}
6076f2128c7SMarcin Wojtas 	}
6086f2128c7SMarcin Wojtas }
6096f2128c7SMarcin Wojtas 
6109b8d05b8SZbigniew Bodek /**
6119b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
6129b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6139b8d05b8SZbigniew Bodek  * @qid: queue index
6149b8d05b8SZbigniew Bodek  *
6159b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
6169b8d05b8SZbigniew Bodek  **/
6179b8d05b8SZbigniew Bodek static int
6189b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
6199b8d05b8SZbigniew Bodek {
6209b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
6219b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
6229b8d05b8SZbigniew Bodek 	int size, i, err;
6236f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
6246f2128c7SMarcin Wojtas 	bus_dmamap_t *map;
6256f2128c7SMarcin Wojtas 	int j;
6266f2128c7SMarcin Wojtas 
6276f2128c7SMarcin Wojtas 	ena_netmap_reset_tx_ring(adapter, qid);
6286f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
6299b8d05b8SZbigniew Bodek 
6309b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
6319b8d05b8SZbigniew Bodek 
6329b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6333f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
6347d2544e6SMarcin Wojtas 		return (ENOMEM);
6359b8d05b8SZbigniew Bodek 
6369b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
6379b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6383f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
6397d2544e6SMarcin Wojtas 		goto err_buf_info_free;
6409b8d05b8SZbigniew Bodek 
6414fa9e02dSMarcin Wojtas 	size = tx_ring->tx_max_header_size;
6424fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = malloc(size, M_DEVBUF,
6434fa9e02dSMarcin Wojtas 	    M_NOWAIT | M_ZERO);
6444fa9e02dSMarcin Wojtas 	if (unlikely(tx_ring->push_buf_intermediate_buf == NULL))
6454fa9e02dSMarcin Wojtas 		goto err_tx_ids_free;
6464fa9e02dSMarcin Wojtas 
6479b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
6489b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
6499b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
6509b8d05b8SZbigniew Bodek 
6519b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
6529b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
6539b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
6549b8d05b8SZbigniew Bodek 
6559b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
6569b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
657af66d7d0SMarcin Wojtas 	tx_ring->acum_pkts = 0;
6589b8d05b8SZbigniew Bodek 
6599b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
660b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6619b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
662b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6639b8d05b8SZbigniew Bodek 
6649b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6659b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6669b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
667888810f0SMarcin Wojtas 		    &tx_ring->tx_buffer_info[i].dmamap);
6683f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
669*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT,
670888810f0SMarcin Wojtas 			    "Unable to create Tx DMA map for buffer %d\n",
6714fa9e02dSMarcin Wojtas 			    i);
6726f2128c7SMarcin Wojtas 			goto err_map_release;
6739b8d05b8SZbigniew Bodek 		}
6746f2128c7SMarcin Wojtas 
6756f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
6766f2128c7SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP) {
6776f2128c7SMarcin Wojtas 			map = tx_ring->tx_buffer_info[i].nm_info.map_seg;
6786f2128c7SMarcin Wojtas 			for (j = 0; j < ENA_PKT_MAX_BUFS; j++) {
6796f2128c7SMarcin Wojtas 				err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6806f2128c7SMarcin Wojtas 				    &map[j]);
6816f2128c7SMarcin Wojtas 				if (unlikely(err != 0)) {
682*9eb1615fSMarcin Wojtas 					ena_trace(NULL, ENA_ALERT, "Unable to create "
6836f2128c7SMarcin Wojtas 					    "Tx DMA for buffer %d %d\n", i, j);
6846f2128c7SMarcin Wojtas 					goto err_map_release;
6856f2128c7SMarcin Wojtas 				}
6866f2128c7SMarcin Wojtas 			}
6876f2128c7SMarcin Wojtas 		}
6886f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
6899b8d05b8SZbigniew Bodek 	}
6909b8d05b8SZbigniew Bodek 
6919b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6929b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6939b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6949b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6953f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
696*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT,
6979b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6989b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6996f2128c7SMarcin Wojtas 		goto err_map_release;
7009b8d05b8SZbigniew Bodek 	}
7019b8d05b8SZbigniew Bodek 
7025cb9db07SMarcin Wojtas 	tx_ring->running = true;
7035cb9db07SMarcin Wojtas 
7049b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
7059b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
7069b8d05b8SZbigniew Bodek 
7079b8d05b8SZbigniew Bodek 	return (0);
7089b8d05b8SZbigniew Bodek 
7096f2128c7SMarcin Wojtas err_map_release:
7106f2128c7SMarcin Wojtas 	ena_release_all_tx_dmamap(tx_ring);
7114fa9e02dSMarcin Wojtas err_tx_ids_free:
712cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7137d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
7147d2544e6SMarcin Wojtas err_buf_info_free:
715cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
7167d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
7177d2544e6SMarcin Wojtas 
7189b8d05b8SZbigniew Bodek 	return (ENOMEM);
7199b8d05b8SZbigniew Bodek }
7209b8d05b8SZbigniew Bodek 
7219b8d05b8SZbigniew Bodek /**
7229b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
7239b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7249b8d05b8SZbigniew Bodek  * @qid: queue index
7259b8d05b8SZbigniew Bodek  *
7269b8d05b8SZbigniew Bodek  * Free all transmit software resources
7279b8d05b8SZbigniew Bodek  **/
7289b8d05b8SZbigniew Bodek static void
7299b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
7309b8d05b8SZbigniew Bodek {
7319b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
7326f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
7336f2128c7SMarcin Wojtas 	struct ena_netmap_tx_info *nm_info;
7346f2128c7SMarcin Wojtas 	int j;
7356f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
7369b8d05b8SZbigniew Bodek 
7379b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
7389b8d05b8SZbigniew Bodek 	    NULL))
7399b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
7409b8d05b8SZbigniew Bodek 
7419b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
7429b8d05b8SZbigniew Bodek 
743b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
7449b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
7459b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
7469b8d05b8SZbigniew Bodek 
7479b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
7489b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
749e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag,
750888810f0SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].dmamap, BUS_DMASYNC_POSTWRITE);
7519b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag,
752888810f0SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].dmamap);
7539b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
754888810f0SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].dmamap);
7554fa9e02dSMarcin Wojtas 
7566f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
7576f2128c7SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP) {
7586f2128c7SMarcin Wojtas 			nm_info = &tx_ring->tx_buffer_info[i].nm_info;
7596f2128c7SMarcin Wojtas 			for (j = 0; j < ENA_PKT_MAX_BUFS; j++) {
7606f2128c7SMarcin Wojtas 				if (nm_info->socket_buf_idx[j] != 0) {
7616f2128c7SMarcin Wojtas 					bus_dmamap_sync(adapter->tx_buf_tag,
7626f2128c7SMarcin Wojtas 					    nm_info->map_seg[j],
7636f2128c7SMarcin Wojtas 					    BUS_DMASYNC_POSTWRITE);
7646f2128c7SMarcin Wojtas 					ena_netmap_unload(adapter,
7656f2128c7SMarcin Wojtas 					    nm_info->map_seg[j]);
7666f2128c7SMarcin Wojtas 				}
7676f2128c7SMarcin Wojtas 				bus_dmamap_destroy(adapter->tx_buf_tag,
7686f2128c7SMarcin Wojtas 				    nm_info->map_seg[j]);
7696f2128c7SMarcin Wojtas 				nm_info->socket_buf_idx[j] = 0;
7706f2128c7SMarcin Wojtas 			}
7716f2128c7SMarcin Wojtas 		}
7726f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
7736f2128c7SMarcin Wojtas 
774e8073738SMarcin Wojtas 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
775e8073738SMarcin Wojtas 		tx_ring->tx_buffer_info[i].mbuf = NULL;
7769b8d05b8SZbigniew Bodek 	}
777416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
7789b8d05b8SZbigniew Bodek 
7799b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
780cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
7819b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
7829b8d05b8SZbigniew Bodek 
783cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7849b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
7854fa9e02dSMarcin Wojtas 
7868483b844SMarcin Wojtas 	free(tx_ring->push_buf_intermediate_buf, M_DEVBUF);
7874fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = NULL;
7889b8d05b8SZbigniew Bodek }
7899b8d05b8SZbigniew Bodek 
7909b8d05b8SZbigniew Bodek /**
7919b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7929b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7939b8d05b8SZbigniew Bodek  *
7949b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7959b8d05b8SZbigniew Bodek  **/
7969b8d05b8SZbigniew Bodek static int
7979b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7989b8d05b8SZbigniew Bodek {
7999b8d05b8SZbigniew Bodek 	int i, rc;
8009b8d05b8SZbigniew Bodek 
8017d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
8029b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
8030bdffe59SMarcin Wojtas 		if (rc != 0) {
8049b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8059b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
8069b8d05b8SZbigniew Bodek 			goto err_setup_tx;
8079b8d05b8SZbigniew Bodek 		}
8087d2544e6SMarcin Wojtas 	}
8099b8d05b8SZbigniew Bodek 
8109b8d05b8SZbigniew Bodek 	return (0);
8119b8d05b8SZbigniew Bodek 
8129b8d05b8SZbigniew Bodek err_setup_tx:
8139b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
8149b8d05b8SZbigniew Bodek 	while (i--)
8159b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
8169b8d05b8SZbigniew Bodek 	return (rc);
8179b8d05b8SZbigniew Bodek }
8189b8d05b8SZbigniew Bodek 
8199b8d05b8SZbigniew Bodek /**
8209b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
8219b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8229b8d05b8SZbigniew Bodek  *
8239b8d05b8SZbigniew Bodek  * Free all transmit software resources
8249b8d05b8SZbigniew Bodek  **/
8259b8d05b8SZbigniew Bodek static void
8269b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
8279b8d05b8SZbigniew Bodek {
8289b8d05b8SZbigniew Bodek 	int i;
8299b8d05b8SZbigniew Bodek 
8307d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
8319b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
8329b8d05b8SZbigniew Bodek }
8339b8d05b8SZbigniew Bodek 
8349b8d05b8SZbigniew Bodek /**
8359b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
8369b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8379b8d05b8SZbigniew Bodek  * @qid: queue index
8389b8d05b8SZbigniew Bodek  *
8399b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8409b8d05b8SZbigniew Bodek  **/
8419b8d05b8SZbigniew Bodek static int
8429b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8439b8d05b8SZbigniew Bodek {
8449b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
8459b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
8469b8d05b8SZbigniew Bodek 	int size, err, i;
8479b8d05b8SZbigniew Bodek 
8489b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
8499b8d05b8SZbigniew Bodek 
8509a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
8519a0f2079SMarcin Wojtas 	ena_netmap_reset_rx_ring(adapter, qid);
8529a0f2079SMarcin Wojtas 	rx_ring->initialized = false;
8539a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
8549a0f2079SMarcin Wojtas 
8559b8d05b8SZbigniew Bodek 	/*
8569b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
8579b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
8589b8d05b8SZbigniew Bodek 	 */
8599b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
8609b8d05b8SZbigniew Bodek 
861cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
8629b8d05b8SZbigniew Bodek 
86343fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
86443fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
86543fefd16SMarcin Wojtas 
86643fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
86743fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
86843fefd16SMarcin Wojtas 
8699b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
8709b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
8719b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
8729b8d05b8SZbigniew Bodek 
8739b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
8749b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
8759b8d05b8SZbigniew Bodek 
8769b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
8779b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
8789b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
8799b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
8809b8d05b8SZbigniew Bodek 		if (err != 0) {
881*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT,
8829b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8837d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
8849b8d05b8SZbigniew Bodek 		}
8859b8d05b8SZbigniew Bodek 	}
8869b8d05b8SZbigniew Bodek 
8879b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8880bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8899b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8900bdffe59SMarcin Wojtas 		if (err != 0) {
8919b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8929b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8939b8d05b8SZbigniew Bodek 		} else {
894*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_INFO,
8959b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8969b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8979b8d05b8SZbigniew Bodek 		}
8989b8d05b8SZbigniew Bodek 	}
8999b8d05b8SZbigniew Bodek 
9009b8d05b8SZbigniew Bodek 	return (0);
9019b8d05b8SZbigniew Bodek 
9027d2544e6SMarcin Wojtas err_buf_info_unmap:
9039b8d05b8SZbigniew Bodek 	while (i--) {
9049b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
9059b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
9069b8d05b8SZbigniew Bodek 	}
9079b8d05b8SZbigniew Bodek 
90843fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
90943fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
910cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
9119b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
9129b8d05b8SZbigniew Bodek 	return (ENOMEM);
9139b8d05b8SZbigniew Bodek }
9149b8d05b8SZbigniew Bodek 
9159b8d05b8SZbigniew Bodek /**
9169b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
9179b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9189b8d05b8SZbigniew Bodek  * @qid: queue index
9199b8d05b8SZbigniew Bodek  *
9209b8d05b8SZbigniew Bodek  * Free all receive software resources
9219b8d05b8SZbigniew Bodek  **/
9229b8d05b8SZbigniew Bodek static void
9239b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
9249b8d05b8SZbigniew Bodek {
9259b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
9269b8d05b8SZbigniew Bodek 
9279b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
9289b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
929e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag,
930e8073738SMarcin Wojtas 		    rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD);
9319b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
9329b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
9339b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
9349b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
9359b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
9369b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
9379b8d05b8SZbigniew Bodek 	}
9389b8d05b8SZbigniew Bodek 
9399b8d05b8SZbigniew Bodek 	/* free LRO resources, */
9409b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
9419b8d05b8SZbigniew Bodek 
9429b8d05b8SZbigniew Bodek 	/* free allocated memory */
943cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
9449b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
9459b8d05b8SZbigniew Bodek 
94643fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
94743fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
9489b8d05b8SZbigniew Bodek }
9499b8d05b8SZbigniew Bodek 
9509b8d05b8SZbigniew Bodek /**
9519b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
9529b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9539b8d05b8SZbigniew Bodek  *
9549b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
9559b8d05b8SZbigniew Bodek  **/
9569b8d05b8SZbigniew Bodek static int
9579b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
9589b8d05b8SZbigniew Bodek {
9599b8d05b8SZbigniew Bodek 	int i, rc = 0;
9609b8d05b8SZbigniew Bodek 
9617d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
9629b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
9630bdffe59SMarcin Wojtas 		if (rc != 0) {
9649b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
9659b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
9669b8d05b8SZbigniew Bodek 			goto err_setup_rx;
9679b8d05b8SZbigniew Bodek 		}
9687d2544e6SMarcin Wojtas 	}
9699b8d05b8SZbigniew Bodek 	return (0);
9709b8d05b8SZbigniew Bodek 
9719b8d05b8SZbigniew Bodek err_setup_rx:
9729b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
9739b8d05b8SZbigniew Bodek 	while (i--)
9749b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9759b8d05b8SZbigniew Bodek 	return (rc);
9769b8d05b8SZbigniew Bodek }
9779b8d05b8SZbigniew Bodek 
9789b8d05b8SZbigniew Bodek /**
9799b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
9809b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9819b8d05b8SZbigniew Bodek  *
9829b8d05b8SZbigniew Bodek  * Free all receive software resources
9839b8d05b8SZbigniew Bodek  **/
9849b8d05b8SZbigniew Bodek static void
9859b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9869b8d05b8SZbigniew Bodek {
9879b8d05b8SZbigniew Bodek 	int i;
9889b8d05b8SZbigniew Bodek 
9897d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
9909b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9919b8d05b8SZbigniew Bodek }
9929b8d05b8SZbigniew Bodek 
9939b8d05b8SZbigniew Bodek static inline int
9949b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9959b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9969b8d05b8SZbigniew Bodek {
9979b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9989b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9999b8d05b8SZbigniew Bodek 	int nsegs, error;
10004727bda6SMarcin Wojtas 	int mlen;
10019b8d05b8SZbigniew Bodek 
10029b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
10033f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
10049b8d05b8SZbigniew Bodek 		return (0);
10059b8d05b8SZbigniew Bodek 
10069b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
100704cf2b88SMarcin Wojtas 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
100804cf2b88SMarcin Wojtas 	    rx_ring->rx_mbuf_sz);
10099b8d05b8SZbigniew Bodek 
10103f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
10114727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
10124727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
10134727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
10149b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
10159b8d05b8SZbigniew Bodek 			return (ENOMEM);
10169b8d05b8SZbigniew Bodek 		}
10174727bda6SMarcin Wojtas 		mlen = MCLBYTES;
10184727bda6SMarcin Wojtas 	} else {
101904cf2b88SMarcin Wojtas 		mlen = rx_ring->rx_mbuf_sz;
10204727bda6SMarcin Wojtas 	}
10219b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
10224727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
10239b8d05b8SZbigniew Bodek 
10249b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
1025*9eb1615fSMarcin Wojtas 	ena_trace(NULL, ENA_DBG | ENA_RSC | ENA_RXPTH,
102630425f93SMarcin Wojtas 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d\n",
10279b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
10289b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
10299b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
10303f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
1031*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_WARNING, "failed to map mbuf, error: %d, "
10329b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
10339b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
10349b8d05b8SZbigniew Bodek 		goto exit;
10359b8d05b8SZbigniew Bodek 
10369b8d05b8SZbigniew Bodek 	}
10379b8d05b8SZbigniew Bodek 
10389b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
10399b8d05b8SZbigniew Bodek 
10409b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
10419b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
10424727bda6SMarcin Wojtas 	ena_buf->len = mlen;
10439b8d05b8SZbigniew Bodek 
1044*9eb1615fSMarcin Wojtas 	ena_trace(NULL, ENA_DBG | ENA_RSC | ENA_RXPTH,
10459b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
10469b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
10479b8d05b8SZbigniew Bodek 
10489b8d05b8SZbigniew Bodek 	return (0);
10499b8d05b8SZbigniew Bodek 
10509b8d05b8SZbigniew Bodek exit:
10519b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10529b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10539b8d05b8SZbigniew Bodek 	return (EFAULT);
10549b8d05b8SZbigniew Bodek }
10559b8d05b8SZbigniew Bodek 
10569b8d05b8SZbigniew Bodek static void
10579b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
10589b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
10599b8d05b8SZbigniew Bodek {
10609b8d05b8SZbigniew Bodek 
10614e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
1062*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_WARNING, "Trying to free unallocated buffer\n");
10639b8d05b8SZbigniew Bodek 		return;
10644e8acd84SMarcin Wojtas 	}
10659b8d05b8SZbigniew Bodek 
1066e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1067e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
10689b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
10699b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10709b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10719b8d05b8SZbigniew Bodek }
10729b8d05b8SZbigniew Bodek 
10739b8d05b8SZbigniew Bodek /**
10749b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
10759b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
10769b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
10779b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
10789b8d05b8SZbigniew Bodek  **/
107938c7b965SMarcin Wojtas int
10809b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
10819b8d05b8SZbigniew Bodek {
10829b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
108343fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
10849b8d05b8SZbigniew Bodek 	uint32_t i;
10859b8d05b8SZbigniew Bodek 	int rc;
10869b8d05b8SZbigniew Bodek 
1087*9eb1615fSMarcin Wojtas 	ena_trace(NULL, ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d\n",
10889b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10899b8d05b8SZbigniew Bodek 
10909b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10919b8d05b8SZbigniew Bodek 
10929b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
109343fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
109443fefd16SMarcin Wojtas 
1095*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_DBG | ENA_RXPTH | ENA_RSC,
109630425f93SMarcin Wojtas 		    "RX buffer - next to use: %d\n", next_to_use);
10979b8d05b8SZbigniew Bodek 
109843fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
109943fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
11009a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
1101358bcc4cSMarcin Wojtas 		if (ena_rx_ring_in_netmap(adapter, rx_ring->qid))
11029a0f2079SMarcin Wojtas 			rc = ena_netmap_alloc_rx_slot(adapter, rx_ring, rx_info);
11039a0f2079SMarcin Wojtas 		else
11049a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
11059b8d05b8SZbigniew Bodek 			rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
11063f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
1107*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_WARNING,
11084e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
11094e8acd84SMarcin Wojtas 			    rx_ring->qid);
11109b8d05b8SZbigniew Bodek 			break;
11119b8d05b8SZbigniew Bodek 		}
11129b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
111343fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
11140bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
1115*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_WARNING,
11169b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
11179b8d05b8SZbigniew Bodek 			    rx_ring->qid);
11189b8d05b8SZbigniew Bodek 			break;
11199b8d05b8SZbigniew Bodek 		}
11209b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
11219b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
11229b8d05b8SZbigniew Bodek 	}
11239b8d05b8SZbigniew Bodek 
11243f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
11259b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
1126*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_WARNING,
11274e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
11284e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
11299b8d05b8SZbigniew Bodek 	}
11309b8d05b8SZbigniew Bodek 
11318483b844SMarcin Wojtas 	if (likely(i != 0))
11329b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
11338483b844SMarcin Wojtas 
11349b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
11359b8d05b8SZbigniew Bodek 	return (i);
11369b8d05b8SZbigniew Bodek }
11379b8d05b8SZbigniew Bodek 
11387d8c4feeSMarcin Wojtas int
113921823546SMarcin Wojtas ena_update_buf_ring_size(struct ena_adapter *adapter,
114021823546SMarcin Wojtas     uint32_t new_buf_ring_size)
114121823546SMarcin Wojtas {
114221823546SMarcin Wojtas 	uint32_t old_buf_ring_size;
114321823546SMarcin Wojtas 	int rc = 0;
114421823546SMarcin Wojtas 	bool dev_was_up;
114521823546SMarcin Wojtas 
114621823546SMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
114721823546SMarcin Wojtas 
114821823546SMarcin Wojtas 	old_buf_ring_size = adapter->buf_ring_size;
114921823546SMarcin Wojtas 	adapter->buf_ring_size = new_buf_ring_size;
115021823546SMarcin Wojtas 
115121823546SMarcin Wojtas 	dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
115221823546SMarcin Wojtas 	ena_down(adapter);
115321823546SMarcin Wojtas 
115421823546SMarcin Wojtas 	/* Reconfigure buf ring for all Tx rings. */
115521823546SMarcin Wojtas 	ena_free_all_io_rings_resources(adapter);
115621823546SMarcin Wojtas 	ena_init_io_rings_advanced(adapter);
115721823546SMarcin Wojtas 	if (dev_was_up) {
115821823546SMarcin Wojtas 		/*
115921823546SMarcin Wojtas 		 * If ena_up() fails, it's not because of recent buf_ring size
116021823546SMarcin Wojtas 		 * changes. Because of that, we just want to revert old drbr
116121823546SMarcin Wojtas 		 * value and trigger the reset because something else had to
116221823546SMarcin Wojtas 		 * go wrong.
116321823546SMarcin Wojtas 		 */
116421823546SMarcin Wojtas 		rc = ena_up(adapter);
116521823546SMarcin Wojtas 		if (unlikely(rc != 0)) {
116621823546SMarcin Wojtas 			device_printf(adapter->pdev,
116721823546SMarcin Wojtas 			    "Failed to configure device after setting new drbr size: %u. Reverting old value: %u and triggering the reset\n",
116821823546SMarcin Wojtas 			    new_buf_ring_size, old_buf_ring_size);
116921823546SMarcin Wojtas 
117021823546SMarcin Wojtas 			/* Revert old size and trigger the reset */
117121823546SMarcin Wojtas 			adapter->buf_ring_size = old_buf_ring_size;
117221823546SMarcin Wojtas 			ena_free_all_io_rings_resources(adapter);
117321823546SMarcin Wojtas 			ena_init_io_rings_advanced(adapter);
117421823546SMarcin Wojtas 
117521823546SMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET,
117621823546SMarcin Wojtas 			    adapter);
117721823546SMarcin Wojtas 			ena_trigger_reset(adapter, ENA_REGS_RESET_OS_TRIGGER);
117821823546SMarcin Wojtas 
117921823546SMarcin Wojtas 		}
118021823546SMarcin Wojtas 	}
118121823546SMarcin Wojtas 
118221823546SMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
118321823546SMarcin Wojtas 
118421823546SMarcin Wojtas 	return (rc);
118521823546SMarcin Wojtas }
118621823546SMarcin Wojtas 
118721823546SMarcin Wojtas int
11887d8c4feeSMarcin Wojtas ena_update_queue_size(struct ena_adapter *adapter, uint32_t new_tx_size,
11897d8c4feeSMarcin Wojtas     uint32_t new_rx_size)
11907d8c4feeSMarcin Wojtas {
11917d8c4feeSMarcin Wojtas 	uint32_t old_tx_size, old_rx_size;
11927d8c4feeSMarcin Wojtas 	int rc = 0;
11937d8c4feeSMarcin Wojtas 	bool dev_was_up;
11947d8c4feeSMarcin Wojtas 
11957d8c4feeSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
11967d8c4feeSMarcin Wojtas 
11979762a033SMarcin Wojtas 	old_tx_size = adapter->requested_tx_ring_size;
11989762a033SMarcin Wojtas 	old_rx_size = adapter->requested_rx_ring_size;
11999762a033SMarcin Wojtas 	adapter->requested_tx_ring_size = new_tx_size;
12009762a033SMarcin Wojtas 	adapter->requested_rx_ring_size = new_rx_size;
12017d8c4feeSMarcin Wojtas 
12027d8c4feeSMarcin Wojtas 	dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
12037d8c4feeSMarcin Wojtas 	ena_down(adapter);
12047d8c4feeSMarcin Wojtas 
12057d8c4feeSMarcin Wojtas 	/* Configure queues with new size. */
12067d8c4feeSMarcin Wojtas 	ena_init_io_rings_basic(adapter);
12077d8c4feeSMarcin Wojtas 	if (dev_was_up) {
12087d8c4feeSMarcin Wojtas 		rc = ena_up(adapter);
12097d8c4feeSMarcin Wojtas 		if (unlikely(rc != 0)) {
12107d8c4feeSMarcin Wojtas 			device_printf(adapter->pdev,
12117d8c4feeSMarcin Wojtas 			    "Failed to configure device with the new sizes - Tx: %u Rx: %u. Reverting old values - Tx: %u Rx: %u\n",
12127d8c4feeSMarcin Wojtas 			    new_tx_size, new_rx_size, old_tx_size, old_rx_size);
12137d8c4feeSMarcin Wojtas 
12147d8c4feeSMarcin Wojtas 			/* Revert old size. */
12159762a033SMarcin Wojtas 			adapter->requested_tx_ring_size = old_tx_size;
12169762a033SMarcin Wojtas 			adapter->requested_rx_ring_size = old_rx_size;
12177d8c4feeSMarcin Wojtas 			ena_init_io_rings_basic(adapter);
12187d8c4feeSMarcin Wojtas 
12197d8c4feeSMarcin Wojtas 			/* And try again. */
12207d8c4feeSMarcin Wojtas 			rc = ena_up(adapter);
12217d8c4feeSMarcin Wojtas 			if (unlikely(rc != 0)) {
12227d8c4feeSMarcin Wojtas 				device_printf(adapter->pdev,
12237d8c4feeSMarcin Wojtas 				    "Failed to revert old queue sizes. Triggering device reset.\n");
12247d8c4feeSMarcin Wojtas 				/*
12257d8c4feeSMarcin Wojtas 				 * If we've failed again, something had to go
12267d8c4feeSMarcin Wojtas 				 * wrong. After reset, the device should try to
12277d8c4feeSMarcin Wojtas 				 * go up
12287d8c4feeSMarcin Wojtas 				 */
12297d8c4feeSMarcin Wojtas 				ENA_FLAG_SET_ATOMIC(
12307d8c4feeSMarcin Wojtas 				    ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
12317d8c4feeSMarcin Wojtas 				ena_trigger_reset(adapter,
12327d8c4feeSMarcin Wojtas 				    ENA_REGS_RESET_OS_TRIGGER);
12337d8c4feeSMarcin Wojtas 			}
12347d8c4feeSMarcin Wojtas 		}
12357d8c4feeSMarcin Wojtas 	}
12367d8c4feeSMarcin Wojtas 
12377d8c4feeSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
12387d8c4feeSMarcin Wojtas 
12397d8c4feeSMarcin Wojtas 	return (rc);
12407d8c4feeSMarcin Wojtas }
12417d8c4feeSMarcin Wojtas 
12429b8d05b8SZbigniew Bodek static void
124356d41ad5SMarcin Wojtas ena_update_io_rings(struct ena_adapter *adapter, uint32_t num)
124456d41ad5SMarcin Wojtas {
124556d41ad5SMarcin Wojtas 	ena_free_all_io_rings_resources(adapter);
124656d41ad5SMarcin Wojtas 	/* Force indirection table to be reinitialized */
124756d41ad5SMarcin Wojtas 	ena_com_rss_destroy(adapter->ena_dev);
124856d41ad5SMarcin Wojtas 
124956d41ad5SMarcin Wojtas 	adapter->num_io_queues = num;
125056d41ad5SMarcin Wojtas 	ena_init_io_rings(adapter);
125156d41ad5SMarcin Wojtas }
125256d41ad5SMarcin Wojtas 
125356d41ad5SMarcin Wojtas /* Caller should sanitize new_num */
125456d41ad5SMarcin Wojtas int
125556d41ad5SMarcin Wojtas ena_update_io_queue_nb(struct ena_adapter *adapter, uint32_t new_num)
125656d41ad5SMarcin Wojtas {
125756d41ad5SMarcin Wojtas 	uint32_t old_num;
125856d41ad5SMarcin Wojtas 	int rc = 0;
125956d41ad5SMarcin Wojtas 	bool dev_was_up;
126056d41ad5SMarcin Wojtas 
126156d41ad5SMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
126256d41ad5SMarcin Wojtas 
126356d41ad5SMarcin Wojtas 	dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
126456d41ad5SMarcin Wojtas 	old_num = adapter->num_io_queues;
126556d41ad5SMarcin Wojtas 	ena_down(adapter);
126656d41ad5SMarcin Wojtas 
126756d41ad5SMarcin Wojtas 	ena_update_io_rings(adapter, new_num);
126856d41ad5SMarcin Wojtas 
126956d41ad5SMarcin Wojtas 	if (dev_was_up) {
127056d41ad5SMarcin Wojtas 		rc = ena_up(adapter);
127156d41ad5SMarcin Wojtas 		if (unlikely(rc != 0)) {
127256d41ad5SMarcin Wojtas 			device_printf(adapter->pdev,
127356d41ad5SMarcin Wojtas 			    "Failed to configure device with %u IO queues. "
127456d41ad5SMarcin Wojtas 			    "Reverting to previous value: %u\n",
127556d41ad5SMarcin Wojtas 			    new_num, old_num);
127656d41ad5SMarcin Wojtas 
127756d41ad5SMarcin Wojtas 			ena_update_io_rings(adapter, old_num);
127856d41ad5SMarcin Wojtas 
127956d41ad5SMarcin Wojtas 			rc = ena_up(adapter);
128056d41ad5SMarcin Wojtas 			if (unlikely(rc != 0)) {
128156d41ad5SMarcin Wojtas 				device_printf(adapter->pdev,
128256d41ad5SMarcin Wojtas 				    "Failed to revert to previous setup IO "
128356d41ad5SMarcin Wojtas 				    "queues. Triggering device reset.\n");
128456d41ad5SMarcin Wojtas 				ENA_FLAG_SET_ATOMIC(
128556d41ad5SMarcin Wojtas 				    ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
128656d41ad5SMarcin Wojtas 				ena_trigger_reset(adapter,
128756d41ad5SMarcin Wojtas 				    ENA_REGS_RESET_OS_TRIGGER);
128856d41ad5SMarcin Wojtas 			}
128956d41ad5SMarcin Wojtas 		}
129056d41ad5SMarcin Wojtas 	}
129156d41ad5SMarcin Wojtas 
129256d41ad5SMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
129356d41ad5SMarcin Wojtas 
129456d41ad5SMarcin Wojtas 	return (rc);
129556d41ad5SMarcin Wojtas }
129656d41ad5SMarcin Wojtas 
129756d41ad5SMarcin Wojtas static void
12989b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
12999b8d05b8SZbigniew Bodek {
13009b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
13019b8d05b8SZbigniew Bodek 	unsigned int i;
13029b8d05b8SZbigniew Bodek 
13039b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
13049b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
13059b8d05b8SZbigniew Bodek 
13060bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
13079b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
13089a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
13099a0f2079SMarcin Wojtas 		if (((if_getflags(adapter->ifp) & IFF_DYING) == 0) &&
13109a0f2079SMarcin Wojtas 		    (adapter->ifp->if_capenable & IFCAP_NETMAP)) {
13119a0f2079SMarcin Wojtas 			if (rx_info->netmap_buf_idx != 0)
13129a0f2079SMarcin Wojtas 				ena_netmap_free_rx_slot(adapter, rx_ring,
13139a0f2079SMarcin Wojtas 				    rx_info);
13149a0f2079SMarcin Wojtas 		}
13159a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
13169b8d05b8SZbigniew Bodek 	}
13179b8d05b8SZbigniew Bodek }
13189b8d05b8SZbigniew Bodek 
13199b8d05b8SZbigniew Bodek /**
13209b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
13219b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
13229b8d05b8SZbigniew Bodek  *
13239b8d05b8SZbigniew Bodek  */
13249b8d05b8SZbigniew Bodek static void
13259b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
13269b8d05b8SZbigniew Bodek {
13279b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
13289b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
13299b8d05b8SZbigniew Bodek 
13307d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
13319b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
13329b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
13339b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
13349b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
1335*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_WARNING, "refilling Queue %d failed. "
13364e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
13379a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
13389a0f2079SMarcin Wojtas 		rx_ring->initialized = true;
13399a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
13409b8d05b8SZbigniew Bodek 	}
13419b8d05b8SZbigniew Bodek }
13429b8d05b8SZbigniew Bodek 
13439b8d05b8SZbigniew Bodek static void
13449b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
13459b8d05b8SZbigniew Bodek {
13469b8d05b8SZbigniew Bodek 	int i;
13479b8d05b8SZbigniew Bodek 
13487d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
13499b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
13509b8d05b8SZbigniew Bodek }
13519b8d05b8SZbigniew Bodek 
13529b8d05b8SZbigniew Bodek /**
13539b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
13549b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
13559b8d05b8SZbigniew Bodek  * @qid: queue index
13569b8d05b8SZbigniew Bodek  **/
13579b8d05b8SZbigniew Bodek static void
13589b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
13599b8d05b8SZbigniew Bodek {
13604e8acd84SMarcin Wojtas 	bool print_once = true;
13619b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
13629b8d05b8SZbigniew Bodek 
1363416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
13649b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
13659b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
13669b8d05b8SZbigniew Bodek 
13679b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
13689b8d05b8SZbigniew Bodek 			continue;
13699b8d05b8SZbigniew Bodek 
13704e8acd84SMarcin Wojtas 		if (print_once) {
13714e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
137230425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
13734e8acd84SMarcin Wojtas 			    qid, i);
13744e8acd84SMarcin Wojtas 			print_once = false;
13754e8acd84SMarcin Wojtas 		} else {
1376*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_DBG,
137730425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
13784e8acd84SMarcin Wojtas 			     qid, i);
13794e8acd84SMarcin Wojtas 		}
13809b8d05b8SZbigniew Bodek 
1381888810f0SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag, tx_info->dmamap,
1382e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTWRITE);
1383888810f0SMarcin Wojtas 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->dmamap);
13844fa9e02dSMarcin Wojtas 
13859b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
13869b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
13879b8d05b8SZbigniew Bodek 	}
1388416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
13899b8d05b8SZbigniew Bodek }
13909b8d05b8SZbigniew Bodek 
13919b8d05b8SZbigniew Bodek static void
13929b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
13939b8d05b8SZbigniew Bodek {
13949b8d05b8SZbigniew Bodek 
13957d8c4feeSMarcin Wojtas 	for (int i = 0; i < adapter->num_io_queues; i++)
13969b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
13979b8d05b8SZbigniew Bodek }
13989b8d05b8SZbigniew Bodek 
13999b8d05b8SZbigniew Bodek static void
14009b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
14019b8d05b8SZbigniew Bodek {
14029b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
14039b8d05b8SZbigniew Bodek 	int i;
14049b8d05b8SZbigniew Bodek 
14057d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14069b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
14079b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
14089b8d05b8SZbigniew Bodek 	}
14099b8d05b8SZbigniew Bodek }
14109b8d05b8SZbigniew Bodek 
14119b8d05b8SZbigniew Bodek static void
14129b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
14139b8d05b8SZbigniew Bodek {
14149b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
14159b8d05b8SZbigniew Bodek 	int i;
14169b8d05b8SZbigniew Bodek 
14177d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14189b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
14199b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
14209b8d05b8SZbigniew Bodek 	}
14219b8d05b8SZbigniew Bodek }
14229b8d05b8SZbigniew Bodek 
14239b8d05b8SZbigniew Bodek static void
14249b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
14259b8d05b8SZbigniew Bodek {
14265cb9db07SMarcin Wojtas 	struct ena_que *queue;
14275cb9db07SMarcin Wojtas 	int i;
14285cb9db07SMarcin Wojtas 
14297d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14305cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
14315cb9db07SMarcin Wojtas 		while (taskqueue_cancel(queue->cleanup_tq,
14325cb9db07SMarcin Wojtas 		    &queue->cleanup_task, NULL))
14335cb9db07SMarcin Wojtas 			taskqueue_drain(queue->cleanup_tq,
14345cb9db07SMarcin Wojtas 			    &queue->cleanup_task);
14355cb9db07SMarcin Wojtas 		taskqueue_free(queue->cleanup_tq);
14365cb9db07SMarcin Wojtas 	}
14375cb9db07SMarcin Wojtas 
14389b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
14399b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
14409b8d05b8SZbigniew Bodek }
14419b8d05b8SZbigniew Bodek 
14429b8d05b8SZbigniew Bodek static int
14439b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
14449b8d05b8SZbigniew Bodek {
14459b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
14469b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
14479b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
14485cb9db07SMarcin Wojtas 	struct ena_que *queue;
14499b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
14509b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
14519b8d05b8SZbigniew Bodek 	int rc, i;
14529b8d05b8SZbigniew Bodek 
14539b8d05b8SZbigniew Bodek 	/* Create TX queues */
14547d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14559b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
14569b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
14579b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
14589b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
14599762a033SMarcin Wojtas 		ctx.queue_size = adapter->requested_tx_ring_size;
14609b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
14619b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
14629b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
14630bdffe59SMarcin Wojtas 		if (rc != 0) {
14649b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
14659b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
14669b8d05b8SZbigniew Bodek 			goto err_tx;
14679b8d05b8SZbigniew Bodek 		}
14689b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
14699b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
14709b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
14719b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
14720bdffe59SMarcin Wojtas 		if (rc != 0) {
14739b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
14749b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
14759b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
14769b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
14779b8d05b8SZbigniew Bodek 			goto err_tx;
14789b8d05b8SZbigniew Bodek 		}
14799b8d05b8SZbigniew Bodek 	}
14809b8d05b8SZbigniew Bodek 
14819b8d05b8SZbigniew Bodek 	/* Create RX queues */
14827d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14839b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
14849b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
14859b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
14869b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
14879762a033SMarcin Wojtas 		ctx.queue_size = adapter->requested_rx_ring_size;
14889b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
14899b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
14909b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
14913f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
14929b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
14939b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
14949b8d05b8SZbigniew Bodek 			goto err_rx;
14959b8d05b8SZbigniew Bodek 		}
14969b8d05b8SZbigniew Bodek 
14979b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
14989b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
14999b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
15009b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
15013f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
15029b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
15039b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
15049b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
15059b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
15069b8d05b8SZbigniew Bodek 			goto err_rx;
15079b8d05b8SZbigniew Bodek 		}
15089b8d05b8SZbigniew Bodek 	}
15099b8d05b8SZbigniew Bodek 
15107d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
15115cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
15125cb9db07SMarcin Wojtas 
15136c3e93cbSGleb Smirnoff 		NET_TASK_INIT(&queue->cleanup_task, 0, ena_cleanup, queue);
15145cb9db07SMarcin Wojtas 		queue->cleanup_tq = taskqueue_create_fast("ena cleanup",
15155cb9db07SMarcin Wojtas 		    M_WAITOK, taskqueue_thread_enqueue, &queue->cleanup_tq);
15165cb9db07SMarcin Wojtas 
15175cb9db07SMarcin Wojtas 		taskqueue_start_threads(&queue->cleanup_tq, 1, PI_NET,
15185cb9db07SMarcin Wojtas 		    "%s queue %d cleanup",
15195cb9db07SMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
15205cb9db07SMarcin Wojtas 	}
15215cb9db07SMarcin Wojtas 
15229b8d05b8SZbigniew Bodek 	return (0);
15239b8d05b8SZbigniew Bodek 
15249b8d05b8SZbigniew Bodek err_rx:
15259b8d05b8SZbigniew Bodek 	while (i--)
15269b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
15277d8c4feeSMarcin Wojtas 	i = adapter->num_io_queues;
15289b8d05b8SZbigniew Bodek err_tx:
15299b8d05b8SZbigniew Bodek 	while (i--)
15309b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
15319b8d05b8SZbigniew Bodek 
15329b8d05b8SZbigniew Bodek 	return (ENXIO);
15339b8d05b8SZbigniew Bodek }
15349b8d05b8SZbigniew Bodek 
15359b8d05b8SZbigniew Bodek /*********************************************************************
15369b8d05b8SZbigniew Bodek  *
15379b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
15389b8d05b8SZbigniew Bodek  *
15399b8d05b8SZbigniew Bodek  **********************************************************************/
15409b8d05b8SZbigniew Bodek 
15419b8d05b8SZbigniew Bodek /**
15429b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
15439b8d05b8SZbigniew Bodek  * @arg: interrupt number
15449b8d05b8SZbigniew Bodek  **/
15459b8d05b8SZbigniew Bodek static void
15469b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
15479b8d05b8SZbigniew Bodek {
15489b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
15499b8d05b8SZbigniew Bodek 
15509b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1551fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)))
15529b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
15539b8d05b8SZbigniew Bodek }
15549b8d05b8SZbigniew Bodek 
15555cb9db07SMarcin Wojtas /**
15565cb9db07SMarcin Wojtas  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
15575cb9db07SMarcin Wojtas  * @arg: queue
15585cb9db07SMarcin Wojtas  **/
15595cb9db07SMarcin Wojtas static int
15605cb9db07SMarcin Wojtas ena_handle_msix(void *arg)
15615cb9db07SMarcin Wojtas {
15625cb9db07SMarcin Wojtas 	struct ena_que *queue = arg;
15635cb9db07SMarcin Wojtas 	struct ena_adapter *adapter = queue->adapter;
15645cb9db07SMarcin Wojtas 	if_t ifp = adapter->ifp;
15655cb9db07SMarcin Wojtas 
15665cb9db07SMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
15675cb9db07SMarcin Wojtas 		return (FILTER_STRAY);
15685cb9db07SMarcin Wojtas 
15695cb9db07SMarcin Wojtas 	taskqueue_enqueue(queue->cleanup_tq, &queue->cleanup_task);
15705cb9db07SMarcin Wojtas 
15715cb9db07SMarcin Wojtas 	return (FILTER_HANDLED);
15725cb9db07SMarcin Wojtas }
15735cb9db07SMarcin Wojtas 
15749b8d05b8SZbigniew Bodek static int
15759b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
15769b8d05b8SZbigniew Bodek {
15779b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
15788805021aSMarcin Wojtas 	int msix_vecs, msix_req;
15798805021aSMarcin Wojtas 	int i, rc = 0;
15809b8d05b8SZbigniew Bodek 
1581fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1582fd43fd2aSMarcin Wojtas 		device_printf(dev, "Error, MSI-X is already enabled\n");
1583fd43fd2aSMarcin Wojtas 		return (EINVAL);
1584fd43fd2aSMarcin Wojtas 	}
1585fd43fd2aSMarcin Wojtas 
15869b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
15877d8c4feeSMarcin Wojtas 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues);
15889b8d05b8SZbigniew Bodek 
1589cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1590cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1591cd5d5804SMarcin Wojtas 
1592*9eb1615fSMarcin Wojtas 	ena_trace(NULL, ENA_DBG, "trying to enable MSI-X, vectors: %d\n", msix_vecs);
15939b8d05b8SZbigniew Bodek 
15949b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
15959b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
15969b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
15979b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
15989b8d05b8SZbigniew Bodek 	}
15999b8d05b8SZbigniew Bodek 
16008805021aSMarcin Wojtas 	msix_req = msix_vecs;
16019b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
16023f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
16039b8d05b8SZbigniew Bodek 		device_printf(dev,
16049b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
16057d2544e6SMarcin Wojtas 
16069b8d05b8SZbigniew Bodek 		rc = ENOSPC;
16077d2544e6SMarcin Wojtas 		goto err_msix_free;
16089b8d05b8SZbigniew Bodek 	}
16099b8d05b8SZbigniew Bodek 
16108805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
16112b5b60feSMarcin Wojtas 		if (msix_vecs == ENA_ADMIN_MSIX_VEC) {
16122b5b60feSMarcin Wojtas 			device_printf(dev,
16132b5b60feSMarcin Wojtas 			    "Not enough number of MSI-x allocated: %d\n",
16142b5b60feSMarcin Wojtas 			    msix_vecs);
16152b5b60feSMarcin Wojtas 			pci_release_msi(dev);
16162b5b60feSMarcin Wojtas 			rc = ENOSPC;
16172b5b60feSMarcin Wojtas 			goto err_msix_free;
16182b5b60feSMarcin Wojtas 		}
16198805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
16208805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
16218805021aSMarcin Wojtas 	}
16228805021aSMarcin Wojtas 
16239b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
1624fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
16259b8d05b8SZbigniew Bodek 
16267d2544e6SMarcin Wojtas 	return (0);
16277d2544e6SMarcin Wojtas 
16287d2544e6SMarcin Wojtas err_msix_free:
16297d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
16307d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
16317d2544e6SMarcin Wojtas 
16329b8d05b8SZbigniew Bodek 	return (rc);
16339b8d05b8SZbigniew Bodek }
16349b8d05b8SZbigniew Bodek 
16359b8d05b8SZbigniew Bodek static void
16369b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
16379b8d05b8SZbigniew Bodek {
16389b8d05b8SZbigniew Bodek 
16399b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
16409b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
16419b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
16429b8d05b8SZbigniew Bodek 	/*
16439b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
16449b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
16459b8d05b8SZbigniew Bodek 	 */
16469b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
16479b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
16489b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
16499b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
16509b8d05b8SZbigniew Bodek }
16519b8d05b8SZbigniew Bodek 
165277958fcdSMarcin Wojtas static int
16539b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
16549b8d05b8SZbigniew Bodek {
16559b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
16569b8d05b8SZbigniew Bodek 	int irq_idx;
16579b8d05b8SZbigniew Bodek 
165877958fcdSMarcin Wojtas 	if (adapter->msix_entries == NULL)
165977958fcdSMarcin Wojtas 		return (EINVAL);
166077958fcdSMarcin Wojtas 
16617d8c4feeSMarcin Wojtas 	for (int i = 0; i < adapter->num_io_queues; i++) {
16629b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
16639b8d05b8SZbigniew Bodek 
16649b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
16659b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
16669b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
16679b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
16689b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
16699b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
1670*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
16719b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
1672277f11c4SMarcin Wojtas 
16739b8d05b8SZbigniew Bodek 		/*
1674277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
16759b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
16769b8d05b8SZbigniew Bodek 		 */
16773f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
16789b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
16799b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
16809b8d05b8SZbigniew Bodek 		    last_bind_cpu;
16819b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
16829b8d05b8SZbigniew Bodek 	}
168377958fcdSMarcin Wojtas 
168477958fcdSMarcin Wojtas 	return (0);
16859b8d05b8SZbigniew Bodek }
16869b8d05b8SZbigniew Bodek 
16879b8d05b8SZbigniew Bodek static int
16889b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
16899b8d05b8SZbigniew Bodek {
16909b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
16919b8d05b8SZbigniew Bodek 	unsigned long flags;
16929b8d05b8SZbigniew Bodek 	int rc, rcc;
16939b8d05b8SZbigniew Bodek 
16949b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
16959b8d05b8SZbigniew Bodek 
16969b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
16979b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
16989b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
16999b8d05b8SZbigniew Bodek 
17003f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
17019b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
17029b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
17037d2544e6SMarcin Wojtas 		return (ENXIO);
17049b8d05b8SZbigniew Bodek 	}
17059b8d05b8SZbigniew Bodek 
17060bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
17070bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
17080bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
17093f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
17109b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
17119b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
17129b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
17137d2544e6SMarcin Wojtas 		goto err_res_free;
17149b8d05b8SZbigniew Bodek 	}
17159b8d05b8SZbigniew Bodek 	irq->requested = true;
17169b8d05b8SZbigniew Bodek 
17179b8d05b8SZbigniew Bodek 	return (rc);
17189b8d05b8SZbigniew Bodek 
17197d2544e6SMarcin Wojtas err_res_free:
1720*9eb1615fSMarcin Wojtas 	ena_trace(NULL, ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
17217d2544e6SMarcin Wojtas 	    irq->vector);
17229b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
17239b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
17243f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
17259b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
17269b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
17279b8d05b8SZbigniew Bodek 	irq->res = NULL;
17289b8d05b8SZbigniew Bodek 
17299b8d05b8SZbigniew Bodek 	return (rc);
17309b8d05b8SZbigniew Bodek }
17319b8d05b8SZbigniew Bodek 
17329b8d05b8SZbigniew Bodek static int
17339b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
17349b8d05b8SZbigniew Bodek {
17359b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
17369b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
17379b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
17389b8d05b8SZbigniew Bodek 
1739fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter))) {
17404e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
17414e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
17429b8d05b8SZbigniew Bodek 		return (EINVAL);
17439b8d05b8SZbigniew Bodek 	} else {
17449b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
17459b8d05b8SZbigniew Bodek 	}
17469b8d05b8SZbigniew Bodek 
17479b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
17489b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
17499b8d05b8SZbigniew Bodek 
17503f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
17519b8d05b8SZbigniew Bodek 			continue;
17529b8d05b8SZbigniew Bodek 
17539b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
17549b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
17553f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
1756469a8407SMarcin Wojtas 			rc = ENOMEM;
17579b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
17589b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
17599b8d05b8SZbigniew Bodek 			goto err;
17609b8d05b8SZbigniew Bodek 		}
17619b8d05b8SZbigniew Bodek 
17620bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
17635cb9db07SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, irq->handler, NULL,
17645cb9db07SMarcin Wojtas 		    irq->data, &irq->cookie);
17653f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
17669b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
17679b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
17689b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
17699b8d05b8SZbigniew Bodek 			goto err;
17709b8d05b8SZbigniew Bodek 		}
17719b8d05b8SZbigniew Bodek 		irq->requested = true;
17729b8d05b8SZbigniew Bodek 
1773*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_INFO, "queue %d - cpu %d\n",
17749b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
17759b8d05b8SZbigniew Bodek 	}
17769b8d05b8SZbigniew Bodek 
17779b8d05b8SZbigniew Bodek 	return (rc);
17789b8d05b8SZbigniew Bodek 
17799b8d05b8SZbigniew Bodek err:
17809b8d05b8SZbigniew Bodek 
17819b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
17829b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
17839b8d05b8SZbigniew Bodek 		rcc = 0;
17849b8d05b8SZbigniew Bodek 
17859b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
17869b8d05b8SZbigniew Bodek 		   free both intr and resources */
17870bdffe59SMarcin Wojtas 		if (irq->requested)
17889b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
17893f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
17909b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
17919b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
17929b8d05b8SZbigniew Bodek 
17939b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
17949b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
17959b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
17969b8d05b8SZbigniew Bodek 		   this iteration */
17979b8d05b8SZbigniew Bodek 		rcc = 0;
17989b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
17999b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
18009b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
18019b8d05b8SZbigniew Bodek 		}
18023f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
18039b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
18049b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
18059b8d05b8SZbigniew Bodek 		irq->requested = false;
18069b8d05b8SZbigniew Bodek 		irq->res = NULL;
18079b8d05b8SZbigniew Bodek 	}
18089b8d05b8SZbigniew Bodek 
18099b8d05b8SZbigniew Bodek 	return (rc);
18109b8d05b8SZbigniew Bodek }
18119b8d05b8SZbigniew Bodek 
18129b8d05b8SZbigniew Bodek static void
18139b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
18149b8d05b8SZbigniew Bodek {
18159b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
18169b8d05b8SZbigniew Bodek 	int rc;
18179b8d05b8SZbigniew Bodek 
18189b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
18199b8d05b8SZbigniew Bodek 	if (irq->requested) {
1820*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
18219b8d05b8SZbigniew Bodek 		    irq->vector);
18229b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
18233f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
18249b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
18259b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
18269b8d05b8SZbigniew Bodek 		irq->requested = 0;
18279b8d05b8SZbigniew Bodek 	}
18289b8d05b8SZbigniew Bodek 
18299b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
1830*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
18319b8d05b8SZbigniew Bodek 		    irq->vector);
18329b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
18339b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
18349b8d05b8SZbigniew Bodek 		irq->res = NULL;
18353f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
18369b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
18379b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
18389b8d05b8SZbigniew Bodek 	}
18399b8d05b8SZbigniew Bodek }
18409b8d05b8SZbigniew Bodek 
18419b8d05b8SZbigniew Bodek static void
18429b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
18439b8d05b8SZbigniew Bodek {
18449b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
18459b8d05b8SZbigniew Bodek 	int rc;
18469b8d05b8SZbigniew Bodek 
18479b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
18489b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
18499b8d05b8SZbigniew Bodek 		if (irq->requested) {
1850*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
18519b8d05b8SZbigniew Bodek 			    irq->vector);
18529b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
18539b8d05b8SZbigniew Bodek 			    irq->cookie);
18543f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
18559b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
18569b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
18579b8d05b8SZbigniew Bodek 			}
18589b8d05b8SZbigniew Bodek 			irq->requested = 0;
18599b8d05b8SZbigniew Bodek 		}
18609b8d05b8SZbigniew Bodek 
18619b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
1862*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
18639b8d05b8SZbigniew Bodek 			    irq->vector);
18649b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
18659b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
18669b8d05b8SZbigniew Bodek 			irq->res = NULL;
18673f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
18689b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
18699b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
18709b8d05b8SZbigniew Bodek 				    irq->vector);
18719b8d05b8SZbigniew Bodek 			}
18729b8d05b8SZbigniew Bodek 		}
18739b8d05b8SZbigniew Bodek 	}
18749b8d05b8SZbigniew Bodek }
18759b8d05b8SZbigniew Bodek 
18769b8d05b8SZbigniew Bodek static void
18779b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
18789b8d05b8SZbigniew Bodek {
18799b8d05b8SZbigniew Bodek 
18809b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
18819b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
18829b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
18839b8d05b8SZbigniew Bodek }
18849b8d05b8SZbigniew Bodek 
18859b8d05b8SZbigniew Bodek static void
18869b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
18879b8d05b8SZbigniew Bodek {
18889b8d05b8SZbigniew Bodek 
1889fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1890fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
18919b8d05b8SZbigniew Bodek 		pci_release_msi(adapter->pdev);
1892fd43fd2aSMarcin Wojtas 	}
18939b8d05b8SZbigniew Bodek 
18949b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
1895fd43fd2aSMarcin Wojtas 	if (adapter->msix_entries != NULL)
1896cd5d5804SMarcin Wojtas 		free(adapter->msix_entries, M_DEVBUF);
18979b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
18989b8d05b8SZbigniew Bodek }
18999b8d05b8SZbigniew Bodek 
19009b8d05b8SZbigniew Bodek static void
19019b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
19029b8d05b8SZbigniew Bodek {
19039b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
19049b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
19059b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
19069b8d05b8SZbigniew Bodek 	int i;
19079b8d05b8SZbigniew Bodek 
19089b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
19097d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
19109b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
19119b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
19129b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
19139b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
19149b8d05b8SZbigniew Bodek 	}
19159b8d05b8SZbigniew Bodek }
19169b8d05b8SZbigniew Bodek 
19179b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
19180bdffe59SMarcin Wojtas static int
19190bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
19209b8d05b8SZbigniew Bodek {
19219b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
19229b8d05b8SZbigniew Bodek 	int rc;
19239b8d05b8SZbigniew Bodek 
192456d41ad5SMarcin Wojtas 	/* In case the RSS table was destroyed */
192556d41ad5SMarcin Wojtas 	if (!ena_dev->rss.tbl_log_size) {
192656d41ad5SMarcin Wojtas 		rc = ena_rss_init_default(adapter);
192756d41ad5SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
192856d41ad5SMarcin Wojtas 			device_printf(adapter->pdev,
192956d41ad5SMarcin Wojtas 			    "WARNING: RSS was not properly re-initialized,"
193056d41ad5SMarcin Wojtas 			    " it will affect bandwidth\n");
193156d41ad5SMarcin Wojtas 			ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
193256d41ad5SMarcin Wojtas 			return (rc);
193356d41ad5SMarcin Wojtas 		}
193456d41ad5SMarcin Wojtas 	}
193556d41ad5SMarcin Wojtas 
19369b8d05b8SZbigniew Bodek 	/* Set indirect table */
19379b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
19380bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
19390bdffe59SMarcin Wojtas 		return (rc);
19409b8d05b8SZbigniew Bodek 
19419b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
19429b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
19430bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
19440bdffe59SMarcin Wojtas 		return (rc);
19459b8d05b8SZbigniew Bodek 
19469b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
19479b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
19480bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
19490bdffe59SMarcin Wojtas 		return (rc);
19509b8d05b8SZbigniew Bodek 
19510bdffe59SMarcin Wojtas 	return (0);
19529b8d05b8SZbigniew Bodek }
19539b8d05b8SZbigniew Bodek 
19549b8d05b8SZbigniew Bodek static int
19559b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
19569b8d05b8SZbigniew Bodek {
19579b8d05b8SZbigniew Bodek 	int rc;
19589b8d05b8SZbigniew Bodek 
1959fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) {
19609b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
196156d41ad5SMarcin Wojtas 		if (rc != 0) {
196256d41ad5SMarcin Wojtas 			device_printf(adapter->pdev,
196356d41ad5SMarcin Wojtas 			    "Failed to configure RSS\n");
19649b8d05b8SZbigniew Bodek 			return (rc);
19659b8d05b8SZbigniew Bodek 		}
196656d41ad5SMarcin Wojtas 	}
19679b8d05b8SZbigniew Bodek 
19687d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
19693f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
19707d2544e6SMarcin Wojtas 		return (rc);
19717d2544e6SMarcin Wojtas 
19729b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
197330217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
197430217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
19759b8d05b8SZbigniew Bodek 
19769b8d05b8SZbigniew Bodek 	return (0);
19779b8d05b8SZbigniew Bodek }
19789b8d05b8SZbigniew Bodek 
19799762a033SMarcin Wojtas static void
19809762a033SMarcin Wojtas set_io_rings_size(struct ena_adapter *adapter, int new_tx_size,
19819762a033SMarcin Wojtas     int new_rx_size)
19829762a033SMarcin Wojtas {
19839762a033SMarcin Wojtas 	int i;
19849762a033SMarcin Wojtas 
19859762a033SMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
19869762a033SMarcin Wojtas 		adapter->tx_ring[i].ring_size = new_tx_size;
19879762a033SMarcin Wojtas 		adapter->rx_ring[i].ring_size = new_rx_size;
19889762a033SMarcin Wojtas 	}
19899762a033SMarcin Wojtas }
19909762a033SMarcin Wojtas 
19919762a033SMarcin Wojtas static int
19929762a033SMarcin Wojtas create_queues_with_size_backoff(struct ena_adapter *adapter)
19939762a033SMarcin Wojtas {
19949762a033SMarcin Wojtas 	int rc;
19959762a033SMarcin Wojtas 	uint32_t cur_rx_ring_size, cur_tx_ring_size;
19969762a033SMarcin Wojtas 	uint32_t new_rx_ring_size, new_tx_ring_size;
19979762a033SMarcin Wojtas 
19989762a033SMarcin Wojtas 	/*
19999762a033SMarcin Wojtas 	 * Current queue sizes might be set to smaller than the requested
20009762a033SMarcin Wojtas 	 * ones due to past queue allocation failures.
20019762a033SMarcin Wojtas 	 */
20029762a033SMarcin Wojtas 	set_io_rings_size(adapter, adapter->requested_tx_ring_size,
20039762a033SMarcin Wojtas 	    adapter->requested_rx_ring_size);
20049762a033SMarcin Wojtas 
20059762a033SMarcin Wojtas 	while (1) {
20069762a033SMarcin Wojtas 		/* Allocate transmit descriptors */
20079762a033SMarcin Wojtas 		rc = ena_setup_all_tx_resources(adapter);
20089762a033SMarcin Wojtas 		if (unlikely(rc != 0)) {
2009*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT, "err_setup_tx\n");
20109762a033SMarcin Wojtas 			goto err_setup_tx;
20119762a033SMarcin Wojtas 		}
20129762a033SMarcin Wojtas 
20139762a033SMarcin Wojtas 		/* Allocate receive descriptors */
20149762a033SMarcin Wojtas 		rc = ena_setup_all_rx_resources(adapter);
20159762a033SMarcin Wojtas 		if (unlikely(rc != 0)) {
2016*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT, "err_setup_rx\n");
20179762a033SMarcin Wojtas 			goto err_setup_rx;
20189762a033SMarcin Wojtas 		}
20199762a033SMarcin Wojtas 
20209762a033SMarcin Wojtas 		/* Create IO queues for Rx & Tx */
20219762a033SMarcin Wojtas 		rc = ena_create_io_queues(adapter);
20229762a033SMarcin Wojtas 		if (unlikely(rc != 0)) {
2023*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT,
20249762a033SMarcin Wojtas 			    "create IO queues failed\n");
20259762a033SMarcin Wojtas 			goto err_io_que;
20269762a033SMarcin Wojtas 		}
20279762a033SMarcin Wojtas 
20289762a033SMarcin Wojtas 		return (0);
20299762a033SMarcin Wojtas 
20309762a033SMarcin Wojtas err_io_que:
20319762a033SMarcin Wojtas 		ena_free_all_rx_resources(adapter);
20329762a033SMarcin Wojtas err_setup_rx:
20339762a033SMarcin Wojtas 		ena_free_all_tx_resources(adapter);
20349762a033SMarcin Wojtas err_setup_tx:
20359762a033SMarcin Wojtas 		/*
20369762a033SMarcin Wojtas 		 * Lower the ring size if ENOMEM. Otherwise, return the
20379762a033SMarcin Wojtas 		 * error straightaway.
20389762a033SMarcin Wojtas 		 */
20399762a033SMarcin Wojtas 		if (unlikely(rc != ENOMEM)) {
2040*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT,
20419762a033SMarcin Wojtas 			    "Queue creation failed with error code: %d\n", rc);
20429762a033SMarcin Wojtas 			return (rc);
20439762a033SMarcin Wojtas 		}
20449762a033SMarcin Wojtas 
20459762a033SMarcin Wojtas 		cur_tx_ring_size = adapter->tx_ring[0].ring_size;
20469762a033SMarcin Wojtas 		cur_rx_ring_size = adapter->rx_ring[0].ring_size;
20479762a033SMarcin Wojtas 
20489762a033SMarcin Wojtas 		device_printf(adapter->pdev,
20499762a033SMarcin Wojtas 		    "Not enough memory to create queues with sizes TX=%d, RX=%d\n",
20509762a033SMarcin Wojtas 		    cur_tx_ring_size, cur_rx_ring_size);
20519762a033SMarcin Wojtas 
20529762a033SMarcin Wojtas 		new_tx_ring_size = cur_tx_ring_size;
20539762a033SMarcin Wojtas 		new_rx_ring_size = cur_rx_ring_size;
20549762a033SMarcin Wojtas 
20559762a033SMarcin Wojtas 		/*
20569762a033SMarcin Wojtas 		 * Decrease the size of a larger queue, or decrease both if they are
20579762a033SMarcin Wojtas 		 * the same size.
20589762a033SMarcin Wojtas 		 */
20599762a033SMarcin Wojtas 		if (cur_rx_ring_size <= cur_tx_ring_size)
20609762a033SMarcin Wojtas 			new_tx_ring_size = cur_tx_ring_size / 2;
20619762a033SMarcin Wojtas 		if (cur_rx_ring_size >= cur_tx_ring_size)
20629762a033SMarcin Wojtas 			new_rx_ring_size = cur_rx_ring_size / 2;
20639762a033SMarcin Wojtas 
20649762a033SMarcin Wojtas 		if (new_tx_ring_size < ENA_MIN_RING_SIZE ||
20659762a033SMarcin Wojtas 		    new_rx_ring_size < ENA_MIN_RING_SIZE) {
20669762a033SMarcin Wojtas 			device_printf(adapter->pdev,
20679762a033SMarcin Wojtas 			    "Queue creation failed with the smallest possible queue size"
20689762a033SMarcin Wojtas 			    "of %d for both queues. Not retrying with smaller queues\n",
20699762a033SMarcin Wojtas 			    ENA_MIN_RING_SIZE);
20709762a033SMarcin Wojtas 			return (rc);
20719762a033SMarcin Wojtas 		}
20729762a033SMarcin Wojtas 
20739762a033SMarcin Wojtas 		set_io_rings_size(adapter, new_tx_ring_size, new_rx_ring_size);
20749762a033SMarcin Wojtas 	}
20759762a033SMarcin Wojtas }
20769762a033SMarcin Wojtas 
207738c7b965SMarcin Wojtas int
20789b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
20799b8d05b8SZbigniew Bodek {
20809b8d05b8SZbigniew Bodek 	int rc = 0;
20819b8d05b8SZbigniew Bodek 
20823f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
20839b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
20849b8d05b8SZbigniew Bodek 		return (ENXIO);
20859b8d05b8SZbigniew Bodek 	}
20869b8d05b8SZbigniew Bodek 
2087579d23aaSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
2088579d23aaSMarcin Wojtas 		return (0);
2089579d23aaSMarcin Wojtas 
20909b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "device is going UP\n");
20919b8d05b8SZbigniew Bodek 
20929b8d05b8SZbigniew Bodek 	/* setup interrupts for IO queues */
209377958fcdSMarcin Wojtas 	rc = ena_setup_io_intr(adapter);
209477958fcdSMarcin Wojtas 	if (unlikely(rc != 0)) {
2095*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "error setting up IO interrupt\n");
209677958fcdSMarcin Wojtas 		goto error;
209777958fcdSMarcin Wojtas 	}
20989b8d05b8SZbigniew Bodek 	rc = ena_request_io_irq(adapter);
20993f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2100*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "err_req_irq\n");
210177958fcdSMarcin Wojtas 		goto error;
21029b8d05b8SZbigniew Bodek 	}
21039b8d05b8SZbigniew Bodek 
21047d8c4feeSMarcin Wojtas 	device_printf(adapter->pdev,
21059762a033SMarcin Wojtas 	    "Creating %u IO queues. Rx queue size: %d, Tx queue size: %d, "
21069762a033SMarcin Wojtas 	    "LLQ is %s\n",
21077d8c4feeSMarcin Wojtas 	    adapter->num_io_queues,
21089762a033SMarcin Wojtas 	    adapter->requested_rx_ring_size,
21099762a033SMarcin Wojtas 	    adapter->requested_tx_ring_size,
21109762a033SMarcin Wojtas 	    (adapter->ena_dev->tx_mem_queue_type ==
21119762a033SMarcin Wojtas 	        ENA_ADMIN_PLACEMENT_POLICY_DEV) ?  "ENABLED" : "DISABLED");
21127d8c4feeSMarcin Wojtas 
21139762a033SMarcin Wojtas 	rc = create_queues_with_size_backoff(adapter);
21143f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2115*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT,
21169762a033SMarcin Wojtas 		    "error creating queues with size backoff\n");
21179762a033SMarcin Wojtas 		goto err_create_queues_with_backoff;
21189b8d05b8SZbigniew Bodek 	}
21199b8d05b8SZbigniew Bodek 
2120fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
21219b8d05b8SZbigniew Bodek 		if_link_state_change(adapter->ifp, LINK_STATE_UP);
21229b8d05b8SZbigniew Bodek 
21239b8d05b8SZbigniew Bodek 	rc = ena_up_complete(adapter);
21243f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
21259b8d05b8SZbigniew Bodek 		goto err_up_complete;
21269b8d05b8SZbigniew Bodek 
21279b8d05b8SZbigniew Bodek 	counter_u64_add(adapter->dev_stats.interface_up, 1);
21289b8d05b8SZbigniew Bodek 
21299b8d05b8SZbigniew Bodek 	ena_update_hwassist(adapter);
21309b8d05b8SZbigniew Bodek 
21319b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
21329b8d05b8SZbigniew Bodek 		IFF_DRV_OACTIVE);
21339b8d05b8SZbigniew Bodek 
213424392281SMarcin Wojtas 	/* Activate timer service only if the device is running.
213524392281SMarcin Wojtas 		* If this flag is not set, it means that the driver is being
213624392281SMarcin Wojtas 		* reset and timer service will be activated afterwards.
213724392281SMarcin Wojtas 		*/
213824392281SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)) {
213924392281SMarcin Wojtas 		callout_reset_sbt(&adapter->timer_service, SBT_1S,
214024392281SMarcin Wojtas 			SBT_1S, ena_timer_service, (void *)adapter, 0);
214124392281SMarcin Wojtas 	}
21429b8d05b8SZbigniew Bodek 
2143fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP, adapter);
214493471047SZbigniew Bodek 
214593471047SZbigniew Bodek 	ena_unmask_all_io_irqs(adapter);
21469b8d05b8SZbigniew Bodek 
21479b8d05b8SZbigniew Bodek 	return (0);
21489b8d05b8SZbigniew Bodek 
21499b8d05b8SZbigniew Bodek err_up_complete:
21509b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
21519b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
21529b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
21539762a033SMarcin Wojtas err_create_queues_with_backoff:
21549b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
215577958fcdSMarcin Wojtas error:
21569b8d05b8SZbigniew Bodek 	return (rc);
21579b8d05b8SZbigniew Bodek }
21589b8d05b8SZbigniew Bodek 
21599b8d05b8SZbigniew Bodek static uint64_t
21609b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
21619b8d05b8SZbigniew Bodek {
21629b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
21639b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
21649b8d05b8SZbigniew Bodek 
21659b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
21669b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
21679b8d05b8SZbigniew Bodek 
21689b8d05b8SZbigniew Bodek 	switch (cnt) {
21699b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
217030217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
21719b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
217230217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
21739b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
217430217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
21759b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
217630217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
21779b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
217830217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
21796c84cec3SMarcin Wojtas 	case IFCOUNTER_OQDROPS:
21806c84cec3SMarcin Wojtas 		return (counter_u64_fetch(stats->tx_drops));
21819b8d05b8SZbigniew Bodek 	default:
21829b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
21839b8d05b8SZbigniew Bodek 	}
21849b8d05b8SZbigniew Bodek }
21859b8d05b8SZbigniew Bodek 
21869b8d05b8SZbigniew Bodek static int
21879b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
21889b8d05b8SZbigniew Bodek {
21899b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
21909b8d05b8SZbigniew Bodek 	return (0);
21919b8d05b8SZbigniew Bodek }
21929b8d05b8SZbigniew Bodek 
21939b8d05b8SZbigniew Bodek static void
21949b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
21959b8d05b8SZbigniew Bodek {
21969b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
2197*9eb1615fSMarcin Wojtas 	ena_trace(NULL, ENA_DBG, "enter\n");
21989b8d05b8SZbigniew Bodek 
21996959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
22009b8d05b8SZbigniew Bodek 
22019b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
22029b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
22039b8d05b8SZbigniew Bodek 
2204fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) {
22056959869eSMarcin Wojtas 		ENA_LOCK_UNLOCK(adapter);
2206*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_INFO, "Link is down\n");
22079b8d05b8SZbigniew Bodek 		return;
22089b8d05b8SZbigniew Bodek 	}
22099b8d05b8SZbigniew Bodek 
22109b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
2211b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
22129b8d05b8SZbigniew Bodek 
22136959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
22149b8d05b8SZbigniew Bodek }
22159b8d05b8SZbigniew Bodek 
22169b8d05b8SZbigniew Bodek static void
22179b8d05b8SZbigniew Bodek ena_init(void *arg)
22189b8d05b8SZbigniew Bodek {
22199b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
22209b8d05b8SZbigniew Bodek 
2221fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
22226959869eSMarcin Wojtas 		ENA_LOCK_LOCK(adapter);
22239b8d05b8SZbigniew Bodek 		ena_up(adapter);
22246959869eSMarcin Wojtas 		ENA_LOCK_UNLOCK(adapter);
22253d3a90f9SZbigniew Bodek 	}
22269b8d05b8SZbigniew Bodek }
22279b8d05b8SZbigniew Bodek 
22289b8d05b8SZbigniew Bodek static int
22299b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
22309b8d05b8SZbigniew Bodek {
22319b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
22329b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
22339b8d05b8SZbigniew Bodek 	int rc;
22349b8d05b8SZbigniew Bodek 
22359b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
22369b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
22379b8d05b8SZbigniew Bodek 
22389b8d05b8SZbigniew Bodek 	/*
22399b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
22409b8d05b8SZbigniew Bodek 	 */
22419b8d05b8SZbigniew Bodek 	rc = 0;
22429b8d05b8SZbigniew Bodek 	switch (command) {
22439b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2244dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
2245dbf2eb54SMarcin Wojtas 			break;
22466959869eSMarcin Wojtas 		ENA_LOCK_LOCK(adapter);
22479b8d05b8SZbigniew Bodek 		ena_down(adapter);
22489b8d05b8SZbigniew Bodek 
22499b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
22509b8d05b8SZbigniew Bodek 
22519b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
22526959869eSMarcin Wojtas 		ENA_LOCK_UNLOCK(adapter);
22539b8d05b8SZbigniew Bodek 		break;
22549b8d05b8SZbigniew Bodek 
22559b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
22560bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
22570bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
22580bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
22590bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
22609b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
22619b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
22629b8d05b8SZbigniew Bodek 				}
22639b8d05b8SZbigniew Bodek 			} else {
22646959869eSMarcin Wojtas 				ENA_LOCK_LOCK(adapter);
22659b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
22666959869eSMarcin Wojtas 				ENA_LOCK_UNLOCK(adapter);
22679b8d05b8SZbigniew Bodek 			}
22689b8d05b8SZbigniew Bodek 		} else {
22690bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
22706959869eSMarcin Wojtas 				ENA_LOCK_LOCK(adapter);
22719b8d05b8SZbigniew Bodek 				ena_down(adapter);
22726959869eSMarcin Wojtas 				ENA_LOCK_UNLOCK(adapter);
2273e67c6554SZbigniew Bodek 			}
22749b8d05b8SZbigniew Bodek 		}
22759b8d05b8SZbigniew Bodek 		break;
22769b8d05b8SZbigniew Bodek 
22779b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
22789b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
22799b8d05b8SZbigniew Bodek 		break;
22809b8d05b8SZbigniew Bodek 
22819b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
22829b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
22839b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
22849b8d05b8SZbigniew Bodek 		break;
22859b8d05b8SZbigniew Bodek 
22869b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
22879b8d05b8SZbigniew Bodek 		{
22889b8d05b8SZbigniew Bodek 			int reinit = 0;
22899b8d05b8SZbigniew Bodek 
22909b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
22919b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
22929b8d05b8SZbigniew Bodek 				reinit = 1;
22939b8d05b8SZbigniew Bodek 			}
22949b8d05b8SZbigniew Bodek 
22950bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
22960bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
22976959869eSMarcin Wojtas 				ENA_LOCK_LOCK(adapter);
22989b8d05b8SZbigniew Bodek 				ena_down(adapter);
22999b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
23006959869eSMarcin Wojtas 				ENA_LOCK_UNLOCK(adapter);
23019b8d05b8SZbigniew Bodek 			}
23029b8d05b8SZbigniew Bodek 		}
23039b8d05b8SZbigniew Bodek 
23049b8d05b8SZbigniew Bodek 		break;
23059b8d05b8SZbigniew Bodek 	default:
23069b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
23079b8d05b8SZbigniew Bodek 		break;
23089b8d05b8SZbigniew Bodek 	}
23099b8d05b8SZbigniew Bodek 
23109b8d05b8SZbigniew Bodek 	return (rc);
23119b8d05b8SZbigniew Bodek }
23129b8d05b8SZbigniew Bodek 
23139b8d05b8SZbigniew Bodek static int
23149b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
23159b8d05b8SZbigniew Bodek {
23169b8d05b8SZbigniew Bodek 	int caps = 0;
23179b8d05b8SZbigniew Bodek 
23180bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
23199b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
23209b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
23210bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
23229b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
23239b8d05b8SZbigniew Bodek 
23240bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
23259b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
23260bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
23279b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
23289b8d05b8SZbigniew Bodek 
23290bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
23300bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
23319b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
23329b8d05b8SZbigniew Bodek 
23330bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
23340bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
23359b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
23369b8d05b8SZbigniew Bodek 
23370bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
23389b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
23390bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
23409b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
23419b8d05b8SZbigniew Bodek 
23420bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
23430bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
23449b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
23459b8d05b8SZbigniew Bodek 
23469b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
23479b8d05b8SZbigniew Bodek 
23489b8d05b8SZbigniew Bodek 	return (caps);
23499b8d05b8SZbigniew Bodek }
23509b8d05b8SZbigniew Bodek 
23519b8d05b8SZbigniew Bodek static void
23529b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
23539b8d05b8SZbigniew Bodek {
23549b8d05b8SZbigniew Bodek 
23559b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
23569b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
23579b8d05b8SZbigniew Bodek }
23589b8d05b8SZbigniew Bodek 
23599b8d05b8SZbigniew Bodek static void
23609b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
23619b8d05b8SZbigniew Bodek {
23629b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
23639b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
23649b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
23659b8d05b8SZbigniew Bodek 	int flags = 0;
23669b8d05b8SZbigniew Bodek 
23679b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
23689b8d05b8SZbigniew Bodek 
23690bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
23700bdffe59SMarcin Wojtas 		if ((feat &
23710bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
23729b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
23730bdffe59SMarcin Wojtas 		if ((feat &
23749b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
23750bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
23769b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
23779b8d05b8SZbigniew Bodek 	}
23789b8d05b8SZbigniew Bodek 
23790bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
23809b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
23819b8d05b8SZbigniew Bodek 
23820bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
23839b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
23849b8d05b8SZbigniew Bodek 
23850bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
23869b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
23879b8d05b8SZbigniew Bodek 
23889b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
23899b8d05b8SZbigniew Bodek }
23909b8d05b8SZbigniew Bodek 
23919b8d05b8SZbigniew Bodek static int
23929b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
23939b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
23949b8d05b8SZbigniew Bodek {
23959b8d05b8SZbigniew Bodek 	if_t ifp;
23969b8d05b8SZbigniew Bodek 	int caps = 0;
23979b8d05b8SZbigniew Bodek 
23989b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
23993f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
2400*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "can not allocate ifnet structure\n");
24019b8d05b8SZbigniew Bodek 		return (ENXIO);
24029b8d05b8SZbigniew Bodek 	}
24039b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
24049b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
24059b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
24069b8d05b8SZbigniew Bodek 
240792dc69a7SMarcin Wojtas 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
240892dc69a7SMarcin Wojtas 	    IFF_KNOWSEPOCH);
24099b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
24109b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
24119b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
24129b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
24139b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
24149b8d05b8SZbigniew Bodek 
24159762a033SMarcin Wojtas 	if_setsendqlen(ifp, adapter->requested_tx_ring_size);
24169b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
24179b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
24189b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
24199b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
24209b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
24219b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
24229b8d05b8SZbigniew Bodek 	/* check hardware support */
24239b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
24249b8d05b8SZbigniew Bodek 	/* ... and set them */
24259b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
24269b8d05b8SZbigniew Bodek 
24279b8d05b8SZbigniew Bodek 	/* TSO parameters */
24288a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
24298a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
24308a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
24318a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
24329b8d05b8SZbigniew Bodek 
24339b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
24349b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
24359b8d05b8SZbigniew Bodek 
24369b8d05b8SZbigniew Bodek 	/*
24379b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
24389b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
24399b8d05b8SZbigniew Bodek 	 */
24409b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
24419b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
24429b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
24439b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
24449b8d05b8SZbigniew Bodek 
24459b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
24469b8d05b8SZbigniew Bodek 
24479b8d05b8SZbigniew Bodek 	return (0);
24489b8d05b8SZbigniew Bodek }
24499b8d05b8SZbigniew Bodek 
245038c7b965SMarcin Wojtas void
24519b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
24529b8d05b8SZbigniew Bodek {
2453a195fab0SMarcin Wojtas 	int rc;
24549b8d05b8SZbigniew Bodek 
2455579d23aaSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
2456579d23aaSMarcin Wojtas 		return;
2457579d23aaSMarcin Wojtas 
24589b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "device is going DOWN\n");
24599b8d05b8SZbigniew Bodek 
24609b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
24619b8d05b8SZbigniew Bodek 
2462fd43fd2aSMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP, adapter);
24639b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
24649b8d05b8SZbigniew Bodek 		IFF_DRV_RUNNING);
24659b8d05b8SZbigniew Bodek 
24669b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
24679b8d05b8SZbigniew Bodek 
2468fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) {
2469a195fab0SMarcin Wojtas 		rc = ena_com_dev_reset(adapter->ena_dev,
2470a195fab0SMarcin Wojtas 			adapter->reset_reason);
24713f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
2472a195fab0SMarcin Wojtas 			device_printf(adapter->pdev,
2473a195fab0SMarcin Wojtas 				"Device reset failed\n");
2474a195fab0SMarcin Wojtas 	}
2475a195fab0SMarcin Wojtas 
24769b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
24779b8d05b8SZbigniew Bodek 
24789b8d05b8SZbigniew Bodek 	ena_free_all_tx_bufs(adapter);
24799b8d05b8SZbigniew Bodek 	ena_free_all_rx_bufs(adapter);
24809b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
24819b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
24829b8d05b8SZbigniew Bodek 
24839b8d05b8SZbigniew Bodek 	counter_u64_add(adapter->dev_stats.interface_down, 1);
24849b8d05b8SZbigniew Bodek }
24859b8d05b8SZbigniew Bodek 
24867d8c4feeSMarcin Wojtas static uint32_t
24877d8c4feeSMarcin Wojtas ena_calc_max_io_queue_num(device_t pdev, struct ena_com_dev *ena_dev,
24889b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
24899b8d05b8SZbigniew Bodek {
24907d8c4feeSMarcin Wojtas 	uint32_t io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
24919b8d05b8SZbigniew Bodek 
24926064f289SMarcin Wojtas 	/* Regular queues capabilities */
24936064f289SMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
24946064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
24956064f289SMarcin Wojtas 		    &get_feat_ctx->max_queue_ext.max_queue_ext;
24964fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, max_queue_ext->max_rx_sq_num,
24974fa9e02dSMarcin Wojtas 			max_queue_ext->max_rx_cq_num);
24986064f289SMarcin Wojtas 
24994fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queue_ext->max_tx_sq_num;
25004fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queue_ext->max_tx_cq_num;
25016064f289SMarcin Wojtas 	} else {
25026064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
25036064f289SMarcin Wojtas 		    &get_feat_ctx->max_queues;
25044fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queues->max_sq_num;
25054fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queues->max_cq_num;
25064fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, io_tx_sq_num, io_tx_cq_num);
25076064f289SMarcin Wojtas 	}
25089b8d05b8SZbigniew Bodek 
25094fa9e02dSMarcin Wojtas 	/* In case of LLQ use the llq fields for the tx SQ/CQ */
25104fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
25114fa9e02dSMarcin Wojtas 		io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
25124fa9e02dSMarcin Wojtas 
25137d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
25147d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_rx_num);
25157d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_tx_sq_num);
25167d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_tx_cq_num);
25179b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
25187d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues,
25197d8c4feeSMarcin Wojtas 	    pci_msix_count(pdev) - 1);
25209b8d05b8SZbigniew Bodek 
25217d8c4feeSMarcin Wojtas 	return (max_num_io_queues);
25229b8d05b8SZbigniew Bodek }
25239b8d05b8SZbigniew Bodek 
25240bdffe59SMarcin Wojtas static int
25254fa9e02dSMarcin Wojtas ena_enable_wc(struct resource *res)
25264fa9e02dSMarcin Wojtas {
2527472d4784SMarcin Wojtas #if defined(__i386) || defined(__amd64) || defined(__aarch64__)
25284fa9e02dSMarcin Wojtas 	vm_offset_t va;
25294fa9e02dSMarcin Wojtas 	vm_size_t len;
25304fa9e02dSMarcin Wojtas 	int rc;
25314fa9e02dSMarcin Wojtas 
25324fa9e02dSMarcin Wojtas 	va = (vm_offset_t)rman_get_virtual(res);
25334fa9e02dSMarcin Wojtas 	len = rman_get_size(res);
25344fa9e02dSMarcin Wojtas 	/* Enable write combining */
2535472d4784SMarcin Wojtas 	rc = pmap_change_attr(va, len, VM_MEMATTR_WRITE_COMBINING);
25364fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
2537*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "pmap_change_attr failed, %d\n", rc);
25384fa9e02dSMarcin Wojtas 		return (rc);
25394fa9e02dSMarcin Wojtas 	}
25404fa9e02dSMarcin Wojtas 
25414fa9e02dSMarcin Wojtas 	return (0);
25424fa9e02dSMarcin Wojtas #endif
25434fa9e02dSMarcin Wojtas 	return (EOPNOTSUPP);
25444fa9e02dSMarcin Wojtas }
25454fa9e02dSMarcin Wojtas 
25464fa9e02dSMarcin Wojtas static int
25474fa9e02dSMarcin Wojtas ena_set_queues_placement_policy(device_t pdev, struct ena_com_dev *ena_dev,
25484fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *llq,
25494fa9e02dSMarcin Wojtas     struct ena_llq_configurations *llq_default_configurations)
25504fa9e02dSMarcin Wojtas {
25514fa9e02dSMarcin Wojtas 	struct ena_adapter *adapter = device_get_softc(pdev);
25524fa9e02dSMarcin Wojtas 	int rc, rid;
25534fa9e02dSMarcin Wojtas 	uint32_t llq_feature_mask;
25544fa9e02dSMarcin Wojtas 
25554fa9e02dSMarcin Wojtas 	llq_feature_mask = 1 << ENA_ADMIN_LLQ;
25564fa9e02dSMarcin Wojtas 	if (!(ena_dev->supported_features & llq_feature_mask)) {
25574fa9e02dSMarcin Wojtas 		device_printf(pdev,
25584fa9e02dSMarcin Wojtas 		    "LLQ is not supported. Fallback to host mode policy.\n");
25594fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
25604fa9e02dSMarcin Wojtas 		return (0);
25614fa9e02dSMarcin Wojtas 	}
25624fa9e02dSMarcin Wojtas 
25634fa9e02dSMarcin Wojtas 	rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
25644fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
25654fa9e02dSMarcin Wojtas 		device_printf(pdev, "Failed to configure the device mode. "
25664fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
25674fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
25684fa9e02dSMarcin Wojtas 		return (0);
25694fa9e02dSMarcin Wojtas 	}
25704fa9e02dSMarcin Wojtas 
25714fa9e02dSMarcin Wojtas 	/* Nothing to config, exit */
25724fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
25734fa9e02dSMarcin Wojtas 		return (0);
25744fa9e02dSMarcin Wojtas 
25754fa9e02dSMarcin Wojtas 	/* Try to allocate resources for LLQ bar */
25764fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_MEM_BAR);
25774fa9e02dSMarcin Wojtas 	adapter->memory = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
25784fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
25794fa9e02dSMarcin Wojtas 	if (unlikely(adapter->memory == NULL)) {
25804fa9e02dSMarcin Wojtas 		device_printf(pdev, "unable to allocate LLQ bar resource. "
25814fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
25824fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
25834fa9e02dSMarcin Wojtas 		return (0);
25844fa9e02dSMarcin Wojtas 	}
25854fa9e02dSMarcin Wojtas 
25864fa9e02dSMarcin Wojtas 	/* Enable write combining for better LLQ performance */
25874fa9e02dSMarcin Wojtas 	rc = ena_enable_wc(adapter->memory);
25884fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
25894fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to enable write combining.\n");
25904fa9e02dSMarcin Wojtas 		return (rc);
25914fa9e02dSMarcin Wojtas 	}
25924fa9e02dSMarcin Wojtas 
25934fa9e02dSMarcin Wojtas 	/*
25944fa9e02dSMarcin Wojtas 	 * Save virtual address of the device's memory region
25954fa9e02dSMarcin Wojtas 	 * for the ena_com layer.
25964fa9e02dSMarcin Wojtas 	 */
25974fa9e02dSMarcin Wojtas 	ena_dev->mem_bar = rman_get_virtual(adapter->memory);
25984fa9e02dSMarcin Wojtas 
25994fa9e02dSMarcin Wojtas 	return (0);
26004fa9e02dSMarcin Wojtas }
26014fa9e02dSMarcin Wojtas 
26024fa9e02dSMarcin Wojtas static inline
26034fa9e02dSMarcin Wojtas void set_default_llq_configurations(struct ena_llq_configurations *llq_config)
26044fa9e02dSMarcin Wojtas {
26054fa9e02dSMarcin Wojtas 	llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
26064fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
26074fa9e02dSMarcin Wojtas 	llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
26084fa9e02dSMarcin Wojtas 	llq_config->llq_num_decs_before_header =
26094fa9e02dSMarcin Wojtas 	    ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
26104fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size_value = 128;
26114fa9e02dSMarcin Wojtas }
26124fa9e02dSMarcin Wojtas 
26134fa9e02dSMarcin Wojtas static int
26147d8c4feeSMarcin Wojtas ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx)
26159b8d05b8SZbigniew Bodek {
26164fa9e02dSMarcin Wojtas 	struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
26174fa9e02dSMarcin Wojtas 	struct ena_com_dev *ena_dev = ctx->ena_dev;
26186064f289SMarcin Wojtas 	uint32_t tx_queue_size = ENA_DEFAULT_RING_SIZE;
26197d8c4feeSMarcin Wojtas 	uint32_t rx_queue_size = ENA_DEFAULT_RING_SIZE;
26207d8c4feeSMarcin Wojtas 	uint32_t max_tx_queue_size;
26217d8c4feeSMarcin Wojtas 	uint32_t max_rx_queue_size;
26229b8d05b8SZbigniew Bodek 
26234fa9e02dSMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
26246064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
26256064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
26267d8c4feeSMarcin Wojtas 		max_rx_queue_size = min_t(uint32_t,
26277d8c4feeSMarcin Wojtas 		    max_queue_ext->max_rx_cq_depth,
26286064f289SMarcin Wojtas 		    max_queue_ext->max_rx_sq_depth);
26297d8c4feeSMarcin Wojtas 		max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
26304fa9e02dSMarcin Wojtas 
26314fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
26324fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
26337d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
26344fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
26354fa9e02dSMarcin Wojtas 		else
26367d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
26376064f289SMarcin Wojtas 			    max_queue_ext->max_tx_sq_depth);
26384fa9e02dSMarcin Wojtas 
26396064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
26406064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_tx_descs);
26417d8c4feeSMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
26427d8c4feeSMarcin Wojtas 		    max_queue_ext->max_per_packet_rx_descs);
26436064f289SMarcin Wojtas 	} else {
26446064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
26456064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queues;
26467d8c4feeSMarcin Wojtas 		max_rx_queue_size = min_t(uint32_t,
26477d8c4feeSMarcin Wojtas 		    max_queues->max_cq_depth,
26486064f289SMarcin Wojtas 		    max_queues->max_sq_depth);
26497d8c4feeSMarcin Wojtas 		max_tx_queue_size = max_queues->max_cq_depth;
26504fa9e02dSMarcin Wojtas 
26514fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
26524fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
26537d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
26544fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
26554fa9e02dSMarcin Wojtas 		else
26567d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
26574fa9e02dSMarcin Wojtas 			    max_queues->max_sq_depth);
26584fa9e02dSMarcin Wojtas 
26596064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
26607d8c4feeSMarcin Wojtas 		    max_queues->max_packet_tx_descs);
26617d8c4feeSMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
26626064f289SMarcin Wojtas 		    max_queues->max_packet_rx_descs);
26636064f289SMarcin Wojtas 	}
26649b8d05b8SZbigniew Bodek 
26659b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
26667d8c4feeSMarcin Wojtas 	max_tx_queue_size = 1 << (flsl(max_tx_queue_size) - 1);
26677d8c4feeSMarcin Wojtas 	max_rx_queue_size = 1 << (flsl(max_rx_queue_size) - 1);
26686064f289SMarcin Wojtas 
26697d8c4feeSMarcin Wojtas 	tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE,
26707d8c4feeSMarcin Wojtas 	    max_tx_queue_size);
26717d8c4feeSMarcin Wojtas 	rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE,
26727d8c4feeSMarcin Wojtas 	    max_rx_queue_size);
26739b8d05b8SZbigniew Bodek 
26747d8c4feeSMarcin Wojtas 	tx_queue_size = 1 << (flsl(tx_queue_size) - 1);
26757d8c4feeSMarcin Wojtas 	rx_queue_size = 1 << (flsl(rx_queue_size) - 1);
26767d8c4feeSMarcin Wojtas 
26777d8c4feeSMarcin Wojtas 	ctx->max_tx_queue_size = max_tx_queue_size;
26787d8c4feeSMarcin Wojtas 	ctx->max_rx_queue_size = max_rx_queue_size;
26796064f289SMarcin Wojtas 	ctx->tx_queue_size = tx_queue_size;
26807d8c4feeSMarcin Wojtas 	ctx->rx_queue_size = rx_queue_size;
26816064f289SMarcin Wojtas 
26826064f289SMarcin Wojtas 	return (0);
26839b8d05b8SZbigniew Bodek }
26849b8d05b8SZbigniew Bodek 
26850bdffe59SMarcin Wojtas static int
26860bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
26879b8d05b8SZbigniew Bodek {
26889b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
26899b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
26909b8d05b8SZbigniew Bodek 	int qid, rc, i;
26919b8d05b8SZbigniew Bodek 
26929b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
26930bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
26944e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
26957d2544e6SMarcin Wojtas 		return (rc);
26969b8d05b8SZbigniew Bodek 	}
26979b8d05b8SZbigniew Bodek 
26989b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
26997d8c4feeSMarcin Wojtas 		qid = i % adapter->num_io_queues;
27009b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
27019b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
27020bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
27039b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
27047d2544e6SMarcin Wojtas 			goto err_rss_destroy;
27059b8d05b8SZbigniew Bodek 		}
27069b8d05b8SZbigniew Bodek 	}
27079b8d05b8SZbigniew Bodek 
2708b40dd828SAndriy Gapon #ifdef RSS
2709b40dd828SAndriy Gapon 	uint8_t rss_algo = rss_gethashalgo();
2710b40dd828SAndriy Gapon 	if (rss_algo == RSS_HASH_TOEPLITZ) {
2711b40dd828SAndriy Gapon 		uint8_t hash_key[RSS_KEYSIZE];
2712b40dd828SAndriy Gapon 
2713b40dd828SAndriy Gapon 		rss_getkey(hash_key);
2714b40dd828SAndriy Gapon 		rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_TOEPLITZ,
2715b40dd828SAndriy Gapon 		    hash_key, RSS_KEYSIZE, 0xFFFFFFFF);
2716b40dd828SAndriy Gapon 	} else
2717b40dd828SAndriy Gapon #endif
27189b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
27199b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
27200bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
27219b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
27227d2544e6SMarcin Wojtas 		goto err_rss_destroy;
27239b8d05b8SZbigniew Bodek 	}
27249b8d05b8SZbigniew Bodek 
27259b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
27260bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
27279b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
27287d2544e6SMarcin Wojtas 		goto err_rss_destroy;
27299b8d05b8SZbigniew Bodek 	}
27309b8d05b8SZbigniew Bodek 
27319b8d05b8SZbigniew Bodek 	return (0);
27329b8d05b8SZbigniew Bodek 
27337d2544e6SMarcin Wojtas err_rss_destroy:
27349b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
27359b8d05b8SZbigniew Bodek 	return (rc);
27369b8d05b8SZbigniew Bodek }
27379b8d05b8SZbigniew Bodek 
27389b8d05b8SZbigniew Bodek static void
27399b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
27409b8d05b8SZbigniew Bodek {
27419b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
27429b8d05b8SZbigniew Bodek 	devclass_t dc;
27439b8d05b8SZbigniew Bodek 	int max;
27449b8d05b8SZbigniew Bodek 	int rc;
27459b8d05b8SZbigniew Bodek 
27469b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
27473f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
2748*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "No devclass ena\n");
27499b8d05b8SZbigniew Bodek 		return;
27509b8d05b8SZbigniew Bodek 	}
27519b8d05b8SZbigniew Bodek 
27529b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
27539b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
27549b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
27559b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
27569b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
2757fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
27583f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
27599b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
27609b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
27610bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
2762fd43fd2aSMarcin Wojtas 				ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
27639b8d05b8SZbigniew Bodek 			}
27649b8d05b8SZbigniew Bodek 		}
27659b8d05b8SZbigniew Bodek 	}
27669b8d05b8SZbigniew Bodek }
27679b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
27689b8d05b8SZbigniew Bodek 
27690bdffe59SMarcin Wojtas static void
277046021271SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev, device_t dev)
27719b8d05b8SZbigniew Bodek {
27729b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
277346021271SMarcin Wojtas 	uintptr_t rid;
27749b8d05b8SZbigniew Bodek 	int rc;
27759b8d05b8SZbigniew Bodek 
27769b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
27779b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
27783f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2779*9eb1615fSMarcin Wojtas 		ena_trace(NULL, ENA_ALERT, "Cannot allocate host info\n");
27809b8d05b8SZbigniew Bodek 		return;
27819b8d05b8SZbigniew Bodek 	}
27829b8d05b8SZbigniew Bodek 
27839b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
27849b8d05b8SZbigniew Bodek 
278546021271SMarcin Wojtas 	if (pci_get_id(dev, PCI_ID_RID, &rid) == 0)
278646021271SMarcin Wojtas 		host_info->bdf = rid;
27879b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
27889b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
27899b8d05b8SZbigniew Bodek 
27909b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
27919b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
27929b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
27939b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
27949b8d05b8SZbigniew Bodek 
27959b8d05b8SZbigniew Bodek 	host_info->driver_version =
27969b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
27979b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
27989b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
27998ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
28009b8d05b8SZbigniew Bodek 
28019b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
28023f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2803a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
2804*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_WARNING, "Cannot set host attributes\n");
28059b8d05b8SZbigniew Bodek 		else
2806*9eb1615fSMarcin Wojtas 			ena_trace(NULL, ENA_ALERT, "Cannot set host attributes\n");
28079b8d05b8SZbigniew Bodek 
28089b8d05b8SZbigniew Bodek 		goto err;
28099b8d05b8SZbigniew Bodek 	}
28109b8d05b8SZbigniew Bodek 
28119b8d05b8SZbigniew Bodek 	return;
28129b8d05b8SZbigniew Bodek 
28139b8d05b8SZbigniew Bodek err:
28149b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
28159b8d05b8SZbigniew Bodek }
28169b8d05b8SZbigniew Bodek 
28179b8d05b8SZbigniew Bodek static int
28189b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
28199b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
28209b8d05b8SZbigniew Bodek {
28219b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
28229b8d05b8SZbigniew Bodek 	bool readless_supported;
28239b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
28249b8d05b8SZbigniew Bodek 	int dma_width;
28259b8d05b8SZbigniew Bodek 	int rc;
28269b8d05b8SZbigniew Bodek 
28279b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
28283f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28299b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
28300bdffe59SMarcin Wojtas 		return (rc);
28319b8d05b8SZbigniew Bodek 	}
28329b8d05b8SZbigniew Bodek 
28339b8d05b8SZbigniew Bodek 	/*
28349b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
28359b8d05b8SZbigniew Bodek 	 * read is disabled
28369b8d05b8SZbigniew Bodek 	 */
28379b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
28389b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
28399b8d05b8SZbigniew Bodek 
2840a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
28413f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28429b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
28439b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
28449b8d05b8SZbigniew Bodek 	}
28459b8d05b8SZbigniew Bodek 
28469b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
28473f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28489b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
28499b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
28509b8d05b8SZbigniew Bodek 	}
28519b8d05b8SZbigniew Bodek 
28529b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
28533f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
28549b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
28559b8d05b8SZbigniew Bodek 		rc = dma_width;
28569b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
28579b8d05b8SZbigniew Bodek 	}
28589b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
28599b8d05b8SZbigniew Bodek 
28609b8d05b8SZbigniew Bodek 	/* ENA admin level init */
286167ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
28623f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28639b8d05b8SZbigniew Bodek 		device_printf(pdev,
28649b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
28659b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
28669b8d05b8SZbigniew Bodek 	}
28679b8d05b8SZbigniew Bodek 
28689b8d05b8SZbigniew Bodek 	/*
28699b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
28709b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
28719b8d05b8SZbigniew Bodek 	 * information
28729b8d05b8SZbigniew Bodek 	 */
28739b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
28749b8d05b8SZbigniew Bodek 
287546021271SMarcin Wojtas 	ena_config_host_info(ena_dev, pdev);
28769b8d05b8SZbigniew Bodek 
28779b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
28789b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
28793f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28809b8d05b8SZbigniew Bodek 		device_printf(pdev,
28819b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
28829b8d05b8SZbigniew Bodek 		goto err_admin_init;
28839b8d05b8SZbigniew Bodek 	}
28849b8d05b8SZbigniew Bodek 
2885e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
2886e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
2887e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
288840621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
2889e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
28909b8d05b8SZbigniew Bodek 
28919b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
28929b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
28933f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28949b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
28959b8d05b8SZbigniew Bodek 		goto err_admin_init;
28969b8d05b8SZbigniew Bodek 	}
28979b8d05b8SZbigniew Bodek 
28989b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
28999b8d05b8SZbigniew Bodek 
29000bdffe59SMarcin Wojtas 	return (0);
29019b8d05b8SZbigniew Bodek 
29029b8d05b8SZbigniew Bodek err_admin_init:
29039b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
29049b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
29059b8d05b8SZbigniew Bodek err_mmio_read_less:
29069b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
29079b8d05b8SZbigniew Bodek 
29080bdffe59SMarcin Wojtas 	return (rc);
29099b8d05b8SZbigniew Bodek }
29109b8d05b8SZbigniew Bodek 
2911aa9c3226SMarcin Wojtas static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter)
29129b8d05b8SZbigniew Bodek {
29139b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
29149b8d05b8SZbigniew Bodek 	int rc;
29159b8d05b8SZbigniew Bodek 
29169b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
29173f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
29189b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
29190bdffe59SMarcin Wojtas 		return (rc);
29209b8d05b8SZbigniew Bodek 	}
29219b8d05b8SZbigniew Bodek 
29229b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
29239b8d05b8SZbigniew Bodek 
29249b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
29253f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
29269b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
29279b8d05b8SZbigniew Bodek 		goto err_disable_msix;
29289b8d05b8SZbigniew Bodek 	}
29299b8d05b8SZbigniew Bodek 
29309b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
29319b8d05b8SZbigniew Bodek 
29329b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
29339b8d05b8SZbigniew Bodek 
29340bdffe59SMarcin Wojtas 	return (0);
29359b8d05b8SZbigniew Bodek 
29369b8d05b8SZbigniew Bodek err_disable_msix:
29379b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
29389b8d05b8SZbigniew Bodek 
29390bdffe59SMarcin Wojtas 	return (rc);
29409b8d05b8SZbigniew Bodek }
29419b8d05b8SZbigniew Bodek 
29429b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
29439b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
29449b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
29459b8d05b8SZbigniew Bodek {
29469b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
294730217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
29489b8d05b8SZbigniew Bodek 	sbintime_t stime;
294930217e2dSMarcin Wojtas 	uint64_t rx_drops;
29506c84cec3SMarcin Wojtas 	uint64_t tx_drops;
295130217e2dSMarcin Wojtas 
295230217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
295330217e2dSMarcin Wojtas 
295430217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
29556c84cec3SMarcin Wojtas 	tx_drops = ((uint64_t)desc->tx_drops_high << 32) | desc->tx_drops_low;
295630217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
295730217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
29586c84cec3SMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.tx_drops);
29596c84cec3SMarcin Wojtas 	counter_u64_add(adapter->hw_stats.tx_drops, tx_drops);
29609b8d05b8SZbigniew Bodek 
29619b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
29629b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
29639b8d05b8SZbigniew Bodek }
29649b8d05b8SZbigniew Bodek 
29659b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
29669b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
29679b8d05b8SZbigniew Bodek {
29689b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
29699b8d05b8SZbigniew Bodek 
29709b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
29719b8d05b8SZbigniew Bodek 		return;
29729b8d05b8SZbigniew Bodek 
297340621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
29749b8d05b8SZbigniew Bodek 		return;
29759b8d05b8SZbigniew Bodek 
29769b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
29779b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
29789b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
29799b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
29809b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
29819b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
29827926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_KEEP_ALIVE_TO);
29839b8d05b8SZbigniew Bodek 	}
2984858659f7SMarcin Wojtas }
29859b8d05b8SZbigniew Bodek 
29869b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
29879b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
29889b8d05b8SZbigniew Bodek {
29890bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
29900bdffe59SMarcin Wojtas 	    false)) {
29919b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
29929b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
29939b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
29947926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_ADMIN_TO);
29959b8d05b8SZbigniew Bodek 	}
2996858659f7SMarcin Wojtas }
29979b8d05b8SZbigniew Bodek 
299874dba3adSMarcin Wojtas static int
2999d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3000d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
3001d12f7bfcSMarcin Wojtas {
3002d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
3003d12f7bfcSMarcin Wojtas 		return (0);
3004d12f7bfcSMarcin Wojtas 
3005d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3006d12f7bfcSMarcin Wojtas 		return (0);
3007d12f7bfcSMarcin Wojtas 
3008d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
3009d12f7bfcSMarcin Wojtas 
3010d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3011d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
3012d12f7bfcSMarcin Wojtas 		    "Queue = %d. Reset the device\n", rx_ring->qid);
30137926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_MISS_INTERRUPT);
3014d12f7bfcSMarcin Wojtas 		return (EIO);
3015d12f7bfcSMarcin Wojtas 	}
3016d12f7bfcSMarcin Wojtas 
3017d12f7bfcSMarcin Wojtas 	return (0);
3018d12f7bfcSMarcin Wojtas }
3019d12f7bfcSMarcin Wojtas 
3020d12f7bfcSMarcin Wojtas static int
3021d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
302274dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
302374dba3adSMarcin Wojtas {
302474dba3adSMarcin Wojtas 	struct bintime curtime, time;
302574dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
3026d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
302774dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
3028d12f7bfcSMarcin Wojtas 	int i, rc = 0;
302974dba3adSMarcin Wojtas 
303074dba3adSMarcin Wojtas 	getbinuptime(&curtime);
303174dba3adSMarcin Wojtas 
303274dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
303374dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
303474dba3adSMarcin Wojtas 
30350bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
303674dba3adSMarcin Wojtas 			continue;
303774dba3adSMarcin Wojtas 
303874dba3adSMarcin Wojtas 		time = curtime;
303974dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
3040d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
3041d12f7bfcSMarcin Wojtas 
3042d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
3043d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
3044d12f7bfcSMarcin Wojtas 			/*
3045d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
3046d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
3047d12f7bfcSMarcin Wojtas 			 */
3048d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
3049d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
3050d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
30517926bc44SMarcin Wojtas 			ena_trigger_reset(adapter,
30527926bc44SMarcin Wojtas 			    ENA_REGS_RESET_MISS_INTERRUPT);
3053d12f7bfcSMarcin Wojtas 			return (EIO);
3054d12f7bfcSMarcin Wojtas 		}
305574dba3adSMarcin Wojtas 
305674dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
3057d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
305874dba3adSMarcin Wojtas 
305974dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
3060*9eb1615fSMarcin Wojtas 				ena_trace(NULL, ENA_WARNING, "Found a Tx that wasn't "
306174dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
306274dba3adSMarcin Wojtas 				    tx_ring->qid, i);
306374dba3adSMarcin Wojtas 
306474dba3adSMarcin Wojtas 			tx_buf->print_once = true;
306574dba3adSMarcin Wojtas 			missed_tx++;
3066d12f7bfcSMarcin Wojtas 		}
3067d12f7bfcSMarcin Wojtas 	}
306874dba3adSMarcin Wojtas 
3069d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
307074dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
3071d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
3072d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
30734e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
30747926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_MISS_TX_CMPL);
3075d12f7bfcSMarcin Wojtas 		rc = EIO;
307674dba3adSMarcin Wojtas 	}
307774dba3adSMarcin Wojtas 
3078d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
3079d12f7bfcSMarcin Wojtas 
3080d12f7bfcSMarcin Wojtas 	return (rc);
308174dba3adSMarcin Wojtas }
308274dba3adSMarcin Wojtas 
30839b8d05b8SZbigniew Bodek /*
30849b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
30859b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
30869b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
30879b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
30889b8d05b8SZbigniew Bodek  */
30890bdffe59SMarcin Wojtas static void
3090d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
30919b8d05b8SZbigniew Bodek {
30929b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
3093d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
309474dba3adSMarcin Wojtas 	int i, budget, rc;
30959b8d05b8SZbigniew Bodek 
30969b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
30979b8d05b8SZbigniew Bodek 	rmb();
30989b8d05b8SZbigniew Bodek 
3099fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
31009b8d05b8SZbigniew Bodek 		return;
31019b8d05b8SZbigniew Bodek 
3102fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
31039b8d05b8SZbigniew Bodek 		return;
31049b8d05b8SZbigniew Bodek 
310540621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
31069b8d05b8SZbigniew Bodek 		return;
31079b8d05b8SZbigniew Bodek 
31089b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
31099b8d05b8SZbigniew Bodek 
31107d8c4feeSMarcin Wojtas 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_io_queues; i++) {
31119b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
3112d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
31139b8d05b8SZbigniew Bodek 
3114d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3115d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
3116d12f7bfcSMarcin Wojtas 			return;
3117d12f7bfcSMarcin Wojtas 
3118d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
31190bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
31209b8d05b8SZbigniew Bodek 			return;
31219b8d05b8SZbigniew Bodek 
31229b8d05b8SZbigniew Bodek 		budget--;
3123cd5d5804SMarcin Wojtas 		if (budget == 0) {
31249b8d05b8SZbigniew Bodek 			i++;
31259b8d05b8SZbigniew Bodek 			break;
31269b8d05b8SZbigniew Bodek 		}
31279b8d05b8SZbigniew Bodek 	}
31289b8d05b8SZbigniew Bodek 
31297d8c4feeSMarcin Wojtas 	adapter->next_monitored_tx_qid = i % adapter->num_io_queues;
31309b8d05b8SZbigniew Bodek }
31319b8d05b8SZbigniew Bodek 
31325cb9db07SMarcin Wojtas /* trigger rx cleanup after 2 consecutive detections */
3133efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
3134efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
3135efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
3136efe6ab18SMarcin Wojtas  * for example).
3137efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
3138efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
3139efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
3140efe6ab18SMarcin Wojtas  * able to send new packets.
3141efe6ab18SMarcin Wojtas  *
3142efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
3143efe6ab18SMarcin Wojtas  */
3144efe6ab18SMarcin Wojtas static void
3145efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
3146efe6ab18SMarcin Wojtas {
3147efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
3148efe6ab18SMarcin Wojtas 	int i, refill_required;
3149efe6ab18SMarcin Wojtas 
3150fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
3151efe6ab18SMarcin Wojtas 		return;
3152efe6ab18SMarcin Wojtas 
3153fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
3154efe6ab18SMarcin Wojtas 		return;
3155efe6ab18SMarcin Wojtas 
31567d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
3157efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
3158efe6ab18SMarcin Wojtas 
31598483b844SMarcin Wojtas 		refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
3160efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3161efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
3162efe6ab18SMarcin Wojtas 
3163efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
3164efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
3165efe6ab18SMarcin Wojtas 				    1);
3166efe6ab18SMarcin Wojtas 
3167efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
3168efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
3169efe6ab18SMarcin Wojtas 
31705cb9db07SMarcin Wojtas 				taskqueue_enqueue(rx_ring->que->cleanup_tq,
31715cb9db07SMarcin Wojtas 				    &rx_ring->que->cleanup_task);
3172efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
3173efe6ab18SMarcin Wojtas 			}
3174efe6ab18SMarcin Wojtas 		} else {
3175efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
3176efe6ab18SMarcin Wojtas 		}
3177efe6ab18SMarcin Wojtas 	}
3178efe6ab18SMarcin Wojtas }
31799b8d05b8SZbigniew Bodek 
318040621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
318140621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
318240621d71SMarcin Wojtas {
318340621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
318440621d71SMarcin Wojtas 
318540621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
318640621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
318740621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
318840621d71SMarcin Wojtas 
318940621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
319040621d71SMarcin Wojtas 		/* convert to usec */
319140621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
319240621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
319340621d71SMarcin Wojtas 
319440621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
319540621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
319640621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
319740621d71SMarcin Wojtas 
319840621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
319940621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
320040621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
320140621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
320240621d71SMarcin Wojtas 		else
320340621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
320440621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
320540621d71SMarcin Wojtas 	}
320640621d71SMarcin Wojtas 
320740621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
320840621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
320940621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
321040621d71SMarcin Wojtas 		else
321140621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
321240621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
321340621d71SMarcin Wojtas 	}
321440621d71SMarcin Wojtas }
321540621d71SMarcin Wojtas 
32169b8d05b8SZbigniew Bodek static void
32179b8d05b8SZbigniew Bodek ena_timer_service(void *data)
32189b8d05b8SZbigniew Bodek {
32199b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
32209b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
32219b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
32229b8d05b8SZbigniew Bodek 
32239b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
32249b8d05b8SZbigniew Bodek 
32259b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
32269b8d05b8SZbigniew Bodek 
3227d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
32289b8d05b8SZbigniew Bodek 
3229efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
3230efe6ab18SMarcin Wojtas 
32310bdffe59SMarcin Wojtas 	if (host_info != NULL)
32329b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
32339b8d05b8SZbigniew Bodek 
3234fd43fd2aSMarcin Wojtas 	if (unlikely(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
32359b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
32369b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
32379b8d05b8SZbigniew Bodek 		return;
32389b8d05b8SZbigniew Bodek 	}
32399b8d05b8SZbigniew Bodek 
32409b8d05b8SZbigniew Bodek 	/*
32419b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
32429b8d05b8SZbigniew Bodek 	 */
32439b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
32449b8d05b8SZbigniew Bodek }
32459b8d05b8SZbigniew Bodek 
324638c7b965SMarcin Wojtas void
324732f63fa7SMarcin Wojtas ena_destroy_device(struct ena_adapter *adapter, bool graceful)
32489b8d05b8SZbigniew Bodek {
324932f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
32509b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
32519b8d05b8SZbigniew Bodek 	bool dev_up;
325232f63fa7SMarcin Wojtas 
325332f63fa7SMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter))
325432f63fa7SMarcin Wojtas 		return;
325532f63fa7SMarcin Wojtas 
325632f63fa7SMarcin Wojtas 	if_link_state_change(ifp, LINK_STATE_DOWN);
325732f63fa7SMarcin Wojtas 
325832f63fa7SMarcin Wojtas 	callout_drain(&adapter->timer_service);
325932f63fa7SMarcin Wojtas 
326032f63fa7SMarcin Wojtas 	dev_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
326132f63fa7SMarcin Wojtas 	if (dev_up)
326232f63fa7SMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
326332f63fa7SMarcin Wojtas 
326432f63fa7SMarcin Wojtas 	if (!graceful)
326532f63fa7SMarcin Wojtas 		ena_com_set_admin_running_state(ena_dev, false);
326632f63fa7SMarcin Wojtas 
326732f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
326832f63fa7SMarcin Wojtas 		ena_down(adapter);
326932f63fa7SMarcin Wojtas 
327032f63fa7SMarcin Wojtas 	/*
327132f63fa7SMarcin Wojtas 	 * Stop the device from sending AENQ events (if the device was up, and
327232f63fa7SMarcin Wojtas 	 * the trigger reset was on, ena_down already performs device reset)
327332f63fa7SMarcin Wojtas 	 */
327432f63fa7SMarcin Wojtas 	if (!(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter) && dev_up))
327532f63fa7SMarcin Wojtas 		ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
327632f63fa7SMarcin Wojtas 
327732f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
327832f63fa7SMarcin Wojtas 
327932f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
328032f63fa7SMarcin Wojtas 
3281e2735b09SMarcin Wojtas 	/*
3282e2735b09SMarcin Wojtas 	 * IO rings resources should be freed because `ena_restore_device()`
3283e2735b09SMarcin Wojtas 	 * calls (not directly) `ena_enable_msix()`, which re-allocates MSIX
3284e2735b09SMarcin Wojtas 	 * vectors. The amount of MSIX vectors after destroy-restore may be
3285e2735b09SMarcin Wojtas 	 * different than before. Therefore, IO rings resources should be
3286e2735b09SMarcin Wojtas 	 * established from scratch each time.
3287e2735b09SMarcin Wojtas 	 */
3288e2735b09SMarcin Wojtas 	ena_free_all_io_rings_resources(adapter);
3289e2735b09SMarcin Wojtas 
329032f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
329132f63fa7SMarcin Wojtas 
329232f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
329332f63fa7SMarcin Wojtas 
329432f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
329532f63fa7SMarcin Wojtas 
329632f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
329732f63fa7SMarcin Wojtas 
329832f63fa7SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
329932f63fa7SMarcin Wojtas 
330032f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
330132f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
330232f63fa7SMarcin Wojtas }
330332f63fa7SMarcin Wojtas 
330432f63fa7SMarcin Wojtas static int
330532f63fa7SMarcin Wojtas ena_device_validate_params(struct ena_adapter *adapter,
330632f63fa7SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
330732f63fa7SMarcin Wojtas {
330832f63fa7SMarcin Wojtas 
330932f63fa7SMarcin Wojtas 	if (memcmp(get_feat_ctx->dev_attr.mac_addr, adapter->mac_addr,
331032f63fa7SMarcin Wojtas 	    ETHER_ADDR_LEN) != 0) {
331132f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
331232f63fa7SMarcin Wojtas 		    "Error, mac address are different\n");
331332f63fa7SMarcin Wojtas 		return (EINVAL);
331432f63fa7SMarcin Wojtas 	}
331532f63fa7SMarcin Wojtas 
331632f63fa7SMarcin Wojtas 	if (get_feat_ctx->dev_attr.max_mtu < if_getmtu(adapter->ifp)) {
331732f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
331832f63fa7SMarcin Wojtas 		    "Error, device max mtu is smaller than ifp MTU\n");
331932f63fa7SMarcin Wojtas 		return (EINVAL);
332032f63fa7SMarcin Wojtas 	}
332132f63fa7SMarcin Wojtas 
332232f63fa7SMarcin Wojtas 	return 0;
332332f63fa7SMarcin Wojtas }
332432f63fa7SMarcin Wojtas 
332538c7b965SMarcin Wojtas int
332632f63fa7SMarcin Wojtas ena_restore_device(struct ena_adapter *adapter)
332732f63fa7SMarcin Wojtas {
332832f63fa7SMarcin Wojtas 	struct ena_com_dev_get_features_ctx get_feat_ctx;
332932f63fa7SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
333032f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
333132f63fa7SMarcin Wojtas 	device_t dev = adapter->pdev;
333232f63fa7SMarcin Wojtas 	int wd_active;
33339b8d05b8SZbigniew Bodek 	int rc;
33349b8d05b8SZbigniew Bodek 
333532f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
333632f63fa7SMarcin Wojtas 
333732f63fa7SMarcin Wojtas 	rc = ena_device_init(adapter, dev, &get_feat_ctx, &wd_active);
333832f63fa7SMarcin Wojtas 	if (rc != 0) {
333932f63fa7SMarcin Wojtas 		device_printf(dev, "Cannot initialize device\n");
334032f63fa7SMarcin Wojtas 		goto err;
334132f63fa7SMarcin Wojtas 	}
334232f63fa7SMarcin Wojtas 	/*
334332f63fa7SMarcin Wojtas 	 * Only enable WD if it was enabled before reset, so it won't override
334432f63fa7SMarcin Wojtas 	 * value set by the user by the sysctl.
334532f63fa7SMarcin Wojtas 	 */
334632f63fa7SMarcin Wojtas 	if (adapter->wd_active != 0)
334732f63fa7SMarcin Wojtas 		adapter->wd_active = wd_active;
334832f63fa7SMarcin Wojtas 
334932f63fa7SMarcin Wojtas 	rc = ena_device_validate_params(adapter, &get_feat_ctx);
335032f63fa7SMarcin Wojtas 	if (rc != 0) {
335132f63fa7SMarcin Wojtas 		device_printf(dev, "Validation of device parameters failed\n");
335232f63fa7SMarcin Wojtas 		goto err_device_destroy;
335332f63fa7SMarcin Wojtas 	}
335432f63fa7SMarcin Wojtas 
335532f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
335632f63fa7SMarcin Wojtas 	/* Make sure we don't have a race with AENQ Links state handler */
335732f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
335832f63fa7SMarcin Wojtas 		if_link_state_change(ifp, LINK_STATE_UP);
335932f63fa7SMarcin Wojtas 
3360aa9c3226SMarcin Wojtas 	rc = ena_enable_msix_and_set_admin_interrupts(adapter);
336132f63fa7SMarcin Wojtas 	if (rc != 0) {
336232f63fa7SMarcin Wojtas 		device_printf(dev, "Enable MSI-X failed\n");
336332f63fa7SMarcin Wojtas 		goto err_device_destroy;
336432f63fa7SMarcin Wojtas 	}
336532f63fa7SMarcin Wojtas 
3366e2735b09SMarcin Wojtas 	/*
3367e2735b09SMarcin Wojtas 	 * Effective value of used MSIX vectors should be the same as before
3368e2735b09SMarcin Wojtas 	 * `ena_destroy_device()`, if possible, or closest to it if less vectors
3369e2735b09SMarcin Wojtas 	 * are available.
3370e2735b09SMarcin Wojtas 	 */
3371e2735b09SMarcin Wojtas 	if ((adapter->msix_vecs - ENA_ADMIN_MSIX_VEC) < adapter->num_io_queues)
3372e2735b09SMarcin Wojtas 		adapter->num_io_queues =
3373e2735b09SMarcin Wojtas 		    adapter->msix_vecs - ENA_ADMIN_MSIX_VEC;
3374e2735b09SMarcin Wojtas 
3375e2735b09SMarcin Wojtas 	/* Re-initialize rings basic information */
3376e2735b09SMarcin Wojtas 	ena_init_io_rings(adapter);
3377e2735b09SMarcin Wojtas 
337832f63fa7SMarcin Wojtas 	/* If the interface was up before the reset bring it up */
337932f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
338032f63fa7SMarcin Wojtas 		rc = ena_up(adapter);
338132f63fa7SMarcin Wojtas 		if (rc != 0) {
338232f63fa7SMarcin Wojtas 			device_printf(dev, "Failed to create I/O queues\n");
338332f63fa7SMarcin Wojtas 			goto err_disable_msix;
338432f63fa7SMarcin Wojtas 		}
338532f63fa7SMarcin Wojtas 	}
338632f63fa7SMarcin Wojtas 
338724392281SMarcin Wojtas 	/* Indicate that device is running again and ready to work */
338832f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
338924392281SMarcin Wojtas 
339024392281SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
339124392281SMarcin Wojtas 		/*
339224392281SMarcin Wojtas 		 * As the AENQ handlers weren't executed during reset because
339324392281SMarcin Wojtas 		 * the flag ENA_FLAG_DEVICE_RUNNING was turned off, the
339424392281SMarcin Wojtas 		 * timestamp must be updated again That will prevent next reset
339524392281SMarcin Wojtas 		 * caused by missing keep alive.
339624392281SMarcin Wojtas 		 */
339724392281SMarcin Wojtas 		adapter->keep_alive_timestamp = getsbinuptime();
339832f63fa7SMarcin Wojtas 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
339932f63fa7SMarcin Wojtas 		    ena_timer_service, (void *)adapter, 0);
340024392281SMarcin Wojtas 	}
34017d8c4feeSMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
340232f63fa7SMarcin Wojtas 
340332f63fa7SMarcin Wojtas 	device_printf(dev,
340432f63fa7SMarcin Wojtas 	    "Device reset completed successfully, Driver info: %s\n", ena_version);
340532f63fa7SMarcin Wojtas 
340632f63fa7SMarcin Wojtas 	return (rc);
340732f63fa7SMarcin Wojtas 
340832f63fa7SMarcin Wojtas err_disable_msix:
340932f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
341032f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
341132f63fa7SMarcin Wojtas err_device_destroy:
341232f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
341332f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
341432f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
341532f63fa7SMarcin Wojtas 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
341632f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
341732f63fa7SMarcin Wojtas err:
341832f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
341932f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
342032f63fa7SMarcin Wojtas 	device_printf(dev, "Reset attempt failed. Can not reset the device\n");
342132f63fa7SMarcin Wojtas 
342232f63fa7SMarcin Wojtas 	return (rc);
342332f63fa7SMarcin Wojtas }
342432f63fa7SMarcin Wojtas 
342532f63fa7SMarcin Wojtas static void
342632f63fa7SMarcin Wojtas ena_reset_task(void *arg, int pending)
342732f63fa7SMarcin Wojtas {
342832f63fa7SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
342932f63fa7SMarcin Wojtas 
3430fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
34319b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
34329b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
34339b8d05b8SZbigniew Bodek 		return;
34349b8d05b8SZbigniew Bodek 	}
34359b8d05b8SZbigniew Bodek 
34366959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
343732f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, false);
343832f63fa7SMarcin Wojtas 	ena_restore_device(adapter);
34396959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
34409b8d05b8SZbigniew Bodek }
34419b8d05b8SZbigniew Bodek 
34429b8d05b8SZbigniew Bodek /**
34439b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
34449b8d05b8SZbigniew Bodek  * @pdev: device information struct
34459b8d05b8SZbigniew Bodek  *
34469b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
34479b8d05b8SZbigniew Bodek  *
34489b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
34499b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
34509b8d05b8SZbigniew Bodek  * and a hardware reset occur.
34519b8d05b8SZbigniew Bodek  **/
34529b8d05b8SZbigniew Bodek static int
34539b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
34549b8d05b8SZbigniew Bodek {
34559b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
34564fa9e02dSMarcin Wojtas 	struct ena_llq_configurations llq_config;
34576064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
34589b8d05b8SZbigniew Bodek 	static int version_printed;
34599b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
34609b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
34617d8c4feeSMarcin Wojtas 	uint32_t max_num_io_queues;
34624fa9e02dSMarcin Wojtas 	int rid, rc;
34634fa9e02dSMarcin Wojtas 
34649b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
34659b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
34669b8d05b8SZbigniew Bodek 
34676959869eSMarcin Wojtas 	ENA_LOCK_INIT(adapter);
34689b8d05b8SZbigniew Bodek 
34696959869eSMarcin Wojtas 	/*
34706959869eSMarcin Wojtas 	 * Set up the timer service - driver is responsible for avoiding
34716959869eSMarcin Wojtas 	 * concurrency, as the callout won't be using any locking inside.
34726959869eSMarcin Wojtas 	 */
34736959869eSMarcin Wojtas 	callout_init(&adapter->timer_service, true);
34749b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
34759b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
34769b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
34779b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
34789b8d05b8SZbigniew Bodek 
34799b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
34809b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
34819b8d05b8SZbigniew Bodek 
34829b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3483cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3484cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
34859b8d05b8SZbigniew Bodek 
34869b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
34879b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
34884fa9e02dSMarcin Wojtas 
34894fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_REG_BAR);
34904fa9e02dSMarcin Wojtas 	adapter->memory = NULL;
34914fa9e02dSMarcin Wojtas 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
34924fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
34934fa9e02dSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
34944fa9e02dSMarcin Wojtas 		device_printf(pdev,
34954fa9e02dSMarcin Wojtas 		    "unable to allocate bus resource: registers!\n");
34964fa9e02dSMarcin Wojtas 		rc = ENOMEM;
34974fa9e02dSMarcin Wojtas 		goto err_dev_free;
34984fa9e02dSMarcin Wojtas 	}
34994fa9e02dSMarcin Wojtas 
35009b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
35019b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
35029b8d05b8SZbigniew Bodek 
35039b8d05b8SZbigniew Bodek 	/* Store register resources */
35049b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
35059b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
35069b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
35079b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
35089b8d05b8SZbigniew Bodek 
35093f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
35109b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
35119b8d05b8SZbigniew Bodek 		rc = ENXIO;
3512cd5d5804SMarcin Wojtas 		goto err_bus_free;
35139b8d05b8SZbigniew Bodek 	}
35149b8d05b8SZbigniew Bodek 
35159b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
35169b8d05b8SZbigniew Bodek 
3517fd43fd2aSMarcin Wojtas 	/* Initially clear all the flags */
3518fd43fd2aSMarcin Wojtas 	ENA_FLAG_ZERO(adapter);
3519fd43fd2aSMarcin Wojtas 
35209b8d05b8SZbigniew Bodek 	/* Device initialization */
35219b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
35223f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
35239b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
35249b8d05b8SZbigniew Bodek 		rc = ENXIO;
35259b8d05b8SZbigniew Bodek 		goto err_bus_free;
35269b8d05b8SZbigniew Bodek 	}
35279b8d05b8SZbigniew Bodek 
35284fa9e02dSMarcin Wojtas 	set_default_llq_configurations(&llq_config);
35294fa9e02dSMarcin Wojtas 
35304fa9e02dSMarcin Wojtas 	rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx.llq,
35314fa9e02dSMarcin Wojtas 	     &llq_config);
35324fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
35334fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to set placement policy\n");
35344fa9e02dSMarcin Wojtas 		goto err_com_free;
35354fa9e02dSMarcin Wojtas 	}
35364fa9e02dSMarcin Wojtas 
35370b432b70SMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
35380b432b70SMarcin Wojtas 		adapter->disable_meta_caching =
35390b432b70SMarcin Wojtas 		    !!(get_feat_ctx.llq.accel_mode.u.get.supported_flags &
35400b432b70SMarcin Wojtas 		    BIT(ENA_ADMIN_DISABLE_META_CACHING));
35410b432b70SMarcin Wojtas 
35429b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
35439b8d05b8SZbigniew Bodek 
35449b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
35459b8d05b8SZbigniew Bodek 
35469b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
35479b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
35489b8d05b8SZbigniew Bodek 
35497d8c4feeSMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
35506064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
35516064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
35526064f289SMarcin Wojtas 
35537d8c4feeSMarcin Wojtas 	/* Calculate initial and maximum IO queue number and size */
35547d8c4feeSMarcin Wojtas 	max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev,
35557d8c4feeSMarcin Wojtas 	    &get_feat_ctx);
35567d8c4feeSMarcin Wojtas 	rc = ena_calc_io_queue_size(&calc_queue_ctx);
35577d8c4feeSMarcin Wojtas 	if (unlikely((rc != 0) || (max_num_io_queues <= 0))) {
35586064f289SMarcin Wojtas 		rc = EFAULT;
35599b8d05b8SZbigniew Bodek 		goto err_com_free;
35609b8d05b8SZbigniew Bodek 	}
35619b8d05b8SZbigniew Bodek 
35629762a033SMarcin Wojtas 	adapter->requested_tx_ring_size = calc_queue_ctx.tx_queue_size;
35639762a033SMarcin Wojtas 	adapter->requested_rx_ring_size = calc_queue_ctx.rx_queue_size;
35647d8c4feeSMarcin Wojtas 	adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size;
35657d8c4feeSMarcin Wojtas 	adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size;
35666064f289SMarcin Wojtas 	adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
35676064f289SMarcin Wojtas 	adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
35686064f289SMarcin Wojtas 
35697d8c4feeSMarcin Wojtas 	adapter->max_num_io_queues = max_num_io_queues;
35707d8c4feeSMarcin Wojtas 
35716064f289SMarcin Wojtas 	adapter->buf_ring_size = ENA_DEFAULT_BUF_RING_SIZE;
35729b8d05b8SZbigniew Bodek 
35737d8c4feeSMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
35747d8c4feeSMarcin Wojtas 
35757d8c4feeSMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
35767d8c4feeSMarcin Wojtas 
35779b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
35789b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
35794e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
35804e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
3581cd5d5804SMarcin Wojtas 		goto err_com_free;
35824e8acd84SMarcin Wojtas 	}
35839b8d05b8SZbigniew Bodek 
35849b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
35854e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
35864e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
3587cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
35884e8acd84SMarcin Wojtas 	}
35899b8d05b8SZbigniew Bodek 
3590e2735b09SMarcin Wojtas 	/*
3591e2735b09SMarcin Wojtas 	 * The amount of requested MSIX vectors is equal to
3592e2735b09SMarcin Wojtas 	 * adapter::max_num_io_queues (see `ena_enable_msix()`), plus a constant
3593e2735b09SMarcin Wojtas 	 * number of admin queue interrupts. The former is initially determined
3594e2735b09SMarcin Wojtas 	 * by HW capabilities (see `ena_calc_max_io_queue_num())` but may not be
3595e2735b09SMarcin Wojtas 	 * achieved if there are not enough system resources. By default, the
3596e2735b09SMarcin Wojtas 	 * number of effectively used IO queues is the same but later on it can
3597e2735b09SMarcin Wojtas 	 * be limited by the user using sysctl interface.
3598e2735b09SMarcin Wojtas 	 */
3599aa9c3226SMarcin Wojtas 	rc = ena_enable_msix_and_set_admin_interrupts(adapter);
36003f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36019b8d05b8SZbigniew Bodek 		device_printf(pdev,
36029b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
3603c115a1e2SMarcin Wojtas 		goto err_io_free;
3604c115a1e2SMarcin Wojtas 	}
3605e2735b09SMarcin Wojtas 	/* By default all of allocated MSIX vectors are actively used */
3606e2735b09SMarcin Wojtas 	adapter->num_io_queues = adapter->msix_vecs - ENA_ADMIN_MSIX_VEC;
3607e2735b09SMarcin Wojtas 
3608e2735b09SMarcin Wojtas 	/* initialize rings basic information */
3609e2735b09SMarcin Wojtas 	ena_init_io_rings(adapter);
3610c115a1e2SMarcin Wojtas 
3611c115a1e2SMarcin Wojtas 	/* setup network interface */
3612c115a1e2SMarcin Wojtas 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
3613c115a1e2SMarcin Wojtas 	if (unlikely(rc != 0)) {
3614c115a1e2SMarcin Wojtas 		device_printf(pdev, "Error with network interface setup\n");
3615c115a1e2SMarcin Wojtas 		goto err_msix_free;
36169b8d05b8SZbigniew Bodek 	}
36179b8d05b8SZbigniew Bodek 
3618081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3619081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3620081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3621081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3622081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3623081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3624081169f2SZbigniew Bodek 
36259b8d05b8SZbigniew Bodek 	/* Initialize statistics */
36269b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
36279b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
362830217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
362930217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
36309b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
36319b8d05b8SZbigniew Bodek 
3632d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3633d17b7d87SMarcin Wojtas 	rc = ena_netmap_attach(adapter);
3634d17b7d87SMarcin Wojtas 	if (rc != 0) {
3635d17b7d87SMarcin Wojtas 		device_printf(pdev, "netmap attach failed: %d\n", rc);
3636d17b7d87SMarcin Wojtas 		goto err_detach;
3637d17b7d87SMarcin Wojtas 	}
3638d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3639d17b7d87SMarcin Wojtas 
36409b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
36419b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
3642fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
36439b8d05b8SZbigniew Bodek 
36449b8d05b8SZbigniew Bodek 	return (0);
36459b8d05b8SZbigniew Bodek 
3646d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3647d17b7d87SMarcin Wojtas err_detach:
3648d17b7d87SMarcin Wojtas 	ether_ifdetach(adapter->ifp);
3649d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3650c115a1e2SMarcin Wojtas err_msix_free:
3651c115a1e2SMarcin Wojtas 	ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR);
3652c115a1e2SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
3653c115a1e2SMarcin Wojtas 	ena_disable_msix(adapter);
3654cd5d5804SMarcin Wojtas err_io_free:
36559b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
36569b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3657cd5d5804SMarcin Wojtas err_tx_tag_free:
36589b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3659cd5d5804SMarcin Wojtas err_com_free:
36609b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36619b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
3662cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
36639b8d05b8SZbigniew Bodek err_bus_free:
36649b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
36659b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
36664fa9e02dSMarcin Wojtas err_dev_free:
36674fa9e02dSMarcin Wojtas 	free(ena_dev, M_DEVBUF);
3668cd5d5804SMarcin Wojtas 
36699b8d05b8SZbigniew Bodek 	return (rc);
36709b8d05b8SZbigniew Bodek }
36719b8d05b8SZbigniew Bodek 
36729b8d05b8SZbigniew Bodek /**
36739b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
36749b8d05b8SZbigniew Bodek  * @pdev: device information struct
36759b8d05b8SZbigniew Bodek  *
36769b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
36779b8d05b8SZbigniew Bodek  * that it should release a PCI device.
36789b8d05b8SZbigniew Bodek  **/
36799b8d05b8SZbigniew Bodek static int
36809b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
36819b8d05b8SZbigniew Bodek {
36829b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
36839b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
36849b8d05b8SZbigniew Bodek 	int rc;
36859b8d05b8SZbigniew Bodek 
36869b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
36879b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
36889b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
36899b8d05b8SZbigniew Bodek 		return (EBUSY);
36909b8d05b8SZbigniew Bodek 	}
36919b8d05b8SZbigniew Bodek 
36929151c55dSMarcin Wojtas 	ether_ifdetach(adapter->ifp);
36939151c55dSMarcin Wojtas 
36946959869eSMarcin Wojtas 	/* Stop timer service */
36956959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
36969b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
36976959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
36986959869eSMarcin Wojtas 
36996959869eSMarcin Wojtas 	/* Release reset task */
37009b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
37019b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
37029b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
37039b8d05b8SZbigniew Bodek 
37046959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
37059b8d05b8SZbigniew Bodek 	ena_down(adapter);
370632f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, true);
37076959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
37089b8d05b8SZbigniew Bodek 
3709d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3710d17b7d87SMarcin Wojtas 	netmap_detach(adapter->ifp);
3711d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3712d17b7d87SMarcin Wojtas 
371330217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
371430217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
37159b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
37169b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
37179b8d05b8SZbigniew Bodek 
37189b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
37193f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
37209b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
37219b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
37229b8d05b8SZbigniew Bodek 
37239b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
37243f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
37259b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
37269b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
37279b8d05b8SZbigniew Bodek 
37289b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
37299b8d05b8SZbigniew Bodek 
37309b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
37319b8d05b8SZbigniew Bodek 
373232f63fa7SMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter)))
373332f63fa7SMarcin Wojtas 		ena_com_rss_destroy(ena_dev);
373432f63fa7SMarcin Wojtas 
373532f63fa7SMarcin Wojtas 	ena_com_delete_host_info(ena_dev);
373632f63fa7SMarcin Wojtas 
37376959869eSMarcin Wojtas 	ENA_LOCK_DESTROY(adapter);
37389b8d05b8SZbigniew Bodek 
37399151c55dSMarcin Wojtas 	if_free(adapter->ifp);
37409151c55dSMarcin Wojtas 
37419b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
37429b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
37439b8d05b8SZbigniew Bodek 
37449b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
37459b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
37469b8d05b8SZbigniew Bodek 
37479b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
37489b8d05b8SZbigniew Bodek }
37499b8d05b8SZbigniew Bodek 
37509b8d05b8SZbigniew Bodek /******************************************************************************
37519b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
37529b8d05b8SZbigniew Bodek  *****************************************************************************/
37539b8d05b8SZbigniew Bodek /**
37549b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
37559b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
37569b8d05b8SZbigniew Bodek  **/
37579b8d05b8SZbigniew Bodek static void
37589b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
37599b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
37609b8d05b8SZbigniew Bodek {
37619b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
37629b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
37639b8d05b8SZbigniew Bodek 	int status;
37649b8d05b8SZbigniew Bodek 	if_t ifp;
37659b8d05b8SZbigniew Bodek 
37669b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
37679b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
37689b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
37699b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
37709b8d05b8SZbigniew Bodek 
37719b8d05b8SZbigniew Bodek 	if (status != 0) {
37729b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
3773fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_LINK_UP, adapter);
377432f63fa7SMarcin Wojtas 		if (!ENA_FLAG_ISSET(ENA_FLAG_ONGOING_RESET, adapter))
377532f63fa7SMarcin Wojtas 			if_link_state_change(ifp, LINK_STATE_UP);
377632f63fa7SMarcin Wojtas 	} else {
37779b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
37789b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
3779fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_LINK_UP, adapter);
37809b8d05b8SZbigniew Bodek 	}
37819b8d05b8SZbigniew Bodek }
37829b8d05b8SZbigniew Bodek 
378340621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
378440621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
378540621d71SMarcin Wojtas {
378640621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
378740621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
378840621d71SMarcin Wojtas 
3789*9eb1615fSMarcin Wojtas 	ENA_WARN(NULL, aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
379040621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
379140621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
379240621d71SMarcin Wojtas 
3793*9eb1615fSMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrome) {
379440621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
379540621d71SMarcin Wojtas 		hints =
379640621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
379740621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
379840621d71SMarcin Wojtas 		break;
379940621d71SMarcin Wojtas 	default:
380040621d71SMarcin Wojtas 		device_printf(adapter->pdev,
380140621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
3802*9eb1615fSMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrome);
380340621d71SMarcin Wojtas 	}
380440621d71SMarcin Wojtas }
380540621d71SMarcin Wojtas 
38069b8d05b8SZbigniew Bodek /**
38079b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
38089b8d05b8SZbigniew Bodek  **/
38099b8d05b8SZbigniew Bodek static void
3810e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
38119b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
38129b8d05b8SZbigniew Bodek {
3813e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3814e6de9a83SMarcin Wojtas 
3815e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
3816e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
38179b8d05b8SZbigniew Bodek }
38189b8d05b8SZbigniew Bodek 
38199b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
38209b8d05b8SZbigniew Bodek     .handlers = {
38219b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
382240621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
38239b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
38249b8d05b8SZbigniew Bodek     },
38259b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
38269b8d05b8SZbigniew Bodek };
38279b8d05b8SZbigniew Bodek 
38289b8d05b8SZbigniew Bodek /*********************************************************************
38299b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
38309b8d05b8SZbigniew Bodek  *********************************************************************/
38319b8d05b8SZbigniew Bodek 
38329b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
38339b8d05b8SZbigniew Bodek     /* Device interface */
38349b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
38359b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
38369b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
38379b8d05b8SZbigniew Bodek     DEVMETHOD_END
38389b8d05b8SZbigniew Bodek };
38399b8d05b8SZbigniew Bodek 
38409b8d05b8SZbigniew Bodek static driver_t ena_driver = {
38419b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
38429b8d05b8SZbigniew Bodek };
38439b8d05b8SZbigniew Bodek 
38449b8d05b8SZbigniew Bodek devclass_t ena_devclass;
38459b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
384640abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
3847329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
38489b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
38499b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
3850d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3851d17b7d87SMarcin Wojtas MODULE_DEPEND(ena, netmap, 1, 1, 1);
3852d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
38539b8d05b8SZbigniew Bodek 
38549b8d05b8SZbigniew Bodek /*********************************************************************/
3855