Home | History | Annotate | Line # | Download | only in pci
if_ena.c revision 1.8
      1 /*-
      2  * BSD LICENSE
      3  *
      4  * Copyright (c) 2015-2017 Amazon.com, Inc. or its affiliates.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  *
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  *
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 #include <sys/cdefs.h>
     31 #if 0
     32 __FBSDID("$FreeBSD: head/sys/dev/ena/ena.c 333456 2018-05-10 09:37:54Z mw $");
     33 #endif
     34 __KERNEL_RCSID(0, "$NetBSD: if_ena.c,v 1.8 2018/11/28 19:07:49 jmcneill Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/bus.h>
     39 #include <sys/endian.h>
     40 #include <sys/kernel.h>
     41 #include <sys/kthread.h>
     42 #include <sys/malloc.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/module.h>
     45 #include <sys/socket.h>
     46 #include <sys/sockio.h>
     47 #include <sys/sysctl.h>
     48 #include <sys/time.h>
     49 #include <sys/workqueue.h>
     50 #include <sys/callout.h>
     51 #include <sys/interrupt.h>
     52 #include <sys/cpu.h>
     53 
     54 #include <sys/bus.h>
     55 
     56 #include <net/if_ether.h>
     57 #include <net/if_vlanvar.h>
     58 
     59 #include <dev/pci/if_enavar.h>
     60 
     61 /*********************************************************
     62  *  Function prototypes
     63  *********************************************************/
     64 static int	ena_probe(device_t, cfdata_t, void *);
     65 static int	ena_intr_msix_mgmnt(void *);
     66 static int	ena_allocate_pci_resources(struct pci_attach_args *,
     67 		    struct ena_adapter *);
     68 static void	ena_free_pci_resources(struct ena_adapter *);
     69 static int	ena_change_mtu(struct ifnet *, int);
     70 static void	ena_init_io_rings_common(struct ena_adapter *,
     71     struct ena_ring *, uint16_t);
     72 static void	ena_init_io_rings(struct ena_adapter *);
     73 static void	ena_free_io_ring_resources(struct ena_adapter *, unsigned int);
     74 static void	ena_free_all_io_rings_resources(struct ena_adapter *);
     75 #if 0
     76 static int	ena_setup_tx_dma_tag(struct ena_adapter *);
     77 static int	ena_free_tx_dma_tag(struct ena_adapter *);
     78 static int	ena_setup_rx_dma_tag(struct ena_adapter *);
     79 static int	ena_free_rx_dma_tag(struct ena_adapter *);
     80 #endif
     81 static int	ena_setup_tx_resources(struct ena_adapter *, int);
     82 static void	ena_free_tx_resources(struct ena_adapter *, int);
     83 static int	ena_setup_all_tx_resources(struct ena_adapter *);
     84 static void	ena_free_all_tx_resources(struct ena_adapter *);
     85 static inline int validate_rx_req_id(struct ena_ring *, uint16_t);
     86 static int	ena_setup_rx_resources(struct ena_adapter *, unsigned int);
     87 static void	ena_free_rx_resources(struct ena_adapter *, unsigned int);
     88 static int	ena_setup_all_rx_resources(struct ena_adapter *);
     89 static void	ena_free_all_rx_resources(struct ena_adapter *);
     90 static inline int ena_alloc_rx_mbuf(struct ena_adapter *, struct ena_ring *,
     91     struct ena_rx_buffer *);
     92 static void	ena_free_rx_mbuf(struct ena_adapter *, struct ena_ring *,
     93     struct ena_rx_buffer *);
     94 static int	ena_refill_rx_bufs(struct ena_ring *, uint32_t);
     95 static void	ena_free_rx_bufs(struct ena_adapter *, unsigned int);
     96 static void	ena_refill_all_rx_bufs(struct ena_adapter *);
     97 static void	ena_free_all_rx_bufs(struct ena_adapter *);
     98 static void	ena_free_tx_bufs(struct ena_adapter *, unsigned int);
     99 static void	ena_free_all_tx_bufs(struct ena_adapter *);
    100 static void	ena_destroy_all_tx_queues(struct ena_adapter *);
    101 static void	ena_destroy_all_rx_queues(struct ena_adapter *);
    102 static void	ena_destroy_all_io_queues(struct ena_adapter *);
    103 static int	ena_create_io_queues(struct ena_adapter *);
    104 static int	ena_tx_cleanup(struct ena_ring *);
    105 static void	ena_deferred_rx_cleanup(struct work *, void *);
    106 static int	ena_rx_cleanup(struct ena_ring *);
    107 static inline int validate_tx_req_id(struct ena_ring *, uint16_t);
    108 #if 0
    109 static void	ena_rx_hash_mbuf(struct ena_ring *, struct ena_com_rx_ctx *,
    110     struct mbuf *);
    111 #endif
    112 static struct mbuf* ena_rx_mbuf(struct ena_ring *, struct ena_com_rx_buf_info *,
    113     struct ena_com_rx_ctx *, uint16_t *);
    114 static inline void ena_rx_checksum(struct ena_ring *, struct ena_com_rx_ctx *,
    115     struct mbuf *);
    116 static int	ena_handle_msix(void *);
    117 static int	ena_enable_msix(struct ena_adapter *);
    118 static int	ena_request_mgmnt_irq(struct ena_adapter *);
    119 static int	ena_request_io_irq(struct ena_adapter *);
    120 static void	ena_free_mgmnt_irq(struct ena_adapter *);
    121 static void	ena_free_io_irq(struct ena_adapter *);
    122 static void	ena_free_irqs(struct ena_adapter*);
    123 static void	ena_disable_msix(struct ena_adapter *);
    124 static void	ena_unmask_all_io_irqs(struct ena_adapter *);
    125 static int	ena_rss_configure(struct ena_adapter *);
    126 static int	ena_up_complete(struct ena_adapter *);
    127 static int	ena_up(struct ena_adapter *);
    128 static void	ena_down(struct ena_adapter *);
    129 #if 0
    130 static uint64_t	ena_get_counter(struct ifnet *, ift_counter);
    131 #endif
    132 static int	ena_media_change(struct ifnet *);
    133 static void	ena_media_status(struct ifnet *, struct ifmediareq *);
    134 static int	ena_init(struct ifnet *);
    135 static int	ena_ioctl(struct ifnet *, u_long, void *);
    136 static int	ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *);
    137 static void	ena_update_host_info(struct ena_admin_host_info *, struct ifnet *);
    138 static void	ena_update_hwassist(struct ena_adapter *);
    139 static int	ena_setup_ifnet(device_t, struct ena_adapter *,
    140     struct ena_com_dev_get_features_ctx *);
    141 static void	ena_tx_csum(struct ena_com_tx_ctx *, struct mbuf *);
    142 static int	ena_check_and_collapse_mbuf(struct ena_ring *tx_ring,
    143     struct mbuf **mbuf);
    144 static int	ena_xmit_mbuf(struct ena_ring *, struct mbuf **);
    145 static void	ena_start_xmit(struct ena_ring *);
    146 static int	ena_mq_start(struct ifnet *, struct mbuf *);
    147 static void	ena_deferred_mq_start(struct work *, void *);
    148 #if 0
    149 static void	ena_qflush(struct ifnet *);
    150 #endif
    151 static int	ena_calc_io_queue_num(struct pci_attach_args *,
    152     struct ena_adapter *, struct ena_com_dev_get_features_ctx *);
    153 static int	ena_calc_queue_size(struct ena_adapter *, uint16_t *,
    154     uint16_t *, struct ena_com_dev_get_features_ctx *);
    155 #if 0
    156 static int	ena_rss_init_default(struct ena_adapter *);
    157 static void	ena_rss_init_default_deferred(void *);
    158 #endif
    159 static void	ena_config_host_info(struct ena_com_dev *);
    160 static void	ena_attach(device_t, device_t, void *);
    161 static int	ena_detach(device_t, int);
    162 static int	ena_device_init(struct ena_adapter *, device_t,
    163     struct ena_com_dev_get_features_ctx *, int *);
    164 static int	ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *,
    165     int);
    166 static void ena_update_on_link_change(void *, struct ena_admin_aenq_entry *);
    167 static void	unimplemented_aenq_handler(void *,
    168     struct ena_admin_aenq_entry *);
    169 static void	ena_timer_service(void *);
    170 
    171 static const char ena_version[] =
    172     DEVICE_NAME DRV_MODULE_NAME " v" DRV_MODULE_VERSION;
    173 
    174 #if 0
    175 static SYSCTL_NODE(_hw, OID_AUTO, ena, CTLFLAG_RD, 0, "ENA driver parameters");
    176 #endif
    177 
    178 /*
    179  * Tuneable number of buffers in the buf-ring (drbr)
    180  */
    181 static int ena_buf_ring_size = 4096;
    182 #if 0
    183 SYSCTL_INT(_hw_ena, OID_AUTO, buf_ring_size, CTLFLAG_RWTUN,
    184     &ena_buf_ring_size, 0, "Size of the bufring");
    185 #endif
    186 
    187 /*
    188  * Logging level for changing verbosity of the output
    189  */
    190 int ena_log_level = ENA_ALERT | ENA_WARNING;
    191 #if 0
    192 SYSCTL_INT(_hw_ena, OID_AUTO, log_level, CTLFLAG_RWTUN,
    193     &ena_log_level, 0, "Logging level indicating verbosity of the logs");
    194 #endif
    195 
    196 static const ena_vendor_info_t ena_vendor_info_array[] = {
    197     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_PF, 0},
    198     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_PF, 0},
    199     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_VF, 0},
    200     { PCI_VENDOR_ID_AMAZON, PCI_DEV_ID_ENA_LLQ_VF, 0},
    201     /* Last entry */
    202     { 0, 0, 0 }
    203 };
    204 
    205 /*
    206  * Contains pointers to event handlers, e.g. link state chage.
    207  */
    208 static struct ena_aenq_handlers aenq_handlers;
    209 
    210 int
    211 ena_dma_alloc(device_t dmadev, bus_size_t size,
    212     ena_mem_handle_t *dma , int mapflags)
    213 {
    214 	struct ena_adapter *adapter = device_private(dmadev);
    215 	uint32_t maxsize;
    216 	bus_dma_segment_t seg;
    217 	int error, nsegs;
    218 
    219 	maxsize = ((size - 1) / PAGE_SIZE + 1) * PAGE_SIZE;
    220 
    221 #if 0
    222 	/* XXX what is this needed for ? */
    223 	dma_space_addr = ENA_DMA_BIT_MASK(adapter->dma_width);
    224 	if (unlikely(dma_space_addr == 0))
    225 		dma_space_addr = BUS_SPACE_MAXADDR;
    226 #endif
    227 
    228 	dma->tag = adapter->sc_dmat;
    229 
    230         if ((error = bus_dmamap_create(dma->tag, maxsize, 1, maxsize, 0,
    231             BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &dma->map)) != 0) {
    232 		ena_trace(ENA_ALERT, "bus_dmamap_create(%ju) failed: %d\n",
    233 		    (uintmax_t)maxsize, error);
    234                 goto fail_create;
    235 	}
    236 
    237 	error = bus_dmamem_alloc(dma->tag, maxsize, 8, 0, &seg, 1, &nsegs,
    238 	    BUS_DMA_ALLOCNOW);
    239 	if (error) {
    240 		ena_trace(ENA_ALERT, "bus_dmamem_alloc(%ju) failed: %d\n",
    241 		    (uintmax_t)maxsize, error);
    242 		goto fail_alloc;
    243 	}
    244 
    245 	error = bus_dmamem_map(dma->tag, &seg, nsegs, maxsize,
    246 	    &dma->vaddr, BUS_DMA_COHERENT);
    247 	if (error) {
    248 		ena_trace(ENA_ALERT, "bus_dmamem_map(%ju) failed: %d\n",
    249 		    (uintmax_t)maxsize, error);
    250 		goto fail_map;
    251 	}
    252 	memset(dma->vaddr, 0, maxsize);
    253 
    254 	error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
    255 	    maxsize, NULL, mapflags);
    256 	if (error) {
    257 		ena_trace(ENA_ALERT, ": bus_dmamap_load failed: %d\n", error);
    258 		goto fail_load;
    259 	}
    260 	dma->paddr = dma->map->dm_segs[0].ds_addr;
    261 
    262 	return (0);
    263 
    264 fail_load:
    265 	bus_dmamem_unmap(dma->tag, dma->vaddr, maxsize);
    266 fail_map:
    267 	bus_dmamem_free(dma->tag, &seg, nsegs);
    268 fail_alloc:
    269 	bus_dmamap_destroy(adapter->sc_dmat, dma->map);
    270 fail_create:
    271 	return (error);
    272 }
    273 
    274 static int
    275 ena_allocate_pci_resources(struct pci_attach_args *pa,
    276     struct ena_adapter *adapter)
    277 {
    278 	bus_size_t size;
    279 
    280 	/*
    281 	 * Map control/status registers.
    282 	*/
    283 	pcireg_t memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ENA_REG_BAR);
    284 	if (pci_mapreg_map(pa, ENA_REG_BAR, memtype, 0, &adapter->sc_btag,
    285 	    &adapter->sc_bhandle, NULL, &size)) {
    286 		aprint_error(": can't map mem space\n");
    287 		return ENXIO;
    288 	}
    289 
    290 	return (0);
    291 }
    292 
    293 static void
    294 ena_free_pci_resources(struct ena_adapter *adapter)
    295 {
    296 	/* Nothing to do */
    297 }
    298 
    299 static int
    300 ena_probe(device_t parent, cfdata_t match, void *aux)
    301 {
    302 	struct pci_attach_args *pa = aux;
    303 	const ena_vendor_info_t *ent;
    304 
    305 	for (int i = 0; i < __arraycount(ena_vendor_info_array); i++) {
    306 		ent = &ena_vendor_info_array[i];
    307 
    308 		if ((PCI_VENDOR(pa->pa_id) == ent->vendor_id) &&
    309 		    (PCI_PRODUCT(pa->pa_id) == ent->device_id)) {
    310 			return 1;
    311 		}
    312 	}
    313 
    314 	return 0;
    315 }
    316 
    317 static int
    318 ena_change_mtu(struct ifnet *ifp, int new_mtu)
    319 {
    320 	struct ena_adapter *adapter = if_getsoftc(ifp);
    321 	int rc;
    322 
    323 	if ((new_mtu > adapter->max_mtu) || (new_mtu < ENA_MIN_MTU)) {
    324 		device_printf(adapter->pdev, "Invalid MTU setting. "
    325 		    "new_mtu: %d max mtu: %d min mtu: %d\n",
    326 		    new_mtu, adapter->max_mtu, ENA_MIN_MTU);
    327 		return (EINVAL);
    328 	}
    329 
    330 	rc = ena_com_set_dev_mtu(adapter->ena_dev, new_mtu);
    331 	if (likely(rc == 0)) {
    332 		ena_trace(ENA_DBG, "set MTU to %d\n", new_mtu);
    333 		if_setmtu(ifp, new_mtu);
    334 	} else {
    335 		device_printf(adapter->pdev, "Failed to set MTU to %d\n",
    336 		    new_mtu);
    337 	}
    338 
    339 	return (rc);
    340 }
    341 
    342 #define EVCNT_INIT(st, f) \
    343 	do {								\
    344 		evcnt_attach_dynamic(&st->f, EVCNT_TYPE_MISC, NULL,	\
    345 		    st->name, #f);					\
    346 	} while (0)
    347 
    348 static inline void
    349 ena_alloc_counters_rx(struct ena_stats_rx *st, int queue)
    350 {
    351 	snprintf(st->name, sizeof(st->name), "ena rxq%d", queue);
    352 
    353 	EVCNT_INIT(st, cnt);
    354 	EVCNT_INIT(st, bytes);
    355 	EVCNT_INIT(st, refil_partial);
    356 	EVCNT_INIT(st, bad_csum);
    357 	EVCNT_INIT(st, mjum_alloc_fail);
    358 	EVCNT_INIT(st, mbuf_alloc_fail);
    359 	EVCNT_INIT(st, dma_mapping_err);
    360 	EVCNT_INIT(st, bad_desc_num);
    361 	EVCNT_INIT(st, bad_req_id);
    362 	EVCNT_INIT(st, empty_rx_ring);
    363 
    364 	/* Make sure all code is updated when new fields added */
    365 	CTASSERT(offsetof(struct ena_stats_rx, empty_rx_ring)
    366 	    + sizeof(st->empty_rx_ring) == sizeof(*st));
    367 }
    368 
    369 static inline void
    370 ena_alloc_counters_tx(struct ena_stats_tx *st, int queue)
    371 {
    372 	snprintf(st->name, sizeof(st->name), "ena txq%d", queue);
    373 
    374 	EVCNT_INIT(st, cnt);
    375 	EVCNT_INIT(st, bytes);
    376 	EVCNT_INIT(st, prepare_ctx_err);
    377 	EVCNT_INIT(st, dma_mapping_err);
    378 	EVCNT_INIT(st, doorbells);
    379 	EVCNT_INIT(st, missing_tx_comp);
    380 	EVCNT_INIT(st, bad_req_id);
    381 	EVCNT_INIT(st, collapse);
    382 	EVCNT_INIT(st, collapse_err);
    383 
    384 	/* Make sure all code is updated when new fields added */
    385 	CTASSERT(offsetof(struct ena_stats_tx, collapse_err)
    386 	    + sizeof(st->collapse_err) == sizeof(*st));
    387 }
    388 
    389 static inline void
    390 ena_alloc_counters_dev(struct ena_stats_dev *st, int queue)
    391 {
    392 	snprintf(st->name, sizeof(st->name), "ena dev ioq%d", queue);
    393 
    394 	EVCNT_INIT(st, wd_expired);
    395 	EVCNT_INIT(st, interface_up);
    396 	EVCNT_INIT(st, interface_down);
    397 	EVCNT_INIT(st, admin_q_pause);
    398 
    399 	/* Make sure all code is updated when new fields added */
    400 	CTASSERT(offsetof(struct ena_stats_dev, admin_q_pause)
    401 	    + sizeof(st->admin_q_pause) == sizeof(*st));
    402 }
    403 
    404 static inline void
    405 ena_alloc_counters_hwstats(struct ena_hw_stats *st, int queue)
    406 {
    407 	snprintf(st->name, sizeof(st->name), "ena hw ioq%d", queue);
    408 
    409 	EVCNT_INIT(st, rx_packets);
    410 	EVCNT_INIT(st, tx_packets);
    411 	EVCNT_INIT(st, rx_bytes);
    412 	EVCNT_INIT(st, tx_bytes);
    413 	EVCNT_INIT(st, rx_drops);
    414 
    415 	/* Make sure all code is updated when new fields added */
    416 	CTASSERT(offsetof(struct ena_hw_stats, rx_drops)
    417 	    + sizeof(st->rx_drops) == sizeof(*st));
    418 }
    419 static inline void
    420 ena_free_counters(struct evcnt *begin, int size)
    421 {
    422 	struct evcnt *end = (struct evcnt *)((char *)begin + size);
    423 
    424 	for (; begin < end; ++begin)
    425 		counter_u64_free(*begin);
    426 }
    427 
    428 static inline void
    429 ena_reset_counters(struct evcnt *begin, int size)
    430 {
    431 	struct evcnt *end = (struct evcnt *)((char *)begin + size);
    432 
    433 	for (; begin < end; ++begin)
    434 		counter_u64_zero(*begin);
    435 }
    436 
    437 static void
    438 ena_init_io_rings_common(struct ena_adapter *adapter, struct ena_ring *ring,
    439     uint16_t qid)
    440 {
    441 
    442 	ring->qid = qid;
    443 	ring->adapter = adapter;
    444 	ring->ena_dev = adapter->ena_dev;
    445 }
    446 
    447 static void
    448 ena_init_io_rings(struct ena_adapter *adapter)
    449 {
    450 	struct ena_com_dev *ena_dev;
    451 	struct ena_ring *txr, *rxr;
    452 	struct ena_que *que;
    453 	int i;
    454 
    455 	ena_dev = adapter->ena_dev;
    456 
    457 	for (i = 0; i < adapter->num_queues; i++) {
    458 		txr = &adapter->tx_ring[i];
    459 		rxr = &adapter->rx_ring[i];
    460 
    461 		/* TX/RX common ring state */
    462 		ena_init_io_rings_common(adapter, txr, i);
    463 		ena_init_io_rings_common(adapter, rxr, i);
    464 
    465 		/* TX specific ring state */
    466 		txr->ring_size = adapter->tx_ring_size;
    467 		txr->tx_max_header_size = ena_dev->tx_max_header_size;
    468 		txr->tx_mem_queue_type = ena_dev->tx_mem_queue_type;
    469 		txr->smoothed_interval =
    470 		    ena_com_get_nonadaptive_moderation_interval_tx(ena_dev);
    471 
    472 		/* Allocate a buf ring */
    473 		txr->br = buf_ring_alloc(ena_buf_ring_size, M_DEVBUF,
    474 		    M_WAITOK, &txr->ring_mtx);
    475 
    476 		/* Alloc TX statistics. */
    477 		ena_alloc_counters_tx(&txr->tx_stats, i);
    478 
    479 		/* RX specific ring state */
    480 		rxr->ring_size = adapter->rx_ring_size;
    481 		rxr->smoothed_interval =
    482 		    ena_com_get_nonadaptive_moderation_interval_rx(ena_dev);
    483 
    484 		/* Alloc RX statistics. */
    485 		ena_alloc_counters_rx(&rxr->rx_stats, i);
    486 
    487 		/* Initialize locks */
    488 		snprintf(txr->mtx_name, sizeof(txr->mtx_name), "%s:tx(%d)",
    489 		    device_xname(adapter->pdev), i);
    490 		snprintf(rxr->mtx_name, sizeof(rxr->mtx_name), "%s:rx(%d)",
    491 		    device_xname(adapter->pdev), i);
    492 
    493 		mutex_init(&txr->ring_mtx, MUTEX_DEFAULT, IPL_NET);
    494 		mutex_init(&rxr->ring_mtx, MUTEX_DEFAULT, IPL_NET);
    495 
    496 		que = &adapter->que[i];
    497 		que->adapter = adapter;
    498 		que->id = i;
    499 		que->tx_ring = txr;
    500 		que->rx_ring = rxr;
    501 
    502 		txr->que = que;
    503 		rxr->que = que;
    504 
    505 		rxr->empty_rx_queue = 0;
    506 	}
    507 }
    508 
    509 static void
    510 ena_free_io_ring_resources(struct ena_adapter *adapter, unsigned int qid)
    511 {
    512 	struct ena_ring *txr = &adapter->tx_ring[qid];
    513 	struct ena_ring *rxr = &adapter->rx_ring[qid];
    514 
    515 	ena_free_counters((struct evcnt *)&txr->tx_stats,
    516 	    sizeof(txr->tx_stats));
    517 	ena_free_counters((struct evcnt *)&rxr->rx_stats,
    518 	    sizeof(rxr->rx_stats));
    519 
    520 	ENA_RING_MTX_LOCK(txr);
    521 	drbr_free(txr->br, M_DEVBUF);
    522 	ENA_RING_MTX_UNLOCK(txr);
    523 
    524 	mutex_destroy(&txr->ring_mtx);
    525 	mutex_destroy(&rxr->ring_mtx);
    526 }
    527 
    528 static void
    529 ena_free_all_io_rings_resources(struct ena_adapter *adapter)
    530 {
    531 	int i;
    532 
    533 	for (i = 0; i < adapter->num_queues; i++)
    534 		ena_free_io_ring_resources(adapter, i);
    535 
    536 }
    537 
    538 #if 0
    539 static int
    540 ena_setup_tx_dma_tag(struct ena_adapter *adapter)
    541 {
    542 	int ret;
    543 
    544 	/* Create DMA tag for Tx buffers */
    545 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev),
    546 	    1, 0,				  /* alignment, bounds 	     */
    547 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
    548 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
    549 	    NULL, NULL,				  /* filter, filterarg 	     */
    550 	    ENA_TSO_MAXSIZE,			  /* maxsize 		     */
    551 	    adapter->max_tx_sgl_size - 1,	  /* nsegments 		     */
    552 	    ENA_TSO_MAXSIZE,			  /* maxsegsize 	     */
    553 	    0,					  /* flags 		     */
    554 	    NULL,				  /* lockfunc 		     */
    555 	    NULL,				  /* lockfuncarg 	     */
    556 	    &adapter->tx_buf_tag);
    557 
    558 	return (ret);
    559 }
    560 #endif
    561 
    562 #if 0
    563 static int
    564 ena_setup_rx_dma_tag(struct ena_adapter *adapter)
    565 {
    566 	int ret;
    567 
    568 	/* Create DMA tag for Rx buffers*/
    569 	ret = bus_dma_tag_create(bus_get_dma_tag(adapter->pdev), /* parent   */
    570 	    1, 0,				  /* alignment, bounds 	     */
    571 	    ENA_DMA_BIT_MASK(adapter->dma_width), /* lowaddr of excl window  */
    572 	    BUS_SPACE_MAXADDR, 			  /* highaddr of excl window */
    573 	    NULL, NULL,				  /* filter, filterarg 	     */
    574 	    MJUM16BYTES,			  /* maxsize 		     */
    575 	    adapter->max_rx_sgl_size,		  /* nsegments 		     */
    576 	    MJUM16BYTES,			  /* maxsegsize 	     */
    577 	    0,					  /* flags 		     */
    578 	    NULL,				  /* lockfunc 		     */
    579 	    NULL,				  /* lockarg 		     */
    580 	    &adapter->rx_buf_tag);
    581 
    582 	return (ret);
    583 }
    584 #endif
    585 
    586 /**
    587  * ena_setup_tx_resources - allocate Tx resources (Descriptors)
    588  * @adapter: network interface device structure
    589  * @qid: queue index
    590  *
    591  * Returns 0 on success, otherwise on failure.
    592  **/
    593 static int
    594 ena_setup_tx_resources(struct ena_adapter *adapter, int qid)
    595 {
    596 	struct ena_que *que = &adapter->que[qid];
    597 	struct ena_ring *tx_ring = que->tx_ring;
    598 	int size, i, err;
    599 #ifdef	RSS
    600 	cpuset_t cpu_mask;
    601 #endif
    602 
    603 	size = sizeof(struct ena_tx_buffer) * tx_ring->ring_size;
    604 
    605 	tx_ring->tx_buffer_info = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
    606 	if (unlikely(tx_ring->tx_buffer_info == NULL))
    607 		return (ENOMEM);
    608 
    609 	size = sizeof(uint16_t) * tx_ring->ring_size;
    610 	tx_ring->free_tx_ids = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
    611 	if (unlikely(tx_ring->free_tx_ids == NULL))
    612 		goto err_buf_info_free;
    613 
    614 	/* Req id stack for TX OOO completions */
    615 	for (i = 0; i < tx_ring->ring_size; i++)
    616 		tx_ring->free_tx_ids[i] = i;
    617 
    618 	/* Reset TX statistics. */
    619 	ena_reset_counters((struct evcnt *)&tx_ring->tx_stats,
    620 	    sizeof(tx_ring->tx_stats));
    621 
    622 	tx_ring->next_to_use = 0;
    623 	tx_ring->next_to_clean = 0;
    624 
    625 	/* Make sure that drbr is empty */
    626 	ENA_RING_MTX_LOCK(tx_ring);
    627 	drbr_flush(adapter->ifp, tx_ring->br);
    628 	ENA_RING_MTX_UNLOCK(tx_ring);
    629 
    630 	/* ... and create the buffer DMA maps */
    631 	for (i = 0; i < tx_ring->ring_size; i++) {
    632 		err = bus_dmamap_create(adapter->sc_dmat,
    633 		    ENA_TSO_MAXSIZE, adapter->max_tx_sgl_size - 1,
    634 		    ENA_TSO_MAXSIZE, 0, 0,
    635 		    &tx_ring->tx_buffer_info[i].map);
    636 		if (unlikely(err != 0)) {
    637 			ena_trace(ENA_ALERT,
    638 			     "Unable to create Tx DMA map for buffer %d\n", i);
    639 			goto err_buf_info_unmap;
    640 		}
    641 	}
    642 
    643 	/* Allocate workqueues */
    644 	int rc = workqueue_create(&tx_ring->enqueue_tq, "ena_tx_enque",
    645 	    ena_deferred_mq_start, tx_ring, 0, IPL_NET, 0);
    646 	if (unlikely(rc == 0)) {
    647 		ena_trace(ENA_ALERT,
    648 		    "Unable to create workqueue for enqueue task\n");
    649 		i = tx_ring->ring_size;
    650 		goto err_buf_info_unmap;
    651 	}
    652 
    653 #if 0
    654 	/* RSS set cpu for thread */
    655 #ifdef RSS
    656 	CPU_SETOF(que->cpu, &cpu_mask);
    657 	taskqueue_start_threads_cpuset(&tx_ring->enqueue_tq, 1, IPL_NET,
    658 	    &cpu_mask, "%s tx_ring enq (bucket %d)",
    659 	    device_xname(adapter->pdev), que->cpu);
    660 #else /* RSS */
    661 	taskqueue_start_threads(&tx_ring->enqueue_tq, 1, IPL_NET,
    662 	    "%s txeq %d", device_xname(adapter->pdev), que->cpu);
    663 #endif /* RSS */
    664 #endif
    665 
    666 	return (0);
    667 
    668 err_buf_info_unmap:
    669 	while (i--) {
    670 		bus_dmamap_destroy(adapter->sc_dmat,
    671 		    tx_ring->tx_buffer_info[i].map);
    672 	}
    673 	free(tx_ring->free_tx_ids, M_DEVBUF);
    674 	tx_ring->free_tx_ids = NULL;
    675 err_buf_info_free:
    676 	free(tx_ring->tx_buffer_info, M_DEVBUF);
    677 	tx_ring->tx_buffer_info = NULL;
    678 
    679 	return (ENOMEM);
    680 }
    681 
    682 /**
    683  * ena_free_tx_resources - Free Tx Resources per Queue
    684  * @adapter: network interface device structure
    685  * @qid: queue index
    686  *
    687  * Free all transmit software resources
    688  **/
    689 static void
    690 ena_free_tx_resources(struct ena_adapter *adapter, int qid)
    691 {
    692 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
    693 
    694 	workqueue_wait(tx_ring->enqueue_tq, &tx_ring->enqueue_task);
    695 	workqueue_destroy(tx_ring->enqueue_tq);
    696 	tx_ring->enqueue_tq = NULL;
    697 
    698 	ENA_RING_MTX_LOCK(tx_ring);
    699 	/* Flush buffer ring, */
    700 	drbr_flush(adapter->ifp, tx_ring->br);
    701 
    702 	/* Free buffer DMA maps, */
    703 	for (int i = 0; i < tx_ring->ring_size; i++) {
    704 		m_freem(tx_ring->tx_buffer_info[i].mbuf);
    705 		tx_ring->tx_buffer_info[i].mbuf = NULL;
    706 		bus_dmamap_unload(adapter->sc_dmat,
    707 		    tx_ring->tx_buffer_info[i].map);
    708 		bus_dmamap_destroy(adapter->sc_dmat,
    709 		    tx_ring->tx_buffer_info[i].map);
    710 	}
    711 	ENA_RING_MTX_UNLOCK(tx_ring);
    712 
    713 	/* And free allocated memory. */
    714 	free(tx_ring->tx_buffer_info, M_DEVBUF);
    715 	tx_ring->tx_buffer_info = NULL;
    716 
    717 	free(tx_ring->free_tx_ids, M_DEVBUF);
    718 	tx_ring->free_tx_ids = NULL;
    719 }
    720 
    721 /**
    722  * ena_setup_all_tx_resources - allocate all queues Tx resources
    723  * @adapter: network interface device structure
    724  *
    725  * Returns 0 on success, otherwise on failure.
    726  **/
    727 static int
    728 ena_setup_all_tx_resources(struct ena_adapter *adapter)
    729 {
    730 	int i, rc;
    731 
    732 	for (i = 0; i < adapter->num_queues; i++) {
    733 		rc = ena_setup_tx_resources(adapter, i);
    734 		if (rc != 0) {
    735 			device_printf(adapter->pdev,
    736 			    "Allocation for Tx Queue %u failed\n", i);
    737 			goto err_setup_tx;
    738 		}
    739 	}
    740 
    741 	return (0);
    742 
    743 err_setup_tx:
    744 	/* Rewind the index freeing the rings as we go */
    745 	while (i--)
    746 		ena_free_tx_resources(adapter, i);
    747 	return (rc);
    748 }
    749 
    750 /**
    751  * ena_free_all_tx_resources - Free Tx Resources for All Queues
    752  * @adapter: network interface device structure
    753  *
    754  * Free all transmit software resources
    755  **/
    756 static void
    757 ena_free_all_tx_resources(struct ena_adapter *adapter)
    758 {
    759 	int i;
    760 
    761 	for (i = 0; i < adapter->num_queues; i++)
    762 		ena_free_tx_resources(adapter, i);
    763 }
    764 
    765 static inline int
    766 validate_rx_req_id(struct ena_ring *rx_ring, uint16_t req_id)
    767 {
    768 	if (likely(req_id < rx_ring->ring_size))
    769 		return (0);
    770 
    771 	device_printf(rx_ring->adapter->pdev, "Invalid rx req_id: %hu\n",
    772 	    req_id);
    773 	counter_u64_add(rx_ring->rx_stats.bad_req_id, 1);
    774 
    775 	/* Trigger device reset */
    776 	rx_ring->adapter->reset_reason = ENA_REGS_RESET_INV_RX_REQ_ID;
    777 	rx_ring->adapter->trigger_reset = true;
    778 
    779 	return (EFAULT);
    780 }
    781 
    782 /**
    783  * ena_setup_rx_resources - allocate Rx resources (Descriptors)
    784  * @adapter: network interface device structure
    785  * @qid: queue index
    786  *
    787  * Returns 0 on success, otherwise on failure.
    788  **/
    789 static int
    790 ena_setup_rx_resources(struct ena_adapter *adapter, unsigned int qid)
    791 {
    792 	struct ena_que *que = &adapter->que[qid];
    793 	struct ena_ring *rx_ring = que->rx_ring;
    794 	int size, err, i;
    795 #ifdef	RSS
    796 	cpuset_t cpu_mask;
    797 #endif
    798 
    799 	size = sizeof(struct ena_rx_buffer) * rx_ring->ring_size;
    800 
    801 	/*
    802 	 * Alloc extra element so in rx path
    803 	 * we can always prefetch rx_info + 1
    804 	 */
    805 	size += sizeof(struct ena_rx_buffer);
    806 
    807 	rx_ring->rx_buffer_info = malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
    808 
    809 	size = sizeof(uint16_t) * rx_ring->ring_size;
    810 	rx_ring->free_rx_ids = malloc(size, M_DEVBUF, M_WAITOK);
    811 
    812 	for (i = 0; i < rx_ring->ring_size; i++)
    813 		rx_ring->free_rx_ids[i] = i;
    814 
    815 	/* Reset RX statistics. */
    816 	ena_reset_counters((struct evcnt *)&rx_ring->rx_stats,
    817 	    sizeof(rx_ring->rx_stats));
    818 
    819 	rx_ring->next_to_clean = 0;
    820 	rx_ring->next_to_use = 0;
    821 
    822 	/* ... and create the buffer DMA maps */
    823 	for (i = 0; i < rx_ring->ring_size; i++) {
    824 		err = bus_dmamap_create(adapter->sc_dmat,
    825 		    MJUM16BYTES, adapter->max_rx_sgl_size, MJUM16BYTES,
    826 		    0, 0,
    827 		    &(rx_ring->rx_buffer_info[i].map));
    828 		if (err != 0) {
    829 			ena_trace(ENA_ALERT,
    830 			    "Unable to create Rx DMA map for buffer %d\n", i);
    831 			goto err_buf_info_unmap;
    832 		}
    833 	}
    834 
    835 #ifdef LRO
    836 	/* Create LRO for the ring */
    837 	if ((adapter->ifp->if_capenable & IFCAP_LRO) != 0) {
    838 		int err = tcp_lro_init(&rx_ring->lro);
    839 		if (err != 0) {
    840 			device_printf(adapter->pdev,
    841 			    "LRO[%d] Initialization failed!\n", qid);
    842 		} else {
    843 			ena_trace(ENA_INFO,
    844 			    "RX Soft LRO[%d] Initialized\n", qid);
    845 			rx_ring->lro.ifp = adapter->ifp;
    846 		}
    847 	}
    848 #endif
    849 
    850 	/* Allocate workqueues */
    851 	int rc = workqueue_create(&rx_ring->cmpl_tq, "ena RX completion",
    852 	    ena_deferred_rx_cleanup, rx_ring, 0, IPL_NET, 0);
    853 	if (unlikely(rc != 0)) {
    854 		ena_trace(ENA_ALERT,
    855 		    "Unable to create workqueue for RX completion task\n");
    856 		goto err_buf_info_unmap;
    857 	}
    858 
    859 #if 0
    860 	/* RSS set cpu for thread */
    861 #ifdef RSS
    862 	CPU_SETOF(que->cpu, &cpu_mask);
    863 	taskqueue_start_threads_cpuset(&rx_ring->cmpl_tq, 1, IPL_NET, &cpu_mask,
    864 	    "%s rx_ring cmpl (bucket %d)",
    865 	    device_xname(adapter->pdev), que->cpu);
    866 #else
    867 	taskqueue_start_threads(&rx_ring->cmpl_tq, 1, IPL_NET,
    868 	    "%s rx_ring cmpl %d", device_xname(adapter->pdev), que->cpu);
    869 #endif
    870 #endif
    871 
    872 	return (0);
    873 
    874 err_buf_info_unmap:
    875 	while (i--) {
    876 		bus_dmamap_destroy(adapter->sc_dmat,
    877 		    rx_ring->rx_buffer_info[i].map);
    878 	}
    879 
    880 	free(rx_ring->free_rx_ids, M_DEVBUF);
    881 	rx_ring->free_rx_ids = NULL;
    882 	free(rx_ring->rx_buffer_info, M_DEVBUF);
    883 	rx_ring->rx_buffer_info = NULL;
    884 	return (ENOMEM);
    885 }
    886 
    887 /**
    888  * ena_free_rx_resources - Free Rx Resources
    889  * @adapter: network interface device structure
    890  * @qid: queue index
    891  *
    892  * Free all receive software resources
    893  **/
    894 static void
    895 ena_free_rx_resources(struct ena_adapter *adapter, unsigned int qid)
    896 {
    897 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
    898 
    899 	workqueue_wait(rx_ring->cmpl_tq, &rx_ring->cmpl_task);
    900 	workqueue_destroy(rx_ring->cmpl_tq);
    901 	rx_ring->cmpl_tq = NULL;
    902 
    903 	/* Free buffer DMA maps, */
    904 	for (int i = 0; i < rx_ring->ring_size; i++) {
    905 		m_freem(rx_ring->rx_buffer_info[i].mbuf);
    906 		rx_ring->rx_buffer_info[i].mbuf = NULL;
    907 		bus_dmamap_unload(adapter->sc_dmat,
    908 		    rx_ring->rx_buffer_info[i].map);
    909 		bus_dmamap_destroy(adapter->sc_dmat,
    910 		    rx_ring->rx_buffer_info[i].map);
    911 	}
    912 
    913 #ifdef LRO
    914 	/* free LRO resources, */
    915 	tcp_lro_free(&rx_ring->lro);
    916 #endif
    917 
    918 	/* free allocated memory */
    919 	free(rx_ring->rx_buffer_info, M_DEVBUF);
    920 	rx_ring->rx_buffer_info = NULL;
    921 
    922 	free(rx_ring->free_rx_ids, M_DEVBUF);
    923 	rx_ring->free_rx_ids = NULL;
    924 }
    925 
    926 /**
    927  * ena_setup_all_rx_resources - allocate all queues Rx resources
    928  * @adapter: network interface device structure
    929  *
    930  * Returns 0 on success, otherwise on failure.
    931  **/
    932 static int
    933 ena_setup_all_rx_resources(struct ena_adapter *adapter)
    934 {
    935 	int i, rc = 0;
    936 
    937 	for (i = 0; i < adapter->num_queues; i++) {
    938 		rc = ena_setup_rx_resources(adapter, i);
    939 		if (rc != 0) {
    940 			device_printf(adapter->pdev,
    941 			    "Allocation for Rx Queue %u failed\n", i);
    942 			goto err_setup_rx;
    943 		}
    944 	}
    945 	return (0);
    946 
    947 err_setup_rx:
    948 	/* rewind the index freeing the rings as we go */
    949 	while (i--)
    950 		ena_free_rx_resources(adapter, i);
    951 	return (rc);
    952 }
    953 
    954 /**
    955  * ena_free_all_rx_resources - Free Rx resources for all queues
    956  * @adapter: network interface device structure
    957  *
    958  * Free all receive software resources
    959  **/
    960 static void
    961 ena_free_all_rx_resources(struct ena_adapter *adapter)
    962 {
    963 	int i;
    964 
    965 	for (i = 0; i < adapter->num_queues; i++)
    966 		ena_free_rx_resources(adapter, i);
    967 }
    968 
    969 static inline int
    970 ena_alloc_rx_mbuf(struct ena_adapter *adapter,
    971     struct ena_ring *rx_ring, struct ena_rx_buffer *rx_info)
    972 {
    973 	struct ena_com_buf *ena_buf;
    974 	int error;
    975 	int mlen;
    976 
    977 	/* if previous allocated frag is not used */
    978 	if (unlikely(rx_info->mbuf != NULL))
    979 		return (0);
    980 
    981 	/* Get mbuf using UMA allocator */
    982 	rx_info->mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM16BYTES);
    983 
    984 	if (unlikely(rx_info->mbuf == NULL)) {
    985 		counter_u64_add(rx_ring->rx_stats.mjum_alloc_fail, 1);
    986 		rx_info->mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
    987 		if (unlikely(rx_info->mbuf == NULL)) {
    988 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
    989 			return (ENOMEM);
    990 		}
    991 		mlen = MCLBYTES;
    992 	} else {
    993 		mlen = MJUM16BYTES;
    994 	}
    995 	/* Set mbuf length*/
    996 	rx_info->mbuf->m_pkthdr.len = rx_info->mbuf->m_len = mlen;
    997 
    998 	/* Map packets for DMA */
    999 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
   1000 	    "Using tag %p for buffers' DMA mapping, mbuf %p len: %d",
   1001 	    adapter->sc_dmat,rx_info->mbuf, rx_info->mbuf->m_len);
   1002 	error = bus_dmamap_load_mbuf(adapter->sc_dmat, rx_info->map,
   1003 	    rx_info->mbuf, BUS_DMA_NOWAIT);
   1004 	if (unlikely((error != 0) || (rx_info->map->dm_nsegs != 1))) {
   1005 		ena_trace(ENA_WARNING, "failed to map mbuf, error: %d, "
   1006 		    "nsegs: %d\n", error, rx_info->map->dm_nsegs);
   1007 		counter_u64_add(rx_ring->rx_stats.dma_mapping_err, 1);
   1008 		goto exit;
   1009 
   1010 	}
   1011 
   1012 	bus_dmamap_sync(adapter->sc_dmat, rx_info->map, 0,
   1013 	    rx_info->map->dm_mapsize, BUS_DMASYNC_PREREAD);
   1014 
   1015 	ena_buf = &rx_info->ena_buf;
   1016 	ena_buf->paddr = rx_info->map->dm_segs[0].ds_addr;
   1017 	ena_buf->len = mlen;
   1018 
   1019 	ena_trace(ENA_DBG | ENA_RSC | ENA_RXPTH,
   1020 	    "ALLOC RX BUF: mbuf %p, rx_info %p, len %d, paddr %#jx\n",
   1021 	    rx_info->mbuf, rx_info,ena_buf->len, (uintmax_t)ena_buf->paddr);
   1022 
   1023 	return (0);
   1024 
   1025 exit:
   1026 	m_freem(rx_info->mbuf);
   1027 	rx_info->mbuf = NULL;
   1028 	return (EFAULT);
   1029 }
   1030 
   1031 static void
   1032 ena_free_rx_mbuf(struct ena_adapter *adapter, struct ena_ring *rx_ring,
   1033     struct ena_rx_buffer *rx_info)
   1034 {
   1035 
   1036 	if (rx_info->mbuf == NULL) {
   1037 		ena_trace(ENA_WARNING, "Trying to free unallocated buffer\n");
   1038 		return;
   1039 	}
   1040 
   1041 	bus_dmamap_unload(adapter->sc_dmat, rx_info->map);
   1042 	m_freem(rx_info->mbuf);
   1043 	rx_info->mbuf = NULL;
   1044 }
   1045 
   1046 /**
   1047  * ena_refill_rx_bufs - Refills ring with descriptors
   1048  * @rx_ring: the ring which we want to feed with free descriptors
   1049  * @num: number of descriptors to refill
   1050  * Refills the ring with newly allocated DMA-mapped mbufs for receiving
   1051  **/
   1052 static int
   1053 ena_refill_rx_bufs(struct ena_ring *rx_ring, uint32_t num)
   1054 {
   1055 	struct ena_adapter *adapter = rx_ring->adapter;
   1056 	uint16_t next_to_use, req_id;
   1057 	uint32_t i;
   1058 	int rc;
   1059 
   1060 	ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC, "refill qid: %d",
   1061 	    rx_ring->qid);
   1062 
   1063 	next_to_use = rx_ring->next_to_use;
   1064 
   1065 	for (i = 0; i < num; i++) {
   1066 		struct ena_rx_buffer *rx_info;
   1067 
   1068 		ena_trace(ENA_DBG | ENA_RXPTH | ENA_RSC,
   1069 		    "RX buffer - next to use: %d", next_to_use);
   1070 
   1071 		req_id = rx_ring->free_rx_ids[next_to_use];
   1072 		rc = validate_rx_req_id(rx_ring, req_id);
   1073 		if (unlikely(rc != 0))
   1074 			break;
   1075 
   1076 		rx_info = &rx_ring->rx_buffer_info[req_id];
   1077 
   1078 		rc = ena_alloc_rx_mbuf(adapter, rx_ring, rx_info);
   1079 		if (unlikely(rc != 0)) {
   1080 			ena_trace(ENA_WARNING,
   1081 			    "failed to alloc buffer for rx queue %d\n",
   1082 			    rx_ring->qid);
   1083 			break;
   1084 		}
   1085 		rc = ena_com_add_single_rx_desc(rx_ring->ena_com_io_sq,
   1086 		    &rx_info->ena_buf, req_id);
   1087 		if (unlikely(rc != 0)) {
   1088 			ena_trace(ENA_WARNING,
   1089 			    "failed to add buffer for rx queue %d\n",
   1090 			    rx_ring->qid);
   1091 			break;
   1092 		}
   1093 		next_to_use = ENA_RX_RING_IDX_NEXT(next_to_use,
   1094 		    rx_ring->ring_size);
   1095 	}
   1096 
   1097 	if (unlikely(i < num)) {
   1098 		counter_u64_add(rx_ring->rx_stats.refil_partial, 1);
   1099 		ena_trace(ENA_WARNING,
   1100 		     "refilled rx qid %d with only %d mbufs (from %d)\n",
   1101 		     rx_ring->qid, i, num);
   1102 	}
   1103 
   1104 	if (likely(i != 0)) {
   1105 		wmb();
   1106 		ena_com_write_sq_doorbell(rx_ring->ena_com_io_sq);
   1107 	}
   1108 	rx_ring->next_to_use = next_to_use;
   1109 	return (i);
   1110 }
   1111 
   1112 static void
   1113 ena_free_rx_bufs(struct ena_adapter *adapter, unsigned int qid)
   1114 {
   1115 	struct ena_ring *rx_ring = &adapter->rx_ring[qid];
   1116 	unsigned int i;
   1117 
   1118 	for (i = 0; i < rx_ring->ring_size; i++) {
   1119 		struct ena_rx_buffer *rx_info = &rx_ring->rx_buffer_info[i];
   1120 
   1121 		if (rx_info->mbuf != NULL)
   1122 			ena_free_rx_mbuf(adapter, rx_ring, rx_info);
   1123 	}
   1124 }
   1125 
   1126 /**
   1127  * ena_refill_all_rx_bufs - allocate all queues Rx buffers
   1128  * @adapter: network interface device structure
   1129  *
   1130  */
   1131 static void
   1132 ena_refill_all_rx_bufs(struct ena_adapter *adapter)
   1133 {
   1134 	struct ena_ring *rx_ring;
   1135 	int i, rc, bufs_num;
   1136 
   1137 	for (i = 0; i < adapter->num_queues; i++) {
   1138 		rx_ring = &adapter->rx_ring[i];
   1139 		bufs_num = rx_ring->ring_size - 1;
   1140 		rc = ena_refill_rx_bufs(rx_ring, bufs_num);
   1141 
   1142 		if (unlikely(rc != bufs_num))
   1143 			ena_trace(ENA_WARNING, "refilling Queue %d failed. "
   1144 			    "Allocated %d buffers from: %d\n", i, rc, bufs_num);
   1145 	}
   1146 }
   1147 
   1148 static void
   1149 ena_free_all_rx_bufs(struct ena_adapter *adapter)
   1150 {
   1151 	int i;
   1152 
   1153 	for (i = 0; i < adapter->num_queues; i++)
   1154 		ena_free_rx_bufs(adapter, i);
   1155 }
   1156 
   1157 /**
   1158  * ena_free_tx_bufs - Free Tx Buffers per Queue
   1159  * @adapter: network interface device structure
   1160  * @qid: queue index
   1161  **/
   1162 static void
   1163 ena_free_tx_bufs(struct ena_adapter *adapter, unsigned int qid)
   1164 {
   1165 	bool print_once = true;
   1166 	struct ena_ring *tx_ring = &adapter->tx_ring[qid];
   1167 
   1168 	ENA_RING_MTX_LOCK(tx_ring);
   1169 	for (int i = 0; i < tx_ring->ring_size; i++) {
   1170 		struct ena_tx_buffer *tx_info = &tx_ring->tx_buffer_info[i];
   1171 
   1172 		if (tx_info->mbuf == NULL)
   1173 			continue;
   1174 
   1175 		if (print_once) {
   1176 			device_printf(adapter->pdev,
   1177 			    "free uncompleted tx mbuf qid %d idx 0x%x",
   1178 			    qid, i);
   1179 			print_once = false;
   1180 		} else {
   1181 			ena_trace(ENA_DBG,
   1182 			    "free uncompleted tx mbuf qid %d idx 0x%x",
   1183 			     qid, i);
   1184 		}
   1185 
   1186 		bus_dmamap_unload(adapter->sc_dmat, tx_info->map);
   1187 		m_free(tx_info->mbuf);
   1188 		tx_info->mbuf = NULL;
   1189 	}
   1190 	ENA_RING_MTX_UNLOCK(tx_ring);
   1191 }
   1192 
   1193 static void
   1194 ena_free_all_tx_bufs(struct ena_adapter *adapter)
   1195 {
   1196 
   1197 	for (int i = 0; i < adapter->num_queues; i++)
   1198 		ena_free_tx_bufs(adapter, i);
   1199 }
   1200 
   1201 static void
   1202 ena_destroy_all_tx_queues(struct ena_adapter *adapter)
   1203 {
   1204 	uint16_t ena_qid;
   1205 	int i;
   1206 
   1207 	for (i = 0; i < adapter->num_queues; i++) {
   1208 		ena_qid = ENA_IO_TXQ_IDX(i);
   1209 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
   1210 	}
   1211 }
   1212 
   1213 static void
   1214 ena_destroy_all_rx_queues(struct ena_adapter *adapter)
   1215 {
   1216 	uint16_t ena_qid;
   1217 	int i;
   1218 
   1219 	for (i = 0; i < adapter->num_queues; i++) {
   1220 		ena_qid = ENA_IO_RXQ_IDX(i);
   1221 		ena_com_destroy_io_queue(adapter->ena_dev, ena_qid);
   1222 	}
   1223 }
   1224 
   1225 static void
   1226 ena_destroy_all_io_queues(struct ena_adapter *adapter)
   1227 {
   1228 	ena_destroy_all_tx_queues(adapter);
   1229 	ena_destroy_all_rx_queues(adapter);
   1230 }
   1231 
   1232 static inline int
   1233 validate_tx_req_id(struct ena_ring *tx_ring, uint16_t req_id)
   1234 {
   1235 	struct ena_adapter *adapter = tx_ring->adapter;
   1236 	struct ena_tx_buffer *tx_info = NULL;
   1237 
   1238 	if (likely(req_id < tx_ring->ring_size)) {
   1239 		tx_info = &tx_ring->tx_buffer_info[req_id];
   1240 		if (tx_info->mbuf != NULL)
   1241 			return (0);
   1242 	}
   1243 
   1244 	if (tx_info->mbuf == NULL)
   1245 		device_printf(adapter->pdev,
   1246 		    "tx_info doesn't have valid mbuf\n");
   1247 	else
   1248 		device_printf(adapter->pdev, "Invalid req_id: %hu\n", req_id);
   1249 
   1250 	counter_u64_add(tx_ring->tx_stats.bad_req_id, 1);
   1251 
   1252 	return (EFAULT);
   1253 }
   1254 
   1255 static int
   1256 ena_create_io_queues(struct ena_adapter *adapter)
   1257 {
   1258 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   1259 	struct ena_com_create_io_ctx ctx;
   1260 	struct ena_ring *ring;
   1261 	uint16_t ena_qid;
   1262 	uint32_t msix_vector;
   1263 	int rc, i;
   1264 
   1265 	/* Create TX queues */
   1266 	for (i = 0; i < adapter->num_queues; i++) {
   1267 		msix_vector = ENA_IO_IRQ_IDX(i);
   1268 		ena_qid = ENA_IO_TXQ_IDX(i);
   1269 		ctx.mem_queue_type = ena_dev->tx_mem_queue_type;
   1270 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_TX;
   1271 		ctx.queue_size = adapter->tx_ring_size;
   1272 		ctx.msix_vector = msix_vector;
   1273 		ctx.qid = ena_qid;
   1274 		rc = ena_com_create_io_queue(ena_dev, &ctx);
   1275 		if (rc != 0) {
   1276 			device_printf(adapter->pdev,
   1277 			    "Failed to create io TX queue #%d rc: %d\n", i, rc);
   1278 			goto err_tx;
   1279 		}
   1280 		ring = &adapter->tx_ring[i];
   1281 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
   1282 		    &ring->ena_com_io_sq,
   1283 		    &ring->ena_com_io_cq);
   1284 		if (rc != 0) {
   1285 			device_printf(adapter->pdev,
   1286 			    "Failed to get TX queue handlers. TX queue num"
   1287 			    " %d rc: %d\n", i, rc);
   1288 			ena_com_destroy_io_queue(ena_dev, ena_qid);
   1289 			goto err_tx;
   1290 		}
   1291 	}
   1292 
   1293 	/* Create RX queues */
   1294 	for (i = 0; i < adapter->num_queues; i++) {
   1295 		msix_vector = ENA_IO_IRQ_IDX(i);
   1296 		ena_qid = ENA_IO_RXQ_IDX(i);
   1297 		ctx.mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
   1298 		ctx.direction = ENA_COM_IO_QUEUE_DIRECTION_RX;
   1299 		ctx.queue_size = adapter->rx_ring_size;
   1300 		ctx.msix_vector = msix_vector;
   1301 		ctx.qid = ena_qid;
   1302 		rc = ena_com_create_io_queue(ena_dev, &ctx);
   1303 		if (unlikely(rc != 0)) {
   1304 			device_printf(adapter->pdev,
   1305 			    "Failed to create io RX queue[%d] rc: %d\n", i, rc);
   1306 			goto err_rx;
   1307 		}
   1308 
   1309 		ring = &adapter->rx_ring[i];
   1310 		rc = ena_com_get_io_handlers(ena_dev, ena_qid,
   1311 		    &ring->ena_com_io_sq,
   1312 		    &ring->ena_com_io_cq);
   1313 		if (unlikely(rc != 0)) {
   1314 			device_printf(adapter->pdev,
   1315 			    "Failed to get RX queue handlers. RX queue num"
   1316 			    " %d rc: %d\n", i, rc);
   1317 			ena_com_destroy_io_queue(ena_dev, ena_qid);
   1318 			goto err_rx;
   1319 		}
   1320 	}
   1321 
   1322 	return (0);
   1323 
   1324 err_rx:
   1325 	while (i--)
   1326 		ena_com_destroy_io_queue(ena_dev, ENA_IO_RXQ_IDX(i));
   1327 	i = adapter->num_queues;
   1328 err_tx:
   1329 	while (i--)
   1330 		ena_com_destroy_io_queue(ena_dev, ENA_IO_TXQ_IDX(i));
   1331 
   1332 	return (ENXIO);
   1333 }
   1334 
   1335 /**
   1336  * ena_tx_cleanup - clear sent packets and corresponding descriptors
   1337  * @tx_ring: ring for which we want to clean packets
   1338  *
   1339  * Once packets are sent, we ask the device in a loop for no longer used
   1340  * descriptors. We find the related mbuf chain in a map (index in an array)
   1341  * and free it, then update ring state.
   1342  * This is performed in "endless" loop, updating ring pointers every
   1343  * TX_COMMIT. The first check of free descriptor is performed before the actual
   1344  * loop, then repeated at the loop end.
   1345  **/
   1346 static int
   1347 ena_tx_cleanup(struct ena_ring *tx_ring)
   1348 {
   1349 	struct ena_adapter *adapter;
   1350 	struct ena_com_io_cq* io_cq;
   1351 	uint16_t next_to_clean;
   1352 	uint16_t req_id;
   1353 	uint16_t ena_qid;
   1354 	unsigned int total_done = 0;
   1355 	int rc;
   1356 	int commit = TX_COMMIT;
   1357 	int budget = TX_BUDGET;
   1358 	int work_done;
   1359 
   1360 	adapter = tx_ring->que->adapter;
   1361 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
   1362 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   1363 	next_to_clean = tx_ring->next_to_clean;
   1364 
   1365 	do {
   1366 		struct ena_tx_buffer *tx_info;
   1367 		struct mbuf *mbuf;
   1368 
   1369 		rc = ena_com_tx_comp_req_id_get(io_cq, &req_id);
   1370 		if (unlikely(rc != 0))
   1371 			break;
   1372 
   1373 		rc = validate_tx_req_id(tx_ring, req_id);
   1374 		if (unlikely(rc != 0))
   1375 			break;
   1376 
   1377 		tx_info = &tx_ring->tx_buffer_info[req_id];
   1378 
   1379 		mbuf = tx_info->mbuf;
   1380 
   1381 		tx_info->mbuf = NULL;
   1382 		bintime_clear(&tx_info->timestamp);
   1383 
   1384 		if (likely(tx_info->num_of_bufs != 0)) {
   1385 			/* Map is no longer required */
   1386 			bus_dmamap_unload(adapter->sc_dmat, tx_info->map);
   1387 		}
   1388 
   1389 		ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d mbuf %p completed",
   1390 		    tx_ring->qid, mbuf);
   1391 
   1392 		m_freem(mbuf);
   1393 
   1394 		total_done += tx_info->tx_descs;
   1395 
   1396 		tx_ring->free_tx_ids[next_to_clean] = req_id;
   1397 		next_to_clean = ENA_TX_RING_IDX_NEXT(next_to_clean,
   1398 		    tx_ring->ring_size);
   1399 
   1400 		if (unlikely(--commit == 0)) {
   1401 			commit = TX_COMMIT;
   1402 			/* update ring state every TX_COMMIT descriptor */
   1403 			tx_ring->next_to_clean = next_to_clean;
   1404 			ena_com_comp_ack(
   1405 			    &adapter->ena_dev->io_sq_queues[ena_qid],
   1406 			    total_done);
   1407 			ena_com_update_dev_comp_head(io_cq);
   1408 			total_done = 0;
   1409 		}
   1410 	} while (likely(--budget));
   1411 
   1412 	work_done = TX_BUDGET - budget;
   1413 
   1414 	ena_trace(ENA_DBG | ENA_TXPTH, "tx: q %d done. total pkts: %d",
   1415 	tx_ring->qid, work_done);
   1416 
   1417 	/* If there is still something to commit update ring state */
   1418 	if (likely(commit != TX_COMMIT)) {
   1419 		tx_ring->next_to_clean = next_to_clean;
   1420 		ena_com_comp_ack(&adapter->ena_dev->io_sq_queues[ena_qid],
   1421 		    total_done);
   1422 		ena_com_update_dev_comp_head(io_cq);
   1423 	}
   1424 
   1425 	workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task, NULL);
   1426 
   1427 	return (work_done);
   1428 }
   1429 
   1430 #if 0
   1431 static void
   1432 ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
   1433     struct mbuf *mbuf)
   1434 {
   1435 	struct ena_adapter *adapter = rx_ring->adapter;
   1436 
   1437 	if (likely(adapter->rss_support)) {
   1438 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
   1439 
   1440 		if (ena_rx_ctx->frag &&
   1441 		    (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
   1442 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
   1443 			return;
   1444 		}
   1445 
   1446 		switch (ena_rx_ctx->l3_proto) {
   1447 		case ENA_ETH_IO_L3_PROTO_IPV4:
   1448 			switch (ena_rx_ctx->l4_proto) {
   1449 			case ENA_ETH_IO_L4_PROTO_TCP:
   1450 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
   1451 				break;
   1452 			case ENA_ETH_IO_L4_PROTO_UDP:
   1453 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
   1454 				break;
   1455 			default:
   1456 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
   1457 			}
   1458 			break;
   1459 		case ENA_ETH_IO_L3_PROTO_IPV6:
   1460 			switch (ena_rx_ctx->l4_proto) {
   1461 			case ENA_ETH_IO_L4_PROTO_TCP:
   1462 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
   1463 				break;
   1464 			case ENA_ETH_IO_L4_PROTO_UDP:
   1465 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
   1466 				break;
   1467 			default:
   1468 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
   1469 			}
   1470 			break;
   1471 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
   1472 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
   1473 			break;
   1474 		default:
   1475 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
   1476 		}
   1477 	} else {
   1478 		mbuf->m_pkthdr.flowid = rx_ring->qid;
   1479 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
   1480 	}
   1481 }
   1482 #endif
   1483 
   1484 /**
   1485  * ena_rx_mbuf - assemble mbuf from descriptors
   1486  * @rx_ring: ring for which we want to clean packets
   1487  * @ena_bufs: buffer info
   1488  * @ena_rx_ctx: metadata for this packet(s)
   1489  * @next_to_clean: ring pointer, will be updated only upon success
   1490  *
   1491  **/
   1492 static struct mbuf*
   1493 ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
   1494     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
   1495 {
   1496 	struct mbuf *mbuf;
   1497 	struct ena_rx_buffer *rx_info;
   1498 	struct ena_adapter *adapter;
   1499 	unsigned int descs = ena_rx_ctx->descs;
   1500 	uint16_t ntc, len, req_id, buf = 0;
   1501 
   1502 	ntc = *next_to_clean;
   1503 	adapter = rx_ring->adapter;
   1504 	rx_info = &rx_ring->rx_buffer_info[ntc];
   1505 
   1506 	if (unlikely(rx_info->mbuf == NULL)) {
   1507 		device_printf(adapter->pdev, "NULL mbuf in rx_info");
   1508 		return (NULL);
   1509 	}
   1510 
   1511 	len = ena_bufs[buf].len;
   1512 	req_id = ena_bufs[buf].req_id;
   1513 	rx_info = &rx_ring->rx_buffer_info[req_id];
   1514 
   1515 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
   1516 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
   1517 
   1518 	mbuf = rx_info->mbuf;
   1519 	KASSERT(mbuf->m_flags & M_PKTHDR);
   1520 	mbuf->m_pkthdr.len = len;
   1521 	mbuf->m_len = len;
   1522 	m_set_rcvif(mbuf, rx_ring->que->adapter->ifp);
   1523 
   1524 	/* Fill mbuf with hash key and it's interpretation for optimization */
   1525 #if 0
   1526 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
   1527 #endif
   1528 
   1529 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
   1530 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
   1531 
   1532 	/* DMA address is not needed anymore, unmap it */
   1533 	bus_dmamap_unload(rx_ring->adapter->sc_dmat, rx_info->map);
   1534 
   1535 	rx_info->mbuf = NULL;
   1536 	rx_ring->free_rx_ids[ntc] = req_id;
   1537 	ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
   1538 
   1539 	/*
   1540 	 * While we have more than 1 descriptors for one rcvd packet, append
   1541 	 * other mbufs to the main one
   1542 	 */
   1543 	while (--descs) {
   1544 		++buf;
   1545 		len = ena_bufs[buf].len;
   1546 		req_id = ena_bufs[buf].req_id;
   1547 		rx_info = &rx_ring->rx_buffer_info[req_id];
   1548 
   1549 		if (unlikely(rx_info->mbuf == NULL)) {
   1550 			device_printf(adapter->pdev, "NULL mbuf in rx_info");
   1551 			/*
   1552 			 * If one of the required mbufs was not allocated yet,
   1553 			 * we can break there.
   1554 			 * All earlier used descriptors will be reallocated
   1555 			 * later and not used mbufs can be reused.
   1556 			 * The next_to_clean pointer will not be updated in case
   1557 			 * of an error, so caller should advance it manually
   1558 			 * in error handling routine to keep it up to date
   1559 			 * with hw ring.
   1560 			 */
   1561 			m_freem(mbuf);
   1562 			return (NULL);
   1563 		}
   1564 
   1565 		if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
   1566 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
   1567 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
   1568 			    mbuf);
   1569 		}
   1570 
   1571 		ena_trace(ENA_DBG | ENA_RXPTH,
   1572 		    "rx mbuf updated. len %d", mbuf->m_pkthdr.len);
   1573 
   1574 		/* Free already appended mbuf, it won't be useful anymore */
   1575 		bus_dmamap_unload(rx_ring->adapter->sc_dmat, rx_info->map);
   1576 		m_freem(rx_info->mbuf);
   1577 		rx_info->mbuf = NULL;
   1578 
   1579 		rx_ring->free_rx_ids[ntc] = req_id;
   1580 		ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
   1581 	}
   1582 
   1583 	*next_to_clean = ntc;
   1584 
   1585 	return (mbuf);
   1586 }
   1587 
   1588 /**
   1589  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
   1590  **/
   1591 static inline void
   1592 ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
   1593     struct mbuf *mbuf)
   1594 {
   1595 
   1596 	/* IPv4 */
   1597 	if ((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4)) {
   1598 		mbuf->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1599 		if (ena_rx_ctx->l3_csum_err) {
   1600 			/* ipv4 checksum error */
   1601 			mbuf->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1602 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
   1603 			ena_trace(ENA_DBG, "RX IPv4 header checksum error");
   1604 			return;
   1605 		}
   1606 
   1607 		/*  TCP/UDP */
   1608 		if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
   1609 		    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
   1610 			mbuf->m_pkthdr.csum_flags |= (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ? M_CSUM_TCPv4 : M_CSUM_UDPv4;
   1611 			if (ena_rx_ctx->l4_csum_err) {
   1612 				/* TCP/UDP checksum error */
   1613 				mbuf->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1614 				counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
   1615 				ena_trace(ENA_DBG, "RX L4 checksum error");
   1616 			}
   1617 		}
   1618 	}
   1619 	/* IPv6 */
   1620 	else if ((ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6)) {
   1621 		/*  TCP/UDP */
   1622 		if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
   1623 		    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
   1624 			mbuf->m_pkthdr.csum_flags |= (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ? M_CSUM_TCPv6 : M_CSUM_UDPv6;
   1625 			if (ena_rx_ctx->l4_csum_err) {
   1626 				/* TCP/UDP checksum error */
   1627 				mbuf->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1628 				counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
   1629 				ena_trace(ENA_DBG, "RX L4 checksum error");
   1630 			}
   1631 		}
   1632 	}
   1633 }
   1634 
   1635 static void
   1636 ena_deferred_rx_cleanup(struct work *wk, void *arg)
   1637 {
   1638 	struct ena_ring *rx_ring = arg;
   1639 	int budget = CLEAN_BUDGET;
   1640 
   1641 	ENA_RING_MTX_LOCK(rx_ring);
   1642 	/*
   1643 	 * If deferred task was executed, perform cleanup of all awaiting
   1644 	 * descs (or until given budget is depleted to avoid infinite loop).
   1645 	 */
   1646 	while (likely(budget--)) {
   1647 		if (ena_rx_cleanup(rx_ring) == 0)
   1648 			break;
   1649 	}
   1650 	ENA_RING_MTX_UNLOCK(rx_ring);
   1651 }
   1652 
   1653 /**
   1654  * ena_rx_cleanup - handle rx irq
   1655  * @arg: ring for which irq is being handled
   1656  **/
   1657 static int
   1658 ena_rx_cleanup(struct ena_ring *rx_ring)
   1659 {
   1660 	struct ena_adapter *adapter;
   1661 	struct mbuf *mbuf;
   1662 	struct ena_com_rx_ctx ena_rx_ctx;
   1663 	struct ena_com_io_cq* io_cq;
   1664 	struct ena_com_io_sq* io_sq;
   1665 	struct ifnet *ifp;
   1666 	uint16_t ena_qid;
   1667 	uint16_t next_to_clean;
   1668 	uint32_t refill_required;
   1669 	uint32_t refill_threshold;
   1670 #ifdef LRO
   1671 	uint32_t do_if_input = 0;
   1672 #endif
   1673 	unsigned int qid;
   1674 	int rc, i;
   1675 	int budget = RX_BUDGET;
   1676 
   1677 	adapter = rx_ring->que->adapter;
   1678 	ifp = adapter->ifp;
   1679 	qid = rx_ring->que->id;
   1680 	ena_qid = ENA_IO_RXQ_IDX(qid);
   1681 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   1682 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
   1683 	next_to_clean = rx_ring->next_to_clean;
   1684 
   1685 	ena_trace(ENA_DBG, "rx: qid %d", qid);
   1686 
   1687 	do {
   1688 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
   1689 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
   1690 		ena_rx_ctx.descs = 0;
   1691 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
   1692 
   1693 		if (unlikely(rc != 0))
   1694 			goto error;
   1695 
   1696 		if (unlikely(ena_rx_ctx.descs == 0))
   1697 			break;
   1698 
   1699 		ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
   1700 		    "descs #: %d l3 proto %d l4 proto %d hash: %x",
   1701 		    rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
   1702 		    ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
   1703 
   1704 		/* Receive mbuf from the ring */
   1705 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
   1706 		    &ena_rx_ctx, &next_to_clean);
   1707 
   1708 		/* Exit if we failed to retrieve a buffer */
   1709 		if (unlikely(mbuf == NULL)) {
   1710 			for (i = 0; i < ena_rx_ctx.descs; ++i) {
   1711 				rx_ring->free_rx_ids[next_to_clean] =
   1712 				    rx_ring->ena_bufs[i].req_id;
   1713 				next_to_clean =
   1714 				    ENA_RX_RING_IDX_NEXT(next_to_clean,
   1715 				    rx_ring->ring_size);
   1716 
   1717 			}
   1718 			break;
   1719 		}
   1720 
   1721 		if (((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0) ||
   1722 		    ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) != 0) ||
   1723 		    ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) != 0) ||
   1724 		    ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) != 0) ||
   1725 		    ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) != 0)) {
   1726 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
   1727 		}
   1728 
   1729 		counter_enter();
   1730 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
   1731 		    mbuf->m_pkthdr.len);
   1732 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
   1733 		    mbuf->m_pkthdr.len);
   1734 		counter_exit();
   1735 #ifdef LRO
   1736 		/*
   1737 		 * LRO is only for IP/TCP packets and TCP checksum of the packet
   1738 		 * should be computed by hardware.
   1739 		 */
   1740 		do_if_input = 1;
   1741 		if (((ifp->if_capenable & IFCAP_LRO) != 0)  &&
   1742 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
   1743 		    (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
   1744 			/*
   1745 			 * Send to the stack if:
   1746 			 *  - LRO not enabled, or
   1747 			 *  - no LRO resources, or
   1748 			 *  - lro enqueue fails
   1749 			 */
   1750 			if ((rx_ring->lro.lro_cnt != 0) &&
   1751 			    (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
   1752 					do_if_input = 0;
   1753 		}
   1754 		if (do_if_input != 0) {
   1755 			ena_trace(ENA_DBG | ENA_RXPTH,
   1756 			    "calling if_input() with mbuf %p", mbuf);
   1757 			(*ifp->if_input)(ifp, mbuf);
   1758 		}
   1759 #endif
   1760 
   1761 		counter_enter();
   1762 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
   1763 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
   1764 		counter_exit();
   1765 	} while (--budget);
   1766 
   1767 	rx_ring->next_to_clean = next_to_clean;
   1768 
   1769 	refill_required = ena_com_free_desc(io_sq);
   1770 	refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
   1771 
   1772 	if (refill_required > refill_threshold) {
   1773 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
   1774 		ena_refill_rx_bufs(rx_ring, refill_required);
   1775 	}
   1776 
   1777 #ifdef LRO
   1778 	tcp_lro_flush_all(&rx_ring->lro);
   1779 #endif
   1780 
   1781 	return (RX_BUDGET - budget);
   1782 
   1783 error:
   1784 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
   1785 	return (RX_BUDGET - budget);
   1786 }
   1787 
   1788 /*********************************************************************
   1789  *
   1790  *  MSIX & Interrupt Service routine
   1791  *
   1792  **********************************************************************/
   1793 
   1794 /**
   1795  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
   1796  * @arg: interrupt number
   1797  **/
   1798 static int
   1799 ena_intr_msix_mgmnt(void *arg)
   1800 {
   1801 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
   1802 
   1803 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
   1804 	if (likely(adapter->running))
   1805 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
   1806 
   1807 	return 1;
   1808 }
   1809 
   1810 /**
   1811  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
   1812  * @arg: interrupt number
   1813  **/
   1814 static int
   1815 ena_handle_msix(void *arg)
   1816 {
   1817 	struct ena_que	*que = arg;
   1818 	struct ena_adapter *adapter = que->adapter;
   1819 	struct ifnet *ifp = adapter->ifp;
   1820 	struct ena_ring *tx_ring;
   1821 	struct ena_ring *rx_ring;
   1822 	struct ena_com_io_cq* io_cq;
   1823 	struct ena_eth_io_intr_reg intr_reg;
   1824 	int qid, ena_qid;
   1825 	int txc, rxc, i;
   1826 
   1827 	if (unlikely((if_getdrvflags(ifp) & IFF_RUNNING) == 0))
   1828 		return 0;
   1829 
   1830 	ena_trace(ENA_DBG, "MSI-X TX/RX routine");
   1831 
   1832 	tx_ring = que->tx_ring;
   1833 	rx_ring = que->rx_ring;
   1834 	qid = que->id;
   1835 	ena_qid = ENA_IO_TXQ_IDX(qid);
   1836 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   1837 
   1838 	for (i = 0; i < CLEAN_BUDGET; ++i) {
   1839 		/*
   1840 		 * If lock cannot be acquired, then deferred cleanup task was
   1841 		 * being executed and rx ring is being cleaned up in
   1842 		 * another thread.
   1843 		 */
   1844 		if (likely(ENA_RING_MTX_TRYLOCK(rx_ring) != 0)) {
   1845 			rxc = ena_rx_cleanup(rx_ring);
   1846 			ENA_RING_MTX_UNLOCK(rx_ring);
   1847 		} else {
   1848 			rxc = 0;
   1849 		}
   1850 
   1851 		/* Protection from calling ena_tx_cleanup from ena_start_xmit */
   1852 		ENA_RING_MTX_LOCK(tx_ring);
   1853 		txc = ena_tx_cleanup(tx_ring);
   1854 		ENA_RING_MTX_UNLOCK(tx_ring);
   1855 
   1856 		if (unlikely((if_getdrvflags(ifp) & IFF_RUNNING) == 0))
   1857 			return 0;
   1858 
   1859 		if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
   1860 		       break;
   1861 	}
   1862 
   1863 	/* Signal that work is done and unmask interrupt */
   1864 	ena_com_update_intr_reg(&intr_reg,
   1865 	    RX_IRQ_INTERVAL,
   1866 	    TX_IRQ_INTERVAL,
   1867 	    true);
   1868 	ena_com_unmask_intr(io_cq, &intr_reg);
   1869 
   1870 	return 1;
   1871 }
   1872 
   1873 static int
   1874 ena_enable_msix(struct ena_adapter *adapter)
   1875 {
   1876 	int msix_req;
   1877 	int counts[PCI_INTR_TYPE_SIZE];
   1878 	int max_type;
   1879 
   1880 	/* Reserved the max msix vectors we might need */
   1881 	msix_req = ENA_MAX_MSIX_VEC(adapter->num_queues);
   1882 
   1883 	counts[PCI_INTR_TYPE_INTX] = 0;
   1884 	counts[PCI_INTR_TYPE_MSI] = 0;
   1885 	counts[PCI_INTR_TYPE_MSIX] = msix_req;
   1886 	max_type = PCI_INTR_TYPE_MSIX;
   1887 
   1888 	if (pci_intr_alloc(&adapter->sc_pa, &adapter->sc_intrs, counts,
   1889 	    max_type) != 0) {
   1890 		aprint_error_dev(adapter->pdev,
   1891 		    "failed to allocate interrupt\n");
   1892 		return ENOSPC;
   1893 	}
   1894 
   1895 	adapter->sc_nintrs = counts[PCI_INTR_TYPE_MSIX];
   1896 
   1897 	if (counts[PCI_INTR_TYPE_MSIX] != msix_req) {
   1898 		device_printf(adapter->pdev,
   1899 		    "Enable only %d MSI-x (out of %d), reduce "
   1900 		    "the number of queues\n", adapter->sc_nintrs, msix_req);
   1901 		adapter->num_queues = adapter->sc_nintrs - ENA_ADMIN_MSIX_VEC;
   1902 	}
   1903 
   1904 	return 0;
   1905 }
   1906 
   1907 #if 0
   1908 static void
   1909 ena_setup_io_intr(struct ena_adapter *adapter)
   1910 {
   1911 	static int last_bind_cpu = -1;
   1912 	int irq_idx;
   1913 
   1914 	for (int i = 0; i < adapter->num_queues; i++) {
   1915 		irq_idx = ENA_IO_IRQ_IDX(i);
   1916 
   1917 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
   1918 		    "%s-TxRx-%d", device_xname(adapter->pdev), i);
   1919 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
   1920 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
   1921 		adapter->irq_tbl[irq_idx].vector =
   1922 		    adapter->msix_entries[irq_idx].vector;
   1923 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
   1924 		    adapter->msix_entries[irq_idx].vector);
   1925 #ifdef	RSS
   1926 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
   1927 		    rss_getcpu(i % rss_getnumbuckets());
   1928 #else
   1929 		/*
   1930 		 * We still want to bind rings to the corresponding cpu
   1931 		 * using something similar to the RSS round-robin technique.
   1932 		 */
   1933 		if (unlikely(last_bind_cpu < 0))
   1934 			last_bind_cpu = CPU_FIRST();
   1935 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
   1936 		    last_bind_cpu;
   1937 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
   1938 #endif
   1939 	}
   1940 }
   1941 #endif
   1942 
   1943 static int
   1944 ena_request_mgmnt_irq(struct ena_adapter *adapter)
   1945 {
   1946 	const char *intrstr;
   1947 	char intrbuf[PCI_INTRSTR_LEN];
   1948 	char intr_xname[INTRDEVNAMEBUF];
   1949 	pci_chipset_tag_t pc = adapter->sc_pa.pa_pc;
   1950 	const int irq_slot = ENA_MGMNT_IRQ_IDX;
   1951 
   1952 	KASSERT(adapter->sc_intrs != NULL);
   1953 	KASSERT(adapter->sc_ihs[irq_slot] == NULL);
   1954 
   1955 	snprintf(intr_xname, sizeof(intr_xname), "%s mgmnt",
   1956 	    device_xname(adapter->pdev));
   1957 	intrstr = pci_intr_string(pc, adapter->sc_intrs[irq_slot],
   1958 	    intrbuf, sizeof(intrbuf));
   1959 
   1960 	adapter->sc_ihs[irq_slot] = pci_intr_establish_xname(
   1961 	    pc, adapter->sc_intrs[irq_slot],
   1962 	    IPL_NET, ena_intr_msix_mgmnt, adapter, intr_xname);
   1963 
   1964 	if (adapter->sc_ihs[irq_slot] == NULL) {
   1965 		device_printf(adapter->pdev, "failed to register "
   1966 		    "interrupt handler for MGMNT irq %s\n",
   1967 		    intrstr);
   1968 		return ENOMEM;
   1969 	}
   1970 
   1971 	aprint_normal_dev(adapter->pdev,
   1972 	    "for MGMNT interrupting at %s\n", intrstr);
   1973 
   1974 	return 0;
   1975 }
   1976 
   1977 static int
   1978 ena_request_io_irq(struct ena_adapter *adapter)
   1979 {
   1980 	const char *intrstr;
   1981 	char intrbuf[PCI_INTRSTR_LEN];
   1982 	char intr_xname[INTRDEVNAMEBUF];
   1983 	pci_chipset_tag_t pc = adapter->sc_pa.pa_pc;
   1984 	const int irq_off = ENA_IO_IRQ_FIRST_IDX;
   1985 	void *vih;
   1986 	kcpuset_t *affinity;
   1987 	int i;
   1988 
   1989 	KASSERT(adapter->sc_intrs != NULL);
   1990 
   1991 	kcpuset_create(&affinity, false);
   1992 
   1993 	for (i = 0; i < adapter->num_queues; i++) {
   1994 		int irq_slot = i + irq_off;
   1995 		int affinity_to = (irq_slot) % ncpu;
   1996 
   1997 		KASSERT((void *)adapter->sc_intrs[irq_slot] != NULL);
   1998 		KASSERT(adapter->sc_ihs[irq_slot] == NULL);
   1999 
   2000 		snprintf(intr_xname, sizeof(intr_xname), "%s ioq%d",
   2001 		    device_xname(adapter->pdev), i);
   2002 		intrstr = pci_intr_string(pc, adapter->sc_intrs[irq_slot],
   2003 		    intrbuf, sizeof(intrbuf));
   2004 
   2005 		vih = pci_intr_establish_xname(adapter->sc_pa.pa_pc,
   2006 		    adapter->sc_intrs[irq_slot], IPL_NET,
   2007 		    ena_handle_msix, &adapter->que[i], intr_xname);
   2008 
   2009 		if (adapter->sc_ihs[ENA_MGMNT_IRQ_IDX] == NULL) {
   2010 			device_printf(adapter->pdev, "failed to register "
   2011 			    "interrupt handler for IO queue %d irq %s\n",
   2012 			    i, intrstr);
   2013 			goto err;
   2014 		}
   2015 
   2016 		kcpuset_zero(affinity);
   2017 		/* Round-robin affinity */
   2018 		kcpuset_set(affinity, affinity_to);
   2019 		int error = interrupt_distribute(vih, affinity, NULL);
   2020 		if (error == 0) {
   2021 			aprint_normal_dev(adapter->pdev,
   2022 			    "for IO queue %d interrupting at %s"
   2023 			    " affinity to %u\n", i, intrstr, affinity_to);
   2024 		} else {
   2025 			aprint_normal_dev(adapter->pdev,
   2026 			    "for IO queue %d interrupting at %s\n", i, intrstr);
   2027 		}
   2028 
   2029 		adapter->sc_ihs[irq_slot] = vih;
   2030 
   2031 #ifdef	RSS
   2032 		ena_trace(ENA_INFO, "queue %d - RSS bucket %d\n",
   2033 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
   2034 #else
   2035 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
   2036 		    i - ENA_IO_IRQ_FIRST_IDX, affinity_to);
   2037 #endif
   2038 	}
   2039 
   2040 	kcpuset_destroy(affinity);
   2041 	return 0;
   2042 
   2043 err:
   2044 	kcpuset_destroy(affinity);
   2045 
   2046 	for (i--; i >= 0; i--) {
   2047 		int irq_slot = i + irq_off;
   2048 		KASSERT(adapter->sc_ihs[irq_slot] != NULL);
   2049 		pci_intr_disestablish(adapter->sc_pa.pa_pc, adapter->sc_ihs[i]);
   2050 		adapter->sc_ihs[i] = NULL;
   2051 	}
   2052 
   2053 	return ENOSPC;
   2054 }
   2055 
   2056 static void
   2057 ena_free_mgmnt_irq(struct ena_adapter *adapter)
   2058 {
   2059 	const int irq_slot = ENA_MGMNT_IRQ_IDX;
   2060 
   2061 	if (adapter->sc_ihs[irq_slot]) {
   2062 		pci_intr_disestablish(adapter->sc_pa.pa_pc,
   2063 		    adapter->sc_ihs[irq_slot]);
   2064 		adapter->sc_ihs[irq_slot] = NULL;
   2065 	}
   2066 }
   2067 
   2068 static void
   2069 ena_free_io_irq(struct ena_adapter *adapter)
   2070 {
   2071 	const int irq_off = ENA_IO_IRQ_FIRST_IDX;
   2072 
   2073 	for (int i = 0; i < adapter->num_queues; i++) {
   2074 		int irq_slot = i + irq_off;
   2075 
   2076 		if (adapter->sc_ihs[irq_slot]) {
   2077 			pci_intr_disestablish(adapter->sc_pa.pa_pc,
   2078 			    adapter->sc_ihs[i]);
   2079 			adapter->sc_ihs[i] = NULL;
   2080 		}
   2081 	}
   2082 }
   2083 
   2084 static void
   2085 ena_free_irqs(struct ena_adapter* adapter)
   2086 {
   2087 
   2088 	ena_free_io_irq(adapter);
   2089 	ena_free_mgmnt_irq(adapter);
   2090 	ena_disable_msix(adapter);
   2091 }
   2092 
   2093 static void
   2094 ena_disable_msix(struct ena_adapter *adapter)
   2095 {
   2096 	pci_intr_release(adapter->sc_pa.pa_pc, adapter->sc_intrs,
   2097 	    adapter->sc_nintrs);
   2098 }
   2099 
   2100 static void
   2101 ena_unmask_all_io_irqs(struct ena_adapter *adapter)
   2102 {
   2103 	struct ena_com_io_cq* io_cq;
   2104 	struct ena_eth_io_intr_reg intr_reg;
   2105 	uint16_t ena_qid;
   2106 	int i;
   2107 
   2108 	/* Unmask interrupts for all queues */
   2109 	for (i = 0; i < adapter->num_queues; i++) {
   2110 		ena_qid = ENA_IO_TXQ_IDX(i);
   2111 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   2112 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
   2113 		ena_com_unmask_intr(io_cq, &intr_reg);
   2114 	}
   2115 }
   2116 
   2117 /* Configure the Rx forwarding */
   2118 static int
   2119 ena_rss_configure(struct ena_adapter *adapter)
   2120 {
   2121 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   2122 	int rc;
   2123 
   2124 	/* Set indirect table */
   2125 	rc = ena_com_indirect_table_set(ena_dev);
   2126 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
   2127 		return (rc);
   2128 
   2129 	/* Configure hash function (if supported) */
   2130 	rc = ena_com_set_hash_function(ena_dev);
   2131 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
   2132 		return (rc);
   2133 
   2134 	/* Configure hash inputs (if supported) */
   2135 	rc = ena_com_set_hash_ctrl(ena_dev);
   2136 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
   2137 		return (rc);
   2138 
   2139 	return (0);
   2140 }
   2141 
   2142 static int
   2143 ena_up_complete(struct ena_adapter *adapter)
   2144 {
   2145 	int rc;
   2146 
   2147 	if (likely(adapter->rss_support)) {
   2148 		rc = ena_rss_configure(adapter);
   2149 		if (rc != 0)
   2150 			return (rc);
   2151 	}
   2152 
   2153 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
   2154 	if (unlikely(rc != 0))
   2155 		return (rc);
   2156 
   2157 	ena_refill_all_rx_bufs(adapter);
   2158 	ena_reset_counters((struct evcnt *)&adapter->hw_stats,
   2159 	    sizeof(adapter->hw_stats));
   2160 
   2161 	return (0);
   2162 }
   2163 
   2164 static int
   2165 ena_up(struct ena_adapter *adapter)
   2166 {
   2167 	int rc = 0;
   2168 
   2169 #if 0
   2170 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
   2171 		device_printf(adapter->pdev, "device is not attached!\n");
   2172 		return (ENXIO);
   2173 	}
   2174 #endif
   2175 
   2176 	if (unlikely(!adapter->running)) {
   2177 		device_printf(adapter->pdev, "device is not running!\n");
   2178 		return (ENXIO);
   2179 	}
   2180 
   2181 	if (!adapter->up) {
   2182 		device_printf(adapter->pdev, "device is going UP\n");
   2183 
   2184 		/* setup interrupts for IO queues */
   2185 		rc = ena_request_io_irq(adapter);
   2186 		if (unlikely(rc != 0)) {
   2187 			ena_trace(ENA_ALERT, "err_req_irq");
   2188 			goto err_req_irq;
   2189 		}
   2190 
   2191 		/* allocate transmit descriptors */
   2192 		rc = ena_setup_all_tx_resources(adapter);
   2193 		if (unlikely(rc != 0)) {
   2194 			ena_trace(ENA_ALERT, "err_setup_tx");
   2195 			goto err_setup_tx;
   2196 		}
   2197 
   2198 		/* allocate receive descriptors */
   2199 		rc = ena_setup_all_rx_resources(adapter);
   2200 		if (unlikely(rc != 0)) {
   2201 			ena_trace(ENA_ALERT, "err_setup_rx");
   2202 			goto err_setup_rx;
   2203 		}
   2204 
   2205 		/* create IO queues for Rx & Tx */
   2206 		rc = ena_create_io_queues(adapter);
   2207 		if (unlikely(rc != 0)) {
   2208 			ena_trace(ENA_ALERT,
   2209 			    "create IO queues failed");
   2210 			goto err_io_que;
   2211 		}
   2212 
   2213 		if (unlikely(adapter->link_status))
   2214 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
   2215 
   2216 		rc = ena_up_complete(adapter);
   2217 		if (unlikely(rc != 0))
   2218 			goto err_up_complete;
   2219 
   2220 		counter_u64_add(adapter->dev_stats.interface_up, 1);
   2221 
   2222 		ena_update_hwassist(adapter);
   2223 
   2224 		if_setdrvflagbits(adapter->ifp, IFF_RUNNING,
   2225 		    IFF_OACTIVE);
   2226 
   2227 		callout_reset(&adapter->timer_service, hz,
   2228 		    ena_timer_service, (void *)adapter);
   2229 
   2230 		adapter->up = true;
   2231 
   2232 		ena_unmask_all_io_irqs(adapter);
   2233 	}
   2234 
   2235 	return (0);
   2236 
   2237 err_up_complete:
   2238 	ena_destroy_all_io_queues(adapter);
   2239 err_io_que:
   2240 	ena_free_all_rx_resources(adapter);
   2241 err_setup_rx:
   2242 	ena_free_all_tx_resources(adapter);
   2243 err_setup_tx:
   2244 	ena_free_io_irq(adapter);
   2245 err_req_irq:
   2246 	return (rc);
   2247 }
   2248 
   2249 #if 0
   2250 static uint64_t
   2251 ena_get_counter(struct ifnet *ifp, ift_counter cnt)
   2252 {
   2253 	struct ena_adapter *adapter;
   2254 	struct ena_hw_stats *stats;
   2255 
   2256 	adapter = if_getsoftc(ifp);
   2257 	stats = &adapter->hw_stats;
   2258 
   2259 	switch (cnt) {
   2260 	case IFCOUNTER_IPACKETS:
   2261 		return (counter_u64_fetch(stats->rx_packets));
   2262 	case IFCOUNTER_OPACKETS:
   2263 		return (counter_u64_fetch(stats->tx_packets));
   2264 	case IFCOUNTER_IBYTES:
   2265 		return (counter_u64_fetch(stats->rx_bytes));
   2266 	case IFCOUNTER_OBYTES:
   2267 		return (counter_u64_fetch(stats->tx_bytes));
   2268 	case IFCOUNTER_IQDROPS:
   2269 		return (counter_u64_fetch(stats->rx_drops));
   2270 	default:
   2271 		return (if_get_counter_default(ifp, cnt));
   2272 	}
   2273 }
   2274 #endif
   2275 
   2276 static int
   2277 ena_media_change(struct ifnet *ifp)
   2278 {
   2279 	/* Media Change is not supported by firmware */
   2280 	return (0);
   2281 }
   2282 
   2283 static void
   2284 ena_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   2285 {
   2286 	struct ena_adapter *adapter = if_getsoftc(ifp);
   2287 	ena_trace(ENA_DBG, "enter");
   2288 
   2289 	mutex_enter(&adapter->global_mtx);
   2290 
   2291 	ifmr->ifm_status = IFM_AVALID;
   2292 	ifmr->ifm_active = IFM_ETHER;
   2293 
   2294 	if (!adapter->link_status) {
   2295 		mutex_exit(&adapter->global_mtx);
   2296 		ena_trace(ENA_INFO, "link_status = false");
   2297 		return;
   2298 	}
   2299 
   2300 	ifmr->ifm_status |= IFM_ACTIVE;
   2301 	ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
   2302 
   2303 	mutex_exit(&adapter->global_mtx);
   2304 }
   2305 
   2306 static int
   2307 ena_init(struct ifnet *ifp)
   2308 {
   2309 	struct ena_adapter *adapter = if_getsoftc(ifp);
   2310 
   2311 	if (!adapter->up) {
   2312 		rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2313 		ena_up(adapter);
   2314 		rw_exit(&adapter->ioctl_sx);
   2315 	}
   2316 
   2317 	return 0;
   2318 }
   2319 
   2320 static int
   2321 ena_ioctl(struct ifnet *ifp, u_long command, void *data)
   2322 {
   2323 	struct ena_adapter *adapter;
   2324 	struct ifreq *ifr;
   2325 	int rc;
   2326 
   2327 	adapter = ifp->if_softc;
   2328 	ifr = (struct ifreq *)data;
   2329 
   2330 	/*
   2331 	 * Acquiring lock to prevent from running up and down routines parallel.
   2332 	 */
   2333 	rc = 0;
   2334 	switch (command) {
   2335 	case SIOCSIFMTU:
   2336 		if (ifp->if_mtu == ifr->ifr_mtu)
   2337 			break;
   2338 		rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2339 		ena_down(adapter);
   2340 
   2341 		ena_change_mtu(ifp, ifr->ifr_mtu);
   2342 
   2343 		rc = ena_up(adapter);
   2344 		rw_exit(&adapter->ioctl_sx);
   2345 		break;
   2346 
   2347 	case SIOCSIFFLAGS:
   2348 		if ((ifp->if_flags & IFF_UP) != 0) {
   2349 			if ((if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
   2350 				if ((ifp->if_flags & (IFF_PROMISC |
   2351 				    IFF_ALLMULTI)) != 0) {
   2352 					device_printf(adapter->pdev,
   2353 					    "ioctl promisc/allmulti\n");
   2354 				}
   2355 			} else {
   2356 				rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2357 				rc = ena_up(adapter);
   2358 				rw_exit(&adapter->ioctl_sx);
   2359 			}
   2360 		} else {
   2361 			if ((if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
   2362 				rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2363 				ena_down(adapter);
   2364 				rw_exit(&adapter->ioctl_sx);
   2365 			}
   2366 		}
   2367 		break;
   2368 
   2369 	case SIOCADDMULTI:
   2370 	case SIOCDELMULTI:
   2371 		break;
   2372 
   2373 	case SIOCSIFMEDIA:
   2374 	case SIOCGIFMEDIA:
   2375 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
   2376 		break;
   2377 
   2378 	case SIOCSIFCAP:
   2379 		{
   2380 			struct ifcapreq *ifcr = data;
   2381 			int reinit = 0;
   2382 
   2383 			if (ifcr->ifcr_capenable != ifp->if_capenable) {
   2384 				ifp->if_capenable = ifcr->ifcr_capenable;
   2385 				reinit = 1;
   2386 			}
   2387 
   2388 			if ((reinit != 0) &&
   2389 			    ((if_getdrvflags(ifp) & IFF_RUNNING) != 0)) {
   2390 				rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2391 				ena_down(adapter);
   2392 				rc = ena_up(adapter);
   2393 				rw_exit(&adapter->ioctl_sx);
   2394 			}
   2395 		}
   2396 
   2397 		break;
   2398 	default:
   2399 		rc = ether_ioctl(ifp, command, data);
   2400 		break;
   2401 	}
   2402 
   2403 	return (rc);
   2404 }
   2405 
   2406 static int
   2407 ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
   2408 {
   2409 	int caps = 0;
   2410 
   2411 	if ((feat->offload.tx &
   2412 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
   2413 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
   2414 		ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
   2415 		caps |= IFCAP_CSUM_IPv4_Tx;
   2416 
   2417 	if ((feat->offload.tx &
   2418 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
   2419 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
   2420 		caps |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
   2421 
   2422 	if ((feat->offload.tx &
   2423 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
   2424 		caps |= IFCAP_TSOv4;
   2425 
   2426 	if ((feat->offload.tx &
   2427 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
   2428 		caps |= IFCAP_TSOv6;
   2429 
   2430 	if ((feat->offload.rx_supported &
   2431 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
   2432 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
   2433 		caps |= IFCAP_CSUM_IPv4_Rx;
   2434 
   2435 	if ((feat->offload.rx_supported &
   2436 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
   2437 		caps |= IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   2438 
   2439 	caps |= IFCAP_LRO;
   2440 
   2441 	return (caps);
   2442 }
   2443 
   2444 static void
   2445 ena_update_host_info(struct ena_admin_host_info *host_info, struct ifnet *ifp)
   2446 {
   2447 
   2448 	host_info->supported_network_features[0] =
   2449 	    (uint32_t)if_getcapabilities(ifp);
   2450 }
   2451 
   2452 static void
   2453 ena_update_hwassist(struct ena_adapter *adapter)
   2454 {
   2455 	struct ifnet *ifp = adapter->ifp;
   2456 	uint32_t feat = adapter->tx_offload_cap;
   2457 	int cap = if_getcapenable(ifp);
   2458 	int flags = 0;
   2459 
   2460 	if_clearhwassist(ifp);
   2461 
   2462 	if ((cap & (IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx))
   2463 	    != 0) {
   2464 		if ((feat &
   2465 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
   2466 			flags |= M_CSUM_IPv4;
   2467 		if ((feat &
   2468 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
   2469 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
   2470 			flags |= M_CSUM_TCPv4 | M_CSUM_UDPv4;
   2471 	}
   2472 
   2473 	if ((cap & IFCAP_CSUM_TCPv6_Tx) != 0)
   2474 		flags |= M_CSUM_TCPv6;
   2475 
   2476 	if ((cap & IFCAP_CSUM_UDPv6_Tx) != 0)
   2477 		flags |= M_CSUM_UDPv6;
   2478 
   2479 	if ((cap & IFCAP_TSOv4) != 0)
   2480 		flags |= M_CSUM_TSOv4;
   2481 
   2482 	if ((cap & IFCAP_TSOv6) != 0)
   2483 		flags |= M_CSUM_TSOv6;
   2484 
   2485 	if_sethwassistbits(ifp, flags, 0);
   2486 }
   2487 
   2488 static int
   2489 ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
   2490     struct ena_com_dev_get_features_ctx *feat)
   2491 {
   2492 	struct ifnet *ifp;
   2493 	int caps = 0;
   2494 
   2495 	ifp = adapter->ifp = &adapter->sc_ec.ec_if;
   2496 	if (unlikely(ifp == NULL)) {
   2497 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
   2498 		return (ENXIO);
   2499 	}
   2500 	if_initname(ifp, device_xname(pdev), device_unit(pdev));
   2501 	if_setdev(ifp, pdev);
   2502 	if_setsoftc(ifp, adapter);
   2503 
   2504 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
   2505 	if_setinitfn(ifp, ena_init);
   2506 	if_settransmitfn(ifp, ena_mq_start);
   2507 #if 0
   2508 	if_setqflushfn(ifp, ena_qflush);
   2509 #endif
   2510 	if_setioctlfn(ifp, ena_ioctl);
   2511 #if 0
   2512 	if_setgetcounterfn(ifp, ena_get_counter);
   2513 #endif
   2514 
   2515 	if_setsendqlen(ifp, adapter->tx_ring_size);
   2516 	if_setsendqready(ifp);
   2517 	if_setmtu(ifp, ETHERMTU);
   2518 	if_setbaudrate(ifp, 0);
   2519 	/* Zeroize capabilities... */
   2520 	if_setcapabilities(ifp, 0);
   2521 	if_setcapenable(ifp, 0);
   2522 	/* check hardware support */
   2523 	caps = ena_get_dev_offloads(feat);
   2524 	/* ... and set them */
   2525 	if_setcapabilitiesbit(ifp, caps, 0);
   2526 	adapter->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   2527 
   2528 #if 0
   2529 	/* TSO parameters */
   2530 	/* XXX no limits on NetBSD, guarded by virtue of dmamap load failing */
   2531 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
   2532 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
   2533 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
   2534 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
   2535 #endif
   2536 
   2537 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
   2538 	if_setcapenable(ifp, if_getcapabilities(ifp));
   2539 
   2540 	/*
   2541 	 * Specify the media types supported by this adapter and register
   2542 	 * callbacks to update media and link information
   2543 	 */
   2544 	ifmedia_init(&adapter->media, IFM_IMASK,
   2545 	    ena_media_change, ena_media_status);
   2546 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   2547 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   2548 
   2549 	ether_ifattach(ifp, adapter->mac_addr);
   2550 
   2551 	return (0);
   2552 }
   2553 
   2554 static void
   2555 ena_down(struct ena_adapter *adapter)
   2556 {
   2557 	int rc;
   2558 
   2559 	if (adapter->up) {
   2560 		device_printf(adapter->pdev, "device is going DOWN\n");
   2561 
   2562 		callout_halt(&adapter->timer_service, &adapter->global_mtx);
   2563 
   2564 		adapter->up = false;
   2565 		if_setdrvflagbits(adapter->ifp, IFF_OACTIVE,
   2566 		    IFF_RUNNING);
   2567 
   2568 		ena_free_io_irq(adapter);
   2569 
   2570 		if (adapter->trigger_reset) {
   2571 			rc = ena_com_dev_reset(adapter->ena_dev,
   2572 			    adapter->reset_reason);
   2573 			if (unlikely(rc != 0))
   2574 				device_printf(adapter->pdev,
   2575 				    "Device reset failed\n");
   2576 		}
   2577 
   2578 		ena_destroy_all_io_queues(adapter);
   2579 
   2580 		ena_free_all_tx_bufs(adapter);
   2581 		ena_free_all_rx_bufs(adapter);
   2582 		ena_free_all_tx_resources(adapter);
   2583 		ena_free_all_rx_resources(adapter);
   2584 
   2585 		counter_u64_add(adapter->dev_stats.interface_down, 1);
   2586 	}
   2587 }
   2588 
   2589 static void
   2590 ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
   2591 {
   2592 	struct ena_com_tx_meta *ena_meta;
   2593 	struct ether_vlan_header *eh;
   2594 	u32 mss;
   2595 	bool offload;
   2596 	uint16_t etype;
   2597 	int ehdrlen;
   2598 	struct ip *ip;
   2599 	int iphlen;
   2600 	struct tcphdr *th;
   2601 
   2602 	offload = false;
   2603 	ena_meta = &ena_tx_ctx->ena_meta;
   2604 
   2605 #if 0
   2606 	u32 mss = mbuf->m_pkthdr.tso_segsz;
   2607 
   2608 	if (mss != 0)
   2609 		offload = true;
   2610 #else
   2611 	mss = mbuf->m_pkthdr.len;	/* XXX don't have tso_segsz */
   2612 #endif
   2613 
   2614 	if ((mbuf->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0)
   2615 		offload = true;
   2616 
   2617 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
   2618 		offload = true;
   2619 
   2620 	if (!offload) {
   2621 		ena_tx_ctx->meta_valid = 0;
   2622 		return;
   2623 	}
   2624 
   2625 	/* Determine where frame payload starts. */
   2626 	eh = mtod(mbuf, struct ether_vlan_header *);
   2627 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   2628 		etype = ntohs(eh->evl_proto);
   2629 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   2630 	} else {
   2631 		etype = htons(eh->evl_encap_proto);
   2632 		ehdrlen = ETHER_HDR_LEN;
   2633 	}
   2634 
   2635 	ip = (struct ip *)(mbuf->m_data + ehdrlen);
   2636 	iphlen = ip->ip_hl << 2;
   2637 	th = (struct tcphdr *)((vaddr_t)ip + iphlen);
   2638 
   2639 	if ((mbuf->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
   2640 		ena_tx_ctx->l3_csum_enable = 1;
   2641 	}
   2642 	if ((mbuf->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0) {
   2643 		ena_tx_ctx->tso_enable = 1;
   2644 		ena_meta->l4_hdr_len = (th->th_off);
   2645 	}
   2646 
   2647 	switch (etype) {
   2648 	case ETHERTYPE_IP:
   2649 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
   2650 		if ((ip->ip_off & htons(IP_DF)) != 0)
   2651 			ena_tx_ctx->df = 1;
   2652 		break;
   2653 	case ETHERTYPE_IPV6:
   2654 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
   2655 
   2656 	default:
   2657 		break;
   2658 	}
   2659 
   2660 	if (ip->ip_p == IPPROTO_TCP) {
   2661 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
   2662 		if ((mbuf->m_pkthdr.csum_flags &
   2663 		    (M_CSUM_TCPv4 | M_CSUM_TCPv6)) != 0)
   2664 			ena_tx_ctx->l4_csum_enable = 1;
   2665 		else
   2666 			ena_tx_ctx->l4_csum_enable = 0;
   2667 	} else if (ip->ip_p == IPPROTO_UDP) {
   2668 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
   2669 		if ((mbuf->m_pkthdr.csum_flags &
   2670 		    (M_CSUM_UDPv4 | M_CSUM_UDPv6)) != 0)
   2671 			ena_tx_ctx->l4_csum_enable = 1;
   2672 		else
   2673 			ena_tx_ctx->l4_csum_enable = 0;
   2674 	} else {
   2675 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
   2676 		ena_tx_ctx->l4_csum_enable = 0;
   2677 	}
   2678 
   2679 	ena_meta->mss = mss;
   2680 	ena_meta->l3_hdr_len = iphlen;
   2681 	ena_meta->l3_hdr_offset = ehdrlen;
   2682 	ena_tx_ctx->meta_valid = 1;
   2683 }
   2684 
   2685 static int
   2686 ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
   2687 {
   2688 	struct ena_adapter *adapter;
   2689 	struct mbuf *collapsed_mbuf;
   2690 	int num_frags;
   2691 
   2692 	adapter = tx_ring->adapter;
   2693 	num_frags = ena_mbuf_count(*mbuf);
   2694 
   2695 	/* One segment must be reserved for configuration descriptor. */
   2696 	if (num_frags < adapter->max_tx_sgl_size)
   2697 		return (0);
   2698 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
   2699 
   2700 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
   2701 	    adapter->max_tx_sgl_size - 1);
   2702 	if (unlikely(collapsed_mbuf == NULL)) {
   2703 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
   2704 		return (ENOMEM);
   2705 	}
   2706 
   2707 	/* If mbuf was collapsed succesfully, original mbuf is released. */
   2708 	*mbuf = collapsed_mbuf;
   2709 
   2710 	return (0);
   2711 }
   2712 
   2713 static int
   2714 ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
   2715 {
   2716 	struct ena_adapter *adapter;
   2717 	struct ena_tx_buffer *tx_info;
   2718 	struct ena_com_tx_ctx ena_tx_ctx;
   2719 	struct ena_com_dev *ena_dev;
   2720 	struct ena_com_buf *ena_buf;
   2721 	struct ena_com_io_sq* io_sq;
   2722 	void *push_hdr;
   2723 	uint16_t next_to_use;
   2724 	uint16_t req_id;
   2725 	uint16_t ena_qid;
   2726 	uint32_t header_len;
   2727 	int i, rc;
   2728 	int nb_hw_desc;
   2729 
   2730 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
   2731 	adapter = tx_ring->que->adapter;
   2732 	ena_dev = adapter->ena_dev;
   2733 	io_sq = &ena_dev->io_sq_queues[ena_qid];
   2734 
   2735 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
   2736 	if (unlikely(rc != 0)) {
   2737 		ena_trace(ENA_WARNING,
   2738 		    "Failed to collapse mbuf! err: %d", rc);
   2739 		return (rc);
   2740 	}
   2741 
   2742 	next_to_use = tx_ring->next_to_use;
   2743 	req_id = tx_ring->free_tx_ids[next_to_use];
   2744 	tx_info = &tx_ring->tx_buffer_info[req_id];
   2745 
   2746 	tx_info->mbuf = *mbuf;
   2747 	tx_info->num_of_bufs = 0;
   2748 
   2749 	ena_buf = tx_info->bufs;
   2750 
   2751 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
   2752 
   2753 	/*
   2754 	 * header_len is just a hint for the device. Because FreeBSD is not
   2755 	 * giving us information about packet header length and it is not
   2756 	 * guaranteed that all packet headers will be in the 1st mbuf, setting
   2757 	 * header_len to 0 is making the device ignore this value and resolve
   2758 	 * header on it's own.
   2759 	 */
   2760 	header_len = 0;
   2761 	push_hdr = NULL;
   2762 
   2763 	rc = bus_dmamap_load_mbuf(adapter->sc_dmat, tx_info->map,
   2764 	    *mbuf, BUS_DMA_NOWAIT);
   2765 
   2766 	if (unlikely((rc != 0) || (tx_info->map->dm_nsegs == 0))) {
   2767 		ena_trace(ENA_WARNING,
   2768 		    "dmamap load failed! err: %d nsegs: %d", rc,
   2769 		    tx_info->map->dm_nsegs);
   2770 		counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
   2771 		tx_info->mbuf = NULL;
   2772 		if (rc == ENOMEM)
   2773 			return (ENA_COM_NO_MEM);
   2774 		else
   2775 			return (ENA_COM_INVAL);
   2776 	}
   2777 
   2778 	for (i = 0; i < tx_info->map->dm_nsegs; i++) {
   2779 		ena_buf->len = tx_info->map->dm_segs[i].ds_len;
   2780 		ena_buf->paddr = tx_info->map->dm_segs[i].ds_addr;
   2781 		ena_buf++;
   2782 	}
   2783 	tx_info->num_of_bufs = tx_info->map->dm_nsegs;
   2784 
   2785 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
   2786 	ena_tx_ctx.ena_bufs = tx_info->bufs;
   2787 	ena_tx_ctx.push_header = push_hdr;
   2788 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
   2789 	ena_tx_ctx.req_id = req_id;
   2790 	ena_tx_ctx.header_len = header_len;
   2791 
   2792 	/* Set flags and meta data */
   2793 	ena_tx_csum(&ena_tx_ctx, *mbuf);
   2794 	/* Prepare the packet's descriptors and send them to device */
   2795 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
   2796 	if (unlikely(rc != 0)) {
   2797 		device_printf(adapter->pdev, "failed to prepare tx bufs\n");
   2798 		counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
   2799 		goto dma_error;
   2800 	}
   2801 
   2802 	counter_enter();
   2803 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
   2804 	counter_u64_add_protected(tx_ring->tx_stats.bytes,
   2805 	    (*mbuf)->m_pkthdr.len);
   2806 
   2807 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
   2808 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
   2809 	    (*mbuf)->m_pkthdr.len);
   2810 	counter_exit();
   2811 
   2812 	tx_info->tx_descs = nb_hw_desc;
   2813 	getbinuptime(&tx_info->timestamp);
   2814 	tx_info->print_once = true;
   2815 
   2816 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
   2817 	    tx_ring->ring_size);
   2818 
   2819 	bus_dmamap_sync(adapter->sc_dmat, tx_info->map, 0,
   2820 	    tx_info->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2821 
   2822 	return (0);
   2823 
   2824 dma_error:
   2825 	tx_info->mbuf = NULL;
   2826 	bus_dmamap_unload(adapter->sc_dmat, tx_info->map);
   2827 
   2828 	return (rc);
   2829 }
   2830 
   2831 static void
   2832 ena_start_xmit(struct ena_ring *tx_ring)
   2833 {
   2834 	struct mbuf *mbuf;
   2835 	struct ena_adapter *adapter = tx_ring->adapter;
   2836 	struct ena_com_io_sq* io_sq;
   2837 	int ena_qid;
   2838 	int acum_pkts = 0;
   2839 	int ret = 0;
   2840 
   2841 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_RUNNING) == 0))
   2842 		return;
   2843 
   2844 	if (unlikely(!adapter->link_status))
   2845 		return;
   2846 
   2847 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
   2848 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
   2849 
   2850 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
   2851 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
   2852 		    " header csum flags %#jx",
   2853 		    mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
   2854 
   2855 		if (unlikely(!ena_com_sq_have_enough_space(io_sq,
   2856 		    ENA_TX_CLEANUP_THRESHOLD)))
   2857 			ena_tx_cleanup(tx_ring);
   2858 
   2859 		if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
   2860 			if (ret == ENA_COM_NO_MEM) {
   2861 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
   2862 			} else if (ret == ENA_COM_NO_SPACE) {
   2863 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
   2864 			} else {
   2865 				m_freem(mbuf);
   2866 				drbr_advance(adapter->ifp, tx_ring->br);
   2867 			}
   2868 
   2869 			break;
   2870 		}
   2871 
   2872 		drbr_advance(adapter->ifp, tx_ring->br);
   2873 
   2874 		if (unlikely((if_getdrvflags(adapter->ifp) &
   2875 		    IFF_RUNNING) == 0))
   2876 			return;
   2877 
   2878 		acum_pkts++;
   2879 
   2880 		/*
   2881 		 * If there's a BPF listener, bounce a copy of this frame
   2882 		 * to him.
   2883 		 */
   2884 		bpf_mtap(adapter->ifp, mbuf, BPF_D_OUT);
   2885 
   2886 		if (unlikely(acum_pkts == DB_THRESHOLD)) {
   2887 			acum_pkts = 0;
   2888 			wmb();
   2889 			/* Trigger the dma engine */
   2890 			ena_com_write_sq_doorbell(io_sq);
   2891 			counter_u64_add(tx_ring->tx_stats.doorbells, 1);
   2892 		}
   2893 
   2894 	}
   2895 
   2896 	if (likely(acum_pkts != 0)) {
   2897 		wmb();
   2898 		/* Trigger the dma engine */
   2899 		ena_com_write_sq_doorbell(io_sq);
   2900 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
   2901 	}
   2902 
   2903 	if (!ena_com_sq_have_enough_space(io_sq, ENA_TX_CLEANUP_THRESHOLD))
   2904 		ena_tx_cleanup(tx_ring);
   2905 }
   2906 
   2907 static void
   2908 ena_deferred_mq_start(struct work *wk, void *arg)
   2909 {
   2910 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
   2911 	struct ifnet *ifp = tx_ring->adapter->ifp;
   2912 
   2913 	while (!drbr_empty(ifp, tx_ring->br) &&
   2914 	    (if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
   2915 		ENA_RING_MTX_LOCK(tx_ring);
   2916 		ena_start_xmit(tx_ring);
   2917 		ENA_RING_MTX_UNLOCK(tx_ring);
   2918 	}
   2919 }
   2920 
   2921 static int
   2922 ena_mq_start(struct ifnet *ifp, struct mbuf *m)
   2923 {
   2924 	struct ena_adapter *adapter = ifp->if_softc;
   2925 	struct ena_ring *tx_ring;
   2926 	int ret, is_drbr_empty;
   2927 	uint32_t i;
   2928 
   2929 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_RUNNING) == 0))
   2930 		return (ENODEV);
   2931 
   2932 	/* Which queue to use */
   2933 	/*
   2934 	 * If everything is setup correctly, it should be the
   2935 	 * same bucket that the current CPU we're on is.
   2936 	 * It should improve performance.
   2937 	 */
   2938 #if 0
   2939 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
   2940 #ifdef	RSS
   2941 		if (rss_hash2bucket(m->m_pkthdr.flowid,
   2942 		    M_HASHTYPE_GET(m), &i) == 0) {
   2943 			i = i % adapter->num_queues;
   2944 
   2945 		} else
   2946 #endif
   2947 		{
   2948 			i = m->m_pkthdr.flowid % adapter->num_queues;
   2949 		}
   2950 	} else {
   2951 #endif
   2952 		i = cpu_index(curcpu()) % adapter->num_queues;
   2953 #if 0
   2954 	}
   2955 #endif
   2956 	tx_ring = &adapter->tx_ring[i];
   2957 
   2958 	/* Check if drbr is empty before putting packet */
   2959 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
   2960 	ret = drbr_enqueue(ifp, tx_ring->br, m);
   2961 	if (unlikely(ret != 0)) {
   2962 		workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
   2963 		    curcpu());
   2964 		return (ret);
   2965 	}
   2966 
   2967 	if ((is_drbr_empty != 0) && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
   2968 		ena_start_xmit(tx_ring);
   2969 		ENA_RING_MTX_UNLOCK(tx_ring);
   2970 	} else {
   2971 		workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
   2972 		    curcpu());
   2973 	}
   2974 
   2975 	return (0);
   2976 }
   2977 
   2978 #if 0
   2979 static void
   2980 ena_qflush(struct ifnet *ifp)
   2981 {
   2982 	struct ena_adapter *adapter = ifp->if_softc;
   2983 	struct ena_ring *tx_ring = adapter->tx_ring;
   2984 	int i;
   2985 
   2986 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
   2987 		if (!drbr_empty(ifp, tx_ring->br)) {
   2988 			ENA_RING_MTX_LOCK(tx_ring);
   2989 			drbr_flush(ifp, tx_ring->br);
   2990 			ENA_RING_MTX_UNLOCK(tx_ring);
   2991 		}
   2992 
   2993 	if_qflush(ifp);
   2994 }
   2995 #endif
   2996 
   2997 static int
   2998 ena_calc_io_queue_num(struct pci_attach_args *pa,
   2999     struct ena_adapter *adapter,
   3000     struct ena_com_dev_get_features_ctx *get_feat_ctx)
   3001 {
   3002 	int io_sq_num, io_cq_num, io_queue_num;
   3003 
   3004 	io_sq_num = get_feat_ctx->max_queues.max_sq_num;
   3005 	io_cq_num = get_feat_ctx->max_queues.max_cq_num;
   3006 
   3007 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
   3008 	io_queue_num = min_t(int, io_queue_num, io_sq_num);
   3009 	io_queue_num = min_t(int, io_queue_num, io_cq_num);
   3010 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
   3011 	io_queue_num = min_t(int, io_queue_num,
   3012 	    pci_msix_count(pa->pa_pc, pa->pa_tag) - 1);
   3013 #ifdef	RSS
   3014 	io_queue_num = min_t(int, io_queue_num, rss_getnumbuckets());
   3015 #endif
   3016 
   3017 	return (io_queue_num);
   3018 }
   3019 
   3020 static int
   3021 ena_calc_queue_size(struct ena_adapter *adapter, uint16_t *max_tx_sgl_size,
   3022     uint16_t *max_rx_sgl_size, struct ena_com_dev_get_features_ctx *feat)
   3023 {
   3024 	uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
   3025 	uint32_t v;
   3026 	uint32_t q;
   3027 
   3028 	queue_size = min_t(uint32_t, queue_size,
   3029 	    feat->max_queues.max_cq_depth);
   3030 	queue_size = min_t(uint32_t, queue_size,
   3031 	    feat->max_queues.max_sq_depth);
   3032 
   3033 	/* round down to the nearest power of 2 */
   3034 	v = queue_size;
   3035 	while (v != 0) {
   3036 		if (powerof2(queue_size) != 0)
   3037 			break;
   3038 		v /= 2;
   3039 		q = rounddown2(queue_size, v);
   3040 		if (q != 0) {
   3041 			queue_size = q;
   3042 			break;
   3043 		}
   3044 	}
   3045 
   3046 	if (unlikely(queue_size == 0)) {
   3047 		device_printf(adapter->pdev, "Invalid queue size\n");
   3048 		return (ENA_COM_FAULT);
   3049 	}
   3050 
   3051 	*max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
   3052 	    feat->max_queues.max_packet_tx_descs);
   3053 	*max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
   3054 	    feat->max_queues.max_packet_rx_descs);
   3055 
   3056 	return (queue_size);
   3057 }
   3058 
   3059 #if 0
   3060 static int
   3061 ena_rss_init_default(struct ena_adapter *adapter)
   3062 {
   3063 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3064 	device_t dev = adapter->pdev;
   3065 	int qid, rc, i;
   3066 
   3067 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
   3068 	if (unlikely(rc != 0)) {
   3069 		device_printf(dev, "Cannot init indirect table\n");
   3070 		return (rc);
   3071 	}
   3072 
   3073 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
   3074 #ifdef	RSS
   3075 		qid = rss_get_indirection_to_bucket(i);
   3076 		qid = qid % adapter->num_queues;
   3077 #else
   3078 		qid = i % adapter->num_queues;
   3079 #endif
   3080 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
   3081 		    ENA_IO_RXQ_IDX(qid));
   3082 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
   3083 			device_printf(dev, "Cannot fill indirect table\n");
   3084 			goto err_rss_destroy;
   3085 		}
   3086 	}
   3087 
   3088 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
   3089 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
   3090 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
   3091 		device_printf(dev, "Cannot fill hash function\n");
   3092 		goto err_rss_destroy;
   3093 	}
   3094 
   3095 	rc = ena_com_set_default_hash_ctrl(ena_dev);
   3096 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
   3097 		device_printf(dev, "Cannot fill hash control\n");
   3098 		goto err_rss_destroy;
   3099 	}
   3100 
   3101 	return (0);
   3102 
   3103 err_rss_destroy:
   3104 	ena_com_rss_destroy(ena_dev);
   3105 	return (rc);
   3106 }
   3107 
   3108 static void
   3109 ena_rss_init_default_deferred(void *arg)
   3110 {
   3111 	struct ena_adapter *adapter;
   3112 	devclass_t dc;
   3113 	int max;
   3114 	int rc;
   3115 
   3116 	dc = devclass_find("ena");
   3117 	if (unlikely(dc == NULL)) {
   3118 		ena_trace(ENA_ALERT, "No devclass ena\n");
   3119 		return;
   3120 	}
   3121 
   3122 	max = devclass_get_maxunit(dc);
   3123 	while (max-- >= 0) {
   3124 		adapter = devclass_get_softc(dc, max);
   3125 		if (adapter != NULL) {
   3126 			rc = ena_rss_init_default(adapter);
   3127 			adapter->rss_support = true;
   3128 			if (unlikely(rc != 0)) {
   3129 				device_printf(adapter->pdev,
   3130 				    "WARNING: RSS was not properly initialized,"
   3131 				    " it will affect bandwidth\n");
   3132 				adapter->rss_support = false;
   3133 			}
   3134 		}
   3135 	}
   3136 }
   3137 SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
   3138 #endif
   3139 
   3140 static void
   3141 ena_config_host_info(struct ena_com_dev *ena_dev)
   3142 {
   3143 	struct ena_admin_host_info *host_info;
   3144 	int rc;
   3145 
   3146 	/* Allocate only the host info */
   3147 	rc = ena_com_allocate_host_info(ena_dev);
   3148 	if (unlikely(rc != 0)) {
   3149 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
   3150 		return;
   3151 	}
   3152 
   3153 	host_info = ena_dev->host_attr.host_info;
   3154 
   3155 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
   3156 	host_info->kernel_ver = osreldate;
   3157 
   3158 	snprintf(host_info->kernel_ver_str, sizeof(host_info->kernel_ver_str),
   3159 	    "%d", osreldate);
   3160 	host_info->os_dist = 0;
   3161 	strncpy(host_info->os_dist_str, osrelease,
   3162 	    sizeof(host_info->os_dist_str) - 1);
   3163 
   3164 	host_info->driver_version =
   3165 		(DRV_MODULE_VER_MAJOR) |
   3166 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
   3167 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
   3168 
   3169 	rc = ena_com_set_host_attributes(ena_dev);
   3170 	if (unlikely(rc != 0)) {
   3171 		if (rc == EOPNOTSUPP)
   3172 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
   3173 		else
   3174 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
   3175 
   3176 		goto err;
   3177 	}
   3178 
   3179 	return;
   3180 
   3181 err:
   3182 	ena_com_delete_host_info(ena_dev);
   3183 }
   3184 
   3185 static int
   3186 ena_device_init(struct ena_adapter *adapter, device_t pdev,
   3187     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
   3188 {
   3189 	struct ena_com_dev* ena_dev = adapter->ena_dev;
   3190 	bool readless_supported;
   3191 	uint32_t aenq_groups;
   3192 	int dma_width;
   3193 	int rc;
   3194 
   3195 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
   3196 	if (unlikely(rc != 0)) {
   3197 		device_printf(pdev, "failed to init mmio read less\n");
   3198 		return (rc);
   3199 	}
   3200 
   3201 	/*
   3202 	 * The PCIe configuration space revision id indicate if mmio reg
   3203 	 * read is disabled
   3204 	 */
   3205 	const int rev = PCI_REVISION(adapter->sc_pa.pa_class);
   3206 	readless_supported = ((rev & ENA_MMIO_DISABLE_REG_READ) == 0);
   3207 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
   3208 
   3209 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
   3210 	if (unlikely(rc != 0)) {
   3211 		device_printf(pdev, "Can not reset device\n");
   3212 		goto err_mmio_read_less;
   3213 	}
   3214 
   3215 	rc = ena_com_validate_version(ena_dev);
   3216 	if (unlikely(rc != 0)) {
   3217 		device_printf(pdev, "device version is too low\n");
   3218 		goto err_mmio_read_less;
   3219 	}
   3220 
   3221 	dma_width = ena_com_get_dma_width(ena_dev);
   3222 	if (unlikely(dma_width < 0)) {
   3223 		device_printf(pdev, "Invalid dma width value %d", dma_width);
   3224 		rc = dma_width;
   3225 		goto err_mmio_read_less;
   3226 	}
   3227 	adapter->dma_width = dma_width;
   3228 
   3229 	/* ENA admin level init */
   3230 	rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
   3231 	if (unlikely(rc != 0)) {
   3232 		device_printf(pdev,
   3233 		    "Can not initialize ena admin queue with device\n");
   3234 		goto err_mmio_read_less;
   3235 	}
   3236 
   3237 	/*
   3238 	 * To enable the msix interrupts the driver needs to know the number
   3239 	 * of queues. So the driver uses polling mode to retrieve this
   3240 	 * information
   3241 	 */
   3242 	ena_com_set_admin_polling_mode(ena_dev, true);
   3243 
   3244 	ena_config_host_info(ena_dev);
   3245 
   3246 	/* Get Device Attributes */
   3247 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
   3248 	if (unlikely(rc != 0)) {
   3249 		device_printf(pdev,
   3250 		    "Cannot get attribute for ena device rc: %d\n", rc);
   3251 		goto err_admin_init;
   3252 	}
   3253 
   3254 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | BIT(ENA_ADMIN_KEEP_ALIVE);
   3255 
   3256 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
   3257 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
   3258 	if (unlikely(rc != 0)) {
   3259 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
   3260 		goto err_admin_init;
   3261 	}
   3262 
   3263 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
   3264 
   3265 	return (0);
   3266 
   3267 err_admin_init:
   3268 	ena_com_delete_host_info(ena_dev);
   3269 	ena_com_admin_destroy(ena_dev);
   3270 err_mmio_read_less:
   3271 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3272 
   3273 	return (rc);
   3274 }
   3275 
   3276 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
   3277     int io_vectors)
   3278 {
   3279 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3280 	int rc;
   3281 
   3282 	rc = ena_enable_msix(adapter);
   3283 	if (unlikely(rc != 0)) {
   3284 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
   3285 		return (rc);
   3286 	}
   3287 
   3288 	rc = ena_request_mgmnt_irq(adapter);
   3289 	if (unlikely(rc != 0)) {
   3290 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
   3291 		goto err_disable_msix;
   3292 	}
   3293 
   3294 	ena_com_set_admin_polling_mode(ena_dev, false);
   3295 
   3296 	ena_com_admin_aenq_enable(ena_dev);
   3297 
   3298 	return (0);
   3299 
   3300 err_disable_msix:
   3301 	ena_disable_msix(adapter);
   3302 
   3303 	return (rc);
   3304 }
   3305 
   3306 /* Function called on ENA_ADMIN_KEEP_ALIVE event */
   3307 static void ena_keep_alive_wd(void *adapter_data,
   3308     struct ena_admin_aenq_entry *aenq_e)
   3309 {
   3310 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
   3311 	struct ena_admin_aenq_keep_alive_desc *desc;
   3312 	sbintime_t stime;
   3313 	uint64_t rx_drops;
   3314 
   3315 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
   3316 
   3317 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
   3318 	counter_u64_zero(adapter->hw_stats.rx_drops);
   3319 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
   3320 
   3321 	stime = getsbinuptime();
   3322 	(void) atomic_swap_64(&adapter->keep_alive_timestamp, stime);
   3323 }
   3324 
   3325 /* Check for keep alive expiration */
   3326 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
   3327 {
   3328 	sbintime_t timestamp, time;
   3329 
   3330 	if (adapter->wd_active == 0)
   3331 		return;
   3332 
   3333 	if (likely(adapter->keep_alive_timeout == 0))
   3334 		return;
   3335 
   3336 	/* FreeBSD uses atomic_load_acq_64() in place of the membar + read */
   3337 	membar_sync();
   3338 	timestamp = adapter->keep_alive_timestamp;
   3339 
   3340 	time = getsbinuptime() - timestamp;
   3341 	if (unlikely(time > adapter->keep_alive_timeout)) {
   3342 		device_printf(adapter->pdev,
   3343 		    "Keep alive watchdog timeout.\n");
   3344 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
   3345 		adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
   3346 		adapter->trigger_reset = true;
   3347 	}
   3348 }
   3349 
   3350 /* Check if admin queue is enabled */
   3351 static void check_for_admin_com_state(struct ena_adapter *adapter)
   3352 {
   3353 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
   3354 	    false)) {
   3355 		device_printf(adapter->pdev,
   3356 		    "ENA admin queue is not in running state!\n");
   3357 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
   3358 		adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
   3359 		adapter->trigger_reset = true;
   3360 	}
   3361 }
   3362 
   3363 static int
   3364 check_missing_comp_in_queue(struct ena_adapter *adapter,
   3365     struct ena_ring *tx_ring)
   3366 {
   3367 	struct bintime curtime, time;
   3368 	struct ena_tx_buffer *tx_buf;
   3369 	uint32_t missed_tx = 0;
   3370 	int i;
   3371 
   3372 	getbinuptime(&curtime);
   3373 
   3374 	for (i = 0; i < tx_ring->ring_size; i++) {
   3375 		tx_buf = &tx_ring->tx_buffer_info[i];
   3376 
   3377 		if (bintime_isset(&tx_buf->timestamp) == 0)
   3378 			continue;
   3379 
   3380 		time = curtime;
   3381 		bintime_sub(&time, &tx_buf->timestamp);
   3382 
   3383 		/* Check again if packet is still waiting */
   3384 		if (unlikely(bttosbt(time) > adapter->missing_tx_timeout)) {
   3385 
   3386 			if (!tx_buf->print_once)
   3387 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
   3388 				    "completed on time, qid %d, index %d.\n",
   3389 				    tx_ring->qid, i);
   3390 
   3391 			tx_buf->print_once = true;
   3392 			missed_tx++;
   3393 			counter_u64_add(tx_ring->tx_stats.missing_tx_comp, 1);
   3394 
   3395 			if (unlikely(missed_tx >
   3396 			    adapter->missing_tx_threshold)) {
   3397 				device_printf(adapter->pdev,
   3398 				    "The number of lost tx completion "
   3399 				    "is above the threshold (%d > %d). "
   3400 				    "Reset the device\n",
   3401 				    missed_tx, adapter->missing_tx_threshold);
   3402 				adapter->reset_reason =
   3403 				    ENA_REGS_RESET_MISS_TX_CMPL;
   3404 				adapter->trigger_reset = true;
   3405 				return (EIO);
   3406 			}
   3407 		}
   3408 	}
   3409 
   3410 	return (0);
   3411 }
   3412 
   3413 /*
   3414  * Check for TX which were not completed on time.
   3415  * Timeout is defined by "missing_tx_timeout".
   3416  * Reset will be performed if number of incompleted
   3417  * transactions exceeds "missing_tx_threshold".
   3418  */
   3419 static void
   3420 check_for_missing_tx_completions(struct ena_adapter *adapter)
   3421 {
   3422 	struct ena_ring *tx_ring;
   3423 	int i, budget, rc;
   3424 
   3425 	/* Make sure the driver doesn't turn the device in other process */
   3426 	rmb();
   3427 
   3428 	if (!adapter->up)
   3429 		return;
   3430 
   3431 	if (adapter->trigger_reset)
   3432 		return;
   3433 
   3434 	if (adapter->missing_tx_timeout == 0)
   3435 		return;
   3436 
   3437 	budget = adapter->missing_tx_max_queues;
   3438 
   3439 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
   3440 		tx_ring = &adapter->tx_ring[i];
   3441 
   3442 		rc = check_missing_comp_in_queue(adapter, tx_ring);
   3443 		if (unlikely(rc != 0))
   3444 			return;
   3445 
   3446 		budget--;
   3447 		if (budget == 0) {
   3448 			i++;
   3449 			break;
   3450 		}
   3451 	}
   3452 
   3453 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
   3454 }
   3455 
   3456 /* trigger deferred rx cleanup after 2 consecutive detections */
   3457 #define EMPTY_RX_REFILL 2
   3458 /* For the rare case where the device runs out of Rx descriptors and the
   3459  * msix handler failed to refill new Rx descriptors (due to a lack of memory
   3460  * for example).
   3461  * This case will lead to a deadlock:
   3462  * The device won't send interrupts since all the new Rx packets will be dropped
   3463  * The msix handler won't allocate new Rx descriptors so the device won't be
   3464  * able to send new packets.
   3465  *
   3466  * When such a situation is detected - execute rx cleanup task in another thread
   3467  */
   3468 static void
   3469 check_for_empty_rx_ring(struct ena_adapter *adapter)
   3470 {
   3471 	struct ena_ring *rx_ring;
   3472 	int i, refill_required;
   3473 
   3474 	if (!adapter->up)
   3475 		return;
   3476 
   3477 	if (adapter->trigger_reset)
   3478 		return;
   3479 
   3480 	for (i = 0; i < adapter->num_queues; i++) {
   3481 		rx_ring = &adapter->rx_ring[i];
   3482 
   3483 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
   3484 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
   3485 			rx_ring->empty_rx_queue++;
   3486 
   3487 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
   3488 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
   3489 				    1);
   3490 
   3491 				device_printf(adapter->pdev,
   3492 				    "trigger refill for ring %d\n", i);
   3493 
   3494 				workqueue_enqueue(rx_ring->cmpl_tq,
   3495 				    &rx_ring->cmpl_task, curcpu());
   3496 				rx_ring->empty_rx_queue = 0;
   3497 			}
   3498 		} else {
   3499 			rx_ring->empty_rx_queue = 0;
   3500 		}
   3501 	}
   3502 }
   3503 
   3504 static void
   3505 ena_timer_service(void *data)
   3506 {
   3507 	struct ena_adapter *adapter = (struct ena_adapter *)data;
   3508 	struct ena_admin_host_info *host_info =
   3509 	    adapter->ena_dev->host_attr.host_info;
   3510 
   3511 	check_for_missing_keep_alive(adapter);
   3512 
   3513 	check_for_admin_com_state(adapter);
   3514 
   3515 	check_for_missing_tx_completions(adapter);
   3516 
   3517 	check_for_empty_rx_ring(adapter);
   3518 
   3519 	if (host_info != NULL)
   3520 		ena_update_host_info(host_info, adapter->ifp);
   3521 
   3522 	if (unlikely(adapter->trigger_reset)) {
   3523 		device_printf(adapter->pdev, "Trigger reset is on\n");
   3524 		workqueue_enqueue(adapter->reset_tq, &adapter->reset_task,
   3525 		    curcpu());
   3526 		return;
   3527 	}
   3528 
   3529 	/*
   3530 	 * Schedule another timeout one second from now.
   3531 	 */
   3532 	callout_schedule(&adapter->timer_service, hz);
   3533 }
   3534 
   3535 static void
   3536 ena_reset_task(struct work *wk, void *arg)
   3537 {
   3538 	struct ena_com_dev_get_features_ctx get_feat_ctx;
   3539 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
   3540 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3541 	bool dev_up;
   3542 	int rc;
   3543 
   3544 	if (unlikely(!adapter->trigger_reset)) {
   3545 		device_printf(adapter->pdev,
   3546 		    "device reset scheduled but trigger_reset is off\n");
   3547 		return;
   3548 	}
   3549 
   3550 	rw_enter(&adapter->ioctl_sx, RW_WRITER);
   3551 
   3552 	callout_halt(&adapter->timer_service, &adapter->global_mtx);
   3553 
   3554 	dev_up = adapter->up;
   3555 
   3556 	ena_com_set_admin_running_state(ena_dev, false);
   3557 	ena_down(adapter);
   3558 	ena_free_mgmnt_irq(adapter);
   3559 	ena_disable_msix(adapter);
   3560 	ena_com_abort_admin_commands(ena_dev);
   3561 	ena_com_wait_for_abort_completion(ena_dev);
   3562 	ena_com_admin_destroy(ena_dev);
   3563 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3564 
   3565 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
   3566 	adapter->trigger_reset = false;
   3567 
   3568 	/* Finished destroy part. Restart the device */
   3569 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
   3570 	    &adapter->wd_active);
   3571 	if (unlikely(rc != 0)) {
   3572 		device_printf(adapter->pdev,
   3573 		    "ENA device init failed! (err: %d)\n", rc);
   3574 		goto err_dev_free;
   3575 	}
   3576 
   3577 	/* XXX dealloc and realloc MSI-X, probably a waste */
   3578 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
   3579 	    adapter->num_queues);
   3580 	if (unlikely(rc != 0)) {
   3581 		device_printf(adapter->pdev, "Enable MSI-X failed\n");
   3582 		goto err_com_free;
   3583 	}
   3584 
   3585 	/* If the interface was up before the reset bring it up */
   3586 	if (dev_up) {
   3587 		rc = ena_up(adapter);
   3588 		if (unlikely(rc != 0)) {
   3589 			device_printf(adapter->pdev,
   3590 			    "Failed to create I/O queues\n");
   3591 			goto err_msix_free;
   3592 		}
   3593 	}
   3594 
   3595 	callout_reset(&adapter->timer_service, hz,
   3596 	    ena_timer_service, (void *)adapter);
   3597 
   3598 	rw_exit(&adapter->ioctl_sx);
   3599 
   3600 	return;
   3601 
   3602 err_msix_free:
   3603 	ena_free_mgmnt_irq(adapter);
   3604 	ena_disable_msix(adapter);
   3605 err_com_free:
   3606 	ena_com_admin_destroy(ena_dev);
   3607 err_dev_free:
   3608 	device_printf(adapter->pdev, "ENA reset failed!\n");
   3609 	adapter->running = false;
   3610 	rw_exit(&adapter->ioctl_sx);
   3611 }
   3612 
   3613 /**
   3614  * ena_attach - Device Initialization Routine
   3615  * @pdev: device information struct
   3616  *
   3617  * Returns 0 on success, otherwise on failure.
   3618  *
   3619  * ena_attach initializes an adapter identified by a device structure.
   3620  * The OS initialization, configuring of the adapter private structure,
   3621  * and a hardware reset occur.
   3622  **/
   3623 static void
   3624 ena_attach(device_t parent, device_t self, void *aux)
   3625 {
   3626 	struct pci_attach_args *pa = aux;
   3627 	struct ena_com_dev_get_features_ctx get_feat_ctx;
   3628 	static int version_printed;
   3629 	struct ena_adapter *adapter = device_private(self);
   3630 	struct ena_com_dev *ena_dev = NULL;
   3631 	uint16_t tx_sgl_size = 0;
   3632 	uint16_t rx_sgl_size = 0;
   3633 	pcireg_t reg;
   3634 	int io_queue_num;
   3635 	int queue_size;
   3636 	int rc;
   3637 
   3638 	adapter->pdev = self;
   3639 	adapter->ifp = &adapter->sc_ec.ec_if;
   3640 	adapter->sc_pa = *pa;	/* used after attach for adapter reset too */
   3641 
   3642 	if (pci_dma64_available(pa))
   3643 		adapter->sc_dmat = pa->pa_dmat64;
   3644 	else
   3645 		adapter->sc_dmat = pa->pa_dmat;
   3646 
   3647 	pci_aprint_devinfo(pa, NULL);
   3648 
   3649 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   3650 	if ((reg & PCI_COMMAND_MASTER_ENABLE) == 0) {
   3651 		reg |= PCI_COMMAND_MASTER_ENABLE;
   3652         	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
   3653 	}
   3654 
   3655 	mutex_init(&adapter->global_mtx, MUTEX_DEFAULT, IPL_NET);
   3656 	rw_init(&adapter->ioctl_sx);
   3657 
   3658 	/* Set up the timer service */
   3659 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
   3660 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
   3661 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
   3662 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
   3663 
   3664 	if (version_printed++ == 0)
   3665 		device_printf(parent, "%s\n", ena_version);
   3666 
   3667 	rc = ena_allocate_pci_resources(pa, adapter);
   3668 	if (unlikely(rc != 0)) {
   3669 		device_printf(parent, "PCI resource allocation failed!\n");
   3670 		ena_free_pci_resources(adapter);
   3671 		return;
   3672 	}
   3673 
   3674 	/* Allocate memory for ena_dev structure */
   3675 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
   3676 	    M_WAITOK | M_ZERO);
   3677 
   3678 	adapter->ena_dev = ena_dev;
   3679 	ena_dev->dmadev = self;
   3680 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
   3681 	    M_WAITOK | M_ZERO);
   3682 
   3683 	/* Store register resources */
   3684 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t = adapter->sc_btag;
   3685 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h = adapter->sc_bhandle;
   3686 
   3687 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
   3688 
   3689 	/* Device initialization */
   3690 	rc = ena_device_init(adapter, self, &get_feat_ctx, &adapter->wd_active);
   3691 	if (unlikely(rc != 0)) {
   3692 		device_printf(self, "ENA device init failed! (err: %d)\n", rc);
   3693 		rc = ENXIO;
   3694 		goto err_bus_free;
   3695 	}
   3696 
   3697 	adapter->keep_alive_timestamp = getsbinuptime();
   3698 
   3699 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
   3700 
   3701 	/* Set for sure that interface is not up */
   3702 	adapter->up = false;
   3703 
   3704 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
   3705 	    ETHER_ADDR_LEN);
   3706 
   3707 	/* calculate IO queue number to create */
   3708 	io_queue_num = ena_calc_io_queue_num(pa, adapter, &get_feat_ctx);
   3709 
   3710 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
   3711 	    io_queue_num);
   3712 	adapter->num_queues = io_queue_num;
   3713 
   3714 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
   3715 
   3716 	/* calculatre ring sizes */
   3717 	queue_size = ena_calc_queue_size(adapter,&tx_sgl_size,
   3718 	    &rx_sgl_size, &get_feat_ctx);
   3719 	if (unlikely((queue_size <= 0) || (io_queue_num <= 0))) {
   3720 		rc = ENA_COM_FAULT;
   3721 		goto err_com_free;
   3722 	}
   3723 
   3724 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
   3725 
   3726 	adapter->tx_ring_size = queue_size;
   3727 	adapter->rx_ring_size = queue_size;
   3728 
   3729 	adapter->max_tx_sgl_size = tx_sgl_size;
   3730 	adapter->max_rx_sgl_size = rx_sgl_size;
   3731 
   3732 #if 0
   3733 	/* set up dma tags for rx and tx buffers */
   3734 	rc = ena_setup_tx_dma_tag(adapter);
   3735 	if (unlikely(rc != 0)) {
   3736 		device_printf(self, "Failed to create TX DMA tag\n");
   3737 		goto err_com_free;
   3738 	}
   3739 
   3740 	rc = ena_setup_rx_dma_tag(adapter);
   3741 	if (unlikely(rc != 0)) {
   3742 		device_printf(self, "Failed to create RX DMA tag\n");
   3743 		goto err_tx_tag_free;
   3744 	}
   3745 #endif
   3746 
   3747 	/* initialize rings basic information */
   3748 	device_printf(self, "initalize %d io queues\n", io_queue_num);
   3749 	ena_init_io_rings(adapter);
   3750 
   3751 	/* setup network interface */
   3752 	rc = ena_setup_ifnet(self, adapter, &get_feat_ctx);
   3753 	if (unlikely(rc != 0)) {
   3754 		device_printf(self, "Error with network interface setup\n");
   3755 		goto err_io_free;
   3756 	}
   3757 
   3758 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
   3759 	if (unlikely(rc != 0)) {
   3760 		device_printf(self,
   3761 		    "Failed to enable and set the admin interrupts\n");
   3762 		goto err_ifp_free;
   3763 	}
   3764 
   3765 	/* Initialize reset task queue */
   3766 	rc = workqueue_create(&adapter->reset_tq, "ena_reset_enqueue",
   3767 	    ena_reset_task, adapter, 0, IPL_NET, 0);
   3768 	if (unlikely(rc != 0)) {
   3769 		ena_trace(ENA_ALERT,
   3770 		    "Unable to create workqueue for reset task\n");
   3771 		goto err_ifp_free;
   3772 	}
   3773 
   3774 	/* Initialize statistics */
   3775 	ena_alloc_counters_dev(&adapter->dev_stats, io_queue_num);
   3776 	ena_alloc_counters_hwstats(&adapter->hw_stats, io_queue_num);
   3777 #if 0
   3778 	ena_sysctl_add_nodes(adapter);
   3779 #endif
   3780 
   3781 	/* Tell the stack that the interface is not active */
   3782 	if_setdrvflagbits(adapter->ifp, IFF_OACTIVE, IFF_RUNNING);
   3783 
   3784 	adapter->running = true;
   3785 	return;
   3786 
   3787 err_ifp_free:
   3788 	if_detach(adapter->ifp);
   3789 	if_free(adapter->ifp);
   3790 err_io_free:
   3791 	ena_free_all_io_rings_resources(adapter);
   3792 #if 0
   3793 	ena_free_rx_dma_tag(adapter);
   3794 err_tx_tag_free:
   3795 	ena_free_tx_dma_tag(adapter);
   3796 #endif
   3797 err_com_free:
   3798 	ena_com_admin_destroy(ena_dev);
   3799 	ena_com_delete_host_info(ena_dev);
   3800 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3801 err_bus_free:
   3802 	free(ena_dev->bus, M_DEVBUF);
   3803 	free(ena_dev, M_DEVBUF);
   3804 	ena_free_pci_resources(adapter);
   3805 }
   3806 
   3807 /**
   3808  * ena_detach - Device Removal Routine
   3809  * @pdev: device information struct
   3810  *
   3811  * ena_detach is called by the device subsystem to alert the driver
   3812  * that it should release a PCI device.
   3813  **/
   3814 static int
   3815 ena_detach(device_t pdev, int flags)
   3816 {
   3817 	struct ena_adapter *adapter = device_private(pdev);
   3818 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3819 #if 0
   3820 	int rc;
   3821 #endif
   3822 
   3823 	/* Make sure VLANS are not using driver */
   3824 	if (VLAN_ATTACHED(&adapter->sc_ec)) {
   3825 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
   3826 		return (EBUSY);
   3827 	}
   3828 
   3829 	/* Free reset task and callout */
   3830 	callout_halt(&adapter->timer_service, &adapter->global_mtx);
   3831 	workqueue_wait(adapter->reset_tq, &adapter->reset_task);
   3832 	workqueue_destroy(adapter->reset_tq);
   3833 	adapter->reset_tq = NULL;
   3834 
   3835 	rw_enter(&adapter->ioctl_sx, RW_WRITER);
   3836 	ena_down(adapter);
   3837 	rw_exit(&adapter->ioctl_sx);
   3838 
   3839 	if (adapter->ifp != NULL) {
   3840 		ether_ifdetach(adapter->ifp);
   3841 		if_free(adapter->ifp);
   3842 	}
   3843 
   3844 	ena_free_all_io_rings_resources(adapter);
   3845 
   3846 	ena_free_counters((struct evcnt *)&adapter->hw_stats,
   3847 	    sizeof(struct ena_hw_stats));
   3848 	ena_free_counters((struct evcnt *)&adapter->dev_stats,
   3849 	    sizeof(struct ena_stats_dev));
   3850 
   3851 	if (likely(adapter->rss_support))
   3852 		ena_com_rss_destroy(ena_dev);
   3853 
   3854 #if 0
   3855 	rc = ena_free_rx_dma_tag(adapter);
   3856 	if (unlikely(rc != 0))
   3857 		device_printf(adapter->pdev,
   3858 		    "Unmapped RX DMA tag associations\n");
   3859 
   3860 	rc = ena_free_tx_dma_tag(adapter);
   3861 	if (unlikely(rc != 0))
   3862 		device_printf(adapter->pdev,
   3863 		    "Unmapped TX DMA tag associations\n");
   3864 #endif
   3865 
   3866 	/* Reset the device only if the device is running. */
   3867 	if (adapter->running)
   3868 		ena_com_dev_reset(ena_dev, adapter->reset_reason);
   3869 
   3870 	ena_com_delete_host_info(ena_dev);
   3871 
   3872 	ena_free_irqs(adapter);
   3873 
   3874 	ena_com_abort_admin_commands(ena_dev);
   3875 
   3876 	ena_com_wait_for_abort_completion(ena_dev);
   3877 
   3878 	ena_com_admin_destroy(ena_dev);
   3879 
   3880 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3881 
   3882 	ena_free_pci_resources(adapter);
   3883 
   3884 	mutex_destroy(&adapter->global_mtx);
   3885 	rw_destroy(&adapter->ioctl_sx);
   3886 
   3887 	if (ena_dev->bus != NULL)
   3888 		free(ena_dev->bus, M_DEVBUF);
   3889 
   3890 	if (ena_dev != NULL)
   3891 		free(ena_dev, M_DEVBUF);
   3892 
   3893 	return 0;
   3894 }
   3895 
   3896 /******************************************************************************
   3897  ******************************** AENQ Handlers *******************************
   3898  *****************************************************************************/
   3899 /**
   3900  * ena_update_on_link_change:
   3901  * Notify the network interface about the change in link status
   3902  **/
   3903 static void
   3904 ena_update_on_link_change(void *adapter_data,
   3905     struct ena_admin_aenq_entry *aenq_e)
   3906 {
   3907 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
   3908 	struct ena_admin_aenq_link_change_desc *aenq_desc;
   3909 	int status;
   3910 	struct ifnet *ifp;
   3911 
   3912 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
   3913 	ifp = adapter->ifp;
   3914 	status = aenq_desc->flags &
   3915 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
   3916 
   3917 	if (status != 0) {
   3918 		device_printf(adapter->pdev, "link is UP\n");
   3919 		if_link_state_change(ifp, LINK_STATE_UP);
   3920 	} else if (status == 0) {
   3921 		device_printf(adapter->pdev, "link is DOWN\n");
   3922 		if_link_state_change(ifp, LINK_STATE_DOWN);
   3923 	} else {
   3924 		device_printf(adapter->pdev, "invalid value recvd\n");
   3925 		BUG();
   3926 	}
   3927 
   3928 	adapter->link_status = status;
   3929 }
   3930 
   3931 /**
   3932  * This handler will called for unknown event group or unimplemented handlers
   3933  **/
   3934 static void
   3935 unimplemented_aenq_handler(void *data,
   3936     struct ena_admin_aenq_entry *aenq_e)
   3937 {
   3938 	return;
   3939 }
   3940 
   3941 static struct ena_aenq_handlers aenq_handlers = {
   3942     .handlers = {
   3943 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
   3944 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
   3945     },
   3946     .unimplemented_handler = unimplemented_aenq_handler
   3947 };
   3948 
   3949 #ifdef __FreeBSD__
   3950 /*********************************************************************
   3951  *  FreeBSD Device Interface Entry Points
   3952  *********************************************************************/
   3953 
   3954 static device_method_t ena_methods[] = {
   3955     /* Device interface */
   3956     DEVMETHOD(device_probe, ena_probe),
   3957     DEVMETHOD(device_attach, ena_attach),
   3958     DEVMETHOD(device_detach, ena_detach),
   3959     DEVMETHOD_END
   3960 };
   3961 
   3962 static driver_t ena_driver = {
   3963     "ena", ena_methods, sizeof(struct ena_adapter),
   3964 };
   3965 
   3966 devclass_t ena_devclass;
   3967 DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
   3968 MODULE_DEPEND(ena, pci, 1, 1, 1);
   3969 MODULE_DEPEND(ena, ether, 1, 1, 1);
   3970 
   3971 /*********************************************************************/
   3972 #endif /* __FreeBSD__ */
   3973 
   3974 #ifdef __NetBSD__
   3975 CFATTACH_DECL_NEW(ena, sizeof(struct ena_adapter), ena_probe, ena_attach,
   3976 	ena_detach, NULL);
   3977 #endif /* __NetBSD */
   3978