xref: /freebsd/sys/dev/ena/ena.c (revision 472d4784a87272264078f53af87bda4e2c8b7c8b)
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 
799b8d05b8SZbigniew Bodek #include "ena.h"
809b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
819b8d05b8SZbigniew Bodek 
829b8d05b8SZbigniew Bodek /*********************************************************
839b8d05b8SZbigniew Bodek  *  Function prototypes
849b8d05b8SZbigniew Bodek  *********************************************************/
859b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
869b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
879b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
889b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
899b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
909b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
919b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
929b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
939b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
94cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
959b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
969b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
979b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
989b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
999b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
1009b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
1019b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1029b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1039b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1049b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
10543fefd16SMarcin Wojtas static inline int validate_rx_req_id(struct ena_ring *, uint16_t);
1069b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1079b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1089b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1099b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1109b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1119b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1129b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1139b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1149b8d05b8SZbigniew Bodek static int	ena_refill_rx_bufs(struct ena_ring *, uint32_t);
1159b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1169b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1179b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1189b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1199b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1209b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1219b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1229b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1239b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1249b8d05b8SZbigniew Bodek static int	ena_tx_cleanup(struct ena_ring *);
1259b8d05b8SZbigniew Bodek static int	ena_rx_cleanup(struct ena_ring *);
126fceb9387SMarcin Wojtas static inline int validate_tx_req_id(struct ena_ring *, uint16_t);
1279b8d05b8SZbigniew Bodek static void	ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *,
1289b8d05b8SZbigniew Bodek     struct mbuf *);
1299b8d05b8SZbigniew Bodek static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *,
1309b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *, uint16_t *);
1319b8d05b8SZbigniew Bodek static inline void ena_rx_checksum(struct ena_ring *, struct ena_com_rx_ctx *,
1329b8d05b8SZbigniew Bodek     struct mbuf *);
1335cb9db07SMarcin Wojtas static void	ena_cleanup(void *arg, int pending);
1345cb9db07SMarcin Wojtas static int	ena_handle_msix(void *);
1359b8d05b8SZbigniew Bodek static int	ena_enable_msix(struct ena_adapter *);
1369b8d05b8SZbigniew Bodek static void	ena_setup_mgmnt_intr(struct ena_adapter *);
13777958fcdSMarcin Wojtas static int	ena_setup_io_intr(struct ena_adapter *);
1389b8d05b8SZbigniew Bodek static int	ena_request_mgmnt_irq(struct ena_adapter *);
1399b8d05b8SZbigniew Bodek static int	ena_request_io_irq(struct ena_adapter *);
1409b8d05b8SZbigniew Bodek static void	ena_free_mgmnt_irq(struct ena_adapter *);
1419b8d05b8SZbigniew Bodek static void	ena_free_io_irq(struct ena_adapter *);
1429b8d05b8SZbigniew Bodek static void	ena_free_irqs(struct ena_adapter*);
1439b8d05b8SZbigniew Bodek static void	ena_disable_msix(struct ena_adapter *);
1449b8d05b8SZbigniew Bodek static void	ena_unmask_all_io_irqs(struct ena_adapter *);
1459b8d05b8SZbigniew Bodek static int	ena_rss_configure(struct ena_adapter *);
1469b8d05b8SZbigniew Bodek static int	ena_up_complete(struct ena_adapter *);
1479b8d05b8SZbigniew Bodek static int	ena_up(struct ena_adapter *);
1489b8d05b8SZbigniew Bodek static void	ena_down(struct ena_adapter *);
1499b8d05b8SZbigniew Bodek static uint64_t	ena_get_counter(if_t, ift_counter);
1509b8d05b8SZbigniew Bodek static int	ena_media_change(if_t);
1519b8d05b8SZbigniew Bodek static void	ena_media_status(if_t, struct ifmediareq *);
1529b8d05b8SZbigniew Bodek static void	ena_init(void *);
1539b8d05b8SZbigniew Bodek static int	ena_ioctl(if_t, u_long, caddr_t);
1549b8d05b8SZbigniew Bodek static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
1559b8d05b8SZbigniew Bodek static void	ena_update_host_info(struct ena_admin_host_info *, if_t);
1569b8d05b8SZbigniew Bodek static void	ena_update_hwassist(struct ena_adapter *);
1579b8d05b8SZbigniew Bodek static int	ena_setup_ifnet(device_t, struct ena_adapter *,
1589b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1599b8d05b8SZbigniew Bodek static void	ena_tx_csum(struct ena_com_tx_ctx *, struct mbuf *);
1601b069f1cSZbigniew Bodek static int	ena_check_and_collapse_mbuf(struct ena_ring *tx_ring,
1611b069f1cSZbigniew Bodek     struct mbuf **mbuf);
1624fa9e02dSMarcin Wojtas static void	ena_dmamap_llq(void *, bus_dma_segment_t *, int, int);
1631e9fb899SZbigniew Bodek static int	ena_xmit_mbuf(struct ena_ring *, struct mbuf **);
1649b8d05b8SZbigniew Bodek static void	ena_start_xmit(struct ena_ring *);
1659b8d05b8SZbigniew Bodek static int	ena_mq_start(if_t, struct mbuf *);
1669b8d05b8SZbigniew Bodek static void	ena_deferred_mq_start(void *, int);
1679b8d05b8SZbigniew Bodek static void	ena_qflush(if_t);
1684fa9e02dSMarcin Wojtas static int	ena_enable_wc(struct resource *);
1694fa9e02dSMarcin Wojtas static int	ena_set_queues_placement_policy(device_t, struct ena_com_dev *,
1704fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *, struct ena_llq_configurations *);
1719b8d05b8SZbigniew Bodek static int	ena_calc_io_queue_num(struct ena_adapter *,
1729b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1736064f289SMarcin Wojtas static int	ena_calc_queue_size(struct ena_adapter *,
1746064f289SMarcin Wojtas     struct ena_calc_queue_size_ctx *);
1756064f289SMarcin Wojtas static int	ena_handle_updated_queues(struct ena_adapter *,
1766064f289SMarcin Wojtas     struct ena_com_dev_get_features_ctx *);
1779b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1789b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
17946021271SMarcin Wojtas static void	ena_config_host_info(struct ena_com_dev *, device_t);
1809b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1819b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1829b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1839b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
1849b8d05b8SZbigniew Bodek static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *,
1859b8d05b8SZbigniew Bodek     int);
1869b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1879b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1889b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1899b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1909b8d05b8SZbigniew Bodek 
1919b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1929b8d05b8SZbigniew Bodek 
1939b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
1949b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
1959b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
1969b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
1979b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
1989b8d05b8SZbigniew Bodek     /* Last entry */
1999b8d05b8SZbigniew Bodek     { 0, 0, 0 }
2009b8d05b8SZbigniew Bodek };
2019b8d05b8SZbigniew Bodek 
2029b8d05b8SZbigniew Bodek /*
2039b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
2049b8d05b8SZbigniew Bodek  */
2059b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
2069b8d05b8SZbigniew Bodek 
2079b8d05b8SZbigniew Bodek void
2089b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2099b8d05b8SZbigniew Bodek {
2100bdffe59SMarcin Wojtas 	if (error != 0)
2119b8d05b8SZbigniew Bodek 		return;
2129b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
2139b8d05b8SZbigniew Bodek }
2149b8d05b8SZbigniew Bodek 
2159b8d05b8SZbigniew Bodek int
2169b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
2179b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
2189b8d05b8SZbigniew Bodek {
2199b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2200bdffe59SMarcin Wojtas 	uint32_t maxsize;
2210bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2229b8d05b8SZbigniew Bodek 	int error;
2239b8d05b8SZbigniew Bodek 
2240bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2250bdffe59SMarcin Wojtas 
2260bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2273f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2289b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2290bdffe59SMarcin Wojtas 
2309b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2319b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2328a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2338a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2349b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2359b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2369b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2379b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2389b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2399b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2409b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2419b8d05b8SZbigniew Bodek 	    &dma->tag);
2423f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2434e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2449b8d05b8SZbigniew Bodek 		goto fail_tag;
2459b8d05b8SZbigniew Bodek 	}
2469b8d05b8SZbigniew Bodek 
2479b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2489b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2493f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2504e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2514e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2529b8d05b8SZbigniew Bodek 		goto fail_map_create;
2539b8d05b8SZbigniew Bodek 	}
2549b8d05b8SZbigniew Bodek 
2559b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2569b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2579b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2583f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
2594e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2609b8d05b8SZbigniew Bodek 		goto fail_map_load;
2619b8d05b8SZbigniew Bodek 	}
2629b8d05b8SZbigniew Bodek 
263e8073738SMarcin Wojtas 	bus_dmamap_sync(dma->tag, dma->map,
264e8073738SMarcin Wojtas 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
265e8073738SMarcin Wojtas 
2669b8d05b8SZbigniew Bodek 	return (0);
2679b8d05b8SZbigniew Bodek 
2689b8d05b8SZbigniew Bodek fail_map_load:
2699b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2707d2544e6SMarcin Wojtas fail_map_create:
2719b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2729b8d05b8SZbigniew Bodek fail_tag:
2739b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2745b14f92eSMarcin Wojtas 	dma->vaddr = NULL;
2755b14f92eSMarcin Wojtas 	dma->paddr = 0;
2769b8d05b8SZbigniew Bodek 
2779b8d05b8SZbigniew Bodek 	return (error);
2789b8d05b8SZbigniew Bodek }
2799b8d05b8SZbigniew Bodek 
2809b8d05b8SZbigniew Bodek static void
2819b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
2829b8d05b8SZbigniew Bodek {
2839b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2849b8d05b8SZbigniew Bodek 
2859b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
2869b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2879b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
2889b8d05b8SZbigniew Bodek 	}
2899b8d05b8SZbigniew Bodek 
2909b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
2919b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
2929b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
2939b8d05b8SZbigniew Bodek 	}
2949b8d05b8SZbigniew Bodek }
2959b8d05b8SZbigniew Bodek 
2969b8d05b8SZbigniew Bodek static int
2979b8d05b8SZbigniew Bodek ena_probe(device_t dev)
2989b8d05b8SZbigniew Bodek {
2999b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3009b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3019b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3029b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3039b8d05b8SZbigniew Bodek 
3049b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3059b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3069b8d05b8SZbigniew Bodek 
3079b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3089b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3099b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3109b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
31130425f93SMarcin Wojtas 			ena_trace(ENA_DBG, "vendor=%x device=%x\n",
3129b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3139b8d05b8SZbigniew Bodek 
3149b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3159b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3169b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3179b8d05b8SZbigniew Bodek 		}
3189b8d05b8SZbigniew Bodek 
3199b8d05b8SZbigniew Bodek 		ent++;
3209b8d05b8SZbigniew Bodek 
3219b8d05b8SZbigniew Bodek 	}
3229b8d05b8SZbigniew Bodek 
3239b8d05b8SZbigniew Bodek 	return (ENXIO);
3249b8d05b8SZbigniew Bodek }
3259b8d05b8SZbigniew Bodek 
3269b8d05b8SZbigniew Bodek static int
3279b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3289b8d05b8SZbigniew Bodek {
3299b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3303cfadb28SMarcin Wojtas 	int rc;
3319b8d05b8SZbigniew Bodek 
3323cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3339b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3343cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3353cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3363cfadb28SMarcin Wojtas 		return (EINVAL);
3379b8d05b8SZbigniew Bodek 	}
3389b8d05b8SZbigniew Bodek 
3399b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3403cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
3413cfadb28SMarcin Wojtas 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
3423cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3433cfadb28SMarcin Wojtas 	} else {
3443cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3453cfadb28SMarcin Wojtas 		    new_mtu);
3463cfadb28SMarcin Wojtas 	}
3479b8d05b8SZbigniew Bodek 
3483cfadb28SMarcin Wojtas 	return (rc);
3499b8d05b8SZbigniew Bodek }
3509b8d05b8SZbigniew Bodek 
3519b8d05b8SZbigniew Bodek static inline void
3529b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3539b8d05b8SZbigniew Bodek {
3549b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3559b8d05b8SZbigniew Bodek 
3569b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3579b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3589b8d05b8SZbigniew Bodek }
3599b8d05b8SZbigniew Bodek 
3609b8d05b8SZbigniew Bodek static inline void
3619b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3629b8d05b8SZbigniew Bodek {
3639b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3649b8d05b8SZbigniew Bodek 
3659b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3669b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3679b8d05b8SZbigniew Bodek }
3689b8d05b8SZbigniew Bodek 
3699b8d05b8SZbigniew Bodek static inline void
3709b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3719b8d05b8SZbigniew Bodek {
3729b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3739b8d05b8SZbigniew Bodek 
3749b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3759b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
3769b8d05b8SZbigniew Bodek }
3779b8d05b8SZbigniew Bodek 
3789b8d05b8SZbigniew Bodek static void
3799b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
3809b8d05b8SZbigniew Bodek     uint16_t qid)
3819b8d05b8SZbigniew Bodek {
3829b8d05b8SZbigniew Bodek 
3839b8d05b8SZbigniew Bodek 	ring->qid = qid;
3849b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
3859b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
386d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
387d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
3889b8d05b8SZbigniew Bodek }
3899b8d05b8SZbigniew Bodek 
390cd5d5804SMarcin Wojtas static void
3919b8d05b8SZbigniew Bodek ena_init_io_rings(struct ena_adapter *adapter)
3929b8d05b8SZbigniew Bodek {
3939b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
3949b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
3959b8d05b8SZbigniew Bodek 	struct ena_que *que;
3969b8d05b8SZbigniew Bodek 	int i;
3979b8d05b8SZbigniew Bodek 
3989b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
3999b8d05b8SZbigniew Bodek 
4009b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
4019b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4029b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4039b8d05b8SZbigniew Bodek 
4049b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4059b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4069b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4079b8d05b8SZbigniew Bodek 
4089b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4099b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
4109b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4119b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4129b8d05b8SZbigniew Bodek 		txr->smoothed_interval =
4139b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
4149b8d05b8SZbigniew Bodek 
4159b8d05b8SZbigniew Bodek 		/* Allocate a buf ring */
4166064f289SMarcin Wojtas 		txr->buf_ring_size = adapter->buf_ring_size;
4176064f289SMarcin Wojtas 		txr->br = buf_ring_alloc(txr->buf_ring_size, M_DEVBUF,
4189b8d05b8SZbigniew Bodek 		    M_WAITOK, &txr->ring_mtx);
4199b8d05b8SZbigniew Bodek 
4209b8d05b8SZbigniew Bodek 		/* Alloc TX statistics. */
4219b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4229b8d05b8SZbigniew Bodek 		    sizeof(txr->tx_stats));
4239b8d05b8SZbigniew Bodek 
4249b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4259b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4269b8d05b8SZbigniew Bodek 		rxr->smoothed_interval =
4279b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
4289b8d05b8SZbigniew Bodek 
4299b8d05b8SZbigniew Bodek 		/* Alloc RX statistics. */
4309b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4319b8d05b8SZbigniew Bodek 		    sizeof(rxr->rx_stats));
4329b8d05b8SZbigniew Bodek 
4339b8d05b8SZbigniew Bodek 		/* Initialize locks */
4349b8d05b8SZbigniew Bodek 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4359b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4369b8d05b8SZbigniew Bodek 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4379b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4389b8d05b8SZbigniew Bodek 
4399b8d05b8SZbigniew Bodek 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
4409b8d05b8SZbigniew Bodek 
4419b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4429b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4439b8d05b8SZbigniew Bodek 		que->id = i;
4449b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4459b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4469b8d05b8SZbigniew Bodek 
4479b8d05b8SZbigniew Bodek 		txr->que = que;
4489b8d05b8SZbigniew Bodek 		rxr->que = que;
449efe6ab18SMarcin Wojtas 
450efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4519b8d05b8SZbigniew Bodek 	}
4529b8d05b8SZbigniew Bodek }
4539b8d05b8SZbigniew Bodek 
4549b8d05b8SZbigniew Bodek static void
4559b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4569b8d05b8SZbigniew Bodek {
4579b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4589b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4599b8d05b8SZbigniew Bodek 
4609b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4619b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4629b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4639b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4649b8d05b8SZbigniew Bodek 
4657d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4667d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4677d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4687d2544e6SMarcin Wojtas 
4699b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
4709b8d05b8SZbigniew Bodek }
4719b8d05b8SZbigniew Bodek 
4729b8d05b8SZbigniew Bodek static void
4739b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
4749b8d05b8SZbigniew Bodek {
4759b8d05b8SZbigniew Bodek 	int i;
4769b8d05b8SZbigniew Bodek 
4779b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
4789b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
4799b8d05b8SZbigniew Bodek 
4809b8d05b8SZbigniew Bodek }
4819b8d05b8SZbigniew Bodek 
4829b8d05b8SZbigniew Bodek static int
4839b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
4849b8d05b8SZbigniew Bodek {
4859b8d05b8SZbigniew Bodek 	int ret;
4869b8d05b8SZbigniew Bodek 
4879b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
4889b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
4899b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
4908a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
4918a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
4929b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
4939b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
4948a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
4959b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
4969b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
4979b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
4989b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
4999b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5009b8d05b8SZbigniew Bodek 
5019b8d05b8SZbigniew Bodek 	return (ret);
5029b8d05b8SZbigniew Bodek }
5039b8d05b8SZbigniew Bodek 
5049b8d05b8SZbigniew Bodek static int
5059b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5069b8d05b8SZbigniew Bodek {
5079b8d05b8SZbigniew Bodek 	int ret;
5089b8d05b8SZbigniew Bodek 
5099b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5109b8d05b8SZbigniew Bodek 
5113f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5129b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5139b8d05b8SZbigniew Bodek 
5149b8d05b8SZbigniew Bodek 	return (ret);
5159b8d05b8SZbigniew Bodek }
5169b8d05b8SZbigniew Bodek 
5179b8d05b8SZbigniew Bodek static int
5189b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5199b8d05b8SZbigniew Bodek {
5209b8d05b8SZbigniew Bodek 	int ret;
5219b8d05b8SZbigniew Bodek 
5229b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5239b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5249b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5258a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5268a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5279b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5289b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsize 		     */
5294727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
5309b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsegsize 	     */
5319b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5329b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5339b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5349b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5359b8d05b8SZbigniew Bodek 
5369b8d05b8SZbigniew Bodek 	return (ret);
5379b8d05b8SZbigniew Bodek }
5389b8d05b8SZbigniew Bodek 
5399b8d05b8SZbigniew Bodek static int
5409b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5419b8d05b8SZbigniew Bodek {
5429b8d05b8SZbigniew Bodek 	int ret;
5439b8d05b8SZbigniew Bodek 
5449b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5459b8d05b8SZbigniew Bodek 
5463f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5479b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5489b8d05b8SZbigniew Bodek 
5499b8d05b8SZbigniew Bodek 	return (ret);
5509b8d05b8SZbigniew Bodek }
5519b8d05b8SZbigniew Bodek 
5529b8d05b8SZbigniew Bodek /**
5539b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
5549b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
5559b8d05b8SZbigniew Bodek  * @qid: queue index
5569b8d05b8SZbigniew Bodek  *
5579b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
5589b8d05b8SZbigniew Bodek  **/
5599b8d05b8SZbigniew Bodek static int
5609b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
5619b8d05b8SZbigniew Bodek {
5629b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
5639b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
5649b8d05b8SZbigniew Bodek 	int size, i, err;
5659b8d05b8SZbigniew Bodek 
5669b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
5679b8d05b8SZbigniew Bodek 
5689b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5693f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
5707d2544e6SMarcin Wojtas 		return (ENOMEM);
5719b8d05b8SZbigniew Bodek 
5729b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
5739b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5743f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
5757d2544e6SMarcin Wojtas 		goto err_buf_info_free;
5769b8d05b8SZbigniew Bodek 
5774fa9e02dSMarcin Wojtas 	size = tx_ring->tx_max_header_size;
5784fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = malloc(size, M_DEVBUF,
5794fa9e02dSMarcin Wojtas 	    M_NOWAIT | M_ZERO);
5804fa9e02dSMarcin Wojtas 	if (unlikely(tx_ring->push_buf_intermediate_buf == NULL))
5814fa9e02dSMarcin Wojtas 		goto err_tx_ids_free;
5824fa9e02dSMarcin Wojtas 
5839b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
5849b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
5859b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
5869b8d05b8SZbigniew Bodek 
5879b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
5889b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
5899b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
5909b8d05b8SZbigniew Bodek 
5919b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
5929b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
593af66d7d0SMarcin Wojtas 	tx_ring->acum_pkts = 0;
5949b8d05b8SZbigniew Bodek 
5959b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
596b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
5979b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
598b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
5999b8d05b8SZbigniew Bodek 
6009b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6019b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6029b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6034fa9e02dSMarcin Wojtas 		    &tx_ring->tx_buffer_info[i].map_head);
6043f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
6054e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
6064fa9e02dSMarcin Wojtas 			    "Unable to create Tx DMA map_head for buffer %d\n",
6074fa9e02dSMarcin Wojtas 			    i);
6087d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
6099b8d05b8SZbigniew Bodek 		}
6104fa9e02dSMarcin Wojtas 		tx_ring->tx_buffer_info[i].seg_mapped = false;
6114fa9e02dSMarcin Wojtas 
6124fa9e02dSMarcin Wojtas 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6134fa9e02dSMarcin Wojtas 		    &tx_ring->tx_buffer_info[i].map_seg);
6144fa9e02dSMarcin Wojtas 		if (unlikely(err != 0)) {
6154fa9e02dSMarcin Wojtas 			ena_trace(ENA_ALERT,
6164fa9e02dSMarcin Wojtas 			    "Unable to create Tx DMA map_seg for buffer %d\n",
6174fa9e02dSMarcin Wojtas 			    i);
6184fa9e02dSMarcin Wojtas 			goto err_buf_info_head_unmap;
6194fa9e02dSMarcin Wojtas 		}
6204fa9e02dSMarcin Wojtas 		tx_ring->tx_buffer_info[i].head_mapped = false;
6219b8d05b8SZbigniew Bodek 	}
6229b8d05b8SZbigniew Bodek 
6239b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6249b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6259b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6269b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6273f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
6284e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT,
6299b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6309b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6317d2544e6SMarcin Wojtas 		goto err_buf_info_unmap;
6329b8d05b8SZbigniew Bodek 	}
6339b8d05b8SZbigniew Bodek 
6345cb9db07SMarcin Wojtas 	tx_ring->running = true;
6355cb9db07SMarcin Wojtas 
6369b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
6379b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
6389b8d05b8SZbigniew Bodek 
6399b8d05b8SZbigniew Bodek 	return (0);
6409b8d05b8SZbigniew Bodek 
6414fa9e02dSMarcin Wojtas err_buf_info_head_unmap:
6424fa9e02dSMarcin Wojtas 	bus_dmamap_destroy(adapter->tx_buf_tag,
6434fa9e02dSMarcin Wojtas 	    tx_ring->tx_buffer_info[i].map_head);
6447d2544e6SMarcin Wojtas err_buf_info_unmap:
6459b8d05b8SZbigniew Bodek 	while (i--) {
6469b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6474fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_head);
6484fa9e02dSMarcin Wojtas 		bus_dmamap_destroy(adapter->tx_buf_tag,
6494fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_seg);
6509b8d05b8SZbigniew Bodek 	}
6514fa9e02dSMarcin Wojtas 	free(tx_ring->push_buf_intermediate_buf, M_DEVBUF);
6524fa9e02dSMarcin Wojtas err_tx_ids_free:
653cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6547d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
6557d2544e6SMarcin Wojtas err_buf_info_free:
656cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6577d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
6587d2544e6SMarcin Wojtas 
6599b8d05b8SZbigniew Bodek 	return (ENOMEM);
6609b8d05b8SZbigniew Bodek }
6619b8d05b8SZbigniew Bodek 
6629b8d05b8SZbigniew Bodek /**
6639b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
6649b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6659b8d05b8SZbigniew Bodek  * @qid: queue index
6669b8d05b8SZbigniew Bodek  *
6679b8d05b8SZbigniew Bodek  * Free all transmit software resources
6689b8d05b8SZbigniew Bodek  **/
6699b8d05b8SZbigniew Bodek static void
6709b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
6719b8d05b8SZbigniew Bodek {
6729b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
6739b8d05b8SZbigniew Bodek 
6749b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
6759b8d05b8SZbigniew Bodek 	    NULL))
6769b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
6779b8d05b8SZbigniew Bodek 
6789b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
6799b8d05b8SZbigniew Bodek 
680b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6819b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
6829b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
6839b8d05b8SZbigniew Bodek 
6849b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
6859b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
6864fa9e02dSMarcin Wojtas 		if (tx_ring->tx_buffer_info[i].head_mapped == true) {
687e8073738SMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag,
6884fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_head,
6894fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
6909b8d05b8SZbigniew Bodek 			bus_dmamap_unload(adapter->tx_buf_tag,
6914fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_head);
6924fa9e02dSMarcin Wojtas 			tx_ring->tx_buffer_info[i].head_mapped = false;
6934fa9e02dSMarcin Wojtas 		}
6949b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6954fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_head);
6964fa9e02dSMarcin Wojtas 
6974fa9e02dSMarcin Wojtas 		if (tx_ring->tx_buffer_info[i].seg_mapped == true) {
6984fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag,
6994fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_seg,
7004fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
7014fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
7024fa9e02dSMarcin Wojtas 			    tx_ring->tx_buffer_info[i].map_seg);
7034fa9e02dSMarcin Wojtas 			tx_ring->tx_buffer_info[i].seg_mapped = false;
7044fa9e02dSMarcin Wojtas 		}
7054fa9e02dSMarcin Wojtas 		bus_dmamap_destroy(adapter->tx_buf_tag,
7064fa9e02dSMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map_seg);
7074fa9e02dSMarcin Wojtas 
708e8073738SMarcin Wojtas 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
709e8073738SMarcin Wojtas 		tx_ring->tx_buffer_info[i].mbuf = NULL;
7109b8d05b8SZbigniew Bodek 	}
711416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
7129b8d05b8SZbigniew Bodek 
7139b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
714cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
7159b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
7169b8d05b8SZbigniew Bodek 
717cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7189b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
7194fa9e02dSMarcin Wojtas 
7204fa9e02dSMarcin Wojtas 	ENA_MEM_FREE(adapter->ena_dev->dmadev,
7214fa9e02dSMarcin Wojtas 	    tx_ring->push_buf_intermediate_buf);
7224fa9e02dSMarcin Wojtas 	tx_ring->push_buf_intermediate_buf = NULL;
7239b8d05b8SZbigniew Bodek }
7249b8d05b8SZbigniew Bodek 
7259b8d05b8SZbigniew Bodek /**
7269b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7279b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7289b8d05b8SZbigniew Bodek  *
7299b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7309b8d05b8SZbigniew Bodek  **/
7319b8d05b8SZbigniew Bodek static int
7329b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7339b8d05b8SZbigniew Bodek {
7349b8d05b8SZbigniew Bodek 	int i, rc;
7359b8d05b8SZbigniew Bodek 
7369b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
7379b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
7380bdffe59SMarcin Wojtas 		if (rc != 0) {
7399b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
7409b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
7419b8d05b8SZbigniew Bodek 			goto err_setup_tx;
7429b8d05b8SZbigniew Bodek 		}
7437d2544e6SMarcin Wojtas 	}
7449b8d05b8SZbigniew Bodek 
7459b8d05b8SZbigniew Bodek 	return (0);
7469b8d05b8SZbigniew Bodek 
7479b8d05b8SZbigniew Bodek err_setup_tx:
7489b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
7499b8d05b8SZbigniew Bodek 	while (i--)
7509b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7519b8d05b8SZbigniew Bodek 	return (rc);
7529b8d05b8SZbigniew Bodek }
7539b8d05b8SZbigniew Bodek 
7549b8d05b8SZbigniew Bodek /**
7559b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
7569b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7579b8d05b8SZbigniew Bodek  *
7589b8d05b8SZbigniew Bodek  * Free all transmit software resources
7599b8d05b8SZbigniew Bodek  **/
7609b8d05b8SZbigniew Bodek static void
7619b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
7629b8d05b8SZbigniew Bodek {
7639b8d05b8SZbigniew Bodek 	int i;
7649b8d05b8SZbigniew Bodek 
7659b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
7669b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7679b8d05b8SZbigniew Bodek }
7689b8d05b8SZbigniew Bodek 
76943fefd16SMarcin Wojtas static inline int
77043fefd16SMarcin Wojtas validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
77143fefd16SMarcin Wojtas {
77243fefd16SMarcin Wojtas 	if (likely(req_id < rx_ring->ring_size))
77343fefd16SMarcin Wojtas 		return (0);
77443fefd16SMarcin Wojtas 
77543fefd16SMarcin Wojtas 	device_printf(rx_ring->adapter->pdev, "Invalid rx req_id: %hu\n",
77643fefd16SMarcin Wojtas 	    req_id);
77743fefd16SMarcin Wojtas 	counter_u64_add(rx_ring->rx_stats.bad_req_id, 1);
77843fefd16SMarcin Wojtas 
77943fefd16SMarcin Wojtas 	/* Trigger device reset */
780858659f7SMarcin Wojtas 	if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, rx_ring->adapter))) {
78143fefd16SMarcin Wojtas 		rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
782fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, rx_ring->adapter);
783858659f7SMarcin Wojtas 	}
78443fefd16SMarcin Wojtas 
78543fefd16SMarcin Wojtas 	return (EFAULT);
78643fefd16SMarcin Wojtas }
78743fefd16SMarcin Wojtas 
7889b8d05b8SZbigniew Bodek /**
7899b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
7909b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7919b8d05b8SZbigniew Bodek  * @qid: queue index
7929b8d05b8SZbigniew Bodek  *
7939b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7949b8d05b8SZbigniew Bodek  **/
7959b8d05b8SZbigniew Bodek static int
7969b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
7979b8d05b8SZbigniew Bodek {
7989b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
7999b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
8009b8d05b8SZbigniew Bodek 	int size, err, i;
8019b8d05b8SZbigniew Bodek 
8029b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
8039b8d05b8SZbigniew Bodek 
8049b8d05b8SZbigniew Bodek 	/*
8059b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
8069b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
8079b8d05b8SZbigniew Bodek 	 */
8089b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
8099b8d05b8SZbigniew Bodek 
810cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
8119b8d05b8SZbigniew Bodek 
81243fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
81343fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
81443fefd16SMarcin Wojtas 
81543fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
81643fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
81743fefd16SMarcin Wojtas 
8189b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
8199b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
8209b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
8219b8d05b8SZbigniew Bodek 
8229b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
8239b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
8249b8d05b8SZbigniew Bodek 
8259b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
8269b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
8279b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
8289b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
8299b8d05b8SZbigniew Bodek 		if (err != 0) {
8304e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
8319b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8327d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
8339b8d05b8SZbigniew Bodek 		}
8349b8d05b8SZbigniew Bodek 	}
8359b8d05b8SZbigniew Bodek 
8369b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8370bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8389b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8390bdffe59SMarcin Wojtas 		if (err != 0) {
8409b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8419b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8429b8d05b8SZbigniew Bodek 		} else {
8439b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8449b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8459b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8469b8d05b8SZbigniew Bodek 		}
8479b8d05b8SZbigniew Bodek 	}
8489b8d05b8SZbigniew Bodek 
8499b8d05b8SZbigniew Bodek 	return (0);
8509b8d05b8SZbigniew Bodek 
8517d2544e6SMarcin Wojtas err_buf_info_unmap:
8529b8d05b8SZbigniew Bodek 	while (i--) {
8539b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8549b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8559b8d05b8SZbigniew Bodek 	}
8569b8d05b8SZbigniew Bodek 
85743fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
85843fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
859cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8609b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8619b8d05b8SZbigniew Bodek 	return (ENOMEM);
8629b8d05b8SZbigniew Bodek }
8639b8d05b8SZbigniew Bodek 
8649b8d05b8SZbigniew Bodek /**
8659b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
8669b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8679b8d05b8SZbigniew Bodek  * @qid: queue index
8689b8d05b8SZbigniew Bodek  *
8699b8d05b8SZbigniew Bodek  * Free all receive software resources
8709b8d05b8SZbigniew Bodek  **/
8719b8d05b8SZbigniew Bodek static void
8729b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8739b8d05b8SZbigniew Bodek {
8749b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
8759b8d05b8SZbigniew Bodek 
8769b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
8779b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
878e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag,
879e8073738SMarcin Wojtas 		    rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD);
8809b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
8819b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
8829b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
8839b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8849b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8859b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8869b8d05b8SZbigniew Bodek 	}
8879b8d05b8SZbigniew Bodek 
8889b8d05b8SZbigniew Bodek 	/* free LRO resources, */
8899b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
8909b8d05b8SZbigniew Bodek 
8919b8d05b8SZbigniew Bodek 	/* free allocated memory */
892cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8939b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8949b8d05b8SZbigniew Bodek 
89543fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
89643fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
8979b8d05b8SZbigniew Bodek }
8989b8d05b8SZbigniew Bodek 
8999b8d05b8SZbigniew Bodek /**
9009b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
9019b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9029b8d05b8SZbigniew Bodek  *
9039b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
9049b8d05b8SZbigniew Bodek  **/
9059b8d05b8SZbigniew Bodek static int
9069b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
9079b8d05b8SZbigniew Bodek {
9089b8d05b8SZbigniew Bodek 	int i, rc = 0;
9099b8d05b8SZbigniew Bodek 
9109b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
9119b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
9120bdffe59SMarcin Wojtas 		if (rc != 0) {
9139b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
9149b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
9159b8d05b8SZbigniew Bodek 			goto err_setup_rx;
9169b8d05b8SZbigniew Bodek 		}
9177d2544e6SMarcin Wojtas 	}
9189b8d05b8SZbigniew Bodek 	return (0);
9199b8d05b8SZbigniew Bodek 
9209b8d05b8SZbigniew Bodek err_setup_rx:
9219b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
9229b8d05b8SZbigniew Bodek 	while (i--)
9239b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9249b8d05b8SZbigniew Bodek 	return (rc);
9259b8d05b8SZbigniew Bodek }
9269b8d05b8SZbigniew Bodek 
9279b8d05b8SZbigniew Bodek /**
9289b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
9299b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9309b8d05b8SZbigniew Bodek  *
9319b8d05b8SZbigniew Bodek  * Free all receive software resources
9329b8d05b8SZbigniew Bodek  **/
9339b8d05b8SZbigniew Bodek static void
9349b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9359b8d05b8SZbigniew Bodek {
9369b8d05b8SZbigniew Bodek 	int i;
9379b8d05b8SZbigniew Bodek 
9389b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
9399b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9409b8d05b8SZbigniew Bodek }
9419b8d05b8SZbigniew Bodek 
9429b8d05b8SZbigniew Bodek static inline int
9439b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9449b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9459b8d05b8SZbigniew Bodek {
9469b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9479b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9489b8d05b8SZbigniew Bodek 	int nsegs, error;
9494727bda6SMarcin Wojtas 	int mlen;
9509b8d05b8SZbigniew Bodek 
9519b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
9523f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
9539b8d05b8SZbigniew Bodek 		return (0);
9549b8d05b8SZbigniew Bodek 
9559b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
9569b8d05b8SZbigniew Bodek 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
9579b8d05b8SZbigniew Bodek 
9583f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
9594727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
9604727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9614727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
9629b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
9639b8d05b8SZbigniew Bodek 			return (ENOMEM);
9649b8d05b8SZbigniew Bodek 		}
9654727bda6SMarcin Wojtas 		mlen = MCLBYTES;
9664727bda6SMarcin Wojtas 	} else {
9674727bda6SMarcin Wojtas 		mlen = MJUM16BYTES;
9684727bda6SMarcin Wojtas 	}
9699b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
9704727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
9719b8d05b8SZbigniew Bodek 
9729b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
9739b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
97430425f93SMarcin Wojtas 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d\n",
9759b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
9769b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
9779b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
9783f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
9794e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
9809b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
9819b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
9829b8d05b8SZbigniew Bodek 		goto exit;
9839b8d05b8SZbigniew Bodek 
9849b8d05b8SZbigniew Bodek 	}
9859b8d05b8SZbigniew Bodek 
9869b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
9879b8d05b8SZbigniew Bodek 
9889b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
9899b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
9904727bda6SMarcin Wojtas 	ena_buf->len = mlen;
9919b8d05b8SZbigniew Bodek 
9929b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9939b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
9949b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
9959b8d05b8SZbigniew Bodek 
9969b8d05b8SZbigniew Bodek 	return (0);
9979b8d05b8SZbigniew Bodek 
9989b8d05b8SZbigniew Bodek exit:
9999b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10009b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10019b8d05b8SZbigniew Bodek 	return (EFAULT);
10029b8d05b8SZbigniew Bodek }
10039b8d05b8SZbigniew Bodek 
10049b8d05b8SZbigniew Bodek static void
10059b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
10069b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
10079b8d05b8SZbigniew Bodek {
10089b8d05b8SZbigniew Bodek 
10094e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
10104e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
10119b8d05b8SZbigniew Bodek 		return;
10124e8acd84SMarcin Wojtas 	}
10139b8d05b8SZbigniew Bodek 
1014e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1015e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
10169b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
10179b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10189b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10199b8d05b8SZbigniew Bodek }
10209b8d05b8SZbigniew Bodek 
10219b8d05b8SZbigniew Bodek /**
10229b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
10239b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
10249b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
10259b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
10269b8d05b8SZbigniew Bodek  **/
10279b8d05b8SZbigniew Bodek static int
10289b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
10299b8d05b8SZbigniew Bodek {
10309b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
103143fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
10329b8d05b8SZbigniew Bodek 	uint32_t i;
10339b8d05b8SZbigniew Bodek 	int rc;
10349b8d05b8SZbigniew Bodek 
103530425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d\n",
10369b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10379b8d05b8SZbigniew Bodek 
10389b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10399b8d05b8SZbigniew Bodek 
10409b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
104143fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
104243fefd16SMarcin Wojtas 
10439b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
104430425f93SMarcin Wojtas 		    "RX buffer - next to use: %d\n", next_to_use);
10459b8d05b8SZbigniew Bodek 
104643fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
104743fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
10489b8d05b8SZbigniew Bodek 
10499b8d05b8SZbigniew Bodek 		rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
10503f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
10514e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10524e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
10534e8acd84SMarcin Wojtas 			    rx_ring->qid);
10549b8d05b8SZbigniew Bodek 			break;
10559b8d05b8SZbigniew Bodek 		}
10569b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
105743fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
10580bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
10594e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10609b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
10619b8d05b8SZbigniew Bodek 			    rx_ring->qid);
10629b8d05b8SZbigniew Bodek 			break;
10639b8d05b8SZbigniew Bodek 		}
10649b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
10659b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
10669b8d05b8SZbigniew Bodek 	}
10679b8d05b8SZbigniew Bodek 
10683f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
10699b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
10704e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING,
10714e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
10724e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
10739b8d05b8SZbigniew Bodek 	}
10749b8d05b8SZbigniew Bodek 
10753f9ed7abSMarcin Wojtas 	if (likely(i != 0)) {
10769b8d05b8SZbigniew Bodek 		wmb();
10779b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
10789b8d05b8SZbigniew Bodek 	}
10799b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
10809b8d05b8SZbigniew Bodek 	return (i);
10819b8d05b8SZbigniew Bodek }
10829b8d05b8SZbigniew Bodek 
10839b8d05b8SZbigniew Bodek static void
10849b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
10859b8d05b8SZbigniew Bodek {
10869b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
10879b8d05b8SZbigniew Bodek 	unsigned int i;
10889b8d05b8SZbigniew Bodek 
10899b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
10909b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
10919b8d05b8SZbigniew Bodek 
10920bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
10939b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
10949b8d05b8SZbigniew Bodek 	}
10959b8d05b8SZbigniew Bodek }
10969b8d05b8SZbigniew Bodek 
10979b8d05b8SZbigniew Bodek /**
10989b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
10999b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
11009b8d05b8SZbigniew Bodek  *
11019b8d05b8SZbigniew Bodek  */
11029b8d05b8SZbigniew Bodek static void
11039b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
11049b8d05b8SZbigniew Bodek {
11059b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
11069b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
11079b8d05b8SZbigniew Bodek 
11089b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11099b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
11109b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
11119b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
11129b8d05b8SZbigniew Bodek 
11139b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
11144e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
11154e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
11169b8d05b8SZbigniew Bodek 	}
11179b8d05b8SZbigniew Bodek }
11189b8d05b8SZbigniew Bodek 
11199b8d05b8SZbigniew Bodek static void
11209b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
11219b8d05b8SZbigniew Bodek {
11229b8d05b8SZbigniew Bodek 	int i;
11239b8d05b8SZbigniew Bodek 
11249b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
11259b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
11269b8d05b8SZbigniew Bodek }
11279b8d05b8SZbigniew Bodek 
11289b8d05b8SZbigniew Bodek /**
11299b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
11309b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
11319b8d05b8SZbigniew Bodek  * @qid: queue index
11329b8d05b8SZbigniew Bodek  **/
11339b8d05b8SZbigniew Bodek static void
11349b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
11359b8d05b8SZbigniew Bodek {
11364e8acd84SMarcin Wojtas 	bool print_once = true;
11379b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
11389b8d05b8SZbigniew Bodek 
1139416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
11409b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
11419b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
11429b8d05b8SZbigniew Bodek 
11439b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
11449b8d05b8SZbigniew Bodek 			continue;
11459b8d05b8SZbigniew Bodek 
11464e8acd84SMarcin Wojtas 		if (print_once) {
11474e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
114830425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
11494e8acd84SMarcin Wojtas 			    qid, i);
11504e8acd84SMarcin Wojtas 			print_once = false;
11514e8acd84SMarcin Wojtas 		} else {
11524e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG,
115330425f93SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x\n",
11544e8acd84SMarcin Wojtas 			     qid, i);
11554e8acd84SMarcin Wojtas 		}
11569b8d05b8SZbigniew Bodek 
11574fa9e02dSMarcin Wojtas 		if (tx_info->head_mapped == true) {
11584fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_head,
1159e8073738SMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
11604fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
11614fa9e02dSMarcin Wojtas 			    tx_info->map_head);
11624fa9e02dSMarcin Wojtas 			tx_info->head_mapped = false;
11634fa9e02dSMarcin Wojtas 		}
11644fa9e02dSMarcin Wojtas 
11654fa9e02dSMarcin Wojtas 		if (tx_info->seg_mapped == true) {
11664fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_seg,
11674fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
11684fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
11694fa9e02dSMarcin Wojtas 			    tx_info->map_seg);
11704fa9e02dSMarcin Wojtas 			tx_info->seg_mapped = false;
11714fa9e02dSMarcin Wojtas 		}
11724fa9e02dSMarcin Wojtas 
11739b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
11749b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
11759b8d05b8SZbigniew Bodek 	}
1176416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
11779b8d05b8SZbigniew Bodek }
11789b8d05b8SZbigniew Bodek 
11799b8d05b8SZbigniew Bodek static void
11809b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
11819b8d05b8SZbigniew Bodek {
11829b8d05b8SZbigniew Bodek 
11839b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++)
11849b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
11859b8d05b8SZbigniew Bodek }
11869b8d05b8SZbigniew Bodek 
11879b8d05b8SZbigniew Bodek static void
11889b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
11899b8d05b8SZbigniew Bodek {
11909b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11919b8d05b8SZbigniew Bodek 	int i;
11929b8d05b8SZbigniew Bodek 
11939b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11949b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11959b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11969b8d05b8SZbigniew Bodek 	}
11979b8d05b8SZbigniew Bodek }
11989b8d05b8SZbigniew Bodek 
11999b8d05b8SZbigniew Bodek static void
12009b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
12019b8d05b8SZbigniew Bodek {
12029b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12039b8d05b8SZbigniew Bodek 	int i;
12049b8d05b8SZbigniew Bodek 
12059b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12069b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12079b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
12089b8d05b8SZbigniew Bodek 	}
12099b8d05b8SZbigniew Bodek }
12109b8d05b8SZbigniew Bodek 
12119b8d05b8SZbigniew Bodek static void
12129b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
12139b8d05b8SZbigniew Bodek {
12145cb9db07SMarcin Wojtas 	struct ena_que *queue;
12155cb9db07SMarcin Wojtas 	int i;
12165cb9db07SMarcin Wojtas 
12175cb9db07SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
12185cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
12195cb9db07SMarcin Wojtas 		while (taskqueue_cancel(queue->cleanup_tq,
12205cb9db07SMarcin Wojtas 		    &queue->cleanup_task, NULL))
12215cb9db07SMarcin Wojtas 			taskqueue_drain(queue->cleanup_tq,
12225cb9db07SMarcin Wojtas 			    &queue->cleanup_task);
12235cb9db07SMarcin Wojtas 		taskqueue_free(queue->cleanup_tq);
12245cb9db07SMarcin Wojtas 	}
12255cb9db07SMarcin Wojtas 
12269b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
12279b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
12289b8d05b8SZbigniew Bodek }
12299b8d05b8SZbigniew Bodek 
1230fceb9387SMarcin Wojtas static inline int
12319b8d05b8SZbigniew Bodek validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
12329b8d05b8SZbigniew Bodek {
12334e8acd84SMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
12349b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info = NULL;
12359b8d05b8SZbigniew Bodek 
12369b8d05b8SZbigniew Bodek 	if (likely(req_id < tx_ring->ring_size)) {
12379b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
12380bdffe59SMarcin Wojtas 		if (tx_info->mbuf != NULL)
12390bdffe59SMarcin Wojtas 			return (0);
12404e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
12414e8acd84SMarcin Wojtas 		    "tx_info doesn't have valid mbuf\n");
12424e306999SMarcin Wojtas 	}
12434e8acd84SMarcin Wojtas 
12444e306999SMarcin Wojtas 	device_printf(adapter->pdev, "Invalid req_id: %hu\n", req_id);
12459b8d05b8SZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
12469b8d05b8SZbigniew Bodek 
1247858659f7SMarcin Wojtas 	/* Trigger device reset */
1248858659f7SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_INV_TX_REQ_ID;
1249858659f7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
1250858659f7SMarcin Wojtas 
12519b8d05b8SZbigniew Bodek 	return (EFAULT);
12529b8d05b8SZbigniew Bodek }
12539b8d05b8SZbigniew Bodek 
12549b8d05b8SZbigniew Bodek static int
12559b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
12569b8d05b8SZbigniew Bodek {
12579b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
12589b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
12599b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
12605cb9db07SMarcin Wojtas 	struct ena_que *queue;
12619b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12629b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
12639b8d05b8SZbigniew Bodek 	int rc, i;
12649b8d05b8SZbigniew Bodek 
12659b8d05b8SZbigniew Bodek 	/* Create TX queues */
12669b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12679b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12689b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
12699b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
12709b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
12719b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
12729b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12739b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12749b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12750bdffe59SMarcin Wojtas 		if (rc != 0) {
12769b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12779b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
12789b8d05b8SZbigniew Bodek 			goto err_tx;
12799b8d05b8SZbigniew Bodek 		}
12809b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
12819b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12829b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12839b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12840bdffe59SMarcin Wojtas 		if (rc != 0) {
12859b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12869b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
12879b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12889b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12899b8d05b8SZbigniew Bodek 			goto err_tx;
12909b8d05b8SZbigniew Bodek 		}
12919b8d05b8SZbigniew Bodek 	}
12929b8d05b8SZbigniew Bodek 
12939b8d05b8SZbigniew Bodek 	/* Create RX queues */
12949b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12959b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12969b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12979b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
12989b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
12999b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
13009b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
13019b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
13029b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
13033f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
13049b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
13059b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
13069b8d05b8SZbigniew Bodek 			goto err_rx;
13079b8d05b8SZbigniew Bodek 		}
13089b8d05b8SZbigniew Bodek 
13099b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
13109b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
13119b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
13129b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
13133f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
13149b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
13159b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
13169b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
13179b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
13189b8d05b8SZbigniew Bodek 			goto err_rx;
13199b8d05b8SZbigniew Bodek 		}
13209b8d05b8SZbigniew Bodek 	}
13219b8d05b8SZbigniew Bodek 
13225cb9db07SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
13235cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
13245cb9db07SMarcin Wojtas 
13255cb9db07SMarcin Wojtas 		TASK_INIT(&queue->cleanup_task, 0, ena_cleanup, queue);
13265cb9db07SMarcin Wojtas 		queue->cleanup_tq = taskqueue_create_fast("ena cleanup",
13275cb9db07SMarcin Wojtas 		    M_WAITOK, taskqueue_thread_enqueue, &queue->cleanup_tq);
13285cb9db07SMarcin Wojtas 
13295cb9db07SMarcin Wojtas 		taskqueue_start_threads(&queue->cleanup_tq, 1, PI_NET,
13305cb9db07SMarcin Wojtas 		    "%s queue %d cleanup",
13315cb9db07SMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
13325cb9db07SMarcin Wojtas 	}
13335cb9db07SMarcin Wojtas 
13349b8d05b8SZbigniew Bodek 	return (0);
13359b8d05b8SZbigniew Bodek 
13369b8d05b8SZbigniew Bodek err_rx:
13379b8d05b8SZbigniew Bodek 	while (i--)
13389b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
13399b8d05b8SZbigniew Bodek 	i = adapter->num_queues;
13409b8d05b8SZbigniew Bodek err_tx:
13419b8d05b8SZbigniew Bodek 	while (i--)
13429b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
13439b8d05b8SZbigniew Bodek 
13449b8d05b8SZbigniew Bodek 	return (ENXIO);
13459b8d05b8SZbigniew Bodek }
13469b8d05b8SZbigniew Bodek 
13479b8d05b8SZbigniew Bodek /**
13489b8d05b8SZbigniew Bodek  * ena_tx_cleanup - clear sent packets and corresponding descriptors
13499b8d05b8SZbigniew Bodek  * @tx_ring: ring for which we want to clean packets
13509b8d05b8SZbigniew Bodek  *
13519b8d05b8SZbigniew Bodek  * Once packets are sent, we ask the device in a loop for no longer used
13529b8d05b8SZbigniew Bodek  * descriptors. We find the related mbuf chain in a map (index in an array)
13539b8d05b8SZbigniew Bodek  * and free it, then update ring state.
13549b8d05b8SZbigniew Bodek  * This is performed in "endless" loop, updating ring pointers every
13559b8d05b8SZbigniew Bodek  * TX_COMMIT. The first check of free descriptor is performed before the actual
13569b8d05b8SZbigniew Bodek  * loop, then repeated at the loop end.
13579b8d05b8SZbigniew Bodek  **/
13589b8d05b8SZbigniew Bodek static int
13599b8d05b8SZbigniew Bodek ena_tx_cleanup(struct ena_ring *tx_ring)
13609b8d05b8SZbigniew Bodek {
13619b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
13629b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
13639b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
13649b8d05b8SZbigniew Bodek 	uint16_t req_id;
13659b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
13669b8d05b8SZbigniew Bodek 	unsigned int total_done = 0;
13679b8d05b8SZbigniew Bodek 	int rc;
13689b8d05b8SZbigniew Bodek 	int commit = TX_COMMIT;
13699b8d05b8SZbigniew Bodek 	int budget = TX_BUDGET;
13709b8d05b8SZbigniew Bodek 	int work_done;
13715cb9db07SMarcin Wojtas 	bool above_thresh;
13729b8d05b8SZbigniew Bodek 
13739b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
13749b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
13759b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
13769b8d05b8SZbigniew Bodek 	next_to_clean = tx_ring->next_to_clean;
13779b8d05b8SZbigniew Bodek 
13789b8d05b8SZbigniew Bodek 	do {
13799b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info;
13809b8d05b8SZbigniew Bodek 		struct mbuf *mbuf;
13819b8d05b8SZbigniew Bodek 
13829b8d05b8SZbigniew Bodek 		rc = ena_com_tx_comp_req_id_get(io_cq, &req_id);
13833f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
13849b8d05b8SZbigniew Bodek 			break;
13859b8d05b8SZbigniew Bodek 
13869b8d05b8SZbigniew Bodek 		rc = validate_tx_req_id(tx_ring, req_id);
13873f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
13889b8d05b8SZbigniew Bodek 			break;
13899b8d05b8SZbigniew Bodek 
13909b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
13919b8d05b8SZbigniew Bodek 
13929b8d05b8SZbigniew Bodek 		mbuf = tx_info->mbuf;
13939b8d05b8SZbigniew Bodek 
13949b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
13959b8d05b8SZbigniew Bodek 		bintime_clear(&tx_info->timestamp);
13969b8d05b8SZbigniew Bodek 
13979b8d05b8SZbigniew Bodek 		/* Map is no longer required */
13984fa9e02dSMarcin Wojtas 		if (tx_info->head_mapped == true) {
13994fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_head,
1400e8073738SMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
14014fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
14024fa9e02dSMarcin Wojtas 			    tx_info->map_head);
14034fa9e02dSMarcin Wojtas 			tx_info->head_mapped = false;
14044fa9e02dSMarcin Wojtas 		}
14054fa9e02dSMarcin Wojtas 		if (tx_info->seg_mapped == true) {
14064fa9e02dSMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_seg,
14074fa9e02dSMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
14084fa9e02dSMarcin Wojtas 			bus_dmamap_unload(adapter->tx_buf_tag,
14094fa9e02dSMarcin Wojtas 			    tx_info->map_seg);
14104fa9e02dSMarcin Wojtas 			tx_info->seg_mapped = false;
14119b8d05b8SZbigniew Bodek 		}
14129b8d05b8SZbigniew Bodek 
141330425f93SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d mbuf %p completed\n",
14144e8acd84SMarcin Wojtas 		    tx_ring->qid, mbuf);
14154e8acd84SMarcin Wojtas 
14169b8d05b8SZbigniew Bodek 		m_freem(mbuf);
14179b8d05b8SZbigniew Bodek 
14189b8d05b8SZbigniew Bodek 		total_done += tx_info->tx_descs;
14199b8d05b8SZbigniew Bodek 
14209b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[next_to_clean] = req_id;
14219b8d05b8SZbigniew Bodek 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
14229b8d05b8SZbigniew Bodek 		    tx_ring->ring_size);
14239b8d05b8SZbigniew Bodek 
14243f9ed7abSMarcin Wojtas 		if (unlikely(--commit == 0)) {
14259b8d05b8SZbigniew Bodek 			commit = TX_COMMIT;
14269b8d05b8SZbigniew Bodek 			/* update ring state every TX_COMMIT descriptor */
14279b8d05b8SZbigniew Bodek 			tx_ring->next_to_clean = next_to_clean;
14280bdffe59SMarcin Wojtas 			ena_com_comp_ack(
14290bdffe59SMarcin Wojtas 			    &adapter->ena_dev->io_sq_queues[ena_qid],
14300bdffe59SMarcin Wojtas 			    total_done);
14319b8d05b8SZbigniew Bodek 			ena_com_update_dev_comp_head(io_cq);
14329b8d05b8SZbigniew Bodek 			total_done = 0;
14339b8d05b8SZbigniew Bodek 		}
14343f9ed7abSMarcin Wojtas 	} while (likely(--budget));
14359b8d05b8SZbigniew Bodek 
14369b8d05b8SZbigniew Bodek 	work_done = TX_BUDGET - budget;
14379b8d05b8SZbigniew Bodek 
143830425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d done. total pkts: %d\n",
14394e8acd84SMarcin Wojtas 	tx_ring->qid, work_done);
14404e8acd84SMarcin Wojtas 
14419b8d05b8SZbigniew Bodek 	/* If there is still something to commit update ring state */
14423f9ed7abSMarcin Wojtas 	if (likely(commit != TX_COMMIT)) {
14439b8d05b8SZbigniew Bodek 		tx_ring->next_to_clean = next_to_clean;
14440bdffe59SMarcin Wojtas 		ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid],
14450bdffe59SMarcin Wojtas 		    total_done);
14469b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(io_cq);
14479b8d05b8SZbigniew Bodek 	}
14489b8d05b8SZbigniew Bodek 
14495cb9db07SMarcin Wojtas 	/*
14505cb9db07SMarcin Wojtas 	 * Need to make the rings circular update visible to
14515cb9db07SMarcin Wojtas 	 * ena_xmit_mbuf() before checking for tx_ring->running.
14525cb9db07SMarcin Wojtas 	 */
14535cb9db07SMarcin Wojtas 	mb();
14545cb9db07SMarcin Wojtas 
14555cb9db07SMarcin Wojtas 	above_thresh = ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
14565cb9db07SMarcin Wojtas 	    ENA_TX_RESUME_THRESH);
14575cb9db07SMarcin Wojtas 	if (unlikely(!tx_ring->running && above_thresh)) {
14585cb9db07SMarcin Wojtas 		ENA_RING_MTX_LOCK(tx_ring);
14595cb9db07SMarcin Wojtas 		above_thresh =
14605cb9db07SMarcin Wojtas 		    ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
14615cb9db07SMarcin Wojtas 		    ENA_TX_RESUME_THRESH);
14625cb9db07SMarcin Wojtas 		if (!tx_ring->running && above_thresh) {
14635cb9db07SMarcin Wojtas 			tx_ring->running = true;
14645cb9db07SMarcin Wojtas 			counter_u64_add(tx_ring->tx_stats.queue_wakeup, 1);
14655cb9db07SMarcin Wojtas 			taskqueue_enqueue(tx_ring->enqueue_tq,
14665cb9db07SMarcin Wojtas 			    &tx_ring->enqueue_task);
14675cb9db07SMarcin Wojtas 		}
14685cb9db07SMarcin Wojtas 		ENA_RING_MTX_UNLOCK(tx_ring);
14695cb9db07SMarcin Wojtas 	}
14709b8d05b8SZbigniew Bodek 
14719b8d05b8SZbigniew Bodek 	return (work_done);
14729b8d05b8SZbigniew Bodek }
14739b8d05b8SZbigniew Bodek 
14749b8d05b8SZbigniew Bodek static void
14759b8d05b8SZbigniew Bodek ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
14769b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
14779b8d05b8SZbigniew Bodek {
14789b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
14799b8d05b8SZbigniew Bodek 
1480fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) {
14819b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
14829b8d05b8SZbigniew Bodek 
14839b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->frag &&
1484bfea0e93SMarcin Wojtas 		    (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
14859b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
14869b8d05b8SZbigniew Bodek 			return;
14879b8d05b8SZbigniew Bodek 		}
14889b8d05b8SZbigniew Bodek 
14899b8d05b8SZbigniew Bodek 		switch (ena_rx_ctx->l3_proto) {
14909b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV4:
14919b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
14929b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14939b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
14949b8d05b8SZbigniew Bodek 				break;
14959b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14969b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
14979b8d05b8SZbigniew Bodek 				break;
14989b8d05b8SZbigniew Bodek 			default:
14999b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
15009b8d05b8SZbigniew Bodek 			}
15019b8d05b8SZbigniew Bodek 			break;
15029b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV6:
15039b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
15049b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
15059b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
15069b8d05b8SZbigniew Bodek 				break;
15079b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
15089b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
15099b8d05b8SZbigniew Bodek 				break;
15109b8d05b8SZbigniew Bodek 			default:
15119b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
15129b8d05b8SZbigniew Bodek 			}
15139b8d05b8SZbigniew Bodek 			break;
15149b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
15159b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
15169b8d05b8SZbigniew Bodek 			break;
15179b8d05b8SZbigniew Bodek 		default:
15189b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
15199b8d05b8SZbigniew Bodek 		}
15209b8d05b8SZbigniew Bodek 	} else {
15219b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = rx_ring->qid;
15229b8d05b8SZbigniew Bodek 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
15239b8d05b8SZbigniew Bodek 	}
15249b8d05b8SZbigniew Bodek }
15259b8d05b8SZbigniew Bodek 
15269b8d05b8SZbigniew Bodek /**
15279b8d05b8SZbigniew Bodek  * ena_rx_mbuf - assemble mbuf from descriptors
15289b8d05b8SZbigniew Bodek  * @rx_ring: ring for which we want to clean packets
15299b8d05b8SZbigniew Bodek  * @ena_bufs: buffer info
15309b8d05b8SZbigniew Bodek  * @ena_rx_ctx: metadata for this packet(s)
153143fefd16SMarcin Wojtas  * @next_to_clean: ring pointer, will be updated only upon success
15329b8d05b8SZbigniew Bodek  *
15339b8d05b8SZbigniew Bodek  **/
15349b8d05b8SZbigniew Bodek static struct mbuf*
15359b8d05b8SZbigniew Bodek ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
15369b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
15379b8d05b8SZbigniew Bodek {
15389b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
15399b8d05b8SZbigniew Bodek 	struct ena_rx_buffer *rx_info;
15409b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
15419b8d05b8SZbigniew Bodek 	unsigned int descs = ena_rx_ctx->descs;
1542c51a229cSMarcin Wojtas 	int rc;
154343fefd16SMarcin Wojtas 	uint16_t ntc, len, req_id, buf = 0;
15449b8d05b8SZbigniew Bodek 
154543fefd16SMarcin Wojtas 	ntc = *next_to_clean;
15469b8d05b8SZbigniew Bodek 	adapter = rx_ring->adapter;
15479b8d05b8SZbigniew Bodek 
154843fefd16SMarcin Wojtas 	len = ena_bufs[buf].len;
154943fefd16SMarcin Wojtas 	req_id = ena_bufs[buf].req_id;
1550c51a229cSMarcin Wojtas 	rc = validate_rx_req_id(rx_ring, req_id);
1551c51a229cSMarcin Wojtas 	if (unlikely(rc != 0))
1552c51a229cSMarcin Wojtas 		return (NULL);
1553c51a229cSMarcin Wojtas 
155443fefd16SMarcin Wojtas 	rx_info = &rx_ring->rx_buffer_info[req_id];
15551d65b4c0SMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
15561d65b4c0SMarcin Wojtas 		device_printf(adapter->pdev, "NULL mbuf in rx_info");
15571d65b4c0SMarcin Wojtas 		return (NULL);
15581d65b4c0SMarcin Wojtas 	}
155943fefd16SMarcin Wojtas 
156030425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx\n",
15619b8d05b8SZbigniew Bodek 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
15629b8d05b8SZbigniew Bodek 
1563e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1564e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
15659b8d05b8SZbigniew Bodek 	mbuf = rx_info->mbuf;
15669b8d05b8SZbigniew Bodek 	mbuf->m_flags |= M_PKTHDR;
15679b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.len = len;
15689b8d05b8SZbigniew Bodek 	mbuf->m_len = len;
15699b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.rcvif = rx_ring->que->adapter->ifp;
15709b8d05b8SZbigniew Bodek 
15719b8d05b8SZbigniew Bodek 	/* Fill mbuf with hash key and it's interpretation for optimization */
15729b8d05b8SZbigniew Bodek 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
15739b8d05b8SZbigniew Bodek 
157430425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d\n",
15759b8d05b8SZbigniew Bodek 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
15769b8d05b8SZbigniew Bodek 
15779b8d05b8SZbigniew Bodek 	/* DMA address is not needed anymore, unmap it */
15789b8d05b8SZbigniew Bodek 	bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
15799b8d05b8SZbigniew Bodek 
15809b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
158143fefd16SMarcin Wojtas 	rx_ring->free_rx_ids[ntc] = req_id;
158243fefd16SMarcin Wojtas 	ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
15839b8d05b8SZbigniew Bodek 
15849b8d05b8SZbigniew Bodek 	/*
15859b8d05b8SZbigniew Bodek 	 * While we have more than 1 descriptors for one rcvd packet, append
15869b8d05b8SZbigniew Bodek 	 * other mbufs to the main one
15879b8d05b8SZbigniew Bodek 	 */
15889b8d05b8SZbigniew Bodek 	while (--descs) {
158943fefd16SMarcin Wojtas 		++buf;
159043fefd16SMarcin Wojtas 		len = ena_bufs[buf].len;
159143fefd16SMarcin Wojtas 		req_id = ena_bufs[buf].req_id;
1592c51a229cSMarcin Wojtas 		rc = validate_rx_req_id(rx_ring, req_id);
1593c51a229cSMarcin Wojtas 		if (unlikely(rc != 0)) {
1594c51a229cSMarcin Wojtas 			/*
1595c51a229cSMarcin Wojtas 			 * If the req_id is invalid, then the device will be
1596c51a229cSMarcin Wojtas 			 * reset. In that case we must free all mbufs that
1597c51a229cSMarcin Wojtas 			 * were already gathered.
1598c51a229cSMarcin Wojtas 			 */
1599c51a229cSMarcin Wojtas 			m_freem(mbuf);
1600c51a229cSMarcin Wojtas 			return (NULL);
1601c51a229cSMarcin Wojtas 		}
160243fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
16039b8d05b8SZbigniew Bodek 
160443fefd16SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
160543fefd16SMarcin Wojtas 			device_printf(adapter->pdev, "NULL mbuf in rx_info");
160643fefd16SMarcin Wojtas 			/*
160743fefd16SMarcin Wojtas 			 * If one of the required mbufs was not allocated yet,
160843fefd16SMarcin Wojtas 			 * we can break there.
160943fefd16SMarcin Wojtas 			 * All earlier used descriptors will be reallocated
161043fefd16SMarcin Wojtas 			 * later and not used mbufs can be reused.
161143fefd16SMarcin Wojtas 			 * The next_to_clean pointer will not be updated in case
161243fefd16SMarcin Wojtas 			 * of an error, so caller should advance it manually
161343fefd16SMarcin Wojtas 			 * in error handling routine to keep it up to date
161443fefd16SMarcin Wojtas 			 * with hw ring.
161543fefd16SMarcin Wojtas 			 */
161643fefd16SMarcin Wojtas 			m_freem(mbuf);
161743fefd16SMarcin Wojtas 			return (NULL);
161843fefd16SMarcin Wojtas 		}
161943fefd16SMarcin Wojtas 
1620e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1621e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTREAD);
16223f9ed7abSMarcin Wojtas 		if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
16239b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
162430425f93SMarcin Wojtas 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p\n",
16259b8d05b8SZbigniew Bodek 			    mbuf);
16269b8d05b8SZbigniew Bodek 		}
16274e8acd84SMarcin Wojtas 
16284e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH,
162930425f93SMarcin Wojtas 		    "rx mbuf updated. len %d\n", mbuf->m_pkthdr.len);
16304e8acd84SMarcin Wojtas 
16319b8d05b8SZbigniew Bodek 		/* Free already appended mbuf, it won't be useful anymore */
16329b8d05b8SZbigniew Bodek 		bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
16339b8d05b8SZbigniew Bodek 		m_freem(rx_info->mbuf);
16349b8d05b8SZbigniew Bodek 		rx_info->mbuf = NULL;
16359b8d05b8SZbigniew Bodek 
163643fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[ntc] = req_id;
163743fefd16SMarcin Wojtas 		ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
16389b8d05b8SZbigniew Bodek 	}
16399b8d05b8SZbigniew Bodek 
164043fefd16SMarcin Wojtas 	*next_to_clean = ntc;
164143fefd16SMarcin Wojtas 
16429b8d05b8SZbigniew Bodek 	return (mbuf);
16439b8d05b8SZbigniew Bodek }
16449b8d05b8SZbigniew Bodek 
16459b8d05b8SZbigniew Bodek /**
16469b8d05b8SZbigniew Bodek  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
16479b8d05b8SZbigniew Bodek  **/
16489b8d05b8SZbigniew Bodek static inline void
16499b8d05b8SZbigniew Bodek ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
16509b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
16519b8d05b8SZbigniew Bodek {
16529b8d05b8SZbigniew Bodek 
16539b8d05b8SZbigniew Bodek 	/* if IP and error */
16543f9ed7abSMarcin Wojtas 	if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
16553f9ed7abSMarcin Wojtas 	    ena_rx_ctx->l3_csum_err)) {
16569b8d05b8SZbigniew Bodek 		/* ipv4 checksum error */
16579b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.csum_flags = 0;
16589b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
165930425f93SMarcin Wojtas 		ena_trace(ENA_DBG, "RX IPv4 header checksum error\n");
16609b8d05b8SZbigniew Bodek 		return;
16619b8d05b8SZbigniew Bodek 	}
16629b8d05b8SZbigniew Bodek 
16639b8d05b8SZbigniew Bodek 	/* if TCP/UDP */
16649b8d05b8SZbigniew Bodek 	if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
16659b8d05b8SZbigniew Bodek 	    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
16669b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->l4_csum_err) {
16679b8d05b8SZbigniew Bodek 			/* TCP/UDP checksum error */
16689b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = 0;
16699b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
167030425f93SMarcin Wojtas 			ena_trace(ENA_DBG, "RX L4 checksum error\n");
16719b8d05b8SZbigniew Bodek 		} else {
16729b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
16739b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
16749b8d05b8SZbigniew Bodek 		}
16759b8d05b8SZbigniew Bodek 	}
16769b8d05b8SZbigniew Bodek }
16779b8d05b8SZbigniew Bodek 
16789b8d05b8SZbigniew Bodek /**
16799b8d05b8SZbigniew Bodek  * ena_rx_cleanup - handle rx irq
16809b8d05b8SZbigniew Bodek  * @arg: ring for which irq is being handled
16819b8d05b8SZbigniew Bodek  **/
16829b8d05b8SZbigniew Bodek static int
16839b8d05b8SZbigniew Bodek ena_rx_cleanup(struct ena_ring *rx_ring)
16849b8d05b8SZbigniew Bodek {
16859b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
16869b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
16879b8d05b8SZbigniew Bodek 	struct ena_com_rx_ctx ena_rx_ctx;
16889b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
16899b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
16909b8d05b8SZbigniew Bodek 	if_t ifp;
16919b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
16929b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
16939b8d05b8SZbigniew Bodek 	uint32_t refill_required;
16949b8d05b8SZbigniew Bodek 	uint32_t refill_threshold;
16959b8d05b8SZbigniew Bodek 	uint32_t do_if_input = 0;
16969b8d05b8SZbigniew Bodek 	unsigned int qid;
169743fefd16SMarcin Wojtas 	int rc, i;
16989b8d05b8SZbigniew Bodek 	int budget = RX_BUDGET;
16999b8d05b8SZbigniew Bodek 
17009b8d05b8SZbigniew Bodek 	adapter = rx_ring->que->adapter;
17019b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
17029b8d05b8SZbigniew Bodek 	qid = rx_ring->que->id;
17039b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_RXQ_IDX(qid);
17049b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
17059b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
17069b8d05b8SZbigniew Bodek 	next_to_clean = rx_ring->next_to_clean;
17079b8d05b8SZbigniew Bodek 
170830425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "rx: qid %d\n", qid);
17094e8acd84SMarcin Wojtas 
17109b8d05b8SZbigniew Bodek 	do {
17119b8d05b8SZbigniew Bodek 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
17129b8d05b8SZbigniew Bodek 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
17139b8d05b8SZbigniew Bodek 		ena_rx_ctx.descs = 0;
1714e8073738SMarcin Wojtas 		bus_dmamap_sync(io_cq->cdesc_addr.mem_handle.tag,
1715e8073738SMarcin Wojtas 		    io_cq->cdesc_addr.mem_handle.map, BUS_DMASYNC_POSTREAD);
17169b8d05b8SZbigniew Bodek 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
17179b8d05b8SZbigniew Bodek 
17180bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
17199b8d05b8SZbigniew Bodek 			goto error;
17209b8d05b8SZbigniew Bodek 
17219b8d05b8SZbigniew Bodek 		if (unlikely(ena_rx_ctx.descs == 0))
17229b8d05b8SZbigniew Bodek 			break;
17239b8d05b8SZbigniew Bodek 
17244e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
172530425f93SMarcin Wojtas 		    "descs #: %d l3 proto %d l4 proto %d hash: %x\n",
17264e8acd84SMarcin Wojtas 		    rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
17274e8acd84SMarcin Wojtas 		    ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
17284e8acd84SMarcin Wojtas 
17299b8d05b8SZbigniew Bodek 		/* Receive mbuf from the ring */
17309b8d05b8SZbigniew Bodek 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
17319b8d05b8SZbigniew Bodek 		    &ena_rx_ctx, &next_to_clean);
1732e8073738SMarcin Wojtas 		bus_dmamap_sync(io_cq->cdesc_addr.mem_handle.tag,
1733e8073738SMarcin Wojtas 		    io_cq->cdesc_addr.mem_handle.map, BUS_DMASYNC_PREREAD);
17349b8d05b8SZbigniew Bodek 		/* Exit if we failed to retrieve a buffer */
17350bdffe59SMarcin Wojtas 		if (unlikely(mbuf == NULL)) {
173643fefd16SMarcin Wojtas 			for (i = 0; i < ena_rx_ctx.descs; ++i) {
173743fefd16SMarcin Wojtas 				rx_ring->free_rx_ids[next_to_clean] =
173843fefd16SMarcin Wojtas 				    rx_ring->ena_bufs[i].req_id;
173943fefd16SMarcin Wojtas 				next_to_clean =
174043fefd16SMarcin Wojtas 				    ENA_RX_RING_IDX_NEXT(next_to_clean,
174143fefd16SMarcin Wojtas 				    rx_ring->ring_size);
174243fefd16SMarcin Wojtas 
174343fefd16SMarcin Wojtas 			}
17449b8d05b8SZbigniew Bodek 			break;
17459b8d05b8SZbigniew Bodek 		}
17469b8d05b8SZbigniew Bodek 
17470bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_RXCSUM) != 0) ||
17480bdffe59SMarcin Wojtas 		    ((ifp->if_capenable & IFCAP_RXCSUM_IPV6) != 0)) {
17499b8d05b8SZbigniew Bodek 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
17509b8d05b8SZbigniew Bodek 		}
17519b8d05b8SZbigniew Bodek 
175230217e2dSMarcin Wojtas 		counter_enter();
175330217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
175430217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
175530217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
175630217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
175730217e2dSMarcin Wojtas 		counter_exit();
17589b8d05b8SZbigniew Bodek 		/*
17599b8d05b8SZbigniew Bodek 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
17609b8d05b8SZbigniew Bodek 		 * should be computed by hardware.
17619b8d05b8SZbigniew Bodek 		 */
17629b8d05b8SZbigniew Bodek 		do_if_input = 1;
17630bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_LRO) != 0)  &&
17640bdffe59SMarcin Wojtas 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
17650bdffe59SMarcin Wojtas 		    (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
17669b8d05b8SZbigniew Bodek 			/*
17679b8d05b8SZbigniew Bodek 			 * Send to the stack if:
17689b8d05b8SZbigniew Bodek 			 *  - LRO not enabled, or
17699b8d05b8SZbigniew Bodek 			 *  - no LRO resources, or
17709b8d05b8SZbigniew Bodek 			 *  - lro enqueue fails
17719b8d05b8SZbigniew Bodek 			 */
17720bdffe59SMarcin Wojtas 			if ((rx_ring->lro.lro_cnt != 0) &&
17730bdffe59SMarcin Wojtas 			    (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
17749b8d05b8SZbigniew Bodek 					do_if_input = 0;
17759b8d05b8SZbigniew Bodek 		}
17760bdffe59SMarcin Wojtas 		if (do_if_input != 0) {
17770bdffe59SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_RXPTH,
177830425f93SMarcin Wojtas 			    "calling if_input() with mbuf %p\n", mbuf);
17799b8d05b8SZbigniew Bodek 			(*ifp->if_input)(ifp, mbuf);
17809b8d05b8SZbigniew Bodek 		}
17819b8d05b8SZbigniew Bodek 
178230217e2dSMarcin Wojtas 		counter_enter();
178330217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
178430217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
178530217e2dSMarcin Wojtas 		counter_exit();
17869b8d05b8SZbigniew Bodek 	} while (--budget);
17879b8d05b8SZbigniew Bodek 
17889b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = next_to_clean;
17899b8d05b8SZbigniew Bodek 
1790a195fab0SMarcin Wojtas 	refill_required = ena_com_free_desc(io_sq);
179182f5a792SMarcin Wojtas 	refill_threshold = min_t(int,
179282f5a792SMarcin Wojtas 	    rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER,
179382f5a792SMarcin Wojtas 	    ENA_RX_REFILL_THRESH_PACKET);
17949b8d05b8SZbigniew Bodek 
17959b8d05b8SZbigniew Bodek 	if (refill_required > refill_threshold) {
17969b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
17979b8d05b8SZbigniew Bodek 		ena_refill_rx_bufs(rx_ring, refill_required);
17989b8d05b8SZbigniew Bodek 	}
17999b8d05b8SZbigniew Bodek 
18009b8d05b8SZbigniew Bodek 	tcp_lro_flush_all(&rx_ring->lro);
18019b8d05b8SZbigniew Bodek 
18029b8d05b8SZbigniew Bodek 	return (RX_BUDGET - budget);
18039b8d05b8SZbigniew Bodek 
18049b8d05b8SZbigniew Bodek error:
18059b8d05b8SZbigniew Bodek 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
1806c9b099ecSMarcin Wojtas 
1807c9b099ecSMarcin Wojtas 	/* Too many desc from the device. Trigger reset */
1808858659f7SMarcin Wojtas 	if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
1809c9b099ecSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
1810fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
1811858659f7SMarcin Wojtas 	}
1812c9b099ecSMarcin Wojtas 
1813c9b099ecSMarcin Wojtas 	return (0);
18149b8d05b8SZbigniew Bodek }
18159b8d05b8SZbigniew Bodek 
18169b8d05b8SZbigniew Bodek /*********************************************************************
18179b8d05b8SZbigniew Bodek  *
18189b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
18199b8d05b8SZbigniew Bodek  *
18209b8d05b8SZbigniew Bodek  **********************************************************************/
18219b8d05b8SZbigniew Bodek 
18229b8d05b8SZbigniew Bodek /**
18239b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
18249b8d05b8SZbigniew Bodek  * @arg: interrupt number
18259b8d05b8SZbigniew Bodek  **/
18269b8d05b8SZbigniew Bodek static void
18279b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
18289b8d05b8SZbigniew Bodek {
18299b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
18309b8d05b8SZbigniew Bodek 
18319b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
1832fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter)))
18339b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
18349b8d05b8SZbigniew Bodek }
18359b8d05b8SZbigniew Bodek 
18369b8d05b8SZbigniew Bodek static void
18375cb9db07SMarcin Wojtas ena_cleanup(void *arg, int pending)
18389b8d05b8SZbigniew Bodek {
18399b8d05b8SZbigniew Bodek 	struct ena_que	*que = arg;
18409b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = que->adapter;
18419b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
18429b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
18439b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
18449b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
18459b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
18469b8d05b8SZbigniew Bodek 	int qid, ena_qid;
18479b8d05b8SZbigniew Bodek 	int txc, rxc, i;
18489b8d05b8SZbigniew Bodek 
18493f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
18509b8d05b8SZbigniew Bodek 		return;
18519b8d05b8SZbigniew Bodek 
185230425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "MSI-X TX/RX routine\n");
18539b8d05b8SZbigniew Bodek 
18549b8d05b8SZbigniew Bodek 	tx_ring = que->tx_ring;
18559b8d05b8SZbigniew Bodek 	rx_ring = que->rx_ring;
18569b8d05b8SZbigniew Bodek 	qid = que->id;
18579b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(qid);
18589b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
18599b8d05b8SZbigniew Bodek 
1860d12f7bfcSMarcin Wojtas 	tx_ring->first_interrupt = true;
1861d12f7bfcSMarcin Wojtas 	rx_ring->first_interrupt = true;
1862d12f7bfcSMarcin Wojtas 
18639b8d05b8SZbigniew Bodek 	for (i = 0; i < CLEAN_BUDGET; ++i) {
18649b8d05b8SZbigniew Bodek 		rxc = ena_rx_cleanup(rx_ring);
18659b8d05b8SZbigniew Bodek 		txc = ena_tx_cleanup(tx_ring);
18669b8d05b8SZbigniew Bodek 
18673f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
18689b8d05b8SZbigniew Bodek 			return;
18699b8d05b8SZbigniew Bodek 
18700bdffe59SMarcin Wojtas 		if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
18719b8d05b8SZbigniew Bodek 		       break;
18729b8d05b8SZbigniew Bodek 	}
18739b8d05b8SZbigniew Bodek 
18749b8d05b8SZbigniew Bodek 	/* Signal that work is done and unmask interrupt */
18759b8d05b8SZbigniew Bodek 	ena_com_update_intr_reg(&intr_reg,
18769b8d05b8SZbigniew Bodek 	    RX_IRQ_INTERVAL,
18779b8d05b8SZbigniew Bodek 	    TX_IRQ_INTERVAL,
18789b8d05b8SZbigniew Bodek 	    true);
18799b8d05b8SZbigniew Bodek 	ena_com_unmask_intr(io_cq, &intr_reg);
18809b8d05b8SZbigniew Bodek }
18819b8d05b8SZbigniew Bodek 
18825cb9db07SMarcin Wojtas /**
18835cb9db07SMarcin Wojtas  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
18845cb9db07SMarcin Wojtas  * @arg: queue
18855cb9db07SMarcin Wojtas  **/
18865cb9db07SMarcin Wojtas static int
18875cb9db07SMarcin Wojtas ena_handle_msix(void *arg)
18885cb9db07SMarcin Wojtas {
18895cb9db07SMarcin Wojtas 	struct ena_que *queue = arg;
18905cb9db07SMarcin Wojtas 	struct ena_adapter *adapter = queue->adapter;
18915cb9db07SMarcin Wojtas 	if_t ifp = adapter->ifp;
18925cb9db07SMarcin Wojtas 
18935cb9db07SMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
18945cb9db07SMarcin Wojtas 		return (FILTER_STRAY);
18955cb9db07SMarcin Wojtas 
18965cb9db07SMarcin Wojtas 	taskqueue_enqueue(queue->cleanup_tq, &queue->cleanup_task);
18975cb9db07SMarcin Wojtas 
18985cb9db07SMarcin Wojtas 	return (FILTER_HANDLED);
18995cb9db07SMarcin Wojtas }
19005cb9db07SMarcin Wojtas 
19019b8d05b8SZbigniew Bodek static int
19029b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
19039b8d05b8SZbigniew Bodek {
19049b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
19058805021aSMarcin Wojtas 	int msix_vecs, msix_req;
19068805021aSMarcin Wojtas 	int i, rc = 0;
19079b8d05b8SZbigniew Bodek 
1908fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
1909fd43fd2aSMarcin Wojtas 		device_printf(dev, "Error, MSI-X is already enabled\n");
1910fd43fd2aSMarcin Wojtas 		return (EINVAL);
1911fd43fd2aSMarcin Wojtas 	}
1912fd43fd2aSMarcin Wojtas 
19139b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
19149b8d05b8SZbigniew Bodek 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues);
19159b8d05b8SZbigniew Bodek 
1916cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1917cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1918cd5d5804SMarcin Wojtas 
191930425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d\n", msix_vecs);
19209b8d05b8SZbigniew Bodek 
19219b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
19229b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
19239b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
19249b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
19259b8d05b8SZbigniew Bodek 	}
19269b8d05b8SZbigniew Bodek 
19278805021aSMarcin Wojtas 	msix_req = msix_vecs;
19289b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
19293f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
19309b8d05b8SZbigniew Bodek 		device_printf(dev,
19319b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
19327d2544e6SMarcin Wojtas 
19339b8d05b8SZbigniew Bodek 		rc = ENOSPC;
19347d2544e6SMarcin Wojtas 		goto err_msix_free;
19359b8d05b8SZbigniew Bodek 	}
19369b8d05b8SZbigniew Bodek 
19378805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
19382b5b60feSMarcin Wojtas 		if (msix_vecs == ENA_ADMIN_MSIX_VEC) {
19392b5b60feSMarcin Wojtas 			device_printf(dev,
19402b5b60feSMarcin Wojtas 			    "Not enough number of MSI-x allocated: %d\n",
19412b5b60feSMarcin Wojtas 			    msix_vecs);
19422b5b60feSMarcin Wojtas 			pci_release_msi(dev);
19432b5b60feSMarcin Wojtas 			rc = ENOSPC;
19442b5b60feSMarcin Wojtas 			goto err_msix_free;
19452b5b60feSMarcin Wojtas 		}
19468805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
19478805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
19488805021aSMarcin Wojtas 		adapter->num_queues = msix_vecs - ENA_ADMIN_MSIX_VEC;
19498805021aSMarcin Wojtas 	}
19508805021aSMarcin Wojtas 
19519b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
1952fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
19539b8d05b8SZbigniew Bodek 
19547d2544e6SMarcin Wojtas 	return (0);
19557d2544e6SMarcin Wojtas 
19567d2544e6SMarcin Wojtas err_msix_free:
19577d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
19587d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
19597d2544e6SMarcin Wojtas 
19609b8d05b8SZbigniew Bodek 	return (rc);
19619b8d05b8SZbigniew Bodek }
19629b8d05b8SZbigniew Bodek 
19639b8d05b8SZbigniew Bodek static void
19649b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
19659b8d05b8SZbigniew Bodek {
19669b8d05b8SZbigniew Bodek 
19679b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
19689b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
19699b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
19709b8d05b8SZbigniew Bodek 	/*
19719b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
19729b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
19739b8d05b8SZbigniew Bodek 	 */
19749b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
19759b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
19769b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
19779b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
19789b8d05b8SZbigniew Bodek }
19799b8d05b8SZbigniew Bodek 
198077958fcdSMarcin Wojtas static int
19819b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
19829b8d05b8SZbigniew Bodek {
19839b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
19849b8d05b8SZbigniew Bodek 	int irq_idx;
19859b8d05b8SZbigniew Bodek 
198677958fcdSMarcin Wojtas 	if (adapter->msix_entries == NULL)
198777958fcdSMarcin Wojtas 		return (EINVAL);
198877958fcdSMarcin Wojtas 
19899b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++) {
19909b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
19919b8d05b8SZbigniew Bodek 
19929b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
19939b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
19949b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
19959b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
19969b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
19979b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
19989b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
19999b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
2000277f11c4SMarcin Wojtas 
20019b8d05b8SZbigniew Bodek 		/*
2002277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
20039b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
20049b8d05b8SZbigniew Bodek 		 */
20053f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
20069b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
20079b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
20089b8d05b8SZbigniew Bodek 		    last_bind_cpu;
20099b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
20109b8d05b8SZbigniew Bodek 	}
201177958fcdSMarcin Wojtas 
201277958fcdSMarcin Wojtas 	return (0);
20139b8d05b8SZbigniew Bodek }
20149b8d05b8SZbigniew Bodek 
20159b8d05b8SZbigniew Bodek static int
20169b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
20179b8d05b8SZbigniew Bodek {
20189b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20199b8d05b8SZbigniew Bodek 	unsigned long flags;
20209b8d05b8SZbigniew Bodek 	int rc, rcc;
20219b8d05b8SZbigniew Bodek 
20229b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
20239b8d05b8SZbigniew Bodek 
20249b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
20259b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
20269b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
20279b8d05b8SZbigniew Bodek 
20283f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
20299b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
20309b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
20317d2544e6SMarcin Wojtas 		return (ENXIO);
20329b8d05b8SZbigniew Bodek 	}
20339b8d05b8SZbigniew Bodek 
20340bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
20350bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
20360bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
20373f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
20389b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
20399b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
20409b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
20417d2544e6SMarcin Wojtas 		goto err_res_free;
20429b8d05b8SZbigniew Bodek 	}
20439b8d05b8SZbigniew Bodek 	irq->requested = true;
20449b8d05b8SZbigniew Bodek 
20459b8d05b8SZbigniew Bodek 	return (rc);
20469b8d05b8SZbigniew Bodek 
20477d2544e6SMarcin Wojtas err_res_free:
20484e8acd84SMarcin Wojtas 	ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
20497d2544e6SMarcin Wojtas 	    irq->vector);
20509b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20519b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
20523f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
20539b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
20549b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
20559b8d05b8SZbigniew Bodek 	irq->res = NULL;
20569b8d05b8SZbigniew Bodek 
20579b8d05b8SZbigniew Bodek 	return (rc);
20589b8d05b8SZbigniew Bodek }
20599b8d05b8SZbigniew Bodek 
20609b8d05b8SZbigniew Bodek static int
20619b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
20629b8d05b8SZbigniew Bodek {
20639b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20649b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
20659b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
20669b8d05b8SZbigniew Bodek 
2067fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter))) {
20684e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
20694e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
20709b8d05b8SZbigniew Bodek 		return (EINVAL);
20719b8d05b8SZbigniew Bodek 	} else {
20729b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
20739b8d05b8SZbigniew Bodek 	}
20749b8d05b8SZbigniew Bodek 
20759b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
20769b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20779b8d05b8SZbigniew Bodek 
20783f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
20799b8d05b8SZbigniew Bodek 			continue;
20809b8d05b8SZbigniew Bodek 
20819b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
20829b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
20833f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
2084469a8407SMarcin Wojtas 			rc = ENOMEM;
20859b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
20869b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
20879b8d05b8SZbigniew Bodek 			goto err;
20889b8d05b8SZbigniew Bodek 		}
20899b8d05b8SZbigniew Bodek 
20900bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
20915cb9db07SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, irq->handler, NULL,
20925cb9db07SMarcin Wojtas 		    irq->data, &irq->cookie);
20933f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
20949b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
20959b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
20969b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
20979b8d05b8SZbigniew Bodek 			goto err;
20989b8d05b8SZbigniew Bodek 		}
20999b8d05b8SZbigniew Bodek 		irq->requested = true;
21009b8d05b8SZbigniew Bodek 
21014e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
21029b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
21039b8d05b8SZbigniew Bodek 	}
21049b8d05b8SZbigniew Bodek 
21059b8d05b8SZbigniew Bodek 	return (rc);
21069b8d05b8SZbigniew Bodek 
21079b8d05b8SZbigniew Bodek err:
21089b8d05b8SZbigniew Bodek 
21099b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
21109b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
21119b8d05b8SZbigniew Bodek 		rcc = 0;
21129b8d05b8SZbigniew Bodek 
21139b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
21149b8d05b8SZbigniew Bodek 		   free both intr and resources */
21150bdffe59SMarcin Wojtas 		if (irq->requested)
21169b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
21173f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
21189b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
21199b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
21209b8d05b8SZbigniew Bodek 
21219b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
21229b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
21239b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
21249b8d05b8SZbigniew Bodek 		   this iteration */
21259b8d05b8SZbigniew Bodek 		rcc = 0;
21269b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
21279b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
21289b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
21299b8d05b8SZbigniew Bodek 		}
21303f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
21319b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
21329b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
21339b8d05b8SZbigniew Bodek 		irq->requested = false;
21349b8d05b8SZbigniew Bodek 		irq->res = NULL;
21359b8d05b8SZbigniew Bodek 	}
21369b8d05b8SZbigniew Bodek 
21379b8d05b8SZbigniew Bodek 	return (rc);
21389b8d05b8SZbigniew Bodek }
21399b8d05b8SZbigniew Bodek 
21409b8d05b8SZbigniew Bodek static void
21419b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
21429b8d05b8SZbigniew Bodek {
21439b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
21449b8d05b8SZbigniew Bodek 	int rc;
21459b8d05b8SZbigniew Bodek 
21469b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
21479b8d05b8SZbigniew Bodek 	if (irq->requested) {
21489b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
21499b8d05b8SZbigniew Bodek 		    irq->vector);
21509b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
21513f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
21529b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
21539b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
21549b8d05b8SZbigniew Bodek 		irq->requested = 0;
21559b8d05b8SZbigniew Bodek 	}
21569b8d05b8SZbigniew Bodek 
21579b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
21589b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
21599b8d05b8SZbigniew Bodek 		    irq->vector);
21609b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
21619b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
21629b8d05b8SZbigniew Bodek 		irq->res = NULL;
21633f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
21649b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
21659b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
21669b8d05b8SZbigniew Bodek 	}
21679b8d05b8SZbigniew Bodek }
21689b8d05b8SZbigniew Bodek 
21699b8d05b8SZbigniew Bodek static void
21709b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
21719b8d05b8SZbigniew Bodek {
21729b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
21739b8d05b8SZbigniew Bodek 	int rc;
21749b8d05b8SZbigniew Bodek 
21759b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
21769b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
21779b8d05b8SZbigniew Bodek 		if (irq->requested) {
21789b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
21799b8d05b8SZbigniew Bodek 			    irq->vector);
21809b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
21819b8d05b8SZbigniew Bodek 			    irq->cookie);
21823f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
21839b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
21849b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
21859b8d05b8SZbigniew Bodek 			}
21869b8d05b8SZbigniew Bodek 			irq->requested = 0;
21879b8d05b8SZbigniew Bodek 		}
21889b8d05b8SZbigniew Bodek 
21899b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
21909b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
21919b8d05b8SZbigniew Bodek 			    irq->vector);
21929b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
21939b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
21949b8d05b8SZbigniew Bodek 			irq->res = NULL;
21953f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
21969b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
21979b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
21989b8d05b8SZbigniew Bodek 				    irq->vector);
21999b8d05b8SZbigniew Bodek 			}
22009b8d05b8SZbigniew Bodek 		}
22019b8d05b8SZbigniew Bodek 	}
22029b8d05b8SZbigniew Bodek }
22039b8d05b8SZbigniew Bodek 
22049b8d05b8SZbigniew Bodek static void
22059b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
22069b8d05b8SZbigniew Bodek {
22079b8d05b8SZbigniew Bodek 
22089b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
22099b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
22109b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
22119b8d05b8SZbigniew Bodek }
22129b8d05b8SZbigniew Bodek 
22139b8d05b8SZbigniew Bodek static void
22149b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
22159b8d05b8SZbigniew Bodek {
22169b8d05b8SZbigniew Bodek 
2217fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_MSIX_ENABLED, adapter)) {
2218fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_MSIX_ENABLED, adapter);
22199b8d05b8SZbigniew Bodek 		pci_release_msi(adapter->pdev);
2220fd43fd2aSMarcin Wojtas 	}
22219b8d05b8SZbigniew Bodek 
22229b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
2223fd43fd2aSMarcin Wojtas 	if (adapter->msix_entries != NULL)
2224cd5d5804SMarcin Wojtas 		free(adapter->msix_entries, M_DEVBUF);
22259b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
22269b8d05b8SZbigniew Bodek }
22279b8d05b8SZbigniew Bodek 
22289b8d05b8SZbigniew Bodek static void
22299b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
22309b8d05b8SZbigniew Bodek {
22319b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
22329b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
22339b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
22349b8d05b8SZbigniew Bodek 	int i;
22359b8d05b8SZbigniew Bodek 
22369b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
22379b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
22389b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
22399b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
22409b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
22419b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
22429b8d05b8SZbigniew Bodek 	}
22439b8d05b8SZbigniew Bodek }
22449b8d05b8SZbigniew Bodek 
22459b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
22460bdffe59SMarcin Wojtas static int
22470bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
22489b8d05b8SZbigniew Bodek {
22499b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
22509b8d05b8SZbigniew Bodek 	int rc;
22519b8d05b8SZbigniew Bodek 
22529b8d05b8SZbigniew Bodek 	/* Set indirect table */
22539b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
22540bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
22550bdffe59SMarcin Wojtas 		return (rc);
22569b8d05b8SZbigniew Bodek 
22579b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
22589b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
22590bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
22600bdffe59SMarcin Wojtas 		return (rc);
22619b8d05b8SZbigniew Bodek 
22629b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
22639b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
22640bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
22650bdffe59SMarcin Wojtas 		return (rc);
22669b8d05b8SZbigniew Bodek 
22670bdffe59SMarcin Wojtas 	return (0);
22689b8d05b8SZbigniew Bodek }
22699b8d05b8SZbigniew Bodek 
22709b8d05b8SZbigniew Bodek static int
22719b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
22729b8d05b8SZbigniew Bodek {
22739b8d05b8SZbigniew Bodek 	int rc;
22749b8d05b8SZbigniew Bodek 
2275fd43fd2aSMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter))) {
22769b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
22770bdffe59SMarcin Wojtas 		if (rc != 0)
22789b8d05b8SZbigniew Bodek 			return (rc);
22799b8d05b8SZbigniew Bodek 	}
22809b8d05b8SZbigniew Bodek 
22817d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
22823f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
22837d2544e6SMarcin Wojtas 		return (rc);
22847d2544e6SMarcin Wojtas 
22859b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
228630217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
228730217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
22889b8d05b8SZbigniew Bodek 
22899b8d05b8SZbigniew Bodek 	return (0);
22909b8d05b8SZbigniew Bodek }
22919b8d05b8SZbigniew Bodek 
22929b8d05b8SZbigniew Bodek static int
22939b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
22949b8d05b8SZbigniew Bodek {
22959b8d05b8SZbigniew Bodek 	int rc = 0;
22969b8d05b8SZbigniew Bodek 
22973f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
22989b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
22999b8d05b8SZbigniew Bodek 		return (ENXIO);
23009b8d05b8SZbigniew Bodek 	}
23019b8d05b8SZbigniew Bodek 
2302fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
23039b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going UP\n");
23049b8d05b8SZbigniew Bodek 
23059b8d05b8SZbigniew Bodek 		/* setup interrupts for IO queues */
230677958fcdSMarcin Wojtas 		rc = ena_setup_io_intr(adapter);
230777958fcdSMarcin Wojtas 		if (unlikely(rc != 0)) {
230877958fcdSMarcin Wojtas 			ena_trace(ENA_ALERT, "error setting up IO interrupt\n");
230977958fcdSMarcin Wojtas 			goto error;
231077958fcdSMarcin Wojtas 		}
23119b8d05b8SZbigniew Bodek 		rc = ena_request_io_irq(adapter);
23123f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
231330425f93SMarcin Wojtas 			ena_trace(ENA_ALERT, "err_req_irq\n");
231477958fcdSMarcin Wojtas 			goto error;
23159b8d05b8SZbigniew Bodek 		}
23169b8d05b8SZbigniew Bodek 
23179b8d05b8SZbigniew Bodek 		/* allocate transmit descriptors */
23189b8d05b8SZbigniew Bodek 		rc = ena_setup_all_tx_resources(adapter);
23193f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
232030425f93SMarcin Wojtas 			ena_trace(ENA_ALERT, "err_setup_tx\n");
23219b8d05b8SZbigniew Bodek 			goto err_setup_tx;
23229b8d05b8SZbigniew Bodek 		}
23239b8d05b8SZbigniew Bodek 
23249b8d05b8SZbigniew Bodek 		/* allocate receive descriptors */
23259b8d05b8SZbigniew Bodek 		rc = ena_setup_all_rx_resources(adapter);
23263f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
232730425f93SMarcin Wojtas 			ena_trace(ENA_ALERT, "err_setup_rx\n");
23289b8d05b8SZbigniew Bodek 			goto err_setup_rx;
23299b8d05b8SZbigniew Bodek 		}
23309b8d05b8SZbigniew Bodek 
23319b8d05b8SZbigniew Bodek 		/* create IO queues for Rx & Tx */
23329b8d05b8SZbigniew Bodek 		rc = ena_create_io_queues(adapter);
23333f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
23349b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT,
233530425f93SMarcin Wojtas 			    "create IO queues failed\n");
23369b8d05b8SZbigniew Bodek 			goto err_io_que;
23379b8d05b8SZbigniew Bodek 		}
23389b8d05b8SZbigniew Bodek 
2339fd43fd2aSMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
23409b8d05b8SZbigniew Bodek 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
23419b8d05b8SZbigniew Bodek 
23429b8d05b8SZbigniew Bodek 		rc = ena_up_complete(adapter);
23433f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
23449b8d05b8SZbigniew Bodek 			goto err_up_complete;
23459b8d05b8SZbigniew Bodek 
23469b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_up, 1);
23479b8d05b8SZbigniew Bodek 
23489b8d05b8SZbigniew Bodek 		ena_update_hwassist(adapter);
23499b8d05b8SZbigniew Bodek 
23509b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
23519b8d05b8SZbigniew Bodek 		    IFF_DRV_OACTIVE);
23529b8d05b8SZbigniew Bodek 
23539b8d05b8SZbigniew Bodek 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
23549b8d05b8SZbigniew Bodek 		    ena_timer_service, (void *)adapter, 0);
23559b8d05b8SZbigniew Bodek 
2356fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP, adapter);
235793471047SZbigniew Bodek 
235893471047SZbigniew Bodek 		ena_unmask_all_io_irqs(adapter);
23599b8d05b8SZbigniew Bodek 	}
23609b8d05b8SZbigniew Bodek 
23619b8d05b8SZbigniew Bodek 	return (0);
23629b8d05b8SZbigniew Bodek 
23639b8d05b8SZbigniew Bodek err_up_complete:
23649b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
23659b8d05b8SZbigniew Bodek err_io_que:
23669b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
23679b8d05b8SZbigniew Bodek err_setup_rx:
23689b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
23699b8d05b8SZbigniew Bodek err_setup_tx:
23709b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
237177958fcdSMarcin Wojtas error:
23729b8d05b8SZbigniew Bodek 	return (rc);
23739b8d05b8SZbigniew Bodek }
23749b8d05b8SZbigniew Bodek 
23759b8d05b8SZbigniew Bodek static uint64_t
23769b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
23779b8d05b8SZbigniew Bodek {
23789b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
23799b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
23809b8d05b8SZbigniew Bodek 
23819b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
23829b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
23839b8d05b8SZbigniew Bodek 
23849b8d05b8SZbigniew Bodek 	switch (cnt) {
23859b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
238630217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
23879b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
238830217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
23899b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
239030217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
23919b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
239230217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
23939b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
239430217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
23959b8d05b8SZbigniew Bodek 	default:
23969b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
23979b8d05b8SZbigniew Bodek 	}
23989b8d05b8SZbigniew Bodek }
23999b8d05b8SZbigniew Bodek 
24009b8d05b8SZbigniew Bodek static int
24019b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
24029b8d05b8SZbigniew Bodek {
24039b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
24049b8d05b8SZbigniew Bodek 	return (0);
24059b8d05b8SZbigniew Bodek }
24069b8d05b8SZbigniew Bodek 
24079b8d05b8SZbigniew Bodek static void
24089b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
24099b8d05b8SZbigniew Bodek {
24109b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
241130425f93SMarcin Wojtas 	ena_trace(ENA_DBG, "enter\n");
24129b8d05b8SZbigniew Bodek 
24135a990212SMarcin Wojtas 	mtx_lock(&adapter->global_mtx);
24149b8d05b8SZbigniew Bodek 
24159b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
24169b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
24179b8d05b8SZbigniew Bodek 
2418fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)) {
24195a990212SMarcin Wojtas 		mtx_unlock(&adapter->global_mtx);
2420fd43fd2aSMarcin Wojtas 		ena_trace(ENA_INFO, "Link is down\n");
24219b8d05b8SZbigniew Bodek 		return;
24229b8d05b8SZbigniew Bodek 	}
24239b8d05b8SZbigniew Bodek 
24249b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
2425b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
24269b8d05b8SZbigniew Bodek 
24275a990212SMarcin Wojtas 	mtx_unlock(&adapter->global_mtx);
24289b8d05b8SZbigniew Bodek }
24299b8d05b8SZbigniew Bodek 
24309b8d05b8SZbigniew Bodek static void
24319b8d05b8SZbigniew Bodek ena_init(void *arg)
24329b8d05b8SZbigniew Bodek {
24339b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
24349b8d05b8SZbigniew Bodek 
2435fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
24363d3a90f9SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
24379b8d05b8SZbigniew Bodek 		ena_up(adapter);
24383d3a90f9SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
24393d3a90f9SZbigniew Bodek 	}
24409b8d05b8SZbigniew Bodek }
24419b8d05b8SZbigniew Bodek 
24429b8d05b8SZbigniew Bodek static int
24439b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
24449b8d05b8SZbigniew Bodek {
24459b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
24469b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
24479b8d05b8SZbigniew Bodek 	int rc;
24489b8d05b8SZbigniew Bodek 
24499b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
24509b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
24519b8d05b8SZbigniew Bodek 
24529b8d05b8SZbigniew Bodek 	/*
24539b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
24549b8d05b8SZbigniew Bodek 	 */
24559b8d05b8SZbigniew Bodek 	rc = 0;
24569b8d05b8SZbigniew Bodek 	switch (command) {
24579b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2458dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
2459dbf2eb54SMarcin Wojtas 			break;
2460e67c6554SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
24619b8d05b8SZbigniew Bodek 		ena_down(adapter);
24629b8d05b8SZbigniew Bodek 
24639b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
24649b8d05b8SZbigniew Bodek 
24659b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
2466e67c6554SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
24679b8d05b8SZbigniew Bodek 		break;
24689b8d05b8SZbigniew Bodek 
24699b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
24700bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
24710bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
24720bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
24730bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
24749b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
24759b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
24769b8d05b8SZbigniew Bodek 				}
24779b8d05b8SZbigniew Bodek 			} else {
2478e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24799b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2480e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
24819b8d05b8SZbigniew Bodek 			}
24829b8d05b8SZbigniew Bodek 		} else {
24830bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2484e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24859b8d05b8SZbigniew Bodek 				ena_down(adapter);
2486e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
2487e67c6554SZbigniew Bodek 			}
24889b8d05b8SZbigniew Bodek 		}
24899b8d05b8SZbigniew Bodek 		break;
24909b8d05b8SZbigniew Bodek 
24919b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
24929b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
24939b8d05b8SZbigniew Bodek 		break;
24949b8d05b8SZbigniew Bodek 
24959b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
24969b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
24979b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
24989b8d05b8SZbigniew Bodek 		break;
24999b8d05b8SZbigniew Bodek 
25009b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
25019b8d05b8SZbigniew Bodek 		{
25029b8d05b8SZbigniew Bodek 			int reinit = 0;
25039b8d05b8SZbigniew Bodek 
25049b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
25059b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
25069b8d05b8SZbigniew Bodek 				reinit = 1;
25079b8d05b8SZbigniew Bodek 			}
25089b8d05b8SZbigniew Bodek 
25090bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
25100bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
2511e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
25129b8d05b8SZbigniew Bodek 				ena_down(adapter);
25139b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2514e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
25159b8d05b8SZbigniew Bodek 			}
25169b8d05b8SZbigniew Bodek 		}
25179b8d05b8SZbigniew Bodek 
25189b8d05b8SZbigniew Bodek 		break;
25199b8d05b8SZbigniew Bodek 	default:
25209b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
25219b8d05b8SZbigniew Bodek 		break;
25229b8d05b8SZbigniew Bodek 	}
25239b8d05b8SZbigniew Bodek 
25249b8d05b8SZbigniew Bodek 	return (rc);
25259b8d05b8SZbigniew Bodek }
25269b8d05b8SZbigniew Bodek 
25279b8d05b8SZbigniew Bodek static int
25289b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
25299b8d05b8SZbigniew Bodek {
25309b8d05b8SZbigniew Bodek 	int caps = 0;
25319b8d05b8SZbigniew Bodek 
25320bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
25339b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
25349b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
25350bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
25369b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
25379b8d05b8SZbigniew Bodek 
25380bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
25399b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
25400bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
25419b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
25429b8d05b8SZbigniew Bodek 
25430bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
25440bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
25459b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
25469b8d05b8SZbigniew Bodek 
25470bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
25480bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
25499b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
25509b8d05b8SZbigniew Bodek 
25510bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
25529b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
25530bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
25549b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
25559b8d05b8SZbigniew Bodek 
25560bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
25570bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
25589b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
25599b8d05b8SZbigniew Bodek 
25609b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
25619b8d05b8SZbigniew Bodek 
25629b8d05b8SZbigniew Bodek 	return (caps);
25639b8d05b8SZbigniew Bodek }
25649b8d05b8SZbigniew Bodek 
25659b8d05b8SZbigniew Bodek static void
25669b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
25679b8d05b8SZbigniew Bodek {
25689b8d05b8SZbigniew Bodek 
25699b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
25709b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
25719b8d05b8SZbigniew Bodek }
25729b8d05b8SZbigniew Bodek 
25739b8d05b8SZbigniew Bodek static void
25749b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
25759b8d05b8SZbigniew Bodek {
25769b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
25779b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
25789b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
25799b8d05b8SZbigniew Bodek 	int flags = 0;
25809b8d05b8SZbigniew Bodek 
25819b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
25829b8d05b8SZbigniew Bodek 
25830bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
25840bdffe59SMarcin Wojtas 		if ((feat &
25850bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
25869b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
25870bdffe59SMarcin Wojtas 		if ((feat &
25889b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
25890bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
25909b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
25919b8d05b8SZbigniew Bodek 	}
25929b8d05b8SZbigniew Bodek 
25930bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
25949b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
25959b8d05b8SZbigniew Bodek 
25960bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
25979b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
25989b8d05b8SZbigniew Bodek 
25990bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
26009b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
26019b8d05b8SZbigniew Bodek 
26029b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
26039b8d05b8SZbigniew Bodek }
26049b8d05b8SZbigniew Bodek 
26059b8d05b8SZbigniew Bodek static int
26069b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
26079b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
26089b8d05b8SZbigniew Bodek {
26099b8d05b8SZbigniew Bodek 	if_t ifp;
26109b8d05b8SZbigniew Bodek 	int caps = 0;
26119b8d05b8SZbigniew Bodek 
26129b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
26133f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
26144e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
26159b8d05b8SZbigniew Bodek 		return (ENXIO);
26169b8d05b8SZbigniew Bodek 	}
26179b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
26189b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
26199b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
26209b8d05b8SZbigniew Bodek 
26219b8d05b8SZbigniew Bodek 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
26229b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
26239b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
26249b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
26259b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
26269b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
26279b8d05b8SZbigniew Bodek 
26289b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
26299b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
26309b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
26319b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
26329b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
26339b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
26349b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
26359b8d05b8SZbigniew Bodek 	/* check hardware support */
26369b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
26379b8d05b8SZbigniew Bodek 	/* ... and set them */
26389b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
26399b8d05b8SZbigniew Bodek 
26409b8d05b8SZbigniew Bodek 	/* TSO parameters */
26418a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
26428a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
26438a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
26448a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
26459b8d05b8SZbigniew Bodek 
26469b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
26479b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
26489b8d05b8SZbigniew Bodek 
26499b8d05b8SZbigniew Bodek 	/*
26509b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
26519b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
26529b8d05b8SZbigniew Bodek 	 */
26539b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
26549b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
26559b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
26569b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
26579b8d05b8SZbigniew Bodek 
26589b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
26599b8d05b8SZbigniew Bodek 
26609b8d05b8SZbigniew Bodek 	return (0);
26619b8d05b8SZbigniew Bodek }
26629b8d05b8SZbigniew Bodek 
26639b8d05b8SZbigniew Bodek static void
26649b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
26659b8d05b8SZbigniew Bodek {
2666a195fab0SMarcin Wojtas 	int rc;
26679b8d05b8SZbigniew Bodek 
2668fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter)) {
26699b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going DOWN\n");
26709b8d05b8SZbigniew Bodek 
26719b8d05b8SZbigniew Bodek 		callout_drain(&adapter->timer_service);
26729b8d05b8SZbigniew Bodek 
2673fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP, adapter);
26749b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
26759b8d05b8SZbigniew Bodek 		    IFF_DRV_RUNNING);
26769b8d05b8SZbigniew Bodek 
26779b8d05b8SZbigniew Bodek 		ena_free_io_irq(adapter);
26789b8d05b8SZbigniew Bodek 
2679fd43fd2aSMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter)) {
2680a195fab0SMarcin Wojtas 			rc = ena_com_dev_reset(adapter->ena_dev,
2681a195fab0SMarcin Wojtas 			    adapter->reset_reason);
26823f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0))
2683a195fab0SMarcin Wojtas 				device_printf(adapter->pdev,
2684a195fab0SMarcin Wojtas 				    "Device reset failed\n");
2685a195fab0SMarcin Wojtas 		}
2686a195fab0SMarcin Wojtas 
26879b8d05b8SZbigniew Bodek 		ena_destroy_all_io_queues(adapter);
26889b8d05b8SZbigniew Bodek 
26899b8d05b8SZbigniew Bodek 		ena_free_all_tx_bufs(adapter);
26909b8d05b8SZbigniew Bodek 		ena_free_all_rx_bufs(adapter);
26919b8d05b8SZbigniew Bodek 		ena_free_all_tx_resources(adapter);
26929b8d05b8SZbigniew Bodek 		ena_free_all_rx_resources(adapter);
26939b8d05b8SZbigniew Bodek 
26949b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_down, 1);
26959b8d05b8SZbigniew Bodek 	}
26969b8d05b8SZbigniew Bodek }
26979b8d05b8SZbigniew Bodek 
26989b8d05b8SZbigniew Bodek static void
26999b8d05b8SZbigniew Bodek ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
27009b8d05b8SZbigniew Bodek {
27019b8d05b8SZbigniew Bodek 	struct ena_com_tx_meta *ena_meta;
27029b8d05b8SZbigniew Bodek 	struct ether_vlan_header *eh;
2703a870eab2SMarcin Wojtas 	struct mbuf *mbuf_next;
27049b8d05b8SZbigniew Bodek 	u32 mss;
27059b8d05b8SZbigniew Bodek 	bool offload;
27069b8d05b8SZbigniew Bodek 	uint16_t etype;
27079b8d05b8SZbigniew Bodek 	int ehdrlen;
27089b8d05b8SZbigniew Bodek 	struct ip *ip;
27099b8d05b8SZbigniew Bodek 	int iphlen;
27109b8d05b8SZbigniew Bodek 	struct tcphdr *th;
2711a870eab2SMarcin Wojtas 	int offset;
27129b8d05b8SZbigniew Bodek 
27139b8d05b8SZbigniew Bodek 	offload = false;
27149b8d05b8SZbigniew Bodek 	ena_meta = &ena_tx_ctx->ena_meta;
27159b8d05b8SZbigniew Bodek 	mss = mbuf->m_pkthdr.tso_segsz;
27169b8d05b8SZbigniew Bodek 
27179b8d05b8SZbigniew Bodek 	if (mss != 0)
27189b8d05b8SZbigniew Bodek 		offload = true;
27199b8d05b8SZbigniew Bodek 
27209b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0)
27219b8d05b8SZbigniew Bodek 		offload = true;
27229b8d05b8SZbigniew Bodek 
27239b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
27249b8d05b8SZbigniew Bodek 		offload = true;
27259b8d05b8SZbigniew Bodek 
27260bdffe59SMarcin Wojtas 	if (!offload) {
27279b8d05b8SZbigniew Bodek 		ena_tx_ctx->meta_valid = 0;
27289b8d05b8SZbigniew Bodek 		return;
27299b8d05b8SZbigniew Bodek 	}
27309b8d05b8SZbigniew Bodek 
27319b8d05b8SZbigniew Bodek 	/* Determine where frame payload starts. */
27329b8d05b8SZbigniew Bodek 	eh = mtod(mbuf, struct ether_vlan_header *);
27339b8d05b8SZbigniew Bodek 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
27349b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_proto);
27359b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
27369b8d05b8SZbigniew Bodek 	} else {
27379b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_encap_proto);
27389b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN;
27399b8d05b8SZbigniew Bodek 	}
27409b8d05b8SZbigniew Bodek 
2741a870eab2SMarcin Wojtas 	mbuf_next = m_getptr(mbuf, ehdrlen, &offset);
2742a870eab2SMarcin Wojtas 	ip = (struct ip *)(mtodo(mbuf_next, offset));
27439b8d05b8SZbigniew Bodek 	iphlen = ip->ip_hl << 2;
2744a870eab2SMarcin Wojtas 
2745a870eab2SMarcin Wojtas 	mbuf_next = m_getptr(mbuf, iphlen + ehdrlen, &offset);
2746a870eab2SMarcin Wojtas 	th = (struct tcphdr *)(mtodo(mbuf_next, offset));
27479b8d05b8SZbigniew Bodek 
27489b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
27499b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_csum_enable = 1;
27509b8d05b8SZbigniew Bodek 	}
27519b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
27529b8d05b8SZbigniew Bodek 		ena_tx_ctx->tso_enable = 1;
27539b8d05b8SZbigniew Bodek 		ena_meta->l4_hdr_len = (th->th_off);
27549b8d05b8SZbigniew Bodek 	}
27559b8d05b8SZbigniew Bodek 
27569b8d05b8SZbigniew Bodek 	switch (etype) {
27579b8d05b8SZbigniew Bodek 	case ETHERTYPE_IP:
27589b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2759cd433385SMarcin Wojtas 		if ((ip->ip_off & htons(IP_DF)) != 0)
27609b8d05b8SZbigniew Bodek 			ena_tx_ctx->df = 1;
27619b8d05b8SZbigniew Bodek 		break;
27629b8d05b8SZbigniew Bodek 	case ETHERTYPE_IPV6:
27639b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
27649b8d05b8SZbigniew Bodek 
27659b8d05b8SZbigniew Bodek 	default:
27669b8d05b8SZbigniew Bodek 		break;
27679b8d05b8SZbigniew Bodek 	}
27689b8d05b8SZbigniew Bodek 
27699b8d05b8SZbigniew Bodek 	if (ip->ip_p == IPPROTO_TCP) {
27709b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
27710bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
27720bdffe59SMarcin Wojtas 		    (CSUM_IP_TCP | CSUM_IP6_TCP)) != 0)
27739b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
27749b8d05b8SZbigniew Bodek 		else
27759b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
27769b8d05b8SZbigniew Bodek 	} else if (ip->ip_p == IPPROTO_UDP) {
27779b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
27780bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
27790bdffe59SMarcin Wojtas 		    (CSUM_IP_UDP | CSUM_IP6_UDP)) != 0)
27809b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
27819b8d05b8SZbigniew Bodek 		else
27829b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
27839b8d05b8SZbigniew Bodek 	} else {
27849b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
27859b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_csum_enable = 0;
27869b8d05b8SZbigniew Bodek 	}
27879b8d05b8SZbigniew Bodek 
27889b8d05b8SZbigniew Bodek 	ena_meta->mss = mss;
27899b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_len = iphlen;
27909b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_offset = ehdrlen;
27919b8d05b8SZbigniew Bodek 	ena_tx_ctx->meta_valid = 1;
27929b8d05b8SZbigniew Bodek }
27939b8d05b8SZbigniew Bodek 
27949b8d05b8SZbigniew Bodek static int
27951b069f1cSZbigniew Bodek ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
27961e9fb899SZbigniew Bodek {
27971e9fb899SZbigniew Bodek 	struct ena_adapter *adapter;
27981b069f1cSZbigniew Bodek 	struct mbuf *collapsed_mbuf;
27991e9fb899SZbigniew Bodek 	int num_frags;
28001e9fb899SZbigniew Bodek 
28011e9fb899SZbigniew Bodek 	adapter = tx_ring->adapter;
28021e9fb899SZbigniew Bodek 	num_frags = ena_mbuf_count(*mbuf);
28031e9fb899SZbigniew Bodek 
28041e9fb899SZbigniew Bodek 	/* One segment must be reserved for configuration descriptor. */
28051e9fb899SZbigniew Bodek 	if (num_frags < adapter->max_tx_sgl_size)
28061e9fb899SZbigniew Bodek 		return (0);
28071b069f1cSZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
28081e9fb899SZbigniew Bodek 
28091b069f1cSZbigniew Bodek 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
28101b069f1cSZbigniew Bodek 	    adapter->max_tx_sgl_size - 1);
28113f9ed7abSMarcin Wojtas 	if (unlikely(collapsed_mbuf == NULL)) {
28121b069f1cSZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
28131e9fb899SZbigniew Bodek 		return (ENOMEM);
28141e9fb899SZbigniew Bodek 	}
28151e9fb899SZbigniew Bodek 
28161b069f1cSZbigniew Bodek 	/* If mbuf was collapsed succesfully, original mbuf is released. */
28171b069f1cSZbigniew Bodek 	*mbuf = collapsed_mbuf;
28181e9fb899SZbigniew Bodek 
28191e9fb899SZbigniew Bodek 	return (0);
28201e9fb899SZbigniew Bodek }
28211e9fb899SZbigniew Bodek 
28224fa9e02dSMarcin Wojtas static void
28234fa9e02dSMarcin Wojtas ena_dmamap_llq(void *arg, bus_dma_segment_t *segs, int nseg, int error)
28244fa9e02dSMarcin Wojtas {
28254fa9e02dSMarcin Wojtas 	struct ena_com_buf *ena_buf = arg;
28264fa9e02dSMarcin Wojtas 
28274fa9e02dSMarcin Wojtas 	if (unlikely(error != 0)) {
28284fa9e02dSMarcin Wojtas 		ena_buf->paddr = 0;
28294fa9e02dSMarcin Wojtas 		return;
28304fa9e02dSMarcin Wojtas 	}
28314fa9e02dSMarcin Wojtas 
28324fa9e02dSMarcin Wojtas 	KASSERT(nseg == 1, ("Invalid num of segments for LLQ dma"));
28334fa9e02dSMarcin Wojtas 
28344fa9e02dSMarcin Wojtas 	ena_buf->paddr = segs->ds_addr;
28354fa9e02dSMarcin Wojtas 	ena_buf->len = segs->ds_len;
28364fa9e02dSMarcin Wojtas }
28374fa9e02dSMarcin Wojtas 
28384fa9e02dSMarcin Wojtas static int
28394fa9e02dSMarcin Wojtas ena_tx_map_mbuf(struct ena_ring *tx_ring, struct ena_tx_buffer *tx_info,
28404fa9e02dSMarcin Wojtas     struct mbuf *mbuf, void **push_hdr, u16 *header_len)
28414fa9e02dSMarcin Wojtas {
28424fa9e02dSMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
28434fa9e02dSMarcin Wojtas 	struct ena_com_buf *ena_buf;
28444fa9e02dSMarcin Wojtas 	bus_dma_segment_t segs[ENA_BUS_DMA_SEGS];
28454fa9e02dSMarcin Wojtas 	uint32_t mbuf_head_len, frag_len;
28464fa9e02dSMarcin Wojtas 	uint16_t push_len = 0;
28474fa9e02dSMarcin Wojtas 	uint16_t delta = 0;
28484fa9e02dSMarcin Wojtas 	int i, rc, nsegs;
28494fa9e02dSMarcin Wojtas 
28504fa9e02dSMarcin Wojtas 	mbuf_head_len = mbuf->m_len;
28514fa9e02dSMarcin Wojtas 	tx_info->mbuf = mbuf;
28524fa9e02dSMarcin Wojtas 	ena_buf = tx_info->bufs;
28534fa9e02dSMarcin Wojtas 
28544fa9e02dSMarcin Wojtas 	if (tx_ring->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV) {
28554fa9e02dSMarcin Wojtas 		/*
28564fa9e02dSMarcin Wojtas 		 * When the device is LLQ mode, the driver will copy
28574fa9e02dSMarcin Wojtas 		 * the header into the device memory space.
28584fa9e02dSMarcin Wojtas 		 * the ena_com layer assumes the header is in a linear
28594fa9e02dSMarcin Wojtas 		 * memory space.
28604fa9e02dSMarcin Wojtas 		 * This assumption might be wrong since part of the header
28614fa9e02dSMarcin Wojtas 		 * can be in the fragmented buffers.
28624fa9e02dSMarcin Wojtas 		 * First check if header fits in the mbuf. If not, copy it to
28634fa9e02dSMarcin Wojtas 		 * separate buffer that will be holding linearized data.
28644fa9e02dSMarcin Wojtas 		 */
28654fa9e02dSMarcin Wojtas 		push_len = min_t(uint32_t, mbuf->m_pkthdr.len,
28664fa9e02dSMarcin Wojtas 		    tx_ring->tx_max_header_size);
28674fa9e02dSMarcin Wojtas 		*header_len = push_len;
28684fa9e02dSMarcin Wojtas 		/* If header is in linear space, just point into mbuf's data. */
28694fa9e02dSMarcin Wojtas 		if (likely(push_len <= mbuf_head_len)) {
28704fa9e02dSMarcin Wojtas 			*push_hdr = mbuf->m_data;
28714fa9e02dSMarcin Wojtas 		/*
28724fa9e02dSMarcin Wojtas 		 * Otherwise, copy whole portion of header from multiple mbufs
28734fa9e02dSMarcin Wojtas 		 * to intermediate buffer.
28744fa9e02dSMarcin Wojtas 		 */
28754fa9e02dSMarcin Wojtas 		} else {
28764fa9e02dSMarcin Wojtas 			m_copydata(mbuf, 0, push_len,
28774fa9e02dSMarcin Wojtas 			    tx_ring->push_buf_intermediate_buf);
28784fa9e02dSMarcin Wojtas 			*push_hdr = tx_ring->push_buf_intermediate_buf;
28794fa9e02dSMarcin Wojtas 
28804fa9e02dSMarcin Wojtas 			counter_u64_add(tx_ring->tx_stats.llq_buffer_copy, 1);
28814fa9e02dSMarcin Wojtas 			delta = push_len - mbuf_head_len;
28824fa9e02dSMarcin Wojtas 		}
28834fa9e02dSMarcin Wojtas 
28844fa9e02dSMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH,
28854fa9e02dSMarcin Wojtas 		    "mbuf: %p header_buf->vaddr: %p push_len: %d\n",
28864fa9e02dSMarcin Wojtas 		    mbuf, *push_hdr, push_len);
28874fa9e02dSMarcin Wojtas 
28884fa9e02dSMarcin Wojtas 		/*
28894fa9e02dSMarcin Wojtas 		* If header was in linear memory space, map for the dma rest of the data
28904fa9e02dSMarcin Wojtas 		* in the first mbuf of the mbuf chain.
28914fa9e02dSMarcin Wojtas 		*/
28924fa9e02dSMarcin Wojtas 		if (mbuf_head_len > push_len) {
28934fa9e02dSMarcin Wojtas 			rc = bus_dmamap_load(adapter->tx_buf_tag,
28944fa9e02dSMarcin Wojtas 			    tx_info->map_head,
28954fa9e02dSMarcin Wojtas 			mbuf->m_data + push_len, mbuf_head_len - push_len,
28964fa9e02dSMarcin Wojtas 			ena_dmamap_llq, ena_buf, BUS_DMA_NOWAIT);
28974fa9e02dSMarcin Wojtas 			if (unlikely((rc != 0) || (ena_buf->paddr == 0)))
28984fa9e02dSMarcin Wojtas 				goto single_dma_error;
28994fa9e02dSMarcin Wojtas 
29004fa9e02dSMarcin Wojtas 			ena_buf++;
29014fa9e02dSMarcin Wojtas 			tx_info->num_of_bufs++;
29024fa9e02dSMarcin Wojtas 
29034fa9e02dSMarcin Wojtas 			tx_info->head_mapped = true;
29044fa9e02dSMarcin Wojtas 		}
29054fa9e02dSMarcin Wojtas 		mbuf = mbuf->m_next;
29064fa9e02dSMarcin Wojtas 	} else {
29074fa9e02dSMarcin Wojtas 		*push_hdr = NULL;
29084fa9e02dSMarcin Wojtas 		/*
29094fa9e02dSMarcin Wojtas 		* header_len is just a hint for the device. Because FreeBSD is not
29104fa9e02dSMarcin Wojtas 		* giving us information about packet header length and it is not
29114fa9e02dSMarcin Wojtas 		* guaranteed that all packet headers will be in the 1st mbuf, setting
29124fa9e02dSMarcin Wojtas 		* header_len to 0 is making the device ignore this value and resolve
29134fa9e02dSMarcin Wojtas 		* header on it's own.
29144fa9e02dSMarcin Wojtas 		*/
29154fa9e02dSMarcin Wojtas 		*header_len = 0;
29164fa9e02dSMarcin Wojtas 	}
29174fa9e02dSMarcin Wojtas 
29184fa9e02dSMarcin Wojtas 	/*
29194fa9e02dSMarcin Wojtas 	 * If header is in non linear space (delta > 0), then skip mbufs
29204fa9e02dSMarcin Wojtas 	 * containing header and map the last one containing both header and the
29214fa9e02dSMarcin Wojtas 	 * packet data.
29224fa9e02dSMarcin Wojtas 	 * The first segment is already counted in.
29234fa9e02dSMarcin Wojtas 	 * If LLQ is not supported, the loop will be skipped.
29244fa9e02dSMarcin Wojtas 	 */
29254fa9e02dSMarcin Wojtas 	while (delta > 0) {
29264fa9e02dSMarcin Wojtas 		frag_len = mbuf->m_len;
29274fa9e02dSMarcin Wojtas 
29284fa9e02dSMarcin Wojtas 		/*
29294fa9e02dSMarcin Wojtas 		 * If whole segment contains header just move to the
29304fa9e02dSMarcin Wojtas 		 * next one and reduce delta.
29314fa9e02dSMarcin Wojtas 		 */
29324fa9e02dSMarcin Wojtas 		if (unlikely(delta >= frag_len)) {
29334fa9e02dSMarcin Wojtas 			delta -= frag_len;
29344fa9e02dSMarcin Wojtas 		} else {
29354fa9e02dSMarcin Wojtas 			/*
29364fa9e02dSMarcin Wojtas 			 * Map rest of the packet data that was contained in
29374fa9e02dSMarcin Wojtas 			 * the mbuf.
29384fa9e02dSMarcin Wojtas 			 */
29394fa9e02dSMarcin Wojtas 			rc = bus_dmamap_load(adapter->tx_buf_tag,
29404fa9e02dSMarcin Wojtas 			    tx_info->map_head, mbuf->m_data + delta,
29414fa9e02dSMarcin Wojtas 			    frag_len - delta, ena_dmamap_llq, ena_buf,
29424fa9e02dSMarcin Wojtas 			    BUS_DMA_NOWAIT);
29434fa9e02dSMarcin Wojtas 			if (unlikely((rc != 0) || (ena_buf->paddr == 0)))
29444fa9e02dSMarcin Wojtas 				goto single_dma_error;
29454fa9e02dSMarcin Wojtas 
29464fa9e02dSMarcin Wojtas 			ena_buf++;
29474fa9e02dSMarcin Wojtas 			tx_info->num_of_bufs++;
29484fa9e02dSMarcin Wojtas 			tx_info->head_mapped = true;
29494fa9e02dSMarcin Wojtas 
29504fa9e02dSMarcin Wojtas 			delta = 0;
29514fa9e02dSMarcin Wojtas 		}
29524fa9e02dSMarcin Wojtas 
29534fa9e02dSMarcin Wojtas 		mbuf = mbuf->m_next;
29544fa9e02dSMarcin Wojtas 	}
29554fa9e02dSMarcin Wojtas 
29564fa9e02dSMarcin Wojtas 	if (mbuf == NULL) {
29574fa9e02dSMarcin Wojtas 		return (0);
29584fa9e02dSMarcin Wojtas 	}
29594fa9e02dSMarcin Wojtas 
29604fa9e02dSMarcin Wojtas 	/* Map rest of the mbufs */
29614fa9e02dSMarcin Wojtas 	rc = bus_dmamap_load_mbuf_sg(adapter->tx_buf_tag, tx_info->map_seg, mbuf,
29624fa9e02dSMarcin Wojtas 	    segs, &nsegs, BUS_DMA_NOWAIT);
29634fa9e02dSMarcin Wojtas 	if (unlikely((rc != 0) || (nsegs == 0))) {
29644fa9e02dSMarcin Wojtas 		ena_trace(ENA_WARNING,
296530425f93SMarcin Wojtas 		    "dmamap load failed! err: %d nsegs: %d\n", rc, nsegs);
29664fa9e02dSMarcin Wojtas 		goto dma_error;
29674fa9e02dSMarcin Wojtas 	}
29684fa9e02dSMarcin Wojtas 
29694fa9e02dSMarcin Wojtas 	for (i = 0; i < nsegs; i++) {
29704fa9e02dSMarcin Wojtas 		ena_buf->len = segs[i].ds_len;
29714fa9e02dSMarcin Wojtas 		ena_buf->paddr = segs[i].ds_addr;
29724fa9e02dSMarcin Wojtas 		ena_buf++;
29734fa9e02dSMarcin Wojtas 	}
29744fa9e02dSMarcin Wojtas 	tx_info->num_of_bufs += nsegs;
29754fa9e02dSMarcin Wojtas 	tx_info->seg_mapped = true;
29764fa9e02dSMarcin Wojtas 
29774fa9e02dSMarcin Wojtas 	return (0);
29784fa9e02dSMarcin Wojtas 
29794fa9e02dSMarcin Wojtas dma_error:
29804fa9e02dSMarcin Wojtas 	if (tx_info->head_mapped == true)
29814fa9e02dSMarcin Wojtas 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map_head);
29824fa9e02dSMarcin Wojtas single_dma_error:
29834fa9e02dSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
29844fa9e02dSMarcin Wojtas 	tx_info->mbuf = NULL;
29854fa9e02dSMarcin Wojtas 	return (rc);
29864fa9e02dSMarcin Wojtas }
29874fa9e02dSMarcin Wojtas 
29881e9fb899SZbigniew Bodek static int
29891e9fb899SZbigniew Bodek ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
29909b8d05b8SZbigniew Bodek {
29919b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
29929b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info;
29939b8d05b8SZbigniew Bodek 	struct ena_com_tx_ctx ena_tx_ctx;
29949b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
29959b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
29969b8d05b8SZbigniew Bodek 	void *push_hdr;
29979b8d05b8SZbigniew Bodek 	uint16_t next_to_use;
29989b8d05b8SZbigniew Bodek 	uint16_t req_id;
29999b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
30004fa9e02dSMarcin Wojtas 	uint16_t header_len;
30014fa9e02dSMarcin Wojtas 	int rc;
30021e9fb899SZbigniew Bodek 	int nb_hw_desc;
30039b8d05b8SZbigniew Bodek 
30049b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
30059b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
30069b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
300790f4da8bSMarcin Wojtas 	io_sq = &ena_dev->io_sq_queues[ena_qid];
30089b8d05b8SZbigniew Bodek 
30091b069f1cSZbigniew Bodek 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
30103f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
30111e9fb899SZbigniew Bodek 		ena_trace(ENA_WARNING,
301230425f93SMarcin Wojtas 		    "Failed to collapse mbuf! err: %d\n", rc);
30131e9fb899SZbigniew Bodek 		return (rc);
30149b8d05b8SZbigniew Bodek 	}
30159b8d05b8SZbigniew Bodek 
301630425f93SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes\n", (*mbuf)->m_pkthdr.len);
30174fa9e02dSMarcin Wojtas 
30189b8d05b8SZbigniew Bodek 	next_to_use = tx_ring->next_to_use;
30199b8d05b8SZbigniew Bodek 	req_id = tx_ring->free_tx_ids[next_to_use];
30209b8d05b8SZbigniew Bodek 	tx_info = &tx_ring->tx_buffer_info[req_id];
30219b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = 0;
30229b8d05b8SZbigniew Bodek 
30234fa9e02dSMarcin Wojtas 	rc = ena_tx_map_mbuf(tx_ring, tx_info, *mbuf, &push_hdr, &header_len);
30244fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
30254fa9e02dSMarcin Wojtas 		ena_trace(ENA_WARNING, "Failed to map TX mbuf\n");
30264fa9e02dSMarcin Wojtas 		return (rc);
30279b8d05b8SZbigniew Bodek 	}
30289b8d05b8SZbigniew Bodek 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
30299b8d05b8SZbigniew Bodek 	ena_tx_ctx.ena_bufs = tx_info->bufs;
30309b8d05b8SZbigniew Bodek 	ena_tx_ctx.push_header = push_hdr;
30319b8d05b8SZbigniew Bodek 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
30329b8d05b8SZbigniew Bodek 	ena_tx_ctx.req_id = req_id;
30339b8d05b8SZbigniew Bodek 	ena_tx_ctx.header_len = header_len;
30349b8d05b8SZbigniew Bodek 
30359b8d05b8SZbigniew Bodek 	/* Set flags and meta data */
30361e9fb899SZbigniew Bodek 	ena_tx_csum(&ena_tx_ctx, *mbuf);
3037af66d7d0SMarcin Wojtas 
3038af66d7d0SMarcin Wojtas 	if (tx_ring->acum_pkts == DB_THRESHOLD ||
3039af66d7d0SMarcin Wojtas 	    ena_com_is_doorbell_needed(tx_ring->ena_com_io_sq, &ena_tx_ctx)) {
3040af66d7d0SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH,
3041af66d7d0SMarcin Wojtas 		    "llq tx max burst size of queue %d achieved, writing doorbell to send burst\n",
3042af66d7d0SMarcin Wojtas 		    tx_ring->que->id);
3043af66d7d0SMarcin Wojtas 		wmb();
3044af66d7d0SMarcin Wojtas 		ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
3045af66d7d0SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
3046af66d7d0SMarcin Wojtas 		tx_ring->acum_pkts = 0;
3047af66d7d0SMarcin Wojtas 	}
3048af66d7d0SMarcin Wojtas 
30499b8d05b8SZbigniew Bodek 	/* Prepare the packet's descriptors and send them to device */
30509b8d05b8SZbigniew Bodek 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
30513f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
3052e3cecf70SMarcin Wojtas 		if (likely(rc == ENA_COM_NO_MEM)) {
3053e3cecf70SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_TXPTH,
3054e3cecf70SMarcin Wojtas 			    "tx ring[%d] if out of space\n", tx_ring->que->id);
3055e3cecf70SMarcin Wojtas 		} else {
3056e3cecf70SMarcin Wojtas 			device_printf(adapter->pdev,
3057e3cecf70SMarcin Wojtas 			    "failed to prepare tx bufs\n");
3058e3cecf70SMarcin Wojtas 		}
30590052f3b5SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
30609b8d05b8SZbigniew Bodek 		goto dma_error;
30619b8d05b8SZbigniew Bodek 	}
30629b8d05b8SZbigniew Bodek 
30639b8d05b8SZbigniew Bodek 	counter_enter();
30649b8d05b8SZbigniew Bodek 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
30650bdffe59SMarcin Wojtas 	counter_u64_add_protected(tx_ring->tx_stats.bytes,
30660bdffe59SMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
306730217e2dSMarcin Wojtas 
306830217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
306930217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
307030217e2dSMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
30719b8d05b8SZbigniew Bodek 	counter_exit();
30729b8d05b8SZbigniew Bodek 
30739b8d05b8SZbigniew Bodek 	tx_info->tx_descs = nb_hw_desc;
30749b8d05b8SZbigniew Bodek 	getbinuptime(&tx_info->timestamp);
30759b8d05b8SZbigniew Bodek 	tx_info->print_once = true;
30769b8d05b8SZbigniew Bodek 
30779b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
30789b8d05b8SZbigniew Bodek 	    tx_ring->ring_size);
30799b8d05b8SZbigniew Bodek 
30805cb9db07SMarcin Wojtas 	/* stop the queue when no more space available, the packet can have up
30815cb9db07SMarcin Wojtas 	 * to sgl_size + 2. one for the meta descriptor and one for header
30825cb9db07SMarcin Wojtas 	 * (if the header is larger than tx_max_header_size).
30835cb9db07SMarcin Wojtas 	 */
30845cb9db07SMarcin Wojtas 	if (unlikely(!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
30855cb9db07SMarcin Wojtas 	    adapter->max_tx_sgl_size + 2))) {
30865cb9db07SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH, "Stop queue %d\n",
30875cb9db07SMarcin Wojtas 		    tx_ring->que->id);
30885cb9db07SMarcin Wojtas 
30895cb9db07SMarcin Wojtas 		tx_ring->running = false;
30905cb9db07SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.queue_stop, 1);
30915cb9db07SMarcin Wojtas 
30925cb9db07SMarcin Wojtas 		/* There is a rare condition where this function decides to
30935cb9db07SMarcin Wojtas 		 * stop the queue but meanwhile tx_cleanup() updates
30945cb9db07SMarcin Wojtas 		 * next_to_completion and terminates.
30955cb9db07SMarcin Wojtas 		 * The queue will remain stopped forever.
30965cb9db07SMarcin Wojtas 		 * To solve this issue this function performs mb(), checks
30975cb9db07SMarcin Wojtas 		 * the wakeup condition and wakes up the queue if needed.
30985cb9db07SMarcin Wojtas 		 */
30995cb9db07SMarcin Wojtas 		mb();
31005cb9db07SMarcin Wojtas 
31015cb9db07SMarcin Wojtas 		if (ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
31025cb9db07SMarcin Wojtas 		    ENA_TX_RESUME_THRESH)) {
31035cb9db07SMarcin Wojtas 			tx_ring->running = true;
31045cb9db07SMarcin Wojtas 			counter_u64_add(tx_ring->tx_stats.queue_wakeup, 1);
31055cb9db07SMarcin Wojtas 		}
31065cb9db07SMarcin Wojtas 	}
31075cb9db07SMarcin Wojtas 
31084fa9e02dSMarcin Wojtas 	if (tx_info->head_mapped == true)
31094fa9e02dSMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_head,
31104fa9e02dSMarcin Wojtas 		    BUS_DMASYNC_PREWRITE);
31114fa9e02dSMarcin Wojtas 	if (tx_info->seg_mapped == true)
31124fa9e02dSMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map_seg,
31130bdffe59SMarcin Wojtas 		    BUS_DMASYNC_PREWRITE);
31149b8d05b8SZbigniew Bodek 
31159b8d05b8SZbigniew Bodek 	return (0);
31169b8d05b8SZbigniew Bodek 
31179b8d05b8SZbigniew Bodek dma_error:
31189b8d05b8SZbigniew Bodek 	tx_info->mbuf = NULL;
31194fa9e02dSMarcin Wojtas 	if (tx_info->seg_mapped == true) {
31204fa9e02dSMarcin Wojtas 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map_seg);
31214fa9e02dSMarcin Wojtas 		tx_info->seg_mapped = false;
31224fa9e02dSMarcin Wojtas 	}
31234fa9e02dSMarcin Wojtas 	if (tx_info->head_mapped == true) {
31244fa9e02dSMarcin Wojtas 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map_head);
31254fa9e02dSMarcin Wojtas 		tx_info->head_mapped = false;
31264fa9e02dSMarcin Wojtas 	}
31279b8d05b8SZbigniew Bodek 
31289b8d05b8SZbigniew Bodek 	return (rc);
31299b8d05b8SZbigniew Bodek }
31309b8d05b8SZbigniew Bodek 
31319b8d05b8SZbigniew Bodek static void
31329b8d05b8SZbigniew Bodek ena_start_xmit(struct ena_ring *tx_ring)
31339b8d05b8SZbigniew Bodek {
31349b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
31359b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = tx_ring->adapter;
31369b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
31379b8d05b8SZbigniew Bodek 	int ena_qid;
31389b8d05b8SZbigniew Bodek 	int ret = 0;
31399b8d05b8SZbigniew Bodek 
31403f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
31419b8d05b8SZbigniew Bodek 		return;
31429b8d05b8SZbigniew Bodek 
3143fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter)))
31449b8d05b8SZbigniew Bodek 		return;
31459b8d05b8SZbigniew Bodek 
31469b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
31479b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
31489b8d05b8SZbigniew Bodek 
31499b8d05b8SZbigniew Bodek 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
31509b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
315130425f93SMarcin Wojtas 		    " header csum flags %#jx\n",
31524e8acd84SMarcin Wojtas 		    mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
31539b8d05b8SZbigniew Bodek 
31545cb9db07SMarcin Wojtas 		if (unlikely(!tx_ring->running)) {
31555cb9db07SMarcin Wojtas 			drbr_putback(adapter->ifp, tx_ring->br, mbuf);
31565cb9db07SMarcin Wojtas 			break;
31575cb9db07SMarcin Wojtas 		}
31589b8d05b8SZbigniew Bodek 
31593f9ed7abSMarcin Wojtas 		if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
31609b8d05b8SZbigniew Bodek 			if (ret == ENA_COM_NO_MEM) {
31619b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
31629b8d05b8SZbigniew Bodek 			} else if (ret == ENA_COM_NO_SPACE) {
31639b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
31649b8d05b8SZbigniew Bodek 			} else {
31659b8d05b8SZbigniew Bodek 				m_freem(mbuf);
31669b8d05b8SZbigniew Bodek 				drbr_advance(adapter->ifp, tx_ring->br);
31679b8d05b8SZbigniew Bodek 			}
31689b8d05b8SZbigniew Bodek 
31699b8d05b8SZbigniew Bodek 			break;
31709b8d05b8SZbigniew Bodek 		}
31719b8d05b8SZbigniew Bodek 
3172b4b29032SZbigniew Bodek 		drbr_advance(adapter->ifp, tx_ring->br);
3173b4b29032SZbigniew Bodek 
31743f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(adapter->ifp) &
31753f9ed7abSMarcin Wojtas 		    IFF_DRV_RUNNING) == 0))
31769b8d05b8SZbigniew Bodek 			return;
31779b8d05b8SZbigniew Bodek 
3178af66d7d0SMarcin Wojtas 		tx_ring->acum_pkts++;
31799b8d05b8SZbigniew Bodek 
31809b8d05b8SZbigniew Bodek 		BPF_MTAP(adapter->ifp, mbuf);
3181af66d7d0SMarcin Wojtas 	}
31829b8d05b8SZbigniew Bodek 
3183af66d7d0SMarcin Wojtas 	if (likely(tx_ring->acum_pkts != 0)) {
31849b8d05b8SZbigniew Bodek 		wmb();
31859b8d05b8SZbigniew Bodek 		/* Trigger the dma engine */
31869b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(io_sq);
31879b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
3188af66d7d0SMarcin Wojtas 		tx_ring->acum_pkts = 0;
31899b8d05b8SZbigniew Bodek 	}
31909b8d05b8SZbigniew Bodek 
31915cb9db07SMarcin Wojtas 	if (unlikely(!tx_ring->running))
31925cb9db07SMarcin Wojtas 		taskqueue_enqueue(tx_ring->que->cleanup_tq,
31935cb9db07SMarcin Wojtas 		    &tx_ring->que->cleanup_task);
31949b8d05b8SZbigniew Bodek }
31959b8d05b8SZbigniew Bodek 
31969b8d05b8SZbigniew Bodek static void
31979b8d05b8SZbigniew Bodek ena_deferred_mq_start(void *arg, int pending)
31989b8d05b8SZbigniew Bodek {
31999b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
32009b8d05b8SZbigniew Bodek 	struct ifnet *ifp = tx_ring->adapter->ifp;
32019b8d05b8SZbigniew Bodek 
32020bdffe59SMarcin Wojtas 	while (!drbr_empty(ifp, tx_ring->br) &&
32035cb9db07SMarcin Wojtas 	    tx_ring->running &&
32040bdffe59SMarcin Wojtas 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
32059b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
32069b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
32079b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
32089b8d05b8SZbigniew Bodek 	}
32099b8d05b8SZbigniew Bodek }
32109b8d05b8SZbigniew Bodek 
32119b8d05b8SZbigniew Bodek static int
32129b8d05b8SZbigniew Bodek ena_mq_start(if_t ifp, struct mbuf *m)
32139b8d05b8SZbigniew Bodek {
32149b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
32159b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
32169b8d05b8SZbigniew Bodek 	int ret, is_drbr_empty;
32179b8d05b8SZbigniew Bodek 	uint32_t i;
32189b8d05b8SZbigniew Bodek 
32193f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
32209b8d05b8SZbigniew Bodek 		return (ENODEV);
32219b8d05b8SZbigniew Bodek 
32229b8d05b8SZbigniew Bodek 	/* Which queue to use */
32239b8d05b8SZbigniew Bodek 	/*
32249b8d05b8SZbigniew Bodek 	 * If everything is setup correctly, it should be the
32259b8d05b8SZbigniew Bodek 	 * same bucket that the current CPU we're on is.
32269b8d05b8SZbigniew Bodek 	 * It should improve performance.
32279b8d05b8SZbigniew Bodek 	 */
32289b8d05b8SZbigniew Bodek 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
32299b8d05b8SZbigniew Bodek 		i = m->m_pkthdr.flowid % adapter->num_queues;
32309b8d05b8SZbigniew Bodek 	} else {
32319b8d05b8SZbigniew Bodek 		i = curcpu % adapter->num_queues;
32329b8d05b8SZbigniew Bodek 	}
32339b8d05b8SZbigniew Bodek 	tx_ring = &adapter->tx_ring[i];
32349b8d05b8SZbigniew Bodek 
32359b8d05b8SZbigniew Bodek 	/* Check if drbr is empty before putting packet */
32369b8d05b8SZbigniew Bodek 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
32379b8d05b8SZbigniew Bodek 	ret = drbr_enqueue(ifp, tx_ring->br, m);
32383f9ed7abSMarcin Wojtas 	if (unlikely(ret != 0)) {
32399b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
32409b8d05b8SZbigniew Bodek 		return (ret);
32419b8d05b8SZbigniew Bodek 	}
32429b8d05b8SZbigniew Bodek 
324367ec48bbSMarcin Wojtas 	if (is_drbr_empty && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
32449b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
32459b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
32469b8d05b8SZbigniew Bodek 	} else {
32479b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
32489b8d05b8SZbigniew Bodek 	}
32499b8d05b8SZbigniew Bodek 
32509b8d05b8SZbigniew Bodek 	return (0);
32519b8d05b8SZbigniew Bodek }
32529b8d05b8SZbigniew Bodek 
32539b8d05b8SZbigniew Bodek static void
32549b8d05b8SZbigniew Bodek ena_qflush(if_t ifp)
32559b8d05b8SZbigniew Bodek {
32569b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
32579b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = adapter->tx_ring;
32589b8d05b8SZbigniew Bodek 	int i;
32599b8d05b8SZbigniew Bodek 
32609b8d05b8SZbigniew Bodek 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
32610bdffe59SMarcin Wojtas 		if (!drbr_empty(ifp, tx_ring->br)) {
32629b8d05b8SZbigniew Bodek 			ENA_RING_MTX_LOCK(tx_ring);
32639b8d05b8SZbigniew Bodek 			drbr_flush(ifp, tx_ring->br);
32649b8d05b8SZbigniew Bodek 			ENA_RING_MTX_UNLOCK(tx_ring);
32659b8d05b8SZbigniew Bodek 		}
32669b8d05b8SZbigniew Bodek 
32679b8d05b8SZbigniew Bodek 	if_qflush(ifp);
32689b8d05b8SZbigniew Bodek }
32699b8d05b8SZbigniew Bodek 
32700bdffe59SMarcin Wojtas static int
32710bdffe59SMarcin Wojtas ena_calc_io_queue_num(struct ena_adapter *adapter,
32729b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
32739b8d05b8SZbigniew Bodek {
32746064f289SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
32754fa9e02dSMarcin Wojtas 	int io_tx_sq_num, io_tx_cq_num, io_rx_num, io_queue_num;
32769b8d05b8SZbigniew Bodek 
32776064f289SMarcin Wojtas 	/* Regular queues capabilities */
32786064f289SMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
32796064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
32806064f289SMarcin Wojtas 		    &get_feat_ctx->max_queue_ext.max_queue_ext;
32814fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, max_queue_ext->max_rx_sq_num,
32824fa9e02dSMarcin Wojtas 			max_queue_ext->max_rx_cq_num);
32836064f289SMarcin Wojtas 
32844fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queue_ext->max_tx_sq_num;
32854fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queue_ext->max_tx_cq_num;
32866064f289SMarcin Wojtas 	} else {
32876064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
32886064f289SMarcin Wojtas 		    &get_feat_ctx->max_queues;
32894fa9e02dSMarcin Wojtas 		io_tx_sq_num = max_queues->max_sq_num;
32904fa9e02dSMarcin Wojtas 		io_tx_cq_num = max_queues->max_cq_num;
32914fa9e02dSMarcin Wojtas 		io_rx_num = min_t(int, io_tx_sq_num, io_tx_cq_num);
32926064f289SMarcin Wojtas 	}
32939b8d05b8SZbigniew Bodek 
32944fa9e02dSMarcin Wojtas 	/* In case of LLQ use the llq fields for the tx SQ/CQ */
32954fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_DEV)
32964fa9e02dSMarcin Wojtas 		io_tx_sq_num = get_feat_ctx->llq.max_llq_num;
32974fa9e02dSMarcin Wojtas 
32989b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
32994fa9e02dSMarcin Wojtas 	io_queue_num = min_t(int, io_queue_num, io_rx_num);
33004fa9e02dSMarcin Wojtas 	io_queue_num = min_t(int, io_queue_num, io_tx_sq_num);
33014fa9e02dSMarcin Wojtas 	io_queue_num = min_t(int, io_queue_num, io_tx_cq_num);
33029b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
33039b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num,
33049b8d05b8SZbigniew Bodek 	    pci_msix_count(adapter->pdev) - 1);
33059b8d05b8SZbigniew Bodek 
33060bdffe59SMarcin Wojtas 	return (io_queue_num);
33079b8d05b8SZbigniew Bodek }
33089b8d05b8SZbigniew Bodek 
33090bdffe59SMarcin Wojtas static int
33104fa9e02dSMarcin Wojtas ena_enable_wc(struct resource *res)
33114fa9e02dSMarcin Wojtas {
3312*472d4784SMarcin Wojtas #if defined(__i386) || defined(__amd64) || defined(__aarch64__)
33134fa9e02dSMarcin Wojtas 	vm_offset_t va;
33144fa9e02dSMarcin Wojtas 	vm_size_t len;
33154fa9e02dSMarcin Wojtas 	int rc;
33164fa9e02dSMarcin Wojtas 
33174fa9e02dSMarcin Wojtas 	va = (vm_offset_t)rman_get_virtual(res);
33184fa9e02dSMarcin Wojtas 	len = rman_get_size(res);
33194fa9e02dSMarcin Wojtas 	/* Enable write combining */
3320*472d4784SMarcin Wojtas 	rc = pmap_change_attr(va, len, VM_MEMATTR_WRITE_COMBINING);
33214fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
33224fa9e02dSMarcin Wojtas 		ena_trace(ENA_ALERT, "pmap_change_attr failed, %d\n", rc);
33234fa9e02dSMarcin Wojtas 		return (rc);
33244fa9e02dSMarcin Wojtas 	}
33254fa9e02dSMarcin Wojtas 
33264fa9e02dSMarcin Wojtas 	return (0);
33274fa9e02dSMarcin Wojtas #endif
33284fa9e02dSMarcin Wojtas 	return (EOPNOTSUPP);
33294fa9e02dSMarcin Wojtas }
33304fa9e02dSMarcin Wojtas 
33314fa9e02dSMarcin Wojtas static int
33324fa9e02dSMarcin Wojtas ena_set_queues_placement_policy(device_t pdev, struct ena_com_dev *ena_dev,
33334fa9e02dSMarcin Wojtas     struct ena_admin_feature_llq_desc *llq,
33344fa9e02dSMarcin Wojtas     struct ena_llq_configurations *llq_default_configurations)
33354fa9e02dSMarcin Wojtas {
33364fa9e02dSMarcin Wojtas 	struct ena_adapter *adapter = device_get_softc(pdev);
33374fa9e02dSMarcin Wojtas 	int rc, rid;
33384fa9e02dSMarcin Wojtas 	uint32_t llq_feature_mask;
33394fa9e02dSMarcin Wojtas 
33404fa9e02dSMarcin Wojtas 	llq_feature_mask = 1 << ENA_ADMIN_LLQ;
33414fa9e02dSMarcin Wojtas 	if (!(ena_dev->supported_features & llq_feature_mask)) {
33424fa9e02dSMarcin Wojtas 		device_printf(pdev,
33434fa9e02dSMarcin Wojtas 		    "LLQ is not supported. Fallback to host mode policy.\n");
33444fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
33454fa9e02dSMarcin Wojtas 		return (0);
33464fa9e02dSMarcin Wojtas 	}
33474fa9e02dSMarcin Wojtas 
33484fa9e02dSMarcin Wojtas 	rc = ena_com_config_dev_mode(ena_dev, llq, llq_default_configurations);
33494fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
33504fa9e02dSMarcin Wojtas 		device_printf(pdev, "Failed to configure the device mode. "
33514fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
33524fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
33534fa9e02dSMarcin Wojtas 		return (0);
33544fa9e02dSMarcin Wojtas 	}
33554fa9e02dSMarcin Wojtas 
33564fa9e02dSMarcin Wojtas 	/* Nothing to config, exit */
33574fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
33584fa9e02dSMarcin Wojtas 		return (0);
33594fa9e02dSMarcin Wojtas 
33604fa9e02dSMarcin Wojtas 	/* Try to allocate resources for LLQ bar */
33614fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_MEM_BAR);
33624fa9e02dSMarcin Wojtas 	adapter->memory = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
33634fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
33644fa9e02dSMarcin Wojtas 	if (unlikely(adapter->memory == NULL)) {
33654fa9e02dSMarcin Wojtas 		device_printf(pdev, "unable to allocate LLQ bar resource. "
33664fa9e02dSMarcin Wojtas 		    "Fallback to host mode policy.\n");
33674fa9e02dSMarcin Wojtas 		ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
33684fa9e02dSMarcin Wojtas 		return (0);
33694fa9e02dSMarcin Wojtas 	}
33704fa9e02dSMarcin Wojtas 
33714fa9e02dSMarcin Wojtas 	/* Enable write combining for better LLQ performance */
33724fa9e02dSMarcin Wojtas 	rc = ena_enable_wc(adapter->memory);
33734fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
33744fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to enable write combining.\n");
33754fa9e02dSMarcin Wojtas 		return (rc);
33764fa9e02dSMarcin Wojtas 	}
33774fa9e02dSMarcin Wojtas 
33784fa9e02dSMarcin Wojtas 	/*
33794fa9e02dSMarcin Wojtas 	 * Save virtual address of the device's memory region
33804fa9e02dSMarcin Wojtas 	 * for the ena_com layer.
33814fa9e02dSMarcin Wojtas 	 */
33824fa9e02dSMarcin Wojtas 	ena_dev->mem_bar = rman_get_virtual(adapter->memory);
33834fa9e02dSMarcin Wojtas 
33844fa9e02dSMarcin Wojtas 	return (0);
33854fa9e02dSMarcin Wojtas }
33864fa9e02dSMarcin Wojtas 
33874fa9e02dSMarcin Wojtas static inline
33884fa9e02dSMarcin Wojtas void set_default_llq_configurations(struct ena_llq_configurations *llq_config)
33894fa9e02dSMarcin Wojtas {
33904fa9e02dSMarcin Wojtas 	llq_config->llq_header_location = ENA_ADMIN_INLINE_HEADER;
33914fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size = ENA_ADMIN_LIST_ENTRY_SIZE_128B;
33924fa9e02dSMarcin Wojtas 	llq_config->llq_stride_ctrl = ENA_ADMIN_MULTIPLE_DESCS_PER_ENTRY;
33934fa9e02dSMarcin Wojtas 	llq_config->llq_num_decs_before_header =
33944fa9e02dSMarcin Wojtas 	    ENA_ADMIN_LLQ_NUM_DESCS_BEFORE_HEADER_2;
33954fa9e02dSMarcin Wojtas 	llq_config->llq_ring_entry_size_value = 128;
33964fa9e02dSMarcin Wojtas }
33974fa9e02dSMarcin Wojtas 
33984fa9e02dSMarcin Wojtas static int
33996064f289SMarcin Wojtas ena_calc_queue_size(struct ena_adapter *adapter,
34006064f289SMarcin Wojtas     struct ena_calc_queue_size_ctx *ctx)
34019b8d05b8SZbigniew Bodek {
34024fa9e02dSMarcin Wojtas 	struct ena_admin_feature_llq_desc *llq = &ctx->get_feat_ctx->llq;
34034fa9e02dSMarcin Wojtas 	struct ena_com_dev *ena_dev = ctx->ena_dev;
34046064f289SMarcin Wojtas 	uint32_t tx_queue_size = ENA_DEFAULT_RING_SIZE;
34056064f289SMarcin Wojtas 	uint32_t rx_queue_size = adapter->rx_ring_size;
34069b8d05b8SZbigniew Bodek 
34074fa9e02dSMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
34086064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
34096064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
34106064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
34116064f289SMarcin Wojtas 		    max_queue_ext->max_rx_cq_depth);
34126064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
34136064f289SMarcin Wojtas 		    max_queue_ext->max_rx_sq_depth);
34146064f289SMarcin Wojtas 		tx_queue_size = min_t(uint32_t, tx_queue_size,
34156064f289SMarcin Wojtas 		    max_queue_ext->max_tx_cq_depth);
34164fa9e02dSMarcin Wojtas 
34174fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
34184fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
34194fa9e02dSMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
34204fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
34214fa9e02dSMarcin Wojtas 		else
34226064f289SMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
34236064f289SMarcin Wojtas 			    max_queue_ext->max_tx_sq_depth);
34244fa9e02dSMarcin Wojtas 
34256064f289SMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
34266064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_rx_descs);
34276064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
34286064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_tx_descs);
34296064f289SMarcin Wojtas 	} else {
34306064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
34316064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queues;
34326064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
34336064f289SMarcin Wojtas 		    max_queues->max_cq_depth);
34346064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
34356064f289SMarcin Wojtas 		    max_queues->max_sq_depth);
34364fa9e02dSMarcin Wojtas 		tx_queue_size = min_t(uint32_t, tx_queue_size,
34374fa9e02dSMarcin Wojtas 		    max_queues->max_cq_depth);
34384fa9e02dSMarcin Wojtas 
34394fa9e02dSMarcin Wojtas 		if (ena_dev->tx_mem_queue_type ==
34404fa9e02dSMarcin Wojtas 		    ENA_ADMIN_PLACEMENT_POLICY_DEV)
34414fa9e02dSMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
34424fa9e02dSMarcin Wojtas 			    llq->max_llq_depth);
34434fa9e02dSMarcin Wojtas 		else
34444fa9e02dSMarcin Wojtas 			tx_queue_size = min_t(uint32_t, tx_queue_size,
34454fa9e02dSMarcin Wojtas 			    max_queues->max_sq_depth);
34464fa9e02dSMarcin Wojtas 
34476064f289SMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
34486064f289SMarcin Wojtas 		    max_queues->max_packet_tx_descs);
34496064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
34506064f289SMarcin Wojtas 		    max_queues->max_packet_rx_descs);
34516064f289SMarcin Wojtas 	}
34529b8d05b8SZbigniew Bodek 
34539b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
34546064f289SMarcin Wojtas 	rx_queue_size = 1 << (fls(rx_queue_size) - 1);
34556064f289SMarcin Wojtas 	tx_queue_size = 1 << (fls(tx_queue_size) - 1);
34566064f289SMarcin Wojtas 
34576064f289SMarcin Wojtas 	if (unlikely(rx_queue_size == 0 || tx_queue_size == 0)) {
34586064f289SMarcin Wojtas 		device_printf(ctx->pdev, "Invalid queue size\n");
34596064f289SMarcin Wojtas 		return (EFAULT);
34609b8d05b8SZbigniew Bodek 	}
34619b8d05b8SZbigniew Bodek 
34626064f289SMarcin Wojtas 	ctx->rx_queue_size = rx_queue_size;
34636064f289SMarcin Wojtas 	ctx->tx_queue_size = tx_queue_size;
34646064f289SMarcin Wojtas 
34656064f289SMarcin Wojtas 	return (0);
34669b8d05b8SZbigniew Bodek }
34679b8d05b8SZbigniew Bodek 
34686064f289SMarcin Wojtas static int
34696064f289SMarcin Wojtas ena_handle_updated_queues(struct ena_adapter *adapter,
34706064f289SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
34716064f289SMarcin Wojtas {
34726064f289SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
34736064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
34746064f289SMarcin Wojtas 	device_t pdev = adapter->pdev;
34756064f289SMarcin Wojtas 	bool are_queues_changed = false;
34766064f289SMarcin Wojtas 	int io_queue_num, rc;
34779b8d05b8SZbigniew Bodek 
34786064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
34796064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = get_feat_ctx;
34806064f289SMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
34816064f289SMarcin Wojtas 
34826064f289SMarcin Wojtas 	io_queue_num = ena_calc_io_queue_num(adapter, get_feat_ctx);
34836064f289SMarcin Wojtas 	rc = ena_calc_queue_size(adapter, &calc_queue_ctx);
34846064f289SMarcin Wojtas 	if (unlikely(rc != 0 || io_queue_num <= 0))
34856064f289SMarcin Wojtas 		return EFAULT;
34866064f289SMarcin Wojtas 
34876064f289SMarcin Wojtas 	if (adapter->tx_ring->buf_ring_size != adapter->buf_ring_size)
34886064f289SMarcin Wojtas 		are_queues_changed = true;
34896064f289SMarcin Wojtas 
34906064f289SMarcin Wojtas 	if (unlikely(adapter->tx_ring_size > calc_queue_ctx.tx_queue_size ||
34916064f289SMarcin Wojtas 	    adapter->rx_ring_size > calc_queue_ctx.rx_queue_size)) {
34926064f289SMarcin Wojtas 		device_printf(pdev,
34936064f289SMarcin Wojtas 		    "Not enough resources to allocate requested queue sizes "
34946064f289SMarcin Wojtas 		    "(TX,RX)=(%d,%d), falling back to queue sizes "
34956064f289SMarcin Wojtas 		    "(TX,RX)=(%d,%d)\n",
34966064f289SMarcin Wojtas 		    adapter->tx_ring_size,
34976064f289SMarcin Wojtas 		    adapter->rx_ring_size,
34986064f289SMarcin Wojtas 		    calc_queue_ctx.tx_queue_size,
34996064f289SMarcin Wojtas 		    calc_queue_ctx.rx_queue_size);
35006064f289SMarcin Wojtas 		adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
35016064f289SMarcin Wojtas 		adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
35026064f289SMarcin Wojtas 		adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
35036064f289SMarcin Wojtas 		adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
35046064f289SMarcin Wojtas 		are_queues_changed = true;
35056064f289SMarcin Wojtas 	}
35066064f289SMarcin Wojtas 
35076064f289SMarcin Wojtas 	if (unlikely(adapter->num_queues > io_queue_num)) {
35086064f289SMarcin Wojtas 		device_printf(pdev,
35096064f289SMarcin Wojtas 		    "Not enough resources to allocate %d queues, "
35106064f289SMarcin Wojtas 		    "falling back to %d queues\n",
35116064f289SMarcin Wojtas 		    adapter->num_queues, io_queue_num);
35126064f289SMarcin Wojtas 		adapter->num_queues = io_queue_num;
3513fd43fd2aSMarcin Wojtas 		if (ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter)) {
35146064f289SMarcin Wojtas 			ena_com_rss_destroy(ena_dev);
35156064f289SMarcin Wojtas 			rc = ena_rss_init_default(adapter);
35166064f289SMarcin Wojtas 			if (unlikely(rc != 0) && (rc != EOPNOTSUPP)) {
35176064f289SMarcin Wojtas 				device_printf(pdev, "Cannot init RSS rc: %d\n",
35186064f289SMarcin Wojtas 				    rc);
35196064f289SMarcin Wojtas 				return (rc);
35206064f289SMarcin Wojtas 			}
35216064f289SMarcin Wojtas 		}
35226064f289SMarcin Wojtas 		are_queues_changed = true;
35236064f289SMarcin Wojtas 	}
35246064f289SMarcin Wojtas 
35256064f289SMarcin Wojtas 	if (unlikely(are_queues_changed)) {
35266064f289SMarcin Wojtas 		ena_free_all_io_rings_resources(adapter);
35276064f289SMarcin Wojtas 		ena_init_io_rings(adapter);
35286064f289SMarcin Wojtas 	}
35296064f289SMarcin Wojtas 
35306064f289SMarcin Wojtas 	return (0);
35319b8d05b8SZbigniew Bodek }
35329b8d05b8SZbigniew Bodek 
35330bdffe59SMarcin Wojtas static int
35340bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
35359b8d05b8SZbigniew Bodek {
35369b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
35379b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
35389b8d05b8SZbigniew Bodek 	int qid, rc, i;
35399b8d05b8SZbigniew Bodek 
35409b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
35410bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
35424e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
35437d2544e6SMarcin Wojtas 		return (rc);
35449b8d05b8SZbigniew Bodek 	}
35459b8d05b8SZbigniew Bodek 
35469b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
35479b8d05b8SZbigniew Bodek 		qid = i % adapter->num_queues;
35489b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
35499b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
35500bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
35519b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
35527d2544e6SMarcin Wojtas 			goto err_rss_destroy;
35539b8d05b8SZbigniew Bodek 		}
35549b8d05b8SZbigniew Bodek 	}
35559b8d05b8SZbigniew Bodek 
35569b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
35579b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
35580bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
35599b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
35607d2544e6SMarcin Wojtas 		goto err_rss_destroy;
35619b8d05b8SZbigniew Bodek 	}
35629b8d05b8SZbigniew Bodek 
35639b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
35640bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
35659b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
35667d2544e6SMarcin Wojtas 		goto err_rss_destroy;
35679b8d05b8SZbigniew Bodek 	}
35689b8d05b8SZbigniew Bodek 
35699b8d05b8SZbigniew Bodek 	return (0);
35709b8d05b8SZbigniew Bodek 
35717d2544e6SMarcin Wojtas err_rss_destroy:
35729b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
35739b8d05b8SZbigniew Bodek 	return (rc);
35749b8d05b8SZbigniew Bodek }
35759b8d05b8SZbigniew Bodek 
35769b8d05b8SZbigniew Bodek static void
35779b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
35789b8d05b8SZbigniew Bodek {
35799b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
35809b8d05b8SZbigniew Bodek 	devclass_t dc;
35819b8d05b8SZbigniew Bodek 	int max;
35829b8d05b8SZbigniew Bodek 	int rc;
35839b8d05b8SZbigniew Bodek 
35849b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
35853f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
35864e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "No devclass ena\n");
35879b8d05b8SZbigniew Bodek 		return;
35889b8d05b8SZbigniew Bodek 	}
35899b8d05b8SZbigniew Bodek 
35909b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
35919b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
35929b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
35939b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
35949b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
3595fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
35963f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
35979b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
35989b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
35990bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
3600fd43fd2aSMarcin Wojtas 				ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_RSS_ACTIVE, adapter);
36019b8d05b8SZbigniew Bodek 			}
36029b8d05b8SZbigniew Bodek 		}
36039b8d05b8SZbigniew Bodek 	}
36049b8d05b8SZbigniew Bodek }
36059b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
36069b8d05b8SZbigniew Bodek 
36070bdffe59SMarcin Wojtas static void
360846021271SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev, device_t dev)
36099b8d05b8SZbigniew Bodek {
36109b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
361146021271SMarcin Wojtas 	uintptr_t rid;
36129b8d05b8SZbigniew Bodek 	int rc;
36139b8d05b8SZbigniew Bodek 
36149b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
36159b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
36163f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36179b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
36189b8d05b8SZbigniew Bodek 		return;
36199b8d05b8SZbigniew Bodek 	}
36209b8d05b8SZbigniew Bodek 
36219b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
36229b8d05b8SZbigniew Bodek 
362346021271SMarcin Wojtas 	if (pci_get_id(dev, PCI_ID_RID, &rid) == 0)
362446021271SMarcin Wojtas 		host_info->bdf = rid;
36259b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
36269b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
36279b8d05b8SZbigniew Bodek 
36289b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
36299b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
36309b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
36319b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
36329b8d05b8SZbigniew Bodek 
36339b8d05b8SZbigniew Bodek 	host_info->driver_version =
36349b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
36359b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
36369b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
36378ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
36389b8d05b8SZbigniew Bodek 
36399b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
36403f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
3641a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
36429b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
36439b8d05b8SZbigniew Bodek 		else
36449b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
36459b8d05b8SZbigniew Bodek 
36469b8d05b8SZbigniew Bodek 		goto err;
36479b8d05b8SZbigniew Bodek 	}
36489b8d05b8SZbigniew Bodek 
36499b8d05b8SZbigniew Bodek 	return;
36509b8d05b8SZbigniew Bodek 
36519b8d05b8SZbigniew Bodek err:
36529b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
36539b8d05b8SZbigniew Bodek }
36549b8d05b8SZbigniew Bodek 
36559b8d05b8SZbigniew Bodek static int
36569b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
36579b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
36589b8d05b8SZbigniew Bodek {
36599b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
36609b8d05b8SZbigniew Bodek 	bool readless_supported;
36619b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
36629b8d05b8SZbigniew Bodek 	int dma_width;
36639b8d05b8SZbigniew Bodek 	int rc;
36649b8d05b8SZbigniew Bodek 
36659b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
36663f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36679b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
36680bdffe59SMarcin Wojtas 		return (rc);
36699b8d05b8SZbigniew Bodek 	}
36709b8d05b8SZbigniew Bodek 
36719b8d05b8SZbigniew Bodek 	/*
36729b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
36739b8d05b8SZbigniew Bodek 	 * read is disabled
36749b8d05b8SZbigniew Bodek 	 */
36759b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
36769b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
36779b8d05b8SZbigniew Bodek 
3678a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
36793f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36809b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
36819b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
36829b8d05b8SZbigniew Bodek 	}
36839b8d05b8SZbigniew Bodek 
36849b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
36853f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36869b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
36879b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
36889b8d05b8SZbigniew Bodek 	}
36899b8d05b8SZbigniew Bodek 
36909b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
36913f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
36929b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
36939b8d05b8SZbigniew Bodek 		rc = dma_width;
36949b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
36959b8d05b8SZbigniew Bodek 	}
36969b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
36979b8d05b8SZbigniew Bodek 
36989b8d05b8SZbigniew Bodek 	/* ENA admin level init */
369967ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
37003f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37019b8d05b8SZbigniew Bodek 		device_printf(pdev,
37029b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
37039b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
37049b8d05b8SZbigniew Bodek 	}
37059b8d05b8SZbigniew Bodek 
37069b8d05b8SZbigniew Bodek 	/*
37079b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
37089b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
37099b8d05b8SZbigniew Bodek 	 * information
37109b8d05b8SZbigniew Bodek 	 */
37119b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
37129b8d05b8SZbigniew Bodek 
371346021271SMarcin Wojtas 	ena_config_host_info(ena_dev, pdev);
37149b8d05b8SZbigniew Bodek 
37159b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
37169b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
37173f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37189b8d05b8SZbigniew Bodek 		device_printf(pdev,
37199b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
37209b8d05b8SZbigniew Bodek 		goto err_admin_init;
37219b8d05b8SZbigniew Bodek 	}
37229b8d05b8SZbigniew Bodek 
3723e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
3724e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
3725e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
372640621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
3727e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
37289b8d05b8SZbigniew Bodek 
37299b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
37309b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
37313f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37329b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
37339b8d05b8SZbigniew Bodek 		goto err_admin_init;
37349b8d05b8SZbigniew Bodek 	}
37359b8d05b8SZbigniew Bodek 
37369b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
37379b8d05b8SZbigniew Bodek 
37380bdffe59SMarcin Wojtas 	return (0);
37399b8d05b8SZbigniew Bodek 
37409b8d05b8SZbigniew Bodek err_admin_init:
37419b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
37429b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
37439b8d05b8SZbigniew Bodek err_mmio_read_less:
37449b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
37459b8d05b8SZbigniew Bodek 
37460bdffe59SMarcin Wojtas 	return (rc);
37479b8d05b8SZbigniew Bodek }
37489b8d05b8SZbigniew Bodek 
37499b8d05b8SZbigniew Bodek static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
37509b8d05b8SZbigniew Bodek     int io_vectors)
37519b8d05b8SZbigniew Bodek {
37529b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
37539b8d05b8SZbigniew Bodek 	int rc;
37549b8d05b8SZbigniew Bodek 
37559b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
37563f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37579b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
37580bdffe59SMarcin Wojtas 		return (rc);
37599b8d05b8SZbigniew Bodek 	}
37609b8d05b8SZbigniew Bodek 
37619b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
37629b8d05b8SZbigniew Bodek 
37639b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
37643f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37659b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
37669b8d05b8SZbigniew Bodek 		goto err_disable_msix;
37679b8d05b8SZbigniew Bodek 	}
37689b8d05b8SZbigniew Bodek 
37699b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
37709b8d05b8SZbigniew Bodek 
37719b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
37729b8d05b8SZbigniew Bodek 
37730bdffe59SMarcin Wojtas 	return (0);
37749b8d05b8SZbigniew Bodek 
37759b8d05b8SZbigniew Bodek err_disable_msix:
37769b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
37779b8d05b8SZbigniew Bodek 
37780bdffe59SMarcin Wojtas 	return (rc);
37799b8d05b8SZbigniew Bodek }
37809b8d05b8SZbigniew Bodek 
37819b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
37829b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
37839b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
37849b8d05b8SZbigniew Bodek {
37859b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
378630217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
37879b8d05b8SZbigniew Bodek 	sbintime_t stime;
378830217e2dSMarcin Wojtas 	uint64_t rx_drops;
378930217e2dSMarcin Wojtas 
379030217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
379130217e2dSMarcin Wojtas 
379230217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
379330217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
379430217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
37959b8d05b8SZbigniew Bodek 
37969b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
37979b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
37989b8d05b8SZbigniew Bodek }
37999b8d05b8SZbigniew Bodek 
38009b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
38019b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
38029b8d05b8SZbigniew Bodek {
38039b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
38049b8d05b8SZbigniew Bodek 
38059b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
38069b8d05b8SZbigniew Bodek 		return;
38079b8d05b8SZbigniew Bodek 
380840621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
38099b8d05b8SZbigniew Bodek 		return;
38109b8d05b8SZbigniew Bodek 
38119b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
38129b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
38139b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
38149b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
38159b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
38169b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
3817858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
3818a195fab0SMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
3819fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
38209b8d05b8SZbigniew Bodek 		}
38219b8d05b8SZbigniew Bodek 	}
3822858659f7SMarcin Wojtas }
38239b8d05b8SZbigniew Bodek 
38249b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
38259b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
38269b8d05b8SZbigniew Bodek {
38270bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
38280bdffe59SMarcin Wojtas 	    false)) {
38299b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
38309b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
38319b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
3832858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
3833a195fab0SMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
3834fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
38359b8d05b8SZbigniew Bodek 		}
38369b8d05b8SZbigniew Bodek 	}
3837858659f7SMarcin Wojtas }
38389b8d05b8SZbigniew Bodek 
383974dba3adSMarcin Wojtas static int
3840d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3841d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
3842d12f7bfcSMarcin Wojtas {
3843d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
3844d12f7bfcSMarcin Wojtas 		return (0);
3845d12f7bfcSMarcin Wojtas 
3846d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3847d12f7bfcSMarcin Wojtas 		return (0);
3848d12f7bfcSMarcin Wojtas 
3849d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
3850d12f7bfcSMarcin Wojtas 
3851d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3852d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
3853d12f7bfcSMarcin Wojtas 		    "Queue = %d. Reset the device\n", rx_ring->qid);
3854858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
3855d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3856fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
3857858659f7SMarcin Wojtas 		}
3858d12f7bfcSMarcin Wojtas 		return (EIO);
3859d12f7bfcSMarcin Wojtas 	}
3860d12f7bfcSMarcin Wojtas 
3861d12f7bfcSMarcin Wojtas 	return (0);
3862d12f7bfcSMarcin Wojtas }
3863d12f7bfcSMarcin Wojtas 
3864d12f7bfcSMarcin Wojtas static int
3865d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
386674dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
386774dba3adSMarcin Wojtas {
386874dba3adSMarcin Wojtas 	struct bintime curtime, time;
386974dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
3870d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
387174dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
3872d12f7bfcSMarcin Wojtas 	int i, rc = 0;
387374dba3adSMarcin Wojtas 
387474dba3adSMarcin Wojtas 	getbinuptime(&curtime);
387574dba3adSMarcin Wojtas 
387674dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
387774dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
387874dba3adSMarcin Wojtas 
38790bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
388074dba3adSMarcin Wojtas 			continue;
388174dba3adSMarcin Wojtas 
388274dba3adSMarcin Wojtas 		time = curtime;
388374dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
3884d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
3885d12f7bfcSMarcin Wojtas 
3886d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
3887d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
3888d12f7bfcSMarcin Wojtas 			/*
3889d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
3890d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
3891d12f7bfcSMarcin Wojtas 			 */
3892d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
3893d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
3894d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
3895858659f7SMarcin Wojtas 			if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET,
3896858659f7SMarcin Wojtas 			    adapter))) {
3897858659f7SMarcin Wojtas 				adapter->reset_reason =
3898858659f7SMarcin Wojtas 				    ENA_REGS_RESET_MISS_INTERRUPT;
3899858659f7SMarcin Wojtas 				ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET,
3900858659f7SMarcin Wojtas 				    adapter);
3901858659f7SMarcin Wojtas 			}
3902d12f7bfcSMarcin Wojtas 			return (EIO);
3903d12f7bfcSMarcin Wojtas 		}
390474dba3adSMarcin Wojtas 
390574dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
3906d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
390774dba3adSMarcin Wojtas 
390874dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
390974dba3adSMarcin Wojtas 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
391074dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
391174dba3adSMarcin Wojtas 				    tx_ring->qid, i);
391274dba3adSMarcin Wojtas 
391374dba3adSMarcin Wojtas 			tx_buf->print_once = true;
391474dba3adSMarcin Wojtas 			missed_tx++;
3915d12f7bfcSMarcin Wojtas 		}
3916d12f7bfcSMarcin Wojtas 	}
391774dba3adSMarcin Wojtas 
3918d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
391974dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
3920d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
3921d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
39224e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
3923858659f7SMarcin Wojtas 		if (likely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
3924d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_TX_CMPL;
3925fd43fd2aSMarcin Wojtas 			ENA_FLAG_SET_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
3926858659f7SMarcin Wojtas 		}
3927d12f7bfcSMarcin Wojtas 		rc = EIO;
392874dba3adSMarcin Wojtas 	}
392974dba3adSMarcin Wojtas 
3930d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
3931d12f7bfcSMarcin Wojtas 
3932d12f7bfcSMarcin Wojtas 	return (rc);
393374dba3adSMarcin Wojtas }
393474dba3adSMarcin Wojtas 
39359b8d05b8SZbigniew Bodek /*
39369b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
39379b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
39389b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
39399b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
39409b8d05b8SZbigniew Bodek  */
39410bdffe59SMarcin Wojtas static void
3942d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
39439b8d05b8SZbigniew Bodek {
39449b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
3945d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
394674dba3adSMarcin Wojtas 	int i, budget, rc;
39479b8d05b8SZbigniew Bodek 
39489b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
39499b8d05b8SZbigniew Bodek 	rmb();
39509b8d05b8SZbigniew Bodek 
3951fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
39529b8d05b8SZbigniew Bodek 		return;
39539b8d05b8SZbigniew Bodek 
3954fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
39559b8d05b8SZbigniew Bodek 		return;
39569b8d05b8SZbigniew Bodek 
395740621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
39589b8d05b8SZbigniew Bodek 		return;
39599b8d05b8SZbigniew Bodek 
39609b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
39619b8d05b8SZbigniew Bodek 
39629b8d05b8SZbigniew Bodek 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
39639b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
3964d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
39659b8d05b8SZbigniew Bodek 
3966d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3967d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
3968d12f7bfcSMarcin Wojtas 			return;
3969d12f7bfcSMarcin Wojtas 
3970d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
39710bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
39729b8d05b8SZbigniew Bodek 			return;
39739b8d05b8SZbigniew Bodek 
39749b8d05b8SZbigniew Bodek 		budget--;
3975cd5d5804SMarcin Wojtas 		if (budget == 0) {
39769b8d05b8SZbigniew Bodek 			i++;
39779b8d05b8SZbigniew Bodek 			break;
39789b8d05b8SZbigniew Bodek 		}
39799b8d05b8SZbigniew Bodek 	}
39809b8d05b8SZbigniew Bodek 
39819b8d05b8SZbigniew Bodek 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
39829b8d05b8SZbigniew Bodek }
39839b8d05b8SZbigniew Bodek 
39845cb9db07SMarcin Wojtas /* trigger rx cleanup after 2 consecutive detections */
3985efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
3986efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
3987efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
3988efe6ab18SMarcin Wojtas  * for example).
3989efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
3990efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
3991efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
3992efe6ab18SMarcin Wojtas  * able to send new packets.
3993efe6ab18SMarcin Wojtas  *
3994efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
3995efe6ab18SMarcin Wojtas  */
3996efe6ab18SMarcin Wojtas static void
3997efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
3998efe6ab18SMarcin Wojtas {
3999efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
4000efe6ab18SMarcin Wojtas 	int i, refill_required;
4001efe6ab18SMarcin Wojtas 
4002fd43fd2aSMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
4003efe6ab18SMarcin Wojtas 		return;
4004efe6ab18SMarcin Wojtas 
4005fd43fd2aSMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))
4006efe6ab18SMarcin Wojtas 		return;
4007efe6ab18SMarcin Wojtas 
4008efe6ab18SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
4009efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
4010efe6ab18SMarcin Wojtas 
4011efe6ab18SMarcin Wojtas 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
4012efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
4013efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
4014efe6ab18SMarcin Wojtas 
4015efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
4016efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
4017efe6ab18SMarcin Wojtas 				    1);
4018efe6ab18SMarcin Wojtas 
4019efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
4020efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
4021efe6ab18SMarcin Wojtas 
40225cb9db07SMarcin Wojtas 				taskqueue_enqueue(rx_ring->que->cleanup_tq,
40235cb9db07SMarcin Wojtas 				    &rx_ring->que->cleanup_task);
4024efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
4025efe6ab18SMarcin Wojtas 			}
4026efe6ab18SMarcin Wojtas 		} else {
4027efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
4028efe6ab18SMarcin Wojtas 		}
4029efe6ab18SMarcin Wojtas 	}
4030efe6ab18SMarcin Wojtas }
40319b8d05b8SZbigniew Bodek 
403240621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
403340621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
403440621d71SMarcin Wojtas {
403540621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
403640621d71SMarcin Wojtas 
403740621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
403840621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
403940621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
404040621d71SMarcin Wojtas 
404140621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
404240621d71SMarcin Wojtas 		/* convert to usec */
404340621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
404440621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
404540621d71SMarcin Wojtas 
404640621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
404740621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
404840621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
404940621d71SMarcin Wojtas 
405040621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
405140621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
405240621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
405340621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
405440621d71SMarcin Wojtas 		else
405540621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
405640621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
405740621d71SMarcin Wojtas 	}
405840621d71SMarcin Wojtas 
405940621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
406040621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
406140621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
406240621d71SMarcin Wojtas 		else
406340621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
406440621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
406540621d71SMarcin Wojtas 	}
406640621d71SMarcin Wojtas }
406740621d71SMarcin Wojtas 
40689b8d05b8SZbigniew Bodek static void
40699b8d05b8SZbigniew Bodek ena_timer_service(void *data)
40709b8d05b8SZbigniew Bodek {
40719b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
40729b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
40739b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
40749b8d05b8SZbigniew Bodek 
40759b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
40769b8d05b8SZbigniew Bodek 
40779b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
40789b8d05b8SZbigniew Bodek 
4079d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
40809b8d05b8SZbigniew Bodek 
4081efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
4082efe6ab18SMarcin Wojtas 
40830bdffe59SMarcin Wojtas 	if (host_info != NULL)
40849b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
40859b8d05b8SZbigniew Bodek 
4086fd43fd2aSMarcin Wojtas 	if (unlikely(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
40879b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
40889b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
40899b8d05b8SZbigniew Bodek 		return;
40909b8d05b8SZbigniew Bodek 	}
40919b8d05b8SZbigniew Bodek 
40929b8d05b8SZbigniew Bodek 	/*
40939b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
40949b8d05b8SZbigniew Bodek 	 */
40959b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
40969b8d05b8SZbigniew Bodek }
40979b8d05b8SZbigniew Bodek 
40989b8d05b8SZbigniew Bodek static void
409932f63fa7SMarcin Wojtas ena_destroy_device(struct ena_adapter *adapter, bool graceful)
41009b8d05b8SZbigniew Bodek {
410132f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
41029b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
41039b8d05b8SZbigniew Bodek 	bool dev_up;
410432f63fa7SMarcin Wojtas 
410532f63fa7SMarcin Wojtas 	if (!ENA_FLAG_ISSET(ENA_FLAG_DEVICE_RUNNING, adapter))
410632f63fa7SMarcin Wojtas 		return;
410732f63fa7SMarcin Wojtas 
410832f63fa7SMarcin Wojtas 	if_link_state_change(ifp, LINK_STATE_DOWN);
410932f63fa7SMarcin Wojtas 
411032f63fa7SMarcin Wojtas 	callout_drain(&adapter->timer_service);
411132f63fa7SMarcin Wojtas 
411232f63fa7SMarcin Wojtas 	dev_up = ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter);
411332f63fa7SMarcin Wojtas 	if (dev_up)
411432f63fa7SMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
411532f63fa7SMarcin Wojtas 	else
411632f63fa7SMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter);
411732f63fa7SMarcin Wojtas 
411832f63fa7SMarcin Wojtas 	if (!graceful)
411932f63fa7SMarcin Wojtas 		ena_com_set_admin_running_state(ena_dev, false);
412032f63fa7SMarcin Wojtas 
412132f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP, adapter))
412232f63fa7SMarcin Wojtas 		ena_down(adapter);
412332f63fa7SMarcin Wojtas 
412432f63fa7SMarcin Wojtas 	/*
412532f63fa7SMarcin Wojtas 	 * Stop the device from sending AENQ events (if the device was up, and
412632f63fa7SMarcin Wojtas 	 * the trigger reset was on, ena_down already performs device reset)
412732f63fa7SMarcin Wojtas 	 */
412832f63fa7SMarcin Wojtas 	if (!(ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter) && dev_up))
412932f63fa7SMarcin Wojtas 		ena_com_dev_reset(adapter->ena_dev, adapter->reset_reason);
413032f63fa7SMarcin Wojtas 
413132f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
413232f63fa7SMarcin Wojtas 
413332f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
413432f63fa7SMarcin Wojtas 
413532f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
413632f63fa7SMarcin Wojtas 
413732f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
413832f63fa7SMarcin Wojtas 
413932f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
414032f63fa7SMarcin Wojtas 
414132f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
414232f63fa7SMarcin Wojtas 
414332f63fa7SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
414432f63fa7SMarcin Wojtas 
414532f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_TRIGGER_RESET, adapter);
414632f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
414732f63fa7SMarcin Wojtas }
414832f63fa7SMarcin Wojtas 
414932f63fa7SMarcin Wojtas static int
415032f63fa7SMarcin Wojtas ena_device_validate_params(struct ena_adapter *adapter,
415132f63fa7SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
415232f63fa7SMarcin Wojtas {
415332f63fa7SMarcin Wojtas 
415432f63fa7SMarcin Wojtas 	if (memcmp(get_feat_ctx->dev_attr.mac_addr, adapter->mac_addr,
415532f63fa7SMarcin Wojtas 	    ETHER_ADDR_LEN) != 0) {
415632f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
415732f63fa7SMarcin Wojtas 		    "Error, mac address are different\n");
415832f63fa7SMarcin Wojtas 		return (EINVAL);
415932f63fa7SMarcin Wojtas 	}
416032f63fa7SMarcin Wojtas 
416132f63fa7SMarcin Wojtas 	if (get_feat_ctx->dev_attr.max_mtu < if_getmtu(adapter->ifp)) {
416232f63fa7SMarcin Wojtas 		device_printf(adapter->pdev,
416332f63fa7SMarcin Wojtas 		    "Error, device max mtu is smaller than ifp MTU\n");
416432f63fa7SMarcin Wojtas 		return (EINVAL);
416532f63fa7SMarcin Wojtas 	}
416632f63fa7SMarcin Wojtas 
416732f63fa7SMarcin Wojtas 	return 0;
416832f63fa7SMarcin Wojtas }
416932f63fa7SMarcin Wojtas 
417032f63fa7SMarcin Wojtas static int
417132f63fa7SMarcin Wojtas ena_restore_device(struct ena_adapter *adapter)
417232f63fa7SMarcin Wojtas {
417332f63fa7SMarcin Wojtas 	struct ena_com_dev_get_features_ctx get_feat_ctx;
417432f63fa7SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
417532f63fa7SMarcin Wojtas 	if_t ifp = adapter->ifp;
417632f63fa7SMarcin Wojtas 	device_t dev = adapter->pdev;
417732f63fa7SMarcin Wojtas 	int wd_active;
41789b8d05b8SZbigniew Bodek 	int rc;
41799b8d05b8SZbigniew Bodek 
418032f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
418132f63fa7SMarcin Wojtas 
418232f63fa7SMarcin Wojtas 	rc = ena_device_init(adapter, dev, &get_feat_ctx, &wd_active);
418332f63fa7SMarcin Wojtas 	if (rc != 0) {
418432f63fa7SMarcin Wojtas 		device_printf(dev, "Cannot initialize device\n");
418532f63fa7SMarcin Wojtas 		goto err;
418632f63fa7SMarcin Wojtas 	}
418732f63fa7SMarcin Wojtas 	/*
418832f63fa7SMarcin Wojtas 	 * Only enable WD if it was enabled before reset, so it won't override
418932f63fa7SMarcin Wojtas 	 * value set by the user by the sysctl.
419032f63fa7SMarcin Wojtas 	 */
419132f63fa7SMarcin Wojtas 	if (adapter->wd_active != 0)
419232f63fa7SMarcin Wojtas 		adapter->wd_active = wd_active;
419332f63fa7SMarcin Wojtas 
419432f63fa7SMarcin Wojtas 	rc = ena_device_validate_params(adapter, &get_feat_ctx);
419532f63fa7SMarcin Wojtas 	if (rc != 0) {
419632f63fa7SMarcin Wojtas 		device_printf(dev, "Validation of device parameters failed\n");
419732f63fa7SMarcin Wojtas 		goto err_device_destroy;
419832f63fa7SMarcin Wojtas 	}
419932f63fa7SMarcin Wojtas 
420032f63fa7SMarcin Wojtas 	rc = ena_handle_updated_queues(adapter, &get_feat_ctx);
420132f63fa7SMarcin Wojtas 	if (rc != 0)
420232f63fa7SMarcin Wojtas 		goto err_device_destroy;
420332f63fa7SMarcin Wojtas 
420432f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
420532f63fa7SMarcin Wojtas 	/* Make sure we don't have a race with AENQ Links state handler */
420632f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_LINK_UP, adapter))
420732f63fa7SMarcin Wojtas 		if_link_state_change(ifp, LINK_STATE_UP);
420832f63fa7SMarcin Wojtas 
420932f63fa7SMarcin Wojtas 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
421032f63fa7SMarcin Wojtas 	    adapter->num_queues);
421132f63fa7SMarcin Wojtas 	if (rc != 0) {
421232f63fa7SMarcin Wojtas 		device_printf(dev, "Enable MSI-X failed\n");
421332f63fa7SMarcin Wojtas 		goto err_device_destroy;
421432f63fa7SMarcin Wojtas 	}
421532f63fa7SMarcin Wojtas 
421632f63fa7SMarcin Wojtas 	/* If the interface was up before the reset bring it up */
421732f63fa7SMarcin Wojtas 	if (ENA_FLAG_ISSET(ENA_FLAG_DEV_UP_BEFORE_RESET, adapter)) {
421832f63fa7SMarcin Wojtas 		rc = ena_up(adapter);
421932f63fa7SMarcin Wojtas 		if (rc != 0) {
422032f63fa7SMarcin Wojtas 			device_printf(dev, "Failed to create I/O queues\n");
422132f63fa7SMarcin Wojtas 			goto err_disable_msix;
422232f63fa7SMarcin Wojtas 		}
422332f63fa7SMarcin Wojtas 	}
422432f63fa7SMarcin Wojtas 
422532f63fa7SMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
422632f63fa7SMarcin Wojtas 	callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
422732f63fa7SMarcin Wojtas 	    ena_timer_service, (void *)adapter, 0);
422832f63fa7SMarcin Wojtas 
422932f63fa7SMarcin Wojtas 	device_printf(dev,
423032f63fa7SMarcin Wojtas 	    "Device reset completed successfully, Driver info: %s\n", ena_version);
423132f63fa7SMarcin Wojtas 
423232f63fa7SMarcin Wojtas 	return (rc);
423332f63fa7SMarcin Wojtas 
423432f63fa7SMarcin Wojtas err_disable_msix:
423532f63fa7SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
423632f63fa7SMarcin Wojtas 	ena_disable_msix(adapter);
423732f63fa7SMarcin Wojtas err_device_destroy:
423832f63fa7SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
423932f63fa7SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
424032f63fa7SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
424132f63fa7SMarcin Wojtas 	ena_com_dev_reset(ena_dev, ENA_REGS_RESET_DRIVER_INVALID_STATE);
424232f63fa7SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
424332f63fa7SMarcin Wojtas err:
424432f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
424532f63fa7SMarcin Wojtas 	ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_ONGOING_RESET, adapter);
424632f63fa7SMarcin Wojtas 	device_printf(dev, "Reset attempt failed. Can not reset the device\n");
424732f63fa7SMarcin Wojtas 
424832f63fa7SMarcin Wojtas 	return (rc);
424932f63fa7SMarcin Wojtas }
425032f63fa7SMarcin Wojtas 
425132f63fa7SMarcin Wojtas static void
425232f63fa7SMarcin Wojtas ena_reset_task(void *arg, int pending)
425332f63fa7SMarcin Wojtas {
425432f63fa7SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
425532f63fa7SMarcin Wojtas 
4256fd43fd2aSMarcin Wojtas 	if (unlikely(!ENA_FLAG_ISSET(ENA_FLAG_TRIGGER_RESET, adapter))) {
42579b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
42589b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
42599b8d05b8SZbigniew Bodek 		return;
42609b8d05b8SZbigniew Bodek 	}
42619b8d05b8SZbigniew Bodek 
42629b8d05b8SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
426332f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, false);
426432f63fa7SMarcin Wojtas 	ena_restore_device(adapter);
42659b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
42669b8d05b8SZbigniew Bodek }
42679b8d05b8SZbigniew Bodek 
42689b8d05b8SZbigniew Bodek /**
42699b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
42709b8d05b8SZbigniew Bodek  * @pdev: device information struct
42719b8d05b8SZbigniew Bodek  *
42729b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
42739b8d05b8SZbigniew Bodek  *
42749b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
42759b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
42769b8d05b8SZbigniew Bodek  * and a hardware reset occur.
42779b8d05b8SZbigniew Bodek  **/
42789b8d05b8SZbigniew Bodek static int
42799b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
42809b8d05b8SZbigniew Bodek {
42819b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
42824fa9e02dSMarcin Wojtas 	struct ena_llq_configurations llq_config;
42836064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
42849b8d05b8SZbigniew Bodek 	static int version_printed;
42859b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
42869b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
42874fa9e02dSMarcin Wojtas 	const char *queue_type_str;
42889b8d05b8SZbigniew Bodek 	int io_queue_num;
42894fa9e02dSMarcin Wojtas 	int rid, rc;
42904fa9e02dSMarcin Wojtas 
42919b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
42929b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
42939b8d05b8SZbigniew Bodek 
42949b8d05b8SZbigniew Bodek 	mtx_init(&adapter->global_mtx, "ENA global mtx", NULL, MTX_DEF);
42959b8d05b8SZbigniew Bodek 	sx_init(&adapter->ioctl_sx, "ENA ioctl sx");
42969b8d05b8SZbigniew Bodek 
42979b8d05b8SZbigniew Bodek 	/* Set up the timer service */
42989b8d05b8SZbigniew Bodek 	callout_init_mtx(&adapter->timer_service, &adapter->global_mtx, 0);
42999b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
43009b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
43019b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
43029b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
43039b8d05b8SZbigniew Bodek 
43049b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
43059b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
43069b8d05b8SZbigniew Bodek 
43079b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
4308cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
4309cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
43109b8d05b8SZbigniew Bodek 
43119b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
43129b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
43134fa9e02dSMarcin Wojtas 
43144fa9e02dSMarcin Wojtas 	rid = PCIR_BAR(ENA_REG_BAR);
43154fa9e02dSMarcin Wojtas 	adapter->memory = NULL;
43164fa9e02dSMarcin Wojtas 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
43174fa9e02dSMarcin Wojtas 	    &rid, RF_ACTIVE);
43184fa9e02dSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
43194fa9e02dSMarcin Wojtas 		device_printf(pdev,
43204fa9e02dSMarcin Wojtas 		    "unable to allocate bus resource: registers!\n");
43214fa9e02dSMarcin Wojtas 		rc = ENOMEM;
43224fa9e02dSMarcin Wojtas 		goto err_dev_free;
43234fa9e02dSMarcin Wojtas 	}
43244fa9e02dSMarcin Wojtas 
43259b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
43269b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
43279b8d05b8SZbigniew Bodek 
43289b8d05b8SZbigniew Bodek 	/* Store register resources */
43299b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
43309b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
43319b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
43329b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
43339b8d05b8SZbigniew Bodek 
43343f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
43359b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
43369b8d05b8SZbigniew Bodek 		rc = ENXIO;
4337cd5d5804SMarcin Wojtas 		goto err_bus_free;
43389b8d05b8SZbigniew Bodek 	}
43399b8d05b8SZbigniew Bodek 
43409b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
43419b8d05b8SZbigniew Bodek 
4342fd43fd2aSMarcin Wojtas 	/* Initially clear all the flags */
4343fd43fd2aSMarcin Wojtas 	ENA_FLAG_ZERO(adapter);
4344fd43fd2aSMarcin Wojtas 
43459b8d05b8SZbigniew Bodek 	/* Device initialization */
43469b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
43473f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
43489b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
43499b8d05b8SZbigniew Bodek 		rc = ENXIO;
43509b8d05b8SZbigniew Bodek 		goto err_bus_free;
43519b8d05b8SZbigniew Bodek 	}
43529b8d05b8SZbigniew Bodek 
43534fa9e02dSMarcin Wojtas 	set_default_llq_configurations(&llq_config);
43544fa9e02dSMarcin Wojtas 
43554fa9e02dSMarcin Wojtas 	rc = ena_set_queues_placement_policy(pdev, ena_dev, &get_feat_ctx.llq,
43564fa9e02dSMarcin Wojtas 	     &llq_config);
43574fa9e02dSMarcin Wojtas 	if (unlikely(rc != 0)) {
43584fa9e02dSMarcin Wojtas 		device_printf(pdev, "failed to set placement policy\n");
43594fa9e02dSMarcin Wojtas 		goto err_com_free;
43604fa9e02dSMarcin Wojtas 	}
43614fa9e02dSMarcin Wojtas 
43624fa9e02dSMarcin Wojtas 	if (ena_dev->tx_mem_queue_type == ENA_ADMIN_PLACEMENT_POLICY_HOST)
43634fa9e02dSMarcin Wojtas 		queue_type_str = "Regular";
43644fa9e02dSMarcin Wojtas 	else
43654fa9e02dSMarcin Wojtas 		queue_type_str = "Low Latency";
43664fa9e02dSMarcin Wojtas 	device_printf(pdev, "Placement policy: %s\n", queue_type_str);
43674fa9e02dSMarcin Wojtas 
43689b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
43699b8d05b8SZbigniew Bodek 
43709b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
43719b8d05b8SZbigniew Bodek 
43729b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
43739b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
43749b8d05b8SZbigniew Bodek 
43756064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
43766064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
43776064f289SMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
43786064f289SMarcin Wojtas 
43799b8d05b8SZbigniew Bodek 	/* calculate IO queue number to create */
43809b8d05b8SZbigniew Bodek 	io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx);
43819b8d05b8SZbigniew Bodek 
43829b8d05b8SZbigniew Bodek 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
43839b8d05b8SZbigniew Bodek 	    io_queue_num);
43849b8d05b8SZbigniew Bodek 	adapter->num_queues = io_queue_num;
43859b8d05b8SZbigniew Bodek 
43863cfadb28SMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
43876064f289SMarcin Wojtas 	// Set the requested Rx ring size
43886064f289SMarcin Wojtas 	adapter->rx_ring_size = ENA_DEFAULT_RING_SIZE;
43899b8d05b8SZbigniew Bodek 	/* calculatre ring sizes */
43906064f289SMarcin Wojtas 	rc = ena_calc_queue_size(adapter, &calc_queue_ctx);
43916064f289SMarcin Wojtas 	if (unlikely((rc != 0) || (io_queue_num <= 0))) {
43926064f289SMarcin Wojtas 		rc = EFAULT;
43939b8d05b8SZbigniew Bodek 		goto err_com_free;
43949b8d05b8SZbigniew Bodek 	}
43959b8d05b8SZbigniew Bodek 
4396a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
4397a195fab0SMarcin Wojtas 
43986064f289SMarcin Wojtas 	adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
43996064f289SMarcin Wojtas 	adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
44009b8d05b8SZbigniew Bodek 
44016064f289SMarcin Wojtas 	adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
44026064f289SMarcin Wojtas 	adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
44036064f289SMarcin Wojtas 
44046064f289SMarcin Wojtas 	adapter->buf_ring_size = ENA_DEFAULT_BUF_RING_SIZE;
44059b8d05b8SZbigniew Bodek 
44069b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
44079b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
44084e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
44094e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
4410cd5d5804SMarcin Wojtas 		goto err_com_free;
44114e8acd84SMarcin Wojtas 	}
44129b8d05b8SZbigniew Bodek 
44139b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
44144e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
44154e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
4416cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
44174e8acd84SMarcin Wojtas 	}
44189b8d05b8SZbigniew Bodek 
44199b8d05b8SZbigniew Bodek 	/* initialize rings basic information */
44206064f289SMarcin Wojtas 	device_printf(pdev,
44216064f289SMarcin Wojtas 	    "Creating %d io queues. Rx queue size: %d, Tx queue size: %d\n",
44226064f289SMarcin Wojtas 	    io_queue_num,
44236064f289SMarcin Wojtas 	    calc_queue_ctx.rx_queue_size,
44246064f289SMarcin Wojtas 	    calc_queue_ctx.tx_queue_size);
4425cd5d5804SMarcin Wojtas 	ena_init_io_rings(adapter);
44269b8d05b8SZbigniew Bodek 
44279b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
44283f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
44299b8d05b8SZbigniew Bodek 		device_printf(pdev,
44309b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
4431c115a1e2SMarcin Wojtas 		goto err_io_free;
4432c115a1e2SMarcin Wojtas 	}
4433c115a1e2SMarcin Wojtas 
4434c115a1e2SMarcin Wojtas 	/* setup network interface */
4435c115a1e2SMarcin Wojtas 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
4436c115a1e2SMarcin Wojtas 	if (unlikely(rc != 0)) {
4437c115a1e2SMarcin Wojtas 		device_printf(pdev, "Error with network interface setup\n");
4438c115a1e2SMarcin Wojtas 		goto err_msix_free;
44399b8d05b8SZbigniew Bodek 	}
44409b8d05b8SZbigniew Bodek 
4441081169f2SZbigniew Bodek 	/* Initialize reset task queue */
4442081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
4443081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
4444081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
4445081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
4446081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
4447081169f2SZbigniew Bodek 
44489b8d05b8SZbigniew Bodek 	/* Initialize statistics */
44499b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
44509b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
445130217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
445230217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
44539b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
44549b8d05b8SZbigniew Bodek 
44559b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
44569b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
4457fd43fd2aSMarcin Wojtas 	ENA_FLAG_SET_ATOMIC(ENA_FLAG_DEVICE_RUNNING, adapter);
44589b8d05b8SZbigniew Bodek 
44599b8d05b8SZbigniew Bodek 	return (0);
44609b8d05b8SZbigniew Bodek 
4461c115a1e2SMarcin Wojtas err_msix_free:
4462c115a1e2SMarcin Wojtas 	ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR);
4463c115a1e2SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
4464c115a1e2SMarcin Wojtas 	ena_disable_msix(adapter);
4465cd5d5804SMarcin Wojtas err_io_free:
44669b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
44679b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
4468cd5d5804SMarcin Wojtas err_tx_tag_free:
44699b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
4470cd5d5804SMarcin Wojtas err_com_free:
44719b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
44729b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
4473cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
44749b8d05b8SZbigniew Bodek err_bus_free:
44759b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
44769b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
44774fa9e02dSMarcin Wojtas err_dev_free:
44784fa9e02dSMarcin Wojtas 	free(ena_dev, M_DEVBUF);
4479cd5d5804SMarcin Wojtas 
44809b8d05b8SZbigniew Bodek 	return (rc);
44819b8d05b8SZbigniew Bodek }
44829b8d05b8SZbigniew Bodek 
44839b8d05b8SZbigniew Bodek /**
44849b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
44859b8d05b8SZbigniew Bodek  * @pdev: device information struct
44869b8d05b8SZbigniew Bodek  *
44879b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
44889b8d05b8SZbigniew Bodek  * that it should release a PCI device.
44899b8d05b8SZbigniew Bodek  **/
44909b8d05b8SZbigniew Bodek static int
44919b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
44929b8d05b8SZbigniew Bodek {
44939b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
44949b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
44959b8d05b8SZbigniew Bodek 	int rc;
44969b8d05b8SZbigniew Bodek 
44979b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
44989b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
44999b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
45009b8d05b8SZbigniew Bodek 		return (EBUSY);
45019b8d05b8SZbigniew Bodek 	}
45029b8d05b8SZbigniew Bodek 
45039151c55dSMarcin Wojtas 	ether_ifdetach(adapter->ifp);
45049151c55dSMarcin Wojtas 
45059b8d05b8SZbigniew Bodek 	/* Free reset task and callout */
45069b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
45079b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
45089b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
45099b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
45109b8d05b8SZbigniew Bodek 
4511e67c6554SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
45129b8d05b8SZbigniew Bodek 	ena_down(adapter);
451332f63fa7SMarcin Wojtas 	ena_destroy_device(adapter, true);
4514e67c6554SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
45159b8d05b8SZbigniew Bodek 
45169b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
45179b8d05b8SZbigniew Bodek 
451830217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
451930217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
45209b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
45219b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
45229b8d05b8SZbigniew Bodek 
45239b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
45243f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
45259b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
45269b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
45279b8d05b8SZbigniew Bodek 
45289b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
45293f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
45309b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
45319b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
45329b8d05b8SZbigniew Bodek 
45339b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
45349b8d05b8SZbigniew Bodek 
45359b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
45369b8d05b8SZbigniew Bodek 
453732f63fa7SMarcin Wojtas 	if (likely(ENA_FLAG_ISSET(ENA_FLAG_RSS_ACTIVE, adapter)))
453832f63fa7SMarcin Wojtas 		ena_com_rss_destroy(ena_dev);
453932f63fa7SMarcin Wojtas 
454032f63fa7SMarcin Wojtas 	ena_com_delete_host_info(ena_dev);
454132f63fa7SMarcin Wojtas 
45429b8d05b8SZbigniew Bodek 	mtx_destroy(&adapter->global_mtx);
45439b8d05b8SZbigniew Bodek 	sx_destroy(&adapter->ioctl_sx);
45449b8d05b8SZbigniew Bodek 
45459151c55dSMarcin Wojtas 	if_free(adapter->ifp);
45469151c55dSMarcin Wojtas 
45479b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
45489b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
45499b8d05b8SZbigniew Bodek 
45509b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
45519b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
45529b8d05b8SZbigniew Bodek 
45539b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
45549b8d05b8SZbigniew Bodek }
45559b8d05b8SZbigniew Bodek 
45569b8d05b8SZbigniew Bodek /******************************************************************************
45579b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
45589b8d05b8SZbigniew Bodek  *****************************************************************************/
45599b8d05b8SZbigniew Bodek /**
45609b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
45619b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
45629b8d05b8SZbigniew Bodek  **/
45639b8d05b8SZbigniew Bodek static void
45649b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
45659b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
45669b8d05b8SZbigniew Bodek {
45679b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
45689b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
45699b8d05b8SZbigniew Bodek 	int status;
45709b8d05b8SZbigniew Bodek 	if_t ifp;
45719b8d05b8SZbigniew Bodek 
45729b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
45739b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
45749b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
45759b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
45769b8d05b8SZbigniew Bodek 
45779b8d05b8SZbigniew Bodek 	if (status != 0) {
45789b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
4579fd43fd2aSMarcin Wojtas 		ENA_FLAG_SET_ATOMIC(ENA_FLAG_LINK_UP, adapter);
458032f63fa7SMarcin Wojtas 		if (!ENA_FLAG_ISSET(ENA_FLAG_ONGOING_RESET, adapter))
458132f63fa7SMarcin Wojtas 			if_link_state_change(ifp, LINK_STATE_UP);
458232f63fa7SMarcin Wojtas 	} else {
45839b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
45849b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
4585fd43fd2aSMarcin Wojtas 		ENA_FLAG_CLEAR_ATOMIC(ENA_FLAG_LINK_UP, adapter);
45869b8d05b8SZbigniew Bodek 	}
45879b8d05b8SZbigniew Bodek }
45889b8d05b8SZbigniew Bodek 
458940621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
459040621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
459140621d71SMarcin Wojtas {
459240621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
459340621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
459440621d71SMarcin Wojtas 
459540621d71SMarcin Wojtas 	ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
459640621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
459740621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
459840621d71SMarcin Wojtas 
459940621d71SMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrom) {
460040621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
460140621d71SMarcin Wojtas 		hints =
460240621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
460340621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
460440621d71SMarcin Wojtas 		break;
460540621d71SMarcin Wojtas 	default:
460640621d71SMarcin Wojtas 		device_printf(adapter->pdev,
460740621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
460840621d71SMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrom);
460940621d71SMarcin Wojtas 	}
461040621d71SMarcin Wojtas }
461140621d71SMarcin Wojtas 
46129b8d05b8SZbigniew Bodek /**
46139b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
46149b8d05b8SZbigniew Bodek  **/
46159b8d05b8SZbigniew Bodek static void
4616e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
46179b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
46189b8d05b8SZbigniew Bodek {
4619e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4620e6de9a83SMarcin Wojtas 
4621e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
4622e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
46239b8d05b8SZbigniew Bodek }
46249b8d05b8SZbigniew Bodek 
46259b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
46269b8d05b8SZbigniew Bodek     .handlers = {
46279b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
462840621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
46299b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
46309b8d05b8SZbigniew Bodek     },
46319b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
46329b8d05b8SZbigniew Bodek };
46339b8d05b8SZbigniew Bodek 
46349b8d05b8SZbigniew Bodek /*********************************************************************
46359b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
46369b8d05b8SZbigniew Bodek  *********************************************************************/
46379b8d05b8SZbigniew Bodek 
46389b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
46399b8d05b8SZbigniew Bodek     /* Device interface */
46409b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
46419b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
46429b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
46439b8d05b8SZbigniew Bodek     DEVMETHOD_END
46449b8d05b8SZbigniew Bodek };
46459b8d05b8SZbigniew Bodek 
46469b8d05b8SZbigniew Bodek static driver_t ena_driver = {
46479b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
46489b8d05b8SZbigniew Bodek };
46499b8d05b8SZbigniew Bodek 
46509b8d05b8SZbigniew Bodek devclass_t ena_devclass;
46519b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
465240abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
4653329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
46549b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
46559b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
46569b8d05b8SZbigniew Bodek 
46579b8d05b8SZbigniew Bodek /*********************************************************************/
4658