xref: /freebsd/sys/dev/ena/ena.c (revision fceb93871598b5f6779a21e3ae3e380694289aea)
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/rss_config.h>
639b8d05b8SZbigniew Bodek #include <net/if_types.h>
649b8d05b8SZbigniew Bodek #include <net/if_vlan_var.h>
659b8d05b8SZbigniew Bodek 
669b8d05b8SZbigniew Bodek #include <netinet/in_rss.h>
679b8d05b8SZbigniew Bodek #include <netinet/in_systm.h>
689b8d05b8SZbigniew Bodek #include <netinet/in.h>
699b8d05b8SZbigniew Bodek #include <netinet/if_ether.h>
709b8d05b8SZbigniew Bodek #include <netinet/ip.h>
719b8d05b8SZbigniew Bodek #include <netinet/ip6.h>
729b8d05b8SZbigniew Bodek #include <netinet/tcp.h>
739b8d05b8SZbigniew Bodek #include <netinet/udp.h>
749b8d05b8SZbigniew Bodek 
759b8d05b8SZbigniew Bodek #include <dev/pci/pcivar.h>
769b8d05b8SZbigniew Bodek #include <dev/pci/pcireg.h>
779b8d05b8SZbigniew Bodek 
789b8d05b8SZbigniew Bodek #include "ena.h"
799b8d05b8SZbigniew Bodek #include "ena_sysctl.h"
809b8d05b8SZbigniew Bodek 
819b8d05b8SZbigniew Bodek /*********************************************************
829b8d05b8SZbigniew Bodek  *  Function prototypes
839b8d05b8SZbigniew Bodek  *********************************************************/
849b8d05b8SZbigniew Bodek static int	ena_probe(device_t);
859b8d05b8SZbigniew Bodek static void	ena_intr_msix_mgmnt(void *);
869b8d05b8SZbigniew Bodek static int	ena_allocate_pci_resources(struct ena_adapter*);
879b8d05b8SZbigniew Bodek static void	ena_free_pci_resources(struct ena_adapter *);
889b8d05b8SZbigniew Bodek static int	ena_change_mtu(if_t, int);
899b8d05b8SZbigniew Bodek static inline void ena_alloc_counters(counter_u64_t *, int);
909b8d05b8SZbigniew Bodek static inline void ena_free_counters(counter_u64_t *, int);
919b8d05b8SZbigniew Bodek static inline void ena_reset_counters(counter_u64_t *, int);
929b8d05b8SZbigniew Bodek static void	ena_init_io_rings_common(struct ena_adapter *,
939b8d05b8SZbigniew Bodek     struct ena_ring *, uint16_t);
94cd5d5804SMarcin Wojtas static void	ena_init_io_rings(struct ena_adapter *);
959b8d05b8SZbigniew Bodek static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
969b8d05b8SZbigniew Bodek static void	ena_free_all_io_rings_resources(struct ena_adapter *);
979b8d05b8SZbigniew Bodek static int	ena_setup_tx_dma_tag(struct ena_adapter *);
989b8d05b8SZbigniew Bodek static int	ena_free_tx_dma_tag(struct ena_adapter *);
999b8d05b8SZbigniew Bodek static int	ena_setup_rx_dma_tag(struct ena_adapter *);
1009b8d05b8SZbigniew Bodek static int	ena_free_rx_dma_tag(struct ena_adapter *);
1019b8d05b8SZbigniew Bodek static int	ena_setup_tx_resources(struct ena_adapter *, int);
1029b8d05b8SZbigniew Bodek static void	ena_free_tx_resources(struct ena_adapter *, int);
1039b8d05b8SZbigniew Bodek static int	ena_setup_all_tx_resources(struct ena_adapter *);
1049b8d05b8SZbigniew Bodek static void	ena_free_all_tx_resources(struct ena_adapter *);
1059b8d05b8SZbigniew Bodek static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
1069b8d05b8SZbigniew Bodek static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
1079b8d05b8SZbigniew Bodek static int	ena_setup_all_rx_resources(struct ena_adapter *);
1089b8d05b8SZbigniew Bodek static void	ena_free_all_rx_resources(struct ena_adapter *);
1099b8d05b8SZbigniew Bodek static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1109b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1119b8d05b8SZbigniew Bodek static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
1129b8d05b8SZbigniew Bodek     struct ena_rx_buffer *);
1139b8d05b8SZbigniew Bodek static int	ena_refill_rx_bufs(struct ena_ring *, uint32_t);
1149b8d05b8SZbigniew Bodek static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
1159b8d05b8SZbigniew Bodek static void	ena_refill_all_rx_bufs(struct ena_adapter *);
1169b8d05b8SZbigniew Bodek static void	ena_free_all_rx_bufs(struct ena_adapter *);
1179b8d05b8SZbigniew Bodek static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
1189b8d05b8SZbigniew Bodek static void	ena_free_all_tx_bufs(struct ena_adapter *);
1199b8d05b8SZbigniew Bodek static void	ena_destroy_all_tx_queues(struct ena_adapter *);
1209b8d05b8SZbigniew Bodek static void	ena_destroy_all_rx_queues(struct ena_adapter *);
1219b8d05b8SZbigniew Bodek static void	ena_destroy_all_io_queues(struct ena_adapter *);
1229b8d05b8SZbigniew Bodek static int	ena_create_io_queues(struct ena_adapter *);
1239b8d05b8SZbigniew Bodek static int	ena_tx_cleanup(struct ena_ring *);
1249b8d05b8SZbigniew Bodek static int	ena_rx_cleanup(struct ena_ring *);
125*fceb9387SMarcin 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 
1949b8d05b8SZbigniew Bodek 
1959b8d05b8SZbigniew Bodek static ena_vendor_info_t ena_vendor_info_array[] = {
1969b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
1979b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
1989b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
1999b8d05b8SZbigniew Bodek     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
2009b8d05b8SZbigniew Bodek     /* Last entry */
2019b8d05b8SZbigniew Bodek     { 0, 0, 0 }
2029b8d05b8SZbigniew Bodek };
2039b8d05b8SZbigniew Bodek 
2049b8d05b8SZbigniew Bodek /*
2059b8d05b8SZbigniew Bodek  * Contains pointers to event handlers, e.g. link state chage.
2069b8d05b8SZbigniew Bodek  */
2079b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers;
2089b8d05b8SZbigniew Bodek 
2099b8d05b8SZbigniew Bodek void
2109b8d05b8SZbigniew Bodek ena_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2119b8d05b8SZbigniew Bodek {
2129b8d05b8SZbigniew Bodek 	if (error)
2139b8d05b8SZbigniew Bodek 		return;
2149b8d05b8SZbigniew Bodek 	*(bus_addr_t *) arg = segs[0].ds_addr;
2159b8d05b8SZbigniew Bodek 	return;
2169b8d05b8SZbigniew Bodek }
2179b8d05b8SZbigniew Bodek 
2189b8d05b8SZbigniew Bodek int
2199b8d05b8SZbigniew Bodek ena_dma_alloc(device_t dmadev, bus_size_t size,
2209b8d05b8SZbigniew Bodek     ena_mem_handle_t *dma , int mapflags)
2219b8d05b8SZbigniew Bodek {
2229b8d05b8SZbigniew Bodek 	struct ena_adapter* adapter = device_get_softc(dmadev);
2239b8d05b8SZbigniew Bodek 	uint32_t maxsize = ((size - 1)/PAGE_SIZE + 1) * PAGE_SIZE;
2249b8d05b8SZbigniew Bodek 	uint64_t dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
2259b8d05b8SZbigniew Bodek 	int error;
2269b8d05b8SZbigniew Bodek 
2279b8d05b8SZbigniew Bodek 	if (dma_space_addr == 0)
2289b8d05b8SZbigniew Bodek 		dma_space_addr = BUS_SPACE_MAXADDR;
2299b8d05b8SZbigniew Bodek 	error = bus_dma_tag_create(bus_get_dma_tag(dmadev), /* parent */
2309b8d05b8SZbigniew Bodek 	    8, 0,	      /* alignment, bounds 		*/
2318a573700SZbigniew Bodek 	    dma_space_addr,   /* lowaddr of exclusion window	*/
2328a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR,/* highaddr of exclusion window	*/
2339b8d05b8SZbigniew Bodek 	    NULL, NULL,	      /* filter, filterarg 		*/
2349b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsize 			*/
2359b8d05b8SZbigniew Bodek 	    1,		      /* nsegments 			*/
2369b8d05b8SZbigniew Bodek 	    maxsize,	      /* maxsegsize 			*/
2379b8d05b8SZbigniew Bodek 	    BUS_DMA_ALLOCNOW, /* flags 				*/
2389b8d05b8SZbigniew Bodek 	    NULL,	      /* lockfunc 			*/
2399b8d05b8SZbigniew Bodek 	    NULL,	      /* lockarg 			*/
2409b8d05b8SZbigniew Bodek 	    &dma->tag);
2419b8d05b8SZbigniew Bodek 	if (error) {
2429b8d05b8SZbigniew Bodek 		device_printf(dmadev,
2439b8d05b8SZbigniew Bodek 		"%s: bus_dma_tag_create failed: %d\n",
2449b8d05b8SZbigniew Bodek 		__func__, error);
2459b8d05b8SZbigniew Bodek 		goto fail_tag;
2469b8d05b8SZbigniew Bodek 	}
2479b8d05b8SZbigniew Bodek 
2489b8d05b8SZbigniew Bodek 	error = bus_dmamem_alloc(dma->tag, (void**) &dma->vaddr,
2499b8d05b8SZbigniew Bodek 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->map);
2509b8d05b8SZbigniew Bodek 	if (error) {
2519b8d05b8SZbigniew Bodek 		device_printf(dmadev,
2529b8d05b8SZbigniew Bodek 		"%s: bus_dmamem_alloc(%ju) failed: %d\n",
2539b8d05b8SZbigniew Bodek 		__func__, (uintmax_t)size, error);
2549b8d05b8SZbigniew Bodek 		goto fail_map_create;
2559b8d05b8SZbigniew Bodek 	}
2569b8d05b8SZbigniew Bodek 
2579b8d05b8SZbigniew Bodek 	dma->paddr = 0;
2589b8d05b8SZbigniew Bodek 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
2599b8d05b8SZbigniew Bodek 	    size, ena_dmamap_callback, &dma->paddr, mapflags);
2609b8d05b8SZbigniew Bodek 	if (error || dma->paddr == 0) {
2619b8d05b8SZbigniew Bodek 		device_printf(dmadev,
2629b8d05b8SZbigniew Bodek 		"%s: bus_dmamap_load failed: %d\n",
2639b8d05b8SZbigniew Bodek 		__func__, error);
2649b8d05b8SZbigniew Bodek 		goto fail_map_load;
2659b8d05b8SZbigniew Bodek 	}
2669b8d05b8SZbigniew Bodek 
2679b8d05b8SZbigniew Bodek 	return (0);
2689b8d05b8SZbigniew Bodek 
2699b8d05b8SZbigniew Bodek fail_map_load:
2709b8d05b8SZbigniew Bodek 	bus_dmamap_unload(dma->tag, dma->map);
2719b8d05b8SZbigniew Bodek fail_map_create:
2729b8d05b8SZbigniew Bodek 	bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
2739b8d05b8SZbigniew Bodek 	bus_dma_tag_destroy(dma->tag);
2749b8d05b8SZbigniew Bodek fail_tag:
2759b8d05b8SZbigniew Bodek 	dma->tag = NULL;
2769b8d05b8SZbigniew Bodek 
2779b8d05b8SZbigniew Bodek 	return (error);
2789b8d05b8SZbigniew Bodek }
2799b8d05b8SZbigniew Bodek 
2809b8d05b8SZbigniew Bodek static int
2819b8d05b8SZbigniew Bodek ena_allocate_pci_resources(struct ena_adapter* adapter)
2829b8d05b8SZbigniew Bodek {
2839b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
2849b8d05b8SZbigniew Bodek 	int rid;
2859b8d05b8SZbigniew Bodek 
2869b8d05b8SZbigniew Bodek 	rid = PCIR_BAR(ENA_REG_BAR);
2879b8d05b8SZbigniew Bodek 	adapter->memory = NULL;
2889b8d05b8SZbigniew Bodek 	adapter->registers = bus_alloc_resource_any(pdev, SYS_RES_MEMORY,
2899b8d05b8SZbigniew Bodek 	    &rid, RF_ACTIVE);
2909b8d05b8SZbigniew Bodek 	if (adapter->registers == NULL) {
2919b8d05b8SZbigniew Bodek 		device_printf(pdev, "Unable to allocate bus resource: "
2929b8d05b8SZbigniew Bodek 		    "registers\n");
2939b8d05b8SZbigniew Bodek 		return (ENXIO);
2949b8d05b8SZbigniew Bodek 	}
2959b8d05b8SZbigniew Bodek 
2969b8d05b8SZbigniew Bodek 	return (0);
2979b8d05b8SZbigniew Bodek }
2989b8d05b8SZbigniew Bodek 
2999b8d05b8SZbigniew Bodek static void
3009b8d05b8SZbigniew Bodek ena_free_pci_resources(struct ena_adapter *adapter)
3019b8d05b8SZbigniew Bodek {
3029b8d05b8SZbigniew Bodek 	device_t pdev = adapter->pdev;
3039b8d05b8SZbigniew Bodek 
3049b8d05b8SZbigniew Bodek 	if (adapter->memory != NULL) {
3059b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3069b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_MEM_BAR), adapter->memory);
3079b8d05b8SZbigniew Bodek 	}
3089b8d05b8SZbigniew Bodek 
3099b8d05b8SZbigniew Bodek 	if (adapter->registers != NULL) {
3109b8d05b8SZbigniew Bodek 		bus_release_resource(pdev, SYS_RES_MEMORY,
3119b8d05b8SZbigniew Bodek 		    PCIR_BAR(ENA_REG_BAR), adapter->registers);
3129b8d05b8SZbigniew Bodek 	}
3139b8d05b8SZbigniew Bodek 
3149b8d05b8SZbigniew Bodek 	return;
3159b8d05b8SZbigniew Bodek }
3169b8d05b8SZbigniew Bodek 
3179b8d05b8SZbigniew Bodek static int
3189b8d05b8SZbigniew Bodek ena_probe(device_t dev)
3199b8d05b8SZbigniew Bodek {
3209b8d05b8SZbigniew Bodek 	ena_vendor_info_t *ent;
3219b8d05b8SZbigniew Bodek 	char		adapter_name[60];
3229b8d05b8SZbigniew Bodek 	uint16_t	pci_vendor_id = 0;
3239b8d05b8SZbigniew Bodek 	uint16_t	pci_device_id = 0;
3249b8d05b8SZbigniew Bodek 
3259b8d05b8SZbigniew Bodek 	pci_vendor_id = pci_get_vendor(dev);
3269b8d05b8SZbigniew Bodek 	pci_device_id = pci_get_device(dev);
3279b8d05b8SZbigniew Bodek 
3289b8d05b8SZbigniew Bodek 	ent = ena_vendor_info_array;
3299b8d05b8SZbigniew Bodek 	while (ent->vendor_id != 0) {
3309b8d05b8SZbigniew Bodek 		if ((pci_vendor_id == ent->vendor_id) &&
3319b8d05b8SZbigniew Bodek 		    (pci_device_id == ent->device_id)) {
3329b8d05b8SZbigniew Bodek 			ena_trace(ENA_DBG, "vendor=%x device=%x ",
3339b8d05b8SZbigniew Bodek 			    pci_vendor_id, pci_device_id);
3349b8d05b8SZbigniew Bodek 
3359b8d05b8SZbigniew Bodek 			sprintf(adapter_name, DEVICE_DESC);
3369b8d05b8SZbigniew Bodek 			device_set_desc_copy(dev, adapter_name);
3379b8d05b8SZbigniew Bodek 			return (BUS_PROBE_DEFAULT);
3389b8d05b8SZbigniew Bodek 		}
3399b8d05b8SZbigniew Bodek 
3409b8d05b8SZbigniew Bodek 		ent++;
3419b8d05b8SZbigniew Bodek 
3429b8d05b8SZbigniew Bodek 	}
3439b8d05b8SZbigniew Bodek 
3449b8d05b8SZbigniew Bodek 	return (ENXIO);
3459b8d05b8SZbigniew Bodek }
3469b8d05b8SZbigniew Bodek 
3479b8d05b8SZbigniew Bodek static int
3489b8d05b8SZbigniew Bodek ena_change_mtu(if_t ifp, int new_mtu)
3499b8d05b8SZbigniew Bodek {
3509b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
3519b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
3529b8d05b8SZbigniew Bodek 	int rc, old_mtu, max_frame;
3539b8d05b8SZbigniew Bodek 
3549b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(adapter->ena_dev, &get_feat_ctx);
3559b8d05b8SZbigniew Bodek 	if (rc) {
3569b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
3579b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device\n");
3589b8d05b8SZbigniew Bodek 		return (ENXIO);
3599b8d05b8SZbigniew Bodek 	}
3609b8d05b8SZbigniew Bodek 
3619b8d05b8SZbigniew Bodek 	/* Save old MTU in case of fail */
3629b8d05b8SZbigniew Bodek 	old_mtu = if_getmtu(ifp);
3639b8d05b8SZbigniew Bodek 
3649b8d05b8SZbigniew Bodek 	/* Change MTU and calculate max frame */
3659b8d05b8SZbigniew Bodek 	if_setmtu(ifp, new_mtu);
3669b8d05b8SZbigniew Bodek 	max_frame = ETHER_MAX_FRAME(ifp, ETHERTYPE_VLAN, 1);
3679b8d05b8SZbigniew Bodek 
3689b8d05b8SZbigniew Bodek 	if ((new_mtu < ENA_MIN_FRAME_LEN) ||
3699b8d05b8SZbigniew Bodek 	    (new_mtu > get_feat_ctx.dev_attr.max_mtu) ||
3709b8d05b8SZbigniew Bodek 	    (max_frame > ENA_MAX_FRAME_LEN)) {
3719b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid MTU setting. "
3729b8d05b8SZbigniew Bodek 		    "new_mtu: %d\n", new_mtu);
3739b8d05b8SZbigniew Bodek 		goto error;
3749b8d05b8SZbigniew Bodek 	}
3759b8d05b8SZbigniew Bodek 
3769b8d05b8SZbigniew Bodek 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
3779b8d05b8SZbigniew Bodek 	if (rc != 0)
3789b8d05b8SZbigniew Bodek 		goto error;
3799b8d05b8SZbigniew Bodek 
3809b8d05b8SZbigniew Bodek 	return (0);
3819b8d05b8SZbigniew Bodek error:
3829b8d05b8SZbigniew Bodek 	if_setmtu(ifp, old_mtu);
3839b8d05b8SZbigniew Bodek 	return (EINVAL);
3849b8d05b8SZbigniew Bodek }
3859b8d05b8SZbigniew Bodek 
3869b8d05b8SZbigniew Bodek static inline void
3879b8d05b8SZbigniew Bodek ena_alloc_counters(counter_u64_t *begin, int size)
3889b8d05b8SZbigniew Bodek {
3899b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3909b8d05b8SZbigniew Bodek 
3919b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
3929b8d05b8SZbigniew Bodek 		*begin = counter_u64_alloc(M_WAITOK);
3939b8d05b8SZbigniew Bodek }
3949b8d05b8SZbigniew Bodek 
3959b8d05b8SZbigniew Bodek static inline void
3969b8d05b8SZbigniew Bodek ena_free_counters(counter_u64_t *begin, int size)
3979b8d05b8SZbigniew Bodek {
3989b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
3999b8d05b8SZbigniew Bodek 
4009b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
4019b8d05b8SZbigniew Bodek 		counter_u64_free(*begin);
4029b8d05b8SZbigniew Bodek }
4039b8d05b8SZbigniew Bodek 
4049b8d05b8SZbigniew Bodek static inline void
4059b8d05b8SZbigniew Bodek ena_reset_counters(counter_u64_t *begin, int size)
4069b8d05b8SZbigniew Bodek {
4079b8d05b8SZbigniew Bodek 	counter_u64_t *end = (counter_u64_t *)((char *)begin + size);
4089b8d05b8SZbigniew Bodek 
4099b8d05b8SZbigniew Bodek 	for (; begin < end; ++begin)
4109b8d05b8SZbigniew Bodek 		counter_u64_zero(*begin);
4119b8d05b8SZbigniew Bodek }
4129b8d05b8SZbigniew Bodek 
4139b8d05b8SZbigniew Bodek static void
4149b8d05b8SZbigniew Bodek ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
4159b8d05b8SZbigniew Bodek     uint16_t qid)
4169b8d05b8SZbigniew Bodek {
4179b8d05b8SZbigniew Bodek 
4189b8d05b8SZbigniew Bodek 	ring->qid = qid;
4199b8d05b8SZbigniew Bodek 	ring->adapter = adapter;
4209b8d05b8SZbigniew Bodek 	ring->ena_dev = adapter->ena_dev;
4219b8d05b8SZbigniew Bodek }
4229b8d05b8SZbigniew Bodek 
423cd5d5804SMarcin Wojtas static void
4249b8d05b8SZbigniew Bodek ena_init_io_rings(struct ena_adapter *adapter)
4259b8d05b8SZbigniew Bodek {
4269b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
4279b8d05b8SZbigniew Bodek 	struct ena_ring *txr, *rxr;
4289b8d05b8SZbigniew Bodek 	struct ena_que *que;
4299b8d05b8SZbigniew Bodek 	int i;
4309b8d05b8SZbigniew Bodek 
4319b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
4329b8d05b8SZbigniew Bodek 
4339b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
4349b8d05b8SZbigniew Bodek 		txr = &adapter->tx_ring[i];
4359b8d05b8SZbigniew Bodek 		rxr = &adapter->rx_ring[i];
4369b8d05b8SZbigniew Bodek 
4379b8d05b8SZbigniew Bodek 		/* TX/RX common ring state */
4389b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, txr, i);
4399b8d05b8SZbigniew Bodek 		ena_init_io_rings_common(adapter, rxr, i);
4409b8d05b8SZbigniew Bodek 
4419b8d05b8SZbigniew Bodek 		/* TX specific ring state */
4429b8d05b8SZbigniew Bodek 		txr->ring_size = adapter->tx_ring_size;
4439b8d05b8SZbigniew Bodek 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
4449b8d05b8SZbigniew Bodek 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
4459b8d05b8SZbigniew Bodek 		txr->smoothed_interval =
4469b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
4479b8d05b8SZbigniew Bodek 
4489b8d05b8SZbigniew Bodek 		/* Allocate a buf ring */
4499b8d05b8SZbigniew Bodek 		txr->br = buf_ring_alloc(ena_buf_ring_size, M_DEVBUF,
4509b8d05b8SZbigniew Bodek 		    M_WAITOK, &txr->ring_mtx);
4519b8d05b8SZbigniew Bodek 
4529b8d05b8SZbigniew Bodek 		/* Alloc TX statistics. */
4539b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&txr->tx_stats,
4549b8d05b8SZbigniew Bodek 		    sizeof(txr->tx_stats));
4559b8d05b8SZbigniew Bodek 
4569b8d05b8SZbigniew Bodek 		/* RX specific ring state */
4579b8d05b8SZbigniew Bodek 		rxr->ring_size = adapter->rx_ring_size;
4589b8d05b8SZbigniew Bodek 		rxr->rx_small_copy_len = adapter->small_copy_len;
4599b8d05b8SZbigniew Bodek 		rxr->smoothed_interval =
4609b8d05b8SZbigniew Bodek 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
4619b8d05b8SZbigniew Bodek 
4629b8d05b8SZbigniew Bodek 		/* Alloc RX statistics. */
4639b8d05b8SZbigniew Bodek 		ena_alloc_counters((counter_u64_t *)&rxr->rx_stats,
4649b8d05b8SZbigniew Bodek 		    sizeof(rxr->rx_stats));
4659b8d05b8SZbigniew Bodek 
4669b8d05b8SZbigniew Bodek 		/* Initialize locks */
4679b8d05b8SZbigniew Bodek 		snprintf(txr->mtx_name, nitems(txr->mtx_name), "%s:tx(%d)",
4689b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4699b8d05b8SZbigniew Bodek 		snprintf(rxr->mtx_name, nitems(rxr->mtx_name), "%s:rx(%d)",
4709b8d05b8SZbigniew Bodek 		    device_get_nameunit(adapter->pdev), i);
4719b8d05b8SZbigniew Bodek 
4729b8d05b8SZbigniew Bodek 		mtx_init(&txr->ring_mtx, txr->mtx_name, NULL, MTX_DEF);
4739b8d05b8SZbigniew Bodek 
4749b8d05b8SZbigniew Bodek 		que = &adapter->que[i];
4759b8d05b8SZbigniew Bodek 		que->adapter = adapter;
4769b8d05b8SZbigniew Bodek 		que->id = i;
4779b8d05b8SZbigniew Bodek 		que->tx_ring = txr;
4789b8d05b8SZbigniew Bodek 		que->rx_ring = rxr;
4799b8d05b8SZbigniew Bodek 
4809b8d05b8SZbigniew Bodek 		txr->que = que;
4819b8d05b8SZbigniew Bodek 		rxr->que = que;
4829b8d05b8SZbigniew Bodek 	}
4839b8d05b8SZbigniew Bodek }
4849b8d05b8SZbigniew Bodek 
4859b8d05b8SZbigniew Bodek static void
4869b8d05b8SZbigniew Bodek ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
4879b8d05b8SZbigniew Bodek {
4889b8d05b8SZbigniew Bodek 	struct ena_ring *txr = &adapter->tx_ring[qid];
4899b8d05b8SZbigniew Bodek 	struct ena_ring *rxr = &adapter->rx_ring[qid];
4909b8d05b8SZbigniew Bodek 
4919b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&txr->tx_stats,
4929b8d05b8SZbigniew Bodek 	    sizeof(txr->tx_stats));
4939b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&rxr->rx_stats,
4949b8d05b8SZbigniew Bodek 	    sizeof(rxr->rx_stats));
4959b8d05b8SZbigniew Bodek 
4969b8d05b8SZbigniew Bodek 	mtx_destroy(&txr->ring_mtx);
4979b8d05b8SZbigniew Bodek 
4989b8d05b8SZbigniew Bodek 	drbr_free(txr->br, M_DEVBUF);
4999b8d05b8SZbigniew Bodek 
5009b8d05b8SZbigniew Bodek }
5019b8d05b8SZbigniew Bodek 
5029b8d05b8SZbigniew Bodek static void
5039b8d05b8SZbigniew Bodek ena_free_all_io_rings_resources(struct ena_adapter *adapter)
5049b8d05b8SZbigniew Bodek {
5059b8d05b8SZbigniew Bodek 	int i;
5069b8d05b8SZbigniew Bodek 
5079b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
5089b8d05b8SZbigniew Bodek 		ena_free_io_ring_resources(adapter, i);
5099b8d05b8SZbigniew Bodek 
5109b8d05b8SZbigniew Bodek }
5119b8d05b8SZbigniew Bodek 
5129b8d05b8SZbigniew Bodek static int
5139b8d05b8SZbigniew Bodek ena_setup_tx_dma_tag(struct ena_adapter *adapter)
5149b8d05b8SZbigniew Bodek {
5159b8d05b8SZbigniew Bodek 	int ret;
5169b8d05b8SZbigniew Bodek 
5179b8d05b8SZbigniew Bodek 	/* Create DMA tag for Tx buffers */
5189b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
5199b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5208a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5218a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5229b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5239b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
5248a573700SZbigniew Bodek 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
5259b8d05b8SZbigniew Bodek 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
5269b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5279b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5289b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfuncarg 	     */
5299b8d05b8SZbigniew Bodek 	    &adapter->tx_buf_tag);
5309b8d05b8SZbigniew Bodek 
5319b8d05b8SZbigniew Bodek 	if (ret != 0)
5329b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Unable to create Tx DMA tag\n");
5339b8d05b8SZbigniew Bodek 
5349b8d05b8SZbigniew Bodek 	return (ret);
5359b8d05b8SZbigniew Bodek }
5369b8d05b8SZbigniew Bodek 
5379b8d05b8SZbigniew Bodek static int
5389b8d05b8SZbigniew Bodek ena_free_tx_dma_tag(struct ena_adapter *adapter)
5399b8d05b8SZbigniew Bodek {
5409b8d05b8SZbigniew Bodek 	int ret;
5419b8d05b8SZbigniew Bodek 
5429b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->tx_buf_tag);
5439b8d05b8SZbigniew Bodek 
5449b8d05b8SZbigniew Bodek 	if (ret == 0)
5459b8d05b8SZbigniew Bodek 		adapter->tx_buf_tag = NULL;
5469b8d05b8SZbigniew Bodek 
5479b8d05b8SZbigniew Bodek 	return (ret);
5489b8d05b8SZbigniew Bodek }
5499b8d05b8SZbigniew Bodek 
5509b8d05b8SZbigniew Bodek static int
5519b8d05b8SZbigniew Bodek ena_setup_rx_dma_tag(struct ena_adapter *adapter)
5529b8d05b8SZbigniew Bodek {
5539b8d05b8SZbigniew Bodek 	int ret;
5549b8d05b8SZbigniew Bodek 
5559b8d05b8SZbigniew Bodek 	/* Create DMA tag for Rx buffers*/
5569b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
5579b8d05b8SZbigniew Bodek 	    1, 0,				  /* alignment, bounds 	     */
5588a573700SZbigniew Bodek 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
5598a573700SZbigniew Bodek 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
5609b8d05b8SZbigniew Bodek 	    NULL, NULL,				  /* filter, filterarg 	     */
5619b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsize 		     */
5629b8d05b8SZbigniew Bodek 	    1,					  /* nsegments 		     */
5639b8d05b8SZbigniew Bodek 	    MJUM16BYTES,			  /* maxsegsize 	     */
5649b8d05b8SZbigniew Bodek 	    0,					  /* flags 		     */
5659b8d05b8SZbigniew Bodek 	    NULL,				  /* lockfunc 		     */
5669b8d05b8SZbigniew Bodek 	    NULL,				  /* lockarg 		     */
5679b8d05b8SZbigniew Bodek 	    &adapter->rx_buf_tag);
5689b8d05b8SZbigniew Bodek 
5699b8d05b8SZbigniew Bodek 	if (ret != 0)
5709b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Unable to create Rx DMA tag\n");
5719b8d05b8SZbigniew Bodek 
5729b8d05b8SZbigniew Bodek 	return (ret);
5739b8d05b8SZbigniew Bodek }
5749b8d05b8SZbigniew Bodek 
5759b8d05b8SZbigniew Bodek static int
5769b8d05b8SZbigniew Bodek ena_free_rx_dma_tag(struct ena_adapter *adapter)
5779b8d05b8SZbigniew Bodek {
5789b8d05b8SZbigniew Bodek 	int ret;
5799b8d05b8SZbigniew Bodek 
5809b8d05b8SZbigniew Bodek 	ret = bus_dma_tag_destroy(adapter->rx_buf_tag);
5819b8d05b8SZbigniew Bodek 
5829b8d05b8SZbigniew Bodek 	if (ret == 0)
5839b8d05b8SZbigniew Bodek 		adapter->rx_buf_tag = NULL;
5849b8d05b8SZbigniew Bodek 
5859b8d05b8SZbigniew Bodek 	return (ret);
5869b8d05b8SZbigniew Bodek }
5879b8d05b8SZbigniew Bodek 
5889b8d05b8SZbigniew Bodek 
5899b8d05b8SZbigniew Bodek /**
5909b8d05b8SZbigniew Bodek  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
5919b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
5929b8d05b8SZbigniew Bodek  * @qid: queue index
5939b8d05b8SZbigniew Bodek  *
5949b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
5959b8d05b8SZbigniew Bodek  **/
5969b8d05b8SZbigniew Bodek static int
5979b8d05b8SZbigniew Bodek ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
5989b8d05b8SZbigniew Bodek {
5999b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
6009b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = que->tx_ring;
6019b8d05b8SZbigniew Bodek 	int size, i, err;
6029b8d05b8SZbigniew Bodek #ifdef	RSS
6039b8d05b8SZbigniew Bodek 	cpuset_t cpu_mask;
6049b8d05b8SZbigniew Bodek #endif
6059b8d05b8SZbigniew Bodek 
6069b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
6079b8d05b8SZbigniew Bodek 
6089b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
609cd5d5804SMarcin Wojtas 	if (tx_ring->tx_buffer_info == NULL)
6109b8d05b8SZbigniew Bodek 		goto err_tx_buffer_info;
6119b8d05b8SZbigniew Bodek 
6129b8d05b8SZbigniew Bodek 	size = sizeof(uint16_t) * tx_ring->ring_size;
6139b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
614cd5d5804SMarcin Wojtas 	if (tx_ring->free_tx_ids == NULL)
6159b8d05b8SZbigniew Bodek 		goto err_tx_reqs;
6169b8d05b8SZbigniew Bodek 
6179b8d05b8SZbigniew Bodek 	/* Req id stack for TX OOO completions */
6189b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++)
6199b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[i] = i;
6209b8d05b8SZbigniew Bodek 
6219b8d05b8SZbigniew Bodek 	/* Reset TX statistics. */
6229b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&tx_ring->tx_stats,
6239b8d05b8SZbigniew Bodek 	    sizeof(tx_ring->tx_stats));
6249b8d05b8SZbigniew Bodek 
6259b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = 0;
6269b8d05b8SZbigniew Bodek 	tx_ring->next_to_clean = 0;
6279b8d05b8SZbigniew Bodek 
6289b8d05b8SZbigniew Bodek 	/* Make sure that drbr is empty */
629b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6309b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
631b38cf613SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
6329b8d05b8SZbigniew Bodek 
6339b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
6349b8d05b8SZbigniew Bodek 	for (i = 0; i < tx_ring->ring_size; i++) {
6359b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->tx_buf_tag, 0,
6369b8d05b8SZbigniew Bodek 		    &tx_ring->tx_buffer_info[i].map);
6379b8d05b8SZbigniew Bodek 		if (err != 0) {
6389b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
6399b8d05b8SZbigniew Bodek 			    "Unable to create Tx DMA map for buffer %d\n", i);
6409b8d05b8SZbigniew Bodek 			goto err_tx_map;
6419b8d05b8SZbigniew Bodek 		}
6429b8d05b8SZbigniew Bodek 	}
6439b8d05b8SZbigniew Bodek 
6449b8d05b8SZbigniew Bodek 	/* Allocate taskqueues */
6459b8d05b8SZbigniew Bodek 	TASK_INIT(&tx_ring->enqueue_task, 0, ena_deferred_mq_start, tx_ring);
6469b8d05b8SZbigniew Bodek 	tx_ring->enqueue_tq = taskqueue_create_fast("ena_tx_enque", M_NOWAIT,
6479b8d05b8SZbigniew Bodek 	    taskqueue_thread_enqueue, &tx_ring->enqueue_tq);
6489b8d05b8SZbigniew Bodek 	if (tx_ring->enqueue_tq == NULL) {
6499b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
6509b8d05b8SZbigniew Bodek 		    "Unable to create taskqueue for enqueue task\n");
6519b8d05b8SZbigniew Bodek 		i = tx_ring->ring_size;
6529b8d05b8SZbigniew Bodek 		goto err_tx_map;
6539b8d05b8SZbigniew Bodek 	}
6549b8d05b8SZbigniew Bodek 
6559b8d05b8SZbigniew Bodek 	/* RSS set cpu for thread */
6569b8d05b8SZbigniew Bodek #ifdef RSS
6579b8d05b8SZbigniew Bodek 	CPU_SETOF(que->cpu, &cpu_mask);
6589b8d05b8SZbigniew Bodek 	taskqueue_start_threads_cpuset(&tx_ring->enqueue_tq, 1, PI_NET,
6599b8d05b8SZbigniew Bodek 	    &cpu_mask, "%s tx_ring enq (bucket %d)",
6609b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev), que->cpu);
6619b8d05b8SZbigniew Bodek #else /* RSS */
6629b8d05b8SZbigniew Bodek 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, PI_NET,
6639b8d05b8SZbigniew Bodek 	    "%s txeq %d", device_get_nameunit(adapter->pdev), que->cpu);
6649b8d05b8SZbigniew Bodek #endif /* RSS */
6659b8d05b8SZbigniew Bodek 
6669b8d05b8SZbigniew Bodek 	return (0);
6679b8d05b8SZbigniew Bodek 
6689b8d05b8SZbigniew Bodek err_tx_map:
6699b8d05b8SZbigniew Bodek 	while (i--) {
6709b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
6719b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
6729b8d05b8SZbigniew Bodek 	}
673cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
6749b8d05b8SZbigniew Bodek err_tx_reqs:
675cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
6769b8d05b8SZbigniew Bodek err_tx_buffer_info:
6779b8d05b8SZbigniew Bodek 	return (ENOMEM);
6789b8d05b8SZbigniew Bodek }
6799b8d05b8SZbigniew Bodek 
6809b8d05b8SZbigniew Bodek /**
6819b8d05b8SZbigniew Bodek  * ena_free_tx_resources - Free Tx Resources per Queue
6829b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
6839b8d05b8SZbigniew Bodek  * @qid: queue index
6849b8d05b8SZbigniew Bodek  *
6859b8d05b8SZbigniew Bodek  * Free all transmit software resources
6869b8d05b8SZbigniew Bodek  **/
6879b8d05b8SZbigniew Bodek static void
6889b8d05b8SZbigniew Bodek ena_free_tx_resources(struct ena_adapter *adapter, int qid)
6899b8d05b8SZbigniew Bodek {
6909b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
6919b8d05b8SZbigniew Bodek 
6929b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
6939b8d05b8SZbigniew Bodek 	    NULL))
6949b8d05b8SZbigniew Bodek 		taskqueue_drain(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
6959b8d05b8SZbigniew Bodek 
6969b8d05b8SZbigniew Bodek 	taskqueue_free(tx_ring->enqueue_tq);
6979b8d05b8SZbigniew Bodek 
698b38cf613SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
6999b8d05b8SZbigniew Bodek 	/* Flush buffer ring, */
7009b8d05b8SZbigniew Bodek 	drbr_flush(adapter->ifp, tx_ring->br);
7019b8d05b8SZbigniew Bodek 
7029b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
7039b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
7049b8d05b8SZbigniew Bodek 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
7059b8d05b8SZbigniew Bodek 		tx_ring->tx_buffer_info[i].mbuf = NULL;
7069b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag,
7079b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
7089b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->tx_buf_tag,
7099b8d05b8SZbigniew Bodek 		    tx_ring->tx_buffer_info[i].map);
7109b8d05b8SZbigniew Bodek 	}
711416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
7129b8d05b8SZbigniew Bodek 
7139b8d05b8SZbigniew Bodek 	/* And free allocated memory. */
714cd5d5804SMarcin Wojtas 	free(tx_ring->tx_buffer_info, M_DEVBUF);
7159b8d05b8SZbigniew Bodek 	tx_ring->tx_buffer_info = NULL;
7169b8d05b8SZbigniew Bodek 
717cd5d5804SMarcin Wojtas 	free(tx_ring->free_tx_ids, M_DEVBUF);
7189b8d05b8SZbigniew Bodek 	tx_ring->free_tx_ids = NULL;
7199b8d05b8SZbigniew Bodek }
7209b8d05b8SZbigniew Bodek 
7219b8d05b8SZbigniew Bodek /**
7229b8d05b8SZbigniew Bodek  * ena_setup_all_tx_resources - allocate all queues Tx resources
7239b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7249b8d05b8SZbigniew Bodek  *
7259b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7269b8d05b8SZbigniew Bodek  **/
7279b8d05b8SZbigniew Bodek static int
7289b8d05b8SZbigniew Bodek ena_setup_all_tx_resources(struct ena_adapter *adapter)
7299b8d05b8SZbigniew Bodek {
7309b8d05b8SZbigniew Bodek 	int i, rc;
7319b8d05b8SZbigniew Bodek 
7329b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
7339b8d05b8SZbigniew Bodek 		rc = ena_setup_tx_resources(adapter, i);
734cd5d5804SMarcin Wojtas 		if (rc == 0)
7359b8d05b8SZbigniew Bodek 			continue;
7369b8d05b8SZbigniew Bodek 
7379b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
7389b8d05b8SZbigniew Bodek 		    "Allocation for Tx Queue %u failed\n", i);
7399b8d05b8SZbigniew Bodek 		goto err_setup_tx;
7409b8d05b8SZbigniew Bodek 	}
7419b8d05b8SZbigniew Bodek 
7429b8d05b8SZbigniew Bodek 	return (0);
7439b8d05b8SZbigniew Bodek 
7449b8d05b8SZbigniew Bodek err_setup_tx:
7459b8d05b8SZbigniew Bodek 	/* Rewind the index freeing the rings as we go */
7469b8d05b8SZbigniew Bodek 	while (i--)
7479b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7489b8d05b8SZbigniew Bodek 	return (rc);
7499b8d05b8SZbigniew Bodek }
7509b8d05b8SZbigniew Bodek 
7519b8d05b8SZbigniew Bodek /**
7529b8d05b8SZbigniew Bodek  * ena_free_all_tx_resources - Free Tx Resources for All Queues
7539b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7549b8d05b8SZbigniew Bodek  *
7559b8d05b8SZbigniew Bodek  * Free all transmit software resources
7569b8d05b8SZbigniew Bodek  **/
7579b8d05b8SZbigniew Bodek static void
7589b8d05b8SZbigniew Bodek ena_free_all_tx_resources(struct ena_adapter *adapter)
7599b8d05b8SZbigniew Bodek {
7609b8d05b8SZbigniew Bodek 	int i;
7619b8d05b8SZbigniew Bodek 
7629b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
7639b8d05b8SZbigniew Bodek 		ena_free_tx_resources(adapter, i);
7649b8d05b8SZbigniew Bodek 
7659b8d05b8SZbigniew Bodek 	return;
7669b8d05b8SZbigniew Bodek }
7679b8d05b8SZbigniew Bodek 
7689b8d05b8SZbigniew Bodek /**
7699b8d05b8SZbigniew Bodek  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
7709b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
7719b8d05b8SZbigniew Bodek  * @qid: queue index
7729b8d05b8SZbigniew Bodek  *
7739b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
7749b8d05b8SZbigniew Bodek  **/
7759b8d05b8SZbigniew Bodek static int
7769b8d05b8SZbigniew Bodek ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
7779b8d05b8SZbigniew Bodek {
7789b8d05b8SZbigniew Bodek 	struct ena_que *que = &adapter->que[qid];
7799b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = que->rx_ring;
7809b8d05b8SZbigniew Bodek 	int size, err, i;
7819b8d05b8SZbigniew Bodek #ifdef	RSS
7829b8d05b8SZbigniew Bodek 	cpuset_t cpu_mask;
7839b8d05b8SZbigniew Bodek #endif
7849b8d05b8SZbigniew Bodek 
7859b8d05b8SZbigniew Bodek 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
7869b8d05b8SZbigniew Bodek 
7879b8d05b8SZbigniew Bodek 	/*
7889b8d05b8SZbigniew Bodek 	 * Alloc extra element so in rx path
7899b8d05b8SZbigniew Bodek 	 * we can always prefetch rx_info + 1
7909b8d05b8SZbigniew Bodek 	 */
7919b8d05b8SZbigniew Bodek 	size += sizeof(struct ena_rx_buffer);
7929b8d05b8SZbigniew Bodek 
793cd5d5804SMarcin Wojtas 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
7949b8d05b8SZbigniew Bodek 
7959b8d05b8SZbigniew Bodek 	/* Reset RX statistics. */
7969b8d05b8SZbigniew Bodek 	ena_reset_counters((counter_u64_t *)&rx_ring->rx_stats,
7979b8d05b8SZbigniew Bodek 	    sizeof(rx_ring->rx_stats));
7989b8d05b8SZbigniew Bodek 
7999b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = 0;
8009b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = 0;
8019b8d05b8SZbigniew Bodek 
8029b8d05b8SZbigniew Bodek 	/* ... and create the buffer DMA maps */
8039b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
8049b8d05b8SZbigniew Bodek 		err = bus_dmamap_create(adapter->rx_buf_tag, 0,
8059b8d05b8SZbigniew Bodek 		    &(rx_ring->rx_buffer_info[i].map));
8069b8d05b8SZbigniew Bodek 		if (err != 0) {
8079b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8089b8d05b8SZbigniew Bodek 			    "Unable to create Rx DMA map for buffer %d\n", i);
8099b8d05b8SZbigniew Bodek 			goto err_rx_dma;
8109b8d05b8SZbigniew Bodek 		}
8119b8d05b8SZbigniew Bodek 	}
8129b8d05b8SZbigniew Bodek 
8139b8d05b8SZbigniew Bodek 	/* Create LRO for the ring */
8149b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_capenable & IFCAP_LRO) {
8159b8d05b8SZbigniew Bodek 		int err = tcp_lro_init(&rx_ring->lro);
8169b8d05b8SZbigniew Bodek 		if (err) {
8179b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
8189b8d05b8SZbigniew Bodek 			    "LRO[%d] Initialization failed!\n", qid);
8199b8d05b8SZbigniew Bodek 		} else {
8209b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO,
8219b8d05b8SZbigniew Bodek 			    "RX Soft LRO[%d] Initialized\n", qid);
8229b8d05b8SZbigniew Bodek 			rx_ring->lro.ifp = adapter->ifp;
8239b8d05b8SZbigniew Bodek 		}
8249b8d05b8SZbigniew Bodek 	}
8259b8d05b8SZbigniew Bodek 
8269b8d05b8SZbigniew Bodek 	return (0);
8279b8d05b8SZbigniew Bodek 
8289b8d05b8SZbigniew Bodek err_rx_dma:
8299b8d05b8SZbigniew Bodek 	while (i--) {
8309b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8319b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8329b8d05b8SZbigniew Bodek 	}
8339b8d05b8SZbigniew Bodek 
834cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8359b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8369b8d05b8SZbigniew Bodek 	ena_trace(ENA_ALERT, "RX resource allocation fail");
8379b8d05b8SZbigniew Bodek 	return (ENOMEM);
8389b8d05b8SZbigniew Bodek }
8399b8d05b8SZbigniew Bodek 
8409b8d05b8SZbigniew Bodek /**
8419b8d05b8SZbigniew Bodek  * ena_free_rx_resources - Free Rx Resources
8429b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8439b8d05b8SZbigniew Bodek  * @qid: queue index
8449b8d05b8SZbigniew Bodek  *
8459b8d05b8SZbigniew Bodek  * Free all receive software resources
8469b8d05b8SZbigniew Bodek  **/
8479b8d05b8SZbigniew Bodek static void
8489b8d05b8SZbigniew Bodek ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
8499b8d05b8SZbigniew Bodek {
8509b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
8519b8d05b8SZbigniew Bodek 
8529b8d05b8SZbigniew Bodek 	ena_trace(ENA_INFO, "%s qid %d\n", __func__, qid);
8539b8d05b8SZbigniew Bodek 
8549b8d05b8SZbigniew Bodek 	/* Free buffer DMA maps, */
8559b8d05b8SZbigniew Bodek 	for (int i = 0; i < rx_ring->ring_size; i++) {
8569b8d05b8SZbigniew Bodek 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
8579b8d05b8SZbigniew Bodek 		rx_ring->rx_buffer_info[i].mbuf = NULL;
8589b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->rx_buf_tag,
8599b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8609b8d05b8SZbigniew Bodek 		bus_dmamap_destroy(adapter->rx_buf_tag,
8619b8d05b8SZbigniew Bodek 		    rx_ring->rx_buffer_info[i].map);
8629b8d05b8SZbigniew Bodek 	}
8639b8d05b8SZbigniew Bodek 
8649b8d05b8SZbigniew Bodek 	/* free LRO resources, */
8659b8d05b8SZbigniew Bodek 	tcp_lro_free(&rx_ring->lro);
8669b8d05b8SZbigniew Bodek 
8679b8d05b8SZbigniew Bodek 	/* free allocated memory */
868cd5d5804SMarcin Wojtas 	free(rx_ring->rx_buffer_info, M_DEVBUF);
8699b8d05b8SZbigniew Bodek 	rx_ring->rx_buffer_info = NULL;
8709b8d05b8SZbigniew Bodek 
8719b8d05b8SZbigniew Bodek 	return;
8729b8d05b8SZbigniew Bodek }
8739b8d05b8SZbigniew Bodek 
8749b8d05b8SZbigniew Bodek /**
8759b8d05b8SZbigniew Bodek  * ena_setup_all_rx_resources - allocate all queues Rx resources
8769b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
8779b8d05b8SZbigniew Bodek  *
8789b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
8799b8d05b8SZbigniew Bodek  **/
8809b8d05b8SZbigniew Bodek static int
8819b8d05b8SZbigniew Bodek ena_setup_all_rx_resources(struct ena_adapter *adapter)
8829b8d05b8SZbigniew Bodek {
8839b8d05b8SZbigniew Bodek 	int i, rc = 0;
8849b8d05b8SZbigniew Bodek 
8859b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
8869b8d05b8SZbigniew Bodek 		rc = ena_setup_rx_resources(adapter, i);
887cd5d5804SMarcin Wojtas 		if (rc == 0)
8889b8d05b8SZbigniew Bodek 			continue;
8899b8d05b8SZbigniew Bodek 
8909b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
8919b8d05b8SZbigniew Bodek 		    "Allocation for Rx Queue %u failed\n", i);
8929b8d05b8SZbigniew Bodek 		goto err_setup_rx;
8939b8d05b8SZbigniew Bodek 	}
8949b8d05b8SZbigniew Bodek 	return (0);
8959b8d05b8SZbigniew Bodek 
8969b8d05b8SZbigniew Bodek err_setup_rx:
8979b8d05b8SZbigniew Bodek 	/* rewind the index freeing the rings as we go */
8989b8d05b8SZbigniew Bodek 	while (i--)
8999b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9009b8d05b8SZbigniew Bodek 	return (rc);
9019b8d05b8SZbigniew Bodek }
9029b8d05b8SZbigniew Bodek 
9039b8d05b8SZbigniew Bodek /**
9049b8d05b8SZbigniew Bodek  * ena_free_all_rx_resources - Free Rx resources for all queues
9059b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
9069b8d05b8SZbigniew Bodek  *
9079b8d05b8SZbigniew Bodek  * Free all receive software resources
9089b8d05b8SZbigniew Bodek  **/
9099b8d05b8SZbigniew Bodek static void
9109b8d05b8SZbigniew Bodek ena_free_all_rx_resources(struct ena_adapter *adapter)
9119b8d05b8SZbigniew Bodek {
9129b8d05b8SZbigniew Bodek 	int i;
9139b8d05b8SZbigniew Bodek 
9149b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
9159b8d05b8SZbigniew Bodek 		ena_free_rx_resources(adapter, i);
9169b8d05b8SZbigniew Bodek 
9179b8d05b8SZbigniew Bodek 	return;
9189b8d05b8SZbigniew Bodek }
9199b8d05b8SZbigniew Bodek 
9209b8d05b8SZbigniew Bodek static inline int
9219b8d05b8SZbigniew Bodek ena_alloc_rx_mbuf(struct ena_adapter *adapter,
9229b8d05b8SZbigniew Bodek     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
9239b8d05b8SZbigniew Bodek {
9249b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
9259b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[1];
9269b8d05b8SZbigniew Bodek 	int nsegs, error;
9279b8d05b8SZbigniew Bodek 
9289b8d05b8SZbigniew Bodek 	/* if previous allocated frag is not used */
9299b8d05b8SZbigniew Bodek 	if (rx_info->mbuf != NULL)
9309b8d05b8SZbigniew Bodek 		return (0);
9319b8d05b8SZbigniew Bodek 
9329b8d05b8SZbigniew Bodek 	/* Get mbuf using UMA allocator */
9339b8d05b8SZbigniew Bodek 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
9349b8d05b8SZbigniew Bodek 
935cd5d5804SMarcin Wojtas 	if (rx_info->mbuf == NULL) {
9369b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
9379b8d05b8SZbigniew Bodek 		return (ENOMEM);
9389b8d05b8SZbigniew Bodek 	}
9399b8d05b8SZbigniew Bodek 	/* Set mbuf length*/
9409b8d05b8SZbigniew Bodek 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = MJUM16BYTES;
9419b8d05b8SZbigniew Bodek 
9429b8d05b8SZbigniew Bodek 	/* Map packets for DMA */
9439b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9449b8d05b8SZbigniew Bodek 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d",
9459b8d05b8SZbigniew Bodek 	    adapter->rx_buf_tag,rx_info->mbuf, rx_info->mbuf->m_len);
9469b8d05b8SZbigniew Bodek 	error = bus_dmamap_load_mbuf_sg(adapter->rx_buf_tag, rx_info->map,
9479b8d05b8SZbigniew Bodek 	    rx_info->mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
9489b8d05b8SZbigniew Bodek 	if (error || (nsegs != 1)) {
9499b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to map mbuf, error: %d, "
9509b8d05b8SZbigniew Bodek 		    "nsegs: %d\n", error, nsegs);
9519b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
9529b8d05b8SZbigniew Bodek 		goto exit;
9539b8d05b8SZbigniew Bodek 
9549b8d05b8SZbigniew Bodek 	}
9559b8d05b8SZbigniew Bodek 
9569b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->rx_buf_tag, rx_info->map, BUS_DMASYNC_PREREAD);
9579b8d05b8SZbigniew Bodek 
9589b8d05b8SZbigniew Bodek 	ena_buf = &rx_info->ena_buf;
9599b8d05b8SZbigniew Bodek 	ena_buf->paddr = segs[0].ds_addr;
9609b8d05b8SZbigniew Bodek 	ena_buf->len = MJUM16BYTES;
9619b8d05b8SZbigniew Bodek 
9629b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
9639b8d05b8SZbigniew Bodek 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
9649b8d05b8SZbigniew Bodek 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
9659b8d05b8SZbigniew Bodek 
9669b8d05b8SZbigniew Bodek 	return (0);
9679b8d05b8SZbigniew Bodek 
9689b8d05b8SZbigniew Bodek exit:
9699b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9709b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9719b8d05b8SZbigniew Bodek 	return (EFAULT);
9729b8d05b8SZbigniew Bodek }
9739b8d05b8SZbigniew Bodek 
9749b8d05b8SZbigniew Bodek static void
9759b8d05b8SZbigniew Bodek ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
9769b8d05b8SZbigniew Bodek     struct ena_rx_buffer *rx_info)
9779b8d05b8SZbigniew Bodek {
9789b8d05b8SZbigniew Bodek 
979cd5d5804SMarcin Wojtas 	if (rx_info->mbuf == NULL)
9809b8d05b8SZbigniew Bodek 		return;
9819b8d05b8SZbigniew Bodek 
9829b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->rx_buf_tag, rx_info->map);
9839b8d05b8SZbigniew Bodek 	m_freem(rx_info->mbuf);
9849b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
9859b8d05b8SZbigniew Bodek 
9869b8d05b8SZbigniew Bodek 	return;
9879b8d05b8SZbigniew Bodek }
9889b8d05b8SZbigniew Bodek 
9899b8d05b8SZbigniew Bodek 
9909b8d05b8SZbigniew Bodek /**
9919b8d05b8SZbigniew Bodek  * ena_refill_rx_bufs - Refills ring with descriptors
9929b8d05b8SZbigniew Bodek  * @rx_ring: the ring which we want to feed with free descriptors
9939b8d05b8SZbigniew Bodek  * @num: number of descriptors to refill
9949b8d05b8SZbigniew Bodek  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
9959b8d05b8SZbigniew Bodek  **/
9969b8d05b8SZbigniew Bodek static int
9979b8d05b8SZbigniew Bodek ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
9989b8d05b8SZbigniew Bodek {
9999b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
10009b8d05b8SZbigniew Bodek 	uint16_t next_to_use;
10019b8d05b8SZbigniew Bodek 	uint32_t i;
10029b8d05b8SZbigniew Bodek 	int rc;
10039b8d05b8SZbigniew Bodek 
10049b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d",
10059b8d05b8SZbigniew Bodek 	    rx_ring->qid);
10069b8d05b8SZbigniew Bodek 
10079b8d05b8SZbigniew Bodek 	next_to_use = rx_ring->next_to_use;
10089b8d05b8SZbigniew Bodek 
10099b8d05b8SZbigniew Bodek 	for (i = 0; i < num; i++) {
10109b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
10119b8d05b8SZbigniew Bodek 		    "RX buffer - next to use: %d", next_to_use);
10129b8d05b8SZbigniew Bodek 
10139b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info =
10149b8d05b8SZbigniew Bodek 		    &rx_ring->rx_buffer_info[next_to_use];
10159b8d05b8SZbigniew Bodek 
10169b8d05b8SZbigniew Bodek 		rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
1017516d3355SZbigniew Bodek 		if (rc != 0) {
10189b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
10199b8d05b8SZbigniew Bodek 			    "failed to alloc buffer for rx queue\n");
10209b8d05b8SZbigniew Bodek 			break;
10219b8d05b8SZbigniew Bodek 		}
10229b8d05b8SZbigniew Bodek 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
10239b8d05b8SZbigniew Bodek 		    &rx_info->ena_buf, next_to_use);
10249b8d05b8SZbigniew Bodek 		if (unlikely(rc)) {
10259b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
10269b8d05b8SZbigniew Bodek 			    "failed to add buffer for rx queue %d\n",
10279b8d05b8SZbigniew Bodek 			    rx_ring->qid);
10289b8d05b8SZbigniew Bodek 			break;
10299b8d05b8SZbigniew Bodek 		}
10309b8d05b8SZbigniew Bodek 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
10319b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
10329b8d05b8SZbigniew Bodek 	}
10339b8d05b8SZbigniew Bodek 
10349b8d05b8SZbigniew Bodek 	if (i < num) {
10359b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
10369b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
10379b8d05b8SZbigniew Bodek 		    "refilled rx queue %d with %d pages only\n",
10389b8d05b8SZbigniew Bodek 		    rx_ring->qid, i);
10399b8d05b8SZbigniew Bodek 	}
10409b8d05b8SZbigniew Bodek 
10419b8d05b8SZbigniew Bodek 	if (i != 0) {
10429b8d05b8SZbigniew Bodek 		wmb();
10439b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
10449b8d05b8SZbigniew Bodek 	}
10459b8d05b8SZbigniew Bodek 	rx_ring->next_to_use = next_to_use;
10469b8d05b8SZbigniew Bodek 	return (i);
10479b8d05b8SZbigniew Bodek }
10489b8d05b8SZbigniew Bodek 
10499b8d05b8SZbigniew Bodek static void
10509b8d05b8SZbigniew Bodek ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
10519b8d05b8SZbigniew Bodek {
10529b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
10539b8d05b8SZbigniew Bodek 	unsigned int i;
10549b8d05b8SZbigniew Bodek 
10559b8d05b8SZbigniew Bodek 	for (i = 0; i < rx_ring->ring_size; i++) {
10569b8d05b8SZbigniew Bodek 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
10579b8d05b8SZbigniew Bodek 
10589b8d05b8SZbigniew Bodek 		if (rx_info->mbuf)
10599b8d05b8SZbigniew Bodek 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
10609b8d05b8SZbigniew Bodek 	}
10619b8d05b8SZbigniew Bodek 
10629b8d05b8SZbigniew Bodek 	return;
10639b8d05b8SZbigniew Bodek }
10649b8d05b8SZbigniew Bodek 
10659b8d05b8SZbigniew Bodek /**
10669b8d05b8SZbigniew Bodek  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
10679b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
10689b8d05b8SZbigniew Bodek  *
10699b8d05b8SZbigniew Bodek  */
10709b8d05b8SZbigniew Bodek static void
10719b8d05b8SZbigniew Bodek ena_refill_all_rx_bufs(struct ena_adapter *adapter)
10729b8d05b8SZbigniew Bodek {
10739b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
10749b8d05b8SZbigniew Bodek 	int i, rc, bufs_num;
10759b8d05b8SZbigniew Bodek 
10769b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
10779b8d05b8SZbigniew Bodek 		rx_ring = &adapter->rx_ring[i];
10789b8d05b8SZbigniew Bodek 		bufs_num = rx_ring->ring_size - 1;
10799b8d05b8SZbigniew Bodek 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
10809b8d05b8SZbigniew Bodek 
10819b8d05b8SZbigniew Bodek 		if (unlikely(rc != bufs_num))
10829b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
10839b8d05b8SZbigniew Bodek 			    "refilling Queue %d failed. allocated %d buffers"
10849b8d05b8SZbigniew Bodek 			    " from: %d\n", i, rc, bufs_num);
10859b8d05b8SZbigniew Bodek 	}
10869b8d05b8SZbigniew Bodek }
10879b8d05b8SZbigniew Bodek 
10889b8d05b8SZbigniew Bodek static void
10899b8d05b8SZbigniew Bodek ena_free_all_rx_bufs(struct ena_adapter *adapter)
10909b8d05b8SZbigniew Bodek {
10919b8d05b8SZbigniew Bodek 	int i;
10929b8d05b8SZbigniew Bodek 
10939b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++)
10949b8d05b8SZbigniew Bodek 		ena_free_rx_bufs(adapter, i);
10959b8d05b8SZbigniew Bodek 	return;
10969b8d05b8SZbigniew Bodek }
10979b8d05b8SZbigniew Bodek 
10989b8d05b8SZbigniew Bodek /**
10999b8d05b8SZbigniew Bodek  * ena_free_tx_bufs - Free Tx Buffers per Queue
11009b8d05b8SZbigniew Bodek  * @adapter: network interface device structure
11019b8d05b8SZbigniew Bodek  * @qid: queue index
11029b8d05b8SZbigniew Bodek  **/
11039b8d05b8SZbigniew Bodek static void
11049b8d05b8SZbigniew Bodek ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
11059b8d05b8SZbigniew Bodek {
11069b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
11079b8d05b8SZbigniew Bodek 
1108416e8864SZbigniew Bodek 	ENA_RING_MTX_LOCK(tx_ring);
11099b8d05b8SZbigniew Bodek 	for (int i = 0; i < tx_ring->ring_size; i++) {
11109b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
11119b8d05b8SZbigniew Bodek 
11129b8d05b8SZbigniew Bodek 		if (tx_info->mbuf == NULL)
11139b8d05b8SZbigniew Bodek 			continue;
11149b8d05b8SZbigniew Bodek 
11159b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_TXPTH | ENA_RSC,
11169b8d05b8SZbigniew Bodek 		    "free uncompleted Tx mbufs qid[%d] idx: 0x%x", qid, i);
11179b8d05b8SZbigniew Bodek 
11189b8d05b8SZbigniew Bodek 		bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
11199b8d05b8SZbigniew Bodek 		m_free(tx_info->mbuf);
11209b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
11219b8d05b8SZbigniew Bodek 	}
1122416e8864SZbigniew Bodek 	ENA_RING_MTX_UNLOCK(tx_ring);
11239b8d05b8SZbigniew Bodek 
11249b8d05b8SZbigniew Bodek 	return;
11259b8d05b8SZbigniew Bodek }
11269b8d05b8SZbigniew Bodek 
11279b8d05b8SZbigniew Bodek static void
11289b8d05b8SZbigniew Bodek ena_free_all_tx_bufs(struct ena_adapter *adapter)
11299b8d05b8SZbigniew Bodek {
11309b8d05b8SZbigniew Bodek 
11319b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++)
11329b8d05b8SZbigniew Bodek 		ena_free_tx_bufs(adapter, i);
11339b8d05b8SZbigniew Bodek 
11349b8d05b8SZbigniew Bodek 	return;
11359b8d05b8SZbigniew Bodek }
11369b8d05b8SZbigniew Bodek 
11379b8d05b8SZbigniew Bodek static void
11389b8d05b8SZbigniew Bodek ena_destroy_all_tx_queues(struct ena_adapter *adapter)
11399b8d05b8SZbigniew Bodek {
11409b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11419b8d05b8SZbigniew Bodek 	int i;
11429b8d05b8SZbigniew Bodek 
11439b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11449b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11459b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11469b8d05b8SZbigniew Bodek 	}
11479b8d05b8SZbigniew Bodek }
11489b8d05b8SZbigniew Bodek 
11499b8d05b8SZbigniew Bodek static void
11509b8d05b8SZbigniew Bodek ena_destroy_all_rx_queues(struct ena_adapter *adapter)
11519b8d05b8SZbigniew Bodek {
11529b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11539b8d05b8SZbigniew Bodek 	int i;
11549b8d05b8SZbigniew Bodek 
11559b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11569b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
11579b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
11589b8d05b8SZbigniew Bodek 	}
11599b8d05b8SZbigniew Bodek }
11609b8d05b8SZbigniew Bodek 
11619b8d05b8SZbigniew Bodek static void
11629b8d05b8SZbigniew Bodek ena_destroy_all_io_queues(struct ena_adapter *adapter)
11639b8d05b8SZbigniew Bodek {
11649b8d05b8SZbigniew Bodek 	ena_destroy_all_tx_queues(adapter);
11659b8d05b8SZbigniew Bodek 	ena_destroy_all_rx_queues(adapter);
11669b8d05b8SZbigniew Bodek }
11679b8d05b8SZbigniew Bodek 
1168*fceb9387SMarcin Wojtas static inline int
11699b8d05b8SZbigniew Bodek validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
11709b8d05b8SZbigniew Bodek {
11719b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info = NULL;
11729b8d05b8SZbigniew Bodek 
11739b8d05b8SZbigniew Bodek 	if (likely(req_id < tx_ring->ring_size)) {
11749b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
11759b8d05b8SZbigniew Bodek 		if (tx_info->mbuf)
11769b8d05b8SZbigniew Bodek 			return 0;
11779b8d05b8SZbigniew Bodek 	}
11789b8d05b8SZbigniew Bodek 
11799b8d05b8SZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
11809b8d05b8SZbigniew Bodek 
11819b8d05b8SZbigniew Bodek 	return (EFAULT);
11829b8d05b8SZbigniew Bodek }
11839b8d05b8SZbigniew Bodek 
11849b8d05b8SZbigniew Bodek static int
11859b8d05b8SZbigniew Bodek ena_create_io_queues(struct ena_adapter *adapter)
11869b8d05b8SZbigniew Bodek {
11879b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
11889b8d05b8SZbigniew Bodek 	struct ena_com_create_io_ctx ctx;
11899b8d05b8SZbigniew Bodek 	struct ena_ring *ring;
11909b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
11919b8d05b8SZbigniew Bodek 	uint32_t msix_vector;
11929b8d05b8SZbigniew Bodek 	int rc, i;
11939b8d05b8SZbigniew Bodek 
11949b8d05b8SZbigniew Bodek 	/* Create TX queues */
11959b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
11969b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
11979b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
11989b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
11999b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
12009b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->tx_ring_size;
12019b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12029b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12039b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12049b8d05b8SZbigniew Bodek 		if (rc) {
12059b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12069b8d05b8SZbigniew Bodek 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
12079b8d05b8SZbigniew Bodek 			goto err_tx;
12089b8d05b8SZbigniew Bodek 		}
12099b8d05b8SZbigniew Bodek 		ring = &adapter->tx_ring[i];
12109b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12119b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12129b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12139b8d05b8SZbigniew Bodek 		if (rc) {
12149b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12159b8d05b8SZbigniew Bodek 			    "Failed to get TX queue handlers. TX queue num"
12169b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12179b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12189b8d05b8SZbigniew Bodek 			goto err_tx;
12199b8d05b8SZbigniew Bodek 		}
12209b8d05b8SZbigniew Bodek 	}
12219b8d05b8SZbigniew Bodek 
12229b8d05b8SZbigniew Bodek 	/* Create RX queues */
12239b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
12249b8d05b8SZbigniew Bodek 		msix_vector = ENA_IO_IRQ_IDX(i);
12259b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_RXQ_IDX(i);
12269b8d05b8SZbigniew Bodek 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
12279b8d05b8SZbigniew Bodek 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
12289b8d05b8SZbigniew Bodek 		ctx.queue_size = adapter->rx_ring_size;
12299b8d05b8SZbigniew Bodek 		ctx.msix_vector = msix_vector;
12309b8d05b8SZbigniew Bodek 		ctx.qid = ena_qid;
12319b8d05b8SZbigniew Bodek 		rc = ena_com_create_io_queue(ena_dev, &ctx);
12329b8d05b8SZbigniew Bodek 		if (rc) {
12339b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12349b8d05b8SZbigniew Bodek 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
12359b8d05b8SZbigniew Bodek 			goto err_rx;
12369b8d05b8SZbigniew Bodek 		}
12379b8d05b8SZbigniew Bodek 
12389b8d05b8SZbigniew Bodek 		ring = &adapter->rx_ring[i];
12399b8d05b8SZbigniew Bodek 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
12409b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_sq,
12419b8d05b8SZbigniew Bodek 		    &ring->ena_com_io_cq);
12429b8d05b8SZbigniew Bodek 		if (rc) {
12439b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
12449b8d05b8SZbigniew Bodek 			    "Failed to get RX queue handlers. RX queue num"
12459b8d05b8SZbigniew Bodek 			    " %d rc: %d\n", i, rc);
12469b8d05b8SZbigniew Bodek 			ena_com_destroy_io_queue(ena_dev, ena_qid);
12479b8d05b8SZbigniew Bodek 			goto err_rx;
12489b8d05b8SZbigniew Bodek 		}
12499b8d05b8SZbigniew Bodek 	}
12509b8d05b8SZbigniew Bodek 
12519b8d05b8SZbigniew Bodek 	return (0);
12529b8d05b8SZbigniew Bodek 
12539b8d05b8SZbigniew Bodek err_rx:
12549b8d05b8SZbigniew Bodek 	while (i--)
12559b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
12569b8d05b8SZbigniew Bodek 	i = adapter->num_queues;
12579b8d05b8SZbigniew Bodek err_tx:
12589b8d05b8SZbigniew Bodek 	while (i--)
12599b8d05b8SZbigniew Bodek 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
12609b8d05b8SZbigniew Bodek 
12619b8d05b8SZbigniew Bodek 	return (ENXIO);
12629b8d05b8SZbigniew Bodek }
12639b8d05b8SZbigniew Bodek 
12649b8d05b8SZbigniew Bodek /**
12659b8d05b8SZbigniew Bodek  * ena_tx_cleanup - clear sent packets and corresponding descriptors
12669b8d05b8SZbigniew Bodek  * @tx_ring: ring for which we want to clean packets
12679b8d05b8SZbigniew Bodek  *
12689b8d05b8SZbigniew Bodek  * Once packets are sent, we ask the device in a loop for no longer used
12699b8d05b8SZbigniew Bodek  * descriptors. We find the related mbuf chain in a map (index in an array)
12709b8d05b8SZbigniew Bodek  * and free it, then update ring state.
12719b8d05b8SZbigniew Bodek  * This is performed in "endless" loop, updating ring pointers every
12729b8d05b8SZbigniew Bodek  * TX_COMMIT. The first check of free descriptor is performed before the actual
12739b8d05b8SZbigniew Bodek  * loop, then repeated at the loop end.
12749b8d05b8SZbigniew Bodek  **/
12759b8d05b8SZbigniew Bodek static int
12769b8d05b8SZbigniew Bodek ena_tx_cleanup(struct ena_ring *tx_ring)
12779b8d05b8SZbigniew Bodek {
12789b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
12799b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
12809b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
12819b8d05b8SZbigniew Bodek 	uint16_t req_id;
12829b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
12839b8d05b8SZbigniew Bodek 	unsigned int total_done = 0;
12849b8d05b8SZbigniew Bodek 	int rc;
12859b8d05b8SZbigniew Bodek 	int commit = TX_COMMIT;
12869b8d05b8SZbigniew Bodek 	int budget = TX_BUDGET;
12879b8d05b8SZbigniew Bodek 	int work_done;
12889b8d05b8SZbigniew Bodek 
12899b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
12909b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
12919b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
12929b8d05b8SZbigniew Bodek 	next_to_clean = tx_ring->next_to_clean;
12939b8d05b8SZbigniew Bodek 
12949b8d05b8SZbigniew Bodek 	do {
12959b8d05b8SZbigniew Bodek 		struct ena_tx_buffer *tx_info;
12969b8d05b8SZbigniew Bodek 		struct mbuf *mbuf;
12979b8d05b8SZbigniew Bodek 
12989b8d05b8SZbigniew Bodek 		rc = ena_com_tx_comp_req_id_get(io_cq, &req_id);
12999b8d05b8SZbigniew Bodek 		if (rc != 0)
13009b8d05b8SZbigniew Bodek 			break;
13019b8d05b8SZbigniew Bodek 
13029b8d05b8SZbigniew Bodek 		rc = validate_tx_req_id(tx_ring, req_id);
13039b8d05b8SZbigniew Bodek 		if (rc)
13049b8d05b8SZbigniew Bodek 			break;
13059b8d05b8SZbigniew Bodek 
13069b8d05b8SZbigniew Bodek 		tx_info = &tx_ring->tx_buffer_info[req_id];
13079b8d05b8SZbigniew Bodek 
13089b8d05b8SZbigniew Bodek 		mbuf = tx_info->mbuf;
13099b8d05b8SZbigniew Bodek 
13109b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
13119b8d05b8SZbigniew Bodek 		bintime_clear(&tx_info->timestamp);
13129b8d05b8SZbigniew Bodek 
13139b8d05b8SZbigniew Bodek 		if (tx_info->num_of_bufs != 0) {
13149b8d05b8SZbigniew Bodek 			/* Map is no longer required */
13159b8d05b8SZbigniew Bodek 			bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
13169b8d05b8SZbigniew Bodek 		}
13179b8d05b8SZbigniew Bodek 
13189b8d05b8SZbigniew Bodek 		m_freem(mbuf);
13199b8d05b8SZbigniew Bodek 
13209b8d05b8SZbigniew Bodek 		total_done += tx_info->tx_descs;
13219b8d05b8SZbigniew Bodek 
13229b8d05b8SZbigniew Bodek 		tx_ring->free_tx_ids[next_to_clean] = req_id;
13239b8d05b8SZbigniew Bodek 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
13249b8d05b8SZbigniew Bodek 		    tx_ring->ring_size);
13259b8d05b8SZbigniew Bodek 
13269b8d05b8SZbigniew Bodek 		if (--commit == 0) {
13279b8d05b8SZbigniew Bodek 			commit = TX_COMMIT;
13289b8d05b8SZbigniew Bodek 			/* update ring state every TX_COMMIT descriptor */
13299b8d05b8SZbigniew Bodek 			tx_ring->next_to_clean = next_to_clean;
13309b8d05b8SZbigniew Bodek 			ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid], total_done);
13319b8d05b8SZbigniew Bodek 			ena_com_update_dev_comp_head(io_cq);
13329b8d05b8SZbigniew Bodek 			total_done = 0;
13339b8d05b8SZbigniew Bodek 		}
13349b8d05b8SZbigniew Bodek 	} while (--budget);
13359b8d05b8SZbigniew Bodek 
13369b8d05b8SZbigniew Bodek 	work_done = TX_BUDGET - budget;
13379b8d05b8SZbigniew Bodek 
13389b8d05b8SZbigniew Bodek 	/* If there is still something to commit update ring state */
13399b8d05b8SZbigniew Bodek 	if (commit != TX_COMMIT) {
13409b8d05b8SZbigniew Bodek 		tx_ring->next_to_clean = next_to_clean;
13419b8d05b8SZbigniew Bodek 		ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid], total_done);
13429b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(io_cq);
13439b8d05b8SZbigniew Bodek 	}
13449b8d05b8SZbigniew Bodek 
13459b8d05b8SZbigniew Bodek 	taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
13469b8d05b8SZbigniew Bodek 
13479b8d05b8SZbigniew Bodek 	return (work_done);
13489b8d05b8SZbigniew Bodek }
13499b8d05b8SZbigniew Bodek 
13509b8d05b8SZbigniew Bodek static void
13519b8d05b8SZbigniew Bodek ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
13529b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
13539b8d05b8SZbigniew Bodek {
13549b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = rx_ring->adapter;
13559b8d05b8SZbigniew Bodek 
13569b8d05b8SZbigniew Bodek 	if (adapter->rss_support) {
13579b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
13589b8d05b8SZbigniew Bodek 
13599b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->frag &&
13609b8d05b8SZbigniew Bodek 		    ena_rx_ctx->l3_proto != ENA_ETH_IO_L4_PROTO_UNKNOWN) {
13619b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
13629b8d05b8SZbigniew Bodek 			return;
13639b8d05b8SZbigniew Bodek 		}
13649b8d05b8SZbigniew Bodek 
13659b8d05b8SZbigniew Bodek 		switch (ena_rx_ctx->l3_proto) {
13669b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV4:
13679b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
13689b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
13699b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
13709b8d05b8SZbigniew Bodek 				break;
13719b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
13729b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
13739b8d05b8SZbigniew Bodek 				break;
13749b8d05b8SZbigniew Bodek 			default:
13759b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
13769b8d05b8SZbigniew Bodek 			}
13779b8d05b8SZbigniew Bodek 			break;
13789b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_IPV6:
13799b8d05b8SZbigniew Bodek 			switch (ena_rx_ctx->l4_proto) {
13809b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_TCP:
13819b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
13829b8d05b8SZbigniew Bodek 				break;
13839b8d05b8SZbigniew Bodek 			case ENA_ETH_IO_L4_PROTO_UDP:
13849b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
13859b8d05b8SZbigniew Bodek 				break;
13869b8d05b8SZbigniew Bodek 			default:
13879b8d05b8SZbigniew Bodek 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
13889b8d05b8SZbigniew Bodek 			}
13899b8d05b8SZbigniew Bodek 			break;
13909b8d05b8SZbigniew Bodek 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
13919b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
13929b8d05b8SZbigniew Bodek 			break;
13939b8d05b8SZbigniew Bodek 		default:
13949b8d05b8SZbigniew Bodek 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
13959b8d05b8SZbigniew Bodek 		}
13969b8d05b8SZbigniew Bodek 	} else {
13979b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.flowid = rx_ring->qid;
13989b8d05b8SZbigniew Bodek 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
13999b8d05b8SZbigniew Bodek 	}
14009b8d05b8SZbigniew Bodek }
14019b8d05b8SZbigniew Bodek 
14029b8d05b8SZbigniew Bodek /**
14039b8d05b8SZbigniew Bodek  * ena_rx_mbuf - assemble mbuf from descriptors
14049b8d05b8SZbigniew Bodek  * @rx_ring: ring for which we want to clean packets
14059b8d05b8SZbigniew Bodek  * @ena_bufs: buffer info
14069b8d05b8SZbigniew Bodek  * @ena_rx_ctx: metadata for this packet(s)
14079b8d05b8SZbigniew Bodek  * @next_to_clean: ring pointer
14089b8d05b8SZbigniew Bodek  *
14099b8d05b8SZbigniew Bodek  **/
14109b8d05b8SZbigniew Bodek static struct mbuf*
14119b8d05b8SZbigniew Bodek ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
14129b8d05b8SZbigniew Bodek     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
14139b8d05b8SZbigniew Bodek {
14149b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
14159b8d05b8SZbigniew Bodek 	struct ena_rx_buffer *rx_info;
14169b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
14179b8d05b8SZbigniew Bodek 	unsigned int len, buf = 0;
14189b8d05b8SZbigniew Bodek 	unsigned int descs = ena_rx_ctx->descs;
14199b8d05b8SZbigniew Bodek 
14209b8d05b8SZbigniew Bodek 	adapter = rx_ring->adapter;
14219b8d05b8SZbigniew Bodek 	rx_info = &rx_ring->rx_buffer_info[*next_to_clean];
14229b8d05b8SZbigniew Bodek 
14239b8d05b8SZbigniew Bodek 	ENA_ASSERT(rx_info->mbuf, "Invalid alloc frag buffer\n");
14249b8d05b8SZbigniew Bodek 
14259b8d05b8SZbigniew Bodek 	len = ena_bufs[0].len;
14269b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
14279b8d05b8SZbigniew Bodek 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
14289b8d05b8SZbigniew Bodek 
14299b8d05b8SZbigniew Bodek 	mbuf = rx_info->mbuf;
14309b8d05b8SZbigniew Bodek 	mbuf->m_flags |= M_PKTHDR;
14319b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.len = len;
14329b8d05b8SZbigniew Bodek 	mbuf->m_len = len;
14339b8d05b8SZbigniew Bodek 	mbuf->m_pkthdr.rcvif = rx_ring->que->adapter->ifp;
14349b8d05b8SZbigniew Bodek 
14359b8d05b8SZbigniew Bodek 	/* Fill mbuf with hash key and it's interpretation for optimization */
14369b8d05b8SZbigniew Bodek 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
14379b8d05b8SZbigniew Bodek 
14389b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
14399b8d05b8SZbigniew Bodek 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
14409b8d05b8SZbigniew Bodek 
14419b8d05b8SZbigniew Bodek 	/* DMA address is not needed anymore, unmap it */
14429b8d05b8SZbigniew Bodek 	bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
14439b8d05b8SZbigniew Bodek 
14449b8d05b8SZbigniew Bodek 	rx_info->mbuf = NULL;
14459b8d05b8SZbigniew Bodek 	*next_to_clean = ENA_RX_RING_IDX_NEXT(*next_to_clean,
14469b8d05b8SZbigniew Bodek 	    rx_ring->ring_size);
14479b8d05b8SZbigniew Bodek 
14489b8d05b8SZbigniew Bodek 	/*
14499b8d05b8SZbigniew Bodek 	 * While we have more than 1 descriptors for one rcvd packet, append
14509b8d05b8SZbigniew Bodek 	 * other mbufs to the main one
14519b8d05b8SZbigniew Bodek 	 */
14529b8d05b8SZbigniew Bodek 	while (--descs) {
14539b8d05b8SZbigniew Bodek 		rx_info = &rx_ring->rx_buffer_info[*next_to_clean];
14549b8d05b8SZbigniew Bodek 		len = ena_bufs[++buf].len;
14559b8d05b8SZbigniew Bodek 
14569b8d05b8SZbigniew Bodek 		if (!m_append(mbuf, len, rx_info->mbuf->m_data)) {
14579b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
14589b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
14599b8d05b8SZbigniew Bodek 			    mbuf);
14609b8d05b8SZbigniew Bodek 		}
14619b8d05b8SZbigniew Bodek 		/* Free already appended mbuf, it won't be useful anymore */
14629b8d05b8SZbigniew Bodek 		bus_dmamap_unload(rx_ring->adapter->rx_buf_tag, rx_info->map);
14639b8d05b8SZbigniew Bodek 		m_freem(rx_info->mbuf);
14649b8d05b8SZbigniew Bodek 		rx_info->mbuf = NULL;
14659b8d05b8SZbigniew Bodek 
14669b8d05b8SZbigniew Bodek 		*next_to_clean = ENA_RX_RING_IDX_NEXT(*next_to_clean,
14679b8d05b8SZbigniew Bodek 		    rx_ring->ring_size);
14689b8d05b8SZbigniew Bodek 	}
14699b8d05b8SZbigniew Bodek 
14709b8d05b8SZbigniew Bodek 	return (mbuf);
14719b8d05b8SZbigniew Bodek }
14729b8d05b8SZbigniew Bodek 
14739b8d05b8SZbigniew Bodek /**
14749b8d05b8SZbigniew Bodek  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
14759b8d05b8SZbigniew Bodek  **/
14769b8d05b8SZbigniew Bodek static inline void
14779b8d05b8SZbigniew Bodek ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
14789b8d05b8SZbigniew Bodek     struct mbuf *mbuf)
14799b8d05b8SZbigniew Bodek {
14809b8d05b8SZbigniew Bodek 
14819b8d05b8SZbigniew Bodek 	/* if IP and error */
14829b8d05b8SZbigniew Bodek 	if ((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) &&
14839b8d05b8SZbigniew Bodek 	    (ena_rx_ctx->l3_csum_err)) {
14849b8d05b8SZbigniew Bodek 		/* ipv4 checksum error */
14859b8d05b8SZbigniew Bodek 		mbuf->m_pkthdr.csum_flags = 0;
14869b8d05b8SZbigniew Bodek 		counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
14879b8d05b8SZbigniew Bodek 		return;
14889b8d05b8SZbigniew Bodek 	}
14899b8d05b8SZbigniew Bodek 
14909b8d05b8SZbigniew Bodek 	/* if TCP/UDP */
14919b8d05b8SZbigniew Bodek 	if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
14929b8d05b8SZbigniew Bodek 	    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
14939b8d05b8SZbigniew Bodek 		if (ena_rx_ctx->l4_csum_err) {
14949b8d05b8SZbigniew Bodek 			/* TCP/UDP checksum error */
14959b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = 0;
14969b8d05b8SZbigniew Bodek 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
14979b8d05b8SZbigniew Bodek 		} else {
14989b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
14999b8d05b8SZbigniew Bodek 			mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID;
15009b8d05b8SZbigniew Bodek 		}
15019b8d05b8SZbigniew Bodek 	}
15029b8d05b8SZbigniew Bodek 
15039b8d05b8SZbigniew Bodek 	return;
15049b8d05b8SZbigniew Bodek }
15059b8d05b8SZbigniew Bodek 
15069b8d05b8SZbigniew Bodek /**
15079b8d05b8SZbigniew Bodek  * ena_rx_cleanup - handle rx irq
15089b8d05b8SZbigniew Bodek  * @arg: ring for which irq is being handled
15099b8d05b8SZbigniew Bodek  **/
15109b8d05b8SZbigniew Bodek static int
15119b8d05b8SZbigniew Bodek ena_rx_cleanup(struct ena_ring *rx_ring)
15129b8d05b8SZbigniew Bodek {
15139b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
15149b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
15159b8d05b8SZbigniew Bodek 	struct ena_com_rx_ctx ena_rx_ctx;
15169b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
15179b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
15189b8d05b8SZbigniew Bodek 	/* struct ena_eth_io_intr_reg intr_reg; */
15199b8d05b8SZbigniew Bodek 	if_t ifp;
15209b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
15219b8d05b8SZbigniew Bodek 	uint16_t next_to_clean;
15229b8d05b8SZbigniew Bodek 	uint32_t refill_required;
15239b8d05b8SZbigniew Bodek 	uint32_t refill_threshold;
15249b8d05b8SZbigniew Bodek 	uint32_t do_if_input = 0;
15259b8d05b8SZbigniew Bodek 	unsigned int qid;
15269b8d05b8SZbigniew Bodek 	int rc;
15279b8d05b8SZbigniew Bodek 	int budget = RX_BUDGET;
15289b8d05b8SZbigniew Bodek 
15299b8d05b8SZbigniew Bodek 	adapter = rx_ring->que->adapter;
15309b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
15319b8d05b8SZbigniew Bodek 	qid = rx_ring->que->id;
15329b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_RXQ_IDX(qid);
15339b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
15349b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
15359b8d05b8SZbigniew Bodek 	next_to_clean = rx_ring->next_to_clean;
15369b8d05b8SZbigniew Bodek 
15379b8d05b8SZbigniew Bodek 	do {
15389b8d05b8SZbigniew Bodek 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
15399b8d05b8SZbigniew Bodek 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
15409b8d05b8SZbigniew Bodek 		ena_rx_ctx.descs = 0;
15419b8d05b8SZbigniew Bodek 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
15429b8d05b8SZbigniew Bodek 
15439b8d05b8SZbigniew Bodek 		if (unlikely(rc))
15449b8d05b8SZbigniew Bodek 			goto error;
15459b8d05b8SZbigniew Bodek 
15469b8d05b8SZbigniew Bodek 		if (unlikely(ena_rx_ctx.descs == 0))
15479b8d05b8SZbigniew Bodek 			break;
15489b8d05b8SZbigniew Bodek 
15499b8d05b8SZbigniew Bodek 		/* Receive mbuf from the ring */
15509b8d05b8SZbigniew Bodek 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
15519b8d05b8SZbigniew Bodek 		    &ena_rx_ctx, &next_to_clean);
15529b8d05b8SZbigniew Bodek 
15539b8d05b8SZbigniew Bodek 		/* Exit if we failed to retrieve a buffer */
15549b8d05b8SZbigniew Bodek 		if (unlikely(!mbuf)) {
15559b8d05b8SZbigniew Bodek 			next_to_clean = ENA_RX_RING_IDX_ADD(next_to_clean,
15569b8d05b8SZbigniew Bodek 			    ena_rx_ctx.descs, rx_ring->ring_size);
15579b8d05b8SZbigniew Bodek 			break;
15589b8d05b8SZbigniew Bodek 		}
15599b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_RXPTH, "Rx: %d bytes",
15609b8d05b8SZbigniew Bodek 		    mbuf->m_pkthdr.len);
15619b8d05b8SZbigniew Bodek 
15629b8d05b8SZbigniew Bodek 		if ((ifp->if_capenable & IFCAP_RXCSUM) ||
15639b8d05b8SZbigniew Bodek 		    (ifp->if_capenable & IFCAP_RXCSUM_IPV6)) {
15649b8d05b8SZbigniew Bodek 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
15659b8d05b8SZbigniew Bodek 		}
15669b8d05b8SZbigniew Bodek 
156730217e2dSMarcin Wojtas 		counter_enter();
156830217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
156930217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
157030217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
157130217e2dSMarcin Wojtas 		    mbuf->m_pkthdr.len);
157230217e2dSMarcin Wojtas 		counter_exit();
15739b8d05b8SZbigniew Bodek 		/*
15749b8d05b8SZbigniew Bodek 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
15759b8d05b8SZbigniew Bodek 		 * should be computed by hardware.
15769b8d05b8SZbigniew Bodek 		 */
15779b8d05b8SZbigniew Bodek 		do_if_input = 1;
15789b8d05b8SZbigniew Bodek 		if ((ifp->if_capenable & IFCAP_LRO) &&
15799b8d05b8SZbigniew Bodek 		    (mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) &&
15809b8d05b8SZbigniew Bodek 		    ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP) {
15819b8d05b8SZbigniew Bodek 			/*
15829b8d05b8SZbigniew Bodek 			 * Send to the stack if:
15839b8d05b8SZbigniew Bodek 			 *  - LRO not enabled, or
15849b8d05b8SZbigniew Bodek 			 *  - no LRO resources, or
15859b8d05b8SZbigniew Bodek 			 *  - lro enqueue fails
15869b8d05b8SZbigniew Bodek 			 */
15879b8d05b8SZbigniew Bodek 			if (rx_ring->lro.lro_cnt != 0 &&
15889b8d05b8SZbigniew Bodek 			    tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0)
15899b8d05b8SZbigniew Bodek 					do_if_input = 0;
15909b8d05b8SZbigniew Bodek 		}
15919b8d05b8SZbigniew Bodek 		if (do_if_input) {
15929b8d05b8SZbigniew Bodek 			ena_trace(ENA_DBG | ENA_RXPTH, "calling if_input() with mbuf %p",
15939b8d05b8SZbigniew Bodek 			    mbuf);
15949b8d05b8SZbigniew Bodek 			(*ifp->if_input)(ifp, mbuf);
15959b8d05b8SZbigniew Bodek 		}
15969b8d05b8SZbigniew Bodek 
159730217e2dSMarcin Wojtas 		counter_enter();
159830217e2dSMarcin Wojtas 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
159930217e2dSMarcin Wojtas 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
160030217e2dSMarcin Wojtas 		counter_exit();
16019b8d05b8SZbigniew Bodek 	} while (--budget);
16029b8d05b8SZbigniew Bodek 
16039b8d05b8SZbigniew Bodek 	rx_ring->next_to_clean = next_to_clean;
16049b8d05b8SZbigniew Bodek 
1605a195fab0SMarcin Wojtas 	refill_required = ena_com_free_desc(io_sq);
16069b8d05b8SZbigniew Bodek 	refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DEVIDER;
16079b8d05b8SZbigniew Bodek 
16089b8d05b8SZbigniew Bodek 	if (refill_required > refill_threshold) {
16099b8d05b8SZbigniew Bodek 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
16109b8d05b8SZbigniew Bodek 		ena_refill_rx_bufs(rx_ring, refill_required);
16119b8d05b8SZbigniew Bodek 	}
16129b8d05b8SZbigniew Bodek 
16139b8d05b8SZbigniew Bodek 	tcp_lro_flush_all(&rx_ring->lro);
16149b8d05b8SZbigniew Bodek 
16159b8d05b8SZbigniew Bodek 	return (RX_BUDGET - budget);
16169b8d05b8SZbigniew Bodek 
16179b8d05b8SZbigniew Bodek error:
16189b8d05b8SZbigniew Bodek 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
16199b8d05b8SZbigniew Bodek 	return (RX_BUDGET - budget);
16209b8d05b8SZbigniew Bodek }
16219b8d05b8SZbigniew Bodek 
16229b8d05b8SZbigniew Bodek /*********************************************************************
16239b8d05b8SZbigniew Bodek  *
16249b8d05b8SZbigniew Bodek  *  MSIX & Interrupt Service routine
16259b8d05b8SZbigniew Bodek  *
16269b8d05b8SZbigniew Bodek  **********************************************************************/
16279b8d05b8SZbigniew Bodek 
16289b8d05b8SZbigniew Bodek /**
16299b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
16309b8d05b8SZbigniew Bodek  * @arg: interrupt number
16319b8d05b8SZbigniew Bodek  **/
16329b8d05b8SZbigniew Bodek static void
16339b8d05b8SZbigniew Bodek ena_intr_msix_mgmnt(void *arg)
16349b8d05b8SZbigniew Bodek {
16359b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
16369b8d05b8SZbigniew Bodek 
16379b8d05b8SZbigniew Bodek 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
16389b8d05b8SZbigniew Bodek 	if (likely(adapter->running))
16399b8d05b8SZbigniew Bodek 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
16409b8d05b8SZbigniew Bodek }
16419b8d05b8SZbigniew Bodek 
16429b8d05b8SZbigniew Bodek /**
16439b8d05b8SZbigniew Bodek  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
16449b8d05b8SZbigniew Bodek  * @arg: interrupt number
16459b8d05b8SZbigniew Bodek  **/
16469b8d05b8SZbigniew Bodek static void
16479b8d05b8SZbigniew Bodek ena_handle_msix(void *arg)
16489b8d05b8SZbigniew Bodek {
16499b8d05b8SZbigniew Bodek 	struct ena_que	*que = arg;
16509b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = que->adapter;
16519b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
16529b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
16539b8d05b8SZbigniew Bodek 	struct ena_ring *rx_ring;
16549b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
16559b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
16569b8d05b8SZbigniew Bodek 	int qid, ena_qid;
16579b8d05b8SZbigniew Bodek 	int txc, rxc, i;
16589b8d05b8SZbigniew Bodek 
16599b8d05b8SZbigniew Bodek 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
16609b8d05b8SZbigniew Bodek 		return;
16619b8d05b8SZbigniew Bodek 
16629b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "MSI-X TX/RX routine");
16639b8d05b8SZbigniew Bodek 
16649b8d05b8SZbigniew Bodek 	tx_ring = que->tx_ring;
16659b8d05b8SZbigniew Bodek 	rx_ring = que->rx_ring;
16669b8d05b8SZbigniew Bodek 	qid = que->id;
16679b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(qid);
16689b8d05b8SZbigniew Bodek 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
16699b8d05b8SZbigniew Bodek 
16709b8d05b8SZbigniew Bodek 	for (i = 0; i < CLEAN_BUDGET; ++i) {
16719b8d05b8SZbigniew Bodek 		rxc = ena_rx_cleanup(rx_ring);
16729b8d05b8SZbigniew Bodek 
16739b8d05b8SZbigniew Bodek 		/* Protection from calling ena_tx_cleanup from ena_start_xmit */
16749b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
16759b8d05b8SZbigniew Bodek 		txc = ena_tx_cleanup(tx_ring);
16769b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
16779b8d05b8SZbigniew Bodek 
16789b8d05b8SZbigniew Bodek 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
16799b8d05b8SZbigniew Bodek 			return;
16809b8d05b8SZbigniew Bodek 
16819b8d05b8SZbigniew Bodek 		if (txc != TX_BUDGET && rxc != RX_BUDGET)
16829b8d05b8SZbigniew Bodek 		       break;
16839b8d05b8SZbigniew Bodek 	}
16849b8d05b8SZbigniew Bodek 
16859b8d05b8SZbigniew Bodek 	/* Signal that work is done and unmask interrupt */
16869b8d05b8SZbigniew Bodek 	ena_com_update_intr_reg(&intr_reg,
16879b8d05b8SZbigniew Bodek 	    RX_IRQ_INTERVAL,
16889b8d05b8SZbigniew Bodek 	    TX_IRQ_INTERVAL,
16899b8d05b8SZbigniew Bodek 	    true);
16909b8d05b8SZbigniew Bodek 	ena_com_unmask_intr(io_cq, &intr_reg);
16919b8d05b8SZbigniew Bodek }
16929b8d05b8SZbigniew Bodek 
16939b8d05b8SZbigniew Bodek static int
16949b8d05b8SZbigniew Bodek ena_enable_msix(struct ena_adapter *adapter)
16959b8d05b8SZbigniew Bodek {
16969b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
16979b8d05b8SZbigniew Bodek 	int i, msix_vecs, rc = 0;
16989b8d05b8SZbigniew Bodek 
16999b8d05b8SZbigniew Bodek 	/* Reserved the max msix vectors we might need */
17009b8d05b8SZbigniew Bodek 	msix_vecs = ENA_MAX_MSIX_VEC(adapter->num_queues);
17019b8d05b8SZbigniew Bodek 
1702cd5d5804SMarcin Wojtas 	adapter->msix_entries = malloc(msix_vecs * sizeof(struct msix_entry),
1703cd5d5804SMarcin Wojtas 	    M_DEVBUF, M_WAITOK | M_ZERO);
1704cd5d5804SMarcin Wojtas 
17059b8d05b8SZbigniew Bodek 	device_printf(dev, "Allocated msix_entries, vectors (cnt: %d)\n",
17069b8d05b8SZbigniew Bodek 	    msix_vecs);
17079b8d05b8SZbigniew Bodek 
17089b8d05b8SZbigniew Bodek 	for (i = 0; i < msix_vecs; i++) {
17099b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].entry = i;
17109b8d05b8SZbigniew Bodek 		/* Vectors must start from 1 */
17119b8d05b8SZbigniew Bodek 		adapter->msix_entries[i].vector = i + 1;
17129b8d05b8SZbigniew Bodek 	}
17139b8d05b8SZbigniew Bodek 
17149b8d05b8SZbigniew Bodek 	rc = pci_alloc_msix(dev, &msix_vecs);
17159b8d05b8SZbigniew Bodek 	if (rc != 0) {
17169b8d05b8SZbigniew Bodek 		device_printf(dev,
17179b8d05b8SZbigniew Bodek 		    "Failed to enable MSIX, vectors %d rc %d\n", msix_vecs, rc);
1718cd5d5804SMarcin Wojtas 		free(adapter->msix_entries, M_DEVBUF);
17199b8d05b8SZbigniew Bodek 		adapter->msix_entries = NULL;
17209b8d05b8SZbigniew Bodek 		rc = ENOSPC;
17219b8d05b8SZbigniew Bodek 		goto error;
17229b8d05b8SZbigniew Bodek 	}
17239b8d05b8SZbigniew Bodek 
17249b8d05b8SZbigniew Bodek 	adapter->msix_vecs = msix_vecs;
17259b8d05b8SZbigniew Bodek 	adapter->msix_enabled = true;
17269b8d05b8SZbigniew Bodek 
17279b8d05b8SZbigniew Bodek error:
17289b8d05b8SZbigniew Bodek 	return (rc);
17299b8d05b8SZbigniew Bodek }
17309b8d05b8SZbigniew Bodek 
17319b8d05b8SZbigniew Bodek static void
17329b8d05b8SZbigniew Bodek ena_setup_mgmnt_intr(struct ena_adapter *adapter)
17339b8d05b8SZbigniew Bodek {
17349b8d05b8SZbigniew Bodek 
17359b8d05b8SZbigniew Bodek 	snprintf(adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].name,
17369b8d05b8SZbigniew Bodek 	    ENA_IRQNAME_SIZE, "ena-mgmnt@pci:%s",
17379b8d05b8SZbigniew Bodek 	    device_get_nameunit(adapter->pdev));
17389b8d05b8SZbigniew Bodek 	/*
17399b8d05b8SZbigniew Bodek 	 * Handler is NULL on purpose, it will be set
17409b8d05b8SZbigniew Bodek 	 * when mgmnt interrupt is acquired
17419b8d05b8SZbigniew Bodek 	 */
17429b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].handler = NULL;
17439b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].data = adapter;
17449b8d05b8SZbigniew Bodek 	adapter->irq_tbl[ENA_MGMNT_IRQ_IDX].vector =
17459b8d05b8SZbigniew Bodek 	    adapter->msix_entries[ENA_MGMNT_IRQ_IDX].vector;
17469b8d05b8SZbigniew Bodek 
17479b8d05b8SZbigniew Bodek 	return;
17489b8d05b8SZbigniew Bodek }
17499b8d05b8SZbigniew Bodek 
17509b8d05b8SZbigniew Bodek static void
17519b8d05b8SZbigniew Bodek ena_setup_io_intr(struct ena_adapter *adapter)
17529b8d05b8SZbigniew Bodek {
17539b8d05b8SZbigniew Bodek 	static int last_bind_cpu = -1;
17549b8d05b8SZbigniew Bodek 	int irq_idx;
17559b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "enter");
17569b8d05b8SZbigniew Bodek 
17579b8d05b8SZbigniew Bodek 	for (int i = 0; i < adapter->num_queues; i++) {
17589b8d05b8SZbigniew Bodek 		irq_idx = ENA_IO_IRQ_IDX(i);
17599b8d05b8SZbigniew Bodek 
17609b8d05b8SZbigniew Bodek 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
17619b8d05b8SZbigniew Bodek 		    "%s-TxRx-%d", device_get_nameunit(adapter->pdev), i);
17629b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
17639b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
17649b8d05b8SZbigniew Bodek 		adapter->irq_tbl[irq_idx].vector =
17659b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector;
17669b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
17679b8d05b8SZbigniew Bodek 		    adapter->msix_entries[irq_idx].vector);
17689b8d05b8SZbigniew Bodek #ifdef	RSS
17699b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
17709b8d05b8SZbigniew Bodek 		    rss_getcpu(i % rss_getnumbuckets());
17719b8d05b8SZbigniew Bodek #else
17729b8d05b8SZbigniew Bodek 		/*
17739b8d05b8SZbigniew Bodek 		 * We still want to bind rings to the corresponding cpu
17749b8d05b8SZbigniew Bodek 		 * using something similar to the RSS round-robin technique.
17759b8d05b8SZbigniew Bodek 		 */
17769b8d05b8SZbigniew Bodek 		if (last_bind_cpu < 0)
17779b8d05b8SZbigniew Bodek 			last_bind_cpu = CPU_FIRST();
17789b8d05b8SZbigniew Bodek 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
17799b8d05b8SZbigniew Bodek 		    last_bind_cpu;
17809b8d05b8SZbigniew Bodek 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
17819b8d05b8SZbigniew Bodek #endif
17829b8d05b8SZbigniew Bodek 	}
17839b8d05b8SZbigniew Bodek 
17849b8d05b8SZbigniew Bodek 	return;
17859b8d05b8SZbigniew Bodek }
17869b8d05b8SZbigniew Bodek 
17879b8d05b8SZbigniew Bodek static int
17889b8d05b8SZbigniew Bodek ena_request_mgmnt_irq(struct ena_adapter *adapter)
17899b8d05b8SZbigniew Bodek {
17909b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
17919b8d05b8SZbigniew Bodek 	unsigned long flags;
17929b8d05b8SZbigniew Bodek 	int rc, rcc;
17939b8d05b8SZbigniew Bodek 
17949b8d05b8SZbigniew Bodek 	flags = RF_ACTIVE | RF_SHAREABLE;
17959b8d05b8SZbigniew Bodek 
17969b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
17979b8d05b8SZbigniew Bodek 	irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
17989b8d05b8SZbigniew Bodek 	    &irq->vector, flags);
17999b8d05b8SZbigniew Bodek 
18009b8d05b8SZbigniew Bodek 	if (irq->res == NULL) {
18019b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not allocate "
18029b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
18039b8d05b8SZbigniew Bodek 		rc = ENXIO;
18049b8d05b8SZbigniew Bodek 		goto exit_res;
18059b8d05b8SZbigniew Bodek 	}
18069b8d05b8SZbigniew Bodek 
18079b8d05b8SZbigniew Bodek 	if ((rc = bus_activate_resource(adapter->pdev, SYS_RES_IRQ, irq->vector,
18089b8d05b8SZbigniew Bodek 	    irq->res)) != 0) {
18099b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "could not activate "
18109b8d05b8SZbigniew Bodek 		    "irq vector: %d\n", irq->vector);
18119b8d05b8SZbigniew Bodek 		goto exit_intr;
18129b8d05b8SZbigniew Bodek 	}
18139b8d05b8SZbigniew Bodek 
18149b8d05b8SZbigniew Bodek 	if ((rc = bus_setup_intr(adapter->pdev, irq->res,
18159b8d05b8SZbigniew Bodek 	    INTR_TYPE_NET | INTR_MPSAFE, NULL,
18169b8d05b8SZbigniew Bodek 	    ena_intr_msix_mgmnt, irq->data, &irq->cookie)) != 0) {
18179b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to register "
18189b8d05b8SZbigniew Bodek 		    "interrupt handler for irq %ju: %d\n",
18199b8d05b8SZbigniew Bodek 		    rman_get_start(irq->res), rc);
18209b8d05b8SZbigniew Bodek 		goto exit_intr;
18219b8d05b8SZbigniew Bodek 	}
18229b8d05b8SZbigniew Bodek 	irq->requested = true;
18239b8d05b8SZbigniew Bodek 
18249b8d05b8SZbigniew Bodek 	return (rc);
18259b8d05b8SZbigniew Bodek 
18269b8d05b8SZbigniew Bodek exit_intr:
18279b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "exit_intr: releasing resource"
18289b8d05b8SZbigniew Bodek 	    " for irq %d\n", irq->vector);
18299b8d05b8SZbigniew Bodek 	rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
18309b8d05b8SZbigniew Bodek 	    irq->vector, irq->res);
18319b8d05b8SZbigniew Bodek 	if (rcc)
18329b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "dev has no parent while "
18339b8d05b8SZbigniew Bodek 		    "releasing res for irq: %d\n", irq->vector);
18349b8d05b8SZbigniew Bodek 	irq->res = NULL;
18359b8d05b8SZbigniew Bodek 
18369b8d05b8SZbigniew Bodek exit_res:
18379b8d05b8SZbigniew Bodek 	return (rc);
18389b8d05b8SZbigniew Bodek }
18399b8d05b8SZbigniew Bodek 
18409b8d05b8SZbigniew Bodek static int
18419b8d05b8SZbigniew Bodek ena_request_io_irq(struct ena_adapter *adapter)
18429b8d05b8SZbigniew Bodek {
18439b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
18449b8d05b8SZbigniew Bodek 	unsigned long flags = 0;
18459b8d05b8SZbigniew Bodek 	int rc = 0, i, rcc;
18469b8d05b8SZbigniew Bodek 
18479b8d05b8SZbigniew Bodek 	if (!adapter->msix_enabled) {
18489b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "failed to request irq\n");
18499b8d05b8SZbigniew Bodek 		return (EINVAL);
18509b8d05b8SZbigniew Bodek 	} else {
18519b8d05b8SZbigniew Bodek 		flags = RF_ACTIVE | RF_SHAREABLE;
18529b8d05b8SZbigniew Bodek 	}
18539b8d05b8SZbigniew Bodek 
18549b8d05b8SZbigniew Bodek 	for (i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
18559b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
18569b8d05b8SZbigniew Bodek 
18579b8d05b8SZbigniew Bodek 		if (irq->requested)
18589b8d05b8SZbigniew Bodek 			continue;
18599b8d05b8SZbigniew Bodek 
18609b8d05b8SZbigniew Bodek 		irq->res = bus_alloc_resource_any(adapter->pdev, SYS_RES_IRQ,
18619b8d05b8SZbigniew Bodek 		    &irq->vector, flags);
18629b8d05b8SZbigniew Bodek 		if (irq->res == NULL) {
18639b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not allocate "
18649b8d05b8SZbigniew Bodek 			    "irq vector: %d\n", irq->vector);
18659b8d05b8SZbigniew Bodek 			goto err;
18669b8d05b8SZbigniew Bodek 		}
18679b8d05b8SZbigniew Bodek 
18689b8d05b8SZbigniew Bodek 		if ((rc = bus_setup_intr(adapter->pdev, irq->res,
18699b8d05b8SZbigniew Bodek 			    INTR_TYPE_NET | INTR_MPSAFE, NULL, irq->handler,
18709b8d05b8SZbigniew Bodek 			    irq->data, &irq->cookie)) != 0) {
18719b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to register "
18729b8d05b8SZbigniew Bodek 			    "interrupt handler for irq %ju: %d\n",
18739b8d05b8SZbigniew Bodek 			    rman_get_start(irq->res), rc);
18749b8d05b8SZbigniew Bodek 			goto err;
18759b8d05b8SZbigniew Bodek 		}
18769b8d05b8SZbigniew Bodek 		irq->requested = true;
18779b8d05b8SZbigniew Bodek 
18789b8d05b8SZbigniew Bodek #ifdef	RSS
18799b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "queue %d - RSS bucket %d\n",
18809b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
18819b8d05b8SZbigniew Bodek #else
18829b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "queue %d - cpu %d\n",
18839b8d05b8SZbigniew Bodek 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
18849b8d05b8SZbigniew Bodek #endif
18859b8d05b8SZbigniew Bodek 	}
18869b8d05b8SZbigniew Bodek 
18879b8d05b8SZbigniew Bodek 	return (rc);
18889b8d05b8SZbigniew Bodek 
18899b8d05b8SZbigniew Bodek err:
18909b8d05b8SZbigniew Bodek 
18919b8d05b8SZbigniew Bodek 	for (; i >= ENA_IO_IRQ_FIRST_IDX; i--) {
18929b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
18939b8d05b8SZbigniew Bodek 		rcc = 0;
18949b8d05b8SZbigniew Bodek 
18959b8d05b8SZbigniew Bodek 		/* Once we entered err: section and irq->requested is true we
18969b8d05b8SZbigniew Bodek 		   free both intr and resources */
18979b8d05b8SZbigniew Bodek 		if (irq->requested == true)
18989b8d05b8SZbigniew Bodek 			rcc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
18999b8d05b8SZbigniew Bodek 		if (rcc)
19009b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "could not release"
19019b8d05b8SZbigniew Bodek 			    " irq: %d, error: %d\n", irq->vector, rcc);
19029b8d05b8SZbigniew Bodek 
19039b8d05b8SZbigniew Bodek 		/* If we entred err: section without irq->requested set we know
19049b8d05b8SZbigniew Bodek 		   it was bus_alloc_resource_any() that needs cleanup, provided
19059b8d05b8SZbigniew Bodek 		   res is not NULL. In case res is NULL no work in needed in
19069b8d05b8SZbigniew Bodek 		   this iteration */
19079b8d05b8SZbigniew Bodek 		rcc = 0;
19089b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
19099b8d05b8SZbigniew Bodek 			rcc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
19109b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
19119b8d05b8SZbigniew Bodek 		}
19129b8d05b8SZbigniew Bodek 		if (rcc)
19139b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
19149b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
19159b8d05b8SZbigniew Bodek 		irq->requested = false;
19169b8d05b8SZbigniew Bodek 		irq->res = NULL;
19179b8d05b8SZbigniew Bodek 	}
19189b8d05b8SZbigniew Bodek 
19199b8d05b8SZbigniew Bodek 	return (rc);
19209b8d05b8SZbigniew Bodek }
19219b8d05b8SZbigniew Bodek 
19229b8d05b8SZbigniew Bodek static void
19239b8d05b8SZbigniew Bodek ena_free_mgmnt_irq(struct ena_adapter *adapter)
19249b8d05b8SZbigniew Bodek {
19259b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19269b8d05b8SZbigniew Bodek 	int rc;
19279b8d05b8SZbigniew Bodek 
19289b8d05b8SZbigniew Bodek 	irq = &adapter->irq_tbl[ENA_MGMNT_IRQ_IDX];
19299b8d05b8SZbigniew Bodek 	if (irq->requested) {
19309b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "tear down irq: %d\n",
19319b8d05b8SZbigniew Bodek 		    irq->vector);
19329b8d05b8SZbigniew Bodek 		rc = bus_teardown_intr(adapter->pdev, irq->res, irq->cookie);
19339b8d05b8SZbigniew Bodek 		if (rc)
19349b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "failed to tear "
19359b8d05b8SZbigniew Bodek 			    "down irq: %d\n", irq->vector);
19369b8d05b8SZbigniew Bodek 		irq->requested = 0;
19379b8d05b8SZbigniew Bodek 	}
19389b8d05b8SZbigniew Bodek 
19399b8d05b8SZbigniew Bodek 	if (irq->res != NULL) {
19409b8d05b8SZbigniew Bodek 		ena_trace(ENA_INFO | ENA_ADMQ, "release resource irq: %d\n",
19419b8d05b8SZbigniew Bodek 		    irq->vector);
19429b8d05b8SZbigniew Bodek 		rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
19439b8d05b8SZbigniew Bodek 		    irq->vector, irq->res);
19449b8d05b8SZbigniew Bodek 		irq->res = NULL;
19459b8d05b8SZbigniew Bodek 		if (rc)
19469b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev, "dev has no parent while "
19479b8d05b8SZbigniew Bodek 			    "releasing res for irq: %d\n", irq->vector);
19489b8d05b8SZbigniew Bodek 	}
19499b8d05b8SZbigniew Bodek 
19509b8d05b8SZbigniew Bodek 	return;
19519b8d05b8SZbigniew Bodek }
19529b8d05b8SZbigniew Bodek 
19539b8d05b8SZbigniew Bodek static void
19549b8d05b8SZbigniew Bodek ena_free_io_irq(struct ena_adapter *adapter)
19559b8d05b8SZbigniew Bodek {
19569b8d05b8SZbigniew Bodek 	struct ena_irq *irq;
19579b8d05b8SZbigniew Bodek 	int rc;
19589b8d05b8SZbigniew Bodek 
19599b8d05b8SZbigniew Bodek 	for (int i = ENA_IO_IRQ_FIRST_IDX; i < adapter->msix_vecs; i++) {
19609b8d05b8SZbigniew Bodek 		irq = &adapter->irq_tbl[i];
19619b8d05b8SZbigniew Bodek 		if (irq->requested) {
19629b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "tear down irq: %d\n",
19639b8d05b8SZbigniew Bodek 			    irq->vector);
19649b8d05b8SZbigniew Bodek 			rc = bus_teardown_intr(adapter->pdev, irq->res,
19659b8d05b8SZbigniew Bodek 			    irq->cookie);
19669b8d05b8SZbigniew Bodek 			if (rc) {
19679b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "failed to tear "
19689b8d05b8SZbigniew Bodek 				    "down irq: %d\n", irq->vector);
19699b8d05b8SZbigniew Bodek 			}
19709b8d05b8SZbigniew Bodek 			irq->requested = 0;
19719b8d05b8SZbigniew Bodek 		}
19729b8d05b8SZbigniew Bodek 
19739b8d05b8SZbigniew Bodek 		if (irq->res != NULL) {
19749b8d05b8SZbigniew Bodek 			ena_trace(ENA_INFO | ENA_IOQ, "release resource irq: %d\n",
19759b8d05b8SZbigniew Bodek 			    irq->vector);
19769b8d05b8SZbigniew Bodek 			rc = bus_release_resource(adapter->pdev, SYS_RES_IRQ,
19779b8d05b8SZbigniew Bodek 			    irq->vector, irq->res);
19789b8d05b8SZbigniew Bodek 			irq->res = NULL;
19799b8d05b8SZbigniew Bodek 			if (rc) {
19809b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev, "dev has no parent"
19819b8d05b8SZbigniew Bodek 				    " while releasing res for irq: %d\n",
19829b8d05b8SZbigniew Bodek 				    irq->vector);
19839b8d05b8SZbigniew Bodek 			}
19849b8d05b8SZbigniew Bodek 		}
19859b8d05b8SZbigniew Bodek 	}
19869b8d05b8SZbigniew Bodek 
19879b8d05b8SZbigniew Bodek 	return;
19889b8d05b8SZbigniew Bodek }
19899b8d05b8SZbigniew Bodek 
19909b8d05b8SZbigniew Bodek static void
19919b8d05b8SZbigniew Bodek ena_free_irqs(struct ena_adapter* adapter)
19929b8d05b8SZbigniew Bodek {
19939b8d05b8SZbigniew Bodek 
19949b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
19959b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
19969b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
19979b8d05b8SZbigniew Bodek }
19989b8d05b8SZbigniew Bodek 
19999b8d05b8SZbigniew Bodek static void
20009b8d05b8SZbigniew Bodek ena_disable_msix(struct ena_adapter *adapter)
20019b8d05b8SZbigniew Bodek {
20029b8d05b8SZbigniew Bodek 
20039b8d05b8SZbigniew Bodek 	pci_release_msi(adapter->pdev);
20049b8d05b8SZbigniew Bodek 
20059b8d05b8SZbigniew Bodek 	adapter->msix_vecs = 0;
2006cd5d5804SMarcin Wojtas 	free(adapter->msix_entries, M_DEVBUF);
20079b8d05b8SZbigniew Bodek 	adapter->msix_entries = NULL;
20089b8d05b8SZbigniew Bodek }
20099b8d05b8SZbigniew Bodek 
20109b8d05b8SZbigniew Bodek static void
20119b8d05b8SZbigniew Bodek ena_unmask_all_io_irqs(struct ena_adapter *adapter)
20129b8d05b8SZbigniew Bodek {
20139b8d05b8SZbigniew Bodek 	struct ena_com_io_cq* io_cq;
20149b8d05b8SZbigniew Bodek 	struct ena_eth_io_intr_reg intr_reg;
20159b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
20169b8d05b8SZbigniew Bodek 	int i;
20179b8d05b8SZbigniew Bodek 
20189b8d05b8SZbigniew Bodek 	/* Unmask interrupts for all queues */
20199b8d05b8SZbigniew Bodek 	for (i = 0; i < adapter->num_queues; i++) {
20209b8d05b8SZbigniew Bodek 		ena_qid = ENA_IO_TXQ_IDX(i);
20219b8d05b8SZbigniew Bodek 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
20229b8d05b8SZbigniew Bodek 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
20239b8d05b8SZbigniew Bodek 		ena_com_unmask_intr(io_cq, &intr_reg);
20249b8d05b8SZbigniew Bodek 	}
20259b8d05b8SZbigniew Bodek }
20269b8d05b8SZbigniew Bodek 
20279b8d05b8SZbigniew Bodek /* Configure the Rx forwarding */
20289b8d05b8SZbigniew Bodek static int ena_rss_configure(struct ena_adapter *adapter)
20299b8d05b8SZbigniew Bodek {
20309b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
20319b8d05b8SZbigniew Bodek 	int rc;
20329b8d05b8SZbigniew Bodek 
20339b8d05b8SZbigniew Bodek 	/* Set indirect table */
20349b8d05b8SZbigniew Bodek 	rc = ena_com_indirect_table_set(ena_dev);
2035a195fab0SMarcin Wojtas 	if (unlikely(rc && rc != EOPNOTSUPP))
20369b8d05b8SZbigniew Bodek 		return rc;
20379b8d05b8SZbigniew Bodek 
20389b8d05b8SZbigniew Bodek 	/* Configure hash function (if supported) */
20399b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_function(ena_dev);
2040a195fab0SMarcin Wojtas 	if (unlikely(rc && (rc != EOPNOTSUPP)))
20419b8d05b8SZbigniew Bodek 		return rc;
20429b8d05b8SZbigniew Bodek 
20439b8d05b8SZbigniew Bodek 	/* Configure hash inputs (if supported) */
20449b8d05b8SZbigniew Bodek 	rc = ena_com_set_hash_ctrl(ena_dev);
2045a195fab0SMarcin Wojtas 	if (unlikely(rc && (rc != EOPNOTSUPP)))
20469b8d05b8SZbigniew Bodek 		return rc;
20479b8d05b8SZbigniew Bodek 
20489b8d05b8SZbigniew Bodek 	return 0;
20499b8d05b8SZbigniew Bodek }
20509b8d05b8SZbigniew Bodek 
20519b8d05b8SZbigniew Bodek static int
20529b8d05b8SZbigniew Bodek ena_up_complete(struct ena_adapter *adapter)
20539b8d05b8SZbigniew Bodek {
20549b8d05b8SZbigniew Bodek 	int rc;
20559b8d05b8SZbigniew Bodek 
20569b8d05b8SZbigniew Bodek 	if (adapter->rss_support) {
20579b8d05b8SZbigniew Bodek 		rc = ena_rss_configure(adapter);
20589b8d05b8SZbigniew Bodek 		if (rc)
20599b8d05b8SZbigniew Bodek 			return (rc);
20609b8d05b8SZbigniew Bodek 	}
20619b8d05b8SZbigniew Bodek 
20629b8d05b8SZbigniew Bodek 	ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
20639b8d05b8SZbigniew Bodek 	ena_refill_all_rx_bufs(adapter);
206430217e2dSMarcin Wojtas 	ena_reset_counters((counter_u64_t *)&adapter->hw_stats,
206530217e2dSMarcin Wojtas 	    sizeof(adapter->hw_stats));
20669b8d05b8SZbigniew Bodek 
20679b8d05b8SZbigniew Bodek 	return (0);
20689b8d05b8SZbigniew Bodek }
20699b8d05b8SZbigniew Bodek 
20709b8d05b8SZbigniew Bodek static int
20719b8d05b8SZbigniew Bodek ena_up(struct ena_adapter *adapter)
20729b8d05b8SZbigniew Bodek {
20739b8d05b8SZbigniew Bodek 	int rc = 0;
20749b8d05b8SZbigniew Bodek 
20759b8d05b8SZbigniew Bodek 	if (!device_is_attached(adapter->pdev)) {
20769b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not attached!\n");
20779b8d05b8SZbigniew Bodek 		return (ENXIO);
20789b8d05b8SZbigniew Bodek 	}
20799b8d05b8SZbigniew Bodek 
20809b8d05b8SZbigniew Bodek 	if (!adapter->running) {
20819b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is not running!\n");
20829b8d05b8SZbigniew Bodek 		return (ENXIO);
20839b8d05b8SZbigniew Bodek 	}
20849b8d05b8SZbigniew Bodek 
20859b8d05b8SZbigniew Bodek 	if (!adapter->up) {
20869b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going UP\n");
20879b8d05b8SZbigniew Bodek 
20889b8d05b8SZbigniew Bodek 		/* setup interrupts for IO queues */
20899b8d05b8SZbigniew Bodek 		ena_setup_io_intr(adapter);
20909b8d05b8SZbigniew Bodek 		rc = ena_request_io_irq(adapter);
20919b8d05b8SZbigniew Bodek 		if (rc) {
20929b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_req_irq");
20939b8d05b8SZbigniew Bodek 			goto err_req_irq;
20949b8d05b8SZbigniew Bodek 		}
20959b8d05b8SZbigniew Bodek 
20969b8d05b8SZbigniew Bodek 		/* allocate transmit descriptors */
20979b8d05b8SZbigniew Bodek 		rc = ena_setup_all_tx_resources(adapter);
20989b8d05b8SZbigniew Bodek 		if (rc) {
20999b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_tx");
21009b8d05b8SZbigniew Bodek 			goto err_setup_tx;
21019b8d05b8SZbigniew Bodek 		}
21029b8d05b8SZbigniew Bodek 
21039b8d05b8SZbigniew Bodek 		/* allocate receive descriptors */
21049b8d05b8SZbigniew Bodek 		rc = ena_setup_all_rx_resources(adapter);
21059b8d05b8SZbigniew Bodek 		if (rc) {
21069b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "err_setup_rx");
21079b8d05b8SZbigniew Bodek 			goto err_setup_rx;
21089b8d05b8SZbigniew Bodek 		}
21099b8d05b8SZbigniew Bodek 
21109b8d05b8SZbigniew Bodek 		/* create IO queues for Rx & Tx */
21119b8d05b8SZbigniew Bodek 		rc = ena_create_io_queues(adapter);
21129b8d05b8SZbigniew Bodek 		if (rc) {
21139b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT,
21149b8d05b8SZbigniew Bodek 			    "create IO queues failed");
21159b8d05b8SZbigniew Bodek 			goto err_io_que;
21169b8d05b8SZbigniew Bodek 		}
21179b8d05b8SZbigniew Bodek 
21189b8d05b8SZbigniew Bodek 		if (adapter->link_status)
21199b8d05b8SZbigniew Bodek 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
21209b8d05b8SZbigniew Bodek 
21219b8d05b8SZbigniew Bodek 		rc = ena_up_complete(adapter);
21229b8d05b8SZbigniew Bodek 		if (rc)
21239b8d05b8SZbigniew Bodek 			goto err_up_complete;
21249b8d05b8SZbigniew Bodek 
21259b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_up, 1);
21269b8d05b8SZbigniew Bodek 
21279b8d05b8SZbigniew Bodek 		ena_update_hwassist(adapter);
21289b8d05b8SZbigniew Bodek 
21299b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_RUNNING,
21309b8d05b8SZbigniew Bodek 		    IFF_DRV_OACTIVE);
21319b8d05b8SZbigniew Bodek 
21329b8d05b8SZbigniew Bodek 		callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
21339b8d05b8SZbigniew Bodek 		    ena_timer_service, (void *)adapter, 0);
21349b8d05b8SZbigniew Bodek 
21359b8d05b8SZbigniew Bodek 		adapter->up = true;
213693471047SZbigniew Bodek 
213793471047SZbigniew Bodek 		ena_unmask_all_io_irqs(adapter);
21389b8d05b8SZbigniew Bodek 	}
21399b8d05b8SZbigniew Bodek 
21409b8d05b8SZbigniew Bodek 	return (0);
21419b8d05b8SZbigniew Bodek 
21429b8d05b8SZbigniew Bodek err_up_complete:
21439b8d05b8SZbigniew Bodek 	ena_destroy_all_io_queues(adapter);
21449b8d05b8SZbigniew Bodek err_io_que:
21459b8d05b8SZbigniew Bodek 	ena_free_all_rx_resources(adapter);
21469b8d05b8SZbigniew Bodek err_setup_rx:
21479b8d05b8SZbigniew Bodek 	ena_free_all_tx_resources(adapter);
21489b8d05b8SZbigniew Bodek err_setup_tx:
21499b8d05b8SZbigniew Bodek 	ena_free_io_irq(adapter);
21509b8d05b8SZbigniew Bodek err_req_irq:
21519b8d05b8SZbigniew Bodek 	return (rc);
21529b8d05b8SZbigniew Bodek }
21539b8d05b8SZbigniew Bodek 
21549b8d05b8SZbigniew Bodek static uint64_t
21559b8d05b8SZbigniew Bodek ena_get_counter(if_t ifp, ift_counter cnt)
21569b8d05b8SZbigniew Bodek {
21579b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
21589b8d05b8SZbigniew Bodek 	struct ena_hw_stats *stats;
21599b8d05b8SZbigniew Bodek 
21609b8d05b8SZbigniew Bodek 	adapter = if_getsoftc(ifp);
21619b8d05b8SZbigniew Bodek 	stats = &adapter->hw_stats;
21629b8d05b8SZbigniew Bodek 
21639b8d05b8SZbigniew Bodek 	switch (cnt) {
21649b8d05b8SZbigniew Bodek 	case IFCOUNTER_IPACKETS:
216530217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_packets));
21669b8d05b8SZbigniew Bodek 	case IFCOUNTER_OPACKETS:
216730217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_packets));
21689b8d05b8SZbigniew Bodek 	case IFCOUNTER_IBYTES:
216930217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_bytes));
21709b8d05b8SZbigniew Bodek 	case IFCOUNTER_OBYTES:
217130217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->tx_bytes));
21729b8d05b8SZbigniew Bodek 	case IFCOUNTER_IQDROPS:
217330217e2dSMarcin Wojtas 		return (counter_u64_fetch(stats->rx_drops));
21749b8d05b8SZbigniew Bodek 	default:
21759b8d05b8SZbigniew Bodek 		return (if_get_counter_default(ifp, cnt));
21769b8d05b8SZbigniew Bodek 	}
21779b8d05b8SZbigniew Bodek }
21789b8d05b8SZbigniew Bodek 
21799b8d05b8SZbigniew Bodek static int
21809b8d05b8SZbigniew Bodek ena_media_change(if_t ifp)
21819b8d05b8SZbigniew Bodek {
21829b8d05b8SZbigniew Bodek 	/* Media Change is not supported by firmware */
21839b8d05b8SZbigniew Bodek 	return (0);
21849b8d05b8SZbigniew Bodek }
21859b8d05b8SZbigniew Bodek 
21869b8d05b8SZbigniew Bodek static void
21879b8d05b8SZbigniew Bodek ena_media_status(if_t ifp, struct ifmediareq *ifmr)
21889b8d05b8SZbigniew Bodek {
21899b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = if_getsoftc(ifp);
21909b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "enter");
21919b8d05b8SZbigniew Bodek 
21929b8d05b8SZbigniew Bodek 	ENA_DEV_LOCK;
21939b8d05b8SZbigniew Bodek 
21949b8d05b8SZbigniew Bodek 	ifmr->ifm_status = IFM_AVALID;
21959b8d05b8SZbigniew Bodek 	ifmr->ifm_active = IFM_ETHER;
21969b8d05b8SZbigniew Bodek 
21979b8d05b8SZbigniew Bodek 	if (!adapter->link_status) {
21989b8d05b8SZbigniew Bodek 		ENA_DEV_UNLOCK;
21999b8d05b8SZbigniew Bodek 		ena_trace(ENA_WARNING, "link_status = false");
22009b8d05b8SZbigniew Bodek 		return;
22019b8d05b8SZbigniew Bodek 	}
22029b8d05b8SZbigniew Bodek 
22039b8d05b8SZbigniew Bodek 	ifmr->ifm_status |= IFM_ACTIVE;
22049b8d05b8SZbigniew Bodek 	ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
22059b8d05b8SZbigniew Bodek 
22069b8d05b8SZbigniew Bodek 	ENA_DEV_UNLOCK;
22079b8d05b8SZbigniew Bodek 
22089b8d05b8SZbigniew Bodek 	return;
22099b8d05b8SZbigniew Bodek }
22109b8d05b8SZbigniew Bodek 
22119b8d05b8SZbigniew Bodek static void
22129b8d05b8SZbigniew Bodek ena_init(void *arg)
22139b8d05b8SZbigniew Bodek {
22149b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
22159b8d05b8SZbigniew Bodek 
22163d3a90f9SZbigniew Bodek 	if (adapter->up == false) {
22173d3a90f9SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
22189b8d05b8SZbigniew Bodek 		ena_up(adapter);
22193d3a90f9SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
22203d3a90f9SZbigniew Bodek 	}
22219b8d05b8SZbigniew Bodek 
22229b8d05b8SZbigniew Bodek 	return;
22239b8d05b8SZbigniew Bodek }
22249b8d05b8SZbigniew Bodek 
22259b8d05b8SZbigniew Bodek static int
22269b8d05b8SZbigniew Bodek ena_ioctl(if_t ifp, u_long command, caddr_t data)
22279b8d05b8SZbigniew Bodek {
22289b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
22299b8d05b8SZbigniew Bodek 	struct ifreq *ifr;
22309b8d05b8SZbigniew Bodek 	int rc;
22319b8d05b8SZbigniew Bodek 
22329b8d05b8SZbigniew Bodek 	adapter = ifp->if_softc;
22339b8d05b8SZbigniew Bodek 	ifr = (struct ifreq *)data;
22349b8d05b8SZbigniew Bodek 
22359b8d05b8SZbigniew Bodek 	/*
22369b8d05b8SZbigniew Bodek 	 * Acquiring lock to prevent from running up and down routines parallel.
22379b8d05b8SZbigniew Bodek 	 */
22389b8d05b8SZbigniew Bodek 	rc = 0;
22399b8d05b8SZbigniew Bodek 	switch (command) {
22409b8d05b8SZbigniew Bodek 	case SIOCSIFMTU:
2241e67c6554SZbigniew Bodek 		sx_xlock(&adapter->ioctl_sx);
22429b8d05b8SZbigniew Bodek 		ena_down(adapter);
22439b8d05b8SZbigniew Bodek 
22449b8d05b8SZbigniew Bodek 		ena_change_mtu(ifp, ifr->ifr_mtu);
22459b8d05b8SZbigniew Bodek 
22469b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
2247e67c6554SZbigniew Bodek 		sx_unlock(&adapter->ioctl_sx);
22489b8d05b8SZbigniew Bodek 		break;
22499b8d05b8SZbigniew Bodek 
22509b8d05b8SZbigniew Bodek 	case SIOCSIFFLAGS:
22519b8d05b8SZbigniew Bodek 		if (ifp->if_flags & IFF_UP) {
22529b8d05b8SZbigniew Bodek 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING)) {
22539b8d05b8SZbigniew Bodek 				if (ifp->if_flags & (IFF_PROMISC |
22549b8d05b8SZbigniew Bodek 				    IFF_ALLMULTI)) {
22559b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
22569b8d05b8SZbigniew Bodek 					    "ioctl promisc/allmulti\n");
22579b8d05b8SZbigniew Bodek 				}
22589b8d05b8SZbigniew Bodek 			} else {
2259e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
22609b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2261e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
22629b8d05b8SZbigniew Bodek 			}
22639b8d05b8SZbigniew Bodek 		} else {
2264e67c6554SZbigniew Bodek 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2265e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
22669b8d05b8SZbigniew Bodek 				ena_down(adapter);
2267e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
2268e67c6554SZbigniew Bodek 			}
22699b8d05b8SZbigniew Bodek 		}
22709b8d05b8SZbigniew Bodek 		break;
22719b8d05b8SZbigniew Bodek 
22729b8d05b8SZbigniew Bodek 	case SIOCADDMULTI:
22739b8d05b8SZbigniew Bodek 	case SIOCDELMULTI:
22749b8d05b8SZbigniew Bodek 		break;
22759b8d05b8SZbigniew Bodek 
22769b8d05b8SZbigniew Bodek 	case SIOCSIFMEDIA:
22779b8d05b8SZbigniew Bodek 	case SIOCGIFMEDIA:
22789b8d05b8SZbigniew Bodek 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
22799b8d05b8SZbigniew Bodek 		break;
22809b8d05b8SZbigniew Bodek 
22819b8d05b8SZbigniew Bodek 	case SIOCSIFCAP:
22829b8d05b8SZbigniew Bodek 		{
22839b8d05b8SZbigniew Bodek 			int reinit = 0;
22849b8d05b8SZbigniew Bodek 
22859b8d05b8SZbigniew Bodek 			if (ifr->ifr_reqcap != ifp->if_capenable) {
22869b8d05b8SZbigniew Bodek 				ifp->if_capenable = ifr->ifr_reqcap;
22879b8d05b8SZbigniew Bodek 				reinit = 1;
22889b8d05b8SZbigniew Bodek 			}
22899b8d05b8SZbigniew Bodek 
22909b8d05b8SZbigniew Bodek 			if (reinit && (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2291e67c6554SZbigniew Bodek 				sx_xlock(&adapter->ioctl_sx);
22929b8d05b8SZbigniew Bodek 				ena_down(adapter);
22939b8d05b8SZbigniew Bodek 				rc = ena_up(adapter);
2294e67c6554SZbigniew Bodek 				sx_unlock(&adapter->ioctl_sx);
22959b8d05b8SZbigniew Bodek 			}
22969b8d05b8SZbigniew Bodek 		}
22979b8d05b8SZbigniew Bodek 
22989b8d05b8SZbigniew Bodek 		break;
22999b8d05b8SZbigniew Bodek 	default:
23009b8d05b8SZbigniew Bodek 		rc = ether_ioctl(ifp, command, data);
23019b8d05b8SZbigniew Bodek 		break;
23029b8d05b8SZbigniew Bodek 	}
23039b8d05b8SZbigniew Bodek 
23049b8d05b8SZbigniew Bodek 	return (rc);
23059b8d05b8SZbigniew Bodek }
23069b8d05b8SZbigniew Bodek 
23079b8d05b8SZbigniew Bodek static int
23089b8d05b8SZbigniew Bodek ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
23099b8d05b8SZbigniew Bodek {
23109b8d05b8SZbigniew Bodek 	int caps = 0;
23119b8d05b8SZbigniew Bodek 
23129b8d05b8SZbigniew Bodek 	if (feat->offload.tx &
23139b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
23149b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
23159b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK))
23169b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM;
23179b8d05b8SZbigniew Bodek 
23189b8d05b8SZbigniew Bodek 	if (feat->offload.tx &
23199b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
23209b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK))
23219b8d05b8SZbigniew Bodek 		caps |= IFCAP_TXCSUM_IPV6;
23229b8d05b8SZbigniew Bodek 
23239b8d05b8SZbigniew Bodek 	if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK)
23249b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO4;
23259b8d05b8SZbigniew Bodek 
23269b8d05b8SZbigniew Bodek 	if (feat->offload.tx & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK)
23279b8d05b8SZbigniew Bodek 		caps |= IFCAP_TSO6;
23289b8d05b8SZbigniew Bodek 
23299b8d05b8SZbigniew Bodek 	if (feat->offload.rx_supported &
23309b8d05b8SZbigniew Bodek 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
23319b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK))
23329b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM;
23339b8d05b8SZbigniew Bodek 
23349b8d05b8SZbigniew Bodek 	if (feat->offload.rx_supported &
23359b8d05b8SZbigniew Bodek 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK)
23369b8d05b8SZbigniew Bodek 		caps |= IFCAP_RXCSUM_IPV6;
23379b8d05b8SZbigniew Bodek 
23389b8d05b8SZbigniew Bodek 	caps |= IFCAP_LRO | IFCAP_JUMBO_MTU;
23399b8d05b8SZbigniew Bodek 
23409b8d05b8SZbigniew Bodek 	return (caps);
23419b8d05b8SZbigniew Bodek }
23429b8d05b8SZbigniew Bodek 
23439b8d05b8SZbigniew Bodek static void
23449b8d05b8SZbigniew Bodek ena_update_host_info(struct ena_admin_host_info *host_info, if_t ifp)
23459b8d05b8SZbigniew Bodek {
23469b8d05b8SZbigniew Bodek 
23479b8d05b8SZbigniew Bodek 	host_info->supported_network_features[0] =
23489b8d05b8SZbigniew Bodek 	    (uint32_t)if_getcapabilities(ifp);
23499b8d05b8SZbigniew Bodek }
23509b8d05b8SZbigniew Bodek 
23519b8d05b8SZbigniew Bodek static void
23529b8d05b8SZbigniew Bodek ena_update_hwassist(struct ena_adapter *adapter)
23539b8d05b8SZbigniew Bodek {
23549b8d05b8SZbigniew Bodek 	if_t ifp = adapter->ifp;
23559b8d05b8SZbigniew Bodek 	uint32_t feat = adapter->tx_offload_cap;
23569b8d05b8SZbigniew Bodek 	int cap = if_getcapenable(ifp);
23579b8d05b8SZbigniew Bodek 	int flags = 0;
23589b8d05b8SZbigniew Bodek 
23599b8d05b8SZbigniew Bodek 	if_clearhwassist(ifp);
23609b8d05b8SZbigniew Bodek 
23619b8d05b8SZbigniew Bodek 	if (cap & IFCAP_TXCSUM) {
23629b8d05b8SZbigniew Bodek 		if (feat & ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)
23639b8d05b8SZbigniew Bodek 			flags |= CSUM_IP;
23649b8d05b8SZbigniew Bodek 		if (feat &
23659b8d05b8SZbigniew Bodek 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
23669b8d05b8SZbigniew Bodek 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK))
23679b8d05b8SZbigniew Bodek 			flags |= CSUM_IP_UDP | CSUM_IP_TCP;
23689b8d05b8SZbigniew Bodek 	}
23699b8d05b8SZbigniew Bodek 
23709b8d05b8SZbigniew Bodek 	if (cap & IFCAP_TXCSUM_IPV6)
23719b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_UDP | CSUM_IP6_TCP;
23729b8d05b8SZbigniew Bodek 
23739b8d05b8SZbigniew Bodek 	if (cap & IFCAP_TSO4)
23749b8d05b8SZbigniew Bodek 		flags |= CSUM_IP_TSO;
23759b8d05b8SZbigniew Bodek 
23769b8d05b8SZbigniew Bodek 	if (cap & IFCAP_TSO6)
23779b8d05b8SZbigniew Bodek 		flags |= CSUM_IP6_TSO;
23789b8d05b8SZbigniew Bodek 
23799b8d05b8SZbigniew Bodek 	if_sethwassistbits(ifp, flags, 0);
23809b8d05b8SZbigniew Bodek }
23819b8d05b8SZbigniew Bodek 
23829b8d05b8SZbigniew Bodek static int
23839b8d05b8SZbigniew Bodek ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
23849b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
23859b8d05b8SZbigniew Bodek {
23869b8d05b8SZbigniew Bodek 	if_t ifp;
23879b8d05b8SZbigniew Bodek 	int caps = 0;
23889b8d05b8SZbigniew Bodek 
23899b8d05b8SZbigniew Bodek 	ena_trace(ENA_DBG, "enter");
23909b8d05b8SZbigniew Bodek 
23919b8d05b8SZbigniew Bodek 	ifp = adapter->ifp = if_gethandle(IFT_ETHER);
23929b8d05b8SZbigniew Bodek 	if (ifp == 0) {
23939b8d05b8SZbigniew Bodek 		device_printf(pdev, "can not allocate ifnet structure\n");
23949b8d05b8SZbigniew Bodek 		return (ENXIO);
23959b8d05b8SZbigniew Bodek 	}
23969b8d05b8SZbigniew Bodek 	if_initname(ifp, device_get_name(pdev), device_get_unit(pdev));
23979b8d05b8SZbigniew Bodek 	if_setdev(ifp, pdev);
23989b8d05b8SZbigniew Bodek 	if_setsoftc(ifp, adapter);
23999b8d05b8SZbigniew Bodek 
24009b8d05b8SZbigniew Bodek 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
24019b8d05b8SZbigniew Bodek 	if_setinitfn(ifp, ena_init);
24029b8d05b8SZbigniew Bodek 	if_settransmitfn(ifp, ena_mq_start);
24039b8d05b8SZbigniew Bodek 	if_setqflushfn(ifp, ena_qflush);
24049b8d05b8SZbigniew Bodek 	if_setioctlfn(ifp, ena_ioctl);
24059b8d05b8SZbigniew Bodek 	if_setgetcounterfn(ifp, ena_get_counter);
24069b8d05b8SZbigniew Bodek 
24079b8d05b8SZbigniew Bodek 	if_setsendqlen(ifp, adapter->tx_ring_size);
24089b8d05b8SZbigniew Bodek 	if_setsendqready(ifp);
24099b8d05b8SZbigniew Bodek 	if_setmtu(ifp, ETHERMTU);
24109b8d05b8SZbigniew Bodek 	if_setbaudrate(ifp, 0);
24119b8d05b8SZbigniew Bodek 	/* Zeroize capabilities... */
24129b8d05b8SZbigniew Bodek 	if_setcapabilities(ifp, 0);
24139b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, 0);
24149b8d05b8SZbigniew Bodek 	/* check hardware support */
24159b8d05b8SZbigniew Bodek 	caps = ena_get_dev_offloads(feat);
24169b8d05b8SZbigniew Bodek 	/* ... and set them */
24179b8d05b8SZbigniew Bodek 	if_setcapabilitiesbit(ifp, caps, 0);
24189b8d05b8SZbigniew Bodek 
24199b8d05b8SZbigniew Bodek 	/* TSO parameters */
24208a573700SZbigniew Bodek 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
24218a573700SZbigniew Bodek 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
24228a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
24238a573700SZbigniew Bodek 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
24249b8d05b8SZbigniew Bodek 
24259b8d05b8SZbigniew Bodek 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
24269b8d05b8SZbigniew Bodek 	if_setcapenable(ifp, if_getcapabilities(ifp));
24279b8d05b8SZbigniew Bodek 
24289b8d05b8SZbigniew Bodek 	/*
24299b8d05b8SZbigniew Bodek 	 * Specify the media types supported by this adapter and register
24309b8d05b8SZbigniew Bodek 	 * callbacks to update media and link information
24319b8d05b8SZbigniew Bodek 	 */
24329b8d05b8SZbigniew Bodek 	ifmedia_init(&adapter->media, IFM_IMASK,
24339b8d05b8SZbigniew Bodek 	    ena_media_change, ena_media_status);
24349b8d05b8SZbigniew Bodek 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
24359b8d05b8SZbigniew Bodek 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
24369b8d05b8SZbigniew Bodek 
24379b8d05b8SZbigniew Bodek 	ether_ifattach(ifp, adapter->mac_addr);
24389b8d05b8SZbigniew Bodek 
24399b8d05b8SZbigniew Bodek 	return (0);
24409b8d05b8SZbigniew Bodek }
24419b8d05b8SZbigniew Bodek 
24429b8d05b8SZbigniew Bodek static void
24439b8d05b8SZbigniew Bodek ena_down(struct ena_adapter *adapter)
24449b8d05b8SZbigniew Bodek {
2445a195fab0SMarcin Wojtas 	int rc;
24469b8d05b8SZbigniew Bodek 
24479b8d05b8SZbigniew Bodek 	if (adapter->up) {
24489b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "device is going DOWN\n");
24499b8d05b8SZbigniew Bodek 
24509b8d05b8SZbigniew Bodek 		callout_drain(&adapter->timer_service);
24519b8d05b8SZbigniew Bodek 
24529b8d05b8SZbigniew Bodek 		adapter->up = false;
24539b8d05b8SZbigniew Bodek 		if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE,
24549b8d05b8SZbigniew Bodek 		    IFF_DRV_RUNNING);
24559b8d05b8SZbigniew Bodek 
24569b8d05b8SZbigniew Bodek 		ena_free_io_irq(adapter);
24579b8d05b8SZbigniew Bodek 
2458a195fab0SMarcin Wojtas 		if (adapter->trigger_reset) {
2459a195fab0SMarcin Wojtas 			rc = ena_com_dev_reset(adapter->ena_dev,
2460a195fab0SMarcin Wojtas 			    adapter->reset_reason);
2461a195fab0SMarcin Wojtas 			if (rc)
2462a195fab0SMarcin Wojtas 				device_printf(adapter->pdev,
2463a195fab0SMarcin Wojtas 				    "Device reset failed\n");
2464a195fab0SMarcin Wojtas 		}
2465a195fab0SMarcin Wojtas 
24669b8d05b8SZbigniew Bodek 		ena_destroy_all_io_queues(adapter);
24679b8d05b8SZbigniew Bodek 
24689b8d05b8SZbigniew Bodek 		ena_free_all_tx_bufs(adapter);
24699b8d05b8SZbigniew Bodek 		ena_free_all_rx_bufs(adapter);
24709b8d05b8SZbigniew Bodek 		ena_free_all_tx_resources(adapter);
24719b8d05b8SZbigniew Bodek 		ena_free_all_rx_resources(adapter);
24729b8d05b8SZbigniew Bodek 
24739b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.interface_down, 1);
24749b8d05b8SZbigniew Bodek 	}
24759b8d05b8SZbigniew Bodek 
24769b8d05b8SZbigniew Bodek 	return;
24779b8d05b8SZbigniew Bodek }
24789b8d05b8SZbigniew Bodek 
24799b8d05b8SZbigniew Bodek static void
24809b8d05b8SZbigniew Bodek ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
24819b8d05b8SZbigniew Bodek {
24829b8d05b8SZbigniew Bodek 	struct ena_com_tx_meta *ena_meta;
24839b8d05b8SZbigniew Bodek 	struct ether_vlan_header *eh;
24849b8d05b8SZbigniew Bodek 	u32 mss;
24859b8d05b8SZbigniew Bodek 	bool offload;
24869b8d05b8SZbigniew Bodek 	uint16_t etype;
24879b8d05b8SZbigniew Bodek 	int ehdrlen;
24889b8d05b8SZbigniew Bodek 	struct ip *ip;
24899b8d05b8SZbigniew Bodek 	int iphlen;
24909b8d05b8SZbigniew Bodek 	struct tcphdr *th;
24919b8d05b8SZbigniew Bodek 
24929b8d05b8SZbigniew Bodek 	offload = false;
24939b8d05b8SZbigniew Bodek 	ena_meta = &ena_tx_ctx->ena_meta;
24949b8d05b8SZbigniew Bodek 	mss = mbuf->m_pkthdr.tso_segsz;
24959b8d05b8SZbigniew Bodek 
24969b8d05b8SZbigniew Bodek 	if (mss != 0)
24979b8d05b8SZbigniew Bodek 		offload = true;
24989b8d05b8SZbigniew Bodek 
24999b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0)
25009b8d05b8SZbigniew Bodek 		offload = true;
25019b8d05b8SZbigniew Bodek 
25029b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
25039b8d05b8SZbigniew Bodek 		offload = true;
25049b8d05b8SZbigniew Bodek 
25059b8d05b8SZbigniew Bodek 	if (offload == false) {
25069b8d05b8SZbigniew Bodek 		ena_tx_ctx->meta_valid = 0;
25079b8d05b8SZbigniew Bodek 		return;
25089b8d05b8SZbigniew Bodek 	}
25099b8d05b8SZbigniew Bodek 
25109b8d05b8SZbigniew Bodek 	/* Determine where frame payload starts. */
25119b8d05b8SZbigniew Bodek 	eh = mtod(mbuf, struct ether_vlan_header *);
25129b8d05b8SZbigniew Bodek 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
25139b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_proto);
25149b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
25159b8d05b8SZbigniew Bodek 	} else {
25169b8d05b8SZbigniew Bodek 		etype = ntohs(eh->evl_encap_proto);
25179b8d05b8SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN;
25189b8d05b8SZbigniew Bodek 	}
25199b8d05b8SZbigniew Bodek 
25209b8d05b8SZbigniew Bodek 	ip = (struct ip *)(mbuf->m_data + ehdrlen);
25219b8d05b8SZbigniew Bodek 	iphlen = ip->ip_hl << 2;
25229b8d05b8SZbigniew Bodek 	th = (struct tcphdr *)((caddr_t)ip + iphlen);
25239b8d05b8SZbigniew Bodek 
25249b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_IP) != 0) {
25259b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_csum_enable = 1;
25269b8d05b8SZbigniew Bodek 	}
25279b8d05b8SZbigniew Bodek 	if ((mbuf->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
25289b8d05b8SZbigniew Bodek 		ena_tx_ctx->tso_enable = 1;
25299b8d05b8SZbigniew Bodek 		ena_meta->l4_hdr_len = (th->th_off);
25309b8d05b8SZbigniew Bodek 	}
25319b8d05b8SZbigniew Bodek 
25329b8d05b8SZbigniew Bodek 	switch (etype) {
25339b8d05b8SZbigniew Bodek 	case ETHERTYPE_IP:
25349b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
25359b8d05b8SZbigniew Bodek 		if (ip->ip_off == 0)
25369b8d05b8SZbigniew Bodek 			ena_tx_ctx->df = 1;
25379b8d05b8SZbigniew Bodek 		break;
25389b8d05b8SZbigniew Bodek 	case ETHERTYPE_IPV6:
25399b8d05b8SZbigniew Bodek 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
25409b8d05b8SZbigniew Bodek 
25419b8d05b8SZbigniew Bodek 	default:
25429b8d05b8SZbigniew Bodek 		break;
25439b8d05b8SZbigniew Bodek 	}
25449b8d05b8SZbigniew Bodek 
25459b8d05b8SZbigniew Bodek 	if (ip->ip_p == IPPROTO_TCP) {
25469b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
25479b8d05b8SZbigniew Bodek 		if (mbuf->m_pkthdr.csum_flags & (CSUM_IP_TCP | CSUM_IP6_TCP))
25489b8d05b8SZbigniew Bodek 		    ena_tx_ctx->l4_csum_enable = 1;
25499b8d05b8SZbigniew Bodek 		else
25509b8d05b8SZbigniew Bodek 		    ena_tx_ctx->l4_csum_enable = 0;
25519b8d05b8SZbigniew Bodek 	} else if (ip->ip_p == IPPROTO_UDP) {
25529b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
25539b8d05b8SZbigniew Bodek 		if (mbuf->m_pkthdr.csum_flags & (CSUM_IP_UDP | CSUM_IP6_UDP))
25549b8d05b8SZbigniew Bodek 		    ena_tx_ctx->l4_csum_enable = 1;
25559b8d05b8SZbigniew Bodek 		else
25569b8d05b8SZbigniew Bodek 		    ena_tx_ctx->l4_csum_enable = 0;
25579b8d05b8SZbigniew Bodek 	} else {
25589b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
25599b8d05b8SZbigniew Bodek 		ena_tx_ctx->l4_csum_enable = 0;
25609b8d05b8SZbigniew Bodek 	}
25619b8d05b8SZbigniew Bodek 
25629b8d05b8SZbigniew Bodek 	ena_meta->mss = mss;
25639b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_len = iphlen;
25649b8d05b8SZbigniew Bodek 	ena_meta->l3_hdr_offset = ehdrlen;
25659b8d05b8SZbigniew Bodek 	ena_tx_ctx->meta_valid = 1;
25669b8d05b8SZbigniew Bodek }
25679b8d05b8SZbigniew Bodek 
25689b8d05b8SZbigniew Bodek static int
25691b069f1cSZbigniew Bodek ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
25701e9fb899SZbigniew Bodek {
25711e9fb899SZbigniew Bodek 	struct ena_adapter *adapter;
25721b069f1cSZbigniew Bodek 	struct mbuf *collapsed_mbuf;
25731e9fb899SZbigniew Bodek 	int num_frags;
25741e9fb899SZbigniew Bodek 
25751e9fb899SZbigniew Bodek 	adapter = tx_ring->adapter;
25761e9fb899SZbigniew Bodek 	num_frags = ena_mbuf_count(*mbuf);
25771e9fb899SZbigniew Bodek 
25781e9fb899SZbigniew Bodek 	/* One segment must be reserved for configuration descriptor. */
25791e9fb899SZbigniew Bodek 	if (num_frags < adapter->max_tx_sgl_size)
25801e9fb899SZbigniew Bodek 		return (0);
25811b069f1cSZbigniew Bodek 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
25821e9fb899SZbigniew Bodek 
25831b069f1cSZbigniew Bodek 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
25841b069f1cSZbigniew Bodek 	    adapter->max_tx_sgl_size - 1);
25851b069f1cSZbigniew Bodek 	if (collapsed_mbuf == NULL) {
25861b069f1cSZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
25871e9fb899SZbigniew Bodek 		return (ENOMEM);
25881e9fb899SZbigniew Bodek 	}
25891e9fb899SZbigniew Bodek 
25901b069f1cSZbigniew Bodek 	/* If mbuf was collapsed succesfully, original mbuf is released. */
25911b069f1cSZbigniew Bodek 	*mbuf = collapsed_mbuf;
25921e9fb899SZbigniew Bodek 
25931e9fb899SZbigniew Bodek 	return (0);
25941e9fb899SZbigniew Bodek }
25951e9fb899SZbigniew Bodek 
25961e9fb899SZbigniew Bodek static int
25971e9fb899SZbigniew Bodek ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
25989b8d05b8SZbigniew Bodek {
25999b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
26009b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info;
26019b8d05b8SZbigniew Bodek 	struct ena_com_tx_ctx ena_tx_ctx;
26029b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev;
26039b8d05b8SZbigniew Bodek 	struct ena_com_buf *ena_buf;
26049b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
26059b8d05b8SZbigniew Bodek 	bus_dma_segment_t segs[ENA_BUS_DMA_SEGS];
26069b8d05b8SZbigniew Bodek 	void *push_hdr;
26079b8d05b8SZbigniew Bodek 	uint16_t next_to_use;
26089b8d05b8SZbigniew Bodek 	uint16_t req_id;
26099b8d05b8SZbigniew Bodek 	uint16_t push_len;
26109b8d05b8SZbigniew Bodek 	uint16_t ena_qid;
26119b8d05b8SZbigniew Bodek 	uint32_t len, nsegs, header_len;
26129b8d05b8SZbigniew Bodek 	int i, rc;
26131e9fb899SZbigniew Bodek 	int nb_hw_desc;
26149b8d05b8SZbigniew Bodek 
26159b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
26169b8d05b8SZbigniew Bodek 	adapter = tx_ring->que->adapter;
26179b8d05b8SZbigniew Bodek 	ena_dev = adapter->ena_dev;
26189b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
26199b8d05b8SZbigniew Bodek 
26201e9fb899SZbigniew Bodek 	ENA_ASSERT(*mbuf, "mbuf is NULL\n");
26219b8d05b8SZbigniew Bodek 
26221b069f1cSZbigniew Bodek 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
26231e9fb899SZbigniew Bodek 	if (rc) {
26241e9fb899SZbigniew Bodek 		ena_trace(ENA_WARNING,
26251b069f1cSZbigniew Bodek 		    "Failed to collapse mbuf! err: %d", rc);
26261e9fb899SZbigniew Bodek 		return (rc);
26279b8d05b8SZbigniew Bodek 	}
26289b8d05b8SZbigniew Bodek 
26299b8d05b8SZbigniew Bodek 	next_to_use = tx_ring->next_to_use;
26309b8d05b8SZbigniew Bodek 	req_id = tx_ring->free_tx_ids[next_to_use];
26319b8d05b8SZbigniew Bodek 	tx_info = &tx_ring->tx_buffer_info[req_id];
26329b8d05b8SZbigniew Bodek 
26331e9fb899SZbigniew Bodek 	tx_info->mbuf = *mbuf;
26349b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = 0;
26359b8d05b8SZbigniew Bodek 
26369b8d05b8SZbigniew Bodek 	ena_buf = tx_info->bufs;
26371e9fb899SZbigniew Bodek 	len = (*mbuf)->m_len;
26389b8d05b8SZbigniew Bodek 
26391e9fb899SZbigniew Bodek 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
26409b8d05b8SZbigniew Bodek 
26419b8d05b8SZbigniew Bodek 	push_len = 0;
26429b8d05b8SZbigniew Bodek 	header_len = min_t(uint32_t, len, tx_ring->tx_max_header_size);
26439b8d05b8SZbigniew Bodek 	push_hdr = NULL;
26449b8d05b8SZbigniew Bodek 
26459b8d05b8SZbigniew Bodek 	rc = bus_dmamap_load_mbuf_sg(adapter->tx_buf_tag, tx_info->map,
26461e9fb899SZbigniew Bodek 	    *mbuf, segs, &nsegs, BUS_DMA_NOWAIT);
26479b8d05b8SZbigniew Bodek 
26489b8d05b8SZbigniew Bodek 	if (rc || (nsegs == 0)) {
26499b8d05b8SZbigniew Bodek 		ena_trace(ENA_WARNING,
26509b8d05b8SZbigniew Bodek 		    "dmamap load failed! err: %d nsegs: %d", rc, nsegs);
26519b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
26529b8d05b8SZbigniew Bodek 		tx_info->mbuf = NULL;
26539b8d05b8SZbigniew Bodek 		if (rc == ENOMEM)
26549b8d05b8SZbigniew Bodek 			return (ENA_COM_NO_MEM);
26559b8d05b8SZbigniew Bodek 		else
26569b8d05b8SZbigniew Bodek 			return (ENA_COM_INVAL);
26579b8d05b8SZbigniew Bodek 	}
26589b8d05b8SZbigniew Bodek 
26599b8d05b8SZbigniew Bodek 	for (i = 0; i < nsegs; i++) {
26609b8d05b8SZbigniew Bodek 		ena_buf->len = segs[i].ds_len;
26619b8d05b8SZbigniew Bodek 		ena_buf->paddr = segs[i].ds_addr;
26629b8d05b8SZbigniew Bodek 		ena_buf++;
26639b8d05b8SZbigniew Bodek 	}
26649b8d05b8SZbigniew Bodek 	tx_info->num_of_bufs = nsegs;
26659b8d05b8SZbigniew Bodek 
26669b8d05b8SZbigniew Bodek 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
26679b8d05b8SZbigniew Bodek 	ena_tx_ctx.ena_bufs = tx_info->bufs;
26689b8d05b8SZbigniew Bodek 	ena_tx_ctx.push_header = push_hdr;
26699b8d05b8SZbigniew Bodek 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
26709b8d05b8SZbigniew Bodek 	ena_tx_ctx.req_id = req_id;
26719b8d05b8SZbigniew Bodek 	ena_tx_ctx.header_len = header_len;
26729b8d05b8SZbigniew Bodek 
26739b8d05b8SZbigniew Bodek 	/* Set flags and meta data */
26741e9fb899SZbigniew Bodek 	ena_tx_csum(&ena_tx_ctx, *mbuf);
26759b8d05b8SZbigniew Bodek 	/* Prepare the packet's descriptors and send them to device */
26769b8d05b8SZbigniew Bodek 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
26779b8d05b8SZbigniew Bodek 	if (rc != 0) {
26789b8d05b8SZbigniew Bodek 		ena_trace(ENA_WARNING, "failed to prepare tx bufs\n");
26799b8d05b8SZbigniew Bodek 		counter_enter();
26809b8d05b8SZbigniew Bodek 		counter_u64_add_protected(tx_ring->tx_stats.queue_stop, 1);
26819b8d05b8SZbigniew Bodek 		counter_u64_add_protected(tx_ring->tx_stats.prepare_ctx_err, 1);
26829b8d05b8SZbigniew Bodek 		counter_exit();
26839b8d05b8SZbigniew Bodek 		goto dma_error;
26849b8d05b8SZbigniew Bodek 	}
26859b8d05b8SZbigniew Bodek 
26869b8d05b8SZbigniew Bodek 	counter_enter();
26879b8d05b8SZbigniew Bodek 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
26881e9fb899SZbigniew Bodek 	counter_u64_add_protected(tx_ring->tx_stats.bytes,  (*mbuf)->m_pkthdr.len);
268930217e2dSMarcin Wojtas 
269030217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
269130217e2dSMarcin Wojtas 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
269230217e2dSMarcin Wojtas 	    (*mbuf)->m_pkthdr.len);
26939b8d05b8SZbigniew Bodek 	counter_exit();
26949b8d05b8SZbigniew Bodek 
26959b8d05b8SZbigniew Bodek 	tx_info->tx_descs = nb_hw_desc;
26969b8d05b8SZbigniew Bodek 	getbinuptime(&tx_info->timestamp);
26979b8d05b8SZbigniew Bodek 	tx_info->print_once = true;
26989b8d05b8SZbigniew Bodek 
26999b8d05b8SZbigniew Bodek 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
27009b8d05b8SZbigniew Bodek 	    tx_ring->ring_size);
27019b8d05b8SZbigniew Bodek 
27029b8d05b8SZbigniew Bodek 	bus_dmamap_sync(adapter->tx_buf_tag, tx_info->map, BUS_DMASYNC_PREWRITE);
27039b8d05b8SZbigniew Bodek 
27049b8d05b8SZbigniew Bodek 	return (0);
27059b8d05b8SZbigniew Bodek 
27069b8d05b8SZbigniew Bodek dma_error:
27079b8d05b8SZbigniew Bodek 	tx_info->mbuf = NULL;
27089b8d05b8SZbigniew Bodek 	bus_dmamap_unload(adapter->tx_buf_tag, tx_info->map);
27099b8d05b8SZbigniew Bodek 
27109b8d05b8SZbigniew Bodek 	return (rc);
27119b8d05b8SZbigniew Bodek }
27129b8d05b8SZbigniew Bodek 
27139b8d05b8SZbigniew Bodek static void
27149b8d05b8SZbigniew Bodek ena_start_xmit(struct ena_ring *tx_ring)
27159b8d05b8SZbigniew Bodek {
27169b8d05b8SZbigniew Bodek 	struct mbuf *mbuf;
27179b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = tx_ring->adapter;
27189b8d05b8SZbigniew Bodek 	struct ena_com_io_sq* io_sq;
27199b8d05b8SZbigniew Bodek 	int ena_qid;
27209b8d05b8SZbigniew Bodek 	int acum_pkts = 0;
27219b8d05b8SZbigniew Bodek 	int ret = 0;
27229b8d05b8SZbigniew Bodek 
27239b8d05b8SZbigniew Bodek 	if ((adapter->ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
27249b8d05b8SZbigniew Bodek 		return;
27259b8d05b8SZbigniew Bodek 
27269b8d05b8SZbigniew Bodek 	if (!adapter->link_status)
27279b8d05b8SZbigniew Bodek 		return;
27289b8d05b8SZbigniew Bodek 
27299b8d05b8SZbigniew Bodek 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
27309b8d05b8SZbigniew Bodek 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
27319b8d05b8SZbigniew Bodek 
27329b8d05b8SZbigniew Bodek 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
27339b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
27349b8d05b8SZbigniew Bodek 		    " header csum flags %#jx",
27359b8d05b8SZbigniew Bodek 		    mbuf, mbuf->m_flags, mbuf->m_pkthdr.csum_flags);
27369b8d05b8SZbigniew Bodek 
2737a195fab0SMarcin Wojtas 		if (!ena_com_sq_have_enough_space(io_sq,
2738a195fab0SMarcin Wojtas 		    ENA_TX_CLEANUP_THRESHOLD))
27399b8d05b8SZbigniew Bodek 			ena_tx_cleanup(tx_ring);
27409b8d05b8SZbigniew Bodek 
27411e9fb899SZbigniew Bodek 		if ((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0) {
27429b8d05b8SZbigniew Bodek 			if (ret == ENA_COM_NO_MEM) {
27439b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
27449b8d05b8SZbigniew Bodek 			} else if (ret == ENA_COM_NO_SPACE) {
27459b8d05b8SZbigniew Bodek 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
27469b8d05b8SZbigniew Bodek 			} else {
27479b8d05b8SZbigniew Bodek 				m_freem(mbuf);
27489b8d05b8SZbigniew Bodek 				drbr_advance(adapter->ifp, tx_ring->br);
27499b8d05b8SZbigniew Bodek 			}
27509b8d05b8SZbigniew Bodek 
27519b8d05b8SZbigniew Bodek 			break;
27529b8d05b8SZbigniew Bodek 		}
27539b8d05b8SZbigniew Bodek 
2754b4b29032SZbigniew Bodek 		drbr_advance(adapter->ifp, tx_ring->br);
2755b4b29032SZbigniew Bodek 
27569b8d05b8SZbigniew Bodek 		if ((adapter->ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
27579b8d05b8SZbigniew Bodek 			return;
27589b8d05b8SZbigniew Bodek 
27599b8d05b8SZbigniew Bodek 		acum_pkts++;
27609b8d05b8SZbigniew Bodek 
27619b8d05b8SZbigniew Bodek 		BPF_MTAP(adapter->ifp, mbuf);
27629b8d05b8SZbigniew Bodek 
27639b8d05b8SZbigniew Bodek 		if (acum_pkts == DB_THRESHOLD) {
27649b8d05b8SZbigniew Bodek 			acum_pkts = 0;
27659b8d05b8SZbigniew Bodek 			wmb();
27669b8d05b8SZbigniew Bodek 			/* Trigger the dma engine */
27679b8d05b8SZbigniew Bodek 			ena_com_write_sq_doorbell(io_sq);
27689b8d05b8SZbigniew Bodek 			counter_u64_add(tx_ring->tx_stats.doorbells, 1);
27699b8d05b8SZbigniew Bodek 		}
27709b8d05b8SZbigniew Bodek 
27719b8d05b8SZbigniew Bodek 	}
27729b8d05b8SZbigniew Bodek 
27739b8d05b8SZbigniew Bodek 	if (acum_pkts) {
27749b8d05b8SZbigniew Bodek 		wmb();
27759b8d05b8SZbigniew Bodek 		/* Trigger the dma engine */
27769b8d05b8SZbigniew Bodek 		ena_com_write_sq_doorbell(io_sq);
27779b8d05b8SZbigniew Bodek 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
27789b8d05b8SZbigniew Bodek 	}
27799b8d05b8SZbigniew Bodek 
2780a195fab0SMarcin Wojtas 	if (!ena_com_sq_have_enough_space(io_sq,
2781a195fab0SMarcin Wojtas 	    ENA_TX_CLEANUP_THRESHOLD))
27829b8d05b8SZbigniew Bodek 		ena_tx_cleanup(tx_ring);
27839b8d05b8SZbigniew Bodek }
27849b8d05b8SZbigniew Bodek 
27859b8d05b8SZbigniew Bodek static void
27869b8d05b8SZbigniew Bodek ena_deferred_mq_start(void *arg, int pending)
27879b8d05b8SZbigniew Bodek {
27889b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
27899b8d05b8SZbigniew Bodek 	struct ifnet *ifp = tx_ring->adapter->ifp;
27909b8d05b8SZbigniew Bodek 
27919b8d05b8SZbigniew Bodek 	while (drbr_empty(ifp, tx_ring->br) == FALSE &&
27929b8d05b8SZbigniew Bodek 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
27939b8d05b8SZbigniew Bodek 		ENA_RING_MTX_LOCK(tx_ring);
27949b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
27959b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
27969b8d05b8SZbigniew Bodek 	}
27979b8d05b8SZbigniew Bodek }
27989b8d05b8SZbigniew Bodek 
27999b8d05b8SZbigniew Bodek static int
28009b8d05b8SZbigniew Bodek ena_mq_start(if_t ifp, struct mbuf *m)
28019b8d05b8SZbigniew Bodek {
28029b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
28039b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
28049b8d05b8SZbigniew Bodek 	int ret, is_drbr_empty;
28059b8d05b8SZbigniew Bodek 	uint32_t i;
28069b8d05b8SZbigniew Bodek 
28079b8d05b8SZbigniew Bodek 	if ((adapter->ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
28089b8d05b8SZbigniew Bodek 		return (ENODEV);
28099b8d05b8SZbigniew Bodek 
28109b8d05b8SZbigniew Bodek 	/* Which queue to use */
28119b8d05b8SZbigniew Bodek 	/*
28129b8d05b8SZbigniew Bodek 	 * If everything is setup correctly, it should be the
28139b8d05b8SZbigniew Bodek 	 * same bucket that the current CPU we're on is.
28149b8d05b8SZbigniew Bodek 	 * It should improve performance.
28159b8d05b8SZbigniew Bodek 	 */
28169b8d05b8SZbigniew Bodek 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
28179b8d05b8SZbigniew Bodek #ifdef	RSS
28189b8d05b8SZbigniew Bodek 		if (rss_hash2bucket(m->m_pkthdr.flowid,
28199b8d05b8SZbigniew Bodek 		    M_HASHTYPE_GET(m), &i) == 0) {
28209b8d05b8SZbigniew Bodek 			i = i % adapter->num_queues;
28219b8d05b8SZbigniew Bodek 
28229b8d05b8SZbigniew Bodek 		} else
28239b8d05b8SZbigniew Bodek #endif
28249b8d05b8SZbigniew Bodek 		{
28259b8d05b8SZbigniew Bodek 			i = m->m_pkthdr.flowid % adapter->num_queues;
28269b8d05b8SZbigniew Bodek 		}
28279b8d05b8SZbigniew Bodek 	} else {
28289b8d05b8SZbigniew Bodek 		i = curcpu % adapter->num_queues;
28299b8d05b8SZbigniew Bodek 	}
28309b8d05b8SZbigniew Bodek 	tx_ring = &adapter->tx_ring[i];
28319b8d05b8SZbigniew Bodek 
28329b8d05b8SZbigniew Bodek 	/* Check if drbr is empty before putting packet */
28339b8d05b8SZbigniew Bodek 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
28349b8d05b8SZbigniew Bodek 	ret = drbr_enqueue(ifp, tx_ring->br, m);
28359b8d05b8SZbigniew Bodek 	if (ret) {
28369b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
28379b8d05b8SZbigniew Bodek 		return (ret);
28389b8d05b8SZbigniew Bodek 	}
28399b8d05b8SZbigniew Bodek 
28409b8d05b8SZbigniew Bodek 	if (is_drbr_empty && ENA_RING_MTX_TRYLOCK(tx_ring)) {
28419b8d05b8SZbigniew Bodek 		ena_start_xmit(tx_ring);
28429b8d05b8SZbigniew Bodek 		ENA_RING_MTX_UNLOCK(tx_ring);
28439b8d05b8SZbigniew Bodek 	} else {
28449b8d05b8SZbigniew Bodek 		taskqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
28459b8d05b8SZbigniew Bodek 	}
28469b8d05b8SZbigniew Bodek 
28479b8d05b8SZbigniew Bodek 	return (0);
28489b8d05b8SZbigniew Bodek }
28499b8d05b8SZbigniew Bodek 
28509b8d05b8SZbigniew Bodek static void
28519b8d05b8SZbigniew Bodek ena_qflush(if_t ifp)
28529b8d05b8SZbigniew Bodek {
28539b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = ifp->if_softc;
28549b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring = adapter->tx_ring;
28559b8d05b8SZbigniew Bodek 	int i;
28569b8d05b8SZbigniew Bodek 
28579b8d05b8SZbigniew Bodek 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
28589b8d05b8SZbigniew Bodek 		if (drbr_empty(ifp, tx_ring->br) == FALSE) {
28599b8d05b8SZbigniew Bodek 			ENA_RING_MTX_LOCK(tx_ring);
28609b8d05b8SZbigniew Bodek 			drbr_flush(ifp, tx_ring->br);
28619b8d05b8SZbigniew Bodek 			ENA_RING_MTX_UNLOCK(tx_ring);
28629b8d05b8SZbigniew Bodek 		}
28639b8d05b8SZbigniew Bodek 
28649b8d05b8SZbigniew Bodek 	if_qflush(ifp);
28659b8d05b8SZbigniew Bodek 
28669b8d05b8SZbigniew Bodek 	return;
28679b8d05b8SZbigniew Bodek }
28689b8d05b8SZbigniew Bodek 
28699b8d05b8SZbigniew Bodek static int ena_calc_io_queue_num(struct ena_adapter *adapter,
28709b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *get_feat_ctx)
28719b8d05b8SZbigniew Bodek {
28729b8d05b8SZbigniew Bodek 	int io_sq_num, io_cq_num, io_queue_num;
28739b8d05b8SZbigniew Bodek 
28749b8d05b8SZbigniew Bodek 	io_sq_num = get_feat_ctx->max_queues.max_sq_num;
28759b8d05b8SZbigniew Bodek 	io_cq_num = get_feat_ctx->max_queues.max_sq_num;
28769b8d05b8SZbigniew Bodek 
28779b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
28789b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_sq_num);
28799b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, io_cq_num);
28809b8d05b8SZbigniew Bodek 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
28819b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num,
28829b8d05b8SZbigniew Bodek 	    pci_msix_count(adapter->pdev) - 1);
28839b8d05b8SZbigniew Bodek #ifdef	RSS
28849b8d05b8SZbigniew Bodek 	io_queue_num = min_t(int, io_queue_num, rss_getnumbuckets());
28859b8d05b8SZbigniew Bodek #endif
28869b8d05b8SZbigniew Bodek 
28879b8d05b8SZbigniew Bodek 	return io_queue_num;
28889b8d05b8SZbigniew Bodek }
28899b8d05b8SZbigniew Bodek 
28909b8d05b8SZbigniew Bodek static int ena_calc_queue_size(struct ena_adapter *adapter,
28919b8d05b8SZbigniew Bodek     uint16_t *max_tx_sgl_size,  uint16_t *max_rx_sgl_size,
28929b8d05b8SZbigniew Bodek     struct ena_com_dev_get_features_ctx *feat)
28939b8d05b8SZbigniew Bodek {
28949b8d05b8SZbigniew Bodek 	uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
28959b8d05b8SZbigniew Bodek 	uint32_t v;
28969b8d05b8SZbigniew Bodek 	uint32_t q;
28979b8d05b8SZbigniew Bodek 
28989b8d05b8SZbigniew Bodek 	queue_size = min_t(uint32_t, queue_size,
28999b8d05b8SZbigniew Bodek 	    feat->max_queues.max_cq_depth);
29009b8d05b8SZbigniew Bodek 	queue_size = min_t(uint32_t, queue_size,
29019b8d05b8SZbigniew Bodek 	    feat->max_queues.max_sq_depth);
29029b8d05b8SZbigniew Bodek 
29039b8d05b8SZbigniew Bodek 	/* round down to the nearest power of 2 */
29049b8d05b8SZbigniew Bodek 	v = queue_size;
29059b8d05b8SZbigniew Bodek 	while (v != 0) {
29069b8d05b8SZbigniew Bodek 		if (powerof2(queue_size))
29079b8d05b8SZbigniew Bodek 			break;
29089b8d05b8SZbigniew Bodek 		v /= 2;
29099b8d05b8SZbigniew Bodek 		q = rounddown2(queue_size, v);
29109b8d05b8SZbigniew Bodek 		if (q != 0) {
29119b8d05b8SZbigniew Bodek 			queue_size = q;
29129b8d05b8SZbigniew Bodek 			break;
29139b8d05b8SZbigniew Bodek 		}
29149b8d05b8SZbigniew Bodek 	}
29159b8d05b8SZbigniew Bodek 
29169b8d05b8SZbigniew Bodek 	if (unlikely(!queue_size)) {
29179b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Invalid queue size\n");
29189b8d05b8SZbigniew Bodek 		return ENA_COM_FAULT;
29199b8d05b8SZbigniew Bodek 	}
29209b8d05b8SZbigniew Bodek 
29219b8d05b8SZbigniew Bodek 	*max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
29229b8d05b8SZbigniew Bodek 	    feat->max_queues.max_packet_tx_descs);
29239b8d05b8SZbigniew Bodek 	*max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
29249b8d05b8SZbigniew Bodek 	    feat->max_queues.max_packet_rx_descs);
29259b8d05b8SZbigniew Bodek 
29269b8d05b8SZbigniew Bodek 	return queue_size;
29279b8d05b8SZbigniew Bodek }
29289b8d05b8SZbigniew Bodek 
29299b8d05b8SZbigniew Bodek static int ena_rss_init_default(struct ena_adapter *adapter)
29309b8d05b8SZbigniew Bodek {
29319b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
29329b8d05b8SZbigniew Bodek 	device_t dev = adapter->pdev;
29339b8d05b8SZbigniew Bodek 	int qid, rc, i;
29349b8d05b8SZbigniew Bodek 
29359b8d05b8SZbigniew Bodek 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
29369b8d05b8SZbigniew Bodek 	if (unlikely(rc)) {
29379b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot init RSS\n");
29389b8d05b8SZbigniew Bodek 		goto err_rss_init;
29399b8d05b8SZbigniew Bodek 	}
29409b8d05b8SZbigniew Bodek 
29419b8d05b8SZbigniew Bodek 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
29429b8d05b8SZbigniew Bodek #ifdef	RSS
29439b8d05b8SZbigniew Bodek 		qid = rss_get_indirection_to_bucket(i);
29449b8d05b8SZbigniew Bodek 		qid = qid % adapter->num_queues;
29459b8d05b8SZbigniew Bodek #else
29469b8d05b8SZbigniew Bodek 		qid = i % adapter->num_queues;
29479b8d05b8SZbigniew Bodek #endif
29489b8d05b8SZbigniew Bodek 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
29499b8d05b8SZbigniew Bodek 						       ENA_IO_RXQ_IDX(qid));
2950a195fab0SMarcin Wojtas 		if (unlikely(rc && (rc != EOPNOTSUPP))) {
29519b8d05b8SZbigniew Bodek 			device_printf(dev, "Cannot fill indirect table\n");
29529b8d05b8SZbigniew Bodek 			goto err_fill_indir;
29539b8d05b8SZbigniew Bodek 		}
29549b8d05b8SZbigniew Bodek 	}
29559b8d05b8SZbigniew Bodek 
29569b8d05b8SZbigniew Bodek 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
29579b8d05b8SZbigniew Bodek 					ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
2958a195fab0SMarcin Wojtas 	if (unlikely(rc && (rc != EOPNOTSUPP))) {
29599b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash function\n");
29609b8d05b8SZbigniew Bodek 		goto err_fill_indir;
29619b8d05b8SZbigniew Bodek 	}
29629b8d05b8SZbigniew Bodek 
29639b8d05b8SZbigniew Bodek 	rc = ena_com_set_default_hash_ctrl(ena_dev);
2964a195fab0SMarcin Wojtas 	if (unlikely(rc && (rc != EOPNOTSUPP))) {
29659b8d05b8SZbigniew Bodek 		device_printf(dev, "Cannot fill hash control\n");
29669b8d05b8SZbigniew Bodek 		goto err_fill_indir;
29679b8d05b8SZbigniew Bodek 	}
29689b8d05b8SZbigniew Bodek 
29699b8d05b8SZbigniew Bodek 	return (0);
29709b8d05b8SZbigniew Bodek 
29719b8d05b8SZbigniew Bodek err_fill_indir:
29729b8d05b8SZbigniew Bodek 	ena_com_rss_destroy(ena_dev);
29739b8d05b8SZbigniew Bodek err_rss_init:
29749b8d05b8SZbigniew Bodek 	return (rc);
29759b8d05b8SZbigniew Bodek }
29769b8d05b8SZbigniew Bodek 
29779b8d05b8SZbigniew Bodek static void
29789b8d05b8SZbigniew Bodek ena_rss_init_default_deferred(void *arg)
29799b8d05b8SZbigniew Bodek {
29809b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
29819b8d05b8SZbigniew Bodek 	devclass_t dc;
29829b8d05b8SZbigniew Bodek 	int max;
29839b8d05b8SZbigniew Bodek 	int rc;
29849b8d05b8SZbigniew Bodek 
29859b8d05b8SZbigniew Bodek 	dc = devclass_find("ena");
29869b8d05b8SZbigniew Bodek 	if (dc == NULL) {
29879b8d05b8SZbigniew Bodek 		ena_trace(ENA_DBG, "No devclass ena\n");
29889b8d05b8SZbigniew Bodek 		return;
29899b8d05b8SZbigniew Bodek 	}
29909b8d05b8SZbigniew Bodek 
29919b8d05b8SZbigniew Bodek 	max = devclass_get_maxunit(dc);
29929b8d05b8SZbigniew Bodek 	while (max-- >= 0) {
29939b8d05b8SZbigniew Bodek 		adapter = devclass_get_softc(dc, max);
29949b8d05b8SZbigniew Bodek 		if (adapter != NULL) {
29959b8d05b8SZbigniew Bodek 			rc = ena_rss_init_default(adapter);
29969b8d05b8SZbigniew Bodek 			adapter->rss_support = true;
29979b8d05b8SZbigniew Bodek 			if (rc) {
29989b8d05b8SZbigniew Bodek 				device_printf(adapter->pdev,
29999b8d05b8SZbigniew Bodek 				    "WARNING: RSS was not properly initialized,"
30009b8d05b8SZbigniew Bodek 				    " it will affect bandwith\n");
30019b8d05b8SZbigniew Bodek 				adapter->rss_support = false;
30029b8d05b8SZbigniew Bodek 			}
30039b8d05b8SZbigniew Bodek 		}
30049b8d05b8SZbigniew Bodek 	}
30059b8d05b8SZbigniew Bodek }
30069b8d05b8SZbigniew Bodek SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
30079b8d05b8SZbigniew Bodek 
30089b8d05b8SZbigniew Bodek static void ena_config_host_info(struct ena_com_dev *ena_dev)
30099b8d05b8SZbigniew Bodek {
30109b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info;
30119b8d05b8SZbigniew Bodek 	int rc;
30129b8d05b8SZbigniew Bodek 
30139b8d05b8SZbigniew Bodek 	/* Allocate only the host info */
30149b8d05b8SZbigniew Bodek 	rc = ena_com_allocate_host_info(ena_dev);
30159b8d05b8SZbigniew Bodek 	if (rc) {
30169b8d05b8SZbigniew Bodek 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
30179b8d05b8SZbigniew Bodek 		return;
30189b8d05b8SZbigniew Bodek 	}
30199b8d05b8SZbigniew Bodek 
30209b8d05b8SZbigniew Bodek 	host_info = ena_dev->host_attr.host_info;
30219b8d05b8SZbigniew Bodek 
30229b8d05b8SZbigniew Bodek 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
30239b8d05b8SZbigniew Bodek 	host_info->kernel_ver = osreldate;
30249b8d05b8SZbigniew Bodek 
30259b8d05b8SZbigniew Bodek 	sprintf(host_info->kernel_ver_str, "%d", osreldate);
30269b8d05b8SZbigniew Bodek 	host_info->os_dist = 0;
30279b8d05b8SZbigniew Bodek 	strncpy(host_info->os_dist_str, osrelease,
30289b8d05b8SZbigniew Bodek 	    sizeof(host_info->os_dist_str) - 1);
30299b8d05b8SZbigniew Bodek 
30309b8d05b8SZbigniew Bodek 	host_info->driver_version =
30319b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MAJOR) |
30329b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
30339b8d05b8SZbigniew Bodek 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
30349b8d05b8SZbigniew Bodek 
30359b8d05b8SZbigniew Bodek 	rc = ena_com_set_host_attributes(ena_dev);
30369b8d05b8SZbigniew Bodek 	if (rc) {
3037a195fab0SMarcin Wojtas 		if (rc == EOPNOTSUPP)
30389b8d05b8SZbigniew Bodek 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
30399b8d05b8SZbigniew Bodek 		else
30409b8d05b8SZbigniew Bodek 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
30419b8d05b8SZbigniew Bodek 
30429b8d05b8SZbigniew Bodek 		goto err;
30439b8d05b8SZbigniew Bodek 	}
30449b8d05b8SZbigniew Bodek 
30459b8d05b8SZbigniew Bodek 	return;
30469b8d05b8SZbigniew Bodek 
30479b8d05b8SZbigniew Bodek err:
30489b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
30499b8d05b8SZbigniew Bodek }
30509b8d05b8SZbigniew Bodek 
30519b8d05b8SZbigniew Bodek static int
30529b8d05b8SZbigniew Bodek ena_device_init(struct ena_adapter *adapter, device_t pdev,
30539b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
30549b8d05b8SZbigniew Bodek {
30559b8d05b8SZbigniew Bodek 	struct ena_com_dev* ena_dev = adapter->ena_dev;
30569b8d05b8SZbigniew Bodek 	bool readless_supported;
30579b8d05b8SZbigniew Bodek 	uint32_t aenq_groups;
30589b8d05b8SZbigniew Bodek 	int dma_width;
30599b8d05b8SZbigniew Bodek 	int rc;
30609b8d05b8SZbigniew Bodek 
30619b8d05b8SZbigniew Bodek 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
30629b8d05b8SZbigniew Bodek 	if (rc) {
30639b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to init mmio read less\n");
30649b8d05b8SZbigniew Bodek 		return rc;
30659b8d05b8SZbigniew Bodek 	}
30669b8d05b8SZbigniew Bodek 
30679b8d05b8SZbigniew Bodek 	/*
30689b8d05b8SZbigniew Bodek 	 * The PCIe configuration space revision id indicate if mmio reg
30699b8d05b8SZbigniew Bodek 	 * read is disabled
30709b8d05b8SZbigniew Bodek 	 */
30719b8d05b8SZbigniew Bodek 	readless_supported = !(pci_get_revid(pdev) & ENA_MMIO_DISABLE_REG_READ);
30729b8d05b8SZbigniew Bodek 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
30739b8d05b8SZbigniew Bodek 
3074a195fab0SMarcin Wojtas 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
30759b8d05b8SZbigniew Bodek 	if (rc) {
30769b8d05b8SZbigniew Bodek 		device_printf(pdev, "Can not reset device\n");
30779b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
30789b8d05b8SZbigniew Bodek 	}
30799b8d05b8SZbigniew Bodek 
30809b8d05b8SZbigniew Bodek 	rc = ena_com_validate_version(ena_dev);
30819b8d05b8SZbigniew Bodek 	if (rc) {
30829b8d05b8SZbigniew Bodek 		device_printf(pdev, "device version is too low\n");
30839b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
30849b8d05b8SZbigniew Bodek 	}
30859b8d05b8SZbigniew Bodek 
30869b8d05b8SZbigniew Bodek 	dma_width = ena_com_get_dma_width(ena_dev);
30879b8d05b8SZbigniew Bodek 	if (dma_width < 0) {
30889b8d05b8SZbigniew Bodek 		device_printf(pdev, "Invalid dma width value %d", dma_width);
30899b8d05b8SZbigniew Bodek 		rc = dma_width;
30909b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
30919b8d05b8SZbigniew Bodek 	}
30929b8d05b8SZbigniew Bodek 	adapter->dma_width = dma_width;
30939b8d05b8SZbigniew Bodek 
30949b8d05b8SZbigniew Bodek 	/* ENA admin level init */
30959b8d05b8SZbigniew Bodek 	rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
30969b8d05b8SZbigniew Bodek 	if (rc) {
30979b8d05b8SZbigniew Bodek 		device_printf(pdev,
30989b8d05b8SZbigniew Bodek 		    "Can not initialize ena admin queue with device\n");
30999b8d05b8SZbigniew Bodek 		goto err_mmio_read_less;
31009b8d05b8SZbigniew Bodek 	}
31019b8d05b8SZbigniew Bodek 
31029b8d05b8SZbigniew Bodek 	/*
31039b8d05b8SZbigniew Bodek 	 * To enable the msix interrupts the driver needs to know the number
31049b8d05b8SZbigniew Bodek 	 * of queues. So the driver uses polling mode to retrieve this
31059b8d05b8SZbigniew Bodek 	 * information
31069b8d05b8SZbigniew Bodek 	 */
31079b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, true);
31089b8d05b8SZbigniew Bodek 
31099b8d05b8SZbigniew Bodek 	ena_config_host_info(ena_dev);
31109b8d05b8SZbigniew Bodek 
31119b8d05b8SZbigniew Bodek 	/* Get Device Attributes */
31129b8d05b8SZbigniew Bodek 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
31139b8d05b8SZbigniew Bodek 	if (rc) {
31149b8d05b8SZbigniew Bodek 		device_printf(pdev,
31159b8d05b8SZbigniew Bodek 		    "Cannot get attribute for ena device rc: %d\n", rc);
31169b8d05b8SZbigniew Bodek 		goto err_admin_init;
31179b8d05b8SZbigniew Bodek 	}
31189b8d05b8SZbigniew Bodek 
31199b8d05b8SZbigniew Bodek 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) |
31209b8d05b8SZbigniew Bodek 	    BIT(ENA_ADMIN_FATAL_ERROR) |
31219b8d05b8SZbigniew Bodek 	    BIT(ENA_ADMIN_WARNING) |
31229b8d05b8SZbigniew Bodek 	    BIT(ENA_ADMIN_NOTIFICATION) |
31239b8d05b8SZbigniew Bodek 	    BIT(ENA_ADMIN_KEEP_ALIVE);
31249b8d05b8SZbigniew Bodek 
31259b8d05b8SZbigniew Bodek 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
31269b8d05b8SZbigniew Bodek 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
31279b8d05b8SZbigniew Bodek 	if (rc) {
31289b8d05b8SZbigniew Bodek 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
31299b8d05b8SZbigniew Bodek 		goto err_admin_init;
31309b8d05b8SZbigniew Bodek 	}
31319b8d05b8SZbigniew Bodek 
31329b8d05b8SZbigniew Bodek 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
31339b8d05b8SZbigniew Bodek 
31349b8d05b8SZbigniew Bodek 	return 0;
31359b8d05b8SZbigniew Bodek 
31369b8d05b8SZbigniew Bodek err_admin_init:
31379b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
31389b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
31399b8d05b8SZbigniew Bodek err_mmio_read_less:
31409b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
31419b8d05b8SZbigniew Bodek 
31429b8d05b8SZbigniew Bodek 	return rc;
31439b8d05b8SZbigniew Bodek }
31449b8d05b8SZbigniew Bodek 
31459b8d05b8SZbigniew Bodek static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
31469b8d05b8SZbigniew Bodek     int io_vectors)
31479b8d05b8SZbigniew Bodek {
31489b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
31499b8d05b8SZbigniew Bodek 	int rc;
31509b8d05b8SZbigniew Bodek 
31519b8d05b8SZbigniew Bodek 	rc = ena_enable_msix(adapter);
31529b8d05b8SZbigniew Bodek 	if (rc) {
31539b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
31549b8d05b8SZbigniew Bodek 		return rc;
31559b8d05b8SZbigniew Bodek 	}
31569b8d05b8SZbigniew Bodek 
31579b8d05b8SZbigniew Bodek 	ena_setup_mgmnt_intr(adapter);
31589b8d05b8SZbigniew Bodek 
31599b8d05b8SZbigniew Bodek 	rc = ena_request_mgmnt_irq(adapter);
31609b8d05b8SZbigniew Bodek 	if (rc) {
31619b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
31629b8d05b8SZbigniew Bodek 		goto err_disable_msix;
31639b8d05b8SZbigniew Bodek 	}
31649b8d05b8SZbigniew Bodek 
31659b8d05b8SZbigniew Bodek 	ena_com_set_admin_polling_mode(ena_dev, false);
31669b8d05b8SZbigniew Bodek 
31679b8d05b8SZbigniew Bodek 	ena_com_admin_aenq_enable(ena_dev);
31689b8d05b8SZbigniew Bodek 
31699b8d05b8SZbigniew Bodek 	return 0;
31709b8d05b8SZbigniew Bodek 
31719b8d05b8SZbigniew Bodek err_disable_msix:
31729b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
31739b8d05b8SZbigniew Bodek 
31749b8d05b8SZbigniew Bodek 	return rc;
31759b8d05b8SZbigniew Bodek }
31769b8d05b8SZbigniew Bodek 
31779b8d05b8SZbigniew Bodek /* Function called on ENA_ADMIN_KEEP_ALIVE event */
31789b8d05b8SZbigniew Bodek static void ena_keep_alive_wd(void *adapter_data,
31799b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
31809b8d05b8SZbigniew Bodek {
31819b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
318230217e2dSMarcin Wojtas 	struct ena_admin_aenq_keep_alive_desc *desc;
31839b8d05b8SZbigniew Bodek 	sbintime_t stime;
318430217e2dSMarcin Wojtas 	uint64_t rx_drops;
318530217e2dSMarcin Wojtas 
318630217e2dSMarcin Wojtas 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
318730217e2dSMarcin Wojtas 
318830217e2dSMarcin Wojtas 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
318930217e2dSMarcin Wojtas 	counter_u64_zero(adapter->hw_stats.rx_drops);
319030217e2dSMarcin Wojtas 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
31919b8d05b8SZbigniew Bodek 
31929b8d05b8SZbigniew Bodek 	stime = getsbinuptime();
31939b8d05b8SZbigniew Bodek 	atomic_store_rel_64(&adapter->keep_alive_timestamp, stime);
31949b8d05b8SZbigniew Bodek }
31959b8d05b8SZbigniew Bodek 
31969b8d05b8SZbigniew Bodek /* Check for keep alive expiration */
31979b8d05b8SZbigniew Bodek static void check_for_missing_keep_alive(struct ena_adapter *adapter)
31989b8d05b8SZbigniew Bodek {
31999b8d05b8SZbigniew Bodek 	sbintime_t timestamp, time;
32009b8d05b8SZbigniew Bodek 
32019b8d05b8SZbigniew Bodek 	if (adapter->wd_active == 0)
32029b8d05b8SZbigniew Bodek 		return;
32039b8d05b8SZbigniew Bodek 
32049b8d05b8SZbigniew Bodek 	if (adapter->keep_alive_timeout == 0)
32059b8d05b8SZbigniew Bodek 		return;
32069b8d05b8SZbigniew Bodek 
32079b8d05b8SZbigniew Bodek 	timestamp = atomic_load_acq_64(&adapter->keep_alive_timestamp);
32089b8d05b8SZbigniew Bodek 	time = getsbinuptime() - timestamp;
32099b8d05b8SZbigniew Bodek 	if (unlikely(time > adapter->keep_alive_timeout)) {
32109b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
32119b8d05b8SZbigniew Bodek 		    "Keep alive watchdog timeout.\n");
32129b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
3213a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
32149b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
32159b8d05b8SZbigniew Bodek 	}
32169b8d05b8SZbigniew Bodek }
32179b8d05b8SZbigniew Bodek 
32189b8d05b8SZbigniew Bodek /* Check if admin queue is enabled */
32199b8d05b8SZbigniew Bodek static void check_for_admin_com_state(struct ena_adapter *adapter)
32209b8d05b8SZbigniew Bodek {
32219b8d05b8SZbigniew Bodek 	if (unlikely(!ena_com_get_admin_running_state(adapter->ena_dev))) {
32229b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
32239b8d05b8SZbigniew Bodek 		    "ENA admin queue is not in running state!\n");
32249b8d05b8SZbigniew Bodek 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
3225a195fab0SMarcin Wojtas 		adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
32269b8d05b8SZbigniew Bodek 		adapter->trigger_reset = true;
32279b8d05b8SZbigniew Bodek 	}
32289b8d05b8SZbigniew Bodek }
32299b8d05b8SZbigniew Bodek 
32309b8d05b8SZbigniew Bodek /*
32319b8d05b8SZbigniew Bodek  * Check for TX which were not completed on time.
32329b8d05b8SZbigniew Bodek  * Timeout is defined by "missing_tx_timeout".
32339b8d05b8SZbigniew Bodek  * Reset will be performed if number of incompleted
32349b8d05b8SZbigniew Bodek  * transactions exceeds "missing_tx_threshold".
32359b8d05b8SZbigniew Bodek  */
32369b8d05b8SZbigniew Bodek static void check_for_missing_tx_completions(struct ena_adapter *adapter)
32379b8d05b8SZbigniew Bodek {
32389b8d05b8SZbigniew Bodek 	struct ena_ring *tx_ring;
32399b8d05b8SZbigniew Bodek 	struct ena_tx_buffer *tx_info;
32409b8d05b8SZbigniew Bodek 	struct bintime curtime, time;
32419b8d05b8SZbigniew Bodek 	int i, j, budget, missed_tx;
32429b8d05b8SZbigniew Bodek 
32439b8d05b8SZbigniew Bodek 	/* Make sure the driver doesn't turn the device in other process */
32449b8d05b8SZbigniew Bodek 	rmb();
32459b8d05b8SZbigniew Bodek 
32469b8d05b8SZbigniew Bodek 	if (!adapter->up)
32479b8d05b8SZbigniew Bodek 		return;
32489b8d05b8SZbigniew Bodek 
32499b8d05b8SZbigniew Bodek 	if (adapter->trigger_reset)
32509b8d05b8SZbigniew Bodek 		return;
32519b8d05b8SZbigniew Bodek 
32529b8d05b8SZbigniew Bodek 	if (adapter->missing_tx_timeout == 0)
32539b8d05b8SZbigniew Bodek 		return;
32549b8d05b8SZbigniew Bodek 
32559b8d05b8SZbigniew Bodek 	budget = adapter->missing_tx_max_queues;
32569b8d05b8SZbigniew Bodek 	getbinuptime(&curtime);
32579b8d05b8SZbigniew Bodek 
32589b8d05b8SZbigniew Bodek 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
32599b8d05b8SZbigniew Bodek 		tx_ring = &adapter->tx_ring[i];
32609b8d05b8SZbigniew Bodek 
32619b8d05b8SZbigniew Bodek 		missed_tx = 0;
32629b8d05b8SZbigniew Bodek 
32639b8d05b8SZbigniew Bodek 		for (j = 0; j < tx_ring->ring_size; j++) {
32649b8d05b8SZbigniew Bodek 			tx_info = &tx_ring->tx_buffer_info[j];
32659b8d05b8SZbigniew Bodek 
32669b8d05b8SZbigniew Bodek 			if (!bintime_isset(&tx_info->timestamp))
32679b8d05b8SZbigniew Bodek 				continue;
32689b8d05b8SZbigniew Bodek 
32699b8d05b8SZbigniew Bodek 			time = curtime;
32709b8d05b8SZbigniew Bodek 			bintime_sub(&time, &tx_info->timestamp);
32719b8d05b8SZbigniew Bodek 
32729b8d05b8SZbigniew Bodek 			/* Check again if packet is still waiting */
32739b8d05b8SZbigniew Bodek 			if (bintime_isset(&tx_info->timestamp) && unlikely(
32749b8d05b8SZbigniew Bodek 			    bttosbt(time) > adapter->missing_tx_timeout)) {
32759b8d05b8SZbigniew Bodek 				if (tx_info->print_once)
32769b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
32779b8d05b8SZbigniew Bodek 					    "Found a Tx that wasn't completed "
32789b8d05b8SZbigniew Bodek 					    "on time, qid %d, index %d.\n",
32799b8d05b8SZbigniew Bodek 					    tx_ring->qid, j);
32809b8d05b8SZbigniew Bodek 
32819b8d05b8SZbigniew Bodek 				tx_info->print_once = false;
32829b8d05b8SZbigniew Bodek 				missed_tx++;
32839b8d05b8SZbigniew Bodek 
32849b8d05b8SZbigniew Bodek 				if (unlikely(missed_tx >
32859b8d05b8SZbigniew Bodek 				    adapter->missing_tx_threshold)) {
32869b8d05b8SZbigniew Bodek 					device_printf(adapter->pdev,
32879b8d05b8SZbigniew Bodek 					    "The number of lost tx completion "
32889b8d05b8SZbigniew Bodek 					    "is above the threshold (%d > %d). "
32899b8d05b8SZbigniew Bodek 					    "Reset the device\n", missed_tx,
32909b8d05b8SZbigniew Bodek 					    adapter->missing_tx_threshold);
3291a195fab0SMarcin Wojtas 					adapter->reset_reason =
3292a195fab0SMarcin Wojtas 					    ENA_REGS_RESET_MISS_TX_CMPL;
32939b8d05b8SZbigniew Bodek 					adapter->trigger_reset = true;
32949b8d05b8SZbigniew Bodek 					return;
32959b8d05b8SZbigniew Bodek 				}
32969b8d05b8SZbigniew Bodek 			}
32979b8d05b8SZbigniew Bodek 		}
32989b8d05b8SZbigniew Bodek 
32999b8d05b8SZbigniew Bodek 		budget--;
3300cd5d5804SMarcin Wojtas 		if (budget == 0) {
33019b8d05b8SZbigniew Bodek 			i++;
33029b8d05b8SZbigniew Bodek 			break;
33039b8d05b8SZbigniew Bodek 		}
33049b8d05b8SZbigniew Bodek 	}
33059b8d05b8SZbigniew Bodek 
33069b8d05b8SZbigniew Bodek 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
33079b8d05b8SZbigniew Bodek }
33089b8d05b8SZbigniew Bodek 
33099b8d05b8SZbigniew Bodek 
33109b8d05b8SZbigniew Bodek static void
33119b8d05b8SZbigniew Bodek ena_timer_service(void *data)
33129b8d05b8SZbigniew Bodek {
33139b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)data;
33149b8d05b8SZbigniew Bodek 	struct ena_admin_host_info *host_info =
33159b8d05b8SZbigniew Bodek 	    adapter->ena_dev->host_attr.host_info;
33169b8d05b8SZbigniew Bodek 
33179b8d05b8SZbigniew Bodek 	check_for_missing_keep_alive(adapter);
33189b8d05b8SZbigniew Bodek 
33199b8d05b8SZbigniew Bodek 	check_for_admin_com_state(adapter);
33209b8d05b8SZbigniew Bodek 
33219b8d05b8SZbigniew Bodek 	check_for_missing_tx_completions(adapter);
33229b8d05b8SZbigniew Bodek 
33239b8d05b8SZbigniew Bodek 	if (host_info)
33249b8d05b8SZbigniew Bodek 		ena_update_host_info(host_info, adapter->ifp);
33259b8d05b8SZbigniew Bodek 
33269b8d05b8SZbigniew Bodek 	if (unlikely(adapter->trigger_reset)) {
33279b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Trigger reset is on\n");
33289b8d05b8SZbigniew Bodek 		taskqueue_enqueue(adapter->reset_tq, &adapter->reset_task);
33299b8d05b8SZbigniew Bodek 		return;
33309b8d05b8SZbigniew Bodek 	}
33319b8d05b8SZbigniew Bodek 
33329b8d05b8SZbigniew Bodek 	/*
33339b8d05b8SZbigniew Bodek 	 * Schedule another timeout one second from now.
33349b8d05b8SZbigniew Bodek 	 */
33359b8d05b8SZbigniew Bodek 	callout_schedule_sbt(&adapter->timer_service, SBT_1S, SBT_1S, 0);
33369b8d05b8SZbigniew Bodek }
33379b8d05b8SZbigniew Bodek 
33389b8d05b8SZbigniew Bodek static void
33399b8d05b8SZbigniew Bodek ena_reset_task(void *arg, int pending)
33409b8d05b8SZbigniew Bodek {
33419b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
33429b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
33439b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
33449b8d05b8SZbigniew Bodek 	bool dev_up;
33459b8d05b8SZbigniew Bodek 	int rc;
33469b8d05b8SZbigniew Bodek 
33479b8d05b8SZbigniew Bodek 	if (unlikely(!adapter->trigger_reset)) {
33489b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33499b8d05b8SZbigniew Bodek 		    "device reset scheduled but trigger_reset is off\n");
33509b8d05b8SZbigniew Bodek 		return;
33519b8d05b8SZbigniew Bodek 	}
33529b8d05b8SZbigniew Bodek 
33539b8d05b8SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
33549b8d05b8SZbigniew Bodek 
33559b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
33569b8d05b8SZbigniew Bodek 
33579b8d05b8SZbigniew Bodek 	dev_up = adapter->up;
33589b8d05b8SZbigniew Bodek 
33599b8d05b8SZbigniew Bodek 	ena_com_set_admin_running_state(ena_dev, false);
33609b8d05b8SZbigniew Bodek 	ena_down(adapter);
3361a195fab0SMarcin Wojtas 	ena_free_mgmnt_irq(adapter);
33629b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
33639b8d05b8SZbigniew Bodek 	ena_com_abort_admin_commands(ena_dev);
33649b8d05b8SZbigniew Bodek 	ena_com_wait_for_abort_completion(ena_dev);
33659b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
33669b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
33679b8d05b8SZbigniew Bodek 
3368a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
33699b8d05b8SZbigniew Bodek 	adapter->trigger_reset = false;
33709b8d05b8SZbigniew Bodek 
33719b8d05b8SZbigniew Bodek 	/* Finished destroy part. Restart the device */
33729b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
33739b8d05b8SZbigniew Bodek 	    &adapter->wd_active);
33749b8d05b8SZbigniew Bodek 	if (rc) {
33759b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
33769b8d05b8SZbigniew Bodek 		    "ENA device init failed! (err: %d)\n", rc);
33779b8d05b8SZbigniew Bodek 		goto err_dev_free;
33789b8d05b8SZbigniew Bodek 	}
33799b8d05b8SZbigniew Bodek 
33809b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
33819b8d05b8SZbigniew Bodek 	    adapter->num_queues);
33829b8d05b8SZbigniew Bodek 	if (rc) {
33839b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "Enable MSI-X failed\n");
33849b8d05b8SZbigniew Bodek 		goto err_com_free;
33859b8d05b8SZbigniew Bodek 	}
33869b8d05b8SZbigniew Bodek 
33879b8d05b8SZbigniew Bodek 	/* If the interface was up before the reset bring it up */
33889b8d05b8SZbigniew Bodek 	if (dev_up) {
33899b8d05b8SZbigniew Bodek 		rc = ena_up(adapter);
33909b8d05b8SZbigniew Bodek 		if (rc) {
33919b8d05b8SZbigniew Bodek 			device_printf(adapter->pdev,
33929b8d05b8SZbigniew Bodek 			    "Failed to create I/O queues\n");
33939b8d05b8SZbigniew Bodek 			goto err_msix_free;
33949b8d05b8SZbigniew Bodek 		}
33959b8d05b8SZbigniew Bodek 	}
33969b8d05b8SZbigniew Bodek 
33979b8d05b8SZbigniew Bodek 	callout_reset_sbt(&adapter->timer_service, SBT_1S, SBT_1S,
33989b8d05b8SZbigniew Bodek 	    ena_timer_service, (void *)adapter, 0);
33999b8d05b8SZbigniew Bodek 
34009b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
34019b8d05b8SZbigniew Bodek 
34029b8d05b8SZbigniew Bodek 	return;
34039b8d05b8SZbigniew Bodek 
34049b8d05b8SZbigniew Bodek err_msix_free:
34059b8d05b8SZbigniew Bodek 	ena_free_mgmnt_irq(adapter);
34069b8d05b8SZbigniew Bodek 	ena_disable_msix(adapter);
34079b8d05b8SZbigniew Bodek err_com_free:
34089b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
34099b8d05b8SZbigniew Bodek err_dev_free:
34109b8d05b8SZbigniew Bodek 	device_printf(adapter->pdev, "ENA reset failed!\n");
34119b8d05b8SZbigniew Bodek 	adapter->running = false;
34129b8d05b8SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
34139b8d05b8SZbigniew Bodek }
34149b8d05b8SZbigniew Bodek 
34159b8d05b8SZbigniew Bodek /**
34169b8d05b8SZbigniew Bodek  * ena_attach - Device Initialization Routine
34179b8d05b8SZbigniew Bodek  * @pdev: device information struct
34189b8d05b8SZbigniew Bodek  *
34199b8d05b8SZbigniew Bodek  * Returns 0 on success, otherwise on failure.
34209b8d05b8SZbigniew Bodek  *
34219b8d05b8SZbigniew Bodek  * ena_attach initializes an adapter identified by a device structure.
34229b8d05b8SZbigniew Bodek  * The OS initialization, configuring of the adapter private structure,
34239b8d05b8SZbigniew Bodek  * and a hardware reset occur.
34249b8d05b8SZbigniew Bodek  **/
34259b8d05b8SZbigniew Bodek static int
34269b8d05b8SZbigniew Bodek ena_attach(device_t pdev)
34279b8d05b8SZbigniew Bodek {
34289b8d05b8SZbigniew Bodek 	struct ena_com_dev_get_features_ctx get_feat_ctx;
34299b8d05b8SZbigniew Bodek 	static int version_printed;
34309b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter;
34319b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = NULL;
34329b8d05b8SZbigniew Bodek 	uint16_t tx_sgl_size = 0;
34339b8d05b8SZbigniew Bodek 	uint16_t rx_sgl_size = 0;
34349b8d05b8SZbigniew Bodek 	int io_queue_num;
34359b8d05b8SZbigniew Bodek 	int queue_size;
34369b8d05b8SZbigniew Bodek 	int rc;
34379b8d05b8SZbigniew Bodek 	struct sysctl_ctx_list *ctx;
34389b8d05b8SZbigniew Bodek 	struct sysctl_oid_list *children;
34399b8d05b8SZbigniew Bodek 
34409b8d05b8SZbigniew Bodek 	adapter = device_get_softc(pdev);
34419b8d05b8SZbigniew Bodek 	adapter->pdev = pdev;
34429b8d05b8SZbigniew Bodek 	ctx = device_get_sysctl_ctx(pdev);
34439b8d05b8SZbigniew Bodek 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(pdev));
34449b8d05b8SZbigniew Bodek 
34459b8d05b8SZbigniew Bodek 	mtx_init(&adapter->global_mtx, "ENA global mtx", NULL, MTX_DEF);
34469b8d05b8SZbigniew Bodek 	sx_init(&adapter->ioctl_sx, "ENA ioctl sx");
34479b8d05b8SZbigniew Bodek 
34489b8d05b8SZbigniew Bodek 	/* Sysctl calls for Watchdog service */
34499b8d05b8SZbigniew Bodek 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "wd_active",
34509b8d05b8SZbigniew Bodek 	    CTLFLAG_RWTUN, &adapter->wd_active, 0,
34519b8d05b8SZbigniew Bodek 	    "Watchdog is active");
34529b8d05b8SZbigniew Bodek 
34539b8d05b8SZbigniew Bodek 	SYSCTL_ADD_QUAD(ctx, children, OID_AUTO, "keep_alive_timeout",
34549b8d05b8SZbigniew Bodek 	    CTLFLAG_RWTUN, &adapter->keep_alive_timeout,
34559b8d05b8SZbigniew Bodek 	    "Timeout for Keep Alive messages");
34569b8d05b8SZbigniew Bodek 
34579b8d05b8SZbigniew Bodek 	SYSCTL_ADD_QUAD(ctx, children, OID_AUTO, "missing_tx_timeout",
34589b8d05b8SZbigniew Bodek 	    CTLFLAG_RWTUN, &adapter->missing_tx_timeout,
34599b8d05b8SZbigniew Bodek 	    "Timeout for TX completion");
34609b8d05b8SZbigniew Bodek 
34619b8d05b8SZbigniew Bodek 	SYSCTL_ADD_U32(ctx, children, OID_AUTO, "missing_tx_max_queues",
34629b8d05b8SZbigniew Bodek 	    CTLFLAG_RWTUN, &adapter->missing_tx_max_queues, 0,
34639b8d05b8SZbigniew Bodek 	    "Number of TX queues to check per run");
34649b8d05b8SZbigniew Bodek 
34659b8d05b8SZbigniew Bodek 	SYSCTL_ADD_U32(ctx, children, OID_AUTO, "missing_tx_threshold",
34669b8d05b8SZbigniew Bodek 	    CTLFLAG_RWTUN, &adapter->missing_tx_threshold, 0,
34679b8d05b8SZbigniew Bodek 	    "Max number of timeouted packets");
34689b8d05b8SZbigniew Bodek 
34699b8d05b8SZbigniew Bodek 	/* Set up the timer service */
34709b8d05b8SZbigniew Bodek 	callout_init_mtx(&adapter->timer_service, &adapter->global_mtx, 0);
34719b8d05b8SZbigniew Bodek 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
34729b8d05b8SZbigniew Bodek 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
34739b8d05b8SZbigniew Bodek 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
34749b8d05b8SZbigniew Bodek 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
34759b8d05b8SZbigniew Bodek 
34769b8d05b8SZbigniew Bodek 	if (version_printed++ == 0)
34779b8d05b8SZbigniew Bodek 		device_printf(pdev, "%s\n", ena_version);
34789b8d05b8SZbigniew Bodek 
34799b8d05b8SZbigniew Bodek 	rc = ena_allocate_pci_resources(adapter);
34809b8d05b8SZbigniew Bodek 	if (rc) {
34819b8d05b8SZbigniew Bodek 		device_printf(pdev, "PCI resource allocation failed!\n");
34829b8d05b8SZbigniew Bodek 		ena_free_pci_resources(adapter);
3483cd5d5804SMarcin Wojtas 		return (rc);
34849b8d05b8SZbigniew Bodek 	}
34859b8d05b8SZbigniew Bodek 
34869b8d05b8SZbigniew Bodek 	/* Allocate memory for ena_dev structure */
3487cd5d5804SMarcin Wojtas 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
3488cd5d5804SMarcin Wojtas 	    M_WAITOK | M_ZERO);
34899b8d05b8SZbigniew Bodek 
34909b8d05b8SZbigniew Bodek 	adapter->ena_dev = ena_dev;
34919b8d05b8SZbigniew Bodek 	ena_dev->dmadev = pdev;
34929b8d05b8SZbigniew Bodek 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
34939b8d05b8SZbigniew Bodek 	    M_WAITOK | M_ZERO);
34949b8d05b8SZbigniew Bodek 
34959b8d05b8SZbigniew Bodek 	/* Store register resources */
34969b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t =
34979b8d05b8SZbigniew Bodek 	    rman_get_bustag(adapter->registers);
34989b8d05b8SZbigniew Bodek 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h =
34999b8d05b8SZbigniew Bodek 	    rman_get_bushandle(adapter->registers);
35009b8d05b8SZbigniew Bodek 
35019b8d05b8SZbigniew Bodek 	if (((struct ena_bus*)(ena_dev->bus))->reg_bar_h == 0) {
35029b8d05b8SZbigniew Bodek 		device_printf(pdev, "failed to pmap registers bar\n");
35039b8d05b8SZbigniew Bodek 		rc = ENXIO;
3504cd5d5804SMarcin Wojtas 		goto err_bus_free;
35059b8d05b8SZbigniew Bodek 	}
35069b8d05b8SZbigniew Bodek 
35079b8d05b8SZbigniew Bodek 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
35089b8d05b8SZbigniew Bodek 
35099b8d05b8SZbigniew Bodek 	/* Device initialization */
35109b8d05b8SZbigniew Bodek 	rc = ena_device_init(adapter, pdev, &get_feat_ctx, &adapter->wd_active);
35119b8d05b8SZbigniew Bodek 	if (rc) {
35129b8d05b8SZbigniew Bodek 		device_printf(pdev, "ENA device init failed! (err: %d)\n", rc);
35139b8d05b8SZbigniew Bodek 		rc = ENXIO;
35149b8d05b8SZbigniew Bodek 		goto err_bus_free;
35159b8d05b8SZbigniew Bodek 	}
35169b8d05b8SZbigniew Bodek 
35179b8d05b8SZbigniew Bodek 	adapter->keep_alive_timestamp = getsbinuptime();
35189b8d05b8SZbigniew Bodek 
35199b8d05b8SZbigniew Bodek 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
35209b8d05b8SZbigniew Bodek 
35219b8d05b8SZbigniew Bodek 	/* Set for sure that interface is not up */
35229b8d05b8SZbigniew Bodek 	adapter->up = false;
35239b8d05b8SZbigniew Bodek 
35249b8d05b8SZbigniew Bodek 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
35259b8d05b8SZbigniew Bodek 	    ETHER_ADDR_LEN);
35269b8d05b8SZbigniew Bodek 
35279b8d05b8SZbigniew Bodek 	adapter->small_copy_len =
35289b8d05b8SZbigniew Bodek 	    ENA_DEFAULT_SMALL_PACKET_LEN;
35299b8d05b8SZbigniew Bodek 
35309b8d05b8SZbigniew Bodek 	/* calculate IO queue number to create */
35319b8d05b8SZbigniew Bodek 	io_queue_num = ena_calc_io_queue_num(adapter, &get_feat_ctx);
35329b8d05b8SZbigniew Bodek 
35339b8d05b8SZbigniew Bodek 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
35349b8d05b8SZbigniew Bodek 	    io_queue_num);
35359b8d05b8SZbigniew Bodek 	adapter->num_queues = io_queue_num;
35369b8d05b8SZbigniew Bodek 
35379b8d05b8SZbigniew Bodek 	/* calculatre ring sizes */
35389b8d05b8SZbigniew Bodek 	queue_size = ena_calc_queue_size(adapter,&tx_sgl_size,
35399b8d05b8SZbigniew Bodek 	    &rx_sgl_size, &get_feat_ctx);
35409b8d05b8SZbigniew Bodek 	if ((queue_size <= 0) || (io_queue_num <= 0)) {
35419b8d05b8SZbigniew Bodek 		rc = ENA_COM_FAULT;
35429b8d05b8SZbigniew Bodek 		goto err_com_free;
35439b8d05b8SZbigniew Bodek 	}
35449b8d05b8SZbigniew Bodek 
3545a195fab0SMarcin Wojtas 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
3546a195fab0SMarcin Wojtas 
35479b8d05b8SZbigniew Bodek 	adapter->tx_ring_size = queue_size;
35489b8d05b8SZbigniew Bodek 	adapter->rx_ring_size = queue_size;
35499b8d05b8SZbigniew Bodek 
35509b8d05b8SZbigniew Bodek 	adapter->max_tx_sgl_size = tx_sgl_size;
35519b8d05b8SZbigniew Bodek 	adapter->max_rx_sgl_size = rx_sgl_size;
35529b8d05b8SZbigniew Bodek 
35539b8d05b8SZbigniew Bodek 	/* set up dma tags for rx and tx buffers */
35549b8d05b8SZbigniew Bodek 	rc = ena_setup_tx_dma_tag(adapter);
35559b8d05b8SZbigniew Bodek 	if (rc)
3556cd5d5804SMarcin Wojtas 		goto err_com_free;
35579b8d05b8SZbigniew Bodek 
35589b8d05b8SZbigniew Bodek 	rc = ena_setup_rx_dma_tag(adapter);
35599b8d05b8SZbigniew Bodek 	if (rc)
3560cd5d5804SMarcin Wojtas 		goto err_tx_tag_free;
35619b8d05b8SZbigniew Bodek 
35629b8d05b8SZbigniew Bodek 	/* initialize rings basic information */
35639b8d05b8SZbigniew Bodek 	device_printf(pdev, "initalize %d io queues\n", io_queue_num);
3564cd5d5804SMarcin Wojtas 	ena_init_io_rings(adapter);
35659b8d05b8SZbigniew Bodek 
35669b8d05b8SZbigniew Bodek 	/* setup network interface */
35679b8d05b8SZbigniew Bodek 	rc = ena_setup_ifnet(pdev, adapter, &get_feat_ctx);
35689b8d05b8SZbigniew Bodek 	if (rc) {
35699b8d05b8SZbigniew Bodek 		device_printf(pdev,"Error with network interface setup\n");
3570cd5d5804SMarcin Wojtas 		goto err_io_free;
35719b8d05b8SZbigniew Bodek 	}
35729b8d05b8SZbigniew Bodek 
35739b8d05b8SZbigniew Bodek 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
35749b8d05b8SZbigniew Bodek 	if (rc) {
35759b8d05b8SZbigniew Bodek 		device_printf(pdev,
35769b8d05b8SZbigniew Bodek 		    "Failed to enable and set the admin interrupts\n");
35779b8d05b8SZbigniew Bodek 		goto err_ifp_free;
35789b8d05b8SZbigniew Bodek 	}
35799b8d05b8SZbigniew Bodek 
3580081169f2SZbigniew Bodek 	/* Initialize reset task queue */
3581081169f2SZbigniew Bodek 	TASK_INIT(&adapter->reset_task, 0, ena_reset_task, adapter);
3582081169f2SZbigniew Bodek 	adapter->reset_tq = taskqueue_create("ena_reset_enqueue",
3583081169f2SZbigniew Bodek 	    M_WAITOK | M_ZERO, taskqueue_thread_enqueue, &adapter->reset_tq);
3584081169f2SZbigniew Bodek 	taskqueue_start_threads(&adapter->reset_tq, 1, PI_NET,
3585081169f2SZbigniew Bodek 	    "%s rstq", device_get_nameunit(adapter->pdev));
3586081169f2SZbigniew Bodek 
35879b8d05b8SZbigniew Bodek 	/* Initialize statistics */
35889b8d05b8SZbigniew Bodek 	ena_alloc_counters((counter_u64_t *)&adapter->dev_stats,
35899b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
359030217e2dSMarcin Wojtas 	ena_alloc_counters((counter_u64_t *)&adapter->hw_stats,
359130217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
35929b8d05b8SZbigniew Bodek 	ena_sysctl_add_nodes(adapter);
35939b8d05b8SZbigniew Bodek 
35949b8d05b8SZbigniew Bodek 	/* Tell the stack that the interface is not active */
35959b8d05b8SZbigniew Bodek 	if_setdrvflagbits(adapter->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
35969b8d05b8SZbigniew Bodek 
35979b8d05b8SZbigniew Bodek 	adapter->running = true;
35989b8d05b8SZbigniew Bodek 	return (0);
35999b8d05b8SZbigniew Bodek 
36009b8d05b8SZbigniew Bodek err_ifp_free:
36019b8d05b8SZbigniew Bodek 	if_detach(adapter->ifp);
36029b8d05b8SZbigniew Bodek 	if_free(adapter->ifp);
3603cd5d5804SMarcin Wojtas err_io_free:
36049b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
36059b8d05b8SZbigniew Bodek 	ena_free_rx_dma_tag(adapter);
3606cd5d5804SMarcin Wojtas err_tx_tag_free:
36079b8d05b8SZbigniew Bodek 	ena_free_tx_dma_tag(adapter);
3608cd5d5804SMarcin Wojtas err_com_free:
36099b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36109b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
3611cd5d5804SMarcin Wojtas 	ena_com_mmio_reg_read_request_destroy(ena_dev);
36129b8d05b8SZbigniew Bodek err_bus_free:
36139b8d05b8SZbigniew Bodek 	free(ena_dev->bus, M_DEVBUF);
36149b8d05b8SZbigniew Bodek 	free(ena_dev, M_DEVBUF);
36159b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
3616cd5d5804SMarcin Wojtas 
36179b8d05b8SZbigniew Bodek 	return (rc);
36189b8d05b8SZbigniew Bodek }
36199b8d05b8SZbigniew Bodek 
36209b8d05b8SZbigniew Bodek /**
36219b8d05b8SZbigniew Bodek  * ena_detach - Device Removal Routine
36229b8d05b8SZbigniew Bodek  * @pdev: device information struct
36239b8d05b8SZbigniew Bodek  *
36249b8d05b8SZbigniew Bodek  * ena_detach is called by the device subsystem to alert the driver
36259b8d05b8SZbigniew Bodek  * that it should release a PCI device.
36269b8d05b8SZbigniew Bodek  **/
36279b8d05b8SZbigniew Bodek static int
36289b8d05b8SZbigniew Bodek ena_detach(device_t pdev)
36299b8d05b8SZbigniew Bodek {
36309b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = device_get_softc(pdev);
36319b8d05b8SZbigniew Bodek 	struct ena_com_dev *ena_dev = adapter->ena_dev;
36329b8d05b8SZbigniew Bodek 	int rc;
36339b8d05b8SZbigniew Bodek 
36349b8d05b8SZbigniew Bodek 	/* Make sure VLANS are not using driver */
36359b8d05b8SZbigniew Bodek 	if (adapter->ifp->if_vlantrunk != NULL) {
36369b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
36379b8d05b8SZbigniew Bodek 		return (EBUSY);
36389b8d05b8SZbigniew Bodek 	}
36399b8d05b8SZbigniew Bodek 
36409b8d05b8SZbigniew Bodek 	/* Free reset task and callout */
36419b8d05b8SZbigniew Bodek 	callout_drain(&adapter->timer_service);
36429b8d05b8SZbigniew Bodek 	while (taskqueue_cancel(adapter->reset_tq, &adapter->reset_task, NULL))
36439b8d05b8SZbigniew Bodek 		taskqueue_drain(adapter->reset_tq, &adapter->reset_task);
36449b8d05b8SZbigniew Bodek 	taskqueue_free(adapter->reset_tq);
36459b8d05b8SZbigniew Bodek 
3646e67c6554SZbigniew Bodek 	sx_xlock(&adapter->ioctl_sx);
36479b8d05b8SZbigniew Bodek 	ena_down(adapter);
3648e67c6554SZbigniew Bodek 	sx_unlock(&adapter->ioctl_sx);
36499b8d05b8SZbigniew Bodek 
36509b8d05b8SZbigniew Bodek 	if (adapter->ifp != NULL) {
36519b8d05b8SZbigniew Bodek 		ether_ifdetach(adapter->ifp);
36529b8d05b8SZbigniew Bodek 		if_free(adapter->ifp);
36539b8d05b8SZbigniew Bodek 	}
36549b8d05b8SZbigniew Bodek 
36559b8d05b8SZbigniew Bodek 	ena_free_all_io_rings_resources(adapter);
36569b8d05b8SZbigniew Bodek 
365730217e2dSMarcin Wojtas 	ena_free_counters((counter_u64_t *)&adapter->hw_stats,
365830217e2dSMarcin Wojtas 	    sizeof(struct ena_hw_stats));
36599b8d05b8SZbigniew Bodek 	ena_free_counters((counter_u64_t *)&adapter->dev_stats,
36609b8d05b8SZbigniew Bodek 	    sizeof(struct ena_stats_dev));
36619b8d05b8SZbigniew Bodek 
36629b8d05b8SZbigniew Bodek 	if (adapter->rss_support)
36639b8d05b8SZbigniew Bodek 		ena_com_rss_destroy(ena_dev);
36649b8d05b8SZbigniew Bodek 
36659b8d05b8SZbigniew Bodek 	rc = ena_free_rx_dma_tag(adapter);
36669b8d05b8SZbigniew Bodek 	if (rc)
36679b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36689b8d05b8SZbigniew Bodek 		    "Unmapped RX DMA tag associations\n");
36699b8d05b8SZbigniew Bodek 
36709b8d05b8SZbigniew Bodek 	rc = ena_free_tx_dma_tag(adapter);
36719b8d05b8SZbigniew Bodek 	if (rc)
36729b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev,
36739b8d05b8SZbigniew Bodek 		    "Unmapped TX DMA tag associations\n");
36749b8d05b8SZbigniew Bodek 
36759b8d05b8SZbigniew Bodek 	/* Reset the device only if the device is running. */
36769b8d05b8SZbigniew Bodek 	if (adapter->running)
3677a195fab0SMarcin Wojtas 		ena_com_dev_reset(ena_dev, adapter->reset_reason);
36789b8d05b8SZbigniew Bodek 
36799b8d05b8SZbigniew Bodek 	ena_com_delete_host_info(ena_dev);
36809b8d05b8SZbigniew Bodek 
36819b8d05b8SZbigniew Bodek 	ena_com_admin_destroy(ena_dev);
36829b8d05b8SZbigniew Bodek 
36839b8d05b8SZbigniew Bodek 	ena_free_irqs(adapter);
36849b8d05b8SZbigniew Bodek 
36859b8d05b8SZbigniew Bodek 	ena_com_mmio_reg_read_request_destroy(ena_dev);
36869b8d05b8SZbigniew Bodek 
36879b8d05b8SZbigniew Bodek 	ena_free_pci_resources(adapter);
36889b8d05b8SZbigniew Bodek 
36899b8d05b8SZbigniew Bodek 	mtx_destroy(&adapter->global_mtx);
36909b8d05b8SZbigniew Bodek 	sx_destroy(&adapter->ioctl_sx);
36919b8d05b8SZbigniew Bodek 
36929b8d05b8SZbigniew Bodek 	if (ena_dev->bus != NULL)
36939b8d05b8SZbigniew Bodek 		free(ena_dev->bus, M_DEVBUF);
36949b8d05b8SZbigniew Bodek 
36959b8d05b8SZbigniew Bodek 	if (ena_dev != NULL)
36969b8d05b8SZbigniew Bodek 		free(ena_dev, M_DEVBUF);
36979b8d05b8SZbigniew Bodek 
36989b8d05b8SZbigniew Bodek 	return (bus_generic_detach(pdev));
36999b8d05b8SZbigniew Bodek }
37009b8d05b8SZbigniew Bodek 
37019b8d05b8SZbigniew Bodek /******************************************************************************
37029b8d05b8SZbigniew Bodek  ******************************** AENQ Handlers *******************************
37039b8d05b8SZbigniew Bodek  *****************************************************************************/
37049b8d05b8SZbigniew Bodek /**
37059b8d05b8SZbigniew Bodek  * ena_update_on_link_change:
37069b8d05b8SZbigniew Bodek  * Notify the network interface about the change in link status
37079b8d05b8SZbigniew Bodek  **/
37089b8d05b8SZbigniew Bodek static void
37099b8d05b8SZbigniew Bodek ena_update_on_link_change(void *adapter_data,
37109b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
37119b8d05b8SZbigniew Bodek {
37129b8d05b8SZbigniew Bodek 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
37139b8d05b8SZbigniew Bodek 	struct ena_admin_aenq_link_change_desc *aenq_desc;
37149b8d05b8SZbigniew Bodek 	int status;
37159b8d05b8SZbigniew Bodek 	if_t ifp;
37169b8d05b8SZbigniew Bodek 
37179b8d05b8SZbigniew Bodek 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
37189b8d05b8SZbigniew Bodek 	ifp = adapter->ifp;
37199b8d05b8SZbigniew Bodek 	status = aenq_desc->flags &
37209b8d05b8SZbigniew Bodek 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
37219b8d05b8SZbigniew Bodek 
37229b8d05b8SZbigniew Bodek 	if (status != 0) {
37239b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is UP\n");
37249b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_UP);
37259b8d05b8SZbigniew Bodek 	} else if (status == 0) {
37269b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "link is DOWN\n");
37279b8d05b8SZbigniew Bodek 		if_link_state_change(ifp, LINK_STATE_DOWN);
37289b8d05b8SZbigniew Bodek 	} else {
37299b8d05b8SZbigniew Bodek 		device_printf(adapter->pdev, "invalid value recvd\n");
37309b8d05b8SZbigniew Bodek 		BUG();
37319b8d05b8SZbigniew Bodek 	}
37329b8d05b8SZbigniew Bodek 
37339b8d05b8SZbigniew Bodek 	adapter->link_status = status;
37349b8d05b8SZbigniew Bodek 
37359b8d05b8SZbigniew Bodek 	return;
37369b8d05b8SZbigniew Bodek }
37379b8d05b8SZbigniew Bodek 
37389b8d05b8SZbigniew Bodek /**
37399b8d05b8SZbigniew Bodek  * This handler will called for unknown event group or unimplemented handlers
37409b8d05b8SZbigniew Bodek  **/
37419b8d05b8SZbigniew Bodek static void
37429b8d05b8SZbigniew Bodek unimplemented_aenq_handler(void *data,
37439b8d05b8SZbigniew Bodek     struct ena_admin_aenq_entry *aenq_e)
37449b8d05b8SZbigniew Bodek {
37459b8d05b8SZbigniew Bodek 	return;
37469b8d05b8SZbigniew Bodek }
37479b8d05b8SZbigniew Bodek 
37489b8d05b8SZbigniew Bodek static struct ena_aenq_handlers aenq_handlers = {
37499b8d05b8SZbigniew Bodek     .handlers = {
37509b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
37519b8d05b8SZbigniew Bodek 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
37529b8d05b8SZbigniew Bodek     },
37539b8d05b8SZbigniew Bodek     .unimplemented_handler = unimplemented_aenq_handler
37549b8d05b8SZbigniew Bodek };
37559b8d05b8SZbigniew Bodek 
37569b8d05b8SZbigniew Bodek /*********************************************************************
37579b8d05b8SZbigniew Bodek  *  FreeBSD Device Interface Entry Points
37589b8d05b8SZbigniew Bodek  *********************************************************************/
37599b8d05b8SZbigniew Bodek 
37609b8d05b8SZbigniew Bodek static device_method_t ena_methods[] = {
37619b8d05b8SZbigniew Bodek     /* Device interface */
37629b8d05b8SZbigniew Bodek     DEVMETHOD(device_probe, ena_probe),
37639b8d05b8SZbigniew Bodek     DEVMETHOD(device_attach, ena_attach),
37649b8d05b8SZbigniew Bodek     DEVMETHOD(device_detach, ena_detach),
37659b8d05b8SZbigniew Bodek     DEVMETHOD_END
37669b8d05b8SZbigniew Bodek };
37679b8d05b8SZbigniew Bodek 
37689b8d05b8SZbigniew Bodek static driver_t ena_driver = {
37699b8d05b8SZbigniew Bodek     "ena", ena_methods, sizeof(struct ena_adapter),
37709b8d05b8SZbigniew Bodek };
37719b8d05b8SZbigniew Bodek 
37729b8d05b8SZbigniew Bodek devclass_t ena_devclass;
37739b8d05b8SZbigniew Bodek DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
37749b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, pci, 1, 1, 1);
37759b8d05b8SZbigniew Bodek MODULE_DEPEND(ena, ether, 1, 1, 1);
37769b8d05b8SZbigniew Bodek 
37779b8d05b8SZbigniew Bodek /*********************************************************************/
3778