xref: /freebsd/sys/dev/vnic/nicvf_queues.c (revision 1d3d30c8d2e41052796c5bdd9220d7e3daabccad)
13c0086b8SZbigniew Bodek /*
23c0086b8SZbigniew Bodek  * Copyright (C) 2015 Cavium Inc.
33c0086b8SZbigniew Bodek  * All rights reserved.
43c0086b8SZbigniew Bodek  *
53c0086b8SZbigniew Bodek  * Redistribution and use in source and binary forms, with or without
63c0086b8SZbigniew Bodek  * modification, are permitted provided that the following conditions
73c0086b8SZbigniew Bodek  * are met:
83c0086b8SZbigniew Bodek  * 1. Redistributions of source code must retain the above copyright
93c0086b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer.
103c0086b8SZbigniew Bodek  * 2. Redistributions in binary form must reproduce the above copyright
113c0086b8SZbigniew Bodek  *    notice, this list of conditions and the following disclaimer in the
123c0086b8SZbigniew Bodek  *    documentation and/or other materials provided with the distribution.
133c0086b8SZbigniew Bodek  *
143c0086b8SZbigniew Bodek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153c0086b8SZbigniew Bodek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163c0086b8SZbigniew Bodek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173c0086b8SZbigniew Bodek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183c0086b8SZbigniew Bodek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193c0086b8SZbigniew Bodek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203c0086b8SZbigniew Bodek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213c0086b8SZbigniew Bodek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223c0086b8SZbigniew Bodek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233c0086b8SZbigniew Bodek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243c0086b8SZbigniew Bodek  * SUCH DAMAGE.
253c0086b8SZbigniew Bodek  *
263c0086b8SZbigniew Bodek  */
272306b72aSZbigniew Bodek #include <sys/cdefs.h>
28856dce91SZbigniew Bodek #include "opt_inet.h"
29856dce91SZbigniew Bodek #include "opt_inet6.h"
30856dce91SZbigniew Bodek 
312306b72aSZbigniew Bodek #include <sys/param.h>
322306b72aSZbigniew Bodek #include <sys/systm.h>
332306b72aSZbigniew Bodek #include <sys/bitset.h>
342306b72aSZbigniew Bodek #include <sys/bitstring.h>
352306b72aSZbigniew Bodek #include <sys/buf_ring.h>
362306b72aSZbigniew Bodek #include <sys/bus.h>
372306b72aSZbigniew Bodek #include <sys/endian.h>
382306b72aSZbigniew Bodek #include <sys/kernel.h>
392306b72aSZbigniew Bodek #include <sys/malloc.h>
402306b72aSZbigniew Bodek #include <sys/module.h>
412306b72aSZbigniew Bodek #include <sys/rman.h>
422306b72aSZbigniew Bodek #include <sys/pciio.h>
432306b72aSZbigniew Bodek #include <sys/pcpu.h>
442306b72aSZbigniew Bodek #include <sys/proc.h>
452306b72aSZbigniew Bodek #include <sys/sockio.h>
462306b72aSZbigniew Bodek #include <sys/socket.h>
47f6cdb4ceSZbigniew Bodek #include <sys/stdatomic.h>
482306b72aSZbigniew Bodek #include <sys/cpuset.h>
492306b72aSZbigniew Bodek #include <sys/lock.h>
502306b72aSZbigniew Bodek #include <sys/mutex.h>
512306b72aSZbigniew Bodek #include <sys/smp.h>
522306b72aSZbigniew Bodek #include <sys/taskqueue.h>
533c0086b8SZbigniew Bodek 
542306b72aSZbigniew Bodek #include <vm/vm.h>
552306b72aSZbigniew Bodek #include <vm/pmap.h>
562306b72aSZbigniew Bodek 
572306b72aSZbigniew Bodek #include <machine/bus.h>
582306b72aSZbigniew Bodek #include <machine/vmparam.h>
592306b72aSZbigniew Bodek 
602306b72aSZbigniew Bodek #include <net/if.h>
612306b72aSZbigniew Bodek #include <net/if_var.h>
622306b72aSZbigniew Bodek #include <net/if_media.h>
632306b72aSZbigniew Bodek #include <net/ifq.h>
647c617aceSZbigniew Bodek #include <net/bpf.h>
657c617aceSZbigniew Bodek #include <net/ethernet.h>
662306b72aSZbigniew Bodek 
67856dce91SZbigniew Bodek #include <netinet/in_systm.h>
68856dce91SZbigniew Bodek #include <netinet/in.h>
69856dce91SZbigniew Bodek #include <netinet/if_ether.h>
70856dce91SZbigniew Bodek #include <netinet/ip.h>
71856dce91SZbigniew Bodek #include <netinet/ip6.h>
72856dce91SZbigniew Bodek #include <netinet/sctp.h>
73856dce91SZbigniew Bodek #include <netinet/tcp.h>
74856dce91SZbigniew Bodek #include <netinet/tcp_lro.h>
75856dce91SZbigniew Bodek #include <netinet/udp.h>
76856dce91SZbigniew Bodek 
77c04716b1SBjoern A. Zeeb #include <netinet6/ip6_var.h>
78c04716b1SBjoern A. Zeeb 
792306b72aSZbigniew Bodek #include <dev/pci/pcireg.h>
802306b72aSZbigniew Bodek #include <dev/pci/pcivar.h>
812306b72aSZbigniew Bodek 
822306b72aSZbigniew Bodek #include "thunder_bgx.h"
833c0086b8SZbigniew Bodek #include "nic_reg.h"
843c0086b8SZbigniew Bodek #include "nic.h"
853c0086b8SZbigniew Bodek #include "q_struct.h"
863c0086b8SZbigniew Bodek #include "nicvf_queues.h"
873c0086b8SZbigniew Bodek 
882306b72aSZbigniew Bodek #define	DEBUG
892306b72aSZbigniew Bodek #undef DEBUG
902306b72aSZbigniew Bodek 
912306b72aSZbigniew Bodek #ifdef DEBUG
922306b72aSZbigniew Bodek #define	dprintf(dev, fmt, ...)	device_printf(dev, fmt, ##__VA_ARGS__)
932306b72aSZbigniew Bodek #else
942306b72aSZbigniew Bodek #define	dprintf(dev, fmt, ...)
952306b72aSZbigniew Bodek #endif
962306b72aSZbigniew Bodek 
972306b72aSZbigniew Bodek MALLOC_DECLARE(M_NICVF);
982306b72aSZbigniew Bodek 
992306b72aSZbigniew Bodek static void nicvf_free_snd_queue(struct nicvf *, struct snd_queue *);
1002306b72aSZbigniew Bodek static struct mbuf * nicvf_get_rcv_mbuf(struct nicvf *, struct cqe_rx_t *);
1012306b72aSZbigniew Bodek static void nicvf_sq_disable(struct nicvf *, int);
1022306b72aSZbigniew Bodek static void nicvf_sq_enable(struct nicvf *, struct snd_queue *, int);
1032306b72aSZbigniew Bodek static void nicvf_put_sq_desc(struct snd_queue *, int);
1042306b72aSZbigniew Bodek static void nicvf_cmp_queue_config(struct nicvf *, struct queue_set *, int,
1052306b72aSZbigniew Bodek     boolean_t);
1062306b72aSZbigniew Bodek static void nicvf_sq_free_used_descs(struct nicvf *, struct snd_queue *, int);
1072306b72aSZbigniew Bodek 
1087c617aceSZbigniew Bodek static int nicvf_tx_mbuf_locked(struct snd_queue *, struct mbuf **);
1097c617aceSZbigniew Bodek 
1102306b72aSZbigniew Bodek static void nicvf_rbdr_task(void *, int);
1112306b72aSZbigniew Bodek static void nicvf_rbdr_task_nowait(void *, int);
1122306b72aSZbigniew Bodek 
1133c0086b8SZbigniew Bodek struct rbuf_info {
1142306b72aSZbigniew Bodek 	bus_dma_tag_t	dmat;
1152306b72aSZbigniew Bodek 	bus_dmamap_t	dmap;
1162306b72aSZbigniew Bodek 	struct mbuf *	mbuf;
1173c0086b8SZbigniew Bodek };
1183c0086b8SZbigniew Bodek 
1192306b72aSZbigniew Bodek #define GET_RBUF_INFO(x) ((struct rbuf_info *)((x) - NICVF_RCV_BUF_ALIGN_BYTES))
1203c0086b8SZbigniew Bodek 
1213c0086b8SZbigniew Bodek /* Poll a register for a specific value */
nicvf_poll_reg(struct nicvf * nic,int qidx,uint64_t reg,int bit_pos,int bits,int val)1223c0086b8SZbigniew Bodek static int nicvf_poll_reg(struct nicvf *nic, int qidx,
1232306b72aSZbigniew Bodek 			  uint64_t reg, int bit_pos, int bits, int val)
1243c0086b8SZbigniew Bodek {
1252306b72aSZbigniew Bodek 	uint64_t bit_mask;
1262306b72aSZbigniew Bodek 	uint64_t reg_val;
1273c0086b8SZbigniew Bodek 	int timeout = 10;
1283c0086b8SZbigniew Bodek 
1292306b72aSZbigniew Bodek 	bit_mask = (1UL << bits) - 1;
1303c0086b8SZbigniew Bodek 	bit_mask = (bit_mask << bit_pos);
1313c0086b8SZbigniew Bodek 
1323c0086b8SZbigniew Bodek 	while (timeout) {
1333c0086b8SZbigniew Bodek 		reg_val = nicvf_queue_reg_read(nic, reg, qidx);
1343c0086b8SZbigniew Bodek 		if (((reg_val & bit_mask) >> bit_pos) == val)
1352306b72aSZbigniew Bodek 			return (0);
1362306b72aSZbigniew Bodek 
1372306b72aSZbigniew Bodek 		DELAY(1000);
1383c0086b8SZbigniew Bodek 		timeout--;
1393c0086b8SZbigniew Bodek 	}
1402306b72aSZbigniew Bodek 	device_printf(nic->dev, "Poll on reg 0x%lx failed\n", reg);
1412306b72aSZbigniew Bodek 	return (ETIMEDOUT);
1422306b72aSZbigniew Bodek }
1432306b72aSZbigniew Bodek 
1442306b72aSZbigniew Bodek /* Callback for bus_dmamap_load() */
1452306b72aSZbigniew Bodek static void
nicvf_dmamap_q_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)1462306b72aSZbigniew Bodek nicvf_dmamap_q_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1472306b72aSZbigniew Bodek {
1482306b72aSZbigniew Bodek 	bus_addr_t *paddr;
1492306b72aSZbigniew Bodek 
1502306b72aSZbigniew Bodek 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
1512306b72aSZbigniew Bodek 	paddr = arg;
1522306b72aSZbigniew Bodek 	*paddr = segs->ds_addr;
1533c0086b8SZbigniew Bodek }
1543c0086b8SZbigniew Bodek 
1553c0086b8SZbigniew Bodek /* Allocate memory for a queue's descriptors */
1562306b72aSZbigniew Bodek static int
nicvf_alloc_q_desc_mem(struct nicvf * nic,struct q_desc_mem * dmem,int q_len,int desc_size,int align_bytes)1572306b72aSZbigniew Bodek nicvf_alloc_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem,
1583c0086b8SZbigniew Bodek     int q_len, int desc_size, int align_bytes)
1593c0086b8SZbigniew Bodek {
16021653635SJohn Baldwin 	int err, err_dmat __diagused;
1613c0086b8SZbigniew Bodek 
1622306b72aSZbigniew Bodek 	/* Create DMA tag first */
1632306b72aSZbigniew Bodek 	err = bus_dma_tag_create(
1642306b72aSZbigniew Bodek 	    bus_get_dma_tag(nic->dev),		/* parent tag */
1652306b72aSZbigniew Bodek 	    align_bytes,			/* alignment */
1662306b72aSZbigniew Bodek 	    0,					/* boundary */
1672306b72aSZbigniew Bodek 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1682306b72aSZbigniew Bodek 	    BUS_SPACE_MAXADDR,			/* highaddr */
1692306b72aSZbigniew Bodek 	    NULL, NULL,				/* filtfunc, filtfuncarg */
1702306b72aSZbigniew Bodek 	    (q_len * desc_size),		/* maxsize */
1712306b72aSZbigniew Bodek 	    1,					/* nsegments */
1722306b72aSZbigniew Bodek 	    (q_len * desc_size),		/* maxsegsize */
1732306b72aSZbigniew Bodek 	    0,					/* flags */
1742306b72aSZbigniew Bodek 	    NULL, NULL,				/* lockfunc, lockfuncarg */
1752306b72aSZbigniew Bodek 	    &dmem->dmat);			/* dmat */
1762306b72aSZbigniew Bodek 
1772306b72aSZbigniew Bodek 	if (err != 0) {
1782306b72aSZbigniew Bodek 		device_printf(nic->dev,
1792306b72aSZbigniew Bodek 		    "Failed to create busdma tag for descriptors ring\n");
1802306b72aSZbigniew Bodek 		return (err);
1812306b72aSZbigniew Bodek 	}
1822306b72aSZbigniew Bodek 
1832306b72aSZbigniew Bodek 	/* Allocate segment of continuous DMA safe memory */
1842306b72aSZbigniew Bodek 	err = bus_dmamem_alloc(
1852306b72aSZbigniew Bodek 	    dmem->dmat,				/* DMA tag */
1862306b72aSZbigniew Bodek 	    &dmem->base,			/* virtual address */
1872306b72aSZbigniew Bodek 	    (BUS_DMA_NOWAIT | BUS_DMA_ZERO),	/* flags */
1882306b72aSZbigniew Bodek 	    &dmem->dmap);			/* DMA map */
1892306b72aSZbigniew Bodek 	if (err != 0) {
1902306b72aSZbigniew Bodek 		device_printf(nic->dev, "Failed to allocate DMA safe memory for"
1912306b72aSZbigniew Bodek 		    "descriptors ring\n");
1922306b72aSZbigniew Bodek 		goto dmamem_fail;
1932306b72aSZbigniew Bodek 	}
1942306b72aSZbigniew Bodek 
1952306b72aSZbigniew Bodek 	err = bus_dmamap_load(
1962306b72aSZbigniew Bodek 	    dmem->dmat,
1972306b72aSZbigniew Bodek 	    dmem->dmap,
1982306b72aSZbigniew Bodek 	    dmem->base,
1992306b72aSZbigniew Bodek 	    (q_len * desc_size),		/* allocation size */
2002306b72aSZbigniew Bodek 	    nicvf_dmamap_q_cb,			/* map to DMA address cb. */
2012306b72aSZbigniew Bodek 	    &dmem->phys_base,			/* physical address */
2022306b72aSZbigniew Bodek 	    BUS_DMA_NOWAIT);
2032306b72aSZbigniew Bodek 	if (err != 0) {
2042306b72aSZbigniew Bodek 		device_printf(nic->dev,
2052306b72aSZbigniew Bodek 		    "Cannot load DMA map of descriptors ring\n");
2062306b72aSZbigniew Bodek 		goto dmamap_fail;
2072306b72aSZbigniew Bodek 	}
2082306b72aSZbigniew Bodek 
2092306b72aSZbigniew Bodek 	dmem->q_len = q_len;
2102306b72aSZbigniew Bodek 	dmem->size = (desc_size * q_len);
2112306b72aSZbigniew Bodek 
2122306b72aSZbigniew Bodek 	return (0);
2132306b72aSZbigniew Bodek 
2142306b72aSZbigniew Bodek dmamap_fail:
2152306b72aSZbigniew Bodek 	bus_dmamem_free(dmem->dmat, dmem->base, dmem->dmap);
2162306b72aSZbigniew Bodek 	dmem->phys_base = 0;
2172306b72aSZbigniew Bodek dmamem_fail:
2182306b72aSZbigniew Bodek 	err_dmat = bus_dma_tag_destroy(dmem->dmat);
2192306b72aSZbigniew Bodek 	dmem->base = NULL;
2202306b72aSZbigniew Bodek 	KASSERT(err_dmat == 0,
2212306b72aSZbigniew Bodek 	    ("%s: Trying to destroy BUSY DMA tag", __func__));
2222306b72aSZbigniew Bodek 
2232306b72aSZbigniew Bodek 	return (err);
2243c0086b8SZbigniew Bodek }
2253c0086b8SZbigniew Bodek 
2263c0086b8SZbigniew Bodek /* Free queue's descriptor memory */
2272306b72aSZbigniew Bodek static void
nicvf_free_q_desc_mem(struct nicvf * nic,struct q_desc_mem * dmem)2282306b72aSZbigniew Bodek nicvf_free_q_desc_mem(struct nicvf *nic, struct q_desc_mem *dmem)
2293c0086b8SZbigniew Bodek {
23021653635SJohn Baldwin 	int err __diagused;
2312306b72aSZbigniew Bodek 
2322306b72aSZbigniew Bodek 	if ((dmem == NULL) || (dmem->base == NULL))
2333c0086b8SZbigniew Bodek 		return;
2343c0086b8SZbigniew Bodek 
2352306b72aSZbigniew Bodek 	/* Unload a map */
2362306b72aSZbigniew Bodek 	bus_dmamap_sync(dmem->dmat, dmem->dmap, BUS_DMASYNC_POSTREAD);
2372306b72aSZbigniew Bodek 	bus_dmamap_unload(dmem->dmat, dmem->dmap);
2382306b72aSZbigniew Bodek 	/* Free DMA memory */
2392306b72aSZbigniew Bodek 	bus_dmamem_free(dmem->dmat, dmem->base, dmem->dmap);
2402306b72aSZbigniew Bodek 	/* Destroy DMA tag */
2412306b72aSZbigniew Bodek 	err = bus_dma_tag_destroy(dmem->dmat);
2422306b72aSZbigniew Bodek 
2432306b72aSZbigniew Bodek 	KASSERT(err == 0,
2442306b72aSZbigniew Bodek 	    ("%s: Trying to destroy BUSY DMA tag", __func__));
2452306b72aSZbigniew Bodek 
2462306b72aSZbigniew Bodek 	dmem->phys_base = 0;
2473c0086b8SZbigniew Bodek 	dmem->base = NULL;
2483c0086b8SZbigniew Bodek }
2493c0086b8SZbigniew Bodek 
2502306b72aSZbigniew Bodek /*
2512306b72aSZbigniew Bodek  * Allocate buffer for packet reception
2523c0086b8SZbigniew Bodek  * HW returns memory address where packet is DMA'ed but not a pointer
2533c0086b8SZbigniew Bodek  * into RBDR ring, so save buffer address at the start of fragment and
2543c0086b8SZbigniew Bodek  * align the start address to a cache aligned address
2553c0086b8SZbigniew Bodek  */
2562306b72aSZbigniew Bodek static __inline int
nicvf_alloc_rcv_buffer(struct nicvf * nic,struct rbdr * rbdr,bus_dmamap_t dmap,int mflags,uint32_t buf_len,bus_addr_t * rbuf)2572306b72aSZbigniew Bodek nicvf_alloc_rcv_buffer(struct nicvf *nic, struct rbdr *rbdr,
2582306b72aSZbigniew Bodek     bus_dmamap_t dmap, int mflags, uint32_t buf_len, bus_addr_t *rbuf)
2593c0086b8SZbigniew Bodek {
2602306b72aSZbigniew Bodek 	struct mbuf *mbuf;
2613c0086b8SZbigniew Bodek 	struct rbuf_info *rinfo;
2622306b72aSZbigniew Bodek 	bus_dma_segment_t segs[1];
2632306b72aSZbigniew Bodek 	int nsegs;
2642306b72aSZbigniew Bodek 	int err;
2653c0086b8SZbigniew Bodek 
2662306b72aSZbigniew Bodek 	mbuf = m_getjcl(mflags, MT_DATA, M_PKTHDR, MCLBYTES);
2672306b72aSZbigniew Bodek 	if (mbuf == NULL)
2682306b72aSZbigniew Bodek 		return (ENOMEM);
2692306b72aSZbigniew Bodek 
2702306b72aSZbigniew Bodek 	/*
2712306b72aSZbigniew Bodek 	 * The length is equal to the actual length + one 128b line
2722306b72aSZbigniew Bodek 	 * used as a room for rbuf_info structure.
2732306b72aSZbigniew Bodek 	 */
2742306b72aSZbigniew Bodek 	mbuf->m_len = mbuf->m_pkthdr.len = buf_len;
2752306b72aSZbigniew Bodek 
2762306b72aSZbigniew Bodek 	err = bus_dmamap_load_mbuf_sg(rbdr->rbdr_buff_dmat, dmap, mbuf, segs,
2772306b72aSZbigniew Bodek 	    &nsegs, BUS_DMA_NOWAIT);
2782306b72aSZbigniew Bodek 	if (err != 0) {
2792306b72aSZbigniew Bodek 		device_printf(nic->dev,
2802306b72aSZbigniew Bodek 		    "Failed to map mbuf into DMA visible memory, err: %d\n",
2812306b72aSZbigniew Bodek 		    err);
2822306b72aSZbigniew Bodek 		m_freem(mbuf);
2832306b72aSZbigniew Bodek 		bus_dmamap_destroy(rbdr->rbdr_buff_dmat, dmap);
2842306b72aSZbigniew Bodek 		return (err);
2853c0086b8SZbigniew Bodek 	}
2862306b72aSZbigniew Bodek 	if (nsegs != 1)
2872306b72aSZbigniew Bodek 		panic("Unexpected number of DMA segments for RB: %d", nsegs);
2882306b72aSZbigniew Bodek 	/*
2892306b72aSZbigniew Bodek 	 * Now use the room for rbuf_info structure
2902306b72aSZbigniew Bodek 	 * and adjust mbuf data and length.
2912306b72aSZbigniew Bodek 	 */
2922306b72aSZbigniew Bodek 	rinfo = (struct rbuf_info *)mbuf->m_data;
2932306b72aSZbigniew Bodek 	m_adj(mbuf, NICVF_RCV_BUF_ALIGN_BYTES);
2942306b72aSZbigniew Bodek 
2952306b72aSZbigniew Bodek 	rinfo->dmat = rbdr->rbdr_buff_dmat;
2962306b72aSZbigniew Bodek 	rinfo->dmap = dmap;
2972306b72aSZbigniew Bodek 	rinfo->mbuf = mbuf;
2982306b72aSZbigniew Bodek 
2992306b72aSZbigniew Bodek 	*rbuf = segs[0].ds_addr + NICVF_RCV_BUF_ALIGN_BYTES;
3002306b72aSZbigniew Bodek 
3012306b72aSZbigniew Bodek 	return (0);
3023c0086b8SZbigniew Bodek }
3033c0086b8SZbigniew Bodek 
3042306b72aSZbigniew Bodek /* Retrieve mbuf for received packet */
3052306b72aSZbigniew Bodek static struct mbuf *
nicvf_rb_ptr_to_mbuf(struct nicvf * nic,bus_addr_t rb_ptr)3062306b72aSZbigniew Bodek nicvf_rb_ptr_to_mbuf(struct nicvf *nic, bus_addr_t rb_ptr)
3073c0086b8SZbigniew Bodek {
3082306b72aSZbigniew Bodek 	struct mbuf *mbuf;
3093c0086b8SZbigniew Bodek 	struct rbuf_info *rinfo;
3103c0086b8SZbigniew Bodek 
3113c0086b8SZbigniew Bodek 	/* Get buffer start address and alignment offset */
3122306b72aSZbigniew Bodek 	rinfo = GET_RBUF_INFO(PHYS_TO_DMAP(rb_ptr));
3133c0086b8SZbigniew Bodek 
3142306b72aSZbigniew Bodek 	/* Now retrieve mbuf to give to stack */
3152306b72aSZbigniew Bodek 	mbuf = rinfo->mbuf;
3162306b72aSZbigniew Bodek 	if (__predict_false(mbuf == NULL)) {
3172306b72aSZbigniew Bodek 		panic("%s: Received packet fragment with NULL mbuf",
3182306b72aSZbigniew Bodek 		    device_get_nameunit(nic->dev));
3193c0086b8SZbigniew Bodek 	}
3202306b72aSZbigniew Bodek 	/*
3212306b72aSZbigniew Bodek 	 * Clear the mbuf in the descriptor to indicate
3222306b72aSZbigniew Bodek 	 * that this slot is processed and free to use.
3232306b72aSZbigniew Bodek 	 */
3242306b72aSZbigniew Bodek 	rinfo->mbuf = NULL;
3253c0086b8SZbigniew Bodek 
3262306b72aSZbigniew Bodek 	bus_dmamap_sync(rinfo->dmat, rinfo->dmap, BUS_DMASYNC_POSTREAD);
3272306b72aSZbigniew Bodek 	bus_dmamap_unload(rinfo->dmat, rinfo->dmap);
3283c0086b8SZbigniew Bodek 
3292306b72aSZbigniew Bodek 	return (mbuf);
3303c0086b8SZbigniew Bodek }
3313c0086b8SZbigniew Bodek 
3323c0086b8SZbigniew Bodek /* Allocate RBDR ring and populate receive buffers */
3332306b72aSZbigniew Bodek static int
nicvf_init_rbdr(struct nicvf * nic,struct rbdr * rbdr,int ring_len,int buf_size,int qidx)3342306b72aSZbigniew Bodek nicvf_init_rbdr(struct nicvf *nic, struct rbdr *rbdr, int ring_len,
3352306b72aSZbigniew Bodek     int buf_size, int qidx)
3363c0086b8SZbigniew Bodek {
3372306b72aSZbigniew Bodek 	bus_dmamap_t dmap;
3382306b72aSZbigniew Bodek 	bus_addr_t rbuf;
3393c0086b8SZbigniew Bodek 	struct rbdr_entry_t *desc;
3402306b72aSZbigniew Bodek 	int idx;
3413c0086b8SZbigniew Bodek 	int err;
3423c0086b8SZbigniew Bodek 
3432306b72aSZbigniew Bodek 	/* Allocate rbdr descriptors ring */
3443c0086b8SZbigniew Bodek 	err = nicvf_alloc_q_desc_mem(nic, &rbdr->dmem, ring_len,
3452306b72aSZbigniew Bodek 	    sizeof(struct rbdr_entry_t), NICVF_RCV_BUF_ALIGN_BYTES);
3462306b72aSZbigniew Bodek 	if (err != 0) {
3472306b72aSZbigniew Bodek 		device_printf(nic->dev,
3482306b72aSZbigniew Bodek 		    "Failed to create RBDR descriptors ring\n");
3492306b72aSZbigniew Bodek 		return (err);
3502306b72aSZbigniew Bodek 	}
3513c0086b8SZbigniew Bodek 
3523c0086b8SZbigniew Bodek 	rbdr->desc = rbdr->dmem.base;
3532306b72aSZbigniew Bodek 	/*
3542306b72aSZbigniew Bodek 	 * Buffer size has to be in multiples of 128 bytes.
3552306b72aSZbigniew Bodek 	 * Make room for metadata of size of one line (128 bytes).
3562306b72aSZbigniew Bodek 	 */
3572306b72aSZbigniew Bodek 	rbdr->dma_size = buf_size - NICVF_RCV_BUF_ALIGN_BYTES;
3582306b72aSZbigniew Bodek 	rbdr->enable = TRUE;
3593c0086b8SZbigniew Bodek 	rbdr->thresh = RBDR_THRESH;
3602306b72aSZbigniew Bodek 	rbdr->nic = nic;
3612306b72aSZbigniew Bodek 	rbdr->idx = qidx;
3623c0086b8SZbigniew Bodek 
3632306b72aSZbigniew Bodek 	/*
3642306b72aSZbigniew Bodek 	 * Create DMA tag for Rx buffers.
3652306b72aSZbigniew Bodek 	 * Each map created using this tag is intended to store Rx payload for
3662306b72aSZbigniew Bodek 	 * one fragment and one header structure containing rbuf_info (thus
3672306b72aSZbigniew Bodek 	 * additional 128 byte line since RB must be a multiple of 128 byte
3682306b72aSZbigniew Bodek 	 * cache line).
3692306b72aSZbigniew Bodek 	 */
3702306b72aSZbigniew Bodek 	if (buf_size > MCLBYTES) {
3712306b72aSZbigniew Bodek 		device_printf(nic->dev,
3722306b72aSZbigniew Bodek 		    "Buffer size to large for mbuf cluster\n");
3732306b72aSZbigniew Bodek 		return (EINVAL);
3742306b72aSZbigniew Bodek 	}
3752306b72aSZbigniew Bodek 	err = bus_dma_tag_create(
3762306b72aSZbigniew Bodek 	    bus_get_dma_tag(nic->dev),		/* parent tag */
3772306b72aSZbigniew Bodek 	    NICVF_RCV_BUF_ALIGN_BYTES,		/* alignment */
3782306b72aSZbigniew Bodek 	    0,					/* boundary */
3792306b72aSZbigniew Bodek 	    DMAP_MAX_PHYSADDR,			/* lowaddr */
3802306b72aSZbigniew Bodek 	    DMAP_MIN_PHYSADDR,			/* highaddr */
3812306b72aSZbigniew Bodek 	    NULL, NULL,				/* filtfunc, filtfuncarg */
3822306b72aSZbigniew Bodek 	    roundup2(buf_size, MCLBYTES),	/* maxsize */
3832306b72aSZbigniew Bodek 	    1,					/* nsegments */
3842306b72aSZbigniew Bodek 	    roundup2(buf_size, MCLBYTES),	/* maxsegsize */
3852306b72aSZbigniew Bodek 	    0,					/* flags */
3862306b72aSZbigniew Bodek 	    NULL, NULL,				/* lockfunc, lockfuncarg */
3872306b72aSZbigniew Bodek 	    &rbdr->rbdr_buff_dmat);		/* dmat */
3882306b72aSZbigniew Bodek 
3892306b72aSZbigniew Bodek 	if (err != 0) {
3902306b72aSZbigniew Bodek 		device_printf(nic->dev,
3912306b72aSZbigniew Bodek 		    "Failed to create busdma tag for RBDR buffers\n");
3922306b72aSZbigniew Bodek 		return (err);
3932306b72aSZbigniew Bodek 	}
3942306b72aSZbigniew Bodek 
3952306b72aSZbigniew Bodek 	rbdr->rbdr_buff_dmaps = malloc(sizeof(*rbdr->rbdr_buff_dmaps) *
3962306b72aSZbigniew Bodek 	    ring_len, M_NICVF, (M_WAITOK | M_ZERO));
3972306b72aSZbigniew Bodek 
3983c0086b8SZbigniew Bodek 	for (idx = 0; idx < ring_len; idx++) {
3992306b72aSZbigniew Bodek 		err = bus_dmamap_create(rbdr->rbdr_buff_dmat, 0, &dmap);
4002306b72aSZbigniew Bodek 		if (err != 0) {
4012306b72aSZbigniew Bodek 			device_printf(nic->dev,
4022306b72aSZbigniew Bodek 			    "Failed to create DMA map for RB\n");
4032306b72aSZbigniew Bodek 			return (err);
4042306b72aSZbigniew Bodek 		}
4052306b72aSZbigniew Bodek 		rbdr->rbdr_buff_dmaps[idx] = dmap;
4062306b72aSZbigniew Bodek 
4072306b72aSZbigniew Bodek 		err = nicvf_alloc_rcv_buffer(nic, rbdr, dmap, M_WAITOK,
4082306b72aSZbigniew Bodek 		    DMA_BUFFER_LEN, &rbuf);
4092306b72aSZbigniew Bodek 		if (err != 0)
4102306b72aSZbigniew Bodek 			return (err);
4113c0086b8SZbigniew Bodek 
4123c0086b8SZbigniew Bodek 		desc = GET_RBDR_DESC(rbdr, idx);
4132306b72aSZbigniew Bodek 		desc->buf_addr = (rbuf >> NICVF_RCV_BUF_ALIGN);
4143c0086b8SZbigniew Bodek 	}
4152306b72aSZbigniew Bodek 
4162306b72aSZbigniew Bodek 	/* Allocate taskqueue */
4172306b72aSZbigniew Bodek 	TASK_INIT(&rbdr->rbdr_task, 0, nicvf_rbdr_task, rbdr);
4182306b72aSZbigniew Bodek 	TASK_INIT(&rbdr->rbdr_task_nowait, 0, nicvf_rbdr_task_nowait, rbdr);
4192306b72aSZbigniew Bodek 	rbdr->rbdr_taskq = taskqueue_create_fast("nicvf_rbdr_taskq", M_WAITOK,
4202306b72aSZbigniew Bodek 	    taskqueue_thread_enqueue, &rbdr->rbdr_taskq);
4212306b72aSZbigniew Bodek 	taskqueue_start_threads(&rbdr->rbdr_taskq, 1, PI_NET, "%s: rbdr_taskq",
4222306b72aSZbigniew Bodek 	    device_get_nameunit(nic->dev));
4232306b72aSZbigniew Bodek 
4242306b72aSZbigniew Bodek 	return (0);
4253c0086b8SZbigniew Bodek }
4263c0086b8SZbigniew Bodek 
4273c0086b8SZbigniew Bodek /* Free RBDR ring and its receive buffers */
4282306b72aSZbigniew Bodek static void
nicvf_free_rbdr(struct nicvf * nic,struct rbdr * rbdr)4292306b72aSZbigniew Bodek nicvf_free_rbdr(struct nicvf *nic, struct rbdr *rbdr)
4303c0086b8SZbigniew Bodek {
4312306b72aSZbigniew Bodek 	struct mbuf *mbuf;
4322306b72aSZbigniew Bodek 	struct queue_set *qs;
4333c0086b8SZbigniew Bodek 	struct rbdr_entry_t *desc;
4343c0086b8SZbigniew Bodek 	struct rbuf_info *rinfo;
4352306b72aSZbigniew Bodek 	bus_addr_t buf_addr;
4362306b72aSZbigniew Bodek 	int head, tail, idx;
43721653635SJohn Baldwin 	int err __diagused;
4383c0086b8SZbigniew Bodek 
4392306b72aSZbigniew Bodek 	qs = nic->qs;
4402306b72aSZbigniew Bodek 
4412306b72aSZbigniew Bodek 	if ((qs == NULL) || (rbdr == NULL))
4423c0086b8SZbigniew Bodek 		return;
4433c0086b8SZbigniew Bodek 
4442306b72aSZbigniew Bodek 	rbdr->enable = FALSE;
4452306b72aSZbigniew Bodek 	if (rbdr->rbdr_taskq != NULL) {
4462306b72aSZbigniew Bodek 		/* Remove tasks */
4472306b72aSZbigniew Bodek 		while (taskqueue_cancel(rbdr->rbdr_taskq,
4482306b72aSZbigniew Bodek 		    &rbdr->rbdr_task_nowait, NULL) != 0) {
4492306b72aSZbigniew Bodek 			/* Finish the nowait task first */
4502306b72aSZbigniew Bodek 			taskqueue_drain(rbdr->rbdr_taskq,
4512306b72aSZbigniew Bodek 			    &rbdr->rbdr_task_nowait);
4522306b72aSZbigniew Bodek 		}
4532306b72aSZbigniew Bodek 		taskqueue_free(rbdr->rbdr_taskq);
4542306b72aSZbigniew Bodek 		rbdr->rbdr_taskq = NULL;
4553c0086b8SZbigniew Bodek 
4562306b72aSZbigniew Bodek 		while (taskqueue_cancel(taskqueue_thread,
4572306b72aSZbigniew Bodek 		    &rbdr->rbdr_task, NULL) != 0) {
4582306b72aSZbigniew Bodek 			/* Now finish the sleepable task */
4592306b72aSZbigniew Bodek 			taskqueue_drain(taskqueue_thread, &rbdr->rbdr_task);
4602306b72aSZbigniew Bodek 		}
4612306b72aSZbigniew Bodek 	}
4622306b72aSZbigniew Bodek 
4632306b72aSZbigniew Bodek 	/*
4642306b72aSZbigniew Bodek 	 * Free all of the memory under the RB descriptors.
4652306b72aSZbigniew Bodek 	 * There are assumptions here:
4662306b72aSZbigniew Bodek 	 * 1. Corresponding RBDR is disabled
4672306b72aSZbigniew Bodek 	 *    - it is safe to operate using head and tail indexes
4682306b72aSZbigniew Bodek 	 * 2. All bffers that were received are properly freed by
4692306b72aSZbigniew Bodek 	 *    the receive handler
4702306b72aSZbigniew Bodek 	 *    - there is no need to unload DMA map and free MBUF for other
4712306b72aSZbigniew Bodek 	 *      descriptors than unused ones
4722306b72aSZbigniew Bodek 	 */
4732306b72aSZbigniew Bodek 	if (rbdr->rbdr_buff_dmat != NULL) {
4743c0086b8SZbigniew Bodek 		head = rbdr->head;
4753c0086b8SZbigniew Bodek 		tail = rbdr->tail;
4763c0086b8SZbigniew Bodek 		while (head != tail) {
4773c0086b8SZbigniew Bodek 			desc = GET_RBDR_DESC(rbdr, head);
4783c0086b8SZbigniew Bodek 			buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
4792306b72aSZbigniew Bodek 			rinfo = GET_RBUF_INFO(PHYS_TO_DMAP(buf_addr));
4802306b72aSZbigniew Bodek 			bus_dmamap_unload(rbdr->rbdr_buff_dmat, rinfo->dmap);
4812306b72aSZbigniew Bodek 			mbuf = rinfo->mbuf;
4822306b72aSZbigniew Bodek 			/* This will destroy everything including rinfo! */
4832306b72aSZbigniew Bodek 			m_freem(mbuf);
4843c0086b8SZbigniew Bodek 			head++;
4853c0086b8SZbigniew Bodek 			head &= (rbdr->dmem.q_len - 1);
4863c0086b8SZbigniew Bodek 		}
4872306b72aSZbigniew Bodek 		/* Free tail descriptor */
4883c0086b8SZbigniew Bodek 		desc = GET_RBDR_DESC(rbdr, tail);
4893c0086b8SZbigniew Bodek 		buf_addr = desc->buf_addr << NICVF_RCV_BUF_ALIGN;
4902306b72aSZbigniew Bodek 		rinfo = GET_RBUF_INFO(PHYS_TO_DMAP(buf_addr));
4912306b72aSZbigniew Bodek 		bus_dmamap_unload(rbdr->rbdr_buff_dmat, rinfo->dmap);
4922306b72aSZbigniew Bodek 		mbuf = rinfo->mbuf;
4932306b72aSZbigniew Bodek 		/* This will destroy everything including rinfo! */
4942306b72aSZbigniew Bodek 		m_freem(mbuf);
4952306b72aSZbigniew Bodek 
4962306b72aSZbigniew Bodek 		/* Destroy DMA maps */
4972306b72aSZbigniew Bodek 		for (idx = 0; idx < qs->rbdr_len; idx++) {
4982306b72aSZbigniew Bodek 			if (rbdr->rbdr_buff_dmaps[idx] == NULL)
4992306b72aSZbigniew Bodek 				continue;
5002306b72aSZbigniew Bodek 			err = bus_dmamap_destroy(rbdr->rbdr_buff_dmat,
5012306b72aSZbigniew Bodek 			    rbdr->rbdr_buff_dmaps[idx]);
5022306b72aSZbigniew Bodek 			KASSERT(err == 0,
5032306b72aSZbigniew Bodek 			    ("%s: Could not destroy DMA map for RB, desc: %d",
5042306b72aSZbigniew Bodek 			    __func__, idx));
5052306b72aSZbigniew Bodek 			rbdr->rbdr_buff_dmaps[idx] = NULL;
5062306b72aSZbigniew Bodek 		}
5072306b72aSZbigniew Bodek 
5082306b72aSZbigniew Bodek 		/* Now destroy the tag */
5092306b72aSZbigniew Bodek 		err = bus_dma_tag_destroy(rbdr->rbdr_buff_dmat);
5102306b72aSZbigniew Bodek 		KASSERT(err == 0,
5112306b72aSZbigniew Bodek 		    ("%s: Trying to destroy BUSY DMA tag", __func__));
5122306b72aSZbigniew Bodek 
5132306b72aSZbigniew Bodek 		rbdr->head = 0;
5142306b72aSZbigniew Bodek 		rbdr->tail = 0;
5152306b72aSZbigniew Bodek 	}
5163c0086b8SZbigniew Bodek 
5173c0086b8SZbigniew Bodek 	/* Free RBDR ring */
5183c0086b8SZbigniew Bodek 	nicvf_free_q_desc_mem(nic, &rbdr->dmem);
5193c0086b8SZbigniew Bodek }
5203c0086b8SZbigniew Bodek 
5212306b72aSZbigniew Bodek /*
5222306b72aSZbigniew Bodek  * Refill receive buffer descriptors with new buffers.
5233c0086b8SZbigniew Bodek  */
5242306b72aSZbigniew Bodek static int
nicvf_refill_rbdr(struct rbdr * rbdr,int mflags)5252306b72aSZbigniew Bodek nicvf_refill_rbdr(struct rbdr *rbdr, int mflags)
5263c0086b8SZbigniew Bodek {
5272306b72aSZbigniew Bodek 	struct nicvf *nic;
5282306b72aSZbigniew Bodek 	struct queue_set *qs;
5292306b72aSZbigniew Bodek 	int rbdr_idx;
5303c0086b8SZbigniew Bodek 	int tail, qcount;
5313c0086b8SZbigniew Bodek 	int refill_rb_cnt;
5323c0086b8SZbigniew Bodek 	struct rbdr_entry_t *desc;
5332306b72aSZbigniew Bodek 	bus_dmamap_t dmap;
5342306b72aSZbigniew Bodek 	bus_addr_t rbuf;
5352306b72aSZbigniew Bodek 	boolean_t rb_alloc_fail;
5362306b72aSZbigniew Bodek 	int new_rb;
5373c0086b8SZbigniew Bodek 
5382306b72aSZbigniew Bodek 	rb_alloc_fail = TRUE;
5392306b72aSZbigniew Bodek 	new_rb = 0;
5402306b72aSZbigniew Bodek 	nic = rbdr->nic;
5412306b72aSZbigniew Bodek 	qs = nic->qs;
5422306b72aSZbigniew Bodek 	rbdr_idx = rbdr->idx;
5432306b72aSZbigniew Bodek 
5443c0086b8SZbigniew Bodek 	/* Check if it's enabled */
5453c0086b8SZbigniew Bodek 	if (!rbdr->enable)
5462306b72aSZbigniew Bodek 		return (0);
5473c0086b8SZbigniew Bodek 
5483c0086b8SZbigniew Bodek 	/* Get no of desc's to be refilled */
5493c0086b8SZbigniew Bodek 	qcount = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, rbdr_idx);
5503c0086b8SZbigniew Bodek 	qcount &= 0x7FFFF;
5513c0086b8SZbigniew Bodek 	/* Doorbell can be ringed with a max of ring size minus 1 */
5522306b72aSZbigniew Bodek 	if (qcount >= (qs->rbdr_len - 1)) {
5532306b72aSZbigniew Bodek 		rb_alloc_fail = FALSE;
5542306b72aSZbigniew Bodek 		goto out;
5552306b72aSZbigniew Bodek 	} else
5563c0086b8SZbigniew Bodek 		refill_rb_cnt = qs->rbdr_len - qcount - 1;
5573c0086b8SZbigniew Bodek 
5583c0086b8SZbigniew Bodek 	/* Start filling descs from tail */
5593c0086b8SZbigniew Bodek 	tail = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, rbdr_idx) >> 3;
5603c0086b8SZbigniew Bodek 	while (refill_rb_cnt) {
5613c0086b8SZbigniew Bodek 		tail++;
5623c0086b8SZbigniew Bodek 		tail &= (rbdr->dmem.q_len - 1);
5633c0086b8SZbigniew Bodek 
5642306b72aSZbigniew Bodek 		dmap = rbdr->rbdr_buff_dmaps[tail];
5652306b72aSZbigniew Bodek 		if (nicvf_alloc_rcv_buffer(nic, rbdr, dmap, mflags,
5662306b72aSZbigniew Bodek 		    DMA_BUFFER_LEN, &rbuf)) {
5672306b72aSZbigniew Bodek 			/* Something went wrong. Resign */
5683c0086b8SZbigniew Bodek 			break;
5692306b72aSZbigniew Bodek 		}
5703c0086b8SZbigniew Bodek 		desc = GET_RBDR_DESC(rbdr, tail);
5712306b72aSZbigniew Bodek 		desc->buf_addr = (rbuf >> NICVF_RCV_BUF_ALIGN);
5723c0086b8SZbigniew Bodek 		refill_rb_cnt--;
5733c0086b8SZbigniew Bodek 		new_rb++;
5743c0086b8SZbigniew Bodek 	}
5753c0086b8SZbigniew Bodek 
5763c0086b8SZbigniew Bodek 	/* make sure all memory stores are done before ringing doorbell */
5772306b72aSZbigniew Bodek 	wmb();
5783c0086b8SZbigniew Bodek 
5793c0086b8SZbigniew Bodek 	/* Check if buffer allocation failed */
5802306b72aSZbigniew Bodek 	if (refill_rb_cnt == 0)
5812306b72aSZbigniew Bodek 		rb_alloc_fail = FALSE;
5823c0086b8SZbigniew Bodek 
5833c0086b8SZbigniew Bodek 	/* Notify HW */
5843c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR,
5853c0086b8SZbigniew Bodek 			      rbdr_idx, new_rb);
5862306b72aSZbigniew Bodek out:
5872306b72aSZbigniew Bodek 	if (!rb_alloc_fail) {
5882306b72aSZbigniew Bodek 		/*
5892306b72aSZbigniew Bodek 		 * Re-enable RBDR interrupts only
5902306b72aSZbigniew Bodek 		 * if buffer allocation is success.
5912306b72aSZbigniew Bodek 		 */
5923c0086b8SZbigniew Bodek 		nicvf_enable_intr(nic, NICVF_INTR_RBDR, rbdr_idx);
5933c0086b8SZbigniew Bodek 
5942306b72aSZbigniew Bodek 		return (0);
5953c0086b8SZbigniew Bodek 	}
5963c0086b8SZbigniew Bodek 
5972306b72aSZbigniew Bodek 	return (ENOMEM);
5982306b72aSZbigniew Bodek }
5992306b72aSZbigniew Bodek 
6002306b72aSZbigniew Bodek /* Refill RBs even if sleep is needed to reclaim memory */
6012306b72aSZbigniew Bodek static void
nicvf_rbdr_task(void * arg,int pending)6022306b72aSZbigniew Bodek nicvf_rbdr_task(void *arg, int pending)
6033c0086b8SZbigniew Bodek {
6042306b72aSZbigniew Bodek 	struct rbdr *rbdr;
6052306b72aSZbigniew Bodek 	int err;
6063c0086b8SZbigniew Bodek 
6072306b72aSZbigniew Bodek 	rbdr = (struct rbdr *)arg;
6082306b72aSZbigniew Bodek 
6092306b72aSZbigniew Bodek 	err = nicvf_refill_rbdr(rbdr, M_WAITOK);
6102306b72aSZbigniew Bodek 	if (__predict_false(err != 0)) {
6112306b72aSZbigniew Bodek 		panic("%s: Failed to refill RBs even when sleep enabled",
6122306b72aSZbigniew Bodek 		    __func__);
6132306b72aSZbigniew Bodek 	}
6143c0086b8SZbigniew Bodek }
6153c0086b8SZbigniew Bodek 
6162306b72aSZbigniew Bodek /* Refill RBs as soon as possible without waiting */
6172306b72aSZbigniew Bodek static void
nicvf_rbdr_task_nowait(void * arg,int pending)6182306b72aSZbigniew Bodek nicvf_rbdr_task_nowait(void *arg, int pending)
6193c0086b8SZbigniew Bodek {
6202306b72aSZbigniew Bodek 	struct rbdr *rbdr;
6212306b72aSZbigniew Bodek 	int err;
6223c0086b8SZbigniew Bodek 
6232306b72aSZbigniew Bodek 	rbdr = (struct rbdr *)arg;
6242306b72aSZbigniew Bodek 
6252306b72aSZbigniew Bodek 	err = nicvf_refill_rbdr(rbdr, M_NOWAIT);
6262306b72aSZbigniew Bodek 	if (err != 0) {
6272306b72aSZbigniew Bodek 		/*
6282306b72aSZbigniew Bodek 		 * Schedule another, sleepable kernel thread
6292306b72aSZbigniew Bodek 		 * that will for sure refill the buffers.
6302306b72aSZbigniew Bodek 		 */
6312306b72aSZbigniew Bodek 		taskqueue_enqueue(taskqueue_thread, &rbdr->rbdr_task);
6323c0086b8SZbigniew Bodek 	}
6333c0086b8SZbigniew Bodek }
6343c0086b8SZbigniew Bodek 
6352306b72aSZbigniew Bodek static int
nicvf_rcv_pkt_handler(struct nicvf * nic,struct cmp_queue * cq,struct cqe_rx_t * cqe_rx,int cqe_type)6362306b72aSZbigniew Bodek nicvf_rcv_pkt_handler(struct nicvf *nic, struct cmp_queue *cq,
6372306b72aSZbigniew Bodek     struct cqe_rx_t *cqe_rx, int cqe_type)
6382306b72aSZbigniew Bodek {
6392306b72aSZbigniew Bodek 	struct mbuf *mbuf;
640053f3d0eSZbigniew Bodek 	struct rcv_queue *rq;
6412306b72aSZbigniew Bodek 	int rq_idx;
6422306b72aSZbigniew Bodek 	int err = 0;
6432306b72aSZbigniew Bodek 
6442306b72aSZbigniew Bodek 	rq_idx = cqe_rx->rq_idx;
645053f3d0eSZbigniew Bodek 	rq = &nic->qs->rq[rq_idx];
6462306b72aSZbigniew Bodek 
6472306b72aSZbigniew Bodek 	/* Check for errors */
6482306b72aSZbigniew Bodek 	err = nicvf_check_cqe_rx_errs(nic, cq, cqe_rx);
6492306b72aSZbigniew Bodek 	if (err && !cqe_rx->rb_cnt)
6502306b72aSZbigniew Bodek 		return (0);
6512306b72aSZbigniew Bodek 
6522306b72aSZbigniew Bodek 	mbuf = nicvf_get_rcv_mbuf(nic, cqe_rx);
6532306b72aSZbigniew Bodek 	if (mbuf == NULL) {
6542306b72aSZbigniew Bodek 		dprintf(nic->dev, "Packet not received\n");
6552306b72aSZbigniew Bodek 		return (0);
6562306b72aSZbigniew Bodek 	}
6572306b72aSZbigniew Bodek 
6582306b72aSZbigniew Bodek 	/* If error packet */
6592306b72aSZbigniew Bodek 	if (err != 0) {
6602306b72aSZbigniew Bodek 		m_freem(mbuf);
6612306b72aSZbigniew Bodek 		return (0);
6622306b72aSZbigniew Bodek 	}
6632306b72aSZbigniew Bodek 
664053f3d0eSZbigniew Bodek 	if (rq->lro_enabled &&
665053f3d0eSZbigniew Bodek 	    ((cqe_rx->l3_type == L3TYPE_IPV4) && (cqe_rx->l4_type == L4TYPE_TCP)) &&
666053f3d0eSZbigniew Bodek 	    (mbuf->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) ==
667053f3d0eSZbigniew Bodek             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) {
668053f3d0eSZbigniew Bodek 		/*
669053f3d0eSZbigniew Bodek 		 * At this point it is known that there are no errors in the
670053f3d0eSZbigniew Bodek 		 * packet. Attempt to LRO enqueue. Send to stack if no resources
671053f3d0eSZbigniew Bodek 		 * or enqueue error.
672053f3d0eSZbigniew Bodek 		 */
673053f3d0eSZbigniew Bodek 		if ((rq->lro.lro_cnt != 0) &&
674053f3d0eSZbigniew Bodek 		    (tcp_lro_rx(&rq->lro, mbuf, 0) == 0))
675053f3d0eSZbigniew Bodek 			return (0);
676053f3d0eSZbigniew Bodek 	}
6772306b72aSZbigniew Bodek 	/*
6782306b72aSZbigniew Bodek 	 * Push this packet to the stack later to avoid
6792306b72aSZbigniew Bodek 	 * unlocking completion task in the middle of work.
6802306b72aSZbigniew Bodek 	 */
6812306b72aSZbigniew Bodek 	err = buf_ring_enqueue(cq->rx_br, mbuf);
6822306b72aSZbigniew Bodek 	if (err != 0) {
6832306b72aSZbigniew Bodek 		/*
6842306b72aSZbigniew Bodek 		 * Failed to enqueue this mbuf.
6852306b72aSZbigniew Bodek 		 * We don't drop it, just schedule another task.
6862306b72aSZbigniew Bodek 		 */
6872306b72aSZbigniew Bodek 		return (err);
6882306b72aSZbigniew Bodek 	}
6892306b72aSZbigniew Bodek 
6902306b72aSZbigniew Bodek 	return (0);
6912306b72aSZbigniew Bodek }
6922306b72aSZbigniew Bodek 
693f6cdb4ceSZbigniew Bodek static void
nicvf_snd_pkt_handler(struct nicvf * nic,struct cmp_queue * cq,struct cqe_send_t * cqe_tx,int cqe_type)6942306b72aSZbigniew Bodek nicvf_snd_pkt_handler(struct nicvf *nic, struct cmp_queue *cq,
6952306b72aSZbigniew Bodek     struct cqe_send_t *cqe_tx, int cqe_type)
6962306b72aSZbigniew Bodek {
6972306b72aSZbigniew Bodek 	bus_dmamap_t dmap;
6982306b72aSZbigniew Bodek 	struct mbuf *mbuf;
6992306b72aSZbigniew Bodek 	struct snd_queue *sq;
7002306b72aSZbigniew Bodek 	struct sq_hdr_subdesc *hdr;
7012306b72aSZbigniew Bodek 
7022306b72aSZbigniew Bodek 	mbuf = NULL;
7032306b72aSZbigniew Bodek 	sq = &nic->qs->sq[cqe_tx->sq_idx];
7042306b72aSZbigniew Bodek 
7052306b72aSZbigniew Bodek 	hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, cqe_tx->sqe_ptr);
706f6cdb4ceSZbigniew Bodek 	if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER)
707f6cdb4ceSZbigniew Bodek 		return;
7082306b72aSZbigniew Bodek 
7092306b72aSZbigniew Bodek 	dprintf(nic->dev,
7102306b72aSZbigniew Bodek 	    "%s Qset #%d SQ #%d SQ ptr #%d subdesc count %d\n",
7112306b72aSZbigniew Bodek 	    __func__, cqe_tx->sq_qs, cqe_tx->sq_idx,
7122306b72aSZbigniew Bodek 	    cqe_tx->sqe_ptr, hdr->subdesc_cnt);
7132306b72aSZbigniew Bodek 
7142306b72aSZbigniew Bodek 	dmap = (bus_dmamap_t)sq->snd_buff[cqe_tx->sqe_ptr].dmap;
7152306b72aSZbigniew Bodek 	bus_dmamap_unload(sq->snd_buff_dmat, dmap);
7162306b72aSZbigniew Bodek 
7172306b72aSZbigniew Bodek 	mbuf = (struct mbuf *)sq->snd_buff[cqe_tx->sqe_ptr].mbuf;
7182306b72aSZbigniew Bodek 	if (mbuf != NULL) {
7192306b72aSZbigniew Bodek 		m_freem(mbuf);
7202306b72aSZbigniew Bodek 		sq->snd_buff[cqe_tx->sqe_ptr].mbuf = NULL;
7216cac5eb7SZbigniew Bodek 		nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
7222306b72aSZbigniew Bodek 	}
7232306b72aSZbigniew Bodek 
7242306b72aSZbigniew Bodek 	nicvf_check_cqe_tx_errs(nic, cq, cqe_tx);
7252306b72aSZbigniew Bodek }
7262306b72aSZbigniew Bodek 
7272306b72aSZbigniew Bodek static int
nicvf_cq_intr_handler(struct nicvf * nic,uint8_t cq_idx)7282306b72aSZbigniew Bodek nicvf_cq_intr_handler(struct nicvf *nic, uint8_t cq_idx)
7292306b72aSZbigniew Bodek {
7302306b72aSZbigniew Bodek 	struct mbuf *mbuf;
731b9545c57SJustin Hibbits 	if_t ifp;
732a67c0faeSWarner Losh 	int processed_cqe, tx_done = 0;
733a67c0faeSWarner Losh #ifdef DEBUG
734a67c0faeSWarner Losh 	int work_done = 0;
735a67c0faeSWarner Losh #endif
7362306b72aSZbigniew Bodek 	int cqe_count, cqe_head;
7372306b72aSZbigniew Bodek 	struct queue_set *qs = nic->qs;
7382306b72aSZbigniew Bodek 	struct cmp_queue *cq = &qs->cq[cq_idx];
7397c617aceSZbigniew Bodek 	struct snd_queue *sq = &qs->sq[cq_idx];
740053f3d0eSZbigniew Bodek 	struct rcv_queue *rq;
7412306b72aSZbigniew Bodek 	struct cqe_rx_t *cq_desc;
742053f3d0eSZbigniew Bodek 	struct lro_ctrl	*lro;
743053f3d0eSZbigniew Bodek 	int rq_idx;
7442306b72aSZbigniew Bodek 	int cmp_err;
7452306b72aSZbigniew Bodek 
7462306b72aSZbigniew Bodek 	NICVF_CMP_LOCK(cq);
7472306b72aSZbigniew Bodek 	cmp_err = 0;
7482306b72aSZbigniew Bodek 	processed_cqe = 0;
7492306b72aSZbigniew Bodek 	/* Get no of valid CQ entries to process */
7502306b72aSZbigniew Bodek 	cqe_count = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, cq_idx);
7512306b72aSZbigniew Bodek 	cqe_count &= CQ_CQE_COUNT;
7522306b72aSZbigniew Bodek 	if (cqe_count == 0)
7532306b72aSZbigniew Bodek 		goto out;
7542306b72aSZbigniew Bodek 
7552306b72aSZbigniew Bodek 	/* Get head of the valid CQ entries */
7562306b72aSZbigniew Bodek 	cqe_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, cq_idx) >> 9;
7572306b72aSZbigniew Bodek 	cqe_head &= 0xFFFF;
7582306b72aSZbigniew Bodek 
7592306b72aSZbigniew Bodek 	dprintf(nic->dev, "%s CQ%d cqe_count %d cqe_head %d\n",
7602306b72aSZbigniew Bodek 	    __func__, cq_idx, cqe_count, cqe_head);
7612306b72aSZbigniew Bodek 	while (processed_cqe < cqe_count) {
7622306b72aSZbigniew Bodek 		/* Get the CQ descriptor */
7632306b72aSZbigniew Bodek 		cq_desc = (struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head);
7642306b72aSZbigniew Bodek 		cqe_head++;
7652306b72aSZbigniew Bodek 		cqe_head &= (cq->dmem.q_len - 1);
76644dd2693SZbigniew Bodek 		/* Prefetch next CQ descriptor */
76744dd2693SZbigniew Bodek 		__builtin_prefetch((struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head));
7682306b72aSZbigniew Bodek 
7692306b72aSZbigniew Bodek 		dprintf(nic->dev, "CQ%d cq_desc->cqe_type %d\n", cq_idx,
7702306b72aSZbigniew Bodek 		    cq_desc->cqe_type);
7712306b72aSZbigniew Bodek 		switch (cq_desc->cqe_type) {
7722306b72aSZbigniew Bodek 		case CQE_TYPE_RX:
7732306b72aSZbigniew Bodek 			cmp_err = nicvf_rcv_pkt_handler(nic, cq, cq_desc,
7742306b72aSZbigniew Bodek 			    CQE_TYPE_RX);
7752306b72aSZbigniew Bodek 			if (__predict_false(cmp_err != 0)) {
7762306b72aSZbigniew Bodek 				/*
7772306b72aSZbigniew Bodek 				 * Ups. Cannot finish now.
7782306b72aSZbigniew Bodek 				 * Let's try again later.
7792306b72aSZbigniew Bodek 				 */
7802306b72aSZbigniew Bodek 				goto done;
7812306b72aSZbigniew Bodek 			}
782a67c0faeSWarner Losh #ifdef DEBUG
7832306b72aSZbigniew Bodek 			work_done++;
784a67c0faeSWarner Losh #endif
7852306b72aSZbigniew Bodek 			break;
7862306b72aSZbigniew Bodek 		case CQE_TYPE_SEND:
787f6cdb4ceSZbigniew Bodek 			nicvf_snd_pkt_handler(nic, cq, (void *)cq_desc,
788f6cdb4ceSZbigniew Bodek 			    CQE_TYPE_SEND);
7892306b72aSZbigniew Bodek 			tx_done++;
7902306b72aSZbigniew Bodek 			break;
7912306b72aSZbigniew Bodek 		case CQE_TYPE_INVALID:
7922306b72aSZbigniew Bodek 		case CQE_TYPE_RX_SPLIT:
7932306b72aSZbigniew Bodek 		case CQE_TYPE_RX_TCP:
7942306b72aSZbigniew Bodek 		case CQE_TYPE_SEND_PTP:
7952306b72aSZbigniew Bodek 			/* Ignore for now */
7962306b72aSZbigniew Bodek 			break;
7972306b72aSZbigniew Bodek 		}
7982306b72aSZbigniew Bodek 		processed_cqe++;
7992306b72aSZbigniew Bodek 	}
8002306b72aSZbigniew Bodek done:
8012306b72aSZbigniew Bodek 	dprintf(nic->dev,
8022306b72aSZbigniew Bodek 	    "%s CQ%d processed_cqe %d work_done %d\n",
8032306b72aSZbigniew Bodek 	    __func__, cq_idx, processed_cqe, work_done);
8042306b72aSZbigniew Bodek 
8052306b72aSZbigniew Bodek 	/* Ring doorbell to inform H/W to reuse processed CQEs */
8062306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_DOOR, cq_idx, processed_cqe);
8072306b72aSZbigniew Bodek 
8082306b72aSZbigniew Bodek 	if ((tx_done > 0) &&
8092306b72aSZbigniew Bodek 	    ((if_getdrvflags(nic->ifp) & IFF_DRV_RUNNING) != 0)) {
8102306b72aSZbigniew Bodek 		/* Reenable TXQ if its stopped earlier due to SQ full */
8112306b72aSZbigniew Bodek 		if_setdrvflagbits(nic->ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
8127c617aceSZbigniew Bodek 		taskqueue_enqueue(sq->snd_taskq, &sq->snd_task);
8132306b72aSZbigniew Bodek 	}
8142306b72aSZbigniew Bodek out:
815053f3d0eSZbigniew Bodek 	/*
816053f3d0eSZbigniew Bodek 	 * Flush any outstanding LRO work
817053f3d0eSZbigniew Bodek 	 */
818053f3d0eSZbigniew Bodek 	rq_idx = cq_idx;
819053f3d0eSZbigniew Bodek 	rq = &nic->qs->rq[rq_idx];
820053f3d0eSZbigniew Bodek 	lro = &rq->lro;
8216dd38b87SSepherosa Ziehau 	tcp_lro_flush_all(lro);
822053f3d0eSZbigniew Bodek 
8232306b72aSZbigniew Bodek 	NICVF_CMP_UNLOCK(cq);
8242306b72aSZbigniew Bodek 
8252306b72aSZbigniew Bodek 	ifp = nic->ifp;
8262306b72aSZbigniew Bodek 	/* Push received MBUFs to the stack */
8272306b72aSZbigniew Bodek 	while (!buf_ring_empty(cq->rx_br)) {
8282306b72aSZbigniew Bodek 		mbuf = buf_ring_dequeue_mc(cq->rx_br);
8292306b72aSZbigniew Bodek 		if (__predict_true(mbuf != NULL))
830b9545c57SJustin Hibbits 			if_input(ifp, mbuf);
8312306b72aSZbigniew Bodek 	}
8322306b72aSZbigniew Bodek 
8332306b72aSZbigniew Bodek 	return (cmp_err);
8342306b72aSZbigniew Bodek }
8352306b72aSZbigniew Bodek 
8362306b72aSZbigniew Bodek /*
8372306b72aSZbigniew Bodek  * Qset error interrupt handler
8382306b72aSZbigniew Bodek  *
8392306b72aSZbigniew Bodek  * As of now only CQ errors are handled
8402306b72aSZbigniew Bodek  */
8412306b72aSZbigniew Bodek static void
nicvf_qs_err_task(void * arg,int pending)8422306b72aSZbigniew Bodek nicvf_qs_err_task(void *arg, int pending)
8432306b72aSZbigniew Bodek {
8442306b72aSZbigniew Bodek 	struct nicvf *nic;
8452306b72aSZbigniew Bodek 	struct queue_set *qs;
8462306b72aSZbigniew Bodek 	int qidx;
8472306b72aSZbigniew Bodek 	uint64_t status;
8482306b72aSZbigniew Bodek 	boolean_t enable = TRUE;
8492306b72aSZbigniew Bodek 
8502306b72aSZbigniew Bodek 	nic = (struct nicvf *)arg;
8512306b72aSZbigniew Bodek 	qs = nic->qs;
8522306b72aSZbigniew Bodek 
8532306b72aSZbigniew Bodek 	/* Deactivate network interface */
8542306b72aSZbigniew Bodek 	if_setdrvflagbits(nic->ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
8552306b72aSZbigniew Bodek 
8562306b72aSZbigniew Bodek 	/* Check if it is CQ err */
8572306b72aSZbigniew Bodek 	for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
8582306b72aSZbigniew Bodek 		status = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS,
8592306b72aSZbigniew Bodek 		    qidx);
8602306b72aSZbigniew Bodek 		if ((status & CQ_ERR_MASK) == 0)
8612306b72aSZbigniew Bodek 			continue;
8622306b72aSZbigniew Bodek 		/* Process already queued CQEs and reconfig CQ */
8632306b72aSZbigniew Bodek 		nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
8642306b72aSZbigniew Bodek 		nicvf_sq_disable(nic, qidx);
8652306b72aSZbigniew Bodek 		(void)nicvf_cq_intr_handler(nic, qidx);
8662306b72aSZbigniew Bodek 		nicvf_cmp_queue_config(nic, qs, qidx, enable);
8672306b72aSZbigniew Bodek 		nicvf_sq_free_used_descs(nic, &qs->sq[qidx], qidx);
8682306b72aSZbigniew Bodek 		nicvf_sq_enable(nic, &qs->sq[qidx], qidx);
8692306b72aSZbigniew Bodek 		nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
8702306b72aSZbigniew Bodek 	}
8712306b72aSZbigniew Bodek 
8722306b72aSZbigniew Bodek 	if_setdrvflagbits(nic->ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
8732306b72aSZbigniew Bodek 	/* Re-enable Qset error interrupt */
8742306b72aSZbigniew Bodek 	nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
8752306b72aSZbigniew Bodek }
8762306b72aSZbigniew Bodek 
8772306b72aSZbigniew Bodek static void
nicvf_cmp_task(void * arg,int pending)8782306b72aSZbigniew Bodek nicvf_cmp_task(void *arg, int pending)
8792306b72aSZbigniew Bodek {
8802306b72aSZbigniew Bodek 	struct cmp_queue *cq;
8812306b72aSZbigniew Bodek 	struct nicvf *nic;
8822306b72aSZbigniew Bodek 	int cmp_err;
8832306b72aSZbigniew Bodek 
8842306b72aSZbigniew Bodek 	cq = (struct cmp_queue *)arg;
8852306b72aSZbigniew Bodek 	nic = cq->nic;
8862306b72aSZbigniew Bodek 
8872306b72aSZbigniew Bodek 	/* Handle CQ descriptors */
8882306b72aSZbigniew Bodek 	cmp_err = nicvf_cq_intr_handler(nic, cq->idx);
8892306b72aSZbigniew Bodek 	if (__predict_false(cmp_err != 0)) {
8902306b72aSZbigniew Bodek 		/*
8912306b72aSZbigniew Bodek 		 * Schedule another thread here since we did not
8922306b72aSZbigniew Bodek 		 * process the entire CQ due to Tx or Rx CQ parse error.
8932306b72aSZbigniew Bodek 		 */
8942306b72aSZbigniew Bodek 		taskqueue_enqueue(cq->cmp_taskq, &cq->cmp_task);
8952306b72aSZbigniew Bodek 	}
8962306b72aSZbigniew Bodek 
8970196c2e8SZbigniew Bodek 	nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->idx);
8982306b72aSZbigniew Bodek 	/* Reenable interrupt (previously disabled in nicvf_intr_handler() */
8992306b72aSZbigniew Bodek 	nicvf_enable_intr(nic, NICVF_INTR_CQ, cq->idx);
9002306b72aSZbigniew Bodek 
9012306b72aSZbigniew Bodek }
9022306b72aSZbigniew Bodek 
9033c0086b8SZbigniew Bodek /* Initialize completion queue */
9042306b72aSZbigniew Bodek static int
nicvf_init_cmp_queue(struct nicvf * nic,struct cmp_queue * cq,int q_len,int qidx)9052306b72aSZbigniew Bodek nicvf_init_cmp_queue(struct nicvf *nic, struct cmp_queue *cq, int q_len,
9062306b72aSZbigniew Bodek     int qidx)
9073c0086b8SZbigniew Bodek {
9083c0086b8SZbigniew Bodek 	int err;
9093c0086b8SZbigniew Bodek 
9102306b72aSZbigniew Bodek 	/* Initizalize lock */
9112306b72aSZbigniew Bodek 	snprintf(cq->mtx_name, sizeof(cq->mtx_name), "%s: CQ(%d) lock",
9122306b72aSZbigniew Bodek 	    device_get_nameunit(nic->dev), qidx);
9132306b72aSZbigniew Bodek 	mtx_init(&cq->mtx, cq->mtx_name, NULL, MTX_DEF);
9142306b72aSZbigniew Bodek 
9153c0086b8SZbigniew Bodek 	err = nicvf_alloc_q_desc_mem(nic, &cq->dmem, q_len, CMP_QUEUE_DESC_SIZE,
9163c0086b8SZbigniew Bodek 				     NICVF_CQ_BASE_ALIGN_BYTES);
9172306b72aSZbigniew Bodek 
9182306b72aSZbigniew Bodek 	if (err != 0) {
9192306b72aSZbigniew Bodek 		device_printf(nic->dev,
9202306b72aSZbigniew Bodek 		    "Could not allocate DMA memory for CQ\n");
9212306b72aSZbigniew Bodek 		return (err);
9222306b72aSZbigniew Bodek 	}
9233c0086b8SZbigniew Bodek 
9243c0086b8SZbigniew Bodek 	cq->desc = cq->dmem.base;
9253bdcfeadSZbigniew Bodek 	cq->thresh = pass1_silicon(nic->dev) ? 0 : CMP_QUEUE_CQE_THRESH;
9262306b72aSZbigniew Bodek 	cq->nic = nic;
9272306b72aSZbigniew Bodek 	cq->idx = qidx;
9283c0086b8SZbigniew Bodek 	nic->cq_coalesce_usecs = (CMP_QUEUE_TIMER_THRESH * 0.05) - 1;
9293c0086b8SZbigniew Bodek 
9302306b72aSZbigniew Bodek 	cq->rx_br = buf_ring_alloc(CMP_QUEUE_LEN * 8, M_DEVBUF, M_WAITOK,
9312306b72aSZbigniew Bodek 	    &cq->mtx);
9322306b72aSZbigniew Bodek 
9332306b72aSZbigniew Bodek 	/* Allocate taskqueue */
9346c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&cq->cmp_task, 0, nicvf_cmp_task, cq);
9352306b72aSZbigniew Bodek 	cq->cmp_taskq = taskqueue_create_fast("nicvf_cmp_taskq", M_WAITOK,
9362306b72aSZbigniew Bodek 	    taskqueue_thread_enqueue, &cq->cmp_taskq);
9372306b72aSZbigniew Bodek 	taskqueue_start_threads(&cq->cmp_taskq, 1, PI_NET, "%s: cmp_taskq(%d)",
9382306b72aSZbigniew Bodek 	    device_get_nameunit(nic->dev), qidx);
9392306b72aSZbigniew Bodek 
9402306b72aSZbigniew Bodek 	return (0);
9413c0086b8SZbigniew Bodek }
9423c0086b8SZbigniew Bodek 
9432306b72aSZbigniew Bodek static void
nicvf_free_cmp_queue(struct nicvf * nic,struct cmp_queue * cq)9442306b72aSZbigniew Bodek nicvf_free_cmp_queue(struct nicvf *nic, struct cmp_queue *cq)
9453c0086b8SZbigniew Bodek {
9463c0086b8SZbigniew Bodek 
9472306b72aSZbigniew Bodek 	if (cq == NULL)
9482306b72aSZbigniew Bodek 		return;
9492306b72aSZbigniew Bodek 	/*
9502306b72aSZbigniew Bodek 	 * The completion queue itself should be disabled by now
9512306b72aSZbigniew Bodek 	 * (ref. nicvf_snd_queue_config()).
9522306b72aSZbigniew Bodek 	 * Ensure that it is safe to disable it or panic.
9532306b72aSZbigniew Bodek 	 */
9542306b72aSZbigniew Bodek 	if (cq->enable)
9552306b72aSZbigniew Bodek 		panic("%s: Trying to free working CQ(%d)", __func__, cq->idx);
9562306b72aSZbigniew Bodek 
9572306b72aSZbigniew Bodek 	if (cq->cmp_taskq != NULL) {
9582306b72aSZbigniew Bodek 		/* Remove task */
9592306b72aSZbigniew Bodek 		while (taskqueue_cancel(cq->cmp_taskq, &cq->cmp_task, NULL) != 0)
9602306b72aSZbigniew Bodek 			taskqueue_drain(cq->cmp_taskq, &cq->cmp_task);
9612306b72aSZbigniew Bodek 
9622306b72aSZbigniew Bodek 		taskqueue_free(cq->cmp_taskq);
9632306b72aSZbigniew Bodek 		cq->cmp_taskq = NULL;
9642306b72aSZbigniew Bodek 	}
9652306b72aSZbigniew Bodek 	/*
9662306b72aSZbigniew Bodek 	 * Completion interrupt will possibly enable interrupts again
9672306b72aSZbigniew Bodek 	 * so disable interrupting now after we finished processing
9682306b72aSZbigniew Bodek 	 * completion task. It is safe to do so since the corresponding CQ
9692306b72aSZbigniew Bodek 	 * was already disabled.
9702306b72aSZbigniew Bodek 	 */
9712306b72aSZbigniew Bodek 	nicvf_disable_intr(nic, NICVF_INTR_CQ, cq->idx);
9722306b72aSZbigniew Bodek 	nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->idx);
9732306b72aSZbigniew Bodek 
9742306b72aSZbigniew Bodek 	NICVF_CMP_LOCK(cq);
9753c0086b8SZbigniew Bodek 	nicvf_free_q_desc_mem(nic, &cq->dmem);
9762306b72aSZbigniew Bodek 	drbr_free(cq->rx_br, M_DEVBUF);
9772306b72aSZbigniew Bodek 	NICVF_CMP_UNLOCK(cq);
9782306b72aSZbigniew Bodek 	mtx_destroy(&cq->mtx);
9792306b72aSZbigniew Bodek 	memset(cq->mtx_name, 0, sizeof(cq->mtx_name));
9802306b72aSZbigniew Bodek }
9812306b72aSZbigniew Bodek 
9827c617aceSZbigniew Bodek int
nicvf_xmit_locked(struct snd_queue * sq)9837c617aceSZbigniew Bodek nicvf_xmit_locked(struct snd_queue *sq)
9847c617aceSZbigniew Bodek {
9857c617aceSZbigniew Bodek 	struct nicvf *nic;
986b9545c57SJustin Hibbits 	if_t ifp;
9877c617aceSZbigniew Bodek 	struct mbuf *next;
9887c617aceSZbigniew Bodek 	int err;
9897c617aceSZbigniew Bodek 
9907c617aceSZbigniew Bodek 	NICVF_TX_LOCK_ASSERT(sq);
9917c617aceSZbigniew Bodek 
9927c617aceSZbigniew Bodek 	nic = sq->nic;
9937c617aceSZbigniew Bodek 	ifp = nic->ifp;
9947c617aceSZbigniew Bodek 	err = 0;
9957c617aceSZbigniew Bodek 
9967c617aceSZbigniew Bodek 	while ((next = drbr_peek(ifp, sq->br)) != NULL) {
9972dc620acSEd Maste 		/* Send a copy of the frame to the BPF listener */
9982dc620acSEd Maste 		ETHER_BPF_MTAP(ifp, next);
9992dc620acSEd Maste 
10007c617aceSZbigniew Bodek 		err = nicvf_tx_mbuf_locked(sq, &next);
10017c617aceSZbigniew Bodek 		if (err != 0) {
10027c617aceSZbigniew Bodek 			if (next == NULL)
10037c617aceSZbigniew Bodek 				drbr_advance(ifp, sq->br);
10047c617aceSZbigniew Bodek 			else
10057c617aceSZbigniew Bodek 				drbr_putback(ifp, sq->br, next);
10067c617aceSZbigniew Bodek 
10077c617aceSZbigniew Bodek 			break;
10087c617aceSZbigniew Bodek 		}
10097c617aceSZbigniew Bodek 		drbr_advance(ifp, sq->br);
10107c617aceSZbigniew Bodek 	}
10117c617aceSZbigniew Bodek 	return (err);
10127c617aceSZbigniew Bodek }
10137c617aceSZbigniew Bodek 
10142306b72aSZbigniew Bodek static void
nicvf_snd_task(void * arg,int pending)10152306b72aSZbigniew Bodek nicvf_snd_task(void *arg, int pending)
10162306b72aSZbigniew Bodek {
10172306b72aSZbigniew Bodek 	struct snd_queue *sq = (struct snd_queue *)arg;
10187c617aceSZbigniew Bodek 	struct nicvf *nic;
1019b9545c57SJustin Hibbits 	if_t ifp;
10207c617aceSZbigniew Bodek 	int err;
10217c617aceSZbigniew Bodek 
10227c617aceSZbigniew Bodek 	nic = sq->nic;
10237c617aceSZbigniew Bodek 	ifp = nic->ifp;
10247c617aceSZbigniew Bodek 
10257c617aceSZbigniew Bodek 	/*
10267c617aceSZbigniew Bodek 	 * Skip sending anything if the driver is not running,
10277c617aceSZbigniew Bodek 	 * SQ full or link is down.
10287c617aceSZbigniew Bodek 	 */
10297c617aceSZbigniew Bodek 	if (((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
10307c617aceSZbigniew Bodek 	    IFF_DRV_RUNNING) || !nic->link_up)
10317c617aceSZbigniew Bodek 		return;
10322306b72aSZbigniew Bodek 
10332306b72aSZbigniew Bodek 	NICVF_TX_LOCK(sq);
10347c617aceSZbigniew Bodek 	err = nicvf_xmit_locked(sq);
10352306b72aSZbigniew Bodek 	NICVF_TX_UNLOCK(sq);
10367c617aceSZbigniew Bodek 	/* Try again */
10377c617aceSZbigniew Bodek 	if (err != 0)
10387c617aceSZbigniew Bodek 		taskqueue_enqueue(sq->snd_taskq, &sq->snd_task);
10393c0086b8SZbigniew Bodek }
10403c0086b8SZbigniew Bodek 
10413c0086b8SZbigniew Bodek /* Initialize transmit queue */
10422306b72aSZbigniew Bodek static int
nicvf_init_snd_queue(struct nicvf * nic,struct snd_queue * sq,int q_len,int qidx)10432306b72aSZbigniew Bodek nicvf_init_snd_queue(struct nicvf *nic, struct snd_queue *sq, int q_len,
10442306b72aSZbigniew Bodek     int qidx)
10453c0086b8SZbigniew Bodek {
10462306b72aSZbigniew Bodek 	size_t i;
10473c0086b8SZbigniew Bodek 	int err;
10483c0086b8SZbigniew Bodek 
10492306b72aSZbigniew Bodek 	/* Initizalize TX lock for this queue */
10502306b72aSZbigniew Bodek 	snprintf(sq->mtx_name, sizeof(sq->mtx_name), "%s: SQ(%d) lock",
10512306b72aSZbigniew Bodek 	    device_get_nameunit(nic->dev), qidx);
10522306b72aSZbigniew Bodek 	mtx_init(&sq->mtx, sq->mtx_name, NULL, MTX_DEF);
10532306b72aSZbigniew Bodek 
10542306b72aSZbigniew Bodek 	NICVF_TX_LOCK(sq);
10552306b72aSZbigniew Bodek 	/* Allocate buffer ring */
10562306b72aSZbigniew Bodek 	sq->br = buf_ring_alloc(q_len / MIN_SQ_DESC_PER_PKT_XMIT, M_DEVBUF,
10572306b72aSZbigniew Bodek 	    M_NOWAIT, &sq->mtx);
10582306b72aSZbigniew Bodek 	if (sq->br == NULL) {
10592306b72aSZbigniew Bodek 		device_printf(nic->dev,
10602306b72aSZbigniew Bodek 		    "ERROR: Could not set up buf ring for SQ(%d)\n", qidx);
10612306b72aSZbigniew Bodek 		err = ENOMEM;
10622306b72aSZbigniew Bodek 		goto error;
10632306b72aSZbigniew Bodek 	}
10642306b72aSZbigniew Bodek 
10652306b72aSZbigniew Bodek 	/* Allocate DMA memory for Tx descriptors */
10663c0086b8SZbigniew Bodek 	err = nicvf_alloc_q_desc_mem(nic, &sq->dmem, q_len, SND_QUEUE_DESC_SIZE,
10673c0086b8SZbigniew Bodek 				     NICVF_SQ_BASE_ALIGN_BYTES);
10682306b72aSZbigniew Bodek 	if (err != 0) {
10692306b72aSZbigniew Bodek 		device_printf(nic->dev,
10702306b72aSZbigniew Bodek 		    "Could not allocate DMA memory for SQ\n");
10712306b72aSZbigniew Bodek 		goto error;
10722306b72aSZbigniew Bodek 	}
10733c0086b8SZbigniew Bodek 
10743c0086b8SZbigniew Bodek 	sq->desc = sq->dmem.base;
10752306b72aSZbigniew Bodek 	sq->head = sq->tail = 0;
1076f6cdb4ceSZbigniew Bodek 	atomic_store_rel_int(&sq->free_cnt, q_len - 1);
10773c0086b8SZbigniew Bodek 	sq->thresh = SND_QUEUE_THRESH;
10782306b72aSZbigniew Bodek 	sq->idx = qidx;
10792306b72aSZbigniew Bodek 	sq->nic = nic;
10803c0086b8SZbigniew Bodek 
10812306b72aSZbigniew Bodek 	/*
10822306b72aSZbigniew Bodek 	 * Allocate DMA maps for Tx buffers
10832306b72aSZbigniew Bodek 	 */
10843c0086b8SZbigniew Bodek 
10852306b72aSZbigniew Bodek 	/* Create DMA tag first */
10862306b72aSZbigniew Bodek 	err = bus_dma_tag_create(
10872306b72aSZbigniew Bodek 	    bus_get_dma_tag(nic->dev),		/* parent tag */
10882306b72aSZbigniew Bodek 	    1,					/* alignment */
10892306b72aSZbigniew Bodek 	    0,					/* boundary */
10902306b72aSZbigniew Bodek 	    BUS_SPACE_MAXADDR,			/* lowaddr */
10912306b72aSZbigniew Bodek 	    BUS_SPACE_MAXADDR,			/* highaddr */
10922306b72aSZbigniew Bodek 	    NULL, NULL,				/* filtfunc, filtfuncarg */
1093af8fe8f1SZbigniew Bodek 	    NICVF_TSO_MAXSIZE,			/* maxsize */
1094af8fe8f1SZbigniew Bodek 	    NICVF_TSO_NSEGS,			/* nsegments */
10952306b72aSZbigniew Bodek 	    MCLBYTES,				/* maxsegsize */
10962306b72aSZbigniew Bodek 	    0,					/* flags */
10972306b72aSZbigniew Bodek 	    NULL, NULL,				/* lockfunc, lockfuncarg */
10982306b72aSZbigniew Bodek 	    &sq->snd_buff_dmat);		/* dmat */
10992306b72aSZbigniew Bodek 
11002306b72aSZbigniew Bodek 	if (err != 0) {
11012306b72aSZbigniew Bodek 		device_printf(nic->dev,
11022306b72aSZbigniew Bodek 		    "Failed to create busdma tag for Tx buffers\n");
11032306b72aSZbigniew Bodek 		goto error;
11043c0086b8SZbigniew Bodek 	}
11053c0086b8SZbigniew Bodek 
11062306b72aSZbigniew Bodek 	/* Allocate send buffers array */
1107ac2fffa4SPedro F. Giffuni 	sq->snd_buff = malloc(sizeof(*sq->snd_buff) * q_len, M_NICVF,
11082306b72aSZbigniew Bodek 	    (M_NOWAIT | M_ZERO));
11092306b72aSZbigniew Bodek 	if (sq->snd_buff == NULL) {
11102306b72aSZbigniew Bodek 		device_printf(nic->dev,
11112306b72aSZbigniew Bodek 		    "Could not allocate memory for Tx buffers array\n");
11122306b72aSZbigniew Bodek 		err = ENOMEM;
11132306b72aSZbigniew Bodek 		goto error;
11142306b72aSZbigniew Bodek 	}
11152306b72aSZbigniew Bodek 
11162306b72aSZbigniew Bodek 	/* Now populate maps */
11172306b72aSZbigniew Bodek 	for (i = 0; i < q_len; i++) {
11182306b72aSZbigniew Bodek 		err = bus_dmamap_create(sq->snd_buff_dmat, 0,
11192306b72aSZbigniew Bodek 		    &sq->snd_buff[i].dmap);
11202306b72aSZbigniew Bodek 		if (err != 0) {
11212306b72aSZbigniew Bodek 			device_printf(nic->dev,
11222306b72aSZbigniew Bodek 			    "Failed to create DMA maps for Tx buffers\n");
11232306b72aSZbigniew Bodek 			goto error;
11242306b72aSZbigniew Bodek 		}
11252306b72aSZbigniew Bodek 	}
11262306b72aSZbigniew Bodek 	NICVF_TX_UNLOCK(sq);
11272306b72aSZbigniew Bodek 
11282306b72aSZbigniew Bodek 	/* Allocate taskqueue */
11292306b72aSZbigniew Bodek 	TASK_INIT(&sq->snd_task, 0, nicvf_snd_task, sq);
11302306b72aSZbigniew Bodek 	sq->snd_taskq = taskqueue_create_fast("nicvf_snd_taskq", M_WAITOK,
11312306b72aSZbigniew Bodek 	    taskqueue_thread_enqueue, &sq->snd_taskq);
11322306b72aSZbigniew Bodek 	taskqueue_start_threads(&sq->snd_taskq, 1, PI_NET, "%s: snd_taskq(%d)",
11332306b72aSZbigniew Bodek 	    device_get_nameunit(nic->dev), qidx);
11342306b72aSZbigniew Bodek 
11352306b72aSZbigniew Bodek 	return (0);
11362306b72aSZbigniew Bodek error:
11372306b72aSZbigniew Bodek 	NICVF_TX_UNLOCK(sq);
11382306b72aSZbigniew Bodek 	return (err);
11392306b72aSZbigniew Bodek }
11402306b72aSZbigniew Bodek 
11412306b72aSZbigniew Bodek static void
nicvf_free_snd_queue(struct nicvf * nic,struct snd_queue * sq)11422306b72aSZbigniew Bodek nicvf_free_snd_queue(struct nicvf *nic, struct snd_queue *sq)
11433c0086b8SZbigniew Bodek {
11442306b72aSZbigniew Bodek 	struct queue_set *qs = nic->qs;
11452306b72aSZbigniew Bodek 	size_t i;
114621653635SJohn Baldwin 	int err __diagused;
11472306b72aSZbigniew Bodek 
11482306b72aSZbigniew Bodek 	if (sq == NULL)
11493c0086b8SZbigniew Bodek 		return;
11503c0086b8SZbigniew Bodek 
11512306b72aSZbigniew Bodek 	if (sq->snd_taskq != NULL) {
11522306b72aSZbigniew Bodek 		/* Remove task */
11532306b72aSZbigniew Bodek 		while (taskqueue_cancel(sq->snd_taskq, &sq->snd_task, NULL) != 0)
11542306b72aSZbigniew Bodek 			taskqueue_drain(sq->snd_taskq, &sq->snd_task);
11553c0086b8SZbigniew Bodek 
11562306b72aSZbigniew Bodek 		taskqueue_free(sq->snd_taskq);
11572306b72aSZbigniew Bodek 		sq->snd_taskq = NULL;
11582306b72aSZbigniew Bodek 	}
11592306b72aSZbigniew Bodek 
11602306b72aSZbigniew Bodek 	NICVF_TX_LOCK(sq);
11612306b72aSZbigniew Bodek 	if (sq->snd_buff_dmat != NULL) {
11622306b72aSZbigniew Bodek 		if (sq->snd_buff != NULL) {
11632306b72aSZbigniew Bodek 			for (i = 0; i < qs->sq_len; i++) {
11642306b72aSZbigniew Bodek 				m_freem(sq->snd_buff[i].mbuf);
11652306b72aSZbigniew Bodek 				sq->snd_buff[i].mbuf = NULL;
11662306b72aSZbigniew Bodek 
11672306b72aSZbigniew Bodek 				bus_dmamap_unload(sq->snd_buff_dmat,
11682306b72aSZbigniew Bodek 				    sq->snd_buff[i].dmap);
11692306b72aSZbigniew Bodek 				err = bus_dmamap_destroy(sq->snd_buff_dmat,
11702306b72aSZbigniew Bodek 				    sq->snd_buff[i].dmap);
11712306b72aSZbigniew Bodek 				/*
11722306b72aSZbigniew Bodek 				 * If bus_dmamap_destroy fails it can cause
11732306b72aSZbigniew Bodek 				 * random panic later if the tag is also
11742306b72aSZbigniew Bodek 				 * destroyed in the process.
11752306b72aSZbigniew Bodek 				 */
11762306b72aSZbigniew Bodek 				KASSERT(err == 0,
11772306b72aSZbigniew Bodek 				    ("%s: Could not destroy DMA map for SQ",
11782306b72aSZbigniew Bodek 				    __func__));
11792306b72aSZbigniew Bodek 			}
11802306b72aSZbigniew Bodek 		}
11812306b72aSZbigniew Bodek 
11822306b72aSZbigniew Bodek 		free(sq->snd_buff, M_NICVF);
11832306b72aSZbigniew Bodek 
11842306b72aSZbigniew Bodek 		err = bus_dma_tag_destroy(sq->snd_buff_dmat);
11852306b72aSZbigniew Bodek 		KASSERT(err == 0,
11862306b72aSZbigniew Bodek 		    ("%s: Trying to destroy BUSY DMA tag", __func__));
11872306b72aSZbigniew Bodek 	}
11882306b72aSZbigniew Bodek 
11892306b72aSZbigniew Bodek 	/* Free private driver ring for this send queue */
11902306b72aSZbigniew Bodek 	if (sq->br != NULL)
11912306b72aSZbigniew Bodek 		drbr_free(sq->br, M_DEVBUF);
11922306b72aSZbigniew Bodek 
11932306b72aSZbigniew Bodek 	if (sq->dmem.base != NULL)
11943c0086b8SZbigniew Bodek 		nicvf_free_q_desc_mem(nic, &sq->dmem);
11952306b72aSZbigniew Bodek 
11962306b72aSZbigniew Bodek 	NICVF_TX_UNLOCK(sq);
11972306b72aSZbigniew Bodek 	/* Destroy Tx lock */
11982306b72aSZbigniew Bodek 	mtx_destroy(&sq->mtx);
11992306b72aSZbigniew Bodek 	memset(sq->mtx_name, 0, sizeof(sq->mtx_name));
12003c0086b8SZbigniew Bodek }
12013c0086b8SZbigniew Bodek 
12022306b72aSZbigniew Bodek static void
nicvf_reclaim_snd_queue(struct nicvf * nic,struct queue_set * qs,int qidx)12032306b72aSZbigniew Bodek nicvf_reclaim_snd_queue(struct nicvf *nic, struct queue_set *qs, int qidx)
12043c0086b8SZbigniew Bodek {
12052306b72aSZbigniew Bodek 
12063c0086b8SZbigniew Bodek 	/* Disable send queue */
12073c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, 0);
12083c0086b8SZbigniew Bodek 	/* Check if SQ is stopped */
12093c0086b8SZbigniew Bodek 	if (nicvf_poll_reg(nic, qidx, NIC_QSET_SQ_0_7_STATUS, 21, 1, 0x01))
12103c0086b8SZbigniew Bodek 		return;
12113c0086b8SZbigniew Bodek 	/* Reset send queue */
12123c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
12133c0086b8SZbigniew Bodek }
12143c0086b8SZbigniew Bodek 
12152306b72aSZbigniew Bodek static void
nicvf_reclaim_rcv_queue(struct nicvf * nic,struct queue_set * qs,int qidx)12162306b72aSZbigniew Bodek nicvf_reclaim_rcv_queue(struct nicvf *nic, struct queue_set *qs, int qidx)
12173c0086b8SZbigniew Bodek {
12183c0086b8SZbigniew Bodek 	union nic_mbx mbx = {};
12193c0086b8SZbigniew Bodek 
12203c0086b8SZbigniew Bodek 	/* Make sure all packets in the pipeline are written back into mem */
12213c0086b8SZbigniew Bodek 	mbx.msg.msg = NIC_MBOX_MSG_RQ_SW_SYNC;
12223c0086b8SZbigniew Bodek 	nicvf_send_msg_to_pf(nic, &mbx);
12233c0086b8SZbigniew Bodek }
12243c0086b8SZbigniew Bodek 
12252306b72aSZbigniew Bodek static void
nicvf_reclaim_cmp_queue(struct nicvf * nic,struct queue_set * qs,int qidx)12262306b72aSZbigniew Bodek nicvf_reclaim_cmp_queue(struct nicvf *nic, struct queue_set *qs, int qidx)
12273c0086b8SZbigniew Bodek {
12282306b72aSZbigniew Bodek 
12293c0086b8SZbigniew Bodek 	/* Disable timer threshold (doesn't get reset upon CQ reset */
12303c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx, 0);
12313c0086b8SZbigniew Bodek 	/* Disable completion queue */
12323c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, 0);
12333c0086b8SZbigniew Bodek 	/* Reset completion queue */
12343c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
12353c0086b8SZbigniew Bodek }
12363c0086b8SZbigniew Bodek 
12372306b72aSZbigniew Bodek static void
nicvf_reclaim_rbdr(struct nicvf * nic,struct rbdr * rbdr,int qidx)12382306b72aSZbigniew Bodek nicvf_reclaim_rbdr(struct nicvf *nic, struct rbdr *rbdr, int qidx)
12393c0086b8SZbigniew Bodek {
12402306b72aSZbigniew Bodek 	uint64_t tmp, fifo_state;
12413c0086b8SZbigniew Bodek 	int timeout = 10;
12423c0086b8SZbigniew Bodek 
12433c0086b8SZbigniew Bodek 	/* Save head and tail pointers for feeing up buffers */
12442306b72aSZbigniew Bodek 	rbdr->head =
12452306b72aSZbigniew Bodek 	    nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_HEAD, qidx) >> 3;
12462306b72aSZbigniew Bodek 	rbdr->tail =
12472306b72aSZbigniew Bodek 	    nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, qidx) >> 3;
12483c0086b8SZbigniew Bodek 
12492306b72aSZbigniew Bodek 	/*
12502306b72aSZbigniew Bodek 	 * If RBDR FIFO is in 'FAIL' state then do a reset first
12513c0086b8SZbigniew Bodek 	 * before relaiming.
12523c0086b8SZbigniew Bodek 	 */
12533c0086b8SZbigniew Bodek 	fifo_state = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_STATUS0, qidx);
12542306b72aSZbigniew Bodek 	if (((fifo_state >> 62) & 0x03) == 0x3) {
12553c0086b8SZbigniew Bodek 		nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG,
12563c0086b8SZbigniew Bodek 		    qidx, NICVF_RBDR_RESET);
12572306b72aSZbigniew Bodek 	}
12583c0086b8SZbigniew Bodek 
12593c0086b8SZbigniew Bodek 	/* Disable RBDR */
12603c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0);
12613c0086b8SZbigniew Bodek 	if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
12623c0086b8SZbigniew Bodek 		return;
12633c0086b8SZbigniew Bodek 	while (1) {
12643c0086b8SZbigniew Bodek 		tmp = nicvf_queue_reg_read(nic,
12652306b72aSZbigniew Bodek 		    NIC_QSET_RBDR_0_1_PREFETCH_STATUS, qidx);
12663c0086b8SZbigniew Bodek 		if ((tmp & 0xFFFFFFFF) == ((tmp >> 32) & 0xFFFFFFFF))
12673c0086b8SZbigniew Bodek 			break;
12682306b72aSZbigniew Bodek 
12692306b72aSZbigniew Bodek 		DELAY(1000);
12703c0086b8SZbigniew Bodek 		timeout--;
12713c0086b8SZbigniew Bodek 		if (!timeout) {
12722306b72aSZbigniew Bodek 			device_printf(nic->dev,
12733c0086b8SZbigniew Bodek 			    "Failed polling on prefetch status\n");
12743c0086b8SZbigniew Bodek 			return;
12753c0086b8SZbigniew Bodek 		}
12763c0086b8SZbigniew Bodek 	}
12772306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx,
12782306b72aSZbigniew Bodek 	    NICVF_RBDR_RESET);
12793c0086b8SZbigniew Bodek 
12803c0086b8SZbigniew Bodek 	if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x02))
12813c0086b8SZbigniew Bodek 		return;
12823c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx, 0x00);
12833c0086b8SZbigniew Bodek 	if (nicvf_poll_reg(nic, qidx, NIC_QSET_RBDR_0_1_STATUS0, 62, 2, 0x00))
12843c0086b8SZbigniew Bodek 		return;
12853c0086b8SZbigniew Bodek }
12863c0086b8SZbigniew Bodek 
12873c0086b8SZbigniew Bodek /* Configures receive queue */
12882306b72aSZbigniew Bodek static void
nicvf_rcv_queue_config(struct nicvf * nic,struct queue_set * qs,int qidx,bool enable)12892306b72aSZbigniew Bodek nicvf_rcv_queue_config(struct nicvf *nic, struct queue_set *qs,
12903c0086b8SZbigniew Bodek     int qidx, bool enable)
12913c0086b8SZbigniew Bodek {
12923c0086b8SZbigniew Bodek 	union nic_mbx mbx = {};
12933c0086b8SZbigniew Bodek 	struct rcv_queue *rq;
12943c0086b8SZbigniew Bodek 	struct rq_cfg rq_cfg;
1295b9545c57SJustin Hibbits 	if_t ifp;
1296053f3d0eSZbigniew Bodek 	struct lro_ctrl	*lro;
1297053f3d0eSZbigniew Bodek 
1298053f3d0eSZbigniew Bodek 	ifp = nic->ifp;
12993c0086b8SZbigniew Bodek 
13003c0086b8SZbigniew Bodek 	rq = &qs->rq[qidx];
13013c0086b8SZbigniew Bodek 	rq->enable = enable;
13023c0086b8SZbigniew Bodek 
1303053f3d0eSZbigniew Bodek 	lro = &rq->lro;
1304053f3d0eSZbigniew Bodek 
13053c0086b8SZbigniew Bodek 	/* Disable receive queue */
13063c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx, 0);
13073c0086b8SZbigniew Bodek 
13083c0086b8SZbigniew Bodek 	if (!rq->enable) {
13093c0086b8SZbigniew Bodek 		nicvf_reclaim_rcv_queue(nic, qs, qidx);
1310053f3d0eSZbigniew Bodek 		/* Free LRO memory */
1311053f3d0eSZbigniew Bodek 		tcp_lro_free(lro);
1312053f3d0eSZbigniew Bodek 		rq->lro_enabled = FALSE;
13133c0086b8SZbigniew Bodek 		return;
13143c0086b8SZbigniew Bodek 	}
13153c0086b8SZbigniew Bodek 
1316053f3d0eSZbigniew Bodek 	/* Configure LRO if enabled */
1317053f3d0eSZbigniew Bodek 	rq->lro_enabled = FALSE;
1318053f3d0eSZbigniew Bodek 	if ((if_getcapenable(ifp) & IFCAP_LRO) != 0) {
1319053f3d0eSZbigniew Bodek 		if (tcp_lro_init(lro) != 0) {
1320053f3d0eSZbigniew Bodek 			device_printf(nic->dev,
1321053f3d0eSZbigniew Bodek 			    "Failed to initialize LRO for RXQ%d\n", qidx);
1322053f3d0eSZbigniew Bodek 		} else {
1323053f3d0eSZbigniew Bodek 			rq->lro_enabled = TRUE;
1324053f3d0eSZbigniew Bodek 			lro->ifp = nic->ifp;
1325053f3d0eSZbigniew Bodek 		}
1326053f3d0eSZbigniew Bodek 	}
1327053f3d0eSZbigniew Bodek 
13283c0086b8SZbigniew Bodek 	rq->cq_qs = qs->vnic_id;
13293c0086b8SZbigniew Bodek 	rq->cq_idx = qidx;
13303c0086b8SZbigniew Bodek 	rq->start_rbdr_qs = qs->vnic_id;
13313c0086b8SZbigniew Bodek 	rq->start_qs_rbdr_idx = qs->rbdr_cnt - 1;
13323c0086b8SZbigniew Bodek 	rq->cont_rbdr_qs = qs->vnic_id;
13333c0086b8SZbigniew Bodek 	rq->cont_qs_rbdr_idx = qs->rbdr_cnt - 1;
13343c0086b8SZbigniew Bodek 	/* all writes of RBDR data to be loaded into L2 Cache as well*/
13353c0086b8SZbigniew Bodek 	rq->caching = 1;
13363c0086b8SZbigniew Bodek 
13373c0086b8SZbigniew Bodek 	/* Send a mailbox msg to PF to config RQ */
13383c0086b8SZbigniew Bodek 	mbx.rq.msg = NIC_MBOX_MSG_RQ_CFG;
13393c0086b8SZbigniew Bodek 	mbx.rq.qs_num = qs->vnic_id;
13403c0086b8SZbigniew Bodek 	mbx.rq.rq_num = qidx;
13415e95f5f5SEd Maste 	mbx.rq.cfg = ((uint64_t)rq->caching << 26) | (rq->cq_qs << 19) |
13423c0086b8SZbigniew Bodek 	    (rq->cq_idx << 16) | (rq->cont_rbdr_qs << 9) |
13432306b72aSZbigniew Bodek 	    (rq->cont_qs_rbdr_idx << 8) | (rq->start_rbdr_qs << 1) |
13442306b72aSZbigniew Bodek 	    (rq->start_qs_rbdr_idx);
13453c0086b8SZbigniew Bodek 	nicvf_send_msg_to_pf(nic, &mbx);
13463c0086b8SZbigniew Bodek 
13473c0086b8SZbigniew Bodek 	mbx.rq.msg = NIC_MBOX_MSG_RQ_BP_CFG;
13482306b72aSZbigniew Bodek 	mbx.rq.cfg = (1UL << 63) | (1UL << 62) | (qs->vnic_id << 0);
13493c0086b8SZbigniew Bodek 	nicvf_send_msg_to_pf(nic, &mbx);
13503c0086b8SZbigniew Bodek 
13512306b72aSZbigniew Bodek 	/*
13522306b72aSZbigniew Bodek 	 * RQ drop config
13533c0086b8SZbigniew Bodek 	 * Enable CQ drop to reserve sufficient CQEs for all tx packets
13543c0086b8SZbigniew Bodek 	 */
13553c0086b8SZbigniew Bodek 	mbx.rq.msg = NIC_MBOX_MSG_RQ_DROP_CFG;
13562306b72aSZbigniew Bodek 	mbx.rq.cfg = (1UL << 62) | (RQ_CQ_DROP << 8);
13573c0086b8SZbigniew Bodek 	nicvf_send_msg_to_pf(nic, &mbx);
13583c0086b8SZbigniew Bodek 
13593c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RQ_GEN_CFG, 0, 0x00);
13603c0086b8SZbigniew Bodek 
13613c0086b8SZbigniew Bodek 	/* Enable Receive queue */
13623c0086b8SZbigniew Bodek 	rq_cfg.ena = 1;
13633c0086b8SZbigniew Bodek 	rq_cfg.tcp_ena = 0;
13642306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RQ_0_7_CFG, qidx,
13652306b72aSZbigniew Bodek 	    *(uint64_t *)&rq_cfg);
13663c0086b8SZbigniew Bodek }
13673c0086b8SZbigniew Bodek 
13683c0086b8SZbigniew Bodek /* Configures completion queue */
13692306b72aSZbigniew Bodek static void
nicvf_cmp_queue_config(struct nicvf * nic,struct queue_set * qs,int qidx,boolean_t enable)13702306b72aSZbigniew Bodek nicvf_cmp_queue_config(struct nicvf *nic, struct queue_set *qs,
13712306b72aSZbigniew Bodek     int qidx, boolean_t enable)
13723c0086b8SZbigniew Bodek {
13733c0086b8SZbigniew Bodek 	struct cmp_queue *cq;
13743c0086b8SZbigniew Bodek 	struct cq_cfg cq_cfg;
13753c0086b8SZbigniew Bodek 
13763c0086b8SZbigniew Bodek 	cq = &qs->cq[qidx];
13773c0086b8SZbigniew Bodek 	cq->enable = enable;
13783c0086b8SZbigniew Bodek 
13793c0086b8SZbigniew Bodek 	if (!cq->enable) {
13803c0086b8SZbigniew Bodek 		nicvf_reclaim_cmp_queue(nic, qs, qidx);
13813c0086b8SZbigniew Bodek 		return;
13823c0086b8SZbigniew Bodek 	}
13833c0086b8SZbigniew Bodek 
13843c0086b8SZbigniew Bodek 	/* Reset completion queue */
13853c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, NICVF_CQ_RESET);
13863c0086b8SZbigniew Bodek 
13873c0086b8SZbigniew Bodek 	/* Set completion queue base address */
13882306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_BASE, qidx,
13892306b72aSZbigniew Bodek 	    (uint64_t)(cq->dmem.phys_base));
13903c0086b8SZbigniew Bodek 
13913c0086b8SZbigniew Bodek 	/* Enable Completion queue */
13923c0086b8SZbigniew Bodek 	cq_cfg.ena = 1;
13933c0086b8SZbigniew Bodek 	cq_cfg.reset = 0;
13943c0086b8SZbigniew Bodek 	cq_cfg.caching = 0;
13953c0086b8SZbigniew Bodek 	cq_cfg.qsize = CMP_QSIZE;
13963c0086b8SZbigniew Bodek 	cq_cfg.avg_con = 0;
13972306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG, qidx, *(uint64_t *)&cq_cfg);
13983c0086b8SZbigniew Bodek 
13993c0086b8SZbigniew Bodek 	/* Set threshold value for interrupt generation */
14003c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_THRESH, qidx, cq->thresh);
14012306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_CFG2, qidx,
14022306b72aSZbigniew Bodek 	    nic->cq_coalesce_usecs);
14033c0086b8SZbigniew Bodek }
14043c0086b8SZbigniew Bodek 
14053c0086b8SZbigniew Bodek /* Configures transmit queue */
14062306b72aSZbigniew Bodek static void
nicvf_snd_queue_config(struct nicvf * nic,struct queue_set * qs,int qidx,boolean_t enable)14072306b72aSZbigniew Bodek nicvf_snd_queue_config(struct nicvf *nic, struct queue_set *qs, int qidx,
14082306b72aSZbigniew Bodek     boolean_t enable)
14093c0086b8SZbigniew Bodek {
14103c0086b8SZbigniew Bodek 	union nic_mbx mbx = {};
14113c0086b8SZbigniew Bodek 	struct snd_queue *sq;
14123c0086b8SZbigniew Bodek 	struct sq_cfg sq_cfg;
14133c0086b8SZbigniew Bodek 
14143c0086b8SZbigniew Bodek 	sq = &qs->sq[qidx];
14153c0086b8SZbigniew Bodek 	sq->enable = enable;
14163c0086b8SZbigniew Bodek 
14173c0086b8SZbigniew Bodek 	if (!sq->enable) {
14183c0086b8SZbigniew Bodek 		nicvf_reclaim_snd_queue(nic, qs, qidx);
14193c0086b8SZbigniew Bodek 		return;
14203c0086b8SZbigniew Bodek 	}
14213c0086b8SZbigniew Bodek 
14223c0086b8SZbigniew Bodek 	/* Reset send queue */
14233c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, NICVF_SQ_RESET);
14243c0086b8SZbigniew Bodek 
14253c0086b8SZbigniew Bodek 	sq->cq_qs = qs->vnic_id;
14263c0086b8SZbigniew Bodek 	sq->cq_idx = qidx;
14273c0086b8SZbigniew Bodek 
14283c0086b8SZbigniew Bodek 	/* Send a mailbox msg to PF to config SQ */
14293c0086b8SZbigniew Bodek 	mbx.sq.msg = NIC_MBOX_MSG_SQ_CFG;
14303c0086b8SZbigniew Bodek 	mbx.sq.qs_num = qs->vnic_id;
14313c0086b8SZbigniew Bodek 	mbx.sq.sq_num = qidx;
14323c0086b8SZbigniew Bodek 	mbx.sq.sqs_mode = nic->sqs_mode;
14333c0086b8SZbigniew Bodek 	mbx.sq.cfg = (sq->cq_qs << 3) | sq->cq_idx;
14343c0086b8SZbigniew Bodek 	nicvf_send_msg_to_pf(nic, &mbx);
14353c0086b8SZbigniew Bodek 
14363c0086b8SZbigniew Bodek 	/* Set queue base address */
14372306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_BASE, qidx,
14382306b72aSZbigniew Bodek 	    (uint64_t)(sq->dmem.phys_base));
14393c0086b8SZbigniew Bodek 
14403c0086b8SZbigniew Bodek 	/* Enable send queue  & set queue size */
14413c0086b8SZbigniew Bodek 	sq_cfg.ena = 1;
14423c0086b8SZbigniew Bodek 	sq_cfg.reset = 0;
14433c0086b8SZbigniew Bodek 	sq_cfg.ldwb = 0;
14443c0086b8SZbigniew Bodek 	sq_cfg.qsize = SND_QSIZE;
14453c0086b8SZbigniew Bodek 	sq_cfg.tstmp_bgx_intf = 0;
14462306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, *(uint64_t *)&sq_cfg);
14473c0086b8SZbigniew Bodek 
14483c0086b8SZbigniew Bodek 	/* Set threshold value for interrupt generation */
14493c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_THRESH, qidx, sq->thresh);
14503c0086b8SZbigniew Bodek }
14513c0086b8SZbigniew Bodek 
14523c0086b8SZbigniew Bodek /* Configures receive buffer descriptor ring */
14532306b72aSZbigniew Bodek static void
nicvf_rbdr_config(struct nicvf * nic,struct queue_set * qs,int qidx,boolean_t enable)14542306b72aSZbigniew Bodek nicvf_rbdr_config(struct nicvf *nic, struct queue_set *qs, int qidx,
14552306b72aSZbigniew Bodek     boolean_t enable)
14563c0086b8SZbigniew Bodek {
14573c0086b8SZbigniew Bodek 	struct rbdr *rbdr;
14583c0086b8SZbigniew Bodek 	struct rbdr_cfg rbdr_cfg;
14593c0086b8SZbigniew Bodek 
14603c0086b8SZbigniew Bodek 	rbdr = &qs->rbdr[qidx];
14613c0086b8SZbigniew Bodek 	nicvf_reclaim_rbdr(nic, rbdr, qidx);
14623c0086b8SZbigniew Bodek 	if (!enable)
14633c0086b8SZbigniew Bodek 		return;
14643c0086b8SZbigniew Bodek 
14653c0086b8SZbigniew Bodek 	/* Set descriptor base address */
14662306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_BASE, qidx,
14672306b72aSZbigniew Bodek 	    (uint64_t)(rbdr->dmem.phys_base));
14683c0086b8SZbigniew Bodek 
14693c0086b8SZbigniew Bodek 	/* Enable RBDR  & set queue size */
14703c0086b8SZbigniew Bodek 	/* Buffer size should be in multiples of 128 bytes */
14713c0086b8SZbigniew Bodek 	rbdr_cfg.ena = 1;
14723c0086b8SZbigniew Bodek 	rbdr_cfg.reset = 0;
14733c0086b8SZbigniew Bodek 	rbdr_cfg.ldwb = 0;
14743c0086b8SZbigniew Bodek 	rbdr_cfg.qsize = RBDR_SIZE;
14753c0086b8SZbigniew Bodek 	rbdr_cfg.avg_con = 0;
14763c0086b8SZbigniew Bodek 	rbdr_cfg.lines = rbdr->dma_size / 128;
14772306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_CFG, qidx,
14782306b72aSZbigniew Bodek 	    *(uint64_t *)&rbdr_cfg);
14793c0086b8SZbigniew Bodek 
14803c0086b8SZbigniew Bodek 	/* Notify HW */
14812306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_DOOR, qidx,
14822306b72aSZbigniew Bodek 	    qs->rbdr_len - 1);
14833c0086b8SZbigniew Bodek 
14843c0086b8SZbigniew Bodek 	/* Set threshold value for interrupt generation */
14852306b72aSZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_RBDR_0_1_THRESH, qidx,
14862306b72aSZbigniew Bodek 	    rbdr->thresh - 1);
14873c0086b8SZbigniew Bodek }
14883c0086b8SZbigniew Bodek 
14893c0086b8SZbigniew Bodek /* Requests PF to assign and enable Qset */
14902306b72aSZbigniew Bodek void
nicvf_qset_config(struct nicvf * nic,boolean_t enable)14912306b72aSZbigniew Bodek nicvf_qset_config(struct nicvf *nic, boolean_t enable)
14923c0086b8SZbigniew Bodek {
14933c0086b8SZbigniew Bodek 	union nic_mbx mbx = {};
14942306b72aSZbigniew Bodek 	struct queue_set *qs;
14953c0086b8SZbigniew Bodek 	struct qs_cfg *qs_cfg;
14963c0086b8SZbigniew Bodek 
14972306b72aSZbigniew Bodek 	qs = nic->qs;
14982306b72aSZbigniew Bodek 	if (qs == NULL) {
14992306b72aSZbigniew Bodek 		device_printf(nic->dev,
15003c0086b8SZbigniew Bodek 		    "Qset is still not allocated, don't init queues\n");
15013c0086b8SZbigniew Bodek 		return;
15023c0086b8SZbigniew Bodek 	}
15033c0086b8SZbigniew Bodek 
15043c0086b8SZbigniew Bodek 	qs->enable = enable;
15053c0086b8SZbigniew Bodek 	qs->vnic_id = nic->vf_id;
15063c0086b8SZbigniew Bodek 
15073c0086b8SZbigniew Bodek 	/* Send a mailbox msg to PF to config Qset */
15083c0086b8SZbigniew Bodek 	mbx.qs.msg = NIC_MBOX_MSG_QS_CFG;
15093c0086b8SZbigniew Bodek 	mbx.qs.num = qs->vnic_id;
15103c0086b8SZbigniew Bodek 
15113c0086b8SZbigniew Bodek 	mbx.qs.cfg = 0;
15123c0086b8SZbigniew Bodek 	qs_cfg = (struct qs_cfg *)&mbx.qs.cfg;
15133c0086b8SZbigniew Bodek 	if (qs->enable) {
15143c0086b8SZbigniew Bodek 		qs_cfg->ena = 1;
15153c0086b8SZbigniew Bodek 		qs_cfg->vnic = qs->vnic_id;
15163c0086b8SZbigniew Bodek 	}
15173c0086b8SZbigniew Bodek 	nicvf_send_msg_to_pf(nic, &mbx);
15183c0086b8SZbigniew Bodek }
15193c0086b8SZbigniew Bodek 
15202306b72aSZbigniew Bodek static void
nicvf_free_resources(struct nicvf * nic)15212306b72aSZbigniew Bodek nicvf_free_resources(struct nicvf *nic)
15223c0086b8SZbigniew Bodek {
15233c0086b8SZbigniew Bodek 	int qidx;
15242306b72aSZbigniew Bodek 	struct queue_set *qs;
15253c0086b8SZbigniew Bodek 
15262306b72aSZbigniew Bodek 	qs = nic->qs;
15272306b72aSZbigniew Bodek 	/*
15282306b72aSZbigniew Bodek 	 * Remove QS error task first since it has to be dead
15292306b72aSZbigniew Bodek 	 * to safely free completion queue tasks.
15302306b72aSZbigniew Bodek 	 */
15312306b72aSZbigniew Bodek 	if (qs->qs_err_taskq != NULL) {
15322306b72aSZbigniew Bodek 		/* Shut down QS error tasks */
15332306b72aSZbigniew Bodek 		while (taskqueue_cancel(qs->qs_err_taskq,
15342306b72aSZbigniew Bodek 		    &qs->qs_err_task,  NULL) != 0) {
15352306b72aSZbigniew Bodek 			taskqueue_drain(qs->qs_err_taskq, &qs->qs_err_task);
15362306b72aSZbigniew Bodek 		}
15372306b72aSZbigniew Bodek 		taskqueue_free(qs->qs_err_taskq);
15382306b72aSZbigniew Bodek 		qs->qs_err_taskq = NULL;
15392306b72aSZbigniew Bodek 	}
15403c0086b8SZbigniew Bodek 	/* Free receive buffer descriptor ring */
15413c0086b8SZbigniew Bodek 	for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
15423c0086b8SZbigniew Bodek 		nicvf_free_rbdr(nic, &qs->rbdr[qidx]);
15433c0086b8SZbigniew Bodek 
15443c0086b8SZbigniew Bodek 	/* Free completion queue */
15453c0086b8SZbigniew Bodek 	for (qidx = 0; qidx < qs->cq_cnt; qidx++)
15463c0086b8SZbigniew Bodek 		nicvf_free_cmp_queue(nic, &qs->cq[qidx]);
15473c0086b8SZbigniew Bodek 
15483c0086b8SZbigniew Bodek 	/* Free send queue */
15493c0086b8SZbigniew Bodek 	for (qidx = 0; qidx < qs->sq_cnt; qidx++)
15503c0086b8SZbigniew Bodek 		nicvf_free_snd_queue(nic, &qs->sq[qidx]);
15513c0086b8SZbigniew Bodek }
15523c0086b8SZbigniew Bodek 
15532306b72aSZbigniew Bodek static int
nicvf_alloc_resources(struct nicvf * nic)15542306b72aSZbigniew Bodek nicvf_alloc_resources(struct nicvf *nic)
15553c0086b8SZbigniew Bodek {
15563c0086b8SZbigniew Bodek 	struct queue_set *qs = nic->qs;
15572306b72aSZbigniew Bodek 	int qidx;
15583c0086b8SZbigniew Bodek 
15593c0086b8SZbigniew Bodek 	/* Alloc receive buffer descriptor ring */
15603c0086b8SZbigniew Bodek 	for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
15613c0086b8SZbigniew Bodek 		if (nicvf_init_rbdr(nic, &qs->rbdr[qidx], qs->rbdr_len,
15622306b72aSZbigniew Bodek 				    DMA_BUFFER_LEN, qidx))
15633c0086b8SZbigniew Bodek 			goto alloc_fail;
15643c0086b8SZbigniew Bodek 	}
15653c0086b8SZbigniew Bodek 
15663c0086b8SZbigniew Bodek 	/* Alloc send queue */
15673c0086b8SZbigniew Bodek 	for (qidx = 0; qidx < qs->sq_cnt; qidx++) {
15682306b72aSZbigniew Bodek 		if (nicvf_init_snd_queue(nic, &qs->sq[qidx], qs->sq_len, qidx))
15693c0086b8SZbigniew Bodek 			goto alloc_fail;
15703c0086b8SZbigniew Bodek 	}
15713c0086b8SZbigniew Bodek 
15723c0086b8SZbigniew Bodek 	/* Alloc completion queue */
15733c0086b8SZbigniew Bodek 	for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
15742306b72aSZbigniew Bodek 		if (nicvf_init_cmp_queue(nic, &qs->cq[qidx], qs->cq_len, qidx))
15753c0086b8SZbigniew Bodek 			goto alloc_fail;
15763c0086b8SZbigniew Bodek 	}
15773c0086b8SZbigniew Bodek 
15782306b72aSZbigniew Bodek 	/* Allocate QS error taskqueue */
15796c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&qs->qs_err_task, 0, nicvf_qs_err_task, nic);
15802306b72aSZbigniew Bodek 	qs->qs_err_taskq = taskqueue_create_fast("nicvf_qs_err_taskq", M_WAITOK,
15812306b72aSZbigniew Bodek 	    taskqueue_thread_enqueue, &qs->qs_err_taskq);
15822306b72aSZbigniew Bodek 	taskqueue_start_threads(&qs->qs_err_taskq, 1, PI_NET, "%s: qs_taskq",
15832306b72aSZbigniew Bodek 	    device_get_nameunit(nic->dev));
15842306b72aSZbigniew Bodek 
15852306b72aSZbigniew Bodek 	return (0);
15863c0086b8SZbigniew Bodek alloc_fail:
15873c0086b8SZbigniew Bodek 	nicvf_free_resources(nic);
15882306b72aSZbigniew Bodek 	return (ENOMEM);
15893c0086b8SZbigniew Bodek }
15903c0086b8SZbigniew Bodek 
15912306b72aSZbigniew Bodek int
nicvf_set_qset_resources(struct nicvf * nic)15922306b72aSZbigniew Bodek nicvf_set_qset_resources(struct nicvf *nic)
15933c0086b8SZbigniew Bodek {
15943c0086b8SZbigniew Bodek 	struct queue_set *qs;
15953c0086b8SZbigniew Bodek 
15962306b72aSZbigniew Bodek 	qs = malloc(sizeof(*qs), M_NICVF, (M_ZERO | M_WAITOK));
15973c0086b8SZbigniew Bodek 	nic->qs = qs;
15983c0086b8SZbigniew Bodek 
15993c0086b8SZbigniew Bodek 	/* Set count of each queue */
16003c0086b8SZbigniew Bodek 	qs->rbdr_cnt = RBDR_CNT;
16018191a879SZbigniew Bodek 	qs->rq_cnt = RCV_QUEUE_CNT;
16022306b72aSZbigniew Bodek 
16033c0086b8SZbigniew Bodek 	qs->sq_cnt = SND_QUEUE_CNT;
16043c0086b8SZbigniew Bodek 	qs->cq_cnt = CMP_QUEUE_CNT;
16053c0086b8SZbigniew Bodek 
16063c0086b8SZbigniew Bodek 	/* Set queue lengths */
16073c0086b8SZbigniew Bodek 	qs->rbdr_len = RCV_BUF_COUNT;
16083c0086b8SZbigniew Bodek 	qs->sq_len = SND_QUEUE_LEN;
16093c0086b8SZbigniew Bodek 	qs->cq_len = CMP_QUEUE_LEN;
16103c0086b8SZbigniew Bodek 
16113c0086b8SZbigniew Bodek 	nic->rx_queues = qs->rq_cnt;
16123c0086b8SZbigniew Bodek 	nic->tx_queues = qs->sq_cnt;
16133c0086b8SZbigniew Bodek 
16142306b72aSZbigniew Bodek 	return (0);
16153c0086b8SZbigniew Bodek }
16163c0086b8SZbigniew Bodek 
16172306b72aSZbigniew Bodek int
nicvf_config_data_transfer(struct nicvf * nic,boolean_t enable)16182306b72aSZbigniew Bodek nicvf_config_data_transfer(struct nicvf *nic, boolean_t enable)
16193c0086b8SZbigniew Bodek {
16202306b72aSZbigniew Bodek 	boolean_t disable = FALSE;
16212306b72aSZbigniew Bodek 	struct queue_set *qs;
16223c0086b8SZbigniew Bodek 	int qidx;
16233c0086b8SZbigniew Bodek 
16242306b72aSZbigniew Bodek 	qs = nic->qs;
16252306b72aSZbigniew Bodek 	if (qs == NULL)
16262306b72aSZbigniew Bodek 		return (0);
16273c0086b8SZbigniew Bodek 
16283c0086b8SZbigniew Bodek 	if (enable) {
16292306b72aSZbigniew Bodek 		if (nicvf_alloc_resources(nic) != 0)
16302306b72aSZbigniew Bodek 			return (ENOMEM);
16313c0086b8SZbigniew Bodek 
16323c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->sq_cnt; qidx++)
16333c0086b8SZbigniew Bodek 			nicvf_snd_queue_config(nic, qs, qidx, enable);
16343c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->cq_cnt; qidx++)
16353c0086b8SZbigniew Bodek 			nicvf_cmp_queue_config(nic, qs, qidx, enable);
16363c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
16373c0086b8SZbigniew Bodek 			nicvf_rbdr_config(nic, qs, qidx, enable);
16383c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->rq_cnt; qidx++)
16393c0086b8SZbigniew Bodek 			nicvf_rcv_queue_config(nic, qs, qidx, enable);
16403c0086b8SZbigniew Bodek 	} else {
16413c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->rq_cnt; qidx++)
16423c0086b8SZbigniew Bodek 			nicvf_rcv_queue_config(nic, qs, qidx, disable);
16433c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
16443c0086b8SZbigniew Bodek 			nicvf_rbdr_config(nic, qs, qidx, disable);
16453c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->sq_cnt; qidx++)
16463c0086b8SZbigniew Bodek 			nicvf_snd_queue_config(nic, qs, qidx, disable);
16473c0086b8SZbigniew Bodek 		for (qidx = 0; qidx < qs->cq_cnt; qidx++)
16483c0086b8SZbigniew Bodek 			nicvf_cmp_queue_config(nic, qs, qidx, disable);
16493c0086b8SZbigniew Bodek 
16503c0086b8SZbigniew Bodek 		nicvf_free_resources(nic);
16513c0086b8SZbigniew Bodek 	}
16523c0086b8SZbigniew Bodek 
16532306b72aSZbigniew Bodek 	return (0);
16543c0086b8SZbigniew Bodek }
16553c0086b8SZbigniew Bodek 
16562306b72aSZbigniew Bodek /*
16572306b72aSZbigniew Bodek  * Get a free desc from SQ
16583c0086b8SZbigniew Bodek  * returns descriptor ponter & descriptor number
16593c0086b8SZbigniew Bodek  */
16602306b72aSZbigniew Bodek static __inline int
nicvf_get_sq_desc(struct snd_queue * sq,int desc_cnt)16612306b72aSZbigniew Bodek nicvf_get_sq_desc(struct snd_queue *sq, int desc_cnt)
16623c0086b8SZbigniew Bodek {
16633c0086b8SZbigniew Bodek 	int qentry;
16643c0086b8SZbigniew Bodek 
16653c0086b8SZbigniew Bodek 	qentry = sq->tail;
1666f6cdb4ceSZbigniew Bodek 	atomic_subtract_int(&sq->free_cnt, desc_cnt);
16673c0086b8SZbigniew Bodek 	sq->tail += desc_cnt;
16683c0086b8SZbigniew Bodek 	sq->tail &= (sq->dmem.q_len - 1);
16693c0086b8SZbigniew Bodek 
16702306b72aSZbigniew Bodek 	return (qentry);
16713c0086b8SZbigniew Bodek }
16723c0086b8SZbigniew Bodek 
16733c0086b8SZbigniew Bodek /* Free descriptor back to SQ for future use */
16742306b72aSZbigniew Bodek static void
nicvf_put_sq_desc(struct snd_queue * sq,int desc_cnt)16752306b72aSZbigniew Bodek nicvf_put_sq_desc(struct snd_queue *sq, int desc_cnt)
16763c0086b8SZbigniew Bodek {
16772306b72aSZbigniew Bodek 
1678f6cdb4ceSZbigniew Bodek 	atomic_add_int(&sq->free_cnt, desc_cnt);
16793c0086b8SZbigniew Bodek 	sq->head += desc_cnt;
16803c0086b8SZbigniew Bodek 	sq->head &= (sq->dmem.q_len - 1);
16813c0086b8SZbigniew Bodek }
16823c0086b8SZbigniew Bodek 
16832306b72aSZbigniew Bodek static __inline int
nicvf_get_nxt_sqentry(struct snd_queue * sq,int qentry)16842306b72aSZbigniew Bodek nicvf_get_nxt_sqentry(struct snd_queue *sq, int qentry)
16853c0086b8SZbigniew Bodek {
16863c0086b8SZbigniew Bodek 	qentry++;
16873c0086b8SZbigniew Bodek 	qentry &= (sq->dmem.q_len - 1);
16882306b72aSZbigniew Bodek 	return (qentry);
16893c0086b8SZbigniew Bodek }
16903c0086b8SZbigniew Bodek 
16912306b72aSZbigniew Bodek static void
nicvf_sq_enable(struct nicvf * nic,struct snd_queue * sq,int qidx)16922306b72aSZbigniew Bodek nicvf_sq_enable(struct nicvf *nic, struct snd_queue *sq, int qidx)
16933c0086b8SZbigniew Bodek {
16942306b72aSZbigniew Bodek 	uint64_t sq_cfg;
16953c0086b8SZbigniew Bodek 
16963c0086b8SZbigniew Bodek 	sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
16973c0086b8SZbigniew Bodek 	sq_cfg |= NICVF_SQ_EN;
16983c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
16993c0086b8SZbigniew Bodek 	/* Ring doorbell so that H/W restarts processing SQEs */
17003c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_DOOR, qidx, 0);
17013c0086b8SZbigniew Bodek }
17023c0086b8SZbigniew Bodek 
17032306b72aSZbigniew Bodek static void
nicvf_sq_disable(struct nicvf * nic,int qidx)17042306b72aSZbigniew Bodek nicvf_sq_disable(struct nicvf *nic, int qidx)
17053c0086b8SZbigniew Bodek {
17062306b72aSZbigniew Bodek 	uint64_t sq_cfg;
17073c0086b8SZbigniew Bodek 
17083c0086b8SZbigniew Bodek 	sq_cfg = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, qidx);
17093c0086b8SZbigniew Bodek 	sq_cfg &= ~NICVF_SQ_EN;
17103c0086b8SZbigniew Bodek 	nicvf_queue_reg_write(nic, NIC_QSET_SQ_0_7_CFG, qidx, sq_cfg);
17113c0086b8SZbigniew Bodek }
17123c0086b8SZbigniew Bodek 
17132306b72aSZbigniew Bodek static void
nicvf_sq_free_used_descs(struct nicvf * nic,struct snd_queue * sq,int qidx)17142306b72aSZbigniew Bodek nicvf_sq_free_used_descs(struct nicvf *nic, struct snd_queue *sq, int qidx)
17153c0086b8SZbigniew Bodek {
1716151ba793SAlexander Kabaev 	uint64_t head;
17172306b72aSZbigniew Bodek 	struct snd_buff *snd_buff;
17183c0086b8SZbigniew Bodek 	struct sq_hdr_subdesc *hdr;
17193c0086b8SZbigniew Bodek 
17202306b72aSZbigniew Bodek 	NICVF_TX_LOCK(sq);
17213c0086b8SZbigniew Bodek 	head = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, qidx) >> 4;
17223c0086b8SZbigniew Bodek 	while (sq->head != head) {
17233c0086b8SZbigniew Bodek 		hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, sq->head);
17243c0086b8SZbigniew Bodek 		if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER) {
17253c0086b8SZbigniew Bodek 			nicvf_put_sq_desc(sq, 1);
17263c0086b8SZbigniew Bodek 			continue;
17273c0086b8SZbigniew Bodek 		}
17282306b72aSZbigniew Bodek 		snd_buff = &sq->snd_buff[sq->head];
17292306b72aSZbigniew Bodek 		if (snd_buff->mbuf != NULL) {
17302306b72aSZbigniew Bodek 			bus_dmamap_unload(sq->snd_buff_dmat, snd_buff->dmap);
17312306b72aSZbigniew Bodek 			m_freem(snd_buff->mbuf);
17322306b72aSZbigniew Bodek 			sq->snd_buff[sq->head].mbuf = NULL;
17332306b72aSZbigniew Bodek 		}
17343c0086b8SZbigniew Bodek 		nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
17353c0086b8SZbigniew Bodek 	}
17362306b72aSZbigniew Bodek 	NICVF_TX_UNLOCK(sq);
17373c0086b8SZbigniew Bodek }
17383c0086b8SZbigniew Bodek 
17392306b72aSZbigniew Bodek /*
17402306b72aSZbigniew Bodek  * Add SQ HEADER subdescriptor.
17413c0086b8SZbigniew Bodek  * First subdescriptor for every send descriptor.
17423c0086b8SZbigniew Bodek  */
1743856dce91SZbigniew Bodek static __inline int
nicvf_sq_add_hdr_subdesc(struct snd_queue * sq,int qentry,int subdesc_cnt,struct mbuf * mbuf,int len)17443c0086b8SZbigniew Bodek nicvf_sq_add_hdr_subdesc(struct snd_queue *sq, int qentry,
17452306b72aSZbigniew Bodek 			 int subdesc_cnt, struct mbuf *mbuf, int len)
17463c0086b8SZbigniew Bodek {
1747af8fe8f1SZbigniew Bodek 	struct nicvf *nic;
17483c0086b8SZbigniew Bodek 	struct sq_hdr_subdesc *hdr;
1749856dce91SZbigniew Bodek 	struct ether_vlan_header *eh;
1750856dce91SZbigniew Bodek #ifdef INET
1751856dce91SZbigniew Bodek 	struct ip *ip;
1752*1d3d30c8SMichael Tuexen #endif
1753*1d3d30c8SMichael Tuexen #if defined(INET6) || defined(INET)
1754af8fe8f1SZbigniew Bodek 	struct tcphdr *th;
1755856dce91SZbigniew Bodek #endif
1756*1d3d30c8SMichael Tuexen #ifdef INET
1757*1d3d30c8SMichael Tuexen 	int iphlen;
1758*1d3d30c8SMichael Tuexen #endif
1759*1d3d30c8SMichael Tuexen 	int ehdrlen, poff, proto;
1760856dce91SZbigniew Bodek 	uint16_t etype;
17613c0086b8SZbigniew Bodek 
1762af8fe8f1SZbigniew Bodek 	nic = sq->nic;
1763af8fe8f1SZbigniew Bodek 
17643c0086b8SZbigniew Bodek 	hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, qentry);
17652306b72aSZbigniew Bodek 	sq->snd_buff[qentry].mbuf = mbuf;
17663c0086b8SZbigniew Bodek 
17673c0086b8SZbigniew Bodek 	memset(hdr, 0, SND_QUEUE_DESC_SIZE);
17683c0086b8SZbigniew Bodek 	hdr->subdesc_type = SQ_DESC_TYPE_HEADER;
17693c0086b8SZbigniew Bodek 	/* Enable notification via CQE after processing SQE */
17703c0086b8SZbigniew Bodek 	hdr->post_cqe = 1;
17713c0086b8SZbigniew Bodek 	/* No of subdescriptors following this */
17723c0086b8SZbigniew Bodek 	hdr->subdesc_cnt = subdesc_cnt;
17733c0086b8SZbigniew Bodek 	hdr->tot_len = len;
17743c0086b8SZbigniew Bodek 
1775856dce91SZbigniew Bodek 	eh = mtod(mbuf, struct ether_vlan_header *);
1776856dce91SZbigniew Bodek 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
1777856dce91SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1778856dce91SZbigniew Bodek 		etype = ntohs(eh->evl_proto);
1779856dce91SZbigniew Bodek 	} else {
1780856dce91SZbigniew Bodek 		ehdrlen = ETHER_HDR_LEN;
1781856dce91SZbigniew Bodek 		etype = ntohs(eh->evl_encap_proto);
1782856dce91SZbigniew Bodek 	}
1783856dce91SZbigniew Bodek 
1784c04716b1SBjoern A. Zeeb 	poff = proto = -1;
1785856dce91SZbigniew Bodek 	switch (etype) {
1786856dce91SZbigniew Bodek #ifdef INET6
1787856dce91SZbigniew Bodek 	case ETHERTYPE_IPV6:
1788c04716b1SBjoern A. Zeeb 		if (mbuf->m_len < ehdrlen + sizeof(struct ip6_hdr)) {
1789c04716b1SBjoern A. Zeeb 			mbuf = m_pullup(mbuf, ehdrlen +sizeof(struct ip6_hdr));
1790856dce91SZbigniew Bodek 			sq->snd_buff[qentry].mbuf = NULL;
1791c04716b1SBjoern A. Zeeb 			if (mbuf == NULL)
1792c04716b1SBjoern A. Zeeb 				return (ENOBUFS);
1793c04716b1SBjoern A. Zeeb 		}
1794c04716b1SBjoern A. Zeeb 		poff = ip6_lasthdr(mbuf, ehdrlen, IPPROTO_IPV6, &proto);
1795c04716b1SBjoern A. Zeeb 		if (poff < 0)
1796c04716b1SBjoern A. Zeeb 			return (ENOBUFS);
1797c04716b1SBjoern A. Zeeb 		poff += ehdrlen;
1798c04716b1SBjoern A. Zeeb 		break;
1799856dce91SZbigniew Bodek #endif
1800856dce91SZbigniew Bodek #ifdef INET
1801856dce91SZbigniew Bodek 	case ETHERTYPE_IP:
1802af8fe8f1SZbigniew Bodek 		if (mbuf->m_len < ehdrlen + sizeof(struct ip)) {
1803af8fe8f1SZbigniew Bodek 			mbuf = m_pullup(mbuf, ehdrlen + sizeof(struct ip));
1804af8fe8f1SZbigniew Bodek 			sq->snd_buff[qentry].mbuf = mbuf;
1805af8fe8f1SZbigniew Bodek 			if (mbuf == NULL)
1806af8fe8f1SZbigniew Bodek 				return (ENOBUFS);
1807af8fe8f1SZbigniew Bodek 		}
18087fe94db2SEd Maste 		if (mbuf->m_pkthdr.csum_flags & CSUM_IP)
18097fe94db2SEd Maste 			hdr->csum_l3 = 1; /* Enable IP csum calculation */
1810af8fe8f1SZbigniew Bodek 
1811856dce91SZbigniew Bodek 		ip = (struct ip *)(mbuf->m_data + ehdrlen);
1812856dce91SZbigniew Bodek 		iphlen = ip->ip_hl << 2;
1813856dce91SZbigniew Bodek 		poff = ehdrlen + iphlen;
1814c04716b1SBjoern A. Zeeb 		proto = ip->ip_p;
1815c04716b1SBjoern A. Zeeb 		break;
1816c04716b1SBjoern A. Zeeb #endif
1817c04716b1SBjoern A. Zeeb 	}
1818856dce91SZbigniew Bodek 
1819c04716b1SBjoern A. Zeeb #if defined(INET6) || defined(INET)
1820c04716b1SBjoern A. Zeeb 	if (poff > 0 && mbuf->m_pkthdr.csum_flags != 0) {
1821c04716b1SBjoern A. Zeeb 		switch (proto) {
1822856dce91SZbigniew Bodek 		case IPPROTO_TCP:
1823856dce91SZbigniew Bodek 			if ((mbuf->m_pkthdr.csum_flags & CSUM_TCP) == 0)
1824856dce91SZbigniew Bodek 				break;
1825856dce91SZbigniew Bodek 
1826856dce91SZbigniew Bodek 			if (mbuf->m_len < (poff + sizeof(struct tcphdr))) {
1827856dce91SZbigniew Bodek 				mbuf = m_pullup(mbuf, poff + sizeof(struct tcphdr));
1828856dce91SZbigniew Bodek 				sq->snd_buff[qentry].mbuf = mbuf;
1829856dce91SZbigniew Bodek 				if (mbuf == NULL)
1830856dce91SZbigniew Bodek 					return (ENOBUFS);
1831856dce91SZbigniew Bodek 			}
1832856dce91SZbigniew Bodek 			hdr->csum_l4 = SEND_L4_CSUM_TCP;
1833856dce91SZbigniew Bodek 			break;
1834856dce91SZbigniew Bodek 		case IPPROTO_UDP:
1835856dce91SZbigniew Bodek 			if ((mbuf->m_pkthdr.csum_flags & CSUM_UDP) == 0)
1836856dce91SZbigniew Bodek 				break;
1837856dce91SZbigniew Bodek 
1838856dce91SZbigniew Bodek 			if (mbuf->m_len < (poff + sizeof(struct udphdr))) {
1839856dce91SZbigniew Bodek 				mbuf = m_pullup(mbuf, poff + sizeof(struct udphdr));
1840856dce91SZbigniew Bodek 				sq->snd_buff[qentry].mbuf = mbuf;
1841856dce91SZbigniew Bodek 				if (mbuf == NULL)
1842856dce91SZbigniew Bodek 					return (ENOBUFS);
1843856dce91SZbigniew Bodek 			}
1844856dce91SZbigniew Bodek 			hdr->csum_l4 = SEND_L4_CSUM_UDP;
1845856dce91SZbigniew Bodek 			break;
1846856dce91SZbigniew Bodek 		case IPPROTO_SCTP:
1847856dce91SZbigniew Bodek 			if ((mbuf->m_pkthdr.csum_flags & CSUM_SCTP) == 0)
1848856dce91SZbigniew Bodek 				break;
1849856dce91SZbigniew Bodek 
1850856dce91SZbigniew Bodek 			if (mbuf->m_len < (poff + sizeof(struct sctphdr))) {
1851856dce91SZbigniew Bodek 				mbuf = m_pullup(mbuf, poff + sizeof(struct sctphdr));
1852856dce91SZbigniew Bodek 				sq->snd_buff[qentry].mbuf = mbuf;
1853856dce91SZbigniew Bodek 				if (mbuf == NULL)
1854856dce91SZbigniew Bodek 					return (ENOBUFS);
1855856dce91SZbigniew Bodek 			}
1856856dce91SZbigniew Bodek 			hdr->csum_l4 = SEND_L4_CSUM_SCTP;
1857856dce91SZbigniew Bodek 			break;
1858856dce91SZbigniew Bodek 		default:
1859856dce91SZbigniew Bodek 			break;
1860856dce91SZbigniew Bodek 		}
1861af8fe8f1SZbigniew Bodek 		hdr->l3_offset = ehdrlen;
1862c04716b1SBjoern A. Zeeb 		hdr->l4_offset = poff;
1863af8fe8f1SZbigniew Bodek 	}
1864af8fe8f1SZbigniew Bodek 
1865af8fe8f1SZbigniew Bodek 	if ((mbuf->m_pkthdr.tso_segsz != 0) && nic->hw_tso) {
1866c04716b1SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)(mbuf->m_data + poff));
1867af8fe8f1SZbigniew Bodek 
1868af8fe8f1SZbigniew Bodek 		hdr->tso = 1;
1869c04716b1SBjoern A. Zeeb 		hdr->tso_start = poff + (th->th_off * 4);
1870af8fe8f1SZbigniew Bodek 		hdr->tso_max_paysize = mbuf->m_pkthdr.tso_segsz;
1871af8fe8f1SZbigniew Bodek 		hdr->inner_l3_offset = ehdrlen - 2;
1872af8fe8f1SZbigniew Bodek 		nic->drv_stats.tx_tso++;
1873af8fe8f1SZbigniew Bodek 	}
1874856dce91SZbigniew Bodek #endif
1875856dce91SZbigniew Bodek 
1876856dce91SZbigniew Bodek 	return (0);
18773c0086b8SZbigniew Bodek }
18783c0086b8SZbigniew Bodek 
18792306b72aSZbigniew Bodek /*
18802306b72aSZbigniew Bodek  * SQ GATHER subdescriptor
18813c0086b8SZbigniew Bodek  * Must follow HDR descriptor
18823c0086b8SZbigniew Bodek  */
nicvf_sq_add_gather_subdesc(struct snd_queue * sq,int qentry,int size,uint64_t data)18833c0086b8SZbigniew Bodek static inline void nicvf_sq_add_gather_subdesc(struct snd_queue *sq, int qentry,
18842306b72aSZbigniew Bodek 					       int size, uint64_t data)
18853c0086b8SZbigniew Bodek {
18863c0086b8SZbigniew Bodek 	struct sq_gather_subdesc *gather;
18873c0086b8SZbigniew Bodek 
18883c0086b8SZbigniew Bodek 	qentry &= (sq->dmem.q_len - 1);
18893c0086b8SZbigniew Bodek 	gather = (struct sq_gather_subdesc *)GET_SQ_DESC(sq, qentry);
18903c0086b8SZbigniew Bodek 
18913c0086b8SZbigniew Bodek 	memset(gather, 0, SND_QUEUE_DESC_SIZE);
18923c0086b8SZbigniew Bodek 	gather->subdesc_type = SQ_DESC_TYPE_GATHER;
18933c0086b8SZbigniew Bodek 	gather->ld_type = NIC_SEND_LD_TYPE_E_LDD;
18943c0086b8SZbigniew Bodek 	gather->size = size;
18953c0086b8SZbigniew Bodek 	gather->addr = data;
18963c0086b8SZbigniew Bodek }
18973c0086b8SZbigniew Bodek 
18982306b72aSZbigniew Bodek /* Put an mbuf to a SQ for packet transfer. */
18997c617aceSZbigniew Bodek static int
nicvf_tx_mbuf_locked(struct snd_queue * sq,struct mbuf ** mbufp)19007c617aceSZbigniew Bodek nicvf_tx_mbuf_locked(struct snd_queue *sq, struct mbuf **mbufp)
19013c0086b8SZbigniew Bodek {
19022306b72aSZbigniew Bodek 	bus_dma_segment_t segs[256];
19032306b72aSZbigniew Bodek 	struct snd_buff *snd_buff;
19042306b72aSZbigniew Bodek 	size_t seg;
19052306b72aSZbigniew Bodek 	int nsegs, qentry;
1906af8fe8f1SZbigniew Bodek 	int subdesc_cnt;
19072306b72aSZbigniew Bodek 	int err;
19083c0086b8SZbigniew Bodek 
19092306b72aSZbigniew Bodek 	NICVF_TX_LOCK_ASSERT(sq);
19102306b72aSZbigniew Bodek 
19112306b72aSZbigniew Bodek 	if (sq->free_cnt == 0)
19122306b72aSZbigniew Bodek 		return (ENOBUFS);
19132306b72aSZbigniew Bodek 
19142306b72aSZbigniew Bodek 	snd_buff = &sq->snd_buff[sq->tail];
19152306b72aSZbigniew Bodek 
19162306b72aSZbigniew Bodek 	err = bus_dmamap_load_mbuf_sg(sq->snd_buff_dmat, snd_buff->dmap,
19177c617aceSZbigniew Bodek 	    *mbufp, segs, &nsegs, BUS_DMA_NOWAIT);
19187c617aceSZbigniew Bodek 	if (__predict_false(err != 0)) {
19192306b72aSZbigniew Bodek 		/* ARM64TODO: Add mbuf defragmenting if we lack maps */
19207c617aceSZbigniew Bodek 		m_freem(*mbufp);
19217c617aceSZbigniew Bodek 		*mbufp = NULL;
19222306b72aSZbigniew Bodek 		return (err);
19233c0086b8SZbigniew Bodek 	}
19242306b72aSZbigniew Bodek 
19252306b72aSZbigniew Bodek 	/* Set how many subdescriptors is required */
1926af8fe8f1SZbigniew Bodek 	subdesc_cnt = MIN_SQ_DESC_PER_PKT_XMIT + nsegs - 1;
19272306b72aSZbigniew Bodek 	if (subdesc_cnt > sq->free_cnt) {
19282306b72aSZbigniew Bodek 		/* ARM64TODO: Add mbuf defragmentation if we lack descriptors */
19292306b72aSZbigniew Bodek 		bus_dmamap_unload(sq->snd_buff_dmat, snd_buff->dmap);
19302306b72aSZbigniew Bodek 		return (ENOBUFS);
19313c0086b8SZbigniew Bodek 	}
19323c0086b8SZbigniew Bodek 
19333c0086b8SZbigniew Bodek 	qentry = nicvf_get_sq_desc(sq, subdesc_cnt);
19343c0086b8SZbigniew Bodek 
19353c0086b8SZbigniew Bodek 	/* Add SQ header subdesc */
19367c617aceSZbigniew Bodek 	err = nicvf_sq_add_hdr_subdesc(sq, qentry, subdesc_cnt - 1, *mbufp,
19377c617aceSZbigniew Bodek 	    (*mbufp)->m_pkthdr.len);
1938856dce91SZbigniew Bodek 	if (err != 0) {
19397c617aceSZbigniew Bodek 		nicvf_put_sq_desc(sq, subdesc_cnt);
1940856dce91SZbigniew Bodek 		bus_dmamap_unload(sq->snd_buff_dmat, snd_buff->dmap);
19417c617aceSZbigniew Bodek 		if (err == ENOBUFS) {
19427c617aceSZbigniew Bodek 			m_freem(*mbufp);
19437c617aceSZbigniew Bodek 			*mbufp = NULL;
19447c617aceSZbigniew Bodek 		}
1945856dce91SZbigniew Bodek 		return (err);
1946856dce91SZbigniew Bodek 	}
19473c0086b8SZbigniew Bodek 
19483c0086b8SZbigniew Bodek 	/* Add SQ gather subdescs */
19492306b72aSZbigniew Bodek 	for (seg = 0; seg < nsegs; seg++) {
19503c0086b8SZbigniew Bodek 		qentry = nicvf_get_nxt_sqentry(sq, qentry);
19512306b72aSZbigniew Bodek 		nicvf_sq_add_gather_subdesc(sq, qentry, segs[seg].ds_len,
19522306b72aSZbigniew Bodek 		    segs[seg].ds_addr);
19533c0086b8SZbigniew Bodek 	}
19543c0086b8SZbigniew Bodek 
19553c0086b8SZbigniew Bodek 	/* make sure all memory stores are done before ringing doorbell */
19562306b72aSZbigniew Bodek 	bus_dmamap_sync(sq->dmem.dmat, sq->dmem.dmap, BUS_DMASYNC_PREWRITE);
19573c0086b8SZbigniew Bodek 
19582306b72aSZbigniew Bodek 	dprintf(sq->nic->dev, "%s: sq->idx: %d, subdesc_cnt: %d\n",
19592306b72aSZbigniew Bodek 	    __func__, sq->idx, subdesc_cnt);
19603c0086b8SZbigniew Bodek 	/* Inform HW to xmit new packet */
19612306b72aSZbigniew Bodek 	nicvf_queue_reg_write(sq->nic, NIC_QSET_SQ_0_7_DOOR,
19622306b72aSZbigniew Bodek 	    sq->idx, subdesc_cnt);
19632306b72aSZbigniew Bodek 	return (0);
19643c0086b8SZbigniew Bodek }
19653c0086b8SZbigniew Bodek 
19662306b72aSZbigniew Bodek static __inline u_int
frag_num(u_int i)19672306b72aSZbigniew Bodek frag_num(u_int i)
19683c0086b8SZbigniew Bodek {
19692306b72aSZbigniew Bodek #if BYTE_ORDER == BIG_ENDIAN
19702306b72aSZbigniew Bodek 	return ((i & ~3) + 3 - (i & 3));
19713c0086b8SZbigniew Bodek #else
19722306b72aSZbigniew Bodek 	return (i);
19733c0086b8SZbigniew Bodek #endif
19743c0086b8SZbigniew Bodek }
19753c0086b8SZbigniew Bodek 
19762306b72aSZbigniew Bodek /* Returns MBUF for a received packet */
19772306b72aSZbigniew Bodek struct mbuf *
nicvf_get_rcv_mbuf(struct nicvf * nic,struct cqe_rx_t * cqe_rx)19782306b72aSZbigniew Bodek nicvf_get_rcv_mbuf(struct nicvf *nic, struct cqe_rx_t *cqe_rx)
19793c0086b8SZbigniew Bodek {
19803c0086b8SZbigniew Bodek 	int frag;
19813c0086b8SZbigniew Bodek 	int payload_len = 0;
19822306b72aSZbigniew Bodek 	struct mbuf *mbuf;
19832306b72aSZbigniew Bodek 	struct mbuf *mbuf_frag;
19842306b72aSZbigniew Bodek 	uint16_t *rb_lens = NULL;
19852306b72aSZbigniew Bodek 	uint64_t *rb_ptrs = NULL;
19863c0086b8SZbigniew Bodek 
19872306b72aSZbigniew Bodek 	mbuf = NULL;
19882306b72aSZbigniew Bodek 	rb_lens = (uint16_t *)((uint8_t *)cqe_rx + (3 * sizeof(uint64_t)));
19892306b72aSZbigniew Bodek 	rb_ptrs = (uint64_t *)((uint8_t *)cqe_rx + (6 * sizeof(uint64_t)));
19903c0086b8SZbigniew Bodek 
19912306b72aSZbigniew Bodek 	dprintf(nic->dev, "%s rb_cnt %d rb0_ptr %lx rb0_sz %d\n",
19923c0086b8SZbigniew Bodek 	    __func__, cqe_rx->rb_cnt, cqe_rx->rb0_ptr, cqe_rx->rb0_sz);
19933c0086b8SZbigniew Bodek 
19943c0086b8SZbigniew Bodek 	for (frag = 0; frag < cqe_rx->rb_cnt; frag++) {
19953c0086b8SZbigniew Bodek 		payload_len = rb_lens[frag_num(frag)];
19962306b72aSZbigniew Bodek 		if (frag == 0) {
19973c0086b8SZbigniew Bodek 			/* First fragment */
19982306b72aSZbigniew Bodek 			mbuf = nicvf_rb_ptr_to_mbuf(nic,
19992306b72aSZbigniew Bodek 			    (*rb_ptrs - cqe_rx->align_pad));
20002306b72aSZbigniew Bodek 			mbuf->m_len = payload_len;
20012306b72aSZbigniew Bodek 			mbuf->m_data += cqe_rx->align_pad;
20022306b72aSZbigniew Bodek 			if_setrcvif(mbuf, nic->ifp);
20033c0086b8SZbigniew Bodek 		} else {
20043c0086b8SZbigniew Bodek 			/* Add fragments */
20052306b72aSZbigniew Bodek 			mbuf_frag = nicvf_rb_ptr_to_mbuf(nic, *rb_ptrs);
20062306b72aSZbigniew Bodek 			m_append(mbuf, payload_len, mbuf_frag->m_data);
20072306b72aSZbigniew Bodek 			m_freem(mbuf_frag);
20083c0086b8SZbigniew Bodek 		}
20093c0086b8SZbigniew Bodek 		/* Next buffer pointer */
20103c0086b8SZbigniew Bodek 		rb_ptrs++;
20113c0086b8SZbigniew Bodek 	}
20122306b72aSZbigniew Bodek 
20132306b72aSZbigniew Bodek 	if (__predict_true(mbuf != NULL)) {
20142306b72aSZbigniew Bodek 		m_fixhdr(mbuf);
20152306b72aSZbigniew Bodek 		mbuf->m_pkthdr.flowid = cqe_rx->rq_idx;
20162306b72aSZbigniew Bodek 		M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE);
2017856dce91SZbigniew Bodek 		if (__predict_true((if_getcapenable(nic->ifp) & IFCAP_RXCSUM) != 0)) {
2018856dce91SZbigniew Bodek 			/*
2019856dce91SZbigniew Bodek 			 * HW by default verifies IP & TCP/UDP/SCTP checksums
2020856dce91SZbigniew Bodek 			 */
2021c8907923SZbigniew Bodek 			if (__predict_true(cqe_rx->l3_type == L3TYPE_IPV4)) {
2022856dce91SZbigniew Bodek 				mbuf->m_pkthdr.csum_flags =
2023856dce91SZbigniew Bodek 				    (CSUM_IP_CHECKED | CSUM_IP_VALID);
2024856dce91SZbigniew Bodek 			}
2025c8907923SZbigniew Bodek 
2026c8907923SZbigniew Bodek 			switch (cqe_rx->l4_type) {
2027c8907923SZbigniew Bodek 			case L4TYPE_UDP:
2028c8907923SZbigniew Bodek 			case L4TYPE_TCP: /* fall through */
2029856dce91SZbigniew Bodek 				mbuf->m_pkthdr.csum_flags |=
2030856dce91SZbigniew Bodek 				    (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
2031c8907923SZbigniew Bodek 				mbuf->m_pkthdr.csum_data = 0xffff;
2032c8907923SZbigniew Bodek 				break;
2033c8907923SZbigniew Bodek 			case L4TYPE_SCTP:
2034c8907923SZbigniew Bodek 				mbuf->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
2035c8907923SZbigniew Bodek 				break;
2036c8907923SZbigniew Bodek 			default:
2037c8907923SZbigniew Bodek 				break;
2038856dce91SZbigniew Bodek 			}
2039856dce91SZbigniew Bodek 		}
20402306b72aSZbigniew Bodek 	}
20412306b72aSZbigniew Bodek 
20422306b72aSZbigniew Bodek 	return (mbuf);
20433c0086b8SZbigniew Bodek }
20443c0086b8SZbigniew Bodek 
20453c0086b8SZbigniew Bodek /* Enable interrupt */
20462306b72aSZbigniew Bodek void
nicvf_enable_intr(struct nicvf * nic,int int_type,int q_idx)20472306b72aSZbigniew Bodek nicvf_enable_intr(struct nicvf *nic, int int_type, int q_idx)
20483c0086b8SZbigniew Bodek {
20492306b72aSZbigniew Bodek 	uint64_t reg_val;
20503c0086b8SZbigniew Bodek 
20513c0086b8SZbigniew Bodek 	reg_val = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
20523c0086b8SZbigniew Bodek 
20533c0086b8SZbigniew Bodek 	switch (int_type) {
20543c0086b8SZbigniew Bodek 	case NICVF_INTR_CQ:
20552306b72aSZbigniew Bodek 		reg_val |= ((1UL << q_idx) << NICVF_INTR_CQ_SHIFT);
20563c0086b8SZbigniew Bodek 		break;
20573c0086b8SZbigniew Bodek 	case NICVF_INTR_SQ:
20582306b72aSZbigniew Bodek 		reg_val |= ((1UL << q_idx) << NICVF_INTR_SQ_SHIFT);
20593c0086b8SZbigniew Bodek 		break;
20603c0086b8SZbigniew Bodek 	case NICVF_INTR_RBDR:
20612306b72aSZbigniew Bodek 		reg_val |= ((1UL << q_idx) << NICVF_INTR_RBDR_SHIFT);
20623c0086b8SZbigniew Bodek 		break;
20633c0086b8SZbigniew Bodek 	case NICVF_INTR_PKT_DROP:
20642306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_PKT_DROP_SHIFT);
20653c0086b8SZbigniew Bodek 		break;
20663c0086b8SZbigniew Bodek 	case NICVF_INTR_TCP_TIMER:
20672306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_TCP_TIMER_SHIFT);
20683c0086b8SZbigniew Bodek 		break;
20693c0086b8SZbigniew Bodek 	case NICVF_INTR_MBOX:
20702306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_MBOX_SHIFT);
20713c0086b8SZbigniew Bodek 		break;
20723c0086b8SZbigniew Bodek 	case NICVF_INTR_QS_ERR:
20732306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_QS_ERR_SHIFT);
20743c0086b8SZbigniew Bodek 		break;
20753c0086b8SZbigniew Bodek 	default:
20762306b72aSZbigniew Bodek 		device_printf(nic->dev,
20773c0086b8SZbigniew Bodek 			   "Failed to enable interrupt: unknown type\n");
20783c0086b8SZbigniew Bodek 		break;
20793c0086b8SZbigniew Bodek 	}
20803c0086b8SZbigniew Bodek 
20813c0086b8SZbigniew Bodek 	nicvf_reg_write(nic, NIC_VF_ENA_W1S, reg_val);
20823c0086b8SZbigniew Bodek }
20833c0086b8SZbigniew Bodek 
20843c0086b8SZbigniew Bodek /* Disable interrupt */
20852306b72aSZbigniew Bodek void
nicvf_disable_intr(struct nicvf * nic,int int_type,int q_idx)20862306b72aSZbigniew Bodek nicvf_disable_intr(struct nicvf *nic, int int_type, int q_idx)
20873c0086b8SZbigniew Bodek {
20882306b72aSZbigniew Bodek 	uint64_t reg_val = 0;
20893c0086b8SZbigniew Bodek 
20903c0086b8SZbigniew Bodek 	switch (int_type) {
20913c0086b8SZbigniew Bodek 	case NICVF_INTR_CQ:
20922306b72aSZbigniew Bodek 		reg_val |= ((1UL << q_idx) << NICVF_INTR_CQ_SHIFT);
20933c0086b8SZbigniew Bodek 		break;
20943c0086b8SZbigniew Bodek 	case NICVF_INTR_SQ:
20952306b72aSZbigniew Bodek 		reg_val |= ((1UL << q_idx) << NICVF_INTR_SQ_SHIFT);
20963c0086b8SZbigniew Bodek 		break;
20973c0086b8SZbigniew Bodek 	case NICVF_INTR_RBDR:
20982306b72aSZbigniew Bodek 		reg_val |= ((1UL << q_idx) << NICVF_INTR_RBDR_SHIFT);
20993c0086b8SZbigniew Bodek 		break;
21003c0086b8SZbigniew Bodek 	case NICVF_INTR_PKT_DROP:
21012306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_PKT_DROP_SHIFT);
21023c0086b8SZbigniew Bodek 		break;
21033c0086b8SZbigniew Bodek 	case NICVF_INTR_TCP_TIMER:
21042306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_TCP_TIMER_SHIFT);
21053c0086b8SZbigniew Bodek 		break;
21063c0086b8SZbigniew Bodek 	case NICVF_INTR_MBOX:
21072306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_MBOX_SHIFT);
21083c0086b8SZbigniew Bodek 		break;
21093c0086b8SZbigniew Bodek 	case NICVF_INTR_QS_ERR:
21102306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_QS_ERR_SHIFT);
21113c0086b8SZbigniew Bodek 		break;
21123c0086b8SZbigniew Bodek 	default:
21132306b72aSZbigniew Bodek 		device_printf(nic->dev,
21143c0086b8SZbigniew Bodek 			   "Failed to disable interrupt: unknown type\n");
21153c0086b8SZbigniew Bodek 		break;
21163c0086b8SZbigniew Bodek 	}
21173c0086b8SZbigniew Bodek 
21183c0086b8SZbigniew Bodek 	nicvf_reg_write(nic, NIC_VF_ENA_W1C, reg_val);
21193c0086b8SZbigniew Bodek }
21203c0086b8SZbigniew Bodek 
21213c0086b8SZbigniew Bodek /* Clear interrupt */
21222306b72aSZbigniew Bodek void
nicvf_clear_intr(struct nicvf * nic,int int_type,int q_idx)21232306b72aSZbigniew Bodek nicvf_clear_intr(struct nicvf *nic, int int_type, int q_idx)
21243c0086b8SZbigniew Bodek {
21252306b72aSZbigniew Bodek 	uint64_t reg_val = 0;
21263c0086b8SZbigniew Bodek 
21273c0086b8SZbigniew Bodek 	switch (int_type) {
21283c0086b8SZbigniew Bodek 	case NICVF_INTR_CQ:
21292306b72aSZbigniew Bodek 		reg_val = ((1UL << q_idx) << NICVF_INTR_CQ_SHIFT);
21303c0086b8SZbigniew Bodek 		break;
21313c0086b8SZbigniew Bodek 	case NICVF_INTR_SQ:
21322306b72aSZbigniew Bodek 		reg_val = ((1UL << q_idx) << NICVF_INTR_SQ_SHIFT);
21333c0086b8SZbigniew Bodek 		break;
21343c0086b8SZbigniew Bodek 	case NICVF_INTR_RBDR:
21352306b72aSZbigniew Bodek 		reg_val = ((1UL << q_idx) << NICVF_INTR_RBDR_SHIFT);
21363c0086b8SZbigniew Bodek 		break;
21373c0086b8SZbigniew Bodek 	case NICVF_INTR_PKT_DROP:
21382306b72aSZbigniew Bodek 		reg_val = (1UL << NICVF_INTR_PKT_DROP_SHIFT);
21393c0086b8SZbigniew Bodek 		break;
21403c0086b8SZbigniew Bodek 	case NICVF_INTR_TCP_TIMER:
21412306b72aSZbigniew Bodek 		reg_val = (1UL << NICVF_INTR_TCP_TIMER_SHIFT);
21423c0086b8SZbigniew Bodek 		break;
21433c0086b8SZbigniew Bodek 	case NICVF_INTR_MBOX:
21442306b72aSZbigniew Bodek 		reg_val = (1UL << NICVF_INTR_MBOX_SHIFT);
21453c0086b8SZbigniew Bodek 		break;
21463c0086b8SZbigniew Bodek 	case NICVF_INTR_QS_ERR:
21472306b72aSZbigniew Bodek 		reg_val |= (1UL << NICVF_INTR_QS_ERR_SHIFT);
21483c0086b8SZbigniew Bodek 		break;
21493c0086b8SZbigniew Bodek 	default:
21502306b72aSZbigniew Bodek 		device_printf(nic->dev,
21513c0086b8SZbigniew Bodek 			   "Failed to clear interrupt: unknown type\n");
21523c0086b8SZbigniew Bodek 		break;
21533c0086b8SZbigniew Bodek 	}
21543c0086b8SZbigniew Bodek 
21553c0086b8SZbigniew Bodek 	nicvf_reg_write(nic, NIC_VF_INT, reg_val);
21563c0086b8SZbigniew Bodek }
21573c0086b8SZbigniew Bodek 
21583c0086b8SZbigniew Bodek /* Check if interrupt is enabled */
21592306b72aSZbigniew Bodek int
nicvf_is_intr_enabled(struct nicvf * nic,int int_type,int q_idx)21602306b72aSZbigniew Bodek nicvf_is_intr_enabled(struct nicvf *nic, int int_type, int q_idx)
21613c0086b8SZbigniew Bodek {
21622306b72aSZbigniew Bodek 	uint64_t reg_val;
21632306b72aSZbigniew Bodek 	uint64_t mask = 0xff;
21643c0086b8SZbigniew Bodek 
21653c0086b8SZbigniew Bodek 	reg_val = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
21663c0086b8SZbigniew Bodek 
21673c0086b8SZbigniew Bodek 	switch (int_type) {
21683c0086b8SZbigniew Bodek 	case NICVF_INTR_CQ:
21692306b72aSZbigniew Bodek 		mask = ((1UL << q_idx) << NICVF_INTR_CQ_SHIFT);
21703c0086b8SZbigniew Bodek 		break;
21713c0086b8SZbigniew Bodek 	case NICVF_INTR_SQ:
21722306b72aSZbigniew Bodek 		mask = ((1UL << q_idx) << NICVF_INTR_SQ_SHIFT);
21733c0086b8SZbigniew Bodek 		break;
21743c0086b8SZbigniew Bodek 	case NICVF_INTR_RBDR:
21752306b72aSZbigniew Bodek 		mask = ((1UL << q_idx) << NICVF_INTR_RBDR_SHIFT);
21763c0086b8SZbigniew Bodek 		break;
21773c0086b8SZbigniew Bodek 	case NICVF_INTR_PKT_DROP:
21783c0086b8SZbigniew Bodek 		mask = NICVF_INTR_PKT_DROP_MASK;
21793c0086b8SZbigniew Bodek 		break;
21803c0086b8SZbigniew Bodek 	case NICVF_INTR_TCP_TIMER:
21813c0086b8SZbigniew Bodek 		mask = NICVF_INTR_TCP_TIMER_MASK;
21823c0086b8SZbigniew Bodek 		break;
21833c0086b8SZbigniew Bodek 	case NICVF_INTR_MBOX:
21843c0086b8SZbigniew Bodek 		mask = NICVF_INTR_MBOX_MASK;
21853c0086b8SZbigniew Bodek 		break;
21863c0086b8SZbigniew Bodek 	case NICVF_INTR_QS_ERR:
21873c0086b8SZbigniew Bodek 		mask = NICVF_INTR_QS_ERR_MASK;
21883c0086b8SZbigniew Bodek 		break;
21893c0086b8SZbigniew Bodek 	default:
21902306b72aSZbigniew Bodek 		device_printf(nic->dev,
21913c0086b8SZbigniew Bodek 			   "Failed to check interrupt enable: unknown type\n");
21923c0086b8SZbigniew Bodek 		break;
21933c0086b8SZbigniew Bodek 	}
21943c0086b8SZbigniew Bodek 
21953c0086b8SZbigniew Bodek 	return (reg_val & mask);
21963c0086b8SZbigniew Bodek }
21973c0086b8SZbigniew Bodek 
21982306b72aSZbigniew Bodek void
nicvf_update_rq_stats(struct nicvf * nic,int rq_idx)21992306b72aSZbigniew Bodek nicvf_update_rq_stats(struct nicvf *nic, int rq_idx)
22003c0086b8SZbigniew Bodek {
22013c0086b8SZbigniew Bodek 	struct rcv_queue *rq;
22023c0086b8SZbigniew Bodek 
22033c0086b8SZbigniew Bodek #define GET_RQ_STATS(reg) \
22043c0086b8SZbigniew Bodek 	nicvf_reg_read(nic, NIC_QSET_RQ_0_7_STAT_0_1 |\
22053c0086b8SZbigniew Bodek 			    (rq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
22063c0086b8SZbigniew Bodek 
22073c0086b8SZbigniew Bodek 	rq = &nic->qs->rq[rq_idx];
22083c0086b8SZbigniew Bodek 	rq->stats.bytes = GET_RQ_STATS(RQ_SQ_STATS_OCTS);
22093c0086b8SZbigniew Bodek 	rq->stats.pkts = GET_RQ_STATS(RQ_SQ_STATS_PKTS);
22103c0086b8SZbigniew Bodek }
22113c0086b8SZbigniew Bodek 
22122306b72aSZbigniew Bodek void
nicvf_update_sq_stats(struct nicvf * nic,int sq_idx)22132306b72aSZbigniew Bodek nicvf_update_sq_stats(struct nicvf *nic, int sq_idx)
22143c0086b8SZbigniew Bodek {
22153c0086b8SZbigniew Bodek 	struct snd_queue *sq;
22163c0086b8SZbigniew Bodek 
22173c0086b8SZbigniew Bodek #define GET_SQ_STATS(reg) \
22183c0086b8SZbigniew Bodek 	nicvf_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1 |\
22193c0086b8SZbigniew Bodek 			    (sq_idx << NIC_Q_NUM_SHIFT) | (reg << 3))
22203c0086b8SZbigniew Bodek 
22213c0086b8SZbigniew Bodek 	sq = &nic->qs->sq[sq_idx];
22223c0086b8SZbigniew Bodek 	sq->stats.bytes = GET_SQ_STATS(RQ_SQ_STATS_OCTS);
22233c0086b8SZbigniew Bodek 	sq->stats.pkts = GET_SQ_STATS(RQ_SQ_STATS_PKTS);
22243c0086b8SZbigniew Bodek }
22253c0086b8SZbigniew Bodek 
22263c0086b8SZbigniew Bodek /* Check for errors in the receive cmp.queue entry */
22272306b72aSZbigniew Bodek int
nicvf_check_cqe_rx_errs(struct nicvf * nic,struct cmp_queue * cq,struct cqe_rx_t * cqe_rx)22282306b72aSZbigniew Bodek nicvf_check_cqe_rx_errs(struct nicvf *nic, struct cmp_queue *cq,
22292306b72aSZbigniew Bodek     struct cqe_rx_t *cqe_rx)
22303c0086b8SZbigniew Bodek {
22313c0086b8SZbigniew Bodek 	struct nicvf_hw_stats *stats = &nic->hw_stats;
22323c0086b8SZbigniew Bodek 	struct nicvf_drv_stats *drv_stats = &nic->drv_stats;
22333c0086b8SZbigniew Bodek 
22343c0086b8SZbigniew Bodek 	if (!cqe_rx->err_level && !cqe_rx->err_opcode) {
22353c0086b8SZbigniew Bodek 		drv_stats->rx_frames_ok++;
22362306b72aSZbigniew Bodek 		return (0);
22373c0086b8SZbigniew Bodek 	}
22383c0086b8SZbigniew Bodek 
22393c0086b8SZbigniew Bodek 	switch (cqe_rx->err_opcode) {
22403c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_RE_PARTIAL:
22413c0086b8SZbigniew Bodek 		stats->rx_bgx_truncated_pkts++;
22423c0086b8SZbigniew Bodek 		break;
22433c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_RE_JABBER:
22443c0086b8SZbigniew Bodek 		stats->rx_jabber_errs++;
22453c0086b8SZbigniew Bodek 		break;
22463c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_RE_FCS:
22473c0086b8SZbigniew Bodek 		stats->rx_fcs_errs++;
22483c0086b8SZbigniew Bodek 		break;
22493c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_RE_RX_CTL:
22503c0086b8SZbigniew Bodek 		stats->rx_bgx_errs++;
22513c0086b8SZbigniew Bodek 		break;
22523c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_PREL2_ERR:
22533c0086b8SZbigniew Bodek 		stats->rx_prel2_errs++;
22543c0086b8SZbigniew Bodek 		break;
22553c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L2_MAL:
22563c0086b8SZbigniew Bodek 		stats->rx_l2_hdr_malformed++;
22573c0086b8SZbigniew Bodek 		break;
22583c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L2_OVERSIZE:
22593c0086b8SZbigniew Bodek 		stats->rx_oversize++;
22603c0086b8SZbigniew Bodek 		break;
22613c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L2_UNDERSIZE:
22623c0086b8SZbigniew Bodek 		stats->rx_undersize++;
22633c0086b8SZbigniew Bodek 		break;
22643c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L2_LENMISM:
22653c0086b8SZbigniew Bodek 		stats->rx_l2_len_mismatch++;
22663c0086b8SZbigniew Bodek 		break;
22673c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L2_PCLP:
22683c0086b8SZbigniew Bodek 		stats->rx_l2_pclp++;
22693c0086b8SZbigniew Bodek 		break;
22703c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_IP_NOT:
22713c0086b8SZbigniew Bodek 		stats->rx_ip_ver_errs++;
22723c0086b8SZbigniew Bodek 		break;
22733c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_IP_CSUM_ERR:
22743c0086b8SZbigniew Bodek 		stats->rx_ip_csum_errs++;
22753c0086b8SZbigniew Bodek 		break;
22763c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_IP_MAL:
22773c0086b8SZbigniew Bodek 		stats->rx_ip_hdr_malformed++;
22783c0086b8SZbigniew Bodek 		break;
22793c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_IP_MALD:
22803c0086b8SZbigniew Bodek 		stats->rx_ip_payload_malformed++;
22813c0086b8SZbigniew Bodek 		break;
22823c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_IP_HOP:
22833c0086b8SZbigniew Bodek 		stats->rx_ip_ttl_errs++;
22843c0086b8SZbigniew Bodek 		break;
22853c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L3_PCLP:
22863c0086b8SZbigniew Bodek 		stats->rx_l3_pclp++;
22873c0086b8SZbigniew Bodek 		break;
22883c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L4_MAL:
22893c0086b8SZbigniew Bodek 		stats->rx_l4_malformed++;
22903c0086b8SZbigniew Bodek 		break;
22913c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L4_CHK:
22923c0086b8SZbigniew Bodek 		stats->rx_l4_csum_errs++;
22933c0086b8SZbigniew Bodek 		break;
22943c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_UDP_LEN:
22953c0086b8SZbigniew Bodek 		stats->rx_udp_len_errs++;
22963c0086b8SZbigniew Bodek 		break;
22973c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L4_PORT:
22983c0086b8SZbigniew Bodek 		stats->rx_l4_port_errs++;
22993c0086b8SZbigniew Bodek 		break;
23003c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_TCP_FLAG:
23013c0086b8SZbigniew Bodek 		stats->rx_tcp_flag_errs++;
23023c0086b8SZbigniew Bodek 		break;
23033c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_TCP_OFFSET:
23043c0086b8SZbigniew Bodek 		stats->rx_tcp_offset_errs++;
23053c0086b8SZbigniew Bodek 		break;
23063c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_L4_PCLP:
23073c0086b8SZbigniew Bodek 		stats->rx_l4_pclp++;
23083c0086b8SZbigniew Bodek 		break;
23093c0086b8SZbigniew Bodek 	case CQ_RX_ERROP_RBDR_TRUNC:
23103c0086b8SZbigniew Bodek 		stats->rx_truncated_pkts++;
23113c0086b8SZbigniew Bodek 		break;
23123c0086b8SZbigniew Bodek 	}
23133c0086b8SZbigniew Bodek 
23142306b72aSZbigniew Bodek 	return (1);
23153c0086b8SZbigniew Bodek }
23163c0086b8SZbigniew Bodek 
23173c0086b8SZbigniew Bodek /* Check for errors in the send cmp.queue entry */
23182306b72aSZbigniew Bodek int
nicvf_check_cqe_tx_errs(struct nicvf * nic,struct cmp_queue * cq,struct cqe_send_t * cqe_tx)23192306b72aSZbigniew Bodek nicvf_check_cqe_tx_errs(struct nicvf *nic, struct cmp_queue *cq,
23202306b72aSZbigniew Bodek     struct cqe_send_t *cqe_tx)
23213c0086b8SZbigniew Bodek {
23223c0086b8SZbigniew Bodek 	struct cmp_queue_stats *stats = &cq->stats;
23233c0086b8SZbigniew Bodek 
23243c0086b8SZbigniew Bodek 	switch (cqe_tx->send_status) {
23253c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_GOOD:
23263c0086b8SZbigniew Bodek 		stats->tx.good++;
23272306b72aSZbigniew Bodek 		return (0);
23283c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_DESC_FAULT:
23293c0086b8SZbigniew Bodek 		stats->tx.desc_fault++;
23303c0086b8SZbigniew Bodek 		break;
23313c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_HDR_CONS_ERR:
23323c0086b8SZbigniew Bodek 		stats->tx.hdr_cons_err++;
23333c0086b8SZbigniew Bodek 		break;
23343c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_SUBDC_ERR:
23353c0086b8SZbigniew Bodek 		stats->tx.subdesc_err++;
23363c0086b8SZbigniew Bodek 		break;
23373c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_IMM_SIZE_OFLOW:
23383c0086b8SZbigniew Bodek 		stats->tx.imm_size_oflow++;
23393c0086b8SZbigniew Bodek 		break;
23403c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_DATA_SEQUENCE_ERR:
23413c0086b8SZbigniew Bodek 		stats->tx.data_seq_err++;
23423c0086b8SZbigniew Bodek 		break;
23433c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_MEM_SEQUENCE_ERR:
23443c0086b8SZbigniew Bodek 		stats->tx.mem_seq_err++;
23453c0086b8SZbigniew Bodek 		break;
23463c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_LOCK_VIOL:
23473c0086b8SZbigniew Bodek 		stats->tx.lock_viol++;
23483c0086b8SZbigniew Bodek 		break;
23493c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_DATA_FAULT:
23503c0086b8SZbigniew Bodek 		stats->tx.data_fault++;
23513c0086b8SZbigniew Bodek 		break;
23523c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_TSTMP_CONFLICT:
23533c0086b8SZbigniew Bodek 		stats->tx.tstmp_conflict++;
23543c0086b8SZbigniew Bodek 		break;
23553c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_TSTMP_TIMEOUT:
23563c0086b8SZbigniew Bodek 		stats->tx.tstmp_timeout++;
23573c0086b8SZbigniew Bodek 		break;
23583c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_MEM_FAULT:
23593c0086b8SZbigniew Bodek 		stats->tx.mem_fault++;
23603c0086b8SZbigniew Bodek 		break;
23613c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_CK_OVERLAP:
23623c0086b8SZbigniew Bodek 		stats->tx.csum_overlap++;
23633c0086b8SZbigniew Bodek 		break;
23643c0086b8SZbigniew Bodek 	case CQ_TX_ERROP_CK_OFLOW:
23653c0086b8SZbigniew Bodek 		stats->tx.csum_overflow++;
23663c0086b8SZbigniew Bodek 		break;
23673c0086b8SZbigniew Bodek 	}
23683c0086b8SZbigniew Bodek 
23692306b72aSZbigniew Bodek 	return (1);
23703c0086b8SZbigniew Bodek }
2371