xref: /freebsd/sys/net/iflib.c (revision 5ee36c68e17f682335375c3047af357236f3dc17)
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>
4009f6ff4fSMatt Macy #include <sys/jail.h>
414c7070dbSScott Long #include <sys/kernel.h>
424c7070dbSScott Long #include <sys/lock.h>
4309f6ff4fSMatt Macy #include <sys/md5.h>
444c7070dbSScott Long #include <sys/mutex.h>
454c7070dbSScott Long #include <sys/module.h>
464c7070dbSScott Long #include <sys/kobj.h>
474c7070dbSScott Long #include <sys/rman.h>
4809f6ff4fSMatt Macy #include <sys/proc.h>
494c7070dbSScott Long #include <sys/sbuf.h>
504c7070dbSScott Long #include <sys/smp.h>
514c7070dbSScott Long #include <sys/socket.h>
5209f6ff4fSMatt Macy #include <sys/sockio.h>
534c7070dbSScott Long #include <sys/sysctl.h>
544c7070dbSScott Long #include <sys/syslog.h>
554c7070dbSScott Long #include <sys/taskqueue.h>
5623ac9029SStephen Hurd #include <sys/limits.h>
574c7070dbSScott Long 
584c7070dbSScott Long #include <net/if.h>
594c7070dbSScott Long #include <net/if_var.h>
604c7070dbSScott Long #include <net/if_types.h>
614c7070dbSScott Long #include <net/if_media.h>
624c7070dbSScott Long #include <net/bpf.h>
634c7070dbSScott Long #include <net/ethernet.h>
644c7070dbSScott Long #include <net/mp_ring.h>
6535e4e998SStephen Hurd #include <net/vnet.h>
664c7070dbSScott Long 
674c7070dbSScott Long #include <netinet/in.h>
684c7070dbSScott Long #include <netinet/in_pcb.h>
694c7070dbSScott Long #include <netinet/tcp_lro.h>
704c7070dbSScott Long #include <netinet/in_systm.h>
714c7070dbSScott Long #include <netinet/if_ether.h>
724c7070dbSScott Long #include <netinet/ip.h>
734c7070dbSScott Long #include <netinet/ip6.h>
744c7070dbSScott Long #include <netinet/tcp.h>
7535e4e998SStephen Hurd #include <netinet/ip_var.h>
7694618825SMark Johnston #include <netinet/netdump/netdump.h>
7735e4e998SStephen Hurd #include <netinet6/ip6_var.h>
784c7070dbSScott Long 
794c7070dbSScott Long #include <machine/bus.h>
804c7070dbSScott Long #include <machine/in_cksum.h>
814c7070dbSScott Long 
824c7070dbSScott Long #include <vm/vm.h>
834c7070dbSScott Long #include <vm/pmap.h>
844c7070dbSScott Long 
854c7070dbSScott Long #include <dev/led/led.h>
864c7070dbSScott Long #include <dev/pci/pcireg.h>
874c7070dbSScott Long #include <dev/pci/pcivar.h>
884c7070dbSScott Long #include <dev/pci/pci_private.h>
894c7070dbSScott Long 
904c7070dbSScott Long #include <net/iflib.h>
9109f6ff4fSMatt Macy #include <net/iflib_private.h>
924c7070dbSScott Long 
934c7070dbSScott Long #include "ifdi_if.h"
944c7070dbSScott Long 
954c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
964c7070dbSScott Long #include <sys/memdesc.h>
974c7070dbSScott Long #include <machine/bus.h>
984c7070dbSScott Long #include <machine/md_var.h>
994c7070dbSScott Long #include <machine/specialreg.h>
1004c7070dbSScott Long #include <x86/include/busdma_impl.h>
1014c7070dbSScott Long #include <x86/iommu/busdma_dmar.h>
1024c7070dbSScott Long #endif
1034c7070dbSScott Long 
10487890dbaSSean Bruno #include <sys/bitstring.h>
1054c7070dbSScott Long /*
10695246abbSSean Bruno  * enable accounting of every mbuf as it comes in to and goes out of
10795246abbSSean Bruno  * iflib's software descriptor references
1084c7070dbSScott Long  */
1094c7070dbSScott Long #define MEMORY_LOGGING 0
1104c7070dbSScott Long /*
1114c7070dbSScott Long  * Enable mbuf vectors for compressing long mbuf chains
1124c7070dbSScott Long  */
1134c7070dbSScott Long 
1144c7070dbSScott Long /*
1154c7070dbSScott Long  * NB:
1164c7070dbSScott Long  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
1174c7070dbSScott Long  *   we prefetch needs to be determined by the time spent in m_free vis a vis
1184c7070dbSScott Long  *   the cost of a prefetch. This will of course vary based on the workload:
1194c7070dbSScott Long  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
1204c7070dbSScott Long  *        is quite expensive, thus suggesting very little prefetch.
1214c7070dbSScott Long  *      - small packet forwarding which is just returning a single mbuf to
1224c7070dbSScott Long  *        UMA will typically be very fast vis a vis the cost of a memory
1234c7070dbSScott Long  *        access.
1244c7070dbSScott Long  */
1254c7070dbSScott Long 
1264c7070dbSScott Long 
1274c7070dbSScott Long /*
1284c7070dbSScott Long  * File organization:
1294c7070dbSScott Long  *  - private structures
1304c7070dbSScott Long  *  - iflib private utility functions
1314c7070dbSScott Long  *  - ifnet functions
1324c7070dbSScott Long  *  - vlan registry and other exported functions
1334c7070dbSScott Long  *  - iflib public core functions
1344c7070dbSScott Long  *
1354c7070dbSScott Long  *
1364c7070dbSScott Long  */
13709f6ff4fSMatt Macy MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
1384c7070dbSScott Long 
1394c7070dbSScott Long struct iflib_txq;
1404c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t;
1414c7070dbSScott Long struct iflib_rxq;
1424c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t;
1434c7070dbSScott Long struct iflib_fl;
1444c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t;
1454c7070dbSScott Long 
1464ecb427aSSean Bruno struct iflib_ctx;
1474ecb427aSSean Bruno 
1482d873474SStephen Hurd static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid);
1492d873474SStephen Hurd 
1504c7070dbSScott Long typedef struct iflib_filter_info {
1514c7070dbSScott Long 	driver_filter_t *ifi_filter;
1524c7070dbSScott Long 	void *ifi_filter_arg;
1534c7070dbSScott Long 	struct grouptask *ifi_task;
15495246abbSSean Bruno 	void *ifi_ctx;
1554c7070dbSScott Long } *iflib_filter_info_t;
1564c7070dbSScott Long 
1574c7070dbSScott Long struct iflib_ctx {
1584c7070dbSScott Long 	KOBJ_FIELDS;
1594c7070dbSScott Long    /*
1604c7070dbSScott Long    * Pointer to hardware driver's softc
1614c7070dbSScott Long    */
1624c7070dbSScott Long 	void *ifc_softc;
1634c7070dbSScott Long 	device_t ifc_dev;
1644c7070dbSScott Long 	if_t ifc_ifp;
1654c7070dbSScott Long 
1664c7070dbSScott Long 	cpuset_t ifc_cpus;
1674c7070dbSScott Long 	if_shared_ctx_t ifc_sctx;
1684c7070dbSScott Long 	struct if_softc_ctx ifc_softc_ctx;
1694c7070dbSScott Long 
170aa8a24d3SStephen Hurd 	struct sx ifc_ctx_sx;
1717b610b60SSean Bruno 	struct mtx ifc_state_mtx;
1724c7070dbSScott Long 
1734c7070dbSScott Long 	uint16_t ifc_nhwtxqs;
1744c7070dbSScott Long 
1754c7070dbSScott Long 	iflib_txq_t ifc_txqs;
1764c7070dbSScott Long 	iflib_rxq_t ifc_rxqs;
1774c7070dbSScott Long 	uint32_t ifc_if_flags;
1784c7070dbSScott Long 	uint32_t ifc_flags;
1794c7070dbSScott Long 	uint32_t ifc_max_fl_buf_size;
1804c7070dbSScott Long 	int ifc_in_detach;
1814c7070dbSScott Long 
1824c7070dbSScott Long 	int ifc_link_state;
1834c7070dbSScott Long 	int ifc_link_irq;
1844c7070dbSScott Long 	int ifc_watchdog_events;
1854c7070dbSScott Long 	struct cdev *ifc_led_dev;
1864c7070dbSScott Long 	struct resource *ifc_msix_mem;
1874c7070dbSScott Long 
1884c7070dbSScott Long 	struct if_irq ifc_legacy_irq;
1894c7070dbSScott Long 	struct grouptask ifc_admin_task;
1904c7070dbSScott Long 	struct grouptask ifc_vflr_task;
1914c7070dbSScott Long 	struct iflib_filter_info ifc_filter_info;
1924c7070dbSScott Long 	struct ifmedia	ifc_media;
1934c7070dbSScott Long 
1944c7070dbSScott Long 	struct sysctl_oid *ifc_sysctl_node;
1954c7070dbSScott Long 	uint16_t ifc_sysctl_ntxqs;
1964c7070dbSScott Long 	uint16_t ifc_sysctl_nrxqs;
19723ac9029SStephen Hurd 	uint16_t ifc_sysctl_qs_eq_override;
198f4d2154eSStephen Hurd 	uint16_t ifc_sysctl_rx_budget;
19923ac9029SStephen Hurd 
20095246abbSSean Bruno 	qidx_t ifc_sysctl_ntxds[8];
20195246abbSSean Bruno 	qidx_t ifc_sysctl_nrxds[8];
2024c7070dbSScott Long 	struct if_txrx ifc_txrx;
2034c7070dbSScott Long #define isc_txd_encap  ifc_txrx.ift_txd_encap
2044c7070dbSScott Long #define isc_txd_flush  ifc_txrx.ift_txd_flush
2054c7070dbSScott Long #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
2064c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available
2074c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
2084c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2094c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush
2104c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2114c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2124c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr
2134c7070dbSScott Long 	eventhandler_tag ifc_vlan_attach_event;
2144c7070dbSScott Long 	eventhandler_tag ifc_vlan_detach_event;
2154c7070dbSScott Long 	uint8_t ifc_mac[ETHER_ADDR_LEN];
2164c7070dbSScott Long 	char ifc_mtx_name[16];
2174c7070dbSScott Long };
2184c7070dbSScott Long 
2194c7070dbSScott Long 
2204c7070dbSScott Long void *
2214c7070dbSScott Long iflib_get_softc(if_ctx_t ctx)
2224c7070dbSScott Long {
2234c7070dbSScott Long 
2244c7070dbSScott Long 	return (ctx->ifc_softc);
2254c7070dbSScott Long }
2264c7070dbSScott Long 
2274c7070dbSScott Long device_t
2284c7070dbSScott Long iflib_get_dev(if_ctx_t ctx)
2294c7070dbSScott Long {
2304c7070dbSScott Long 
2314c7070dbSScott Long 	return (ctx->ifc_dev);
2324c7070dbSScott Long }
2334c7070dbSScott Long 
2344c7070dbSScott Long if_t
2354c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx)
2364c7070dbSScott Long {
2374c7070dbSScott Long 
2384c7070dbSScott Long 	return (ctx->ifc_ifp);
2394c7070dbSScott Long }
2404c7070dbSScott Long 
2414c7070dbSScott Long struct ifmedia *
2424c7070dbSScott Long iflib_get_media(if_ctx_t ctx)
2434c7070dbSScott Long {
2444c7070dbSScott Long 
2454c7070dbSScott Long 	return (&ctx->ifc_media);
2464c7070dbSScott Long }
2474c7070dbSScott Long 
24809f6ff4fSMatt Macy uint32_t
24909f6ff4fSMatt Macy iflib_get_flags(if_ctx_t ctx)
25009f6ff4fSMatt Macy {
25109f6ff4fSMatt Macy 	return (ctx->ifc_flags);
25209f6ff4fSMatt Macy }
25309f6ff4fSMatt Macy 
25409f6ff4fSMatt Macy void
25509f6ff4fSMatt Macy iflib_set_detach(if_ctx_t ctx)
25609f6ff4fSMatt Macy {
25709f6ff4fSMatt Macy 	ctx->ifc_in_detach = 1;
25809f6ff4fSMatt Macy }
25909f6ff4fSMatt Macy 
2604c7070dbSScott Long void
2614c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
2624c7070dbSScott Long {
2634c7070dbSScott Long 
2644c7070dbSScott Long 	bcopy(mac, ctx->ifc_mac, ETHER_ADDR_LEN);
2654c7070dbSScott Long }
2664c7070dbSScott Long 
2674c7070dbSScott Long if_softc_ctx_t
2684c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx)
2694c7070dbSScott Long {
2704c7070dbSScott Long 
2714c7070dbSScott Long 	return (&ctx->ifc_softc_ctx);
2724c7070dbSScott Long }
2734c7070dbSScott Long 
2744c7070dbSScott Long if_shared_ctx_t
2754c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx)
2764c7070dbSScott Long {
2774c7070dbSScott Long 
2784c7070dbSScott Long 	return (ctx->ifc_sctx);
2794c7070dbSScott Long }
2804c7070dbSScott Long 
28195246abbSSean Bruno #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
2824c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
2835e888388SSean Bruno #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1)))
2844c7070dbSScott Long 
2854c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
2864c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
2874c7070dbSScott Long 
2884c7070dbSScott Long #define RX_SW_DESC_MAP_CREATED	(1 << 0)
289ab2e3f79SStephen Hurd #define TX_SW_DESC_MAP_CREATED	(1 << 1)
290ab2e3f79SStephen Hurd #define RX_SW_DESC_INUSE        (1 << 3)
291ab2e3f79SStephen Hurd #define TX_SW_DESC_MAPPED       (1 << 4)
2924c7070dbSScott Long 
2932cc3b2eeSGleb Smirnoff #define	M_TOOBIG		M_PROTO1
2945c5ca36cSSean Bruno 
295e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array {
296e035717eSSean Bruno 	bus_dmamap_t	*ifsd_map;         /* bus_dma maps for packet */
297e035717eSSean Bruno 	struct mbuf	**ifsd_m;           /* pkthdr mbufs */
298e035717eSSean Bruno 	caddr_t		*ifsd_cl;          /* direct cluster pointer for rx */
299e035717eSSean Bruno 	uint8_t		*ifsd_flags;
300e035717eSSean Bruno } iflib_rxsd_array_t;
3014c7070dbSScott Long 
3024c7070dbSScott Long typedef struct iflib_sw_tx_desc_array {
3034c7070dbSScott Long 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
3044c7070dbSScott Long 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
3054c7070dbSScott Long 	uint8_t		*ifsd_flags;
30695246abbSSean Bruno } if_txsd_vec_t;
3074c7070dbSScott Long 
3084c7070dbSScott Long 
3094c7070dbSScott Long /* magic number that should be high enough for any hardware */
3104c7070dbSScott Long #define IFLIB_MAX_TX_SEGS		128
31109b57b7fSStephen Hurd /* bnxt supports 64 with hardware LRO enabled */
31209b57b7fSStephen Hurd #define IFLIB_MAX_RX_SEGS		64
31395246abbSSean Bruno #define IFLIB_RX_COPY_THRESH		128
3144c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH		32
31595246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */
31695246abbSSean Bruno #define IFLIB_MIN_DESC_SEC		16384
31795246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ	16
3184c7070dbSScott Long #define IFLIB_QUEUE_IDLE		0
3194c7070dbSScott Long #define IFLIB_QUEUE_HUNG		1
3204c7070dbSScott Long #define IFLIB_QUEUE_WORKING		2
32195246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */
32295246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR	4
3234c7070dbSScott Long 
32495246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */
32595246abbSSean Bruno #define TX_BATCH_SIZE			32
3264c7070dbSScott Long 
3274c7070dbSScott Long #define IFLIB_RESTART_BUDGET		8
3284c7070dbSScott Long 
3294c7070dbSScott Long 
3304c7070dbSScott Long #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
3314c7070dbSScott Long 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
3324c7070dbSScott Long 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
3334c7070dbSScott Long struct iflib_txq {
33495246abbSSean Bruno 	qidx_t		ift_in_use;
33595246abbSSean Bruno 	qidx_t		ift_cidx;
33695246abbSSean Bruno 	qidx_t		ift_cidx_processed;
33795246abbSSean Bruno 	qidx_t		ift_pidx;
3384c7070dbSScott Long 	uint8_t		ift_gen;
33923ac9029SStephen Hurd 	uint8_t		ift_br_offset;
34095246abbSSean Bruno 	uint16_t	ift_npending;
34195246abbSSean Bruno 	uint16_t	ift_db_pending;
34295246abbSSean Bruno 	uint16_t	ift_rs_pending;
3434c7070dbSScott Long 	/* implicit pad */
34495246abbSSean Bruno 	uint8_t		ift_txd_size[8];
3454c7070dbSScott Long 	uint64_t	ift_processed;
3464c7070dbSScott Long 	uint64_t	ift_cleaned;
34795246abbSSean Bruno 	uint64_t	ift_cleaned_prev;
3484c7070dbSScott Long #if MEMORY_LOGGING
3494c7070dbSScott Long 	uint64_t	ift_enqueued;
3504c7070dbSScott Long 	uint64_t	ift_dequeued;
3514c7070dbSScott Long #endif
3524c7070dbSScott Long 	uint64_t	ift_no_tx_dma_setup;
3534c7070dbSScott Long 	uint64_t	ift_no_desc_avail;
3544c7070dbSScott Long 	uint64_t	ift_mbuf_defrag_failed;
3554c7070dbSScott Long 	uint64_t	ift_mbuf_defrag;
3564c7070dbSScott Long 	uint64_t	ift_map_failed;
3574c7070dbSScott Long 	uint64_t	ift_txd_encap_efbig;
3584c7070dbSScott Long 	uint64_t	ift_pullups;
3594c7070dbSScott Long 
3604c7070dbSScott Long 	struct mtx	ift_mtx;
3614c7070dbSScott Long 	struct mtx	ift_db_mtx;
3624c7070dbSScott Long 
3634c7070dbSScott Long 	/* constant values */
3644c7070dbSScott Long 	if_ctx_t	ift_ctx;
36595246abbSSean Bruno 	struct ifmp_ring        *ift_br;
3664c7070dbSScott Long 	struct grouptask	ift_task;
36795246abbSSean Bruno 	qidx_t		ift_size;
3684c7070dbSScott Long 	uint16_t	ift_id;
3694c7070dbSScott Long 	struct callout	ift_timer;
3704c7070dbSScott Long 
37195246abbSSean Bruno 	if_txsd_vec_t	ift_sds;
3724c7070dbSScott Long 	uint8_t		ift_qstatus;
3734c7070dbSScott Long 	uint8_t		ift_closed;
37495246abbSSean Bruno 	uint8_t		ift_update_freq;
3754c7070dbSScott Long 	struct iflib_filter_info ift_filter_info;
3764c7070dbSScott Long 	bus_dma_tag_t		ift_desc_tag;
3774c7070dbSScott Long 	bus_dma_tag_t		ift_tso_desc_tag;
3784c7070dbSScott Long 	iflib_dma_info_t	ift_ifdi;
3794c7070dbSScott Long #define MTX_NAME_LEN 16
3804c7070dbSScott Long 	char                    ift_mtx_name[MTX_NAME_LEN];
3814c7070dbSScott Long 	char                    ift_db_mtx_name[MTX_NAME_LEN];
3824c7070dbSScott Long 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
3831248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
3841248952aSSean Bruno 	uint64_t ift_cpu_exec_count[256];
3851248952aSSean Bruno #endif
3864c7070dbSScott Long } __aligned(CACHE_LINE_SIZE);
3874c7070dbSScott Long 
3884c7070dbSScott Long struct iflib_fl {
38995246abbSSean Bruno 	qidx_t		ifl_cidx;
39095246abbSSean Bruno 	qidx_t		ifl_pidx;
39195246abbSSean Bruno 	qidx_t		ifl_credits;
3924c7070dbSScott Long 	uint8_t		ifl_gen;
39395246abbSSean Bruno 	uint8_t		ifl_rxd_size;
3944c7070dbSScott Long #if MEMORY_LOGGING
3954c7070dbSScott Long 	uint64_t	ifl_m_enqueued;
3964c7070dbSScott Long 	uint64_t	ifl_m_dequeued;
3974c7070dbSScott Long 	uint64_t	ifl_cl_enqueued;
3984c7070dbSScott Long 	uint64_t	ifl_cl_dequeued;
3994c7070dbSScott Long #endif
4004c7070dbSScott Long 	/* implicit pad */
4014c7070dbSScott Long 
40287890dbaSSean Bruno 	bitstr_t 	*ifl_rx_bitmap;
40387890dbaSSean Bruno 	qidx_t		ifl_fragidx;
4044c7070dbSScott Long 	/* constant */
40595246abbSSean Bruno 	qidx_t		ifl_size;
4064c7070dbSScott Long 	uint16_t	ifl_buf_size;
4074c7070dbSScott Long 	uint16_t	ifl_cltype;
4084c7070dbSScott Long 	uma_zone_t	ifl_zone;
409e035717eSSean Bruno 	iflib_rxsd_array_t	ifl_sds;
4104c7070dbSScott Long 	iflib_rxq_t	ifl_rxq;
4114c7070dbSScott Long 	uint8_t		ifl_id;
4124c7070dbSScott Long 	bus_dma_tag_t           ifl_desc_tag;
4134c7070dbSScott Long 	iflib_dma_info_t	ifl_ifdi;
4144c7070dbSScott Long 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
4154c7070dbSScott Long 	caddr_t		ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
41695246abbSSean Bruno 	qidx_t	ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
4174c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4184c7070dbSScott Long 
41995246abbSSean Bruno static inline qidx_t
42095246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
4214c7070dbSScott Long {
42295246abbSSean Bruno 	qidx_t used;
4234c7070dbSScott Long 
4244c7070dbSScott Long 	if (pidx > cidx)
4254c7070dbSScott Long 		used = pidx - cidx;
4264c7070dbSScott Long 	else if (pidx < cidx)
4274c7070dbSScott Long 		used = size - cidx + pidx;
4284c7070dbSScott Long 	else if (gen == 0 && pidx == cidx)
4294c7070dbSScott Long 		used = 0;
4304c7070dbSScott Long 	else if (gen == 1 && pidx == cidx)
4314c7070dbSScott Long 		used = size;
4324c7070dbSScott Long 	else
4334c7070dbSScott Long 		panic("bad state");
4344c7070dbSScott Long 
4354c7070dbSScott Long 	return (used);
4364c7070dbSScott Long }
4374c7070dbSScott Long 
4384c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
4394c7070dbSScott Long 
4404c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \
4414c7070dbSScott Long 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
4424c7070dbSScott Long 
4434c7070dbSScott Long struct iflib_rxq {
4444c7070dbSScott Long 	/* If there is a separate completion queue -
4454c7070dbSScott Long 	 * these are the cq cidx and pidx. Otherwise
4464c7070dbSScott Long 	 * these are unused.
4474c7070dbSScott Long 	 */
44895246abbSSean Bruno 	qidx_t		ifr_size;
44995246abbSSean Bruno 	qidx_t		ifr_cq_cidx;
45095246abbSSean Bruno 	qidx_t		ifr_cq_pidx;
4514c7070dbSScott Long 	uint8_t		ifr_cq_gen;
45223ac9029SStephen Hurd 	uint8_t		ifr_fl_offset;
4534c7070dbSScott Long 
4544c7070dbSScott Long 	if_ctx_t	ifr_ctx;
4554c7070dbSScott Long 	iflib_fl_t	ifr_fl;
4564c7070dbSScott Long 	uint64_t	ifr_rx_irq;
4574c7070dbSScott Long 	uint16_t	ifr_id;
4584c7070dbSScott Long 	uint8_t		ifr_lro_enabled;
4594c7070dbSScott Long 	uint8_t		ifr_nfl;
46095246abbSSean Bruno 	uint8_t		ifr_ntxqirq;
46195246abbSSean Bruno 	uint8_t		ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
4624c7070dbSScott Long 	struct lro_ctrl			ifr_lc;
4634c7070dbSScott Long 	struct grouptask        ifr_task;
4644c7070dbSScott Long 	struct iflib_filter_info ifr_filter_info;
4654c7070dbSScott Long 	iflib_dma_info_t		ifr_ifdi;
466ab2e3f79SStephen Hurd 
4674c7070dbSScott Long 	/* dynamically allocate if any drivers need a value substantially larger than this */
4684c7070dbSScott Long 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
4691248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
4701248952aSSean Bruno 	uint64_t ifr_cpu_exec_count[256];
4711248952aSSean Bruno #endif
4724c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4734c7070dbSScott Long 
47495246abbSSean Bruno typedef struct if_rxsd {
47595246abbSSean Bruno 	caddr_t *ifsd_cl;
47695246abbSSean Bruno 	struct mbuf **ifsd_m;
47795246abbSSean Bruno 	iflib_fl_t ifsd_fl;
47895246abbSSean Bruno 	qidx_t ifsd_cidx;
47995246abbSSean Bruno } *if_rxsd_t;
48095246abbSSean Bruno 
48195246abbSSean Bruno /* multiple of word size */
48295246abbSSean Bruno #ifdef __LP64__
483ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	6
48495246abbSSean Bruno #define RXD_INFO_SIZE	5
48595246abbSSean Bruno #define PKT_TYPE uint64_t
48695246abbSSean Bruno #else
487ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	11
48895246abbSSean Bruno #define RXD_INFO_SIZE	8
48995246abbSSean Bruno #define PKT_TYPE uint32_t
49095246abbSSean Bruno #endif
49195246abbSSean Bruno #define PKT_LOOP_BOUND  ((PKT_INFO_SIZE/3)*3)
49295246abbSSean Bruno #define RXD_LOOP_BOUND  ((RXD_INFO_SIZE/4)*4)
49395246abbSSean Bruno 
49495246abbSSean Bruno typedef struct if_pkt_info_pad {
49595246abbSSean Bruno 	PKT_TYPE pkt_val[PKT_INFO_SIZE];
49695246abbSSean Bruno } *if_pkt_info_pad_t;
49795246abbSSean Bruno typedef struct if_rxd_info_pad {
49895246abbSSean Bruno 	PKT_TYPE rxd_val[RXD_INFO_SIZE];
49995246abbSSean Bruno } *if_rxd_info_pad_t;
50095246abbSSean Bruno 
50195246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info));
50295246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info));
50395246abbSSean Bruno 
50495246abbSSean Bruno 
50595246abbSSean Bruno static inline void
50695246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi)
50795246abbSSean Bruno {
50895246abbSSean Bruno 	if_pkt_info_pad_t pi_pad;
50995246abbSSean Bruno 
51095246abbSSean Bruno 	pi_pad = (if_pkt_info_pad_t)pi;
51195246abbSSean Bruno 	pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0;
51295246abbSSean Bruno 	pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0;
51395246abbSSean Bruno #ifndef __LP64__
514ab2e3f79SStephen Hurd 	pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0;
515ab2e3f79SStephen Hurd 	pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0;
51695246abbSSean Bruno #endif
51795246abbSSean Bruno }
51895246abbSSean Bruno 
51909f6ff4fSMatt Macy static device_method_t iflib_pseudo_methods[] = {
52009f6ff4fSMatt Macy 	DEVMETHOD(device_attach, noop_attach),
52109f6ff4fSMatt Macy 	DEVMETHOD(device_detach, iflib_pseudo_detach),
52209f6ff4fSMatt Macy 	DEVMETHOD_END
52309f6ff4fSMatt Macy };
52409f6ff4fSMatt Macy 
52509f6ff4fSMatt Macy driver_t iflib_pseudodriver = {
52609f6ff4fSMatt Macy 	"iflib_pseudo", iflib_pseudo_methods, sizeof(struct iflib_ctx),
52709f6ff4fSMatt Macy };
52809f6ff4fSMatt Macy 
52995246abbSSean Bruno static inline void
53095246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri)
53195246abbSSean Bruno {
53295246abbSSean Bruno 	if_rxd_info_pad_t ri_pad;
53395246abbSSean Bruno 	int i;
53495246abbSSean Bruno 
53595246abbSSean Bruno 	ri_pad = (if_rxd_info_pad_t)ri;
53695246abbSSean Bruno 	for (i = 0; i < RXD_LOOP_BOUND; i += 4) {
53795246abbSSean Bruno 		ri_pad->rxd_val[i] = 0;
53895246abbSSean Bruno 		ri_pad->rxd_val[i+1] = 0;
53995246abbSSean Bruno 		ri_pad->rxd_val[i+2] = 0;
54095246abbSSean Bruno 		ri_pad->rxd_val[i+3] = 0;
54195246abbSSean Bruno 	}
54295246abbSSean Bruno #ifdef __LP64__
54395246abbSSean Bruno 	ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0;
54495246abbSSean Bruno #endif
54595246abbSSean Bruno }
54695246abbSSean Bruno 
5474c7070dbSScott Long /*
5484c7070dbSScott Long  * Only allow a single packet to take up most 1/nth of the tx ring
5494c7070dbSScott Long  */
5504c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12
5514c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1
5524c7070dbSScott Long 
5534c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
5544c7070dbSScott Long 
555aa8a24d3SStephen Hurd #define CTX_LOCK_INIT(_sc)  sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock")
556aa8a24d3SStephen Hurd #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx)
557aa8a24d3SStephen Hurd #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx)
558aa8a24d3SStephen Hurd #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx)
5594c7070dbSScott Long 
5607b610b60SSean Bruno 
5617b610b60SSean Bruno #define STATE_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF)
5627b610b60SSean Bruno #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx)
5637b610b60SSean Bruno #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx)
5647b610b60SSean Bruno #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx)
5657b610b60SSean Bruno 
566ab2e3f79SStephen Hurd 
5674c7070dbSScott Long 
5684c7070dbSScott Long #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
5694c7070dbSScott Long #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
5704c7070dbSScott Long 
5714c7070dbSScott Long 
5724c7070dbSScott Long /* Our boot-time initialization hook */
5734c7070dbSScott Long static int	iflib_module_event_handler(module_t, int, void *);
5744c7070dbSScott Long 
5754c7070dbSScott Long static moduledata_t iflib_moduledata = {
5764c7070dbSScott Long 	"iflib",
5774c7070dbSScott Long 	iflib_module_event_handler,
5784c7070dbSScott Long 	NULL
5794c7070dbSScott Long };
5804c7070dbSScott Long 
5814c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
5824c7070dbSScott Long MODULE_VERSION(iflib, 1);
5834c7070dbSScott Long 
5844c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1);
5854c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1);
5864c7070dbSScott Long 
587ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
588ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
589ab2e3f79SStephen Hurd 
5904c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS
5914c7070dbSScott Long #ifdef INVARIANTS
5924c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1
5934c7070dbSScott Long #else
5944c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0
5954c7070dbSScott Long #endif /* !INVARIANTS */
5964c7070dbSScott Long #endif
5974c7070dbSScott Long 
598ab2e3f79SStephen Hurd static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0,
599ab2e3f79SStephen Hurd                    "iflib driver parameters");
600ab2e3f79SStephen Hurd 
6014c7070dbSScott Long /*
6024c7070dbSScott Long  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
6034c7070dbSScott Long  */
6044c7070dbSScott Long static int iflib_min_tx_latency = 0;
6054c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
606da69b8f9SSean Bruno 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
60795246abbSSean Bruno static int iflib_no_tx_batch = 0;
60895246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
60995246abbSSean Bruno 		   &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput");
6104c7070dbSScott Long 
6114c7070dbSScott Long 
6124c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS
6134c7070dbSScott Long 
6144c7070dbSScott Long static int iflib_tx_seen;
6154c7070dbSScott Long static int iflib_tx_sent;
6164c7070dbSScott Long static int iflib_tx_encap;
6174c7070dbSScott Long static int iflib_rx_allocs;
6184c7070dbSScott Long static int iflib_fl_refills;
6194c7070dbSScott Long static int iflib_fl_refills_large;
6204c7070dbSScott Long static int iflib_tx_frees;
6214c7070dbSScott Long 
6224c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
6234c7070dbSScott Long 		   &iflib_tx_seen, 0, "# tx mbufs seen");
6244c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
6254c7070dbSScott Long 		   &iflib_tx_sent, 0, "# tx mbufs sent");
6264c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
6274c7070dbSScott Long 		   &iflib_tx_encap, 0, "# tx mbufs encapped");
6284c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
6294c7070dbSScott Long 		   &iflib_tx_frees, 0, "# tx frees");
6304c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
6314c7070dbSScott Long 		   &iflib_rx_allocs, 0, "# rx allocations");
6324c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
6334c7070dbSScott Long 		   &iflib_fl_refills, 0, "# refills");
6344c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
6354c7070dbSScott Long 		   &iflib_fl_refills_large, 0, "# large refills");
6364c7070dbSScott Long 
6374c7070dbSScott Long 
6384c7070dbSScott Long static int iflib_txq_drain_flushing;
6394c7070dbSScott Long static int iflib_txq_drain_oactive;
6404c7070dbSScott Long static int iflib_txq_drain_notready;
6414c7070dbSScott Long static int iflib_txq_drain_encapfail;
6424c7070dbSScott Long 
6434c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
6444c7070dbSScott Long 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
6454c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
6464c7070dbSScott Long 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
6474c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
6484c7070dbSScott Long 		   &iflib_txq_drain_notready, 0, "# drain notready");
6494c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_encapfail, CTLFLAG_RD,
6504c7070dbSScott Long 		   &iflib_txq_drain_encapfail, 0, "# drain encap fails");
6514c7070dbSScott Long 
6524c7070dbSScott Long 
6534c7070dbSScott Long static int iflib_encap_load_mbuf_fail;
654d14c853bSStephen Hurd static int iflib_encap_pad_mbuf_fail;
6554c7070dbSScott Long static int iflib_encap_txq_avail_fail;
6564c7070dbSScott Long static int iflib_encap_txd_encap_fail;
6574c7070dbSScott Long 
6584c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
6594c7070dbSScott Long 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
660d14c853bSStephen Hurd SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD,
661d14c853bSStephen Hurd 		   &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures");
6624c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
6634c7070dbSScott Long 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
6644c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
6654c7070dbSScott Long 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
6664c7070dbSScott Long 
6674c7070dbSScott Long static int iflib_task_fn_rxs;
6684c7070dbSScott Long static int iflib_rx_intr_enables;
6694c7070dbSScott Long static int iflib_fast_intrs;
6704c7070dbSScott Long static int iflib_intr_link;
6714c7070dbSScott Long static int iflib_intr_msix;
6724c7070dbSScott Long static int iflib_rx_unavail;
6734c7070dbSScott Long static int iflib_rx_ctx_inactive;
6744c7070dbSScott Long static int iflib_rx_zero_len;
6754c7070dbSScott Long static int iflib_rx_if_input;
6764c7070dbSScott Long static int iflib_rx_mbuf_null;
6774c7070dbSScott Long static int iflib_rxd_flush;
6784c7070dbSScott Long 
6794c7070dbSScott Long static int iflib_verbose_debug;
6804c7070dbSScott Long 
6814c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_link, CTLFLAG_RD,
6824c7070dbSScott Long 		   &iflib_intr_link, 0, "# intr link calls");
6834c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_msix, CTLFLAG_RD,
6844c7070dbSScott Long 		   &iflib_intr_msix, 0, "# intr msix calls");
6854c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
6864c7070dbSScott Long 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
6874c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
6884c7070dbSScott Long 		   &iflib_rx_intr_enables, 0, "# rx intr enables");
6894c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
6904c7070dbSScott Long 		   &iflib_fast_intrs, 0, "# fast_intr calls");
6914c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
6924c7070dbSScott Long 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
6934c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
6944c7070dbSScott Long 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
6954c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_zero_len, CTLFLAG_RD,
6964c7070dbSScott Long 		   &iflib_rx_zero_len, 0, "# times rxeof saw zero len mbuf");
6974c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
6984c7070dbSScott Long 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
6994c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD,
7004c7070dbSScott Long 		   &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf");
7014c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
7024c7070dbSScott Long 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
7034c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
7044c7070dbSScott Long 		   &iflib_verbose_debug, 0, "enable verbose debugging");
7054c7070dbSScott Long 
7064c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
707da69b8f9SSean Bruno static void
708da69b8f9SSean Bruno iflib_debug_reset(void)
709da69b8f9SSean Bruno {
710da69b8f9SSean Bruno 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
711da69b8f9SSean Bruno 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
712da69b8f9SSean Bruno 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
713da69b8f9SSean Bruno 		iflib_txq_drain_notready = iflib_txq_drain_encapfail =
714d14c853bSStephen Hurd 		iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail =
715d14c853bSStephen Hurd 		iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail =
716d14c853bSStephen Hurd 		iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs =
717d14c853bSStephen Hurd 		iflib_intr_link = iflib_intr_msix = iflib_rx_unavail =
718da69b8f9SSean Bruno 		iflib_rx_ctx_inactive = iflib_rx_zero_len = iflib_rx_if_input =
719da69b8f9SSean Bruno 		iflib_rx_mbuf_null = iflib_rxd_flush = 0;
720da69b8f9SSean Bruno }
7214c7070dbSScott Long 
7224c7070dbSScott Long #else
7234c7070dbSScott Long #define DBG_COUNTER_INC(name)
724da69b8f9SSean Bruno static void iflib_debug_reset(void) {}
7254c7070dbSScott Long #endif
7264c7070dbSScott Long 
7274c7070dbSScott Long #define IFLIB_DEBUG 0
7284c7070dbSScott Long 
7294c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx);
7304c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx);
7314c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx);
7324c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
73395246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
7344c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx);
7354c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx);
7364c7070dbSScott Long static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, char *str);
7374c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
7384c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
7394c7070dbSScott Long static int iflib_register(if_ctx_t);
7404c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx);
7414c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
7424c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx);
743da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq);
7441248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx);
74595246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx);
74695246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
74795246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m);
74895246abbSSean Bruno #endif
7494c7070dbSScott Long 
75094618825SMark Johnston NETDUMP_DEFINE(iflib);
75194618825SMark Johnston 
7524c7070dbSScott Long #ifdef DEV_NETMAP
7534c7070dbSScott Long #include <sys/selinfo.h>
7544c7070dbSScott Long #include <net/netmap.h>
7554c7070dbSScott Long #include <dev/netmap/netmap_kern.h>
7564c7070dbSScott Long 
7574c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1);
7584c7070dbSScott Long 
7592d873474SStephen Hurd static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init);
7602d873474SStephen Hurd 
7614c7070dbSScott Long /*
7624c7070dbSScott Long  * device-specific sysctl variables:
7634c7070dbSScott Long  *
76491d546a0SConrad Meyer  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
7654c7070dbSScott Long  *	During regular operations the CRC is stripped, but on some
7664c7070dbSScott Long  *	hardware reception of frames not multiple of 64 is slower,
7674c7070dbSScott Long  *	so using crcstrip=0 helps in benchmarks.
7684c7070dbSScott Long  *
76991d546a0SConrad Meyer  * iflib_rx_miss, iflib_rx_miss_bufs:
7704c7070dbSScott Long  *	count packets that might be missed due to lost interrupts.
7714c7070dbSScott Long  */
7724c7070dbSScott Long SYSCTL_DECL(_dev_netmap);
7734c7070dbSScott Long /*
7744c7070dbSScott Long  * The xl driver by default strips CRCs and we do not override it.
7754c7070dbSScott Long  */
7764c7070dbSScott Long 
7774c7070dbSScott Long int iflib_crcstrip = 1;
7784c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
7794c7070dbSScott Long     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames");
7804c7070dbSScott Long 
7814c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs;
7824c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
7834c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr");
78491d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
7854c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs");
7864c7070dbSScott Long 
7874c7070dbSScott Long /*
7884c7070dbSScott Long  * Register/unregister. We are already under netmap lock.
7894c7070dbSScott Long  * Only called on the first register or the last unregister.
7904c7070dbSScott Long  */
7914c7070dbSScott Long static int
7924c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff)
7934c7070dbSScott Long {
7944c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
7954c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
79695246abbSSean Bruno 	int status;
7974c7070dbSScott Long 
7984c7070dbSScott Long 	CTX_LOCK(ctx);
7994c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
8004c7070dbSScott Long 
8014c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
8024c7070dbSScott Long 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
8034c7070dbSScott Long 
8044c7070dbSScott Long 	if (!CTX_IS_VF(ctx))
8051248952aSSean Bruno 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
8064c7070dbSScott Long 
8074c7070dbSScott Long 	/* enable or disable flags and callbacks in na and ifp */
8084c7070dbSScott Long 	if (onoff) {
8094c7070dbSScott Long 		nm_set_native_flags(na);
8104c7070dbSScott Long 	} else {
8114c7070dbSScott Long 		nm_clear_native_flags(na);
8124c7070dbSScott Long 	}
81395246abbSSean Bruno 	iflib_stop(ctx);
81495246abbSSean Bruno 	iflib_init_locked(ctx);
8151248952aSSean Bruno 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
81695246abbSSean Bruno 	status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1;
81795246abbSSean Bruno 	if (status)
81895246abbSSean Bruno 		nm_clear_native_flags(na);
8194c7070dbSScott Long 	CTX_UNLOCK(ctx);
82095246abbSSean Bruno 	return (status);
8214c7070dbSScott Long }
8224c7070dbSScott Long 
8232d873474SStephen Hurd static int
8242d873474SStephen Hurd netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init)
8252d873474SStephen Hurd {
8262d873474SStephen Hurd 	struct netmap_adapter *na = kring->na;
8272d873474SStephen Hurd 	u_int const lim = kring->nkr_num_slots - 1;
8282d873474SStephen Hurd 	u_int head = kring->rhead;
8292d873474SStephen Hurd 	struct netmap_ring *ring = kring->ring;
8302d873474SStephen Hurd 	bus_dmamap_t *map;
8312d873474SStephen Hurd 	struct if_rxd_update iru;
8322d873474SStephen Hurd 	if_ctx_t ctx = rxq->ifr_ctx;
8332d873474SStephen Hurd 	iflib_fl_t fl = &rxq->ifr_fl[0];
8342d873474SStephen Hurd 	uint32_t refill_pidx, nic_i;
8352d873474SStephen Hurd 
8362d873474SStephen Hurd 	if (nm_i == head && __predict_true(!init))
8372d873474SStephen Hurd 		return 0;
8382d873474SStephen Hurd 	iru_init(&iru, rxq, 0 /* flid */);
8392d873474SStephen Hurd 	map = fl->ifl_sds.ifsd_map;
8402d873474SStephen Hurd 	refill_pidx = netmap_idx_k2n(kring, nm_i);
8412d873474SStephen Hurd 	/*
8422d873474SStephen Hurd 	 * IMPORTANT: we must leave one free slot in the ring,
8432d873474SStephen Hurd 	 * so move head back by one unit
8442d873474SStephen Hurd 	 */
8452d873474SStephen Hurd 	head = nm_prev(head, lim);
8461ae4848cSMatt Macy 	nic_i = UINT_MAX;
8472d873474SStephen Hurd 	while (nm_i != head) {
8482d873474SStephen Hurd 		for (int tmp_pidx = 0; tmp_pidx < IFLIB_MAX_RX_REFRESH && nm_i != head; tmp_pidx++) {
8492d873474SStephen Hurd 			struct netmap_slot *slot = &ring->slot[nm_i];
8502d873474SStephen Hurd 			void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[tmp_pidx]);
8512d873474SStephen Hurd 			uint32_t nic_i_dma = refill_pidx;
8522d873474SStephen Hurd 			nic_i = netmap_idx_k2n(kring, nm_i);
8532d873474SStephen Hurd 
8542d873474SStephen Hurd 			MPASS(tmp_pidx < IFLIB_MAX_RX_REFRESH);
8552d873474SStephen Hurd 
8562d873474SStephen Hurd 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
8572d873474SStephen Hurd 			        return netmap_ring_reinit(kring);
8582d873474SStephen Hurd 
8592d873474SStephen Hurd 			fl->ifl_vm_addrs[tmp_pidx] = addr;
8602d873474SStephen Hurd 			if (__predict_false(init) && map) {
8612d873474SStephen Hurd 				netmap_load_map(na, fl->ifl_ifdi->idi_tag, map[nic_i], addr);
8622d873474SStephen Hurd 			} else if (map && (slot->flags & NS_BUF_CHANGED)) {
8632d873474SStephen Hurd 				/* buffer has changed, reload map */
8642d873474SStephen Hurd 				netmap_reload_map(na, fl->ifl_ifdi->idi_tag, map[nic_i], addr);
8652d873474SStephen Hurd 			}
8662d873474SStephen Hurd 			slot->flags &= ~NS_BUF_CHANGED;
8672d873474SStephen Hurd 
8682d873474SStephen Hurd 			nm_i = nm_next(nm_i, lim);
8692d873474SStephen Hurd 			fl->ifl_rxd_idxs[tmp_pidx] = nic_i = nm_next(nic_i, lim);
8702d873474SStephen Hurd 			if (nm_i != head && tmp_pidx < IFLIB_MAX_RX_REFRESH-1)
8712d873474SStephen Hurd 				continue;
8722d873474SStephen Hurd 
8732d873474SStephen Hurd 			iru.iru_pidx = refill_pidx;
8742d873474SStephen Hurd 			iru.iru_count = tmp_pidx+1;
8752d873474SStephen Hurd 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
8762d873474SStephen Hurd 
8772d873474SStephen Hurd 			refill_pidx = nic_i;
8782d873474SStephen Hurd 			if (map == NULL)
8792d873474SStephen Hurd 				continue;
8802d873474SStephen Hurd 
8812d873474SStephen Hurd 			for (int n = 0; n < iru.iru_count; n++) {
8822d873474SStephen Hurd 				bus_dmamap_sync(fl->ifl_ifdi->idi_tag, map[nic_i_dma],
8832d873474SStephen Hurd 						BUS_DMASYNC_PREREAD);
8842d873474SStephen Hurd 				/* XXX - change this to not use the netmap func*/
8852d873474SStephen Hurd 				nic_i_dma = nm_next(nic_i_dma, lim);
8862d873474SStephen Hurd 			}
8872d873474SStephen Hurd 		}
8882d873474SStephen Hurd 	}
8892d873474SStephen Hurd 	kring->nr_hwcur = head;
8902d873474SStephen Hurd 
8912d873474SStephen Hurd 	if (map)
8922d873474SStephen Hurd 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
8932d873474SStephen Hurd 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
8941ae4848cSMatt Macy 	if (__predict_true(nic_i != UINT_MAX))
8952d873474SStephen Hurd 		ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
8962d873474SStephen Hurd 	return (0);
8972d873474SStephen Hurd }
8982d873474SStephen Hurd 
8994c7070dbSScott Long /*
9004c7070dbSScott Long  * Reconcile kernel and user view of the transmit ring.
9014c7070dbSScott Long  *
9024c7070dbSScott Long  * All information is in the kring.
9034c7070dbSScott Long  * Userspace wants to send packets up to the one before kring->rhead,
9044c7070dbSScott Long  * kernel knows kring->nr_hwcur is the first unsent packet.
9054c7070dbSScott Long  *
9064c7070dbSScott Long  * Here we push packets out (as many as possible), and possibly
9074c7070dbSScott Long  * reclaim buffers from previously completed transmission.
9084c7070dbSScott Long  *
9094c7070dbSScott Long  * The caller (netmap) guarantees that there is only one instance
9104c7070dbSScott Long  * running at any time. Any interference with other driver
9114c7070dbSScott Long  * methods should be handled by the individual drivers.
9124c7070dbSScott Long  */
9134c7070dbSScott Long static int
9144c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags)
9154c7070dbSScott Long {
9164c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
9174c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
9184c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
9194c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
9204c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
9214c7070dbSScott Long 	u_int n;
9224c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
9234c7070dbSScott Long 	u_int const head = kring->rhead;
9244c7070dbSScott Long 	struct if_pkt_info pi;
9254c7070dbSScott Long 
9264c7070dbSScott Long 	/*
9274c7070dbSScott Long 	 * interrupts on every tx packet are expensive so request
9284c7070dbSScott Long 	 * them every half ring, or where NS_REPORT is set
9294c7070dbSScott Long 	 */
9304c7070dbSScott Long 	u_int report_frequency = kring->nkr_num_slots >> 1;
9314c7070dbSScott Long 	/* device-specific */
9324c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
9334c7070dbSScott Long 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
9344c7070dbSScott Long 
93595246abbSSean Bruno 	if (txq->ift_sds.ifsd_map)
9364c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
9374c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9384c7070dbSScott Long 
9394c7070dbSScott Long 
9404c7070dbSScott Long 	/*
9414c7070dbSScott Long 	 * First part: process new packets to send.
9424c7070dbSScott Long 	 * nm_i is the current index in the netmap ring,
9434c7070dbSScott Long 	 * nic_i is the corresponding index in the NIC ring.
9444c7070dbSScott Long 	 *
9454c7070dbSScott Long 	 * If we have packets to send (nm_i != head)
9464c7070dbSScott Long 	 * iterate over the netmap ring, fetch length and update
9474c7070dbSScott Long 	 * the corresponding slot in the NIC ring. Some drivers also
9484c7070dbSScott Long 	 * need to update the buffer's physical address in the NIC slot
9494c7070dbSScott Long 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
9504c7070dbSScott Long 	 *
9514c7070dbSScott Long 	 * The netmap_reload_map() calls is especially expensive,
9524c7070dbSScott Long 	 * even when (as in this case) the tag is 0, so do only
9534c7070dbSScott Long 	 * when the buffer has actually changed.
9544c7070dbSScott Long 	 *
9554c7070dbSScott Long 	 * If possible do not set the report/intr bit on all slots,
9564c7070dbSScott Long 	 * but only a few times per ring or when NS_REPORT is set.
9574c7070dbSScott Long 	 *
9584c7070dbSScott Long 	 * Finally, on 10G and faster drivers, it might be useful
9594c7070dbSScott Long 	 * to prefetch the next slot and txr entry.
9604c7070dbSScott Long 	 */
9614c7070dbSScott Long 
9622d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, kring->nr_hwcur);
963*5ee36c68SStephen Hurd 	if (nm_i != head) {	/* we have new packets to send */
96495246abbSSean Bruno 		pkt_info_zero(&pi);
96595246abbSSean Bruno 		pi.ipi_segs = txq->ift_segs;
96695246abbSSean Bruno 		pi.ipi_qsidx = kring->ring_id;
9674c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
9684c7070dbSScott Long 
9694c7070dbSScott Long 		__builtin_prefetch(&ring->slot[nm_i]);
9704c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
97195246abbSSean Bruno 		if (txq->ift_sds.ifsd_map)
9724c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
9734c7070dbSScott Long 
9744c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
9754c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
9764c7070dbSScott Long 			u_int len = slot->len;
9770a1b74a3SSean Bruno 			uint64_t paddr;
9784c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
9794c7070dbSScott Long 			int flags = (slot->flags & NS_REPORT ||
9804c7070dbSScott Long 				nic_i == 0 || nic_i == report_frequency) ?
9814c7070dbSScott Long 				IPI_TX_INTR : 0;
9824c7070dbSScott Long 
9834c7070dbSScott Long 			/* device-specific */
98495246abbSSean Bruno 			pi.ipi_len = len;
98595246abbSSean Bruno 			pi.ipi_segs[0].ds_addr = paddr;
98695246abbSSean Bruno 			pi.ipi_segs[0].ds_len = len;
98795246abbSSean Bruno 			pi.ipi_nsegs = 1;
98895246abbSSean Bruno 			pi.ipi_ndescs = 0;
9894c7070dbSScott Long 			pi.ipi_pidx = nic_i;
9904c7070dbSScott Long 			pi.ipi_flags = flags;
9914c7070dbSScott Long 
9924c7070dbSScott Long 			/* Fill the slot in the NIC ring. */
9934c7070dbSScott Long 			ctx->isc_txd_encap(ctx->ifc_softc, &pi);
9944c7070dbSScott Long 
9954c7070dbSScott Long 			/* prefetch for next round */
9964c7070dbSScott Long 			__builtin_prefetch(&ring->slot[nm_i + 1]);
9974c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
99895246abbSSean Bruno 			if (txq->ift_sds.ifsd_map) {
9994c7070dbSScott Long 				__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
10004c7070dbSScott Long 
10014c7070dbSScott Long 				NM_CHECK_ADDR_LEN(na, addr, len);
10024c7070dbSScott Long 
10034c7070dbSScott Long 				if (slot->flags & NS_BUF_CHANGED) {
10044c7070dbSScott Long 					/* buffer has changed, reload map */
10054c7070dbSScott Long 					netmap_reload_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[nic_i], addr);
10064c7070dbSScott Long 				}
10074c7070dbSScott Long 				/* make sure changes to the buffer are synced */
10084c7070dbSScott Long 				bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_sds.ifsd_map[nic_i],
10094c7070dbSScott Long 						BUS_DMASYNC_PREWRITE);
101095246abbSSean Bruno 			}
101195246abbSSean Bruno 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
10124c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
10134c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
10144c7070dbSScott Long 		}
10154c7070dbSScott Long 		kring->nr_hwcur = head;
10164c7070dbSScott Long 
10174c7070dbSScott Long 		/* synchronize the NIC ring */
101895246abbSSean Bruno 		if (txq->ift_sds.ifsd_map)
10194c7070dbSScott Long 			bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
10204c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
10214c7070dbSScott Long 
10224c7070dbSScott Long 		/* (re)start the tx unit up to slot nic_i (excluded) */
10234c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
10244c7070dbSScott Long 	}
10254c7070dbSScott Long 
10264c7070dbSScott Long 	/*
10274c7070dbSScott Long 	 * Second part: reclaim buffers for completed transmissions.
1028*5ee36c68SStephen Hurd 	 *
1029*5ee36c68SStephen Hurd 	 * If there are unclaimed buffers, attempt to reclaim them.
1030*5ee36c68SStephen Hurd 	 * If none are reclaimed, and TX IRQs are not in use, do an initial
1031*5ee36c68SStephen Hurd 	 * minimal delay, then trigger the tx handler which will spin in the
1032*5ee36c68SStephen Hurd 	 * group task queue.
10334c7070dbSScott Long 	 */
1034*5ee36c68SStephen Hurd 	if (kring->nr_hwtail != nm_prev(head, lim)) {
10354c7070dbSScott Long 		if (iflib_tx_credits_update(ctx, txq)) {
10364c7070dbSScott Long 			/* some tx completed, increment avail */
10374c7070dbSScott Long 			nic_i = txq->ift_cidx_processed;
10384c7070dbSScott Long 			kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
10394c7070dbSScott Long 		}
1040*5ee36c68SStephen Hurd 		else {
1041*5ee36c68SStephen Hurd 			if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) {
1042*5ee36c68SStephen Hurd 				DELAY(1);
1043*5ee36c68SStephen Hurd 				GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txq->ift_id].ift_task);
1044*5ee36c68SStephen Hurd 			}
1045*5ee36c68SStephen Hurd 		}
1046*5ee36c68SStephen Hurd 	}
10474c7070dbSScott Long 	return (0);
10484c7070dbSScott Long }
10494c7070dbSScott Long 
10504c7070dbSScott Long /*
10514c7070dbSScott Long  * Reconcile kernel and user view of the receive ring.
10524c7070dbSScott Long  * Same as for the txsync, this routine must be efficient.
10534c7070dbSScott Long  * The caller guarantees a single invocations, but races against
10544c7070dbSScott Long  * the rest of the driver should be handled here.
10554c7070dbSScott Long  *
10564c7070dbSScott Long  * On call, kring->rhead is the first packet that userspace wants
10574c7070dbSScott Long  * to keep, and kring->rcur is the wakeup point.
10584c7070dbSScott Long  * The kernel has previously reported packets up to kring->rtail.
10594c7070dbSScott Long  *
10604c7070dbSScott Long  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
10614c7070dbSScott Long  * of whether or not we received an interrupt.
10624c7070dbSScott Long  */
10634c7070dbSScott Long static int
10644c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
10654c7070dbSScott Long {
10664c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
10674c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
106895246abbSSean Bruno 	uint32_t nm_i;	/* index into the netmap ring */
10692d873474SStephen Hurd 	uint32_t nic_i;	/* index into the NIC ring */
10704c7070dbSScott Long 	u_int i, n;
10714c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
10722d873474SStephen Hurd 	u_int const head = netmap_idx_n2k(kring, kring->rhead);
10734c7070dbSScott Long 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
1074ab2e3f79SStephen Hurd 	struct if_rxd_info ri;
107595246abbSSean Bruno 
107695246abbSSean Bruno 	struct ifnet *ifp = na->ifp;
10774c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
10784c7070dbSScott Long 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
10794c7070dbSScott Long 	iflib_fl_t fl = rxq->ifr_fl;
10804c7070dbSScott Long 	if (head > lim)
10814c7070dbSScott Long 		return netmap_ring_reinit(kring);
10824c7070dbSScott Long 
10834c7070dbSScott Long 	/* XXX check sync modes */
108495246abbSSean Bruno 	for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) {
108595246abbSSean Bruno 		if (fl->ifl_sds.ifsd_map == NULL)
108695246abbSSean Bruno 			continue;
10874c7070dbSScott Long 		bus_dmamap_sync(rxq->ifr_fl[i].ifl_desc_tag, fl->ifl_ifdi->idi_map,
10884c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
108995246abbSSean Bruno 	}
10904c7070dbSScott Long 	/*
10914c7070dbSScott Long 	 * First part: import newly received packets.
10924c7070dbSScott Long 	 *
10934c7070dbSScott Long 	 * nm_i is the index of the next free slot in the netmap ring,
10944c7070dbSScott Long 	 * nic_i is the index of the next received packet in the NIC ring,
10954c7070dbSScott Long 	 * and they may differ in case if_init() has been called while
10964c7070dbSScott Long 	 * in netmap mode. For the receive ring we have
10974c7070dbSScott Long 	 *
10984c7070dbSScott Long 	 *	nic_i = rxr->next_check;
10994c7070dbSScott Long 	 *	nm_i = kring->nr_hwtail (previous)
11004c7070dbSScott Long 	 * and
11014c7070dbSScott Long 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
11024c7070dbSScott Long 	 *
11034c7070dbSScott Long 	 * rxr->next_check is set to 0 on a ring reinit
11044c7070dbSScott Long 	 */
11054c7070dbSScott Long 	if (netmap_no_pendintr || force_update) {
11064c7070dbSScott Long 		int crclen = iflib_crcstrip ? 0 : 4;
11074c7070dbSScott Long 		int error, avail;
11084c7070dbSScott Long 
11092d873474SStephen Hurd 		for (i = 0; i < rxq->ifr_nfl; i++) {
11102d873474SStephen Hurd 			fl = &rxq->ifr_fl[i];
11114c7070dbSScott Long 			nic_i = fl->ifl_cidx;
11124c7070dbSScott Long 			nm_i = netmap_idx_n2k(kring, nic_i);
111395246abbSSean Bruno 			avail = iflib_rxd_avail(ctx, rxq, nic_i, USHRT_MAX);
11144c7070dbSScott Long 			for (n = 0; avail > 0; n++, avail--) {
1115ab2e3f79SStephen Hurd 				rxd_info_zero(&ri);
1116ab2e3f79SStephen Hurd 				ri.iri_frags = rxq->ifr_frags;
1117ab2e3f79SStephen Hurd 				ri.iri_qsidx = kring->ring_id;
1118ab2e3f79SStephen Hurd 				ri.iri_ifp = ctx->ifc_ifp;
1119ab2e3f79SStephen Hurd 				ri.iri_cidx = nic_i;
112095246abbSSean Bruno 
1121ab2e3f79SStephen Hurd 				error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
1122ab2e3f79SStephen Hurd 				ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen;
11237cb7c6e3SNavdeep Parhar 				ring->slot[nm_i].flags = 0;
112495246abbSSean Bruno 				if (fl->ifl_sds.ifsd_map)
11254c7070dbSScott Long 					bus_dmamap_sync(fl->ifl_ifdi->idi_tag,
1126e035717eSSean Bruno 							fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
11274c7070dbSScott Long 				nm_i = nm_next(nm_i, lim);
11284c7070dbSScott Long 				nic_i = nm_next(nic_i, lim);
11294c7070dbSScott Long 			}
11304c7070dbSScott Long 			if (n) { /* update the state variables */
11314c7070dbSScott Long 				if (netmap_no_pendintr && !force_update) {
11324c7070dbSScott Long 					/* diagnostics */
11334c7070dbSScott Long 					iflib_rx_miss ++;
11344c7070dbSScott Long 					iflib_rx_miss_bufs += n;
11354c7070dbSScott Long 				}
11364c7070dbSScott Long 				fl->ifl_cidx = nic_i;
11372d873474SStephen Hurd 				kring->nr_hwtail = netmap_idx_k2n(kring, nm_i);
11384c7070dbSScott Long 			}
11394c7070dbSScott Long 			kring->nr_kflags &= ~NKR_PENDINTR;
11404c7070dbSScott Long 		}
11414c7070dbSScott Long 	}
11424c7070dbSScott Long 	/*
11434c7070dbSScott Long 	 * Second part: skip past packets that userspace has released.
11444c7070dbSScott Long 	 * (kring->nr_hwcur to head excluded),
11454c7070dbSScott Long 	 * and make the buffers available for reception.
11464c7070dbSScott Long 	 * As usual nm_i is the index in the netmap ring,
11474c7070dbSScott Long 	 * nic_i is the index in the NIC ring, and
11484c7070dbSScott Long 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
11494c7070dbSScott Long 	 */
11504c7070dbSScott Long 	/* XXX not sure how this will work with multiple free lists */
11512d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, kring->nr_hwcur);
115295246abbSSean Bruno 
11532d873474SStephen Hurd 	return (netmap_fl_refill(rxq, kring, nm_i, false));
11544c7070dbSScott Long }
11554c7070dbSScott Long 
115695246abbSSean Bruno static void
115795246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff)
115895246abbSSean Bruno {
115995246abbSSean Bruno 	struct ifnet *ifp = na->ifp;
116095246abbSSean Bruno 	if_ctx_t ctx = ifp->if_softc;
116195246abbSSean Bruno 
1162ab2e3f79SStephen Hurd 	CTX_LOCK(ctx);
116395246abbSSean Bruno 	if (onoff) {
116495246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
116595246abbSSean Bruno 	} else {
116695246abbSSean Bruno 		IFDI_INTR_DISABLE(ctx);
116795246abbSSean Bruno 	}
1168ab2e3f79SStephen Hurd 	CTX_UNLOCK(ctx);
116995246abbSSean Bruno }
117095246abbSSean Bruno 
117195246abbSSean Bruno 
11724c7070dbSScott Long static int
11734c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx)
11744c7070dbSScott Long {
11754c7070dbSScott Long 	struct netmap_adapter na;
117623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
11774c7070dbSScott Long 
11784c7070dbSScott Long 	bzero(&na, sizeof(na));
11794c7070dbSScott Long 
11804c7070dbSScott Long 	na.ifp = ctx->ifc_ifp;
11814c7070dbSScott Long 	na.na_flags = NAF_BDG_MAYSLEEP;
11824c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
11834c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
11844c7070dbSScott Long 
118523ac9029SStephen Hurd 	na.num_tx_desc = scctx->isc_ntxd[0];
118623ac9029SStephen Hurd 	na.num_rx_desc = scctx->isc_nrxd[0];
11874c7070dbSScott Long 	na.nm_txsync = iflib_netmap_txsync;
11884c7070dbSScott Long 	na.nm_rxsync = iflib_netmap_rxsync;
11894c7070dbSScott Long 	na.nm_register = iflib_netmap_register;
119095246abbSSean Bruno 	na.nm_intr = iflib_netmap_intr;
11914c7070dbSScott Long 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
11924c7070dbSScott Long 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
11934c7070dbSScott Long 	return (netmap_attach(&na));
11944c7070dbSScott Long }
11954c7070dbSScott Long 
11964c7070dbSScott Long static void
11974c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
11984c7070dbSScott Long {
11994c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
12004c7070dbSScott Long 	struct netmap_slot *slot;
12014c7070dbSScott Long 
12024c7070dbSScott Long 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1203e099b90bSPedro F. Giffuni 	if (slot == NULL)
12044c7070dbSScott Long 		return;
120595246abbSSean Bruno 	if (txq->ift_sds.ifsd_map == NULL)
120695246abbSSean Bruno 		return;
12074c7070dbSScott Long 
120823ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
12094c7070dbSScott Long 
12104c7070dbSScott Long 		/*
12114c7070dbSScott Long 		 * In netmap mode, set the map for the packet buffer.
12124c7070dbSScott Long 		 * NOTE: Some drivers (not this one) also need to set
12134c7070dbSScott Long 		 * the physical buffer address in the NIC ring.
12144c7070dbSScott Long 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
12154c7070dbSScott Long 		 * netmap slot index, si
12164c7070dbSScott Long 		 */
12172ff91c17SVincenzo Maffione 		int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i);
12184c7070dbSScott Long 		netmap_load_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[i], NMB(na, slot + si));
12194c7070dbSScott Long 	}
12204c7070dbSScott Long }
12212d873474SStephen Hurd 
12224c7070dbSScott Long static void
12234c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
12244c7070dbSScott Long {
12254c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
12262ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[rxq->ifr_id];
12274c7070dbSScott Long 	struct netmap_slot *slot;
12282d873474SStephen Hurd 	uint32_t nm_i;
12294c7070dbSScott Long 
12304c7070dbSScott Long 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1231e099b90bSPedro F. Giffuni 	if (slot == NULL)
12324c7070dbSScott Long 		return;
12332d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, 0);
12342d873474SStephen Hurd 	netmap_fl_refill(rxq, kring, nm_i, true);
12354c7070dbSScott Long }
12364c7070dbSScott Long 
12374c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp)
12384c7070dbSScott Long 
12394c7070dbSScott Long #else
12404c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq)
12414c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq)
12424c7070dbSScott Long #define iflib_netmap_detach(ifp)
12434c7070dbSScott Long 
12444c7070dbSScott Long #define iflib_netmap_attach(ctx) (0)
12454c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0)
124695246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0)
12474c7070dbSScott Long 
12484c7070dbSScott Long #endif
12494c7070dbSScott Long 
12504c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
12514c7070dbSScott Long static __inline void
12524c7070dbSScott Long prefetch(void *x)
12534c7070dbSScott Long {
12544c7070dbSScott Long 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12554c7070dbSScott Long }
12563429c02fSStephen Hurd static __inline void
12573429c02fSStephen Hurd prefetch2cachelines(void *x)
12583429c02fSStephen Hurd {
12593429c02fSStephen Hurd 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12603429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128)
12613429c02fSStephen Hurd 	__asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long)))));
12623429c02fSStephen Hurd #endif
12633429c02fSStephen Hurd }
12644c7070dbSScott Long #else
12654c7070dbSScott Long #define prefetch(x)
12663429c02fSStephen Hurd #define prefetch2cachelines(x)
12674c7070dbSScott Long #endif
12684c7070dbSScott Long 
12694c7070dbSScott Long static void
127009f6ff4fSMatt Macy iflib_gen_mac(if_ctx_t ctx)
127109f6ff4fSMatt Macy {
127209f6ff4fSMatt Macy 	struct thread *td;
127309f6ff4fSMatt Macy 	struct ifnet *ifp;
127409f6ff4fSMatt Macy 	MD5_CTX mdctx;
127509f6ff4fSMatt Macy 	char uuid[HOSTUUIDLEN+1];
127609f6ff4fSMatt Macy 	char buf[HOSTUUIDLEN+16];
127709f6ff4fSMatt Macy 	uint8_t *mac;
127809f6ff4fSMatt Macy 	unsigned char digest[16];
127909f6ff4fSMatt Macy 
128009f6ff4fSMatt Macy 	td = curthread;
128109f6ff4fSMatt Macy 	ifp = ctx->ifc_ifp;
128209f6ff4fSMatt Macy 	mac = ctx->ifc_mac;
128309f6ff4fSMatt Macy 	uuid[HOSTUUIDLEN] = 0;
128409f6ff4fSMatt Macy 	bcopy(td->td_ucred->cr_prison->pr_hostuuid, uuid, HOSTUUIDLEN);
128509f6ff4fSMatt Macy 	snprintf(buf, HOSTUUIDLEN+16, "%s-%s", uuid, device_get_nameunit(ctx->ifc_dev));
128609f6ff4fSMatt Macy 	/*
128709f6ff4fSMatt Macy 	 * Generate a pseudo-random, deterministic MAC
128809f6ff4fSMatt Macy 	 * address based on the UUID and unit number.
128909f6ff4fSMatt Macy 	 * The FreeBSD Foundation OUI of 58-9C-FC is used.
129009f6ff4fSMatt Macy 	 */
129109f6ff4fSMatt Macy 	MD5Init(&mdctx);
129209f6ff4fSMatt Macy 	MD5Update(&mdctx, buf, strlen(buf));
129309f6ff4fSMatt Macy 	MD5Final(digest, &mdctx);
129409f6ff4fSMatt Macy 
129509f6ff4fSMatt Macy 	mac[0] = 0x58;
129609f6ff4fSMatt Macy 	mac[1] = 0x9C;
129709f6ff4fSMatt Macy 	mac[2] = 0xFC;
129809f6ff4fSMatt Macy 	mac[3] = digest[0];
129909f6ff4fSMatt Macy 	mac[4] = digest[1];
130009f6ff4fSMatt Macy 	mac[5] = digest[2];
130109f6ff4fSMatt Macy }
130209f6ff4fSMatt Macy 
130309f6ff4fSMatt Macy static void
130410e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid)
130510e0d938SStephen Hurd {
130610e0d938SStephen Hurd 	iflib_fl_t fl;
130710e0d938SStephen Hurd 
130810e0d938SStephen Hurd 	fl = &rxq->ifr_fl[flid];
130910e0d938SStephen Hurd 	iru->iru_paddrs = fl->ifl_bus_addrs;
131010e0d938SStephen Hurd 	iru->iru_vaddrs = &fl->ifl_vm_addrs[0];
131110e0d938SStephen Hurd 	iru->iru_idxs = fl->ifl_rxd_idxs;
131210e0d938SStephen Hurd 	iru->iru_qsidx = rxq->ifr_id;
131310e0d938SStephen Hurd 	iru->iru_buf_size = fl->ifl_buf_size;
131410e0d938SStephen Hurd 	iru->iru_flidx = fl->ifl_id;
131510e0d938SStephen Hurd }
131610e0d938SStephen Hurd 
131710e0d938SStephen Hurd static void
13184c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
13194c7070dbSScott Long {
13204c7070dbSScott Long 	if (err)
13214c7070dbSScott Long 		return;
13224c7070dbSScott Long 	*(bus_addr_t *) arg = segs[0].ds_addr;
13234c7070dbSScott Long }
13244c7070dbSScott Long 
13254c7070dbSScott Long int
13264c7070dbSScott Long iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
13274c7070dbSScott Long {
13284c7070dbSScott Long 	int err;
13294c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
13304c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
13314c7070dbSScott Long 
13324c7070dbSScott Long 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
13334c7070dbSScott Long 
13344c7070dbSScott Long 	err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
13354c7070dbSScott Long 				sctx->isc_q_align, 0,	/* alignment, bounds */
13364c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* lowaddr */
13374c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* highaddr */
13384c7070dbSScott Long 				NULL, NULL,		/* filter, filterarg */
13394c7070dbSScott Long 				size,			/* maxsize */
13404c7070dbSScott Long 				1,			/* nsegments */
13414c7070dbSScott Long 				size,			/* maxsegsize */
13424c7070dbSScott Long 				BUS_DMA_ALLOCNOW,	/* flags */
13434c7070dbSScott Long 				NULL,			/* lockfunc */
13444c7070dbSScott Long 				NULL,			/* lockarg */
13454c7070dbSScott Long 				&dma->idi_tag);
13464c7070dbSScott Long 	if (err) {
13474c7070dbSScott Long 		device_printf(dev,
13484c7070dbSScott Long 		    "%s: bus_dma_tag_create failed: %d\n",
13494c7070dbSScott Long 		    __func__, err);
13504c7070dbSScott Long 		goto fail_0;
13514c7070dbSScott Long 	}
13524c7070dbSScott Long 
13534c7070dbSScott Long 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
13544c7070dbSScott Long 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
13554c7070dbSScott Long 	if (err) {
13564c7070dbSScott Long 		device_printf(dev,
13574c7070dbSScott Long 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
13584c7070dbSScott Long 		    __func__, (uintmax_t)size, err);
13594c7070dbSScott Long 		goto fail_1;
13604c7070dbSScott Long 	}
13614c7070dbSScott Long 
13624c7070dbSScott Long 	dma->idi_paddr = IF_BAD_DMA;
13634c7070dbSScott Long 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
13644c7070dbSScott Long 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
13654c7070dbSScott Long 	if (err || dma->idi_paddr == IF_BAD_DMA) {
13664c7070dbSScott Long 		device_printf(dev,
13674c7070dbSScott Long 		    "%s: bus_dmamap_load failed: %d\n",
13684c7070dbSScott Long 		    __func__, err);
13694c7070dbSScott Long 		goto fail_2;
13704c7070dbSScott Long 	}
13714c7070dbSScott Long 
13724c7070dbSScott Long 	dma->idi_size = size;
13734c7070dbSScott Long 	return (0);
13744c7070dbSScott Long 
13754c7070dbSScott Long fail_2:
13764c7070dbSScott Long 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
13774c7070dbSScott Long fail_1:
13784c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
13794c7070dbSScott Long fail_0:
13804c7070dbSScott Long 	dma->idi_tag = NULL;
13814c7070dbSScott Long 
13824c7070dbSScott Long 	return (err);
13834c7070dbSScott Long }
13844c7070dbSScott Long 
13854c7070dbSScott Long int
13864c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
13874c7070dbSScott Long {
13884c7070dbSScott Long 	int i, err;
13894c7070dbSScott Long 	iflib_dma_info_t *dmaiter;
13904c7070dbSScott Long 
13914c7070dbSScott Long 	dmaiter = dmalist;
13924c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++) {
13934c7070dbSScott Long 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
13944c7070dbSScott Long 			break;
13954c7070dbSScott Long 	}
13964c7070dbSScott Long 	if (err)
13974c7070dbSScott Long 		iflib_dma_free_multi(dmalist, i);
13984c7070dbSScott Long 	return (err);
13994c7070dbSScott Long }
14004c7070dbSScott Long 
14014c7070dbSScott Long void
14024c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma)
14034c7070dbSScott Long {
14044c7070dbSScott Long 	if (dma->idi_tag == NULL)
14054c7070dbSScott Long 		return;
14064c7070dbSScott Long 	if (dma->idi_paddr != IF_BAD_DMA) {
14074c7070dbSScott Long 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
14084c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
14094c7070dbSScott Long 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
14104c7070dbSScott Long 		dma->idi_paddr = IF_BAD_DMA;
14114c7070dbSScott Long 	}
14124c7070dbSScott Long 	if (dma->idi_vaddr != NULL) {
14134c7070dbSScott Long 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
14144c7070dbSScott Long 		dma->idi_vaddr = NULL;
14154c7070dbSScott Long 	}
14164c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
14174c7070dbSScott Long 	dma->idi_tag = NULL;
14184c7070dbSScott Long }
14194c7070dbSScott Long 
14204c7070dbSScott Long void
14214c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
14224c7070dbSScott Long {
14234c7070dbSScott Long 	int i;
14244c7070dbSScott Long 	iflib_dma_info_t *dmaiter = dmalist;
14254c7070dbSScott Long 
14264c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++)
14274c7070dbSScott Long 		iflib_dma_free(*dmaiter);
14284c7070dbSScott Long }
14294c7070dbSScott Long 
1430bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP
1431bd84f700SSean Bruno static const int iflib_started = 1;
1432bd84f700SSean Bruno #else
1433bd84f700SSean Bruno /*
1434bd84f700SSean Bruno  * We used to abuse the smp_started flag to decide if the queues have been
1435bd84f700SSean Bruno  * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()).
1436bd84f700SSean Bruno  * That gave bad races, since the SYSINIT() runs strictly after smp_started
1437bd84f700SSean Bruno  * is set.  Run a SYSINIT() strictly after that to just set a usable
1438bd84f700SSean Bruno  * completion flag.
1439bd84f700SSean Bruno  */
1440bd84f700SSean Bruno 
1441bd84f700SSean Bruno static int iflib_started;
1442bd84f700SSean Bruno 
1443bd84f700SSean Bruno static void
1444bd84f700SSean Bruno iflib_record_started(void *arg)
1445bd84f700SSean Bruno {
1446bd84f700SSean Bruno 	iflib_started = 1;
1447bd84f700SSean Bruno }
1448bd84f700SSean Bruno 
1449bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST,
1450bd84f700SSean Bruno 	iflib_record_started, NULL);
1451bd84f700SSean Bruno #endif
1452bd84f700SSean Bruno 
14534c7070dbSScott Long static int
14544c7070dbSScott Long iflib_fast_intr(void *arg)
14554c7070dbSScott Long {
14564c7070dbSScott Long 	iflib_filter_info_t info = arg;
14574c7070dbSScott Long 	struct grouptask *gtask = info->ifi_task;
145895246abbSSean Bruno 	if (!iflib_started)
145995246abbSSean Bruno 		return (FILTER_HANDLED);
146095246abbSSean Bruno 
146195246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
146295246abbSSean Bruno 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
146395246abbSSean Bruno 		return (FILTER_HANDLED);
146495246abbSSean Bruno 
146595246abbSSean Bruno 	GROUPTASK_ENQUEUE(gtask);
146695246abbSSean Bruno 	return (FILTER_HANDLED);
146795246abbSSean Bruno }
146895246abbSSean Bruno 
146995246abbSSean Bruno static int
147095246abbSSean Bruno iflib_fast_intr_rxtx(void *arg)
147195246abbSSean Bruno {
147295246abbSSean Bruno 	iflib_filter_info_t info = arg;
147395246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
147495246abbSSean Bruno 	iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
14751ae4848cSMatt Macy 	if_ctx_t ctx = NULL;;
147695246abbSSean Bruno 	int i, cidx;
147795246abbSSean Bruno 
147895246abbSSean Bruno 	if (!iflib_started)
147995246abbSSean Bruno 		return (FILTER_HANDLED);
148095246abbSSean Bruno 
148195246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
148295246abbSSean Bruno 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
148395246abbSSean Bruno 		return (FILTER_HANDLED);
148495246abbSSean Bruno 
14851ae4848cSMatt Macy 	MPASS(rxq->ifr_ntxqirq);
148695246abbSSean Bruno 	for (i = 0; i < rxq->ifr_ntxqirq; i++) {
148795246abbSSean Bruno 		qidx_t txqid = rxq->ifr_txqid[i];
148895246abbSSean Bruno 
1489ab2e3f79SStephen Hurd 		ctx = rxq->ifr_ctx;
1490ab2e3f79SStephen Hurd 
149195246abbSSean Bruno 		if (!ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false)) {
149295246abbSSean Bruno 			IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
149395246abbSSean Bruno 			continue;
149495246abbSSean Bruno 		}
149595246abbSSean Bruno 		GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
149695246abbSSean Bruno 	}
149795246abbSSean Bruno 	if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
149895246abbSSean Bruno 		cidx = rxq->ifr_cq_cidx;
149995246abbSSean Bruno 	else
150095246abbSSean Bruno 		cidx = rxq->ifr_fl[0].ifl_cidx;
150195246abbSSean Bruno 	if (iflib_rxd_avail(ctx, rxq, cidx, 1))
150295246abbSSean Bruno 		GROUPTASK_ENQUEUE(gtask);
150395246abbSSean Bruno 	else
150495246abbSSean Bruno 		IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
150595246abbSSean Bruno 	return (FILTER_HANDLED);
150695246abbSSean Bruno }
150795246abbSSean Bruno 
150895246abbSSean Bruno 
150995246abbSSean Bruno static int
151095246abbSSean Bruno iflib_fast_intr_ctx(void *arg)
151195246abbSSean Bruno {
151295246abbSSean Bruno 	iflib_filter_info_t info = arg;
151395246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
15144c7070dbSScott Long 
1515bd84f700SSean Bruno 	if (!iflib_started)
15161248952aSSean Bruno 		return (FILTER_HANDLED);
15171248952aSSean Bruno 
15184c7070dbSScott Long 	DBG_COUNTER_INC(fast_intrs);
15194c7070dbSScott Long 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
15204c7070dbSScott Long 		return (FILTER_HANDLED);
15214c7070dbSScott Long 
15224c7070dbSScott Long 	GROUPTASK_ENQUEUE(gtask);
15234c7070dbSScott Long 	return (FILTER_HANDLED);
15244c7070dbSScott Long }
15254c7070dbSScott Long 
15264c7070dbSScott Long static int
15274c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
15284c7070dbSScott Long 	driver_filter_t filter, driver_intr_t handler, void *arg,
15294c7070dbSScott Long 				 char *name)
15304c7070dbSScott Long {
15312b2fc973SSean Bruno 	int rc, flags;
15324c7070dbSScott Long 	struct resource *res;
15332b2fc973SSean Bruno 	void *tag = NULL;
15344c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
15354c7070dbSScott Long 
15362b2fc973SSean Bruno 	flags = RF_ACTIVE;
15372b2fc973SSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
15382b2fc973SSean Bruno 		flags |= RF_SHAREABLE;
15394c7070dbSScott Long 	MPASS(rid < 512);
15404c7070dbSScott Long 	irq->ii_rid = rid;
15412b2fc973SSean Bruno 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid, flags);
15424c7070dbSScott Long 	if (res == NULL) {
15434c7070dbSScott Long 		device_printf(dev,
15444c7070dbSScott Long 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
15454c7070dbSScott Long 		return (ENOMEM);
15464c7070dbSScott Long 	}
15474c7070dbSScott Long 	irq->ii_res = res;
15484c7070dbSScott Long 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
15494c7070dbSScott Long 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
15504c7070dbSScott Long 						filter, handler, arg, &tag);
15514c7070dbSScott Long 	if (rc != 0) {
15524c7070dbSScott Long 		device_printf(dev,
15534c7070dbSScott Long 		    "failed to setup interrupt for rid %d, name %s: %d\n",
15544c7070dbSScott Long 					  rid, name ? name : "unknown", rc);
15554c7070dbSScott Long 		return (rc);
15564c7070dbSScott Long 	} else if (name)
1557f454e7ebSJohn Baldwin 		bus_describe_intr(dev, res, tag, "%s", name);
15584c7070dbSScott Long 
15594c7070dbSScott Long 	irq->ii_tag = tag;
15604c7070dbSScott Long 	return (0);
15614c7070dbSScott Long }
15624c7070dbSScott Long 
15634c7070dbSScott Long 
15644c7070dbSScott Long /*********************************************************************
15654c7070dbSScott Long  *
15664c7070dbSScott Long  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
15674c7070dbSScott Long  *  the information needed to transmit a packet on the wire. This is
15684c7070dbSScott Long  *  called only once at attach, setup is done every reset.
15694c7070dbSScott Long  *
15704c7070dbSScott Long  **********************************************************************/
15714c7070dbSScott Long 
15724c7070dbSScott Long static int
15734c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq)
15744c7070dbSScott Long {
15754c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
15764c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
15774c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
15784c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
15794c7070dbSScott Long 	int err, nsegments, ntsosegments;
15804c7070dbSScott Long 
15814c7070dbSScott Long 	nsegments = scctx->isc_tx_nsegments;
15824c7070dbSScott Long 	ntsosegments = scctx->isc_tx_tso_segments_max;
158323ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[0] > 0);
158423ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
15854c7070dbSScott Long 	MPASS(nsegments > 0);
15864c7070dbSScott Long 	MPASS(ntsosegments > 0);
15874c7070dbSScott Long 	/*
15884c7070dbSScott Long 	 * Setup DMA descriptor areas.
15894c7070dbSScott Long 	 */
15904c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
15914c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
15924c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
15934c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
15944c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
15954c7070dbSScott Long 			       sctx->isc_tx_maxsize,		/* maxsize */
15964c7070dbSScott Long 			       nsegments,	/* nsegments */
15974c7070dbSScott Long 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
15984c7070dbSScott Long 			       0,			/* flags */
15994c7070dbSScott Long 			       NULL,			/* lockfunc */
16004c7070dbSScott Long 			       NULL,			/* lockfuncarg */
16014c7070dbSScott Long 			       &txq->ift_desc_tag))) {
16024c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
16039d0a88deSDimitry Andric 		device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n",
16049d0a88deSDimitry Andric 		    (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
16054c7070dbSScott Long 		goto fail;
16064c7070dbSScott Long 	}
16074c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
16084c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
16094c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
16104c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
16114c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
16124c7070dbSScott Long 			       scctx->isc_tx_tso_size_max,		/* maxsize */
16134c7070dbSScott Long 			       ntsosegments,	/* nsegments */
16144c7070dbSScott Long 			       scctx->isc_tx_tso_segsize_max,	/* maxsegsize */
16154c7070dbSScott Long 			       0,			/* flags */
16164c7070dbSScott Long 			       NULL,			/* lockfunc */
16174c7070dbSScott Long 			       NULL,			/* lockfuncarg */
16184c7070dbSScott Long 			       &txq->ift_tso_desc_tag))) {
16194c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX TSO DMA tag: %d\n", err);
16204c7070dbSScott Long 
16214c7070dbSScott Long 		goto fail;
16224c7070dbSScott Long 	}
16234c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_flags =
1624ac2fffa4SPedro F. Giffuni 	    (uint8_t *) malloc(sizeof(uint8_t) *
1625ac2fffa4SPedro F. Giffuni 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
16264c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
16274c7070dbSScott Long 		err = ENOMEM;
16284c7070dbSScott Long 		goto fail;
16294c7070dbSScott Long 	}
16304c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_m =
1631ac2fffa4SPedro F. Giffuni 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
1632ac2fffa4SPedro F. Giffuni 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
16334c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
16344c7070dbSScott Long 		err = ENOMEM;
16354c7070dbSScott Long 		goto fail;
16364c7070dbSScott Long 	}
16374c7070dbSScott Long 
16384c7070dbSScott Long         /* Create the descriptor buffer dma maps */
163995246abbSSean Bruno #if defined(ACPI_DMAR) || (! (defined(__i386__) || defined(__amd64__)))
16404c7070dbSScott Long 	if ((ctx->ifc_flags & IFC_DMAR) == 0)
16414c7070dbSScott Long 		return (0);
16424c7070dbSScott Long 
16434c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_map =
1644ac2fffa4SPedro F. Giffuni 	    (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
16454c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer map memory\n");
16464c7070dbSScott Long 		err = ENOMEM;
16474c7070dbSScott Long 		goto fail;
16484c7070dbSScott Long 	}
16494c7070dbSScott Long 
165023ac9029SStephen Hurd 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
16514c7070dbSScott Long 		err = bus_dmamap_create(txq->ift_desc_tag, 0, &txq->ift_sds.ifsd_map[i]);
16524c7070dbSScott Long 		if (err != 0) {
16534c7070dbSScott Long 			device_printf(dev, "Unable to create TX DMA map\n");
16544c7070dbSScott Long 			goto fail;
16554c7070dbSScott Long 		}
16564c7070dbSScott Long 	}
16574c7070dbSScott Long #endif
16584c7070dbSScott Long 	return (0);
16594c7070dbSScott Long fail:
16604c7070dbSScott Long 	/* We free all, it handles case where we are in the middle */
16614c7070dbSScott Long 	iflib_tx_structures_free(ctx);
16624c7070dbSScott Long 	return (err);
16634c7070dbSScott Long }
16644c7070dbSScott Long 
16654c7070dbSScott Long static void
16664c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
16674c7070dbSScott Long {
16684c7070dbSScott Long 	bus_dmamap_t map;
16694c7070dbSScott Long 
16704c7070dbSScott Long 	map = NULL;
16714c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL)
16724c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[i];
16734c7070dbSScott Long 	if (map != NULL) {
16744c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag, map);
16754c7070dbSScott Long 		bus_dmamap_destroy(txq->ift_desc_tag, map);
16764c7070dbSScott Long 		txq->ift_sds.ifsd_map[i] = NULL;
16774c7070dbSScott Long 	}
16784c7070dbSScott Long }
16794c7070dbSScott Long 
16804c7070dbSScott Long static void
16814c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq)
16824c7070dbSScott Long {
16834c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
16844c7070dbSScott Long 
168523ac9029SStephen Hurd 	for (int i = 0; i < txq->ift_size; i++)
16864c7070dbSScott Long 		iflib_txsd_destroy(ctx, txq, i);
16874c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
16884c7070dbSScott Long 		free(txq->ift_sds.ifsd_map, M_IFLIB);
16894c7070dbSScott Long 		txq->ift_sds.ifsd_map = NULL;
16904c7070dbSScott Long 	}
16914c7070dbSScott Long 	if (txq->ift_sds.ifsd_m != NULL) {
16924c7070dbSScott Long 		free(txq->ift_sds.ifsd_m, M_IFLIB);
16934c7070dbSScott Long 		txq->ift_sds.ifsd_m = NULL;
16944c7070dbSScott Long 	}
16954c7070dbSScott Long 	if (txq->ift_sds.ifsd_flags != NULL) {
16964c7070dbSScott Long 		free(txq->ift_sds.ifsd_flags, M_IFLIB);
16974c7070dbSScott Long 		txq->ift_sds.ifsd_flags = NULL;
16984c7070dbSScott Long 	}
16994c7070dbSScott Long 	if (txq->ift_desc_tag != NULL) {
17004c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_desc_tag);
17014c7070dbSScott Long 		txq->ift_desc_tag = NULL;
17024c7070dbSScott Long 	}
17034c7070dbSScott Long 	if (txq->ift_tso_desc_tag != NULL) {
17044c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_tso_desc_tag);
17054c7070dbSScott Long 		txq->ift_tso_desc_tag = NULL;
17064c7070dbSScott Long 	}
17074c7070dbSScott Long }
17084c7070dbSScott Long 
17094c7070dbSScott Long static void
17104c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
17114c7070dbSScott Long {
17124c7070dbSScott Long 	struct mbuf **mp;
17134c7070dbSScott Long 
17144c7070dbSScott Long 	mp = &txq->ift_sds.ifsd_m[i];
17154c7070dbSScott Long 	if (*mp == NULL)
17164c7070dbSScott Long 		return;
17174c7070dbSScott Long 
17184c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
17194c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag,
17204c7070dbSScott Long 				txq->ift_sds.ifsd_map[i],
17214c7070dbSScott Long 				BUS_DMASYNC_POSTWRITE);
17224c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag,
17234c7070dbSScott Long 				  txq->ift_sds.ifsd_map[i]);
17244c7070dbSScott Long 	}
172523ac9029SStephen Hurd 	m_free(*mp);
17264c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
17274c7070dbSScott Long 	*mp = NULL;
17284c7070dbSScott Long }
17294c7070dbSScott Long 
17304c7070dbSScott Long static int
17314c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq)
17324c7070dbSScott Long {
17334c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
173423ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
17354c7070dbSScott Long 	iflib_dma_info_t di;
17364c7070dbSScott Long 	int i;
17374c7070dbSScott Long 
17384c7070dbSScott Long 	/* Set number of descriptors available */
17394c7070dbSScott Long 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
174095246abbSSean Bruno 	/* XXX make configurable */
174195246abbSSean Bruno 	txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
17424c7070dbSScott Long 
17434c7070dbSScott Long 	/* Reset indices */
174495246abbSSean Bruno 	txq->ift_cidx_processed = 0;
174595246abbSSean Bruno 	txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
174623ac9029SStephen Hurd 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
17474c7070dbSScott Long 
17484c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
17494c7070dbSScott Long 		bzero((void *)di->idi_vaddr, di->idi_size);
17504c7070dbSScott Long 
17514c7070dbSScott Long 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
17524c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
17534c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
17544c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
17554c7070dbSScott Long 	return (0);
17564c7070dbSScott Long }
17574c7070dbSScott Long 
17584c7070dbSScott Long /*********************************************************************
17594c7070dbSScott Long  *
17604c7070dbSScott Long  *  Allocate memory for rx_buffer structures. Since we use one
17614c7070dbSScott Long  *  rx_buffer per received packet, the maximum number of rx_buffer's
17624c7070dbSScott Long  *  that we'll need is equal to the number of receive descriptors
17634c7070dbSScott Long  *  that we've allocated.
17644c7070dbSScott Long  *
17654c7070dbSScott Long  **********************************************************************/
17664c7070dbSScott Long static int
17674c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq)
17684c7070dbSScott Long {
17694c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
17704c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
177123ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
17724c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
17734c7070dbSScott Long 	iflib_fl_t fl;
17744c7070dbSScott Long 	int			err;
17754c7070dbSScott Long 
177623ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[0] > 0);
177723ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
17784c7070dbSScott Long 
17794c7070dbSScott Long 	fl = rxq->ifr_fl;
17804c7070dbSScott Long 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
178123ac9029SStephen Hurd 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
17824c7070dbSScott Long 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
17834c7070dbSScott Long 					 1, 0,			/* alignment, bounds */
17844c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* lowaddr */
17854c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* highaddr */
17864c7070dbSScott Long 					 NULL, NULL,		/* filter, filterarg */
17874c7070dbSScott Long 					 sctx->isc_rx_maxsize,	/* maxsize */
17884c7070dbSScott Long 					 sctx->isc_rx_nsegments,	/* nsegments */
17894c7070dbSScott Long 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
17904c7070dbSScott Long 					 0,			/* flags */
17914c7070dbSScott Long 					 NULL,			/* lockfunc */
17924c7070dbSScott Long 					 NULL,			/* lockarg */
17934c7070dbSScott Long 					 &fl->ifl_desc_tag);
17944c7070dbSScott Long 		if (err) {
17954c7070dbSScott Long 			device_printf(dev, "%s: bus_dma_tag_create failed %d\n",
17964c7070dbSScott Long 				__func__, err);
17974c7070dbSScott Long 			goto fail;
17984c7070dbSScott Long 		}
1799e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_flags =
1800ac2fffa4SPedro F. Giffuni 		      (uint8_t *) malloc(sizeof(uint8_t) *
1801ac2fffa4SPedro F. Giffuni 					 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1802e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1803e035717eSSean Bruno 			err = ENOMEM;
1804e035717eSSean Bruno 			goto fail;
1805e035717eSSean Bruno 		}
1806e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_m =
1807ac2fffa4SPedro F. Giffuni 		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
1808ac2fffa4SPedro F. Giffuni 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1809e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1810e035717eSSean Bruno 			err = ENOMEM;
1811e035717eSSean Bruno 			goto fail;
1812e035717eSSean Bruno 		}
1813e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_cl =
1814ac2fffa4SPedro F. Giffuni 		      (caddr_t *) malloc(sizeof(caddr_t) *
1815ac2fffa4SPedro F. Giffuni 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1816e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1817e035717eSSean Bruno 			err = ENOMEM;
1818e035717eSSean Bruno 			goto fail;
1819e035717eSSean Bruno 		}
18204c7070dbSScott Long 
1821e035717eSSean Bruno 		/* Create the descriptor buffer dma maps */
182295246abbSSean Bruno #if defined(ACPI_DMAR) || (! (defined(__i386__) || defined(__amd64__)))
1823e035717eSSean Bruno 		if ((ctx->ifc_flags & IFC_DMAR) == 0)
1824e035717eSSean Bruno 			continue;
1825e035717eSSean Bruno 
1826e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_map =
1827ac2fffa4SPedro F. Giffuni 		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1828e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer map memory\n");
1829e035717eSSean Bruno 			err = ENOMEM;
1830e035717eSSean Bruno 			goto fail;
1831e035717eSSean Bruno 		}
1832e035717eSSean Bruno 
1833e035717eSSean Bruno 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1834e035717eSSean Bruno 			err = bus_dmamap_create(fl->ifl_desc_tag, 0, &fl->ifl_sds.ifsd_map[i]);
1835e035717eSSean Bruno 			if (err != 0) {
183695246abbSSean Bruno 				device_printf(dev, "Unable to create RX buffer DMA map\n");
18374c7070dbSScott Long 				goto fail;
18384c7070dbSScott Long 			}
18394c7070dbSScott Long 		}
1840e035717eSSean Bruno #endif
1841835809f9SSean Bruno 	}
18424c7070dbSScott Long 	return (0);
18434c7070dbSScott Long 
18444c7070dbSScott Long fail:
18454c7070dbSScott Long 	iflib_rx_structures_free(ctx);
18464c7070dbSScott Long 	return (err);
18474c7070dbSScott Long }
18484c7070dbSScott Long 
18494c7070dbSScott Long 
18504c7070dbSScott Long /*
18514c7070dbSScott Long  * Internal service routines
18524c7070dbSScott Long  */
18534c7070dbSScott Long 
18544c7070dbSScott Long struct rxq_refill_cb_arg {
18554c7070dbSScott Long 	int               error;
18564c7070dbSScott Long 	bus_dma_segment_t seg;
18574c7070dbSScott Long 	int               nseg;
18584c7070dbSScott Long };
18594c7070dbSScott Long 
18604c7070dbSScott Long static void
18614c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
18624c7070dbSScott Long {
18634c7070dbSScott Long 	struct rxq_refill_cb_arg *cb_arg = arg;
18644c7070dbSScott Long 
18654c7070dbSScott Long 	cb_arg->error = error;
18664c7070dbSScott Long 	cb_arg->seg = segs[0];
18674c7070dbSScott Long 	cb_arg->nseg = nseg;
18684c7070dbSScott Long }
18694c7070dbSScott Long 
18704c7070dbSScott Long 
18714c7070dbSScott Long #ifdef ACPI_DMAR
18724c7070dbSScott Long #define IS_DMAR(ctx) (ctx->ifc_flags & IFC_DMAR)
18734c7070dbSScott Long #else
18744c7070dbSScott Long #define IS_DMAR(ctx) (0)
18754c7070dbSScott Long #endif
18764c7070dbSScott Long 
18774c7070dbSScott Long /**
18784c7070dbSScott Long  *	rxq_refill - refill an rxq  free-buffer list
18794c7070dbSScott Long  *	@ctx: the iflib context
18804c7070dbSScott Long  *	@rxq: the free-list to refill
18814c7070dbSScott Long  *	@n: the number of new buffers to allocate
18824c7070dbSScott Long  *
18834c7070dbSScott Long  *	(Re)populate an rxq free-buffer list with up to @n new packet buffers.
18844c7070dbSScott Long  *	The caller must assure that @n does not exceed the queue's capacity.
18854c7070dbSScott Long  */
18864c7070dbSScott Long static void
18874c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
18884c7070dbSScott Long {
18894c7070dbSScott Long 	struct mbuf *m;
189087890dbaSSean Bruno 	int idx, frag_idx = fl->ifl_fragidx;
189187890dbaSSean Bruno         int pidx = fl->ifl_pidx;
1892e035717eSSean Bruno 	caddr_t cl, *sd_cl;
1893e035717eSSean Bruno 	struct mbuf **sd_m;
1894e035717eSSean Bruno 	uint8_t *sd_flags;
189595246abbSSean Bruno 	struct if_rxd_update iru;
1896e035717eSSean Bruno 	bus_dmamap_t *sd_map;
18974c7070dbSScott Long 	int n, i = 0;
18984c7070dbSScott Long 	uint64_t bus_addr;
18994c7070dbSScott Long 	int err;
1900a1b799caSStephen Hurd 	qidx_t credits;
19014c7070dbSScott Long 
1902e035717eSSean Bruno 	sd_m = fl->ifl_sds.ifsd_m;
1903e035717eSSean Bruno 	sd_map = fl->ifl_sds.ifsd_map;
1904e035717eSSean Bruno 	sd_cl = fl->ifl_sds.ifsd_cl;
1905e035717eSSean Bruno 	sd_flags = fl->ifl_sds.ifsd_flags;
1906e035717eSSean Bruno 	idx = pidx;
1907a1b799caSStephen Hurd 	credits = fl->ifl_credits;
1908e035717eSSean Bruno 
19094c7070dbSScott Long 	n  = count;
19104c7070dbSScott Long 	MPASS(n > 0);
1911a1b799caSStephen Hurd 	MPASS(credits + n <= fl->ifl_size);
19124c7070dbSScott Long 
19134c7070dbSScott Long 	if (pidx < fl->ifl_cidx)
19144c7070dbSScott Long 		MPASS(pidx + n <= fl->ifl_cidx);
1915a1b799caSStephen Hurd 	if (pidx == fl->ifl_cidx && (credits < fl->ifl_size))
19164c7070dbSScott Long 		MPASS(fl->ifl_gen == 0);
19174c7070dbSScott Long 	if (pidx > fl->ifl_cidx)
19184c7070dbSScott Long 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
19194c7070dbSScott Long 
19204c7070dbSScott Long 	DBG_COUNTER_INC(fl_refills);
19214c7070dbSScott Long 	if (n > 8)
19224c7070dbSScott Long 		DBG_COUNTER_INC(fl_refills_large);
19232d873474SStephen Hurd 	iru_init(&iru, fl->ifl_rxq, fl->ifl_id);
19244c7070dbSScott Long 	while (n--) {
19254c7070dbSScott Long 		/*
19264c7070dbSScott Long 		 * We allocate an uninitialized mbuf + cluster, mbuf is
19274c7070dbSScott Long 		 * initialized after rx.
19284c7070dbSScott Long 		 *
19294c7070dbSScott Long 		 * If the cluster is still set then we know a minimum sized packet was received
19304c7070dbSScott Long 		 */
193187890dbaSSean Bruno 		bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,  &frag_idx);
193287890dbaSSean Bruno 		if ((frag_idx < 0) || (frag_idx >= fl->ifl_size))
193387890dbaSSean Bruno                 	bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
193487890dbaSSean Bruno 		if ((cl = sd_cl[frag_idx]) == NULL) {
193587890dbaSSean Bruno                        if ((cl = sd_cl[frag_idx] = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
19364c7070dbSScott Long 				break;
19374c7070dbSScott Long #if MEMORY_LOGGING
19384c7070dbSScott Long 			fl->ifl_cl_enqueued++;
19394c7070dbSScott Long #endif
19404c7070dbSScott Long 		}
19414c7070dbSScott Long 		if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
19424c7070dbSScott Long 			break;
19434c7070dbSScott Long 		}
19444c7070dbSScott Long #if MEMORY_LOGGING
19454c7070dbSScott Long 		fl->ifl_m_enqueued++;
19464c7070dbSScott Long #endif
19474c7070dbSScott Long 
19484c7070dbSScott Long 		DBG_COUNTER_INC(rx_allocs);
19494c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
19504c7070dbSScott Long 		if (!IS_DMAR(ctx)) {
19514c7070dbSScott Long 			bus_addr = pmap_kextract((vm_offset_t)cl);
19524c7070dbSScott Long 		} else
19534c7070dbSScott Long #endif
19544c7070dbSScott Long 		{
19554c7070dbSScott Long 			struct rxq_refill_cb_arg cb_arg;
19564c7070dbSScott Long 
19574c7070dbSScott Long 			cb_arg.error = 0;
195895246abbSSean Bruno 			MPASS(sd_map != NULL);
195987890dbaSSean Bruno 			MPASS(sd_map[frag_idx] != NULL);
196087890dbaSSean Bruno 			err = bus_dmamap_load(fl->ifl_desc_tag, sd_map[frag_idx],
19614c7070dbSScott Long 		         cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 0);
196287890dbaSSean Bruno 			bus_dmamap_sync(fl->ifl_desc_tag, sd_map[frag_idx],
196387890dbaSSean Bruno 					BUS_DMASYNC_PREREAD);
19644c7070dbSScott Long 
19654c7070dbSScott Long 			if (err != 0 || cb_arg.error) {
19664c7070dbSScott Long 				/*
19674c7070dbSScott Long 				 * !zone_pack ?
19684c7070dbSScott Long 				 */
19694c7070dbSScott Long 				if (fl->ifl_zone == zone_pack)
19704c7070dbSScott Long 					uma_zfree(fl->ifl_zone, cl);
19714c7070dbSScott Long 				m_free(m);
19724c7070dbSScott Long 				n = 0;
19734c7070dbSScott Long 				goto done;
19744c7070dbSScott Long 			}
19754c7070dbSScott Long 			bus_addr = cb_arg.seg.ds_addr;
19764c7070dbSScott Long 		}
197787890dbaSSean Bruno                 bit_set(fl->ifl_rx_bitmap, frag_idx);
197887890dbaSSean Bruno 		sd_flags[frag_idx] |= RX_SW_DESC_INUSE;
19794c7070dbSScott Long 
198087890dbaSSean Bruno 		MPASS(sd_m[frag_idx] == NULL);
198187890dbaSSean Bruno 		sd_cl[frag_idx] = cl;
198287890dbaSSean Bruno 		sd_m[frag_idx] = m;
198387890dbaSSean Bruno 		fl->ifl_rxd_idxs[i] = frag_idx;
19844c7070dbSScott Long 		fl->ifl_bus_addrs[i] = bus_addr;
19854c7070dbSScott Long 		fl->ifl_vm_addrs[i] = cl;
1986a1b799caSStephen Hurd 		credits++;
19874c7070dbSScott Long 		i++;
1988a1b799caSStephen Hurd 		MPASS(credits <= fl->ifl_size);
1989e035717eSSean Bruno 		if (++idx == fl->ifl_size) {
19904c7070dbSScott Long 			fl->ifl_gen = 1;
1991e035717eSSean Bruno 			idx = 0;
19924c7070dbSScott Long 		}
19934c7070dbSScott Long 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
199495246abbSSean Bruno 			iru.iru_pidx = pidx;
199595246abbSSean Bruno 			iru.iru_count = i;
199695246abbSSean Bruno 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
19974c7070dbSScott Long 			i = 0;
1998e035717eSSean Bruno 			pidx = idx;
1999fa5416a8SSean Bruno 			fl->ifl_pidx = idx;
2000a1b799caSStephen Hurd 			fl->ifl_credits = credits;
200187890dbaSSean Bruno 		}
2002e035717eSSean Bruno 
20034c7070dbSScott Long 	}
20044c7070dbSScott Long done:
2005a1b799caSStephen Hurd 	if (i) {
2006a1b799caSStephen Hurd 		iru.iru_pidx = pidx;
2007a1b799caSStephen Hurd 		iru.iru_count = i;
2008a1b799caSStephen Hurd 		ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
2009a1b799caSStephen Hurd 		fl->ifl_pidx = idx;
2010a1b799caSStephen Hurd 		fl->ifl_credits = credits;
2011a1b799caSStephen Hurd 	}
20124c7070dbSScott Long 	DBG_COUNTER_INC(rxd_flush);
20134c7070dbSScott Long 	if (fl->ifl_pidx == 0)
20144c7070dbSScott Long 		pidx = fl->ifl_size - 1;
20154c7070dbSScott Long 	else
20164c7070dbSScott Long 		pidx = fl->ifl_pidx - 1;
201795246abbSSean Bruno 
201895246abbSSean Bruno 	if (sd_map)
201995246abbSSean Bruno 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
202095246abbSSean Bruno 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
20214c7070dbSScott Long 	ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
202287890dbaSSean Bruno 	fl->ifl_fragidx = frag_idx;
20234c7070dbSScott Long }
20244c7070dbSScott Long 
20254c7070dbSScott Long static __inline void
20264c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max)
20274c7070dbSScott Long {
20284c7070dbSScott Long 	/* we avoid allowing pidx to catch up with cidx as it confuses ixl */
20294c7070dbSScott Long 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
20304c7070dbSScott Long #ifdef INVARIANTS
20314c7070dbSScott Long 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
20324c7070dbSScott Long #endif
20334c7070dbSScott Long 
20344c7070dbSScott Long 	MPASS(fl->ifl_credits <= fl->ifl_size);
20354c7070dbSScott Long 	MPASS(reclaimable == delta);
20364c7070dbSScott Long 
20374c7070dbSScott Long 	if (reclaimable > 0)
20384c7070dbSScott Long 		_iflib_fl_refill(ctx, fl, min(max, reclaimable));
20394c7070dbSScott Long }
20404c7070dbSScott Long 
20414c7070dbSScott Long static void
20424c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl)
20434c7070dbSScott Long {
20444c7070dbSScott Long 	iflib_dma_info_t idi = fl->ifl_ifdi;
20454c7070dbSScott Long 	uint32_t i;
20464c7070dbSScott Long 
20474c7070dbSScott Long 	for (i = 0; i < fl->ifl_size; i++) {
2048e035717eSSean Bruno 		struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
2049e035717eSSean Bruno 		uint8_t *sd_flags = &fl->ifl_sds.ifsd_flags[i];
2050e035717eSSean Bruno 		caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
20514c7070dbSScott Long 
2052e035717eSSean Bruno 		if (*sd_flags & RX_SW_DESC_INUSE) {
2053e035717eSSean Bruno 			if (fl->ifl_sds.ifsd_map != NULL) {
2054e035717eSSean Bruno 				bus_dmamap_t sd_map = fl->ifl_sds.ifsd_map[i];
2055e035717eSSean Bruno 				bus_dmamap_unload(fl->ifl_desc_tag, sd_map);
2056a4e59607SStephen Hurd 				if (fl->ifl_rxq->ifr_ctx->ifc_in_detach)
2057e035717eSSean Bruno 					bus_dmamap_destroy(fl->ifl_desc_tag, sd_map);
20584c7070dbSScott Long 			}
2059e035717eSSean Bruno 			if (*sd_m != NULL) {
2060e035717eSSean Bruno 				m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
2061e035717eSSean Bruno 				uma_zfree(zone_mbuf, *sd_m);
2062e035717eSSean Bruno 			}
2063e035717eSSean Bruno 			if (*sd_cl != NULL)
2064e035717eSSean Bruno 				uma_zfree(fl->ifl_zone, *sd_cl);
2065e035717eSSean Bruno 			*sd_flags = 0;
20664c7070dbSScott Long 		} else {
2067e035717eSSean Bruno 			MPASS(*sd_cl == NULL);
2068e035717eSSean Bruno 			MPASS(*sd_m == NULL);
20694c7070dbSScott Long 		}
20704c7070dbSScott Long #if MEMORY_LOGGING
20714c7070dbSScott Long 		fl->ifl_m_dequeued++;
20724c7070dbSScott Long 		fl->ifl_cl_dequeued++;
20734c7070dbSScott Long #endif
2074e035717eSSean Bruno 		*sd_cl = NULL;
2075e035717eSSean Bruno 		*sd_m = NULL;
20764c7070dbSScott Long 	}
207795246abbSSean Bruno #ifdef INVARIANTS
207895246abbSSean Bruno 	for (i = 0; i < fl->ifl_size; i++) {
2079ab2e3f79SStephen Hurd 		MPASS(fl->ifl_sds.ifsd_flags[i] == 0);
208095246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
208195246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
208295246abbSSean Bruno 	}
208395246abbSSean Bruno #endif
20844c7070dbSScott Long 	/*
20854c7070dbSScott Long 	 * Reset free list values
20864c7070dbSScott Long 	 */
208787890dbaSSean Bruno 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
20884c7070dbSScott Long 	bzero(idi->idi_vaddr, idi->idi_size);
20894c7070dbSScott Long }
20904c7070dbSScott Long 
20914c7070dbSScott Long /*********************************************************************
20924c7070dbSScott Long  *
20934c7070dbSScott Long  *  Initialize a receive ring and its buffers.
20944c7070dbSScott Long  *
20954c7070dbSScott Long  **********************************************************************/
20964c7070dbSScott Long static int
20974c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl)
20984c7070dbSScott Long {
20994c7070dbSScott Long 	iflib_rxq_t rxq = fl->ifl_rxq;
21004c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
21014c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
21024c7070dbSScott Long 
21037274b2f6SStephen Hurd 	bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1);
21044c7070dbSScott Long 	/*
21054c7070dbSScott Long 	** Free current RX buffer structs and their mbufs
21064c7070dbSScott Long 	*/
21074c7070dbSScott Long 	iflib_fl_bufs_free(fl);
21084c7070dbSScott Long 	/* Now replenish the mbufs */
21094c7070dbSScott Long 	MPASS(fl->ifl_credits == 0);
21104c7070dbSScott Long 	/*
21114c7070dbSScott Long 	 * XXX don't set the max_frame_size to larger
21124c7070dbSScott Long 	 * than the hardware can handle
21134c7070dbSScott Long 	 */
21144c7070dbSScott Long 	if (sctx->isc_max_frame_size <= 2048)
21154c7070dbSScott Long 		fl->ifl_buf_size = MCLBYTES;
211695246abbSSean Bruno #ifndef CONTIGMALLOC_WORKS
211795246abbSSean Bruno 	else
211895246abbSSean Bruno 		fl->ifl_buf_size = MJUMPAGESIZE;
211995246abbSSean Bruno #else
21204c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 4096)
21214c7070dbSScott Long 		fl->ifl_buf_size = MJUMPAGESIZE;
21224c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 9216)
21234c7070dbSScott Long 		fl->ifl_buf_size = MJUM9BYTES;
21244c7070dbSScott Long 	else
21254c7070dbSScott Long 		fl->ifl_buf_size = MJUM16BYTES;
212695246abbSSean Bruno #endif
21274c7070dbSScott Long 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
21284c7070dbSScott Long 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
21294c7070dbSScott Long 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
21304c7070dbSScott Long 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
21314c7070dbSScott Long 
21324c7070dbSScott Long 
21334c7070dbSScott Long 	/* avoid pre-allocating zillions of clusters to an idle card
21344c7070dbSScott Long 	 * potentially speeding up attach
21354c7070dbSScott Long 	 */
21364c7070dbSScott Long 	_iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
21374c7070dbSScott Long 	MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
21384c7070dbSScott Long 	if (min(128, fl->ifl_size) != fl->ifl_credits)
21394c7070dbSScott Long 		return (ENOBUFS);
21404c7070dbSScott Long 	/*
21414c7070dbSScott Long 	 * handle failure
21424c7070dbSScott Long 	 */
21434c7070dbSScott Long 	MPASS(rxq != NULL);
21444c7070dbSScott Long 	MPASS(fl->ifl_ifdi != NULL);
21454c7070dbSScott Long 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
21464c7070dbSScott Long 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
21474c7070dbSScott Long 	return (0);
21484c7070dbSScott Long }
21494c7070dbSScott Long 
21504c7070dbSScott Long /*********************************************************************
21514c7070dbSScott Long  *
21524c7070dbSScott Long  *  Free receive ring data structures
21534c7070dbSScott Long  *
21544c7070dbSScott Long  **********************************************************************/
21554c7070dbSScott Long static void
21564c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq)
21574c7070dbSScott Long {
21584c7070dbSScott Long 	iflib_fl_t fl;
21594c7070dbSScott Long 	int i;
21604c7070dbSScott Long 
21614c7070dbSScott Long 	if (rxq->ifr_fl != NULL) {
21624c7070dbSScott Long 		for (i = 0; i < rxq->ifr_nfl; i++) {
21634c7070dbSScott Long 			fl = &rxq->ifr_fl[i];
21644c7070dbSScott Long 			if (fl->ifl_desc_tag != NULL) {
21654c7070dbSScott Long 				bus_dma_tag_destroy(fl->ifl_desc_tag);
21664c7070dbSScott Long 				fl->ifl_desc_tag = NULL;
21674c7070dbSScott Long 			}
2168e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_m, M_IFLIB);
2169e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_cl, M_IFLIB);
2170e035717eSSean Bruno 			/* XXX destroy maps first */
2171e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_map, M_IFLIB);
2172e035717eSSean Bruno 			fl->ifl_sds.ifsd_m = NULL;
2173e035717eSSean Bruno 			fl->ifl_sds.ifsd_cl = NULL;
2174e035717eSSean Bruno 			fl->ifl_sds.ifsd_map = NULL;
21754c7070dbSScott Long 		}
21764c7070dbSScott Long 		free(rxq->ifr_fl, M_IFLIB);
21774c7070dbSScott Long 		rxq->ifr_fl = NULL;
21784c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
21794c7070dbSScott Long 	}
21804c7070dbSScott Long }
21814c7070dbSScott Long 
21824c7070dbSScott Long /*
21834c7070dbSScott Long  * MI independent logic
21844c7070dbSScott Long  *
21854c7070dbSScott Long  */
21864c7070dbSScott Long static void
21874c7070dbSScott Long iflib_timer(void *arg)
21884c7070dbSScott Long {
2189ab2e3f79SStephen Hurd 	iflib_txq_t txq = arg;
21904c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
2191ab2e3f79SStephen Hurd 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
21924c7070dbSScott Long 
21934c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
21944c7070dbSScott Long 		return;
21954c7070dbSScott Long 	/*
21964c7070dbSScott Long 	** Check on the state of the TX queue(s), this
21974c7070dbSScott Long 	** can be done without the lock because its RO
21984c7070dbSScott Long 	** and the HUNG state will be static if set.
21994c7070dbSScott Long 	*/
2200ab2e3f79SStephen Hurd 	IFDI_TIMER(ctx, txq->ift_id);
2201ab2e3f79SStephen Hurd 	if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
2202ab2e3f79SStephen Hurd 	    ((txq->ift_cleaned_prev == txq->ift_cleaned) ||
2203ab2e3f79SStephen Hurd 	     (sctx->isc_pause_frames == 0)))
2204ab2e3f79SStephen Hurd 		goto hung;
2205a9693502SSean Bruno 
2206ab2e3f79SStephen Hurd 	if (ifmp_ring_is_stalled(txq->ift_br))
2207ab2e3f79SStephen Hurd 		txq->ift_qstatus = IFLIB_QUEUE_HUNG;
2208ab2e3f79SStephen Hurd 	txq->ift_cleaned_prev = txq->ift_cleaned;
2209ab2e3f79SStephen Hurd 	/* handle any laggards */
2210ab2e3f79SStephen Hurd 	if (txq->ift_db_pending)
2211ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
2212a9693502SSean Bruno 
2213ab2e3f79SStephen Hurd 	sctx->isc_pause_frames = 0;
2214d300df01SStephen Hurd 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
2215ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
2216ab2e3f79SStephen Hurd 	return;
2217ab2e3f79SStephen Hurd  hung:
2218ab2e3f79SStephen Hurd 	device_printf(ctx->ifc_dev,  "TX(%d) desc avail = %d, pidx = %d\n",
2219ab2e3f79SStephen Hurd 				  txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
22207b610b60SSean Bruno 	STATE_LOCK(ctx);
22217b610b60SSean Bruno 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
22227b610b60SSean Bruno 	ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET);
2223ab2e3f79SStephen Hurd 	iflib_admin_intr_deferred(ctx);
22247b610b60SSean Bruno 	STATE_UNLOCK(ctx);
22254c7070dbSScott Long }
22264c7070dbSScott Long 
22274c7070dbSScott Long static void
22284c7070dbSScott Long iflib_init_locked(if_ctx_t ctx)
22294c7070dbSScott Long {
22304c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
22311248952aSSean Bruno 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
22324c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
22334c7070dbSScott Long 	iflib_fl_t fl;
22344c7070dbSScott Long 	iflib_txq_t txq;
22354c7070dbSScott Long 	iflib_rxq_t rxq;
2236ab2e3f79SStephen Hurd 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
22374c7070dbSScott Long 
22384c7070dbSScott Long 
22394c7070dbSScott Long 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
22404c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
22414c7070dbSScott Long 
22421248952aSSean Bruno 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
22431248952aSSean Bruno 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
22444c7070dbSScott Long 	/* Set hardware offload abilities */
22454c7070dbSScott Long 	if_clearhwassist(ifp);
22464c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
22471248952aSSean Bruno 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
22484c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
22491248952aSSean Bruno 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
22504c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO4)
22514c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
22524c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO6)
22534c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
22544c7070dbSScott Long 
22554c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
22564c7070dbSScott Long 		CALLOUT_LOCK(txq);
22574c7070dbSScott Long 		callout_stop(&txq->ift_timer);
22584c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
22594c7070dbSScott Long 		iflib_netmap_txq_init(ctx, txq);
22604c7070dbSScott Long 	}
226123ac9029SStephen Hurd #ifdef INVARIANTS
226223ac9029SStephen Hurd 	i = if_getdrvflags(ifp);
226323ac9029SStephen Hurd #endif
22644c7070dbSScott Long 	IFDI_INIT(ctx);
226523ac9029SStephen Hurd 	MPASS(if_getdrvflags(ifp) == i);
22664c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
226795246abbSSean Bruno 		/* XXX this should really be done on a per-queue basis */
2268d0d0ad0aSStephen Hurd 		if (if_getcapenable(ifp) & IFCAP_NETMAP) {
2269d0d0ad0aSStephen Hurd 			MPASS(rxq->ifr_id == i);
2270d0d0ad0aSStephen Hurd 			iflib_netmap_rxq_init(ctx, rxq);
227195246abbSSean Bruno 			continue;
2272d0d0ad0aSStephen Hurd 		}
22734c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
22744c7070dbSScott Long 			if (iflib_fl_setup(fl)) {
22754c7070dbSScott Long 				device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n");
22764c7070dbSScott Long 				goto done;
22774c7070dbSScott Long 			}
22784c7070dbSScott Long 		}
22794c7070dbSScott Long 	}
22804c7070dbSScott Long 	done:
22814c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
22824c7070dbSScott Long 	IFDI_INTR_ENABLE(ctx);
22834c7070dbSScott Long 	txq = ctx->ifc_txqs;
22844c7070dbSScott Long 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
2285ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
2286ab2e3f79SStephen Hurd 			txq->ift_timer.c_cpu);
22874c7070dbSScott Long }
22884c7070dbSScott Long 
22894c7070dbSScott Long static int
22904c7070dbSScott Long iflib_media_change(if_t ifp)
22914c7070dbSScott Long {
22924c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
22934c7070dbSScott Long 	int err;
22944c7070dbSScott Long 
22954c7070dbSScott Long 	CTX_LOCK(ctx);
22964c7070dbSScott Long 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
22974c7070dbSScott Long 		iflib_init_locked(ctx);
22984c7070dbSScott Long 	CTX_UNLOCK(ctx);
22994c7070dbSScott Long 	return (err);
23004c7070dbSScott Long }
23014c7070dbSScott Long 
23024c7070dbSScott Long static void
23034c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
23044c7070dbSScott Long {
23054c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
23064c7070dbSScott Long 
23074c7070dbSScott Long 	CTX_LOCK(ctx);
2308ab2e3f79SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
23094c7070dbSScott Long 	IFDI_MEDIA_STATUS(ctx, ifmr);
23104c7070dbSScott Long 	CTX_UNLOCK(ctx);
23114c7070dbSScott Long }
23124c7070dbSScott Long 
231309f6ff4fSMatt Macy void
23144c7070dbSScott Long iflib_stop(if_ctx_t ctx)
23154c7070dbSScott Long {
23164c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
23174c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
23184c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
23194c7070dbSScott Long 	iflib_dma_info_t di;
23204c7070dbSScott Long 	iflib_fl_t fl;
23214c7070dbSScott Long 	int i, j;
23224c7070dbSScott Long 
23234c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
23244c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
23254c7070dbSScott Long 
23264c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
2327ab2e3f79SStephen Hurd 	DELAY(1000);
2328da69b8f9SSean Bruno 	IFDI_STOP(ctx);
2329ab2e3f79SStephen Hurd 	DELAY(1000);
23304c7070dbSScott Long 
2331da69b8f9SSean Bruno 	iflib_debug_reset();
23324c7070dbSScott Long 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
23334c7070dbSScott Long 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
23344c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
23354c7070dbSScott Long 
2336226fb85dSStephen Hurd 		CALLOUT_LOCK(txq);
2337226fb85dSStephen Hurd 		callout_stop(&txq->ift_timer);
2338226fb85dSStephen Hurd 		CALLOUT_UNLOCK(txq);
2339226fb85dSStephen Hurd 
23404c7070dbSScott Long 		/* clean any enqueued buffers */
2341da69b8f9SSean Bruno 		iflib_ifmp_purge(txq);
23424c7070dbSScott Long 		/* Free any existing tx buffers. */
234323ac9029SStephen Hurd 		for (j = 0; j < txq->ift_size; j++) {
23444c7070dbSScott Long 			iflib_txsd_free(ctx, txq, j);
23454c7070dbSScott Long 		}
2346ab2e3f79SStephen Hurd 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2347ab2e3f79SStephen Hurd 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
23484c7070dbSScott Long 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
23494c7070dbSScott Long 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
2350ab2e3f79SStephen Hurd 		txq->ift_pullups = 0;
235195246abbSSean Bruno 		ifmp_ring_reset_stats(txq->ift_br);
23524c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwtxqs; j++, di++)
23534c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
23544c7070dbSScott Long 	}
23554c7070dbSScott Long 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
23564c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
23574c7070dbSScott Long 
235866def526SMateusz Guzik 		for (j = 0, di = rxq->ifr_ifdi; j < rxq->ifr_nfl; j++, di++)
23594c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
23604c7070dbSScott Long 		/* also resets the free lists pidx/cidx */
23614c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
23624c7070dbSScott Long 			iflib_fl_bufs_free(fl);
23634c7070dbSScott Long 	}
23644c7070dbSScott Long }
23654c7070dbSScott Long 
236695246abbSSean Bruno static inline caddr_t
236795246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx)
236895246abbSSean Bruno {
236995246abbSSean Bruno 	qidx_t size;
237095246abbSSean Bruno 	int nrxd;
237195246abbSSean Bruno 	caddr_t start, end, cur, next;
237295246abbSSean Bruno 
237395246abbSSean Bruno 	nrxd = fl->ifl_size;
237495246abbSSean Bruno 	size = fl->ifl_rxd_size;
237595246abbSSean Bruno 	start = fl->ifl_ifdi->idi_vaddr;
237695246abbSSean Bruno 
237795246abbSSean Bruno 	if (__predict_false(size == 0))
237895246abbSSean Bruno 		return (start);
237995246abbSSean Bruno 	cur = start + size*cidx;
238095246abbSSean Bruno 	end = start + size*nrxd;
238195246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
238295246abbSSean Bruno 	return (next < end ? next : start);
238395246abbSSean Bruno }
238495246abbSSean Bruno 
2385e035717eSSean Bruno static inline void
2386e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx)
2387e035717eSSean Bruno {
2388e035717eSSean Bruno 	int nextptr;
2389e035717eSSean Bruno 	int nrxd = fl->ifl_size;
239095246abbSSean Bruno 	caddr_t next_rxd;
239195246abbSSean Bruno 
2392e035717eSSean Bruno 
2393e035717eSSean Bruno 	nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2394e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2395e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
239695246abbSSean Bruno 	next_rxd = calc_next_rxd(fl, cidx);
239795246abbSSean Bruno 	prefetch(next_rxd);
2398e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2399e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2400e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2401e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2402e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2403e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2404e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2405e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2406e035717eSSean Bruno }
2407e035717eSSean Bruno 
2408e035717eSSean Bruno static void
240995246abbSSean Bruno rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int unload, if_rxsd_t sd)
24104c7070dbSScott Long {
24114c7070dbSScott Long 	int flid, cidx;
2412e035717eSSean Bruno 	bus_dmamap_t map;
24134c7070dbSScott Long 	iflib_fl_t fl;
24144c7070dbSScott Long 	iflib_dma_info_t di;
2415e035717eSSean Bruno 	int next;
24164c7070dbSScott Long 
241795246abbSSean Bruno 	map = NULL;
24184c7070dbSScott Long 	flid = irf->irf_flid;
24194c7070dbSScott Long 	cidx = irf->irf_idx;
24204c7070dbSScott Long 	fl = &rxq->ifr_fl[flid];
242195246abbSSean Bruno 	sd->ifsd_fl = fl;
242295246abbSSean Bruno 	sd->ifsd_cidx = cidx;
242395246abbSSean Bruno 	sd->ifsd_m = &fl->ifl_sds.ifsd_m[cidx];
242495246abbSSean Bruno 	sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
24254c7070dbSScott Long 	fl->ifl_credits--;
24264c7070dbSScott Long #if MEMORY_LOGGING
24274c7070dbSScott Long 	fl->ifl_m_dequeued++;
24284c7070dbSScott Long #endif
242995246abbSSean Bruno 	if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
2430e035717eSSean Bruno 		prefetch_pkts(fl, cidx);
2431e035717eSSean Bruno 	if (fl->ifl_sds.ifsd_map != NULL) {
2432e035717eSSean Bruno 		next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2433e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_map[next]);
2434e035717eSSean Bruno 		map = fl->ifl_sds.ifsd_map[cidx];
24354c7070dbSScott Long 		di = fl->ifl_ifdi;
2436e035717eSSean Bruno 		next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1);
2437e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_flags[next]);
24384c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
24394c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
24404c7070dbSScott Long 
24414c7070dbSScott Long 	/* not valid assert if bxe really does SGE from non-contiguous elements */
24424c7070dbSScott Long 		MPASS(fl->ifl_cidx == cidx);
24434c7070dbSScott Long 		if (unload)
2444e035717eSSean Bruno 			bus_dmamap_unload(fl->ifl_desc_tag, map);
2445e035717eSSean Bruno 	}
244695246abbSSean Bruno 	fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1);
244795246abbSSean Bruno 	if (__predict_false(fl->ifl_cidx == 0))
24484c7070dbSScott Long 		fl->ifl_gen = 0;
244995246abbSSean Bruno 	if (map != NULL)
245095246abbSSean Bruno 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
245195246abbSSean Bruno 			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
245287890dbaSSean Bruno         bit_clear(fl->ifl_rx_bitmap, cidx);
24534c7070dbSScott Long }
24544c7070dbSScott Long 
24554c7070dbSScott Long static struct mbuf *
245695246abbSSean Bruno assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd)
24574c7070dbSScott Long {
245895246abbSSean Bruno 	int i, padlen , flags;
245995246abbSSean Bruno 	struct mbuf *m, *mh, *mt;
246095246abbSSean Bruno 	caddr_t cl;
24614c7070dbSScott Long 
24624c7070dbSScott Long 	i = 0;
246323ac9029SStephen Hurd 	mh = NULL;
24644c7070dbSScott Long 	do {
246595246abbSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[i], TRUE, sd);
24664c7070dbSScott Long 
246795246abbSSean Bruno 		MPASS(*sd->ifsd_cl != NULL);
246895246abbSSean Bruno 		MPASS(*sd->ifsd_m != NULL);
246923ac9029SStephen Hurd 
247023ac9029SStephen Hurd 		/* Don't include zero-length frags */
247123ac9029SStephen Hurd 		if (ri->iri_frags[i].irf_len == 0) {
247223ac9029SStephen Hurd 			/* XXX we can save the cluster here, but not the mbuf */
247395246abbSSean Bruno 			m_init(*sd->ifsd_m, M_NOWAIT, MT_DATA, 0);
247495246abbSSean Bruno 			m_free(*sd->ifsd_m);
247595246abbSSean Bruno 			*sd->ifsd_m = NULL;
247623ac9029SStephen Hurd 			continue;
247723ac9029SStephen Hurd 		}
247895246abbSSean Bruno 		m = *sd->ifsd_m;
247995246abbSSean Bruno 		*sd->ifsd_m = NULL;
248023ac9029SStephen Hurd 		if (mh == NULL) {
24814c7070dbSScott Long 			flags = M_PKTHDR|M_EXT;
24824c7070dbSScott Long 			mh = mt = m;
24834c7070dbSScott Long 			padlen = ri->iri_pad;
24844c7070dbSScott Long 		} else {
24854c7070dbSScott Long 			flags = M_EXT;
24864c7070dbSScott Long 			mt->m_next = m;
24874c7070dbSScott Long 			mt = m;
24884c7070dbSScott Long 			/* assuming padding is only on the first fragment */
24894c7070dbSScott Long 			padlen = 0;
24904c7070dbSScott Long 		}
249195246abbSSean Bruno 		cl = *sd->ifsd_cl;
249295246abbSSean Bruno 		*sd->ifsd_cl = NULL;
24934c7070dbSScott Long 
24944c7070dbSScott Long 		/* Can these two be made one ? */
24954c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, flags);
249695246abbSSean Bruno 		m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
24974c7070dbSScott Long 		/*
24984c7070dbSScott Long 		 * These must follow m_init and m_cljset
24994c7070dbSScott Long 		 */
25004c7070dbSScott Long 		m->m_data += padlen;
25014c7070dbSScott Long 		ri->iri_len -= padlen;
250223ac9029SStephen Hurd 		m->m_len = ri->iri_frags[i].irf_len;
25034c7070dbSScott Long 	} while (++i < ri->iri_nfrags);
25044c7070dbSScott Long 
25054c7070dbSScott Long 	return (mh);
25064c7070dbSScott Long }
25074c7070dbSScott Long 
25084c7070dbSScott Long /*
25094c7070dbSScott Long  * Process one software descriptor
25104c7070dbSScott Long  */
25114c7070dbSScott Long static struct mbuf *
25124c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
25134c7070dbSScott Long {
251495246abbSSean Bruno 	struct if_rxsd sd;
25154c7070dbSScott Long 	struct mbuf *m;
25164c7070dbSScott Long 
25174c7070dbSScott Long 	/* should I merge this back in now that the two paths are basically duplicated? */
251823ac9029SStephen Hurd 	if (ri->iri_nfrags == 1 &&
251918628b74SMark Johnston 	    ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) {
252095246abbSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[0], FALSE, &sd);
252195246abbSSean Bruno 		m = *sd.ifsd_m;
252295246abbSSean Bruno 		*sd.ifsd_m = NULL;
25234c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
252495246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
252595246abbSSean Bruno 		if (!IP_ALIGNED(m))
252695246abbSSean Bruno 			m->m_data += 2;
252795246abbSSean Bruno #endif
252895246abbSSean Bruno 		memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
252923ac9029SStephen Hurd 		m->m_len = ri->iri_frags[0].irf_len;
25304c7070dbSScott Long        } else {
253195246abbSSean Bruno 		m = assemble_segments(rxq, ri, &sd);
25324c7070dbSScott Long 	}
25334c7070dbSScott Long 	m->m_pkthdr.len = ri->iri_len;
25344c7070dbSScott Long 	m->m_pkthdr.rcvif = ri->iri_ifp;
25354c7070dbSScott Long 	m->m_flags |= ri->iri_flags;
25364c7070dbSScott Long 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
25374c7070dbSScott Long 	m->m_pkthdr.flowid = ri->iri_flowid;
25384c7070dbSScott Long 	M_HASHTYPE_SET(m, ri->iri_rsstype);
25394c7070dbSScott Long 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
25404c7070dbSScott Long 	m->m_pkthdr.csum_data = ri->iri_csum_data;
25414c7070dbSScott Long 	return (m);
25424c7070dbSScott Long }
25434c7070dbSScott Long 
254435e4e998SStephen Hurd #if defined(INET6) || defined(INET)
2545fe1bcadaSStephen Hurd static void
2546fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6)
2547fe1bcadaSStephen Hurd {
2548fe1bcadaSStephen Hurd 	CURVNET_SET(lc->ifp->if_vnet);
2549fe1bcadaSStephen Hurd #if defined(INET6)
2550fe1bcadaSStephen Hurd 	*v6 = VNET(ip6_forwarding);
2551fe1bcadaSStephen Hurd #endif
2552fe1bcadaSStephen Hurd #if defined(INET)
2553fe1bcadaSStephen Hurd 	*v4 = VNET(ipforwarding);
2554fe1bcadaSStephen Hurd #endif
2555fe1bcadaSStephen Hurd 	CURVNET_RESTORE();
2556fe1bcadaSStephen Hurd }
2557fe1bcadaSStephen Hurd 
255835e4e998SStephen Hurd /*
255935e4e998SStephen Hurd  * Returns true if it's possible this packet could be LROed.
256035e4e998SStephen Hurd  * if it returns false, it is guaranteed that tcp_lro_rx()
256135e4e998SStephen Hurd  * would not return zero.
256235e4e998SStephen Hurd  */
256335e4e998SStephen Hurd static bool
2564fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding)
256535e4e998SStephen Hurd {
256635e4e998SStephen Hurd 	struct ether_header *eh;
256735e4e998SStephen Hurd 	uint16_t eh_type;
256835e4e998SStephen Hurd 
256935e4e998SStephen Hurd 	eh = mtod(m, struct ether_header *);
257035e4e998SStephen Hurd 	eh_type = ntohs(eh->ether_type);
257135e4e998SStephen Hurd 	switch (eh_type) {
2572abec4724SSean Bruno #if defined(INET6)
257335e4e998SStephen Hurd 		case ETHERTYPE_IPV6:
2574fe1bcadaSStephen Hurd 			return !v6_forwarding;
2575abec4724SSean Bruno #endif
2576abec4724SSean Bruno #if defined (INET)
257735e4e998SStephen Hurd 		case ETHERTYPE_IP:
2578fe1bcadaSStephen Hurd 			return !v4_forwarding;
2579abec4724SSean Bruno #endif
258035e4e998SStephen Hurd 	}
258135e4e998SStephen Hurd 
258235e4e998SStephen Hurd 	return false;
258335e4e998SStephen Hurd }
2584fe1bcadaSStephen Hurd #else
2585fe1bcadaSStephen Hurd static void
2586fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused)
2587fe1bcadaSStephen Hurd {
2588fe1bcadaSStephen Hurd }
258935e4e998SStephen Hurd #endif
259035e4e998SStephen Hurd 
25914c7070dbSScott Long static bool
259295246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
25934c7070dbSScott Long {
25944c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
25954c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
259623ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
25974c7070dbSScott Long 	int avail, i;
259895246abbSSean Bruno 	qidx_t *cidxp;
25994c7070dbSScott Long 	struct if_rxd_info ri;
26004c7070dbSScott Long 	int err, budget_left, rx_bytes, rx_pkts;
26014c7070dbSScott Long 	iflib_fl_t fl;
26024c7070dbSScott Long 	struct ifnet *ifp;
26034c7070dbSScott Long 	int lro_enabled;
260435e4e998SStephen Hurd 	bool lro_possible = false;
2605fe1bcadaSStephen Hurd 	bool v4_forwarding, v6_forwarding;
260695246abbSSean Bruno 
26074c7070dbSScott Long 	/*
26084c7070dbSScott Long 	 * XXX early demux data packets so that if_input processing only handles
26094c7070dbSScott Long 	 * acks in interrupt context
26104c7070dbSScott Long 	 */
261120f63282SStephen Hurd 	struct mbuf *m, *mh, *mt, *mf;
26124c7070dbSScott Long 
261395246abbSSean Bruno 	ifp = ctx->ifc_ifp;
26144c7070dbSScott Long 	mh = mt = NULL;
26154c7070dbSScott Long 	MPASS(budget > 0);
26164c7070dbSScott Long 	rx_pkts	= rx_bytes = 0;
261723ac9029SStephen Hurd 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
26184c7070dbSScott Long 		cidxp = &rxq->ifr_cq_cidx;
26194c7070dbSScott Long 	else
26204c7070dbSScott Long 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
262123ac9029SStephen Hurd 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
26224c7070dbSScott Long 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
26234c7070dbSScott Long 			__iflib_fl_refill_lt(ctx, fl, budget + 8);
26244c7070dbSScott Long 		DBG_COUNTER_INC(rx_unavail);
26254c7070dbSScott Long 		return (false);
26264c7070dbSScott Long 	}
26274c7070dbSScott Long 
26284c7070dbSScott Long 	for (budget_left = budget; (budget_left > 0) && (avail > 0); budget_left--, avail--) {
26294c7070dbSScott Long 		if (__predict_false(!CTX_ACTIVE(ctx))) {
26304c7070dbSScott Long 			DBG_COUNTER_INC(rx_ctx_inactive);
26314c7070dbSScott Long 			break;
26324c7070dbSScott Long 		}
26334c7070dbSScott Long 		/*
26344c7070dbSScott Long 		 * Reset client set fields to their default values
26354c7070dbSScott Long 		 */
263695246abbSSean Bruno 		rxd_info_zero(&ri);
26374c7070dbSScott Long 		ri.iri_qsidx = rxq->ifr_id;
26384c7070dbSScott Long 		ri.iri_cidx = *cidxp;
263995246abbSSean Bruno 		ri.iri_ifp = ifp;
26404c7070dbSScott Long 		ri.iri_frags = rxq->ifr_frags;
26414c7070dbSScott Long 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
26424c7070dbSScott Long 
264395246abbSSean Bruno 		if (err)
264495246abbSSean Bruno 			goto err;
264523ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
264623ac9029SStephen Hurd 			*cidxp = ri.iri_cidx;
264723ac9029SStephen Hurd 			/* Update our consumer index */
264895246abbSSean Bruno 			/* XXX NB: shurd - check if this is still safe */
264923ac9029SStephen Hurd 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) {
265023ac9029SStephen Hurd 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
26514c7070dbSScott Long 				rxq->ifr_cq_gen = 0;
26524c7070dbSScott Long 			}
26534c7070dbSScott Long 			/* was this only a completion queue message? */
26544c7070dbSScott Long 			if (__predict_false(ri.iri_nfrags == 0))
26554c7070dbSScott Long 				continue;
26564c7070dbSScott Long 		}
26574c7070dbSScott Long 		MPASS(ri.iri_nfrags != 0);
26584c7070dbSScott Long 		MPASS(ri.iri_len != 0);
26594c7070dbSScott Long 
26604c7070dbSScott Long 		/* will advance the cidx on the corresponding free lists */
26614c7070dbSScott Long 		m = iflib_rxd_pkt_get(rxq, &ri);
26624c7070dbSScott Long 		if (avail == 0 && budget_left)
266323ac9029SStephen Hurd 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
26644c7070dbSScott Long 
26654c7070dbSScott Long 		if (__predict_false(m == NULL)) {
26664c7070dbSScott Long 			DBG_COUNTER_INC(rx_mbuf_null);
26674c7070dbSScott Long 			continue;
26684c7070dbSScott Long 		}
26694c7070dbSScott Long 		/* imm_pkt: -- cxgb */
26704c7070dbSScott Long 		if (mh == NULL)
26714c7070dbSScott Long 			mh = mt = m;
26724c7070dbSScott Long 		else {
26734c7070dbSScott Long 			mt->m_nextpkt = m;
26744c7070dbSScott Long 			mt = m;
26754c7070dbSScott Long 		}
26764c7070dbSScott Long 	}
26774c7070dbSScott Long 	/* make sure that we can refill faster than drain */
26784c7070dbSScott Long 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2679ab2e3f79SStephen Hurd 		__iflib_fl_refill_lt(ctx, fl, budget + 8);
26804c7070dbSScott Long 
26814c7070dbSScott Long 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
2682fe1bcadaSStephen Hurd 	if (lro_enabled)
2683fe1bcadaSStephen Hurd 		iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding);
268420f63282SStephen Hurd 	mt = mf = NULL;
26854c7070dbSScott Long 	while (mh != NULL) {
26864c7070dbSScott Long 		m = mh;
26874c7070dbSScott Long 		mh = mh->m_nextpkt;
26884c7070dbSScott Long 		m->m_nextpkt = NULL;
268995246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
269095246abbSSean Bruno 		if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
269195246abbSSean Bruno 			continue;
269295246abbSSean Bruno #endif
26934c7070dbSScott Long 		rx_bytes += m->m_pkthdr.len;
26944c7070dbSScott Long 		rx_pkts++;
2695aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
269635e4e998SStephen Hurd 		if (lro_enabled) {
269735e4e998SStephen Hurd 			if (!lro_possible) {
2698fe1bcadaSStephen Hurd 				lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding);
269935e4e998SStephen Hurd 				if (lro_possible && mf != NULL) {
270035e4e998SStephen Hurd 					ifp->if_input(ifp, mf);
270135e4e998SStephen Hurd 					DBG_COUNTER_INC(rx_if_input);
270235e4e998SStephen Hurd 					mt = mf = NULL;
270335e4e998SStephen Hurd 				}
270435e4e998SStephen Hurd 			}
270525ac1dd5SStephen Hurd 			if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) ==
270625ac1dd5SStephen Hurd 			    (CSUM_L4_CALC|CSUM_L4_VALID)) {
270735e4e998SStephen Hurd 				if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
27084c7070dbSScott Long 					continue;
270920f63282SStephen Hurd 			}
271025ac1dd5SStephen Hurd 		}
2711aaeb188aSBjoern A. Zeeb #endif
271235e4e998SStephen Hurd 		if (lro_possible) {
271335e4e998SStephen Hurd 			ifp->if_input(ifp, m);
271435e4e998SStephen Hurd 			DBG_COUNTER_INC(rx_if_input);
271535e4e998SStephen Hurd 			continue;
271635e4e998SStephen Hurd 		}
271735e4e998SStephen Hurd 
271835e4e998SStephen Hurd 		if (mf == NULL)
271935e4e998SStephen Hurd 			mf = m;
272020f63282SStephen Hurd 		if (mt != NULL)
272120f63282SStephen Hurd 			mt->m_nextpkt = m;
272220f63282SStephen Hurd 		mt = m;
272320f63282SStephen Hurd 	}
272420f63282SStephen Hurd 	if (mf != NULL) {
272520f63282SStephen Hurd 		ifp->if_input(ifp, mf);
27264c7070dbSScott Long 		DBG_COUNTER_INC(rx_if_input);
27274c7070dbSScott Long 	}
272823ac9029SStephen Hurd 
27294c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
27304c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
27314c7070dbSScott Long 
27324c7070dbSScott Long 	/*
27334c7070dbSScott Long 	 * Flush any outstanding LRO work
27344c7070dbSScott Long 	 */
2735aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
273623ac9029SStephen Hurd 	tcp_lro_flush_all(&rxq->ifr_lc);
2737aaeb188aSBjoern A. Zeeb #endif
2738ab2e3f79SStephen Hurd 	if (avail)
2739ab2e3f79SStephen Hurd 		return true;
2740ab2e3f79SStephen Hurd 	return (iflib_rxd_avail(ctx, rxq, *cidxp, 1));
274195246abbSSean Bruno err:
27427b610b60SSean Bruno 	STATE_LOCK(ctx);
2743ab2e3f79SStephen Hurd 	ctx->ifc_flags |= IFC_DO_RESET;
2744ab2e3f79SStephen Hurd 	iflib_admin_intr_deferred(ctx);
27457b610b60SSean Bruno 	STATE_UNLOCK(ctx);
274695246abbSSean Bruno 	return (false);
274795246abbSSean Bruno }
274895246abbSSean Bruno 
274995246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1)
275095246abbSSean Bruno static inline qidx_t
275195246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
275295246abbSSean Bruno {
275395246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
275495246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
275595246abbSSean Bruno 	if (in_use > 4*minthresh)
275695246abbSSean Bruno 		return (notify_count);
275795246abbSSean Bruno 	if (in_use > 2*minthresh)
275895246abbSSean Bruno 		return (notify_count >> 1);
275995246abbSSean Bruno 	if (in_use > minthresh)
276095246abbSSean Bruno 		return (notify_count >> 3);
276195246abbSSean Bruno 	return (0);
276295246abbSSean Bruno }
276395246abbSSean Bruno 
276495246abbSSean Bruno static inline qidx_t
276595246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq)
276695246abbSSean Bruno {
276795246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
276895246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
276995246abbSSean Bruno 	if (txq->ift_in_use > 4*minthresh)
277095246abbSSean Bruno 		return (notify_count);
277195246abbSSean Bruno 	if (txq->ift_in_use > 2*minthresh)
277295246abbSSean Bruno 		return (notify_count >> 1);
277395246abbSSean Bruno 	if (txq->ift_in_use > minthresh)
277495246abbSSean Bruno 		return (notify_count >> 2);
27752b2fc973SSean Bruno 	return (2);
27764c7070dbSScott Long }
27774c7070dbSScott Long 
27784c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
27794c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
278095246abbSSean Bruno 
278195246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
278295246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
278323ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
27844c7070dbSScott Long 
278595246abbSSean Bruno /* forward compatibility for cxgb */
278695246abbSSean Bruno #define FIRST_QSET(ctx) 0
278795246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
278895246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
278995246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
279095246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
279195246abbSSean Bruno 
279295246abbSSean Bruno /* XXX we should be setting this to something other than zero */
279395246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
279495246abbSSean Bruno #define MAX_TX_DESC(ctx) ((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max)
279595246abbSSean Bruno 
279695246abbSSean Bruno static inline bool
279795246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use)
27984c7070dbSScott Long {
279995246abbSSean Bruno 	qidx_t dbval, max;
280095246abbSSean Bruno 	bool rang;
28014c7070dbSScott Long 
280295246abbSSean Bruno 	rang = false;
280395246abbSSean Bruno 	max = TXQ_MAX_DB_DEFERRED(txq, in_use);
280495246abbSSean Bruno 	if (ring || txq->ift_db_pending >= max) {
28054c7070dbSScott Long 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
28064c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
28074c7070dbSScott Long 		txq->ift_db_pending = txq->ift_npending = 0;
280895246abbSSean Bruno 		rang = true;
28094c7070dbSScott Long 	}
281095246abbSSean Bruno 	return (rang);
28114c7070dbSScott Long }
28124c7070dbSScott Long 
28134c7070dbSScott Long #ifdef PKT_DEBUG
28144c7070dbSScott Long static void
28154c7070dbSScott Long print_pkt(if_pkt_info_t pi)
28164c7070dbSScott Long {
28174c7070dbSScott Long 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
28184c7070dbSScott Long 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
28194c7070dbSScott Long 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
28204c7070dbSScott Long 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
28214c7070dbSScott Long 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
28224c7070dbSScott Long 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
28234c7070dbSScott Long }
28244c7070dbSScott Long #endif
28254c7070dbSScott Long 
28264c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
28274c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
28284c7070dbSScott Long 
28294c7070dbSScott Long static int
28304c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
28314c7070dbSScott Long {
2832ab2e3f79SStephen Hurd 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
28334c7070dbSScott Long 	struct ether_vlan_header *eh;
283423ac9029SStephen Hurd 	struct mbuf *m, *n;
28354c7070dbSScott Long 
283623ac9029SStephen Hurd 	n = m = *mp;
2837ab2e3f79SStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
2838ab2e3f79SStephen Hurd 	    M_WRITABLE(m) == 0) {
2839ab2e3f79SStephen Hurd 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
2840ab2e3f79SStephen Hurd 			return (ENOMEM);
2841ab2e3f79SStephen Hurd 		} else {
2842ab2e3f79SStephen Hurd 			m_freem(*mp);
2843ab2e3f79SStephen Hurd 			n = *mp = m;
2844ab2e3f79SStephen Hurd 		}
2845ab2e3f79SStephen Hurd 	}
28461248952aSSean Bruno 
28474c7070dbSScott Long 	/*
28484c7070dbSScott Long 	 * Determine where frame payload starts.
28494c7070dbSScott Long 	 * Jump over vlan headers if already present,
28504c7070dbSScott Long 	 * helpful for QinQ too.
28514c7070dbSScott Long 	 */
28524c7070dbSScott Long 	if (__predict_false(m->m_len < sizeof(*eh))) {
28534c7070dbSScott Long 		txq->ift_pullups++;
28544c7070dbSScott Long 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
28554c7070dbSScott Long 			return (ENOMEM);
28564c7070dbSScott Long 	}
28574c7070dbSScott Long 	eh = mtod(m, struct ether_vlan_header *);
28584c7070dbSScott Long 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
28594c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_proto);
28604c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
28614c7070dbSScott Long 	} else {
28624c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
28634c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
28644c7070dbSScott Long 	}
28654c7070dbSScott Long 
28664c7070dbSScott Long 	switch (pi->ipi_etype) {
28674c7070dbSScott Long #ifdef INET
28684c7070dbSScott Long 	case ETHERTYPE_IP:
28694c7070dbSScott Long 	{
28704c7070dbSScott Long 		struct ip *ip = NULL;
28714c7070dbSScott Long 		struct tcphdr *th = NULL;
28724c7070dbSScott Long 		int minthlen;
28734c7070dbSScott Long 
28744c7070dbSScott Long 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
28754c7070dbSScott Long 		if (__predict_false(m->m_len < minthlen)) {
28764c7070dbSScott Long 			/*
28774c7070dbSScott Long 			 * if this code bloat is causing too much of a hit
28784c7070dbSScott Long 			 * move it to a separate function and mark it noinline
28794c7070dbSScott Long 			 */
28804c7070dbSScott Long 			if (m->m_len == pi->ipi_ehdrlen) {
28814c7070dbSScott Long 				n = m->m_next;
28824c7070dbSScott Long 				MPASS(n);
28834c7070dbSScott Long 				if (n->m_len >= sizeof(*ip))  {
28844c7070dbSScott Long 					ip = (struct ip *)n->m_data;
28854c7070dbSScott Long 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
28864c7070dbSScott Long 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
28874c7070dbSScott Long 				} else {
28884c7070dbSScott Long 					txq->ift_pullups++;
28894c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
28904c7070dbSScott Long 						return (ENOMEM);
28914c7070dbSScott Long 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
28924c7070dbSScott Long 				}
28934c7070dbSScott Long 			} else {
28944c7070dbSScott Long 				txq->ift_pullups++;
28954c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
28964c7070dbSScott Long 					return (ENOMEM);
28974c7070dbSScott Long 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
28984c7070dbSScott Long 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
28994c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
29004c7070dbSScott Long 			}
29014c7070dbSScott Long 		} else {
29024c7070dbSScott Long 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
29034c7070dbSScott Long 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
29044c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
29054c7070dbSScott Long 		}
29064c7070dbSScott Long 		pi->ipi_ip_hlen = ip->ip_hl << 2;
29074c7070dbSScott Long 		pi->ipi_ipproto = ip->ip_p;
29084c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV4;
29094c7070dbSScott Long 
2910c5cf2172SStephen Hurd 		if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP))
29114c7070dbSScott Long                        ip->ip_sum = 0;
29124c7070dbSScott Long 
2913c5cf2172SStephen Hurd 		if (IS_TSO4(pi)) {
29144c7070dbSScott Long 			if (pi->ipi_ipproto == IPPROTO_TCP) {
29154c7070dbSScott Long 				if (__predict_false(th == NULL)) {
29164c7070dbSScott Long 					txq->ift_pullups++;
29174c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
29184c7070dbSScott Long 						return (ENOMEM);
29194c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
29204c7070dbSScott Long 				}
29214c7070dbSScott Long 				pi->ipi_tcp_hflags = th->th_flags;
29224c7070dbSScott Long 				pi->ipi_tcp_hlen = th->th_off << 2;
29234c7070dbSScott Long 				pi->ipi_tcp_seq = th->th_seq;
29244c7070dbSScott Long 			}
29254c7070dbSScott Long 			if (__predict_false(ip->ip_p != IPPROTO_TCP))
29264c7070dbSScott Long 				return (ENXIO);
29274c7070dbSScott Long 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
29284c7070dbSScott Long 					       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
29294c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
29301248952aSSean Bruno 			if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
29311248952aSSean Bruno 				ip->ip_sum = 0;
29321248952aSSean Bruno 				ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
29331248952aSSean Bruno 			}
29344c7070dbSScott Long 		}
29354c7070dbSScott Long 		break;
29364c7070dbSScott Long 	}
29374c7070dbSScott Long #endif
29384c7070dbSScott Long #ifdef INET6
29394c7070dbSScott Long 	case ETHERTYPE_IPV6:
29404c7070dbSScott Long 	{
29414c7070dbSScott Long 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
29424c7070dbSScott Long 		struct tcphdr *th;
29434c7070dbSScott Long 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
29444c7070dbSScott Long 
29454c7070dbSScott Long 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
29464c7070dbSScott Long 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
29474c7070dbSScott Long 				return (ENOMEM);
29484c7070dbSScott Long 		}
29494c7070dbSScott Long 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
29504c7070dbSScott Long 
29514c7070dbSScott Long 		/* XXX-BZ this will go badly in case of ext hdrs. */
29524c7070dbSScott Long 		pi->ipi_ipproto = ip6->ip6_nxt;
29534c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV6;
29544c7070dbSScott Long 
2955c5cf2172SStephen Hurd 		if (IS_TSO6(pi)) {
29564c7070dbSScott Long 			if (pi->ipi_ipproto == IPPROTO_TCP) {
29574c7070dbSScott Long 				if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
29584c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
29594c7070dbSScott Long 						return (ENOMEM);
29604c7070dbSScott Long 				}
29614c7070dbSScott Long 				pi->ipi_tcp_hflags = th->th_flags;
29624c7070dbSScott Long 				pi->ipi_tcp_hlen = th->th_off << 2;
29634c7070dbSScott Long 			}
29644c7070dbSScott Long 
29654c7070dbSScott Long 			if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
29664c7070dbSScott Long 				return (ENXIO);
29674c7070dbSScott Long 			/*
29684c7070dbSScott Long 			 * The corresponding flag is set by the stack in the IPv4
29694c7070dbSScott Long 			 * TSO case, but not in IPv6 (at least in FreeBSD 10.2).
29704c7070dbSScott Long 			 * So, set it here because the rest of the flow requires it.
29714c7070dbSScott Long 			 */
29724c7070dbSScott Long 			pi->ipi_csum_flags |= CSUM_TCP_IPV6;
29734c7070dbSScott Long 			th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
29744c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
29754c7070dbSScott Long 		}
29764c7070dbSScott Long 		break;
29774c7070dbSScott Long 	}
29784c7070dbSScott Long #endif
29794c7070dbSScott Long 	default:
29804c7070dbSScott Long 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
29814c7070dbSScott Long 		pi->ipi_ip_hlen = 0;
29824c7070dbSScott Long 		break;
29834c7070dbSScott Long 	}
29844c7070dbSScott Long 	*mp = m;
29851248952aSSean Bruno 
29864c7070dbSScott Long 	return (0);
29874c7070dbSScott Long }
29884c7070dbSScott Long 
29894c7070dbSScott Long static  __noinline  struct mbuf *
29904c7070dbSScott Long collapse_pkthdr(struct mbuf *m0)
29914c7070dbSScott Long {
29924c7070dbSScott Long 	struct mbuf *m, *m_next, *tmp;
29934c7070dbSScott Long 
29944c7070dbSScott Long 	m = m0;
29954c7070dbSScott Long 	m_next = m->m_next;
29964c7070dbSScott Long 	while (m_next != NULL && m_next->m_len == 0) {
29974c7070dbSScott Long 		m = m_next;
29984c7070dbSScott Long 		m->m_next = NULL;
29994c7070dbSScott Long 		m_free(m);
30004c7070dbSScott Long 		m_next = m_next->m_next;
30014c7070dbSScott Long 	}
30024c7070dbSScott Long 	m = m0;
30034c7070dbSScott Long 	m->m_next = m_next;
30044c7070dbSScott Long 	if ((m_next->m_flags & M_EXT) == 0) {
30054c7070dbSScott Long 		m = m_defrag(m, M_NOWAIT);
30064c7070dbSScott Long 	} else {
30074c7070dbSScott Long 		tmp = m_next->m_next;
30084c7070dbSScott Long 		memcpy(m_next, m, MPKTHSIZE);
30094c7070dbSScott Long 		m = m_next;
30104c7070dbSScott Long 		m->m_next = tmp;
30114c7070dbSScott Long 	}
30124c7070dbSScott Long 	return (m);
30134c7070dbSScott Long }
30144c7070dbSScott Long 
30154c7070dbSScott Long /*
30164c7070dbSScott Long  * If dodgy hardware rejects the scatter gather chain we've handed it
301723ac9029SStephen Hurd  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
301823ac9029SStephen Hurd  * m_defrag'd mbufs
30194c7070dbSScott Long  */
30204c7070dbSScott Long static __noinline struct mbuf *
302123ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq)
30224c7070dbSScott Long {
302323ac9029SStephen Hurd 	int ntxd, i, pidx;
30244c7070dbSScott Long 	struct mbuf *m, *mh, **ifsd_m;
30254c7070dbSScott Long 
30264c7070dbSScott Long 	pidx = txq->ift_pidx;
30274c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
302823ac9029SStephen Hurd 	ntxd = txq->ift_size;
30294c7070dbSScott Long 	mh = m = ifsd_m[pidx];
30304c7070dbSScott Long 	ifsd_m[pidx] = NULL;
30314c7070dbSScott Long #if MEMORY_LOGGING
30324c7070dbSScott Long 	txq->ift_dequeued++;
30334c7070dbSScott Long #endif
30344c7070dbSScott Long 	i = 1;
30354c7070dbSScott Long 
303623ac9029SStephen Hurd 	while (m) {
30374c7070dbSScott Long 		ifsd_m[(pidx + i) & (ntxd -1)] = NULL;
30384c7070dbSScott Long #if MEMORY_LOGGING
30394c7070dbSScott Long 		txq->ift_dequeued++;
30404c7070dbSScott Long #endif
30414c7070dbSScott Long 		m = m->m_next;
30424c7070dbSScott Long 		i++;
30434c7070dbSScott Long 	}
30444c7070dbSScott Long 	return (mh);
30454c7070dbSScott Long }
30464c7070dbSScott Long 
30474c7070dbSScott Long static int
30484c7070dbSScott Long iflib_busdma_load_mbuf_sg(iflib_txq_t txq, bus_dma_tag_t tag, bus_dmamap_t map,
30494c7070dbSScott Long 			  struct mbuf **m0, bus_dma_segment_t *segs, int *nsegs,
30504c7070dbSScott Long 			  int max_segs, int flags)
30514c7070dbSScott Long {
30524c7070dbSScott Long 	if_ctx_t ctx;
30534c7070dbSScott Long 	if_shared_ctx_t		sctx;
305423ac9029SStephen Hurd 	if_softc_ctx_t		scctx;
305525d52811SSean Bruno 	int i, next, pidx, err, ntxd, count;
3056aa8fa07cSSean Bruno 	struct mbuf *m, *tmp, **ifsd_m;
30574c7070dbSScott Long 
30584c7070dbSScott Long 	m = *m0;
30594c7070dbSScott Long 
30604c7070dbSScott Long 	/*
30614c7070dbSScott Long 	 * Please don't ever do this
30624c7070dbSScott Long 	 */
30634c7070dbSScott Long 	if (__predict_false(m->m_len == 0))
30644c7070dbSScott Long 		*m0 = m = collapse_pkthdr(m);
30654c7070dbSScott Long 
30664c7070dbSScott Long 	ctx = txq->ift_ctx;
30674c7070dbSScott Long 	sctx = ctx->ifc_sctx;
306823ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
30694c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
307023ac9029SStephen Hurd 	ntxd = txq->ift_size;
30714c7070dbSScott Long 	pidx = txq->ift_pidx;
3072ab2e3f79SStephen Hurd 	if (map != NULL) {
30734c7070dbSScott Long 		uint8_t *ifsd_flags = txq->ift_sds.ifsd_flags;
3074ab2e3f79SStephen Hurd 
30754c7070dbSScott Long 		err = bus_dmamap_load_mbuf_sg(tag, map,
30764c7070dbSScott Long 					      *m0, segs, nsegs, BUS_DMA_NOWAIT);
30774c7070dbSScott Long 		if (err)
30784c7070dbSScott Long 			return (err);
30794c7070dbSScott Long 		ifsd_flags[pidx] |= TX_SW_DESC_MAPPED;
3080aa8fa07cSSean Bruno 		count = 0;
30814c7070dbSScott Long 		m = *m0;
30824c7070dbSScott Long 		do {
3083aa8fa07cSSean Bruno 			if (__predict_false(m->m_len <= 0)) {
3084aa8fa07cSSean Bruno 				tmp = m;
30854c7070dbSScott Long 				m = m->m_next;
3086aa8fa07cSSean Bruno 				tmp->m_next = NULL;
3087aa8fa07cSSean Bruno 				m_free(tmp);
3088aa8fa07cSSean Bruno 				continue;
3089aa8fa07cSSean Bruno 			}
3090dcbc025fSSean Bruno 			m = m->m_next;
3091dcbc025fSSean Bruno 			count++;
3092dcbc025fSSean Bruno 		} while (m != NULL);
30935c5ca36cSSean Bruno 		if (count > *nsegs) {
30945c5ca36cSSean Bruno 			ifsd_m[pidx] = *m0;
30955c5ca36cSSean Bruno 			ifsd_m[pidx]->m_flags |= M_TOOBIG;
3096dcbc025fSSean Bruno 			return (0);
30975c5ca36cSSean Bruno 		}
3098dcbc025fSSean Bruno 		m = *m0;
3099dcbc025fSSean Bruno 		count = 0;
3100dcbc025fSSean Bruno 		do {
3101aa8fa07cSSean Bruno 			next = (pidx + count) & (ntxd-1);
3102aa8fa07cSSean Bruno 			MPASS(ifsd_m[next] == NULL);
3103aa8fa07cSSean Bruno 			ifsd_m[next] = m;
3104aa8fa07cSSean Bruno 			count++;
3105aa8fa07cSSean Bruno 			tmp = m;
3106aa8fa07cSSean Bruno 			m = m->m_next;
31074c7070dbSScott Long 		} while (m != NULL);
31084c7070dbSScott Long 	} else {
310925d52811SSean Bruno 		int buflen, sgsize, maxsegsz, max_sgsize;
31104c7070dbSScott Long 		vm_offset_t vaddr;
31114c7070dbSScott Long 		vm_paddr_t curaddr;
31124c7070dbSScott Long 
31134c7070dbSScott Long 		count = i = 0;
31144c7070dbSScott Long 		m = *m0;
311525d52811SSean Bruno 		if (m->m_pkthdr.csum_flags & CSUM_TSO)
311625d52811SSean Bruno 			maxsegsz = scctx->isc_tx_tso_segsize_max;
311725d52811SSean Bruno 		else
311825d52811SSean Bruno 			maxsegsz = sctx->isc_tx_maxsegsize;
311925d52811SSean Bruno 
31204c7070dbSScott Long 		do {
31214c7070dbSScott Long 			if (__predict_false(m->m_len <= 0)) {
31224c7070dbSScott Long 				tmp = m;
31234c7070dbSScott Long 				m = m->m_next;
31244c7070dbSScott Long 				tmp->m_next = NULL;
31254c7070dbSScott Long 				m_free(tmp);
31264c7070dbSScott Long 				continue;
31274c7070dbSScott Long 			}
31284c7070dbSScott Long 			buflen = m->m_len;
31294c7070dbSScott Long 			vaddr = (vm_offset_t)m->m_data;
31304c7070dbSScott Long 			/*
31314c7070dbSScott Long 			 * see if we can't be smarter about physically
31324c7070dbSScott Long 			 * contiguous mappings
31334c7070dbSScott Long 			 */
31344c7070dbSScott Long 			next = (pidx + count) & (ntxd-1);
31354c7070dbSScott Long 			MPASS(ifsd_m[next] == NULL);
31364c7070dbSScott Long #if MEMORY_LOGGING
31374c7070dbSScott Long 			txq->ift_enqueued++;
31384c7070dbSScott Long #endif
31394c7070dbSScott Long 			ifsd_m[next] = m;
31404c7070dbSScott Long 			while (buflen > 0) {
31419d35858fSSean Bruno 				if (i >= max_segs)
31429d35858fSSean Bruno 					goto err;
31434c7070dbSScott Long 				max_sgsize = MIN(buflen, maxsegsz);
31444c7070dbSScott Long 				curaddr = pmap_kextract(vaddr);
31454c7070dbSScott Long 				sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
31464c7070dbSScott Long 				sgsize = MIN(sgsize, max_sgsize);
31474c7070dbSScott Long 				segs[i].ds_addr = curaddr;
31484c7070dbSScott Long 				segs[i].ds_len = sgsize;
31494c7070dbSScott Long 				vaddr += sgsize;
31504c7070dbSScott Long 				buflen -= sgsize;
31514c7070dbSScott Long 				i++;
31524c7070dbSScott Long 			}
31534c7070dbSScott Long 			count++;
31544c7070dbSScott Long 			tmp = m;
31554c7070dbSScott Long 			m = m->m_next;
31564c7070dbSScott Long 		} while (m != NULL);
31574c7070dbSScott Long 		*nsegs = i;
31584c7070dbSScott Long 	}
31594c7070dbSScott Long 	return (0);
31604c7070dbSScott Long err:
316123ac9029SStephen Hurd 	*m0 = iflib_remove_mbuf(txq);
31624c7070dbSScott Long 	return (EFBIG);
31634c7070dbSScott Long }
31644c7070dbSScott Long 
316595246abbSSean Bruno static inline caddr_t
316695246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid)
316795246abbSSean Bruno {
316895246abbSSean Bruno 	qidx_t size;
316995246abbSSean Bruno 	int ntxd;
317095246abbSSean Bruno 	caddr_t start, end, cur, next;
317195246abbSSean Bruno 
317295246abbSSean Bruno 	ntxd = txq->ift_size;
317395246abbSSean Bruno 	size = txq->ift_txd_size[qid];
317495246abbSSean Bruno 	start = txq->ift_ifdi[qid].idi_vaddr;
317595246abbSSean Bruno 
317695246abbSSean Bruno 	if (__predict_false(size == 0))
317795246abbSSean Bruno 		return (start);
317895246abbSSean Bruno 	cur = start + size*cidx;
317995246abbSSean Bruno 	end = start + size*ntxd;
318095246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
318195246abbSSean Bruno 	return (next < end ? next : start);
318295246abbSSean Bruno }
318395246abbSSean Bruno 
3184d14c853bSStephen Hurd /*
3185d14c853bSStephen Hurd  * Pad an mbuf to ensure a minimum ethernet frame size.
3186d14c853bSStephen Hurd  * min_frame_size is the frame size (less CRC) to pad the mbuf to
3187d14c853bSStephen Hurd  */
3188d14c853bSStephen Hurd static __noinline int
3189a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size)
3190d14c853bSStephen Hurd {
3191d14c853bSStephen Hurd 	/*
3192d14c853bSStephen Hurd 	 * 18 is enough bytes to pad an ARP packet to 46 bytes, and
3193d14c853bSStephen Hurd 	 * and ARP message is the smallest common payload I can think of
3194d14c853bSStephen Hurd 	 */
3195d14c853bSStephen Hurd 	static char pad[18];	/* just zeros */
3196d14c853bSStephen Hurd 	int n;
3197a15fbbb8SStephen Hurd 	struct mbuf *new_head;
3198d14c853bSStephen Hurd 
3199a15fbbb8SStephen Hurd 	if (!M_WRITABLE(*m_head)) {
3200a15fbbb8SStephen Hurd 		new_head = m_dup(*m_head, M_NOWAIT);
3201a15fbbb8SStephen Hurd 		if (new_head == NULL) {
320204993890SStephen Hurd 			m_freem(*m_head);
3203a15fbbb8SStephen Hurd 			device_printf(dev, "cannot pad short frame, m_dup() failed");
320406c47d48SStephen Hurd 			DBG_COUNTER_INC(encap_pad_mbuf_fail);
3205a15fbbb8SStephen Hurd 			return ENOMEM;
3206a15fbbb8SStephen Hurd 		}
3207a15fbbb8SStephen Hurd 		m_freem(*m_head);
3208a15fbbb8SStephen Hurd 		*m_head = new_head;
3209a15fbbb8SStephen Hurd 	}
3210a15fbbb8SStephen Hurd 
3211a15fbbb8SStephen Hurd 	for (n = min_frame_size - (*m_head)->m_pkthdr.len;
3212d14c853bSStephen Hurd 	     n > 0; n -= sizeof(pad))
3213a15fbbb8SStephen Hurd 		if (!m_append(*m_head, min(n, sizeof(pad)), pad))
3214d14c853bSStephen Hurd 			break;
3215d14c853bSStephen Hurd 
3216d14c853bSStephen Hurd 	if (n > 0) {
3217a15fbbb8SStephen Hurd 		m_freem(*m_head);
3218d14c853bSStephen Hurd 		device_printf(dev, "cannot pad short frame\n");
3219d14c853bSStephen Hurd 		DBG_COUNTER_INC(encap_pad_mbuf_fail);
3220d14c853bSStephen Hurd 		return (ENOBUFS);
3221d14c853bSStephen Hurd 	}
3222d14c853bSStephen Hurd 
3223d14c853bSStephen Hurd 	return 0;
3224d14c853bSStephen Hurd }
3225d14c853bSStephen Hurd 
32264c7070dbSScott Long static int
32274c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
32284c7070dbSScott Long {
32294c7070dbSScott Long 	if_ctx_t		ctx;
32304c7070dbSScott Long 	if_shared_ctx_t		sctx;
32314c7070dbSScott Long 	if_softc_ctx_t		scctx;
32324c7070dbSScott Long 	bus_dma_segment_t	*segs;
32334c7070dbSScott Long 	struct mbuf		*m_head;
323495246abbSSean Bruno 	void			*next_txd;
32354c7070dbSScott Long 	bus_dmamap_t		map;
32364c7070dbSScott Long 	struct if_pkt_info	pi;
32374c7070dbSScott Long 	int remap = 0;
32384c7070dbSScott Long 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
32394c7070dbSScott Long 	bus_dma_tag_t desc_tag;
32404c7070dbSScott Long 
32414c7070dbSScott Long 	segs = txq->ift_segs;
32424c7070dbSScott Long 	ctx = txq->ift_ctx;
32434c7070dbSScott Long 	sctx = ctx->ifc_sctx;
32444c7070dbSScott Long 	scctx = &ctx->ifc_softc_ctx;
32454c7070dbSScott Long 	segs = txq->ift_segs;
324623ac9029SStephen Hurd 	ntxd = txq->ift_size;
32474c7070dbSScott Long 	m_head = *m_headp;
32484c7070dbSScott Long 	map = NULL;
32494c7070dbSScott Long 
32504c7070dbSScott Long 	/*
32514c7070dbSScott Long 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
32524c7070dbSScott Long 	 */
32534c7070dbSScott Long 	cidx = txq->ift_cidx;
32544c7070dbSScott Long 	pidx = txq->ift_pidx;
325595246abbSSean Bruno 	if (ctx->ifc_flags & IFC_PREFETCH) {
32564c7070dbSScott Long 		next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
325795246abbSSean Bruno 		if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) {
325895246abbSSean Bruno 			next_txd = calc_next_txd(txq, cidx, 0);
325995246abbSSean Bruno 			prefetch(next_txd);
326095246abbSSean Bruno 		}
32614c7070dbSScott Long 
32624c7070dbSScott Long 		/* prefetch the next cache line of mbuf pointers and flags */
32634c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_m[next]);
32644c7070dbSScott Long 		if (txq->ift_sds.ifsd_map != NULL) {
32654c7070dbSScott Long 			prefetch(&txq->ift_sds.ifsd_map[next]);
32664c7070dbSScott Long 			next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
32674c7070dbSScott Long 			prefetch(&txq->ift_sds.ifsd_flags[next]);
32684c7070dbSScott Long 		}
3269ab2e3f79SStephen Hurd 	} else if (txq->ift_sds.ifsd_map != NULL)
327095246abbSSean Bruno 		map = txq->ift_sds.ifsd_map[pidx];
32714c7070dbSScott Long 
32724c7070dbSScott Long 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
32734c7070dbSScott Long 		desc_tag = txq->ift_tso_desc_tag;
32744c7070dbSScott Long 		max_segs = scctx->isc_tx_tso_segments_max;
32754c7070dbSScott Long 	} else {
32764c7070dbSScott Long 		desc_tag = txq->ift_desc_tag;
32774c7070dbSScott Long 		max_segs = scctx->isc_tx_nsegments;
32784c7070dbSScott Long 	}
3279d14c853bSStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) &&
3280d14c853bSStephen Hurd 	    __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) {
3281a15fbbb8SStephen Hurd 		err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size);
3282d14c853bSStephen Hurd 		if (err)
3283d14c853bSStephen Hurd 			return err;
3284d14c853bSStephen Hurd 	}
3285a15fbbb8SStephen Hurd 	m_head = *m_headp;
328695246abbSSean Bruno 
328795246abbSSean Bruno 	pkt_info_zero(&pi);
3288ab2e3f79SStephen Hurd 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
3289ab2e3f79SStephen Hurd 	pi.ipi_pidx = pidx;
3290ab2e3f79SStephen Hurd 	pi.ipi_qsidx = txq->ift_id;
32913429c02fSStephen Hurd 	pi.ipi_len = m_head->m_pkthdr.len;
32923429c02fSStephen Hurd 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
32933429c02fSStephen Hurd 	pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0;
32944c7070dbSScott Long 
32954c7070dbSScott Long 	/* deliberate bitwise OR to make one condition */
32964c7070dbSScott Long 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
32974c7070dbSScott Long 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0))
32984c7070dbSScott Long 			return (err);
32994c7070dbSScott Long 		m_head = *m_headp;
33004c7070dbSScott Long 	}
33014c7070dbSScott Long 
33024c7070dbSScott Long retry:
33034c7070dbSScott Long 	err = iflib_busdma_load_mbuf_sg(txq, desc_tag, map, m_headp, segs, &nsegs, max_segs, BUS_DMA_NOWAIT);
33044c7070dbSScott Long defrag:
33054c7070dbSScott Long 	if (__predict_false(err)) {
33064c7070dbSScott Long 		switch (err) {
33074c7070dbSScott Long 		case EFBIG:
33084c7070dbSScott Long 			/* try collapse once and defrag once */
3309f7594707SAndrew Gallatin 			if (remap == 0) {
33104c7070dbSScott Long 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
3311f7594707SAndrew Gallatin 				/* try defrag if collapsing fails */
3312f7594707SAndrew Gallatin 				if (m_head == NULL)
3313f7594707SAndrew Gallatin 					remap++;
3314f7594707SAndrew Gallatin 			}
33154c7070dbSScott Long 			if (remap == 1)
33164c7070dbSScott Long 				m_head = m_defrag(*m_headp, M_NOWAIT);
33174c7070dbSScott Long 			remap++;
33184c7070dbSScott Long 			if (__predict_false(m_head == NULL))
33194c7070dbSScott Long 				goto defrag_failed;
33204c7070dbSScott Long 			txq->ift_mbuf_defrag++;
33214c7070dbSScott Long 			*m_headp = m_head;
33224c7070dbSScott Long 			goto retry;
33234c7070dbSScott Long 			break;
33244c7070dbSScott Long 		case ENOMEM:
33254c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
33264c7070dbSScott Long 			break;
33274c7070dbSScott Long 		default:
33284c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
33294c7070dbSScott Long 			m_freem(*m_headp);
33304c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
33314c7070dbSScott Long 			*m_headp = NULL;
33324c7070dbSScott Long 			break;
33334c7070dbSScott Long 		}
33344c7070dbSScott Long 		txq->ift_map_failed++;
33354c7070dbSScott Long 		DBG_COUNTER_INC(encap_load_mbuf_fail);
33364c7070dbSScott Long 		return (err);
33374c7070dbSScott Long 	}
33384c7070dbSScott Long 
33394c7070dbSScott Long 	/*
33404c7070dbSScott Long 	 * XXX assumes a 1 to 1 relationship between segments and
33414c7070dbSScott Long 	 *        descriptors - this does not hold true on all drivers, e.g.
33424c7070dbSScott Long 	 *        cxgb
33434c7070dbSScott Long 	 */
33444c7070dbSScott Long 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
33454c7070dbSScott Long 		txq->ift_no_desc_avail++;
33464c7070dbSScott Long 		if (map != NULL)
33474c7070dbSScott Long 			bus_dmamap_unload(desc_tag, map);
33484c7070dbSScott Long 		DBG_COUNTER_INC(encap_txq_avail_fail);
334923ac9029SStephen Hurd 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
33504c7070dbSScott Long 			GROUPTASK_ENQUEUE(&txq->ift_task);
33514c7070dbSScott Long 		return (ENOBUFS);
33524c7070dbSScott Long 	}
335395246abbSSean Bruno 	/*
335495246abbSSean Bruno 	 * On Intel cards we can greatly reduce the number of TX interrupts
335595246abbSSean Bruno 	 * we see by only setting report status on every Nth descriptor.
335695246abbSSean Bruno 	 * However, this also means that the driver will need to keep track
335795246abbSSean Bruno 	 * of the descriptors that RS was set on to check them for the DD bit.
335895246abbSSean Bruno 	 */
335995246abbSSean Bruno 	txq->ift_rs_pending += nsegs + 1;
336095246abbSSean Bruno 	if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) ||
33611f7ce05dSAndrew Gallatin 	     iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) {
336295246abbSSean Bruno 		pi.ipi_flags |= IPI_TX_INTR;
336395246abbSSean Bruno 		txq->ift_rs_pending = 0;
336495246abbSSean Bruno 	}
336595246abbSSean Bruno 
33664c7070dbSScott Long 	pi.ipi_segs = segs;
33674c7070dbSScott Long 	pi.ipi_nsegs = nsegs;
33684c7070dbSScott Long 
336923ac9029SStephen Hurd 	MPASS(pidx >= 0 && pidx < txq->ift_size);
33704c7070dbSScott Long #ifdef PKT_DEBUG
33714c7070dbSScott Long 	print_pkt(&pi);
33724c7070dbSScott Long #endif
337395246abbSSean Bruno 	if (map != NULL)
337495246abbSSean Bruno 		bus_dmamap_sync(desc_tag, map, BUS_DMASYNC_PREWRITE);
33754c7070dbSScott Long 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
337695246abbSSean Bruno 		if (map != NULL)
33774c7070dbSScott Long 			bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
33784c7070dbSScott Long 					BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
33794c7070dbSScott Long 		DBG_COUNTER_INC(tx_encap);
338095246abbSSean Bruno 		MPASS(pi.ipi_new_pidx < txq->ift_size);
33814c7070dbSScott Long 
33824c7070dbSScott Long 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
33834c7070dbSScott Long 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
338423ac9029SStephen Hurd 			ndesc += txq->ift_size;
33854c7070dbSScott Long 			txq->ift_gen = 1;
33864c7070dbSScott Long 		}
33871248952aSSean Bruno 		/*
33881248952aSSean Bruno 		 * drivers can need as many as
33891248952aSSean Bruno 		 * two sentinels
33901248952aSSean Bruno 		 */
33911248952aSSean Bruno 		MPASS(ndesc <= pi.ipi_nsegs + 2);
33924c7070dbSScott Long 		MPASS(pi.ipi_new_pidx != pidx);
33934c7070dbSScott Long 		MPASS(ndesc > 0);
33944c7070dbSScott Long 		txq->ift_in_use += ndesc;
339595246abbSSean Bruno 
33964c7070dbSScott Long 		/*
33974c7070dbSScott Long 		 * We update the last software descriptor again here because there may
33984c7070dbSScott Long 		 * be a sentinel and/or there may be more mbufs than segments
33994c7070dbSScott Long 		 */
34004c7070dbSScott Long 		txq->ift_pidx = pi.ipi_new_pidx;
34014c7070dbSScott Long 		txq->ift_npending += pi.ipi_ndescs;
3402f7594707SAndrew Gallatin 	} else {
340323ac9029SStephen Hurd 		*m_headp = m_head = iflib_remove_mbuf(txq);
3404f7594707SAndrew Gallatin 		if (err == EFBIG) {
34054c7070dbSScott Long 			txq->ift_txd_encap_efbig++;
3406f7594707SAndrew Gallatin 			if (remap < 2) {
3407f7594707SAndrew Gallatin 				remap = 1;
34084c7070dbSScott Long 				goto defrag;
3409f7594707SAndrew Gallatin 			}
3410f7594707SAndrew Gallatin 		}
34114c7070dbSScott Long 		DBG_COUNTER_INC(encap_txd_encap_fail);
3412f7594707SAndrew Gallatin 		goto defrag_failed;
3413f7594707SAndrew Gallatin 	}
34144c7070dbSScott Long 	return (err);
34154c7070dbSScott Long 
34164c7070dbSScott Long defrag_failed:
34174c7070dbSScott Long 	txq->ift_mbuf_defrag_failed++;
34184c7070dbSScott Long 	txq->ift_map_failed++;
34194c7070dbSScott Long 	m_freem(*m_headp);
34204c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
34214c7070dbSScott Long 	*m_headp = NULL;
34224c7070dbSScott Long 	return (ENOMEM);
34234c7070dbSScott Long }
34244c7070dbSScott Long 
34254c7070dbSScott Long static void
34264c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n)
34274c7070dbSScott Long {
34284c7070dbSScott Long 	int hasmap;
34294c7070dbSScott Long 	uint32_t qsize, cidx, mask, gen;
34304c7070dbSScott Long 	struct mbuf *m, **ifsd_m;
34314c7070dbSScott Long 	uint8_t *ifsd_flags;
34324c7070dbSScott Long 	bus_dmamap_t *ifsd_map;
343395246abbSSean Bruno 	bool do_prefetch;
34344c7070dbSScott Long 
34354c7070dbSScott Long 	cidx = txq->ift_cidx;
34364c7070dbSScott Long 	gen = txq->ift_gen;
343723ac9029SStephen Hurd 	qsize = txq->ift_size;
34384c7070dbSScott Long 	mask = qsize-1;
34394c7070dbSScott Long 	hasmap = txq->ift_sds.ifsd_map != NULL;
34404c7070dbSScott Long 	ifsd_flags = txq->ift_sds.ifsd_flags;
34414c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
34424c7070dbSScott Long 	ifsd_map = txq->ift_sds.ifsd_map;
344395246abbSSean Bruno 	do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH);
34444c7070dbSScott Long 
344594618825SMark Johnston 	while (n-- > 0) {
344695246abbSSean Bruno 		if (do_prefetch) {
34474c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 3) & mask]);
34484c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 4) & mask]);
344995246abbSSean Bruno 		}
34504c7070dbSScott Long 		if (ifsd_m[cidx] != NULL) {
34514c7070dbSScott Long 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
34524c7070dbSScott Long 			prefetch(&ifsd_flags[(cidx + CACHE_PTR_INCREMENT) & mask]);
34534c7070dbSScott Long 			if (hasmap && (ifsd_flags[cidx] & TX_SW_DESC_MAPPED)) {
34544c7070dbSScott Long 				/*
34554c7070dbSScott Long 				 * does it matter if it's not the TSO tag? If so we'll
34564c7070dbSScott Long 				 * have to add the type to flags
34574c7070dbSScott Long 				 */
34584c7070dbSScott Long 				bus_dmamap_unload(txq->ift_desc_tag, ifsd_map[cidx]);
34594c7070dbSScott Long 				ifsd_flags[cidx] &= ~TX_SW_DESC_MAPPED;
34604c7070dbSScott Long 			}
34614c7070dbSScott Long 			if ((m = ifsd_m[cidx]) != NULL) {
34624c7070dbSScott Long 				/* XXX we don't support any drivers that batch packets yet */
34634c7070dbSScott Long 				MPASS(m->m_nextpkt == NULL);
34645c5ca36cSSean Bruno 				/* if the number of clusters exceeds the number of segments
34655c5ca36cSSean Bruno 				 * there won't be space on the ring to save a pointer to each
34665c5ca36cSSean Bruno 				 * cluster so we simply free the list here
34675c5ca36cSSean Bruno 				 */
34685c5ca36cSSean Bruno 				if (m->m_flags & M_TOOBIG) {
34695c5ca36cSSean Bruno 					m_freem(m);
34705c5ca36cSSean Bruno 				} else {
347123ac9029SStephen Hurd 					m_free(m);
34725c5ca36cSSean Bruno 				}
34734c7070dbSScott Long 				ifsd_m[cidx] = NULL;
34744c7070dbSScott Long #if MEMORY_LOGGING
34754c7070dbSScott Long 				txq->ift_dequeued++;
34764c7070dbSScott Long #endif
34774c7070dbSScott Long 				DBG_COUNTER_INC(tx_frees);
34784c7070dbSScott Long 			}
34794c7070dbSScott Long 		}
34804c7070dbSScott Long 		if (__predict_false(++cidx == qsize)) {
34814c7070dbSScott Long 			cidx = 0;
34824c7070dbSScott Long 			gen = 0;
34834c7070dbSScott Long 		}
34844c7070dbSScott Long 	}
34854c7070dbSScott Long 	txq->ift_cidx = cidx;
34864c7070dbSScott Long 	txq->ift_gen = gen;
34874c7070dbSScott Long }
34884c7070dbSScott Long 
34894c7070dbSScott Long static __inline int
34904c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
34914c7070dbSScott Long {
34924c7070dbSScott Long 	int reclaim;
34934c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
34944c7070dbSScott Long 
34954c7070dbSScott Long 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
34964c7070dbSScott Long 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
34974c7070dbSScott Long 
34984c7070dbSScott Long 	/*
34994c7070dbSScott Long 	 * Need a rate-limiting check so that this isn't called every time
35004c7070dbSScott Long 	 */
35014c7070dbSScott Long 	iflib_tx_credits_update(ctx, txq);
35024c7070dbSScott Long 	reclaim = DESC_RECLAIMABLE(txq);
35034c7070dbSScott Long 
35044c7070dbSScott Long 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
35054c7070dbSScott Long #ifdef INVARIANTS
35064c7070dbSScott Long 		if (iflib_verbose_debug) {
35074c7070dbSScott Long 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
35084c7070dbSScott Long 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
35094c7070dbSScott Long 			       reclaim, thresh);
35104c7070dbSScott Long 
35114c7070dbSScott Long 		}
35124c7070dbSScott Long #endif
35134c7070dbSScott Long 		return (0);
35144c7070dbSScott Long 	}
35154c7070dbSScott Long 	iflib_tx_desc_free(txq, reclaim);
35164c7070dbSScott Long 	txq->ift_cleaned += reclaim;
35174c7070dbSScott Long 	txq->ift_in_use -= reclaim;
35184c7070dbSScott Long 
35194c7070dbSScott Long 	return (reclaim);
35204c7070dbSScott Long }
35214c7070dbSScott Long 
35224c7070dbSScott Long static struct mbuf **
352395246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
35244c7070dbSScott Long {
352595246abbSSean Bruno 	int next, size;
352695246abbSSean Bruno 	struct mbuf **items;
35274c7070dbSScott Long 
352895246abbSSean Bruno 	size = r->size;
352995246abbSSean Bruno 	next = (cidx + CACHE_PTR_INCREMENT) & (size-1);
353095246abbSSean Bruno 	items = __DEVOLATILE(struct mbuf **, &r->items[0]);
353195246abbSSean Bruno 
353295246abbSSean Bruno 	prefetch(items[(cidx + offset) & (size-1)]);
353395246abbSSean Bruno 	if (remaining > 1) {
35343429c02fSStephen Hurd 		prefetch2cachelines(&items[next]);
35353429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]);
35363429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]);
35373429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]);
353895246abbSSean Bruno 	}
353995246abbSSean Bruno 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)]));
35404c7070dbSScott Long }
35414c7070dbSScott Long 
35424c7070dbSScott Long static void
35434c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget)
35444c7070dbSScott Long {
35454c7070dbSScott Long 
354695246abbSSean Bruno 	ifmp_ring_check_drainage(txq->ift_br, budget);
35474c7070dbSScott Long }
35484c7070dbSScott Long 
35494c7070dbSScott Long static uint32_t
35504c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r)
35514c7070dbSScott Long {
35524c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
35534c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
35544c7070dbSScott Long 
35551248952aSSean Bruno 	return ((TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) ||
355695246abbSSean Bruno 		ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false));
35574c7070dbSScott Long }
35584c7070dbSScott Long 
35594c7070dbSScott Long static uint32_t
35604c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
35614c7070dbSScott Long {
35624c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
35634c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
356495246abbSSean Bruno 	struct ifnet *ifp = ctx->ifc_ifp;
35654c7070dbSScott Long 	struct mbuf **mp, *m;
356695246abbSSean Bruno 	int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail;
356795246abbSSean Bruno 	int reclaimed, err, in_use_prev, desc_used;
356895246abbSSean Bruno 	bool do_prefetch, ring, rang;
35694c7070dbSScott Long 
35704c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
35714c7070dbSScott Long 			    !LINK_ACTIVE(ctx))) {
35724c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_notready);
35734c7070dbSScott Long 		return (0);
35744c7070dbSScott Long 	}
357595246abbSSean Bruno 	reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
357695246abbSSean Bruno 	rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use);
35774c7070dbSScott Long 	avail = IDXDIFF(pidx, cidx, r->size);
35784c7070dbSScott Long 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
35794c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_flushing);
35804c7070dbSScott Long 		for (i = 0; i < avail; i++) {
358123ac9029SStephen Hurd 			m_free(r->items[(cidx + i) & (r->size-1)]);
35824c7070dbSScott Long 			r->items[(cidx + i) & (r->size-1)] = NULL;
35834c7070dbSScott Long 		}
35844c7070dbSScott Long 		return (avail);
35854c7070dbSScott Long 	}
358695246abbSSean Bruno 
35874c7070dbSScott Long 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
35884c7070dbSScott Long 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
35894c7070dbSScott Long 		CALLOUT_LOCK(txq);
35904c7070dbSScott Long 		callout_stop(&txq->ift_timer);
35914c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
35924c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_oactive);
35934c7070dbSScott Long 		return (0);
35944c7070dbSScott Long 	}
359595246abbSSean Bruno 	if (reclaimed)
359695246abbSSean Bruno 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
35974c7070dbSScott Long 	consumed = mcast_sent = bytes_sent = pkt_sent = 0;
35984c7070dbSScott Long 	count = MIN(avail, TX_BATCH_SIZE);
3599da69b8f9SSean Bruno #ifdef INVARIANTS
3600da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3601da69b8f9SSean Bruno 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3602da69b8f9SSean Bruno 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3603da69b8f9SSean Bruno #endif
360495246abbSSean Bruno 	do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
360595246abbSSean Bruno 	avail = TXQ_AVAIL(txq);
36061ae4848cSMatt Macy 	err = 0;
360795246abbSSean Bruno 	for (desc_used = i = 0; i < count && avail > MAX_TX_DESC(ctx) + 2; i++) {
36081ae4848cSMatt Macy 		int rem = do_prefetch ? count - i : 0;
36094c7070dbSScott Long 
361095246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, rem);
3611da69b8f9SSean Bruno 		MPASS(mp != NULL && *mp != NULL);
361295246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq)) {
361395246abbSSean Bruno 			consumed++;
361495246abbSSean Bruno 			reclaimed++;
361595246abbSSean Bruno 			continue;
361695246abbSSean Bruno 		}
36174c7070dbSScott Long 		in_use_prev = txq->ift_in_use;
361895246abbSSean Bruno 		err = iflib_encap(txq, mp);
361995246abbSSean Bruno 		if (__predict_false(err)) {
36204c7070dbSScott Long 			DBG_COUNTER_INC(txq_drain_encapfail);
3621da69b8f9SSean Bruno 			/* no room - bail out */
362295246abbSSean Bruno 			if (err == ENOBUFS)
36234c7070dbSScott Long 				break;
36244c7070dbSScott Long 			consumed++;
3625da69b8f9SSean Bruno 			DBG_COUNTER_INC(txq_drain_encapfail);
3626da69b8f9SSean Bruno 			/* we can't send this packet - skip it */
36274c7070dbSScott Long 			continue;
3628da69b8f9SSean Bruno 		}
362995246abbSSean Bruno 		consumed++;
36304c7070dbSScott Long 		pkt_sent++;
36314c7070dbSScott Long 		m = *mp;
36324c7070dbSScott Long 		DBG_COUNTER_INC(tx_sent);
36334c7070dbSScott Long 		bytes_sent += m->m_pkthdr.len;
363495246abbSSean Bruno 		mcast_sent += !!(m->m_flags & M_MCAST);
363595246abbSSean Bruno 		avail = TXQ_AVAIL(txq);
36364c7070dbSScott Long 
36374c7070dbSScott Long 		txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
36384c7070dbSScott Long 		desc_used += (txq->ift_in_use - in_use_prev);
36394c7070dbSScott Long 		ETHER_BPF_MTAP(ifp, m);
364095246abbSSean Bruno 		if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
36414c7070dbSScott Long 			break;
364295246abbSSean Bruno 		rang = iflib_txd_db_check(ctx, txq, false, in_use_prev);
36434c7070dbSScott Long 	}
36444c7070dbSScott Long 
364595246abbSSean Bruno 	/* deliberate use of bitwise or to avoid gratuitous short-circuit */
364695246abbSSean Bruno 	ring = rang ? false  : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx));
364795246abbSSean Bruno 	iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use);
36484c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
36494c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
36504c7070dbSScott Long 	if (mcast_sent)
36514c7070dbSScott Long 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3652da69b8f9SSean Bruno #ifdef INVARIANTS
3653da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3654da69b8f9SSean Bruno 		printf("consumed=%d\n", consumed);
3655da69b8f9SSean Bruno #endif
36564c7070dbSScott Long 	return (consumed);
36574c7070dbSScott Long }
36584c7070dbSScott Long 
3659da69b8f9SSean Bruno static uint32_t
3660da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r)
3661da69b8f9SSean Bruno {
3662da69b8f9SSean Bruno 	return (1);
3663da69b8f9SSean Bruno }
3664da69b8f9SSean Bruno 
3665da69b8f9SSean Bruno static uint32_t
3666da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3667da69b8f9SSean Bruno {
3668da69b8f9SSean Bruno 	int i, avail;
3669da69b8f9SSean Bruno 	struct mbuf **mp;
3670da69b8f9SSean Bruno 	iflib_txq_t txq;
3671da69b8f9SSean Bruno 
3672da69b8f9SSean Bruno 	txq = r->cookie;
3673da69b8f9SSean Bruno 
3674da69b8f9SSean Bruno 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3675da69b8f9SSean Bruno 	CALLOUT_LOCK(txq);
3676da69b8f9SSean Bruno 	callout_stop(&txq->ift_timer);
3677da69b8f9SSean Bruno 	CALLOUT_UNLOCK(txq);
3678da69b8f9SSean Bruno 
3679da69b8f9SSean Bruno 	avail = IDXDIFF(pidx, cidx, r->size);
3680da69b8f9SSean Bruno 	for (i = 0; i < avail; i++) {
368195246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, avail - i);
368295246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq))
368395246abbSSean Bruno 			continue;
3684da69b8f9SSean Bruno 		m_freem(*mp);
3685da69b8f9SSean Bruno 	}
3686da69b8f9SSean Bruno 	MPASS(ifmp_ring_is_stalled(r) == 0);
3687da69b8f9SSean Bruno 	return (avail);
3688da69b8f9SSean Bruno }
3689da69b8f9SSean Bruno 
3690da69b8f9SSean Bruno static void
3691da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq)
3692da69b8f9SSean Bruno {
3693da69b8f9SSean Bruno 	struct ifmp_ring *r;
3694da69b8f9SSean Bruno 
369595246abbSSean Bruno 	r = txq->ift_br;
3696da69b8f9SSean Bruno 	r->drain = iflib_txq_drain_free;
3697da69b8f9SSean Bruno 	r->can_drain = iflib_txq_drain_always;
3698da69b8f9SSean Bruno 
3699da69b8f9SSean Bruno 	ifmp_ring_check_drainage(r, r->size);
3700da69b8f9SSean Bruno 
3701da69b8f9SSean Bruno 	r->drain = iflib_txq_drain;
3702da69b8f9SSean Bruno 	r->can_drain = iflib_txq_can_drain;
3703da69b8f9SSean Bruno }
3704da69b8f9SSean Bruno 
37054c7070dbSScott Long static void
370623ac9029SStephen Hurd _task_fn_tx(void *context)
37074c7070dbSScott Long {
37084c7070dbSScott Long 	iflib_txq_t txq = context;
37094c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
371095246abbSSean Bruno 	struct ifnet *ifp = ctx->ifc_ifp;
37114c7070dbSScott Long 
37121248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
37131248952aSSean Bruno 	txq->ift_cpu_exec_count[curcpu]++;
37141248952aSSean Bruno #endif
37154c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
37164c7070dbSScott Long 		return;
3717d0d0ad0aSStephen Hurd 	if (if_getcapenable(ifp) & IFCAP_NETMAP) {
3718*5ee36c68SStephen Hurd 		/*
3719*5ee36c68SStephen Hurd 		 * If there are no available credits, and TX IRQs are not in use,
3720*5ee36c68SStephen Hurd 		 * re-schedule the task immediately.
3721*5ee36c68SStephen Hurd 		 */
372295246abbSSean Bruno 		if (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false))
372395246abbSSean Bruno 			netmap_tx_irq(ifp, txq->ift_id);
3724*5ee36c68SStephen Hurd 		else {
3725*5ee36c68SStephen Hurd 			if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) {
3726*5ee36c68SStephen Hurd 				struct netmap_kring *kring = NA(ctx->ifc_ifp)->tx_rings[txq->ift_id];
3727*5ee36c68SStephen Hurd 
3728*5ee36c68SStephen Hurd 				if (kring->nr_hwtail != nm_prev(kring->rhead, kring->nkr_num_slots - 1))
3729*5ee36c68SStephen Hurd 					GROUPTASK_ENQUEUE(&txq->ift_task);
3730*5ee36c68SStephen Hurd 			}
3731*5ee36c68SStephen Hurd 		}
373295246abbSSean Bruno 		IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
373395246abbSSean Bruno 		return;
373495246abbSSean Bruno 	}
373595246abbSSean Bruno 	if (txq->ift_db_pending)
373695246abbSSean Bruno 		ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE);
373795246abbSSean Bruno 	ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
373895246abbSSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
373995246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
374095246abbSSean Bruno 	else {
37411ae4848cSMatt Macy #ifdef INVARIANTS
37421ae4848cSMatt Macy 		int rc =
37431ae4848cSMatt Macy #endif
37441ae4848cSMatt Macy 			IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
374595246abbSSean Bruno 			KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
374695246abbSSean Bruno 	}
37474c7070dbSScott Long }
37484c7070dbSScott Long 
37494c7070dbSScott Long static void
375023ac9029SStephen Hurd _task_fn_rx(void *context)
37514c7070dbSScott Long {
37524c7070dbSScott Long 	iflib_rxq_t rxq = context;
37534c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
37544c7070dbSScott Long 	bool more;
3755f4d2154eSStephen Hurd 	uint16_t budget;
37564c7070dbSScott Long 
37571248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
37581248952aSSean Bruno 	rxq->ifr_cpu_exec_count[curcpu]++;
37591248952aSSean Bruno #endif
37604c7070dbSScott Long 	DBG_COUNTER_INC(task_fn_rxs);
37614c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
37624c7070dbSScott Long 		return;
3763d0d0ad0aSStephen Hurd 	more = true;
3764d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP
3765d0d0ad0aSStephen Hurd 	if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) {
3766d0d0ad0aSStephen Hurd 		u_int work = 0;
3767d0d0ad0aSStephen Hurd 		if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work)) {
3768d0d0ad0aSStephen Hurd 			more = false;
3769d0d0ad0aSStephen Hurd 		}
3770d0d0ad0aSStephen Hurd 	}
3771d0d0ad0aSStephen Hurd #endif
3772f4d2154eSStephen Hurd 	budget = ctx->ifc_sysctl_rx_budget;
3773f4d2154eSStephen Hurd 	if (budget == 0)
3774f4d2154eSStephen Hurd 		budget = 16;	/* XXX */
3775f4d2154eSStephen Hurd 	if (more == false || (more = iflib_rxeof(rxq, budget)) == false) {
37764c7070dbSScott Long 		if (ctx->ifc_flags & IFC_LEGACY)
37774c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
37784c7070dbSScott Long 		else {
37791ae4848cSMatt Macy #ifdef INVARIANTS
37801ae4848cSMatt Macy 			int rc =
37811ae4848cSMatt Macy #endif
37821ae4848cSMatt Macy 				IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
378323ac9029SStephen Hurd 			KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
37841ae4848cSMatt Macy 			DBG_COUNTER_INC(rx_intr_enables);
37854c7070dbSScott Long 		}
37864c7070dbSScott Long 	}
37874c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
37884c7070dbSScott Long 		return;
37894c7070dbSScott Long 	if (more)
37904c7070dbSScott Long 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
37914c7070dbSScott Long }
37924c7070dbSScott Long 
37934c7070dbSScott Long static void
379423ac9029SStephen Hurd _task_fn_admin(void *context)
37954c7070dbSScott Long {
37964c7070dbSScott Long 	if_ctx_t ctx = context;
37974c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
37984c7070dbSScott Long 	iflib_txq_t txq;
3799ab2e3f79SStephen Hurd 	int i;
38007b610b60SSean Bruno 	bool oactive, running, do_reset, do_watchdog;
3801ab2e3f79SStephen Hurd 
38027b610b60SSean Bruno 	STATE_LOCK(ctx);
38037b610b60SSean Bruno 	running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING);
38047b610b60SSean Bruno 	oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE);
38057b610b60SSean Bruno 	do_reset = (ctx->ifc_flags & IFC_DO_RESET);
38067b610b60SSean Bruno 	do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG);
38077b610b60SSean Bruno 	ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG);
38087b610b60SSean Bruno 	STATE_UNLOCK(ctx);
38097b610b60SSean Bruno 
38107b610b60SSean Bruno 	if (!running & !oactive)
3811ab2e3f79SStephen Hurd 		return;
38124c7070dbSScott Long 
38134c7070dbSScott Long 	CTX_LOCK(ctx);
38144c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
38154c7070dbSScott Long 		CALLOUT_LOCK(txq);
38164c7070dbSScott Long 		callout_stop(&txq->ift_timer);
38174c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
38184c7070dbSScott Long 	}
38197b610b60SSean Bruno 	if (do_watchdog) {
38207b610b60SSean Bruno 		ctx->ifc_watchdog_events++;
38217b610b60SSean Bruno 		IFDI_WATCHDOG_RESET(ctx);
38227b610b60SSean Bruno 	}
3823d300df01SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
3824ab2e3f79SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
3825ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
3826ab2e3f79SStephen Hurd 	IFDI_LINK_INTR_ENABLE(ctx);
38277b610b60SSean Bruno 	if (do_reset)
3828ab2e3f79SStephen Hurd 		iflib_if_init_locked(ctx);
38294c7070dbSScott Long 	CTX_UNLOCK(ctx);
38304c7070dbSScott Long 
3831ab2e3f79SStephen Hurd 	if (LINK_ACTIVE(ctx) == 0)
38324c7070dbSScott Long 		return;
38334c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
38344c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
38354c7070dbSScott Long }
38364c7070dbSScott Long 
38374c7070dbSScott Long 
38384c7070dbSScott Long static void
383923ac9029SStephen Hurd _task_fn_iov(void *context)
38404c7070dbSScott Long {
38414c7070dbSScott Long 	if_ctx_t ctx = context;
38424c7070dbSScott Long 
38434c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
38444c7070dbSScott Long 		return;
38454c7070dbSScott Long 
38464c7070dbSScott Long 	CTX_LOCK(ctx);
38474c7070dbSScott Long 	IFDI_VFLR_HANDLE(ctx);
38484c7070dbSScott Long 	CTX_UNLOCK(ctx);
38494c7070dbSScott Long }
38504c7070dbSScott Long 
38514c7070dbSScott Long static int
38524c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
38534c7070dbSScott Long {
38544c7070dbSScott Long 	int err;
38554c7070dbSScott Long 	if_int_delay_info_t info;
38564c7070dbSScott Long 	if_ctx_t ctx;
38574c7070dbSScott Long 
38584c7070dbSScott Long 	info = (if_int_delay_info_t)arg1;
38594c7070dbSScott Long 	ctx = info->iidi_ctx;
38604c7070dbSScott Long 	info->iidi_req = req;
38614c7070dbSScott Long 	info->iidi_oidp = oidp;
38624c7070dbSScott Long 	CTX_LOCK(ctx);
38634c7070dbSScott Long 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
38644c7070dbSScott Long 	CTX_UNLOCK(ctx);
38654c7070dbSScott Long 	return (err);
38664c7070dbSScott Long }
38674c7070dbSScott Long 
38684c7070dbSScott Long /*********************************************************************
38694c7070dbSScott Long  *
38704c7070dbSScott Long  *  IFNET FUNCTIONS
38714c7070dbSScott Long  *
38724c7070dbSScott Long  **********************************************************************/
38734c7070dbSScott Long 
38744c7070dbSScott Long static void
38754c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx)
38764c7070dbSScott Long {
38774c7070dbSScott Long 	iflib_stop(ctx);
38784c7070dbSScott Long 	iflib_init_locked(ctx);
38794c7070dbSScott Long }
38804c7070dbSScott Long 
38814c7070dbSScott Long 
38824c7070dbSScott Long static void
38834c7070dbSScott Long iflib_if_init(void *arg)
38844c7070dbSScott Long {
38854c7070dbSScott Long 	if_ctx_t ctx = arg;
38864c7070dbSScott Long 
38874c7070dbSScott Long 	CTX_LOCK(ctx);
38884c7070dbSScott Long 	iflib_if_init_locked(ctx);
38894c7070dbSScott Long 	CTX_UNLOCK(ctx);
38904c7070dbSScott Long }
38914c7070dbSScott Long 
38924c7070dbSScott Long static int
38934c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m)
38944c7070dbSScott Long {
38954c7070dbSScott Long 	if_ctx_t	ctx = if_getsoftc(ifp);
38964c7070dbSScott Long 
38974c7070dbSScott Long 	iflib_txq_t txq;
389823ac9029SStephen Hurd 	int err, qidx;
38994c7070dbSScott Long 
39004c7070dbSScott Long 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
39014c7070dbSScott Long 		DBG_COUNTER_INC(tx_frees);
39024c7070dbSScott Long 		m_freem(m);
3903da69b8f9SSean Bruno 		return (ENOBUFS);
39044c7070dbSScott Long 	}
39054c7070dbSScott Long 
390623ac9029SStephen Hurd 	MPASS(m->m_nextpkt == NULL);
39074c7070dbSScott Long 	qidx = 0;
39084c7070dbSScott Long 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
39094c7070dbSScott Long 		qidx = QIDX(ctx, m);
39104c7070dbSScott Long 	/*
39114c7070dbSScott Long 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
39124c7070dbSScott Long 	 */
39134c7070dbSScott Long 	txq = &ctx->ifc_txqs[qidx];
39144c7070dbSScott Long 
39154c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
39164c7070dbSScott Long 	if (txq->ift_closed) {
39174c7070dbSScott Long 		while (m != NULL) {
39184c7070dbSScott Long 			next = m->m_nextpkt;
39194c7070dbSScott Long 			m->m_nextpkt = NULL;
39204c7070dbSScott Long 			m_freem(m);
39214c7070dbSScott Long 			m = next;
39224c7070dbSScott Long 		}
39234c7070dbSScott Long 		return (ENOBUFS);
39244c7070dbSScott Long 	}
39254c7070dbSScott Long #endif
392623ac9029SStephen Hurd #ifdef notyet
39274c7070dbSScott Long 	qidx = count = 0;
39284c7070dbSScott Long 	mp = marr;
39294c7070dbSScott Long 	next = m;
39304c7070dbSScott Long 	do {
39314c7070dbSScott Long 		count++;
39324c7070dbSScott Long 		next = next->m_nextpkt;
39334c7070dbSScott Long 	} while (next != NULL);
39344c7070dbSScott Long 
393516fb86abSConrad Meyer 	if (count > nitems(marr))
39364c7070dbSScott Long 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
39374c7070dbSScott Long 			/* XXX check nextpkt */
39384c7070dbSScott Long 			m_freem(m);
39394c7070dbSScott Long 			/* XXX simplify for now */
39404c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
39414c7070dbSScott Long 			return (ENOBUFS);
39424c7070dbSScott Long 		}
39434c7070dbSScott Long 	for (next = m, i = 0; next != NULL; i++) {
39444c7070dbSScott Long 		mp[i] = next;
39454c7070dbSScott Long 		next = next->m_nextpkt;
39464c7070dbSScott Long 		mp[i]->m_nextpkt = NULL;
39474c7070dbSScott Long 	}
394823ac9029SStephen Hurd #endif
39494c7070dbSScott Long 	DBG_COUNTER_INC(tx_seen);
395095246abbSSean Bruno 	err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE);
39514c7070dbSScott Long 
3952ab2e3f79SStephen Hurd 	GROUPTASK_ENQUEUE(&txq->ift_task);
39531225d9daSStephen Hurd 	if (err) {
39544c7070dbSScott Long 		/* support forthcoming later */
39554c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
39564c7070dbSScott Long 		txq->ift_closed = TRUE;
39574c7070dbSScott Long #endif
395895246abbSSean Bruno 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
395923ac9029SStephen Hurd 		m_freem(m);
39604c7070dbSScott Long 	}
39614c7070dbSScott Long 
39624c7070dbSScott Long 	return (err);
39634c7070dbSScott Long }
39644c7070dbSScott Long 
39654c7070dbSScott Long static void
39664c7070dbSScott Long iflib_if_qflush(if_t ifp)
39674c7070dbSScott Long {
39684c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
39694c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
39704c7070dbSScott Long 	int i;
39714c7070dbSScott Long 
39727b610b60SSean Bruno 	STATE_LOCK(ctx);
39734c7070dbSScott Long 	ctx->ifc_flags |= IFC_QFLUSH;
39747b610b60SSean Bruno 	STATE_UNLOCK(ctx);
39754c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
397695246abbSSean Bruno 		while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
39774c7070dbSScott Long 			iflib_txq_check_drain(txq, 0);
39787b610b60SSean Bruno 	STATE_LOCK(ctx);
39794c7070dbSScott Long 	ctx->ifc_flags &= ~IFC_QFLUSH;
39807b610b60SSean Bruno 	STATE_UNLOCK(ctx);
39814c7070dbSScott Long 
39824c7070dbSScott Long 	if_qflush(ifp);
39834c7070dbSScott Long }
39844c7070dbSScott Long 
39854c7070dbSScott Long 
398623ac9029SStephen Hurd #define IFCAP_FLAGS (IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
398718a660b3SSean Bruno 		     IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
39884c7070dbSScott Long 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO)
39894c7070dbSScott Long 
39904c7070dbSScott Long static int
39914c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
39924c7070dbSScott Long {
39934c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
39944c7070dbSScott Long 	struct ifreq	*ifr = (struct ifreq *)data;
39954c7070dbSScott Long #if defined(INET) || defined(INET6)
39964c7070dbSScott Long 	struct ifaddr	*ifa = (struct ifaddr *)data;
39974c7070dbSScott Long #endif
39984c7070dbSScott Long 	bool		avoid_reset = FALSE;
39994c7070dbSScott Long 	int		err = 0, reinit = 0, bits;
40004c7070dbSScott Long 
40014c7070dbSScott Long 	switch (command) {
40024c7070dbSScott Long 	case SIOCSIFADDR:
40034c7070dbSScott Long #ifdef INET
40044c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET)
40054c7070dbSScott Long 			avoid_reset = TRUE;
40064c7070dbSScott Long #endif
40074c7070dbSScott Long #ifdef INET6
40084c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET6)
40094c7070dbSScott Long 			avoid_reset = TRUE;
40104c7070dbSScott Long #endif
40114c7070dbSScott Long 		/*
40124c7070dbSScott Long 		** Calling init results in link renegotiation,
40134c7070dbSScott Long 		** so we avoid doing it when possible.
40144c7070dbSScott Long 		*/
40154c7070dbSScott Long 		if (avoid_reset) {
40164c7070dbSScott Long 			if_setflagbits(ifp, IFF_UP,0);
40174c7070dbSScott Long 			if (!(if_getdrvflags(ifp)& IFF_DRV_RUNNING))
40184c7070dbSScott Long 				reinit = 1;
40194c7070dbSScott Long #ifdef INET
40204c7070dbSScott Long 			if (!(if_getflags(ifp) & IFF_NOARP))
40214c7070dbSScott Long 				arp_ifinit(ifp, ifa);
40224c7070dbSScott Long #endif
40234c7070dbSScott Long 		} else
40244c7070dbSScott Long 			err = ether_ioctl(ifp, command, data);
40254c7070dbSScott Long 		break;
40264c7070dbSScott Long 	case SIOCSIFMTU:
40274c7070dbSScott Long 		CTX_LOCK(ctx);
40284c7070dbSScott Long 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
40294c7070dbSScott Long 			CTX_UNLOCK(ctx);
40304c7070dbSScott Long 			break;
40314c7070dbSScott Long 		}
40324c7070dbSScott Long 		bits = if_getdrvflags(ifp);
40334c7070dbSScott Long 		/* stop the driver and free any clusters before proceeding */
40344c7070dbSScott Long 		iflib_stop(ctx);
40354c7070dbSScott Long 
40364c7070dbSScott Long 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
40377b610b60SSean Bruno 			STATE_LOCK(ctx);
40384c7070dbSScott Long 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
40394c7070dbSScott Long 				ctx->ifc_flags |= IFC_MULTISEG;
40404c7070dbSScott Long 			else
40414c7070dbSScott Long 				ctx->ifc_flags &= ~IFC_MULTISEG;
40427b610b60SSean Bruno 			STATE_UNLOCK(ctx);
40434c7070dbSScott Long 			err = if_setmtu(ifp, ifr->ifr_mtu);
40444c7070dbSScott Long 		}
40454c7070dbSScott Long 		iflib_init_locked(ctx);
40467b610b60SSean Bruno 		STATE_LOCK(ctx);
40474c7070dbSScott Long 		if_setdrvflags(ifp, bits);
40487b610b60SSean Bruno 		STATE_UNLOCK(ctx);
40494c7070dbSScott Long 		CTX_UNLOCK(ctx);
40504c7070dbSScott Long 		break;
40514c7070dbSScott Long 	case SIOCSIFFLAGS:
4052ab2e3f79SStephen Hurd 		CTX_LOCK(ctx);
4053ab2e3f79SStephen Hurd 		if (if_getflags(ifp) & IFF_UP) {
4054ab2e3f79SStephen Hurd 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4055ab2e3f79SStephen Hurd 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
4056ab2e3f79SStephen Hurd 				    (IFF_PROMISC | IFF_ALLMULTI)) {
4057ab2e3f79SStephen Hurd 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
4058ab2e3f79SStephen Hurd 				}
4059ab2e3f79SStephen Hurd 			} else
4060ab2e3f79SStephen Hurd 				reinit = 1;
4061ab2e3f79SStephen Hurd 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4062ab2e3f79SStephen Hurd 			iflib_stop(ctx);
4063ab2e3f79SStephen Hurd 		}
4064ab2e3f79SStephen Hurd 		ctx->ifc_if_flags = if_getflags(ifp);
4065ab2e3f79SStephen Hurd 		CTX_UNLOCK(ctx);
40664c7070dbSScott Long 		break;
40674c7070dbSScott Long 	case SIOCADDMULTI:
40684c7070dbSScott Long 	case SIOCDELMULTI:
40694c7070dbSScott Long 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4070ab2e3f79SStephen Hurd 			CTX_LOCK(ctx);
4071ab2e3f79SStephen Hurd 			IFDI_INTR_DISABLE(ctx);
4072ab2e3f79SStephen Hurd 			IFDI_MULTI_SET(ctx);
4073ab2e3f79SStephen Hurd 			IFDI_INTR_ENABLE(ctx);
4074ab2e3f79SStephen Hurd 			CTX_UNLOCK(ctx);
40754c7070dbSScott Long 		}
40764c7070dbSScott Long 		break;
40774c7070dbSScott Long 	case SIOCSIFMEDIA:
40784c7070dbSScott Long 		CTX_LOCK(ctx);
40794c7070dbSScott Long 		IFDI_MEDIA_SET(ctx);
40804c7070dbSScott Long 		CTX_UNLOCK(ctx);
40814c7070dbSScott Long 		/* falls thru */
40824c7070dbSScott Long 	case SIOCGIFMEDIA:
4083a027c8e9SStephen Hurd 	case SIOCGIFXMEDIA:
40844c7070dbSScott Long 		err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command);
40854c7070dbSScott Long 		break;
40864c7070dbSScott Long 	case SIOCGI2C:
40874c7070dbSScott Long 	{
40884c7070dbSScott Long 		struct ifi2creq i2c;
40894c7070dbSScott Long 
4090541d96aaSBrooks Davis 		err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
40914c7070dbSScott Long 		if (err != 0)
40924c7070dbSScott Long 			break;
40934c7070dbSScott Long 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
40944c7070dbSScott Long 			err = EINVAL;
40954c7070dbSScott Long 			break;
40964c7070dbSScott Long 		}
40974c7070dbSScott Long 		if (i2c.len > sizeof(i2c.data)) {
40984c7070dbSScott Long 			err = EINVAL;
40994c7070dbSScott Long 			break;
41004c7070dbSScott Long 		}
41014c7070dbSScott Long 
41024c7070dbSScott Long 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
4103541d96aaSBrooks Davis 			err = copyout(&i2c, ifr_data_get_ptr(ifr),
4104541d96aaSBrooks Davis 			    sizeof(i2c));
41054c7070dbSScott Long 		break;
41064c7070dbSScott Long 	}
41074c7070dbSScott Long 	case SIOCSIFCAP:
41084c7070dbSScott Long 	{
41094c7070dbSScott Long 		int mask, setmask;
41104c7070dbSScott Long 
41114c7070dbSScott Long 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
41124c7070dbSScott Long 		setmask = 0;
41134c7070dbSScott Long #ifdef TCP_OFFLOAD
41144c7070dbSScott Long 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
41154c7070dbSScott Long #endif
41164c7070dbSScott Long 		setmask |= (mask & IFCAP_FLAGS);
41174c7070dbSScott Long 
41188b2a1db9SSean Bruno 		if (setmask  & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
41198b2a1db9SSean Bruno 			setmask |= (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
41204c7070dbSScott Long 		if ((mask & IFCAP_WOL) &&
41214c7070dbSScott Long 		    (if_getcapabilities(ifp) & IFCAP_WOL) != 0)
41224c7070dbSScott Long 			setmask |= (mask & (IFCAP_WOL_MCAST|IFCAP_WOL_MAGIC));
41234c7070dbSScott Long 		if_vlancap(ifp);
41244c7070dbSScott Long 		/*
41254c7070dbSScott Long 		 * want to ensure that traffic has stopped before we change any of the flags
41264c7070dbSScott Long 		 */
41274c7070dbSScott Long 		if (setmask) {
41284c7070dbSScott Long 			CTX_LOCK(ctx);
41294c7070dbSScott Long 			bits = if_getdrvflags(ifp);
41308b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
41314c7070dbSScott Long 				iflib_stop(ctx);
41327b610b60SSean Bruno 			STATE_LOCK(ctx);
41334c7070dbSScott Long 			if_togglecapenable(ifp, setmask);
41347b610b60SSean Bruno 			STATE_UNLOCK(ctx);
41358b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
41364c7070dbSScott Long 				iflib_init_locked(ctx);
41377b610b60SSean Bruno 			STATE_LOCK(ctx);
41384c7070dbSScott Long 			if_setdrvflags(ifp, bits);
41397b610b60SSean Bruno 			STATE_UNLOCK(ctx);
41404c7070dbSScott Long 			CTX_UNLOCK(ctx);
41414c7070dbSScott Long 		}
41424c7070dbSScott Long 		break;
41434c7070dbSScott Long 	    }
41444c7070dbSScott Long 	case SIOCGPRIVATE_0:
41454c7070dbSScott Long 	case SIOCSDRVSPEC:
41464c7070dbSScott Long 	case SIOCGDRVSPEC:
41474c7070dbSScott Long 		CTX_LOCK(ctx);
41484c7070dbSScott Long 		err = IFDI_PRIV_IOCTL(ctx, command, data);
41494c7070dbSScott Long 		CTX_UNLOCK(ctx);
41504c7070dbSScott Long 		break;
41514c7070dbSScott Long 	default:
41524c7070dbSScott Long 		err = ether_ioctl(ifp, command, data);
41534c7070dbSScott Long 		break;
41544c7070dbSScott Long 	}
41554c7070dbSScott Long 	if (reinit)
41564c7070dbSScott Long 		iflib_if_init(ctx);
41574c7070dbSScott Long 	return (err);
41584c7070dbSScott Long }
41594c7070dbSScott Long 
41604c7070dbSScott Long static uint64_t
41614c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt)
41624c7070dbSScott Long {
41634c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
41644c7070dbSScott Long 
41654c7070dbSScott Long 	return (IFDI_GET_COUNTER(ctx, cnt));
41664c7070dbSScott Long }
41674c7070dbSScott Long 
41684c7070dbSScott Long /*********************************************************************
41694c7070dbSScott Long  *
41704c7070dbSScott Long  *  OTHER FUNCTIONS EXPORTED TO THE STACK
41714c7070dbSScott Long  *
41724c7070dbSScott Long  **********************************************************************/
41734c7070dbSScott Long 
41744c7070dbSScott Long static void
41754c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
41764c7070dbSScott Long {
41774c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
41784c7070dbSScott Long 
41794c7070dbSScott Long 	if ((void *)ctx != arg)
41804c7070dbSScott Long 		return;
41814c7070dbSScott Long 
41824c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
41834c7070dbSScott Long 		return;
41844c7070dbSScott Long 
41854c7070dbSScott Long 	CTX_LOCK(ctx);
41864c7070dbSScott Long 	IFDI_VLAN_REGISTER(ctx, vtag);
41874c7070dbSScott Long 	/* Re-init to load the changes */
41884c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
418921e10b16SSean Bruno 		iflib_if_init_locked(ctx);
41904c7070dbSScott Long 	CTX_UNLOCK(ctx);
41914c7070dbSScott Long }
41924c7070dbSScott Long 
41934c7070dbSScott Long static void
41944c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
41954c7070dbSScott Long {
41964c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
41974c7070dbSScott Long 
41984c7070dbSScott Long 	if ((void *)ctx != arg)
41994c7070dbSScott Long 		return;
42004c7070dbSScott Long 
42014c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
42024c7070dbSScott Long 		return;
42034c7070dbSScott Long 
42044c7070dbSScott Long 	CTX_LOCK(ctx);
42054c7070dbSScott Long 	IFDI_VLAN_UNREGISTER(ctx, vtag);
42064c7070dbSScott Long 	/* Re-init to load the changes */
42074c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
420821e10b16SSean Bruno 		iflib_if_init_locked(ctx);
42094c7070dbSScott Long 	CTX_UNLOCK(ctx);
42104c7070dbSScott Long }
42114c7070dbSScott Long 
42124c7070dbSScott Long static void
42134c7070dbSScott Long iflib_led_func(void *arg, int onoff)
42144c7070dbSScott Long {
42154c7070dbSScott Long 	if_ctx_t ctx = arg;
42164c7070dbSScott Long 
42174c7070dbSScott Long 	CTX_LOCK(ctx);
42184c7070dbSScott Long 	IFDI_LED_FUNC(ctx, onoff);
42194c7070dbSScott Long 	CTX_UNLOCK(ctx);
42204c7070dbSScott Long }
42214c7070dbSScott Long 
42224c7070dbSScott Long /*********************************************************************
42234c7070dbSScott Long  *
42244c7070dbSScott Long  *  BUS FUNCTION DEFINITIONS
42254c7070dbSScott Long  *
42264c7070dbSScott Long  **********************************************************************/
42274c7070dbSScott Long 
42284c7070dbSScott Long int
42294c7070dbSScott Long iflib_device_probe(device_t dev)
42304c7070dbSScott Long {
42314c7070dbSScott Long 	pci_vendor_info_t *ent;
42324c7070dbSScott Long 
42334c7070dbSScott Long 	uint16_t	pci_vendor_id, pci_device_id;
42344c7070dbSScott Long 	uint16_t	pci_subvendor_id, pci_subdevice_id;
42354c7070dbSScott Long 	uint16_t	pci_rev_id;
42364c7070dbSScott Long 	if_shared_ctx_t sctx;
42374c7070dbSScott Long 
42384c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
42394c7070dbSScott Long 		return (ENOTSUP);
42404c7070dbSScott Long 
42414c7070dbSScott Long 	pci_vendor_id = pci_get_vendor(dev);
42424c7070dbSScott Long 	pci_device_id = pci_get_device(dev);
42434c7070dbSScott Long 	pci_subvendor_id = pci_get_subvendor(dev);
42444c7070dbSScott Long 	pci_subdevice_id = pci_get_subdevice(dev);
42454c7070dbSScott Long 	pci_rev_id = pci_get_revid(dev);
42464c7070dbSScott Long 	if (sctx->isc_parse_devinfo != NULL)
42474c7070dbSScott Long 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
42484c7070dbSScott Long 
42494c7070dbSScott Long 	ent = sctx->isc_vendor_info;
42504c7070dbSScott Long 	while (ent->pvi_vendor_id != 0) {
42514c7070dbSScott Long 		if (pci_vendor_id != ent->pvi_vendor_id) {
42524c7070dbSScott Long 			ent++;
42534c7070dbSScott Long 			continue;
42544c7070dbSScott Long 		}
42554c7070dbSScott Long 		if ((pci_device_id == ent->pvi_device_id) &&
42564c7070dbSScott Long 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
42574c7070dbSScott Long 		     (ent->pvi_subvendor_id == 0)) &&
42584c7070dbSScott Long 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
42594c7070dbSScott Long 		     (ent->pvi_subdevice_id == 0)) &&
42604c7070dbSScott Long 		    ((pci_rev_id == ent->pvi_rev_id) ||
42614c7070dbSScott Long 		     (ent->pvi_rev_id == 0))) {
42624c7070dbSScott Long 
42634c7070dbSScott Long 			device_set_desc_copy(dev, ent->pvi_name);
42644c7070dbSScott Long 			/* this needs to be changed to zero if the bus probing code
42654c7070dbSScott Long 			 * ever stops re-probing on best match because the sctx
42664c7070dbSScott Long 			 * may have its values over written by register calls
42674c7070dbSScott Long 			 * in subsequent probes
42684c7070dbSScott Long 			 */
42694c7070dbSScott Long 			return (BUS_PROBE_DEFAULT);
42704c7070dbSScott Long 		}
42714c7070dbSScott Long 		ent++;
42724c7070dbSScott Long 	}
42734c7070dbSScott Long 	return (ENXIO);
42744c7070dbSScott Long }
42754c7070dbSScott Long 
427609f6ff4fSMatt Macy static void
427709f6ff4fSMatt Macy iflib_reset_qvalues(if_ctx_t ctx)
42784c7070dbSScott Long {
427909f6ff4fSMatt Macy 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
428009f6ff4fSMatt Macy 	if_shared_ctx_t sctx = ctx->ifc_sctx;
428109f6ff4fSMatt Macy 	device_t dev = ctx->ifc_dev;
428209f6ff4fSMatt Macy 	int i, main_txq, main_rxq;
42834c7070dbSScott Long 
428409f6ff4fSMatt Macy 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
428509f6ff4fSMatt Macy 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
42864c7070dbSScott Long 
428709f6ff4fSMatt Macy 	scctx->isc_txrx_budget_bytes_max = IFLIB_MAX_TX_BYTES;
428809f6ff4fSMatt Macy 	scctx->isc_tx_qdepth = IFLIB_DEFAULT_TX_QDEPTH;
428923ac9029SStephen Hurd 	/*
429023ac9029SStephen Hurd 	 * XXX sanity check that ntxd & nrxd are a power of 2
429123ac9029SStephen Hurd 	 */
429223ac9029SStephen Hurd 	if (ctx->ifc_sysctl_ntxqs != 0)
429323ac9029SStephen Hurd 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
429423ac9029SStephen Hurd 	if (ctx->ifc_sysctl_nrxqs != 0)
429523ac9029SStephen Hurd 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
429623ac9029SStephen Hurd 
429723ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
429823ac9029SStephen Hurd 		if (ctx->ifc_sysctl_ntxds[i] != 0)
429923ac9029SStephen Hurd 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
430023ac9029SStephen Hurd 		else
430123ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
430223ac9029SStephen Hurd 	}
430323ac9029SStephen Hurd 
430423ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
430523ac9029SStephen Hurd 		if (ctx->ifc_sysctl_nrxds[i] != 0)
430623ac9029SStephen Hurd 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
430723ac9029SStephen Hurd 		else
430823ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
430923ac9029SStephen Hurd 	}
431023ac9029SStephen Hurd 
431123ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
431223ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
431323ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
431423ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
431523ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
431623ac9029SStephen Hurd 		}
431723ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
431823ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
431923ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
432023ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
432123ac9029SStephen Hurd 		}
432223ac9029SStephen Hurd 	}
432323ac9029SStephen Hurd 
432423ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
432523ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
432623ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
432723ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
432823ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
432923ac9029SStephen Hurd 		}
433023ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
433123ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
433223ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
433323ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
433423ac9029SStephen Hurd 		}
433523ac9029SStephen Hurd 	}
433609f6ff4fSMatt Macy }
4337ab2e3f79SStephen Hurd 
433809f6ff4fSMatt Macy int
433909f6ff4fSMatt Macy iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
434009f6ff4fSMatt Macy {
434109f6ff4fSMatt Macy 	int err, rid, msix;
434209f6ff4fSMatt Macy 	if_ctx_t ctx;
434309f6ff4fSMatt Macy 	if_t ifp;
434409f6ff4fSMatt Macy 	if_softc_ctx_t scctx;
434509f6ff4fSMatt Macy 	int i;
434609f6ff4fSMatt Macy 	uint16_t main_txq;
434709f6ff4fSMatt Macy 	uint16_t main_rxq;
434809f6ff4fSMatt Macy 
434909f6ff4fSMatt Macy 
435009f6ff4fSMatt Macy 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
435109f6ff4fSMatt Macy 
435209f6ff4fSMatt Macy 	if (sc == NULL) {
435309f6ff4fSMatt Macy 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
435409f6ff4fSMatt Macy 		device_set_softc(dev, ctx);
435509f6ff4fSMatt Macy 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
435609f6ff4fSMatt Macy 	}
435709f6ff4fSMatt Macy 
435809f6ff4fSMatt Macy 	ctx->ifc_sctx = sctx;
435909f6ff4fSMatt Macy 	ctx->ifc_dev = dev;
436009f6ff4fSMatt Macy 	ctx->ifc_softc = sc;
436109f6ff4fSMatt Macy 
436209f6ff4fSMatt Macy 	if ((err = iflib_register(ctx)) != 0) {
436309f6ff4fSMatt Macy 		if (ctx->ifc_flags & IFC_SC_ALLOCATED)
436409f6ff4fSMatt Macy 			free(sc, M_IFLIB);
436509f6ff4fSMatt Macy 		free(ctx, M_IFLIB);
436609f6ff4fSMatt Macy 		device_printf(dev, "iflib_register failed %d\n", err);
436709f6ff4fSMatt Macy 		return (err);
436809f6ff4fSMatt Macy 	}
436909f6ff4fSMatt Macy 	iflib_add_device_sysctl_pre(ctx);
437009f6ff4fSMatt Macy 
437109f6ff4fSMatt Macy 	scctx = &ctx->ifc_softc_ctx;
437209f6ff4fSMatt Macy 	ifp = ctx->ifc_ifp;
437309f6ff4fSMatt Macy 
437409f6ff4fSMatt Macy 	iflib_reset_qvalues(ctx);
4375aa8a24d3SStephen Hurd 	CTX_LOCK(ctx);
4376ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
4377aa8a24d3SStephen Hurd 		CTX_UNLOCK(ctx);
43784c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
43794c7070dbSScott Long 		return (err);
43804c7070dbSScott Long 	}
43811248952aSSean Bruno 	_iflib_pre_assert(scctx);
43821248952aSSean Bruno 	ctx->ifc_txrx = *scctx->isc_txrx;
43831248952aSSean Bruno 
43841248952aSSean Bruno #ifdef INVARIANTS
43851248952aSSean Bruno 	MPASS(scctx->isc_capenable);
43861248952aSSean Bruno 	if (scctx->isc_capenable & IFCAP_TXCSUM)
43871248952aSSean Bruno 		MPASS(scctx->isc_tx_csum_flags);
43881248952aSSean Bruno #endif
43891248952aSSean Bruno 
439018a660b3SSean Bruno 	if_setcapabilities(ifp, scctx->isc_capenable | IFCAP_HWSTATS);
439118a660b3SSean Bruno 	if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS);
43921248952aSSean Bruno 
43931248952aSSean Bruno 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
43941248952aSSean Bruno 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
43951248952aSSean Bruno 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
43961248952aSSean Bruno 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
439723ac9029SStephen Hurd 
43984c7070dbSScott Long #ifdef ACPI_DMAR
43994c7070dbSScott Long 	if (dmar_get_dma_tag(device_get_parent(dev), dev) != NULL)
44004c7070dbSScott Long 		ctx->ifc_flags |= IFC_DMAR;
440195246abbSSean Bruno #elif !(defined(__i386__) || defined(__amd64__))
440295246abbSSean Bruno 	/* set unconditionally for !x86 */
440395246abbSSean Bruno 	ctx->ifc_flags |= IFC_DMAR;
44044c7070dbSScott Long #endif
44054c7070dbSScott Long 
440695246abbSSean Bruno 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
440795246abbSSean Bruno 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
440823ac9029SStephen Hurd 
440923ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
441023ac9029SStephen Hurd 	device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
441123ac9029SStephen Hurd 		      scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
441223ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
441323ac9029SStephen Hurd 		if (!powerof2(scctx->isc_nrxd[i])) {
441423ac9029SStephen Hurd 			/* round down instead? */
441523ac9029SStephen Hurd 			device_printf(dev, "# rx descriptors must be a power of 2\n");
441623ac9029SStephen Hurd 			err = EINVAL;
441723ac9029SStephen Hurd 			goto fail;
441823ac9029SStephen Hurd 		}
441923ac9029SStephen Hurd 	}
442023ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
442123ac9029SStephen Hurd 		if (!powerof2(scctx->isc_ntxd[i])) {
442223ac9029SStephen Hurd 			device_printf(dev,
442323ac9029SStephen Hurd 			    "# tx descriptors must be a power of 2");
442423ac9029SStephen Hurd 			err = EINVAL;
442523ac9029SStephen Hurd 			goto fail;
442623ac9029SStephen Hurd 		}
442723ac9029SStephen Hurd 	}
442823ac9029SStephen Hurd 
442923ac9029SStephen Hurd 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
443023ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
443123ac9029SStephen Hurd 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
443223ac9029SStephen Hurd 		    MAX_SINGLE_PACKET_FRACTION);
443323ac9029SStephen Hurd 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
443423ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
443523ac9029SStephen Hurd 		scctx->isc_tx_tso_segments_max = max(1,
443623ac9029SStephen Hurd 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
44374c7070dbSScott Long 
44384c7070dbSScott Long 	/*
44394c7070dbSScott Long 	 * Protect the stack against modern hardware
44404c7070dbSScott Long 	 */
44414c7070dbSScott Long 	if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
44424c7070dbSScott Long 		scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
44434c7070dbSScott Long 
44444c7070dbSScott Long 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
44454c7070dbSScott Long 	ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
44464c7070dbSScott Long 	ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
44474c7070dbSScott Long 	ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
44484c7070dbSScott Long 	if (scctx->isc_rss_table_size == 0)
44494c7070dbSScott Long 		scctx->isc_rss_table_size = 64;
445023ac9029SStephen Hurd 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
4451da69b8f9SSean Bruno 
4452da69b8f9SSean Bruno 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
4453da69b8f9SSean Bruno 	/* XXX format name */
4454ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
4455e516b535SStephen Hurd 
4456772593dbSStephen Hurd 	/* Set up cpu set.  If it fails, use the set of all CPUs. */
4457e516b535SStephen Hurd 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) {
4458e516b535SStephen Hurd 		device_printf(dev, "Unable to fetch CPU list\n");
4459e516b535SStephen Hurd 		CPU_COPY(&all_cpus, &ctx->ifc_cpus);
4460e516b535SStephen Hurd 	}
4461e516b535SStephen Hurd 	MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0);
4462e516b535SStephen Hurd 
44634c7070dbSScott Long 	/*
44644c7070dbSScott Long 	** Now setup MSI or MSI/X, should
44654c7070dbSScott Long 	** return us the number of supported
44664c7070dbSScott Long 	** vectors. (Will be 1 for MSI)
44674c7070dbSScott Long 	*/
44684c7070dbSScott Long 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
44694c7070dbSScott Long 		msix = scctx->isc_vectors;
44704c7070dbSScott Long 	} else if (scctx->isc_msix_bar != 0)
4471f7ae9a84SSean Bruno 	       /*
4472f7ae9a84SSean Bruno 		* The simple fact that isc_msix_bar is not 0 does not mean we
4473f7ae9a84SSean Bruno 		* we have a good value there that is known to work.
4474f7ae9a84SSean Bruno 		*/
44754c7070dbSScott Long 		msix = iflib_msix_init(ctx);
44764c7070dbSScott Long 	else {
44774c7070dbSScott Long 		scctx->isc_vectors = 1;
44784c7070dbSScott Long 		scctx->isc_ntxqsets = 1;
44794c7070dbSScott Long 		scctx->isc_nrxqsets = 1;
44804c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
44814c7070dbSScott Long 		msix = 0;
44824c7070dbSScott Long 	}
44834c7070dbSScott Long 	/* Get memory for the station queues */
44844c7070dbSScott Long 	if ((err = iflib_queues_alloc(ctx))) {
44854c7070dbSScott Long 		device_printf(dev, "Unable to allocate queue memory\n");
44864c7070dbSScott Long 		goto fail;
44874c7070dbSScott Long 	}
44884c7070dbSScott Long 
4489ac88e6daSStephen Hurd 	if ((err = iflib_qset_structures_setup(ctx)))
44904c7070dbSScott Long 		goto fail_queues;
449169b7fc3eSSean Bruno 
4492bd84f700SSean Bruno 	/*
4493bd84f700SSean Bruno 	 * Group taskqueues aren't properly set up until SMP is started,
4494bd84f700SSean Bruno 	 * so we disable interrupts until we can handle them post
4495bd84f700SSean Bruno 	 * SI_SUB_SMP.
4496bd84f700SSean Bruno 	 *
4497bd84f700SSean Bruno 	 * XXX: disabling interrupts doesn't actually work, at least for
4498bd84f700SSean Bruno 	 * the non-MSI case.  When they occur before SI_SUB_SMP completes,
4499bd84f700SSean Bruno 	 * we do null handling and depend on this not causing too large an
4500bd84f700SSean Bruno 	 * interrupt storm.
4501bd84f700SSean Bruno 	 */
45021248952aSSean Bruno 	IFDI_INTR_DISABLE(ctx);
45034c7070dbSScott Long 	if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
45044c7070dbSScott Long 		device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
45054c7070dbSScott Long 		goto fail_intr_free;
45064c7070dbSScott Long 	}
45074c7070dbSScott Long 	if (msix <= 1) {
45084c7070dbSScott Long 		rid = 0;
45094c7070dbSScott Long 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
45104c7070dbSScott Long 			MPASS(msix == 1);
45114c7070dbSScott Long 			rid = 1;
45124c7070dbSScott Long 		}
451323ac9029SStephen Hurd 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
45144c7070dbSScott Long 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
45154c7070dbSScott Long 			goto fail_intr_free;
45164c7070dbSScott Long 		}
45174c7070dbSScott Long 	}
45184c7070dbSScott Long 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
4519ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
45204c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
45214c7070dbSScott Long 		goto fail_detach;
45224c7070dbSScott Long 	}
45234c7070dbSScott Long 	if ((err = iflib_netmap_attach(ctx))) {
45244c7070dbSScott Long 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
45254c7070dbSScott Long 		goto fail_detach;
45264c7070dbSScott Long 	}
45274c7070dbSScott Long 	*ctxp = ctx;
45284c7070dbSScott Long 
452994618825SMark Johnston 	NETDUMP_SET(ctx->ifc_ifp, iflib);
453094618825SMark Johnston 
453123ac9029SStephen Hurd 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
45324c7070dbSScott Long 	iflib_add_device_sysctl_post(ctx);
45334ecb427aSSean Bruno 	ctx->ifc_flags |= IFC_INIT_DONE;
4534aa8a24d3SStephen Hurd 	CTX_UNLOCK(ctx);
45354c7070dbSScott Long 	return (0);
45364c7070dbSScott Long fail_detach:
45374c7070dbSScott Long 	ether_ifdetach(ctx->ifc_ifp);
45384c7070dbSScott Long fail_intr_free:
45394c7070dbSScott Long 	if (scctx->isc_intr == IFLIB_INTR_MSIX || scctx->isc_intr == IFLIB_INTR_MSI)
45404c7070dbSScott Long 		pci_release_msi(ctx->ifc_dev);
45414c7070dbSScott Long fail_queues:
45426108c013SStephen Hurd 	iflib_tx_structures_free(ctx);
45436108c013SStephen Hurd 	iflib_rx_structures_free(ctx);
45444c7070dbSScott Long fail:
45454c7070dbSScott Long 	IFDI_DETACH(ctx);
4546aa8a24d3SStephen Hurd 	CTX_UNLOCK(ctx);
45474c7070dbSScott Long 	return (err);
45484c7070dbSScott Long }
45494c7070dbSScott Long 
45504c7070dbSScott Long int
455109f6ff4fSMatt Macy iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
455209f6ff4fSMatt Macy 					  struct iflib_cloneattach_ctx *clctx)
455309f6ff4fSMatt Macy {
455409f6ff4fSMatt Macy 	int err;
455509f6ff4fSMatt Macy 	if_ctx_t ctx;
455609f6ff4fSMatt Macy 	if_t ifp;
455709f6ff4fSMatt Macy 	if_softc_ctx_t scctx;
455809f6ff4fSMatt Macy 	int i;
455909f6ff4fSMatt Macy 	void *sc;
456009f6ff4fSMatt Macy 	uint16_t main_txq;
456109f6ff4fSMatt Macy 	uint16_t main_rxq;
456209f6ff4fSMatt Macy 
456309f6ff4fSMatt Macy 	ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO);
456409f6ff4fSMatt Macy 	sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
456509f6ff4fSMatt Macy 	ctx->ifc_flags |= IFC_SC_ALLOCATED;
456609f6ff4fSMatt Macy 	if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL))
456709f6ff4fSMatt Macy 		ctx->ifc_flags |= IFC_PSEUDO;
456809f6ff4fSMatt Macy 
456909f6ff4fSMatt Macy 	ctx->ifc_sctx = sctx;
457009f6ff4fSMatt Macy 	ctx->ifc_softc = sc;
457109f6ff4fSMatt Macy 	ctx->ifc_dev = dev;
457209f6ff4fSMatt Macy 
457309f6ff4fSMatt Macy 	if ((err = iflib_register(ctx)) != 0) {
457409f6ff4fSMatt Macy 		device_printf(dev, "%s: iflib_register failed %d\n", __func__, err);
457509f6ff4fSMatt Macy 		free(sc, M_IFLIB);
457609f6ff4fSMatt Macy 		free(ctx, M_IFLIB);
457709f6ff4fSMatt Macy 		return (err);
457809f6ff4fSMatt Macy 	}
457909f6ff4fSMatt Macy 	iflib_add_device_sysctl_pre(ctx);
458009f6ff4fSMatt Macy 
458109f6ff4fSMatt Macy 	scctx = &ctx->ifc_softc_ctx;
458209f6ff4fSMatt Macy 	ifp = ctx->ifc_ifp;
458309f6ff4fSMatt Macy 
458409f6ff4fSMatt Macy 	/*
458509f6ff4fSMatt Macy 	 * XXX sanity check that ntxd & nrxd are a power of 2
458609f6ff4fSMatt Macy 	 */
458709f6ff4fSMatt Macy 	iflib_reset_qvalues(ctx);
458809f6ff4fSMatt Macy 
458909f6ff4fSMatt Macy 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
459009f6ff4fSMatt Macy 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
459109f6ff4fSMatt Macy 		return (err);
459209f6ff4fSMatt Macy 	}
459309f6ff4fSMatt Macy 	if (sctx->isc_flags & IFLIB_GEN_MAC)
459409f6ff4fSMatt Macy 		iflib_gen_mac(ctx);
459509f6ff4fSMatt Macy 	if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name,
459609f6ff4fSMatt Macy 								clctx->cc_params)) != 0) {
459709f6ff4fSMatt Macy 		device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err);
459809f6ff4fSMatt Macy 		return (err);
459909f6ff4fSMatt Macy 	}
460009f6ff4fSMatt Macy 	ifmedia_add(&ctx->ifc_media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
460109f6ff4fSMatt Macy 	ifmedia_add(&ctx->ifc_media, IFM_ETHER | IFM_AUTO, 0, NULL);
460209f6ff4fSMatt Macy 	ifmedia_set(&ctx->ifc_media, IFM_ETHER | IFM_AUTO);
460309f6ff4fSMatt Macy 
460409f6ff4fSMatt Macy #ifdef INVARIANTS
460509f6ff4fSMatt Macy 	MPASS(scctx->isc_capenable);
460609f6ff4fSMatt Macy 	if (scctx->isc_capenable & IFCAP_TXCSUM)
460709f6ff4fSMatt Macy 		MPASS(scctx->isc_tx_csum_flags);
460809f6ff4fSMatt Macy #endif
460909f6ff4fSMatt Macy 
461009f6ff4fSMatt Macy 	if_setcapabilities(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE);
461109f6ff4fSMatt Macy 	if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE);
461209f6ff4fSMatt Macy 
461309f6ff4fSMatt Macy 	ifp->if_flags |= IFF_NOGROUP;
461409f6ff4fSMatt Macy 	if (sctx->isc_flags & IFLIB_PSEUDO) {
461509f6ff4fSMatt Macy 		ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
461609f6ff4fSMatt Macy 
461709f6ff4fSMatt Macy 		if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
461809f6ff4fSMatt Macy 			device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
461909f6ff4fSMatt Macy 			goto fail_detach;
462009f6ff4fSMatt Macy 		}
462109f6ff4fSMatt Macy 		*ctxp = ctx;
462209f6ff4fSMatt Macy 
462309f6ff4fSMatt Macy 		if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
462409f6ff4fSMatt Macy 		iflib_add_device_sysctl_post(ctx);
462509f6ff4fSMatt Macy 		ctx->ifc_flags |= IFC_INIT_DONE;
462609f6ff4fSMatt Macy 		return (0);
462709f6ff4fSMatt Macy 	}
462809f6ff4fSMatt Macy 	_iflib_pre_assert(scctx);
462909f6ff4fSMatt Macy 	ctx->ifc_txrx = *scctx->isc_txrx;
463009f6ff4fSMatt Macy 
463109f6ff4fSMatt Macy 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
463209f6ff4fSMatt Macy 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
463309f6ff4fSMatt Macy 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
463409f6ff4fSMatt Macy 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
463509f6ff4fSMatt Macy 
463609f6ff4fSMatt Macy 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
463709f6ff4fSMatt Macy 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
463809f6ff4fSMatt Macy 
463909f6ff4fSMatt Macy 	/* XXX change for per-queue sizes */
464009f6ff4fSMatt Macy 	device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
464109f6ff4fSMatt Macy 		      scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
464209f6ff4fSMatt Macy 	for (i = 0; i < sctx->isc_nrxqs; i++) {
464309f6ff4fSMatt Macy 		if (!powerof2(scctx->isc_nrxd[i])) {
464409f6ff4fSMatt Macy 			/* round down instead? */
464509f6ff4fSMatt Macy 			device_printf(dev, "# rx descriptors must be a power of 2\n");
464609f6ff4fSMatt Macy 			err = EINVAL;
464709f6ff4fSMatt Macy 			goto fail;
464809f6ff4fSMatt Macy 		}
464909f6ff4fSMatt Macy 	}
465009f6ff4fSMatt Macy 	for (i = 0; i < sctx->isc_ntxqs; i++) {
465109f6ff4fSMatt Macy 		if (!powerof2(scctx->isc_ntxd[i])) {
465209f6ff4fSMatt Macy 			device_printf(dev,
465309f6ff4fSMatt Macy 			    "# tx descriptors must be a power of 2");
465409f6ff4fSMatt Macy 			err = EINVAL;
465509f6ff4fSMatt Macy 			goto fail;
465609f6ff4fSMatt Macy 		}
465709f6ff4fSMatt Macy 	}
465809f6ff4fSMatt Macy 
465909f6ff4fSMatt Macy 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
466009f6ff4fSMatt Macy 	    MAX_SINGLE_PACKET_FRACTION)
466109f6ff4fSMatt Macy 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
466209f6ff4fSMatt Macy 		    MAX_SINGLE_PACKET_FRACTION);
466309f6ff4fSMatt Macy 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
466409f6ff4fSMatt Macy 	    MAX_SINGLE_PACKET_FRACTION)
466509f6ff4fSMatt Macy 		scctx->isc_tx_tso_segments_max = max(1,
466609f6ff4fSMatt Macy 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
466709f6ff4fSMatt Macy 
466809f6ff4fSMatt Macy 	/*
466909f6ff4fSMatt Macy 	 * Protect the stack against modern hardware
467009f6ff4fSMatt Macy 	 */
467109f6ff4fSMatt Macy 	if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
467209f6ff4fSMatt Macy 		scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
467309f6ff4fSMatt Macy 
467409f6ff4fSMatt Macy 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
467509f6ff4fSMatt Macy 	ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
467609f6ff4fSMatt Macy 	ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
467709f6ff4fSMatt Macy 	ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
467809f6ff4fSMatt Macy 	if (scctx->isc_rss_table_size == 0)
467909f6ff4fSMatt Macy 		scctx->isc_rss_table_size = 64;
468009f6ff4fSMatt Macy 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
468109f6ff4fSMatt Macy 
468209f6ff4fSMatt Macy 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
468309f6ff4fSMatt Macy 	/* XXX format name */
468409f6ff4fSMatt Macy 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
468509f6ff4fSMatt Macy 
468609f6ff4fSMatt Macy 	/* XXX --- can support > 1 -- but keep it simple for now */
468709f6ff4fSMatt Macy 	scctx->isc_intr = IFLIB_INTR_LEGACY;
468809f6ff4fSMatt Macy 
468909f6ff4fSMatt Macy 	/* Get memory for the station queues */
469009f6ff4fSMatt Macy 	if ((err = iflib_queues_alloc(ctx))) {
469109f6ff4fSMatt Macy 		device_printf(dev, "Unable to allocate queue memory\n");
469209f6ff4fSMatt Macy 		goto fail;
469309f6ff4fSMatt Macy 	}
469409f6ff4fSMatt Macy 
469509f6ff4fSMatt Macy 	if ((err = iflib_qset_structures_setup(ctx))) {
469609f6ff4fSMatt Macy 		device_printf(dev, "qset structure setup failed %d\n", err);
469709f6ff4fSMatt Macy 		goto fail_queues;
469809f6ff4fSMatt Macy 	}
469909f6ff4fSMatt Macy 	/*
470009f6ff4fSMatt Macy 	 * XXX What if anything do we want to do about interrupts?
470109f6ff4fSMatt Macy 	 */
470209f6ff4fSMatt Macy 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
470309f6ff4fSMatt Macy 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
470409f6ff4fSMatt Macy 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
470509f6ff4fSMatt Macy 		goto fail_detach;
470609f6ff4fSMatt Macy 	}
470709f6ff4fSMatt Macy 	/* XXX handle more than one queue */
470809f6ff4fSMatt Macy 	for (i = 0; i < scctx->isc_nrxqsets; i++)
470909f6ff4fSMatt Macy 		IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl);
471009f6ff4fSMatt Macy 
471109f6ff4fSMatt Macy 	*ctxp = ctx;
471209f6ff4fSMatt Macy 
471309f6ff4fSMatt Macy 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
471409f6ff4fSMatt Macy 	iflib_add_device_sysctl_post(ctx);
471509f6ff4fSMatt Macy 	ctx->ifc_flags |= IFC_INIT_DONE;
471609f6ff4fSMatt Macy 	return (0);
471709f6ff4fSMatt Macy fail_detach:
471809f6ff4fSMatt Macy 	ether_ifdetach(ctx->ifc_ifp);
471909f6ff4fSMatt Macy fail_queues:
472009f6ff4fSMatt Macy 	iflib_tx_structures_free(ctx);
472109f6ff4fSMatt Macy 	iflib_rx_structures_free(ctx);
472209f6ff4fSMatt Macy fail:
472309f6ff4fSMatt Macy 	IFDI_DETACH(ctx);
472409f6ff4fSMatt Macy 	return (err);
472509f6ff4fSMatt Macy }
472609f6ff4fSMatt Macy 
472709f6ff4fSMatt Macy int
472809f6ff4fSMatt Macy iflib_pseudo_deregister(if_ctx_t ctx)
472909f6ff4fSMatt Macy {
473009f6ff4fSMatt Macy 	if_t ifp = ctx->ifc_ifp;
473109f6ff4fSMatt Macy 	iflib_txq_t txq;
473209f6ff4fSMatt Macy 	iflib_rxq_t rxq;
473309f6ff4fSMatt Macy 	int i, j;
473409f6ff4fSMatt Macy 	struct taskqgroup *tqg;
473509f6ff4fSMatt Macy 	iflib_fl_t fl;
473609f6ff4fSMatt Macy 
473709f6ff4fSMatt Macy 	/* Unregister VLAN events */
473809f6ff4fSMatt Macy 	if (ctx->ifc_vlan_attach_event != NULL)
473909f6ff4fSMatt Macy 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
474009f6ff4fSMatt Macy 	if (ctx->ifc_vlan_detach_event != NULL)
474109f6ff4fSMatt Macy 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
474209f6ff4fSMatt Macy 
474309f6ff4fSMatt Macy 	ether_ifdetach(ifp);
474409f6ff4fSMatt Macy 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
474509f6ff4fSMatt Macy 	CTX_LOCK_DESTROY(ctx);
474609f6ff4fSMatt Macy 	/* XXX drain any dependent tasks */
474709f6ff4fSMatt Macy 	tqg = qgroup_if_io_tqg;
474809f6ff4fSMatt Macy 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
474909f6ff4fSMatt Macy 		callout_drain(&txq->ift_timer);
475009f6ff4fSMatt Macy 		if (txq->ift_task.gt_uniq != NULL)
475109f6ff4fSMatt Macy 			taskqgroup_detach(tqg, &txq->ift_task);
475209f6ff4fSMatt Macy 	}
475309f6ff4fSMatt Macy 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
475409f6ff4fSMatt Macy 		if (rxq->ifr_task.gt_uniq != NULL)
475509f6ff4fSMatt Macy 			taskqgroup_detach(tqg, &rxq->ifr_task);
475609f6ff4fSMatt Macy 
475709f6ff4fSMatt Macy 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
475809f6ff4fSMatt Macy 			free(fl->ifl_rx_bitmap, M_IFLIB);
475909f6ff4fSMatt Macy 	}
476009f6ff4fSMatt Macy 	tqg = qgroup_if_config_tqg;
476109f6ff4fSMatt Macy 	if (ctx->ifc_admin_task.gt_uniq != NULL)
476209f6ff4fSMatt Macy 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
476309f6ff4fSMatt Macy 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
476409f6ff4fSMatt Macy 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
476509f6ff4fSMatt Macy 
476609f6ff4fSMatt Macy 	if_free(ifp);
476709f6ff4fSMatt Macy 
476809f6ff4fSMatt Macy 	iflib_tx_structures_free(ctx);
476909f6ff4fSMatt Macy 	iflib_rx_structures_free(ctx);
477009f6ff4fSMatt Macy 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
477109f6ff4fSMatt Macy 		free(ctx->ifc_softc, M_IFLIB);
477209f6ff4fSMatt Macy 	free(ctx, M_IFLIB);
477309f6ff4fSMatt Macy 	return (0);
477409f6ff4fSMatt Macy }
477509f6ff4fSMatt Macy 
477609f6ff4fSMatt Macy int
47774c7070dbSScott Long iflib_device_attach(device_t dev)
47784c7070dbSScott Long {
47794c7070dbSScott Long 	if_ctx_t ctx;
47804c7070dbSScott Long 	if_shared_ctx_t sctx;
47814c7070dbSScott Long 
47824c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
47834c7070dbSScott Long 		return (ENOTSUP);
47844c7070dbSScott Long 
47854c7070dbSScott Long 	pci_enable_busmaster(dev);
47864c7070dbSScott Long 
47874c7070dbSScott Long 	return (iflib_device_register(dev, NULL, sctx, &ctx));
47884c7070dbSScott Long }
47894c7070dbSScott Long 
47904c7070dbSScott Long int
47914c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx)
47924c7070dbSScott Long {
47934c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
47944c7070dbSScott Long 	iflib_txq_t txq;
47954c7070dbSScott Long 	iflib_rxq_t rxq;
47964c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
479787890dbaSSean Bruno 	int i, j;
47984c7070dbSScott Long 	struct taskqgroup *tqg;
479987890dbaSSean Bruno 	iflib_fl_t fl;
48004c7070dbSScott Long 
48014c7070dbSScott Long 	/* Make sure VLANS are not using driver */
48024c7070dbSScott Long 	if (if_vlantrunkinuse(ifp)) {
48034c7070dbSScott Long 		device_printf(dev,"Vlan in use, detach first\n");
48044c7070dbSScott Long 		return (EBUSY);
48054c7070dbSScott Long 	}
48064c7070dbSScott Long 
48074c7070dbSScott Long 	CTX_LOCK(ctx);
48084c7070dbSScott Long 	ctx->ifc_in_detach = 1;
48094c7070dbSScott Long 	iflib_stop(ctx);
48104c7070dbSScott Long 	CTX_UNLOCK(ctx);
48114c7070dbSScott Long 
48124c7070dbSScott Long 	/* Unregister VLAN events */
48134c7070dbSScott Long 	if (ctx->ifc_vlan_attach_event != NULL)
48144c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
48154c7070dbSScott Long 	if (ctx->ifc_vlan_detach_event != NULL)
48164c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
48174c7070dbSScott Long 
48184c7070dbSScott Long 	iflib_netmap_detach(ifp);
48194c7070dbSScott Long 	ether_ifdetach(ifp);
4820ab2e3f79SStephen Hurd 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
4821ab2e3f79SStephen Hurd 	CTX_LOCK_DESTROY(ctx);
48224c7070dbSScott Long 	if (ctx->ifc_led_dev != NULL)
48234c7070dbSScott Long 		led_destroy(ctx->ifc_led_dev);
48244c7070dbSScott Long 	/* XXX drain any dependent tasks */
4825ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
482623ac9029SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
48274c7070dbSScott Long 		callout_drain(&txq->ift_timer);
48284c7070dbSScott Long 		if (txq->ift_task.gt_uniq != NULL)
48294c7070dbSScott Long 			taskqgroup_detach(tqg, &txq->ift_task);
48304c7070dbSScott Long 	}
48314c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
48324c7070dbSScott Long 		if (rxq->ifr_task.gt_uniq != NULL)
48334c7070dbSScott Long 			taskqgroup_detach(tqg, &rxq->ifr_task);
483487890dbaSSean Bruno 
483587890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
483687890dbaSSean Bruno 			free(fl->ifl_rx_bitmap, M_IFLIB);
483787890dbaSSean Bruno 
48384c7070dbSScott Long 	}
4839ab2e3f79SStephen Hurd 	tqg = qgroup_if_config_tqg;
48404c7070dbSScott Long 	if (ctx->ifc_admin_task.gt_uniq != NULL)
48414c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
48424c7070dbSScott Long 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
48434c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
48444c7070dbSScott Long 
48454c7070dbSScott Long 	IFDI_DETACH(ctx);
484623ac9029SStephen Hurd 	device_set_softc(ctx->ifc_dev, NULL);
48474c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
48484c7070dbSScott Long 		pci_release_msi(dev);
48494c7070dbSScott Long 	}
48504c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
48514c7070dbSScott Long 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
48524c7070dbSScott Long 	}
48534c7070dbSScott Long 	if (ctx->ifc_msix_mem != NULL) {
48544c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
48554c7070dbSScott Long 			ctx->ifc_softc_ctx.isc_msix_bar, ctx->ifc_msix_mem);
48564c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
48574c7070dbSScott Long 	}
48584c7070dbSScott Long 
48594c7070dbSScott Long 	bus_generic_detach(dev);
48604c7070dbSScott Long 	if_free(ifp);
48614c7070dbSScott Long 
48624c7070dbSScott Long 	iflib_tx_structures_free(ctx);
48634c7070dbSScott Long 	iflib_rx_structures_free(ctx);
486423ac9029SStephen Hurd 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
486523ac9029SStephen Hurd 		free(ctx->ifc_softc, M_IFLIB);
486623ac9029SStephen Hurd 	free(ctx, M_IFLIB);
48674c7070dbSScott Long 	return (0);
48684c7070dbSScott Long }
48694c7070dbSScott Long 
48704c7070dbSScott Long 
48714c7070dbSScott Long int
48724c7070dbSScott Long iflib_device_detach(device_t dev)
48734c7070dbSScott Long {
48744c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
48754c7070dbSScott Long 
48764c7070dbSScott Long 	return (iflib_device_deregister(ctx));
48774c7070dbSScott Long }
48784c7070dbSScott Long 
48794c7070dbSScott Long int
48804c7070dbSScott Long iflib_device_suspend(device_t dev)
48814c7070dbSScott Long {
48824c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
48834c7070dbSScott Long 
48844c7070dbSScott Long 	CTX_LOCK(ctx);
48854c7070dbSScott Long 	IFDI_SUSPEND(ctx);
48864c7070dbSScott Long 	CTX_UNLOCK(ctx);
48874c7070dbSScott Long 
48884c7070dbSScott Long 	return bus_generic_suspend(dev);
48894c7070dbSScott Long }
48904c7070dbSScott Long int
48914c7070dbSScott Long iflib_device_shutdown(device_t dev)
48924c7070dbSScott Long {
48934c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
48944c7070dbSScott Long 
48954c7070dbSScott Long 	CTX_LOCK(ctx);
48964c7070dbSScott Long 	IFDI_SHUTDOWN(ctx);
48974c7070dbSScott Long 	CTX_UNLOCK(ctx);
48984c7070dbSScott Long 
48994c7070dbSScott Long 	return bus_generic_suspend(dev);
49004c7070dbSScott Long }
49014c7070dbSScott Long 
49024c7070dbSScott Long 
49034c7070dbSScott Long int
49044c7070dbSScott Long iflib_device_resume(device_t dev)
49054c7070dbSScott Long {
49064c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
49074c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
49084c7070dbSScott Long 
49094c7070dbSScott Long 	CTX_LOCK(ctx);
49104c7070dbSScott Long 	IFDI_RESUME(ctx);
49114c7070dbSScott Long 	iflib_init_locked(ctx);
49124c7070dbSScott Long 	CTX_UNLOCK(ctx);
49134c7070dbSScott Long 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
49144c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
49154c7070dbSScott Long 
49164c7070dbSScott Long 	return (bus_generic_resume(dev));
49174c7070dbSScott Long }
49184c7070dbSScott Long 
49194c7070dbSScott Long int
49204c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
49214c7070dbSScott Long {
49224c7070dbSScott Long 	int error;
49234c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
49244c7070dbSScott Long 
49254c7070dbSScott Long 	CTX_LOCK(ctx);
49264c7070dbSScott Long 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
49274c7070dbSScott Long 	CTX_UNLOCK(ctx);
49284c7070dbSScott Long 
49294c7070dbSScott Long 	return (error);
49304c7070dbSScott Long }
49314c7070dbSScott Long 
49324c7070dbSScott Long void
49334c7070dbSScott Long iflib_device_iov_uninit(device_t dev)
49344c7070dbSScott Long {
49354c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
49364c7070dbSScott Long 
49374c7070dbSScott Long 	CTX_LOCK(ctx);
49384c7070dbSScott Long 	IFDI_IOV_UNINIT(ctx);
49394c7070dbSScott Long 	CTX_UNLOCK(ctx);
49404c7070dbSScott Long }
49414c7070dbSScott Long 
49424c7070dbSScott Long int
49434c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
49444c7070dbSScott Long {
49454c7070dbSScott Long 	int error;
49464c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
49474c7070dbSScott Long 
49484c7070dbSScott Long 	CTX_LOCK(ctx);
49494c7070dbSScott Long 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
49504c7070dbSScott Long 	CTX_UNLOCK(ctx);
49514c7070dbSScott Long 
49524c7070dbSScott Long 	return (error);
49534c7070dbSScott Long }
49544c7070dbSScott Long 
49554c7070dbSScott Long /*********************************************************************
49564c7070dbSScott Long  *
49574c7070dbSScott Long  *  MODULE FUNCTION DEFINITIONS
49584c7070dbSScott Long  *
49594c7070dbSScott Long  **********************************************************************/
49604c7070dbSScott Long 
4961ab2e3f79SStephen Hurd /*
4962ab2e3f79SStephen Hurd  * - Start a fast taskqueue thread for each core
4963ab2e3f79SStephen Hurd  * - Start a taskqueue for control operations
4964ab2e3f79SStephen Hurd  */
49654c7070dbSScott Long static int
49664c7070dbSScott Long iflib_module_init(void)
49674c7070dbSScott Long {
49684c7070dbSScott Long 	return (0);
49694c7070dbSScott Long }
49704c7070dbSScott Long 
49714c7070dbSScott Long static int
49724c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg)
49734c7070dbSScott Long {
49744c7070dbSScott Long 	int err;
49754c7070dbSScott Long 
49764c7070dbSScott Long 	switch (what) {
49774c7070dbSScott Long 	case MOD_LOAD:
49784c7070dbSScott Long 		if ((err = iflib_module_init()) != 0)
49794c7070dbSScott Long 			return (err);
49804c7070dbSScott Long 		break;
49814c7070dbSScott Long 	case MOD_UNLOAD:
49824c7070dbSScott Long 		return (EBUSY);
49834c7070dbSScott Long 	default:
49844c7070dbSScott Long 		return (EOPNOTSUPP);
49854c7070dbSScott Long 	}
49864c7070dbSScott Long 
49874c7070dbSScott Long 	return (0);
49884c7070dbSScott Long }
49894c7070dbSScott Long 
49904c7070dbSScott Long /*********************************************************************
49914c7070dbSScott Long  *
49924c7070dbSScott Long  *  PUBLIC FUNCTION DEFINITIONS
49934c7070dbSScott Long  *     ordered as in iflib.h
49944c7070dbSScott Long  *
49954c7070dbSScott Long  **********************************************************************/
49964c7070dbSScott Long 
49974c7070dbSScott Long 
49984c7070dbSScott Long static void
49994c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx)
50004c7070dbSScott Long {
50014c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsize);
50024c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsegsize);
50034c7070dbSScott Long 
50044c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsize);
50054c7070dbSScott Long 	MPASS(sctx->isc_rx_nsegments);
50064c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsegsize);
50074c7070dbSScott Long 
500823ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_min[0]);
500923ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_max[0]);
501023ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_default[0]);
501123ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_min[0]);
501223ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_max[0]);
501323ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_default[0]);
50144c7070dbSScott Long }
50154c7070dbSScott Long 
50161248952aSSean Bruno static void
50171248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx)
50181248952aSSean Bruno {
50191248952aSSean Bruno 
50201248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_encap);
50211248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_flush);
50221248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
50231248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_available);
50241248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
50251248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_refill);
50261248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_flush);
50271248952aSSean Bruno }
50282fe66646SSean Bruno 
50294c7070dbSScott Long static int
50304c7070dbSScott Long iflib_register(if_ctx_t ctx)
50314c7070dbSScott Long {
50324c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
50334c7070dbSScott Long 	driver_t *driver = sctx->isc_driver;
50344c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
50354c7070dbSScott Long 	if_t ifp;
50364c7070dbSScott Long 
50374c7070dbSScott Long 	_iflib_assert(sctx);
50384c7070dbSScott Long 
5039aa8a24d3SStephen Hurd 	CTX_LOCK_INIT(ctx);
50407b610b60SSean Bruno 	STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
50414c7070dbSScott Long 	ifp = ctx->ifc_ifp = if_gethandle(IFT_ETHER);
50424c7070dbSScott Long 	if (ifp == NULL) {
50434c7070dbSScott Long 		device_printf(dev, "can not allocate ifnet structure\n");
50444c7070dbSScott Long 		return (ENOMEM);
50454c7070dbSScott Long 	}
50464c7070dbSScott Long 
50474c7070dbSScott Long 	/*
50484c7070dbSScott Long 	 * Initialize our context's device specific methods
50494c7070dbSScott Long 	 */
50504c7070dbSScott Long 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
50514c7070dbSScott Long 	kobj_class_compile((kobj_class_t) driver);
50524c7070dbSScott Long 	driver->refs++;
50534c7070dbSScott Long 
50544c7070dbSScott Long 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
50554c7070dbSScott Long 	if_setsoftc(ifp, ctx);
50564c7070dbSScott Long 	if_setdev(ifp, dev);
50574c7070dbSScott Long 	if_setinitfn(ifp, iflib_if_init);
50584c7070dbSScott Long 	if_setioctlfn(ifp, iflib_if_ioctl);
50594c7070dbSScott Long 	if_settransmitfn(ifp, iflib_if_transmit);
50604c7070dbSScott Long 	if_setqflushfn(ifp, iflib_if_qflush);
50614c7070dbSScott Long 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
50624c7070dbSScott Long 
50634c7070dbSScott Long 	ctx->ifc_vlan_attach_event =
50644c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
50654c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
50664c7070dbSScott Long 	ctx->ifc_vlan_detach_event =
50674c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
50684c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
50694c7070dbSScott Long 
50704c7070dbSScott Long 	ifmedia_init(&ctx->ifc_media, IFM_IMASK,
50714c7070dbSScott Long 					 iflib_media_change, iflib_media_status);
50724c7070dbSScott Long 
50734c7070dbSScott Long 	return (0);
50744c7070dbSScott Long }
50754c7070dbSScott Long 
50764c7070dbSScott Long 
50774c7070dbSScott Long static int
50784c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx)
50794c7070dbSScott Long {
50804c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
508123ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
50824c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
508323ac9029SStephen Hurd 	int nrxqsets = scctx->isc_nrxqsets;
508423ac9029SStephen Hurd 	int ntxqsets = scctx->isc_ntxqsets;
50854c7070dbSScott Long 	iflib_txq_t txq;
50864c7070dbSScott Long 	iflib_rxq_t rxq;
50874c7070dbSScott Long 	iflib_fl_t fl = NULL;
508823ac9029SStephen Hurd 	int i, j, cpu, err, txconf, rxconf;
50894c7070dbSScott Long 	iflib_dma_info_t ifdip;
509023ac9029SStephen Hurd 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
509123ac9029SStephen Hurd 	uint32_t *txqsizes = scctx->isc_txqsizes;
50924c7070dbSScott Long 	uint8_t nrxqs = sctx->isc_nrxqs;
50934c7070dbSScott Long 	uint8_t ntxqs = sctx->isc_ntxqs;
50944c7070dbSScott Long 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
50954c7070dbSScott Long 	caddr_t *vaddrs;
50964c7070dbSScott Long 	uint64_t *paddrs;
50974c7070dbSScott Long 
509823ac9029SStephen Hurd 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
509923ac9029SStephen Hurd 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
51004c7070dbSScott Long 
51014c7070dbSScott Long /* Allocate the TX ring struct memory */
5102b89827a0SStephen Hurd 	if (!(ctx->ifc_txqs =
5103ac2fffa4SPedro F. Giffuni 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
5104ac2fffa4SPedro F. Giffuni 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
51054c7070dbSScott Long 		device_printf(dev, "Unable to allocate TX ring memory\n");
51064c7070dbSScott Long 		err = ENOMEM;
51074c7070dbSScott Long 		goto fail;
51084c7070dbSScott Long 	}
51094c7070dbSScott Long 
51104c7070dbSScott Long 	/* Now allocate the RX */
5111b89827a0SStephen Hurd 	if (!(ctx->ifc_rxqs =
5112ac2fffa4SPedro F. Giffuni 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
5113ac2fffa4SPedro F. Giffuni 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
51144c7070dbSScott Long 		device_printf(dev, "Unable to allocate RX ring memory\n");
51154c7070dbSScott Long 		err = ENOMEM;
51164c7070dbSScott Long 		goto rx_fail;
51174c7070dbSScott Long 	}
51184c7070dbSScott Long 
5119b89827a0SStephen Hurd 	txq = ctx->ifc_txqs;
5120b89827a0SStephen Hurd 	rxq = ctx->ifc_rxqs;
51214c7070dbSScott Long 
51224c7070dbSScott Long 	/*
51234c7070dbSScott Long 	 * XXX handle allocation failure
51244c7070dbSScott Long 	 */
512596c85efbSNathan Whitehorn 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
51264c7070dbSScott Long 		/* Set up some basics */
51274c7070dbSScott Long 
51284c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
51294c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
51304c7070dbSScott Long 			err = ENOMEM;
51310d0338afSConrad Meyer 			goto err_tx_desc;
51324c7070dbSScott Long 		}
51334c7070dbSScott Long 		txq->ift_ifdi = ifdip;
51344c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, ifdip++) {
51354c7070dbSScott Long 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
51364c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
51374c7070dbSScott Long 				err = ENOMEM;
51384c7070dbSScott Long 				goto err_tx_desc;
51394c7070dbSScott Long 			}
514095246abbSSean Bruno 			txq->ift_txd_size[j] = scctx->isc_txd_size[j];
51414c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
51424c7070dbSScott Long 		}
51434c7070dbSScott Long 		txq->ift_ctx = ctx;
51444c7070dbSScott Long 		txq->ift_id = i;
514523ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
514623ac9029SStephen Hurd 			txq->ift_br_offset = 1;
514723ac9029SStephen Hurd 		} else {
514823ac9029SStephen Hurd 			txq->ift_br_offset = 0;
514923ac9029SStephen Hurd 		}
51504c7070dbSScott Long 		/* XXX fix this */
515196c85efbSNathan Whitehorn 		txq->ift_timer.c_cpu = cpu;
51524c7070dbSScott Long 
51534c7070dbSScott Long 		if (iflib_txsd_alloc(txq)) {
51544c7070dbSScott Long 			device_printf(dev, "Critical Failure setting up TX buffers\n");
51554c7070dbSScott Long 			err = ENOMEM;
51564c7070dbSScott Long 			goto err_tx_desc;
51574c7070dbSScott Long 		}
51584c7070dbSScott Long 
51594c7070dbSScott Long 		/* Initialize the TX lock */
51604c7070dbSScott Long 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout",
51614c7070dbSScott Long 		    device_get_nameunit(dev), txq->ift_id);
51624c7070dbSScott Long 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
51634c7070dbSScott Long 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
51644c7070dbSScott Long 
51654c7070dbSScott Long 		snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db",
51664c7070dbSScott Long 			 device_get_nameunit(dev), txq->ift_id);
51674c7070dbSScott Long 
516895246abbSSean Bruno 		err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
51694c7070dbSScott Long 				      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
51704c7070dbSScott Long 		if (err) {
51714c7070dbSScott Long 			/* XXX free any allocated rings */
51724c7070dbSScott Long 			device_printf(dev, "Unable to allocate buf_ring\n");
51730d0338afSConrad Meyer 			goto err_tx_desc;
51744c7070dbSScott Long 		}
51754c7070dbSScott Long 	}
51764c7070dbSScott Long 
51774c7070dbSScott Long 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
51784c7070dbSScott Long 		/* Set up some basics */
51794c7070dbSScott Long 
51804c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
51814c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
51824c7070dbSScott Long 			err = ENOMEM;
51830d0338afSConrad Meyer 			goto err_tx_desc;
51844c7070dbSScott Long 		}
51854c7070dbSScott Long 
51864c7070dbSScott Long 		rxq->ifr_ifdi = ifdip;
518795246abbSSean Bruno 		/* XXX this needs to be changed if #rx queues != #tx queues */
518895246abbSSean Bruno 		rxq->ifr_ntxqirq = 1;
518995246abbSSean Bruno 		rxq->ifr_txqid[0] = i;
51904c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, ifdip++) {
51914c7070dbSScott Long 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
51924c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
51934c7070dbSScott Long 				err = ENOMEM;
51944c7070dbSScott Long 				goto err_tx_desc;
51954c7070dbSScott Long 			}
51964c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
51974c7070dbSScott Long 		}
51984c7070dbSScott Long 		rxq->ifr_ctx = ctx;
51994c7070dbSScott Long 		rxq->ifr_id = i;
520023ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
520123ac9029SStephen Hurd 			rxq->ifr_fl_offset = 1;
52024c7070dbSScott Long 		} else {
520323ac9029SStephen Hurd 			rxq->ifr_fl_offset = 0;
52044c7070dbSScott Long 		}
52054c7070dbSScott Long 		rxq->ifr_nfl = nfree_lists;
52064c7070dbSScott Long 		if (!(fl =
5207ac2fffa4SPedro F. Giffuni 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
52084c7070dbSScott Long 			device_printf(dev, "Unable to allocate free list memory\n");
52094c7070dbSScott Long 			err = ENOMEM;
52100d0338afSConrad Meyer 			goto err_tx_desc;
52114c7070dbSScott Long 		}
52124c7070dbSScott Long 		rxq->ifr_fl = fl;
52134c7070dbSScott Long 		for (j = 0; j < nfree_lists; j++) {
521495246abbSSean Bruno 			fl[j].ifl_rxq = rxq;
521595246abbSSean Bruno 			fl[j].ifl_id = j;
521695246abbSSean Bruno 			fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
521795246abbSSean Bruno 			fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
52184c7070dbSScott Long 		}
52194c7070dbSScott Long         /* Allocate receive buffers for the ring*/
52204c7070dbSScott Long 		if (iflib_rxsd_alloc(rxq)) {
52214c7070dbSScott Long 			device_printf(dev,
52224c7070dbSScott Long 			    "Critical Failure setting up receive buffers\n");
52234c7070dbSScott Long 			err = ENOMEM;
52244c7070dbSScott Long 			goto err_rx_desc;
52254c7070dbSScott Long 		}
522687890dbaSSean Bruno 
522787890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
522887890dbaSSean Bruno 			fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, M_WAITOK|M_ZERO);
52294c7070dbSScott Long 	}
52304c7070dbSScott Long 
52314c7070dbSScott Long 	/* TXQs */
52324c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
52334c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
52344c7070dbSScott Long 	for (i = 0; i < ntxqsets; i++) {
52354c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
52364c7070dbSScott Long 
52374c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, di++) {
52384c7070dbSScott Long 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
52394c7070dbSScott Long 			paddrs[i*ntxqs + j] = di->idi_paddr;
52404c7070dbSScott Long 		}
52414c7070dbSScott Long 	}
52424c7070dbSScott Long 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
52434c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
52444c7070dbSScott Long 		iflib_tx_structures_free(ctx);
52454c7070dbSScott Long 		free(vaddrs, M_IFLIB);
52464c7070dbSScott Long 		free(paddrs, M_IFLIB);
52474c7070dbSScott Long 		goto err_rx_desc;
52484c7070dbSScott Long 	}
52494c7070dbSScott Long 	free(vaddrs, M_IFLIB);
52504c7070dbSScott Long 	free(paddrs, M_IFLIB);
52514c7070dbSScott Long 
52524c7070dbSScott Long 	/* RXQs */
52534c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
52544c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
52554c7070dbSScott Long 	for (i = 0; i < nrxqsets; i++) {
52564c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
52574c7070dbSScott Long 
52584c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, di++) {
52594c7070dbSScott Long 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
52604c7070dbSScott Long 			paddrs[i*nrxqs + j] = di->idi_paddr;
52614c7070dbSScott Long 		}
52624c7070dbSScott Long 	}
52634c7070dbSScott Long 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
52644c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
52654c7070dbSScott Long 		iflib_tx_structures_free(ctx);
52664c7070dbSScott Long 		free(vaddrs, M_IFLIB);
52674c7070dbSScott Long 		free(paddrs, M_IFLIB);
52684c7070dbSScott Long 		goto err_rx_desc;
52694c7070dbSScott Long 	}
52704c7070dbSScott Long 	free(vaddrs, M_IFLIB);
52714c7070dbSScott Long 	free(paddrs, M_IFLIB);
52724c7070dbSScott Long 
52734c7070dbSScott Long 	return (0);
52744c7070dbSScott Long 
52754c7070dbSScott Long /* XXX handle allocation failure changes */
52764c7070dbSScott Long err_rx_desc:
52774c7070dbSScott Long err_tx_desc:
5278b89827a0SStephen Hurd rx_fail:
52794c7070dbSScott Long 	if (ctx->ifc_rxqs != NULL)
52804c7070dbSScott Long 		free(ctx->ifc_rxqs, M_IFLIB);
52814c7070dbSScott Long 	ctx->ifc_rxqs = NULL;
52824c7070dbSScott Long 	if (ctx->ifc_txqs != NULL)
52834c7070dbSScott Long 		free(ctx->ifc_txqs, M_IFLIB);
52844c7070dbSScott Long 	ctx->ifc_txqs = NULL;
52854c7070dbSScott Long fail:
52864c7070dbSScott Long 	return (err);
52874c7070dbSScott Long }
52884c7070dbSScott Long 
52894c7070dbSScott Long static int
52904c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx)
52914c7070dbSScott Long {
52924c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
52934c7070dbSScott Long 	int i;
52944c7070dbSScott Long 
52954c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
52964c7070dbSScott Long 		iflib_txq_setup(txq);
52974c7070dbSScott Long 
52984c7070dbSScott Long 	return (0);
52994c7070dbSScott Long }
53004c7070dbSScott Long 
53014c7070dbSScott Long static void
53024c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx)
53034c7070dbSScott Long {
53044c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
53054c7070dbSScott Long 	int i, j;
53064c7070dbSScott Long 
53074c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
53084c7070dbSScott Long 		iflib_txq_destroy(txq);
53094c7070dbSScott Long 		for (j = 0; j < ctx->ifc_nhwtxqs; j++)
53104c7070dbSScott Long 			iflib_dma_free(&txq->ift_ifdi[j]);
53114c7070dbSScott Long 	}
53124c7070dbSScott Long 	free(ctx->ifc_txqs, M_IFLIB);
53134c7070dbSScott Long 	ctx->ifc_txqs = NULL;
53144c7070dbSScott Long 	IFDI_QUEUES_FREE(ctx);
53154c7070dbSScott Long }
53164c7070dbSScott Long 
53174c7070dbSScott Long /*********************************************************************
53184c7070dbSScott Long  *
53194c7070dbSScott Long  *  Initialize all receive rings.
53204c7070dbSScott Long  *
53214c7070dbSScott Long  **********************************************************************/
53224c7070dbSScott Long static int
53234c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx)
53244c7070dbSScott Long {
53254c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
5326aaeb188aSBjoern A. Zeeb 	int q;
5327aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
5328aaeb188aSBjoern A. Zeeb 	int i, err;
5329aaeb188aSBjoern A. Zeeb #endif
53304c7070dbSScott Long 
53314c7070dbSScott Long 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
5332aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
53334c7070dbSScott Long 		tcp_lro_free(&rxq->ifr_lc);
533423ac9029SStephen Hurd 		if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
533523ac9029SStephen Hurd 		    TCP_LRO_ENTRIES, min(1024,
533623ac9029SStephen Hurd 		    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) {
53374c7070dbSScott Long 			device_printf(ctx->ifc_dev, "LRO Initialization failed!\n");
53384c7070dbSScott Long 			goto fail;
53394c7070dbSScott Long 		}
53404c7070dbSScott Long 		rxq->ifr_lro_enabled = TRUE;
5341aaeb188aSBjoern A. Zeeb #endif
53424c7070dbSScott Long 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
53434c7070dbSScott Long 	}
53444c7070dbSScott Long 	return (0);
5345aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
53464c7070dbSScott Long fail:
53474c7070dbSScott Long 	/*
53484c7070dbSScott Long 	 * Free RX software descriptors allocated so far, we will only handle
53494c7070dbSScott Long 	 * the rings that completed, the failing case will have
53504c7070dbSScott Long 	 * cleaned up for itself. 'q' failed, so its the terminus.
53514c7070dbSScott Long 	 */
53524c7070dbSScott Long 	rxq = ctx->ifc_rxqs;
53534c7070dbSScott Long 	for (i = 0; i < q; ++i, rxq++) {
53544c7070dbSScott Long 		iflib_rx_sds_free(rxq);
53554c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
53564c7070dbSScott Long 	}
53574c7070dbSScott Long 	return (err);
5358aaeb188aSBjoern A. Zeeb #endif
53594c7070dbSScott Long }
53604c7070dbSScott Long 
53614c7070dbSScott Long /*********************************************************************
53624c7070dbSScott Long  *
53634c7070dbSScott Long  *  Free all receive rings.
53644c7070dbSScott Long  *
53654c7070dbSScott Long  **********************************************************************/
53664c7070dbSScott Long static void
53674c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx)
53684c7070dbSScott Long {
53694c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
53704c7070dbSScott Long 
537123ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
53724c7070dbSScott Long 		iflib_rx_sds_free(rxq);
53734c7070dbSScott Long 	}
53744c7070dbSScott Long }
53754c7070dbSScott Long 
53764c7070dbSScott Long static int
53774c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx)
53784c7070dbSScott Long {
53794c7070dbSScott Long 	int err;
53804c7070dbSScott Long 
53816108c013SStephen Hurd 	/*
53826108c013SStephen Hurd 	 * It is expected that the caller takes care of freeing queues if this
53836108c013SStephen Hurd 	 * fails.
53846108c013SStephen Hurd 	 */
5385ac88e6daSStephen Hurd 	if ((err = iflib_tx_structures_setup(ctx)) != 0) {
5386ac88e6daSStephen Hurd 		device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err);
53874c7070dbSScott Long 		return (err);
5388ac88e6daSStephen Hurd 	}
53894c7070dbSScott Long 
53906108c013SStephen Hurd 	if ((err = iflib_rx_structures_setup(ctx)) != 0)
53914c7070dbSScott Long 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
53926108c013SStephen Hurd 
53934c7070dbSScott Long 	return (err);
53944c7070dbSScott Long }
53954c7070dbSScott Long 
53964c7070dbSScott Long int
53974c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
53984c7070dbSScott Long 				driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, char *name)
53994c7070dbSScott Long {
54004c7070dbSScott Long 
54014c7070dbSScott Long 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
54024c7070dbSScott Long }
54034c7070dbSScott Long 
5404b103855eSStephen Hurd #ifdef SMP
5405aa3c5dd8SSean Bruno static int
5406b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid)
54074c7070dbSScott Long {
5408b103855eSStephen Hurd 	cpuset_t cpus;
5409aa3c5dd8SSean Bruno 	int i, cpuid, eqid, count;
54104c7070dbSScott Long 
5411b103855eSStephen Hurd 	CPU_COPY(&ctx->ifc_cpus, &cpus);
5412b103855eSStephen Hurd 	count = CPU_COUNT(&cpus);
5413aa3c5dd8SSean Bruno 	eqid = qid % count;
54144c7070dbSScott Long 	/* clear up to the qid'th bit */
5415aa3c5dd8SSean Bruno 	for (i = 0; i < eqid; i++) {
5416b103855eSStephen Hurd 		cpuid = CPU_FFS(&cpus);
5417aa3c5dd8SSean Bruno 		MPASS(cpuid != 0);
5418b103855eSStephen Hurd 		CPU_CLR(cpuid-1, &cpus);
54194c7070dbSScott Long 	}
5420b103855eSStephen Hurd 	cpuid = CPU_FFS(&cpus);
5421aa3c5dd8SSean Bruno 	MPASS(cpuid != 0);
5422aa3c5dd8SSean Bruno 	return (cpuid-1);
54234c7070dbSScott Long }
54244c7070dbSScott Long 
5425b103855eSStephen Hurd #ifdef SCHED_ULE
5426b103855eSStephen Hurd extern struct cpu_group *cpu_top;              /* CPU topology */
5427b103855eSStephen Hurd 
5428b103855eSStephen Hurd static int
5429b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp)
5430b103855eSStephen Hurd {
5431b103855eSStephen Hurd 	int i;
5432b103855eSStephen Hurd 
5433b103855eSStephen Hurd 	if (grp->cg_children == 0)
5434b103855eSStephen Hurd 		return -1;
5435b103855eSStephen Hurd 
5436b103855eSStephen Hurd 	MPASS(grp->cg_child);
5437b103855eSStephen Hurd 	for (i = 0; i < grp->cg_children; i++) {
5438b103855eSStephen Hurd 		if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask))
5439b103855eSStephen Hurd 			return i;
5440b103855eSStephen Hurd 	}
5441b103855eSStephen Hurd 
5442b103855eSStephen Hurd 	return -1;
5443b103855eSStephen Hurd }
5444b103855eSStephen Hurd 
5445b103855eSStephen Hurd /*
54460b75ac77SStephen Hurd  * Find the nth "close" core to the specified core
54470b75ac77SStephen Hurd  * "close" is defined as the deepest level that shares
54480b75ac77SStephen Hurd  * at least an L2 cache.  With threads, this will be
54490b75ac77SStephen Hurd  * threads on the same core.  If the sahred cache is L3
54500b75ac77SStephen Hurd  * or higher, simply returns the same core.
5451b103855eSStephen Hurd  */
5452b103855eSStephen Hurd static int
54530b75ac77SStephen Hurd find_close_core(int cpu, int core_offset)
5454b103855eSStephen Hurd {
5455b103855eSStephen Hurd 	struct cpu_group *grp;
5456b103855eSStephen Hurd 	int i;
54570b75ac77SStephen Hurd 	int fcpu;
5458b103855eSStephen Hurd 	cpuset_t cs;
5459b103855eSStephen Hurd 
5460b103855eSStephen Hurd 	grp = cpu_top;
5461b103855eSStephen Hurd 	if (grp == NULL)
5462b103855eSStephen Hurd 		return cpu;
5463b103855eSStephen Hurd 	i = 0;
5464b103855eSStephen Hurd 	while ((i = find_child_with_core(cpu, grp)) != -1) {
5465b103855eSStephen Hurd 		/* If the child only has one cpu, don't descend */
5466b103855eSStephen Hurd 		if (grp->cg_child[i].cg_count <= 1)
5467b103855eSStephen Hurd 			break;
5468b103855eSStephen Hurd 		grp = &grp->cg_child[i];
5469b103855eSStephen Hurd 	}
5470b103855eSStephen Hurd 
5471b103855eSStephen Hurd 	/* If they don't share at least an L2 cache, use the same CPU */
5472b103855eSStephen Hurd 	if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE)
5473b103855eSStephen Hurd 		return cpu;
5474b103855eSStephen Hurd 
5475b103855eSStephen Hurd 	/* Now pick one */
5476b103855eSStephen Hurd 	CPU_COPY(&grp->cg_mask, &cs);
54770b75ac77SStephen Hurd 
54780b75ac77SStephen Hurd 	/* Add the selected CPU offset to core offset. */
54790b75ac77SStephen Hurd 	for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) {
54800b75ac77SStephen Hurd 		if (fcpu - 1 == cpu)
54810b75ac77SStephen Hurd 			break;
54820b75ac77SStephen Hurd 		CPU_CLR(fcpu - 1, &cs);
54830b75ac77SStephen Hurd 	}
54840b75ac77SStephen Hurd 	MPASS(fcpu);
54850b75ac77SStephen Hurd 
54860b75ac77SStephen Hurd 	core_offset += i;
54870b75ac77SStephen Hurd 
54880b75ac77SStephen Hurd 	CPU_COPY(&grp->cg_mask, &cs);
54890b75ac77SStephen Hurd 	for (i = core_offset % grp->cg_count; i > 0; i--) {
5490b103855eSStephen Hurd 		MPASS(CPU_FFS(&cs));
5491b103855eSStephen Hurd 		CPU_CLR(CPU_FFS(&cs) - 1, &cs);
5492b103855eSStephen Hurd 	}
5493b103855eSStephen Hurd 	MPASS(CPU_FFS(&cs));
5494b103855eSStephen Hurd 	return CPU_FFS(&cs) - 1;
5495b103855eSStephen Hurd }
5496b103855eSStephen Hurd #else
5497b103855eSStephen Hurd static int
54980b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused)
5499b103855eSStephen Hurd {
550097755e83SKonstantin Belousov 	return cpu;
5501b103855eSStephen Hurd }
5502b103855eSStephen Hurd #endif
5503b103855eSStephen Hurd 
5504b103855eSStephen Hurd static int
55050b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid)
5506b103855eSStephen Hurd {
5507b103855eSStephen Hurd 	switch (type) {
5508b103855eSStephen Hurd 	case IFLIB_INTR_TX:
55090b75ac77SStephen Hurd 		/* TX queues get cores which share at least an L2 cache with the corresponding RX queue */
55100b75ac77SStephen Hurd 		/* XXX handle multiple RX threads per core and more than two core per L2 group */
5511b103855eSStephen Hurd 		return qid / CPU_COUNT(&ctx->ifc_cpus) + 1;
5512b103855eSStephen Hurd 	case IFLIB_INTR_RX:
5513b103855eSStephen Hurd 	case IFLIB_INTR_RXTX:
55140b75ac77SStephen Hurd 		/* RX queues get the specified core */
5515b103855eSStephen Hurd 		return qid / CPU_COUNT(&ctx->ifc_cpus);
5516b103855eSStephen Hurd 	default:
5517b103855eSStephen Hurd 		return -1;
5518b103855eSStephen Hurd 	}
5519b103855eSStephen Hurd }
5520b103855eSStephen Hurd #else
55210b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid)	CPU_FIRST()
55220b75ac77SStephen Hurd #define find_close_core(cpuid, tid)	CPU_FIRST()
5523b103855eSStephen Hurd #define find_nth(ctx, gid)		CPU_FIRST()
5524b103855eSStephen Hurd #endif
5525b103855eSStephen Hurd 
5526b103855eSStephen Hurd /* Just to avoid copy/paste */
5527b103855eSStephen Hurd static inline int
5528b103855eSStephen Hurd iflib_irq_set_affinity(if_ctx_t ctx, int irq, iflib_intr_type_t type, int qid,
5529b103855eSStephen Hurd     struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, char *name)
5530b103855eSStephen Hurd {
5531b103855eSStephen Hurd 	int cpuid;
5532b103855eSStephen Hurd 	int err, tid;
5533b103855eSStephen Hurd 
5534b103855eSStephen Hurd 	cpuid = find_nth(ctx, qid);
55350b75ac77SStephen Hurd 	tid = get_core_offset(ctx, type, qid);
5536b103855eSStephen Hurd 	MPASS(tid >= 0);
55370b75ac77SStephen Hurd 	cpuid = find_close_core(cpuid, tid);
5538b103855eSStephen Hurd 	err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, irq, name);
5539b103855eSStephen Hurd 	if (err) {
5540b103855eSStephen Hurd 		device_printf(ctx->ifc_dev, "taskqgroup_attach_cpu failed %d\n", err);
5541b103855eSStephen Hurd 		return (err);
5542b103855eSStephen Hurd 	}
5543b103855eSStephen Hurd #ifdef notyet
5544b103855eSStephen Hurd 	if (cpuid > ctx->ifc_cpuid_highest)
5545b103855eSStephen Hurd 		ctx->ifc_cpuid_highest = cpuid;
5546b103855eSStephen Hurd #endif
5547b103855eSStephen Hurd 	return 0;
5548b103855eSStephen Hurd }
5549b103855eSStephen Hurd 
55504c7070dbSScott Long int
55514c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
55524c7070dbSScott Long 						iflib_intr_type_t type, driver_filter_t *filter,
55534c7070dbSScott Long 						void *filter_arg, int qid, char *name)
55544c7070dbSScott Long {
55554c7070dbSScott Long 	struct grouptask *gtask;
55564c7070dbSScott Long 	struct taskqgroup *tqg;
55574c7070dbSScott Long 	iflib_filter_info_t info;
555823ac9029SStephen Hurd 	gtask_fn_t *fn;
5559b103855eSStephen Hurd 	int tqrid, err;
556095246abbSSean Bruno 	driver_filter_t *intr_fast;
55614c7070dbSScott Long 	void *q;
55624c7070dbSScott Long 
55634c7070dbSScott Long 	info = &ctx->ifc_filter_info;
5564add6f7d0SSean Bruno 	tqrid = rid;
55654c7070dbSScott Long 
55664c7070dbSScott Long 	switch (type) {
55674c7070dbSScott Long 	/* XXX merge tx/rx for netmap? */
55684c7070dbSScott Long 	case IFLIB_INTR_TX:
55694c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
55704c7070dbSScott Long 		info = &ctx->ifc_txqs[qid].ift_filter_info;
55714c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
5572ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
55734c7070dbSScott Long 		fn = _task_fn_tx;
557495246abbSSean Bruno 		intr_fast = iflib_fast_intr;
5575da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
5576*5ee36c68SStephen Hurd 		ctx->ifc_flags |= IFC_NETMAP_TX_IRQ;
55774c7070dbSScott Long 		break;
55784c7070dbSScott Long 	case IFLIB_INTR_RX:
55794c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
55804c7070dbSScott Long 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
55814c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5582ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
55834c7070dbSScott Long 		fn = _task_fn_rx;
5584ab2e3f79SStephen Hurd 		intr_fast = iflib_fast_intr;
558595246abbSSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
558695246abbSSean Bruno 		break;
558795246abbSSean Bruno 	case IFLIB_INTR_RXTX:
558895246abbSSean Bruno 		q = &ctx->ifc_rxqs[qid];
558995246abbSSean Bruno 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
559095246abbSSean Bruno 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5591ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
559295246abbSSean Bruno 		fn = _task_fn_rx;
559395246abbSSean Bruno 		intr_fast = iflib_fast_intr_rxtx;
5594da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
55954c7070dbSScott Long 		break;
55964c7070dbSScott Long 	case IFLIB_INTR_ADMIN:
55974c7070dbSScott Long 		q = ctx;
5598da69b8f9SSean Bruno 		tqrid = -1;
55994c7070dbSScott Long 		info = &ctx->ifc_filter_info;
56004c7070dbSScott Long 		gtask = &ctx->ifc_admin_task;
5601ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
56024c7070dbSScott Long 		fn = _task_fn_admin;
560395246abbSSean Bruno 		intr_fast = iflib_fast_intr_ctx;
56044c7070dbSScott Long 		break;
56054c7070dbSScott Long 	default:
56064c7070dbSScott Long 		panic("unknown net intr type");
56074c7070dbSScott Long 	}
56084c7070dbSScott Long 
56094c7070dbSScott Long 	info->ifi_filter = filter;
56104c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
56114c7070dbSScott Long 	info->ifi_task = gtask;
561295246abbSSean Bruno 	info->ifi_ctx = q;
56134c7070dbSScott Long 
561495246abbSSean Bruno 	err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info,  name);
5615da69b8f9SSean Bruno 	if (err != 0) {
5616da69b8f9SSean Bruno 		device_printf(ctx->ifc_dev, "_iflib_irq_alloc failed %d\n", err);
56174c7070dbSScott Long 		return (err);
5618da69b8f9SSean Bruno 	}
5619da69b8f9SSean Bruno 	if (type == IFLIB_INTR_ADMIN)
5620da69b8f9SSean Bruno 		return (0);
5621da69b8f9SSean Bruno 
56224c7070dbSScott Long 	if (tqrid != -1) {
5623b103855eSStephen Hurd 		err = iflib_irq_set_affinity(ctx, rman_get_start(irq->ii_res), type, qid, gtask, tqg, q, name);
5624b103855eSStephen Hurd 		if (err)
5625b103855eSStephen Hurd 			return (err);
5626aa3c5dd8SSean Bruno 	} else {
56271c0054d2SStephen Hurd 		taskqgroup_attach(tqg, gtask, q, rman_get_start(irq->ii_res), name);
5628aa3c5dd8SSean Bruno 	}
56294c7070dbSScott Long 
56304c7070dbSScott Long 	return (0);
56314c7070dbSScott Long }
56324c7070dbSScott Long 
56334c7070dbSScott Long void
56341c0054d2SStephen Hurd iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,  void *arg, int qid, char *name)
56354c7070dbSScott Long {
56364c7070dbSScott Long 	struct grouptask *gtask;
56374c7070dbSScott Long 	struct taskqgroup *tqg;
563823ac9029SStephen Hurd 	gtask_fn_t *fn;
56394c7070dbSScott Long 	void *q;
56401c0054d2SStephen Hurd 	int irq_num = -1;
5641b103855eSStephen Hurd 	int err;
56424c7070dbSScott Long 
56434c7070dbSScott Long 	switch (type) {
56444c7070dbSScott Long 	case IFLIB_INTR_TX:
56454c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
56464c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
5647ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
56484c7070dbSScott Long 		fn = _task_fn_tx;
56491c0054d2SStephen Hurd 		if (irq != NULL)
56501c0054d2SStephen Hurd 			irq_num = rman_get_start(irq->ii_res);
56514c7070dbSScott Long 		break;
56524c7070dbSScott Long 	case IFLIB_INTR_RX:
56534c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
56544c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5655ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
56564c7070dbSScott Long 		fn = _task_fn_rx;
56571c0054d2SStephen Hurd 		if (irq != NULL)
56581c0054d2SStephen Hurd 			irq_num = rman_get_start(irq->ii_res);
56594c7070dbSScott Long 		break;
56604c7070dbSScott Long 	case IFLIB_INTR_IOV:
56614c7070dbSScott Long 		q = ctx;
56624c7070dbSScott Long 		gtask = &ctx->ifc_vflr_task;
5663ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
56644c7070dbSScott Long 		fn = _task_fn_iov;
56654c7070dbSScott Long 		break;
56664c7070dbSScott Long 	default:
56674c7070dbSScott Long 		panic("unknown net intr type");
56684c7070dbSScott Long 	}
56694c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
5670b103855eSStephen Hurd 	if (irq_num != -1) {
5671b103855eSStephen Hurd 		err = iflib_irq_set_affinity(ctx, irq_num, type, qid, gtask, tqg, q, name);
5672b103855eSStephen Hurd 		if (err)
56731c0054d2SStephen Hurd 			taskqgroup_attach(tqg, gtask, q, irq_num, name);
56744c7070dbSScott Long 	}
5675b103855eSStephen Hurd 	else {
5676b103855eSStephen Hurd 		taskqgroup_attach(tqg, gtask, q, irq_num, name);
5677b103855eSStephen Hurd 	}
5678b103855eSStephen Hurd }
56794c7070dbSScott Long 
56804c7070dbSScott Long void
56814c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
56824c7070dbSScott Long {
56834c7070dbSScott Long 	if (irq->ii_tag)
56844c7070dbSScott Long 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
56854c7070dbSScott Long 
56864c7070dbSScott Long 	if (irq->ii_res)
56874c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, irq->ii_rid, irq->ii_res);
56884c7070dbSScott Long }
56894c7070dbSScott Long 
56904c7070dbSScott Long static int
56914c7070dbSScott Long iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, char *name)
56924c7070dbSScott Long {
56934c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
56944c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
56954c7070dbSScott Long 	if_irq_t irq = &ctx->ifc_legacy_irq;
56964c7070dbSScott Long 	iflib_filter_info_t info;
56974c7070dbSScott Long 	struct grouptask *gtask;
56984c7070dbSScott Long 	struct taskqgroup *tqg;
569923ac9029SStephen Hurd 	gtask_fn_t *fn;
57004c7070dbSScott Long 	int tqrid;
57014c7070dbSScott Long 	void *q;
57024c7070dbSScott Long 	int err;
57034c7070dbSScott Long 
57044c7070dbSScott Long 	q = &ctx->ifc_rxqs[0];
57054c7070dbSScott Long 	info = &rxq[0].ifr_filter_info;
57064c7070dbSScott Long 	gtask = &rxq[0].ifr_task;
5707ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
57084c7070dbSScott Long 	tqrid = irq->ii_rid = *rid;
57094c7070dbSScott Long 	fn = _task_fn_rx;
57104c7070dbSScott Long 
57114c7070dbSScott Long 	ctx->ifc_flags |= IFC_LEGACY;
57124c7070dbSScott Long 	info->ifi_filter = filter;
57134c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
57144c7070dbSScott Long 	info->ifi_task = gtask;
57154ecb427aSSean Bruno 	info->ifi_ctx = ctx;
57164c7070dbSScott Long 
57174c7070dbSScott Long 	/* We allocate a single interrupt resource */
571895246abbSSean Bruno 	if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr_ctx, NULL, info, name)) != 0)
57194c7070dbSScott Long 		return (err);
57204c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
57219c58cafaSStephen Hurd 	taskqgroup_attach(tqg, gtask, q, rman_get_start(irq->ii_res), name);
57224c7070dbSScott Long 
57234c7070dbSScott Long 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
57249c58cafaSStephen Hurd 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, rman_get_start(irq->ii_res), "tx");
57254c7070dbSScott Long 	return (0);
57264c7070dbSScott Long }
57274c7070dbSScott Long 
57284c7070dbSScott Long void
57294c7070dbSScott Long iflib_led_create(if_ctx_t ctx)
57304c7070dbSScott Long {
57314c7070dbSScott Long 
57324c7070dbSScott Long 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
57334c7070dbSScott Long 	    device_get_nameunit(ctx->ifc_dev));
57344c7070dbSScott Long }
57354c7070dbSScott Long 
57364c7070dbSScott Long void
57374c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
57384c7070dbSScott Long {
57394c7070dbSScott Long 
57404c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
57414c7070dbSScott Long }
57424c7070dbSScott Long 
57434c7070dbSScott Long void
57444c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
57454c7070dbSScott Long {
57464c7070dbSScott Long 
57474c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
57484c7070dbSScott Long }
57494c7070dbSScott Long 
57504c7070dbSScott Long void
57514c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx)
57524c7070dbSScott Long {
57531248952aSSean Bruno #ifdef INVARIANTS
57541248952aSSean Bruno 	struct grouptask *gtask;
57551248952aSSean Bruno 
57561248952aSSean Bruno 	gtask = &ctx->ifc_admin_task;
5757d0d0ad0aSStephen Hurd 	MPASS(gtask != NULL && gtask->gt_taskqueue != NULL);
57581248952aSSean Bruno #endif
57594c7070dbSScott Long 
57604c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
57614c7070dbSScott Long }
57624c7070dbSScott Long 
57634c7070dbSScott Long void
57644c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx)
57654c7070dbSScott Long {
57664c7070dbSScott Long 
57674c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
57684c7070dbSScott Long }
57694c7070dbSScott Long 
57704c7070dbSScott Long void
57714c7070dbSScott Long iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name)
57724c7070dbSScott Long {
57734c7070dbSScott Long 
5774ab2e3f79SStephen Hurd 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, -1, name);
57754c7070dbSScott Long }
57764c7070dbSScott Long 
57774c7070dbSScott Long void
5778aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn,
5779aa8a24d3SStephen Hurd 	const char *name)
57804c7070dbSScott Long {
57814c7070dbSScott Long 
57824c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, ctx);
5783ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, -1, name);
57844c7070dbSScott Long }
57854c7070dbSScott Long 
57864c7070dbSScott Long void
578723ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask)
578823ac9029SStephen Hurd {
578923ac9029SStephen Hurd 
5790ab2e3f79SStephen Hurd 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
579123ac9029SStephen Hurd }
579223ac9029SStephen Hurd 
579323ac9029SStephen Hurd void
579423ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
57954c7070dbSScott Long {
57964c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
57974c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
57984c7070dbSScott Long 
57994c7070dbSScott Long 	if_setbaudrate(ifp, baudrate);
58007b610b60SSean Bruno 	if (baudrate >= IF_Gbps(10)) {
58017b610b60SSean Bruno 		STATE_LOCK(ctx);
580295246abbSSean Bruno 		ctx->ifc_flags |= IFC_PREFETCH;
58037b610b60SSean Bruno 		STATE_UNLOCK(ctx);
58047b610b60SSean Bruno 	}
58054c7070dbSScott Long 	/* If link down, disable watchdog */
58064c7070dbSScott Long 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
58074c7070dbSScott Long 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
58084c7070dbSScott Long 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
58094c7070dbSScott Long 	}
58104c7070dbSScott Long 	ctx->ifc_link_state = link_state;
58114c7070dbSScott Long 	if_link_state_change(ifp, link_state);
58124c7070dbSScott Long }
58134c7070dbSScott Long 
58144c7070dbSScott Long static int
58154c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
58164c7070dbSScott Long {
58174c7070dbSScott Long 	int credits;
58181248952aSSean Bruno #ifdef INVARIANTS
58191248952aSSean Bruno 	int credits_pre = txq->ift_cidx_processed;
58201248952aSSean Bruno #endif
58214c7070dbSScott Long 
58224c7070dbSScott Long 	if (ctx->isc_txd_credits_update == NULL)
58234c7070dbSScott Long 		return (0);
58244c7070dbSScott Long 
582595246abbSSean Bruno 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
58264c7070dbSScott Long 		return (0);
58274c7070dbSScott Long 
58284c7070dbSScott Long 	txq->ift_processed += credits;
58294c7070dbSScott Long 	txq->ift_cidx_processed += credits;
58304c7070dbSScott Long 
58311248952aSSean Bruno 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
58324c7070dbSScott Long 	if (txq->ift_cidx_processed >= txq->ift_size)
58334c7070dbSScott Long 		txq->ift_cidx_processed -= txq->ift_size;
58344c7070dbSScott Long 	return (credits);
58354c7070dbSScott Long }
58364c7070dbSScott Long 
58374c7070dbSScott Long static int
583895246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
58394c7070dbSScott Long {
58404c7070dbSScott Long 
584123ac9029SStephen Hurd 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
584223ac9029SStephen Hurd 	    budget));
58434c7070dbSScott Long }
58444c7070dbSScott Long 
58454c7070dbSScott Long void
58464c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
58474c7070dbSScott Long 	const char *description, if_int_delay_info_t info,
58484c7070dbSScott Long 	int offset, int value)
58494c7070dbSScott Long {
58504c7070dbSScott Long 	info->iidi_ctx = ctx;
58514c7070dbSScott Long 	info->iidi_offset = offset;
58524c7070dbSScott Long 	info->iidi_value = value;
58534c7070dbSScott Long 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
58544c7070dbSScott Long 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
58554c7070dbSScott Long 	    OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
58564c7070dbSScott Long 	    info, 0, iflib_sysctl_int_delay, "I", description);
58574c7070dbSScott Long }
58584c7070dbSScott Long 
5859aa8a24d3SStephen Hurd struct sx *
58604c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx)
58614c7070dbSScott Long {
58624c7070dbSScott Long 
5863aa8a24d3SStephen Hurd 	return (&ctx->ifc_ctx_sx);
58644c7070dbSScott Long }
58654c7070dbSScott Long 
58664c7070dbSScott Long static int
58674c7070dbSScott Long iflib_msix_init(if_ctx_t ctx)
58684c7070dbSScott Long {
58694c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
58704c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
58714c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
58724c7070dbSScott Long 	int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs;
58734c7070dbSScott Long 	int iflib_num_tx_queues, iflib_num_rx_queues;
58744c7070dbSScott Long 	int err, admincnt, bar;
58754c7070dbSScott Long 
5876d2735264SStephen Hurd 	iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs;
5877d2735264SStephen Hurd 	iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs;
587823ac9029SStephen Hurd 
5879d2735264SStephen Hurd 	device_printf(dev, "msix_init qsets capped at %d\n", imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets));
58801248952aSSean Bruno 
58814c7070dbSScott Long 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
58824c7070dbSScott Long 	admincnt = sctx->isc_admin_intrcnt;
58830fdea539SStephen Hurd 	/* Override by global tuneable */
58840fdea539SStephen Hurd 	{
58850fdea539SStephen Hurd 		int i;
58860fdea539SStephen Hurd 		size_t len = sizeof(i);
58870fdea539SStephen Hurd 		err = kernel_sysctlbyname(curthread, "hw.pci.enable_msix", &i, &len, NULL, 0, NULL, 0);
58880fdea539SStephen Hurd 		if (err == 0) {
58890fdea539SStephen Hurd 			if (i == 0)
58900fdea539SStephen Hurd 				goto msi;
58910fdea539SStephen Hurd 		}
58920fdea539SStephen Hurd 		else {
58930fdea539SStephen Hurd 			device_printf(dev, "unable to read hw.pci.enable_msix.");
58940fdea539SStephen Hurd 		}
58950fdea539SStephen Hurd 	}
58964c7070dbSScott Long 	/* Override by tuneable */
5897ea351d3fSSean Bruno 	if (scctx->isc_disable_msix)
58984c7070dbSScott Long 		goto msi;
58994c7070dbSScott Long 
59004c7070dbSScott Long 	/*
59014c7070dbSScott Long 	** When used in a virtualized environment
59024c7070dbSScott Long 	** PCI BUSMASTER capability may not be set
59034c7070dbSScott Long 	** so explicity set it here and rewrite
59044c7070dbSScott Long 	** the ENABLE in the MSIX control register
59054c7070dbSScott Long 	** at this point to cause the host to
59064c7070dbSScott Long 	** successfully initialize us.
59074c7070dbSScott Long 	*/
59084c7070dbSScott Long 	{
59094c7070dbSScott Long 		int msix_ctrl, rid;
59104c7070dbSScott Long 
591196eeabefSSean Bruno  		pci_enable_busmaster(dev);
5912f7ae9a84SSean Bruno 		rid = 0;
5913f7ae9a84SSean Bruno 		if (pci_find_cap(dev, PCIY_MSIX, &rid) == 0 && rid != 0) {
59144c7070dbSScott Long 			rid += PCIR_MSIX_CTRL;
59154c7070dbSScott Long 			msix_ctrl = pci_read_config(dev, rid, 2);
59164c7070dbSScott Long 			msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
59174c7070dbSScott Long 			pci_write_config(dev, rid, msix_ctrl, 2);
5918f7ae9a84SSean Bruno 		} else {
5919f7ae9a84SSean Bruno 			device_printf(dev, "PCIY_MSIX capability not found; "
5920f7ae9a84SSean Bruno 			                   "or rid %d == 0.\n", rid);
5921f7ae9a84SSean Bruno 			goto msi;
5922f7ae9a84SSean Bruno 		}
59234c7070dbSScott Long 	}
59244c7070dbSScott Long 
59254c7070dbSScott Long 	/*
59264c7070dbSScott Long 	 * bar == -1 => "trust me I know what I'm doing"
59274c7070dbSScott Long 	 * Some drivers are for hardware that is so shoddily
59284c7070dbSScott Long 	 * documented that no one knows which bars are which
59294c7070dbSScott Long 	 * so the developer has to map all bars. This hack
59304c7070dbSScott Long 	 * allows shoddy garbage to use msix in this framework.
59314c7070dbSScott Long 	 */
59324c7070dbSScott Long 	if (bar != -1) {
59334c7070dbSScott Long 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
59344c7070dbSScott Long 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
59354c7070dbSScott Long 		if (ctx->ifc_msix_mem == NULL) {
59364c7070dbSScott Long 			/* May not be enabled */
59374c7070dbSScott Long 			device_printf(dev, "Unable to map MSIX table \n");
59384c7070dbSScott Long 			goto msi;
59394c7070dbSScott Long 		}
59404c7070dbSScott Long 	}
59414c7070dbSScott Long 	/* First try MSI/X */
59424c7070dbSScott Long 	if ((msgs = pci_msix_count(dev)) == 0) { /* system has msix disabled */
59434c7070dbSScott Long 		device_printf(dev, "System has MSIX disabled \n");
59444c7070dbSScott Long 		bus_release_resource(dev, SYS_RES_MEMORY,
59454c7070dbSScott Long 		    bar, ctx->ifc_msix_mem);
59464c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
59474c7070dbSScott Long 		goto msi;
59484c7070dbSScott Long 	}
59494c7070dbSScott Long #if IFLIB_DEBUG
59504c7070dbSScott Long 	/* use only 1 qset in debug mode */
59514c7070dbSScott Long 	queuemsgs = min(msgs - admincnt, 1);
59524c7070dbSScott Long #else
59534c7070dbSScott Long 	queuemsgs = msgs - admincnt;
59544c7070dbSScott Long #endif
59554c7070dbSScott Long #ifdef RSS
59564c7070dbSScott Long 	queues = imin(queuemsgs, rss_getnumbuckets());
59574c7070dbSScott Long #else
59584c7070dbSScott Long 	queues = queuemsgs;
59594c7070dbSScott Long #endif
59604c7070dbSScott Long 	queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
59614c7070dbSScott Long 	device_printf(dev, "pxm cpus: %d queue msgs: %d admincnt: %d\n",
59624c7070dbSScott Long 				  CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
59634c7070dbSScott Long #ifdef  RSS
59644c7070dbSScott Long 	/* If we're doing RSS, clamp at the number of RSS buckets */
59654c7070dbSScott Long 	if (queues > rss_getnumbuckets())
59664c7070dbSScott Long 		queues = rss_getnumbuckets();
59674c7070dbSScott Long #endif
596823ac9029SStephen Hurd 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
596923ac9029SStephen Hurd 		rx_queues = iflib_num_rx_queues;
59704c7070dbSScott Long 	else
59714c7070dbSScott Long 		rx_queues = queues;
5972d2735264SStephen Hurd 
5973d2735264SStephen Hurd 	if (rx_queues > scctx->isc_nrxqsets)
5974d2735264SStephen Hurd 		rx_queues = scctx->isc_nrxqsets;
5975d2735264SStephen Hurd 
597623ac9029SStephen Hurd 	/*
597723ac9029SStephen Hurd 	 * We want this to be all logical CPUs by default
597823ac9029SStephen Hurd 	 */
59794c7070dbSScott Long 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
59804c7070dbSScott Long 		tx_queues = iflib_num_tx_queues;
59814c7070dbSScott Long 	else
598223ac9029SStephen Hurd 		tx_queues = mp_ncpus;
598323ac9029SStephen Hurd 
5984d2735264SStephen Hurd 	if (tx_queues > scctx->isc_ntxqsets)
5985d2735264SStephen Hurd 		tx_queues = scctx->isc_ntxqsets;
5986d2735264SStephen Hurd 
598723ac9029SStephen Hurd 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
598823ac9029SStephen Hurd #ifdef INVARIANTS
598923ac9029SStephen Hurd 		if (tx_queues != rx_queues)
599023ac9029SStephen Hurd 			device_printf(dev, "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
599123ac9029SStephen Hurd 				      min(rx_queues, tx_queues), min(rx_queues, tx_queues));
599223ac9029SStephen Hurd #endif
599323ac9029SStephen Hurd 		tx_queues = min(rx_queues, tx_queues);
599423ac9029SStephen Hurd 		rx_queues = min(rx_queues, tx_queues);
599523ac9029SStephen Hurd 	}
59964c7070dbSScott Long 
5997ab2e3f79SStephen Hurd 	device_printf(dev, "using %d rx queues %d tx queues \n", rx_queues, tx_queues);
59984c7070dbSScott Long 
5999ab2e3f79SStephen Hurd 	vectors = rx_queues + admincnt;
60004c7070dbSScott Long 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
60014c7070dbSScott Long 		device_printf(dev,
60024c7070dbSScott Long 					  "Using MSIX interrupts with %d vectors\n", vectors);
60034c7070dbSScott Long 		scctx->isc_vectors = vectors;
60044c7070dbSScott Long 		scctx->isc_nrxqsets = rx_queues;
60054c7070dbSScott Long 		scctx->isc_ntxqsets = tx_queues;
60064c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSIX;
600723ac9029SStephen Hurd 
60084c7070dbSScott Long 		return (vectors);
60094c7070dbSScott Long 	} else {
60104c7070dbSScott Long 		device_printf(dev, "failed to allocate %d msix vectors, err: %d - using MSI\n", vectors, err);
60114c7070dbSScott Long 	}
60124c7070dbSScott Long msi:
60134c7070dbSScott Long 	vectors = pci_msi_count(dev);
60144c7070dbSScott Long 	scctx->isc_nrxqsets = 1;
60154c7070dbSScott Long 	scctx->isc_ntxqsets = 1;
60164c7070dbSScott Long 	scctx->isc_vectors = vectors;
60174c7070dbSScott Long 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
60184c7070dbSScott Long 		device_printf(dev,"Using an MSI interrupt\n");
60194c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSI;
60204c7070dbSScott Long 	} else {
60214c7070dbSScott Long 		device_printf(dev,"Using a Legacy interrupt\n");
60224c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
60234c7070dbSScott Long 	}
60244c7070dbSScott Long 
60254c7070dbSScott Long 	return (vectors);
60264c7070dbSScott Long }
60274c7070dbSScott Long 
60284c7070dbSScott Long char * ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
60294c7070dbSScott Long 
60304c7070dbSScott Long static int
60314c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
60324c7070dbSScott Long {
60334c7070dbSScott Long 	int rc;
60344c7070dbSScott Long 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
60354c7070dbSScott Long 	struct sbuf *sb;
60364c7070dbSScott Long 	char *ring_state = "UNKNOWN";
60374c7070dbSScott Long 
60384c7070dbSScott Long 	/* XXX needed ? */
60394c7070dbSScott Long 	rc = sysctl_wire_old_buffer(req, 0);
60404c7070dbSScott Long 	MPASS(rc == 0);
60414c7070dbSScott Long 	if (rc != 0)
60424c7070dbSScott Long 		return (rc);
60434c7070dbSScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
60444c7070dbSScott Long 	MPASS(sb != NULL);
60454c7070dbSScott Long 	if (sb == NULL)
60464c7070dbSScott Long 		return (ENOMEM);
60474c7070dbSScott Long 	if (state[3] <= 3)
60484c7070dbSScott Long 		ring_state = ring_states[state[3]];
60494c7070dbSScott Long 
60504c7070dbSScott Long 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
60514c7070dbSScott Long 		    state[0], state[1], state[2], ring_state);
60524c7070dbSScott Long 	rc = sbuf_finish(sb);
60534c7070dbSScott Long 	sbuf_delete(sb);
60544c7070dbSScott Long         return(rc);
60554c7070dbSScott Long }
60564c7070dbSScott Long 
605723ac9029SStephen Hurd enum iflib_ndesc_handler {
605823ac9029SStephen Hurd 	IFLIB_NTXD_HANDLER,
605923ac9029SStephen Hurd 	IFLIB_NRXD_HANDLER,
606023ac9029SStephen Hurd };
60614c7070dbSScott Long 
606223ac9029SStephen Hurd static int
606323ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
606423ac9029SStephen Hurd {
606523ac9029SStephen Hurd 	if_ctx_t ctx = (void *)arg1;
606623ac9029SStephen Hurd 	enum iflib_ndesc_handler type = arg2;
606723ac9029SStephen Hurd 	char buf[256] = {0};
606895246abbSSean Bruno 	qidx_t *ndesc;
606923ac9029SStephen Hurd 	char *p, *next;
607023ac9029SStephen Hurd 	int nqs, rc, i;
607123ac9029SStephen Hurd 
607223ac9029SStephen Hurd 	MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER);
607323ac9029SStephen Hurd 
607423ac9029SStephen Hurd 	nqs = 8;
607523ac9029SStephen Hurd 	switch(type) {
607623ac9029SStephen Hurd 	case IFLIB_NTXD_HANDLER:
607723ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_ntxds;
607823ac9029SStephen Hurd 		if (ctx->ifc_sctx)
607923ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_ntxqs;
608023ac9029SStephen Hurd 		break;
608123ac9029SStephen Hurd 	case IFLIB_NRXD_HANDLER:
608223ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_nrxds;
608323ac9029SStephen Hurd 		if (ctx->ifc_sctx)
608423ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_nrxqs;
608523ac9029SStephen Hurd 		break;
60861ae4848cSMatt Macy 	default:
60871ae4848cSMatt Macy 			panic("unhandled type");
608823ac9029SStephen Hurd 	}
608923ac9029SStephen Hurd 	if (nqs == 0)
609023ac9029SStephen Hurd 		nqs = 8;
609123ac9029SStephen Hurd 
609223ac9029SStephen Hurd 	for (i=0; i<8; i++) {
609323ac9029SStephen Hurd 		if (i >= nqs)
609423ac9029SStephen Hurd 			break;
609523ac9029SStephen Hurd 		if (i)
609623ac9029SStephen Hurd 			strcat(buf, ",");
609723ac9029SStephen Hurd 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
609823ac9029SStephen Hurd 	}
609923ac9029SStephen Hurd 
610023ac9029SStephen Hurd 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
610123ac9029SStephen Hurd 	if (rc || req->newptr == NULL)
610223ac9029SStephen Hurd 		return rc;
610323ac9029SStephen Hurd 
610423ac9029SStephen Hurd 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
610523ac9029SStephen Hurd 	    i++, p = strsep(&next, " ,")) {
610623ac9029SStephen Hurd 		ndesc[i] = strtoul(p, NULL, 10);
610723ac9029SStephen Hurd 	}
610823ac9029SStephen Hurd 
610923ac9029SStephen Hurd 	return(rc);
611023ac9029SStephen Hurd }
61114c7070dbSScott Long 
61124c7070dbSScott Long #define NAME_BUFLEN 32
61134c7070dbSScott Long static void
61144c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx)
61154c7070dbSScott Long {
61164c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
61174c7070dbSScott Long 	struct sysctl_oid_list *child, *oid_list;
61184c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
61194c7070dbSScott Long 	struct sysctl_oid *node;
61204c7070dbSScott Long 
61214c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
61224c7070dbSScott Long 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
61234c7070dbSScott Long 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
61244c7070dbSScott Long 						      CTLFLAG_RD, NULL, "IFLIB fields");
61254c7070dbSScott Long 	oid_list = SYSCTL_CHILDREN(node);
61264c7070dbSScott Long 
612723ac9029SStephen Hurd 	SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
612823ac9029SStephen Hurd 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
612923ac9029SStephen Hurd 		       "driver version");
613023ac9029SStephen Hurd 
61314c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
61324c7070dbSScott Long 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
61334c7070dbSScott Long 			"# of txqs to use, 0 => use default #");
61344c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
613523ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
613623ac9029SStephen Hurd 			"# of rxqs to use, 0 => use default #");
613723ac9029SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
613823ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
613923ac9029SStephen Hurd                        "permit #txq != #rxq");
6140ea351d3fSSean Bruno 	SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix",
6141ea351d3fSSean Bruno                       CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
6142ea351d3fSSean Bruno                       "disable MSIX (default 0)");
6143f4d2154eSStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget",
6144f4d2154eSStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0,
6145f4d2154eSStephen Hurd                        "set the rx budget");
61464c7070dbSScott Long 
614723ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
614823ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
614923ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER,
615023ac9029SStephen Hurd                        mp_ndesc_handler, "A",
615123ac9029SStephen Hurd                        "list of # of tx descriptors to use, 0 = use default #");
615223ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
615323ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER,
615423ac9029SStephen Hurd                        mp_ndesc_handler, "A",
615523ac9029SStephen Hurd                        "list of # of rx descriptors to use, 0 = use default #");
61564c7070dbSScott Long }
61574c7070dbSScott Long 
61584c7070dbSScott Long static void
61594c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx)
61604c7070dbSScott Long {
61614c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
61624c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
61634c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
61644c7070dbSScott Long 	struct sysctl_oid_list *child;
61654c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
61664c7070dbSScott Long 	iflib_fl_t fl;
61674c7070dbSScott Long 	iflib_txq_t txq;
61684c7070dbSScott Long 	iflib_rxq_t rxq;
61694c7070dbSScott Long 	int i, j;
61704c7070dbSScott Long 	char namebuf[NAME_BUFLEN];
61714c7070dbSScott Long 	char *qfmt;
61724c7070dbSScott Long 	struct sysctl_oid *queue_node, *fl_node, *node;
61734c7070dbSScott Long 	struct sysctl_oid_list *queue_list, *fl_list;
61744c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
61754c7070dbSScott Long 
61764c7070dbSScott Long 	node = ctx->ifc_sysctl_node;
61774c7070dbSScott Long 	child = SYSCTL_CHILDREN(node);
61784c7070dbSScott Long 
61794c7070dbSScott Long 	if (scctx->isc_ntxqsets > 100)
61804c7070dbSScott Long 		qfmt = "txq%03d";
61814c7070dbSScott Long 	else if (scctx->isc_ntxqsets > 10)
61824c7070dbSScott Long 		qfmt = "txq%02d";
61834c7070dbSScott Long 	else
61844c7070dbSScott Long 		qfmt = "txq%d";
61854c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
61864c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
61874c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
61884c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
61894c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
61904c7070dbSScott Long #if MEMORY_LOGGING
61914c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
61924c7070dbSScott Long 				CTLFLAG_RD,
61934c7070dbSScott Long 				&txq->ift_dequeued, "total mbufs freed");
61944c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
61954c7070dbSScott Long 				CTLFLAG_RD,
61964c7070dbSScott Long 				&txq->ift_enqueued, "total mbufs enqueued");
61974c7070dbSScott Long #endif
61984c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
61994c7070dbSScott Long 				   CTLFLAG_RD,
62004c7070dbSScott Long 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
62014c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
62024c7070dbSScott Long 				   CTLFLAG_RD,
62034c7070dbSScott Long 				   &txq->ift_pullups, "# of times m_pullup was called");
62044c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
62054c7070dbSScott Long 				   CTLFLAG_RD,
62064c7070dbSScott Long 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
62074c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
62084c7070dbSScott Long 				   CTLFLAG_RD,
620923ac9029SStephen Hurd 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
62104c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
62114c7070dbSScott Long 				   CTLFLAG_RD,
62124c7070dbSScott Long 				   &txq->ift_map_failed, "# of times dma map failed");
62134c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
62144c7070dbSScott Long 				   CTLFLAG_RD,
62154c7070dbSScott Long 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
62164c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
62174c7070dbSScott Long 				   CTLFLAG_RD,
62184c7070dbSScott Long 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
62194c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
62204c7070dbSScott Long 				   CTLFLAG_RD,
62214c7070dbSScott Long 				   &txq->ift_pidx, 1, "Producer Index");
62224c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
62234c7070dbSScott Long 				   CTLFLAG_RD,
62244c7070dbSScott Long 				   &txq->ift_cidx, 1, "Consumer Index");
62254c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
62264c7070dbSScott Long 				   CTLFLAG_RD,
62274c7070dbSScott Long 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
62284c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
62294c7070dbSScott Long 				   CTLFLAG_RD,
62304c7070dbSScott Long 				   &txq->ift_in_use, 1, "descriptors in use");
62314c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
62324c7070dbSScott Long 				   CTLFLAG_RD,
62334c7070dbSScott Long 				   &txq->ift_processed, "descriptors procesed for clean");
62344c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
62354c7070dbSScott Long 				   CTLFLAG_RD,
62364c7070dbSScott Long 				   &txq->ift_cleaned, "total cleaned");
62374c7070dbSScott Long 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
623895246abbSSean Bruno 				CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br->state),
62394c7070dbSScott Long 				0, mp_ring_state_handler, "A", "soft ring state");
62404c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
624195246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->enqueues,
62424c7070dbSScott Long 				       "# of enqueues to the mp_ring for this queue");
62434c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
624495246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->drops,
62454c7070dbSScott Long 				       "# of drops in the mp_ring for this queue");
62464c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
624795246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->starts,
62484c7070dbSScott Long 				       "# of normal consumer starts in the mp_ring for this queue");
62494c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
625095246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->stalls,
62514c7070dbSScott Long 					       "# of consumer stalls in the mp_ring for this queue");
62524c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
625395246abbSSean Bruno 			       CTLFLAG_RD, &txq->ift_br->restarts,
62544c7070dbSScott Long 				       "# of consumer restarts in the mp_ring for this queue");
62554c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
625695246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->abdications,
62574c7070dbSScott Long 				       "# of consumer abdications in the mp_ring for this queue");
62584c7070dbSScott Long 	}
62594c7070dbSScott Long 
62604c7070dbSScott Long 	if (scctx->isc_nrxqsets > 100)
62614c7070dbSScott Long 		qfmt = "rxq%03d";
62624c7070dbSScott Long 	else if (scctx->isc_nrxqsets > 10)
62634c7070dbSScott Long 		qfmt = "rxq%02d";
62644c7070dbSScott Long 	else
62654c7070dbSScott Long 		qfmt = "rxq%d";
62664c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
62674c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
62684c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
62694c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
62704c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
627123ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
62724c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx",
62734c7070dbSScott Long 				       CTLFLAG_RD,
62744c7070dbSScott Long 				       &rxq->ifr_cq_pidx, 1, "Producer Index");
62754c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
62764c7070dbSScott Long 				       CTLFLAG_RD,
62774c7070dbSScott Long 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
62784c7070dbSScott Long 		}
6279da69b8f9SSean Bruno 
62804c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
62814c7070dbSScott Long 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
62824c7070dbSScott Long 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
62834c7070dbSScott Long 						     CTLFLAG_RD, NULL, "freelist Name");
62844c7070dbSScott Long 			fl_list = SYSCTL_CHILDREN(fl_node);
62854c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
62864c7070dbSScott Long 				       CTLFLAG_RD,
62874c7070dbSScott Long 				       &fl->ifl_pidx, 1, "Producer Index");
62884c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
62894c7070dbSScott Long 				       CTLFLAG_RD,
62904c7070dbSScott Long 				       &fl->ifl_cidx, 1, "Consumer Index");
62914c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
62924c7070dbSScott Long 				       CTLFLAG_RD,
62934c7070dbSScott Long 				       &fl->ifl_credits, 1, "credits available");
62944c7070dbSScott Long #if MEMORY_LOGGING
62954c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
62964c7070dbSScott Long 					CTLFLAG_RD,
62974c7070dbSScott Long 					&fl->ifl_m_enqueued, "mbufs allocated");
62984c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
62994c7070dbSScott Long 					CTLFLAG_RD,
63004c7070dbSScott Long 					&fl->ifl_m_dequeued, "mbufs freed");
63014c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
63024c7070dbSScott Long 					CTLFLAG_RD,
63034c7070dbSScott Long 					&fl->ifl_cl_enqueued, "clusters allocated");
63044c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
63054c7070dbSScott Long 					CTLFLAG_RD,
63064c7070dbSScott Long 					&fl->ifl_cl_dequeued, "clusters freed");
63074c7070dbSScott Long #endif
63084c7070dbSScott Long 
63094c7070dbSScott Long 		}
63104c7070dbSScott Long 	}
63114c7070dbSScott Long 
63124c7070dbSScott Long }
631395246abbSSean Bruno 
631495246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
631595246abbSSean Bruno static struct mbuf *
631695246abbSSean Bruno iflib_fixup_rx(struct mbuf *m)
631795246abbSSean Bruno {
631895246abbSSean Bruno 	struct mbuf *n;
631995246abbSSean Bruno 
632095246abbSSean Bruno 	if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
632195246abbSSean Bruno 		bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
632295246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
632395246abbSSean Bruno 		n = m;
632495246abbSSean Bruno 	} else {
632595246abbSSean Bruno 		MGETHDR(n, M_NOWAIT, MT_DATA);
632695246abbSSean Bruno 		if (n == NULL) {
632795246abbSSean Bruno 			m_freem(m);
632895246abbSSean Bruno 			return (NULL);
632995246abbSSean Bruno 		}
633095246abbSSean Bruno 		bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
633195246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
633295246abbSSean Bruno 		m->m_len -= ETHER_HDR_LEN;
633395246abbSSean Bruno 		n->m_len = ETHER_HDR_LEN;
633495246abbSSean Bruno 		M_MOVE_PKTHDR(n, m);
633595246abbSSean Bruno 		n->m_next = m;
633695246abbSSean Bruno 	}
633795246abbSSean Bruno 	return (n);
633895246abbSSean Bruno }
633995246abbSSean Bruno #endif
634094618825SMark Johnston 
634194618825SMark Johnston #ifdef NETDUMP
634294618825SMark Johnston static void
634394618825SMark Johnston iflib_netdump_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize)
634494618825SMark Johnston {
634594618825SMark Johnston 	if_ctx_t ctx;
634694618825SMark Johnston 
634794618825SMark Johnston 	ctx = if_getsoftc(ifp);
634894618825SMark Johnston 	CTX_LOCK(ctx);
634994618825SMark Johnston 	*nrxr = NRXQSETS(ctx);
635094618825SMark Johnston 	*ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size;
635194618825SMark Johnston 	*clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size;
635294618825SMark Johnston 	CTX_UNLOCK(ctx);
635394618825SMark Johnston }
635494618825SMark Johnston 
635594618825SMark Johnston static void
635694618825SMark Johnston iflib_netdump_event(struct ifnet *ifp, enum netdump_ev event)
635794618825SMark Johnston {
635894618825SMark Johnston 	if_ctx_t ctx;
635994618825SMark Johnston 	if_softc_ctx_t scctx;
636094618825SMark Johnston 	iflib_fl_t fl;
636194618825SMark Johnston 	iflib_rxq_t rxq;
636294618825SMark Johnston 	int i, j;
636394618825SMark Johnston 
636494618825SMark Johnston 	ctx = if_getsoftc(ifp);
636594618825SMark Johnston 	scctx = &ctx->ifc_softc_ctx;
636694618825SMark Johnston 
636794618825SMark Johnston 	switch (event) {
636894618825SMark Johnston 	case NETDUMP_START:
636994618825SMark Johnston 		for (i = 0; i < scctx->isc_nrxqsets; i++) {
637094618825SMark Johnston 			rxq = &ctx->ifc_rxqs[i];
637194618825SMark Johnston 			for (j = 0; j < rxq->ifr_nfl; j++) {
637294618825SMark Johnston 				fl = rxq->ifr_fl;
637394618825SMark Johnston 				fl->ifl_zone = m_getzone(fl->ifl_buf_size);
637494618825SMark Johnston 			}
637594618825SMark Johnston 		}
637694618825SMark Johnston 		iflib_no_tx_batch = 1;
637794618825SMark Johnston 		break;
637894618825SMark Johnston 	default:
637994618825SMark Johnston 		break;
638094618825SMark Johnston 	}
638194618825SMark Johnston }
638294618825SMark Johnston 
638394618825SMark Johnston static int
638494618825SMark Johnston iflib_netdump_transmit(struct ifnet *ifp, struct mbuf *m)
638594618825SMark Johnston {
638694618825SMark Johnston 	if_ctx_t ctx;
638794618825SMark Johnston 	iflib_txq_t txq;
638894618825SMark Johnston 	int error;
638994618825SMark Johnston 
639094618825SMark Johnston 	ctx = if_getsoftc(ifp);
639194618825SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
639294618825SMark Johnston 	    IFF_DRV_RUNNING)
639394618825SMark Johnston 		return (EBUSY);
639494618825SMark Johnston 
639594618825SMark Johnston 	txq = &ctx->ifc_txqs[0];
639694618825SMark Johnston 	error = iflib_encap(txq, &m);
639794618825SMark Johnston 	if (error == 0)
639894618825SMark Johnston 		(void)iflib_txd_db_check(ctx, txq, true, txq->ift_in_use);
639994618825SMark Johnston 	return (error);
640094618825SMark Johnston }
640194618825SMark Johnston 
640294618825SMark Johnston static int
640394618825SMark Johnston iflib_netdump_poll(struct ifnet *ifp, int count)
640494618825SMark Johnston {
640594618825SMark Johnston 	if_ctx_t ctx;
640694618825SMark Johnston 	if_softc_ctx_t scctx;
640794618825SMark Johnston 	iflib_txq_t txq;
640894618825SMark Johnston 	int i;
640994618825SMark Johnston 
641094618825SMark Johnston 	ctx = if_getsoftc(ifp);
641194618825SMark Johnston 	scctx = &ctx->ifc_softc_ctx;
641294618825SMark Johnston 
641394618825SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
641494618825SMark Johnston 	    IFF_DRV_RUNNING)
641594618825SMark Johnston 		return (EBUSY);
641694618825SMark Johnston 
641794618825SMark Johnston 	txq = &ctx->ifc_txqs[0];
641894618825SMark Johnston 	(void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
641994618825SMark Johnston 
642094618825SMark Johnston 	for (i = 0; i < scctx->isc_nrxqsets; i++)
642194618825SMark Johnston 		(void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */);
642294618825SMark Johnston 	return (0);
642394618825SMark Johnston }
642494618825SMark Johnston #endif /* NETDUMP */
6425