xref: /freebsd/sys/dev/ena/ena.c (revision 56d41ad5fe9c2fcbc2363bf3ca7e69684a26b4bd)
19b8d05b8SZbigniew Bodek /*-
29b8d05b8SZbigniew Bodek  * BSD LICENSE
39b8d05b8SZbigniew Bodek  *
49d0073e4SMarcin Wojtas  * Copyright (c) 2015-2019 Amazon.com, Inc. or its affiliates.
59b8d05b8SZbigniew Bodek  * All rights reserved.
69b8d05b8SZbigniew Bodek  *
79b8d05b8SZbigniew Bodek  * Redistribution and use in source and binary forms, with or without
89b8d05b8SZbigniew Bodek  * modification, are permitted provided that the following conditions
99b8d05b8SZbigniew Bodek  * are met:
109b8d05b8SZbigniew Bodek  *
119b8d05b8SZbigniew Bodek  * 1. Redistributions of source code must retain the above copyright
129b8d05b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer.
139b8d05b8SZbigniew Bodek  *
149b8d05b8SZbigniew Bodek  * 2. Redistributions in binary form must reproduce the above copyright
159b8d05b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer in the
169b8d05b8SZbigniew Bodek  *    documentation and/or other materials provided with the distribution.
179b8d05b8SZbigniew Bodek  *
189b8d05b8SZbigniew Bodek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199b8d05b8SZbigniew Bodek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
209b8d05b8SZbigniew Bodek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
219b8d05b8SZbigniew Bodek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
229b8d05b8SZbigniew Bodek  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239b8d05b8SZbigniew Bodek  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
249b8d05b8SZbigniew Bodek  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
259b8d05b8SZbigniew Bodek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
269b8d05b8SZbigniew Bodek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279b8d05b8SZbigniew Bodek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
289b8d05b8SZbigniew Bodek  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299b8d05b8SZbigniew Bodek  */
309b8d05b8SZbigniew Bodek #include <sys/cdefs.h>
319b8d05b8SZbigniew Bodek __FBSDID("$FreeBSD$");
329b8d05b8SZbigniew Bodek 
339b8d05b8SZbigniew Bodek #include <sys/param.h>
349b8d05b8SZbigniew Bodek #include <sys/systm.h>
359b8d05b8SZbigniew Bodek #include <sys/bus.h>
369b8d05b8SZbigniew Bodek #include <sys/endian.h>
379b8d05b8SZbigniew Bodek #include <sys/kernel.h>
389b8d05b8SZbigniew Bodek #include <sys/kthread.h>
399b8d05b8SZbigniew Bodek #include <sys/malloc.h>
409b8d05b8SZbigniew Bodek #include <sys/mbuf.h>
419b8d05b8SZbigniew Bodek #include <sys/module.h>
429b8d05b8SZbigniew Bodek #include <sys/rman.h>
439b8d05b8SZbigniew Bodek #include <sys/smp.h>
449b8d05b8SZbigniew Bodek #include <sys/socket.h>
459b8d05b8SZbigniew Bodek #include <sys/sockio.h>
469b8d05b8SZbigniew Bodek #include <sys/sysctl.h>
479b8d05b8SZbigniew Bodek #include <sys/taskqueue.h>
489b8d05b8SZbigniew Bodek #include <sys/time.h>
499b8d05b8SZbigniew Bodek #include <sys/eventhandler.h>
509b8d05b8SZbigniew Bodek 
519b8d05b8SZbigniew Bodek #include <machine/bus.h>
529b8d05b8SZbigniew Bodek #include <machine/resource.h>
539b8d05b8SZbigniew Bodek #include <machine/in_cksum.h>
549b8d05b8SZbigniew Bodek 
559b8d05b8SZbigniew Bodek #include <net/bpf.h>
569b8d05b8SZbigniew Bodek #include <net/ethernet.h>
579b8d05b8SZbigniew Bodek #include <net/if.h>
589b8d05b8SZbigniew Bodek #include <net/if_var.h>
599b8d05b8SZbigniew Bodek #include <net/if_arp.h>
609b8d05b8SZbigniew Bodek #include <net/if_dl.h>
619b8d05b8SZbigniew Bodek #include <net/if_media.h>
629b8d05b8SZbigniew Bodek #include <net/if_types.h>
639b8d05b8SZbigniew Bodek #include <net/if_vlan_var.h>
649b8d05b8SZbigniew Bodek 
659b8d05b8SZbigniew Bodek #include <netinet/in_systm.h>
669b8d05b8SZbigniew Bodek #include <netinet/in.h>
679b8d05b8SZbigniew Bodek #include <netinet/if_ether.h>
689b8d05b8SZbigniew Bodek #include <netinet/ip.h>
699b8d05b8SZbigniew Bodek #include <netinet/ip6.h>
709b8d05b8SZbigniew Bodek #include <netinet/tcp.h>
719b8d05b8SZbigniew Bodek #include <netinet/udp.h>
729b8d05b8SZbigniew Bodek 
739b8d05b8SZbigniew Bodek #include <dev/pci/pcivar.h>
749b8d05b8SZbigniew Bodek #include <dev/pci/pcireg.h>
759b8d05b8SZbigniew Bodek 
764fa9e02dSMarcin Wojtas #include <vm/vm.h>
774fa9e02dSMarcin Wojtas #include <vm/pmap.h>
784fa9e02dSMarcin Wojtas 
7938c7b965SMarcin Wojtas #include "ena_datapath.h"
809b8d05b8SZbigniew Bodek #include "ena.h"
819b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
829b8d05b8SZbigniew Bodek 
83d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
84d17b7d87SMarcin Wojtas #include "ena_netmap.h"
85d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
86d17b7d87SMarcin Wojtas 
879b8d05b8SZbigniew Bodek /*********************************************************
889b8d05b8SZbigniew Bodek  *  Function prototypes
899b8d05b8SZbigniew Bodek  *********************************************************/
909b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
919b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
929b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
939b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
949b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
959b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
969b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
979b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
989b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
997d8c4feeSMarcin Wojtas static void	ena_init_io_rings_basic(struct ena_adapter *);
1007d8c4feeSMarcin Wojtas static void	ena_init_io_rings_advanced(struct ena_adapter *);
101cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
1029b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
1039b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
1049b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
1059b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
1069b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
1079b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
1086f2128c7SMarcin Wojtas static void	ena_release_all_tx_dmamap(struct ena_ring *);
1099b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1109b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1119b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1129b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
1139b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1149b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1159b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1169b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1179b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1189b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1199b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1209b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1219b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1229b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1239b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1249b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1259b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1269b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1279b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1289b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1299b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1305cb9db07SMarcin Wojtas static int	ena_handle_msix(void *);
1319b8d05b8SZbigniew Bodek static int	ena_enable_msix(struct ena_adapter *);
1329b8d05b8SZbigniew Bodek static void	ena_setup_mgmnt_intr(struct ena_adapter *);
13377958fcdSMarcin Wojtas static int	ena_setup_io_intr(struct ena_adapter *);
1349b8d05b8SZbigniew Bodek static int	ena_request_mgmnt_irq(struct ena_adapter *);
1359b8d05b8SZbigniew Bodek static int	ena_request_io_irq(struct ena_adapter *);
1369b8d05b8SZbigniew Bodek static void	ena_free_mgmnt_irq(struct ena_adapter *);
1379b8d05b8SZbigniew Bodek static void	ena_free_io_irq(struct ena_adapter *);
1389b8d05b8SZbigniew Bodek static void	ena_free_irqs(struct ena_adapter*);
1399b8d05b8SZbigniew Bodek static void	ena_disable_msix(struct ena_adapter *);
1409b8d05b8SZbigniew Bodek static void	ena_unmask_all_io_irqs(struct ena_adapter *);
1419b8d05b8SZbigniew Bodek static int	ena_rss_configure(struct ena_adapter *);
1429b8d05b8SZbigniew Bodek static int	ena_up_complete(struct ena_adapter *);
1439b8d05b8SZbigniew Bodek static uint64_t	ena_get_counter(if_t, ift_counter);
1449b8d05b8SZbigniew Bodek static int	ena_media_change(if_t);
1459b8d05b8SZbigniew Bodek static void	ena_media_status(if_t, struct ifmediareq *);
1469b8d05b8SZbigniew Bodek static void	ena_init(void *);
1479b8d05b8SZbigniew Bodek static int	ena_ioctl(if_t, u_long, caddr_t);
1489b8d05b8SZbigniew Bodek static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
1499b8d05b8SZbigniew Bodek static void	ena_update_host_info(struct ena_admin_host_info *, if_t);
1509b8d05b8SZbigniew Bodek static void	ena_update_hwassist(struct ena_adapter *);
1519b8d05b8SZbigniew Bodek static int	ena_setup_ifnet(device_t, struct ena_adapter *,
1529b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1534fa9e02dSMarcin Wojtas static int	ena_enable_wc(struct resource *);
1544fa9e02dSMarcin Wojtas static int	ena_set_queues_placement_policy(device_t, struct ena_com_dev *,
1554fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *, struct ena_llq_configurations *);
1567d8c4feeSMarcin Wojtas static uint32_t	ena_calc_max_io_queue_num(device_t, struct ena_com_dev *,
1579b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1587d8c4feeSMarcin Wojtas static int	ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *);
1599b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1609b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
16146021271SMarcin Wojtas static void	ena_config_host_info(struct ena_com_dev *, device_t);
1629b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1639b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1649b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1659b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
166aa9c3226SMarcin Wojtas static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *);
1679b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1689b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1699b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1709b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1719b8d05b8SZbigniew Bodek 
1729b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1739b8d05b8SZbigniew Bodek 
1749b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
1759b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
1769b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
1779b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
1789b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
1799b8d05b8SZbigniew Bodek     /* Last entry */
1809b8d05b8SZbigniew Bodek     { 0, 0, 0 }
1819b8d05b8SZbigniew Bodek };
1829b8d05b8SZbigniew Bodek 
1839b8d05b8SZbigniew Bodek /*
1849b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
1859b8d05b8SZbigniew Bodek  */
1869b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
1879b8d05b8SZbigniew Bodek 
1889b8d05b8SZbigniew Bodek void
1899b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1909b8d05b8SZbigniew Bodek {
1910bdffe59SMarcin Wojtas 	if (error != 0)
1929b8d05b8SZbigniew Bodek 		return;
1939b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
1949b8d05b8SZbigniew Bodek }
1959b8d05b8SZbigniew Bodek 
1969b8d05b8SZbigniew Bodek int
1979b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
1989b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
1999b8d05b8SZbigniew Bodek {
2009b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2010bdffe59SMarcin Wojtas 	uint32_t maxsize;
2020bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2039b8d05b8SZbigniew Bodek 	int error;
2049b8d05b8SZbigniew Bodek 
2050bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2060bdffe59SMarcin Wojtas 
2070bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2083f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2099b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2100bdffe59SMarcin Wojtas 
2119b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2129b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2138a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2148a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2159b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2169b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2179b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2189b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2199b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2209b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2219b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2229b8d05b8SZbigniew Bodek 	    &dma->tag);
2233f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2244e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2259b8d05b8SZbigniew Bodek 		goto fail_tag;
2269b8d05b8SZbigniew Bodek 	}
2279b8d05b8SZbigniew Bodek 
2289b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2299b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2303f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2314e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2324e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2339b8d05b8SZbigniew Bodek 		goto fail_map_create;
2349b8d05b8SZbigniew Bodek 	}
2359b8d05b8SZbigniew Bodek 
2369b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2379b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2389b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2393f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
2404e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2419b8d05b8SZbigniew Bodek 		goto fail_map_load;
2429b8d05b8SZbigniew Bodek 	}
2439b8d05b8SZbigniew Bodek 
244e8073738SMarcin Wojtas 	bus_dmamap_sync(dma->tag, dma->map,
245e8073738SMarcin Wojtas 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
246e8073738SMarcin Wojtas 
2479b8d05b8SZbigniew Bodek 	return (0);
2489b8d05b8SZbigniew Bodek 
2499b8d05b8SZbigniew Bodek fail_map_load:
2509b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2517d2544e6SMarcin Wojtas fail_map_create:
2529b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2539b8d05b8SZbigniew Bodek fail_tag:
2549b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2555b14f92eSMarcin Wojtas 	dma->vaddr = NULL;
2565b14f92eSMarcin Wojtas 	dma->paddr = 0;
2579b8d05b8SZbigniew Bodek 
2589b8d05b8SZbigniew Bodek 	return (error);
2599b8d05b8SZbigniew Bodek }
2609b8d05b8SZbigniew Bodek 
2618483b844SMarcin Wojtas /*
2628483b844SMarcin Wojtas  * This function should generate unique key for the whole driver.
2638483b844SMarcin Wojtas  * If the key was already genereated in the previous call (for example
2648483b844SMarcin Wojtas  * for another adapter), then it should be returned instead.
2658483b844SMarcin Wojtas  */
2668483b844SMarcin Wojtas void
2678483b844SMarcin Wojtas ena_rss_key_fill(void *key, size_t size)
2688483b844SMarcin Wojtas {
2698483b844SMarcin Wojtas 	static bool key_generated;
2708483b844SMarcin Wojtas 	static uint8_t default_key[ENA_HASH_KEY_SIZE];
2718483b844SMarcin Wojtas 
2728483b844SMarcin Wojtas 	KASSERT(size <= ENA_HASH_KEY_SIZE, ("Requested more bytes than ENA RSS key can hold"));
2738483b844SMarcin Wojtas 
2748483b844SMarcin Wojtas 	if (!key_generated) {
2758483b844SMarcin Wojtas 		arc4random_buf(default_key, ENA_HASH_KEY_SIZE);
2768483b844SMarcin Wojtas 		key_generated = true;
2778483b844SMarcin Wojtas 	}
2788483b844SMarcin Wojtas 
2798483b844SMarcin Wojtas 	memcpy(key, default_key, size);
2808483b844SMarcin Wojtas }
2818483b844SMarcin Wojtas 
2829b8d05b8SZbigniew Bodek static void
2839b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
2849b8d05b8SZbigniew Bodek {
2859b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2869b8d05b8SZbigniew Bodek 
2879b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
2889b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2899b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
2909b8d05b8SZbigniew Bodek 	}
2919b8d05b8SZbigniew Bodek 
2929b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
2939b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2949b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
2959b8d05b8SZbigniew Bodek 	}
2969b8d05b8SZbigniew Bodek }
2979b8d05b8SZbigniew Bodek 
2989b8d05b8SZbigniew Bodek static int
2999b8d05b8SZbigniew Bodek ena_probe(device_t dev)
3009b8d05b8SZbigniew Bodek {
3019b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3029b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3039b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3049b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3059b8d05b8SZbigniew Bodek 
3069b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3079b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3089b8d05b8SZbigniew Bodek 
3099b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3109b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3119b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3129b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
31330425f93SMarcin Wojtas 			ena_trace(ENA_DBG, "vendor=%x device=%x\n",
3149b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3159b8d05b8SZbigniew Bodek 
3169b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3179b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3189b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3199b8d05b8SZbigniew Bodek 		}
3209b8d05b8SZbigniew Bodek 
3219b8d05b8SZbigniew Bodek 		ent++;
3229b8d05b8SZbigniew Bodek 
3239b8d05b8SZbigniew Bodek 	}
3249b8d05b8SZbigniew Bodek 
3259b8d05b8SZbigniew Bodek 	return (ENXIO);
3269b8d05b8SZbigniew Bodek }
3279b8d05b8SZbigniew Bodek 
3289b8d05b8SZbigniew Bodek static int
3299b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3309b8d05b8SZbigniew Bodek {
3319b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3323cfadb28SMarcin Wojtas 	int rc;
3339b8d05b8SZbigniew Bodek 
3343cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3359b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3363cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3373cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3383cfadb28SMarcin Wojtas 		return (EINVAL);
3399b8d05b8SZbigniew Bodek 	}
3409b8d05b8SZbigniew Bodek 
3419b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3423cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
3433cfadb28SMarcin Wojtas 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
3443cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3453cfadb28SMarcin Wojtas 	} else {
3463cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3473cfadb28SMarcin Wojtas 		    new_mtu);
3483cfadb28SMarcin Wojtas 	}
3499b8d05b8SZbigniew Bodek 
3503cfadb28SMarcin Wojtas 	return (rc);
3519b8d05b8SZbigniew Bodek }
3529b8d05b8SZbigniew Bodek 
3539b8d05b8SZbigniew Bodek static inline void
3549b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3559b8d05b8SZbigniew Bodek {
3569b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3579b8d05b8SZbigniew Bodek 
3589b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3599b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3609b8d05b8SZbigniew Bodek }
3619b8d05b8SZbigniew Bodek 
3629b8d05b8SZbigniew Bodek static inline void
3639b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3649b8d05b8SZbigniew Bodek {
3659b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3669b8d05b8SZbigniew Bodek 
3679b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3689b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3699b8d05b8SZbigniew Bodek }
3709b8d05b8SZbigniew Bodek 
3719b8d05b8SZbigniew Bodek static inline void
3729b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3739b8d05b8SZbigniew Bodek {
3749b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3759b8d05b8SZbigniew Bodek 
3769b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3779b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
3789b8d05b8SZbigniew Bodek }
3799b8d05b8SZbigniew Bodek 
3809b8d05b8SZbigniew Bodek static void
3819b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
3829b8d05b8SZbigniew Bodek     uint16_t qid)
3839b8d05b8SZbigniew Bodek {
3849b8d05b8SZbigniew Bodek 
3859b8d05b8SZbigniew Bodek 	ring->qid = qid;
3869b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
3879b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
388d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
389d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
3909b8d05b8SZbigniew Bodek }
3919b8d05b8SZbigniew Bodek 
392cd5d5804SMarcin Wojtas static void
3937d8c4feeSMarcin Wojtas ena_init_io_rings_basic(struct ena_adapter *adapter)
3949b8d05b8SZbigniew Bodek {
3959b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
3969b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
3979b8d05b8SZbigniew Bodek 	struct ena_que *que;
3989b8d05b8SZbigniew Bodek 	int i;
3999b8d05b8SZbigniew Bodek 
4009b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
4019b8d05b8SZbigniew Bodek 
4027d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
4039b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4049b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4059b8d05b8SZbigniew Bodek 
4069b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4079b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4089b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4099b8d05b8SZbigniew Bodek 
4109b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4119b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
4129b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4139b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4149b8d05b8SZbigniew Bodek 
4159b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4169b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4179b8d05b8SZbigniew Bodek 
4189b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4199b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4209b8d05b8SZbigniew Bodek 		que->id = i;
4219b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4229b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4239b8d05b8SZbigniew Bodek 
4249b8d05b8SZbigniew Bodek 		txr->que = que;
4259b8d05b8SZbigniew Bodek 		rxr->que = que;
426efe6ab18SMarcin Wojtas 
427efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4287d8c4feeSMarcin Wojtas 		rxr->rx_mbuf_sz = ena_mbuf_sz;
4299b8d05b8SZbigniew Bodek 	}
4309b8d05b8SZbigniew Bodek }
4319b8d05b8SZbigniew Bodek 
4329b8d05b8SZbigniew Bodek static void
4337d8c4feeSMarcin Wojtas ena_init_io_rings_advanced(struct ena_adapter *adapter)
4347d8c4feeSMarcin Wojtas {
4357d8c4feeSMarcin Wojtas 	struct ena_ring *txr, *rxr;
4367d8c4feeSMarcin Wojtas 	int i;
4377d8c4feeSMarcin Wojtas 
4387d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
4397d8c4feeSMarcin Wojtas 		txr = &adapter->tx_ring[i];
4407d8c4feeSMarcin Wojtas 		rxr = &adapter->rx_ring[i];
4417d8c4feeSMarcin Wojtas 
4427d8c4feeSMarcin Wojtas 		/* Allocate a buf ring */
4437d8c4feeSMarcin Wojtas 		txr->buf_ring_size = adapter->buf_ring_size;
4447d8c4feeSMarcin Wojtas 		txr->br = buf_ring_alloc(txr->buf_ring_size, M_DEVBUF,
4457d8c4feeSMarcin Wojtas 		    M_WAITOK, &txr->ring_mtx);
4467d8c4feeSMarcin Wojtas 
4477d8c4feeSMarcin Wojtas 		/* Allocate Tx statistics. */
4487d8c4feeSMarcin Wojtas 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4497d8c4feeSMarcin Wojtas 		    sizeof(txr->tx_stats));
4507d8c4feeSMarcin Wojtas 
4517d8c4feeSMarcin Wojtas 		/* Allocate Rx statistics. */
4527d8c4feeSMarcin Wojtas 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4537d8c4feeSMarcin Wojtas 		    sizeof(rxr->rx_stats));
4547d8c4feeSMarcin Wojtas 
4557d8c4feeSMarcin Wojtas 		/* Initialize locks */
4567d8c4feeSMarcin Wojtas 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4577d8c4feeSMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
4587d8c4feeSMarcin Wojtas 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4597d8c4feeSMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
4607d8c4feeSMarcin Wojtas 
4617d8c4feeSMarcin Wojtas 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
4627d8c4feeSMarcin Wojtas 	}
4637d8c4feeSMarcin Wojtas }
4647d8c4feeSMarcin Wojtas 
4657d8c4feeSMarcin Wojtas static void
4667d8c4feeSMarcin Wojtas ena_init_io_rings(struct ena_adapter *adapter)
4677d8c4feeSMarcin Wojtas {
4687d8c4feeSMarcin Wojtas 	/*
4697d8c4feeSMarcin Wojtas 	 * IO rings initialization can be divided into the 2 steps:
4707d8c4feeSMarcin Wojtas 	 *   1. Initialize variables and fields with initial values and copy
4717d8c4feeSMarcin Wojtas 	 *      them from adapter/ena_dev (basic)
4727d8c4feeSMarcin Wojtas 	 *   2. Allocate mutex, counters and buf_ring (advanced)
4737d8c4feeSMarcin Wojtas 	 */
4747d8c4feeSMarcin Wojtas 	ena_init_io_rings_basic(adapter);
4757d8c4feeSMarcin Wojtas 	ena_init_io_rings_advanced(adapter);
4767d8c4feeSMarcin Wojtas }
4777d8c4feeSMarcin Wojtas 
4787d8c4feeSMarcin Wojtas static void
4799b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4809b8d05b8SZbigniew Bodek {
4819b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4829b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4839b8d05b8SZbigniew Bodek 
4849b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4859b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4869b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4879b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4889b8d05b8SZbigniew Bodek 
4897d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4907d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4917d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4927d2544e6SMarcin Wojtas 
4939b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
4949b8d05b8SZbigniew Bodek }
4959b8d05b8SZbigniew Bodek 
4969b8d05b8SZbigniew Bodek static void
4979b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
4989b8d05b8SZbigniew Bodek {
4999b8d05b8SZbigniew Bodek 	int i;
5009b8d05b8SZbigniew Bodek 
5017d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
5029b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
5039b8d05b8SZbigniew Bodek 
5049b8d05b8SZbigniew Bodek }
5059b8d05b8SZbigniew Bodek 
5069b8d05b8SZbigniew Bodek static int
5079b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
5089b8d05b8SZbigniew Bodek {
5099b8d05b8SZbigniew Bodek 	int ret;
5109b8d05b8SZbigniew Bodek 
5119b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
5129b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
5139b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5148a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5158a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5169b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5179b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
5188a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
5199b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
5209b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5219b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5229b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
5239b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5249b8d05b8SZbigniew Bodek 
5259b8d05b8SZbigniew Bodek 	return (ret);
5269b8d05b8SZbigniew Bodek }
5279b8d05b8SZbigniew Bodek 
5289b8d05b8SZbigniew Bodek static int
5299b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5309b8d05b8SZbigniew Bodek {
5319b8d05b8SZbigniew Bodek 	int ret;
5329b8d05b8SZbigniew Bodek 
5339b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5349b8d05b8SZbigniew Bodek 
5353f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5369b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5379b8d05b8SZbigniew Bodek 
5389b8d05b8SZbigniew Bodek 	return (ret);
5399b8d05b8SZbigniew Bodek }
5409b8d05b8SZbigniew Bodek 
5419b8d05b8SZbigniew Bodek static int
5429b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5439b8d05b8SZbigniew Bodek {
5449b8d05b8SZbigniew Bodek 	int ret;
5459b8d05b8SZbigniew Bodek 
5469b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5479b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5489b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5498a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5508a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5519b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
55204cf2b88SMarcin Wojtas 	    ena_mbuf_sz,			  /* maxsize 		     */
5534727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
55404cf2b88SMarcin Wojtas 	    ena_mbuf_sz,			  /* maxsegsize 	     */
5559b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5569b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5579b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5589b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5599b8d05b8SZbigniew Bodek 
5609b8d05b8SZbigniew Bodek 	return (ret);
5619b8d05b8SZbigniew Bodek }
5629b8d05b8SZbigniew Bodek 
5639b8d05b8SZbigniew Bodek static int
5649b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5659b8d05b8SZbigniew Bodek {
5669b8d05b8SZbigniew Bodek 	int ret;
5679b8d05b8SZbigniew Bodek 
5689b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5699b8d05b8SZbigniew Bodek 
5703f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5719b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5729b8d05b8SZbigniew Bodek 
5739b8d05b8SZbigniew Bodek 	return (ret);
5749b8d05b8SZbigniew Bodek }
5759b8d05b8SZbigniew Bodek 
5766f2128c7SMarcin Wojtas static void
5776f2128c7SMarcin Wojtas ena_release_all_tx_dmamap(struct ena_ring *tx_ring)
5786f2128c7SMarcin Wojtas {
5796f2128c7SMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
5806f2128c7SMarcin Wojtas 	struct ena_tx_buffer *tx_info;
5816f2128c7SMarcin Wojtas 	bus_dma_tag_t tx_tag = adapter->tx_buf_tag;;
5826f2128c7SMarcin Wojtas 	int i;
5836f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
5846f2128c7SMarcin Wojtas 	struct ena_netmap_tx_info *nm_info;
5856f2128c7SMarcin Wojtas 	int j;
5866f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
5876f2128c7SMarcin Wojtas 
5886f2128c7SMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; ++i) {
5896f2128c7SMarcin Wojtas 		tx_info = &tx_ring->tx_buffer_info[i];
5906f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
5916f2128c7SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP) {
5926f2128c7SMarcin Wojtas 			nm_info = &tx_info->nm_info;
5936f2128c7SMarcin Wojtas 			for (j = 0; j < ENA_PKT_MAX_BUFS; ++j) {
5946f2128c7SMarcin Wojtas 				if (nm_info->map_seg[j] != NULL) {
5956f2128c7SMarcin Wojtas 					bus_dmamap_destroy(tx_tag,
5966f2128c7SMarcin Wojtas 					    nm_info->map_seg[j]);
5976f2128c7SMarcin Wojtas 					nm_info->map_seg[j] = NULL;
5986f2128c7SMarcin Wojtas 				}
5996f2128c7SMarcin Wojtas 			}
6006f2128c7SMarcin Wojtas 		}
6016f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
602888810f0SMarcin Wojtas 		if (tx_info->dmamap != NULL) {
603888810f0SMarcin Wojtas 			bus_dmamap_destroy(tx_tag, tx_info->dmamap);
604888810f0SMarcin Wojtas 			tx_info->dmamap = NULL;
6056f2128c7SMarcin Wojtas 		}
6066f2128c7SMarcin Wojtas 	}
6076f2128c7SMarcin Wojtas }
6086f2128c7SMarcin Wojtas 
6099b8d05b8SZbigniew Bodek /**
6109b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
6119b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6129b8d05b8SZbigniew Bodek  * @qid: queue index
6139b8d05b8SZbigniew Bodek  *
6149b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
6159b8d05b8SZbigniew Bodek  **/
6169b8d05b8SZbigniew Bodek static int
6179b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
6189b8d05b8SZbigniew Bodek {
6199b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
6209b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
6219b8d05b8SZbigniew Bodek 	int size, i, err;
6226f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
6236f2128c7SMarcin Wojtas 	bus_dmamap_t *map;
6246f2128c7SMarcin Wojtas 	int j;
6256f2128c7SMarcin Wojtas 
6266f2128c7SMarcin Wojtas 	ena_netmap_reset_tx_ring(adapter, qid);
6276f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
6289b8d05b8SZbigniew Bodek 
6299b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
6309b8d05b8SZbigniew Bodek 
6319b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6323f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
6337d2544e6SMarcin Wojtas 		return (ENOMEM);
6349b8d05b8SZbigniew Bodek 
6359b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
6369b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6373f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
6387d2544e6SMarcin Wojtas 		goto err_buf_info_free;
6399b8d05b8SZbigniew Bodek 
6404fa9e02dSMarcin Wojtas 	size = tx_ring->tx_max_header_size;
6414fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = malloc(size, M_DEVBUF,
6424fa9e02dSMarcin Wojtas 	    M_NOWAIT | M_ZERO);
6434fa9e02dSMarcin Wojtas 	if (unlikely(tx_ring->push_buf_intermediate_buf == NULL))
6444fa9e02dSMarcin Wojtas 		goto err_tx_ids_free;
6454fa9e02dSMarcin Wojtas 
6469b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
6479b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
6489b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
6499b8d05b8SZbigniew Bodek 
6509b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
6519b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
6529b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
6539b8d05b8SZbigniew Bodek 
6549b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
6559b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
656af66d7d0SMarcin Wojtas 	tx_ring->acum_pkts = 0;
6579b8d05b8SZbigniew Bodek 
6589b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
659b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6609b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
661b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6629b8d05b8SZbigniew Bodek 
6639b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6649b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6659b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
666888810f0SMarcin Wojtas 		    &tx_ring->tx_buffer_info[i].dmamap);
6673f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
6684e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
669888810f0SMarcin Wojtas 			    "Unable to create Tx DMA map for buffer %d\n",
6704fa9e02dSMarcin Wojtas 			    i);
6716f2128c7SMarcin Wojtas 			goto err_map_release;
6729b8d05b8SZbigniew Bodek 		}
6736f2128c7SMarcin Wojtas 
6746f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
6756f2128c7SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP) {
6766f2128c7SMarcin Wojtas 			map = tx_ring->tx_buffer_info[i].nm_info.map_seg;
6776f2128c7SMarcin Wojtas 			for (j = 0; j < ENA_PKT_MAX_BUFS; j++) {
6786f2128c7SMarcin Wojtas 				err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6796f2128c7SMarcin Wojtas 				    &map[j]);
6806f2128c7SMarcin Wojtas 				if (unlikely(err != 0)) {
6816f2128c7SMarcin Wojtas 					ena_trace(ENA_ALERT, "Unable to create "
6826f2128c7SMarcin Wojtas 					    "Tx DMA for buffer %d %d\n", i, j);
6836f2128c7SMarcin Wojtas 					goto err_map_release;
6846f2128c7SMarcin Wojtas 				}
6856f2128c7SMarcin Wojtas 			}
6866f2128c7SMarcin Wojtas 		}
6876f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
6889b8d05b8SZbigniew Bodek 	}
6899b8d05b8SZbigniew Bodek 
6909b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6919b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6929b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6939b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6943f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
6954e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT,
6969b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6979b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6986f2128c7SMarcin Wojtas 		goto err_map_release;
6999b8d05b8SZbigniew Bodek 	}
7009b8d05b8SZbigniew Bodek 
7015cb9db07SMarcin Wojtas 	tx_ring->running = true;
7025cb9db07SMarcin Wojtas 
7039b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
7049b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
7059b8d05b8SZbigniew Bodek 
7069b8d05b8SZbigniew Bodek 	return (0);
7079b8d05b8SZbigniew Bodek 
7086f2128c7SMarcin Wojtas err_map_release:
7096f2128c7SMarcin Wojtas 	ena_release_all_tx_dmamap(tx_ring);
7104fa9e02dSMarcin Wojtas err_tx_ids_free:
711cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7127d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
7137d2544e6SMarcin Wojtas err_buf_info_free:
714cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
7157d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
7167d2544e6SMarcin Wojtas 
7179b8d05b8SZbigniew Bodek 	return (ENOMEM);
7189b8d05b8SZbigniew Bodek }
7199b8d05b8SZbigniew Bodek 
7209b8d05b8SZbigniew Bodek /**
7219b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
7229b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7239b8d05b8SZbigniew Bodek  * @qid: queue index
7249b8d05b8SZbigniew Bodek  *
7259b8d05b8SZbigniew Bodek  * Free all transmit software resources
7269b8d05b8SZbigniew Bodek  **/
7279b8d05b8SZbigniew Bodek static void
7289b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
7299b8d05b8SZbigniew Bodek {
7309b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
7316f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
7326f2128c7SMarcin Wojtas 	struct ena_netmap_tx_info *nm_info;
7336f2128c7SMarcin Wojtas 	int j;
7346f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
7359b8d05b8SZbigniew Bodek 
7369b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
7379b8d05b8SZbigniew Bodek 	    NULL))
7389b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
7399b8d05b8SZbigniew Bodek 
7409b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
7419b8d05b8SZbigniew Bodek 
742b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
7439b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
7449b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
7459b8d05b8SZbigniew Bodek 
7469b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
7479b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
748e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag,
749888810f0SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].dmamap, BUS_DMASYNC_POSTWRITE);
7509b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag,
751888810f0SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].dmamap);
7529b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
753888810f0SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].dmamap);
7544fa9e02dSMarcin Wojtas 
7556f2128c7SMarcin Wojtas #ifdef DEV_NETMAP
7566f2128c7SMarcin Wojtas 		if (adapter->ifp->if_capenable & IFCAP_NETMAP) {
7576f2128c7SMarcin Wojtas 			nm_info = &tx_ring->tx_buffer_info[i].nm_info;
7586f2128c7SMarcin Wojtas 			for (j = 0; j < ENA_PKT_MAX_BUFS; j++) {
7596f2128c7SMarcin Wojtas 				if (nm_info->socket_buf_idx[j] != 0) {
7606f2128c7SMarcin Wojtas 					bus_dmamap_sync(adapter->tx_buf_tag,
7616f2128c7SMarcin Wojtas 					    nm_info->map_seg[j],
7626f2128c7SMarcin Wojtas 					    BUS_DMASYNC_POSTWRITE);
7636f2128c7SMarcin Wojtas 					ena_netmap_unload(adapter,
7646f2128c7SMarcin Wojtas 					    nm_info->map_seg[j]);
7656f2128c7SMarcin Wojtas 				}
7666f2128c7SMarcin Wojtas 				bus_dmamap_destroy(adapter->tx_buf_tag,
7676f2128c7SMarcin Wojtas 				    nm_info->map_seg[j]);
7686f2128c7SMarcin Wojtas 				nm_info->socket_buf_idx[j] = 0;
7696f2128c7SMarcin Wojtas 			}
7706f2128c7SMarcin Wojtas 		}
7716f2128c7SMarcin Wojtas #endif /* DEV_NETMAP */
7726f2128c7SMarcin Wojtas 
773e8073738SMarcin Wojtas 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
774e8073738SMarcin Wojtas 		tx_ring->tx_buffer_info[i].mbuf = NULL;
7759b8d05b8SZbigniew Bodek 	}
776416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
7779b8d05b8SZbigniew Bodek 
7789b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
779cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
7809b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
7819b8d05b8SZbigniew Bodek 
782cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7839b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
7844fa9e02dSMarcin Wojtas 
7858483b844SMarcin Wojtas 	free(tx_ring->push_buf_intermediate_buf, M_DEVBUF);
7864fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = NULL;
7879b8d05b8SZbigniew Bodek }
7889b8d05b8SZbigniew Bodek 
7899b8d05b8SZbigniew Bodek /**
7909b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7919b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7929b8d05b8SZbigniew Bodek  *
7939b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7949b8d05b8SZbigniew Bodek  **/
7959b8d05b8SZbigniew Bodek static int
7969b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7979b8d05b8SZbigniew Bodek {
7989b8d05b8SZbigniew Bodek 	int i, rc;
7999b8d05b8SZbigniew Bodek 
8007d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
8019b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
8020bdffe59SMarcin Wojtas 		if (rc != 0) {
8039b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8049b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
8059b8d05b8SZbigniew Bodek 			goto err_setup_tx;
8069b8d05b8SZbigniew Bodek 		}
8077d2544e6SMarcin Wojtas 	}
8089b8d05b8SZbigniew Bodek 
8099b8d05b8SZbigniew Bodek 	return (0);
8109b8d05b8SZbigniew Bodek 
8119b8d05b8SZbigniew Bodek err_setup_tx:
8129b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
8139b8d05b8SZbigniew Bodek 	while (i--)
8149b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
8159b8d05b8SZbigniew Bodek 	return (rc);
8169b8d05b8SZbigniew Bodek }
8179b8d05b8SZbigniew Bodek 
8189b8d05b8SZbigniew Bodek /**
8199b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
8209b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8219b8d05b8SZbigniew Bodek  *
8229b8d05b8SZbigniew Bodek  * Free all transmit software resources
8239b8d05b8SZbigniew Bodek  **/
8249b8d05b8SZbigniew Bodek static void
8259b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
8269b8d05b8SZbigniew Bodek {
8279b8d05b8SZbigniew Bodek 	int i;
8289b8d05b8SZbigniew Bodek 
8297d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
8309b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
8319b8d05b8SZbigniew Bodek }
8329b8d05b8SZbigniew Bodek 
8339b8d05b8SZbigniew Bodek /**
8349b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
8359b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8369b8d05b8SZbigniew Bodek  * @qid: queue index
8379b8d05b8SZbigniew Bodek  *
8389b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8399b8d05b8SZbigniew Bodek  **/
8409b8d05b8SZbigniew Bodek static int
8419b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8429b8d05b8SZbigniew Bodek {
8439b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
8449b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
8459b8d05b8SZbigniew Bodek 	int size, err, i;
8469b8d05b8SZbigniew Bodek 
8479b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
8489b8d05b8SZbigniew Bodek 
8499a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
8509a0f2079SMarcin Wojtas 	ena_netmap_reset_rx_ring(adapter, qid);
8519a0f2079SMarcin Wojtas 	rx_ring->initialized = false;
8529a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
8539a0f2079SMarcin Wojtas 
8549b8d05b8SZbigniew Bodek 	/*
8559b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
8569b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
8579b8d05b8SZbigniew Bodek 	 */
8589b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
8599b8d05b8SZbigniew Bodek 
860cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
8619b8d05b8SZbigniew Bodek 
86243fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
86343fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
86443fefd16SMarcin Wojtas 
86543fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
86643fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
86743fefd16SMarcin Wojtas 
8689b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
8699b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
8709b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
8719b8d05b8SZbigniew Bodek 
8729b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
8739b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
8749b8d05b8SZbigniew Bodek 
8759b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
8769b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
8779b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
8789b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
8799b8d05b8SZbigniew Bodek 		if (err != 0) {
8804e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
8819b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8827d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
8839b8d05b8SZbigniew Bodek 		}
8849b8d05b8SZbigniew Bodek 	}
8859b8d05b8SZbigniew Bodek 
8869b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8870bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8889b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8890bdffe59SMarcin Wojtas 		if (err != 0) {
8909b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8919b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8929b8d05b8SZbigniew Bodek 		} else {
8939b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8949b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8959b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8969b8d05b8SZbigniew Bodek 		}
8979b8d05b8SZbigniew Bodek 	}
8989b8d05b8SZbigniew Bodek 
8999b8d05b8SZbigniew Bodek 	return (0);
9009b8d05b8SZbigniew Bodek 
9017d2544e6SMarcin Wojtas err_buf_info_unmap:
9029b8d05b8SZbigniew Bodek 	while (i--) {
9039b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
9049b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
9059b8d05b8SZbigniew Bodek 	}
9069b8d05b8SZbigniew Bodek 
90743fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
90843fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
909cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
9109b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
9119b8d05b8SZbigniew Bodek 	return (ENOMEM);
9129b8d05b8SZbigniew Bodek }
9139b8d05b8SZbigniew Bodek 
9149b8d05b8SZbigniew Bodek /**
9159b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
9169b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9179b8d05b8SZbigniew Bodek  * @qid: queue index
9189b8d05b8SZbigniew Bodek  *
9199b8d05b8SZbigniew Bodek  * Free all receive software resources
9209b8d05b8SZbigniew Bodek  **/
9219b8d05b8SZbigniew Bodek static void
9229b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
9239b8d05b8SZbigniew Bodek {
9249b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
9259b8d05b8SZbigniew Bodek 
9269b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
9279b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
928e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag,
929e8073738SMarcin Wojtas 		    rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD);
9309b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
9319b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
9329b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
9339b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
9349b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
9359b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
9369b8d05b8SZbigniew Bodek 	}
9379b8d05b8SZbigniew Bodek 
9389b8d05b8SZbigniew Bodek 	/* free LRO resources, */
9399b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
9409b8d05b8SZbigniew Bodek 
9419b8d05b8SZbigniew Bodek 	/* free allocated memory */
942cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
9439b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
9449b8d05b8SZbigniew Bodek 
94543fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
94643fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
9479b8d05b8SZbigniew Bodek }
9489b8d05b8SZbigniew Bodek 
9499b8d05b8SZbigniew Bodek /**
9509b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
9519b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9529b8d05b8SZbigniew Bodek  *
9539b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
9549b8d05b8SZbigniew Bodek  **/
9559b8d05b8SZbigniew Bodek static int
9569b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
9579b8d05b8SZbigniew Bodek {
9589b8d05b8SZbigniew Bodek 	int i, rc = 0;
9599b8d05b8SZbigniew Bodek 
9607d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
9619b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
9620bdffe59SMarcin Wojtas 		if (rc != 0) {
9639b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
9649b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
9659b8d05b8SZbigniew Bodek 			goto err_setup_rx;
9669b8d05b8SZbigniew Bodek 		}
9677d2544e6SMarcin Wojtas 	}
9689b8d05b8SZbigniew Bodek 	return (0);
9699b8d05b8SZbigniew Bodek 
9709b8d05b8SZbigniew Bodek err_setup_rx:
9719b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
9729b8d05b8SZbigniew Bodek 	while (i--)
9739b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9749b8d05b8SZbigniew Bodek 	return (rc);
9759b8d05b8SZbigniew Bodek }
9769b8d05b8SZbigniew Bodek 
9779b8d05b8SZbigniew Bodek /**
9789b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
9799b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9809b8d05b8SZbigniew Bodek  *
9819b8d05b8SZbigniew Bodek  * Free all receive software resources
9829b8d05b8SZbigniew Bodek  **/
9839b8d05b8SZbigniew Bodek static void
9849b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9859b8d05b8SZbigniew Bodek {
9869b8d05b8SZbigniew Bodek 	int i;
9879b8d05b8SZbigniew Bodek 
9887d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
9899b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9909b8d05b8SZbigniew Bodek }
9919b8d05b8SZbigniew Bodek 
9929b8d05b8SZbigniew Bodek static inline int
9939b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9949b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9959b8d05b8SZbigniew Bodek {
9969b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9979b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9989b8d05b8SZbigniew Bodek 	int nsegs, error;
9994727bda6SMarcin Wojtas 	int mlen;
10009b8d05b8SZbigniew Bodek 
10019b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
10023f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
10039b8d05b8SZbigniew Bodek 		return (0);
10049b8d05b8SZbigniew Bodek 
10059b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
100604cf2b88SMarcin Wojtas 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
100704cf2b88SMarcin Wojtas 	    rx_ring->rx_mbuf_sz);
10089b8d05b8SZbigniew Bodek 
10093f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
10104727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
10114727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
10124727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
10139b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
10149b8d05b8SZbigniew Bodek 			return (ENOMEM);
10159b8d05b8SZbigniew Bodek 		}
10164727bda6SMarcin Wojtas 		mlen = MCLBYTES;
10174727bda6SMarcin Wojtas 	} else {
101804cf2b88SMarcin Wojtas 		mlen = rx_ring->rx_mbuf_sz;
10194727bda6SMarcin Wojtas 	}
10209b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
10214727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
10229b8d05b8SZbigniew Bodek 
10239b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
10249b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
102530425f93SMarcin Wojtas 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d\n",
10269b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
10279b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
10289b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
10293f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
10304e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
10319b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
10329b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
10339b8d05b8SZbigniew Bodek 		goto exit;
10349b8d05b8SZbigniew Bodek 
10359b8d05b8SZbigniew Bodek 	}
10369b8d05b8SZbigniew Bodek 
10379b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
10389b8d05b8SZbigniew Bodek 
10399b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
10409b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
10414727bda6SMarcin Wojtas 	ena_buf->len = mlen;
10429b8d05b8SZbigniew Bodek 
10439b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
10449b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
10459b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
10469b8d05b8SZbigniew Bodek 
10479b8d05b8SZbigniew Bodek 	return (0);
10489b8d05b8SZbigniew Bodek 
10499b8d05b8SZbigniew Bodek exit:
10509b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10519b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10529b8d05b8SZbigniew Bodek 	return (EFAULT);
10539b8d05b8SZbigniew Bodek }
10549b8d05b8SZbigniew Bodek 
10559b8d05b8SZbigniew Bodek static void
10569b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
10579b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
10589b8d05b8SZbigniew Bodek {
10599b8d05b8SZbigniew Bodek 
10604e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
10614e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
10629b8d05b8SZbigniew Bodek 		return;
10634e8acd84SMarcin Wojtas 	}
10649b8d05b8SZbigniew Bodek 
1065e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1066e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
10679b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
10689b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10699b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10709b8d05b8SZbigniew Bodek }
10719b8d05b8SZbigniew Bodek 
10729b8d05b8SZbigniew Bodek /**
10739b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
10749b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
10759b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
10769b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
10779b8d05b8SZbigniew Bodek  **/
107838c7b965SMarcin Wojtas int
10799b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
10809b8d05b8SZbigniew Bodek {
10819b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
108243fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
10839b8d05b8SZbigniew Bodek 	uint32_t i;
10849b8d05b8SZbigniew Bodek 	int rc;
10859b8d05b8SZbigniew Bodek 
108630425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d\n",
10879b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10889b8d05b8SZbigniew Bodek 
10899b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10909b8d05b8SZbigniew Bodek 
10919b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
109243fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
109343fefd16SMarcin Wojtas 
10949b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
109530425f93SMarcin Wojtas 		    "RX buffer - next to use: %d\n", next_to_use);
10969b8d05b8SZbigniew Bodek 
109743fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
109843fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
10999a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
1100358bcc4cSMarcin Wojtas 		if (ena_rx_ring_in_netmap(adapter, rx_ring->qid))
11019a0f2079SMarcin Wojtas 			rc = ena_netmap_alloc_rx_slot(adapter, rx_ring, rx_info);
11029a0f2079SMarcin Wojtas 		else
11039a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
11049b8d05b8SZbigniew Bodek 			rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
11053f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
11064e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
11074e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
11084e8acd84SMarcin Wojtas 			    rx_ring->qid);
11099b8d05b8SZbigniew Bodek 			break;
11109b8d05b8SZbigniew Bodek 		}
11119b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
111243fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
11130bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
11144e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
11159b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
11169b8d05b8SZbigniew Bodek 			    rx_ring->qid);
11179b8d05b8SZbigniew Bodek 			break;
11189b8d05b8SZbigniew Bodek 		}
11199b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
11209b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
11219b8d05b8SZbigniew Bodek 	}
11229b8d05b8SZbigniew Bodek 
11233f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
11249b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
11254e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING,
11264e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
11274e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
11289b8d05b8SZbigniew Bodek 	}
11299b8d05b8SZbigniew Bodek 
11308483b844SMarcin Wojtas 	if (likely(i != 0))
11319b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
11328483b844SMarcin Wojtas 
11339b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
11349b8d05b8SZbigniew Bodek 	return (i);
11359b8d05b8SZbigniew Bodek }
11369b8d05b8SZbigniew Bodek 
11377d8c4feeSMarcin Wojtas int
113821823546SMarcin Wojtas ena_update_buf_ring_size(struct ena_adapter *adapter,
113921823546SMarcin Wojtas     uint32_t new_buf_ring_size)
114021823546SMarcin Wojtas {
114121823546SMarcin Wojtas 	uint32_t old_buf_ring_size;
114221823546SMarcin Wojtas 	int rc = 0;
114321823546SMarcin Wojtas 	bool dev_was_up;
114421823546SMarcin Wojtas 
114521823546SMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
114621823546SMarcin Wojtas 
114721823546SMarcin Wojtas 	old_buf_ring_size = adapter->buf_ring_size;
114821823546SMarcin Wojtas 	adapter->buf_ring_size = new_buf_ring_size;
114921823546SMarcin Wojtas 
115021823546SMarcin Wojtas 	dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
115121823546SMarcin Wojtas 	ena_down(adapter);
115221823546SMarcin Wojtas 
115321823546SMarcin Wojtas 	/* Reconfigure buf ring for all Tx rings. */
115421823546SMarcin Wojtas 	ena_free_all_io_rings_resources(adapter);
115521823546SMarcin Wojtas 	ena_init_io_rings_advanced(adapter);
115621823546SMarcin Wojtas 	if (dev_was_up) {
115721823546SMarcin Wojtas 		/*
115821823546SMarcin Wojtas 		 * If ena_up() fails, it's not because of recent buf_ring size
115921823546SMarcin Wojtas 		 * changes. Because of that, we just want to revert old drbr
116021823546SMarcin Wojtas 		 * value and trigger the reset because something else had to
116121823546SMarcin Wojtas 		 * go wrong.
116221823546SMarcin Wojtas 		 */
116321823546SMarcin Wojtas 		rc = ena_up(adapter);
116421823546SMarcin Wojtas 		if (unlikely(rc != 0)) {
116521823546SMarcin Wojtas 			device_printf(adapter->pdev,
116621823546SMarcin Wojtas 			    "Failed to configure device after setting new drbr size: %u. Reverting old value: %u and triggering the reset\n",
116721823546SMarcin Wojtas 			    new_buf_ring_size, old_buf_ring_size);
116821823546SMarcin Wojtas 
116921823546SMarcin Wojtas 			/* Revert old size and trigger the reset */
117021823546SMarcin Wojtas 			adapter->buf_ring_size = old_buf_ring_size;
117121823546SMarcin Wojtas 			ena_free_all_io_rings_resources(adapter);
117221823546SMarcin Wojtas 			ena_init_io_rings_advanced(adapter);
117321823546SMarcin Wojtas 
117421823546SMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET,
117521823546SMarcin Wojtas 			    adapter);
117621823546SMarcin Wojtas 			ena_trigger_reset(adapter, ENA_REGS_RESET_OS_TRIGGER);
117721823546SMarcin Wojtas 
117821823546SMarcin Wojtas 		}
117921823546SMarcin Wojtas 	}
118021823546SMarcin Wojtas 
118121823546SMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
118221823546SMarcin Wojtas 
118321823546SMarcin Wojtas 	return (rc);
118421823546SMarcin Wojtas }
118521823546SMarcin Wojtas 
118621823546SMarcin Wojtas int
11877d8c4feeSMarcin Wojtas ena_update_queue_size(struct ena_adapter *adapter, uint32_t new_tx_size,
11887d8c4feeSMarcin Wojtas     uint32_t new_rx_size)
11897d8c4feeSMarcin Wojtas {
11907d8c4feeSMarcin Wojtas 	uint32_t old_tx_size, old_rx_size;
11917d8c4feeSMarcin Wojtas 	int rc = 0;
11927d8c4feeSMarcin Wojtas 	bool dev_was_up;
11937d8c4feeSMarcin Wojtas 
11947d8c4feeSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
11957d8c4feeSMarcin Wojtas 
11967d8c4feeSMarcin Wojtas 	old_tx_size = adapter->tx_ring_size;
11977d8c4feeSMarcin Wojtas 	old_rx_size = adapter->rx_ring_size;
11987d8c4feeSMarcin Wojtas 	adapter->tx_ring_size = new_tx_size;
11997d8c4feeSMarcin Wojtas 	adapter->rx_ring_size = new_rx_size;
12007d8c4feeSMarcin Wojtas 
12017d8c4feeSMarcin Wojtas 	dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
12027d8c4feeSMarcin Wojtas 	ena_down(adapter);
12037d8c4feeSMarcin Wojtas 
12047d8c4feeSMarcin Wojtas 	/* Configure queues with new size. */
12057d8c4feeSMarcin Wojtas 	ena_init_io_rings_basic(adapter);
12067d8c4feeSMarcin Wojtas 	if (dev_was_up) {
12077d8c4feeSMarcin Wojtas 		rc = ena_up(adapter);
12087d8c4feeSMarcin Wojtas 		if (unlikely(rc != 0)) {
12097d8c4feeSMarcin Wojtas 			device_printf(adapter->pdev,
12107d8c4feeSMarcin Wojtas 			    "Failed to configure device with the new sizes - Tx: %u Rx: %u. Reverting old values - Tx: %u Rx: %u\n",
12117d8c4feeSMarcin Wojtas 			    new_tx_size, new_rx_size, old_tx_size, old_rx_size);
12127d8c4feeSMarcin Wojtas 
12137d8c4feeSMarcin Wojtas 			/* Revert old size. */
12147d8c4feeSMarcin Wojtas 			adapter->tx_ring_size = old_tx_size;
12157d8c4feeSMarcin Wojtas 			adapter->rx_ring_size = old_rx_size;
12167d8c4feeSMarcin Wojtas 			ena_init_io_rings_basic(adapter);
12177d8c4feeSMarcin Wojtas 
12187d8c4feeSMarcin Wojtas 			/* And try again. */
12197d8c4feeSMarcin Wojtas 			rc = ena_up(adapter);
12207d8c4feeSMarcin Wojtas 			if (unlikely(rc != 0)) {
12217d8c4feeSMarcin Wojtas 				device_printf(adapter->pdev,
12227d8c4feeSMarcin Wojtas 				    "Failed to revert old queue sizes. Triggering device reset.\n");
12237d8c4feeSMarcin Wojtas 				/*
12247d8c4feeSMarcin Wojtas 				 * If we've failed again, something had to go
12257d8c4feeSMarcin Wojtas 				 * wrong. After reset, the device should try to
12267d8c4feeSMarcin Wojtas 				 * go up
12277d8c4feeSMarcin Wojtas 				 */
12287d8c4feeSMarcin Wojtas 				ENA_FLAG_SET_ATOMIC(
12297d8c4feeSMarcin Wojtas 				    ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
12307d8c4feeSMarcin Wojtas 				ena_trigger_reset(adapter,
12317d8c4feeSMarcin Wojtas 				    ENA_REGS_RESET_OS_TRIGGER);
12327d8c4feeSMarcin Wojtas 			}
12337d8c4feeSMarcin Wojtas 		}
12347d8c4feeSMarcin Wojtas 	}
12357d8c4feeSMarcin Wojtas 
12367d8c4feeSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
12377d8c4feeSMarcin Wojtas 
12387d8c4feeSMarcin Wojtas 	return (rc);
12397d8c4feeSMarcin Wojtas }
12407d8c4feeSMarcin Wojtas 
12419b8d05b8SZbigniew Bodek static void
1242*56d41ad5SMarcin Wojtas ena_update_io_rings(struct ena_adapter *adapter, uint32_t num)
1243*56d41ad5SMarcin Wojtas {
1244*56d41ad5SMarcin Wojtas 	ena_free_all_io_rings_resources(adapter);
1245*56d41ad5SMarcin Wojtas 	/* Force indirection table to be reinitialized */
1246*56d41ad5SMarcin Wojtas 	ena_com_rss_destroy(adapter->ena_dev);
1247*56d41ad5SMarcin Wojtas 
1248*56d41ad5SMarcin Wojtas 	adapter->num_io_queues = num;
1249*56d41ad5SMarcin Wojtas 	ena_init_io_rings(adapter);
1250*56d41ad5SMarcin Wojtas }
1251*56d41ad5SMarcin Wojtas 
1252*56d41ad5SMarcin Wojtas /* Caller should sanitize new_num */
1253*56d41ad5SMarcin Wojtas int
1254*56d41ad5SMarcin Wojtas ena_update_io_queue_nb(struct ena_adapter *adapter, uint32_t new_num)
1255*56d41ad5SMarcin Wojtas {
1256*56d41ad5SMarcin Wojtas 	uint32_t old_num;
1257*56d41ad5SMarcin Wojtas 	int rc = 0;
1258*56d41ad5SMarcin Wojtas 	bool dev_was_up;
1259*56d41ad5SMarcin Wojtas 
1260*56d41ad5SMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
1261*56d41ad5SMarcin Wojtas 
1262*56d41ad5SMarcin Wojtas 	dev_was_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
1263*56d41ad5SMarcin Wojtas 	old_num = adapter->num_io_queues;
1264*56d41ad5SMarcin Wojtas 	ena_down(adapter);
1265*56d41ad5SMarcin Wojtas 
1266*56d41ad5SMarcin Wojtas 	ena_update_io_rings(adapter, new_num);
1267*56d41ad5SMarcin Wojtas 
1268*56d41ad5SMarcin Wojtas 	if (dev_was_up) {
1269*56d41ad5SMarcin Wojtas 		rc = ena_up(adapter);
1270*56d41ad5SMarcin Wojtas 		if (unlikely(rc != 0)) {
1271*56d41ad5SMarcin Wojtas 			device_printf(adapter->pdev,
1272*56d41ad5SMarcin Wojtas 			    "Failed to configure device with %u IO queues. "
1273*56d41ad5SMarcin Wojtas 			    "Reverting to previous value: %u\n",
1274*56d41ad5SMarcin Wojtas 			    new_num, old_num);
1275*56d41ad5SMarcin Wojtas 
1276*56d41ad5SMarcin Wojtas 			ena_update_io_rings(adapter, old_num);
1277*56d41ad5SMarcin Wojtas 
1278*56d41ad5SMarcin Wojtas 			rc = ena_up(adapter);
1279*56d41ad5SMarcin Wojtas 			if (unlikely(rc != 0)) {
1280*56d41ad5SMarcin Wojtas 				device_printf(adapter->pdev,
1281*56d41ad5SMarcin Wojtas 				    "Failed to revert to previous setup IO "
1282*56d41ad5SMarcin Wojtas 				    "queues. Triggering device reset.\n");
1283*56d41ad5SMarcin Wojtas 				ENA_FLAG_SET_ATOMIC(
1284*56d41ad5SMarcin Wojtas 				    ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
1285*56d41ad5SMarcin Wojtas 				ena_trigger_reset(adapter,
1286*56d41ad5SMarcin Wojtas 				    ENA_REGS_RESET_OS_TRIGGER);
1287*56d41ad5SMarcin Wojtas 			}
1288*56d41ad5SMarcin Wojtas 		}
1289*56d41ad5SMarcin Wojtas 	}
1290*56d41ad5SMarcin Wojtas 
1291*56d41ad5SMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
1292*56d41ad5SMarcin Wojtas 
1293*56d41ad5SMarcin Wojtas 	return (rc);
1294*56d41ad5SMarcin Wojtas }
1295*56d41ad5SMarcin Wojtas 
1296*56d41ad5SMarcin Wojtas static void
12979b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
12989b8d05b8SZbigniew Bodek {
12999b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
13009b8d05b8SZbigniew Bodek 	unsigned int i;
13019b8d05b8SZbigniew Bodek 
13029b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
13039b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
13049b8d05b8SZbigniew Bodek 
13050bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
13069b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
13079a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
13089a0f2079SMarcin Wojtas 		if (((if_getflags(adapter->ifp) & IFF_DYING) == 0) &&
13099a0f2079SMarcin Wojtas 		    (adapter->ifp->if_capenable & IFCAP_NETMAP)) {
13109a0f2079SMarcin Wojtas 			if (rx_info->netmap_buf_idx != 0)
13119a0f2079SMarcin Wojtas 				ena_netmap_free_rx_slot(adapter, rx_ring,
13129a0f2079SMarcin Wojtas 				    rx_info);
13139a0f2079SMarcin Wojtas 		}
13149a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
13159b8d05b8SZbigniew Bodek 	}
13169b8d05b8SZbigniew Bodek }
13179b8d05b8SZbigniew Bodek 
13189b8d05b8SZbigniew Bodek /**
13199b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
13209b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
13219b8d05b8SZbigniew Bodek  *
13229b8d05b8SZbigniew Bodek  */
13239b8d05b8SZbigniew Bodek static void
13249b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
13259b8d05b8SZbigniew Bodek {
13269b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
13279b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
13289b8d05b8SZbigniew Bodek 
13297d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
13309b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
13319b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
13329b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
13339b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
13344e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
13354e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
13369a0f2079SMarcin Wojtas #ifdef DEV_NETMAP
13379a0f2079SMarcin Wojtas 		rx_ring->initialized = true;
13389a0f2079SMarcin Wojtas #endif /* DEV_NETMAP */
13399b8d05b8SZbigniew Bodek 	}
13409b8d05b8SZbigniew Bodek }
13419b8d05b8SZbigniew Bodek 
13429b8d05b8SZbigniew Bodek static void
13439b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
13449b8d05b8SZbigniew Bodek {
13459b8d05b8SZbigniew Bodek 	int i;
13469b8d05b8SZbigniew Bodek 
13477d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++)
13489b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
13499b8d05b8SZbigniew Bodek }
13509b8d05b8SZbigniew Bodek 
13519b8d05b8SZbigniew Bodek /**
13529b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
13539b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
13549b8d05b8SZbigniew Bodek  * @qid: queue index
13559b8d05b8SZbigniew Bodek  **/
13569b8d05b8SZbigniew Bodek static void
13579b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
13589b8d05b8SZbigniew Bodek {
13594e8acd84SMarcin Wojtas 	bool print_once = true;
13609b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
13619b8d05b8SZbigniew Bodek 
1362416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
13639b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
13649b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
13659b8d05b8SZbigniew Bodek 
13669b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
13679b8d05b8SZbigniew Bodek 			continue;
13689b8d05b8SZbigniew Bodek 
13694e8acd84SMarcin Wojtas 		if (print_once) {
13704e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
137130425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
13724e8acd84SMarcin Wojtas 			    qid, i);
13734e8acd84SMarcin Wojtas 			print_once = false;
13744e8acd84SMarcin Wojtas 		} else {
13754e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG,
137630425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
13774e8acd84SMarcin Wojtas 			     qid, i);
13784e8acd84SMarcin Wojtas 		}
13799b8d05b8SZbigniew Bodek 
1380888810f0SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag, tx_info->dmamap,
1381e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTWRITE);
1382888810f0SMarcin Wojtas 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->dmamap);
13834fa9e02dSMarcin Wojtas 
13849b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
13859b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
13869b8d05b8SZbigniew Bodek 	}
1387416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
13889b8d05b8SZbigniew Bodek }
13899b8d05b8SZbigniew Bodek 
13909b8d05b8SZbigniew Bodek static void
13919b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
13929b8d05b8SZbigniew Bodek {
13939b8d05b8SZbigniew Bodek 
13947d8c4feeSMarcin Wojtas 	for (int i = 0; i < adapter->num_io_queues; i++)
13959b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
13969b8d05b8SZbigniew Bodek }
13979b8d05b8SZbigniew Bodek 
13989b8d05b8SZbigniew Bodek static void
13999b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
14009b8d05b8SZbigniew Bodek {
14019b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
14029b8d05b8SZbigniew Bodek 	int i;
14039b8d05b8SZbigniew Bodek 
14047d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14059b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
14069b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
14079b8d05b8SZbigniew Bodek 	}
14089b8d05b8SZbigniew Bodek }
14099b8d05b8SZbigniew Bodek 
14109b8d05b8SZbigniew Bodek static void
14119b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
14129b8d05b8SZbigniew Bodek {
14139b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
14149b8d05b8SZbigniew Bodek 	int i;
14159b8d05b8SZbigniew Bodek 
14167d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14179b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
14189b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
14199b8d05b8SZbigniew Bodek 	}
14209b8d05b8SZbigniew Bodek }
14219b8d05b8SZbigniew Bodek 
14229b8d05b8SZbigniew Bodek static void
14239b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
14249b8d05b8SZbigniew Bodek {
14255cb9db07SMarcin Wojtas 	struct ena_que *queue;
14265cb9db07SMarcin Wojtas 	int i;
14275cb9db07SMarcin Wojtas 
14287d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14295cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
14305cb9db07SMarcin Wojtas 		while (taskqueue_cancel(queue->cleanup_tq,
14315cb9db07SMarcin Wojtas 		    &queue->cleanup_task, NULL))
14325cb9db07SMarcin Wojtas 			taskqueue_drain(queue->cleanup_tq,
14335cb9db07SMarcin Wojtas 			    &queue->cleanup_task);
14345cb9db07SMarcin Wojtas 		taskqueue_free(queue->cleanup_tq);
14355cb9db07SMarcin Wojtas 	}
14365cb9db07SMarcin Wojtas 
14379b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
14389b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
14399b8d05b8SZbigniew Bodek }
14409b8d05b8SZbigniew Bodek 
14419b8d05b8SZbigniew Bodek static int
14429b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
14439b8d05b8SZbigniew Bodek {
14449b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
14459b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
14469b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
14475cb9db07SMarcin Wojtas 	struct ena_que *queue;
14489b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
14499b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
14509b8d05b8SZbigniew Bodek 	int rc, i;
14519b8d05b8SZbigniew Bodek 
14529b8d05b8SZbigniew Bodek 	/* Create TX queues */
14537d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14549b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
14559b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
14569b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
14579b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
14589b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
14599b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
14609b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
14619b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
14620bdffe59SMarcin Wojtas 		if (rc != 0) {
14639b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
14649b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
14659b8d05b8SZbigniew Bodek 			goto err_tx;
14669b8d05b8SZbigniew Bodek 		}
14679b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
14689b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
14699b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
14709b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
14710bdffe59SMarcin Wojtas 		if (rc != 0) {
14729b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
14739b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
14749b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
14759b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
14769b8d05b8SZbigniew Bodek 			goto err_tx;
14779b8d05b8SZbigniew Bodek 		}
14789b8d05b8SZbigniew Bodek 	}
14799b8d05b8SZbigniew Bodek 
14809b8d05b8SZbigniew Bodek 	/* Create RX queues */
14817d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
14829b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
14839b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
14849b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
14859b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
14869b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
14879b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
14889b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
14899b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
14903f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
14919b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
14929b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
14939b8d05b8SZbigniew Bodek 			goto err_rx;
14949b8d05b8SZbigniew Bodek 		}
14959b8d05b8SZbigniew Bodek 
14969b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
14979b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
14989b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
14999b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
15003f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
15019b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
15029b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
15039b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
15049b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
15059b8d05b8SZbigniew Bodek 			goto err_rx;
15069b8d05b8SZbigniew Bodek 		}
15079b8d05b8SZbigniew Bodek 	}
15089b8d05b8SZbigniew Bodek 
15097d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
15105cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
15115cb9db07SMarcin Wojtas 
15126c3e93cbSGleb Smirnoff 		NET_TASK_INIT(&queue->cleanup_task, 0, ena_cleanup, queue);
15135cb9db07SMarcin Wojtas 		queue->cleanup_tq = taskqueue_create_fast("ena cleanup",
15145cb9db07SMarcin Wojtas 		    M_WAITOK, taskqueue_thread_enqueue, &queue->cleanup_tq);
15155cb9db07SMarcin Wojtas 
15165cb9db07SMarcin Wojtas 		taskqueue_start_threads(&queue->cleanup_tq, 1, PI_NET,
15175cb9db07SMarcin Wojtas 		    "%s queue %d cleanup",
15185cb9db07SMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
15195cb9db07SMarcin Wojtas 	}
15205cb9db07SMarcin Wojtas 
15219b8d05b8SZbigniew Bodek 	return (0);
15229b8d05b8SZbigniew Bodek 
15239b8d05b8SZbigniew Bodek err_rx:
15249b8d05b8SZbigniew Bodek 	while (i--)
15259b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
15267d8c4feeSMarcin Wojtas 	i = adapter->num_io_queues;
15279b8d05b8SZbigniew Bodek err_tx:
15289b8d05b8SZbigniew Bodek 	while (i--)
15299b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
15309b8d05b8SZbigniew Bodek 
15319b8d05b8SZbigniew Bodek 	return (ENXIO);
15329b8d05b8SZbigniew Bodek }
15339b8d05b8SZbigniew Bodek 
15349b8d05b8SZbigniew Bodek /*********************************************************************
15359b8d05b8SZbigniew Bodek  *
15369b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
15379b8d05b8SZbigniew Bodek  *
15389b8d05b8SZbigniew Bodek  **********************************************************************/
15399b8d05b8SZbigniew Bodek 
15409b8d05b8SZbigniew Bodek /**
15419b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
15429b8d05b8SZbigniew Bodek  * @arg: interrupt number
15439b8d05b8SZbigniew Bodek  **/
15449b8d05b8SZbigniew Bodek static void
15459b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
15469b8d05b8SZbigniew Bodek {
15479b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
15489b8d05b8SZbigniew Bodek 
15499b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1550fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)))
15519b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
15529b8d05b8SZbigniew Bodek }
15539b8d05b8SZbigniew Bodek 
15545cb9db07SMarcin Wojtas /**
15555cb9db07SMarcin Wojtas  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
15565cb9db07SMarcin Wojtas  * @arg: queue
15575cb9db07SMarcin Wojtas  **/
15585cb9db07SMarcin Wojtas static int
15595cb9db07SMarcin Wojtas ena_handle_msix(void *arg)
15605cb9db07SMarcin Wojtas {
15615cb9db07SMarcin Wojtas 	struct ena_que *queue = arg;
15625cb9db07SMarcin Wojtas 	struct ena_adapter *adapter = queue->adapter;
15635cb9db07SMarcin Wojtas 	if_t ifp = adapter->ifp;
15645cb9db07SMarcin Wojtas 
15655cb9db07SMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
15665cb9db07SMarcin Wojtas 		return (FILTER_STRAY);
15675cb9db07SMarcin Wojtas 
15685cb9db07SMarcin Wojtas 	taskqueue_enqueue(queue->cleanup_tq, &queue->cleanup_task);
15695cb9db07SMarcin Wojtas 
15705cb9db07SMarcin Wojtas 	return (FILTER_HANDLED);
15715cb9db07SMarcin Wojtas }
15725cb9db07SMarcin Wojtas 
15739b8d05b8SZbigniew Bodek static int
15749b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
15759b8d05b8SZbigniew Bodek {
15769b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
15778805021aSMarcin Wojtas 	int msix_vecs, msix_req;
15788805021aSMarcin Wojtas 	int i, rc = 0;
15799b8d05b8SZbigniew Bodek 
1580fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1581fd43fd2aSMarcin Wojtas 		device_printf(dev, "Error, MSI-X is already enabled\n");
1582fd43fd2aSMarcin Wojtas 		return (EINVAL);
1583fd43fd2aSMarcin Wojtas 	}
1584fd43fd2aSMarcin Wojtas 
15859b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
15867d8c4feeSMarcin Wojtas 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->max_num_io_queues);
15879b8d05b8SZbigniew Bodek 
1588cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1589cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1590cd5d5804SMarcin Wojtas 
159130425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d\n", msix_vecs);
15929b8d05b8SZbigniew Bodek 
15939b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
15949b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
15959b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
15969b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
15979b8d05b8SZbigniew Bodek 	}
15989b8d05b8SZbigniew Bodek 
15998805021aSMarcin Wojtas 	msix_req = msix_vecs;
16009b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
16013f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
16029b8d05b8SZbigniew Bodek 		device_printf(dev,
16039b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
16047d2544e6SMarcin Wojtas 
16059b8d05b8SZbigniew Bodek 		rc = ENOSPC;
16067d2544e6SMarcin Wojtas 		goto err_msix_free;
16079b8d05b8SZbigniew Bodek 	}
16089b8d05b8SZbigniew Bodek 
16098805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
16102b5b60feSMarcin Wojtas 		if (msix_vecs == ENA_ADMIN_MSIX_VEC) {
16112b5b60feSMarcin Wojtas 			device_printf(dev,
16122b5b60feSMarcin Wojtas 			    "Not enough number of MSI-x allocated: %d\n",
16132b5b60feSMarcin Wojtas 			    msix_vecs);
16142b5b60feSMarcin Wojtas 			pci_release_msi(dev);
16152b5b60feSMarcin Wojtas 			rc = ENOSPC;
16162b5b60feSMarcin Wojtas 			goto err_msix_free;
16172b5b60feSMarcin Wojtas 		}
16188805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
16198805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
16208805021aSMarcin Wojtas 	}
16218805021aSMarcin Wojtas 
16229b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
1623fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
16249b8d05b8SZbigniew Bodek 
16257d2544e6SMarcin Wojtas 	return (0);
16267d2544e6SMarcin Wojtas 
16277d2544e6SMarcin Wojtas err_msix_free:
16287d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
16297d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
16307d2544e6SMarcin Wojtas 
16319b8d05b8SZbigniew Bodek 	return (rc);
16329b8d05b8SZbigniew Bodek }
16339b8d05b8SZbigniew Bodek 
16349b8d05b8SZbigniew Bodek static void
16359b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
16369b8d05b8SZbigniew Bodek {
16379b8d05b8SZbigniew Bodek 
16389b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
16399b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
16409b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
16419b8d05b8SZbigniew Bodek 	/*
16429b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
16439b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
16449b8d05b8SZbigniew Bodek 	 */
16459b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
16469b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
16479b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
16489b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
16499b8d05b8SZbigniew Bodek }
16509b8d05b8SZbigniew Bodek 
165177958fcdSMarcin Wojtas static int
16529b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
16539b8d05b8SZbigniew Bodek {
16549b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
16559b8d05b8SZbigniew Bodek 	int irq_idx;
16569b8d05b8SZbigniew Bodek 
165777958fcdSMarcin Wojtas 	if (adapter->msix_entries == NULL)
165877958fcdSMarcin Wojtas 		return (EINVAL);
165977958fcdSMarcin Wojtas 
16607d8c4feeSMarcin Wojtas 	for (int i = 0; i < adapter->num_io_queues; i++) {
16619b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
16629b8d05b8SZbigniew Bodek 
16639b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
16649b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
16659b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
16669b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
16679b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
16689b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
16699b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
16709b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
1671277f11c4SMarcin Wojtas 
16729b8d05b8SZbigniew Bodek 		/*
1673277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
16749b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
16759b8d05b8SZbigniew Bodek 		 */
16763f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
16779b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
16789b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
16799b8d05b8SZbigniew Bodek 		    last_bind_cpu;
16809b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
16819b8d05b8SZbigniew Bodek 	}
168277958fcdSMarcin Wojtas 
168377958fcdSMarcin Wojtas 	return (0);
16849b8d05b8SZbigniew Bodek }
16859b8d05b8SZbigniew Bodek 
16869b8d05b8SZbigniew Bodek static int
16879b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
16889b8d05b8SZbigniew Bodek {
16899b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
16909b8d05b8SZbigniew Bodek 	unsigned long flags;
16919b8d05b8SZbigniew Bodek 	int rc, rcc;
16929b8d05b8SZbigniew Bodek 
16939b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
16949b8d05b8SZbigniew Bodek 
16959b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
16969b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
16979b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
16989b8d05b8SZbigniew Bodek 
16993f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
17009b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
17019b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
17027d2544e6SMarcin Wojtas 		return (ENXIO);
17039b8d05b8SZbigniew Bodek 	}
17049b8d05b8SZbigniew Bodek 
17050bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
17060bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
17070bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
17083f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
17099b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
17109b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
17119b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
17127d2544e6SMarcin Wojtas 		goto err_res_free;
17139b8d05b8SZbigniew Bodek 	}
17149b8d05b8SZbigniew Bodek 	irq->requested = true;
17159b8d05b8SZbigniew Bodek 
17169b8d05b8SZbigniew Bodek 	return (rc);
17179b8d05b8SZbigniew Bodek 
17187d2544e6SMarcin Wojtas err_res_free:
17194e8acd84SMarcin Wojtas 	ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
17207d2544e6SMarcin Wojtas 	    irq->vector);
17219b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
17229b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
17233f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
17249b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
17259b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
17269b8d05b8SZbigniew Bodek 	irq->res = NULL;
17279b8d05b8SZbigniew Bodek 
17289b8d05b8SZbigniew Bodek 	return (rc);
17299b8d05b8SZbigniew Bodek }
17309b8d05b8SZbigniew Bodek 
17319b8d05b8SZbigniew Bodek static int
17329b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
17339b8d05b8SZbigniew Bodek {
17349b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
17359b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
17369b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
17379b8d05b8SZbigniew Bodek 
1738fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter))) {
17394e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
17404e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
17419b8d05b8SZbigniew Bodek 		return (EINVAL);
17429b8d05b8SZbigniew Bodek 	} else {
17439b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
17449b8d05b8SZbigniew Bodek 	}
17459b8d05b8SZbigniew Bodek 
17469b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
17479b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
17489b8d05b8SZbigniew Bodek 
17493f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
17509b8d05b8SZbigniew Bodek 			continue;
17519b8d05b8SZbigniew Bodek 
17529b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
17539b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
17543f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
1755469a8407SMarcin Wojtas 			rc = ENOMEM;
17569b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
17579b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
17589b8d05b8SZbigniew Bodek 			goto err;
17599b8d05b8SZbigniew Bodek 		}
17609b8d05b8SZbigniew Bodek 
17610bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
17625cb9db07SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, irq->handler, NULL,
17635cb9db07SMarcin Wojtas 		    irq->data, &irq->cookie);
17643f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
17659b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
17669b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
17679b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
17689b8d05b8SZbigniew Bodek 			goto err;
17699b8d05b8SZbigniew Bodek 		}
17709b8d05b8SZbigniew Bodek 		irq->requested = true;
17719b8d05b8SZbigniew Bodek 
17724e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
17739b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
17749b8d05b8SZbigniew Bodek 	}
17759b8d05b8SZbigniew Bodek 
17769b8d05b8SZbigniew Bodek 	return (rc);
17779b8d05b8SZbigniew Bodek 
17789b8d05b8SZbigniew Bodek err:
17799b8d05b8SZbigniew Bodek 
17809b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
17819b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
17829b8d05b8SZbigniew Bodek 		rcc = 0;
17839b8d05b8SZbigniew Bodek 
17849b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
17859b8d05b8SZbigniew Bodek 		   free both intr and resources */
17860bdffe59SMarcin Wojtas 		if (irq->requested)
17879b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
17883f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
17899b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
17909b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
17919b8d05b8SZbigniew Bodek 
17929b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
17939b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
17949b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
17959b8d05b8SZbigniew Bodek 		   this iteration */
17969b8d05b8SZbigniew Bodek 		rcc = 0;
17979b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
17989b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
17999b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
18009b8d05b8SZbigniew Bodek 		}
18013f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
18029b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
18039b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
18049b8d05b8SZbigniew Bodek 		irq->requested = false;
18059b8d05b8SZbigniew Bodek 		irq->res = NULL;
18069b8d05b8SZbigniew Bodek 	}
18079b8d05b8SZbigniew Bodek 
18089b8d05b8SZbigniew Bodek 	return (rc);
18099b8d05b8SZbigniew Bodek }
18109b8d05b8SZbigniew Bodek 
18119b8d05b8SZbigniew Bodek static void
18129b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
18139b8d05b8SZbigniew Bodek {
18149b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
18159b8d05b8SZbigniew Bodek 	int rc;
18169b8d05b8SZbigniew Bodek 
18179b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
18189b8d05b8SZbigniew Bodek 	if (irq->requested) {
18199b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
18209b8d05b8SZbigniew Bodek 		    irq->vector);
18219b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
18223f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
18239b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
18249b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
18259b8d05b8SZbigniew Bodek 		irq->requested = 0;
18269b8d05b8SZbigniew Bodek 	}
18279b8d05b8SZbigniew Bodek 
18289b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
18299b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
18309b8d05b8SZbigniew Bodek 		    irq->vector);
18319b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
18329b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
18339b8d05b8SZbigniew Bodek 		irq->res = NULL;
18343f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
18359b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
18369b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
18379b8d05b8SZbigniew Bodek 	}
18389b8d05b8SZbigniew Bodek }
18399b8d05b8SZbigniew Bodek 
18409b8d05b8SZbigniew Bodek static void
18419b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
18429b8d05b8SZbigniew Bodek {
18439b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
18449b8d05b8SZbigniew Bodek 	int rc;
18459b8d05b8SZbigniew Bodek 
18469b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
18479b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
18489b8d05b8SZbigniew Bodek 		if (irq->requested) {
18499b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
18509b8d05b8SZbigniew Bodek 			    irq->vector);
18519b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
18529b8d05b8SZbigniew Bodek 			    irq->cookie);
18533f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
18549b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
18559b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
18569b8d05b8SZbigniew Bodek 			}
18579b8d05b8SZbigniew Bodek 			irq->requested = 0;
18589b8d05b8SZbigniew Bodek 		}
18599b8d05b8SZbigniew Bodek 
18609b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
18619b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
18629b8d05b8SZbigniew Bodek 			    irq->vector);
18639b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
18649b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
18659b8d05b8SZbigniew Bodek 			irq->res = NULL;
18663f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
18679b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
18689b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
18699b8d05b8SZbigniew Bodek 				    irq->vector);
18709b8d05b8SZbigniew Bodek 			}
18719b8d05b8SZbigniew Bodek 		}
18729b8d05b8SZbigniew Bodek 	}
18739b8d05b8SZbigniew Bodek }
18749b8d05b8SZbigniew Bodek 
18759b8d05b8SZbigniew Bodek static void
18769b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
18779b8d05b8SZbigniew Bodek {
18789b8d05b8SZbigniew Bodek 
18799b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
18809b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
18819b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
18829b8d05b8SZbigniew Bodek }
18839b8d05b8SZbigniew Bodek 
18849b8d05b8SZbigniew Bodek static void
18859b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
18869b8d05b8SZbigniew Bodek {
18879b8d05b8SZbigniew Bodek 
1888fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1889fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
18909b8d05b8SZbigniew Bodek 		pci_release_msi(adapter->pdev);
1891fd43fd2aSMarcin Wojtas 	}
18929b8d05b8SZbigniew Bodek 
18939b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
1894fd43fd2aSMarcin Wojtas 	if (adapter->msix_entries != NULL)
1895cd5d5804SMarcin Wojtas 		free(adapter->msix_entries, M_DEVBUF);
18969b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
18979b8d05b8SZbigniew Bodek }
18989b8d05b8SZbigniew Bodek 
18999b8d05b8SZbigniew Bodek static void
19009b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
19019b8d05b8SZbigniew Bodek {
19029b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
19039b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
19049b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
19059b8d05b8SZbigniew Bodek 	int i;
19069b8d05b8SZbigniew Bodek 
19079b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
19087d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
19099b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
19109b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
19119b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
19129b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
19139b8d05b8SZbigniew Bodek 	}
19149b8d05b8SZbigniew Bodek }
19159b8d05b8SZbigniew Bodek 
19169b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
19170bdffe59SMarcin Wojtas static int
19180bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
19199b8d05b8SZbigniew Bodek {
19209b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
19219b8d05b8SZbigniew Bodek 	int rc;
19229b8d05b8SZbigniew Bodek 
1923*56d41ad5SMarcin Wojtas 	/* In case the RSS table was destroyed */
1924*56d41ad5SMarcin Wojtas 	if (!ena_dev->rss.tbl_log_size) {
1925*56d41ad5SMarcin Wojtas 		rc = ena_rss_init_default(adapter);
1926*56d41ad5SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
1927*56d41ad5SMarcin Wojtas 			device_printf(adapter->pdev,
1928*56d41ad5SMarcin Wojtas 			    "WARNING: RSS was not properly re-initialized,"
1929*56d41ad5SMarcin Wojtas 			    " it will affect bandwidth\n");
1930*56d41ad5SMarcin Wojtas 			ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
1931*56d41ad5SMarcin Wojtas 			return (rc);
1932*56d41ad5SMarcin Wojtas 		}
1933*56d41ad5SMarcin Wojtas 	}
1934*56d41ad5SMarcin Wojtas 
19359b8d05b8SZbigniew Bodek 	/* Set indirect table */
19369b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
19370bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
19380bdffe59SMarcin Wojtas 		return (rc);
19399b8d05b8SZbigniew Bodek 
19409b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
19419b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
19420bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
19430bdffe59SMarcin Wojtas 		return (rc);
19449b8d05b8SZbigniew Bodek 
19459b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
19469b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
19470bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
19480bdffe59SMarcin Wojtas 		return (rc);
19499b8d05b8SZbigniew Bodek 
19500bdffe59SMarcin Wojtas 	return (0);
19519b8d05b8SZbigniew Bodek }
19529b8d05b8SZbigniew Bodek 
19539b8d05b8SZbigniew Bodek static int
19549b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
19559b8d05b8SZbigniew Bodek {
19569b8d05b8SZbigniew Bodek 	int rc;
19579b8d05b8SZbigniew Bodek 
1958fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) {
19599b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
1960*56d41ad5SMarcin Wojtas 		if (rc != 0) {
1961*56d41ad5SMarcin Wojtas 			device_printf(adapter->pdev,
1962*56d41ad5SMarcin Wojtas 			    "Failed to configure RSS\n");
19639b8d05b8SZbigniew Bodek 			return (rc);
19649b8d05b8SZbigniew Bodek 		}
1965*56d41ad5SMarcin Wojtas 	}
19669b8d05b8SZbigniew Bodek 
19677d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
19683f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
19697d2544e6SMarcin Wojtas 		return (rc);
19707d2544e6SMarcin Wojtas 
19719b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
197230217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
197330217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
19749b8d05b8SZbigniew Bodek 
19759b8d05b8SZbigniew Bodek 	return (0);
19769b8d05b8SZbigniew Bodek }
19779b8d05b8SZbigniew Bodek 
197838c7b965SMarcin Wojtas int
19799b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
19809b8d05b8SZbigniew Bodek {
19819b8d05b8SZbigniew Bodek 	int rc = 0;
19829b8d05b8SZbigniew Bodek 
19833f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
19849b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
19859b8d05b8SZbigniew Bodek 		return (ENXIO);
19869b8d05b8SZbigniew Bodek 	}
19879b8d05b8SZbigniew Bodek 
1988579d23aaSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
1989579d23aaSMarcin Wojtas 		return (0);
1990579d23aaSMarcin Wojtas 
19919b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "device is going UP\n");
19929b8d05b8SZbigniew Bodek 
19939b8d05b8SZbigniew Bodek 	/* setup interrupts for IO queues */
199477958fcdSMarcin Wojtas 	rc = ena_setup_io_intr(adapter);
199577958fcdSMarcin Wojtas 	if (unlikely(rc != 0)) {
199677958fcdSMarcin Wojtas 		ena_trace(ENA_ALERT, "error setting up IO interrupt\n");
199777958fcdSMarcin Wojtas 		goto error;
199877958fcdSMarcin Wojtas 	}
19999b8d05b8SZbigniew Bodek 	rc = ena_request_io_irq(adapter);
20003f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
200130425f93SMarcin Wojtas 		ena_trace(ENA_ALERT, "err_req_irq\n");
200277958fcdSMarcin Wojtas 		goto error;
20039b8d05b8SZbigniew Bodek 	}
20049b8d05b8SZbigniew Bodek 
20057d8c4feeSMarcin Wojtas 	device_printf(adapter->pdev,
20067d8c4feeSMarcin Wojtas 	    "Creating %u IO queues. Rx queue size: %d, Tx queue size: %d, LLQ is %s\n",
20077d8c4feeSMarcin Wojtas 	    adapter->num_io_queues,
20087d8c4feeSMarcin Wojtas 	    adapter->rx_ring_size,
20097d8c4feeSMarcin Wojtas 	    adapter->tx_ring_size,
20107d8c4feeSMarcin Wojtas 	    (adapter->ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) ?
20117d8c4feeSMarcin Wojtas 	    "ENABLED" : "DISABLED");
20127d8c4feeSMarcin Wojtas 
20139b8d05b8SZbigniew Bodek 	/* allocate transmit descriptors */
20149b8d05b8SZbigniew Bodek 	rc = ena_setup_all_tx_resources(adapter);
20153f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
201630425f93SMarcin Wojtas 		ena_trace(ENA_ALERT, "err_setup_tx\n");
20179b8d05b8SZbigniew Bodek 		goto err_setup_tx;
20189b8d05b8SZbigniew Bodek 	}
20199b8d05b8SZbigniew Bodek 
20209b8d05b8SZbigniew Bodek 	/* allocate receive descriptors */
20219b8d05b8SZbigniew Bodek 	rc = ena_setup_all_rx_resources(adapter);
20223f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
202330425f93SMarcin Wojtas 		ena_trace(ENA_ALERT, "err_setup_rx\n");
20249b8d05b8SZbigniew Bodek 		goto err_setup_rx;
20259b8d05b8SZbigniew Bodek 	}
20269b8d05b8SZbigniew Bodek 
20279b8d05b8SZbigniew Bodek 	/* create IO queues for Rx & Tx */
20289b8d05b8SZbigniew Bodek 	rc = ena_create_io_queues(adapter);
20293f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
20309b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT,
203130425f93SMarcin Wojtas 			"create IO queues failed\n");
20329b8d05b8SZbigniew Bodek 		goto err_io_que;
20339b8d05b8SZbigniew Bodek 	}
20349b8d05b8SZbigniew Bodek 
2035fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
20369b8d05b8SZbigniew Bodek 		if_link_state_change(adapter->ifp, LINK_STATE_UP);
20379b8d05b8SZbigniew Bodek 
20389b8d05b8SZbigniew Bodek 	rc = ena_up_complete(adapter);
20393f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
20409b8d05b8SZbigniew Bodek 		goto err_up_complete;
20419b8d05b8SZbigniew Bodek 
20429b8d05b8SZbigniew Bodek 	counter_u64_add(adapter->dev_stats.interface_up, 1);
20439b8d05b8SZbigniew Bodek 
20449b8d05b8SZbigniew Bodek 	ena_update_hwassist(adapter);
20459b8d05b8SZbigniew Bodek 
20469b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
20479b8d05b8SZbigniew Bodek 		IFF_DRV_OACTIVE);
20489b8d05b8SZbigniew Bodek 
204924392281SMarcin Wojtas 	/* Activate timer service only if the device is running.
205024392281SMarcin Wojtas 		* If this flag is not set, it means that the driver is being
205124392281SMarcin Wojtas 		* reset and timer service will be activated afterwards.
205224392281SMarcin Wojtas 		*/
205324392281SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)) {
205424392281SMarcin Wojtas 		callout_reset_sbt(&adapter->timer_service, SBT_1S,
205524392281SMarcin Wojtas 			SBT_1S, ena_timer_service, (void *)adapter, 0);
205624392281SMarcin Wojtas 	}
20579b8d05b8SZbigniew Bodek 
2058fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP, adapter);
205993471047SZbigniew Bodek 
206093471047SZbigniew Bodek 	ena_unmask_all_io_irqs(adapter);
20619b8d05b8SZbigniew Bodek 
20629b8d05b8SZbigniew Bodek 	return (0);
20639b8d05b8SZbigniew Bodek 
20649b8d05b8SZbigniew Bodek err_up_complete:
20659b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
20669b8d05b8SZbigniew Bodek err_io_que:
20679b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
20689b8d05b8SZbigniew Bodek err_setup_rx:
20699b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
20709b8d05b8SZbigniew Bodek err_setup_tx:
20719b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
207277958fcdSMarcin Wojtas error:
20739b8d05b8SZbigniew Bodek 	return (rc);
20749b8d05b8SZbigniew Bodek }
20759b8d05b8SZbigniew Bodek 
20769b8d05b8SZbigniew Bodek static uint64_t
20779b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
20789b8d05b8SZbigniew Bodek {
20799b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
20809b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
20819b8d05b8SZbigniew Bodek 
20829b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
20839b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
20849b8d05b8SZbigniew Bodek 
20859b8d05b8SZbigniew Bodek 	switch (cnt) {
20869b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
208730217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
20889b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
208930217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
20909b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
209130217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
20929b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
209330217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
20949b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
209530217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
20966c84cec3SMarcin Wojtas 	case IFCOUNTER_OQDROPS:
20976c84cec3SMarcin Wojtas 		return (counter_u64_fetch(stats->tx_drops));
20989b8d05b8SZbigniew Bodek 	default:
20999b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
21009b8d05b8SZbigniew Bodek 	}
21019b8d05b8SZbigniew Bodek }
21029b8d05b8SZbigniew Bodek 
21039b8d05b8SZbigniew Bodek static int
21049b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
21059b8d05b8SZbigniew Bodek {
21069b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
21079b8d05b8SZbigniew Bodek 	return (0);
21089b8d05b8SZbigniew Bodek }
21099b8d05b8SZbigniew Bodek 
21109b8d05b8SZbigniew Bodek static void
21119b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
21129b8d05b8SZbigniew Bodek {
21139b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
211430425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "enter\n");
21159b8d05b8SZbigniew Bodek 
21166959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
21179b8d05b8SZbigniew Bodek 
21189b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
21199b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
21209b8d05b8SZbigniew Bodek 
2121fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) {
21226959869eSMarcin Wojtas 		ENA_LOCK_UNLOCK(adapter);
2123fd43fd2aSMarcin Wojtas 		ena_trace(ENA_INFO, "Link is down\n");
21249b8d05b8SZbigniew Bodek 		return;
21259b8d05b8SZbigniew Bodek 	}
21269b8d05b8SZbigniew Bodek 
21279b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
2128b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
21299b8d05b8SZbigniew Bodek 
21306959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
21319b8d05b8SZbigniew Bodek }
21329b8d05b8SZbigniew Bodek 
21339b8d05b8SZbigniew Bodek static void
21349b8d05b8SZbigniew Bodek ena_init(void *arg)
21359b8d05b8SZbigniew Bodek {
21369b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
21379b8d05b8SZbigniew Bodek 
2138fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
21396959869eSMarcin Wojtas 		ENA_LOCK_LOCK(adapter);
21409b8d05b8SZbigniew Bodek 		ena_up(adapter);
21416959869eSMarcin Wojtas 		ENA_LOCK_UNLOCK(adapter);
21423d3a90f9SZbigniew Bodek 	}
21439b8d05b8SZbigniew Bodek }
21449b8d05b8SZbigniew Bodek 
21459b8d05b8SZbigniew Bodek static int
21469b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
21479b8d05b8SZbigniew Bodek {
21489b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
21499b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
21509b8d05b8SZbigniew Bodek 	int rc;
21519b8d05b8SZbigniew Bodek 
21529b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
21539b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
21549b8d05b8SZbigniew Bodek 
21559b8d05b8SZbigniew Bodek 	/*
21569b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
21579b8d05b8SZbigniew Bodek 	 */
21589b8d05b8SZbigniew Bodek 	rc = 0;
21599b8d05b8SZbigniew Bodek 	switch (command) {
21609b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2161dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
2162dbf2eb54SMarcin Wojtas 			break;
21636959869eSMarcin Wojtas 		ENA_LOCK_LOCK(adapter);
21649b8d05b8SZbigniew Bodek 		ena_down(adapter);
21659b8d05b8SZbigniew Bodek 
21669b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
21679b8d05b8SZbigniew Bodek 
21689b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
21696959869eSMarcin Wojtas 		ENA_LOCK_UNLOCK(adapter);
21709b8d05b8SZbigniew Bodek 		break;
21719b8d05b8SZbigniew Bodek 
21729b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
21730bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
21740bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
21750bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
21760bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
21779b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
21789b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
21799b8d05b8SZbigniew Bodek 				}
21809b8d05b8SZbigniew Bodek 			} else {
21816959869eSMarcin Wojtas 				ENA_LOCK_LOCK(adapter);
21829b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
21836959869eSMarcin Wojtas 				ENA_LOCK_UNLOCK(adapter);
21849b8d05b8SZbigniew Bodek 			}
21859b8d05b8SZbigniew Bodek 		} else {
21860bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
21876959869eSMarcin Wojtas 				ENA_LOCK_LOCK(adapter);
21889b8d05b8SZbigniew Bodek 				ena_down(adapter);
21896959869eSMarcin Wojtas 				ENA_LOCK_UNLOCK(adapter);
2190e67c6554SZbigniew Bodek 			}
21919b8d05b8SZbigniew Bodek 		}
21929b8d05b8SZbigniew Bodek 		break;
21939b8d05b8SZbigniew Bodek 
21949b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
21959b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
21969b8d05b8SZbigniew Bodek 		break;
21979b8d05b8SZbigniew Bodek 
21989b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
21999b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
22009b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
22019b8d05b8SZbigniew Bodek 		break;
22029b8d05b8SZbigniew Bodek 
22039b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
22049b8d05b8SZbigniew Bodek 		{
22059b8d05b8SZbigniew Bodek 			int reinit = 0;
22069b8d05b8SZbigniew Bodek 
22079b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
22089b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
22099b8d05b8SZbigniew Bodek 				reinit = 1;
22109b8d05b8SZbigniew Bodek 			}
22119b8d05b8SZbigniew Bodek 
22120bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
22130bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
22146959869eSMarcin Wojtas 				ENA_LOCK_LOCK(adapter);
22159b8d05b8SZbigniew Bodek 				ena_down(adapter);
22169b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
22176959869eSMarcin Wojtas 				ENA_LOCK_UNLOCK(adapter);
22189b8d05b8SZbigniew Bodek 			}
22199b8d05b8SZbigniew Bodek 		}
22209b8d05b8SZbigniew Bodek 
22219b8d05b8SZbigniew Bodek 		break;
22229b8d05b8SZbigniew Bodek 	default:
22239b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
22249b8d05b8SZbigniew Bodek 		break;
22259b8d05b8SZbigniew Bodek 	}
22269b8d05b8SZbigniew Bodek 
22279b8d05b8SZbigniew Bodek 	return (rc);
22289b8d05b8SZbigniew Bodek }
22299b8d05b8SZbigniew Bodek 
22309b8d05b8SZbigniew Bodek static int
22319b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
22329b8d05b8SZbigniew Bodek {
22339b8d05b8SZbigniew Bodek 	int caps = 0;
22349b8d05b8SZbigniew Bodek 
22350bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
22369b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
22379b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
22380bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
22399b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
22409b8d05b8SZbigniew Bodek 
22410bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
22429b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
22430bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
22449b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
22459b8d05b8SZbigniew Bodek 
22460bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
22470bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
22489b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
22499b8d05b8SZbigniew Bodek 
22500bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
22510bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
22529b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
22539b8d05b8SZbigniew Bodek 
22540bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
22559b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
22560bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
22579b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
22589b8d05b8SZbigniew Bodek 
22590bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
22600bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
22619b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
22629b8d05b8SZbigniew Bodek 
22639b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
22649b8d05b8SZbigniew Bodek 
22659b8d05b8SZbigniew Bodek 	return (caps);
22669b8d05b8SZbigniew Bodek }
22679b8d05b8SZbigniew Bodek 
22689b8d05b8SZbigniew Bodek static void
22699b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
22709b8d05b8SZbigniew Bodek {
22719b8d05b8SZbigniew Bodek 
22729b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
22739b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
22749b8d05b8SZbigniew Bodek }
22759b8d05b8SZbigniew Bodek 
22769b8d05b8SZbigniew Bodek static void
22779b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
22789b8d05b8SZbigniew Bodek {
22799b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
22809b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
22819b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
22829b8d05b8SZbigniew Bodek 	int flags = 0;
22839b8d05b8SZbigniew Bodek 
22849b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
22859b8d05b8SZbigniew Bodek 
22860bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
22870bdffe59SMarcin Wojtas 		if ((feat &
22880bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
22899b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
22900bdffe59SMarcin Wojtas 		if ((feat &
22919b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
22920bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
22939b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
22949b8d05b8SZbigniew Bodek 	}
22959b8d05b8SZbigniew Bodek 
22960bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
22979b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
22989b8d05b8SZbigniew Bodek 
22990bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
23009b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
23019b8d05b8SZbigniew Bodek 
23020bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
23039b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
23049b8d05b8SZbigniew Bodek 
23059b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
23069b8d05b8SZbigniew Bodek }
23079b8d05b8SZbigniew Bodek 
23089b8d05b8SZbigniew Bodek static int
23099b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
23109b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
23119b8d05b8SZbigniew Bodek {
23129b8d05b8SZbigniew Bodek 	if_t ifp;
23139b8d05b8SZbigniew Bodek 	int caps = 0;
23149b8d05b8SZbigniew Bodek 
23159b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
23163f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
23174e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
23189b8d05b8SZbigniew Bodek 		return (ENXIO);
23199b8d05b8SZbigniew Bodek 	}
23209b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
23219b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
23229b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
23239b8d05b8SZbigniew Bodek 
232492dc69a7SMarcin Wojtas 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
232592dc69a7SMarcin Wojtas 	    IFF_KNOWSEPOCH);
23269b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
23279b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
23289b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
23299b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
23309b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
23319b8d05b8SZbigniew Bodek 
23329b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
23339b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
23349b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
23359b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
23369b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
23379b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
23389b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
23399b8d05b8SZbigniew Bodek 	/* check hardware support */
23409b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
23419b8d05b8SZbigniew Bodek 	/* ... and set them */
23429b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
23439b8d05b8SZbigniew Bodek 
23449b8d05b8SZbigniew Bodek 	/* TSO parameters */
23458a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
23468a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
23478a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
23488a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
23499b8d05b8SZbigniew Bodek 
23509b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
23519b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
23529b8d05b8SZbigniew Bodek 
23539b8d05b8SZbigniew Bodek 	/*
23549b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
23559b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
23569b8d05b8SZbigniew Bodek 	 */
23579b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
23589b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
23599b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
23609b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
23619b8d05b8SZbigniew Bodek 
23629b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
23639b8d05b8SZbigniew Bodek 
23649b8d05b8SZbigniew Bodek 	return (0);
23659b8d05b8SZbigniew Bodek }
23669b8d05b8SZbigniew Bodek 
236738c7b965SMarcin Wojtas void
23689b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
23699b8d05b8SZbigniew Bodek {
2370a195fab0SMarcin Wojtas 	int rc;
23719b8d05b8SZbigniew Bodek 
2372579d23aaSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
2373579d23aaSMarcin Wojtas 		return;
2374579d23aaSMarcin Wojtas 
23759b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "device is going DOWN\n");
23769b8d05b8SZbigniew Bodek 
23779b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
23789b8d05b8SZbigniew Bodek 
2379fd43fd2aSMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP, adapter);
23809b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
23819b8d05b8SZbigniew Bodek 		IFF_DRV_RUNNING);
23829b8d05b8SZbigniew Bodek 
23839b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
23849b8d05b8SZbigniew Bodek 
2385fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) {
2386a195fab0SMarcin Wojtas 		rc = ena_com_dev_reset(adapter->ena_dev,
2387a195fab0SMarcin Wojtas 			adapter->reset_reason);
23883f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
2389a195fab0SMarcin Wojtas 			device_printf(adapter->pdev,
2390a195fab0SMarcin Wojtas 				"Device reset failed\n");
2391a195fab0SMarcin Wojtas 	}
2392a195fab0SMarcin Wojtas 
23939b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
23949b8d05b8SZbigniew Bodek 
23959b8d05b8SZbigniew Bodek 	ena_free_all_tx_bufs(adapter);
23969b8d05b8SZbigniew Bodek 	ena_free_all_rx_bufs(adapter);
23979b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
23989b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
23999b8d05b8SZbigniew Bodek 
24009b8d05b8SZbigniew Bodek 	counter_u64_add(adapter->dev_stats.interface_down, 1);
24019b8d05b8SZbigniew Bodek }
24029b8d05b8SZbigniew Bodek 
24037d8c4feeSMarcin Wojtas static uint32_t
24047d8c4feeSMarcin Wojtas ena_calc_max_io_queue_num(device_t pdev, struct ena_com_dev *ena_dev,
24059b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
24069b8d05b8SZbigniew Bodek {
24077d8c4feeSMarcin Wojtas 	uint32_t io_tx_sq_num, io_tx_cq_num, io_rx_num, max_num_io_queues;
24089b8d05b8SZbigniew Bodek 
24096064f289SMarcin Wojtas 	/* Regular queues capabilities */
24106064f289SMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
24116064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
24126064f289SMarcin Wojtas 		    &get_feat_ctx->max_queue_ext.max_queue_ext;
24134fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, max_queue_ext->max_rx_sq_num,
24144fa9e02dSMarcin Wojtas 			max_queue_ext->max_rx_cq_num);
24156064f289SMarcin Wojtas 
24164fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queue_ext->max_tx_sq_num;
24174fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queue_ext->max_tx_cq_num;
24186064f289SMarcin Wojtas 	} else {
24196064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
24206064f289SMarcin Wojtas 		    &get_feat_ctx->max_queues;
24214fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queues->max_sq_num;
24224fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queues->max_cq_num;
24234fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, io_tx_sq_num, io_tx_cq_num);
24246064f289SMarcin Wojtas 	}
24259b8d05b8SZbigniew Bodek 
24264fa9e02dSMarcin Wojtas 	/* In case of LLQ use the llq fields for the tx SQ/CQ */
24274fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
24284fa9e02dSMarcin Wojtas 		io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
24294fa9e02dSMarcin Wojtas 
24307d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
24317d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_rx_num);
24327d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_tx_sq_num);
24337d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues, io_tx_cq_num);
24349b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
24357d8c4feeSMarcin Wojtas 	max_num_io_queues = min_t(uint32_t, max_num_io_queues,
24367d8c4feeSMarcin Wojtas 	    pci_msix_count(pdev) - 1);
24379b8d05b8SZbigniew Bodek 
24387d8c4feeSMarcin Wojtas 	return (max_num_io_queues);
24399b8d05b8SZbigniew Bodek }
24409b8d05b8SZbigniew Bodek 
24410bdffe59SMarcin Wojtas static int
24424fa9e02dSMarcin Wojtas ena_enable_wc(struct resource *res)
24434fa9e02dSMarcin Wojtas {
2444472d4784SMarcin Wojtas #if defined(__i386) || defined(__amd64) || defined(__aarch64__)
24454fa9e02dSMarcin Wojtas 	vm_offset_t va;
24464fa9e02dSMarcin Wojtas 	vm_size_t len;
24474fa9e02dSMarcin Wojtas 	int rc;
24484fa9e02dSMarcin Wojtas 
24494fa9e02dSMarcin Wojtas 	va = (vm_offset_t)rman_get_virtual(res);
24504fa9e02dSMarcin Wojtas 	len = rman_get_size(res);
24514fa9e02dSMarcin Wojtas 	/* Enable write combining */
2452472d4784SMarcin Wojtas 	rc = pmap_change_attr(va, len, VM_MEMATTR_WRITE_COMBINING);
24534fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
24544fa9e02dSMarcin Wojtas 		ena_trace(ENA_ALERT, "pmap_change_attr failed, %d\n", rc);
24554fa9e02dSMarcin Wojtas 		return (rc);
24564fa9e02dSMarcin Wojtas 	}
24574fa9e02dSMarcin Wojtas 
24584fa9e02dSMarcin Wojtas 	return (0);
24594fa9e02dSMarcin Wojtas #endif
24604fa9e02dSMarcin Wojtas 	return (EOPNOTSUPP);
24614fa9e02dSMarcin Wojtas }
24624fa9e02dSMarcin Wojtas 
24634fa9e02dSMarcin Wojtas static int
24644fa9e02dSMarcin Wojtas ena_set_queues_placement_policy(device_t pdev, struct ena_com_dev *ena_dev,
24654fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *llq,
24664fa9e02dSMarcin Wojtas     struct ena_llq_configurations *llq_default_configurations)
24674fa9e02dSMarcin Wojtas {
24684fa9e02dSMarcin Wojtas 	struct ena_adapter *adapter = device_get_softc(pdev);
24694fa9e02dSMarcin Wojtas 	int rc, rid;
24704fa9e02dSMarcin Wojtas 	uint32_t llq_feature_mask;
24714fa9e02dSMarcin Wojtas 
24724fa9e02dSMarcin Wojtas 	llq_feature_mask = 1 << ENA_ADMIN_LLQ;
24734fa9e02dSMarcin Wojtas 	if (!(ena_dev->supported_features & llq_feature_mask)) {
24744fa9e02dSMarcin Wojtas 		device_printf(pdev,
24754fa9e02dSMarcin Wojtas 		    "LLQ is not supported. Fallback to host mode policy.\n");
24764fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
24774fa9e02dSMarcin Wojtas 		return (0);
24784fa9e02dSMarcin Wojtas 	}
24794fa9e02dSMarcin Wojtas 
24804fa9e02dSMarcin Wojtas 	rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
24814fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
24824fa9e02dSMarcin Wojtas 		device_printf(pdev, "Failed to configure the device mode. "
24834fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
24844fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
24854fa9e02dSMarcin Wojtas 		return (0);
24864fa9e02dSMarcin Wojtas 	}
24874fa9e02dSMarcin Wojtas 
24884fa9e02dSMarcin Wojtas 	/* Nothing to config, exit */
24894fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
24904fa9e02dSMarcin Wojtas 		return (0);
24914fa9e02dSMarcin Wojtas 
24924fa9e02dSMarcin Wojtas 	/* Try to allocate resources for LLQ bar */
24934fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_MEM_BAR);
24944fa9e02dSMarcin Wojtas 	adapter->memory = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
24954fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
24964fa9e02dSMarcin Wojtas 	if (unlikely(adapter->memory == NULL)) {
24974fa9e02dSMarcin Wojtas 		device_printf(pdev, "unable to allocate LLQ bar resource. "
24984fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
24994fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
25004fa9e02dSMarcin Wojtas 		return (0);
25014fa9e02dSMarcin Wojtas 	}
25024fa9e02dSMarcin Wojtas 
25034fa9e02dSMarcin Wojtas 	/* Enable write combining for better LLQ performance */
25044fa9e02dSMarcin Wojtas 	rc = ena_enable_wc(adapter->memory);
25054fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
25064fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to enable write combining.\n");
25074fa9e02dSMarcin Wojtas 		return (rc);
25084fa9e02dSMarcin Wojtas 	}
25094fa9e02dSMarcin Wojtas 
25104fa9e02dSMarcin Wojtas 	/*
25114fa9e02dSMarcin Wojtas 	 * Save virtual address of the device's memory region
25124fa9e02dSMarcin Wojtas 	 * for the ena_com layer.
25134fa9e02dSMarcin Wojtas 	 */
25144fa9e02dSMarcin Wojtas 	ena_dev->mem_bar = rman_get_virtual(adapter->memory);
25154fa9e02dSMarcin Wojtas 
25164fa9e02dSMarcin Wojtas 	return (0);
25174fa9e02dSMarcin Wojtas }
25184fa9e02dSMarcin Wojtas 
25194fa9e02dSMarcin Wojtas static inline
25204fa9e02dSMarcin Wojtas void set_default_llq_configurations(struct ena_llq_configurations *llq_config)
25214fa9e02dSMarcin Wojtas {
25224fa9e02dSMarcin Wojtas 	llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
25234fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
25244fa9e02dSMarcin Wojtas 	llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
25254fa9e02dSMarcin Wojtas 	llq_config->llq_num_decs_before_header =
25264fa9e02dSMarcin Wojtas 	    ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
25274fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size_value = 128;
25284fa9e02dSMarcin Wojtas }
25294fa9e02dSMarcin Wojtas 
25304fa9e02dSMarcin Wojtas static int
25317d8c4feeSMarcin Wojtas ena_calc_io_queue_size(struct ena_calc_queue_size_ctx *ctx)
25329b8d05b8SZbigniew Bodek {
25334fa9e02dSMarcin Wojtas 	struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
25344fa9e02dSMarcin Wojtas 	struct ena_com_dev *ena_dev = ctx->ena_dev;
25356064f289SMarcin Wojtas 	uint32_t tx_queue_size = ENA_DEFAULT_RING_SIZE;
25367d8c4feeSMarcin Wojtas 	uint32_t rx_queue_size = ENA_DEFAULT_RING_SIZE;
25377d8c4feeSMarcin Wojtas 	uint32_t max_tx_queue_size;
25387d8c4feeSMarcin Wojtas 	uint32_t max_rx_queue_size;
25399b8d05b8SZbigniew Bodek 
25404fa9e02dSMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
25416064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
25426064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
25437d8c4feeSMarcin Wojtas 		max_rx_queue_size = min_t(uint32_t,
25447d8c4feeSMarcin Wojtas 		    max_queue_ext->max_rx_cq_depth,
25456064f289SMarcin Wojtas 		    max_queue_ext->max_rx_sq_depth);
25467d8c4feeSMarcin Wojtas 		max_tx_queue_size = max_queue_ext->max_tx_cq_depth;
25474fa9e02dSMarcin Wojtas 
25484fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
25494fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
25507d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
25514fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
25524fa9e02dSMarcin Wojtas 		else
25537d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
25546064f289SMarcin Wojtas 			    max_queue_ext->max_tx_sq_depth);
25554fa9e02dSMarcin Wojtas 
25566064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
25576064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_tx_descs);
25587d8c4feeSMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
25597d8c4feeSMarcin Wojtas 		    max_queue_ext->max_per_packet_rx_descs);
25606064f289SMarcin Wojtas 	} else {
25616064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
25626064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queues;
25637d8c4feeSMarcin Wojtas 		max_rx_queue_size = min_t(uint32_t,
25647d8c4feeSMarcin Wojtas 		    max_queues->max_cq_depth,
25656064f289SMarcin Wojtas 		    max_queues->max_sq_depth);
25667d8c4feeSMarcin Wojtas 		max_tx_queue_size = max_queues->max_cq_depth;
25674fa9e02dSMarcin Wojtas 
25684fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
25694fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
25707d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
25714fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
25724fa9e02dSMarcin Wojtas 		else
25737d8c4feeSMarcin Wojtas 			max_tx_queue_size = min_t(uint32_t, max_tx_queue_size,
25744fa9e02dSMarcin Wojtas 			    max_queues->max_sq_depth);
25754fa9e02dSMarcin Wojtas 
25766064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
25777d8c4feeSMarcin Wojtas 		    max_queues->max_packet_tx_descs);
25787d8c4feeSMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
25796064f289SMarcin Wojtas 		    max_queues->max_packet_rx_descs);
25806064f289SMarcin Wojtas 	}
25819b8d05b8SZbigniew Bodek 
25829b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
25837d8c4feeSMarcin Wojtas 	max_tx_queue_size = 1 << (flsl(max_tx_queue_size) - 1);
25847d8c4feeSMarcin Wojtas 	max_rx_queue_size = 1 << (flsl(max_rx_queue_size) - 1);
25856064f289SMarcin Wojtas 
25867d8c4feeSMarcin Wojtas 	tx_queue_size = clamp_val(tx_queue_size, ENA_MIN_RING_SIZE,
25877d8c4feeSMarcin Wojtas 	    max_tx_queue_size);
25887d8c4feeSMarcin Wojtas 	rx_queue_size = clamp_val(rx_queue_size, ENA_MIN_RING_SIZE,
25897d8c4feeSMarcin Wojtas 	    max_rx_queue_size);
25909b8d05b8SZbigniew Bodek 
25917d8c4feeSMarcin Wojtas 	tx_queue_size = 1 << (flsl(tx_queue_size) - 1);
25927d8c4feeSMarcin Wojtas 	rx_queue_size = 1 << (flsl(rx_queue_size) - 1);
25937d8c4feeSMarcin Wojtas 
25947d8c4feeSMarcin Wojtas 	ctx->max_tx_queue_size = max_tx_queue_size;
25957d8c4feeSMarcin Wojtas 	ctx->max_rx_queue_size = max_rx_queue_size;
25966064f289SMarcin Wojtas 	ctx->tx_queue_size = tx_queue_size;
25977d8c4feeSMarcin Wojtas 	ctx->rx_queue_size = rx_queue_size;
25986064f289SMarcin Wojtas 
25996064f289SMarcin Wojtas 	return (0);
26009b8d05b8SZbigniew Bodek }
26019b8d05b8SZbigniew Bodek 
26020bdffe59SMarcin Wojtas static int
26030bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
26049b8d05b8SZbigniew Bodek {
26059b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
26069b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
26079b8d05b8SZbigniew Bodek 	int qid, rc, i;
26089b8d05b8SZbigniew Bodek 
26099b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
26100bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
26114e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
26127d2544e6SMarcin Wojtas 		return (rc);
26139b8d05b8SZbigniew Bodek 	}
26149b8d05b8SZbigniew Bodek 
26159b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
26167d8c4feeSMarcin Wojtas 		qid = i % adapter->num_io_queues;
26179b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
26189b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
26190bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
26209b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
26217d2544e6SMarcin Wojtas 			goto err_rss_destroy;
26229b8d05b8SZbigniew Bodek 		}
26239b8d05b8SZbigniew Bodek 	}
26249b8d05b8SZbigniew Bodek 
26259b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
26269b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
26270bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
26289b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
26297d2544e6SMarcin Wojtas 		goto err_rss_destroy;
26309b8d05b8SZbigniew Bodek 	}
26319b8d05b8SZbigniew Bodek 
26329b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
26330bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
26349b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
26357d2544e6SMarcin Wojtas 		goto err_rss_destroy;
26369b8d05b8SZbigniew Bodek 	}
26379b8d05b8SZbigniew Bodek 
26389b8d05b8SZbigniew Bodek 	return (0);
26399b8d05b8SZbigniew Bodek 
26407d2544e6SMarcin Wojtas err_rss_destroy:
26419b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
26429b8d05b8SZbigniew Bodek 	return (rc);
26439b8d05b8SZbigniew Bodek }
26449b8d05b8SZbigniew Bodek 
26459b8d05b8SZbigniew Bodek static void
26469b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
26479b8d05b8SZbigniew Bodek {
26489b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
26499b8d05b8SZbigniew Bodek 	devclass_t dc;
26509b8d05b8SZbigniew Bodek 	int max;
26519b8d05b8SZbigniew Bodek 	int rc;
26529b8d05b8SZbigniew Bodek 
26539b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
26543f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
26554e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "No devclass ena\n");
26569b8d05b8SZbigniew Bodek 		return;
26579b8d05b8SZbigniew Bodek 	}
26589b8d05b8SZbigniew Bodek 
26599b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
26609b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
26619b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
26629b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
26639b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
2664fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
26653f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
26669b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
26679b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
26680bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
2669fd43fd2aSMarcin Wojtas 				ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
26709b8d05b8SZbigniew Bodek 			}
26719b8d05b8SZbigniew Bodek 		}
26729b8d05b8SZbigniew Bodek 	}
26739b8d05b8SZbigniew Bodek }
26749b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
26759b8d05b8SZbigniew Bodek 
26760bdffe59SMarcin Wojtas static void
267746021271SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev, device_t dev)
26789b8d05b8SZbigniew Bodek {
26799b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
268046021271SMarcin Wojtas 	uintptr_t rid;
26819b8d05b8SZbigniew Bodek 	int rc;
26829b8d05b8SZbigniew Bodek 
26839b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
26849b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
26853f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
26869b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
26879b8d05b8SZbigniew Bodek 		return;
26889b8d05b8SZbigniew Bodek 	}
26899b8d05b8SZbigniew Bodek 
26909b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
26919b8d05b8SZbigniew Bodek 
269246021271SMarcin Wojtas 	if (pci_get_id(dev, PCI_ID_RID, &rid) == 0)
269346021271SMarcin Wojtas 		host_info->bdf = rid;
26949b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
26959b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
26969b8d05b8SZbigniew Bodek 
26979b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
26989b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
26999b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
27009b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
27019b8d05b8SZbigniew Bodek 
27029b8d05b8SZbigniew Bodek 	host_info->driver_version =
27039b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
27049b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
27059b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
27068ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
27079b8d05b8SZbigniew Bodek 
27089b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
27093f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2710a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
27119b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
27129b8d05b8SZbigniew Bodek 		else
27139b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
27149b8d05b8SZbigniew Bodek 
27159b8d05b8SZbigniew Bodek 		goto err;
27169b8d05b8SZbigniew Bodek 	}
27179b8d05b8SZbigniew Bodek 
27189b8d05b8SZbigniew Bodek 	return;
27199b8d05b8SZbigniew Bodek 
27209b8d05b8SZbigniew Bodek err:
27219b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
27229b8d05b8SZbigniew Bodek }
27239b8d05b8SZbigniew Bodek 
27249b8d05b8SZbigniew Bodek static int
27259b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
27269b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
27279b8d05b8SZbigniew Bodek {
27289b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
27299b8d05b8SZbigniew Bodek 	bool readless_supported;
27309b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
27319b8d05b8SZbigniew Bodek 	int dma_width;
27329b8d05b8SZbigniew Bodek 	int rc;
27339b8d05b8SZbigniew Bodek 
27349b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
27353f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27369b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
27370bdffe59SMarcin Wojtas 		return (rc);
27389b8d05b8SZbigniew Bodek 	}
27399b8d05b8SZbigniew Bodek 
27409b8d05b8SZbigniew Bodek 	/*
27419b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
27429b8d05b8SZbigniew Bodek 	 * read is disabled
27439b8d05b8SZbigniew Bodek 	 */
27449b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
27459b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
27469b8d05b8SZbigniew Bodek 
2747a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
27483f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27499b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
27509b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
27519b8d05b8SZbigniew Bodek 	}
27529b8d05b8SZbigniew Bodek 
27539b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
27543f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27559b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
27569b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
27579b8d05b8SZbigniew Bodek 	}
27589b8d05b8SZbigniew Bodek 
27599b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
27603f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
27619b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
27629b8d05b8SZbigniew Bodek 		rc = dma_width;
27639b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
27649b8d05b8SZbigniew Bodek 	}
27659b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
27669b8d05b8SZbigniew Bodek 
27679b8d05b8SZbigniew Bodek 	/* ENA admin level init */
276867ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
27693f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27709b8d05b8SZbigniew Bodek 		device_printf(pdev,
27719b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
27729b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
27739b8d05b8SZbigniew Bodek 	}
27749b8d05b8SZbigniew Bodek 
27759b8d05b8SZbigniew Bodek 	/*
27769b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
27779b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
27789b8d05b8SZbigniew Bodek 	 * information
27799b8d05b8SZbigniew Bodek 	 */
27809b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
27819b8d05b8SZbigniew Bodek 
278246021271SMarcin Wojtas 	ena_config_host_info(ena_dev, pdev);
27839b8d05b8SZbigniew Bodek 
27849b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
27859b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
27863f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27879b8d05b8SZbigniew Bodek 		device_printf(pdev,
27889b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
27899b8d05b8SZbigniew Bodek 		goto err_admin_init;
27909b8d05b8SZbigniew Bodek 	}
27919b8d05b8SZbigniew Bodek 
2792e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
2793e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
2794e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
279540621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
2796e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
27979b8d05b8SZbigniew Bodek 
27989b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
27999b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
28003f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28019b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
28029b8d05b8SZbigniew Bodek 		goto err_admin_init;
28039b8d05b8SZbigniew Bodek 	}
28049b8d05b8SZbigniew Bodek 
28059b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
28069b8d05b8SZbigniew Bodek 
28070bdffe59SMarcin Wojtas 	return (0);
28089b8d05b8SZbigniew Bodek 
28099b8d05b8SZbigniew Bodek err_admin_init:
28109b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
28119b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
28129b8d05b8SZbigniew Bodek err_mmio_read_less:
28139b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
28149b8d05b8SZbigniew Bodek 
28150bdffe59SMarcin Wojtas 	return (rc);
28169b8d05b8SZbigniew Bodek }
28179b8d05b8SZbigniew Bodek 
2818aa9c3226SMarcin Wojtas static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter)
28199b8d05b8SZbigniew Bodek {
28209b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
28219b8d05b8SZbigniew Bodek 	int rc;
28229b8d05b8SZbigniew Bodek 
28239b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
28243f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28259b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
28260bdffe59SMarcin Wojtas 		return (rc);
28279b8d05b8SZbigniew Bodek 	}
28289b8d05b8SZbigniew Bodek 
28299b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
28309b8d05b8SZbigniew Bodek 
28319b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
28323f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
28339b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
28349b8d05b8SZbigniew Bodek 		goto err_disable_msix;
28359b8d05b8SZbigniew Bodek 	}
28369b8d05b8SZbigniew Bodek 
28379b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
28389b8d05b8SZbigniew Bodek 
28399b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
28409b8d05b8SZbigniew Bodek 
28410bdffe59SMarcin Wojtas 	return (0);
28429b8d05b8SZbigniew Bodek 
28439b8d05b8SZbigniew Bodek err_disable_msix:
28449b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
28459b8d05b8SZbigniew Bodek 
28460bdffe59SMarcin Wojtas 	return (rc);
28479b8d05b8SZbigniew Bodek }
28489b8d05b8SZbigniew Bodek 
28499b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
28509b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
28519b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
28529b8d05b8SZbigniew Bodek {
28539b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
285430217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
28559b8d05b8SZbigniew Bodek 	sbintime_t stime;
285630217e2dSMarcin Wojtas 	uint64_t rx_drops;
28576c84cec3SMarcin Wojtas 	uint64_t tx_drops;
285830217e2dSMarcin Wojtas 
285930217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
286030217e2dSMarcin Wojtas 
286130217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
28626c84cec3SMarcin Wojtas 	tx_drops = ((uint64_t)desc->tx_drops_high << 32) | desc->tx_drops_low;
286330217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
286430217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
28656c84cec3SMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.tx_drops);
28666c84cec3SMarcin Wojtas 	counter_u64_add(adapter->hw_stats.tx_drops, tx_drops);
28679b8d05b8SZbigniew Bodek 
28689b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
28699b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
28709b8d05b8SZbigniew Bodek }
28719b8d05b8SZbigniew Bodek 
28729b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
28739b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
28749b8d05b8SZbigniew Bodek {
28759b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
28769b8d05b8SZbigniew Bodek 
28779b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
28789b8d05b8SZbigniew Bodek 		return;
28799b8d05b8SZbigniew Bodek 
288040621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
28819b8d05b8SZbigniew Bodek 		return;
28829b8d05b8SZbigniew Bodek 
28839b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
28849b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
28859b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
28869b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
28879b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
28889b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
28897926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_KEEP_ALIVE_TO);
28909b8d05b8SZbigniew Bodek 	}
2891858659f7SMarcin Wojtas }
28929b8d05b8SZbigniew Bodek 
28939b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
28949b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
28959b8d05b8SZbigniew Bodek {
28960bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
28970bdffe59SMarcin Wojtas 	    false)) {
28989b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
28999b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
29009b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
29017926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_ADMIN_TO);
29029b8d05b8SZbigniew Bodek 	}
2903858659f7SMarcin Wojtas }
29049b8d05b8SZbigniew Bodek 
290574dba3adSMarcin Wojtas static int
2906d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
2907d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
2908d12f7bfcSMarcin Wojtas {
2909d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
2910d12f7bfcSMarcin Wojtas 		return (0);
2911d12f7bfcSMarcin Wojtas 
2912d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
2913d12f7bfcSMarcin Wojtas 		return (0);
2914d12f7bfcSMarcin Wojtas 
2915d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
2916d12f7bfcSMarcin Wojtas 
2917d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
2918d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
2919d12f7bfcSMarcin Wojtas 		    "Queue = %d. Reset the device\n", rx_ring->qid);
29207926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_MISS_INTERRUPT);
2921d12f7bfcSMarcin Wojtas 		return (EIO);
2922d12f7bfcSMarcin Wojtas 	}
2923d12f7bfcSMarcin Wojtas 
2924d12f7bfcSMarcin Wojtas 	return (0);
2925d12f7bfcSMarcin Wojtas }
2926d12f7bfcSMarcin Wojtas 
2927d12f7bfcSMarcin Wojtas static int
2928d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
292974dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
293074dba3adSMarcin Wojtas {
293174dba3adSMarcin Wojtas 	struct bintime curtime, time;
293274dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
2933d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
293474dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
2935d12f7bfcSMarcin Wojtas 	int i, rc = 0;
293674dba3adSMarcin Wojtas 
293774dba3adSMarcin Wojtas 	getbinuptime(&curtime);
293874dba3adSMarcin Wojtas 
293974dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
294074dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
294174dba3adSMarcin Wojtas 
29420bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
294374dba3adSMarcin Wojtas 			continue;
294474dba3adSMarcin Wojtas 
294574dba3adSMarcin Wojtas 		time = curtime;
294674dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
2947d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
2948d12f7bfcSMarcin Wojtas 
2949d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
2950d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
2951d12f7bfcSMarcin Wojtas 			/*
2952d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
2953d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
2954d12f7bfcSMarcin Wojtas 			 */
2955d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
2956d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
2957d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
29587926bc44SMarcin Wojtas 			ena_trigger_reset(adapter,
29597926bc44SMarcin Wojtas 			    ENA_REGS_RESET_MISS_INTERRUPT);
2960d12f7bfcSMarcin Wojtas 			return (EIO);
2961d12f7bfcSMarcin Wojtas 		}
296274dba3adSMarcin Wojtas 
296374dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
2964d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
296574dba3adSMarcin Wojtas 
296674dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
296774dba3adSMarcin Wojtas 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
296874dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
296974dba3adSMarcin Wojtas 				    tx_ring->qid, i);
297074dba3adSMarcin Wojtas 
297174dba3adSMarcin Wojtas 			tx_buf->print_once = true;
297274dba3adSMarcin Wojtas 			missed_tx++;
2973d12f7bfcSMarcin Wojtas 		}
2974d12f7bfcSMarcin Wojtas 	}
297574dba3adSMarcin Wojtas 
2976d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
297774dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
2978d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
2979d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
29804e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
29817926bc44SMarcin Wojtas 		ena_trigger_reset(adapter, ENA_REGS_RESET_MISS_TX_CMPL);
2982d12f7bfcSMarcin Wojtas 		rc = EIO;
298374dba3adSMarcin Wojtas 	}
298474dba3adSMarcin Wojtas 
2985d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
2986d12f7bfcSMarcin Wojtas 
2987d12f7bfcSMarcin Wojtas 	return (rc);
298874dba3adSMarcin Wojtas }
298974dba3adSMarcin Wojtas 
29909b8d05b8SZbigniew Bodek /*
29919b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
29929b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
29939b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
29949b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
29959b8d05b8SZbigniew Bodek  */
29960bdffe59SMarcin Wojtas static void
2997d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
29989b8d05b8SZbigniew Bodek {
29999b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
3000d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
300174dba3adSMarcin Wojtas 	int i, budget, rc;
30029b8d05b8SZbigniew Bodek 
30039b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
30049b8d05b8SZbigniew Bodek 	rmb();
30059b8d05b8SZbigniew Bodek 
3006fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
30079b8d05b8SZbigniew Bodek 		return;
30089b8d05b8SZbigniew Bodek 
3009fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
30109b8d05b8SZbigniew Bodek 		return;
30119b8d05b8SZbigniew Bodek 
301240621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
30139b8d05b8SZbigniew Bodek 		return;
30149b8d05b8SZbigniew Bodek 
30159b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
30169b8d05b8SZbigniew Bodek 
30177d8c4feeSMarcin Wojtas 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_io_queues; i++) {
30189b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
3019d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
30209b8d05b8SZbigniew Bodek 
3021d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3022d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
3023d12f7bfcSMarcin Wojtas 			return;
3024d12f7bfcSMarcin Wojtas 
3025d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
30260bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
30279b8d05b8SZbigniew Bodek 			return;
30289b8d05b8SZbigniew Bodek 
30299b8d05b8SZbigniew Bodek 		budget--;
3030cd5d5804SMarcin Wojtas 		if (budget == 0) {
30319b8d05b8SZbigniew Bodek 			i++;
30329b8d05b8SZbigniew Bodek 			break;
30339b8d05b8SZbigniew Bodek 		}
30349b8d05b8SZbigniew Bodek 	}
30359b8d05b8SZbigniew Bodek 
30367d8c4feeSMarcin Wojtas 	adapter->next_monitored_tx_qid = i % adapter->num_io_queues;
30379b8d05b8SZbigniew Bodek }
30389b8d05b8SZbigniew Bodek 
30395cb9db07SMarcin Wojtas /* trigger rx cleanup after 2 consecutive detections */
3040efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
3041efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
3042efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
3043efe6ab18SMarcin Wojtas  * for example).
3044efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
3045efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
3046efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
3047efe6ab18SMarcin Wojtas  * able to send new packets.
3048efe6ab18SMarcin Wojtas  *
3049efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
3050efe6ab18SMarcin Wojtas  */
3051efe6ab18SMarcin Wojtas static void
3052efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
3053efe6ab18SMarcin Wojtas {
3054efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
3055efe6ab18SMarcin Wojtas 	int i, refill_required;
3056efe6ab18SMarcin Wojtas 
3057fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
3058efe6ab18SMarcin Wojtas 		return;
3059efe6ab18SMarcin Wojtas 
3060fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
3061efe6ab18SMarcin Wojtas 		return;
3062efe6ab18SMarcin Wojtas 
30637d8c4feeSMarcin Wojtas 	for (i = 0; i < adapter->num_io_queues; i++) {
3064efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
3065efe6ab18SMarcin Wojtas 
30668483b844SMarcin Wojtas 		refill_required = ena_com_free_q_entries(rx_ring->ena_com_io_sq);
3067efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3068efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
3069efe6ab18SMarcin Wojtas 
3070efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
3071efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
3072efe6ab18SMarcin Wojtas 				    1);
3073efe6ab18SMarcin Wojtas 
3074efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
3075efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
3076efe6ab18SMarcin Wojtas 
30775cb9db07SMarcin Wojtas 				taskqueue_enqueue(rx_ring->que->cleanup_tq,
30785cb9db07SMarcin Wojtas 				    &rx_ring->que->cleanup_task);
3079efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
3080efe6ab18SMarcin Wojtas 			}
3081efe6ab18SMarcin Wojtas 		} else {
3082efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
3083efe6ab18SMarcin Wojtas 		}
3084efe6ab18SMarcin Wojtas 	}
3085efe6ab18SMarcin Wojtas }
30869b8d05b8SZbigniew Bodek 
308740621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
308840621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
308940621d71SMarcin Wojtas {
309040621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
309140621d71SMarcin Wojtas 
309240621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
309340621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
309440621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
309540621d71SMarcin Wojtas 
309640621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
309740621d71SMarcin Wojtas 		/* convert to usec */
309840621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
309940621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
310040621d71SMarcin Wojtas 
310140621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
310240621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
310340621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
310440621d71SMarcin Wojtas 
310540621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
310640621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
310740621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
310840621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
310940621d71SMarcin Wojtas 		else
311040621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
311140621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
311240621d71SMarcin Wojtas 	}
311340621d71SMarcin Wojtas 
311440621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
311540621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
311640621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
311740621d71SMarcin Wojtas 		else
311840621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
311940621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
312040621d71SMarcin Wojtas 	}
312140621d71SMarcin Wojtas }
312240621d71SMarcin Wojtas 
31239b8d05b8SZbigniew Bodek static void
31249b8d05b8SZbigniew Bodek ena_timer_service(void *data)
31259b8d05b8SZbigniew Bodek {
31269b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
31279b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
31289b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
31299b8d05b8SZbigniew Bodek 
31309b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
31319b8d05b8SZbigniew Bodek 
31329b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
31339b8d05b8SZbigniew Bodek 
3134d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
31359b8d05b8SZbigniew Bodek 
3136efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
3137efe6ab18SMarcin Wojtas 
31380bdffe59SMarcin Wojtas 	if (host_info != NULL)
31399b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
31409b8d05b8SZbigniew Bodek 
3141fd43fd2aSMarcin Wojtas 	if (unlikely(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
31429b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
31439b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
31449b8d05b8SZbigniew Bodek 		return;
31459b8d05b8SZbigniew Bodek 	}
31469b8d05b8SZbigniew Bodek 
31479b8d05b8SZbigniew Bodek 	/*
31489b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
31499b8d05b8SZbigniew Bodek 	 */
31509b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
31519b8d05b8SZbigniew Bodek }
31529b8d05b8SZbigniew Bodek 
315338c7b965SMarcin Wojtas void
315432f63fa7SMarcin Wojtas ena_destroy_device(struct ena_adapter *adapter, bool graceful)
31559b8d05b8SZbigniew Bodek {
315632f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
31579b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
31589b8d05b8SZbigniew Bodek 	bool dev_up;
315932f63fa7SMarcin Wojtas 
316032f63fa7SMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter))
316132f63fa7SMarcin Wojtas 		return;
316232f63fa7SMarcin Wojtas 
316332f63fa7SMarcin Wojtas 	if_link_state_change(ifp, LINK_STATE_DOWN);
316432f63fa7SMarcin Wojtas 
316532f63fa7SMarcin Wojtas 	callout_drain(&adapter->timer_service);
316632f63fa7SMarcin Wojtas 
316732f63fa7SMarcin Wojtas 	dev_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
316832f63fa7SMarcin Wojtas 	if (dev_up)
316932f63fa7SMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
317032f63fa7SMarcin Wojtas 
317132f63fa7SMarcin Wojtas 	if (!graceful)
317232f63fa7SMarcin Wojtas 		ena_com_set_admin_running_state(ena_dev, false);
317332f63fa7SMarcin Wojtas 
317432f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
317532f63fa7SMarcin Wojtas 		ena_down(adapter);
317632f63fa7SMarcin Wojtas 
317732f63fa7SMarcin Wojtas 	/*
317832f63fa7SMarcin Wojtas 	 * Stop the device from sending AENQ events (if the device was up, and
317932f63fa7SMarcin Wojtas 	 * the trigger reset was on, ena_down already performs device reset)
318032f63fa7SMarcin Wojtas 	 */
318132f63fa7SMarcin Wojtas 	if (!(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter) && dev_up))
318232f63fa7SMarcin Wojtas 		ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
318332f63fa7SMarcin Wojtas 
318432f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
318532f63fa7SMarcin Wojtas 
318632f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
318732f63fa7SMarcin Wojtas 
3188e2735b09SMarcin Wojtas 	/*
3189e2735b09SMarcin Wojtas 	 * IO rings resources should be freed because `ena_restore_device()`
3190e2735b09SMarcin Wojtas 	 * calls (not directly) `ena_enable_msix()`, which re-allocates MSIX
3191e2735b09SMarcin Wojtas 	 * vectors. The amount of MSIX vectors after destroy-restore may be
3192e2735b09SMarcin Wojtas 	 * different than before. Therefore, IO rings resources should be
3193e2735b09SMarcin Wojtas 	 * established from scratch each time.
3194e2735b09SMarcin Wojtas 	 */
3195e2735b09SMarcin Wojtas 	ena_free_all_io_rings_resources(adapter);
3196e2735b09SMarcin Wojtas 
319732f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
319832f63fa7SMarcin Wojtas 
319932f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
320032f63fa7SMarcin Wojtas 
320132f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
320232f63fa7SMarcin Wojtas 
320332f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
320432f63fa7SMarcin Wojtas 
320532f63fa7SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
320632f63fa7SMarcin Wojtas 
320732f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
320832f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
320932f63fa7SMarcin Wojtas }
321032f63fa7SMarcin Wojtas 
321132f63fa7SMarcin Wojtas static int
321232f63fa7SMarcin Wojtas ena_device_validate_params(struct ena_adapter *adapter,
321332f63fa7SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
321432f63fa7SMarcin Wojtas {
321532f63fa7SMarcin Wojtas 
321632f63fa7SMarcin Wojtas 	if (memcmp(get_feat_ctx->dev_attr.mac_addr, adapter->mac_addr,
321732f63fa7SMarcin Wojtas 	    ETHER_ADDR_LEN) != 0) {
321832f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
321932f63fa7SMarcin Wojtas 		    "Error, mac address are different\n");
322032f63fa7SMarcin Wojtas 		return (EINVAL);
322132f63fa7SMarcin Wojtas 	}
322232f63fa7SMarcin Wojtas 
322332f63fa7SMarcin Wojtas 	if (get_feat_ctx->dev_attr.max_mtu < if_getmtu(adapter->ifp)) {
322432f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
322532f63fa7SMarcin Wojtas 		    "Error, device max mtu is smaller than ifp MTU\n");
322632f63fa7SMarcin Wojtas 		return (EINVAL);
322732f63fa7SMarcin Wojtas 	}
322832f63fa7SMarcin Wojtas 
322932f63fa7SMarcin Wojtas 	return 0;
323032f63fa7SMarcin Wojtas }
323132f63fa7SMarcin Wojtas 
323238c7b965SMarcin Wojtas int
323332f63fa7SMarcin Wojtas ena_restore_device(struct ena_adapter *adapter)
323432f63fa7SMarcin Wojtas {
323532f63fa7SMarcin Wojtas 	struct ena_com_dev_get_features_ctx get_feat_ctx;
323632f63fa7SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
323732f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
323832f63fa7SMarcin Wojtas 	device_t dev = adapter->pdev;
323932f63fa7SMarcin Wojtas 	int wd_active;
32409b8d05b8SZbigniew Bodek 	int rc;
32419b8d05b8SZbigniew Bodek 
324232f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
324332f63fa7SMarcin Wojtas 
324432f63fa7SMarcin Wojtas 	rc = ena_device_init(adapter, dev, &get_feat_ctx, &wd_active);
324532f63fa7SMarcin Wojtas 	if (rc != 0) {
324632f63fa7SMarcin Wojtas 		device_printf(dev, "Cannot initialize device\n");
324732f63fa7SMarcin Wojtas 		goto err;
324832f63fa7SMarcin Wojtas 	}
324932f63fa7SMarcin Wojtas 	/*
325032f63fa7SMarcin Wojtas 	 * Only enable WD if it was enabled before reset, so it won't override
325132f63fa7SMarcin Wojtas 	 * value set by the user by the sysctl.
325232f63fa7SMarcin Wojtas 	 */
325332f63fa7SMarcin Wojtas 	if (adapter->wd_active != 0)
325432f63fa7SMarcin Wojtas 		adapter->wd_active = wd_active;
325532f63fa7SMarcin Wojtas 
325632f63fa7SMarcin Wojtas 	rc = ena_device_validate_params(adapter, &get_feat_ctx);
325732f63fa7SMarcin Wojtas 	if (rc != 0) {
325832f63fa7SMarcin Wojtas 		device_printf(dev, "Validation of device parameters failed\n");
325932f63fa7SMarcin Wojtas 		goto err_device_destroy;
326032f63fa7SMarcin Wojtas 	}
326132f63fa7SMarcin Wojtas 
326232f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
326332f63fa7SMarcin Wojtas 	/* Make sure we don't have a race with AENQ Links state handler */
326432f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
326532f63fa7SMarcin Wojtas 		if_link_state_change(ifp, LINK_STATE_UP);
326632f63fa7SMarcin Wojtas 
3267aa9c3226SMarcin Wojtas 	rc = ena_enable_msix_and_set_admin_interrupts(adapter);
326832f63fa7SMarcin Wojtas 	if (rc != 0) {
326932f63fa7SMarcin Wojtas 		device_printf(dev, "Enable MSI-X failed\n");
327032f63fa7SMarcin Wojtas 		goto err_device_destroy;
327132f63fa7SMarcin Wojtas 	}
327232f63fa7SMarcin Wojtas 
3273e2735b09SMarcin Wojtas 	/*
3274e2735b09SMarcin Wojtas 	 * Effective value of used MSIX vectors should be the same as before
3275e2735b09SMarcin Wojtas 	 * `ena_destroy_device()`, if possible, or closest to it if less vectors
3276e2735b09SMarcin Wojtas 	 * are available.
3277e2735b09SMarcin Wojtas 	 */
3278e2735b09SMarcin Wojtas 	if ((adapter->msix_vecs - ENA_ADMIN_MSIX_VEC) < adapter->num_io_queues)
3279e2735b09SMarcin Wojtas 		adapter->num_io_queues =
3280e2735b09SMarcin Wojtas 		    adapter->msix_vecs - ENA_ADMIN_MSIX_VEC;
3281e2735b09SMarcin Wojtas 
3282e2735b09SMarcin Wojtas 	/* Re-initialize rings basic information */
3283e2735b09SMarcin Wojtas 	ena_init_io_rings(adapter);
3284e2735b09SMarcin Wojtas 
328532f63fa7SMarcin Wojtas 	/* If the interface was up before the reset bring it up */
328632f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
328732f63fa7SMarcin Wojtas 		rc = ena_up(adapter);
328832f63fa7SMarcin Wojtas 		if (rc != 0) {
328932f63fa7SMarcin Wojtas 			device_printf(dev, "Failed to create I/O queues\n");
329032f63fa7SMarcin Wojtas 			goto err_disable_msix;
329132f63fa7SMarcin Wojtas 		}
329232f63fa7SMarcin Wojtas 	}
329332f63fa7SMarcin Wojtas 
329424392281SMarcin Wojtas 	/* Indicate that device is running again and ready to work */
329532f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
329624392281SMarcin Wojtas 
329724392281SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
329824392281SMarcin Wojtas 		/*
329924392281SMarcin Wojtas 		 * As the AENQ handlers weren't executed during reset because
330024392281SMarcin Wojtas 		 * the flag ENA_FLAG_DEVICE_RUNNING was turned off, the
330124392281SMarcin Wojtas 		 * timestamp must be updated again That will prevent next reset
330224392281SMarcin Wojtas 		 * caused by missing keep alive.
330324392281SMarcin Wojtas 		 */
330424392281SMarcin Wojtas 		adapter->keep_alive_timestamp = getsbinuptime();
330532f63fa7SMarcin Wojtas 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
330632f63fa7SMarcin Wojtas 		    ena_timer_service, (void *)adapter, 0);
330724392281SMarcin Wojtas 	}
33087d8c4feeSMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
330932f63fa7SMarcin Wojtas 
331032f63fa7SMarcin Wojtas 	device_printf(dev,
331132f63fa7SMarcin Wojtas 	    "Device reset completed successfully, Driver info: %s\n", ena_version);
331232f63fa7SMarcin Wojtas 
331332f63fa7SMarcin Wojtas 	return (rc);
331432f63fa7SMarcin Wojtas 
331532f63fa7SMarcin Wojtas err_disable_msix:
331632f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
331732f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
331832f63fa7SMarcin Wojtas err_device_destroy:
331932f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
332032f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
332132f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
332232f63fa7SMarcin Wojtas 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
332332f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
332432f63fa7SMarcin Wojtas err:
332532f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
332632f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
332732f63fa7SMarcin Wojtas 	device_printf(dev, "Reset attempt failed. Can not reset the device\n");
332832f63fa7SMarcin Wojtas 
332932f63fa7SMarcin Wojtas 	return (rc);
333032f63fa7SMarcin Wojtas }
333132f63fa7SMarcin Wojtas 
333232f63fa7SMarcin Wojtas static void
333332f63fa7SMarcin Wojtas ena_reset_task(void *arg, int pending)
333432f63fa7SMarcin Wojtas {
333532f63fa7SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
333632f63fa7SMarcin Wojtas 
3337fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
33389b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33399b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
33409b8d05b8SZbigniew Bodek 		return;
33419b8d05b8SZbigniew Bodek 	}
33429b8d05b8SZbigniew Bodek 
33436959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
334432f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, false);
334532f63fa7SMarcin Wojtas 	ena_restore_device(adapter);
33466959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
33479b8d05b8SZbigniew Bodek }
33489b8d05b8SZbigniew Bodek 
33499b8d05b8SZbigniew Bodek /**
33509b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
33519b8d05b8SZbigniew Bodek  * @pdev: device information struct
33529b8d05b8SZbigniew Bodek  *
33539b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
33549b8d05b8SZbigniew Bodek  *
33559b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
33569b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
33579b8d05b8SZbigniew Bodek  * and a hardware reset occur.
33589b8d05b8SZbigniew Bodek  **/
33599b8d05b8SZbigniew Bodek static int
33609b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
33619b8d05b8SZbigniew Bodek {
33629b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
33634fa9e02dSMarcin Wojtas 	struct ena_llq_configurations llq_config;
33646064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
33659b8d05b8SZbigniew Bodek 	static int version_printed;
33669b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
33679b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
33687d8c4feeSMarcin Wojtas 	uint32_t max_num_io_queues;
33694fa9e02dSMarcin Wojtas 	int rid, rc;
33704fa9e02dSMarcin Wojtas 
33719b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
33729b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
33739b8d05b8SZbigniew Bodek 
33746959869eSMarcin Wojtas 	ENA_LOCK_INIT(adapter);
33759b8d05b8SZbigniew Bodek 
33766959869eSMarcin Wojtas 	/*
33776959869eSMarcin Wojtas 	 * Set up the timer service - driver is responsible for avoiding
33786959869eSMarcin Wojtas 	 * concurrency, as the callout won't be using any locking inside.
33796959869eSMarcin Wojtas 	 */
33806959869eSMarcin Wojtas 	callout_init(&adapter->timer_service, true);
33819b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
33829b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
33839b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
33849b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
33859b8d05b8SZbigniew Bodek 
33869b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
33879b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
33889b8d05b8SZbigniew Bodek 
33899b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3390cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3391cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
33929b8d05b8SZbigniew Bodek 
33939b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
33949b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
33954fa9e02dSMarcin Wojtas 
33964fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_REG_BAR);
33974fa9e02dSMarcin Wojtas 	adapter->memory = NULL;
33984fa9e02dSMarcin Wojtas 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
33994fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
34004fa9e02dSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
34014fa9e02dSMarcin Wojtas 		device_printf(pdev,
34024fa9e02dSMarcin Wojtas 		    "unable to allocate bus resource: registers!\n");
34034fa9e02dSMarcin Wojtas 		rc = ENOMEM;
34044fa9e02dSMarcin Wojtas 		goto err_dev_free;
34054fa9e02dSMarcin Wojtas 	}
34064fa9e02dSMarcin Wojtas 
34079b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
34089b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
34099b8d05b8SZbigniew Bodek 
34109b8d05b8SZbigniew Bodek 	/* Store register resources */
34119b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
34129b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
34139b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
34149b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
34159b8d05b8SZbigniew Bodek 
34163f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
34179b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
34189b8d05b8SZbigniew Bodek 		rc = ENXIO;
3419cd5d5804SMarcin Wojtas 		goto err_bus_free;
34209b8d05b8SZbigniew Bodek 	}
34219b8d05b8SZbigniew Bodek 
34229b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
34239b8d05b8SZbigniew Bodek 
3424fd43fd2aSMarcin Wojtas 	/* Initially clear all the flags */
3425fd43fd2aSMarcin Wojtas 	ENA_FLAG_ZERO(adapter);
3426fd43fd2aSMarcin Wojtas 
34279b8d05b8SZbigniew Bodek 	/* Device initialization */
34289b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
34293f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
34309b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
34319b8d05b8SZbigniew Bodek 		rc = ENXIO;
34329b8d05b8SZbigniew Bodek 		goto err_bus_free;
34339b8d05b8SZbigniew Bodek 	}
34349b8d05b8SZbigniew Bodek 
34354fa9e02dSMarcin Wojtas 	set_default_llq_configurations(&llq_config);
34364fa9e02dSMarcin Wojtas 
34374fa9e02dSMarcin Wojtas 	rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx.llq,
34384fa9e02dSMarcin Wojtas 	     &llq_config);
34394fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
34404fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to set placement policy\n");
34414fa9e02dSMarcin Wojtas 		goto err_com_free;
34424fa9e02dSMarcin Wojtas 	}
34434fa9e02dSMarcin Wojtas 
34449b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
34459b8d05b8SZbigniew Bodek 
34469b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
34479b8d05b8SZbigniew Bodek 
34489b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
34499b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
34509b8d05b8SZbigniew Bodek 
34517d8c4feeSMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
34526064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
34536064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
34546064f289SMarcin Wojtas 
34557d8c4feeSMarcin Wojtas 	/* Calculate initial and maximum IO queue number and size */
34567d8c4feeSMarcin Wojtas 	max_num_io_queues = ena_calc_max_io_queue_num(pdev, ena_dev,
34577d8c4feeSMarcin Wojtas 	    &get_feat_ctx);
34587d8c4feeSMarcin Wojtas 	rc = ena_calc_io_queue_size(&calc_queue_ctx);
34597d8c4feeSMarcin Wojtas 	if (unlikely((rc != 0) || (max_num_io_queues <= 0))) {
34606064f289SMarcin Wojtas 		rc = EFAULT;
34619b8d05b8SZbigniew Bodek 		goto err_com_free;
34629b8d05b8SZbigniew Bodek 	}
34639b8d05b8SZbigniew Bodek 
34646064f289SMarcin Wojtas 	adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
34656064f289SMarcin Wojtas 	adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
34667d8c4feeSMarcin Wojtas 	adapter->max_tx_ring_size = calc_queue_ctx.max_tx_queue_size;
34677d8c4feeSMarcin Wojtas 	adapter->max_rx_ring_size = calc_queue_ctx.max_rx_queue_size;
34686064f289SMarcin Wojtas 	adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
34696064f289SMarcin Wojtas 	adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
34706064f289SMarcin Wojtas 
34717d8c4feeSMarcin Wojtas 	adapter->max_num_io_queues = max_num_io_queues;
34727d8c4feeSMarcin Wojtas 
34736064f289SMarcin Wojtas 	adapter->buf_ring_size = ENA_DEFAULT_BUF_RING_SIZE;
34749b8d05b8SZbigniew Bodek 
34757d8c4feeSMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
34767d8c4feeSMarcin Wojtas 
34777d8c4feeSMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
34787d8c4feeSMarcin Wojtas 
34799b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
34809b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
34814e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
34824e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
3483cd5d5804SMarcin Wojtas 		goto err_com_free;
34844e8acd84SMarcin Wojtas 	}
34859b8d05b8SZbigniew Bodek 
34869b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
34874e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
34884e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
3489cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
34904e8acd84SMarcin Wojtas 	}
34919b8d05b8SZbigniew Bodek 
3492e2735b09SMarcin Wojtas 	/*
3493e2735b09SMarcin Wojtas 	 * The amount of requested MSIX vectors is equal to
3494e2735b09SMarcin Wojtas 	 * adapter::max_num_io_queues (see `ena_enable_msix()`), plus a constant
3495e2735b09SMarcin Wojtas 	 * number of admin queue interrupts. The former is initially determined
3496e2735b09SMarcin Wojtas 	 * by HW capabilities (see `ena_calc_max_io_queue_num())` but may not be
3497e2735b09SMarcin Wojtas 	 * achieved if there are not enough system resources. By default, the
3498e2735b09SMarcin Wojtas 	 * number of effectively used IO queues is the same but later on it can
3499e2735b09SMarcin Wojtas 	 * be limited by the user using sysctl interface.
3500e2735b09SMarcin Wojtas 	 */
3501aa9c3226SMarcin Wojtas 	rc = ena_enable_msix_and_set_admin_interrupts(adapter);
35023f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
35039b8d05b8SZbigniew Bodek 		device_printf(pdev,
35049b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
3505c115a1e2SMarcin Wojtas 		goto err_io_free;
3506c115a1e2SMarcin Wojtas 	}
3507e2735b09SMarcin Wojtas 	/* By default all of allocated MSIX vectors are actively used */
3508e2735b09SMarcin Wojtas 	adapter->num_io_queues = adapter->msix_vecs - ENA_ADMIN_MSIX_VEC;
3509e2735b09SMarcin Wojtas 
3510e2735b09SMarcin Wojtas 	/* initialize rings basic information */
3511e2735b09SMarcin Wojtas 	ena_init_io_rings(adapter);
3512c115a1e2SMarcin Wojtas 
3513c115a1e2SMarcin Wojtas 	/* setup network interface */
3514c115a1e2SMarcin Wojtas 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
3515c115a1e2SMarcin Wojtas 	if (unlikely(rc != 0)) {
3516c115a1e2SMarcin Wojtas 		device_printf(pdev, "Error with network interface setup\n");
3517c115a1e2SMarcin Wojtas 		goto err_msix_free;
35189b8d05b8SZbigniew Bodek 	}
35199b8d05b8SZbigniew Bodek 
3520081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3521081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3522081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3523081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3524081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3525081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3526081169f2SZbigniew Bodek 
35279b8d05b8SZbigniew Bodek 	/* Initialize statistics */
35289b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
35299b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
353030217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
353130217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
35329b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
35339b8d05b8SZbigniew Bodek 
3534d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3535d17b7d87SMarcin Wojtas 	rc = ena_netmap_attach(adapter);
3536d17b7d87SMarcin Wojtas 	if (rc != 0) {
3537d17b7d87SMarcin Wojtas 		device_printf(pdev, "netmap attach failed: %d\n", rc);
3538d17b7d87SMarcin Wojtas 		goto err_detach;
3539d17b7d87SMarcin Wojtas 	}
3540d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3541d17b7d87SMarcin Wojtas 
35429b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
35439b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
3544fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
35459b8d05b8SZbigniew Bodek 
35469b8d05b8SZbigniew Bodek 	return (0);
35479b8d05b8SZbigniew Bodek 
3548d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3549d17b7d87SMarcin Wojtas err_detach:
3550d17b7d87SMarcin Wojtas 	ether_ifdetach(adapter->ifp);
3551d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3552c115a1e2SMarcin Wojtas err_msix_free:
3553c115a1e2SMarcin Wojtas 	ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR);
3554c115a1e2SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
3555c115a1e2SMarcin Wojtas 	ena_disable_msix(adapter);
3556cd5d5804SMarcin Wojtas err_io_free:
35579b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
35589b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3559cd5d5804SMarcin Wojtas err_tx_tag_free:
35609b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3561cd5d5804SMarcin Wojtas err_com_free:
35629b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
35639b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
3564cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
35659b8d05b8SZbigniew Bodek err_bus_free:
35669b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
35679b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
35684fa9e02dSMarcin Wojtas err_dev_free:
35694fa9e02dSMarcin Wojtas 	free(ena_dev, M_DEVBUF);
3570cd5d5804SMarcin Wojtas 
35719b8d05b8SZbigniew Bodek 	return (rc);
35729b8d05b8SZbigniew Bodek }
35739b8d05b8SZbigniew Bodek 
35749b8d05b8SZbigniew Bodek /**
35759b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
35769b8d05b8SZbigniew Bodek  * @pdev: device information struct
35779b8d05b8SZbigniew Bodek  *
35789b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
35799b8d05b8SZbigniew Bodek  * that it should release a PCI device.
35809b8d05b8SZbigniew Bodek  **/
35819b8d05b8SZbigniew Bodek static int
35829b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
35839b8d05b8SZbigniew Bodek {
35849b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
35859b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
35869b8d05b8SZbigniew Bodek 	int rc;
35879b8d05b8SZbigniew Bodek 
35889b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
35899b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
35909b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
35919b8d05b8SZbigniew Bodek 		return (EBUSY);
35929b8d05b8SZbigniew Bodek 	}
35939b8d05b8SZbigniew Bodek 
35949151c55dSMarcin Wojtas 	ether_ifdetach(adapter->ifp);
35959151c55dSMarcin Wojtas 
35966959869eSMarcin Wojtas 	/* Stop timer service */
35976959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
35989b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
35996959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
36006959869eSMarcin Wojtas 
36016959869eSMarcin Wojtas 	/* Release reset task */
36029b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
36039b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
36049b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
36059b8d05b8SZbigniew Bodek 
36066959869eSMarcin Wojtas 	ENA_LOCK_LOCK(adapter);
36079b8d05b8SZbigniew Bodek 	ena_down(adapter);
360832f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, true);
36096959869eSMarcin Wojtas 	ENA_LOCK_UNLOCK(adapter);
36109b8d05b8SZbigniew Bodek 
3611d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3612d17b7d87SMarcin Wojtas 	netmap_detach(adapter->ifp);
3613d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
3614d17b7d87SMarcin Wojtas 
36159b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
36169b8d05b8SZbigniew Bodek 
361730217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
361830217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
36199b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
36209b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
36219b8d05b8SZbigniew Bodek 
36229b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
36233f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
36249b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36259b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
36269b8d05b8SZbigniew Bodek 
36279b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
36283f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
36299b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36309b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
36319b8d05b8SZbigniew Bodek 
36329b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
36339b8d05b8SZbigniew Bodek 
36349b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
36359b8d05b8SZbigniew Bodek 
363632f63fa7SMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter)))
363732f63fa7SMarcin Wojtas 		ena_com_rss_destroy(ena_dev);
363832f63fa7SMarcin Wojtas 
363932f63fa7SMarcin Wojtas 	ena_com_delete_host_info(ena_dev);
364032f63fa7SMarcin Wojtas 
36416959869eSMarcin Wojtas 	ENA_LOCK_DESTROY(adapter);
36429b8d05b8SZbigniew Bodek 
36439151c55dSMarcin Wojtas 	if_free(adapter->ifp);
36449151c55dSMarcin Wojtas 
36459b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
36469b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
36479b8d05b8SZbigniew Bodek 
36489b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
36499b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
36509b8d05b8SZbigniew Bodek 
36519b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
36529b8d05b8SZbigniew Bodek }
36539b8d05b8SZbigniew Bodek 
36549b8d05b8SZbigniew Bodek /******************************************************************************
36559b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
36569b8d05b8SZbigniew Bodek  *****************************************************************************/
36579b8d05b8SZbigniew Bodek /**
36589b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
36599b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
36609b8d05b8SZbigniew Bodek  **/
36619b8d05b8SZbigniew Bodek static void
36629b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
36639b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
36649b8d05b8SZbigniew Bodek {
36659b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
36669b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
36679b8d05b8SZbigniew Bodek 	int status;
36689b8d05b8SZbigniew Bodek 	if_t ifp;
36699b8d05b8SZbigniew Bodek 
36709b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
36719b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
36729b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
36739b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
36749b8d05b8SZbigniew Bodek 
36759b8d05b8SZbigniew Bodek 	if (status != 0) {
36769b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
3677fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_LINK_UP, adapter);
367832f63fa7SMarcin Wojtas 		if (!ENA_FLAG_ISSET(ENA_FLAG_ONGOING_RESET, adapter))
367932f63fa7SMarcin Wojtas 			if_link_state_change(ifp, LINK_STATE_UP);
368032f63fa7SMarcin Wojtas 	} else {
36819b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
36829b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
3683fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_LINK_UP, adapter);
36849b8d05b8SZbigniew Bodek 	}
36859b8d05b8SZbigniew Bodek }
36869b8d05b8SZbigniew Bodek 
368740621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
368840621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
368940621d71SMarcin Wojtas {
369040621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
369140621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
369240621d71SMarcin Wojtas 
369340621d71SMarcin Wojtas 	ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
369440621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
369540621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
369640621d71SMarcin Wojtas 
369740621d71SMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrom) {
369840621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
369940621d71SMarcin Wojtas 		hints =
370040621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
370140621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
370240621d71SMarcin Wojtas 		break;
370340621d71SMarcin Wojtas 	default:
370440621d71SMarcin Wojtas 		device_printf(adapter->pdev,
370540621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
370640621d71SMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrom);
370740621d71SMarcin Wojtas 	}
370840621d71SMarcin Wojtas }
370940621d71SMarcin Wojtas 
37109b8d05b8SZbigniew Bodek /**
37119b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
37129b8d05b8SZbigniew Bodek  **/
37139b8d05b8SZbigniew Bodek static void
3714e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
37159b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
37169b8d05b8SZbigniew Bodek {
3717e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
3718e6de9a83SMarcin Wojtas 
3719e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
3720e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
37219b8d05b8SZbigniew Bodek }
37229b8d05b8SZbigniew Bodek 
37239b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
37249b8d05b8SZbigniew Bodek     .handlers = {
37259b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
372640621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
37279b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
37289b8d05b8SZbigniew Bodek     },
37299b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
37309b8d05b8SZbigniew Bodek };
37319b8d05b8SZbigniew Bodek 
37329b8d05b8SZbigniew Bodek /*********************************************************************
37339b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
37349b8d05b8SZbigniew Bodek  *********************************************************************/
37359b8d05b8SZbigniew Bodek 
37369b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
37379b8d05b8SZbigniew Bodek     /* Device interface */
37389b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
37399b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
37409b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
37419b8d05b8SZbigniew Bodek     DEVMETHOD_END
37429b8d05b8SZbigniew Bodek };
37439b8d05b8SZbigniew Bodek 
37449b8d05b8SZbigniew Bodek static driver_t ena_driver = {
37459b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
37469b8d05b8SZbigniew Bodek };
37479b8d05b8SZbigniew Bodek 
37489b8d05b8SZbigniew Bodek devclass_t ena_devclass;
37499b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
375040abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
3751329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
37529b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
37539b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
3754d17b7d87SMarcin Wojtas #ifdef DEV_NETMAP
3755d17b7d87SMarcin Wojtas MODULE_DEPEND(ena, netmap, 1, 1, 1);
3756d17b7d87SMarcin Wojtas #endif /* DEV_NETMAP */
37579b8d05b8SZbigniew Bodek 
37589b8d05b8SZbigniew Bodek /*********************************************************************/
3759