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