Home | History | Annotate | Line # | Download | only in pci
if_ena.c revision 1.13
      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.13 2019/01/23 11:15:11 ryoon 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_enq",
    645 	    ena_deferred_mq_start, tx_ring, 0, IPL_NET, WQ_PERCPU | WQ_MPSAFE);
    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_comp",
    852 	    ena_deferred_rx_cleanup, rx_ring, 0, IPL_NET, WQ_PERCPU | WQ_MPSAFE);
    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 	if (atomic_cas_uint(&tx_ring->task_pending, 0, 1) == 0)
   1426 		workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task, NULL);
   1427 
   1428 	return (work_done);
   1429 }
   1430 
   1431 #if 0
   1432 static void
   1433 ena_rx_hash_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
   1434     struct mbuf *mbuf)
   1435 {
   1436 	struct ena_adapter *adapter = rx_ring->adapter;
   1437 
   1438 	if (likely(adapter->rss_support)) {
   1439 		mbuf->m_pkthdr.flowid = ena_rx_ctx->hash;
   1440 
   1441 		if (ena_rx_ctx->frag &&
   1442 		    (ena_rx_ctx->l3_proto != ENA_ETH_IO_L3_PROTO_UNKNOWN)) {
   1443 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
   1444 			return;
   1445 		}
   1446 
   1447 		switch (ena_rx_ctx->l3_proto) {
   1448 		case ENA_ETH_IO_L3_PROTO_IPV4:
   1449 			switch (ena_rx_ctx->l4_proto) {
   1450 			case ENA_ETH_IO_L4_PROTO_TCP:
   1451 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4);
   1452 				break;
   1453 			case ENA_ETH_IO_L4_PROTO_UDP:
   1454 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4);
   1455 				break;
   1456 			default:
   1457 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4);
   1458 			}
   1459 			break;
   1460 		case ENA_ETH_IO_L3_PROTO_IPV6:
   1461 			switch (ena_rx_ctx->l4_proto) {
   1462 			case ENA_ETH_IO_L4_PROTO_TCP:
   1463 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6);
   1464 				break;
   1465 			case ENA_ETH_IO_L4_PROTO_UDP:
   1466 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6);
   1467 				break;
   1468 			default:
   1469 				M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6);
   1470 			}
   1471 			break;
   1472 		case ENA_ETH_IO_L3_PROTO_UNKNOWN:
   1473 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
   1474 			break;
   1475 		default:
   1476 			M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH);
   1477 		}
   1478 	} else {
   1479 		mbuf->m_pkthdr.flowid = rx_ring->qid;
   1480 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE);
   1481 	}
   1482 }
   1483 #endif
   1484 
   1485 /**
   1486  * ena_rx_mbuf - assemble mbuf from descriptors
   1487  * @rx_ring: ring for which we want to clean packets
   1488  * @ena_bufs: buffer info
   1489  * @ena_rx_ctx: metadata for this packet(s)
   1490  * @next_to_clean: ring pointer, will be updated only upon success
   1491  *
   1492  **/
   1493 static struct mbuf*
   1494 ena_rx_mbuf(struct ena_ring *rx_ring, struct ena_com_rx_buf_info *ena_bufs,
   1495     struct ena_com_rx_ctx *ena_rx_ctx, uint16_t *next_to_clean)
   1496 {
   1497 	struct mbuf *mbuf;
   1498 	struct ena_rx_buffer *rx_info;
   1499 	struct ena_adapter *adapter;
   1500 	unsigned int descs = ena_rx_ctx->descs;
   1501 	uint16_t ntc, len, req_id, buf = 0;
   1502 
   1503 	ntc = *next_to_clean;
   1504 	adapter = rx_ring->adapter;
   1505 	rx_info = &rx_ring->rx_buffer_info[ntc];
   1506 
   1507 	if (unlikely(rx_info->mbuf == NULL)) {
   1508 		device_printf(adapter->pdev, "NULL mbuf in rx_info");
   1509 		return (NULL);
   1510 	}
   1511 
   1512 	len = ena_bufs[buf].len;
   1513 	req_id = ena_bufs[buf].req_id;
   1514 	rx_info = &rx_ring->rx_buffer_info[req_id];
   1515 
   1516 	ena_trace(ENA_DBG | ENA_RXPTH, "rx_info %p, mbuf %p, paddr %jx",
   1517 	    rx_info, rx_info->mbuf, (uintmax_t)rx_info->ena_buf.paddr);
   1518 
   1519 	mbuf = rx_info->mbuf;
   1520 	KASSERT(mbuf->m_flags & M_PKTHDR);
   1521 	mbuf->m_pkthdr.len = len;
   1522 	mbuf->m_len = len;
   1523 	m_set_rcvif(mbuf, rx_ring->que->adapter->ifp);
   1524 
   1525 	/* Fill mbuf with hash key and it's interpretation for optimization */
   1526 #if 0
   1527 	ena_rx_hash_mbuf(rx_ring, ena_rx_ctx, mbuf);
   1528 #endif
   1529 
   1530 	ena_trace(ENA_DBG | ENA_RXPTH, "rx mbuf 0x%p, flags=0x%x, len: %d",
   1531 	    mbuf, mbuf->m_flags, mbuf->m_pkthdr.len);
   1532 
   1533 	/* DMA address is not needed anymore, unmap it */
   1534 	bus_dmamap_unload(rx_ring->adapter->sc_dmat, rx_info->map);
   1535 
   1536 	rx_info->mbuf = NULL;
   1537 	rx_ring->free_rx_ids[ntc] = req_id;
   1538 	ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
   1539 
   1540 	/*
   1541 	 * While we have more than 1 descriptors for one rcvd packet, append
   1542 	 * other mbufs to the main one
   1543 	 */
   1544 	while (--descs) {
   1545 		++buf;
   1546 		len = ena_bufs[buf].len;
   1547 		req_id = ena_bufs[buf].req_id;
   1548 		rx_info = &rx_ring->rx_buffer_info[req_id];
   1549 
   1550 		if (unlikely(rx_info->mbuf == NULL)) {
   1551 			device_printf(adapter->pdev, "NULL mbuf in rx_info");
   1552 			/*
   1553 			 * If one of the required mbufs was not allocated yet,
   1554 			 * we can break there.
   1555 			 * All earlier used descriptors will be reallocated
   1556 			 * later and not used mbufs can be reused.
   1557 			 * The next_to_clean pointer will not be updated in case
   1558 			 * of an error, so caller should advance it manually
   1559 			 * in error handling routine to keep it up to date
   1560 			 * with hw ring.
   1561 			 */
   1562 			m_freem(mbuf);
   1563 			return (NULL);
   1564 		}
   1565 
   1566 		if (unlikely(m_append(mbuf, len, rx_info->mbuf->m_data) == 0)) {
   1567 			counter_u64_add(rx_ring->rx_stats.mbuf_alloc_fail, 1);
   1568 			ena_trace(ENA_WARNING, "Failed to append Rx mbuf %p",
   1569 			    mbuf);
   1570 		}
   1571 
   1572 		ena_trace(ENA_DBG | ENA_RXPTH,
   1573 		    "rx mbuf updated. len %d", mbuf->m_pkthdr.len);
   1574 
   1575 		/* Free already appended mbuf, it won't be useful anymore */
   1576 		bus_dmamap_unload(rx_ring->adapter->sc_dmat, rx_info->map);
   1577 		m_freem(rx_info->mbuf);
   1578 		rx_info->mbuf = NULL;
   1579 
   1580 		rx_ring->free_rx_ids[ntc] = req_id;
   1581 		ntc = ENA_RX_RING_IDX_NEXT(ntc, rx_ring->ring_size);
   1582 	}
   1583 
   1584 	*next_to_clean = ntc;
   1585 
   1586 	return (mbuf);
   1587 }
   1588 
   1589 /**
   1590  * ena_rx_checksum - indicate in mbuf if hw indicated a good cksum
   1591  **/
   1592 static inline void
   1593 ena_rx_checksum(struct ena_ring *rx_ring, struct ena_com_rx_ctx *ena_rx_ctx,
   1594     struct mbuf *mbuf)
   1595 {
   1596 
   1597 	/* IPv4 */
   1598 	if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV4) {
   1599 		mbuf->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1600 		if (ena_rx_ctx->l3_csum_err) {
   1601 			/* ipv4 checksum error */
   1602 			mbuf->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1603 			counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
   1604 			ena_trace(ENA_DBG, "RX IPv4 header checksum error");
   1605 			return;
   1606 		}
   1607 
   1608 		/*  TCP/UDP */
   1609 		if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
   1610 		    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
   1611 			mbuf->m_pkthdr.csum_flags |= (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ? M_CSUM_TCPv4 : M_CSUM_UDPv4;
   1612 			if (ena_rx_ctx->l4_csum_err) {
   1613 				/* TCP/UDP checksum error */
   1614 				mbuf->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1615 				counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
   1616 				ena_trace(ENA_DBG, "RX L4 checksum error");
   1617 			}
   1618 		}
   1619 	}
   1620 	/* IPv6 */
   1621 	else if (ena_rx_ctx->l3_proto == ENA_ETH_IO_L3_PROTO_IPV6) {
   1622 		/*  TCP/UDP */
   1623 		if ((ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ||
   1624 		    (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_UDP)) {
   1625 			mbuf->m_pkthdr.csum_flags |= (ena_rx_ctx->l4_proto == ENA_ETH_IO_L4_PROTO_TCP) ? M_CSUM_TCPv6 : M_CSUM_UDPv6;
   1626 			if (ena_rx_ctx->l4_csum_err) {
   1627 				/* TCP/UDP checksum error */
   1628 				mbuf->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1629 				counter_u64_add(rx_ring->rx_stats.bad_csum, 1);
   1630 				ena_trace(ENA_DBG, "RX L4 checksum error");
   1631 			}
   1632 		}
   1633 	}
   1634 }
   1635 
   1636 static void
   1637 ena_deferred_rx_cleanup(struct work *wk, void *arg)
   1638 {
   1639 	struct ena_ring *rx_ring = arg;
   1640 	int budget = CLEAN_BUDGET;
   1641 
   1642 	atomic_swap_uint(&rx_ring->task_pending, 0);
   1643 
   1644 	ENA_RING_MTX_LOCK(rx_ring);
   1645 	/*
   1646 	 * If deferred task was executed, perform cleanup of all awaiting
   1647 	 * descs (or until given budget is depleted to avoid infinite loop).
   1648 	 */
   1649 	while (likely(budget--)) {
   1650 		if (ena_rx_cleanup(rx_ring) == 0)
   1651 			break;
   1652 	}
   1653 	ENA_RING_MTX_UNLOCK(rx_ring);
   1654 }
   1655 
   1656 /**
   1657  * ena_rx_cleanup - handle rx irq
   1658  * @arg: ring for which irq is being handled
   1659  **/
   1660 static int
   1661 ena_rx_cleanup(struct ena_ring *rx_ring)
   1662 {
   1663 	struct ena_adapter *adapter;
   1664 	struct mbuf *mbuf;
   1665 	struct ena_com_rx_ctx ena_rx_ctx;
   1666 	struct ena_com_io_cq* io_cq;
   1667 	struct ena_com_io_sq* io_sq;
   1668 	struct ifnet *ifp;
   1669 	uint16_t ena_qid;
   1670 	uint16_t next_to_clean;
   1671 	uint32_t refill_required;
   1672 	uint32_t refill_threshold;
   1673 	uint32_t do_if_input = 0;
   1674 	unsigned int qid;
   1675 	int rc, i;
   1676 	int budget = RX_BUDGET;
   1677 
   1678 	adapter = rx_ring->que->adapter;
   1679 	ifp = adapter->ifp;
   1680 	qid = rx_ring->que->id;
   1681 	ena_qid = ENA_IO_RXQ_IDX(qid);
   1682 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   1683 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
   1684 	next_to_clean = rx_ring->next_to_clean;
   1685 
   1686 	ena_trace(ENA_DBG, "rx: qid %d", qid);
   1687 
   1688 	do {
   1689 		ena_rx_ctx.ena_bufs = rx_ring->ena_bufs;
   1690 		ena_rx_ctx.max_bufs = adapter->max_rx_sgl_size;
   1691 		ena_rx_ctx.descs = 0;
   1692 		rc = ena_com_rx_pkt(io_cq, io_sq, &ena_rx_ctx);
   1693 
   1694 		if (unlikely(rc != 0))
   1695 			goto error;
   1696 
   1697 		if (unlikely(ena_rx_ctx.descs == 0))
   1698 			break;
   1699 
   1700 		ena_trace(ENA_DBG | ENA_RXPTH, "rx: q %d got packet from ena. "
   1701 		    "descs #: %d l3 proto %d l4 proto %d hash: %x",
   1702 		    rx_ring->qid, ena_rx_ctx.descs, ena_rx_ctx.l3_proto,
   1703 		    ena_rx_ctx.l4_proto, ena_rx_ctx.hash);
   1704 
   1705 		/* Receive mbuf from the ring */
   1706 		mbuf = ena_rx_mbuf(rx_ring, rx_ring->ena_bufs,
   1707 		    &ena_rx_ctx, &next_to_clean);
   1708 
   1709 		/* Exit if we failed to retrieve a buffer */
   1710 		if (unlikely(mbuf == NULL)) {
   1711 			for (i = 0; i < ena_rx_ctx.descs; ++i) {
   1712 				rx_ring->free_rx_ids[next_to_clean] =
   1713 				    rx_ring->ena_bufs[i].req_id;
   1714 				next_to_clean =
   1715 				    ENA_RX_RING_IDX_NEXT(next_to_clean,
   1716 				    rx_ring->ring_size);
   1717 
   1718 			}
   1719 			break;
   1720 		}
   1721 
   1722 		if (((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) != 0) ||
   1723 		    ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) != 0) ||
   1724 		    ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) != 0) ||
   1725 		    ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) != 0) ||
   1726 		    ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) != 0)) {
   1727 			ena_rx_checksum(rx_ring, &ena_rx_ctx, mbuf);
   1728 		}
   1729 
   1730 		counter_enter();
   1731 		counter_u64_add_protected(rx_ring->rx_stats.bytes,
   1732 		    mbuf->m_pkthdr.len);
   1733 		counter_u64_add_protected(adapter->hw_stats.rx_bytes,
   1734 		    mbuf->m_pkthdr.len);
   1735 		counter_exit();
   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 #ifdef LRO
   1742 		if (((ifp->if_capenable & IFCAP_LRO) != 0)  &&
   1743 		    ((mbuf->m_pkthdr.csum_flags & CSUM_IP_VALID) != 0) &&
   1744 		    (ena_rx_ctx.l4_proto == ENA_ETH_IO_L4_PROTO_TCP)) {
   1745 			/*
   1746 			 * Send to the stack if:
   1747 			 *  - LRO not enabled, or
   1748 			 *  - no LRO resources, or
   1749 			 *  - lro enqueue fails
   1750 			 */
   1751 			if ((rx_ring->lro.lro_cnt != 0) &&
   1752 			    (tcp_lro_rx(&rx_ring->lro, mbuf, 0) == 0))
   1753 					do_if_input = 0;
   1754 		}
   1755 #endif
   1756 		if (do_if_input != 0) {
   1757 			ena_trace(ENA_DBG | ENA_RXPTH,
   1758 			    "calling if_input() with mbuf %p", mbuf);
   1759 			if_percpuq_enqueue(ifp->if_percpuq, mbuf);
   1760 		}
   1761 
   1762 		counter_enter();
   1763 		counter_u64_add_protected(rx_ring->rx_stats.cnt, 1);
   1764 		counter_u64_add_protected(adapter->hw_stats.rx_packets, 1);
   1765 		counter_exit();
   1766 	} while (--budget);
   1767 
   1768 	rx_ring->next_to_clean = next_to_clean;
   1769 
   1770 	refill_required = ena_com_free_desc(io_sq);
   1771 	refill_threshold = rx_ring->ring_size / ENA_RX_REFILL_THRESH_DIVIDER;
   1772 
   1773 	if (refill_required > refill_threshold) {
   1774 		ena_com_update_dev_comp_head(rx_ring->ena_com_io_cq);
   1775 		ena_refill_rx_bufs(rx_ring, refill_required);
   1776 	}
   1777 
   1778 #ifdef LRO
   1779 	tcp_lro_flush_all(&rx_ring->lro);
   1780 #endif
   1781 
   1782 	return (RX_BUDGET - budget);
   1783 
   1784 error:
   1785 	counter_u64_add(rx_ring->rx_stats.bad_desc_num, 1);
   1786 	return (RX_BUDGET - budget);
   1787 }
   1788 
   1789 /*********************************************************************
   1790  *
   1791  *  MSIX & Interrupt Service routine
   1792  *
   1793  **********************************************************************/
   1794 
   1795 /**
   1796  * ena_handle_msix - MSIX Interrupt Handler for admin/async queue
   1797  * @arg: interrupt number
   1798  **/
   1799 static int
   1800 ena_intr_msix_mgmnt(void *arg)
   1801 {
   1802 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
   1803 
   1804 	ena_com_admin_q_comp_intr_handler(adapter->ena_dev);
   1805 	if (likely(adapter->running))
   1806 		ena_com_aenq_intr_handler(adapter->ena_dev, arg);
   1807 
   1808 	return 1;
   1809 }
   1810 
   1811 /**
   1812  * ena_handle_msix - MSIX Interrupt Handler for Tx/Rx
   1813  * @arg: interrupt number
   1814  **/
   1815 static int
   1816 ena_handle_msix(void *arg)
   1817 {
   1818 	struct ena_que	*que = arg;
   1819 	struct ena_adapter *adapter = que->adapter;
   1820 	struct ifnet *ifp = adapter->ifp;
   1821 	struct ena_ring *tx_ring;
   1822 	struct ena_ring *rx_ring;
   1823 	struct ena_com_io_cq* io_cq;
   1824 	struct ena_eth_io_intr_reg intr_reg;
   1825 	int qid, ena_qid;
   1826 	int txc, rxc, i;
   1827 
   1828 	if (unlikely((if_getdrvflags(ifp) & IFF_RUNNING) == 0))
   1829 		return 0;
   1830 
   1831 	ena_trace(ENA_DBG, "MSI-X TX/RX routine");
   1832 
   1833 	tx_ring = que->tx_ring;
   1834 	rx_ring = que->rx_ring;
   1835 	qid = que->id;
   1836 	ena_qid = ENA_IO_TXQ_IDX(qid);
   1837 	io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   1838 
   1839 	for (i = 0; i < CLEAN_BUDGET; ++i) {
   1840 		/*
   1841 		 * If lock cannot be acquired, then deferred cleanup task was
   1842 		 * being executed and rx ring is being cleaned up in
   1843 		 * another thread.
   1844 		 */
   1845 		if (likely(ENA_RING_MTX_TRYLOCK(rx_ring) != 0)) {
   1846 			rxc = ena_rx_cleanup(rx_ring);
   1847 			ENA_RING_MTX_UNLOCK(rx_ring);
   1848 		} else {
   1849 			rxc = 0;
   1850 		}
   1851 
   1852 		/* Protection from calling ena_tx_cleanup from ena_start_xmit */
   1853 		ENA_RING_MTX_LOCK(tx_ring);
   1854 		txc = ena_tx_cleanup(tx_ring);
   1855 		ENA_RING_MTX_UNLOCK(tx_ring);
   1856 
   1857 		if (unlikely((if_getdrvflags(ifp) & IFF_RUNNING) == 0))
   1858 			return 0;
   1859 
   1860 		if ((txc != TX_BUDGET) && (rxc != RX_BUDGET))
   1861 		       break;
   1862 	}
   1863 
   1864 	/* Signal that work is done and unmask interrupt */
   1865 	ena_com_update_intr_reg(&intr_reg,
   1866 	    RX_IRQ_INTERVAL,
   1867 	    TX_IRQ_INTERVAL,
   1868 	    true);
   1869 	ena_com_unmask_intr(io_cq, &intr_reg);
   1870 
   1871 	return 1;
   1872 }
   1873 
   1874 static int
   1875 ena_enable_msix(struct ena_adapter *adapter)
   1876 {
   1877 	int msix_req;
   1878 	int counts[PCI_INTR_TYPE_SIZE];
   1879 	int max_type;
   1880 
   1881 	/* Reserved the max msix vectors we might need */
   1882 	msix_req = ENA_MAX_MSIX_VEC(adapter->num_queues);
   1883 
   1884 	counts[PCI_INTR_TYPE_INTX] = 0;
   1885 	counts[PCI_INTR_TYPE_MSI] = 0;
   1886 	counts[PCI_INTR_TYPE_MSIX] = msix_req;
   1887 	max_type = PCI_INTR_TYPE_MSIX;
   1888 
   1889 	if (pci_intr_alloc(&adapter->sc_pa, &adapter->sc_intrs, counts,
   1890 	    max_type) != 0) {
   1891 		aprint_error_dev(adapter->pdev,
   1892 		    "failed to allocate interrupt\n");
   1893 		return ENOSPC;
   1894 	}
   1895 
   1896 	adapter->sc_nintrs = counts[PCI_INTR_TYPE_MSIX];
   1897 
   1898 	if (counts[PCI_INTR_TYPE_MSIX] != msix_req) {
   1899 		device_printf(adapter->pdev,
   1900 		    "Enable only %d MSI-x (out of %d), reduce "
   1901 		    "the number of queues\n", adapter->sc_nintrs, msix_req);
   1902 		adapter->num_queues = adapter->sc_nintrs - ENA_ADMIN_MSIX_VEC;
   1903 	}
   1904 
   1905 	return 0;
   1906 }
   1907 
   1908 #if 0
   1909 static void
   1910 ena_setup_io_intr(struct ena_adapter *adapter)
   1911 {
   1912 	static int last_bind_cpu = -1;
   1913 	int irq_idx;
   1914 
   1915 	for (int i = 0; i < adapter->num_queues; i++) {
   1916 		irq_idx = ENA_IO_IRQ_IDX(i);
   1917 
   1918 		snprintf(adapter->irq_tbl[irq_idx].name, ENA_IRQNAME_SIZE,
   1919 		    "%s-TxRx-%d", device_xname(adapter->pdev), i);
   1920 		adapter->irq_tbl[irq_idx].handler = ena_handle_msix;
   1921 		adapter->irq_tbl[irq_idx].data = &adapter->que[i];
   1922 		adapter->irq_tbl[irq_idx].vector =
   1923 		    adapter->msix_entries[irq_idx].vector;
   1924 		ena_trace(ENA_INFO | ENA_IOQ, "ena_setup_io_intr vector: %d\n",
   1925 		    adapter->msix_entries[irq_idx].vector);
   1926 #ifdef	RSS
   1927 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
   1928 		    rss_getcpu(i % rss_getnumbuckets());
   1929 #else
   1930 		/*
   1931 		 * We still want to bind rings to the corresponding cpu
   1932 		 * using something similar to the RSS round-robin technique.
   1933 		 */
   1934 		if (unlikely(last_bind_cpu < 0))
   1935 			last_bind_cpu = CPU_FIRST();
   1936 		adapter->que[i].cpu = adapter->irq_tbl[irq_idx].cpu =
   1937 		    last_bind_cpu;
   1938 		last_bind_cpu = CPU_NEXT(last_bind_cpu);
   1939 #endif
   1940 	}
   1941 }
   1942 #endif
   1943 
   1944 static int
   1945 ena_request_mgmnt_irq(struct ena_adapter *adapter)
   1946 {
   1947 	const char *intrstr;
   1948 	char intrbuf[PCI_INTRSTR_LEN];
   1949 	char intr_xname[INTRDEVNAMEBUF];
   1950 	pci_chipset_tag_t pc = adapter->sc_pa.pa_pc;
   1951 	const int irq_slot = ENA_MGMNT_IRQ_IDX;
   1952 
   1953 	KASSERT(adapter->sc_intrs != NULL);
   1954 	KASSERT(adapter->sc_ihs[irq_slot] == NULL);
   1955 
   1956 	snprintf(intr_xname, sizeof(intr_xname), "%s mgmnt",
   1957 	    device_xname(adapter->pdev));
   1958 	intrstr = pci_intr_string(pc, adapter->sc_intrs[irq_slot],
   1959 	    intrbuf, sizeof(intrbuf));
   1960 
   1961 	adapter->sc_ihs[irq_slot] = pci_intr_establish_xname(
   1962 	    pc, adapter->sc_intrs[irq_slot],
   1963 	    IPL_NET, ena_intr_msix_mgmnt, adapter, intr_xname);
   1964 
   1965 	if (adapter->sc_ihs[irq_slot] == NULL) {
   1966 		device_printf(adapter->pdev, "failed to register "
   1967 		    "interrupt handler for MGMNT irq %s\n",
   1968 		    intrstr);
   1969 		return ENOMEM;
   1970 	}
   1971 
   1972 	aprint_normal_dev(adapter->pdev,
   1973 	    "for MGMNT interrupting at %s\n", intrstr);
   1974 
   1975 	return 0;
   1976 }
   1977 
   1978 static int
   1979 ena_request_io_irq(struct ena_adapter *adapter)
   1980 {
   1981 	const char *intrstr;
   1982 	char intrbuf[PCI_INTRSTR_LEN];
   1983 	char intr_xname[INTRDEVNAMEBUF];
   1984 	pci_chipset_tag_t pc = adapter->sc_pa.pa_pc;
   1985 	const int irq_off = ENA_IO_IRQ_FIRST_IDX;
   1986 	void *vih;
   1987 	kcpuset_t *affinity;
   1988 	int i;
   1989 
   1990 	KASSERT(adapter->sc_intrs != NULL);
   1991 
   1992 	kcpuset_create(&affinity, false);
   1993 
   1994 	for (i = 0; i < adapter->num_queues; i++) {
   1995 		int irq_slot = i + irq_off;
   1996 		int affinity_to = (irq_slot) % ncpu;
   1997 
   1998 		KASSERT((void *)adapter->sc_intrs[irq_slot] != NULL);
   1999 		KASSERT(adapter->sc_ihs[irq_slot] == NULL);
   2000 
   2001 		snprintf(intr_xname, sizeof(intr_xname), "%s ioq%d",
   2002 		    device_xname(adapter->pdev), i);
   2003 		intrstr = pci_intr_string(pc, adapter->sc_intrs[irq_slot],
   2004 		    intrbuf, sizeof(intrbuf));
   2005 
   2006 		vih = pci_intr_establish_xname(adapter->sc_pa.pa_pc,
   2007 		    adapter->sc_intrs[irq_slot], IPL_NET,
   2008 		    ena_handle_msix, &adapter->que[i], intr_xname);
   2009 
   2010 		if (adapter->sc_ihs[ENA_MGMNT_IRQ_IDX] == NULL) {
   2011 			device_printf(adapter->pdev, "failed to register "
   2012 			    "interrupt handler for IO queue %d irq %s\n",
   2013 			    i, intrstr);
   2014 			goto err;
   2015 		}
   2016 
   2017 		kcpuset_zero(affinity);
   2018 		/* Round-robin affinity */
   2019 		kcpuset_set(affinity, affinity_to);
   2020 		int error = interrupt_distribute(vih, affinity, NULL);
   2021 		if (error == 0) {
   2022 			aprint_normal_dev(adapter->pdev,
   2023 			    "for IO queue %d interrupting at %s"
   2024 			    " affinity to %u\n", i, intrstr, affinity_to);
   2025 		} else {
   2026 			aprint_normal_dev(adapter->pdev,
   2027 			    "for IO queue %d interrupting at %s\n", i, intrstr);
   2028 		}
   2029 
   2030 		adapter->sc_ihs[irq_slot] = vih;
   2031 
   2032 #ifdef	RSS
   2033 		ena_trace(ENA_INFO, "queue %d - RSS bucket %d\n",
   2034 		    i - ENA_IO_IRQ_FIRST_IDX, irq->cpu);
   2035 #else
   2036 		ena_trace(ENA_INFO, "queue %d - cpu %d\n",
   2037 		    i - ENA_IO_IRQ_FIRST_IDX, affinity_to);
   2038 #endif
   2039 	}
   2040 
   2041 	kcpuset_destroy(affinity);
   2042 	return 0;
   2043 
   2044 err:
   2045 	kcpuset_destroy(affinity);
   2046 
   2047 	for (i--; i >= 0; i--) {
   2048 #if defined(DEBUG) || defined(DIAGNOSTIC)
   2049 		int irq_slot = i + irq_off;
   2050 #endif
   2051 		KASSERT(adapter->sc_ihs[irq_slot] != NULL);
   2052 		pci_intr_disestablish(adapter->sc_pa.pa_pc, adapter->sc_ihs[i]);
   2053 		adapter->sc_ihs[i] = NULL;
   2054 	}
   2055 
   2056 	return ENOSPC;
   2057 }
   2058 
   2059 static void
   2060 ena_free_mgmnt_irq(struct ena_adapter *adapter)
   2061 {
   2062 	const int irq_slot = ENA_MGMNT_IRQ_IDX;
   2063 
   2064 	if (adapter->sc_ihs[irq_slot]) {
   2065 		pci_intr_disestablish(adapter->sc_pa.pa_pc,
   2066 		    adapter->sc_ihs[irq_slot]);
   2067 		adapter->sc_ihs[irq_slot] = NULL;
   2068 	}
   2069 }
   2070 
   2071 static void
   2072 ena_free_io_irq(struct ena_adapter *adapter)
   2073 {
   2074 	const int irq_off = ENA_IO_IRQ_FIRST_IDX;
   2075 
   2076 	for (int i = 0; i < adapter->num_queues; i++) {
   2077 		int irq_slot = i + irq_off;
   2078 
   2079 		if (adapter->sc_ihs[irq_slot]) {
   2080 			pci_intr_disestablish(adapter->sc_pa.pa_pc,
   2081 			    adapter->sc_ihs[i]);
   2082 			adapter->sc_ihs[i] = NULL;
   2083 		}
   2084 	}
   2085 }
   2086 
   2087 static void
   2088 ena_free_irqs(struct ena_adapter* adapter)
   2089 {
   2090 
   2091 	ena_free_io_irq(adapter);
   2092 	ena_free_mgmnt_irq(adapter);
   2093 	ena_disable_msix(adapter);
   2094 }
   2095 
   2096 static void
   2097 ena_disable_msix(struct ena_adapter *adapter)
   2098 {
   2099 	pci_intr_release(adapter->sc_pa.pa_pc, adapter->sc_intrs,
   2100 	    adapter->sc_nintrs);
   2101 }
   2102 
   2103 static void
   2104 ena_unmask_all_io_irqs(struct ena_adapter *adapter)
   2105 {
   2106 	struct ena_com_io_cq* io_cq;
   2107 	struct ena_eth_io_intr_reg intr_reg;
   2108 	uint16_t ena_qid;
   2109 	int i;
   2110 
   2111 	/* Unmask interrupts for all queues */
   2112 	for (i = 0; i < adapter->num_queues; i++) {
   2113 		ena_qid = ENA_IO_TXQ_IDX(i);
   2114 		io_cq = &adapter->ena_dev->io_cq_queues[ena_qid];
   2115 		ena_com_update_intr_reg(&intr_reg, 0, 0, true);
   2116 		ena_com_unmask_intr(io_cq, &intr_reg);
   2117 	}
   2118 }
   2119 
   2120 /* Configure the Rx forwarding */
   2121 static int
   2122 ena_rss_configure(struct ena_adapter *adapter)
   2123 {
   2124 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   2125 	int rc;
   2126 
   2127 	/* Set indirect table */
   2128 	rc = ena_com_indirect_table_set(ena_dev);
   2129 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
   2130 		return (rc);
   2131 
   2132 	/* Configure hash function (if supported) */
   2133 	rc = ena_com_set_hash_function(ena_dev);
   2134 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
   2135 		return (rc);
   2136 
   2137 	/* Configure hash inputs (if supported) */
   2138 	rc = ena_com_set_hash_ctrl(ena_dev);
   2139 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP)))
   2140 		return (rc);
   2141 
   2142 	return (0);
   2143 }
   2144 
   2145 static int
   2146 ena_up_complete(struct ena_adapter *adapter)
   2147 {
   2148 	int rc;
   2149 
   2150 	if (likely(adapter->rss_support)) {
   2151 		rc = ena_rss_configure(adapter);
   2152 		if (rc != 0)
   2153 			return (rc);
   2154 	}
   2155 
   2156 	rc = ena_change_mtu(adapter->ifp, adapter->ifp->if_mtu);
   2157 	if (unlikely(rc != 0))
   2158 		return (rc);
   2159 
   2160 	ena_refill_all_rx_bufs(adapter);
   2161 	ena_reset_counters((struct evcnt *)&adapter->hw_stats,
   2162 	    sizeof(adapter->hw_stats));
   2163 
   2164 	return (0);
   2165 }
   2166 
   2167 static int
   2168 ena_up(struct ena_adapter *adapter)
   2169 {
   2170 	int rc = 0;
   2171 
   2172 #if 0
   2173 	if (unlikely(device_is_attached(adapter->pdev) == 0)) {
   2174 		device_printf(adapter->pdev, "device is not attached!\n");
   2175 		return (ENXIO);
   2176 	}
   2177 #endif
   2178 
   2179 	if (unlikely(!adapter->running)) {
   2180 		device_printf(adapter->pdev, "device is not running!\n");
   2181 		return (ENXIO);
   2182 	}
   2183 
   2184 	if (!adapter->up) {
   2185 		device_printf(adapter->pdev, "device is going UP\n");
   2186 
   2187 		/* setup interrupts for IO queues */
   2188 		rc = ena_request_io_irq(adapter);
   2189 		if (unlikely(rc != 0)) {
   2190 			ena_trace(ENA_ALERT, "err_req_irq");
   2191 			goto err_req_irq;
   2192 		}
   2193 
   2194 		/* allocate transmit descriptors */
   2195 		rc = ena_setup_all_tx_resources(adapter);
   2196 		if (unlikely(rc != 0)) {
   2197 			ena_trace(ENA_ALERT, "err_setup_tx");
   2198 			goto err_setup_tx;
   2199 		}
   2200 
   2201 		/* allocate receive descriptors */
   2202 		rc = ena_setup_all_rx_resources(adapter);
   2203 		if (unlikely(rc != 0)) {
   2204 			ena_trace(ENA_ALERT, "err_setup_rx");
   2205 			goto err_setup_rx;
   2206 		}
   2207 
   2208 		/* create IO queues for Rx & Tx */
   2209 		rc = ena_create_io_queues(adapter);
   2210 		if (unlikely(rc != 0)) {
   2211 			ena_trace(ENA_ALERT,
   2212 			    "create IO queues failed");
   2213 			goto err_io_que;
   2214 		}
   2215 
   2216 		if (unlikely(adapter->link_status))
   2217 			if_link_state_change(adapter->ifp, LINK_STATE_UP);
   2218 
   2219 		rc = ena_up_complete(adapter);
   2220 		if (unlikely(rc != 0))
   2221 			goto err_up_complete;
   2222 
   2223 		counter_u64_add(adapter->dev_stats.interface_up, 1);
   2224 
   2225 		ena_update_hwassist(adapter);
   2226 
   2227 		if_setdrvflagbits(adapter->ifp, IFF_RUNNING,
   2228 		    IFF_OACTIVE);
   2229 
   2230 		callout_reset(&adapter->timer_service, hz,
   2231 		    ena_timer_service, (void *)adapter);
   2232 
   2233 		adapter->up = true;
   2234 
   2235 		ena_unmask_all_io_irqs(adapter);
   2236 	}
   2237 
   2238 	return (0);
   2239 
   2240 err_up_complete:
   2241 	ena_destroy_all_io_queues(adapter);
   2242 err_io_que:
   2243 	ena_free_all_rx_resources(adapter);
   2244 err_setup_rx:
   2245 	ena_free_all_tx_resources(adapter);
   2246 err_setup_tx:
   2247 	ena_free_io_irq(adapter);
   2248 err_req_irq:
   2249 	return (rc);
   2250 }
   2251 
   2252 #if 0
   2253 static uint64_t
   2254 ena_get_counter(struct ifnet *ifp, ift_counter cnt)
   2255 {
   2256 	struct ena_adapter *adapter;
   2257 	struct ena_hw_stats *stats;
   2258 
   2259 	adapter = if_getsoftc(ifp);
   2260 	stats = &adapter->hw_stats;
   2261 
   2262 	switch (cnt) {
   2263 	case IFCOUNTER_IPACKETS:
   2264 		return (counter_u64_fetch(stats->rx_packets));
   2265 	case IFCOUNTER_OPACKETS:
   2266 		return (counter_u64_fetch(stats->tx_packets));
   2267 	case IFCOUNTER_IBYTES:
   2268 		return (counter_u64_fetch(stats->rx_bytes));
   2269 	case IFCOUNTER_OBYTES:
   2270 		return (counter_u64_fetch(stats->tx_bytes));
   2271 	case IFCOUNTER_IQDROPS:
   2272 		return (counter_u64_fetch(stats->rx_drops));
   2273 	default:
   2274 		return (if_get_counter_default(ifp, cnt));
   2275 	}
   2276 }
   2277 #endif
   2278 
   2279 static int
   2280 ena_media_change(struct ifnet *ifp)
   2281 {
   2282 	/* Media Change is not supported by firmware */
   2283 	return (0);
   2284 }
   2285 
   2286 static void
   2287 ena_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   2288 {
   2289 	struct ena_adapter *adapter = if_getsoftc(ifp);
   2290 	ena_trace(ENA_DBG, "enter");
   2291 
   2292 	mutex_enter(&adapter->global_mtx);
   2293 
   2294 	ifmr->ifm_status = IFM_AVALID;
   2295 	ifmr->ifm_active = IFM_ETHER;
   2296 
   2297 	if (!adapter->link_status) {
   2298 		mutex_exit(&adapter->global_mtx);
   2299 		ena_trace(ENA_INFO, "link_status = false");
   2300 		return;
   2301 	}
   2302 
   2303 	ifmr->ifm_status |= IFM_ACTIVE;
   2304 	ifmr->ifm_active |= IFM_10G_T | IFM_FDX;
   2305 
   2306 	mutex_exit(&adapter->global_mtx);
   2307 }
   2308 
   2309 static int
   2310 ena_init(struct ifnet *ifp)
   2311 {
   2312 	struct ena_adapter *adapter = if_getsoftc(ifp);
   2313 
   2314 	if (!adapter->up) {
   2315 		rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2316 		ena_up(adapter);
   2317 		rw_exit(&adapter->ioctl_sx);
   2318 	}
   2319 
   2320 	return 0;
   2321 }
   2322 
   2323 static int
   2324 ena_ioctl(struct ifnet *ifp, u_long command, void *data)
   2325 {
   2326 	struct ena_adapter *adapter;
   2327 	struct ifreq *ifr;
   2328 	int rc;
   2329 
   2330 	adapter = ifp->if_softc;
   2331 	ifr = (struct ifreq *)data;
   2332 
   2333 	/*
   2334 	 * Acquiring lock to prevent from running up and down routines parallel.
   2335 	 */
   2336 	rc = 0;
   2337 	switch (command) {
   2338 	case SIOCSIFMTU:
   2339 		if (ifp->if_mtu == ifr->ifr_mtu)
   2340 			break;
   2341 		rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2342 		ena_down(adapter);
   2343 
   2344 		ena_change_mtu(ifp, ifr->ifr_mtu);
   2345 
   2346 		rc = ena_up(adapter);
   2347 		rw_exit(&adapter->ioctl_sx);
   2348 		break;
   2349 
   2350 	case SIOCSIFFLAGS:
   2351 		if ((ifp->if_flags & IFF_UP) != 0) {
   2352 			if ((if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
   2353 				if ((ifp->if_flags & (IFF_PROMISC |
   2354 				    IFF_ALLMULTI)) != 0) {
   2355 					device_printf(adapter->pdev,
   2356 					    "ioctl promisc/allmulti\n");
   2357 				}
   2358 			} else {
   2359 				rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2360 				rc = ena_up(adapter);
   2361 				rw_exit(&adapter->ioctl_sx);
   2362 			}
   2363 		} else {
   2364 			if ((if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
   2365 				rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2366 				ena_down(adapter);
   2367 				rw_exit(&adapter->ioctl_sx);
   2368 			}
   2369 		}
   2370 		break;
   2371 
   2372 	case SIOCADDMULTI:
   2373 	case SIOCDELMULTI:
   2374 		break;
   2375 
   2376 	case SIOCSIFMEDIA:
   2377 	case SIOCGIFMEDIA:
   2378 		rc = ifmedia_ioctl(ifp, ifr, &adapter->media, command);
   2379 		break;
   2380 
   2381 	case SIOCSIFCAP:
   2382 		{
   2383 			struct ifcapreq *ifcr = data;
   2384 			int reinit = 0;
   2385 
   2386 			if (ifcr->ifcr_capenable != ifp->if_capenable) {
   2387 				ifp->if_capenable = ifcr->ifcr_capenable;
   2388 				reinit = 1;
   2389 			}
   2390 
   2391 			if ((reinit != 0) &&
   2392 			    ((if_getdrvflags(ifp) & IFF_RUNNING) != 0)) {
   2393 				rw_enter(&adapter->ioctl_sx, RW_WRITER);
   2394 				ena_down(adapter);
   2395 				rc = ena_up(adapter);
   2396 				rw_exit(&adapter->ioctl_sx);
   2397 			}
   2398 		}
   2399 
   2400 		break;
   2401 	default:
   2402 		rc = ether_ioctl(ifp, command, data);
   2403 		break;
   2404 	}
   2405 
   2406 	return (rc);
   2407 }
   2408 
   2409 static int
   2410 ena_get_dev_offloads(struct ena_com_dev_get_features_ctx *feat)
   2411 {
   2412 	int caps = 0;
   2413 
   2414 	if ((feat->offload.tx &
   2415 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
   2416 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK |
   2417 		ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK)) != 0)
   2418 		caps |= IFCAP_CSUM_IPv4_Tx;
   2419 
   2420 	if ((feat->offload.tx &
   2421 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_FULL_MASK |
   2422 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV6_CSUM_PART_MASK)) != 0)
   2423 		caps |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
   2424 
   2425 	if ((feat->offload.tx &
   2426 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV4_MASK) != 0)
   2427 		caps |= IFCAP_TSOv4;
   2428 
   2429 	if ((feat->offload.tx &
   2430 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TSO_IPV6_MASK) != 0)
   2431 		caps |= IFCAP_TSOv6;
   2432 
   2433 	if ((feat->offload.rx_supported &
   2434 	    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV4_CSUM_MASK |
   2435 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L3_CSUM_IPV4_MASK)) != 0)
   2436 		caps |= IFCAP_CSUM_IPv4_Rx;
   2437 
   2438 	if ((feat->offload.rx_supported &
   2439 	    ENA_ADMIN_FEATURE_OFFLOAD_DESC_RX_L4_IPV6_CSUM_MASK) != 0)
   2440 		caps |= IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   2441 
   2442 	caps |= IFCAP_LRO;
   2443 
   2444 	return (caps);
   2445 }
   2446 
   2447 static void
   2448 ena_update_host_info(struct ena_admin_host_info *host_info, struct ifnet *ifp)
   2449 {
   2450 
   2451 	host_info->supported_network_features[0] =
   2452 	    (uint32_t)if_getcapabilities(ifp);
   2453 }
   2454 
   2455 static void
   2456 ena_update_hwassist(struct ena_adapter *adapter)
   2457 {
   2458 	struct ifnet *ifp = adapter->ifp;
   2459 	uint32_t feat = adapter->tx_offload_cap;
   2460 	int cap = if_getcapenable(ifp);
   2461 	int flags = 0;
   2462 
   2463 	if_clearhwassist(ifp);
   2464 
   2465 	if ((cap & (IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx))
   2466 	    != 0) {
   2467 		if ((feat &
   2468 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L3_CSUM_IPV4_MASK) != 0)
   2469 			flags |= M_CSUM_IPv4;
   2470 		if ((feat &
   2471 		    (ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_FULL_MASK |
   2472 		    ENA_ADMIN_FEATURE_OFFLOAD_DESC_TX_L4_IPV4_CSUM_PART_MASK)) != 0)
   2473 			flags |= M_CSUM_TCPv4 | M_CSUM_UDPv4;
   2474 	}
   2475 
   2476 	if ((cap & IFCAP_CSUM_TCPv6_Tx) != 0)
   2477 		flags |= M_CSUM_TCPv6;
   2478 
   2479 	if ((cap & IFCAP_CSUM_UDPv6_Tx) != 0)
   2480 		flags |= M_CSUM_UDPv6;
   2481 
   2482 	if ((cap & IFCAP_TSOv4) != 0)
   2483 		flags |= M_CSUM_TSOv4;
   2484 
   2485 	if ((cap & IFCAP_TSOv6) != 0)
   2486 		flags |= M_CSUM_TSOv6;
   2487 
   2488 	if_sethwassistbits(ifp, flags, 0);
   2489 }
   2490 
   2491 static int
   2492 ena_setup_ifnet(device_t pdev, struct ena_adapter *adapter,
   2493     struct ena_com_dev_get_features_ctx *feat)
   2494 {
   2495 	struct ifnet *ifp;
   2496 	int caps = 0;
   2497 
   2498 	ifp = adapter->ifp = &adapter->sc_ec.ec_if;
   2499 	if (unlikely(ifp == NULL)) {
   2500 		ena_trace(ENA_ALERT, "can not allocate ifnet structure\n");
   2501 		return (ENXIO);
   2502 	}
   2503 	if_initname(ifp, "ena", device_unit(pdev));
   2504 	if_setdev(ifp, pdev);
   2505 	if_setsoftc(ifp, adapter);
   2506 
   2507 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
   2508 	if_setinitfn(ifp, ena_init);
   2509 	if_settransmitfn(ifp, ena_mq_start);
   2510 #if 0
   2511 	if_setqflushfn(ifp, ena_qflush);
   2512 #endif
   2513 	if_setioctlfn(ifp, ena_ioctl);
   2514 #if 0
   2515 	if_setgetcounterfn(ifp, ena_get_counter);
   2516 #endif
   2517 
   2518 	if_setsendqlen(ifp, adapter->tx_ring_size);
   2519 	if_setsendqready(ifp);
   2520 	if_setmtu(ifp, ETHERMTU);
   2521 	if_setbaudrate(ifp, 0);
   2522 	/* Zeroize capabilities... */
   2523 	if_setcapabilities(ifp, 0);
   2524 	if_setcapenable(ifp, 0);
   2525 	/* check hardware support */
   2526 	caps = ena_get_dev_offloads(feat);
   2527 	/* ... and set them */
   2528 	if_setcapabilitiesbit(ifp, caps, 0);
   2529 	adapter->sc_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   2530 
   2531 #if 0
   2532 	/* TSO parameters */
   2533 	/* XXX no limits on NetBSD, guarded by virtue of dmamap load failing */
   2534 	ifp->if_hw_tsomax = ENA_TSO_MAXSIZE -
   2535 	    (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
   2536 	ifp->if_hw_tsomaxsegcount = adapter->max_tx_sgl_size - 1;
   2537 	ifp->if_hw_tsomaxsegsize = ENA_TSO_MAXSIZE;
   2538 #endif
   2539 
   2540 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
   2541 	if_setcapenable(ifp, if_getcapabilities(ifp));
   2542 
   2543 	/*
   2544 	 * Specify the media types supported by this adapter and register
   2545 	 * callbacks to update media and link information
   2546 	 */
   2547 	ifmedia_init(&adapter->media, IFM_IMASK,
   2548 	    ena_media_change, ena_media_status);
   2549 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   2550 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   2551 
   2552 	if_attach(ifp);
   2553 	if_deferred_start_init(ifp, NULL);
   2554 
   2555 	ether_ifattach(ifp, adapter->mac_addr);
   2556 
   2557 	return (0);
   2558 }
   2559 
   2560 static void
   2561 ena_down(struct ena_adapter *adapter)
   2562 {
   2563 	int rc;
   2564 
   2565 	if (adapter->up) {
   2566 		device_printf(adapter->pdev, "device is going DOWN\n");
   2567 
   2568 		callout_halt(&adapter->timer_service, &adapter->global_mtx);
   2569 
   2570 		adapter->up = false;
   2571 		if_setdrvflagbits(adapter->ifp, IFF_OACTIVE,
   2572 		    IFF_RUNNING);
   2573 
   2574 		ena_free_io_irq(adapter);
   2575 
   2576 		if (adapter->trigger_reset) {
   2577 			rc = ena_com_dev_reset(adapter->ena_dev,
   2578 			    adapter->reset_reason);
   2579 			if (unlikely(rc != 0))
   2580 				device_printf(adapter->pdev,
   2581 				    "Device reset failed\n");
   2582 		}
   2583 
   2584 		ena_destroy_all_io_queues(adapter);
   2585 
   2586 		ena_free_all_tx_bufs(adapter);
   2587 		ena_free_all_rx_bufs(adapter);
   2588 		ena_free_all_tx_resources(adapter);
   2589 		ena_free_all_rx_resources(adapter);
   2590 
   2591 		counter_u64_add(adapter->dev_stats.interface_down, 1);
   2592 	}
   2593 }
   2594 
   2595 static void
   2596 ena_tx_csum(struct ena_com_tx_ctx *ena_tx_ctx, struct mbuf *mbuf)
   2597 {
   2598 	struct ena_com_tx_meta *ena_meta;
   2599 	struct ether_vlan_header *eh;
   2600 	u32 mss;
   2601 	bool offload;
   2602 	uint16_t etype;
   2603 	int ehdrlen;
   2604 	struct ip *ip;
   2605 	int iphlen;
   2606 	struct tcphdr *th;
   2607 
   2608 	offload = false;
   2609 	ena_meta = &ena_tx_ctx->ena_meta;
   2610 
   2611 #if 0
   2612 	u32 mss = mbuf->m_pkthdr.tso_segsz;
   2613 
   2614 	if (mss != 0)
   2615 		offload = true;
   2616 #else
   2617 	mss = mbuf->m_pkthdr.len;	/* XXX don't have tso_segsz */
   2618 #endif
   2619 
   2620 	if ((mbuf->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0)
   2621 		offload = true;
   2622 
   2623 	if ((mbuf->m_pkthdr.csum_flags & CSUM_OFFLOAD) != 0)
   2624 		offload = true;
   2625 
   2626 	if (!offload) {
   2627 		ena_tx_ctx->meta_valid = 0;
   2628 		return;
   2629 	}
   2630 
   2631 	/* Determine where frame payload starts. */
   2632 	eh = mtod(mbuf, struct ether_vlan_header *);
   2633 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   2634 		etype = ntohs(eh->evl_proto);
   2635 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   2636 	} else {
   2637 		etype = htons(eh->evl_encap_proto);
   2638 		ehdrlen = ETHER_HDR_LEN;
   2639 	}
   2640 
   2641 	ip = (struct ip *)(mbuf->m_data + ehdrlen);
   2642 	iphlen = ip->ip_hl << 2;
   2643 	th = (struct tcphdr *)((vaddr_t)ip + iphlen);
   2644 
   2645 	if ((mbuf->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) {
   2646 		ena_tx_ctx->l3_csum_enable = 1;
   2647 	}
   2648 	if ((mbuf->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0) {
   2649 		ena_tx_ctx->tso_enable = 1;
   2650 		ena_meta->l4_hdr_len = (th->th_off);
   2651 	}
   2652 
   2653 	switch (etype) {
   2654 	case ETHERTYPE_IP:
   2655 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV4;
   2656 		if ((ip->ip_off & htons(IP_DF)) != 0)
   2657 			ena_tx_ctx->df = 1;
   2658 		break;
   2659 	case ETHERTYPE_IPV6:
   2660 		ena_tx_ctx->l3_proto = ENA_ETH_IO_L3_PROTO_IPV6;
   2661 
   2662 	default:
   2663 		break;
   2664 	}
   2665 
   2666 	if (ip->ip_p == IPPROTO_TCP) {
   2667 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_TCP;
   2668 		if ((mbuf->m_pkthdr.csum_flags &
   2669 		    (M_CSUM_TCPv4 | M_CSUM_TCPv6)) != 0)
   2670 			ena_tx_ctx->l4_csum_enable = 1;
   2671 		else
   2672 			ena_tx_ctx->l4_csum_enable = 0;
   2673 	} else if (ip->ip_p == IPPROTO_UDP) {
   2674 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UDP;
   2675 		if ((mbuf->m_pkthdr.csum_flags &
   2676 		    (M_CSUM_UDPv4 | M_CSUM_UDPv6)) != 0)
   2677 			ena_tx_ctx->l4_csum_enable = 1;
   2678 		else
   2679 			ena_tx_ctx->l4_csum_enable = 0;
   2680 	} else {
   2681 		ena_tx_ctx->l4_proto = ENA_ETH_IO_L4_PROTO_UNKNOWN;
   2682 		ena_tx_ctx->l4_csum_enable = 0;
   2683 	}
   2684 
   2685 	ena_meta->mss = mss;
   2686 	ena_meta->l3_hdr_len = iphlen;
   2687 	ena_meta->l3_hdr_offset = ehdrlen;
   2688 	ena_tx_ctx->meta_valid = 1;
   2689 }
   2690 
   2691 static int
   2692 ena_check_and_collapse_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
   2693 {
   2694 	struct ena_adapter *adapter;
   2695 	struct mbuf *collapsed_mbuf;
   2696 	int num_frags;
   2697 
   2698 	adapter = tx_ring->adapter;
   2699 	num_frags = ena_mbuf_count(*mbuf);
   2700 
   2701 	/* One segment must be reserved for configuration descriptor. */
   2702 	if (num_frags < adapter->max_tx_sgl_size)
   2703 		return (0);
   2704 	counter_u64_add(tx_ring->tx_stats.collapse, 1);
   2705 
   2706 	collapsed_mbuf = m_collapse(*mbuf, M_NOWAIT,
   2707 	    adapter->max_tx_sgl_size - 1);
   2708 	if (unlikely(collapsed_mbuf == NULL)) {
   2709 		counter_u64_add(tx_ring->tx_stats.collapse_err, 1);
   2710 		return (ENOMEM);
   2711 	}
   2712 
   2713 	/* If mbuf was collapsed succesfully, original mbuf is released. */
   2714 	*mbuf = collapsed_mbuf;
   2715 
   2716 	return (0);
   2717 }
   2718 
   2719 static int
   2720 ena_xmit_mbuf(struct ena_ring *tx_ring, struct mbuf **mbuf)
   2721 {
   2722 	struct ena_adapter *adapter;
   2723 	struct ena_tx_buffer *tx_info;
   2724 	struct ena_com_tx_ctx ena_tx_ctx;
   2725 	struct ena_com_dev *ena_dev;
   2726 	struct ena_com_buf *ena_buf;
   2727 	struct ena_com_io_sq* io_sq;
   2728 	void *push_hdr;
   2729 	uint16_t next_to_use;
   2730 	uint16_t req_id;
   2731 	uint16_t ena_qid;
   2732 	uint32_t header_len;
   2733 	int i, rc;
   2734 	int nb_hw_desc;
   2735 
   2736 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
   2737 	adapter = tx_ring->que->adapter;
   2738 	ena_dev = adapter->ena_dev;
   2739 	io_sq = &ena_dev->io_sq_queues[ena_qid];
   2740 
   2741 	rc = ena_check_and_collapse_mbuf(tx_ring, mbuf);
   2742 	if (unlikely(rc != 0)) {
   2743 		ena_trace(ENA_WARNING,
   2744 		    "Failed to collapse mbuf! err: %d", rc);
   2745 		return (rc);
   2746 	}
   2747 
   2748 	next_to_use = tx_ring->next_to_use;
   2749 	req_id = tx_ring->free_tx_ids[next_to_use];
   2750 	tx_info = &tx_ring->tx_buffer_info[req_id];
   2751 
   2752 	tx_info->mbuf = *mbuf;
   2753 	tx_info->num_of_bufs = 0;
   2754 
   2755 	ena_buf = tx_info->bufs;
   2756 
   2757 	ena_trace(ENA_DBG | ENA_TXPTH, "Tx: %d bytes", (*mbuf)->m_pkthdr.len);
   2758 
   2759 	/*
   2760 	 * header_len is just a hint for the device. Because FreeBSD is not
   2761 	 * giving us information about packet header length and it is not
   2762 	 * guaranteed that all packet headers will be in the 1st mbuf, setting
   2763 	 * header_len to 0 is making the device ignore this value and resolve
   2764 	 * header on it's own.
   2765 	 */
   2766 	header_len = 0;
   2767 	push_hdr = NULL;
   2768 
   2769 	rc = bus_dmamap_load_mbuf(adapter->sc_dmat, tx_info->map,
   2770 	    *mbuf, BUS_DMA_NOWAIT);
   2771 
   2772 	if (unlikely((rc != 0) || (tx_info->map->dm_nsegs == 0))) {
   2773 		ena_trace(ENA_WARNING,
   2774 		    "dmamap load failed! err: %d nsegs: %d", rc,
   2775 		    tx_info->map->dm_nsegs);
   2776 		counter_u64_add(tx_ring->tx_stats.dma_mapping_err, 1);
   2777 		tx_info->mbuf = NULL;
   2778 		if (rc == ENOMEM)
   2779 			return (ENA_COM_NO_MEM);
   2780 		else
   2781 			return (ENA_COM_INVAL);
   2782 	}
   2783 
   2784 	for (i = 0; i < tx_info->map->dm_nsegs; i++) {
   2785 		ena_buf->len = tx_info->map->dm_segs[i].ds_len;
   2786 		ena_buf->paddr = tx_info->map->dm_segs[i].ds_addr;
   2787 		ena_buf++;
   2788 	}
   2789 	tx_info->num_of_bufs = tx_info->map->dm_nsegs;
   2790 
   2791 	memset(&ena_tx_ctx, 0x0, sizeof(struct ena_com_tx_ctx));
   2792 	ena_tx_ctx.ena_bufs = tx_info->bufs;
   2793 	ena_tx_ctx.push_header = push_hdr;
   2794 	ena_tx_ctx.num_bufs = tx_info->num_of_bufs;
   2795 	ena_tx_ctx.req_id = req_id;
   2796 	ena_tx_ctx.header_len = header_len;
   2797 
   2798 	/* Set flags and meta data */
   2799 	ena_tx_csum(&ena_tx_ctx, *mbuf);
   2800 	/* Prepare the packet's descriptors and send them to device */
   2801 	rc = ena_com_prepare_tx(io_sq, &ena_tx_ctx, &nb_hw_desc);
   2802 	if (unlikely(rc != 0)) {
   2803 		device_printf(adapter->pdev, "failed to prepare tx bufs\n");
   2804 		counter_u64_add(tx_ring->tx_stats.prepare_ctx_err, 1);
   2805 		goto dma_error;
   2806 	}
   2807 
   2808 	counter_enter();
   2809 	counter_u64_add_protected(tx_ring->tx_stats.cnt, 1);
   2810 	counter_u64_add_protected(tx_ring->tx_stats.bytes,
   2811 	    (*mbuf)->m_pkthdr.len);
   2812 
   2813 	counter_u64_add_protected(adapter->hw_stats.tx_packets, 1);
   2814 	counter_u64_add_protected(adapter->hw_stats.tx_bytes,
   2815 	    (*mbuf)->m_pkthdr.len);
   2816 	counter_exit();
   2817 
   2818 	tx_info->tx_descs = nb_hw_desc;
   2819 	getbinuptime(&tx_info->timestamp);
   2820 	tx_info->print_once = true;
   2821 
   2822 	tx_ring->next_to_use = ENA_TX_RING_IDX_NEXT(next_to_use,
   2823 	    tx_ring->ring_size);
   2824 
   2825 	bus_dmamap_sync(adapter->sc_dmat, tx_info->map, 0,
   2826 	    tx_info->map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2827 
   2828 	return (0);
   2829 
   2830 dma_error:
   2831 	tx_info->mbuf = NULL;
   2832 	bus_dmamap_unload(adapter->sc_dmat, tx_info->map);
   2833 
   2834 	return (rc);
   2835 }
   2836 
   2837 static void
   2838 ena_start_xmit(struct ena_ring *tx_ring)
   2839 {
   2840 	struct mbuf *mbuf;
   2841 	struct ena_adapter *adapter = tx_ring->adapter;
   2842 	struct ena_com_io_sq* io_sq;
   2843 	int ena_qid;
   2844 	int acum_pkts = 0;
   2845 	int ret = 0;
   2846 
   2847 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_RUNNING) == 0))
   2848 		return;
   2849 
   2850 	if (unlikely(!adapter->link_status))
   2851 		return;
   2852 
   2853 	ena_qid = ENA_IO_TXQ_IDX(tx_ring->que->id);
   2854 	io_sq = &adapter->ena_dev->io_sq_queues[ena_qid];
   2855 
   2856 	while ((mbuf = drbr_peek(adapter->ifp, tx_ring->br)) != NULL) {
   2857 		ena_trace(ENA_DBG | ENA_TXPTH, "\ndequeued mbuf %p with flags %#x and"
   2858 		    " header csum flags %#jx",
   2859 		    mbuf, mbuf->m_flags, (uint64_t)mbuf->m_pkthdr.csum_flags);
   2860 
   2861 		if (unlikely(!ena_com_sq_have_enough_space(io_sq,
   2862 		    ENA_TX_CLEANUP_THRESHOLD)))
   2863 			ena_tx_cleanup(tx_ring);
   2864 
   2865 		if (unlikely((ret = ena_xmit_mbuf(tx_ring, &mbuf)) != 0)) {
   2866 			if (ret == ENA_COM_NO_MEM) {
   2867 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
   2868 			} else if (ret == ENA_COM_NO_SPACE) {
   2869 				drbr_putback(adapter->ifp, tx_ring->br, mbuf);
   2870 			} else {
   2871 				m_freem(mbuf);
   2872 				drbr_advance(adapter->ifp, tx_ring->br);
   2873 			}
   2874 
   2875 			break;
   2876 		}
   2877 
   2878 		drbr_advance(adapter->ifp, tx_ring->br);
   2879 
   2880 		if (unlikely((if_getdrvflags(adapter->ifp) &
   2881 		    IFF_RUNNING) == 0))
   2882 			return;
   2883 
   2884 		acum_pkts++;
   2885 
   2886 		/*
   2887 		 * If there's a BPF listener, bounce a copy of this frame
   2888 		 * to him.
   2889 		 */
   2890 		bpf_mtap(adapter->ifp, mbuf, BPF_D_OUT);
   2891 
   2892 		if (unlikely(acum_pkts == DB_THRESHOLD)) {
   2893 			acum_pkts = 0;
   2894 			wmb();
   2895 			/* Trigger the dma engine */
   2896 			ena_com_write_sq_doorbell(io_sq);
   2897 			counter_u64_add(tx_ring->tx_stats.doorbells, 1);
   2898 		}
   2899 
   2900 	}
   2901 
   2902 	if (likely(acum_pkts != 0)) {
   2903 		wmb();
   2904 		/* Trigger the dma engine */
   2905 		ena_com_write_sq_doorbell(io_sq);
   2906 		counter_u64_add(tx_ring->tx_stats.doorbells, 1);
   2907 	}
   2908 
   2909 	if (!ena_com_sq_have_enough_space(io_sq, ENA_TX_CLEANUP_THRESHOLD))
   2910 		ena_tx_cleanup(tx_ring);
   2911 }
   2912 
   2913 static void
   2914 ena_deferred_mq_start(struct work *wk, void *arg)
   2915 {
   2916 	struct ena_ring *tx_ring = (struct ena_ring *)arg;
   2917 	struct ifnet *ifp = tx_ring->adapter->ifp;
   2918 
   2919 	atomic_swap_uint(&tx_ring->task_pending, 0);
   2920 
   2921 	while (!drbr_empty(ifp, tx_ring->br) &&
   2922 	    (if_getdrvflags(ifp) & IFF_RUNNING) != 0) {
   2923 		ENA_RING_MTX_LOCK(tx_ring);
   2924 		ena_start_xmit(tx_ring);
   2925 		ENA_RING_MTX_UNLOCK(tx_ring);
   2926 	}
   2927 }
   2928 
   2929 static int
   2930 ena_mq_start(struct ifnet *ifp, struct mbuf *m)
   2931 {
   2932 	struct ena_adapter *adapter = ifp->if_softc;
   2933 	struct ena_ring *tx_ring;
   2934 	int ret, is_drbr_empty;
   2935 	uint32_t i;
   2936 
   2937 	if (unlikely((if_getdrvflags(adapter->ifp) & IFF_RUNNING) == 0))
   2938 		return (ENODEV);
   2939 
   2940 	/* Which queue to use */
   2941 	/*
   2942 	 * If everything is setup correctly, it should be the
   2943 	 * same bucket that the current CPU we're on is.
   2944 	 * It should improve performance.
   2945 	 */
   2946 #if 0
   2947 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
   2948 #ifdef	RSS
   2949 		if (rss_hash2bucket(m->m_pkthdr.flowid,
   2950 		    M_HASHTYPE_GET(m), &i) == 0) {
   2951 			i = i % adapter->num_queues;
   2952 
   2953 		} else
   2954 #endif
   2955 		{
   2956 			i = m->m_pkthdr.flowid % adapter->num_queues;
   2957 		}
   2958 	} else {
   2959 #endif
   2960 		i = cpu_index(curcpu()) % adapter->num_queues;
   2961 #if 0
   2962 	}
   2963 #endif
   2964 	tx_ring = &adapter->tx_ring[i];
   2965 
   2966 	/* Check if drbr is empty before putting packet */
   2967 	is_drbr_empty = drbr_empty(ifp, tx_ring->br);
   2968 	ret = drbr_enqueue(ifp, tx_ring->br, m);
   2969 	if (unlikely(ret != 0)) {
   2970 		if (atomic_cas_uint(&tx_ring->task_pending, 0, 1) == 0)
   2971 			workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
   2972 			    curcpu());
   2973 		return (ret);
   2974 	}
   2975 
   2976 	if ((is_drbr_empty != 0) && (ENA_RING_MTX_TRYLOCK(tx_ring) != 0)) {
   2977 		ena_start_xmit(tx_ring);
   2978 		ENA_RING_MTX_UNLOCK(tx_ring);
   2979 	} else {
   2980 		if (atomic_cas_uint(&tx_ring->task_pending, 0, 1) == 0)
   2981 			workqueue_enqueue(tx_ring->enqueue_tq, &tx_ring->enqueue_task,
   2982 			    curcpu());
   2983 	}
   2984 
   2985 	return (0);
   2986 }
   2987 
   2988 #if 0
   2989 static void
   2990 ena_qflush(struct ifnet *ifp)
   2991 {
   2992 	struct ena_adapter *adapter = ifp->if_softc;
   2993 	struct ena_ring *tx_ring = adapter->tx_ring;
   2994 	int i;
   2995 
   2996 	for(i = 0; i < adapter->num_queues; ++i, ++tx_ring)
   2997 		if (!drbr_empty(ifp, tx_ring->br)) {
   2998 			ENA_RING_MTX_LOCK(tx_ring);
   2999 			drbr_flush(ifp, tx_ring->br);
   3000 			ENA_RING_MTX_UNLOCK(tx_ring);
   3001 		}
   3002 
   3003 	if_qflush(ifp);
   3004 }
   3005 #endif
   3006 
   3007 static int
   3008 ena_calc_io_queue_num(struct pci_attach_args *pa,
   3009     struct ena_adapter *adapter,
   3010     struct ena_com_dev_get_features_ctx *get_feat_ctx)
   3011 {
   3012 	int io_sq_num, io_cq_num, io_queue_num;
   3013 
   3014 	io_sq_num = get_feat_ctx->max_queues.max_sq_num;
   3015 	io_cq_num = get_feat_ctx->max_queues.max_cq_num;
   3016 
   3017 	io_queue_num = min_t(int, mp_ncpus, ENA_MAX_NUM_IO_QUEUES);
   3018 	io_queue_num = min_t(int, io_queue_num, io_sq_num);
   3019 	io_queue_num = min_t(int, io_queue_num, io_cq_num);
   3020 	/* 1 IRQ for for mgmnt and 1 IRQ for each TX/RX pair */
   3021 	io_queue_num = min_t(int, io_queue_num,
   3022 	    pci_msix_count(pa->pa_pc, pa->pa_tag) - 1);
   3023 #ifdef	RSS
   3024 	io_queue_num = min_t(int, io_queue_num, rss_getnumbuckets());
   3025 #endif
   3026 
   3027 	return (io_queue_num);
   3028 }
   3029 
   3030 static int
   3031 ena_calc_queue_size(struct ena_adapter *adapter, uint16_t *max_tx_sgl_size,
   3032     uint16_t *max_rx_sgl_size, struct ena_com_dev_get_features_ctx *feat)
   3033 {
   3034 	uint32_t queue_size = ENA_DEFAULT_RING_SIZE;
   3035 	uint32_t v;
   3036 	uint32_t q;
   3037 
   3038 	queue_size = min_t(uint32_t, queue_size,
   3039 	    feat->max_queues.max_cq_depth);
   3040 	queue_size = min_t(uint32_t, queue_size,
   3041 	    feat->max_queues.max_sq_depth);
   3042 
   3043 	/* round down to the nearest power of 2 */
   3044 	v = queue_size;
   3045 	while (v != 0) {
   3046 		if (powerof2(queue_size) != 0)
   3047 			break;
   3048 		v /= 2;
   3049 		q = rounddown2(queue_size, v);
   3050 		if (q != 0) {
   3051 			queue_size = q;
   3052 			break;
   3053 		}
   3054 	}
   3055 
   3056 	if (unlikely(queue_size == 0)) {
   3057 		device_printf(adapter->pdev, "Invalid queue size\n");
   3058 		return (ENA_COM_FAULT);
   3059 	}
   3060 
   3061 	*max_tx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
   3062 	    feat->max_queues.max_packet_tx_descs);
   3063 	*max_rx_sgl_size = min_t(uint16_t, ENA_PKT_MAX_BUFS,
   3064 	    feat->max_queues.max_packet_rx_descs);
   3065 
   3066 	return (queue_size);
   3067 }
   3068 
   3069 #if 0
   3070 static int
   3071 ena_rss_init_default(struct ena_adapter *adapter)
   3072 {
   3073 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3074 	device_t dev = adapter->pdev;
   3075 	int qid, rc, i;
   3076 
   3077 	rc = ena_com_rss_init(ena_dev, ENA_RX_RSS_TABLE_LOG_SIZE);
   3078 	if (unlikely(rc != 0)) {
   3079 		device_printf(dev, "Cannot init indirect table\n");
   3080 		return (rc);
   3081 	}
   3082 
   3083 	for (i = 0; i < ENA_RX_RSS_TABLE_SIZE; i++) {
   3084 #ifdef	RSS
   3085 		qid = rss_get_indirection_to_bucket(i);
   3086 		qid = qid % adapter->num_queues;
   3087 #else
   3088 		qid = i % adapter->num_queues;
   3089 #endif
   3090 		rc = ena_com_indirect_table_fill_entry(ena_dev, i,
   3091 		    ENA_IO_RXQ_IDX(qid));
   3092 		if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
   3093 			device_printf(dev, "Cannot fill indirect table\n");
   3094 			goto err_rss_destroy;
   3095 		}
   3096 	}
   3097 
   3098 	rc = ena_com_fill_hash_function(ena_dev, ENA_ADMIN_CRC32, NULL,
   3099 	    ENA_HASH_KEY_SIZE, 0xFFFFFFFF);
   3100 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
   3101 		device_printf(dev, "Cannot fill hash function\n");
   3102 		goto err_rss_destroy;
   3103 	}
   3104 
   3105 	rc = ena_com_set_default_hash_ctrl(ena_dev);
   3106 	if (unlikely((rc != 0) && (rc != EOPNOTSUPP))) {
   3107 		device_printf(dev, "Cannot fill hash control\n");
   3108 		goto err_rss_destroy;
   3109 	}
   3110 
   3111 	return (0);
   3112 
   3113 err_rss_destroy:
   3114 	ena_com_rss_destroy(ena_dev);
   3115 	return (rc);
   3116 }
   3117 
   3118 static void
   3119 ena_rss_init_default_deferred(void *arg)
   3120 {
   3121 	struct ena_adapter *adapter;
   3122 	devclass_t dc;
   3123 	int max;
   3124 	int rc;
   3125 
   3126 	dc = devclass_find("ena");
   3127 	if (unlikely(dc == NULL)) {
   3128 		ena_trace(ENA_ALERT, "No devclass ena\n");
   3129 		return;
   3130 	}
   3131 
   3132 	max = devclass_get_maxunit(dc);
   3133 	while (max-- >= 0) {
   3134 		adapter = devclass_get_softc(dc, max);
   3135 		if (adapter != NULL) {
   3136 			rc = ena_rss_init_default(adapter);
   3137 			adapter->rss_support = true;
   3138 			if (unlikely(rc != 0)) {
   3139 				device_printf(adapter->pdev,
   3140 				    "WARNING: RSS was not properly initialized,"
   3141 				    " it will affect bandwidth\n");
   3142 				adapter->rss_support = false;
   3143 			}
   3144 		}
   3145 	}
   3146 }
   3147 SYSINIT(ena_rss_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, ena_rss_init_default_deferred, NULL);
   3148 #endif
   3149 
   3150 static void
   3151 ena_config_host_info(struct ena_com_dev *ena_dev)
   3152 {
   3153 	struct ena_admin_host_info *host_info;
   3154 	int rc;
   3155 
   3156 	/* Allocate only the host info */
   3157 	rc = ena_com_allocate_host_info(ena_dev);
   3158 	if (unlikely(rc != 0)) {
   3159 		ena_trace(ENA_ALERT, "Cannot allocate host info\n");
   3160 		return;
   3161 	}
   3162 
   3163 	host_info = ena_dev->host_attr.host_info;
   3164 
   3165 	host_info->os_type = ENA_ADMIN_OS_FREEBSD;
   3166 	host_info->kernel_ver = osreldate;
   3167 
   3168 	snprintf(host_info->kernel_ver_str, sizeof(host_info->kernel_ver_str),
   3169 	    "%d", osreldate);
   3170 	host_info->os_dist = 0;
   3171 	strncpy(host_info->os_dist_str, osrelease,
   3172 	    sizeof(host_info->os_dist_str) - 1);
   3173 
   3174 	host_info->driver_version =
   3175 		(DRV_MODULE_VER_MAJOR) |
   3176 		(DRV_MODULE_VER_MINOR << ENA_ADMIN_HOST_INFO_MINOR_SHIFT) |
   3177 		(DRV_MODULE_VER_SUBMINOR << ENA_ADMIN_HOST_INFO_SUB_MINOR_SHIFT);
   3178 
   3179 	rc = ena_com_set_host_attributes(ena_dev);
   3180 	if (unlikely(rc != 0)) {
   3181 		if (rc == EOPNOTSUPP)
   3182 			ena_trace(ENA_WARNING, "Cannot set host attributes\n");
   3183 		else
   3184 			ena_trace(ENA_ALERT, "Cannot set host attributes\n");
   3185 
   3186 		goto err;
   3187 	}
   3188 
   3189 	return;
   3190 
   3191 err:
   3192 	ena_com_delete_host_info(ena_dev);
   3193 }
   3194 
   3195 static int
   3196 ena_device_init(struct ena_adapter *adapter, device_t pdev,
   3197     struct ena_com_dev_get_features_ctx *get_feat_ctx, int *wd_active)
   3198 {
   3199 	struct ena_com_dev* ena_dev = adapter->ena_dev;
   3200 	bool readless_supported;
   3201 	uint32_t aenq_groups;
   3202 	int dma_width;
   3203 	int rc;
   3204 
   3205 	rc = ena_com_mmio_reg_read_request_init(ena_dev);
   3206 	if (unlikely(rc != 0)) {
   3207 		device_printf(pdev, "failed to init mmio read less\n");
   3208 		return (rc);
   3209 	}
   3210 
   3211 	/*
   3212 	 * The PCIe configuration space revision id indicate if mmio reg
   3213 	 * read is disabled
   3214 	 */
   3215 	const int rev = PCI_REVISION(adapter->sc_pa.pa_class);
   3216 	readless_supported = ((rev & ENA_MMIO_DISABLE_REG_READ) == 0);
   3217 	ena_com_set_mmio_read_mode(ena_dev, readless_supported);
   3218 
   3219 	rc = ena_com_dev_reset(ena_dev, ENA_REGS_RESET_NORMAL);
   3220 	if (unlikely(rc != 0)) {
   3221 		device_printf(pdev, "Can not reset device\n");
   3222 		goto err_mmio_read_less;
   3223 	}
   3224 
   3225 	rc = ena_com_validate_version(ena_dev);
   3226 	if (unlikely(rc != 0)) {
   3227 		device_printf(pdev, "device version is too low\n");
   3228 		goto err_mmio_read_less;
   3229 	}
   3230 
   3231 	dma_width = ena_com_get_dma_width(ena_dev);
   3232 	if (unlikely(dma_width < 0)) {
   3233 		device_printf(pdev, "Invalid dma width value %d", dma_width);
   3234 		rc = dma_width;
   3235 		goto err_mmio_read_less;
   3236 	}
   3237 	adapter->dma_width = dma_width;
   3238 
   3239 	/* ENA admin level init */
   3240 	rc = ena_com_admin_init(ena_dev, &aenq_handlers, true);
   3241 	if (unlikely(rc != 0)) {
   3242 		device_printf(pdev,
   3243 		    "Can not initialize ena admin queue with device\n");
   3244 		goto err_mmio_read_less;
   3245 	}
   3246 
   3247 	/*
   3248 	 * To enable the msix interrupts the driver needs to know the number
   3249 	 * of queues. So the driver uses polling mode to retrieve this
   3250 	 * information
   3251 	 */
   3252 	ena_com_set_admin_polling_mode(ena_dev, true);
   3253 
   3254 	ena_config_host_info(ena_dev);
   3255 
   3256 	/* Get Device Attributes */
   3257 	rc = ena_com_get_dev_attr_feat(ena_dev, get_feat_ctx);
   3258 	if (unlikely(rc != 0)) {
   3259 		device_printf(pdev,
   3260 		    "Cannot get attribute for ena device rc: %d\n", rc);
   3261 		goto err_admin_init;
   3262 	}
   3263 
   3264 	aenq_groups = BIT(ENA_ADMIN_LINK_CHANGE) | BIT(ENA_ADMIN_KEEP_ALIVE);
   3265 
   3266 	aenq_groups &= get_feat_ctx->aenq.supported_groups;
   3267 	rc = ena_com_set_aenq_config(ena_dev, aenq_groups);
   3268 	if (unlikely(rc != 0)) {
   3269 		device_printf(pdev, "Cannot configure aenq groups rc: %d\n", rc);
   3270 		goto err_admin_init;
   3271 	}
   3272 
   3273 	*wd_active = !!(aenq_groups & BIT(ENA_ADMIN_KEEP_ALIVE));
   3274 
   3275 	return (0);
   3276 
   3277 err_admin_init:
   3278 	ena_com_delete_host_info(ena_dev);
   3279 	ena_com_admin_destroy(ena_dev);
   3280 err_mmio_read_less:
   3281 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3282 
   3283 	return (rc);
   3284 }
   3285 
   3286 static int ena_enable_msix_and_set_admin_interrupts(struct ena_adapter *adapter,
   3287     int io_vectors)
   3288 {
   3289 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3290 	int rc;
   3291 
   3292 	rc = ena_enable_msix(adapter);
   3293 	if (unlikely(rc != 0)) {
   3294 		device_printf(adapter->pdev, "Error with MSI-X enablement\n");
   3295 		return (rc);
   3296 	}
   3297 
   3298 	rc = ena_request_mgmnt_irq(adapter);
   3299 	if (unlikely(rc != 0)) {
   3300 		device_printf(adapter->pdev, "Cannot setup mgmnt queue intr\n");
   3301 		goto err_disable_msix;
   3302 	}
   3303 
   3304 	ena_com_set_admin_polling_mode(ena_dev, false);
   3305 
   3306 	ena_com_admin_aenq_enable(ena_dev);
   3307 
   3308 	return (0);
   3309 
   3310 err_disable_msix:
   3311 	ena_disable_msix(adapter);
   3312 
   3313 	return (rc);
   3314 }
   3315 
   3316 /* Function called on ENA_ADMIN_KEEP_ALIVE event */
   3317 static void ena_keep_alive_wd(void *adapter_data,
   3318     struct ena_admin_aenq_entry *aenq_e)
   3319 {
   3320 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
   3321 	struct ena_admin_aenq_keep_alive_desc *desc;
   3322 	sbintime_t stime;
   3323 	uint64_t rx_drops;
   3324 
   3325 	desc = (struct ena_admin_aenq_keep_alive_desc *)aenq_e;
   3326 
   3327 	rx_drops = ((uint64_t)desc->rx_drops_high << 32) | desc->rx_drops_low;
   3328 	counter_u64_zero(adapter->hw_stats.rx_drops);
   3329 	counter_u64_add(adapter->hw_stats.rx_drops, rx_drops);
   3330 
   3331 	stime = getsbinuptime();
   3332 	(void) atomic_swap_64(&adapter->keep_alive_timestamp, stime);
   3333 }
   3334 
   3335 /* Check for keep alive expiration */
   3336 static void check_for_missing_keep_alive(struct ena_adapter *adapter)
   3337 {
   3338 	sbintime_t timestamp, time;
   3339 
   3340 	if (adapter->wd_active == 0)
   3341 		return;
   3342 
   3343 	if (likely(adapter->keep_alive_timeout == 0))
   3344 		return;
   3345 
   3346 	/* FreeBSD uses atomic_load_acq_64() in place of the membar + read */
   3347 	membar_sync();
   3348 	timestamp = adapter->keep_alive_timestamp;
   3349 
   3350 	time = getsbinuptime() - timestamp;
   3351 	if (unlikely(time > adapter->keep_alive_timeout)) {
   3352 		device_printf(adapter->pdev,
   3353 		    "Keep alive watchdog timeout.\n");
   3354 		counter_u64_add(adapter->dev_stats.wd_expired, 1);
   3355 		adapter->reset_reason = ENA_REGS_RESET_KEEP_ALIVE_TO;
   3356 		adapter->trigger_reset = true;
   3357 	}
   3358 }
   3359 
   3360 /* Check if admin queue is enabled */
   3361 static void check_for_admin_com_state(struct ena_adapter *adapter)
   3362 {
   3363 	if (unlikely(ena_com_get_admin_running_state(adapter->ena_dev) ==
   3364 	    false)) {
   3365 		device_printf(adapter->pdev,
   3366 		    "ENA admin queue is not in running state!\n");
   3367 		counter_u64_add(adapter->dev_stats.admin_q_pause, 1);
   3368 		adapter->reset_reason = ENA_REGS_RESET_ADMIN_TO;
   3369 		adapter->trigger_reset = true;
   3370 	}
   3371 }
   3372 
   3373 static int
   3374 check_missing_comp_in_queue(struct ena_adapter *adapter,
   3375     struct ena_ring *tx_ring)
   3376 {
   3377 	struct bintime curtime, time;
   3378 	struct ena_tx_buffer *tx_buf;
   3379 	uint32_t missed_tx = 0;
   3380 	int i;
   3381 
   3382 	getbinuptime(&curtime);
   3383 
   3384 	for (i = 0; i < tx_ring->ring_size; i++) {
   3385 		tx_buf = &tx_ring->tx_buffer_info[i];
   3386 
   3387 		if (bintime_isset(&tx_buf->timestamp) == 0)
   3388 			continue;
   3389 
   3390 		time = curtime;
   3391 		bintime_sub(&time, &tx_buf->timestamp);
   3392 
   3393 		/* Check again if packet is still waiting */
   3394 		if (unlikely(bttosbt(time) > adapter->missing_tx_timeout)) {
   3395 
   3396 			if (!tx_buf->print_once)
   3397 				ena_trace(ENA_WARNING, "Found a Tx that wasn't "
   3398 				    "completed on time, qid %d, index %d.\n",
   3399 				    tx_ring->qid, i);
   3400 
   3401 			tx_buf->print_once = true;
   3402 			missed_tx++;
   3403 			counter_u64_add(tx_ring->tx_stats.missing_tx_comp, 1);
   3404 
   3405 			if (unlikely(missed_tx >
   3406 			    adapter->missing_tx_threshold)) {
   3407 				device_printf(adapter->pdev,
   3408 				    "The number of lost tx completion "
   3409 				    "is above the threshold (%d > %d). "
   3410 				    "Reset the device\n",
   3411 				    missed_tx, adapter->missing_tx_threshold);
   3412 				adapter->reset_reason =
   3413 				    ENA_REGS_RESET_MISS_TX_CMPL;
   3414 				adapter->trigger_reset = true;
   3415 				return (EIO);
   3416 			}
   3417 		}
   3418 	}
   3419 
   3420 	return (0);
   3421 }
   3422 
   3423 /*
   3424  * Check for TX which were not completed on time.
   3425  * Timeout is defined by "missing_tx_timeout".
   3426  * Reset will be performed if number of incompleted
   3427  * transactions exceeds "missing_tx_threshold".
   3428  */
   3429 static void
   3430 check_for_missing_tx_completions(struct ena_adapter *adapter)
   3431 {
   3432 	struct ena_ring *tx_ring;
   3433 	int i, budget, rc;
   3434 
   3435 	/* Make sure the driver doesn't turn the device in other process */
   3436 	rmb();
   3437 
   3438 	if (!adapter->up)
   3439 		return;
   3440 
   3441 	if (adapter->trigger_reset)
   3442 		return;
   3443 
   3444 	if (adapter->missing_tx_timeout == 0)
   3445 		return;
   3446 
   3447 	budget = adapter->missing_tx_max_queues;
   3448 
   3449 	for (i = adapter->next_monitored_tx_qid; i < adapter->num_queues; i++) {
   3450 		tx_ring = &adapter->tx_ring[i];
   3451 
   3452 		rc = check_missing_comp_in_queue(adapter, tx_ring);
   3453 		if (unlikely(rc != 0))
   3454 			return;
   3455 
   3456 		budget--;
   3457 		if (budget == 0) {
   3458 			i++;
   3459 			break;
   3460 		}
   3461 	}
   3462 
   3463 	adapter->next_monitored_tx_qid = i % adapter->num_queues;
   3464 }
   3465 
   3466 /* trigger deferred rx cleanup after 2 consecutive detections */
   3467 #define EMPTY_RX_REFILL 2
   3468 /* For the rare case where the device runs out of Rx descriptors and the
   3469  * msix handler failed to refill new Rx descriptors (due to a lack of memory
   3470  * for example).
   3471  * This case will lead to a deadlock:
   3472  * The device won't send interrupts since all the new Rx packets will be dropped
   3473  * The msix handler won't allocate new Rx descriptors so the device won't be
   3474  * able to send new packets.
   3475  *
   3476  * When such a situation is detected - execute rx cleanup task in another thread
   3477  */
   3478 static void
   3479 check_for_empty_rx_ring(struct ena_adapter *adapter)
   3480 {
   3481 	struct ena_ring *rx_ring;
   3482 	int i, refill_required;
   3483 
   3484 	if (!adapter->up)
   3485 		return;
   3486 
   3487 	if (adapter->trigger_reset)
   3488 		return;
   3489 
   3490 	for (i = 0; i < adapter->num_queues; i++) {
   3491 		rx_ring = &adapter->rx_ring[i];
   3492 
   3493 		refill_required = ena_com_free_desc(rx_ring->ena_com_io_sq);
   3494 		if (unlikely(refill_required == (rx_ring->ring_size - 1))) {
   3495 			rx_ring->empty_rx_queue++;
   3496 
   3497 			if (rx_ring->empty_rx_queue >= EMPTY_RX_REFILL)	{
   3498 				counter_u64_add(rx_ring->rx_stats.empty_rx_ring,
   3499 				    1);
   3500 
   3501 				device_printf(adapter->pdev,
   3502 				    "trigger refill for ring %d\n", i);
   3503 
   3504 				if (atomic_cas_uint(&rx_ring->task_pending, 0, 1) == 0)
   3505 					workqueue_enqueue(rx_ring->cmpl_tq,
   3506 					    &rx_ring->cmpl_task, curcpu());
   3507 				rx_ring->empty_rx_queue = 0;
   3508 			}
   3509 		} else {
   3510 			rx_ring->empty_rx_queue = 0;
   3511 		}
   3512 	}
   3513 }
   3514 
   3515 static void
   3516 ena_timer_service(void *data)
   3517 {
   3518 	struct ena_adapter *adapter = (struct ena_adapter *)data;
   3519 	struct ena_admin_host_info *host_info =
   3520 	    adapter->ena_dev->host_attr.host_info;
   3521 
   3522 	check_for_missing_keep_alive(adapter);
   3523 
   3524 	check_for_admin_com_state(adapter);
   3525 
   3526 	check_for_missing_tx_completions(adapter);
   3527 
   3528 	check_for_empty_rx_ring(adapter);
   3529 
   3530 	if (host_info != NULL)
   3531 		ena_update_host_info(host_info, adapter->ifp);
   3532 
   3533 	if (unlikely(adapter->trigger_reset)) {
   3534 		device_printf(adapter->pdev, "Trigger reset is on\n");
   3535 		workqueue_enqueue(adapter->reset_tq, &adapter->reset_task,
   3536 		    curcpu());
   3537 		return;
   3538 	}
   3539 
   3540 	/*
   3541 	 * Schedule another timeout one second from now.
   3542 	 */
   3543 	callout_schedule(&adapter->timer_service, hz);
   3544 }
   3545 
   3546 static void
   3547 ena_reset_task(struct work *wk, void *arg)
   3548 {
   3549 	struct ena_com_dev_get_features_ctx get_feat_ctx;
   3550 	struct ena_adapter *adapter = (struct ena_adapter *)arg;
   3551 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3552 	bool dev_up;
   3553 	int rc;
   3554 
   3555 	if (unlikely(!adapter->trigger_reset)) {
   3556 		device_printf(adapter->pdev,
   3557 		    "device reset scheduled but trigger_reset is off\n");
   3558 		return;
   3559 	}
   3560 
   3561 	rw_enter(&adapter->ioctl_sx, RW_WRITER);
   3562 
   3563 	callout_halt(&adapter->timer_service, &adapter->global_mtx);
   3564 
   3565 	dev_up = adapter->up;
   3566 
   3567 	ena_com_set_admin_running_state(ena_dev, false);
   3568 	ena_down(adapter);
   3569 	ena_free_mgmnt_irq(adapter);
   3570 	ena_disable_msix(adapter);
   3571 	ena_com_abort_admin_commands(ena_dev);
   3572 	ena_com_wait_for_abort_completion(ena_dev);
   3573 	ena_com_admin_destroy(ena_dev);
   3574 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3575 
   3576 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
   3577 	adapter->trigger_reset = false;
   3578 
   3579 	/* Finished destroy part. Restart the device */
   3580 	rc = ena_device_init(adapter, adapter->pdev, &get_feat_ctx,
   3581 	    &adapter->wd_active);
   3582 	if (unlikely(rc != 0)) {
   3583 		device_printf(adapter->pdev,
   3584 		    "ENA device init failed! (err: %d)\n", rc);
   3585 		goto err_dev_free;
   3586 	}
   3587 
   3588 	/* XXX dealloc and realloc MSI-X, probably a waste */
   3589 	rc = ena_enable_msix_and_set_admin_interrupts(adapter,
   3590 	    adapter->num_queues);
   3591 	if (unlikely(rc != 0)) {
   3592 		device_printf(adapter->pdev, "Enable MSI-X failed\n");
   3593 		goto err_com_free;
   3594 	}
   3595 
   3596 	/* If the interface was up before the reset bring it up */
   3597 	if (dev_up) {
   3598 		rc = ena_up(adapter);
   3599 		if (unlikely(rc != 0)) {
   3600 			device_printf(adapter->pdev,
   3601 			    "Failed to create I/O queues\n");
   3602 			goto err_msix_free;
   3603 		}
   3604 	}
   3605 
   3606 	callout_reset(&adapter->timer_service, hz,
   3607 	    ena_timer_service, (void *)adapter);
   3608 
   3609 	rw_exit(&adapter->ioctl_sx);
   3610 
   3611 	return;
   3612 
   3613 err_msix_free:
   3614 	ena_free_mgmnt_irq(adapter);
   3615 	ena_disable_msix(adapter);
   3616 err_com_free:
   3617 	ena_com_admin_destroy(ena_dev);
   3618 err_dev_free:
   3619 	device_printf(adapter->pdev, "ENA reset failed!\n");
   3620 	adapter->running = false;
   3621 	rw_exit(&adapter->ioctl_sx);
   3622 }
   3623 
   3624 /**
   3625  * ena_attach - Device Initialization Routine
   3626  * @pdev: device information struct
   3627  *
   3628  * Returns 0 on success, otherwise on failure.
   3629  *
   3630  * ena_attach initializes an adapter identified by a device structure.
   3631  * The OS initialization, configuring of the adapter private structure,
   3632  * and a hardware reset occur.
   3633  **/
   3634 static void
   3635 ena_attach(device_t parent, device_t self, void *aux)
   3636 {
   3637 	struct pci_attach_args *pa = aux;
   3638 	struct ena_com_dev_get_features_ctx get_feat_ctx;
   3639 	static int version_printed;
   3640 	struct ena_adapter *adapter = device_private(self);
   3641 	struct ena_com_dev *ena_dev = NULL;
   3642 	uint16_t tx_sgl_size = 0;
   3643 	uint16_t rx_sgl_size = 0;
   3644 	pcireg_t reg;
   3645 	int io_queue_num;
   3646 	int queue_size;
   3647 	int rc;
   3648 
   3649 	adapter->pdev = self;
   3650 	adapter->ifp = &adapter->sc_ec.ec_if;
   3651 	adapter->sc_pa = *pa;	/* used after attach for adapter reset too */
   3652 
   3653 	if (pci_dma64_available(pa))
   3654 		adapter->sc_dmat = pa->pa_dmat64;
   3655 	else
   3656 		adapter->sc_dmat = pa->pa_dmat;
   3657 
   3658 	pci_aprint_devinfo(pa, NULL);
   3659 
   3660 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   3661 	if ((reg & PCI_COMMAND_MASTER_ENABLE) == 0) {
   3662 		reg |= PCI_COMMAND_MASTER_ENABLE;
   3663         	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
   3664 	}
   3665 
   3666 	mutex_init(&adapter->global_mtx, MUTEX_DEFAULT, IPL_NET);
   3667 	rw_init(&adapter->ioctl_sx);
   3668 
   3669 	/* Set up the timer service */
   3670 	adapter->keep_alive_timeout = DEFAULT_KEEP_ALIVE_TO;
   3671 	adapter->missing_tx_timeout = DEFAULT_TX_CMP_TO;
   3672 	adapter->missing_tx_max_queues = DEFAULT_TX_MONITORED_QUEUES;
   3673 	adapter->missing_tx_threshold = DEFAULT_TX_CMP_THRESHOLD;
   3674 
   3675 	if (version_printed++ == 0)
   3676 		device_printf(parent, "%s\n", ena_version);
   3677 
   3678 	rc = ena_allocate_pci_resources(pa, adapter);
   3679 	if (unlikely(rc != 0)) {
   3680 		device_printf(parent, "PCI resource allocation failed!\n");
   3681 		ena_free_pci_resources(adapter);
   3682 		return;
   3683 	}
   3684 
   3685 	/* Allocate memory for ena_dev structure */
   3686 	ena_dev = malloc(sizeof(struct ena_com_dev), M_DEVBUF,
   3687 	    M_WAITOK | M_ZERO);
   3688 
   3689 	adapter->ena_dev = ena_dev;
   3690 	ena_dev->dmadev = self;
   3691 	ena_dev->bus = malloc(sizeof(struct ena_bus), M_DEVBUF,
   3692 	    M_WAITOK | M_ZERO);
   3693 
   3694 	/* Store register resources */
   3695 	((struct ena_bus*)(ena_dev->bus))->reg_bar_t = adapter->sc_btag;
   3696 	((struct ena_bus*)(ena_dev->bus))->reg_bar_h = adapter->sc_bhandle;
   3697 
   3698 	ena_dev->tx_mem_queue_type = ENA_ADMIN_PLACEMENT_POLICY_HOST;
   3699 
   3700 	/* Device initialization */
   3701 	rc = ena_device_init(adapter, self, &get_feat_ctx, &adapter->wd_active);
   3702 	if (unlikely(rc != 0)) {
   3703 		device_printf(self, "ENA device init failed! (err: %d)\n", rc);
   3704 		rc = ENXIO;
   3705 		goto err_bus_free;
   3706 	}
   3707 
   3708 	adapter->keep_alive_timestamp = getsbinuptime();
   3709 
   3710 	adapter->tx_offload_cap = get_feat_ctx.offload.tx;
   3711 
   3712 	/* Set for sure that interface is not up */
   3713 	adapter->up = false;
   3714 
   3715 	memcpy(adapter->mac_addr, get_feat_ctx.dev_attr.mac_addr,
   3716 	    ETHER_ADDR_LEN);
   3717 
   3718 	/* calculate IO queue number to create */
   3719 	io_queue_num = ena_calc_io_queue_num(pa, adapter, &get_feat_ctx);
   3720 
   3721 	ENA_ASSERT(io_queue_num > 0, "Invalid queue number: %d\n",
   3722 	    io_queue_num);
   3723 	adapter->num_queues = io_queue_num;
   3724 
   3725 	adapter->max_mtu = get_feat_ctx.dev_attr.max_mtu;
   3726 
   3727 	/* calculatre ring sizes */
   3728 	queue_size = ena_calc_queue_size(adapter,&tx_sgl_size,
   3729 	    &rx_sgl_size, &get_feat_ctx);
   3730 	if (unlikely((queue_size <= 0) || (io_queue_num <= 0))) {
   3731 		rc = ENA_COM_FAULT;
   3732 		goto err_com_free;
   3733 	}
   3734 
   3735 	adapter->reset_reason = ENA_REGS_RESET_NORMAL;
   3736 
   3737 	adapter->tx_ring_size = queue_size;
   3738 	adapter->rx_ring_size = queue_size;
   3739 
   3740 	adapter->max_tx_sgl_size = tx_sgl_size;
   3741 	adapter->max_rx_sgl_size = rx_sgl_size;
   3742 
   3743 #if 0
   3744 	/* set up dma tags for rx and tx buffers */
   3745 	rc = ena_setup_tx_dma_tag(adapter);
   3746 	if (unlikely(rc != 0)) {
   3747 		device_printf(self, "Failed to create TX DMA tag\n");
   3748 		goto err_com_free;
   3749 	}
   3750 
   3751 	rc = ena_setup_rx_dma_tag(adapter);
   3752 	if (unlikely(rc != 0)) {
   3753 		device_printf(self, "Failed to create RX DMA tag\n");
   3754 		goto err_tx_tag_free;
   3755 	}
   3756 #endif
   3757 
   3758 	/* initialize rings basic information */
   3759 	device_printf(self, "initalize %d io queues\n", io_queue_num);
   3760 	ena_init_io_rings(adapter);
   3761 
   3762 	/* setup network interface */
   3763 	rc = ena_setup_ifnet(self, adapter, &get_feat_ctx);
   3764 	if (unlikely(rc != 0)) {
   3765 		device_printf(self, "Error with network interface setup\n");
   3766 		goto err_io_free;
   3767 	}
   3768 
   3769 	rc = ena_enable_msix_and_set_admin_interrupts(adapter, io_queue_num);
   3770 	if (unlikely(rc != 0)) {
   3771 		device_printf(self,
   3772 		    "Failed to enable and set the admin interrupts\n");
   3773 		goto err_ifp_free;
   3774 	}
   3775 
   3776 	callout_init(&adapter->timer_service, CALLOUT_MPSAFE);
   3777 
   3778 	/* Initialize reset task queue */
   3779 	rc = workqueue_create(&adapter->reset_tq, "ena_reset_enq",
   3780 	    ena_reset_task, adapter, 0, IPL_NET, WQ_PERCPU | WQ_MPSAFE);
   3781 	if (unlikely(rc != 0)) {
   3782 		ena_trace(ENA_ALERT,
   3783 		    "Unable to create workqueue for reset task\n");
   3784 		goto err_ifp_free;
   3785 	}
   3786 
   3787 	/* Initialize statistics */
   3788 	ena_alloc_counters_dev(&adapter->dev_stats, io_queue_num);
   3789 	ena_alloc_counters_hwstats(&adapter->hw_stats, io_queue_num);
   3790 #if 0
   3791 	ena_sysctl_add_nodes(adapter);
   3792 #endif
   3793 
   3794 	/* Tell the stack that the interface is not active */
   3795 	if_setdrvflagbits(adapter->ifp, IFF_OACTIVE, IFF_RUNNING);
   3796 
   3797 	adapter->running = true;
   3798 	return;
   3799 
   3800 err_ifp_free:
   3801 	if_detach(adapter->ifp);
   3802 	if_free(adapter->ifp);
   3803 err_io_free:
   3804 	ena_free_all_io_rings_resources(adapter);
   3805 #if 0
   3806 	ena_free_rx_dma_tag(adapter);
   3807 err_tx_tag_free:
   3808 	ena_free_tx_dma_tag(adapter);
   3809 #endif
   3810 err_com_free:
   3811 	ena_com_admin_destroy(ena_dev);
   3812 	ena_com_delete_host_info(ena_dev);
   3813 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3814 err_bus_free:
   3815 	free(ena_dev->bus, M_DEVBUF);
   3816 	free(ena_dev, M_DEVBUF);
   3817 	ena_free_pci_resources(adapter);
   3818 }
   3819 
   3820 /**
   3821  * ena_detach - Device Removal Routine
   3822  * @pdev: device information struct
   3823  *
   3824  * ena_detach is called by the device subsystem to alert the driver
   3825  * that it should release a PCI device.
   3826  **/
   3827 static int
   3828 ena_detach(device_t pdev, int flags)
   3829 {
   3830 	struct ena_adapter *adapter = device_private(pdev);
   3831 	struct ena_com_dev *ena_dev = adapter->ena_dev;
   3832 #if 0
   3833 	int rc;
   3834 #endif
   3835 
   3836 	/* Make sure VLANS are not using driver */
   3837 	if (VLAN_ATTACHED(&adapter->sc_ec)) {
   3838 		device_printf(adapter->pdev ,"VLAN is in use, detach first\n");
   3839 		return (EBUSY);
   3840 	}
   3841 
   3842 	/* Free reset task and callout */
   3843 	callout_halt(&adapter->timer_service, &adapter->global_mtx);
   3844 	callout_destroy(&adapter->timer_service);
   3845 	workqueue_wait(adapter->reset_tq, &adapter->reset_task);
   3846 	workqueue_destroy(adapter->reset_tq);
   3847 	adapter->reset_tq = NULL;
   3848 
   3849 	rw_enter(&adapter->ioctl_sx, RW_WRITER);
   3850 	ena_down(adapter);
   3851 	rw_exit(&adapter->ioctl_sx);
   3852 
   3853 	if (adapter->ifp != NULL) {
   3854 		ether_ifdetach(adapter->ifp);
   3855 		if_free(adapter->ifp);
   3856 	}
   3857 
   3858 	ena_free_all_io_rings_resources(adapter);
   3859 
   3860 	ena_free_counters((struct evcnt *)&adapter->hw_stats,
   3861 	    sizeof(struct ena_hw_stats));
   3862 	ena_free_counters((struct evcnt *)&adapter->dev_stats,
   3863 	    sizeof(struct ena_stats_dev));
   3864 
   3865 	if (likely(adapter->rss_support))
   3866 		ena_com_rss_destroy(ena_dev);
   3867 
   3868 #if 0
   3869 	rc = ena_free_rx_dma_tag(adapter);
   3870 	if (unlikely(rc != 0))
   3871 		device_printf(adapter->pdev,
   3872 		    "Unmapped RX DMA tag associations\n");
   3873 
   3874 	rc = ena_free_tx_dma_tag(adapter);
   3875 	if (unlikely(rc != 0))
   3876 		device_printf(adapter->pdev,
   3877 		    "Unmapped TX DMA tag associations\n");
   3878 #endif
   3879 
   3880 	/* Reset the device only if the device is running. */
   3881 	if (adapter->running)
   3882 		ena_com_dev_reset(ena_dev, adapter->reset_reason);
   3883 
   3884 	ena_com_delete_host_info(ena_dev);
   3885 
   3886 	ena_free_irqs(adapter);
   3887 
   3888 	ena_com_abort_admin_commands(ena_dev);
   3889 
   3890 	ena_com_wait_for_abort_completion(ena_dev);
   3891 
   3892 	ena_com_admin_destroy(ena_dev);
   3893 
   3894 	ena_com_mmio_reg_read_request_destroy(ena_dev);
   3895 
   3896 	ena_free_pci_resources(adapter);
   3897 
   3898 	mutex_destroy(&adapter->global_mtx);
   3899 	rw_destroy(&adapter->ioctl_sx);
   3900 
   3901 	if (ena_dev->bus != NULL)
   3902 		free(ena_dev->bus, M_DEVBUF);
   3903 
   3904 	if (ena_dev != NULL)
   3905 		free(ena_dev, M_DEVBUF);
   3906 
   3907 	return 0;
   3908 }
   3909 
   3910 /******************************************************************************
   3911  ******************************** AENQ Handlers *******************************
   3912  *****************************************************************************/
   3913 /**
   3914  * ena_update_on_link_change:
   3915  * Notify the network interface about the change in link status
   3916  **/
   3917 static void
   3918 ena_update_on_link_change(void *adapter_data,
   3919     struct ena_admin_aenq_entry *aenq_e)
   3920 {
   3921 	struct ena_adapter *adapter = (struct ena_adapter *)adapter_data;
   3922 	struct ena_admin_aenq_link_change_desc *aenq_desc;
   3923 	int status;
   3924 	struct ifnet *ifp;
   3925 
   3926 	aenq_desc = (struct ena_admin_aenq_link_change_desc *)aenq_e;
   3927 	ifp = adapter->ifp;
   3928 	status = aenq_desc->flags &
   3929 	    ENA_ADMIN_AENQ_LINK_CHANGE_DESC_LINK_STATUS_MASK;
   3930 
   3931 	if (status != 0) {
   3932 		device_printf(adapter->pdev, "link is UP\n");
   3933 		if_link_state_change(ifp, LINK_STATE_UP);
   3934 	} else if (status == 0) {
   3935 		device_printf(adapter->pdev, "link is DOWN\n");
   3936 		if_link_state_change(ifp, LINK_STATE_DOWN);
   3937 	} else {
   3938 		device_printf(adapter->pdev, "invalid value recvd\n");
   3939 		BUG();
   3940 	}
   3941 
   3942 	adapter->link_status = status;
   3943 }
   3944 
   3945 /**
   3946  * This handler will called for unknown event group or unimplemented handlers
   3947  **/
   3948 static void
   3949 unimplemented_aenq_handler(void *data,
   3950     struct ena_admin_aenq_entry *aenq_e)
   3951 {
   3952 	return;
   3953 }
   3954 
   3955 static struct ena_aenq_handlers aenq_handlers = {
   3956     .handlers = {
   3957 	    [ENA_ADMIN_LINK_CHANGE] = ena_update_on_link_change,
   3958 	    [ENA_ADMIN_KEEP_ALIVE] = ena_keep_alive_wd,
   3959     },
   3960     .unimplemented_handler = unimplemented_aenq_handler
   3961 };
   3962 
   3963 #ifdef __FreeBSD__
   3964 /*********************************************************************
   3965  *  FreeBSD Device Interface Entry Points
   3966  *********************************************************************/
   3967 
   3968 static device_method_t ena_methods[] = {
   3969     /* Device interface */
   3970     DEVMETHOD(device_probe, ena_probe),
   3971     DEVMETHOD(device_attach, ena_attach),
   3972     DEVMETHOD(device_detach, ena_detach),
   3973     DEVMETHOD_END
   3974 };
   3975 
   3976 static driver_t ena_driver = {
   3977     "ena", ena_methods, sizeof(struct ena_adapter),
   3978 };
   3979 
   3980 devclass_t ena_devclass;
   3981 DRIVER_MODULE(ena, pci, ena_driver, ena_devclass, 0, 0);
   3982 MODULE_DEPEND(ena, pci, 1, 1, 1);
   3983 MODULE_DEPEND(ena, ether, 1, 1, 1);
   3984 
   3985 /*********************************************************************/
   3986 #endif /* __FreeBSD__ */
   3987 
   3988 #ifdef __NetBSD__
   3989 CFATTACH_DECL_NEW(ena, sizeof(struct ena_adapter), ena_probe, ena_attach,
   3990 	ena_detach, NULL);
   3991 #endif /* __NetBSD */
   3992