xref: /freebsd/sys/net/iflib.c (revision d0d0ad0ae2840d26bb0b1d70f5a0184106dd1e97)
14c7070dbSScott Long /*-
2e035717eSSean Bruno  * Copyright (c) 2014-2017, Matthew Macy <mmacy@nextbsd.org>
34c7070dbSScott Long  * All rights reserved.
44c7070dbSScott Long  *
54c7070dbSScott Long  * Redistribution and use in source and binary forms, with or without
64c7070dbSScott Long  * modification, are permitted provided that the following conditions are met:
74c7070dbSScott Long  *
84c7070dbSScott Long  *  1. Redistributions of source code must retain the above copyright notice,
94c7070dbSScott Long  *     this list of conditions and the following disclaimer.
104c7070dbSScott Long  *
114c7070dbSScott Long  *  2. Neither the name of Matthew Macy nor the names of its
124c7070dbSScott Long  *     contributors may be used to endorse or promote products derived from
134c7070dbSScott Long  *     this software without specific prior written permission.
144c7070dbSScott Long  *
154c7070dbSScott Long  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
164c7070dbSScott Long  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174c7070dbSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184c7070dbSScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
194c7070dbSScott Long  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
204c7070dbSScott Long  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
214c7070dbSScott Long  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
224c7070dbSScott Long  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
234c7070dbSScott Long  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
244c7070dbSScott Long  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
254c7070dbSScott Long  * POSSIBILITY OF SUCH DAMAGE.
264c7070dbSScott Long  */
274c7070dbSScott Long 
284c7070dbSScott Long #include <sys/cdefs.h>
294c7070dbSScott Long __FBSDID("$FreeBSD$");
304c7070dbSScott Long 
31aaeb188aSBjoern A. Zeeb #include "opt_inet.h"
32aaeb188aSBjoern A. Zeeb #include "opt_inet6.h"
33aaeb188aSBjoern A. Zeeb #include "opt_acpi.h"
34aaeb188aSBjoern A. Zeeb 
354c7070dbSScott Long #include <sys/param.h>
364c7070dbSScott Long #include <sys/types.h>
374c7070dbSScott Long #include <sys/bus.h>
384c7070dbSScott Long #include <sys/eventhandler.h>
394c7070dbSScott Long #include <sys/sockio.h>
404c7070dbSScott Long #include <sys/kernel.h>
414c7070dbSScott Long #include <sys/lock.h>
424c7070dbSScott Long #include <sys/mutex.h>
434c7070dbSScott Long #include <sys/module.h>
444c7070dbSScott Long #include <sys/kobj.h>
454c7070dbSScott Long #include <sys/rman.h>
464c7070dbSScott Long #include <sys/sbuf.h>
474c7070dbSScott Long #include <sys/smp.h>
484c7070dbSScott Long #include <sys/socket.h>
494c7070dbSScott Long #include <sys/sysctl.h>
504c7070dbSScott Long #include <sys/syslog.h>
514c7070dbSScott Long #include <sys/taskqueue.h>
5223ac9029SStephen Hurd #include <sys/limits.h>
534c7070dbSScott Long 
54ab2e3f79SStephen Hurd 
554c7070dbSScott Long #include <net/if.h>
564c7070dbSScott Long #include <net/if_var.h>
574c7070dbSScott Long #include <net/if_types.h>
584c7070dbSScott Long #include <net/if_media.h>
594c7070dbSScott Long #include <net/bpf.h>
604c7070dbSScott Long #include <net/ethernet.h>
614c7070dbSScott Long #include <net/mp_ring.h>
624c7070dbSScott Long 
634c7070dbSScott Long #include <netinet/in.h>
644c7070dbSScott Long #include <netinet/in_pcb.h>
654c7070dbSScott Long #include <netinet/tcp_lro.h>
664c7070dbSScott Long #include <netinet/in_systm.h>
674c7070dbSScott Long #include <netinet/if_ether.h>
684c7070dbSScott Long #include <netinet/ip.h>
694c7070dbSScott Long #include <netinet/ip6.h>
704c7070dbSScott Long #include <netinet/tcp.h>
714c7070dbSScott Long 
724c7070dbSScott Long #include <machine/bus.h>
734c7070dbSScott Long #include <machine/in_cksum.h>
744c7070dbSScott Long 
754c7070dbSScott Long #include <vm/vm.h>
764c7070dbSScott Long #include <vm/pmap.h>
774c7070dbSScott Long 
784c7070dbSScott Long #include <dev/led/led.h>
794c7070dbSScott Long #include <dev/pci/pcireg.h>
804c7070dbSScott Long #include <dev/pci/pcivar.h>
814c7070dbSScott Long #include <dev/pci/pci_private.h>
824c7070dbSScott Long 
834c7070dbSScott Long #include <net/iflib.h>
844c7070dbSScott Long 
854c7070dbSScott Long #include "ifdi_if.h"
864c7070dbSScott Long 
874c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
884c7070dbSScott Long #include <sys/memdesc.h>
894c7070dbSScott Long #include <machine/bus.h>
904c7070dbSScott Long #include <machine/md_var.h>
914c7070dbSScott Long #include <machine/specialreg.h>
924c7070dbSScott Long #include <x86/include/busdma_impl.h>
934c7070dbSScott Long #include <x86/iommu/busdma_dmar.h>
944c7070dbSScott Long #endif
954c7070dbSScott Long 
9687890dbaSSean Bruno #include <sys/bitstring.h>
974c7070dbSScott Long /*
9895246abbSSean Bruno  * enable accounting of every mbuf as it comes in to and goes out of
9995246abbSSean Bruno  * iflib's software descriptor references
1004c7070dbSScott Long  */
1014c7070dbSScott Long #define MEMORY_LOGGING 0
1024c7070dbSScott Long /*
1034c7070dbSScott Long  * Enable mbuf vectors for compressing long mbuf chains
1044c7070dbSScott Long  */
1054c7070dbSScott Long 
1064c7070dbSScott Long /*
1074c7070dbSScott Long  * NB:
1084c7070dbSScott Long  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
1094c7070dbSScott Long  *   we prefetch needs to be determined by the time spent in m_free vis a vis
1104c7070dbSScott Long  *   the cost of a prefetch. This will of course vary based on the workload:
1114c7070dbSScott Long  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
1124c7070dbSScott Long  *        is quite expensive, thus suggesting very little prefetch.
1134c7070dbSScott Long  *      - small packet forwarding which is just returning a single mbuf to
1144c7070dbSScott Long  *        UMA will typically be very fast vis a vis the cost of a memory
1154c7070dbSScott Long  *        access.
1164c7070dbSScott Long  */
1174c7070dbSScott Long 
1184c7070dbSScott Long 
1194c7070dbSScott Long /*
1204c7070dbSScott Long  * File organization:
1214c7070dbSScott Long  *  - private structures
1224c7070dbSScott Long  *  - iflib private utility functions
1234c7070dbSScott Long  *  - ifnet functions
1244c7070dbSScott Long  *  - vlan registry and other exported functions
1254c7070dbSScott Long  *  - iflib public core functions
1264c7070dbSScott Long  *
1274c7070dbSScott Long  *
1284c7070dbSScott Long  */
1294c7070dbSScott Long static MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
1304c7070dbSScott Long 
1314c7070dbSScott Long struct iflib_txq;
1324c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t;
1334c7070dbSScott Long struct iflib_rxq;
1344c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t;
1354c7070dbSScott Long struct iflib_fl;
1364c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t;
1374c7070dbSScott Long 
1384ecb427aSSean Bruno struct iflib_ctx;
1394ecb427aSSean Bruno 
1404c7070dbSScott Long typedef struct iflib_filter_info {
1414c7070dbSScott Long 	driver_filter_t *ifi_filter;
1424c7070dbSScott Long 	void *ifi_filter_arg;
1434c7070dbSScott Long 	struct grouptask *ifi_task;
14495246abbSSean Bruno 	void *ifi_ctx;
1454c7070dbSScott Long } *iflib_filter_info_t;
1464c7070dbSScott Long 
1474c7070dbSScott Long struct iflib_ctx {
1484c7070dbSScott Long 	KOBJ_FIELDS;
1494c7070dbSScott Long    /*
1504c7070dbSScott Long    * Pointer to hardware driver's softc
1514c7070dbSScott Long    */
1524c7070dbSScott Long 	void *ifc_softc;
1534c7070dbSScott Long 	device_t ifc_dev;
1544c7070dbSScott Long 	if_t ifc_ifp;
1554c7070dbSScott Long 
1564c7070dbSScott Long 	cpuset_t ifc_cpus;
1574c7070dbSScott Long 	if_shared_ctx_t ifc_sctx;
1584c7070dbSScott Long 	struct if_softc_ctx ifc_softc_ctx;
1594c7070dbSScott Long 
160ab2e3f79SStephen Hurd 	struct mtx ifc_mtx;
1614c7070dbSScott Long 
1624c7070dbSScott Long 	uint16_t ifc_nhwtxqs;
1634c7070dbSScott Long 	uint16_t ifc_nhwrxqs;
1644c7070dbSScott Long 
1654c7070dbSScott Long 	iflib_txq_t ifc_txqs;
1664c7070dbSScott Long 	iflib_rxq_t ifc_rxqs;
1674c7070dbSScott Long 	uint32_t ifc_if_flags;
1684c7070dbSScott Long 	uint32_t ifc_flags;
1694c7070dbSScott Long 	uint32_t ifc_max_fl_buf_size;
1704c7070dbSScott Long 	int ifc_in_detach;
1714c7070dbSScott Long 
1724c7070dbSScott Long 	int ifc_link_state;
1734c7070dbSScott Long 	int ifc_link_irq;
1744c7070dbSScott Long 	int ifc_watchdog_events;
1754c7070dbSScott Long 	struct cdev *ifc_led_dev;
1764c7070dbSScott Long 	struct resource *ifc_msix_mem;
1774c7070dbSScott Long 
1784c7070dbSScott Long 	struct if_irq ifc_legacy_irq;
1794c7070dbSScott Long 	struct grouptask ifc_admin_task;
1804c7070dbSScott Long 	struct grouptask ifc_vflr_task;
1814c7070dbSScott Long 	struct iflib_filter_info ifc_filter_info;
1824c7070dbSScott Long 	struct ifmedia	ifc_media;
1834c7070dbSScott Long 
1844c7070dbSScott Long 	struct sysctl_oid *ifc_sysctl_node;
1854c7070dbSScott Long 	uint16_t ifc_sysctl_ntxqs;
1864c7070dbSScott Long 	uint16_t ifc_sysctl_nrxqs;
18723ac9029SStephen Hurd 	uint16_t ifc_sysctl_qs_eq_override;
18823ac9029SStephen Hurd 
18995246abbSSean Bruno 	qidx_t ifc_sysctl_ntxds[8];
19095246abbSSean Bruno 	qidx_t ifc_sysctl_nrxds[8];
1914c7070dbSScott Long 	struct if_txrx ifc_txrx;
1924c7070dbSScott Long #define isc_txd_encap  ifc_txrx.ift_txd_encap
1934c7070dbSScott Long #define isc_txd_flush  ifc_txrx.ift_txd_flush
1944c7070dbSScott Long #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
1954c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available
1964c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
1974c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
1984c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush
1994c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2004c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2014c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr
2024c7070dbSScott Long 	eventhandler_tag ifc_vlan_attach_event;
2034c7070dbSScott Long 	eventhandler_tag ifc_vlan_detach_event;
2044c7070dbSScott Long 	uint8_t ifc_mac[ETHER_ADDR_LEN];
2054c7070dbSScott Long 	char ifc_mtx_name[16];
2064c7070dbSScott Long };
2074c7070dbSScott Long 
2084c7070dbSScott Long 
2094c7070dbSScott Long void *
2104c7070dbSScott Long iflib_get_softc(if_ctx_t ctx)
2114c7070dbSScott Long {
2124c7070dbSScott Long 
2134c7070dbSScott Long 	return (ctx->ifc_softc);
2144c7070dbSScott Long }
2154c7070dbSScott Long 
2164c7070dbSScott Long device_t
2174c7070dbSScott Long iflib_get_dev(if_ctx_t ctx)
2184c7070dbSScott Long {
2194c7070dbSScott Long 
2204c7070dbSScott Long 	return (ctx->ifc_dev);
2214c7070dbSScott Long }
2224c7070dbSScott Long 
2234c7070dbSScott Long if_t
2244c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx)
2254c7070dbSScott Long {
2264c7070dbSScott Long 
2274c7070dbSScott Long 	return (ctx->ifc_ifp);
2284c7070dbSScott Long }
2294c7070dbSScott Long 
2304c7070dbSScott Long struct ifmedia *
2314c7070dbSScott Long iflib_get_media(if_ctx_t ctx)
2324c7070dbSScott Long {
2334c7070dbSScott Long 
2344c7070dbSScott Long 	return (&ctx->ifc_media);
2354c7070dbSScott Long }
2364c7070dbSScott Long 
2374c7070dbSScott Long void
2384c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
2394c7070dbSScott Long {
2404c7070dbSScott Long 
2414c7070dbSScott Long 	bcopy(mac, ctx->ifc_mac, ETHER_ADDR_LEN);
2424c7070dbSScott Long }
2434c7070dbSScott Long 
2444c7070dbSScott Long if_softc_ctx_t
2454c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx)
2464c7070dbSScott Long {
2474c7070dbSScott Long 
2484c7070dbSScott Long 	return (&ctx->ifc_softc_ctx);
2494c7070dbSScott Long }
2504c7070dbSScott Long 
2514c7070dbSScott Long if_shared_ctx_t
2524c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx)
2534c7070dbSScott Long {
2544c7070dbSScott Long 
2554c7070dbSScott Long 	return (ctx->ifc_sctx);
2564c7070dbSScott Long }
2574c7070dbSScott Long 
25895246abbSSean Bruno #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
2594c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
2605e888388SSean Bruno #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1)))
2614c7070dbSScott Long 
2624c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
2634c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
2644c7070dbSScott Long 
2654c7070dbSScott Long #define RX_SW_DESC_MAP_CREATED	(1 << 0)
266ab2e3f79SStephen Hurd #define TX_SW_DESC_MAP_CREATED	(1 << 1)
267ab2e3f79SStephen Hurd #define RX_SW_DESC_INUSE        (1 << 3)
268ab2e3f79SStephen Hurd #define TX_SW_DESC_MAPPED       (1 << 4)
2694c7070dbSScott Long 
2702cc3b2eeSGleb Smirnoff #define	M_TOOBIG		M_PROTO1
2715c5ca36cSSean Bruno 
272e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array {
273e035717eSSean Bruno 	bus_dmamap_t	*ifsd_map;         /* bus_dma maps for packet */
274e035717eSSean Bruno 	struct mbuf	**ifsd_m;           /* pkthdr mbufs */
275e035717eSSean Bruno 	caddr_t		*ifsd_cl;          /* direct cluster pointer for rx */
276e035717eSSean Bruno 	uint8_t		*ifsd_flags;
277e035717eSSean Bruno } iflib_rxsd_array_t;
2784c7070dbSScott Long 
2794c7070dbSScott Long typedef struct iflib_sw_tx_desc_array {
2804c7070dbSScott Long 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
2814c7070dbSScott Long 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
2824c7070dbSScott Long 	uint8_t		*ifsd_flags;
28395246abbSSean Bruno } if_txsd_vec_t;
2844c7070dbSScott Long 
2854c7070dbSScott Long 
2864c7070dbSScott Long /* magic number that should be high enough for any hardware */
2874c7070dbSScott Long #define IFLIB_MAX_TX_SEGS		128
2884c7070dbSScott Long #define IFLIB_MAX_RX_SEGS		32
28995246abbSSean Bruno #define IFLIB_RX_COPY_THRESH		128
2904c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH		32
29195246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */
29295246abbSSean Bruno #define IFLIB_MIN_DESC_SEC		16384
29395246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ	16
2944c7070dbSScott Long #define IFLIB_QUEUE_IDLE		0
2954c7070dbSScott Long #define IFLIB_QUEUE_HUNG		1
2964c7070dbSScott Long #define IFLIB_QUEUE_WORKING		2
29795246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */
29895246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR	4
2994c7070dbSScott Long 
30095246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */
30195246abbSSean Bruno #define TX_BATCH_SIZE			32
3024c7070dbSScott Long 
3034c7070dbSScott Long #define IFLIB_RESTART_BUDGET		8
3044c7070dbSScott Long 
30595246abbSSean Bruno #define	IFC_LEGACY		0x001
30695246abbSSean Bruno #define	IFC_QFLUSH		0x002
30795246abbSSean Bruno #define	IFC_MULTISEG		0x004
30895246abbSSean Bruno #define	IFC_DMAR		0x008
30995246abbSSean Bruno #define	IFC_SC_ALLOCATED	0x010
31095246abbSSean Bruno #define	IFC_INIT_DONE		0x020
31195246abbSSean Bruno #define	IFC_PREFETCH		0x040
31295246abbSSean Bruno #define	IFC_DO_RESET		0x080
31395246abbSSean Bruno #define	IFC_CHECK_HUNG		0x100
3144c7070dbSScott Long 
3154c7070dbSScott Long #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
3164c7070dbSScott Long 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
3174c7070dbSScott Long 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
3184c7070dbSScott Long struct iflib_txq {
31995246abbSSean Bruno 	qidx_t		ift_in_use;
32095246abbSSean Bruno 	qidx_t		ift_cidx;
32195246abbSSean Bruno 	qidx_t		ift_cidx_processed;
32295246abbSSean Bruno 	qidx_t		ift_pidx;
3234c7070dbSScott Long 	uint8_t		ift_gen;
32423ac9029SStephen Hurd 	uint8_t		ift_br_offset;
32595246abbSSean Bruno 	uint16_t	ift_npending;
32695246abbSSean Bruno 	uint16_t	ift_db_pending;
32795246abbSSean Bruno 	uint16_t	ift_rs_pending;
3284c7070dbSScott Long 	/* implicit pad */
32995246abbSSean Bruno 	uint8_t		ift_txd_size[8];
3304c7070dbSScott Long 	uint64_t	ift_processed;
3314c7070dbSScott Long 	uint64_t	ift_cleaned;
33295246abbSSean Bruno 	uint64_t	ift_cleaned_prev;
3334c7070dbSScott Long #if MEMORY_LOGGING
3344c7070dbSScott Long 	uint64_t	ift_enqueued;
3354c7070dbSScott Long 	uint64_t	ift_dequeued;
3364c7070dbSScott Long #endif
3374c7070dbSScott Long 	uint64_t	ift_no_tx_dma_setup;
3384c7070dbSScott Long 	uint64_t	ift_no_desc_avail;
3394c7070dbSScott Long 	uint64_t	ift_mbuf_defrag_failed;
3404c7070dbSScott Long 	uint64_t	ift_mbuf_defrag;
3414c7070dbSScott Long 	uint64_t	ift_map_failed;
3424c7070dbSScott Long 	uint64_t	ift_txd_encap_efbig;
3434c7070dbSScott Long 	uint64_t	ift_pullups;
3444c7070dbSScott Long 
3454c7070dbSScott Long 	struct mtx	ift_mtx;
3464c7070dbSScott Long 	struct mtx	ift_db_mtx;
3474c7070dbSScott Long 
3484c7070dbSScott Long 	/* constant values */
3494c7070dbSScott Long 	if_ctx_t	ift_ctx;
35095246abbSSean Bruno 	struct ifmp_ring        *ift_br;
3514c7070dbSScott Long 	struct grouptask	ift_task;
35295246abbSSean Bruno 	qidx_t		ift_size;
3534c7070dbSScott Long 	uint16_t	ift_id;
3544c7070dbSScott Long 	struct callout	ift_timer;
3554c7070dbSScott Long 
35695246abbSSean Bruno 	if_txsd_vec_t	ift_sds;
3574c7070dbSScott Long 	uint8_t		ift_qstatus;
3584c7070dbSScott Long 	uint8_t		ift_closed;
35995246abbSSean Bruno 	uint8_t		ift_update_freq;
3604c7070dbSScott Long 	struct iflib_filter_info ift_filter_info;
3614c7070dbSScott Long 	bus_dma_tag_t		ift_desc_tag;
3624c7070dbSScott Long 	bus_dma_tag_t		ift_tso_desc_tag;
3634c7070dbSScott Long 	iflib_dma_info_t	ift_ifdi;
3644c7070dbSScott Long #define MTX_NAME_LEN 16
3654c7070dbSScott Long 	char                    ift_mtx_name[MTX_NAME_LEN];
3664c7070dbSScott Long 	char                    ift_db_mtx_name[MTX_NAME_LEN];
3674c7070dbSScott Long 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
3681248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
3691248952aSSean Bruno 	uint64_t ift_cpu_exec_count[256];
3701248952aSSean Bruno #endif
3714c7070dbSScott Long } __aligned(CACHE_LINE_SIZE);
3724c7070dbSScott Long 
3734c7070dbSScott Long struct iflib_fl {
37495246abbSSean Bruno 	qidx_t		ifl_cidx;
37595246abbSSean Bruno 	qidx_t		ifl_pidx;
37695246abbSSean Bruno 	qidx_t		ifl_credits;
3774c7070dbSScott Long 	uint8_t		ifl_gen;
37895246abbSSean Bruno 	uint8_t		ifl_rxd_size;
3794c7070dbSScott Long #if MEMORY_LOGGING
3804c7070dbSScott Long 	uint64_t	ifl_m_enqueued;
3814c7070dbSScott Long 	uint64_t	ifl_m_dequeued;
3824c7070dbSScott Long 	uint64_t	ifl_cl_enqueued;
3834c7070dbSScott Long 	uint64_t	ifl_cl_dequeued;
3844c7070dbSScott Long #endif
3854c7070dbSScott Long 	/* implicit pad */
3864c7070dbSScott Long 
38787890dbaSSean Bruno 	bitstr_t 	*ifl_rx_bitmap;
38887890dbaSSean Bruno 	qidx_t		ifl_fragidx;
3894c7070dbSScott Long 	/* constant */
39095246abbSSean Bruno 	qidx_t		ifl_size;
3914c7070dbSScott Long 	uint16_t	ifl_buf_size;
3924c7070dbSScott Long 	uint16_t	ifl_cltype;
3934c7070dbSScott Long 	uma_zone_t	ifl_zone;
394e035717eSSean Bruno 	iflib_rxsd_array_t	ifl_sds;
3954c7070dbSScott Long 	iflib_rxq_t	ifl_rxq;
3964c7070dbSScott Long 	uint8_t		ifl_id;
3974c7070dbSScott Long 	bus_dma_tag_t           ifl_desc_tag;
3984c7070dbSScott Long 	iflib_dma_info_t	ifl_ifdi;
3994c7070dbSScott Long 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
4004c7070dbSScott Long 	caddr_t		ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
40195246abbSSean Bruno 	qidx_t	ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
4024c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4034c7070dbSScott Long 
40495246abbSSean Bruno static inline qidx_t
40595246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
4064c7070dbSScott Long {
40795246abbSSean Bruno 	qidx_t used;
4084c7070dbSScott Long 
4094c7070dbSScott Long 	if (pidx > cidx)
4104c7070dbSScott Long 		used = pidx - cidx;
4114c7070dbSScott Long 	else if (pidx < cidx)
4124c7070dbSScott Long 		used = size - cidx + pidx;
4134c7070dbSScott Long 	else if (gen == 0 && pidx == cidx)
4144c7070dbSScott Long 		used = 0;
4154c7070dbSScott Long 	else if (gen == 1 && pidx == cidx)
4164c7070dbSScott Long 		used = size;
4174c7070dbSScott Long 	else
4184c7070dbSScott Long 		panic("bad state");
4194c7070dbSScott Long 
4204c7070dbSScott Long 	return (used);
4214c7070dbSScott Long }
4224c7070dbSScott Long 
4234c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
4244c7070dbSScott Long 
4254c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \
4264c7070dbSScott Long 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
4274c7070dbSScott Long 
4284c7070dbSScott Long struct iflib_rxq {
4294c7070dbSScott Long 	/* If there is a separate completion queue -
4304c7070dbSScott Long 	 * these are the cq cidx and pidx. Otherwise
4314c7070dbSScott Long 	 * these are unused.
4324c7070dbSScott Long 	 */
43395246abbSSean Bruno 	qidx_t		ifr_size;
43495246abbSSean Bruno 	qidx_t		ifr_cq_cidx;
43595246abbSSean Bruno 	qidx_t		ifr_cq_pidx;
4364c7070dbSScott Long 	uint8_t		ifr_cq_gen;
43723ac9029SStephen Hurd 	uint8_t		ifr_fl_offset;
4384c7070dbSScott Long 
4394c7070dbSScott Long 	if_ctx_t	ifr_ctx;
4404c7070dbSScott Long 	iflib_fl_t	ifr_fl;
4414c7070dbSScott Long 	uint64_t	ifr_rx_irq;
4424c7070dbSScott Long 	uint16_t	ifr_id;
4434c7070dbSScott Long 	uint8_t		ifr_lro_enabled;
4444c7070dbSScott Long 	uint8_t		ifr_nfl;
44595246abbSSean Bruno 	uint8_t		ifr_ntxqirq;
44695246abbSSean Bruno 	uint8_t		ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
4474c7070dbSScott Long 	struct lro_ctrl			ifr_lc;
4484c7070dbSScott Long 	struct grouptask        ifr_task;
4494c7070dbSScott Long 	struct iflib_filter_info ifr_filter_info;
4504c7070dbSScott Long 	iflib_dma_info_t		ifr_ifdi;
451ab2e3f79SStephen Hurd 
4524c7070dbSScott Long 	/* dynamically allocate if any drivers need a value substantially larger than this */
4534c7070dbSScott Long 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
4541248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
4551248952aSSean Bruno 	uint64_t ifr_cpu_exec_count[256];
4561248952aSSean Bruno #endif
4574c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4584c7070dbSScott Long 
45995246abbSSean Bruno typedef struct if_rxsd {
46095246abbSSean Bruno 	caddr_t *ifsd_cl;
46195246abbSSean Bruno 	struct mbuf **ifsd_m;
46295246abbSSean Bruno 	iflib_fl_t ifsd_fl;
46395246abbSSean Bruno 	qidx_t ifsd_cidx;
46495246abbSSean Bruno } *if_rxsd_t;
46595246abbSSean Bruno 
46695246abbSSean Bruno /* multiple of word size */
46795246abbSSean Bruno #ifdef __LP64__
468ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	6
46995246abbSSean Bruno #define RXD_INFO_SIZE	5
47095246abbSSean Bruno #define PKT_TYPE uint64_t
47195246abbSSean Bruno #else
472ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	11
47395246abbSSean Bruno #define RXD_INFO_SIZE	8
47495246abbSSean Bruno #define PKT_TYPE uint32_t
47595246abbSSean Bruno #endif
47695246abbSSean Bruno #define PKT_LOOP_BOUND  ((PKT_INFO_SIZE/3)*3)
47795246abbSSean Bruno #define RXD_LOOP_BOUND  ((RXD_INFO_SIZE/4)*4)
47895246abbSSean Bruno 
47995246abbSSean Bruno typedef struct if_pkt_info_pad {
48095246abbSSean Bruno 	PKT_TYPE pkt_val[PKT_INFO_SIZE];
48195246abbSSean Bruno } *if_pkt_info_pad_t;
48295246abbSSean Bruno typedef struct if_rxd_info_pad {
48395246abbSSean Bruno 	PKT_TYPE rxd_val[RXD_INFO_SIZE];
48495246abbSSean Bruno } *if_rxd_info_pad_t;
48595246abbSSean Bruno 
48695246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info));
48795246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info));
48895246abbSSean Bruno 
48995246abbSSean Bruno 
49095246abbSSean Bruno static inline void
49195246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi)
49295246abbSSean Bruno {
49395246abbSSean Bruno 	if_pkt_info_pad_t pi_pad;
49495246abbSSean Bruno 
49595246abbSSean Bruno 	pi_pad = (if_pkt_info_pad_t)pi;
49695246abbSSean Bruno 	pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0;
49795246abbSSean Bruno 	pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0;
49895246abbSSean Bruno #ifndef __LP64__
499ab2e3f79SStephen Hurd 	pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0;
500ab2e3f79SStephen Hurd 	pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0;
50195246abbSSean Bruno #endif
50295246abbSSean Bruno }
50395246abbSSean Bruno 
50495246abbSSean Bruno static inline void
50595246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri)
50695246abbSSean Bruno {
50795246abbSSean Bruno 	if_rxd_info_pad_t ri_pad;
50895246abbSSean Bruno 	int i;
50995246abbSSean Bruno 
51095246abbSSean Bruno 	ri_pad = (if_rxd_info_pad_t)ri;
51195246abbSSean Bruno 	for (i = 0; i < RXD_LOOP_BOUND; i += 4) {
51295246abbSSean Bruno 		ri_pad->rxd_val[i] = 0;
51395246abbSSean Bruno 		ri_pad->rxd_val[i+1] = 0;
51495246abbSSean Bruno 		ri_pad->rxd_val[i+2] = 0;
51595246abbSSean Bruno 		ri_pad->rxd_val[i+3] = 0;
51695246abbSSean Bruno 	}
51795246abbSSean Bruno #ifdef __LP64__
51895246abbSSean Bruno 	ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0;
51995246abbSSean Bruno #endif
52095246abbSSean Bruno }
52195246abbSSean Bruno 
5224c7070dbSScott Long /*
5234c7070dbSScott Long  * Only allow a single packet to take up most 1/nth of the tx ring
5244c7070dbSScott Long  */
5254c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12
5264c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1
5274c7070dbSScott Long 
5284c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
5294c7070dbSScott Long 
530ab2e3f79SStephen Hurd #define CTX_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_mtx, _name, "iflib ctx lock", MTX_DEF)
5314c7070dbSScott Long 
532ab2e3f79SStephen Hurd #define CTX_LOCK(ctx) mtx_lock(&(ctx)->ifc_mtx)
533ab2e3f79SStephen Hurd #define CTX_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_mtx)
534ab2e3f79SStephen Hurd #define CTX_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_mtx)
535ab2e3f79SStephen Hurd 
5364c7070dbSScott Long 
5374c7070dbSScott Long #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
5384c7070dbSScott Long #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
5394c7070dbSScott Long 
5404c7070dbSScott Long 
5414c7070dbSScott Long /* Our boot-time initialization hook */
5424c7070dbSScott Long static int	iflib_module_event_handler(module_t, int, void *);
5434c7070dbSScott Long 
5444c7070dbSScott Long static moduledata_t iflib_moduledata = {
5454c7070dbSScott Long 	"iflib",
5464c7070dbSScott Long 	iflib_module_event_handler,
5474c7070dbSScott Long 	NULL
5484c7070dbSScott Long };
5494c7070dbSScott Long 
5504c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
5514c7070dbSScott Long MODULE_VERSION(iflib, 1);
5524c7070dbSScott Long 
5534c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1);
5544c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1);
5554c7070dbSScott Long 
556ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
557ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
558ab2e3f79SStephen Hurd 
5594c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS
5604c7070dbSScott Long #ifdef INVARIANTS
5614c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1
5624c7070dbSScott Long #else
5634c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0
5644c7070dbSScott Long #endif /* !INVARIANTS */
5654c7070dbSScott Long #endif
5664c7070dbSScott Long 
567ab2e3f79SStephen Hurd static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0,
568ab2e3f79SStephen Hurd                    "iflib driver parameters");
569ab2e3f79SStephen Hurd 
5704c7070dbSScott Long /*
5714c7070dbSScott Long  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
5724c7070dbSScott Long  */
5734c7070dbSScott Long static int iflib_min_tx_latency = 0;
5744c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
575da69b8f9SSean Bruno 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
57695246abbSSean Bruno static int iflib_no_tx_batch = 0;
57795246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
57895246abbSSean Bruno 		   &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput");
5794c7070dbSScott Long 
5804c7070dbSScott Long 
5814c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS
5824c7070dbSScott Long 
5834c7070dbSScott Long static int iflib_tx_seen;
5844c7070dbSScott Long static int iflib_tx_sent;
5854c7070dbSScott Long static int iflib_tx_encap;
5864c7070dbSScott Long static int iflib_rx_allocs;
5874c7070dbSScott Long static int iflib_fl_refills;
5884c7070dbSScott Long static int iflib_fl_refills_large;
5894c7070dbSScott Long static int iflib_tx_frees;
5904c7070dbSScott Long 
5914c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
5924c7070dbSScott Long 		   &iflib_tx_seen, 0, "# tx mbufs seen");
5934c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
5944c7070dbSScott Long 		   &iflib_tx_sent, 0, "# tx mbufs sent");
5954c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
5964c7070dbSScott Long 		   &iflib_tx_encap, 0, "# tx mbufs encapped");
5974c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
5984c7070dbSScott Long 		   &iflib_tx_frees, 0, "# tx frees");
5994c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
6004c7070dbSScott Long 		   &iflib_rx_allocs, 0, "# rx allocations");
6014c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
6024c7070dbSScott Long 		   &iflib_fl_refills, 0, "# refills");
6034c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
6044c7070dbSScott Long 		   &iflib_fl_refills_large, 0, "# large refills");
6054c7070dbSScott Long 
6064c7070dbSScott Long 
6074c7070dbSScott Long static int iflib_txq_drain_flushing;
6084c7070dbSScott Long static int iflib_txq_drain_oactive;
6094c7070dbSScott Long static int iflib_txq_drain_notready;
6104c7070dbSScott Long static int iflib_txq_drain_encapfail;
6114c7070dbSScott Long 
6124c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
6134c7070dbSScott Long 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
6144c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
6154c7070dbSScott Long 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
6164c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
6174c7070dbSScott Long 		   &iflib_txq_drain_notready, 0, "# drain notready");
6184c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_encapfail, CTLFLAG_RD,
6194c7070dbSScott Long 		   &iflib_txq_drain_encapfail, 0, "# drain encap fails");
6204c7070dbSScott Long 
6214c7070dbSScott Long 
6224c7070dbSScott Long static int iflib_encap_load_mbuf_fail;
6234c7070dbSScott Long static int iflib_encap_txq_avail_fail;
6244c7070dbSScott Long static int iflib_encap_txd_encap_fail;
6254c7070dbSScott Long 
6264c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
6274c7070dbSScott Long 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
6284c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
6294c7070dbSScott Long 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
6304c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
6314c7070dbSScott Long 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
6324c7070dbSScott Long 
6334c7070dbSScott Long static int iflib_task_fn_rxs;
6344c7070dbSScott Long static int iflib_rx_intr_enables;
6354c7070dbSScott Long static int iflib_fast_intrs;
6364c7070dbSScott Long static int iflib_intr_link;
6374c7070dbSScott Long static int iflib_intr_msix;
6384c7070dbSScott Long static int iflib_rx_unavail;
6394c7070dbSScott Long static int iflib_rx_ctx_inactive;
6404c7070dbSScott Long static int iflib_rx_zero_len;
6414c7070dbSScott Long static int iflib_rx_if_input;
6424c7070dbSScott Long static int iflib_rx_mbuf_null;
6434c7070dbSScott Long static int iflib_rxd_flush;
6444c7070dbSScott Long 
6454c7070dbSScott Long static int iflib_verbose_debug;
6464c7070dbSScott Long 
6474c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_link, CTLFLAG_RD,
6484c7070dbSScott Long 		   &iflib_intr_link, 0, "# intr link calls");
6494c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_msix, CTLFLAG_RD,
6504c7070dbSScott Long 		   &iflib_intr_msix, 0, "# intr msix calls");
6514c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
6524c7070dbSScott Long 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
6534c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
6544c7070dbSScott Long 		   &iflib_rx_intr_enables, 0, "# rx intr enables");
6554c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
6564c7070dbSScott Long 		   &iflib_fast_intrs, 0, "# fast_intr calls");
6574c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
6584c7070dbSScott Long 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
6594c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
6604c7070dbSScott Long 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
6614c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_zero_len, CTLFLAG_RD,
6624c7070dbSScott Long 		   &iflib_rx_zero_len, 0, "# times rxeof saw zero len mbuf");
6634c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
6644c7070dbSScott Long 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
6654c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD,
6664c7070dbSScott Long 		   &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf");
6674c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
6684c7070dbSScott Long 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
6694c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
6704c7070dbSScott Long 		   &iflib_verbose_debug, 0, "enable verbose debugging");
6714c7070dbSScott Long 
6724c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
673da69b8f9SSean Bruno static void
674da69b8f9SSean Bruno iflib_debug_reset(void)
675da69b8f9SSean Bruno {
676da69b8f9SSean Bruno 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
677da69b8f9SSean Bruno 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
678da69b8f9SSean Bruno 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
679da69b8f9SSean Bruno 		iflib_txq_drain_notready = iflib_txq_drain_encapfail =
680da69b8f9SSean Bruno 		iflib_encap_load_mbuf_fail = iflib_encap_txq_avail_fail =
681da69b8f9SSean Bruno 		iflib_encap_txd_encap_fail = iflib_task_fn_rxs = iflib_rx_intr_enables =
682da69b8f9SSean Bruno 		iflib_fast_intrs = iflib_intr_link = iflib_intr_msix = iflib_rx_unavail =
683da69b8f9SSean Bruno 		iflib_rx_ctx_inactive = iflib_rx_zero_len = iflib_rx_if_input =
684da69b8f9SSean Bruno 		iflib_rx_mbuf_null = iflib_rxd_flush = 0;
685da69b8f9SSean Bruno }
6864c7070dbSScott Long 
6874c7070dbSScott Long #else
6884c7070dbSScott Long #define DBG_COUNTER_INC(name)
689da69b8f9SSean Bruno static void iflib_debug_reset(void) {}
6904c7070dbSScott Long #endif
6914c7070dbSScott Long 
6924c7070dbSScott Long 
6934c7070dbSScott Long 
6944c7070dbSScott Long #define IFLIB_DEBUG 0
6954c7070dbSScott Long 
6964c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx);
6974c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx);
6984c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx);
6994c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
70095246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
7014c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx);
7024c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx);
7034c7070dbSScott Long static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, char *str);
7044c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
7054c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
7064c7070dbSScott Long static int iflib_register(if_ctx_t);
7074c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx);
7084c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
7094c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx);
710da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq);
7111248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx);
71295246abbSSean Bruno static void iflib_stop(if_ctx_t ctx);
71395246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx);
71495246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
71595246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m);
71695246abbSSean Bruno #endif
7174c7070dbSScott Long 
7184c7070dbSScott Long #ifdef DEV_NETMAP
7194c7070dbSScott Long #include <sys/selinfo.h>
7204c7070dbSScott Long #include <net/netmap.h>
7214c7070dbSScott Long #include <dev/netmap/netmap_kern.h>
7224c7070dbSScott Long 
7234c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1);
7244c7070dbSScott Long 
7254c7070dbSScott Long /*
7264c7070dbSScott Long  * device-specific sysctl variables:
7274c7070dbSScott Long  *
72891d546a0SConrad Meyer  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
7294c7070dbSScott Long  *	During regular operations the CRC is stripped, but on some
7304c7070dbSScott Long  *	hardware reception of frames not multiple of 64 is slower,
7314c7070dbSScott Long  *	so using crcstrip=0 helps in benchmarks.
7324c7070dbSScott Long  *
73391d546a0SConrad Meyer  * iflib_rx_miss, iflib_rx_miss_bufs:
7344c7070dbSScott Long  *	count packets that might be missed due to lost interrupts.
7354c7070dbSScott Long  */
7364c7070dbSScott Long SYSCTL_DECL(_dev_netmap);
7374c7070dbSScott Long /*
7384c7070dbSScott Long  * The xl driver by default strips CRCs and we do not override it.
7394c7070dbSScott Long  */
7404c7070dbSScott Long 
7414c7070dbSScott Long int iflib_crcstrip = 1;
7424c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
7434c7070dbSScott Long     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames");
7444c7070dbSScott Long 
7454c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs;
7464c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
7474c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr");
74891d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
7494c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs");
7504c7070dbSScott Long 
7514c7070dbSScott Long /*
7524c7070dbSScott Long  * Register/unregister. We are already under netmap lock.
7534c7070dbSScott Long  * Only called on the first register or the last unregister.
7544c7070dbSScott Long  */
7554c7070dbSScott Long static int
7564c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff)
7574c7070dbSScott Long {
7584c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
7594c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
76095246abbSSean Bruno 	int status;
7614c7070dbSScott Long 
7624c7070dbSScott Long 	CTX_LOCK(ctx);
7634c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
7644c7070dbSScott Long 
7654c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
7664c7070dbSScott Long 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
7674c7070dbSScott Long 
7684c7070dbSScott Long 	if (!CTX_IS_VF(ctx))
7691248952aSSean Bruno 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
7704c7070dbSScott Long 
7714c7070dbSScott Long 	/* enable or disable flags and callbacks in na and ifp */
7724c7070dbSScott Long 	if (onoff) {
7734c7070dbSScott Long 		nm_set_native_flags(na);
7744c7070dbSScott Long 	} else {
7754c7070dbSScott Long 		nm_clear_native_flags(na);
7764c7070dbSScott Long 	}
77795246abbSSean Bruno 	iflib_stop(ctx);
77895246abbSSean Bruno 	iflib_init_locked(ctx);
7791248952aSSean Bruno 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
78095246abbSSean Bruno 	status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1;
78195246abbSSean Bruno 	if (status)
78295246abbSSean Bruno 		nm_clear_native_flags(na);
7834c7070dbSScott Long 	CTX_UNLOCK(ctx);
78495246abbSSean Bruno 	return (status);
7854c7070dbSScott Long }
7864c7070dbSScott Long 
7874c7070dbSScott Long /*
7884c7070dbSScott Long  * Reconcile kernel and user view of the transmit ring.
7894c7070dbSScott Long  *
7904c7070dbSScott Long  * All information is in the kring.
7914c7070dbSScott Long  * Userspace wants to send packets up to the one before kring->rhead,
7924c7070dbSScott Long  * kernel knows kring->nr_hwcur is the first unsent packet.
7934c7070dbSScott Long  *
7944c7070dbSScott Long  * Here we push packets out (as many as possible), and possibly
7954c7070dbSScott Long  * reclaim buffers from previously completed transmission.
7964c7070dbSScott Long  *
7974c7070dbSScott Long  * The caller (netmap) guarantees that there is only one instance
7984c7070dbSScott Long  * running at any time. Any interference with other driver
7994c7070dbSScott Long  * methods should be handled by the individual drivers.
8004c7070dbSScott Long  */
8014c7070dbSScott Long static int
8024c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags)
8034c7070dbSScott Long {
8044c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
8054c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
8064c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
8074c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
8084c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
8094c7070dbSScott Long 	u_int n;
8104c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
8114c7070dbSScott Long 	u_int const head = kring->rhead;
8124c7070dbSScott Long 	struct if_pkt_info pi;
8134c7070dbSScott Long 
8144c7070dbSScott Long 	/*
8154c7070dbSScott Long 	 * interrupts on every tx packet are expensive so request
8164c7070dbSScott Long 	 * them every half ring, or where NS_REPORT is set
8174c7070dbSScott Long 	 */
8184c7070dbSScott Long 	u_int report_frequency = kring->nkr_num_slots >> 1;
8194c7070dbSScott Long 	/* device-specific */
8204c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
8214c7070dbSScott Long 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
8224c7070dbSScott Long 
82395246abbSSean Bruno 	if (txq->ift_sds.ifsd_map)
8244c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
8254c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
8264c7070dbSScott Long 
8274c7070dbSScott Long 
8284c7070dbSScott Long 	/*
8294c7070dbSScott Long 	 * First part: process new packets to send.
8304c7070dbSScott Long 	 * nm_i is the current index in the netmap ring,
8314c7070dbSScott Long 	 * nic_i is the corresponding index in the NIC ring.
8324c7070dbSScott Long 	 *
8334c7070dbSScott Long 	 * If we have packets to send (nm_i != head)
8344c7070dbSScott Long 	 * iterate over the netmap ring, fetch length and update
8354c7070dbSScott Long 	 * the corresponding slot in the NIC ring. Some drivers also
8364c7070dbSScott Long 	 * need to update the buffer's physical address in the NIC slot
8374c7070dbSScott Long 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
8384c7070dbSScott Long 	 *
8394c7070dbSScott Long 	 * The netmap_reload_map() calls is especially expensive,
8404c7070dbSScott Long 	 * even when (as in this case) the tag is 0, so do only
8414c7070dbSScott Long 	 * when the buffer has actually changed.
8424c7070dbSScott Long 	 *
8434c7070dbSScott Long 	 * If possible do not set the report/intr bit on all slots,
8444c7070dbSScott Long 	 * but only a few times per ring or when NS_REPORT is set.
8454c7070dbSScott Long 	 *
8464c7070dbSScott Long 	 * Finally, on 10G and faster drivers, it might be useful
8474c7070dbSScott Long 	 * to prefetch the next slot and txr entry.
8484c7070dbSScott Long 	 */
8494c7070dbSScott Long 
8504c7070dbSScott Long 	nm_i = kring->nr_hwcur;
85195246abbSSean Bruno 	pkt_info_zero(&pi);
85295246abbSSean Bruno 	pi.ipi_segs = txq->ift_segs;
85395246abbSSean Bruno 	pi.ipi_qsidx = kring->ring_id;
8544c7070dbSScott Long 	if (nm_i != head) {	/* we have new packets to send */
8554c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
8564c7070dbSScott Long 
8574c7070dbSScott Long 		__builtin_prefetch(&ring->slot[nm_i]);
8584c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
85995246abbSSean Bruno 		if (txq->ift_sds.ifsd_map)
8604c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
8614c7070dbSScott Long 
8624c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
8634c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
8644c7070dbSScott Long 			u_int len = slot->len;
8650a1b74a3SSean Bruno 			uint64_t paddr;
8664c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
8674c7070dbSScott Long 			int flags = (slot->flags & NS_REPORT ||
8684c7070dbSScott Long 				nic_i == 0 || nic_i == report_frequency) ?
8694c7070dbSScott Long 				IPI_TX_INTR : 0;
8704c7070dbSScott Long 
8714c7070dbSScott Long 			/* device-specific */
87295246abbSSean Bruno 			pi.ipi_len = len;
87395246abbSSean Bruno 			pi.ipi_segs[0].ds_addr = paddr;
87495246abbSSean Bruno 			pi.ipi_segs[0].ds_len = len;
87595246abbSSean Bruno 			pi.ipi_nsegs = 1;
87695246abbSSean Bruno 			pi.ipi_ndescs = 0;
8774c7070dbSScott Long 			pi.ipi_pidx = nic_i;
8784c7070dbSScott Long 			pi.ipi_flags = flags;
8794c7070dbSScott Long 
8804c7070dbSScott Long 			/* Fill the slot in the NIC ring. */
8814c7070dbSScott Long 			ctx->isc_txd_encap(ctx->ifc_softc, &pi);
8824c7070dbSScott Long 
8834c7070dbSScott Long 			/* prefetch for next round */
8844c7070dbSScott Long 			__builtin_prefetch(&ring->slot[nm_i + 1]);
8854c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
88695246abbSSean Bruno 			if (txq->ift_sds.ifsd_map) {
8874c7070dbSScott Long 				__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
8884c7070dbSScott Long 
8894c7070dbSScott Long 				NM_CHECK_ADDR_LEN(na, addr, len);
8904c7070dbSScott Long 
8914c7070dbSScott Long 				if (slot->flags & NS_BUF_CHANGED) {
8924c7070dbSScott Long 					/* buffer has changed, reload map */
8934c7070dbSScott Long 					netmap_reload_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[nic_i], addr);
8944c7070dbSScott Long 				}
8954c7070dbSScott Long 				/* make sure changes to the buffer are synced */
8964c7070dbSScott Long 				bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_sds.ifsd_map[nic_i],
8974c7070dbSScott Long 						BUS_DMASYNC_PREWRITE);
89895246abbSSean Bruno 			}
89995246abbSSean Bruno 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
9004c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
9014c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
9024c7070dbSScott Long 		}
9034c7070dbSScott Long 		kring->nr_hwcur = head;
9044c7070dbSScott Long 
9054c7070dbSScott Long 		/* synchronize the NIC ring */
90695246abbSSean Bruno 		if (txq->ift_sds.ifsd_map)
9074c7070dbSScott Long 			bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
9084c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
9094c7070dbSScott Long 
9104c7070dbSScott Long 		/* (re)start the tx unit up to slot nic_i (excluded) */
9114c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
9124c7070dbSScott Long 	}
9134c7070dbSScott Long 
9144c7070dbSScott Long 	/*
9154c7070dbSScott Long 	 * Second part: reclaim buffers for completed transmissions.
9164c7070dbSScott Long 	 */
9174c7070dbSScott Long 	if (iflib_tx_credits_update(ctx, txq)) {
9184c7070dbSScott Long 		/* some tx completed, increment avail */
9194c7070dbSScott Long 		nic_i = txq->ift_cidx_processed;
9204c7070dbSScott Long 		kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
9214c7070dbSScott Long 	}
9224c7070dbSScott Long 	return (0);
9234c7070dbSScott Long }
9244c7070dbSScott Long 
9254c7070dbSScott Long /*
9264c7070dbSScott Long  * Reconcile kernel and user view of the receive ring.
9274c7070dbSScott Long  * Same as for the txsync, this routine must be efficient.
9284c7070dbSScott Long  * The caller guarantees a single invocations, but races against
9294c7070dbSScott Long  * the rest of the driver should be handled here.
9304c7070dbSScott Long  *
9314c7070dbSScott Long  * On call, kring->rhead is the first packet that userspace wants
9324c7070dbSScott Long  * to keep, and kring->rcur is the wakeup point.
9334c7070dbSScott Long  * The kernel has previously reported packets up to kring->rtail.
9344c7070dbSScott Long  *
9354c7070dbSScott Long  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
9364c7070dbSScott Long  * of whether or not we received an interrupt.
9374c7070dbSScott Long  */
9384c7070dbSScott Long static int
9394c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
9404c7070dbSScott Long {
9414c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
9424c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
94395246abbSSean Bruno 	uint32_t nm_i;	/* index into the netmap ring */
944ab2e3f79SStephen Hurd 	uint32_t nic_i, nic_i_start;	/* index into the NIC ring */
9454c7070dbSScott Long 	u_int i, n;
9464c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
9474c7070dbSScott Long 	u_int const head = kring->rhead;
9484c7070dbSScott Long 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
949ab2e3f79SStephen Hurd 	struct if_rxd_info ri;
950ab2e3f79SStephen Hurd 	struct if_rxd_update iru;
95195246abbSSean Bruno 
95295246abbSSean Bruno 	struct ifnet *ifp = na->ifp;
9534c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
9544c7070dbSScott Long 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
9554c7070dbSScott Long 	iflib_fl_t fl = rxq->ifr_fl;
9564c7070dbSScott Long 	if (head > lim)
9574c7070dbSScott Long 		return netmap_ring_reinit(kring);
9584c7070dbSScott Long 
9594c7070dbSScott Long 	/* XXX check sync modes */
96095246abbSSean Bruno 	for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) {
96195246abbSSean Bruno 		if (fl->ifl_sds.ifsd_map == NULL)
96295246abbSSean Bruno 			continue;
9634c7070dbSScott Long 		bus_dmamap_sync(rxq->ifr_fl[i].ifl_desc_tag, fl->ifl_ifdi->idi_map,
9644c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
96595246abbSSean Bruno 	}
9664c7070dbSScott Long 	/*
9674c7070dbSScott Long 	 * First part: import newly received packets.
9684c7070dbSScott Long 	 *
9694c7070dbSScott Long 	 * nm_i is the index of the next free slot in the netmap ring,
9704c7070dbSScott Long 	 * nic_i is the index of the next received packet in the NIC ring,
9714c7070dbSScott Long 	 * and they may differ in case if_init() has been called while
9724c7070dbSScott Long 	 * in netmap mode. For the receive ring we have
9734c7070dbSScott Long 	 *
9744c7070dbSScott Long 	 *	nic_i = rxr->next_check;
9754c7070dbSScott Long 	 *	nm_i = kring->nr_hwtail (previous)
9764c7070dbSScott Long 	 * and
9774c7070dbSScott Long 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
9784c7070dbSScott Long 	 *
9794c7070dbSScott Long 	 * rxr->next_check is set to 0 on a ring reinit
9804c7070dbSScott Long 	 */
9814c7070dbSScott Long 	if (netmap_no_pendintr || force_update) {
9824c7070dbSScott Long 		int crclen = iflib_crcstrip ? 0 : 4;
9834c7070dbSScott Long 		int error, avail;
9844c7070dbSScott Long 		uint16_t slot_flags = kring->nkr_slot_flags;
9854c7070dbSScott Long 
9864c7070dbSScott Long 		for (fl = rxq->ifr_fl, i = 0; i < rxq->ifr_nfl; i++, fl++) {
9874c7070dbSScott Long 			nic_i = fl->ifl_cidx;
9884c7070dbSScott Long 			nm_i = netmap_idx_n2k(kring, nic_i);
98995246abbSSean Bruno 			avail = iflib_rxd_avail(ctx, rxq, nic_i, USHRT_MAX);
9904c7070dbSScott Long 			for (n = 0; avail > 0; n++, avail--) {
991ab2e3f79SStephen Hurd 				rxd_info_zero(&ri);
992ab2e3f79SStephen Hurd 				ri.iri_frags = rxq->ifr_frags;
993ab2e3f79SStephen Hurd 				ri.iri_qsidx = kring->ring_id;
994ab2e3f79SStephen Hurd 				ri.iri_ifp = ctx->ifc_ifp;
995ab2e3f79SStephen Hurd 				ri.iri_cidx = nic_i;
99695246abbSSean Bruno 
997ab2e3f79SStephen Hurd 				error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
998ab2e3f79SStephen Hurd 				ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen;
9994c7070dbSScott Long 				ring->slot[nm_i].flags = slot_flags;
100095246abbSSean Bruno 				if (fl->ifl_sds.ifsd_map)
10014c7070dbSScott Long 					bus_dmamap_sync(fl->ifl_ifdi->idi_tag,
1002e035717eSSean Bruno 							fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
10034c7070dbSScott Long 				nm_i = nm_next(nm_i, lim);
10044c7070dbSScott Long 				nic_i = nm_next(nic_i, lim);
10054c7070dbSScott Long 			}
10064c7070dbSScott Long 			if (n) { /* update the state variables */
10074c7070dbSScott Long 				if (netmap_no_pendintr && !force_update) {
10084c7070dbSScott Long 					/* diagnostics */
10094c7070dbSScott Long 					iflib_rx_miss ++;
10104c7070dbSScott Long 					iflib_rx_miss_bufs += n;
10114c7070dbSScott Long 				}
10124c7070dbSScott Long 				fl->ifl_cidx = nic_i;
10134c7070dbSScott Long 				kring->nr_hwtail = nm_i;
10144c7070dbSScott Long 			}
10154c7070dbSScott Long 			kring->nr_kflags &= ~NKR_PENDINTR;
10164c7070dbSScott Long 		}
10174c7070dbSScott Long 	}
10184c7070dbSScott Long 	/*
10194c7070dbSScott Long 	 * Second part: skip past packets that userspace has released.
10204c7070dbSScott Long 	 * (kring->nr_hwcur to head excluded),
10214c7070dbSScott Long 	 * and make the buffers available for reception.
10224c7070dbSScott Long 	 * As usual nm_i is the index in the netmap ring,
10234c7070dbSScott Long 	 * nic_i is the index in the NIC ring, and
10244c7070dbSScott Long 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
10254c7070dbSScott Long 	 */
10264c7070dbSScott Long 	/* XXX not sure how this will work with multiple free lists */
10274c7070dbSScott Long 	nm_i = kring->nr_hwcur;
102895246abbSSean Bruno 	if (nm_i == head)
102995246abbSSean Bruno 		return (0);
103095246abbSSean Bruno 
1031ab2e3f79SStephen Hurd 	iru.iru_paddrs = fl->ifl_bus_addrs;
1032ab2e3f79SStephen Hurd 	iru.iru_vaddrs = &fl->ifl_vm_addrs[0];
1033ab2e3f79SStephen Hurd 	iru.iru_idxs = fl->ifl_rxd_idxs;
1034ab2e3f79SStephen Hurd 	iru.iru_qsidx = rxq->ifr_id;
1035ab2e3f79SStephen Hurd 	iru.iru_buf_size = fl->ifl_buf_size;
1036ab2e3f79SStephen Hurd 	iru.iru_flidx = fl->ifl_id;
1037ab2e3f79SStephen Hurd 	nic_i_start = nic_i = netmap_idx_k2n(kring, nm_i);
1038ab2e3f79SStephen Hurd 	for (i = 0; nm_i != head; i++) {
1039ab2e3f79SStephen Hurd 		struct netmap_slot *slot = &ring->slot[nm_i];
1040ab2e3f79SStephen Hurd 		void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[i]);
1041ab2e3f79SStephen Hurd 
1042ab2e3f79SStephen Hurd 		if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
1043ab2e3f79SStephen Hurd 			goto ring_reset;
1044ab2e3f79SStephen Hurd 
1045ab2e3f79SStephen Hurd 		fl->ifl_vm_addrs[i] = addr;
1046ab2e3f79SStephen Hurd 		if (fl->ifl_sds.ifsd_map && (slot->flags & NS_BUF_CHANGED)) {
1047ab2e3f79SStephen Hurd 			/* buffer has changed, reload map */
1048ab2e3f79SStephen Hurd 			netmap_reload_map(na, fl->ifl_ifdi->idi_tag, fl->ifl_sds.ifsd_map[nic_i], addr);
1049ab2e3f79SStephen Hurd 		}
1050ab2e3f79SStephen Hurd 		slot->flags &= ~NS_BUF_CHANGED;
1051ab2e3f79SStephen Hurd 
1052ab2e3f79SStephen Hurd 		nm_i = nm_next(nm_i, lim);
1053ab2e3f79SStephen Hurd 		fl->ifl_rxd_idxs[i] = nic_i = nm_next(nic_i, lim);
1054ab2e3f79SStephen Hurd 		if (nm_i != head && i < IFLIB_MAX_RX_REFRESH)
1055ab2e3f79SStephen Hurd 			continue;
1056ab2e3f79SStephen Hurd 
1057ab2e3f79SStephen Hurd 		iru.iru_pidx = nic_i_start;
1058ab2e3f79SStephen Hurd 		iru.iru_count = i;
1059ab2e3f79SStephen Hurd 		i = 0;
1060ab2e3f79SStephen Hurd 		ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
1061ab2e3f79SStephen Hurd 		if (fl->ifl_sds.ifsd_map == NULL) {
1062ab2e3f79SStephen Hurd 			nic_i_start = nic_i;
1063ab2e3f79SStephen Hurd 			continue;
1064ab2e3f79SStephen Hurd 		}
1065ab2e3f79SStephen Hurd 		nic_i = nic_i_start;
1066ab2e3f79SStephen Hurd 		for (n = 0; n < iru.iru_count; n++) {
1067ab2e3f79SStephen Hurd 			bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_sds.ifsd_map[nic_i],
1068ab2e3f79SStephen Hurd 					BUS_DMASYNC_PREREAD);
1069ab2e3f79SStephen Hurd 			nic_i = nm_next(nic_i, lim);
1070ab2e3f79SStephen Hurd 		}
1071ab2e3f79SStephen Hurd 		nic_i_start = nic_i;
1072ab2e3f79SStephen Hurd 	}
1073ab2e3f79SStephen Hurd 	kring->nr_hwcur = head;
1074ab2e3f79SStephen Hurd 
1075ab2e3f79SStephen Hurd 	if (fl->ifl_sds.ifsd_map)
1076ab2e3f79SStephen Hurd 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
1077ab2e3f79SStephen Hurd 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1078ab2e3f79SStephen Hurd 	/*
1079ab2e3f79SStephen Hurd 	 * IMPORTANT: we must leave one free slot in the ring,
1080ab2e3f79SStephen Hurd 	 * so move nic_i back by one unit
1081ab2e3f79SStephen Hurd 	 */
1082ab2e3f79SStephen Hurd 	nic_i = nm_prev(nic_i, lim);
1083ab2e3f79SStephen Hurd 	ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
1084ab2e3f79SStephen Hurd 	return 0;
1085ab2e3f79SStephen Hurd 
1086ab2e3f79SStephen Hurd ring_reset:
1087ab2e3f79SStephen Hurd 	return netmap_ring_reinit(kring);
10884c7070dbSScott Long }
10894c7070dbSScott Long 
109095246abbSSean Bruno static void
109195246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff)
109295246abbSSean Bruno {
109395246abbSSean Bruno 	struct ifnet *ifp = na->ifp;
109495246abbSSean Bruno 	if_ctx_t ctx = ifp->if_softc;
109595246abbSSean Bruno 
1096ab2e3f79SStephen Hurd 	CTX_LOCK(ctx);
109795246abbSSean Bruno 	if (onoff) {
109895246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
109995246abbSSean Bruno 	} else {
110095246abbSSean Bruno 		IFDI_INTR_DISABLE(ctx);
110195246abbSSean Bruno 	}
1102ab2e3f79SStephen Hurd 	CTX_UNLOCK(ctx);
110395246abbSSean Bruno }
110495246abbSSean Bruno 
110595246abbSSean Bruno 
11064c7070dbSScott Long static int
11074c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx)
11084c7070dbSScott Long {
11094c7070dbSScott Long 	struct netmap_adapter na;
111023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
11114c7070dbSScott Long 
11124c7070dbSScott Long 	bzero(&na, sizeof(na));
11134c7070dbSScott Long 
11144c7070dbSScott Long 	na.ifp = ctx->ifc_ifp;
11154c7070dbSScott Long 	na.na_flags = NAF_BDG_MAYSLEEP;
11164c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
11174c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
11184c7070dbSScott Long 
111923ac9029SStephen Hurd 	na.num_tx_desc = scctx->isc_ntxd[0];
112023ac9029SStephen Hurd 	na.num_rx_desc = scctx->isc_nrxd[0];
11214c7070dbSScott Long 	na.nm_txsync = iflib_netmap_txsync;
11224c7070dbSScott Long 	na.nm_rxsync = iflib_netmap_rxsync;
11234c7070dbSScott Long 	na.nm_register = iflib_netmap_register;
112495246abbSSean Bruno 	na.nm_intr = iflib_netmap_intr;
11254c7070dbSScott Long 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
11264c7070dbSScott Long 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
11274c7070dbSScott Long 	return (netmap_attach(&na));
11284c7070dbSScott Long }
11294c7070dbSScott Long 
11304c7070dbSScott Long static void
11314c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
11324c7070dbSScott Long {
11334c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
11344c7070dbSScott Long 	struct netmap_slot *slot;
11354c7070dbSScott Long 
11364c7070dbSScott Long 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1137e099b90bSPedro F. Giffuni 	if (slot == NULL)
11384c7070dbSScott Long 		return;
113995246abbSSean Bruno 	if (txq->ift_sds.ifsd_map == NULL)
114095246abbSSean Bruno 		return;
11414c7070dbSScott Long 
114223ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
11434c7070dbSScott Long 
11444c7070dbSScott Long 		/*
11454c7070dbSScott Long 		 * In netmap mode, set the map for the packet buffer.
11464c7070dbSScott Long 		 * NOTE: Some drivers (not this one) also need to set
11474c7070dbSScott Long 		 * the physical buffer address in the NIC ring.
11484c7070dbSScott Long 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
11494c7070dbSScott Long 		 * netmap slot index, si
11504c7070dbSScott Long 		 */
11514c7070dbSScott Long 		int si = netmap_idx_n2k(&na->tx_rings[txq->ift_id], i);
11524c7070dbSScott Long 		netmap_load_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[i], NMB(na, slot + si));
11534c7070dbSScott Long 	}
11544c7070dbSScott Long }
11554c7070dbSScott Long static void
11564c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
11574c7070dbSScott Long {
11584c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
11594c7070dbSScott Long 	struct netmap_slot *slot;
1160ab2e3f79SStephen Hurd 	struct if_rxd_update iru;
1161ab2e3f79SStephen Hurd 	iflib_fl_t fl;
1162ab2e3f79SStephen Hurd 	bus_dmamap_t *map;
1163ab2e3f79SStephen Hurd 	int nrxd;
1164ab2e3f79SStephen Hurd 	uint32_t i, j, pidx_start;
11654c7070dbSScott Long 
11664c7070dbSScott Long 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1167e099b90bSPedro F. Giffuni 	if (slot == NULL)
11684c7070dbSScott Long 		return;
1169ab2e3f79SStephen Hurd 	fl = &rxq->ifr_fl[0];
1170ab2e3f79SStephen Hurd 	map = fl->ifl_sds.ifsd_map;
1171ab2e3f79SStephen Hurd 	nrxd = ctx->ifc_softc_ctx.isc_nrxd[0];
1172ab2e3f79SStephen Hurd 	iru.iru_paddrs = fl->ifl_bus_addrs;
1173ab2e3f79SStephen Hurd 	iru.iru_vaddrs = &fl->ifl_vm_addrs[0];
1174ab2e3f79SStephen Hurd 	iru.iru_idxs = fl->ifl_rxd_idxs;
1175ab2e3f79SStephen Hurd 	iru.iru_qsidx = rxq->ifr_id;
1176ab2e3f79SStephen Hurd 	iru.iru_buf_size = rxq->ifr_fl[0].ifl_buf_size;
1177ab2e3f79SStephen Hurd 	iru.iru_flidx = 0;
1178ab2e3f79SStephen Hurd 
1179ab2e3f79SStephen Hurd 	for (pidx_start = i = j = 0; i < nrxd; i++, j++) {
1180ab2e3f79SStephen Hurd 		int sj = netmap_idx_n2k(&na->rx_rings[rxq->ifr_id], i);
1181ab2e3f79SStephen Hurd 		void *addr;
1182ab2e3f79SStephen Hurd 
1183ab2e3f79SStephen Hurd 		fl->ifl_rxd_idxs[j] = i;
1184ab2e3f79SStephen Hurd 		addr = fl->ifl_vm_addrs[j] = PNMB(na, slot + sj, &fl->ifl_bus_addrs[j]);
1185ab2e3f79SStephen Hurd 		if (map) {
1186ab2e3f79SStephen Hurd 			netmap_load_map(na, rxq->ifr_fl[0].ifl_ifdi->idi_tag, *map, addr);
1187ab2e3f79SStephen Hurd 			map++;
1188ab2e3f79SStephen Hurd 		}
1189ab2e3f79SStephen Hurd 
1190ab2e3f79SStephen Hurd 		if (j < IFLIB_MAX_RX_REFRESH && i < nrxd - 1)
1191ab2e3f79SStephen Hurd 			continue;
1192ab2e3f79SStephen Hurd 
1193ab2e3f79SStephen Hurd 		iru.iru_pidx = pidx_start;
1194ab2e3f79SStephen Hurd 		pidx_start = i;
1195ab2e3f79SStephen Hurd 		iru.iru_count = j;
1196ab2e3f79SStephen Hurd 		j = 0;
1197ab2e3f79SStephen Hurd 		MPASS(pidx_start + j <= nrxd);
1198ab2e3f79SStephen Hurd 		/* Update descriptors and the cached value */
1199ab2e3f79SStephen Hurd 		ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
1200ab2e3f79SStephen Hurd 	}
1201ab2e3f79SStephen Hurd 	/* preserve queue */
1202ab2e3f79SStephen Hurd 	if (ctx->ifc_ifp->if_capenable & IFCAP_NETMAP) {
1203ab2e3f79SStephen Hurd 		struct netmap_kring *kring = &na->rx_rings[rxq->ifr_id];
1204ab2e3f79SStephen Hurd 		int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
1205ab2e3f79SStephen Hurd 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, t);
1206ab2e3f79SStephen Hurd 	} else
1207ab2e3f79SStephen Hurd 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, nrxd-1);
12084c7070dbSScott Long }
12094c7070dbSScott Long 
12104c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp)
12114c7070dbSScott Long 
12124c7070dbSScott Long #else
12134c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq)
12144c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq)
12154c7070dbSScott Long #define iflib_netmap_detach(ifp)
12164c7070dbSScott Long 
12174c7070dbSScott Long #define iflib_netmap_attach(ctx) (0)
12184c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0)
121995246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0)
12204c7070dbSScott Long 
12214c7070dbSScott Long #endif
12224c7070dbSScott Long 
12234c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
12244c7070dbSScott Long static __inline void
12254c7070dbSScott Long prefetch(void *x)
12264c7070dbSScott Long {
12274c7070dbSScott Long 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12284c7070dbSScott Long }
12294c7070dbSScott Long #else
12304c7070dbSScott Long #define prefetch(x)
12314c7070dbSScott Long #endif
12324c7070dbSScott Long 
12334c7070dbSScott Long static void
12344c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
12354c7070dbSScott Long {
12364c7070dbSScott Long 	if (err)
12374c7070dbSScott Long 		return;
12384c7070dbSScott Long 	*(bus_addr_t *) arg = segs[0].ds_addr;
12394c7070dbSScott Long }
12404c7070dbSScott Long 
12414c7070dbSScott Long int
12424c7070dbSScott Long iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
12434c7070dbSScott Long {
12444c7070dbSScott Long 	int err;
12454c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
12464c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
12474c7070dbSScott Long 
12484c7070dbSScott Long 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
12494c7070dbSScott Long 
12504c7070dbSScott Long 	err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
12514c7070dbSScott Long 				sctx->isc_q_align, 0,	/* alignment, bounds */
12524c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* lowaddr */
12534c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* highaddr */
12544c7070dbSScott Long 				NULL, NULL,		/* filter, filterarg */
12554c7070dbSScott Long 				size,			/* maxsize */
12564c7070dbSScott Long 				1,			/* nsegments */
12574c7070dbSScott Long 				size,			/* maxsegsize */
12584c7070dbSScott Long 				BUS_DMA_ALLOCNOW,	/* flags */
12594c7070dbSScott Long 				NULL,			/* lockfunc */
12604c7070dbSScott Long 				NULL,			/* lockarg */
12614c7070dbSScott Long 				&dma->idi_tag);
12624c7070dbSScott Long 	if (err) {
12634c7070dbSScott Long 		device_printf(dev,
12644c7070dbSScott Long 		    "%s: bus_dma_tag_create failed: %d\n",
12654c7070dbSScott Long 		    __func__, err);
12664c7070dbSScott Long 		goto fail_0;
12674c7070dbSScott Long 	}
12684c7070dbSScott Long 
12694c7070dbSScott Long 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
12704c7070dbSScott Long 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
12714c7070dbSScott Long 	if (err) {
12724c7070dbSScott Long 		device_printf(dev,
12734c7070dbSScott Long 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
12744c7070dbSScott Long 		    __func__, (uintmax_t)size, err);
12754c7070dbSScott Long 		goto fail_1;
12764c7070dbSScott Long 	}
12774c7070dbSScott Long 
12784c7070dbSScott Long 	dma->idi_paddr = IF_BAD_DMA;
12794c7070dbSScott Long 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
12804c7070dbSScott Long 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
12814c7070dbSScott Long 	if (err || dma->idi_paddr == IF_BAD_DMA) {
12824c7070dbSScott Long 		device_printf(dev,
12834c7070dbSScott Long 		    "%s: bus_dmamap_load failed: %d\n",
12844c7070dbSScott Long 		    __func__, err);
12854c7070dbSScott Long 		goto fail_2;
12864c7070dbSScott Long 	}
12874c7070dbSScott Long 
12884c7070dbSScott Long 	dma->idi_size = size;
12894c7070dbSScott Long 	return (0);
12904c7070dbSScott Long 
12914c7070dbSScott Long fail_2:
12924c7070dbSScott Long 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
12934c7070dbSScott Long fail_1:
12944c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
12954c7070dbSScott Long fail_0:
12964c7070dbSScott Long 	dma->idi_tag = NULL;
12974c7070dbSScott Long 
12984c7070dbSScott Long 	return (err);
12994c7070dbSScott Long }
13004c7070dbSScott Long 
13014c7070dbSScott Long int
13024c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
13034c7070dbSScott Long {
13044c7070dbSScott Long 	int i, err;
13054c7070dbSScott Long 	iflib_dma_info_t *dmaiter;
13064c7070dbSScott Long 
13074c7070dbSScott Long 	dmaiter = dmalist;
13084c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++) {
13094c7070dbSScott Long 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
13104c7070dbSScott Long 			break;
13114c7070dbSScott Long 	}
13124c7070dbSScott Long 	if (err)
13134c7070dbSScott Long 		iflib_dma_free_multi(dmalist, i);
13144c7070dbSScott Long 	return (err);
13154c7070dbSScott Long }
13164c7070dbSScott Long 
13174c7070dbSScott Long void
13184c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma)
13194c7070dbSScott Long {
13204c7070dbSScott Long 	if (dma->idi_tag == NULL)
13214c7070dbSScott Long 		return;
13224c7070dbSScott Long 	if (dma->idi_paddr != IF_BAD_DMA) {
13234c7070dbSScott Long 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
13244c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
13254c7070dbSScott Long 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
13264c7070dbSScott Long 		dma->idi_paddr = IF_BAD_DMA;
13274c7070dbSScott Long 	}
13284c7070dbSScott Long 	if (dma->idi_vaddr != NULL) {
13294c7070dbSScott Long 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
13304c7070dbSScott Long 		dma->idi_vaddr = NULL;
13314c7070dbSScott Long 	}
13324c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
13334c7070dbSScott Long 	dma->idi_tag = NULL;
13344c7070dbSScott Long }
13354c7070dbSScott Long 
13364c7070dbSScott Long void
13374c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
13384c7070dbSScott Long {
13394c7070dbSScott Long 	int i;
13404c7070dbSScott Long 	iflib_dma_info_t *dmaiter = dmalist;
13414c7070dbSScott Long 
13424c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++)
13434c7070dbSScott Long 		iflib_dma_free(*dmaiter);
13444c7070dbSScott Long }
13454c7070dbSScott Long 
1346bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP
1347bd84f700SSean Bruno static const int iflib_started = 1;
1348bd84f700SSean Bruno #else
1349bd84f700SSean Bruno /*
1350bd84f700SSean Bruno  * We used to abuse the smp_started flag to decide if the queues have been
1351bd84f700SSean Bruno  * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()).
1352bd84f700SSean Bruno  * That gave bad races, since the SYSINIT() runs strictly after smp_started
1353bd84f700SSean Bruno  * is set.  Run a SYSINIT() strictly after that to just set a usable
1354bd84f700SSean Bruno  * completion flag.
1355bd84f700SSean Bruno  */
1356bd84f700SSean Bruno 
1357bd84f700SSean Bruno static int iflib_started;
1358bd84f700SSean Bruno 
1359bd84f700SSean Bruno static void
1360bd84f700SSean Bruno iflib_record_started(void *arg)
1361bd84f700SSean Bruno {
1362bd84f700SSean Bruno 	iflib_started = 1;
1363bd84f700SSean Bruno }
1364bd84f700SSean Bruno 
1365bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST,
1366bd84f700SSean Bruno 	iflib_record_started, NULL);
1367bd84f700SSean Bruno #endif
1368bd84f700SSean Bruno 
13694c7070dbSScott Long static int
13704c7070dbSScott Long iflib_fast_intr(void *arg)
13714c7070dbSScott Long {
13724c7070dbSScott Long 	iflib_filter_info_t info = arg;
13734c7070dbSScott Long 	struct grouptask *gtask = info->ifi_task;
137495246abbSSean Bruno 	if (!iflib_started)
137595246abbSSean Bruno 		return (FILTER_HANDLED);
137695246abbSSean Bruno 
137795246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
137895246abbSSean Bruno 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
137995246abbSSean Bruno 		return (FILTER_HANDLED);
138095246abbSSean Bruno 
138195246abbSSean Bruno 	GROUPTASK_ENQUEUE(gtask);
138295246abbSSean Bruno 	return (FILTER_HANDLED);
138395246abbSSean Bruno }
138495246abbSSean Bruno 
138595246abbSSean Bruno static int
138695246abbSSean Bruno iflib_fast_intr_rxtx(void *arg)
138795246abbSSean Bruno {
138895246abbSSean Bruno 	iflib_filter_info_t info = arg;
138995246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
139095246abbSSean Bruno 	iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
139195246abbSSean Bruno 	if_ctx_t ctx;
139295246abbSSean Bruno 	int i, cidx;
139395246abbSSean Bruno 
139495246abbSSean Bruno 	if (!iflib_started)
139595246abbSSean Bruno 		return (FILTER_HANDLED);
139695246abbSSean Bruno 
139795246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
139895246abbSSean Bruno 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
139995246abbSSean Bruno 		return (FILTER_HANDLED);
140095246abbSSean Bruno 
140195246abbSSean Bruno 	for (i = 0; i < rxq->ifr_ntxqirq; i++) {
140295246abbSSean Bruno 		qidx_t txqid = rxq->ifr_txqid[i];
140395246abbSSean Bruno 
1404ab2e3f79SStephen Hurd 		ctx = rxq->ifr_ctx;
1405ab2e3f79SStephen Hurd 
140695246abbSSean Bruno 		if (!ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false)) {
140795246abbSSean Bruno 			IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
140895246abbSSean Bruno 			continue;
140995246abbSSean Bruno 		}
141095246abbSSean Bruno 		GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
141195246abbSSean Bruno 	}
141295246abbSSean Bruno 	if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
141395246abbSSean Bruno 		cidx = rxq->ifr_cq_cidx;
141495246abbSSean Bruno 	else
141595246abbSSean Bruno 		cidx = rxq->ifr_fl[0].ifl_cidx;
141695246abbSSean Bruno 	if (iflib_rxd_avail(ctx, rxq, cidx, 1))
141795246abbSSean Bruno 		GROUPTASK_ENQUEUE(gtask);
141895246abbSSean Bruno 	else
141995246abbSSean Bruno 		IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
142095246abbSSean Bruno 	return (FILTER_HANDLED);
142195246abbSSean Bruno }
142295246abbSSean Bruno 
142395246abbSSean Bruno 
142495246abbSSean Bruno static int
142595246abbSSean Bruno iflib_fast_intr_ctx(void *arg)
142695246abbSSean Bruno {
142795246abbSSean Bruno 	iflib_filter_info_t info = arg;
142895246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
14294c7070dbSScott Long 
1430bd84f700SSean Bruno 	if (!iflib_started)
14311248952aSSean Bruno 		return (FILTER_HANDLED);
14321248952aSSean Bruno 
14334c7070dbSScott Long 	DBG_COUNTER_INC(fast_intrs);
14344c7070dbSScott Long 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
14354c7070dbSScott Long 		return (FILTER_HANDLED);
14364c7070dbSScott Long 
14374c7070dbSScott Long 	GROUPTASK_ENQUEUE(gtask);
14384c7070dbSScott Long 	return (FILTER_HANDLED);
14394c7070dbSScott Long }
14404c7070dbSScott Long 
14414c7070dbSScott Long static int
14424c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
14434c7070dbSScott Long 	driver_filter_t filter, driver_intr_t handler, void *arg,
14444c7070dbSScott Long 				 char *name)
14454c7070dbSScott Long {
14462b2fc973SSean Bruno 	int rc, flags;
14474c7070dbSScott Long 	struct resource *res;
14482b2fc973SSean Bruno 	void *tag = NULL;
14494c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
14504c7070dbSScott Long 
14512b2fc973SSean Bruno 	flags = RF_ACTIVE;
14522b2fc973SSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
14532b2fc973SSean Bruno 		flags |= RF_SHAREABLE;
14544c7070dbSScott Long 	MPASS(rid < 512);
14554c7070dbSScott Long 	irq->ii_rid = rid;
14562b2fc973SSean Bruno 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid, flags);
14574c7070dbSScott Long 	if (res == NULL) {
14584c7070dbSScott Long 		device_printf(dev,
14594c7070dbSScott Long 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
14604c7070dbSScott Long 		return (ENOMEM);
14614c7070dbSScott Long 	}
14624c7070dbSScott Long 	irq->ii_res = res;
14634c7070dbSScott Long 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
14644c7070dbSScott Long 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
14654c7070dbSScott Long 						filter, handler, arg, &tag);
14664c7070dbSScott Long 	if (rc != 0) {
14674c7070dbSScott Long 		device_printf(dev,
14684c7070dbSScott Long 		    "failed to setup interrupt for rid %d, name %s: %d\n",
14694c7070dbSScott Long 					  rid, name ? name : "unknown", rc);
14704c7070dbSScott Long 		return (rc);
14714c7070dbSScott Long 	} else if (name)
1472f454e7ebSJohn Baldwin 		bus_describe_intr(dev, res, tag, "%s", name);
14734c7070dbSScott Long 
14744c7070dbSScott Long 	irq->ii_tag = tag;
14754c7070dbSScott Long 	return (0);
14764c7070dbSScott Long }
14774c7070dbSScott Long 
14784c7070dbSScott Long 
14794c7070dbSScott Long /*********************************************************************
14804c7070dbSScott Long  *
14814c7070dbSScott Long  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
14824c7070dbSScott Long  *  the information needed to transmit a packet on the wire. This is
14834c7070dbSScott Long  *  called only once at attach, setup is done every reset.
14844c7070dbSScott Long  *
14854c7070dbSScott Long  **********************************************************************/
14864c7070dbSScott Long 
14874c7070dbSScott Long static int
14884c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq)
14894c7070dbSScott Long {
14904c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
14914c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
14924c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
14934c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
14944c7070dbSScott Long 	int err, nsegments, ntsosegments;
14954c7070dbSScott Long 
14964c7070dbSScott Long 	nsegments = scctx->isc_tx_nsegments;
14974c7070dbSScott Long 	ntsosegments = scctx->isc_tx_tso_segments_max;
149823ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[0] > 0);
149923ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
15004c7070dbSScott Long 	MPASS(nsegments > 0);
15014c7070dbSScott Long 	MPASS(ntsosegments > 0);
15024c7070dbSScott Long 	/*
15034c7070dbSScott Long 	 * Setup DMA descriptor areas.
15044c7070dbSScott Long 	 */
15054c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
15064c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
15074c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
15084c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
15094c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
15104c7070dbSScott Long 			       sctx->isc_tx_maxsize,		/* maxsize */
15114c7070dbSScott Long 			       nsegments,	/* nsegments */
15124c7070dbSScott Long 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
15134c7070dbSScott Long 			       0,			/* flags */
15144c7070dbSScott Long 			       NULL,			/* lockfunc */
15154c7070dbSScott Long 			       NULL,			/* lockfuncarg */
15164c7070dbSScott Long 			       &txq->ift_desc_tag))) {
15174c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
15189d0a88deSDimitry Andric 		device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n",
15199d0a88deSDimitry Andric 		    (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
15204c7070dbSScott Long 		goto fail;
15214c7070dbSScott Long 	}
15224c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
15234c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
15244c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
15254c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
15264c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
15274c7070dbSScott Long 			       scctx->isc_tx_tso_size_max,		/* maxsize */
15284c7070dbSScott Long 			       ntsosegments,	/* nsegments */
15294c7070dbSScott Long 			       scctx->isc_tx_tso_segsize_max,	/* maxsegsize */
15304c7070dbSScott Long 			       0,			/* flags */
15314c7070dbSScott Long 			       NULL,			/* lockfunc */
15324c7070dbSScott Long 			       NULL,			/* lockfuncarg */
15334c7070dbSScott Long 			       &txq->ift_tso_desc_tag))) {
15344c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX TSO DMA tag: %d\n", err);
15354c7070dbSScott Long 
15364c7070dbSScott Long 		goto fail;
15374c7070dbSScott Long 	}
15384c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_flags =
15394c7070dbSScott Long 	    (uint8_t *) malloc(sizeof(uint8_t) *
154023ac9029SStephen Hurd 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
15414c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
15424c7070dbSScott Long 		err = ENOMEM;
15434c7070dbSScott Long 		goto fail;
15444c7070dbSScott Long 	}
15454c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_m =
15464c7070dbSScott Long 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
154723ac9029SStephen Hurd 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
15484c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
15494c7070dbSScott Long 		err = ENOMEM;
15504c7070dbSScott Long 		goto fail;
15514c7070dbSScott Long 	}
15524c7070dbSScott Long 
15534c7070dbSScott Long         /* Create the descriptor buffer dma maps */
155495246abbSSean Bruno #if defined(ACPI_DMAR) || (! (defined(__i386__) || defined(__amd64__)))
15554c7070dbSScott Long 	if ((ctx->ifc_flags & IFC_DMAR) == 0)
15564c7070dbSScott Long 		return (0);
15574c7070dbSScott Long 
15584c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_map =
155923ac9029SStephen Hurd 	    (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
15604c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer map memory\n");
15614c7070dbSScott Long 		err = ENOMEM;
15624c7070dbSScott Long 		goto fail;
15634c7070dbSScott Long 	}
15644c7070dbSScott Long 
156523ac9029SStephen Hurd 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
15664c7070dbSScott Long 		err = bus_dmamap_create(txq->ift_desc_tag, 0, &txq->ift_sds.ifsd_map[i]);
15674c7070dbSScott Long 		if (err != 0) {
15684c7070dbSScott Long 			device_printf(dev, "Unable to create TX DMA map\n");
15694c7070dbSScott Long 			goto fail;
15704c7070dbSScott Long 		}
15714c7070dbSScott Long 	}
15724c7070dbSScott Long #endif
15734c7070dbSScott Long 	return (0);
15744c7070dbSScott Long fail:
15754c7070dbSScott Long 	/* We free all, it handles case where we are in the middle */
15764c7070dbSScott Long 	iflib_tx_structures_free(ctx);
15774c7070dbSScott Long 	return (err);
15784c7070dbSScott Long }
15794c7070dbSScott Long 
15804c7070dbSScott Long static void
15814c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
15824c7070dbSScott Long {
15834c7070dbSScott Long 	bus_dmamap_t map;
15844c7070dbSScott Long 
15854c7070dbSScott Long 	map = NULL;
15864c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL)
15874c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[i];
15884c7070dbSScott Long 	if (map != NULL) {
15894c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag, map);
15904c7070dbSScott Long 		bus_dmamap_destroy(txq->ift_desc_tag, map);
15914c7070dbSScott Long 		txq->ift_sds.ifsd_map[i] = NULL;
15924c7070dbSScott Long 	}
15934c7070dbSScott Long }
15944c7070dbSScott Long 
15954c7070dbSScott Long static void
15964c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq)
15974c7070dbSScott Long {
15984c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
15994c7070dbSScott Long 
160023ac9029SStephen Hurd 	for (int i = 0; i < txq->ift_size; i++)
16014c7070dbSScott Long 		iflib_txsd_destroy(ctx, txq, i);
16024c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
16034c7070dbSScott Long 		free(txq->ift_sds.ifsd_map, M_IFLIB);
16044c7070dbSScott Long 		txq->ift_sds.ifsd_map = NULL;
16054c7070dbSScott Long 	}
16064c7070dbSScott Long 	if (txq->ift_sds.ifsd_m != NULL) {
16074c7070dbSScott Long 		free(txq->ift_sds.ifsd_m, M_IFLIB);
16084c7070dbSScott Long 		txq->ift_sds.ifsd_m = NULL;
16094c7070dbSScott Long 	}
16104c7070dbSScott Long 	if (txq->ift_sds.ifsd_flags != NULL) {
16114c7070dbSScott Long 		free(txq->ift_sds.ifsd_flags, M_IFLIB);
16124c7070dbSScott Long 		txq->ift_sds.ifsd_flags = NULL;
16134c7070dbSScott Long 	}
16144c7070dbSScott Long 	if (txq->ift_desc_tag != NULL) {
16154c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_desc_tag);
16164c7070dbSScott Long 		txq->ift_desc_tag = NULL;
16174c7070dbSScott Long 	}
16184c7070dbSScott Long 	if (txq->ift_tso_desc_tag != NULL) {
16194c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_tso_desc_tag);
16204c7070dbSScott Long 		txq->ift_tso_desc_tag = NULL;
16214c7070dbSScott Long 	}
16224c7070dbSScott Long }
16234c7070dbSScott Long 
16244c7070dbSScott Long static void
16254c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
16264c7070dbSScott Long {
16274c7070dbSScott Long 	struct mbuf **mp;
16284c7070dbSScott Long 
16294c7070dbSScott Long 	mp = &txq->ift_sds.ifsd_m[i];
16304c7070dbSScott Long 	if (*mp == NULL)
16314c7070dbSScott Long 		return;
16324c7070dbSScott Long 
16334c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
16344c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag,
16354c7070dbSScott Long 				txq->ift_sds.ifsd_map[i],
16364c7070dbSScott Long 				BUS_DMASYNC_POSTWRITE);
16374c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag,
16384c7070dbSScott Long 				  txq->ift_sds.ifsd_map[i]);
16394c7070dbSScott Long 	}
164023ac9029SStephen Hurd 	m_free(*mp);
16414c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
16424c7070dbSScott Long 	*mp = NULL;
16434c7070dbSScott Long }
16444c7070dbSScott Long 
16454c7070dbSScott Long static int
16464c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq)
16474c7070dbSScott Long {
16484c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
164923ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
16504c7070dbSScott Long 	iflib_dma_info_t di;
16514c7070dbSScott Long 	int i;
16524c7070dbSScott Long 
16534c7070dbSScott Long 	/* Set number of descriptors available */
16544c7070dbSScott Long 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
165595246abbSSean Bruno 	/* XXX make configurable */
165695246abbSSean Bruno 	txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
16574c7070dbSScott Long 
16584c7070dbSScott Long 	/* Reset indices */
165995246abbSSean Bruno 	txq->ift_cidx_processed = 0;
166095246abbSSean Bruno 	txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
166123ac9029SStephen Hurd 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
16624c7070dbSScott Long 
16634c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
16644c7070dbSScott Long 		bzero((void *)di->idi_vaddr, di->idi_size);
16654c7070dbSScott Long 
16664c7070dbSScott Long 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
16674c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
16684c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
16694c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
16704c7070dbSScott Long 	return (0);
16714c7070dbSScott Long }
16724c7070dbSScott Long 
16734c7070dbSScott Long /*********************************************************************
16744c7070dbSScott Long  *
16754c7070dbSScott Long  *  Allocate memory for rx_buffer structures. Since we use one
16764c7070dbSScott Long  *  rx_buffer per received packet, the maximum number of rx_buffer's
16774c7070dbSScott Long  *  that we'll need is equal to the number of receive descriptors
16784c7070dbSScott Long  *  that we've allocated.
16794c7070dbSScott Long  *
16804c7070dbSScott Long  **********************************************************************/
16814c7070dbSScott Long static int
16824c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq)
16834c7070dbSScott Long {
16844c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
16854c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
168623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
16874c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
16884c7070dbSScott Long 	iflib_fl_t fl;
16894c7070dbSScott Long 	int			err;
16904c7070dbSScott Long 
169123ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[0] > 0);
169223ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
16934c7070dbSScott Long 
16944c7070dbSScott Long 	fl = rxq->ifr_fl;
16954c7070dbSScott Long 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
169623ac9029SStephen Hurd 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
16974c7070dbSScott Long 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
16984c7070dbSScott Long 					 1, 0,			/* alignment, bounds */
16994c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* lowaddr */
17004c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* highaddr */
17014c7070dbSScott Long 					 NULL, NULL,		/* filter, filterarg */
17024c7070dbSScott Long 					 sctx->isc_rx_maxsize,	/* maxsize */
17034c7070dbSScott Long 					 sctx->isc_rx_nsegments,	/* nsegments */
17044c7070dbSScott Long 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
17054c7070dbSScott Long 					 0,			/* flags */
17064c7070dbSScott Long 					 NULL,			/* lockfunc */
17074c7070dbSScott Long 					 NULL,			/* lockarg */
17084c7070dbSScott Long 					 &fl->ifl_desc_tag);
17094c7070dbSScott Long 		if (err) {
17104c7070dbSScott Long 			device_printf(dev, "%s: bus_dma_tag_create failed %d\n",
17114c7070dbSScott Long 				__func__, err);
17124c7070dbSScott Long 			goto fail;
17134c7070dbSScott Long 		}
1714e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_flags =
1715e035717eSSean Bruno 		      (uint8_t *) malloc(sizeof(uint8_t) *
1716e035717eSSean Bruno 					 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1717e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1718e035717eSSean Bruno 			err = ENOMEM;
1719e035717eSSean Bruno 			goto fail;
1720e035717eSSean Bruno 		}
1721e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_m =
1722e035717eSSean Bruno 		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
1723e035717eSSean Bruno 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1724e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1725e035717eSSean Bruno 			err = ENOMEM;
1726e035717eSSean Bruno 			goto fail;
1727e035717eSSean Bruno 		}
1728e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_cl =
1729e035717eSSean Bruno 		      (caddr_t *) malloc(sizeof(caddr_t) *
1730e035717eSSean Bruno 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1731e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1732e035717eSSean Bruno 			err = ENOMEM;
1733e035717eSSean Bruno 			goto fail;
1734e035717eSSean Bruno 		}
17354c7070dbSScott Long 
1736e035717eSSean Bruno 		/* Create the descriptor buffer dma maps */
173795246abbSSean Bruno #if defined(ACPI_DMAR) || (! (defined(__i386__) || defined(__amd64__)))
1738e035717eSSean Bruno 		if ((ctx->ifc_flags & IFC_DMAR) == 0)
1739e035717eSSean Bruno 			continue;
1740e035717eSSean Bruno 
1741e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_map =
1742e035717eSSean Bruno 		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1743e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer map memory\n");
1744e035717eSSean Bruno 			err = ENOMEM;
1745e035717eSSean Bruno 			goto fail;
1746e035717eSSean Bruno 		}
1747e035717eSSean Bruno 
1748e035717eSSean Bruno 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1749e035717eSSean Bruno 			err = bus_dmamap_create(fl->ifl_desc_tag, 0, &fl->ifl_sds.ifsd_map[i]);
1750e035717eSSean Bruno 			if (err != 0) {
175195246abbSSean Bruno 				device_printf(dev, "Unable to create RX buffer DMA map\n");
17524c7070dbSScott Long 				goto fail;
17534c7070dbSScott Long 			}
17544c7070dbSScott Long 		}
1755e035717eSSean Bruno #endif
1756835809f9SSean Bruno 	}
17574c7070dbSScott Long 	return (0);
17584c7070dbSScott Long 
17594c7070dbSScott Long fail:
17604c7070dbSScott Long 	iflib_rx_structures_free(ctx);
17614c7070dbSScott Long 	return (err);
17624c7070dbSScott Long }
17634c7070dbSScott Long 
17644c7070dbSScott Long 
17654c7070dbSScott Long /*
17664c7070dbSScott Long  * Internal service routines
17674c7070dbSScott Long  */
17684c7070dbSScott Long 
17694c7070dbSScott Long struct rxq_refill_cb_arg {
17704c7070dbSScott Long 	int               error;
17714c7070dbSScott Long 	bus_dma_segment_t seg;
17724c7070dbSScott Long 	int               nseg;
17734c7070dbSScott Long };
17744c7070dbSScott Long 
17754c7070dbSScott Long static void
17764c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
17774c7070dbSScott Long {
17784c7070dbSScott Long 	struct rxq_refill_cb_arg *cb_arg = arg;
17794c7070dbSScott Long 
17804c7070dbSScott Long 	cb_arg->error = error;
17814c7070dbSScott Long 	cb_arg->seg = segs[0];
17824c7070dbSScott Long 	cb_arg->nseg = nseg;
17834c7070dbSScott Long }
17844c7070dbSScott Long 
17854c7070dbSScott Long 
17864c7070dbSScott Long #ifdef ACPI_DMAR
17874c7070dbSScott Long #define IS_DMAR(ctx) (ctx->ifc_flags & IFC_DMAR)
17884c7070dbSScott Long #else
17894c7070dbSScott Long #define IS_DMAR(ctx) (0)
17904c7070dbSScott Long #endif
17914c7070dbSScott Long 
17924c7070dbSScott Long /**
17934c7070dbSScott Long  *	rxq_refill - refill an rxq  free-buffer list
17944c7070dbSScott Long  *	@ctx: the iflib context
17954c7070dbSScott Long  *	@rxq: the free-list to refill
17964c7070dbSScott Long  *	@n: the number of new buffers to allocate
17974c7070dbSScott Long  *
17984c7070dbSScott Long  *	(Re)populate an rxq free-buffer list with up to @n new packet buffers.
17994c7070dbSScott Long  *	The caller must assure that @n does not exceed the queue's capacity.
18004c7070dbSScott Long  */
18014c7070dbSScott Long static void
18024c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
18034c7070dbSScott Long {
18044c7070dbSScott Long 	struct mbuf *m;
180587890dbaSSean Bruno 	int idx, frag_idx = fl->ifl_fragidx;
180687890dbaSSean Bruno         int pidx = fl->ifl_pidx;
1807e035717eSSean Bruno 	caddr_t cl, *sd_cl;
1808e035717eSSean Bruno 	struct mbuf **sd_m;
1809e035717eSSean Bruno 	uint8_t *sd_flags;
181095246abbSSean Bruno 	struct if_rxd_update iru;
1811e035717eSSean Bruno 	bus_dmamap_t *sd_map;
18124c7070dbSScott Long 	int n, i = 0;
18134c7070dbSScott Long 	uint64_t bus_addr;
18144c7070dbSScott Long 	int err;
18154c7070dbSScott Long 
1816e035717eSSean Bruno 	sd_m = fl->ifl_sds.ifsd_m;
1817e035717eSSean Bruno 	sd_map = fl->ifl_sds.ifsd_map;
1818e035717eSSean Bruno 	sd_cl = fl->ifl_sds.ifsd_cl;
1819e035717eSSean Bruno 	sd_flags = fl->ifl_sds.ifsd_flags;
1820e035717eSSean Bruno 	idx = pidx;
1821e035717eSSean Bruno 
18224c7070dbSScott Long 	n  = count;
18234c7070dbSScott Long 	MPASS(n > 0);
18244c7070dbSScott Long 	MPASS(fl->ifl_credits + n <= fl->ifl_size);
18254c7070dbSScott Long 
18264c7070dbSScott Long 	if (pidx < fl->ifl_cidx)
18274c7070dbSScott Long 		MPASS(pidx + n <= fl->ifl_cidx);
18284c7070dbSScott Long 	if (pidx == fl->ifl_cidx && (fl->ifl_credits < fl->ifl_size))
18294c7070dbSScott Long 		MPASS(fl->ifl_gen == 0);
18304c7070dbSScott Long 	if (pidx > fl->ifl_cidx)
18314c7070dbSScott Long 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
18324c7070dbSScott Long 
18334c7070dbSScott Long 	DBG_COUNTER_INC(fl_refills);
18344c7070dbSScott Long 	if (n > 8)
18354c7070dbSScott Long 		DBG_COUNTER_INC(fl_refills_large);
183695246abbSSean Bruno 	iru.iru_paddrs = fl->ifl_bus_addrs;
183795246abbSSean Bruno 	iru.iru_vaddrs = &fl->ifl_vm_addrs[0];
183895246abbSSean Bruno 	iru.iru_idxs = fl->ifl_rxd_idxs;
183995246abbSSean Bruno 	iru.iru_qsidx = fl->ifl_rxq->ifr_id;
184095246abbSSean Bruno 	iru.iru_buf_size = fl->ifl_buf_size;
184195246abbSSean Bruno 	iru.iru_flidx = fl->ifl_id;
18424c7070dbSScott Long 	while (n--) {
18434c7070dbSScott Long 		/*
18444c7070dbSScott Long 		 * We allocate an uninitialized mbuf + cluster, mbuf is
18454c7070dbSScott Long 		 * initialized after rx.
18464c7070dbSScott Long 		 *
18474c7070dbSScott Long 		 * If the cluster is still set then we know a minimum sized packet was received
18484c7070dbSScott Long 		 */
184987890dbaSSean Bruno 		bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,  &frag_idx);
185087890dbaSSean Bruno 		if ((frag_idx < 0) || (frag_idx >= fl->ifl_size))
185187890dbaSSean Bruno                 	bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
185287890dbaSSean Bruno 		if ((cl = sd_cl[frag_idx]) == NULL) {
185387890dbaSSean Bruno                        if ((cl = sd_cl[frag_idx] = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
18544c7070dbSScott Long 				break;
18554c7070dbSScott Long #if MEMORY_LOGGING
18564c7070dbSScott Long 			fl->ifl_cl_enqueued++;
18574c7070dbSScott Long #endif
18584c7070dbSScott Long 		}
18594c7070dbSScott Long 		if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
18604c7070dbSScott Long 			break;
18614c7070dbSScott Long 		}
18624c7070dbSScott Long #if MEMORY_LOGGING
18634c7070dbSScott Long 		fl->ifl_m_enqueued++;
18644c7070dbSScott Long #endif
18654c7070dbSScott Long 
18664c7070dbSScott Long 		DBG_COUNTER_INC(rx_allocs);
18674c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
18684c7070dbSScott Long 		if (!IS_DMAR(ctx)) {
18694c7070dbSScott Long 			bus_addr = pmap_kextract((vm_offset_t)cl);
18704c7070dbSScott Long 		} else
18714c7070dbSScott Long #endif
18724c7070dbSScott Long 		{
18734c7070dbSScott Long 			struct rxq_refill_cb_arg cb_arg;
18744c7070dbSScott Long 			iflib_rxq_t q;
18754c7070dbSScott Long 
18764c7070dbSScott Long 			cb_arg.error = 0;
18774c7070dbSScott Long 			q = fl->ifl_rxq;
187895246abbSSean Bruno 			MPASS(sd_map != NULL);
187987890dbaSSean Bruno 			MPASS(sd_map[frag_idx] != NULL);
188087890dbaSSean Bruno 			err = bus_dmamap_load(fl->ifl_desc_tag, sd_map[frag_idx],
18814c7070dbSScott Long 		         cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 0);
188287890dbaSSean Bruno 			bus_dmamap_sync(fl->ifl_desc_tag, sd_map[frag_idx],
188387890dbaSSean Bruno 					BUS_DMASYNC_PREREAD);
18844c7070dbSScott Long 
18854c7070dbSScott Long 			if (err != 0 || cb_arg.error) {
18864c7070dbSScott Long 				/*
18874c7070dbSScott Long 				 * !zone_pack ?
18884c7070dbSScott Long 				 */
18894c7070dbSScott Long 				if (fl->ifl_zone == zone_pack)
18904c7070dbSScott Long 					uma_zfree(fl->ifl_zone, cl);
18914c7070dbSScott Long 				m_free(m);
18924c7070dbSScott Long 				n = 0;
18934c7070dbSScott Long 				goto done;
18944c7070dbSScott Long 			}
18954c7070dbSScott Long 			bus_addr = cb_arg.seg.ds_addr;
18964c7070dbSScott Long 		}
189787890dbaSSean Bruno                 bit_set(fl->ifl_rx_bitmap, frag_idx);
189887890dbaSSean Bruno 		sd_flags[frag_idx] |= RX_SW_DESC_INUSE;
18994c7070dbSScott Long 
190087890dbaSSean Bruno 		MPASS(sd_m[frag_idx] == NULL);
190187890dbaSSean Bruno 		sd_cl[frag_idx] = cl;
190287890dbaSSean Bruno 		sd_m[frag_idx] = m;
190387890dbaSSean Bruno 		fl->ifl_rxd_idxs[i] = frag_idx;
19044c7070dbSScott Long 		fl->ifl_bus_addrs[i] = bus_addr;
19054c7070dbSScott Long 		fl->ifl_vm_addrs[i] = cl;
19064c7070dbSScott Long 		fl->ifl_credits++;
19074c7070dbSScott Long 		i++;
19084c7070dbSScott Long 		MPASS(fl->ifl_credits <= fl->ifl_size);
1909e035717eSSean Bruno 		if (++idx == fl->ifl_size) {
19104c7070dbSScott Long 			fl->ifl_gen = 1;
1911e035717eSSean Bruno 			idx = 0;
19124c7070dbSScott Long 		}
19134c7070dbSScott Long 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
191495246abbSSean Bruno 			iru.iru_pidx = pidx;
191595246abbSSean Bruno 			iru.iru_count = i;
191695246abbSSean Bruno 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
19174c7070dbSScott Long 			i = 0;
1918e035717eSSean Bruno 			pidx = idx;
1919fa5416a8SSean Bruno 			fl->ifl_pidx = idx;
192087890dbaSSean Bruno 		}
1921e035717eSSean Bruno 
19224c7070dbSScott Long 	}
19234c7070dbSScott Long done:
19244c7070dbSScott Long 	DBG_COUNTER_INC(rxd_flush);
19254c7070dbSScott Long 	if (fl->ifl_pidx == 0)
19264c7070dbSScott Long 		pidx = fl->ifl_size - 1;
19274c7070dbSScott Long 	else
19284c7070dbSScott Long 		pidx = fl->ifl_pidx - 1;
192995246abbSSean Bruno 
193095246abbSSean Bruno 	if (sd_map)
193195246abbSSean Bruno 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
193295246abbSSean Bruno 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
19334c7070dbSScott Long 	ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
193487890dbaSSean Bruno 	fl->ifl_fragidx = frag_idx;
19354c7070dbSScott Long }
19364c7070dbSScott Long 
19374c7070dbSScott Long static __inline void
19384c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max)
19394c7070dbSScott Long {
19404c7070dbSScott Long 	/* we avoid allowing pidx to catch up with cidx as it confuses ixl */
19414c7070dbSScott Long 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
19424c7070dbSScott Long #ifdef INVARIANTS
19434c7070dbSScott Long 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
19444c7070dbSScott Long #endif
19454c7070dbSScott Long 
19464c7070dbSScott Long 	MPASS(fl->ifl_credits <= fl->ifl_size);
19474c7070dbSScott Long 	MPASS(reclaimable == delta);
19484c7070dbSScott Long 
19494c7070dbSScott Long 	if (reclaimable > 0)
19504c7070dbSScott Long 		_iflib_fl_refill(ctx, fl, min(max, reclaimable));
19514c7070dbSScott Long }
19524c7070dbSScott Long 
19534c7070dbSScott Long static void
19544c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl)
19554c7070dbSScott Long {
19564c7070dbSScott Long 	iflib_dma_info_t idi = fl->ifl_ifdi;
19574c7070dbSScott Long 	uint32_t i;
19584c7070dbSScott Long 
19594c7070dbSScott Long 	for (i = 0; i < fl->ifl_size; i++) {
1960e035717eSSean Bruno 		struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
1961e035717eSSean Bruno 		uint8_t *sd_flags = &fl->ifl_sds.ifsd_flags[i];
1962e035717eSSean Bruno 		caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
19634c7070dbSScott Long 
1964e035717eSSean Bruno 		if (*sd_flags & RX_SW_DESC_INUSE) {
1965e035717eSSean Bruno 			if (fl->ifl_sds.ifsd_map != NULL) {
1966e035717eSSean Bruno 				bus_dmamap_t sd_map = fl->ifl_sds.ifsd_map[i];
1967e035717eSSean Bruno 				bus_dmamap_unload(fl->ifl_desc_tag, sd_map);
1968e035717eSSean Bruno 				bus_dmamap_destroy(fl->ifl_desc_tag, sd_map);
19694c7070dbSScott Long 			}
1970e035717eSSean Bruno 			if (*sd_m != NULL) {
1971e035717eSSean Bruno 				m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
1972e035717eSSean Bruno 				uma_zfree(zone_mbuf, *sd_m);
1973e035717eSSean Bruno 			}
1974e035717eSSean Bruno 			if (*sd_cl != NULL)
1975e035717eSSean Bruno 				uma_zfree(fl->ifl_zone, *sd_cl);
1976e035717eSSean Bruno 			*sd_flags = 0;
19774c7070dbSScott Long 		} else {
1978e035717eSSean Bruno 			MPASS(*sd_cl == NULL);
1979e035717eSSean Bruno 			MPASS(*sd_m == NULL);
19804c7070dbSScott Long 		}
19814c7070dbSScott Long #if MEMORY_LOGGING
19824c7070dbSScott Long 		fl->ifl_m_dequeued++;
19834c7070dbSScott Long 		fl->ifl_cl_dequeued++;
19844c7070dbSScott Long #endif
1985e035717eSSean Bruno 		*sd_cl = NULL;
1986e035717eSSean Bruno 		*sd_m = NULL;
19874c7070dbSScott Long 	}
198895246abbSSean Bruno #ifdef INVARIANTS
198995246abbSSean Bruno 	for (i = 0; i < fl->ifl_size; i++) {
1990ab2e3f79SStephen Hurd 		MPASS(fl->ifl_sds.ifsd_flags[i] == 0);
199195246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
199295246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
199395246abbSSean Bruno 	}
199495246abbSSean Bruno #endif
19954c7070dbSScott Long 	/*
19964c7070dbSScott Long 	 * Reset free list values
19974c7070dbSScott Long 	 */
199887890dbaSSean Bruno 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
19994c7070dbSScott Long 	bzero(idi->idi_vaddr, idi->idi_size);
20004c7070dbSScott Long }
20014c7070dbSScott Long 
20024c7070dbSScott Long /*********************************************************************
20034c7070dbSScott Long  *
20044c7070dbSScott Long  *  Initialize a receive ring and its buffers.
20054c7070dbSScott Long  *
20064c7070dbSScott Long  **********************************************************************/
20074c7070dbSScott Long static int
20084c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl)
20094c7070dbSScott Long {
20104c7070dbSScott Long 	iflib_rxq_t rxq = fl->ifl_rxq;
20114c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
20124c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
20134c7070dbSScott Long 
2014ab2e3f79SStephen Hurd 	bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size);
20154c7070dbSScott Long 	/*
20164c7070dbSScott Long 	** Free current RX buffer structs and their mbufs
20174c7070dbSScott Long 	*/
20184c7070dbSScott Long 	iflib_fl_bufs_free(fl);
20194c7070dbSScott Long 	/* Now replenish the mbufs */
20204c7070dbSScott Long 	MPASS(fl->ifl_credits == 0);
20214c7070dbSScott Long 	/*
20224c7070dbSScott Long 	 * XXX don't set the max_frame_size to larger
20234c7070dbSScott Long 	 * than the hardware can handle
20244c7070dbSScott Long 	 */
20254c7070dbSScott Long 	if (sctx->isc_max_frame_size <= 2048)
20264c7070dbSScott Long 		fl->ifl_buf_size = MCLBYTES;
202795246abbSSean Bruno #ifndef CONTIGMALLOC_WORKS
202895246abbSSean Bruno 	else
202995246abbSSean Bruno 		fl->ifl_buf_size = MJUMPAGESIZE;
203095246abbSSean Bruno #else
20314c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 4096)
20324c7070dbSScott Long 		fl->ifl_buf_size = MJUMPAGESIZE;
20334c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 9216)
20344c7070dbSScott Long 		fl->ifl_buf_size = MJUM9BYTES;
20354c7070dbSScott Long 	else
20364c7070dbSScott Long 		fl->ifl_buf_size = MJUM16BYTES;
203795246abbSSean Bruno #endif
20384c7070dbSScott Long 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
20394c7070dbSScott Long 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
20404c7070dbSScott Long 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
20414c7070dbSScott Long 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
20424c7070dbSScott Long 
20434c7070dbSScott Long 
20444c7070dbSScott Long 	/* avoid pre-allocating zillions of clusters to an idle card
20454c7070dbSScott Long 	 * potentially speeding up attach
20464c7070dbSScott Long 	 */
20474c7070dbSScott Long 	_iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
20484c7070dbSScott Long 	MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
20494c7070dbSScott Long 	if (min(128, fl->ifl_size) != fl->ifl_credits)
20504c7070dbSScott Long 		return (ENOBUFS);
20514c7070dbSScott Long 	/*
20524c7070dbSScott Long 	 * handle failure
20534c7070dbSScott Long 	 */
20544c7070dbSScott Long 	MPASS(rxq != NULL);
20554c7070dbSScott Long 	MPASS(fl->ifl_ifdi != NULL);
20564c7070dbSScott Long 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
20574c7070dbSScott Long 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
20584c7070dbSScott Long 	return (0);
20594c7070dbSScott Long }
20604c7070dbSScott Long 
20614c7070dbSScott Long /*********************************************************************
20624c7070dbSScott Long  *
20634c7070dbSScott Long  *  Free receive ring data structures
20644c7070dbSScott Long  *
20654c7070dbSScott Long  **********************************************************************/
20664c7070dbSScott Long static void
20674c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq)
20684c7070dbSScott Long {
20694c7070dbSScott Long 	iflib_fl_t fl;
20704c7070dbSScott Long 	int i;
20714c7070dbSScott Long 
20724c7070dbSScott Long 	if (rxq->ifr_fl != NULL) {
20734c7070dbSScott Long 		for (i = 0; i < rxq->ifr_nfl; i++) {
20744c7070dbSScott Long 			fl = &rxq->ifr_fl[i];
20754c7070dbSScott Long 			if (fl->ifl_desc_tag != NULL) {
20764c7070dbSScott Long 				bus_dma_tag_destroy(fl->ifl_desc_tag);
20774c7070dbSScott Long 				fl->ifl_desc_tag = NULL;
20784c7070dbSScott Long 			}
2079e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_m, M_IFLIB);
2080e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_cl, M_IFLIB);
2081e035717eSSean Bruno 			/* XXX destroy maps first */
2082e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_map, M_IFLIB);
2083e035717eSSean Bruno 			fl->ifl_sds.ifsd_m = NULL;
2084e035717eSSean Bruno 			fl->ifl_sds.ifsd_cl = NULL;
2085e035717eSSean Bruno 			fl->ifl_sds.ifsd_map = NULL;
20864c7070dbSScott Long 		}
20874c7070dbSScott Long 		free(rxq->ifr_fl, M_IFLIB);
20884c7070dbSScott Long 		rxq->ifr_fl = NULL;
20894c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
20904c7070dbSScott Long 	}
20914c7070dbSScott Long }
20924c7070dbSScott Long 
20934c7070dbSScott Long /*
20944c7070dbSScott Long  * MI independent logic
20954c7070dbSScott Long  *
20964c7070dbSScott Long  */
20974c7070dbSScott Long static void
20984c7070dbSScott Long iflib_timer(void *arg)
20994c7070dbSScott Long {
2100ab2e3f79SStephen Hurd 	iflib_txq_t txq = arg;
21014c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
2102ab2e3f79SStephen Hurd 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
21034c7070dbSScott Long 
21044c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
21054c7070dbSScott Long 		return;
21064c7070dbSScott Long 	/*
21074c7070dbSScott Long 	** Check on the state of the TX queue(s), this
21084c7070dbSScott Long 	** can be done without the lock because its RO
21094c7070dbSScott Long 	** and the HUNG state will be static if set.
21104c7070dbSScott Long 	*/
2111ab2e3f79SStephen Hurd 	IFDI_TIMER(ctx, txq->ift_id);
2112ab2e3f79SStephen Hurd 	if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
2113ab2e3f79SStephen Hurd 	    ((txq->ift_cleaned_prev == txq->ift_cleaned) ||
2114ab2e3f79SStephen Hurd 	     (sctx->isc_pause_frames == 0)))
2115ab2e3f79SStephen Hurd 		goto hung;
2116a9693502SSean Bruno 
2117ab2e3f79SStephen Hurd 	if (ifmp_ring_is_stalled(txq->ift_br))
2118ab2e3f79SStephen Hurd 		txq->ift_qstatus = IFLIB_QUEUE_HUNG;
2119ab2e3f79SStephen Hurd 	txq->ift_cleaned_prev = txq->ift_cleaned;
2120ab2e3f79SStephen Hurd 	/* handle any laggards */
2121ab2e3f79SStephen Hurd 	if (txq->ift_db_pending)
2122ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
2123a9693502SSean Bruno 
2124ab2e3f79SStephen Hurd 	sctx->isc_pause_frames = 0;
2125d300df01SStephen Hurd 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
2126ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
2127ab2e3f79SStephen Hurd 	return;
2128ab2e3f79SStephen Hurd hung:
2129ab2e3f79SStephen Hurd 	CTX_LOCK(ctx);
2130ab2e3f79SStephen Hurd 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
2131ab2e3f79SStephen Hurd 	device_printf(ctx->ifc_dev,  "TX(%d) desc avail = %d, pidx = %d\n",
2132ab2e3f79SStephen Hurd 				  txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
2133ab2e3f79SStephen Hurd 
2134ab2e3f79SStephen Hurd 	IFDI_WATCHDOG_RESET(ctx);
2135ab2e3f79SStephen Hurd 	ctx->ifc_watchdog_events++;
2136ab2e3f79SStephen Hurd 
2137ab2e3f79SStephen Hurd 	ctx->ifc_flags |= IFC_DO_RESET;
2138ab2e3f79SStephen Hurd 	iflib_admin_intr_deferred(ctx);
2139ab2e3f79SStephen Hurd 	CTX_UNLOCK(ctx);
21404c7070dbSScott Long }
21414c7070dbSScott Long 
21424c7070dbSScott Long static void
21434c7070dbSScott Long iflib_init_locked(if_ctx_t ctx)
21444c7070dbSScott Long {
21454c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
21461248952aSSean Bruno 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
21474c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
21484c7070dbSScott Long 	iflib_fl_t fl;
21494c7070dbSScott Long 	iflib_txq_t txq;
21504c7070dbSScott Long 	iflib_rxq_t rxq;
2151ab2e3f79SStephen Hurd 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
21524c7070dbSScott Long 
21534c7070dbSScott Long 
21544c7070dbSScott Long 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
21554c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
21564c7070dbSScott Long 
21571248952aSSean Bruno 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
21581248952aSSean Bruno 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
21594c7070dbSScott Long 	/* Set hardware offload abilities */
21604c7070dbSScott Long 	if_clearhwassist(ifp);
21614c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
21621248952aSSean Bruno 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
21634c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
21641248952aSSean Bruno 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
21654c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO4)
21664c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
21674c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO6)
21684c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
21694c7070dbSScott Long 
21704c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
21714c7070dbSScott Long 		CALLOUT_LOCK(txq);
21724c7070dbSScott Long 		callout_stop(&txq->ift_timer);
21734c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
21744c7070dbSScott Long 		iflib_netmap_txq_init(ctx, txq);
21754c7070dbSScott Long 	}
217623ac9029SStephen Hurd #ifdef INVARIANTS
217723ac9029SStephen Hurd 	i = if_getdrvflags(ifp);
217823ac9029SStephen Hurd #endif
21794c7070dbSScott Long 	IFDI_INIT(ctx);
218023ac9029SStephen Hurd 	MPASS(if_getdrvflags(ifp) == i);
21814c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
218295246abbSSean Bruno 		/* XXX this should really be done on a per-queue basis */
2183*d0d0ad0aSStephen Hurd 		if (if_getcapenable(ifp) & IFCAP_NETMAP) {
2184*d0d0ad0aSStephen Hurd 			MPASS(rxq->ifr_id == i);
2185*d0d0ad0aSStephen Hurd 			iflib_netmap_rxq_init(ctx, rxq);
218695246abbSSean Bruno 			continue;
2187*d0d0ad0aSStephen Hurd 		}
21884c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
21894c7070dbSScott Long 			if (iflib_fl_setup(fl)) {
21904c7070dbSScott Long 				device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n");
21914c7070dbSScott Long 				goto done;
21924c7070dbSScott Long 			}
21934c7070dbSScott Long 		}
21944c7070dbSScott Long 	}
21954c7070dbSScott Long 	done:
21964c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
21974c7070dbSScott Long 	IFDI_INTR_ENABLE(ctx);
21984c7070dbSScott Long 	txq = ctx->ifc_txqs;
21994c7070dbSScott Long 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
2200ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
2201ab2e3f79SStephen Hurd 			txq->ift_timer.c_cpu);
22024c7070dbSScott Long }
22034c7070dbSScott Long 
22044c7070dbSScott Long static int
22054c7070dbSScott Long iflib_media_change(if_t ifp)
22064c7070dbSScott Long {
22074c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
22084c7070dbSScott Long 	int err;
22094c7070dbSScott Long 
22104c7070dbSScott Long 	CTX_LOCK(ctx);
22114c7070dbSScott Long 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
22124c7070dbSScott Long 		iflib_init_locked(ctx);
22134c7070dbSScott Long 	CTX_UNLOCK(ctx);
22144c7070dbSScott Long 	return (err);
22154c7070dbSScott Long }
22164c7070dbSScott Long 
22174c7070dbSScott Long static void
22184c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
22194c7070dbSScott Long {
22204c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
22214c7070dbSScott Long 
22224c7070dbSScott Long 	CTX_LOCK(ctx);
2223ab2e3f79SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
22244c7070dbSScott Long 	IFDI_MEDIA_STATUS(ctx, ifmr);
22254c7070dbSScott Long 	CTX_UNLOCK(ctx);
22264c7070dbSScott Long }
22274c7070dbSScott Long 
22284c7070dbSScott Long static void
22294c7070dbSScott Long iflib_stop(if_ctx_t ctx)
22304c7070dbSScott Long {
22314c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
22324c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
22334c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
22344c7070dbSScott Long 	iflib_dma_info_t di;
22354c7070dbSScott Long 	iflib_fl_t fl;
22364c7070dbSScott Long 	int i, j;
22374c7070dbSScott Long 
22384c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
22394c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
22404c7070dbSScott Long 
22414c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
2242ab2e3f79SStephen Hurd 	DELAY(1000);
2243da69b8f9SSean Bruno 	IFDI_STOP(ctx);
2244ab2e3f79SStephen Hurd 	DELAY(1000);
22454c7070dbSScott Long 
2246da69b8f9SSean Bruno 	iflib_debug_reset();
22474c7070dbSScott Long 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
22484c7070dbSScott Long 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
22494c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
22504c7070dbSScott Long 
22514c7070dbSScott Long 		/* clean any enqueued buffers */
2252da69b8f9SSean Bruno 		iflib_ifmp_purge(txq);
22534c7070dbSScott Long 		/* Free any existing tx buffers. */
225423ac9029SStephen Hurd 		for (j = 0; j < txq->ift_size; j++) {
22554c7070dbSScott Long 			iflib_txsd_free(ctx, txq, j);
22564c7070dbSScott Long 		}
2257ab2e3f79SStephen Hurd 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2258ab2e3f79SStephen Hurd 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
22594c7070dbSScott Long 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
22604c7070dbSScott Long 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
2261ab2e3f79SStephen Hurd 		txq->ift_pullups = 0;
226295246abbSSean Bruno 		ifmp_ring_reset_stats(txq->ift_br);
22634c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwtxqs; j++, di++)
22644c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
22654c7070dbSScott Long 	}
22664c7070dbSScott Long 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
22674c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
22684c7070dbSScott Long 
22694c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwrxqs; j++, di++)
22704c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
22714c7070dbSScott Long 		/* also resets the free lists pidx/cidx */
22724c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
22734c7070dbSScott Long 			iflib_fl_bufs_free(fl);
22744c7070dbSScott Long 	}
22754c7070dbSScott Long }
22764c7070dbSScott Long 
227795246abbSSean Bruno static inline caddr_t
227895246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx)
227995246abbSSean Bruno {
228095246abbSSean Bruno 	qidx_t size;
228195246abbSSean Bruno 	int nrxd;
228295246abbSSean Bruno 	caddr_t start, end, cur, next;
228395246abbSSean Bruno 
228495246abbSSean Bruno 	nrxd = fl->ifl_size;
228595246abbSSean Bruno 	size = fl->ifl_rxd_size;
228695246abbSSean Bruno 	start = fl->ifl_ifdi->idi_vaddr;
228795246abbSSean Bruno 
228895246abbSSean Bruno 	if (__predict_false(size == 0))
228995246abbSSean Bruno 		return (start);
229095246abbSSean Bruno 	cur = start + size*cidx;
229195246abbSSean Bruno 	end = start + size*nrxd;
229295246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
229395246abbSSean Bruno 	return (next < end ? next : start);
229495246abbSSean Bruno }
229595246abbSSean Bruno 
2296e035717eSSean Bruno static inline void
2297e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx)
2298e035717eSSean Bruno {
2299e035717eSSean Bruno 	int nextptr;
2300e035717eSSean Bruno 	int nrxd = fl->ifl_size;
230195246abbSSean Bruno 	caddr_t next_rxd;
230295246abbSSean Bruno 
2303e035717eSSean Bruno 
2304e035717eSSean Bruno 	nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2305e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2306e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
230795246abbSSean Bruno 	next_rxd = calc_next_rxd(fl, cidx);
230895246abbSSean Bruno 	prefetch(next_rxd);
2309e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2310e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2311e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2312e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2313e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2314e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2315e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2316e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2317e035717eSSean Bruno }
2318e035717eSSean Bruno 
2319e035717eSSean Bruno static void
232095246abbSSean Bruno rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int unload, if_rxsd_t sd)
23214c7070dbSScott Long {
23224c7070dbSScott Long 	int flid, cidx;
2323e035717eSSean Bruno 	bus_dmamap_t map;
23244c7070dbSScott Long 	iflib_fl_t fl;
23254c7070dbSScott Long 	iflib_dma_info_t di;
2326e035717eSSean Bruno 	int next;
23274c7070dbSScott Long 
232895246abbSSean Bruno 	map = NULL;
23294c7070dbSScott Long 	flid = irf->irf_flid;
23304c7070dbSScott Long 	cidx = irf->irf_idx;
23314c7070dbSScott Long 	fl = &rxq->ifr_fl[flid];
233295246abbSSean Bruno 	sd->ifsd_fl = fl;
233395246abbSSean Bruno 	sd->ifsd_cidx = cidx;
233495246abbSSean Bruno 	sd->ifsd_m = &fl->ifl_sds.ifsd_m[cidx];
233595246abbSSean Bruno 	sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
23364c7070dbSScott Long 	fl->ifl_credits--;
23374c7070dbSScott Long #if MEMORY_LOGGING
23384c7070dbSScott Long 	fl->ifl_m_dequeued++;
23394c7070dbSScott Long #endif
234095246abbSSean Bruno 	if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
2341e035717eSSean Bruno 		prefetch_pkts(fl, cidx);
2342e035717eSSean Bruno 	if (fl->ifl_sds.ifsd_map != NULL) {
2343e035717eSSean Bruno 		next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2344e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_map[next]);
2345e035717eSSean Bruno 		map = fl->ifl_sds.ifsd_map[cidx];
23464c7070dbSScott Long 		di = fl->ifl_ifdi;
2347e035717eSSean Bruno 		next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1);
2348e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_flags[next]);
23494c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
23504c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
23514c7070dbSScott Long 
23524c7070dbSScott Long 	/* not valid assert if bxe really does SGE from non-contiguous elements */
23534c7070dbSScott Long 		MPASS(fl->ifl_cidx == cidx);
23544c7070dbSScott Long 		if (unload)
2355e035717eSSean Bruno 			bus_dmamap_unload(fl->ifl_desc_tag, map);
2356e035717eSSean Bruno 	}
235795246abbSSean Bruno 	fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1);
235895246abbSSean Bruno 	if (__predict_false(fl->ifl_cidx == 0))
23594c7070dbSScott Long 		fl->ifl_gen = 0;
236095246abbSSean Bruno 	if (map != NULL)
236195246abbSSean Bruno 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
236295246abbSSean Bruno 			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
236387890dbaSSean Bruno         bit_clear(fl->ifl_rx_bitmap, cidx);
23644c7070dbSScott Long }
23654c7070dbSScott Long 
23664c7070dbSScott Long static struct mbuf *
236795246abbSSean Bruno assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd)
23684c7070dbSScott Long {
236995246abbSSean Bruno 	int i, padlen , flags;
237095246abbSSean Bruno 	struct mbuf *m, *mh, *mt;
237195246abbSSean Bruno 	caddr_t cl;
23724c7070dbSScott Long 
23734c7070dbSScott Long 	i = 0;
237423ac9029SStephen Hurd 	mh = NULL;
23754c7070dbSScott Long 	do {
237695246abbSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[i], TRUE, sd);
23774c7070dbSScott Long 
237895246abbSSean Bruno 		MPASS(*sd->ifsd_cl != NULL);
237995246abbSSean Bruno 		MPASS(*sd->ifsd_m != NULL);
238023ac9029SStephen Hurd 
238123ac9029SStephen Hurd 		/* Don't include zero-length frags */
238223ac9029SStephen Hurd 		if (ri->iri_frags[i].irf_len == 0) {
238323ac9029SStephen Hurd 			/* XXX we can save the cluster here, but not the mbuf */
238495246abbSSean Bruno 			m_init(*sd->ifsd_m, M_NOWAIT, MT_DATA, 0);
238595246abbSSean Bruno 			m_free(*sd->ifsd_m);
238695246abbSSean Bruno 			*sd->ifsd_m = NULL;
238723ac9029SStephen Hurd 			continue;
238823ac9029SStephen Hurd 		}
238995246abbSSean Bruno 		m = *sd->ifsd_m;
239095246abbSSean Bruno 		*sd->ifsd_m = NULL;
239123ac9029SStephen Hurd 		if (mh == NULL) {
23924c7070dbSScott Long 			flags = M_PKTHDR|M_EXT;
23934c7070dbSScott Long 			mh = mt = m;
23944c7070dbSScott Long 			padlen = ri->iri_pad;
23954c7070dbSScott Long 		} else {
23964c7070dbSScott Long 			flags = M_EXT;
23974c7070dbSScott Long 			mt->m_next = m;
23984c7070dbSScott Long 			mt = m;
23994c7070dbSScott Long 			/* assuming padding is only on the first fragment */
24004c7070dbSScott Long 			padlen = 0;
24014c7070dbSScott Long 		}
240295246abbSSean Bruno 		cl = *sd->ifsd_cl;
240395246abbSSean Bruno 		*sd->ifsd_cl = NULL;
24044c7070dbSScott Long 
24054c7070dbSScott Long 		/* Can these two be made one ? */
24064c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, flags);
240795246abbSSean Bruno 		m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
24084c7070dbSScott Long 		/*
24094c7070dbSScott Long 		 * These must follow m_init and m_cljset
24104c7070dbSScott Long 		 */
24114c7070dbSScott Long 		m->m_data += padlen;
24124c7070dbSScott Long 		ri->iri_len -= padlen;
241323ac9029SStephen Hurd 		m->m_len = ri->iri_frags[i].irf_len;
24144c7070dbSScott Long 	} while (++i < ri->iri_nfrags);
24154c7070dbSScott Long 
24164c7070dbSScott Long 	return (mh);
24174c7070dbSScott Long }
24184c7070dbSScott Long 
24194c7070dbSScott Long /*
24204c7070dbSScott Long  * Process one software descriptor
24214c7070dbSScott Long  */
24224c7070dbSScott Long static struct mbuf *
24234c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
24244c7070dbSScott Long {
242595246abbSSean Bruno 	struct if_rxsd sd;
24264c7070dbSScott Long 	struct mbuf *m;
24274c7070dbSScott Long 
24284c7070dbSScott Long 	/* should I merge this back in now that the two paths are basically duplicated? */
242923ac9029SStephen Hurd 	if (ri->iri_nfrags == 1 &&
243023ac9029SStephen Hurd 	    ri->iri_frags[0].irf_len <= IFLIB_RX_COPY_THRESH) {
243195246abbSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[0], FALSE, &sd);
243295246abbSSean Bruno 		m = *sd.ifsd_m;
243395246abbSSean Bruno 		*sd.ifsd_m = NULL;
24344c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
243595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
243695246abbSSean Bruno 		if (!IP_ALIGNED(m))
243795246abbSSean Bruno 			m->m_data += 2;
243895246abbSSean Bruno #endif
243995246abbSSean Bruno 		memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
244023ac9029SStephen Hurd 		m->m_len = ri->iri_frags[0].irf_len;
24414c7070dbSScott Long        } else {
244295246abbSSean Bruno 		m = assemble_segments(rxq, ri, &sd);
24434c7070dbSScott Long 	}
24444c7070dbSScott Long 	m->m_pkthdr.len = ri->iri_len;
24454c7070dbSScott Long 	m->m_pkthdr.rcvif = ri->iri_ifp;
24464c7070dbSScott Long 	m->m_flags |= ri->iri_flags;
24474c7070dbSScott Long 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
24484c7070dbSScott Long 	m->m_pkthdr.flowid = ri->iri_flowid;
24494c7070dbSScott Long 	M_HASHTYPE_SET(m, ri->iri_rsstype);
24504c7070dbSScott Long 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
24514c7070dbSScott Long 	m->m_pkthdr.csum_data = ri->iri_csum_data;
24524c7070dbSScott Long 	return (m);
24534c7070dbSScott Long }
24544c7070dbSScott Long 
24554c7070dbSScott Long static bool
245695246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
24574c7070dbSScott Long {
24584c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
24594c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
246023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
24614c7070dbSScott Long 	int avail, i;
246295246abbSSean Bruno 	qidx_t *cidxp;
24634c7070dbSScott Long 	struct if_rxd_info ri;
24644c7070dbSScott Long 	int err, budget_left, rx_bytes, rx_pkts;
24654c7070dbSScott Long 	iflib_fl_t fl;
24664c7070dbSScott Long 	struct ifnet *ifp;
24674c7070dbSScott Long 	int lro_enabled;
246895246abbSSean Bruno 
24694c7070dbSScott Long 	/*
24704c7070dbSScott Long 	 * XXX early demux data packets so that if_input processing only handles
24714c7070dbSScott Long 	 * acks in interrupt context
24724c7070dbSScott Long 	 */
2473ab2e3f79SStephen Hurd 	struct mbuf *m, *mh, *mt;
24744c7070dbSScott Long 
247595246abbSSean Bruno 	ifp = ctx->ifc_ifp;
24764c7070dbSScott Long 	mh = mt = NULL;
24774c7070dbSScott Long 	MPASS(budget > 0);
24784c7070dbSScott Long 	rx_pkts	= rx_bytes = 0;
247923ac9029SStephen Hurd 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
24804c7070dbSScott Long 		cidxp = &rxq->ifr_cq_cidx;
24814c7070dbSScott Long 	else
24824c7070dbSScott Long 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
248323ac9029SStephen Hurd 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
24844c7070dbSScott Long 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
24854c7070dbSScott Long 			__iflib_fl_refill_lt(ctx, fl, budget + 8);
24864c7070dbSScott Long 		DBG_COUNTER_INC(rx_unavail);
24874c7070dbSScott Long 		return (false);
24884c7070dbSScott Long 	}
24894c7070dbSScott Long 
24904c7070dbSScott Long 	for (budget_left = budget; (budget_left > 0) && (avail > 0); budget_left--, avail--) {
24914c7070dbSScott Long 		if (__predict_false(!CTX_ACTIVE(ctx))) {
24924c7070dbSScott Long 			DBG_COUNTER_INC(rx_ctx_inactive);
24934c7070dbSScott Long 			break;
24944c7070dbSScott Long 		}
24954c7070dbSScott Long 		/*
24964c7070dbSScott Long 		 * Reset client set fields to their default values
24974c7070dbSScott Long 		 */
249895246abbSSean Bruno 		rxd_info_zero(&ri);
24994c7070dbSScott Long 		ri.iri_qsidx = rxq->ifr_id;
25004c7070dbSScott Long 		ri.iri_cidx = *cidxp;
250195246abbSSean Bruno 		ri.iri_ifp = ifp;
25024c7070dbSScott Long 		ri.iri_frags = rxq->ifr_frags;
25034c7070dbSScott Long 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
25044c7070dbSScott Long 
250595246abbSSean Bruno 		if (err)
250695246abbSSean Bruno 			goto err;
250723ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
250823ac9029SStephen Hurd 			*cidxp = ri.iri_cidx;
250923ac9029SStephen Hurd 			/* Update our consumer index */
251095246abbSSean Bruno 			/* XXX NB: shurd - check if this is still safe */
251123ac9029SStephen Hurd 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) {
251223ac9029SStephen Hurd 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
25134c7070dbSScott Long 				rxq->ifr_cq_gen = 0;
25144c7070dbSScott Long 			}
25154c7070dbSScott Long 			/* was this only a completion queue message? */
25164c7070dbSScott Long 			if (__predict_false(ri.iri_nfrags == 0))
25174c7070dbSScott Long 				continue;
25184c7070dbSScott Long 		}
25194c7070dbSScott Long 		MPASS(ri.iri_nfrags != 0);
25204c7070dbSScott Long 		MPASS(ri.iri_len != 0);
25214c7070dbSScott Long 
25224c7070dbSScott Long 		/* will advance the cidx on the corresponding free lists */
25234c7070dbSScott Long 		m = iflib_rxd_pkt_get(rxq, &ri);
25244c7070dbSScott Long 		if (avail == 0 && budget_left)
252523ac9029SStephen Hurd 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
25264c7070dbSScott Long 
25274c7070dbSScott Long 		if (__predict_false(m == NULL)) {
25284c7070dbSScott Long 			DBG_COUNTER_INC(rx_mbuf_null);
25294c7070dbSScott Long 			continue;
25304c7070dbSScott Long 		}
25314c7070dbSScott Long 		/* imm_pkt: -- cxgb */
25324c7070dbSScott Long 		if (mh == NULL)
25334c7070dbSScott Long 			mh = mt = m;
25344c7070dbSScott Long 		else {
25354c7070dbSScott Long 			mt->m_nextpkt = m;
25364c7070dbSScott Long 			mt = m;
25374c7070dbSScott Long 		}
25384c7070dbSScott Long 	}
25394c7070dbSScott Long 	/* make sure that we can refill faster than drain */
25404c7070dbSScott Long 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2541ab2e3f79SStephen Hurd 		__iflib_fl_refill_lt(ctx, fl, budget + 8);
25424c7070dbSScott Long 
25434c7070dbSScott Long 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
25444c7070dbSScott Long 	while (mh != NULL) {
25454c7070dbSScott Long 		m = mh;
25464c7070dbSScott Long 		mh = mh->m_nextpkt;
25474c7070dbSScott Long 		m->m_nextpkt = NULL;
254895246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
254995246abbSSean Bruno 		if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
255095246abbSSean Bruno 			continue;
255195246abbSSean Bruno #endif
25524c7070dbSScott Long 		rx_bytes += m->m_pkthdr.len;
25534c7070dbSScott Long 		rx_pkts++;
2554aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
2555ab2e3f79SStephen Hurd 		if (lro_enabled && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
25564c7070dbSScott Long 			continue;
2557aaeb188aSBjoern A. Zeeb #endif
25584c7070dbSScott Long 		DBG_COUNTER_INC(rx_if_input);
2559ab2e3f79SStephen Hurd 		ifp->if_input(ifp, m);
25604c7070dbSScott Long 	}
256123ac9029SStephen Hurd 
25624c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
25634c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
25644c7070dbSScott Long 
25654c7070dbSScott Long 	/*
25664c7070dbSScott Long 	 * Flush any outstanding LRO work
25674c7070dbSScott Long 	 */
2568aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
256923ac9029SStephen Hurd 	tcp_lro_flush_all(&rxq->ifr_lc);
2570aaeb188aSBjoern A. Zeeb #endif
2571ab2e3f79SStephen Hurd 	if (avail)
2572ab2e3f79SStephen Hurd 		return true;
2573ab2e3f79SStephen Hurd 	return (iflib_rxd_avail(ctx, rxq, *cidxp, 1));
257495246abbSSean Bruno err:
2575ab2e3f79SStephen Hurd 	CTX_LOCK(ctx);
2576ab2e3f79SStephen Hurd 	ctx->ifc_flags |= IFC_DO_RESET;
2577ab2e3f79SStephen Hurd 	iflib_admin_intr_deferred(ctx);
2578ab2e3f79SStephen Hurd 	CTX_UNLOCK(ctx);
257995246abbSSean Bruno 	return (false);
258095246abbSSean Bruno }
258195246abbSSean Bruno 
258295246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1)
258395246abbSSean Bruno static inline qidx_t
258495246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
258595246abbSSean Bruno {
258695246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
258795246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
258895246abbSSean Bruno 	if (in_use > 4*minthresh)
258995246abbSSean Bruno 		return (notify_count);
259095246abbSSean Bruno 	if (in_use > 2*minthresh)
259195246abbSSean Bruno 		return (notify_count >> 1);
259295246abbSSean Bruno 	if (in_use > minthresh)
259395246abbSSean Bruno 		return (notify_count >> 3);
259495246abbSSean Bruno 	return (0);
259595246abbSSean Bruno }
259695246abbSSean Bruno 
259795246abbSSean Bruno static inline qidx_t
259895246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq)
259995246abbSSean Bruno {
260095246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
260195246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
260295246abbSSean Bruno 	if (txq->ift_in_use > 4*minthresh)
260395246abbSSean Bruno 		return (notify_count);
260495246abbSSean Bruno 	if (txq->ift_in_use > 2*minthresh)
260595246abbSSean Bruno 		return (notify_count >> 1);
260695246abbSSean Bruno 	if (txq->ift_in_use > minthresh)
260795246abbSSean Bruno 		return (notify_count >> 2);
26082b2fc973SSean Bruno 	return (2);
26094c7070dbSScott Long }
26104c7070dbSScott Long 
26114c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
26124c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
261395246abbSSean Bruno 
261495246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
261595246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
261623ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
26174c7070dbSScott Long 
261895246abbSSean Bruno /* forward compatibility for cxgb */
261995246abbSSean Bruno #define FIRST_QSET(ctx) 0
262095246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
262195246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
262295246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
262395246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
262495246abbSSean Bruno 
262595246abbSSean Bruno /* XXX we should be setting this to something other than zero */
262695246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
262795246abbSSean Bruno #define MAX_TX_DESC(ctx) ((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max)
262895246abbSSean Bruno 
262995246abbSSean Bruno static inline bool
263095246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use)
26314c7070dbSScott Long {
263295246abbSSean Bruno 	qidx_t dbval, max;
263395246abbSSean Bruno 	bool rang;
26344c7070dbSScott Long 
263595246abbSSean Bruno 	rang = false;
263695246abbSSean Bruno 	max = TXQ_MAX_DB_DEFERRED(txq, in_use);
263795246abbSSean Bruno 	if (ring || txq->ift_db_pending >= max) {
26384c7070dbSScott Long 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
26394c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
26404c7070dbSScott Long 		txq->ift_db_pending = txq->ift_npending = 0;
264195246abbSSean Bruno 		rang = true;
26424c7070dbSScott Long 	}
264395246abbSSean Bruno 	return (rang);
26444c7070dbSScott Long }
26454c7070dbSScott Long 
26464c7070dbSScott Long #ifdef PKT_DEBUG
26474c7070dbSScott Long static void
26484c7070dbSScott Long print_pkt(if_pkt_info_t pi)
26494c7070dbSScott Long {
26504c7070dbSScott Long 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
26514c7070dbSScott Long 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
26524c7070dbSScott Long 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
26534c7070dbSScott Long 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
26544c7070dbSScott Long 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
26554c7070dbSScott Long 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
26564c7070dbSScott Long }
26574c7070dbSScott Long #endif
26584c7070dbSScott Long 
26594c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
26604c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
26614c7070dbSScott Long 
26624c7070dbSScott Long static int
26634c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
26644c7070dbSScott Long {
2665ab2e3f79SStephen Hurd 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
26664c7070dbSScott Long 	struct ether_vlan_header *eh;
266723ac9029SStephen Hurd 	struct mbuf *m, *n;
26684c7070dbSScott Long 
266923ac9029SStephen Hurd 	n = m = *mp;
2670ab2e3f79SStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
2671ab2e3f79SStephen Hurd 	    M_WRITABLE(m) == 0) {
2672ab2e3f79SStephen Hurd 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
2673ab2e3f79SStephen Hurd 			return (ENOMEM);
2674ab2e3f79SStephen Hurd 		} else {
2675ab2e3f79SStephen Hurd 			m_freem(*mp);
2676ab2e3f79SStephen Hurd 			n = *mp = m;
2677ab2e3f79SStephen Hurd 		}
2678ab2e3f79SStephen Hurd 	}
26791248952aSSean Bruno 
26804c7070dbSScott Long 	/*
26814c7070dbSScott Long 	 * Determine where frame payload starts.
26824c7070dbSScott Long 	 * Jump over vlan headers if already present,
26834c7070dbSScott Long 	 * helpful for QinQ too.
26844c7070dbSScott Long 	 */
26854c7070dbSScott Long 	if (__predict_false(m->m_len < sizeof(*eh))) {
26864c7070dbSScott Long 		txq->ift_pullups++;
26874c7070dbSScott Long 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
26884c7070dbSScott Long 			return (ENOMEM);
26894c7070dbSScott Long 	}
26904c7070dbSScott Long 	eh = mtod(m, struct ether_vlan_header *);
26914c7070dbSScott Long 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
26924c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_proto);
26934c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
26944c7070dbSScott Long 	} else {
26954c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
26964c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
26974c7070dbSScott Long 	}
26984c7070dbSScott Long 
26994c7070dbSScott Long 	switch (pi->ipi_etype) {
27004c7070dbSScott Long #ifdef INET
27014c7070dbSScott Long 	case ETHERTYPE_IP:
27024c7070dbSScott Long 	{
27034c7070dbSScott Long 		struct ip *ip = NULL;
27044c7070dbSScott Long 		struct tcphdr *th = NULL;
27054c7070dbSScott Long 		int minthlen;
27064c7070dbSScott Long 
27074c7070dbSScott Long 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
27084c7070dbSScott Long 		if (__predict_false(m->m_len < minthlen)) {
27094c7070dbSScott Long 			/*
27104c7070dbSScott Long 			 * if this code bloat is causing too much of a hit
27114c7070dbSScott Long 			 * move it to a separate function and mark it noinline
27124c7070dbSScott Long 			 */
27134c7070dbSScott Long 			if (m->m_len == pi->ipi_ehdrlen) {
27144c7070dbSScott Long 				n = m->m_next;
27154c7070dbSScott Long 				MPASS(n);
27164c7070dbSScott Long 				if (n->m_len >= sizeof(*ip))  {
27174c7070dbSScott Long 					ip = (struct ip *)n->m_data;
27184c7070dbSScott Long 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
27194c7070dbSScott Long 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
27204c7070dbSScott Long 				} else {
27214c7070dbSScott Long 					txq->ift_pullups++;
27224c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
27234c7070dbSScott Long 						return (ENOMEM);
27244c7070dbSScott Long 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
27254c7070dbSScott Long 				}
27264c7070dbSScott Long 			} else {
27274c7070dbSScott Long 				txq->ift_pullups++;
27284c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
27294c7070dbSScott Long 					return (ENOMEM);
27304c7070dbSScott Long 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
27314c7070dbSScott Long 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
27324c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
27334c7070dbSScott Long 			}
27344c7070dbSScott Long 		} else {
27354c7070dbSScott Long 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
27364c7070dbSScott Long 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
27374c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
27384c7070dbSScott Long 		}
27394c7070dbSScott Long 		pi->ipi_ip_hlen = ip->ip_hl << 2;
27404c7070dbSScott Long 		pi->ipi_ipproto = ip->ip_p;
27414c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV4;
27424c7070dbSScott Long 
2743ab2e3f79SStephen Hurd 		if (pi->ipi_csum_flags & CSUM_IP)
27444c7070dbSScott Long                        ip->ip_sum = 0;
27454c7070dbSScott Long 
27464c7070dbSScott Long 		if (pi->ipi_ipproto == IPPROTO_TCP) {
27474c7070dbSScott Long 			if (__predict_false(th == NULL)) {
27484c7070dbSScott Long 				txq->ift_pullups++;
27494c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
27504c7070dbSScott Long 					return (ENOMEM);
27514c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
27524c7070dbSScott Long 			}
27534c7070dbSScott Long 			pi->ipi_tcp_hflags = th->th_flags;
27544c7070dbSScott Long 			pi->ipi_tcp_hlen = th->th_off << 2;
27554c7070dbSScott Long 			pi->ipi_tcp_seq = th->th_seq;
27564c7070dbSScott Long 		}
2757ab2e3f79SStephen Hurd 		if (IS_TSO4(pi)) {
27584c7070dbSScott Long 			if (__predict_false(ip->ip_p != IPPROTO_TCP))
27594c7070dbSScott Long 				return (ENXIO);
27604c7070dbSScott Long 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
27614c7070dbSScott Long 					       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
27624c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
27631248952aSSean Bruno 			if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
27641248952aSSean Bruno 				ip->ip_sum = 0;
27651248952aSSean Bruno 				ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
27661248952aSSean Bruno 			}
27674c7070dbSScott Long 		}
27684c7070dbSScott Long 		break;
27694c7070dbSScott Long 	}
27704c7070dbSScott Long #endif
27714c7070dbSScott Long #ifdef INET6
27724c7070dbSScott Long 	case ETHERTYPE_IPV6:
27734c7070dbSScott Long 	{
27744c7070dbSScott Long 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
27754c7070dbSScott Long 		struct tcphdr *th;
27764c7070dbSScott Long 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
27774c7070dbSScott Long 
27784c7070dbSScott Long 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
27794c7070dbSScott Long 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
27804c7070dbSScott Long 				return (ENOMEM);
27814c7070dbSScott Long 		}
27824c7070dbSScott Long 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
27834c7070dbSScott Long 
27844c7070dbSScott Long 		/* XXX-BZ this will go badly in case of ext hdrs. */
27854c7070dbSScott Long 		pi->ipi_ipproto = ip6->ip6_nxt;
27864c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV6;
27874c7070dbSScott Long 
27884c7070dbSScott Long 		if (pi->ipi_ipproto == IPPROTO_TCP) {
27894c7070dbSScott Long 			if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
27904c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
27914c7070dbSScott Long 					return (ENOMEM);
27924c7070dbSScott Long 			}
27934c7070dbSScott Long 			pi->ipi_tcp_hflags = th->th_flags;
27944c7070dbSScott Long 			pi->ipi_tcp_hlen = th->th_off << 2;
27954c7070dbSScott Long 		}
2796ab2e3f79SStephen Hurd 		if (IS_TSO6(pi)) {
27974c7070dbSScott Long 
27984c7070dbSScott Long 			if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
27994c7070dbSScott Long 				return (ENXIO);
28004c7070dbSScott Long 			/*
28014c7070dbSScott Long 			 * The corresponding flag is set by the stack in the IPv4
28024c7070dbSScott Long 			 * TSO case, but not in IPv6 (at least in FreeBSD 10.2).
28034c7070dbSScott Long 			 * So, set it here because the rest of the flow requires it.
28044c7070dbSScott Long 			 */
28054c7070dbSScott Long 			pi->ipi_csum_flags |= CSUM_TCP_IPV6;
28064c7070dbSScott Long 			th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
28074c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
28084c7070dbSScott Long 		}
28094c7070dbSScott Long 		break;
28104c7070dbSScott Long 	}
28114c7070dbSScott Long #endif
28124c7070dbSScott Long 	default:
28134c7070dbSScott Long 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
28144c7070dbSScott Long 		pi->ipi_ip_hlen = 0;
28154c7070dbSScott Long 		break;
28164c7070dbSScott Long 	}
28174c7070dbSScott Long 	*mp = m;
28181248952aSSean Bruno 
28194c7070dbSScott Long 	return (0);
28204c7070dbSScott Long }
28214c7070dbSScott Long 
28224c7070dbSScott Long static  __noinline  struct mbuf *
28234c7070dbSScott Long collapse_pkthdr(struct mbuf *m0)
28244c7070dbSScott Long {
28254c7070dbSScott Long 	struct mbuf *m, *m_next, *tmp;
28264c7070dbSScott Long 
28274c7070dbSScott Long 	m = m0;
28284c7070dbSScott Long 	m_next = m->m_next;
28294c7070dbSScott Long 	while (m_next != NULL && m_next->m_len == 0) {
28304c7070dbSScott Long 		m = m_next;
28314c7070dbSScott Long 		m->m_next = NULL;
28324c7070dbSScott Long 		m_free(m);
28334c7070dbSScott Long 		m_next = m_next->m_next;
28344c7070dbSScott Long 	}
28354c7070dbSScott Long 	m = m0;
28364c7070dbSScott Long 	m->m_next = m_next;
28374c7070dbSScott Long 	if ((m_next->m_flags & M_EXT) == 0) {
28384c7070dbSScott Long 		m = m_defrag(m, M_NOWAIT);
28394c7070dbSScott Long 	} else {
28404c7070dbSScott Long 		tmp = m_next->m_next;
28414c7070dbSScott Long 		memcpy(m_next, m, MPKTHSIZE);
28424c7070dbSScott Long 		m = m_next;
28434c7070dbSScott Long 		m->m_next = tmp;
28444c7070dbSScott Long 	}
28454c7070dbSScott Long 	return (m);
28464c7070dbSScott Long }
28474c7070dbSScott Long 
28484c7070dbSScott Long /*
28494c7070dbSScott Long  * If dodgy hardware rejects the scatter gather chain we've handed it
285023ac9029SStephen Hurd  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
285123ac9029SStephen Hurd  * m_defrag'd mbufs
28524c7070dbSScott Long  */
28534c7070dbSScott Long static __noinline struct mbuf *
285423ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq)
28554c7070dbSScott Long {
285623ac9029SStephen Hurd 	int ntxd, i, pidx;
28574c7070dbSScott Long 	struct mbuf *m, *mh, **ifsd_m;
28584c7070dbSScott Long 
28594c7070dbSScott Long 	pidx = txq->ift_pidx;
28604c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
286123ac9029SStephen Hurd 	ntxd = txq->ift_size;
28624c7070dbSScott Long 	mh = m = ifsd_m[pidx];
28634c7070dbSScott Long 	ifsd_m[pidx] = NULL;
28644c7070dbSScott Long #if MEMORY_LOGGING
28654c7070dbSScott Long 	txq->ift_dequeued++;
28664c7070dbSScott Long #endif
28674c7070dbSScott Long 	i = 1;
28684c7070dbSScott Long 
286923ac9029SStephen Hurd 	while (m) {
28704c7070dbSScott Long 		ifsd_m[(pidx + i) & (ntxd -1)] = NULL;
28714c7070dbSScott Long #if MEMORY_LOGGING
28724c7070dbSScott Long 		txq->ift_dequeued++;
28734c7070dbSScott Long #endif
28744c7070dbSScott Long 		m = m->m_next;
28754c7070dbSScott Long 		i++;
28764c7070dbSScott Long 	}
28774c7070dbSScott Long 	return (mh);
28784c7070dbSScott Long }
28794c7070dbSScott Long 
28804c7070dbSScott Long static int
28814c7070dbSScott Long iflib_busdma_load_mbuf_sg(iflib_txq_t txq, bus_dma_tag_t tag, bus_dmamap_t map,
28824c7070dbSScott Long 			  struct mbuf **m0, bus_dma_segment_t *segs, int *nsegs,
28834c7070dbSScott Long 			  int max_segs, int flags)
28844c7070dbSScott Long {
28854c7070dbSScott Long 	if_ctx_t ctx;
28864c7070dbSScott Long 	if_shared_ctx_t		sctx;
288723ac9029SStephen Hurd 	if_softc_ctx_t		scctx;
288825d52811SSean Bruno 	int i, next, pidx, err, ntxd, count;
2889aa8fa07cSSean Bruno 	struct mbuf *m, *tmp, **ifsd_m;
28904c7070dbSScott Long 
28914c7070dbSScott Long 	m = *m0;
28924c7070dbSScott Long 
28934c7070dbSScott Long 	/*
28944c7070dbSScott Long 	 * Please don't ever do this
28954c7070dbSScott Long 	 */
28964c7070dbSScott Long 	if (__predict_false(m->m_len == 0))
28974c7070dbSScott Long 		*m0 = m = collapse_pkthdr(m);
28984c7070dbSScott Long 
28994c7070dbSScott Long 	ctx = txq->ift_ctx;
29004c7070dbSScott Long 	sctx = ctx->ifc_sctx;
290123ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
29024c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
290323ac9029SStephen Hurd 	ntxd = txq->ift_size;
29044c7070dbSScott Long 	pidx = txq->ift_pidx;
2905ab2e3f79SStephen Hurd 	if (map != NULL) {
29064c7070dbSScott Long 		uint8_t *ifsd_flags = txq->ift_sds.ifsd_flags;
2907ab2e3f79SStephen Hurd 
29084c7070dbSScott Long 		err = bus_dmamap_load_mbuf_sg(tag, map,
29094c7070dbSScott Long 					      *m0, segs, nsegs, BUS_DMA_NOWAIT);
29104c7070dbSScott Long 		if (err)
29114c7070dbSScott Long 			return (err);
29124c7070dbSScott Long 		ifsd_flags[pidx] |= TX_SW_DESC_MAPPED;
2913aa8fa07cSSean Bruno 		count = 0;
29144c7070dbSScott Long 		m = *m0;
29154c7070dbSScott Long 		do {
2916aa8fa07cSSean Bruno 			if (__predict_false(m->m_len <= 0)) {
2917aa8fa07cSSean Bruno 				tmp = m;
29184c7070dbSScott Long 				m = m->m_next;
2919aa8fa07cSSean Bruno 				tmp->m_next = NULL;
2920aa8fa07cSSean Bruno 				m_free(tmp);
2921aa8fa07cSSean Bruno 				continue;
2922aa8fa07cSSean Bruno 			}
2923dcbc025fSSean Bruno 			m = m->m_next;
2924dcbc025fSSean Bruno 			count++;
2925dcbc025fSSean Bruno 		} while (m != NULL);
29265c5ca36cSSean Bruno 		if (count > *nsegs) {
29275c5ca36cSSean Bruno 			ifsd_m[pidx] = *m0;
29285c5ca36cSSean Bruno 			ifsd_m[pidx]->m_flags |= M_TOOBIG;
2929dcbc025fSSean Bruno 			return (0);
29305c5ca36cSSean Bruno 		}
2931dcbc025fSSean Bruno 		m = *m0;
2932dcbc025fSSean Bruno 		count = 0;
2933dcbc025fSSean Bruno 		do {
2934aa8fa07cSSean Bruno 			next = (pidx + count) & (ntxd-1);
2935aa8fa07cSSean Bruno 			MPASS(ifsd_m[next] == NULL);
2936aa8fa07cSSean Bruno 			ifsd_m[next] = m;
2937aa8fa07cSSean Bruno 			count++;
2938aa8fa07cSSean Bruno 			tmp = m;
2939aa8fa07cSSean Bruno 			m = m->m_next;
29404c7070dbSScott Long 		} while (m != NULL);
29414c7070dbSScott Long 	} else {
294225d52811SSean Bruno 		int buflen, sgsize, maxsegsz, max_sgsize;
29434c7070dbSScott Long 		vm_offset_t vaddr;
29444c7070dbSScott Long 		vm_paddr_t curaddr;
29454c7070dbSScott Long 
29464c7070dbSScott Long 		count = i = 0;
29474c7070dbSScott Long 		m = *m0;
294825d52811SSean Bruno 		if (m->m_pkthdr.csum_flags & CSUM_TSO)
294925d52811SSean Bruno 			maxsegsz = scctx->isc_tx_tso_segsize_max;
295025d52811SSean Bruno 		else
295125d52811SSean Bruno 			maxsegsz = sctx->isc_tx_maxsegsize;
295225d52811SSean Bruno 
29534c7070dbSScott Long 		do {
29544c7070dbSScott Long 			if (__predict_false(m->m_len <= 0)) {
29554c7070dbSScott Long 				tmp = m;
29564c7070dbSScott Long 				m = m->m_next;
29574c7070dbSScott Long 				tmp->m_next = NULL;
29584c7070dbSScott Long 				m_free(tmp);
29594c7070dbSScott Long 				continue;
29604c7070dbSScott Long 			}
29614c7070dbSScott Long 			buflen = m->m_len;
29624c7070dbSScott Long 			vaddr = (vm_offset_t)m->m_data;
29634c7070dbSScott Long 			/*
29644c7070dbSScott Long 			 * see if we can't be smarter about physically
29654c7070dbSScott Long 			 * contiguous mappings
29664c7070dbSScott Long 			 */
29674c7070dbSScott Long 			next = (pidx + count) & (ntxd-1);
29684c7070dbSScott Long 			MPASS(ifsd_m[next] == NULL);
29694c7070dbSScott Long #if MEMORY_LOGGING
29704c7070dbSScott Long 			txq->ift_enqueued++;
29714c7070dbSScott Long #endif
29724c7070dbSScott Long 			ifsd_m[next] = m;
29734c7070dbSScott Long 			while (buflen > 0) {
29749d35858fSSean Bruno 				if (i >= max_segs)
29759d35858fSSean Bruno 					goto err;
29764c7070dbSScott Long 				max_sgsize = MIN(buflen, maxsegsz);
29774c7070dbSScott Long 				curaddr = pmap_kextract(vaddr);
29784c7070dbSScott Long 				sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
29794c7070dbSScott Long 				sgsize = MIN(sgsize, max_sgsize);
29804c7070dbSScott Long 				segs[i].ds_addr = curaddr;
29814c7070dbSScott Long 				segs[i].ds_len = sgsize;
29824c7070dbSScott Long 				vaddr += sgsize;
29834c7070dbSScott Long 				buflen -= sgsize;
29844c7070dbSScott Long 				i++;
29854c7070dbSScott Long 			}
29864c7070dbSScott Long 			count++;
29874c7070dbSScott Long 			tmp = m;
29884c7070dbSScott Long 			m = m->m_next;
29894c7070dbSScott Long 		} while (m != NULL);
29904c7070dbSScott Long 		*nsegs = i;
29914c7070dbSScott Long 	}
29924c7070dbSScott Long 	return (0);
29934c7070dbSScott Long err:
299423ac9029SStephen Hurd 	*m0 = iflib_remove_mbuf(txq);
29954c7070dbSScott Long 	return (EFBIG);
29964c7070dbSScott Long }
29974c7070dbSScott Long 
299895246abbSSean Bruno static inline caddr_t
299995246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid)
300095246abbSSean Bruno {
300195246abbSSean Bruno 	qidx_t size;
300295246abbSSean Bruno 	int ntxd;
300395246abbSSean Bruno 	caddr_t start, end, cur, next;
300495246abbSSean Bruno 
300595246abbSSean Bruno 	ntxd = txq->ift_size;
300695246abbSSean Bruno 	size = txq->ift_txd_size[qid];
300795246abbSSean Bruno 	start = txq->ift_ifdi[qid].idi_vaddr;
300895246abbSSean Bruno 
300995246abbSSean Bruno 	if (__predict_false(size == 0))
301095246abbSSean Bruno 		return (start);
301195246abbSSean Bruno 	cur = start + size*cidx;
301295246abbSSean Bruno 	end = start + size*ntxd;
301395246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
301495246abbSSean Bruno 	return (next < end ? next : start);
301595246abbSSean Bruno }
301695246abbSSean Bruno 
30174c7070dbSScott Long static int
30184c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
30194c7070dbSScott Long {
30204c7070dbSScott Long 	if_ctx_t		ctx;
30214c7070dbSScott Long 	if_shared_ctx_t		sctx;
30224c7070dbSScott Long 	if_softc_ctx_t		scctx;
30234c7070dbSScott Long 	bus_dma_segment_t	*segs;
30244c7070dbSScott Long 	struct mbuf		*m_head;
302595246abbSSean Bruno 	void			*next_txd;
30264c7070dbSScott Long 	bus_dmamap_t		map;
30274c7070dbSScott Long 	struct if_pkt_info	pi;
30284c7070dbSScott Long 	int remap = 0;
30294c7070dbSScott Long 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
30304c7070dbSScott Long 	bus_dma_tag_t desc_tag;
30314c7070dbSScott Long 
30324c7070dbSScott Long 	segs = txq->ift_segs;
30334c7070dbSScott Long 	ctx = txq->ift_ctx;
30344c7070dbSScott Long 	sctx = ctx->ifc_sctx;
30354c7070dbSScott Long 	scctx = &ctx->ifc_softc_ctx;
30364c7070dbSScott Long 	segs = txq->ift_segs;
303723ac9029SStephen Hurd 	ntxd = txq->ift_size;
30384c7070dbSScott Long 	m_head = *m_headp;
30394c7070dbSScott Long 	map = NULL;
30404c7070dbSScott Long 
30414c7070dbSScott Long 	/*
30424c7070dbSScott Long 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
30434c7070dbSScott Long 	 */
30444c7070dbSScott Long 	cidx = txq->ift_cidx;
30454c7070dbSScott Long 	pidx = txq->ift_pidx;
304695246abbSSean Bruno 	if (ctx->ifc_flags & IFC_PREFETCH) {
30474c7070dbSScott Long 		next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
304895246abbSSean Bruno 		if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) {
304995246abbSSean Bruno 			next_txd = calc_next_txd(txq, cidx, 0);
305095246abbSSean Bruno 			prefetch(next_txd);
305195246abbSSean Bruno 		}
30524c7070dbSScott Long 
30534c7070dbSScott Long 		/* prefetch the next cache line of mbuf pointers and flags */
30544c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_m[next]);
30554c7070dbSScott Long 		if (txq->ift_sds.ifsd_map != NULL) {
30564c7070dbSScott Long 			prefetch(&txq->ift_sds.ifsd_map[next]);
30574c7070dbSScott Long 			next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
30584c7070dbSScott Long 			prefetch(&txq->ift_sds.ifsd_flags[next]);
30594c7070dbSScott Long 		}
3060ab2e3f79SStephen Hurd 	} else if (txq->ift_sds.ifsd_map != NULL)
306195246abbSSean Bruno 		map = txq->ift_sds.ifsd_map[pidx];
30624c7070dbSScott Long 
30634c7070dbSScott Long 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
30644c7070dbSScott Long 		desc_tag = txq->ift_tso_desc_tag;
30654c7070dbSScott Long 		max_segs = scctx->isc_tx_tso_segments_max;
30664c7070dbSScott Long 	} else {
30674c7070dbSScott Long 		desc_tag = txq->ift_desc_tag;
30684c7070dbSScott Long 		max_segs = scctx->isc_tx_nsegments;
30694c7070dbSScott Long 	}
30704c7070dbSScott Long 	m_head = *m_headp;
307195246abbSSean Bruno 
307295246abbSSean Bruno 	pkt_info_zero(&pi);
3073d300df01SStephen Hurd 	pi.ipi_len = m_head->m_pkthdr.len;
3074ab2e3f79SStephen Hurd 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
3075d300df01SStephen Hurd 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
3076d300df01SStephen Hurd 	pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0;
3077ab2e3f79SStephen Hurd 	pi.ipi_pidx = pidx;
3078ab2e3f79SStephen Hurd 	pi.ipi_qsidx = txq->ift_id;
30794c7070dbSScott Long 
30804c7070dbSScott Long 	/* deliberate bitwise OR to make one condition */
30814c7070dbSScott Long 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
30824c7070dbSScott Long 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0))
30834c7070dbSScott Long 			return (err);
30844c7070dbSScott Long 		m_head = *m_headp;
30854c7070dbSScott Long 	}
30864c7070dbSScott Long 
30874c7070dbSScott Long retry:
30884c7070dbSScott Long 	err = iflib_busdma_load_mbuf_sg(txq, desc_tag, map, m_headp, segs, &nsegs, max_segs, BUS_DMA_NOWAIT);
30894c7070dbSScott Long defrag:
30904c7070dbSScott Long 	if (__predict_false(err)) {
30914c7070dbSScott Long 		switch (err) {
30924c7070dbSScott Long 		case EFBIG:
30934c7070dbSScott Long 			/* try collapse once and defrag once */
30944c7070dbSScott Long 			if (remap == 0)
30954c7070dbSScott Long 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
30964c7070dbSScott Long 			if (remap == 1)
30974c7070dbSScott Long 				m_head = m_defrag(*m_headp, M_NOWAIT);
30984c7070dbSScott Long 			remap++;
30994c7070dbSScott Long 			if (__predict_false(m_head == NULL))
31004c7070dbSScott Long 				goto defrag_failed;
31014c7070dbSScott Long 			txq->ift_mbuf_defrag++;
31024c7070dbSScott Long 			*m_headp = m_head;
31034c7070dbSScott Long 			goto retry;
31044c7070dbSScott Long 			break;
31054c7070dbSScott Long 		case ENOMEM:
31064c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
31074c7070dbSScott Long 			break;
31084c7070dbSScott Long 		default:
31094c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
31104c7070dbSScott Long 			m_freem(*m_headp);
31114c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
31124c7070dbSScott Long 			*m_headp = NULL;
31134c7070dbSScott Long 			break;
31144c7070dbSScott Long 		}
31154c7070dbSScott Long 		txq->ift_map_failed++;
31164c7070dbSScott Long 		DBG_COUNTER_INC(encap_load_mbuf_fail);
31174c7070dbSScott Long 		return (err);
31184c7070dbSScott Long 	}
31194c7070dbSScott Long 
31204c7070dbSScott Long 	/*
31214c7070dbSScott Long 	 * XXX assumes a 1 to 1 relationship between segments and
31224c7070dbSScott Long 	 *        descriptors - this does not hold true on all drivers, e.g.
31234c7070dbSScott Long 	 *        cxgb
31244c7070dbSScott Long 	 */
31254c7070dbSScott Long 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
31264c7070dbSScott Long 		txq->ift_no_desc_avail++;
31274c7070dbSScott Long 		if (map != NULL)
31284c7070dbSScott Long 			bus_dmamap_unload(desc_tag, map);
31294c7070dbSScott Long 		DBG_COUNTER_INC(encap_txq_avail_fail);
313023ac9029SStephen Hurd 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
31314c7070dbSScott Long 			GROUPTASK_ENQUEUE(&txq->ift_task);
31324c7070dbSScott Long 		return (ENOBUFS);
31334c7070dbSScott Long 	}
313495246abbSSean Bruno 	/*
313595246abbSSean Bruno 	 * On Intel cards we can greatly reduce the number of TX interrupts
313695246abbSSean Bruno 	 * we see by only setting report status on every Nth descriptor.
313795246abbSSean Bruno 	 * However, this also means that the driver will need to keep track
313895246abbSSean Bruno 	 * of the descriptors that RS was set on to check them for the DD bit.
313995246abbSSean Bruno 	 */
314095246abbSSean Bruno 	txq->ift_rs_pending += nsegs + 1;
314195246abbSSean Bruno 	if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) ||
314295246abbSSean Bruno 	     iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs - 1) <= MAX_TX_DESC(ctx)) {
314395246abbSSean Bruno 		pi.ipi_flags |= IPI_TX_INTR;
314495246abbSSean Bruno 		txq->ift_rs_pending = 0;
314595246abbSSean Bruno 	}
314695246abbSSean Bruno 
31474c7070dbSScott Long 	pi.ipi_segs = segs;
31484c7070dbSScott Long 	pi.ipi_nsegs = nsegs;
31494c7070dbSScott Long 
315023ac9029SStephen Hurd 	MPASS(pidx >= 0 && pidx < txq->ift_size);
31514c7070dbSScott Long #ifdef PKT_DEBUG
31524c7070dbSScott Long 	print_pkt(&pi);
31534c7070dbSScott Long #endif
315495246abbSSean Bruno 	if (map != NULL)
315595246abbSSean Bruno 		bus_dmamap_sync(desc_tag, map, BUS_DMASYNC_PREWRITE);
31564c7070dbSScott Long 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
315795246abbSSean Bruno 		if (map != NULL)
31584c7070dbSScott Long 			bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
31594c7070dbSScott Long 					BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
31604c7070dbSScott Long 		DBG_COUNTER_INC(tx_encap);
316195246abbSSean Bruno 		MPASS(pi.ipi_new_pidx < txq->ift_size);
31624c7070dbSScott Long 
31634c7070dbSScott Long 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
31644c7070dbSScott Long 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
316523ac9029SStephen Hurd 			ndesc += txq->ift_size;
31664c7070dbSScott Long 			txq->ift_gen = 1;
31674c7070dbSScott Long 		}
31681248952aSSean Bruno 		/*
31691248952aSSean Bruno 		 * drivers can need as many as
31701248952aSSean Bruno 		 * two sentinels
31711248952aSSean Bruno 		 */
31721248952aSSean Bruno 		MPASS(ndesc <= pi.ipi_nsegs + 2);
31734c7070dbSScott Long 		MPASS(pi.ipi_new_pidx != pidx);
31744c7070dbSScott Long 		MPASS(ndesc > 0);
31754c7070dbSScott Long 		txq->ift_in_use += ndesc;
317695246abbSSean Bruno 
31774c7070dbSScott Long 		/*
31784c7070dbSScott Long 		 * We update the last software descriptor again here because there may
31794c7070dbSScott Long 		 * be a sentinel and/or there may be more mbufs than segments
31804c7070dbSScott Long 		 */
31814c7070dbSScott Long 		txq->ift_pidx = pi.ipi_new_pidx;
31824c7070dbSScott Long 		txq->ift_npending += pi.ipi_ndescs;
31834c7070dbSScott Long 	} else if (__predict_false(err == EFBIG && remap < 2)) {
318423ac9029SStephen Hurd 		*m_headp = m_head = iflib_remove_mbuf(txq);
31854c7070dbSScott Long 		remap = 1;
31864c7070dbSScott Long 		txq->ift_txd_encap_efbig++;
31874c7070dbSScott Long 		goto defrag;
31884c7070dbSScott Long 	} else
31894c7070dbSScott Long 		DBG_COUNTER_INC(encap_txd_encap_fail);
31904c7070dbSScott Long 	return (err);
31914c7070dbSScott Long 
31924c7070dbSScott Long defrag_failed:
31934c7070dbSScott Long 	txq->ift_mbuf_defrag_failed++;
31944c7070dbSScott Long 	txq->ift_map_failed++;
31954c7070dbSScott Long 	m_freem(*m_headp);
31964c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
31974c7070dbSScott Long 	*m_headp = NULL;
31984c7070dbSScott Long 	return (ENOMEM);
31994c7070dbSScott Long }
32004c7070dbSScott Long 
32014c7070dbSScott Long static void
32024c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n)
32034c7070dbSScott Long {
32044c7070dbSScott Long 	int hasmap;
32054c7070dbSScott Long 	uint32_t qsize, cidx, mask, gen;
32064c7070dbSScott Long 	struct mbuf *m, **ifsd_m;
32074c7070dbSScott Long 	uint8_t *ifsd_flags;
32084c7070dbSScott Long 	bus_dmamap_t *ifsd_map;
320995246abbSSean Bruno 	bool do_prefetch;
32104c7070dbSScott Long 
32114c7070dbSScott Long 	cidx = txq->ift_cidx;
32124c7070dbSScott Long 	gen = txq->ift_gen;
321323ac9029SStephen Hurd 	qsize = txq->ift_size;
32144c7070dbSScott Long 	mask = qsize-1;
32154c7070dbSScott Long 	hasmap = txq->ift_sds.ifsd_map != NULL;
32164c7070dbSScott Long 	ifsd_flags = txq->ift_sds.ifsd_flags;
32174c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
32184c7070dbSScott Long 	ifsd_map = txq->ift_sds.ifsd_map;
321995246abbSSean Bruno 	do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH);
32204c7070dbSScott Long 
32214c7070dbSScott Long 	while (n--) {
322295246abbSSean Bruno 		if (do_prefetch) {
32234c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 3) & mask]);
32244c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 4) & mask]);
322595246abbSSean Bruno 		}
32264c7070dbSScott Long 		if (ifsd_m[cidx] != NULL) {
32274c7070dbSScott Long 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
32284c7070dbSScott Long 			prefetch(&ifsd_flags[(cidx + CACHE_PTR_INCREMENT) & mask]);
32294c7070dbSScott Long 			if (hasmap && (ifsd_flags[cidx] & TX_SW_DESC_MAPPED)) {
32304c7070dbSScott Long 				/*
32314c7070dbSScott Long 				 * does it matter if it's not the TSO tag? If so we'll
32324c7070dbSScott Long 				 * have to add the type to flags
32334c7070dbSScott Long 				 */
32344c7070dbSScott Long 				bus_dmamap_unload(txq->ift_desc_tag, ifsd_map[cidx]);
32354c7070dbSScott Long 				ifsd_flags[cidx] &= ~TX_SW_DESC_MAPPED;
32364c7070dbSScott Long 			}
32374c7070dbSScott Long 			if ((m = ifsd_m[cidx]) != NULL) {
32384c7070dbSScott Long 				/* XXX we don't support any drivers that batch packets yet */
32394c7070dbSScott Long 				MPASS(m->m_nextpkt == NULL);
32405c5ca36cSSean Bruno 				/* if the number of clusters exceeds the number of segments
32415c5ca36cSSean Bruno 				 * there won't be space on the ring to save a pointer to each
32425c5ca36cSSean Bruno 				 * cluster so we simply free the list here
32435c5ca36cSSean Bruno 				 */
32445c5ca36cSSean Bruno 				if (m->m_flags & M_TOOBIG) {
32455c5ca36cSSean Bruno 					m_freem(m);
32465c5ca36cSSean Bruno 				} else {
324723ac9029SStephen Hurd 					m_free(m);
32485c5ca36cSSean Bruno 				}
32494c7070dbSScott Long 				ifsd_m[cidx] = NULL;
32504c7070dbSScott Long #if MEMORY_LOGGING
32514c7070dbSScott Long 				txq->ift_dequeued++;
32524c7070dbSScott Long #endif
32534c7070dbSScott Long 				DBG_COUNTER_INC(tx_frees);
32544c7070dbSScott Long 			}
32554c7070dbSScott Long 		}
32564c7070dbSScott Long 		if (__predict_false(++cidx == qsize)) {
32574c7070dbSScott Long 			cidx = 0;
32584c7070dbSScott Long 			gen = 0;
32594c7070dbSScott Long 		}
32604c7070dbSScott Long 	}
32614c7070dbSScott Long 	txq->ift_cidx = cidx;
32624c7070dbSScott Long 	txq->ift_gen = gen;
32634c7070dbSScott Long }
32644c7070dbSScott Long 
32654c7070dbSScott Long static __inline int
32664c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
32674c7070dbSScott Long {
32684c7070dbSScott Long 	int reclaim;
32694c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
32704c7070dbSScott Long 
32714c7070dbSScott Long 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
32724c7070dbSScott Long 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
32734c7070dbSScott Long 
32744c7070dbSScott Long 	/*
32754c7070dbSScott Long 	 * Need a rate-limiting check so that this isn't called every time
32764c7070dbSScott Long 	 */
32774c7070dbSScott Long 	iflib_tx_credits_update(ctx, txq);
32784c7070dbSScott Long 	reclaim = DESC_RECLAIMABLE(txq);
32794c7070dbSScott Long 
32804c7070dbSScott Long 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
32814c7070dbSScott Long #ifdef INVARIANTS
32824c7070dbSScott Long 		if (iflib_verbose_debug) {
32834c7070dbSScott Long 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
32844c7070dbSScott Long 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
32854c7070dbSScott Long 			       reclaim, thresh);
32864c7070dbSScott Long 
32874c7070dbSScott Long 		}
32884c7070dbSScott Long #endif
32894c7070dbSScott Long 		return (0);
32904c7070dbSScott Long 	}
32914c7070dbSScott Long 	iflib_tx_desc_free(txq, reclaim);
32924c7070dbSScott Long 	txq->ift_cleaned += reclaim;
32934c7070dbSScott Long 	txq->ift_in_use -= reclaim;
32944c7070dbSScott Long 
32954c7070dbSScott Long 	return (reclaim);
32964c7070dbSScott Long }
32974c7070dbSScott Long 
32984c7070dbSScott Long static struct mbuf **
329995246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
33004c7070dbSScott Long {
330195246abbSSean Bruno 	int next, size;
330295246abbSSean Bruno 	struct mbuf **items;
33034c7070dbSScott Long 
330495246abbSSean Bruno 	size = r->size;
330595246abbSSean Bruno 	next = (cidx + CACHE_PTR_INCREMENT) & (size-1);
330695246abbSSean Bruno 	items = __DEVOLATILE(struct mbuf **, &r->items[0]);
330795246abbSSean Bruno 
330895246abbSSean Bruno 	prefetch(items[(cidx + offset) & (size-1)]);
330995246abbSSean Bruno 	if (remaining > 1) {
3310ab2e3f79SStephen Hurd 		prefetch(&items[next]);
3311ab2e3f79SStephen Hurd 		prefetch(items[(cidx + offset + 1) & (size-1)]);
3312ab2e3f79SStephen Hurd 		prefetch(items[(cidx + offset + 2) & (size-1)]);
3313ab2e3f79SStephen Hurd 		prefetch(items[(cidx + offset + 3) & (size-1)]);
331495246abbSSean Bruno 	}
331595246abbSSean Bruno 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)]));
33164c7070dbSScott Long }
33174c7070dbSScott Long 
33184c7070dbSScott Long static void
33194c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget)
33204c7070dbSScott Long {
33214c7070dbSScott Long 
332295246abbSSean Bruno 	ifmp_ring_check_drainage(txq->ift_br, budget);
33234c7070dbSScott Long }
33244c7070dbSScott Long 
33254c7070dbSScott Long static uint32_t
33264c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r)
33274c7070dbSScott Long {
33284c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
33294c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
33304c7070dbSScott Long 
33311248952aSSean Bruno 	return ((TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) ||
333295246abbSSean Bruno 		ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false));
33334c7070dbSScott Long }
33344c7070dbSScott Long 
33354c7070dbSScott Long static uint32_t
33364c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
33374c7070dbSScott Long {
33384c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
33394c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
334095246abbSSean Bruno 	struct ifnet *ifp = ctx->ifc_ifp;
33414c7070dbSScott Long 	struct mbuf **mp, *m;
334295246abbSSean Bruno 	int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail;
334395246abbSSean Bruno 	int reclaimed, err, in_use_prev, desc_used;
334495246abbSSean Bruno 	bool do_prefetch, ring, rang;
33454c7070dbSScott Long 
33464c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
33474c7070dbSScott Long 			    !LINK_ACTIVE(ctx))) {
33484c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_notready);
33494c7070dbSScott Long 		return (0);
33504c7070dbSScott Long 	}
335195246abbSSean Bruno 	reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
335295246abbSSean Bruno 	rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use);
33534c7070dbSScott Long 	avail = IDXDIFF(pidx, cidx, r->size);
33544c7070dbSScott Long 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
33554c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_flushing);
33564c7070dbSScott Long 		for (i = 0; i < avail; i++) {
335723ac9029SStephen Hurd 			m_free(r->items[(cidx + i) & (r->size-1)]);
33584c7070dbSScott Long 			r->items[(cidx + i) & (r->size-1)] = NULL;
33594c7070dbSScott Long 		}
33604c7070dbSScott Long 		return (avail);
33614c7070dbSScott Long 	}
336295246abbSSean Bruno 
33634c7070dbSScott Long 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
33644c7070dbSScott Long 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
33654c7070dbSScott Long 		CALLOUT_LOCK(txq);
33664c7070dbSScott Long 		callout_stop(&txq->ift_timer);
33674c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
33684c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_oactive);
33694c7070dbSScott Long 		return (0);
33704c7070dbSScott Long 	}
337195246abbSSean Bruno 	if (reclaimed)
337295246abbSSean Bruno 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
33734c7070dbSScott Long 	consumed = mcast_sent = bytes_sent = pkt_sent = 0;
33744c7070dbSScott Long 	count = MIN(avail, TX_BATCH_SIZE);
3375da69b8f9SSean Bruno #ifdef INVARIANTS
3376da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3377da69b8f9SSean Bruno 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3378da69b8f9SSean Bruno 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3379da69b8f9SSean Bruno #endif
338095246abbSSean Bruno 	do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
338195246abbSSean Bruno 	avail = TXQ_AVAIL(txq);
338295246abbSSean Bruno 	for (desc_used = i = 0; i < count && avail > MAX_TX_DESC(ctx) + 2; i++) {
338395246abbSSean Bruno 		int pidx_prev, rem = do_prefetch ? count - i : 0;
33844c7070dbSScott Long 
338595246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, rem);
3386da69b8f9SSean Bruno 		MPASS(mp != NULL && *mp != NULL);
338795246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq)) {
338895246abbSSean Bruno 			consumed++;
338995246abbSSean Bruno 			reclaimed++;
339095246abbSSean Bruno 			continue;
339195246abbSSean Bruno 		}
33924c7070dbSScott Long 		in_use_prev = txq->ift_in_use;
339395246abbSSean Bruno 		pidx_prev = txq->ift_pidx;
339495246abbSSean Bruno 		err = iflib_encap(txq, mp);
339595246abbSSean Bruno 		if (__predict_false(err)) {
33964c7070dbSScott Long 			DBG_COUNTER_INC(txq_drain_encapfail);
3397da69b8f9SSean Bruno 			/* no room - bail out */
339895246abbSSean Bruno 			if (err == ENOBUFS)
33994c7070dbSScott Long 				break;
34004c7070dbSScott Long 			consumed++;
3401da69b8f9SSean Bruno 			DBG_COUNTER_INC(txq_drain_encapfail);
3402da69b8f9SSean Bruno 			/* we can't send this packet - skip it */
34034c7070dbSScott Long 			continue;
3404da69b8f9SSean Bruno 		}
340595246abbSSean Bruno 		consumed++;
34064c7070dbSScott Long 		pkt_sent++;
34074c7070dbSScott Long 		m = *mp;
34084c7070dbSScott Long 		DBG_COUNTER_INC(tx_sent);
34094c7070dbSScott Long 		bytes_sent += m->m_pkthdr.len;
341095246abbSSean Bruno 		mcast_sent += !!(m->m_flags & M_MCAST);
341195246abbSSean Bruno 		avail = TXQ_AVAIL(txq);
34124c7070dbSScott Long 
34134c7070dbSScott Long 		txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
34144c7070dbSScott Long 		desc_used += (txq->ift_in_use - in_use_prev);
34154c7070dbSScott Long 		ETHER_BPF_MTAP(ifp, m);
341695246abbSSean Bruno 		if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
34174c7070dbSScott Long 			break;
341895246abbSSean Bruno 		rang = iflib_txd_db_check(ctx, txq, false, in_use_prev);
34194c7070dbSScott Long 	}
34204c7070dbSScott Long 
342195246abbSSean Bruno 	/* deliberate use of bitwise or to avoid gratuitous short-circuit */
342295246abbSSean Bruno 	ring = rang ? false  : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx));
342395246abbSSean Bruno 	iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use);
34244c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
34254c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
34264c7070dbSScott Long 	if (mcast_sent)
34274c7070dbSScott Long 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3428da69b8f9SSean Bruno #ifdef INVARIANTS
3429da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3430da69b8f9SSean Bruno 		printf("consumed=%d\n", consumed);
3431da69b8f9SSean Bruno #endif
34324c7070dbSScott Long 	return (consumed);
34334c7070dbSScott Long }
34344c7070dbSScott Long 
3435da69b8f9SSean Bruno static uint32_t
3436da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r)
3437da69b8f9SSean Bruno {
3438da69b8f9SSean Bruno 	return (1);
3439da69b8f9SSean Bruno }
3440da69b8f9SSean Bruno 
3441da69b8f9SSean Bruno static uint32_t
3442da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3443da69b8f9SSean Bruno {
3444da69b8f9SSean Bruno 	int i, avail;
3445da69b8f9SSean Bruno 	struct mbuf **mp;
3446da69b8f9SSean Bruno 	iflib_txq_t txq;
3447da69b8f9SSean Bruno 
3448da69b8f9SSean Bruno 	txq = r->cookie;
3449da69b8f9SSean Bruno 
3450da69b8f9SSean Bruno 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3451da69b8f9SSean Bruno 	CALLOUT_LOCK(txq);
3452da69b8f9SSean Bruno 	callout_stop(&txq->ift_timer);
3453da69b8f9SSean Bruno 	CALLOUT_UNLOCK(txq);
3454da69b8f9SSean Bruno 
3455da69b8f9SSean Bruno 	avail = IDXDIFF(pidx, cidx, r->size);
3456da69b8f9SSean Bruno 	for (i = 0; i < avail; i++) {
345795246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, avail - i);
345895246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq))
345995246abbSSean Bruno 			continue;
3460da69b8f9SSean Bruno 		m_freem(*mp);
3461da69b8f9SSean Bruno 	}
3462da69b8f9SSean Bruno 	MPASS(ifmp_ring_is_stalled(r) == 0);
3463da69b8f9SSean Bruno 	return (avail);
3464da69b8f9SSean Bruno }
3465da69b8f9SSean Bruno 
3466da69b8f9SSean Bruno static void
3467da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq)
3468da69b8f9SSean Bruno {
3469da69b8f9SSean Bruno 	struct ifmp_ring *r;
3470da69b8f9SSean Bruno 
347195246abbSSean Bruno 	r = txq->ift_br;
3472da69b8f9SSean Bruno 	r->drain = iflib_txq_drain_free;
3473da69b8f9SSean Bruno 	r->can_drain = iflib_txq_drain_always;
3474da69b8f9SSean Bruno 
3475da69b8f9SSean Bruno 	ifmp_ring_check_drainage(r, r->size);
3476da69b8f9SSean Bruno 
3477da69b8f9SSean Bruno 	r->drain = iflib_txq_drain;
3478da69b8f9SSean Bruno 	r->can_drain = iflib_txq_can_drain;
3479da69b8f9SSean Bruno }
3480da69b8f9SSean Bruno 
34814c7070dbSScott Long static void
348223ac9029SStephen Hurd _task_fn_tx(void *context)
34834c7070dbSScott Long {
34844c7070dbSScott Long 	iflib_txq_t txq = context;
34854c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
348695246abbSSean Bruno 	struct ifnet *ifp = ctx->ifc_ifp;
348795246abbSSean Bruno 	int rc;
34884c7070dbSScott Long 
34891248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
34901248952aSSean Bruno 	txq->ift_cpu_exec_count[curcpu]++;
34911248952aSSean Bruno #endif
34924c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
34934c7070dbSScott Long 		return;
3494*d0d0ad0aSStephen Hurd 	if (if_getcapenable(ifp) & IFCAP_NETMAP) {
349595246abbSSean Bruno 		if (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false))
349695246abbSSean Bruno 			netmap_tx_irq(ifp, txq->ift_id);
349795246abbSSean Bruno 		IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
349895246abbSSean Bruno 		return;
349995246abbSSean Bruno 	}
350095246abbSSean Bruno 	if (txq->ift_db_pending)
350195246abbSSean Bruno 		ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE);
3502ab2e3f79SStephen Hurd 	else
350395246abbSSean Bruno 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
350495246abbSSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
350595246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
350695246abbSSean Bruno 	else {
350795246abbSSean Bruno 		rc = IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
350895246abbSSean Bruno 		KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
350995246abbSSean Bruno 	}
35104c7070dbSScott Long }
35114c7070dbSScott Long 
35124c7070dbSScott Long static void
351323ac9029SStephen Hurd _task_fn_rx(void *context)
35144c7070dbSScott Long {
35154c7070dbSScott Long 	iflib_rxq_t rxq = context;
35164c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
35174c7070dbSScott Long 	bool more;
351823ac9029SStephen Hurd 	int rc;
35194c7070dbSScott Long 
35201248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
35211248952aSSean Bruno 	rxq->ifr_cpu_exec_count[curcpu]++;
35221248952aSSean Bruno #endif
35234c7070dbSScott Long 	DBG_COUNTER_INC(task_fn_rxs);
35244c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
35254c7070dbSScott Long 		return;
3526*d0d0ad0aSStephen Hurd 	more = true;
3527*d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP
3528*d0d0ad0aSStephen Hurd 	if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) {
3529*d0d0ad0aSStephen Hurd 		u_int work = 0;
3530*d0d0ad0aSStephen Hurd 		if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work)) {
3531*d0d0ad0aSStephen Hurd 			more = false;
3532*d0d0ad0aSStephen Hurd 		}
3533*d0d0ad0aSStephen Hurd 	}
3534*d0d0ad0aSStephen Hurd #endif
3535*d0d0ad0aSStephen Hurd 	if (more == false || (more = iflib_rxeof(rxq, 16 /* XXX */)) == false) {
35364c7070dbSScott Long 		if (ctx->ifc_flags & IFC_LEGACY)
35374c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
35384c7070dbSScott Long 		else {
35394c7070dbSScott Long 			DBG_COUNTER_INC(rx_intr_enables);
354095246abbSSean Bruno 			rc = IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
354123ac9029SStephen Hurd 			KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
35424c7070dbSScott Long 		}
35434c7070dbSScott Long 	}
35444c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
35454c7070dbSScott Long 		return;
35464c7070dbSScott Long 	if (more)
35474c7070dbSScott Long 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
35484c7070dbSScott Long }
35494c7070dbSScott Long 
35504c7070dbSScott Long static void
355123ac9029SStephen Hurd _task_fn_admin(void *context)
35524c7070dbSScott Long {
35534c7070dbSScott Long 	if_ctx_t ctx = context;
35544c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
35554c7070dbSScott Long 	iflib_txq_t txq;
3556ab2e3f79SStephen Hurd 	int i;
3557ab2e3f79SStephen Hurd 
3558ab2e3f79SStephen Hurd 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) {
3559ab2e3f79SStephen Hurd 		if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
3560ab2e3f79SStephen Hurd 			return;
3561ab2e3f79SStephen Hurd 		}
3562ab2e3f79SStephen Hurd 	}
35634c7070dbSScott Long 
35644c7070dbSScott Long 	CTX_LOCK(ctx);
35654c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
35664c7070dbSScott Long 		CALLOUT_LOCK(txq);
35674c7070dbSScott Long 		callout_stop(&txq->ift_timer);
35684c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
35694c7070dbSScott Long 	}
3570d300df01SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
3571ab2e3f79SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
3572ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
3573ab2e3f79SStephen Hurd 	IFDI_LINK_INTR_ENABLE(ctx);
3574ab2e3f79SStephen Hurd 	if (ctx->ifc_flags & IFC_DO_RESET) {
3575ab2e3f79SStephen Hurd 		ctx->ifc_flags &= ~IFC_DO_RESET;
3576ab2e3f79SStephen Hurd 		iflib_if_init_locked(ctx);
3577ab2e3f79SStephen Hurd 	}
35784c7070dbSScott Long 	CTX_UNLOCK(ctx);
35794c7070dbSScott Long 
3580ab2e3f79SStephen Hurd 	if (LINK_ACTIVE(ctx) == 0)
35814c7070dbSScott Long 		return;
35824c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
35834c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
35844c7070dbSScott Long }
35854c7070dbSScott Long 
35864c7070dbSScott Long 
35874c7070dbSScott Long static void
358823ac9029SStephen Hurd _task_fn_iov(void *context)
35894c7070dbSScott Long {
35904c7070dbSScott Long 	if_ctx_t ctx = context;
35914c7070dbSScott Long 
35924c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
35934c7070dbSScott Long 		return;
35944c7070dbSScott Long 
35954c7070dbSScott Long 	CTX_LOCK(ctx);
35964c7070dbSScott Long 	IFDI_VFLR_HANDLE(ctx);
35974c7070dbSScott Long 	CTX_UNLOCK(ctx);
35984c7070dbSScott Long }
35994c7070dbSScott Long 
36004c7070dbSScott Long static int
36014c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
36024c7070dbSScott Long {
36034c7070dbSScott Long 	int err;
36044c7070dbSScott Long 	if_int_delay_info_t info;
36054c7070dbSScott Long 	if_ctx_t ctx;
36064c7070dbSScott Long 
36074c7070dbSScott Long 	info = (if_int_delay_info_t)arg1;
36084c7070dbSScott Long 	ctx = info->iidi_ctx;
36094c7070dbSScott Long 	info->iidi_req = req;
36104c7070dbSScott Long 	info->iidi_oidp = oidp;
36114c7070dbSScott Long 	CTX_LOCK(ctx);
36124c7070dbSScott Long 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
36134c7070dbSScott Long 	CTX_UNLOCK(ctx);
36144c7070dbSScott Long 	return (err);
36154c7070dbSScott Long }
36164c7070dbSScott Long 
36174c7070dbSScott Long /*********************************************************************
36184c7070dbSScott Long  *
36194c7070dbSScott Long  *  IFNET FUNCTIONS
36204c7070dbSScott Long  *
36214c7070dbSScott Long  **********************************************************************/
36224c7070dbSScott Long 
36234c7070dbSScott Long static void
36244c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx)
36254c7070dbSScott Long {
36264c7070dbSScott Long 	iflib_stop(ctx);
36274c7070dbSScott Long 	iflib_init_locked(ctx);
36284c7070dbSScott Long }
36294c7070dbSScott Long 
36304c7070dbSScott Long 
36314c7070dbSScott Long static void
36324c7070dbSScott Long iflib_if_init(void *arg)
36334c7070dbSScott Long {
36344c7070dbSScott Long 	if_ctx_t ctx = arg;
36354c7070dbSScott Long 
36364c7070dbSScott Long 	CTX_LOCK(ctx);
36374c7070dbSScott Long 	iflib_if_init_locked(ctx);
36384c7070dbSScott Long 	CTX_UNLOCK(ctx);
36394c7070dbSScott Long }
36404c7070dbSScott Long 
36414c7070dbSScott Long static int
36424c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m)
36434c7070dbSScott Long {
36444c7070dbSScott Long 	if_ctx_t	ctx = if_getsoftc(ifp);
36454c7070dbSScott Long 
36464c7070dbSScott Long 	iflib_txq_t txq;
364723ac9029SStephen Hurd 	int err, qidx;
36484c7070dbSScott Long 
36494c7070dbSScott Long 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
36504c7070dbSScott Long 		DBG_COUNTER_INC(tx_frees);
36514c7070dbSScott Long 		m_freem(m);
3652da69b8f9SSean Bruno 		return (ENOBUFS);
36534c7070dbSScott Long 	}
36544c7070dbSScott Long 
365523ac9029SStephen Hurd 	MPASS(m->m_nextpkt == NULL);
36564c7070dbSScott Long 	qidx = 0;
36574c7070dbSScott Long 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
36584c7070dbSScott Long 		qidx = QIDX(ctx, m);
36594c7070dbSScott Long 	/*
36604c7070dbSScott Long 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
36614c7070dbSScott Long 	 */
36624c7070dbSScott Long 	txq = &ctx->ifc_txqs[qidx];
36634c7070dbSScott Long 
36644c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
36654c7070dbSScott Long 	if (txq->ift_closed) {
36664c7070dbSScott Long 		while (m != NULL) {
36674c7070dbSScott Long 			next = m->m_nextpkt;
36684c7070dbSScott Long 			m->m_nextpkt = NULL;
36694c7070dbSScott Long 			m_freem(m);
36704c7070dbSScott Long 			m = next;
36714c7070dbSScott Long 		}
36724c7070dbSScott Long 		return (ENOBUFS);
36734c7070dbSScott Long 	}
36744c7070dbSScott Long #endif
367523ac9029SStephen Hurd #ifdef notyet
36764c7070dbSScott Long 	qidx = count = 0;
36774c7070dbSScott Long 	mp = marr;
36784c7070dbSScott Long 	next = m;
36794c7070dbSScott Long 	do {
36804c7070dbSScott Long 		count++;
36814c7070dbSScott Long 		next = next->m_nextpkt;
36824c7070dbSScott Long 	} while (next != NULL);
36834c7070dbSScott Long 
368416fb86abSConrad Meyer 	if (count > nitems(marr))
36854c7070dbSScott Long 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
36864c7070dbSScott Long 			/* XXX check nextpkt */
36874c7070dbSScott Long 			m_freem(m);
36884c7070dbSScott Long 			/* XXX simplify for now */
36894c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
36904c7070dbSScott Long 			return (ENOBUFS);
36914c7070dbSScott Long 		}
36924c7070dbSScott Long 	for (next = m, i = 0; next != NULL; i++) {
36934c7070dbSScott Long 		mp[i] = next;
36944c7070dbSScott Long 		next = next->m_nextpkt;
36954c7070dbSScott Long 		mp[i]->m_nextpkt = NULL;
36964c7070dbSScott Long 	}
369723ac9029SStephen Hurd #endif
36984c7070dbSScott Long 	DBG_COUNTER_INC(tx_seen);
369995246abbSSean Bruno 	err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE);
37004c7070dbSScott Long 
3701d300df01SStephen Hurd 	if (err) {
3702ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
37034c7070dbSScott Long 		/* support forthcoming later */
37044c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
37054c7070dbSScott Long 		txq->ift_closed = TRUE;
37064c7070dbSScott Long #endif
370795246abbSSean Bruno 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
370823ac9029SStephen Hurd 		m_freem(m);
3709ab2e3f79SStephen Hurd 	} else if (TXQ_AVAIL(txq) < (txq->ift_size >> 1)) {
3710ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
37114c7070dbSScott Long 	}
37124c7070dbSScott Long 
37134c7070dbSScott Long 	return (err);
37144c7070dbSScott Long }
37154c7070dbSScott Long 
37164c7070dbSScott Long static void
37174c7070dbSScott Long iflib_if_qflush(if_t ifp)
37184c7070dbSScott Long {
37194c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
37204c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
37214c7070dbSScott Long 	int i;
37224c7070dbSScott Long 
37234c7070dbSScott Long 	CTX_LOCK(ctx);
37244c7070dbSScott Long 	ctx->ifc_flags |= IFC_QFLUSH;
37254c7070dbSScott Long 	CTX_UNLOCK(ctx);
37264c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
372795246abbSSean Bruno 		while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
37284c7070dbSScott Long 			iflib_txq_check_drain(txq, 0);
37294c7070dbSScott Long 	CTX_LOCK(ctx);
37304c7070dbSScott Long 	ctx->ifc_flags &= ~IFC_QFLUSH;
37314c7070dbSScott Long 	CTX_UNLOCK(ctx);
37324c7070dbSScott Long 
37334c7070dbSScott Long 	if_qflush(ifp);
37344c7070dbSScott Long }
37354c7070dbSScott Long 
37364c7070dbSScott Long 
373723ac9029SStephen Hurd #define IFCAP_FLAGS (IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
373818a660b3SSean Bruno 		     IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
37394c7070dbSScott Long 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO)
37404c7070dbSScott Long 
37414c7070dbSScott Long static int
37424c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
37434c7070dbSScott Long {
37444c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
37454c7070dbSScott Long 	struct ifreq	*ifr = (struct ifreq *)data;
37464c7070dbSScott Long #if defined(INET) || defined(INET6)
37474c7070dbSScott Long 	struct ifaddr	*ifa = (struct ifaddr *)data;
37484c7070dbSScott Long #endif
37494c7070dbSScott Long 	bool		avoid_reset = FALSE;
37504c7070dbSScott Long 	int		err = 0, reinit = 0, bits;
37514c7070dbSScott Long 
37524c7070dbSScott Long 	switch (command) {
37534c7070dbSScott Long 	case SIOCSIFADDR:
37544c7070dbSScott Long #ifdef INET
37554c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET)
37564c7070dbSScott Long 			avoid_reset = TRUE;
37574c7070dbSScott Long #endif
37584c7070dbSScott Long #ifdef INET6
37594c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET6)
37604c7070dbSScott Long 			avoid_reset = TRUE;
37614c7070dbSScott Long #endif
37624c7070dbSScott Long 		/*
37634c7070dbSScott Long 		** Calling init results in link renegotiation,
37644c7070dbSScott Long 		** so we avoid doing it when possible.
37654c7070dbSScott Long 		*/
37664c7070dbSScott Long 		if (avoid_reset) {
37674c7070dbSScott Long 			if_setflagbits(ifp, IFF_UP,0);
37684c7070dbSScott Long 			if (!(if_getdrvflags(ifp)& IFF_DRV_RUNNING))
37694c7070dbSScott Long 				reinit = 1;
37704c7070dbSScott Long #ifdef INET
37714c7070dbSScott Long 			if (!(if_getflags(ifp) & IFF_NOARP))
37724c7070dbSScott Long 				arp_ifinit(ifp, ifa);
37734c7070dbSScott Long #endif
37744c7070dbSScott Long 		} else
37754c7070dbSScott Long 			err = ether_ioctl(ifp, command, data);
37764c7070dbSScott Long 		break;
37774c7070dbSScott Long 	case SIOCSIFMTU:
37784c7070dbSScott Long 		CTX_LOCK(ctx);
37794c7070dbSScott Long 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
37804c7070dbSScott Long 			CTX_UNLOCK(ctx);
37814c7070dbSScott Long 			break;
37824c7070dbSScott Long 		}
37834c7070dbSScott Long 		bits = if_getdrvflags(ifp);
37844c7070dbSScott Long 		/* stop the driver and free any clusters before proceeding */
37854c7070dbSScott Long 		iflib_stop(ctx);
37864c7070dbSScott Long 
37874c7070dbSScott Long 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
37884c7070dbSScott Long 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
37894c7070dbSScott Long 				ctx->ifc_flags |= IFC_MULTISEG;
37904c7070dbSScott Long 			else
37914c7070dbSScott Long 				ctx->ifc_flags &= ~IFC_MULTISEG;
37924c7070dbSScott Long 			err = if_setmtu(ifp, ifr->ifr_mtu);
37934c7070dbSScott Long 		}
37944c7070dbSScott Long 		iflib_init_locked(ctx);
37954c7070dbSScott Long 		if_setdrvflags(ifp, bits);
37964c7070dbSScott Long 		CTX_UNLOCK(ctx);
37974c7070dbSScott Long 		break;
37984c7070dbSScott Long 	case SIOCSIFFLAGS:
3799ab2e3f79SStephen Hurd 		CTX_LOCK(ctx);
3800ab2e3f79SStephen Hurd 		if (if_getflags(ifp) & IFF_UP) {
3801ab2e3f79SStephen Hurd 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3802ab2e3f79SStephen Hurd 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
3803ab2e3f79SStephen Hurd 				    (IFF_PROMISC | IFF_ALLMULTI)) {
3804ab2e3f79SStephen Hurd 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
3805ab2e3f79SStephen Hurd 				}
3806ab2e3f79SStephen Hurd 			} else
3807ab2e3f79SStephen Hurd 				reinit = 1;
3808ab2e3f79SStephen Hurd 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3809ab2e3f79SStephen Hurd 			iflib_stop(ctx);
3810ab2e3f79SStephen Hurd 		}
3811ab2e3f79SStephen Hurd 		ctx->ifc_if_flags = if_getflags(ifp);
3812ab2e3f79SStephen Hurd 		CTX_UNLOCK(ctx);
38134c7070dbSScott Long 		break;
38144c7070dbSScott Long 	case SIOCADDMULTI:
38154c7070dbSScott Long 	case SIOCDELMULTI:
38164c7070dbSScott Long 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3817ab2e3f79SStephen Hurd 			CTX_LOCK(ctx);
3818ab2e3f79SStephen Hurd 			IFDI_INTR_DISABLE(ctx);
3819ab2e3f79SStephen Hurd 			IFDI_MULTI_SET(ctx);
3820ab2e3f79SStephen Hurd 			IFDI_INTR_ENABLE(ctx);
3821ab2e3f79SStephen Hurd 			CTX_UNLOCK(ctx);
38224c7070dbSScott Long 		}
38234c7070dbSScott Long 		break;
38244c7070dbSScott Long 	case SIOCSIFMEDIA:
38254c7070dbSScott Long 		CTX_LOCK(ctx);
38264c7070dbSScott Long 		IFDI_MEDIA_SET(ctx);
38274c7070dbSScott Long 		CTX_UNLOCK(ctx);
38284c7070dbSScott Long 		/* falls thru */
38294c7070dbSScott Long 	case SIOCGIFMEDIA:
38304c7070dbSScott Long 		err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command);
38314c7070dbSScott Long 		break;
38324c7070dbSScott Long 	case SIOCGI2C:
38334c7070dbSScott Long 	{
38344c7070dbSScott Long 		struct ifi2creq i2c;
38354c7070dbSScott Long 
38364c7070dbSScott Long 		err = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
38374c7070dbSScott Long 		if (err != 0)
38384c7070dbSScott Long 			break;
38394c7070dbSScott Long 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
38404c7070dbSScott Long 			err = EINVAL;
38414c7070dbSScott Long 			break;
38424c7070dbSScott Long 		}
38434c7070dbSScott Long 		if (i2c.len > sizeof(i2c.data)) {
38444c7070dbSScott Long 			err = EINVAL;
38454c7070dbSScott Long 			break;
38464c7070dbSScott Long 		}
38474c7070dbSScott Long 
38484c7070dbSScott Long 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
38494c7070dbSScott Long 			err = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
38504c7070dbSScott Long 		break;
38514c7070dbSScott Long 	}
38524c7070dbSScott Long 	case SIOCSIFCAP:
38534c7070dbSScott Long 	{
38544c7070dbSScott Long 		int mask, setmask;
38554c7070dbSScott Long 
38564c7070dbSScott Long 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
38574c7070dbSScott Long 		setmask = 0;
38584c7070dbSScott Long #ifdef TCP_OFFLOAD
38594c7070dbSScott Long 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
38604c7070dbSScott Long #endif
38614c7070dbSScott Long 		setmask |= (mask & IFCAP_FLAGS);
38624c7070dbSScott Long 
38638b2a1db9SSean Bruno 		if (setmask  & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
38648b2a1db9SSean Bruno 			setmask |= (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
38654c7070dbSScott Long 		if ((mask & IFCAP_WOL) &&
38664c7070dbSScott Long 		    (if_getcapabilities(ifp) & IFCAP_WOL) != 0)
38674c7070dbSScott Long 			setmask |= (mask & (IFCAP_WOL_MCAST|IFCAP_WOL_MAGIC));
38684c7070dbSScott Long 		if_vlancap(ifp);
38694c7070dbSScott Long 		/*
38704c7070dbSScott Long 		 * want to ensure that traffic has stopped before we change any of the flags
38714c7070dbSScott Long 		 */
38724c7070dbSScott Long 		if (setmask) {
38734c7070dbSScott Long 			CTX_LOCK(ctx);
38744c7070dbSScott Long 			bits = if_getdrvflags(ifp);
38758b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
38764c7070dbSScott Long 				iflib_stop(ctx);
38774c7070dbSScott Long 			if_togglecapenable(ifp, setmask);
38788b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
38794c7070dbSScott Long 				iflib_init_locked(ctx);
38804c7070dbSScott Long 			if_setdrvflags(ifp, bits);
38814c7070dbSScott Long 			CTX_UNLOCK(ctx);
38824c7070dbSScott Long 		}
38834c7070dbSScott Long 		break;
38844c7070dbSScott Long 	    }
38854c7070dbSScott Long 	case SIOCGPRIVATE_0:
38864c7070dbSScott Long 	case SIOCSDRVSPEC:
38874c7070dbSScott Long 	case SIOCGDRVSPEC:
38884c7070dbSScott Long 		CTX_LOCK(ctx);
38894c7070dbSScott Long 		err = IFDI_PRIV_IOCTL(ctx, command, data);
38904c7070dbSScott Long 		CTX_UNLOCK(ctx);
38914c7070dbSScott Long 		break;
38924c7070dbSScott Long 	default:
38934c7070dbSScott Long 		err = ether_ioctl(ifp, command, data);
38944c7070dbSScott Long 		break;
38954c7070dbSScott Long 	}
38964c7070dbSScott Long 	if (reinit)
38974c7070dbSScott Long 		iflib_if_init(ctx);
38984c7070dbSScott Long 	return (err);
38994c7070dbSScott Long }
39004c7070dbSScott Long 
39014c7070dbSScott Long static uint64_t
39024c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt)
39034c7070dbSScott Long {
39044c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
39054c7070dbSScott Long 
39064c7070dbSScott Long 	return (IFDI_GET_COUNTER(ctx, cnt));
39074c7070dbSScott Long }
39084c7070dbSScott Long 
39094c7070dbSScott Long /*********************************************************************
39104c7070dbSScott Long  *
39114c7070dbSScott Long  *  OTHER FUNCTIONS EXPORTED TO THE STACK
39124c7070dbSScott Long  *
39134c7070dbSScott Long  **********************************************************************/
39144c7070dbSScott Long 
39154c7070dbSScott Long static void
39164c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
39174c7070dbSScott Long {
39184c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
39194c7070dbSScott Long 
39204c7070dbSScott Long 	if ((void *)ctx != arg)
39214c7070dbSScott Long 		return;
39224c7070dbSScott Long 
39234c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
39244c7070dbSScott Long 		return;
39254c7070dbSScott Long 
39264c7070dbSScott Long 	CTX_LOCK(ctx);
39274c7070dbSScott Long 	IFDI_VLAN_REGISTER(ctx, vtag);
39284c7070dbSScott Long 	/* Re-init to load the changes */
39294c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
393021e10b16SSean Bruno 		iflib_if_init_locked(ctx);
39314c7070dbSScott Long 	CTX_UNLOCK(ctx);
39324c7070dbSScott Long }
39334c7070dbSScott Long 
39344c7070dbSScott Long static void
39354c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
39364c7070dbSScott Long {
39374c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
39384c7070dbSScott Long 
39394c7070dbSScott Long 	if ((void *)ctx != arg)
39404c7070dbSScott Long 		return;
39414c7070dbSScott Long 
39424c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
39434c7070dbSScott Long 		return;
39444c7070dbSScott Long 
39454c7070dbSScott Long 	CTX_LOCK(ctx);
39464c7070dbSScott Long 	IFDI_VLAN_UNREGISTER(ctx, vtag);
39474c7070dbSScott Long 	/* Re-init to load the changes */
39484c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
394921e10b16SSean Bruno 		iflib_if_init_locked(ctx);
39504c7070dbSScott Long 	CTX_UNLOCK(ctx);
39514c7070dbSScott Long }
39524c7070dbSScott Long 
39534c7070dbSScott Long static void
39544c7070dbSScott Long iflib_led_func(void *arg, int onoff)
39554c7070dbSScott Long {
39564c7070dbSScott Long 	if_ctx_t ctx = arg;
39574c7070dbSScott Long 
39584c7070dbSScott Long 	CTX_LOCK(ctx);
39594c7070dbSScott Long 	IFDI_LED_FUNC(ctx, onoff);
39604c7070dbSScott Long 	CTX_UNLOCK(ctx);
39614c7070dbSScott Long }
39624c7070dbSScott Long 
39634c7070dbSScott Long /*********************************************************************
39644c7070dbSScott Long  *
39654c7070dbSScott Long  *  BUS FUNCTION DEFINITIONS
39664c7070dbSScott Long  *
39674c7070dbSScott Long  **********************************************************************/
39684c7070dbSScott Long 
39694c7070dbSScott Long int
39704c7070dbSScott Long iflib_device_probe(device_t dev)
39714c7070dbSScott Long {
39724c7070dbSScott Long 	pci_vendor_info_t *ent;
39734c7070dbSScott Long 
39744c7070dbSScott Long 	uint16_t	pci_vendor_id, pci_device_id;
39754c7070dbSScott Long 	uint16_t	pci_subvendor_id, pci_subdevice_id;
39764c7070dbSScott Long 	uint16_t	pci_rev_id;
39774c7070dbSScott Long 	if_shared_ctx_t sctx;
39784c7070dbSScott Long 
39794c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
39804c7070dbSScott Long 		return (ENOTSUP);
39814c7070dbSScott Long 
39824c7070dbSScott Long 	pci_vendor_id = pci_get_vendor(dev);
39834c7070dbSScott Long 	pci_device_id = pci_get_device(dev);
39844c7070dbSScott Long 	pci_subvendor_id = pci_get_subvendor(dev);
39854c7070dbSScott Long 	pci_subdevice_id = pci_get_subdevice(dev);
39864c7070dbSScott Long 	pci_rev_id = pci_get_revid(dev);
39874c7070dbSScott Long 	if (sctx->isc_parse_devinfo != NULL)
39884c7070dbSScott Long 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
39894c7070dbSScott Long 
39904c7070dbSScott Long 	ent = sctx->isc_vendor_info;
39914c7070dbSScott Long 	while (ent->pvi_vendor_id != 0) {
39924c7070dbSScott Long 		if (pci_vendor_id != ent->pvi_vendor_id) {
39934c7070dbSScott Long 			ent++;
39944c7070dbSScott Long 			continue;
39954c7070dbSScott Long 		}
39964c7070dbSScott Long 		if ((pci_device_id == ent->pvi_device_id) &&
39974c7070dbSScott Long 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
39984c7070dbSScott Long 		     (ent->pvi_subvendor_id == 0)) &&
39994c7070dbSScott Long 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
40004c7070dbSScott Long 		     (ent->pvi_subdevice_id == 0)) &&
40014c7070dbSScott Long 		    ((pci_rev_id == ent->pvi_rev_id) ||
40024c7070dbSScott Long 		     (ent->pvi_rev_id == 0))) {
40034c7070dbSScott Long 
40044c7070dbSScott Long 			device_set_desc_copy(dev, ent->pvi_name);
40054c7070dbSScott Long 			/* this needs to be changed to zero if the bus probing code
40064c7070dbSScott Long 			 * ever stops re-probing on best match because the sctx
40074c7070dbSScott Long 			 * may have its values over written by register calls
40084c7070dbSScott Long 			 * in subsequent probes
40094c7070dbSScott Long 			 */
40104c7070dbSScott Long 			return (BUS_PROBE_DEFAULT);
40114c7070dbSScott Long 		}
40124c7070dbSScott Long 		ent++;
40134c7070dbSScott Long 	}
40144c7070dbSScott Long 	return (ENXIO);
40154c7070dbSScott Long }
40164c7070dbSScott Long 
40174c7070dbSScott Long int
40184c7070dbSScott Long iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
40194c7070dbSScott Long {
40204c7070dbSScott Long 	int err, rid, msix, msix_bar;
40214c7070dbSScott Long 	if_ctx_t ctx;
40224c7070dbSScott Long 	if_t ifp;
40234c7070dbSScott Long 	if_softc_ctx_t scctx;
402423ac9029SStephen Hurd 	int i;
402523ac9029SStephen Hurd 	uint16_t main_txq;
402623ac9029SStephen Hurd 	uint16_t main_rxq;
40274c7070dbSScott Long 
40284c7070dbSScott Long 
40294c7070dbSScott Long 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
40304c7070dbSScott Long 
40314c7070dbSScott Long 	if (sc == NULL) {
40324c7070dbSScott Long 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
40334c7070dbSScott Long 		device_set_softc(dev, ctx);
403423ac9029SStephen Hurd 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
40354c7070dbSScott Long 	}
40364c7070dbSScott Long 
40374c7070dbSScott Long 	ctx->ifc_sctx = sctx;
40384c7070dbSScott Long 	ctx->ifc_dev = dev;
40394c7070dbSScott Long 	ctx->ifc_softc = sc;
40404c7070dbSScott Long 
40414c7070dbSScott Long 	if ((err = iflib_register(ctx)) != 0) {
40424c7070dbSScott Long 		device_printf(dev, "iflib_register failed %d\n", err);
40434c7070dbSScott Long 		return (err);
40444c7070dbSScott Long 	}
40454c7070dbSScott Long 	iflib_add_device_sysctl_pre(ctx);
404623ac9029SStephen Hurd 
404723ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
40481248952aSSean Bruno 	ifp = ctx->ifc_ifp;
40491248952aSSean Bruno 
405023ac9029SStephen Hurd 	/*
405123ac9029SStephen Hurd 	 * XXX sanity check that ntxd & nrxd are a power of 2
405223ac9029SStephen Hurd 	 */
405323ac9029SStephen Hurd 	if (ctx->ifc_sysctl_ntxqs != 0)
405423ac9029SStephen Hurd 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
405523ac9029SStephen Hurd 	if (ctx->ifc_sysctl_nrxqs != 0)
405623ac9029SStephen Hurd 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
405723ac9029SStephen Hurd 
405823ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
405923ac9029SStephen Hurd 		if (ctx->ifc_sysctl_ntxds[i] != 0)
406023ac9029SStephen Hurd 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
406123ac9029SStephen Hurd 		else
406223ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
406323ac9029SStephen Hurd 	}
406423ac9029SStephen Hurd 
406523ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
406623ac9029SStephen Hurd 		if (ctx->ifc_sysctl_nrxds[i] != 0)
406723ac9029SStephen Hurd 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
406823ac9029SStephen Hurd 		else
406923ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
407023ac9029SStephen Hurd 	}
407123ac9029SStephen Hurd 
407223ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
407323ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
407423ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
407523ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
407623ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
407723ac9029SStephen Hurd 		}
407823ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
407923ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
408023ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
408123ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
408223ac9029SStephen Hurd 		}
408323ac9029SStephen Hurd 	}
408423ac9029SStephen Hurd 
408523ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
408623ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
408723ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
408823ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
408923ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
409023ac9029SStephen Hurd 		}
409123ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
409223ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
409323ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
409423ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
409523ac9029SStephen Hurd 		}
409623ac9029SStephen Hurd 	}
4097ab2e3f79SStephen Hurd 
4098ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
40994c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
41004c7070dbSScott Long 		return (err);
41014c7070dbSScott Long 	}
41021248952aSSean Bruno 	_iflib_pre_assert(scctx);
41031248952aSSean Bruno 	ctx->ifc_txrx = *scctx->isc_txrx;
41041248952aSSean Bruno 
41051248952aSSean Bruno #ifdef INVARIANTS
41061248952aSSean Bruno 	MPASS(scctx->isc_capenable);
41071248952aSSean Bruno 	if (scctx->isc_capenable & IFCAP_TXCSUM)
41081248952aSSean Bruno 		MPASS(scctx->isc_tx_csum_flags);
41091248952aSSean Bruno #endif
41101248952aSSean Bruno 
411118a660b3SSean Bruno 	if_setcapabilities(ifp, scctx->isc_capenable | IFCAP_HWSTATS);
411218a660b3SSean Bruno 	if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS);
41131248952aSSean Bruno 
41141248952aSSean Bruno 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
41151248952aSSean Bruno 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
41161248952aSSean Bruno 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
41171248952aSSean Bruno 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
411823ac9029SStephen Hurd 
41194c7070dbSScott Long #ifdef ACPI_DMAR
41204c7070dbSScott Long 	if (dmar_get_dma_tag(device_get_parent(dev), dev) != NULL)
41214c7070dbSScott Long 		ctx->ifc_flags |= IFC_DMAR;
412295246abbSSean Bruno #elif !(defined(__i386__) || defined(__amd64__))
412395246abbSSean Bruno 	/* set unconditionally for !x86 */
412495246abbSSean Bruno 	ctx->ifc_flags |= IFC_DMAR;
41254c7070dbSScott Long #endif
41264c7070dbSScott Long 
41274c7070dbSScott Long 	msix_bar = scctx->isc_msix_bar;
412895246abbSSean Bruno 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
412995246abbSSean Bruno 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
413023ac9029SStephen Hurd 
413123ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
413223ac9029SStephen Hurd 	device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
413323ac9029SStephen Hurd 		      scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
413423ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
413523ac9029SStephen Hurd 		if (!powerof2(scctx->isc_nrxd[i])) {
413623ac9029SStephen Hurd 			/* round down instead? */
413723ac9029SStephen Hurd 			device_printf(dev, "# rx descriptors must be a power of 2\n");
413823ac9029SStephen Hurd 			err = EINVAL;
413923ac9029SStephen Hurd 			goto fail;
414023ac9029SStephen Hurd 		}
414123ac9029SStephen Hurd 	}
414223ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
414323ac9029SStephen Hurd 		if (!powerof2(scctx->isc_ntxd[i])) {
414423ac9029SStephen Hurd 			device_printf(dev,
414523ac9029SStephen Hurd 			    "# tx descriptors must be a power of 2");
414623ac9029SStephen Hurd 			err = EINVAL;
414723ac9029SStephen Hurd 			goto fail;
414823ac9029SStephen Hurd 		}
414923ac9029SStephen Hurd 	}
415023ac9029SStephen Hurd 
415123ac9029SStephen Hurd 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
415223ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
415323ac9029SStephen Hurd 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
415423ac9029SStephen Hurd 		    MAX_SINGLE_PACKET_FRACTION);
415523ac9029SStephen Hurd 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
415623ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
415723ac9029SStephen Hurd 		scctx->isc_tx_tso_segments_max = max(1,
415823ac9029SStephen Hurd 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
41594c7070dbSScott Long 
41604c7070dbSScott Long 	/*
41614c7070dbSScott Long 	 * Protect the stack against modern hardware
41624c7070dbSScott Long 	 */
41634c7070dbSScott Long 	if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
41644c7070dbSScott Long 		scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
41654c7070dbSScott Long 
41664c7070dbSScott Long 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
41674c7070dbSScott Long 	ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
41684c7070dbSScott Long 	ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
41694c7070dbSScott Long 	ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
41704c7070dbSScott Long 	if (scctx->isc_rss_table_size == 0)
41714c7070dbSScott Long 		scctx->isc_rss_table_size = 64;
417223ac9029SStephen Hurd 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
4173da69b8f9SSean Bruno 
4174da69b8f9SSean Bruno 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
4175da69b8f9SSean Bruno 	/* XXX format name */
4176ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
41774c7070dbSScott Long 	/*
41784c7070dbSScott Long 	** Now setup MSI or MSI/X, should
41794c7070dbSScott Long 	** return us the number of supported
41804c7070dbSScott Long 	** vectors. (Will be 1 for MSI)
41814c7070dbSScott Long 	*/
41824c7070dbSScott Long 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
41834c7070dbSScott Long 		msix = scctx->isc_vectors;
41844c7070dbSScott Long 	} else if (scctx->isc_msix_bar != 0)
4185f7ae9a84SSean Bruno 	       /*
4186f7ae9a84SSean Bruno 		* The simple fact that isc_msix_bar is not 0 does not mean we
4187f7ae9a84SSean Bruno 		* we have a good value there that is known to work.
4188f7ae9a84SSean Bruno 		*/
41894c7070dbSScott Long 		msix = iflib_msix_init(ctx);
41904c7070dbSScott Long 	else {
41914c7070dbSScott Long 		scctx->isc_vectors = 1;
41924c7070dbSScott Long 		scctx->isc_ntxqsets = 1;
41934c7070dbSScott Long 		scctx->isc_nrxqsets = 1;
41944c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
41954c7070dbSScott Long 		msix = 0;
41964c7070dbSScott Long 	}
41974c7070dbSScott Long 	/* Get memory for the station queues */
41984c7070dbSScott Long 	if ((err = iflib_queues_alloc(ctx))) {
41994c7070dbSScott Long 		device_printf(dev, "Unable to allocate queue memory\n");
42004c7070dbSScott Long 		goto fail;
42014c7070dbSScott Long 	}
42024c7070dbSScott Long 
42034c7070dbSScott Long 	if ((err = iflib_qset_structures_setup(ctx))) {
42044c7070dbSScott Long 		device_printf(dev, "qset structure setup failed %d\n", err);
42054c7070dbSScott Long 		goto fail_queues;
42064c7070dbSScott Long 	}
420769b7fc3eSSean Bruno 
4208bd84f700SSean Bruno 	/*
4209bd84f700SSean Bruno 	 * Group taskqueues aren't properly set up until SMP is started,
4210bd84f700SSean Bruno 	 * so we disable interrupts until we can handle them post
4211bd84f700SSean Bruno 	 * SI_SUB_SMP.
4212bd84f700SSean Bruno 	 *
4213bd84f700SSean Bruno 	 * XXX: disabling interrupts doesn't actually work, at least for
4214bd84f700SSean Bruno 	 * the non-MSI case.  When they occur before SI_SUB_SMP completes,
4215bd84f700SSean Bruno 	 * we do null handling and depend on this not causing too large an
4216bd84f700SSean Bruno 	 * interrupt storm.
4217bd84f700SSean Bruno 	 */
42181248952aSSean Bruno 	IFDI_INTR_DISABLE(ctx);
42194c7070dbSScott Long 	if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
42204c7070dbSScott Long 		device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
42214c7070dbSScott Long 		goto fail_intr_free;
42224c7070dbSScott Long 	}
42234c7070dbSScott Long 	if (msix <= 1) {
42244c7070dbSScott Long 		rid = 0;
42254c7070dbSScott Long 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
42264c7070dbSScott Long 			MPASS(msix == 1);
42274c7070dbSScott Long 			rid = 1;
42284c7070dbSScott Long 		}
422923ac9029SStephen Hurd 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
42304c7070dbSScott Long 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
42314c7070dbSScott Long 			goto fail_intr_free;
42324c7070dbSScott Long 		}
42334c7070dbSScott Long 	}
42344c7070dbSScott Long 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
4235ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
42364c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
42374c7070dbSScott Long 		goto fail_detach;
42384c7070dbSScott Long 	}
42394c7070dbSScott Long 	if ((err = iflib_netmap_attach(ctx))) {
42404c7070dbSScott Long 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
42414c7070dbSScott Long 		goto fail_detach;
42424c7070dbSScott Long 	}
42434c7070dbSScott Long 	*ctxp = ctx;
42444c7070dbSScott Long 
424523ac9029SStephen Hurd 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
42464c7070dbSScott Long 	iflib_add_device_sysctl_post(ctx);
42474ecb427aSSean Bruno 	ctx->ifc_flags |= IFC_INIT_DONE;
42484c7070dbSScott Long 	return (0);
42494c7070dbSScott Long fail_detach:
42504c7070dbSScott Long 	ether_ifdetach(ctx->ifc_ifp);
42514c7070dbSScott Long fail_intr_free:
42524c7070dbSScott Long 	if (scctx->isc_intr == IFLIB_INTR_MSIX || scctx->isc_intr == IFLIB_INTR_MSI)
42534c7070dbSScott Long 		pci_release_msi(ctx->ifc_dev);
42544c7070dbSScott Long fail_queues:
42554c7070dbSScott Long 	/* XXX free queues */
42564c7070dbSScott Long fail:
42574c7070dbSScott Long 	IFDI_DETACH(ctx);
42584c7070dbSScott Long 	return (err);
42594c7070dbSScott Long }
42604c7070dbSScott Long 
42614c7070dbSScott Long int
42624c7070dbSScott Long iflib_device_attach(device_t dev)
42634c7070dbSScott Long {
42644c7070dbSScott Long 	if_ctx_t ctx;
42654c7070dbSScott Long 	if_shared_ctx_t sctx;
42664c7070dbSScott Long 
42674c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
42684c7070dbSScott Long 		return (ENOTSUP);
42694c7070dbSScott Long 
42704c7070dbSScott Long 	pci_enable_busmaster(dev);
42714c7070dbSScott Long 
42724c7070dbSScott Long 	return (iflib_device_register(dev, NULL, sctx, &ctx));
42734c7070dbSScott Long }
42744c7070dbSScott Long 
42754c7070dbSScott Long int
42764c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx)
42774c7070dbSScott Long {
42784c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
42794c7070dbSScott Long 	iflib_txq_t txq;
42804c7070dbSScott Long 	iflib_rxq_t rxq;
42814c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
428287890dbaSSean Bruno 	int i, j;
42834c7070dbSScott Long 	struct taskqgroup *tqg;
428487890dbaSSean Bruno 	iflib_fl_t fl;
42854c7070dbSScott Long 
42864c7070dbSScott Long 	/* Make sure VLANS are not using driver */
42874c7070dbSScott Long 	if (if_vlantrunkinuse(ifp)) {
42884c7070dbSScott Long 		device_printf(dev,"Vlan in use, detach first\n");
42894c7070dbSScott Long 		return (EBUSY);
42904c7070dbSScott Long 	}
42914c7070dbSScott Long 
42924c7070dbSScott Long 	CTX_LOCK(ctx);
42934c7070dbSScott Long 	ctx->ifc_in_detach = 1;
42944c7070dbSScott Long 	iflib_stop(ctx);
42954c7070dbSScott Long 	CTX_UNLOCK(ctx);
42964c7070dbSScott Long 
42974c7070dbSScott Long 	/* Unregister VLAN events */
42984c7070dbSScott Long 	if (ctx->ifc_vlan_attach_event != NULL)
42994c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
43004c7070dbSScott Long 	if (ctx->ifc_vlan_detach_event != NULL)
43014c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
43024c7070dbSScott Long 
43034c7070dbSScott Long 	iflib_netmap_detach(ifp);
43044c7070dbSScott Long 	ether_ifdetach(ifp);
4305ab2e3f79SStephen Hurd 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
4306ab2e3f79SStephen Hurd 	CTX_LOCK_DESTROY(ctx);
43074c7070dbSScott Long 	if (ctx->ifc_led_dev != NULL)
43084c7070dbSScott Long 		led_destroy(ctx->ifc_led_dev);
43094c7070dbSScott Long 	/* XXX drain any dependent tasks */
4310ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
431123ac9029SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
43124c7070dbSScott Long 		callout_drain(&txq->ift_timer);
43134c7070dbSScott Long 		if (txq->ift_task.gt_uniq != NULL)
43144c7070dbSScott Long 			taskqgroup_detach(tqg, &txq->ift_task);
43154c7070dbSScott Long 	}
43164c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
43174c7070dbSScott Long 		if (rxq->ifr_task.gt_uniq != NULL)
43184c7070dbSScott Long 			taskqgroup_detach(tqg, &rxq->ifr_task);
431987890dbaSSean Bruno 
432087890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
432187890dbaSSean Bruno 			free(fl->ifl_rx_bitmap, M_IFLIB);
432287890dbaSSean Bruno 
43234c7070dbSScott Long 	}
4324ab2e3f79SStephen Hurd 	tqg = qgroup_if_config_tqg;
43254c7070dbSScott Long 	if (ctx->ifc_admin_task.gt_uniq != NULL)
43264c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
43274c7070dbSScott Long 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
43284c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
43294c7070dbSScott Long 
43304c7070dbSScott Long 	IFDI_DETACH(ctx);
433123ac9029SStephen Hurd 	device_set_softc(ctx->ifc_dev, NULL);
43324c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
43334c7070dbSScott Long 		pci_release_msi(dev);
43344c7070dbSScott Long 	}
43354c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
43364c7070dbSScott Long 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
43374c7070dbSScott Long 	}
43384c7070dbSScott Long 	if (ctx->ifc_msix_mem != NULL) {
43394c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
43404c7070dbSScott Long 			ctx->ifc_softc_ctx.isc_msix_bar, ctx->ifc_msix_mem);
43414c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
43424c7070dbSScott Long 	}
43434c7070dbSScott Long 
43444c7070dbSScott Long 	bus_generic_detach(dev);
43454c7070dbSScott Long 	if_free(ifp);
43464c7070dbSScott Long 
43474c7070dbSScott Long 	iflib_tx_structures_free(ctx);
43484c7070dbSScott Long 	iflib_rx_structures_free(ctx);
434923ac9029SStephen Hurd 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
435023ac9029SStephen Hurd 		free(ctx->ifc_softc, M_IFLIB);
435123ac9029SStephen Hurd 	free(ctx, M_IFLIB);
43524c7070dbSScott Long 	return (0);
43534c7070dbSScott Long }
43544c7070dbSScott Long 
43554c7070dbSScott Long 
43564c7070dbSScott Long int
43574c7070dbSScott Long iflib_device_detach(device_t dev)
43584c7070dbSScott Long {
43594c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
43604c7070dbSScott Long 
43614c7070dbSScott Long 	return (iflib_device_deregister(ctx));
43624c7070dbSScott Long }
43634c7070dbSScott Long 
43644c7070dbSScott Long int
43654c7070dbSScott Long iflib_device_suspend(device_t dev)
43664c7070dbSScott Long {
43674c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
43684c7070dbSScott Long 
43694c7070dbSScott Long 	CTX_LOCK(ctx);
43704c7070dbSScott Long 	IFDI_SUSPEND(ctx);
43714c7070dbSScott Long 	CTX_UNLOCK(ctx);
43724c7070dbSScott Long 
43734c7070dbSScott Long 	return bus_generic_suspend(dev);
43744c7070dbSScott Long }
43754c7070dbSScott Long int
43764c7070dbSScott Long iflib_device_shutdown(device_t dev)
43774c7070dbSScott Long {
43784c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
43794c7070dbSScott Long 
43804c7070dbSScott Long 	CTX_LOCK(ctx);
43814c7070dbSScott Long 	IFDI_SHUTDOWN(ctx);
43824c7070dbSScott Long 	CTX_UNLOCK(ctx);
43834c7070dbSScott Long 
43844c7070dbSScott Long 	return bus_generic_suspend(dev);
43854c7070dbSScott Long }
43864c7070dbSScott Long 
43874c7070dbSScott Long 
43884c7070dbSScott Long int
43894c7070dbSScott Long iflib_device_resume(device_t dev)
43904c7070dbSScott Long {
43914c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
43924c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
43934c7070dbSScott Long 
43944c7070dbSScott Long 	CTX_LOCK(ctx);
43954c7070dbSScott Long 	IFDI_RESUME(ctx);
43964c7070dbSScott Long 	iflib_init_locked(ctx);
43974c7070dbSScott Long 	CTX_UNLOCK(ctx);
43984c7070dbSScott Long 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
43994c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
44004c7070dbSScott Long 
44014c7070dbSScott Long 	return (bus_generic_resume(dev));
44024c7070dbSScott Long }
44034c7070dbSScott Long 
44044c7070dbSScott Long int
44054c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
44064c7070dbSScott Long {
44074c7070dbSScott Long 	int error;
44084c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
44094c7070dbSScott Long 
44104c7070dbSScott Long 	CTX_LOCK(ctx);
44114c7070dbSScott Long 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
44124c7070dbSScott Long 	CTX_UNLOCK(ctx);
44134c7070dbSScott Long 
44144c7070dbSScott Long 	return (error);
44154c7070dbSScott Long }
44164c7070dbSScott Long 
44174c7070dbSScott Long void
44184c7070dbSScott Long iflib_device_iov_uninit(device_t dev)
44194c7070dbSScott Long {
44204c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
44214c7070dbSScott Long 
44224c7070dbSScott Long 	CTX_LOCK(ctx);
44234c7070dbSScott Long 	IFDI_IOV_UNINIT(ctx);
44244c7070dbSScott Long 	CTX_UNLOCK(ctx);
44254c7070dbSScott Long }
44264c7070dbSScott Long 
44274c7070dbSScott Long int
44284c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
44294c7070dbSScott Long {
44304c7070dbSScott Long 	int error;
44314c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
44324c7070dbSScott Long 
44334c7070dbSScott Long 	CTX_LOCK(ctx);
44344c7070dbSScott Long 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
44354c7070dbSScott Long 	CTX_UNLOCK(ctx);
44364c7070dbSScott Long 
44374c7070dbSScott Long 	return (error);
44384c7070dbSScott Long }
44394c7070dbSScott Long 
44404c7070dbSScott Long /*********************************************************************
44414c7070dbSScott Long  *
44424c7070dbSScott Long  *  MODULE FUNCTION DEFINITIONS
44434c7070dbSScott Long  *
44444c7070dbSScott Long  **********************************************************************/
44454c7070dbSScott Long 
4446ab2e3f79SStephen Hurd /*
4447ab2e3f79SStephen Hurd  * - Start a fast taskqueue thread for each core
4448ab2e3f79SStephen Hurd  * - Start a taskqueue for control operations
4449ab2e3f79SStephen Hurd  */
44504c7070dbSScott Long static int
44514c7070dbSScott Long iflib_module_init(void)
44524c7070dbSScott Long {
44534c7070dbSScott Long 	return (0);
44544c7070dbSScott Long }
44554c7070dbSScott Long 
44564c7070dbSScott Long static int
44574c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg)
44584c7070dbSScott Long {
44594c7070dbSScott Long 	int err;
44604c7070dbSScott Long 
44614c7070dbSScott Long 	switch (what) {
44624c7070dbSScott Long 	case MOD_LOAD:
44634c7070dbSScott Long 		if ((err = iflib_module_init()) != 0)
44644c7070dbSScott Long 			return (err);
44654c7070dbSScott Long 		break;
44664c7070dbSScott Long 	case MOD_UNLOAD:
44674c7070dbSScott Long 		return (EBUSY);
44684c7070dbSScott Long 	default:
44694c7070dbSScott Long 		return (EOPNOTSUPP);
44704c7070dbSScott Long 	}
44714c7070dbSScott Long 
44724c7070dbSScott Long 	return (0);
44734c7070dbSScott Long }
44744c7070dbSScott Long 
44754c7070dbSScott Long /*********************************************************************
44764c7070dbSScott Long  *
44774c7070dbSScott Long  *  PUBLIC FUNCTION DEFINITIONS
44784c7070dbSScott Long  *     ordered as in iflib.h
44794c7070dbSScott Long  *
44804c7070dbSScott Long  **********************************************************************/
44814c7070dbSScott Long 
44824c7070dbSScott Long 
44834c7070dbSScott Long static void
44844c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx)
44854c7070dbSScott Long {
44864c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsize);
44874c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsegsize);
44884c7070dbSScott Long 
44894c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsize);
44904c7070dbSScott Long 	MPASS(sctx->isc_rx_nsegments);
44914c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsegsize);
44924c7070dbSScott Long 
449323ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_min[0]);
449423ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_max[0]);
449523ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_default[0]);
449623ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_min[0]);
449723ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_max[0]);
449823ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_default[0]);
44994c7070dbSScott Long }
45004c7070dbSScott Long 
45011248952aSSean Bruno static void
45021248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx)
45031248952aSSean Bruno {
45041248952aSSean Bruno 
45051248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_encap);
45061248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_flush);
45071248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
45081248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_available);
45091248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
45101248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_refill);
45111248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_flush);
45121248952aSSean Bruno }
45132fe66646SSean Bruno 
45144c7070dbSScott Long static int
45154c7070dbSScott Long iflib_register(if_ctx_t ctx)
45164c7070dbSScott Long {
45174c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
45184c7070dbSScott Long 	driver_t *driver = sctx->isc_driver;
45194c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
45204c7070dbSScott Long 	if_t ifp;
45214c7070dbSScott Long 
45224c7070dbSScott Long 	_iflib_assert(sctx);
45234c7070dbSScott Long 
45244c7070dbSScott Long 	CTX_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
45254c7070dbSScott Long 
45264c7070dbSScott Long 	ifp = ctx->ifc_ifp = if_gethandle(IFT_ETHER);
45274c7070dbSScott Long 	if (ifp == NULL) {
45284c7070dbSScott Long 		device_printf(dev, "can not allocate ifnet structure\n");
45294c7070dbSScott Long 		return (ENOMEM);
45304c7070dbSScott Long 	}
45314c7070dbSScott Long 
45324c7070dbSScott Long 	/*
45334c7070dbSScott Long 	 * Initialize our context's device specific methods
45344c7070dbSScott Long 	 */
45354c7070dbSScott Long 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
45364c7070dbSScott Long 	kobj_class_compile((kobj_class_t) driver);
45374c7070dbSScott Long 	driver->refs++;
45384c7070dbSScott Long 
45394c7070dbSScott Long 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
45404c7070dbSScott Long 	if_setsoftc(ifp, ctx);
45414c7070dbSScott Long 	if_setdev(ifp, dev);
45424c7070dbSScott Long 	if_setinitfn(ifp, iflib_if_init);
45434c7070dbSScott Long 	if_setioctlfn(ifp, iflib_if_ioctl);
45444c7070dbSScott Long 	if_settransmitfn(ifp, iflib_if_transmit);
45454c7070dbSScott Long 	if_setqflushfn(ifp, iflib_if_qflush);
45464c7070dbSScott Long 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
45474c7070dbSScott Long 
45484c7070dbSScott Long 	ctx->ifc_vlan_attach_event =
45494c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
45504c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
45514c7070dbSScott Long 	ctx->ifc_vlan_detach_event =
45524c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
45534c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
45544c7070dbSScott Long 
45554c7070dbSScott Long 	ifmedia_init(&ctx->ifc_media, IFM_IMASK,
45564c7070dbSScott Long 					 iflib_media_change, iflib_media_status);
45574c7070dbSScott Long 
45584c7070dbSScott Long 	return (0);
45594c7070dbSScott Long }
45604c7070dbSScott Long 
45614c7070dbSScott Long 
45624c7070dbSScott Long static int
45634c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx)
45644c7070dbSScott Long {
45654c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
456623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
45674c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
456823ac9029SStephen Hurd 	int nrxqsets = scctx->isc_nrxqsets;
456923ac9029SStephen Hurd 	int ntxqsets = scctx->isc_ntxqsets;
45704c7070dbSScott Long 	iflib_txq_t txq;
45714c7070dbSScott Long 	iflib_rxq_t rxq;
45724c7070dbSScott Long 	iflib_fl_t fl = NULL;
457323ac9029SStephen Hurd 	int i, j, cpu, err, txconf, rxconf;
45744c7070dbSScott Long 	iflib_dma_info_t ifdip;
457523ac9029SStephen Hurd 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
457623ac9029SStephen Hurd 	uint32_t *txqsizes = scctx->isc_txqsizes;
45774c7070dbSScott Long 	uint8_t nrxqs = sctx->isc_nrxqs;
45784c7070dbSScott Long 	uint8_t ntxqs = sctx->isc_ntxqs;
45794c7070dbSScott Long 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
45804c7070dbSScott Long 	caddr_t *vaddrs;
45814c7070dbSScott Long 	uint64_t *paddrs;
45824c7070dbSScott Long 	struct ifmp_ring **brscp;
45834c7070dbSScott Long 
458423ac9029SStephen Hurd 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
458523ac9029SStephen Hurd 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
45864c7070dbSScott Long 
45870d0338afSConrad Meyer 	brscp = NULL;
458823ac9029SStephen Hurd 	txq = NULL;
45890d0338afSConrad Meyer 	rxq = NULL;
45900d0338afSConrad Meyer 
45914c7070dbSScott Long /* Allocate the TX ring struct memory */
45924c7070dbSScott Long 	if (!(txq =
45934c7070dbSScott Long 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
45944c7070dbSScott Long 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
45954c7070dbSScott Long 		device_printf(dev, "Unable to allocate TX ring memory\n");
45964c7070dbSScott Long 		err = ENOMEM;
45974c7070dbSScott Long 		goto fail;
45984c7070dbSScott Long 	}
45994c7070dbSScott Long 
46004c7070dbSScott Long 	/* Now allocate the RX */
46014c7070dbSScott Long 	if (!(rxq =
46024c7070dbSScott Long 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
46034c7070dbSScott Long 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
46044c7070dbSScott Long 		device_printf(dev, "Unable to allocate RX ring memory\n");
46054c7070dbSScott Long 		err = ENOMEM;
46064c7070dbSScott Long 		goto rx_fail;
46074c7070dbSScott Long 	}
46084c7070dbSScott Long 
46094c7070dbSScott Long 	ctx->ifc_txqs = txq;
46104c7070dbSScott Long 	ctx->ifc_rxqs = rxq;
46114c7070dbSScott Long 
46124c7070dbSScott Long 	/*
46134c7070dbSScott Long 	 * XXX handle allocation failure
46144c7070dbSScott Long 	 */
461596c85efbSNathan Whitehorn 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
46164c7070dbSScott Long 		/* Set up some basics */
46174c7070dbSScott Long 
46184c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
46194c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
46204c7070dbSScott Long 			err = ENOMEM;
46210d0338afSConrad Meyer 			goto err_tx_desc;
46224c7070dbSScott Long 		}
46234c7070dbSScott Long 		txq->ift_ifdi = ifdip;
46244c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, ifdip++) {
46254c7070dbSScott Long 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
46264c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
46274c7070dbSScott Long 				err = ENOMEM;
46284c7070dbSScott Long 				goto err_tx_desc;
46294c7070dbSScott Long 			}
463095246abbSSean Bruno 			txq->ift_txd_size[j] = scctx->isc_txd_size[j];
46314c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
46324c7070dbSScott Long 		}
46334c7070dbSScott Long 		txq->ift_ctx = ctx;
46344c7070dbSScott Long 		txq->ift_id = i;
463523ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
463623ac9029SStephen Hurd 			txq->ift_br_offset = 1;
463723ac9029SStephen Hurd 		} else {
463823ac9029SStephen Hurd 			txq->ift_br_offset = 0;
463923ac9029SStephen Hurd 		}
46404c7070dbSScott Long 		/* XXX fix this */
464196c85efbSNathan Whitehorn 		txq->ift_timer.c_cpu = cpu;
46424c7070dbSScott Long 
46434c7070dbSScott Long 		if (iflib_txsd_alloc(txq)) {
46444c7070dbSScott Long 			device_printf(dev, "Critical Failure setting up TX buffers\n");
46454c7070dbSScott Long 			err = ENOMEM;
46464c7070dbSScott Long 			goto err_tx_desc;
46474c7070dbSScott Long 		}
46484c7070dbSScott Long 
46494c7070dbSScott Long 		/* Initialize the TX lock */
46504c7070dbSScott Long 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout",
46514c7070dbSScott Long 		    device_get_nameunit(dev), txq->ift_id);
46524c7070dbSScott Long 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
46534c7070dbSScott Long 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
46544c7070dbSScott Long 
46554c7070dbSScott Long 		snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db",
46564c7070dbSScott Long 			 device_get_nameunit(dev), txq->ift_id);
46574c7070dbSScott Long 
465895246abbSSean Bruno 		err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
46594c7070dbSScott Long 				      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
46604c7070dbSScott Long 		if (err) {
46614c7070dbSScott Long 			/* XXX free any allocated rings */
46624c7070dbSScott Long 			device_printf(dev, "Unable to allocate buf_ring\n");
46630d0338afSConrad Meyer 			goto err_tx_desc;
46644c7070dbSScott Long 		}
46654c7070dbSScott Long 	}
46664c7070dbSScott Long 
46674c7070dbSScott Long 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
46684c7070dbSScott Long 		/* Set up some basics */
46694c7070dbSScott Long 
46704c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
46714c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
46724c7070dbSScott Long 			err = ENOMEM;
46730d0338afSConrad Meyer 			goto err_tx_desc;
46744c7070dbSScott Long 		}
46754c7070dbSScott Long 
46764c7070dbSScott Long 		rxq->ifr_ifdi = ifdip;
467795246abbSSean Bruno 		/* XXX this needs to be changed if #rx queues != #tx queues */
467895246abbSSean Bruno 		rxq->ifr_ntxqirq = 1;
467995246abbSSean Bruno 		rxq->ifr_txqid[0] = i;
46804c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, ifdip++) {
46814c7070dbSScott Long 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
46824c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
46834c7070dbSScott Long 				err = ENOMEM;
46844c7070dbSScott Long 				goto err_tx_desc;
46854c7070dbSScott Long 			}
46864c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
46874c7070dbSScott Long 		}
46884c7070dbSScott Long 		rxq->ifr_ctx = ctx;
46894c7070dbSScott Long 		rxq->ifr_id = i;
469023ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
469123ac9029SStephen Hurd 			rxq->ifr_fl_offset = 1;
46924c7070dbSScott Long 		} else {
469323ac9029SStephen Hurd 			rxq->ifr_fl_offset = 0;
46944c7070dbSScott Long 		}
46954c7070dbSScott Long 		rxq->ifr_nfl = nfree_lists;
46964c7070dbSScott Long 		if (!(fl =
46974c7070dbSScott Long 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
46984c7070dbSScott Long 			device_printf(dev, "Unable to allocate free list memory\n");
46994c7070dbSScott Long 			err = ENOMEM;
47000d0338afSConrad Meyer 			goto err_tx_desc;
47014c7070dbSScott Long 		}
47024c7070dbSScott Long 		rxq->ifr_fl = fl;
47034c7070dbSScott Long 		for (j = 0; j < nfree_lists; j++) {
470495246abbSSean Bruno 			fl[j].ifl_rxq = rxq;
470595246abbSSean Bruno 			fl[j].ifl_id = j;
470695246abbSSean Bruno 			fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
470795246abbSSean Bruno 			fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
47084c7070dbSScott Long 		}
47094c7070dbSScott Long         /* Allocate receive buffers for the ring*/
47104c7070dbSScott Long 		if (iflib_rxsd_alloc(rxq)) {
47114c7070dbSScott Long 			device_printf(dev,
47124c7070dbSScott Long 			    "Critical Failure setting up receive buffers\n");
47134c7070dbSScott Long 			err = ENOMEM;
47144c7070dbSScott Long 			goto err_rx_desc;
47154c7070dbSScott Long 		}
471687890dbaSSean Bruno 
471787890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
471887890dbaSSean Bruno 			fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, M_WAITOK|M_ZERO);
47194c7070dbSScott Long 	}
47204c7070dbSScott Long 
47214c7070dbSScott Long 	/* TXQs */
47224c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
47234c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
47244c7070dbSScott Long 	for (i = 0; i < ntxqsets; i++) {
47254c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
47264c7070dbSScott Long 
47274c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, di++) {
47284c7070dbSScott Long 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
47294c7070dbSScott Long 			paddrs[i*ntxqs + j] = di->idi_paddr;
47304c7070dbSScott Long 		}
47314c7070dbSScott Long 	}
47324c7070dbSScott Long 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
47334c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
47344c7070dbSScott Long 		iflib_tx_structures_free(ctx);
47354c7070dbSScott Long 		free(vaddrs, M_IFLIB);
47364c7070dbSScott Long 		free(paddrs, M_IFLIB);
47374c7070dbSScott Long 		goto err_rx_desc;
47384c7070dbSScott Long 	}
47394c7070dbSScott Long 	free(vaddrs, M_IFLIB);
47404c7070dbSScott Long 	free(paddrs, M_IFLIB);
47414c7070dbSScott Long 
47424c7070dbSScott Long 	/* RXQs */
47434c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
47444c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
47454c7070dbSScott Long 	for (i = 0; i < nrxqsets; i++) {
47464c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
47474c7070dbSScott Long 
47484c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, di++) {
47494c7070dbSScott Long 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
47504c7070dbSScott Long 			paddrs[i*nrxqs + j] = di->idi_paddr;
47514c7070dbSScott Long 		}
47524c7070dbSScott Long 	}
47534c7070dbSScott Long 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
47544c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
47554c7070dbSScott Long 		iflib_tx_structures_free(ctx);
47564c7070dbSScott Long 		free(vaddrs, M_IFLIB);
47574c7070dbSScott Long 		free(paddrs, M_IFLIB);
47584c7070dbSScott Long 		goto err_rx_desc;
47594c7070dbSScott Long 	}
47604c7070dbSScott Long 	free(vaddrs, M_IFLIB);
47614c7070dbSScott Long 	free(paddrs, M_IFLIB);
47624c7070dbSScott Long 
47634c7070dbSScott Long 	return (0);
47644c7070dbSScott Long 
47654c7070dbSScott Long /* XXX handle allocation failure changes */
47664c7070dbSScott Long err_rx_desc:
47674c7070dbSScott Long err_tx_desc:
47684c7070dbSScott Long 	if (ctx->ifc_rxqs != NULL)
47694c7070dbSScott Long 		free(ctx->ifc_rxqs, M_IFLIB);
47704c7070dbSScott Long 	ctx->ifc_rxqs = NULL;
47714c7070dbSScott Long 	if (ctx->ifc_txqs != NULL)
47724c7070dbSScott Long 		free(ctx->ifc_txqs, M_IFLIB);
47734c7070dbSScott Long 	ctx->ifc_txqs = NULL;
47740d0338afSConrad Meyer rx_fail:
47750d0338afSConrad Meyer 	if (brscp != NULL)
47760d0338afSConrad Meyer 		free(brscp, M_IFLIB);
47770d0338afSConrad Meyer 	if (rxq != NULL)
47780d0338afSConrad Meyer 		free(rxq, M_IFLIB);
47790d0338afSConrad Meyer 	if (txq != NULL)
47800d0338afSConrad Meyer 		free(txq, M_IFLIB);
47814c7070dbSScott Long fail:
47824c7070dbSScott Long 	return (err);
47834c7070dbSScott Long }
47844c7070dbSScott Long 
47854c7070dbSScott Long static int
47864c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx)
47874c7070dbSScott Long {
47884c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
47894c7070dbSScott Long 	int i;
47904c7070dbSScott Long 
47914c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
47924c7070dbSScott Long 		iflib_txq_setup(txq);
47934c7070dbSScott Long 
47944c7070dbSScott Long 	return (0);
47954c7070dbSScott Long }
47964c7070dbSScott Long 
47974c7070dbSScott Long static void
47984c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx)
47994c7070dbSScott Long {
48004c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
48014c7070dbSScott Long 	int i, j;
48024c7070dbSScott Long 
48034c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
48044c7070dbSScott Long 		iflib_txq_destroy(txq);
48054c7070dbSScott Long 		for (j = 0; j < ctx->ifc_nhwtxqs; j++)
48064c7070dbSScott Long 			iflib_dma_free(&txq->ift_ifdi[j]);
48074c7070dbSScott Long 	}
48084c7070dbSScott Long 	free(ctx->ifc_txqs, M_IFLIB);
48094c7070dbSScott Long 	ctx->ifc_txqs = NULL;
48104c7070dbSScott Long 	IFDI_QUEUES_FREE(ctx);
48114c7070dbSScott Long }
48124c7070dbSScott Long 
48134c7070dbSScott Long /*********************************************************************
48144c7070dbSScott Long  *
48154c7070dbSScott Long  *  Initialize all receive rings.
48164c7070dbSScott Long  *
48174c7070dbSScott Long  **********************************************************************/
48184c7070dbSScott Long static int
48194c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx)
48204c7070dbSScott Long {
48214c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
4822aaeb188aSBjoern A. Zeeb 	int q;
4823aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
4824aaeb188aSBjoern A. Zeeb 	int i, err;
4825aaeb188aSBjoern A. Zeeb #endif
48264c7070dbSScott Long 
48274c7070dbSScott Long 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
4828aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
48294c7070dbSScott Long 		tcp_lro_free(&rxq->ifr_lc);
483023ac9029SStephen Hurd 		if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
483123ac9029SStephen Hurd 		    TCP_LRO_ENTRIES, min(1024,
483223ac9029SStephen Hurd 		    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) {
48334c7070dbSScott Long 			device_printf(ctx->ifc_dev, "LRO Initialization failed!\n");
48344c7070dbSScott Long 			goto fail;
48354c7070dbSScott Long 		}
48364c7070dbSScott Long 		rxq->ifr_lro_enabled = TRUE;
4837aaeb188aSBjoern A. Zeeb #endif
48384c7070dbSScott Long 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
48394c7070dbSScott Long 	}
48404c7070dbSScott Long 	return (0);
4841aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
48424c7070dbSScott Long fail:
48434c7070dbSScott Long 	/*
48444c7070dbSScott Long 	 * Free RX software descriptors allocated so far, we will only handle
48454c7070dbSScott Long 	 * the rings that completed, the failing case will have
48464c7070dbSScott Long 	 * cleaned up for itself. 'q' failed, so its the terminus.
48474c7070dbSScott Long 	 */
48484c7070dbSScott Long 	rxq = ctx->ifc_rxqs;
48494c7070dbSScott Long 	for (i = 0; i < q; ++i, rxq++) {
48504c7070dbSScott Long 		iflib_rx_sds_free(rxq);
48514c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
48524c7070dbSScott Long 	}
48534c7070dbSScott Long 	return (err);
4854aaeb188aSBjoern A. Zeeb #endif
48554c7070dbSScott Long }
48564c7070dbSScott Long 
48574c7070dbSScott Long /*********************************************************************
48584c7070dbSScott Long  *
48594c7070dbSScott Long  *  Free all receive rings.
48604c7070dbSScott Long  *
48614c7070dbSScott Long  **********************************************************************/
48624c7070dbSScott Long static void
48634c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx)
48644c7070dbSScott Long {
48654c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
48664c7070dbSScott Long 
486723ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
48684c7070dbSScott Long 		iflib_rx_sds_free(rxq);
48694c7070dbSScott Long 	}
48704c7070dbSScott Long }
48714c7070dbSScott Long 
48724c7070dbSScott Long static int
48734c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx)
48744c7070dbSScott Long {
48754c7070dbSScott Long 	int err;
48764c7070dbSScott Long 
48774c7070dbSScott Long 	if ((err = iflib_tx_structures_setup(ctx)) != 0)
48784c7070dbSScott Long 		return (err);
48794c7070dbSScott Long 
48804c7070dbSScott Long 	if ((err = iflib_rx_structures_setup(ctx)) != 0) {
48814c7070dbSScott Long 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
48824c7070dbSScott Long 		iflib_tx_structures_free(ctx);
48834c7070dbSScott Long 		iflib_rx_structures_free(ctx);
48844c7070dbSScott Long 	}
48854c7070dbSScott Long 	return (err);
48864c7070dbSScott Long }
48874c7070dbSScott Long 
48884c7070dbSScott Long int
48894c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
48904c7070dbSScott Long 				driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, char *name)
48914c7070dbSScott Long {
48924c7070dbSScott Long 
48934c7070dbSScott Long 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
48944c7070dbSScott Long }
48954c7070dbSScott Long 
4896aa3c5dd8SSean Bruno static int
4897ab2e3f79SStephen Hurd find_nth(if_ctx_t ctx, cpuset_t *cpus, int qid)
48984c7070dbSScott Long {
4899aa3c5dd8SSean Bruno 	int i, cpuid, eqid, count;
49004c7070dbSScott Long 
4901ab2e3f79SStephen Hurd 	CPU_COPY(&ctx->ifc_cpus, cpus);
4902aa3c5dd8SSean Bruno 	count = CPU_COUNT(&ctx->ifc_cpus);
4903aa3c5dd8SSean Bruno 	eqid = qid % count;
49044c7070dbSScott Long 	/* clear up to the qid'th bit */
4905aa3c5dd8SSean Bruno 	for (i = 0; i < eqid; i++) {
4906ab2e3f79SStephen Hurd 		cpuid = CPU_FFS(cpus);
4907aa3c5dd8SSean Bruno 		MPASS(cpuid != 0);
4908ab2e3f79SStephen Hurd 		CPU_CLR(cpuid-1, cpus);
49094c7070dbSScott Long 	}
4910ab2e3f79SStephen Hurd 	cpuid = CPU_FFS(cpus);
4911aa3c5dd8SSean Bruno 	MPASS(cpuid != 0);
4912aa3c5dd8SSean Bruno 	return (cpuid-1);
49134c7070dbSScott Long }
49144c7070dbSScott Long 
49154c7070dbSScott Long int
49164c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
49174c7070dbSScott Long 						iflib_intr_type_t type, driver_filter_t *filter,
49184c7070dbSScott Long 						void *filter_arg, int qid, char *name)
49194c7070dbSScott Long {
49204c7070dbSScott Long 	struct grouptask *gtask;
49214c7070dbSScott Long 	struct taskqgroup *tqg;
49224c7070dbSScott Long 	iflib_filter_info_t info;
4923ab2e3f79SStephen Hurd 	cpuset_t cpus;
492423ac9029SStephen Hurd 	gtask_fn_t *fn;
4925ab2e3f79SStephen Hurd 	int tqrid, err, cpuid;
492695246abbSSean Bruno 	driver_filter_t *intr_fast;
49274c7070dbSScott Long 	void *q;
49284c7070dbSScott Long 
49294c7070dbSScott Long 	info = &ctx->ifc_filter_info;
4930add6f7d0SSean Bruno 	tqrid = rid;
49314c7070dbSScott Long 
49324c7070dbSScott Long 	switch (type) {
49334c7070dbSScott Long 	/* XXX merge tx/rx for netmap? */
49344c7070dbSScott Long 	case IFLIB_INTR_TX:
49354c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
49364c7070dbSScott Long 		info = &ctx->ifc_txqs[qid].ift_filter_info;
49374c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
4938ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
49394c7070dbSScott Long 		fn = _task_fn_tx;
494095246abbSSean Bruno 		intr_fast = iflib_fast_intr;
4941da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
49424c7070dbSScott Long 		break;
49434c7070dbSScott Long 	case IFLIB_INTR_RX:
49444c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
49454c7070dbSScott Long 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
49464c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
4947ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
49484c7070dbSScott Long 		fn = _task_fn_rx;
4949ab2e3f79SStephen Hurd 		intr_fast = iflib_fast_intr;
495095246abbSSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
495195246abbSSean Bruno 		break;
495295246abbSSean Bruno 	case IFLIB_INTR_RXTX:
495395246abbSSean Bruno 		q = &ctx->ifc_rxqs[qid];
495495246abbSSean Bruno 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
495595246abbSSean Bruno 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
4956ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
495795246abbSSean Bruno 		fn = _task_fn_rx;
495895246abbSSean Bruno 		intr_fast = iflib_fast_intr_rxtx;
4959da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
49604c7070dbSScott Long 		break;
49614c7070dbSScott Long 	case IFLIB_INTR_ADMIN:
49624c7070dbSScott Long 		q = ctx;
4963da69b8f9SSean Bruno 		tqrid = -1;
49644c7070dbSScott Long 		info = &ctx->ifc_filter_info;
49654c7070dbSScott Long 		gtask = &ctx->ifc_admin_task;
4966ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
49674c7070dbSScott Long 		fn = _task_fn_admin;
496895246abbSSean Bruno 		intr_fast = iflib_fast_intr_ctx;
49694c7070dbSScott Long 		break;
49704c7070dbSScott Long 	default:
49714c7070dbSScott Long 		panic("unknown net intr type");
49724c7070dbSScott Long 	}
49734c7070dbSScott Long 
49744c7070dbSScott Long 	info->ifi_filter = filter;
49754c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
49764c7070dbSScott Long 	info->ifi_task = gtask;
497795246abbSSean Bruno 	info->ifi_ctx = q;
49784c7070dbSScott Long 
497995246abbSSean Bruno 	err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info,  name);
4980da69b8f9SSean Bruno 	if (err != 0) {
4981da69b8f9SSean Bruno 		device_printf(ctx->ifc_dev, "_iflib_irq_alloc failed %d\n", err);
49824c7070dbSScott Long 		return (err);
4983da69b8f9SSean Bruno 	}
4984da69b8f9SSean Bruno 	if (type == IFLIB_INTR_ADMIN)
4985da69b8f9SSean Bruno 		return (0);
4986da69b8f9SSean Bruno 
49874c7070dbSScott Long 	if (tqrid != -1) {
4988ab2e3f79SStephen Hurd 		cpuid = find_nth(ctx, &cpus, qid);
4989ab2e3f79SStephen Hurd 		taskqgroup_attach_cpu(tqg, gtask, q, cpuid, irq->ii_rid, name);
4990aa3c5dd8SSean Bruno 	} else {
49914c7070dbSScott Long 		taskqgroup_attach(tqg, gtask, q, tqrid, name);
4992aa3c5dd8SSean Bruno 	}
49934c7070dbSScott Long 
49944c7070dbSScott Long 	return (0);
49954c7070dbSScott Long }
49964c7070dbSScott Long 
49974c7070dbSScott Long void
49984c7070dbSScott Long iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name)
49994c7070dbSScott Long {
50004c7070dbSScott Long 	struct grouptask *gtask;
50014c7070dbSScott Long 	struct taskqgroup *tqg;
500223ac9029SStephen Hurd 	gtask_fn_t *fn;
50034c7070dbSScott Long 	void *q;
50044c7070dbSScott Long 
50054c7070dbSScott Long 	switch (type) {
50064c7070dbSScott Long 	case IFLIB_INTR_TX:
50074c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
50084c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
5009ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
50104c7070dbSScott Long 		fn = _task_fn_tx;
50114c7070dbSScott Long 		break;
50124c7070dbSScott Long 	case IFLIB_INTR_RX:
50134c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
50144c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5015ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
50164c7070dbSScott Long 		fn = _task_fn_rx;
50174c7070dbSScott Long 		break;
50184c7070dbSScott Long 	case IFLIB_INTR_IOV:
50194c7070dbSScott Long 		q = ctx;
50204c7070dbSScott Long 		gtask = &ctx->ifc_vflr_task;
5021ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
50224c7070dbSScott Long 		rid = -1;
50234c7070dbSScott Long 		fn = _task_fn_iov;
50244c7070dbSScott Long 		break;
50254c7070dbSScott Long 	default:
50264c7070dbSScott Long 		panic("unknown net intr type");
50274c7070dbSScott Long 	}
50284c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
50294c7070dbSScott Long 	taskqgroup_attach(tqg, gtask, q, rid, name);
50304c7070dbSScott Long }
50314c7070dbSScott Long 
50324c7070dbSScott Long void
50334c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
50344c7070dbSScott Long {
50354c7070dbSScott Long 	if (irq->ii_tag)
50364c7070dbSScott Long 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
50374c7070dbSScott Long 
50384c7070dbSScott Long 	if (irq->ii_res)
50394c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, irq->ii_rid, irq->ii_res);
50404c7070dbSScott Long }
50414c7070dbSScott Long 
50424c7070dbSScott Long static int
50434c7070dbSScott Long iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, char *name)
50444c7070dbSScott Long {
50454c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
50464c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
50474c7070dbSScott Long 	if_irq_t irq = &ctx->ifc_legacy_irq;
50484c7070dbSScott Long 	iflib_filter_info_t info;
50494c7070dbSScott Long 	struct grouptask *gtask;
50504c7070dbSScott Long 	struct taskqgroup *tqg;
505123ac9029SStephen Hurd 	gtask_fn_t *fn;
50524c7070dbSScott Long 	int tqrid;
50534c7070dbSScott Long 	void *q;
50544c7070dbSScott Long 	int err;
50554c7070dbSScott Long 
50564c7070dbSScott Long 	q = &ctx->ifc_rxqs[0];
50574c7070dbSScott Long 	info = &rxq[0].ifr_filter_info;
50584c7070dbSScott Long 	gtask = &rxq[0].ifr_task;
5059ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
50604c7070dbSScott Long 	tqrid = irq->ii_rid = *rid;
50614c7070dbSScott Long 	fn = _task_fn_rx;
50624c7070dbSScott Long 
50634c7070dbSScott Long 	ctx->ifc_flags |= IFC_LEGACY;
50644c7070dbSScott Long 	info->ifi_filter = filter;
50654c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
50664c7070dbSScott Long 	info->ifi_task = gtask;
50674ecb427aSSean Bruno 	info->ifi_ctx = ctx;
50684c7070dbSScott Long 
50694c7070dbSScott Long 	/* We allocate a single interrupt resource */
507095246abbSSean Bruno 	if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr_ctx, NULL, info, name)) != 0)
50714c7070dbSScott Long 		return (err);
50724c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
50734c7070dbSScott Long 	taskqgroup_attach(tqg, gtask, q, tqrid, name);
50744c7070dbSScott Long 
50754c7070dbSScott Long 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
5076ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, tqrid, "tx");
50774c7070dbSScott Long 	return (0);
50784c7070dbSScott Long }
50794c7070dbSScott Long 
50804c7070dbSScott Long void
50814c7070dbSScott Long iflib_led_create(if_ctx_t ctx)
50824c7070dbSScott Long {
50834c7070dbSScott Long 
50844c7070dbSScott Long 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
50854c7070dbSScott Long 	    device_get_nameunit(ctx->ifc_dev));
50864c7070dbSScott Long }
50874c7070dbSScott Long 
50884c7070dbSScott Long void
50894c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
50904c7070dbSScott Long {
50914c7070dbSScott Long 
50924c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
50934c7070dbSScott Long }
50944c7070dbSScott Long 
50954c7070dbSScott Long void
50964c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
50974c7070dbSScott Long {
50984c7070dbSScott Long 
50994c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
51004c7070dbSScott Long }
51014c7070dbSScott Long 
51024c7070dbSScott Long void
51034c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx)
51044c7070dbSScott Long {
51051248952aSSean Bruno #ifdef INVARIANTS
51061248952aSSean Bruno 	struct grouptask *gtask;
51071248952aSSean Bruno 
51081248952aSSean Bruno 	gtask = &ctx->ifc_admin_task;
5109*d0d0ad0aSStephen Hurd 	MPASS(gtask != NULL && gtask->gt_taskqueue != NULL);
51101248952aSSean Bruno #endif
51114c7070dbSScott Long 
51124c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
51134c7070dbSScott Long }
51144c7070dbSScott Long 
51154c7070dbSScott Long void
51164c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx)
51174c7070dbSScott Long {
51184c7070dbSScott Long 
51194c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
51204c7070dbSScott Long }
51214c7070dbSScott Long 
51224c7070dbSScott Long void
51234c7070dbSScott Long iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name)
51244c7070dbSScott Long {
51254c7070dbSScott Long 
5126ab2e3f79SStephen Hurd 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, -1, name);
51274c7070dbSScott Long }
51284c7070dbSScott Long 
51294c7070dbSScott Long void
513023ac9029SStephen Hurd iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask, gtask_fn_t *fn,
51314c7070dbSScott Long 	char *name)
51324c7070dbSScott Long {
51334c7070dbSScott Long 
51344c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, ctx);
5135ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, -1, name);
51364c7070dbSScott Long }
51374c7070dbSScott Long 
51384c7070dbSScott Long void
513923ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask)
514023ac9029SStephen Hurd {
514123ac9029SStephen Hurd 
5142ab2e3f79SStephen Hurd 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
514323ac9029SStephen Hurd }
514423ac9029SStephen Hurd 
514523ac9029SStephen Hurd void
514623ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
51474c7070dbSScott Long {
51484c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
51494c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
51504c7070dbSScott Long 
51514c7070dbSScott Long 	if_setbaudrate(ifp, baudrate);
515295246abbSSean Bruno 	if (baudrate >= IF_Gbps(10))
515395246abbSSean Bruno 		ctx->ifc_flags |= IFC_PREFETCH;
515423ac9029SStephen Hurd 
51554c7070dbSScott Long 	/* If link down, disable watchdog */
51564c7070dbSScott Long 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
51574c7070dbSScott Long 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
51584c7070dbSScott Long 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
51594c7070dbSScott Long 	}
51604c7070dbSScott Long 	ctx->ifc_link_state = link_state;
51614c7070dbSScott Long 	if_link_state_change(ifp, link_state);
51624c7070dbSScott Long }
51634c7070dbSScott Long 
51644c7070dbSScott Long static int
51654c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
51664c7070dbSScott Long {
51674c7070dbSScott Long 	int credits;
51681248952aSSean Bruno #ifdef INVARIANTS
51691248952aSSean Bruno 	int credits_pre = txq->ift_cidx_processed;
51701248952aSSean Bruno #endif
51714c7070dbSScott Long 
51724c7070dbSScott Long 	if (ctx->isc_txd_credits_update == NULL)
51734c7070dbSScott Long 		return (0);
51744c7070dbSScott Long 
517595246abbSSean Bruno 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
51764c7070dbSScott Long 		return (0);
51774c7070dbSScott Long 
51784c7070dbSScott Long 	txq->ift_processed += credits;
51794c7070dbSScott Long 	txq->ift_cidx_processed += credits;
51804c7070dbSScott Long 
51811248952aSSean Bruno 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
51824c7070dbSScott Long 	if (txq->ift_cidx_processed >= txq->ift_size)
51834c7070dbSScott Long 		txq->ift_cidx_processed -= txq->ift_size;
51844c7070dbSScott Long 	return (credits);
51854c7070dbSScott Long }
51864c7070dbSScott Long 
51874c7070dbSScott Long static int
518895246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
51894c7070dbSScott Long {
51904c7070dbSScott Long 
519123ac9029SStephen Hurd 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
519223ac9029SStephen Hurd 	    budget));
51934c7070dbSScott Long }
51944c7070dbSScott Long 
51954c7070dbSScott Long void
51964c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
51974c7070dbSScott Long 	const char *description, if_int_delay_info_t info,
51984c7070dbSScott Long 	int offset, int value)
51994c7070dbSScott Long {
52004c7070dbSScott Long 	info->iidi_ctx = ctx;
52014c7070dbSScott Long 	info->iidi_offset = offset;
52024c7070dbSScott Long 	info->iidi_value = value;
52034c7070dbSScott Long 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
52044c7070dbSScott Long 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
52054c7070dbSScott Long 	    OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
52064c7070dbSScott Long 	    info, 0, iflib_sysctl_int_delay, "I", description);
52074c7070dbSScott Long }
52084c7070dbSScott Long 
5209ab2e3f79SStephen Hurd struct mtx *
52104c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx)
52114c7070dbSScott Long {
52124c7070dbSScott Long 
5213ab2e3f79SStephen Hurd 	return (&ctx->ifc_mtx);
52144c7070dbSScott Long }
52154c7070dbSScott Long 
52164c7070dbSScott Long static int
52174c7070dbSScott Long iflib_msix_init(if_ctx_t ctx)
52184c7070dbSScott Long {
52194c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
52204c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
52214c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
52224c7070dbSScott Long 	int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs;
52234c7070dbSScott Long 	int iflib_num_tx_queues, iflib_num_rx_queues;
52244c7070dbSScott Long 	int err, admincnt, bar;
52254c7070dbSScott Long 
522623ac9029SStephen Hurd 	iflib_num_tx_queues = scctx->isc_ntxqsets;
522723ac9029SStephen Hurd 	iflib_num_rx_queues = scctx->isc_nrxqsets;
522823ac9029SStephen Hurd 
52291248952aSSean Bruno 	device_printf(dev, "msix_init qsets capped at %d\n", iflib_num_tx_queues);
52301248952aSSean Bruno 
52314c7070dbSScott Long 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
52324c7070dbSScott Long 	admincnt = sctx->isc_admin_intrcnt;
52334c7070dbSScott Long 	/* Override by tuneable */
5234ea351d3fSSean Bruno 	if (scctx->isc_disable_msix)
52354c7070dbSScott Long 		goto msi;
52364c7070dbSScott Long 
52374c7070dbSScott Long 	/*
52384c7070dbSScott Long 	** When used in a virtualized environment
52394c7070dbSScott Long 	** PCI BUSMASTER capability may not be set
52404c7070dbSScott Long 	** so explicity set it here and rewrite
52414c7070dbSScott Long 	** the ENABLE in the MSIX control register
52424c7070dbSScott Long 	** at this point to cause the host to
52434c7070dbSScott Long 	** successfully initialize us.
52444c7070dbSScott Long 	*/
52454c7070dbSScott Long 	{
52464c7070dbSScott Long 		int msix_ctrl, rid;
52474c7070dbSScott Long 
524896eeabefSSean Bruno  		pci_enable_busmaster(dev);
5249f7ae9a84SSean Bruno 		rid = 0;
5250f7ae9a84SSean Bruno 		if (pci_find_cap(dev, PCIY_MSIX, &rid) == 0 && rid != 0) {
52514c7070dbSScott Long 			rid += PCIR_MSIX_CTRL;
52524c7070dbSScott Long 			msix_ctrl = pci_read_config(dev, rid, 2);
52534c7070dbSScott Long 			msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
52544c7070dbSScott Long 			pci_write_config(dev, rid, msix_ctrl, 2);
5255f7ae9a84SSean Bruno 		} else {
5256f7ae9a84SSean Bruno 			device_printf(dev, "PCIY_MSIX capability not found; "
5257f7ae9a84SSean Bruno 			                   "or rid %d == 0.\n", rid);
5258f7ae9a84SSean Bruno 			goto msi;
5259f7ae9a84SSean Bruno 		}
52604c7070dbSScott Long 	}
52614c7070dbSScott Long 
52624c7070dbSScott Long 	/*
52634c7070dbSScott Long 	 * bar == -1 => "trust me I know what I'm doing"
52644c7070dbSScott Long 	 * Some drivers are for hardware that is so shoddily
52654c7070dbSScott Long 	 * documented that no one knows which bars are which
52664c7070dbSScott Long 	 * so the developer has to map all bars. This hack
52674c7070dbSScott Long 	 * allows shoddy garbage to use msix in this framework.
52684c7070dbSScott Long 	 */
52694c7070dbSScott Long 	if (bar != -1) {
52704c7070dbSScott Long 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
52714c7070dbSScott Long 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
52724c7070dbSScott Long 		if (ctx->ifc_msix_mem == NULL) {
52734c7070dbSScott Long 			/* May not be enabled */
52744c7070dbSScott Long 			device_printf(dev, "Unable to map MSIX table \n");
52754c7070dbSScott Long 			goto msi;
52764c7070dbSScott Long 		}
52774c7070dbSScott Long 	}
52784c7070dbSScott Long 	/* First try MSI/X */
52794c7070dbSScott Long 	if ((msgs = pci_msix_count(dev)) == 0) { /* system has msix disabled */
52804c7070dbSScott Long 		device_printf(dev, "System has MSIX disabled \n");
52814c7070dbSScott Long 		bus_release_resource(dev, SYS_RES_MEMORY,
52824c7070dbSScott Long 		    bar, ctx->ifc_msix_mem);
52834c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
52844c7070dbSScott Long 		goto msi;
52854c7070dbSScott Long 	}
52864c7070dbSScott Long #if IFLIB_DEBUG
52874c7070dbSScott Long 	/* use only 1 qset in debug mode */
52884c7070dbSScott Long 	queuemsgs = min(msgs - admincnt, 1);
52894c7070dbSScott Long #else
52904c7070dbSScott Long 	queuemsgs = msgs - admincnt;
52914c7070dbSScott Long #endif
52924c7070dbSScott Long 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) == 0) {
52934c7070dbSScott Long #ifdef RSS
52944c7070dbSScott Long 		queues = imin(queuemsgs, rss_getnumbuckets());
52954c7070dbSScott Long #else
52964c7070dbSScott Long 		queues = queuemsgs;
52974c7070dbSScott Long #endif
52984c7070dbSScott Long 		queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
52994c7070dbSScott Long 		device_printf(dev, "pxm cpus: %d queue msgs: %d admincnt: %d\n",
53004c7070dbSScott Long 					  CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
53014c7070dbSScott Long 	} else {
53024c7070dbSScott Long 		device_printf(dev, "Unable to fetch CPU list\n");
53034c7070dbSScott Long 		/* Figure out a reasonable auto config value */
53044c7070dbSScott Long 		queues = min(queuemsgs, mp_ncpus);
53054c7070dbSScott Long 	}
53064c7070dbSScott Long #ifdef  RSS
53074c7070dbSScott Long 	/* If we're doing RSS, clamp at the number of RSS buckets */
53084c7070dbSScott Long 	if (queues > rss_getnumbuckets())
53094c7070dbSScott Long 		queues = rss_getnumbuckets();
53104c7070dbSScott Long #endif
531123ac9029SStephen Hurd 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
531223ac9029SStephen Hurd 		rx_queues = iflib_num_rx_queues;
53134c7070dbSScott Long 	else
53144c7070dbSScott Long 		rx_queues = queues;
531523ac9029SStephen Hurd 	/*
531623ac9029SStephen Hurd 	 * We want this to be all logical CPUs by default
531723ac9029SStephen Hurd 	 */
53184c7070dbSScott Long 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
53194c7070dbSScott Long 		tx_queues = iflib_num_tx_queues;
53204c7070dbSScott Long 	else
532123ac9029SStephen Hurd 		tx_queues = mp_ncpus;
532223ac9029SStephen Hurd 
532323ac9029SStephen Hurd 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
532423ac9029SStephen Hurd #ifdef INVARIANTS
532523ac9029SStephen Hurd 		if (tx_queues != rx_queues)
532623ac9029SStephen Hurd 			device_printf(dev, "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
532723ac9029SStephen Hurd 				      min(rx_queues, tx_queues), min(rx_queues, tx_queues));
532823ac9029SStephen Hurd #endif
532923ac9029SStephen Hurd 		tx_queues = min(rx_queues, tx_queues);
533023ac9029SStephen Hurd 		rx_queues = min(rx_queues, tx_queues);
533123ac9029SStephen Hurd 	}
53324c7070dbSScott Long 
5333ab2e3f79SStephen Hurd 	device_printf(dev, "using %d rx queues %d tx queues \n", rx_queues, tx_queues);
53344c7070dbSScott Long 
5335ab2e3f79SStephen Hurd 	vectors = rx_queues + admincnt;
53364c7070dbSScott Long 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
53374c7070dbSScott Long 		device_printf(dev,
53384c7070dbSScott Long 					  "Using MSIX interrupts with %d vectors\n", vectors);
53394c7070dbSScott Long 		scctx->isc_vectors = vectors;
53404c7070dbSScott Long 		scctx->isc_nrxqsets = rx_queues;
53414c7070dbSScott Long 		scctx->isc_ntxqsets = tx_queues;
53424c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSIX;
534323ac9029SStephen Hurd 
53444c7070dbSScott Long 		return (vectors);
53454c7070dbSScott Long 	} else {
53464c7070dbSScott Long 		device_printf(dev, "failed to allocate %d msix vectors, err: %d - using MSI\n", vectors, err);
53474c7070dbSScott Long 	}
53484c7070dbSScott Long msi:
53494c7070dbSScott Long 	vectors = pci_msi_count(dev);
53504c7070dbSScott Long 	scctx->isc_nrxqsets = 1;
53514c7070dbSScott Long 	scctx->isc_ntxqsets = 1;
53524c7070dbSScott Long 	scctx->isc_vectors = vectors;
53534c7070dbSScott Long 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
53544c7070dbSScott Long 		device_printf(dev,"Using an MSI interrupt\n");
53554c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSI;
53564c7070dbSScott Long 	} else {
53574c7070dbSScott Long 		device_printf(dev,"Using a Legacy interrupt\n");
53584c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
53594c7070dbSScott Long 	}
53604c7070dbSScott Long 
53614c7070dbSScott Long 	return (vectors);
53624c7070dbSScott Long }
53634c7070dbSScott Long 
53644c7070dbSScott Long char * ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
53654c7070dbSScott Long 
53664c7070dbSScott Long static int
53674c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
53684c7070dbSScott Long {
53694c7070dbSScott Long 	int rc;
53704c7070dbSScott Long 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
53714c7070dbSScott Long 	struct sbuf *sb;
53724c7070dbSScott Long 	char *ring_state = "UNKNOWN";
53734c7070dbSScott Long 
53744c7070dbSScott Long 	/* XXX needed ? */
53754c7070dbSScott Long 	rc = sysctl_wire_old_buffer(req, 0);
53764c7070dbSScott Long 	MPASS(rc == 0);
53774c7070dbSScott Long 	if (rc != 0)
53784c7070dbSScott Long 		return (rc);
53794c7070dbSScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
53804c7070dbSScott Long 	MPASS(sb != NULL);
53814c7070dbSScott Long 	if (sb == NULL)
53824c7070dbSScott Long 		return (ENOMEM);
53834c7070dbSScott Long 	if (state[3] <= 3)
53844c7070dbSScott Long 		ring_state = ring_states[state[3]];
53854c7070dbSScott Long 
53864c7070dbSScott Long 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
53874c7070dbSScott Long 		    state[0], state[1], state[2], ring_state);
53884c7070dbSScott Long 	rc = sbuf_finish(sb);
53894c7070dbSScott Long 	sbuf_delete(sb);
53904c7070dbSScott Long         return(rc);
53914c7070dbSScott Long }
53924c7070dbSScott Long 
539323ac9029SStephen Hurd enum iflib_ndesc_handler {
539423ac9029SStephen Hurd 	IFLIB_NTXD_HANDLER,
539523ac9029SStephen Hurd 	IFLIB_NRXD_HANDLER,
539623ac9029SStephen Hurd };
53974c7070dbSScott Long 
539823ac9029SStephen Hurd static int
539923ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
540023ac9029SStephen Hurd {
540123ac9029SStephen Hurd 	if_ctx_t ctx = (void *)arg1;
540223ac9029SStephen Hurd 	enum iflib_ndesc_handler type = arg2;
540323ac9029SStephen Hurd 	char buf[256] = {0};
540495246abbSSean Bruno 	qidx_t *ndesc;
540523ac9029SStephen Hurd 	char *p, *next;
540623ac9029SStephen Hurd 	int nqs, rc, i;
540723ac9029SStephen Hurd 
540823ac9029SStephen Hurd 	MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER);
540923ac9029SStephen Hurd 
541023ac9029SStephen Hurd 	nqs = 8;
541123ac9029SStephen Hurd 	switch(type) {
541223ac9029SStephen Hurd 	case IFLIB_NTXD_HANDLER:
541323ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_ntxds;
541423ac9029SStephen Hurd 		if (ctx->ifc_sctx)
541523ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_ntxqs;
541623ac9029SStephen Hurd 		break;
541723ac9029SStephen Hurd 	case IFLIB_NRXD_HANDLER:
541823ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_nrxds;
541923ac9029SStephen Hurd 		if (ctx->ifc_sctx)
542023ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_nrxqs;
542123ac9029SStephen Hurd 		break;
542223ac9029SStephen Hurd 	}
542323ac9029SStephen Hurd 	if (nqs == 0)
542423ac9029SStephen Hurd 		nqs = 8;
542523ac9029SStephen Hurd 
542623ac9029SStephen Hurd 	for (i=0; i<8; i++) {
542723ac9029SStephen Hurd 		if (i >= nqs)
542823ac9029SStephen Hurd 			break;
542923ac9029SStephen Hurd 		if (i)
543023ac9029SStephen Hurd 			strcat(buf, ",");
543123ac9029SStephen Hurd 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
543223ac9029SStephen Hurd 	}
543323ac9029SStephen Hurd 
543423ac9029SStephen Hurd 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
543523ac9029SStephen Hurd 	if (rc || req->newptr == NULL)
543623ac9029SStephen Hurd 		return rc;
543723ac9029SStephen Hurd 
543823ac9029SStephen Hurd 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
543923ac9029SStephen Hurd 	    i++, p = strsep(&next, " ,")) {
544023ac9029SStephen Hurd 		ndesc[i] = strtoul(p, NULL, 10);
544123ac9029SStephen Hurd 	}
544223ac9029SStephen Hurd 
544323ac9029SStephen Hurd 	return(rc);
544423ac9029SStephen Hurd }
54454c7070dbSScott Long 
54464c7070dbSScott Long #define NAME_BUFLEN 32
54474c7070dbSScott Long static void
54484c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx)
54494c7070dbSScott Long {
54504c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
54514c7070dbSScott Long 	struct sysctl_oid_list *child, *oid_list;
54524c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
54534c7070dbSScott Long 	struct sysctl_oid *node;
54544c7070dbSScott Long 
54554c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
54564c7070dbSScott Long 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
54574c7070dbSScott Long 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
54584c7070dbSScott Long 						      CTLFLAG_RD, NULL, "IFLIB fields");
54594c7070dbSScott Long 	oid_list = SYSCTL_CHILDREN(node);
54604c7070dbSScott Long 
546123ac9029SStephen Hurd 	SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
546223ac9029SStephen Hurd 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
546323ac9029SStephen Hurd 		       "driver version");
546423ac9029SStephen Hurd 
54654c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
54664c7070dbSScott Long 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
54674c7070dbSScott Long 			"# of txqs to use, 0 => use default #");
54684c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
546923ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
547023ac9029SStephen Hurd 			"# of rxqs to use, 0 => use default #");
547123ac9029SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
547223ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
547323ac9029SStephen Hurd                        "permit #txq != #rxq");
5474ea351d3fSSean Bruno        SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix",
5475ea351d3fSSean Bruno                       CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
5476ea351d3fSSean Bruno                       "disable MSIX (default 0)");
54774c7070dbSScott Long 
547823ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
547923ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
548023ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER,
548123ac9029SStephen Hurd                        mp_ndesc_handler, "A",
548223ac9029SStephen Hurd                        "list of # of tx descriptors to use, 0 = use default #");
548323ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
548423ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER,
548523ac9029SStephen Hurd                        mp_ndesc_handler, "A",
548623ac9029SStephen Hurd                        "list of # of rx descriptors to use, 0 = use default #");
54874c7070dbSScott Long }
54884c7070dbSScott Long 
54894c7070dbSScott Long static void
54904c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx)
54914c7070dbSScott Long {
54924c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
54934c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
54944c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
54954c7070dbSScott Long 	struct sysctl_oid_list *child;
54964c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
54974c7070dbSScott Long 	iflib_fl_t fl;
54984c7070dbSScott Long 	iflib_txq_t txq;
54994c7070dbSScott Long 	iflib_rxq_t rxq;
55004c7070dbSScott Long 	int i, j;
55014c7070dbSScott Long 	char namebuf[NAME_BUFLEN];
55024c7070dbSScott Long 	char *qfmt;
55034c7070dbSScott Long 	struct sysctl_oid *queue_node, *fl_node, *node;
55044c7070dbSScott Long 	struct sysctl_oid_list *queue_list, *fl_list;
55054c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
55064c7070dbSScott Long 
55074c7070dbSScott Long 	node = ctx->ifc_sysctl_node;
55084c7070dbSScott Long 	child = SYSCTL_CHILDREN(node);
55094c7070dbSScott Long 
55104c7070dbSScott Long 	if (scctx->isc_ntxqsets > 100)
55114c7070dbSScott Long 		qfmt = "txq%03d";
55124c7070dbSScott Long 	else if (scctx->isc_ntxqsets > 10)
55134c7070dbSScott Long 		qfmt = "txq%02d";
55144c7070dbSScott Long 	else
55154c7070dbSScott Long 		qfmt = "txq%d";
55164c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
55174c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
55184c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
55194c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
55204c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
55214c7070dbSScott Long #if MEMORY_LOGGING
55224c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
55234c7070dbSScott Long 				CTLFLAG_RD,
55244c7070dbSScott Long 				&txq->ift_dequeued, "total mbufs freed");
55254c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
55264c7070dbSScott Long 				CTLFLAG_RD,
55274c7070dbSScott Long 				&txq->ift_enqueued, "total mbufs enqueued");
55284c7070dbSScott Long #endif
55294c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
55304c7070dbSScott Long 				   CTLFLAG_RD,
55314c7070dbSScott Long 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
55324c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
55334c7070dbSScott Long 				   CTLFLAG_RD,
55344c7070dbSScott Long 				   &txq->ift_pullups, "# of times m_pullup was called");
55354c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
55364c7070dbSScott Long 				   CTLFLAG_RD,
55374c7070dbSScott Long 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
55384c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
55394c7070dbSScott Long 				   CTLFLAG_RD,
554023ac9029SStephen Hurd 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
55414c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
55424c7070dbSScott Long 				   CTLFLAG_RD,
55434c7070dbSScott Long 				   &txq->ift_map_failed, "# of times dma map failed");
55444c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
55454c7070dbSScott Long 				   CTLFLAG_RD,
55464c7070dbSScott Long 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
55474c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
55484c7070dbSScott Long 				   CTLFLAG_RD,
55494c7070dbSScott Long 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
55504c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
55514c7070dbSScott Long 				   CTLFLAG_RD,
55524c7070dbSScott Long 				   &txq->ift_pidx, 1, "Producer Index");
55534c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
55544c7070dbSScott Long 				   CTLFLAG_RD,
55554c7070dbSScott Long 				   &txq->ift_cidx, 1, "Consumer Index");
55564c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
55574c7070dbSScott Long 				   CTLFLAG_RD,
55584c7070dbSScott Long 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
55594c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
55604c7070dbSScott Long 				   CTLFLAG_RD,
55614c7070dbSScott Long 				   &txq->ift_in_use, 1, "descriptors in use");
55624c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
55634c7070dbSScott Long 				   CTLFLAG_RD,
55644c7070dbSScott Long 				   &txq->ift_processed, "descriptors procesed for clean");
55654c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
55664c7070dbSScott Long 				   CTLFLAG_RD,
55674c7070dbSScott Long 				   &txq->ift_cleaned, "total cleaned");
55684c7070dbSScott Long 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
556995246abbSSean Bruno 				CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br->state),
55704c7070dbSScott Long 				0, mp_ring_state_handler, "A", "soft ring state");
55714c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
557295246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->enqueues,
55734c7070dbSScott Long 				       "# of enqueues to the mp_ring for this queue");
55744c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
557595246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->drops,
55764c7070dbSScott Long 				       "# of drops in the mp_ring for this queue");
55774c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
557895246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->starts,
55794c7070dbSScott Long 				       "# of normal consumer starts in the mp_ring for this queue");
55804c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
558195246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->stalls,
55824c7070dbSScott Long 					       "# of consumer stalls in the mp_ring for this queue");
55834c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
558495246abbSSean Bruno 			       CTLFLAG_RD, &txq->ift_br->restarts,
55854c7070dbSScott Long 				       "# of consumer restarts in the mp_ring for this queue");
55864c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
558795246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->abdications,
55884c7070dbSScott Long 				       "# of consumer abdications in the mp_ring for this queue");
55894c7070dbSScott Long 	}
55904c7070dbSScott Long 
55914c7070dbSScott Long 	if (scctx->isc_nrxqsets > 100)
55924c7070dbSScott Long 		qfmt = "rxq%03d";
55934c7070dbSScott Long 	else if (scctx->isc_nrxqsets > 10)
55944c7070dbSScott Long 		qfmt = "rxq%02d";
55954c7070dbSScott Long 	else
55964c7070dbSScott Long 		qfmt = "rxq%d";
55974c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
55984c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
55994c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
56004c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
56014c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
560223ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
56034c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx",
56044c7070dbSScott Long 				       CTLFLAG_RD,
56054c7070dbSScott Long 				       &rxq->ifr_cq_pidx, 1, "Producer Index");
56064c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
56074c7070dbSScott Long 				       CTLFLAG_RD,
56084c7070dbSScott Long 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
56094c7070dbSScott Long 		}
5610da69b8f9SSean Bruno 
56114c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
56124c7070dbSScott Long 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
56134c7070dbSScott Long 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
56144c7070dbSScott Long 						     CTLFLAG_RD, NULL, "freelist Name");
56154c7070dbSScott Long 			fl_list = SYSCTL_CHILDREN(fl_node);
56164c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
56174c7070dbSScott Long 				       CTLFLAG_RD,
56184c7070dbSScott Long 				       &fl->ifl_pidx, 1, "Producer Index");
56194c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
56204c7070dbSScott Long 				       CTLFLAG_RD,
56214c7070dbSScott Long 				       &fl->ifl_cidx, 1, "Consumer Index");
56224c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
56234c7070dbSScott Long 				       CTLFLAG_RD,
56244c7070dbSScott Long 				       &fl->ifl_credits, 1, "credits available");
56254c7070dbSScott Long #if MEMORY_LOGGING
56264c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
56274c7070dbSScott Long 					CTLFLAG_RD,
56284c7070dbSScott Long 					&fl->ifl_m_enqueued, "mbufs allocated");
56294c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
56304c7070dbSScott Long 					CTLFLAG_RD,
56314c7070dbSScott Long 					&fl->ifl_m_dequeued, "mbufs freed");
56324c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
56334c7070dbSScott Long 					CTLFLAG_RD,
56344c7070dbSScott Long 					&fl->ifl_cl_enqueued, "clusters allocated");
56354c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
56364c7070dbSScott Long 					CTLFLAG_RD,
56374c7070dbSScott Long 					&fl->ifl_cl_dequeued, "clusters freed");
56384c7070dbSScott Long #endif
56394c7070dbSScott Long 
56404c7070dbSScott Long 		}
56414c7070dbSScott Long 	}
56424c7070dbSScott Long 
56434c7070dbSScott Long }
564495246abbSSean Bruno 
564595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
564695246abbSSean Bruno static struct mbuf *
564795246abbSSean Bruno iflib_fixup_rx(struct mbuf *m)
564895246abbSSean Bruno {
564995246abbSSean Bruno 	struct mbuf *n;
565095246abbSSean Bruno 
565195246abbSSean Bruno 	if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
565295246abbSSean Bruno 		bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
565395246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
565495246abbSSean Bruno 		n = m;
565595246abbSSean Bruno 	} else {
565695246abbSSean Bruno 		MGETHDR(n, M_NOWAIT, MT_DATA);
565795246abbSSean Bruno 		if (n == NULL) {
565895246abbSSean Bruno 			m_freem(m);
565995246abbSSean Bruno 			return (NULL);
566095246abbSSean Bruno 		}
566195246abbSSean Bruno 		bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
566295246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
566395246abbSSean Bruno 		m->m_len -= ETHER_HDR_LEN;
566495246abbSSean Bruno 		n->m_len = ETHER_HDR_LEN;
566595246abbSSean Bruno 		M_MOVE_PKTHDR(n, m);
566695246abbSSean Bruno 		n->m_next = m;
566795246abbSSean Bruno 	}
566895246abbSSean Bruno 	return (n);
566995246abbSSean Bruno }
567095246abbSSean Bruno #endif
5671