xref: /freebsd/sys/net/iflib.c (revision 814fa34dfb3706aede6ac6c2ea896da90c2a1d75)
14c7070dbSScott Long /*-
27b610b60SSean Bruno  * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io>
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"
34b103855eSStephen Hurd #include "opt_sched.h"
35aaeb188aSBjoern A. Zeeb 
364c7070dbSScott Long #include <sys/param.h>
374c7070dbSScott Long #include <sys/types.h>
384c7070dbSScott Long #include <sys/bus.h>
394c7070dbSScott Long #include <sys/eventhandler.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>
4909f6ff4fSMatt Macy #include <sys/sockio.h>
504c7070dbSScott Long #include <sys/sysctl.h>
514c7070dbSScott Long #include <sys/syslog.h>
524c7070dbSScott Long #include <sys/taskqueue.h>
5323ac9029SStephen Hurd #include <sys/limits.h>
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>
627790c8c1SConrad Meyer #include <net/debugnet.h>
636d49b41eSAndrew Gallatin #include <net/pfil.h>
6435e4e998SStephen Hurd #include <net/vnet.h>
654c7070dbSScott Long 
664c7070dbSScott Long #include <netinet/in.h>
674c7070dbSScott Long #include <netinet/in_pcb.h>
684c7070dbSScott Long #include <netinet/tcp_lro.h>
694c7070dbSScott Long #include <netinet/in_systm.h>
704c7070dbSScott Long #include <netinet/if_ether.h>
714c7070dbSScott Long #include <netinet/ip.h>
724c7070dbSScott Long #include <netinet/ip6.h>
734c7070dbSScott Long #include <netinet/tcp.h>
7435e4e998SStephen Hurd #include <netinet/ip_var.h>
7535e4e998SStephen Hurd #include <netinet6/ip6_var.h>
764c7070dbSScott Long 
774c7070dbSScott Long #include <machine/bus.h>
784c7070dbSScott Long #include <machine/in_cksum.h>
794c7070dbSScott Long 
804c7070dbSScott Long #include <vm/vm.h>
814c7070dbSScott Long #include <vm/pmap.h>
824c7070dbSScott Long 
834c7070dbSScott Long #include <dev/led/led.h>
844c7070dbSScott Long #include <dev/pci/pcireg.h>
854c7070dbSScott Long #include <dev/pci/pcivar.h>
864c7070dbSScott Long #include <dev/pci/pci_private.h>
874c7070dbSScott Long 
884c7070dbSScott Long #include <net/iflib.h>
8909f6ff4fSMatt Macy #include <net/iflib_private.h>
904c7070dbSScott Long 
914c7070dbSScott Long #include "ifdi_if.h"
924c7070dbSScott Long 
9377c1fcecSEric Joyner #ifdef PCI_IOV
9477c1fcecSEric Joyner #include <dev/pci/pci_iov.h>
9577c1fcecSEric Joyner #endif
9677c1fcecSEric Joyner 
9787890dbaSSean Bruno #include <sys/bitstring.h>
984c7070dbSScott Long /*
9995246abbSSean Bruno  * enable accounting of every mbuf as it comes in to and goes out of
10095246abbSSean Bruno  * iflib's software descriptor references
1014c7070dbSScott Long  */
1024c7070dbSScott Long #define MEMORY_LOGGING 0
1034c7070dbSScott Long /*
1044c7070dbSScott Long  * Enable mbuf vectors for compressing long mbuf chains
1054c7070dbSScott Long  */
1064c7070dbSScott Long 
1074c7070dbSScott Long /*
1084c7070dbSScott Long  * NB:
1094c7070dbSScott Long  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
1104c7070dbSScott Long  *   we prefetch needs to be determined by the time spent in m_free vis a vis
1114c7070dbSScott Long  *   the cost of a prefetch. This will of course vary based on the workload:
1124c7070dbSScott Long  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
1134c7070dbSScott Long  *        is quite expensive, thus suggesting very little prefetch.
1144c7070dbSScott Long  *      - small packet forwarding which is just returning a single mbuf to
1154c7070dbSScott Long  *        UMA will typically be very fast vis a vis the cost of a memory
1164c7070dbSScott Long  *        access.
1174c7070dbSScott Long  */
1184c7070dbSScott Long 
1194c7070dbSScott Long 
1204c7070dbSScott Long /*
1214c7070dbSScott Long  * File organization:
1224c7070dbSScott Long  *  - private structures
1234c7070dbSScott Long  *  - iflib private utility functions
1244c7070dbSScott Long  *  - ifnet functions
1254c7070dbSScott Long  *  - vlan registry and other exported functions
1264c7070dbSScott Long  *  - iflib public core functions
1274c7070dbSScott Long  *
1284c7070dbSScott Long  *
1294c7070dbSScott Long  */
13009f6ff4fSMatt Macy MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
1314c7070dbSScott Long 
132fb1a29b4SHans Petter Selasky #define	IFLIB_RXEOF_MORE (1U << 0)
133fb1a29b4SHans Petter Selasky #define	IFLIB_RXEOF_EMPTY (2U << 0)
134fb1a29b4SHans Petter Selasky 
1354c7070dbSScott Long struct iflib_txq;
1364c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t;
1374c7070dbSScott Long struct iflib_rxq;
1384c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t;
1394c7070dbSScott Long struct iflib_fl;
1404c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t;
1414c7070dbSScott Long 
1424ecb427aSSean Bruno struct iflib_ctx;
1434ecb427aSSean Bruno 
1442d873474SStephen Hurd static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid);
145dd7fbcf1SStephen Hurd static void iflib_timer(void *arg);
1462d873474SStephen Hurd 
1474c7070dbSScott Long typedef struct iflib_filter_info {
1484c7070dbSScott Long 	driver_filter_t *ifi_filter;
1494c7070dbSScott Long 	void *ifi_filter_arg;
1504c7070dbSScott Long 	struct grouptask *ifi_task;
15195246abbSSean Bruno 	void *ifi_ctx;
1524c7070dbSScott Long } *iflib_filter_info_t;
1534c7070dbSScott Long 
1544c7070dbSScott Long struct iflib_ctx {
1554c7070dbSScott Long 	KOBJ_FIELDS;
1564c7070dbSScott Long 	/*
1574c7070dbSScott Long 	 * Pointer to hardware driver's softc
1584c7070dbSScott Long 	 */
1594c7070dbSScott Long 	void *ifc_softc;
1604c7070dbSScott Long 	device_t ifc_dev;
1614c7070dbSScott Long 	if_t ifc_ifp;
1624c7070dbSScott Long 
1634c7070dbSScott Long 	cpuset_t ifc_cpus;
1644c7070dbSScott Long 	if_shared_ctx_t ifc_sctx;
1654c7070dbSScott Long 	struct if_softc_ctx ifc_softc_ctx;
1664c7070dbSScott Long 
167aa8a24d3SStephen Hurd 	struct sx ifc_ctx_sx;
1687b610b60SSean Bruno 	struct mtx ifc_state_mtx;
1694c7070dbSScott Long 
1704c7070dbSScott Long 	iflib_txq_t ifc_txqs;
1714c7070dbSScott Long 	iflib_rxq_t ifc_rxqs;
1724c7070dbSScott Long 	uint32_t ifc_if_flags;
1734c7070dbSScott Long 	uint32_t ifc_flags;
1744c7070dbSScott Long 	uint32_t ifc_max_fl_buf_size;
1751b9d9394SEric Joyner 	uint32_t ifc_rx_mbuf_sz;
1764c7070dbSScott Long 
1774c7070dbSScott Long 	int ifc_link_state;
1784c7070dbSScott Long 	int ifc_watchdog_events;
1794c7070dbSScott Long 	struct cdev *ifc_led_dev;
1804c7070dbSScott Long 	struct resource *ifc_msix_mem;
1814c7070dbSScott Long 
1824c7070dbSScott Long 	struct if_irq ifc_legacy_irq;
1834c7070dbSScott Long 	struct grouptask ifc_admin_task;
1844c7070dbSScott Long 	struct grouptask ifc_vflr_task;
1854c7070dbSScott Long 	struct iflib_filter_info ifc_filter_info;
1864c7070dbSScott Long 	struct ifmedia	ifc_media;
187e2621d96SMatt Macy 	struct ifmedia	*ifc_mediap;
1884c7070dbSScott Long 
1894c7070dbSScott Long 	struct sysctl_oid *ifc_sysctl_node;
1904c7070dbSScott Long 	uint16_t ifc_sysctl_ntxqs;
1914c7070dbSScott Long 	uint16_t ifc_sysctl_nrxqs;
19223ac9029SStephen Hurd 	uint16_t ifc_sysctl_qs_eq_override;
193f4d2154eSStephen Hurd 	uint16_t ifc_sysctl_rx_budget;
194fe51d4cdSStephen Hurd 	uint16_t ifc_sysctl_tx_abdicate;
195f154ece0SStephen Hurd 	uint16_t ifc_sysctl_core_offset;
196f154ece0SStephen Hurd #define	CORE_OFFSET_UNSPECIFIED	0xffff
197f154ece0SStephen Hurd 	uint8_t  ifc_sysctl_separate_txrx;
19823ac9029SStephen Hurd 
19995246abbSSean Bruno 	qidx_t ifc_sysctl_ntxds[8];
20095246abbSSean Bruno 	qidx_t ifc_sysctl_nrxds[8];
2014c7070dbSScott Long 	struct if_txrx ifc_txrx;
2024c7070dbSScott Long #define isc_txd_encap  ifc_txrx.ift_txd_encap
2034c7070dbSScott Long #define isc_txd_flush  ifc_txrx.ift_txd_flush
2044c7070dbSScott Long #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
2054c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available
2064c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
2074c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2084c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush
2094c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2104c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2114c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr
2124c7070dbSScott Long 	eventhandler_tag ifc_vlan_attach_event;
2134c7070dbSScott Long 	eventhandler_tag ifc_vlan_detach_event;
2141fd8c72cSKyle Evans 	struct ether_addr ifc_mac;
2154c7070dbSScott Long };
2164c7070dbSScott Long 
2174c7070dbSScott Long void *
2184c7070dbSScott Long iflib_get_softc(if_ctx_t ctx)
2194c7070dbSScott Long {
2204c7070dbSScott Long 
2214c7070dbSScott Long 	return (ctx->ifc_softc);
2224c7070dbSScott Long }
2234c7070dbSScott Long 
2244c7070dbSScott Long device_t
2254c7070dbSScott Long iflib_get_dev(if_ctx_t ctx)
2264c7070dbSScott Long {
2274c7070dbSScott Long 
2284c7070dbSScott Long 	return (ctx->ifc_dev);
2294c7070dbSScott Long }
2304c7070dbSScott Long 
2314c7070dbSScott Long if_t
2324c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx)
2334c7070dbSScott Long {
2344c7070dbSScott Long 
2354c7070dbSScott Long 	return (ctx->ifc_ifp);
2364c7070dbSScott Long }
2374c7070dbSScott Long 
2384c7070dbSScott Long struct ifmedia *
2394c7070dbSScott Long iflib_get_media(if_ctx_t ctx)
2404c7070dbSScott Long {
2414c7070dbSScott Long 
242e2621d96SMatt Macy 	return (ctx->ifc_mediap);
2434c7070dbSScott Long }
2444c7070dbSScott Long 
24509f6ff4fSMatt Macy uint32_t
24609f6ff4fSMatt Macy iflib_get_flags(if_ctx_t ctx)
24709f6ff4fSMatt Macy {
24809f6ff4fSMatt Macy 	return (ctx->ifc_flags);
24909f6ff4fSMatt Macy }
25009f6ff4fSMatt Macy 
25109f6ff4fSMatt Macy void
2524c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
2534c7070dbSScott Long {
2544c7070dbSScott Long 
2551fd8c72cSKyle Evans 	bcopy(mac, ctx->ifc_mac.octet, ETHER_ADDR_LEN);
2564c7070dbSScott Long }
2574c7070dbSScott Long 
2584c7070dbSScott Long if_softc_ctx_t
2594c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx)
2604c7070dbSScott Long {
2614c7070dbSScott Long 
2624c7070dbSScott Long 	return (&ctx->ifc_softc_ctx);
2634c7070dbSScott Long }
2644c7070dbSScott Long 
2654c7070dbSScott Long if_shared_ctx_t
2664c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx)
2674c7070dbSScott Long {
2684c7070dbSScott Long 
2694c7070dbSScott Long 	return (ctx->ifc_sctx);
2704c7070dbSScott Long }
2714c7070dbSScott Long 
27295246abbSSean Bruno #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
2734c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
2745e888388SSean Bruno #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1)))
2754c7070dbSScott Long 
2764c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
2774c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
2784c7070dbSScott Long 
279e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array {
280e035717eSSean Bruno 	bus_dmamap_t	*ifsd_map;         /* bus_dma maps for packet */
281e035717eSSean Bruno 	struct mbuf	**ifsd_m;           /* pkthdr mbufs */
282e035717eSSean Bruno 	caddr_t		*ifsd_cl;          /* direct cluster pointer for rx */
283fbec776dSAndrew Gallatin 	bus_addr_t	*ifsd_ba;          /* bus addr of cluster for rx */
284e035717eSSean Bruno } iflib_rxsd_array_t;
2854c7070dbSScott Long 
2864c7070dbSScott Long typedef struct iflib_sw_tx_desc_array {
2874c7070dbSScott Long 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
2888a04b53dSKonstantin Belousov 	bus_dmamap_t	*ifsd_tso_map;     /* bus_dma maps for TSO packet */
2894c7070dbSScott Long 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
29095246abbSSean Bruno } if_txsd_vec_t;
2914c7070dbSScott Long 
2924c7070dbSScott Long /* magic number that should be high enough for any hardware */
2934c7070dbSScott Long #define IFLIB_MAX_TX_SEGS		128
29495246abbSSean Bruno #define IFLIB_RX_COPY_THRESH		128
2954c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH		32
29695246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */
29795246abbSSean Bruno #define IFLIB_MIN_DESC_SEC		16384
29895246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ	16
2994c7070dbSScott Long #define IFLIB_QUEUE_IDLE		0
3004c7070dbSScott Long #define IFLIB_QUEUE_HUNG		1
3014c7070dbSScott Long #define IFLIB_QUEUE_WORKING		2
30295246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */
30395246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR	4
3044c7070dbSScott Long 
30595246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */
30695246abbSSean Bruno #define TX_BATCH_SIZE			32
3074c7070dbSScott Long 
3084c7070dbSScott Long #define IFLIB_RESTART_BUDGET		8
3094c7070dbSScott Long 
3104c7070dbSScott Long #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
3114c7070dbSScott Long 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
3124c7070dbSScott Long 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
3131722eeacSMarius Strobl 
3144c7070dbSScott Long struct iflib_txq {
31595246abbSSean Bruno 	qidx_t		ift_in_use;
31695246abbSSean Bruno 	qidx_t		ift_cidx;
31795246abbSSean Bruno 	qidx_t		ift_cidx_processed;
31895246abbSSean Bruno 	qidx_t		ift_pidx;
3194c7070dbSScott Long 	uint8_t		ift_gen;
32023ac9029SStephen Hurd 	uint8_t		ift_br_offset;
32195246abbSSean Bruno 	uint16_t	ift_npending;
32295246abbSSean Bruno 	uint16_t	ift_db_pending;
32395246abbSSean Bruno 	uint16_t	ift_rs_pending;
3244c7070dbSScott Long 	/* implicit pad */
32595246abbSSean Bruno 	uint8_t		ift_txd_size[8];
3264c7070dbSScott Long 	uint64_t	ift_processed;
3274c7070dbSScott Long 	uint64_t	ift_cleaned;
32895246abbSSean Bruno 	uint64_t	ift_cleaned_prev;
3294c7070dbSScott Long #if MEMORY_LOGGING
3304c7070dbSScott Long 	uint64_t	ift_enqueued;
3314c7070dbSScott Long 	uint64_t	ift_dequeued;
3324c7070dbSScott Long #endif
3334c7070dbSScott Long 	uint64_t	ift_no_tx_dma_setup;
3344c7070dbSScott Long 	uint64_t	ift_no_desc_avail;
3354c7070dbSScott Long 	uint64_t	ift_mbuf_defrag_failed;
3364c7070dbSScott Long 	uint64_t	ift_mbuf_defrag;
3374c7070dbSScott Long 	uint64_t	ift_map_failed;
3384c7070dbSScott Long 	uint64_t	ift_txd_encap_efbig;
3394c7070dbSScott Long 	uint64_t	ift_pullups;
340dd7fbcf1SStephen Hurd 	uint64_t	ift_last_timer_tick;
3414c7070dbSScott Long 
3424c7070dbSScott Long 	struct mtx	ift_mtx;
3434c7070dbSScott Long 	struct mtx	ift_db_mtx;
3444c7070dbSScott Long 
3454c7070dbSScott Long 	/* constant values */
3464c7070dbSScott Long 	if_ctx_t	ift_ctx;
34795246abbSSean Bruno 	struct ifmp_ring        *ift_br;
3484c7070dbSScott Long 	struct grouptask	ift_task;
34995246abbSSean Bruno 	qidx_t		ift_size;
3504c7070dbSScott Long 	uint16_t	ift_id;
3514c7070dbSScott Long 	struct callout	ift_timer;
3524c7070dbSScott Long 
35395246abbSSean Bruno 	if_txsd_vec_t	ift_sds;
3544c7070dbSScott Long 	uint8_t		ift_qstatus;
3554c7070dbSScott Long 	uint8_t		ift_closed;
35695246abbSSean Bruno 	uint8_t		ift_update_freq;
3574c7070dbSScott Long 	struct iflib_filter_info ift_filter_info;
358bfce461eSMarius Strobl 	bus_dma_tag_t	ift_buf_tag;
359bfce461eSMarius Strobl 	bus_dma_tag_t	ift_tso_buf_tag;
3604c7070dbSScott Long 	iflib_dma_info_t	ift_ifdi;
361*814fa34dSMark Johnston #define	MTX_NAME_LEN	32
3624c7070dbSScott Long 	char                    ift_mtx_name[MTX_NAME_LEN];
3634c7070dbSScott Long 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
3641248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
3651248952aSSean Bruno 	uint64_t ift_cpu_exec_count[256];
3661248952aSSean Bruno #endif
3674c7070dbSScott Long } __aligned(CACHE_LINE_SIZE);
3684c7070dbSScott Long 
3694c7070dbSScott Long struct iflib_fl {
37095246abbSSean Bruno 	qidx_t		ifl_cidx;
37195246abbSSean Bruno 	qidx_t		ifl_pidx;
37295246abbSSean Bruno 	qidx_t		ifl_credits;
3734c7070dbSScott Long 	uint8_t		ifl_gen;
37495246abbSSean Bruno 	uint8_t		ifl_rxd_size;
3754c7070dbSScott Long #if MEMORY_LOGGING
3764c7070dbSScott Long 	uint64_t	ifl_m_enqueued;
3774c7070dbSScott Long 	uint64_t	ifl_m_dequeued;
3784c7070dbSScott Long 	uint64_t	ifl_cl_enqueued;
3794c7070dbSScott Long 	uint64_t	ifl_cl_dequeued;
3804c7070dbSScott Long #endif
3814c7070dbSScott Long 	/* implicit pad */
38287890dbaSSean Bruno 	bitstr_t 	*ifl_rx_bitmap;
38387890dbaSSean Bruno 	qidx_t		ifl_fragidx;
3844c7070dbSScott Long 	/* constant */
38595246abbSSean Bruno 	qidx_t		ifl_size;
3864c7070dbSScott Long 	uint16_t	ifl_buf_size;
3874c7070dbSScott Long 	uint16_t	ifl_cltype;
3884c7070dbSScott Long 	uma_zone_t	ifl_zone;
389e035717eSSean Bruno 	iflib_rxsd_array_t	ifl_sds;
3904c7070dbSScott Long 	iflib_rxq_t	ifl_rxq;
3914c7070dbSScott Long 	uint8_t		ifl_id;
392bfce461eSMarius Strobl 	bus_dma_tag_t	ifl_buf_tag;
3934c7070dbSScott Long 	iflib_dma_info_t	ifl_ifdi;
3944c7070dbSScott Long 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
3954c7070dbSScott Long 	caddr_t		ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
39695246abbSSean Bruno 	qidx_t	ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
3974c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
3984c7070dbSScott Long 
39995246abbSSean Bruno static inline qidx_t
40095246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
4014c7070dbSScott Long {
40295246abbSSean Bruno 	qidx_t used;
4034c7070dbSScott Long 
4044c7070dbSScott Long 	if (pidx > cidx)
4054c7070dbSScott Long 		used = pidx - cidx;
4064c7070dbSScott Long 	else if (pidx < cidx)
4074c7070dbSScott Long 		used = size - cidx + pidx;
4084c7070dbSScott Long 	else if (gen == 0 && pidx == cidx)
4094c7070dbSScott Long 		used = 0;
4104c7070dbSScott Long 	else if (gen == 1 && pidx == cidx)
4114c7070dbSScott Long 		used = size;
4124c7070dbSScott Long 	else
4134c7070dbSScott Long 		panic("bad state");
4144c7070dbSScott Long 
4154c7070dbSScott Long 	return (used);
4164c7070dbSScott Long }
4174c7070dbSScott Long 
4184c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
4194c7070dbSScott Long 
4204c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \
4214c7070dbSScott Long 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
4224c7070dbSScott Long 
4234c7070dbSScott Long struct iflib_rxq {
4244c7070dbSScott Long 	if_ctx_t	ifr_ctx;
4254c7070dbSScott Long 	iflib_fl_t	ifr_fl;
4264c7070dbSScott Long 	uint64_t	ifr_rx_irq;
4276d49b41eSAndrew Gallatin 	struct pfil_head	*pfil;
4281722eeacSMarius Strobl 	/*
4291722eeacSMarius Strobl 	 * If there is a separate completion queue (IFLIB_HAS_RXCQ), this is
4301722eeacSMarius Strobl 	 * the command queue consumer index.  Otherwise it's unused.
4311722eeacSMarius Strobl 	 */
4321722eeacSMarius Strobl 	qidx_t		ifr_cq_cidx;
4334c7070dbSScott Long 	uint16_t	ifr_id;
4344c7070dbSScott Long 	uint8_t		ifr_nfl;
43595246abbSSean Bruno 	uint8_t		ifr_ntxqirq;
43695246abbSSean Bruno 	uint8_t		ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
4371722eeacSMarius Strobl 	uint8_t		ifr_fl_offset;
4384c7070dbSScott Long 	struct lro_ctrl			ifr_lc;
4394c7070dbSScott Long 	struct grouptask        ifr_task;
440fb1a29b4SHans Petter Selasky 	struct callout		ifr_watchdog;
4414c7070dbSScott Long 	struct iflib_filter_info ifr_filter_info;
4424c7070dbSScott Long 	iflib_dma_info_t		ifr_ifdi;
443ab2e3f79SStephen Hurd 
4444c7070dbSScott Long 	/* dynamically allocate if any drivers need a value substantially larger than this */
4454c7070dbSScott Long 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
4461248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
4471248952aSSean Bruno 	uint64_t ifr_cpu_exec_count[256];
4481248952aSSean Bruno #endif
4494c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4504c7070dbSScott Long 
45195246abbSSean Bruno typedef struct if_rxsd {
45295246abbSSean Bruno 	caddr_t *ifsd_cl;
45395246abbSSean Bruno 	iflib_fl_t ifsd_fl;
45495246abbSSean Bruno } *if_rxsd_t;
45595246abbSSean Bruno 
45695246abbSSean Bruno /* multiple of word size */
45795246abbSSean Bruno #ifdef __LP64__
458ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	6
45995246abbSSean Bruno #define RXD_INFO_SIZE	5
46095246abbSSean Bruno #define PKT_TYPE uint64_t
46195246abbSSean Bruno #else
462ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	11
46395246abbSSean Bruno #define RXD_INFO_SIZE	8
46495246abbSSean Bruno #define PKT_TYPE uint32_t
46595246abbSSean Bruno #endif
46695246abbSSean Bruno #define PKT_LOOP_BOUND  ((PKT_INFO_SIZE/3)*3)
46795246abbSSean Bruno #define RXD_LOOP_BOUND  ((RXD_INFO_SIZE/4)*4)
46895246abbSSean Bruno 
46995246abbSSean Bruno typedef struct if_pkt_info_pad {
47095246abbSSean Bruno 	PKT_TYPE pkt_val[PKT_INFO_SIZE];
47195246abbSSean Bruno } *if_pkt_info_pad_t;
47295246abbSSean Bruno typedef struct if_rxd_info_pad {
47395246abbSSean Bruno 	PKT_TYPE rxd_val[RXD_INFO_SIZE];
47495246abbSSean Bruno } *if_rxd_info_pad_t;
47595246abbSSean Bruno 
47695246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info));
47795246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info));
47895246abbSSean Bruno 
47995246abbSSean Bruno 
48095246abbSSean Bruno static inline void
48195246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi)
48295246abbSSean Bruno {
48395246abbSSean Bruno 	if_pkt_info_pad_t pi_pad;
48495246abbSSean Bruno 
48595246abbSSean Bruno 	pi_pad = (if_pkt_info_pad_t)pi;
48695246abbSSean Bruno 	pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0;
48795246abbSSean Bruno 	pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0;
48895246abbSSean Bruno #ifndef __LP64__
489ab2e3f79SStephen Hurd 	pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0;
490ab2e3f79SStephen Hurd 	pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0;
49195246abbSSean Bruno #endif
49295246abbSSean Bruno }
49395246abbSSean Bruno 
49409f6ff4fSMatt Macy static device_method_t iflib_pseudo_methods[] = {
49509f6ff4fSMatt Macy 	DEVMETHOD(device_attach, noop_attach),
49609f6ff4fSMatt Macy 	DEVMETHOD(device_detach, iflib_pseudo_detach),
49709f6ff4fSMatt Macy 	DEVMETHOD_END
49809f6ff4fSMatt Macy };
49909f6ff4fSMatt Macy 
50009f6ff4fSMatt Macy driver_t iflib_pseudodriver = {
50109f6ff4fSMatt Macy 	"iflib_pseudo", iflib_pseudo_methods, sizeof(struct iflib_ctx),
50209f6ff4fSMatt Macy };
50309f6ff4fSMatt Macy 
50495246abbSSean Bruno static inline void
50595246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri)
50695246abbSSean Bruno {
50795246abbSSean Bruno 	if_rxd_info_pad_t ri_pad;
50895246abbSSean Bruno 	int i;
50995246abbSSean Bruno 
51095246abbSSean Bruno 	ri_pad = (if_rxd_info_pad_t)ri;
51195246abbSSean Bruno 	for (i = 0; i < RXD_LOOP_BOUND; i += 4) {
51295246abbSSean Bruno 		ri_pad->rxd_val[i] = 0;
51395246abbSSean Bruno 		ri_pad->rxd_val[i+1] = 0;
51495246abbSSean Bruno 		ri_pad->rxd_val[i+2] = 0;
51595246abbSSean Bruno 		ri_pad->rxd_val[i+3] = 0;
51695246abbSSean Bruno 	}
51795246abbSSean Bruno #ifdef __LP64__
51895246abbSSean Bruno 	ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0;
51995246abbSSean Bruno #endif
52095246abbSSean Bruno }
52195246abbSSean Bruno 
5224c7070dbSScott Long /*
5234c7070dbSScott Long  * Only allow a single packet to take up most 1/nth of the tx ring
5244c7070dbSScott Long  */
5254c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12
5264c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1
5274c7070dbSScott Long 
5284c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
5294c7070dbSScott Long 
530aa8a24d3SStephen Hurd #define CTX_LOCK_INIT(_sc)  sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock")
531aa8a24d3SStephen Hurd #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx)
532aa8a24d3SStephen Hurd #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx)
533aa8a24d3SStephen Hurd #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx)
5344c7070dbSScott Long 
5357b610b60SSean Bruno #define STATE_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF)
5367b610b60SSean Bruno #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx)
5377b610b60SSean Bruno #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx)
5387b610b60SSean Bruno #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx)
5397b610b60SSean Bruno 
5404c7070dbSScott Long #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
5414c7070dbSScott Long #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
5424c7070dbSScott Long 
54377c1fcecSEric Joyner void
54477c1fcecSEric Joyner iflib_set_detach(if_ctx_t ctx)
54577c1fcecSEric Joyner {
54677c1fcecSEric Joyner 	STATE_LOCK(ctx);
54777c1fcecSEric Joyner 	ctx->ifc_flags |= IFC_IN_DETACH;
54877c1fcecSEric Joyner 	STATE_UNLOCK(ctx);
54977c1fcecSEric Joyner }
5504c7070dbSScott Long 
5514c7070dbSScott Long /* Our boot-time initialization hook */
5524c7070dbSScott Long static int	iflib_module_event_handler(module_t, int, void *);
5534c7070dbSScott Long 
5544c7070dbSScott Long static moduledata_t iflib_moduledata = {
5554c7070dbSScott Long 	"iflib",
5564c7070dbSScott Long 	iflib_module_event_handler,
5574c7070dbSScott Long 	NULL
5584c7070dbSScott Long };
5594c7070dbSScott Long 
5604c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
5614c7070dbSScott Long MODULE_VERSION(iflib, 1);
5624c7070dbSScott Long 
5634c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1);
5644c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1);
5654c7070dbSScott Long 
566ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
567ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
568ab2e3f79SStephen Hurd 
5694c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS
5704c7070dbSScott Long #ifdef INVARIANTS
5714c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1
5724c7070dbSScott Long #else
5734c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0
5744c7070dbSScott Long #endif /* !INVARIANTS */
5754c7070dbSScott Long #endif
5764c7070dbSScott Long 
5777029da5cSPawel Biernacki static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
578ab2e3f79SStephen Hurd     "iflib driver parameters");
579ab2e3f79SStephen Hurd 
5804c7070dbSScott Long /*
5814c7070dbSScott Long  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
5824c7070dbSScott Long  */
5834c7070dbSScott Long static int iflib_min_tx_latency = 0;
5844c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
585da69b8f9SSean Bruno 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
58695246abbSSean Bruno static int iflib_no_tx_batch = 0;
58795246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
58895246abbSSean Bruno 		   &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput");
5894c7070dbSScott Long 
5904c7070dbSScott Long 
5914c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS
5924c7070dbSScott Long 
5934c7070dbSScott Long static int iflib_tx_seen;
5944c7070dbSScott Long static int iflib_tx_sent;
5954c7070dbSScott Long static int iflib_tx_encap;
5964c7070dbSScott Long static int iflib_rx_allocs;
5974c7070dbSScott Long static int iflib_fl_refills;
5984c7070dbSScott Long static int iflib_fl_refills_large;
5994c7070dbSScott Long static int iflib_tx_frees;
6004c7070dbSScott Long 
6014c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
6021722eeacSMarius Strobl 		   &iflib_tx_seen, 0, "# TX mbufs seen");
6034c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
6041722eeacSMarius Strobl 		   &iflib_tx_sent, 0, "# TX mbufs sent");
6054c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
6061722eeacSMarius Strobl 		   &iflib_tx_encap, 0, "# TX mbufs encapped");
6074c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
6081722eeacSMarius Strobl 		   &iflib_tx_frees, 0, "# TX frees");
6094c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
6101722eeacSMarius Strobl 		   &iflib_rx_allocs, 0, "# RX allocations");
6114c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
6124c7070dbSScott Long 		   &iflib_fl_refills, 0, "# refills");
6134c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
6144c7070dbSScott Long 		   &iflib_fl_refills_large, 0, "# large refills");
6154c7070dbSScott Long 
6164c7070dbSScott Long 
6174c7070dbSScott Long static int iflib_txq_drain_flushing;
6184c7070dbSScott Long static int iflib_txq_drain_oactive;
6194c7070dbSScott Long static int iflib_txq_drain_notready;
6204c7070dbSScott Long 
6214c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
6224c7070dbSScott Long 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
6234c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
6244c7070dbSScott Long 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
6254c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
6264c7070dbSScott Long 		   &iflib_txq_drain_notready, 0, "# drain notready");
6274c7070dbSScott Long 
6284c7070dbSScott Long 
6294c7070dbSScott Long static int iflib_encap_load_mbuf_fail;
630d14c853bSStephen Hurd static int iflib_encap_pad_mbuf_fail;
6314c7070dbSScott Long static int iflib_encap_txq_avail_fail;
6324c7070dbSScott Long static int iflib_encap_txd_encap_fail;
6334c7070dbSScott Long 
6344c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
6354c7070dbSScott Long 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
636d14c853bSStephen Hurd SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD,
637d14c853bSStephen Hurd 		   &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures");
6384c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
6394c7070dbSScott Long 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
6404c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
6414c7070dbSScott Long 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
6424c7070dbSScott Long 
6434c7070dbSScott Long static int iflib_task_fn_rxs;
6444c7070dbSScott Long static int iflib_rx_intr_enables;
6454c7070dbSScott Long static int iflib_fast_intrs;
6464c7070dbSScott Long static int iflib_rx_unavail;
6474c7070dbSScott Long static int iflib_rx_ctx_inactive;
6484c7070dbSScott Long static int iflib_rx_if_input;
6494c7070dbSScott Long static int iflib_rxd_flush;
6504c7070dbSScott Long 
6514c7070dbSScott Long static int iflib_verbose_debug;
6524c7070dbSScott Long 
6534c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
6544c7070dbSScott Long 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
6554c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
6561722eeacSMarius Strobl 		   &iflib_rx_intr_enables, 0, "# RX intr enables");
6574c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
6584c7070dbSScott Long 		   &iflib_fast_intrs, 0, "# fast_intr calls");
6594c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
6604c7070dbSScott Long 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
6614c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
6624c7070dbSScott Long 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
6634c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
6644c7070dbSScott Long 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
6654c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
6664c7070dbSScott Long 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
6674c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
6684c7070dbSScott Long 		   &iflib_verbose_debug, 0, "enable verbose debugging");
6694c7070dbSScott Long 
6704c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
671da69b8f9SSean Bruno static void
672da69b8f9SSean Bruno iflib_debug_reset(void)
673da69b8f9SSean Bruno {
674da69b8f9SSean Bruno 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
675da69b8f9SSean Bruno 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
676da69b8f9SSean Bruno 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
67764e6fc13SStephen Hurd 		iflib_txq_drain_notready =
678d14c853bSStephen Hurd 		iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail =
679d14c853bSStephen Hurd 		iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail =
680d14c853bSStephen Hurd 		iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs =
68164e6fc13SStephen Hurd 		iflib_rx_unavail =
68264e6fc13SStephen Hurd 		iflib_rx_ctx_inactive = iflib_rx_if_input =
6836d49b41eSAndrew Gallatin 		iflib_rxd_flush = 0;
684da69b8f9SSean Bruno }
6854c7070dbSScott Long 
6864c7070dbSScott Long #else
6874c7070dbSScott Long #define DBG_COUNTER_INC(name)
688da69b8f9SSean Bruno static void iflib_debug_reset(void) {}
6894c7070dbSScott Long #endif
6904c7070dbSScott Long 
6914c7070dbSScott Long #define IFLIB_DEBUG 0
6924c7070dbSScott Long 
6934c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx);
6944c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx);
6954c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx);
6964c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
69795246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
6984c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx);
6994c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx);
7003e0e6330SStephen Hurd static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str);
7014c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
7024c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
703b8ca4756SPatrick Kelsey #ifdef ALTQ
704b8ca4756SPatrick Kelsey static void iflib_altq_if_start(if_t ifp);
705b8ca4756SPatrick Kelsey static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m);
706b8ca4756SPatrick Kelsey #endif
7074c7070dbSScott Long static int iflib_register(if_ctx_t);
70856614414SEric Joyner static void iflib_deregister(if_ctx_t);
7091558015eSEric Joyner static void iflib_unregister_vlan_handlers(if_ctx_t ctx);
710b3813609SPatrick Kelsey static uint16_t iflib_get_mbuf_size_for(unsigned int size);
7114c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx);
7124c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
7134c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx);
714da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq);
7151248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx);
71695246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx);
71777c1fcecSEric Joyner static void iflib_free_intr_mem(if_ctx_t ctx);
71895246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
71995246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m);
72095246abbSSean Bruno #endif
7214c7070dbSScott Long 
722f154ece0SStephen Hurd static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets =
723f154ece0SStephen Hurd     SLIST_HEAD_INITIALIZER(cpu_offsets);
724f154ece0SStephen Hurd struct cpu_offset {
725f154ece0SStephen Hurd 	SLIST_ENTRY(cpu_offset) entries;
726f154ece0SStephen Hurd 	cpuset_t	set;
727f154ece0SStephen Hurd 	unsigned int	refcount;
728f154ece0SStephen Hurd 	uint16_t	offset;
729f154ece0SStephen Hurd };
730f154ece0SStephen Hurd static struct mtx cpu_offset_mtx;
731f154ece0SStephen Hurd MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock",
732f154ece0SStephen Hurd     MTX_DEF);
733f154ece0SStephen Hurd 
7347790c8c1SConrad Meyer DEBUGNET_DEFINE(iflib);
73594618825SMark Johnston 
7364c7070dbSScott Long #ifdef DEV_NETMAP
7374c7070dbSScott Long #include <sys/selinfo.h>
7384c7070dbSScott Long #include <net/netmap.h>
7394c7070dbSScott Long #include <dev/netmap/netmap_kern.h>
7404c7070dbSScott Long 
7414c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1);
7424c7070dbSScott Long 
7432d873474SStephen Hurd static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init);
7442d873474SStephen Hurd 
7454c7070dbSScott Long /*
7464c7070dbSScott Long  * device-specific sysctl variables:
7474c7070dbSScott Long  *
74891d546a0SConrad Meyer  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
7494c7070dbSScott Long  *	During regular operations the CRC is stripped, but on some
7504c7070dbSScott Long  *	hardware reception of frames not multiple of 64 is slower,
7514c7070dbSScott Long  *	so using crcstrip=0 helps in benchmarks.
7524c7070dbSScott Long  *
75391d546a0SConrad Meyer  * iflib_rx_miss, iflib_rx_miss_bufs:
7544c7070dbSScott Long  *	count packets that might be missed due to lost interrupts.
7554c7070dbSScott Long  */
7564c7070dbSScott Long SYSCTL_DECL(_dev_netmap);
7574c7070dbSScott Long /*
7584c7070dbSScott Long  * The xl driver by default strips CRCs and we do not override it.
7594c7070dbSScott Long  */
7604c7070dbSScott Long 
7614c7070dbSScott Long int iflib_crcstrip = 1;
7624c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
7631722eeacSMarius Strobl     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames");
7644c7070dbSScott Long 
7654c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs;
7664c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
7671722eeacSMarius Strobl     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr");
76891d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
7691722eeacSMarius Strobl     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs");
7704c7070dbSScott Long 
7714c7070dbSScott Long /*
7724c7070dbSScott Long  * Register/unregister. We are already under netmap lock.
7734c7070dbSScott Long  * Only called on the first register or the last unregister.
7744c7070dbSScott Long  */
7754c7070dbSScott Long static int
7764c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff)
7774c7070dbSScott Long {
7781722eeacSMarius Strobl 	if_t ifp = na->ifp;
7794c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
78095246abbSSean Bruno 	int status;
7814c7070dbSScott Long 
7824c7070dbSScott Long 	CTX_LOCK(ctx);
7834c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
7844c7070dbSScott Long 
7854c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
7864c7070dbSScott Long 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
7874c7070dbSScott Long 
7884c7070dbSScott Long 	if (!CTX_IS_VF(ctx))
7891248952aSSean Bruno 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
7904c7070dbSScott Long 
7914c7070dbSScott Long 	/* enable or disable flags and callbacks in na and ifp */
7924c7070dbSScott Long 	if (onoff) {
7934c7070dbSScott Long 		nm_set_native_flags(na);
7944c7070dbSScott Long 	} else {
7954c7070dbSScott Long 		nm_clear_native_flags(na);
7964c7070dbSScott Long 	}
79795246abbSSean Bruno 	iflib_stop(ctx);
79895246abbSSean Bruno 	iflib_init_locked(ctx);
7991248952aSSean Bruno 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
80095246abbSSean Bruno 	status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1;
80195246abbSSean Bruno 	if (status)
80295246abbSSean Bruno 		nm_clear_native_flags(na);
8034c7070dbSScott Long 	CTX_UNLOCK(ctx);
80495246abbSSean Bruno 	return (status);
8054c7070dbSScott Long }
8064c7070dbSScott Long 
8072d873474SStephen Hurd static int
8082d873474SStephen Hurd netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init)
8092d873474SStephen Hurd {
8102d873474SStephen Hurd 	struct netmap_adapter *na = kring->na;
8112d873474SStephen Hurd 	u_int const lim = kring->nkr_num_slots - 1;
8122d873474SStephen Hurd 	u_int head = kring->rhead;
8132d873474SStephen Hurd 	struct netmap_ring *ring = kring->ring;
8142d873474SStephen Hurd 	bus_dmamap_t *map;
8152d873474SStephen Hurd 	struct if_rxd_update iru;
8162d873474SStephen Hurd 	if_ctx_t ctx = rxq->ifr_ctx;
8172d873474SStephen Hurd 	iflib_fl_t fl = &rxq->ifr_fl[0];
8182d873474SStephen Hurd 	uint32_t refill_pidx, nic_i;
81964e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS
82064e6fc13SStephen Hurd 	int rf_count = 0;
82164e6fc13SStephen Hurd #endif
8222d873474SStephen Hurd 
8232d873474SStephen Hurd 	if (nm_i == head && __predict_true(!init))
8242d873474SStephen Hurd 		return 0;
8252d873474SStephen Hurd 	iru_init(&iru, rxq, 0 /* flid */);
8262d873474SStephen Hurd 	map = fl->ifl_sds.ifsd_map;
8272d873474SStephen Hurd 	refill_pidx = netmap_idx_k2n(kring, nm_i);
8282d873474SStephen Hurd 	/*
8292d873474SStephen Hurd 	 * IMPORTANT: we must leave one free slot in the ring,
8302d873474SStephen Hurd 	 * so move head back by one unit
8312d873474SStephen Hurd 	 */
8322d873474SStephen Hurd 	head = nm_prev(head, lim);
8331ae4848cSMatt Macy 	nic_i = UINT_MAX;
83464e6fc13SStephen Hurd 	DBG_COUNTER_INC(fl_refills);
8352d873474SStephen Hurd 	while (nm_i != head) {
83664e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS
83764e6fc13SStephen Hurd 		if (++rf_count == 9)
83864e6fc13SStephen Hurd 			DBG_COUNTER_INC(fl_refills_large);
83964e6fc13SStephen Hurd #endif
8402d873474SStephen Hurd 		for (int tmp_pidx = 0; tmp_pidx < IFLIB_MAX_RX_REFRESH && nm_i != head; tmp_pidx++) {
8412d873474SStephen Hurd 			struct netmap_slot *slot = &ring->slot[nm_i];
8422d873474SStephen Hurd 			void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[tmp_pidx]);
8432d873474SStephen Hurd 			uint32_t nic_i_dma = refill_pidx;
8442d873474SStephen Hurd 			nic_i = netmap_idx_k2n(kring, nm_i);
8452d873474SStephen Hurd 
8462d873474SStephen Hurd 			MPASS(tmp_pidx < IFLIB_MAX_RX_REFRESH);
8472d873474SStephen Hurd 
8482d873474SStephen Hurd 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
8492d873474SStephen Hurd 			        return netmap_ring_reinit(kring);
8502d873474SStephen Hurd 
8512d873474SStephen Hurd 			fl->ifl_vm_addrs[tmp_pidx] = addr;
85295dcf343SMarius Strobl 			if (__predict_false(init)) {
85395dcf343SMarius Strobl 				netmap_load_map(na, fl->ifl_buf_tag,
85495dcf343SMarius Strobl 				    map[nic_i], addr);
85595dcf343SMarius Strobl 			} else if (slot->flags & NS_BUF_CHANGED) {
8562d873474SStephen Hurd 				/* buffer has changed, reload map */
85795dcf343SMarius Strobl 				netmap_reload_map(na, fl->ifl_buf_tag,
85895dcf343SMarius Strobl 				    map[nic_i], addr);
8592d873474SStephen Hurd 			}
8602d873474SStephen Hurd 			slot->flags &= ~NS_BUF_CHANGED;
8612d873474SStephen Hurd 
8622d873474SStephen Hurd 			nm_i = nm_next(nm_i, lim);
8632d873474SStephen Hurd 			fl->ifl_rxd_idxs[tmp_pidx] = nic_i = nm_next(nic_i, lim);
8642d873474SStephen Hurd 			if (nm_i != head && tmp_pidx < IFLIB_MAX_RX_REFRESH-1)
8652d873474SStephen Hurd 				continue;
8662d873474SStephen Hurd 
8672d873474SStephen Hurd 			iru.iru_pidx = refill_pidx;
8682d873474SStephen Hurd 			iru.iru_count = tmp_pidx+1;
8692d873474SStephen Hurd 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
8702d873474SStephen Hurd 			refill_pidx = nic_i;
8712d873474SStephen Hurd 			for (int n = 0; n < iru.iru_count; n++) {
87295dcf343SMarius Strobl 				bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i_dma],
8732d873474SStephen Hurd 						BUS_DMASYNC_PREREAD);
8742d873474SStephen Hurd 				/* XXX - change this to not use the netmap func*/
8752d873474SStephen Hurd 				nic_i_dma = nm_next(nic_i_dma, lim);
8762d873474SStephen Hurd 			}
8772d873474SStephen Hurd 		}
8782d873474SStephen Hurd 	}
8792d873474SStephen Hurd 	kring->nr_hwcur = head;
8802d873474SStephen Hurd 
8812d873474SStephen Hurd 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
8822d873474SStephen Hurd 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
88364e6fc13SStephen Hurd 	if (__predict_true(nic_i != UINT_MAX)) {
8842d873474SStephen Hurd 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
88564e6fc13SStephen Hurd 		DBG_COUNTER_INC(rxd_flush);
88664e6fc13SStephen Hurd 	}
8872d873474SStephen Hurd 	return (0);
8882d873474SStephen Hurd }
8892d873474SStephen Hurd 
8904c7070dbSScott Long /*
8914c7070dbSScott Long  * Reconcile kernel and user view of the transmit ring.
8924c7070dbSScott Long  *
8934c7070dbSScott Long  * All information is in the kring.
8944c7070dbSScott Long  * Userspace wants to send packets up to the one before kring->rhead,
8954c7070dbSScott Long  * kernel knows kring->nr_hwcur is the first unsent packet.
8964c7070dbSScott Long  *
8974c7070dbSScott Long  * Here we push packets out (as many as possible), and possibly
8984c7070dbSScott Long  * reclaim buffers from previously completed transmission.
8994c7070dbSScott Long  *
9004c7070dbSScott Long  * The caller (netmap) guarantees that there is only one instance
9014c7070dbSScott Long  * running at any time. Any interference with other driver
9024c7070dbSScott Long  * methods should be handled by the individual drivers.
9034c7070dbSScott Long  */
9044c7070dbSScott Long static int
9054c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags)
9064c7070dbSScott Long {
9074c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
9081722eeacSMarius Strobl 	if_t ifp = na->ifp;
9094c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
910dd7fbcf1SStephen Hurd 	u_int nm_i;	/* index into the netmap kring */
9114c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
9124c7070dbSScott Long 	u_int n;
9134c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
9144c7070dbSScott Long 	u_int const head = kring->rhead;
9154c7070dbSScott Long 	struct if_pkt_info pi;
9164c7070dbSScott Long 
9174c7070dbSScott Long 	/*
9184c7070dbSScott Long 	 * interrupts on every tx packet are expensive so request
9194c7070dbSScott Long 	 * them every half ring, or where NS_REPORT is set
9204c7070dbSScott Long 	 */
9214c7070dbSScott Long 	u_int report_frequency = kring->nkr_num_slots >> 1;
9224c7070dbSScott Long 	/* device-specific */
9234c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
9244c7070dbSScott Long 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
9254c7070dbSScott Long 
92695dcf343SMarius Strobl 	bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
9274c7070dbSScott Long 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9284c7070dbSScott Long 
9294c7070dbSScott Long 	/*
9304c7070dbSScott Long 	 * First part: process new packets to send.
931dd7fbcf1SStephen Hurd 	 * nm_i is the current index in the netmap kring,
9324c7070dbSScott Long 	 * nic_i is the corresponding index in the NIC ring.
9334c7070dbSScott Long 	 *
9344c7070dbSScott Long 	 * If we have packets to send (nm_i != head)
9354c7070dbSScott Long 	 * iterate over the netmap ring, fetch length and update
9364c7070dbSScott Long 	 * the corresponding slot in the NIC ring. Some drivers also
9374c7070dbSScott Long 	 * need to update the buffer's physical address in the NIC slot
9384c7070dbSScott Long 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
9394c7070dbSScott Long 	 *
9404c7070dbSScott Long 	 * The netmap_reload_map() calls is especially expensive,
9414c7070dbSScott Long 	 * even when (as in this case) the tag is 0, so do only
9424c7070dbSScott Long 	 * when the buffer has actually changed.
9434c7070dbSScott Long 	 *
9444c7070dbSScott Long 	 * If possible do not set the report/intr bit on all slots,
9454c7070dbSScott Long 	 * but only a few times per ring or when NS_REPORT is set.
9464c7070dbSScott Long 	 *
9474c7070dbSScott Long 	 * Finally, on 10G and faster drivers, it might be useful
9484c7070dbSScott Long 	 * to prefetch the next slot and txr entry.
9494c7070dbSScott Long 	 */
9504c7070dbSScott Long 
951dd7fbcf1SStephen Hurd 	nm_i = kring->nr_hwcur;
9525ee36c68SStephen Hurd 	if (nm_i != head) {	/* we have new packets to send */
95395246abbSSean Bruno 		pkt_info_zero(&pi);
95495246abbSSean Bruno 		pi.ipi_segs = txq->ift_segs;
95595246abbSSean Bruno 		pi.ipi_qsidx = kring->ring_id;
9564c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
9574c7070dbSScott Long 
9584c7070dbSScott Long 		__builtin_prefetch(&ring->slot[nm_i]);
9594c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
9604c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
9614c7070dbSScott Long 
9624c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
9634c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
9644c7070dbSScott Long 			u_int len = slot->len;
9650a1b74a3SSean Bruno 			uint64_t paddr;
9664c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
9674c7070dbSScott Long 			int flags = (slot->flags & NS_REPORT ||
9684c7070dbSScott Long 				nic_i == 0 || nic_i == report_frequency) ?
9694c7070dbSScott Long 				IPI_TX_INTR : 0;
9704c7070dbSScott Long 
9714c7070dbSScott Long 			/* device-specific */
97295246abbSSean Bruno 			pi.ipi_len = len;
97395246abbSSean Bruno 			pi.ipi_segs[0].ds_addr = paddr;
97495246abbSSean Bruno 			pi.ipi_segs[0].ds_len = len;
97595246abbSSean Bruno 			pi.ipi_nsegs = 1;
97695246abbSSean Bruno 			pi.ipi_ndescs = 0;
9774c7070dbSScott Long 			pi.ipi_pidx = nic_i;
9784c7070dbSScott Long 			pi.ipi_flags = flags;
9794c7070dbSScott Long 
9804c7070dbSScott Long 			/* Fill the slot in the NIC ring. */
9814c7070dbSScott Long 			ctx->isc_txd_encap(ctx->ifc_softc, &pi);
98264e6fc13SStephen Hurd 			DBG_COUNTER_INC(tx_encap);
9834c7070dbSScott Long 
9844c7070dbSScott Long 			/* prefetch for next round */
9854c7070dbSScott Long 			__builtin_prefetch(&ring->slot[nm_i + 1]);
9864c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
9874c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
9884c7070dbSScott Long 
9894c7070dbSScott Long 			NM_CHECK_ADDR_LEN(na, addr, len);
9904c7070dbSScott Long 
9914c7070dbSScott Long 			if (slot->flags & NS_BUF_CHANGED) {
9924c7070dbSScott Long 				/* buffer has changed, reload map */
993bfce461eSMarius Strobl 				netmap_reload_map(na, txq->ift_buf_tag,
994bfce461eSMarius Strobl 				    txq->ift_sds.ifsd_map[nic_i], addr);
9954c7070dbSScott Long 			}
9964c7070dbSScott Long 			/* make sure changes to the buffer are synced */
99795dcf343SMarius Strobl 			bus_dmamap_sync(txq->ift_buf_tag,
99895dcf343SMarius Strobl 			    txq->ift_sds.ifsd_map[nic_i],
9994c7070dbSScott Long 			    BUS_DMASYNC_PREWRITE);
100095dcf343SMarius Strobl 
100195246abbSSean Bruno 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
10024c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
10034c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
10044c7070dbSScott Long 		}
1005dd7fbcf1SStephen Hurd 		kring->nr_hwcur = nm_i;
10064c7070dbSScott Long 
10074c7070dbSScott Long 		/* synchronize the NIC ring */
100895dcf343SMarius Strobl 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
10094c7070dbSScott Long 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
10104c7070dbSScott Long 
10114c7070dbSScott Long 		/* (re)start the tx unit up to slot nic_i (excluded) */
10124c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
10134c7070dbSScott Long 	}
10144c7070dbSScott Long 
10154c7070dbSScott Long 	/*
10164c7070dbSScott Long 	 * Second part: reclaim buffers for completed transmissions.
10175ee36c68SStephen Hurd 	 *
10185ee36c68SStephen Hurd 	 * If there are unclaimed buffers, attempt to reclaim them.
10195ee36c68SStephen Hurd 	 * If none are reclaimed, and TX IRQs are not in use, do an initial
10205ee36c68SStephen Hurd 	 * minimal delay, then trigger the tx handler which will spin in the
10215ee36c68SStephen Hurd 	 * group task queue.
10224c7070dbSScott Long 	 */
1023dd7fbcf1SStephen Hurd 	if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
10244c7070dbSScott Long 		if (iflib_tx_credits_update(ctx, txq)) {
10254c7070dbSScott Long 			/* some tx completed, increment avail */
10264c7070dbSScott Long 			nic_i = txq->ift_cidx_processed;
10274c7070dbSScott Long 			kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
10284c7070dbSScott Long 		}
10295ee36c68SStephen Hurd 	}
1030dd7fbcf1SStephen Hurd 	if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ))
1031dd7fbcf1SStephen Hurd 		if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
1032dd7fbcf1SStephen Hurd 			callout_reset_on(&txq->ift_timer, hz < 2000 ? 1 : hz / 1000,
1033dd7fbcf1SStephen Hurd 			    iflib_timer, txq, txq->ift_timer.c_cpu);
10345ee36c68SStephen Hurd 	}
10354c7070dbSScott Long 	return (0);
10364c7070dbSScott Long }
10374c7070dbSScott Long 
10384c7070dbSScott Long /*
10394c7070dbSScott Long  * Reconcile kernel and user view of the receive ring.
10404c7070dbSScott Long  * Same as for the txsync, this routine must be efficient.
10414c7070dbSScott Long  * The caller guarantees a single invocations, but races against
10424c7070dbSScott Long  * the rest of the driver should be handled here.
10434c7070dbSScott Long  *
10444c7070dbSScott Long  * On call, kring->rhead is the first packet that userspace wants
10454c7070dbSScott Long  * to keep, and kring->rcur is the wakeup point.
10464c7070dbSScott Long  * The kernel has previously reported packets up to kring->rtail.
10474c7070dbSScott Long  *
10484c7070dbSScott Long  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
10494c7070dbSScott Long  * of whether or not we received an interrupt.
10504c7070dbSScott Long  */
10514c7070dbSScott Long static int
10524c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
10534c7070dbSScott Long {
10544c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
10554c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
10561722eeacSMarius Strobl 	if_t ifp = na->ifp;
105795dcf343SMarius Strobl 	iflib_fl_t fl;
105895246abbSSean Bruno 	uint32_t nm_i;	/* index into the netmap ring */
10592d873474SStephen Hurd 	uint32_t nic_i;	/* index into the NIC ring */
10604c7070dbSScott Long 	u_int i, n;
10614c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
1062dd7fbcf1SStephen Hurd 	u_int const head = kring->rhead;
10634c7070dbSScott Long 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
1064ab2e3f79SStephen Hurd 	struct if_rxd_info ri;
106595246abbSSean Bruno 
10664c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
10674c7070dbSScott Long 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
10684c7070dbSScott Long 	if (head > lim)
10694c7070dbSScott Long 		return netmap_ring_reinit(kring);
10704c7070dbSScott Long 
107195dcf343SMarius Strobl 	/*
107295dcf343SMarius Strobl 	 * XXX netmap_fl_refill() only ever (re)fills free list 0 so far.
107395dcf343SMarius Strobl 	 */
107495dcf343SMarius Strobl 
107595246abbSSean Bruno 	for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) {
107695dcf343SMarius Strobl 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
10774c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
107895246abbSSean Bruno 	}
107995dcf343SMarius Strobl 
10804c7070dbSScott Long 	/*
10814c7070dbSScott Long 	 * First part: import newly received packets.
10824c7070dbSScott Long 	 *
10834c7070dbSScott Long 	 * nm_i is the index of the next free slot in the netmap ring,
10844c7070dbSScott Long 	 * nic_i is the index of the next received packet in the NIC ring,
10854c7070dbSScott Long 	 * and they may differ in case if_init() has been called while
10864c7070dbSScott Long 	 * in netmap mode. For the receive ring we have
10874c7070dbSScott Long 	 *
10884c7070dbSScott Long 	 *	nic_i = rxr->next_check;
10894c7070dbSScott Long 	 *	nm_i = kring->nr_hwtail (previous)
10904c7070dbSScott Long 	 * and
10914c7070dbSScott Long 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
10924c7070dbSScott Long 	 *
10934c7070dbSScott Long 	 * rxr->next_check is set to 0 on a ring reinit
10944c7070dbSScott Long 	 */
10954c7070dbSScott Long 	if (netmap_no_pendintr || force_update) {
10964c7070dbSScott Long 		int crclen = iflib_crcstrip ? 0 : 4;
10974c7070dbSScott Long 		int error, avail;
10984c7070dbSScott Long 
10992d873474SStephen Hurd 		for (i = 0; i < rxq->ifr_nfl; i++) {
11002d873474SStephen Hurd 			fl = &rxq->ifr_fl[i];
11014c7070dbSScott Long 			nic_i = fl->ifl_cidx;
11024c7070dbSScott Long 			nm_i = netmap_idx_n2k(kring, nic_i);
110395dcf343SMarius Strobl 			avail = ctx->isc_rxd_available(ctx->ifc_softc,
110495dcf343SMarius Strobl 			    rxq->ifr_id, nic_i, USHRT_MAX);
11054c7070dbSScott Long 			for (n = 0; avail > 0; n++, avail--) {
1106ab2e3f79SStephen Hurd 				rxd_info_zero(&ri);
1107ab2e3f79SStephen Hurd 				ri.iri_frags = rxq->ifr_frags;
1108ab2e3f79SStephen Hurd 				ri.iri_qsidx = kring->ring_id;
1109ab2e3f79SStephen Hurd 				ri.iri_ifp = ctx->ifc_ifp;
1110ab2e3f79SStephen Hurd 				ri.iri_cidx = nic_i;
111195246abbSSean Bruno 
1112ab2e3f79SStephen Hurd 				error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
1113ab2e3f79SStephen Hurd 				ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen;
11147cb7c6e3SNavdeep Parhar 				ring->slot[nm_i].flags = 0;
111595dcf343SMarius Strobl 				bus_dmamap_sync(fl->ifl_buf_tag,
1116e035717eSSean Bruno 				    fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
11174c7070dbSScott Long 				nm_i = nm_next(nm_i, lim);
11184c7070dbSScott Long 				nic_i = nm_next(nic_i, lim);
11194c7070dbSScott Long 			}
11204c7070dbSScott Long 			if (n) { /* update the state variables */
11214c7070dbSScott Long 				if (netmap_no_pendintr && !force_update) {
11224c7070dbSScott Long 					/* diagnostics */
11234c7070dbSScott Long 					iflib_rx_miss ++;
11244c7070dbSScott Long 					iflib_rx_miss_bufs += n;
11254c7070dbSScott Long 				}
11264c7070dbSScott Long 				fl->ifl_cidx = nic_i;
1127dd7fbcf1SStephen Hurd 				kring->nr_hwtail = nm_i;
11284c7070dbSScott Long 			}
11294c7070dbSScott Long 			kring->nr_kflags &= ~NKR_PENDINTR;
11304c7070dbSScott Long 		}
11314c7070dbSScott Long 	}
11324c7070dbSScott Long 	/*
11334c7070dbSScott Long 	 * Second part: skip past packets that userspace has released.
11344c7070dbSScott Long 	 * (kring->nr_hwcur to head excluded),
11354c7070dbSScott Long 	 * and make the buffers available for reception.
11364c7070dbSScott Long 	 * As usual nm_i is the index in the netmap ring,
11374c7070dbSScott Long 	 * nic_i is the index in the NIC ring, and
11384c7070dbSScott Long 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
11394c7070dbSScott Long 	 */
11404c7070dbSScott Long 	/* XXX not sure how this will work with multiple free lists */
1141dd7fbcf1SStephen Hurd 	nm_i = kring->nr_hwcur;
114295246abbSSean Bruno 
11432d873474SStephen Hurd 	return (netmap_fl_refill(rxq, kring, nm_i, false));
11444c7070dbSScott Long }
11454c7070dbSScott Long 
114695246abbSSean Bruno static void
114795246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff)
114895246abbSSean Bruno {
11491722eeacSMarius Strobl 	if_ctx_t ctx = na->ifp->if_softc;
115095246abbSSean Bruno 
1151ab2e3f79SStephen Hurd 	CTX_LOCK(ctx);
115295246abbSSean Bruno 	if (onoff) {
115395246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
115495246abbSSean Bruno 	} else {
115595246abbSSean Bruno 		IFDI_INTR_DISABLE(ctx);
115695246abbSSean Bruno 	}
1157ab2e3f79SStephen Hurd 	CTX_UNLOCK(ctx);
115895246abbSSean Bruno }
115995246abbSSean Bruno 
116095246abbSSean Bruno 
11614c7070dbSScott Long static int
11624c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx)
11634c7070dbSScott Long {
11644c7070dbSScott Long 	struct netmap_adapter na;
116523ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
11664c7070dbSScott Long 
11674c7070dbSScott Long 	bzero(&na, sizeof(na));
11684c7070dbSScott Long 
11694c7070dbSScott Long 	na.ifp = ctx->ifc_ifp;
11704c7070dbSScott Long 	na.na_flags = NAF_BDG_MAYSLEEP;
11714c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
11724c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
11734c7070dbSScott Long 
117423ac9029SStephen Hurd 	na.num_tx_desc = scctx->isc_ntxd[0];
117523ac9029SStephen Hurd 	na.num_rx_desc = scctx->isc_nrxd[0];
11764c7070dbSScott Long 	na.nm_txsync = iflib_netmap_txsync;
11774c7070dbSScott Long 	na.nm_rxsync = iflib_netmap_rxsync;
11784c7070dbSScott Long 	na.nm_register = iflib_netmap_register;
117995246abbSSean Bruno 	na.nm_intr = iflib_netmap_intr;
11804c7070dbSScott Long 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
11814c7070dbSScott Long 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
11824c7070dbSScott Long 	return (netmap_attach(&na));
11834c7070dbSScott Long }
11844c7070dbSScott Long 
11854c7070dbSScott Long static void
11864c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
11874c7070dbSScott Long {
11884c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
11894c7070dbSScott Long 	struct netmap_slot *slot;
11904c7070dbSScott Long 
11914c7070dbSScott Long 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1192e099b90bSPedro F. Giffuni 	if (slot == NULL)
11934c7070dbSScott Long 		return;
119423ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
11954c7070dbSScott Long 
11964c7070dbSScott Long 		/*
11974c7070dbSScott Long 		 * In netmap mode, set the map for the packet buffer.
11984c7070dbSScott Long 		 * NOTE: Some drivers (not this one) also need to set
11994c7070dbSScott Long 		 * the physical buffer address in the NIC ring.
12004c7070dbSScott Long 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
12014c7070dbSScott Long 		 * netmap slot index, si
12024c7070dbSScott Long 		 */
12032ff91c17SVincenzo Maffione 		int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i);
1204bfce461eSMarius Strobl 		netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i],
1205bfce461eSMarius Strobl 		    NMB(na, slot + si));
12064c7070dbSScott Long 	}
12074c7070dbSScott Long }
12082d873474SStephen Hurd 
12094c7070dbSScott Long static void
12104c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
12114c7070dbSScott Long {
12124c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
12132ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[rxq->ifr_id];
12144c7070dbSScott Long 	struct netmap_slot *slot;
12152d873474SStephen Hurd 	uint32_t nm_i;
12164c7070dbSScott Long 
12174c7070dbSScott Long 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1218e099b90bSPedro F. Giffuni 	if (slot == NULL)
12194c7070dbSScott Long 		return;
12202d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, 0);
12212d873474SStephen Hurd 	netmap_fl_refill(rxq, kring, nm_i, true);
12224c7070dbSScott Long }
12234c7070dbSScott Long 
1224dd7fbcf1SStephen Hurd static void
122595dcf343SMarius Strobl iflib_netmap_timer_adjust(if_ctx_t ctx, iflib_txq_t txq, uint32_t *reset_on)
1226dd7fbcf1SStephen Hurd {
1227dd7fbcf1SStephen Hurd 	struct netmap_kring *kring;
122895dcf343SMarius Strobl 	uint16_t txqid;
1229dd7fbcf1SStephen Hurd 
123095dcf343SMarius Strobl 	txqid = txq->ift_id;
1231dd7fbcf1SStephen Hurd 	kring = NA(ctx->ifc_ifp)->tx_rings[txqid];
1232dd7fbcf1SStephen Hurd 
1233dd7fbcf1SStephen Hurd 	if (kring->nr_hwcur != nm_next(kring->nr_hwtail, kring->nkr_num_slots - 1)) {
123495dcf343SMarius Strobl 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
123595dcf343SMarius Strobl 		    BUS_DMASYNC_POSTREAD);
1236dd7fbcf1SStephen Hurd 		if (ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false))
1237dd7fbcf1SStephen Hurd 			netmap_tx_irq(ctx->ifc_ifp, txqid);
1238dd7fbcf1SStephen Hurd 		if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) {
1239dd7fbcf1SStephen Hurd 			if (hz < 2000)
1240dd7fbcf1SStephen Hurd 				*reset_on = 1;
1241dd7fbcf1SStephen Hurd 			else
1242dd7fbcf1SStephen Hurd 				*reset_on = hz / 1000;
1243dd7fbcf1SStephen Hurd 		}
1244dd7fbcf1SStephen Hurd 	}
1245dd7fbcf1SStephen Hurd }
1246dd7fbcf1SStephen Hurd 
12474c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp)
12484c7070dbSScott Long 
12494c7070dbSScott Long #else
12504c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq)
12514c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq)
12524c7070dbSScott Long #define iflib_netmap_detach(ifp)
12534c7070dbSScott Long 
12544c7070dbSScott Long #define iflib_netmap_attach(ctx) (0)
12554c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0)
125695246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0)
125795dcf343SMarius Strobl #define iflib_netmap_timer_adjust(ctx, txq, reset_on)
12584c7070dbSScott Long #endif
12594c7070dbSScott Long 
12604c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
12614c7070dbSScott Long static __inline void
12624c7070dbSScott Long prefetch(void *x)
12634c7070dbSScott Long {
12644c7070dbSScott Long 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12654c7070dbSScott Long }
12663429c02fSStephen Hurd static __inline void
12673429c02fSStephen Hurd prefetch2cachelines(void *x)
12683429c02fSStephen Hurd {
12693429c02fSStephen Hurd 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12703429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128)
12713429c02fSStephen Hurd 	__asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long)))));
12723429c02fSStephen Hurd #endif
12733429c02fSStephen Hurd }
12744c7070dbSScott Long #else
12754c7070dbSScott Long #define prefetch(x)
12763429c02fSStephen Hurd #define prefetch2cachelines(x)
12774c7070dbSScott Long #endif
12784c7070dbSScott Long 
12794c7070dbSScott Long static void
128010e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid)
128110e0d938SStephen Hurd {
128210e0d938SStephen Hurd 	iflib_fl_t fl;
128310e0d938SStephen Hurd 
128410e0d938SStephen Hurd 	fl = &rxq->ifr_fl[flid];
128510e0d938SStephen Hurd 	iru->iru_paddrs = fl->ifl_bus_addrs;
128610e0d938SStephen Hurd 	iru->iru_vaddrs = &fl->ifl_vm_addrs[0];
128710e0d938SStephen Hurd 	iru->iru_idxs = fl->ifl_rxd_idxs;
128810e0d938SStephen Hurd 	iru->iru_qsidx = rxq->ifr_id;
128910e0d938SStephen Hurd 	iru->iru_buf_size = fl->ifl_buf_size;
129010e0d938SStephen Hurd 	iru->iru_flidx = fl->ifl_id;
129110e0d938SStephen Hurd }
129210e0d938SStephen Hurd 
129310e0d938SStephen Hurd static void
12944c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
12954c7070dbSScott Long {
12964c7070dbSScott Long 	if (err)
12974c7070dbSScott Long 		return;
12984c7070dbSScott Long 	*(bus_addr_t *) arg = segs[0].ds_addr;
12994c7070dbSScott Long }
13004c7070dbSScott Long 
13014c7070dbSScott Long int
13028f82136aSPatrick Kelsey iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags)
13034c7070dbSScott Long {
13044c7070dbSScott Long 	int err;
13054c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
13064c7070dbSScott Long 
13074c7070dbSScott Long 	err = bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
13088f82136aSPatrick Kelsey 				align, 0,		/* alignment, bounds */
13094c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* lowaddr */
13104c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* highaddr */
13114c7070dbSScott Long 				NULL, NULL,		/* filter, filterarg */
13124c7070dbSScott Long 				size,			/* maxsize */
13134c7070dbSScott Long 				1,			/* nsegments */
13144c7070dbSScott Long 				size,			/* maxsegsize */
13154c7070dbSScott Long 				BUS_DMA_ALLOCNOW,	/* flags */
13164c7070dbSScott Long 				NULL,			/* lockfunc */
13174c7070dbSScott Long 				NULL,			/* lockarg */
13184c7070dbSScott Long 				&dma->idi_tag);
13194c7070dbSScott Long 	if (err) {
13204c7070dbSScott Long 		device_printf(dev,
13214c7070dbSScott Long 		    "%s: bus_dma_tag_create failed: %d\n",
13224c7070dbSScott Long 		    __func__, err);
13234c7070dbSScott Long 		goto fail_0;
13244c7070dbSScott Long 	}
13254c7070dbSScott Long 
13264c7070dbSScott Long 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
13274c7070dbSScott Long 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
13284c7070dbSScott Long 	if (err) {
13294c7070dbSScott Long 		device_printf(dev,
13304c7070dbSScott Long 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
13314c7070dbSScott Long 		    __func__, (uintmax_t)size, err);
13324c7070dbSScott Long 		goto fail_1;
13334c7070dbSScott Long 	}
13344c7070dbSScott Long 
13354c7070dbSScott Long 	dma->idi_paddr = IF_BAD_DMA;
13364c7070dbSScott Long 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
13374c7070dbSScott Long 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
13384c7070dbSScott Long 	if (err || dma->idi_paddr == IF_BAD_DMA) {
13394c7070dbSScott Long 		device_printf(dev,
13404c7070dbSScott Long 		    "%s: bus_dmamap_load failed: %d\n",
13414c7070dbSScott Long 		    __func__, err);
13424c7070dbSScott Long 		goto fail_2;
13434c7070dbSScott Long 	}
13444c7070dbSScott Long 
13454c7070dbSScott Long 	dma->idi_size = size;
13464c7070dbSScott Long 	return (0);
13474c7070dbSScott Long 
13484c7070dbSScott Long fail_2:
13494c7070dbSScott Long 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
13504c7070dbSScott Long fail_1:
13514c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
13524c7070dbSScott Long fail_0:
13534c7070dbSScott Long 	dma->idi_tag = NULL;
13544c7070dbSScott Long 
13554c7070dbSScott Long 	return (err);
13564c7070dbSScott Long }
13574c7070dbSScott Long 
13584c7070dbSScott Long int
13598f82136aSPatrick Kelsey iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
13608f82136aSPatrick Kelsey {
13618f82136aSPatrick Kelsey 	if_shared_ctx_t sctx = ctx->ifc_sctx;
13628f82136aSPatrick Kelsey 
13638f82136aSPatrick Kelsey 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
13648f82136aSPatrick Kelsey 
13658f82136aSPatrick Kelsey 	return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags));
13668f82136aSPatrick Kelsey }
13678f82136aSPatrick Kelsey 
13688f82136aSPatrick Kelsey int
13694c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
13704c7070dbSScott Long {
13714c7070dbSScott Long 	int i, err;
13724c7070dbSScott Long 	iflib_dma_info_t *dmaiter;
13734c7070dbSScott Long 
13744c7070dbSScott Long 	dmaiter = dmalist;
13754c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++) {
13764c7070dbSScott Long 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
13774c7070dbSScott Long 			break;
13784c7070dbSScott Long 	}
13794c7070dbSScott Long 	if (err)
13804c7070dbSScott Long 		iflib_dma_free_multi(dmalist, i);
13814c7070dbSScott Long 	return (err);
13824c7070dbSScott Long }
13834c7070dbSScott Long 
13844c7070dbSScott Long void
13854c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma)
13864c7070dbSScott Long {
13874c7070dbSScott Long 	if (dma->idi_tag == NULL)
13884c7070dbSScott Long 		return;
13894c7070dbSScott Long 	if (dma->idi_paddr != IF_BAD_DMA) {
13904c7070dbSScott Long 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
13914c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
13924c7070dbSScott Long 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
13934c7070dbSScott Long 		dma->idi_paddr = IF_BAD_DMA;
13944c7070dbSScott Long 	}
13954c7070dbSScott Long 	if (dma->idi_vaddr != NULL) {
13964c7070dbSScott Long 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
13974c7070dbSScott Long 		dma->idi_vaddr = NULL;
13984c7070dbSScott Long 	}
13994c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
14004c7070dbSScott Long 	dma->idi_tag = NULL;
14014c7070dbSScott Long }
14024c7070dbSScott Long 
14034c7070dbSScott Long void
14044c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
14054c7070dbSScott Long {
14064c7070dbSScott Long 	int i;
14074c7070dbSScott Long 	iflib_dma_info_t *dmaiter = dmalist;
14084c7070dbSScott Long 
14094c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++)
14104c7070dbSScott Long 		iflib_dma_free(*dmaiter);
14114c7070dbSScott Long }
14124c7070dbSScott Long 
14134c7070dbSScott Long static int
14144c7070dbSScott Long iflib_fast_intr(void *arg)
14154c7070dbSScott Long {
14164c7070dbSScott Long 	iflib_filter_info_t info = arg;
14174c7070dbSScott Long 	struct grouptask *gtask = info->ifi_task;
1418ca62461bSStephen Hurd 	int result;
1419ca62461bSStephen Hurd 
142095246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
1421ca62461bSStephen Hurd 	if (info->ifi_filter != NULL) {
1422ca62461bSStephen Hurd 		result = info->ifi_filter(info->ifi_filter_arg);
1423ca62461bSStephen Hurd 		if ((result & FILTER_SCHEDULE_THREAD) == 0)
1424ca62461bSStephen Hurd 			return (result);
1425ca62461bSStephen Hurd 	}
142695246abbSSean Bruno 
142795246abbSSean Bruno 	GROUPTASK_ENQUEUE(gtask);
142895246abbSSean Bruno 	return (FILTER_HANDLED);
142995246abbSSean Bruno }
143095246abbSSean Bruno 
143195246abbSSean Bruno static int
143295246abbSSean Bruno iflib_fast_intr_rxtx(void *arg)
143395246abbSSean Bruno {
143495246abbSSean Bruno 	iflib_filter_info_t info = arg;
143595246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
143695dcf343SMarius Strobl 	if_ctx_t ctx;
143795246abbSSean Bruno 	iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
143895dcf343SMarius Strobl 	iflib_txq_t txq;
143995dcf343SMarius Strobl 	void *sc;
1440ca62461bSStephen Hurd 	int i, cidx, result;
144195dcf343SMarius Strobl 	qidx_t txqid;
14423d10e9edSMarius Strobl 	bool intr_enable, intr_legacy;
144395246abbSSean Bruno 
144495246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
1445ca62461bSStephen Hurd 	if (info->ifi_filter != NULL) {
1446ca62461bSStephen Hurd 		result = info->ifi_filter(info->ifi_filter_arg);
1447ca62461bSStephen Hurd 		if ((result & FILTER_SCHEDULE_THREAD) == 0)
1448ca62461bSStephen Hurd 			return (result);
1449ca62461bSStephen Hurd 	}
145095246abbSSean Bruno 
145195dcf343SMarius Strobl 	ctx = rxq->ifr_ctx;
145295dcf343SMarius Strobl 	sc = ctx->ifc_softc;
14533d10e9edSMarius Strobl 	intr_enable = false;
14543d10e9edSMarius Strobl 	intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY);
14551ae4848cSMatt Macy 	MPASS(rxq->ifr_ntxqirq);
145695246abbSSean Bruno 	for (i = 0; i < rxq->ifr_ntxqirq; i++) {
145795dcf343SMarius Strobl 		txqid = rxq->ifr_txqid[i];
145895dcf343SMarius Strobl 		txq = &ctx->ifc_txqs[txqid];
145995dcf343SMarius Strobl 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
14608a04b53dSKonstantin Belousov 		    BUS_DMASYNC_POSTREAD);
146195dcf343SMarius Strobl 		if (!ctx->isc_txd_credits_update(sc, txqid, false)) {
14623d10e9edSMarius Strobl 			if (intr_legacy)
14633d10e9edSMarius Strobl 				intr_enable = true;
14643d10e9edSMarius Strobl 			else
146595246abbSSean Bruno 				IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
146695246abbSSean Bruno 			continue;
146795246abbSSean Bruno 		}
146895dcf343SMarius Strobl 		GROUPTASK_ENQUEUE(&txq->ift_task);
146995246abbSSean Bruno 	}
147095246abbSSean Bruno 	if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
147195246abbSSean Bruno 		cidx = rxq->ifr_cq_cidx;
147295246abbSSean Bruno 	else
147395246abbSSean Bruno 		cidx = rxq->ifr_fl[0].ifl_cidx;
147495246abbSSean Bruno 	if (iflib_rxd_avail(ctx, rxq, cidx, 1))
147595246abbSSean Bruno 		GROUPTASK_ENQUEUE(gtask);
147664e6fc13SStephen Hurd 	else {
14773d10e9edSMarius Strobl 		if (intr_legacy)
14783d10e9edSMarius Strobl 			intr_enable = true;
14793d10e9edSMarius Strobl 		else
148095246abbSSean Bruno 			IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
148164e6fc13SStephen Hurd 		DBG_COUNTER_INC(rx_intr_enables);
148264e6fc13SStephen Hurd 	}
14833d10e9edSMarius Strobl 	if (intr_enable)
14843d10e9edSMarius Strobl 		IFDI_INTR_ENABLE(ctx);
148595246abbSSean Bruno 	return (FILTER_HANDLED);
148695246abbSSean Bruno }
148795246abbSSean Bruno 
148895246abbSSean Bruno 
148995246abbSSean Bruno static int
149095246abbSSean Bruno iflib_fast_intr_ctx(void *arg)
149195246abbSSean Bruno {
149295246abbSSean Bruno 	iflib_filter_info_t info = arg;
149395246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
1494ca62461bSStephen Hurd 	int result;
14954c7070dbSScott Long 
14964c7070dbSScott Long 	DBG_COUNTER_INC(fast_intrs);
1497ca62461bSStephen Hurd 	if (info->ifi_filter != NULL) {
1498ca62461bSStephen Hurd 		result = info->ifi_filter(info->ifi_filter_arg);
1499ca62461bSStephen Hurd 		if ((result & FILTER_SCHEDULE_THREAD) == 0)
1500ca62461bSStephen Hurd 			return (result);
1501ca62461bSStephen Hurd 	}
15024c7070dbSScott Long 
15034c7070dbSScott Long 	GROUPTASK_ENQUEUE(gtask);
15044c7070dbSScott Long 	return (FILTER_HANDLED);
15054c7070dbSScott Long }
15064c7070dbSScott Long 
15074c7070dbSScott Long static int
15084c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
15094c7070dbSScott Long 		 driver_filter_t filter, driver_intr_t handler, void *arg,
15103e0e6330SStephen Hurd 		 const char *name)
15114c7070dbSScott Long {
15124c7070dbSScott Long 	struct resource *res;
15132b2fc973SSean Bruno 	void *tag = NULL;
15144c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
1515d49e83eaSMarius Strobl 	int flags, i, rc;
15164c7070dbSScott Long 
15172b2fc973SSean Bruno 	flags = RF_ACTIVE;
15182b2fc973SSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
15192b2fc973SSean Bruno 		flags |= RF_SHAREABLE;
15204c7070dbSScott Long 	MPASS(rid < 512);
1521d49e83eaSMarius Strobl 	i = rid;
1522d49e83eaSMarius Strobl 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, flags);
15234c7070dbSScott Long 	if (res == NULL) {
15244c7070dbSScott Long 		device_printf(dev,
15254c7070dbSScott Long 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
15264c7070dbSScott Long 		return (ENOMEM);
15274c7070dbSScott Long 	}
15284c7070dbSScott Long 	irq->ii_res = res;
15294c7070dbSScott Long 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
15304c7070dbSScott Long 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
15314c7070dbSScott Long 						filter, handler, arg, &tag);
15324c7070dbSScott Long 	if (rc != 0) {
15334c7070dbSScott Long 		device_printf(dev,
15344c7070dbSScott Long 		    "failed to setup interrupt for rid %d, name %s: %d\n",
15354c7070dbSScott Long 					  rid, name ? name : "unknown", rc);
15364c7070dbSScott Long 		return (rc);
15374c7070dbSScott Long 	} else if (name)
1538f454e7ebSJohn Baldwin 		bus_describe_intr(dev, res, tag, "%s", name);
15394c7070dbSScott Long 
15404c7070dbSScott Long 	irq->ii_tag = tag;
15414c7070dbSScott Long 	return (0);
15424c7070dbSScott Long }
15434c7070dbSScott Long 
15444c7070dbSScott Long /*********************************************************************
15454c7070dbSScott Long  *
1546bfce461eSMarius Strobl  *  Allocate DMA resources for TX buffers as well as memory for the TX
1547bfce461eSMarius Strobl  *  mbuf map.  TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a
1548bfce461eSMarius Strobl  *  iflib_sw_tx_desc_array structure, storing all the information that
1549bfce461eSMarius Strobl  *  is needed to transmit a packet on the wire.  This is called only
1550bfce461eSMarius Strobl  *  once at attach, setup is done every reset.
15514c7070dbSScott Long  *
15524c7070dbSScott Long  **********************************************************************/
15534c7070dbSScott Long static int
15544c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq)
15554c7070dbSScott Long {
15564c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
15574c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
15584c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
15594c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
15607f87c040SMarius Strobl 	bus_size_t tsomaxsize;
15614c7070dbSScott Long 	int err, nsegments, ntsosegments;
15628a04b53dSKonstantin Belousov 	bool tso;
15634c7070dbSScott Long 
15644c7070dbSScott Long 	nsegments = scctx->isc_tx_nsegments;
15654c7070dbSScott Long 	ntsosegments = scctx->isc_tx_tso_segments_max;
15667f87c040SMarius Strobl 	tsomaxsize = scctx->isc_tx_tso_size_max;
15677f87c040SMarius Strobl 	if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU)
15687f87c040SMarius Strobl 		tsomaxsize += sizeof(struct ether_vlan_header);
156923ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[0] > 0);
157023ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
15714c7070dbSScott Long 	MPASS(nsegments > 0);
15727f87c040SMarius Strobl 	if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) {
15734c7070dbSScott Long 		MPASS(ntsosegments > 0);
15747f87c040SMarius Strobl 		MPASS(sctx->isc_tso_maxsize >= tsomaxsize);
15757f87c040SMarius Strobl 	}
15767f87c040SMarius Strobl 
15774c7070dbSScott Long 	/*
1578bfce461eSMarius Strobl 	 * Set up DMA tags for TX buffers.
15794c7070dbSScott Long 	 */
15804c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
15814c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
15824c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
15834c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
15844c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
15854c7070dbSScott Long 			       sctx->isc_tx_maxsize,		/* maxsize */
15864c7070dbSScott Long 			       nsegments,	/* nsegments */
15874c7070dbSScott Long 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
15884c7070dbSScott Long 			       0,			/* flags */
15894c7070dbSScott Long 			       NULL,			/* lockfunc */
15904c7070dbSScott Long 			       NULL,			/* lockfuncarg */
1591bfce461eSMarius Strobl 			       &txq->ift_buf_tag))) {
15924c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
15939d0a88deSDimitry Andric 		device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n",
15949d0a88deSDimitry Andric 		    (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
15954c7070dbSScott Long 		goto fail;
15964c7070dbSScott Long 	}
15978a04b53dSKonstantin Belousov 	tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0;
15988a04b53dSKonstantin Belousov 	if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev),
15994c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
16004c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
16014c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
16024c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
16037f87c040SMarius Strobl 			       tsomaxsize,		/* maxsize */
16044c7070dbSScott Long 			       ntsosegments,	/* nsegments */
16057f87c040SMarius Strobl 			       sctx->isc_tso_maxsegsize,/* maxsegsize */
16064c7070dbSScott Long 			       0,			/* flags */
16074c7070dbSScott Long 			       NULL,			/* lockfunc */
16084c7070dbSScott Long 			       NULL,			/* lockfuncarg */
1609bfce461eSMarius Strobl 			       &txq->ift_tso_buf_tag))) {
1610bfce461eSMarius Strobl 		device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n",
1611bfce461eSMarius Strobl 		    err);
16124c7070dbSScott Long 		goto fail;
16134c7070dbSScott Long 	}
1614bfce461eSMarius Strobl 
1615bfce461eSMarius Strobl 	/* Allocate memory for the TX mbuf map. */
16164c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_m =
1617ac2fffa4SPedro F. Giffuni 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
1618ac2fffa4SPedro F. Giffuni 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1619bfce461eSMarius Strobl 		device_printf(dev, "Unable to allocate TX mbuf map memory\n");
16204c7070dbSScott Long 		err = ENOMEM;
16214c7070dbSScott Long 		goto fail;
16224c7070dbSScott Long 	}
16234c7070dbSScott Long 
1624bfce461eSMarius Strobl 	/*
1625bfce461eSMarius Strobl 	 * Create the DMA maps for TX buffers.
1626bfce461eSMarius Strobl 	 */
16278a04b53dSKonstantin Belousov 	if ((txq->ift_sds.ifsd_map = (bus_dmamap_t *)malloc(
16288a04b53dSKonstantin Belousov 	    sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
16298a04b53dSKonstantin Belousov 	    M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
1630bfce461eSMarius Strobl 		device_printf(dev,
1631bfce461eSMarius Strobl 		    "Unable to allocate TX buffer DMA map memory\n");
16324c7070dbSScott Long 		err = ENOMEM;
16334c7070dbSScott Long 		goto fail;
16344c7070dbSScott Long 	}
16358a04b53dSKonstantin Belousov 	if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc(
16368a04b53dSKonstantin Belousov 	    sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
16378a04b53dSKonstantin Belousov 	    M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
1638bfce461eSMarius Strobl 		device_printf(dev,
1639bfce461eSMarius Strobl 		    "Unable to allocate TSO TX buffer map memory\n");
16408a04b53dSKonstantin Belousov 		err = ENOMEM;
16418a04b53dSKonstantin Belousov 		goto fail;
16428a04b53dSKonstantin Belousov 	}
164323ac9029SStephen Hurd 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
1644bfce461eSMarius Strobl 		err = bus_dmamap_create(txq->ift_buf_tag, 0,
16458a04b53dSKonstantin Belousov 		    &txq->ift_sds.ifsd_map[i]);
16464c7070dbSScott Long 		if (err != 0) {
16474c7070dbSScott Long 			device_printf(dev, "Unable to create TX DMA map\n");
16484c7070dbSScott Long 			goto fail;
16494c7070dbSScott Long 		}
16508a04b53dSKonstantin Belousov 		if (!tso)
16518a04b53dSKonstantin Belousov 			continue;
1652bfce461eSMarius Strobl 		err = bus_dmamap_create(txq->ift_tso_buf_tag, 0,
16538a04b53dSKonstantin Belousov 		    &txq->ift_sds.ifsd_tso_map[i]);
16548a04b53dSKonstantin Belousov 		if (err != 0) {
16558a04b53dSKonstantin Belousov 			device_printf(dev, "Unable to create TSO TX DMA map\n");
16568a04b53dSKonstantin Belousov 			goto fail;
16578a04b53dSKonstantin Belousov 		}
16584c7070dbSScott Long 	}
16594c7070dbSScott Long 	return (0);
16604c7070dbSScott Long fail:
16614c7070dbSScott Long 	/* We free all, it handles case where we are in the middle */
16624c7070dbSScott Long 	iflib_tx_structures_free(ctx);
16634c7070dbSScott Long 	return (err);
16644c7070dbSScott Long }
16654c7070dbSScott Long 
16664c7070dbSScott Long static void
16674c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
16684c7070dbSScott Long {
16694c7070dbSScott Long 	bus_dmamap_t map;
16704c7070dbSScott Long 
1671db8e8f1eSEric Joyner 	if (txq->ift_sds.ifsd_map != NULL) {
16724c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[i];
1673bfce461eSMarius Strobl 		bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE);
1674bfce461eSMarius Strobl 		bus_dmamap_unload(txq->ift_buf_tag, map);
1675bfce461eSMarius Strobl 		bus_dmamap_destroy(txq->ift_buf_tag, map);
16764c7070dbSScott Long 		txq->ift_sds.ifsd_map[i] = NULL;
16774c7070dbSScott Long 	}
16788a04b53dSKonstantin Belousov 
1679db8e8f1eSEric Joyner 	if (txq->ift_sds.ifsd_tso_map != NULL) {
16808a04b53dSKonstantin Belousov 		map = txq->ift_sds.ifsd_tso_map[i];
1681bfce461eSMarius Strobl 		bus_dmamap_sync(txq->ift_tso_buf_tag, map,
16828a04b53dSKonstantin Belousov 		    BUS_DMASYNC_POSTWRITE);
1683bfce461eSMarius Strobl 		bus_dmamap_unload(txq->ift_tso_buf_tag, map);
1684bfce461eSMarius Strobl 		bus_dmamap_destroy(txq->ift_tso_buf_tag, map);
16858a04b53dSKonstantin Belousov 		txq->ift_sds.ifsd_tso_map[i] = NULL;
16868a04b53dSKonstantin Belousov 	}
16874c7070dbSScott Long }
16884c7070dbSScott Long 
16894c7070dbSScott Long static void
16904c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq)
16914c7070dbSScott Long {
16924c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
16934c7070dbSScott Long 
169423ac9029SStephen Hurd 	for (int i = 0; i < txq->ift_size; i++)
16954c7070dbSScott Long 		iflib_txsd_destroy(ctx, txq, i);
1696244e7cffSEric Joyner 
1697244e7cffSEric Joyner 	if (txq->ift_br != NULL) {
1698244e7cffSEric Joyner 		ifmp_ring_free(txq->ift_br);
1699244e7cffSEric Joyner 		txq->ift_br = NULL;
1700244e7cffSEric Joyner 	}
1701244e7cffSEric Joyner 
1702244e7cffSEric Joyner 	mtx_destroy(&txq->ift_mtx);
1703244e7cffSEric Joyner 
17044c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
17054c7070dbSScott Long 		free(txq->ift_sds.ifsd_map, M_IFLIB);
17064c7070dbSScott Long 		txq->ift_sds.ifsd_map = NULL;
17074c7070dbSScott Long 	}
17088a04b53dSKonstantin Belousov 	if (txq->ift_sds.ifsd_tso_map != NULL) {
17098a04b53dSKonstantin Belousov 		free(txq->ift_sds.ifsd_tso_map, M_IFLIB);
17108a04b53dSKonstantin Belousov 		txq->ift_sds.ifsd_tso_map = NULL;
17118a04b53dSKonstantin Belousov 	}
17124c7070dbSScott Long 	if (txq->ift_sds.ifsd_m != NULL) {
17134c7070dbSScott Long 		free(txq->ift_sds.ifsd_m, M_IFLIB);
17144c7070dbSScott Long 		txq->ift_sds.ifsd_m = NULL;
17154c7070dbSScott Long 	}
1716bfce461eSMarius Strobl 	if (txq->ift_buf_tag != NULL) {
1717bfce461eSMarius Strobl 		bus_dma_tag_destroy(txq->ift_buf_tag);
1718bfce461eSMarius Strobl 		txq->ift_buf_tag = NULL;
17194c7070dbSScott Long 	}
1720bfce461eSMarius Strobl 	if (txq->ift_tso_buf_tag != NULL) {
1721bfce461eSMarius Strobl 		bus_dma_tag_destroy(txq->ift_tso_buf_tag);
1722bfce461eSMarius Strobl 		txq->ift_tso_buf_tag = NULL;
17234c7070dbSScott Long 	}
1724244e7cffSEric Joyner 	if (txq->ift_ifdi != NULL) {
1725244e7cffSEric Joyner 		free(txq->ift_ifdi, M_IFLIB);
1726244e7cffSEric Joyner 	}
17274c7070dbSScott Long }
17284c7070dbSScott Long 
17294c7070dbSScott Long static void
17304c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
17314c7070dbSScott Long {
17324c7070dbSScott Long 	struct mbuf **mp;
17334c7070dbSScott Long 
17344c7070dbSScott Long 	mp = &txq->ift_sds.ifsd_m[i];
17354c7070dbSScott Long 	if (*mp == NULL)
17364c7070dbSScott Long 		return;
17374c7070dbSScott Long 
17384c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
1739bfce461eSMarius Strobl 		bus_dmamap_sync(txq->ift_buf_tag,
17408a04b53dSKonstantin Belousov 		    txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE);
1741bfce461eSMarius Strobl 		bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]);
17428a04b53dSKonstantin Belousov 	}
17438a04b53dSKonstantin Belousov 	if (txq->ift_sds.ifsd_tso_map != NULL) {
1744bfce461eSMarius Strobl 		bus_dmamap_sync(txq->ift_tso_buf_tag,
17458a04b53dSKonstantin Belousov 		    txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE);
1746bfce461eSMarius Strobl 		bus_dmamap_unload(txq->ift_tso_buf_tag,
17478a04b53dSKonstantin Belousov 		    txq->ift_sds.ifsd_tso_map[i]);
17484c7070dbSScott Long 	}
174923ac9029SStephen Hurd 	m_free(*mp);
17504c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
17514c7070dbSScott Long 	*mp = NULL;
17524c7070dbSScott Long }
17534c7070dbSScott Long 
17544c7070dbSScott Long static int
17554c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq)
17564c7070dbSScott Long {
17574c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
175823ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
17594d261ce2SStephen Hurd 	if_shared_ctx_t sctx = ctx->ifc_sctx;
17604c7070dbSScott Long 	iflib_dma_info_t di;
17614c7070dbSScott Long 	int i;
17624c7070dbSScott Long 
17634c7070dbSScott Long 	/* Set number of descriptors available */
17644c7070dbSScott Long 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
176595246abbSSean Bruno 	/* XXX make configurable */
176695246abbSSean Bruno 	txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
17674c7070dbSScott Long 
17684c7070dbSScott Long 	/* Reset indices */
176995246abbSSean Bruno 	txq->ift_cidx_processed = 0;
177095246abbSSean Bruno 	txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
177123ac9029SStephen Hurd 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
17724c7070dbSScott Long 
17734d261ce2SStephen Hurd 	for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
17744c7070dbSScott Long 		bzero((void *)di->idi_vaddr, di->idi_size);
17754c7070dbSScott Long 
17764c7070dbSScott Long 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
17774d261ce2SStephen Hurd 	for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
17784c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
17794c7070dbSScott Long 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17804c7070dbSScott Long 	return (0);
17814c7070dbSScott Long }
17824c7070dbSScott Long 
17834c7070dbSScott Long /*********************************************************************
17844c7070dbSScott Long  *
1785bfce461eSMarius Strobl  *  Allocate DMA resources for RX buffers as well as memory for the RX
1786bfce461eSMarius Strobl  *  mbuf map, direct RX cluster pointer map and RX cluster bus address
1787bfce461eSMarius Strobl  *  map.  RX DMA map, RX mbuf map, direct RX cluster pointer map and
1788bfce461eSMarius Strobl  *  RX cluster map are kept in a iflib_sw_rx_desc_array structure.
1789bfce461eSMarius Strobl  *  Since we use use one entry in iflib_sw_rx_desc_array per received
1790bfce461eSMarius Strobl  *  packet, the maximum number of entries we'll need is equal to the
1791bfce461eSMarius Strobl  *  number of hardware receive descriptors that we've allocated.
17924c7070dbSScott Long  *
17934c7070dbSScott Long  **********************************************************************/
17944c7070dbSScott Long static int
17954c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq)
17964c7070dbSScott Long {
17974c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
17984c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
179923ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
18004c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
18014c7070dbSScott Long 	iflib_fl_t fl;
18024c7070dbSScott Long 	int			err;
18034c7070dbSScott Long 
180423ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[0] > 0);
180523ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
18064c7070dbSScott Long 
18074c7070dbSScott Long 	fl = rxq->ifr_fl;
18084c7070dbSScott Long 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
180923ac9029SStephen Hurd 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
1810bfce461eSMarius Strobl 		/* Set up DMA tag for RX buffers. */
18114c7070dbSScott Long 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
18124c7070dbSScott Long 					 1, 0,			/* alignment, bounds */
18134c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* lowaddr */
18144c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* highaddr */
18154c7070dbSScott Long 					 NULL, NULL,		/* filter, filterarg */
18164c7070dbSScott Long 					 sctx->isc_rx_maxsize,	/* maxsize */
18174c7070dbSScott Long 					 sctx->isc_rx_nsegments,	/* nsegments */
18184c7070dbSScott Long 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
18194c7070dbSScott Long 					 0,			/* flags */
18204c7070dbSScott Long 					 NULL,			/* lockfunc */
18214c7070dbSScott Long 					 NULL,			/* lockarg */
1822bfce461eSMarius Strobl 					 &fl->ifl_buf_tag);
18234c7070dbSScott Long 		if (err) {
1824bfce461eSMarius Strobl 			device_printf(dev,
1825bfce461eSMarius Strobl 			    "Unable to allocate RX DMA tag: %d\n", err);
18264c7070dbSScott Long 			goto fail;
18274c7070dbSScott Long 		}
1828bfce461eSMarius Strobl 
1829bfce461eSMarius Strobl 		/* Allocate memory for the RX mbuf map. */
1830e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_m =
1831ac2fffa4SPedro F. Giffuni 		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
1832ac2fffa4SPedro F. Giffuni 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1833bfce461eSMarius Strobl 			device_printf(dev,
1834bfce461eSMarius Strobl 			    "Unable to allocate RX mbuf map memory\n");
1835e035717eSSean Bruno 			err = ENOMEM;
1836e035717eSSean Bruno 			goto fail;
1837e035717eSSean Bruno 		}
1838bfce461eSMarius Strobl 
1839bfce461eSMarius Strobl 		/* Allocate memory for the direct RX cluster pointer map. */
1840e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_cl =
1841ac2fffa4SPedro F. Giffuni 		      (caddr_t *) malloc(sizeof(caddr_t) *
1842ac2fffa4SPedro F. Giffuni 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1843bfce461eSMarius Strobl 			device_printf(dev,
1844bfce461eSMarius Strobl 			    "Unable to allocate RX cluster map memory\n");
1845e035717eSSean Bruno 			err = ENOMEM;
1846e035717eSSean Bruno 			goto fail;
1847e035717eSSean Bruno 		}
18484c7070dbSScott Long 
1849bfce461eSMarius Strobl 		/* Allocate memory for the RX cluster bus address map. */
1850fbec776dSAndrew Gallatin 		if (!(fl->ifl_sds.ifsd_ba =
1851fbec776dSAndrew Gallatin 		      (bus_addr_t *) malloc(sizeof(bus_addr_t) *
1852fbec776dSAndrew Gallatin 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1853bfce461eSMarius Strobl 			device_printf(dev,
1854bfce461eSMarius Strobl 			    "Unable to allocate RX bus address map memory\n");
1855fbec776dSAndrew Gallatin 			err = ENOMEM;
1856fbec776dSAndrew Gallatin 			goto fail;
1857fbec776dSAndrew Gallatin 		}
1858e035717eSSean Bruno 
1859bfce461eSMarius Strobl 		/*
1860bfce461eSMarius Strobl 		 * Create the DMA maps for RX buffers.
1861bfce461eSMarius Strobl 		 */
1862e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_map =
1863ac2fffa4SPedro F. Giffuni 		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1864bfce461eSMarius Strobl 			device_printf(dev,
1865bfce461eSMarius Strobl 			    "Unable to allocate RX buffer DMA map memory\n");
1866e035717eSSean Bruno 			err = ENOMEM;
1867e035717eSSean Bruno 			goto fail;
1868e035717eSSean Bruno 		}
1869e035717eSSean Bruno 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1870bfce461eSMarius Strobl 			err = bus_dmamap_create(fl->ifl_buf_tag, 0,
1871bfce461eSMarius Strobl 			    &fl->ifl_sds.ifsd_map[i]);
1872e035717eSSean Bruno 			if (err != 0) {
187395246abbSSean Bruno 				device_printf(dev, "Unable to create RX buffer DMA map\n");
18744c7070dbSScott Long 				goto fail;
18754c7070dbSScott Long 			}
18764c7070dbSScott Long 		}
1877835809f9SSean Bruno 	}
18784c7070dbSScott Long 	return (0);
18794c7070dbSScott Long 
18804c7070dbSScott Long fail:
18814c7070dbSScott Long 	iflib_rx_structures_free(ctx);
18824c7070dbSScott Long 	return (err);
18834c7070dbSScott Long }
18844c7070dbSScott Long 
18854c7070dbSScott Long 
18864c7070dbSScott Long /*
18874c7070dbSScott Long  * Internal service routines
18884c7070dbSScott Long  */
18894c7070dbSScott Long 
18904c7070dbSScott Long struct rxq_refill_cb_arg {
18914c7070dbSScott Long 	int               error;
18924c7070dbSScott Long 	bus_dma_segment_t seg;
18934c7070dbSScott Long 	int               nseg;
18944c7070dbSScott Long };
18954c7070dbSScott Long 
18964c7070dbSScott Long static void
18974c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
18984c7070dbSScott Long {
18994c7070dbSScott Long 	struct rxq_refill_cb_arg *cb_arg = arg;
19004c7070dbSScott Long 
19014c7070dbSScott Long 	cb_arg->error = error;
19024c7070dbSScott Long 	cb_arg->seg = segs[0];
19034c7070dbSScott Long 	cb_arg->nseg = nseg;
19044c7070dbSScott Long }
19054c7070dbSScott Long 
19064c7070dbSScott Long /**
19071722eeacSMarius Strobl  * _iflib_fl_refill - refill an rxq free-buffer list
19084c7070dbSScott Long  * @ctx: the iflib context
19091722eeacSMarius Strobl  * @fl: the free list to refill
19101722eeacSMarius Strobl  * @count: the number of new buffers to allocate
19114c7070dbSScott Long  *
19121722eeacSMarius Strobl  * (Re)populate an rxq free-buffer list with up to @count new packet buffers.
19131722eeacSMarius Strobl  * The caller must assure that @count does not exceed the queue's capacity.
19144c7070dbSScott Long  */
1915fb1a29b4SHans Petter Selasky static uint8_t
19164c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
19174c7070dbSScott Long {
191895246abbSSean Bruno 	struct if_rxd_update iru;
1919fbec776dSAndrew Gallatin 	struct rxq_refill_cb_arg cb_arg;
19203db348b5SMarius Strobl 	struct mbuf *m;
19213db348b5SMarius Strobl 	caddr_t cl, *sd_cl;
19223db348b5SMarius Strobl 	struct mbuf **sd_m;
1923e035717eSSean Bruno 	bus_dmamap_t *sd_map;
1924fbec776dSAndrew Gallatin 	bus_addr_t bus_addr, *sd_ba;
19253db348b5SMarius Strobl 	int err, frag_idx, i, idx, n, pidx;
1926a1b799caSStephen Hurd 	qidx_t credits;
19274c7070dbSScott Long 
1928e035717eSSean Bruno 	sd_m = fl->ifl_sds.ifsd_m;
1929e035717eSSean Bruno 	sd_map = fl->ifl_sds.ifsd_map;
1930e035717eSSean Bruno 	sd_cl = fl->ifl_sds.ifsd_cl;
1931fbec776dSAndrew Gallatin 	sd_ba = fl->ifl_sds.ifsd_ba;
19323db348b5SMarius Strobl 	pidx = fl->ifl_pidx;
1933e035717eSSean Bruno 	idx = pidx;
19343db348b5SMarius Strobl 	frag_idx = fl->ifl_fragidx;
1935a1b799caSStephen Hurd 	credits = fl->ifl_credits;
1936e035717eSSean Bruno 
19373db348b5SMarius Strobl 	i = 0;
19384c7070dbSScott Long 	n = count;
19394c7070dbSScott Long 	MPASS(n > 0);
1940a1b799caSStephen Hurd 	MPASS(credits + n <= fl->ifl_size);
19414c7070dbSScott Long 
19424c7070dbSScott Long 	if (pidx < fl->ifl_cidx)
19434c7070dbSScott Long 		MPASS(pidx + n <= fl->ifl_cidx);
1944a1b799caSStephen Hurd 	if (pidx == fl->ifl_cidx && (credits < fl->ifl_size))
19454c7070dbSScott Long 		MPASS(fl->ifl_gen == 0);
19464c7070dbSScott Long 	if (pidx > fl->ifl_cidx)
19474c7070dbSScott Long 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
19484c7070dbSScott Long 
19494c7070dbSScott Long 	DBG_COUNTER_INC(fl_refills);
19504c7070dbSScott Long 	if (n > 8)
19514c7070dbSScott Long 		DBG_COUNTER_INC(fl_refills_large);
19522d873474SStephen Hurd 	iru_init(&iru, fl->ifl_rxq, fl->ifl_id);
19534c7070dbSScott Long 	while (n--) {
19544c7070dbSScott Long 		/*
19554c7070dbSScott Long 		 * We allocate an uninitialized mbuf + cluster, mbuf is
19564c7070dbSScott Long 		 * initialized after rx.
19574c7070dbSScott Long 		 *
19584c7070dbSScott Long 		 * If the cluster is still set then we know a minimum sized packet was received
19594c7070dbSScott Long 		 */
19603db348b5SMarius Strobl 		bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,
19613db348b5SMarius Strobl 		    &frag_idx);
19623db348b5SMarius Strobl 		if (frag_idx < 0)
196387890dbaSSean Bruno 			bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
19643db348b5SMarius Strobl 		MPASS(frag_idx >= 0);
196587890dbaSSean Bruno 		if ((cl = sd_cl[frag_idx]) == NULL) {
1966fbec776dSAndrew Gallatin 			if ((cl = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
19674c7070dbSScott Long 				break;
19684c7070dbSScott Long 
19694c7070dbSScott Long 			cb_arg.error = 0;
197095246abbSSean Bruno 			MPASS(sd_map != NULL);
1971bfce461eSMarius Strobl 			err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx],
19728a04b53dSKonstantin Belousov 			    cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg,
19738a04b53dSKonstantin Belousov 			    BUS_DMA_NOWAIT);
19744c7070dbSScott Long 			if (err != 0 || cb_arg.error) {
19754c7070dbSScott Long 				/*
19764c7070dbSScott Long 				 * !zone_pack ?
19774c7070dbSScott Long 				 */
19784c7070dbSScott Long 				if (fl->ifl_zone == zone_pack)
19794c7070dbSScott Long 					uma_zfree(fl->ifl_zone, cl);
1980fbec776dSAndrew Gallatin 				break;
19814c7070dbSScott Long 			}
19824c7070dbSScott Long 
1983fbec776dSAndrew Gallatin 			sd_ba[frag_idx] =  bus_addr = cb_arg.seg.ds_addr;
198487890dbaSSean Bruno 			sd_cl[frag_idx] = cl;
1985fbec776dSAndrew Gallatin #if MEMORY_LOGGING
1986fbec776dSAndrew Gallatin 			fl->ifl_cl_enqueued++;
1987fbec776dSAndrew Gallatin #endif
1988fbec776dSAndrew Gallatin 		} else {
1989fbec776dSAndrew Gallatin 			bus_addr = sd_ba[frag_idx];
1990fbec776dSAndrew Gallatin 		}
199195dcf343SMarius Strobl 		bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx],
199295dcf343SMarius Strobl 		    BUS_DMASYNC_PREREAD);
1993fbec776dSAndrew Gallatin 
19946d49b41eSAndrew Gallatin 		if (sd_m[frag_idx] == NULL) {
1995fbec776dSAndrew Gallatin 			if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
1996fbec776dSAndrew Gallatin 				break;
1997fbec776dSAndrew Gallatin 			}
199887890dbaSSean Bruno 			sd_m[frag_idx] = m;
19996d49b41eSAndrew Gallatin 		}
20003db348b5SMarius Strobl 		bit_set(fl->ifl_rx_bitmap, frag_idx);
2001fbec776dSAndrew Gallatin #if MEMORY_LOGGING
2002fbec776dSAndrew Gallatin 		fl->ifl_m_enqueued++;
2003fbec776dSAndrew Gallatin #endif
2004fbec776dSAndrew Gallatin 
2005fbec776dSAndrew Gallatin 		DBG_COUNTER_INC(rx_allocs);
200687890dbaSSean Bruno 		fl->ifl_rxd_idxs[i] = frag_idx;
20074c7070dbSScott Long 		fl->ifl_bus_addrs[i] = bus_addr;
20084c7070dbSScott Long 		fl->ifl_vm_addrs[i] = cl;
2009a1b799caSStephen Hurd 		credits++;
20104c7070dbSScott Long 		i++;
2011a1b799caSStephen Hurd 		MPASS(credits <= fl->ifl_size);
2012e035717eSSean Bruno 		if (++idx == fl->ifl_size) {
20134c7070dbSScott Long 			fl->ifl_gen = 1;
2014e035717eSSean Bruno 			idx = 0;
20154c7070dbSScott Long 		}
20164c7070dbSScott Long 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
201795246abbSSean Bruno 			iru.iru_pidx = pidx;
201895246abbSSean Bruno 			iru.iru_count = i;
201995246abbSSean Bruno 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
20204c7070dbSScott Long 			i = 0;
2021e035717eSSean Bruno 			pidx = idx;
2022fa5416a8SSean Bruno 			fl->ifl_pidx = idx;
2023a1b799caSStephen Hurd 			fl->ifl_credits = credits;
202487890dbaSSean Bruno 		}
20254c7070dbSScott Long 	}
2026fbec776dSAndrew Gallatin 
2027a1b799caSStephen Hurd 	if (i) {
2028a1b799caSStephen Hurd 		iru.iru_pidx = pidx;
2029a1b799caSStephen Hurd 		iru.iru_count = i;
2030a1b799caSStephen Hurd 		ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
2031a1b799caSStephen Hurd 		fl->ifl_pidx = idx;
2032a1b799caSStephen Hurd 		fl->ifl_credits = credits;
2033a1b799caSStephen Hurd 	}
20344c7070dbSScott Long 	DBG_COUNTER_INC(rxd_flush);
20354c7070dbSScott Long 	if (fl->ifl_pidx == 0)
20364c7070dbSScott Long 		pidx = fl->ifl_size - 1;
20374c7070dbSScott Long 	else
20384c7070dbSScott Long 		pidx = fl->ifl_pidx - 1;
203995246abbSSean Bruno 
204095246abbSSean Bruno 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
204195246abbSSean Bruno 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
20424c7070dbSScott Long 	ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
20439e9b738aSPatrick Kelsey 	fl->ifl_fragidx = frag_idx + 1;
20449e9b738aSPatrick Kelsey 	if (fl->ifl_fragidx == fl->ifl_size)
20459e9b738aSPatrick Kelsey 		fl->ifl_fragidx = 0;
2046fb1a29b4SHans Petter Selasky 
2047fb1a29b4SHans Petter Selasky 	return (n == -1 ? 0 : IFLIB_RXEOF_EMPTY);
20484c7070dbSScott Long }
20494c7070dbSScott Long 
2050fb1a29b4SHans Petter Selasky static __inline uint8_t
20513caff188SPatrick Kelsey __iflib_fl_refill_all(if_ctx_t ctx, iflib_fl_t fl)
20524c7070dbSScott Long {
20534c7070dbSScott Long 	/* we avoid allowing pidx to catch up with cidx as it confuses ixl */
20544c7070dbSScott Long 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
20554c7070dbSScott Long #ifdef INVARIANTS
20564c7070dbSScott Long 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
20574c7070dbSScott Long #endif
20584c7070dbSScott Long 
20594c7070dbSScott Long 	MPASS(fl->ifl_credits <= fl->ifl_size);
20604c7070dbSScott Long 	MPASS(reclaimable == delta);
20614c7070dbSScott Long 
20624c7070dbSScott Long 	if (reclaimable > 0)
20633caff188SPatrick Kelsey 		return (_iflib_fl_refill(ctx, fl, reclaimable));
2064fb1a29b4SHans Petter Selasky 	return (0);
20654c7070dbSScott Long }
20664c7070dbSScott Long 
206777c1fcecSEric Joyner uint8_t
206877c1fcecSEric Joyner iflib_in_detach(if_ctx_t ctx)
206977c1fcecSEric Joyner {
207077c1fcecSEric Joyner 	bool in_detach;
20711722eeacSMarius Strobl 
207277c1fcecSEric Joyner 	STATE_LOCK(ctx);
207377c1fcecSEric Joyner 	in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH);
207477c1fcecSEric Joyner 	STATE_UNLOCK(ctx);
207577c1fcecSEric Joyner 	return (in_detach);
207677c1fcecSEric Joyner }
207777c1fcecSEric Joyner 
20784c7070dbSScott Long static void
20794c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl)
20804c7070dbSScott Long {
20814c7070dbSScott Long 	iflib_dma_info_t idi = fl->ifl_ifdi;
20828a04b53dSKonstantin Belousov 	bus_dmamap_t sd_map;
20834c7070dbSScott Long 	uint32_t i;
20844c7070dbSScott Long 
20854c7070dbSScott Long 	for (i = 0; i < fl->ifl_size; i++) {
2086e035717eSSean Bruno 		struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
2087e035717eSSean Bruno 		caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
20884c7070dbSScott Long 
2089fbec776dSAndrew Gallatin 		if (*sd_cl != NULL) {
20908a04b53dSKonstantin Belousov 			sd_map = fl->ifl_sds.ifsd_map[i];
2091bfce461eSMarius Strobl 			bus_dmamap_sync(fl->ifl_buf_tag, sd_map,
20928a04b53dSKonstantin Belousov 			    BUS_DMASYNC_POSTREAD);
2093bfce461eSMarius Strobl 			bus_dmamap_unload(fl->ifl_buf_tag, sd_map);
2094fbec776dSAndrew Gallatin 			if (*sd_cl != NULL)
2095fbec776dSAndrew Gallatin 				uma_zfree(fl->ifl_zone, *sd_cl);
2096e035717eSSean Bruno 			if (*sd_m != NULL) {
2097e035717eSSean Bruno 				m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
2098e035717eSSean Bruno 				uma_zfree(zone_mbuf, *sd_m);
2099e035717eSSean Bruno 			}
21004c7070dbSScott Long 		} else {
2101e035717eSSean Bruno 			MPASS(*sd_cl == NULL);
2102e035717eSSean Bruno 			MPASS(*sd_m == NULL);
21034c7070dbSScott Long 		}
21044c7070dbSScott Long #if MEMORY_LOGGING
21054c7070dbSScott Long 		fl->ifl_m_dequeued++;
21064c7070dbSScott Long 		fl->ifl_cl_dequeued++;
21074c7070dbSScott Long #endif
2108e035717eSSean Bruno 		*sd_cl = NULL;
2109e035717eSSean Bruno 		*sd_m = NULL;
21104c7070dbSScott Long 	}
211195246abbSSean Bruno #ifdef INVARIANTS
211295246abbSSean Bruno 	for (i = 0; i < fl->ifl_size; i++) {
211395246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
211495246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
211595246abbSSean Bruno 	}
211695246abbSSean Bruno #endif
21174c7070dbSScott Long 	/*
21184c7070dbSScott Long 	 * Reset free list values
21194c7070dbSScott Long 	 */
212087890dbaSSean Bruno 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
21214c7070dbSScott Long 	bzero(idi->idi_vaddr, idi->idi_size);
21224c7070dbSScott Long }
21234c7070dbSScott Long 
21244c7070dbSScott Long /*********************************************************************
21254c7070dbSScott Long  *
21261722eeacSMarius Strobl  *  Initialize a free list and its buffers.
21274c7070dbSScott Long  *
21284c7070dbSScott Long  **********************************************************************/
21294c7070dbSScott Long static int
21304c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl)
21314c7070dbSScott Long {
21324c7070dbSScott Long 	iflib_rxq_t rxq = fl->ifl_rxq;
21334c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
2134b3813609SPatrick Kelsey 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2135b3813609SPatrick Kelsey 	int qidx;
21364c7070dbSScott Long 
21377274b2f6SStephen Hurd 	bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1);
21384c7070dbSScott Long 	/*
21394c7070dbSScott Long 	** Free current RX buffer structs and their mbufs
21404c7070dbSScott Long 	*/
21414c7070dbSScott Long 	iflib_fl_bufs_free(fl);
21424c7070dbSScott Long 	/* Now replenish the mbufs */
21434c7070dbSScott Long 	MPASS(fl->ifl_credits == 0);
2144b3813609SPatrick Kelsey 	qidx = rxq->ifr_fl_offset + fl->ifl_id;
2145b3813609SPatrick Kelsey 	if (scctx->isc_rxd_buf_size[qidx] != 0)
2146b3813609SPatrick Kelsey 		fl->ifl_buf_size = scctx->isc_rxd_buf_size[qidx];
2147b3813609SPatrick Kelsey 	else
21481b9d9394SEric Joyner 		fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz;
2149b3813609SPatrick Kelsey 	/*
2150b3813609SPatrick Kelsey 	 * ifl_buf_size may be a driver-supplied value, so pull it up
2151b3813609SPatrick Kelsey 	 * to the selected mbuf size.
2152b3813609SPatrick Kelsey 	 */
2153b3813609SPatrick Kelsey 	fl->ifl_buf_size = iflib_get_mbuf_size_for(fl->ifl_buf_size);
21544c7070dbSScott Long 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
21554c7070dbSScott Long 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
21564c7070dbSScott Long 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
21574c7070dbSScott Long 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
21584c7070dbSScott Long 
21594c7070dbSScott Long 
21604c7070dbSScott Long 	/* avoid pre-allocating zillions of clusters to an idle card
21614c7070dbSScott Long 	 * potentially speeding up attach
21624c7070dbSScott Long 	 */
2163fb1a29b4SHans Petter Selasky 	(void) _iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
21644c7070dbSScott Long 	MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
21654c7070dbSScott Long 	if (min(128, fl->ifl_size) != fl->ifl_credits)
21664c7070dbSScott Long 		return (ENOBUFS);
21674c7070dbSScott Long 	/*
21684c7070dbSScott Long 	 * handle failure
21694c7070dbSScott Long 	 */
21704c7070dbSScott Long 	MPASS(rxq != NULL);
21714c7070dbSScott Long 	MPASS(fl->ifl_ifdi != NULL);
21724c7070dbSScott Long 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
21734c7070dbSScott Long 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
21744c7070dbSScott Long 	return (0);
21754c7070dbSScott Long }
21764c7070dbSScott Long 
21774c7070dbSScott Long /*********************************************************************
21784c7070dbSScott Long  *
21794c7070dbSScott Long  *  Free receive ring data structures
21804c7070dbSScott Long  *
21814c7070dbSScott Long  **********************************************************************/
21824c7070dbSScott Long static void
21834c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq)
21844c7070dbSScott Long {
21854c7070dbSScott Long 	iflib_fl_t fl;
21868a04b53dSKonstantin Belousov 	int i, j;
21874c7070dbSScott Long 
21884c7070dbSScott Long 	if (rxq->ifr_fl != NULL) {
21894c7070dbSScott Long 		for (i = 0; i < rxq->ifr_nfl; i++) {
21904c7070dbSScott Long 			fl = &rxq->ifr_fl[i];
2191bfce461eSMarius Strobl 			if (fl->ifl_buf_tag != NULL) {
21928a04b53dSKonstantin Belousov 				if (fl->ifl_sds.ifsd_map != NULL) {
219377102fd6SAndrew Gallatin 					for (j = 0; j < fl->ifl_size; j++) {
21948a04b53dSKonstantin Belousov 						bus_dmamap_sync(
2195bfce461eSMarius Strobl 						    fl->ifl_buf_tag,
219677102fd6SAndrew Gallatin 						    fl->ifl_sds.ifsd_map[j],
21978a04b53dSKonstantin Belousov 						    BUS_DMASYNC_POSTREAD);
21988a04b53dSKonstantin Belousov 						bus_dmamap_unload(
2199bfce461eSMarius Strobl 						    fl->ifl_buf_tag,
220077102fd6SAndrew Gallatin 						    fl->ifl_sds.ifsd_map[j]);
2201db8e8f1eSEric Joyner 						bus_dmamap_destroy(
2202db8e8f1eSEric Joyner 						    fl->ifl_buf_tag,
2203db8e8f1eSEric Joyner 						    fl->ifl_sds.ifsd_map[j]);
22048a04b53dSKonstantin Belousov 					}
22058a04b53dSKonstantin Belousov 				}
2206bfce461eSMarius Strobl 				bus_dma_tag_destroy(fl->ifl_buf_tag);
2207bfce461eSMarius Strobl 				fl->ifl_buf_tag = NULL;
22084c7070dbSScott Long 			}
2209e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_m, M_IFLIB);
2210e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_cl, M_IFLIB);
2211fbec776dSAndrew Gallatin 			free(fl->ifl_sds.ifsd_ba, M_IFLIB);
2212e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_map, M_IFLIB);
2213e035717eSSean Bruno 			fl->ifl_sds.ifsd_m = NULL;
2214e035717eSSean Bruno 			fl->ifl_sds.ifsd_cl = NULL;
2215fbec776dSAndrew Gallatin 			fl->ifl_sds.ifsd_ba = NULL;
2216e035717eSSean Bruno 			fl->ifl_sds.ifsd_map = NULL;
22174c7070dbSScott Long 		}
22184c7070dbSScott Long 		free(rxq->ifr_fl, M_IFLIB);
22194c7070dbSScott Long 		rxq->ifr_fl = NULL;
2220244e7cffSEric Joyner 		free(rxq->ifr_ifdi, M_IFLIB);
2221244e7cffSEric Joyner 		rxq->ifr_ifdi = NULL;
22221722eeacSMarius Strobl 		rxq->ifr_cq_cidx = 0;
22234c7070dbSScott Long 	}
22244c7070dbSScott Long }
22254c7070dbSScott Long 
22264c7070dbSScott Long /*
22271722eeacSMarius Strobl  * Timer routine
22284c7070dbSScott Long  */
22294c7070dbSScott Long static void
22304c7070dbSScott Long iflib_timer(void *arg)
22314c7070dbSScott Long {
2232ab2e3f79SStephen Hurd 	iflib_txq_t txq = arg;
22334c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
2234ab2e3f79SStephen Hurd 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
2235dd7fbcf1SStephen Hurd 	uint64_t this_tick = ticks;
2236dd7fbcf1SStephen Hurd 	uint32_t reset_on = hz / 2;
22374c7070dbSScott Long 
22384c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
22394c7070dbSScott Long 		return;
22401722eeacSMarius Strobl 
22414c7070dbSScott Long 	/*
22424c7070dbSScott Long 	** Check on the state of the TX queue(s), this
22434c7070dbSScott Long 	** can be done without the lock because its RO
22444c7070dbSScott Long 	** and the HUNG state will be static if set.
22454c7070dbSScott Long 	*/
2246dd7fbcf1SStephen Hurd 	if (this_tick - txq->ift_last_timer_tick >= hz / 2) {
2247dd7fbcf1SStephen Hurd 		txq->ift_last_timer_tick = this_tick;
2248ab2e3f79SStephen Hurd 		IFDI_TIMER(ctx, txq->ift_id);
2249ab2e3f79SStephen Hurd 		if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
2250ab2e3f79SStephen Hurd 		    ((txq->ift_cleaned_prev == txq->ift_cleaned) ||
2251ab2e3f79SStephen Hurd 		     (sctx->isc_pause_frames == 0)))
2252ab2e3f79SStephen Hurd 			goto hung;
2253a9693502SSean Bruno 
2254f6afed72SEric Joyner 		if (txq->ift_qstatus != IFLIB_QUEUE_IDLE &&
2255f6afed72SEric Joyner 		    ifmp_ring_is_stalled(txq->ift_br)) {
2256f6afed72SEric Joyner 			KASSERT(ctx->ifc_link_state == LINK_STATE_UP, ("queue can't be marked as hung if interface is down"));
2257ab2e3f79SStephen Hurd 			txq->ift_qstatus = IFLIB_QUEUE_HUNG;
2258f6afed72SEric Joyner 		}
2259ab2e3f79SStephen Hurd 		txq->ift_cleaned_prev = txq->ift_cleaned;
2260dd7fbcf1SStephen Hurd 	}
2261dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP
2262dd7fbcf1SStephen Hurd 	if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP)
226395dcf343SMarius Strobl 		iflib_netmap_timer_adjust(ctx, txq, &reset_on);
2264dd7fbcf1SStephen Hurd #endif
2265ab2e3f79SStephen Hurd 	/* handle any laggards */
2266ab2e3f79SStephen Hurd 	if (txq->ift_db_pending)
2267ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
2268a9693502SSean Bruno 
2269ab2e3f79SStephen Hurd 	sctx->isc_pause_frames = 0;
2270d300df01SStephen Hurd 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
2271dd7fbcf1SStephen Hurd 		callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu);
2272ab2e3f79SStephen Hurd 	return;
22731722eeacSMarius Strobl 
2274ab2e3f79SStephen Hurd  hung:
22751722eeacSMarius Strobl 	device_printf(ctx->ifc_dev,
22761722eeacSMarius Strobl 	    "Watchdog timeout (TX: %d desc avail: %d pidx: %d) -- resetting\n",
2277ab2e3f79SStephen Hurd 	    txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
22787b610b60SSean Bruno 	STATE_LOCK(ctx);
22797b610b60SSean Bruno 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
22807b610b60SSean Bruno 	ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET);
2281940f62d6SEric Joyner 	iflib_admin_intr_deferred(ctx);
228246fa0c25SEric Joyner 	STATE_UNLOCK(ctx);
22834c7070dbSScott Long }
22844c7070dbSScott Long 
2285b3813609SPatrick Kelsey static uint16_t
2286b3813609SPatrick Kelsey iflib_get_mbuf_size_for(unsigned int size)
2287b3813609SPatrick Kelsey {
2288b3813609SPatrick Kelsey 
2289b3813609SPatrick Kelsey 	if (size <= MCLBYTES)
2290b3813609SPatrick Kelsey 		return (MCLBYTES);
2291b3813609SPatrick Kelsey 	else
2292b3813609SPatrick Kelsey 		return (MJUMPAGESIZE);
2293b3813609SPatrick Kelsey }
2294b3813609SPatrick Kelsey 
22954c7070dbSScott Long static void
22961b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(if_ctx_t ctx)
22971b9d9394SEric Joyner {
22981b9d9394SEric Joyner 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
22991b9d9394SEric Joyner 
23001b9d9394SEric Joyner 	/*
23011b9d9394SEric Joyner 	 * XXX don't set the max_frame_size to larger
23021b9d9394SEric Joyner 	 * than the hardware can handle
23031b9d9394SEric Joyner 	 */
2304b3813609SPatrick Kelsey 	ctx->ifc_rx_mbuf_sz =
2305b3813609SPatrick Kelsey 	    iflib_get_mbuf_size_for(sctx->isc_max_frame_size);
23061b9d9394SEric Joyner }
23071b9d9394SEric Joyner 
23081b9d9394SEric Joyner uint32_t
23091b9d9394SEric Joyner iflib_get_rx_mbuf_sz(if_ctx_t ctx)
23101b9d9394SEric Joyner {
23111722eeacSMarius Strobl 
23121b9d9394SEric Joyner 	return (ctx->ifc_rx_mbuf_sz);
23131b9d9394SEric Joyner }
23141b9d9394SEric Joyner 
23151b9d9394SEric Joyner static void
23164c7070dbSScott Long iflib_init_locked(if_ctx_t ctx)
23174c7070dbSScott Long {
23184c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
23191248952aSSean Bruno 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
23204c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
23214c7070dbSScott Long 	iflib_fl_t fl;
23224c7070dbSScott Long 	iflib_txq_t txq;
23234c7070dbSScott Long 	iflib_rxq_t rxq;
2324ab2e3f79SStephen Hurd 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
23254c7070dbSScott Long 
23264c7070dbSScott Long 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
23274c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
23284c7070dbSScott Long 
23291248952aSSean Bruno 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
23301248952aSSean Bruno 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
23314c7070dbSScott Long 	/* Set hardware offload abilities */
23324c7070dbSScott Long 	if_clearhwassist(ifp);
23334c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
23341248952aSSean Bruno 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
23354c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
23361248952aSSean Bruno 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
23374c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO4)
23384c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
23394c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO6)
23404c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
23414c7070dbSScott Long 
23424c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
23434c7070dbSScott Long 		CALLOUT_LOCK(txq);
23444c7070dbSScott Long 		callout_stop(&txq->ift_timer);
23454c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
23464c7070dbSScott Long 		iflib_netmap_txq_init(ctx, txq);
23474c7070dbSScott Long 	}
23481b9d9394SEric Joyner 
23491b9d9394SEric Joyner 	/*
23501b9d9394SEric Joyner 	 * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so
23511b9d9394SEric Joyner 	 * that drivers can use the value when setting up the hardware receive
23521b9d9394SEric Joyner 	 * buffers.
23531b9d9394SEric Joyner 	 */
23541b9d9394SEric Joyner 	iflib_calc_rx_mbuf_sz(ctx);
23551b9d9394SEric Joyner 
235623ac9029SStephen Hurd #ifdef INVARIANTS
235723ac9029SStephen Hurd 	i = if_getdrvflags(ifp);
235823ac9029SStephen Hurd #endif
23594c7070dbSScott Long 	IFDI_INIT(ctx);
236023ac9029SStephen Hurd 	MPASS(if_getdrvflags(ifp) == i);
23614c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
236295246abbSSean Bruno 		/* XXX this should really be done on a per-queue basis */
2363d0d0ad0aSStephen Hurd 		if (if_getcapenable(ifp) & IFCAP_NETMAP) {
2364d0d0ad0aSStephen Hurd 			MPASS(rxq->ifr_id == i);
2365d0d0ad0aSStephen Hurd 			iflib_netmap_rxq_init(ctx, rxq);
236695246abbSSean Bruno 			continue;
2367d0d0ad0aSStephen Hurd 		}
23684c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
23694c7070dbSScott Long 			if (iflib_fl_setup(fl)) {
23703d10e9edSMarius Strobl 				device_printf(ctx->ifc_dev,
23713d10e9edSMarius Strobl 				    "setting up free list %d failed - "
23723d10e9edSMarius Strobl 				    "check cluster settings\n", j);
23734c7070dbSScott Long 				goto done;
23744c7070dbSScott Long 			}
23754c7070dbSScott Long 		}
23764c7070dbSScott Long 	}
23774c7070dbSScott Long done:
23784c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
23794c7070dbSScott Long 	IFDI_INTR_ENABLE(ctx);
23804c7070dbSScott Long 	txq = ctx->ifc_txqs;
23814c7070dbSScott Long 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
2382ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
2383ab2e3f79SStephen Hurd 			txq->ift_timer.c_cpu);
23844c7070dbSScott Long }
23854c7070dbSScott Long 
23864c7070dbSScott Long static int
23874c7070dbSScott Long iflib_media_change(if_t ifp)
23884c7070dbSScott Long {
23894c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
23904c7070dbSScott Long 	int err;
23914c7070dbSScott Long 
23924c7070dbSScott Long 	CTX_LOCK(ctx);
23934c7070dbSScott Long 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
23944c7070dbSScott Long 		iflib_init_locked(ctx);
23954c7070dbSScott Long 	CTX_UNLOCK(ctx);
23964c7070dbSScott Long 	return (err);
23974c7070dbSScott Long }
23984c7070dbSScott Long 
23994c7070dbSScott Long static void
24004c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
24014c7070dbSScott Long {
24024c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
24034c7070dbSScott Long 
24044c7070dbSScott Long 	CTX_LOCK(ctx);
2405ab2e3f79SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
24064c7070dbSScott Long 	IFDI_MEDIA_STATUS(ctx, ifmr);
24074c7070dbSScott Long 	CTX_UNLOCK(ctx);
24084c7070dbSScott Long }
24094c7070dbSScott Long 
241009f6ff4fSMatt Macy void
24114c7070dbSScott Long iflib_stop(if_ctx_t ctx)
24124c7070dbSScott Long {
24134c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
24144c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
24154c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
24164d261ce2SStephen Hurd 	if_shared_ctx_t sctx = ctx->ifc_sctx;
24174c7070dbSScott Long 	iflib_dma_info_t di;
24184c7070dbSScott Long 	iflib_fl_t fl;
24194c7070dbSScott Long 	int i, j;
24204c7070dbSScott Long 
24214c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
24224c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
24234c7070dbSScott Long 
24244c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
2425ab2e3f79SStephen Hurd 	DELAY(1000);
2426da69b8f9SSean Bruno 	IFDI_STOP(ctx);
2427ab2e3f79SStephen Hurd 	DELAY(1000);
24284c7070dbSScott Long 
2429da69b8f9SSean Bruno 	iflib_debug_reset();
24304c7070dbSScott Long 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
24314c7070dbSScott Long 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
24324c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
24334c7070dbSScott Long 
2434226fb85dSStephen Hurd 		CALLOUT_LOCK(txq);
2435226fb85dSStephen Hurd 		callout_stop(&txq->ift_timer);
2436226fb85dSStephen Hurd 		CALLOUT_UNLOCK(txq);
2437226fb85dSStephen Hurd 
24384c7070dbSScott Long 		/* clean any enqueued buffers */
2439da69b8f9SSean Bruno 		iflib_ifmp_purge(txq);
24404c7070dbSScott Long 		/* Free any existing tx buffers. */
244123ac9029SStephen Hurd 		for (j = 0; j < txq->ift_size; j++) {
24424c7070dbSScott Long 			iflib_txsd_free(ctx, txq, j);
24434c7070dbSScott Long 		}
2444ab2e3f79SStephen Hurd 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2445ab2e3f79SStephen Hurd 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
24464c7070dbSScott Long 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
24474c7070dbSScott Long 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
2448ab2e3f79SStephen Hurd 		txq->ift_pullups = 0;
244995246abbSSean Bruno 		ifmp_ring_reset_stats(txq->ift_br);
24504d261ce2SStephen Hurd 		for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++)
24514c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
24524c7070dbSScott Long 	}
24534c7070dbSScott Long 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
24544c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
24554c7070dbSScott Long 
24561722eeacSMarius Strobl 		rxq->ifr_cq_cidx = 0;
24574d261ce2SStephen Hurd 		for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++)
24584c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
24594c7070dbSScott Long 		/* also resets the free lists pidx/cidx */
24604c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
24614c7070dbSScott Long 			iflib_fl_bufs_free(fl);
24624c7070dbSScott Long 	}
24634c7070dbSScott Long }
24644c7070dbSScott Long 
246595246abbSSean Bruno static inline caddr_t
246695246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx)
246795246abbSSean Bruno {
246895246abbSSean Bruno 	qidx_t size;
246995246abbSSean Bruno 	int nrxd;
247095246abbSSean Bruno 	caddr_t start, end, cur, next;
247195246abbSSean Bruno 
247295246abbSSean Bruno 	nrxd = fl->ifl_size;
247395246abbSSean Bruno 	size = fl->ifl_rxd_size;
247495246abbSSean Bruno 	start = fl->ifl_ifdi->idi_vaddr;
247595246abbSSean Bruno 
247695246abbSSean Bruno 	if (__predict_false(size == 0))
247795246abbSSean Bruno 		return (start);
247895246abbSSean Bruno 	cur = start + size*cidx;
247995246abbSSean Bruno 	end = start + size*nrxd;
248095246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
248195246abbSSean Bruno 	return (next < end ? next : start);
248295246abbSSean Bruno }
248395246abbSSean Bruno 
2484e035717eSSean Bruno static inline void
2485e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx)
2486e035717eSSean Bruno {
2487e035717eSSean Bruno 	int nextptr;
2488e035717eSSean Bruno 	int nrxd = fl->ifl_size;
248995246abbSSean Bruno 	caddr_t next_rxd;
249095246abbSSean Bruno 
2491e035717eSSean Bruno 
2492e035717eSSean Bruno 	nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2493e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2494e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
249595246abbSSean Bruno 	next_rxd = calc_next_rxd(fl, cidx);
249695246abbSSean Bruno 	prefetch(next_rxd);
2497e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2498e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2499e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2500e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2501e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2502e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2503e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2504e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2505e035717eSSean Bruno }
2506e035717eSSean Bruno 
25076d49b41eSAndrew Gallatin static struct mbuf *
25086d49b41eSAndrew Gallatin rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd,
25096d49b41eSAndrew Gallatin     int *pf_rv, if_rxd_info_t ri)
25104c7070dbSScott Long {
2511e035717eSSean Bruno 	bus_dmamap_t map;
25124c7070dbSScott Long 	iflib_fl_t fl;
25136d49b41eSAndrew Gallatin 	caddr_t payload;
25146d49b41eSAndrew Gallatin 	struct mbuf *m;
25156d49b41eSAndrew Gallatin 	int flid, cidx, len, next;
25164c7070dbSScott Long 
251795246abbSSean Bruno 	map = NULL;
25184c7070dbSScott Long 	flid = irf->irf_flid;
25194c7070dbSScott Long 	cidx = irf->irf_idx;
25204c7070dbSScott Long 	fl = &rxq->ifr_fl[flid];
252195246abbSSean Bruno 	sd->ifsd_fl = fl;
25226d49b41eSAndrew Gallatin 	m = fl->ifl_sds.ifsd_m[cidx];
252395246abbSSean Bruno 	sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
25244c7070dbSScott Long 	fl->ifl_credits--;
25254c7070dbSScott Long #if MEMORY_LOGGING
25264c7070dbSScott Long 	fl->ifl_m_dequeued++;
25274c7070dbSScott Long #endif
252895246abbSSean Bruno 	if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
2529e035717eSSean Bruno 		prefetch_pkts(fl, cidx);
2530e035717eSSean Bruno 	next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2531e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_map[next]);
2532e035717eSSean Bruno 	map = fl->ifl_sds.ifsd_map[cidx];
25334c7070dbSScott Long 
2534bfce461eSMarius Strobl 	bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD);
25356d49b41eSAndrew Gallatin 
25364f2beb72SPatrick Kelsey 	if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL &&
25374f2beb72SPatrick Kelsey 	    irf->irf_len != 0) {
25386d49b41eSAndrew Gallatin 		payload  = *sd->ifsd_cl;
25396d49b41eSAndrew Gallatin 		payload +=  ri->iri_pad;
25406d49b41eSAndrew Gallatin 		len = ri->iri_len - ri->iri_pad;
25416d49b41eSAndrew Gallatin 		*pf_rv = pfil_run_hooks(rxq->pfil, payload, ri->iri_ifp,
25426d49b41eSAndrew Gallatin 		    len | PFIL_MEMPTR | PFIL_IN, NULL);
25436d49b41eSAndrew Gallatin 		switch (*pf_rv) {
25446d49b41eSAndrew Gallatin 		case PFIL_DROPPED:
25456d49b41eSAndrew Gallatin 		case PFIL_CONSUMED:
25466d49b41eSAndrew Gallatin 			/*
25476d49b41eSAndrew Gallatin 			 * The filter ate it.  Everything is recycled.
25486d49b41eSAndrew Gallatin 			 */
25496d49b41eSAndrew Gallatin 			m = NULL;
25506d49b41eSAndrew Gallatin 			unload = 0;
25516d49b41eSAndrew Gallatin 			break;
25526d49b41eSAndrew Gallatin 		case PFIL_REALLOCED:
25536d49b41eSAndrew Gallatin 			/*
25546d49b41eSAndrew Gallatin 			 * The filter copied it.  Everything is recycled.
25556d49b41eSAndrew Gallatin 			 */
25566d49b41eSAndrew Gallatin 			m = pfil_mem2mbuf(payload);
25576d49b41eSAndrew Gallatin 			unload = 0;
25586d49b41eSAndrew Gallatin 			break;
25596d49b41eSAndrew Gallatin 		case PFIL_PASS:
25606d49b41eSAndrew Gallatin 			/*
25616d49b41eSAndrew Gallatin 			 * Filter said it was OK, so receive like
25626d49b41eSAndrew Gallatin 			 * normal
25636d49b41eSAndrew Gallatin 			 */
25646d49b41eSAndrew Gallatin 			fl->ifl_sds.ifsd_m[cidx] = NULL;
25656d49b41eSAndrew Gallatin 			break;
25666d49b41eSAndrew Gallatin 		default:
25676d49b41eSAndrew Gallatin 			MPASS(0);
25686d49b41eSAndrew Gallatin 		}
25696d49b41eSAndrew Gallatin 	} else {
25706d49b41eSAndrew Gallatin 		fl->ifl_sds.ifsd_m[cidx] = NULL;
25716d49b41eSAndrew Gallatin 		*pf_rv = PFIL_PASS;
25726d49b41eSAndrew Gallatin 	}
25736d49b41eSAndrew Gallatin 
25744f2beb72SPatrick Kelsey 	if (unload && irf->irf_len != 0)
2575bfce461eSMarius Strobl 		bus_dmamap_unload(fl->ifl_buf_tag, map);
257695246abbSSean Bruno 	fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1);
257795246abbSSean Bruno 	if (__predict_false(fl->ifl_cidx == 0))
25784c7070dbSScott Long 		fl->ifl_gen = 0;
257987890dbaSSean Bruno 	bit_clear(fl->ifl_rx_bitmap, cidx);
25806d49b41eSAndrew Gallatin 	return (m);
25814c7070dbSScott Long }
25824c7070dbSScott Long 
25834c7070dbSScott Long static struct mbuf *
25846d49b41eSAndrew Gallatin assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv)
25854c7070dbSScott Long {
258695246abbSSean Bruno 	struct mbuf *m, *mh, *mt;
258795246abbSSean Bruno 	caddr_t cl;
25886d49b41eSAndrew Gallatin 	int  *pf_rv_ptr, flags, i, padlen;
25896d49b41eSAndrew Gallatin 	bool consumed;
25904c7070dbSScott Long 
25914c7070dbSScott Long 	i = 0;
259223ac9029SStephen Hurd 	mh = NULL;
25936d49b41eSAndrew Gallatin 	consumed = false;
25946d49b41eSAndrew Gallatin 	*pf_rv = PFIL_PASS;
25956d49b41eSAndrew Gallatin 	pf_rv_ptr = pf_rv;
25964c7070dbSScott Long 	do {
25976d49b41eSAndrew Gallatin 		m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd,
25986d49b41eSAndrew Gallatin 		    pf_rv_ptr, ri);
25994c7070dbSScott Long 
260095246abbSSean Bruno 		MPASS(*sd->ifsd_cl != NULL);
260123ac9029SStephen Hurd 
26026d49b41eSAndrew Gallatin 		/*
26036d49b41eSAndrew Gallatin 		 * Exclude zero-length frags & frags from
26046d49b41eSAndrew Gallatin 		 * packets the filter has consumed or dropped
26056d49b41eSAndrew Gallatin 		 */
26066d49b41eSAndrew Gallatin 		if (ri->iri_frags[i].irf_len == 0 || consumed ||
26076d49b41eSAndrew Gallatin 		    *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) {
26086d49b41eSAndrew Gallatin 			if (mh == NULL) {
26096d49b41eSAndrew Gallatin 				/* everything saved here */
26106d49b41eSAndrew Gallatin 				consumed = true;
26116d49b41eSAndrew Gallatin 				pf_rv_ptr = NULL;
261223ac9029SStephen Hurd 				continue;
261323ac9029SStephen Hurd 			}
26146d49b41eSAndrew Gallatin 			/* XXX we can save the cluster here, but not the mbuf */
26156d49b41eSAndrew Gallatin 			m_init(m, M_NOWAIT, MT_DATA, 0);
26166d49b41eSAndrew Gallatin 			m_free(m);
26176d49b41eSAndrew Gallatin 			continue;
26186d49b41eSAndrew Gallatin 		}
261923ac9029SStephen Hurd 		if (mh == NULL) {
26204c7070dbSScott Long 			flags = M_PKTHDR|M_EXT;
26214c7070dbSScott Long 			mh = mt = m;
26224c7070dbSScott Long 			padlen = ri->iri_pad;
26234c7070dbSScott Long 		} else {
26244c7070dbSScott Long 			flags = M_EXT;
26254c7070dbSScott Long 			mt->m_next = m;
26264c7070dbSScott Long 			mt = m;
26274c7070dbSScott Long 			/* assuming padding is only on the first fragment */
26284c7070dbSScott Long 			padlen = 0;
26294c7070dbSScott Long 		}
263095246abbSSean Bruno 		cl = *sd->ifsd_cl;
263195246abbSSean Bruno 		*sd->ifsd_cl = NULL;
26324c7070dbSScott Long 
26334c7070dbSScott Long 		/* Can these two be made one ? */
26344c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, flags);
263595246abbSSean Bruno 		m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
26364c7070dbSScott Long 		/*
26374c7070dbSScott Long 		 * These must follow m_init and m_cljset
26384c7070dbSScott Long 		 */
26394c7070dbSScott Long 		m->m_data += padlen;
26404c7070dbSScott Long 		ri->iri_len -= padlen;
264123ac9029SStephen Hurd 		m->m_len = ri->iri_frags[i].irf_len;
26424c7070dbSScott Long 	} while (++i < ri->iri_nfrags);
26434c7070dbSScott Long 
26444c7070dbSScott Long 	return (mh);
26454c7070dbSScott Long }
26464c7070dbSScott Long 
26474c7070dbSScott Long /*
26484c7070dbSScott Long  * Process one software descriptor
26494c7070dbSScott Long  */
26504c7070dbSScott Long static struct mbuf *
26514c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
26524c7070dbSScott Long {
265395246abbSSean Bruno 	struct if_rxsd sd;
26544c7070dbSScott Long 	struct mbuf *m;
26556d49b41eSAndrew Gallatin 	int pf_rv;
26564c7070dbSScott Long 
26574c7070dbSScott Long 	/* should I merge this back in now that the two paths are basically duplicated? */
265823ac9029SStephen Hurd 	if (ri->iri_nfrags == 1 &&
26594f2beb72SPatrick Kelsey 	    ri->iri_frags[0].irf_len != 0 &&
266018628b74SMark Johnston 	    ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) {
26616d49b41eSAndrew Gallatin 		m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd,
26626d49b41eSAndrew Gallatin 		    &pf_rv, ri);
26636d49b41eSAndrew Gallatin 		if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
26646d49b41eSAndrew Gallatin 			return (m);
26656d49b41eSAndrew Gallatin 		if (pf_rv == PFIL_PASS) {
26664c7070dbSScott Long 			m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
266795246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
266895246abbSSean Bruno 			if (!IP_ALIGNED(m))
266995246abbSSean Bruno 				m->m_data += 2;
267095246abbSSean Bruno #endif
267195246abbSSean Bruno 			memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
267223ac9029SStephen Hurd 			m->m_len = ri->iri_frags[0].irf_len;
26736d49b41eSAndrew Gallatin 		}
26744c7070dbSScott Long 	} else {
26756d49b41eSAndrew Gallatin 		m = assemble_segments(rxq, ri, &sd, &pf_rv);
26764f2beb72SPatrick Kelsey 		if (m == NULL)
26774f2beb72SPatrick Kelsey 			return (NULL);
26786d49b41eSAndrew Gallatin 		if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
26796d49b41eSAndrew Gallatin 			return (m);
26804c7070dbSScott Long 	}
26814c7070dbSScott Long 	m->m_pkthdr.len = ri->iri_len;
26824c7070dbSScott Long 	m->m_pkthdr.rcvif = ri->iri_ifp;
26834c7070dbSScott Long 	m->m_flags |= ri->iri_flags;
26844c7070dbSScott Long 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
26854c7070dbSScott Long 	m->m_pkthdr.flowid = ri->iri_flowid;
26864c7070dbSScott Long 	M_HASHTYPE_SET(m, ri->iri_rsstype);
26874c7070dbSScott Long 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
26884c7070dbSScott Long 	m->m_pkthdr.csum_data = ri->iri_csum_data;
26894c7070dbSScott Long 	return (m);
26904c7070dbSScott Long }
26914c7070dbSScott Long 
269235e4e998SStephen Hurd #if defined(INET6) || defined(INET)
2693fe1bcadaSStephen Hurd static void
2694fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6)
2695fe1bcadaSStephen Hurd {
2696fe1bcadaSStephen Hurd 	CURVNET_SET(lc->ifp->if_vnet);
2697fe1bcadaSStephen Hurd #if defined(INET6)
2698188adcb7SMarko Zec 	*v6 = V_ip6_forwarding;
2699fe1bcadaSStephen Hurd #endif
2700fe1bcadaSStephen Hurd #if defined(INET)
2701188adcb7SMarko Zec 	*v4 = V_ipforwarding;
2702fe1bcadaSStephen Hurd #endif
2703fe1bcadaSStephen Hurd 	CURVNET_RESTORE();
2704fe1bcadaSStephen Hurd }
2705fe1bcadaSStephen Hurd 
270635e4e998SStephen Hurd /*
270735e4e998SStephen Hurd  * Returns true if it's possible this packet could be LROed.
270835e4e998SStephen Hurd  * if it returns false, it is guaranteed that tcp_lro_rx()
270935e4e998SStephen Hurd  * would not return zero.
271035e4e998SStephen Hurd  */
271135e4e998SStephen Hurd static bool
2712fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding)
271335e4e998SStephen Hurd {
271435e4e998SStephen Hurd 	struct ether_header *eh;
271535e4e998SStephen Hurd 
271635e4e998SStephen Hurd 	eh = mtod(m, struct ether_header *);
27176aee0bfaSMarko Zec 	switch (eh->ether_type) {
2718abec4724SSean Bruno #if defined(INET6)
27196aee0bfaSMarko Zec 		case htons(ETHERTYPE_IPV6):
27206aee0bfaSMarko Zec 			return (!v6_forwarding);
2721abec4724SSean Bruno #endif
2722abec4724SSean Bruno #if defined (INET)
27236aee0bfaSMarko Zec 		case htons(ETHERTYPE_IP):
27246aee0bfaSMarko Zec 			return (!v4_forwarding);
2725abec4724SSean Bruno #endif
272635e4e998SStephen Hurd 	}
272735e4e998SStephen Hurd 
272835e4e998SStephen Hurd 	return false;
272935e4e998SStephen Hurd }
2730fe1bcadaSStephen Hurd #else
2731fe1bcadaSStephen Hurd static void
2732fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused)
2733fe1bcadaSStephen Hurd {
2734fe1bcadaSStephen Hurd }
273535e4e998SStephen Hurd #endif
273635e4e998SStephen Hurd 
2737fb1a29b4SHans Petter Selasky static void
2738fb1a29b4SHans Petter Selasky _task_fn_rx_watchdog(void *context)
2739fb1a29b4SHans Petter Selasky {
2740fb1a29b4SHans Petter Selasky 	iflib_rxq_t rxq = context;
2741fb1a29b4SHans Petter Selasky 
2742fb1a29b4SHans Petter Selasky 	GROUPTASK_ENQUEUE(&rxq->ifr_task);
2743fb1a29b4SHans Petter Selasky }
2744fb1a29b4SHans Petter Selasky 
2745fb1a29b4SHans Petter Selasky static uint8_t
274695246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
27474c7070dbSScott Long {
27481722eeacSMarius Strobl 	if_t ifp;
27494c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
27504c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
275123ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
27524c7070dbSScott Long 	int avail, i;
275395246abbSSean Bruno 	qidx_t *cidxp;
27544c7070dbSScott Long 	struct if_rxd_info ri;
27554c7070dbSScott Long 	int err, budget_left, rx_bytes, rx_pkts;
27564c7070dbSScott Long 	iflib_fl_t fl;
27574c7070dbSScott Long 	int lro_enabled;
2758f6cb0deaSMatt Macy 	bool v4_forwarding, v6_forwarding, lro_possible;
2759fb1a29b4SHans Petter Selasky 	uint8_t retval = 0;
276095246abbSSean Bruno 
27614c7070dbSScott Long 	/*
27624c7070dbSScott Long 	 * XXX early demux data packets so that if_input processing only handles
27634c7070dbSScott Long 	 * acks in interrupt context
27644c7070dbSScott Long 	 */
276520f63282SStephen Hurd 	struct mbuf *m, *mh, *mt, *mf;
27664c7070dbSScott Long 
27670b8df657SGleb Smirnoff 	NET_EPOCH_ASSERT();
27680b8df657SGleb Smirnoff 
2769f6cb0deaSMatt Macy 	lro_possible = v4_forwarding = v6_forwarding = false;
277095246abbSSean Bruno 	ifp = ctx->ifc_ifp;
27714c7070dbSScott Long 	mh = mt = NULL;
27724c7070dbSScott Long 	MPASS(budget > 0);
27734c7070dbSScott Long 	rx_pkts	= rx_bytes = 0;
277423ac9029SStephen Hurd 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
27754c7070dbSScott Long 		cidxp = &rxq->ifr_cq_cidx;
27764c7070dbSScott Long 	else
27774c7070dbSScott Long 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
277823ac9029SStephen Hurd 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
27794c7070dbSScott Long 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
27803caff188SPatrick Kelsey 			retval |= __iflib_fl_refill_all(ctx, fl);
27814c7070dbSScott Long 		DBG_COUNTER_INC(rx_unavail);
2782fb1a29b4SHans Petter Selasky 		return (retval);
27834c7070dbSScott Long 	}
27844c7070dbSScott Long 
27856d49b41eSAndrew Gallatin 	/* pfil needs the vnet to be set */
27866d49b41eSAndrew Gallatin 	CURVNET_SET_QUIET(ifp->if_vnet);
27878b8d9093SMarius Strobl 	for (budget_left = budget; budget_left > 0 && avail > 0;) {
27884c7070dbSScott Long 		if (__predict_false(!CTX_ACTIVE(ctx))) {
27894c7070dbSScott Long 			DBG_COUNTER_INC(rx_ctx_inactive);
27904c7070dbSScott Long 			break;
27914c7070dbSScott Long 		}
27924c7070dbSScott Long 		/*
27934c7070dbSScott Long 		 * Reset client set fields to their default values
27944c7070dbSScott Long 		 */
279595246abbSSean Bruno 		rxd_info_zero(&ri);
27964c7070dbSScott Long 		ri.iri_qsidx = rxq->ifr_id;
27974c7070dbSScott Long 		ri.iri_cidx = *cidxp;
279895246abbSSean Bruno 		ri.iri_ifp = ifp;
27994c7070dbSScott Long 		ri.iri_frags = rxq->ifr_frags;
28004c7070dbSScott Long 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
28014c7070dbSScott Long 
280295246abbSSean Bruno 		if (err)
280395246abbSSean Bruno 			goto err;
28046d49b41eSAndrew Gallatin 		rx_pkts += 1;
28056d49b41eSAndrew Gallatin 		rx_bytes += ri.iri_len;
280623ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
280723ac9029SStephen Hurd 			*cidxp = ri.iri_cidx;
280823ac9029SStephen Hurd 			/* Update our consumer index */
280995246abbSSean Bruno 			/* XXX NB: shurd - check if this is still safe */
28101722eeacSMarius Strobl 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0])
281123ac9029SStephen Hurd 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
28124c7070dbSScott Long 			/* was this only a completion queue message? */
28134c7070dbSScott Long 			if (__predict_false(ri.iri_nfrags == 0))
28144c7070dbSScott Long 				continue;
28154c7070dbSScott Long 		}
28164c7070dbSScott Long 		MPASS(ri.iri_nfrags != 0);
28174c7070dbSScott Long 		MPASS(ri.iri_len != 0);
28184c7070dbSScott Long 
28194c7070dbSScott Long 		/* will advance the cidx on the corresponding free lists */
28204c7070dbSScott Long 		m = iflib_rxd_pkt_get(rxq, &ri);
28218b8d9093SMarius Strobl 		avail--;
28228b8d9093SMarius Strobl 		budget_left--;
28234c7070dbSScott Long 		if (avail == 0 && budget_left)
282423ac9029SStephen Hurd 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
28254c7070dbSScott Long 
28266d49b41eSAndrew Gallatin 		if (__predict_false(m == NULL))
28274c7070dbSScott Long 			continue;
28286d49b41eSAndrew Gallatin 
28294c7070dbSScott Long 		/* imm_pkt: -- cxgb */
28304c7070dbSScott Long 		if (mh == NULL)
28314c7070dbSScott Long 			mh = mt = m;
28324c7070dbSScott Long 		else {
28334c7070dbSScott Long 			mt->m_nextpkt = m;
28344c7070dbSScott Long 			mt = m;
28354c7070dbSScott Long 		}
28364c7070dbSScott Long 	}
28376d49b41eSAndrew Gallatin 	CURVNET_RESTORE();
28384c7070dbSScott Long 	/* make sure that we can refill faster than drain */
28394c7070dbSScott Long 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
28403caff188SPatrick Kelsey 		retval |= __iflib_fl_refill_all(ctx, fl);
28414c7070dbSScott Long 
28424c7070dbSScott Long 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
2843fe1bcadaSStephen Hurd 	if (lro_enabled)
2844fe1bcadaSStephen Hurd 		iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding);
284520f63282SStephen Hurd 	mt = mf = NULL;
28464c7070dbSScott Long 	while (mh != NULL) {
28474c7070dbSScott Long 		m = mh;
28484c7070dbSScott Long 		mh = mh->m_nextpkt;
28494c7070dbSScott Long 		m->m_nextpkt = NULL;
285095246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
285195246abbSSean Bruno 		if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
285295246abbSSean Bruno 			continue;
285395246abbSSean Bruno #endif
28544c7070dbSScott Long 		rx_bytes += m->m_pkthdr.len;
28554c7070dbSScott Long 		rx_pkts++;
2856aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
285735e4e998SStephen Hurd 		if (lro_enabled) {
285835e4e998SStephen Hurd 			if (!lro_possible) {
2859fe1bcadaSStephen Hurd 				lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding);
286035e4e998SStephen Hurd 				if (lro_possible && mf != NULL) {
286135e4e998SStephen Hurd 					ifp->if_input(ifp, mf);
286235e4e998SStephen Hurd 					DBG_COUNTER_INC(rx_if_input);
286335e4e998SStephen Hurd 					mt = mf = NULL;
286435e4e998SStephen Hurd 				}
286535e4e998SStephen Hurd 			}
286625ac1dd5SStephen Hurd 			if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) ==
286725ac1dd5SStephen Hurd 			    (CSUM_L4_CALC|CSUM_L4_VALID)) {
286835e4e998SStephen Hurd 				if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
28694c7070dbSScott Long 					continue;
287020f63282SStephen Hurd 			}
287125ac1dd5SStephen Hurd 		}
2872aaeb188aSBjoern A. Zeeb #endif
287335e4e998SStephen Hurd 		if (lro_possible) {
287435e4e998SStephen Hurd 			ifp->if_input(ifp, m);
287535e4e998SStephen Hurd 			DBG_COUNTER_INC(rx_if_input);
287635e4e998SStephen Hurd 			continue;
287735e4e998SStephen Hurd 		}
287835e4e998SStephen Hurd 
287935e4e998SStephen Hurd 		if (mf == NULL)
288035e4e998SStephen Hurd 			mf = m;
288120f63282SStephen Hurd 		if (mt != NULL)
288220f63282SStephen Hurd 			mt->m_nextpkt = m;
288320f63282SStephen Hurd 		mt = m;
288420f63282SStephen Hurd 	}
288520f63282SStephen Hurd 	if (mf != NULL) {
288620f63282SStephen Hurd 		ifp->if_input(ifp, mf);
28874c7070dbSScott Long 		DBG_COUNTER_INC(rx_if_input);
28884c7070dbSScott Long 	}
288923ac9029SStephen Hurd 
28904c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
28914c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
28924c7070dbSScott Long 
28934c7070dbSScott Long 	/*
28944c7070dbSScott Long 	 * Flush any outstanding LRO work
28954c7070dbSScott Long 	 */
2896aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
289723ac9029SStephen Hurd 	tcp_lro_flush_all(&rxq->ifr_lc);
2898aaeb188aSBjoern A. Zeeb #endif
2899fb1a29b4SHans Petter Selasky 	if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0)
2900fb1a29b4SHans Petter Selasky 		retval |= IFLIB_RXEOF_MORE;
2901fb1a29b4SHans Petter Selasky 	return (retval);
290295246abbSSean Bruno err:
29037b610b60SSean Bruno 	STATE_LOCK(ctx);
2904ab2e3f79SStephen Hurd 	ctx->ifc_flags |= IFC_DO_RESET;
2905940f62d6SEric Joyner 	iflib_admin_intr_deferred(ctx);
290646fa0c25SEric Joyner 	STATE_UNLOCK(ctx);
2907fb1a29b4SHans Petter Selasky 	return (0);
290895246abbSSean Bruno }
290995246abbSSean Bruno 
291095246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1)
291195246abbSSean Bruno static inline qidx_t
291295246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
291395246abbSSean Bruno {
291495246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
291595246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
291695246abbSSean Bruno 	if (in_use > 4*minthresh)
291795246abbSSean Bruno 		return (notify_count);
291895246abbSSean Bruno 	if (in_use > 2*minthresh)
291995246abbSSean Bruno 		return (notify_count >> 1);
292095246abbSSean Bruno 	if (in_use > minthresh)
292195246abbSSean Bruno 		return (notify_count >> 3);
292295246abbSSean Bruno 	return (0);
292395246abbSSean Bruno }
292495246abbSSean Bruno 
292595246abbSSean Bruno static inline qidx_t
292695246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq)
292795246abbSSean Bruno {
292895246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
292995246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
293095246abbSSean Bruno 	if (txq->ift_in_use > 4*minthresh)
293195246abbSSean Bruno 		return (notify_count);
293295246abbSSean Bruno 	if (txq->ift_in_use > 2*minthresh)
293395246abbSSean Bruno 		return (notify_count >> 1);
293495246abbSSean Bruno 	if (txq->ift_in_use > minthresh)
293595246abbSSean Bruno 		return (notify_count >> 2);
29362b2fc973SSean Bruno 	return (2);
29374c7070dbSScott Long }
29384c7070dbSScott Long 
29394c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
29404c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
294195246abbSSean Bruno 
294295246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
294395246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
294423ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
29454c7070dbSScott Long 
294695246abbSSean Bruno /* forward compatibility for cxgb */
294795246abbSSean Bruno #define FIRST_QSET(ctx) 0
294895246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
294995246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
295095246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
295195246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
295295246abbSSean Bruno 
295395246abbSSean Bruno /* XXX we should be setting this to something other than zero */
295495246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
29557474544bSMarius Strobl #define	MAX_TX_DESC(ctx) max((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \
29567474544bSMarius Strobl     (ctx)->ifc_softc_ctx.isc_tx_nsegments)
295795246abbSSean Bruno 
295895246abbSSean Bruno static inline bool
295995246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use)
29604c7070dbSScott Long {
296195246abbSSean Bruno 	qidx_t dbval, max;
296295246abbSSean Bruno 	bool rang;
29634c7070dbSScott Long 
296495246abbSSean Bruno 	rang = false;
296595246abbSSean Bruno 	max = TXQ_MAX_DB_DEFERRED(txq, in_use);
296695246abbSSean Bruno 	if (ring || txq->ift_db_pending >= max) {
29674c7070dbSScott Long 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
296895dcf343SMarius Strobl 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
296995dcf343SMarius Strobl 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
29704c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
29714c7070dbSScott Long 		txq->ift_db_pending = txq->ift_npending = 0;
297295246abbSSean Bruno 		rang = true;
29734c7070dbSScott Long 	}
297495246abbSSean Bruno 	return (rang);
29754c7070dbSScott Long }
29764c7070dbSScott Long 
29774c7070dbSScott Long #ifdef PKT_DEBUG
29784c7070dbSScott Long static void
29794c7070dbSScott Long print_pkt(if_pkt_info_t pi)
29804c7070dbSScott Long {
29814c7070dbSScott Long 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
29824c7070dbSScott Long 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
29834c7070dbSScott Long 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
29844c7070dbSScott Long 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
29854c7070dbSScott Long 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
29864c7070dbSScott Long 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
29874c7070dbSScott Long }
29884c7070dbSScott Long #endif
29894c7070dbSScott Long 
29904c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
2991a06424ddSEric Joyner #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO))
29924c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
2993a06424ddSEric Joyner #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO))
29944c7070dbSScott Long 
29954c7070dbSScott Long static int
29964c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
29974c7070dbSScott Long {
2998ab2e3f79SStephen Hurd 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
29994c7070dbSScott Long 	struct ether_vlan_header *eh;
3000c9a49a4fSMarius Strobl 	struct mbuf *m;
30014c7070dbSScott Long 
30028b8d9093SMarius Strobl 	m = *mp;
3003ab2e3f79SStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
3004ab2e3f79SStephen Hurd 	    M_WRITABLE(m) == 0) {
3005ab2e3f79SStephen Hurd 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
3006ab2e3f79SStephen Hurd 			return (ENOMEM);
3007ab2e3f79SStephen Hurd 		} else {
3008ab2e3f79SStephen Hurd 			m_freem(*mp);
300964e6fc13SStephen Hurd 			DBG_COUNTER_INC(tx_frees);
30108b8d9093SMarius Strobl 			*mp = m;
3011ab2e3f79SStephen Hurd 		}
3012ab2e3f79SStephen Hurd 	}
30131248952aSSean Bruno 
30144c7070dbSScott Long 	/*
30154c7070dbSScott Long 	 * Determine where frame payload starts.
30164c7070dbSScott Long 	 * Jump over vlan headers if already present,
30174c7070dbSScott Long 	 * helpful for QinQ too.
30184c7070dbSScott Long 	 */
30194c7070dbSScott Long 	if (__predict_false(m->m_len < sizeof(*eh))) {
30204c7070dbSScott Long 		txq->ift_pullups++;
30214c7070dbSScott Long 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
30224c7070dbSScott Long 			return (ENOMEM);
30234c7070dbSScott Long 	}
30244c7070dbSScott Long 	eh = mtod(m, struct ether_vlan_header *);
30254c7070dbSScott Long 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
30264c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_proto);
30274c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
30284c7070dbSScott Long 	} else {
30294c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
30304c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
30314c7070dbSScott Long 	}
30324c7070dbSScott Long 
30334c7070dbSScott Long 	switch (pi->ipi_etype) {
30344c7070dbSScott Long #ifdef INET
30354c7070dbSScott Long 	case ETHERTYPE_IP:
30364c7070dbSScott Long 	{
3037c9a49a4fSMarius Strobl 		struct mbuf *n;
30384c7070dbSScott Long 		struct ip *ip = NULL;
30394c7070dbSScott Long 		struct tcphdr *th = NULL;
30404c7070dbSScott Long 		int minthlen;
30414c7070dbSScott Long 
30424c7070dbSScott Long 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
30434c7070dbSScott Long 		if (__predict_false(m->m_len < minthlen)) {
30444c7070dbSScott Long 			/*
30454c7070dbSScott Long 			 * if this code bloat is causing too much of a hit
30464c7070dbSScott Long 			 * move it to a separate function and mark it noinline
30474c7070dbSScott Long 			 */
30484c7070dbSScott Long 			if (m->m_len == pi->ipi_ehdrlen) {
30494c7070dbSScott Long 				n = m->m_next;
30504c7070dbSScott Long 				MPASS(n);
30514c7070dbSScott Long 				if (n->m_len >= sizeof(*ip))  {
30524c7070dbSScott Long 					ip = (struct ip *)n->m_data;
30534c7070dbSScott Long 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
30544c7070dbSScott Long 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
30554c7070dbSScott Long 				} else {
30564c7070dbSScott Long 					txq->ift_pullups++;
30574c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
30584c7070dbSScott Long 						return (ENOMEM);
30594c7070dbSScott Long 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
30604c7070dbSScott Long 				}
30614c7070dbSScott Long 			} else {
30624c7070dbSScott Long 				txq->ift_pullups++;
30634c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
30644c7070dbSScott Long 					return (ENOMEM);
30654c7070dbSScott Long 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
30664c7070dbSScott Long 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
30674c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
30684c7070dbSScott Long 			}
30694c7070dbSScott Long 		} else {
30704c7070dbSScott Long 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
30714c7070dbSScott Long 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
30724c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
30734c7070dbSScott Long 		}
30744c7070dbSScott Long 		pi->ipi_ip_hlen = ip->ip_hl << 2;
30754c7070dbSScott Long 		pi->ipi_ipproto = ip->ip_p;
30764c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV4;
30774c7070dbSScott Long 
3078a06424ddSEric Joyner 		/* TCP checksum offload may require TCP header length */
3079a06424ddSEric Joyner 		if (IS_TX_OFFLOAD4(pi)) {
3080a06424ddSEric Joyner 			if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) {
30814c7070dbSScott Long 				if (__predict_false(th == NULL)) {
30824c7070dbSScott Long 					txq->ift_pullups++;
30834c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
30844c7070dbSScott Long 						return (ENOMEM);
30854c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
30864c7070dbSScott Long 				}
30874c7070dbSScott Long 				pi->ipi_tcp_hflags = th->th_flags;
30884c7070dbSScott Long 				pi->ipi_tcp_hlen = th->th_off << 2;
30894c7070dbSScott Long 				pi->ipi_tcp_seq = th->th_seq;
30904c7070dbSScott Long 			}
3091a06424ddSEric Joyner 			if (IS_TSO4(pi)) {
30924c7070dbSScott Long 				if (__predict_false(ip->ip_p != IPPROTO_TCP))
30934c7070dbSScott Long 					return (ENXIO);
30948d4ceb9cSStephen Hurd 				/*
30958d4ceb9cSStephen Hurd 				 * TSO always requires hardware checksum offload.
30968d4ceb9cSStephen Hurd 				 */
30978d4ceb9cSStephen Hurd 				pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP);
30984c7070dbSScott Long 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
30994c7070dbSScott Long 						       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
31004c7070dbSScott Long 				pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
31011248952aSSean Bruno 				if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
31021248952aSSean Bruno 					ip->ip_sum = 0;
31031248952aSSean Bruno 					ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
31041248952aSSean Bruno 				}
31054c7070dbSScott Long 			}
3106a06424ddSEric Joyner 		}
31078d4ceb9cSStephen Hurd 		if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP))
31088d4ceb9cSStephen Hurd                        ip->ip_sum = 0;
31098d4ceb9cSStephen Hurd 
31104c7070dbSScott Long 		break;
31114c7070dbSScott Long 	}
31124c7070dbSScott Long #endif
31134c7070dbSScott Long #ifdef INET6
31144c7070dbSScott Long 	case ETHERTYPE_IPV6:
31154c7070dbSScott Long 	{
31164c7070dbSScott Long 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
31174c7070dbSScott Long 		struct tcphdr *th;
31184c7070dbSScott Long 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
31194c7070dbSScott Long 
31204c7070dbSScott Long 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
312164e6fc13SStephen Hurd 			txq->ift_pullups++;
31224c7070dbSScott Long 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
31234c7070dbSScott Long 				return (ENOMEM);
31244c7070dbSScott Long 		}
31254c7070dbSScott Long 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
31264c7070dbSScott Long 
31274c7070dbSScott Long 		/* XXX-BZ this will go badly in case of ext hdrs. */
31284c7070dbSScott Long 		pi->ipi_ipproto = ip6->ip6_nxt;
31294c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV6;
31304c7070dbSScott Long 
3131a06424ddSEric Joyner 		/* TCP checksum offload may require TCP header length */
3132a06424ddSEric Joyner 		if (IS_TX_OFFLOAD6(pi)) {
31334c7070dbSScott Long 			if (pi->ipi_ipproto == IPPROTO_TCP) {
31344c7070dbSScott Long 				if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
3135a06424ddSEric Joyner 					txq->ift_pullups++;
31364c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
31374c7070dbSScott Long 						return (ENOMEM);
31384c7070dbSScott Long 				}
31394c7070dbSScott Long 				pi->ipi_tcp_hflags = th->th_flags;
31404c7070dbSScott Long 				pi->ipi_tcp_hlen = th->th_off << 2;
3141a06424ddSEric Joyner 				pi->ipi_tcp_seq = th->th_seq;
31424c7070dbSScott Long 			}
3143a06424ddSEric Joyner 			if (IS_TSO6(pi)) {
31444c7070dbSScott Long 				if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
31454c7070dbSScott Long 					return (ENXIO);
31464c7070dbSScott Long 				/*
31478d4ceb9cSStephen Hurd 				 * TSO always requires hardware checksum offload.
31484c7070dbSScott Long 				 */
3149a06424ddSEric Joyner 				pi->ipi_csum_flags |= CSUM_IP6_TCP;
31504c7070dbSScott Long 				th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
31514c7070dbSScott Long 				pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
31524c7070dbSScott Long 			}
3153a06424ddSEric Joyner 		}
31544c7070dbSScott Long 		break;
31554c7070dbSScott Long 	}
31564c7070dbSScott Long #endif
31574c7070dbSScott Long 	default:
31584c7070dbSScott Long 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
31594c7070dbSScott Long 		pi->ipi_ip_hlen = 0;
31604c7070dbSScott Long 		break;
31614c7070dbSScott Long 	}
31624c7070dbSScott Long 	*mp = m;
31631248952aSSean Bruno 
31644c7070dbSScott Long 	return (0);
31654c7070dbSScott Long }
31664c7070dbSScott Long 
31674c7070dbSScott Long /*
31684c7070dbSScott Long  * If dodgy hardware rejects the scatter gather chain we've handed it
316923ac9029SStephen Hurd  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
317023ac9029SStephen Hurd  * m_defrag'd mbufs
31714c7070dbSScott Long  */
31724c7070dbSScott Long static __noinline struct mbuf *
317323ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq)
31744c7070dbSScott Long {
3175fbec776dSAndrew Gallatin 	int ntxd, pidx;
3176fbec776dSAndrew Gallatin 	struct mbuf *m, **ifsd_m;
31774c7070dbSScott Long 
31784c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
317923ac9029SStephen Hurd 	ntxd = txq->ift_size;
3180fbec776dSAndrew Gallatin 	pidx = txq->ift_pidx & (ntxd - 1);
3181fbec776dSAndrew Gallatin 	ifsd_m = txq->ift_sds.ifsd_m;
3182fbec776dSAndrew Gallatin 	m = ifsd_m[pidx];
31834c7070dbSScott Long 	ifsd_m[pidx] = NULL;
3184bfce461eSMarius Strobl 	bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]);
31858a04b53dSKonstantin Belousov 	if (txq->ift_sds.ifsd_tso_map != NULL)
3186bfce461eSMarius Strobl 		bus_dmamap_unload(txq->ift_tso_buf_tag,
31878a04b53dSKonstantin Belousov 		    txq->ift_sds.ifsd_tso_map[pidx]);
31884c7070dbSScott Long #if MEMORY_LOGGING
31894c7070dbSScott Long 	txq->ift_dequeued++;
31904c7070dbSScott Long #endif
3191fbec776dSAndrew Gallatin 	return (m);
31924c7070dbSScott Long }
31934c7070dbSScott Long 
319495246abbSSean Bruno static inline caddr_t
319595246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid)
319695246abbSSean Bruno {
319795246abbSSean Bruno 	qidx_t size;
319895246abbSSean Bruno 	int ntxd;
319995246abbSSean Bruno 	caddr_t start, end, cur, next;
320095246abbSSean Bruno 
320195246abbSSean Bruno 	ntxd = txq->ift_size;
320295246abbSSean Bruno 	size = txq->ift_txd_size[qid];
320395246abbSSean Bruno 	start = txq->ift_ifdi[qid].idi_vaddr;
320495246abbSSean Bruno 
320595246abbSSean Bruno 	if (__predict_false(size == 0))
320695246abbSSean Bruno 		return (start);
320795246abbSSean Bruno 	cur = start + size*cidx;
320895246abbSSean Bruno 	end = start + size*ntxd;
320995246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
321095246abbSSean Bruno 	return (next < end ? next : start);
321195246abbSSean Bruno }
321295246abbSSean Bruno 
3213d14c853bSStephen Hurd /*
3214d14c853bSStephen Hurd  * Pad an mbuf to ensure a minimum ethernet frame size.
3215d14c853bSStephen Hurd  * min_frame_size is the frame size (less CRC) to pad the mbuf to
3216d14c853bSStephen Hurd  */
3217d14c853bSStephen Hurd static __noinline int
3218a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size)
3219d14c853bSStephen Hurd {
3220d14c853bSStephen Hurd 	/*
3221d14c853bSStephen Hurd 	 * 18 is enough bytes to pad an ARP packet to 46 bytes, and
3222d14c853bSStephen Hurd 	 * and ARP message is the smallest common payload I can think of
3223d14c853bSStephen Hurd 	 */
3224d14c853bSStephen Hurd 	static char pad[18];	/* just zeros */
3225d14c853bSStephen Hurd 	int n;
3226a15fbbb8SStephen Hurd 	struct mbuf *new_head;
3227d14c853bSStephen Hurd 
3228a15fbbb8SStephen Hurd 	if (!M_WRITABLE(*m_head)) {
3229a15fbbb8SStephen Hurd 		new_head = m_dup(*m_head, M_NOWAIT);
3230a15fbbb8SStephen Hurd 		if (new_head == NULL) {
323104993890SStephen Hurd 			m_freem(*m_head);
3232a15fbbb8SStephen Hurd 			device_printf(dev, "cannot pad short frame, m_dup() failed");
323306c47d48SStephen Hurd 			DBG_COUNTER_INC(encap_pad_mbuf_fail);
323464e6fc13SStephen Hurd 			DBG_COUNTER_INC(tx_frees);
3235a15fbbb8SStephen Hurd 			return ENOMEM;
3236a15fbbb8SStephen Hurd 		}
3237a15fbbb8SStephen Hurd 		m_freem(*m_head);
3238a15fbbb8SStephen Hurd 		*m_head = new_head;
3239a15fbbb8SStephen Hurd 	}
3240a15fbbb8SStephen Hurd 
3241a15fbbb8SStephen Hurd 	for (n = min_frame_size - (*m_head)->m_pkthdr.len;
3242d14c853bSStephen Hurd 	     n > 0; n -= sizeof(pad))
3243a15fbbb8SStephen Hurd 		if (!m_append(*m_head, min(n, sizeof(pad)), pad))
3244d14c853bSStephen Hurd 			break;
3245d14c853bSStephen Hurd 
3246d14c853bSStephen Hurd 	if (n > 0) {
3247a15fbbb8SStephen Hurd 		m_freem(*m_head);
3248d14c853bSStephen Hurd 		device_printf(dev, "cannot pad short frame\n");
3249d14c853bSStephen Hurd 		DBG_COUNTER_INC(encap_pad_mbuf_fail);
325064e6fc13SStephen Hurd 		DBG_COUNTER_INC(tx_frees);
3251d14c853bSStephen Hurd 		return (ENOBUFS);
3252d14c853bSStephen Hurd 	}
3253d14c853bSStephen Hurd 
3254d14c853bSStephen Hurd 	return 0;
3255d14c853bSStephen Hurd }
3256d14c853bSStephen Hurd 
32574c7070dbSScott Long static int
32584c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
32594c7070dbSScott Long {
32604c7070dbSScott Long 	if_ctx_t		ctx;
32614c7070dbSScott Long 	if_shared_ctx_t		sctx;
32624c7070dbSScott Long 	if_softc_ctx_t		scctx;
3263bfce461eSMarius Strobl 	bus_dma_tag_t		buf_tag;
32644c7070dbSScott Long 	bus_dma_segment_t	*segs;
3265fbec776dSAndrew Gallatin 	struct mbuf		*m_head, **ifsd_m;
326695246abbSSean Bruno 	void			*next_txd;
32674c7070dbSScott Long 	bus_dmamap_t		map;
32684c7070dbSScott Long 	struct if_pkt_info	pi;
32694c7070dbSScott Long 	int remap = 0;
32704c7070dbSScott Long 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
32714c7070dbSScott Long 
32724c7070dbSScott Long 	ctx = txq->ift_ctx;
32734c7070dbSScott Long 	sctx = ctx->ifc_sctx;
32744c7070dbSScott Long 	scctx = &ctx->ifc_softc_ctx;
32754c7070dbSScott Long 	segs = txq->ift_segs;
327623ac9029SStephen Hurd 	ntxd = txq->ift_size;
32774c7070dbSScott Long 	m_head = *m_headp;
32784c7070dbSScott Long 	map = NULL;
32794c7070dbSScott Long 
32804c7070dbSScott Long 	/*
32814c7070dbSScott Long 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
32824c7070dbSScott Long 	 */
32834c7070dbSScott Long 	cidx = txq->ift_cidx;
32844c7070dbSScott Long 	pidx = txq->ift_pidx;
328595246abbSSean Bruno 	if (ctx->ifc_flags & IFC_PREFETCH) {
32864c7070dbSScott Long 		next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
328795246abbSSean Bruno 		if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) {
328895246abbSSean Bruno 			next_txd = calc_next_txd(txq, cidx, 0);
328995246abbSSean Bruno 			prefetch(next_txd);
329095246abbSSean Bruno 		}
32914c7070dbSScott Long 
32924c7070dbSScott Long 		/* prefetch the next cache line of mbuf pointers and flags */
32934c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_m[next]);
32944c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_map[next]);
32954c7070dbSScott Long 		next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
32964c7070dbSScott Long 	}
329795246abbSSean Bruno 	map = txq->ift_sds.ifsd_map[pidx];
3298fbec776dSAndrew Gallatin 	ifsd_m = txq->ift_sds.ifsd_m;
32994c7070dbSScott Long 
33004c7070dbSScott Long 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
3301bfce461eSMarius Strobl 		buf_tag = txq->ift_tso_buf_tag;
33024c7070dbSScott Long 		max_segs = scctx->isc_tx_tso_segments_max;
33038a04b53dSKonstantin Belousov 		map = txq->ift_sds.ifsd_tso_map[pidx];
3304bfce461eSMarius Strobl 		MPASS(buf_tag != NULL);
33057f87c040SMarius Strobl 		MPASS(max_segs > 0);
33064c7070dbSScott Long 	} else {
3307bfce461eSMarius Strobl 		buf_tag = txq->ift_buf_tag;
33084c7070dbSScott Long 		max_segs = scctx->isc_tx_nsegments;
33098a04b53dSKonstantin Belousov 		map = txq->ift_sds.ifsd_map[pidx];
33104c7070dbSScott Long 	}
3311d14c853bSStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) &&
3312d14c853bSStephen Hurd 	    __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) {
3313a15fbbb8SStephen Hurd 		err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size);
331464e6fc13SStephen Hurd 		if (err) {
331564e6fc13SStephen Hurd 			DBG_COUNTER_INC(encap_txd_encap_fail);
3316d14c853bSStephen Hurd 			return err;
3317d14c853bSStephen Hurd 		}
331864e6fc13SStephen Hurd 	}
3319a15fbbb8SStephen Hurd 	m_head = *m_headp;
332095246abbSSean Bruno 
332195246abbSSean Bruno 	pkt_info_zero(&pi);
3322ab2e3f79SStephen Hurd 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
3323ab2e3f79SStephen Hurd 	pi.ipi_pidx = pidx;
3324ab2e3f79SStephen Hurd 	pi.ipi_qsidx = txq->ift_id;
33253429c02fSStephen Hurd 	pi.ipi_len = m_head->m_pkthdr.len;
33263429c02fSStephen Hurd 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
33271722eeacSMarius Strobl 	pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0;
33284c7070dbSScott Long 
33294c7070dbSScott Long 	/* deliberate bitwise OR to make one condition */
33304c7070dbSScott Long 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
333164e6fc13SStephen Hurd 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) {
333264e6fc13SStephen Hurd 			DBG_COUNTER_INC(encap_txd_encap_fail);
33334c7070dbSScott Long 			return (err);
333464e6fc13SStephen Hurd 		}
33354c7070dbSScott Long 		m_head = *m_headp;
33364c7070dbSScott Long 	}
33374c7070dbSScott Long 
33384c7070dbSScott Long retry:
3339bfce461eSMarius Strobl 	err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs,
3340fbec776dSAndrew Gallatin 	    BUS_DMA_NOWAIT);
33414c7070dbSScott Long defrag:
33424c7070dbSScott Long 	if (__predict_false(err)) {
33434c7070dbSScott Long 		switch (err) {
33444c7070dbSScott Long 		case EFBIG:
33454c7070dbSScott Long 			/* try collapse once and defrag once */
3346f7594707SAndrew Gallatin 			if (remap == 0) {
33474c7070dbSScott Long 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
3348f7594707SAndrew Gallatin 				/* try defrag if collapsing fails */
3349f7594707SAndrew Gallatin 				if (m_head == NULL)
3350f7594707SAndrew Gallatin 					remap++;
3351f7594707SAndrew Gallatin 			}
335264e6fc13SStephen Hurd 			if (remap == 1) {
335364e6fc13SStephen Hurd 				txq->ift_mbuf_defrag++;
33544c7070dbSScott Long 				m_head = m_defrag(*m_headp, M_NOWAIT);
335564e6fc13SStephen Hurd 			}
33563e8d1baeSEric Joyner 			/*
33573e8d1baeSEric Joyner 			 * remap should never be >1 unless bus_dmamap_load_mbuf_sg
33583e8d1baeSEric Joyner 			 * failed to map an mbuf that was run through m_defrag
33593e8d1baeSEric Joyner 			 */
33603e8d1baeSEric Joyner 			MPASS(remap <= 1);
33613e8d1baeSEric Joyner 			if (__predict_false(m_head == NULL || remap > 1))
33624c7070dbSScott Long 				goto defrag_failed;
33633e8d1baeSEric Joyner 			remap++;
33644c7070dbSScott Long 			*m_headp = m_head;
33654c7070dbSScott Long 			goto retry;
33664c7070dbSScott Long 			break;
33674c7070dbSScott Long 		case ENOMEM:
33684c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
33694c7070dbSScott Long 			break;
33704c7070dbSScott Long 		default:
33714c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
33724c7070dbSScott Long 			m_freem(*m_headp);
33734c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
33744c7070dbSScott Long 			*m_headp = NULL;
33754c7070dbSScott Long 			break;
33764c7070dbSScott Long 		}
33774c7070dbSScott Long 		txq->ift_map_failed++;
33784c7070dbSScott Long 		DBG_COUNTER_INC(encap_load_mbuf_fail);
337964e6fc13SStephen Hurd 		DBG_COUNTER_INC(encap_txd_encap_fail);
33804c7070dbSScott Long 		return (err);
33814c7070dbSScott Long 	}
3382fbec776dSAndrew Gallatin 	ifsd_m[pidx] = m_head;
33834c7070dbSScott Long 	/*
33844c7070dbSScott Long 	 * XXX assumes a 1 to 1 relationship between segments and
33854c7070dbSScott Long 	 *        descriptors - this does not hold true on all drivers, e.g.
33864c7070dbSScott Long 	 *        cxgb
33874c7070dbSScott Long 	 */
33884c7070dbSScott Long 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
33894c7070dbSScott Long 		txq->ift_no_desc_avail++;
3390bfce461eSMarius Strobl 		bus_dmamap_unload(buf_tag, map);
33914c7070dbSScott Long 		DBG_COUNTER_INC(encap_txq_avail_fail);
339264e6fc13SStephen Hurd 		DBG_COUNTER_INC(encap_txd_encap_fail);
339323ac9029SStephen Hurd 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
33944c7070dbSScott Long 			GROUPTASK_ENQUEUE(&txq->ift_task);
33954c7070dbSScott Long 		return (ENOBUFS);
33964c7070dbSScott Long 	}
339795246abbSSean Bruno 	/*
339895246abbSSean Bruno 	 * On Intel cards we can greatly reduce the number of TX interrupts
339995246abbSSean Bruno 	 * we see by only setting report status on every Nth descriptor.
340095246abbSSean Bruno 	 * However, this also means that the driver will need to keep track
340195246abbSSean Bruno 	 * of the descriptors that RS was set on to check them for the DD bit.
340295246abbSSean Bruno 	 */
340395246abbSSean Bruno 	txq->ift_rs_pending += nsegs + 1;
340495246abbSSean Bruno 	if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) ||
34051f7ce05dSAndrew Gallatin 	     iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) {
340695246abbSSean Bruno 		pi.ipi_flags |= IPI_TX_INTR;
340795246abbSSean Bruno 		txq->ift_rs_pending = 0;
340895246abbSSean Bruno 	}
340995246abbSSean Bruno 
34104c7070dbSScott Long 	pi.ipi_segs = segs;
34114c7070dbSScott Long 	pi.ipi_nsegs = nsegs;
34124c7070dbSScott Long 
341323ac9029SStephen Hurd 	MPASS(pidx >= 0 && pidx < txq->ift_size);
34144c7070dbSScott Long #ifdef PKT_DEBUG
34154c7070dbSScott Long 	print_pkt(&pi);
34164c7070dbSScott Long #endif
34174c7070dbSScott Long 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
341895dcf343SMarius Strobl 		bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE);
34194c7070dbSScott Long 		DBG_COUNTER_INC(tx_encap);
342095246abbSSean Bruno 		MPASS(pi.ipi_new_pidx < txq->ift_size);
34214c7070dbSScott Long 
34224c7070dbSScott Long 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
34234c7070dbSScott Long 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
342423ac9029SStephen Hurd 			ndesc += txq->ift_size;
34254c7070dbSScott Long 			txq->ift_gen = 1;
34264c7070dbSScott Long 		}
34271248952aSSean Bruno 		/*
34281248952aSSean Bruno 		 * drivers can need as many as
34291248952aSSean Bruno 		 * two sentinels
34301248952aSSean Bruno 		 */
34311248952aSSean Bruno 		MPASS(ndesc <= pi.ipi_nsegs + 2);
34324c7070dbSScott Long 		MPASS(pi.ipi_new_pidx != pidx);
34334c7070dbSScott Long 		MPASS(ndesc > 0);
34344c7070dbSScott Long 		txq->ift_in_use += ndesc;
343595246abbSSean Bruno 
34364c7070dbSScott Long 		/*
34374c7070dbSScott Long 		 * We update the last software descriptor again here because there may
34384c7070dbSScott Long 		 * be a sentinel and/or there may be more mbufs than segments
34394c7070dbSScott Long 		 */
34404c7070dbSScott Long 		txq->ift_pidx = pi.ipi_new_pidx;
34414c7070dbSScott Long 		txq->ift_npending += pi.ipi_ndescs;
3442f7594707SAndrew Gallatin 	} else {
344323ac9029SStephen Hurd 		*m_headp = m_head = iflib_remove_mbuf(txq);
3444f7594707SAndrew Gallatin 		if (err == EFBIG) {
34454c7070dbSScott Long 			txq->ift_txd_encap_efbig++;
3446f7594707SAndrew Gallatin 			if (remap < 2) {
3447f7594707SAndrew Gallatin 				remap = 1;
34484c7070dbSScott Long 				goto defrag;
3449f7594707SAndrew Gallatin 			}
3450f7594707SAndrew Gallatin 		}
3451f7594707SAndrew Gallatin 		goto defrag_failed;
3452f7594707SAndrew Gallatin 	}
345364e6fc13SStephen Hurd 	/*
345464e6fc13SStephen Hurd 	 * err can't possibly be non-zero here, so we don't neet to test it
345564e6fc13SStephen Hurd 	 * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail).
345664e6fc13SStephen Hurd 	 */
34574c7070dbSScott Long 	return (err);
34584c7070dbSScott Long 
34594c7070dbSScott Long defrag_failed:
34604c7070dbSScott Long 	txq->ift_mbuf_defrag_failed++;
34614c7070dbSScott Long 	txq->ift_map_failed++;
34624c7070dbSScott Long 	m_freem(*m_headp);
34634c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
34644c7070dbSScott Long 	*m_headp = NULL;
346564e6fc13SStephen Hurd 	DBG_COUNTER_INC(encap_txd_encap_fail);
34664c7070dbSScott Long 	return (ENOMEM);
34674c7070dbSScott Long }
34684c7070dbSScott Long 
34694c7070dbSScott Long static void
34704c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n)
34714c7070dbSScott Long {
34724c7070dbSScott Long 	uint32_t qsize, cidx, mask, gen;
34734c7070dbSScott Long 	struct mbuf *m, **ifsd_m;
347495246abbSSean Bruno 	bool do_prefetch;
34754c7070dbSScott Long 
34764c7070dbSScott Long 	cidx = txq->ift_cidx;
34774c7070dbSScott Long 	gen = txq->ift_gen;
347823ac9029SStephen Hurd 	qsize = txq->ift_size;
34794c7070dbSScott Long 	mask = qsize-1;
34804c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
348195246abbSSean Bruno 	do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH);
34824c7070dbSScott Long 
348394618825SMark Johnston 	while (n-- > 0) {
348495246abbSSean Bruno 		if (do_prefetch) {
34854c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 3) & mask]);
34864c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 4) & mask]);
348795246abbSSean Bruno 		}
34884c7070dbSScott Long 		if ((m = ifsd_m[cidx]) != NULL) {
3489fbec776dSAndrew Gallatin 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
34908a04b53dSKonstantin Belousov 			if (m->m_pkthdr.csum_flags & CSUM_TSO) {
3491bfce461eSMarius Strobl 				bus_dmamap_sync(txq->ift_tso_buf_tag,
34928a04b53dSKonstantin Belousov 				    txq->ift_sds.ifsd_tso_map[cidx],
34938a04b53dSKonstantin Belousov 				    BUS_DMASYNC_POSTWRITE);
3494bfce461eSMarius Strobl 				bus_dmamap_unload(txq->ift_tso_buf_tag,
34958a04b53dSKonstantin Belousov 				    txq->ift_sds.ifsd_tso_map[cidx]);
34968a04b53dSKonstantin Belousov 			} else {
3497bfce461eSMarius Strobl 				bus_dmamap_sync(txq->ift_buf_tag,
34988a04b53dSKonstantin Belousov 				    txq->ift_sds.ifsd_map[cidx],
34998a04b53dSKonstantin Belousov 				    BUS_DMASYNC_POSTWRITE);
3500bfce461eSMarius Strobl 				bus_dmamap_unload(txq->ift_buf_tag,
35018a04b53dSKonstantin Belousov 				    txq->ift_sds.ifsd_map[cidx]);
35028a04b53dSKonstantin Belousov 			}
35034c7070dbSScott Long 			/* XXX we don't support any drivers that batch packets yet */
35044c7070dbSScott Long 			MPASS(m->m_nextpkt == NULL);
35055c5ca36cSSean Bruno 			m_freem(m);
35064c7070dbSScott Long 			ifsd_m[cidx] = NULL;
35074c7070dbSScott Long #if MEMORY_LOGGING
35084c7070dbSScott Long 			txq->ift_dequeued++;
35094c7070dbSScott Long #endif
35104c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
35114c7070dbSScott Long 		}
35124c7070dbSScott Long 		if (__predict_false(++cidx == qsize)) {
35134c7070dbSScott Long 			cidx = 0;
35144c7070dbSScott Long 			gen = 0;
35154c7070dbSScott Long 		}
35164c7070dbSScott Long 	}
35174c7070dbSScott Long 	txq->ift_cidx = cidx;
35184c7070dbSScott Long 	txq->ift_gen = gen;
35194c7070dbSScott Long }
35204c7070dbSScott Long 
35214c7070dbSScott Long static __inline int
35224c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
35234c7070dbSScott Long {
35244c7070dbSScott Long 	int reclaim;
35254c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
35264c7070dbSScott Long 
35274c7070dbSScott Long 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
35284c7070dbSScott Long 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
35294c7070dbSScott Long 
35304c7070dbSScott Long 	/*
35314c7070dbSScott Long 	 * Need a rate-limiting check so that this isn't called every time
35324c7070dbSScott Long 	 */
35334c7070dbSScott Long 	iflib_tx_credits_update(ctx, txq);
35344c7070dbSScott Long 	reclaim = DESC_RECLAIMABLE(txq);
35354c7070dbSScott Long 
35364c7070dbSScott Long 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
35374c7070dbSScott Long #ifdef INVARIANTS
35384c7070dbSScott Long 		if (iflib_verbose_debug) {
35394c7070dbSScott Long 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
35404c7070dbSScott Long 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
35414c7070dbSScott Long 			       reclaim, thresh);
35424c7070dbSScott Long 
35434c7070dbSScott Long 		}
35444c7070dbSScott Long #endif
35454c7070dbSScott Long 		return (0);
35464c7070dbSScott Long 	}
35474c7070dbSScott Long 	iflib_tx_desc_free(txq, reclaim);
35484c7070dbSScott Long 	txq->ift_cleaned += reclaim;
35494c7070dbSScott Long 	txq->ift_in_use -= reclaim;
35504c7070dbSScott Long 
35514c7070dbSScott Long 	return (reclaim);
35524c7070dbSScott Long }
35534c7070dbSScott Long 
35544c7070dbSScott Long static struct mbuf **
355595246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
35564c7070dbSScott Long {
355795246abbSSean Bruno 	int next, size;
355895246abbSSean Bruno 	struct mbuf **items;
35594c7070dbSScott Long 
356095246abbSSean Bruno 	size = r->size;
356195246abbSSean Bruno 	next = (cidx + CACHE_PTR_INCREMENT) & (size-1);
356295246abbSSean Bruno 	items = __DEVOLATILE(struct mbuf **, &r->items[0]);
356395246abbSSean Bruno 
356495246abbSSean Bruno 	prefetch(items[(cidx + offset) & (size-1)]);
356595246abbSSean Bruno 	if (remaining > 1) {
35663429c02fSStephen Hurd 		prefetch2cachelines(&items[next]);
35673429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]);
35683429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]);
35693429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]);
357095246abbSSean Bruno 	}
357195246abbSSean Bruno 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)]));
35724c7070dbSScott Long }
35734c7070dbSScott Long 
35744c7070dbSScott Long static void
35754c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget)
35764c7070dbSScott Long {
35774c7070dbSScott Long 
357895246abbSSean Bruno 	ifmp_ring_check_drainage(txq->ift_br, budget);
35794c7070dbSScott Long }
35804c7070dbSScott Long 
35814c7070dbSScott Long static uint32_t
35824c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r)
35834c7070dbSScott Long {
35844c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
35854c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
35864c7070dbSScott Long 
358795dcf343SMarius Strobl 	if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2)
358895dcf343SMarius Strobl 		return (1);
35898a04b53dSKonstantin Belousov 	bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
35908a04b53dSKonstantin Belousov 	    BUS_DMASYNC_POSTREAD);
359195dcf343SMarius Strobl 	return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id,
359295dcf343SMarius Strobl 	    false));
35934c7070dbSScott Long }
35944c7070dbSScott Long 
35954c7070dbSScott Long static uint32_t
35964c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
35974c7070dbSScott Long {
35984c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
35994c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
36001722eeacSMarius Strobl 	if_t ifp = ctx->ifc_ifp;
3601c2c5d1e7SMarius Strobl 	struct mbuf *m, **mp;
3602c2c5d1e7SMarius Strobl 	int avail, bytes_sent, consumed, count, err, i, in_use_prev;
3603c2c5d1e7SMarius Strobl 	int mcast_sent, pkt_sent, reclaimed, txq_avail;
3604c2c5d1e7SMarius Strobl 	bool do_prefetch, rang, ring;
36054c7070dbSScott Long 
36064c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
36074c7070dbSScott Long 			    !LINK_ACTIVE(ctx))) {
36084c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_notready);
36094c7070dbSScott Long 		return (0);
36104c7070dbSScott Long 	}
361195246abbSSean Bruno 	reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
361295246abbSSean Bruno 	rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use);
36134c7070dbSScott Long 	avail = IDXDIFF(pidx, cidx, r->size);
36144c7070dbSScott Long 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
36154c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_flushing);
36164c7070dbSScott Long 		for (i = 0; i < avail; i++) {
3617bc0e855bSStephen Hurd 			if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq))
361823ac9029SStephen Hurd 				m_free(r->items[(cidx + i) & (r->size-1)]);
36194c7070dbSScott Long 			r->items[(cidx + i) & (r->size-1)] = NULL;
36204c7070dbSScott Long 		}
36214c7070dbSScott Long 		return (avail);
36224c7070dbSScott Long 	}
362395246abbSSean Bruno 
36244c7070dbSScott Long 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
36254c7070dbSScott Long 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
36264c7070dbSScott Long 		CALLOUT_LOCK(txq);
36274c7070dbSScott Long 		callout_stop(&txq->ift_timer);
36284c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
36294c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_oactive);
36304c7070dbSScott Long 		return (0);
36314c7070dbSScott Long 	}
363295246abbSSean Bruno 	if (reclaimed)
363395246abbSSean Bruno 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
36344c7070dbSScott Long 	consumed = mcast_sent = bytes_sent = pkt_sent = 0;
36354c7070dbSScott Long 	count = MIN(avail, TX_BATCH_SIZE);
3636da69b8f9SSean Bruno #ifdef INVARIANTS
3637da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3638da69b8f9SSean Bruno 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3639da69b8f9SSean Bruno 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3640da69b8f9SSean Bruno #endif
364195246abbSSean Bruno 	do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
3642c2c5d1e7SMarius Strobl 	txq_avail = TXQ_AVAIL(txq);
36431ae4848cSMatt Macy 	err = 0;
3644c2c5d1e7SMarius Strobl 	for (i = 0; i < count && txq_avail > MAX_TX_DESC(ctx) + 2; i++) {
36451ae4848cSMatt Macy 		int rem = do_prefetch ? count - i : 0;
36464c7070dbSScott Long 
364795246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, rem);
3648da69b8f9SSean Bruno 		MPASS(mp != NULL && *mp != NULL);
364995246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq)) {
365095246abbSSean Bruno 			consumed++;
365195246abbSSean Bruno 			continue;
365295246abbSSean Bruno 		}
36534c7070dbSScott Long 		in_use_prev = txq->ift_in_use;
365495246abbSSean Bruno 		err = iflib_encap(txq, mp);
365595246abbSSean Bruno 		if (__predict_false(err)) {
3656da69b8f9SSean Bruno 			/* no room - bail out */
365795246abbSSean Bruno 			if (err == ENOBUFS)
36584c7070dbSScott Long 				break;
36594c7070dbSScott Long 			consumed++;
3660da69b8f9SSean Bruno 			/* we can't send this packet - skip it */
36614c7070dbSScott Long 			continue;
3662da69b8f9SSean Bruno 		}
366395246abbSSean Bruno 		consumed++;
36644c7070dbSScott Long 		pkt_sent++;
36654c7070dbSScott Long 		m = *mp;
36664c7070dbSScott Long 		DBG_COUNTER_INC(tx_sent);
36674c7070dbSScott Long 		bytes_sent += m->m_pkthdr.len;
366895246abbSSean Bruno 		mcast_sent += !!(m->m_flags & M_MCAST);
3669c2c5d1e7SMarius Strobl 		txq_avail = TXQ_AVAIL(txq);
36704c7070dbSScott Long 
36714c7070dbSScott Long 		txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
36724c7070dbSScott Long 		ETHER_BPF_MTAP(ifp, m);
367395246abbSSean Bruno 		if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
36744c7070dbSScott Long 			break;
367595246abbSSean Bruno 		rang = iflib_txd_db_check(ctx, txq, false, in_use_prev);
36764c7070dbSScott Long 	}
36774c7070dbSScott Long 
367895246abbSSean Bruno 	/* deliberate use of bitwise or to avoid gratuitous short-circuit */
367995246abbSSean Bruno 	ring = rang ? false  : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx));
368095246abbSSean Bruno 	iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use);
36814c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
36824c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
36834c7070dbSScott Long 	if (mcast_sent)
36844c7070dbSScott Long 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3685da69b8f9SSean Bruno #ifdef INVARIANTS
3686da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3687da69b8f9SSean Bruno 		printf("consumed=%d\n", consumed);
3688da69b8f9SSean Bruno #endif
36894c7070dbSScott Long 	return (consumed);
36904c7070dbSScott Long }
36914c7070dbSScott Long 
3692da69b8f9SSean Bruno static uint32_t
3693da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r)
3694da69b8f9SSean Bruno {
3695da69b8f9SSean Bruno 	return (1);
3696da69b8f9SSean Bruno }
3697da69b8f9SSean Bruno 
3698da69b8f9SSean Bruno static uint32_t
3699da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3700da69b8f9SSean Bruno {
3701da69b8f9SSean Bruno 	int i, avail;
3702da69b8f9SSean Bruno 	struct mbuf **mp;
3703da69b8f9SSean Bruno 	iflib_txq_t txq;
3704da69b8f9SSean Bruno 
3705da69b8f9SSean Bruno 	txq = r->cookie;
3706da69b8f9SSean Bruno 
3707da69b8f9SSean Bruno 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3708da69b8f9SSean Bruno 	CALLOUT_LOCK(txq);
3709da69b8f9SSean Bruno 	callout_stop(&txq->ift_timer);
3710da69b8f9SSean Bruno 	CALLOUT_UNLOCK(txq);
3711da69b8f9SSean Bruno 
3712da69b8f9SSean Bruno 	avail = IDXDIFF(pidx, cidx, r->size);
3713da69b8f9SSean Bruno 	for (i = 0; i < avail; i++) {
371495246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, avail - i);
371595246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq))
371695246abbSSean Bruno 			continue;
3717da69b8f9SSean Bruno 		m_freem(*mp);
371864e6fc13SStephen Hurd 		DBG_COUNTER_INC(tx_frees);
3719da69b8f9SSean Bruno 	}
3720da69b8f9SSean Bruno 	MPASS(ifmp_ring_is_stalled(r) == 0);
3721da69b8f9SSean Bruno 	return (avail);
3722da69b8f9SSean Bruno }
3723da69b8f9SSean Bruno 
3724da69b8f9SSean Bruno static void
3725da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq)
3726da69b8f9SSean Bruno {
3727da69b8f9SSean Bruno 	struct ifmp_ring *r;
3728da69b8f9SSean Bruno 
372995246abbSSean Bruno 	r = txq->ift_br;
3730da69b8f9SSean Bruno 	r->drain = iflib_txq_drain_free;
3731da69b8f9SSean Bruno 	r->can_drain = iflib_txq_drain_always;
3732da69b8f9SSean Bruno 
3733da69b8f9SSean Bruno 	ifmp_ring_check_drainage(r, r->size);
3734da69b8f9SSean Bruno 
3735da69b8f9SSean Bruno 	r->drain = iflib_txq_drain;
3736da69b8f9SSean Bruno 	r->can_drain = iflib_txq_can_drain;
3737da69b8f9SSean Bruno }
3738da69b8f9SSean Bruno 
37394c7070dbSScott Long static void
374023ac9029SStephen Hurd _task_fn_tx(void *context)
37414c7070dbSScott Long {
37424c7070dbSScott Long 	iflib_txq_t txq = context;
37434c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
3744a6611c93SMarius Strobl #if defined(ALTQ) || defined(DEV_NETMAP)
3745a6611c93SMarius Strobl 	if_t ifp = ctx->ifc_ifp;
3746a6611c93SMarius Strobl #endif
3747fe51d4cdSStephen Hurd 	int abdicate = ctx->ifc_sysctl_tx_abdicate;
37484c7070dbSScott Long 
37491248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
37501248952aSSean Bruno 	txq->ift_cpu_exec_count[curcpu]++;
37511248952aSSean Bruno #endif
37524c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
37534c7070dbSScott Long 		return;
375495dcf343SMarius Strobl #ifdef DEV_NETMAP
3755a6611c93SMarius Strobl 	if (if_getcapenable(ifp) & IFCAP_NETMAP) {
37568a04b53dSKonstantin Belousov 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
37578a04b53dSKonstantin Belousov 		    BUS_DMASYNC_POSTREAD);
375895246abbSSean Bruno 		if (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false))
3759a6611c93SMarius Strobl 			netmap_tx_irq(ifp, txq->ift_id);
37603d10e9edSMarius Strobl 		if (ctx->ifc_flags & IFC_LEGACY)
37613d10e9edSMarius Strobl 			IFDI_INTR_ENABLE(ctx);
37623d10e9edSMarius Strobl 		else
376395246abbSSean Bruno 			IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
376495246abbSSean Bruno 		return;
376595246abbSSean Bruno 	}
376695dcf343SMarius Strobl #endif
3767b8ca4756SPatrick Kelsey #ifdef ALTQ
3768b8ca4756SPatrick Kelsey 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
3769b8ca4756SPatrick Kelsey 		iflib_altq_if_start(ifp);
3770b8ca4756SPatrick Kelsey #endif
377195246abbSSean Bruno 	if (txq->ift_db_pending)
3772fe51d4cdSStephen Hurd 		ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate);
3773fe51d4cdSStephen Hurd 	else if (!abdicate)
3774fe51d4cdSStephen Hurd 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
3775fe51d4cdSStephen Hurd 	/*
3776fe51d4cdSStephen Hurd 	 * When abdicating, we always need to check drainage, not just when we don't enqueue
3777fe51d4cdSStephen Hurd 	 */
3778fe51d4cdSStephen Hurd 	if (abdicate)
3779fe51d4cdSStephen Hurd 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
378095246abbSSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
378195246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
37823d10e9edSMarius Strobl 	else
37831ae4848cSMatt Macy 		IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
37844c7070dbSScott Long }
37854c7070dbSScott Long 
37864c7070dbSScott Long static void
378723ac9029SStephen Hurd _task_fn_rx(void *context)
37884c7070dbSScott Long {
37894c7070dbSScott Long 	iflib_rxq_t rxq = context;
37904c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
3791fb1a29b4SHans Petter Selasky 	uint8_t more;
3792f4d2154eSStephen Hurd 	uint16_t budget;
37934c7070dbSScott Long 
37941248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
37951248952aSSean Bruno 	rxq->ifr_cpu_exec_count[curcpu]++;
37961248952aSSean Bruno #endif
37974c7070dbSScott Long 	DBG_COUNTER_INC(task_fn_rxs);
37984c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
37994c7070dbSScott Long 		return;
3800d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP
3801d0d0ad0aSStephen Hurd 	if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) {
3802d0d0ad0aSStephen Hurd 		u_int work = 0;
3803d0d0ad0aSStephen Hurd 		if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work)) {
3804fb1a29b4SHans Petter Selasky 			more = 0;
3805fb1a29b4SHans Petter Selasky 			goto skip_rxeof;
3806d0d0ad0aSStephen Hurd 		}
3807d0d0ad0aSStephen Hurd 	}
3808d0d0ad0aSStephen Hurd #endif
3809f4d2154eSStephen Hurd 	budget = ctx->ifc_sysctl_rx_budget;
3810f4d2154eSStephen Hurd 	if (budget == 0)
3811f4d2154eSStephen Hurd 		budget = 16;	/* XXX */
3812fb1a29b4SHans Petter Selasky 	more = iflib_rxeof(rxq, budget);
3813fb1a29b4SHans Petter Selasky #ifdef DEV_NETMAP
3814fb1a29b4SHans Petter Selasky skip_rxeof:
3815fb1a29b4SHans Petter Selasky #endif
3816fb1a29b4SHans Petter Selasky 	if ((more & IFLIB_RXEOF_MORE) == 0) {
38174c7070dbSScott Long 		if (ctx->ifc_flags & IFC_LEGACY)
38184c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
38193d10e9edSMarius Strobl 		else
38201ae4848cSMatt Macy 			IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
38211ae4848cSMatt Macy 		DBG_COUNTER_INC(rx_intr_enables);
38224c7070dbSScott Long 	}
38234c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
38244c7070dbSScott Long 		return;
3825fb1a29b4SHans Petter Selasky 
3826fb1a29b4SHans Petter Selasky 	if (more & IFLIB_RXEOF_MORE)
38274c7070dbSScott Long 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
3828fb1a29b4SHans Petter Selasky 	else if (more & IFLIB_RXEOF_EMPTY)
3829fb1a29b4SHans Petter Selasky 		callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq);
38304c7070dbSScott Long }
38314c7070dbSScott Long 
38324c7070dbSScott Long static void
383323ac9029SStephen Hurd _task_fn_admin(void *context)
38344c7070dbSScott Long {
38354c7070dbSScott Long 	if_ctx_t ctx = context;
38364c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
38374c7070dbSScott Long 	iflib_txq_t txq;
3838ab2e3f79SStephen Hurd 	int i;
383977c1fcecSEric Joyner 	bool oactive, running, do_reset, do_watchdog, in_detach;
3840dd7fbcf1SStephen Hurd 	uint32_t reset_on = hz / 2;
3841ab2e3f79SStephen Hurd 
38427b610b60SSean Bruno 	STATE_LOCK(ctx);
38437b610b60SSean Bruno 	running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING);
38447b610b60SSean Bruno 	oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE);
38457b610b60SSean Bruno 	do_reset = (ctx->ifc_flags & IFC_DO_RESET);
38467b610b60SSean Bruno 	do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG);
384777c1fcecSEric Joyner 	in_detach = (ctx->ifc_flags & IFC_IN_DETACH);
38487b610b60SSean Bruno 	ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG);
38497b610b60SSean Bruno 	STATE_UNLOCK(ctx);
38507b610b60SSean Bruno 
385177c1fcecSEric Joyner 	if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))
385277c1fcecSEric Joyner 		return;
385377c1fcecSEric Joyner 	if (in_detach)
3854ab2e3f79SStephen Hurd 		return;
38554c7070dbSScott Long 
38564c7070dbSScott Long 	CTX_LOCK(ctx);
38574c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
38584c7070dbSScott Long 		CALLOUT_LOCK(txq);
38594c7070dbSScott Long 		callout_stop(&txq->ift_timer);
38604c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
38614c7070dbSScott Long 	}
38627b610b60SSean Bruno 	if (do_watchdog) {
38637b610b60SSean Bruno 		ctx->ifc_watchdog_events++;
38647b610b60SSean Bruno 		IFDI_WATCHDOG_RESET(ctx);
38657b610b60SSean Bruno 	}
3866d300df01SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
3867dd7fbcf1SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
3868dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP
3869dd7fbcf1SStephen Hurd 		reset_on = hz / 2;
3870dd7fbcf1SStephen Hurd 		if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP)
387195dcf343SMarius Strobl 			iflib_netmap_timer_adjust(ctx, txq, &reset_on);
3872dd7fbcf1SStephen Hurd #endif
3873dd7fbcf1SStephen Hurd 		callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu);
3874dd7fbcf1SStephen Hurd 	}
3875ab2e3f79SStephen Hurd 	IFDI_LINK_INTR_ENABLE(ctx);
38767b610b60SSean Bruno 	if (do_reset)
3877ab2e3f79SStephen Hurd 		iflib_if_init_locked(ctx);
38784c7070dbSScott Long 	CTX_UNLOCK(ctx);
38794c7070dbSScott Long 
3880ab2e3f79SStephen Hurd 	if (LINK_ACTIVE(ctx) == 0)
38814c7070dbSScott Long 		return;
38824c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
38834c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
38844c7070dbSScott Long }
38854c7070dbSScott Long 
38864c7070dbSScott Long 
38874c7070dbSScott Long static void
388823ac9029SStephen Hurd _task_fn_iov(void *context)
38894c7070dbSScott Long {
38904c7070dbSScott Long 	if_ctx_t ctx = context;
38914c7070dbSScott Long 
389277c1fcecSEric Joyner 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) &&
389377c1fcecSEric Joyner 	    !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))
38944c7070dbSScott Long 		return;
38954c7070dbSScott Long 
38964c7070dbSScott Long 	CTX_LOCK(ctx);
38974c7070dbSScott Long 	IFDI_VFLR_HANDLE(ctx);
38984c7070dbSScott Long 	CTX_UNLOCK(ctx);
38994c7070dbSScott Long }
39004c7070dbSScott Long 
39014c7070dbSScott Long static int
39024c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
39034c7070dbSScott Long {
39044c7070dbSScott Long 	int err;
39054c7070dbSScott Long 	if_int_delay_info_t info;
39064c7070dbSScott Long 	if_ctx_t ctx;
39074c7070dbSScott Long 
39084c7070dbSScott Long 	info = (if_int_delay_info_t)arg1;
39094c7070dbSScott Long 	ctx = info->iidi_ctx;
39104c7070dbSScott Long 	info->iidi_req = req;
39114c7070dbSScott Long 	info->iidi_oidp = oidp;
39124c7070dbSScott Long 	CTX_LOCK(ctx);
39134c7070dbSScott Long 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
39144c7070dbSScott Long 	CTX_UNLOCK(ctx);
39154c7070dbSScott Long 	return (err);
39164c7070dbSScott Long }
39174c7070dbSScott Long 
39184c7070dbSScott Long /*********************************************************************
39194c7070dbSScott Long  *
39204c7070dbSScott Long  *  IFNET FUNCTIONS
39214c7070dbSScott Long  *
39224c7070dbSScott Long  **********************************************************************/
39234c7070dbSScott Long 
39244c7070dbSScott Long static void
39254c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx)
39264c7070dbSScott Long {
39274c7070dbSScott Long 	iflib_stop(ctx);
39284c7070dbSScott Long 	iflib_init_locked(ctx);
39294c7070dbSScott Long }
39304c7070dbSScott Long 
39314c7070dbSScott Long 
39324c7070dbSScott Long static void
39334c7070dbSScott Long iflib_if_init(void *arg)
39344c7070dbSScott Long {
39354c7070dbSScott Long 	if_ctx_t ctx = arg;
39364c7070dbSScott Long 
39374c7070dbSScott Long 	CTX_LOCK(ctx);
39384c7070dbSScott Long 	iflib_if_init_locked(ctx);
39394c7070dbSScott Long 	CTX_UNLOCK(ctx);
39404c7070dbSScott Long }
39414c7070dbSScott Long 
39424c7070dbSScott Long static int
39434c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m)
39444c7070dbSScott Long {
39454c7070dbSScott Long 	if_ctx_t	ctx = if_getsoftc(ifp);
39464c7070dbSScott Long 
39474c7070dbSScott Long 	iflib_txq_t txq;
394823ac9029SStephen Hurd 	int err, qidx;
3949fe51d4cdSStephen Hurd 	int abdicate = ctx->ifc_sysctl_tx_abdicate;
39504c7070dbSScott Long 
39514c7070dbSScott Long 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
39524c7070dbSScott Long 		DBG_COUNTER_INC(tx_frees);
39534c7070dbSScott Long 		m_freem(m);
3954225eae1bSEric Joyner 		return (ENETDOWN);
39554c7070dbSScott Long 	}
39564c7070dbSScott Long 
395723ac9029SStephen Hurd 	MPASS(m->m_nextpkt == NULL);
3958b8ca4756SPatrick Kelsey 	/* ALTQ-enabled interfaces always use queue 0. */
39594c7070dbSScott Long 	qidx = 0;
3960b8ca4756SPatrick Kelsey 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !ALTQ_IS_ENABLED(&ifp->if_snd))
39614c7070dbSScott Long 		qidx = QIDX(ctx, m);
39624c7070dbSScott Long 	/*
39634c7070dbSScott Long 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
39644c7070dbSScott Long 	 */
39654c7070dbSScott Long 	txq = &ctx->ifc_txqs[qidx];
39664c7070dbSScott Long 
39674c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
39684c7070dbSScott Long 	if (txq->ift_closed) {
39694c7070dbSScott Long 		while (m != NULL) {
39704c7070dbSScott Long 			next = m->m_nextpkt;
39714c7070dbSScott Long 			m->m_nextpkt = NULL;
39724c7070dbSScott Long 			m_freem(m);
397364e6fc13SStephen Hurd 			DBG_COUNTER_INC(tx_frees);
39744c7070dbSScott Long 			m = next;
39754c7070dbSScott Long 		}
39764c7070dbSScott Long 		return (ENOBUFS);
39774c7070dbSScott Long 	}
39784c7070dbSScott Long #endif
397923ac9029SStephen Hurd #ifdef notyet
39804c7070dbSScott Long 	qidx = count = 0;
39814c7070dbSScott Long 	mp = marr;
39824c7070dbSScott Long 	next = m;
39834c7070dbSScott Long 	do {
39844c7070dbSScott Long 		count++;
39854c7070dbSScott Long 		next = next->m_nextpkt;
39864c7070dbSScott Long 	} while (next != NULL);
39874c7070dbSScott Long 
398816fb86abSConrad Meyer 	if (count > nitems(marr))
39894c7070dbSScott Long 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
39904c7070dbSScott Long 			/* XXX check nextpkt */
39914c7070dbSScott Long 			m_freem(m);
39924c7070dbSScott Long 			/* XXX simplify for now */
39934c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
39944c7070dbSScott Long 			return (ENOBUFS);
39954c7070dbSScott Long 		}
39964c7070dbSScott Long 	for (next = m, i = 0; next != NULL; i++) {
39974c7070dbSScott Long 		mp[i] = next;
39984c7070dbSScott Long 		next = next->m_nextpkt;
39994c7070dbSScott Long 		mp[i]->m_nextpkt = NULL;
40004c7070dbSScott Long 	}
400123ac9029SStephen Hurd #endif
40024c7070dbSScott Long 	DBG_COUNTER_INC(tx_seen);
4003fe51d4cdSStephen Hurd 	err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate);
40044c7070dbSScott Long 
4005fe51d4cdSStephen Hurd 	if (abdicate)
4006ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
40071225d9daSStephen Hurd  	if (err) {
4008fe51d4cdSStephen Hurd 		if (!abdicate)
4009fe51d4cdSStephen Hurd 			GROUPTASK_ENQUEUE(&txq->ift_task);
40104c7070dbSScott Long 		/* support forthcoming later */
40114c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
40124c7070dbSScott Long 		txq->ift_closed = TRUE;
40134c7070dbSScott Long #endif
401495246abbSSean Bruno 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
401523ac9029SStephen Hurd 		m_freem(m);
401664e6fc13SStephen Hurd 		DBG_COUNTER_INC(tx_frees);
40174c7070dbSScott Long 	}
40184c7070dbSScott Long 
40194c7070dbSScott Long 	return (err);
40204c7070dbSScott Long }
40214c7070dbSScott Long 
4022b8ca4756SPatrick Kelsey #ifdef ALTQ
4023b8ca4756SPatrick Kelsey /*
4024b8ca4756SPatrick Kelsey  * The overall approach to integrating iflib with ALTQ is to continue to use
4025b8ca4756SPatrick Kelsey  * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware
4026b8ca4756SPatrick Kelsey  * ring.  Technically, when using ALTQ, queueing to an intermediate mp_ring
4027b8ca4756SPatrick Kelsey  * is redundant/unnecessary, but doing so minimizes the amount of
4028b8ca4756SPatrick Kelsey  * ALTQ-specific code required in iflib.  It is assumed that the overhead of
4029b8ca4756SPatrick Kelsey  * redundantly queueing to an intermediate mp_ring is swamped by the
4030b8ca4756SPatrick Kelsey  * performance limitations inherent in using ALTQ.
4031b8ca4756SPatrick Kelsey  *
4032b8ca4756SPatrick Kelsey  * When ALTQ support is compiled in, all iflib drivers will use a transmit
4033b8ca4756SPatrick Kelsey  * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the
4034b8ca4756SPatrick Kelsey  * given interface.  If ALTQ is enabled for an interface, then all
4035b8ca4756SPatrick Kelsey  * transmitted packets for that interface will be submitted to the ALTQ
4036b8ca4756SPatrick Kelsey  * subsystem via IFQ_ENQUEUE().  We don't use the legacy if_transmit()
4037b8ca4756SPatrick Kelsey  * implementation because it uses IFQ_HANDOFF(), which will duplicatively
4038b8ca4756SPatrick Kelsey  * update stats that the iflib machinery handles, and which is sensitve to
4039b8ca4756SPatrick Kelsey  * the disused IFF_DRV_OACTIVE flag.  Additionally, iflib_altq_if_start()
4040b8ca4756SPatrick Kelsey  * will be installed as the start routine for use by ALTQ facilities that
4041b8ca4756SPatrick Kelsey  * need to trigger queue drains on a scheduled basis.
4042b8ca4756SPatrick Kelsey  *
4043b8ca4756SPatrick Kelsey  */
4044b8ca4756SPatrick Kelsey static void
4045b8ca4756SPatrick Kelsey iflib_altq_if_start(if_t ifp)
4046b8ca4756SPatrick Kelsey {
4047b8ca4756SPatrick Kelsey 	struct ifaltq *ifq = &ifp->if_snd;
4048b8ca4756SPatrick Kelsey 	struct mbuf *m;
4049b8ca4756SPatrick Kelsey 
4050b8ca4756SPatrick Kelsey 	IFQ_LOCK(ifq);
4051b8ca4756SPatrick Kelsey 	IFQ_DEQUEUE_NOLOCK(ifq, m);
4052b8ca4756SPatrick Kelsey 	while (m != NULL) {
4053b8ca4756SPatrick Kelsey 		iflib_if_transmit(ifp, m);
4054b8ca4756SPatrick Kelsey 		IFQ_DEQUEUE_NOLOCK(ifq, m);
4055b8ca4756SPatrick Kelsey 	}
4056b8ca4756SPatrick Kelsey 	IFQ_UNLOCK(ifq);
4057b8ca4756SPatrick Kelsey }
4058b8ca4756SPatrick Kelsey 
4059b8ca4756SPatrick Kelsey static int
4060b8ca4756SPatrick Kelsey iflib_altq_if_transmit(if_t ifp, struct mbuf *m)
4061b8ca4756SPatrick Kelsey {
4062b8ca4756SPatrick Kelsey 	int err;
4063b8ca4756SPatrick Kelsey 
4064b8ca4756SPatrick Kelsey 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
4065b8ca4756SPatrick Kelsey 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
4066b8ca4756SPatrick Kelsey 		if (err == 0)
4067b8ca4756SPatrick Kelsey 			iflib_altq_if_start(ifp);
4068b8ca4756SPatrick Kelsey 	} else
4069b8ca4756SPatrick Kelsey 		err = iflib_if_transmit(ifp, m);
4070b8ca4756SPatrick Kelsey 
4071b8ca4756SPatrick Kelsey 	return (err);
4072b8ca4756SPatrick Kelsey }
4073b8ca4756SPatrick Kelsey #endif /* ALTQ */
4074b8ca4756SPatrick Kelsey 
40754c7070dbSScott Long static void
40764c7070dbSScott Long iflib_if_qflush(if_t ifp)
40774c7070dbSScott Long {
40784c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
40794c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
40804c7070dbSScott Long 	int i;
40814c7070dbSScott Long 
40827b610b60SSean Bruno 	STATE_LOCK(ctx);
40834c7070dbSScott Long 	ctx->ifc_flags |= IFC_QFLUSH;
40847b610b60SSean Bruno 	STATE_UNLOCK(ctx);
40854c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
408695246abbSSean Bruno 		while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
40874c7070dbSScott Long 			iflib_txq_check_drain(txq, 0);
40887b610b60SSean Bruno 	STATE_LOCK(ctx);
40894c7070dbSScott Long 	ctx->ifc_flags &= ~IFC_QFLUSH;
40907b610b60SSean Bruno 	STATE_UNLOCK(ctx);
40914c7070dbSScott Long 
4092b8ca4756SPatrick Kelsey 	/*
4093b8ca4756SPatrick Kelsey 	 * When ALTQ is enabled, this will also take care of purging the
4094b8ca4756SPatrick Kelsey 	 * ALTQ queue(s).
4095b8ca4756SPatrick Kelsey 	 */
40964c7070dbSScott Long 	if_qflush(ifp);
40974c7070dbSScott Long }
40984c7070dbSScott Long 
40994c7070dbSScott Long 
41000c919c23SStephen Hurd #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
41010c919c23SStephen Hurd 		     IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
41020c919c23SStephen Hurd 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \
41036554362cSAndrew Gallatin 		     IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_NOMAP)
41044c7070dbSScott Long 
41054c7070dbSScott Long static int
41064c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
41074c7070dbSScott Long {
41084c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
41094c7070dbSScott Long 	struct ifreq	*ifr = (struct ifreq *)data;
41104c7070dbSScott Long #if defined(INET) || defined(INET6)
41114c7070dbSScott Long 	struct ifaddr	*ifa = (struct ifaddr *)data;
41124c7070dbSScott Long #endif
41131722eeacSMarius Strobl 	bool		avoid_reset = false;
41144c7070dbSScott Long 	int		err = 0, reinit = 0, bits;
41154c7070dbSScott Long 
41164c7070dbSScott Long 	switch (command) {
41174c7070dbSScott Long 	case SIOCSIFADDR:
41184c7070dbSScott Long #ifdef INET
41194c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET)
41201722eeacSMarius Strobl 			avoid_reset = true;
41214c7070dbSScott Long #endif
41224c7070dbSScott Long #ifdef INET6
41234c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET6)
41241722eeacSMarius Strobl 			avoid_reset = true;
41254c7070dbSScott Long #endif
41264c7070dbSScott Long 		/*
41274c7070dbSScott Long 		** Calling init results in link renegotiation,
41284c7070dbSScott Long 		** so we avoid doing it when possible.
41294c7070dbSScott Long 		*/
41304c7070dbSScott Long 		if (avoid_reset) {
41314c7070dbSScott Long 			if_setflagbits(ifp, IFF_UP,0);
41324c7070dbSScott Long 			if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
41334c7070dbSScott Long 				reinit = 1;
41344c7070dbSScott Long #ifdef INET
41354c7070dbSScott Long 			if (!(if_getflags(ifp) & IFF_NOARP))
41364c7070dbSScott Long 				arp_ifinit(ifp, ifa);
41374c7070dbSScott Long #endif
41384c7070dbSScott Long 		} else
41394c7070dbSScott Long 			err = ether_ioctl(ifp, command, data);
41404c7070dbSScott Long 		break;
41414c7070dbSScott Long 	case SIOCSIFMTU:
41424c7070dbSScott Long 		CTX_LOCK(ctx);
41434c7070dbSScott Long 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
41444c7070dbSScott Long 			CTX_UNLOCK(ctx);
41454c7070dbSScott Long 			break;
41464c7070dbSScott Long 		}
41474c7070dbSScott Long 		bits = if_getdrvflags(ifp);
41484c7070dbSScott Long 		/* stop the driver and free any clusters before proceeding */
41494c7070dbSScott Long 		iflib_stop(ctx);
41504c7070dbSScott Long 
41514c7070dbSScott Long 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
41527b610b60SSean Bruno 			STATE_LOCK(ctx);
41534c7070dbSScott Long 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
41544c7070dbSScott Long 				ctx->ifc_flags |= IFC_MULTISEG;
41554c7070dbSScott Long 			else
41564c7070dbSScott Long 				ctx->ifc_flags &= ~IFC_MULTISEG;
41577b610b60SSean Bruno 			STATE_UNLOCK(ctx);
41584c7070dbSScott Long 			err = if_setmtu(ifp, ifr->ifr_mtu);
41594c7070dbSScott Long 		}
41604c7070dbSScott Long 		iflib_init_locked(ctx);
41617b610b60SSean Bruno 		STATE_LOCK(ctx);
41624c7070dbSScott Long 		if_setdrvflags(ifp, bits);
41637b610b60SSean Bruno 		STATE_UNLOCK(ctx);
41644c7070dbSScott Long 		CTX_UNLOCK(ctx);
41654c7070dbSScott Long 		break;
41664c7070dbSScott Long 	case SIOCSIFFLAGS:
4167ab2e3f79SStephen Hurd 		CTX_LOCK(ctx);
4168ab2e3f79SStephen Hurd 		if (if_getflags(ifp) & IFF_UP) {
4169ab2e3f79SStephen Hurd 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4170ab2e3f79SStephen Hurd 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
4171ab2e3f79SStephen Hurd 				    (IFF_PROMISC | IFF_ALLMULTI)) {
4172ab2e3f79SStephen Hurd 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
4173ab2e3f79SStephen Hurd 				}
4174ab2e3f79SStephen Hurd 			} else
4175ab2e3f79SStephen Hurd 				reinit = 1;
4176ab2e3f79SStephen Hurd 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4177ab2e3f79SStephen Hurd 			iflib_stop(ctx);
4178ab2e3f79SStephen Hurd 		}
4179ab2e3f79SStephen Hurd 		ctx->ifc_if_flags = if_getflags(ifp);
4180ab2e3f79SStephen Hurd 		CTX_UNLOCK(ctx);
41814c7070dbSScott Long 		break;
41824c7070dbSScott Long 	case SIOCADDMULTI:
41834c7070dbSScott Long 	case SIOCDELMULTI:
41844c7070dbSScott Long 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4185ab2e3f79SStephen Hurd 			CTX_LOCK(ctx);
4186ab2e3f79SStephen Hurd 			IFDI_INTR_DISABLE(ctx);
4187ab2e3f79SStephen Hurd 			IFDI_MULTI_SET(ctx);
4188ab2e3f79SStephen Hurd 			IFDI_INTR_ENABLE(ctx);
4189ab2e3f79SStephen Hurd 			CTX_UNLOCK(ctx);
41904c7070dbSScott Long 		}
41914c7070dbSScott Long 		break;
41924c7070dbSScott Long 	case SIOCSIFMEDIA:
41934c7070dbSScott Long 		CTX_LOCK(ctx);
41944c7070dbSScott Long 		IFDI_MEDIA_SET(ctx);
41954c7070dbSScott Long 		CTX_UNLOCK(ctx);
41961722eeacSMarius Strobl 		/* FALLTHROUGH */
41974c7070dbSScott Long 	case SIOCGIFMEDIA:
4198a027c8e9SStephen Hurd 	case SIOCGIFXMEDIA:
4199e2621d96SMatt Macy 		err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command);
42004c7070dbSScott Long 		break;
42014c7070dbSScott Long 	case SIOCGI2C:
42024c7070dbSScott Long 	{
42034c7070dbSScott Long 		struct ifi2creq i2c;
42044c7070dbSScott Long 
4205541d96aaSBrooks Davis 		err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
42064c7070dbSScott Long 		if (err != 0)
42074c7070dbSScott Long 			break;
42084c7070dbSScott Long 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
42094c7070dbSScott Long 			err = EINVAL;
42104c7070dbSScott Long 			break;
42114c7070dbSScott Long 		}
42124c7070dbSScott Long 		if (i2c.len > sizeof(i2c.data)) {
42134c7070dbSScott Long 			err = EINVAL;
42144c7070dbSScott Long 			break;
42154c7070dbSScott Long 		}
42164c7070dbSScott Long 
42174c7070dbSScott Long 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
4218541d96aaSBrooks Davis 			err = copyout(&i2c, ifr_data_get_ptr(ifr),
4219541d96aaSBrooks Davis 			    sizeof(i2c));
42204c7070dbSScott Long 		break;
42214c7070dbSScott Long 	}
42224c7070dbSScott Long 	case SIOCSIFCAP:
42234c7070dbSScott Long 	{
42240c919c23SStephen Hurd 		int mask, setmask, oldmask;
42254c7070dbSScott Long 
42260c919c23SStephen Hurd 		oldmask = if_getcapenable(ifp);
42270c919c23SStephen Hurd 		mask = ifr->ifr_reqcap ^ oldmask;
42286554362cSAndrew Gallatin 		mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_NOMAP;
42294c7070dbSScott Long 		setmask = 0;
42304c7070dbSScott Long #ifdef TCP_OFFLOAD
42314c7070dbSScott Long 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
42324c7070dbSScott Long #endif
42334c7070dbSScott Long 		setmask |= (mask & IFCAP_FLAGS);
42340c919c23SStephen Hurd 		setmask |= (mask & IFCAP_WOL);
42354c7070dbSScott Long 
42360c919c23SStephen Hurd 		/*
4237a42546dfSStephen Hurd 		 * If any RX csum has changed, change all the ones that
4238a42546dfSStephen Hurd 		 * are supported by the driver.
42390c919c23SStephen Hurd 		 */
4240a42546dfSStephen Hurd 		if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
4241a42546dfSStephen Hurd 			setmask |= ctx->ifc_softc_ctx.isc_capabilities &
4242a42546dfSStephen Hurd 			    (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
4243a42546dfSStephen Hurd 		}
42440c919c23SStephen Hurd 
42454c7070dbSScott Long 		/*
42464c7070dbSScott Long 		 * want to ensure that traffic has stopped before we change any of the flags
42474c7070dbSScott Long 		 */
42484c7070dbSScott Long 		if (setmask) {
42494c7070dbSScott Long 			CTX_LOCK(ctx);
42504c7070dbSScott Long 			bits = if_getdrvflags(ifp);
42510c919c23SStephen Hurd 			if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL)
42524c7070dbSScott Long 				iflib_stop(ctx);
42537b610b60SSean Bruno 			STATE_LOCK(ctx);
42544c7070dbSScott Long 			if_togglecapenable(ifp, setmask);
42557b610b60SSean Bruno 			STATE_UNLOCK(ctx);
42560c919c23SStephen Hurd 			if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL)
42574c7070dbSScott Long 				iflib_init_locked(ctx);
42587b610b60SSean Bruno 			STATE_LOCK(ctx);
42594c7070dbSScott Long 			if_setdrvflags(ifp, bits);
42607b610b60SSean Bruno 			STATE_UNLOCK(ctx);
42614c7070dbSScott Long 			CTX_UNLOCK(ctx);
42624c7070dbSScott Long 		}
42630c919c23SStephen Hurd 		if_vlancap(ifp);
42644c7070dbSScott Long 		break;
42654c7070dbSScott Long 	}
42664c7070dbSScott Long 	case SIOCGPRIVATE_0:
42674c7070dbSScott Long 	case SIOCSDRVSPEC:
42684c7070dbSScott Long 	case SIOCGDRVSPEC:
42694c7070dbSScott Long 		CTX_LOCK(ctx);
42704c7070dbSScott Long 		err = IFDI_PRIV_IOCTL(ctx, command, data);
42714c7070dbSScott Long 		CTX_UNLOCK(ctx);
42724c7070dbSScott Long 		break;
42734c7070dbSScott Long 	default:
42744c7070dbSScott Long 		err = ether_ioctl(ifp, command, data);
42754c7070dbSScott Long 		break;
42764c7070dbSScott Long 	}
42774c7070dbSScott Long 	if (reinit)
42784c7070dbSScott Long 		iflib_if_init(ctx);
42794c7070dbSScott Long 	return (err);
42804c7070dbSScott Long }
42814c7070dbSScott Long 
42824c7070dbSScott Long static uint64_t
42834c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt)
42844c7070dbSScott Long {
42854c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
42864c7070dbSScott Long 
42874c7070dbSScott Long 	return (IFDI_GET_COUNTER(ctx, cnt));
42884c7070dbSScott Long }
42894c7070dbSScott Long 
42904c7070dbSScott Long /*********************************************************************
42914c7070dbSScott Long  *
42924c7070dbSScott Long  *  OTHER FUNCTIONS EXPORTED TO THE STACK
42934c7070dbSScott Long  *
42944c7070dbSScott Long  **********************************************************************/
42954c7070dbSScott Long 
42964c7070dbSScott Long static void
42974c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
42984c7070dbSScott Long {
42994c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
43004c7070dbSScott Long 
43014c7070dbSScott Long 	if ((void *)ctx != arg)
43024c7070dbSScott Long 		return;
43034c7070dbSScott Long 
43044c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
43054c7070dbSScott Long 		return;
43064c7070dbSScott Long 
430753b5b9b0SEric Joyner 	if (iflib_in_detach(ctx))
430853b5b9b0SEric Joyner 		return;
430953b5b9b0SEric Joyner 
43104c7070dbSScott Long 	CTX_LOCK(ctx);
431145818bf1SEric Joyner 	/* Driver may need all untagged packets to be flushed */
431245818bf1SEric Joyner 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
431345818bf1SEric Joyner 		iflib_stop(ctx);
43144c7070dbSScott Long 	IFDI_VLAN_REGISTER(ctx, vtag);
431545818bf1SEric Joyner 	/* Re-init to load the changes, if required */
431645818bf1SEric Joyner 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
431745818bf1SEric Joyner 		iflib_init_locked(ctx);
43184c7070dbSScott Long 	CTX_UNLOCK(ctx);
43194c7070dbSScott Long }
43204c7070dbSScott Long 
43214c7070dbSScott Long static void
43224c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
43234c7070dbSScott Long {
43244c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
43254c7070dbSScott Long 
43264c7070dbSScott Long 	if ((void *)ctx != arg)
43274c7070dbSScott Long 		return;
43284c7070dbSScott Long 
43294c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
43304c7070dbSScott Long 		return;
43314c7070dbSScott Long 
43324c7070dbSScott Long 	CTX_LOCK(ctx);
433345818bf1SEric Joyner 	/* Driver may need all tagged packets to be flushed */
433445818bf1SEric Joyner 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
433545818bf1SEric Joyner 		iflib_stop(ctx);
43364c7070dbSScott Long 	IFDI_VLAN_UNREGISTER(ctx, vtag);
433745818bf1SEric Joyner 	/* Re-init to load the changes, if required */
433845818bf1SEric Joyner 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
433945818bf1SEric Joyner 		iflib_init_locked(ctx);
43404c7070dbSScott Long 	CTX_UNLOCK(ctx);
43414c7070dbSScott Long }
43424c7070dbSScott Long 
43434c7070dbSScott Long static void
43444c7070dbSScott Long iflib_led_func(void *arg, int onoff)
43454c7070dbSScott Long {
43464c7070dbSScott Long 	if_ctx_t ctx = arg;
43474c7070dbSScott Long 
43484c7070dbSScott Long 	CTX_LOCK(ctx);
43494c7070dbSScott Long 	IFDI_LED_FUNC(ctx, onoff);
43504c7070dbSScott Long 	CTX_UNLOCK(ctx);
43514c7070dbSScott Long }
43524c7070dbSScott Long 
43534c7070dbSScott Long /*********************************************************************
43544c7070dbSScott Long  *
43554c7070dbSScott Long  *  BUS FUNCTION DEFINITIONS
43564c7070dbSScott Long  *
43574c7070dbSScott Long  **********************************************************************/
43584c7070dbSScott Long 
43594c7070dbSScott Long int
43604c7070dbSScott Long iflib_device_probe(device_t dev)
43614c7070dbSScott Long {
4362d49e83eaSMarius Strobl 	const pci_vendor_info_t *ent;
43634c7070dbSScott Long 	if_shared_ctx_t sctx;
4364d49e83eaSMarius Strobl 	uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id;
4365d49e83eaSMarius Strobl 	uint16_t pci_vendor_id;
43664c7070dbSScott Long 
43674c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
43684c7070dbSScott Long 		return (ENOTSUP);
43694c7070dbSScott Long 
43704c7070dbSScott Long 	pci_vendor_id = pci_get_vendor(dev);
43714c7070dbSScott Long 	pci_device_id = pci_get_device(dev);
43724c7070dbSScott Long 	pci_subvendor_id = pci_get_subvendor(dev);
43734c7070dbSScott Long 	pci_subdevice_id = pci_get_subdevice(dev);
43744c7070dbSScott Long 	pci_rev_id = pci_get_revid(dev);
43754c7070dbSScott Long 	if (sctx->isc_parse_devinfo != NULL)
43764c7070dbSScott Long 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
43774c7070dbSScott Long 
43784c7070dbSScott Long 	ent = sctx->isc_vendor_info;
43794c7070dbSScott Long 	while (ent->pvi_vendor_id != 0) {
43804c7070dbSScott Long 		if (pci_vendor_id != ent->pvi_vendor_id) {
43814c7070dbSScott Long 			ent++;
43824c7070dbSScott Long 			continue;
43834c7070dbSScott Long 		}
43844c7070dbSScott Long 		if ((pci_device_id == ent->pvi_device_id) &&
43854c7070dbSScott Long 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
43864c7070dbSScott Long 		     (ent->pvi_subvendor_id == 0)) &&
43874c7070dbSScott Long 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
43884c7070dbSScott Long 		     (ent->pvi_subdevice_id == 0)) &&
43894c7070dbSScott Long 		    ((pci_rev_id == ent->pvi_rev_id) ||
43904c7070dbSScott Long 		     (ent->pvi_rev_id == 0))) {
43914c7070dbSScott Long 
43924c7070dbSScott Long 			device_set_desc_copy(dev, ent->pvi_name);
43934c7070dbSScott Long 			/* this needs to be changed to zero if the bus probing code
43944c7070dbSScott Long 			 * ever stops re-probing on best match because the sctx
43954c7070dbSScott Long 			 * may have its values over written by register calls
43964c7070dbSScott Long 			 * in subsequent probes
43974c7070dbSScott Long 			 */
43984c7070dbSScott Long 			return (BUS_PROBE_DEFAULT);
43994c7070dbSScott Long 		}
44004c7070dbSScott Long 		ent++;
44014c7070dbSScott Long 	}
44024c7070dbSScott Long 	return (ENXIO);
44034c7070dbSScott Long }
44044c7070dbSScott Long 
4405668d6dbbSEric Joyner int
4406668d6dbbSEric Joyner iflib_device_probe_vendor(device_t dev)
4407668d6dbbSEric Joyner {
4408668d6dbbSEric Joyner 	int probe;
4409668d6dbbSEric Joyner 
4410668d6dbbSEric Joyner 	probe = iflib_device_probe(dev);
4411668d6dbbSEric Joyner 	if (probe == BUS_PROBE_DEFAULT)
4412668d6dbbSEric Joyner 		return (BUS_PROBE_VENDOR);
4413668d6dbbSEric Joyner 	else
4414668d6dbbSEric Joyner 		return (probe);
4415668d6dbbSEric Joyner }
4416668d6dbbSEric Joyner 
441709f6ff4fSMatt Macy static void
441809f6ff4fSMatt Macy iflib_reset_qvalues(if_ctx_t ctx)
44194c7070dbSScott Long {
442009f6ff4fSMatt Macy 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
442109f6ff4fSMatt Macy 	if_shared_ctx_t sctx = ctx->ifc_sctx;
442209f6ff4fSMatt Macy 	device_t dev = ctx->ifc_dev;
442346d0f824SMatt Macy 	int i;
44244c7070dbSScott Long 
442523ac9029SStephen Hurd 	if (ctx->ifc_sysctl_ntxqs != 0)
442623ac9029SStephen Hurd 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
442723ac9029SStephen Hurd 	if (ctx->ifc_sysctl_nrxqs != 0)
442823ac9029SStephen Hurd 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
442923ac9029SStephen Hurd 
443023ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
443123ac9029SStephen Hurd 		if (ctx->ifc_sysctl_ntxds[i] != 0)
443223ac9029SStephen Hurd 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
443323ac9029SStephen Hurd 		else
443423ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
443523ac9029SStephen Hurd 	}
443623ac9029SStephen Hurd 
443723ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
443823ac9029SStephen Hurd 		if (ctx->ifc_sysctl_nrxds[i] != 0)
443923ac9029SStephen Hurd 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
444023ac9029SStephen Hurd 		else
444123ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
444223ac9029SStephen Hurd 	}
444323ac9029SStephen Hurd 
444423ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
444523ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
444623ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
444723ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
444823ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
444923ac9029SStephen Hurd 		}
445023ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
445123ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
445223ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
445323ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
445423ac9029SStephen Hurd 		}
4455afb77372SEric Joyner 		if (!powerof2(scctx->isc_nrxd[i])) {
4456afb77372SEric Joyner 			device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n",
4457afb77372SEric Joyner 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]);
4458afb77372SEric Joyner 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
4459afb77372SEric Joyner 		}
446023ac9029SStephen Hurd 	}
446123ac9029SStephen Hurd 
446223ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
446323ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
446423ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
446523ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
446623ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
446723ac9029SStephen Hurd 		}
446823ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
446923ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
447023ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
447123ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
447223ac9029SStephen Hurd 		}
4473afb77372SEric Joyner 		if (!powerof2(scctx->isc_ntxd[i])) {
4474afb77372SEric Joyner 			device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n",
4475afb77372SEric Joyner 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]);
4476afb77372SEric Joyner 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
4477afb77372SEric Joyner 		}
447823ac9029SStephen Hurd 	}
447909f6ff4fSMatt Macy }
4480ab2e3f79SStephen Hurd 
44816d49b41eSAndrew Gallatin static void
44826d49b41eSAndrew Gallatin iflib_add_pfil(if_ctx_t ctx)
44836d49b41eSAndrew Gallatin {
44846d49b41eSAndrew Gallatin 	struct pfil_head *pfil;
44856d49b41eSAndrew Gallatin 	struct pfil_head_args pa;
44866d49b41eSAndrew Gallatin 	iflib_rxq_t rxq;
44876d49b41eSAndrew Gallatin 	int i;
44886d49b41eSAndrew Gallatin 
44896d49b41eSAndrew Gallatin 	pa.pa_version = PFIL_VERSION;
44906d49b41eSAndrew Gallatin 	pa.pa_flags = PFIL_IN;
44916d49b41eSAndrew Gallatin 	pa.pa_type = PFIL_TYPE_ETHERNET;
44926d49b41eSAndrew Gallatin 	pa.pa_headname = ctx->ifc_ifp->if_xname;
44936d49b41eSAndrew Gallatin 	pfil = pfil_head_register(&pa);
44946d49b41eSAndrew Gallatin 
44956d49b41eSAndrew Gallatin 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
44966d49b41eSAndrew Gallatin 		rxq->pfil = pfil;
44976d49b41eSAndrew Gallatin 	}
44986d49b41eSAndrew Gallatin }
44996d49b41eSAndrew Gallatin 
45006d49b41eSAndrew Gallatin static void
45016d49b41eSAndrew Gallatin iflib_rem_pfil(if_ctx_t ctx)
45026d49b41eSAndrew Gallatin {
45036d49b41eSAndrew Gallatin 	struct pfil_head *pfil;
45046d49b41eSAndrew Gallatin 	iflib_rxq_t rxq;
45056d49b41eSAndrew Gallatin 	int i;
45066d49b41eSAndrew Gallatin 
45076d49b41eSAndrew Gallatin 	rxq = ctx->ifc_rxqs;
45086d49b41eSAndrew Gallatin 	pfil = rxq->pfil;
45096d49b41eSAndrew Gallatin 	for (i = 0; i < NRXQSETS(ctx); i++, rxq++) {
45106d49b41eSAndrew Gallatin 		rxq->pfil = NULL;
45116d49b41eSAndrew Gallatin 	}
45126d49b41eSAndrew Gallatin 	pfil_head_unregister(pfil);
45136d49b41eSAndrew Gallatin }
45146d49b41eSAndrew Gallatin 
4515f154ece0SStephen Hurd static uint16_t
4516f154ece0SStephen Hurd get_ctx_core_offset(if_ctx_t ctx)
4517f154ece0SStephen Hurd {
4518f154ece0SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
4519f154ece0SStephen Hurd 	struct cpu_offset *op;
4520f154ece0SStephen Hurd 	uint16_t qc;
4521f154ece0SStephen Hurd 	uint16_t ret = ctx->ifc_sysctl_core_offset;
4522f154ece0SStephen Hurd 
4523f154ece0SStephen Hurd 	if (ret != CORE_OFFSET_UNSPECIFIED)
4524f154ece0SStephen Hurd 		return (ret);
4525f154ece0SStephen Hurd 
4526f154ece0SStephen Hurd 	if (ctx->ifc_sysctl_separate_txrx)
4527f154ece0SStephen Hurd 		qc = scctx->isc_ntxqsets + scctx->isc_nrxqsets;
4528f154ece0SStephen Hurd 	else
4529f154ece0SStephen Hurd 		qc = max(scctx->isc_ntxqsets, scctx->isc_nrxqsets);
4530f154ece0SStephen Hurd 
4531f154ece0SStephen Hurd 	mtx_lock(&cpu_offset_mtx);
4532f154ece0SStephen Hurd 	SLIST_FOREACH(op, &cpu_offsets, entries) {
4533f154ece0SStephen Hurd 		if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
4534f154ece0SStephen Hurd 			ret = op->offset;
4535f154ece0SStephen Hurd 			op->offset += qc;
4536f154ece0SStephen Hurd 			MPASS(op->refcount < UINT_MAX);
4537f154ece0SStephen Hurd 			op->refcount++;
4538f154ece0SStephen Hurd 			break;
4539f154ece0SStephen Hurd 		}
4540f154ece0SStephen Hurd 	}
4541f154ece0SStephen Hurd 	if (ret == CORE_OFFSET_UNSPECIFIED) {
4542f154ece0SStephen Hurd 		ret = 0;
4543f154ece0SStephen Hurd 		op = malloc(sizeof(struct cpu_offset), M_IFLIB,
4544f154ece0SStephen Hurd 		    M_NOWAIT | M_ZERO);
4545f154ece0SStephen Hurd 		if (op == NULL) {
4546f154ece0SStephen Hurd 			device_printf(ctx->ifc_dev,
4547f154ece0SStephen Hurd 			    "allocation for cpu offset failed.\n");
4548f154ece0SStephen Hurd 		} else {
4549f154ece0SStephen Hurd 			op->offset = qc;
4550f154ece0SStephen Hurd 			op->refcount = 1;
4551f154ece0SStephen Hurd 			CPU_COPY(&ctx->ifc_cpus, &op->set);
4552f154ece0SStephen Hurd 			SLIST_INSERT_HEAD(&cpu_offsets, op, entries);
4553f154ece0SStephen Hurd 		}
4554f154ece0SStephen Hurd 	}
4555f154ece0SStephen Hurd 	mtx_unlock(&cpu_offset_mtx);
4556f154ece0SStephen Hurd 
4557f154ece0SStephen Hurd 	return (ret);
4558f154ece0SStephen Hurd }
4559f154ece0SStephen Hurd 
4560f154ece0SStephen Hurd static void
4561f154ece0SStephen Hurd unref_ctx_core_offset(if_ctx_t ctx)
4562f154ece0SStephen Hurd {
4563f154ece0SStephen Hurd 	struct cpu_offset *op, *top;
4564f154ece0SStephen Hurd 
4565f154ece0SStephen Hurd 	mtx_lock(&cpu_offset_mtx);
4566f154ece0SStephen Hurd 	SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) {
4567f154ece0SStephen Hurd 		if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
4568f154ece0SStephen Hurd 			MPASS(op->refcount > 0);
4569f154ece0SStephen Hurd 			op->refcount--;
4570f154ece0SStephen Hurd 			if (op->refcount == 0) {
4571f154ece0SStephen Hurd 				SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries);
4572f154ece0SStephen Hurd 				free(op, M_IFLIB);
4573f154ece0SStephen Hurd 			}
4574f154ece0SStephen Hurd 			break;
4575f154ece0SStephen Hurd 		}
4576f154ece0SStephen Hurd 	}
4577f154ece0SStephen Hurd 	mtx_unlock(&cpu_offset_mtx);
4578f154ece0SStephen Hurd }
4579f154ece0SStephen Hurd 
458009f6ff4fSMatt Macy int
458109f6ff4fSMatt Macy iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
458209f6ff4fSMatt Macy {
458309f6ff4fSMatt Macy 	if_ctx_t ctx;
458409f6ff4fSMatt Macy 	if_t ifp;
458509f6ff4fSMatt Macy 	if_softc_ctx_t scctx;
45863d10e9edSMarius Strobl 	kobjop_desc_t kobj_desc;
45873d10e9edSMarius Strobl 	kobj_method_t *kobj_method;
4588afb77372SEric Joyner 	int err, msix, rid;
45893d10e9edSMarius Strobl 	uint16_t main_rxq, main_txq;
459009f6ff4fSMatt Macy 
459109f6ff4fSMatt Macy 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
459209f6ff4fSMatt Macy 
459309f6ff4fSMatt Macy 	if (sc == NULL) {
459409f6ff4fSMatt Macy 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
459509f6ff4fSMatt Macy 		device_set_softc(dev, ctx);
459609f6ff4fSMatt Macy 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
459709f6ff4fSMatt Macy 	}
459809f6ff4fSMatt Macy 
459909f6ff4fSMatt Macy 	ctx->ifc_sctx = sctx;
460009f6ff4fSMatt Macy 	ctx->ifc_dev = dev;
460109f6ff4fSMatt Macy 	ctx->ifc_softc = sc;
460209f6ff4fSMatt Macy 
460309f6ff4fSMatt Macy 	if ((err = iflib_register(ctx)) != 0) {
460409f6ff4fSMatt Macy 		device_printf(dev, "iflib_register failed %d\n", err);
46057f3eb9daSPatrick Kelsey 		goto fail_ctx_free;
460609f6ff4fSMatt Macy 	}
460709f6ff4fSMatt Macy 	iflib_add_device_sysctl_pre(ctx);
460809f6ff4fSMatt Macy 
460909f6ff4fSMatt Macy 	scctx = &ctx->ifc_softc_ctx;
461009f6ff4fSMatt Macy 	ifp = ctx->ifc_ifp;
461109f6ff4fSMatt Macy 
461209f6ff4fSMatt Macy 	iflib_reset_qvalues(ctx);
4613aa8a24d3SStephen Hurd 	CTX_LOCK(ctx);
4614ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
46154c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
46167f3eb9daSPatrick Kelsey 		goto fail_unlock;
46174c7070dbSScott Long 	}
46181248952aSSean Bruno 	_iflib_pre_assert(scctx);
46191248952aSSean Bruno 	ctx->ifc_txrx = *scctx->isc_txrx;
46201248952aSSean Bruno 
4621e2621d96SMatt Macy 	if (sctx->isc_flags & IFLIB_DRIVER_MEDIA)
4622e2621d96SMatt Macy 		ctx->ifc_mediap = scctx->isc_media;
4623e2621d96SMatt Macy 
46241248952aSSean Bruno #ifdef INVARIANTS
46257f87c040SMarius Strobl 	if (scctx->isc_capabilities & IFCAP_TXCSUM)
46261248952aSSean Bruno 		MPASS(scctx->isc_tx_csum_flags);
46271248952aSSean Bruno #endif
46281248952aSSean Bruno 
46296554362cSAndrew Gallatin 	if_setcapabilities(ifp,
46306554362cSAndrew Gallatin 	    scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_NOMAP);
46316554362cSAndrew Gallatin 	if_setcapenable(ifp,
46326554362cSAndrew Gallatin 	    scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_NOMAP);
46331248952aSSean Bruno 
46341248952aSSean Bruno 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
46351248952aSSean Bruno 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
46361248952aSSean Bruno 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
46371248952aSSean Bruno 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
463823ac9029SStephen Hurd 
463995246abbSSean Bruno 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
464095246abbSSean Bruno 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
464123ac9029SStephen Hurd 
464223ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
46431722eeacSMarius Strobl 	device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n",
464423ac9029SStephen Hurd 	    scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
464523ac9029SStephen Hurd 
464623ac9029SStephen Hurd 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
464723ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
464823ac9029SStephen Hurd 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
464923ac9029SStephen Hurd 		    MAX_SINGLE_PACKET_FRACTION);
465023ac9029SStephen Hurd 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
465123ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
465223ac9029SStephen Hurd 		scctx->isc_tx_tso_segments_max = max(1,
465323ac9029SStephen Hurd 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
46544c7070dbSScott Long 
46554c7070dbSScott Long 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
46567f87c040SMarius Strobl 	if (if_getcapabilities(ifp) & IFCAP_TSO) {
46577f87c040SMarius Strobl 		/*
46587f87c040SMarius Strobl 		 * The stack can't handle a TSO size larger than IP_MAXPACKET,
46597f87c040SMarius Strobl 		 * but some MACs do.
46607f87c040SMarius Strobl 		 */
46617f87c040SMarius Strobl 		if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max,
46627f87c040SMarius Strobl 		    IP_MAXPACKET));
46637f87c040SMarius Strobl 		/*
46647f87c040SMarius Strobl 		 * Take maximum number of m_pullup(9)'s in iflib_parse_header()
46657f87c040SMarius Strobl 		 * into account.  In the worst case, each of these calls will
46667f87c040SMarius Strobl 		 * add another mbuf and, thus, the requirement for another DMA
46677f87c040SMarius Strobl 		 * segment.  So for best performance, it doesn't make sense to
46687f87c040SMarius Strobl 		 * advertize a maximum of TSO segments that typically will
46697f87c040SMarius Strobl 		 * require defragmentation in iflib_encap().
46707f87c040SMarius Strobl 		 */
46717f87c040SMarius Strobl 		if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3);
46727f87c040SMarius Strobl 		if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max);
46737f87c040SMarius Strobl 	}
46744c7070dbSScott Long 	if (scctx->isc_rss_table_size == 0)
46754c7070dbSScott Long 		scctx->isc_rss_table_size = 64;
467623ac9029SStephen Hurd 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
4677da69b8f9SSean Bruno 
4678da69b8f9SSean Bruno 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
4679da69b8f9SSean Bruno 	/* XXX format name */
4680f855ec81SMarius Strobl 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx,
4681f855ec81SMarius Strobl 	    NULL, NULL, "admin");
4682e516b535SStephen Hurd 
4683772593dbSStephen Hurd 	/* Set up cpu set.  If it fails, use the set of all CPUs. */
4684e516b535SStephen Hurd 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) {
4685e516b535SStephen Hurd 		device_printf(dev, "Unable to fetch CPU list\n");
4686e516b535SStephen Hurd 		CPU_COPY(&all_cpus, &ctx->ifc_cpus);
4687e516b535SStephen Hurd 	}
4688e516b535SStephen Hurd 	MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0);
4689e516b535SStephen Hurd 
46904c7070dbSScott Long 	/*
4691b97de13aSMarius Strobl 	** Now set up MSI or MSI-X, should return us the number of supported
4692b97de13aSMarius Strobl 	** vectors (will be 1 for a legacy interrupt and MSI).
46934c7070dbSScott Long 	*/
46944c7070dbSScott Long 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
46954c7070dbSScott Long 		msix = scctx->isc_vectors;
46964c7070dbSScott Long 	} else if (scctx->isc_msix_bar != 0)
4697f7ae9a84SSean Bruno 	       /*
4698f7ae9a84SSean Bruno 		* The simple fact that isc_msix_bar is not 0 does not mean we
4699f7ae9a84SSean Bruno 		* we have a good value there that is known to work.
4700f7ae9a84SSean Bruno 		*/
47014c7070dbSScott Long 		msix = iflib_msix_init(ctx);
47024c7070dbSScott Long 	else {
47034c7070dbSScott Long 		scctx->isc_vectors = 1;
47044c7070dbSScott Long 		scctx->isc_ntxqsets = 1;
47054c7070dbSScott Long 		scctx->isc_nrxqsets = 1;
47064c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
47074c7070dbSScott Long 		msix = 0;
47084c7070dbSScott Long 	}
47094c7070dbSScott Long 	/* Get memory for the station queues */
47104c7070dbSScott Long 	if ((err = iflib_queues_alloc(ctx))) {
47114c7070dbSScott Long 		device_printf(dev, "Unable to allocate queue memory\n");
47127f3eb9daSPatrick Kelsey 		goto fail_intr_free;
47134c7070dbSScott Long 	}
47144c7070dbSScott Long 
4715ac88e6daSStephen Hurd 	if ((err = iflib_qset_structures_setup(ctx)))
47164c7070dbSScott Long 		goto fail_queues;
471769b7fc3eSSean Bruno 
4718bd84f700SSean Bruno 	/*
4719f154ece0SStephen Hurd 	 * Now that we know how many queues there are, get the core offset.
4720f154ece0SStephen Hurd 	 */
4721f154ece0SStephen Hurd 	ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx);
4722f154ece0SStephen Hurd 
47233d10e9edSMarius Strobl 	if (msix > 1) {
47243d10e9edSMarius Strobl 		/*
47253d10e9edSMarius Strobl 		 * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable
47263d10e9edSMarius Strobl 		 * aren't the default NULL implementation.
47273d10e9edSMarius Strobl 		 */
47283d10e9edSMarius Strobl 		kobj_desc = &ifdi_rx_queue_intr_enable_desc;
47293d10e9edSMarius Strobl 		kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
47303d10e9edSMarius Strobl 		    kobj_desc);
47313d10e9edSMarius Strobl 		if (kobj_method == &kobj_desc->deflt) {
47323d10e9edSMarius Strobl 			device_printf(dev,
47333d10e9edSMarius Strobl 			    "MSI-X requires ifdi_rx_queue_intr_enable method");
47343d10e9edSMarius Strobl 			err = EOPNOTSUPP;
47357f3eb9daSPatrick Kelsey 			goto fail_queues;
47364c7070dbSScott Long 		}
47373d10e9edSMarius Strobl 		kobj_desc = &ifdi_tx_queue_intr_enable_desc;
47383d10e9edSMarius Strobl 		kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
47393d10e9edSMarius Strobl 		    kobj_desc);
47403d10e9edSMarius Strobl 		if (kobj_method == &kobj_desc->deflt) {
47413d10e9edSMarius Strobl 			device_printf(dev,
47423d10e9edSMarius Strobl 			    "MSI-X requires ifdi_tx_queue_intr_enable method");
47433d10e9edSMarius Strobl 			err = EOPNOTSUPP;
47443d10e9edSMarius Strobl 			goto fail_queues;
47453d10e9edSMarius Strobl 		}
47463d10e9edSMarius Strobl 
47473d10e9edSMarius Strobl 		/*
47483d10e9edSMarius Strobl 		 * Assign the MSI-X vectors.
47493d10e9edSMarius Strobl 		 * Note that the default NULL ifdi_msix_intr_assign method will
47503d10e9edSMarius Strobl 		 * fail here, too.
47513d10e9edSMarius Strobl 		 */
47523d10e9edSMarius Strobl 		err = IFDI_MSIX_INTR_ASSIGN(ctx, msix);
47533d10e9edSMarius Strobl 		if (err != 0) {
47543d10e9edSMarius Strobl 			device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n",
47553d10e9edSMarius Strobl 			    err);
47563d10e9edSMarius Strobl 			goto fail_queues;
47573d10e9edSMarius Strobl 		}
4758197c6798SEric Joyner 	} else if (scctx->isc_intr != IFLIB_INTR_MSIX) {
47594c7070dbSScott Long 		rid = 0;
47604c7070dbSScott Long 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
47614c7070dbSScott Long 			MPASS(msix == 1);
47624c7070dbSScott Long 			rid = 1;
47634c7070dbSScott Long 		}
476423ac9029SStephen Hurd 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
47654c7070dbSScott Long 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
47667f3eb9daSPatrick Kelsey 			goto fail_queues;
47674c7070dbSScott Long 		}
4768197c6798SEric Joyner 	} else {
4769197c6798SEric Joyner 		device_printf(dev,
4770197c6798SEric Joyner 		    "Cannot use iflib with only 1 MSI-X interrupt!\n");
4771197c6798SEric Joyner 		err = ENODEV;
4772197c6798SEric Joyner 		goto fail_intr_free;
47734c7070dbSScott Long 	}
47747f87c040SMarius Strobl 
47751fd8c72cSKyle Evans 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
47767f87c040SMarius Strobl 
4777ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
47784c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
47794c7070dbSScott Long 		goto fail_detach;
47804c7070dbSScott Long 	}
47817f87c040SMarius Strobl 
47827f87c040SMarius Strobl 	/*
47837f87c040SMarius Strobl 	 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
47847f87c040SMarius Strobl 	 * This must appear after the call to ether_ifattach() because
47857f87c040SMarius Strobl 	 * ether_ifattach() sets if_hdrlen to the default value.
47867f87c040SMarius Strobl 	 */
47877f87c040SMarius Strobl 	if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
47887f87c040SMarius Strobl 		if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
47897f87c040SMarius Strobl 
47904c7070dbSScott Long 	if ((err = iflib_netmap_attach(ctx))) {
47914c7070dbSScott Long 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
47924c7070dbSScott Long 		goto fail_detach;
47934c7070dbSScott Long 	}
47944c7070dbSScott Long 	*ctxp = ctx;
47954c7070dbSScott Long 
47967790c8c1SConrad Meyer 	DEBUGNET_SET(ctx->ifc_ifp, iflib);
479794618825SMark Johnston 
479823ac9029SStephen Hurd 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
47994c7070dbSScott Long 	iflib_add_device_sysctl_post(ctx);
48006d49b41eSAndrew Gallatin 	iflib_add_pfil(ctx);
48014ecb427aSSean Bruno 	ctx->ifc_flags |= IFC_INIT_DONE;
4802aa8a24d3SStephen Hurd 	CTX_UNLOCK(ctx);
48033d10e9edSMarius Strobl 
48044c7070dbSScott Long 	return (0);
480577c1fcecSEric Joyner 
48064c7070dbSScott Long fail_detach:
48074c7070dbSScott Long 	ether_ifdetach(ctx->ifc_ifp);
48084c7070dbSScott Long fail_intr_free:
48097f3eb9daSPatrick Kelsey 	iflib_free_intr_mem(ctx);
48104c7070dbSScott Long fail_queues:
48116108c013SStephen Hurd 	iflib_tx_structures_free(ctx);
48126108c013SStephen Hurd 	iflib_rx_structures_free(ctx);
4813197c6798SEric Joyner 	taskqgroup_detach(qgroup_if_config_tqg, &ctx->ifc_admin_task);
48144c7070dbSScott Long 	IFDI_DETACH(ctx);
48157f3eb9daSPatrick Kelsey fail_unlock:
4816aa8a24d3SStephen Hurd 	CTX_UNLOCK(ctx);
481756614414SEric Joyner 	iflib_deregister(ctx);
48187f3eb9daSPatrick Kelsey fail_ctx_free:
48197f3f6aadSEric Joyner 	device_set_softc(ctx->ifc_dev, NULL);
48207f3eb9daSPatrick Kelsey         if (ctx->ifc_flags & IFC_SC_ALLOCATED)
48217f3eb9daSPatrick Kelsey                 free(ctx->ifc_softc, M_IFLIB);
48227f3eb9daSPatrick Kelsey         free(ctx, M_IFLIB);
48234c7070dbSScott Long 	return (err);
48244c7070dbSScott Long }
48254c7070dbSScott Long 
48264c7070dbSScott Long int
482709f6ff4fSMatt Macy iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
482809f6ff4fSMatt Macy 					  struct iflib_cloneattach_ctx *clctx)
482909f6ff4fSMatt Macy {
483009f6ff4fSMatt Macy 	int err;
483109f6ff4fSMatt Macy 	if_ctx_t ctx;
483209f6ff4fSMatt Macy 	if_t ifp;
483309f6ff4fSMatt Macy 	if_softc_ctx_t scctx;
483409f6ff4fSMatt Macy 	int i;
483509f6ff4fSMatt Macy 	void *sc;
483609f6ff4fSMatt Macy 	uint16_t main_txq;
483709f6ff4fSMatt Macy 	uint16_t main_rxq;
483809f6ff4fSMatt Macy 
483909f6ff4fSMatt Macy 	ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO);
484009f6ff4fSMatt Macy 	sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
484109f6ff4fSMatt Macy 	ctx->ifc_flags |= IFC_SC_ALLOCATED;
484209f6ff4fSMatt Macy 	if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL))
484309f6ff4fSMatt Macy 		ctx->ifc_flags |= IFC_PSEUDO;
484409f6ff4fSMatt Macy 
484509f6ff4fSMatt Macy 	ctx->ifc_sctx = sctx;
484609f6ff4fSMatt Macy 	ctx->ifc_softc = sc;
484709f6ff4fSMatt Macy 	ctx->ifc_dev = dev;
484809f6ff4fSMatt Macy 
484909f6ff4fSMatt Macy 	if ((err = iflib_register(ctx)) != 0) {
485009f6ff4fSMatt Macy 		device_printf(dev, "%s: iflib_register failed %d\n", __func__, err);
48517f3eb9daSPatrick Kelsey 		goto fail_ctx_free;
485209f6ff4fSMatt Macy 	}
485309f6ff4fSMatt Macy 	iflib_add_device_sysctl_pre(ctx);
485409f6ff4fSMatt Macy 
485509f6ff4fSMatt Macy 	scctx = &ctx->ifc_softc_ctx;
485609f6ff4fSMatt Macy 	ifp = ctx->ifc_ifp;
485709f6ff4fSMatt Macy 
485809f6ff4fSMatt Macy 	iflib_reset_qvalues(ctx);
4859aac9c817SEric Joyner 	CTX_LOCK(ctx);
486009f6ff4fSMatt Macy 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
486109f6ff4fSMatt Macy 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
4862aac9c817SEric Joyner 		goto fail_unlock;
486309f6ff4fSMatt Macy 	}
486409f6ff4fSMatt Macy 	if (sctx->isc_flags & IFLIB_GEN_MAC)
48651fd8c72cSKyle Evans 		ether_gen_addr(ifp, &ctx->ifc_mac);
486609f6ff4fSMatt Macy 	if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name,
486709f6ff4fSMatt Macy 								clctx->cc_params)) != 0) {
486809f6ff4fSMatt Macy 		device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err);
48697f3eb9daSPatrick Kelsey 		goto fail_ctx_free;
487009f6ff4fSMatt Macy 	}
4871e2621d96SMatt Macy 	ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
4872e2621d96SMatt Macy 	ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL);
4873e2621d96SMatt Macy 	ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO);
487409f6ff4fSMatt Macy 
487509f6ff4fSMatt Macy #ifdef INVARIANTS
48767f87c040SMarius Strobl 	if (scctx->isc_capabilities & IFCAP_TXCSUM)
487709f6ff4fSMatt Macy 		MPASS(scctx->isc_tx_csum_flags);
487809f6ff4fSMatt Macy #endif
487909f6ff4fSMatt Macy 
48807f87c040SMarius Strobl 	if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_LINKSTATE);
488109f6ff4fSMatt Macy 	if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE);
488209f6ff4fSMatt Macy 
488309f6ff4fSMatt Macy 	ifp->if_flags |= IFF_NOGROUP;
488409f6ff4fSMatt Macy 	if (sctx->isc_flags & IFLIB_PSEUDO) {
48851fd8c72cSKyle Evans 		ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
488609f6ff4fSMatt Macy 
488709f6ff4fSMatt Macy 		if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
488809f6ff4fSMatt Macy 			device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
488909f6ff4fSMatt Macy 			goto fail_detach;
489009f6ff4fSMatt Macy 		}
489109f6ff4fSMatt Macy 		*ctxp = ctx;
489209f6ff4fSMatt Macy 
48937f87c040SMarius Strobl 		/*
48947f87c040SMarius Strobl 		 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
48957f87c040SMarius Strobl 		 * This must appear after the call to ether_ifattach() because
48967f87c040SMarius Strobl 		 * ether_ifattach() sets if_hdrlen to the default value.
48977f87c040SMarius Strobl 		 */
48987f87c040SMarius Strobl 		if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
48997f87c040SMarius Strobl 			if_setifheaderlen(ifp,
49007f87c040SMarius Strobl 			    sizeof(struct ether_vlan_header));
49017f87c040SMarius Strobl 
490209f6ff4fSMatt Macy 		if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
490309f6ff4fSMatt Macy 		iflib_add_device_sysctl_post(ctx);
490409f6ff4fSMatt Macy 		ctx->ifc_flags |= IFC_INIT_DONE;
490509f6ff4fSMatt Macy 		return (0);
490609f6ff4fSMatt Macy 	}
490709f6ff4fSMatt Macy 	_iflib_pre_assert(scctx);
490809f6ff4fSMatt Macy 	ctx->ifc_txrx = *scctx->isc_txrx;
490909f6ff4fSMatt Macy 
491009f6ff4fSMatt Macy 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
491109f6ff4fSMatt Macy 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
491209f6ff4fSMatt Macy 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
491309f6ff4fSMatt Macy 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
491409f6ff4fSMatt Macy 
491509f6ff4fSMatt Macy 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
491609f6ff4fSMatt Macy 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
491709f6ff4fSMatt Macy 
491809f6ff4fSMatt Macy 	/* XXX change for per-queue sizes */
49191722eeacSMarius Strobl 	device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n",
492009f6ff4fSMatt Macy 	    scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
492109f6ff4fSMatt Macy 
492209f6ff4fSMatt Macy 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
492309f6ff4fSMatt Macy 	    MAX_SINGLE_PACKET_FRACTION)
492409f6ff4fSMatt Macy 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
492509f6ff4fSMatt Macy 		    MAX_SINGLE_PACKET_FRACTION);
492609f6ff4fSMatt Macy 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
492709f6ff4fSMatt Macy 	    MAX_SINGLE_PACKET_FRACTION)
492809f6ff4fSMatt Macy 		scctx->isc_tx_tso_segments_max = max(1,
492909f6ff4fSMatt Macy 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
493009f6ff4fSMatt Macy 
493109f6ff4fSMatt Macy 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
49327f87c040SMarius Strobl 	if (if_getcapabilities(ifp) & IFCAP_TSO) {
49337f87c040SMarius Strobl 		/*
49347f87c040SMarius Strobl 		 * The stack can't handle a TSO size larger than IP_MAXPACKET,
49357f87c040SMarius Strobl 		 * but some MACs do.
49367f87c040SMarius Strobl 		 */
49377f87c040SMarius Strobl 		if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max,
49387f87c040SMarius Strobl 		    IP_MAXPACKET));
49397f87c040SMarius Strobl 		/*
49407f87c040SMarius Strobl 		 * Take maximum number of m_pullup(9)'s in iflib_parse_header()
49417f87c040SMarius Strobl 		 * into account.  In the worst case, each of these calls will
49427f87c040SMarius Strobl 		 * add another mbuf and, thus, the requirement for another DMA
49437f87c040SMarius Strobl 		 * segment.  So for best performance, it doesn't make sense to
49447f87c040SMarius Strobl 		 * advertize a maximum of TSO segments that typically will
49457f87c040SMarius Strobl 		 * require defragmentation in iflib_encap().
49467f87c040SMarius Strobl 		 */
49477f87c040SMarius Strobl 		if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3);
49487f87c040SMarius Strobl 		if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max);
49497f87c040SMarius Strobl 	}
495009f6ff4fSMatt Macy 	if (scctx->isc_rss_table_size == 0)
495109f6ff4fSMatt Macy 		scctx->isc_rss_table_size = 64;
495209f6ff4fSMatt Macy 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
495309f6ff4fSMatt Macy 
495409f6ff4fSMatt Macy 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
495509f6ff4fSMatt Macy 	/* XXX format name */
4956f855ec81SMarius Strobl 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx,
4957f855ec81SMarius Strobl 	    NULL, NULL, "admin");
495809f6ff4fSMatt Macy 
495909f6ff4fSMatt Macy 	/* XXX --- can support > 1 -- but keep it simple for now */
496009f6ff4fSMatt Macy 	scctx->isc_intr = IFLIB_INTR_LEGACY;
496109f6ff4fSMatt Macy 
496209f6ff4fSMatt Macy 	/* Get memory for the station queues */
496309f6ff4fSMatt Macy 	if ((err = iflib_queues_alloc(ctx))) {
496409f6ff4fSMatt Macy 		device_printf(dev, "Unable to allocate queue memory\n");
49657f3eb9daSPatrick Kelsey 		goto fail_iflib_detach;
496609f6ff4fSMatt Macy 	}
496709f6ff4fSMatt Macy 
496809f6ff4fSMatt Macy 	if ((err = iflib_qset_structures_setup(ctx))) {
496909f6ff4fSMatt Macy 		device_printf(dev, "qset structure setup failed %d\n", err);
497009f6ff4fSMatt Macy 		goto fail_queues;
497109f6ff4fSMatt Macy 	}
49727f87c040SMarius Strobl 
497309f6ff4fSMatt Macy 	/*
497409f6ff4fSMatt Macy 	 * XXX What if anything do we want to do about interrupts?
497509f6ff4fSMatt Macy 	 */
49761fd8c72cSKyle Evans 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
497709f6ff4fSMatt Macy 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
497809f6ff4fSMatt Macy 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
497909f6ff4fSMatt Macy 		goto fail_detach;
498009f6ff4fSMatt Macy 	}
49817f87c040SMarius Strobl 
49827f87c040SMarius Strobl 	/*
49837f87c040SMarius Strobl 	 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
49847f87c040SMarius Strobl 	 * This must appear after the call to ether_ifattach() because
49857f87c040SMarius Strobl 	 * ether_ifattach() sets if_hdrlen to the default value.
49867f87c040SMarius Strobl 	 */
49877f87c040SMarius Strobl 	if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
49887f87c040SMarius Strobl 		if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
49897f87c040SMarius Strobl 
499009f6ff4fSMatt Macy 	/* XXX handle more than one queue */
499109f6ff4fSMatt Macy 	for (i = 0; i < scctx->isc_nrxqsets; i++)
499209f6ff4fSMatt Macy 		IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl);
499309f6ff4fSMatt Macy 
499409f6ff4fSMatt Macy 	*ctxp = ctx;
499509f6ff4fSMatt Macy 
499609f6ff4fSMatt Macy 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
499709f6ff4fSMatt Macy 	iflib_add_device_sysctl_post(ctx);
499809f6ff4fSMatt Macy 	ctx->ifc_flags |= IFC_INIT_DONE;
4999aac9c817SEric Joyner 	CTX_UNLOCK(ctx);
50003d10e9edSMarius Strobl 
500109f6ff4fSMatt Macy 	return (0);
500209f6ff4fSMatt Macy fail_detach:
500309f6ff4fSMatt Macy 	ether_ifdetach(ctx->ifc_ifp);
500409f6ff4fSMatt Macy fail_queues:
500509f6ff4fSMatt Macy 	iflib_tx_structures_free(ctx);
500609f6ff4fSMatt Macy 	iflib_rx_structures_free(ctx);
50077f3eb9daSPatrick Kelsey fail_iflib_detach:
500809f6ff4fSMatt Macy 	IFDI_DETACH(ctx);
5009aac9c817SEric Joyner fail_unlock:
5010aac9c817SEric Joyner 	CTX_UNLOCK(ctx);
501156614414SEric Joyner 	iflib_deregister(ctx);
50127f3eb9daSPatrick Kelsey fail_ctx_free:
50137f3eb9daSPatrick Kelsey 	free(ctx->ifc_softc, M_IFLIB);
50147f3eb9daSPatrick Kelsey 	free(ctx, M_IFLIB);
501509f6ff4fSMatt Macy 	return (err);
501609f6ff4fSMatt Macy }
501709f6ff4fSMatt Macy 
501809f6ff4fSMatt Macy int
501909f6ff4fSMatt Macy iflib_pseudo_deregister(if_ctx_t ctx)
502009f6ff4fSMatt Macy {
502109f6ff4fSMatt Macy 	if_t ifp = ctx->ifc_ifp;
502209f6ff4fSMatt Macy 	iflib_txq_t txq;
502309f6ff4fSMatt Macy 	iflib_rxq_t rxq;
502409f6ff4fSMatt Macy 	int i, j;
502509f6ff4fSMatt Macy 	struct taskqgroup *tqg;
502609f6ff4fSMatt Macy 	iflib_fl_t fl;
502709f6ff4fSMatt Macy 
50281558015eSEric Joyner 	/* Unregister VLAN event handlers early */
50291558015eSEric Joyner 	iflib_unregister_vlan_handlers(ctx);
50301558015eSEric Joyner 
503109f6ff4fSMatt Macy 	ether_ifdetach(ifp);
503209f6ff4fSMatt Macy 	/* XXX drain any dependent tasks */
503309f6ff4fSMatt Macy 	tqg = qgroup_if_io_tqg;
503409f6ff4fSMatt Macy 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
503509f6ff4fSMatt Macy 		callout_drain(&txq->ift_timer);
503609f6ff4fSMatt Macy 		if (txq->ift_task.gt_uniq != NULL)
503709f6ff4fSMatt Macy 			taskqgroup_detach(tqg, &txq->ift_task);
503809f6ff4fSMatt Macy 	}
503909f6ff4fSMatt Macy 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
5040fb1a29b4SHans Petter Selasky 		callout_drain(&rxq->ifr_watchdog);
504109f6ff4fSMatt Macy 		if (rxq->ifr_task.gt_uniq != NULL)
504209f6ff4fSMatt Macy 			taskqgroup_detach(tqg, &rxq->ifr_task);
504309f6ff4fSMatt Macy 
504409f6ff4fSMatt Macy 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
504509f6ff4fSMatt Macy 			free(fl->ifl_rx_bitmap, M_IFLIB);
504609f6ff4fSMatt Macy 	}
504709f6ff4fSMatt Macy 	tqg = qgroup_if_config_tqg;
504809f6ff4fSMatt Macy 	if (ctx->ifc_admin_task.gt_uniq != NULL)
504909f6ff4fSMatt Macy 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
505009f6ff4fSMatt Macy 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
505109f6ff4fSMatt Macy 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
505209f6ff4fSMatt Macy 
505309f6ff4fSMatt Macy 	iflib_tx_structures_free(ctx);
505409f6ff4fSMatt Macy 	iflib_rx_structures_free(ctx);
505556614414SEric Joyner 
505656614414SEric Joyner 	iflib_deregister(ctx);
505756614414SEric Joyner 
505809f6ff4fSMatt Macy 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
505909f6ff4fSMatt Macy 		free(ctx->ifc_softc, M_IFLIB);
506009f6ff4fSMatt Macy 	free(ctx, M_IFLIB);
506109f6ff4fSMatt Macy 	return (0);
506209f6ff4fSMatt Macy }
506309f6ff4fSMatt Macy 
506409f6ff4fSMatt Macy int
50654c7070dbSScott Long iflib_device_attach(device_t dev)
50664c7070dbSScott Long {
50674c7070dbSScott Long 	if_ctx_t ctx;
50684c7070dbSScott Long 	if_shared_ctx_t sctx;
50694c7070dbSScott Long 
50704c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
50714c7070dbSScott Long 		return (ENOTSUP);
50724c7070dbSScott Long 
50734c7070dbSScott Long 	pci_enable_busmaster(dev);
50744c7070dbSScott Long 
50754c7070dbSScott Long 	return (iflib_device_register(dev, NULL, sctx, &ctx));
50764c7070dbSScott Long }
50774c7070dbSScott Long 
50784c7070dbSScott Long int
50794c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx)
50804c7070dbSScott Long {
50814c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
50824c7070dbSScott Long 	iflib_txq_t txq;
50834c7070dbSScott Long 	iflib_rxq_t rxq;
50844c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
508587890dbaSSean Bruno 	int i, j;
50864c7070dbSScott Long 	struct taskqgroup *tqg;
508787890dbaSSean Bruno 	iflib_fl_t fl;
50884c7070dbSScott Long 
50894c7070dbSScott Long 	/* Make sure VLANS are not using driver */
50904c7070dbSScott Long 	if (if_vlantrunkinuse(ifp)) {
50914c7070dbSScott Long 		device_printf(dev, "Vlan in use, detach first\n");
50924c7070dbSScott Long 		return (EBUSY);
50934c7070dbSScott Long 	}
509477c1fcecSEric Joyner #ifdef PCI_IOV
509577c1fcecSEric Joyner 	if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) {
509677c1fcecSEric Joyner 		device_printf(dev, "SR-IOV in use; detach first.\n");
509777c1fcecSEric Joyner 		return (EBUSY);
509877c1fcecSEric Joyner 	}
509977c1fcecSEric Joyner #endif
510077c1fcecSEric Joyner 
510177c1fcecSEric Joyner 	STATE_LOCK(ctx);
510277c1fcecSEric Joyner 	ctx->ifc_flags |= IFC_IN_DETACH;
510377c1fcecSEric Joyner 	STATE_UNLOCK(ctx);
51044c7070dbSScott Long 
51051558015eSEric Joyner 	/* Unregister VLAN handlers before calling iflib_stop() */
51061558015eSEric Joyner 	iflib_unregister_vlan_handlers(ctx);
51071558015eSEric Joyner 
51081558015eSEric Joyner 	iflib_netmap_detach(ifp);
51091558015eSEric Joyner 	ether_ifdetach(ifp);
51101558015eSEric Joyner 
51114c7070dbSScott Long 	CTX_LOCK(ctx);
51124c7070dbSScott Long 	iflib_stop(ctx);
51134c7070dbSScott Long 	CTX_UNLOCK(ctx);
51144c7070dbSScott Long 
51156d49b41eSAndrew Gallatin 	iflib_rem_pfil(ctx);
51164c7070dbSScott Long 	if (ctx->ifc_led_dev != NULL)
51174c7070dbSScott Long 		led_destroy(ctx->ifc_led_dev);
51184c7070dbSScott Long 	/* XXX drain any dependent tasks */
5119ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
512023ac9029SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
51214c7070dbSScott Long 		callout_drain(&txq->ift_timer);
51224c7070dbSScott Long 		if (txq->ift_task.gt_uniq != NULL)
51234c7070dbSScott Long 			taskqgroup_detach(tqg, &txq->ift_task);
51244c7070dbSScott Long 	}
51254c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
51264c7070dbSScott Long 		if (rxq->ifr_task.gt_uniq != NULL)
51274c7070dbSScott Long 			taskqgroup_detach(tqg, &rxq->ifr_task);
512887890dbaSSean Bruno 
512987890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
513087890dbaSSean Bruno 			free(fl->ifl_rx_bitmap, M_IFLIB);
51314c7070dbSScott Long 	}
5132ab2e3f79SStephen Hurd 	tqg = qgroup_if_config_tqg;
51334c7070dbSScott Long 	if (ctx->ifc_admin_task.gt_uniq != NULL)
51344c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
51354c7070dbSScott Long 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
51364c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
51376c3c3194SMatt Macy 	CTX_LOCK(ctx);
51384c7070dbSScott Long 	IFDI_DETACH(ctx);
51396c3c3194SMatt Macy 	CTX_UNLOCK(ctx);
51406c3c3194SMatt Macy 
51416c3c3194SMatt Macy 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
514277c1fcecSEric Joyner 	iflib_free_intr_mem(ctx);
514377c1fcecSEric Joyner 
514477c1fcecSEric Joyner 	bus_generic_detach(dev);
514577c1fcecSEric Joyner 
514677c1fcecSEric Joyner 	iflib_tx_structures_free(ctx);
514777c1fcecSEric Joyner 	iflib_rx_structures_free(ctx);
514856614414SEric Joyner 
514956614414SEric Joyner 	iflib_deregister(ctx);
515056614414SEric Joyner 
515156614414SEric Joyner 	device_set_softc(ctx->ifc_dev, NULL);
515277c1fcecSEric Joyner 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
515377c1fcecSEric Joyner 		free(ctx->ifc_softc, M_IFLIB);
5154f154ece0SStephen Hurd 	unref_ctx_core_offset(ctx);
515577c1fcecSEric Joyner 	free(ctx, M_IFLIB);
515677c1fcecSEric Joyner 	return (0);
515777c1fcecSEric Joyner }
515877c1fcecSEric Joyner 
515977c1fcecSEric Joyner static void
516077c1fcecSEric Joyner iflib_free_intr_mem(if_ctx_t ctx)
516177c1fcecSEric Joyner {
516277c1fcecSEric Joyner 
51634c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
51644c7070dbSScott Long 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
51654c7070dbSScott Long 	}
5166b97de13aSMarius Strobl 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
5167b97de13aSMarius Strobl 		pci_release_msi(ctx->ifc_dev);
5168b97de13aSMarius Strobl 	}
51694c7070dbSScott Long 	if (ctx->ifc_msix_mem != NULL) {
51704c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
5171b97de13aSMarius Strobl 		    rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem);
51724c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
51734c7070dbSScott Long 	}
51744c7070dbSScott Long }
51754c7070dbSScott Long 
51764c7070dbSScott Long int
51774c7070dbSScott Long iflib_device_detach(device_t dev)
51784c7070dbSScott Long {
51794c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
51804c7070dbSScott Long 
51814c7070dbSScott Long 	return (iflib_device_deregister(ctx));
51824c7070dbSScott Long }
51834c7070dbSScott Long 
51844c7070dbSScott Long int
51854c7070dbSScott Long iflib_device_suspend(device_t dev)
51864c7070dbSScott Long {
51874c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
51884c7070dbSScott Long 
51894c7070dbSScott Long 	CTX_LOCK(ctx);
51904c7070dbSScott Long 	IFDI_SUSPEND(ctx);
51914c7070dbSScott Long 	CTX_UNLOCK(ctx);
51924c7070dbSScott Long 
51934c7070dbSScott Long 	return bus_generic_suspend(dev);
51944c7070dbSScott Long }
51954c7070dbSScott Long int
51964c7070dbSScott Long iflib_device_shutdown(device_t dev)
51974c7070dbSScott Long {
51984c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
51994c7070dbSScott Long 
52004c7070dbSScott Long 	CTX_LOCK(ctx);
52014c7070dbSScott Long 	IFDI_SHUTDOWN(ctx);
52024c7070dbSScott Long 	CTX_UNLOCK(ctx);
52034c7070dbSScott Long 
52044c7070dbSScott Long 	return bus_generic_suspend(dev);
52054c7070dbSScott Long }
52064c7070dbSScott Long 
52074c7070dbSScott Long 
52084c7070dbSScott Long int
52094c7070dbSScott Long iflib_device_resume(device_t dev)
52104c7070dbSScott Long {
52114c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
52124c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
52134c7070dbSScott Long 
52144c7070dbSScott Long 	CTX_LOCK(ctx);
52154c7070dbSScott Long 	IFDI_RESUME(ctx);
5216cd28ea92SStephen Hurd 	iflib_if_init_locked(ctx);
52174c7070dbSScott Long 	CTX_UNLOCK(ctx);
52184c7070dbSScott Long 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
52194c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
52204c7070dbSScott Long 
52214c7070dbSScott Long 	return (bus_generic_resume(dev));
52224c7070dbSScott Long }
52234c7070dbSScott Long 
52244c7070dbSScott Long int
52254c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
52264c7070dbSScott Long {
52274c7070dbSScott Long 	int error;
52284c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
52294c7070dbSScott Long 
52304c7070dbSScott Long 	CTX_LOCK(ctx);
52314c7070dbSScott Long 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
52324c7070dbSScott Long 	CTX_UNLOCK(ctx);
52334c7070dbSScott Long 
52344c7070dbSScott Long 	return (error);
52354c7070dbSScott Long }
52364c7070dbSScott Long 
52374c7070dbSScott Long void
52384c7070dbSScott Long iflib_device_iov_uninit(device_t dev)
52394c7070dbSScott Long {
52404c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
52414c7070dbSScott Long 
52424c7070dbSScott Long 	CTX_LOCK(ctx);
52434c7070dbSScott Long 	IFDI_IOV_UNINIT(ctx);
52444c7070dbSScott Long 	CTX_UNLOCK(ctx);
52454c7070dbSScott Long }
52464c7070dbSScott Long 
52474c7070dbSScott Long int
52484c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
52494c7070dbSScott Long {
52504c7070dbSScott Long 	int error;
52514c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
52524c7070dbSScott Long 
52534c7070dbSScott Long 	CTX_LOCK(ctx);
52544c7070dbSScott Long 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
52554c7070dbSScott Long 	CTX_UNLOCK(ctx);
52564c7070dbSScott Long 
52574c7070dbSScott Long 	return (error);
52584c7070dbSScott Long }
52594c7070dbSScott Long 
52604c7070dbSScott Long /*********************************************************************
52614c7070dbSScott Long  *
52624c7070dbSScott Long  *  MODULE FUNCTION DEFINITIONS
52634c7070dbSScott Long  *
52644c7070dbSScott Long  **********************************************************************/
52654c7070dbSScott Long 
5266ab2e3f79SStephen Hurd /*
5267ab2e3f79SStephen Hurd  * - Start a fast taskqueue thread for each core
5268ab2e3f79SStephen Hurd  * - Start a taskqueue for control operations
5269ab2e3f79SStephen Hurd  */
52704c7070dbSScott Long static int
52714c7070dbSScott Long iflib_module_init(void)
52724c7070dbSScott Long {
52734c7070dbSScott Long 	return (0);
52744c7070dbSScott Long }
52754c7070dbSScott Long 
52764c7070dbSScott Long static int
52774c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg)
52784c7070dbSScott Long {
52794c7070dbSScott Long 	int err;
52804c7070dbSScott Long 
52814c7070dbSScott Long 	switch (what) {
52824c7070dbSScott Long 	case MOD_LOAD:
52834c7070dbSScott Long 		if ((err = iflib_module_init()) != 0)
52844c7070dbSScott Long 			return (err);
52854c7070dbSScott Long 		break;
52864c7070dbSScott Long 	case MOD_UNLOAD:
52874c7070dbSScott Long 		return (EBUSY);
52884c7070dbSScott Long 	default:
52894c7070dbSScott Long 		return (EOPNOTSUPP);
52904c7070dbSScott Long 	}
52914c7070dbSScott Long 
52924c7070dbSScott Long 	return (0);
52934c7070dbSScott Long }
52944c7070dbSScott Long 
52954c7070dbSScott Long /*********************************************************************
52964c7070dbSScott Long  *
52974c7070dbSScott Long  *  PUBLIC FUNCTION DEFINITIONS
52984c7070dbSScott Long  *     ordered as in iflib.h
52994c7070dbSScott Long  *
53004c7070dbSScott Long  **********************************************************************/
53014c7070dbSScott Long 
53024c7070dbSScott Long 
53034c7070dbSScott Long static void
53044c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx)
53054c7070dbSScott Long {
5306afb77372SEric Joyner 	int i;
5307afb77372SEric Joyner 
53084c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsize);
53094c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsegsize);
53104c7070dbSScott Long 
53114c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsize);
53124c7070dbSScott Long 	MPASS(sctx->isc_rx_nsegments);
53134c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsegsize);
53144c7070dbSScott Long 
5315afb77372SEric Joyner 	MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8);
5316afb77372SEric Joyner 	for (i = 0; i < sctx->isc_nrxqs; i++) {
5317afb77372SEric Joyner 		MPASS(sctx->isc_nrxd_min[i]);
5318afb77372SEric Joyner 		MPASS(powerof2(sctx->isc_nrxd_min[i]));
5319afb77372SEric Joyner 		MPASS(sctx->isc_nrxd_max[i]);
5320afb77372SEric Joyner 		MPASS(powerof2(sctx->isc_nrxd_max[i]));
5321afb77372SEric Joyner 		MPASS(sctx->isc_nrxd_default[i]);
5322afb77372SEric Joyner 		MPASS(powerof2(sctx->isc_nrxd_default[i]));
5323afb77372SEric Joyner 	}
5324afb77372SEric Joyner 
5325afb77372SEric Joyner 	MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8);
5326afb77372SEric Joyner 	for (i = 0; i < sctx->isc_ntxqs; i++) {
5327afb77372SEric Joyner 		MPASS(sctx->isc_ntxd_min[i]);
5328afb77372SEric Joyner 		MPASS(powerof2(sctx->isc_ntxd_min[i]));
5329afb77372SEric Joyner 		MPASS(sctx->isc_ntxd_max[i]);
5330afb77372SEric Joyner 		MPASS(powerof2(sctx->isc_ntxd_max[i]));
5331afb77372SEric Joyner 		MPASS(sctx->isc_ntxd_default[i]);
5332afb77372SEric Joyner 		MPASS(powerof2(sctx->isc_ntxd_default[i]));
5333afb77372SEric Joyner 	}
53344c7070dbSScott Long }
53354c7070dbSScott Long 
53361248952aSSean Bruno static void
53371248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx)
53381248952aSSean Bruno {
53391248952aSSean Bruno 
53401248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_encap);
53411248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_flush);
53421248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
53431248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_available);
53441248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
53451248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_refill);
53461248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_flush);
53471248952aSSean Bruno }
53482fe66646SSean Bruno 
53494c7070dbSScott Long static int
53504c7070dbSScott Long iflib_register(if_ctx_t ctx)
53514c7070dbSScott Long {
53524c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
53534c7070dbSScott Long 	driver_t *driver = sctx->isc_driver;
53544c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
53554c7070dbSScott Long 	if_t ifp;
53564c7070dbSScott Long 
53574c7070dbSScott Long 	_iflib_assert(sctx);
53584c7070dbSScott Long 
5359aa8a24d3SStephen Hurd 	CTX_LOCK_INIT(ctx);
53607b610b60SSean Bruno 	STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
536177c1fcecSEric Joyner 	ifp = ctx->ifc_ifp = if_alloc(IFT_ETHER);
53624c7070dbSScott Long 	if (ifp == NULL) {
53634c7070dbSScott Long 		device_printf(dev, "can not allocate ifnet structure\n");
53644c7070dbSScott Long 		return (ENOMEM);
53654c7070dbSScott Long 	}
53664c7070dbSScott Long 
53674c7070dbSScott Long 	/*
53684c7070dbSScott Long 	 * Initialize our context's device specific methods
53694c7070dbSScott Long 	 */
53704c7070dbSScott Long 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
53714c7070dbSScott Long 	kobj_class_compile((kobj_class_t) driver);
53724c7070dbSScott Long 
53734c7070dbSScott Long 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
53744c7070dbSScott Long 	if_setsoftc(ifp, ctx);
53754c7070dbSScott Long 	if_setdev(ifp, dev);
53764c7070dbSScott Long 	if_setinitfn(ifp, iflib_if_init);
53774c7070dbSScott Long 	if_setioctlfn(ifp, iflib_if_ioctl);
5378b8ca4756SPatrick Kelsey #ifdef ALTQ
5379b8ca4756SPatrick Kelsey 	if_setstartfn(ifp, iflib_altq_if_start);
5380b8ca4756SPatrick Kelsey 	if_settransmitfn(ifp, iflib_altq_if_transmit);
53818f410865SPatrick Kelsey 	if_setsendqready(ifp);
5382b8ca4756SPatrick Kelsey #else
53834c7070dbSScott Long 	if_settransmitfn(ifp, iflib_if_transmit);
5384b8ca4756SPatrick Kelsey #endif
53854c7070dbSScott Long 	if_setqflushfn(ifp, iflib_if_qflush);
5386e87c4940SGleb Smirnoff 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
5387e87c4940SGleb Smirnoff 	    IFF_KNOWSEPOCH);
53884c7070dbSScott Long 
53894c7070dbSScott Long 	ctx->ifc_vlan_attach_event =
53904c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
53914c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
53924c7070dbSScott Long 	ctx->ifc_vlan_detach_event =
53934c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
53944c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
53954c7070dbSScott Long 
5396e2621d96SMatt Macy 	if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) {
5397e2621d96SMatt Macy 		ctx->ifc_mediap = &ctx->ifc_media;
5398e2621d96SMatt Macy 		ifmedia_init(ctx->ifc_mediap, IFM_IMASK,
53994c7070dbSScott Long 		    iflib_media_change, iflib_media_status);
5400e2621d96SMatt Macy 	}
54014c7070dbSScott Long 	return (0);
54024c7070dbSScott Long }
54034c7070dbSScott Long 
540456614414SEric Joyner static void
54051558015eSEric Joyner iflib_unregister_vlan_handlers(if_ctx_t ctx)
540656614414SEric Joyner {
540756614414SEric Joyner 	/* Unregister VLAN events */
540856614414SEric Joyner 	if (ctx->ifc_vlan_attach_event != NULL) {
540956614414SEric Joyner 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
541056614414SEric Joyner 		ctx->ifc_vlan_attach_event = NULL;
541156614414SEric Joyner 	}
541256614414SEric Joyner 	if (ctx->ifc_vlan_detach_event != NULL) {
541356614414SEric Joyner 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
541456614414SEric Joyner 		ctx->ifc_vlan_detach_event = NULL;
541556614414SEric Joyner 	}
541656614414SEric Joyner 
54171558015eSEric Joyner }
54181558015eSEric Joyner 
54191558015eSEric Joyner static void
54201558015eSEric Joyner iflib_deregister(if_ctx_t ctx)
54211558015eSEric Joyner {
54221558015eSEric Joyner 	if_t ifp = ctx->ifc_ifp;
54231558015eSEric Joyner 
54241558015eSEric Joyner 	/* Remove all media */
54251558015eSEric Joyner 	ifmedia_removeall(&ctx->ifc_media);
54261558015eSEric Joyner 
54271558015eSEric Joyner 	/* Ensure that VLAN event handlers are unregistered */
54281558015eSEric Joyner 	iflib_unregister_vlan_handlers(ctx);
54291558015eSEric Joyner 
543056614414SEric Joyner 	/* Release kobject reference */
543156614414SEric Joyner 	kobj_delete((kobj_t) ctx, NULL);
543256614414SEric Joyner 
543356614414SEric Joyner 	/* Free the ifnet structure */
543456614414SEric Joyner 	if_free(ifp);
543556614414SEric Joyner 
543656614414SEric Joyner 	STATE_LOCK_DESTROY(ctx);
543756614414SEric Joyner 
543856614414SEric Joyner 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
543956614414SEric Joyner 	CTX_LOCK_DESTROY(ctx);
544056614414SEric Joyner }
544156614414SEric Joyner 
54424c7070dbSScott Long static int
54434c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx)
54444c7070dbSScott Long {
54454c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
544623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
54474c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
544823ac9029SStephen Hurd 	int nrxqsets = scctx->isc_nrxqsets;
544923ac9029SStephen Hurd 	int ntxqsets = scctx->isc_ntxqsets;
54504c7070dbSScott Long 	iflib_txq_t txq;
54514c7070dbSScott Long 	iflib_rxq_t rxq;
54524c7070dbSScott Long 	iflib_fl_t fl = NULL;
545323ac9029SStephen Hurd 	int i, j, cpu, err, txconf, rxconf;
54544c7070dbSScott Long 	iflib_dma_info_t ifdip;
545523ac9029SStephen Hurd 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
545623ac9029SStephen Hurd 	uint32_t *txqsizes = scctx->isc_txqsizes;
54574c7070dbSScott Long 	uint8_t nrxqs = sctx->isc_nrxqs;
54584c7070dbSScott Long 	uint8_t ntxqs = sctx->isc_ntxqs;
54594c7070dbSScott Long 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
54604c7070dbSScott Long 	caddr_t *vaddrs;
54614c7070dbSScott Long 	uint64_t *paddrs;
54624c7070dbSScott Long 
546323ac9029SStephen Hurd 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
546423ac9029SStephen Hurd 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
54654c7070dbSScott Long 
54664c7070dbSScott Long 	/* Allocate the TX ring struct memory */
5467b89827a0SStephen Hurd 	if (!(ctx->ifc_txqs =
5468ac2fffa4SPedro F. Giffuni 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
5469ac2fffa4SPedro F. Giffuni 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
54704c7070dbSScott Long 		device_printf(dev, "Unable to allocate TX ring memory\n");
54714c7070dbSScott Long 		err = ENOMEM;
54724c7070dbSScott Long 		goto fail;
54734c7070dbSScott Long 	}
54744c7070dbSScott Long 
54754c7070dbSScott Long 	/* Now allocate the RX */
5476b89827a0SStephen Hurd 	if (!(ctx->ifc_rxqs =
5477ac2fffa4SPedro F. Giffuni 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
5478ac2fffa4SPedro F. Giffuni 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
54794c7070dbSScott Long 		device_printf(dev, "Unable to allocate RX ring memory\n");
54804c7070dbSScott Long 		err = ENOMEM;
54814c7070dbSScott Long 		goto rx_fail;
54824c7070dbSScott Long 	}
54834c7070dbSScott Long 
5484b89827a0SStephen Hurd 	txq = ctx->ifc_txqs;
5485b89827a0SStephen Hurd 	rxq = ctx->ifc_rxqs;
54864c7070dbSScott Long 
54874c7070dbSScott Long 	/*
54884c7070dbSScott Long 	 * XXX handle allocation failure
54894c7070dbSScott Long 	 */
549096c85efbSNathan Whitehorn 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
54914c7070dbSScott Long 		/* Set up some basics */
54924c7070dbSScott Long 
5493bfce461eSMarius Strobl 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs,
5494bfce461eSMarius Strobl 		    M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
5495bfce461eSMarius Strobl 			device_printf(dev,
5496bfce461eSMarius Strobl 			    "Unable to allocate TX DMA info memory\n");
54974c7070dbSScott Long 			err = ENOMEM;
54980d0338afSConrad Meyer 			goto err_tx_desc;
54994c7070dbSScott Long 		}
55004c7070dbSScott Long 		txq->ift_ifdi = ifdip;
55014c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, ifdip++) {
5502bfce461eSMarius Strobl 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) {
5503bfce461eSMarius Strobl 				device_printf(dev,
5504bfce461eSMarius Strobl 				    "Unable to allocate TX descriptors\n");
55054c7070dbSScott Long 				err = ENOMEM;
55064c7070dbSScott Long 				goto err_tx_desc;
55074c7070dbSScott Long 			}
550895246abbSSean Bruno 			txq->ift_txd_size[j] = scctx->isc_txd_size[j];
55094c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
55104c7070dbSScott Long 		}
55114c7070dbSScott Long 		txq->ift_ctx = ctx;
55124c7070dbSScott Long 		txq->ift_id = i;
551323ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
551423ac9029SStephen Hurd 			txq->ift_br_offset = 1;
551523ac9029SStephen Hurd 		} else {
551623ac9029SStephen Hurd 			txq->ift_br_offset = 0;
551723ac9029SStephen Hurd 		}
55184c7070dbSScott Long 		/* XXX fix this */
551996c85efbSNathan Whitehorn 		txq->ift_timer.c_cpu = cpu;
55204c7070dbSScott Long 
55214c7070dbSScott Long 		if (iflib_txsd_alloc(txq)) {
55224c7070dbSScott Long 			device_printf(dev, "Critical Failure setting up TX buffers\n");
55234c7070dbSScott Long 			err = ENOMEM;
55244c7070dbSScott Long 			goto err_tx_desc;
55254c7070dbSScott Long 		}
55264c7070dbSScott Long 
55274c7070dbSScott Long 		/* Initialize the TX lock */
55281722eeacSMarius Strobl 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout",
55294c7070dbSScott Long 		    device_get_nameunit(dev), txq->ift_id);
55304c7070dbSScott Long 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
55314c7070dbSScott Long 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
55324c7070dbSScott Long 
553395246abbSSean Bruno 		err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
55344c7070dbSScott Long 				      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
55354c7070dbSScott Long 		if (err) {
55364c7070dbSScott Long 			/* XXX free any allocated rings */
55374c7070dbSScott Long 			device_printf(dev, "Unable to allocate buf_ring\n");
55380d0338afSConrad Meyer 			goto err_tx_desc;
55394c7070dbSScott Long 		}
55404c7070dbSScott Long 	}
55414c7070dbSScott Long 
55424c7070dbSScott Long 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
55434c7070dbSScott Long 		/* Set up some basics */
5544fb1a29b4SHans Petter Selasky 		callout_init(&rxq->ifr_watchdog, 1);
55454c7070dbSScott Long 
5546bfce461eSMarius Strobl 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs,
5547bfce461eSMarius Strobl 		   M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
5548bfce461eSMarius Strobl 			device_printf(dev,
5549bfce461eSMarius Strobl 			    "Unable to allocate RX DMA info memory\n");
55504c7070dbSScott Long 			err = ENOMEM;
55510d0338afSConrad Meyer 			goto err_tx_desc;
55524c7070dbSScott Long 		}
55534c7070dbSScott Long 
55544c7070dbSScott Long 		rxq->ifr_ifdi = ifdip;
555595246abbSSean Bruno 		/* XXX this needs to be changed if #rx queues != #tx queues */
555695246abbSSean Bruno 		rxq->ifr_ntxqirq = 1;
555795246abbSSean Bruno 		rxq->ifr_txqid[0] = i;
55584c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, ifdip++) {
5559bfce461eSMarius Strobl 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) {
5560bfce461eSMarius Strobl 				device_printf(dev,
5561bfce461eSMarius Strobl 				    "Unable to allocate RX descriptors\n");
55624c7070dbSScott Long 				err = ENOMEM;
55634c7070dbSScott Long 				goto err_tx_desc;
55644c7070dbSScott Long 			}
55654c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
55664c7070dbSScott Long 		}
55674c7070dbSScott Long 		rxq->ifr_ctx = ctx;
55684c7070dbSScott Long 		rxq->ifr_id = i;
556923ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
557023ac9029SStephen Hurd 			rxq->ifr_fl_offset = 1;
55714c7070dbSScott Long 		} else {
557223ac9029SStephen Hurd 			rxq->ifr_fl_offset = 0;
55734c7070dbSScott Long 		}
55744c7070dbSScott Long 		rxq->ifr_nfl = nfree_lists;
55754c7070dbSScott Long 		if (!(fl =
5576ac2fffa4SPedro F. Giffuni 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
55774c7070dbSScott Long 			device_printf(dev, "Unable to allocate free list memory\n");
55784c7070dbSScott Long 			err = ENOMEM;
55790d0338afSConrad Meyer 			goto err_tx_desc;
55804c7070dbSScott Long 		}
55814c7070dbSScott Long 		rxq->ifr_fl = fl;
55824c7070dbSScott Long 		for (j = 0; j < nfree_lists; j++) {
558395246abbSSean Bruno 			fl[j].ifl_rxq = rxq;
558495246abbSSean Bruno 			fl[j].ifl_id = j;
558595246abbSSean Bruno 			fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
558695246abbSSean Bruno 			fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
55874c7070dbSScott Long 		}
55884c7070dbSScott Long 		/* Allocate receive buffers for the ring */
55894c7070dbSScott Long 		if (iflib_rxsd_alloc(rxq)) {
55904c7070dbSScott Long 			device_printf(dev,
55914c7070dbSScott Long 			    "Critical Failure setting up receive buffers\n");
55924c7070dbSScott Long 			err = ENOMEM;
55934c7070dbSScott Long 			goto err_rx_desc;
55944c7070dbSScott Long 		}
559587890dbaSSean Bruno 
559687890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
55973db348b5SMarius Strobl 			fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB,
55983db348b5SMarius Strobl 			    M_WAITOK);
55994c7070dbSScott Long 	}
56004c7070dbSScott Long 
56014c7070dbSScott Long 	/* TXQs */
56024c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
56034c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
56044c7070dbSScott Long 	for (i = 0; i < ntxqsets; i++) {
56054c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
56064c7070dbSScott Long 
56074c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, di++) {
56084c7070dbSScott Long 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
56094c7070dbSScott Long 			paddrs[i*ntxqs + j] = di->idi_paddr;
56104c7070dbSScott Long 		}
56114c7070dbSScott Long 	}
56124c7070dbSScott Long 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
5613bfce461eSMarius Strobl 		device_printf(ctx->ifc_dev,
5614bfce461eSMarius Strobl 		    "Unable to allocate device TX queue\n");
56154c7070dbSScott Long 		iflib_tx_structures_free(ctx);
56164c7070dbSScott Long 		free(vaddrs, M_IFLIB);
56174c7070dbSScott Long 		free(paddrs, M_IFLIB);
56184c7070dbSScott Long 		goto err_rx_desc;
56194c7070dbSScott Long 	}
56204c7070dbSScott Long 	free(vaddrs, M_IFLIB);
56214c7070dbSScott Long 	free(paddrs, M_IFLIB);
56224c7070dbSScott Long 
56234c7070dbSScott Long 	/* RXQs */
56244c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
56254c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
56264c7070dbSScott Long 	for (i = 0; i < nrxqsets; i++) {
56274c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
56284c7070dbSScott Long 
56294c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, di++) {
56304c7070dbSScott Long 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
56314c7070dbSScott Long 			paddrs[i*nrxqs + j] = di->idi_paddr;
56324c7070dbSScott Long 		}
56334c7070dbSScott Long 	}
56344c7070dbSScott Long 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
5635bfce461eSMarius Strobl 		device_printf(ctx->ifc_dev,
5636bfce461eSMarius Strobl 		    "Unable to allocate device RX queue\n");
56374c7070dbSScott Long 		iflib_tx_structures_free(ctx);
56384c7070dbSScott Long 		free(vaddrs, M_IFLIB);
56394c7070dbSScott Long 		free(paddrs, M_IFLIB);
56404c7070dbSScott Long 		goto err_rx_desc;
56414c7070dbSScott Long 	}
56424c7070dbSScott Long 	free(vaddrs, M_IFLIB);
56434c7070dbSScott Long 	free(paddrs, M_IFLIB);
56444c7070dbSScott Long 
56454c7070dbSScott Long 	return (0);
56464c7070dbSScott Long 
56474c7070dbSScott Long /* XXX handle allocation failure changes */
56484c7070dbSScott Long err_rx_desc:
56494c7070dbSScott Long err_tx_desc:
5650b89827a0SStephen Hurd rx_fail:
56514c7070dbSScott Long 	if (ctx->ifc_rxqs != NULL)
56524c7070dbSScott Long 		free(ctx->ifc_rxqs, M_IFLIB);
56534c7070dbSScott Long 	ctx->ifc_rxqs = NULL;
56544c7070dbSScott Long 	if (ctx->ifc_txqs != NULL)
56554c7070dbSScott Long 		free(ctx->ifc_txqs, M_IFLIB);
56564c7070dbSScott Long 	ctx->ifc_txqs = NULL;
56574c7070dbSScott Long fail:
56584c7070dbSScott Long 	return (err);
56594c7070dbSScott Long }
56604c7070dbSScott Long 
56614c7070dbSScott Long static int
56624c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx)
56634c7070dbSScott Long {
56644c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
56654c7070dbSScott Long 	int i;
56664c7070dbSScott Long 
56674c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
56684c7070dbSScott Long 		iflib_txq_setup(txq);
56694c7070dbSScott Long 
56704c7070dbSScott Long 	return (0);
56714c7070dbSScott Long }
56724c7070dbSScott Long 
56734c7070dbSScott Long static void
56744c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx)
56754c7070dbSScott Long {
56764c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
56774d261ce2SStephen Hurd 	if_shared_ctx_t sctx = ctx->ifc_sctx;
56784c7070dbSScott Long 	int i, j;
56794c7070dbSScott Long 
56804c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
56814d261ce2SStephen Hurd 		for (j = 0; j < sctx->isc_ntxqs; j++)
56824c7070dbSScott Long 			iflib_dma_free(&txq->ift_ifdi[j]);
5683244e7cffSEric Joyner 		iflib_txq_destroy(txq);
56844c7070dbSScott Long 	}
56854c7070dbSScott Long 	free(ctx->ifc_txqs, M_IFLIB);
56864c7070dbSScott Long 	ctx->ifc_txqs = NULL;
56874c7070dbSScott Long 	IFDI_QUEUES_FREE(ctx);
56884c7070dbSScott Long }
56894c7070dbSScott Long 
56904c7070dbSScott Long /*********************************************************************
56914c7070dbSScott Long  *
56924c7070dbSScott Long  *  Initialize all receive rings.
56934c7070dbSScott Long  *
56944c7070dbSScott Long  **********************************************************************/
56954c7070dbSScott Long static int
56964c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx)
56974c7070dbSScott Long {
56984c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
5699aaeb188aSBjoern A. Zeeb 	int q;
5700aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
57013d10e9edSMarius Strobl 	int err, i;
5702aaeb188aSBjoern A. Zeeb #endif
57034c7070dbSScott Long 
57044c7070dbSScott Long 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
5705aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
57063d10e9edSMarius Strobl 		if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) {
57073d10e9edSMarius Strobl 			err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
570823ac9029SStephen Hurd 			    TCP_LRO_ENTRIES, min(1024,
57093d10e9edSMarius Strobl 			    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]));
57103d10e9edSMarius Strobl 			if (err != 0) {
57113d10e9edSMarius Strobl 				device_printf(ctx->ifc_dev,
57123d10e9edSMarius Strobl 				    "LRO Initialization failed!\n");
57134c7070dbSScott Long 				goto fail;
57144c7070dbSScott Long 			}
57153d10e9edSMarius Strobl 		}
5716aaeb188aSBjoern A. Zeeb #endif
57174c7070dbSScott Long 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
57184c7070dbSScott Long 	}
57194c7070dbSScott Long 	return (0);
5720aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
57214c7070dbSScott Long fail:
57224c7070dbSScott Long 	/*
57233d10e9edSMarius Strobl 	 * Free LRO resources allocated so far, we will only handle
57244c7070dbSScott Long 	 * the rings that completed, the failing case will have
57254c7070dbSScott Long 	 * cleaned up for itself.  'q' failed, so its the terminus.
57264c7070dbSScott Long 	 */
57274c7070dbSScott Long 	rxq = ctx->ifc_rxqs;
57284c7070dbSScott Long 	for (i = 0; i < q; ++i, rxq++) {
57293d10e9edSMarius Strobl 		if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO)
57303d10e9edSMarius Strobl 			tcp_lro_free(&rxq->ifr_lc);
57314c7070dbSScott Long 	}
57324c7070dbSScott Long 	return (err);
5733aaeb188aSBjoern A. Zeeb #endif
57344c7070dbSScott Long }
57354c7070dbSScott Long 
57364c7070dbSScott Long /*********************************************************************
57374c7070dbSScott Long  *
57384c7070dbSScott Long  *  Free all receive rings.
57394c7070dbSScott Long  *
57404c7070dbSScott Long  **********************************************************************/
57414c7070dbSScott Long static void
57424c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx)
57434c7070dbSScott Long {
57444c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
5745db8e8f1eSEric Joyner 	if_shared_ctx_t sctx = ctx->ifc_sctx;
5746db8e8f1eSEric Joyner 	int i, j;
57474c7070dbSScott Long 
57483d10e9edSMarius Strobl 	for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
5749db8e8f1eSEric Joyner 		for (j = 0; j < sctx->isc_nrxqs; j++)
5750db8e8f1eSEric Joyner 			iflib_dma_free(&rxq->ifr_ifdi[j]);
57514c7070dbSScott Long 		iflib_rx_sds_free(rxq);
5752007b804fSMarius Strobl #if defined(INET6) || defined(INET)
57533d10e9edSMarius Strobl 		if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO)
57543d10e9edSMarius Strobl 			tcp_lro_free(&rxq->ifr_lc);
5755007b804fSMarius Strobl #endif
57564c7070dbSScott Long 	}
575777c1fcecSEric Joyner 	free(ctx->ifc_rxqs, M_IFLIB);
575877c1fcecSEric Joyner 	ctx->ifc_rxqs = NULL;
57594c7070dbSScott Long }
57604c7070dbSScott Long 
57614c7070dbSScott Long static int
57624c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx)
57634c7070dbSScott Long {
57644c7070dbSScott Long 	int err;
57654c7070dbSScott Long 
57666108c013SStephen Hurd 	/*
57676108c013SStephen Hurd 	 * It is expected that the caller takes care of freeing queues if this
57686108c013SStephen Hurd 	 * fails.
57696108c013SStephen Hurd 	 */
5770ac88e6daSStephen Hurd 	if ((err = iflib_tx_structures_setup(ctx)) != 0) {
5771ac88e6daSStephen Hurd 		device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err);
57724c7070dbSScott Long 		return (err);
5773ac88e6daSStephen Hurd 	}
57744c7070dbSScott Long 
57756108c013SStephen Hurd 	if ((err = iflib_rx_structures_setup(ctx)) != 0)
57764c7070dbSScott Long 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
57776108c013SStephen Hurd 
57784c7070dbSScott Long 	return (err);
57794c7070dbSScott Long }
57804c7070dbSScott Long 
57814c7070dbSScott Long int
57824c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
57833e0e6330SStephen Hurd 		driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name)
57844c7070dbSScott Long {
57854c7070dbSScott Long 
57864c7070dbSScott Long 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
57874c7070dbSScott Long }
57884c7070dbSScott Long 
5789b103855eSStephen Hurd #ifdef SMP
5790aa3c5dd8SSean Bruno static int
5791b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid)
57924c7070dbSScott Long {
5793b103855eSStephen Hurd 	cpuset_t cpus;
5794aa3c5dd8SSean Bruno 	int i, cpuid, eqid, count;
57954c7070dbSScott Long 
5796b103855eSStephen Hurd 	CPU_COPY(&ctx->ifc_cpus, &cpus);
5797b103855eSStephen Hurd 	count = CPU_COUNT(&cpus);
5798aa3c5dd8SSean Bruno 	eqid = qid % count;
57994c7070dbSScott Long 	/* clear up to the qid'th bit */
5800aa3c5dd8SSean Bruno 	for (i = 0; i < eqid; i++) {
5801b103855eSStephen Hurd 		cpuid = CPU_FFS(&cpus);
5802aa3c5dd8SSean Bruno 		MPASS(cpuid != 0);
5803b103855eSStephen Hurd 		CPU_CLR(cpuid-1, &cpus);
58044c7070dbSScott Long 	}
5805b103855eSStephen Hurd 	cpuid = CPU_FFS(&cpus);
5806aa3c5dd8SSean Bruno 	MPASS(cpuid != 0);
5807aa3c5dd8SSean Bruno 	return (cpuid-1);
58084c7070dbSScott Long }
58094c7070dbSScott Long 
5810b103855eSStephen Hurd #ifdef SCHED_ULE
5811b103855eSStephen Hurd extern struct cpu_group *cpu_top;              /* CPU topology */
5812b103855eSStephen Hurd 
5813b103855eSStephen Hurd static int
5814b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp)
5815b103855eSStephen Hurd {
5816b103855eSStephen Hurd 	int i;
5817b103855eSStephen Hurd 
5818b103855eSStephen Hurd 	if (grp->cg_children == 0)
5819b103855eSStephen Hurd 		return -1;
5820b103855eSStephen Hurd 
5821b103855eSStephen Hurd 	MPASS(grp->cg_child);
5822b103855eSStephen Hurd 	for (i = 0; i < grp->cg_children; i++) {
5823b103855eSStephen Hurd 		if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask))
5824b103855eSStephen Hurd 			return i;
5825b103855eSStephen Hurd 	}
5826b103855eSStephen Hurd 
5827b103855eSStephen Hurd 	return -1;
5828b103855eSStephen Hurd }
5829b103855eSStephen Hurd 
5830b103855eSStephen Hurd /*
58310b75ac77SStephen Hurd  * Find the nth "close" core to the specified core
58320b75ac77SStephen Hurd  * "close" is defined as the deepest level that shares
58330b75ac77SStephen Hurd  * at least an L2 cache.  With threads, this will be
5834f154ece0SStephen Hurd  * threads on the same core.  If the shared cache is L3
58350b75ac77SStephen Hurd  * or higher, simply returns the same core.
5836b103855eSStephen Hurd  */
5837b103855eSStephen Hurd static int
58380b75ac77SStephen Hurd find_close_core(int cpu, int core_offset)
5839b103855eSStephen Hurd {
5840b103855eSStephen Hurd 	struct cpu_group *grp;
5841b103855eSStephen Hurd 	int i;
58420b75ac77SStephen Hurd 	int fcpu;
5843b103855eSStephen Hurd 	cpuset_t cs;
5844b103855eSStephen Hurd 
5845b103855eSStephen Hurd 	grp = cpu_top;
5846b103855eSStephen Hurd 	if (grp == NULL)
5847b103855eSStephen Hurd 		return cpu;
5848b103855eSStephen Hurd 	i = 0;
5849b103855eSStephen Hurd 	while ((i = find_child_with_core(cpu, grp)) != -1) {
5850b103855eSStephen Hurd 		/* If the child only has one cpu, don't descend */
5851b103855eSStephen Hurd 		if (grp->cg_child[i].cg_count <= 1)
5852b103855eSStephen Hurd 			break;
5853b103855eSStephen Hurd 		grp = &grp->cg_child[i];
5854b103855eSStephen Hurd 	}
5855b103855eSStephen Hurd 
5856b103855eSStephen Hurd 	/* If they don't share at least an L2 cache, use the same CPU */
5857b103855eSStephen Hurd 	if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE)
5858b103855eSStephen Hurd 		return cpu;
5859b103855eSStephen Hurd 
5860b103855eSStephen Hurd 	/* Now pick one */
5861b103855eSStephen Hurd 	CPU_COPY(&grp->cg_mask, &cs);
58620b75ac77SStephen Hurd 
58630b75ac77SStephen Hurd 	/* Add the selected CPU offset to core offset. */
58640b75ac77SStephen Hurd 	for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) {
58650b75ac77SStephen Hurd 		if (fcpu - 1 == cpu)
58660b75ac77SStephen Hurd 			break;
58670b75ac77SStephen Hurd 		CPU_CLR(fcpu - 1, &cs);
58680b75ac77SStephen Hurd 	}
58690b75ac77SStephen Hurd 	MPASS(fcpu);
58700b75ac77SStephen Hurd 
58710b75ac77SStephen Hurd 	core_offset += i;
58720b75ac77SStephen Hurd 
58730b75ac77SStephen Hurd 	CPU_COPY(&grp->cg_mask, &cs);
58740b75ac77SStephen Hurd 	for (i = core_offset % grp->cg_count; i > 0; i--) {
5875b103855eSStephen Hurd 		MPASS(CPU_FFS(&cs));
5876b103855eSStephen Hurd 		CPU_CLR(CPU_FFS(&cs) - 1, &cs);
5877b103855eSStephen Hurd 	}
5878b103855eSStephen Hurd 	MPASS(CPU_FFS(&cs));
5879b103855eSStephen Hurd 	return CPU_FFS(&cs) - 1;
5880b103855eSStephen Hurd }
5881b103855eSStephen Hurd #else
5882b103855eSStephen Hurd static int
58830b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused)
5884b103855eSStephen Hurd {
588597755e83SKonstantin Belousov 	return cpu;
5886b103855eSStephen Hurd }
5887b103855eSStephen Hurd #endif
5888b103855eSStephen Hurd 
5889b103855eSStephen Hurd static int
58900b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid)
5891b103855eSStephen Hurd {
5892b103855eSStephen Hurd 	switch (type) {
5893b103855eSStephen Hurd 	case IFLIB_INTR_TX:
58940b75ac77SStephen Hurd 		/* TX queues get cores which share at least an L2 cache with the corresponding RX queue */
58950b75ac77SStephen Hurd 		/* XXX handle multiple RX threads per core and more than two core per L2 group */
5896b103855eSStephen Hurd 		return qid / CPU_COUNT(&ctx->ifc_cpus) + 1;
5897b103855eSStephen Hurd 	case IFLIB_INTR_RX:
5898b103855eSStephen Hurd 	case IFLIB_INTR_RXTX:
58990b75ac77SStephen Hurd 		/* RX queues get the specified core */
5900b103855eSStephen Hurd 		return qid / CPU_COUNT(&ctx->ifc_cpus);
5901b103855eSStephen Hurd 	default:
5902b103855eSStephen Hurd 		return -1;
5903b103855eSStephen Hurd 	}
5904b103855eSStephen Hurd }
5905b103855eSStephen Hurd #else
59060b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid)	CPU_FIRST()
59070b75ac77SStephen Hurd #define find_close_core(cpuid, tid)	CPU_FIRST()
5908b103855eSStephen Hurd #define find_nth(ctx, gid)		CPU_FIRST()
5909b103855eSStephen Hurd #endif
5910b103855eSStephen Hurd 
5911b103855eSStephen Hurd /* Just to avoid copy/paste */
5912b103855eSStephen Hurd static inline int
5913f855ec81SMarius Strobl iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,
5914f855ec81SMarius Strobl     int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq,
5915f855ec81SMarius Strobl     const char *name)
5916b103855eSStephen Hurd {
5917f855ec81SMarius Strobl 	device_t dev;
5918f154ece0SStephen Hurd 	int co, cpuid, err, tid;
5919b103855eSStephen Hurd 
5920f855ec81SMarius Strobl 	dev = ctx->ifc_dev;
5921f154ece0SStephen Hurd 	co = ctx->ifc_sysctl_core_offset;
5922f154ece0SStephen Hurd 	if (ctx->ifc_sysctl_separate_txrx && type == IFLIB_INTR_TX)
5923f154ece0SStephen Hurd 		co += ctx->ifc_softc_ctx.isc_nrxqsets;
5924f154ece0SStephen Hurd 	cpuid = find_nth(ctx, qid + co);
59250b75ac77SStephen Hurd 	tid = get_core_offset(ctx, type, qid);
59263d10e9edSMarius Strobl 	if (tid < 0) {
59273d10e9edSMarius Strobl 		device_printf(dev, "get_core_offset failed\n");
59283d10e9edSMarius Strobl 		return (EOPNOTSUPP);
59293d10e9edSMarius Strobl 	}
59300b75ac77SStephen Hurd 	cpuid = find_close_core(cpuid, tid);
5931f855ec81SMarius Strobl 	err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, irq->ii_res,
5932f855ec81SMarius Strobl 	    name);
5933b103855eSStephen Hurd 	if (err) {
5934f855ec81SMarius Strobl 		device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err);
5935b103855eSStephen Hurd 		return (err);
5936b103855eSStephen Hurd 	}
5937b103855eSStephen Hurd #ifdef notyet
5938b103855eSStephen Hurd 	if (cpuid > ctx->ifc_cpuid_highest)
5939b103855eSStephen Hurd 		ctx->ifc_cpuid_highest = cpuid;
5940b103855eSStephen Hurd #endif
59413d10e9edSMarius Strobl 	return (0);
5942b103855eSStephen Hurd }
5943b103855eSStephen Hurd 
59444c7070dbSScott Long int
59454c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
59464c7070dbSScott Long 			iflib_intr_type_t type, driver_filter_t *filter,
59473e0e6330SStephen Hurd 			void *filter_arg, int qid, const char *name)
59484c7070dbSScott Long {
5949f855ec81SMarius Strobl 	device_t dev;
59504c7070dbSScott Long 	struct grouptask *gtask;
59514c7070dbSScott Long 	struct taskqgroup *tqg;
59524c7070dbSScott Long 	iflib_filter_info_t info;
595323ac9029SStephen Hurd 	gtask_fn_t *fn;
5954b103855eSStephen Hurd 	int tqrid, err;
595595246abbSSean Bruno 	driver_filter_t *intr_fast;
59564c7070dbSScott Long 	void *q;
59574c7070dbSScott Long 
59584c7070dbSScott Long 	info = &ctx->ifc_filter_info;
5959add6f7d0SSean Bruno 	tqrid = rid;
59604c7070dbSScott Long 
59614c7070dbSScott Long 	switch (type) {
59624c7070dbSScott Long 	/* XXX merge tx/rx for netmap? */
59634c7070dbSScott Long 	case IFLIB_INTR_TX:
59644c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
59654c7070dbSScott Long 		info = &ctx->ifc_txqs[qid].ift_filter_info;
59664c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
5967ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
59684c7070dbSScott Long 		fn = _task_fn_tx;
596995246abbSSean Bruno 		intr_fast = iflib_fast_intr;
5970da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
59715ee36c68SStephen Hurd 		ctx->ifc_flags |= IFC_NETMAP_TX_IRQ;
59724c7070dbSScott Long 		break;
59734c7070dbSScott Long 	case IFLIB_INTR_RX:
59744c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
59754c7070dbSScott Long 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
59764c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5977ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
59784c7070dbSScott Long 		fn = _task_fn_rx;
5979ab2e3f79SStephen Hurd 		intr_fast = iflib_fast_intr;
59806c3e93cbSGleb Smirnoff 		NET_GROUPTASK_INIT(gtask, 0, fn, q);
598195246abbSSean Bruno 		break;
598295246abbSSean Bruno 	case IFLIB_INTR_RXTX:
598395246abbSSean Bruno 		q = &ctx->ifc_rxqs[qid];
598495246abbSSean Bruno 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
598595246abbSSean Bruno 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5986ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
598795246abbSSean Bruno 		fn = _task_fn_rx;
598895246abbSSean Bruno 		intr_fast = iflib_fast_intr_rxtx;
59896c3e93cbSGleb Smirnoff 		NET_GROUPTASK_INIT(gtask, 0, fn, q);
59904c7070dbSScott Long 		break;
59914c7070dbSScott Long 	case IFLIB_INTR_ADMIN:
59924c7070dbSScott Long 		q = ctx;
5993da69b8f9SSean Bruno 		tqrid = -1;
59944c7070dbSScott Long 		info = &ctx->ifc_filter_info;
59954c7070dbSScott Long 		gtask = &ctx->ifc_admin_task;
5996ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
59974c7070dbSScott Long 		fn = _task_fn_admin;
599895246abbSSean Bruno 		intr_fast = iflib_fast_intr_ctx;
59994c7070dbSScott Long 		break;
60004c7070dbSScott Long 	default:
60013d10e9edSMarius Strobl 		device_printf(ctx->ifc_dev, "%s: unknown net intr type\n",
60023d10e9edSMarius Strobl 		    __func__);
60033d10e9edSMarius Strobl 		return (EINVAL);
60044c7070dbSScott Long 	}
60054c7070dbSScott Long 
60064c7070dbSScott Long 	info->ifi_filter = filter;
60074c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
60084c7070dbSScott Long 	info->ifi_task = gtask;
600995246abbSSean Bruno 	info->ifi_ctx = q;
60104c7070dbSScott Long 
6011f855ec81SMarius Strobl 	dev = ctx->ifc_dev;
601295246abbSSean Bruno 	err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info,  name);
6013da69b8f9SSean Bruno 	if (err != 0) {
6014f855ec81SMarius Strobl 		device_printf(dev, "_iflib_irq_alloc failed %d\n", err);
60154c7070dbSScott Long 		return (err);
6016da69b8f9SSean Bruno 	}
6017da69b8f9SSean Bruno 	if (type == IFLIB_INTR_ADMIN)
6018da69b8f9SSean Bruno 		return (0);
6019da69b8f9SSean Bruno 
60204c7070dbSScott Long 	if (tqrid != -1) {
6021f855ec81SMarius Strobl 		err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg,
6022f855ec81SMarius Strobl 		    q, name);
6023b103855eSStephen Hurd 		if (err)
6024b103855eSStephen Hurd 			return (err);
6025aa3c5dd8SSean Bruno 	} else {
6026f855ec81SMarius Strobl 		taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name);
6027aa3c5dd8SSean Bruno 	}
60284c7070dbSScott Long 
60294c7070dbSScott Long 	return (0);
60304c7070dbSScott Long }
60314c7070dbSScott Long 
60324c7070dbSScott Long void
60333e0e6330SStephen Hurd iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, void *arg, int qid, const char *name)
60344c7070dbSScott Long {
60354c7070dbSScott Long 	struct grouptask *gtask;
60364c7070dbSScott Long 	struct taskqgroup *tqg;
603723ac9029SStephen Hurd 	gtask_fn_t *fn;
60384c7070dbSScott Long 	void *q;
6039b103855eSStephen Hurd 	int err;
60404c7070dbSScott Long 
60414c7070dbSScott Long 	switch (type) {
60424c7070dbSScott Long 	case IFLIB_INTR_TX:
60434c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
60444c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
6045ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
60464c7070dbSScott Long 		fn = _task_fn_tx;
6047f98977b5SHans Petter Selasky 		GROUPTASK_INIT(gtask, 0, fn, q);
60484c7070dbSScott Long 		break;
60494c7070dbSScott Long 	case IFLIB_INTR_RX:
60504c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
60514c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
6052ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
60534c7070dbSScott Long 		fn = _task_fn_rx;
6054f98977b5SHans Petter Selasky 		NET_GROUPTASK_INIT(gtask, 0, fn, q);
60554c7070dbSScott Long 		break;
60564c7070dbSScott Long 	case IFLIB_INTR_IOV:
60574c7070dbSScott Long 		q = ctx;
60584c7070dbSScott Long 		gtask = &ctx->ifc_vflr_task;
6059ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
60604c7070dbSScott Long 		fn = _task_fn_iov;
6061f98977b5SHans Petter Selasky 		GROUPTASK_INIT(gtask, 0, fn, q);
60624c7070dbSScott Long 		break;
60634c7070dbSScott Long 	default:
60644c7070dbSScott Long 		panic("unknown net intr type");
60654c7070dbSScott Long 	}
6066f855ec81SMarius Strobl 	if (irq != NULL) {
6067f855ec81SMarius Strobl 		err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg,
6068f855ec81SMarius Strobl 		    q, name);
6069b103855eSStephen Hurd 		if (err)
6070f855ec81SMarius Strobl 			taskqgroup_attach(tqg, gtask, q, ctx->ifc_dev,
6071f855ec81SMarius Strobl 			    irq->ii_res, name);
6072f855ec81SMarius Strobl 	} else {
6073f855ec81SMarius Strobl 		taskqgroup_attach(tqg, gtask, q, NULL, NULL, name);
6074b103855eSStephen Hurd 	}
6075b103855eSStephen Hurd }
60764c7070dbSScott Long 
60774c7070dbSScott Long void
60784c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
60794c7070dbSScott Long {
6080b97de13aSMarius Strobl 
60814c7070dbSScott Long 	if (irq->ii_tag)
60824c7070dbSScott Long 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
60834c7070dbSScott Long 
60844c7070dbSScott Long 	if (irq->ii_res)
6085b97de13aSMarius Strobl 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ,
6086b97de13aSMarius Strobl 		    rman_get_rid(irq->ii_res), irq->ii_res);
60874c7070dbSScott Long }
60884c7070dbSScott Long 
60894c7070dbSScott Long static int
60903e0e6330SStephen Hurd iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name)
60914c7070dbSScott Long {
60924c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
60934c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
60944c7070dbSScott Long 	if_irq_t irq = &ctx->ifc_legacy_irq;
60954c7070dbSScott Long 	iflib_filter_info_t info;
6096f855ec81SMarius Strobl 	device_t dev;
60974c7070dbSScott Long 	struct grouptask *gtask;
6098f855ec81SMarius Strobl 	struct resource *res;
60994c7070dbSScott Long 	struct taskqgroup *tqg;
61004c7070dbSScott Long 	void *q;
6101d49e83eaSMarius Strobl 	int err, tqrid;
610241669133SMark Johnston 	bool rx_only;
61034c7070dbSScott Long 
61044c7070dbSScott Long 	q = &ctx->ifc_rxqs[0];
61054c7070dbSScott Long 	info = &rxq[0].ifr_filter_info;
61064c7070dbSScott Long 	gtask = &rxq[0].ifr_task;
6107ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
6108d49e83eaSMarius Strobl 	tqrid = *rid;
610941669133SMark Johnston 	rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0;
61104c7070dbSScott Long 
61114c7070dbSScott Long 	ctx->ifc_flags |= IFC_LEGACY;
61124c7070dbSScott Long 	info->ifi_filter = filter;
61134c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
61144c7070dbSScott Long 	info->ifi_task = gtask;
611541669133SMark Johnston 	info->ifi_ctx = rx_only ? ctx : q;
61164c7070dbSScott Long 
6117f855ec81SMarius Strobl 	dev = ctx->ifc_dev;
61184c7070dbSScott Long 	/* We allocate a single interrupt resource */
611941669133SMark Johnston 	err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr_ctx :
612041669133SMark Johnston 	    iflib_fast_intr_rxtx, NULL, info, name);
612141669133SMark Johnston 	if (err != 0)
61224c7070dbSScott Long 		return (err);
6123f98977b5SHans Petter Selasky 	NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, q);
6124f855ec81SMarius Strobl 	res = irq->ii_res;
6125f855ec81SMarius Strobl 	taskqgroup_attach(tqg, gtask, q, dev, res, name);
61264c7070dbSScott Long 
61274c7070dbSScott Long 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
6128f855ec81SMarius Strobl 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res,
6129f855ec81SMarius Strobl 	    "tx");
61304c7070dbSScott Long 	return (0);
61314c7070dbSScott Long }
61324c7070dbSScott Long 
61334c7070dbSScott Long void
61344c7070dbSScott Long iflib_led_create(if_ctx_t ctx)
61354c7070dbSScott Long {
61364c7070dbSScott Long 
61374c7070dbSScott Long 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
61384c7070dbSScott Long 	    device_get_nameunit(ctx->ifc_dev));
61394c7070dbSScott Long }
61404c7070dbSScott Long 
61414c7070dbSScott Long void
61424c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
61434c7070dbSScott Long {
61444c7070dbSScott Long 
61454c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
61464c7070dbSScott Long }
61474c7070dbSScott Long 
61484c7070dbSScott Long void
61494c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
61504c7070dbSScott Long {
61514c7070dbSScott Long 
61524c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
61534c7070dbSScott Long }
61544c7070dbSScott Long 
61554c7070dbSScott Long void
61564c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx)
61574c7070dbSScott Long {
615846fa0c25SEric Joyner 
6159ed6611ccSEd Maste 	MPASS(ctx->ifc_admin_task.gt_taskqueue != NULL);
61604c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
61614c7070dbSScott Long }
61624c7070dbSScott Long 
61634c7070dbSScott Long void
61644c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx)
61654c7070dbSScott Long {
61664c7070dbSScott Long 
61674c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
61684c7070dbSScott Long }
61694c7070dbSScott Long 
61704c7070dbSScott Long void
6171d49e83eaSMarius Strobl iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name)
61724c7070dbSScott Long {
61734c7070dbSScott Long 
6174f855ec81SMarius Strobl 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL,
6175f855ec81SMarius Strobl 	    name);
61764c7070dbSScott Long }
61774c7070dbSScott Long 
61784c7070dbSScott Long void
6179aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn,
6180aa8a24d3SStephen Hurd 	const char *name)
61814c7070dbSScott Long {
61824c7070dbSScott Long 
61834c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, ctx);
6184f855ec81SMarius Strobl 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL,
6185f855ec81SMarius Strobl 	    name);
61864c7070dbSScott Long }
61874c7070dbSScott Long 
61884c7070dbSScott Long void
618923ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask)
619023ac9029SStephen Hurd {
619123ac9029SStephen Hurd 
6192ab2e3f79SStephen Hurd 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
619323ac9029SStephen Hurd }
619423ac9029SStephen Hurd 
619523ac9029SStephen Hurd void
619623ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
61974c7070dbSScott Long {
61984c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
61994c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
62004c7070dbSScott Long 
62014c7070dbSScott Long 	if_setbaudrate(ifp, baudrate);
62027b610b60SSean Bruno 	if (baudrate >= IF_Gbps(10)) {
62037b610b60SSean Bruno 		STATE_LOCK(ctx);
620495246abbSSean Bruno 		ctx->ifc_flags |= IFC_PREFETCH;
62057b610b60SSean Bruno 		STATE_UNLOCK(ctx);
62067b610b60SSean Bruno 	}
62074c7070dbSScott Long 	/* If link down, disable watchdog */
62084c7070dbSScott Long 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
62094c7070dbSScott Long 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
62104c7070dbSScott Long 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
62114c7070dbSScott Long 	}
62124c7070dbSScott Long 	ctx->ifc_link_state = link_state;
62134c7070dbSScott Long 	if_link_state_change(ifp, link_state);
62144c7070dbSScott Long }
62154c7070dbSScott Long 
62164c7070dbSScott Long static int
62174c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
62184c7070dbSScott Long {
62194c7070dbSScott Long 	int credits;
62201248952aSSean Bruno #ifdef INVARIANTS
62211248952aSSean Bruno 	int credits_pre = txq->ift_cidx_processed;
62221248952aSSean Bruno #endif
62234c7070dbSScott Long 
62248a04b53dSKonstantin Belousov 	bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
62258a04b53dSKonstantin Belousov 	    BUS_DMASYNC_POSTREAD);
622695246abbSSean Bruno 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
62274c7070dbSScott Long 		return (0);
62284c7070dbSScott Long 
62294c7070dbSScott Long 	txq->ift_processed += credits;
62304c7070dbSScott Long 	txq->ift_cidx_processed += credits;
62314c7070dbSScott Long 
62321248952aSSean Bruno 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
62334c7070dbSScott Long 	if (txq->ift_cidx_processed >= txq->ift_size)
62344c7070dbSScott Long 		txq->ift_cidx_processed -= txq->ift_size;
62354c7070dbSScott Long 	return (credits);
62364c7070dbSScott Long }
62374c7070dbSScott Long 
62384c7070dbSScott Long static int
623995246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
62404c7070dbSScott Long {
624195dcf343SMarius Strobl 	iflib_fl_t fl;
624295dcf343SMarius Strobl 	u_int i;
62434c7070dbSScott Long 
624495dcf343SMarius Strobl 	for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++)
624595dcf343SMarius Strobl 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
624695dcf343SMarius Strobl 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
624723ac9029SStephen Hurd 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
624823ac9029SStephen Hurd 	    budget));
62494c7070dbSScott Long }
62504c7070dbSScott Long 
62514c7070dbSScott Long void
62524c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
62534c7070dbSScott Long 	const char *description, if_int_delay_info_t info,
62544c7070dbSScott Long 	int offset, int value)
62554c7070dbSScott Long {
62564c7070dbSScott Long 	info->iidi_ctx = ctx;
62574c7070dbSScott Long 	info->iidi_offset = offset;
62584c7070dbSScott Long 	info->iidi_value = value;
62594c7070dbSScott Long 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
62604c7070dbSScott Long 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
62617029da5cSPawel Biernacki 	    OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
62624c7070dbSScott Long 	    info, 0, iflib_sysctl_int_delay, "I", description);
62634c7070dbSScott Long }
62644c7070dbSScott Long 
6265aa8a24d3SStephen Hurd struct sx *
62664c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx)
62674c7070dbSScott Long {
62684c7070dbSScott Long 
6269aa8a24d3SStephen Hurd 	return (&ctx->ifc_ctx_sx);
62704c7070dbSScott Long }
62714c7070dbSScott Long 
62724c7070dbSScott Long static int
62734c7070dbSScott Long iflib_msix_init(if_ctx_t ctx)
62744c7070dbSScott Long {
62754c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
62764c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
62774c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
62783d10e9edSMarius Strobl 	int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues;
62793d10e9edSMarius Strobl 	int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors;
62804c7070dbSScott Long 
6281d2735264SStephen Hurd 	iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs;
6282d2735264SStephen Hurd 	iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs;
628323ac9029SStephen Hurd 
6284b97de13aSMarius Strobl 	if (bootverbose)
6285b97de13aSMarius Strobl 		device_printf(dev, "msix_init qsets capped at %d\n",
6286b97de13aSMarius Strobl 		    imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets));
62871248952aSSean Bruno 
62884c7070dbSScott Long 	/* Override by tuneable */
6289ea351d3fSSean Bruno 	if (scctx->isc_disable_msix)
62904c7070dbSScott Long 		goto msi;
62914c7070dbSScott Long 
6292b97de13aSMarius Strobl 	/* First try MSI-X */
6293b97de13aSMarius Strobl 	if ((msgs = pci_msix_count(dev)) == 0) {
6294b97de13aSMarius Strobl 		if (bootverbose)
6295b97de13aSMarius Strobl 			device_printf(dev, "MSI-X not supported or disabled\n");
6296b97de13aSMarius Strobl 		goto msi;
6297b97de13aSMarius Strobl 	}
62983d10e9edSMarius Strobl 
62993d10e9edSMarius Strobl 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
63004c7070dbSScott Long 	/*
63014c7070dbSScott Long 	 * bar == -1 => "trust me I know what I'm doing"
63024c7070dbSScott Long 	 * Some drivers are for hardware that is so shoddily
63034c7070dbSScott Long 	 * documented that no one knows which bars are which
63044c7070dbSScott Long 	 * so the developer has to map all bars. This hack
6305b97de13aSMarius Strobl 	 * allows shoddy garbage to use MSI-X in this framework.
63064c7070dbSScott Long 	 */
63074c7070dbSScott Long 	if (bar != -1) {
63084c7070dbSScott Long 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
63094c7070dbSScott Long 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
63104c7070dbSScott Long 		if (ctx->ifc_msix_mem == NULL) {
6311b97de13aSMarius Strobl 			device_printf(dev, "Unable to map MSI-X table\n");
63124c7070dbSScott Long 			goto msi;
63134c7070dbSScott Long 		}
63144c7070dbSScott Long 	}
63153d10e9edSMarius Strobl 
63163d10e9edSMarius Strobl 	admincnt = sctx->isc_admin_intrcnt;
63174c7070dbSScott Long #if IFLIB_DEBUG
63184c7070dbSScott Long 	/* use only 1 qset in debug mode */
63194c7070dbSScott Long 	queuemsgs = min(msgs - admincnt, 1);
63204c7070dbSScott Long #else
63214c7070dbSScott Long 	queuemsgs = msgs - admincnt;
63224c7070dbSScott Long #endif
63234c7070dbSScott Long #ifdef RSS
63244c7070dbSScott Long 	queues = imin(queuemsgs, rss_getnumbuckets());
63254c7070dbSScott Long #else
63264c7070dbSScott Long 	queues = queuemsgs;
63274c7070dbSScott Long #endif
63284c7070dbSScott Long 	queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
6329b97de13aSMarius Strobl 	if (bootverbose)
6330b97de13aSMarius Strobl 		device_printf(dev,
6331b97de13aSMarius Strobl 		    "intr CPUs: %d queue msgs: %d admincnt: %d\n",
63324c7070dbSScott Long 		    CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
63334c7070dbSScott Long #ifdef  RSS
63344c7070dbSScott Long 	/* If we're doing RSS, clamp at the number of RSS buckets */
63354c7070dbSScott Long 	if (queues > rss_getnumbuckets())
63364c7070dbSScott Long 		queues = rss_getnumbuckets();
63374c7070dbSScott Long #endif
633823ac9029SStephen Hurd 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
633923ac9029SStephen Hurd 		rx_queues = iflib_num_rx_queues;
63404c7070dbSScott Long 	else
63414c7070dbSScott Long 		rx_queues = queues;
6342d2735264SStephen Hurd 
6343d2735264SStephen Hurd 	if (rx_queues > scctx->isc_nrxqsets)
6344d2735264SStephen Hurd 		rx_queues = scctx->isc_nrxqsets;
6345d2735264SStephen Hurd 
634623ac9029SStephen Hurd 	/*
634723ac9029SStephen Hurd 	 * We want this to be all logical CPUs by default
634823ac9029SStephen Hurd 	 */
63494c7070dbSScott Long 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
63504c7070dbSScott Long 		tx_queues = iflib_num_tx_queues;
63514c7070dbSScott Long 	else
635223ac9029SStephen Hurd 		tx_queues = mp_ncpus;
635323ac9029SStephen Hurd 
6354d2735264SStephen Hurd 	if (tx_queues > scctx->isc_ntxqsets)
6355d2735264SStephen Hurd 		tx_queues = scctx->isc_ntxqsets;
6356d2735264SStephen Hurd 
635723ac9029SStephen Hurd 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
635823ac9029SStephen Hurd #ifdef INVARIANTS
635923ac9029SStephen Hurd 		if (tx_queues != rx_queues)
636077c1fcecSEric Joyner 			device_printf(dev,
636177c1fcecSEric Joyner 			    "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
636223ac9029SStephen Hurd 			    min(rx_queues, tx_queues), min(rx_queues, tx_queues));
636323ac9029SStephen Hurd #endif
636423ac9029SStephen Hurd 		tx_queues = min(rx_queues, tx_queues);
636523ac9029SStephen Hurd 		rx_queues = min(rx_queues, tx_queues);
636623ac9029SStephen Hurd 	}
63674c7070dbSScott Long 
63683d10e9edSMarius Strobl 	vectors = rx_queues + admincnt;
63693d10e9edSMarius Strobl 	if (msgs < vectors) {
63703d10e9edSMarius Strobl 		device_printf(dev,
63713d10e9edSMarius Strobl 		    "insufficient number of MSI-X vectors "
63723d10e9edSMarius Strobl 		    "(supported %d, need %d)\n", msgs, vectors);
63733d10e9edSMarius Strobl 		goto msi;
63743d10e9edSMarius Strobl 	}
63753d10e9edSMarius Strobl 
63761722eeacSMarius Strobl 	device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues,
63771722eeacSMarius Strobl 	    tx_queues);
63783d10e9edSMarius Strobl 	msgs = vectors;
63794c7070dbSScott Long 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
63803d10e9edSMarius Strobl 		if (vectors != msgs) {
63813d10e9edSMarius Strobl 			device_printf(dev,
63823d10e9edSMarius Strobl 			    "Unable to allocate sufficient MSI-X vectors "
63833d10e9edSMarius Strobl 			    "(got %d, need %d)\n", vectors, msgs);
63843d10e9edSMarius Strobl 			pci_release_msi(dev);
63853d10e9edSMarius Strobl 			if (bar != -1) {
63863d10e9edSMarius Strobl 				bus_release_resource(dev, SYS_RES_MEMORY, bar,
63873d10e9edSMarius Strobl 				    ctx->ifc_msix_mem);
63883d10e9edSMarius Strobl 				ctx->ifc_msix_mem = NULL;
63893d10e9edSMarius Strobl 			}
63903d10e9edSMarius Strobl 			goto msi;
63913d10e9edSMarius Strobl 		}
6392b97de13aSMarius Strobl 		device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
6393b97de13aSMarius Strobl 		    vectors);
63944c7070dbSScott Long 		scctx->isc_vectors = vectors;
63954c7070dbSScott Long 		scctx->isc_nrxqsets = rx_queues;
63964c7070dbSScott Long 		scctx->isc_ntxqsets = tx_queues;
63974c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSIX;
639823ac9029SStephen Hurd 
63994c7070dbSScott Long 		return (vectors);
64004c7070dbSScott Long 	} else {
640177c1fcecSEric Joyner 		device_printf(dev,
64023d10e9edSMarius Strobl 		    "failed to allocate %d MSI-X vectors, err: %d\n", vectors,
64033d10e9edSMarius Strobl 		    err);
64043d10e9edSMarius Strobl 		if (bar != -1) {
6405e4defe55SMarius Strobl 			bus_release_resource(dev, SYS_RES_MEMORY, bar,
6406e4defe55SMarius Strobl 			    ctx->ifc_msix_mem);
6407e4defe55SMarius Strobl 			ctx->ifc_msix_mem = NULL;
64084c7070dbSScott Long 		}
64093d10e9edSMarius Strobl 	}
64103d10e9edSMarius Strobl 
64114c7070dbSScott Long msi:
64124c7070dbSScott Long 	vectors = pci_msi_count(dev);
64134c7070dbSScott Long 	scctx->isc_nrxqsets = 1;
64144c7070dbSScott Long 	scctx->isc_ntxqsets = 1;
64154c7070dbSScott Long 	scctx->isc_vectors = vectors;
64164c7070dbSScott Long 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
64174c7070dbSScott Long 		device_printf(dev,"Using an MSI interrupt\n");
64184c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSI;
64194c7070dbSScott Long 	} else {
6420e4defe55SMarius Strobl 		scctx->isc_vectors = 1;
64214c7070dbSScott Long 		device_printf(dev,"Using a Legacy interrupt\n");
64224c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
64234c7070dbSScott Long 	}
64244c7070dbSScott Long 
64254c7070dbSScott Long 	return (vectors);
64264c7070dbSScott Long }
64274c7070dbSScott Long 
6428e4defe55SMarius Strobl static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
64294c7070dbSScott Long 
64304c7070dbSScott Long static int
64314c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
64324c7070dbSScott Long {
64334c7070dbSScott Long 	int rc;
64344c7070dbSScott Long 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
64354c7070dbSScott Long 	struct sbuf *sb;
6436e4defe55SMarius Strobl 	const char *ring_state = "UNKNOWN";
64374c7070dbSScott Long 
64384c7070dbSScott Long 	/* XXX needed ? */
64394c7070dbSScott Long 	rc = sysctl_wire_old_buffer(req, 0);
64404c7070dbSScott Long 	MPASS(rc == 0);
64414c7070dbSScott Long 	if (rc != 0)
64424c7070dbSScott Long 		return (rc);
64434c7070dbSScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
64444c7070dbSScott Long 	MPASS(sb != NULL);
64454c7070dbSScott Long 	if (sb == NULL)
64464c7070dbSScott Long 		return (ENOMEM);
64474c7070dbSScott Long 	if (state[3] <= 3)
64484c7070dbSScott Long 		ring_state = ring_states[state[3]];
64494c7070dbSScott Long 
64504c7070dbSScott Long 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
64514c7070dbSScott Long 		    state[0], state[1], state[2], ring_state);
64524c7070dbSScott Long 	rc = sbuf_finish(sb);
64534c7070dbSScott Long 	sbuf_delete(sb);
64544c7070dbSScott Long         return(rc);
64554c7070dbSScott Long }
64564c7070dbSScott Long 
645723ac9029SStephen Hurd enum iflib_ndesc_handler {
645823ac9029SStephen Hurd 	IFLIB_NTXD_HANDLER,
645923ac9029SStephen Hurd 	IFLIB_NRXD_HANDLER,
646023ac9029SStephen Hurd };
64614c7070dbSScott Long 
646223ac9029SStephen Hurd static int
646323ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
646423ac9029SStephen Hurd {
646523ac9029SStephen Hurd 	if_ctx_t ctx = (void *)arg1;
646623ac9029SStephen Hurd 	enum iflib_ndesc_handler type = arg2;
646723ac9029SStephen Hurd 	char buf[256] = {0};
646895246abbSSean Bruno 	qidx_t *ndesc;
646923ac9029SStephen Hurd 	char *p, *next;
647023ac9029SStephen Hurd 	int nqs, rc, i;
647123ac9029SStephen Hurd 
647223ac9029SStephen Hurd 	nqs = 8;
647323ac9029SStephen Hurd 	switch(type) {
647423ac9029SStephen Hurd 	case IFLIB_NTXD_HANDLER:
647523ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_ntxds;
647623ac9029SStephen Hurd 		if (ctx->ifc_sctx)
647723ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_ntxqs;
647823ac9029SStephen Hurd 		break;
647923ac9029SStephen Hurd 	case IFLIB_NRXD_HANDLER:
648023ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_nrxds;
648123ac9029SStephen Hurd 		if (ctx->ifc_sctx)
648223ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_nrxqs;
648323ac9029SStephen Hurd 		break;
64841ae4848cSMatt Macy 	default:
64853d10e9edSMarius Strobl 		printf("%s: unhandled type\n", __func__);
64863d10e9edSMarius Strobl 		return (EINVAL);
648723ac9029SStephen Hurd 	}
648823ac9029SStephen Hurd 	if (nqs == 0)
648923ac9029SStephen Hurd 		nqs = 8;
649023ac9029SStephen Hurd 
649123ac9029SStephen Hurd 	for (i=0; i<8; i++) {
649223ac9029SStephen Hurd 		if (i >= nqs)
649323ac9029SStephen Hurd 			break;
649423ac9029SStephen Hurd 		if (i)
649523ac9029SStephen Hurd 			strcat(buf, ",");
649623ac9029SStephen Hurd 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
649723ac9029SStephen Hurd 	}
649823ac9029SStephen Hurd 
649923ac9029SStephen Hurd 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
650023ac9029SStephen Hurd 	if (rc || req->newptr == NULL)
650123ac9029SStephen Hurd 		return rc;
650223ac9029SStephen Hurd 
650323ac9029SStephen Hurd 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
650423ac9029SStephen Hurd 	    i++, p = strsep(&next, " ,")) {
650523ac9029SStephen Hurd 		ndesc[i] = strtoul(p, NULL, 10);
650623ac9029SStephen Hurd 	}
650723ac9029SStephen Hurd 
650823ac9029SStephen Hurd 	return(rc);
650923ac9029SStephen Hurd }
65104c7070dbSScott Long 
65114c7070dbSScott Long #define NAME_BUFLEN 32
65124c7070dbSScott Long static void
65134c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx)
65144c7070dbSScott Long {
65154c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
65164c7070dbSScott Long 	struct sysctl_oid_list *child, *oid_list;
65174c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
65184c7070dbSScott Long 	struct sysctl_oid *node;
65194c7070dbSScott Long 
65204c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
65214c7070dbSScott Long 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
65224c7070dbSScott Long 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
65237029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IFLIB fields");
65244c7070dbSScott Long 	oid_list = SYSCTL_CHILDREN(node);
65254c7070dbSScott Long 
652610a1e981SEric Joyner 	SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
652710a1e981SEric Joyner 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version,
652823ac9029SStephen Hurd 		       "driver version");
652923ac9029SStephen Hurd 
65304c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
65314c7070dbSScott Long 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
65324c7070dbSScott Long 			"# of txqs to use, 0 => use default #");
65334c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
653423ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
653523ac9029SStephen Hurd 			"# of rxqs to use, 0 => use default #");
653623ac9029SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
653723ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
653823ac9029SStephen Hurd                        "permit #txq != #rxq");
6539ea351d3fSSean Bruno 	SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix",
6540ea351d3fSSean Bruno                       CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
6541b97de13aSMarius Strobl                       "disable MSI-X (default 0)");
6542f4d2154eSStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget",
6543f4d2154eSStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0,
65441722eeacSMarius Strobl 		       "set the RX budget");
6545fe51d4cdSStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate",
6546fe51d4cdSStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0,
65471722eeacSMarius Strobl 		       "cause TX to abdicate instead of running to completion");
6548f154ece0SStephen Hurd 	ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED;
6549f154ece0SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "core_offset",
6550f154ece0SStephen Hurd 		       CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0,
6551f154ece0SStephen Hurd 		       "offset to start using cores at");
6552f154ece0SStephen Hurd 	SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "separate_txrx",
6553f154ece0SStephen Hurd 		       CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0,
6554f154ece0SStephen Hurd 		       "use separate cores for TX and RX");
65554c7070dbSScott Long 
655623ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
655723ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
65587029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
65597029da5cSPawel Biernacki 	    IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A",
65601722eeacSMarius Strobl 	    "list of # of TX descriptors to use, 0 = use default #");
656123ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
65627029da5cSPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
65637029da5cSPawel Biernacki 	    IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A",
65641722eeacSMarius Strobl 	    "list of # of RX descriptors to use, 0 = use default #");
65654c7070dbSScott Long }
65664c7070dbSScott Long 
65674c7070dbSScott Long static void
65684c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx)
65694c7070dbSScott Long {
65704c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
65714c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
65724c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
65734c7070dbSScott Long 	struct sysctl_oid_list *child;
65744c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
65754c7070dbSScott Long 	iflib_fl_t fl;
65764c7070dbSScott Long 	iflib_txq_t txq;
65774c7070dbSScott Long 	iflib_rxq_t rxq;
65784c7070dbSScott Long 	int i, j;
65794c7070dbSScott Long 	char namebuf[NAME_BUFLEN];
65804c7070dbSScott Long 	char *qfmt;
65814c7070dbSScott Long 	struct sysctl_oid *queue_node, *fl_node, *node;
65824c7070dbSScott Long 	struct sysctl_oid_list *queue_list, *fl_list;
65834c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
65844c7070dbSScott Long 
65854c7070dbSScott Long 	node = ctx->ifc_sysctl_node;
65864c7070dbSScott Long 	child = SYSCTL_CHILDREN(node);
65874c7070dbSScott Long 
65884c7070dbSScott Long 	if (scctx->isc_ntxqsets > 100)
65894c7070dbSScott Long 		qfmt = "txq%03d";
65904c7070dbSScott Long 	else if (scctx->isc_ntxqsets > 10)
65914c7070dbSScott Long 		qfmt = "txq%02d";
65924c7070dbSScott Long 	else
65934c7070dbSScott Long 		qfmt = "txq%d";
65944c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
65954c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
65964c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
65977029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
65984c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
65994c7070dbSScott Long #if MEMORY_LOGGING
66004c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
66014c7070dbSScott Long 				CTLFLAG_RD,
66024c7070dbSScott Long 				&txq->ift_dequeued, "total mbufs freed");
66034c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
66044c7070dbSScott Long 				CTLFLAG_RD,
66054c7070dbSScott Long 				&txq->ift_enqueued, "total mbufs enqueued");
66064c7070dbSScott Long #endif
66074c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
66084c7070dbSScott Long 				   CTLFLAG_RD,
66094c7070dbSScott Long 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
66104c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
66114c7070dbSScott Long 				   CTLFLAG_RD,
66124c7070dbSScott Long 				   &txq->ift_pullups, "# of times m_pullup was called");
66134c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
66144c7070dbSScott Long 				   CTLFLAG_RD,
66154c7070dbSScott Long 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
66164c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
66174c7070dbSScott Long 				   CTLFLAG_RD,
661823ac9029SStephen Hurd 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
66194c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
66204c7070dbSScott Long 				   CTLFLAG_RD,
66211722eeacSMarius Strobl 				   &txq->ift_map_failed, "# of times DMA map failed");
66224c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
66234c7070dbSScott Long 				   CTLFLAG_RD,
66244c7070dbSScott Long 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
66254c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
66264c7070dbSScott Long 				   CTLFLAG_RD,
66274c7070dbSScott Long 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
66284c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
66294c7070dbSScott Long 				   CTLFLAG_RD,
66304c7070dbSScott Long 				   &txq->ift_pidx, 1, "Producer Index");
66314c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
66324c7070dbSScott Long 				   CTLFLAG_RD,
66334c7070dbSScott Long 				   &txq->ift_cidx, 1, "Consumer Index");
66344c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
66354c7070dbSScott Long 				   CTLFLAG_RD,
66364c7070dbSScott Long 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
66374c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
66384c7070dbSScott Long 				   CTLFLAG_RD,
66394c7070dbSScott Long 				   &txq->ift_in_use, 1, "descriptors in use");
66404c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
66414c7070dbSScott Long 				   CTLFLAG_RD,
66424c7070dbSScott Long 				   &txq->ift_processed, "descriptors procesed for clean");
66434c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
66444c7070dbSScott Long 				   CTLFLAG_RD,
66454c7070dbSScott Long 				   &txq->ift_cleaned, "total cleaned");
66464c7070dbSScott Long 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
66477029da5cSPawel Biernacki 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
66487029da5cSPawel Biernacki 		    __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0,
66497029da5cSPawel Biernacki 		    mp_ring_state_handler, "A", "soft ring state");
66504c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
665195246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->enqueues,
66524c7070dbSScott Long 				       "# of enqueues to the mp_ring for this queue");
66534c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
665495246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->drops,
66554c7070dbSScott Long 				       "# of drops in the mp_ring for this queue");
66564c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
665795246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->starts,
66584c7070dbSScott Long 				       "# of normal consumer starts in the mp_ring for this queue");
66594c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
666095246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->stalls,
66614c7070dbSScott Long 					       "# of consumer stalls in the mp_ring for this queue");
66624c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
666395246abbSSean Bruno 			       CTLFLAG_RD, &txq->ift_br->restarts,
66644c7070dbSScott Long 				       "# of consumer restarts in the mp_ring for this queue");
66654c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
666695246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->abdications,
66674c7070dbSScott Long 				       "# of consumer abdications in the mp_ring for this queue");
66684c7070dbSScott Long 	}
66694c7070dbSScott Long 
66704c7070dbSScott Long 	if (scctx->isc_nrxqsets > 100)
66714c7070dbSScott Long 		qfmt = "rxq%03d";
66724c7070dbSScott Long 	else if (scctx->isc_nrxqsets > 10)
66734c7070dbSScott Long 		qfmt = "rxq%02d";
66744c7070dbSScott Long 	else
66754c7070dbSScott Long 		qfmt = "rxq%d";
66764c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
66774c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
66784c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
66797029da5cSPawel Biernacki 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
66804c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
668123ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
66824c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
66834c7070dbSScott Long 				       CTLFLAG_RD,
66844c7070dbSScott Long 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
66854c7070dbSScott Long 		}
6686da69b8f9SSean Bruno 
66874c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
66884c7070dbSScott Long 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
66894c7070dbSScott Long 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
66907029da5cSPawel Biernacki 			    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "freelist Name");
66914c7070dbSScott Long 			fl_list = SYSCTL_CHILDREN(fl_node);
66924c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
66934c7070dbSScott Long 				       CTLFLAG_RD,
66944c7070dbSScott Long 				       &fl->ifl_pidx, 1, "Producer Index");
66954c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
66964c7070dbSScott Long 				       CTLFLAG_RD,
66974c7070dbSScott Long 				       &fl->ifl_cidx, 1, "Consumer Index");
66984c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
66994c7070dbSScott Long 				       CTLFLAG_RD,
67004c7070dbSScott Long 				       &fl->ifl_credits, 1, "credits available");
6701b3813609SPatrick Kelsey 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size",
6702b3813609SPatrick Kelsey 				       CTLFLAG_RD,
6703b3813609SPatrick Kelsey 				       &fl->ifl_buf_size, 1, "buffer size");
67044c7070dbSScott Long #if MEMORY_LOGGING
67054c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
67064c7070dbSScott Long 					CTLFLAG_RD,
67074c7070dbSScott Long 					&fl->ifl_m_enqueued, "mbufs allocated");
67084c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
67094c7070dbSScott Long 					CTLFLAG_RD,
67104c7070dbSScott Long 					&fl->ifl_m_dequeued, "mbufs freed");
67114c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
67124c7070dbSScott Long 					CTLFLAG_RD,
67134c7070dbSScott Long 					&fl->ifl_cl_enqueued, "clusters allocated");
67144c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
67154c7070dbSScott Long 					CTLFLAG_RD,
67164c7070dbSScott Long 					&fl->ifl_cl_dequeued, "clusters freed");
67174c7070dbSScott Long #endif
67184c7070dbSScott Long 
67194c7070dbSScott Long 		}
67204c7070dbSScott Long 	}
67214c7070dbSScott Long 
67224c7070dbSScott Long }
672395246abbSSean Bruno 
672477c1fcecSEric Joyner void
672577c1fcecSEric Joyner iflib_request_reset(if_ctx_t ctx)
672677c1fcecSEric Joyner {
672777c1fcecSEric Joyner 
672877c1fcecSEric Joyner 	STATE_LOCK(ctx);
672977c1fcecSEric Joyner 	ctx->ifc_flags |= IFC_DO_RESET;
673077c1fcecSEric Joyner 	STATE_UNLOCK(ctx);
673177c1fcecSEric Joyner }
673277c1fcecSEric Joyner 
673395246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
673495246abbSSean Bruno static struct mbuf *
673595246abbSSean Bruno iflib_fixup_rx(struct mbuf *m)
673695246abbSSean Bruno {
673795246abbSSean Bruno 	struct mbuf *n;
673895246abbSSean Bruno 
673995246abbSSean Bruno 	if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
674095246abbSSean Bruno 		bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
674195246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
674295246abbSSean Bruno 		n = m;
674395246abbSSean Bruno 	} else {
674495246abbSSean Bruno 		MGETHDR(n, M_NOWAIT, MT_DATA);
674595246abbSSean Bruno 		if (n == NULL) {
674695246abbSSean Bruno 			m_freem(m);
674795246abbSSean Bruno 			return (NULL);
674895246abbSSean Bruno 		}
674995246abbSSean Bruno 		bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
675095246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
675195246abbSSean Bruno 		m->m_len -= ETHER_HDR_LEN;
675295246abbSSean Bruno 		n->m_len = ETHER_HDR_LEN;
675395246abbSSean Bruno 		M_MOVE_PKTHDR(n, m);
675495246abbSSean Bruno 		n->m_next = m;
675595246abbSSean Bruno 	}
675695246abbSSean Bruno 	return (n);
675795246abbSSean Bruno }
675895246abbSSean Bruno #endif
675994618825SMark Johnston 
67607790c8c1SConrad Meyer #ifdef DEBUGNET
676194618825SMark Johnston static void
67627790c8c1SConrad Meyer iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
676394618825SMark Johnston {
676494618825SMark Johnston 	if_ctx_t ctx;
676594618825SMark Johnston 
676694618825SMark Johnston 	ctx = if_getsoftc(ifp);
676794618825SMark Johnston 	CTX_LOCK(ctx);
676894618825SMark Johnston 	*nrxr = NRXQSETS(ctx);
676994618825SMark Johnston 	*ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size;
677094618825SMark Johnston 	*clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size;
677194618825SMark Johnston 	CTX_UNLOCK(ctx);
677294618825SMark Johnston }
677394618825SMark Johnston 
677494618825SMark Johnston static void
67757790c8c1SConrad Meyer iflib_debugnet_event(if_t ifp, enum debugnet_ev event)
677694618825SMark Johnston {
677794618825SMark Johnston 	if_ctx_t ctx;
677894618825SMark Johnston 	if_softc_ctx_t scctx;
677994618825SMark Johnston 	iflib_fl_t fl;
678094618825SMark Johnston 	iflib_rxq_t rxq;
678194618825SMark Johnston 	int i, j;
678294618825SMark Johnston 
678394618825SMark Johnston 	ctx = if_getsoftc(ifp);
678494618825SMark Johnston 	scctx = &ctx->ifc_softc_ctx;
678594618825SMark Johnston 
678694618825SMark Johnston 	switch (event) {
67877790c8c1SConrad Meyer 	case DEBUGNET_START:
678894618825SMark Johnston 		for (i = 0; i < scctx->isc_nrxqsets; i++) {
678994618825SMark Johnston 			rxq = &ctx->ifc_rxqs[i];
679094618825SMark Johnston 			for (j = 0; j < rxq->ifr_nfl; j++) {
679194618825SMark Johnston 				fl = rxq->ifr_fl;
679294618825SMark Johnston 				fl->ifl_zone = m_getzone(fl->ifl_buf_size);
679394618825SMark Johnston 			}
679494618825SMark Johnston 		}
679594618825SMark Johnston 		iflib_no_tx_batch = 1;
679694618825SMark Johnston 		break;
679794618825SMark Johnston 	default:
679894618825SMark Johnston 		break;
679994618825SMark Johnston 	}
680094618825SMark Johnston }
680194618825SMark Johnston 
680294618825SMark Johnston static int
68037790c8c1SConrad Meyer iflib_debugnet_transmit(if_t ifp, struct mbuf *m)
680494618825SMark Johnston {
680594618825SMark Johnston 	if_ctx_t ctx;
680694618825SMark Johnston 	iflib_txq_t txq;
680794618825SMark Johnston 	int error;
680894618825SMark Johnston 
680994618825SMark Johnston 	ctx = if_getsoftc(ifp);
681094618825SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
681194618825SMark Johnston 	    IFF_DRV_RUNNING)
681294618825SMark Johnston 		return (EBUSY);
681394618825SMark Johnston 
681494618825SMark Johnston 	txq = &ctx->ifc_txqs[0];
681594618825SMark Johnston 	error = iflib_encap(txq, &m);
681694618825SMark Johnston 	if (error == 0)
681794618825SMark Johnston 		(void)iflib_txd_db_check(ctx, txq, true, txq->ift_in_use);
681894618825SMark Johnston 	return (error);
681994618825SMark Johnston }
682094618825SMark Johnston 
682194618825SMark Johnston static int
68227790c8c1SConrad Meyer iflib_debugnet_poll(if_t ifp, int count)
682394618825SMark Johnston {
68240b8df657SGleb Smirnoff 	struct epoch_tracker et;
682594618825SMark Johnston 	if_ctx_t ctx;
682694618825SMark Johnston 	if_softc_ctx_t scctx;
682794618825SMark Johnston 	iflib_txq_t txq;
682894618825SMark Johnston 	int i;
682994618825SMark Johnston 
683094618825SMark Johnston 	ctx = if_getsoftc(ifp);
683194618825SMark Johnston 	scctx = &ctx->ifc_softc_ctx;
683294618825SMark Johnston 
683394618825SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
683494618825SMark Johnston 	    IFF_DRV_RUNNING)
683594618825SMark Johnston 		return (EBUSY);
683694618825SMark Johnston 
683794618825SMark Johnston 	txq = &ctx->ifc_txqs[0];
683894618825SMark Johnston 	(void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
683994618825SMark Johnston 
68400b8df657SGleb Smirnoff 	NET_EPOCH_ENTER(et);
684194618825SMark Johnston 	for (i = 0; i < scctx->isc_nrxqsets; i++)
684294618825SMark Johnston 		(void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */);
68430b8df657SGleb Smirnoff 	NET_EPOCH_EXIT(et);
684494618825SMark Johnston 	return (0);
684594618825SMark Johnston }
68467790c8c1SConrad Meyer #endif /* DEBUGNET */
6847