xref: /freebsd/sys/dev/ena/ena.c (revision 5cb9db0706fe8cf89eb6c9ca1787eff2057bfd6f)
19b8d05b8SZbigniew Bodek /*-
29b8d05b8SZbigniew Bodek  * BSD LICENSE
39b8d05b8SZbigniew Bodek  *
49b8d05b8SZbigniew Bodek  * Copyright (c) 2015-2017 Amazon.com, Inc. or its affiliates.
59b8d05b8SZbigniew Bodek  * All rights reserved.
69b8d05b8SZbigniew Bodek  *
79b8d05b8SZbigniew Bodek  * Redistribution and use in source and binary forms, with or without
89b8d05b8SZbigniew Bodek  * modification, are permitted provided that the following conditions
99b8d05b8SZbigniew Bodek  * are met:
109b8d05b8SZbigniew Bodek  *
119b8d05b8SZbigniew Bodek  * 1. Redistributions of source code must retain the above copyright
129b8d05b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer.
139b8d05b8SZbigniew Bodek  *
149b8d05b8SZbigniew Bodek  * 2. Redistributions in binary form must reproduce the above copyright
159b8d05b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer in the
169b8d05b8SZbigniew Bodek  *    documentation and/or other materials provided with the distribution.
179b8d05b8SZbigniew Bodek  *
189b8d05b8SZbigniew Bodek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199b8d05b8SZbigniew Bodek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
209b8d05b8SZbigniew Bodek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
219b8d05b8SZbigniew Bodek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
229b8d05b8SZbigniew Bodek  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
239b8d05b8SZbigniew Bodek  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
249b8d05b8SZbigniew Bodek  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
259b8d05b8SZbigniew Bodek  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
269b8d05b8SZbigniew Bodek  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279b8d05b8SZbigniew Bodek  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
289b8d05b8SZbigniew Bodek  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
299b8d05b8SZbigniew Bodek  */
309b8d05b8SZbigniew Bodek #include <sys/cdefs.h>
319b8d05b8SZbigniew Bodek __FBSDID("$FreeBSD$");
329b8d05b8SZbigniew Bodek 
339b8d05b8SZbigniew Bodek #include <sys/param.h>
349b8d05b8SZbigniew Bodek #include <sys/systm.h>
359b8d05b8SZbigniew Bodek #include <sys/bus.h>
369b8d05b8SZbigniew Bodek #include <sys/endian.h>
379b8d05b8SZbigniew Bodek #include <sys/kernel.h>
389b8d05b8SZbigniew Bodek #include <sys/kthread.h>
399b8d05b8SZbigniew Bodek #include <sys/malloc.h>
409b8d05b8SZbigniew Bodek #include <sys/mbuf.h>
419b8d05b8SZbigniew Bodek #include <sys/module.h>
429b8d05b8SZbigniew Bodek #include <sys/rman.h>
439b8d05b8SZbigniew Bodek #include <sys/smp.h>
449b8d05b8SZbigniew Bodek #include <sys/socket.h>
459b8d05b8SZbigniew Bodek #include <sys/sockio.h>
469b8d05b8SZbigniew Bodek #include <sys/sysctl.h>
479b8d05b8SZbigniew Bodek #include <sys/taskqueue.h>
489b8d05b8SZbigniew Bodek #include <sys/time.h>
499b8d05b8SZbigniew Bodek #include <sys/eventhandler.h>
509b8d05b8SZbigniew Bodek 
519b8d05b8SZbigniew Bodek #include <machine/bus.h>
529b8d05b8SZbigniew Bodek #include <machine/resource.h>
539b8d05b8SZbigniew Bodek #include <machine/in_cksum.h>
549b8d05b8SZbigniew Bodek 
559b8d05b8SZbigniew Bodek #include <net/bpf.h>
569b8d05b8SZbigniew Bodek #include <net/ethernet.h>
579b8d05b8SZbigniew Bodek #include <net/if.h>
589b8d05b8SZbigniew Bodek #include <net/if_var.h>
599b8d05b8SZbigniew Bodek #include <net/if_arp.h>
609b8d05b8SZbigniew Bodek #include <net/if_dl.h>
619b8d05b8SZbigniew Bodek #include <net/if_media.h>
629b8d05b8SZbigniew Bodek #include <net/if_types.h>
639b8d05b8SZbigniew Bodek #include <net/if_vlan_var.h>
649b8d05b8SZbigniew Bodek 
659b8d05b8SZbigniew Bodek #include <netinet/in_systm.h>
669b8d05b8SZbigniew Bodek #include <netinet/in.h>
679b8d05b8SZbigniew Bodek #include <netinet/if_ether.h>
689b8d05b8SZbigniew Bodek #include <netinet/ip.h>
699b8d05b8SZbigniew Bodek #include <netinet/ip6.h>
709b8d05b8SZbigniew Bodek #include <netinet/tcp.h>
719b8d05b8SZbigniew Bodek #include <netinet/udp.h>
729b8d05b8SZbigniew Bodek 
739b8d05b8SZbigniew Bodek #include <dev/pci/pcivar.h>
749b8d05b8SZbigniew Bodek #include <dev/pci/pcireg.h>
759b8d05b8SZbigniew Bodek 
769b8d05b8SZbigniew Bodek #include "ena.h"
779b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
789b8d05b8SZbigniew Bodek 
799b8d05b8SZbigniew Bodek /*********************************************************
809b8d05b8SZbigniew Bodek  *  Function prototypes
819b8d05b8SZbigniew Bodek  *********************************************************/
829b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
839b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
849b8d05b8SZbigniew Bodek static int	ena_allocate_pci_resources(struct ena_adapter*);
859b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
869b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
879b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
889b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
899b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
909b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
919b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
92cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
939b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
949b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
959b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
969b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
979b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
989b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
999b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1009b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1019b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1029b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
10343fefd16SMarcin Wojtas static inline int validate_rx_req_id(struct ena_ring *, uint16_t);
1049b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1059b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1069b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1079b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1089b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1099b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1109b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1119b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1129b8d05b8SZbigniew Bodek static int	ena_refill_rx_bufs(struct ena_ring *, uint32_t);
1139b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1149b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1159b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1169b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1179b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1189b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1199b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1209b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1219b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1229b8d05b8SZbigniew Bodek static int	ena_tx_cleanup(struct ena_ring *);
1239b8d05b8SZbigniew Bodek static int	ena_rx_cleanup(struct ena_ring *);
124fceb9387SMarcin Wojtas static inline int validate_tx_req_id(struct ena_ring *, uint16_t);
1259b8d05b8SZbigniew Bodek static void	ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *,
1269b8d05b8SZbigniew Bodek     struct mbuf *);
1279b8d05b8SZbigniew Bodek static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *,
1289b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *, uint16_t *);
1299b8d05b8SZbigniew Bodek static inline void ena_rx_checksum(struct ena_ring *, struct ena_com_rx_ctx *,
1309b8d05b8SZbigniew Bodek     struct mbuf *);
131*5cb9db07SMarcin Wojtas static void	ena_cleanup(void *arg, int pending);
132*5cb9db07SMarcin Wojtas static int	ena_handle_msix(void *);
1339b8d05b8SZbigniew Bodek static int	ena_enable_msix(struct ena_adapter *);
1349b8d05b8SZbigniew Bodek static void	ena_setup_mgmnt_intr(struct ena_adapter *);
1359b8d05b8SZbigniew Bodek static void	ena_setup_io_intr(struct ena_adapter *);
1369b8d05b8SZbigniew Bodek static int	ena_request_mgmnt_irq(struct ena_adapter *);
1379b8d05b8SZbigniew Bodek static int	ena_request_io_irq(struct ena_adapter *);
1389b8d05b8SZbigniew Bodek static void	ena_free_mgmnt_irq(struct ena_adapter *);
1399b8d05b8SZbigniew Bodek static void	ena_free_io_irq(struct ena_adapter *);
1409b8d05b8SZbigniew Bodek static void	ena_free_irqs(struct ena_adapter*);
1419b8d05b8SZbigniew Bodek static void	ena_disable_msix(struct ena_adapter *);
1429b8d05b8SZbigniew Bodek static void	ena_unmask_all_io_irqs(struct ena_adapter *);
1439b8d05b8SZbigniew Bodek static int	ena_rss_configure(struct ena_adapter *);
1449b8d05b8SZbigniew Bodek static int	ena_up_complete(struct ena_adapter *);
1459b8d05b8SZbigniew Bodek static int	ena_up(struct ena_adapter *);
1469b8d05b8SZbigniew Bodek static void	ena_down(struct ena_adapter *);
1479b8d05b8SZbigniew Bodek static uint64_t	ena_get_counter(if_t, ift_counter);
1489b8d05b8SZbigniew Bodek static int	ena_media_change(if_t);
1499b8d05b8SZbigniew Bodek static void	ena_media_status(if_t, struct ifmediareq *);
1509b8d05b8SZbigniew Bodek static void	ena_init(void *);
1519b8d05b8SZbigniew Bodek static int	ena_ioctl(if_t, u_long, caddr_t);
1529b8d05b8SZbigniew Bodek static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
1539b8d05b8SZbigniew Bodek static void	ena_update_host_info(struct ena_admin_host_info *, if_t);
1549b8d05b8SZbigniew Bodek static void	ena_update_hwassist(struct ena_adapter *);
1559b8d05b8SZbigniew Bodek static int	ena_setup_ifnet(device_t, struct ena_adapter *,
1569b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1579b8d05b8SZbigniew Bodek static void	ena_tx_csum(struct ena_com_tx_ctx *, struct mbuf *);
1581b069f1cSZbigniew Bodek static int	ena_check_and_collapse_mbuf(struct ena_ring *tx_ring,
1591b069f1cSZbigniew Bodek     struct mbuf **mbuf);
1601e9fb899SZbigniew Bodek static int	ena_xmit_mbuf(struct ena_ring *, struct mbuf **);
1619b8d05b8SZbigniew Bodek static void	ena_start_xmit(struct ena_ring *);
1629b8d05b8SZbigniew Bodek static int	ena_mq_start(if_t, struct mbuf *);
1639b8d05b8SZbigniew Bodek static void	ena_deferred_mq_start(void *, int);
1649b8d05b8SZbigniew Bodek static void	ena_qflush(if_t);
1659b8d05b8SZbigniew Bodek static int	ena_calc_io_queue_num(struct ena_adapter *,
1669b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *);
1676064f289SMarcin Wojtas static int	ena_calc_queue_size(struct ena_adapter *,
1686064f289SMarcin Wojtas     struct ena_calc_queue_size_ctx *);
1696064f289SMarcin Wojtas static int	ena_handle_updated_queues(struct ena_adapter *,
1706064f289SMarcin Wojtas     struct ena_com_dev_get_features_ctx *);
1719b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1729b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
1739b8d05b8SZbigniew Bodek static void	ena_config_host_info(struct ena_com_dev *);
1749b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1759b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1769b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1779b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
1789b8d05b8SZbigniew Bodek static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *,
1799b8d05b8SZbigniew Bodek     int);
1809b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1819b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1829b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1839b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1849b8d05b8SZbigniew Bodek 
1859b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1869b8d05b8SZbigniew Bodek 
1879b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
1889b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
1899b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
1909b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
1919b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
1929b8d05b8SZbigniew Bodek     /* Last entry */
1939b8d05b8SZbigniew Bodek     { 0, 0, 0 }
1949b8d05b8SZbigniew Bodek };
1959b8d05b8SZbigniew Bodek 
1969b8d05b8SZbigniew Bodek /*
1979b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
1989b8d05b8SZbigniew Bodek  */
1999b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
2009b8d05b8SZbigniew Bodek 
2019b8d05b8SZbigniew Bodek void
2029b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2039b8d05b8SZbigniew Bodek {
2040bdffe59SMarcin Wojtas 	if (error != 0)
2059b8d05b8SZbigniew Bodek 		return;
2069b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
2079b8d05b8SZbigniew Bodek }
2089b8d05b8SZbigniew Bodek 
2099b8d05b8SZbigniew Bodek int
2109b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
2119b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
2129b8d05b8SZbigniew Bodek {
2139b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2140bdffe59SMarcin Wojtas 	uint32_t maxsize;
2150bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2169b8d05b8SZbigniew Bodek 	int error;
2179b8d05b8SZbigniew Bodek 
2180bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2190bdffe59SMarcin Wojtas 
2200bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2213f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2229b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2230bdffe59SMarcin Wojtas 
2249b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2259b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2268a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2278a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2289b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2299b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2309b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2319b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2329b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2339b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2349b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2359b8d05b8SZbigniew Bodek 	    &dma->tag);
2363f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2374e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2389b8d05b8SZbigniew Bodek 		goto fail_tag;
2399b8d05b8SZbigniew Bodek 	}
2409b8d05b8SZbigniew Bodek 
2419b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2429b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2433f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2444e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2454e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2469b8d05b8SZbigniew Bodek 		goto fail_map_create;
2479b8d05b8SZbigniew Bodek 	}
2489b8d05b8SZbigniew Bodek 
2499b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2509b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2519b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2523f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
2534e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2549b8d05b8SZbigniew Bodek 		goto fail_map_load;
2559b8d05b8SZbigniew Bodek 	}
2569b8d05b8SZbigniew Bodek 
257e8073738SMarcin Wojtas 	bus_dmamap_sync(dma->tag, dma->map,
258e8073738SMarcin Wojtas 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
259e8073738SMarcin Wojtas 
2609b8d05b8SZbigniew Bodek 	return (0);
2619b8d05b8SZbigniew Bodek 
2629b8d05b8SZbigniew Bodek fail_map_load:
2639b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2647d2544e6SMarcin Wojtas fail_map_create:
2659b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2669b8d05b8SZbigniew Bodek fail_tag:
2679b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2685b14f92eSMarcin Wojtas 	dma->vaddr = NULL;
2695b14f92eSMarcin Wojtas 	dma->paddr = 0;
2709b8d05b8SZbigniew Bodek 
2719b8d05b8SZbigniew Bodek 	return (error);
2729b8d05b8SZbigniew Bodek }
2739b8d05b8SZbigniew Bodek 
2749b8d05b8SZbigniew Bodek static int
2759b8d05b8SZbigniew Bodek ena_allocate_pci_resources(struct ena_adapter* adapter)
2769b8d05b8SZbigniew Bodek {
2779b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2789b8d05b8SZbigniew Bodek 	int rid;
2799b8d05b8SZbigniew Bodek 
2809b8d05b8SZbigniew Bodek 	rid = PCIR_BAR(ENA_REG_BAR);
2819b8d05b8SZbigniew Bodek 	adapter->memory = NULL;
2829b8d05b8SZbigniew Bodek 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
2839b8d05b8SZbigniew Bodek 	    &rid, RF_ACTIVE);
2843f9ed7abSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
2859b8d05b8SZbigniew Bodek 		device_printf(pdev, "Unable to allocate bus resource: "
2869b8d05b8SZbigniew Bodek 		    "registers\n");
2879b8d05b8SZbigniew Bodek 		return (ENXIO);
2889b8d05b8SZbigniew Bodek 	}
2899b8d05b8SZbigniew Bodek 
2909b8d05b8SZbigniew Bodek 	return (0);
2919b8d05b8SZbigniew Bodek }
2929b8d05b8SZbigniew Bodek 
2939b8d05b8SZbigniew Bodek static void
2949b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
2959b8d05b8SZbigniew Bodek {
2969b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2979b8d05b8SZbigniew Bodek 
2989b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
2999b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3009b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
3019b8d05b8SZbigniew Bodek 	}
3029b8d05b8SZbigniew Bodek 
3039b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
3049b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3059b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
3069b8d05b8SZbigniew Bodek 	}
3079b8d05b8SZbigniew Bodek }
3089b8d05b8SZbigniew Bodek 
3099b8d05b8SZbigniew Bodek static int
3109b8d05b8SZbigniew Bodek ena_probe(device_t dev)
3119b8d05b8SZbigniew Bodek {
3129b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3139b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3149b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3159b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3169b8d05b8SZbigniew Bodek 
3179b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3189b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3199b8d05b8SZbigniew Bodek 
3209b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3219b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3229b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3239b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
3249b8d05b8SZbigniew Bodek 			ena_trace(ENA_DBG, "vendor=%x device=%x ",
3259b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3269b8d05b8SZbigniew Bodek 
3279b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3289b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3299b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3309b8d05b8SZbigniew Bodek 		}
3319b8d05b8SZbigniew Bodek 
3329b8d05b8SZbigniew Bodek 		ent++;
3339b8d05b8SZbigniew Bodek 
3349b8d05b8SZbigniew Bodek 	}
3359b8d05b8SZbigniew Bodek 
3369b8d05b8SZbigniew Bodek 	return (ENXIO);
3379b8d05b8SZbigniew Bodek }
3389b8d05b8SZbigniew Bodek 
3399b8d05b8SZbigniew Bodek static int
3409b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3419b8d05b8SZbigniew Bodek {
3429b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3433cfadb28SMarcin Wojtas 	int rc;
3449b8d05b8SZbigniew Bodek 
3453cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3469b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3473cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3483cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3493cfadb28SMarcin Wojtas 		return (EINVAL);
3509b8d05b8SZbigniew Bodek 	}
3519b8d05b8SZbigniew Bodek 
3529b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3533cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
3543cfadb28SMarcin Wojtas 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
3553cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3563cfadb28SMarcin Wojtas 	} else {
3573cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3583cfadb28SMarcin Wojtas 		    new_mtu);
3593cfadb28SMarcin Wojtas 	}
3609b8d05b8SZbigniew Bodek 
3613cfadb28SMarcin Wojtas 	return (rc);
3629b8d05b8SZbigniew Bodek }
3639b8d05b8SZbigniew Bodek 
3649b8d05b8SZbigniew Bodek static inline void
3659b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3669b8d05b8SZbigniew Bodek {
3679b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3689b8d05b8SZbigniew Bodek 
3699b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3709b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3719b8d05b8SZbigniew Bodek }
3729b8d05b8SZbigniew Bodek 
3739b8d05b8SZbigniew Bodek static inline void
3749b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3759b8d05b8SZbigniew Bodek {
3769b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3779b8d05b8SZbigniew Bodek 
3789b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3799b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3809b8d05b8SZbigniew Bodek }
3819b8d05b8SZbigniew Bodek 
3829b8d05b8SZbigniew Bodek static inline void
3839b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3849b8d05b8SZbigniew Bodek {
3859b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3869b8d05b8SZbigniew Bodek 
3879b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3889b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
3899b8d05b8SZbigniew Bodek }
3909b8d05b8SZbigniew Bodek 
3919b8d05b8SZbigniew Bodek static void
3929b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
3939b8d05b8SZbigniew Bodek     uint16_t qid)
3949b8d05b8SZbigniew Bodek {
3959b8d05b8SZbigniew Bodek 
3969b8d05b8SZbigniew Bodek 	ring->qid = qid;
3979b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
3989b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
399d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
400d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
4019b8d05b8SZbigniew Bodek }
4029b8d05b8SZbigniew Bodek 
403cd5d5804SMarcin Wojtas static void
4049b8d05b8SZbigniew Bodek ena_init_io_rings(struct ena_adapter *adapter)
4059b8d05b8SZbigniew Bodek {
4069b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
4079b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
4089b8d05b8SZbigniew Bodek 	struct ena_que *que;
4099b8d05b8SZbigniew Bodek 	int i;
4109b8d05b8SZbigniew Bodek 
4119b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
4129b8d05b8SZbigniew Bodek 
4139b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
4149b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4159b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4169b8d05b8SZbigniew Bodek 
4179b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4189b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4199b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4209b8d05b8SZbigniew Bodek 
4219b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4229b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
4239b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4249b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4259b8d05b8SZbigniew Bodek 		txr->smoothed_interval =
4269b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
4279b8d05b8SZbigniew Bodek 
4289b8d05b8SZbigniew Bodek 		/* Allocate a buf ring */
4296064f289SMarcin Wojtas 		txr->buf_ring_size = adapter->buf_ring_size;
4306064f289SMarcin Wojtas 		txr->br = buf_ring_alloc(txr->buf_ring_size, M_DEVBUF,
4319b8d05b8SZbigniew Bodek 		    M_WAITOK, &txr->ring_mtx);
4329b8d05b8SZbigniew Bodek 
4339b8d05b8SZbigniew Bodek 		/* Alloc TX statistics. */
4349b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4359b8d05b8SZbigniew Bodek 		    sizeof(txr->tx_stats));
4369b8d05b8SZbigniew Bodek 
4379b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4389b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4399b8d05b8SZbigniew Bodek 		rxr->smoothed_interval =
4409b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
4419b8d05b8SZbigniew Bodek 
4429b8d05b8SZbigniew Bodek 		/* Alloc RX statistics. */
4439b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4449b8d05b8SZbigniew Bodek 		    sizeof(rxr->rx_stats));
4459b8d05b8SZbigniew Bodek 
4469b8d05b8SZbigniew Bodek 		/* Initialize locks */
4479b8d05b8SZbigniew Bodek 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4489b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4499b8d05b8SZbigniew Bodek 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4509b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4519b8d05b8SZbigniew Bodek 
4529b8d05b8SZbigniew Bodek 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
4539b8d05b8SZbigniew Bodek 
4549b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4559b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4569b8d05b8SZbigniew Bodek 		que->id = i;
4579b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4589b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4599b8d05b8SZbigniew Bodek 
4609b8d05b8SZbigniew Bodek 		txr->que = que;
4619b8d05b8SZbigniew Bodek 		rxr->que = que;
462efe6ab18SMarcin Wojtas 
463efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4649b8d05b8SZbigniew Bodek 	}
4659b8d05b8SZbigniew Bodek }
4669b8d05b8SZbigniew Bodek 
4679b8d05b8SZbigniew Bodek static void
4689b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4699b8d05b8SZbigniew Bodek {
4709b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4719b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4729b8d05b8SZbigniew Bodek 
4739b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4749b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4759b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4769b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4779b8d05b8SZbigniew Bodek 
4787d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4797d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4807d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4817d2544e6SMarcin Wojtas 
4829b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
4839b8d05b8SZbigniew Bodek }
4849b8d05b8SZbigniew Bodek 
4859b8d05b8SZbigniew Bodek static void
4869b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
4879b8d05b8SZbigniew Bodek {
4889b8d05b8SZbigniew Bodek 	int i;
4899b8d05b8SZbigniew Bodek 
4909b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
4919b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
4929b8d05b8SZbigniew Bodek 
4939b8d05b8SZbigniew Bodek }
4949b8d05b8SZbigniew Bodek 
4959b8d05b8SZbigniew Bodek static int
4969b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
4979b8d05b8SZbigniew Bodek {
4989b8d05b8SZbigniew Bodek 	int ret;
4999b8d05b8SZbigniew Bodek 
5009b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
5019b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
5029b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5038a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5048a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5059b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5069b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
5078a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
5089b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
5099b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5109b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5119b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
5129b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5139b8d05b8SZbigniew Bodek 
5149b8d05b8SZbigniew Bodek 	return (ret);
5159b8d05b8SZbigniew Bodek }
5169b8d05b8SZbigniew Bodek 
5179b8d05b8SZbigniew Bodek static int
5189b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5199b8d05b8SZbigniew Bodek {
5209b8d05b8SZbigniew Bodek 	int ret;
5219b8d05b8SZbigniew Bodek 
5229b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5239b8d05b8SZbigniew Bodek 
5243f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5259b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5269b8d05b8SZbigniew Bodek 
5279b8d05b8SZbigniew Bodek 	return (ret);
5289b8d05b8SZbigniew Bodek }
5299b8d05b8SZbigniew Bodek 
5309b8d05b8SZbigniew Bodek static int
5319b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5329b8d05b8SZbigniew Bodek {
5339b8d05b8SZbigniew Bodek 	int ret;
5349b8d05b8SZbigniew Bodek 
5359b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5369b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5379b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5388a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5398a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5409b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5419b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsize 		     */
5424727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
5439b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsegsize 	     */
5449b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5459b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5469b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5479b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5489b8d05b8SZbigniew Bodek 
5499b8d05b8SZbigniew Bodek 	return (ret);
5509b8d05b8SZbigniew Bodek }
5519b8d05b8SZbigniew Bodek 
5529b8d05b8SZbigniew Bodek static int
5539b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5549b8d05b8SZbigniew Bodek {
5559b8d05b8SZbigniew Bodek 	int ret;
5569b8d05b8SZbigniew Bodek 
5579b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5589b8d05b8SZbigniew Bodek 
5593f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5609b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5619b8d05b8SZbigniew Bodek 
5629b8d05b8SZbigniew Bodek 	return (ret);
5639b8d05b8SZbigniew Bodek }
5649b8d05b8SZbigniew Bodek 
5659b8d05b8SZbigniew Bodek /**
5669b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
5679b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
5689b8d05b8SZbigniew Bodek  * @qid: queue index
5699b8d05b8SZbigniew Bodek  *
5709b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
5719b8d05b8SZbigniew Bodek  **/
5729b8d05b8SZbigniew Bodek static int
5739b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
5749b8d05b8SZbigniew Bodek {
5759b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
5769b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
5779b8d05b8SZbigniew Bodek 	int size, i, err;
5789b8d05b8SZbigniew Bodek 
5799b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
5809b8d05b8SZbigniew Bodek 
5819b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5823f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
5837d2544e6SMarcin Wojtas 		return (ENOMEM);
5849b8d05b8SZbigniew Bodek 
5859b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
5869b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5873f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
5887d2544e6SMarcin Wojtas 		goto err_buf_info_free;
5899b8d05b8SZbigniew Bodek 
5909b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
5919b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
5929b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
5939b8d05b8SZbigniew Bodek 
5949b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
5959b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
5969b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
5979b8d05b8SZbigniew Bodek 
5989b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
5999b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
6009b8d05b8SZbigniew Bodek 
6019b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
602b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6039b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
604b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6059b8d05b8SZbigniew Bodek 
6069b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6079b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6089b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6099b8d05b8SZbigniew Bodek 		    &tx_ring->tx_buffer_info[i].map);
6103f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
6114e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
6129b8d05b8SZbigniew Bodek 			     "Unable to create Tx DMA map for buffer %d\n", i);
6137d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
6149b8d05b8SZbigniew Bodek 		}
6159b8d05b8SZbigniew Bodek 	}
6169b8d05b8SZbigniew Bodek 
6179b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6189b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6199b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6209b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6213f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
6224e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT,
6239b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6249b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6257d2544e6SMarcin Wojtas 		goto err_buf_info_unmap;
6269b8d05b8SZbigniew Bodek 	}
6279b8d05b8SZbigniew Bodek 
628*5cb9db07SMarcin Wojtas 	tx_ring->running = true;
629*5cb9db07SMarcin Wojtas 
6309b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
6319b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
6329b8d05b8SZbigniew Bodek 
6339b8d05b8SZbigniew Bodek 	return (0);
6349b8d05b8SZbigniew Bodek 
6357d2544e6SMarcin Wojtas err_buf_info_unmap:
6369b8d05b8SZbigniew Bodek 	while (i--) {
6379b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6389b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6399b8d05b8SZbigniew Bodek 	}
640cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6417d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
6427d2544e6SMarcin Wojtas err_buf_info_free:
643cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6447d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
6457d2544e6SMarcin Wojtas 
6469b8d05b8SZbigniew Bodek 	return (ENOMEM);
6479b8d05b8SZbigniew Bodek }
6489b8d05b8SZbigniew Bodek 
6499b8d05b8SZbigniew Bodek /**
6509b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
6519b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6529b8d05b8SZbigniew Bodek  * @qid: queue index
6539b8d05b8SZbigniew Bodek  *
6549b8d05b8SZbigniew Bodek  * Free all transmit software resources
6559b8d05b8SZbigniew Bodek  **/
6569b8d05b8SZbigniew Bodek static void
6579b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
6589b8d05b8SZbigniew Bodek {
6599b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
6609b8d05b8SZbigniew Bodek 
6619b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
6629b8d05b8SZbigniew Bodek 	    NULL))
6639b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
6649b8d05b8SZbigniew Bodek 
6659b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
6669b8d05b8SZbigniew Bodek 
667b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6689b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
6699b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
6709b8d05b8SZbigniew Bodek 
6719b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
6729b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
673e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag,
674e8073738SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map, BUS_DMASYNC_POSTWRITE);
6759b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag,
6769b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6779b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6789b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
679e8073738SMarcin Wojtas 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
680e8073738SMarcin Wojtas 		tx_ring->tx_buffer_info[i].mbuf = NULL;
6819b8d05b8SZbigniew Bodek 	}
682416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6839b8d05b8SZbigniew Bodek 
6849b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
685cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6869b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
6879b8d05b8SZbigniew Bodek 
688cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6899b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
6909b8d05b8SZbigniew Bodek }
6919b8d05b8SZbigniew Bodek 
6929b8d05b8SZbigniew Bodek /**
6939b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
6949b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6959b8d05b8SZbigniew Bodek  *
6969b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
6979b8d05b8SZbigniew Bodek  **/
6989b8d05b8SZbigniew Bodek static int
6999b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7009b8d05b8SZbigniew Bodek {
7019b8d05b8SZbigniew Bodek 	int i, rc;
7029b8d05b8SZbigniew Bodek 
7039b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
7049b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
7050bdffe59SMarcin Wojtas 		if (rc != 0) {
7069b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
7079b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
7089b8d05b8SZbigniew Bodek 			goto err_setup_tx;
7099b8d05b8SZbigniew Bodek 		}
7107d2544e6SMarcin Wojtas 	}
7119b8d05b8SZbigniew Bodek 
7129b8d05b8SZbigniew Bodek 	return (0);
7139b8d05b8SZbigniew Bodek 
7149b8d05b8SZbigniew Bodek err_setup_tx:
7159b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
7169b8d05b8SZbigniew Bodek 	while (i--)
7179b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7189b8d05b8SZbigniew Bodek 	return (rc);
7199b8d05b8SZbigniew Bodek }
7209b8d05b8SZbigniew Bodek 
7219b8d05b8SZbigniew Bodek /**
7229b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
7239b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7249b8d05b8SZbigniew Bodek  *
7259b8d05b8SZbigniew Bodek  * Free all transmit software resources
7269b8d05b8SZbigniew Bodek  **/
7279b8d05b8SZbigniew Bodek static void
7289b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
7299b8d05b8SZbigniew Bodek {
7309b8d05b8SZbigniew Bodek 	int i;
7319b8d05b8SZbigniew Bodek 
7329b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
7339b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7349b8d05b8SZbigniew Bodek }
7359b8d05b8SZbigniew Bodek 
73643fefd16SMarcin Wojtas static inline int
73743fefd16SMarcin Wojtas validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
73843fefd16SMarcin Wojtas {
73943fefd16SMarcin Wojtas 	if (likely(req_id < rx_ring->ring_size))
74043fefd16SMarcin Wojtas 		return (0);
74143fefd16SMarcin Wojtas 
74243fefd16SMarcin Wojtas 	device_printf(rx_ring->adapter->pdev, "Invalid rx req_id: %hu\n",
74343fefd16SMarcin Wojtas 	    req_id);
74443fefd16SMarcin Wojtas 	counter_u64_add(rx_ring->rx_stats.bad_req_id, 1);
74543fefd16SMarcin Wojtas 
74643fefd16SMarcin Wojtas 	/* Trigger device reset */
74743fefd16SMarcin Wojtas 	rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
74843fefd16SMarcin Wojtas 	rx_ring->adapter->trigger_reset = true;
74943fefd16SMarcin Wojtas 
75043fefd16SMarcin Wojtas 	return (EFAULT);
75143fefd16SMarcin Wojtas }
75243fefd16SMarcin Wojtas 
7539b8d05b8SZbigniew Bodek /**
7549b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
7559b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7569b8d05b8SZbigniew Bodek  * @qid: queue index
7579b8d05b8SZbigniew Bodek  *
7589b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7599b8d05b8SZbigniew Bodek  **/
7609b8d05b8SZbigniew Bodek static int
7619b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
7629b8d05b8SZbigniew Bodek {
7639b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
7649b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
7659b8d05b8SZbigniew Bodek 	int size, err, i;
7669b8d05b8SZbigniew Bodek 
7679b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
7689b8d05b8SZbigniew Bodek 
7699b8d05b8SZbigniew Bodek 	/*
7709b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
7719b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
7729b8d05b8SZbigniew Bodek 	 */
7739b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
7749b8d05b8SZbigniew Bodek 
775cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
7769b8d05b8SZbigniew Bodek 
77743fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
77843fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
77943fefd16SMarcin Wojtas 
78043fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
78143fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
78243fefd16SMarcin Wojtas 
7839b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
7849b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
7859b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
7869b8d05b8SZbigniew Bodek 
7879b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
7889b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
7899b8d05b8SZbigniew Bodek 
7909b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
7919b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
7929b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
7939b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
7949b8d05b8SZbigniew Bodek 		if (err != 0) {
7954e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
7969b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
7977d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
7989b8d05b8SZbigniew Bodek 		}
7999b8d05b8SZbigniew Bodek 	}
8009b8d05b8SZbigniew Bodek 
8019b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8020bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8039b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8040bdffe59SMarcin Wojtas 		if (err != 0) {
8059b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8069b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8079b8d05b8SZbigniew Bodek 		} else {
8089b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8099b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8109b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8119b8d05b8SZbigniew Bodek 		}
8129b8d05b8SZbigniew Bodek 	}
8139b8d05b8SZbigniew Bodek 
8149b8d05b8SZbigniew Bodek 	return (0);
8159b8d05b8SZbigniew Bodek 
8167d2544e6SMarcin Wojtas err_buf_info_unmap:
8179b8d05b8SZbigniew Bodek 	while (i--) {
8189b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8199b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8209b8d05b8SZbigniew Bodek 	}
8219b8d05b8SZbigniew Bodek 
82243fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
82343fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
824cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8259b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8269b8d05b8SZbigniew Bodek 	return (ENOMEM);
8279b8d05b8SZbigniew Bodek }
8289b8d05b8SZbigniew Bodek 
8299b8d05b8SZbigniew Bodek /**
8309b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
8319b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8329b8d05b8SZbigniew Bodek  * @qid: queue index
8339b8d05b8SZbigniew Bodek  *
8349b8d05b8SZbigniew Bodek  * Free all receive software resources
8359b8d05b8SZbigniew Bodek  **/
8369b8d05b8SZbigniew Bodek static void
8379b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8389b8d05b8SZbigniew Bodek {
8399b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
8409b8d05b8SZbigniew Bodek 
8419b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
8429b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
843e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag,
844e8073738SMarcin Wojtas 		    rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD);
8459b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
8469b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
8479b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
8489b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8499b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8509b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8519b8d05b8SZbigniew Bodek 	}
8529b8d05b8SZbigniew Bodek 
8539b8d05b8SZbigniew Bodek 	/* free LRO resources, */
8549b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
8559b8d05b8SZbigniew Bodek 
8569b8d05b8SZbigniew Bodek 	/* free allocated memory */
857cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8589b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8599b8d05b8SZbigniew Bodek 
86043fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
86143fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
8629b8d05b8SZbigniew Bodek }
8639b8d05b8SZbigniew Bodek 
8649b8d05b8SZbigniew Bodek /**
8659b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
8669b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8679b8d05b8SZbigniew Bodek  *
8689b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8699b8d05b8SZbigniew Bodek  **/
8709b8d05b8SZbigniew Bodek static int
8719b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
8729b8d05b8SZbigniew Bodek {
8739b8d05b8SZbigniew Bodek 	int i, rc = 0;
8749b8d05b8SZbigniew Bodek 
8759b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
8769b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
8770bdffe59SMarcin Wojtas 		if (rc != 0) {
8789b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8799b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
8809b8d05b8SZbigniew Bodek 			goto err_setup_rx;
8819b8d05b8SZbigniew Bodek 		}
8827d2544e6SMarcin Wojtas 	}
8839b8d05b8SZbigniew Bodek 	return (0);
8849b8d05b8SZbigniew Bodek 
8859b8d05b8SZbigniew Bodek err_setup_rx:
8869b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
8879b8d05b8SZbigniew Bodek 	while (i--)
8889b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
8899b8d05b8SZbigniew Bodek 	return (rc);
8909b8d05b8SZbigniew Bodek }
8919b8d05b8SZbigniew Bodek 
8929b8d05b8SZbigniew Bodek /**
8939b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
8949b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8959b8d05b8SZbigniew Bodek  *
8969b8d05b8SZbigniew Bodek  * Free all receive software resources
8979b8d05b8SZbigniew Bodek  **/
8989b8d05b8SZbigniew Bodek static void
8999b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9009b8d05b8SZbigniew Bodek {
9019b8d05b8SZbigniew Bodek 	int i;
9029b8d05b8SZbigniew Bodek 
9039b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
9049b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9059b8d05b8SZbigniew Bodek }
9069b8d05b8SZbigniew Bodek 
9079b8d05b8SZbigniew Bodek static inline int
9089b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9099b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9109b8d05b8SZbigniew Bodek {
9119b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9129b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9139b8d05b8SZbigniew Bodek 	int nsegs, error;
9144727bda6SMarcin Wojtas 	int mlen;
9159b8d05b8SZbigniew Bodek 
9169b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
9173f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
9189b8d05b8SZbigniew Bodek 		return (0);
9199b8d05b8SZbigniew Bodek 
9209b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
9219b8d05b8SZbigniew Bodek 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
9229b8d05b8SZbigniew Bodek 
9233f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
9244727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
9254727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9264727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
9279b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
9289b8d05b8SZbigniew Bodek 			return (ENOMEM);
9299b8d05b8SZbigniew Bodek 		}
9304727bda6SMarcin Wojtas 		mlen = MCLBYTES;
9314727bda6SMarcin Wojtas 	} else {
9324727bda6SMarcin Wojtas 		mlen = MJUM16BYTES;
9334727bda6SMarcin Wojtas 	}
9349b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
9354727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
9369b8d05b8SZbigniew Bodek 
9379b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
9389b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9399b8d05b8SZbigniew Bodek 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d",
9409b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
9419b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
9429b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
9433f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
9444e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
9459b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
9469b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
9479b8d05b8SZbigniew Bodek 		goto exit;
9489b8d05b8SZbigniew Bodek 
9499b8d05b8SZbigniew Bodek 	}
9509b8d05b8SZbigniew Bodek 
9519b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
9529b8d05b8SZbigniew Bodek 
9539b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
9549b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
9554727bda6SMarcin Wojtas 	ena_buf->len = mlen;
9569b8d05b8SZbigniew Bodek 
9579b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9589b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
9599b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
9609b8d05b8SZbigniew Bodek 
9619b8d05b8SZbigniew Bodek 	return (0);
9629b8d05b8SZbigniew Bodek 
9639b8d05b8SZbigniew Bodek exit:
9649b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9659b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9669b8d05b8SZbigniew Bodek 	return (EFAULT);
9679b8d05b8SZbigniew Bodek }
9689b8d05b8SZbigniew Bodek 
9699b8d05b8SZbigniew Bodek static void
9709b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
9719b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
9729b8d05b8SZbigniew Bodek {
9739b8d05b8SZbigniew Bodek 
9744e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
9754e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
9769b8d05b8SZbigniew Bodek 		return;
9774e8acd84SMarcin Wojtas 	}
9789b8d05b8SZbigniew Bodek 
979e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
980e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
9819b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
9829b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9839b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9849b8d05b8SZbigniew Bodek }
9859b8d05b8SZbigniew Bodek 
9869b8d05b8SZbigniew Bodek /**
9879b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
9889b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
9899b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
9909b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
9919b8d05b8SZbigniew Bodek  **/
9929b8d05b8SZbigniew Bodek static int
9939b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
9949b8d05b8SZbigniew Bodek {
9959b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
99643fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
9979b8d05b8SZbigniew Bodek 	uint32_t i;
9989b8d05b8SZbigniew Bodek 	int rc;
9999b8d05b8SZbigniew Bodek 
10009b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d",
10019b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10029b8d05b8SZbigniew Bodek 
10039b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10049b8d05b8SZbigniew Bodek 
10059b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
100643fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
100743fefd16SMarcin Wojtas 
10089b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
10099b8d05b8SZbigniew Bodek 		    "RX buffer - next to use: %d", next_to_use);
10109b8d05b8SZbigniew Bodek 
101143fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
101243fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
10139b8d05b8SZbigniew Bodek 
10149b8d05b8SZbigniew Bodek 		rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
10153f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
10164e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10174e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
10184e8acd84SMarcin Wojtas 			    rx_ring->qid);
10199b8d05b8SZbigniew Bodek 			break;
10209b8d05b8SZbigniew Bodek 		}
10219b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
102243fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
10230bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
10244e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10259b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
10269b8d05b8SZbigniew Bodek 			    rx_ring->qid);
10279b8d05b8SZbigniew Bodek 			break;
10289b8d05b8SZbigniew Bodek 		}
10299b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
10309b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
10319b8d05b8SZbigniew Bodek 	}
10329b8d05b8SZbigniew Bodek 
10333f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
10349b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
10354e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING,
10364e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
10374e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
10389b8d05b8SZbigniew Bodek 	}
10399b8d05b8SZbigniew Bodek 
10403f9ed7abSMarcin Wojtas 	if (likely(i != 0)) {
10419b8d05b8SZbigniew Bodek 		wmb();
10429b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
10439b8d05b8SZbigniew Bodek 	}
10449b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
10459b8d05b8SZbigniew Bodek 	return (i);
10469b8d05b8SZbigniew Bodek }
10479b8d05b8SZbigniew Bodek 
10489b8d05b8SZbigniew Bodek static void
10499b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
10509b8d05b8SZbigniew Bodek {
10519b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
10529b8d05b8SZbigniew Bodek 	unsigned int i;
10539b8d05b8SZbigniew Bodek 
10549b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
10559b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
10569b8d05b8SZbigniew Bodek 
10570bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
10589b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
10599b8d05b8SZbigniew Bodek 	}
10609b8d05b8SZbigniew Bodek }
10619b8d05b8SZbigniew Bodek 
10629b8d05b8SZbigniew Bodek /**
10639b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
10649b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
10659b8d05b8SZbigniew Bodek  *
10669b8d05b8SZbigniew Bodek  */
10679b8d05b8SZbigniew Bodek static void
10689b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
10699b8d05b8SZbigniew Bodek {
10709b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
10719b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
10729b8d05b8SZbigniew Bodek 
10739b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
10749b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
10759b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
10769b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
10779b8d05b8SZbigniew Bodek 
10789b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
10794e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
10804e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
10819b8d05b8SZbigniew Bodek 	}
10829b8d05b8SZbigniew Bodek }
10839b8d05b8SZbigniew Bodek 
10849b8d05b8SZbigniew Bodek static void
10859b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
10869b8d05b8SZbigniew Bodek {
10879b8d05b8SZbigniew Bodek 	int i;
10889b8d05b8SZbigniew Bodek 
10899b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
10909b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
10919b8d05b8SZbigniew Bodek }
10929b8d05b8SZbigniew Bodek 
10939b8d05b8SZbigniew Bodek /**
10949b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
10959b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
10969b8d05b8SZbigniew Bodek  * @qid: queue index
10979b8d05b8SZbigniew Bodek  **/
10989b8d05b8SZbigniew Bodek static void
10999b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
11009b8d05b8SZbigniew Bodek {
11014e8acd84SMarcin Wojtas 	bool print_once = true;
11029b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
11039b8d05b8SZbigniew Bodek 
1104416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
11059b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
11069b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
11079b8d05b8SZbigniew Bodek 
11089b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
11099b8d05b8SZbigniew Bodek 			continue;
11109b8d05b8SZbigniew Bodek 
11114e8acd84SMarcin Wojtas 		if (print_once) {
11124e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
11134e8acd84SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x",
11144e8acd84SMarcin Wojtas 			    qid, i);
11154e8acd84SMarcin Wojtas 			print_once = false;
11164e8acd84SMarcin Wojtas 		} else {
11174e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG,
11184e8acd84SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x",
11194e8acd84SMarcin Wojtas 			     qid, i);
11204e8acd84SMarcin Wojtas 		}
11219b8d05b8SZbigniew Bodek 
1122e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map,
1123e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTWRITE);
11249b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
11259b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
11269b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
11279b8d05b8SZbigniew Bodek 	}
1128416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
11299b8d05b8SZbigniew Bodek }
11309b8d05b8SZbigniew Bodek 
11319b8d05b8SZbigniew Bodek static void
11329b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
11339b8d05b8SZbigniew Bodek {
11349b8d05b8SZbigniew Bodek 
11359b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++)
11369b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
11379b8d05b8SZbigniew Bodek }
11389b8d05b8SZbigniew Bodek 
11399b8d05b8SZbigniew Bodek static void
11409b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
11419b8d05b8SZbigniew Bodek {
11429b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11439b8d05b8SZbigniew Bodek 	int i;
11449b8d05b8SZbigniew Bodek 
11459b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11469b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11479b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11489b8d05b8SZbigniew Bodek 	}
11499b8d05b8SZbigniew Bodek }
11509b8d05b8SZbigniew Bodek 
11519b8d05b8SZbigniew Bodek static void
11529b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
11539b8d05b8SZbigniew Bodek {
11549b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11559b8d05b8SZbigniew Bodek 	int i;
11569b8d05b8SZbigniew Bodek 
11579b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11589b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
11599b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11609b8d05b8SZbigniew Bodek 	}
11619b8d05b8SZbigniew Bodek }
11629b8d05b8SZbigniew Bodek 
11639b8d05b8SZbigniew Bodek static void
11649b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
11659b8d05b8SZbigniew Bodek {
1166*5cb9db07SMarcin Wojtas 	struct ena_que *queue;
1167*5cb9db07SMarcin Wojtas 	int i;
1168*5cb9db07SMarcin Wojtas 
1169*5cb9db07SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
1170*5cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
1171*5cb9db07SMarcin Wojtas 		while (taskqueue_cancel(queue->cleanup_tq,
1172*5cb9db07SMarcin Wojtas 		    &queue->cleanup_task, NULL))
1173*5cb9db07SMarcin Wojtas 			taskqueue_drain(queue->cleanup_tq,
1174*5cb9db07SMarcin Wojtas 			    &queue->cleanup_task);
1175*5cb9db07SMarcin Wojtas 		taskqueue_free(queue->cleanup_tq);
1176*5cb9db07SMarcin Wojtas 	}
1177*5cb9db07SMarcin Wojtas 
11789b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
11799b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
11809b8d05b8SZbigniew Bodek }
11819b8d05b8SZbigniew Bodek 
1182fceb9387SMarcin Wojtas static inline int
11839b8d05b8SZbigniew Bodek validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
11849b8d05b8SZbigniew Bodek {
11854e8acd84SMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
11869b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info = NULL;
11879b8d05b8SZbigniew Bodek 
11889b8d05b8SZbigniew Bodek 	if (likely(req_id < tx_ring->ring_size)) {
11899b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
11900bdffe59SMarcin Wojtas 		if (tx_info->mbuf != NULL)
11910bdffe59SMarcin Wojtas 			return (0);
11924e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
11934e8acd84SMarcin Wojtas 		    "tx_info doesn't have valid mbuf\n");
11944e306999SMarcin Wojtas 	}
11954e8acd84SMarcin Wojtas 
11964e306999SMarcin Wojtas 	device_printf(adapter->pdev, "Invalid req_id: %hu\n", req_id);
11979b8d05b8SZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
11989b8d05b8SZbigniew Bodek 
11999b8d05b8SZbigniew Bodek 	return (EFAULT);
12009b8d05b8SZbigniew Bodek }
12019b8d05b8SZbigniew Bodek 
12029b8d05b8SZbigniew Bodek static int
12039b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
12049b8d05b8SZbigniew Bodek {
12059b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
12069b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
12079b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
1208*5cb9db07SMarcin Wojtas 	struct ena_que *queue;
12099b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12109b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
12119b8d05b8SZbigniew Bodek 	int rc, i;
12129b8d05b8SZbigniew Bodek 
12139b8d05b8SZbigniew Bodek 	/* Create TX queues */
12149b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12159b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12169b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
12179b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
12189b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
12199b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
12209b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12219b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12229b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12230bdffe59SMarcin Wojtas 		if (rc != 0) {
12249b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12259b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
12269b8d05b8SZbigniew Bodek 			goto err_tx;
12279b8d05b8SZbigniew Bodek 		}
12289b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
12299b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12309b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12319b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12320bdffe59SMarcin Wojtas 		if (rc != 0) {
12339b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12349b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
12359b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12369b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12379b8d05b8SZbigniew Bodek 			goto err_tx;
12389b8d05b8SZbigniew Bodek 		}
12399b8d05b8SZbigniew Bodek 	}
12409b8d05b8SZbigniew Bodek 
12419b8d05b8SZbigniew Bodek 	/* Create RX queues */
12429b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12439b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12449b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12459b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
12469b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
12479b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
12489b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12499b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12509b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12513f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12529b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12539b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
12549b8d05b8SZbigniew Bodek 			goto err_rx;
12559b8d05b8SZbigniew Bodek 		}
12569b8d05b8SZbigniew Bodek 
12579b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
12589b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12599b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12609b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12613f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12629b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12639b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
12649b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12659b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12669b8d05b8SZbigniew Bodek 			goto err_rx;
12679b8d05b8SZbigniew Bodek 		}
12689b8d05b8SZbigniew Bodek 	}
12699b8d05b8SZbigniew Bodek 
1270*5cb9db07SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
1271*5cb9db07SMarcin Wojtas 		queue = &adapter->que[i];
1272*5cb9db07SMarcin Wojtas 
1273*5cb9db07SMarcin Wojtas 		TASK_INIT(&queue->cleanup_task, 0, ena_cleanup, queue);
1274*5cb9db07SMarcin Wojtas 		queue->cleanup_tq = taskqueue_create_fast("ena cleanup",
1275*5cb9db07SMarcin Wojtas 		    M_WAITOK, taskqueue_thread_enqueue, &queue->cleanup_tq);
1276*5cb9db07SMarcin Wojtas 
1277*5cb9db07SMarcin Wojtas 		taskqueue_start_threads(&queue->cleanup_tq, 1, PI_NET,
1278*5cb9db07SMarcin Wojtas 		    "%s queue %d cleanup",
1279*5cb9db07SMarcin Wojtas 		    device_get_nameunit(adapter->pdev), i);
1280*5cb9db07SMarcin Wojtas 	}
1281*5cb9db07SMarcin Wojtas 
12829b8d05b8SZbigniew Bodek 	return (0);
12839b8d05b8SZbigniew Bodek 
12849b8d05b8SZbigniew Bodek err_rx:
12859b8d05b8SZbigniew Bodek 	while (i--)
12869b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
12879b8d05b8SZbigniew Bodek 	i = adapter->num_queues;
12889b8d05b8SZbigniew Bodek err_tx:
12899b8d05b8SZbigniew Bodek 	while (i--)
12909b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
12919b8d05b8SZbigniew Bodek 
12929b8d05b8SZbigniew Bodek 	return (ENXIO);
12939b8d05b8SZbigniew Bodek }
12949b8d05b8SZbigniew Bodek 
12959b8d05b8SZbigniew Bodek /**
12969b8d05b8SZbigniew Bodek  * ena_tx_cleanup - clear sent packets and corresponding descriptors
12979b8d05b8SZbigniew Bodek  * @tx_ring: ring for which we want to clean packets
12989b8d05b8SZbigniew Bodek  *
12999b8d05b8SZbigniew Bodek  * Once packets are sent, we ask the device in a loop for no longer used
13009b8d05b8SZbigniew Bodek  * descriptors. We find the related mbuf chain in a map (index in an array)
13019b8d05b8SZbigniew Bodek  * and free it, then update ring state.
13029b8d05b8SZbigniew Bodek  * This is performed in "endless" loop, updating ring pointers every
13039b8d05b8SZbigniew Bodek  * TX_COMMIT. The first check of free descriptor is performed before the actual
13049b8d05b8SZbigniew Bodek  * loop, then repeated at the loop end.
13059b8d05b8SZbigniew Bodek  **/
13069b8d05b8SZbigniew Bodek static int
13079b8d05b8SZbigniew Bodek ena_tx_cleanup(struct ena_ring *tx_ring)
13089b8d05b8SZbigniew Bodek {
13099b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
13109b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
13119b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
13129b8d05b8SZbigniew Bodek 	uint16_t req_id;
13139b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
13149b8d05b8SZbigniew Bodek 	unsigned int total_done = 0;
13159b8d05b8SZbigniew Bodek 	int rc;
13169b8d05b8SZbigniew Bodek 	int commit = TX_COMMIT;
13179b8d05b8SZbigniew Bodek 	int budget = TX_BUDGET;
13189b8d05b8SZbigniew Bodek 	int work_done;
1319*5cb9db07SMarcin Wojtas 	bool above_thresh;
13209b8d05b8SZbigniew Bodek 
13219b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
13229b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
13239b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
13249b8d05b8SZbigniew Bodek 	next_to_clean = tx_ring->next_to_clean;
13259b8d05b8SZbigniew Bodek 
13269b8d05b8SZbigniew Bodek 	do {
13279b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info;
13289b8d05b8SZbigniew Bodek 		struct mbuf *mbuf;
13299b8d05b8SZbigniew Bodek 
13309b8d05b8SZbigniew Bodek 		rc = ena_com_tx_comp_req_id_get(io_cq, &req_id);
13313f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
13329b8d05b8SZbigniew Bodek 			break;
13339b8d05b8SZbigniew Bodek 
13349b8d05b8SZbigniew Bodek 		rc = validate_tx_req_id(tx_ring, req_id);
13353f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
13369b8d05b8SZbigniew Bodek 			break;
13379b8d05b8SZbigniew Bodek 
13389b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
13399b8d05b8SZbigniew Bodek 
13409b8d05b8SZbigniew Bodek 		mbuf = tx_info->mbuf;
13419b8d05b8SZbigniew Bodek 
13429b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
13439b8d05b8SZbigniew Bodek 		bintime_clear(&tx_info->timestamp);
13449b8d05b8SZbigniew Bodek 
13453f9ed7abSMarcin Wojtas 		if (likely(tx_info->num_of_bufs != 0)) {
13469b8d05b8SZbigniew Bodek 			/* Map is no longer required */
1347e8073738SMarcin Wojtas 			bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map,
1348e8073738SMarcin Wojtas 			    BUS_DMASYNC_POSTWRITE);
13499b8d05b8SZbigniew Bodek 			bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
13509b8d05b8SZbigniew Bodek 		}
13519b8d05b8SZbigniew Bodek 
13524e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d mbuf %p completed",
13534e8acd84SMarcin Wojtas 		    tx_ring->qid, mbuf);
13544e8acd84SMarcin Wojtas 
13559b8d05b8SZbigniew Bodek 		m_freem(mbuf);
13569b8d05b8SZbigniew Bodek 
13579b8d05b8SZbigniew Bodek 		total_done += tx_info->tx_descs;
13589b8d05b8SZbigniew Bodek 
13599b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[next_to_clean] = req_id;
13609b8d05b8SZbigniew Bodek 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
13619b8d05b8SZbigniew Bodek 		    tx_ring->ring_size);
13629b8d05b8SZbigniew Bodek 
13633f9ed7abSMarcin Wojtas 		if (unlikely(--commit == 0)) {
13649b8d05b8SZbigniew Bodek 			commit = TX_COMMIT;
13659b8d05b8SZbigniew Bodek 			/* update ring state every TX_COMMIT descriptor */
13669b8d05b8SZbigniew Bodek 			tx_ring->next_to_clean = next_to_clean;
13670bdffe59SMarcin Wojtas 			ena_com_comp_ack(
13680bdffe59SMarcin Wojtas 			    &adapter->ena_dev->io_sq_queues[ena_qid],
13690bdffe59SMarcin Wojtas 			    total_done);
13709b8d05b8SZbigniew Bodek 			ena_com_update_dev_comp_head(io_cq);
13719b8d05b8SZbigniew Bodek 			total_done = 0;
13729b8d05b8SZbigniew Bodek 		}
13733f9ed7abSMarcin Wojtas 	} while (likely(--budget));
13749b8d05b8SZbigniew Bodek 
13759b8d05b8SZbigniew Bodek 	work_done = TX_BUDGET - budget;
13769b8d05b8SZbigniew Bodek 
13774e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d done. total pkts: %d",
13784e8acd84SMarcin Wojtas 	tx_ring->qid, work_done);
13794e8acd84SMarcin Wojtas 
13809b8d05b8SZbigniew Bodek 	/* If there is still something to commit update ring state */
13813f9ed7abSMarcin Wojtas 	if (likely(commit != TX_COMMIT)) {
13829b8d05b8SZbigniew Bodek 		tx_ring->next_to_clean = next_to_clean;
13830bdffe59SMarcin Wojtas 		ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid],
13840bdffe59SMarcin Wojtas 		    total_done);
13859b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(io_cq);
13869b8d05b8SZbigniew Bodek 	}
13879b8d05b8SZbigniew Bodek 
1388*5cb9db07SMarcin Wojtas 	/*
1389*5cb9db07SMarcin Wojtas 	 * Need to make the rings circular update visible to
1390*5cb9db07SMarcin Wojtas 	 * ena_xmit_mbuf() before checking for tx_ring->running.
1391*5cb9db07SMarcin Wojtas 	 */
1392*5cb9db07SMarcin Wojtas 	mb();
1393*5cb9db07SMarcin Wojtas 
1394*5cb9db07SMarcin Wojtas 	above_thresh = ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
1395*5cb9db07SMarcin Wojtas 	    ENA_TX_RESUME_THRESH);
1396*5cb9db07SMarcin Wojtas 	if (unlikely(!tx_ring->running && above_thresh)) {
1397*5cb9db07SMarcin Wojtas 		ENA_RING_MTX_LOCK(tx_ring);
1398*5cb9db07SMarcin Wojtas 		above_thresh =
1399*5cb9db07SMarcin Wojtas 		    ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
1400*5cb9db07SMarcin Wojtas 		    ENA_TX_RESUME_THRESH);
1401*5cb9db07SMarcin Wojtas 		if (!tx_ring->running && above_thresh) {
1402*5cb9db07SMarcin Wojtas 			tx_ring->running = true;
1403*5cb9db07SMarcin Wojtas 			counter_u64_add(tx_ring->tx_stats.queue_wakeup, 1);
1404*5cb9db07SMarcin Wojtas 			taskqueue_enqueue(tx_ring->enqueue_tq,
1405*5cb9db07SMarcin Wojtas 			    &tx_ring->enqueue_task);
1406*5cb9db07SMarcin Wojtas 		}
1407*5cb9db07SMarcin Wojtas 		ENA_RING_MTX_UNLOCK(tx_ring);
1408*5cb9db07SMarcin Wojtas 	}
14099b8d05b8SZbigniew Bodek 
14109b8d05b8SZbigniew Bodek 	return (work_done);
14119b8d05b8SZbigniew Bodek }
14129b8d05b8SZbigniew Bodek 
14139b8d05b8SZbigniew Bodek static void
14149b8d05b8SZbigniew Bodek ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
14159b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
14169b8d05b8SZbigniew Bodek {
14179b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
14189b8d05b8SZbigniew Bodek 
14193f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support)) {
14209b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
14219b8d05b8SZbigniew Bodek 
14229b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->frag &&
1423bfea0e93SMarcin Wojtas 		    (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
14249b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
14259b8d05b8SZbigniew Bodek 			return;
14269b8d05b8SZbigniew Bodek 		}
14279b8d05b8SZbigniew Bodek 
14289b8d05b8SZbigniew Bodek 		switch (ena_rx_ctx->l3_proto) {
14299b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV4:
14309b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
14319b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14329b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
14339b8d05b8SZbigniew Bodek 				break;
14349b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14359b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
14369b8d05b8SZbigniew Bodek 				break;
14379b8d05b8SZbigniew Bodek 			default:
14389b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
14399b8d05b8SZbigniew Bodek 			}
14409b8d05b8SZbigniew Bodek 			break;
14419b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV6:
14429b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
14439b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14449b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
14459b8d05b8SZbigniew Bodek 				break;
14469b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14479b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
14489b8d05b8SZbigniew Bodek 				break;
14499b8d05b8SZbigniew Bodek 			default:
14509b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
14519b8d05b8SZbigniew Bodek 			}
14529b8d05b8SZbigniew Bodek 			break;
14539b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
14549b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
14559b8d05b8SZbigniew Bodek 			break;
14569b8d05b8SZbigniew Bodek 		default:
14579b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
14589b8d05b8SZbigniew Bodek 		}
14599b8d05b8SZbigniew Bodek 	} else {
14609b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = rx_ring->qid;
14619b8d05b8SZbigniew Bodek 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
14629b8d05b8SZbigniew Bodek 	}
14639b8d05b8SZbigniew Bodek }
14649b8d05b8SZbigniew Bodek 
14659b8d05b8SZbigniew Bodek /**
14669b8d05b8SZbigniew Bodek  * ena_rx_mbuf - assemble mbuf from descriptors
14679b8d05b8SZbigniew Bodek  * @rx_ring: ring for which we want to clean packets
14689b8d05b8SZbigniew Bodek  * @ena_bufs: buffer info
14699b8d05b8SZbigniew Bodek  * @ena_rx_ctx: metadata for this packet(s)
147043fefd16SMarcin Wojtas  * @next_to_clean: ring pointer, will be updated only upon success
14719b8d05b8SZbigniew Bodek  *
14729b8d05b8SZbigniew Bodek  **/
14739b8d05b8SZbigniew Bodek static struct mbuf*
14749b8d05b8SZbigniew Bodek ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
14759b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
14769b8d05b8SZbigniew Bodek {
14779b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
14789b8d05b8SZbigniew Bodek 	struct ena_rx_buffer *rx_info;
14799b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
14809b8d05b8SZbigniew Bodek 	unsigned int descs = ena_rx_ctx->descs;
1481c51a229cSMarcin Wojtas 	int rc;
148243fefd16SMarcin Wojtas 	uint16_t ntc, len, req_id, buf = 0;
14839b8d05b8SZbigniew Bodek 
148443fefd16SMarcin Wojtas 	ntc = *next_to_clean;
14859b8d05b8SZbigniew Bodek 	adapter = rx_ring->adapter;
14869b8d05b8SZbigniew Bodek 
148743fefd16SMarcin Wojtas 	len = ena_bufs[buf].len;
148843fefd16SMarcin Wojtas 	req_id = ena_bufs[buf].req_id;
1489c51a229cSMarcin Wojtas 	rc = validate_rx_req_id(rx_ring, req_id);
1490c51a229cSMarcin Wojtas 	if (unlikely(rc != 0))
1491c51a229cSMarcin Wojtas 		return (NULL);
1492c51a229cSMarcin Wojtas 
149343fefd16SMarcin Wojtas 	rx_info = &rx_ring->rx_buffer_info[req_id];
14941d65b4c0SMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
14951d65b4c0SMarcin Wojtas 		device_printf(adapter->pdev, "NULL mbuf in rx_info");
14961d65b4c0SMarcin Wojtas 		return (NULL);
14971d65b4c0SMarcin Wojtas 	}
149843fefd16SMarcin Wojtas 
14999b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
15009b8d05b8SZbigniew Bodek 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
15019b8d05b8SZbigniew Bodek 
1502e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1503e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
15049b8d05b8SZbigniew Bodek 	mbuf = rx_info->mbuf;
15059b8d05b8SZbigniew Bodek 	mbuf->m_flags |= M_PKTHDR;
15069b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.len = len;
15079b8d05b8SZbigniew Bodek 	mbuf->m_len = len;
15089b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.rcvif = rx_ring->que->adapter->ifp;
15099b8d05b8SZbigniew Bodek 
15109b8d05b8SZbigniew Bodek 	/* Fill mbuf with hash key and it's interpretation for optimization */
15119b8d05b8SZbigniew Bodek 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
15129b8d05b8SZbigniew Bodek 
15139b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
15149b8d05b8SZbigniew Bodek 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
15159b8d05b8SZbigniew Bodek 
15169b8d05b8SZbigniew Bodek 	/* DMA address is not needed anymore, unmap it */
15179b8d05b8SZbigniew Bodek 	bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
15189b8d05b8SZbigniew Bodek 
15199b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
152043fefd16SMarcin Wojtas 	rx_ring->free_rx_ids[ntc] = req_id;
152143fefd16SMarcin Wojtas 	ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
15229b8d05b8SZbigniew Bodek 
15239b8d05b8SZbigniew Bodek 	/*
15249b8d05b8SZbigniew Bodek 	 * While we have more than 1 descriptors for one rcvd packet, append
15259b8d05b8SZbigniew Bodek 	 * other mbufs to the main one
15269b8d05b8SZbigniew Bodek 	 */
15279b8d05b8SZbigniew Bodek 	while (--descs) {
152843fefd16SMarcin Wojtas 		++buf;
152943fefd16SMarcin Wojtas 		len = ena_bufs[buf].len;
153043fefd16SMarcin Wojtas 		req_id = ena_bufs[buf].req_id;
1531c51a229cSMarcin Wojtas 		rc = validate_rx_req_id(rx_ring, req_id);
1532c51a229cSMarcin Wojtas 		if (unlikely(rc != 0)) {
1533c51a229cSMarcin Wojtas 			/*
1534c51a229cSMarcin Wojtas 			 * If the req_id is invalid, then the device will be
1535c51a229cSMarcin Wojtas 			 * reset. In that case we must free all mbufs that
1536c51a229cSMarcin Wojtas 			 * were already gathered.
1537c51a229cSMarcin Wojtas 			 */
1538c51a229cSMarcin Wojtas 			m_freem(mbuf);
1539c51a229cSMarcin Wojtas 			return (NULL);
1540c51a229cSMarcin Wojtas 		}
154143fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
15429b8d05b8SZbigniew Bodek 
154343fefd16SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
154443fefd16SMarcin Wojtas 			device_printf(adapter->pdev, "NULL mbuf in rx_info");
154543fefd16SMarcin Wojtas 			/*
154643fefd16SMarcin Wojtas 			 * If one of the required mbufs was not allocated yet,
154743fefd16SMarcin Wojtas 			 * we can break there.
154843fefd16SMarcin Wojtas 			 * All earlier used descriptors will be reallocated
154943fefd16SMarcin Wojtas 			 * later and not used mbufs can be reused.
155043fefd16SMarcin Wojtas 			 * The next_to_clean pointer will not be updated in case
155143fefd16SMarcin Wojtas 			 * of an error, so caller should advance it manually
155243fefd16SMarcin Wojtas 			 * in error handling routine to keep it up to date
155343fefd16SMarcin Wojtas 			 * with hw ring.
155443fefd16SMarcin Wojtas 			 */
155543fefd16SMarcin Wojtas 			m_freem(mbuf);
155643fefd16SMarcin Wojtas 			return (NULL);
155743fefd16SMarcin Wojtas 		}
155843fefd16SMarcin Wojtas 
1559e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1560e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTREAD);
15613f9ed7abSMarcin Wojtas 		if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
15629b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
15639b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
15649b8d05b8SZbigniew Bodek 			    mbuf);
15659b8d05b8SZbigniew Bodek 		}
15664e8acd84SMarcin Wojtas 
15674e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH,
15684e8acd84SMarcin Wojtas 		    "rx mbuf updated. len %d", mbuf->m_pkthdr.len);
15694e8acd84SMarcin Wojtas 
15709b8d05b8SZbigniew Bodek 		/* Free already appended mbuf, it won't be useful anymore */
15719b8d05b8SZbigniew Bodek 		bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
15729b8d05b8SZbigniew Bodek 		m_freem(rx_info->mbuf);
15739b8d05b8SZbigniew Bodek 		rx_info->mbuf = NULL;
15749b8d05b8SZbigniew Bodek 
157543fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[ntc] = req_id;
157643fefd16SMarcin Wojtas 		ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
15779b8d05b8SZbigniew Bodek 	}
15789b8d05b8SZbigniew Bodek 
157943fefd16SMarcin Wojtas 	*next_to_clean = ntc;
158043fefd16SMarcin Wojtas 
15819b8d05b8SZbigniew Bodek 	return (mbuf);
15829b8d05b8SZbigniew Bodek }
15839b8d05b8SZbigniew Bodek 
15849b8d05b8SZbigniew Bodek /**
15859b8d05b8SZbigniew Bodek  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
15869b8d05b8SZbigniew Bodek  **/
15879b8d05b8SZbigniew Bodek static inline void
15889b8d05b8SZbigniew Bodek ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
15899b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
15909b8d05b8SZbigniew Bodek {
15919b8d05b8SZbigniew Bodek 
15929b8d05b8SZbigniew Bodek 	/* if IP and error */
15933f9ed7abSMarcin Wojtas 	if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
15943f9ed7abSMarcin Wojtas 	    ena_rx_ctx->l3_csum_err)) {
15959b8d05b8SZbigniew Bodek 		/* ipv4 checksum error */
15969b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.csum_flags = 0;
15979b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
15984e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG, "RX IPv4 header checksum error");
15999b8d05b8SZbigniew Bodek 		return;
16009b8d05b8SZbigniew Bodek 	}
16019b8d05b8SZbigniew Bodek 
16029b8d05b8SZbigniew Bodek 	/* if TCP/UDP */
16039b8d05b8SZbigniew Bodek 	if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
16049b8d05b8SZbigniew Bodek 	    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
16059b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->l4_csum_err) {
16069b8d05b8SZbigniew Bodek 			/* TCP/UDP checksum error */
16079b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = 0;
16089b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
16094e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG, "RX L4 checksum error");
16109b8d05b8SZbigniew Bodek 		} else {
16119b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
16129b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
16139b8d05b8SZbigniew Bodek 		}
16149b8d05b8SZbigniew Bodek 	}
16159b8d05b8SZbigniew Bodek }
16169b8d05b8SZbigniew Bodek 
16179b8d05b8SZbigniew Bodek /**
16189b8d05b8SZbigniew Bodek  * ena_rx_cleanup - handle rx irq
16199b8d05b8SZbigniew Bodek  * @arg: ring for which irq is being handled
16209b8d05b8SZbigniew Bodek  **/
16219b8d05b8SZbigniew Bodek static int
16229b8d05b8SZbigniew Bodek ena_rx_cleanup(struct ena_ring *rx_ring)
16239b8d05b8SZbigniew Bodek {
16249b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
16259b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
16269b8d05b8SZbigniew Bodek 	struct ena_com_rx_ctx ena_rx_ctx;
16279b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
16289b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
16299b8d05b8SZbigniew Bodek 	if_t ifp;
16309b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
16319b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
16329b8d05b8SZbigniew Bodek 	uint32_t refill_required;
16339b8d05b8SZbigniew Bodek 	uint32_t refill_threshold;
16349b8d05b8SZbigniew Bodek 	uint32_t do_if_input = 0;
16359b8d05b8SZbigniew Bodek 	unsigned int qid;
163643fefd16SMarcin Wojtas 	int rc, i;
16379b8d05b8SZbigniew Bodek 	int budget = RX_BUDGET;
16389b8d05b8SZbigniew Bodek 
16399b8d05b8SZbigniew Bodek 	adapter = rx_ring->que->adapter;
16409b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
16419b8d05b8SZbigniew Bodek 	qid = rx_ring->que->id;
16429b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_RXQ_IDX(qid);
16439b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
16449b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
16459b8d05b8SZbigniew Bodek 	next_to_clean = rx_ring->next_to_clean;
16469b8d05b8SZbigniew Bodek 
16474e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG, "rx: qid %d", qid);
16484e8acd84SMarcin Wojtas 
16499b8d05b8SZbigniew Bodek 	do {
16509b8d05b8SZbigniew Bodek 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
16519b8d05b8SZbigniew Bodek 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
16529b8d05b8SZbigniew Bodek 		ena_rx_ctx.descs = 0;
1653e8073738SMarcin Wojtas 		bus_dmamap_sync(io_cq->cdesc_addr.mem_handle.tag,
1654e8073738SMarcin Wojtas 		    io_cq->cdesc_addr.mem_handle.map, BUS_DMASYNC_POSTREAD);
16559b8d05b8SZbigniew Bodek 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
16569b8d05b8SZbigniew Bodek 
16570bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
16589b8d05b8SZbigniew Bodek 			goto error;
16599b8d05b8SZbigniew Bodek 
16609b8d05b8SZbigniew Bodek 		if (unlikely(ena_rx_ctx.descs == 0))
16619b8d05b8SZbigniew Bodek 			break;
16629b8d05b8SZbigniew Bodek 
16634e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
16644e8acd84SMarcin Wojtas 		    "descs #: %d l3 proto %d l4 proto %d hash: %x",
16654e8acd84SMarcin Wojtas 		    rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
16664e8acd84SMarcin Wojtas 		    ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
16674e8acd84SMarcin Wojtas 
16689b8d05b8SZbigniew Bodek 		/* Receive mbuf from the ring */
16699b8d05b8SZbigniew Bodek 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
16709b8d05b8SZbigniew Bodek 		    &ena_rx_ctx, &next_to_clean);
1671e8073738SMarcin Wojtas 		bus_dmamap_sync(io_cq->cdesc_addr.mem_handle.tag,
1672e8073738SMarcin Wojtas 		    io_cq->cdesc_addr.mem_handle.map, BUS_DMASYNC_PREREAD);
16739b8d05b8SZbigniew Bodek 		/* Exit if we failed to retrieve a buffer */
16740bdffe59SMarcin Wojtas 		if (unlikely(mbuf == NULL)) {
167543fefd16SMarcin Wojtas 			for (i = 0; i < ena_rx_ctx.descs; ++i) {
167643fefd16SMarcin Wojtas 				rx_ring->free_rx_ids[next_to_clean] =
167743fefd16SMarcin Wojtas 				    rx_ring->ena_bufs[i].req_id;
167843fefd16SMarcin Wojtas 				next_to_clean =
167943fefd16SMarcin Wojtas 				    ENA_RX_RING_IDX_NEXT(next_to_clean,
168043fefd16SMarcin Wojtas 				    rx_ring->ring_size);
168143fefd16SMarcin Wojtas 
168243fefd16SMarcin Wojtas 			}
16839b8d05b8SZbigniew Bodek 			break;
16849b8d05b8SZbigniew Bodek 		}
16859b8d05b8SZbigniew Bodek 
16860bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_RXCSUM) != 0) ||
16870bdffe59SMarcin Wojtas 		    ((ifp->if_capenable & IFCAP_RXCSUM_IPV6) != 0)) {
16889b8d05b8SZbigniew Bodek 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
16899b8d05b8SZbigniew Bodek 		}
16909b8d05b8SZbigniew Bodek 
169130217e2dSMarcin Wojtas 		counter_enter();
169230217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
169330217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
169430217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
169530217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
169630217e2dSMarcin Wojtas 		counter_exit();
16979b8d05b8SZbigniew Bodek 		/*
16989b8d05b8SZbigniew Bodek 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
16999b8d05b8SZbigniew Bodek 		 * should be computed by hardware.
17009b8d05b8SZbigniew Bodek 		 */
17019b8d05b8SZbigniew Bodek 		do_if_input = 1;
17020bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_LRO) != 0)  &&
17030bdffe59SMarcin Wojtas 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
17040bdffe59SMarcin Wojtas 		    (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
17059b8d05b8SZbigniew Bodek 			/*
17069b8d05b8SZbigniew Bodek 			 * Send to the stack if:
17079b8d05b8SZbigniew Bodek 			 *  - LRO not enabled, or
17089b8d05b8SZbigniew Bodek 			 *  - no LRO resources, or
17099b8d05b8SZbigniew Bodek 			 *  - lro enqueue fails
17109b8d05b8SZbigniew Bodek 			 */
17110bdffe59SMarcin Wojtas 			if ((rx_ring->lro.lro_cnt != 0) &&
17120bdffe59SMarcin Wojtas 			    (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
17139b8d05b8SZbigniew Bodek 					do_if_input = 0;
17149b8d05b8SZbigniew Bodek 		}
17150bdffe59SMarcin Wojtas 		if (do_if_input != 0) {
17160bdffe59SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_RXPTH,
17170bdffe59SMarcin Wojtas 			    "calling if_input() with mbuf %p", mbuf);
17189b8d05b8SZbigniew Bodek 			(*ifp->if_input)(ifp, mbuf);
17199b8d05b8SZbigniew Bodek 		}
17209b8d05b8SZbigniew Bodek 
172130217e2dSMarcin Wojtas 		counter_enter();
172230217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
172330217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
172430217e2dSMarcin Wojtas 		counter_exit();
17259b8d05b8SZbigniew Bodek 	} while (--budget);
17269b8d05b8SZbigniew Bodek 
17279b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = next_to_clean;
17289b8d05b8SZbigniew Bodek 
1729a195fab0SMarcin Wojtas 	refill_required = ena_com_free_desc(io_sq);
17305a990212SMarcin Wojtas 	refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
17319b8d05b8SZbigniew Bodek 
17329b8d05b8SZbigniew Bodek 	if (refill_required > refill_threshold) {
17339b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
17349b8d05b8SZbigniew Bodek 		ena_refill_rx_bufs(rx_ring, refill_required);
17359b8d05b8SZbigniew Bodek 	}
17369b8d05b8SZbigniew Bodek 
17379b8d05b8SZbigniew Bodek 	tcp_lro_flush_all(&rx_ring->lro);
17389b8d05b8SZbigniew Bodek 
17399b8d05b8SZbigniew Bodek 	return (RX_BUDGET - budget);
17409b8d05b8SZbigniew Bodek 
17419b8d05b8SZbigniew Bodek error:
17429b8d05b8SZbigniew Bodek 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
1743c9b099ecSMarcin Wojtas 
1744c9b099ecSMarcin Wojtas 	/* Too many desc from the device. Trigger reset */
1745c9b099ecSMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
1746c9b099ecSMarcin Wojtas 	adapter->trigger_reset = true;
1747c9b099ecSMarcin Wojtas 
1748c9b099ecSMarcin Wojtas 	return (0);
17499b8d05b8SZbigniew Bodek }
17509b8d05b8SZbigniew Bodek 
17519b8d05b8SZbigniew Bodek /*********************************************************************
17529b8d05b8SZbigniew Bodek  *
17539b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
17549b8d05b8SZbigniew Bodek  *
17559b8d05b8SZbigniew Bodek  **********************************************************************/
17569b8d05b8SZbigniew Bodek 
17579b8d05b8SZbigniew Bodek /**
17589b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
17599b8d05b8SZbigniew Bodek  * @arg: interrupt number
17609b8d05b8SZbigniew Bodek  **/
17619b8d05b8SZbigniew Bodek static void
17629b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
17639b8d05b8SZbigniew Bodek {
17649b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
17659b8d05b8SZbigniew Bodek 
17669b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
17679b8d05b8SZbigniew Bodek 	if (likely(adapter->running))
17689b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
17699b8d05b8SZbigniew Bodek }
17709b8d05b8SZbigniew Bodek 
17719b8d05b8SZbigniew Bodek static void
1772*5cb9db07SMarcin Wojtas ena_cleanup(void *arg, int pending)
17739b8d05b8SZbigniew Bodek {
17749b8d05b8SZbigniew Bodek 	struct ena_que	*que = arg;
17759b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = que->adapter;
17769b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
17779b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
17789b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
17799b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
17809b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
17819b8d05b8SZbigniew Bodek 	int qid, ena_qid;
17829b8d05b8SZbigniew Bodek 	int txc, rxc, i;
17839b8d05b8SZbigniew Bodek 
17843f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
17859b8d05b8SZbigniew Bodek 		return;
17869b8d05b8SZbigniew Bodek 
17879b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "MSI-X TX/RX routine");
17889b8d05b8SZbigniew Bodek 
17899b8d05b8SZbigniew Bodek 	tx_ring = que->tx_ring;
17909b8d05b8SZbigniew Bodek 	rx_ring = que->rx_ring;
17919b8d05b8SZbigniew Bodek 	qid = que->id;
17929b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(qid);
17939b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
17949b8d05b8SZbigniew Bodek 
1795d12f7bfcSMarcin Wojtas 	tx_ring->first_interrupt = true;
1796d12f7bfcSMarcin Wojtas 	rx_ring->first_interrupt = true;
1797d12f7bfcSMarcin Wojtas 
17989b8d05b8SZbigniew Bodek 	for (i = 0; i < CLEAN_BUDGET; ++i) {
17999b8d05b8SZbigniew Bodek 		rxc = ena_rx_cleanup(rx_ring);
18009b8d05b8SZbigniew Bodek 		txc = ena_tx_cleanup(tx_ring);
18019b8d05b8SZbigniew Bodek 
18023f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
18039b8d05b8SZbigniew Bodek 			return;
18049b8d05b8SZbigniew Bodek 
18050bdffe59SMarcin Wojtas 		if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
18069b8d05b8SZbigniew Bodek 		       break;
18079b8d05b8SZbigniew Bodek 	}
18089b8d05b8SZbigniew Bodek 
18099b8d05b8SZbigniew Bodek 	/* Signal that work is done and unmask interrupt */
18109b8d05b8SZbigniew Bodek 	ena_com_update_intr_reg(&intr_reg,
18119b8d05b8SZbigniew Bodek 	    RX_IRQ_INTERVAL,
18129b8d05b8SZbigniew Bodek 	    TX_IRQ_INTERVAL,
18139b8d05b8SZbigniew Bodek 	    true);
18149b8d05b8SZbigniew Bodek 	ena_com_unmask_intr(io_cq, &intr_reg);
18159b8d05b8SZbigniew Bodek }
18169b8d05b8SZbigniew Bodek 
1817*5cb9db07SMarcin Wojtas /**
1818*5cb9db07SMarcin Wojtas  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
1819*5cb9db07SMarcin Wojtas  * @arg: queue
1820*5cb9db07SMarcin Wojtas  **/
1821*5cb9db07SMarcin Wojtas static int
1822*5cb9db07SMarcin Wojtas ena_handle_msix(void *arg)
1823*5cb9db07SMarcin Wojtas {
1824*5cb9db07SMarcin Wojtas 	struct ena_que *queue = arg;
1825*5cb9db07SMarcin Wojtas 	struct ena_adapter *adapter = queue->adapter;
1826*5cb9db07SMarcin Wojtas 	if_t ifp = adapter->ifp;
1827*5cb9db07SMarcin Wojtas 
1828*5cb9db07SMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
1829*5cb9db07SMarcin Wojtas 		return (FILTER_STRAY);
1830*5cb9db07SMarcin Wojtas 
1831*5cb9db07SMarcin Wojtas 	taskqueue_enqueue(queue->cleanup_tq, &queue->cleanup_task);
1832*5cb9db07SMarcin Wojtas 
1833*5cb9db07SMarcin Wojtas 	return (FILTER_HANDLED);
1834*5cb9db07SMarcin Wojtas }
1835*5cb9db07SMarcin Wojtas 
18369b8d05b8SZbigniew Bodek static int
18379b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
18389b8d05b8SZbigniew Bodek {
18399b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
18408805021aSMarcin Wojtas 	int msix_vecs, msix_req;
18418805021aSMarcin Wojtas 	int i, rc = 0;
18429b8d05b8SZbigniew Bodek 
18439b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
18449b8d05b8SZbigniew Bodek 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues);
18459b8d05b8SZbigniew Bodek 
1846cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1847cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1848cd5d5804SMarcin Wojtas 
18494e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d", msix_vecs);
18509b8d05b8SZbigniew Bodek 
18519b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
18529b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
18539b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
18549b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
18559b8d05b8SZbigniew Bodek 	}
18569b8d05b8SZbigniew Bodek 
18578805021aSMarcin Wojtas 	msix_req = msix_vecs;
18589b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
18593f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
18609b8d05b8SZbigniew Bodek 		device_printf(dev,
18619b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
18627d2544e6SMarcin Wojtas 
18639b8d05b8SZbigniew Bodek 		rc = ENOSPC;
18647d2544e6SMarcin Wojtas 		goto err_msix_free;
18659b8d05b8SZbigniew Bodek 	}
18669b8d05b8SZbigniew Bodek 
18678805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
18682b5b60feSMarcin Wojtas 		if (msix_vecs == ENA_ADMIN_MSIX_VEC) {
18692b5b60feSMarcin Wojtas 			device_printf(dev,
18702b5b60feSMarcin Wojtas 			    "Not enough number of MSI-x allocated: %d\n",
18712b5b60feSMarcin Wojtas 			    msix_vecs);
18722b5b60feSMarcin Wojtas 			pci_release_msi(dev);
18732b5b60feSMarcin Wojtas 			rc = ENOSPC;
18742b5b60feSMarcin Wojtas 			goto err_msix_free;
18752b5b60feSMarcin Wojtas 		}
18768805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
18778805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
18788805021aSMarcin Wojtas 		adapter->num_queues = msix_vecs - ENA_ADMIN_MSIX_VEC;
18798805021aSMarcin Wojtas 	}
18808805021aSMarcin Wojtas 
18819b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
18829b8d05b8SZbigniew Bodek 	adapter->msix_enabled = true;
18839b8d05b8SZbigniew Bodek 
18847d2544e6SMarcin Wojtas 	return (0);
18857d2544e6SMarcin Wojtas 
18867d2544e6SMarcin Wojtas err_msix_free:
18877d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
18887d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
18897d2544e6SMarcin Wojtas 
18909b8d05b8SZbigniew Bodek 	return (rc);
18919b8d05b8SZbigniew Bodek }
18929b8d05b8SZbigniew Bodek 
18939b8d05b8SZbigniew Bodek static void
18949b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
18959b8d05b8SZbigniew Bodek {
18969b8d05b8SZbigniew Bodek 
18979b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
18989b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
18999b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
19009b8d05b8SZbigniew Bodek 	/*
19019b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
19029b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
19039b8d05b8SZbigniew Bodek 	 */
19049b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
19059b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
19069b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
19079b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
19089b8d05b8SZbigniew Bodek }
19099b8d05b8SZbigniew Bodek 
19109b8d05b8SZbigniew Bodek static void
19119b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
19129b8d05b8SZbigniew Bodek {
19139b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
19149b8d05b8SZbigniew Bodek 	int irq_idx;
19159b8d05b8SZbigniew Bodek 
19169b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++) {
19179b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
19189b8d05b8SZbigniew Bodek 
19199b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
19209b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
19219b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
19229b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
19239b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
19249b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
19259b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
19269b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
1927277f11c4SMarcin Wojtas 
19289b8d05b8SZbigniew Bodek 		/*
1929277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
19309b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
19319b8d05b8SZbigniew Bodek 		 */
19323f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
19339b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
19349b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
19359b8d05b8SZbigniew Bodek 		    last_bind_cpu;
19369b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
19379b8d05b8SZbigniew Bodek 	}
19389b8d05b8SZbigniew Bodek }
19399b8d05b8SZbigniew Bodek 
19409b8d05b8SZbigniew Bodek static int
19419b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
19429b8d05b8SZbigniew Bodek {
19439b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19449b8d05b8SZbigniew Bodek 	unsigned long flags;
19459b8d05b8SZbigniew Bodek 	int rc, rcc;
19469b8d05b8SZbigniew Bodek 
19479b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
19489b8d05b8SZbigniew Bodek 
19499b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
19509b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
19519b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
19529b8d05b8SZbigniew Bodek 
19533f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
19549b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
19559b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
19567d2544e6SMarcin Wojtas 		return (ENXIO);
19579b8d05b8SZbigniew Bodek 	}
19589b8d05b8SZbigniew Bodek 
19590bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
19600bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
19610bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
19623f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
19639b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
19649b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
19659b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
19667d2544e6SMarcin Wojtas 		goto err_res_free;
19679b8d05b8SZbigniew Bodek 	}
19689b8d05b8SZbigniew Bodek 	irq->requested = true;
19699b8d05b8SZbigniew Bodek 
19709b8d05b8SZbigniew Bodek 	return (rc);
19719b8d05b8SZbigniew Bodek 
19727d2544e6SMarcin Wojtas err_res_free:
19734e8acd84SMarcin Wojtas 	ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
19747d2544e6SMarcin Wojtas 	    irq->vector);
19759b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
19769b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
19773f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
19789b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
19799b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
19809b8d05b8SZbigniew Bodek 	irq->res = NULL;
19819b8d05b8SZbigniew Bodek 
19829b8d05b8SZbigniew Bodek 	return (rc);
19839b8d05b8SZbigniew Bodek }
19849b8d05b8SZbigniew Bodek 
19859b8d05b8SZbigniew Bodek static int
19869b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
19879b8d05b8SZbigniew Bodek {
19889b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19899b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
19909b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
19919b8d05b8SZbigniew Bodek 
19923f9ed7abSMarcin Wojtas 	if (unlikely(adapter->msix_enabled == 0)) {
19934e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
19944e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
19959b8d05b8SZbigniew Bodek 		return (EINVAL);
19969b8d05b8SZbigniew Bodek 	} else {
19979b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
19989b8d05b8SZbigniew Bodek 	}
19999b8d05b8SZbigniew Bodek 
20009b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
20019b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20029b8d05b8SZbigniew Bodek 
20033f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
20049b8d05b8SZbigniew Bodek 			continue;
20059b8d05b8SZbigniew Bodek 
20069b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
20079b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
20083f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
2009469a8407SMarcin Wojtas 			rc = ENOMEM;
20109b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
20119b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
20129b8d05b8SZbigniew Bodek 			goto err;
20139b8d05b8SZbigniew Bodek 		}
20149b8d05b8SZbigniew Bodek 
20150bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
2016*5cb9db07SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, irq->handler, NULL,
2017*5cb9db07SMarcin Wojtas 		    irq->data, &irq->cookie);
20183f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
20199b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
20209b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
20219b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
20229b8d05b8SZbigniew Bodek 			goto err;
20239b8d05b8SZbigniew Bodek 		}
20249b8d05b8SZbigniew Bodek 		irq->requested = true;
20259b8d05b8SZbigniew Bodek 
20264e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
20279b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
20289b8d05b8SZbigniew Bodek 	}
20299b8d05b8SZbigniew Bodek 
20309b8d05b8SZbigniew Bodek 	return (rc);
20319b8d05b8SZbigniew Bodek 
20329b8d05b8SZbigniew Bodek err:
20339b8d05b8SZbigniew Bodek 
20349b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
20359b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20369b8d05b8SZbigniew Bodek 		rcc = 0;
20379b8d05b8SZbigniew Bodek 
20389b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
20399b8d05b8SZbigniew Bodek 		   free both intr and resources */
20400bdffe59SMarcin Wojtas 		if (irq->requested)
20419b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
20423f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
20439b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
20449b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
20459b8d05b8SZbigniew Bodek 
20469b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
20479b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
20489b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
20499b8d05b8SZbigniew Bodek 		   this iteration */
20509b8d05b8SZbigniew Bodek 		rcc = 0;
20519b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
20529b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20539b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
20549b8d05b8SZbigniew Bodek 		}
20553f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
20569b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
20579b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
20589b8d05b8SZbigniew Bodek 		irq->requested = false;
20599b8d05b8SZbigniew Bodek 		irq->res = NULL;
20609b8d05b8SZbigniew Bodek 	}
20619b8d05b8SZbigniew Bodek 
20629b8d05b8SZbigniew Bodek 	return (rc);
20639b8d05b8SZbigniew Bodek }
20649b8d05b8SZbigniew Bodek 
20659b8d05b8SZbigniew Bodek static void
20669b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
20679b8d05b8SZbigniew Bodek {
20689b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20699b8d05b8SZbigniew Bodek 	int rc;
20709b8d05b8SZbigniew Bodek 
20719b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
20729b8d05b8SZbigniew Bodek 	if (irq->requested) {
20739b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
20749b8d05b8SZbigniew Bodek 		    irq->vector);
20759b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
20763f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
20779b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
20789b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
20799b8d05b8SZbigniew Bodek 		irq->requested = 0;
20809b8d05b8SZbigniew Bodek 	}
20819b8d05b8SZbigniew Bodek 
20829b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
20839b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
20849b8d05b8SZbigniew Bodek 		    irq->vector);
20859b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20869b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
20879b8d05b8SZbigniew Bodek 		irq->res = NULL;
20883f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
20899b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
20909b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
20919b8d05b8SZbigniew Bodek 	}
20929b8d05b8SZbigniew Bodek }
20939b8d05b8SZbigniew Bodek 
20949b8d05b8SZbigniew Bodek static void
20959b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
20969b8d05b8SZbigniew Bodek {
20979b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20989b8d05b8SZbigniew Bodek 	int rc;
20999b8d05b8SZbigniew Bodek 
21009b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
21019b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
21029b8d05b8SZbigniew Bodek 		if (irq->requested) {
21039b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
21049b8d05b8SZbigniew Bodek 			    irq->vector);
21059b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
21069b8d05b8SZbigniew Bodek 			    irq->cookie);
21073f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
21089b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
21099b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
21109b8d05b8SZbigniew Bodek 			}
21119b8d05b8SZbigniew Bodek 			irq->requested = 0;
21129b8d05b8SZbigniew Bodek 		}
21139b8d05b8SZbigniew Bodek 
21149b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
21159b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
21169b8d05b8SZbigniew Bodek 			    irq->vector);
21179b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
21189b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
21199b8d05b8SZbigniew Bodek 			irq->res = NULL;
21203f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
21219b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
21229b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
21239b8d05b8SZbigniew Bodek 				    irq->vector);
21249b8d05b8SZbigniew Bodek 			}
21259b8d05b8SZbigniew Bodek 		}
21269b8d05b8SZbigniew Bodek 	}
21279b8d05b8SZbigniew Bodek }
21289b8d05b8SZbigniew Bodek 
21299b8d05b8SZbigniew Bodek static void
21309b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
21319b8d05b8SZbigniew Bodek {
21329b8d05b8SZbigniew Bodek 
21339b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
21349b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
21359b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
21369b8d05b8SZbigniew Bodek }
21379b8d05b8SZbigniew Bodek 
21389b8d05b8SZbigniew Bodek static void
21399b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
21409b8d05b8SZbigniew Bodek {
21419b8d05b8SZbigniew Bodek 
21429b8d05b8SZbigniew Bodek 	pci_release_msi(adapter->pdev);
21439b8d05b8SZbigniew Bodek 
21449b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
2145cd5d5804SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
21469b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
21479b8d05b8SZbigniew Bodek }
21489b8d05b8SZbigniew Bodek 
21499b8d05b8SZbigniew Bodek static void
21509b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
21519b8d05b8SZbigniew Bodek {
21529b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
21539b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
21549b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
21559b8d05b8SZbigniew Bodek 	int i;
21569b8d05b8SZbigniew Bodek 
21579b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
21589b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
21599b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
21609b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
21619b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
21629b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
21639b8d05b8SZbigniew Bodek 	}
21649b8d05b8SZbigniew Bodek }
21659b8d05b8SZbigniew Bodek 
21669b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
21670bdffe59SMarcin Wojtas static int
21680bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
21699b8d05b8SZbigniew Bodek {
21709b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
21719b8d05b8SZbigniew Bodek 	int rc;
21729b8d05b8SZbigniew Bodek 
21739b8d05b8SZbigniew Bodek 	/* Set indirect table */
21749b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
21750bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21760bdffe59SMarcin Wojtas 		return (rc);
21779b8d05b8SZbigniew Bodek 
21789b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
21799b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
21800bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21810bdffe59SMarcin Wojtas 		return (rc);
21829b8d05b8SZbigniew Bodek 
21839b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
21849b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
21850bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21860bdffe59SMarcin Wojtas 		return (rc);
21879b8d05b8SZbigniew Bodek 
21880bdffe59SMarcin Wojtas 	return (0);
21899b8d05b8SZbigniew Bodek }
21909b8d05b8SZbigniew Bodek 
21919b8d05b8SZbigniew Bodek static int
21929b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
21939b8d05b8SZbigniew Bodek {
21949b8d05b8SZbigniew Bodek 	int rc;
21959b8d05b8SZbigniew Bodek 
21963f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support)) {
21979b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
21980bdffe59SMarcin Wojtas 		if (rc != 0)
21999b8d05b8SZbigniew Bodek 			return (rc);
22009b8d05b8SZbigniew Bodek 	}
22019b8d05b8SZbigniew Bodek 
22027d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
22033f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
22047d2544e6SMarcin Wojtas 		return (rc);
22057d2544e6SMarcin Wojtas 
22069b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
220730217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
220830217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
22099b8d05b8SZbigniew Bodek 
22109b8d05b8SZbigniew Bodek 	return (0);
22119b8d05b8SZbigniew Bodek }
22129b8d05b8SZbigniew Bodek 
22139b8d05b8SZbigniew Bodek static int
22149b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
22159b8d05b8SZbigniew Bodek {
22169b8d05b8SZbigniew Bodek 	int rc = 0;
22179b8d05b8SZbigniew Bodek 
22183f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
22199b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
22209b8d05b8SZbigniew Bodek 		return (ENXIO);
22219b8d05b8SZbigniew Bodek 	}
22229b8d05b8SZbigniew Bodek 
22233f9ed7abSMarcin Wojtas 	if (unlikely(!adapter->running)) {
22249b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not running!\n");
22259b8d05b8SZbigniew Bodek 		return (ENXIO);
22269b8d05b8SZbigniew Bodek 	}
22279b8d05b8SZbigniew Bodek 
22289b8d05b8SZbigniew Bodek 	if (!adapter->up) {
22299b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going UP\n");
22309b8d05b8SZbigniew Bodek 
22319b8d05b8SZbigniew Bodek 		/* setup interrupts for IO queues */
22329b8d05b8SZbigniew Bodek 		ena_setup_io_intr(adapter);
22339b8d05b8SZbigniew Bodek 		rc = ena_request_io_irq(adapter);
22343f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22359b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_req_irq");
22369b8d05b8SZbigniew Bodek 			goto err_req_irq;
22379b8d05b8SZbigniew Bodek 		}
22389b8d05b8SZbigniew Bodek 
22399b8d05b8SZbigniew Bodek 		/* allocate transmit descriptors */
22409b8d05b8SZbigniew Bodek 		rc = ena_setup_all_tx_resources(adapter);
22413f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22429b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_tx");
22439b8d05b8SZbigniew Bodek 			goto err_setup_tx;
22449b8d05b8SZbigniew Bodek 		}
22459b8d05b8SZbigniew Bodek 
22469b8d05b8SZbigniew Bodek 		/* allocate receive descriptors */
22479b8d05b8SZbigniew Bodek 		rc = ena_setup_all_rx_resources(adapter);
22483f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22499b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_rx");
22509b8d05b8SZbigniew Bodek 			goto err_setup_rx;
22519b8d05b8SZbigniew Bodek 		}
22529b8d05b8SZbigniew Bodek 
22539b8d05b8SZbigniew Bodek 		/* create IO queues for Rx & Tx */
22549b8d05b8SZbigniew Bodek 		rc = ena_create_io_queues(adapter);
22553f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22569b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT,
22579b8d05b8SZbigniew Bodek 			    "create IO queues failed");
22589b8d05b8SZbigniew Bodek 			goto err_io_que;
22599b8d05b8SZbigniew Bodek 		}
22609b8d05b8SZbigniew Bodek 
22613f9ed7abSMarcin Wojtas 		if (unlikely(adapter->link_status))
22629b8d05b8SZbigniew Bodek 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
22639b8d05b8SZbigniew Bodek 
22649b8d05b8SZbigniew Bodek 		rc = ena_up_complete(adapter);
22653f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
22669b8d05b8SZbigniew Bodek 			goto err_up_complete;
22679b8d05b8SZbigniew Bodek 
22689b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_up, 1);
22699b8d05b8SZbigniew Bodek 
22709b8d05b8SZbigniew Bodek 		ena_update_hwassist(adapter);
22719b8d05b8SZbigniew Bodek 
22729b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
22739b8d05b8SZbigniew Bodek 		    IFF_DRV_OACTIVE);
22749b8d05b8SZbigniew Bodek 
22759b8d05b8SZbigniew Bodek 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
22769b8d05b8SZbigniew Bodek 		    ena_timer_service, (void *)adapter, 0);
22779b8d05b8SZbigniew Bodek 
22789b8d05b8SZbigniew Bodek 		adapter->up = true;
227993471047SZbigniew Bodek 
228093471047SZbigniew Bodek 		ena_unmask_all_io_irqs(adapter);
22819b8d05b8SZbigniew Bodek 	}
22829b8d05b8SZbigniew Bodek 
22839b8d05b8SZbigniew Bodek 	return (0);
22849b8d05b8SZbigniew Bodek 
22859b8d05b8SZbigniew Bodek err_up_complete:
22869b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
22879b8d05b8SZbigniew Bodek err_io_que:
22889b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
22899b8d05b8SZbigniew Bodek err_setup_rx:
22909b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
22919b8d05b8SZbigniew Bodek err_setup_tx:
22929b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
22939b8d05b8SZbigniew Bodek err_req_irq:
22949b8d05b8SZbigniew Bodek 	return (rc);
22959b8d05b8SZbigniew Bodek }
22969b8d05b8SZbigniew Bodek 
22979b8d05b8SZbigniew Bodek static uint64_t
22989b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
22999b8d05b8SZbigniew Bodek {
23009b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
23019b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
23029b8d05b8SZbigniew Bodek 
23039b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
23049b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
23059b8d05b8SZbigniew Bodek 
23069b8d05b8SZbigniew Bodek 	switch (cnt) {
23079b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
230830217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
23099b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
231030217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
23119b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
231230217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
23139b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
231430217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
23159b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
231630217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
23179b8d05b8SZbigniew Bodek 	default:
23189b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
23199b8d05b8SZbigniew Bodek 	}
23209b8d05b8SZbigniew Bodek }
23219b8d05b8SZbigniew Bodek 
23229b8d05b8SZbigniew Bodek static int
23239b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
23249b8d05b8SZbigniew Bodek {
23259b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
23269b8d05b8SZbigniew Bodek 	return (0);
23279b8d05b8SZbigniew Bodek }
23289b8d05b8SZbigniew Bodek 
23299b8d05b8SZbigniew Bodek static void
23309b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
23319b8d05b8SZbigniew Bodek {
23329b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
23339b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "enter");
23349b8d05b8SZbigniew Bodek 
23355a990212SMarcin Wojtas 	mtx_lock(&adapter->global_mtx);
23369b8d05b8SZbigniew Bodek 
23379b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
23389b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
23399b8d05b8SZbigniew Bodek 
23409b8d05b8SZbigniew Bodek 	if (!adapter->link_status) {
23415a990212SMarcin Wojtas 		mtx_unlock(&adapter->global_mtx);
23424e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "link_status = false");
23439b8d05b8SZbigniew Bodek 		return;
23449b8d05b8SZbigniew Bodek 	}
23459b8d05b8SZbigniew Bodek 
23469b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
2347b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
23489b8d05b8SZbigniew Bodek 
23495a990212SMarcin Wojtas 	mtx_unlock(&adapter->global_mtx);
23509b8d05b8SZbigniew Bodek }
23519b8d05b8SZbigniew Bodek 
23529b8d05b8SZbigniew Bodek static void
23539b8d05b8SZbigniew Bodek ena_init(void *arg)
23549b8d05b8SZbigniew Bodek {
23559b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
23569b8d05b8SZbigniew Bodek 
23570bdffe59SMarcin Wojtas 	if (!adapter->up) {
23583d3a90f9SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
23599b8d05b8SZbigniew Bodek 		ena_up(adapter);
23603d3a90f9SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
23613d3a90f9SZbigniew Bodek 	}
23629b8d05b8SZbigniew Bodek }
23639b8d05b8SZbigniew Bodek 
23649b8d05b8SZbigniew Bodek static int
23659b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
23669b8d05b8SZbigniew Bodek {
23679b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
23689b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
23699b8d05b8SZbigniew Bodek 	int rc;
23709b8d05b8SZbigniew Bodek 
23719b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
23729b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
23739b8d05b8SZbigniew Bodek 
23749b8d05b8SZbigniew Bodek 	/*
23759b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
23769b8d05b8SZbigniew Bodek 	 */
23779b8d05b8SZbigniew Bodek 	rc = 0;
23789b8d05b8SZbigniew Bodek 	switch (command) {
23799b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2380dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
2381dbf2eb54SMarcin Wojtas 			break;
2382e67c6554SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
23839b8d05b8SZbigniew Bodek 		ena_down(adapter);
23849b8d05b8SZbigniew Bodek 
23859b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
23869b8d05b8SZbigniew Bodek 
23879b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
2388e67c6554SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
23899b8d05b8SZbigniew Bodek 		break;
23909b8d05b8SZbigniew Bodek 
23919b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
23920bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
23930bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
23940bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
23950bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
23969b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
23979b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
23989b8d05b8SZbigniew Bodek 				}
23999b8d05b8SZbigniew Bodek 			} else {
2400e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24019b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2402e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
24039b8d05b8SZbigniew Bodek 			}
24049b8d05b8SZbigniew Bodek 		} else {
24050bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2406e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24079b8d05b8SZbigniew Bodek 				ena_down(adapter);
2408e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
2409e67c6554SZbigniew Bodek 			}
24109b8d05b8SZbigniew Bodek 		}
24119b8d05b8SZbigniew Bodek 		break;
24129b8d05b8SZbigniew Bodek 
24139b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
24149b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
24159b8d05b8SZbigniew Bodek 		break;
24169b8d05b8SZbigniew Bodek 
24179b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
24189b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
24199b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
24209b8d05b8SZbigniew Bodek 		break;
24219b8d05b8SZbigniew Bodek 
24229b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
24239b8d05b8SZbigniew Bodek 		{
24249b8d05b8SZbigniew Bodek 			int reinit = 0;
24259b8d05b8SZbigniew Bodek 
24269b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
24279b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
24289b8d05b8SZbigniew Bodek 				reinit = 1;
24299b8d05b8SZbigniew Bodek 			}
24309b8d05b8SZbigniew Bodek 
24310bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
24320bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
2433e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24349b8d05b8SZbigniew Bodek 				ena_down(adapter);
24359b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2436e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
24379b8d05b8SZbigniew Bodek 			}
24389b8d05b8SZbigniew Bodek 		}
24399b8d05b8SZbigniew Bodek 
24409b8d05b8SZbigniew Bodek 		break;
24419b8d05b8SZbigniew Bodek 	default:
24429b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
24439b8d05b8SZbigniew Bodek 		break;
24449b8d05b8SZbigniew Bodek 	}
24459b8d05b8SZbigniew Bodek 
24469b8d05b8SZbigniew Bodek 	return (rc);
24479b8d05b8SZbigniew Bodek }
24489b8d05b8SZbigniew Bodek 
24499b8d05b8SZbigniew Bodek static int
24509b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
24519b8d05b8SZbigniew Bodek {
24529b8d05b8SZbigniew Bodek 	int caps = 0;
24539b8d05b8SZbigniew Bodek 
24540bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24559b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
24569b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
24570bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
24589b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
24599b8d05b8SZbigniew Bodek 
24600bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24619b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
24620bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
24639b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
24649b8d05b8SZbigniew Bodek 
24650bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24660bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
24679b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
24689b8d05b8SZbigniew Bodek 
24690bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24700bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
24719b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
24729b8d05b8SZbigniew Bodek 
24730bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
24749b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
24750bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
24769b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
24779b8d05b8SZbigniew Bodek 
24780bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
24790bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
24809b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
24819b8d05b8SZbigniew Bodek 
24829b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
24839b8d05b8SZbigniew Bodek 
24849b8d05b8SZbigniew Bodek 	return (caps);
24859b8d05b8SZbigniew Bodek }
24869b8d05b8SZbigniew Bodek 
24879b8d05b8SZbigniew Bodek static void
24889b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
24899b8d05b8SZbigniew Bodek {
24909b8d05b8SZbigniew Bodek 
24919b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
24929b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
24939b8d05b8SZbigniew Bodek }
24949b8d05b8SZbigniew Bodek 
24959b8d05b8SZbigniew Bodek static void
24969b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
24979b8d05b8SZbigniew Bodek {
24989b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
24999b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
25009b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
25019b8d05b8SZbigniew Bodek 	int flags = 0;
25029b8d05b8SZbigniew Bodek 
25039b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
25049b8d05b8SZbigniew Bodek 
25050bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
25060bdffe59SMarcin Wojtas 		if ((feat &
25070bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
25089b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
25090bdffe59SMarcin Wojtas 		if ((feat &
25109b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
25110bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
25129b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
25139b8d05b8SZbigniew Bodek 	}
25149b8d05b8SZbigniew Bodek 
25150bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
25169b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
25179b8d05b8SZbigniew Bodek 
25180bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
25199b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
25209b8d05b8SZbigniew Bodek 
25210bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
25229b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
25239b8d05b8SZbigniew Bodek 
25249b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
25259b8d05b8SZbigniew Bodek }
25269b8d05b8SZbigniew Bodek 
25279b8d05b8SZbigniew Bodek static int
25289b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
25299b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
25309b8d05b8SZbigniew Bodek {
25319b8d05b8SZbigniew Bodek 	if_t ifp;
25329b8d05b8SZbigniew Bodek 	int caps = 0;
25339b8d05b8SZbigniew Bodek 
25349b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
25353f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
25364e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
25379b8d05b8SZbigniew Bodek 		return (ENXIO);
25389b8d05b8SZbigniew Bodek 	}
25399b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
25409b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
25419b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
25429b8d05b8SZbigniew Bodek 
25439b8d05b8SZbigniew Bodek 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
25449b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
25459b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
25469b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
25479b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
25489b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
25499b8d05b8SZbigniew Bodek 
25509b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
25519b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
25529b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
25539b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
25549b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
25559b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
25569b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
25579b8d05b8SZbigniew Bodek 	/* check hardware support */
25589b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
25599b8d05b8SZbigniew Bodek 	/* ... and set them */
25609b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
25619b8d05b8SZbigniew Bodek 
25629b8d05b8SZbigniew Bodek 	/* TSO parameters */
25638a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
25648a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
25658a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
25668a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
25679b8d05b8SZbigniew Bodek 
25689b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
25699b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
25709b8d05b8SZbigniew Bodek 
25719b8d05b8SZbigniew Bodek 	/*
25729b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
25739b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
25749b8d05b8SZbigniew Bodek 	 */
25759b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
25769b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
25779b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
25789b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
25799b8d05b8SZbigniew Bodek 
25809b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
25819b8d05b8SZbigniew Bodek 
25829b8d05b8SZbigniew Bodek 	return (0);
25839b8d05b8SZbigniew Bodek }
25849b8d05b8SZbigniew Bodek 
25859b8d05b8SZbigniew Bodek static void
25869b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
25879b8d05b8SZbigniew Bodek {
2588a195fab0SMarcin Wojtas 	int rc;
25899b8d05b8SZbigniew Bodek 
25909b8d05b8SZbigniew Bodek 	if (adapter->up) {
25919b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going DOWN\n");
25929b8d05b8SZbigniew Bodek 
25939b8d05b8SZbigniew Bodek 		callout_drain(&adapter->timer_service);
25949b8d05b8SZbigniew Bodek 
25959b8d05b8SZbigniew Bodek 		adapter->up = false;
25969b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
25979b8d05b8SZbigniew Bodek 		    IFF_DRV_RUNNING);
25989b8d05b8SZbigniew Bodek 
25999b8d05b8SZbigniew Bodek 		ena_free_io_irq(adapter);
26009b8d05b8SZbigniew Bodek 
2601a195fab0SMarcin Wojtas 		if (adapter->trigger_reset) {
2602a195fab0SMarcin Wojtas 			rc = ena_com_dev_reset(adapter->ena_dev,
2603a195fab0SMarcin Wojtas 			    adapter->reset_reason);
26043f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0))
2605a195fab0SMarcin Wojtas 				device_printf(adapter->pdev,
2606a195fab0SMarcin Wojtas 				    "Device reset failed\n");
2607a195fab0SMarcin Wojtas 		}
2608a195fab0SMarcin Wojtas 
26099b8d05b8SZbigniew Bodek 		ena_destroy_all_io_queues(adapter);
26109b8d05b8SZbigniew Bodek 
26119b8d05b8SZbigniew Bodek 		ena_free_all_tx_bufs(adapter);
26129b8d05b8SZbigniew Bodek 		ena_free_all_rx_bufs(adapter);
26139b8d05b8SZbigniew Bodek 		ena_free_all_tx_resources(adapter);
26149b8d05b8SZbigniew Bodek 		ena_free_all_rx_resources(adapter);
26159b8d05b8SZbigniew Bodek 
26169b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_down, 1);
26179b8d05b8SZbigniew Bodek 	}
26189b8d05b8SZbigniew Bodek }
26199b8d05b8SZbigniew Bodek 
26209b8d05b8SZbigniew Bodek static void
26219b8d05b8SZbigniew Bodek ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
26229b8d05b8SZbigniew Bodek {
26239b8d05b8SZbigniew Bodek 	struct ena_com_tx_meta *ena_meta;
26249b8d05b8SZbigniew Bodek 	struct ether_vlan_header *eh;
26259b8d05b8SZbigniew Bodek 	u32 mss;
26269b8d05b8SZbigniew Bodek 	bool offload;
26279b8d05b8SZbigniew Bodek 	uint16_t etype;
26289b8d05b8SZbigniew Bodek 	int ehdrlen;
26299b8d05b8SZbigniew Bodek 	struct ip *ip;
26309b8d05b8SZbigniew Bodek 	int iphlen;
26319b8d05b8SZbigniew Bodek 	struct tcphdr *th;
26329b8d05b8SZbigniew Bodek 
26339b8d05b8SZbigniew Bodek 	offload = false;
26349b8d05b8SZbigniew Bodek 	ena_meta = &ena_tx_ctx->ena_meta;
26359b8d05b8SZbigniew Bodek 	mss = mbuf->m_pkthdr.tso_segsz;
26369b8d05b8SZbigniew Bodek 
26379b8d05b8SZbigniew Bodek 	if (mss != 0)
26389b8d05b8SZbigniew Bodek 		offload = true;
26399b8d05b8SZbigniew Bodek 
26409b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0)
26419b8d05b8SZbigniew Bodek 		offload = true;
26429b8d05b8SZbigniew Bodek 
26439b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
26449b8d05b8SZbigniew Bodek 		offload = true;
26459b8d05b8SZbigniew Bodek 
26460bdffe59SMarcin Wojtas 	if (!offload) {
26479b8d05b8SZbigniew Bodek 		ena_tx_ctx->meta_valid = 0;
26489b8d05b8SZbigniew Bodek 		return;
26499b8d05b8SZbigniew Bodek 	}
26509b8d05b8SZbigniew Bodek 
26519b8d05b8SZbigniew Bodek 	/* Determine where frame payload starts. */
26529b8d05b8SZbigniew Bodek 	eh = mtod(mbuf, struct ether_vlan_header *);
26539b8d05b8SZbigniew Bodek 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
26549b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_proto);
26559b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
26569b8d05b8SZbigniew Bodek 	} else {
26579b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_encap_proto);
26589b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN;
26599b8d05b8SZbigniew Bodek 	}
26609b8d05b8SZbigniew Bodek 
26619b8d05b8SZbigniew Bodek 	ip = (struct ip *)(mbuf->m_data + ehdrlen);
26629b8d05b8SZbigniew Bodek 	iphlen = ip->ip_hl << 2;
26639b8d05b8SZbigniew Bodek 	th = (struct tcphdr *)((caddr_t)ip + iphlen);
26649b8d05b8SZbigniew Bodek 
26659b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
26669b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_csum_enable = 1;
26679b8d05b8SZbigniew Bodek 	}
26689b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
26699b8d05b8SZbigniew Bodek 		ena_tx_ctx->tso_enable = 1;
26709b8d05b8SZbigniew Bodek 		ena_meta->l4_hdr_len = (th->th_off);
26719b8d05b8SZbigniew Bodek 	}
26729b8d05b8SZbigniew Bodek 
26739b8d05b8SZbigniew Bodek 	switch (etype) {
26749b8d05b8SZbigniew Bodek 	case ETHERTYPE_IP:
26759b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2676cd433385SMarcin Wojtas 		if ((ip->ip_off & htons(IP_DF)) != 0)
26779b8d05b8SZbigniew Bodek 			ena_tx_ctx->df = 1;
26789b8d05b8SZbigniew Bodek 		break;
26799b8d05b8SZbigniew Bodek 	case ETHERTYPE_IPV6:
26809b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
26819b8d05b8SZbigniew Bodek 
26829b8d05b8SZbigniew Bodek 	default:
26839b8d05b8SZbigniew Bodek 		break;
26849b8d05b8SZbigniew Bodek 	}
26859b8d05b8SZbigniew Bodek 
26869b8d05b8SZbigniew Bodek 	if (ip->ip_p == IPPROTO_TCP) {
26879b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
26880bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
26890bdffe59SMarcin Wojtas 		    (CSUM_IP_TCP | CSUM_IP6_TCP)) != 0)
26909b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
26919b8d05b8SZbigniew Bodek 		else
26929b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
26939b8d05b8SZbigniew Bodek 	} else if (ip->ip_p == IPPROTO_UDP) {
26949b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
26950bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
26960bdffe59SMarcin Wojtas 		    (CSUM_IP_UDP | CSUM_IP6_UDP)) != 0)
26979b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
26989b8d05b8SZbigniew Bodek 		else
26999b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
27009b8d05b8SZbigniew Bodek 	} else {
27019b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
27029b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_csum_enable = 0;
27039b8d05b8SZbigniew Bodek 	}
27049b8d05b8SZbigniew Bodek 
27059b8d05b8SZbigniew Bodek 	ena_meta->mss = mss;
27069b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_len = iphlen;
27079b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_offset = ehdrlen;
27089b8d05b8SZbigniew Bodek 	ena_tx_ctx->meta_valid = 1;
27099b8d05b8SZbigniew Bodek }
27109b8d05b8SZbigniew Bodek 
27119b8d05b8SZbigniew Bodek static int
27121b069f1cSZbigniew Bodek ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
27131e9fb899SZbigniew Bodek {
27141e9fb899SZbigniew Bodek 	struct ena_adapter *adapter;
27151b069f1cSZbigniew Bodek 	struct mbuf *collapsed_mbuf;
27161e9fb899SZbigniew Bodek 	int num_frags;
27171e9fb899SZbigniew Bodek 
27181e9fb899SZbigniew Bodek 	adapter = tx_ring->adapter;
27191e9fb899SZbigniew Bodek 	num_frags = ena_mbuf_count(*mbuf);
27201e9fb899SZbigniew Bodek 
27211e9fb899SZbigniew Bodek 	/* One segment must be reserved for configuration descriptor. */
27221e9fb899SZbigniew Bodek 	if (num_frags < adapter->max_tx_sgl_size)
27231e9fb899SZbigniew Bodek 		return (0);
27241b069f1cSZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
27251e9fb899SZbigniew Bodek 
27261b069f1cSZbigniew Bodek 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
27271b069f1cSZbigniew Bodek 	    adapter->max_tx_sgl_size - 1);
27283f9ed7abSMarcin Wojtas 	if (unlikely(collapsed_mbuf == NULL)) {
27291b069f1cSZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
27301e9fb899SZbigniew Bodek 		return (ENOMEM);
27311e9fb899SZbigniew Bodek 	}
27321e9fb899SZbigniew Bodek 
27331b069f1cSZbigniew Bodek 	/* If mbuf was collapsed succesfully, original mbuf is released. */
27341b069f1cSZbigniew Bodek 	*mbuf = collapsed_mbuf;
27351e9fb899SZbigniew Bodek 
27361e9fb899SZbigniew Bodek 	return (0);
27371e9fb899SZbigniew Bodek }
27381e9fb899SZbigniew Bodek 
27391e9fb899SZbigniew Bodek static int
27401e9fb899SZbigniew Bodek ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
27419b8d05b8SZbigniew Bodek {
27429b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
27439b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info;
27449b8d05b8SZbigniew Bodek 	struct ena_com_tx_ctx ena_tx_ctx;
27459b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
27469b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
27479b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
27489b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[ENA_BUS_DMA_SEGS];
27499b8d05b8SZbigniew Bodek 	void *push_hdr;
27509b8d05b8SZbigniew Bodek 	uint16_t next_to_use;
27519b8d05b8SZbigniew Bodek 	uint16_t req_id;
27529b8d05b8SZbigniew Bodek 	uint16_t push_len;
27539b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
27542339f28cSMarcin Wojtas 	uint32_t nsegs, header_len;
27559b8d05b8SZbigniew Bodek 	int i, rc;
27561e9fb899SZbigniew Bodek 	int nb_hw_desc;
27579b8d05b8SZbigniew Bodek 
27589b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
27599b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
27609b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
276190f4da8bSMarcin Wojtas 	io_sq = &ena_dev->io_sq_queues[ena_qid];
27629b8d05b8SZbigniew Bodek 
27631b069f1cSZbigniew Bodek 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
27643f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27651e9fb899SZbigniew Bodek 		ena_trace(ENA_WARNING,
27661b069f1cSZbigniew Bodek 		    "Failed to collapse mbuf! err: %d", rc);
27671e9fb899SZbigniew Bodek 		return (rc);
27689b8d05b8SZbigniew Bodek 	}
27699b8d05b8SZbigniew Bodek 
27709b8d05b8SZbigniew Bodek 	next_to_use = tx_ring->next_to_use;
27719b8d05b8SZbigniew Bodek 	req_id = tx_ring->free_tx_ids[next_to_use];
27729b8d05b8SZbigniew Bodek 	tx_info = &tx_ring->tx_buffer_info[req_id];
27739b8d05b8SZbigniew Bodek 
27741e9fb899SZbigniew Bodek 	tx_info->mbuf = *mbuf;
27759b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = 0;
27769b8d05b8SZbigniew Bodek 
27779b8d05b8SZbigniew Bodek 	ena_buf = tx_info->bufs;
27789b8d05b8SZbigniew Bodek 
27791e9fb899SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
27809b8d05b8SZbigniew Bodek 
27819b8d05b8SZbigniew Bodek 	push_len = 0;
27822339f28cSMarcin Wojtas 	/*
27832339f28cSMarcin Wojtas 	 * header_len is just a hint for the device. Because FreeBSD is not
27842339f28cSMarcin Wojtas 	 * giving us information about packet header length and it is not
27852339f28cSMarcin Wojtas 	 * guaranteed that all packet headers will be in the 1st mbuf, setting
27862339f28cSMarcin Wojtas 	 * header_len to 0 is making the device ignore this value and resolve
27872339f28cSMarcin Wojtas 	 * header on it's own.
27882339f28cSMarcin Wojtas 	 */
27892339f28cSMarcin Wojtas 	header_len = 0;
27909b8d05b8SZbigniew Bodek 	push_hdr = NULL;
27919b8d05b8SZbigniew Bodek 
27929b8d05b8SZbigniew Bodek 	rc = bus_dmamap_load_mbuf_sg(adapter->tx_buf_tag, tx_info->map,
27931e9fb899SZbigniew Bodek 	    *mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
27949b8d05b8SZbigniew Bodek 
27953f9ed7abSMarcin Wojtas 	if (unlikely((rc != 0) || (nsegs == 0))) {
27969b8d05b8SZbigniew Bodek 		ena_trace(ENA_WARNING,
27979b8d05b8SZbigniew Bodek 		    "dmamap load failed! err: %d nsegs: %d", rc, nsegs);
27989b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
27999b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
28009b8d05b8SZbigniew Bodek 		if (rc == ENOMEM)
28019b8d05b8SZbigniew Bodek 			return (ENA_COM_NO_MEM);
28029b8d05b8SZbigniew Bodek 		else
28039b8d05b8SZbigniew Bodek 			return (ENA_COM_INVAL);
28049b8d05b8SZbigniew Bodek 	}
28059b8d05b8SZbigniew Bodek 
28069b8d05b8SZbigniew Bodek 	for (i = 0; i < nsegs; i++) {
28079b8d05b8SZbigniew Bodek 		ena_buf->len = segs[i].ds_len;
28089b8d05b8SZbigniew Bodek 		ena_buf->paddr = segs[i].ds_addr;
28099b8d05b8SZbigniew Bodek 		ena_buf++;
28109b8d05b8SZbigniew Bodek 	}
28119b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = nsegs;
28129b8d05b8SZbigniew Bodek 
28139b8d05b8SZbigniew Bodek 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
28149b8d05b8SZbigniew Bodek 	ena_tx_ctx.ena_bufs = tx_info->bufs;
28159b8d05b8SZbigniew Bodek 	ena_tx_ctx.push_header = push_hdr;
28169b8d05b8SZbigniew Bodek 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
28179b8d05b8SZbigniew Bodek 	ena_tx_ctx.req_id = req_id;
28189b8d05b8SZbigniew Bodek 	ena_tx_ctx.header_len = header_len;
28199b8d05b8SZbigniew Bodek 
28209b8d05b8SZbigniew Bodek 	/* Set flags and meta data */
28211e9fb899SZbigniew Bodek 	ena_tx_csum(&ena_tx_ctx, *mbuf);
28229b8d05b8SZbigniew Bodek 	/* Prepare the packet's descriptors and send them to device */
28239b8d05b8SZbigniew Bodek 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
28243f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2825e3cecf70SMarcin Wojtas 		if (likely(rc == ENA_COM_NO_MEM)) {
2826e3cecf70SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_TXPTH,
2827e3cecf70SMarcin Wojtas 			    "tx ring[%d] if out of space\n", tx_ring->que->id);
2828e3cecf70SMarcin Wojtas 		} else {
2829e3cecf70SMarcin Wojtas 			device_printf(adapter->pdev,
2830e3cecf70SMarcin Wojtas 			    "failed to prepare tx bufs\n");
2831e3cecf70SMarcin Wojtas 		}
28320052f3b5SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
28339b8d05b8SZbigniew Bodek 		goto dma_error;
28349b8d05b8SZbigniew Bodek 	}
28359b8d05b8SZbigniew Bodek 
28369b8d05b8SZbigniew Bodek 	counter_enter();
28379b8d05b8SZbigniew Bodek 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
28380bdffe59SMarcin Wojtas 	counter_u64_add_protected(tx_ring->tx_stats.bytes,
28390bdffe59SMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
284030217e2dSMarcin Wojtas 
284130217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
284230217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
284330217e2dSMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
28449b8d05b8SZbigniew Bodek 	counter_exit();
28459b8d05b8SZbigniew Bodek 
28469b8d05b8SZbigniew Bodek 	tx_info->tx_descs = nb_hw_desc;
28479b8d05b8SZbigniew Bodek 	getbinuptime(&tx_info->timestamp);
28489b8d05b8SZbigniew Bodek 	tx_info->print_once = true;
28499b8d05b8SZbigniew Bodek 
28509b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
28519b8d05b8SZbigniew Bodek 	    tx_ring->ring_size);
28529b8d05b8SZbigniew Bodek 
2853*5cb9db07SMarcin Wojtas 	/* stop the queue when no more space available, the packet can have up
2854*5cb9db07SMarcin Wojtas 	 * to sgl_size + 2. one for the meta descriptor and one for header
2855*5cb9db07SMarcin Wojtas 	 * (if the header is larger than tx_max_header_size).
2856*5cb9db07SMarcin Wojtas 	 */
2857*5cb9db07SMarcin Wojtas 	if (unlikely(!ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
2858*5cb9db07SMarcin Wojtas 	    adapter->max_tx_sgl_size + 2))) {
2859*5cb9db07SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_TXPTH, "Stop queue %d\n",
2860*5cb9db07SMarcin Wojtas 		    tx_ring->que->id);
2861*5cb9db07SMarcin Wojtas 
2862*5cb9db07SMarcin Wojtas 		tx_ring->running = false;
2863*5cb9db07SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.queue_stop, 1);
2864*5cb9db07SMarcin Wojtas 
2865*5cb9db07SMarcin Wojtas 		/* There is a rare condition where this function decides to
2866*5cb9db07SMarcin Wojtas 		 * stop the queue but meanwhile tx_cleanup() updates
2867*5cb9db07SMarcin Wojtas 		 * next_to_completion and terminates.
2868*5cb9db07SMarcin Wojtas 		 * The queue will remain stopped forever.
2869*5cb9db07SMarcin Wojtas 		 * To solve this issue this function performs mb(), checks
2870*5cb9db07SMarcin Wojtas 		 * the wakeup condition and wakes up the queue if needed.
2871*5cb9db07SMarcin Wojtas 		 */
2872*5cb9db07SMarcin Wojtas 		mb();
2873*5cb9db07SMarcin Wojtas 
2874*5cb9db07SMarcin Wojtas 		if (ena_com_sq_have_enough_space(tx_ring->ena_com_io_sq,
2875*5cb9db07SMarcin Wojtas 		    ENA_TX_RESUME_THRESH)) {
2876*5cb9db07SMarcin Wojtas 			tx_ring->running = true;
2877*5cb9db07SMarcin Wojtas 			counter_u64_add(tx_ring->tx_stats.queue_wakeup, 1);
2878*5cb9db07SMarcin Wojtas 		}
2879*5cb9db07SMarcin Wojtas 	}
2880*5cb9db07SMarcin Wojtas 
28810bdffe59SMarcin Wojtas 	bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map,
28820bdffe59SMarcin Wojtas 	    BUS_DMASYNC_PREWRITE);
28839b8d05b8SZbigniew Bodek 
28849b8d05b8SZbigniew Bodek 	return (0);
28859b8d05b8SZbigniew Bodek 
28869b8d05b8SZbigniew Bodek dma_error:
28879b8d05b8SZbigniew Bodek 	tx_info->mbuf = NULL;
28889b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
28899b8d05b8SZbigniew Bodek 
28909b8d05b8SZbigniew Bodek 	return (rc);
28919b8d05b8SZbigniew Bodek }
28929b8d05b8SZbigniew Bodek 
28939b8d05b8SZbigniew Bodek static void
28949b8d05b8SZbigniew Bodek ena_start_xmit(struct ena_ring *tx_ring)
28959b8d05b8SZbigniew Bodek {
28969b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
28979b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = tx_ring->adapter;
28989b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
28999b8d05b8SZbigniew Bodek 	int ena_qid;
29009b8d05b8SZbigniew Bodek 	int acum_pkts = 0;
29019b8d05b8SZbigniew Bodek 	int ret = 0;
29029b8d05b8SZbigniew Bodek 
29033f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
29049b8d05b8SZbigniew Bodek 		return;
29059b8d05b8SZbigniew Bodek 
29063f9ed7abSMarcin Wojtas 	if (unlikely(!adapter->link_status))
29079b8d05b8SZbigniew Bodek 		return;
29089b8d05b8SZbigniew Bodek 
29099b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
29109b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
29119b8d05b8SZbigniew Bodek 
29129b8d05b8SZbigniew Bodek 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
29139b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
29149b8d05b8SZbigniew Bodek 		    " header csum flags %#jx",
29154e8acd84SMarcin Wojtas 		    mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
29169b8d05b8SZbigniew Bodek 
2917*5cb9db07SMarcin Wojtas 		if (unlikely(!tx_ring->running)) {
2918*5cb9db07SMarcin Wojtas 			drbr_putback(adapter->ifp, tx_ring->br, mbuf);
2919*5cb9db07SMarcin Wojtas 			break;
2920*5cb9db07SMarcin Wojtas 		}
29219b8d05b8SZbigniew Bodek 
29223f9ed7abSMarcin Wojtas 		if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
29239b8d05b8SZbigniew Bodek 			if (ret == ENA_COM_NO_MEM) {
29249b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
29259b8d05b8SZbigniew Bodek 			} else if (ret == ENA_COM_NO_SPACE) {
29269b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
29279b8d05b8SZbigniew Bodek 			} else {
29289b8d05b8SZbigniew Bodek 				m_freem(mbuf);
29299b8d05b8SZbigniew Bodek 				drbr_advance(adapter->ifp, tx_ring->br);
29309b8d05b8SZbigniew Bodek 			}
29319b8d05b8SZbigniew Bodek 
29329b8d05b8SZbigniew Bodek 			break;
29339b8d05b8SZbigniew Bodek 		}
29349b8d05b8SZbigniew Bodek 
2935b4b29032SZbigniew Bodek 		drbr_advance(adapter->ifp, tx_ring->br);
2936b4b29032SZbigniew Bodek 
29373f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(adapter->ifp) &
29383f9ed7abSMarcin Wojtas 		    IFF_DRV_RUNNING) == 0))
29399b8d05b8SZbigniew Bodek 			return;
29409b8d05b8SZbigniew Bodek 
29419b8d05b8SZbigniew Bodek 		acum_pkts++;
29429b8d05b8SZbigniew Bodek 
29439b8d05b8SZbigniew Bodek 		BPF_MTAP(adapter->ifp, mbuf);
29449b8d05b8SZbigniew Bodek 
29453f9ed7abSMarcin Wojtas 		if (unlikely(acum_pkts == DB_THRESHOLD)) {
29469b8d05b8SZbigniew Bodek 			acum_pkts = 0;
29479b8d05b8SZbigniew Bodek 			wmb();
29489b8d05b8SZbigniew Bodek 			/* Trigger the dma engine */
29499b8d05b8SZbigniew Bodek 			ena_com_write_sq_doorbell(io_sq);
29509b8d05b8SZbigniew Bodek 			counter_u64_add(tx_ring->tx_stats.doorbells, 1);
29519b8d05b8SZbigniew Bodek 		}
29529b8d05b8SZbigniew Bodek 
29539b8d05b8SZbigniew Bodek 	}
29549b8d05b8SZbigniew Bodek 
29553f9ed7abSMarcin Wojtas 	if (likely(acum_pkts != 0)) {
29569b8d05b8SZbigniew Bodek 		wmb();
29579b8d05b8SZbigniew Bodek 		/* Trigger the dma engine */
29589b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(io_sq);
29599b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
29609b8d05b8SZbigniew Bodek 	}
29619b8d05b8SZbigniew Bodek 
2962*5cb9db07SMarcin Wojtas 	if (unlikely(!tx_ring->running))
2963*5cb9db07SMarcin Wojtas 		taskqueue_enqueue(tx_ring->que->cleanup_tq,
2964*5cb9db07SMarcin Wojtas 		    &tx_ring->que->cleanup_task);
29659b8d05b8SZbigniew Bodek }
29669b8d05b8SZbigniew Bodek 
29679b8d05b8SZbigniew Bodek static void
29689b8d05b8SZbigniew Bodek ena_deferred_mq_start(void *arg, int pending)
29699b8d05b8SZbigniew Bodek {
29709b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
29719b8d05b8SZbigniew Bodek 	struct ifnet *ifp = tx_ring->adapter->ifp;
29729b8d05b8SZbigniew Bodek 
29730bdffe59SMarcin Wojtas 	while (!drbr_empty(ifp, tx_ring->br) &&
2974*5cb9db07SMarcin Wojtas 	    tx_ring->running &&
29750bdffe59SMarcin Wojtas 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
29769b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
29779b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
29789b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
29799b8d05b8SZbigniew Bodek 	}
29809b8d05b8SZbigniew Bodek }
29819b8d05b8SZbigniew Bodek 
29829b8d05b8SZbigniew Bodek static int
29839b8d05b8SZbigniew Bodek ena_mq_start(if_t ifp, struct mbuf *m)
29849b8d05b8SZbigniew Bodek {
29859b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
29869b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
29879b8d05b8SZbigniew Bodek 	int ret, is_drbr_empty;
29889b8d05b8SZbigniew Bodek 	uint32_t i;
29899b8d05b8SZbigniew Bodek 
29903f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
29919b8d05b8SZbigniew Bodek 		return (ENODEV);
29929b8d05b8SZbigniew Bodek 
29939b8d05b8SZbigniew Bodek 	/* Which queue to use */
29949b8d05b8SZbigniew Bodek 	/*
29959b8d05b8SZbigniew Bodek 	 * If everything is setup correctly, it should be the
29969b8d05b8SZbigniew Bodek 	 * same bucket that the current CPU we're on is.
29979b8d05b8SZbigniew Bodek 	 * It should improve performance.
29989b8d05b8SZbigniew Bodek 	 */
29999b8d05b8SZbigniew Bodek 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
30009b8d05b8SZbigniew Bodek 		i = m->m_pkthdr.flowid % adapter->num_queues;
30019b8d05b8SZbigniew Bodek 	} else {
30029b8d05b8SZbigniew Bodek 		i = curcpu % adapter->num_queues;
30039b8d05b8SZbigniew Bodek 	}
30049b8d05b8SZbigniew Bodek 	tx_ring = &adapter->tx_ring[i];
30059b8d05b8SZbigniew Bodek 
30069b8d05b8SZbigniew Bodek 	/* Check if drbr is empty before putting packet */
30079b8d05b8SZbigniew Bodek 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
30089b8d05b8SZbigniew Bodek 	ret = drbr_enqueue(ifp, tx_ring->br, m);
30093f9ed7abSMarcin Wojtas 	if (unlikely(ret != 0)) {
30109b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
30119b8d05b8SZbigniew Bodek 		return (ret);
30129b8d05b8SZbigniew Bodek 	}
30139b8d05b8SZbigniew Bodek 
301467ec48bbSMarcin Wojtas 	if (is_drbr_empty && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
30159b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
30169b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
30179b8d05b8SZbigniew Bodek 	} else {
30189b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
30199b8d05b8SZbigniew Bodek 	}
30209b8d05b8SZbigniew Bodek 
30219b8d05b8SZbigniew Bodek 	return (0);
30229b8d05b8SZbigniew Bodek }
30239b8d05b8SZbigniew Bodek 
30249b8d05b8SZbigniew Bodek static void
30259b8d05b8SZbigniew Bodek ena_qflush(if_t ifp)
30269b8d05b8SZbigniew Bodek {
30279b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
30289b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = adapter->tx_ring;
30299b8d05b8SZbigniew Bodek 	int i;
30309b8d05b8SZbigniew Bodek 
30319b8d05b8SZbigniew Bodek 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
30320bdffe59SMarcin Wojtas 		if (!drbr_empty(ifp, tx_ring->br)) {
30339b8d05b8SZbigniew Bodek 			ENA_RING_MTX_LOCK(tx_ring);
30349b8d05b8SZbigniew Bodek 			drbr_flush(ifp, tx_ring->br);
30359b8d05b8SZbigniew Bodek 			ENA_RING_MTX_UNLOCK(tx_ring);
30369b8d05b8SZbigniew Bodek 		}
30379b8d05b8SZbigniew Bodek 
30389b8d05b8SZbigniew Bodek 	if_qflush(ifp);
30399b8d05b8SZbigniew Bodek }
30409b8d05b8SZbigniew Bodek 
30410bdffe59SMarcin Wojtas static int
30420bdffe59SMarcin Wojtas ena_calc_io_queue_num(struct ena_adapter *adapter,
30439b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
30449b8d05b8SZbigniew Bodek {
30456064f289SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
30469b8d05b8SZbigniew Bodek 	int io_sq_num, io_cq_num, io_queue_num;
30479b8d05b8SZbigniew Bodek 
30486064f289SMarcin Wojtas 	/* Regular queues capabilities */
30496064f289SMarcin Wojtas 	if (ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
30506064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
30516064f289SMarcin Wojtas 		    &get_feat_ctx->max_queue_ext.max_queue_ext;
30526064f289SMarcin Wojtas 		io_sq_num = max_queue_ext->max_rx_sq_num;
30536064f289SMarcin Wojtas 		io_sq_num = min_t(int, io_sq_num, max_queue_ext->max_tx_sq_num);
30546064f289SMarcin Wojtas 
30556064f289SMarcin Wojtas 		io_cq_num = max_queue_ext->max_rx_cq_num;
30566064f289SMarcin Wojtas 		io_cq_num = min_t(int, io_cq_num, max_queue_ext->max_tx_cq_num);
30576064f289SMarcin Wojtas 	} else {
30586064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
30596064f289SMarcin Wojtas 		    &get_feat_ctx->max_queues;
30606064f289SMarcin Wojtas 		io_sq_num = max_queues->max_sq_num;
30616064f289SMarcin Wojtas 		io_cq_num = max_queues->max_cq_num;
30626064f289SMarcin Wojtas 	}
30639b8d05b8SZbigniew Bodek 
30649b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
30659b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_sq_num);
30669b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_cq_num);
30679b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
30689b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num,
30699b8d05b8SZbigniew Bodek 	    pci_msix_count(adapter->pdev) - 1);
30709b8d05b8SZbigniew Bodek 
30710bdffe59SMarcin Wojtas 	return (io_queue_num);
30729b8d05b8SZbigniew Bodek }
30739b8d05b8SZbigniew Bodek 
30740bdffe59SMarcin Wojtas static int
30756064f289SMarcin Wojtas ena_calc_queue_size(struct ena_adapter *adapter,
30766064f289SMarcin Wojtas     struct ena_calc_queue_size_ctx *ctx)
30779b8d05b8SZbigniew Bodek {
30786064f289SMarcin Wojtas 	uint32_t tx_queue_size = ENA_DEFAULT_RING_SIZE;
30796064f289SMarcin Wojtas 	uint32_t rx_queue_size = adapter->rx_ring_size;
30809b8d05b8SZbigniew Bodek 
30816064f289SMarcin Wojtas 	if (ctx->ena_dev->supported_features & BIT(ENA_ADMIN_MAX_QUEUES_EXT)) {
30826064f289SMarcin Wojtas 		struct ena_admin_queue_ext_feature_fields *max_queue_ext =
30836064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queue_ext.max_queue_ext;
30846064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
30856064f289SMarcin Wojtas 		    max_queue_ext->max_rx_cq_depth);
30866064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
30876064f289SMarcin Wojtas 		    max_queue_ext->max_rx_sq_depth);
30886064f289SMarcin Wojtas 		tx_queue_size = min_t(uint32_t, tx_queue_size,
30896064f289SMarcin Wojtas 		    max_queue_ext->max_tx_cq_depth);
30906064f289SMarcin Wojtas 		tx_queue_size = min_t(uint32_t, tx_queue_size,
30916064f289SMarcin Wojtas 		    max_queue_ext->max_tx_sq_depth);
30926064f289SMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
30936064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_rx_descs);
30946064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
30956064f289SMarcin Wojtas 		    max_queue_ext->max_per_packet_tx_descs);
30966064f289SMarcin Wojtas 	} else {
30976064f289SMarcin Wojtas 		struct ena_admin_queue_feature_desc *max_queues =
30986064f289SMarcin Wojtas 		    &ctx->get_feat_ctx->max_queues;
30996064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
31006064f289SMarcin Wojtas 		    max_queues->max_cq_depth);
31016064f289SMarcin Wojtas 		rx_queue_size = min_t(uint32_t, rx_queue_size,
31026064f289SMarcin Wojtas 		    max_queues->max_sq_depth);
31036064f289SMarcin Wojtas 		tx_queue_size = rx_queue_size;
31046064f289SMarcin Wojtas 		ctx->max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
31056064f289SMarcin Wojtas 		    max_queues->max_packet_tx_descs);
31066064f289SMarcin Wojtas 		ctx->max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
31076064f289SMarcin Wojtas 		    max_queues->max_packet_rx_descs);
31086064f289SMarcin Wojtas 	}
31099b8d05b8SZbigniew Bodek 
31109b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
31116064f289SMarcin Wojtas 	rx_queue_size = 1 << (fls(rx_queue_size) - 1);
31126064f289SMarcin Wojtas 	tx_queue_size = 1 << (fls(tx_queue_size) - 1);
31136064f289SMarcin Wojtas 
31146064f289SMarcin Wojtas 	if (unlikely(rx_queue_size == 0 || tx_queue_size == 0)) {
31156064f289SMarcin Wojtas 		device_printf(ctx->pdev, "Invalid queue size\n");
31166064f289SMarcin Wojtas 		return (EFAULT);
31179b8d05b8SZbigniew Bodek 	}
31189b8d05b8SZbigniew Bodek 
31196064f289SMarcin Wojtas 	ctx->rx_queue_size = rx_queue_size;
31206064f289SMarcin Wojtas 	ctx->tx_queue_size = tx_queue_size;
31216064f289SMarcin Wojtas 
31226064f289SMarcin Wojtas 	return (0);
31239b8d05b8SZbigniew Bodek }
31249b8d05b8SZbigniew Bodek 
31256064f289SMarcin Wojtas static int
31266064f289SMarcin Wojtas ena_handle_updated_queues(struct ena_adapter *adapter,
31276064f289SMarcin Wojtas     struct ena_com_dev_get_features_ctx *get_feat_ctx)
31286064f289SMarcin Wojtas {
31296064f289SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
31306064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
31316064f289SMarcin Wojtas 	device_t pdev = adapter->pdev;
31326064f289SMarcin Wojtas 	bool are_queues_changed = false;
31336064f289SMarcin Wojtas 	int io_queue_num, rc;
31349b8d05b8SZbigniew Bodek 
31356064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
31366064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = get_feat_ctx;
31376064f289SMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
31386064f289SMarcin Wojtas 
31396064f289SMarcin Wojtas 	io_queue_num = ena_calc_io_queue_num(adapter, get_feat_ctx);
31406064f289SMarcin Wojtas 	rc = ena_calc_queue_size(adapter, &calc_queue_ctx);
31416064f289SMarcin Wojtas 	if (unlikely(rc != 0 || io_queue_num <= 0))
31426064f289SMarcin Wojtas 		return EFAULT;
31436064f289SMarcin Wojtas 
31446064f289SMarcin Wojtas 	if (adapter->tx_ring->buf_ring_size != adapter->buf_ring_size)
31456064f289SMarcin Wojtas 		are_queues_changed = true;
31466064f289SMarcin Wojtas 
31476064f289SMarcin Wojtas 	if (unlikely(adapter->tx_ring_size > calc_queue_ctx.tx_queue_size ||
31486064f289SMarcin Wojtas 	    adapter->rx_ring_size > calc_queue_ctx.rx_queue_size)) {
31496064f289SMarcin Wojtas 		device_printf(pdev,
31506064f289SMarcin Wojtas 		    "Not enough resources to allocate requested queue sizes "
31516064f289SMarcin Wojtas 		    "(TX,RX)=(%d,%d), falling back to queue sizes "
31526064f289SMarcin Wojtas 		    "(TX,RX)=(%d,%d)\n",
31536064f289SMarcin Wojtas 		    adapter->tx_ring_size,
31546064f289SMarcin Wojtas 		    adapter->rx_ring_size,
31556064f289SMarcin Wojtas 		    calc_queue_ctx.tx_queue_size,
31566064f289SMarcin Wojtas 		    calc_queue_ctx.rx_queue_size);
31576064f289SMarcin Wojtas 		adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
31586064f289SMarcin Wojtas 		adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
31596064f289SMarcin Wojtas 		adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
31606064f289SMarcin Wojtas 		adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
31616064f289SMarcin Wojtas 		are_queues_changed = true;
31626064f289SMarcin Wojtas 	}
31636064f289SMarcin Wojtas 
31646064f289SMarcin Wojtas 	if (unlikely(adapter->num_queues > io_queue_num)) {
31656064f289SMarcin Wojtas 		device_printf(pdev,
31666064f289SMarcin Wojtas 		    "Not enough resources to allocate %d queues, "
31676064f289SMarcin Wojtas 		    "falling back to %d queues\n",
31686064f289SMarcin Wojtas 		    adapter->num_queues, io_queue_num);
31696064f289SMarcin Wojtas 		adapter->num_queues = io_queue_num;
31706064f289SMarcin Wojtas 		if (adapter->rss_support) {
31716064f289SMarcin Wojtas 			ena_com_rss_destroy(ena_dev);
31726064f289SMarcin Wojtas 			rc = ena_rss_init_default(adapter);
31736064f289SMarcin Wojtas 			if (unlikely(rc != 0) && (rc != EOPNOTSUPP)) {
31746064f289SMarcin Wojtas 				device_printf(pdev, "Cannot init RSS rc: %d\n",
31756064f289SMarcin Wojtas 				    rc);
31766064f289SMarcin Wojtas 				return (rc);
31776064f289SMarcin Wojtas 			}
31786064f289SMarcin Wojtas 		}
31796064f289SMarcin Wojtas 		are_queues_changed = true;
31806064f289SMarcin Wojtas 	}
31816064f289SMarcin Wojtas 
31826064f289SMarcin Wojtas 	if (unlikely(are_queues_changed)) {
31836064f289SMarcin Wojtas 		ena_free_all_io_rings_resources(adapter);
31846064f289SMarcin Wojtas 		ena_init_io_rings(adapter);
31856064f289SMarcin Wojtas 	}
31866064f289SMarcin Wojtas 
31876064f289SMarcin Wojtas 	return (0);
31889b8d05b8SZbigniew Bodek }
31899b8d05b8SZbigniew Bodek 
31900bdffe59SMarcin Wojtas static int
31910bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
31929b8d05b8SZbigniew Bodek {
31939b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
31949b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
31959b8d05b8SZbigniew Bodek 	int qid, rc, i;
31969b8d05b8SZbigniew Bodek 
31979b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
31980bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
31994e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
32007d2544e6SMarcin Wojtas 		return (rc);
32019b8d05b8SZbigniew Bodek 	}
32029b8d05b8SZbigniew Bodek 
32039b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
32049b8d05b8SZbigniew Bodek 		qid = i % adapter->num_queues;
32059b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
32069b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
32070bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
32089b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
32097d2544e6SMarcin Wojtas 			goto err_rss_destroy;
32109b8d05b8SZbigniew Bodek 		}
32119b8d05b8SZbigniew Bodek 	}
32129b8d05b8SZbigniew Bodek 
32139b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
32149b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
32150bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
32169b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
32177d2544e6SMarcin Wojtas 		goto err_rss_destroy;
32189b8d05b8SZbigniew Bodek 	}
32199b8d05b8SZbigniew Bodek 
32209b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
32210bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
32229b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
32237d2544e6SMarcin Wojtas 		goto err_rss_destroy;
32249b8d05b8SZbigniew Bodek 	}
32259b8d05b8SZbigniew Bodek 
32269b8d05b8SZbigniew Bodek 	return (0);
32279b8d05b8SZbigniew Bodek 
32287d2544e6SMarcin Wojtas err_rss_destroy:
32299b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
32309b8d05b8SZbigniew Bodek 	return (rc);
32319b8d05b8SZbigniew Bodek }
32329b8d05b8SZbigniew Bodek 
32339b8d05b8SZbigniew Bodek static void
32349b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
32359b8d05b8SZbigniew Bodek {
32369b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
32379b8d05b8SZbigniew Bodek 	devclass_t dc;
32389b8d05b8SZbigniew Bodek 	int max;
32399b8d05b8SZbigniew Bodek 	int rc;
32409b8d05b8SZbigniew Bodek 
32419b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
32423f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
32434e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "No devclass ena\n");
32449b8d05b8SZbigniew Bodek 		return;
32459b8d05b8SZbigniew Bodek 	}
32469b8d05b8SZbigniew Bodek 
32479b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
32489b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
32499b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
32509b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
32519b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
32529b8d05b8SZbigniew Bodek 			adapter->rss_support = true;
32533f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
32549b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
32559b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
32560bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
32579b8d05b8SZbigniew Bodek 				adapter->rss_support = false;
32589b8d05b8SZbigniew Bodek 			}
32599b8d05b8SZbigniew Bodek 		}
32609b8d05b8SZbigniew Bodek 	}
32619b8d05b8SZbigniew Bodek }
32629b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
32639b8d05b8SZbigniew Bodek 
32640bdffe59SMarcin Wojtas static void
32650bdffe59SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev)
32669b8d05b8SZbigniew Bodek {
32679b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
32689b8d05b8SZbigniew Bodek 	int rc;
32699b8d05b8SZbigniew Bodek 
32709b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
32719b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
32723f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32739b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
32749b8d05b8SZbigniew Bodek 		return;
32759b8d05b8SZbigniew Bodek 	}
32769b8d05b8SZbigniew Bodek 
32779b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
32789b8d05b8SZbigniew Bodek 
32799b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
32809b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
32819b8d05b8SZbigniew Bodek 
32829b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
32839b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
32849b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
32859b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
32869b8d05b8SZbigniew Bodek 
32879b8d05b8SZbigniew Bodek 	host_info->driver_version =
32889b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
32899b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
32909b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
32918ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
32929b8d05b8SZbigniew Bodek 
32939b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
32943f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
3295a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
32969b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
32979b8d05b8SZbigniew Bodek 		else
32989b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
32999b8d05b8SZbigniew Bodek 
33009b8d05b8SZbigniew Bodek 		goto err;
33019b8d05b8SZbigniew Bodek 	}
33029b8d05b8SZbigniew Bodek 
33039b8d05b8SZbigniew Bodek 	return;
33049b8d05b8SZbigniew Bodek 
33059b8d05b8SZbigniew Bodek err:
33069b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
33079b8d05b8SZbigniew Bodek }
33089b8d05b8SZbigniew Bodek 
33099b8d05b8SZbigniew Bodek static int
33109b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
33119b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
33129b8d05b8SZbigniew Bodek {
33139b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
33149b8d05b8SZbigniew Bodek 	bool readless_supported;
33159b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
33169b8d05b8SZbigniew Bodek 	int dma_width;
33179b8d05b8SZbigniew Bodek 	int rc;
33189b8d05b8SZbigniew Bodek 
33199b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
33203f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33219b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
33220bdffe59SMarcin Wojtas 		return (rc);
33239b8d05b8SZbigniew Bodek 	}
33249b8d05b8SZbigniew Bodek 
33259b8d05b8SZbigniew Bodek 	/*
33269b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
33279b8d05b8SZbigniew Bodek 	 * read is disabled
33289b8d05b8SZbigniew Bodek 	 */
33299b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
33309b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
33319b8d05b8SZbigniew Bodek 
3332a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
33333f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33349b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
33359b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
33369b8d05b8SZbigniew Bodek 	}
33379b8d05b8SZbigniew Bodek 
33389b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
33393f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33409b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
33419b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
33429b8d05b8SZbigniew Bodek 	}
33439b8d05b8SZbigniew Bodek 
33449b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
33453f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
33469b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
33479b8d05b8SZbigniew Bodek 		rc = dma_width;
33489b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
33499b8d05b8SZbigniew Bodek 	}
33509b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
33519b8d05b8SZbigniew Bodek 
33529b8d05b8SZbigniew Bodek 	/* ENA admin level init */
335367ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
33543f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33559b8d05b8SZbigniew Bodek 		device_printf(pdev,
33569b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
33579b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
33589b8d05b8SZbigniew Bodek 	}
33599b8d05b8SZbigniew Bodek 
33609b8d05b8SZbigniew Bodek 	/*
33619b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
33629b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
33639b8d05b8SZbigniew Bodek 	 * information
33649b8d05b8SZbigniew Bodek 	 */
33659b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
33669b8d05b8SZbigniew Bodek 
33679b8d05b8SZbigniew Bodek 	ena_config_host_info(ena_dev);
33689b8d05b8SZbigniew Bodek 
33699b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
33709b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
33713f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33729b8d05b8SZbigniew Bodek 		device_printf(pdev,
33739b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
33749b8d05b8SZbigniew Bodek 		goto err_admin_init;
33759b8d05b8SZbigniew Bodek 	}
33769b8d05b8SZbigniew Bodek 
3377e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
3378e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
3379e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
338040621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
3381e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
33829b8d05b8SZbigniew Bodek 
33839b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
33849b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
33853f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
33869b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
33879b8d05b8SZbigniew Bodek 		goto err_admin_init;
33889b8d05b8SZbigniew Bodek 	}
33899b8d05b8SZbigniew Bodek 
33909b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
33919b8d05b8SZbigniew Bodek 
33920bdffe59SMarcin Wojtas 	return (0);
33939b8d05b8SZbigniew Bodek 
33949b8d05b8SZbigniew Bodek err_admin_init:
33959b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
33969b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
33979b8d05b8SZbigniew Bodek err_mmio_read_less:
33989b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
33999b8d05b8SZbigniew Bodek 
34000bdffe59SMarcin Wojtas 	return (rc);
34019b8d05b8SZbigniew Bodek }
34029b8d05b8SZbigniew Bodek 
34039b8d05b8SZbigniew Bodek static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
34049b8d05b8SZbigniew Bodek     int io_vectors)
34059b8d05b8SZbigniew Bodek {
34069b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
34079b8d05b8SZbigniew Bodek 	int rc;
34089b8d05b8SZbigniew Bodek 
34099b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
34103f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
34119b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
34120bdffe59SMarcin Wojtas 		return (rc);
34139b8d05b8SZbigniew Bodek 	}
34149b8d05b8SZbigniew Bodek 
34159b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
34169b8d05b8SZbigniew Bodek 
34179b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
34183f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
34199b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
34209b8d05b8SZbigniew Bodek 		goto err_disable_msix;
34219b8d05b8SZbigniew Bodek 	}
34229b8d05b8SZbigniew Bodek 
34239b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
34249b8d05b8SZbigniew Bodek 
34259b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
34269b8d05b8SZbigniew Bodek 
34270bdffe59SMarcin Wojtas 	return (0);
34289b8d05b8SZbigniew Bodek 
34299b8d05b8SZbigniew Bodek err_disable_msix:
34309b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
34319b8d05b8SZbigniew Bodek 
34320bdffe59SMarcin Wojtas 	return (rc);
34339b8d05b8SZbigniew Bodek }
34349b8d05b8SZbigniew Bodek 
34359b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
34369b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
34379b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
34389b8d05b8SZbigniew Bodek {
34399b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
344030217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
34419b8d05b8SZbigniew Bodek 	sbintime_t stime;
344230217e2dSMarcin Wojtas 	uint64_t rx_drops;
344330217e2dSMarcin Wojtas 
344430217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
344530217e2dSMarcin Wojtas 
344630217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
344730217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
344830217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
34499b8d05b8SZbigniew Bodek 
34509b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
34519b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
34529b8d05b8SZbigniew Bodek }
34539b8d05b8SZbigniew Bodek 
34549b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
34559b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
34569b8d05b8SZbigniew Bodek {
34579b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
34589b8d05b8SZbigniew Bodek 
34599b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
34609b8d05b8SZbigniew Bodek 		return;
34619b8d05b8SZbigniew Bodek 
346240621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
34639b8d05b8SZbigniew Bodek 		return;
34649b8d05b8SZbigniew Bodek 
34659b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
34669b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
34679b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
34689b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
34699b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
34709b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
3471a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
34729b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
34739b8d05b8SZbigniew Bodek 	}
34749b8d05b8SZbigniew Bodek }
34759b8d05b8SZbigniew Bodek 
34769b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
34779b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
34789b8d05b8SZbigniew Bodek {
34790bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
34800bdffe59SMarcin Wojtas 	    false)) {
34819b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
34829b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
34839b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
3484a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
34859b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
34869b8d05b8SZbigniew Bodek 	}
34879b8d05b8SZbigniew Bodek }
34889b8d05b8SZbigniew Bodek 
348974dba3adSMarcin Wojtas static int
3490d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3491d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
3492d12f7bfcSMarcin Wojtas {
3493d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
3494d12f7bfcSMarcin Wojtas 		return (0);
3495d12f7bfcSMarcin Wojtas 
3496d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3497d12f7bfcSMarcin Wojtas 		return (0);
3498d12f7bfcSMarcin Wojtas 
3499d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
3500d12f7bfcSMarcin Wojtas 
3501d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3502d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
3503d12f7bfcSMarcin Wojtas 		   "Queue = %d. Reset the device\n", rx_ring->qid);
3504d12f7bfcSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3505d12f7bfcSMarcin Wojtas 		adapter->trigger_reset = true;
3506d12f7bfcSMarcin Wojtas 		return (EIO);
3507d12f7bfcSMarcin Wojtas 	}
3508d12f7bfcSMarcin Wojtas 
3509d12f7bfcSMarcin Wojtas 	return (0);
3510d12f7bfcSMarcin Wojtas }
3511d12f7bfcSMarcin Wojtas 
3512d12f7bfcSMarcin Wojtas static int
3513d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
351474dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
351574dba3adSMarcin Wojtas {
351674dba3adSMarcin Wojtas 	struct bintime curtime, time;
351774dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
3518d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
351974dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
3520d12f7bfcSMarcin Wojtas 	int i, rc = 0;
352174dba3adSMarcin Wojtas 
352274dba3adSMarcin Wojtas 	getbinuptime(&curtime);
352374dba3adSMarcin Wojtas 
352474dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
352574dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
352674dba3adSMarcin Wojtas 
35270bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
352874dba3adSMarcin Wojtas 			continue;
352974dba3adSMarcin Wojtas 
353074dba3adSMarcin Wojtas 		time = curtime;
353174dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
3532d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
3533d12f7bfcSMarcin Wojtas 
3534d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
3535d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
3536d12f7bfcSMarcin Wojtas 			/*
3537d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
3538d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
3539d12f7bfcSMarcin Wojtas 			 */
3540d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
3541d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
3542d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
3543d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3544d12f7bfcSMarcin Wojtas 			adapter->trigger_reset = true;
3545d12f7bfcSMarcin Wojtas 			return (EIO);
3546d12f7bfcSMarcin Wojtas 		}
354774dba3adSMarcin Wojtas 
354874dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
3549d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
355074dba3adSMarcin Wojtas 
355174dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
355274dba3adSMarcin Wojtas 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
355374dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
355474dba3adSMarcin Wojtas 				    tx_ring->qid, i);
355574dba3adSMarcin Wojtas 
355674dba3adSMarcin Wojtas 			tx_buf->print_once = true;
355774dba3adSMarcin Wojtas 			missed_tx++;
3558d12f7bfcSMarcin Wojtas 		}
3559d12f7bfcSMarcin Wojtas 	}
356074dba3adSMarcin Wojtas 
3561d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
356274dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
3563d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
3564d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
35654e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
3566d12f7bfcSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_MISS_TX_CMPL;
356774dba3adSMarcin Wojtas 		adapter->trigger_reset = true;
3568d12f7bfcSMarcin Wojtas 		rc = EIO;
356974dba3adSMarcin Wojtas 	}
357074dba3adSMarcin Wojtas 
3571d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
3572d12f7bfcSMarcin Wojtas 
3573d12f7bfcSMarcin Wojtas 	return (rc);
357474dba3adSMarcin Wojtas }
357574dba3adSMarcin Wojtas 
35769b8d05b8SZbigniew Bodek /*
35779b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
35789b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
35799b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
35809b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
35819b8d05b8SZbigniew Bodek  */
35820bdffe59SMarcin Wojtas static void
3583d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
35849b8d05b8SZbigniew Bodek {
35859b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
3586d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
358774dba3adSMarcin Wojtas 	int i, budget, rc;
35889b8d05b8SZbigniew Bodek 
35899b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
35909b8d05b8SZbigniew Bodek 	rmb();
35919b8d05b8SZbigniew Bodek 
35929b8d05b8SZbigniew Bodek 	if (!adapter->up)
35939b8d05b8SZbigniew Bodek 		return;
35949b8d05b8SZbigniew Bodek 
35959b8d05b8SZbigniew Bodek 	if (adapter->trigger_reset)
35969b8d05b8SZbigniew Bodek 		return;
35979b8d05b8SZbigniew Bodek 
359840621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
35999b8d05b8SZbigniew Bodek 		return;
36009b8d05b8SZbigniew Bodek 
36019b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
36029b8d05b8SZbigniew Bodek 
36039b8d05b8SZbigniew Bodek 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
36049b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
3605d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
36069b8d05b8SZbigniew Bodek 
3607d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3608d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
3609d12f7bfcSMarcin Wojtas 			return;
3610d12f7bfcSMarcin Wojtas 
3611d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
36120bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
36139b8d05b8SZbigniew Bodek 			return;
36149b8d05b8SZbigniew Bodek 
36159b8d05b8SZbigniew Bodek 		budget--;
3616cd5d5804SMarcin Wojtas 		if (budget == 0) {
36179b8d05b8SZbigniew Bodek 			i++;
36189b8d05b8SZbigniew Bodek 			break;
36199b8d05b8SZbigniew Bodek 		}
36209b8d05b8SZbigniew Bodek 	}
36219b8d05b8SZbigniew Bodek 
36229b8d05b8SZbigniew Bodek 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
36239b8d05b8SZbigniew Bodek }
36249b8d05b8SZbigniew Bodek 
3625*5cb9db07SMarcin Wojtas /* trigger rx cleanup after 2 consecutive detections */
3626efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
3627efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
3628efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
3629efe6ab18SMarcin Wojtas  * for example).
3630efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
3631efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
3632efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
3633efe6ab18SMarcin Wojtas  * able to send new packets.
3634efe6ab18SMarcin Wojtas  *
3635efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
3636efe6ab18SMarcin Wojtas  */
3637efe6ab18SMarcin Wojtas static void
3638efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
3639efe6ab18SMarcin Wojtas {
3640efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
3641efe6ab18SMarcin Wojtas 	int i, refill_required;
3642efe6ab18SMarcin Wojtas 
3643efe6ab18SMarcin Wojtas 	if (!adapter->up)
3644efe6ab18SMarcin Wojtas 		return;
3645efe6ab18SMarcin Wojtas 
3646efe6ab18SMarcin Wojtas 	if (adapter->trigger_reset)
3647efe6ab18SMarcin Wojtas 		return;
3648efe6ab18SMarcin Wojtas 
3649efe6ab18SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
3650efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
3651efe6ab18SMarcin Wojtas 
3652efe6ab18SMarcin Wojtas 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
3653efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3654efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
3655efe6ab18SMarcin Wojtas 
3656efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
3657efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
3658efe6ab18SMarcin Wojtas 				    1);
3659efe6ab18SMarcin Wojtas 
3660efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
3661efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
3662efe6ab18SMarcin Wojtas 
3663*5cb9db07SMarcin Wojtas 				taskqueue_enqueue(rx_ring->que->cleanup_tq,
3664*5cb9db07SMarcin Wojtas 				    &rx_ring->que->cleanup_task);
3665efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
3666efe6ab18SMarcin Wojtas 			}
3667efe6ab18SMarcin Wojtas 		} else {
3668efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
3669efe6ab18SMarcin Wojtas 		}
3670efe6ab18SMarcin Wojtas 	}
3671efe6ab18SMarcin Wojtas }
36729b8d05b8SZbigniew Bodek 
367340621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
367440621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
367540621d71SMarcin Wojtas {
367640621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
367740621d71SMarcin Wojtas 
367840621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
367940621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
368040621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
368140621d71SMarcin Wojtas 
368240621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
368340621d71SMarcin Wojtas 		/* convert to usec */
368440621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
368540621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
368640621d71SMarcin Wojtas 
368740621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
368840621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
368940621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
369040621d71SMarcin Wojtas 
369140621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
369240621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
369340621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
369440621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
369540621d71SMarcin Wojtas 		else
369640621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
369740621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
369840621d71SMarcin Wojtas 	}
369940621d71SMarcin Wojtas 
370040621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
370140621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
370240621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
370340621d71SMarcin Wojtas 		else
370440621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
370540621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
370640621d71SMarcin Wojtas 	}
370740621d71SMarcin Wojtas }
370840621d71SMarcin Wojtas 
37099b8d05b8SZbigniew Bodek static void
37109b8d05b8SZbigniew Bodek ena_timer_service(void *data)
37119b8d05b8SZbigniew Bodek {
37129b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
37139b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
37149b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
37159b8d05b8SZbigniew Bodek 
37169b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
37179b8d05b8SZbigniew Bodek 
37189b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
37199b8d05b8SZbigniew Bodek 
3720d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
37219b8d05b8SZbigniew Bodek 
3722efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
3723efe6ab18SMarcin Wojtas 
37240bdffe59SMarcin Wojtas 	if (host_info != NULL)
37259b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
37269b8d05b8SZbigniew Bodek 
37279b8d05b8SZbigniew Bodek 	if (unlikely(adapter->trigger_reset)) {
37289b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
37299b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
37309b8d05b8SZbigniew Bodek 		return;
37319b8d05b8SZbigniew Bodek 	}
37329b8d05b8SZbigniew Bodek 
37339b8d05b8SZbigniew Bodek 	/*
37349b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
37359b8d05b8SZbigniew Bodek 	 */
37369b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
37379b8d05b8SZbigniew Bodek }
37389b8d05b8SZbigniew Bodek 
37399b8d05b8SZbigniew Bodek static void
37409b8d05b8SZbigniew Bodek ena_reset_task(void *arg, int pending)
37419b8d05b8SZbigniew Bodek {
37429b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
37439b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
37449b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
37459b8d05b8SZbigniew Bodek 	bool dev_up;
37469b8d05b8SZbigniew Bodek 	int rc;
37479b8d05b8SZbigniew Bodek 
37489b8d05b8SZbigniew Bodek 	if (unlikely(!adapter->trigger_reset)) {
37499b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
37509b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
37519b8d05b8SZbigniew Bodek 		return;
37529b8d05b8SZbigniew Bodek 	}
37539b8d05b8SZbigniew Bodek 
37549b8d05b8SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
37559b8d05b8SZbigniew Bodek 
37569b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
37579b8d05b8SZbigniew Bodek 
37589b8d05b8SZbigniew Bodek 	dev_up = adapter->up;
37599b8d05b8SZbigniew Bodek 
37609b8d05b8SZbigniew Bodek 	ena_com_set_admin_running_state(ena_dev, false);
37619b8d05b8SZbigniew Bodek 	ena_down(adapter);
3762a195fab0SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
37639b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
37649b8d05b8SZbigniew Bodek 	ena_com_abort_admin_commands(ena_dev);
37659b8d05b8SZbigniew Bodek 	ena_com_wait_for_abort_completion(ena_dev);
37669b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
37679b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
37689b8d05b8SZbigniew Bodek 
3769a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
37709b8d05b8SZbigniew Bodek 	adapter->trigger_reset = false;
37719b8d05b8SZbigniew Bodek 
37729b8d05b8SZbigniew Bodek 	/* Finished destroy part. Restart the device */
37739b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
37749b8d05b8SZbigniew Bodek 	    &adapter->wd_active);
37753f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37769b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
37779b8d05b8SZbigniew Bodek 		    "ENA device init failed! (err: %d)\n", rc);
37789b8d05b8SZbigniew Bodek 		goto err_dev_free;
37799b8d05b8SZbigniew Bodek 	}
37809b8d05b8SZbigniew Bodek 
37816064f289SMarcin Wojtas 	rc = ena_handle_updated_queues(adapter, &get_feat_ctx);
37826064f289SMarcin Wojtas 	if (unlikely(rc != 0))
37836064f289SMarcin Wojtas 		goto err_dev_free;
37846064f289SMarcin Wojtas 
37859b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
37869b8d05b8SZbigniew Bodek 	    adapter->num_queues);
37873f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37889b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Enable MSI-X failed\n");
37899b8d05b8SZbigniew Bodek 		goto err_com_free;
37909b8d05b8SZbigniew Bodek 	}
37919b8d05b8SZbigniew Bodek 
37929b8d05b8SZbigniew Bodek 	/* If the interface was up before the reset bring it up */
37939b8d05b8SZbigniew Bodek 	if (dev_up) {
37949b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
37953f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
37969b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
37979b8d05b8SZbigniew Bodek 			    "Failed to create I/O queues\n");
37989b8d05b8SZbigniew Bodek 			goto err_msix_free;
37999b8d05b8SZbigniew Bodek 		}
38009b8d05b8SZbigniew Bodek 	}
38019b8d05b8SZbigniew Bodek 
38029b8d05b8SZbigniew Bodek 	callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
38039b8d05b8SZbigniew Bodek 	    ena_timer_service, (void *)adapter, 0);
38049b8d05b8SZbigniew Bodek 
38059b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
38069b8d05b8SZbigniew Bodek 
38079b8d05b8SZbigniew Bodek 	return;
38089b8d05b8SZbigniew Bodek 
38099b8d05b8SZbigniew Bodek err_msix_free:
38109b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
38119b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
38129b8d05b8SZbigniew Bodek err_com_free:
38139b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
38149b8d05b8SZbigniew Bodek err_dev_free:
38159b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "ENA reset failed!\n");
38169b8d05b8SZbigniew Bodek 	adapter->running = false;
38179b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
38189b8d05b8SZbigniew Bodek }
38199b8d05b8SZbigniew Bodek 
38209b8d05b8SZbigniew Bodek /**
38219b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
38229b8d05b8SZbigniew Bodek  * @pdev: device information struct
38239b8d05b8SZbigniew Bodek  *
38249b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
38259b8d05b8SZbigniew Bodek  *
38269b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
38279b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
38289b8d05b8SZbigniew Bodek  * and a hardware reset occur.
38299b8d05b8SZbigniew Bodek  **/
38309b8d05b8SZbigniew Bodek static int
38319b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
38329b8d05b8SZbigniew Bodek {
38339b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
38346064f289SMarcin Wojtas 	struct ena_calc_queue_size_ctx calc_queue_ctx = { 0 };
38359b8d05b8SZbigniew Bodek 	static int version_printed;
38369b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
38379b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
38389b8d05b8SZbigniew Bodek 	int io_queue_num;
38399b8d05b8SZbigniew Bodek 	int rc;
38409b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
38419b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
38429b8d05b8SZbigniew Bodek 
38439b8d05b8SZbigniew Bodek 	mtx_init(&adapter->global_mtx, "ENA global mtx", NULL, MTX_DEF);
38449b8d05b8SZbigniew Bodek 	sx_init(&adapter->ioctl_sx, "ENA ioctl sx");
38459b8d05b8SZbigniew Bodek 
38469b8d05b8SZbigniew Bodek 	/* Set up the timer service */
38479b8d05b8SZbigniew Bodek 	callout_init_mtx(&adapter->timer_service, &adapter->global_mtx, 0);
38489b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
38499b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
38509b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
38519b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
38529b8d05b8SZbigniew Bodek 
38539b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
38549b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
38559b8d05b8SZbigniew Bodek 
38569b8d05b8SZbigniew Bodek 	rc = ena_allocate_pci_resources(adapter);
38573f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
38589b8d05b8SZbigniew Bodek 		device_printf(pdev, "PCI resource allocation failed!\n");
38599b8d05b8SZbigniew Bodek 		ena_free_pci_resources(adapter);
3860cd5d5804SMarcin Wojtas 		return (rc);
38619b8d05b8SZbigniew Bodek 	}
38629b8d05b8SZbigniew Bodek 
38639b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3864cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3865cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
38669b8d05b8SZbigniew Bodek 
38679b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
38689b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
38699b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
38709b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
38719b8d05b8SZbigniew Bodek 
38729b8d05b8SZbigniew Bodek 	/* Store register resources */
38739b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
38749b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
38759b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
38769b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
38779b8d05b8SZbigniew Bodek 
38783f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
38799b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
38809b8d05b8SZbigniew Bodek 		rc = ENXIO;
3881cd5d5804SMarcin Wojtas 		goto err_bus_free;
38829b8d05b8SZbigniew Bodek 	}
38839b8d05b8SZbigniew Bodek 
38849b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
38859b8d05b8SZbigniew Bodek 
38869b8d05b8SZbigniew Bodek 	/* Device initialization */
38879b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
38883f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
38899b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
38909b8d05b8SZbigniew Bodek 		rc = ENXIO;
38919b8d05b8SZbigniew Bodek 		goto err_bus_free;
38929b8d05b8SZbigniew Bodek 	}
38939b8d05b8SZbigniew Bodek 
38949b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
38959b8d05b8SZbigniew Bodek 
38969b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
38979b8d05b8SZbigniew Bodek 
38989b8d05b8SZbigniew Bodek 	/* Set for sure that interface is not up */
38999b8d05b8SZbigniew Bodek 	adapter->up = false;
39009b8d05b8SZbigniew Bodek 
39019b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
39029b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
39039b8d05b8SZbigniew Bodek 
39046064f289SMarcin Wojtas 	calc_queue_ctx.ena_dev = ena_dev;
39056064f289SMarcin Wojtas 	calc_queue_ctx.get_feat_ctx = &get_feat_ctx;
39066064f289SMarcin Wojtas 	calc_queue_ctx.pdev = pdev;
39076064f289SMarcin Wojtas 
39089b8d05b8SZbigniew Bodek 	/* calculate IO queue number to create */
39099b8d05b8SZbigniew Bodek 	io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx);
39109b8d05b8SZbigniew Bodek 
39119b8d05b8SZbigniew Bodek 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
39129b8d05b8SZbigniew Bodek 	    io_queue_num);
39139b8d05b8SZbigniew Bodek 	adapter->num_queues = io_queue_num;
39149b8d05b8SZbigniew Bodek 
39153cfadb28SMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
39166064f289SMarcin Wojtas 	// Set the requested Rx ring size
39176064f289SMarcin Wojtas 	adapter->rx_ring_size = ENA_DEFAULT_RING_SIZE;
39189b8d05b8SZbigniew Bodek 	/* calculatre ring sizes */
39196064f289SMarcin Wojtas 	rc = ena_calc_queue_size(adapter, &calc_queue_ctx);
39206064f289SMarcin Wojtas 	if (unlikely((rc != 0) || (io_queue_num <= 0))) {
39216064f289SMarcin Wojtas 		rc = EFAULT;
39229b8d05b8SZbigniew Bodek 		goto err_com_free;
39239b8d05b8SZbigniew Bodek 	}
39249b8d05b8SZbigniew Bodek 
3925a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3926a195fab0SMarcin Wojtas 
39276064f289SMarcin Wojtas 	adapter->tx_ring_size = calc_queue_ctx.tx_queue_size;
39286064f289SMarcin Wojtas 	adapter->rx_ring_size = calc_queue_ctx.rx_queue_size;
39299b8d05b8SZbigniew Bodek 
39306064f289SMarcin Wojtas 	adapter->max_tx_sgl_size = calc_queue_ctx.max_tx_sgl_size;
39316064f289SMarcin Wojtas 	adapter->max_rx_sgl_size = calc_queue_ctx.max_rx_sgl_size;
39326064f289SMarcin Wojtas 
39336064f289SMarcin Wojtas 	adapter->buf_ring_size = ENA_DEFAULT_BUF_RING_SIZE;
39349b8d05b8SZbigniew Bodek 
39359b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
39369b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
39374e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
39384e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
3939cd5d5804SMarcin Wojtas 		goto err_com_free;
39404e8acd84SMarcin Wojtas 	}
39419b8d05b8SZbigniew Bodek 
39429b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
39434e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
39444e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
3945cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
39464e8acd84SMarcin Wojtas 	}
39479b8d05b8SZbigniew Bodek 
39489b8d05b8SZbigniew Bodek 	/* initialize rings basic information */
39496064f289SMarcin Wojtas 	device_printf(pdev,
39506064f289SMarcin Wojtas 	    "Creating %d io queues. Rx queue size: %d, Tx queue size: %d\n",
39516064f289SMarcin Wojtas 	    io_queue_num,
39526064f289SMarcin Wojtas 	    calc_queue_ctx.rx_queue_size,
39536064f289SMarcin Wojtas 	    calc_queue_ctx.tx_queue_size);
3954cd5d5804SMarcin Wojtas 	ena_init_io_rings(adapter);
39559b8d05b8SZbigniew Bodek 
39569b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
39573f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
39589b8d05b8SZbigniew Bodek 		device_printf(pdev,
39599b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
3960c115a1e2SMarcin Wojtas 		goto err_io_free;
3961c115a1e2SMarcin Wojtas 	}
3962c115a1e2SMarcin Wojtas 
3963c115a1e2SMarcin Wojtas 	/* setup network interface */
3964c115a1e2SMarcin Wojtas 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
3965c115a1e2SMarcin Wojtas 	if (unlikely(rc != 0)) {
3966c115a1e2SMarcin Wojtas 		device_printf(pdev, "Error with network interface setup\n");
3967c115a1e2SMarcin Wojtas 		goto err_msix_free;
39689b8d05b8SZbigniew Bodek 	}
39699b8d05b8SZbigniew Bodek 
3970081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3971081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3972081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3973081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3974081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3975081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3976081169f2SZbigniew Bodek 
39779b8d05b8SZbigniew Bodek 	/* Initialize statistics */
39789b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
39799b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
398030217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
398130217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
39829b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
39839b8d05b8SZbigniew Bodek 
39849b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
39859b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
39869b8d05b8SZbigniew Bodek 
39879b8d05b8SZbigniew Bodek 	adapter->running = true;
39889b8d05b8SZbigniew Bodek 	return (0);
39899b8d05b8SZbigniew Bodek 
3990c115a1e2SMarcin Wojtas err_msix_free:
3991c115a1e2SMarcin Wojtas 	ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR);
3992c115a1e2SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
3993c115a1e2SMarcin Wojtas 	ena_disable_msix(adapter);
3994cd5d5804SMarcin Wojtas err_io_free:
39959b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
39969b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3997cd5d5804SMarcin Wojtas err_tx_tag_free:
39989b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3999cd5d5804SMarcin Wojtas err_com_free:
40009b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
40019b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
4002cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
40039b8d05b8SZbigniew Bodek err_bus_free:
40049b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
40059b8d05b8SZbigniew Bodek 	free(ena_dev, M_DEVBUF);
40069b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
4007cd5d5804SMarcin Wojtas 
40089b8d05b8SZbigniew Bodek 	return (rc);
40099b8d05b8SZbigniew Bodek }
40109b8d05b8SZbigniew Bodek 
40119b8d05b8SZbigniew Bodek /**
40129b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
40139b8d05b8SZbigniew Bodek  * @pdev: device information struct
40149b8d05b8SZbigniew Bodek  *
40159b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
40169b8d05b8SZbigniew Bodek  * that it should release a PCI device.
40179b8d05b8SZbigniew Bodek  **/
40189b8d05b8SZbigniew Bodek static int
40199b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
40209b8d05b8SZbigniew Bodek {
40219b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
40229b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
40239b8d05b8SZbigniew Bodek 	int rc;
40249b8d05b8SZbigniew Bodek 
40259b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
40269b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
40279b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
40289b8d05b8SZbigniew Bodek 		return (EBUSY);
40299b8d05b8SZbigniew Bodek 	}
40309b8d05b8SZbigniew Bodek 
40319151c55dSMarcin Wojtas 	ether_ifdetach(adapter->ifp);
40329151c55dSMarcin Wojtas 
40339b8d05b8SZbigniew Bodek 	/* Free reset task and callout */
40349b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
40359b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
40369b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
40379b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
40389b8d05b8SZbigniew Bodek 
4039e67c6554SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
40409b8d05b8SZbigniew Bodek 	ena_down(adapter);
4041e67c6554SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
40429b8d05b8SZbigniew Bodek 
40439b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
40449b8d05b8SZbigniew Bodek 
404530217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
404630217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
40479b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
40489b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
40499b8d05b8SZbigniew Bodek 
40503f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support))
40519b8d05b8SZbigniew Bodek 		ena_com_rss_destroy(ena_dev);
40529b8d05b8SZbigniew Bodek 
40539b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
40543f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
40559b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
40569b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
40579b8d05b8SZbigniew Bodek 
40589b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
40593f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
40609b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
40619b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
40629b8d05b8SZbigniew Bodek 
40639b8d05b8SZbigniew Bodek 	/* Reset the device only if the device is running. */
40649b8d05b8SZbigniew Bodek 	if (adapter->running)
4065a195fab0SMarcin Wojtas 		ena_com_dev_reset(ena_dev, adapter->reset_reason);
40669b8d05b8SZbigniew Bodek 
40679b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
40689b8d05b8SZbigniew Bodek 
40699b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
40709b8d05b8SZbigniew Bodek 
4071197f0284SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
4072197f0284SMarcin Wojtas 
4073197f0284SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
4074197f0284SMarcin Wojtas 
4075197f0284SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
4076197f0284SMarcin Wojtas 
40779b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
40789b8d05b8SZbigniew Bodek 
40799b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
40809b8d05b8SZbigniew Bodek 
40819b8d05b8SZbigniew Bodek 	mtx_destroy(&adapter->global_mtx);
40829b8d05b8SZbigniew Bodek 	sx_destroy(&adapter->ioctl_sx);
40839b8d05b8SZbigniew Bodek 
40849151c55dSMarcin Wojtas 	if_free(adapter->ifp);
40859151c55dSMarcin Wojtas 
40869b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
40879b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
40889b8d05b8SZbigniew Bodek 
40899b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
40909b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
40919b8d05b8SZbigniew Bodek 
40929b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
40939b8d05b8SZbigniew Bodek }
40949b8d05b8SZbigniew Bodek 
40959b8d05b8SZbigniew Bodek /******************************************************************************
40969b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
40979b8d05b8SZbigniew Bodek  *****************************************************************************/
40989b8d05b8SZbigniew Bodek /**
40999b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
41009b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
41019b8d05b8SZbigniew Bodek  **/
41029b8d05b8SZbigniew Bodek static void
41039b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
41049b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
41059b8d05b8SZbigniew Bodek {
41069b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
41079b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
41089b8d05b8SZbigniew Bodek 	int status;
41099b8d05b8SZbigniew Bodek 	if_t ifp;
41109b8d05b8SZbigniew Bodek 
41119b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
41129b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
41139b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
41149b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
41159b8d05b8SZbigniew Bodek 
41169b8d05b8SZbigniew Bodek 	if (status != 0) {
41179b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
41189b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_UP);
41199b8d05b8SZbigniew Bodek 	} else if (status == 0) {
41209b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
41219b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
41229b8d05b8SZbigniew Bodek 	} else {
41239b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "invalid value recvd\n");
41249b8d05b8SZbigniew Bodek 		BUG();
41259b8d05b8SZbigniew Bodek 	}
41269b8d05b8SZbigniew Bodek 
41279b8d05b8SZbigniew Bodek 	adapter->link_status = status;
41289b8d05b8SZbigniew Bodek }
41299b8d05b8SZbigniew Bodek 
413040621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
413140621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
413240621d71SMarcin Wojtas {
413340621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
413440621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
413540621d71SMarcin Wojtas 
413640621d71SMarcin Wojtas 	ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
413740621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
413840621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
413940621d71SMarcin Wojtas 
414040621d71SMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrom) {
414140621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
414240621d71SMarcin Wojtas 		hints =
414340621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
414440621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
414540621d71SMarcin Wojtas 		break;
414640621d71SMarcin Wojtas 	default:
414740621d71SMarcin Wojtas 		device_printf(adapter->pdev,
414840621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
414940621d71SMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrom);
415040621d71SMarcin Wojtas 	}
415140621d71SMarcin Wojtas }
415240621d71SMarcin Wojtas 
41539b8d05b8SZbigniew Bodek /**
41549b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
41559b8d05b8SZbigniew Bodek  **/
41569b8d05b8SZbigniew Bodek static void
4157e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
41589b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
41599b8d05b8SZbigniew Bodek {
4160e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4161e6de9a83SMarcin Wojtas 
4162e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
4163e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
41649b8d05b8SZbigniew Bodek }
41659b8d05b8SZbigniew Bodek 
41669b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
41679b8d05b8SZbigniew Bodek     .handlers = {
41689b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
416940621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
41709b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
41719b8d05b8SZbigniew Bodek     },
41729b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
41739b8d05b8SZbigniew Bodek };
41749b8d05b8SZbigniew Bodek 
41759b8d05b8SZbigniew Bodek /*********************************************************************
41769b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
41779b8d05b8SZbigniew Bodek  *********************************************************************/
41789b8d05b8SZbigniew Bodek 
41799b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
41809b8d05b8SZbigniew Bodek     /* Device interface */
41819b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
41829b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
41839b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
41849b8d05b8SZbigniew Bodek     DEVMETHOD_END
41859b8d05b8SZbigniew Bodek };
41869b8d05b8SZbigniew Bodek 
41879b8d05b8SZbigniew Bodek static driver_t ena_driver = {
41889b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
41899b8d05b8SZbigniew Bodek };
41909b8d05b8SZbigniew Bodek 
41919b8d05b8SZbigniew Bodek devclass_t ena_devclass;
41929b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
419340abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
4194329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
41959b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
41969b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
41979b8d05b8SZbigniew Bodek 
41989b8d05b8SZbigniew Bodek /*********************************************************************/
4199