xref: /freebsd/sys/dev/ena/ena.c (revision 4e30699966e78ddcae7e46d0704822e7508072e3)
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 *);
123efe6ab18SMarcin Wojtas static void	ena_deferred_rx_cleanup(void *, int);
1249b8d05b8SZbigniew Bodek static int	ena_rx_cleanup(struct ena_ring *);
125fceb9387SMarcin Wojtas static inline int validate_tx_req_id(struct ena_ring *, uint16_t);
1269b8d05b8SZbigniew Bodek static void	ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *,
1279b8d05b8SZbigniew Bodek     struct mbuf *);
1289b8d05b8SZbigniew Bodek static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *,
1299b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *, uint16_t *);
1309b8d05b8SZbigniew Bodek static inline void ena_rx_checksum(struct ena_ring *, struct ena_com_rx_ctx *,
1319b8d05b8SZbigniew Bodek     struct mbuf *);
1329b8d05b8SZbigniew Bodek static void	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 *);
1679b8d05b8SZbigniew Bodek static int	ena_calc_queue_size(struct ena_adapter *, uint16_t *,
1689b8d05b8SZbigniew Bodek     uint16_t *, struct ena_com_dev_get_features_ctx *);
1699b8d05b8SZbigniew Bodek static int	ena_rss_init_default(struct ena_adapter *);
1709b8d05b8SZbigniew Bodek static void	ena_rss_init_default_deferred(void *);
1719b8d05b8SZbigniew Bodek static void	ena_config_host_info(struct ena_com_dev *);
1729b8d05b8SZbigniew Bodek static int	ena_attach(device_t);
1739b8d05b8SZbigniew Bodek static int	ena_detach(device_t);
1749b8d05b8SZbigniew Bodek static int	ena_device_init(struct ena_adapter *, device_t,
1759b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *, int *);
1769b8d05b8SZbigniew Bodek static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *,
1779b8d05b8SZbigniew Bodek     int);
1789b8d05b8SZbigniew Bodek static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
1799b8d05b8SZbigniew Bodek static void	unimplemented_aenq_handler(void *,
1809b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *);
1819b8d05b8SZbigniew Bodek static void	ena_timer_service(void *);
1829b8d05b8SZbigniew Bodek 
1839b8d05b8SZbigniew Bodek static char ena_version[] = DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
1849b8d05b8SZbigniew Bodek 
1859b8d05b8SZbigniew Bodek static SYSCTL_NODE(_hw, OID_AUTO, ena, CTLFLAG_RD, 0, "ENA driver parameters");
1869b8d05b8SZbigniew Bodek 
1879b8d05b8SZbigniew Bodek /*
1889b8d05b8SZbigniew Bodek  * Tuneable number of buffers in the buf-ring (drbr)
1899b8d05b8SZbigniew Bodek  */
1909b8d05b8SZbigniew Bodek static int ena_buf_ring_size = 4096;
1919b8d05b8SZbigniew Bodek SYSCTL_INT(_hw_ena, OID_AUTO, buf_ring_size, CTLFLAG_RWTUN,
1929b8d05b8SZbigniew Bodek     &ena_buf_ring_size, 0, "Size of the bufring");
1939b8d05b8SZbigniew Bodek 
1944e8acd84SMarcin Wojtas /*
1954e8acd84SMarcin Wojtas  * Logging level for changing verbosity of the output
1964e8acd84SMarcin Wojtas  */
1974e8acd84SMarcin Wojtas int ena_log_level = ENA_ALERT | ENA_WARNING;
1984e8acd84SMarcin Wojtas SYSCTL_INT(_hw_ena, OID_AUTO, log_level, CTLFLAG_RWTUN,
1994e8acd84SMarcin Wojtas     &ena_log_level, 0, "Logging level indicating verbosity of the logs");
2004e8acd84SMarcin Wojtas 
2019b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
2029b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
2039b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
2049b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
2059b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
2069b8d05b8SZbigniew Bodek     /* Last entry */
2079b8d05b8SZbigniew Bodek     { 0, 0, 0 }
2089b8d05b8SZbigniew Bodek };
2099b8d05b8SZbigniew Bodek 
2109b8d05b8SZbigniew Bodek /*
2119b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
2129b8d05b8SZbigniew Bodek  */
2139b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
2149b8d05b8SZbigniew Bodek 
2159b8d05b8SZbigniew Bodek void
2169b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2179b8d05b8SZbigniew Bodek {
2180bdffe59SMarcin Wojtas 	if (error != 0)
2199b8d05b8SZbigniew Bodek 		return;
2209b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
2219b8d05b8SZbigniew Bodek }
2229b8d05b8SZbigniew Bodek 
2239b8d05b8SZbigniew Bodek int
2249b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
2259b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
2269b8d05b8SZbigniew Bodek {
2279b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2280bdffe59SMarcin Wojtas 	uint32_t maxsize;
2290bdffe59SMarcin Wojtas 	uint64_t dma_space_addr;
2309b8d05b8SZbigniew Bodek 	int error;
2319b8d05b8SZbigniew Bodek 
2320bdffe59SMarcin Wojtas 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
2330bdffe59SMarcin Wojtas 
2340bdffe59SMarcin Wojtas 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2353f9ed7abSMarcin Wojtas 	if (unlikely(dma_space_addr == 0))
2369b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2370bdffe59SMarcin Wojtas 
2389b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2399b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2408a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2418a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2429b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2439b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2449b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2459b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2469b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2479b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2489b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2499b8d05b8SZbigniew Bodek 	    &dma->tag);
2503f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2514e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dma_tag_create failed: %d\n", error);
2529b8d05b8SZbigniew Bodek 		goto fail_tag;
2539b8d05b8SZbigniew Bodek 	}
2549b8d05b8SZbigniew Bodek 
2559b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2569b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2573f9ed7abSMarcin Wojtas 	if (unlikely(error != 0)) {
2584e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
2594e8acd84SMarcin Wojtas 		    (uintmax_t)size, error);
2609b8d05b8SZbigniew Bodek 		goto fail_map_create;
2619b8d05b8SZbigniew Bodek 	}
2629b8d05b8SZbigniew Bodek 
2639b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2649b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2659b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2663f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (dma->paddr == 0))) {
2674e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
2689b8d05b8SZbigniew Bodek 		goto fail_map_load;
2699b8d05b8SZbigniew Bodek 	}
2709b8d05b8SZbigniew Bodek 
271e8073738SMarcin Wojtas 	bus_dmamap_sync(dma->tag, dma->map,
272e8073738SMarcin Wojtas 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
273e8073738SMarcin Wojtas 
2749b8d05b8SZbigniew Bodek 	return (0);
2759b8d05b8SZbigniew Bodek 
2769b8d05b8SZbigniew Bodek fail_map_load:
2779b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2787d2544e6SMarcin Wojtas fail_map_create:
2799b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2809b8d05b8SZbigniew Bodek fail_tag:
2819b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2825b14f92eSMarcin Wojtas 	dma->vaddr = NULL;
2835b14f92eSMarcin Wojtas 	dma->paddr = 0;
2849b8d05b8SZbigniew Bodek 
2859b8d05b8SZbigniew Bodek 	return (error);
2869b8d05b8SZbigniew Bodek }
2879b8d05b8SZbigniew Bodek 
2889b8d05b8SZbigniew Bodek static int
2899b8d05b8SZbigniew Bodek ena_allocate_pci_resources(struct ena_adapter* adapter)
2909b8d05b8SZbigniew Bodek {
2919b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2929b8d05b8SZbigniew Bodek 	int rid;
2939b8d05b8SZbigniew Bodek 
2949b8d05b8SZbigniew Bodek 	rid = PCIR_BAR(ENA_REG_BAR);
2959b8d05b8SZbigniew Bodek 	adapter->memory = NULL;
2969b8d05b8SZbigniew Bodek 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
2979b8d05b8SZbigniew Bodek 	    &rid, RF_ACTIVE);
2983f9ed7abSMarcin Wojtas 	if (unlikely(adapter->registers == NULL)) {
2999b8d05b8SZbigniew Bodek 		device_printf(pdev, "Unable to allocate bus resource: "
3009b8d05b8SZbigniew Bodek 		    "registers\n");
3019b8d05b8SZbigniew Bodek 		return (ENXIO);
3029b8d05b8SZbigniew Bodek 	}
3039b8d05b8SZbigniew Bodek 
3049b8d05b8SZbigniew Bodek 	return (0);
3059b8d05b8SZbigniew Bodek }
3069b8d05b8SZbigniew Bodek 
3079b8d05b8SZbigniew Bodek static void
3089b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
3099b8d05b8SZbigniew Bodek {
3109b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
3119b8d05b8SZbigniew Bodek 
3129b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
3139b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3149b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
3159b8d05b8SZbigniew Bodek 	}
3169b8d05b8SZbigniew Bodek 
3179b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
3189b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3199b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
3209b8d05b8SZbigniew Bodek 	}
3219b8d05b8SZbigniew Bodek }
3229b8d05b8SZbigniew Bodek 
3239b8d05b8SZbigniew Bodek static int
3249b8d05b8SZbigniew Bodek ena_probe(device_t dev)
3259b8d05b8SZbigniew Bodek {
3269b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3279b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3289b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3299b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3309b8d05b8SZbigniew Bodek 
3319b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3329b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3339b8d05b8SZbigniew Bodek 
3349b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3359b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3369b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3379b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
3389b8d05b8SZbigniew Bodek 			ena_trace(ENA_DBG, "vendor=%x device=%x ",
3399b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3409b8d05b8SZbigniew Bodek 
3419b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3429b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3439b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3449b8d05b8SZbigniew Bodek 		}
3459b8d05b8SZbigniew Bodek 
3469b8d05b8SZbigniew Bodek 		ent++;
3479b8d05b8SZbigniew Bodek 
3489b8d05b8SZbigniew Bodek 	}
3499b8d05b8SZbigniew Bodek 
3509b8d05b8SZbigniew Bodek 	return (ENXIO);
3519b8d05b8SZbigniew Bodek }
3529b8d05b8SZbigniew Bodek 
3539b8d05b8SZbigniew Bodek static int
3549b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3559b8d05b8SZbigniew Bodek {
3569b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3573cfadb28SMarcin Wojtas 	int rc;
3589b8d05b8SZbigniew Bodek 
3593cfadb28SMarcin Wojtas 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
3609b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3613cfadb28SMarcin Wojtas 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
3623cfadb28SMarcin Wojtas 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
3633cfadb28SMarcin Wojtas 		return (EINVAL);
3649b8d05b8SZbigniew Bodek 	}
3659b8d05b8SZbigniew Bodek 
3669b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3673cfadb28SMarcin Wojtas 	if (likely(rc == 0)) {
3683cfadb28SMarcin Wojtas 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
3693cfadb28SMarcin Wojtas 		if_setmtu(ifp, new_mtu);
3703cfadb28SMarcin Wojtas 	} else {
3713cfadb28SMarcin Wojtas 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
3723cfadb28SMarcin Wojtas 		    new_mtu);
3733cfadb28SMarcin Wojtas 	}
3749b8d05b8SZbigniew Bodek 
3753cfadb28SMarcin Wojtas 	return (rc);
3769b8d05b8SZbigniew Bodek }
3779b8d05b8SZbigniew Bodek 
3789b8d05b8SZbigniew Bodek static inline void
3799b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3809b8d05b8SZbigniew Bodek {
3819b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3829b8d05b8SZbigniew Bodek 
3839b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3849b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3859b8d05b8SZbigniew Bodek }
3869b8d05b8SZbigniew Bodek 
3879b8d05b8SZbigniew Bodek static inline void
3889b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3899b8d05b8SZbigniew Bodek {
3909b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3919b8d05b8SZbigniew Bodek 
3929b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3939b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
3949b8d05b8SZbigniew Bodek }
3959b8d05b8SZbigniew Bodek 
3969b8d05b8SZbigniew Bodek static inline void
3979b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
3989b8d05b8SZbigniew Bodek {
3999b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
4009b8d05b8SZbigniew Bodek 
4019b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
4029b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
4039b8d05b8SZbigniew Bodek }
4049b8d05b8SZbigniew Bodek 
4059b8d05b8SZbigniew Bodek static void
4069b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
4079b8d05b8SZbigniew Bodek     uint16_t qid)
4089b8d05b8SZbigniew Bodek {
4099b8d05b8SZbigniew Bodek 
4109b8d05b8SZbigniew Bodek 	ring->qid = qid;
4119b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
4129b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
413d12f7bfcSMarcin Wojtas 	ring->first_interrupt = false;
414d12f7bfcSMarcin Wojtas 	ring->no_interrupt_event_cnt = 0;
4159b8d05b8SZbigniew Bodek }
4169b8d05b8SZbigniew Bodek 
417cd5d5804SMarcin Wojtas static void
4189b8d05b8SZbigniew Bodek ena_init_io_rings(struct ena_adapter *adapter)
4199b8d05b8SZbigniew Bodek {
4209b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
4219b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
4229b8d05b8SZbigniew Bodek 	struct ena_que *que;
4239b8d05b8SZbigniew Bodek 	int i;
4249b8d05b8SZbigniew Bodek 
4259b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
4269b8d05b8SZbigniew Bodek 
4279b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
4289b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4299b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4309b8d05b8SZbigniew Bodek 
4319b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4329b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4339b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4349b8d05b8SZbigniew Bodek 
4359b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4369b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
4379b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4389b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4399b8d05b8SZbigniew Bodek 		txr->smoothed_interval =
4409b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
4419b8d05b8SZbigniew Bodek 
4429b8d05b8SZbigniew Bodek 		/* Allocate a buf ring */
4439b8d05b8SZbigniew Bodek 		txr->br = buf_ring_alloc(ena_buf_ring_size, M_DEVBUF,
4449b8d05b8SZbigniew Bodek 		    M_WAITOK, &txr->ring_mtx);
4459b8d05b8SZbigniew Bodek 
4469b8d05b8SZbigniew Bodek 		/* Alloc TX statistics. */
4479b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4489b8d05b8SZbigniew Bodek 		    sizeof(txr->tx_stats));
4499b8d05b8SZbigniew Bodek 
4509b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4519b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4529b8d05b8SZbigniew Bodek 		rxr->smoothed_interval =
4539b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
4549b8d05b8SZbigniew Bodek 
4559b8d05b8SZbigniew Bodek 		/* Alloc RX statistics. */
4569b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4579b8d05b8SZbigniew Bodek 		    sizeof(rxr->rx_stats));
4589b8d05b8SZbigniew Bodek 
4599b8d05b8SZbigniew Bodek 		/* Initialize locks */
4609b8d05b8SZbigniew Bodek 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4619b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4629b8d05b8SZbigniew Bodek 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4639b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4649b8d05b8SZbigniew Bodek 
4659b8d05b8SZbigniew Bodek 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
466efe6ab18SMarcin Wojtas 		mtx_init(&rxr->ring_mtx, rxr->mtx_name, NULL, MTX_DEF);
4679b8d05b8SZbigniew Bodek 
4689b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4699b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4709b8d05b8SZbigniew Bodek 		que->id = i;
4719b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4729b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4739b8d05b8SZbigniew Bodek 
4749b8d05b8SZbigniew Bodek 		txr->que = que;
4759b8d05b8SZbigniew Bodek 		rxr->que = que;
476efe6ab18SMarcin Wojtas 
477efe6ab18SMarcin Wojtas 		rxr->empty_rx_queue = 0;
4789b8d05b8SZbigniew Bodek 	}
4799b8d05b8SZbigniew Bodek }
4809b8d05b8SZbigniew Bodek 
4819b8d05b8SZbigniew Bodek static void
4829b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4839b8d05b8SZbigniew Bodek {
4849b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4859b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4869b8d05b8SZbigniew Bodek 
4879b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4889b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4899b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4909b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4919b8d05b8SZbigniew Bodek 
4927d2544e6SMarcin Wojtas 	ENA_RING_MTX_LOCK(txr);
4937d2544e6SMarcin Wojtas 	drbr_free(txr->br, M_DEVBUF);
4947d2544e6SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(txr);
4957d2544e6SMarcin Wojtas 
4969b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
497efe6ab18SMarcin Wojtas 	mtx_destroy(&rxr->ring_mtx);
4989b8d05b8SZbigniew Bodek }
4999b8d05b8SZbigniew Bodek 
5009b8d05b8SZbigniew Bodek static void
5019b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
5029b8d05b8SZbigniew Bodek {
5039b8d05b8SZbigniew Bodek 	int i;
5049b8d05b8SZbigniew Bodek 
5059b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
5069b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
5079b8d05b8SZbigniew Bodek 
5089b8d05b8SZbigniew Bodek }
5099b8d05b8SZbigniew Bodek 
5109b8d05b8SZbigniew Bodek static int
5119b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
5129b8d05b8SZbigniew Bodek {
5139b8d05b8SZbigniew Bodek 	int ret;
5149b8d05b8SZbigniew Bodek 
5159b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
5169b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
5179b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5188a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5198a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5209b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5219b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
5228a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
5239b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
5249b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5259b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5269b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
5279b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5289b8d05b8SZbigniew Bodek 
5299b8d05b8SZbigniew Bodek 	return (ret);
5309b8d05b8SZbigniew Bodek }
5319b8d05b8SZbigniew Bodek 
5329b8d05b8SZbigniew Bodek static int
5339b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5349b8d05b8SZbigniew Bodek {
5359b8d05b8SZbigniew Bodek 	int ret;
5369b8d05b8SZbigniew Bodek 
5379b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5389b8d05b8SZbigniew Bodek 
5393f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5409b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5419b8d05b8SZbigniew Bodek 
5429b8d05b8SZbigniew Bodek 	return (ret);
5439b8d05b8SZbigniew Bodek }
5449b8d05b8SZbigniew Bodek 
5459b8d05b8SZbigniew Bodek static int
5469b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5479b8d05b8SZbigniew Bodek {
5489b8d05b8SZbigniew Bodek 	int ret;
5499b8d05b8SZbigniew Bodek 
5509b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5519b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5529b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5538a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5548a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5559b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5569b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsize 		     */
5574727bda6SMarcin Wojtas 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
5589b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsegsize 	     */
5599b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5609b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5619b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5629b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5639b8d05b8SZbigniew Bodek 
5649b8d05b8SZbigniew Bodek 	return (ret);
5659b8d05b8SZbigniew Bodek }
5669b8d05b8SZbigniew Bodek 
5679b8d05b8SZbigniew Bodek static int
5689b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5699b8d05b8SZbigniew Bodek {
5709b8d05b8SZbigniew Bodek 	int ret;
5719b8d05b8SZbigniew Bodek 
5729b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5739b8d05b8SZbigniew Bodek 
5743f9ed7abSMarcin Wojtas 	if (likely(ret == 0))
5759b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5769b8d05b8SZbigniew Bodek 
5779b8d05b8SZbigniew Bodek 	return (ret);
5789b8d05b8SZbigniew Bodek }
5799b8d05b8SZbigniew Bodek 
5809b8d05b8SZbigniew Bodek /**
5819b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
5829b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
5839b8d05b8SZbigniew Bodek  * @qid: queue index
5849b8d05b8SZbigniew Bodek  *
5859b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
5869b8d05b8SZbigniew Bodek  **/
5879b8d05b8SZbigniew Bodek static int
5889b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
5899b8d05b8SZbigniew Bodek {
5909b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
5919b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
5929b8d05b8SZbigniew Bodek 	int size, i, err;
5939b8d05b8SZbigniew Bodek 
5949b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
5959b8d05b8SZbigniew Bodek 
5969b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
5973f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->tx_buffer_info == NULL))
5987d2544e6SMarcin Wojtas 		return (ENOMEM);
5999b8d05b8SZbigniew Bodek 
6009b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
6019b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
6023f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->free_tx_ids == NULL))
6037d2544e6SMarcin Wojtas 		goto err_buf_info_free;
6049b8d05b8SZbigniew Bodek 
6059b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
6069b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
6079b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
6089b8d05b8SZbigniew Bodek 
6099b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
6109b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
6119b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
6129b8d05b8SZbigniew Bodek 
6139b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
6149b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
6159b8d05b8SZbigniew Bodek 
6169b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
617b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6189b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
619b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6209b8d05b8SZbigniew Bodek 
6219b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6229b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6239b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6249b8d05b8SZbigniew Bodek 		    &tx_ring->tx_buffer_info[i].map);
6253f9ed7abSMarcin Wojtas 		if (unlikely(err != 0)) {
6264e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
6279b8d05b8SZbigniew Bodek 			     "Unable to create Tx DMA map for buffer %d\n", i);
6287d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
6299b8d05b8SZbigniew Bodek 		}
6309b8d05b8SZbigniew Bodek 	}
6319b8d05b8SZbigniew Bodek 
6329b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6339b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6349b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6359b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6363f9ed7abSMarcin Wojtas 	if (unlikely(tx_ring->enqueue_tq == NULL)) {
6374e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT,
6389b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6399b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6407d2544e6SMarcin Wojtas 		goto err_buf_info_unmap;
6419b8d05b8SZbigniew Bodek 	}
6429b8d05b8SZbigniew Bodek 
6439b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
6449b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
6459b8d05b8SZbigniew Bodek 
6469b8d05b8SZbigniew Bodek 	return (0);
6479b8d05b8SZbigniew Bodek 
6487d2544e6SMarcin Wojtas err_buf_info_unmap:
6499b8d05b8SZbigniew Bodek 	while (i--) {
6509b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6519b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6529b8d05b8SZbigniew Bodek 	}
653cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6547d2544e6SMarcin Wojtas 	tx_ring->free_tx_ids = NULL;
6557d2544e6SMarcin Wojtas err_buf_info_free:
656cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6577d2544e6SMarcin Wojtas 	tx_ring->tx_buffer_info = NULL;
6587d2544e6SMarcin Wojtas 
6599b8d05b8SZbigniew Bodek 	return (ENOMEM);
6609b8d05b8SZbigniew Bodek }
6619b8d05b8SZbigniew Bodek 
6629b8d05b8SZbigniew Bodek /**
6639b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
6649b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6659b8d05b8SZbigniew Bodek  * @qid: queue index
6669b8d05b8SZbigniew Bodek  *
6679b8d05b8SZbigniew Bodek  * Free all transmit software resources
6689b8d05b8SZbigniew Bodek  **/
6699b8d05b8SZbigniew Bodek static void
6709b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
6719b8d05b8SZbigniew Bodek {
6729b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
6739b8d05b8SZbigniew Bodek 
6749b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
6759b8d05b8SZbigniew Bodek 	    NULL))
6769b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
6779b8d05b8SZbigniew Bodek 
6789b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
6799b8d05b8SZbigniew Bodek 
680b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6819b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
6829b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
6839b8d05b8SZbigniew Bodek 
6849b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
6859b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
686e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag,
687e8073738SMarcin Wojtas 		    tx_ring->tx_buffer_info[i].map, BUS_DMASYNC_POSTWRITE);
6889b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag,
6899b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6909b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6919b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
692e8073738SMarcin Wojtas 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
693e8073738SMarcin Wojtas 		tx_ring->tx_buffer_info[i].mbuf = NULL;
6949b8d05b8SZbigniew Bodek 	}
695416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6969b8d05b8SZbigniew Bodek 
6979b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
698cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6999b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
7009b8d05b8SZbigniew Bodek 
701cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7029b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
7039b8d05b8SZbigniew Bodek }
7049b8d05b8SZbigniew Bodek 
7059b8d05b8SZbigniew Bodek /**
7069b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7079b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7089b8d05b8SZbigniew Bodek  *
7099b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7109b8d05b8SZbigniew Bodek  **/
7119b8d05b8SZbigniew Bodek static int
7129b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7139b8d05b8SZbigniew Bodek {
7149b8d05b8SZbigniew Bodek 	int i, rc;
7159b8d05b8SZbigniew Bodek 
7169b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
7179b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
7180bdffe59SMarcin Wojtas 		if (rc != 0) {
7199b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
7209b8d05b8SZbigniew Bodek 			    "Allocation for Tx Queue %u failed\n", i);
7219b8d05b8SZbigniew Bodek 			goto err_setup_tx;
7229b8d05b8SZbigniew Bodek 		}
7237d2544e6SMarcin Wojtas 	}
7249b8d05b8SZbigniew Bodek 
7259b8d05b8SZbigniew Bodek 	return (0);
7269b8d05b8SZbigniew Bodek 
7279b8d05b8SZbigniew Bodek err_setup_tx:
7289b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
7299b8d05b8SZbigniew Bodek 	while (i--)
7309b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7319b8d05b8SZbigniew Bodek 	return (rc);
7329b8d05b8SZbigniew Bodek }
7339b8d05b8SZbigniew Bodek 
7349b8d05b8SZbigniew Bodek /**
7359b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
7369b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7379b8d05b8SZbigniew Bodek  *
7389b8d05b8SZbigniew Bodek  * Free all transmit software resources
7399b8d05b8SZbigniew Bodek  **/
7409b8d05b8SZbigniew Bodek static void
7419b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
7429b8d05b8SZbigniew Bodek {
7439b8d05b8SZbigniew Bodek 	int i;
7449b8d05b8SZbigniew Bodek 
7459b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
7469b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7479b8d05b8SZbigniew Bodek }
7489b8d05b8SZbigniew Bodek 
74943fefd16SMarcin Wojtas static inline int
75043fefd16SMarcin Wojtas validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
75143fefd16SMarcin Wojtas {
75243fefd16SMarcin Wojtas 	if (likely(req_id < rx_ring->ring_size))
75343fefd16SMarcin Wojtas 		return (0);
75443fefd16SMarcin Wojtas 
75543fefd16SMarcin Wojtas 	device_printf(rx_ring->adapter->pdev, "Invalid rx req_id: %hu\n",
75643fefd16SMarcin Wojtas 	    req_id);
75743fefd16SMarcin Wojtas 	counter_u64_add(rx_ring->rx_stats.bad_req_id, 1);
75843fefd16SMarcin Wojtas 
75943fefd16SMarcin Wojtas 	/* Trigger device reset */
76043fefd16SMarcin Wojtas 	rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
76143fefd16SMarcin Wojtas 	rx_ring->adapter->trigger_reset = true;
76243fefd16SMarcin Wojtas 
76343fefd16SMarcin Wojtas 	return (EFAULT);
76443fefd16SMarcin Wojtas }
76543fefd16SMarcin Wojtas 
7669b8d05b8SZbigniew Bodek /**
7679b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
7689b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7699b8d05b8SZbigniew Bodek  * @qid: queue index
7709b8d05b8SZbigniew Bodek  *
7719b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7729b8d05b8SZbigniew Bodek  **/
7739b8d05b8SZbigniew Bodek static int
7749b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
7759b8d05b8SZbigniew Bodek {
7769b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
7779b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
7789b8d05b8SZbigniew Bodek 	int size, err, i;
7799b8d05b8SZbigniew Bodek 
7809b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
7819b8d05b8SZbigniew Bodek 
7829b8d05b8SZbigniew Bodek 	/*
7839b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
7849b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
7859b8d05b8SZbigniew Bodek 	 */
7869b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
7879b8d05b8SZbigniew Bodek 
788cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
7899b8d05b8SZbigniew Bodek 
79043fefd16SMarcin Wojtas 	size = sizeof(uint16_t) * rx_ring->ring_size;
79143fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
79243fefd16SMarcin Wojtas 
79343fefd16SMarcin Wojtas 	for (i = 0; i < rx_ring->ring_size; i++)
79443fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[i] = i;
79543fefd16SMarcin Wojtas 
7969b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
7979b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
7989b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
7999b8d05b8SZbigniew Bodek 
8009b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
8019b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
8029b8d05b8SZbigniew Bodek 
8039b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
8049b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
8059b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
8069b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
8079b8d05b8SZbigniew Bodek 		if (err != 0) {
8084e8acd84SMarcin Wojtas 			ena_trace(ENA_ALERT,
8099b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8107d2544e6SMarcin Wojtas 			goto err_buf_info_unmap;
8119b8d05b8SZbigniew Bodek 		}
8129b8d05b8SZbigniew Bodek 	}
8139b8d05b8SZbigniew Bodek 
8149b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8150bdffe59SMarcin Wojtas 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
8169b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8170bdffe59SMarcin Wojtas 		if (err != 0) {
8189b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8199b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8209b8d05b8SZbigniew Bodek 		} else {
8219b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8229b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8239b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8249b8d05b8SZbigniew Bodek 		}
8259b8d05b8SZbigniew Bodek 	}
8269b8d05b8SZbigniew Bodek 
827efe6ab18SMarcin Wojtas 	/* Allocate taskqueues */
828efe6ab18SMarcin Wojtas 	TASK_INIT(&rx_ring->cmpl_task, 0, ena_deferred_rx_cleanup, rx_ring);
829efe6ab18SMarcin Wojtas 	rx_ring->cmpl_tq = taskqueue_create_fast("ena RX completion", M_WAITOK,
830efe6ab18SMarcin Wojtas 	    taskqueue_thread_enqueue, &rx_ring->cmpl_tq);
831efe6ab18SMarcin Wojtas 
832efe6ab18SMarcin Wojtas 	taskqueue_start_threads(&rx_ring->cmpl_tq, 1, PI_NET,
833efe6ab18SMarcin Wojtas 	    "%s rx_ring cmpl %d", device_get_nameunit(adapter->pdev), que->cpu);
834efe6ab18SMarcin Wojtas 
8359b8d05b8SZbigniew Bodek 	return (0);
8369b8d05b8SZbigniew Bodek 
8377d2544e6SMarcin Wojtas err_buf_info_unmap:
8389b8d05b8SZbigniew Bodek 	while (i--) {
8399b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8409b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8419b8d05b8SZbigniew Bodek 	}
8429b8d05b8SZbigniew Bodek 
84343fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
84443fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
845cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8469b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8479b8d05b8SZbigniew Bodek 	return (ENOMEM);
8489b8d05b8SZbigniew Bodek }
8499b8d05b8SZbigniew Bodek 
8509b8d05b8SZbigniew Bodek /**
8519b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
8529b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8539b8d05b8SZbigniew Bodek  * @qid: queue index
8549b8d05b8SZbigniew Bodek  *
8559b8d05b8SZbigniew Bodek  * Free all receive software resources
8569b8d05b8SZbigniew Bodek  **/
8579b8d05b8SZbigniew Bodek static void
8589b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8599b8d05b8SZbigniew Bodek {
8609b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
8619b8d05b8SZbigniew Bodek 
862efe6ab18SMarcin Wojtas 	while (taskqueue_cancel(rx_ring->cmpl_tq, &rx_ring->cmpl_task, NULL) != 0)
863efe6ab18SMarcin Wojtas 		taskqueue_drain(rx_ring->cmpl_tq, &rx_ring->cmpl_task);
864efe6ab18SMarcin Wojtas 
865efe6ab18SMarcin Wojtas 	taskqueue_free(rx_ring->cmpl_tq);
866efe6ab18SMarcin Wojtas 
8679b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
8689b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
869e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag,
870e8073738SMarcin Wojtas 		    rx_ring->rx_buffer_info[i].map, BUS_DMASYNC_POSTREAD);
8719b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
8729b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
8739b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
8749b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8759b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8769b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8779b8d05b8SZbigniew Bodek 	}
8789b8d05b8SZbigniew Bodek 
8799b8d05b8SZbigniew Bodek 	/* free LRO resources, */
8809b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
8819b8d05b8SZbigniew Bodek 
8829b8d05b8SZbigniew Bodek 	/* free allocated memory */
883cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8849b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8859b8d05b8SZbigniew Bodek 
88643fefd16SMarcin Wojtas 	free(rx_ring->free_rx_ids, M_DEVBUF);
88743fefd16SMarcin Wojtas 	rx_ring->free_rx_ids = NULL;
8889b8d05b8SZbigniew Bodek }
8899b8d05b8SZbigniew Bodek 
8909b8d05b8SZbigniew Bodek /**
8919b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
8929b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8939b8d05b8SZbigniew Bodek  *
8949b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8959b8d05b8SZbigniew Bodek  **/
8969b8d05b8SZbigniew Bodek static int
8979b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
8989b8d05b8SZbigniew Bodek {
8999b8d05b8SZbigniew Bodek 	int i, rc = 0;
9009b8d05b8SZbigniew Bodek 
9019b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
9029b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
9030bdffe59SMarcin Wojtas 		if (rc != 0) {
9049b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
9059b8d05b8SZbigniew Bodek 			    "Allocation for Rx Queue %u failed\n", i);
9069b8d05b8SZbigniew Bodek 			goto err_setup_rx;
9079b8d05b8SZbigniew Bodek 		}
9087d2544e6SMarcin Wojtas 	}
9099b8d05b8SZbigniew Bodek 	return (0);
9109b8d05b8SZbigniew Bodek 
9119b8d05b8SZbigniew Bodek err_setup_rx:
9129b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
9139b8d05b8SZbigniew Bodek 	while (i--)
9149b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9159b8d05b8SZbigniew Bodek 	return (rc);
9169b8d05b8SZbigniew Bodek }
9179b8d05b8SZbigniew Bodek 
9189b8d05b8SZbigniew Bodek /**
9199b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
9209b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9219b8d05b8SZbigniew Bodek  *
9229b8d05b8SZbigniew Bodek  * Free all receive software resources
9239b8d05b8SZbigniew Bodek  **/
9249b8d05b8SZbigniew Bodek static void
9259b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9269b8d05b8SZbigniew Bodek {
9279b8d05b8SZbigniew Bodek 	int i;
9289b8d05b8SZbigniew Bodek 
9299b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
9309b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9319b8d05b8SZbigniew Bodek }
9329b8d05b8SZbigniew Bodek 
9339b8d05b8SZbigniew Bodek static inline int
9349b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9359b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9369b8d05b8SZbigniew Bodek {
9379b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9389b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9399b8d05b8SZbigniew Bodek 	int nsegs, error;
9404727bda6SMarcin Wojtas 	int mlen;
9419b8d05b8SZbigniew Bodek 
9429b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
9433f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf != NULL))
9449b8d05b8SZbigniew Bodek 		return (0);
9459b8d05b8SZbigniew Bodek 
9469b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
9479b8d05b8SZbigniew Bodek 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
9489b8d05b8SZbigniew Bodek 
9493f9ed7abSMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
9504727bda6SMarcin Wojtas 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
9514727bda6SMarcin Wojtas 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
9524727bda6SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
9539b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
9549b8d05b8SZbigniew Bodek 			return (ENOMEM);
9559b8d05b8SZbigniew Bodek 		}
9564727bda6SMarcin Wojtas 		mlen = MCLBYTES;
9574727bda6SMarcin Wojtas 	} else {
9584727bda6SMarcin Wojtas 		mlen = MJUM16BYTES;
9594727bda6SMarcin Wojtas 	}
9609b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
9614727bda6SMarcin Wojtas 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
9629b8d05b8SZbigniew Bodek 
9639b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
9649b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9659b8d05b8SZbigniew Bodek 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d",
9669b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
9679b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
9689b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
9693f9ed7abSMarcin Wojtas 	if (unlikely((error != 0) || (nsegs != 1))) {
9704e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
9719b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
9729b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
9739b8d05b8SZbigniew Bodek 		goto exit;
9749b8d05b8SZbigniew Bodek 
9759b8d05b8SZbigniew Bodek 	}
9769b8d05b8SZbigniew Bodek 
9779b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
9789b8d05b8SZbigniew Bodek 
9799b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
9809b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
9814727bda6SMarcin Wojtas 	ena_buf->len = mlen;
9829b8d05b8SZbigniew Bodek 
9839b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9849b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
9859b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
9869b8d05b8SZbigniew Bodek 
9879b8d05b8SZbigniew Bodek 	return (0);
9889b8d05b8SZbigniew Bodek 
9899b8d05b8SZbigniew Bodek exit:
9909b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9919b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9929b8d05b8SZbigniew Bodek 	return (EFAULT);
9939b8d05b8SZbigniew Bodek }
9949b8d05b8SZbigniew Bodek 
9959b8d05b8SZbigniew Bodek static void
9969b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
9979b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
9989b8d05b8SZbigniew Bodek {
9999b8d05b8SZbigniew Bodek 
10004e8acd84SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
10014e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
10029b8d05b8SZbigniew Bodek 		return;
10034e8acd84SMarcin Wojtas 	}
10049b8d05b8SZbigniew Bodek 
1005e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1006e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
10079b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
10089b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
10099b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
10109b8d05b8SZbigniew Bodek }
10119b8d05b8SZbigniew Bodek 
10129b8d05b8SZbigniew Bodek /**
10139b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
10149b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
10159b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
10169b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
10179b8d05b8SZbigniew Bodek  **/
10189b8d05b8SZbigniew Bodek static int
10199b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
10209b8d05b8SZbigniew Bodek {
10219b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
102243fefd16SMarcin Wojtas 	uint16_t next_to_use, req_id;
10239b8d05b8SZbigniew Bodek 	uint32_t i;
10249b8d05b8SZbigniew Bodek 	int rc;
10259b8d05b8SZbigniew Bodek 
10269b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d",
10279b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10289b8d05b8SZbigniew Bodek 
10299b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10309b8d05b8SZbigniew Bodek 
10319b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
103243fefd16SMarcin Wojtas 		struct ena_rx_buffer *rx_info;
103343fefd16SMarcin Wojtas 
10349b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
10359b8d05b8SZbigniew Bodek 		    "RX buffer - next to use: %d", next_to_use);
10369b8d05b8SZbigniew Bodek 
103743fefd16SMarcin Wojtas 		req_id = rx_ring->free_rx_ids[next_to_use];
103843fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
10399b8d05b8SZbigniew Bodek 
10409b8d05b8SZbigniew Bodek 		rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
10413f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
10424e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10434e8acd84SMarcin Wojtas 			    "failed to alloc buffer for rx queue %d\n",
10444e8acd84SMarcin Wojtas 			    rx_ring->qid);
10459b8d05b8SZbigniew Bodek 			break;
10469b8d05b8SZbigniew Bodek 		}
10479b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
104843fefd16SMarcin Wojtas 		    &rx_info->ena_buf, req_id);
10490bdffe59SMarcin Wojtas 		if (unlikely(rc != 0)) {
10504e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING,
10519b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
10529b8d05b8SZbigniew Bodek 			    rx_ring->qid);
10539b8d05b8SZbigniew Bodek 			break;
10549b8d05b8SZbigniew Bodek 		}
10559b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
10569b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
10579b8d05b8SZbigniew Bodek 	}
10589b8d05b8SZbigniew Bodek 
10593f9ed7abSMarcin Wojtas 	if (unlikely(i < num)) {
10609b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
10614e8acd84SMarcin Wojtas 		ena_trace(ENA_WARNING,
10624e8acd84SMarcin Wojtas 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
10634e8acd84SMarcin Wojtas 		     rx_ring->qid, i, num);
10649b8d05b8SZbigniew Bodek 	}
10659b8d05b8SZbigniew Bodek 
10663f9ed7abSMarcin Wojtas 	if (likely(i != 0)) {
10679b8d05b8SZbigniew Bodek 		wmb();
10689b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
10699b8d05b8SZbigniew Bodek 	}
10709b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
10719b8d05b8SZbigniew Bodek 	return (i);
10729b8d05b8SZbigniew Bodek }
10739b8d05b8SZbigniew Bodek 
10749b8d05b8SZbigniew Bodek static void
10759b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
10769b8d05b8SZbigniew Bodek {
10779b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
10789b8d05b8SZbigniew Bodek 	unsigned int i;
10799b8d05b8SZbigniew Bodek 
10809b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
10819b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
10829b8d05b8SZbigniew Bodek 
10830bdffe59SMarcin Wojtas 		if (rx_info->mbuf != NULL)
10849b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
10859b8d05b8SZbigniew Bodek 	}
10869b8d05b8SZbigniew Bodek }
10879b8d05b8SZbigniew Bodek 
10889b8d05b8SZbigniew Bodek /**
10899b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
10909b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
10919b8d05b8SZbigniew Bodek  *
10929b8d05b8SZbigniew Bodek  */
10939b8d05b8SZbigniew Bodek static void
10949b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
10959b8d05b8SZbigniew Bodek {
10969b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
10979b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
10989b8d05b8SZbigniew Bodek 
10999b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11009b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
11019b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
11029b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
11039b8d05b8SZbigniew Bodek 
11049b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
11054e8acd84SMarcin Wojtas 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
11064e8acd84SMarcin Wojtas 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
11079b8d05b8SZbigniew Bodek 	}
11089b8d05b8SZbigniew Bodek }
11099b8d05b8SZbigniew Bodek 
11109b8d05b8SZbigniew Bodek static void
11119b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
11129b8d05b8SZbigniew Bodek {
11139b8d05b8SZbigniew Bodek 	int i;
11149b8d05b8SZbigniew Bodek 
11159b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
11169b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
11179b8d05b8SZbigniew Bodek }
11189b8d05b8SZbigniew Bodek 
11199b8d05b8SZbigniew Bodek /**
11209b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
11219b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
11229b8d05b8SZbigniew Bodek  * @qid: queue index
11239b8d05b8SZbigniew Bodek  **/
11249b8d05b8SZbigniew Bodek static void
11259b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
11269b8d05b8SZbigniew Bodek {
11274e8acd84SMarcin Wojtas 	bool print_once = true;
11289b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
11299b8d05b8SZbigniew Bodek 
1130416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
11319b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
11329b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
11339b8d05b8SZbigniew Bodek 
11349b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
11359b8d05b8SZbigniew Bodek 			continue;
11369b8d05b8SZbigniew Bodek 
11374e8acd84SMarcin Wojtas 		if (print_once) {
11384e8acd84SMarcin Wojtas 			device_printf(adapter->pdev,
11394e8acd84SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x",
11404e8acd84SMarcin Wojtas 			    qid, i);
11414e8acd84SMarcin Wojtas 			print_once = false;
11424e8acd84SMarcin Wojtas 		} else {
11434e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG,
11444e8acd84SMarcin Wojtas 			    "free uncompleted tx mbuf qid %d idx 0x%x",
11454e8acd84SMarcin Wojtas 			     qid, i);
11464e8acd84SMarcin Wojtas 		}
11479b8d05b8SZbigniew Bodek 
1148e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map,
1149e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTWRITE);
11509b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
11519b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
11529b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
11539b8d05b8SZbigniew Bodek 	}
1154416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
11559b8d05b8SZbigniew Bodek }
11569b8d05b8SZbigniew Bodek 
11579b8d05b8SZbigniew Bodek static void
11589b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
11599b8d05b8SZbigniew Bodek {
11609b8d05b8SZbigniew Bodek 
11619b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++)
11629b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
11639b8d05b8SZbigniew Bodek }
11649b8d05b8SZbigniew Bodek 
11659b8d05b8SZbigniew Bodek static void
11669b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
11679b8d05b8SZbigniew Bodek {
11689b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11699b8d05b8SZbigniew Bodek 	int i;
11709b8d05b8SZbigniew Bodek 
11719b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11729b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11739b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11749b8d05b8SZbigniew Bodek 	}
11759b8d05b8SZbigniew Bodek }
11769b8d05b8SZbigniew Bodek 
11779b8d05b8SZbigniew Bodek static void
11789b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
11799b8d05b8SZbigniew Bodek {
11809b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11819b8d05b8SZbigniew Bodek 	int i;
11829b8d05b8SZbigniew Bodek 
11839b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11849b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
11859b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11869b8d05b8SZbigniew Bodek 	}
11879b8d05b8SZbigniew Bodek }
11889b8d05b8SZbigniew Bodek 
11899b8d05b8SZbigniew Bodek static void
11909b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
11919b8d05b8SZbigniew Bodek {
11929b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
11939b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
11949b8d05b8SZbigniew Bodek }
11959b8d05b8SZbigniew Bodek 
1196fceb9387SMarcin Wojtas static inline int
11979b8d05b8SZbigniew Bodek validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
11989b8d05b8SZbigniew Bodek {
11994e8acd84SMarcin Wojtas 	struct ena_adapter *adapter = tx_ring->adapter;
12009b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info = NULL;
12019b8d05b8SZbigniew Bodek 
12029b8d05b8SZbigniew Bodek 	if (likely(req_id < tx_ring->ring_size)) {
12039b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
12040bdffe59SMarcin Wojtas 		if (tx_info->mbuf != NULL)
12050bdffe59SMarcin Wojtas 			return (0);
12064e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
12074e8acd84SMarcin Wojtas 		    "tx_info doesn't have valid mbuf\n");
1208*4e306999SMarcin Wojtas 	}
12094e8acd84SMarcin Wojtas 
1210*4e306999SMarcin Wojtas 	device_printf(adapter->pdev, "Invalid req_id: %hu\n", req_id);
12119b8d05b8SZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
12129b8d05b8SZbigniew Bodek 
12139b8d05b8SZbigniew Bodek 	return (EFAULT);
12149b8d05b8SZbigniew Bodek }
12159b8d05b8SZbigniew Bodek 
12169b8d05b8SZbigniew Bodek static int
12179b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
12189b8d05b8SZbigniew Bodek {
12199b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
12209b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
12219b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
12229b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12239b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
12249b8d05b8SZbigniew Bodek 	int rc, i;
12259b8d05b8SZbigniew Bodek 
12269b8d05b8SZbigniew Bodek 	/* Create TX queues */
12279b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12289b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12299b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
12309b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
12319b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
12329b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
12339b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12349b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12359b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12360bdffe59SMarcin Wojtas 		if (rc != 0) {
12379b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12389b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
12399b8d05b8SZbigniew Bodek 			goto err_tx;
12409b8d05b8SZbigniew Bodek 		}
12419b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
12429b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12439b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12449b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12450bdffe59SMarcin Wojtas 		if (rc != 0) {
12469b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12479b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
12489b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12499b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12509b8d05b8SZbigniew Bodek 			goto err_tx;
12519b8d05b8SZbigniew Bodek 		}
12529b8d05b8SZbigniew Bodek 	}
12539b8d05b8SZbigniew Bodek 
12549b8d05b8SZbigniew Bodek 	/* Create RX queues */
12559b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12569b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12579b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12589b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
12599b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
12609b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
12619b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12629b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12639b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12643f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12659b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12669b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
12679b8d05b8SZbigniew Bodek 			goto err_rx;
12689b8d05b8SZbigniew Bodek 		}
12699b8d05b8SZbigniew Bodek 
12709b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
12719b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12729b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12739b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12743f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
12759b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12769b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
12779b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12789b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12799b8d05b8SZbigniew Bodek 			goto err_rx;
12809b8d05b8SZbigniew Bodek 		}
12819b8d05b8SZbigniew Bodek 	}
12829b8d05b8SZbigniew Bodek 
12839b8d05b8SZbigniew Bodek 	return (0);
12849b8d05b8SZbigniew Bodek 
12859b8d05b8SZbigniew Bodek err_rx:
12869b8d05b8SZbigniew Bodek 	while (i--)
12879b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
12889b8d05b8SZbigniew Bodek 	i = adapter->num_queues;
12899b8d05b8SZbigniew Bodek err_tx:
12909b8d05b8SZbigniew Bodek 	while (i--)
12919b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
12929b8d05b8SZbigniew Bodek 
12939b8d05b8SZbigniew Bodek 	return (ENXIO);
12949b8d05b8SZbigniew Bodek }
12959b8d05b8SZbigniew Bodek 
12969b8d05b8SZbigniew Bodek /**
12979b8d05b8SZbigniew Bodek  * ena_tx_cleanup - clear sent packets and corresponding descriptors
12989b8d05b8SZbigniew Bodek  * @tx_ring: ring for which we want to clean packets
12999b8d05b8SZbigniew Bodek  *
13009b8d05b8SZbigniew Bodek  * Once packets are sent, we ask the device in a loop for no longer used
13019b8d05b8SZbigniew Bodek  * descriptors. We find the related mbuf chain in a map (index in an array)
13029b8d05b8SZbigniew Bodek  * and free it, then update ring state.
13039b8d05b8SZbigniew Bodek  * This is performed in "endless" loop, updating ring pointers every
13049b8d05b8SZbigniew Bodek  * TX_COMMIT. The first check of free descriptor is performed before the actual
13059b8d05b8SZbigniew Bodek  * loop, then repeated at the loop end.
13069b8d05b8SZbigniew Bodek  **/
13079b8d05b8SZbigniew Bodek static int
13089b8d05b8SZbigniew Bodek ena_tx_cleanup(struct ena_ring *tx_ring)
13099b8d05b8SZbigniew Bodek {
13109b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
13119b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
13129b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
13139b8d05b8SZbigniew Bodek 	uint16_t req_id;
13149b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
13159b8d05b8SZbigniew Bodek 	unsigned int total_done = 0;
13169b8d05b8SZbigniew Bodek 	int rc;
13179b8d05b8SZbigniew Bodek 	int commit = TX_COMMIT;
13189b8d05b8SZbigniew Bodek 	int budget = TX_BUDGET;
13199b8d05b8SZbigniew Bodek 	int work_done;
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 
13889b8d05b8SZbigniew Bodek 	taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
13899b8d05b8SZbigniew Bodek 
13909b8d05b8SZbigniew Bodek 	return (work_done);
13919b8d05b8SZbigniew Bodek }
13929b8d05b8SZbigniew Bodek 
13939b8d05b8SZbigniew Bodek static void
13949b8d05b8SZbigniew Bodek ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
13959b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
13969b8d05b8SZbigniew Bodek {
13979b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
13989b8d05b8SZbigniew Bodek 
13993f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support)) {
14009b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
14019b8d05b8SZbigniew Bodek 
14029b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->frag &&
1403bfea0e93SMarcin Wojtas 		    (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
14049b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
14059b8d05b8SZbigniew Bodek 			return;
14069b8d05b8SZbigniew Bodek 		}
14079b8d05b8SZbigniew Bodek 
14089b8d05b8SZbigniew Bodek 		switch (ena_rx_ctx->l3_proto) {
14099b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV4:
14109b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
14119b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14129b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
14139b8d05b8SZbigniew Bodek 				break;
14149b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14159b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
14169b8d05b8SZbigniew Bodek 				break;
14179b8d05b8SZbigniew Bodek 			default:
14189b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
14199b8d05b8SZbigniew Bodek 			}
14209b8d05b8SZbigniew Bodek 			break;
14219b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV6:
14229b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
14239b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
14249b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
14259b8d05b8SZbigniew Bodek 				break;
14269b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
14279b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
14289b8d05b8SZbigniew Bodek 				break;
14299b8d05b8SZbigniew Bodek 			default:
14309b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
14319b8d05b8SZbigniew Bodek 			}
14329b8d05b8SZbigniew Bodek 			break;
14339b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
14349b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
14359b8d05b8SZbigniew Bodek 			break;
14369b8d05b8SZbigniew Bodek 		default:
14379b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
14389b8d05b8SZbigniew Bodek 		}
14399b8d05b8SZbigniew Bodek 	} else {
14409b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = rx_ring->qid;
14419b8d05b8SZbigniew Bodek 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
14429b8d05b8SZbigniew Bodek 	}
14439b8d05b8SZbigniew Bodek }
14449b8d05b8SZbigniew Bodek 
14459b8d05b8SZbigniew Bodek /**
14469b8d05b8SZbigniew Bodek  * ena_rx_mbuf - assemble mbuf from descriptors
14479b8d05b8SZbigniew Bodek  * @rx_ring: ring for which we want to clean packets
14489b8d05b8SZbigniew Bodek  * @ena_bufs: buffer info
14499b8d05b8SZbigniew Bodek  * @ena_rx_ctx: metadata for this packet(s)
145043fefd16SMarcin Wojtas  * @next_to_clean: ring pointer, will be updated only upon success
14519b8d05b8SZbigniew Bodek  *
14529b8d05b8SZbigniew Bodek  **/
14539b8d05b8SZbigniew Bodek static struct mbuf*
14549b8d05b8SZbigniew Bodek ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
14559b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
14569b8d05b8SZbigniew Bodek {
14579b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
14589b8d05b8SZbigniew Bodek 	struct ena_rx_buffer *rx_info;
14599b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
14609b8d05b8SZbigniew Bodek 	unsigned int descs = ena_rx_ctx->descs;
1461c51a229cSMarcin Wojtas 	int rc;
146243fefd16SMarcin Wojtas 	uint16_t ntc, len, req_id, buf = 0;
14639b8d05b8SZbigniew Bodek 
146443fefd16SMarcin Wojtas 	ntc = *next_to_clean;
14659b8d05b8SZbigniew Bodek 	adapter = rx_ring->adapter;
14669b8d05b8SZbigniew Bodek 
146743fefd16SMarcin Wojtas 	len = ena_bufs[buf].len;
146843fefd16SMarcin Wojtas 	req_id = ena_bufs[buf].req_id;
1469c51a229cSMarcin Wojtas 	rc = validate_rx_req_id(rx_ring, req_id);
1470c51a229cSMarcin Wojtas 	if (unlikely(rc != 0))
1471c51a229cSMarcin Wojtas 		return (NULL);
1472c51a229cSMarcin Wojtas 
147343fefd16SMarcin Wojtas 	rx_info = &rx_ring->rx_buffer_info[req_id];
14741d65b4c0SMarcin Wojtas 	if (unlikely(rx_info->mbuf == NULL)) {
14751d65b4c0SMarcin Wojtas 		device_printf(adapter->pdev, "NULL mbuf in rx_info");
14761d65b4c0SMarcin Wojtas 		return (NULL);
14771d65b4c0SMarcin Wojtas 	}
147843fefd16SMarcin Wojtas 
14799b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
14809b8d05b8SZbigniew Bodek 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
14819b8d05b8SZbigniew Bodek 
1482e8073738SMarcin Wojtas 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1483e8073738SMarcin Wojtas 	    BUS_DMASYNC_POSTREAD);
14849b8d05b8SZbigniew Bodek 	mbuf = rx_info->mbuf;
14859b8d05b8SZbigniew Bodek 	mbuf->m_flags |= M_PKTHDR;
14869b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.len = len;
14879b8d05b8SZbigniew Bodek 	mbuf->m_len = len;
14889b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.rcvif = rx_ring->que->adapter->ifp;
14899b8d05b8SZbigniew Bodek 
14909b8d05b8SZbigniew Bodek 	/* Fill mbuf with hash key and it's interpretation for optimization */
14919b8d05b8SZbigniew Bodek 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
14929b8d05b8SZbigniew Bodek 
14939b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
14949b8d05b8SZbigniew Bodek 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
14959b8d05b8SZbigniew Bodek 
14969b8d05b8SZbigniew Bodek 	/* DMA address is not needed anymore, unmap it */
14979b8d05b8SZbigniew Bodek 	bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
14989b8d05b8SZbigniew Bodek 
14999b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
150043fefd16SMarcin Wojtas 	rx_ring->free_rx_ids[ntc] = req_id;
150143fefd16SMarcin Wojtas 	ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
15029b8d05b8SZbigniew Bodek 
15039b8d05b8SZbigniew Bodek 	/*
15049b8d05b8SZbigniew Bodek 	 * While we have more than 1 descriptors for one rcvd packet, append
15059b8d05b8SZbigniew Bodek 	 * other mbufs to the main one
15069b8d05b8SZbigniew Bodek 	 */
15079b8d05b8SZbigniew Bodek 	while (--descs) {
150843fefd16SMarcin Wojtas 		++buf;
150943fefd16SMarcin Wojtas 		len = ena_bufs[buf].len;
151043fefd16SMarcin Wojtas 		req_id = ena_bufs[buf].req_id;
1511c51a229cSMarcin Wojtas 		rc = validate_rx_req_id(rx_ring, req_id);
1512c51a229cSMarcin Wojtas 		if (unlikely(rc != 0)) {
1513c51a229cSMarcin Wojtas 			/*
1514c51a229cSMarcin Wojtas 			 * If the req_id is invalid, then the device will be
1515c51a229cSMarcin Wojtas 			 * reset. In that case we must free all mbufs that
1516c51a229cSMarcin Wojtas 			 * were already gathered.
1517c51a229cSMarcin Wojtas 			 */
1518c51a229cSMarcin Wojtas 			m_freem(mbuf);
1519c51a229cSMarcin Wojtas 			return (NULL);
1520c51a229cSMarcin Wojtas 		}
152143fefd16SMarcin Wojtas 		rx_info = &rx_ring->rx_buffer_info[req_id];
15229b8d05b8SZbigniew Bodek 
152343fefd16SMarcin Wojtas 		if (unlikely(rx_info->mbuf == NULL)) {
152443fefd16SMarcin Wojtas 			device_printf(adapter->pdev, "NULL mbuf in rx_info");
152543fefd16SMarcin Wojtas 			/*
152643fefd16SMarcin Wojtas 			 * If one of the required mbufs was not allocated yet,
152743fefd16SMarcin Wojtas 			 * we can break there.
152843fefd16SMarcin Wojtas 			 * All earlier used descriptors will be reallocated
152943fefd16SMarcin Wojtas 			 * later and not used mbufs can be reused.
153043fefd16SMarcin Wojtas 			 * The next_to_clean pointer will not be updated in case
153143fefd16SMarcin Wojtas 			 * of an error, so caller should advance it manually
153243fefd16SMarcin Wojtas 			 * in error handling routine to keep it up to date
153343fefd16SMarcin Wojtas 			 * with hw ring.
153443fefd16SMarcin Wojtas 			 */
153543fefd16SMarcin Wojtas 			m_freem(mbuf);
153643fefd16SMarcin Wojtas 			return (NULL);
153743fefd16SMarcin Wojtas 		}
153843fefd16SMarcin Wojtas 
1539e8073738SMarcin Wojtas 		bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map,
1540e8073738SMarcin Wojtas 		    BUS_DMASYNC_POSTREAD);
15413f9ed7abSMarcin Wojtas 		if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
15429b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
15439b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
15449b8d05b8SZbigniew Bodek 			    mbuf);
15459b8d05b8SZbigniew Bodek 		}
15464e8acd84SMarcin Wojtas 
15474e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH,
15484e8acd84SMarcin Wojtas 		    "rx mbuf updated. len %d", mbuf->m_pkthdr.len);
15494e8acd84SMarcin Wojtas 
15509b8d05b8SZbigniew Bodek 		/* Free already appended mbuf, it won't be useful anymore */
15519b8d05b8SZbigniew Bodek 		bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
15529b8d05b8SZbigniew Bodek 		m_freem(rx_info->mbuf);
15539b8d05b8SZbigniew Bodek 		rx_info->mbuf = NULL;
15549b8d05b8SZbigniew Bodek 
155543fefd16SMarcin Wojtas 		rx_ring->free_rx_ids[ntc] = req_id;
155643fefd16SMarcin Wojtas 		ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
15579b8d05b8SZbigniew Bodek 	}
15589b8d05b8SZbigniew Bodek 
155943fefd16SMarcin Wojtas 	*next_to_clean = ntc;
156043fefd16SMarcin Wojtas 
15619b8d05b8SZbigniew Bodek 	return (mbuf);
15629b8d05b8SZbigniew Bodek }
15639b8d05b8SZbigniew Bodek 
15649b8d05b8SZbigniew Bodek /**
15659b8d05b8SZbigniew Bodek  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
15669b8d05b8SZbigniew Bodek  **/
15679b8d05b8SZbigniew Bodek static inline void
15689b8d05b8SZbigniew Bodek ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
15699b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
15709b8d05b8SZbigniew Bodek {
15719b8d05b8SZbigniew Bodek 
15729b8d05b8SZbigniew Bodek 	/* if IP and error */
15733f9ed7abSMarcin Wojtas 	if (unlikely((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
15743f9ed7abSMarcin Wojtas 	    ena_rx_ctx->l3_csum_err)) {
15759b8d05b8SZbigniew Bodek 		/* ipv4 checksum error */
15769b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.csum_flags = 0;
15779b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
15784e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG, "RX IPv4 header checksum error");
15799b8d05b8SZbigniew Bodek 		return;
15809b8d05b8SZbigniew Bodek 	}
15819b8d05b8SZbigniew Bodek 
15829b8d05b8SZbigniew Bodek 	/* if TCP/UDP */
15839b8d05b8SZbigniew Bodek 	if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
15849b8d05b8SZbigniew Bodek 	    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
15859b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->l4_csum_err) {
15869b8d05b8SZbigniew Bodek 			/* TCP/UDP checksum error */
15879b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = 0;
15889b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
15894e8acd84SMarcin Wojtas 			ena_trace(ENA_DBG, "RX L4 checksum error");
15909b8d05b8SZbigniew Bodek 		} else {
15919b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
15929b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
15939b8d05b8SZbigniew Bodek 		}
15949b8d05b8SZbigniew Bodek 	}
15959b8d05b8SZbigniew Bodek }
15969b8d05b8SZbigniew Bodek 
1597efe6ab18SMarcin Wojtas static void
1598efe6ab18SMarcin Wojtas ena_deferred_rx_cleanup(void *arg, int pending)
1599efe6ab18SMarcin Wojtas {
1600efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring = arg;
1601efe6ab18SMarcin Wojtas 	int budget = CLEAN_BUDGET;
1602efe6ab18SMarcin Wojtas 
1603efe6ab18SMarcin Wojtas 	ENA_RING_MTX_LOCK(rx_ring);
1604efe6ab18SMarcin Wojtas 	/*
1605efe6ab18SMarcin Wojtas 	 * If deferred task was executed, perform cleanup of all awaiting
1606efe6ab18SMarcin Wojtas 	 * descs (or until given budget is depleted to avoid infinite loop).
1607efe6ab18SMarcin Wojtas 	 */
16083f9ed7abSMarcin Wojtas 	while (likely(budget--)) {
1609efe6ab18SMarcin Wojtas 		if (ena_rx_cleanup(rx_ring) == 0)
1610efe6ab18SMarcin Wojtas 			break;
1611efe6ab18SMarcin Wojtas 	}
1612efe6ab18SMarcin Wojtas 	ENA_RING_MTX_UNLOCK(rx_ring);
1613efe6ab18SMarcin Wojtas }
1614efe6ab18SMarcin Wojtas 
16159b8d05b8SZbigniew Bodek /**
16169b8d05b8SZbigniew Bodek  * ena_rx_cleanup - handle rx irq
16179b8d05b8SZbigniew Bodek  * @arg: ring for which irq is being handled
16189b8d05b8SZbigniew Bodek  **/
16199b8d05b8SZbigniew Bodek static int
16209b8d05b8SZbigniew Bodek ena_rx_cleanup(struct ena_ring *rx_ring)
16219b8d05b8SZbigniew Bodek {
16229b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
16239b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
16249b8d05b8SZbigniew Bodek 	struct ena_com_rx_ctx ena_rx_ctx;
16259b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
16269b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
16279b8d05b8SZbigniew Bodek 	if_t ifp;
16289b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
16299b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
16309b8d05b8SZbigniew Bodek 	uint32_t refill_required;
16319b8d05b8SZbigniew Bodek 	uint32_t refill_threshold;
16329b8d05b8SZbigniew Bodek 	uint32_t do_if_input = 0;
16339b8d05b8SZbigniew Bodek 	unsigned int qid;
163443fefd16SMarcin Wojtas 	int rc, i;
16359b8d05b8SZbigniew Bodek 	int budget = RX_BUDGET;
16369b8d05b8SZbigniew Bodek 
16379b8d05b8SZbigniew Bodek 	adapter = rx_ring->que->adapter;
16389b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
16399b8d05b8SZbigniew Bodek 	qid = rx_ring->que->id;
16409b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_RXQ_IDX(qid);
16419b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
16429b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
16439b8d05b8SZbigniew Bodek 	next_to_clean = rx_ring->next_to_clean;
16449b8d05b8SZbigniew Bodek 
16454e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG, "rx: qid %d", qid);
16464e8acd84SMarcin Wojtas 
16479b8d05b8SZbigniew Bodek 	do {
16489b8d05b8SZbigniew Bodek 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
16499b8d05b8SZbigniew Bodek 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
16509b8d05b8SZbigniew Bodek 		ena_rx_ctx.descs = 0;
1651e8073738SMarcin Wojtas 		bus_dmamap_sync(io_cq->cdesc_addr.mem_handle.tag,
1652e8073738SMarcin Wojtas 		    io_cq->cdesc_addr.mem_handle.map, BUS_DMASYNC_POSTREAD);
16539b8d05b8SZbigniew Bodek 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
16549b8d05b8SZbigniew Bodek 
16550bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
16569b8d05b8SZbigniew Bodek 			goto error;
16579b8d05b8SZbigniew Bodek 
16589b8d05b8SZbigniew Bodek 		if (unlikely(ena_rx_ctx.descs == 0))
16599b8d05b8SZbigniew Bodek 			break;
16609b8d05b8SZbigniew Bodek 
16614e8acd84SMarcin Wojtas 		ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
16624e8acd84SMarcin Wojtas 		    "descs #: %d l3 proto %d l4 proto %d hash: %x",
16634e8acd84SMarcin Wojtas 		    rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
16644e8acd84SMarcin Wojtas 		    ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
16654e8acd84SMarcin Wojtas 
16669b8d05b8SZbigniew Bodek 		/* Receive mbuf from the ring */
16679b8d05b8SZbigniew Bodek 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
16689b8d05b8SZbigniew Bodek 		    &ena_rx_ctx, &next_to_clean);
1669e8073738SMarcin Wojtas 		bus_dmamap_sync(io_cq->cdesc_addr.mem_handle.tag,
1670e8073738SMarcin Wojtas 		    io_cq->cdesc_addr.mem_handle.map, BUS_DMASYNC_PREREAD);
16719b8d05b8SZbigniew Bodek 		/* Exit if we failed to retrieve a buffer */
16720bdffe59SMarcin Wojtas 		if (unlikely(mbuf == NULL)) {
167343fefd16SMarcin Wojtas 			for (i = 0; i < ena_rx_ctx.descs; ++i) {
167443fefd16SMarcin Wojtas 				rx_ring->free_rx_ids[next_to_clean] =
167543fefd16SMarcin Wojtas 				    rx_ring->ena_bufs[i].req_id;
167643fefd16SMarcin Wojtas 				next_to_clean =
167743fefd16SMarcin Wojtas 				    ENA_RX_RING_IDX_NEXT(next_to_clean,
167843fefd16SMarcin Wojtas 				    rx_ring->ring_size);
167943fefd16SMarcin Wojtas 
168043fefd16SMarcin Wojtas 			}
16819b8d05b8SZbigniew Bodek 			break;
16829b8d05b8SZbigniew Bodek 		}
16839b8d05b8SZbigniew Bodek 
16840bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_RXCSUM) != 0) ||
16850bdffe59SMarcin Wojtas 		    ((ifp->if_capenable & IFCAP_RXCSUM_IPV6) != 0)) {
16869b8d05b8SZbigniew Bodek 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
16879b8d05b8SZbigniew Bodek 		}
16889b8d05b8SZbigniew Bodek 
168930217e2dSMarcin Wojtas 		counter_enter();
169030217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
169130217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
169230217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
169330217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
169430217e2dSMarcin Wojtas 		counter_exit();
16959b8d05b8SZbigniew Bodek 		/*
16969b8d05b8SZbigniew Bodek 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
16979b8d05b8SZbigniew Bodek 		 * should be computed by hardware.
16989b8d05b8SZbigniew Bodek 		 */
16999b8d05b8SZbigniew Bodek 		do_if_input = 1;
17000bdffe59SMarcin Wojtas 		if (((ifp->if_capenable & IFCAP_LRO) != 0)  &&
17010bdffe59SMarcin Wojtas 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
17020bdffe59SMarcin Wojtas 		    (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
17039b8d05b8SZbigniew Bodek 			/*
17049b8d05b8SZbigniew Bodek 			 * Send to the stack if:
17059b8d05b8SZbigniew Bodek 			 *  - LRO not enabled, or
17069b8d05b8SZbigniew Bodek 			 *  - no LRO resources, or
17079b8d05b8SZbigniew Bodek 			 *  - lro enqueue fails
17089b8d05b8SZbigniew Bodek 			 */
17090bdffe59SMarcin Wojtas 			if ((rx_ring->lro.lro_cnt != 0) &&
17100bdffe59SMarcin Wojtas 			    (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
17119b8d05b8SZbigniew Bodek 					do_if_input = 0;
17129b8d05b8SZbigniew Bodek 		}
17130bdffe59SMarcin Wojtas 		if (do_if_input != 0) {
17140bdffe59SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_RXPTH,
17150bdffe59SMarcin Wojtas 			    "calling if_input() with mbuf %p", mbuf);
17169b8d05b8SZbigniew Bodek 			(*ifp->if_input)(ifp, mbuf);
17179b8d05b8SZbigniew Bodek 		}
17189b8d05b8SZbigniew Bodek 
171930217e2dSMarcin Wojtas 		counter_enter();
172030217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
172130217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
172230217e2dSMarcin Wojtas 		counter_exit();
17239b8d05b8SZbigniew Bodek 	} while (--budget);
17249b8d05b8SZbigniew Bodek 
17259b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = next_to_clean;
17269b8d05b8SZbigniew Bodek 
1727a195fab0SMarcin Wojtas 	refill_required = ena_com_free_desc(io_sq);
17285a990212SMarcin Wojtas 	refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
17299b8d05b8SZbigniew Bodek 
17309b8d05b8SZbigniew Bodek 	if (refill_required > refill_threshold) {
17319b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
17329b8d05b8SZbigniew Bodek 		ena_refill_rx_bufs(rx_ring, refill_required);
17339b8d05b8SZbigniew Bodek 	}
17349b8d05b8SZbigniew Bodek 
17359b8d05b8SZbigniew Bodek 	tcp_lro_flush_all(&rx_ring->lro);
17369b8d05b8SZbigniew Bodek 
17379b8d05b8SZbigniew Bodek 	return (RX_BUDGET - budget);
17389b8d05b8SZbigniew Bodek 
17399b8d05b8SZbigniew Bodek error:
17409b8d05b8SZbigniew Bodek 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
1741c9b099ecSMarcin Wojtas 
1742c9b099ecSMarcin Wojtas 	/* Too many desc from the device. Trigger reset */
1743c9b099ecSMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_TOO_MANY_RX_DESCS;
1744c9b099ecSMarcin Wojtas 	adapter->trigger_reset = true;
1745c9b099ecSMarcin Wojtas 
1746c9b099ecSMarcin Wojtas 	return (0);
17479b8d05b8SZbigniew Bodek }
17489b8d05b8SZbigniew Bodek 
17499b8d05b8SZbigniew Bodek /*********************************************************************
17509b8d05b8SZbigniew Bodek  *
17519b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
17529b8d05b8SZbigniew Bodek  *
17539b8d05b8SZbigniew Bodek  **********************************************************************/
17549b8d05b8SZbigniew Bodek 
17559b8d05b8SZbigniew Bodek /**
17569b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
17579b8d05b8SZbigniew Bodek  * @arg: interrupt number
17589b8d05b8SZbigniew Bodek  **/
17599b8d05b8SZbigniew Bodek static void
17609b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
17619b8d05b8SZbigniew Bodek {
17629b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
17639b8d05b8SZbigniew Bodek 
17649b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
17659b8d05b8SZbigniew Bodek 	if (likely(adapter->running))
17669b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
17679b8d05b8SZbigniew Bodek }
17689b8d05b8SZbigniew Bodek 
17699b8d05b8SZbigniew Bodek /**
17709b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
17719b8d05b8SZbigniew Bodek  * @arg: interrupt number
17729b8d05b8SZbigniew Bodek  **/
17739b8d05b8SZbigniew Bodek static void
17749b8d05b8SZbigniew Bodek ena_handle_msix(void *arg)
17759b8d05b8SZbigniew Bodek {
17769b8d05b8SZbigniew Bodek 	struct ena_que	*que = arg;
17779b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = que->adapter;
17789b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
17799b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
17809b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
17819b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
17829b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
17839b8d05b8SZbigniew Bodek 	int qid, ena_qid;
17849b8d05b8SZbigniew Bodek 	int txc, rxc, i;
17859b8d05b8SZbigniew Bodek 
17863f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
17879b8d05b8SZbigniew Bodek 		return;
17889b8d05b8SZbigniew Bodek 
17899b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "MSI-X TX/RX routine");
17909b8d05b8SZbigniew Bodek 
17919b8d05b8SZbigniew Bodek 	tx_ring = que->tx_ring;
17929b8d05b8SZbigniew Bodek 	rx_ring = que->rx_ring;
17939b8d05b8SZbigniew Bodek 	qid = que->id;
17949b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(qid);
17959b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
17969b8d05b8SZbigniew Bodek 
1797d12f7bfcSMarcin Wojtas 	tx_ring->first_interrupt = true;
1798d12f7bfcSMarcin Wojtas 	rx_ring->first_interrupt = true;
1799d12f7bfcSMarcin Wojtas 
18009b8d05b8SZbigniew Bodek 	for (i = 0; i < CLEAN_BUDGET; ++i) {
1801efe6ab18SMarcin Wojtas 		/*
1802efe6ab18SMarcin Wojtas 		 * If lock cannot be acquired, then deferred cleanup task was
1803efe6ab18SMarcin Wojtas 		 * being executed and rx ring is being cleaned up in
1804efe6ab18SMarcin Wojtas 		 * another thread.
1805efe6ab18SMarcin Wojtas 		 */
18063f9ed7abSMarcin Wojtas 		if (likely(ENA_RING_MTX_TRYLOCK(rx_ring) != 0)) {
18079b8d05b8SZbigniew Bodek 			rxc = ena_rx_cleanup(rx_ring);
1808efe6ab18SMarcin Wojtas 			ENA_RING_MTX_UNLOCK(rx_ring);
1809efe6ab18SMarcin Wojtas 		} else {
1810efe6ab18SMarcin Wojtas 			rxc = 0;
1811efe6ab18SMarcin Wojtas 		}
18129b8d05b8SZbigniew Bodek 
18139b8d05b8SZbigniew Bodek 		/* Protection from calling ena_tx_cleanup from ena_start_xmit */
18149b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
18159b8d05b8SZbigniew Bodek 		txc = ena_tx_cleanup(tx_ring);
18169b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
18179b8d05b8SZbigniew Bodek 
18183f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0))
18199b8d05b8SZbigniew Bodek 			return;
18209b8d05b8SZbigniew Bodek 
18210bdffe59SMarcin Wojtas 		if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
18229b8d05b8SZbigniew Bodek 		       break;
18239b8d05b8SZbigniew Bodek 	}
18249b8d05b8SZbigniew Bodek 
18259b8d05b8SZbigniew Bodek 	/* Signal that work is done and unmask interrupt */
18269b8d05b8SZbigniew Bodek 	ena_com_update_intr_reg(&intr_reg,
18279b8d05b8SZbigniew Bodek 	    RX_IRQ_INTERVAL,
18289b8d05b8SZbigniew Bodek 	    TX_IRQ_INTERVAL,
18299b8d05b8SZbigniew Bodek 	    true);
18309b8d05b8SZbigniew Bodek 	ena_com_unmask_intr(io_cq, &intr_reg);
18319b8d05b8SZbigniew Bodek }
18329b8d05b8SZbigniew Bodek 
18339b8d05b8SZbigniew Bodek static int
18349b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
18359b8d05b8SZbigniew Bodek {
18369b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
18378805021aSMarcin Wojtas 	int msix_vecs, msix_req;
18388805021aSMarcin Wojtas 	int i, rc = 0;
18399b8d05b8SZbigniew Bodek 
18409b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
18419b8d05b8SZbigniew Bodek 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues);
18429b8d05b8SZbigniew Bodek 
1843cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1844cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1845cd5d5804SMarcin Wojtas 
18464e8acd84SMarcin Wojtas 	ena_trace(ENA_DBG, "trying to enable MSI-X, vectors: %d", msix_vecs);
18479b8d05b8SZbigniew Bodek 
18489b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
18499b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
18509b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
18519b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
18529b8d05b8SZbigniew Bodek 	}
18539b8d05b8SZbigniew Bodek 
18548805021aSMarcin Wojtas 	msix_req = msix_vecs;
18559b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
18563f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
18579b8d05b8SZbigniew Bodek 		device_printf(dev,
18589b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
18597d2544e6SMarcin Wojtas 
18609b8d05b8SZbigniew Bodek 		rc = ENOSPC;
18617d2544e6SMarcin Wojtas 		goto err_msix_free;
18629b8d05b8SZbigniew Bodek 	}
18639b8d05b8SZbigniew Bodek 
18648805021aSMarcin Wojtas 	if (msix_vecs != msix_req) {
18652b5b60feSMarcin Wojtas 		if (msix_vecs == ENA_ADMIN_MSIX_VEC) {
18662b5b60feSMarcin Wojtas 			device_printf(dev,
18672b5b60feSMarcin Wojtas 			    "Not enough number of MSI-x allocated: %d\n",
18682b5b60feSMarcin Wojtas 			    msix_vecs);
18692b5b60feSMarcin Wojtas 			pci_release_msi(dev);
18702b5b60feSMarcin Wojtas 			rc = ENOSPC;
18712b5b60feSMarcin Wojtas 			goto err_msix_free;
18722b5b60feSMarcin Wojtas 		}
18738805021aSMarcin Wojtas 		device_printf(dev, "Enable only %d MSI-x (out of %d), reduce "
18748805021aSMarcin Wojtas 		    "the number of queues\n", msix_vecs, msix_req);
18758805021aSMarcin Wojtas 		adapter->num_queues = msix_vecs - ENA_ADMIN_MSIX_VEC;
18768805021aSMarcin Wojtas 	}
18778805021aSMarcin Wojtas 
18789b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
18799b8d05b8SZbigniew Bodek 	adapter->msix_enabled = true;
18809b8d05b8SZbigniew Bodek 
18817d2544e6SMarcin Wojtas 	return (0);
18827d2544e6SMarcin Wojtas 
18837d2544e6SMarcin Wojtas err_msix_free:
18847d2544e6SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
18857d2544e6SMarcin Wojtas 	adapter->msix_entries = NULL;
18867d2544e6SMarcin Wojtas 
18879b8d05b8SZbigniew Bodek 	return (rc);
18889b8d05b8SZbigniew Bodek }
18899b8d05b8SZbigniew Bodek 
18909b8d05b8SZbigniew Bodek static void
18919b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
18929b8d05b8SZbigniew Bodek {
18939b8d05b8SZbigniew Bodek 
18949b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
18959b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
18969b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
18979b8d05b8SZbigniew Bodek 	/*
18989b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
18999b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
19009b8d05b8SZbigniew Bodek 	 */
19019b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
19029b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
19039b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
19049b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
19059b8d05b8SZbigniew Bodek }
19069b8d05b8SZbigniew Bodek 
19079b8d05b8SZbigniew Bodek static void
19089b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
19099b8d05b8SZbigniew Bodek {
19109b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
19119b8d05b8SZbigniew Bodek 	int irq_idx;
19129b8d05b8SZbigniew Bodek 
19139b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++) {
19149b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
19159b8d05b8SZbigniew Bodek 
19169b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
19179b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
19189b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
19199b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
19209b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
19219b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
19229b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
19239b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
1924277f11c4SMarcin Wojtas 
19259b8d05b8SZbigniew Bodek 		/*
1926277f11c4SMarcin Wojtas 		 * We want to bind rings to the corresponding cpu
19279b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
19289b8d05b8SZbigniew Bodek 		 */
19293f9ed7abSMarcin Wojtas 		if (unlikely(last_bind_cpu < 0))
19309b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
19319b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
19329b8d05b8SZbigniew Bodek 		    last_bind_cpu;
19339b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
19349b8d05b8SZbigniew Bodek 	}
19359b8d05b8SZbigniew Bodek }
19369b8d05b8SZbigniew Bodek 
19379b8d05b8SZbigniew Bodek static int
19389b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
19399b8d05b8SZbigniew Bodek {
19409b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19419b8d05b8SZbigniew Bodek 	unsigned long flags;
19429b8d05b8SZbigniew Bodek 	int rc, rcc;
19439b8d05b8SZbigniew Bodek 
19449b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
19459b8d05b8SZbigniew Bodek 
19469b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
19479b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
19489b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
19499b8d05b8SZbigniew Bodek 
19503f9ed7abSMarcin Wojtas 	if (unlikely(irq->res == NULL)) {
19519b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
19529b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
19537d2544e6SMarcin Wojtas 		return (ENXIO);
19549b8d05b8SZbigniew Bodek 	}
19559b8d05b8SZbigniew Bodek 
19560bdffe59SMarcin Wojtas 	rc = bus_setup_intr(adapter->pdev, irq->res,
19570bdffe59SMarcin Wojtas 	    INTR_TYPE_NET | INTR_MPSAFE, NULL, ena_intr_msix_mgmnt,
19580bdffe59SMarcin Wojtas 	    irq->data, &irq->cookie);
19593f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
19609b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
19619b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
19629b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
19637d2544e6SMarcin Wojtas 		goto err_res_free;
19649b8d05b8SZbigniew Bodek 	}
19659b8d05b8SZbigniew Bodek 	irq->requested = true;
19669b8d05b8SZbigniew Bodek 
19679b8d05b8SZbigniew Bodek 	return (rc);
19689b8d05b8SZbigniew Bodek 
19697d2544e6SMarcin Wojtas err_res_free:
19704e8acd84SMarcin Wojtas 	ena_trace(ENA_INFO | ENA_ADMQ, "releasing resource for irq %d\n",
19717d2544e6SMarcin Wojtas 	    irq->vector);
19729b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
19739b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
19743f9ed7abSMarcin Wojtas 	if (unlikely(rcc != 0))
19759b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
19769b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
19779b8d05b8SZbigniew Bodek 	irq->res = NULL;
19789b8d05b8SZbigniew Bodek 
19799b8d05b8SZbigniew Bodek 	return (rc);
19809b8d05b8SZbigniew Bodek }
19819b8d05b8SZbigniew Bodek 
19829b8d05b8SZbigniew Bodek static int
19839b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
19849b8d05b8SZbigniew Bodek {
19859b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19869b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
19879b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
19889b8d05b8SZbigniew Bodek 
19893f9ed7abSMarcin Wojtas 	if (unlikely(adapter->msix_enabled == 0)) {
19904e8acd84SMarcin Wojtas 		device_printf(adapter->pdev,
19914e8acd84SMarcin Wojtas 		    "failed to request I/O IRQ: MSI-X is not enabled\n");
19929b8d05b8SZbigniew Bodek 		return (EINVAL);
19939b8d05b8SZbigniew Bodek 	} else {
19949b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
19959b8d05b8SZbigniew Bodek 	}
19969b8d05b8SZbigniew Bodek 
19979b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
19989b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
19999b8d05b8SZbigniew Bodek 
20003f9ed7abSMarcin Wojtas 		if (unlikely(irq->requested))
20019b8d05b8SZbigniew Bodek 			continue;
20029b8d05b8SZbigniew Bodek 
20039b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
20049b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
20053f9ed7abSMarcin Wojtas 		if (unlikely(irq->res == NULL)) {
2006469a8407SMarcin Wojtas 			rc = ENOMEM;
20079b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
20089b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
20099b8d05b8SZbigniew Bodek 			goto err;
20109b8d05b8SZbigniew Bodek 		}
20119b8d05b8SZbigniew Bodek 
20120bdffe59SMarcin Wojtas 		rc = bus_setup_intr(adapter->pdev, irq->res,
20130bdffe59SMarcin Wojtas 		    INTR_TYPE_NET | INTR_MPSAFE, NULL,
20140bdffe59SMarcin Wojtas 		    irq->handler, irq->data, &irq->cookie);
20153f9ed7abSMarcin Wojtas 		 if (unlikely(rc != 0)) {
20169b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
20179b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
20189b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
20199b8d05b8SZbigniew Bodek 			goto err;
20209b8d05b8SZbigniew Bodek 		}
20219b8d05b8SZbigniew Bodek 		irq->requested = true;
20229b8d05b8SZbigniew Bodek 
20234e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
20249b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
20259b8d05b8SZbigniew Bodek 	}
20269b8d05b8SZbigniew Bodek 
20279b8d05b8SZbigniew Bodek 	return (rc);
20289b8d05b8SZbigniew Bodek 
20299b8d05b8SZbigniew Bodek err:
20309b8d05b8SZbigniew Bodek 
20319b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
20329b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20339b8d05b8SZbigniew Bodek 		rcc = 0;
20349b8d05b8SZbigniew Bodek 
20359b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
20369b8d05b8SZbigniew Bodek 		   free both intr and resources */
20370bdffe59SMarcin Wojtas 		if (irq->requested)
20389b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
20393f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
20409b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
20419b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
20429b8d05b8SZbigniew Bodek 
20439b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
20449b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
20459b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
20469b8d05b8SZbigniew Bodek 		   this iteration */
20479b8d05b8SZbigniew Bodek 		rcc = 0;
20489b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
20499b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20509b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
20519b8d05b8SZbigniew Bodek 		}
20523f9ed7abSMarcin Wojtas 		if (unlikely(rcc != 0))
20539b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
20549b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
20559b8d05b8SZbigniew Bodek 		irq->requested = false;
20569b8d05b8SZbigniew Bodek 		irq->res = NULL;
20579b8d05b8SZbigniew Bodek 	}
20589b8d05b8SZbigniew Bodek 
20599b8d05b8SZbigniew Bodek 	return (rc);
20609b8d05b8SZbigniew Bodek }
20619b8d05b8SZbigniew Bodek 
20629b8d05b8SZbigniew Bodek static void
20639b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
20649b8d05b8SZbigniew Bodek {
20659b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20669b8d05b8SZbigniew Bodek 	int rc;
20679b8d05b8SZbigniew Bodek 
20689b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
20699b8d05b8SZbigniew Bodek 	if (irq->requested) {
20709b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
20719b8d05b8SZbigniew Bodek 		    irq->vector);
20729b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
20733f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
20749b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
20759b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
20769b8d05b8SZbigniew Bodek 		irq->requested = 0;
20779b8d05b8SZbigniew Bodek 	}
20789b8d05b8SZbigniew Bodek 
20799b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
20809b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
20819b8d05b8SZbigniew Bodek 		    irq->vector);
20829b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
20839b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
20849b8d05b8SZbigniew Bodek 		irq->res = NULL;
20853f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
20869b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
20879b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
20889b8d05b8SZbigniew Bodek 	}
20899b8d05b8SZbigniew Bodek }
20909b8d05b8SZbigniew Bodek 
20919b8d05b8SZbigniew Bodek static void
20929b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
20939b8d05b8SZbigniew Bodek {
20949b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
20959b8d05b8SZbigniew Bodek 	int rc;
20969b8d05b8SZbigniew Bodek 
20979b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
20989b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
20999b8d05b8SZbigniew Bodek 		if (irq->requested) {
21009b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
21019b8d05b8SZbigniew Bodek 			    irq->vector);
21029b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
21039b8d05b8SZbigniew Bodek 			    irq->cookie);
21043f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
21059b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
21069b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
21079b8d05b8SZbigniew Bodek 			}
21089b8d05b8SZbigniew Bodek 			irq->requested = 0;
21099b8d05b8SZbigniew Bodek 		}
21109b8d05b8SZbigniew Bodek 
21119b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
21129b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
21139b8d05b8SZbigniew Bodek 			    irq->vector);
21149b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
21159b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
21169b8d05b8SZbigniew Bodek 			irq->res = NULL;
21173f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
21189b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
21199b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
21209b8d05b8SZbigniew Bodek 				    irq->vector);
21219b8d05b8SZbigniew Bodek 			}
21229b8d05b8SZbigniew Bodek 		}
21239b8d05b8SZbigniew Bodek 	}
21249b8d05b8SZbigniew Bodek }
21259b8d05b8SZbigniew Bodek 
21269b8d05b8SZbigniew Bodek static void
21279b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
21289b8d05b8SZbigniew Bodek {
21299b8d05b8SZbigniew Bodek 
21309b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
21319b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
21329b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
21339b8d05b8SZbigniew Bodek }
21349b8d05b8SZbigniew Bodek 
21359b8d05b8SZbigniew Bodek static void
21369b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
21379b8d05b8SZbigniew Bodek {
21389b8d05b8SZbigniew Bodek 
21399b8d05b8SZbigniew Bodek 	pci_release_msi(adapter->pdev);
21409b8d05b8SZbigniew Bodek 
21419b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
2142cd5d5804SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
21439b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
21449b8d05b8SZbigniew Bodek }
21459b8d05b8SZbigniew Bodek 
21469b8d05b8SZbigniew Bodek static void
21479b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
21489b8d05b8SZbigniew Bodek {
21499b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
21509b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
21519b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
21529b8d05b8SZbigniew Bodek 	int i;
21539b8d05b8SZbigniew Bodek 
21549b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
21559b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
21569b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
21579b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
21589b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
21599b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
21609b8d05b8SZbigniew Bodek 	}
21619b8d05b8SZbigniew Bodek }
21629b8d05b8SZbigniew Bodek 
21639b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
21640bdffe59SMarcin Wojtas static int
21650bdffe59SMarcin Wojtas ena_rss_configure(struct ena_adapter *adapter)
21669b8d05b8SZbigniew Bodek {
21679b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
21689b8d05b8SZbigniew Bodek 	int rc;
21699b8d05b8SZbigniew Bodek 
21709b8d05b8SZbigniew Bodek 	/* Set indirect table */
21719b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
21720bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21730bdffe59SMarcin Wojtas 		return (rc);
21749b8d05b8SZbigniew Bodek 
21759b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
21769b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
21770bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21780bdffe59SMarcin Wojtas 		return (rc);
21799b8d05b8SZbigniew Bodek 
21809b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
21819b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
21820bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
21830bdffe59SMarcin Wojtas 		return (rc);
21849b8d05b8SZbigniew Bodek 
21850bdffe59SMarcin Wojtas 	return (0);
21869b8d05b8SZbigniew Bodek }
21879b8d05b8SZbigniew Bodek 
21889b8d05b8SZbigniew Bodek static int
21899b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
21909b8d05b8SZbigniew Bodek {
21919b8d05b8SZbigniew Bodek 	int rc;
21929b8d05b8SZbigniew Bodek 
21933f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support)) {
21949b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
21950bdffe59SMarcin Wojtas 		if (rc != 0)
21969b8d05b8SZbigniew Bodek 			return (rc);
21979b8d05b8SZbigniew Bodek 	}
21989b8d05b8SZbigniew Bodek 
21997d2544e6SMarcin Wojtas 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
22003f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
22017d2544e6SMarcin Wojtas 		return (rc);
22027d2544e6SMarcin Wojtas 
22039b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
220430217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
220530217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
22069b8d05b8SZbigniew Bodek 
22079b8d05b8SZbigniew Bodek 	return (0);
22089b8d05b8SZbigniew Bodek }
22099b8d05b8SZbigniew Bodek 
22109b8d05b8SZbigniew Bodek static int
22119b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
22129b8d05b8SZbigniew Bodek {
22139b8d05b8SZbigniew Bodek 	int rc = 0;
22149b8d05b8SZbigniew Bodek 
22153f9ed7abSMarcin Wojtas 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
22169b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
22179b8d05b8SZbigniew Bodek 		return (ENXIO);
22189b8d05b8SZbigniew Bodek 	}
22199b8d05b8SZbigniew Bodek 
22203f9ed7abSMarcin Wojtas 	if (unlikely(!adapter->running)) {
22219b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not running!\n");
22229b8d05b8SZbigniew Bodek 		return (ENXIO);
22239b8d05b8SZbigniew Bodek 	}
22249b8d05b8SZbigniew Bodek 
22259b8d05b8SZbigniew Bodek 	if (!adapter->up) {
22269b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going UP\n");
22279b8d05b8SZbigniew Bodek 
22289b8d05b8SZbigniew Bodek 		/* setup interrupts for IO queues */
22299b8d05b8SZbigniew Bodek 		ena_setup_io_intr(adapter);
22309b8d05b8SZbigniew Bodek 		rc = ena_request_io_irq(adapter);
22313f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22329b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_req_irq");
22339b8d05b8SZbigniew Bodek 			goto err_req_irq;
22349b8d05b8SZbigniew Bodek 		}
22359b8d05b8SZbigniew Bodek 
22369b8d05b8SZbigniew Bodek 		/* allocate transmit descriptors */
22379b8d05b8SZbigniew Bodek 		rc = ena_setup_all_tx_resources(adapter);
22383f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22399b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_tx");
22409b8d05b8SZbigniew Bodek 			goto err_setup_tx;
22419b8d05b8SZbigniew Bodek 		}
22429b8d05b8SZbigniew Bodek 
22439b8d05b8SZbigniew Bodek 		/* allocate receive descriptors */
22449b8d05b8SZbigniew Bodek 		rc = ena_setup_all_rx_resources(adapter);
22453f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22469b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_rx");
22479b8d05b8SZbigniew Bodek 			goto err_setup_rx;
22489b8d05b8SZbigniew Bodek 		}
22499b8d05b8SZbigniew Bodek 
22509b8d05b8SZbigniew Bodek 		/* create IO queues for Rx & Tx */
22519b8d05b8SZbigniew Bodek 		rc = ena_create_io_queues(adapter);
22523f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
22539b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT,
22549b8d05b8SZbigniew Bodek 			    "create IO queues failed");
22559b8d05b8SZbigniew Bodek 			goto err_io_que;
22569b8d05b8SZbigniew Bodek 		}
22579b8d05b8SZbigniew Bodek 
22583f9ed7abSMarcin Wojtas 		if (unlikely(adapter->link_status))
22599b8d05b8SZbigniew Bodek 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
22609b8d05b8SZbigniew Bodek 
22619b8d05b8SZbigniew Bodek 		rc = ena_up_complete(adapter);
22623f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0))
22639b8d05b8SZbigniew Bodek 			goto err_up_complete;
22649b8d05b8SZbigniew Bodek 
22659b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_up, 1);
22669b8d05b8SZbigniew Bodek 
22679b8d05b8SZbigniew Bodek 		ena_update_hwassist(adapter);
22689b8d05b8SZbigniew Bodek 
22699b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
22709b8d05b8SZbigniew Bodek 		    IFF_DRV_OACTIVE);
22719b8d05b8SZbigniew Bodek 
22729b8d05b8SZbigniew Bodek 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
22739b8d05b8SZbigniew Bodek 		    ena_timer_service, (void *)adapter, 0);
22749b8d05b8SZbigniew Bodek 
22759b8d05b8SZbigniew Bodek 		adapter->up = true;
227693471047SZbigniew Bodek 
227793471047SZbigniew Bodek 		ena_unmask_all_io_irqs(adapter);
22789b8d05b8SZbigniew Bodek 	}
22799b8d05b8SZbigniew Bodek 
22809b8d05b8SZbigniew Bodek 	return (0);
22819b8d05b8SZbigniew Bodek 
22829b8d05b8SZbigniew Bodek err_up_complete:
22839b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
22849b8d05b8SZbigniew Bodek err_io_que:
22859b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
22869b8d05b8SZbigniew Bodek err_setup_rx:
22879b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
22889b8d05b8SZbigniew Bodek err_setup_tx:
22899b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
22909b8d05b8SZbigniew Bodek err_req_irq:
22919b8d05b8SZbigniew Bodek 	return (rc);
22929b8d05b8SZbigniew Bodek }
22939b8d05b8SZbigniew Bodek 
22949b8d05b8SZbigniew Bodek static uint64_t
22959b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
22969b8d05b8SZbigniew Bodek {
22979b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
22989b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
22999b8d05b8SZbigniew Bodek 
23009b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
23019b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
23029b8d05b8SZbigniew Bodek 
23039b8d05b8SZbigniew Bodek 	switch (cnt) {
23049b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
230530217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
23069b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
230730217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
23089b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
230930217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
23109b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
231130217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
23129b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
231330217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
23149b8d05b8SZbigniew Bodek 	default:
23159b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
23169b8d05b8SZbigniew Bodek 	}
23179b8d05b8SZbigniew Bodek }
23189b8d05b8SZbigniew Bodek 
23199b8d05b8SZbigniew Bodek static int
23209b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
23219b8d05b8SZbigniew Bodek {
23229b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
23239b8d05b8SZbigniew Bodek 	return (0);
23249b8d05b8SZbigniew Bodek }
23259b8d05b8SZbigniew Bodek 
23269b8d05b8SZbigniew Bodek static void
23279b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
23289b8d05b8SZbigniew Bodek {
23299b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
23309b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "enter");
23319b8d05b8SZbigniew Bodek 
23325a990212SMarcin Wojtas 	mtx_lock(&adapter->global_mtx);
23339b8d05b8SZbigniew Bodek 
23349b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
23359b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
23369b8d05b8SZbigniew Bodek 
23379b8d05b8SZbigniew Bodek 	if (!adapter->link_status) {
23385a990212SMarcin Wojtas 		mtx_unlock(&adapter->global_mtx);
23394e8acd84SMarcin Wojtas 		ena_trace(ENA_INFO, "link_status = false");
23409b8d05b8SZbigniew Bodek 		return;
23419b8d05b8SZbigniew Bodek 	}
23429b8d05b8SZbigniew Bodek 
23439b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
2344b8ca5dbeSMarcin Wojtas 	ifmr->ifm_active |= IFM_UNKNOWN | IFM_FDX;
23459b8d05b8SZbigniew Bodek 
23465a990212SMarcin Wojtas 	mtx_unlock(&adapter->global_mtx);
23479b8d05b8SZbigniew Bodek }
23489b8d05b8SZbigniew Bodek 
23499b8d05b8SZbigniew Bodek static void
23509b8d05b8SZbigniew Bodek ena_init(void *arg)
23519b8d05b8SZbigniew Bodek {
23529b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
23539b8d05b8SZbigniew Bodek 
23540bdffe59SMarcin Wojtas 	if (!adapter->up) {
23553d3a90f9SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
23569b8d05b8SZbigniew Bodek 		ena_up(adapter);
23573d3a90f9SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
23583d3a90f9SZbigniew Bodek 	}
23599b8d05b8SZbigniew Bodek }
23609b8d05b8SZbigniew Bodek 
23619b8d05b8SZbigniew Bodek static int
23629b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
23639b8d05b8SZbigniew Bodek {
23649b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
23659b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
23669b8d05b8SZbigniew Bodek 	int rc;
23679b8d05b8SZbigniew Bodek 
23689b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
23699b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
23709b8d05b8SZbigniew Bodek 
23719b8d05b8SZbigniew Bodek 	/*
23729b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
23739b8d05b8SZbigniew Bodek 	 */
23749b8d05b8SZbigniew Bodek 	rc = 0;
23759b8d05b8SZbigniew Bodek 	switch (command) {
23769b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2377dbf2eb54SMarcin Wojtas 		if (ifp->if_mtu == ifr->ifr_mtu)
2378dbf2eb54SMarcin Wojtas 			break;
2379e67c6554SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
23809b8d05b8SZbigniew Bodek 		ena_down(adapter);
23819b8d05b8SZbigniew Bodek 
23829b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
23839b8d05b8SZbigniew Bodek 
23849b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
2385e67c6554SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
23869b8d05b8SZbigniew Bodek 		break;
23879b8d05b8SZbigniew Bodek 
23889b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
23890bdffe59SMarcin Wojtas 		if ((ifp->if_flags & IFF_UP) != 0) {
23900bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
23910bdffe59SMarcin Wojtas 				if ((ifp->if_flags & (IFF_PROMISC |
23920bdffe59SMarcin Wojtas 				    IFF_ALLMULTI)) != 0) {
23939b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
23949b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
23959b8d05b8SZbigniew Bodek 				}
23969b8d05b8SZbigniew Bodek 			} else {
2397e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
23989b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2399e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
24009b8d05b8SZbigniew Bodek 			}
24019b8d05b8SZbigniew Bodek 		} else {
24020bdffe59SMarcin Wojtas 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2403e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24049b8d05b8SZbigniew Bodek 				ena_down(adapter);
2405e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
2406e67c6554SZbigniew Bodek 			}
24079b8d05b8SZbigniew Bodek 		}
24089b8d05b8SZbigniew Bodek 		break;
24099b8d05b8SZbigniew Bodek 
24109b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
24119b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
24129b8d05b8SZbigniew Bodek 		break;
24139b8d05b8SZbigniew Bodek 
24149b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
24159b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
24169b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
24179b8d05b8SZbigniew Bodek 		break;
24189b8d05b8SZbigniew Bodek 
24199b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
24209b8d05b8SZbigniew Bodek 		{
24219b8d05b8SZbigniew Bodek 			int reinit = 0;
24229b8d05b8SZbigniew Bodek 
24239b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
24249b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
24259b8d05b8SZbigniew Bodek 				reinit = 1;
24269b8d05b8SZbigniew Bodek 			}
24279b8d05b8SZbigniew Bodek 
24280bdffe59SMarcin Wojtas 			if ((reinit != 0) &&
24290bdffe59SMarcin Wojtas 			    ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)) {
2430e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
24319b8d05b8SZbigniew Bodek 				ena_down(adapter);
24329b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2433e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
24349b8d05b8SZbigniew Bodek 			}
24359b8d05b8SZbigniew Bodek 		}
24369b8d05b8SZbigniew Bodek 
24379b8d05b8SZbigniew Bodek 		break;
24389b8d05b8SZbigniew Bodek 	default:
24399b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
24409b8d05b8SZbigniew Bodek 		break;
24419b8d05b8SZbigniew Bodek 	}
24429b8d05b8SZbigniew Bodek 
24439b8d05b8SZbigniew Bodek 	return (rc);
24449b8d05b8SZbigniew Bodek }
24459b8d05b8SZbigniew Bodek 
24469b8d05b8SZbigniew Bodek static int
24479b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
24489b8d05b8SZbigniew Bodek {
24499b8d05b8SZbigniew Bodek 	int caps = 0;
24509b8d05b8SZbigniew Bodek 
24510bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24529b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
24539b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
24540bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
24559b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
24569b8d05b8SZbigniew Bodek 
24570bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24589b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
24590bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
24609b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
24619b8d05b8SZbigniew Bodek 
24620bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24630bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
24649b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
24659b8d05b8SZbigniew Bodek 
24660bdffe59SMarcin Wojtas 	if ((feat->offload.tx &
24670bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
24689b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
24699b8d05b8SZbigniew Bodek 
24700bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
24719b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
24720bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
24739b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
24749b8d05b8SZbigniew Bodek 
24750bdffe59SMarcin Wojtas 	if ((feat->offload.rx_supported &
24760bdffe59SMarcin Wojtas 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
24779b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
24789b8d05b8SZbigniew Bodek 
24799b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
24809b8d05b8SZbigniew Bodek 
24819b8d05b8SZbigniew Bodek 	return (caps);
24829b8d05b8SZbigniew Bodek }
24839b8d05b8SZbigniew Bodek 
24849b8d05b8SZbigniew Bodek static void
24859b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
24869b8d05b8SZbigniew Bodek {
24879b8d05b8SZbigniew Bodek 
24889b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
24899b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
24909b8d05b8SZbigniew Bodek }
24919b8d05b8SZbigniew Bodek 
24929b8d05b8SZbigniew Bodek static void
24939b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
24949b8d05b8SZbigniew Bodek {
24959b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
24969b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
24979b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
24989b8d05b8SZbigniew Bodek 	int flags = 0;
24999b8d05b8SZbigniew Bodek 
25009b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
25019b8d05b8SZbigniew Bodek 
25020bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM) != 0) {
25030bdffe59SMarcin Wojtas 		if ((feat &
25040bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
25059b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
25060bdffe59SMarcin Wojtas 		if ((feat &
25079b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
25080bdffe59SMarcin Wojtas 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
25099b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
25109b8d05b8SZbigniew Bodek 	}
25119b8d05b8SZbigniew Bodek 
25120bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TXCSUM_IPV6) != 0)
25139b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
25149b8d05b8SZbigniew Bodek 
25150bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO4) != 0)
25169b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
25179b8d05b8SZbigniew Bodek 
25180bdffe59SMarcin Wojtas 	if ((cap & IFCAP_TSO6) != 0)
25199b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
25209b8d05b8SZbigniew Bodek 
25219b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
25229b8d05b8SZbigniew Bodek }
25239b8d05b8SZbigniew Bodek 
25249b8d05b8SZbigniew Bodek static int
25259b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
25269b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
25279b8d05b8SZbigniew Bodek {
25289b8d05b8SZbigniew Bodek 	if_t ifp;
25299b8d05b8SZbigniew Bodek 	int caps = 0;
25309b8d05b8SZbigniew Bodek 
25319b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
25323f9ed7abSMarcin Wojtas 	if (unlikely(ifp == NULL)) {
25334e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
25349b8d05b8SZbigniew Bodek 		return (ENXIO);
25359b8d05b8SZbigniew Bodek 	}
25369b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
25379b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
25389b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
25399b8d05b8SZbigniew Bodek 
25409b8d05b8SZbigniew Bodek 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
25419b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
25429b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
25439b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
25449b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
25459b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
25469b8d05b8SZbigniew Bodek 
25479b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
25489b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
25499b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
25509b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
25519b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
25529b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
25539b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
25549b8d05b8SZbigniew Bodek 	/* check hardware support */
25559b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
25569b8d05b8SZbigniew Bodek 	/* ... and set them */
25579b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
25589b8d05b8SZbigniew Bodek 
25599b8d05b8SZbigniew Bodek 	/* TSO parameters */
25608a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
25618a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
25628a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
25638a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
25649b8d05b8SZbigniew Bodek 
25659b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
25669b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
25679b8d05b8SZbigniew Bodek 
25689b8d05b8SZbigniew Bodek 	/*
25699b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
25709b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
25719b8d05b8SZbigniew Bodek 	 */
25729b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
25739b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
25749b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
25759b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
25769b8d05b8SZbigniew Bodek 
25779b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
25789b8d05b8SZbigniew Bodek 
25799b8d05b8SZbigniew Bodek 	return (0);
25809b8d05b8SZbigniew Bodek }
25819b8d05b8SZbigniew Bodek 
25829b8d05b8SZbigniew Bodek static void
25839b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
25849b8d05b8SZbigniew Bodek {
2585a195fab0SMarcin Wojtas 	int rc;
25869b8d05b8SZbigniew Bodek 
25879b8d05b8SZbigniew Bodek 	if (adapter->up) {
25889b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going DOWN\n");
25899b8d05b8SZbigniew Bodek 
25909b8d05b8SZbigniew Bodek 		callout_drain(&adapter->timer_service);
25919b8d05b8SZbigniew Bodek 
25929b8d05b8SZbigniew Bodek 		adapter->up = false;
25939b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
25949b8d05b8SZbigniew Bodek 		    IFF_DRV_RUNNING);
25959b8d05b8SZbigniew Bodek 
25969b8d05b8SZbigniew Bodek 		ena_free_io_irq(adapter);
25979b8d05b8SZbigniew Bodek 
2598a195fab0SMarcin Wojtas 		if (adapter->trigger_reset) {
2599a195fab0SMarcin Wojtas 			rc = ena_com_dev_reset(adapter->ena_dev,
2600a195fab0SMarcin Wojtas 			    adapter->reset_reason);
26013f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0))
2602a195fab0SMarcin Wojtas 				device_printf(adapter->pdev,
2603a195fab0SMarcin Wojtas 				    "Device reset failed\n");
2604a195fab0SMarcin Wojtas 		}
2605a195fab0SMarcin Wojtas 
26069b8d05b8SZbigniew Bodek 		ena_destroy_all_io_queues(adapter);
26079b8d05b8SZbigniew Bodek 
26089b8d05b8SZbigniew Bodek 		ena_free_all_tx_bufs(adapter);
26099b8d05b8SZbigniew Bodek 		ena_free_all_rx_bufs(adapter);
26109b8d05b8SZbigniew Bodek 		ena_free_all_tx_resources(adapter);
26119b8d05b8SZbigniew Bodek 		ena_free_all_rx_resources(adapter);
26129b8d05b8SZbigniew Bodek 
26139b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_down, 1);
26149b8d05b8SZbigniew Bodek 	}
26159b8d05b8SZbigniew Bodek }
26169b8d05b8SZbigniew Bodek 
26179b8d05b8SZbigniew Bodek static void
26189b8d05b8SZbigniew Bodek ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
26199b8d05b8SZbigniew Bodek {
26209b8d05b8SZbigniew Bodek 	struct ena_com_tx_meta *ena_meta;
26219b8d05b8SZbigniew Bodek 	struct ether_vlan_header *eh;
26229b8d05b8SZbigniew Bodek 	u32 mss;
26239b8d05b8SZbigniew Bodek 	bool offload;
26249b8d05b8SZbigniew Bodek 	uint16_t etype;
26259b8d05b8SZbigniew Bodek 	int ehdrlen;
26269b8d05b8SZbigniew Bodek 	struct ip *ip;
26279b8d05b8SZbigniew Bodek 	int iphlen;
26289b8d05b8SZbigniew Bodek 	struct tcphdr *th;
26299b8d05b8SZbigniew Bodek 
26309b8d05b8SZbigniew Bodek 	offload = false;
26319b8d05b8SZbigniew Bodek 	ena_meta = &ena_tx_ctx->ena_meta;
26329b8d05b8SZbigniew Bodek 	mss = mbuf->m_pkthdr.tso_segsz;
26339b8d05b8SZbigniew Bodek 
26349b8d05b8SZbigniew Bodek 	if (mss != 0)
26359b8d05b8SZbigniew Bodek 		offload = true;
26369b8d05b8SZbigniew Bodek 
26379b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0)
26389b8d05b8SZbigniew Bodek 		offload = true;
26399b8d05b8SZbigniew Bodek 
26409b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
26419b8d05b8SZbigniew Bodek 		offload = true;
26429b8d05b8SZbigniew Bodek 
26430bdffe59SMarcin Wojtas 	if (!offload) {
26449b8d05b8SZbigniew Bodek 		ena_tx_ctx->meta_valid = 0;
26459b8d05b8SZbigniew Bodek 		return;
26469b8d05b8SZbigniew Bodek 	}
26479b8d05b8SZbigniew Bodek 
26489b8d05b8SZbigniew Bodek 	/* Determine where frame payload starts. */
26499b8d05b8SZbigniew Bodek 	eh = mtod(mbuf, struct ether_vlan_header *);
26509b8d05b8SZbigniew Bodek 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
26519b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_proto);
26529b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
26539b8d05b8SZbigniew Bodek 	} else {
26549b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_encap_proto);
26559b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN;
26569b8d05b8SZbigniew Bodek 	}
26579b8d05b8SZbigniew Bodek 
26589b8d05b8SZbigniew Bodek 	ip = (struct ip *)(mbuf->m_data + ehdrlen);
26599b8d05b8SZbigniew Bodek 	iphlen = ip->ip_hl << 2;
26609b8d05b8SZbigniew Bodek 	th = (struct tcphdr *)((caddr_t)ip + iphlen);
26619b8d05b8SZbigniew Bodek 
26629b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
26639b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_csum_enable = 1;
26649b8d05b8SZbigniew Bodek 	}
26659b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
26669b8d05b8SZbigniew Bodek 		ena_tx_ctx->tso_enable = 1;
26679b8d05b8SZbigniew Bodek 		ena_meta->l4_hdr_len = (th->th_off);
26689b8d05b8SZbigniew Bodek 	}
26699b8d05b8SZbigniew Bodek 
26709b8d05b8SZbigniew Bodek 	switch (etype) {
26719b8d05b8SZbigniew Bodek 	case ETHERTYPE_IP:
26729b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
2673cd433385SMarcin Wojtas 		if ((ip->ip_off & htons(IP_DF)) != 0)
26749b8d05b8SZbigniew Bodek 			ena_tx_ctx->df = 1;
26759b8d05b8SZbigniew Bodek 		break;
26769b8d05b8SZbigniew Bodek 	case ETHERTYPE_IPV6:
26779b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
26789b8d05b8SZbigniew Bodek 
26799b8d05b8SZbigniew Bodek 	default:
26809b8d05b8SZbigniew Bodek 		break;
26819b8d05b8SZbigniew Bodek 	}
26829b8d05b8SZbigniew Bodek 
26839b8d05b8SZbigniew Bodek 	if (ip->ip_p == IPPROTO_TCP) {
26849b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
26850bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
26860bdffe59SMarcin Wojtas 		    (CSUM_IP_TCP | CSUM_IP6_TCP)) != 0)
26879b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
26889b8d05b8SZbigniew Bodek 		else
26899b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
26909b8d05b8SZbigniew Bodek 	} else if (ip->ip_p == IPPROTO_UDP) {
26919b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
26920bdffe59SMarcin Wojtas 		if ((mbuf->m_pkthdr.csum_flags &
26930bdffe59SMarcin Wojtas 		    (CSUM_IP_UDP | CSUM_IP6_UDP)) != 0)
26949b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 1;
26959b8d05b8SZbigniew Bodek 		else
26969b8d05b8SZbigniew Bodek 			ena_tx_ctx->l4_csum_enable = 0;
26979b8d05b8SZbigniew Bodek 	} else {
26989b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
26999b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_csum_enable = 0;
27009b8d05b8SZbigniew Bodek 	}
27019b8d05b8SZbigniew Bodek 
27029b8d05b8SZbigniew Bodek 	ena_meta->mss = mss;
27039b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_len = iphlen;
27049b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_offset = ehdrlen;
27059b8d05b8SZbigniew Bodek 	ena_tx_ctx->meta_valid = 1;
27069b8d05b8SZbigniew Bodek }
27079b8d05b8SZbigniew Bodek 
27089b8d05b8SZbigniew Bodek static int
27091b069f1cSZbigniew Bodek ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
27101e9fb899SZbigniew Bodek {
27111e9fb899SZbigniew Bodek 	struct ena_adapter *adapter;
27121b069f1cSZbigniew Bodek 	struct mbuf *collapsed_mbuf;
27131e9fb899SZbigniew Bodek 	int num_frags;
27141e9fb899SZbigniew Bodek 
27151e9fb899SZbigniew Bodek 	adapter = tx_ring->adapter;
27161e9fb899SZbigniew Bodek 	num_frags = ena_mbuf_count(*mbuf);
27171e9fb899SZbigniew Bodek 
27181e9fb899SZbigniew Bodek 	/* One segment must be reserved for configuration descriptor. */
27191e9fb899SZbigniew Bodek 	if (num_frags < adapter->max_tx_sgl_size)
27201e9fb899SZbigniew Bodek 		return (0);
27211b069f1cSZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
27221e9fb899SZbigniew Bodek 
27231b069f1cSZbigniew Bodek 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
27241b069f1cSZbigniew Bodek 	    adapter->max_tx_sgl_size - 1);
27253f9ed7abSMarcin Wojtas 	if (unlikely(collapsed_mbuf == NULL)) {
27261b069f1cSZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
27271e9fb899SZbigniew Bodek 		return (ENOMEM);
27281e9fb899SZbigniew Bodek 	}
27291e9fb899SZbigniew Bodek 
27301b069f1cSZbigniew Bodek 	/* If mbuf was collapsed succesfully, original mbuf is released. */
27311b069f1cSZbigniew Bodek 	*mbuf = collapsed_mbuf;
27321e9fb899SZbigniew Bodek 
27331e9fb899SZbigniew Bodek 	return (0);
27341e9fb899SZbigniew Bodek }
27351e9fb899SZbigniew Bodek 
27361e9fb899SZbigniew Bodek static int
27371e9fb899SZbigniew Bodek ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
27389b8d05b8SZbigniew Bodek {
27399b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
27409b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info;
27419b8d05b8SZbigniew Bodek 	struct ena_com_tx_ctx ena_tx_ctx;
27429b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
27439b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
27449b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
27459b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[ENA_BUS_DMA_SEGS];
27469b8d05b8SZbigniew Bodek 	void *push_hdr;
27479b8d05b8SZbigniew Bodek 	uint16_t next_to_use;
27489b8d05b8SZbigniew Bodek 	uint16_t req_id;
27499b8d05b8SZbigniew Bodek 	uint16_t push_len;
27509b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
27512339f28cSMarcin Wojtas 	uint32_t nsegs, header_len;
27529b8d05b8SZbigniew Bodek 	int i, rc;
27531e9fb899SZbigniew Bodek 	int nb_hw_desc;
27549b8d05b8SZbigniew Bodek 
27559b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
27569b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
27579b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
275890f4da8bSMarcin Wojtas 	io_sq = &ena_dev->io_sq_queues[ena_qid];
27599b8d05b8SZbigniew Bodek 
27601b069f1cSZbigniew Bodek 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
27613f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
27621e9fb899SZbigniew Bodek 		ena_trace(ENA_WARNING,
27631b069f1cSZbigniew Bodek 		    "Failed to collapse mbuf! err: %d", rc);
27641e9fb899SZbigniew Bodek 		return (rc);
27659b8d05b8SZbigniew Bodek 	}
27669b8d05b8SZbigniew Bodek 
27679b8d05b8SZbigniew Bodek 	next_to_use = tx_ring->next_to_use;
27689b8d05b8SZbigniew Bodek 	req_id = tx_ring->free_tx_ids[next_to_use];
27699b8d05b8SZbigniew Bodek 	tx_info = &tx_ring->tx_buffer_info[req_id];
27709b8d05b8SZbigniew Bodek 
27711e9fb899SZbigniew Bodek 	tx_info->mbuf = *mbuf;
27729b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = 0;
27739b8d05b8SZbigniew Bodek 
27749b8d05b8SZbigniew Bodek 	ena_buf = tx_info->bufs;
27759b8d05b8SZbigniew Bodek 
27761e9fb899SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
27779b8d05b8SZbigniew Bodek 
27789b8d05b8SZbigniew Bodek 	push_len = 0;
27792339f28cSMarcin Wojtas 	/*
27802339f28cSMarcin Wojtas 	 * header_len is just a hint for the device. Because FreeBSD is not
27812339f28cSMarcin Wojtas 	 * giving us information about packet header length and it is not
27822339f28cSMarcin Wojtas 	 * guaranteed that all packet headers will be in the 1st mbuf, setting
27832339f28cSMarcin Wojtas 	 * header_len to 0 is making the device ignore this value and resolve
27842339f28cSMarcin Wojtas 	 * header on it's own.
27852339f28cSMarcin Wojtas 	 */
27862339f28cSMarcin Wojtas 	header_len = 0;
27879b8d05b8SZbigniew Bodek 	push_hdr = NULL;
27889b8d05b8SZbigniew Bodek 
27899b8d05b8SZbigniew Bodek 	rc = bus_dmamap_load_mbuf_sg(adapter->tx_buf_tag, tx_info->map,
27901e9fb899SZbigniew Bodek 	    *mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
27919b8d05b8SZbigniew Bodek 
27923f9ed7abSMarcin Wojtas 	if (unlikely((rc != 0) || (nsegs == 0))) {
27939b8d05b8SZbigniew Bodek 		ena_trace(ENA_WARNING,
27949b8d05b8SZbigniew Bodek 		    "dmamap load failed! err: %d nsegs: %d", rc, nsegs);
27959b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
27969b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
27979b8d05b8SZbigniew Bodek 		if (rc == ENOMEM)
27989b8d05b8SZbigniew Bodek 			return (ENA_COM_NO_MEM);
27999b8d05b8SZbigniew Bodek 		else
28009b8d05b8SZbigniew Bodek 			return (ENA_COM_INVAL);
28019b8d05b8SZbigniew Bodek 	}
28029b8d05b8SZbigniew Bodek 
28039b8d05b8SZbigniew Bodek 	for (i = 0; i < nsegs; i++) {
28049b8d05b8SZbigniew Bodek 		ena_buf->len = segs[i].ds_len;
28059b8d05b8SZbigniew Bodek 		ena_buf->paddr = segs[i].ds_addr;
28069b8d05b8SZbigniew Bodek 		ena_buf++;
28079b8d05b8SZbigniew Bodek 	}
28089b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = nsegs;
28099b8d05b8SZbigniew Bodek 
28109b8d05b8SZbigniew Bodek 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
28119b8d05b8SZbigniew Bodek 	ena_tx_ctx.ena_bufs = tx_info->bufs;
28129b8d05b8SZbigniew Bodek 	ena_tx_ctx.push_header = push_hdr;
28139b8d05b8SZbigniew Bodek 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
28149b8d05b8SZbigniew Bodek 	ena_tx_ctx.req_id = req_id;
28159b8d05b8SZbigniew Bodek 	ena_tx_ctx.header_len = header_len;
28169b8d05b8SZbigniew Bodek 
28179b8d05b8SZbigniew Bodek 	/* Set flags and meta data */
28181e9fb899SZbigniew Bodek 	ena_tx_csum(&ena_tx_ctx, *mbuf);
28199b8d05b8SZbigniew Bodek 	/* Prepare the packet's descriptors and send them to device */
28209b8d05b8SZbigniew Bodek 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
28213f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
2822e3cecf70SMarcin Wojtas 		if (likely(rc == ENA_COM_NO_MEM)) {
2823e3cecf70SMarcin Wojtas 			ena_trace(ENA_DBG | ENA_TXPTH,
2824e3cecf70SMarcin Wojtas 			    "tx ring[%d] if out of space\n", tx_ring->que->id);
2825e3cecf70SMarcin Wojtas 		} else {
2826e3cecf70SMarcin Wojtas 			device_printf(adapter->pdev,
2827e3cecf70SMarcin Wojtas 			    "failed to prepare tx bufs\n");
2828e3cecf70SMarcin Wojtas 		}
28290052f3b5SMarcin Wojtas 		counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
28309b8d05b8SZbigniew Bodek 		goto dma_error;
28319b8d05b8SZbigniew Bodek 	}
28329b8d05b8SZbigniew Bodek 
28339b8d05b8SZbigniew Bodek 	counter_enter();
28349b8d05b8SZbigniew Bodek 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
28350bdffe59SMarcin Wojtas 	counter_u64_add_protected(tx_ring->tx_stats.bytes,
28360bdffe59SMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
283730217e2dSMarcin Wojtas 
283830217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
283930217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
284030217e2dSMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
28419b8d05b8SZbigniew Bodek 	counter_exit();
28429b8d05b8SZbigniew Bodek 
28439b8d05b8SZbigniew Bodek 	tx_info->tx_descs = nb_hw_desc;
28449b8d05b8SZbigniew Bodek 	getbinuptime(&tx_info->timestamp);
28459b8d05b8SZbigniew Bodek 	tx_info->print_once = true;
28469b8d05b8SZbigniew Bodek 
28479b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
28489b8d05b8SZbigniew Bodek 	    tx_ring->ring_size);
28499b8d05b8SZbigniew Bodek 
28500bdffe59SMarcin Wojtas 	bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map,
28510bdffe59SMarcin Wojtas 	    BUS_DMASYNC_PREWRITE);
28529b8d05b8SZbigniew Bodek 
28539b8d05b8SZbigniew Bodek 	return (0);
28549b8d05b8SZbigniew Bodek 
28559b8d05b8SZbigniew Bodek dma_error:
28569b8d05b8SZbigniew Bodek 	tx_info->mbuf = NULL;
28579b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
28589b8d05b8SZbigniew Bodek 
28599b8d05b8SZbigniew Bodek 	return (rc);
28609b8d05b8SZbigniew Bodek }
28619b8d05b8SZbigniew Bodek 
28629b8d05b8SZbigniew Bodek static void
28639b8d05b8SZbigniew Bodek ena_start_xmit(struct ena_ring *tx_ring)
28649b8d05b8SZbigniew Bodek {
28659b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
28669b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = tx_ring->adapter;
28679b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
28689b8d05b8SZbigniew Bodek 	int ena_qid;
28699b8d05b8SZbigniew Bodek 	int acum_pkts = 0;
28709b8d05b8SZbigniew Bodek 	int ret = 0;
28719b8d05b8SZbigniew Bodek 
28723f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
28739b8d05b8SZbigniew Bodek 		return;
28749b8d05b8SZbigniew Bodek 
28753f9ed7abSMarcin Wojtas 	if (unlikely(!adapter->link_status))
28769b8d05b8SZbigniew Bodek 		return;
28779b8d05b8SZbigniew Bodek 
28789b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
28799b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
28809b8d05b8SZbigniew Bodek 
28819b8d05b8SZbigniew Bodek 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
28829b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
28839b8d05b8SZbigniew Bodek 		    " header csum flags %#jx",
28844e8acd84SMarcin Wojtas 		    mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
28859b8d05b8SZbigniew Bodek 
288667ec48bbSMarcin Wojtas 		if (unlikely(ena_com_free_desc(io_sq) <=
288767ec48bbSMarcin Wojtas 		    ENA_TX_CLEANUP_THRESHOLD))
28889b8d05b8SZbigniew Bodek 			ena_tx_cleanup(tx_ring);
28899b8d05b8SZbigniew Bodek 
28903f9ed7abSMarcin Wojtas 		if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
28919b8d05b8SZbigniew Bodek 			if (ret == ENA_COM_NO_MEM) {
28929b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
28939b8d05b8SZbigniew Bodek 			} else if (ret == ENA_COM_NO_SPACE) {
28949b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
28959b8d05b8SZbigniew Bodek 			} else {
28969b8d05b8SZbigniew Bodek 				m_freem(mbuf);
28979b8d05b8SZbigniew Bodek 				drbr_advance(adapter->ifp, tx_ring->br);
28989b8d05b8SZbigniew Bodek 			}
28999b8d05b8SZbigniew Bodek 
29009b8d05b8SZbigniew Bodek 			break;
29019b8d05b8SZbigniew Bodek 		}
29029b8d05b8SZbigniew Bodek 
2903b4b29032SZbigniew Bodek 		drbr_advance(adapter->ifp, tx_ring->br);
2904b4b29032SZbigniew Bodek 
29053f9ed7abSMarcin Wojtas 		if (unlikely((if_getdrvflags(adapter->ifp) &
29063f9ed7abSMarcin Wojtas 		    IFF_DRV_RUNNING) == 0))
29079b8d05b8SZbigniew Bodek 			return;
29089b8d05b8SZbigniew Bodek 
29099b8d05b8SZbigniew Bodek 		acum_pkts++;
29109b8d05b8SZbigniew Bodek 
29119b8d05b8SZbigniew Bodek 		BPF_MTAP(adapter->ifp, mbuf);
29129b8d05b8SZbigniew Bodek 
29133f9ed7abSMarcin Wojtas 		if (unlikely(acum_pkts == DB_THRESHOLD)) {
29149b8d05b8SZbigniew Bodek 			acum_pkts = 0;
29159b8d05b8SZbigniew Bodek 			wmb();
29169b8d05b8SZbigniew Bodek 			/* Trigger the dma engine */
29179b8d05b8SZbigniew Bodek 			ena_com_write_sq_doorbell(io_sq);
29189b8d05b8SZbigniew Bodek 			counter_u64_add(tx_ring->tx_stats.doorbells, 1);
29199b8d05b8SZbigniew Bodek 		}
29209b8d05b8SZbigniew Bodek 
29219b8d05b8SZbigniew Bodek 	}
29229b8d05b8SZbigniew Bodek 
29233f9ed7abSMarcin Wojtas 	if (likely(acum_pkts != 0)) {
29249b8d05b8SZbigniew Bodek 		wmb();
29259b8d05b8SZbigniew Bodek 		/* Trigger the dma engine */
29269b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(io_sq);
29279b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
29289b8d05b8SZbigniew Bodek 	}
29299b8d05b8SZbigniew Bodek 
293067ec48bbSMarcin Wojtas 	if (ena_com_free_desc(io_sq) <= ENA_TX_CLEANUP_THRESHOLD)
29319b8d05b8SZbigniew Bodek 		ena_tx_cleanup(tx_ring);
29329b8d05b8SZbigniew Bodek }
29339b8d05b8SZbigniew Bodek 
29349b8d05b8SZbigniew Bodek static void
29359b8d05b8SZbigniew Bodek ena_deferred_mq_start(void *arg, int pending)
29369b8d05b8SZbigniew Bodek {
29379b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
29389b8d05b8SZbigniew Bodek 	struct ifnet *ifp = tx_ring->adapter->ifp;
29399b8d05b8SZbigniew Bodek 
29400bdffe59SMarcin Wojtas 	while (!drbr_empty(ifp, tx_ring->br) &&
29410bdffe59SMarcin Wojtas 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
29429b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
29439b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
29449b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
29459b8d05b8SZbigniew Bodek 	}
29469b8d05b8SZbigniew Bodek }
29479b8d05b8SZbigniew Bodek 
29489b8d05b8SZbigniew Bodek static int
29499b8d05b8SZbigniew Bodek ena_mq_start(if_t ifp, struct mbuf *m)
29509b8d05b8SZbigniew Bodek {
29519b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
29529b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
29539b8d05b8SZbigniew Bodek 	int ret, is_drbr_empty;
29549b8d05b8SZbigniew Bodek 	uint32_t i;
29559b8d05b8SZbigniew Bodek 
29563f9ed7abSMarcin Wojtas 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_DRV_RUNNING) == 0))
29579b8d05b8SZbigniew Bodek 		return (ENODEV);
29589b8d05b8SZbigniew Bodek 
29599b8d05b8SZbigniew Bodek 	/* Which queue to use */
29609b8d05b8SZbigniew Bodek 	/*
29619b8d05b8SZbigniew Bodek 	 * If everything is setup correctly, it should be the
29629b8d05b8SZbigniew Bodek 	 * same bucket that the current CPU we're on is.
29639b8d05b8SZbigniew Bodek 	 * It should improve performance.
29649b8d05b8SZbigniew Bodek 	 */
29659b8d05b8SZbigniew Bodek 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
29669b8d05b8SZbigniew Bodek 		i = m->m_pkthdr.flowid % adapter->num_queues;
29679b8d05b8SZbigniew Bodek 	} else {
29689b8d05b8SZbigniew Bodek 		i = curcpu % adapter->num_queues;
29699b8d05b8SZbigniew Bodek 	}
29709b8d05b8SZbigniew Bodek 	tx_ring = &adapter->tx_ring[i];
29719b8d05b8SZbigniew Bodek 
29729b8d05b8SZbigniew Bodek 	/* Check if drbr is empty before putting packet */
29739b8d05b8SZbigniew Bodek 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
29749b8d05b8SZbigniew Bodek 	ret = drbr_enqueue(ifp, tx_ring->br, m);
29753f9ed7abSMarcin Wojtas 	if (unlikely(ret != 0)) {
29769b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
29779b8d05b8SZbigniew Bodek 		return (ret);
29789b8d05b8SZbigniew Bodek 	}
29799b8d05b8SZbigniew Bodek 
298067ec48bbSMarcin Wojtas 	if (is_drbr_empty && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
29819b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
29829b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
29839b8d05b8SZbigniew Bodek 	} else {
29849b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
29859b8d05b8SZbigniew Bodek 	}
29869b8d05b8SZbigniew Bodek 
29879b8d05b8SZbigniew Bodek 	return (0);
29889b8d05b8SZbigniew Bodek }
29899b8d05b8SZbigniew Bodek 
29909b8d05b8SZbigniew Bodek static void
29919b8d05b8SZbigniew Bodek ena_qflush(if_t ifp)
29929b8d05b8SZbigniew Bodek {
29939b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
29949b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = adapter->tx_ring;
29959b8d05b8SZbigniew Bodek 	int i;
29969b8d05b8SZbigniew Bodek 
29979b8d05b8SZbigniew Bodek 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
29980bdffe59SMarcin Wojtas 		if (!drbr_empty(ifp, tx_ring->br)) {
29999b8d05b8SZbigniew Bodek 			ENA_RING_MTX_LOCK(tx_ring);
30009b8d05b8SZbigniew Bodek 			drbr_flush(ifp, tx_ring->br);
30019b8d05b8SZbigniew Bodek 			ENA_RING_MTX_UNLOCK(tx_ring);
30029b8d05b8SZbigniew Bodek 		}
30039b8d05b8SZbigniew Bodek 
30049b8d05b8SZbigniew Bodek 	if_qflush(ifp);
30059b8d05b8SZbigniew Bodek }
30069b8d05b8SZbigniew Bodek 
30070bdffe59SMarcin Wojtas static int
30080bdffe59SMarcin Wojtas ena_calc_io_queue_num(struct ena_adapter *adapter,
30099b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
30109b8d05b8SZbigniew Bodek {
30119b8d05b8SZbigniew Bodek 	int io_sq_num, io_cq_num, io_queue_num;
30129b8d05b8SZbigniew Bodek 
30139b8d05b8SZbigniew Bodek 	io_sq_num = get_feat_ctx->max_queues.max_sq_num;
3014ad5a5afaSMarcin Wojtas 	io_cq_num = get_feat_ctx->max_queues.max_cq_num;
30159b8d05b8SZbigniew Bodek 
30169b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
30179b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_sq_num);
30189b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_cq_num);
30199b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
30209b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num,
30219b8d05b8SZbigniew Bodek 	    pci_msix_count(adapter->pdev) - 1);
30229b8d05b8SZbigniew Bodek 
30230bdffe59SMarcin Wojtas 	return (io_queue_num);
30249b8d05b8SZbigniew Bodek }
30259b8d05b8SZbigniew Bodek 
30260bdffe59SMarcin Wojtas static int
30270bdffe59SMarcin Wojtas ena_calc_queue_size(struct ena_adapter *adapter, uint16_t *max_tx_sgl_size,
30280bdffe59SMarcin Wojtas     uint16_t *max_rx_sgl_size, struct ena_com_dev_get_features_ctx *feat)
30299b8d05b8SZbigniew Bodek {
30309b8d05b8SZbigniew Bodek 	uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
30319b8d05b8SZbigniew Bodek 	uint32_t v;
30329b8d05b8SZbigniew Bodek 	uint32_t q;
30339b8d05b8SZbigniew Bodek 
30349b8d05b8SZbigniew Bodek 	queue_size = min_t(uint32_t, queue_size,
30359b8d05b8SZbigniew Bodek 	    feat->max_queues.max_cq_depth);
30369b8d05b8SZbigniew Bodek 	queue_size = min_t(uint32_t, queue_size,
30379b8d05b8SZbigniew Bodek 	    feat->max_queues.max_sq_depth);
30389b8d05b8SZbigniew Bodek 
30399b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
30409b8d05b8SZbigniew Bodek 	v = queue_size;
30419b8d05b8SZbigniew Bodek 	while (v != 0) {
30420bdffe59SMarcin Wojtas 		if (powerof2(queue_size) != 0)
30439b8d05b8SZbigniew Bodek 			break;
30449b8d05b8SZbigniew Bodek 		v /= 2;
30459b8d05b8SZbigniew Bodek 		q = rounddown2(queue_size, v);
30469b8d05b8SZbigniew Bodek 		if (q != 0) {
30479b8d05b8SZbigniew Bodek 			queue_size = q;
30489b8d05b8SZbigniew Bodek 			break;
30499b8d05b8SZbigniew Bodek 		}
30509b8d05b8SZbigniew Bodek 	}
30519b8d05b8SZbigniew Bodek 
30520bdffe59SMarcin Wojtas 	if (unlikely(queue_size == 0)) {
30539b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid queue size\n");
30540bdffe59SMarcin Wojtas 		return (ENA_COM_FAULT);
30559b8d05b8SZbigniew Bodek 	}
30569b8d05b8SZbigniew Bodek 
30579b8d05b8SZbigniew Bodek 	*max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
30589b8d05b8SZbigniew Bodek 	    feat->max_queues.max_packet_tx_descs);
30599b8d05b8SZbigniew Bodek 	*max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
30609b8d05b8SZbigniew Bodek 	    feat->max_queues.max_packet_rx_descs);
30619b8d05b8SZbigniew Bodek 
30620bdffe59SMarcin Wojtas 	return (queue_size);
30639b8d05b8SZbigniew Bodek }
30649b8d05b8SZbigniew Bodek 
30650bdffe59SMarcin Wojtas static int
30660bdffe59SMarcin Wojtas ena_rss_init_default(struct ena_adapter *adapter)
30679b8d05b8SZbigniew Bodek {
30689b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
30699b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
30709b8d05b8SZbigniew Bodek 	int qid, rc, i;
30719b8d05b8SZbigniew Bodek 
30729b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
30730bdffe59SMarcin Wojtas 	if (unlikely(rc != 0)) {
30744e8acd84SMarcin Wojtas 		device_printf(dev, "Cannot init indirect table\n");
30757d2544e6SMarcin Wojtas 		return (rc);
30769b8d05b8SZbigniew Bodek 	}
30779b8d05b8SZbigniew Bodek 
30789b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
30799b8d05b8SZbigniew Bodek 		qid = i % adapter->num_queues;
30809b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
30819b8d05b8SZbigniew Bodek 		    ENA_IO_RXQ_IDX(qid));
30820bdffe59SMarcin Wojtas 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
30839b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
30847d2544e6SMarcin Wojtas 			goto err_rss_destroy;
30859b8d05b8SZbigniew Bodek 		}
30869b8d05b8SZbigniew Bodek 	}
30879b8d05b8SZbigniew Bodek 
30889b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
30899b8d05b8SZbigniew Bodek 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
30900bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
30919b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
30927d2544e6SMarcin Wojtas 		goto err_rss_destroy;
30939b8d05b8SZbigniew Bodek 	}
30949b8d05b8SZbigniew Bodek 
30959b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
30960bdffe59SMarcin Wojtas 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
30979b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
30987d2544e6SMarcin Wojtas 		goto err_rss_destroy;
30999b8d05b8SZbigniew Bodek 	}
31009b8d05b8SZbigniew Bodek 
31019b8d05b8SZbigniew Bodek 	return (0);
31029b8d05b8SZbigniew Bodek 
31037d2544e6SMarcin Wojtas err_rss_destroy:
31049b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
31059b8d05b8SZbigniew Bodek 	return (rc);
31069b8d05b8SZbigniew Bodek }
31079b8d05b8SZbigniew Bodek 
31089b8d05b8SZbigniew Bodek static void
31099b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
31109b8d05b8SZbigniew Bodek {
31119b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
31129b8d05b8SZbigniew Bodek 	devclass_t dc;
31139b8d05b8SZbigniew Bodek 	int max;
31149b8d05b8SZbigniew Bodek 	int rc;
31159b8d05b8SZbigniew Bodek 
31169b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
31173f9ed7abSMarcin Wojtas 	if (unlikely(dc == NULL)) {
31184e8acd84SMarcin Wojtas 		ena_trace(ENA_ALERT, "No devclass ena\n");
31199b8d05b8SZbigniew Bodek 		return;
31209b8d05b8SZbigniew Bodek 	}
31219b8d05b8SZbigniew Bodek 
31229b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
31239b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
31249b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
31259b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
31269b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
31279b8d05b8SZbigniew Bodek 			adapter->rss_support = true;
31283f9ed7abSMarcin Wojtas 			if (unlikely(rc != 0)) {
31299b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
31309b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
31310bdffe59SMarcin Wojtas 				    " it will affect bandwidth\n");
31329b8d05b8SZbigniew Bodek 				adapter->rss_support = false;
31339b8d05b8SZbigniew Bodek 			}
31349b8d05b8SZbigniew Bodek 		}
31359b8d05b8SZbigniew Bodek 	}
31369b8d05b8SZbigniew Bodek }
31379b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
31389b8d05b8SZbigniew Bodek 
31390bdffe59SMarcin Wojtas static void
31400bdffe59SMarcin Wojtas ena_config_host_info(struct ena_com_dev *ena_dev)
31419b8d05b8SZbigniew Bodek {
31429b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
31439b8d05b8SZbigniew Bodek 	int rc;
31449b8d05b8SZbigniew Bodek 
31459b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
31469b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
31473f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
31489b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
31499b8d05b8SZbigniew Bodek 		return;
31509b8d05b8SZbigniew Bodek 	}
31519b8d05b8SZbigniew Bodek 
31529b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
31539b8d05b8SZbigniew Bodek 
31549b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
31559b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
31569b8d05b8SZbigniew Bodek 
31579b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
31589b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
31599b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
31609b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
31619b8d05b8SZbigniew Bodek 
31629b8d05b8SZbigniew Bodek 	host_info->driver_version =
31639b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
31649b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
31659b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
31668ece6b25SMarcin Wojtas 	host_info->num_cpus = mp_ncpus;
31679b8d05b8SZbigniew Bodek 
31689b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
31693f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
3170a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
31719b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
31729b8d05b8SZbigniew Bodek 		else
31739b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
31749b8d05b8SZbigniew Bodek 
31759b8d05b8SZbigniew Bodek 		goto err;
31769b8d05b8SZbigniew Bodek 	}
31779b8d05b8SZbigniew Bodek 
31789b8d05b8SZbigniew Bodek 	return;
31799b8d05b8SZbigniew Bodek 
31809b8d05b8SZbigniew Bodek err:
31819b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
31829b8d05b8SZbigniew Bodek }
31839b8d05b8SZbigniew Bodek 
31849b8d05b8SZbigniew Bodek static int
31859b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
31869b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
31879b8d05b8SZbigniew Bodek {
31889b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
31899b8d05b8SZbigniew Bodek 	bool readless_supported;
31909b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
31919b8d05b8SZbigniew Bodek 	int dma_width;
31929b8d05b8SZbigniew Bodek 	int rc;
31939b8d05b8SZbigniew Bodek 
31949b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
31953f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
31969b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
31970bdffe59SMarcin Wojtas 		return (rc);
31989b8d05b8SZbigniew Bodek 	}
31999b8d05b8SZbigniew Bodek 
32009b8d05b8SZbigniew Bodek 	/*
32019b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
32029b8d05b8SZbigniew Bodek 	 * read is disabled
32039b8d05b8SZbigniew Bodek 	 */
32049b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
32059b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
32069b8d05b8SZbigniew Bodek 
3207a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
32083f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32099b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
32109b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
32119b8d05b8SZbigniew Bodek 	}
32129b8d05b8SZbigniew Bodek 
32139b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
32143f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32159b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
32169b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
32179b8d05b8SZbigniew Bodek 	}
32189b8d05b8SZbigniew Bodek 
32199b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
32203f9ed7abSMarcin Wojtas 	if (unlikely(dma_width < 0)) {
32219b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
32229b8d05b8SZbigniew Bodek 		rc = dma_width;
32239b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
32249b8d05b8SZbigniew Bodek 	}
32259b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
32269b8d05b8SZbigniew Bodek 
32279b8d05b8SZbigniew Bodek 	/* ENA admin level init */
322867ec48bbSMarcin Wojtas 	rc = ena_com_admin_init(ena_dev, &aenq_handlers);
32293f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32309b8d05b8SZbigniew Bodek 		device_printf(pdev,
32319b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
32329b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
32339b8d05b8SZbigniew Bodek 	}
32349b8d05b8SZbigniew Bodek 
32359b8d05b8SZbigniew Bodek 	/*
32369b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
32379b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
32389b8d05b8SZbigniew Bodek 	 * information
32399b8d05b8SZbigniew Bodek 	 */
32409b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
32419b8d05b8SZbigniew Bodek 
32429b8d05b8SZbigniew Bodek 	ena_config_host_info(ena_dev);
32439b8d05b8SZbigniew Bodek 
32449b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
32459b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
32463f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32479b8d05b8SZbigniew Bodek 		device_printf(pdev,
32489b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
32499b8d05b8SZbigniew Bodek 		goto err_admin_init;
32509b8d05b8SZbigniew Bodek 	}
32519b8d05b8SZbigniew Bodek 
3252e6de9a83SMarcin Wojtas 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
3253e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_FATAL_ERROR) |
3254e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_WARNING) |
325540621d71SMarcin Wojtas 	    BIT(ENA_ADMIN_NOTIFICATION) |
3256e6de9a83SMarcin Wojtas 	    BIT(ENA_ADMIN_KEEP_ALIVE);
32579b8d05b8SZbigniew Bodek 
32589b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
32599b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
32603f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32619b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
32629b8d05b8SZbigniew Bodek 		goto err_admin_init;
32639b8d05b8SZbigniew Bodek 	}
32649b8d05b8SZbigniew Bodek 
32659b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
32669b8d05b8SZbigniew Bodek 
32670bdffe59SMarcin Wojtas 	return (0);
32689b8d05b8SZbigniew Bodek 
32699b8d05b8SZbigniew Bodek err_admin_init:
32709b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
32719b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
32729b8d05b8SZbigniew Bodek err_mmio_read_less:
32739b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
32749b8d05b8SZbigniew Bodek 
32750bdffe59SMarcin Wojtas 	return (rc);
32769b8d05b8SZbigniew Bodek }
32779b8d05b8SZbigniew Bodek 
32789b8d05b8SZbigniew Bodek static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
32799b8d05b8SZbigniew Bodek     int io_vectors)
32809b8d05b8SZbigniew Bodek {
32819b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
32829b8d05b8SZbigniew Bodek 	int rc;
32839b8d05b8SZbigniew Bodek 
32849b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
32853f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32869b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
32870bdffe59SMarcin Wojtas 		return (rc);
32889b8d05b8SZbigniew Bodek 	}
32899b8d05b8SZbigniew Bodek 
32909b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
32919b8d05b8SZbigniew Bodek 
32929b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
32933f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
32949b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
32959b8d05b8SZbigniew Bodek 		goto err_disable_msix;
32969b8d05b8SZbigniew Bodek 	}
32979b8d05b8SZbigniew Bodek 
32989b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
32999b8d05b8SZbigniew Bodek 
33009b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
33019b8d05b8SZbigniew Bodek 
33020bdffe59SMarcin Wojtas 	return (0);
33039b8d05b8SZbigniew Bodek 
33049b8d05b8SZbigniew Bodek err_disable_msix:
33059b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
33069b8d05b8SZbigniew Bodek 
33070bdffe59SMarcin Wojtas 	return (rc);
33089b8d05b8SZbigniew Bodek }
33099b8d05b8SZbigniew Bodek 
33109b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
33119b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
33129b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
33139b8d05b8SZbigniew Bodek {
33149b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
331530217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
33169b8d05b8SZbigniew Bodek 	sbintime_t stime;
331730217e2dSMarcin Wojtas 	uint64_t rx_drops;
331830217e2dSMarcin Wojtas 
331930217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
332030217e2dSMarcin Wojtas 
332130217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
332230217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
332330217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
33249b8d05b8SZbigniew Bodek 
33259b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
33269b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
33279b8d05b8SZbigniew Bodek }
33289b8d05b8SZbigniew Bodek 
33299b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
33309b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
33319b8d05b8SZbigniew Bodek {
33329b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
33339b8d05b8SZbigniew Bodek 
33349b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
33359b8d05b8SZbigniew Bodek 		return;
33369b8d05b8SZbigniew Bodek 
333740621d71SMarcin Wojtas 	if (adapter->keep_alive_timeout == ENA_HW_HINTS_NO_TIMEOUT)
33389b8d05b8SZbigniew Bodek 		return;
33399b8d05b8SZbigniew Bodek 
33409b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
33419b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
33429b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
33439b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33449b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
33459b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
3346a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
33479b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
33489b8d05b8SZbigniew Bodek 	}
33499b8d05b8SZbigniew Bodek }
33509b8d05b8SZbigniew Bodek 
33519b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
33529b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
33539b8d05b8SZbigniew Bodek {
33540bdffe59SMarcin Wojtas 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
33550bdffe59SMarcin Wojtas 	    false)) {
33569b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33579b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
33589b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
3359a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
33609b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
33619b8d05b8SZbigniew Bodek 	}
33629b8d05b8SZbigniew Bodek }
33639b8d05b8SZbigniew Bodek 
336474dba3adSMarcin Wojtas static int
3365d12f7bfcSMarcin Wojtas check_for_rx_interrupt_queue(struct ena_adapter *adapter,
3366d12f7bfcSMarcin Wojtas     struct ena_ring *rx_ring)
3367d12f7bfcSMarcin Wojtas {
3368d12f7bfcSMarcin Wojtas 	if (likely(rx_ring->first_interrupt))
3369d12f7bfcSMarcin Wojtas 		return (0);
3370d12f7bfcSMarcin Wojtas 
3371d12f7bfcSMarcin Wojtas 	if (ena_com_cq_empty(rx_ring->ena_com_io_cq))
3372d12f7bfcSMarcin Wojtas 		return (0);
3373d12f7bfcSMarcin Wojtas 
3374d12f7bfcSMarcin Wojtas 	rx_ring->no_interrupt_event_cnt++;
3375d12f7bfcSMarcin Wojtas 
3376d12f7bfcSMarcin Wojtas 	if (rx_ring->no_interrupt_event_cnt == ENA_MAX_NO_INTERRUPT_ITERATIONS) {
3377d12f7bfcSMarcin Wojtas 		device_printf(adapter->pdev, "Potential MSIX issue on Rx side "
3378d12f7bfcSMarcin Wojtas 		   "Queue = %d. Reset the device\n", rx_ring->qid);
3379d12f7bfcSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3380d12f7bfcSMarcin Wojtas 		adapter->trigger_reset = true;
3381d12f7bfcSMarcin Wojtas 		return (EIO);
3382d12f7bfcSMarcin Wojtas 	}
3383d12f7bfcSMarcin Wojtas 
3384d12f7bfcSMarcin Wojtas 	return (0);
3385d12f7bfcSMarcin Wojtas }
3386d12f7bfcSMarcin Wojtas 
3387d12f7bfcSMarcin Wojtas static int
3388d12f7bfcSMarcin Wojtas check_missing_comp_in_tx_queue(struct ena_adapter *adapter,
338974dba3adSMarcin Wojtas     struct ena_ring *tx_ring)
339074dba3adSMarcin Wojtas {
339174dba3adSMarcin Wojtas 	struct bintime curtime, time;
339274dba3adSMarcin Wojtas 	struct ena_tx_buffer *tx_buf;
3393d12f7bfcSMarcin Wojtas 	sbintime_t time_offset;
339474dba3adSMarcin Wojtas 	uint32_t missed_tx = 0;
3395d12f7bfcSMarcin Wojtas 	int i, rc = 0;
339674dba3adSMarcin Wojtas 
339774dba3adSMarcin Wojtas 	getbinuptime(&curtime);
339874dba3adSMarcin Wojtas 
339974dba3adSMarcin Wojtas 	for (i = 0; i < tx_ring->ring_size; i++) {
340074dba3adSMarcin Wojtas 		tx_buf = &tx_ring->tx_buffer_info[i];
340174dba3adSMarcin Wojtas 
34020bdffe59SMarcin Wojtas 		if (bintime_isset(&tx_buf->timestamp) == 0)
340374dba3adSMarcin Wojtas 			continue;
340474dba3adSMarcin Wojtas 
340574dba3adSMarcin Wojtas 		time = curtime;
340674dba3adSMarcin Wojtas 		bintime_sub(&time, &tx_buf->timestamp);
3407d12f7bfcSMarcin Wojtas 		time_offset = bttosbt(time);
3408d12f7bfcSMarcin Wojtas 
3409d12f7bfcSMarcin Wojtas 		if (unlikely(!tx_ring->first_interrupt &&
3410d12f7bfcSMarcin Wojtas 		    time_offset > 2 * adapter->missing_tx_timeout)) {
3411d12f7bfcSMarcin Wojtas 			/*
3412d12f7bfcSMarcin Wojtas 			 * If after graceful period interrupt is still not
3413d12f7bfcSMarcin Wojtas 			 * received, we schedule a reset.
3414d12f7bfcSMarcin Wojtas 			 */
3415d12f7bfcSMarcin Wojtas 			device_printf(adapter->pdev,
3416d12f7bfcSMarcin Wojtas 			    "Potential MSIX issue on Tx side Queue = %d. "
3417d12f7bfcSMarcin Wojtas 			    "Reset the device\n", tx_ring->qid);
3418d12f7bfcSMarcin Wojtas 			adapter->reset_reason = ENA_REGS_RESET_MISS_INTERRUPT;
3419d12f7bfcSMarcin Wojtas 			adapter->trigger_reset = true;
3420d12f7bfcSMarcin Wojtas 			return (EIO);
3421d12f7bfcSMarcin Wojtas 		}
342274dba3adSMarcin Wojtas 
342374dba3adSMarcin Wojtas 		/* Check again if packet is still waiting */
3424d12f7bfcSMarcin Wojtas 		if (unlikely(time_offset > adapter->missing_tx_timeout)) {
342574dba3adSMarcin Wojtas 
342674dba3adSMarcin Wojtas 			if (!tx_buf->print_once)
342774dba3adSMarcin Wojtas 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
342874dba3adSMarcin Wojtas 				    "completed on time, qid %d, index %d.\n",
342974dba3adSMarcin Wojtas 				    tx_ring->qid, i);
343074dba3adSMarcin Wojtas 
343174dba3adSMarcin Wojtas 			tx_buf->print_once = true;
343274dba3adSMarcin Wojtas 			missed_tx++;
3433d12f7bfcSMarcin Wojtas 		}
3434d12f7bfcSMarcin Wojtas 	}
343574dba3adSMarcin Wojtas 
3436d12f7bfcSMarcin Wojtas 	if (unlikely(missed_tx > adapter->missing_tx_threshold)) {
343774dba3adSMarcin Wojtas 		device_printf(adapter->pdev,
3438d12f7bfcSMarcin Wojtas 		    "The number of lost tx completion is above the threshold "
3439d12f7bfcSMarcin Wojtas 		    "(%d > %d). Reset the device\n",
34404e8acd84SMarcin Wojtas 		    missed_tx, adapter->missing_tx_threshold);
3441d12f7bfcSMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_MISS_TX_CMPL;
344274dba3adSMarcin Wojtas 		adapter->trigger_reset = true;
3443d12f7bfcSMarcin Wojtas 		rc = EIO;
344474dba3adSMarcin Wojtas 	}
344574dba3adSMarcin Wojtas 
3446d12f7bfcSMarcin Wojtas 	counter_u64_add(tx_ring->tx_stats.missing_tx_comp, missed_tx);
3447d12f7bfcSMarcin Wojtas 
3448d12f7bfcSMarcin Wojtas 	return (rc);
344974dba3adSMarcin Wojtas }
345074dba3adSMarcin Wojtas 
34519b8d05b8SZbigniew Bodek /*
34529b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
34539b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
34549b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
34559b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
34569b8d05b8SZbigniew Bodek  */
34570bdffe59SMarcin Wojtas static void
3458d12f7bfcSMarcin Wojtas check_for_missing_completions(struct ena_adapter *adapter)
34599b8d05b8SZbigniew Bodek {
34609b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
3461d12f7bfcSMarcin Wojtas 	struct ena_ring *rx_ring;
346274dba3adSMarcin Wojtas 	int i, budget, rc;
34639b8d05b8SZbigniew Bodek 
34649b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
34659b8d05b8SZbigniew Bodek 	rmb();
34669b8d05b8SZbigniew Bodek 
34679b8d05b8SZbigniew Bodek 	if (!adapter->up)
34689b8d05b8SZbigniew Bodek 		return;
34699b8d05b8SZbigniew Bodek 
34709b8d05b8SZbigniew Bodek 	if (adapter->trigger_reset)
34719b8d05b8SZbigniew Bodek 		return;
34729b8d05b8SZbigniew Bodek 
347340621d71SMarcin Wojtas 	if (adapter->missing_tx_timeout == ENA_HW_HINTS_NO_TIMEOUT)
34749b8d05b8SZbigniew Bodek 		return;
34759b8d05b8SZbigniew Bodek 
34769b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
34779b8d05b8SZbigniew Bodek 
34789b8d05b8SZbigniew Bodek 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
34799b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
3480d12f7bfcSMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
34819b8d05b8SZbigniew Bodek 
3482d12f7bfcSMarcin Wojtas 		rc = check_missing_comp_in_tx_queue(adapter, tx_ring);
3483d12f7bfcSMarcin Wojtas 		if (unlikely(rc != 0))
3484d12f7bfcSMarcin Wojtas 			return;
3485d12f7bfcSMarcin Wojtas 
3486d12f7bfcSMarcin Wojtas 		rc = check_for_rx_interrupt_queue(adapter, rx_ring);
34870bdffe59SMarcin Wojtas 		if (unlikely(rc != 0))
34889b8d05b8SZbigniew Bodek 			return;
34899b8d05b8SZbigniew Bodek 
34909b8d05b8SZbigniew Bodek 		budget--;
3491cd5d5804SMarcin Wojtas 		if (budget == 0) {
34929b8d05b8SZbigniew Bodek 			i++;
34939b8d05b8SZbigniew Bodek 			break;
34949b8d05b8SZbigniew Bodek 		}
34959b8d05b8SZbigniew Bodek 	}
34969b8d05b8SZbigniew Bodek 
34979b8d05b8SZbigniew Bodek 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
34989b8d05b8SZbigniew Bodek }
34999b8d05b8SZbigniew Bodek 
3500efe6ab18SMarcin Wojtas /* trigger deferred rx cleanup after 2 consecutive detections */
3501efe6ab18SMarcin Wojtas #define EMPTY_RX_REFILL 2
3502efe6ab18SMarcin Wojtas /* For the rare case where the device runs out of Rx descriptors and the
3503efe6ab18SMarcin Wojtas  * msix handler failed to refill new Rx descriptors (due to a lack of memory
3504efe6ab18SMarcin Wojtas  * for example).
3505efe6ab18SMarcin Wojtas  * This case will lead to a deadlock:
3506efe6ab18SMarcin Wojtas  * The device won't send interrupts since all the new Rx packets will be dropped
3507efe6ab18SMarcin Wojtas  * The msix handler won't allocate new Rx descriptors so the device won't be
3508efe6ab18SMarcin Wojtas  * able to send new packets.
3509efe6ab18SMarcin Wojtas  *
3510efe6ab18SMarcin Wojtas  * When such a situation is detected - execute rx cleanup task in another thread
3511efe6ab18SMarcin Wojtas  */
3512efe6ab18SMarcin Wojtas static void
3513efe6ab18SMarcin Wojtas check_for_empty_rx_ring(struct ena_adapter *adapter)
3514efe6ab18SMarcin Wojtas {
3515efe6ab18SMarcin Wojtas 	struct ena_ring *rx_ring;
3516efe6ab18SMarcin Wojtas 	int i, refill_required;
3517efe6ab18SMarcin Wojtas 
3518efe6ab18SMarcin Wojtas 	if (!adapter->up)
3519efe6ab18SMarcin Wojtas 		return;
3520efe6ab18SMarcin Wojtas 
3521efe6ab18SMarcin Wojtas 	if (adapter->trigger_reset)
3522efe6ab18SMarcin Wojtas 		return;
3523efe6ab18SMarcin Wojtas 
3524efe6ab18SMarcin Wojtas 	for (i = 0; i < adapter->num_queues; i++) {
3525efe6ab18SMarcin Wojtas 		rx_ring = &adapter->rx_ring[i];
3526efe6ab18SMarcin Wojtas 
3527efe6ab18SMarcin Wojtas 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
3528efe6ab18SMarcin Wojtas 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
3529efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue++;
3530efe6ab18SMarcin Wojtas 
3531efe6ab18SMarcin Wojtas 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
3532efe6ab18SMarcin Wojtas 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
3533efe6ab18SMarcin Wojtas 				    1);
3534efe6ab18SMarcin Wojtas 
3535efe6ab18SMarcin Wojtas 				device_printf(adapter->pdev,
3536efe6ab18SMarcin Wojtas 				    "trigger refill for ring %d\n", i);
3537efe6ab18SMarcin Wojtas 
3538efe6ab18SMarcin Wojtas 				taskqueue_enqueue(rx_ring->cmpl_tq,
3539efe6ab18SMarcin Wojtas 				    &rx_ring->cmpl_task);
3540efe6ab18SMarcin Wojtas 				rx_ring->empty_rx_queue = 0;
3541efe6ab18SMarcin Wojtas 			}
3542efe6ab18SMarcin Wojtas 		} else {
3543efe6ab18SMarcin Wojtas 			rx_ring->empty_rx_queue = 0;
3544efe6ab18SMarcin Wojtas 		}
3545efe6ab18SMarcin Wojtas 	}
3546efe6ab18SMarcin Wojtas }
35479b8d05b8SZbigniew Bodek 
354840621d71SMarcin Wojtas static void ena_update_hints(struct ena_adapter *adapter,
354940621d71SMarcin Wojtas 			     struct ena_admin_ena_hw_hints *hints)
355040621d71SMarcin Wojtas {
355140621d71SMarcin Wojtas 	struct ena_com_dev *ena_dev = adapter->ena_dev;
355240621d71SMarcin Wojtas 
355340621d71SMarcin Wojtas 	if (hints->admin_completion_tx_timeout)
355440621d71SMarcin Wojtas 		ena_dev->admin_queue.completion_timeout =
355540621d71SMarcin Wojtas 		    hints->admin_completion_tx_timeout * 1000;
355640621d71SMarcin Wojtas 
355740621d71SMarcin Wojtas 	if (hints->mmio_read_timeout)
355840621d71SMarcin Wojtas 		/* convert to usec */
355940621d71SMarcin Wojtas 		ena_dev->mmio_read.reg_read_to =
356040621d71SMarcin Wojtas 		    hints->mmio_read_timeout * 1000;
356140621d71SMarcin Wojtas 
356240621d71SMarcin Wojtas 	if (hints->missed_tx_completion_count_threshold_to_reset)
356340621d71SMarcin Wojtas 		adapter->missing_tx_threshold =
356440621d71SMarcin Wojtas 		    hints->missed_tx_completion_count_threshold_to_reset;
356540621d71SMarcin Wojtas 
356640621d71SMarcin Wojtas 	if (hints->missing_tx_completion_timeout) {
356740621d71SMarcin Wojtas 		if (hints->missing_tx_completion_timeout ==
356840621d71SMarcin Wojtas 		     ENA_HW_HINTS_NO_TIMEOUT)
356940621d71SMarcin Wojtas 			adapter->missing_tx_timeout = ENA_HW_HINTS_NO_TIMEOUT;
357040621d71SMarcin Wojtas 		else
357140621d71SMarcin Wojtas 			adapter->missing_tx_timeout =
357240621d71SMarcin Wojtas 			    SBT_1MS * hints->missing_tx_completion_timeout;
357340621d71SMarcin Wojtas 	}
357440621d71SMarcin Wojtas 
357540621d71SMarcin Wojtas 	if (hints->driver_watchdog_timeout) {
357640621d71SMarcin Wojtas 		if (hints->driver_watchdog_timeout == ENA_HW_HINTS_NO_TIMEOUT)
357740621d71SMarcin Wojtas 			adapter->keep_alive_timeout = ENA_HW_HINTS_NO_TIMEOUT;
357840621d71SMarcin Wojtas 		else
357940621d71SMarcin Wojtas 			adapter->keep_alive_timeout =
358040621d71SMarcin Wojtas 			    SBT_1MS * hints->driver_watchdog_timeout;
358140621d71SMarcin Wojtas 	}
358240621d71SMarcin Wojtas }
358340621d71SMarcin Wojtas 
35849b8d05b8SZbigniew Bodek static void
35859b8d05b8SZbigniew Bodek ena_timer_service(void *data)
35869b8d05b8SZbigniew Bodek {
35879b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
35889b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
35899b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
35909b8d05b8SZbigniew Bodek 
35919b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
35929b8d05b8SZbigniew Bodek 
35939b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
35949b8d05b8SZbigniew Bodek 
3595d12f7bfcSMarcin Wojtas 	check_for_missing_completions(adapter);
35969b8d05b8SZbigniew Bodek 
3597efe6ab18SMarcin Wojtas 	check_for_empty_rx_ring(adapter);
3598efe6ab18SMarcin Wojtas 
35990bdffe59SMarcin Wojtas 	if (host_info != NULL)
36009b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
36019b8d05b8SZbigniew Bodek 
36029b8d05b8SZbigniew Bodek 	if (unlikely(adapter->trigger_reset)) {
36039b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
36049b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
36059b8d05b8SZbigniew Bodek 		return;
36069b8d05b8SZbigniew Bodek 	}
36079b8d05b8SZbigniew Bodek 
36089b8d05b8SZbigniew Bodek 	/*
36099b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
36109b8d05b8SZbigniew Bodek 	 */
36119b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
36129b8d05b8SZbigniew Bodek }
36139b8d05b8SZbigniew Bodek 
36149b8d05b8SZbigniew Bodek static void
36159b8d05b8SZbigniew Bodek ena_reset_task(void *arg, int pending)
36169b8d05b8SZbigniew Bodek {
36179b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
36189b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
36199b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
36209b8d05b8SZbigniew Bodek 	bool dev_up;
36219b8d05b8SZbigniew Bodek 	int rc;
36229b8d05b8SZbigniew Bodek 
36239b8d05b8SZbigniew Bodek 	if (unlikely(!adapter->trigger_reset)) {
36249b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36259b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
36269b8d05b8SZbigniew Bodek 		return;
36279b8d05b8SZbigniew Bodek 	}
36289b8d05b8SZbigniew Bodek 
36299b8d05b8SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
36309b8d05b8SZbigniew Bodek 
36319b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
36329b8d05b8SZbigniew Bodek 
36339b8d05b8SZbigniew Bodek 	dev_up = adapter->up;
36349b8d05b8SZbigniew Bodek 
36359b8d05b8SZbigniew Bodek 	ena_com_set_admin_running_state(ena_dev, false);
36369b8d05b8SZbigniew Bodek 	ena_down(adapter);
3637a195fab0SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
36389b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
36399b8d05b8SZbigniew Bodek 	ena_com_abort_admin_commands(ena_dev);
36409b8d05b8SZbigniew Bodek 	ena_com_wait_for_abort_completion(ena_dev);
36419b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36429b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
36439b8d05b8SZbigniew Bodek 
3644a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
36459b8d05b8SZbigniew Bodek 	adapter->trigger_reset = false;
36469b8d05b8SZbigniew Bodek 
36479b8d05b8SZbigniew Bodek 	/* Finished destroy part. Restart the device */
36489b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
36499b8d05b8SZbigniew Bodek 	    &adapter->wd_active);
36503f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36519b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36529b8d05b8SZbigniew Bodek 		    "ENA device init failed! (err: %d)\n", rc);
36539b8d05b8SZbigniew Bodek 		goto err_dev_free;
36549b8d05b8SZbigniew Bodek 	}
36559b8d05b8SZbigniew Bodek 
36569b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
36579b8d05b8SZbigniew Bodek 	    adapter->num_queues);
36583f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
36599b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Enable MSI-X failed\n");
36609b8d05b8SZbigniew Bodek 		goto err_com_free;
36619b8d05b8SZbigniew Bodek 	}
36629b8d05b8SZbigniew Bodek 
36639b8d05b8SZbigniew Bodek 	/* If the interface was up before the reset bring it up */
36649b8d05b8SZbigniew Bodek 	if (dev_up) {
36659b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
36663f9ed7abSMarcin Wojtas 		if (unlikely(rc != 0)) {
36679b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
36689b8d05b8SZbigniew Bodek 			    "Failed to create I/O queues\n");
36699b8d05b8SZbigniew Bodek 			goto err_msix_free;
36709b8d05b8SZbigniew Bodek 		}
36719b8d05b8SZbigniew Bodek 	}
36729b8d05b8SZbigniew Bodek 
36739b8d05b8SZbigniew Bodek 	callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
36749b8d05b8SZbigniew Bodek 	    ena_timer_service, (void *)adapter, 0);
36759b8d05b8SZbigniew Bodek 
36769b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
36779b8d05b8SZbigniew Bodek 
36789b8d05b8SZbigniew Bodek 	return;
36799b8d05b8SZbigniew Bodek 
36809b8d05b8SZbigniew Bodek err_msix_free:
36819b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
36829b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
36839b8d05b8SZbigniew Bodek err_com_free:
36849b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36859b8d05b8SZbigniew Bodek err_dev_free:
36869b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "ENA reset failed!\n");
36879b8d05b8SZbigniew Bodek 	adapter->running = false;
36889b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
36899b8d05b8SZbigniew Bodek }
36909b8d05b8SZbigniew Bodek 
36919b8d05b8SZbigniew Bodek /**
36929b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
36939b8d05b8SZbigniew Bodek  * @pdev: device information struct
36949b8d05b8SZbigniew Bodek  *
36959b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
36969b8d05b8SZbigniew Bodek  *
36979b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
36989b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
36999b8d05b8SZbigniew Bodek  * and a hardware reset occur.
37009b8d05b8SZbigniew Bodek  **/
37019b8d05b8SZbigniew Bodek static int
37029b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
37039b8d05b8SZbigniew Bodek {
37049b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
37059b8d05b8SZbigniew Bodek 	static int version_printed;
37069b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
37079b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
37089b8d05b8SZbigniew Bodek 	uint16_t tx_sgl_size = 0;
37099b8d05b8SZbigniew Bodek 	uint16_t rx_sgl_size = 0;
37109b8d05b8SZbigniew Bodek 	int io_queue_num;
37119b8d05b8SZbigniew Bodek 	int queue_size;
37129b8d05b8SZbigniew Bodek 	int rc;
37139b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
37149b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
37159b8d05b8SZbigniew Bodek 
37169b8d05b8SZbigniew Bodek 	mtx_init(&adapter->global_mtx, "ENA global mtx", NULL, MTX_DEF);
37179b8d05b8SZbigniew Bodek 	sx_init(&adapter->ioctl_sx, "ENA ioctl sx");
37189b8d05b8SZbigniew Bodek 
37199b8d05b8SZbigniew Bodek 	/* Set up the timer service */
37209b8d05b8SZbigniew Bodek 	callout_init_mtx(&adapter->timer_service, &adapter->global_mtx, 0);
37219b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
37229b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
37239b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
37249b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
37259b8d05b8SZbigniew Bodek 
37269b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
37279b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
37289b8d05b8SZbigniew Bodek 
37299b8d05b8SZbigniew Bodek 	rc = ena_allocate_pci_resources(adapter);
37303f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37319b8d05b8SZbigniew Bodek 		device_printf(pdev, "PCI resource allocation failed!\n");
37329b8d05b8SZbigniew Bodek 		ena_free_pci_resources(adapter);
3733cd5d5804SMarcin Wojtas 		return (rc);
37349b8d05b8SZbigniew Bodek 	}
37359b8d05b8SZbigniew Bodek 
37369b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3737cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3738cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
37399b8d05b8SZbigniew Bodek 
37409b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
37419b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
37429b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
37439b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
37449b8d05b8SZbigniew Bodek 
37459b8d05b8SZbigniew Bodek 	/* Store register resources */
37469b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
37479b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
37489b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
37499b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
37509b8d05b8SZbigniew Bodek 
37513f9ed7abSMarcin Wojtas 	if (unlikely(((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0)) {
37529b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
37539b8d05b8SZbigniew Bodek 		rc = ENXIO;
3754cd5d5804SMarcin Wojtas 		goto err_bus_free;
37559b8d05b8SZbigniew Bodek 	}
37569b8d05b8SZbigniew Bodek 
37579b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
37589b8d05b8SZbigniew Bodek 
37599b8d05b8SZbigniew Bodek 	/* Device initialization */
37609b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
37613f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
37629b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
37639b8d05b8SZbigniew Bodek 		rc = ENXIO;
37649b8d05b8SZbigniew Bodek 		goto err_bus_free;
37659b8d05b8SZbigniew Bodek 	}
37669b8d05b8SZbigniew Bodek 
37679b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
37689b8d05b8SZbigniew Bodek 
37699b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
37709b8d05b8SZbigniew Bodek 
37719b8d05b8SZbigniew Bodek 	/* Set for sure that interface is not up */
37729b8d05b8SZbigniew Bodek 	adapter->up = false;
37739b8d05b8SZbigniew Bodek 
37749b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
37759b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
37769b8d05b8SZbigniew Bodek 
37779b8d05b8SZbigniew Bodek 	/* calculate IO queue number to create */
37789b8d05b8SZbigniew Bodek 	io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx);
37799b8d05b8SZbigniew Bodek 
37809b8d05b8SZbigniew Bodek 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
37819b8d05b8SZbigniew Bodek 	    io_queue_num);
37829b8d05b8SZbigniew Bodek 	adapter->num_queues = io_queue_num;
37839b8d05b8SZbigniew Bodek 
37843cfadb28SMarcin Wojtas 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
37853cfadb28SMarcin Wojtas 
37869b8d05b8SZbigniew Bodek 	/* calculatre ring sizes */
37879b8d05b8SZbigniew Bodek 	queue_size = ena_calc_queue_size(adapter,&tx_sgl_size,
37889b8d05b8SZbigniew Bodek 	    &rx_sgl_size, &get_feat_ctx);
37893f9ed7abSMarcin Wojtas 	if (unlikely((queue_size <= 0) || (io_queue_num <= 0))) {
37909b8d05b8SZbigniew Bodek 		rc = ENA_COM_FAULT;
37919b8d05b8SZbigniew Bodek 		goto err_com_free;
37929b8d05b8SZbigniew Bodek 	}
37939b8d05b8SZbigniew Bodek 
3794a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3795a195fab0SMarcin Wojtas 
37969b8d05b8SZbigniew Bodek 	adapter->tx_ring_size = queue_size;
37979b8d05b8SZbigniew Bodek 	adapter->rx_ring_size = queue_size;
37989b8d05b8SZbigniew Bodek 
37999b8d05b8SZbigniew Bodek 	adapter->max_tx_sgl_size = tx_sgl_size;
38009b8d05b8SZbigniew Bodek 	adapter->max_rx_sgl_size = rx_sgl_size;
38019b8d05b8SZbigniew Bodek 
38029b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
38039b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
38044e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
38054e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create TX DMA tag\n");
3806cd5d5804SMarcin Wojtas 		goto err_com_free;
38074e8acd84SMarcin Wojtas 	}
38089b8d05b8SZbigniew Bodek 
38099b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
38104e8acd84SMarcin Wojtas 	if (unlikely(rc != 0)) {
38114e8acd84SMarcin Wojtas 		device_printf(pdev, "Failed to create RX DMA tag\n");
3812cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
38134e8acd84SMarcin Wojtas 	}
38149b8d05b8SZbigniew Bodek 
38159b8d05b8SZbigniew Bodek 	/* initialize rings basic information */
38169b8d05b8SZbigniew Bodek 	device_printf(pdev, "initalize %d io queues\n", io_queue_num);
3817cd5d5804SMarcin Wojtas 	ena_init_io_rings(adapter);
38189b8d05b8SZbigniew Bodek 
38199b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
38203f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0)) {
38219b8d05b8SZbigniew Bodek 		device_printf(pdev,
38229b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
3823c115a1e2SMarcin Wojtas 		goto err_io_free;
3824c115a1e2SMarcin Wojtas 	}
3825c115a1e2SMarcin Wojtas 
3826c115a1e2SMarcin Wojtas 	/* setup network interface */
3827c115a1e2SMarcin Wojtas 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
3828c115a1e2SMarcin Wojtas 	if (unlikely(rc != 0)) {
3829c115a1e2SMarcin Wojtas 		device_printf(pdev, "Error with network interface setup\n");
3830c115a1e2SMarcin Wojtas 		goto err_msix_free;
38319b8d05b8SZbigniew Bodek 	}
38329b8d05b8SZbigniew Bodek 
3833081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3834081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3835081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3836081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3837081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3838081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3839081169f2SZbigniew Bodek 
38409b8d05b8SZbigniew Bodek 	/* Initialize statistics */
38419b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
38429b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
384330217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
384430217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
38459b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
38469b8d05b8SZbigniew Bodek 
38479b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
38489b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
38499b8d05b8SZbigniew Bodek 
38509b8d05b8SZbigniew Bodek 	adapter->running = true;
38519b8d05b8SZbigniew Bodek 	return (0);
38529b8d05b8SZbigniew Bodek 
3853c115a1e2SMarcin Wojtas err_msix_free:
3854c115a1e2SMarcin Wojtas 	ena_com_dev_reset(adapter->ena_dev, ENA_REGS_RESET_INIT_ERR);
3855c115a1e2SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
3856c115a1e2SMarcin Wojtas 	ena_disable_msix(adapter);
3857cd5d5804SMarcin Wojtas err_io_free:
38589b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
38599b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3860cd5d5804SMarcin Wojtas err_tx_tag_free:
38619b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3862cd5d5804SMarcin Wojtas err_com_free:
38639b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
38649b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
3865cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
38669b8d05b8SZbigniew Bodek err_bus_free:
38679b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
38689b8d05b8SZbigniew Bodek 	free(ena_dev, M_DEVBUF);
38699b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
3870cd5d5804SMarcin Wojtas 
38719b8d05b8SZbigniew Bodek 	return (rc);
38729b8d05b8SZbigniew Bodek }
38739b8d05b8SZbigniew Bodek 
38749b8d05b8SZbigniew Bodek /**
38759b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
38769b8d05b8SZbigniew Bodek  * @pdev: device information struct
38779b8d05b8SZbigniew Bodek  *
38789b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
38799b8d05b8SZbigniew Bodek  * that it should release a PCI device.
38809b8d05b8SZbigniew Bodek  **/
38819b8d05b8SZbigniew Bodek static int
38829b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
38839b8d05b8SZbigniew Bodek {
38849b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
38859b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
38869b8d05b8SZbigniew Bodek 	int rc;
38879b8d05b8SZbigniew Bodek 
38889b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
38899b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
38909b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
38919b8d05b8SZbigniew Bodek 		return (EBUSY);
38929b8d05b8SZbigniew Bodek 	}
38939b8d05b8SZbigniew Bodek 
38949151c55dSMarcin Wojtas 	ether_ifdetach(adapter->ifp);
38959151c55dSMarcin Wojtas 
38969b8d05b8SZbigniew Bodek 	/* Free reset task and callout */
38979b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
38989b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
38999b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
39009b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
39019b8d05b8SZbigniew Bodek 
3902e67c6554SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
39039b8d05b8SZbigniew Bodek 	ena_down(adapter);
3904e67c6554SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
39059b8d05b8SZbigniew Bodek 
39069b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
39079b8d05b8SZbigniew Bodek 
390830217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
390930217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
39109b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
39119b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
39129b8d05b8SZbigniew Bodek 
39133f9ed7abSMarcin Wojtas 	if (likely(adapter->rss_support))
39149b8d05b8SZbigniew Bodek 		ena_com_rss_destroy(ena_dev);
39159b8d05b8SZbigniew Bodek 
39169b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
39173f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
39189b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
39199b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
39209b8d05b8SZbigniew Bodek 
39219b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
39223f9ed7abSMarcin Wojtas 	if (unlikely(rc != 0))
39239b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
39249b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
39259b8d05b8SZbigniew Bodek 
39269b8d05b8SZbigniew Bodek 	/* Reset the device only if the device is running. */
39279b8d05b8SZbigniew Bodek 	if (adapter->running)
3928a195fab0SMarcin Wojtas 		ena_com_dev_reset(ena_dev, adapter->reset_reason);
39299b8d05b8SZbigniew Bodek 
39309b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
39319b8d05b8SZbigniew Bodek 
39329b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
39339b8d05b8SZbigniew Bodek 
3934197f0284SMarcin Wojtas 	ena_com_abort_admin_commands(ena_dev);
3935197f0284SMarcin Wojtas 
3936197f0284SMarcin Wojtas 	ena_com_wait_for_abort_completion(ena_dev);
3937197f0284SMarcin Wojtas 
3938197f0284SMarcin Wojtas 	ena_com_admin_destroy(ena_dev);
3939197f0284SMarcin Wojtas 
39409b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
39419b8d05b8SZbigniew Bodek 
39429b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
39439b8d05b8SZbigniew Bodek 
39449b8d05b8SZbigniew Bodek 	mtx_destroy(&adapter->global_mtx);
39459b8d05b8SZbigniew Bodek 	sx_destroy(&adapter->ioctl_sx);
39469b8d05b8SZbigniew Bodek 
39479151c55dSMarcin Wojtas 	if_free(adapter->ifp);
39489151c55dSMarcin Wojtas 
39499b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
39509b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
39519b8d05b8SZbigniew Bodek 
39529b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
39539b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
39549b8d05b8SZbigniew Bodek 
39559b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
39569b8d05b8SZbigniew Bodek }
39579b8d05b8SZbigniew Bodek 
39589b8d05b8SZbigniew Bodek /******************************************************************************
39599b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
39609b8d05b8SZbigniew Bodek  *****************************************************************************/
39619b8d05b8SZbigniew Bodek /**
39629b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
39639b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
39649b8d05b8SZbigniew Bodek  **/
39659b8d05b8SZbigniew Bodek static void
39669b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
39679b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
39689b8d05b8SZbigniew Bodek {
39699b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
39709b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
39719b8d05b8SZbigniew Bodek 	int status;
39729b8d05b8SZbigniew Bodek 	if_t ifp;
39739b8d05b8SZbigniew Bodek 
39749b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
39759b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
39769b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
39779b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
39789b8d05b8SZbigniew Bodek 
39799b8d05b8SZbigniew Bodek 	if (status != 0) {
39809b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
39819b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_UP);
39829b8d05b8SZbigniew Bodek 	} else if (status == 0) {
39839b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
39849b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
39859b8d05b8SZbigniew Bodek 	} else {
39869b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "invalid value recvd\n");
39879b8d05b8SZbigniew Bodek 		BUG();
39889b8d05b8SZbigniew Bodek 	}
39899b8d05b8SZbigniew Bodek 
39909b8d05b8SZbigniew Bodek 	adapter->link_status = status;
39919b8d05b8SZbigniew Bodek }
39929b8d05b8SZbigniew Bodek 
399340621d71SMarcin Wojtas static void ena_notification(void *adapter_data,
399440621d71SMarcin Wojtas     struct ena_admin_aenq_entry *aenq_e)
399540621d71SMarcin Wojtas {
399640621d71SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
399740621d71SMarcin Wojtas 	struct ena_admin_ena_hw_hints *hints;
399840621d71SMarcin Wojtas 
399940621d71SMarcin Wojtas 	ENA_WARN(aenq_e->aenq_common_desc.group != ENA_ADMIN_NOTIFICATION,
400040621d71SMarcin Wojtas 	    "Invalid group(%x) expected %x\n",	aenq_e->aenq_common_desc.group,
400140621d71SMarcin Wojtas 	    ENA_ADMIN_NOTIFICATION);
400240621d71SMarcin Wojtas 
400340621d71SMarcin Wojtas 	switch (aenq_e->aenq_common_desc.syndrom) {
400440621d71SMarcin Wojtas 	case ENA_ADMIN_UPDATE_HINTS:
400540621d71SMarcin Wojtas 		hints =
400640621d71SMarcin Wojtas 		    (struct ena_admin_ena_hw_hints *)(&aenq_e->inline_data_w4);
400740621d71SMarcin Wojtas 		ena_update_hints(adapter, hints);
400840621d71SMarcin Wojtas 		break;
400940621d71SMarcin Wojtas 	default:
401040621d71SMarcin Wojtas 		device_printf(adapter->pdev,
401140621d71SMarcin Wojtas 		    "Invalid aenq notification link state %d\n",
401240621d71SMarcin Wojtas 		    aenq_e->aenq_common_desc.syndrom);
401340621d71SMarcin Wojtas 	}
401440621d71SMarcin Wojtas }
401540621d71SMarcin Wojtas 
40169b8d05b8SZbigniew Bodek /**
40179b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
40189b8d05b8SZbigniew Bodek  **/
40199b8d05b8SZbigniew Bodek static void
4020e6de9a83SMarcin Wojtas unimplemented_aenq_handler(void *adapter_data,
40219b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
40229b8d05b8SZbigniew Bodek {
4023e6de9a83SMarcin Wojtas 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
4024e6de9a83SMarcin Wojtas 
4025e6de9a83SMarcin Wojtas 	device_printf(adapter->pdev,
4026e6de9a83SMarcin Wojtas 	    "Unknown event was received or event with unimplemented handler\n");
40279b8d05b8SZbigniew Bodek }
40289b8d05b8SZbigniew Bodek 
40299b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
40309b8d05b8SZbigniew Bodek     .handlers = {
40319b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
403240621d71SMarcin Wojtas 	    [ENA_ADMIN_NOTIFICATION] = ena_notification,
40339b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
40349b8d05b8SZbigniew Bodek     },
40359b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
40369b8d05b8SZbigniew Bodek };
40379b8d05b8SZbigniew Bodek 
40389b8d05b8SZbigniew Bodek /*********************************************************************
40399b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
40409b8d05b8SZbigniew Bodek  *********************************************************************/
40419b8d05b8SZbigniew Bodek 
40429b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
40439b8d05b8SZbigniew Bodek     /* Device interface */
40449b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
40459b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
40469b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
40479b8d05b8SZbigniew Bodek     DEVMETHOD_END
40489b8d05b8SZbigniew Bodek };
40499b8d05b8SZbigniew Bodek 
40509b8d05b8SZbigniew Bodek static driver_t ena_driver = {
40519b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
40529b8d05b8SZbigniew Bodek };
40539b8d05b8SZbigniew Bodek 
40549b8d05b8SZbigniew Bodek devclass_t ena_devclass;
40559b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
405640abe76bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, ena, ena_vendor_info_array,
4057329e817fSWarner Losh     nitems(ena_vendor_info_array) - 1);
40589b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
40599b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
40609b8d05b8SZbigniew Bodek 
40619b8d05b8SZbigniew Bodek /*********************************************************************/
4062