xref: /freebsd/sys/net/iflib.c (revision 835809f99fb625567e0f5e06d1465e61e20cb3f3)
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 
544c7070dbSScott Long 
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 
964c7070dbSScott Long /*
974c7070dbSScott Long  * enable accounting of every mbuf as it comes in to and goes out of iflib's software descriptor references
984c7070dbSScott Long  */
994c7070dbSScott Long #define MEMORY_LOGGING 0
1004c7070dbSScott Long /*
1014c7070dbSScott Long  * Enable mbuf vectors for compressing long mbuf chains
1024c7070dbSScott Long  */
1034c7070dbSScott Long 
1044c7070dbSScott Long /*
1054c7070dbSScott Long  * NB:
1064c7070dbSScott Long  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
1074c7070dbSScott Long  *   we prefetch needs to be determined by the time spent in m_free vis a vis
1084c7070dbSScott Long  *   the cost of a prefetch. This will of course vary based on the workload:
1094c7070dbSScott Long  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
1104c7070dbSScott Long  *        is quite expensive, thus suggesting very little prefetch.
1114c7070dbSScott Long  *      - small packet forwarding which is just returning a single mbuf to
1124c7070dbSScott Long  *        UMA will typically be very fast vis a vis the cost of a memory
1134c7070dbSScott Long  *        access.
1144c7070dbSScott Long  */
1154c7070dbSScott Long 
1164c7070dbSScott Long 
1174c7070dbSScott Long /*
1184c7070dbSScott Long  * File organization:
1194c7070dbSScott Long  *  - private structures
1204c7070dbSScott Long  *  - iflib private utility functions
1214c7070dbSScott Long  *  - ifnet functions
1224c7070dbSScott Long  *  - vlan registry and other exported functions
1234c7070dbSScott Long  *  - iflib public core functions
1244c7070dbSScott Long  *
1254c7070dbSScott Long  *
1264c7070dbSScott Long  */
1274c7070dbSScott Long static MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
1284c7070dbSScott Long 
1294c7070dbSScott Long struct iflib_txq;
1304c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t;
1314c7070dbSScott Long struct iflib_rxq;
1324c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t;
1334c7070dbSScott Long struct iflib_fl;
1344c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t;
1354c7070dbSScott Long 
1364ecb427aSSean Bruno struct iflib_ctx;
1374ecb427aSSean Bruno 
1384c7070dbSScott Long typedef struct iflib_filter_info {
1394c7070dbSScott Long 	driver_filter_t *ifi_filter;
1404c7070dbSScott Long 	void *ifi_filter_arg;
1414c7070dbSScott Long 	struct grouptask *ifi_task;
1424ecb427aSSean Bruno 	struct iflib_ctx *ifi_ctx;
1434c7070dbSScott Long } *iflib_filter_info_t;
1444c7070dbSScott Long 
1454c7070dbSScott Long struct iflib_ctx {
1464c7070dbSScott Long 	KOBJ_FIELDS;
1474c7070dbSScott Long    /*
1484c7070dbSScott Long    * Pointer to hardware driver's softc
1494c7070dbSScott Long    */
1504c7070dbSScott Long 	void *ifc_softc;
1514c7070dbSScott Long 	device_t ifc_dev;
1524c7070dbSScott Long 	if_t ifc_ifp;
1534c7070dbSScott Long 
1544c7070dbSScott Long 	cpuset_t ifc_cpus;
1554c7070dbSScott Long 	if_shared_ctx_t ifc_sctx;
1564c7070dbSScott Long 	struct if_softc_ctx ifc_softc_ctx;
1574c7070dbSScott Long 
1584c7070dbSScott Long 	struct mtx ifc_mtx;
1594c7070dbSScott Long 
1604c7070dbSScott Long 	uint16_t ifc_nhwtxqs;
1614c7070dbSScott Long 	uint16_t ifc_nhwrxqs;
1624c7070dbSScott Long 
1634c7070dbSScott Long 	iflib_txq_t ifc_txqs;
1644c7070dbSScott Long 	iflib_rxq_t ifc_rxqs;
1654c7070dbSScott Long 	uint32_t ifc_if_flags;
1664c7070dbSScott Long 	uint32_t ifc_flags;
1674c7070dbSScott Long 	uint32_t ifc_max_fl_buf_size;
1684c7070dbSScott Long 	int ifc_in_detach;
1694c7070dbSScott Long 
1704c7070dbSScott Long 	int ifc_link_state;
1714c7070dbSScott Long 	int ifc_link_irq;
1724c7070dbSScott Long 	int ifc_pause_frames;
1734c7070dbSScott Long 	int ifc_watchdog_events;
1744c7070dbSScott Long 	struct cdev *ifc_led_dev;
1754c7070dbSScott Long 	struct resource *ifc_msix_mem;
1764c7070dbSScott Long 
1774c7070dbSScott Long 	struct if_irq ifc_legacy_irq;
1784c7070dbSScott Long 	struct grouptask ifc_admin_task;
1794c7070dbSScott Long 	struct grouptask ifc_vflr_task;
1804c7070dbSScott Long 	struct iflib_filter_info ifc_filter_info;
1814c7070dbSScott Long 	struct ifmedia	ifc_media;
1824c7070dbSScott Long 
1834c7070dbSScott Long 	struct sysctl_oid *ifc_sysctl_node;
1844c7070dbSScott Long 	uint16_t ifc_sysctl_ntxqs;
1854c7070dbSScott Long 	uint16_t ifc_sysctl_nrxqs;
18623ac9029SStephen Hurd 	uint16_t ifc_sysctl_qs_eq_override;
18723ac9029SStephen Hurd 
18823ac9029SStephen Hurd 	uint16_t ifc_sysctl_ntxds[8];
18923ac9029SStephen Hurd 	uint16_t ifc_sysctl_nrxds[8];
1904c7070dbSScott Long 	struct if_txrx ifc_txrx;
1914c7070dbSScott Long #define isc_txd_encap  ifc_txrx.ift_txd_encap
1924c7070dbSScott Long #define isc_txd_flush  ifc_txrx.ift_txd_flush
1934c7070dbSScott Long #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
1944c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available
1954c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
1964c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
1974c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush
1984c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
1994c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2004c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr
2014c7070dbSScott Long 	eventhandler_tag ifc_vlan_attach_event;
2024c7070dbSScott Long 	eventhandler_tag ifc_vlan_detach_event;
2034c7070dbSScott Long 	uint8_t ifc_mac[ETHER_ADDR_LEN];
2044c7070dbSScott Long 	char ifc_mtx_name[16];
2054c7070dbSScott Long };
2064c7070dbSScott Long 
2074c7070dbSScott Long 
2084c7070dbSScott Long void *
2094c7070dbSScott Long iflib_get_softc(if_ctx_t ctx)
2104c7070dbSScott Long {
2114c7070dbSScott Long 
2124c7070dbSScott Long 	return (ctx->ifc_softc);
2134c7070dbSScott Long }
2144c7070dbSScott Long 
2154c7070dbSScott Long device_t
2164c7070dbSScott Long iflib_get_dev(if_ctx_t ctx)
2174c7070dbSScott Long {
2184c7070dbSScott Long 
2194c7070dbSScott Long 	return (ctx->ifc_dev);
2204c7070dbSScott Long }
2214c7070dbSScott Long 
2224c7070dbSScott Long if_t
2234c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx)
2244c7070dbSScott Long {
2254c7070dbSScott Long 
2264c7070dbSScott Long 	return (ctx->ifc_ifp);
2274c7070dbSScott Long }
2284c7070dbSScott Long 
2294c7070dbSScott Long struct ifmedia *
2304c7070dbSScott Long iflib_get_media(if_ctx_t ctx)
2314c7070dbSScott Long {
2324c7070dbSScott Long 
2334c7070dbSScott Long 	return (&ctx->ifc_media);
2344c7070dbSScott Long }
2354c7070dbSScott Long 
2364c7070dbSScott Long void
2374c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
2384c7070dbSScott Long {
2394c7070dbSScott Long 
2404c7070dbSScott Long 	bcopy(mac, ctx->ifc_mac, ETHER_ADDR_LEN);
2414c7070dbSScott Long }
2424c7070dbSScott Long 
2434c7070dbSScott Long if_softc_ctx_t
2444c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx)
2454c7070dbSScott Long {
2464c7070dbSScott Long 
2474c7070dbSScott Long 	return (&ctx->ifc_softc_ctx);
2484c7070dbSScott Long }
2494c7070dbSScott Long 
2504c7070dbSScott Long if_shared_ctx_t
2514c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx)
2524c7070dbSScott Long {
2534c7070dbSScott Long 
2544c7070dbSScott Long 	return (ctx->ifc_sctx);
2554c7070dbSScott Long }
2564c7070dbSScott Long 
2574c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
2584c7070dbSScott Long 
2594c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
2604c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
2614c7070dbSScott Long 
2624c7070dbSScott Long #define RX_SW_DESC_MAP_CREATED	(1 << 0)
2634c7070dbSScott Long #define TX_SW_DESC_MAP_CREATED	(1 << 1)
2644c7070dbSScott Long #define RX_SW_DESC_INUSE        (1 << 3)
2654c7070dbSScott Long #define TX_SW_DESC_MAPPED       (1 << 4)
2664c7070dbSScott Long 
267e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array {
268e035717eSSean Bruno 	bus_dmamap_t	*ifsd_map;         /* bus_dma maps for packet */
269e035717eSSean Bruno 	struct mbuf	**ifsd_m;           /* pkthdr mbufs */
270e035717eSSean Bruno 	caddr_t		*ifsd_cl;          /* direct cluster pointer for rx */
271e035717eSSean Bruno 	uint8_t		*ifsd_flags;
272e035717eSSean Bruno } iflib_rxsd_array_t;
2734c7070dbSScott Long 
2744c7070dbSScott Long typedef struct iflib_sw_tx_desc_array {
2754c7070dbSScott Long 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
2764c7070dbSScott Long 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
2774c7070dbSScott Long 	uint8_t		*ifsd_flags;
2784c7070dbSScott Long } iflib_txsd_array_t;
2794c7070dbSScott Long 
2804c7070dbSScott Long 
2814c7070dbSScott Long /* magic number that should be high enough for any hardware */
2824c7070dbSScott Long #define IFLIB_MAX_TX_SEGS		128
2834c7070dbSScott Long #define IFLIB_MAX_RX_SEGS		32
284e035717eSSean Bruno #define IFLIB_RX_COPY_THRESH		63
2854c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH		32
2864c7070dbSScott Long #define IFLIB_QUEUE_IDLE		0
2874c7070dbSScott Long #define IFLIB_QUEUE_HUNG		1
2884c7070dbSScott Long #define IFLIB_QUEUE_WORKING		2
2894c7070dbSScott Long 
2904c7070dbSScott Long /* this should really scale with ring size - 32 is a fairly arbitrary value for this */
2914c7070dbSScott Long #define TX_BATCH_SIZE			16
2924c7070dbSScott Long 
2934c7070dbSScott Long #define IFLIB_RESTART_BUDGET		8
2944c7070dbSScott Long 
29523ac9029SStephen Hurd #define	IFC_LEGACY		0x01
29623ac9029SStephen Hurd #define	IFC_QFLUSH		0x02
29723ac9029SStephen Hurd #define	IFC_MULTISEG		0x04
29823ac9029SStephen Hurd #define	IFC_DMAR		0x08
29923ac9029SStephen Hurd #define	IFC_SC_ALLOCATED	0x10
3004ecb427aSSean Bruno #define	IFC_INIT_DONE		0x20
3014ecb427aSSean Bruno 
3024c7070dbSScott Long 
3034c7070dbSScott Long #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
3044c7070dbSScott Long 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
3054c7070dbSScott Long 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
3064c7070dbSScott Long struct iflib_txq {
3074c7070dbSScott Long 	uint16_t	ift_in_use;
3084c7070dbSScott Long 	uint16_t	ift_cidx;
3094c7070dbSScott Long 	uint16_t	ift_cidx_processed;
3104c7070dbSScott Long 	uint16_t	ift_pidx;
3114c7070dbSScott Long 	uint8_t		ift_gen;
3124c7070dbSScott Long 	uint8_t		ift_db_pending;
3134c7070dbSScott Long 	uint8_t		ift_db_pending_queued;
3144c7070dbSScott Long 	uint8_t		ift_npending;
31523ac9029SStephen Hurd 	uint8_t		ift_br_offset;
3164c7070dbSScott Long 	/* implicit pad */
3174c7070dbSScott Long 	uint64_t	ift_processed;
3184c7070dbSScott Long 	uint64_t	ift_cleaned;
3194c7070dbSScott Long #if MEMORY_LOGGING
3204c7070dbSScott Long 	uint64_t	ift_enqueued;
3214c7070dbSScott Long 	uint64_t	ift_dequeued;
3224c7070dbSScott Long #endif
3234c7070dbSScott Long 	uint64_t	ift_no_tx_dma_setup;
3244c7070dbSScott Long 	uint64_t	ift_no_desc_avail;
3254c7070dbSScott Long 	uint64_t	ift_mbuf_defrag_failed;
3264c7070dbSScott Long 	uint64_t	ift_mbuf_defrag;
3274c7070dbSScott Long 	uint64_t	ift_map_failed;
3284c7070dbSScott Long 	uint64_t	ift_txd_encap_efbig;
3294c7070dbSScott Long 	uint64_t	ift_pullups;
3304c7070dbSScott Long 
3314c7070dbSScott Long 	struct mtx	ift_mtx;
3324c7070dbSScott Long 	struct mtx	ift_db_mtx;
3334c7070dbSScott Long 
3344c7070dbSScott Long 	/* constant values */
3354c7070dbSScott Long 	if_ctx_t	ift_ctx;
3364c7070dbSScott Long 	struct ifmp_ring        **ift_br;
3374c7070dbSScott Long 	struct grouptask	ift_task;
3384c7070dbSScott Long 	uint16_t	ift_size;
3394c7070dbSScott Long 	uint16_t	ift_id;
3404c7070dbSScott Long 	struct callout	ift_timer;
3414c7070dbSScott Long 	struct callout	ift_db_check;
3424c7070dbSScott Long 
3434c7070dbSScott Long 	iflib_txsd_array_t	ift_sds;
3444c7070dbSScott Long 	uint8_t			ift_nbr;
3454c7070dbSScott Long 	uint8_t			ift_qstatus;
3464c7070dbSScott Long 	uint8_t			ift_active;
3474c7070dbSScott Long 	uint8_t			ift_closed;
3484c7070dbSScott Long 	int			ift_watchdog_time;
3494c7070dbSScott Long 	struct iflib_filter_info ift_filter_info;
3504c7070dbSScott Long 	bus_dma_tag_t		ift_desc_tag;
3514c7070dbSScott Long 	bus_dma_tag_t		ift_tso_desc_tag;
3524c7070dbSScott Long 	iflib_dma_info_t	ift_ifdi;
3534c7070dbSScott Long #define MTX_NAME_LEN 16
3544c7070dbSScott Long 	char                    ift_mtx_name[MTX_NAME_LEN];
3554c7070dbSScott Long 	char                    ift_db_mtx_name[MTX_NAME_LEN];
3564c7070dbSScott Long 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
3571248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
3581248952aSSean Bruno 	uint64_t ift_cpu_exec_count[256];
3591248952aSSean Bruno #endif
3604c7070dbSScott Long } __aligned(CACHE_LINE_SIZE);
3614c7070dbSScott Long 
3624c7070dbSScott Long struct iflib_fl {
3634c7070dbSScott Long 	uint16_t	ifl_cidx;
3644c7070dbSScott Long 	uint16_t	ifl_pidx;
3654c7070dbSScott Long 	uint16_t	ifl_credits;
3664c7070dbSScott Long 	uint8_t		ifl_gen;
3674c7070dbSScott Long #if MEMORY_LOGGING
3684c7070dbSScott Long 	uint64_t	ifl_m_enqueued;
3694c7070dbSScott Long 	uint64_t	ifl_m_dequeued;
3704c7070dbSScott Long 	uint64_t	ifl_cl_enqueued;
3714c7070dbSScott Long 	uint64_t	ifl_cl_dequeued;
3724c7070dbSScott Long #endif
3734c7070dbSScott Long 	/* implicit pad */
3744c7070dbSScott Long 
3754c7070dbSScott Long 	/* constant */
3764c7070dbSScott Long 	uint16_t	ifl_size;
3774c7070dbSScott Long 	uint16_t	ifl_buf_size;
3784c7070dbSScott Long 	uint16_t	ifl_cltype;
3794c7070dbSScott Long 	uma_zone_t	ifl_zone;
380e035717eSSean Bruno 	iflib_rxsd_array_t	ifl_sds;
3814c7070dbSScott Long 	iflib_rxq_t	ifl_rxq;
3824c7070dbSScott Long 	uint8_t		ifl_id;
3834c7070dbSScott Long 	bus_dma_tag_t           ifl_desc_tag;
3844c7070dbSScott Long 	iflib_dma_info_t	ifl_ifdi;
3854c7070dbSScott Long 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
3864c7070dbSScott Long 	caddr_t		ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
3874c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
3884c7070dbSScott Long 
3894c7070dbSScott Long static inline int
3904c7070dbSScott Long get_inuse(int size, int cidx, int pidx, int gen)
3914c7070dbSScott Long {
3924c7070dbSScott Long 	int used;
3934c7070dbSScott Long 
3944c7070dbSScott Long 	if (pidx > cidx)
3954c7070dbSScott Long 		used = pidx - cidx;
3964c7070dbSScott Long 	else if (pidx < cidx)
3974c7070dbSScott Long 		used = size - cidx + pidx;
3984c7070dbSScott Long 	else if (gen == 0 && pidx == cidx)
3994c7070dbSScott Long 		used = 0;
4004c7070dbSScott Long 	else if (gen == 1 && pidx == cidx)
4014c7070dbSScott Long 		used = size;
4024c7070dbSScott Long 	else
4034c7070dbSScott Long 		panic("bad state");
4044c7070dbSScott Long 
4054c7070dbSScott Long 	return (used);
4064c7070dbSScott Long }
4074c7070dbSScott Long 
4084c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
4094c7070dbSScott Long 
4104c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \
4114c7070dbSScott Long 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
4124c7070dbSScott Long 
4134c7070dbSScott Long struct iflib_rxq {
4144c7070dbSScott Long 	/* If there is a separate completion queue -
4154c7070dbSScott Long 	 * these are the cq cidx and pidx. Otherwise
4164c7070dbSScott Long 	 * these are unused.
4174c7070dbSScott Long 	 */
4184c7070dbSScott Long 	uint16_t	ifr_size;
4194c7070dbSScott Long 	uint16_t	ifr_cq_cidx;
4204c7070dbSScott Long 	uint16_t	ifr_cq_pidx;
4214c7070dbSScott Long 	uint8_t		ifr_cq_gen;
42223ac9029SStephen Hurd 	uint8_t		ifr_fl_offset;
4234c7070dbSScott Long 
4244c7070dbSScott Long 	if_ctx_t	ifr_ctx;
4254c7070dbSScott Long 	iflib_fl_t	ifr_fl;
4264c7070dbSScott Long 	uint64_t	ifr_rx_irq;
4274c7070dbSScott Long 	uint16_t	ifr_id;
4284c7070dbSScott Long 	uint8_t		ifr_lro_enabled;
4294c7070dbSScott Long 	uint8_t		ifr_nfl;
4304c7070dbSScott Long 	struct lro_ctrl			ifr_lc;
4314c7070dbSScott Long 	struct grouptask        ifr_task;
4324c7070dbSScott Long 	struct iflib_filter_info ifr_filter_info;
4334c7070dbSScott Long 	iflib_dma_info_t		ifr_ifdi;
4344c7070dbSScott Long 	/* dynamically allocate if any drivers need a value substantially larger than this */
4354c7070dbSScott Long 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
4361248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
4371248952aSSean Bruno 	uint64_t ifr_cpu_exec_count[256];
4381248952aSSean Bruno #endif
4394c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4404c7070dbSScott Long 
4414c7070dbSScott Long /*
4424c7070dbSScott Long  * Only allow a single packet to take up most 1/nth of the tx ring
4434c7070dbSScott Long  */
4444c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12
4454c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1
4464c7070dbSScott Long 
4474c7070dbSScott Long static int enable_msix = 1;
4484c7070dbSScott Long 
4494c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
4504c7070dbSScott Long 
4514c7070dbSScott Long #define CTX_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_mtx, _name, "iflib ctx lock", MTX_DEF)
4524c7070dbSScott Long 
4534c7070dbSScott Long #define CTX_LOCK(ctx) mtx_lock(&(ctx)->ifc_mtx)
4544c7070dbSScott Long #define CTX_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_mtx)
4554c7070dbSScott Long #define CTX_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_mtx)
4564c7070dbSScott Long 
4574c7070dbSScott Long 
4584c7070dbSScott Long #define TXDB_LOCK_INIT(txq)  mtx_init(&(txq)->ift_db_mtx, (txq)->ift_db_mtx_name, NULL, MTX_DEF)
4594c7070dbSScott Long #define TXDB_TRYLOCK(txq) mtx_trylock(&(txq)->ift_db_mtx)
4604c7070dbSScott Long #define TXDB_LOCK(txq) mtx_lock(&(txq)->ift_db_mtx)
4614c7070dbSScott Long #define TXDB_UNLOCK(txq) mtx_unlock(&(txq)->ift_db_mtx)
4624c7070dbSScott Long #define TXDB_LOCK_DESTROY(txq) mtx_destroy(&(txq)->ift_db_mtx)
4634c7070dbSScott Long 
4644c7070dbSScott Long #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
4654c7070dbSScott Long #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
4664c7070dbSScott Long 
4674c7070dbSScott Long 
4684c7070dbSScott Long /* Our boot-time initialization hook */
4694c7070dbSScott Long static int	iflib_module_event_handler(module_t, int, void *);
4704c7070dbSScott Long 
4714c7070dbSScott Long static moduledata_t iflib_moduledata = {
4724c7070dbSScott Long 	"iflib",
4734c7070dbSScott Long 	iflib_module_event_handler,
4744c7070dbSScott Long 	NULL
4754c7070dbSScott Long };
4764c7070dbSScott Long 
4774c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
4784c7070dbSScott Long MODULE_VERSION(iflib, 1);
4794c7070dbSScott Long 
4804c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1);
4814c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1);
4824c7070dbSScott Long 
4834c7070dbSScott Long TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
4844c7070dbSScott Long TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
4854c7070dbSScott Long 
4864c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS
4874c7070dbSScott Long #ifdef INVARIANTS
4884c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1
4894c7070dbSScott Long #else
4904c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0
4914c7070dbSScott Long #endif /* !INVARIANTS */
4924c7070dbSScott Long #endif
4934c7070dbSScott Long 
4944c7070dbSScott Long static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0,
4954c7070dbSScott Long                    "iflib driver parameters");
4964c7070dbSScott Long 
4974c7070dbSScott Long /*
4984c7070dbSScott Long  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
4994c7070dbSScott Long  */
5004c7070dbSScott Long static int iflib_min_tx_latency = 0;
5014c7070dbSScott Long 
5024c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
503da69b8f9SSean Bruno 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
5044c7070dbSScott Long 
5054c7070dbSScott Long 
5064c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS
5074c7070dbSScott Long 
5084c7070dbSScott Long static int iflib_tx_seen;
5094c7070dbSScott Long static int iflib_tx_sent;
5104c7070dbSScott Long static int iflib_tx_encap;
5114c7070dbSScott Long static int iflib_rx_allocs;
5124c7070dbSScott Long static int iflib_fl_refills;
5134c7070dbSScott Long static int iflib_fl_refills_large;
5144c7070dbSScott Long static int iflib_tx_frees;
5154c7070dbSScott Long 
5164c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
5174c7070dbSScott Long 		   &iflib_tx_seen, 0, "# tx mbufs seen");
5184c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
5194c7070dbSScott Long 		   &iflib_tx_sent, 0, "# tx mbufs sent");
5204c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
5214c7070dbSScott Long 		   &iflib_tx_encap, 0, "# tx mbufs encapped");
5224c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
5234c7070dbSScott Long 		   &iflib_tx_frees, 0, "# tx frees");
5244c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
5254c7070dbSScott Long 		   &iflib_rx_allocs, 0, "# rx allocations");
5264c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
5274c7070dbSScott Long 		   &iflib_fl_refills, 0, "# refills");
5284c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
5294c7070dbSScott Long 		   &iflib_fl_refills_large, 0, "# large refills");
5304c7070dbSScott Long 
5314c7070dbSScott Long 
5324c7070dbSScott Long static int iflib_txq_drain_flushing;
5334c7070dbSScott Long static int iflib_txq_drain_oactive;
5344c7070dbSScott Long static int iflib_txq_drain_notready;
5354c7070dbSScott Long static int iflib_txq_drain_encapfail;
5364c7070dbSScott Long 
5374c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
5384c7070dbSScott Long 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
5394c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
5404c7070dbSScott Long 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
5414c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
5424c7070dbSScott Long 		   &iflib_txq_drain_notready, 0, "# drain notready");
5434c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_encapfail, CTLFLAG_RD,
5444c7070dbSScott Long 		   &iflib_txq_drain_encapfail, 0, "# drain encap fails");
5454c7070dbSScott Long 
5464c7070dbSScott Long 
5474c7070dbSScott Long static int iflib_encap_load_mbuf_fail;
5484c7070dbSScott Long static int iflib_encap_txq_avail_fail;
5494c7070dbSScott Long static int iflib_encap_txd_encap_fail;
5504c7070dbSScott Long 
5514c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
5524c7070dbSScott Long 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
5534c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
5544c7070dbSScott Long 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
5554c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
5564c7070dbSScott Long 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
5574c7070dbSScott Long 
5584c7070dbSScott Long static int iflib_task_fn_rxs;
5594c7070dbSScott Long static int iflib_rx_intr_enables;
5604c7070dbSScott Long static int iflib_fast_intrs;
5614c7070dbSScott Long static int iflib_intr_link;
5624c7070dbSScott Long static int iflib_intr_msix;
5634c7070dbSScott Long static int iflib_rx_unavail;
5644c7070dbSScott Long static int iflib_rx_ctx_inactive;
5654c7070dbSScott Long static int iflib_rx_zero_len;
5664c7070dbSScott Long static int iflib_rx_if_input;
5674c7070dbSScott Long static int iflib_rx_mbuf_null;
5684c7070dbSScott Long static int iflib_rxd_flush;
5694c7070dbSScott Long 
5704c7070dbSScott Long static int iflib_verbose_debug;
5714c7070dbSScott Long 
5724c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_link, CTLFLAG_RD,
5734c7070dbSScott Long 		   &iflib_intr_link, 0, "# intr link calls");
5744c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_msix, CTLFLAG_RD,
5754c7070dbSScott Long 		   &iflib_intr_msix, 0, "# intr msix calls");
5764c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
5774c7070dbSScott Long 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
5784c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
5794c7070dbSScott Long 		   &iflib_rx_intr_enables, 0, "# rx intr enables");
5804c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
5814c7070dbSScott Long 		   &iflib_fast_intrs, 0, "# fast_intr calls");
5824c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
5834c7070dbSScott Long 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
5844c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
5854c7070dbSScott Long 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
5864c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_zero_len, CTLFLAG_RD,
5874c7070dbSScott Long 		   &iflib_rx_zero_len, 0, "# times rxeof saw zero len mbuf");
5884c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
5894c7070dbSScott Long 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
5904c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD,
5914c7070dbSScott Long 		   &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf");
5924c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
5934c7070dbSScott Long 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
5944c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
5954c7070dbSScott Long 		   &iflib_verbose_debug, 0, "enable verbose debugging");
5964c7070dbSScott Long 
5974c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
598da69b8f9SSean Bruno static void
599da69b8f9SSean Bruno iflib_debug_reset(void)
600da69b8f9SSean Bruno {
601da69b8f9SSean Bruno 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
602da69b8f9SSean Bruno 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
603da69b8f9SSean Bruno 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
604da69b8f9SSean Bruno 		iflib_txq_drain_notready = iflib_txq_drain_encapfail =
605da69b8f9SSean Bruno 		iflib_encap_load_mbuf_fail = iflib_encap_txq_avail_fail =
606da69b8f9SSean Bruno 		iflib_encap_txd_encap_fail = iflib_task_fn_rxs = iflib_rx_intr_enables =
607da69b8f9SSean Bruno 		iflib_fast_intrs = iflib_intr_link = iflib_intr_msix = iflib_rx_unavail =
608da69b8f9SSean Bruno 		iflib_rx_ctx_inactive = iflib_rx_zero_len = iflib_rx_if_input =
609da69b8f9SSean Bruno 		iflib_rx_mbuf_null = iflib_rxd_flush = 0;
610da69b8f9SSean Bruno }
6114c7070dbSScott Long 
6124c7070dbSScott Long #else
6134c7070dbSScott Long #define DBG_COUNTER_INC(name)
614da69b8f9SSean Bruno static void iflib_debug_reset(void) {}
6154c7070dbSScott Long #endif
6164c7070dbSScott Long 
6174c7070dbSScott Long 
6184c7070dbSScott Long 
6194c7070dbSScott Long #define IFLIB_DEBUG 0
6204c7070dbSScott Long 
6214c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx);
6224c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx);
6234c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx);
6244c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
62523ac9029SStephen Hurd static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, int cidx, int budget);
6264c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx);
6274c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx);
6284c7070dbSScott Long static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, char *str);
6294c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
6304c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
6314c7070dbSScott Long static int iflib_register(if_ctx_t);
6324c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx);
6334c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
6344c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx);
635da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq);
6361248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx);
6374c7070dbSScott Long 
6384c7070dbSScott Long #ifdef DEV_NETMAP
6394c7070dbSScott Long #include <sys/selinfo.h>
6404c7070dbSScott Long #include <net/netmap.h>
6414c7070dbSScott Long #include <dev/netmap/netmap_kern.h>
6424c7070dbSScott Long 
6434c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1);
6444c7070dbSScott Long 
6454c7070dbSScott Long /*
6464c7070dbSScott Long  * device-specific sysctl variables:
6474c7070dbSScott Long  *
64891d546a0SConrad Meyer  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
6494c7070dbSScott Long  *	During regular operations the CRC is stripped, but on some
6504c7070dbSScott Long  *	hardware reception of frames not multiple of 64 is slower,
6514c7070dbSScott Long  *	so using crcstrip=0 helps in benchmarks.
6524c7070dbSScott Long  *
65391d546a0SConrad Meyer  * iflib_rx_miss, iflib_rx_miss_bufs:
6544c7070dbSScott Long  *	count packets that might be missed due to lost interrupts.
6554c7070dbSScott Long  */
6564c7070dbSScott Long SYSCTL_DECL(_dev_netmap);
6574c7070dbSScott Long /*
6584c7070dbSScott Long  * The xl driver by default strips CRCs and we do not override it.
6594c7070dbSScott Long  */
6604c7070dbSScott Long 
6614c7070dbSScott Long int iflib_crcstrip = 1;
6624c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
6634c7070dbSScott Long     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames");
6644c7070dbSScott Long 
6654c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs;
6664c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
6674c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr");
66891d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
6694c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs");
6704c7070dbSScott Long 
6714c7070dbSScott Long /*
6724c7070dbSScott Long  * Register/unregister. We are already under netmap lock.
6734c7070dbSScott Long  * Only called on the first register or the last unregister.
6744c7070dbSScott Long  */
6754c7070dbSScott Long static int
6764c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff)
6774c7070dbSScott Long {
6784c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
6794c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
6804c7070dbSScott Long 
6814c7070dbSScott Long 	CTX_LOCK(ctx);
6824c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
6834c7070dbSScott Long 
6844c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
6854c7070dbSScott Long 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6864c7070dbSScott Long 
6874c7070dbSScott Long 	if (!CTX_IS_VF(ctx))
6881248952aSSean Bruno 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
6894c7070dbSScott Long 
6904c7070dbSScott Long 	/* enable or disable flags and callbacks in na and ifp */
6914c7070dbSScott Long 	if (onoff) {
6924c7070dbSScott Long 		nm_set_native_flags(na);
6934c7070dbSScott Long 	} else {
6944c7070dbSScott Long 		nm_clear_native_flags(na);
6954c7070dbSScott Long 	}
6964c7070dbSScott Long 	IFDI_INIT(ctx);
6971248952aSSean Bruno 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
6984c7070dbSScott Long 	CTX_UNLOCK(ctx);
6994c7070dbSScott Long 	return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
7004c7070dbSScott Long }
7014c7070dbSScott Long 
7024c7070dbSScott Long /*
7034c7070dbSScott Long  * Reconcile kernel and user view of the transmit ring.
7044c7070dbSScott Long  *
7054c7070dbSScott Long  * All information is in the kring.
7064c7070dbSScott Long  * Userspace wants to send packets up to the one before kring->rhead,
7074c7070dbSScott Long  * kernel knows kring->nr_hwcur is the first unsent packet.
7084c7070dbSScott Long  *
7094c7070dbSScott Long  * Here we push packets out (as many as possible), and possibly
7104c7070dbSScott Long  * reclaim buffers from previously completed transmission.
7114c7070dbSScott Long  *
7124c7070dbSScott Long  * The caller (netmap) guarantees that there is only one instance
7134c7070dbSScott Long  * running at any time. Any interference with other driver
7144c7070dbSScott Long  * methods should be handled by the individual drivers.
7154c7070dbSScott Long  */
7164c7070dbSScott Long static int
7174c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags)
7184c7070dbSScott Long {
7194c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
7204c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
7214c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
7224c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
7234c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
7244c7070dbSScott Long 	u_int n;
7254c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
7264c7070dbSScott Long 	u_int const head = kring->rhead;
7274c7070dbSScott Long 	struct if_pkt_info pi;
7284c7070dbSScott Long 
7294c7070dbSScott Long 	/*
7304c7070dbSScott Long 	 * interrupts on every tx packet are expensive so request
7314c7070dbSScott Long 	 * them every half ring, or where NS_REPORT is set
7324c7070dbSScott Long 	 */
7334c7070dbSScott Long 	u_int report_frequency = kring->nkr_num_slots >> 1;
7344c7070dbSScott Long 	/* device-specific */
7354c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
7364c7070dbSScott Long 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
7374c7070dbSScott Long 
7384c7070dbSScott Long 	pi.ipi_segs = txq->ift_segs;
7394c7070dbSScott Long 	pi.ipi_qsidx = kring->ring_id;
7404c7070dbSScott Long 	pi.ipi_ndescs = 0;
7414c7070dbSScott Long 
7424c7070dbSScott Long 	bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
7434c7070dbSScott Long 					BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
7444c7070dbSScott Long 
7454c7070dbSScott Long 
7464c7070dbSScott Long 	/*
7474c7070dbSScott Long 	 * First part: process new packets to send.
7484c7070dbSScott Long 	 * nm_i is the current index in the netmap ring,
7494c7070dbSScott Long 	 * nic_i is the corresponding index in the NIC ring.
7504c7070dbSScott Long 	 *
7514c7070dbSScott Long 	 * If we have packets to send (nm_i != head)
7524c7070dbSScott Long 	 * iterate over the netmap ring, fetch length and update
7534c7070dbSScott Long 	 * the corresponding slot in the NIC ring. Some drivers also
7544c7070dbSScott Long 	 * need to update the buffer's physical address in the NIC slot
7554c7070dbSScott Long 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
7564c7070dbSScott Long 	 *
7574c7070dbSScott Long 	 * The netmap_reload_map() calls is especially expensive,
7584c7070dbSScott Long 	 * even when (as in this case) the tag is 0, so do only
7594c7070dbSScott Long 	 * when the buffer has actually changed.
7604c7070dbSScott Long 	 *
7614c7070dbSScott Long 	 * If possible do not set the report/intr bit on all slots,
7624c7070dbSScott Long 	 * but only a few times per ring or when NS_REPORT is set.
7634c7070dbSScott Long 	 *
7644c7070dbSScott Long 	 * Finally, on 10G and faster drivers, it might be useful
7654c7070dbSScott Long 	 * to prefetch the next slot and txr entry.
7664c7070dbSScott Long 	 */
7674c7070dbSScott Long 
7684c7070dbSScott Long 	nm_i = kring->nr_hwcur;
7694c7070dbSScott Long 	if (nm_i != head) {	/* we have new packets to send */
7704c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
7714c7070dbSScott Long 
7724c7070dbSScott Long 		__builtin_prefetch(&ring->slot[nm_i]);
7734c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
7744c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
7754c7070dbSScott Long 
7764c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
7774c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
7784c7070dbSScott Long 			u_int len = slot->len;
7794c7070dbSScott Long 			uint64_t paddr;
7804c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
7814c7070dbSScott Long 			int flags = (slot->flags & NS_REPORT ||
7824c7070dbSScott Long 				nic_i == 0 || nic_i == report_frequency) ?
7834c7070dbSScott Long 				IPI_TX_INTR : 0;
7844c7070dbSScott Long 
7854c7070dbSScott Long 			/* device-specific */
7864c7070dbSScott Long 			pi.ipi_pidx = nic_i;
7874c7070dbSScott Long 			pi.ipi_flags = flags;
7884c7070dbSScott Long 
7894c7070dbSScott Long 			/* Fill the slot in the NIC ring. */
7904c7070dbSScott Long 			ctx->isc_txd_encap(ctx->ifc_softc, &pi);
7914c7070dbSScott Long 
7924c7070dbSScott Long 			/* prefetch for next round */
7934c7070dbSScott Long 			__builtin_prefetch(&ring->slot[nm_i + 1]);
7944c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
7954c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
7964c7070dbSScott Long 
7974c7070dbSScott Long 			NM_CHECK_ADDR_LEN(na, addr, len);
7984c7070dbSScott Long 
7994c7070dbSScott Long 			if (slot->flags & NS_BUF_CHANGED) {
8004c7070dbSScott Long 				/* buffer has changed, reload map */
8014c7070dbSScott Long 				netmap_reload_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[nic_i], addr);
8024c7070dbSScott Long 			}
8034c7070dbSScott Long 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
8044c7070dbSScott Long 
8054c7070dbSScott Long 			/* make sure changes to the buffer are synced */
8064c7070dbSScott Long 			bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_sds.ifsd_map[nic_i],
8074c7070dbSScott Long 							BUS_DMASYNC_PREWRITE);
8084c7070dbSScott Long 
8094c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
8104c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
8114c7070dbSScott Long 		}
8124c7070dbSScott Long 		kring->nr_hwcur = head;
8134c7070dbSScott Long 
8144c7070dbSScott Long 		/* synchronize the NIC ring */
8154c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
8164c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
8174c7070dbSScott Long 
8184c7070dbSScott Long 		/* (re)start the tx unit up to slot nic_i (excluded) */
8194c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
8204c7070dbSScott Long 	}
8214c7070dbSScott Long 
8224c7070dbSScott Long 	/*
8234c7070dbSScott Long 	 * Second part: reclaim buffers for completed transmissions.
8244c7070dbSScott Long 	 */
8254c7070dbSScott Long 	if (iflib_tx_credits_update(ctx, txq)) {
8264c7070dbSScott Long 		/* some tx completed, increment avail */
8274c7070dbSScott Long 		nic_i = txq->ift_cidx_processed;
8284c7070dbSScott Long 		kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
8294c7070dbSScott Long 	}
8304c7070dbSScott Long 	return (0);
8314c7070dbSScott Long }
8324c7070dbSScott Long 
8334c7070dbSScott Long /*
8344c7070dbSScott Long  * Reconcile kernel and user view of the receive ring.
8354c7070dbSScott Long  * Same as for the txsync, this routine must be efficient.
8364c7070dbSScott Long  * The caller guarantees a single invocations, but races against
8374c7070dbSScott Long  * the rest of the driver should be handled here.
8384c7070dbSScott Long  *
8394c7070dbSScott Long  * On call, kring->rhead is the first packet that userspace wants
8404c7070dbSScott Long  * to keep, and kring->rcur is the wakeup point.
8414c7070dbSScott Long  * The kernel has previously reported packets up to kring->rtail.
8424c7070dbSScott Long  *
8434c7070dbSScott Long  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
8444c7070dbSScott Long  * of whether or not we received an interrupt.
8454c7070dbSScott Long  */
8464c7070dbSScott Long static int
8474c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
8484c7070dbSScott Long {
8494c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
8504c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
8514c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
8524c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
8534c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
8544c7070dbSScott Long 	u_int i, n;
8554c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
8564c7070dbSScott Long 	u_int const head = kring->rhead;
8574c7070dbSScott Long 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
8584c7070dbSScott Long 	struct if_rxd_info ri;
8594c7070dbSScott Long 	/* device-specific */
8604c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
8614c7070dbSScott Long 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
8624c7070dbSScott Long 	iflib_fl_t fl = rxq->ifr_fl;
8634c7070dbSScott Long 	if (head > lim)
8644c7070dbSScott Long 		return netmap_ring_reinit(kring);
8654c7070dbSScott Long 
8664c7070dbSScott Long 	bzero(&ri, sizeof(ri));
8674c7070dbSScott Long 	ri.iri_qsidx = kring->ring_id;
8684c7070dbSScott Long 	ri.iri_ifp = ctx->ifc_ifp;
8694c7070dbSScott Long 	/* XXX check sync modes */
8704c7070dbSScott Long 	for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++)
8714c7070dbSScott Long 		bus_dmamap_sync(rxq->ifr_fl[i].ifl_desc_tag, fl->ifl_ifdi->idi_map,
8724c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
8734c7070dbSScott Long 
8744c7070dbSScott Long 	/*
8754c7070dbSScott Long 	 * First part: import newly received packets.
8764c7070dbSScott Long 	 *
8774c7070dbSScott Long 	 * nm_i is the index of the next free slot in the netmap ring,
8784c7070dbSScott Long 	 * nic_i is the index of the next received packet in the NIC ring,
8794c7070dbSScott Long 	 * and they may differ in case if_init() has been called while
8804c7070dbSScott Long 	 * in netmap mode. For the receive ring we have
8814c7070dbSScott Long 	 *
8824c7070dbSScott Long 	 *	nic_i = rxr->next_check;
8834c7070dbSScott Long 	 *	nm_i = kring->nr_hwtail (previous)
8844c7070dbSScott Long 	 * and
8854c7070dbSScott Long 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
8864c7070dbSScott Long 	 *
8874c7070dbSScott Long 	 * rxr->next_check is set to 0 on a ring reinit
8884c7070dbSScott Long 	 */
8894c7070dbSScott Long 	if (netmap_no_pendintr || force_update) {
8904c7070dbSScott Long 		int crclen = iflib_crcstrip ? 0 : 4;
8914c7070dbSScott Long 		int error, avail;
8924c7070dbSScott Long 		uint16_t slot_flags = kring->nkr_slot_flags;
8934c7070dbSScott Long 
8944c7070dbSScott Long 		for (fl = rxq->ifr_fl, i = 0; i < rxq->ifr_nfl; i++, fl++) {
8954c7070dbSScott Long 			nic_i = fl->ifl_cidx;
8964c7070dbSScott Long 			nm_i = netmap_idx_n2k(kring, nic_i);
89723ac9029SStephen Hurd 			avail = ctx->isc_rxd_available(ctx->ifc_softc, kring->ring_id, nic_i, INT_MAX);
8984c7070dbSScott Long 			for (n = 0; avail > 0; n++, avail--) {
8994c7070dbSScott Long 				error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
9004c7070dbSScott Long 				if (error)
9014c7070dbSScott Long 					ring->slot[nm_i].len = 0;
9024c7070dbSScott Long 				else
9034c7070dbSScott Long 					ring->slot[nm_i].len = ri.iri_len - crclen;
9044c7070dbSScott Long 				ring->slot[nm_i].flags = slot_flags;
9054c7070dbSScott Long 				bus_dmamap_sync(fl->ifl_ifdi->idi_tag,
906e035717eSSean Bruno 								fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
9074c7070dbSScott Long 				nm_i = nm_next(nm_i, lim);
9084c7070dbSScott Long 				nic_i = nm_next(nic_i, lim);
9094c7070dbSScott Long 			}
9104c7070dbSScott Long 			if (n) { /* update the state variables */
9114c7070dbSScott Long 				if (netmap_no_pendintr && !force_update) {
9124c7070dbSScott Long 					/* diagnostics */
9134c7070dbSScott Long 					iflib_rx_miss ++;
9144c7070dbSScott Long 					iflib_rx_miss_bufs += n;
9154c7070dbSScott Long 				}
9164c7070dbSScott Long 				fl->ifl_cidx = nic_i;
9174c7070dbSScott Long 				kring->nr_hwtail = nm_i;
9184c7070dbSScott Long 			}
9194c7070dbSScott Long 			kring->nr_kflags &= ~NKR_PENDINTR;
9204c7070dbSScott Long 		}
9214c7070dbSScott Long 	}
9224c7070dbSScott Long 	/*
9234c7070dbSScott Long 	 * Second part: skip past packets that userspace has released.
9244c7070dbSScott Long 	 * (kring->nr_hwcur to head excluded),
9254c7070dbSScott Long 	 * and make the buffers available for reception.
9264c7070dbSScott Long 	 * As usual nm_i is the index in the netmap ring,
9274c7070dbSScott Long 	 * nic_i is the index in the NIC ring, and
9284c7070dbSScott Long 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
9294c7070dbSScott Long 	 */
9304c7070dbSScott Long 	/* XXX not sure how this will work with multiple free lists */
9314c7070dbSScott Long 	nm_i = kring->nr_hwcur;
9324c7070dbSScott Long 	if (nm_i != head) {
9334c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
9344c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
9354c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
9364c7070dbSScott Long 			uint64_t paddr;
9374c7070dbSScott Long 			caddr_t vaddr;
9384c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
9394c7070dbSScott Long 
9404c7070dbSScott Long 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
9414c7070dbSScott Long 				goto ring_reset;
9424c7070dbSScott Long 
9434c7070dbSScott Long 			vaddr = addr;
9444c7070dbSScott Long 			if (slot->flags & NS_BUF_CHANGED) {
9454c7070dbSScott Long 				/* buffer has changed, reload map */
946e035717eSSean Bruno 				netmap_reload_map(na, fl->ifl_ifdi->idi_tag, fl->ifl_sds.ifsd_map[nic_i], addr);
9474c7070dbSScott Long 				slot->flags &= ~NS_BUF_CHANGED;
9484c7070dbSScott Long 			}
9494c7070dbSScott Long 			/*
9504c7070dbSScott Long 			 * XXX we should be batching this operation - TODO
9514c7070dbSScott Long 			 */
95223ac9029SStephen Hurd 			ctx->isc_rxd_refill(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i, &paddr, &vaddr, 1, fl->ifl_buf_size);
953e035717eSSean Bruno 			bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_sds.ifsd_map[nic_i],
9544c7070dbSScott Long 			    BUS_DMASYNC_PREREAD);
9554c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
9564c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
9574c7070dbSScott Long 		}
9584c7070dbSScott Long 		kring->nr_hwcur = head;
9594c7070dbSScott Long 
9604c7070dbSScott Long 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
9614c7070dbSScott Long 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
9624c7070dbSScott Long 		/*
9634c7070dbSScott Long 		 * IMPORTANT: we must leave one free slot in the ring,
9644c7070dbSScott Long 		 * so move nic_i back by one unit
9654c7070dbSScott Long 		 */
9664c7070dbSScott Long 		nic_i = nm_prev(nic_i, lim);
9674c7070dbSScott Long 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
9684c7070dbSScott Long 	}
9694c7070dbSScott Long 
9704c7070dbSScott Long 	return 0;
9714c7070dbSScott Long 
9724c7070dbSScott Long ring_reset:
9734c7070dbSScott Long 	return netmap_ring_reinit(kring);
9744c7070dbSScott Long }
9754c7070dbSScott Long 
9764c7070dbSScott Long static int
9774c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx)
9784c7070dbSScott Long {
9794c7070dbSScott Long 	struct netmap_adapter na;
98023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
9814c7070dbSScott Long 
9824c7070dbSScott Long 	bzero(&na, sizeof(na));
9834c7070dbSScott Long 
9844c7070dbSScott Long 	na.ifp = ctx->ifc_ifp;
9854c7070dbSScott Long 	na.na_flags = NAF_BDG_MAYSLEEP;
9864c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
9874c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
9884c7070dbSScott Long 
98923ac9029SStephen Hurd 	na.num_tx_desc = scctx->isc_ntxd[0];
99023ac9029SStephen Hurd 	na.num_rx_desc = scctx->isc_nrxd[0];
9914c7070dbSScott Long 	na.nm_txsync = iflib_netmap_txsync;
9924c7070dbSScott Long 	na.nm_rxsync = iflib_netmap_rxsync;
9934c7070dbSScott Long 	na.nm_register = iflib_netmap_register;
9944c7070dbSScott Long 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
9954c7070dbSScott Long 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
9964c7070dbSScott Long 	return (netmap_attach(&na));
9974c7070dbSScott Long }
9984c7070dbSScott Long 
9994c7070dbSScott Long static void
10004c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
10014c7070dbSScott Long {
10024c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
10034c7070dbSScott Long 	struct netmap_slot *slot;
10044c7070dbSScott Long 
10054c7070dbSScott Long 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
10064c7070dbSScott Long 	if (slot == 0)
10074c7070dbSScott Long 		return;
10084c7070dbSScott Long 
100923ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
10104c7070dbSScott Long 
10114c7070dbSScott Long 		/*
10124c7070dbSScott Long 		 * In netmap mode, set the map for the packet buffer.
10134c7070dbSScott Long 		 * NOTE: Some drivers (not this one) also need to set
10144c7070dbSScott Long 		 * the physical buffer address in the NIC ring.
10154c7070dbSScott Long 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
10164c7070dbSScott Long 		 * netmap slot index, si
10174c7070dbSScott Long 		 */
10184c7070dbSScott Long 		int si = netmap_idx_n2k(&na->tx_rings[txq->ift_id], i);
10194c7070dbSScott Long 		netmap_load_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[i], NMB(na, slot + si));
10204c7070dbSScott Long 	}
10214c7070dbSScott Long }
10224c7070dbSScott Long static void
10234c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
10244c7070dbSScott Long {
10254c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
10264c7070dbSScott Long 	struct netmap_slot *slot;
1027e035717eSSean Bruno 	bus_dmamap_t *map;
10284c7070dbSScott Long 	int nrxd;
10294c7070dbSScott Long 
10304c7070dbSScott Long 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
10314c7070dbSScott Long 	if (slot == 0)
10324c7070dbSScott Long 		return;
1033e035717eSSean Bruno 	map = rxq->ifr_fl[0].ifl_sds.ifsd_map;
103423ac9029SStephen Hurd 	nrxd = ctx->ifc_softc_ctx.isc_nrxd[0];
1035e035717eSSean Bruno 	for (int i = 0; i < nrxd; i++, map++) {
10364c7070dbSScott Long 			int sj = netmap_idx_n2k(&na->rx_rings[rxq->ifr_id], i);
10374c7070dbSScott Long 			uint64_t paddr;
10384c7070dbSScott Long 			void *addr;
10394c7070dbSScott Long 			caddr_t vaddr;
10404c7070dbSScott Long 
10414c7070dbSScott Long 			vaddr = addr = PNMB(na, slot + sj, &paddr);
1042e035717eSSean Bruno 			netmap_load_map(na, rxq->ifr_fl[0].ifl_ifdi->idi_tag, *map, addr);
10434c7070dbSScott Long 			/* Update descriptor and the cached value */
104423ac9029SStephen Hurd 			ctx->isc_rxd_refill(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, i, &paddr, &vaddr, 1, rxq->ifr_fl[0].ifl_buf_size);
10454c7070dbSScott Long 	}
10464c7070dbSScott Long 	/* preserve queue */
10474c7070dbSScott Long 	if (ctx->ifc_ifp->if_capenable & IFCAP_NETMAP) {
10484c7070dbSScott Long 		struct netmap_kring *kring = &na->rx_rings[rxq->ifr_id];
10494c7070dbSScott Long 		int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
10504c7070dbSScott Long 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, t);
10514c7070dbSScott Long 	} else
10524c7070dbSScott Long 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, nrxd-1);
10534c7070dbSScott Long }
10544c7070dbSScott Long 
10554c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp)
10564c7070dbSScott Long 
10574c7070dbSScott Long #else
10584c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq)
10594c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq)
10604c7070dbSScott Long #define iflib_netmap_detach(ifp)
10614c7070dbSScott Long 
10624c7070dbSScott Long #define iflib_netmap_attach(ctx) (0)
10634c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0)
10644c7070dbSScott Long 
10654c7070dbSScott Long #endif
10664c7070dbSScott Long 
10674c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
10684c7070dbSScott Long static __inline void
10694c7070dbSScott Long prefetch(void *x)
10704c7070dbSScott Long {
10714c7070dbSScott Long 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
10724c7070dbSScott Long }
10734c7070dbSScott Long #else
10744c7070dbSScott Long #define prefetch(x)
10754c7070dbSScott Long #endif
10764c7070dbSScott Long 
10774c7070dbSScott Long static void
10784c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
10794c7070dbSScott Long {
10804c7070dbSScott Long 	if (err)
10814c7070dbSScott Long 		return;
10824c7070dbSScott Long 	*(bus_addr_t *) arg = segs[0].ds_addr;
10834c7070dbSScott Long }
10844c7070dbSScott Long 
10854c7070dbSScott Long int
10864c7070dbSScott Long iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
10874c7070dbSScott Long {
10884c7070dbSScott Long 	int err;
10894c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
10904c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
10914c7070dbSScott Long 
10924c7070dbSScott Long 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
10934c7070dbSScott Long 
10944c7070dbSScott Long 	err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
10954c7070dbSScott Long 				sctx->isc_q_align, 0,	/* alignment, bounds */
10964c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* lowaddr */
10974c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* highaddr */
10984c7070dbSScott Long 				NULL, NULL,		/* filter, filterarg */
10994c7070dbSScott Long 				size,			/* maxsize */
11004c7070dbSScott Long 				1,			/* nsegments */
11014c7070dbSScott Long 				size,			/* maxsegsize */
11024c7070dbSScott Long 				BUS_DMA_ALLOCNOW,	/* flags */
11034c7070dbSScott Long 				NULL,			/* lockfunc */
11044c7070dbSScott Long 				NULL,			/* lockarg */
11054c7070dbSScott Long 				&dma->idi_tag);
11064c7070dbSScott Long 	if (err) {
11074c7070dbSScott Long 		device_printf(dev,
11084c7070dbSScott Long 		    "%s: bus_dma_tag_create failed: %d\n",
11094c7070dbSScott Long 		    __func__, err);
11104c7070dbSScott Long 		goto fail_0;
11114c7070dbSScott Long 	}
11124c7070dbSScott Long 
11134c7070dbSScott Long 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
11144c7070dbSScott Long 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
11154c7070dbSScott Long 	if (err) {
11164c7070dbSScott Long 		device_printf(dev,
11174c7070dbSScott Long 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
11184c7070dbSScott Long 		    __func__, (uintmax_t)size, err);
11194c7070dbSScott Long 		goto fail_1;
11204c7070dbSScott Long 	}
11214c7070dbSScott Long 
11224c7070dbSScott Long 	dma->idi_paddr = IF_BAD_DMA;
11234c7070dbSScott Long 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
11244c7070dbSScott Long 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
11254c7070dbSScott Long 	if (err || dma->idi_paddr == IF_BAD_DMA) {
11264c7070dbSScott Long 		device_printf(dev,
11274c7070dbSScott Long 		    "%s: bus_dmamap_load failed: %d\n",
11284c7070dbSScott Long 		    __func__, err);
11294c7070dbSScott Long 		goto fail_2;
11304c7070dbSScott Long 	}
11314c7070dbSScott Long 
11324c7070dbSScott Long 	dma->idi_size = size;
11334c7070dbSScott Long 	return (0);
11344c7070dbSScott Long 
11354c7070dbSScott Long fail_2:
11364c7070dbSScott Long 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
11374c7070dbSScott Long fail_1:
11384c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
11394c7070dbSScott Long fail_0:
11404c7070dbSScott Long 	dma->idi_tag = NULL;
11414c7070dbSScott Long 
11424c7070dbSScott Long 	return (err);
11434c7070dbSScott Long }
11444c7070dbSScott Long 
11454c7070dbSScott Long int
11464c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
11474c7070dbSScott Long {
11484c7070dbSScott Long 	int i, err;
11494c7070dbSScott Long 	iflib_dma_info_t *dmaiter;
11504c7070dbSScott Long 
11514c7070dbSScott Long 	dmaiter = dmalist;
11524c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++) {
11534c7070dbSScott Long 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
11544c7070dbSScott Long 			break;
11554c7070dbSScott Long 	}
11564c7070dbSScott Long 	if (err)
11574c7070dbSScott Long 		iflib_dma_free_multi(dmalist, i);
11584c7070dbSScott Long 	return (err);
11594c7070dbSScott Long }
11604c7070dbSScott Long 
11614c7070dbSScott Long void
11624c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma)
11634c7070dbSScott Long {
11644c7070dbSScott Long 	if (dma->idi_tag == NULL)
11654c7070dbSScott Long 		return;
11664c7070dbSScott Long 	if (dma->idi_paddr != IF_BAD_DMA) {
11674c7070dbSScott Long 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
11684c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
11694c7070dbSScott Long 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
11704c7070dbSScott Long 		dma->idi_paddr = IF_BAD_DMA;
11714c7070dbSScott Long 	}
11724c7070dbSScott Long 	if (dma->idi_vaddr != NULL) {
11734c7070dbSScott Long 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
11744c7070dbSScott Long 		dma->idi_vaddr = NULL;
11754c7070dbSScott Long 	}
11764c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
11774c7070dbSScott Long 	dma->idi_tag = NULL;
11784c7070dbSScott Long }
11794c7070dbSScott Long 
11804c7070dbSScott Long void
11814c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
11824c7070dbSScott Long {
11834c7070dbSScott Long 	int i;
11844c7070dbSScott Long 	iflib_dma_info_t *dmaiter = dmalist;
11854c7070dbSScott Long 
11864c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++)
11874c7070dbSScott Long 		iflib_dma_free(*dmaiter);
11884c7070dbSScott Long }
11894c7070dbSScott Long 
1190bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP
1191bd84f700SSean Bruno static const int iflib_started = 1;
1192bd84f700SSean Bruno #else
1193bd84f700SSean Bruno /*
1194bd84f700SSean Bruno  * We used to abuse the smp_started flag to decide if the queues have been
1195bd84f700SSean Bruno  * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()).
1196bd84f700SSean Bruno  * That gave bad races, since the SYSINIT() runs strictly after smp_started
1197bd84f700SSean Bruno  * is set.  Run a SYSINIT() strictly after that to just set a usable
1198bd84f700SSean Bruno  * completion flag.
1199bd84f700SSean Bruno  */
1200bd84f700SSean Bruno 
1201bd84f700SSean Bruno static int iflib_started;
1202bd84f700SSean Bruno 
1203bd84f700SSean Bruno static void
1204bd84f700SSean Bruno iflib_record_started(void *arg)
1205bd84f700SSean Bruno {
1206bd84f700SSean Bruno 	iflib_started = 1;
1207bd84f700SSean Bruno }
1208bd84f700SSean Bruno 
1209bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST,
1210bd84f700SSean Bruno 	iflib_record_started, NULL);
1211bd84f700SSean Bruno #endif
1212bd84f700SSean Bruno 
12134c7070dbSScott Long static int
12144c7070dbSScott Long iflib_fast_intr(void *arg)
12154c7070dbSScott Long {
12164c7070dbSScott Long 	iflib_filter_info_t info = arg;
12174c7070dbSScott Long 	struct grouptask *gtask = info->ifi_task;
12184c7070dbSScott Long 
1219bd84f700SSean Bruno 	if (!iflib_started)
12201248952aSSean Bruno 		return (FILTER_HANDLED);
12211248952aSSean Bruno 
12224c7070dbSScott Long 	DBG_COUNTER_INC(fast_intrs);
12234c7070dbSScott Long 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
12244c7070dbSScott Long 		return (FILTER_HANDLED);
12254c7070dbSScott Long 
12264c7070dbSScott Long 	GROUPTASK_ENQUEUE(gtask);
12274c7070dbSScott Long 	return (FILTER_HANDLED);
12284c7070dbSScott Long }
12294c7070dbSScott Long 
12304c7070dbSScott Long static int
12314c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
12324c7070dbSScott Long 	driver_filter_t filter, driver_intr_t handler, void *arg,
12334c7070dbSScott Long 				 char *name)
12344c7070dbSScott Long {
12354c7070dbSScott Long 	int rc;
12364c7070dbSScott Long 	struct resource *res;
12374c7070dbSScott Long 	void *tag;
12384c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
12394c7070dbSScott Long 
12404c7070dbSScott Long 	MPASS(rid < 512);
12414c7070dbSScott Long 	irq->ii_rid = rid;
12424c7070dbSScott Long 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid,
12434c7070dbSScott Long 				     RF_SHAREABLE | RF_ACTIVE);
12444c7070dbSScott Long 	if (res == NULL) {
12454c7070dbSScott Long 		device_printf(dev,
12464c7070dbSScott Long 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
12474c7070dbSScott Long 		return (ENOMEM);
12484c7070dbSScott Long 	}
12494c7070dbSScott Long 	irq->ii_res = res;
12504c7070dbSScott Long 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
12514c7070dbSScott Long 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
12524c7070dbSScott Long 						filter, handler, arg, &tag);
12534c7070dbSScott Long 	if (rc != 0) {
12544c7070dbSScott Long 		device_printf(dev,
12554c7070dbSScott Long 		    "failed to setup interrupt for rid %d, name %s: %d\n",
12564c7070dbSScott Long 					  rid, name ? name : "unknown", rc);
12574c7070dbSScott Long 		return (rc);
12584c7070dbSScott Long 	} else if (name)
1259f454e7ebSJohn Baldwin 		bus_describe_intr(dev, res, tag, "%s", name);
12604c7070dbSScott Long 
12614c7070dbSScott Long 	irq->ii_tag = tag;
12624c7070dbSScott Long 	return (0);
12634c7070dbSScott Long }
12644c7070dbSScott Long 
12654c7070dbSScott Long 
12664c7070dbSScott Long /*********************************************************************
12674c7070dbSScott Long  *
12684c7070dbSScott Long  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
12694c7070dbSScott Long  *  the information needed to transmit a packet on the wire. This is
12704c7070dbSScott Long  *  called only once at attach, setup is done every reset.
12714c7070dbSScott Long  *
12724c7070dbSScott Long  **********************************************************************/
12734c7070dbSScott Long 
12744c7070dbSScott Long static int
12754c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq)
12764c7070dbSScott Long {
12774c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
12784c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
12794c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
12804c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
12814c7070dbSScott Long 	int err, nsegments, ntsosegments;
12824c7070dbSScott Long 
12834c7070dbSScott Long 	nsegments = scctx->isc_tx_nsegments;
12844c7070dbSScott Long 	ntsosegments = scctx->isc_tx_tso_segments_max;
128523ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[0] > 0);
128623ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
12874c7070dbSScott Long 	MPASS(nsegments > 0);
12884c7070dbSScott Long 	MPASS(ntsosegments > 0);
12894c7070dbSScott Long 	/*
12904c7070dbSScott Long 	 * Setup DMA descriptor areas.
12914c7070dbSScott Long 	 */
12924c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
12934c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
12944c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
12954c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
12964c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
12974c7070dbSScott Long 			       sctx->isc_tx_maxsize,		/* maxsize */
12984c7070dbSScott Long 			       nsegments,	/* nsegments */
12994c7070dbSScott Long 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
13004c7070dbSScott Long 			       0,			/* flags */
13014c7070dbSScott Long 			       NULL,			/* lockfunc */
13024c7070dbSScott Long 			       NULL,			/* lockfuncarg */
13034c7070dbSScott Long 			       &txq->ift_desc_tag))) {
13044c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
13054c7070dbSScott Long 		device_printf(dev,"maxsize: %zd nsegments: %d maxsegsize: %zd\n",
13064c7070dbSScott Long 					  sctx->isc_tx_maxsize, nsegments, sctx->isc_tx_maxsegsize);
13074c7070dbSScott Long 		goto fail;
13084c7070dbSScott Long 	}
13094c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
13104c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
13114c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
13124c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
13134c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
13144c7070dbSScott Long 			       scctx->isc_tx_tso_size_max,		/* maxsize */
13154c7070dbSScott Long 			       ntsosegments,	/* nsegments */
13164c7070dbSScott Long 			       scctx->isc_tx_tso_segsize_max,	/* maxsegsize */
13174c7070dbSScott Long 			       0,			/* flags */
13184c7070dbSScott Long 			       NULL,			/* lockfunc */
13194c7070dbSScott Long 			       NULL,			/* lockfuncarg */
13204c7070dbSScott Long 			       &txq->ift_tso_desc_tag))) {
13214c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX TSO DMA tag: %d\n", err);
13224c7070dbSScott Long 
13234c7070dbSScott Long 		goto fail;
13244c7070dbSScott Long 	}
13254c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_flags =
13264c7070dbSScott Long 	    (uint8_t *) malloc(sizeof(uint8_t) *
132723ac9029SStephen Hurd 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
13284c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
13294c7070dbSScott Long 		err = ENOMEM;
13304c7070dbSScott Long 		goto fail;
13314c7070dbSScott Long 	}
13324c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_m =
13334c7070dbSScott Long 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
133423ac9029SStephen Hurd 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
13354c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
13364c7070dbSScott Long 		err = ENOMEM;
13374c7070dbSScott Long 		goto fail;
13384c7070dbSScott Long 	}
13394c7070dbSScott Long 
13404c7070dbSScott Long         /* Create the descriptor buffer dma maps */
13414c7070dbSScott Long #if defined(ACPI_DMAR) || (!(defined(__i386__) && !defined(__amd64__)))
13424c7070dbSScott Long 	if ((ctx->ifc_flags & IFC_DMAR) == 0)
13434c7070dbSScott Long 		return (0);
13444c7070dbSScott Long 
13454c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_map =
134623ac9029SStephen Hurd 	    (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
13474c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer map memory\n");
13484c7070dbSScott Long 		err = ENOMEM;
13494c7070dbSScott Long 		goto fail;
13504c7070dbSScott Long 	}
13514c7070dbSScott Long 
135223ac9029SStephen Hurd 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
13534c7070dbSScott Long 		err = bus_dmamap_create(txq->ift_desc_tag, 0, &txq->ift_sds.ifsd_map[i]);
13544c7070dbSScott Long 		if (err != 0) {
13554c7070dbSScott Long 			device_printf(dev, "Unable to create TX DMA map\n");
13564c7070dbSScott Long 			goto fail;
13574c7070dbSScott Long 		}
13584c7070dbSScott Long 	}
13594c7070dbSScott Long #endif
13604c7070dbSScott Long 	return (0);
13614c7070dbSScott Long fail:
13624c7070dbSScott Long 	/* We free all, it handles case where we are in the middle */
13634c7070dbSScott Long 	iflib_tx_structures_free(ctx);
13644c7070dbSScott Long 	return (err);
13654c7070dbSScott Long }
13664c7070dbSScott Long 
13674c7070dbSScott Long static void
13684c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
13694c7070dbSScott Long {
13704c7070dbSScott Long 	bus_dmamap_t map;
13714c7070dbSScott Long 
13724c7070dbSScott Long 	map = NULL;
13734c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL)
13744c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[i];
13754c7070dbSScott Long 	if (map != NULL) {
13764c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag, map);
13774c7070dbSScott Long 		bus_dmamap_destroy(txq->ift_desc_tag, map);
13784c7070dbSScott Long 		txq->ift_sds.ifsd_map[i] = NULL;
13794c7070dbSScott Long 	}
13804c7070dbSScott Long }
13814c7070dbSScott Long 
13824c7070dbSScott Long static void
13834c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq)
13844c7070dbSScott Long {
13854c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
13864c7070dbSScott Long 
138723ac9029SStephen Hurd 	for (int i = 0; i < txq->ift_size; i++)
13884c7070dbSScott Long 		iflib_txsd_destroy(ctx, txq, i);
13894c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
13904c7070dbSScott Long 		free(txq->ift_sds.ifsd_map, M_IFLIB);
13914c7070dbSScott Long 		txq->ift_sds.ifsd_map = NULL;
13924c7070dbSScott Long 	}
13934c7070dbSScott Long 	if (txq->ift_sds.ifsd_m != NULL) {
13944c7070dbSScott Long 		free(txq->ift_sds.ifsd_m, M_IFLIB);
13954c7070dbSScott Long 		txq->ift_sds.ifsd_m = NULL;
13964c7070dbSScott Long 	}
13974c7070dbSScott Long 	if (txq->ift_sds.ifsd_flags != NULL) {
13984c7070dbSScott Long 		free(txq->ift_sds.ifsd_flags, M_IFLIB);
13994c7070dbSScott Long 		txq->ift_sds.ifsd_flags = NULL;
14004c7070dbSScott Long 	}
14014c7070dbSScott Long 	if (txq->ift_desc_tag != NULL) {
14024c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_desc_tag);
14034c7070dbSScott Long 		txq->ift_desc_tag = NULL;
14044c7070dbSScott Long 	}
14054c7070dbSScott Long 	if (txq->ift_tso_desc_tag != NULL) {
14064c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_tso_desc_tag);
14074c7070dbSScott Long 		txq->ift_tso_desc_tag = NULL;
14084c7070dbSScott Long 	}
14094c7070dbSScott Long }
14104c7070dbSScott Long 
14114c7070dbSScott Long static void
14124c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
14134c7070dbSScott Long {
14144c7070dbSScott Long 	struct mbuf **mp;
14154c7070dbSScott Long 
14164c7070dbSScott Long 	mp = &txq->ift_sds.ifsd_m[i];
14174c7070dbSScott Long 	if (*mp == NULL)
14184c7070dbSScott Long 		return;
14194c7070dbSScott Long 
14204c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
14214c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag,
14224c7070dbSScott Long 				txq->ift_sds.ifsd_map[i],
14234c7070dbSScott Long 				BUS_DMASYNC_POSTWRITE);
14244c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag,
14254c7070dbSScott Long 				  txq->ift_sds.ifsd_map[i]);
14264c7070dbSScott Long 	}
142723ac9029SStephen Hurd 	m_free(*mp);
14284c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
14294c7070dbSScott Long 	*mp = NULL;
14304c7070dbSScott Long }
14314c7070dbSScott Long 
14324c7070dbSScott Long static int
14334c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq)
14344c7070dbSScott Long {
14354c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
143623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
14374c7070dbSScott Long 	iflib_dma_info_t di;
14384c7070dbSScott Long 	int i;
14394c7070dbSScott Long 
14404c7070dbSScott Long 	/* Set number of descriptors available */
14414c7070dbSScott Long 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
14424c7070dbSScott Long 
14434c7070dbSScott Long 	/* Reset indices */
14444c7070dbSScott Long 	txq->ift_cidx_processed = txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
144523ac9029SStephen Hurd 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
14464c7070dbSScott Long 
14474c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
14484c7070dbSScott Long 		bzero((void *)di->idi_vaddr, di->idi_size);
14494c7070dbSScott Long 
14504c7070dbSScott Long 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
14514c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
14524c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
14534c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
14544c7070dbSScott Long 	return (0);
14554c7070dbSScott Long }
14564c7070dbSScott Long 
14574c7070dbSScott Long /*********************************************************************
14584c7070dbSScott Long  *
14594c7070dbSScott Long  *  Allocate memory for rx_buffer structures. Since we use one
14604c7070dbSScott Long  *  rx_buffer per received packet, the maximum number of rx_buffer's
14614c7070dbSScott Long  *  that we'll need is equal to the number of receive descriptors
14624c7070dbSScott Long  *  that we've allocated.
14634c7070dbSScott Long  *
14644c7070dbSScott Long  **********************************************************************/
14654c7070dbSScott Long static int
14664c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq)
14674c7070dbSScott Long {
14684c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
14694c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
147023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
14714c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
14724c7070dbSScott Long 	iflib_fl_t fl;
14734c7070dbSScott Long 	int			err;
14744c7070dbSScott Long 
147523ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[0] > 0);
147623ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
14774c7070dbSScott Long 
14784c7070dbSScott Long 	fl = rxq->ifr_fl;
14794c7070dbSScott Long 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
148023ac9029SStephen Hurd 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
14814c7070dbSScott Long 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
14824c7070dbSScott Long 					 1, 0,			/* alignment, bounds */
14834c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* lowaddr */
14844c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* highaddr */
14854c7070dbSScott Long 					 NULL, NULL,		/* filter, filterarg */
14864c7070dbSScott Long 					 sctx->isc_rx_maxsize,	/* maxsize */
14874c7070dbSScott Long 					 sctx->isc_rx_nsegments,	/* nsegments */
14884c7070dbSScott Long 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
14894c7070dbSScott Long 					 0,			/* flags */
14904c7070dbSScott Long 					 NULL,			/* lockfunc */
14914c7070dbSScott Long 					 NULL,			/* lockarg */
14924c7070dbSScott Long 					 &fl->ifl_desc_tag);
14934c7070dbSScott Long 		if (err) {
14944c7070dbSScott Long 			device_printf(dev, "%s: bus_dma_tag_create failed %d\n",
14954c7070dbSScott Long 				__func__, err);
14964c7070dbSScott Long 			goto fail;
14974c7070dbSScott Long 		}
1498e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_flags =
1499e035717eSSean Bruno 		      (uint8_t *) malloc(sizeof(uint8_t) *
1500e035717eSSean Bruno 					 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1501e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1502e035717eSSean Bruno 			err = ENOMEM;
1503e035717eSSean Bruno 			goto fail;
1504e035717eSSean Bruno 		}
1505e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_m =
1506e035717eSSean Bruno 		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
1507e035717eSSean Bruno 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1508e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1509e035717eSSean Bruno 			err = ENOMEM;
1510e035717eSSean Bruno 			goto fail;
1511e035717eSSean Bruno 		}
1512e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_cl =
1513e035717eSSean Bruno 		      (caddr_t *) malloc(sizeof(caddr_t) *
1514e035717eSSean Bruno 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1515e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1516e035717eSSean Bruno 			err = ENOMEM;
1517e035717eSSean Bruno 			goto fail;
1518e035717eSSean Bruno 		}
15194c7070dbSScott Long 
1520e035717eSSean Bruno 		/* Create the descriptor buffer dma maps */
1521e035717eSSean Bruno #if defined(ACPI_DMAR) || (!(defined(__i386__) && !defined(__amd64__)))
1522e035717eSSean Bruno 		if ((ctx->ifc_flags & IFC_DMAR) == 0)
1523e035717eSSean Bruno 			continue;
1524e035717eSSean Bruno 
1525e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_map =
1526e035717eSSean Bruno 		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1527e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer map memory\n");
1528e035717eSSean Bruno 			err = ENOMEM;
1529e035717eSSean Bruno 			goto fail;
1530e035717eSSean Bruno 		}
1531e035717eSSean Bruno 
1532e035717eSSean Bruno 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1533e035717eSSean Bruno 			err = bus_dmamap_create(fl->ifl_desc_tag, 0, &fl->ifl_sds.ifsd_map[i]);
1534e035717eSSean Bruno 			if (err != 0) {
1535e035717eSSean Bruno 				device_printf(dev, "Unable to create TX DMA map\n");
15364c7070dbSScott Long 				goto fail;
15374c7070dbSScott Long 			}
15384c7070dbSScott Long 		}
1539e035717eSSean Bruno #endif
1540*835809f9SSean Bruno 	}
15414c7070dbSScott Long 	return (0);
15424c7070dbSScott Long 
15434c7070dbSScott Long fail:
15444c7070dbSScott Long 	iflib_rx_structures_free(ctx);
15454c7070dbSScott Long 	return (err);
15464c7070dbSScott Long }
15474c7070dbSScott Long 
15484c7070dbSScott Long 
15494c7070dbSScott Long /*
15504c7070dbSScott Long  * Internal service routines
15514c7070dbSScott Long  */
15524c7070dbSScott Long 
15534c7070dbSScott Long struct rxq_refill_cb_arg {
15544c7070dbSScott Long 	int               error;
15554c7070dbSScott Long 	bus_dma_segment_t seg;
15564c7070dbSScott Long 	int               nseg;
15574c7070dbSScott Long };
15584c7070dbSScott Long 
15594c7070dbSScott Long static void
15604c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
15614c7070dbSScott Long {
15624c7070dbSScott Long 	struct rxq_refill_cb_arg *cb_arg = arg;
15634c7070dbSScott Long 
15644c7070dbSScott Long 	cb_arg->error = error;
15654c7070dbSScott Long 	cb_arg->seg = segs[0];
15664c7070dbSScott Long 	cb_arg->nseg = nseg;
15674c7070dbSScott Long }
15684c7070dbSScott Long 
15694c7070dbSScott Long 
15704c7070dbSScott Long #ifdef ACPI_DMAR
15714c7070dbSScott Long #define IS_DMAR(ctx) (ctx->ifc_flags & IFC_DMAR)
15724c7070dbSScott Long #else
15734c7070dbSScott Long #define IS_DMAR(ctx) (0)
15744c7070dbSScott Long #endif
15754c7070dbSScott Long 
15764c7070dbSScott Long /**
15774c7070dbSScott Long  *	rxq_refill - refill an rxq  free-buffer list
15784c7070dbSScott Long  *	@ctx: the iflib context
15794c7070dbSScott Long  *	@rxq: the free-list to refill
15804c7070dbSScott Long  *	@n: the number of new buffers to allocate
15814c7070dbSScott Long  *
15824c7070dbSScott Long  *	(Re)populate an rxq free-buffer list with up to @n new packet buffers.
15834c7070dbSScott Long  *	The caller must assure that @n does not exceed the queue's capacity.
15844c7070dbSScott Long  */
15854c7070dbSScott Long static void
15864c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
15874c7070dbSScott Long {
15884c7070dbSScott Long 	struct mbuf *m;
1589e035717eSSean Bruno 	int idx, pidx = fl->ifl_pidx;
1590e035717eSSean Bruno 	caddr_t cl, *sd_cl;
1591e035717eSSean Bruno 	struct mbuf **sd_m;
1592e035717eSSean Bruno 	uint8_t *sd_flags;
1593e035717eSSean Bruno 	bus_dmamap_t *sd_map;
15944c7070dbSScott Long 	int n, i = 0;
15954c7070dbSScott Long 	uint64_t bus_addr;
15964c7070dbSScott Long 	int err;
15974c7070dbSScott Long 
1598e035717eSSean Bruno 	sd_m = fl->ifl_sds.ifsd_m;
1599e035717eSSean Bruno 	sd_map = fl->ifl_sds.ifsd_map;
1600e035717eSSean Bruno 	sd_cl = fl->ifl_sds.ifsd_cl;
1601e035717eSSean Bruno 	sd_flags = fl->ifl_sds.ifsd_flags;
1602e035717eSSean Bruno 	idx = pidx;
1603e035717eSSean Bruno 
16044c7070dbSScott Long 	n  = count;
16054c7070dbSScott Long 	MPASS(n > 0);
16064c7070dbSScott Long 	MPASS(fl->ifl_credits + n <= fl->ifl_size);
16074c7070dbSScott Long 
16084c7070dbSScott Long 	if (pidx < fl->ifl_cidx)
16094c7070dbSScott Long 		MPASS(pidx + n <= fl->ifl_cidx);
16104c7070dbSScott Long 	if (pidx == fl->ifl_cidx && (fl->ifl_credits < fl->ifl_size))
16114c7070dbSScott Long 		MPASS(fl->ifl_gen == 0);
16124c7070dbSScott Long 	if (pidx > fl->ifl_cidx)
16134c7070dbSScott Long 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
16144c7070dbSScott Long 
16154c7070dbSScott Long 	DBG_COUNTER_INC(fl_refills);
16164c7070dbSScott Long 	if (n > 8)
16174c7070dbSScott Long 		DBG_COUNTER_INC(fl_refills_large);
16184c7070dbSScott Long 
16194c7070dbSScott Long 	while (n--) {
16204c7070dbSScott Long 		/*
16214c7070dbSScott Long 		 * We allocate an uninitialized mbuf + cluster, mbuf is
16224c7070dbSScott Long 		 * initialized after rx.
16234c7070dbSScott Long 		 *
16244c7070dbSScott Long 		 * If the cluster is still set then we know a minimum sized packet was received
16254c7070dbSScott Long 		 */
1626e035717eSSean Bruno 		if ((cl = sd_cl[idx]) == NULL) {
1627e035717eSSean Bruno 			if ((cl = sd_cl[idx] = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
16284c7070dbSScott Long 				break;
16294c7070dbSScott Long #if MEMORY_LOGGING
16304c7070dbSScott Long 			fl->ifl_cl_enqueued++;
16314c7070dbSScott Long #endif
16324c7070dbSScott Long 		}
16334c7070dbSScott Long 		if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
16344c7070dbSScott Long 			break;
16354c7070dbSScott Long 		}
16364c7070dbSScott Long #if MEMORY_LOGGING
16374c7070dbSScott Long 		fl->ifl_m_enqueued++;
16384c7070dbSScott Long #endif
16394c7070dbSScott Long 
16404c7070dbSScott Long 		DBG_COUNTER_INC(rx_allocs);
16414c7070dbSScott Long #ifdef notyet
1642e035717eSSean Bruno 		if ((sd_flags[pidx] & RX_SW_DESC_MAP_CREATED) == 0) {
16434c7070dbSScott Long 			int err;
16444c7070dbSScott Long 
1645e035717eSSean Bruno 			if ((err = bus_dmamap_create(fl->ifl_ifdi->idi_tag, 0, &sd_map[idx]))) {
16464c7070dbSScott Long 				log(LOG_WARNING, "bus_dmamap_create failed %d\n", err);
16474c7070dbSScott Long 				uma_zfree(fl->ifl_zone, cl);
16484c7070dbSScott Long 				n = 0;
16494c7070dbSScott Long 				goto done;
16504c7070dbSScott Long 			}
1651e035717eSSean Bruno 			sd_flags[idx] |= RX_SW_DESC_MAP_CREATED;
16524c7070dbSScott Long 		}
16534c7070dbSScott Long #endif
16544c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
16554c7070dbSScott Long 		if (!IS_DMAR(ctx)) {
16564c7070dbSScott Long 			bus_addr = pmap_kextract((vm_offset_t)cl);
16574c7070dbSScott Long 		} else
16584c7070dbSScott Long #endif
16594c7070dbSScott Long 		{
16604c7070dbSScott Long 			struct rxq_refill_cb_arg cb_arg;
16614c7070dbSScott Long 			iflib_rxq_t q;
16624c7070dbSScott Long 
16634c7070dbSScott Long 			cb_arg.error = 0;
16644c7070dbSScott Long 			q = fl->ifl_rxq;
1665e035717eSSean Bruno 			err = bus_dmamap_load(fl->ifl_desc_tag, sd_map[idx],
16664c7070dbSScott Long 		         cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 0);
16674c7070dbSScott Long 
16684c7070dbSScott Long 			if (err != 0 || cb_arg.error) {
16694c7070dbSScott Long 				/*
16704c7070dbSScott Long 				 * !zone_pack ?
16714c7070dbSScott Long 				 */
16724c7070dbSScott Long 				if (fl->ifl_zone == zone_pack)
16734c7070dbSScott Long 					uma_zfree(fl->ifl_zone, cl);
16744c7070dbSScott Long 				m_free(m);
16754c7070dbSScott Long 				n = 0;
16764c7070dbSScott Long 				goto done;
16774c7070dbSScott Long 			}
16784c7070dbSScott Long 			bus_addr = cb_arg.seg.ds_addr;
16794c7070dbSScott Long 		}
1680e035717eSSean Bruno 		sd_flags[idx] |= RX_SW_DESC_INUSE;
16814c7070dbSScott Long 
1682e035717eSSean Bruno 		MPASS(sd_m[idx] == NULL);
1683e035717eSSean Bruno 		sd_cl[idx] = cl;
1684e035717eSSean Bruno 		sd_m[idx] = m;
16854c7070dbSScott Long 		fl->ifl_bus_addrs[i] = bus_addr;
16864c7070dbSScott Long 		fl->ifl_vm_addrs[i] = cl;
16874c7070dbSScott Long 		fl->ifl_credits++;
16884c7070dbSScott Long 		i++;
16894c7070dbSScott Long 		MPASS(fl->ifl_credits <= fl->ifl_size);
1690e035717eSSean Bruno 		if (++idx == fl->ifl_size) {
16914c7070dbSScott Long 			fl->ifl_gen = 1;
1692e035717eSSean Bruno 			idx = 0;
16934c7070dbSScott Long 		}
16944c7070dbSScott Long 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
16954c7070dbSScott Long 			ctx->isc_rxd_refill(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx,
169623ac9029SStephen Hurd 								 fl->ifl_bus_addrs, fl->ifl_vm_addrs, i, fl->ifl_buf_size);
16974c7070dbSScott Long 			i = 0;
1698e035717eSSean Bruno 			pidx = idx;
16994c7070dbSScott Long 		}
1700e035717eSSean Bruno 		fl->ifl_pidx = idx;
1701e035717eSSean Bruno 
17024c7070dbSScott Long 	}
17034c7070dbSScott Long done:
17044c7070dbSScott Long 	DBG_COUNTER_INC(rxd_flush);
17054c7070dbSScott Long 	if (fl->ifl_pidx == 0)
17064c7070dbSScott Long 		pidx = fl->ifl_size - 1;
17074c7070dbSScott Long 	else
17084c7070dbSScott Long 		pidx = fl->ifl_pidx - 1;
17094c7070dbSScott Long 	ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
17104c7070dbSScott Long }
17114c7070dbSScott Long 
17124c7070dbSScott Long static __inline void
17134c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max)
17144c7070dbSScott Long {
17154c7070dbSScott Long 	/* we avoid allowing pidx to catch up with cidx as it confuses ixl */
17164c7070dbSScott Long 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
17174c7070dbSScott Long #ifdef INVARIANTS
17184c7070dbSScott Long 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
17194c7070dbSScott Long #endif
17204c7070dbSScott Long 
17214c7070dbSScott Long 	MPASS(fl->ifl_credits <= fl->ifl_size);
17224c7070dbSScott Long 	MPASS(reclaimable == delta);
17234c7070dbSScott Long 
17244c7070dbSScott Long 	if (reclaimable > 0)
17254c7070dbSScott Long 		_iflib_fl_refill(ctx, fl, min(max, reclaimable));
17264c7070dbSScott Long }
17274c7070dbSScott Long 
17284c7070dbSScott Long static void
17294c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl)
17304c7070dbSScott Long {
17314c7070dbSScott Long 	iflib_dma_info_t idi = fl->ifl_ifdi;
17324c7070dbSScott Long 	uint32_t i;
17334c7070dbSScott Long 
17344c7070dbSScott Long 	for (i = 0; i < fl->ifl_size; i++) {
1735e035717eSSean Bruno 		struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
1736e035717eSSean Bruno 		uint8_t *sd_flags = &fl->ifl_sds.ifsd_flags[i];
1737e035717eSSean Bruno 		caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
17384c7070dbSScott Long 
1739e035717eSSean Bruno 		if (*sd_flags & RX_SW_DESC_INUSE) {
1740e035717eSSean Bruno 			if (fl->ifl_sds.ifsd_map != NULL) {
1741e035717eSSean Bruno 				bus_dmamap_t sd_map = fl->ifl_sds.ifsd_map[i];
1742e035717eSSean Bruno 				bus_dmamap_unload(fl->ifl_desc_tag, sd_map);
1743e035717eSSean Bruno 				bus_dmamap_destroy(fl->ifl_desc_tag, sd_map);
17444c7070dbSScott Long 			}
1745e035717eSSean Bruno 			if (*sd_m != NULL) {
1746e035717eSSean Bruno 				m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
1747e035717eSSean Bruno 				uma_zfree(zone_mbuf, *sd_m);
1748e035717eSSean Bruno 			}
1749e035717eSSean Bruno 			if (*sd_cl != NULL)
1750e035717eSSean Bruno 				uma_zfree(fl->ifl_zone, *sd_cl);
1751e035717eSSean Bruno 			*sd_flags = 0;
17524c7070dbSScott Long 		} else {
1753e035717eSSean Bruno 			MPASS(*sd_cl == NULL);
1754e035717eSSean Bruno 			MPASS(*sd_m == NULL);
17554c7070dbSScott Long 		}
17564c7070dbSScott Long #if MEMORY_LOGGING
17574c7070dbSScott Long 		fl->ifl_m_dequeued++;
17584c7070dbSScott Long 		fl->ifl_cl_dequeued++;
17594c7070dbSScott Long #endif
1760e035717eSSean Bruno 		*sd_cl = NULL;
1761e035717eSSean Bruno 		*sd_m = NULL;
17624c7070dbSScott Long 	}
17634c7070dbSScott Long 	/*
17644c7070dbSScott Long 	 * Reset free list values
17654c7070dbSScott Long 	 */
17664c7070dbSScott Long 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = 0;;
17674c7070dbSScott Long 	bzero(idi->idi_vaddr, idi->idi_size);
17684c7070dbSScott Long }
17694c7070dbSScott Long 
17704c7070dbSScott Long /*********************************************************************
17714c7070dbSScott Long  *
17724c7070dbSScott Long  *  Initialize a receive ring and its buffers.
17734c7070dbSScott Long  *
17744c7070dbSScott Long  **********************************************************************/
17754c7070dbSScott Long static int
17764c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl)
17774c7070dbSScott Long {
17784c7070dbSScott Long 	iflib_rxq_t rxq = fl->ifl_rxq;
17794c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
17804c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
17814c7070dbSScott Long 
17824c7070dbSScott Long 	/*
17834c7070dbSScott Long 	** Free current RX buffer structs and their mbufs
17844c7070dbSScott Long 	*/
17854c7070dbSScott Long 	iflib_fl_bufs_free(fl);
17864c7070dbSScott Long 	/* Now replenish the mbufs */
17874c7070dbSScott Long 	MPASS(fl->ifl_credits == 0);
17884c7070dbSScott Long 	/*
17894c7070dbSScott Long 	 * XXX don't set the max_frame_size to larger
17904c7070dbSScott Long 	 * than the hardware can handle
17914c7070dbSScott Long 	 */
17924c7070dbSScott Long 	if (sctx->isc_max_frame_size <= 2048)
17934c7070dbSScott Long 		fl->ifl_buf_size = MCLBYTES;
17944c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 4096)
17954c7070dbSScott Long 		fl->ifl_buf_size = MJUMPAGESIZE;
17964c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 9216)
17974c7070dbSScott Long 		fl->ifl_buf_size = MJUM9BYTES;
17984c7070dbSScott Long 	else
17994c7070dbSScott Long 		fl->ifl_buf_size = MJUM16BYTES;
18004c7070dbSScott Long 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
18014c7070dbSScott Long 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
18024c7070dbSScott Long 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
18034c7070dbSScott Long 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
18044c7070dbSScott Long 
18054c7070dbSScott Long 
18064c7070dbSScott Long 	/* avoid pre-allocating zillions of clusters to an idle card
18074c7070dbSScott Long 	 * potentially speeding up attach
18084c7070dbSScott Long 	 */
18094c7070dbSScott Long 	_iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
18104c7070dbSScott Long 	MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
18114c7070dbSScott Long 	if (min(128, fl->ifl_size) != fl->ifl_credits)
18124c7070dbSScott Long 		return (ENOBUFS);
18134c7070dbSScott Long 	/*
18144c7070dbSScott Long 	 * handle failure
18154c7070dbSScott Long 	 */
18164c7070dbSScott Long 	MPASS(rxq != NULL);
18174c7070dbSScott Long 	MPASS(fl->ifl_ifdi != NULL);
18184c7070dbSScott Long 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
18194c7070dbSScott Long 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
18204c7070dbSScott Long 	return (0);
18214c7070dbSScott Long }
18224c7070dbSScott Long 
18234c7070dbSScott Long /*********************************************************************
18244c7070dbSScott Long  *
18254c7070dbSScott Long  *  Free receive ring data structures
18264c7070dbSScott Long  *
18274c7070dbSScott Long  **********************************************************************/
18284c7070dbSScott Long static void
18294c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq)
18304c7070dbSScott Long {
18314c7070dbSScott Long 	iflib_fl_t fl;
18324c7070dbSScott Long 	int i;
18334c7070dbSScott Long 
18344c7070dbSScott Long 	if (rxq->ifr_fl != NULL) {
18354c7070dbSScott Long 		for (i = 0; i < rxq->ifr_nfl; i++) {
18364c7070dbSScott Long 			fl = &rxq->ifr_fl[i];
18374c7070dbSScott Long 			if (fl->ifl_desc_tag != NULL) {
18384c7070dbSScott Long 				bus_dma_tag_destroy(fl->ifl_desc_tag);
18394c7070dbSScott Long 				fl->ifl_desc_tag = NULL;
18404c7070dbSScott Long 			}
1841e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_m, M_IFLIB);
1842e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_cl, M_IFLIB);
1843e035717eSSean Bruno 			/* XXX destroy maps first */
1844e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_map, M_IFLIB);
1845e035717eSSean Bruno 			fl->ifl_sds.ifsd_m = NULL;
1846e035717eSSean Bruno 			fl->ifl_sds.ifsd_cl = NULL;
1847e035717eSSean Bruno 			fl->ifl_sds.ifsd_map = NULL;
18484c7070dbSScott Long 		}
18494c7070dbSScott Long 		free(rxq->ifr_fl, M_IFLIB);
18504c7070dbSScott Long 		rxq->ifr_fl = NULL;
18514c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
18524c7070dbSScott Long 	}
18534c7070dbSScott Long }
18544c7070dbSScott Long 
18554c7070dbSScott Long /*
18564c7070dbSScott Long  * MI independent logic
18574c7070dbSScott Long  *
18584c7070dbSScott Long  */
18594c7070dbSScott Long static void
18604c7070dbSScott Long iflib_timer(void *arg)
18614c7070dbSScott Long {
18624c7070dbSScott Long 	iflib_txq_t txq = arg;
18634c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
18644c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
18654c7070dbSScott Long 
18664c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
18674c7070dbSScott Long 		return;
18684c7070dbSScott Long 	/*
18694c7070dbSScott Long 	** Check on the state of the TX queue(s), this
18704c7070dbSScott Long 	** can be done without the lock because its RO
18714c7070dbSScott Long 	** and the HUNG state will be static if set.
18724c7070dbSScott Long 	*/
18734c7070dbSScott Long 	IFDI_TIMER(ctx, txq->ift_id);
18744c7070dbSScott Long 	if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
18754c7070dbSScott Long 		(ctx->ifc_pause_frames == 0))
18764c7070dbSScott Long 		goto hung;
18774c7070dbSScott Long 
18784c7070dbSScott Long 	if (TXQ_AVAIL(txq) <= 2*scctx->isc_tx_nsegments ||
18794c7070dbSScott Long 	    ifmp_ring_is_stalled(txq->ift_br[0]))
18804c7070dbSScott Long 		GROUPTASK_ENQUEUE(&txq->ift_task);
18814c7070dbSScott Long 
18824c7070dbSScott Long 	ctx->ifc_pause_frames = 0;
18834c7070dbSScott Long 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
18844c7070dbSScott Long 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
18854c7070dbSScott Long 	return;
18864c7070dbSScott Long hung:
18874c7070dbSScott Long 	CTX_LOCK(ctx);
18884c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, 0, IFF_DRV_RUNNING);
18894c7070dbSScott Long 	device_printf(ctx->ifc_dev,  "TX(%d) desc avail = %d, pidx = %d\n",
18904c7070dbSScott Long 				  txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
18914c7070dbSScott Long 
18924c7070dbSScott Long 	IFDI_WATCHDOG_RESET(ctx);
18934c7070dbSScott Long 	ctx->ifc_watchdog_events++;
18944c7070dbSScott Long 	ctx->ifc_pause_frames = 0;
18954c7070dbSScott Long 
18964c7070dbSScott Long 	iflib_init_locked(ctx);
18974c7070dbSScott Long 	CTX_UNLOCK(ctx);
18984c7070dbSScott Long }
18994c7070dbSScott Long 
19004c7070dbSScott Long static void
19014c7070dbSScott Long iflib_init_locked(if_ctx_t ctx)
19024c7070dbSScott Long {
19034c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
19041248952aSSean Bruno 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
19054c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
19064c7070dbSScott Long 	iflib_fl_t fl;
19074c7070dbSScott Long 	iflib_txq_t txq;
19084c7070dbSScott Long 	iflib_rxq_t rxq;
19091248952aSSean Bruno 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
19104c7070dbSScott Long 
19114c7070dbSScott Long 
19124c7070dbSScott Long 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
19134c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
19144c7070dbSScott Long 
19151248952aSSean Bruno 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
19161248952aSSean Bruno 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
19174c7070dbSScott Long 	/* Set hardware offload abilities */
19184c7070dbSScott Long 	if_clearhwassist(ifp);
19194c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
19201248952aSSean Bruno 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
19214c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
19221248952aSSean Bruno 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
19234c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO4)
19244c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
19254c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO6)
19264c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
19274c7070dbSScott Long 
19284c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
19294c7070dbSScott Long 		CALLOUT_LOCK(txq);
19304c7070dbSScott Long 		callout_stop(&txq->ift_timer);
19314c7070dbSScott Long 		callout_stop(&txq->ift_db_check);
19324c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
19334c7070dbSScott Long 		iflib_netmap_txq_init(ctx, txq);
19344c7070dbSScott Long 	}
19354c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
19364c7070dbSScott Long 		iflib_netmap_rxq_init(ctx, rxq);
19374c7070dbSScott Long 	}
193823ac9029SStephen Hurd #ifdef INVARIANTS
193923ac9029SStephen Hurd 	i = if_getdrvflags(ifp);
194023ac9029SStephen Hurd #endif
19414c7070dbSScott Long 	IFDI_INIT(ctx);
194223ac9029SStephen Hurd 	MPASS(if_getdrvflags(ifp) == i);
19434c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
19444c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
19454c7070dbSScott Long 			if (iflib_fl_setup(fl)) {
19464c7070dbSScott Long 				device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n");
19474c7070dbSScott Long 				goto done;
19484c7070dbSScott Long 			}
19494c7070dbSScott Long 		}
19504c7070dbSScott Long 	}
19514c7070dbSScott Long 	done:
19524c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
19534c7070dbSScott Long 	IFDI_INTR_ENABLE(ctx);
19544c7070dbSScott Long 	txq = ctx->ifc_txqs;
19554c7070dbSScott Long 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
19564c7070dbSScott Long 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
19574c7070dbSScott Long 			txq->ift_timer.c_cpu);
19584c7070dbSScott Long }
19594c7070dbSScott Long 
19604c7070dbSScott Long static int
19614c7070dbSScott Long iflib_media_change(if_t ifp)
19624c7070dbSScott Long {
19634c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
19644c7070dbSScott Long 	int err;
19654c7070dbSScott Long 
19664c7070dbSScott Long 	CTX_LOCK(ctx);
19674c7070dbSScott Long 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
19684c7070dbSScott Long 		iflib_init_locked(ctx);
19694c7070dbSScott Long 	CTX_UNLOCK(ctx);
19704c7070dbSScott Long 	return (err);
19714c7070dbSScott Long }
19724c7070dbSScott Long 
19734c7070dbSScott Long static void
19744c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
19754c7070dbSScott Long {
19764c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
19774c7070dbSScott Long 
19784c7070dbSScott Long 	CTX_LOCK(ctx);
19794c7070dbSScott Long 	IFDI_UPDATE_ADMIN_STATUS(ctx);
19804c7070dbSScott Long 	IFDI_MEDIA_STATUS(ctx, ifmr);
19814c7070dbSScott Long 	CTX_UNLOCK(ctx);
19824c7070dbSScott Long }
19834c7070dbSScott Long 
19844c7070dbSScott Long static void
19854c7070dbSScott Long iflib_stop(if_ctx_t ctx)
19864c7070dbSScott Long {
19874c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
19884c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
19894c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
19904c7070dbSScott Long 	iflib_dma_info_t di;
19914c7070dbSScott Long 	iflib_fl_t fl;
19924c7070dbSScott Long 	int i, j;
19934c7070dbSScott Long 
19944c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
19954c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
19964c7070dbSScott Long 
19974c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
1998da69b8f9SSean Bruno 	DELAY(100000);
1999da69b8f9SSean Bruno 	IFDI_STOP(ctx);
2000da69b8f9SSean Bruno 	DELAY(100000);
20014c7070dbSScott Long 
2002da69b8f9SSean Bruno 	iflib_debug_reset();
20034c7070dbSScott Long 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
20044c7070dbSScott Long 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
20054c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
20064c7070dbSScott Long 
20074c7070dbSScott Long 		/* clean any enqueued buffers */
2008da69b8f9SSean Bruno 		iflib_ifmp_purge(txq);
20094c7070dbSScott Long 		/* Free any existing tx buffers. */
201023ac9029SStephen Hurd 		for (j = 0; j < txq->ift_size; j++) {
20114c7070dbSScott Long 			iflib_txsd_free(ctx, txq, j);
20124c7070dbSScott Long 		}
20134c7070dbSScott Long 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2014da69b8f9SSean Bruno 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
20154c7070dbSScott Long 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
20164c7070dbSScott Long 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
20174c7070dbSScott Long 		txq->ift_pullups = 0;
20184c7070dbSScott Long 		ifmp_ring_reset_stats(txq->ift_br[0]);
20194c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwtxqs; j++, di++)
20204c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
20214c7070dbSScott Long 	}
20224c7070dbSScott Long 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
20234c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
20244c7070dbSScott Long 
20254c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwrxqs; j++, di++)
20264c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
20274c7070dbSScott Long 		/* also resets the free lists pidx/cidx */
20284c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
20294c7070dbSScott Long 			iflib_fl_bufs_free(fl);
20304c7070dbSScott Long 	}
20314c7070dbSScott Long }
20324c7070dbSScott Long 
2033e035717eSSean Bruno static inline void
2034e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx)
2035e035717eSSean Bruno {
2036e035717eSSean Bruno 	int nextptr;
2037e035717eSSean Bruno 	int nrxd = fl->ifl_size;
2038e035717eSSean Bruno 
2039e035717eSSean Bruno 	nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2040e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2041e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
2042e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2043e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2044e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2045e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2046e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2047e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2048e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2049e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2050e035717eSSean Bruno }
2051e035717eSSean Bruno 
2052e035717eSSean Bruno static void
2053e035717eSSean Bruno rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int *cltype, int unload, iflib_fl_t *pfl, int *pcidx)
20544c7070dbSScott Long {
20554c7070dbSScott Long 	int flid, cidx;
2056e035717eSSean Bruno 	bus_dmamap_t map;
20574c7070dbSScott Long 	iflib_fl_t fl;
20584c7070dbSScott Long 	iflib_dma_info_t di;
2059e035717eSSean Bruno 	int next;
20604c7070dbSScott Long 
20614c7070dbSScott Long 	flid = irf->irf_flid;
20624c7070dbSScott Long 	cidx = irf->irf_idx;
20634c7070dbSScott Long 	fl = &rxq->ifr_fl[flid];
20644c7070dbSScott Long 	fl->ifl_credits--;
20654c7070dbSScott Long #if MEMORY_LOGGING
20664c7070dbSScott Long 	fl->ifl_m_dequeued++;
20674c7070dbSScott Long 	if (cltype)
20684c7070dbSScott Long 		fl->ifl_cl_dequeued++;
20694c7070dbSScott Long #endif
2070e035717eSSean Bruno 	prefetch_pkts(fl, cidx);
2071e035717eSSean Bruno 	if (fl->ifl_sds.ifsd_map != NULL) {
2072e035717eSSean Bruno 		next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2073e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_map[next]);
2074e035717eSSean Bruno 		map = fl->ifl_sds.ifsd_map[cidx];
20754c7070dbSScott Long 		di = fl->ifl_ifdi;
2076e035717eSSean Bruno 		next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1);
2077e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_flags[next]);
20784c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
20794c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
20804c7070dbSScott Long 
20814c7070dbSScott Long 	/* not valid assert if bxe really does SGE from non-contiguous elements */
20824c7070dbSScott Long 		MPASS(fl->ifl_cidx == cidx);
20834c7070dbSScott Long 		if (unload)
2084e035717eSSean Bruno 			bus_dmamap_unload(fl->ifl_desc_tag, map);
2085e035717eSSean Bruno 	}
20864c7070dbSScott Long 	if (__predict_false(++fl->ifl_cidx == fl->ifl_size)) {
20874c7070dbSScott Long 		fl->ifl_cidx = 0;
20884c7070dbSScott Long 		fl->ifl_gen = 0;
20894c7070dbSScott Long 	}
20904c7070dbSScott Long 	/* YES ick */
20914c7070dbSScott Long 	if (cltype)
20924c7070dbSScott Long 		*cltype = fl->ifl_cltype;
2093e035717eSSean Bruno 	*pfl = fl;
2094e035717eSSean Bruno 	*pcidx = cidx;
20954c7070dbSScott Long }
20964c7070dbSScott Long 
20974c7070dbSScott Long static struct mbuf *
20984c7070dbSScott Long assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri)
20994c7070dbSScott Long {
21004c7070dbSScott Long 	int i, padlen , flags, cltype;
2101e035717eSSean Bruno 	struct mbuf *m, *mh, *mt, *sd_m;
2102e035717eSSean Bruno 	iflib_fl_t fl;
2103e035717eSSean Bruno 	int cidx;
2104e035717eSSean Bruno 	caddr_t cl, sd_cl;
21054c7070dbSScott Long 
21064c7070dbSScott Long 	i = 0;
210723ac9029SStephen Hurd 	mh = NULL;
21084c7070dbSScott Long 	do {
2109e035717eSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[i], &cltype, TRUE, &fl, &cidx);
2110e035717eSSean Bruno 		sd_m = fl->ifl_sds.ifsd_m[cidx];
2111e035717eSSean Bruno 		sd_cl = fl->ifl_sds.ifsd_cl[cidx];
21124c7070dbSScott Long 
2113e035717eSSean Bruno 		MPASS(sd_cl != NULL);
2114e035717eSSean Bruno 		MPASS(sd_m != NULL);
211523ac9029SStephen Hurd 
211623ac9029SStephen Hurd 		/* Don't include zero-length frags */
211723ac9029SStephen Hurd 		if (ri->iri_frags[i].irf_len == 0) {
211823ac9029SStephen Hurd 			/* XXX we can save the cluster here, but not the mbuf */
2119e035717eSSean Bruno 			m_init(sd_m, M_NOWAIT, MT_DATA, 0);
2120e035717eSSean Bruno 			m_free(sd_m);
2121e035717eSSean Bruno 			fl->ifl_sds.ifsd_m[cidx] = NULL;
212223ac9029SStephen Hurd 			continue;
212323ac9029SStephen Hurd 		}
2124e035717eSSean Bruno 		m = sd_m;
212523ac9029SStephen Hurd 		if (mh == NULL) {
21264c7070dbSScott Long 			flags = M_PKTHDR|M_EXT;
21274c7070dbSScott Long 			mh = mt = m;
21284c7070dbSScott Long 			padlen = ri->iri_pad;
21294c7070dbSScott Long 		} else {
21304c7070dbSScott Long 			flags = M_EXT;
21314c7070dbSScott Long 			mt->m_next = m;
21324c7070dbSScott Long 			mt = m;
21334c7070dbSScott Long 			/* assuming padding is only on the first fragment */
21344c7070dbSScott Long 			padlen = 0;
21354c7070dbSScott Long 		}
2136e035717eSSean Bruno 		fl->ifl_sds.ifsd_m[cidx] = NULL;
2137e035717eSSean Bruno 		cl = fl->ifl_sds.ifsd_cl[cidx];
2138e035717eSSean Bruno 		fl->ifl_sds.ifsd_cl[cidx] = NULL;
21394c7070dbSScott Long 
21404c7070dbSScott Long 		/* Can these two be made one ? */
21414c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, flags);
21424c7070dbSScott Long 		m_cljset(m, cl, cltype);
21434c7070dbSScott Long 		/*
21444c7070dbSScott Long 		 * These must follow m_init and m_cljset
21454c7070dbSScott Long 		 */
21464c7070dbSScott Long 		m->m_data += padlen;
21474c7070dbSScott Long 		ri->iri_len -= padlen;
214823ac9029SStephen Hurd 		m->m_len = ri->iri_frags[i].irf_len;
21494c7070dbSScott Long 	} while (++i < ri->iri_nfrags);
21504c7070dbSScott Long 
21514c7070dbSScott Long 	return (mh);
21524c7070dbSScott Long }
21534c7070dbSScott Long 
21544c7070dbSScott Long /*
21554c7070dbSScott Long  * Process one software descriptor
21564c7070dbSScott Long  */
21574c7070dbSScott Long static struct mbuf *
21584c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
21594c7070dbSScott Long {
21604c7070dbSScott Long 	struct mbuf *m;
2161e035717eSSean Bruno 	iflib_fl_t fl;
2162e035717eSSean Bruno 	caddr_t sd_cl;
2163e035717eSSean Bruno 	int cidx;
21644c7070dbSScott Long 
21654c7070dbSScott Long 	/* should I merge this back in now that the two paths are basically duplicated? */
216623ac9029SStephen Hurd 	if (ri->iri_nfrags == 1 &&
216723ac9029SStephen Hurd 	    ri->iri_frags[0].irf_len <= IFLIB_RX_COPY_THRESH) {
2168e035717eSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[0], NULL, FALSE, &fl, &cidx);
2169e035717eSSean Bruno 		m = fl->ifl_sds.ifsd_m[cidx];
2170e035717eSSean Bruno 		fl->ifl_sds.ifsd_m[cidx] = NULL;
2171e035717eSSean Bruno 		sd_cl = fl->ifl_sds.ifsd_cl[cidx];
21724c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
2173e035717eSSean Bruno 		memcpy(m->m_data, sd_cl, ri->iri_len);
217423ac9029SStephen Hurd 		m->m_len = ri->iri_frags[0].irf_len;
21754c7070dbSScott Long        } else {
21764c7070dbSScott Long 		m = assemble_segments(rxq, ri);
21774c7070dbSScott Long 	}
21784c7070dbSScott Long 	m->m_pkthdr.len = ri->iri_len;
21794c7070dbSScott Long 	m->m_pkthdr.rcvif = ri->iri_ifp;
21804c7070dbSScott Long 	m->m_flags |= ri->iri_flags;
21814c7070dbSScott Long 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
21824c7070dbSScott Long 	m->m_pkthdr.flowid = ri->iri_flowid;
21834c7070dbSScott Long 	M_HASHTYPE_SET(m, ri->iri_rsstype);
21844c7070dbSScott Long 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
21854c7070dbSScott Long 	m->m_pkthdr.csum_data = ri->iri_csum_data;
21864c7070dbSScott Long 	return (m);
21874c7070dbSScott Long }
21884c7070dbSScott Long 
21894c7070dbSScott Long static bool
21904c7070dbSScott Long iflib_rxeof(iflib_rxq_t rxq, int budget)
21914c7070dbSScott Long {
21924c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
21934c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
219423ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
21954c7070dbSScott Long 	int avail, i;
21964c7070dbSScott Long 	uint16_t *cidxp;
21974c7070dbSScott Long 	struct if_rxd_info ri;
21984c7070dbSScott Long 	int err, budget_left, rx_bytes, rx_pkts;
21994c7070dbSScott Long 	iflib_fl_t fl;
22004c7070dbSScott Long 	struct ifnet *ifp;
22014c7070dbSScott Long 	int lro_enabled;
22024c7070dbSScott Long 	/*
22034c7070dbSScott Long 	 * XXX early demux data packets so that if_input processing only handles
22044c7070dbSScott Long 	 * acks in interrupt context
22054c7070dbSScott Long 	 */
22064c7070dbSScott Long 	struct mbuf *m, *mh, *mt;
22074c7070dbSScott Long 
22084c7070dbSScott Long 	if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &budget)) {
22094c7070dbSScott Long 		return (FALSE);
22104c7070dbSScott Long 	}
22114c7070dbSScott Long 
22124c7070dbSScott Long 	mh = mt = NULL;
22134c7070dbSScott Long 	MPASS(budget > 0);
22144c7070dbSScott Long 	rx_pkts	= rx_bytes = 0;
221523ac9029SStephen Hurd 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
22164c7070dbSScott Long 		cidxp = &rxq->ifr_cq_cidx;
22174c7070dbSScott Long 	else
22184c7070dbSScott Long 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
221923ac9029SStephen Hurd 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
22204c7070dbSScott Long 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
22214c7070dbSScott Long 			__iflib_fl_refill_lt(ctx, fl, budget + 8);
22224c7070dbSScott Long 		DBG_COUNTER_INC(rx_unavail);
22234c7070dbSScott Long 		return (false);
22244c7070dbSScott Long 	}
22254c7070dbSScott Long 
22264c7070dbSScott Long 	for (budget_left = budget; (budget_left > 0) && (avail > 0); budget_left--, avail--) {
22274c7070dbSScott Long 		if (__predict_false(!CTX_ACTIVE(ctx))) {
22284c7070dbSScott Long 			DBG_COUNTER_INC(rx_ctx_inactive);
22294c7070dbSScott Long 			break;
22304c7070dbSScott Long 		}
22314c7070dbSScott Long 		/*
22324c7070dbSScott Long 		 * Reset client set fields to their default values
22334c7070dbSScott Long 		 */
22344c7070dbSScott Long 		bzero(&ri, sizeof(ri));
22354c7070dbSScott Long 		ri.iri_qsidx = rxq->ifr_id;
22364c7070dbSScott Long 		ri.iri_cidx = *cidxp;
22374c7070dbSScott Long 		ri.iri_ifp = ctx->ifc_ifp;
22384c7070dbSScott Long 		ri.iri_frags = rxq->ifr_frags;
22394c7070dbSScott Long 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
22404c7070dbSScott Long 
22414c7070dbSScott Long 		/* in lieu of handling correctly - make sure it isn't being unhandled */
22424c7070dbSScott Long 		MPASS(err == 0);
224323ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
224423ac9029SStephen Hurd 			*cidxp = ri.iri_cidx;
224523ac9029SStephen Hurd 			/* Update our consumer index */
224623ac9029SStephen Hurd 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) {
224723ac9029SStephen Hurd 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
22484c7070dbSScott Long 				rxq->ifr_cq_gen = 0;
22494c7070dbSScott Long 			}
22504c7070dbSScott Long 			/* was this only a completion queue message? */
22514c7070dbSScott Long 			if (__predict_false(ri.iri_nfrags == 0))
22524c7070dbSScott Long 				continue;
22534c7070dbSScott Long 		}
22544c7070dbSScott Long 		MPASS(ri.iri_nfrags != 0);
22554c7070dbSScott Long 		MPASS(ri.iri_len != 0);
22564c7070dbSScott Long 
22574c7070dbSScott Long 		/* will advance the cidx on the corresponding free lists */
22584c7070dbSScott Long 		m = iflib_rxd_pkt_get(rxq, &ri);
22594c7070dbSScott Long 		if (avail == 0 && budget_left)
226023ac9029SStephen Hurd 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
22614c7070dbSScott Long 
22624c7070dbSScott Long 		if (__predict_false(m == NULL)) {
22634c7070dbSScott Long 			DBG_COUNTER_INC(rx_mbuf_null);
22644c7070dbSScott Long 			continue;
22654c7070dbSScott Long 		}
22664c7070dbSScott Long 		/* imm_pkt: -- cxgb */
22674c7070dbSScott Long 		if (mh == NULL)
22684c7070dbSScott Long 			mh = mt = m;
22694c7070dbSScott Long 		else {
22704c7070dbSScott Long 			mt->m_nextpkt = m;
22714c7070dbSScott Long 			mt = m;
22724c7070dbSScott Long 		}
22734c7070dbSScott Long 	}
22744c7070dbSScott Long 	/* make sure that we can refill faster than drain */
22754c7070dbSScott Long 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
22764c7070dbSScott Long 		__iflib_fl_refill_lt(ctx, fl, budget + 8);
22774c7070dbSScott Long 
22784c7070dbSScott Long 	ifp = ctx->ifc_ifp;
22794c7070dbSScott Long 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
22804c7070dbSScott Long 	while (mh != NULL) {
22814c7070dbSScott Long 		m = mh;
22824c7070dbSScott Long 		mh = mh->m_nextpkt;
22834c7070dbSScott Long 		m->m_nextpkt = NULL;
22844c7070dbSScott Long 		rx_bytes += m->m_pkthdr.len;
22854c7070dbSScott Long 		rx_pkts++;
2286aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
22874c7070dbSScott Long 		if (lro_enabled && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
22884c7070dbSScott Long 			continue;
2289aaeb188aSBjoern A. Zeeb #endif
22904c7070dbSScott Long 		DBG_COUNTER_INC(rx_if_input);
22914c7070dbSScott Long 		ifp->if_input(ifp, m);
22924c7070dbSScott Long 	}
229323ac9029SStephen Hurd 
22944c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
22954c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
22964c7070dbSScott Long 
22974c7070dbSScott Long 	/*
22984c7070dbSScott Long 	 * Flush any outstanding LRO work
22994c7070dbSScott Long 	 */
2300aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
230123ac9029SStephen Hurd 	tcp_lro_flush_all(&rxq->ifr_lc);
2302aaeb188aSBjoern A. Zeeb #endif
230323ac9029SStephen Hurd 	if (avail)
230423ac9029SStephen Hurd 		return true;
230523ac9029SStephen Hurd 	return (iflib_rxd_avail(ctx, rxq, *cidxp, 1));
23064c7070dbSScott Long }
23074c7070dbSScott Long 
23084c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
23094c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
231023ac9029SStephen Hurd #define TXQ_MAX_DB_DEFERRED(size) (size >> 5)
231123ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
23124c7070dbSScott Long 
23134c7070dbSScott Long static __inline void
23144c7070dbSScott Long iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring)
23154c7070dbSScott Long {
23164c7070dbSScott Long 	uint32_t dbval;
23174c7070dbSScott Long 
231823ac9029SStephen Hurd 	if (ring || txq->ift_db_pending >=
231923ac9029SStephen Hurd 	    TXQ_MAX_DB_DEFERRED(txq->ift_size)) {
23204c7070dbSScott Long 
23214c7070dbSScott Long 		/* the lock will only ever be contended in the !min_latency case */
23224c7070dbSScott Long 		if (!TXDB_TRYLOCK(txq))
23234c7070dbSScott Long 			return;
23244c7070dbSScott Long 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
23254c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
23264c7070dbSScott Long 		txq->ift_db_pending = txq->ift_npending = 0;
23274c7070dbSScott Long 		TXDB_UNLOCK(txq);
23284c7070dbSScott Long 	}
23294c7070dbSScott Long }
23304c7070dbSScott Long 
23314c7070dbSScott Long static void
23324c7070dbSScott Long iflib_txd_deferred_db_check(void * arg)
23334c7070dbSScott Long {
23344c7070dbSScott Long 	iflib_txq_t txq = arg;
23354c7070dbSScott Long 
23364c7070dbSScott Long 	/* simple non-zero boolean so use bitwise OR */
23374c7070dbSScott Long 	if ((txq->ift_db_pending | txq->ift_npending) &&
23384c7070dbSScott Long 	    txq->ift_db_pending >= txq->ift_db_pending_queued)
23394c7070dbSScott Long 		iflib_txd_db_check(txq->ift_ctx, txq, TRUE);
23404c7070dbSScott Long 	txq->ift_db_pending_queued = 0;
23414c7070dbSScott Long 	if (ifmp_ring_is_stalled(txq->ift_br[0]))
23424c7070dbSScott Long 		iflib_txq_check_drain(txq, 4);
23434c7070dbSScott Long }
23444c7070dbSScott Long 
23454c7070dbSScott Long #ifdef PKT_DEBUG
23464c7070dbSScott Long static void
23474c7070dbSScott Long print_pkt(if_pkt_info_t pi)
23484c7070dbSScott Long {
23494c7070dbSScott Long 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
23504c7070dbSScott Long 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
23514c7070dbSScott Long 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
23524c7070dbSScott Long 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
23534c7070dbSScott Long 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
23544c7070dbSScott Long 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
23554c7070dbSScott Long }
23564c7070dbSScott Long #endif
23574c7070dbSScott Long 
23584c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
23594c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
23604c7070dbSScott Long 
23614c7070dbSScott Long static int
23624c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
23634c7070dbSScott Long {
23641248952aSSean Bruno 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
23654c7070dbSScott Long 	struct ether_vlan_header *eh;
236623ac9029SStephen Hurd 	struct mbuf *m, *n;
23674c7070dbSScott Long 
236823ac9029SStephen Hurd 	n = m = *mp;
23691248952aSSean Bruno 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
23701248952aSSean Bruno 	    M_WRITABLE(m) == 0) {
23711248952aSSean Bruno 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
23721248952aSSean Bruno 			return (ENOMEM);
23731248952aSSean Bruno 		} else {
23741248952aSSean Bruno 			m_freem(*mp);
23751248952aSSean Bruno 			n = *mp = m;
23761248952aSSean Bruno 		}
23771248952aSSean Bruno 	}
23781248952aSSean Bruno 
23794c7070dbSScott Long 	/*
23804c7070dbSScott Long 	 * Determine where frame payload starts.
23814c7070dbSScott Long 	 * Jump over vlan headers if already present,
23824c7070dbSScott Long 	 * helpful for QinQ too.
23834c7070dbSScott Long 	 */
23844c7070dbSScott Long 	if (__predict_false(m->m_len < sizeof(*eh))) {
23854c7070dbSScott Long 		txq->ift_pullups++;
23864c7070dbSScott Long 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
23874c7070dbSScott Long 			return (ENOMEM);
23884c7070dbSScott Long 	}
23894c7070dbSScott Long 	eh = mtod(m, struct ether_vlan_header *);
23904c7070dbSScott Long 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
23914c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_proto);
23924c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
23934c7070dbSScott Long 	} else {
23944c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
23954c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
23964c7070dbSScott Long 	}
23974c7070dbSScott Long 
23984c7070dbSScott Long 	switch (pi->ipi_etype) {
23994c7070dbSScott Long #ifdef INET
24004c7070dbSScott Long 	case ETHERTYPE_IP:
24014c7070dbSScott Long 	{
24024c7070dbSScott Long 		struct ip *ip = NULL;
24034c7070dbSScott Long 		struct tcphdr *th = NULL;
24044c7070dbSScott Long 		int minthlen;
24054c7070dbSScott Long 
24064c7070dbSScott Long 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
24074c7070dbSScott Long 		if (__predict_false(m->m_len < minthlen)) {
24084c7070dbSScott Long 			/*
24094c7070dbSScott Long 			 * if this code bloat is causing too much of a hit
24104c7070dbSScott Long 			 * move it to a separate function and mark it noinline
24114c7070dbSScott Long 			 */
24124c7070dbSScott Long 			if (m->m_len == pi->ipi_ehdrlen) {
24134c7070dbSScott Long 				n = m->m_next;
24144c7070dbSScott Long 				MPASS(n);
24154c7070dbSScott Long 				if (n->m_len >= sizeof(*ip))  {
24164c7070dbSScott Long 					ip = (struct ip *)n->m_data;
24174c7070dbSScott Long 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
24184c7070dbSScott Long 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
24194c7070dbSScott Long 				} else {
24204c7070dbSScott Long 					txq->ift_pullups++;
24214c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
24224c7070dbSScott Long 						return (ENOMEM);
24234c7070dbSScott Long 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
24244c7070dbSScott Long 				}
24254c7070dbSScott Long 			} else {
24264c7070dbSScott Long 				txq->ift_pullups++;
24274c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
24284c7070dbSScott Long 					return (ENOMEM);
24294c7070dbSScott Long 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
24304c7070dbSScott Long 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
24314c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
24324c7070dbSScott Long 			}
24334c7070dbSScott Long 		} else {
24344c7070dbSScott Long 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
24354c7070dbSScott Long 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
24364c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
24374c7070dbSScott Long 		}
24384c7070dbSScott Long 		pi->ipi_ip_hlen = ip->ip_hl << 2;
24394c7070dbSScott Long 		pi->ipi_ipproto = ip->ip_p;
24404c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV4;
24414c7070dbSScott Long 
24424c7070dbSScott Long 		if (pi->ipi_csum_flags & CSUM_IP)
24434c7070dbSScott Long                        ip->ip_sum = 0;
24444c7070dbSScott Long 
24454c7070dbSScott Long 		if (pi->ipi_ipproto == IPPROTO_TCP) {
24464c7070dbSScott Long 			if (__predict_false(th == NULL)) {
24474c7070dbSScott Long 				txq->ift_pullups++;
24484c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
24494c7070dbSScott Long 					return (ENOMEM);
24504c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
24514c7070dbSScott Long 			}
24524c7070dbSScott Long 			pi->ipi_tcp_hflags = th->th_flags;
24534c7070dbSScott Long 			pi->ipi_tcp_hlen = th->th_off << 2;
24544c7070dbSScott Long 			pi->ipi_tcp_seq = th->th_seq;
24554c7070dbSScott Long 		}
24564c7070dbSScott Long 		if (IS_TSO4(pi)) {
24574c7070dbSScott Long 			if (__predict_false(ip->ip_p != IPPROTO_TCP))
24584c7070dbSScott Long 				return (ENXIO);
24594c7070dbSScott Long 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
24604c7070dbSScott Long 					       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
24614c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
24621248952aSSean Bruno 			if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
24631248952aSSean Bruno 				ip->ip_sum = 0;
24641248952aSSean Bruno 				ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
24651248952aSSean Bruno 			}
24664c7070dbSScott Long 		}
24674c7070dbSScott Long 		break;
24684c7070dbSScott Long 	}
24694c7070dbSScott Long #endif
24704c7070dbSScott Long #ifdef INET6
24714c7070dbSScott Long 	case ETHERTYPE_IPV6:
24724c7070dbSScott Long 	{
24734c7070dbSScott Long 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
24744c7070dbSScott Long 		struct tcphdr *th;
24754c7070dbSScott Long 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
24764c7070dbSScott Long 
24774c7070dbSScott Long 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
24784c7070dbSScott Long 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
24794c7070dbSScott Long 				return (ENOMEM);
24804c7070dbSScott Long 		}
24814c7070dbSScott Long 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
24824c7070dbSScott Long 
24834c7070dbSScott Long 		/* XXX-BZ this will go badly in case of ext hdrs. */
24844c7070dbSScott Long 		pi->ipi_ipproto = ip6->ip6_nxt;
24854c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV6;
24864c7070dbSScott Long 
24874c7070dbSScott Long 		if (pi->ipi_ipproto == IPPROTO_TCP) {
24884c7070dbSScott Long 			if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
24894c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
24904c7070dbSScott Long 					return (ENOMEM);
24914c7070dbSScott Long 			}
24924c7070dbSScott Long 			pi->ipi_tcp_hflags = th->th_flags;
24934c7070dbSScott Long 			pi->ipi_tcp_hlen = th->th_off << 2;
24944c7070dbSScott Long 		}
24954c7070dbSScott Long 		if (IS_TSO6(pi)) {
24964c7070dbSScott Long 
24974c7070dbSScott Long 			if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
24984c7070dbSScott Long 				return (ENXIO);
24994c7070dbSScott Long 			/*
25004c7070dbSScott Long 			 * The corresponding flag is set by the stack in the IPv4
25014c7070dbSScott Long 			 * TSO case, but not in IPv6 (at least in FreeBSD 10.2).
25024c7070dbSScott Long 			 * So, set it here because the rest of the flow requires it.
25034c7070dbSScott Long 			 */
25044c7070dbSScott Long 			pi->ipi_csum_flags |= CSUM_TCP_IPV6;
25054c7070dbSScott Long 			th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
25064c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
25074c7070dbSScott Long 		}
25084c7070dbSScott Long 		break;
25094c7070dbSScott Long 	}
25104c7070dbSScott Long #endif
25114c7070dbSScott Long 	default:
25124c7070dbSScott Long 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
25134c7070dbSScott Long 		pi->ipi_ip_hlen = 0;
25144c7070dbSScott Long 		break;
25154c7070dbSScott Long 	}
25164c7070dbSScott Long 	*mp = m;
25171248952aSSean Bruno 
25184c7070dbSScott Long 	return (0);
25194c7070dbSScott Long }
25204c7070dbSScott Long 
25214c7070dbSScott Long static  __noinline  struct mbuf *
25224c7070dbSScott Long collapse_pkthdr(struct mbuf *m0)
25234c7070dbSScott Long {
25244c7070dbSScott Long 	struct mbuf *m, *m_next, *tmp;
25254c7070dbSScott Long 
25264c7070dbSScott Long 	m = m0;
25274c7070dbSScott Long 	m_next = m->m_next;
25284c7070dbSScott Long 	while (m_next != NULL && m_next->m_len == 0) {
25294c7070dbSScott Long 		m = m_next;
25304c7070dbSScott Long 		m->m_next = NULL;
25314c7070dbSScott Long 		m_free(m);
25324c7070dbSScott Long 		m_next = m_next->m_next;
25334c7070dbSScott Long 	}
25344c7070dbSScott Long 	m = m0;
25354c7070dbSScott Long 	m->m_next = m_next;
25364c7070dbSScott Long 	if ((m_next->m_flags & M_EXT) == 0) {
25374c7070dbSScott Long 		m = m_defrag(m, M_NOWAIT);
25384c7070dbSScott Long 	} else {
25394c7070dbSScott Long 		tmp = m_next->m_next;
25404c7070dbSScott Long 		memcpy(m_next, m, MPKTHSIZE);
25414c7070dbSScott Long 		m = m_next;
25424c7070dbSScott Long 		m->m_next = tmp;
25434c7070dbSScott Long 	}
25444c7070dbSScott Long 	return (m);
25454c7070dbSScott Long }
25464c7070dbSScott Long 
25474c7070dbSScott Long /*
25484c7070dbSScott Long  * If dodgy hardware rejects the scatter gather chain we've handed it
254923ac9029SStephen Hurd  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
255023ac9029SStephen Hurd  * m_defrag'd mbufs
25514c7070dbSScott Long  */
25524c7070dbSScott Long static __noinline struct mbuf *
255323ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq)
25544c7070dbSScott Long {
255523ac9029SStephen Hurd 	int ntxd, i, pidx;
25564c7070dbSScott Long 	struct mbuf *m, *mh, **ifsd_m;
25574c7070dbSScott Long 
25584c7070dbSScott Long 	pidx = txq->ift_pidx;
25594c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
256023ac9029SStephen Hurd 	ntxd = txq->ift_size;
25614c7070dbSScott Long 	mh = m = ifsd_m[pidx];
25624c7070dbSScott Long 	ifsd_m[pidx] = NULL;
25634c7070dbSScott Long #if MEMORY_LOGGING
25644c7070dbSScott Long 	txq->ift_dequeued++;
25654c7070dbSScott Long #endif
25664c7070dbSScott Long 	i = 1;
25674c7070dbSScott Long 
256823ac9029SStephen Hurd 	while (m) {
25694c7070dbSScott Long 		ifsd_m[(pidx + i) & (ntxd -1)] = NULL;
25704c7070dbSScott Long #if MEMORY_LOGGING
25714c7070dbSScott Long 		txq->ift_dequeued++;
25724c7070dbSScott Long #endif
25734c7070dbSScott Long 		m = m->m_next;
25744c7070dbSScott Long 		i++;
25754c7070dbSScott Long 	}
25764c7070dbSScott Long 	return (mh);
25774c7070dbSScott Long }
25784c7070dbSScott Long 
25794c7070dbSScott Long static int
25804c7070dbSScott Long iflib_busdma_load_mbuf_sg(iflib_txq_t txq, bus_dma_tag_t tag, bus_dmamap_t map,
25814c7070dbSScott Long 			  struct mbuf **m0, bus_dma_segment_t *segs, int *nsegs,
25824c7070dbSScott Long 			  int max_segs, int flags)
25834c7070dbSScott Long {
25844c7070dbSScott Long 	if_ctx_t ctx;
25854c7070dbSScott Long 	if_shared_ctx_t		sctx;
258623ac9029SStephen Hurd 	if_softc_ctx_t		scctx;
25874c7070dbSScott Long 	int i, next, pidx, mask, err, maxsegsz, ntxd, count;
25884c7070dbSScott Long 	struct mbuf *m, *tmp, **ifsd_m, **mp;
25894c7070dbSScott Long 
25904c7070dbSScott Long 	m = *m0;
25914c7070dbSScott Long 
25924c7070dbSScott Long 	/*
25934c7070dbSScott Long 	 * Please don't ever do this
25944c7070dbSScott Long 	 */
25954c7070dbSScott Long 	if (__predict_false(m->m_len == 0))
25964c7070dbSScott Long 		*m0 = m = collapse_pkthdr(m);
25974c7070dbSScott Long 
25984c7070dbSScott Long 	ctx = txq->ift_ctx;
25994c7070dbSScott Long 	sctx = ctx->ifc_sctx;
260023ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
26014c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
260223ac9029SStephen Hurd 	ntxd = txq->ift_size;
26034c7070dbSScott Long 	pidx = txq->ift_pidx;
26044c7070dbSScott Long 	if (map != NULL) {
26054c7070dbSScott Long 		uint8_t *ifsd_flags = txq->ift_sds.ifsd_flags;
26064c7070dbSScott Long 
26074c7070dbSScott Long 		err = bus_dmamap_load_mbuf_sg(tag, map,
26084c7070dbSScott Long 					      *m0, segs, nsegs, BUS_DMA_NOWAIT);
26094c7070dbSScott Long 		if (err)
26104c7070dbSScott Long 			return (err);
26114c7070dbSScott Long 		ifsd_flags[pidx] |= TX_SW_DESC_MAPPED;
26124c7070dbSScott Long 		i = 0;
26134c7070dbSScott Long 		next = pidx;
261423ac9029SStephen Hurd 		mask = (txq->ift_size-1);
26154c7070dbSScott Long 		m = *m0;
26164c7070dbSScott Long 		do {
26174c7070dbSScott Long 			mp = &ifsd_m[next];
26184c7070dbSScott Long 			*mp = m;
26194c7070dbSScott Long 			m = m->m_next;
26204c7070dbSScott Long 			if (__predict_false((*mp)->m_len == 0)) {
26214c7070dbSScott Long 				m_free(*mp);
26224c7070dbSScott Long 				*mp = NULL;
26234c7070dbSScott Long 			} else
26244c7070dbSScott Long 				next = (pidx + i) & (ntxd-1);
26254c7070dbSScott Long 		} while (m != NULL);
26264c7070dbSScott Long 	} else {
26274c7070dbSScott Long 		int buflen, sgsize, max_sgsize;
26284c7070dbSScott Long 		vm_offset_t vaddr;
26294c7070dbSScott Long 		vm_paddr_t curaddr;
26304c7070dbSScott Long 
26314c7070dbSScott Long 		count = i = 0;
26324c7070dbSScott Long 		maxsegsz = sctx->isc_tx_maxsize;
26334c7070dbSScott Long 		m = *m0;
26344c7070dbSScott Long 		do {
26354c7070dbSScott Long 			if (__predict_false(m->m_len <= 0)) {
26364c7070dbSScott Long 				tmp = m;
26374c7070dbSScott Long 				m = m->m_next;
26384c7070dbSScott Long 				tmp->m_next = NULL;
26394c7070dbSScott Long 				m_free(tmp);
26404c7070dbSScott Long 				continue;
26414c7070dbSScott Long 			}
26424c7070dbSScott Long 			buflen = m->m_len;
26434c7070dbSScott Long 			vaddr = (vm_offset_t)m->m_data;
26444c7070dbSScott Long 			/*
26454c7070dbSScott Long 			 * see if we can't be smarter about physically
26464c7070dbSScott Long 			 * contiguous mappings
26474c7070dbSScott Long 			 */
26484c7070dbSScott Long 			next = (pidx + count) & (ntxd-1);
26494c7070dbSScott Long 			MPASS(ifsd_m[next] == NULL);
26504c7070dbSScott Long #if MEMORY_LOGGING
26514c7070dbSScott Long 			txq->ift_enqueued++;
26524c7070dbSScott Long #endif
26534c7070dbSScott Long 			ifsd_m[next] = m;
26544c7070dbSScott Long 			while (buflen > 0) {
26554c7070dbSScott Long 				max_sgsize = MIN(buflen, maxsegsz);
26564c7070dbSScott Long 				curaddr = pmap_kextract(vaddr);
26574c7070dbSScott Long 				sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
26584c7070dbSScott Long 				sgsize = MIN(sgsize, max_sgsize);
26594c7070dbSScott Long 				segs[i].ds_addr = curaddr;
26604c7070dbSScott Long 				segs[i].ds_len = sgsize;
26614c7070dbSScott Long 				vaddr += sgsize;
26624c7070dbSScott Long 				buflen -= sgsize;
26634c7070dbSScott Long 				i++;
26644c7070dbSScott Long 				if (i >= max_segs)
26654c7070dbSScott Long 					goto err;
26664c7070dbSScott Long 			}
26674c7070dbSScott Long 			count++;
26684c7070dbSScott Long 			tmp = m;
26694c7070dbSScott Long 			m = m->m_next;
26704c7070dbSScott Long 		} while (m != NULL);
26714c7070dbSScott Long 		*nsegs = i;
26724c7070dbSScott Long 	}
26734c7070dbSScott Long 	return (0);
26744c7070dbSScott Long err:
267523ac9029SStephen Hurd 	*m0 = iflib_remove_mbuf(txq);
26764c7070dbSScott Long 	return (EFBIG);
26774c7070dbSScott Long }
26784c7070dbSScott Long 
26794c7070dbSScott Long static int
26804c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
26814c7070dbSScott Long {
26824c7070dbSScott Long 	if_ctx_t		ctx;
26834c7070dbSScott Long 	if_shared_ctx_t		sctx;
26844c7070dbSScott Long 	if_softc_ctx_t		scctx;
26854c7070dbSScott Long 	bus_dma_segment_t	*segs;
26864c7070dbSScott Long 	struct mbuf		*m_head;
26874c7070dbSScott Long 	bus_dmamap_t		map;
26884c7070dbSScott Long 	struct if_pkt_info	pi;
26894c7070dbSScott Long 	int remap = 0;
26904c7070dbSScott Long 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
26914c7070dbSScott Long 	bus_dma_tag_t desc_tag;
26924c7070dbSScott Long 
26934c7070dbSScott Long 	segs = txq->ift_segs;
26944c7070dbSScott Long 	ctx = txq->ift_ctx;
26954c7070dbSScott Long 	sctx = ctx->ifc_sctx;
26964c7070dbSScott Long 	scctx = &ctx->ifc_softc_ctx;
26974c7070dbSScott Long 	segs = txq->ift_segs;
269823ac9029SStephen Hurd 	ntxd = txq->ift_size;
26994c7070dbSScott Long 	m_head = *m_headp;
27004c7070dbSScott Long 	map = NULL;
27014c7070dbSScott Long 
27024c7070dbSScott Long 	/*
27034c7070dbSScott Long 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
27044c7070dbSScott Long 	 */
27054c7070dbSScott Long 	cidx = txq->ift_cidx;
27064c7070dbSScott Long 	pidx = txq->ift_pidx;
27074c7070dbSScott Long 	next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
27084c7070dbSScott Long 
27094c7070dbSScott Long 	/* prefetch the next cache line of mbuf pointers and flags */
27104c7070dbSScott Long 	prefetch(&txq->ift_sds.ifsd_m[next]);
27114c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
27124c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_map[next]);
27134c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[pidx];
27144c7070dbSScott Long 		next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
27154c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_flags[next]);
27164c7070dbSScott Long 	}
27174c7070dbSScott Long 
27184c7070dbSScott Long 
27194c7070dbSScott Long 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
27204c7070dbSScott Long 		desc_tag = txq->ift_tso_desc_tag;
27214c7070dbSScott Long 		max_segs = scctx->isc_tx_tso_segments_max;
27224c7070dbSScott Long 	} else {
27234c7070dbSScott Long 		desc_tag = txq->ift_desc_tag;
27244c7070dbSScott Long 		max_segs = scctx->isc_tx_nsegments;
27254c7070dbSScott Long 	}
27264c7070dbSScott Long 	m_head = *m_headp;
27274c7070dbSScott Long 	bzero(&pi, sizeof(pi));
27284c7070dbSScott Long 	pi.ipi_len = m_head->m_pkthdr.len;
27294c7070dbSScott Long 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
27304c7070dbSScott Long 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
27314c7070dbSScott Long 	pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0;
27324c7070dbSScott Long 	pi.ipi_pidx = pidx;
27334c7070dbSScott Long 	pi.ipi_qsidx = txq->ift_id;
27344c7070dbSScott Long 
27354c7070dbSScott Long 	/* deliberate bitwise OR to make one condition */
27364c7070dbSScott Long 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
27374c7070dbSScott Long 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0))
27384c7070dbSScott Long 			return (err);
27394c7070dbSScott Long 		m_head = *m_headp;
27404c7070dbSScott Long 	}
27414c7070dbSScott Long 
27424c7070dbSScott Long retry:
27434c7070dbSScott Long 	err = iflib_busdma_load_mbuf_sg(txq, desc_tag, map, m_headp, segs, &nsegs, max_segs, BUS_DMA_NOWAIT);
27444c7070dbSScott Long defrag:
27454c7070dbSScott Long 	if (__predict_false(err)) {
27464c7070dbSScott Long 		switch (err) {
27474c7070dbSScott Long 		case EFBIG:
27484c7070dbSScott Long 			/* try collapse once and defrag once */
27494c7070dbSScott Long 			if (remap == 0)
27504c7070dbSScott Long 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
27514c7070dbSScott Long 			if (remap == 1)
27524c7070dbSScott Long 				m_head = m_defrag(*m_headp, M_NOWAIT);
27534c7070dbSScott Long 			remap++;
27544c7070dbSScott Long 			if (__predict_false(m_head == NULL))
27554c7070dbSScott Long 				goto defrag_failed;
27564c7070dbSScott Long 			txq->ift_mbuf_defrag++;
27574c7070dbSScott Long 			*m_headp = m_head;
27584c7070dbSScott Long 			goto retry;
27594c7070dbSScott Long 			break;
27604c7070dbSScott Long 		case ENOMEM:
27614c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
27624c7070dbSScott Long 			break;
27634c7070dbSScott Long 		default:
27644c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
27654c7070dbSScott Long 			m_freem(*m_headp);
27664c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
27674c7070dbSScott Long 			*m_headp = NULL;
27684c7070dbSScott Long 			break;
27694c7070dbSScott Long 		}
27704c7070dbSScott Long 		txq->ift_map_failed++;
27714c7070dbSScott Long 		DBG_COUNTER_INC(encap_load_mbuf_fail);
27724c7070dbSScott Long 		return (err);
27734c7070dbSScott Long 	}
27744c7070dbSScott Long 
27754c7070dbSScott Long 	/*
27764c7070dbSScott Long 	 * XXX assumes a 1 to 1 relationship between segments and
27774c7070dbSScott Long 	 *        descriptors - this does not hold true on all drivers, e.g.
27784c7070dbSScott Long 	 *        cxgb
27794c7070dbSScott Long 	 */
27804c7070dbSScott Long 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
27814c7070dbSScott Long 		txq->ift_no_desc_avail++;
27824c7070dbSScott Long 		if (map != NULL)
27834c7070dbSScott Long 			bus_dmamap_unload(desc_tag, map);
27844c7070dbSScott Long 		DBG_COUNTER_INC(encap_txq_avail_fail);
278523ac9029SStephen Hurd 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
27864c7070dbSScott Long 			GROUPTASK_ENQUEUE(&txq->ift_task);
27874c7070dbSScott Long 		return (ENOBUFS);
27884c7070dbSScott Long 	}
27894c7070dbSScott Long 	pi.ipi_segs = segs;
27904c7070dbSScott Long 	pi.ipi_nsegs = nsegs;
27914c7070dbSScott Long 
279223ac9029SStephen Hurd 	MPASS(pidx >= 0 && pidx < txq->ift_size);
27934c7070dbSScott Long #ifdef PKT_DEBUG
27944c7070dbSScott Long 	print_pkt(&pi);
27954c7070dbSScott Long #endif
27964c7070dbSScott Long 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
27974c7070dbSScott Long 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
27984c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
27994c7070dbSScott Long 
28004c7070dbSScott Long 		DBG_COUNTER_INC(tx_encap);
280123ac9029SStephen Hurd 		MPASS(pi.ipi_new_pidx >= 0 &&
280223ac9029SStephen Hurd 		    pi.ipi_new_pidx < txq->ift_size);
28034c7070dbSScott Long 
28044c7070dbSScott Long 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
28054c7070dbSScott Long 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
280623ac9029SStephen Hurd 			ndesc += txq->ift_size;
28074c7070dbSScott Long 			txq->ift_gen = 1;
28084c7070dbSScott Long 		}
28091248952aSSean Bruno 		/*
28101248952aSSean Bruno 		 * drivers can need as many as
28111248952aSSean Bruno 		 * two sentinels
28121248952aSSean Bruno 		 */
28131248952aSSean Bruno 		MPASS(ndesc <= pi.ipi_nsegs + 2);
28144c7070dbSScott Long 		MPASS(pi.ipi_new_pidx != pidx);
28154c7070dbSScott Long 		MPASS(ndesc > 0);
28164c7070dbSScott Long 		txq->ift_in_use += ndesc;
28174c7070dbSScott Long 		/*
28184c7070dbSScott Long 		 * We update the last software descriptor again here because there may
28194c7070dbSScott Long 		 * be a sentinel and/or there may be more mbufs than segments
28204c7070dbSScott Long 		 */
28214c7070dbSScott Long 		txq->ift_pidx = pi.ipi_new_pidx;
28224c7070dbSScott Long 		txq->ift_npending += pi.ipi_ndescs;
28234c7070dbSScott Long 	} else if (__predict_false(err == EFBIG && remap < 2)) {
282423ac9029SStephen Hurd 		*m_headp = m_head = iflib_remove_mbuf(txq);
28254c7070dbSScott Long 		remap = 1;
28264c7070dbSScott Long 		txq->ift_txd_encap_efbig++;
28274c7070dbSScott Long 		goto defrag;
28284c7070dbSScott Long 	} else
28294c7070dbSScott Long 		DBG_COUNTER_INC(encap_txd_encap_fail);
28304c7070dbSScott Long 	return (err);
28314c7070dbSScott Long 
28324c7070dbSScott Long defrag_failed:
28334c7070dbSScott Long 	txq->ift_mbuf_defrag_failed++;
28344c7070dbSScott Long 	txq->ift_map_failed++;
28354c7070dbSScott Long 	m_freem(*m_headp);
28364c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
28374c7070dbSScott Long 	*m_headp = NULL;
28384c7070dbSScott Long 	return (ENOMEM);
28394c7070dbSScott Long }
28404c7070dbSScott Long 
28414c7070dbSScott Long /* forward compatibility for cxgb */
28424c7070dbSScott Long #define FIRST_QSET(ctx) 0
28434c7070dbSScott Long 
28444c7070dbSScott Long #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
28454c7070dbSScott Long #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
284623ac9029SStephen Hurd #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
28474c7070dbSScott Long #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
28484c7070dbSScott Long #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
28494c7070dbSScott Long #define MAX_TX_DESC(ctx) ((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max)
28504c7070dbSScott Long 
28514c7070dbSScott Long 
28524c7070dbSScott Long 
28534c7070dbSScott Long /* if there are more than TXQ_MIN_OCCUPANCY packets pending we consider deferring
28544c7070dbSScott Long  * doorbell writes
28554c7070dbSScott Long  *
28564c7070dbSScott Long  * ORing with 2 assures that min occupancy is never less than 2 without any conditional logic
28574c7070dbSScott Long  */
285823ac9029SStephen Hurd #define TXQ_MIN_OCCUPANCY(size) ((size >> 6)| 0x2)
28594c7070dbSScott Long 
28604c7070dbSScott Long static inline int
28614c7070dbSScott Long iflib_txq_min_occupancy(iflib_txq_t txq)
28624c7070dbSScott Long {
28634c7070dbSScott Long 	if_ctx_t ctx;
28644c7070dbSScott Long 
28654c7070dbSScott Long 	ctx = txq->ift_ctx;
286623ac9029SStephen Hurd 	return (get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx,
286723ac9029SStephen Hurd 	    txq->ift_gen) < TXQ_MIN_OCCUPANCY(txq->ift_size) +
286823ac9029SStephen Hurd 	    MAX_TX_DESC(ctx));
28694c7070dbSScott Long }
28704c7070dbSScott Long 
28714c7070dbSScott Long static void
28724c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n)
28734c7070dbSScott Long {
28744c7070dbSScott Long 	int hasmap;
28754c7070dbSScott Long 	uint32_t qsize, cidx, mask, gen;
28764c7070dbSScott Long 	struct mbuf *m, **ifsd_m;
28774c7070dbSScott Long 	uint8_t *ifsd_flags;
28784c7070dbSScott Long 	bus_dmamap_t *ifsd_map;
28794c7070dbSScott Long 
28804c7070dbSScott Long 	cidx = txq->ift_cidx;
28814c7070dbSScott Long 	gen = txq->ift_gen;
288223ac9029SStephen Hurd 	qsize = txq->ift_size;
28834c7070dbSScott Long 	mask = qsize-1;
28844c7070dbSScott Long 	hasmap = txq->ift_sds.ifsd_map != NULL;
28854c7070dbSScott Long 	ifsd_flags = txq->ift_sds.ifsd_flags;
28864c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
28874c7070dbSScott Long 	ifsd_map = txq->ift_sds.ifsd_map;
28884c7070dbSScott Long 
28894c7070dbSScott Long 	while (n--) {
28904c7070dbSScott Long 		prefetch(ifsd_m[(cidx + 3) & mask]);
28914c7070dbSScott Long 		prefetch(ifsd_m[(cidx + 4) & mask]);
28924c7070dbSScott Long 
28934c7070dbSScott Long 		if (ifsd_m[cidx] != NULL) {
28944c7070dbSScott Long 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
28954c7070dbSScott Long 			prefetch(&ifsd_flags[(cidx + CACHE_PTR_INCREMENT) & mask]);
28964c7070dbSScott Long 			if (hasmap && (ifsd_flags[cidx] & TX_SW_DESC_MAPPED)) {
28974c7070dbSScott Long 				/*
28984c7070dbSScott Long 				 * does it matter if it's not the TSO tag? If so we'll
28994c7070dbSScott Long 				 * have to add the type to flags
29004c7070dbSScott Long 				 */
29014c7070dbSScott Long 				bus_dmamap_unload(txq->ift_desc_tag, ifsd_map[cidx]);
29024c7070dbSScott Long 				ifsd_flags[cidx] &= ~TX_SW_DESC_MAPPED;
29034c7070dbSScott Long 			}
29044c7070dbSScott Long 			if ((m = ifsd_m[cidx]) != NULL) {
29054c7070dbSScott Long 				/* XXX we don't support any drivers that batch packets yet */
29064c7070dbSScott Long 				MPASS(m->m_nextpkt == NULL);
29074c7070dbSScott Long 
290823ac9029SStephen Hurd 				m_free(m);
29094c7070dbSScott Long 				ifsd_m[cidx] = NULL;
29104c7070dbSScott Long #if MEMORY_LOGGING
29114c7070dbSScott Long 				txq->ift_dequeued++;
29124c7070dbSScott Long #endif
29134c7070dbSScott Long 				DBG_COUNTER_INC(tx_frees);
29144c7070dbSScott Long 			}
29154c7070dbSScott Long 		}
29164c7070dbSScott Long 		if (__predict_false(++cidx == qsize)) {
29174c7070dbSScott Long 			cidx = 0;
29184c7070dbSScott Long 			gen = 0;
29194c7070dbSScott Long 		}
29204c7070dbSScott Long 	}
29214c7070dbSScott Long 	txq->ift_cidx = cidx;
29224c7070dbSScott Long 	txq->ift_gen = gen;
29234c7070dbSScott Long }
29244c7070dbSScott Long 
29254c7070dbSScott Long static __inline int
29264c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
29274c7070dbSScott Long {
29284c7070dbSScott Long 	int reclaim;
29294c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
29304c7070dbSScott Long 
29314c7070dbSScott Long 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
29324c7070dbSScott Long 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
29334c7070dbSScott Long 
29344c7070dbSScott Long 	/*
29354c7070dbSScott Long 	 * Need a rate-limiting check so that this isn't called every time
29364c7070dbSScott Long 	 */
29374c7070dbSScott Long 	iflib_tx_credits_update(ctx, txq);
29384c7070dbSScott Long 	reclaim = DESC_RECLAIMABLE(txq);
29394c7070dbSScott Long 
29404c7070dbSScott Long 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
29414c7070dbSScott Long #ifdef INVARIANTS
29424c7070dbSScott Long 		if (iflib_verbose_debug) {
29434c7070dbSScott Long 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
29444c7070dbSScott Long 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
29454c7070dbSScott Long 			       reclaim, thresh);
29464c7070dbSScott Long 
29474c7070dbSScott Long 		}
29484c7070dbSScott Long #endif
29494c7070dbSScott Long 		return (0);
29504c7070dbSScott Long 	}
29514c7070dbSScott Long 	iflib_tx_desc_free(txq, reclaim);
29524c7070dbSScott Long 	txq->ift_cleaned += reclaim;
29534c7070dbSScott Long 	txq->ift_in_use -= reclaim;
29544c7070dbSScott Long 
29554c7070dbSScott Long 	if (txq->ift_active == FALSE)
29564c7070dbSScott Long 		txq->ift_active = TRUE;
29574c7070dbSScott Long 
29584c7070dbSScott Long 	return (reclaim);
29594c7070dbSScott Long }
29604c7070dbSScott Long 
29614c7070dbSScott Long static struct mbuf **
29624c7070dbSScott Long _ring_peek_one(struct ifmp_ring *r, int cidx, int offset)
29634c7070dbSScott Long {
29644c7070dbSScott Long 
29654c7070dbSScott Long 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (r->size-1)]));
29664c7070dbSScott Long }
29674c7070dbSScott Long 
29684c7070dbSScott Long static void
29694c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget)
29704c7070dbSScott Long {
29714c7070dbSScott Long 
29724c7070dbSScott Long 	ifmp_ring_check_drainage(txq->ift_br[0], budget);
29734c7070dbSScott Long }
29744c7070dbSScott Long 
29754c7070dbSScott Long static uint32_t
29764c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r)
29774c7070dbSScott Long {
29784c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
29794c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
29804c7070dbSScott Long 
29811248952aSSean Bruno 	return ((TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) ||
29824c7070dbSScott Long 		ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, txq->ift_cidx_processed, false));
29834c7070dbSScott Long }
29844c7070dbSScott Long 
29854c7070dbSScott Long static uint32_t
29864c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
29874c7070dbSScott Long {
29884c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
29894c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
29904c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
29914c7070dbSScott Long 	struct mbuf **mp, *m;
29924c7070dbSScott Long 	int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail, err, in_use_prev, desc_used;
29934c7070dbSScott Long 
29944c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
29954c7070dbSScott Long 			    !LINK_ACTIVE(ctx))) {
29964c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_notready);
29974c7070dbSScott Long 		return (0);
29984c7070dbSScott Long 	}
29994c7070dbSScott Long 
30004c7070dbSScott Long 	avail = IDXDIFF(pidx, cidx, r->size);
30014c7070dbSScott Long 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
30024c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_flushing);
30034c7070dbSScott Long 		for (i = 0; i < avail; i++) {
300423ac9029SStephen Hurd 			m_free(r->items[(cidx + i) & (r->size-1)]);
30054c7070dbSScott Long 			r->items[(cidx + i) & (r->size-1)] = NULL;
30064c7070dbSScott Long 		}
30074c7070dbSScott Long 		return (avail);
30084c7070dbSScott Long 	}
30094c7070dbSScott Long 	iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
30104c7070dbSScott Long 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
30114c7070dbSScott Long 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
30124c7070dbSScott Long 		CALLOUT_LOCK(txq);
30134c7070dbSScott Long 		callout_stop(&txq->ift_timer);
30144c7070dbSScott Long 		callout_stop(&txq->ift_db_check);
30154c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
30164c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_oactive);
30174c7070dbSScott Long 		return (0);
30184c7070dbSScott Long 	}
30194c7070dbSScott Long 	consumed = mcast_sent = bytes_sent = pkt_sent = 0;
30204c7070dbSScott Long 	count = MIN(avail, TX_BATCH_SIZE);
3021da69b8f9SSean Bruno #ifdef INVARIANTS
3022da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3023da69b8f9SSean Bruno 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3024da69b8f9SSean Bruno 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3025da69b8f9SSean Bruno #endif
30264c7070dbSScott Long 
30274c7070dbSScott Long 	for (desc_used = i = 0; i < count && TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2; i++) {
30284c7070dbSScott Long 		mp = _ring_peek_one(r, cidx, i);
3029da69b8f9SSean Bruno 		MPASS(mp != NULL && *mp != NULL);
30304c7070dbSScott Long 		in_use_prev = txq->ift_in_use;
3031da69b8f9SSean Bruno 		if ((err = iflib_encap(txq, mp)) == ENOBUFS) {
30324c7070dbSScott Long 			DBG_COUNTER_INC(txq_drain_encapfail);
3033da69b8f9SSean Bruno 			/* no room - bail out */
30344c7070dbSScott Long 			break;
30354c7070dbSScott Long 		}
30364c7070dbSScott Long 		consumed++;
3037da69b8f9SSean Bruno 		if (err) {
3038da69b8f9SSean Bruno 			DBG_COUNTER_INC(txq_drain_encapfail);
3039da69b8f9SSean Bruno 			/* we can't send this packet - skip it */
30404c7070dbSScott Long 			continue;
3041da69b8f9SSean Bruno 		}
30424c7070dbSScott Long 		pkt_sent++;
30434c7070dbSScott Long 		m = *mp;
30444c7070dbSScott Long 		DBG_COUNTER_INC(tx_sent);
30454c7070dbSScott Long 		bytes_sent += m->m_pkthdr.len;
30464c7070dbSScott Long 		if (m->m_flags & M_MCAST)
30474c7070dbSScott Long 			mcast_sent++;
30484c7070dbSScott Long 
30494c7070dbSScott Long 		txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
30504c7070dbSScott Long 		desc_used += (txq->ift_in_use - in_use_prev);
30514c7070dbSScott Long 		iflib_txd_db_check(ctx, txq, FALSE);
30524c7070dbSScott Long 		ETHER_BPF_MTAP(ifp, m);
30534c7070dbSScott Long 		if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
30544c7070dbSScott Long 			break;
30554c7070dbSScott Long 
30561248952aSSean Bruno 		if (desc_used >= TXQ_MAX_DB_CONSUMED(txq->ift_size))
30574c7070dbSScott Long 			break;
30584c7070dbSScott Long 	}
30594c7070dbSScott Long 
30604c7070dbSScott Long 	if ((iflib_min_tx_latency || iflib_txq_min_occupancy(txq)) && txq->ift_db_pending)
30614c7070dbSScott Long 		iflib_txd_db_check(ctx, txq, TRUE);
30621248952aSSean Bruno 	else if ((txq->ift_db_pending || TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx) + 2) &&
30634c7070dbSScott Long 		 (callout_pending(&txq->ift_db_check) == 0)) {
30644c7070dbSScott Long 		txq->ift_db_pending_queued = txq->ift_db_pending;
30654c7070dbSScott Long 		callout_reset_on(&txq->ift_db_check, 1, iflib_txd_deferred_db_check,
30664c7070dbSScott Long 				 txq, txq->ift_db_check.c_cpu);
30674c7070dbSScott Long 	}
30684c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
30694c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
30704c7070dbSScott Long 	if (mcast_sent)
30714c7070dbSScott Long 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3072da69b8f9SSean Bruno #ifdef INVARIANTS
3073da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3074da69b8f9SSean Bruno 		printf("consumed=%d\n", consumed);
3075da69b8f9SSean Bruno #endif
30764c7070dbSScott Long 	return (consumed);
30774c7070dbSScott Long }
30784c7070dbSScott Long 
3079da69b8f9SSean Bruno static uint32_t
3080da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r)
3081da69b8f9SSean Bruno {
3082da69b8f9SSean Bruno 	return (1);
3083da69b8f9SSean Bruno }
3084da69b8f9SSean Bruno 
3085da69b8f9SSean Bruno static uint32_t
3086da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3087da69b8f9SSean Bruno {
3088da69b8f9SSean Bruno 	int i, avail;
3089da69b8f9SSean Bruno 	struct mbuf **mp;
3090da69b8f9SSean Bruno 	iflib_txq_t txq;
3091da69b8f9SSean Bruno 
3092da69b8f9SSean Bruno 	txq = r->cookie;
3093da69b8f9SSean Bruno 
3094da69b8f9SSean Bruno 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3095da69b8f9SSean Bruno 	CALLOUT_LOCK(txq);
3096da69b8f9SSean Bruno 	callout_stop(&txq->ift_timer);
3097da69b8f9SSean Bruno 	callout_stop(&txq->ift_db_check);
3098da69b8f9SSean Bruno 	CALLOUT_UNLOCK(txq);
3099da69b8f9SSean Bruno 
3100da69b8f9SSean Bruno 	avail = IDXDIFF(pidx, cidx, r->size);
3101da69b8f9SSean Bruno 	for (i = 0; i < avail; i++) {
3102da69b8f9SSean Bruno 		mp = _ring_peek_one(r, cidx, i);
3103da69b8f9SSean Bruno 		m_freem(*mp);
3104da69b8f9SSean Bruno 	}
3105da69b8f9SSean Bruno 	MPASS(ifmp_ring_is_stalled(r) == 0);
3106da69b8f9SSean Bruno 	return (avail);
3107da69b8f9SSean Bruno }
3108da69b8f9SSean Bruno 
3109da69b8f9SSean Bruno static void
3110da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq)
3111da69b8f9SSean Bruno {
3112da69b8f9SSean Bruno 	struct ifmp_ring *r;
3113da69b8f9SSean Bruno 
3114da69b8f9SSean Bruno 	r = txq->ift_br[0];
3115da69b8f9SSean Bruno 	r->drain = iflib_txq_drain_free;
3116da69b8f9SSean Bruno 	r->can_drain = iflib_txq_drain_always;
3117da69b8f9SSean Bruno 
3118da69b8f9SSean Bruno 	ifmp_ring_check_drainage(r, r->size);
3119da69b8f9SSean Bruno 
3120da69b8f9SSean Bruno 	r->drain = iflib_txq_drain;
3121da69b8f9SSean Bruno 	r->can_drain = iflib_txq_can_drain;
3122da69b8f9SSean Bruno }
3123da69b8f9SSean Bruno 
31244c7070dbSScott Long static void
312523ac9029SStephen Hurd _task_fn_tx(void *context)
31264c7070dbSScott Long {
31274c7070dbSScott Long 	iflib_txq_t txq = context;
31284c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
31294c7070dbSScott Long 
31301248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
31311248952aSSean Bruno 	txq->ift_cpu_exec_count[curcpu]++;
31321248952aSSean Bruno #endif
31334c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
31344c7070dbSScott Long 		return;
31354c7070dbSScott Long 	ifmp_ring_check_drainage(txq->ift_br[0], TX_BATCH_SIZE);
31364c7070dbSScott Long }
31374c7070dbSScott Long 
31384c7070dbSScott Long static void
313923ac9029SStephen Hurd _task_fn_rx(void *context)
31404c7070dbSScott Long {
31414c7070dbSScott Long 	iflib_rxq_t rxq = context;
31424c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
31434c7070dbSScott Long 	bool more;
314423ac9029SStephen Hurd 	int rc;
31454c7070dbSScott Long 
31461248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
31471248952aSSean Bruno 	rxq->ifr_cpu_exec_count[curcpu]++;
31481248952aSSean Bruno #endif
31494c7070dbSScott Long 	DBG_COUNTER_INC(task_fn_rxs);
31504c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
31514c7070dbSScott Long 		return;
31524c7070dbSScott Long 
31534c7070dbSScott Long 	if ((more = iflib_rxeof(rxq, 16 /* XXX */)) == false) {
31544c7070dbSScott Long 		if (ctx->ifc_flags & IFC_LEGACY)
31554c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
31564c7070dbSScott Long 		else {
31574c7070dbSScott Long 			DBG_COUNTER_INC(rx_intr_enables);
315823ac9029SStephen Hurd 			rc = IFDI_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
315923ac9029SStephen Hurd 			KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
31604c7070dbSScott Long 		}
31614c7070dbSScott Long 	}
31624c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
31634c7070dbSScott Long 		return;
31644c7070dbSScott Long 	if (more)
31654c7070dbSScott Long 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
31664c7070dbSScott Long }
31674c7070dbSScott Long 
31684c7070dbSScott Long static void
316923ac9029SStephen Hurd _task_fn_admin(void *context)
31704c7070dbSScott Long {
31714c7070dbSScott Long 	if_ctx_t ctx = context;
31724c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
31734c7070dbSScott Long 	iflib_txq_t txq;
31744c7070dbSScott Long 	int i;
31754c7070dbSScott Long 
31764c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
31774c7070dbSScott Long 		return;
31784c7070dbSScott Long 
31794c7070dbSScott Long 	CTX_LOCK(ctx);
31804c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
31814c7070dbSScott Long 		CALLOUT_LOCK(txq);
31824c7070dbSScott Long 		callout_stop(&txq->ift_timer);
31834c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
31844c7070dbSScott Long 	}
31854c7070dbSScott Long 	IFDI_UPDATE_ADMIN_STATUS(ctx);
31864c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
31874c7070dbSScott Long 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
31884c7070dbSScott Long 	IFDI_LINK_INTR_ENABLE(ctx);
31894c7070dbSScott Long 	CTX_UNLOCK(ctx);
31904c7070dbSScott Long 
31914c7070dbSScott Long 	if (LINK_ACTIVE(ctx) == 0)
31924c7070dbSScott Long 		return;
31934c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
31944c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
31954c7070dbSScott Long }
31964c7070dbSScott Long 
31974c7070dbSScott Long 
31984c7070dbSScott Long static void
319923ac9029SStephen Hurd _task_fn_iov(void *context)
32004c7070dbSScott Long {
32014c7070dbSScott Long 	if_ctx_t ctx = context;
32024c7070dbSScott Long 
32034c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
32044c7070dbSScott Long 		return;
32054c7070dbSScott Long 
32064c7070dbSScott Long 	CTX_LOCK(ctx);
32074c7070dbSScott Long 	IFDI_VFLR_HANDLE(ctx);
32084c7070dbSScott Long 	CTX_UNLOCK(ctx);
32094c7070dbSScott Long }
32104c7070dbSScott Long 
32114c7070dbSScott Long static int
32124c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
32134c7070dbSScott Long {
32144c7070dbSScott Long 	int err;
32154c7070dbSScott Long 	if_int_delay_info_t info;
32164c7070dbSScott Long 	if_ctx_t ctx;
32174c7070dbSScott Long 
32184c7070dbSScott Long 	info = (if_int_delay_info_t)arg1;
32194c7070dbSScott Long 	ctx = info->iidi_ctx;
32204c7070dbSScott Long 	info->iidi_req = req;
32214c7070dbSScott Long 	info->iidi_oidp = oidp;
32224c7070dbSScott Long 	CTX_LOCK(ctx);
32234c7070dbSScott Long 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
32244c7070dbSScott Long 	CTX_UNLOCK(ctx);
32254c7070dbSScott Long 	return (err);
32264c7070dbSScott Long }
32274c7070dbSScott Long 
32284c7070dbSScott Long /*********************************************************************
32294c7070dbSScott Long  *
32304c7070dbSScott Long  *  IFNET FUNCTIONS
32314c7070dbSScott Long  *
32324c7070dbSScott Long  **********************************************************************/
32334c7070dbSScott Long 
32344c7070dbSScott Long static void
32354c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx)
32364c7070dbSScott Long {
32374c7070dbSScott Long 	iflib_stop(ctx);
32384c7070dbSScott Long 	iflib_init_locked(ctx);
32394c7070dbSScott Long }
32404c7070dbSScott Long 
32414c7070dbSScott Long 
32424c7070dbSScott Long static void
32434c7070dbSScott Long iflib_if_init(void *arg)
32444c7070dbSScott Long {
32454c7070dbSScott Long 	if_ctx_t ctx = arg;
32464c7070dbSScott Long 
32474c7070dbSScott Long 	CTX_LOCK(ctx);
32484c7070dbSScott Long 	iflib_if_init_locked(ctx);
32494c7070dbSScott Long 	CTX_UNLOCK(ctx);
32504c7070dbSScott Long }
32514c7070dbSScott Long 
32524c7070dbSScott Long static int
32534c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m)
32544c7070dbSScott Long {
32554c7070dbSScott Long 	if_ctx_t	ctx = if_getsoftc(ifp);
32564c7070dbSScott Long 
32574c7070dbSScott Long 	iflib_txq_t txq;
325823ac9029SStephen Hurd 	int err, qidx;
32594c7070dbSScott Long 
32604c7070dbSScott Long 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
32614c7070dbSScott Long 		DBG_COUNTER_INC(tx_frees);
32624c7070dbSScott Long 		m_freem(m);
3263da69b8f9SSean Bruno 		return (ENOBUFS);
32644c7070dbSScott Long 	}
32654c7070dbSScott Long 
326623ac9029SStephen Hurd 	MPASS(m->m_nextpkt == NULL);
32674c7070dbSScott Long 	qidx = 0;
32684c7070dbSScott Long 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
32694c7070dbSScott Long 		qidx = QIDX(ctx, m);
32704c7070dbSScott Long 	/*
32714c7070dbSScott Long 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
32724c7070dbSScott Long 	 */
32734c7070dbSScott Long 	txq = &ctx->ifc_txqs[qidx];
32744c7070dbSScott Long 
32754c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
32764c7070dbSScott Long 	if (txq->ift_closed) {
32774c7070dbSScott Long 		while (m != NULL) {
32784c7070dbSScott Long 			next = m->m_nextpkt;
32794c7070dbSScott Long 			m->m_nextpkt = NULL;
32804c7070dbSScott Long 			m_freem(m);
32814c7070dbSScott Long 			m = next;
32824c7070dbSScott Long 		}
32834c7070dbSScott Long 		return (ENOBUFS);
32844c7070dbSScott Long 	}
32854c7070dbSScott Long #endif
328623ac9029SStephen Hurd #ifdef notyet
32874c7070dbSScott Long 	qidx = count = 0;
32884c7070dbSScott Long 	mp = marr;
32894c7070dbSScott Long 	next = m;
32904c7070dbSScott Long 	do {
32914c7070dbSScott Long 		count++;
32924c7070dbSScott Long 		next = next->m_nextpkt;
32934c7070dbSScott Long 	} while (next != NULL);
32944c7070dbSScott Long 
329516fb86abSConrad Meyer 	if (count > nitems(marr))
32964c7070dbSScott Long 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
32974c7070dbSScott Long 			/* XXX check nextpkt */
32984c7070dbSScott Long 			m_freem(m);
32994c7070dbSScott Long 			/* XXX simplify for now */
33004c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
33014c7070dbSScott Long 			return (ENOBUFS);
33024c7070dbSScott Long 		}
33034c7070dbSScott Long 	for (next = m, i = 0; next != NULL; i++) {
33044c7070dbSScott Long 		mp[i] = next;
33054c7070dbSScott Long 		next = next->m_nextpkt;
33064c7070dbSScott Long 		mp[i]->m_nextpkt = NULL;
33074c7070dbSScott Long 	}
330823ac9029SStephen Hurd #endif
33094c7070dbSScott Long 	DBG_COUNTER_INC(tx_seen);
331023ac9029SStephen Hurd 	err = ifmp_ring_enqueue(txq->ift_br[0], (void **)&m, 1, TX_BATCH_SIZE);
33114c7070dbSScott Long 
33124c7070dbSScott Long 	if (err) {
331323ac9029SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
33144c7070dbSScott Long 		/* support forthcoming later */
33154c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
33164c7070dbSScott Long 		txq->ift_closed = TRUE;
33174c7070dbSScott Long #endif
33184c7070dbSScott Long 		ifmp_ring_check_drainage(txq->ift_br[0], TX_BATCH_SIZE);
331923ac9029SStephen Hurd 		m_freem(m);
332023ac9029SStephen Hurd 	} else if (TXQ_AVAIL(txq) < (txq->ift_size >> 1)) {
332123ac9029SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
33224c7070dbSScott Long 	}
33234c7070dbSScott Long 
33244c7070dbSScott Long 	return (err);
33254c7070dbSScott Long }
33264c7070dbSScott Long 
33274c7070dbSScott Long static void
33284c7070dbSScott Long iflib_if_qflush(if_t ifp)
33294c7070dbSScott Long {
33304c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
33314c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
33324c7070dbSScott Long 	int i;
33334c7070dbSScott Long 
33344c7070dbSScott Long 	CTX_LOCK(ctx);
33354c7070dbSScott Long 	ctx->ifc_flags |= IFC_QFLUSH;
33364c7070dbSScott Long 	CTX_UNLOCK(ctx);
33374c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
33384c7070dbSScott Long 		while (!(ifmp_ring_is_idle(txq->ift_br[0]) || ifmp_ring_is_stalled(txq->ift_br[0])))
33394c7070dbSScott Long 			iflib_txq_check_drain(txq, 0);
33404c7070dbSScott Long 	CTX_LOCK(ctx);
33414c7070dbSScott Long 	ctx->ifc_flags &= ~IFC_QFLUSH;
33424c7070dbSScott Long 	CTX_UNLOCK(ctx);
33434c7070dbSScott Long 
33444c7070dbSScott Long 	if_qflush(ifp);
33454c7070dbSScott Long }
33464c7070dbSScott Long 
33474c7070dbSScott Long 
334823ac9029SStephen Hurd #define IFCAP_FLAGS (IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
33494c7070dbSScott Long 		     IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTAGGING |	\
33504c7070dbSScott Long 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO)
33514c7070dbSScott Long 
33524c7070dbSScott Long static int
33534c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
33544c7070dbSScott Long {
33554c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
33564c7070dbSScott Long 	struct ifreq	*ifr = (struct ifreq *)data;
33574c7070dbSScott Long #if defined(INET) || defined(INET6)
33584c7070dbSScott Long 	struct ifaddr	*ifa = (struct ifaddr *)data;
33594c7070dbSScott Long #endif
33604c7070dbSScott Long 	bool		avoid_reset = FALSE;
33614c7070dbSScott Long 	int		err = 0, reinit = 0, bits;
33624c7070dbSScott Long 
33634c7070dbSScott Long 	switch (command) {
33644c7070dbSScott Long 	case SIOCSIFADDR:
33654c7070dbSScott Long #ifdef INET
33664c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET)
33674c7070dbSScott Long 			avoid_reset = TRUE;
33684c7070dbSScott Long #endif
33694c7070dbSScott Long #ifdef INET6
33704c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET6)
33714c7070dbSScott Long 			avoid_reset = TRUE;
33724c7070dbSScott Long #endif
33734c7070dbSScott Long 		/*
33744c7070dbSScott Long 		** Calling init results in link renegotiation,
33754c7070dbSScott Long 		** so we avoid doing it when possible.
33764c7070dbSScott Long 		*/
33774c7070dbSScott Long 		if (avoid_reset) {
33784c7070dbSScott Long 			if_setflagbits(ifp, IFF_UP,0);
33794c7070dbSScott Long 			if (!(if_getdrvflags(ifp)& IFF_DRV_RUNNING))
33804c7070dbSScott Long 				reinit = 1;
33814c7070dbSScott Long #ifdef INET
33824c7070dbSScott Long 			if (!(if_getflags(ifp) & IFF_NOARP))
33834c7070dbSScott Long 				arp_ifinit(ifp, ifa);
33844c7070dbSScott Long #endif
33854c7070dbSScott Long 		} else
33864c7070dbSScott Long 			err = ether_ioctl(ifp, command, data);
33874c7070dbSScott Long 		break;
33884c7070dbSScott Long 	case SIOCSIFMTU:
33894c7070dbSScott Long 		CTX_LOCK(ctx);
33904c7070dbSScott Long 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
33914c7070dbSScott Long 			CTX_UNLOCK(ctx);
33924c7070dbSScott Long 			break;
33934c7070dbSScott Long 		}
33944c7070dbSScott Long 		bits = if_getdrvflags(ifp);
33954c7070dbSScott Long 		/* stop the driver and free any clusters before proceeding */
33964c7070dbSScott Long 		iflib_stop(ctx);
33974c7070dbSScott Long 
33984c7070dbSScott Long 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
33994c7070dbSScott Long 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
34004c7070dbSScott Long 				ctx->ifc_flags |= IFC_MULTISEG;
34014c7070dbSScott Long 			else
34024c7070dbSScott Long 				ctx->ifc_flags &= ~IFC_MULTISEG;
34034c7070dbSScott Long 			err = if_setmtu(ifp, ifr->ifr_mtu);
34044c7070dbSScott Long 		}
34054c7070dbSScott Long 		iflib_init_locked(ctx);
34064c7070dbSScott Long 		if_setdrvflags(ifp, bits);
34074c7070dbSScott Long 		CTX_UNLOCK(ctx);
34084c7070dbSScott Long 		break;
34094c7070dbSScott Long 	case SIOCSIFFLAGS:
34104c7070dbSScott Long 		CTX_LOCK(ctx);
34114c7070dbSScott Long 		if (if_getflags(ifp) & IFF_UP) {
34124c7070dbSScott Long 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
34134c7070dbSScott Long 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
34144c7070dbSScott Long 				    (IFF_PROMISC | IFF_ALLMULTI)) {
34154c7070dbSScott Long 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
34164c7070dbSScott Long 				}
34174c7070dbSScott Long 			} else
34184c7070dbSScott Long 				reinit = 1;
34194c7070dbSScott Long 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
34204c7070dbSScott Long 			iflib_stop(ctx);
34214c7070dbSScott Long 		}
34224c7070dbSScott Long 		ctx->ifc_if_flags = if_getflags(ifp);
34234c7070dbSScott Long 		CTX_UNLOCK(ctx);
34244c7070dbSScott Long 		break;
34254c7070dbSScott Long 
34264c7070dbSScott Long 		break;
34274c7070dbSScott Long 	case SIOCADDMULTI:
34284c7070dbSScott Long 	case SIOCDELMULTI:
34294c7070dbSScott Long 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
34304c7070dbSScott Long 			CTX_LOCK(ctx);
34314c7070dbSScott Long 			IFDI_INTR_DISABLE(ctx);
34324c7070dbSScott Long 			IFDI_MULTI_SET(ctx);
34334c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
34344c7070dbSScott Long 			CTX_UNLOCK(ctx);
34354c7070dbSScott Long 		}
34364c7070dbSScott Long 		break;
34374c7070dbSScott Long 	case SIOCSIFMEDIA:
34384c7070dbSScott Long 		CTX_LOCK(ctx);
34394c7070dbSScott Long 		IFDI_MEDIA_SET(ctx);
34404c7070dbSScott Long 		CTX_UNLOCK(ctx);
34414c7070dbSScott Long 		/* falls thru */
34424c7070dbSScott Long 	case SIOCGIFMEDIA:
34434c7070dbSScott Long 		err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command);
34444c7070dbSScott Long 		break;
34454c7070dbSScott Long 	case SIOCGI2C:
34464c7070dbSScott Long 	{
34474c7070dbSScott Long 		struct ifi2creq i2c;
34484c7070dbSScott Long 
34494c7070dbSScott Long 		err = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
34504c7070dbSScott Long 		if (err != 0)
34514c7070dbSScott Long 			break;
34524c7070dbSScott Long 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
34534c7070dbSScott Long 			err = EINVAL;
34544c7070dbSScott Long 			break;
34554c7070dbSScott Long 		}
34564c7070dbSScott Long 		if (i2c.len > sizeof(i2c.data)) {
34574c7070dbSScott Long 			err = EINVAL;
34584c7070dbSScott Long 			break;
34594c7070dbSScott Long 		}
34604c7070dbSScott Long 
34614c7070dbSScott Long 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
34624c7070dbSScott Long 			err = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
34634c7070dbSScott Long 		break;
34644c7070dbSScott Long 	}
34654c7070dbSScott Long 	case SIOCSIFCAP:
34664c7070dbSScott Long 	{
34674c7070dbSScott Long 		int mask, setmask;
34684c7070dbSScott Long 
34694c7070dbSScott Long 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
34704c7070dbSScott Long 		setmask = 0;
34714c7070dbSScott Long #ifdef TCP_OFFLOAD
34724c7070dbSScott Long 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
34734c7070dbSScott Long #endif
34744c7070dbSScott Long 		setmask |= (mask & IFCAP_FLAGS);
34754c7070dbSScott Long 
34768b2a1db9SSean Bruno 		if (setmask  & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
34778b2a1db9SSean Bruno 			setmask |= (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
34784c7070dbSScott Long 		if ((mask & IFCAP_WOL) &&
34794c7070dbSScott Long 		    (if_getcapabilities(ifp) & IFCAP_WOL) != 0)
34804c7070dbSScott Long 			setmask |= (mask & (IFCAP_WOL_MCAST|IFCAP_WOL_MAGIC));
34814c7070dbSScott Long 		if_vlancap(ifp);
34824c7070dbSScott Long 		/*
34834c7070dbSScott Long 		 * want to ensure that traffic has stopped before we change any of the flags
34844c7070dbSScott Long 		 */
34854c7070dbSScott Long 		if (setmask) {
34864c7070dbSScott Long 			CTX_LOCK(ctx);
34874c7070dbSScott Long 			bits = if_getdrvflags(ifp);
34888b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
34894c7070dbSScott Long 				iflib_stop(ctx);
34904c7070dbSScott Long 			if_togglecapenable(ifp, setmask);
34918b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
34924c7070dbSScott Long 				iflib_init_locked(ctx);
34934c7070dbSScott Long 			if_setdrvflags(ifp, bits);
34944c7070dbSScott Long 			CTX_UNLOCK(ctx);
34954c7070dbSScott Long 		}
34964c7070dbSScott Long 		break;
34974c7070dbSScott Long 	    }
34984c7070dbSScott Long 	case SIOCGPRIVATE_0:
34994c7070dbSScott Long 	case SIOCSDRVSPEC:
35004c7070dbSScott Long 	case SIOCGDRVSPEC:
35014c7070dbSScott Long 		CTX_LOCK(ctx);
35024c7070dbSScott Long 		err = IFDI_PRIV_IOCTL(ctx, command, data);
35034c7070dbSScott Long 		CTX_UNLOCK(ctx);
35044c7070dbSScott Long 		break;
35054c7070dbSScott Long 	default:
35064c7070dbSScott Long 		err = ether_ioctl(ifp, command, data);
35074c7070dbSScott Long 		break;
35084c7070dbSScott Long 	}
35094c7070dbSScott Long 	if (reinit)
35104c7070dbSScott Long 		iflib_if_init(ctx);
35114c7070dbSScott Long 	return (err);
35124c7070dbSScott Long }
35134c7070dbSScott Long 
35144c7070dbSScott Long static uint64_t
35154c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt)
35164c7070dbSScott Long {
35174c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
35184c7070dbSScott Long 
35194c7070dbSScott Long 	return (IFDI_GET_COUNTER(ctx, cnt));
35204c7070dbSScott Long }
35214c7070dbSScott Long 
35224c7070dbSScott Long /*********************************************************************
35234c7070dbSScott Long  *
35244c7070dbSScott Long  *  OTHER FUNCTIONS EXPORTED TO THE STACK
35254c7070dbSScott Long  *
35264c7070dbSScott Long  **********************************************************************/
35274c7070dbSScott Long 
35284c7070dbSScott Long static void
35294c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
35304c7070dbSScott Long {
35314c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
35324c7070dbSScott Long 
35334c7070dbSScott Long 	if ((void *)ctx != arg)
35344c7070dbSScott Long 		return;
35354c7070dbSScott Long 
35364c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
35374c7070dbSScott Long 		return;
35384c7070dbSScott Long 
35394c7070dbSScott Long 	CTX_LOCK(ctx);
35404c7070dbSScott Long 	IFDI_VLAN_REGISTER(ctx, vtag);
35414c7070dbSScott Long 	/* Re-init to load the changes */
35424c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
35434c7070dbSScott Long 		iflib_init_locked(ctx);
35444c7070dbSScott Long 	CTX_UNLOCK(ctx);
35454c7070dbSScott Long }
35464c7070dbSScott Long 
35474c7070dbSScott Long static void
35484c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
35494c7070dbSScott Long {
35504c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
35514c7070dbSScott Long 
35524c7070dbSScott Long 	if ((void *)ctx != arg)
35534c7070dbSScott Long 		return;
35544c7070dbSScott Long 
35554c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
35564c7070dbSScott Long 		return;
35574c7070dbSScott Long 
35584c7070dbSScott Long 	CTX_LOCK(ctx);
35594c7070dbSScott Long 	IFDI_VLAN_UNREGISTER(ctx, vtag);
35604c7070dbSScott Long 	/* Re-init to load the changes */
35614c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
35624c7070dbSScott Long 		iflib_init_locked(ctx);
35634c7070dbSScott Long 	CTX_UNLOCK(ctx);
35644c7070dbSScott Long }
35654c7070dbSScott Long 
35664c7070dbSScott Long static void
35674c7070dbSScott Long iflib_led_func(void *arg, int onoff)
35684c7070dbSScott Long {
35694c7070dbSScott Long 	if_ctx_t ctx = arg;
35704c7070dbSScott Long 
35714c7070dbSScott Long 	CTX_LOCK(ctx);
35724c7070dbSScott Long 	IFDI_LED_FUNC(ctx, onoff);
35734c7070dbSScott Long 	CTX_UNLOCK(ctx);
35744c7070dbSScott Long }
35754c7070dbSScott Long 
35764c7070dbSScott Long /*********************************************************************
35774c7070dbSScott Long  *
35784c7070dbSScott Long  *  BUS FUNCTION DEFINITIONS
35794c7070dbSScott Long  *
35804c7070dbSScott Long  **********************************************************************/
35814c7070dbSScott Long 
35824c7070dbSScott Long int
35834c7070dbSScott Long iflib_device_probe(device_t dev)
35844c7070dbSScott Long {
35854c7070dbSScott Long 	pci_vendor_info_t *ent;
35864c7070dbSScott Long 
35874c7070dbSScott Long 	uint16_t	pci_vendor_id, pci_device_id;
35884c7070dbSScott Long 	uint16_t	pci_subvendor_id, pci_subdevice_id;
35894c7070dbSScott Long 	uint16_t	pci_rev_id;
35904c7070dbSScott Long 	if_shared_ctx_t sctx;
35914c7070dbSScott Long 
35924c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
35934c7070dbSScott Long 		return (ENOTSUP);
35944c7070dbSScott Long 
35954c7070dbSScott Long 	pci_vendor_id = pci_get_vendor(dev);
35964c7070dbSScott Long 	pci_device_id = pci_get_device(dev);
35974c7070dbSScott Long 	pci_subvendor_id = pci_get_subvendor(dev);
35984c7070dbSScott Long 	pci_subdevice_id = pci_get_subdevice(dev);
35994c7070dbSScott Long 	pci_rev_id = pci_get_revid(dev);
36004c7070dbSScott Long 	if (sctx->isc_parse_devinfo != NULL)
36014c7070dbSScott Long 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
36024c7070dbSScott Long 
36034c7070dbSScott Long 	ent = sctx->isc_vendor_info;
36044c7070dbSScott Long 	while (ent->pvi_vendor_id != 0) {
36054c7070dbSScott Long 		if (pci_vendor_id != ent->pvi_vendor_id) {
36064c7070dbSScott Long 			ent++;
36074c7070dbSScott Long 			continue;
36084c7070dbSScott Long 		}
36094c7070dbSScott Long 		if ((pci_device_id == ent->pvi_device_id) &&
36104c7070dbSScott Long 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
36114c7070dbSScott Long 		     (ent->pvi_subvendor_id == 0)) &&
36124c7070dbSScott Long 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
36134c7070dbSScott Long 		     (ent->pvi_subdevice_id == 0)) &&
36144c7070dbSScott Long 		    ((pci_rev_id == ent->pvi_rev_id) ||
36154c7070dbSScott Long 		     (ent->pvi_rev_id == 0))) {
36164c7070dbSScott Long 
36174c7070dbSScott Long 			device_set_desc_copy(dev, ent->pvi_name);
36184c7070dbSScott Long 			/* this needs to be changed to zero if the bus probing code
36194c7070dbSScott Long 			 * ever stops re-probing on best match because the sctx
36204c7070dbSScott Long 			 * may have its values over written by register calls
36214c7070dbSScott Long 			 * in subsequent probes
36224c7070dbSScott Long 			 */
36234c7070dbSScott Long 			return (BUS_PROBE_DEFAULT);
36244c7070dbSScott Long 		}
36254c7070dbSScott Long 		ent++;
36264c7070dbSScott Long 	}
36274c7070dbSScott Long 	return (ENXIO);
36284c7070dbSScott Long }
36294c7070dbSScott Long 
36304c7070dbSScott Long int
36314c7070dbSScott Long iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
36324c7070dbSScott Long {
36334c7070dbSScott Long 	int err, rid, msix, msix_bar;
36344c7070dbSScott Long 	if_ctx_t ctx;
36354c7070dbSScott Long 	if_t ifp;
36364c7070dbSScott Long 	if_softc_ctx_t scctx;
363723ac9029SStephen Hurd 	int i;
363823ac9029SStephen Hurd 	uint16_t main_txq;
363923ac9029SStephen Hurd 	uint16_t main_rxq;
36404c7070dbSScott Long 
36414c7070dbSScott Long 
36424c7070dbSScott Long 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
36434c7070dbSScott Long 
36444c7070dbSScott Long 	if (sc == NULL) {
36454c7070dbSScott Long 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
36464c7070dbSScott Long 		device_set_softc(dev, ctx);
364723ac9029SStephen Hurd 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
36484c7070dbSScott Long 	}
36494c7070dbSScott Long 
36504c7070dbSScott Long 	ctx->ifc_sctx = sctx;
36514c7070dbSScott Long 	ctx->ifc_dev = dev;
36524c7070dbSScott Long 	ctx->ifc_softc = sc;
36534c7070dbSScott Long 
36544c7070dbSScott Long 	if ((err = iflib_register(ctx)) != 0) {
36554c7070dbSScott Long 		device_printf(dev, "iflib_register failed %d\n", err);
36564c7070dbSScott Long 		return (err);
36574c7070dbSScott Long 	}
36584c7070dbSScott Long 	iflib_add_device_sysctl_pre(ctx);
365923ac9029SStephen Hurd 
366023ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
36611248952aSSean Bruno 	ifp = ctx->ifc_ifp;
36621248952aSSean Bruno 
366323ac9029SStephen Hurd 	/*
366423ac9029SStephen Hurd 	 * XXX sanity check that ntxd & nrxd are a power of 2
366523ac9029SStephen Hurd 	 */
366623ac9029SStephen Hurd 	if (ctx->ifc_sysctl_ntxqs != 0)
366723ac9029SStephen Hurd 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
366823ac9029SStephen Hurd 	if (ctx->ifc_sysctl_nrxqs != 0)
366923ac9029SStephen Hurd 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
367023ac9029SStephen Hurd 
367123ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
367223ac9029SStephen Hurd 		if (ctx->ifc_sysctl_ntxds[i] != 0)
367323ac9029SStephen Hurd 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
367423ac9029SStephen Hurd 		else
367523ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
367623ac9029SStephen Hurd 	}
367723ac9029SStephen Hurd 
367823ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
367923ac9029SStephen Hurd 		if (ctx->ifc_sysctl_nrxds[i] != 0)
368023ac9029SStephen Hurd 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
368123ac9029SStephen Hurd 		else
368223ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
368323ac9029SStephen Hurd 	}
368423ac9029SStephen Hurd 
368523ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
368623ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
368723ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
368823ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
368923ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
369023ac9029SStephen Hurd 		}
369123ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
369223ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
369323ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
369423ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
369523ac9029SStephen Hurd 		}
369623ac9029SStephen Hurd 	}
369723ac9029SStephen Hurd 
369823ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
369923ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
370023ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
370123ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
370223ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
370323ac9029SStephen Hurd 		}
370423ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
370523ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
370623ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
370723ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
370823ac9029SStephen Hurd 		}
370923ac9029SStephen Hurd 	}
371023ac9029SStephen Hurd 
37114c7070dbSScott Long 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
37124c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
37134c7070dbSScott Long 		return (err);
37144c7070dbSScott Long 	}
37151248952aSSean Bruno 	_iflib_pre_assert(scctx);
37161248952aSSean Bruno 	ctx->ifc_txrx = *scctx->isc_txrx;
37171248952aSSean Bruno 
37181248952aSSean Bruno #ifdef INVARIANTS
37191248952aSSean Bruno 	MPASS(scctx->isc_capenable);
37201248952aSSean Bruno 	if (scctx->isc_capenable & IFCAP_TXCSUM)
37211248952aSSean Bruno 		MPASS(scctx->isc_tx_csum_flags);
37221248952aSSean Bruno #endif
37231248952aSSean Bruno 
37241248952aSSean Bruno 	if_setcapabilities(ifp, scctx->isc_capenable);
37251248952aSSean Bruno 	if_setcapenable(ifp, scctx->isc_capenable);
37261248952aSSean Bruno 
37271248952aSSean Bruno 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
37281248952aSSean Bruno 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
37291248952aSSean Bruno 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
37301248952aSSean Bruno 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
373123ac9029SStephen Hurd 
37324c7070dbSScott Long #ifdef ACPI_DMAR
37334c7070dbSScott Long 	if (dmar_get_dma_tag(device_get_parent(dev), dev) != NULL)
37344c7070dbSScott Long 		ctx->ifc_flags |= IFC_DMAR;
37354c7070dbSScott Long #endif
37364c7070dbSScott Long 
37374c7070dbSScott Long 	msix_bar = scctx->isc_msix_bar;
37384c7070dbSScott Long 
373923ac9029SStephen Hurd 	if(sctx->isc_flags & IFLIB_HAS_TXCQ)
374023ac9029SStephen Hurd 		main_txq = 1;
374123ac9029SStephen Hurd 	else
374223ac9029SStephen Hurd 		main_txq = 0;
374323ac9029SStephen Hurd 
374423ac9029SStephen Hurd 	if(sctx->isc_flags & IFLIB_HAS_RXCQ)
374523ac9029SStephen Hurd 		main_rxq = 1;
374623ac9029SStephen Hurd 	else
374723ac9029SStephen Hurd 		main_rxq = 0;
374823ac9029SStephen Hurd 
374923ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
375023ac9029SStephen Hurd 	device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
375123ac9029SStephen Hurd 		      scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
375223ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
375323ac9029SStephen Hurd 		if (!powerof2(scctx->isc_nrxd[i])) {
375423ac9029SStephen Hurd 			/* round down instead? */
375523ac9029SStephen Hurd 			device_printf(dev, "# rx descriptors must be a power of 2\n");
375623ac9029SStephen Hurd 			err = EINVAL;
375723ac9029SStephen Hurd 			goto fail;
375823ac9029SStephen Hurd 		}
375923ac9029SStephen Hurd 	}
376023ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
376123ac9029SStephen Hurd 		if (!powerof2(scctx->isc_ntxd[i])) {
376223ac9029SStephen Hurd 			device_printf(dev,
376323ac9029SStephen Hurd 			    "# tx descriptors must be a power of 2");
376423ac9029SStephen Hurd 			err = EINVAL;
376523ac9029SStephen Hurd 			goto fail;
376623ac9029SStephen Hurd 		}
376723ac9029SStephen Hurd 	}
376823ac9029SStephen Hurd 
376923ac9029SStephen Hurd 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
377023ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
377123ac9029SStephen Hurd 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
377223ac9029SStephen Hurd 		    MAX_SINGLE_PACKET_FRACTION);
377323ac9029SStephen Hurd 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
377423ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
377523ac9029SStephen Hurd 		scctx->isc_tx_tso_segments_max = max(1,
377623ac9029SStephen Hurd 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
37774c7070dbSScott Long 
37784c7070dbSScott Long 	/*
37794c7070dbSScott Long 	 * Protect the stack against modern hardware
37804c7070dbSScott Long 	 */
37814c7070dbSScott Long 	if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
37824c7070dbSScott Long 		scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
37834c7070dbSScott Long 
37844c7070dbSScott Long 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
37854c7070dbSScott Long 	ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
37864c7070dbSScott Long 	ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
37874c7070dbSScott Long 	ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
37884c7070dbSScott Long 	if (scctx->isc_rss_table_size == 0)
37894c7070dbSScott Long 		scctx->isc_rss_table_size = 64;
379023ac9029SStephen Hurd 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
3791da69b8f9SSean Bruno 
3792da69b8f9SSean Bruno 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
3793da69b8f9SSean Bruno 	/* XXX format name */
3794da69b8f9SSean Bruno 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
37954c7070dbSScott Long 	/*
37964c7070dbSScott Long 	** Now setup MSI or MSI/X, should
37974c7070dbSScott Long 	** return us the number of supported
37984c7070dbSScott Long 	** vectors. (Will be 1 for MSI)
37994c7070dbSScott Long 	*/
38004c7070dbSScott Long 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
38014c7070dbSScott Long 		msix = scctx->isc_vectors;
38024c7070dbSScott Long 	} else if (scctx->isc_msix_bar != 0)
3803f7ae9a84SSean Bruno 	       /*
3804f7ae9a84SSean Bruno 		* The simple fact that isc_msix_bar is not 0 does not mean we
3805f7ae9a84SSean Bruno 		* we have a good value there that is known to work.
3806f7ae9a84SSean Bruno 		*/
38074c7070dbSScott Long 		msix = iflib_msix_init(ctx);
38084c7070dbSScott Long 	else {
38094c7070dbSScott Long 		scctx->isc_vectors = 1;
38104c7070dbSScott Long 		scctx->isc_ntxqsets = 1;
38114c7070dbSScott Long 		scctx->isc_nrxqsets = 1;
38124c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
38134c7070dbSScott Long 		msix = 0;
38144c7070dbSScott Long 	}
38154c7070dbSScott Long 	/* Get memory for the station queues */
38164c7070dbSScott Long 	if ((err = iflib_queues_alloc(ctx))) {
38174c7070dbSScott Long 		device_printf(dev, "Unable to allocate queue memory\n");
38184c7070dbSScott Long 		goto fail;
38194c7070dbSScott Long 	}
38204c7070dbSScott Long 
38214c7070dbSScott Long 	if ((err = iflib_qset_structures_setup(ctx))) {
38224c7070dbSScott Long 		device_printf(dev, "qset structure setup failed %d\n", err);
38234c7070dbSScott Long 		goto fail_queues;
38244c7070dbSScott Long 	}
382569b7fc3eSSean Bruno 
3826bd84f700SSean Bruno 	/*
3827bd84f700SSean Bruno 	 * Group taskqueues aren't properly set up until SMP is started,
3828bd84f700SSean Bruno 	 * so we disable interrupts until we can handle them post
3829bd84f700SSean Bruno 	 * SI_SUB_SMP.
3830bd84f700SSean Bruno 	 *
3831bd84f700SSean Bruno 	 * XXX: disabling interrupts doesn't actually work, at least for
3832bd84f700SSean Bruno 	 * the non-MSI case.  When they occur before SI_SUB_SMP completes,
3833bd84f700SSean Bruno 	 * we do null handling and depend on this not causing too large an
3834bd84f700SSean Bruno 	 * interrupt storm.
3835bd84f700SSean Bruno 	 */
38361248952aSSean Bruno 	IFDI_INTR_DISABLE(ctx);
38374c7070dbSScott Long 	if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
38384c7070dbSScott Long 		device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
38394c7070dbSScott Long 		goto fail_intr_free;
38404c7070dbSScott Long 	}
38414c7070dbSScott Long 	if (msix <= 1) {
38424c7070dbSScott Long 		rid = 0;
38434c7070dbSScott Long 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
38444c7070dbSScott Long 			MPASS(msix == 1);
38454c7070dbSScott Long 			rid = 1;
38464c7070dbSScott Long 		}
384723ac9029SStephen Hurd 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
38484c7070dbSScott Long 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
38494c7070dbSScott Long 			goto fail_intr_free;
38504c7070dbSScott Long 		}
38514c7070dbSScott Long 	}
38524c7070dbSScott Long 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
38534c7070dbSScott Long 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
38544c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
38554c7070dbSScott Long 		goto fail_detach;
38564c7070dbSScott Long 	}
38574c7070dbSScott Long 	if ((err = iflib_netmap_attach(ctx))) {
38584c7070dbSScott Long 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
38594c7070dbSScott Long 		goto fail_detach;
38604c7070dbSScott Long 	}
38614c7070dbSScott Long 	*ctxp = ctx;
38624c7070dbSScott Long 
386323ac9029SStephen Hurd 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
38644c7070dbSScott Long 	iflib_add_device_sysctl_post(ctx);
38654ecb427aSSean Bruno 	ctx->ifc_flags |= IFC_INIT_DONE;
38664c7070dbSScott Long 	return (0);
38674c7070dbSScott Long fail_detach:
38684c7070dbSScott Long 	ether_ifdetach(ctx->ifc_ifp);
38694c7070dbSScott Long fail_intr_free:
38704c7070dbSScott Long 	if (scctx->isc_intr == IFLIB_INTR_MSIX || scctx->isc_intr == IFLIB_INTR_MSI)
38714c7070dbSScott Long 		pci_release_msi(ctx->ifc_dev);
38724c7070dbSScott Long fail_queues:
38734c7070dbSScott Long 	/* XXX free queues */
38744c7070dbSScott Long fail:
38754c7070dbSScott Long 	IFDI_DETACH(ctx);
38764c7070dbSScott Long 	return (err);
38774c7070dbSScott Long }
38784c7070dbSScott Long 
38794c7070dbSScott Long int
38804c7070dbSScott Long iflib_device_attach(device_t dev)
38814c7070dbSScott Long {
38824c7070dbSScott Long 	if_ctx_t ctx;
38834c7070dbSScott Long 	if_shared_ctx_t sctx;
38844c7070dbSScott Long 
38854c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
38864c7070dbSScott Long 		return (ENOTSUP);
38874c7070dbSScott Long 
38884c7070dbSScott Long 	pci_enable_busmaster(dev);
38894c7070dbSScott Long 
38904c7070dbSScott Long 	return (iflib_device_register(dev, NULL, sctx, &ctx));
38914c7070dbSScott Long }
38924c7070dbSScott Long 
38934c7070dbSScott Long int
38944c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx)
38954c7070dbSScott Long {
38964c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
38974c7070dbSScott Long 	iflib_txq_t txq;
38984c7070dbSScott Long 	iflib_rxq_t rxq;
38994c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
39004c7070dbSScott Long 	int i;
39014c7070dbSScott Long 	struct taskqgroup *tqg;
39024c7070dbSScott Long 
39034c7070dbSScott Long 	/* Make sure VLANS are not using driver */
39044c7070dbSScott Long 	if (if_vlantrunkinuse(ifp)) {
39054c7070dbSScott Long 		device_printf(dev,"Vlan in use, detach first\n");
39064c7070dbSScott Long 		return (EBUSY);
39074c7070dbSScott Long 	}
39084c7070dbSScott Long 
39094c7070dbSScott Long 	CTX_LOCK(ctx);
39104c7070dbSScott Long 	ctx->ifc_in_detach = 1;
39114c7070dbSScott Long 	iflib_stop(ctx);
39124c7070dbSScott Long 	CTX_UNLOCK(ctx);
39134c7070dbSScott Long 
39144c7070dbSScott Long 	/* Unregister VLAN events */
39154c7070dbSScott Long 	if (ctx->ifc_vlan_attach_event != NULL)
39164c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
39174c7070dbSScott Long 	if (ctx->ifc_vlan_detach_event != NULL)
39184c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
39194c7070dbSScott Long 
39204c7070dbSScott Long 	iflib_netmap_detach(ifp);
39214c7070dbSScott Long 	ether_ifdetach(ifp);
39224c7070dbSScott Long 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
39234c7070dbSScott Long 	CTX_LOCK_DESTROY(ctx);
39244c7070dbSScott Long 	if (ctx->ifc_led_dev != NULL)
39254c7070dbSScott Long 		led_destroy(ctx->ifc_led_dev);
39264c7070dbSScott Long 	/* XXX drain any dependent tasks */
39274c7070dbSScott Long 	tqg = qgroup_if_io_tqg;
392823ac9029SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
39294c7070dbSScott Long 		callout_drain(&txq->ift_timer);
39304c7070dbSScott Long 		callout_drain(&txq->ift_db_check);
39314c7070dbSScott Long 		if (txq->ift_task.gt_uniq != NULL)
39324c7070dbSScott Long 			taskqgroup_detach(tqg, &txq->ift_task);
39334c7070dbSScott Long 	}
39344c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
39354c7070dbSScott Long 		if (rxq->ifr_task.gt_uniq != NULL)
39364c7070dbSScott Long 			taskqgroup_detach(tqg, &rxq->ifr_task);
39374c7070dbSScott Long 	}
39384c7070dbSScott Long 	tqg = qgroup_if_config_tqg;
39394c7070dbSScott Long 	if (ctx->ifc_admin_task.gt_uniq != NULL)
39404c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
39414c7070dbSScott Long 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
39424c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
39434c7070dbSScott Long 
39444c7070dbSScott Long 	IFDI_DETACH(ctx);
394523ac9029SStephen Hurd 	device_set_softc(ctx->ifc_dev, NULL);
39464c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
39474c7070dbSScott Long 		pci_release_msi(dev);
39484c7070dbSScott Long 	}
39494c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
39504c7070dbSScott Long 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
39514c7070dbSScott Long 	}
39524c7070dbSScott Long 	if (ctx->ifc_msix_mem != NULL) {
39534c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
39544c7070dbSScott Long 			ctx->ifc_softc_ctx.isc_msix_bar, ctx->ifc_msix_mem);
39554c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
39564c7070dbSScott Long 	}
39574c7070dbSScott Long 
39584c7070dbSScott Long 	bus_generic_detach(dev);
39594c7070dbSScott Long 	if_free(ifp);
39604c7070dbSScott Long 
39614c7070dbSScott Long 	iflib_tx_structures_free(ctx);
39624c7070dbSScott Long 	iflib_rx_structures_free(ctx);
396323ac9029SStephen Hurd 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
396423ac9029SStephen Hurd 		free(ctx->ifc_softc, M_IFLIB);
396523ac9029SStephen Hurd 	free(ctx, M_IFLIB);
39664c7070dbSScott Long 	return (0);
39674c7070dbSScott Long }
39684c7070dbSScott Long 
39694c7070dbSScott Long 
39704c7070dbSScott Long int
39714c7070dbSScott Long iflib_device_detach(device_t dev)
39724c7070dbSScott Long {
39734c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39744c7070dbSScott Long 
39754c7070dbSScott Long 	return (iflib_device_deregister(ctx));
39764c7070dbSScott Long }
39774c7070dbSScott Long 
39784c7070dbSScott Long int
39794c7070dbSScott Long iflib_device_suspend(device_t dev)
39804c7070dbSScott Long {
39814c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39824c7070dbSScott Long 
39834c7070dbSScott Long 	CTX_LOCK(ctx);
39844c7070dbSScott Long 	IFDI_SUSPEND(ctx);
39854c7070dbSScott Long 	CTX_UNLOCK(ctx);
39864c7070dbSScott Long 
39874c7070dbSScott Long 	return bus_generic_suspend(dev);
39884c7070dbSScott Long }
39894c7070dbSScott Long int
39904c7070dbSScott Long iflib_device_shutdown(device_t dev)
39914c7070dbSScott Long {
39924c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39934c7070dbSScott Long 
39944c7070dbSScott Long 	CTX_LOCK(ctx);
39954c7070dbSScott Long 	IFDI_SHUTDOWN(ctx);
39964c7070dbSScott Long 	CTX_UNLOCK(ctx);
39974c7070dbSScott Long 
39984c7070dbSScott Long 	return bus_generic_suspend(dev);
39994c7070dbSScott Long }
40004c7070dbSScott Long 
40014c7070dbSScott Long 
40024c7070dbSScott Long int
40034c7070dbSScott Long iflib_device_resume(device_t dev)
40044c7070dbSScott Long {
40054c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
40064c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
40074c7070dbSScott Long 
40084c7070dbSScott Long 	CTX_LOCK(ctx);
40094c7070dbSScott Long 	IFDI_RESUME(ctx);
40104c7070dbSScott Long 	iflib_init_locked(ctx);
40114c7070dbSScott Long 	CTX_UNLOCK(ctx);
40124c7070dbSScott Long 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
40134c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
40144c7070dbSScott Long 
40154c7070dbSScott Long 	return (bus_generic_resume(dev));
40164c7070dbSScott Long }
40174c7070dbSScott Long 
40184c7070dbSScott Long int
40194c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
40204c7070dbSScott Long {
40214c7070dbSScott Long 	int error;
40224c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
40234c7070dbSScott Long 
40244c7070dbSScott Long 	CTX_LOCK(ctx);
40254c7070dbSScott Long 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
40264c7070dbSScott Long 	CTX_UNLOCK(ctx);
40274c7070dbSScott Long 
40284c7070dbSScott Long 	return (error);
40294c7070dbSScott Long }
40304c7070dbSScott Long 
40314c7070dbSScott Long void
40324c7070dbSScott Long iflib_device_iov_uninit(device_t dev)
40334c7070dbSScott Long {
40344c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
40354c7070dbSScott Long 
40364c7070dbSScott Long 	CTX_LOCK(ctx);
40374c7070dbSScott Long 	IFDI_IOV_UNINIT(ctx);
40384c7070dbSScott Long 	CTX_UNLOCK(ctx);
40394c7070dbSScott Long }
40404c7070dbSScott Long 
40414c7070dbSScott Long int
40424c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
40434c7070dbSScott Long {
40444c7070dbSScott Long 	int error;
40454c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
40464c7070dbSScott Long 
40474c7070dbSScott Long 	CTX_LOCK(ctx);
40484c7070dbSScott Long 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
40494c7070dbSScott Long 	CTX_UNLOCK(ctx);
40504c7070dbSScott Long 
40514c7070dbSScott Long 	return (error);
40524c7070dbSScott Long }
40534c7070dbSScott Long 
40544c7070dbSScott Long /*********************************************************************
40554c7070dbSScott Long  *
40564c7070dbSScott Long  *  MODULE FUNCTION DEFINITIONS
40574c7070dbSScott Long  *
40584c7070dbSScott Long  **********************************************************************/
40594c7070dbSScott Long 
40604c7070dbSScott Long /*
40614c7070dbSScott Long  * - Start a fast taskqueue thread for each core
40624c7070dbSScott Long  * - Start a taskqueue for control operations
40634c7070dbSScott Long  */
40644c7070dbSScott Long static int
40654c7070dbSScott Long iflib_module_init(void)
40664c7070dbSScott Long {
40674c7070dbSScott Long 	return (0);
40684c7070dbSScott Long }
40694c7070dbSScott Long 
40704c7070dbSScott Long static int
40714c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg)
40724c7070dbSScott Long {
40734c7070dbSScott Long 	int err;
40744c7070dbSScott Long 
40754c7070dbSScott Long 	switch (what) {
40764c7070dbSScott Long 	case MOD_LOAD:
40774c7070dbSScott Long 		if ((err = iflib_module_init()) != 0)
40784c7070dbSScott Long 			return (err);
40794c7070dbSScott Long 		break;
40804c7070dbSScott Long 	case MOD_UNLOAD:
40814c7070dbSScott Long 		return (EBUSY);
40824c7070dbSScott Long 	default:
40834c7070dbSScott Long 		return (EOPNOTSUPP);
40844c7070dbSScott Long 	}
40854c7070dbSScott Long 
40864c7070dbSScott Long 	return (0);
40874c7070dbSScott Long }
40884c7070dbSScott Long 
40894c7070dbSScott Long /*********************************************************************
40904c7070dbSScott Long  *
40914c7070dbSScott Long  *  PUBLIC FUNCTION DEFINITIONS
40924c7070dbSScott Long  *     ordered as in iflib.h
40934c7070dbSScott Long  *
40944c7070dbSScott Long  **********************************************************************/
40954c7070dbSScott Long 
40964c7070dbSScott Long 
40974c7070dbSScott Long static void
40984c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx)
40994c7070dbSScott Long {
41004c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsize);
41014c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsegsize);
41024c7070dbSScott Long 
41034c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsize);
41044c7070dbSScott Long 	MPASS(sctx->isc_rx_nsegments);
41054c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsegsize);
41064c7070dbSScott Long 
410723ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_min[0]);
410823ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_max[0]);
410923ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_default[0]);
411023ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_min[0]);
411123ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_max[0]);
411223ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_default[0]);
41134c7070dbSScott Long }
41144c7070dbSScott Long 
41151248952aSSean Bruno static void
41161248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx)
41171248952aSSean Bruno {
41181248952aSSean Bruno 
41191248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_encap);
41201248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_flush);
41211248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
41221248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_available);
41231248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
41241248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_refill);
41251248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_flush);
41261248952aSSean Bruno }
41272fe66646SSean Bruno 
41284c7070dbSScott Long static int
41294c7070dbSScott Long iflib_register(if_ctx_t ctx)
41304c7070dbSScott Long {
41314c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
41324c7070dbSScott Long 	driver_t *driver = sctx->isc_driver;
41334c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
41344c7070dbSScott Long 	if_t ifp;
41354c7070dbSScott Long 
41364c7070dbSScott Long 	_iflib_assert(sctx);
41374c7070dbSScott Long 
41384c7070dbSScott Long 	CTX_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
41394c7070dbSScott Long 
41404c7070dbSScott Long 	ifp = ctx->ifc_ifp = if_gethandle(IFT_ETHER);
41414c7070dbSScott Long 	if (ifp == NULL) {
41424c7070dbSScott Long 		device_printf(dev, "can not allocate ifnet structure\n");
41434c7070dbSScott Long 		return (ENOMEM);
41444c7070dbSScott Long 	}
41454c7070dbSScott Long 
41464c7070dbSScott Long 	/*
41474c7070dbSScott Long 	 * Initialize our context's device specific methods
41484c7070dbSScott Long 	 */
41494c7070dbSScott Long 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
41504c7070dbSScott Long 	kobj_class_compile((kobj_class_t) driver);
41514c7070dbSScott Long 	driver->refs++;
41524c7070dbSScott Long 
41534c7070dbSScott Long 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
41544c7070dbSScott Long 	if_setsoftc(ifp, ctx);
41554c7070dbSScott Long 	if_setdev(ifp, dev);
41564c7070dbSScott Long 	if_setinitfn(ifp, iflib_if_init);
41574c7070dbSScott Long 	if_setioctlfn(ifp, iflib_if_ioctl);
41584c7070dbSScott Long 	if_settransmitfn(ifp, iflib_if_transmit);
41594c7070dbSScott Long 	if_setqflushfn(ifp, iflib_if_qflush);
41604c7070dbSScott Long 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
41614c7070dbSScott Long 
41624c7070dbSScott Long 	ctx->ifc_vlan_attach_event =
41634c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
41644c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
41654c7070dbSScott Long 	ctx->ifc_vlan_detach_event =
41664c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
41674c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
41684c7070dbSScott Long 
41694c7070dbSScott Long 	ifmedia_init(&ctx->ifc_media, IFM_IMASK,
41704c7070dbSScott Long 					 iflib_media_change, iflib_media_status);
41714c7070dbSScott Long 
41724c7070dbSScott Long 	return (0);
41734c7070dbSScott Long }
41744c7070dbSScott Long 
41754c7070dbSScott Long 
41764c7070dbSScott Long static int
41774c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx)
41784c7070dbSScott Long {
41794c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
418023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
41814c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
418223ac9029SStephen Hurd 	int nrxqsets = scctx->isc_nrxqsets;
418323ac9029SStephen Hurd 	int ntxqsets = scctx->isc_ntxqsets;
41844c7070dbSScott Long 	iflib_txq_t txq;
41854c7070dbSScott Long 	iflib_rxq_t rxq;
41864c7070dbSScott Long 	iflib_fl_t fl = NULL;
418723ac9029SStephen Hurd 	int i, j, cpu, err, txconf, rxconf;
41884c7070dbSScott Long 	iflib_dma_info_t ifdip;
418923ac9029SStephen Hurd 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
419023ac9029SStephen Hurd 	uint32_t *txqsizes = scctx->isc_txqsizes;
41914c7070dbSScott Long 	uint8_t nrxqs = sctx->isc_nrxqs;
41924c7070dbSScott Long 	uint8_t ntxqs = sctx->isc_ntxqs;
41934c7070dbSScott Long 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
41944c7070dbSScott Long 	caddr_t *vaddrs;
41954c7070dbSScott Long 	uint64_t *paddrs;
41964c7070dbSScott Long 	struct ifmp_ring **brscp;
41974c7070dbSScott Long 	int nbuf_rings = 1; /* XXX determine dynamically */
41984c7070dbSScott Long 
419923ac9029SStephen Hurd 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
420023ac9029SStephen Hurd 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
42014c7070dbSScott Long 
42020d0338afSConrad Meyer 	brscp = NULL;
420323ac9029SStephen Hurd 	txq = NULL;
42040d0338afSConrad Meyer 	rxq = NULL;
42050d0338afSConrad Meyer 
42064c7070dbSScott Long /* Allocate the TX ring struct memory */
42074c7070dbSScott Long 	if (!(txq =
42084c7070dbSScott Long 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
42094c7070dbSScott Long 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
42104c7070dbSScott Long 		device_printf(dev, "Unable to allocate TX ring memory\n");
42114c7070dbSScott Long 		err = ENOMEM;
42124c7070dbSScott Long 		goto fail;
42134c7070dbSScott Long 	}
42144c7070dbSScott Long 
42154c7070dbSScott Long 	/* Now allocate the RX */
42164c7070dbSScott Long 	if (!(rxq =
42174c7070dbSScott Long 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
42184c7070dbSScott Long 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
42194c7070dbSScott Long 		device_printf(dev, "Unable to allocate RX ring memory\n");
42204c7070dbSScott Long 		err = ENOMEM;
42214c7070dbSScott Long 		goto rx_fail;
42224c7070dbSScott Long 	}
42234c7070dbSScott Long 	if (!(brscp = malloc(sizeof(void *) * nbuf_rings * nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
42244c7070dbSScott Long 		device_printf(dev, "Unable to buf_ring_sc * memory\n");
42254c7070dbSScott Long 		err = ENOMEM;
42264c7070dbSScott Long 		goto rx_fail;
42274c7070dbSScott Long 	}
42284c7070dbSScott Long 
42294c7070dbSScott Long 	ctx->ifc_txqs = txq;
42304c7070dbSScott Long 	ctx->ifc_rxqs = rxq;
42314c7070dbSScott Long 
42324c7070dbSScott Long 	/*
42334c7070dbSScott Long 	 * XXX handle allocation failure
42344c7070dbSScott Long 	 */
423596c85efbSNathan Whitehorn 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
42364c7070dbSScott Long 		/* Set up some basics */
42374c7070dbSScott Long 
42384c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
42394c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
42404c7070dbSScott Long 			err = ENOMEM;
42410d0338afSConrad Meyer 			goto err_tx_desc;
42424c7070dbSScott Long 		}
42434c7070dbSScott Long 		txq->ift_ifdi = ifdip;
42444c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, ifdip++) {
42454c7070dbSScott Long 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
42464c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
42474c7070dbSScott Long 				err = ENOMEM;
42484c7070dbSScott Long 				goto err_tx_desc;
42494c7070dbSScott Long 			}
42504c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
42514c7070dbSScott Long 		}
42524c7070dbSScott Long 		txq->ift_ctx = ctx;
42534c7070dbSScott Long 		txq->ift_id = i;
425423ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
425523ac9029SStephen Hurd 			txq->ift_br_offset = 1;
425623ac9029SStephen Hurd 		} else {
425723ac9029SStephen Hurd 			txq->ift_br_offset = 0;
425823ac9029SStephen Hurd 		}
42594c7070dbSScott Long 		/* XXX fix this */
426096c85efbSNathan Whitehorn 		txq->ift_timer.c_cpu = cpu;
426196c85efbSNathan Whitehorn 		txq->ift_db_check.c_cpu = cpu;
42624c7070dbSScott Long 		txq->ift_nbr = nbuf_rings;
42634c7070dbSScott Long 
42644c7070dbSScott Long 		if (iflib_txsd_alloc(txq)) {
42654c7070dbSScott Long 			device_printf(dev, "Critical Failure setting up TX buffers\n");
42664c7070dbSScott Long 			err = ENOMEM;
42674c7070dbSScott Long 			goto err_tx_desc;
42684c7070dbSScott Long 		}
42694c7070dbSScott Long 
42704c7070dbSScott Long 		/* Initialize the TX lock */
42714c7070dbSScott Long 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout",
42724c7070dbSScott Long 		    device_get_nameunit(dev), txq->ift_id);
42734c7070dbSScott Long 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
42744c7070dbSScott Long 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
42754c7070dbSScott Long 		callout_init_mtx(&txq->ift_db_check, &txq->ift_mtx, 0);
42764c7070dbSScott Long 
42774c7070dbSScott Long 		snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db",
42784c7070dbSScott Long 			 device_get_nameunit(dev), txq->ift_id);
42794c7070dbSScott Long 		TXDB_LOCK_INIT(txq);
42804c7070dbSScott Long 
42814c7070dbSScott Long 		txq->ift_br = brscp + i*nbuf_rings;
42824c7070dbSScott Long 		for (j = 0; j < nbuf_rings; j++) {
42834c7070dbSScott Long 			err = ifmp_ring_alloc(&txq->ift_br[j], 2048, txq, iflib_txq_drain,
42844c7070dbSScott Long 					      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
42854c7070dbSScott Long 			if (err) {
42864c7070dbSScott Long 				/* XXX free any allocated rings */
42874c7070dbSScott Long 				device_printf(dev, "Unable to allocate buf_ring\n");
42880d0338afSConrad Meyer 				goto err_tx_desc;
42894c7070dbSScott Long 			}
42904c7070dbSScott Long 		}
42914c7070dbSScott Long 	}
42924c7070dbSScott Long 
42934c7070dbSScott Long 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
42944c7070dbSScott Long 		/* Set up some basics */
42954c7070dbSScott Long 
42964c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
42974c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
42984c7070dbSScott Long 			err = ENOMEM;
42990d0338afSConrad Meyer 			goto err_tx_desc;
43004c7070dbSScott Long 		}
43014c7070dbSScott Long 
43024c7070dbSScott Long 		rxq->ifr_ifdi = ifdip;
43034c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, ifdip++) {
43044c7070dbSScott Long 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
43054c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
43064c7070dbSScott Long 				err = ENOMEM;
43074c7070dbSScott Long 				goto err_tx_desc;
43084c7070dbSScott Long 			}
43094c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
43104c7070dbSScott Long 		}
43114c7070dbSScott Long 		rxq->ifr_ctx = ctx;
43124c7070dbSScott Long 		rxq->ifr_id = i;
431323ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
431423ac9029SStephen Hurd 			rxq->ifr_fl_offset = 1;
43154c7070dbSScott Long 		} else {
431623ac9029SStephen Hurd 			rxq->ifr_fl_offset = 0;
43174c7070dbSScott Long 		}
43184c7070dbSScott Long 		rxq->ifr_nfl = nfree_lists;
43194c7070dbSScott Long 		if (!(fl =
43204c7070dbSScott Long 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
43214c7070dbSScott Long 			device_printf(dev, "Unable to allocate free list memory\n");
43224c7070dbSScott Long 			err = ENOMEM;
43230d0338afSConrad Meyer 			goto err_tx_desc;
43244c7070dbSScott Long 		}
43254c7070dbSScott Long 		rxq->ifr_fl = fl;
43264c7070dbSScott Long 		for (j = 0; j < nfree_lists; j++) {
43274c7070dbSScott Long 			rxq->ifr_fl[j].ifl_rxq = rxq;
43284c7070dbSScott Long 			rxq->ifr_fl[j].ifl_id = j;
432923ac9029SStephen Hurd 			rxq->ifr_fl[j].ifl_ifdi =
433023ac9029SStephen Hurd 			    &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
43314c7070dbSScott Long 		}
43324c7070dbSScott Long         /* Allocate receive buffers for the ring*/
43334c7070dbSScott Long 		if (iflib_rxsd_alloc(rxq)) {
43344c7070dbSScott Long 			device_printf(dev,
43354c7070dbSScott Long 			    "Critical Failure setting up receive buffers\n");
43364c7070dbSScott Long 			err = ENOMEM;
43374c7070dbSScott Long 			goto err_rx_desc;
43384c7070dbSScott Long 		}
43394c7070dbSScott Long 	}
43404c7070dbSScott Long 
43414c7070dbSScott Long 	/* TXQs */
43424c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
43434c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
43444c7070dbSScott Long 	for (i = 0; i < ntxqsets; i++) {
43454c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
43464c7070dbSScott Long 
43474c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, di++) {
43484c7070dbSScott Long 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
43494c7070dbSScott Long 			paddrs[i*ntxqs + j] = di->idi_paddr;
43504c7070dbSScott Long 		}
43514c7070dbSScott Long 	}
43524c7070dbSScott Long 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
43534c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
43544c7070dbSScott Long 		iflib_tx_structures_free(ctx);
43554c7070dbSScott Long 		free(vaddrs, M_IFLIB);
43564c7070dbSScott Long 		free(paddrs, M_IFLIB);
43574c7070dbSScott Long 		goto err_rx_desc;
43584c7070dbSScott Long 	}
43594c7070dbSScott Long 	free(vaddrs, M_IFLIB);
43604c7070dbSScott Long 	free(paddrs, M_IFLIB);
43614c7070dbSScott Long 
43624c7070dbSScott Long 	/* RXQs */
43634c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
43644c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
43654c7070dbSScott Long 	for (i = 0; i < nrxqsets; i++) {
43664c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
43674c7070dbSScott Long 
43684c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, di++) {
43694c7070dbSScott Long 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
43704c7070dbSScott Long 			paddrs[i*nrxqs + j] = di->idi_paddr;
43714c7070dbSScott Long 		}
43724c7070dbSScott Long 	}
43734c7070dbSScott Long 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
43744c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
43754c7070dbSScott Long 		iflib_tx_structures_free(ctx);
43764c7070dbSScott Long 		free(vaddrs, M_IFLIB);
43774c7070dbSScott Long 		free(paddrs, M_IFLIB);
43784c7070dbSScott Long 		goto err_rx_desc;
43794c7070dbSScott Long 	}
43804c7070dbSScott Long 	free(vaddrs, M_IFLIB);
43814c7070dbSScott Long 	free(paddrs, M_IFLIB);
43824c7070dbSScott Long 
43834c7070dbSScott Long 	return (0);
43844c7070dbSScott Long 
43854c7070dbSScott Long /* XXX handle allocation failure changes */
43864c7070dbSScott Long err_rx_desc:
43874c7070dbSScott Long err_tx_desc:
43884c7070dbSScott Long 	if (ctx->ifc_rxqs != NULL)
43894c7070dbSScott Long 		free(ctx->ifc_rxqs, M_IFLIB);
43904c7070dbSScott Long 	ctx->ifc_rxqs = NULL;
43914c7070dbSScott Long 	if (ctx->ifc_txqs != NULL)
43924c7070dbSScott Long 		free(ctx->ifc_txqs, M_IFLIB);
43934c7070dbSScott Long 	ctx->ifc_txqs = NULL;
43940d0338afSConrad Meyer rx_fail:
43950d0338afSConrad Meyer 	if (brscp != NULL)
43960d0338afSConrad Meyer 		free(brscp, M_IFLIB);
43970d0338afSConrad Meyer 	if (rxq != NULL)
43980d0338afSConrad Meyer 		free(rxq, M_IFLIB);
43990d0338afSConrad Meyer 	if (txq != NULL)
44000d0338afSConrad Meyer 		free(txq, M_IFLIB);
44014c7070dbSScott Long fail:
44024c7070dbSScott Long 	return (err);
44034c7070dbSScott Long }
44044c7070dbSScott Long 
44054c7070dbSScott Long static int
44064c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx)
44074c7070dbSScott Long {
44084c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
44094c7070dbSScott Long 	int i;
44104c7070dbSScott Long 
44114c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
44124c7070dbSScott Long 		iflib_txq_setup(txq);
44134c7070dbSScott Long 
44144c7070dbSScott Long 	return (0);
44154c7070dbSScott Long }
44164c7070dbSScott Long 
44174c7070dbSScott Long static void
44184c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx)
44194c7070dbSScott Long {
44204c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
44214c7070dbSScott Long 	int i, j;
44224c7070dbSScott Long 
44234c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
44244c7070dbSScott Long 		iflib_txq_destroy(txq);
44254c7070dbSScott Long 		for (j = 0; j < ctx->ifc_nhwtxqs; j++)
44264c7070dbSScott Long 			iflib_dma_free(&txq->ift_ifdi[j]);
44274c7070dbSScott Long 	}
44284c7070dbSScott Long 	free(ctx->ifc_txqs, M_IFLIB);
44294c7070dbSScott Long 	ctx->ifc_txqs = NULL;
44304c7070dbSScott Long 	IFDI_QUEUES_FREE(ctx);
44314c7070dbSScott Long }
44324c7070dbSScott Long 
44334c7070dbSScott Long /*********************************************************************
44344c7070dbSScott Long  *
44354c7070dbSScott Long  *  Initialize all receive rings.
44364c7070dbSScott Long  *
44374c7070dbSScott Long  **********************************************************************/
44384c7070dbSScott Long static int
44394c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx)
44404c7070dbSScott Long {
44414c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
4442aaeb188aSBjoern A. Zeeb 	int q;
4443aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
4444aaeb188aSBjoern A. Zeeb 	int i, err;
4445aaeb188aSBjoern A. Zeeb #endif
44464c7070dbSScott Long 
44474c7070dbSScott Long 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
4448aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
44494c7070dbSScott Long 		tcp_lro_free(&rxq->ifr_lc);
445023ac9029SStephen Hurd 		if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
445123ac9029SStephen Hurd 		    TCP_LRO_ENTRIES, min(1024,
445223ac9029SStephen Hurd 		    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) {
44534c7070dbSScott Long 			device_printf(ctx->ifc_dev, "LRO Initialization failed!\n");
44544c7070dbSScott Long 			goto fail;
44554c7070dbSScott Long 		}
44564c7070dbSScott Long 		rxq->ifr_lro_enabled = TRUE;
4457aaeb188aSBjoern A. Zeeb #endif
44584c7070dbSScott Long 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
44594c7070dbSScott Long 	}
44604c7070dbSScott Long 	return (0);
4461aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
44624c7070dbSScott Long fail:
44634c7070dbSScott Long 	/*
44644c7070dbSScott Long 	 * Free RX software descriptors allocated so far, we will only handle
44654c7070dbSScott Long 	 * the rings that completed, the failing case will have
44664c7070dbSScott Long 	 * cleaned up for itself. 'q' failed, so its the terminus.
44674c7070dbSScott Long 	 */
44684c7070dbSScott Long 	rxq = ctx->ifc_rxqs;
44694c7070dbSScott Long 	for (i = 0; i < q; ++i, rxq++) {
44704c7070dbSScott Long 		iflib_rx_sds_free(rxq);
44714c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
44724c7070dbSScott Long 	}
44734c7070dbSScott Long 	return (err);
4474aaeb188aSBjoern A. Zeeb #endif
44754c7070dbSScott Long }
44764c7070dbSScott Long 
44774c7070dbSScott Long /*********************************************************************
44784c7070dbSScott Long  *
44794c7070dbSScott Long  *  Free all receive rings.
44804c7070dbSScott Long  *
44814c7070dbSScott Long  **********************************************************************/
44824c7070dbSScott Long static void
44834c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx)
44844c7070dbSScott Long {
44854c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
44864c7070dbSScott Long 
448723ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
44884c7070dbSScott Long 		iflib_rx_sds_free(rxq);
44894c7070dbSScott Long 	}
44904c7070dbSScott Long }
44914c7070dbSScott Long 
44924c7070dbSScott Long static int
44934c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx)
44944c7070dbSScott Long {
44954c7070dbSScott Long 	int err;
44964c7070dbSScott Long 
44974c7070dbSScott Long 	if ((err = iflib_tx_structures_setup(ctx)) != 0)
44984c7070dbSScott Long 		return (err);
44994c7070dbSScott Long 
45004c7070dbSScott Long 	if ((err = iflib_rx_structures_setup(ctx)) != 0) {
45014c7070dbSScott Long 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
45024c7070dbSScott Long 		iflib_tx_structures_free(ctx);
45034c7070dbSScott Long 		iflib_rx_structures_free(ctx);
45044c7070dbSScott Long 	}
45054c7070dbSScott Long 	return (err);
45064c7070dbSScott Long }
45074c7070dbSScott Long 
45084c7070dbSScott Long int
45094c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
45104c7070dbSScott Long 				driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, char *name)
45114c7070dbSScott Long {
45124c7070dbSScott Long 
45134c7070dbSScott Long 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
45144c7070dbSScott Long }
45154c7070dbSScott Long 
4516aa3c5dd8SSean Bruno static int
45174c7070dbSScott Long find_nth(if_ctx_t ctx, cpuset_t *cpus, int qid)
45184c7070dbSScott Long {
4519aa3c5dd8SSean Bruno 	int i, cpuid, eqid, count;
45204c7070dbSScott Long 
45214c7070dbSScott Long 	CPU_COPY(&ctx->ifc_cpus, cpus);
4522aa3c5dd8SSean Bruno 	count = CPU_COUNT(&ctx->ifc_cpus);
4523aa3c5dd8SSean Bruno 	eqid = qid % count;
45244c7070dbSScott Long 	/* clear up to the qid'th bit */
4525aa3c5dd8SSean Bruno 	for (i = 0; i < eqid; i++) {
45264c7070dbSScott Long 		cpuid = CPU_FFS(cpus);
4527aa3c5dd8SSean Bruno 		MPASS(cpuid != 0);
4528aa3c5dd8SSean Bruno 		CPU_CLR(cpuid-1, cpus);
45294c7070dbSScott Long 	}
4530aa3c5dd8SSean Bruno 	cpuid = CPU_FFS(cpus);
4531aa3c5dd8SSean Bruno 	MPASS(cpuid != 0);
4532aa3c5dd8SSean Bruno 	return (cpuid-1);
45334c7070dbSScott Long }
45344c7070dbSScott Long 
45354c7070dbSScott Long int
45364c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
45374c7070dbSScott Long 						iflib_intr_type_t type, driver_filter_t *filter,
45384c7070dbSScott Long 						void *filter_arg, int qid, char *name)
45394c7070dbSScott Long {
45404c7070dbSScott Long 	struct grouptask *gtask;
45414c7070dbSScott Long 	struct taskqgroup *tqg;
45424c7070dbSScott Long 	iflib_filter_info_t info;
45434c7070dbSScott Long 	cpuset_t cpus;
454423ac9029SStephen Hurd 	gtask_fn_t *fn;
4545aa3c5dd8SSean Bruno 	int tqrid, err, cpuid;
45464c7070dbSScott Long 	void *q;
45474c7070dbSScott Long 
45484c7070dbSScott Long 	info = &ctx->ifc_filter_info;
4549add6f7d0SSean Bruno 	tqrid = rid;
45504c7070dbSScott Long 
45514c7070dbSScott Long 	switch (type) {
45524c7070dbSScott Long 	/* XXX merge tx/rx for netmap? */
45534c7070dbSScott Long 	case IFLIB_INTR_TX:
45544c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
45554c7070dbSScott Long 		info = &ctx->ifc_txqs[qid].ift_filter_info;
45564c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
45574c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
45584c7070dbSScott Long 		fn = _task_fn_tx;
4559da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
45604c7070dbSScott Long 		break;
45614c7070dbSScott Long 	case IFLIB_INTR_RX:
45624c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
45634c7070dbSScott Long 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
45644c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
45654c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
45664c7070dbSScott Long 		fn = _task_fn_rx;
4567da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
45684c7070dbSScott Long 		break;
45694c7070dbSScott Long 	case IFLIB_INTR_ADMIN:
45704c7070dbSScott Long 		q = ctx;
4571da69b8f9SSean Bruno 		tqrid = -1;
45724c7070dbSScott Long 		info = &ctx->ifc_filter_info;
45734c7070dbSScott Long 		gtask = &ctx->ifc_admin_task;
45744c7070dbSScott Long 		tqg = qgroup_if_config_tqg;
45754c7070dbSScott Long 		fn = _task_fn_admin;
45764c7070dbSScott Long 		break;
45774c7070dbSScott Long 	default:
45784c7070dbSScott Long 		panic("unknown net intr type");
45794c7070dbSScott Long 	}
45804c7070dbSScott Long 
45814c7070dbSScott Long 	info->ifi_filter = filter;
45824c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
45834c7070dbSScott Long 	info->ifi_task = gtask;
45844ecb427aSSean Bruno 	info->ifi_ctx = ctx;
45854c7070dbSScott Long 
45864c7070dbSScott Long 	err = _iflib_irq_alloc(ctx, irq, rid, iflib_fast_intr, NULL, info,  name);
4587da69b8f9SSean Bruno 	if (err != 0) {
4588da69b8f9SSean Bruno 		device_printf(ctx->ifc_dev, "_iflib_irq_alloc failed %d\n", err);
45894c7070dbSScott Long 		return (err);
4590da69b8f9SSean Bruno 	}
4591da69b8f9SSean Bruno 	if (type == IFLIB_INTR_ADMIN)
4592da69b8f9SSean Bruno 		return (0);
4593da69b8f9SSean Bruno 
45944c7070dbSScott Long 	if (tqrid != -1) {
4595aa3c5dd8SSean Bruno 		cpuid = find_nth(ctx, &cpus, qid);
4596aa3c5dd8SSean Bruno 		taskqgroup_attach_cpu(tqg, gtask, q, cpuid, irq->ii_rid, name);
4597aa3c5dd8SSean Bruno 	} else {
45984c7070dbSScott Long 		taskqgroup_attach(tqg, gtask, q, tqrid, name);
4599aa3c5dd8SSean Bruno 	}
46004c7070dbSScott Long 
46014c7070dbSScott Long 	return (0);
46024c7070dbSScott Long }
46034c7070dbSScott Long 
46044c7070dbSScott Long void
46054c7070dbSScott Long iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name)
46064c7070dbSScott Long {
46074c7070dbSScott Long 	struct grouptask *gtask;
46084c7070dbSScott Long 	struct taskqgroup *tqg;
460923ac9029SStephen Hurd 	gtask_fn_t *fn;
46104c7070dbSScott Long 	void *q;
46114c7070dbSScott Long 
46124c7070dbSScott Long 	switch (type) {
46134c7070dbSScott Long 	case IFLIB_INTR_TX:
46144c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
46154c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
46164c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
46174c7070dbSScott Long 		fn = _task_fn_tx;
46184c7070dbSScott Long 		break;
46194c7070dbSScott Long 	case IFLIB_INTR_RX:
46204c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
46214c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
46224c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
46234c7070dbSScott Long 		fn = _task_fn_rx;
46244c7070dbSScott Long 		break;
46254c7070dbSScott Long 	case IFLIB_INTR_IOV:
46264c7070dbSScott Long 		q = ctx;
46274c7070dbSScott Long 		gtask = &ctx->ifc_vflr_task;
46284c7070dbSScott Long 		tqg = qgroup_if_config_tqg;
46294c7070dbSScott Long 		rid = -1;
46304c7070dbSScott Long 		fn = _task_fn_iov;
46314c7070dbSScott Long 		break;
46324c7070dbSScott Long 	default:
46334c7070dbSScott Long 		panic("unknown net intr type");
46344c7070dbSScott Long 	}
46354c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
46364c7070dbSScott Long 	taskqgroup_attach(tqg, gtask, q, rid, name);
46374c7070dbSScott Long }
46384c7070dbSScott Long 
46394c7070dbSScott Long void
46404c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
46414c7070dbSScott Long {
46424c7070dbSScott Long 	if (irq->ii_tag)
46434c7070dbSScott Long 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
46444c7070dbSScott Long 
46454c7070dbSScott Long 	if (irq->ii_res)
46464c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, irq->ii_rid, irq->ii_res);
46474c7070dbSScott Long }
46484c7070dbSScott Long 
46494c7070dbSScott Long static int
46504c7070dbSScott Long iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, char *name)
46514c7070dbSScott Long {
46524c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
46534c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
46544c7070dbSScott Long 	if_irq_t irq = &ctx->ifc_legacy_irq;
46554c7070dbSScott Long 	iflib_filter_info_t info;
46564c7070dbSScott Long 	struct grouptask *gtask;
46574c7070dbSScott Long 	struct taskqgroup *tqg;
465823ac9029SStephen Hurd 	gtask_fn_t *fn;
46594c7070dbSScott Long 	int tqrid;
46604c7070dbSScott Long 	void *q;
46614c7070dbSScott Long 	int err;
46624c7070dbSScott Long 
46634c7070dbSScott Long 	q = &ctx->ifc_rxqs[0];
46644c7070dbSScott Long 	info = &rxq[0].ifr_filter_info;
46654c7070dbSScott Long 	gtask = &rxq[0].ifr_task;
46664c7070dbSScott Long 	tqg = qgroup_if_io_tqg;
46674c7070dbSScott Long 	tqrid = irq->ii_rid = *rid;
46684c7070dbSScott Long 	fn = _task_fn_rx;
46694c7070dbSScott Long 
46704c7070dbSScott Long 	ctx->ifc_flags |= IFC_LEGACY;
46714c7070dbSScott Long 	info->ifi_filter = filter;
46724c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
46734c7070dbSScott Long 	info->ifi_task = gtask;
46744ecb427aSSean Bruno 	info->ifi_ctx = ctx;
46754c7070dbSScott Long 
46764c7070dbSScott Long 	/* We allocate a single interrupt resource */
46774c7070dbSScott Long 	if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr, NULL, info, name)) != 0)
46784c7070dbSScott Long 		return (err);
46794c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
46804c7070dbSScott Long 	taskqgroup_attach(tqg, gtask, q, tqrid, name);
46814c7070dbSScott Long 
46824c7070dbSScott Long 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
46834c7070dbSScott Long 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, tqrid, "tx");
46844c7070dbSScott Long 	return (0);
46854c7070dbSScott Long }
46864c7070dbSScott Long 
46874c7070dbSScott Long void
46884c7070dbSScott Long iflib_led_create(if_ctx_t ctx)
46894c7070dbSScott Long {
46904c7070dbSScott Long 
46914c7070dbSScott Long 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
46924c7070dbSScott Long 								  device_get_nameunit(ctx->ifc_dev));
46934c7070dbSScott Long }
46944c7070dbSScott Long 
46954c7070dbSScott Long void
46964c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
46974c7070dbSScott Long {
46984c7070dbSScott Long 
46994c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
47004c7070dbSScott Long }
47014c7070dbSScott Long 
47024c7070dbSScott Long void
47034c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
47044c7070dbSScott Long {
47054c7070dbSScott Long 
47064c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
47074c7070dbSScott Long }
47084c7070dbSScott Long 
47094c7070dbSScott Long void
47104c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx)
47114c7070dbSScott Long {
47121248952aSSean Bruno #ifdef INVARIANTS
47131248952aSSean Bruno 	struct grouptask *gtask;
47141248952aSSean Bruno 
47151248952aSSean Bruno 	gtask = &ctx->ifc_admin_task;
47161248952aSSean Bruno 	MPASS(gtask->gt_taskqueue != NULL);
47171248952aSSean Bruno #endif
47184c7070dbSScott Long 
47194c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
47204c7070dbSScott Long }
47214c7070dbSScott Long 
47224c7070dbSScott Long void
47234c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx)
47244c7070dbSScott Long {
47254c7070dbSScott Long 
47264c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
47274c7070dbSScott Long }
47284c7070dbSScott Long 
47294c7070dbSScott Long void
47304c7070dbSScott Long iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name)
47314c7070dbSScott Long {
47324c7070dbSScott Long 
47334c7070dbSScott Long 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, -1, name);
47344c7070dbSScott Long }
47354c7070dbSScott Long 
47364c7070dbSScott Long void
473723ac9029SStephen Hurd iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask, gtask_fn_t *fn,
47384c7070dbSScott Long 	char *name)
47394c7070dbSScott Long {
47404c7070dbSScott Long 
47414c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, ctx);
47424c7070dbSScott Long 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, -1, name);
47434c7070dbSScott Long }
47444c7070dbSScott Long 
47454c7070dbSScott Long void
474623ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask)
474723ac9029SStephen Hurd {
474823ac9029SStephen Hurd 
474923ac9029SStephen Hurd 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
475023ac9029SStephen Hurd }
475123ac9029SStephen Hurd 
475223ac9029SStephen Hurd void
475323ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
47544c7070dbSScott Long {
47554c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
47564c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
47574c7070dbSScott Long 
47584c7070dbSScott Long 	if_setbaudrate(ifp, baudrate);
475923ac9029SStephen Hurd 
47604c7070dbSScott Long 	/* If link down, disable watchdog */
47614c7070dbSScott Long 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
47624c7070dbSScott Long 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
47634c7070dbSScott Long 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
47644c7070dbSScott Long 	}
47654c7070dbSScott Long 	ctx->ifc_link_state = link_state;
47664c7070dbSScott Long 	if_link_state_change(ifp, link_state);
47674c7070dbSScott Long }
47684c7070dbSScott Long 
47694c7070dbSScott Long static int
47704c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
47714c7070dbSScott Long {
47724c7070dbSScott Long 	int credits;
47731248952aSSean Bruno #ifdef INVARIANTS
47741248952aSSean Bruno 	int credits_pre = txq->ift_cidx_processed;
47751248952aSSean Bruno #endif
47764c7070dbSScott Long 
47774c7070dbSScott Long 	if (ctx->isc_txd_credits_update == NULL)
47784c7070dbSScott Long 		return (0);
47794c7070dbSScott Long 
47804c7070dbSScott Long 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, txq->ift_cidx_processed, true)) == 0)
47814c7070dbSScott Long 		return (0);
47824c7070dbSScott Long 
47834c7070dbSScott Long 	txq->ift_processed += credits;
47844c7070dbSScott Long 	txq->ift_cidx_processed += credits;
47854c7070dbSScott Long 
47861248952aSSean Bruno 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
47874c7070dbSScott Long 	if (txq->ift_cidx_processed >= txq->ift_size)
47884c7070dbSScott Long 		txq->ift_cidx_processed -= txq->ift_size;
47894c7070dbSScott Long 	return (credits);
47904c7070dbSScott Long }
47914c7070dbSScott Long 
47924c7070dbSScott Long static int
479323ac9029SStephen Hurd iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, int cidx, int budget)
47944c7070dbSScott Long {
47954c7070dbSScott Long 
479623ac9029SStephen Hurd 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
479723ac9029SStephen Hurd 	    budget));
47984c7070dbSScott Long }
47994c7070dbSScott Long 
48004c7070dbSScott Long void
48014c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
48024c7070dbSScott Long 	const char *description, if_int_delay_info_t info,
48034c7070dbSScott Long 	int offset, int value)
48044c7070dbSScott Long {
48054c7070dbSScott Long 	info->iidi_ctx = ctx;
48064c7070dbSScott Long 	info->iidi_offset = offset;
48074c7070dbSScott Long 	info->iidi_value = value;
48084c7070dbSScott Long 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
48094c7070dbSScott Long 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
48104c7070dbSScott Long 	    OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
48114c7070dbSScott Long 	    info, 0, iflib_sysctl_int_delay, "I", description);
48124c7070dbSScott Long }
48134c7070dbSScott Long 
48144c7070dbSScott Long struct mtx *
48154c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx)
48164c7070dbSScott Long {
48174c7070dbSScott Long 
48184c7070dbSScott Long 	return (&ctx->ifc_mtx);
48194c7070dbSScott Long }
48204c7070dbSScott Long 
48214c7070dbSScott Long static int
48224c7070dbSScott Long iflib_msix_init(if_ctx_t ctx)
48234c7070dbSScott Long {
48244c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
48254c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
48264c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
48274c7070dbSScott Long 	int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs;
48284c7070dbSScott Long 	int iflib_num_tx_queues, iflib_num_rx_queues;
48294c7070dbSScott Long 	int err, admincnt, bar;
48304c7070dbSScott Long 
483123ac9029SStephen Hurd 	iflib_num_tx_queues = scctx->isc_ntxqsets;
483223ac9029SStephen Hurd 	iflib_num_rx_queues = scctx->isc_nrxqsets;
483323ac9029SStephen Hurd 
48341248952aSSean Bruno 	device_printf(dev, "msix_init qsets capped at %d\n", iflib_num_tx_queues);
48351248952aSSean Bruno 
48364c7070dbSScott Long 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
48374c7070dbSScott Long 	admincnt = sctx->isc_admin_intrcnt;
48384c7070dbSScott Long 	/* Override by tuneable */
48394c7070dbSScott Long 	if (enable_msix == 0)
48404c7070dbSScott Long 		goto msi;
48414c7070dbSScott Long 
48424c7070dbSScott Long 	/*
48434c7070dbSScott Long 	** When used in a virtualized environment
48444c7070dbSScott Long 	** PCI BUSMASTER capability may not be set
48454c7070dbSScott Long 	** so explicity set it here and rewrite
48464c7070dbSScott Long 	** the ENABLE in the MSIX control register
48474c7070dbSScott Long 	** at this point to cause the host to
48484c7070dbSScott Long 	** successfully initialize us.
48494c7070dbSScott Long 	*/
48504c7070dbSScott Long 	{
48514c7070dbSScott Long 		int msix_ctrl, rid;
48524c7070dbSScott Long 
485396eeabefSSean Bruno  		pci_enable_busmaster(dev);
4854f7ae9a84SSean Bruno 		rid = 0;
4855f7ae9a84SSean Bruno 		if (pci_find_cap(dev, PCIY_MSIX, &rid) == 0 && rid != 0) {
48564c7070dbSScott Long 			rid += PCIR_MSIX_CTRL;
48574c7070dbSScott Long 			msix_ctrl = pci_read_config(dev, rid, 2);
48584c7070dbSScott Long 			msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
48594c7070dbSScott Long 			pci_write_config(dev, rid, msix_ctrl, 2);
4860f7ae9a84SSean Bruno 		} else {
4861f7ae9a84SSean Bruno 			device_printf(dev, "PCIY_MSIX capability not found; "
4862f7ae9a84SSean Bruno 			                   "or rid %d == 0.\n", rid);
4863f7ae9a84SSean Bruno 			goto msi;
4864f7ae9a84SSean Bruno 		}
48654c7070dbSScott Long 	}
48664c7070dbSScott Long 
48674c7070dbSScott Long 	/*
48684c7070dbSScott Long 	 * bar == -1 => "trust me I know what I'm doing"
48694c7070dbSScott Long 	 * https://www.youtube.com/watch?v=nnwWKkNau4I
48704c7070dbSScott Long 	 * Some drivers are for hardware that is so shoddily
48714c7070dbSScott Long 	 * documented that no one knows which bars are which
48724c7070dbSScott Long 	 * so the developer has to map all bars. This hack
48734c7070dbSScott Long 	 * allows shoddy garbage to use msix in this framework.
48744c7070dbSScott Long 	 */
48754c7070dbSScott Long 	if (bar != -1) {
48764c7070dbSScott Long 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
48774c7070dbSScott Long 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
48784c7070dbSScott Long 		if (ctx->ifc_msix_mem == NULL) {
48794c7070dbSScott Long 			/* May not be enabled */
48804c7070dbSScott Long 			device_printf(dev, "Unable to map MSIX table \n");
48814c7070dbSScott Long 			goto msi;
48824c7070dbSScott Long 		}
48834c7070dbSScott Long 	}
48844c7070dbSScott Long 	/* First try MSI/X */
48854c7070dbSScott Long 	if ((msgs = pci_msix_count(dev)) == 0) { /* system has msix disabled */
48864c7070dbSScott Long 		device_printf(dev, "System has MSIX disabled \n");
48874c7070dbSScott Long 		bus_release_resource(dev, SYS_RES_MEMORY,
48884c7070dbSScott Long 		    bar, ctx->ifc_msix_mem);
48894c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
48904c7070dbSScott Long 		goto msi;
48914c7070dbSScott Long 	}
48924c7070dbSScott Long #if IFLIB_DEBUG
48934c7070dbSScott Long 	/* use only 1 qset in debug mode */
48944c7070dbSScott Long 	queuemsgs = min(msgs - admincnt, 1);
48954c7070dbSScott Long #else
48964c7070dbSScott Long 	queuemsgs = msgs - admincnt;
48974c7070dbSScott Long #endif
48984c7070dbSScott Long 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) == 0) {
48994c7070dbSScott Long #ifdef RSS
49004c7070dbSScott Long 		queues = imin(queuemsgs, rss_getnumbuckets());
49014c7070dbSScott Long #else
49024c7070dbSScott Long 		queues = queuemsgs;
49034c7070dbSScott Long #endif
49044c7070dbSScott Long 		queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
49054c7070dbSScott Long 		device_printf(dev, "pxm cpus: %d queue msgs: %d admincnt: %d\n",
49064c7070dbSScott Long 					  CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
49074c7070dbSScott Long 	} else {
49084c7070dbSScott Long 		device_printf(dev, "Unable to fetch CPU list\n");
49094c7070dbSScott Long 		/* Figure out a reasonable auto config value */
49104c7070dbSScott Long 		queues = min(queuemsgs, mp_ncpus);
49114c7070dbSScott Long 	}
49124c7070dbSScott Long #ifdef  RSS
49134c7070dbSScott Long 	/* If we're doing RSS, clamp at the number of RSS buckets */
49144c7070dbSScott Long 	if (queues > rss_getnumbuckets())
49154c7070dbSScott Long 		queues = rss_getnumbuckets();
49164c7070dbSScott Long #endif
491723ac9029SStephen Hurd 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
491823ac9029SStephen Hurd 		rx_queues = iflib_num_rx_queues;
49194c7070dbSScott Long 	else
49204c7070dbSScott Long 		rx_queues = queues;
492123ac9029SStephen Hurd 	/*
492223ac9029SStephen Hurd 	 * We want this to be all logical CPUs by default
492323ac9029SStephen Hurd 	 */
49244c7070dbSScott Long 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
49254c7070dbSScott Long 		tx_queues = iflib_num_tx_queues;
49264c7070dbSScott Long 	else
492723ac9029SStephen Hurd 		tx_queues = mp_ncpus;
492823ac9029SStephen Hurd 
492923ac9029SStephen Hurd 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
493023ac9029SStephen Hurd #ifdef INVARIANTS
493123ac9029SStephen Hurd 		if (tx_queues != rx_queues)
493223ac9029SStephen Hurd 			device_printf(dev, "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
493323ac9029SStephen Hurd 				      min(rx_queues, tx_queues), min(rx_queues, tx_queues));
493423ac9029SStephen Hurd #endif
493523ac9029SStephen Hurd 		tx_queues = min(rx_queues, tx_queues);
493623ac9029SStephen Hurd 		rx_queues = min(rx_queues, tx_queues);
493723ac9029SStephen Hurd 	}
49384c7070dbSScott Long 
49394c7070dbSScott Long 	device_printf(dev, "using %d rx queues %d tx queues \n", rx_queues, tx_queues);
49404c7070dbSScott Long 
494123ac9029SStephen Hurd 	vectors = rx_queues + admincnt;
49424c7070dbSScott Long 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
49434c7070dbSScott Long 		device_printf(dev,
49444c7070dbSScott Long 					  "Using MSIX interrupts with %d vectors\n", vectors);
49454c7070dbSScott Long 		scctx->isc_vectors = vectors;
49464c7070dbSScott Long 		scctx->isc_nrxqsets = rx_queues;
49474c7070dbSScott Long 		scctx->isc_ntxqsets = tx_queues;
49484c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSIX;
494923ac9029SStephen Hurd 
49504c7070dbSScott Long 		return (vectors);
49514c7070dbSScott Long 	} else {
49524c7070dbSScott Long 		device_printf(dev, "failed to allocate %d msix vectors, err: %d - using MSI\n", vectors, err);
49534c7070dbSScott Long 	}
49544c7070dbSScott Long msi:
49554c7070dbSScott Long 	vectors = pci_msi_count(dev);
49564c7070dbSScott Long 	scctx->isc_nrxqsets = 1;
49574c7070dbSScott Long 	scctx->isc_ntxqsets = 1;
49584c7070dbSScott Long 	scctx->isc_vectors = vectors;
49594c7070dbSScott Long 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
49604c7070dbSScott Long 		device_printf(dev,"Using an MSI interrupt\n");
49614c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSI;
49624c7070dbSScott Long 	} else {
49634c7070dbSScott Long 		device_printf(dev,"Using a Legacy interrupt\n");
49644c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
49654c7070dbSScott Long 	}
49664c7070dbSScott Long 
49674c7070dbSScott Long 	return (vectors);
49684c7070dbSScott Long }
49694c7070dbSScott Long 
49704c7070dbSScott Long char * ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
49714c7070dbSScott Long 
49724c7070dbSScott Long static int
49734c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
49744c7070dbSScott Long {
49754c7070dbSScott Long 	int rc;
49764c7070dbSScott Long 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
49774c7070dbSScott Long 	struct sbuf *sb;
49784c7070dbSScott Long 	char *ring_state = "UNKNOWN";
49794c7070dbSScott Long 
49804c7070dbSScott Long 	/* XXX needed ? */
49814c7070dbSScott Long 	rc = sysctl_wire_old_buffer(req, 0);
49824c7070dbSScott Long 	MPASS(rc == 0);
49834c7070dbSScott Long 	if (rc != 0)
49844c7070dbSScott Long 		return (rc);
49854c7070dbSScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
49864c7070dbSScott Long 	MPASS(sb != NULL);
49874c7070dbSScott Long 	if (sb == NULL)
49884c7070dbSScott Long 		return (ENOMEM);
49894c7070dbSScott Long 	if (state[3] <= 3)
49904c7070dbSScott Long 		ring_state = ring_states[state[3]];
49914c7070dbSScott Long 
49924c7070dbSScott Long 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
49934c7070dbSScott Long 		    state[0], state[1], state[2], ring_state);
49944c7070dbSScott Long 	rc = sbuf_finish(sb);
49954c7070dbSScott Long 	sbuf_delete(sb);
49964c7070dbSScott Long         return(rc);
49974c7070dbSScott Long }
49984c7070dbSScott Long 
499923ac9029SStephen Hurd enum iflib_ndesc_handler {
500023ac9029SStephen Hurd 	IFLIB_NTXD_HANDLER,
500123ac9029SStephen Hurd 	IFLIB_NRXD_HANDLER,
500223ac9029SStephen Hurd };
50034c7070dbSScott Long 
500423ac9029SStephen Hurd static int
500523ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
500623ac9029SStephen Hurd {
500723ac9029SStephen Hurd 	if_ctx_t ctx = (void *)arg1;
500823ac9029SStephen Hurd 	enum iflib_ndesc_handler type = arg2;
500923ac9029SStephen Hurd 	char buf[256] = {0};
501023ac9029SStephen Hurd 	uint16_t *ndesc;
501123ac9029SStephen Hurd 	char *p, *next;
501223ac9029SStephen Hurd 	int nqs, rc, i;
501323ac9029SStephen Hurd 
501423ac9029SStephen Hurd 	MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER);
501523ac9029SStephen Hurd 
501623ac9029SStephen Hurd 	nqs = 8;
501723ac9029SStephen Hurd 	switch(type) {
501823ac9029SStephen Hurd 	case IFLIB_NTXD_HANDLER:
501923ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_ntxds;
502023ac9029SStephen Hurd 		if (ctx->ifc_sctx)
502123ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_ntxqs;
502223ac9029SStephen Hurd 		break;
502323ac9029SStephen Hurd 	case IFLIB_NRXD_HANDLER:
502423ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_nrxds;
502523ac9029SStephen Hurd 		if (ctx->ifc_sctx)
502623ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_nrxqs;
502723ac9029SStephen Hurd 		break;
502823ac9029SStephen Hurd 	}
502923ac9029SStephen Hurd 	if (nqs == 0)
503023ac9029SStephen Hurd 		nqs = 8;
503123ac9029SStephen Hurd 
503223ac9029SStephen Hurd 	for (i=0; i<8; i++) {
503323ac9029SStephen Hurd 		if (i >= nqs)
503423ac9029SStephen Hurd 			break;
503523ac9029SStephen Hurd 		if (i)
503623ac9029SStephen Hurd 			strcat(buf, ",");
503723ac9029SStephen Hurd 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
503823ac9029SStephen Hurd 	}
503923ac9029SStephen Hurd 
504023ac9029SStephen Hurd 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
504123ac9029SStephen Hurd 	if (rc || req->newptr == NULL)
504223ac9029SStephen Hurd 		return rc;
504323ac9029SStephen Hurd 
504423ac9029SStephen Hurd 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
504523ac9029SStephen Hurd 	    i++, p = strsep(&next, " ,")) {
504623ac9029SStephen Hurd 		ndesc[i] = strtoul(p, NULL, 10);
504723ac9029SStephen Hurd 	}
504823ac9029SStephen Hurd 
504923ac9029SStephen Hurd 	return(rc);
505023ac9029SStephen Hurd }
50514c7070dbSScott Long 
50524c7070dbSScott Long #define NAME_BUFLEN 32
50534c7070dbSScott Long static void
50544c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx)
50554c7070dbSScott Long {
50564c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
50574c7070dbSScott Long 	struct sysctl_oid_list *child, *oid_list;
50584c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
50594c7070dbSScott Long 	struct sysctl_oid *node;
50604c7070dbSScott Long 
50614c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
50624c7070dbSScott Long 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
50634c7070dbSScott Long 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
50644c7070dbSScott Long 						      CTLFLAG_RD, NULL, "IFLIB fields");
50654c7070dbSScott Long 	oid_list = SYSCTL_CHILDREN(node);
50664c7070dbSScott Long 
506723ac9029SStephen Hurd 	SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
506823ac9029SStephen Hurd 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
506923ac9029SStephen Hurd 		       "driver version");
507023ac9029SStephen Hurd 
50714c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
50724c7070dbSScott Long 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
50734c7070dbSScott Long 			"# of txqs to use, 0 => use default #");
50744c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
507523ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
507623ac9029SStephen Hurd 			"# of rxqs to use, 0 => use default #");
507723ac9029SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
507823ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
507923ac9029SStephen Hurd                        "permit #txq != #rxq");
50804c7070dbSScott Long 
508123ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
508223ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
508323ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER,
508423ac9029SStephen Hurd                        mp_ndesc_handler, "A",
508523ac9029SStephen Hurd                        "list of # of tx descriptors to use, 0 = use default #");
508623ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
508723ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER,
508823ac9029SStephen Hurd                        mp_ndesc_handler, "A",
508923ac9029SStephen Hurd                        "list of # of rx descriptors to use, 0 = use default #");
50904c7070dbSScott Long }
50914c7070dbSScott Long 
50924c7070dbSScott Long static void
50934c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx)
50944c7070dbSScott Long {
50954c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
50964c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
50974c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
50984c7070dbSScott Long 	struct sysctl_oid_list *child;
50994c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
51004c7070dbSScott Long 	iflib_fl_t fl;
51014c7070dbSScott Long 	iflib_txq_t txq;
51024c7070dbSScott Long 	iflib_rxq_t rxq;
51034c7070dbSScott Long 	int i, j;
51044c7070dbSScott Long 	char namebuf[NAME_BUFLEN];
51054c7070dbSScott Long 	char *qfmt;
51064c7070dbSScott Long 	struct sysctl_oid *queue_node, *fl_node, *node;
51074c7070dbSScott Long 	struct sysctl_oid_list *queue_list, *fl_list;
51084c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
51094c7070dbSScott Long 
51104c7070dbSScott Long 	node = ctx->ifc_sysctl_node;
51114c7070dbSScott Long 	child = SYSCTL_CHILDREN(node);
51124c7070dbSScott Long 
51134c7070dbSScott Long 	if (scctx->isc_ntxqsets > 100)
51144c7070dbSScott Long 		qfmt = "txq%03d";
51154c7070dbSScott Long 	else if (scctx->isc_ntxqsets > 10)
51164c7070dbSScott Long 		qfmt = "txq%02d";
51174c7070dbSScott Long 	else
51184c7070dbSScott Long 		qfmt = "txq%d";
51194c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
51204c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
51214c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
51224c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
51234c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
51244c7070dbSScott Long #if MEMORY_LOGGING
51254c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
51264c7070dbSScott Long 				CTLFLAG_RD,
51274c7070dbSScott Long 				&txq->ift_dequeued, "total mbufs freed");
51284c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
51294c7070dbSScott Long 				CTLFLAG_RD,
51304c7070dbSScott Long 				&txq->ift_enqueued, "total mbufs enqueued");
51314c7070dbSScott Long #endif
51324c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
51334c7070dbSScott Long 				   CTLFLAG_RD,
51344c7070dbSScott Long 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
51354c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
51364c7070dbSScott Long 				   CTLFLAG_RD,
51374c7070dbSScott Long 				   &txq->ift_pullups, "# of times m_pullup was called");
51384c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
51394c7070dbSScott Long 				   CTLFLAG_RD,
51404c7070dbSScott Long 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
51414c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
51424c7070dbSScott Long 				   CTLFLAG_RD,
514323ac9029SStephen Hurd 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
51444c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
51454c7070dbSScott Long 				   CTLFLAG_RD,
51464c7070dbSScott Long 				   &txq->ift_map_failed, "# of times dma map failed");
51474c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
51484c7070dbSScott Long 				   CTLFLAG_RD,
51494c7070dbSScott Long 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
51504c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
51514c7070dbSScott Long 				   CTLFLAG_RD,
51524c7070dbSScott Long 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
51534c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
51544c7070dbSScott Long 				   CTLFLAG_RD,
51554c7070dbSScott Long 				   &txq->ift_pidx, 1, "Producer Index");
51564c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
51574c7070dbSScott Long 				   CTLFLAG_RD,
51584c7070dbSScott Long 				   &txq->ift_cidx, 1, "Consumer Index");
51594c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
51604c7070dbSScott Long 				   CTLFLAG_RD,
51614c7070dbSScott Long 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
51624c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
51634c7070dbSScott Long 				   CTLFLAG_RD,
51644c7070dbSScott Long 				   &txq->ift_in_use, 1, "descriptors in use");
51654c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
51664c7070dbSScott Long 				   CTLFLAG_RD,
51674c7070dbSScott Long 				   &txq->ift_processed, "descriptors procesed for clean");
51684c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
51694c7070dbSScott Long 				   CTLFLAG_RD,
51704c7070dbSScott Long 				   &txq->ift_cleaned, "total cleaned");
51714c7070dbSScott Long 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
51724c7070dbSScott Long 				CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br[0]->state),
51734c7070dbSScott Long 				0, mp_ring_state_handler, "A", "soft ring state");
51744c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
51754c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->enqueues,
51764c7070dbSScott Long 				       "# of enqueues to the mp_ring for this queue");
51774c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
51784c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->drops,
51794c7070dbSScott Long 				       "# of drops in the mp_ring for this queue");
51804c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
51814c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->starts,
51824c7070dbSScott Long 				       "# of normal consumer starts in the mp_ring for this queue");
51834c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
51844c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->stalls,
51854c7070dbSScott Long 					       "# of consumer stalls in the mp_ring for this queue");
51864c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
51874c7070dbSScott Long 			       CTLFLAG_RD, &txq->ift_br[0]->restarts,
51884c7070dbSScott Long 				       "# of consumer restarts in the mp_ring for this queue");
51894c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
51904c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->abdications,
51914c7070dbSScott Long 				       "# of consumer abdications in the mp_ring for this queue");
51924c7070dbSScott Long 	}
51934c7070dbSScott Long 
51944c7070dbSScott Long 	if (scctx->isc_nrxqsets > 100)
51954c7070dbSScott Long 		qfmt = "rxq%03d";
51964c7070dbSScott Long 	else if (scctx->isc_nrxqsets > 10)
51974c7070dbSScott Long 		qfmt = "rxq%02d";
51984c7070dbSScott Long 	else
51994c7070dbSScott Long 		qfmt = "rxq%d";
52004c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
52014c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
52024c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
52034c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
52044c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
520523ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
52064c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx",
52074c7070dbSScott Long 				       CTLFLAG_RD,
52084c7070dbSScott Long 				       &rxq->ifr_cq_pidx, 1, "Producer Index");
52094c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
52104c7070dbSScott Long 				       CTLFLAG_RD,
52114c7070dbSScott Long 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
52124c7070dbSScott Long 		}
5213da69b8f9SSean Bruno 
52144c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
52154c7070dbSScott Long 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
52164c7070dbSScott Long 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
52174c7070dbSScott Long 						     CTLFLAG_RD, NULL, "freelist Name");
52184c7070dbSScott Long 			fl_list = SYSCTL_CHILDREN(fl_node);
52194c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
52204c7070dbSScott Long 				       CTLFLAG_RD,
52214c7070dbSScott Long 				       &fl->ifl_pidx, 1, "Producer Index");
52224c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
52234c7070dbSScott Long 				       CTLFLAG_RD,
52244c7070dbSScott Long 				       &fl->ifl_cidx, 1, "Consumer Index");
52254c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
52264c7070dbSScott Long 				       CTLFLAG_RD,
52274c7070dbSScott Long 				       &fl->ifl_credits, 1, "credits available");
52284c7070dbSScott Long #if MEMORY_LOGGING
52294c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
52304c7070dbSScott Long 					CTLFLAG_RD,
52314c7070dbSScott Long 					&fl->ifl_m_enqueued, "mbufs allocated");
52324c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
52334c7070dbSScott Long 					CTLFLAG_RD,
52344c7070dbSScott Long 					&fl->ifl_m_dequeued, "mbufs freed");
52354c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
52364c7070dbSScott Long 					CTLFLAG_RD,
52374c7070dbSScott Long 					&fl->ifl_cl_enqueued, "clusters allocated");
52384c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
52394c7070dbSScott Long 					CTLFLAG_RD,
52404c7070dbSScott Long 					&fl->ifl_cl_dequeued, "clusters freed");
52414c7070dbSScott Long #endif
52424c7070dbSScott Long 
52434c7070dbSScott Long 		}
52444c7070dbSScott Long 	}
52454c7070dbSScott Long 
52464c7070dbSScott Long }
5247