xref: /freebsd/sys/net/iflib.c (revision 96eeabefbe7b3d14e195815f4913c7c08a52b716)
14c7070dbSScott Long /*-
24c7070dbSScott Long  * Copyright (c) 2014-2016, 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 
2674c7070dbSScott Long typedef struct iflib_sw_rx_desc {
2684c7070dbSScott Long 	bus_dmamap_t    ifsd_map;         /* bus_dma map for packet */
2694c7070dbSScott Long 	struct mbuf    *ifsd_m;           /* rx: uninitialized mbuf */
2704c7070dbSScott Long 	caddr_t         ifsd_cl;          /* direct cluster pointer for rx */
2714c7070dbSScott Long 	uint16_t	ifsd_flags;
2724c7070dbSScott Long } *iflib_rxsd_t;
2734c7070dbSScott Long 
2744c7070dbSScott Long typedef struct iflib_sw_tx_desc_val {
2754c7070dbSScott Long 	bus_dmamap_t    ifsd_map;         /* bus_dma map for packet */
2764c7070dbSScott Long 	struct mbuf    *ifsd_m;           /* pkthdr mbuf */
2774c7070dbSScott Long 	uint8_t		ifsd_flags;
2784c7070dbSScott Long } *iflib_txsd_val_t;
2794c7070dbSScott Long 
2804c7070dbSScott Long typedef struct iflib_sw_tx_desc_array {
2814c7070dbSScott Long 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
2824c7070dbSScott Long 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
2834c7070dbSScott Long 	uint8_t		*ifsd_flags;
2844c7070dbSScott Long } iflib_txsd_array_t;
2854c7070dbSScott Long 
2864c7070dbSScott Long 
2874c7070dbSScott Long /* magic number that should be high enough for any hardware */
2884c7070dbSScott Long #define IFLIB_MAX_TX_SEGS		128
2894c7070dbSScott Long #define IFLIB_MAX_RX_SEGS		32
2904c7070dbSScott Long #define IFLIB_RX_COPY_THRESH		128
2914c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH		32
2924c7070dbSScott Long #define IFLIB_QUEUE_IDLE		0
2934c7070dbSScott Long #define IFLIB_QUEUE_HUNG		1
2944c7070dbSScott Long #define IFLIB_QUEUE_WORKING		2
2954c7070dbSScott Long 
2964c7070dbSScott Long /* this should really scale with ring size - 32 is a fairly arbitrary value for this */
2974c7070dbSScott Long #define TX_BATCH_SIZE			16
2984c7070dbSScott Long 
2994c7070dbSScott Long #define IFLIB_RESTART_BUDGET		8
3004c7070dbSScott Long 
30123ac9029SStephen Hurd #define	IFC_LEGACY		0x01
30223ac9029SStephen Hurd #define	IFC_QFLUSH		0x02
30323ac9029SStephen Hurd #define	IFC_MULTISEG		0x04
30423ac9029SStephen Hurd #define	IFC_DMAR		0x08
30523ac9029SStephen Hurd #define	IFC_SC_ALLOCATED	0x10
3064ecb427aSSean Bruno #define	IFC_INIT_DONE		0x20
3074ecb427aSSean Bruno 
3084c7070dbSScott Long 
3094c7070dbSScott Long #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
3104c7070dbSScott Long 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
3114c7070dbSScott Long 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
3124c7070dbSScott Long struct iflib_txq {
3134c7070dbSScott Long 	uint16_t	ift_in_use;
3144c7070dbSScott Long 	uint16_t	ift_cidx;
3154c7070dbSScott Long 	uint16_t	ift_cidx_processed;
3164c7070dbSScott Long 	uint16_t	ift_pidx;
3174c7070dbSScott Long 	uint8_t		ift_gen;
3184c7070dbSScott Long 	uint8_t		ift_db_pending;
3194c7070dbSScott Long 	uint8_t		ift_db_pending_queued;
3204c7070dbSScott Long 	uint8_t		ift_npending;
32123ac9029SStephen Hurd 	uint8_t		ift_br_offset;
3224c7070dbSScott Long 	/* implicit pad */
3234c7070dbSScott Long 	uint64_t	ift_processed;
3244c7070dbSScott Long 	uint64_t	ift_cleaned;
3254c7070dbSScott Long #if MEMORY_LOGGING
3264c7070dbSScott Long 	uint64_t	ift_enqueued;
3274c7070dbSScott Long 	uint64_t	ift_dequeued;
3284c7070dbSScott Long #endif
3294c7070dbSScott Long 	uint64_t	ift_no_tx_dma_setup;
3304c7070dbSScott Long 	uint64_t	ift_no_desc_avail;
3314c7070dbSScott Long 	uint64_t	ift_mbuf_defrag_failed;
3324c7070dbSScott Long 	uint64_t	ift_mbuf_defrag;
3334c7070dbSScott Long 	uint64_t	ift_map_failed;
3344c7070dbSScott Long 	uint64_t	ift_txd_encap_efbig;
3354c7070dbSScott Long 	uint64_t	ift_pullups;
3364c7070dbSScott Long 
3374c7070dbSScott Long 	struct mtx	ift_mtx;
3384c7070dbSScott Long 	struct mtx	ift_db_mtx;
3394c7070dbSScott Long 
3404c7070dbSScott Long 	/* constant values */
3414c7070dbSScott Long 	if_ctx_t	ift_ctx;
3424c7070dbSScott Long 	struct ifmp_ring        **ift_br;
3434c7070dbSScott Long 	struct grouptask	ift_task;
3444c7070dbSScott Long 	uint16_t	ift_size;
3454c7070dbSScott Long 	uint16_t	ift_id;
3464c7070dbSScott Long 	struct callout	ift_timer;
3474c7070dbSScott Long 	struct callout	ift_db_check;
3484c7070dbSScott Long 
3494c7070dbSScott Long 	iflib_txsd_array_t	ift_sds;
3504c7070dbSScott Long 	uint8_t			ift_nbr;
3514c7070dbSScott Long 	uint8_t			ift_qstatus;
3524c7070dbSScott Long 	uint8_t			ift_active;
3534c7070dbSScott Long 	uint8_t			ift_closed;
3544c7070dbSScott Long 	int			ift_watchdog_time;
3554c7070dbSScott Long 	struct iflib_filter_info ift_filter_info;
3564c7070dbSScott Long 	bus_dma_tag_t		ift_desc_tag;
3574c7070dbSScott Long 	bus_dma_tag_t		ift_tso_desc_tag;
3584c7070dbSScott Long 	iflib_dma_info_t	ift_ifdi;
3594c7070dbSScott Long #define MTX_NAME_LEN 16
3604c7070dbSScott Long 	char                    ift_mtx_name[MTX_NAME_LEN];
3614c7070dbSScott Long 	char                    ift_db_mtx_name[MTX_NAME_LEN];
3624c7070dbSScott Long 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
3631248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
3641248952aSSean Bruno 	uint64_t ift_cpu_exec_count[256];
3651248952aSSean Bruno #endif
3664c7070dbSScott Long } __aligned(CACHE_LINE_SIZE);
3674c7070dbSScott Long 
3684c7070dbSScott Long struct iflib_fl {
3694c7070dbSScott Long 	uint16_t	ifl_cidx;
3704c7070dbSScott Long 	uint16_t	ifl_pidx;
3714c7070dbSScott Long 	uint16_t	ifl_credits;
3724c7070dbSScott Long 	uint8_t		ifl_gen;
3734c7070dbSScott Long #if MEMORY_LOGGING
3744c7070dbSScott Long 	uint64_t	ifl_m_enqueued;
3754c7070dbSScott Long 	uint64_t	ifl_m_dequeued;
3764c7070dbSScott Long 	uint64_t	ifl_cl_enqueued;
3774c7070dbSScott Long 	uint64_t	ifl_cl_dequeued;
3784c7070dbSScott Long #endif
3794c7070dbSScott Long 	/* implicit pad */
3804c7070dbSScott Long 
3814c7070dbSScott Long 	/* constant */
3824c7070dbSScott Long 	uint16_t	ifl_size;
3834c7070dbSScott Long 	uint16_t	ifl_buf_size;
3844c7070dbSScott Long 	uint16_t	ifl_cltype;
3854c7070dbSScott Long 	uma_zone_t	ifl_zone;
3864c7070dbSScott Long 	iflib_rxsd_t	ifl_sds;
3874c7070dbSScott Long 	iflib_rxq_t	ifl_rxq;
3884c7070dbSScott Long 	uint8_t		ifl_id;
3894c7070dbSScott Long 	bus_dma_tag_t           ifl_desc_tag;
3904c7070dbSScott Long 	iflib_dma_info_t	ifl_ifdi;
3914c7070dbSScott Long 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
3924c7070dbSScott Long 	caddr_t		ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
3934c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
3944c7070dbSScott Long 
3954c7070dbSScott Long static inline int
3964c7070dbSScott Long get_inuse(int size, int cidx, int pidx, int gen)
3974c7070dbSScott Long {
3984c7070dbSScott Long 	int used;
3994c7070dbSScott Long 
4004c7070dbSScott Long 	if (pidx > cidx)
4014c7070dbSScott Long 		used = pidx - cidx;
4024c7070dbSScott Long 	else if (pidx < cidx)
4034c7070dbSScott Long 		used = size - cidx + pidx;
4044c7070dbSScott Long 	else if (gen == 0 && pidx == cidx)
4054c7070dbSScott Long 		used = 0;
4064c7070dbSScott Long 	else if (gen == 1 && pidx == cidx)
4074c7070dbSScott Long 		used = size;
4084c7070dbSScott Long 	else
4094c7070dbSScott Long 		panic("bad state");
4104c7070dbSScott Long 
4114c7070dbSScott Long 	return (used);
4124c7070dbSScott Long }
4134c7070dbSScott Long 
4144c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
4154c7070dbSScott Long 
4164c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \
4174c7070dbSScott Long 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
4184c7070dbSScott Long 
4194c7070dbSScott Long struct iflib_rxq {
4204c7070dbSScott Long 	/* If there is a separate completion queue -
4214c7070dbSScott Long 	 * these are the cq cidx and pidx. Otherwise
4224c7070dbSScott Long 	 * these are unused.
4234c7070dbSScott Long 	 */
4244c7070dbSScott Long 	uint16_t	ifr_size;
4254c7070dbSScott Long 	uint16_t	ifr_cq_cidx;
4264c7070dbSScott Long 	uint16_t	ifr_cq_pidx;
4274c7070dbSScott Long 	uint8_t		ifr_cq_gen;
42823ac9029SStephen Hurd 	uint8_t		ifr_fl_offset;
4294c7070dbSScott Long 
4304c7070dbSScott Long 	if_ctx_t	ifr_ctx;
4314c7070dbSScott Long 	iflib_fl_t	ifr_fl;
4324c7070dbSScott Long 	uint64_t	ifr_rx_irq;
4334c7070dbSScott Long 	uint16_t	ifr_id;
4344c7070dbSScott Long 	uint8_t		ifr_lro_enabled;
4354c7070dbSScott Long 	uint8_t		ifr_nfl;
4364c7070dbSScott Long 	struct lro_ctrl			ifr_lc;
4374c7070dbSScott Long 	struct grouptask        ifr_task;
4384c7070dbSScott Long 	struct iflib_filter_info ifr_filter_info;
4394c7070dbSScott Long 	iflib_dma_info_t		ifr_ifdi;
4404c7070dbSScott Long 	/* dynamically allocate if any drivers need a value substantially larger than this */
4414c7070dbSScott Long 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
4421248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
4431248952aSSean Bruno 	uint64_t ifr_cpu_exec_count[256];
4441248952aSSean Bruno #endif
4454c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4464c7070dbSScott Long 
4474c7070dbSScott Long /*
4484c7070dbSScott Long  * Only allow a single packet to take up most 1/nth of the tx ring
4494c7070dbSScott Long  */
4504c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12
4514c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1
4524c7070dbSScott Long 
4534c7070dbSScott Long static int enable_msix = 1;
4544c7070dbSScott Long 
4554c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
4564c7070dbSScott Long 
4574c7070dbSScott Long #define CTX_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_mtx, _name, "iflib ctx lock", MTX_DEF)
4584c7070dbSScott Long 
4594c7070dbSScott Long #define CTX_LOCK(ctx) mtx_lock(&(ctx)->ifc_mtx)
4604c7070dbSScott Long #define CTX_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_mtx)
4614c7070dbSScott Long #define CTX_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_mtx)
4624c7070dbSScott Long 
4634c7070dbSScott Long 
4644c7070dbSScott Long #define TXDB_LOCK_INIT(txq)  mtx_init(&(txq)->ift_db_mtx, (txq)->ift_db_mtx_name, NULL, MTX_DEF)
4654c7070dbSScott Long #define TXDB_TRYLOCK(txq) mtx_trylock(&(txq)->ift_db_mtx)
4664c7070dbSScott Long #define TXDB_LOCK(txq) mtx_lock(&(txq)->ift_db_mtx)
4674c7070dbSScott Long #define TXDB_UNLOCK(txq) mtx_unlock(&(txq)->ift_db_mtx)
4684c7070dbSScott Long #define TXDB_LOCK_DESTROY(txq) mtx_destroy(&(txq)->ift_db_mtx)
4694c7070dbSScott Long 
4704c7070dbSScott Long #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
4714c7070dbSScott Long #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
4724c7070dbSScott Long 
4734c7070dbSScott Long 
4744c7070dbSScott Long /* Our boot-time initialization hook */
4754c7070dbSScott Long static int	iflib_module_event_handler(module_t, int, void *);
4764c7070dbSScott Long 
4774c7070dbSScott Long static moduledata_t iflib_moduledata = {
4784c7070dbSScott Long 	"iflib",
4794c7070dbSScott Long 	iflib_module_event_handler,
4804c7070dbSScott Long 	NULL
4814c7070dbSScott Long };
4824c7070dbSScott Long 
4834c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
4844c7070dbSScott Long MODULE_VERSION(iflib, 1);
4854c7070dbSScott Long 
4864c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1);
4874c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1);
4884c7070dbSScott Long 
4894c7070dbSScott Long TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
4904c7070dbSScott Long TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
4914c7070dbSScott Long 
4924c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS
4934c7070dbSScott Long #ifdef INVARIANTS
4944c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1
4954c7070dbSScott Long #else
4964c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0
4974c7070dbSScott Long #endif /* !INVARIANTS */
4984c7070dbSScott Long #endif
4994c7070dbSScott Long 
5004c7070dbSScott Long static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0,
5014c7070dbSScott Long                    "iflib driver parameters");
5024c7070dbSScott Long 
5034c7070dbSScott Long /*
5044c7070dbSScott Long  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
5054c7070dbSScott Long  */
5064c7070dbSScott Long static int iflib_min_tx_latency = 0;
5074c7070dbSScott Long 
5084c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
509da69b8f9SSean Bruno 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
5104c7070dbSScott Long 
5114c7070dbSScott Long 
5124c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS
5134c7070dbSScott Long 
5144c7070dbSScott Long static int iflib_tx_seen;
5154c7070dbSScott Long static int iflib_tx_sent;
5164c7070dbSScott Long static int iflib_tx_encap;
5174c7070dbSScott Long static int iflib_rx_allocs;
5184c7070dbSScott Long static int iflib_fl_refills;
5194c7070dbSScott Long static int iflib_fl_refills_large;
5204c7070dbSScott Long static int iflib_tx_frees;
5214c7070dbSScott Long 
5224c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
5234c7070dbSScott Long 		   &iflib_tx_seen, 0, "# tx mbufs seen");
5244c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
5254c7070dbSScott Long 		   &iflib_tx_sent, 0, "# tx mbufs sent");
5264c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
5274c7070dbSScott Long 		   &iflib_tx_encap, 0, "# tx mbufs encapped");
5284c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
5294c7070dbSScott Long 		   &iflib_tx_frees, 0, "# tx frees");
5304c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
5314c7070dbSScott Long 		   &iflib_rx_allocs, 0, "# rx allocations");
5324c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
5334c7070dbSScott Long 		   &iflib_fl_refills, 0, "# refills");
5344c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
5354c7070dbSScott Long 		   &iflib_fl_refills_large, 0, "# large refills");
5364c7070dbSScott Long 
5374c7070dbSScott Long 
5384c7070dbSScott Long static int iflib_txq_drain_flushing;
5394c7070dbSScott Long static int iflib_txq_drain_oactive;
5404c7070dbSScott Long static int iflib_txq_drain_notready;
5414c7070dbSScott Long static int iflib_txq_drain_encapfail;
5424c7070dbSScott Long 
5434c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
5444c7070dbSScott Long 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
5454c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
5464c7070dbSScott Long 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
5474c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
5484c7070dbSScott Long 		   &iflib_txq_drain_notready, 0, "# drain notready");
5494c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_encapfail, CTLFLAG_RD,
5504c7070dbSScott Long 		   &iflib_txq_drain_encapfail, 0, "# drain encap fails");
5514c7070dbSScott Long 
5524c7070dbSScott Long 
5534c7070dbSScott Long static int iflib_encap_load_mbuf_fail;
5544c7070dbSScott Long static int iflib_encap_txq_avail_fail;
5554c7070dbSScott Long static int iflib_encap_txd_encap_fail;
5564c7070dbSScott Long 
5574c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
5584c7070dbSScott Long 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
5594c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
5604c7070dbSScott Long 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
5614c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
5624c7070dbSScott Long 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
5634c7070dbSScott Long 
5644c7070dbSScott Long static int iflib_task_fn_rxs;
5654c7070dbSScott Long static int iflib_rx_intr_enables;
5664c7070dbSScott Long static int iflib_fast_intrs;
5674c7070dbSScott Long static int iflib_intr_link;
5684c7070dbSScott Long static int iflib_intr_msix;
5694c7070dbSScott Long static int iflib_rx_unavail;
5704c7070dbSScott Long static int iflib_rx_ctx_inactive;
5714c7070dbSScott Long static int iflib_rx_zero_len;
5724c7070dbSScott Long static int iflib_rx_if_input;
5734c7070dbSScott Long static int iflib_rx_mbuf_null;
5744c7070dbSScott Long static int iflib_rxd_flush;
5754c7070dbSScott Long 
5764c7070dbSScott Long static int iflib_verbose_debug;
5774c7070dbSScott Long 
5784c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_link, CTLFLAG_RD,
5794c7070dbSScott Long 		   &iflib_intr_link, 0, "# intr link calls");
5804c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_msix, CTLFLAG_RD,
5814c7070dbSScott Long 		   &iflib_intr_msix, 0, "# intr msix calls");
5824c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
5834c7070dbSScott Long 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
5844c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
5854c7070dbSScott Long 		   &iflib_rx_intr_enables, 0, "# rx intr enables");
5864c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
5874c7070dbSScott Long 		   &iflib_fast_intrs, 0, "# fast_intr calls");
5884c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
5894c7070dbSScott Long 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
5904c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
5914c7070dbSScott Long 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
5924c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_zero_len, CTLFLAG_RD,
5934c7070dbSScott Long 		   &iflib_rx_zero_len, 0, "# times rxeof saw zero len mbuf");
5944c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
5954c7070dbSScott Long 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
5964c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD,
5974c7070dbSScott Long 		   &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf");
5984c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
5994c7070dbSScott Long 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
6004c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
6014c7070dbSScott Long 		   &iflib_verbose_debug, 0, "enable verbose debugging");
6024c7070dbSScott Long 
6034c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
604da69b8f9SSean Bruno static void
605da69b8f9SSean Bruno iflib_debug_reset(void)
606da69b8f9SSean Bruno {
607da69b8f9SSean Bruno 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
608da69b8f9SSean Bruno 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
609da69b8f9SSean Bruno 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
610da69b8f9SSean Bruno 		iflib_txq_drain_notready = iflib_txq_drain_encapfail =
611da69b8f9SSean Bruno 		iflib_encap_load_mbuf_fail = iflib_encap_txq_avail_fail =
612da69b8f9SSean Bruno 		iflib_encap_txd_encap_fail = iflib_task_fn_rxs = iflib_rx_intr_enables =
613da69b8f9SSean Bruno 		iflib_fast_intrs = iflib_intr_link = iflib_intr_msix = iflib_rx_unavail =
614da69b8f9SSean Bruno 		iflib_rx_ctx_inactive = iflib_rx_zero_len = iflib_rx_if_input =
615da69b8f9SSean Bruno 		iflib_rx_mbuf_null = iflib_rxd_flush = 0;
616da69b8f9SSean Bruno }
6174c7070dbSScott Long 
6184c7070dbSScott Long #else
6194c7070dbSScott Long #define DBG_COUNTER_INC(name)
620da69b8f9SSean Bruno static void iflib_debug_reset(void) {}
6214c7070dbSScott Long #endif
6224c7070dbSScott Long 
6234c7070dbSScott Long 
6244c7070dbSScott Long 
6254c7070dbSScott Long #define IFLIB_DEBUG 0
6264c7070dbSScott Long 
6274c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx);
6284c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx);
6294c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx);
6304c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
63123ac9029SStephen Hurd static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, int cidx, int budget);
6324c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx);
6334c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx);
6344c7070dbSScott Long static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, char *str);
6354c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
6364c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
6374c7070dbSScott Long static int iflib_register(if_ctx_t);
6384c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx);
6394c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
6404c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx);
641da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq);
6421248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx);
6434c7070dbSScott Long 
6444c7070dbSScott Long #ifdef DEV_NETMAP
6454c7070dbSScott Long #include <sys/selinfo.h>
6464c7070dbSScott Long #include <net/netmap.h>
6474c7070dbSScott Long #include <dev/netmap/netmap_kern.h>
6484c7070dbSScott Long 
6494c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1);
6504c7070dbSScott Long 
6514c7070dbSScott Long /*
6524c7070dbSScott Long  * device-specific sysctl variables:
6534c7070dbSScott Long  *
65491d546a0SConrad Meyer  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
6554c7070dbSScott Long  *	During regular operations the CRC is stripped, but on some
6564c7070dbSScott Long  *	hardware reception of frames not multiple of 64 is slower,
6574c7070dbSScott Long  *	so using crcstrip=0 helps in benchmarks.
6584c7070dbSScott Long  *
65991d546a0SConrad Meyer  * iflib_rx_miss, iflib_rx_miss_bufs:
6604c7070dbSScott Long  *	count packets that might be missed due to lost interrupts.
6614c7070dbSScott Long  */
6624c7070dbSScott Long SYSCTL_DECL(_dev_netmap);
6634c7070dbSScott Long /*
6644c7070dbSScott Long  * The xl driver by default strips CRCs and we do not override it.
6654c7070dbSScott Long  */
6664c7070dbSScott Long 
6674c7070dbSScott Long int iflib_crcstrip = 1;
6684c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
6694c7070dbSScott Long     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames");
6704c7070dbSScott Long 
6714c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs;
6724c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
6734c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr");
67491d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
6754c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs");
6764c7070dbSScott Long 
6774c7070dbSScott Long /*
6784c7070dbSScott Long  * Register/unregister. We are already under netmap lock.
6794c7070dbSScott Long  * Only called on the first register or the last unregister.
6804c7070dbSScott Long  */
6814c7070dbSScott Long static int
6824c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff)
6834c7070dbSScott Long {
6844c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
6854c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
6864c7070dbSScott Long 
6874c7070dbSScott Long 	CTX_LOCK(ctx);
6884c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
6894c7070dbSScott Long 
6904c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
6914c7070dbSScott Long 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6924c7070dbSScott Long 
6934c7070dbSScott Long 	if (!CTX_IS_VF(ctx))
6941248952aSSean Bruno 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
6954c7070dbSScott Long 
6964c7070dbSScott Long 	/* enable or disable flags and callbacks in na and ifp */
6974c7070dbSScott Long 	if (onoff) {
6984c7070dbSScott Long 		nm_set_native_flags(na);
6994c7070dbSScott Long 	} else {
7004c7070dbSScott Long 		nm_clear_native_flags(na);
7014c7070dbSScott Long 	}
7024c7070dbSScott Long 	IFDI_INIT(ctx);
7031248952aSSean Bruno 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
7044c7070dbSScott Long 	CTX_UNLOCK(ctx);
7054c7070dbSScott Long 	return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
7064c7070dbSScott Long }
7074c7070dbSScott Long 
7084c7070dbSScott Long /*
7094c7070dbSScott Long  * Reconcile kernel and user view of the transmit ring.
7104c7070dbSScott Long  *
7114c7070dbSScott Long  * All information is in the kring.
7124c7070dbSScott Long  * Userspace wants to send packets up to the one before kring->rhead,
7134c7070dbSScott Long  * kernel knows kring->nr_hwcur is the first unsent packet.
7144c7070dbSScott Long  *
7154c7070dbSScott Long  * Here we push packets out (as many as possible), and possibly
7164c7070dbSScott Long  * reclaim buffers from previously completed transmission.
7174c7070dbSScott Long  *
7184c7070dbSScott Long  * The caller (netmap) guarantees that there is only one instance
7194c7070dbSScott Long  * running at any time. Any interference with other driver
7204c7070dbSScott Long  * methods should be handled by the individual drivers.
7214c7070dbSScott Long  */
7224c7070dbSScott Long static int
7234c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags)
7244c7070dbSScott Long {
7254c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
7264c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
7274c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
7284c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
7294c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
7304c7070dbSScott Long 	u_int n;
7314c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
7324c7070dbSScott Long 	u_int const head = kring->rhead;
7334c7070dbSScott Long 	struct if_pkt_info pi;
7344c7070dbSScott Long 
7354c7070dbSScott Long 	/*
7364c7070dbSScott Long 	 * interrupts on every tx packet are expensive so request
7374c7070dbSScott Long 	 * them every half ring, or where NS_REPORT is set
7384c7070dbSScott Long 	 */
7394c7070dbSScott Long 	u_int report_frequency = kring->nkr_num_slots >> 1;
7404c7070dbSScott Long 	/* device-specific */
7414c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
7424c7070dbSScott Long 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
7434c7070dbSScott Long 
7444c7070dbSScott Long 	pi.ipi_segs = txq->ift_segs;
7454c7070dbSScott Long 	pi.ipi_qsidx = kring->ring_id;
7464c7070dbSScott Long 	pi.ipi_ndescs = 0;
7474c7070dbSScott Long 
7484c7070dbSScott Long 	bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
7494c7070dbSScott Long 					BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
7504c7070dbSScott Long 
7514c7070dbSScott Long 
7524c7070dbSScott Long 	/*
7534c7070dbSScott Long 	 * First part: process new packets to send.
7544c7070dbSScott Long 	 * nm_i is the current index in the netmap ring,
7554c7070dbSScott Long 	 * nic_i is the corresponding index in the NIC ring.
7564c7070dbSScott Long 	 *
7574c7070dbSScott Long 	 * If we have packets to send (nm_i != head)
7584c7070dbSScott Long 	 * iterate over the netmap ring, fetch length and update
7594c7070dbSScott Long 	 * the corresponding slot in the NIC ring. Some drivers also
7604c7070dbSScott Long 	 * need to update the buffer's physical address in the NIC slot
7614c7070dbSScott Long 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
7624c7070dbSScott Long 	 *
7634c7070dbSScott Long 	 * The netmap_reload_map() calls is especially expensive,
7644c7070dbSScott Long 	 * even when (as in this case) the tag is 0, so do only
7654c7070dbSScott Long 	 * when the buffer has actually changed.
7664c7070dbSScott Long 	 *
7674c7070dbSScott Long 	 * If possible do not set the report/intr bit on all slots,
7684c7070dbSScott Long 	 * but only a few times per ring or when NS_REPORT is set.
7694c7070dbSScott Long 	 *
7704c7070dbSScott Long 	 * Finally, on 10G and faster drivers, it might be useful
7714c7070dbSScott Long 	 * to prefetch the next slot and txr entry.
7724c7070dbSScott Long 	 */
7734c7070dbSScott Long 
7744c7070dbSScott Long 	nm_i = kring->nr_hwcur;
7754c7070dbSScott Long 	if (nm_i != head) {	/* we have new packets to send */
7764c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
7774c7070dbSScott Long 
7784c7070dbSScott Long 		__builtin_prefetch(&ring->slot[nm_i]);
7794c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
7804c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
7814c7070dbSScott Long 
7824c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
7834c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
7844c7070dbSScott Long 			u_int len = slot->len;
7854c7070dbSScott Long 			uint64_t paddr;
7864c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
7874c7070dbSScott Long 			int flags = (slot->flags & NS_REPORT ||
7884c7070dbSScott Long 				nic_i == 0 || nic_i == report_frequency) ?
7894c7070dbSScott Long 				IPI_TX_INTR : 0;
7904c7070dbSScott Long 
7914c7070dbSScott Long 			/* device-specific */
7924c7070dbSScott Long 			pi.ipi_pidx = nic_i;
7934c7070dbSScott Long 			pi.ipi_flags = flags;
7944c7070dbSScott Long 
7954c7070dbSScott Long 			/* Fill the slot in the NIC ring. */
7964c7070dbSScott Long 			ctx->isc_txd_encap(ctx->ifc_softc, &pi);
7974c7070dbSScott Long 
7984c7070dbSScott Long 			/* prefetch for next round */
7994c7070dbSScott Long 			__builtin_prefetch(&ring->slot[nm_i + 1]);
8004c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
8014c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
8024c7070dbSScott Long 
8034c7070dbSScott Long 			NM_CHECK_ADDR_LEN(na, addr, len);
8044c7070dbSScott Long 
8054c7070dbSScott Long 			if (slot->flags & NS_BUF_CHANGED) {
8064c7070dbSScott Long 				/* buffer has changed, reload map */
8074c7070dbSScott Long 				netmap_reload_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[nic_i], addr);
8084c7070dbSScott Long 			}
8094c7070dbSScott Long 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
8104c7070dbSScott Long 
8114c7070dbSScott Long 			/* make sure changes to the buffer are synced */
8124c7070dbSScott Long 			bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_sds.ifsd_map[nic_i],
8134c7070dbSScott Long 							BUS_DMASYNC_PREWRITE);
8144c7070dbSScott Long 
8154c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
8164c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
8174c7070dbSScott Long 		}
8184c7070dbSScott Long 		kring->nr_hwcur = head;
8194c7070dbSScott Long 
8204c7070dbSScott Long 		/* synchronize the NIC ring */
8214c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
8224c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
8234c7070dbSScott Long 
8244c7070dbSScott Long 		/* (re)start the tx unit up to slot nic_i (excluded) */
8254c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
8264c7070dbSScott Long 	}
8274c7070dbSScott Long 
8284c7070dbSScott Long 	/*
8294c7070dbSScott Long 	 * Second part: reclaim buffers for completed transmissions.
8304c7070dbSScott Long 	 */
8314c7070dbSScott Long 	if (iflib_tx_credits_update(ctx, txq)) {
8324c7070dbSScott Long 		/* some tx completed, increment avail */
8334c7070dbSScott Long 		nic_i = txq->ift_cidx_processed;
8344c7070dbSScott Long 		kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
8354c7070dbSScott Long 	}
8364c7070dbSScott Long 	return (0);
8374c7070dbSScott Long }
8384c7070dbSScott Long 
8394c7070dbSScott Long /*
8404c7070dbSScott Long  * Reconcile kernel and user view of the receive ring.
8414c7070dbSScott Long  * Same as for the txsync, this routine must be efficient.
8424c7070dbSScott Long  * The caller guarantees a single invocations, but races against
8434c7070dbSScott Long  * the rest of the driver should be handled here.
8444c7070dbSScott Long  *
8454c7070dbSScott Long  * On call, kring->rhead is the first packet that userspace wants
8464c7070dbSScott Long  * to keep, and kring->rcur is the wakeup point.
8474c7070dbSScott Long  * The kernel has previously reported packets up to kring->rtail.
8484c7070dbSScott Long  *
8494c7070dbSScott Long  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
8504c7070dbSScott Long  * of whether or not we received an interrupt.
8514c7070dbSScott Long  */
8524c7070dbSScott Long static int
8534c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
8544c7070dbSScott Long {
8554c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
8564c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
8574c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
8584c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
8594c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
8604c7070dbSScott Long 	u_int i, n;
8614c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
8624c7070dbSScott Long 	u_int const head = kring->rhead;
8634c7070dbSScott Long 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
8644c7070dbSScott Long 	struct if_rxd_info ri;
8654c7070dbSScott Long 	/* device-specific */
8664c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
8674c7070dbSScott Long 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
8684c7070dbSScott Long 	iflib_fl_t fl = rxq->ifr_fl;
8694c7070dbSScott Long 	if (head > lim)
8704c7070dbSScott Long 		return netmap_ring_reinit(kring);
8714c7070dbSScott Long 
8724c7070dbSScott Long 	bzero(&ri, sizeof(ri));
8734c7070dbSScott Long 	ri.iri_qsidx = kring->ring_id;
8744c7070dbSScott Long 	ri.iri_ifp = ctx->ifc_ifp;
8754c7070dbSScott Long 	/* XXX check sync modes */
8764c7070dbSScott Long 	for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++)
8774c7070dbSScott Long 		bus_dmamap_sync(rxq->ifr_fl[i].ifl_desc_tag, fl->ifl_ifdi->idi_map,
8784c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
8794c7070dbSScott Long 
8804c7070dbSScott Long 	/*
8814c7070dbSScott Long 	 * First part: import newly received packets.
8824c7070dbSScott Long 	 *
8834c7070dbSScott Long 	 * nm_i is the index of the next free slot in the netmap ring,
8844c7070dbSScott Long 	 * nic_i is the index of the next received packet in the NIC ring,
8854c7070dbSScott Long 	 * and they may differ in case if_init() has been called while
8864c7070dbSScott Long 	 * in netmap mode. For the receive ring we have
8874c7070dbSScott Long 	 *
8884c7070dbSScott Long 	 *	nic_i = rxr->next_check;
8894c7070dbSScott Long 	 *	nm_i = kring->nr_hwtail (previous)
8904c7070dbSScott Long 	 * and
8914c7070dbSScott Long 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
8924c7070dbSScott Long 	 *
8934c7070dbSScott Long 	 * rxr->next_check is set to 0 on a ring reinit
8944c7070dbSScott Long 	 */
8954c7070dbSScott Long 	if (netmap_no_pendintr || force_update) {
8964c7070dbSScott Long 		int crclen = iflib_crcstrip ? 0 : 4;
8974c7070dbSScott Long 		int error, avail;
8984c7070dbSScott Long 		uint16_t slot_flags = kring->nkr_slot_flags;
8994c7070dbSScott Long 
9004c7070dbSScott Long 		for (fl = rxq->ifr_fl, i = 0; i < rxq->ifr_nfl; i++, fl++) {
9014c7070dbSScott Long 			nic_i = fl->ifl_cidx;
9024c7070dbSScott Long 			nm_i = netmap_idx_n2k(kring, nic_i);
90323ac9029SStephen Hurd 			avail = ctx->isc_rxd_available(ctx->ifc_softc, kring->ring_id, nic_i, INT_MAX);
9044c7070dbSScott Long 			for (n = 0; avail > 0; n++, avail--) {
9054c7070dbSScott Long 				error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
9064c7070dbSScott Long 				if (error)
9074c7070dbSScott Long 					ring->slot[nm_i].len = 0;
9084c7070dbSScott Long 				else
9094c7070dbSScott Long 					ring->slot[nm_i].len = ri.iri_len - crclen;
9104c7070dbSScott Long 				ring->slot[nm_i].flags = slot_flags;
9114c7070dbSScott Long 				bus_dmamap_sync(fl->ifl_ifdi->idi_tag,
9124c7070dbSScott Long 								fl->ifl_sds[nic_i].ifsd_map, BUS_DMASYNC_POSTREAD);
9134c7070dbSScott Long 				nm_i = nm_next(nm_i, lim);
9144c7070dbSScott Long 				nic_i = nm_next(nic_i, lim);
9154c7070dbSScott Long 			}
9164c7070dbSScott Long 			if (n) { /* update the state variables */
9174c7070dbSScott Long 				if (netmap_no_pendintr && !force_update) {
9184c7070dbSScott Long 					/* diagnostics */
9194c7070dbSScott Long 					iflib_rx_miss ++;
9204c7070dbSScott Long 					iflib_rx_miss_bufs += n;
9214c7070dbSScott Long 				}
9224c7070dbSScott Long 				fl->ifl_cidx = nic_i;
9234c7070dbSScott Long 				kring->nr_hwtail = nm_i;
9244c7070dbSScott Long 			}
9254c7070dbSScott Long 			kring->nr_kflags &= ~NKR_PENDINTR;
9264c7070dbSScott Long 		}
9274c7070dbSScott Long 	}
9284c7070dbSScott Long 	/*
9294c7070dbSScott Long 	 * Second part: skip past packets that userspace has released.
9304c7070dbSScott Long 	 * (kring->nr_hwcur to head excluded),
9314c7070dbSScott Long 	 * and make the buffers available for reception.
9324c7070dbSScott Long 	 * As usual nm_i is the index in the netmap ring,
9334c7070dbSScott Long 	 * nic_i is the index in the NIC ring, and
9344c7070dbSScott Long 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
9354c7070dbSScott Long 	 */
9364c7070dbSScott Long 	/* XXX not sure how this will work with multiple free lists */
9374c7070dbSScott Long 	nm_i = kring->nr_hwcur;
9384c7070dbSScott Long 	if (nm_i != head) {
9394c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
9404c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
9414c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
9424c7070dbSScott Long 			uint64_t paddr;
9434c7070dbSScott Long 			caddr_t vaddr;
9444c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
9454c7070dbSScott Long 
9464c7070dbSScott Long 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
9474c7070dbSScott Long 				goto ring_reset;
9484c7070dbSScott Long 
9494c7070dbSScott Long 			vaddr = addr;
9504c7070dbSScott Long 			if (slot->flags & NS_BUF_CHANGED) {
9514c7070dbSScott Long 				/* buffer has changed, reload map */
9524c7070dbSScott Long 				netmap_reload_map(na, fl->ifl_ifdi->idi_tag, fl->ifl_sds[nic_i].ifsd_map, addr);
9534c7070dbSScott Long 				slot->flags &= ~NS_BUF_CHANGED;
9544c7070dbSScott Long 			}
9554c7070dbSScott Long 			/*
9564c7070dbSScott Long 			 * XXX we should be batching this operation - TODO
9574c7070dbSScott Long 			 */
95823ac9029SStephen Hurd 			ctx->isc_rxd_refill(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i, &paddr, &vaddr, 1, fl->ifl_buf_size);
9594c7070dbSScott Long 			bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_sds[nic_i].ifsd_map,
9604c7070dbSScott Long 			    BUS_DMASYNC_PREREAD);
9614c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
9624c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
9634c7070dbSScott Long 		}
9644c7070dbSScott Long 		kring->nr_hwcur = head;
9654c7070dbSScott Long 
9664c7070dbSScott Long 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
9674c7070dbSScott Long 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
9684c7070dbSScott Long 		/*
9694c7070dbSScott Long 		 * IMPORTANT: we must leave one free slot in the ring,
9704c7070dbSScott Long 		 * so move nic_i back by one unit
9714c7070dbSScott Long 		 */
9724c7070dbSScott Long 		nic_i = nm_prev(nic_i, lim);
9734c7070dbSScott Long 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
9744c7070dbSScott Long 	}
9754c7070dbSScott Long 
9764c7070dbSScott Long 	return 0;
9774c7070dbSScott Long 
9784c7070dbSScott Long ring_reset:
9794c7070dbSScott Long 	return netmap_ring_reinit(kring);
9804c7070dbSScott Long }
9814c7070dbSScott Long 
9824c7070dbSScott Long static int
9834c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx)
9844c7070dbSScott Long {
9854c7070dbSScott Long 	struct netmap_adapter na;
98623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
9874c7070dbSScott Long 
9884c7070dbSScott Long 	bzero(&na, sizeof(na));
9894c7070dbSScott Long 
9904c7070dbSScott Long 	na.ifp = ctx->ifc_ifp;
9914c7070dbSScott Long 	na.na_flags = NAF_BDG_MAYSLEEP;
9924c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
9934c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
9944c7070dbSScott Long 
99523ac9029SStephen Hurd 	na.num_tx_desc = scctx->isc_ntxd[0];
99623ac9029SStephen Hurd 	na.num_rx_desc = scctx->isc_nrxd[0];
9974c7070dbSScott Long 	na.nm_txsync = iflib_netmap_txsync;
9984c7070dbSScott Long 	na.nm_rxsync = iflib_netmap_rxsync;
9994c7070dbSScott Long 	na.nm_register = iflib_netmap_register;
10004c7070dbSScott Long 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
10014c7070dbSScott Long 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
10024c7070dbSScott Long 	return (netmap_attach(&na));
10034c7070dbSScott Long }
10044c7070dbSScott Long 
10054c7070dbSScott Long static void
10064c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
10074c7070dbSScott Long {
10084c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
10094c7070dbSScott Long 	struct netmap_slot *slot;
10104c7070dbSScott Long 
10114c7070dbSScott Long 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
10124c7070dbSScott Long 	if (slot == 0)
10134c7070dbSScott Long 		return;
10144c7070dbSScott Long 
101523ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
10164c7070dbSScott Long 
10174c7070dbSScott Long 		/*
10184c7070dbSScott Long 		 * In netmap mode, set the map for the packet buffer.
10194c7070dbSScott Long 		 * NOTE: Some drivers (not this one) also need to set
10204c7070dbSScott Long 		 * the physical buffer address in the NIC ring.
10214c7070dbSScott Long 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
10224c7070dbSScott Long 		 * netmap slot index, si
10234c7070dbSScott Long 		 */
10244c7070dbSScott Long 		int si = netmap_idx_n2k(&na->tx_rings[txq->ift_id], i);
10254c7070dbSScott Long 		netmap_load_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[i], NMB(na, slot + si));
10264c7070dbSScott Long 	}
10274c7070dbSScott Long }
10284c7070dbSScott Long static void
10294c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
10304c7070dbSScott Long {
10314c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
10324c7070dbSScott Long 	struct netmap_slot *slot;
10334c7070dbSScott Long 	iflib_rxsd_t sd;
10344c7070dbSScott Long 	int nrxd;
10354c7070dbSScott Long 
10364c7070dbSScott Long 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
10374c7070dbSScott Long 	if (slot == 0)
10384c7070dbSScott Long 		return;
10394c7070dbSScott Long 	sd = rxq->ifr_fl[0].ifl_sds;
104023ac9029SStephen Hurd 	nrxd = ctx->ifc_softc_ctx.isc_nrxd[0];
10414c7070dbSScott Long 	for (int i = 0; i < nrxd; i++, sd++) {
10424c7070dbSScott Long 			int sj = netmap_idx_n2k(&na->rx_rings[rxq->ifr_id], i);
10434c7070dbSScott Long 			uint64_t paddr;
10444c7070dbSScott Long 			void *addr;
10454c7070dbSScott Long 			caddr_t vaddr;
10464c7070dbSScott Long 
10474c7070dbSScott Long 			vaddr = addr = PNMB(na, slot + sj, &paddr);
10484c7070dbSScott Long 			netmap_load_map(na, rxq->ifr_fl[0].ifl_ifdi->idi_tag, sd->ifsd_map, addr);
10494c7070dbSScott Long 			/* Update descriptor and the cached value */
105023ac9029SStephen 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);
10514c7070dbSScott Long 	}
10524c7070dbSScott Long 	/* preserve queue */
10534c7070dbSScott Long 	if (ctx->ifc_ifp->if_capenable & IFCAP_NETMAP) {
10544c7070dbSScott Long 		struct netmap_kring *kring = &na->rx_rings[rxq->ifr_id];
10554c7070dbSScott Long 		int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
10564c7070dbSScott Long 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, t);
10574c7070dbSScott Long 	} else
10584c7070dbSScott Long 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, nrxd-1);
10594c7070dbSScott Long }
10604c7070dbSScott Long 
10614c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp)
10624c7070dbSScott Long 
10634c7070dbSScott Long #else
10644c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq)
10654c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq)
10664c7070dbSScott Long #define iflib_netmap_detach(ifp)
10674c7070dbSScott Long 
10684c7070dbSScott Long #define iflib_netmap_attach(ctx) (0)
10694c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0)
10704c7070dbSScott Long 
10714c7070dbSScott Long #endif
10724c7070dbSScott Long 
10734c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
10744c7070dbSScott Long static __inline void
10754c7070dbSScott Long prefetch(void *x)
10764c7070dbSScott Long {
10774c7070dbSScott Long 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
10784c7070dbSScott Long }
10794c7070dbSScott Long #else
10804c7070dbSScott Long #define prefetch(x)
10814c7070dbSScott Long #endif
10824c7070dbSScott Long 
10834c7070dbSScott Long static void
10844c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
10854c7070dbSScott Long {
10864c7070dbSScott Long 	if (err)
10874c7070dbSScott Long 		return;
10884c7070dbSScott Long 	*(bus_addr_t *) arg = segs[0].ds_addr;
10894c7070dbSScott Long }
10904c7070dbSScott Long 
10914c7070dbSScott Long int
10924c7070dbSScott Long iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
10934c7070dbSScott Long {
10944c7070dbSScott Long 	int err;
10954c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
10964c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
10974c7070dbSScott Long 
10984c7070dbSScott Long 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
10994c7070dbSScott Long 
11004c7070dbSScott Long 	err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
11014c7070dbSScott Long 				sctx->isc_q_align, 0,	/* alignment, bounds */
11024c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* lowaddr */
11034c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* highaddr */
11044c7070dbSScott Long 				NULL, NULL,		/* filter, filterarg */
11054c7070dbSScott Long 				size,			/* maxsize */
11064c7070dbSScott Long 				1,			/* nsegments */
11074c7070dbSScott Long 				size,			/* maxsegsize */
11084c7070dbSScott Long 				BUS_DMA_ALLOCNOW,	/* flags */
11094c7070dbSScott Long 				NULL,			/* lockfunc */
11104c7070dbSScott Long 				NULL,			/* lockarg */
11114c7070dbSScott Long 				&dma->idi_tag);
11124c7070dbSScott Long 	if (err) {
11134c7070dbSScott Long 		device_printf(dev,
11144c7070dbSScott Long 		    "%s: bus_dma_tag_create failed: %d\n",
11154c7070dbSScott Long 		    __func__, err);
11164c7070dbSScott Long 		goto fail_0;
11174c7070dbSScott Long 	}
11184c7070dbSScott Long 
11194c7070dbSScott Long 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
11204c7070dbSScott Long 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
11214c7070dbSScott Long 	if (err) {
11224c7070dbSScott Long 		device_printf(dev,
11234c7070dbSScott Long 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
11244c7070dbSScott Long 		    __func__, (uintmax_t)size, err);
11254c7070dbSScott Long 		goto fail_1;
11264c7070dbSScott Long 	}
11274c7070dbSScott Long 
11284c7070dbSScott Long 	dma->idi_paddr = IF_BAD_DMA;
11294c7070dbSScott Long 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
11304c7070dbSScott Long 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
11314c7070dbSScott Long 	if (err || dma->idi_paddr == IF_BAD_DMA) {
11324c7070dbSScott Long 		device_printf(dev,
11334c7070dbSScott Long 		    "%s: bus_dmamap_load failed: %d\n",
11344c7070dbSScott Long 		    __func__, err);
11354c7070dbSScott Long 		goto fail_2;
11364c7070dbSScott Long 	}
11374c7070dbSScott Long 
11384c7070dbSScott Long 	dma->idi_size = size;
11394c7070dbSScott Long 	return (0);
11404c7070dbSScott Long 
11414c7070dbSScott Long fail_2:
11424c7070dbSScott Long 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
11434c7070dbSScott Long fail_1:
11444c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
11454c7070dbSScott Long fail_0:
11464c7070dbSScott Long 	dma->idi_tag = NULL;
11474c7070dbSScott Long 
11484c7070dbSScott Long 	return (err);
11494c7070dbSScott Long }
11504c7070dbSScott Long 
11514c7070dbSScott Long int
11524c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
11534c7070dbSScott Long {
11544c7070dbSScott Long 	int i, err;
11554c7070dbSScott Long 	iflib_dma_info_t *dmaiter;
11564c7070dbSScott Long 
11574c7070dbSScott Long 	dmaiter = dmalist;
11584c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++) {
11594c7070dbSScott Long 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
11604c7070dbSScott Long 			break;
11614c7070dbSScott Long 	}
11624c7070dbSScott Long 	if (err)
11634c7070dbSScott Long 		iflib_dma_free_multi(dmalist, i);
11644c7070dbSScott Long 	return (err);
11654c7070dbSScott Long }
11664c7070dbSScott Long 
11674c7070dbSScott Long void
11684c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma)
11694c7070dbSScott Long {
11704c7070dbSScott Long 	if (dma->idi_tag == NULL)
11714c7070dbSScott Long 		return;
11724c7070dbSScott Long 	if (dma->idi_paddr != IF_BAD_DMA) {
11734c7070dbSScott Long 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
11744c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
11754c7070dbSScott Long 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
11764c7070dbSScott Long 		dma->idi_paddr = IF_BAD_DMA;
11774c7070dbSScott Long 	}
11784c7070dbSScott Long 	if (dma->idi_vaddr != NULL) {
11794c7070dbSScott Long 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
11804c7070dbSScott Long 		dma->idi_vaddr = NULL;
11814c7070dbSScott Long 	}
11824c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
11834c7070dbSScott Long 	dma->idi_tag = NULL;
11844c7070dbSScott Long }
11854c7070dbSScott Long 
11864c7070dbSScott Long void
11874c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
11884c7070dbSScott Long {
11894c7070dbSScott Long 	int i;
11904c7070dbSScott Long 	iflib_dma_info_t *dmaiter = dmalist;
11914c7070dbSScott Long 
11924c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++)
11934c7070dbSScott Long 		iflib_dma_free(*dmaiter);
11944c7070dbSScott Long }
11954c7070dbSScott Long 
1196bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP
1197bd84f700SSean Bruno static const int iflib_started = 1;
1198bd84f700SSean Bruno #else
1199bd84f700SSean Bruno /*
1200bd84f700SSean Bruno  * We used to abuse the smp_started flag to decide if the queues have been
1201bd84f700SSean Bruno  * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()).
1202bd84f700SSean Bruno  * That gave bad races, since the SYSINIT() runs strictly after smp_started
1203bd84f700SSean Bruno  * is set.  Run a SYSINIT() strictly after that to just set a usable
1204bd84f700SSean Bruno  * completion flag.
1205bd84f700SSean Bruno  */
1206bd84f700SSean Bruno 
1207bd84f700SSean Bruno static int iflib_started;
1208bd84f700SSean Bruno 
1209bd84f700SSean Bruno static void
1210bd84f700SSean Bruno iflib_record_started(void *arg)
1211bd84f700SSean Bruno {
1212bd84f700SSean Bruno 	iflib_started = 1;
1213bd84f700SSean Bruno }
1214bd84f700SSean Bruno 
1215bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST,
1216bd84f700SSean Bruno 	iflib_record_started, NULL);
1217bd84f700SSean Bruno #endif
1218bd84f700SSean Bruno 
12194c7070dbSScott Long static int
12204c7070dbSScott Long iflib_fast_intr(void *arg)
12214c7070dbSScott Long {
12224c7070dbSScott Long 	iflib_filter_info_t info = arg;
12234c7070dbSScott Long 	struct grouptask *gtask = info->ifi_task;
12244c7070dbSScott Long 
1225bd84f700SSean Bruno 	if (!iflib_started)
12261248952aSSean Bruno 		return (FILTER_HANDLED);
12271248952aSSean Bruno 
12284c7070dbSScott Long 	DBG_COUNTER_INC(fast_intrs);
12294c7070dbSScott Long 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
12304c7070dbSScott Long 		return (FILTER_HANDLED);
12314c7070dbSScott Long 
12324c7070dbSScott Long 	GROUPTASK_ENQUEUE(gtask);
12334c7070dbSScott Long 	return (FILTER_HANDLED);
12344c7070dbSScott Long }
12354c7070dbSScott Long 
12364c7070dbSScott Long static int
12374c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
12384c7070dbSScott Long 	driver_filter_t filter, driver_intr_t handler, void *arg,
12394c7070dbSScott Long 				 char *name)
12404c7070dbSScott Long {
12414c7070dbSScott Long 	int rc;
12424c7070dbSScott Long 	struct resource *res;
12434c7070dbSScott Long 	void *tag;
12444c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
12454c7070dbSScott Long 
12464c7070dbSScott Long 	MPASS(rid < 512);
12474c7070dbSScott Long 	irq->ii_rid = rid;
12484c7070dbSScott Long 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid,
12494c7070dbSScott Long 				     RF_SHAREABLE | RF_ACTIVE);
12504c7070dbSScott Long 	if (res == NULL) {
12514c7070dbSScott Long 		device_printf(dev,
12524c7070dbSScott Long 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
12534c7070dbSScott Long 		return (ENOMEM);
12544c7070dbSScott Long 	}
12554c7070dbSScott Long 	irq->ii_res = res;
12564c7070dbSScott Long 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
12574c7070dbSScott Long 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
12584c7070dbSScott Long 						filter, handler, arg, &tag);
12594c7070dbSScott Long 	if (rc != 0) {
12604c7070dbSScott Long 		device_printf(dev,
12614c7070dbSScott Long 		    "failed to setup interrupt for rid %d, name %s: %d\n",
12624c7070dbSScott Long 					  rid, name ? name : "unknown", rc);
12634c7070dbSScott Long 		return (rc);
12644c7070dbSScott Long 	} else if (name)
1265f454e7ebSJohn Baldwin 		bus_describe_intr(dev, res, tag, "%s", name);
12664c7070dbSScott Long 
12674c7070dbSScott Long 	irq->ii_tag = tag;
12684c7070dbSScott Long 	return (0);
12694c7070dbSScott Long }
12704c7070dbSScott Long 
12714c7070dbSScott Long 
12724c7070dbSScott Long /*********************************************************************
12734c7070dbSScott Long  *
12744c7070dbSScott Long  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
12754c7070dbSScott Long  *  the information needed to transmit a packet on the wire. This is
12764c7070dbSScott Long  *  called only once at attach, setup is done every reset.
12774c7070dbSScott Long  *
12784c7070dbSScott Long  **********************************************************************/
12794c7070dbSScott Long 
12804c7070dbSScott Long static int
12814c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq)
12824c7070dbSScott Long {
12834c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
12844c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
12854c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
12864c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
12874c7070dbSScott Long 	int err, nsegments, ntsosegments;
12884c7070dbSScott Long 
12894c7070dbSScott Long 	nsegments = scctx->isc_tx_nsegments;
12904c7070dbSScott Long 	ntsosegments = scctx->isc_tx_tso_segments_max;
129123ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[0] > 0);
129223ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
12934c7070dbSScott Long 	MPASS(nsegments > 0);
12944c7070dbSScott Long 	MPASS(ntsosegments > 0);
12954c7070dbSScott Long 	/*
12964c7070dbSScott Long 	 * Setup DMA descriptor areas.
12974c7070dbSScott Long 	 */
12984c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
12994c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
13004c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
13014c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
13024c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
13034c7070dbSScott Long 			       sctx->isc_tx_maxsize,		/* maxsize */
13044c7070dbSScott Long 			       nsegments,	/* nsegments */
13054c7070dbSScott Long 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
13064c7070dbSScott Long 			       0,			/* flags */
13074c7070dbSScott Long 			       NULL,			/* lockfunc */
13084c7070dbSScott Long 			       NULL,			/* lockfuncarg */
13094c7070dbSScott Long 			       &txq->ift_desc_tag))) {
13104c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
13114c7070dbSScott Long 		device_printf(dev,"maxsize: %zd nsegments: %d maxsegsize: %zd\n",
13124c7070dbSScott Long 					  sctx->isc_tx_maxsize, nsegments, sctx->isc_tx_maxsegsize);
13134c7070dbSScott Long 		goto fail;
13144c7070dbSScott Long 	}
13154c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
13164c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
13174c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
13184c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
13194c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
13204c7070dbSScott Long 			       scctx->isc_tx_tso_size_max,		/* maxsize */
13214c7070dbSScott Long 			       ntsosegments,	/* nsegments */
13224c7070dbSScott Long 			       scctx->isc_tx_tso_segsize_max,	/* maxsegsize */
13234c7070dbSScott Long 			       0,			/* flags */
13244c7070dbSScott Long 			       NULL,			/* lockfunc */
13254c7070dbSScott Long 			       NULL,			/* lockfuncarg */
13264c7070dbSScott Long 			       &txq->ift_tso_desc_tag))) {
13274c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX TSO DMA tag: %d\n", err);
13284c7070dbSScott Long 
13294c7070dbSScott Long 		goto fail;
13304c7070dbSScott Long 	}
13314c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_flags =
13324c7070dbSScott Long 	    (uint8_t *) malloc(sizeof(uint8_t) *
133323ac9029SStephen Hurd 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
13344c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
13354c7070dbSScott Long 		err = ENOMEM;
13364c7070dbSScott Long 		goto fail;
13374c7070dbSScott Long 	}
13384c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_m =
13394c7070dbSScott Long 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
134023ac9029SStephen Hurd 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
13414c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
13424c7070dbSScott Long 		err = ENOMEM;
13434c7070dbSScott Long 		goto fail;
13444c7070dbSScott Long 	}
13454c7070dbSScott Long 
13464c7070dbSScott Long         /* Create the descriptor buffer dma maps */
13474c7070dbSScott Long #if defined(ACPI_DMAR) || (!(defined(__i386__) && !defined(__amd64__)))
13484c7070dbSScott Long 	if ((ctx->ifc_flags & IFC_DMAR) == 0)
13494c7070dbSScott Long 		return (0);
13504c7070dbSScott Long 
13514c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_map =
135223ac9029SStephen Hurd 	    (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
13534c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer map memory\n");
13544c7070dbSScott Long 		err = ENOMEM;
13554c7070dbSScott Long 		goto fail;
13564c7070dbSScott Long 	}
13574c7070dbSScott Long 
135823ac9029SStephen Hurd 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
13594c7070dbSScott Long 		err = bus_dmamap_create(txq->ift_desc_tag, 0, &txq->ift_sds.ifsd_map[i]);
13604c7070dbSScott Long 		if (err != 0) {
13614c7070dbSScott Long 			device_printf(dev, "Unable to create TX DMA map\n");
13624c7070dbSScott Long 			goto fail;
13634c7070dbSScott Long 		}
13644c7070dbSScott Long 	}
13654c7070dbSScott Long #endif
13664c7070dbSScott Long 	return (0);
13674c7070dbSScott Long fail:
13684c7070dbSScott Long 	/* We free all, it handles case where we are in the middle */
13694c7070dbSScott Long 	iflib_tx_structures_free(ctx);
13704c7070dbSScott Long 	return (err);
13714c7070dbSScott Long }
13724c7070dbSScott Long 
13734c7070dbSScott Long static void
13744c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
13754c7070dbSScott Long {
13764c7070dbSScott Long 	bus_dmamap_t map;
13774c7070dbSScott Long 
13784c7070dbSScott Long 	map = NULL;
13794c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL)
13804c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[i];
13814c7070dbSScott Long 	if (map != NULL) {
13824c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag, map);
13834c7070dbSScott Long 		bus_dmamap_destroy(txq->ift_desc_tag, map);
13844c7070dbSScott Long 		txq->ift_sds.ifsd_map[i] = NULL;
13854c7070dbSScott Long 	}
13864c7070dbSScott Long }
13874c7070dbSScott Long 
13884c7070dbSScott Long static void
13894c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq)
13904c7070dbSScott Long {
13914c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
13924c7070dbSScott Long 
139323ac9029SStephen Hurd 	for (int i = 0; i < txq->ift_size; i++)
13944c7070dbSScott Long 		iflib_txsd_destroy(ctx, txq, i);
13954c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
13964c7070dbSScott Long 		free(txq->ift_sds.ifsd_map, M_IFLIB);
13974c7070dbSScott Long 		txq->ift_sds.ifsd_map = NULL;
13984c7070dbSScott Long 	}
13994c7070dbSScott Long 	if (txq->ift_sds.ifsd_m != NULL) {
14004c7070dbSScott Long 		free(txq->ift_sds.ifsd_m, M_IFLIB);
14014c7070dbSScott Long 		txq->ift_sds.ifsd_m = NULL;
14024c7070dbSScott Long 	}
14034c7070dbSScott Long 	if (txq->ift_sds.ifsd_flags != NULL) {
14044c7070dbSScott Long 		free(txq->ift_sds.ifsd_flags, M_IFLIB);
14054c7070dbSScott Long 		txq->ift_sds.ifsd_flags = NULL;
14064c7070dbSScott Long 	}
14074c7070dbSScott Long 	if (txq->ift_desc_tag != NULL) {
14084c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_desc_tag);
14094c7070dbSScott Long 		txq->ift_desc_tag = NULL;
14104c7070dbSScott Long 	}
14114c7070dbSScott Long 	if (txq->ift_tso_desc_tag != NULL) {
14124c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_tso_desc_tag);
14134c7070dbSScott Long 		txq->ift_tso_desc_tag = NULL;
14144c7070dbSScott Long 	}
14154c7070dbSScott Long }
14164c7070dbSScott Long 
14174c7070dbSScott Long static void
14184c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
14194c7070dbSScott Long {
14204c7070dbSScott Long 	struct mbuf **mp;
14214c7070dbSScott Long 
14224c7070dbSScott Long 	mp = &txq->ift_sds.ifsd_m[i];
14234c7070dbSScott Long 	if (*mp == NULL)
14244c7070dbSScott Long 		return;
14254c7070dbSScott Long 
14264c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
14274c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag,
14284c7070dbSScott Long 				txq->ift_sds.ifsd_map[i],
14294c7070dbSScott Long 				BUS_DMASYNC_POSTWRITE);
14304c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag,
14314c7070dbSScott Long 				  txq->ift_sds.ifsd_map[i]);
14324c7070dbSScott Long 	}
143323ac9029SStephen Hurd 	m_free(*mp);
14344c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
14354c7070dbSScott Long 	*mp = NULL;
14364c7070dbSScott Long }
14374c7070dbSScott Long 
14384c7070dbSScott Long static int
14394c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq)
14404c7070dbSScott Long {
14414c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
144223ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
14434c7070dbSScott Long 	iflib_dma_info_t di;
14444c7070dbSScott Long 	int i;
14454c7070dbSScott Long 
14464c7070dbSScott Long 	/* Set number of descriptors available */
14474c7070dbSScott Long 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
14484c7070dbSScott Long 
14494c7070dbSScott Long 	/* Reset indices */
14504c7070dbSScott Long 	txq->ift_cidx_processed = txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
145123ac9029SStephen Hurd 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
14524c7070dbSScott Long 
14534c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
14544c7070dbSScott Long 		bzero((void *)di->idi_vaddr, di->idi_size);
14554c7070dbSScott Long 
14564c7070dbSScott Long 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
14574c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
14584c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
14594c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
14604c7070dbSScott Long 	return (0);
14614c7070dbSScott Long }
14624c7070dbSScott Long 
14634c7070dbSScott Long /*********************************************************************
14644c7070dbSScott Long  *
14654c7070dbSScott Long  *  Allocate memory for rx_buffer structures. Since we use one
14664c7070dbSScott Long  *  rx_buffer per received packet, the maximum number of rx_buffer's
14674c7070dbSScott Long  *  that we'll need is equal to the number of receive descriptors
14684c7070dbSScott Long  *  that we've allocated.
14694c7070dbSScott Long  *
14704c7070dbSScott Long  **********************************************************************/
14714c7070dbSScott Long static int
14724c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq)
14734c7070dbSScott Long {
14744c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
14754c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
147623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
14774c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
14784c7070dbSScott Long 	iflib_fl_t fl;
14794c7070dbSScott Long 	iflib_rxsd_t	rxsd;
14804c7070dbSScott Long 	int			err;
14814c7070dbSScott Long 
148223ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[0] > 0);
148323ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
14844c7070dbSScott Long 
14854c7070dbSScott Long 	fl = rxq->ifr_fl;
14864c7070dbSScott Long 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
14874c7070dbSScott Long 		fl->ifl_sds = malloc(sizeof(struct iflib_sw_rx_desc) *
148823ac9029SStephen Hurd 		    scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB,
148923ac9029SStephen Hurd 		    M_WAITOK | M_ZERO);
14904c7070dbSScott Long 		if (fl->ifl_sds == NULL) {
14914c7070dbSScott Long 			device_printf(dev, "Unable to allocate rx sw desc memory\n");
14924c7070dbSScott Long 			return (ENOMEM);
14934c7070dbSScott Long 		}
149423ac9029SStephen Hurd 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
14954c7070dbSScott Long 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
14964c7070dbSScott Long 					 1, 0,			/* alignment, bounds */
14974c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* lowaddr */
14984c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* highaddr */
14994c7070dbSScott Long 					 NULL, NULL,		/* filter, filterarg */
15004c7070dbSScott Long 					 sctx->isc_rx_maxsize,	/* maxsize */
15014c7070dbSScott Long 					 sctx->isc_rx_nsegments,	/* nsegments */
15024c7070dbSScott Long 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
15034c7070dbSScott Long 					 0,			/* flags */
15044c7070dbSScott Long 					 NULL,			/* lockfunc */
15054c7070dbSScott Long 					 NULL,			/* lockarg */
15064c7070dbSScott Long 					 &fl->ifl_desc_tag);
15074c7070dbSScott Long 		if (err) {
15084c7070dbSScott Long 			device_printf(dev, "%s: bus_dma_tag_create failed %d\n",
15094c7070dbSScott Long 				__func__, err);
15104c7070dbSScott Long 			goto fail;
15114c7070dbSScott Long 		}
15124c7070dbSScott Long 
15134c7070dbSScott Long 		rxsd = fl->ifl_sds;
151423ac9029SStephen Hurd 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++, rxsd++) {
15154c7070dbSScott Long 			err = bus_dmamap_create(fl->ifl_desc_tag, 0, &rxsd->ifsd_map);
15164c7070dbSScott Long 			if (err) {
15174c7070dbSScott Long 				device_printf(dev, "%s: bus_dmamap_create failed: %d\n",
15184c7070dbSScott Long 					__func__, err);
15194c7070dbSScott Long 				goto fail;
15204c7070dbSScott Long 			}
15214c7070dbSScott Long 		}
15224c7070dbSScott Long 	}
15234c7070dbSScott Long 	return (0);
15244c7070dbSScott Long 
15254c7070dbSScott Long fail:
15264c7070dbSScott Long 	iflib_rx_structures_free(ctx);
15274c7070dbSScott Long 	return (err);
15284c7070dbSScott Long }
15294c7070dbSScott Long 
15304c7070dbSScott Long 
15314c7070dbSScott Long /*
15324c7070dbSScott Long  * Internal service routines
15334c7070dbSScott Long  */
15344c7070dbSScott Long 
15354c7070dbSScott Long struct rxq_refill_cb_arg {
15364c7070dbSScott Long 	int               error;
15374c7070dbSScott Long 	bus_dma_segment_t seg;
15384c7070dbSScott Long 	int               nseg;
15394c7070dbSScott Long };
15404c7070dbSScott Long 
15414c7070dbSScott Long static void
15424c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
15434c7070dbSScott Long {
15444c7070dbSScott Long 	struct rxq_refill_cb_arg *cb_arg = arg;
15454c7070dbSScott Long 
15464c7070dbSScott Long 	cb_arg->error = error;
15474c7070dbSScott Long 	cb_arg->seg = segs[0];
15484c7070dbSScott Long 	cb_arg->nseg = nseg;
15494c7070dbSScott Long }
15504c7070dbSScott Long 
15514c7070dbSScott Long 
15524c7070dbSScott Long #ifdef ACPI_DMAR
15534c7070dbSScott Long #define IS_DMAR(ctx) (ctx->ifc_flags & IFC_DMAR)
15544c7070dbSScott Long #else
15554c7070dbSScott Long #define IS_DMAR(ctx) (0)
15564c7070dbSScott Long #endif
15574c7070dbSScott Long 
15584c7070dbSScott Long /**
15594c7070dbSScott Long  *	rxq_refill - refill an rxq  free-buffer list
15604c7070dbSScott Long  *	@ctx: the iflib context
15614c7070dbSScott Long  *	@rxq: the free-list to refill
15624c7070dbSScott Long  *	@n: the number of new buffers to allocate
15634c7070dbSScott Long  *
15644c7070dbSScott Long  *	(Re)populate an rxq free-buffer list with up to @n new packet buffers.
15654c7070dbSScott Long  *	The caller must assure that @n does not exceed the queue's capacity.
15664c7070dbSScott Long  */
15674c7070dbSScott Long static void
15684c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
15694c7070dbSScott Long {
15704c7070dbSScott Long 	struct mbuf *m;
15714c7070dbSScott Long 	int pidx = fl->ifl_pidx;
15724c7070dbSScott Long 	iflib_rxsd_t rxsd = &fl->ifl_sds[pidx];
15734c7070dbSScott Long 	caddr_t cl;
15744c7070dbSScott Long 	int n, i = 0;
15754c7070dbSScott Long 	uint64_t bus_addr;
15764c7070dbSScott Long 	int err;
15774c7070dbSScott Long 
15784c7070dbSScott Long 	n  = count;
15794c7070dbSScott Long 	MPASS(n > 0);
15804c7070dbSScott Long 	MPASS(fl->ifl_credits + n <= fl->ifl_size);
15814c7070dbSScott Long 
15824c7070dbSScott Long 	if (pidx < fl->ifl_cidx)
15834c7070dbSScott Long 		MPASS(pidx + n <= fl->ifl_cidx);
15844c7070dbSScott Long 	if (pidx == fl->ifl_cidx && (fl->ifl_credits < fl->ifl_size))
15854c7070dbSScott Long 		MPASS(fl->ifl_gen == 0);
15864c7070dbSScott Long 	if (pidx > fl->ifl_cidx)
15874c7070dbSScott Long 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
15884c7070dbSScott Long 
15894c7070dbSScott Long 	DBG_COUNTER_INC(fl_refills);
15904c7070dbSScott Long 	if (n > 8)
15914c7070dbSScott Long 		DBG_COUNTER_INC(fl_refills_large);
15924c7070dbSScott Long 
15934c7070dbSScott Long 	while (n--) {
15944c7070dbSScott Long 		/*
15954c7070dbSScott Long 		 * We allocate an uninitialized mbuf + cluster, mbuf is
15964c7070dbSScott Long 		 * initialized after rx.
15974c7070dbSScott Long 		 *
15984c7070dbSScott Long 		 * If the cluster is still set then we know a minimum sized packet was received
15994c7070dbSScott Long 		 */
16004c7070dbSScott Long 		if ((cl = rxsd->ifsd_cl) == NULL) {
16014c7070dbSScott Long 			if ((cl = rxsd->ifsd_cl = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
16024c7070dbSScott Long 				break;
16034c7070dbSScott Long #if MEMORY_LOGGING
16044c7070dbSScott Long 			fl->ifl_cl_enqueued++;
16054c7070dbSScott Long #endif
16064c7070dbSScott Long 		}
16074c7070dbSScott Long 		if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
16084c7070dbSScott Long 			break;
16094c7070dbSScott Long 		}
16104c7070dbSScott Long #if MEMORY_LOGGING
16114c7070dbSScott Long 		fl->ifl_m_enqueued++;
16124c7070dbSScott Long #endif
16134c7070dbSScott Long 
16144c7070dbSScott Long 		DBG_COUNTER_INC(rx_allocs);
16154c7070dbSScott Long #ifdef notyet
16164c7070dbSScott Long 		if ((rxsd->ifsd_flags & RX_SW_DESC_MAP_CREATED) == 0) {
16174c7070dbSScott Long 			int err;
16184c7070dbSScott Long 
16194c7070dbSScott Long 			if ((err = bus_dmamap_create(fl->ifl_ifdi->idi_tag, 0, &rxsd->ifsd_map))) {
16204c7070dbSScott Long 				log(LOG_WARNING, "bus_dmamap_create failed %d\n", err);
16214c7070dbSScott Long 				uma_zfree(fl->ifl_zone, cl);
16224c7070dbSScott Long 				n = 0;
16234c7070dbSScott Long 				goto done;
16244c7070dbSScott Long 			}
16254c7070dbSScott Long 			rxsd->ifsd_flags |= RX_SW_DESC_MAP_CREATED;
16264c7070dbSScott Long 		}
16274c7070dbSScott Long #endif
16284c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
16294c7070dbSScott Long 		if (!IS_DMAR(ctx)) {
16304c7070dbSScott Long 			bus_addr = pmap_kextract((vm_offset_t)cl);
16314c7070dbSScott Long 		} else
16324c7070dbSScott Long #endif
16334c7070dbSScott Long 		{
16344c7070dbSScott Long 			struct rxq_refill_cb_arg cb_arg;
16354c7070dbSScott Long 			iflib_rxq_t q;
16364c7070dbSScott Long 
16374c7070dbSScott Long 			cb_arg.error = 0;
16384c7070dbSScott Long 			q = fl->ifl_rxq;
16394c7070dbSScott Long 			err = bus_dmamap_load(fl->ifl_desc_tag, rxsd->ifsd_map,
16404c7070dbSScott Long 		         cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 0);
16414c7070dbSScott Long 
16424c7070dbSScott Long 			if (err != 0 || cb_arg.error) {
16434c7070dbSScott Long 				/*
16444c7070dbSScott Long 				 * !zone_pack ?
16454c7070dbSScott Long 				 */
16464c7070dbSScott Long 				if (fl->ifl_zone == zone_pack)
16474c7070dbSScott Long 					uma_zfree(fl->ifl_zone, cl);
16484c7070dbSScott Long 				m_free(m);
16494c7070dbSScott Long 				n = 0;
16504c7070dbSScott Long 				goto done;
16514c7070dbSScott Long 			}
16524c7070dbSScott Long 			bus_addr = cb_arg.seg.ds_addr;
16534c7070dbSScott Long 		}
16544c7070dbSScott Long 		rxsd->ifsd_flags |= RX_SW_DESC_INUSE;
16554c7070dbSScott Long 
16564c7070dbSScott Long 		MPASS(rxsd->ifsd_m == NULL);
16574c7070dbSScott Long 		rxsd->ifsd_cl = cl;
16584c7070dbSScott Long 		rxsd->ifsd_m = m;
16594c7070dbSScott Long 		fl->ifl_bus_addrs[i] = bus_addr;
16604c7070dbSScott Long 		fl->ifl_vm_addrs[i] = cl;
16614c7070dbSScott Long 		rxsd++;
16624c7070dbSScott Long 		fl->ifl_credits++;
16634c7070dbSScott Long 		i++;
16644c7070dbSScott Long 		MPASS(fl->ifl_credits <= fl->ifl_size);
16654c7070dbSScott Long 		if (++fl->ifl_pidx == fl->ifl_size) {
16664c7070dbSScott Long 			fl->ifl_pidx = 0;
16674c7070dbSScott Long 			fl->ifl_gen = 1;
16684c7070dbSScott Long 			rxsd = fl->ifl_sds;
16694c7070dbSScott Long 		}
16704c7070dbSScott Long 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
16714c7070dbSScott Long 			ctx->isc_rxd_refill(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx,
167223ac9029SStephen Hurd 								 fl->ifl_bus_addrs, fl->ifl_vm_addrs, i, fl->ifl_buf_size);
16734c7070dbSScott Long 			i = 0;
16744c7070dbSScott Long 			pidx = fl->ifl_pidx;
16754c7070dbSScott Long 		}
16764c7070dbSScott Long 	}
16774c7070dbSScott Long done:
16784c7070dbSScott Long 	DBG_COUNTER_INC(rxd_flush);
16794c7070dbSScott Long 	if (fl->ifl_pidx == 0)
16804c7070dbSScott Long 		pidx = fl->ifl_size - 1;
16814c7070dbSScott Long 	else
16824c7070dbSScott Long 		pidx = fl->ifl_pidx - 1;
16834c7070dbSScott Long 	ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
16844c7070dbSScott Long }
16854c7070dbSScott Long 
16864c7070dbSScott Long static __inline void
16874c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max)
16884c7070dbSScott Long {
16894c7070dbSScott Long 	/* we avoid allowing pidx to catch up with cidx as it confuses ixl */
16904c7070dbSScott Long 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
16914c7070dbSScott Long #ifdef INVARIANTS
16924c7070dbSScott Long 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
16934c7070dbSScott Long #endif
16944c7070dbSScott Long 
16954c7070dbSScott Long 	MPASS(fl->ifl_credits <= fl->ifl_size);
16964c7070dbSScott Long 	MPASS(reclaimable == delta);
16974c7070dbSScott Long 
16984c7070dbSScott Long 	if (reclaimable > 0)
16994c7070dbSScott Long 		_iflib_fl_refill(ctx, fl, min(max, reclaimable));
17004c7070dbSScott Long }
17014c7070dbSScott Long 
17024c7070dbSScott Long static void
17034c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl)
17044c7070dbSScott Long {
17054c7070dbSScott Long 	iflib_dma_info_t idi = fl->ifl_ifdi;
17064c7070dbSScott Long 	uint32_t i;
17074c7070dbSScott Long 
17084c7070dbSScott Long 	for (i = 0; i < fl->ifl_size; i++) {
17094c7070dbSScott Long 		iflib_rxsd_t d = &fl->ifl_sds[i];
17104c7070dbSScott Long 
17114c7070dbSScott Long 		if (d->ifsd_flags & RX_SW_DESC_INUSE) {
17124c7070dbSScott Long 			bus_dmamap_unload(fl->ifl_desc_tag, d->ifsd_map);
17134c7070dbSScott Long 			bus_dmamap_destroy(fl->ifl_desc_tag, d->ifsd_map);
17144c7070dbSScott Long 			if (d->ifsd_m != NULL) {
17154c7070dbSScott Long 				m_init(d->ifsd_m, M_NOWAIT, MT_DATA, 0);
17164c7070dbSScott Long 				uma_zfree(zone_mbuf, d->ifsd_m);
17174c7070dbSScott Long 			}
17184c7070dbSScott Long 			if (d->ifsd_cl != NULL)
17194c7070dbSScott Long 				uma_zfree(fl->ifl_zone, d->ifsd_cl);
17204c7070dbSScott Long 			d->ifsd_flags = 0;
17214c7070dbSScott Long 		} else {
17224c7070dbSScott Long 			MPASS(d->ifsd_cl == NULL);
17234c7070dbSScott Long 			MPASS(d->ifsd_m == NULL);
17244c7070dbSScott Long 		}
17254c7070dbSScott Long #if MEMORY_LOGGING
17264c7070dbSScott Long 		fl->ifl_m_dequeued++;
17274c7070dbSScott Long 		fl->ifl_cl_dequeued++;
17284c7070dbSScott Long #endif
17294c7070dbSScott Long 		d->ifsd_cl = NULL;
17304c7070dbSScott Long 		d->ifsd_m = NULL;
17314c7070dbSScott Long 	}
17324c7070dbSScott Long 	/*
17334c7070dbSScott Long 	 * Reset free list values
17344c7070dbSScott Long 	 */
17354c7070dbSScott Long 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = 0;;
17364c7070dbSScott Long 	bzero(idi->idi_vaddr, idi->idi_size);
17374c7070dbSScott Long }
17384c7070dbSScott Long 
17394c7070dbSScott Long /*********************************************************************
17404c7070dbSScott Long  *
17414c7070dbSScott Long  *  Initialize a receive ring and its buffers.
17424c7070dbSScott Long  *
17434c7070dbSScott Long  **********************************************************************/
17444c7070dbSScott Long static int
17454c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl)
17464c7070dbSScott Long {
17474c7070dbSScott Long 	iflib_rxq_t rxq = fl->ifl_rxq;
17484c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
17494c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
17504c7070dbSScott Long 
17514c7070dbSScott Long 	/*
17524c7070dbSScott Long 	** Free current RX buffer structs and their mbufs
17534c7070dbSScott Long 	*/
17544c7070dbSScott Long 	iflib_fl_bufs_free(fl);
17554c7070dbSScott Long 	/* Now replenish the mbufs */
17564c7070dbSScott Long 	MPASS(fl->ifl_credits == 0);
17574c7070dbSScott Long 	/*
17584c7070dbSScott Long 	 * XXX don't set the max_frame_size to larger
17594c7070dbSScott Long 	 * than the hardware can handle
17604c7070dbSScott Long 	 */
17614c7070dbSScott Long 	if (sctx->isc_max_frame_size <= 2048)
17624c7070dbSScott Long 		fl->ifl_buf_size = MCLBYTES;
17634c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 4096)
17644c7070dbSScott Long 		fl->ifl_buf_size = MJUMPAGESIZE;
17654c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 9216)
17664c7070dbSScott Long 		fl->ifl_buf_size = MJUM9BYTES;
17674c7070dbSScott Long 	else
17684c7070dbSScott Long 		fl->ifl_buf_size = MJUM16BYTES;
17694c7070dbSScott Long 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
17704c7070dbSScott Long 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
17714c7070dbSScott Long 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
17724c7070dbSScott Long 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
17734c7070dbSScott Long 
17744c7070dbSScott Long 
17754c7070dbSScott Long 	/* avoid pre-allocating zillions of clusters to an idle card
17764c7070dbSScott Long 	 * potentially speeding up attach
17774c7070dbSScott Long 	 */
17784c7070dbSScott Long 	_iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
17794c7070dbSScott Long 	MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
17804c7070dbSScott Long 	if (min(128, fl->ifl_size) != fl->ifl_credits)
17814c7070dbSScott Long 		return (ENOBUFS);
17824c7070dbSScott Long 	/*
17834c7070dbSScott Long 	 * handle failure
17844c7070dbSScott Long 	 */
17854c7070dbSScott Long 	MPASS(rxq != NULL);
17864c7070dbSScott Long 	MPASS(fl->ifl_ifdi != NULL);
17874c7070dbSScott Long 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
17884c7070dbSScott Long 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17894c7070dbSScott Long 	return (0);
17904c7070dbSScott Long }
17914c7070dbSScott Long 
17924c7070dbSScott Long /*********************************************************************
17934c7070dbSScott Long  *
17944c7070dbSScott Long  *  Free receive ring data structures
17954c7070dbSScott Long  *
17964c7070dbSScott Long  **********************************************************************/
17974c7070dbSScott Long static void
17984c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq)
17994c7070dbSScott Long {
18004c7070dbSScott Long 	iflib_fl_t fl;
18014c7070dbSScott Long 	int i;
18024c7070dbSScott Long 
18034c7070dbSScott Long 	if (rxq->ifr_fl != NULL) {
18044c7070dbSScott Long 		for (i = 0; i < rxq->ifr_nfl; i++) {
18054c7070dbSScott Long 			fl = &rxq->ifr_fl[i];
18064c7070dbSScott Long 			if (fl->ifl_desc_tag != NULL) {
18074c7070dbSScott Long 				bus_dma_tag_destroy(fl->ifl_desc_tag);
18084c7070dbSScott Long 				fl->ifl_desc_tag = NULL;
18094c7070dbSScott Long 			}
18104c7070dbSScott Long 		}
18114c7070dbSScott Long 		if (rxq->ifr_fl->ifl_sds != NULL)
18124c7070dbSScott Long 			free(rxq->ifr_fl->ifl_sds, M_IFLIB);
18134c7070dbSScott Long 
18144c7070dbSScott Long 		free(rxq->ifr_fl, M_IFLIB);
18154c7070dbSScott Long 		rxq->ifr_fl = NULL;
18164c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
18174c7070dbSScott Long 	}
18184c7070dbSScott Long }
18194c7070dbSScott Long 
18204c7070dbSScott Long /*
18214c7070dbSScott Long  * MI independent logic
18224c7070dbSScott Long  *
18234c7070dbSScott Long  */
18244c7070dbSScott Long static void
18254c7070dbSScott Long iflib_timer(void *arg)
18264c7070dbSScott Long {
18274c7070dbSScott Long 	iflib_txq_t txq = arg;
18284c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
18294c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
18304c7070dbSScott Long 
18314c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
18324c7070dbSScott Long 		return;
18334c7070dbSScott Long 	/*
18344c7070dbSScott Long 	** Check on the state of the TX queue(s), this
18354c7070dbSScott Long 	** can be done without the lock because its RO
18364c7070dbSScott Long 	** and the HUNG state will be static if set.
18374c7070dbSScott Long 	*/
18384c7070dbSScott Long 	IFDI_TIMER(ctx, txq->ift_id);
18394c7070dbSScott Long 	if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
18404c7070dbSScott Long 		(ctx->ifc_pause_frames == 0))
18414c7070dbSScott Long 		goto hung;
18424c7070dbSScott Long 
18434c7070dbSScott Long 	if (TXQ_AVAIL(txq) <= 2*scctx->isc_tx_nsegments ||
18444c7070dbSScott Long 	    ifmp_ring_is_stalled(txq->ift_br[0]))
18454c7070dbSScott Long 		GROUPTASK_ENQUEUE(&txq->ift_task);
18464c7070dbSScott Long 
18474c7070dbSScott Long 	ctx->ifc_pause_frames = 0;
18484c7070dbSScott Long 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
18494c7070dbSScott Long 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
18504c7070dbSScott Long 	return;
18514c7070dbSScott Long hung:
18524c7070dbSScott Long 	CTX_LOCK(ctx);
18534c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, 0, IFF_DRV_RUNNING);
18544c7070dbSScott Long 	device_printf(ctx->ifc_dev,  "TX(%d) desc avail = %d, pidx = %d\n",
18554c7070dbSScott Long 				  txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
18564c7070dbSScott Long 
18574c7070dbSScott Long 	IFDI_WATCHDOG_RESET(ctx);
18584c7070dbSScott Long 	ctx->ifc_watchdog_events++;
18594c7070dbSScott Long 	ctx->ifc_pause_frames = 0;
18604c7070dbSScott Long 
18614c7070dbSScott Long 	iflib_init_locked(ctx);
18624c7070dbSScott Long 	CTX_UNLOCK(ctx);
18634c7070dbSScott Long }
18644c7070dbSScott Long 
18654c7070dbSScott Long static void
18664c7070dbSScott Long iflib_init_locked(if_ctx_t ctx)
18674c7070dbSScott Long {
18684c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
18691248952aSSean Bruno 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
18704c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
18714c7070dbSScott Long 	iflib_fl_t fl;
18724c7070dbSScott Long 	iflib_txq_t txq;
18734c7070dbSScott Long 	iflib_rxq_t rxq;
18741248952aSSean Bruno 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
18754c7070dbSScott Long 
18764c7070dbSScott Long 
18774c7070dbSScott Long 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
18784c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
18794c7070dbSScott Long 
18801248952aSSean Bruno 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
18811248952aSSean Bruno 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
18824c7070dbSScott Long 	/* Set hardware offload abilities */
18834c7070dbSScott Long 	if_clearhwassist(ifp);
18844c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
18851248952aSSean Bruno 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
18864c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
18871248952aSSean Bruno 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
18884c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO4)
18894c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
18904c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO6)
18914c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
18924c7070dbSScott Long 
18934c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
18944c7070dbSScott Long 		CALLOUT_LOCK(txq);
18954c7070dbSScott Long 		callout_stop(&txq->ift_timer);
18964c7070dbSScott Long 		callout_stop(&txq->ift_db_check);
18974c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
18984c7070dbSScott Long 		iflib_netmap_txq_init(ctx, txq);
18994c7070dbSScott Long 	}
19004c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
19014c7070dbSScott Long 		iflib_netmap_rxq_init(ctx, rxq);
19024c7070dbSScott Long 	}
190323ac9029SStephen Hurd #ifdef INVARIANTS
190423ac9029SStephen Hurd 	i = if_getdrvflags(ifp);
190523ac9029SStephen Hurd #endif
19064c7070dbSScott Long 	IFDI_INIT(ctx);
190723ac9029SStephen Hurd 	MPASS(if_getdrvflags(ifp) == i);
19084c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
19094c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
19104c7070dbSScott Long 			if (iflib_fl_setup(fl)) {
19114c7070dbSScott Long 				device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n");
19124c7070dbSScott Long 				goto done;
19134c7070dbSScott Long 			}
19144c7070dbSScott Long 		}
19154c7070dbSScott Long 	}
19164c7070dbSScott Long 	done:
19174c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
19184c7070dbSScott Long 	IFDI_INTR_ENABLE(ctx);
19194c7070dbSScott Long 	txq = ctx->ifc_txqs;
19204c7070dbSScott Long 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
19214c7070dbSScott Long 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
19224c7070dbSScott Long 			txq->ift_timer.c_cpu);
19234c7070dbSScott Long }
19244c7070dbSScott Long 
19254c7070dbSScott Long static int
19264c7070dbSScott Long iflib_media_change(if_t ifp)
19274c7070dbSScott Long {
19284c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
19294c7070dbSScott Long 	int err;
19304c7070dbSScott Long 
19314c7070dbSScott Long 	CTX_LOCK(ctx);
19324c7070dbSScott Long 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
19334c7070dbSScott Long 		iflib_init_locked(ctx);
19344c7070dbSScott Long 	CTX_UNLOCK(ctx);
19354c7070dbSScott Long 	return (err);
19364c7070dbSScott Long }
19374c7070dbSScott Long 
19384c7070dbSScott Long static void
19394c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
19404c7070dbSScott Long {
19414c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
19424c7070dbSScott Long 
19434c7070dbSScott Long 	CTX_LOCK(ctx);
19444c7070dbSScott Long 	IFDI_UPDATE_ADMIN_STATUS(ctx);
19454c7070dbSScott Long 	IFDI_MEDIA_STATUS(ctx, ifmr);
19464c7070dbSScott Long 	CTX_UNLOCK(ctx);
19474c7070dbSScott Long }
19484c7070dbSScott Long 
19494c7070dbSScott Long static void
19504c7070dbSScott Long iflib_stop(if_ctx_t ctx)
19514c7070dbSScott Long {
19524c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
19534c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
19544c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
19554c7070dbSScott Long 	iflib_dma_info_t di;
19564c7070dbSScott Long 	iflib_fl_t fl;
19574c7070dbSScott Long 	int i, j;
19584c7070dbSScott Long 
19594c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
19604c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
19614c7070dbSScott Long 
19624c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
1963da69b8f9SSean Bruno 	DELAY(100000);
1964da69b8f9SSean Bruno 	IFDI_STOP(ctx);
1965da69b8f9SSean Bruno 	DELAY(100000);
19664c7070dbSScott Long 
1967da69b8f9SSean Bruno 	iflib_debug_reset();
19684c7070dbSScott Long 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
19694c7070dbSScott Long 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
19704c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
19714c7070dbSScott Long 
19724c7070dbSScott Long 		/* clean any enqueued buffers */
1973da69b8f9SSean Bruno 		iflib_ifmp_purge(txq);
19744c7070dbSScott Long 		/* Free any existing tx buffers. */
197523ac9029SStephen Hurd 		for (j = 0; j < txq->ift_size; j++) {
19764c7070dbSScott Long 			iflib_txsd_free(ctx, txq, j);
19774c7070dbSScott Long 		}
19784c7070dbSScott Long 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
1979da69b8f9SSean Bruno 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
19804c7070dbSScott Long 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
19814c7070dbSScott Long 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
19824c7070dbSScott Long 		txq->ift_pullups = 0;
19834c7070dbSScott Long 		ifmp_ring_reset_stats(txq->ift_br[0]);
19844c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwtxqs; j++, di++)
19854c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
19864c7070dbSScott Long 	}
19874c7070dbSScott Long 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
19884c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
19894c7070dbSScott Long 
19904c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwrxqs; j++, di++)
19914c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
19924c7070dbSScott Long 		/* also resets the free lists pidx/cidx */
19934c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
19944c7070dbSScott Long 			iflib_fl_bufs_free(fl);
19954c7070dbSScott Long 	}
19964c7070dbSScott Long }
19974c7070dbSScott Long 
19984c7070dbSScott Long static iflib_rxsd_t
19994c7070dbSScott Long rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int *cltype, int unload)
20004c7070dbSScott Long {
20014c7070dbSScott Long 	int flid, cidx;
20024c7070dbSScott Long 	iflib_rxsd_t sd;
20034c7070dbSScott Long 	iflib_fl_t fl;
20044c7070dbSScott Long 	iflib_dma_info_t di;
20054c7070dbSScott Long 
20064c7070dbSScott Long 	flid = irf->irf_flid;
20074c7070dbSScott Long 	cidx = irf->irf_idx;
20084c7070dbSScott Long 	fl = &rxq->ifr_fl[flid];
20094c7070dbSScott Long 	fl->ifl_credits--;
20104c7070dbSScott Long #if MEMORY_LOGGING
20114c7070dbSScott Long 	fl->ifl_m_dequeued++;
20124c7070dbSScott Long 	if (cltype)
20134c7070dbSScott Long 		fl->ifl_cl_dequeued++;
20144c7070dbSScott Long #endif
20154c7070dbSScott Long 	sd = &fl->ifl_sds[cidx];
20164c7070dbSScott Long 	di = fl->ifl_ifdi;
20174c7070dbSScott Long 	bus_dmamap_sync(di->idi_tag, di->idi_map,
20184c7070dbSScott Long 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
20194c7070dbSScott Long 
20204c7070dbSScott Long 	/* not valid assert if bxe really does SGE from non-contiguous elements */
20214c7070dbSScott Long 	MPASS(fl->ifl_cidx == cidx);
20224c7070dbSScott Long 	if (unload)
20234c7070dbSScott Long 		bus_dmamap_unload(fl->ifl_desc_tag, sd->ifsd_map);
20244c7070dbSScott Long 
20254c7070dbSScott Long 	if (__predict_false(++fl->ifl_cidx == fl->ifl_size)) {
20264c7070dbSScott Long 		fl->ifl_cidx = 0;
20274c7070dbSScott Long 		fl->ifl_gen = 0;
20284c7070dbSScott Long 	}
20294c7070dbSScott Long 	/* YES ick */
20304c7070dbSScott Long 	if (cltype)
20314c7070dbSScott Long 		*cltype = fl->ifl_cltype;
20324c7070dbSScott Long 	return (sd);
20334c7070dbSScott Long }
20344c7070dbSScott Long 
20354c7070dbSScott Long static struct mbuf *
20364c7070dbSScott Long assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri)
20374c7070dbSScott Long {
20384c7070dbSScott Long 	int i, padlen , flags, cltype;
20394c7070dbSScott Long 	struct mbuf *m, *mh, *mt;
20404c7070dbSScott Long 	iflib_rxsd_t sd;
20414c7070dbSScott Long 	caddr_t cl;
20424c7070dbSScott Long 
20434c7070dbSScott Long 	i = 0;
204423ac9029SStephen Hurd 	mh = NULL;
20454c7070dbSScott Long 	do {
20464c7070dbSScott Long 		sd = rxd_frag_to_sd(rxq, &ri->iri_frags[i], &cltype, TRUE);
20474c7070dbSScott Long 
20484c7070dbSScott Long 		MPASS(sd->ifsd_cl != NULL);
20494c7070dbSScott Long 		MPASS(sd->ifsd_m != NULL);
205023ac9029SStephen Hurd 
205123ac9029SStephen Hurd 		/* Don't include zero-length frags */
205223ac9029SStephen Hurd 		if (ri->iri_frags[i].irf_len == 0) {
205323ac9029SStephen Hurd 			/* XXX we can save the cluster here, but not the mbuf */
205423ac9029SStephen Hurd 			m_init(sd->ifsd_m, M_NOWAIT, MT_DATA, 0);
205523ac9029SStephen Hurd 			m_free(sd->ifsd_m);
205623ac9029SStephen Hurd 			sd->ifsd_m = NULL;
205723ac9029SStephen Hurd 			continue;
205823ac9029SStephen Hurd 		}
205923ac9029SStephen Hurd 
20604c7070dbSScott Long 		m = sd->ifsd_m;
206123ac9029SStephen Hurd 		if (mh == NULL) {
20624c7070dbSScott Long 			flags = M_PKTHDR|M_EXT;
20634c7070dbSScott Long 			mh = mt = m;
20644c7070dbSScott Long 			padlen = ri->iri_pad;
20654c7070dbSScott Long 		} else {
20664c7070dbSScott Long 			flags = M_EXT;
20674c7070dbSScott Long 			mt->m_next = m;
20684c7070dbSScott Long 			mt = m;
20694c7070dbSScott Long 			/* assuming padding is only on the first fragment */
20704c7070dbSScott Long 			padlen = 0;
20714c7070dbSScott Long 		}
20724c7070dbSScott Long 		sd->ifsd_m = NULL;
20734c7070dbSScott Long 		cl = sd->ifsd_cl;
20744c7070dbSScott Long 		sd->ifsd_cl = NULL;
20754c7070dbSScott Long 
20764c7070dbSScott Long 		/* Can these two be made one ? */
20774c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, flags);
20784c7070dbSScott Long 		m_cljset(m, cl, cltype);
20794c7070dbSScott Long 		/*
20804c7070dbSScott Long 		 * These must follow m_init and m_cljset
20814c7070dbSScott Long 		 */
20824c7070dbSScott Long 		m->m_data += padlen;
20834c7070dbSScott Long 		ri->iri_len -= padlen;
208423ac9029SStephen Hurd 		m->m_len = ri->iri_frags[i].irf_len;
20854c7070dbSScott Long 	} while (++i < ri->iri_nfrags);
20864c7070dbSScott Long 
20874c7070dbSScott Long 	return (mh);
20884c7070dbSScott Long }
20894c7070dbSScott Long 
20904c7070dbSScott Long /*
20914c7070dbSScott Long  * Process one software descriptor
20924c7070dbSScott Long  */
20934c7070dbSScott Long static struct mbuf *
20944c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
20954c7070dbSScott Long {
20964c7070dbSScott Long 	struct mbuf *m;
20974c7070dbSScott Long 	iflib_rxsd_t sd;
20984c7070dbSScott Long 
20994c7070dbSScott Long 	/* should I merge this back in now that the two paths are basically duplicated? */
210023ac9029SStephen Hurd 	if (ri->iri_nfrags == 1 &&
210123ac9029SStephen Hurd 	    ri->iri_frags[0].irf_len <= IFLIB_RX_COPY_THRESH) {
21024c7070dbSScott Long 		sd = rxd_frag_to_sd(rxq, &ri->iri_frags[0], NULL, FALSE);
21034c7070dbSScott Long 		m = sd->ifsd_m;
21044c7070dbSScott Long 		sd->ifsd_m = NULL;
21054c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
21064c7070dbSScott Long 		memcpy(m->m_data, sd->ifsd_cl, ri->iri_len);
210723ac9029SStephen Hurd 		m->m_len = ri->iri_frags[0].irf_len;
21084c7070dbSScott Long        } else {
21094c7070dbSScott Long 		m = assemble_segments(rxq, ri);
21104c7070dbSScott Long 	}
21114c7070dbSScott Long 	m->m_pkthdr.len = ri->iri_len;
21124c7070dbSScott Long 	m->m_pkthdr.rcvif = ri->iri_ifp;
21134c7070dbSScott Long 	m->m_flags |= ri->iri_flags;
21144c7070dbSScott Long 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
21154c7070dbSScott Long 	m->m_pkthdr.flowid = ri->iri_flowid;
21164c7070dbSScott Long 	M_HASHTYPE_SET(m, ri->iri_rsstype);
21174c7070dbSScott Long 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
21184c7070dbSScott Long 	m->m_pkthdr.csum_data = ri->iri_csum_data;
21194c7070dbSScott Long 	return (m);
21204c7070dbSScott Long }
21214c7070dbSScott Long 
21224c7070dbSScott Long static bool
21234c7070dbSScott Long iflib_rxeof(iflib_rxq_t rxq, int budget)
21244c7070dbSScott Long {
21254c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
21264c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
212723ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
21284c7070dbSScott Long 	int avail, i;
21294c7070dbSScott Long 	uint16_t *cidxp;
21304c7070dbSScott Long 	struct if_rxd_info ri;
21314c7070dbSScott Long 	int err, budget_left, rx_bytes, rx_pkts;
21324c7070dbSScott Long 	iflib_fl_t fl;
21334c7070dbSScott Long 	struct ifnet *ifp;
21344c7070dbSScott Long 	int lro_enabled;
21354c7070dbSScott Long 	/*
21364c7070dbSScott Long 	 * XXX early demux data packets so that if_input processing only handles
21374c7070dbSScott Long 	 * acks in interrupt context
21384c7070dbSScott Long 	 */
21394c7070dbSScott Long 	struct mbuf *m, *mh, *mt;
21404c7070dbSScott Long 
21414c7070dbSScott Long 	if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &budget)) {
21424c7070dbSScott Long 		return (FALSE);
21434c7070dbSScott Long 	}
21444c7070dbSScott Long 
21454c7070dbSScott Long 	mh = mt = NULL;
21464c7070dbSScott Long 	MPASS(budget > 0);
21474c7070dbSScott Long 	rx_pkts	= rx_bytes = 0;
214823ac9029SStephen Hurd 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
21494c7070dbSScott Long 		cidxp = &rxq->ifr_cq_cidx;
21504c7070dbSScott Long 	else
21514c7070dbSScott Long 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
215223ac9029SStephen Hurd 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
21534c7070dbSScott Long 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
21544c7070dbSScott Long 			__iflib_fl_refill_lt(ctx, fl, budget + 8);
21554c7070dbSScott Long 		DBG_COUNTER_INC(rx_unavail);
21564c7070dbSScott Long 		return (false);
21574c7070dbSScott Long 	}
21584c7070dbSScott Long 
21594c7070dbSScott Long 	for (budget_left = budget; (budget_left > 0) && (avail > 0); budget_left--, avail--) {
21604c7070dbSScott Long 		if (__predict_false(!CTX_ACTIVE(ctx))) {
21614c7070dbSScott Long 			DBG_COUNTER_INC(rx_ctx_inactive);
21624c7070dbSScott Long 			break;
21634c7070dbSScott Long 		}
21644c7070dbSScott Long 		/*
21654c7070dbSScott Long 		 * Reset client set fields to their default values
21664c7070dbSScott Long 		 */
21674c7070dbSScott Long 		bzero(&ri, sizeof(ri));
21684c7070dbSScott Long 		ri.iri_qsidx = rxq->ifr_id;
21694c7070dbSScott Long 		ri.iri_cidx = *cidxp;
21704c7070dbSScott Long 		ri.iri_ifp = ctx->ifc_ifp;
21714c7070dbSScott Long 		ri.iri_frags = rxq->ifr_frags;
21724c7070dbSScott Long 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
21734c7070dbSScott Long 
21744c7070dbSScott Long 		/* in lieu of handling correctly - make sure it isn't being unhandled */
21754c7070dbSScott Long 		MPASS(err == 0);
217623ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
217723ac9029SStephen Hurd 			*cidxp = ri.iri_cidx;
217823ac9029SStephen Hurd 			/* Update our consumer index */
217923ac9029SStephen Hurd 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) {
218023ac9029SStephen Hurd 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
21814c7070dbSScott Long 				rxq->ifr_cq_gen = 0;
21824c7070dbSScott Long 			}
21834c7070dbSScott Long 			/* was this only a completion queue message? */
21844c7070dbSScott Long 			if (__predict_false(ri.iri_nfrags == 0))
21854c7070dbSScott Long 				continue;
21864c7070dbSScott Long 		}
21874c7070dbSScott Long 		MPASS(ri.iri_nfrags != 0);
21884c7070dbSScott Long 		MPASS(ri.iri_len != 0);
21894c7070dbSScott Long 
21904c7070dbSScott Long 		/* will advance the cidx on the corresponding free lists */
21914c7070dbSScott Long 		m = iflib_rxd_pkt_get(rxq, &ri);
21924c7070dbSScott Long 		if (avail == 0 && budget_left)
219323ac9029SStephen Hurd 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
21944c7070dbSScott Long 
21954c7070dbSScott Long 		if (__predict_false(m == NULL)) {
21964c7070dbSScott Long 			DBG_COUNTER_INC(rx_mbuf_null);
21974c7070dbSScott Long 			continue;
21984c7070dbSScott Long 		}
21994c7070dbSScott Long 		/* imm_pkt: -- cxgb */
22004c7070dbSScott Long 		if (mh == NULL)
22014c7070dbSScott Long 			mh = mt = m;
22024c7070dbSScott Long 		else {
22034c7070dbSScott Long 			mt->m_nextpkt = m;
22044c7070dbSScott Long 			mt = m;
22054c7070dbSScott Long 		}
22064c7070dbSScott Long 	}
22074c7070dbSScott Long 	/* make sure that we can refill faster than drain */
22084c7070dbSScott Long 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
22094c7070dbSScott Long 		__iflib_fl_refill_lt(ctx, fl, budget + 8);
22104c7070dbSScott Long 
22114c7070dbSScott Long 	ifp = ctx->ifc_ifp;
22124c7070dbSScott Long 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
22134c7070dbSScott Long 	while (mh != NULL) {
22144c7070dbSScott Long 		m = mh;
22154c7070dbSScott Long 		mh = mh->m_nextpkt;
22164c7070dbSScott Long 		m->m_nextpkt = NULL;
22174c7070dbSScott Long 		rx_bytes += m->m_pkthdr.len;
22184c7070dbSScott Long 		rx_pkts++;
2219aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
22204c7070dbSScott Long 		if (lro_enabled && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
22214c7070dbSScott Long 			continue;
2222aaeb188aSBjoern A. Zeeb #endif
22234c7070dbSScott Long 		DBG_COUNTER_INC(rx_if_input);
22244c7070dbSScott Long 		ifp->if_input(ifp, m);
22254c7070dbSScott Long 	}
222623ac9029SStephen Hurd 
22274c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
22284c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
22294c7070dbSScott Long 
22304c7070dbSScott Long 	/*
22314c7070dbSScott Long 	 * Flush any outstanding LRO work
22324c7070dbSScott Long 	 */
2233aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
223423ac9029SStephen Hurd 	tcp_lro_flush_all(&rxq->ifr_lc);
2235aaeb188aSBjoern A. Zeeb #endif
223623ac9029SStephen Hurd 	if (avail)
223723ac9029SStephen Hurd 		return true;
223823ac9029SStephen Hurd 	return (iflib_rxd_avail(ctx, rxq, *cidxp, 1));
22394c7070dbSScott Long }
22404c7070dbSScott Long 
22414c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
22424c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
224323ac9029SStephen Hurd #define TXQ_MAX_DB_DEFERRED(size) (size >> 5)
224423ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
22454c7070dbSScott Long 
22464c7070dbSScott Long static __inline void
22474c7070dbSScott Long iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring)
22484c7070dbSScott Long {
22494c7070dbSScott Long 	uint32_t dbval;
22504c7070dbSScott Long 
225123ac9029SStephen Hurd 	if (ring || txq->ift_db_pending >=
225223ac9029SStephen Hurd 	    TXQ_MAX_DB_DEFERRED(txq->ift_size)) {
22534c7070dbSScott Long 
22544c7070dbSScott Long 		/* the lock will only ever be contended in the !min_latency case */
22554c7070dbSScott Long 		if (!TXDB_TRYLOCK(txq))
22564c7070dbSScott Long 			return;
22574c7070dbSScott Long 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
22584c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
22594c7070dbSScott Long 		txq->ift_db_pending = txq->ift_npending = 0;
22604c7070dbSScott Long 		TXDB_UNLOCK(txq);
22614c7070dbSScott Long 	}
22624c7070dbSScott Long }
22634c7070dbSScott Long 
22644c7070dbSScott Long static void
22654c7070dbSScott Long iflib_txd_deferred_db_check(void * arg)
22664c7070dbSScott Long {
22674c7070dbSScott Long 	iflib_txq_t txq = arg;
22684c7070dbSScott Long 
22694c7070dbSScott Long 	/* simple non-zero boolean so use bitwise OR */
22704c7070dbSScott Long 	if ((txq->ift_db_pending | txq->ift_npending) &&
22714c7070dbSScott Long 	    txq->ift_db_pending >= txq->ift_db_pending_queued)
22724c7070dbSScott Long 		iflib_txd_db_check(txq->ift_ctx, txq, TRUE);
22734c7070dbSScott Long 	txq->ift_db_pending_queued = 0;
22744c7070dbSScott Long 	if (ifmp_ring_is_stalled(txq->ift_br[0]))
22754c7070dbSScott Long 		iflib_txq_check_drain(txq, 4);
22764c7070dbSScott Long }
22774c7070dbSScott Long 
22784c7070dbSScott Long #ifdef PKT_DEBUG
22794c7070dbSScott Long static void
22804c7070dbSScott Long print_pkt(if_pkt_info_t pi)
22814c7070dbSScott Long {
22824c7070dbSScott Long 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
22834c7070dbSScott Long 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
22844c7070dbSScott Long 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
22854c7070dbSScott Long 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
22864c7070dbSScott Long 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
22874c7070dbSScott Long 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
22884c7070dbSScott Long }
22894c7070dbSScott Long #endif
22904c7070dbSScott Long 
22914c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
22924c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
22934c7070dbSScott Long 
22944c7070dbSScott Long static int
22954c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
22964c7070dbSScott Long {
22971248952aSSean Bruno 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
22984c7070dbSScott Long 	struct ether_vlan_header *eh;
229923ac9029SStephen Hurd 	struct mbuf *m, *n;
23004c7070dbSScott Long 
230123ac9029SStephen Hurd 	n = m = *mp;
23021248952aSSean Bruno 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
23031248952aSSean Bruno 	    M_WRITABLE(m) == 0) {
23041248952aSSean Bruno 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
23051248952aSSean Bruno 			return (ENOMEM);
23061248952aSSean Bruno 		} else {
23071248952aSSean Bruno 			m_freem(*mp);
23081248952aSSean Bruno 			n = *mp = m;
23091248952aSSean Bruno 		}
23101248952aSSean Bruno 	}
23111248952aSSean Bruno 
23124c7070dbSScott Long 	/*
23134c7070dbSScott Long 	 * Determine where frame payload starts.
23144c7070dbSScott Long 	 * Jump over vlan headers if already present,
23154c7070dbSScott Long 	 * helpful for QinQ too.
23164c7070dbSScott Long 	 */
23174c7070dbSScott Long 	if (__predict_false(m->m_len < sizeof(*eh))) {
23184c7070dbSScott Long 		txq->ift_pullups++;
23194c7070dbSScott Long 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
23204c7070dbSScott Long 			return (ENOMEM);
23214c7070dbSScott Long 	}
23224c7070dbSScott Long 	eh = mtod(m, struct ether_vlan_header *);
23234c7070dbSScott Long 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
23244c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_proto);
23254c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
23264c7070dbSScott Long 	} else {
23274c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
23284c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
23294c7070dbSScott Long 	}
23304c7070dbSScott Long 
23314c7070dbSScott Long 	switch (pi->ipi_etype) {
23324c7070dbSScott Long #ifdef INET
23334c7070dbSScott Long 	case ETHERTYPE_IP:
23344c7070dbSScott Long 	{
23354c7070dbSScott Long 		struct ip *ip = NULL;
23364c7070dbSScott Long 		struct tcphdr *th = NULL;
23374c7070dbSScott Long 		int minthlen;
23384c7070dbSScott Long 
23394c7070dbSScott Long 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
23404c7070dbSScott Long 		if (__predict_false(m->m_len < minthlen)) {
23414c7070dbSScott Long 			/*
23424c7070dbSScott Long 			 * if this code bloat is causing too much of a hit
23434c7070dbSScott Long 			 * move it to a separate function and mark it noinline
23444c7070dbSScott Long 			 */
23454c7070dbSScott Long 			if (m->m_len == pi->ipi_ehdrlen) {
23464c7070dbSScott Long 				n = m->m_next;
23474c7070dbSScott Long 				MPASS(n);
23484c7070dbSScott Long 				if (n->m_len >= sizeof(*ip))  {
23494c7070dbSScott Long 					ip = (struct ip *)n->m_data;
23504c7070dbSScott Long 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
23514c7070dbSScott Long 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
23524c7070dbSScott Long 				} else {
23534c7070dbSScott Long 					txq->ift_pullups++;
23544c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
23554c7070dbSScott Long 						return (ENOMEM);
23564c7070dbSScott Long 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
23574c7070dbSScott Long 				}
23584c7070dbSScott Long 			} else {
23594c7070dbSScott Long 				txq->ift_pullups++;
23604c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
23614c7070dbSScott Long 					return (ENOMEM);
23624c7070dbSScott Long 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
23634c7070dbSScott Long 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
23644c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
23654c7070dbSScott Long 			}
23664c7070dbSScott Long 		} else {
23674c7070dbSScott Long 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
23684c7070dbSScott Long 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
23694c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
23704c7070dbSScott Long 		}
23714c7070dbSScott Long 		pi->ipi_ip_hlen = ip->ip_hl << 2;
23724c7070dbSScott Long 		pi->ipi_ipproto = ip->ip_p;
23734c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV4;
23744c7070dbSScott Long 
23754c7070dbSScott Long 		if (pi->ipi_csum_flags & CSUM_IP)
23764c7070dbSScott Long                        ip->ip_sum = 0;
23774c7070dbSScott Long 
23784c7070dbSScott Long 		if (pi->ipi_ipproto == IPPROTO_TCP) {
23794c7070dbSScott Long 			if (__predict_false(th == NULL)) {
23804c7070dbSScott Long 				txq->ift_pullups++;
23814c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
23824c7070dbSScott Long 					return (ENOMEM);
23834c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
23844c7070dbSScott Long 			}
23854c7070dbSScott Long 			pi->ipi_tcp_hflags = th->th_flags;
23864c7070dbSScott Long 			pi->ipi_tcp_hlen = th->th_off << 2;
23874c7070dbSScott Long 			pi->ipi_tcp_seq = th->th_seq;
23884c7070dbSScott Long 		}
23894c7070dbSScott Long 		if (IS_TSO4(pi)) {
23904c7070dbSScott Long 			if (__predict_false(ip->ip_p != IPPROTO_TCP))
23914c7070dbSScott Long 				return (ENXIO);
23924c7070dbSScott Long 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
23934c7070dbSScott Long 					       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
23944c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
23951248952aSSean Bruno 			if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
23961248952aSSean Bruno 				ip->ip_sum = 0;
23971248952aSSean Bruno 				ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
23981248952aSSean Bruno 			}
23994c7070dbSScott Long 		}
24004c7070dbSScott Long 		break;
24014c7070dbSScott Long 	}
24024c7070dbSScott Long #endif
24034c7070dbSScott Long #ifdef INET6
24044c7070dbSScott Long 	case ETHERTYPE_IPV6:
24054c7070dbSScott Long 	{
24064c7070dbSScott Long 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
24074c7070dbSScott Long 		struct tcphdr *th;
24084c7070dbSScott Long 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
24094c7070dbSScott Long 
24104c7070dbSScott Long 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
24114c7070dbSScott Long 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
24124c7070dbSScott Long 				return (ENOMEM);
24134c7070dbSScott Long 		}
24144c7070dbSScott Long 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
24154c7070dbSScott Long 
24164c7070dbSScott Long 		/* XXX-BZ this will go badly in case of ext hdrs. */
24174c7070dbSScott Long 		pi->ipi_ipproto = ip6->ip6_nxt;
24184c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV6;
24194c7070dbSScott Long 
24204c7070dbSScott Long 		if (pi->ipi_ipproto == IPPROTO_TCP) {
24214c7070dbSScott Long 			if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
24224c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
24234c7070dbSScott Long 					return (ENOMEM);
24244c7070dbSScott Long 			}
24254c7070dbSScott Long 			pi->ipi_tcp_hflags = th->th_flags;
24264c7070dbSScott Long 			pi->ipi_tcp_hlen = th->th_off << 2;
24274c7070dbSScott Long 		}
24284c7070dbSScott Long 		if (IS_TSO6(pi)) {
24294c7070dbSScott Long 
24304c7070dbSScott Long 			if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
24314c7070dbSScott Long 				return (ENXIO);
24324c7070dbSScott Long 			/*
24334c7070dbSScott Long 			 * The corresponding flag is set by the stack in the IPv4
24344c7070dbSScott Long 			 * TSO case, but not in IPv6 (at least in FreeBSD 10.2).
24354c7070dbSScott Long 			 * So, set it here because the rest of the flow requires it.
24364c7070dbSScott Long 			 */
24374c7070dbSScott Long 			pi->ipi_csum_flags |= CSUM_TCP_IPV6;
24384c7070dbSScott Long 			th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
24394c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
24404c7070dbSScott Long 		}
24414c7070dbSScott Long 		break;
24424c7070dbSScott Long 	}
24434c7070dbSScott Long #endif
24444c7070dbSScott Long 	default:
24454c7070dbSScott Long 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
24464c7070dbSScott Long 		pi->ipi_ip_hlen = 0;
24474c7070dbSScott Long 		break;
24484c7070dbSScott Long 	}
24494c7070dbSScott Long 	*mp = m;
24501248952aSSean Bruno 
24514c7070dbSScott Long 	return (0);
24524c7070dbSScott Long }
24534c7070dbSScott Long 
24544c7070dbSScott Long static  __noinline  struct mbuf *
24554c7070dbSScott Long collapse_pkthdr(struct mbuf *m0)
24564c7070dbSScott Long {
24574c7070dbSScott Long 	struct mbuf *m, *m_next, *tmp;
24584c7070dbSScott Long 
24594c7070dbSScott Long 	m = m0;
24604c7070dbSScott Long 	m_next = m->m_next;
24614c7070dbSScott Long 	while (m_next != NULL && m_next->m_len == 0) {
24624c7070dbSScott Long 		m = m_next;
24634c7070dbSScott Long 		m->m_next = NULL;
24644c7070dbSScott Long 		m_free(m);
24654c7070dbSScott Long 		m_next = m_next->m_next;
24664c7070dbSScott Long 	}
24674c7070dbSScott Long 	m = m0;
24684c7070dbSScott Long 	m->m_next = m_next;
24694c7070dbSScott Long 	if ((m_next->m_flags & M_EXT) == 0) {
24704c7070dbSScott Long 		m = m_defrag(m, M_NOWAIT);
24714c7070dbSScott Long 	} else {
24724c7070dbSScott Long 		tmp = m_next->m_next;
24734c7070dbSScott Long 		memcpy(m_next, m, MPKTHSIZE);
24744c7070dbSScott Long 		m = m_next;
24754c7070dbSScott Long 		m->m_next = tmp;
24764c7070dbSScott Long 	}
24774c7070dbSScott Long 	return (m);
24784c7070dbSScott Long }
24794c7070dbSScott Long 
24804c7070dbSScott Long /*
24814c7070dbSScott Long  * If dodgy hardware rejects the scatter gather chain we've handed it
248223ac9029SStephen Hurd  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
248323ac9029SStephen Hurd  * m_defrag'd mbufs
24844c7070dbSScott Long  */
24854c7070dbSScott Long static __noinline struct mbuf *
248623ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq)
24874c7070dbSScott Long {
248823ac9029SStephen Hurd 	int ntxd, i, pidx;
24894c7070dbSScott Long 	struct mbuf *m, *mh, **ifsd_m;
24904c7070dbSScott Long 
24914c7070dbSScott Long 	pidx = txq->ift_pidx;
24924c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
249323ac9029SStephen Hurd 	ntxd = txq->ift_size;
24944c7070dbSScott Long 	mh = m = ifsd_m[pidx];
24954c7070dbSScott Long 	ifsd_m[pidx] = NULL;
24964c7070dbSScott Long #if MEMORY_LOGGING
24974c7070dbSScott Long 	txq->ift_dequeued++;
24984c7070dbSScott Long #endif
24994c7070dbSScott Long 	i = 1;
25004c7070dbSScott Long 
250123ac9029SStephen Hurd 	while (m) {
25024c7070dbSScott Long 		ifsd_m[(pidx + i) & (ntxd -1)] = NULL;
25034c7070dbSScott Long #if MEMORY_LOGGING
25044c7070dbSScott Long 		txq->ift_dequeued++;
25054c7070dbSScott Long #endif
25064c7070dbSScott Long 		m = m->m_next;
25074c7070dbSScott Long 		i++;
25084c7070dbSScott Long 	}
25094c7070dbSScott Long 	return (mh);
25104c7070dbSScott Long }
25114c7070dbSScott Long 
25124c7070dbSScott Long static int
25134c7070dbSScott Long iflib_busdma_load_mbuf_sg(iflib_txq_t txq, bus_dma_tag_t tag, bus_dmamap_t map,
25144c7070dbSScott Long 			  struct mbuf **m0, bus_dma_segment_t *segs, int *nsegs,
25154c7070dbSScott Long 			  int max_segs, int flags)
25164c7070dbSScott Long {
25174c7070dbSScott Long 	if_ctx_t ctx;
25184c7070dbSScott Long 	if_shared_ctx_t		sctx;
251923ac9029SStephen Hurd 	if_softc_ctx_t		scctx;
25204c7070dbSScott Long 	int i, next, pidx, mask, err, maxsegsz, ntxd, count;
25214c7070dbSScott Long 	struct mbuf *m, *tmp, **ifsd_m, **mp;
25224c7070dbSScott Long 
25234c7070dbSScott Long 	m = *m0;
25244c7070dbSScott Long 
25254c7070dbSScott Long 	/*
25264c7070dbSScott Long 	 * Please don't ever do this
25274c7070dbSScott Long 	 */
25284c7070dbSScott Long 	if (__predict_false(m->m_len == 0))
25294c7070dbSScott Long 		*m0 = m = collapse_pkthdr(m);
25304c7070dbSScott Long 
25314c7070dbSScott Long 	ctx = txq->ift_ctx;
25324c7070dbSScott Long 	sctx = ctx->ifc_sctx;
253323ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
25344c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
253523ac9029SStephen Hurd 	ntxd = txq->ift_size;
25364c7070dbSScott Long 	pidx = txq->ift_pidx;
25374c7070dbSScott Long 	if (map != NULL) {
25384c7070dbSScott Long 		uint8_t *ifsd_flags = txq->ift_sds.ifsd_flags;
25394c7070dbSScott Long 
25404c7070dbSScott Long 		err = bus_dmamap_load_mbuf_sg(tag, map,
25414c7070dbSScott Long 					      *m0, segs, nsegs, BUS_DMA_NOWAIT);
25424c7070dbSScott Long 		if (err)
25434c7070dbSScott Long 			return (err);
25444c7070dbSScott Long 		ifsd_flags[pidx] |= TX_SW_DESC_MAPPED;
25454c7070dbSScott Long 		i = 0;
25464c7070dbSScott Long 		next = pidx;
254723ac9029SStephen Hurd 		mask = (txq->ift_size-1);
25484c7070dbSScott Long 		m = *m0;
25494c7070dbSScott Long 		do {
25504c7070dbSScott Long 			mp = &ifsd_m[next];
25514c7070dbSScott Long 			*mp = m;
25524c7070dbSScott Long 			m = m->m_next;
25534c7070dbSScott Long 			if (__predict_false((*mp)->m_len == 0)) {
25544c7070dbSScott Long 				m_free(*mp);
25554c7070dbSScott Long 				*mp = NULL;
25564c7070dbSScott Long 			} else
25574c7070dbSScott Long 				next = (pidx + i) & (ntxd-1);
25584c7070dbSScott Long 		} while (m != NULL);
25594c7070dbSScott Long 	} else {
25604c7070dbSScott Long 		int buflen, sgsize, max_sgsize;
25614c7070dbSScott Long 		vm_offset_t vaddr;
25624c7070dbSScott Long 		vm_paddr_t curaddr;
25634c7070dbSScott Long 
25644c7070dbSScott Long 		count = i = 0;
25654c7070dbSScott Long 		maxsegsz = sctx->isc_tx_maxsize;
25664c7070dbSScott Long 		m = *m0;
25674c7070dbSScott Long 		do {
25684c7070dbSScott Long 			if (__predict_false(m->m_len <= 0)) {
25694c7070dbSScott Long 				tmp = m;
25704c7070dbSScott Long 				m = m->m_next;
25714c7070dbSScott Long 				tmp->m_next = NULL;
25724c7070dbSScott Long 				m_free(tmp);
25734c7070dbSScott Long 				continue;
25744c7070dbSScott Long 			}
25754c7070dbSScott Long 			buflen = m->m_len;
25764c7070dbSScott Long 			vaddr = (vm_offset_t)m->m_data;
25774c7070dbSScott Long 			/*
25784c7070dbSScott Long 			 * see if we can't be smarter about physically
25794c7070dbSScott Long 			 * contiguous mappings
25804c7070dbSScott Long 			 */
25814c7070dbSScott Long 			next = (pidx + count) & (ntxd-1);
25824c7070dbSScott Long 			MPASS(ifsd_m[next] == NULL);
25834c7070dbSScott Long #if MEMORY_LOGGING
25844c7070dbSScott Long 			txq->ift_enqueued++;
25854c7070dbSScott Long #endif
25864c7070dbSScott Long 			ifsd_m[next] = m;
25874c7070dbSScott Long 			while (buflen > 0) {
25884c7070dbSScott Long 				max_sgsize = MIN(buflen, maxsegsz);
25894c7070dbSScott Long 				curaddr = pmap_kextract(vaddr);
25904c7070dbSScott Long 				sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
25914c7070dbSScott Long 				sgsize = MIN(sgsize, max_sgsize);
25924c7070dbSScott Long 				segs[i].ds_addr = curaddr;
25934c7070dbSScott Long 				segs[i].ds_len = sgsize;
25944c7070dbSScott Long 				vaddr += sgsize;
25954c7070dbSScott Long 				buflen -= sgsize;
25964c7070dbSScott Long 				i++;
25974c7070dbSScott Long 				if (i >= max_segs)
25984c7070dbSScott Long 					goto err;
25994c7070dbSScott Long 			}
26004c7070dbSScott Long 			count++;
26014c7070dbSScott Long 			tmp = m;
26024c7070dbSScott Long 			m = m->m_next;
26034c7070dbSScott Long 		} while (m != NULL);
26044c7070dbSScott Long 		*nsegs = i;
26054c7070dbSScott Long 	}
26064c7070dbSScott Long 	return (0);
26074c7070dbSScott Long err:
260823ac9029SStephen Hurd 	*m0 = iflib_remove_mbuf(txq);
26094c7070dbSScott Long 	return (EFBIG);
26104c7070dbSScott Long }
26114c7070dbSScott Long 
26124c7070dbSScott Long static int
26134c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
26144c7070dbSScott Long {
26154c7070dbSScott Long 	if_ctx_t		ctx;
26164c7070dbSScott Long 	if_shared_ctx_t		sctx;
26174c7070dbSScott Long 	if_softc_ctx_t		scctx;
26184c7070dbSScott Long 	bus_dma_segment_t	*segs;
26194c7070dbSScott Long 	struct mbuf		*m_head;
26204c7070dbSScott Long 	bus_dmamap_t		map;
26214c7070dbSScott Long 	struct if_pkt_info	pi;
26224c7070dbSScott Long 	int remap = 0;
26234c7070dbSScott Long 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
26244c7070dbSScott Long 	bus_dma_tag_t desc_tag;
26254c7070dbSScott Long 
26264c7070dbSScott Long 	segs = txq->ift_segs;
26274c7070dbSScott Long 	ctx = txq->ift_ctx;
26284c7070dbSScott Long 	sctx = ctx->ifc_sctx;
26294c7070dbSScott Long 	scctx = &ctx->ifc_softc_ctx;
26304c7070dbSScott Long 	segs = txq->ift_segs;
263123ac9029SStephen Hurd 	ntxd = txq->ift_size;
26324c7070dbSScott Long 	m_head = *m_headp;
26334c7070dbSScott Long 	map = NULL;
26344c7070dbSScott Long 
26354c7070dbSScott Long 	/*
26364c7070dbSScott Long 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
26374c7070dbSScott Long 	 */
26384c7070dbSScott Long 	cidx = txq->ift_cidx;
26394c7070dbSScott Long 	pidx = txq->ift_pidx;
26404c7070dbSScott Long 	next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
26414c7070dbSScott Long 
26424c7070dbSScott Long 	/* prefetch the next cache line of mbuf pointers and flags */
26434c7070dbSScott Long 	prefetch(&txq->ift_sds.ifsd_m[next]);
26444c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
26454c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_map[next]);
26464c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[pidx];
26474c7070dbSScott Long 		next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
26484c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_flags[next]);
26494c7070dbSScott Long 	}
26504c7070dbSScott Long 
26514c7070dbSScott Long 
26524c7070dbSScott Long 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
26534c7070dbSScott Long 		desc_tag = txq->ift_tso_desc_tag;
26544c7070dbSScott Long 		max_segs = scctx->isc_tx_tso_segments_max;
26554c7070dbSScott Long 	} else {
26564c7070dbSScott Long 		desc_tag = txq->ift_desc_tag;
26574c7070dbSScott Long 		max_segs = scctx->isc_tx_nsegments;
26584c7070dbSScott Long 	}
26594c7070dbSScott Long 	m_head = *m_headp;
26604c7070dbSScott Long 	bzero(&pi, sizeof(pi));
26614c7070dbSScott Long 	pi.ipi_len = m_head->m_pkthdr.len;
26624c7070dbSScott Long 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
26634c7070dbSScott Long 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
26644c7070dbSScott Long 	pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0;
26654c7070dbSScott Long 	pi.ipi_pidx = pidx;
26664c7070dbSScott Long 	pi.ipi_qsidx = txq->ift_id;
26674c7070dbSScott Long 
26684c7070dbSScott Long 	/* deliberate bitwise OR to make one condition */
26694c7070dbSScott Long 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
26704c7070dbSScott Long 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0))
26714c7070dbSScott Long 			return (err);
26724c7070dbSScott Long 		m_head = *m_headp;
26734c7070dbSScott Long 	}
26744c7070dbSScott Long 
26754c7070dbSScott Long retry:
26764c7070dbSScott Long 	err = iflib_busdma_load_mbuf_sg(txq, desc_tag, map, m_headp, segs, &nsegs, max_segs, BUS_DMA_NOWAIT);
26774c7070dbSScott Long defrag:
26784c7070dbSScott Long 	if (__predict_false(err)) {
26794c7070dbSScott Long 		switch (err) {
26804c7070dbSScott Long 		case EFBIG:
26814c7070dbSScott Long 			/* try collapse once and defrag once */
26824c7070dbSScott Long 			if (remap == 0)
26834c7070dbSScott Long 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
26844c7070dbSScott Long 			if (remap == 1)
26854c7070dbSScott Long 				m_head = m_defrag(*m_headp, M_NOWAIT);
26864c7070dbSScott Long 			remap++;
26874c7070dbSScott Long 			if (__predict_false(m_head == NULL))
26884c7070dbSScott Long 				goto defrag_failed;
26894c7070dbSScott Long 			txq->ift_mbuf_defrag++;
26904c7070dbSScott Long 			*m_headp = m_head;
26914c7070dbSScott Long 			goto retry;
26924c7070dbSScott Long 			break;
26934c7070dbSScott Long 		case ENOMEM:
26944c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
26954c7070dbSScott Long 			break;
26964c7070dbSScott Long 		default:
26974c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
26984c7070dbSScott Long 			m_freem(*m_headp);
26994c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
27004c7070dbSScott Long 			*m_headp = NULL;
27014c7070dbSScott Long 			break;
27024c7070dbSScott Long 		}
27034c7070dbSScott Long 		txq->ift_map_failed++;
27044c7070dbSScott Long 		DBG_COUNTER_INC(encap_load_mbuf_fail);
27054c7070dbSScott Long 		return (err);
27064c7070dbSScott Long 	}
27074c7070dbSScott Long 
27084c7070dbSScott Long 	/*
27094c7070dbSScott Long 	 * XXX assumes a 1 to 1 relationship between segments and
27104c7070dbSScott Long 	 *        descriptors - this does not hold true on all drivers, e.g.
27114c7070dbSScott Long 	 *        cxgb
27124c7070dbSScott Long 	 */
27134c7070dbSScott Long 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
27144c7070dbSScott Long 		txq->ift_no_desc_avail++;
27154c7070dbSScott Long 		if (map != NULL)
27164c7070dbSScott Long 			bus_dmamap_unload(desc_tag, map);
27174c7070dbSScott Long 		DBG_COUNTER_INC(encap_txq_avail_fail);
271823ac9029SStephen Hurd 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
27194c7070dbSScott Long 			GROUPTASK_ENQUEUE(&txq->ift_task);
27204c7070dbSScott Long 		return (ENOBUFS);
27214c7070dbSScott Long 	}
27224c7070dbSScott Long 	pi.ipi_segs = segs;
27234c7070dbSScott Long 	pi.ipi_nsegs = nsegs;
27244c7070dbSScott Long 
272523ac9029SStephen Hurd 	MPASS(pidx >= 0 && pidx < txq->ift_size);
27264c7070dbSScott Long #ifdef PKT_DEBUG
27274c7070dbSScott Long 	print_pkt(&pi);
27284c7070dbSScott Long #endif
27294c7070dbSScott Long 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
27304c7070dbSScott Long 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
27314c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
27324c7070dbSScott Long 
27334c7070dbSScott Long 		DBG_COUNTER_INC(tx_encap);
273423ac9029SStephen Hurd 		MPASS(pi.ipi_new_pidx >= 0 &&
273523ac9029SStephen Hurd 		    pi.ipi_new_pidx < txq->ift_size);
27364c7070dbSScott Long 
27374c7070dbSScott Long 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
27384c7070dbSScott Long 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
273923ac9029SStephen Hurd 			ndesc += txq->ift_size;
27404c7070dbSScott Long 			txq->ift_gen = 1;
27414c7070dbSScott Long 		}
27421248952aSSean Bruno 		/*
27431248952aSSean Bruno 		 * drivers can need as many as
27441248952aSSean Bruno 		 * two sentinels
27451248952aSSean Bruno 		 */
27461248952aSSean Bruno 		MPASS(ndesc <= pi.ipi_nsegs + 2);
27474c7070dbSScott Long 		MPASS(pi.ipi_new_pidx != pidx);
27484c7070dbSScott Long 		MPASS(ndesc > 0);
27494c7070dbSScott Long 		txq->ift_in_use += ndesc;
27504c7070dbSScott Long 		/*
27514c7070dbSScott Long 		 * We update the last software descriptor again here because there may
27524c7070dbSScott Long 		 * be a sentinel and/or there may be more mbufs than segments
27534c7070dbSScott Long 		 */
27544c7070dbSScott Long 		txq->ift_pidx = pi.ipi_new_pidx;
27554c7070dbSScott Long 		txq->ift_npending += pi.ipi_ndescs;
27564c7070dbSScott Long 	} else if (__predict_false(err == EFBIG && remap < 2)) {
275723ac9029SStephen Hurd 		*m_headp = m_head = iflib_remove_mbuf(txq);
27584c7070dbSScott Long 		remap = 1;
27594c7070dbSScott Long 		txq->ift_txd_encap_efbig++;
27604c7070dbSScott Long 		goto defrag;
27614c7070dbSScott Long 	} else
27624c7070dbSScott Long 		DBG_COUNTER_INC(encap_txd_encap_fail);
27634c7070dbSScott Long 	return (err);
27644c7070dbSScott Long 
27654c7070dbSScott Long defrag_failed:
27664c7070dbSScott Long 	txq->ift_mbuf_defrag_failed++;
27674c7070dbSScott Long 	txq->ift_map_failed++;
27684c7070dbSScott Long 	m_freem(*m_headp);
27694c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
27704c7070dbSScott Long 	*m_headp = NULL;
27714c7070dbSScott Long 	return (ENOMEM);
27724c7070dbSScott Long }
27734c7070dbSScott Long 
27744c7070dbSScott Long /* forward compatibility for cxgb */
27754c7070dbSScott Long #define FIRST_QSET(ctx) 0
27764c7070dbSScott Long 
27774c7070dbSScott Long #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
27784c7070dbSScott Long #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
277923ac9029SStephen Hurd #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
27804c7070dbSScott Long #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
27814c7070dbSScott Long #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
27824c7070dbSScott Long #define MAX_TX_DESC(ctx) ((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max)
27834c7070dbSScott Long 
27844c7070dbSScott Long 
27854c7070dbSScott Long 
27864c7070dbSScott Long /* if there are more than TXQ_MIN_OCCUPANCY packets pending we consider deferring
27874c7070dbSScott Long  * doorbell writes
27884c7070dbSScott Long  *
27894c7070dbSScott Long  * ORing with 2 assures that min occupancy is never less than 2 without any conditional logic
27904c7070dbSScott Long  */
279123ac9029SStephen Hurd #define TXQ_MIN_OCCUPANCY(size) ((size >> 6)| 0x2)
27924c7070dbSScott Long 
27934c7070dbSScott Long static inline int
27944c7070dbSScott Long iflib_txq_min_occupancy(iflib_txq_t txq)
27954c7070dbSScott Long {
27964c7070dbSScott Long 	if_ctx_t ctx;
27974c7070dbSScott Long 
27984c7070dbSScott Long 	ctx = txq->ift_ctx;
279923ac9029SStephen Hurd 	return (get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx,
280023ac9029SStephen Hurd 	    txq->ift_gen) < TXQ_MIN_OCCUPANCY(txq->ift_size) +
280123ac9029SStephen Hurd 	    MAX_TX_DESC(ctx));
28024c7070dbSScott Long }
28034c7070dbSScott Long 
28044c7070dbSScott Long static void
28054c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n)
28064c7070dbSScott Long {
28074c7070dbSScott Long 	int hasmap;
28084c7070dbSScott Long 	uint32_t qsize, cidx, mask, gen;
28094c7070dbSScott Long 	struct mbuf *m, **ifsd_m;
28104c7070dbSScott Long 	uint8_t *ifsd_flags;
28114c7070dbSScott Long 	bus_dmamap_t *ifsd_map;
28124c7070dbSScott Long 
28134c7070dbSScott Long 	cidx = txq->ift_cidx;
28144c7070dbSScott Long 	gen = txq->ift_gen;
281523ac9029SStephen Hurd 	qsize = txq->ift_size;
28164c7070dbSScott Long 	mask = qsize-1;
28174c7070dbSScott Long 	hasmap = txq->ift_sds.ifsd_map != NULL;
28184c7070dbSScott Long 	ifsd_flags = txq->ift_sds.ifsd_flags;
28194c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
28204c7070dbSScott Long 	ifsd_map = txq->ift_sds.ifsd_map;
28214c7070dbSScott Long 
28224c7070dbSScott Long 	while (n--) {
28234c7070dbSScott Long 		prefetch(ifsd_m[(cidx + 3) & mask]);
28244c7070dbSScott Long 		prefetch(ifsd_m[(cidx + 4) & mask]);
28254c7070dbSScott Long 
28264c7070dbSScott Long 		if (ifsd_m[cidx] != NULL) {
28274c7070dbSScott Long 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
28284c7070dbSScott Long 			prefetch(&ifsd_flags[(cidx + CACHE_PTR_INCREMENT) & mask]);
28294c7070dbSScott Long 			if (hasmap && (ifsd_flags[cidx] & TX_SW_DESC_MAPPED)) {
28304c7070dbSScott Long 				/*
28314c7070dbSScott Long 				 * does it matter if it's not the TSO tag? If so we'll
28324c7070dbSScott Long 				 * have to add the type to flags
28334c7070dbSScott Long 				 */
28344c7070dbSScott Long 				bus_dmamap_unload(txq->ift_desc_tag, ifsd_map[cidx]);
28354c7070dbSScott Long 				ifsd_flags[cidx] &= ~TX_SW_DESC_MAPPED;
28364c7070dbSScott Long 			}
28374c7070dbSScott Long 			if ((m = ifsd_m[cidx]) != NULL) {
28384c7070dbSScott Long 				/* XXX we don't support any drivers that batch packets yet */
28394c7070dbSScott Long 				MPASS(m->m_nextpkt == NULL);
28404c7070dbSScott Long 
284123ac9029SStephen Hurd 				m_free(m);
28424c7070dbSScott Long 				ifsd_m[cidx] = NULL;
28434c7070dbSScott Long #if MEMORY_LOGGING
28444c7070dbSScott Long 				txq->ift_dequeued++;
28454c7070dbSScott Long #endif
28464c7070dbSScott Long 				DBG_COUNTER_INC(tx_frees);
28474c7070dbSScott Long 			}
28484c7070dbSScott Long 		}
28494c7070dbSScott Long 		if (__predict_false(++cidx == qsize)) {
28504c7070dbSScott Long 			cidx = 0;
28514c7070dbSScott Long 			gen = 0;
28524c7070dbSScott Long 		}
28534c7070dbSScott Long 	}
28544c7070dbSScott Long 	txq->ift_cidx = cidx;
28554c7070dbSScott Long 	txq->ift_gen = gen;
28564c7070dbSScott Long }
28574c7070dbSScott Long 
28584c7070dbSScott Long static __inline int
28594c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
28604c7070dbSScott Long {
28614c7070dbSScott Long 	int reclaim;
28624c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
28634c7070dbSScott Long 
28644c7070dbSScott Long 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
28654c7070dbSScott Long 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
28664c7070dbSScott Long 
28674c7070dbSScott Long 	/*
28684c7070dbSScott Long 	 * Need a rate-limiting check so that this isn't called every time
28694c7070dbSScott Long 	 */
28704c7070dbSScott Long 	iflib_tx_credits_update(ctx, txq);
28714c7070dbSScott Long 	reclaim = DESC_RECLAIMABLE(txq);
28724c7070dbSScott Long 
28734c7070dbSScott Long 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
28744c7070dbSScott Long #ifdef INVARIANTS
28754c7070dbSScott Long 		if (iflib_verbose_debug) {
28764c7070dbSScott Long 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
28774c7070dbSScott Long 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
28784c7070dbSScott Long 			       reclaim, thresh);
28794c7070dbSScott Long 
28804c7070dbSScott Long 		}
28814c7070dbSScott Long #endif
28824c7070dbSScott Long 		return (0);
28834c7070dbSScott Long 	}
28844c7070dbSScott Long 	iflib_tx_desc_free(txq, reclaim);
28854c7070dbSScott Long 	txq->ift_cleaned += reclaim;
28864c7070dbSScott Long 	txq->ift_in_use -= reclaim;
28874c7070dbSScott Long 
28884c7070dbSScott Long 	if (txq->ift_active == FALSE)
28894c7070dbSScott Long 		txq->ift_active = TRUE;
28904c7070dbSScott Long 
28914c7070dbSScott Long 	return (reclaim);
28924c7070dbSScott Long }
28934c7070dbSScott Long 
28944c7070dbSScott Long static struct mbuf **
28954c7070dbSScott Long _ring_peek_one(struct ifmp_ring *r, int cidx, int offset)
28964c7070dbSScott Long {
28974c7070dbSScott Long 
28984c7070dbSScott Long 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (r->size-1)]));
28994c7070dbSScott Long }
29004c7070dbSScott Long 
29014c7070dbSScott Long static void
29024c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget)
29034c7070dbSScott Long {
29044c7070dbSScott Long 
29054c7070dbSScott Long 	ifmp_ring_check_drainage(txq->ift_br[0], budget);
29064c7070dbSScott Long }
29074c7070dbSScott Long 
29084c7070dbSScott Long static uint32_t
29094c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r)
29104c7070dbSScott Long {
29114c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
29124c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
29134c7070dbSScott Long 
29141248952aSSean Bruno 	return ((TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) ||
29154c7070dbSScott Long 		ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, txq->ift_cidx_processed, false));
29164c7070dbSScott Long }
29174c7070dbSScott Long 
29184c7070dbSScott Long static uint32_t
29194c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
29204c7070dbSScott Long {
29214c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
29224c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
29234c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
29244c7070dbSScott Long 	struct mbuf **mp, *m;
29254c7070dbSScott Long 	int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail, err, in_use_prev, desc_used;
29264c7070dbSScott Long 
29274c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
29284c7070dbSScott Long 			    !LINK_ACTIVE(ctx))) {
29294c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_notready);
29304c7070dbSScott Long 		return (0);
29314c7070dbSScott Long 	}
29324c7070dbSScott Long 
29334c7070dbSScott Long 	avail = IDXDIFF(pidx, cidx, r->size);
29344c7070dbSScott Long 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
29354c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_flushing);
29364c7070dbSScott Long 		for (i = 0; i < avail; i++) {
293723ac9029SStephen Hurd 			m_free(r->items[(cidx + i) & (r->size-1)]);
29384c7070dbSScott Long 			r->items[(cidx + i) & (r->size-1)] = NULL;
29394c7070dbSScott Long 		}
29404c7070dbSScott Long 		return (avail);
29414c7070dbSScott Long 	}
29424c7070dbSScott Long 	iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
29434c7070dbSScott Long 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
29444c7070dbSScott Long 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
29454c7070dbSScott Long 		CALLOUT_LOCK(txq);
29464c7070dbSScott Long 		callout_stop(&txq->ift_timer);
29474c7070dbSScott Long 		callout_stop(&txq->ift_db_check);
29484c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
29494c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_oactive);
29504c7070dbSScott Long 		return (0);
29514c7070dbSScott Long 	}
29524c7070dbSScott Long 	consumed = mcast_sent = bytes_sent = pkt_sent = 0;
29534c7070dbSScott Long 	count = MIN(avail, TX_BATCH_SIZE);
2954da69b8f9SSean Bruno #ifdef INVARIANTS
2955da69b8f9SSean Bruno 	if (iflib_verbose_debug)
2956da69b8f9SSean Bruno 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
2957da69b8f9SSean Bruno 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
2958da69b8f9SSean Bruno #endif
29594c7070dbSScott Long 
29604c7070dbSScott Long 	for (desc_used = i = 0; i < count && TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2; i++) {
29614c7070dbSScott Long 		mp = _ring_peek_one(r, cidx, i);
2962da69b8f9SSean Bruno 		MPASS(mp != NULL && *mp != NULL);
29634c7070dbSScott Long 		in_use_prev = txq->ift_in_use;
2964da69b8f9SSean Bruno 		if ((err = iflib_encap(txq, mp)) == ENOBUFS) {
29654c7070dbSScott Long 			DBG_COUNTER_INC(txq_drain_encapfail);
2966da69b8f9SSean Bruno 			/* no room - bail out */
29674c7070dbSScott Long 			break;
29684c7070dbSScott Long 		}
29694c7070dbSScott Long 		consumed++;
2970da69b8f9SSean Bruno 		if (err) {
2971da69b8f9SSean Bruno 			DBG_COUNTER_INC(txq_drain_encapfail);
2972da69b8f9SSean Bruno 			/* we can't send this packet - skip it */
29734c7070dbSScott Long 			continue;
2974da69b8f9SSean Bruno 		}
29754c7070dbSScott Long 		pkt_sent++;
29764c7070dbSScott Long 		m = *mp;
29774c7070dbSScott Long 		DBG_COUNTER_INC(tx_sent);
29784c7070dbSScott Long 		bytes_sent += m->m_pkthdr.len;
29794c7070dbSScott Long 		if (m->m_flags & M_MCAST)
29804c7070dbSScott Long 			mcast_sent++;
29814c7070dbSScott Long 
29824c7070dbSScott Long 		txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
29834c7070dbSScott Long 		desc_used += (txq->ift_in_use - in_use_prev);
29844c7070dbSScott Long 		iflib_txd_db_check(ctx, txq, FALSE);
29854c7070dbSScott Long 		ETHER_BPF_MTAP(ifp, m);
29864c7070dbSScott Long 		if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
29874c7070dbSScott Long 			break;
29884c7070dbSScott Long 
29891248952aSSean Bruno 		if (desc_used >= TXQ_MAX_DB_CONSUMED(txq->ift_size))
29904c7070dbSScott Long 			break;
29914c7070dbSScott Long 	}
29924c7070dbSScott Long 
29934c7070dbSScott Long 	if ((iflib_min_tx_latency || iflib_txq_min_occupancy(txq)) && txq->ift_db_pending)
29944c7070dbSScott Long 		iflib_txd_db_check(ctx, txq, TRUE);
29951248952aSSean Bruno 	else if ((txq->ift_db_pending || TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx) + 2) &&
29964c7070dbSScott Long 		 (callout_pending(&txq->ift_db_check) == 0)) {
29974c7070dbSScott Long 		txq->ift_db_pending_queued = txq->ift_db_pending;
29984c7070dbSScott Long 		callout_reset_on(&txq->ift_db_check, 1, iflib_txd_deferred_db_check,
29994c7070dbSScott Long 				 txq, txq->ift_db_check.c_cpu);
30004c7070dbSScott Long 	}
30014c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
30024c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
30034c7070dbSScott Long 	if (mcast_sent)
30044c7070dbSScott Long 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3005da69b8f9SSean Bruno #ifdef INVARIANTS
3006da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3007da69b8f9SSean Bruno 		printf("consumed=%d\n", consumed);
3008da69b8f9SSean Bruno #endif
30094c7070dbSScott Long 	return (consumed);
30104c7070dbSScott Long }
30114c7070dbSScott Long 
3012da69b8f9SSean Bruno static uint32_t
3013da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r)
3014da69b8f9SSean Bruno {
3015da69b8f9SSean Bruno 	return (1);
3016da69b8f9SSean Bruno }
3017da69b8f9SSean Bruno 
3018da69b8f9SSean Bruno static uint32_t
3019da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3020da69b8f9SSean Bruno {
3021da69b8f9SSean Bruno 	int i, avail;
3022da69b8f9SSean Bruno 	struct mbuf **mp;
3023da69b8f9SSean Bruno 	iflib_txq_t txq;
3024da69b8f9SSean Bruno 
3025da69b8f9SSean Bruno 	txq = r->cookie;
3026da69b8f9SSean Bruno 
3027da69b8f9SSean Bruno 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3028da69b8f9SSean Bruno 	CALLOUT_LOCK(txq);
3029da69b8f9SSean Bruno 	callout_stop(&txq->ift_timer);
3030da69b8f9SSean Bruno 	callout_stop(&txq->ift_db_check);
3031da69b8f9SSean Bruno 	CALLOUT_UNLOCK(txq);
3032da69b8f9SSean Bruno 
3033da69b8f9SSean Bruno 	avail = IDXDIFF(pidx, cidx, r->size);
3034da69b8f9SSean Bruno 	for (i = 0; i < avail; i++) {
3035da69b8f9SSean Bruno 		mp = _ring_peek_one(r, cidx, i);
3036da69b8f9SSean Bruno 		m_freem(*mp);
3037da69b8f9SSean Bruno 	}
3038da69b8f9SSean Bruno 	MPASS(ifmp_ring_is_stalled(r) == 0);
3039da69b8f9SSean Bruno 	return (avail);
3040da69b8f9SSean Bruno }
3041da69b8f9SSean Bruno 
3042da69b8f9SSean Bruno static void
3043da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq)
3044da69b8f9SSean Bruno {
3045da69b8f9SSean Bruno 	struct ifmp_ring *r;
3046da69b8f9SSean Bruno 
3047da69b8f9SSean Bruno 	r = txq->ift_br[0];
3048da69b8f9SSean Bruno 	r->drain = iflib_txq_drain_free;
3049da69b8f9SSean Bruno 	r->can_drain = iflib_txq_drain_always;
3050da69b8f9SSean Bruno 
3051da69b8f9SSean Bruno 	ifmp_ring_check_drainage(r, r->size);
3052da69b8f9SSean Bruno 
3053da69b8f9SSean Bruno 	r->drain = iflib_txq_drain;
3054da69b8f9SSean Bruno 	r->can_drain = iflib_txq_can_drain;
3055da69b8f9SSean Bruno }
3056da69b8f9SSean Bruno 
30574c7070dbSScott Long static void
305823ac9029SStephen Hurd _task_fn_tx(void *context)
30594c7070dbSScott Long {
30604c7070dbSScott Long 	iflib_txq_t txq = context;
30614c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
30624c7070dbSScott Long 
30631248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
30641248952aSSean Bruno 	txq->ift_cpu_exec_count[curcpu]++;
30651248952aSSean Bruno #endif
30664c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
30674c7070dbSScott Long 		return;
30684c7070dbSScott Long 	ifmp_ring_check_drainage(txq->ift_br[0], TX_BATCH_SIZE);
30694c7070dbSScott Long }
30704c7070dbSScott Long 
30714c7070dbSScott Long static void
307223ac9029SStephen Hurd _task_fn_rx(void *context)
30734c7070dbSScott Long {
30744c7070dbSScott Long 	iflib_rxq_t rxq = context;
30754c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
30764c7070dbSScott Long 	bool more;
307723ac9029SStephen Hurd 	int rc;
30784c7070dbSScott Long 
30791248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
30801248952aSSean Bruno 	rxq->ifr_cpu_exec_count[curcpu]++;
30811248952aSSean Bruno #endif
30824c7070dbSScott Long 	DBG_COUNTER_INC(task_fn_rxs);
30834c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
30844c7070dbSScott Long 		return;
30854c7070dbSScott Long 
30864c7070dbSScott Long 	if ((more = iflib_rxeof(rxq, 16 /* XXX */)) == false) {
30874c7070dbSScott Long 		if (ctx->ifc_flags & IFC_LEGACY)
30884c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
30894c7070dbSScott Long 		else {
30904c7070dbSScott Long 			DBG_COUNTER_INC(rx_intr_enables);
309123ac9029SStephen Hurd 			rc = IFDI_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
309223ac9029SStephen Hurd 			KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
30934c7070dbSScott Long 		}
30944c7070dbSScott Long 	}
30954c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
30964c7070dbSScott Long 		return;
30974c7070dbSScott Long 	if (more)
30984c7070dbSScott Long 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
30994c7070dbSScott Long }
31004c7070dbSScott Long 
31014c7070dbSScott Long static void
310223ac9029SStephen Hurd _task_fn_admin(void *context)
31034c7070dbSScott Long {
31044c7070dbSScott Long 	if_ctx_t ctx = context;
31054c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
31064c7070dbSScott Long 	iflib_txq_t txq;
31074c7070dbSScott Long 	int i;
31084c7070dbSScott Long 
31094c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
31104c7070dbSScott Long 		return;
31114c7070dbSScott Long 
31124c7070dbSScott Long 	CTX_LOCK(ctx);
31134c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
31144c7070dbSScott Long 		CALLOUT_LOCK(txq);
31154c7070dbSScott Long 		callout_stop(&txq->ift_timer);
31164c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
31174c7070dbSScott Long 	}
31184c7070dbSScott Long 	IFDI_UPDATE_ADMIN_STATUS(ctx);
31194c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
31204c7070dbSScott Long 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
31214c7070dbSScott Long 	IFDI_LINK_INTR_ENABLE(ctx);
31224c7070dbSScott Long 	CTX_UNLOCK(ctx);
31234c7070dbSScott Long 
31244c7070dbSScott Long 	if (LINK_ACTIVE(ctx) == 0)
31254c7070dbSScott Long 		return;
31264c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
31274c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
31284c7070dbSScott Long }
31294c7070dbSScott Long 
31304c7070dbSScott Long 
31314c7070dbSScott Long static void
313223ac9029SStephen Hurd _task_fn_iov(void *context)
31334c7070dbSScott Long {
31344c7070dbSScott Long 	if_ctx_t ctx = context;
31354c7070dbSScott Long 
31364c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
31374c7070dbSScott Long 		return;
31384c7070dbSScott Long 
31394c7070dbSScott Long 	CTX_LOCK(ctx);
31404c7070dbSScott Long 	IFDI_VFLR_HANDLE(ctx);
31414c7070dbSScott Long 	CTX_UNLOCK(ctx);
31424c7070dbSScott Long }
31434c7070dbSScott Long 
31444c7070dbSScott Long static int
31454c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
31464c7070dbSScott Long {
31474c7070dbSScott Long 	int err;
31484c7070dbSScott Long 	if_int_delay_info_t info;
31494c7070dbSScott Long 	if_ctx_t ctx;
31504c7070dbSScott Long 
31514c7070dbSScott Long 	info = (if_int_delay_info_t)arg1;
31524c7070dbSScott Long 	ctx = info->iidi_ctx;
31534c7070dbSScott Long 	info->iidi_req = req;
31544c7070dbSScott Long 	info->iidi_oidp = oidp;
31554c7070dbSScott Long 	CTX_LOCK(ctx);
31564c7070dbSScott Long 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
31574c7070dbSScott Long 	CTX_UNLOCK(ctx);
31584c7070dbSScott Long 	return (err);
31594c7070dbSScott Long }
31604c7070dbSScott Long 
31614c7070dbSScott Long /*********************************************************************
31624c7070dbSScott Long  *
31634c7070dbSScott Long  *  IFNET FUNCTIONS
31644c7070dbSScott Long  *
31654c7070dbSScott Long  **********************************************************************/
31664c7070dbSScott Long 
31674c7070dbSScott Long static void
31684c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx)
31694c7070dbSScott Long {
31704c7070dbSScott Long 	iflib_stop(ctx);
31714c7070dbSScott Long 	iflib_init_locked(ctx);
31724c7070dbSScott Long }
31734c7070dbSScott Long 
31744c7070dbSScott Long 
31754c7070dbSScott Long static void
31764c7070dbSScott Long iflib_if_init(void *arg)
31774c7070dbSScott Long {
31784c7070dbSScott Long 	if_ctx_t ctx = arg;
31794c7070dbSScott Long 
31804c7070dbSScott Long 	CTX_LOCK(ctx);
31814c7070dbSScott Long 	iflib_if_init_locked(ctx);
31824c7070dbSScott Long 	CTX_UNLOCK(ctx);
31834c7070dbSScott Long }
31844c7070dbSScott Long 
31854c7070dbSScott Long static int
31864c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m)
31874c7070dbSScott Long {
31884c7070dbSScott Long 	if_ctx_t	ctx = if_getsoftc(ifp);
31894c7070dbSScott Long 
31904c7070dbSScott Long 	iflib_txq_t txq;
319123ac9029SStephen Hurd 	int err, qidx;
31924c7070dbSScott Long 
31934c7070dbSScott Long 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
31944c7070dbSScott Long 		DBG_COUNTER_INC(tx_frees);
31954c7070dbSScott Long 		m_freem(m);
3196da69b8f9SSean Bruno 		return (ENOBUFS);
31974c7070dbSScott Long 	}
31984c7070dbSScott Long 
319923ac9029SStephen Hurd 	MPASS(m->m_nextpkt == NULL);
32004c7070dbSScott Long 	qidx = 0;
32014c7070dbSScott Long 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
32024c7070dbSScott Long 		qidx = QIDX(ctx, m);
32034c7070dbSScott Long 	/*
32044c7070dbSScott Long 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
32054c7070dbSScott Long 	 */
32064c7070dbSScott Long 	txq = &ctx->ifc_txqs[qidx];
32074c7070dbSScott Long 
32084c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
32094c7070dbSScott Long 	if (txq->ift_closed) {
32104c7070dbSScott Long 		while (m != NULL) {
32114c7070dbSScott Long 			next = m->m_nextpkt;
32124c7070dbSScott Long 			m->m_nextpkt = NULL;
32134c7070dbSScott Long 			m_freem(m);
32144c7070dbSScott Long 			m = next;
32154c7070dbSScott Long 		}
32164c7070dbSScott Long 		return (ENOBUFS);
32174c7070dbSScott Long 	}
32184c7070dbSScott Long #endif
321923ac9029SStephen Hurd #ifdef notyet
32204c7070dbSScott Long 	qidx = count = 0;
32214c7070dbSScott Long 	mp = marr;
32224c7070dbSScott Long 	next = m;
32234c7070dbSScott Long 	do {
32244c7070dbSScott Long 		count++;
32254c7070dbSScott Long 		next = next->m_nextpkt;
32264c7070dbSScott Long 	} while (next != NULL);
32274c7070dbSScott Long 
322816fb86abSConrad Meyer 	if (count > nitems(marr))
32294c7070dbSScott Long 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
32304c7070dbSScott Long 			/* XXX check nextpkt */
32314c7070dbSScott Long 			m_freem(m);
32324c7070dbSScott Long 			/* XXX simplify for now */
32334c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
32344c7070dbSScott Long 			return (ENOBUFS);
32354c7070dbSScott Long 		}
32364c7070dbSScott Long 	for (next = m, i = 0; next != NULL; i++) {
32374c7070dbSScott Long 		mp[i] = next;
32384c7070dbSScott Long 		next = next->m_nextpkt;
32394c7070dbSScott Long 		mp[i]->m_nextpkt = NULL;
32404c7070dbSScott Long 	}
324123ac9029SStephen Hurd #endif
32424c7070dbSScott Long 	DBG_COUNTER_INC(tx_seen);
324323ac9029SStephen Hurd 	err = ifmp_ring_enqueue(txq->ift_br[0], (void **)&m, 1, TX_BATCH_SIZE);
32444c7070dbSScott Long 
32454c7070dbSScott Long 	if (err) {
324623ac9029SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
32474c7070dbSScott Long 		/* support forthcoming later */
32484c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
32494c7070dbSScott Long 		txq->ift_closed = TRUE;
32504c7070dbSScott Long #endif
32514c7070dbSScott Long 		ifmp_ring_check_drainage(txq->ift_br[0], TX_BATCH_SIZE);
325223ac9029SStephen Hurd 		m_freem(m);
325323ac9029SStephen Hurd 	} else if (TXQ_AVAIL(txq) < (txq->ift_size >> 1)) {
325423ac9029SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
32554c7070dbSScott Long 	}
32564c7070dbSScott Long 
32574c7070dbSScott Long 	return (err);
32584c7070dbSScott Long }
32594c7070dbSScott Long 
32604c7070dbSScott Long static void
32614c7070dbSScott Long iflib_if_qflush(if_t ifp)
32624c7070dbSScott Long {
32634c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
32644c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
32654c7070dbSScott Long 	int i;
32664c7070dbSScott Long 
32674c7070dbSScott Long 	CTX_LOCK(ctx);
32684c7070dbSScott Long 	ctx->ifc_flags |= IFC_QFLUSH;
32694c7070dbSScott Long 	CTX_UNLOCK(ctx);
32704c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
32714c7070dbSScott Long 		while (!(ifmp_ring_is_idle(txq->ift_br[0]) || ifmp_ring_is_stalled(txq->ift_br[0])))
32724c7070dbSScott Long 			iflib_txq_check_drain(txq, 0);
32734c7070dbSScott Long 	CTX_LOCK(ctx);
32744c7070dbSScott Long 	ctx->ifc_flags &= ~IFC_QFLUSH;
32754c7070dbSScott Long 	CTX_UNLOCK(ctx);
32764c7070dbSScott Long 
32774c7070dbSScott Long 	if_qflush(ifp);
32784c7070dbSScott Long }
32794c7070dbSScott Long 
32804c7070dbSScott Long 
328123ac9029SStephen Hurd #define IFCAP_FLAGS (IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
32824c7070dbSScott Long 		     IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTAGGING |	\
32834c7070dbSScott Long 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO)
32844c7070dbSScott Long 
32854c7070dbSScott Long static int
32864c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
32874c7070dbSScott Long {
32884c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
32894c7070dbSScott Long 	struct ifreq	*ifr = (struct ifreq *)data;
32904c7070dbSScott Long #if defined(INET) || defined(INET6)
32914c7070dbSScott Long 	struct ifaddr	*ifa = (struct ifaddr *)data;
32924c7070dbSScott Long #endif
32934c7070dbSScott Long 	bool		avoid_reset = FALSE;
32944c7070dbSScott Long 	int		err = 0, reinit = 0, bits;
32954c7070dbSScott Long 
32964c7070dbSScott Long 	switch (command) {
32974c7070dbSScott Long 	case SIOCSIFADDR:
32984c7070dbSScott Long #ifdef INET
32994c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET)
33004c7070dbSScott Long 			avoid_reset = TRUE;
33014c7070dbSScott Long #endif
33024c7070dbSScott Long #ifdef INET6
33034c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET6)
33044c7070dbSScott Long 			avoid_reset = TRUE;
33054c7070dbSScott Long #endif
33064c7070dbSScott Long 		/*
33074c7070dbSScott Long 		** Calling init results in link renegotiation,
33084c7070dbSScott Long 		** so we avoid doing it when possible.
33094c7070dbSScott Long 		*/
33104c7070dbSScott Long 		if (avoid_reset) {
33114c7070dbSScott Long 			if_setflagbits(ifp, IFF_UP,0);
33124c7070dbSScott Long 			if (!(if_getdrvflags(ifp)& IFF_DRV_RUNNING))
33134c7070dbSScott Long 				reinit = 1;
33144c7070dbSScott Long #ifdef INET
33154c7070dbSScott Long 			if (!(if_getflags(ifp) & IFF_NOARP))
33164c7070dbSScott Long 				arp_ifinit(ifp, ifa);
33174c7070dbSScott Long #endif
33184c7070dbSScott Long 		} else
33194c7070dbSScott Long 			err = ether_ioctl(ifp, command, data);
33204c7070dbSScott Long 		break;
33214c7070dbSScott Long 	case SIOCSIFMTU:
33224c7070dbSScott Long 		CTX_LOCK(ctx);
33234c7070dbSScott Long 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
33244c7070dbSScott Long 			CTX_UNLOCK(ctx);
33254c7070dbSScott Long 			break;
33264c7070dbSScott Long 		}
33274c7070dbSScott Long 		bits = if_getdrvflags(ifp);
33284c7070dbSScott Long 		/* stop the driver and free any clusters before proceeding */
33294c7070dbSScott Long 		iflib_stop(ctx);
33304c7070dbSScott Long 
33314c7070dbSScott Long 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
33324c7070dbSScott Long 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
33334c7070dbSScott Long 				ctx->ifc_flags |= IFC_MULTISEG;
33344c7070dbSScott Long 			else
33354c7070dbSScott Long 				ctx->ifc_flags &= ~IFC_MULTISEG;
33364c7070dbSScott Long 			err = if_setmtu(ifp, ifr->ifr_mtu);
33374c7070dbSScott Long 		}
33384c7070dbSScott Long 		iflib_init_locked(ctx);
33394c7070dbSScott Long 		if_setdrvflags(ifp, bits);
33404c7070dbSScott Long 		CTX_UNLOCK(ctx);
33414c7070dbSScott Long 		break;
33424c7070dbSScott Long 	case SIOCSIFFLAGS:
33434c7070dbSScott Long 		CTX_LOCK(ctx);
33444c7070dbSScott Long 		if (if_getflags(ifp) & IFF_UP) {
33454c7070dbSScott Long 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
33464c7070dbSScott Long 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
33474c7070dbSScott Long 				    (IFF_PROMISC | IFF_ALLMULTI)) {
33484c7070dbSScott Long 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
33494c7070dbSScott Long 				}
33504c7070dbSScott Long 			} else
33514c7070dbSScott Long 				reinit = 1;
33524c7070dbSScott Long 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
33534c7070dbSScott Long 			iflib_stop(ctx);
33544c7070dbSScott Long 		}
33554c7070dbSScott Long 		ctx->ifc_if_flags = if_getflags(ifp);
33564c7070dbSScott Long 		CTX_UNLOCK(ctx);
33574c7070dbSScott Long 		break;
33584c7070dbSScott Long 
33594c7070dbSScott Long 		break;
33604c7070dbSScott Long 	case SIOCADDMULTI:
33614c7070dbSScott Long 	case SIOCDELMULTI:
33624c7070dbSScott Long 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
33634c7070dbSScott Long 			CTX_LOCK(ctx);
33644c7070dbSScott Long 			IFDI_INTR_DISABLE(ctx);
33654c7070dbSScott Long 			IFDI_MULTI_SET(ctx);
33664c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
33674c7070dbSScott Long 			CTX_UNLOCK(ctx);
33684c7070dbSScott Long 		}
33694c7070dbSScott Long 		break;
33704c7070dbSScott Long 	case SIOCSIFMEDIA:
33714c7070dbSScott Long 		CTX_LOCK(ctx);
33724c7070dbSScott Long 		IFDI_MEDIA_SET(ctx);
33734c7070dbSScott Long 		CTX_UNLOCK(ctx);
33744c7070dbSScott Long 		/* falls thru */
33754c7070dbSScott Long 	case SIOCGIFMEDIA:
33764c7070dbSScott Long 		err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command);
33774c7070dbSScott Long 		break;
33784c7070dbSScott Long 	case SIOCGI2C:
33794c7070dbSScott Long 	{
33804c7070dbSScott Long 		struct ifi2creq i2c;
33814c7070dbSScott Long 
33824c7070dbSScott Long 		err = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
33834c7070dbSScott Long 		if (err != 0)
33844c7070dbSScott Long 			break;
33854c7070dbSScott Long 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
33864c7070dbSScott Long 			err = EINVAL;
33874c7070dbSScott Long 			break;
33884c7070dbSScott Long 		}
33894c7070dbSScott Long 		if (i2c.len > sizeof(i2c.data)) {
33904c7070dbSScott Long 			err = EINVAL;
33914c7070dbSScott Long 			break;
33924c7070dbSScott Long 		}
33934c7070dbSScott Long 
33944c7070dbSScott Long 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
33954c7070dbSScott Long 			err = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
33964c7070dbSScott Long 		break;
33974c7070dbSScott Long 	}
33984c7070dbSScott Long 	case SIOCSIFCAP:
33994c7070dbSScott Long 	{
34004c7070dbSScott Long 		int mask, setmask;
34014c7070dbSScott Long 
34024c7070dbSScott Long 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
34034c7070dbSScott Long 		setmask = 0;
34044c7070dbSScott Long #ifdef TCP_OFFLOAD
34054c7070dbSScott Long 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
34064c7070dbSScott Long #endif
34074c7070dbSScott Long 		setmask |= (mask & IFCAP_FLAGS);
34084c7070dbSScott Long 
34098b2a1db9SSean Bruno 		if (setmask  & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
34108b2a1db9SSean Bruno 			setmask |= (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
34114c7070dbSScott Long 		if ((mask & IFCAP_WOL) &&
34124c7070dbSScott Long 		    (if_getcapabilities(ifp) & IFCAP_WOL) != 0)
34134c7070dbSScott Long 			setmask |= (mask & (IFCAP_WOL_MCAST|IFCAP_WOL_MAGIC));
34144c7070dbSScott Long 		if_vlancap(ifp);
34154c7070dbSScott Long 		/*
34164c7070dbSScott Long 		 * want to ensure that traffic has stopped before we change any of the flags
34174c7070dbSScott Long 		 */
34184c7070dbSScott Long 		if (setmask) {
34194c7070dbSScott Long 			CTX_LOCK(ctx);
34204c7070dbSScott Long 			bits = if_getdrvflags(ifp);
34218b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
34224c7070dbSScott Long 				iflib_stop(ctx);
34234c7070dbSScott Long 			if_togglecapenable(ifp, setmask);
34248b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
34254c7070dbSScott Long 				iflib_init_locked(ctx);
34264c7070dbSScott Long 			if_setdrvflags(ifp, bits);
34274c7070dbSScott Long 			CTX_UNLOCK(ctx);
34284c7070dbSScott Long 		}
34294c7070dbSScott Long 		break;
34304c7070dbSScott Long 	    }
34314c7070dbSScott Long 	case SIOCGPRIVATE_0:
34324c7070dbSScott Long 	case SIOCSDRVSPEC:
34334c7070dbSScott Long 	case SIOCGDRVSPEC:
34344c7070dbSScott Long 		CTX_LOCK(ctx);
34354c7070dbSScott Long 		err = IFDI_PRIV_IOCTL(ctx, command, data);
34364c7070dbSScott Long 		CTX_UNLOCK(ctx);
34374c7070dbSScott Long 		break;
34384c7070dbSScott Long 	default:
34394c7070dbSScott Long 		err = ether_ioctl(ifp, command, data);
34404c7070dbSScott Long 		break;
34414c7070dbSScott Long 	}
34424c7070dbSScott Long 	if (reinit)
34434c7070dbSScott Long 		iflib_if_init(ctx);
34444c7070dbSScott Long 	return (err);
34454c7070dbSScott Long }
34464c7070dbSScott Long 
34474c7070dbSScott Long static uint64_t
34484c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt)
34494c7070dbSScott Long {
34504c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
34514c7070dbSScott Long 
34524c7070dbSScott Long 	return (IFDI_GET_COUNTER(ctx, cnt));
34534c7070dbSScott Long }
34544c7070dbSScott Long 
34554c7070dbSScott Long /*********************************************************************
34564c7070dbSScott Long  *
34574c7070dbSScott Long  *  OTHER FUNCTIONS EXPORTED TO THE STACK
34584c7070dbSScott Long  *
34594c7070dbSScott Long  **********************************************************************/
34604c7070dbSScott Long 
34614c7070dbSScott Long static void
34624c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
34634c7070dbSScott Long {
34644c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
34654c7070dbSScott Long 
34664c7070dbSScott Long 	if ((void *)ctx != arg)
34674c7070dbSScott Long 		return;
34684c7070dbSScott Long 
34694c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
34704c7070dbSScott Long 		return;
34714c7070dbSScott Long 
34724c7070dbSScott Long 	CTX_LOCK(ctx);
34734c7070dbSScott Long 	IFDI_VLAN_REGISTER(ctx, vtag);
34744c7070dbSScott Long 	/* Re-init to load the changes */
34754c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
34764c7070dbSScott Long 		iflib_init_locked(ctx);
34774c7070dbSScott Long 	CTX_UNLOCK(ctx);
34784c7070dbSScott Long }
34794c7070dbSScott Long 
34804c7070dbSScott Long static void
34814c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
34824c7070dbSScott Long {
34834c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
34844c7070dbSScott Long 
34854c7070dbSScott Long 	if ((void *)ctx != arg)
34864c7070dbSScott Long 		return;
34874c7070dbSScott Long 
34884c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
34894c7070dbSScott Long 		return;
34904c7070dbSScott Long 
34914c7070dbSScott Long 	CTX_LOCK(ctx);
34924c7070dbSScott Long 	IFDI_VLAN_UNREGISTER(ctx, vtag);
34934c7070dbSScott Long 	/* Re-init to load the changes */
34944c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
34954c7070dbSScott Long 		iflib_init_locked(ctx);
34964c7070dbSScott Long 	CTX_UNLOCK(ctx);
34974c7070dbSScott Long }
34984c7070dbSScott Long 
34994c7070dbSScott Long static void
35004c7070dbSScott Long iflib_led_func(void *arg, int onoff)
35014c7070dbSScott Long {
35024c7070dbSScott Long 	if_ctx_t ctx = arg;
35034c7070dbSScott Long 
35044c7070dbSScott Long 	CTX_LOCK(ctx);
35054c7070dbSScott Long 	IFDI_LED_FUNC(ctx, onoff);
35064c7070dbSScott Long 	CTX_UNLOCK(ctx);
35074c7070dbSScott Long }
35084c7070dbSScott Long 
35094c7070dbSScott Long /*********************************************************************
35104c7070dbSScott Long  *
35114c7070dbSScott Long  *  BUS FUNCTION DEFINITIONS
35124c7070dbSScott Long  *
35134c7070dbSScott Long  **********************************************************************/
35144c7070dbSScott Long 
35154c7070dbSScott Long int
35164c7070dbSScott Long iflib_device_probe(device_t dev)
35174c7070dbSScott Long {
35184c7070dbSScott Long 	pci_vendor_info_t *ent;
35194c7070dbSScott Long 
35204c7070dbSScott Long 	uint16_t	pci_vendor_id, pci_device_id;
35214c7070dbSScott Long 	uint16_t	pci_subvendor_id, pci_subdevice_id;
35224c7070dbSScott Long 	uint16_t	pci_rev_id;
35234c7070dbSScott Long 	if_shared_ctx_t sctx;
35244c7070dbSScott Long 
35254c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
35264c7070dbSScott Long 		return (ENOTSUP);
35274c7070dbSScott Long 
35284c7070dbSScott Long 	pci_vendor_id = pci_get_vendor(dev);
35294c7070dbSScott Long 	pci_device_id = pci_get_device(dev);
35304c7070dbSScott Long 	pci_subvendor_id = pci_get_subvendor(dev);
35314c7070dbSScott Long 	pci_subdevice_id = pci_get_subdevice(dev);
35324c7070dbSScott Long 	pci_rev_id = pci_get_revid(dev);
35334c7070dbSScott Long 	if (sctx->isc_parse_devinfo != NULL)
35344c7070dbSScott Long 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
35354c7070dbSScott Long 
35364c7070dbSScott Long 	ent = sctx->isc_vendor_info;
35374c7070dbSScott Long 	while (ent->pvi_vendor_id != 0) {
35384c7070dbSScott Long 		if (pci_vendor_id != ent->pvi_vendor_id) {
35394c7070dbSScott Long 			ent++;
35404c7070dbSScott Long 			continue;
35414c7070dbSScott Long 		}
35424c7070dbSScott Long 		if ((pci_device_id == ent->pvi_device_id) &&
35434c7070dbSScott Long 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
35444c7070dbSScott Long 		     (ent->pvi_subvendor_id == 0)) &&
35454c7070dbSScott Long 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
35464c7070dbSScott Long 		     (ent->pvi_subdevice_id == 0)) &&
35474c7070dbSScott Long 		    ((pci_rev_id == ent->pvi_rev_id) ||
35484c7070dbSScott Long 		     (ent->pvi_rev_id == 0))) {
35494c7070dbSScott Long 
35504c7070dbSScott Long 			device_set_desc_copy(dev, ent->pvi_name);
35514c7070dbSScott Long 			/* this needs to be changed to zero if the bus probing code
35524c7070dbSScott Long 			 * ever stops re-probing on best match because the sctx
35534c7070dbSScott Long 			 * may have its values over written by register calls
35544c7070dbSScott Long 			 * in subsequent probes
35554c7070dbSScott Long 			 */
35564c7070dbSScott Long 			return (BUS_PROBE_DEFAULT);
35574c7070dbSScott Long 		}
35584c7070dbSScott Long 		ent++;
35594c7070dbSScott Long 	}
35604c7070dbSScott Long 	return (ENXIO);
35614c7070dbSScott Long }
35624c7070dbSScott Long 
35634c7070dbSScott Long int
35644c7070dbSScott Long iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
35654c7070dbSScott Long {
35664c7070dbSScott Long 	int err, rid, msix, msix_bar;
35674c7070dbSScott Long 	if_ctx_t ctx;
35684c7070dbSScott Long 	if_t ifp;
35694c7070dbSScott Long 	if_softc_ctx_t scctx;
357023ac9029SStephen Hurd 	int i;
357123ac9029SStephen Hurd 	uint16_t main_txq;
357223ac9029SStephen Hurd 	uint16_t main_rxq;
35734c7070dbSScott Long 
35744c7070dbSScott Long 
35754c7070dbSScott Long 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
35764c7070dbSScott Long 
35774c7070dbSScott Long 	if (sc == NULL) {
35784c7070dbSScott Long 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
35794c7070dbSScott Long 		device_set_softc(dev, ctx);
358023ac9029SStephen Hurd 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
35814c7070dbSScott Long 	}
35824c7070dbSScott Long 
35834c7070dbSScott Long 	ctx->ifc_sctx = sctx;
35844c7070dbSScott Long 	ctx->ifc_dev = dev;
35854c7070dbSScott Long 	ctx->ifc_softc = sc;
35864c7070dbSScott Long 
35874c7070dbSScott Long 	if ((err = iflib_register(ctx)) != 0) {
35884c7070dbSScott Long 		device_printf(dev, "iflib_register failed %d\n", err);
35894c7070dbSScott Long 		return (err);
35904c7070dbSScott Long 	}
35914c7070dbSScott Long 	iflib_add_device_sysctl_pre(ctx);
359223ac9029SStephen Hurd 
359323ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
35941248952aSSean Bruno 	ifp = ctx->ifc_ifp;
35951248952aSSean Bruno 
359623ac9029SStephen Hurd 	/*
359723ac9029SStephen Hurd 	 * XXX sanity check that ntxd & nrxd are a power of 2
359823ac9029SStephen Hurd 	 */
359923ac9029SStephen Hurd 	if (ctx->ifc_sysctl_ntxqs != 0)
360023ac9029SStephen Hurd 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
360123ac9029SStephen Hurd 	if (ctx->ifc_sysctl_nrxqs != 0)
360223ac9029SStephen Hurd 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
360323ac9029SStephen Hurd 
360423ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
360523ac9029SStephen Hurd 		if (ctx->ifc_sysctl_ntxds[i] != 0)
360623ac9029SStephen Hurd 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
360723ac9029SStephen Hurd 		else
360823ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
360923ac9029SStephen Hurd 	}
361023ac9029SStephen Hurd 
361123ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
361223ac9029SStephen Hurd 		if (ctx->ifc_sysctl_nrxds[i] != 0)
361323ac9029SStephen Hurd 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
361423ac9029SStephen Hurd 		else
361523ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
361623ac9029SStephen Hurd 	}
361723ac9029SStephen Hurd 
361823ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
361923ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
362023ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
362123ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
362223ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
362323ac9029SStephen Hurd 		}
362423ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
362523ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
362623ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
362723ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
362823ac9029SStephen Hurd 		}
362923ac9029SStephen Hurd 	}
363023ac9029SStephen Hurd 
363123ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
363223ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
363323ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
363423ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
363523ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
363623ac9029SStephen Hurd 		}
363723ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
363823ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
363923ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
364023ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
364123ac9029SStephen Hurd 		}
364223ac9029SStephen Hurd 	}
364323ac9029SStephen Hurd 
36444c7070dbSScott Long 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
36454c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
36464c7070dbSScott Long 		return (err);
36474c7070dbSScott Long 	}
36481248952aSSean Bruno 	_iflib_pre_assert(scctx);
36491248952aSSean Bruno 	ctx->ifc_txrx = *scctx->isc_txrx;
36501248952aSSean Bruno 
36511248952aSSean Bruno #ifdef INVARIANTS
36521248952aSSean Bruno 	MPASS(scctx->isc_capenable);
36531248952aSSean Bruno 	if (scctx->isc_capenable & IFCAP_TXCSUM)
36541248952aSSean Bruno 		MPASS(scctx->isc_tx_csum_flags);
36551248952aSSean Bruno #endif
36561248952aSSean Bruno 
36571248952aSSean Bruno 	if_setcapabilities(ifp, scctx->isc_capenable);
36581248952aSSean Bruno 	if_setcapenable(ifp, scctx->isc_capenable);
36591248952aSSean Bruno 
36601248952aSSean Bruno 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
36611248952aSSean Bruno 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
36621248952aSSean Bruno 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
36631248952aSSean Bruno 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
366423ac9029SStephen Hurd 
36654c7070dbSScott Long #ifdef ACPI_DMAR
36664c7070dbSScott Long 	if (dmar_get_dma_tag(device_get_parent(dev), dev) != NULL)
36674c7070dbSScott Long 		ctx->ifc_flags |= IFC_DMAR;
36684c7070dbSScott Long #endif
36694c7070dbSScott Long 
36704c7070dbSScott Long 	msix_bar = scctx->isc_msix_bar;
36714c7070dbSScott Long 
367223ac9029SStephen Hurd 	if(sctx->isc_flags & IFLIB_HAS_TXCQ)
367323ac9029SStephen Hurd 		main_txq = 1;
367423ac9029SStephen Hurd 	else
367523ac9029SStephen Hurd 		main_txq = 0;
367623ac9029SStephen Hurd 
367723ac9029SStephen Hurd 	if(sctx->isc_flags & IFLIB_HAS_RXCQ)
367823ac9029SStephen Hurd 		main_rxq = 1;
367923ac9029SStephen Hurd 	else
368023ac9029SStephen Hurd 		main_rxq = 0;
368123ac9029SStephen Hurd 
368223ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
368323ac9029SStephen Hurd 	device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
368423ac9029SStephen Hurd 		      scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
368523ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
368623ac9029SStephen Hurd 		if (!powerof2(scctx->isc_nrxd[i])) {
368723ac9029SStephen Hurd 			/* round down instead? */
368823ac9029SStephen Hurd 			device_printf(dev, "# rx descriptors must be a power of 2\n");
368923ac9029SStephen Hurd 			err = EINVAL;
369023ac9029SStephen Hurd 			goto fail;
369123ac9029SStephen Hurd 		}
369223ac9029SStephen Hurd 	}
369323ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
369423ac9029SStephen Hurd 		if (!powerof2(scctx->isc_ntxd[i])) {
369523ac9029SStephen Hurd 			device_printf(dev,
369623ac9029SStephen Hurd 			    "# tx descriptors must be a power of 2");
369723ac9029SStephen Hurd 			err = EINVAL;
369823ac9029SStephen Hurd 			goto fail;
369923ac9029SStephen Hurd 		}
370023ac9029SStephen Hurd 	}
370123ac9029SStephen Hurd 
370223ac9029SStephen Hurd 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
370323ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
370423ac9029SStephen Hurd 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
370523ac9029SStephen Hurd 		    MAX_SINGLE_PACKET_FRACTION);
370623ac9029SStephen Hurd 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
370723ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
370823ac9029SStephen Hurd 		scctx->isc_tx_tso_segments_max = max(1,
370923ac9029SStephen Hurd 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
37104c7070dbSScott Long 
37114c7070dbSScott Long 	/*
37124c7070dbSScott Long 	 * Protect the stack against modern hardware
37134c7070dbSScott Long 	 */
37144c7070dbSScott Long 	if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
37154c7070dbSScott Long 		scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
37164c7070dbSScott Long 
37174c7070dbSScott Long 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
37184c7070dbSScott Long 	ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
37194c7070dbSScott Long 	ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
37204c7070dbSScott Long 	ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
37214c7070dbSScott Long 	if (scctx->isc_rss_table_size == 0)
37224c7070dbSScott Long 		scctx->isc_rss_table_size = 64;
372323ac9029SStephen Hurd 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
3724da69b8f9SSean Bruno 
3725da69b8f9SSean Bruno 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
3726da69b8f9SSean Bruno 	/* XXX format name */
3727da69b8f9SSean Bruno 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
37284c7070dbSScott Long 	/*
37294c7070dbSScott Long 	** Now setup MSI or MSI/X, should
37304c7070dbSScott Long 	** return us the number of supported
37314c7070dbSScott Long 	** vectors. (Will be 1 for MSI)
37324c7070dbSScott Long 	*/
37334c7070dbSScott Long 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
37344c7070dbSScott Long 		msix = scctx->isc_vectors;
37354c7070dbSScott Long 	} else if (scctx->isc_msix_bar != 0)
3736f7ae9a84SSean Bruno 	       /*
3737f7ae9a84SSean Bruno 		* The simple fact that isc_msix_bar is not 0 does not mean we
3738f7ae9a84SSean Bruno 		* we have a good value there that is known to work.
3739f7ae9a84SSean Bruno 		*/
37404c7070dbSScott Long 		msix = iflib_msix_init(ctx);
37414c7070dbSScott Long 	else {
37424c7070dbSScott Long 		scctx->isc_vectors = 1;
37434c7070dbSScott Long 		scctx->isc_ntxqsets = 1;
37444c7070dbSScott Long 		scctx->isc_nrxqsets = 1;
37454c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
37464c7070dbSScott Long 		msix = 0;
37474c7070dbSScott Long 	}
37484c7070dbSScott Long 	/* Get memory for the station queues */
37494c7070dbSScott Long 	if ((err = iflib_queues_alloc(ctx))) {
37504c7070dbSScott Long 		device_printf(dev, "Unable to allocate queue memory\n");
37514c7070dbSScott Long 		goto fail;
37524c7070dbSScott Long 	}
37534c7070dbSScott Long 
37544c7070dbSScott Long 	if ((err = iflib_qset_structures_setup(ctx))) {
37554c7070dbSScott Long 		device_printf(dev, "qset structure setup failed %d\n", err);
37564c7070dbSScott Long 		goto fail_queues;
37574c7070dbSScott Long 	}
375869b7fc3eSSean Bruno 
3759bd84f700SSean Bruno 	/*
3760bd84f700SSean Bruno 	 * Group taskqueues aren't properly set up until SMP is started,
3761bd84f700SSean Bruno 	 * so we disable interrupts until we can handle them post
3762bd84f700SSean Bruno 	 * SI_SUB_SMP.
3763bd84f700SSean Bruno 	 *
3764bd84f700SSean Bruno 	 * XXX: disabling interrupts doesn't actually work, at least for
3765bd84f700SSean Bruno 	 * the non-MSI case.  When they occur before SI_SUB_SMP completes,
3766bd84f700SSean Bruno 	 * we do null handling and depend on this not causing too large an
3767bd84f700SSean Bruno 	 * interrupt storm.
3768bd84f700SSean Bruno 	 */
37691248952aSSean Bruno 	IFDI_INTR_DISABLE(ctx);
37704c7070dbSScott Long 	if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
37714c7070dbSScott Long 		device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
37724c7070dbSScott Long 		goto fail_intr_free;
37734c7070dbSScott Long 	}
37744c7070dbSScott Long 	if (msix <= 1) {
37754c7070dbSScott Long 		rid = 0;
37764c7070dbSScott Long 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
37774c7070dbSScott Long 			MPASS(msix == 1);
37784c7070dbSScott Long 			rid = 1;
37794c7070dbSScott Long 		}
378023ac9029SStephen Hurd 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
37814c7070dbSScott Long 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
37824c7070dbSScott Long 			goto fail_intr_free;
37834c7070dbSScott Long 		}
37844c7070dbSScott Long 	}
37854c7070dbSScott Long 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
37864c7070dbSScott Long 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
37874c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
37884c7070dbSScott Long 		goto fail_detach;
37894c7070dbSScott Long 	}
37904c7070dbSScott Long 	if ((err = iflib_netmap_attach(ctx))) {
37914c7070dbSScott Long 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
37924c7070dbSScott Long 		goto fail_detach;
37934c7070dbSScott Long 	}
37944c7070dbSScott Long 	*ctxp = ctx;
37954c7070dbSScott Long 
379623ac9029SStephen Hurd 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
37974c7070dbSScott Long 	iflib_add_device_sysctl_post(ctx);
37984ecb427aSSean Bruno 	ctx->ifc_flags |= IFC_INIT_DONE;
37994c7070dbSScott Long 	return (0);
38004c7070dbSScott Long fail_detach:
38014c7070dbSScott Long 	ether_ifdetach(ctx->ifc_ifp);
38024c7070dbSScott Long fail_intr_free:
38034c7070dbSScott Long 	if (scctx->isc_intr == IFLIB_INTR_MSIX || scctx->isc_intr == IFLIB_INTR_MSI)
38044c7070dbSScott Long 		pci_release_msi(ctx->ifc_dev);
38054c7070dbSScott Long fail_queues:
38064c7070dbSScott Long 	/* XXX free queues */
38074c7070dbSScott Long fail:
38084c7070dbSScott Long 	IFDI_DETACH(ctx);
38094c7070dbSScott Long 	return (err);
38104c7070dbSScott Long }
38114c7070dbSScott Long 
38124c7070dbSScott Long int
38134c7070dbSScott Long iflib_device_attach(device_t dev)
38144c7070dbSScott Long {
38154c7070dbSScott Long 	if_ctx_t ctx;
38164c7070dbSScott Long 	if_shared_ctx_t sctx;
38174c7070dbSScott Long 
38184c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
38194c7070dbSScott Long 		return (ENOTSUP);
38204c7070dbSScott Long 
38214c7070dbSScott Long 	pci_enable_busmaster(dev);
38224c7070dbSScott Long 
38234c7070dbSScott Long 	return (iflib_device_register(dev, NULL, sctx, &ctx));
38244c7070dbSScott Long }
38254c7070dbSScott Long 
38264c7070dbSScott Long int
38274c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx)
38284c7070dbSScott Long {
38294c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
38304c7070dbSScott Long 	iflib_txq_t txq;
38314c7070dbSScott Long 	iflib_rxq_t rxq;
38324c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
38334c7070dbSScott Long 	int i;
38344c7070dbSScott Long 	struct taskqgroup *tqg;
38354c7070dbSScott Long 
38364c7070dbSScott Long 	/* Make sure VLANS are not using driver */
38374c7070dbSScott Long 	if (if_vlantrunkinuse(ifp)) {
38384c7070dbSScott Long 		device_printf(dev,"Vlan in use, detach first\n");
38394c7070dbSScott Long 		return (EBUSY);
38404c7070dbSScott Long 	}
38414c7070dbSScott Long 
38424c7070dbSScott Long 	CTX_LOCK(ctx);
38434c7070dbSScott Long 	ctx->ifc_in_detach = 1;
38444c7070dbSScott Long 	iflib_stop(ctx);
38454c7070dbSScott Long 	CTX_UNLOCK(ctx);
38464c7070dbSScott Long 
38474c7070dbSScott Long 	/* Unregister VLAN events */
38484c7070dbSScott Long 	if (ctx->ifc_vlan_attach_event != NULL)
38494c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
38504c7070dbSScott Long 	if (ctx->ifc_vlan_detach_event != NULL)
38514c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
38524c7070dbSScott Long 
38534c7070dbSScott Long 	iflib_netmap_detach(ifp);
38544c7070dbSScott Long 	ether_ifdetach(ifp);
38554c7070dbSScott Long 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
38564c7070dbSScott Long 	CTX_LOCK_DESTROY(ctx);
38574c7070dbSScott Long 	if (ctx->ifc_led_dev != NULL)
38584c7070dbSScott Long 		led_destroy(ctx->ifc_led_dev);
38594c7070dbSScott Long 	/* XXX drain any dependent tasks */
38604c7070dbSScott Long 	tqg = qgroup_if_io_tqg;
386123ac9029SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
38624c7070dbSScott Long 		callout_drain(&txq->ift_timer);
38634c7070dbSScott Long 		callout_drain(&txq->ift_db_check);
38644c7070dbSScott Long 		if (txq->ift_task.gt_uniq != NULL)
38654c7070dbSScott Long 			taskqgroup_detach(tqg, &txq->ift_task);
38664c7070dbSScott Long 	}
38674c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
38684c7070dbSScott Long 		if (rxq->ifr_task.gt_uniq != NULL)
38694c7070dbSScott Long 			taskqgroup_detach(tqg, &rxq->ifr_task);
38704c7070dbSScott Long 	}
38714c7070dbSScott Long 	tqg = qgroup_if_config_tqg;
38724c7070dbSScott Long 	if (ctx->ifc_admin_task.gt_uniq != NULL)
38734c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
38744c7070dbSScott Long 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
38754c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
38764c7070dbSScott Long 
38774c7070dbSScott Long 	IFDI_DETACH(ctx);
387823ac9029SStephen Hurd 	device_set_softc(ctx->ifc_dev, NULL);
38794c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
38804c7070dbSScott Long 		pci_release_msi(dev);
38814c7070dbSScott Long 	}
38824c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
38834c7070dbSScott Long 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
38844c7070dbSScott Long 	}
38854c7070dbSScott Long 	if (ctx->ifc_msix_mem != NULL) {
38864c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
38874c7070dbSScott Long 			ctx->ifc_softc_ctx.isc_msix_bar, ctx->ifc_msix_mem);
38884c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
38894c7070dbSScott Long 	}
38904c7070dbSScott Long 
38914c7070dbSScott Long 	bus_generic_detach(dev);
38924c7070dbSScott Long 	if_free(ifp);
38934c7070dbSScott Long 
38944c7070dbSScott Long 	iflib_tx_structures_free(ctx);
38954c7070dbSScott Long 	iflib_rx_structures_free(ctx);
389623ac9029SStephen Hurd 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
389723ac9029SStephen Hurd 		free(ctx->ifc_softc, M_IFLIB);
389823ac9029SStephen Hurd 	free(ctx, M_IFLIB);
38994c7070dbSScott Long 	return (0);
39004c7070dbSScott Long }
39014c7070dbSScott Long 
39024c7070dbSScott Long 
39034c7070dbSScott Long int
39044c7070dbSScott Long iflib_device_detach(device_t dev)
39054c7070dbSScott Long {
39064c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39074c7070dbSScott Long 
39084c7070dbSScott Long 	return (iflib_device_deregister(ctx));
39094c7070dbSScott Long }
39104c7070dbSScott Long 
39114c7070dbSScott Long int
39124c7070dbSScott Long iflib_device_suspend(device_t dev)
39134c7070dbSScott Long {
39144c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39154c7070dbSScott Long 
39164c7070dbSScott Long 	CTX_LOCK(ctx);
39174c7070dbSScott Long 	IFDI_SUSPEND(ctx);
39184c7070dbSScott Long 	CTX_UNLOCK(ctx);
39194c7070dbSScott Long 
39204c7070dbSScott Long 	return bus_generic_suspend(dev);
39214c7070dbSScott Long }
39224c7070dbSScott Long int
39234c7070dbSScott Long iflib_device_shutdown(device_t dev)
39244c7070dbSScott Long {
39254c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39264c7070dbSScott Long 
39274c7070dbSScott Long 	CTX_LOCK(ctx);
39284c7070dbSScott Long 	IFDI_SHUTDOWN(ctx);
39294c7070dbSScott Long 	CTX_UNLOCK(ctx);
39304c7070dbSScott Long 
39314c7070dbSScott Long 	return bus_generic_suspend(dev);
39324c7070dbSScott Long }
39334c7070dbSScott Long 
39344c7070dbSScott Long 
39354c7070dbSScott Long int
39364c7070dbSScott Long iflib_device_resume(device_t dev)
39374c7070dbSScott Long {
39384c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39394c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
39404c7070dbSScott Long 
39414c7070dbSScott Long 	CTX_LOCK(ctx);
39424c7070dbSScott Long 	IFDI_RESUME(ctx);
39434c7070dbSScott Long 	iflib_init_locked(ctx);
39444c7070dbSScott Long 	CTX_UNLOCK(ctx);
39454c7070dbSScott Long 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
39464c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
39474c7070dbSScott Long 
39484c7070dbSScott Long 	return (bus_generic_resume(dev));
39494c7070dbSScott Long }
39504c7070dbSScott Long 
39514c7070dbSScott Long int
39524c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
39534c7070dbSScott Long {
39544c7070dbSScott Long 	int error;
39554c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39564c7070dbSScott Long 
39574c7070dbSScott Long 	CTX_LOCK(ctx);
39584c7070dbSScott Long 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
39594c7070dbSScott Long 	CTX_UNLOCK(ctx);
39604c7070dbSScott Long 
39614c7070dbSScott Long 	return (error);
39624c7070dbSScott Long }
39634c7070dbSScott Long 
39644c7070dbSScott Long void
39654c7070dbSScott Long iflib_device_iov_uninit(device_t dev)
39664c7070dbSScott Long {
39674c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39684c7070dbSScott Long 
39694c7070dbSScott Long 	CTX_LOCK(ctx);
39704c7070dbSScott Long 	IFDI_IOV_UNINIT(ctx);
39714c7070dbSScott Long 	CTX_UNLOCK(ctx);
39724c7070dbSScott Long }
39734c7070dbSScott Long 
39744c7070dbSScott Long int
39754c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
39764c7070dbSScott Long {
39774c7070dbSScott Long 	int error;
39784c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
39794c7070dbSScott Long 
39804c7070dbSScott Long 	CTX_LOCK(ctx);
39814c7070dbSScott Long 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
39824c7070dbSScott Long 	CTX_UNLOCK(ctx);
39834c7070dbSScott Long 
39844c7070dbSScott Long 	return (error);
39854c7070dbSScott Long }
39864c7070dbSScott Long 
39874c7070dbSScott Long /*********************************************************************
39884c7070dbSScott Long  *
39894c7070dbSScott Long  *  MODULE FUNCTION DEFINITIONS
39904c7070dbSScott Long  *
39914c7070dbSScott Long  **********************************************************************/
39924c7070dbSScott Long 
39934c7070dbSScott Long /*
39944c7070dbSScott Long  * - Start a fast taskqueue thread for each core
39954c7070dbSScott Long  * - Start a taskqueue for control operations
39964c7070dbSScott Long  */
39974c7070dbSScott Long static int
39984c7070dbSScott Long iflib_module_init(void)
39994c7070dbSScott Long {
40004c7070dbSScott Long 	return (0);
40014c7070dbSScott Long }
40024c7070dbSScott Long 
40034c7070dbSScott Long static int
40044c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg)
40054c7070dbSScott Long {
40064c7070dbSScott Long 	int err;
40074c7070dbSScott Long 
40084c7070dbSScott Long 	switch (what) {
40094c7070dbSScott Long 	case MOD_LOAD:
40104c7070dbSScott Long 		if ((err = iflib_module_init()) != 0)
40114c7070dbSScott Long 			return (err);
40124c7070dbSScott Long 		break;
40134c7070dbSScott Long 	case MOD_UNLOAD:
40144c7070dbSScott Long 		return (EBUSY);
40154c7070dbSScott Long 	default:
40164c7070dbSScott Long 		return (EOPNOTSUPP);
40174c7070dbSScott Long 	}
40184c7070dbSScott Long 
40194c7070dbSScott Long 	return (0);
40204c7070dbSScott Long }
40214c7070dbSScott Long 
40224c7070dbSScott Long /*********************************************************************
40234c7070dbSScott Long  *
40244c7070dbSScott Long  *  PUBLIC FUNCTION DEFINITIONS
40254c7070dbSScott Long  *     ordered as in iflib.h
40264c7070dbSScott Long  *
40274c7070dbSScott Long  **********************************************************************/
40284c7070dbSScott Long 
40294c7070dbSScott Long 
40304c7070dbSScott Long static void
40314c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx)
40324c7070dbSScott Long {
40334c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsize);
40344c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsegsize);
40354c7070dbSScott Long 
40364c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsize);
40374c7070dbSScott Long 	MPASS(sctx->isc_rx_nsegments);
40384c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsegsize);
40394c7070dbSScott Long 
404023ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_min[0]);
404123ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_max[0]);
404223ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_default[0]);
404323ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_min[0]);
404423ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_max[0]);
404523ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_default[0]);
40464c7070dbSScott Long }
40474c7070dbSScott Long 
40481248952aSSean Bruno static void
40491248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx)
40501248952aSSean Bruno {
40511248952aSSean Bruno 
40521248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_encap);
40531248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_flush);
40541248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
40551248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_available);
40561248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
40571248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_refill);
40581248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_flush);
40591248952aSSean Bruno }
40602fe66646SSean Bruno 
40614c7070dbSScott Long static int
40624c7070dbSScott Long iflib_register(if_ctx_t ctx)
40634c7070dbSScott Long {
40644c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
40654c7070dbSScott Long 	driver_t *driver = sctx->isc_driver;
40664c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
40674c7070dbSScott Long 	if_t ifp;
40684c7070dbSScott Long 
40694c7070dbSScott Long 	_iflib_assert(sctx);
40704c7070dbSScott Long 
40714c7070dbSScott Long 	CTX_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
40724c7070dbSScott Long 
40734c7070dbSScott Long 	ifp = ctx->ifc_ifp = if_gethandle(IFT_ETHER);
40744c7070dbSScott Long 	if (ifp == NULL) {
40754c7070dbSScott Long 		device_printf(dev, "can not allocate ifnet structure\n");
40764c7070dbSScott Long 		return (ENOMEM);
40774c7070dbSScott Long 	}
40784c7070dbSScott Long 
40794c7070dbSScott Long 	/*
40804c7070dbSScott Long 	 * Initialize our context's device specific methods
40814c7070dbSScott Long 	 */
40824c7070dbSScott Long 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
40834c7070dbSScott Long 	kobj_class_compile((kobj_class_t) driver);
40844c7070dbSScott Long 	driver->refs++;
40854c7070dbSScott Long 
40864c7070dbSScott Long 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
40874c7070dbSScott Long 	if_setsoftc(ifp, ctx);
40884c7070dbSScott Long 	if_setdev(ifp, dev);
40894c7070dbSScott Long 	if_setinitfn(ifp, iflib_if_init);
40904c7070dbSScott Long 	if_setioctlfn(ifp, iflib_if_ioctl);
40914c7070dbSScott Long 	if_settransmitfn(ifp, iflib_if_transmit);
40924c7070dbSScott Long 	if_setqflushfn(ifp, iflib_if_qflush);
40934c7070dbSScott Long 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
40944c7070dbSScott Long 
40954c7070dbSScott Long 	ctx->ifc_vlan_attach_event =
40964c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
40974c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
40984c7070dbSScott Long 	ctx->ifc_vlan_detach_event =
40994c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
41004c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
41014c7070dbSScott Long 
41024c7070dbSScott Long 	ifmedia_init(&ctx->ifc_media, IFM_IMASK,
41034c7070dbSScott Long 					 iflib_media_change, iflib_media_status);
41044c7070dbSScott Long 
41054c7070dbSScott Long 	return (0);
41064c7070dbSScott Long }
41074c7070dbSScott Long 
41084c7070dbSScott Long 
41094c7070dbSScott Long static int
41104c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx)
41114c7070dbSScott Long {
41124c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
411323ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
41144c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
411523ac9029SStephen Hurd 	int nrxqsets = scctx->isc_nrxqsets;
411623ac9029SStephen Hurd 	int ntxqsets = scctx->isc_ntxqsets;
41174c7070dbSScott Long 	iflib_txq_t txq;
41184c7070dbSScott Long 	iflib_rxq_t rxq;
41194c7070dbSScott Long 	iflib_fl_t fl = NULL;
412023ac9029SStephen Hurd 	int i, j, cpu, err, txconf, rxconf;
41214c7070dbSScott Long 	iflib_dma_info_t ifdip;
412223ac9029SStephen Hurd 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
412323ac9029SStephen Hurd 	uint32_t *txqsizes = scctx->isc_txqsizes;
41244c7070dbSScott Long 	uint8_t nrxqs = sctx->isc_nrxqs;
41254c7070dbSScott Long 	uint8_t ntxqs = sctx->isc_ntxqs;
41264c7070dbSScott Long 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
41274c7070dbSScott Long 	caddr_t *vaddrs;
41284c7070dbSScott Long 	uint64_t *paddrs;
41294c7070dbSScott Long 	struct ifmp_ring **brscp;
41304c7070dbSScott Long 	int nbuf_rings = 1; /* XXX determine dynamically */
41314c7070dbSScott Long 
413223ac9029SStephen Hurd 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
413323ac9029SStephen Hurd 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
41344c7070dbSScott Long 
41350d0338afSConrad Meyer 	brscp = NULL;
413623ac9029SStephen Hurd 	txq = NULL;
41370d0338afSConrad Meyer 	rxq = NULL;
41380d0338afSConrad Meyer 
41394c7070dbSScott Long /* Allocate the TX ring struct memory */
41404c7070dbSScott Long 	if (!(txq =
41414c7070dbSScott Long 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
41424c7070dbSScott Long 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
41434c7070dbSScott Long 		device_printf(dev, "Unable to allocate TX ring memory\n");
41444c7070dbSScott Long 		err = ENOMEM;
41454c7070dbSScott Long 		goto fail;
41464c7070dbSScott Long 	}
41474c7070dbSScott Long 
41484c7070dbSScott Long 	/* Now allocate the RX */
41494c7070dbSScott Long 	if (!(rxq =
41504c7070dbSScott Long 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
41514c7070dbSScott Long 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
41524c7070dbSScott Long 		device_printf(dev, "Unable to allocate RX ring memory\n");
41534c7070dbSScott Long 		err = ENOMEM;
41544c7070dbSScott Long 		goto rx_fail;
41554c7070dbSScott Long 	}
41564c7070dbSScott Long 	if (!(brscp = malloc(sizeof(void *) * nbuf_rings * nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
41574c7070dbSScott Long 		device_printf(dev, "Unable to buf_ring_sc * memory\n");
41584c7070dbSScott Long 		err = ENOMEM;
41594c7070dbSScott Long 		goto rx_fail;
41604c7070dbSScott Long 	}
41614c7070dbSScott Long 
41624c7070dbSScott Long 	ctx->ifc_txqs = txq;
41634c7070dbSScott Long 	ctx->ifc_rxqs = rxq;
41644c7070dbSScott Long 
41654c7070dbSScott Long 	/*
41664c7070dbSScott Long 	 * XXX handle allocation failure
41674c7070dbSScott Long 	 */
416896c85efbSNathan Whitehorn 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
41694c7070dbSScott Long 		/* Set up some basics */
41704c7070dbSScott Long 
41714c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
41724c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
41734c7070dbSScott Long 			err = ENOMEM;
41740d0338afSConrad Meyer 			goto err_tx_desc;
41754c7070dbSScott Long 		}
41764c7070dbSScott Long 		txq->ift_ifdi = ifdip;
41774c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, ifdip++) {
41784c7070dbSScott Long 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
41794c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
41804c7070dbSScott Long 				err = ENOMEM;
41814c7070dbSScott Long 				goto err_tx_desc;
41824c7070dbSScott Long 			}
41834c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
41844c7070dbSScott Long 		}
41854c7070dbSScott Long 		txq->ift_ctx = ctx;
41864c7070dbSScott Long 		txq->ift_id = i;
418723ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
418823ac9029SStephen Hurd 			txq->ift_br_offset = 1;
418923ac9029SStephen Hurd 		} else {
419023ac9029SStephen Hurd 			txq->ift_br_offset = 0;
419123ac9029SStephen Hurd 		}
41924c7070dbSScott Long 		/* XXX fix this */
419396c85efbSNathan Whitehorn 		txq->ift_timer.c_cpu = cpu;
419496c85efbSNathan Whitehorn 		txq->ift_db_check.c_cpu = cpu;
41954c7070dbSScott Long 		txq->ift_nbr = nbuf_rings;
41964c7070dbSScott Long 
41974c7070dbSScott Long 		if (iflib_txsd_alloc(txq)) {
41984c7070dbSScott Long 			device_printf(dev, "Critical Failure setting up TX buffers\n");
41994c7070dbSScott Long 			err = ENOMEM;
42004c7070dbSScott Long 			goto err_tx_desc;
42014c7070dbSScott Long 		}
42024c7070dbSScott Long 
42034c7070dbSScott Long 		/* Initialize the TX lock */
42044c7070dbSScott Long 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout",
42054c7070dbSScott Long 		    device_get_nameunit(dev), txq->ift_id);
42064c7070dbSScott Long 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
42074c7070dbSScott Long 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
42084c7070dbSScott Long 		callout_init_mtx(&txq->ift_db_check, &txq->ift_mtx, 0);
42094c7070dbSScott Long 
42104c7070dbSScott Long 		snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db",
42114c7070dbSScott Long 			 device_get_nameunit(dev), txq->ift_id);
42124c7070dbSScott Long 		TXDB_LOCK_INIT(txq);
42134c7070dbSScott Long 
42144c7070dbSScott Long 		txq->ift_br = brscp + i*nbuf_rings;
42154c7070dbSScott Long 		for (j = 0; j < nbuf_rings; j++) {
42164c7070dbSScott Long 			err = ifmp_ring_alloc(&txq->ift_br[j], 2048, txq, iflib_txq_drain,
42174c7070dbSScott Long 					      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
42184c7070dbSScott Long 			if (err) {
42194c7070dbSScott Long 				/* XXX free any allocated rings */
42204c7070dbSScott Long 				device_printf(dev, "Unable to allocate buf_ring\n");
42210d0338afSConrad Meyer 				goto err_tx_desc;
42224c7070dbSScott Long 			}
42234c7070dbSScott Long 		}
42244c7070dbSScott Long 	}
42254c7070dbSScott Long 
42264c7070dbSScott Long 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
42274c7070dbSScott Long 		/* Set up some basics */
42284c7070dbSScott Long 
42294c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
42304c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
42314c7070dbSScott Long 			err = ENOMEM;
42320d0338afSConrad Meyer 			goto err_tx_desc;
42334c7070dbSScott Long 		}
42344c7070dbSScott Long 
42354c7070dbSScott Long 		rxq->ifr_ifdi = ifdip;
42364c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, ifdip++) {
42374c7070dbSScott Long 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
42384c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
42394c7070dbSScott Long 				err = ENOMEM;
42404c7070dbSScott Long 				goto err_tx_desc;
42414c7070dbSScott Long 			}
42424c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
42434c7070dbSScott Long 		}
42444c7070dbSScott Long 		rxq->ifr_ctx = ctx;
42454c7070dbSScott Long 		rxq->ifr_id = i;
424623ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
424723ac9029SStephen Hurd 			rxq->ifr_fl_offset = 1;
42484c7070dbSScott Long 		} else {
424923ac9029SStephen Hurd 			rxq->ifr_fl_offset = 0;
42504c7070dbSScott Long 		}
42514c7070dbSScott Long 		rxq->ifr_nfl = nfree_lists;
42524c7070dbSScott Long 		if (!(fl =
42534c7070dbSScott Long 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
42544c7070dbSScott Long 			device_printf(dev, "Unable to allocate free list memory\n");
42554c7070dbSScott Long 			err = ENOMEM;
42560d0338afSConrad Meyer 			goto err_tx_desc;
42574c7070dbSScott Long 		}
42584c7070dbSScott Long 		rxq->ifr_fl = fl;
42594c7070dbSScott Long 		for (j = 0; j < nfree_lists; j++) {
42604c7070dbSScott Long 			rxq->ifr_fl[j].ifl_rxq = rxq;
42614c7070dbSScott Long 			rxq->ifr_fl[j].ifl_id = j;
426223ac9029SStephen Hurd 			rxq->ifr_fl[j].ifl_ifdi =
426323ac9029SStephen Hurd 			    &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
42644c7070dbSScott Long 		}
42654c7070dbSScott Long         /* Allocate receive buffers for the ring*/
42664c7070dbSScott Long 		if (iflib_rxsd_alloc(rxq)) {
42674c7070dbSScott Long 			device_printf(dev,
42684c7070dbSScott Long 			    "Critical Failure setting up receive buffers\n");
42694c7070dbSScott Long 			err = ENOMEM;
42704c7070dbSScott Long 			goto err_rx_desc;
42714c7070dbSScott Long 		}
42724c7070dbSScott Long 	}
42734c7070dbSScott Long 
42744c7070dbSScott Long 	/* TXQs */
42754c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
42764c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
42774c7070dbSScott Long 	for (i = 0; i < ntxqsets; i++) {
42784c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
42794c7070dbSScott Long 
42804c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, di++) {
42814c7070dbSScott Long 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
42824c7070dbSScott Long 			paddrs[i*ntxqs + j] = di->idi_paddr;
42834c7070dbSScott Long 		}
42844c7070dbSScott Long 	}
42854c7070dbSScott Long 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
42864c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
42874c7070dbSScott Long 		iflib_tx_structures_free(ctx);
42884c7070dbSScott Long 		free(vaddrs, M_IFLIB);
42894c7070dbSScott Long 		free(paddrs, M_IFLIB);
42904c7070dbSScott Long 		goto err_rx_desc;
42914c7070dbSScott Long 	}
42924c7070dbSScott Long 	free(vaddrs, M_IFLIB);
42934c7070dbSScott Long 	free(paddrs, M_IFLIB);
42944c7070dbSScott Long 
42954c7070dbSScott Long 	/* RXQs */
42964c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
42974c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
42984c7070dbSScott Long 	for (i = 0; i < nrxqsets; i++) {
42994c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
43004c7070dbSScott Long 
43014c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, di++) {
43024c7070dbSScott Long 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
43034c7070dbSScott Long 			paddrs[i*nrxqs + j] = di->idi_paddr;
43044c7070dbSScott Long 		}
43054c7070dbSScott Long 	}
43064c7070dbSScott Long 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
43074c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
43084c7070dbSScott Long 		iflib_tx_structures_free(ctx);
43094c7070dbSScott Long 		free(vaddrs, M_IFLIB);
43104c7070dbSScott Long 		free(paddrs, M_IFLIB);
43114c7070dbSScott Long 		goto err_rx_desc;
43124c7070dbSScott Long 	}
43134c7070dbSScott Long 	free(vaddrs, M_IFLIB);
43144c7070dbSScott Long 	free(paddrs, M_IFLIB);
43154c7070dbSScott Long 
43164c7070dbSScott Long 	return (0);
43174c7070dbSScott Long 
43184c7070dbSScott Long /* XXX handle allocation failure changes */
43194c7070dbSScott Long err_rx_desc:
43204c7070dbSScott Long err_tx_desc:
43214c7070dbSScott Long 	if (ctx->ifc_rxqs != NULL)
43224c7070dbSScott Long 		free(ctx->ifc_rxqs, M_IFLIB);
43234c7070dbSScott Long 	ctx->ifc_rxqs = NULL;
43244c7070dbSScott Long 	if (ctx->ifc_txqs != NULL)
43254c7070dbSScott Long 		free(ctx->ifc_txqs, M_IFLIB);
43264c7070dbSScott Long 	ctx->ifc_txqs = NULL;
43270d0338afSConrad Meyer rx_fail:
43280d0338afSConrad Meyer 	if (brscp != NULL)
43290d0338afSConrad Meyer 		free(brscp, M_IFLIB);
43300d0338afSConrad Meyer 	if (rxq != NULL)
43310d0338afSConrad Meyer 		free(rxq, M_IFLIB);
43320d0338afSConrad Meyer 	if (txq != NULL)
43330d0338afSConrad Meyer 		free(txq, M_IFLIB);
43344c7070dbSScott Long fail:
43354c7070dbSScott Long 	return (err);
43364c7070dbSScott Long }
43374c7070dbSScott Long 
43384c7070dbSScott Long static int
43394c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx)
43404c7070dbSScott Long {
43414c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
43424c7070dbSScott Long 	int i;
43434c7070dbSScott Long 
43444c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
43454c7070dbSScott Long 		iflib_txq_setup(txq);
43464c7070dbSScott Long 
43474c7070dbSScott Long 	return (0);
43484c7070dbSScott Long }
43494c7070dbSScott Long 
43504c7070dbSScott Long static void
43514c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx)
43524c7070dbSScott Long {
43534c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
43544c7070dbSScott Long 	int i, j;
43554c7070dbSScott Long 
43564c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
43574c7070dbSScott Long 		iflib_txq_destroy(txq);
43584c7070dbSScott Long 		for (j = 0; j < ctx->ifc_nhwtxqs; j++)
43594c7070dbSScott Long 			iflib_dma_free(&txq->ift_ifdi[j]);
43604c7070dbSScott Long 	}
43614c7070dbSScott Long 	free(ctx->ifc_txqs, M_IFLIB);
43624c7070dbSScott Long 	ctx->ifc_txqs = NULL;
43634c7070dbSScott Long 	IFDI_QUEUES_FREE(ctx);
43644c7070dbSScott Long }
43654c7070dbSScott Long 
43664c7070dbSScott Long /*********************************************************************
43674c7070dbSScott Long  *
43684c7070dbSScott Long  *  Initialize all receive rings.
43694c7070dbSScott Long  *
43704c7070dbSScott Long  **********************************************************************/
43714c7070dbSScott Long static int
43724c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx)
43734c7070dbSScott Long {
43744c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
4375aaeb188aSBjoern A. Zeeb 	int q;
4376aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
4377aaeb188aSBjoern A. Zeeb 	int i, err;
4378aaeb188aSBjoern A. Zeeb #endif
43794c7070dbSScott Long 
43804c7070dbSScott Long 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
4381aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
43824c7070dbSScott Long 		tcp_lro_free(&rxq->ifr_lc);
438323ac9029SStephen Hurd 		if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
438423ac9029SStephen Hurd 		    TCP_LRO_ENTRIES, min(1024,
438523ac9029SStephen Hurd 		    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) {
43864c7070dbSScott Long 			device_printf(ctx->ifc_dev, "LRO Initialization failed!\n");
43874c7070dbSScott Long 			goto fail;
43884c7070dbSScott Long 		}
43894c7070dbSScott Long 		rxq->ifr_lro_enabled = TRUE;
4390aaeb188aSBjoern A. Zeeb #endif
43914c7070dbSScott Long 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
43924c7070dbSScott Long 	}
43934c7070dbSScott Long 	return (0);
4394aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
43954c7070dbSScott Long fail:
43964c7070dbSScott Long 	/*
43974c7070dbSScott Long 	 * Free RX software descriptors allocated so far, we will only handle
43984c7070dbSScott Long 	 * the rings that completed, the failing case will have
43994c7070dbSScott Long 	 * cleaned up for itself. 'q' failed, so its the terminus.
44004c7070dbSScott Long 	 */
44014c7070dbSScott Long 	rxq = ctx->ifc_rxqs;
44024c7070dbSScott Long 	for (i = 0; i < q; ++i, rxq++) {
44034c7070dbSScott Long 		iflib_rx_sds_free(rxq);
44044c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
44054c7070dbSScott Long 	}
44064c7070dbSScott Long 	return (err);
4407aaeb188aSBjoern A. Zeeb #endif
44084c7070dbSScott Long }
44094c7070dbSScott Long 
44104c7070dbSScott Long /*********************************************************************
44114c7070dbSScott Long  *
44124c7070dbSScott Long  *  Free all receive rings.
44134c7070dbSScott Long  *
44144c7070dbSScott Long  **********************************************************************/
44154c7070dbSScott Long static void
44164c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx)
44174c7070dbSScott Long {
44184c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
44194c7070dbSScott Long 
442023ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
44214c7070dbSScott Long 		iflib_rx_sds_free(rxq);
44224c7070dbSScott Long 	}
44234c7070dbSScott Long }
44244c7070dbSScott Long 
44254c7070dbSScott Long static int
44264c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx)
44274c7070dbSScott Long {
44284c7070dbSScott Long 	int err;
44294c7070dbSScott Long 
44304c7070dbSScott Long 	if ((err = iflib_tx_structures_setup(ctx)) != 0)
44314c7070dbSScott Long 		return (err);
44324c7070dbSScott Long 
44334c7070dbSScott Long 	if ((err = iflib_rx_structures_setup(ctx)) != 0) {
44344c7070dbSScott Long 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
44354c7070dbSScott Long 		iflib_tx_structures_free(ctx);
44364c7070dbSScott Long 		iflib_rx_structures_free(ctx);
44374c7070dbSScott Long 	}
44384c7070dbSScott Long 	return (err);
44394c7070dbSScott Long }
44404c7070dbSScott Long 
44414c7070dbSScott Long int
44424c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
44434c7070dbSScott Long 				driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, char *name)
44444c7070dbSScott Long {
44454c7070dbSScott Long 
44464c7070dbSScott Long 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
44474c7070dbSScott Long }
44484c7070dbSScott Long 
4449aa3c5dd8SSean Bruno static int
44504c7070dbSScott Long find_nth(if_ctx_t ctx, cpuset_t *cpus, int qid)
44514c7070dbSScott Long {
4452aa3c5dd8SSean Bruno 	int i, cpuid, eqid, count;
44534c7070dbSScott Long 
44544c7070dbSScott Long 	CPU_COPY(&ctx->ifc_cpus, cpus);
4455aa3c5dd8SSean Bruno 	count = CPU_COUNT(&ctx->ifc_cpus);
4456aa3c5dd8SSean Bruno 	eqid = qid % count;
44574c7070dbSScott Long 	/* clear up to the qid'th bit */
4458aa3c5dd8SSean Bruno 	for (i = 0; i < eqid; i++) {
44594c7070dbSScott Long 		cpuid = CPU_FFS(cpus);
4460aa3c5dd8SSean Bruno 		MPASS(cpuid != 0);
4461aa3c5dd8SSean Bruno 		CPU_CLR(cpuid-1, cpus);
44624c7070dbSScott Long 	}
4463aa3c5dd8SSean Bruno 	cpuid = CPU_FFS(cpus);
4464aa3c5dd8SSean Bruno 	MPASS(cpuid != 0);
4465aa3c5dd8SSean Bruno 	return (cpuid-1);
44664c7070dbSScott Long }
44674c7070dbSScott Long 
44684c7070dbSScott Long int
44694c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
44704c7070dbSScott Long 						iflib_intr_type_t type, driver_filter_t *filter,
44714c7070dbSScott Long 						void *filter_arg, int qid, char *name)
44724c7070dbSScott Long {
44734c7070dbSScott Long 	struct grouptask *gtask;
44744c7070dbSScott Long 	struct taskqgroup *tqg;
44754c7070dbSScott Long 	iflib_filter_info_t info;
44764c7070dbSScott Long 	cpuset_t cpus;
447723ac9029SStephen Hurd 	gtask_fn_t *fn;
4478aa3c5dd8SSean Bruno 	int tqrid, err, cpuid;
44794c7070dbSScott Long 	void *q;
44804c7070dbSScott Long 
44814c7070dbSScott Long 	info = &ctx->ifc_filter_info;
4482add6f7d0SSean Bruno 	tqrid = rid;
44834c7070dbSScott Long 
44844c7070dbSScott Long 	switch (type) {
44854c7070dbSScott Long 	/* XXX merge tx/rx for netmap? */
44864c7070dbSScott Long 	case IFLIB_INTR_TX:
44874c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
44884c7070dbSScott Long 		info = &ctx->ifc_txqs[qid].ift_filter_info;
44894c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
44904c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
44914c7070dbSScott Long 		fn = _task_fn_tx;
4492da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
44934c7070dbSScott Long 		break;
44944c7070dbSScott Long 	case IFLIB_INTR_RX:
44954c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
44964c7070dbSScott Long 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
44974c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
44984c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
44994c7070dbSScott Long 		fn = _task_fn_rx;
4500da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
45014c7070dbSScott Long 		break;
45024c7070dbSScott Long 	case IFLIB_INTR_ADMIN:
45034c7070dbSScott Long 		q = ctx;
4504da69b8f9SSean Bruno 		tqrid = -1;
45054c7070dbSScott Long 		info = &ctx->ifc_filter_info;
45064c7070dbSScott Long 		gtask = &ctx->ifc_admin_task;
45074c7070dbSScott Long 		tqg = qgroup_if_config_tqg;
45084c7070dbSScott Long 		fn = _task_fn_admin;
45094c7070dbSScott Long 		break;
45104c7070dbSScott Long 	default:
45114c7070dbSScott Long 		panic("unknown net intr type");
45124c7070dbSScott Long 	}
45134c7070dbSScott Long 
45144c7070dbSScott Long 	info->ifi_filter = filter;
45154c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
45164c7070dbSScott Long 	info->ifi_task = gtask;
45174ecb427aSSean Bruno 	info->ifi_ctx = ctx;
45184c7070dbSScott Long 
45194c7070dbSScott Long 	err = _iflib_irq_alloc(ctx, irq, rid, iflib_fast_intr, NULL, info,  name);
4520da69b8f9SSean Bruno 	if (err != 0) {
4521da69b8f9SSean Bruno 		device_printf(ctx->ifc_dev, "_iflib_irq_alloc failed %d\n", err);
45224c7070dbSScott Long 		return (err);
4523da69b8f9SSean Bruno 	}
4524da69b8f9SSean Bruno 	if (type == IFLIB_INTR_ADMIN)
4525da69b8f9SSean Bruno 		return (0);
4526da69b8f9SSean Bruno 
45274c7070dbSScott Long 	if (tqrid != -1) {
4528aa3c5dd8SSean Bruno 		cpuid = find_nth(ctx, &cpus, qid);
4529aa3c5dd8SSean Bruno 		taskqgroup_attach_cpu(tqg, gtask, q, cpuid, irq->ii_rid, name);
4530aa3c5dd8SSean Bruno 	} else {
45314c7070dbSScott Long 		taskqgroup_attach(tqg, gtask, q, tqrid, name);
4532aa3c5dd8SSean Bruno 	}
45334c7070dbSScott Long 
45344c7070dbSScott Long 	return (0);
45354c7070dbSScott Long }
45364c7070dbSScott Long 
45374c7070dbSScott Long void
45384c7070dbSScott Long iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name)
45394c7070dbSScott Long {
45404c7070dbSScott Long 	struct grouptask *gtask;
45414c7070dbSScott Long 	struct taskqgroup *tqg;
454223ac9029SStephen Hurd 	gtask_fn_t *fn;
45434c7070dbSScott Long 	void *q;
45444c7070dbSScott Long 
45454c7070dbSScott Long 	switch (type) {
45464c7070dbSScott Long 	case IFLIB_INTR_TX:
45474c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
45484c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
45494c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
45504c7070dbSScott Long 		fn = _task_fn_tx;
45514c7070dbSScott Long 		break;
45524c7070dbSScott Long 	case IFLIB_INTR_RX:
45534c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
45544c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
45554c7070dbSScott Long 		tqg = qgroup_if_io_tqg;
45564c7070dbSScott Long 		fn = _task_fn_rx;
45574c7070dbSScott Long 		break;
45584c7070dbSScott Long 	case IFLIB_INTR_IOV:
45594c7070dbSScott Long 		q = ctx;
45604c7070dbSScott Long 		gtask = &ctx->ifc_vflr_task;
45614c7070dbSScott Long 		tqg = qgroup_if_config_tqg;
45624c7070dbSScott Long 		rid = -1;
45634c7070dbSScott Long 		fn = _task_fn_iov;
45644c7070dbSScott Long 		break;
45654c7070dbSScott Long 	default:
45664c7070dbSScott Long 		panic("unknown net intr type");
45674c7070dbSScott Long 	}
45684c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
45694c7070dbSScott Long 	taskqgroup_attach(tqg, gtask, q, rid, name);
45704c7070dbSScott Long }
45714c7070dbSScott Long 
45724c7070dbSScott Long void
45734c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
45744c7070dbSScott Long {
45754c7070dbSScott Long 	if (irq->ii_tag)
45764c7070dbSScott Long 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
45774c7070dbSScott Long 
45784c7070dbSScott Long 	if (irq->ii_res)
45794c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, irq->ii_rid, irq->ii_res);
45804c7070dbSScott Long }
45814c7070dbSScott Long 
45824c7070dbSScott Long static int
45834c7070dbSScott Long iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, char *name)
45844c7070dbSScott Long {
45854c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
45864c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
45874c7070dbSScott Long 	if_irq_t irq = &ctx->ifc_legacy_irq;
45884c7070dbSScott Long 	iflib_filter_info_t info;
45894c7070dbSScott Long 	struct grouptask *gtask;
45904c7070dbSScott Long 	struct taskqgroup *tqg;
459123ac9029SStephen Hurd 	gtask_fn_t *fn;
45924c7070dbSScott Long 	int tqrid;
45934c7070dbSScott Long 	void *q;
45944c7070dbSScott Long 	int err;
45954c7070dbSScott Long 
45964c7070dbSScott Long 	q = &ctx->ifc_rxqs[0];
45974c7070dbSScott Long 	info = &rxq[0].ifr_filter_info;
45984c7070dbSScott Long 	gtask = &rxq[0].ifr_task;
45994c7070dbSScott Long 	tqg = qgroup_if_io_tqg;
46004c7070dbSScott Long 	tqrid = irq->ii_rid = *rid;
46014c7070dbSScott Long 	fn = _task_fn_rx;
46024c7070dbSScott Long 
46034c7070dbSScott Long 	ctx->ifc_flags |= IFC_LEGACY;
46044c7070dbSScott Long 	info->ifi_filter = filter;
46054c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
46064c7070dbSScott Long 	info->ifi_task = gtask;
46074ecb427aSSean Bruno 	info->ifi_ctx = ctx;
46084c7070dbSScott Long 
46094c7070dbSScott Long 	/* We allocate a single interrupt resource */
46104c7070dbSScott Long 	if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr, NULL, info, name)) != 0)
46114c7070dbSScott Long 		return (err);
46124c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
46134c7070dbSScott Long 	taskqgroup_attach(tqg, gtask, q, tqrid, name);
46144c7070dbSScott Long 
46154c7070dbSScott Long 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
46164c7070dbSScott Long 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, tqrid, "tx");
46174c7070dbSScott Long 	return (0);
46184c7070dbSScott Long }
46194c7070dbSScott Long 
46204c7070dbSScott Long void
46214c7070dbSScott Long iflib_led_create(if_ctx_t ctx)
46224c7070dbSScott Long {
46234c7070dbSScott Long 
46244c7070dbSScott Long 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
46254c7070dbSScott Long 								  device_get_nameunit(ctx->ifc_dev));
46264c7070dbSScott Long }
46274c7070dbSScott Long 
46284c7070dbSScott Long void
46294c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
46304c7070dbSScott Long {
46314c7070dbSScott Long 
46324c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
46334c7070dbSScott Long }
46344c7070dbSScott Long 
46354c7070dbSScott Long void
46364c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
46374c7070dbSScott Long {
46384c7070dbSScott Long 
46394c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
46404c7070dbSScott Long }
46414c7070dbSScott Long 
46424c7070dbSScott Long void
46434c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx)
46444c7070dbSScott Long {
46451248952aSSean Bruno #ifdef INVARIANTS
46461248952aSSean Bruno 	struct grouptask *gtask;
46471248952aSSean Bruno 
46481248952aSSean Bruno 	gtask = &ctx->ifc_admin_task;
46491248952aSSean Bruno 	MPASS(gtask->gt_taskqueue != NULL);
46501248952aSSean Bruno #endif
46514c7070dbSScott Long 
46524c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
46534c7070dbSScott Long }
46544c7070dbSScott Long 
46554c7070dbSScott Long void
46564c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx)
46574c7070dbSScott Long {
46584c7070dbSScott Long 
46594c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
46604c7070dbSScott Long }
46614c7070dbSScott Long 
46624c7070dbSScott Long void
46634c7070dbSScott Long iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name)
46644c7070dbSScott Long {
46654c7070dbSScott Long 
46664c7070dbSScott Long 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, -1, name);
46674c7070dbSScott Long }
46684c7070dbSScott Long 
46694c7070dbSScott Long void
467023ac9029SStephen Hurd iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask, gtask_fn_t *fn,
46714c7070dbSScott Long 	char *name)
46724c7070dbSScott Long {
46734c7070dbSScott Long 
46744c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, ctx);
46754c7070dbSScott Long 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, -1, name);
46764c7070dbSScott Long }
46774c7070dbSScott Long 
46784c7070dbSScott Long void
467923ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask)
468023ac9029SStephen Hurd {
468123ac9029SStephen Hurd 
468223ac9029SStephen Hurd 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
468323ac9029SStephen Hurd }
468423ac9029SStephen Hurd 
468523ac9029SStephen Hurd void
468623ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
46874c7070dbSScott Long {
46884c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
46894c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
46904c7070dbSScott Long 
46914c7070dbSScott Long 	if_setbaudrate(ifp, baudrate);
469223ac9029SStephen Hurd 
46934c7070dbSScott Long 	/* If link down, disable watchdog */
46944c7070dbSScott Long 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
46954c7070dbSScott Long 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
46964c7070dbSScott Long 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
46974c7070dbSScott Long 	}
46984c7070dbSScott Long 	ctx->ifc_link_state = link_state;
46994c7070dbSScott Long 	if_link_state_change(ifp, link_state);
47004c7070dbSScott Long }
47014c7070dbSScott Long 
47024c7070dbSScott Long static int
47034c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
47044c7070dbSScott Long {
47054c7070dbSScott Long 	int credits;
47061248952aSSean Bruno #ifdef INVARIANTS
47071248952aSSean Bruno 	int credits_pre = txq->ift_cidx_processed;
47081248952aSSean Bruno #endif
47094c7070dbSScott Long 
47104c7070dbSScott Long 	if (ctx->isc_txd_credits_update == NULL)
47114c7070dbSScott Long 		return (0);
47124c7070dbSScott Long 
47134c7070dbSScott Long 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, txq->ift_cidx_processed, true)) == 0)
47144c7070dbSScott Long 		return (0);
47154c7070dbSScott Long 
47164c7070dbSScott Long 	txq->ift_processed += credits;
47174c7070dbSScott Long 	txq->ift_cidx_processed += credits;
47184c7070dbSScott Long 
47191248952aSSean Bruno 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
47204c7070dbSScott Long 	if (txq->ift_cidx_processed >= txq->ift_size)
47214c7070dbSScott Long 		txq->ift_cidx_processed -= txq->ift_size;
47224c7070dbSScott Long 	return (credits);
47234c7070dbSScott Long }
47244c7070dbSScott Long 
47254c7070dbSScott Long static int
472623ac9029SStephen Hurd iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, int cidx, int budget)
47274c7070dbSScott Long {
47284c7070dbSScott Long 
472923ac9029SStephen Hurd 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
473023ac9029SStephen Hurd 	    budget));
47314c7070dbSScott Long }
47324c7070dbSScott Long 
47334c7070dbSScott Long void
47344c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
47354c7070dbSScott Long 	const char *description, if_int_delay_info_t info,
47364c7070dbSScott Long 	int offset, int value)
47374c7070dbSScott Long {
47384c7070dbSScott Long 	info->iidi_ctx = ctx;
47394c7070dbSScott Long 	info->iidi_offset = offset;
47404c7070dbSScott Long 	info->iidi_value = value;
47414c7070dbSScott Long 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
47424c7070dbSScott Long 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
47434c7070dbSScott Long 	    OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
47444c7070dbSScott Long 	    info, 0, iflib_sysctl_int_delay, "I", description);
47454c7070dbSScott Long }
47464c7070dbSScott Long 
47474c7070dbSScott Long struct mtx *
47484c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx)
47494c7070dbSScott Long {
47504c7070dbSScott Long 
47514c7070dbSScott Long 	return (&ctx->ifc_mtx);
47524c7070dbSScott Long }
47534c7070dbSScott Long 
47544c7070dbSScott Long static int
47554c7070dbSScott Long iflib_msix_init(if_ctx_t ctx)
47564c7070dbSScott Long {
47574c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
47584c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
47594c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
47604c7070dbSScott Long 	int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs;
47614c7070dbSScott Long 	int iflib_num_tx_queues, iflib_num_rx_queues;
47624c7070dbSScott Long 	int err, admincnt, bar;
47634c7070dbSScott Long 
476423ac9029SStephen Hurd 	iflib_num_tx_queues = scctx->isc_ntxqsets;
476523ac9029SStephen Hurd 	iflib_num_rx_queues = scctx->isc_nrxqsets;
476623ac9029SStephen Hurd 
47671248952aSSean Bruno 	device_printf(dev, "msix_init qsets capped at %d\n", iflib_num_tx_queues);
47681248952aSSean Bruno 
47694c7070dbSScott Long 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
47704c7070dbSScott Long 	admincnt = sctx->isc_admin_intrcnt;
47714c7070dbSScott Long 	/* Override by tuneable */
47724c7070dbSScott Long 	if (enable_msix == 0)
47734c7070dbSScott Long 		goto msi;
47744c7070dbSScott Long 
47754c7070dbSScott Long 	/*
47764c7070dbSScott Long 	** When used in a virtualized environment
47774c7070dbSScott Long 	** PCI BUSMASTER capability may not be set
47784c7070dbSScott Long 	** so explicity set it here and rewrite
47794c7070dbSScott Long 	** the ENABLE in the MSIX control register
47804c7070dbSScott Long 	** at this point to cause the host to
47814c7070dbSScott Long 	** successfully initialize us.
47824c7070dbSScott Long 	*/
47834c7070dbSScott Long 	{
47844c7070dbSScott Long 		int msix_ctrl, rid;
47854c7070dbSScott Long 
4786*96eeabefSSean Bruno  		pci_enable_busmaster(dev);
4787f7ae9a84SSean Bruno 		rid = 0;
4788f7ae9a84SSean Bruno 		if (pci_find_cap(dev, PCIY_MSIX, &rid) == 0 && rid != 0) {
47894c7070dbSScott Long 			rid += PCIR_MSIX_CTRL;
47904c7070dbSScott Long 			msix_ctrl = pci_read_config(dev, rid, 2);
47914c7070dbSScott Long 			msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
47924c7070dbSScott Long 			pci_write_config(dev, rid, msix_ctrl, 2);
4793f7ae9a84SSean Bruno 		} else {
4794f7ae9a84SSean Bruno 			device_printf(dev, "PCIY_MSIX capability not found; "
4795f7ae9a84SSean Bruno 			                   "or rid %d == 0.\n", rid);
4796f7ae9a84SSean Bruno 			goto msi;
4797f7ae9a84SSean Bruno 		}
47984c7070dbSScott Long 	}
47994c7070dbSScott Long 
48004c7070dbSScott Long 	/*
48014c7070dbSScott Long 	 * bar == -1 => "trust me I know what I'm doing"
48024c7070dbSScott Long 	 * https://www.youtube.com/watch?v=nnwWKkNau4I
48034c7070dbSScott Long 	 * Some drivers are for hardware that is so shoddily
48044c7070dbSScott Long 	 * documented that no one knows which bars are which
48054c7070dbSScott Long 	 * so the developer has to map all bars. This hack
48064c7070dbSScott Long 	 * allows shoddy garbage to use msix in this framework.
48074c7070dbSScott Long 	 */
48084c7070dbSScott Long 	if (bar != -1) {
48094c7070dbSScott Long 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
48104c7070dbSScott Long 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
48114c7070dbSScott Long 		if (ctx->ifc_msix_mem == NULL) {
48124c7070dbSScott Long 			/* May not be enabled */
48134c7070dbSScott Long 			device_printf(dev, "Unable to map MSIX table \n");
48144c7070dbSScott Long 			goto msi;
48154c7070dbSScott Long 		}
48164c7070dbSScott Long 	}
48174c7070dbSScott Long 	/* First try MSI/X */
48184c7070dbSScott Long 	if ((msgs = pci_msix_count(dev)) == 0) { /* system has msix disabled */
48194c7070dbSScott Long 		device_printf(dev, "System has MSIX disabled \n");
48204c7070dbSScott Long 		bus_release_resource(dev, SYS_RES_MEMORY,
48214c7070dbSScott Long 		    bar, ctx->ifc_msix_mem);
48224c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
48234c7070dbSScott Long 		goto msi;
48244c7070dbSScott Long 	}
48254c7070dbSScott Long #if IFLIB_DEBUG
48264c7070dbSScott Long 	/* use only 1 qset in debug mode */
48274c7070dbSScott Long 	queuemsgs = min(msgs - admincnt, 1);
48284c7070dbSScott Long #else
48294c7070dbSScott Long 	queuemsgs = msgs - admincnt;
48304c7070dbSScott Long #endif
48314c7070dbSScott Long 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) == 0) {
48324c7070dbSScott Long #ifdef RSS
48334c7070dbSScott Long 		queues = imin(queuemsgs, rss_getnumbuckets());
48344c7070dbSScott Long #else
48354c7070dbSScott Long 		queues = queuemsgs;
48364c7070dbSScott Long #endif
48374c7070dbSScott Long 		queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
48384c7070dbSScott Long 		device_printf(dev, "pxm cpus: %d queue msgs: %d admincnt: %d\n",
48394c7070dbSScott Long 					  CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
48404c7070dbSScott Long 	} else {
48414c7070dbSScott Long 		device_printf(dev, "Unable to fetch CPU list\n");
48424c7070dbSScott Long 		/* Figure out a reasonable auto config value */
48434c7070dbSScott Long 		queues = min(queuemsgs, mp_ncpus);
48444c7070dbSScott Long 	}
48454c7070dbSScott Long #ifdef  RSS
48464c7070dbSScott Long 	/* If we're doing RSS, clamp at the number of RSS buckets */
48474c7070dbSScott Long 	if (queues > rss_getnumbuckets())
48484c7070dbSScott Long 		queues = rss_getnumbuckets();
48494c7070dbSScott Long #endif
485023ac9029SStephen Hurd 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
485123ac9029SStephen Hurd 		rx_queues = iflib_num_rx_queues;
48524c7070dbSScott Long 	else
48534c7070dbSScott Long 		rx_queues = queues;
485423ac9029SStephen Hurd 	/*
485523ac9029SStephen Hurd 	 * We want this to be all logical CPUs by default
485623ac9029SStephen Hurd 	 */
48574c7070dbSScott Long 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
48584c7070dbSScott Long 		tx_queues = iflib_num_tx_queues;
48594c7070dbSScott Long 	else
486023ac9029SStephen Hurd 		tx_queues = mp_ncpus;
486123ac9029SStephen Hurd 
486223ac9029SStephen Hurd 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
486323ac9029SStephen Hurd #ifdef INVARIANTS
486423ac9029SStephen Hurd 		if (tx_queues != rx_queues)
486523ac9029SStephen Hurd 			device_printf(dev, "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
486623ac9029SStephen Hurd 				      min(rx_queues, tx_queues), min(rx_queues, tx_queues));
486723ac9029SStephen Hurd #endif
486823ac9029SStephen Hurd 		tx_queues = min(rx_queues, tx_queues);
486923ac9029SStephen Hurd 		rx_queues = min(rx_queues, tx_queues);
487023ac9029SStephen Hurd 	}
48714c7070dbSScott Long 
48724c7070dbSScott Long 	device_printf(dev, "using %d rx queues %d tx queues \n", rx_queues, tx_queues);
48734c7070dbSScott Long 
487423ac9029SStephen Hurd 	vectors = rx_queues + admincnt;
48754c7070dbSScott Long 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
48764c7070dbSScott Long 		device_printf(dev,
48774c7070dbSScott Long 					  "Using MSIX interrupts with %d vectors\n", vectors);
48784c7070dbSScott Long 		scctx->isc_vectors = vectors;
48794c7070dbSScott Long 		scctx->isc_nrxqsets = rx_queues;
48804c7070dbSScott Long 		scctx->isc_ntxqsets = tx_queues;
48814c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSIX;
488223ac9029SStephen Hurd 
48834c7070dbSScott Long 		return (vectors);
48844c7070dbSScott Long 	} else {
48854c7070dbSScott Long 		device_printf(dev, "failed to allocate %d msix vectors, err: %d - using MSI\n", vectors, err);
48864c7070dbSScott Long 	}
48874c7070dbSScott Long msi:
48884c7070dbSScott Long 	vectors = pci_msi_count(dev);
48894c7070dbSScott Long 	scctx->isc_nrxqsets = 1;
48904c7070dbSScott Long 	scctx->isc_ntxqsets = 1;
48914c7070dbSScott Long 	scctx->isc_vectors = vectors;
48924c7070dbSScott Long 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
48934c7070dbSScott Long 		device_printf(dev,"Using an MSI interrupt\n");
48944c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSI;
48954c7070dbSScott Long 	} else {
48964c7070dbSScott Long 		device_printf(dev,"Using a Legacy interrupt\n");
48974c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
48984c7070dbSScott Long 	}
48994c7070dbSScott Long 
49004c7070dbSScott Long 	return (vectors);
49014c7070dbSScott Long }
49024c7070dbSScott Long 
49034c7070dbSScott Long char * ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
49044c7070dbSScott Long 
49054c7070dbSScott Long static int
49064c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
49074c7070dbSScott Long {
49084c7070dbSScott Long 	int rc;
49094c7070dbSScott Long 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
49104c7070dbSScott Long 	struct sbuf *sb;
49114c7070dbSScott Long 	char *ring_state = "UNKNOWN";
49124c7070dbSScott Long 
49134c7070dbSScott Long 	/* XXX needed ? */
49144c7070dbSScott Long 	rc = sysctl_wire_old_buffer(req, 0);
49154c7070dbSScott Long 	MPASS(rc == 0);
49164c7070dbSScott Long 	if (rc != 0)
49174c7070dbSScott Long 		return (rc);
49184c7070dbSScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
49194c7070dbSScott Long 	MPASS(sb != NULL);
49204c7070dbSScott Long 	if (sb == NULL)
49214c7070dbSScott Long 		return (ENOMEM);
49224c7070dbSScott Long 	if (state[3] <= 3)
49234c7070dbSScott Long 		ring_state = ring_states[state[3]];
49244c7070dbSScott Long 
49254c7070dbSScott Long 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
49264c7070dbSScott Long 		    state[0], state[1], state[2], ring_state);
49274c7070dbSScott Long 	rc = sbuf_finish(sb);
49284c7070dbSScott Long 	sbuf_delete(sb);
49294c7070dbSScott Long         return(rc);
49304c7070dbSScott Long }
49314c7070dbSScott Long 
493223ac9029SStephen Hurd enum iflib_ndesc_handler {
493323ac9029SStephen Hurd 	IFLIB_NTXD_HANDLER,
493423ac9029SStephen Hurd 	IFLIB_NRXD_HANDLER,
493523ac9029SStephen Hurd };
49364c7070dbSScott Long 
493723ac9029SStephen Hurd static int
493823ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
493923ac9029SStephen Hurd {
494023ac9029SStephen Hurd 	if_ctx_t ctx = (void *)arg1;
494123ac9029SStephen Hurd 	enum iflib_ndesc_handler type = arg2;
494223ac9029SStephen Hurd 	char buf[256] = {0};
494323ac9029SStephen Hurd 	uint16_t *ndesc;
494423ac9029SStephen Hurd 	char *p, *next;
494523ac9029SStephen Hurd 	int nqs, rc, i;
494623ac9029SStephen Hurd 
494723ac9029SStephen Hurd 	MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER);
494823ac9029SStephen Hurd 
494923ac9029SStephen Hurd 	nqs = 8;
495023ac9029SStephen Hurd 	switch(type) {
495123ac9029SStephen Hurd 	case IFLIB_NTXD_HANDLER:
495223ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_ntxds;
495323ac9029SStephen Hurd 		if (ctx->ifc_sctx)
495423ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_ntxqs;
495523ac9029SStephen Hurd 		break;
495623ac9029SStephen Hurd 	case IFLIB_NRXD_HANDLER:
495723ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_nrxds;
495823ac9029SStephen Hurd 		if (ctx->ifc_sctx)
495923ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_nrxqs;
496023ac9029SStephen Hurd 		break;
496123ac9029SStephen Hurd 	}
496223ac9029SStephen Hurd 	if (nqs == 0)
496323ac9029SStephen Hurd 		nqs = 8;
496423ac9029SStephen Hurd 
496523ac9029SStephen Hurd 	for (i=0; i<8; i++) {
496623ac9029SStephen Hurd 		if (i >= nqs)
496723ac9029SStephen Hurd 			break;
496823ac9029SStephen Hurd 		if (i)
496923ac9029SStephen Hurd 			strcat(buf, ",");
497023ac9029SStephen Hurd 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
497123ac9029SStephen Hurd 	}
497223ac9029SStephen Hurd 
497323ac9029SStephen Hurd 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
497423ac9029SStephen Hurd 	if (rc || req->newptr == NULL)
497523ac9029SStephen Hurd 		return rc;
497623ac9029SStephen Hurd 
497723ac9029SStephen Hurd 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
497823ac9029SStephen Hurd 	    i++, p = strsep(&next, " ,")) {
497923ac9029SStephen Hurd 		ndesc[i] = strtoul(p, NULL, 10);
498023ac9029SStephen Hurd 	}
498123ac9029SStephen Hurd 
498223ac9029SStephen Hurd 	return(rc);
498323ac9029SStephen Hurd }
49844c7070dbSScott Long 
49854c7070dbSScott Long #define NAME_BUFLEN 32
49864c7070dbSScott Long static void
49874c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx)
49884c7070dbSScott Long {
49894c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
49904c7070dbSScott Long 	struct sysctl_oid_list *child, *oid_list;
49914c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
49924c7070dbSScott Long 	struct sysctl_oid *node;
49934c7070dbSScott Long 
49944c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
49954c7070dbSScott Long 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
49964c7070dbSScott Long 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
49974c7070dbSScott Long 						      CTLFLAG_RD, NULL, "IFLIB fields");
49984c7070dbSScott Long 	oid_list = SYSCTL_CHILDREN(node);
49994c7070dbSScott Long 
500023ac9029SStephen Hurd 	SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
500123ac9029SStephen Hurd 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
500223ac9029SStephen Hurd 		       "driver version");
500323ac9029SStephen Hurd 
50044c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
50054c7070dbSScott Long 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
50064c7070dbSScott Long 			"# of txqs to use, 0 => use default #");
50074c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
500823ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
500923ac9029SStephen Hurd 			"# of rxqs to use, 0 => use default #");
501023ac9029SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
501123ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
501223ac9029SStephen Hurd                        "permit #txq != #rxq");
50134c7070dbSScott Long 
501423ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
501523ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
501623ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER,
501723ac9029SStephen Hurd                        mp_ndesc_handler, "A",
501823ac9029SStephen Hurd                        "list of # of tx descriptors to use, 0 = use default #");
501923ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
502023ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER,
502123ac9029SStephen Hurd                        mp_ndesc_handler, "A",
502223ac9029SStephen Hurd                        "list of # of rx descriptors to use, 0 = use default #");
50234c7070dbSScott Long }
50244c7070dbSScott Long 
50254c7070dbSScott Long static void
50264c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx)
50274c7070dbSScott Long {
50284c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
50294c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
50304c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
50314c7070dbSScott Long 	struct sysctl_oid_list *child;
50324c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
50334c7070dbSScott Long 	iflib_fl_t fl;
50344c7070dbSScott Long 	iflib_txq_t txq;
50354c7070dbSScott Long 	iflib_rxq_t rxq;
50364c7070dbSScott Long 	int i, j;
50374c7070dbSScott Long 	char namebuf[NAME_BUFLEN];
50384c7070dbSScott Long 	char *qfmt;
50394c7070dbSScott Long 	struct sysctl_oid *queue_node, *fl_node, *node;
50404c7070dbSScott Long 	struct sysctl_oid_list *queue_list, *fl_list;
50414c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
50424c7070dbSScott Long 
50434c7070dbSScott Long 	node = ctx->ifc_sysctl_node;
50444c7070dbSScott Long 	child = SYSCTL_CHILDREN(node);
50454c7070dbSScott Long 
50464c7070dbSScott Long 	if (scctx->isc_ntxqsets > 100)
50474c7070dbSScott Long 		qfmt = "txq%03d";
50484c7070dbSScott Long 	else if (scctx->isc_ntxqsets > 10)
50494c7070dbSScott Long 		qfmt = "txq%02d";
50504c7070dbSScott Long 	else
50514c7070dbSScott Long 		qfmt = "txq%d";
50524c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
50534c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
50544c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
50554c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
50564c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
50574c7070dbSScott Long #if MEMORY_LOGGING
50584c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
50594c7070dbSScott Long 				CTLFLAG_RD,
50604c7070dbSScott Long 				&txq->ift_dequeued, "total mbufs freed");
50614c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
50624c7070dbSScott Long 				CTLFLAG_RD,
50634c7070dbSScott Long 				&txq->ift_enqueued, "total mbufs enqueued");
50644c7070dbSScott Long #endif
50654c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
50664c7070dbSScott Long 				   CTLFLAG_RD,
50674c7070dbSScott Long 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
50684c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
50694c7070dbSScott Long 				   CTLFLAG_RD,
50704c7070dbSScott Long 				   &txq->ift_pullups, "# of times m_pullup was called");
50714c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
50724c7070dbSScott Long 				   CTLFLAG_RD,
50734c7070dbSScott Long 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
50744c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
50754c7070dbSScott Long 				   CTLFLAG_RD,
507623ac9029SStephen Hurd 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
50774c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
50784c7070dbSScott Long 				   CTLFLAG_RD,
50794c7070dbSScott Long 				   &txq->ift_map_failed, "# of times dma map failed");
50804c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
50814c7070dbSScott Long 				   CTLFLAG_RD,
50824c7070dbSScott Long 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
50834c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
50844c7070dbSScott Long 				   CTLFLAG_RD,
50854c7070dbSScott Long 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
50864c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
50874c7070dbSScott Long 				   CTLFLAG_RD,
50884c7070dbSScott Long 				   &txq->ift_pidx, 1, "Producer Index");
50894c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
50904c7070dbSScott Long 				   CTLFLAG_RD,
50914c7070dbSScott Long 				   &txq->ift_cidx, 1, "Consumer Index");
50924c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
50934c7070dbSScott Long 				   CTLFLAG_RD,
50944c7070dbSScott Long 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
50954c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
50964c7070dbSScott Long 				   CTLFLAG_RD,
50974c7070dbSScott Long 				   &txq->ift_in_use, 1, "descriptors in use");
50984c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
50994c7070dbSScott Long 				   CTLFLAG_RD,
51004c7070dbSScott Long 				   &txq->ift_processed, "descriptors procesed for clean");
51014c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
51024c7070dbSScott Long 				   CTLFLAG_RD,
51034c7070dbSScott Long 				   &txq->ift_cleaned, "total cleaned");
51044c7070dbSScott Long 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
51054c7070dbSScott Long 				CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br[0]->state),
51064c7070dbSScott Long 				0, mp_ring_state_handler, "A", "soft ring state");
51074c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
51084c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->enqueues,
51094c7070dbSScott Long 				       "# of enqueues to the mp_ring for this queue");
51104c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
51114c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->drops,
51124c7070dbSScott Long 				       "# of drops in the mp_ring for this queue");
51134c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
51144c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->starts,
51154c7070dbSScott Long 				       "# of normal consumer starts in the mp_ring for this queue");
51164c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
51174c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->stalls,
51184c7070dbSScott Long 					       "# of consumer stalls in the mp_ring for this queue");
51194c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
51204c7070dbSScott Long 			       CTLFLAG_RD, &txq->ift_br[0]->restarts,
51214c7070dbSScott Long 				       "# of consumer restarts in the mp_ring for this queue");
51224c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
51234c7070dbSScott Long 				       CTLFLAG_RD, &txq->ift_br[0]->abdications,
51244c7070dbSScott Long 				       "# of consumer abdications in the mp_ring for this queue");
51254c7070dbSScott Long 	}
51264c7070dbSScott Long 
51274c7070dbSScott Long 	if (scctx->isc_nrxqsets > 100)
51284c7070dbSScott Long 		qfmt = "rxq%03d";
51294c7070dbSScott Long 	else if (scctx->isc_nrxqsets > 10)
51304c7070dbSScott Long 		qfmt = "rxq%02d";
51314c7070dbSScott Long 	else
51324c7070dbSScott Long 		qfmt = "rxq%d";
51334c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
51344c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
51354c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
51364c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
51374c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
513823ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
51394c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx",
51404c7070dbSScott Long 				       CTLFLAG_RD,
51414c7070dbSScott Long 				       &rxq->ifr_cq_pidx, 1, "Producer Index");
51424c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
51434c7070dbSScott Long 				       CTLFLAG_RD,
51444c7070dbSScott Long 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
51454c7070dbSScott Long 		}
5146da69b8f9SSean Bruno 
51474c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
51484c7070dbSScott Long 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
51494c7070dbSScott Long 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
51504c7070dbSScott Long 						     CTLFLAG_RD, NULL, "freelist Name");
51514c7070dbSScott Long 			fl_list = SYSCTL_CHILDREN(fl_node);
51524c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
51534c7070dbSScott Long 				       CTLFLAG_RD,
51544c7070dbSScott Long 				       &fl->ifl_pidx, 1, "Producer Index");
51554c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
51564c7070dbSScott Long 				       CTLFLAG_RD,
51574c7070dbSScott Long 				       &fl->ifl_cidx, 1, "Consumer Index");
51584c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
51594c7070dbSScott Long 				       CTLFLAG_RD,
51604c7070dbSScott Long 				       &fl->ifl_credits, 1, "credits available");
51614c7070dbSScott Long #if MEMORY_LOGGING
51624c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
51634c7070dbSScott Long 					CTLFLAG_RD,
51644c7070dbSScott Long 					&fl->ifl_m_enqueued, "mbufs allocated");
51654c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
51664c7070dbSScott Long 					CTLFLAG_RD,
51674c7070dbSScott Long 					&fl->ifl_m_dequeued, "mbufs freed");
51684c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
51694c7070dbSScott Long 					CTLFLAG_RD,
51704c7070dbSScott Long 					&fl->ifl_cl_enqueued, "clusters allocated");
51714c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
51724c7070dbSScott Long 					CTLFLAG_RD,
51734c7070dbSScott Long 					&fl->ifl_cl_dequeued, "clusters freed");
51744c7070dbSScott Long #endif
51754c7070dbSScott Long 
51764c7070dbSScott Long 		}
51774c7070dbSScott Long 	}
51784c7070dbSScott Long 
51794c7070dbSScott Long }
5180