xref: /freebsd/sys/net/iflib.c (revision b89827a052e7f5f1f29895529ad813b354620bdc)
14c7070dbSScott Long /*-
27b610b60SSean Bruno  * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io>
34c7070dbSScott Long  * All rights reserved.
44c7070dbSScott Long  *
54c7070dbSScott Long  * Redistribution and use in source and binary forms, with or without
64c7070dbSScott Long  * modification, are permitted provided that the following conditions are met:
74c7070dbSScott Long  *
84c7070dbSScott Long  *  1. Redistributions of source code must retain the above copyright notice,
94c7070dbSScott Long  *     this list of conditions and the following disclaimer.
104c7070dbSScott Long  *
114c7070dbSScott Long  *  2. Neither the name of Matthew Macy nor the names of its
124c7070dbSScott Long  *     contributors may be used to endorse or promote products derived from
134c7070dbSScott Long  *     this software without specific prior written permission.
144c7070dbSScott Long  *
154c7070dbSScott Long  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
164c7070dbSScott Long  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174c7070dbSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184c7070dbSScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
194c7070dbSScott Long  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
204c7070dbSScott Long  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
214c7070dbSScott Long  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
224c7070dbSScott Long  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
234c7070dbSScott Long  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
244c7070dbSScott Long  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
254c7070dbSScott Long  * POSSIBILITY OF SUCH DAMAGE.
264c7070dbSScott Long  */
274c7070dbSScott Long 
284c7070dbSScott Long #include <sys/cdefs.h>
294c7070dbSScott Long __FBSDID("$FreeBSD$");
304c7070dbSScott Long 
31aaeb188aSBjoern A. Zeeb #include "opt_inet.h"
32aaeb188aSBjoern A. Zeeb #include "opt_inet6.h"
33aaeb188aSBjoern A. Zeeb #include "opt_acpi.h"
34b103855eSStephen Hurd #include "opt_sched.h"
35aaeb188aSBjoern A. Zeeb 
364c7070dbSScott Long #include <sys/param.h>
374c7070dbSScott Long #include <sys/types.h>
384c7070dbSScott Long #include <sys/bus.h>
394c7070dbSScott Long #include <sys/eventhandler.h>
404c7070dbSScott Long #include <sys/sockio.h>
414c7070dbSScott Long #include <sys/kernel.h>
424c7070dbSScott Long #include <sys/lock.h>
434c7070dbSScott Long #include <sys/mutex.h>
444c7070dbSScott Long #include <sys/module.h>
454c7070dbSScott Long #include <sys/kobj.h>
464c7070dbSScott Long #include <sys/rman.h>
474c7070dbSScott Long #include <sys/sbuf.h>
484c7070dbSScott Long #include <sys/smp.h>
494c7070dbSScott Long #include <sys/socket.h>
504c7070dbSScott Long #include <sys/sysctl.h>
514c7070dbSScott Long #include <sys/syslog.h>
524c7070dbSScott Long #include <sys/taskqueue.h>
5323ac9029SStephen Hurd #include <sys/limits.h>
544c7070dbSScott Long 
55ab2e3f79SStephen Hurd 
564c7070dbSScott Long #include <net/if.h>
574c7070dbSScott Long #include <net/if_var.h>
584c7070dbSScott Long #include <net/if_types.h>
594c7070dbSScott Long #include <net/if_media.h>
604c7070dbSScott Long #include <net/bpf.h>
614c7070dbSScott Long #include <net/ethernet.h>
624c7070dbSScott Long #include <net/mp_ring.h>
6335e4e998SStephen Hurd #include <net/vnet.h>
644c7070dbSScott Long 
654c7070dbSScott Long #include <netinet/in.h>
664c7070dbSScott Long #include <netinet/in_pcb.h>
674c7070dbSScott Long #include <netinet/tcp_lro.h>
684c7070dbSScott Long #include <netinet/in_systm.h>
694c7070dbSScott Long #include <netinet/if_ether.h>
704c7070dbSScott Long #include <netinet/ip.h>
714c7070dbSScott Long #include <netinet/ip6.h>
724c7070dbSScott Long #include <netinet/tcp.h>
7335e4e998SStephen Hurd #include <netinet/ip_var.h>
7435e4e998SStephen Hurd #include <netinet6/ip6_var.h>
754c7070dbSScott Long 
764c7070dbSScott Long #include <machine/bus.h>
774c7070dbSScott Long #include <machine/in_cksum.h>
784c7070dbSScott Long 
794c7070dbSScott Long #include <vm/vm.h>
804c7070dbSScott Long #include <vm/pmap.h>
814c7070dbSScott Long 
824c7070dbSScott Long #include <dev/led/led.h>
834c7070dbSScott Long #include <dev/pci/pcireg.h>
844c7070dbSScott Long #include <dev/pci/pcivar.h>
854c7070dbSScott Long #include <dev/pci/pci_private.h>
864c7070dbSScott Long 
874c7070dbSScott Long #include <net/iflib.h>
884c7070dbSScott Long 
894c7070dbSScott Long #include "ifdi_if.h"
904c7070dbSScott Long 
914c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
924c7070dbSScott Long #include <sys/memdesc.h>
934c7070dbSScott Long #include <machine/bus.h>
944c7070dbSScott Long #include <machine/md_var.h>
954c7070dbSScott Long #include <machine/specialreg.h>
964c7070dbSScott Long #include <x86/include/busdma_impl.h>
974c7070dbSScott Long #include <x86/iommu/busdma_dmar.h>
984c7070dbSScott Long #endif
994c7070dbSScott Long 
10087890dbaSSean Bruno #include <sys/bitstring.h>
1014c7070dbSScott Long /*
10295246abbSSean Bruno  * enable accounting of every mbuf as it comes in to and goes out of
10395246abbSSean Bruno  * iflib's software descriptor references
1044c7070dbSScott Long  */
1054c7070dbSScott Long #define MEMORY_LOGGING 0
1064c7070dbSScott Long /*
1074c7070dbSScott Long  * Enable mbuf vectors for compressing long mbuf chains
1084c7070dbSScott Long  */
1094c7070dbSScott Long 
1104c7070dbSScott Long /*
1114c7070dbSScott Long  * NB:
1124c7070dbSScott Long  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
1134c7070dbSScott Long  *   we prefetch needs to be determined by the time spent in m_free vis a vis
1144c7070dbSScott Long  *   the cost of a prefetch. This will of course vary based on the workload:
1154c7070dbSScott Long  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
1164c7070dbSScott Long  *        is quite expensive, thus suggesting very little prefetch.
1174c7070dbSScott Long  *      - small packet forwarding which is just returning a single mbuf to
1184c7070dbSScott Long  *        UMA will typically be very fast vis a vis the cost of a memory
1194c7070dbSScott Long  *        access.
1204c7070dbSScott Long  */
1214c7070dbSScott Long 
1224c7070dbSScott Long 
1234c7070dbSScott Long /*
1244c7070dbSScott Long  * File organization:
1254c7070dbSScott Long  *  - private structures
1264c7070dbSScott Long  *  - iflib private utility functions
1274c7070dbSScott Long  *  - ifnet functions
1284c7070dbSScott Long  *  - vlan registry and other exported functions
1294c7070dbSScott Long  *  - iflib public core functions
1304c7070dbSScott Long  *
1314c7070dbSScott Long  *
1324c7070dbSScott Long  */
1334c7070dbSScott Long static MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
1344c7070dbSScott Long 
1354c7070dbSScott Long struct iflib_txq;
1364c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t;
1374c7070dbSScott Long struct iflib_rxq;
1384c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t;
1394c7070dbSScott Long struct iflib_fl;
1404c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t;
1414c7070dbSScott Long 
1424ecb427aSSean Bruno struct iflib_ctx;
1434ecb427aSSean Bruno 
1442d873474SStephen Hurd static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid);
1452d873474SStephen Hurd 
1464c7070dbSScott Long typedef struct iflib_filter_info {
1474c7070dbSScott Long 	driver_filter_t *ifi_filter;
1484c7070dbSScott Long 	void *ifi_filter_arg;
1494c7070dbSScott Long 	struct grouptask *ifi_task;
15095246abbSSean Bruno 	void *ifi_ctx;
1514c7070dbSScott Long } *iflib_filter_info_t;
1524c7070dbSScott Long 
1534c7070dbSScott Long struct iflib_ctx {
1544c7070dbSScott Long 	KOBJ_FIELDS;
1554c7070dbSScott Long    /*
1564c7070dbSScott Long    * Pointer to hardware driver's softc
1574c7070dbSScott Long    */
1584c7070dbSScott Long 	void *ifc_softc;
1594c7070dbSScott Long 	device_t ifc_dev;
1604c7070dbSScott Long 	if_t ifc_ifp;
1614c7070dbSScott Long 
1624c7070dbSScott Long 	cpuset_t ifc_cpus;
1634c7070dbSScott Long 	if_shared_ctx_t ifc_sctx;
1644c7070dbSScott Long 	struct if_softc_ctx ifc_softc_ctx;
1654c7070dbSScott Long 
166aa8a24d3SStephen Hurd 	struct sx ifc_ctx_sx;
1677b610b60SSean Bruno 	struct mtx ifc_state_mtx;
1684c7070dbSScott Long 
1694c7070dbSScott Long 	uint16_t ifc_nhwtxqs;
1704c7070dbSScott Long 
1714c7070dbSScott Long 	iflib_txq_t ifc_txqs;
1724c7070dbSScott Long 	iflib_rxq_t ifc_rxqs;
1734c7070dbSScott Long 	uint32_t ifc_if_flags;
1744c7070dbSScott Long 	uint32_t ifc_flags;
1754c7070dbSScott Long 	uint32_t ifc_max_fl_buf_size;
1764c7070dbSScott Long 	int ifc_in_detach;
1774c7070dbSScott Long 
1784c7070dbSScott Long 	int ifc_link_state;
1794c7070dbSScott Long 	int ifc_link_irq;
1804c7070dbSScott Long 	int ifc_watchdog_events;
1814c7070dbSScott Long 	struct cdev *ifc_led_dev;
1824c7070dbSScott Long 	struct resource *ifc_msix_mem;
1834c7070dbSScott Long 
1844c7070dbSScott Long 	struct if_irq ifc_legacy_irq;
1854c7070dbSScott Long 	struct grouptask ifc_admin_task;
1864c7070dbSScott Long 	struct grouptask ifc_vflr_task;
1874c7070dbSScott Long 	struct iflib_filter_info ifc_filter_info;
1884c7070dbSScott Long 	struct ifmedia	ifc_media;
1894c7070dbSScott Long 
1904c7070dbSScott Long 	struct sysctl_oid *ifc_sysctl_node;
1914c7070dbSScott Long 	uint16_t ifc_sysctl_ntxqs;
1924c7070dbSScott Long 	uint16_t ifc_sysctl_nrxqs;
19323ac9029SStephen Hurd 	uint16_t ifc_sysctl_qs_eq_override;
194f4d2154eSStephen Hurd 	uint16_t ifc_sysctl_rx_budget;
19523ac9029SStephen Hurd 
19695246abbSSean Bruno 	qidx_t ifc_sysctl_ntxds[8];
19795246abbSSean Bruno 	qidx_t ifc_sysctl_nrxds[8];
1984c7070dbSScott Long 	struct if_txrx ifc_txrx;
1994c7070dbSScott Long #define isc_txd_encap  ifc_txrx.ift_txd_encap
2004c7070dbSScott Long #define isc_txd_flush  ifc_txrx.ift_txd_flush
2014c7070dbSScott Long #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
2024c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available
2034c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
2044c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2054c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush
2064c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2074c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill
2084c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr
2094c7070dbSScott Long 	eventhandler_tag ifc_vlan_attach_event;
2104c7070dbSScott Long 	eventhandler_tag ifc_vlan_detach_event;
2114c7070dbSScott Long 	uint8_t ifc_mac[ETHER_ADDR_LEN];
2124c7070dbSScott Long 	char ifc_mtx_name[16];
2134c7070dbSScott Long };
2144c7070dbSScott Long 
2154c7070dbSScott Long 
2164c7070dbSScott Long void *
2174c7070dbSScott Long iflib_get_softc(if_ctx_t ctx)
2184c7070dbSScott Long {
2194c7070dbSScott Long 
2204c7070dbSScott Long 	return (ctx->ifc_softc);
2214c7070dbSScott Long }
2224c7070dbSScott Long 
2234c7070dbSScott Long device_t
2244c7070dbSScott Long iflib_get_dev(if_ctx_t ctx)
2254c7070dbSScott Long {
2264c7070dbSScott Long 
2274c7070dbSScott Long 	return (ctx->ifc_dev);
2284c7070dbSScott Long }
2294c7070dbSScott Long 
2304c7070dbSScott Long if_t
2314c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx)
2324c7070dbSScott Long {
2334c7070dbSScott Long 
2344c7070dbSScott Long 	return (ctx->ifc_ifp);
2354c7070dbSScott Long }
2364c7070dbSScott Long 
2374c7070dbSScott Long struct ifmedia *
2384c7070dbSScott Long iflib_get_media(if_ctx_t ctx)
2394c7070dbSScott Long {
2404c7070dbSScott Long 
2414c7070dbSScott Long 	return (&ctx->ifc_media);
2424c7070dbSScott Long }
2434c7070dbSScott Long 
2444c7070dbSScott Long void
2454c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
2464c7070dbSScott Long {
2474c7070dbSScott Long 
2484c7070dbSScott Long 	bcopy(mac, ctx->ifc_mac, ETHER_ADDR_LEN);
2494c7070dbSScott Long }
2504c7070dbSScott Long 
2514c7070dbSScott Long if_softc_ctx_t
2524c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx)
2534c7070dbSScott Long {
2544c7070dbSScott Long 
2554c7070dbSScott Long 	return (&ctx->ifc_softc_ctx);
2564c7070dbSScott Long }
2574c7070dbSScott Long 
2584c7070dbSScott Long if_shared_ctx_t
2594c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx)
2604c7070dbSScott Long {
2614c7070dbSScott Long 
2624c7070dbSScott Long 	return (ctx->ifc_sctx);
2634c7070dbSScott Long }
2644c7070dbSScott Long 
26595246abbSSean Bruno #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
2664c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
2675e888388SSean Bruno #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1)))
2684c7070dbSScott Long 
2694c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
2704c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
2714c7070dbSScott Long 
2724c7070dbSScott Long #define RX_SW_DESC_MAP_CREATED	(1 << 0)
273ab2e3f79SStephen Hurd #define TX_SW_DESC_MAP_CREATED	(1 << 1)
274ab2e3f79SStephen Hurd #define RX_SW_DESC_INUSE        (1 << 3)
275ab2e3f79SStephen Hurd #define TX_SW_DESC_MAPPED       (1 << 4)
2764c7070dbSScott Long 
2772cc3b2eeSGleb Smirnoff #define	M_TOOBIG		M_PROTO1
2785c5ca36cSSean Bruno 
279e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array {
280e035717eSSean Bruno 	bus_dmamap_t	*ifsd_map;         /* bus_dma maps for packet */
281e035717eSSean Bruno 	struct mbuf	**ifsd_m;           /* pkthdr mbufs */
282e035717eSSean Bruno 	caddr_t		*ifsd_cl;          /* direct cluster pointer for rx */
283e035717eSSean Bruno 	uint8_t		*ifsd_flags;
284e035717eSSean Bruno } iflib_rxsd_array_t;
2854c7070dbSScott Long 
2864c7070dbSScott Long typedef struct iflib_sw_tx_desc_array {
2874c7070dbSScott Long 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
2884c7070dbSScott Long 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
2894c7070dbSScott Long 	uint8_t		*ifsd_flags;
29095246abbSSean Bruno } if_txsd_vec_t;
2914c7070dbSScott Long 
2924c7070dbSScott Long 
2934c7070dbSScott Long /* magic number that should be high enough for any hardware */
2944c7070dbSScott Long #define IFLIB_MAX_TX_SEGS		128
29509b57b7fSStephen Hurd /* bnxt supports 64 with hardware LRO enabled */
29609b57b7fSStephen Hurd #define IFLIB_MAX_RX_SEGS		64
29795246abbSSean Bruno #define IFLIB_RX_COPY_THRESH		128
2984c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH		32
29995246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */
30095246abbSSean Bruno #define IFLIB_MIN_DESC_SEC		16384
30195246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ	16
3024c7070dbSScott Long #define IFLIB_QUEUE_IDLE		0
3034c7070dbSScott Long #define IFLIB_QUEUE_HUNG		1
3044c7070dbSScott Long #define IFLIB_QUEUE_WORKING		2
30595246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */
30695246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR	4
3074c7070dbSScott Long 
30895246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */
30995246abbSSean Bruno #define TX_BATCH_SIZE			32
3104c7070dbSScott Long 
3114c7070dbSScott Long #define IFLIB_RESTART_BUDGET		8
3124c7070dbSScott Long 
31395246abbSSean Bruno #define	IFC_LEGACY		0x001
31495246abbSSean Bruno #define	IFC_QFLUSH		0x002
31595246abbSSean Bruno #define	IFC_MULTISEG		0x004
31695246abbSSean Bruno #define	IFC_DMAR		0x008
31795246abbSSean Bruno #define	IFC_SC_ALLOCATED	0x010
31895246abbSSean Bruno #define	IFC_INIT_DONE		0x020
31995246abbSSean Bruno #define	IFC_PREFETCH		0x040
32095246abbSSean Bruno #define	IFC_DO_RESET		0x080
3217b610b60SSean Bruno #define	IFC_DO_WATCHDOG		0x100
3227b610b60SSean Bruno #define	IFC_CHECK_HUNG		0x200
3237b610b60SSean Bruno 
3244c7070dbSScott Long 
3254c7070dbSScott Long #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
3264c7070dbSScott Long 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
3274c7070dbSScott Long 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
3284c7070dbSScott Long struct iflib_txq {
32995246abbSSean Bruno 	qidx_t		ift_in_use;
33095246abbSSean Bruno 	qidx_t		ift_cidx;
33195246abbSSean Bruno 	qidx_t		ift_cidx_processed;
33295246abbSSean Bruno 	qidx_t		ift_pidx;
3334c7070dbSScott Long 	uint8_t		ift_gen;
33423ac9029SStephen Hurd 	uint8_t		ift_br_offset;
33595246abbSSean Bruno 	uint16_t	ift_npending;
33695246abbSSean Bruno 	uint16_t	ift_db_pending;
33795246abbSSean Bruno 	uint16_t	ift_rs_pending;
3384c7070dbSScott Long 	/* implicit pad */
33995246abbSSean Bruno 	uint8_t		ift_txd_size[8];
3404c7070dbSScott Long 	uint64_t	ift_processed;
3414c7070dbSScott Long 	uint64_t	ift_cleaned;
34295246abbSSean Bruno 	uint64_t	ift_cleaned_prev;
3434c7070dbSScott Long #if MEMORY_LOGGING
3444c7070dbSScott Long 	uint64_t	ift_enqueued;
3454c7070dbSScott Long 	uint64_t	ift_dequeued;
3464c7070dbSScott Long #endif
3474c7070dbSScott Long 	uint64_t	ift_no_tx_dma_setup;
3484c7070dbSScott Long 	uint64_t	ift_no_desc_avail;
3494c7070dbSScott Long 	uint64_t	ift_mbuf_defrag_failed;
3504c7070dbSScott Long 	uint64_t	ift_mbuf_defrag;
3514c7070dbSScott Long 	uint64_t	ift_map_failed;
3524c7070dbSScott Long 	uint64_t	ift_txd_encap_efbig;
3534c7070dbSScott Long 	uint64_t	ift_pullups;
3544c7070dbSScott Long 
3554c7070dbSScott Long 	struct mtx	ift_mtx;
3564c7070dbSScott Long 	struct mtx	ift_db_mtx;
3574c7070dbSScott Long 
3584c7070dbSScott Long 	/* constant values */
3594c7070dbSScott Long 	if_ctx_t	ift_ctx;
36095246abbSSean Bruno 	struct ifmp_ring        *ift_br;
3614c7070dbSScott Long 	struct grouptask	ift_task;
36295246abbSSean Bruno 	qidx_t		ift_size;
3634c7070dbSScott Long 	uint16_t	ift_id;
3644c7070dbSScott Long 	struct callout	ift_timer;
3654c7070dbSScott Long 
36695246abbSSean Bruno 	if_txsd_vec_t	ift_sds;
3674c7070dbSScott Long 	uint8_t		ift_qstatus;
3684c7070dbSScott Long 	uint8_t		ift_closed;
36995246abbSSean Bruno 	uint8_t		ift_update_freq;
3704c7070dbSScott Long 	struct iflib_filter_info ift_filter_info;
3714c7070dbSScott Long 	bus_dma_tag_t		ift_desc_tag;
3724c7070dbSScott Long 	bus_dma_tag_t		ift_tso_desc_tag;
3734c7070dbSScott Long 	iflib_dma_info_t	ift_ifdi;
3744c7070dbSScott Long #define MTX_NAME_LEN 16
3754c7070dbSScott Long 	char                    ift_mtx_name[MTX_NAME_LEN];
3764c7070dbSScott Long 	char                    ift_db_mtx_name[MTX_NAME_LEN];
3774c7070dbSScott Long 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
3781248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
3791248952aSSean Bruno 	uint64_t ift_cpu_exec_count[256];
3801248952aSSean Bruno #endif
3814c7070dbSScott Long } __aligned(CACHE_LINE_SIZE);
3824c7070dbSScott Long 
3834c7070dbSScott Long struct iflib_fl {
38495246abbSSean Bruno 	qidx_t		ifl_cidx;
38595246abbSSean Bruno 	qidx_t		ifl_pidx;
38695246abbSSean Bruno 	qidx_t		ifl_credits;
3874c7070dbSScott Long 	uint8_t		ifl_gen;
38895246abbSSean Bruno 	uint8_t		ifl_rxd_size;
3894c7070dbSScott Long #if MEMORY_LOGGING
3904c7070dbSScott Long 	uint64_t	ifl_m_enqueued;
3914c7070dbSScott Long 	uint64_t	ifl_m_dequeued;
3924c7070dbSScott Long 	uint64_t	ifl_cl_enqueued;
3934c7070dbSScott Long 	uint64_t	ifl_cl_dequeued;
3944c7070dbSScott Long #endif
3954c7070dbSScott Long 	/* implicit pad */
3964c7070dbSScott Long 
39787890dbaSSean Bruno 	bitstr_t 	*ifl_rx_bitmap;
39887890dbaSSean Bruno 	qidx_t		ifl_fragidx;
3994c7070dbSScott Long 	/* constant */
40095246abbSSean Bruno 	qidx_t		ifl_size;
4014c7070dbSScott Long 	uint16_t	ifl_buf_size;
4024c7070dbSScott Long 	uint16_t	ifl_cltype;
4034c7070dbSScott Long 	uma_zone_t	ifl_zone;
404e035717eSSean Bruno 	iflib_rxsd_array_t	ifl_sds;
4054c7070dbSScott Long 	iflib_rxq_t	ifl_rxq;
4064c7070dbSScott Long 	uint8_t		ifl_id;
4074c7070dbSScott Long 	bus_dma_tag_t           ifl_desc_tag;
4084c7070dbSScott Long 	iflib_dma_info_t	ifl_ifdi;
4094c7070dbSScott Long 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
4104c7070dbSScott Long 	caddr_t		ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
41195246abbSSean Bruno 	qidx_t	ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
4124c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4134c7070dbSScott Long 
41495246abbSSean Bruno static inline qidx_t
41595246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
4164c7070dbSScott Long {
41795246abbSSean Bruno 	qidx_t used;
4184c7070dbSScott Long 
4194c7070dbSScott Long 	if (pidx > cidx)
4204c7070dbSScott Long 		used = pidx - cidx;
4214c7070dbSScott Long 	else if (pidx < cidx)
4224c7070dbSScott Long 		used = size - cidx + pidx;
4234c7070dbSScott Long 	else if (gen == 0 && pidx == cidx)
4244c7070dbSScott Long 		used = 0;
4254c7070dbSScott Long 	else if (gen == 1 && pidx == cidx)
4264c7070dbSScott Long 		used = size;
4274c7070dbSScott Long 	else
4284c7070dbSScott Long 		panic("bad state");
4294c7070dbSScott Long 
4304c7070dbSScott Long 	return (used);
4314c7070dbSScott Long }
4324c7070dbSScott Long 
4334c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
4344c7070dbSScott Long 
4354c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \
4364c7070dbSScott Long 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
4374c7070dbSScott Long 
4384c7070dbSScott Long struct iflib_rxq {
4394c7070dbSScott Long 	/* If there is a separate completion queue -
4404c7070dbSScott Long 	 * these are the cq cidx and pidx. Otherwise
4414c7070dbSScott Long 	 * these are unused.
4424c7070dbSScott Long 	 */
44395246abbSSean Bruno 	qidx_t		ifr_size;
44495246abbSSean Bruno 	qidx_t		ifr_cq_cidx;
44595246abbSSean Bruno 	qidx_t		ifr_cq_pidx;
4464c7070dbSScott Long 	uint8_t		ifr_cq_gen;
44723ac9029SStephen Hurd 	uint8_t		ifr_fl_offset;
4484c7070dbSScott Long 
4494c7070dbSScott Long 	if_ctx_t	ifr_ctx;
4504c7070dbSScott Long 	iflib_fl_t	ifr_fl;
4514c7070dbSScott Long 	uint64_t	ifr_rx_irq;
4524c7070dbSScott Long 	uint16_t	ifr_id;
4534c7070dbSScott Long 	uint8_t		ifr_lro_enabled;
4544c7070dbSScott Long 	uint8_t		ifr_nfl;
45595246abbSSean Bruno 	uint8_t		ifr_ntxqirq;
45695246abbSSean Bruno 	uint8_t		ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
4574c7070dbSScott Long 	struct lro_ctrl			ifr_lc;
4584c7070dbSScott Long 	struct grouptask        ifr_task;
4594c7070dbSScott Long 	struct iflib_filter_info ifr_filter_info;
4604c7070dbSScott Long 	iflib_dma_info_t		ifr_ifdi;
461ab2e3f79SStephen Hurd 
4624c7070dbSScott Long 	/* dynamically allocate if any drivers need a value substantially larger than this */
4634c7070dbSScott Long 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
4641248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
4651248952aSSean Bruno 	uint64_t ifr_cpu_exec_count[256];
4661248952aSSean Bruno #endif
4674c7070dbSScott Long }  __aligned(CACHE_LINE_SIZE);
4684c7070dbSScott Long 
46995246abbSSean Bruno typedef struct if_rxsd {
47095246abbSSean Bruno 	caddr_t *ifsd_cl;
47195246abbSSean Bruno 	struct mbuf **ifsd_m;
47295246abbSSean Bruno 	iflib_fl_t ifsd_fl;
47395246abbSSean Bruno 	qidx_t ifsd_cidx;
47495246abbSSean Bruno } *if_rxsd_t;
47595246abbSSean Bruno 
47695246abbSSean Bruno /* multiple of word size */
47795246abbSSean Bruno #ifdef __LP64__
478ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	6
47995246abbSSean Bruno #define RXD_INFO_SIZE	5
48095246abbSSean Bruno #define PKT_TYPE uint64_t
48195246abbSSean Bruno #else
482ab2e3f79SStephen Hurd #define PKT_INFO_SIZE	11
48395246abbSSean Bruno #define RXD_INFO_SIZE	8
48495246abbSSean Bruno #define PKT_TYPE uint32_t
48595246abbSSean Bruno #endif
48695246abbSSean Bruno #define PKT_LOOP_BOUND  ((PKT_INFO_SIZE/3)*3)
48795246abbSSean Bruno #define RXD_LOOP_BOUND  ((RXD_INFO_SIZE/4)*4)
48895246abbSSean Bruno 
48995246abbSSean Bruno typedef struct if_pkt_info_pad {
49095246abbSSean Bruno 	PKT_TYPE pkt_val[PKT_INFO_SIZE];
49195246abbSSean Bruno } *if_pkt_info_pad_t;
49295246abbSSean Bruno typedef struct if_rxd_info_pad {
49395246abbSSean Bruno 	PKT_TYPE rxd_val[RXD_INFO_SIZE];
49495246abbSSean Bruno } *if_rxd_info_pad_t;
49595246abbSSean Bruno 
49695246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info));
49795246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info));
49895246abbSSean Bruno 
49995246abbSSean Bruno 
50095246abbSSean Bruno static inline void
50195246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi)
50295246abbSSean Bruno {
50395246abbSSean Bruno 	if_pkt_info_pad_t pi_pad;
50495246abbSSean Bruno 
50595246abbSSean Bruno 	pi_pad = (if_pkt_info_pad_t)pi;
50695246abbSSean Bruno 	pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0;
50795246abbSSean Bruno 	pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0;
50895246abbSSean Bruno #ifndef __LP64__
509ab2e3f79SStephen Hurd 	pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0;
510ab2e3f79SStephen Hurd 	pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0;
51195246abbSSean Bruno #endif
51295246abbSSean Bruno }
51395246abbSSean Bruno 
51495246abbSSean Bruno static inline void
51595246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri)
51695246abbSSean Bruno {
51795246abbSSean Bruno 	if_rxd_info_pad_t ri_pad;
51895246abbSSean Bruno 	int i;
51995246abbSSean Bruno 
52095246abbSSean Bruno 	ri_pad = (if_rxd_info_pad_t)ri;
52195246abbSSean Bruno 	for (i = 0; i < RXD_LOOP_BOUND; i += 4) {
52295246abbSSean Bruno 		ri_pad->rxd_val[i] = 0;
52395246abbSSean Bruno 		ri_pad->rxd_val[i+1] = 0;
52495246abbSSean Bruno 		ri_pad->rxd_val[i+2] = 0;
52595246abbSSean Bruno 		ri_pad->rxd_val[i+3] = 0;
52695246abbSSean Bruno 	}
52795246abbSSean Bruno #ifdef __LP64__
52895246abbSSean Bruno 	ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0;
52995246abbSSean Bruno #endif
53095246abbSSean Bruno }
53195246abbSSean Bruno 
5324c7070dbSScott Long /*
5334c7070dbSScott Long  * Only allow a single packet to take up most 1/nth of the tx ring
5344c7070dbSScott Long  */
5354c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12
5364c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1
5374c7070dbSScott Long 
5384c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
5394c7070dbSScott Long 
540aa8a24d3SStephen Hurd #define CTX_LOCK_INIT(_sc)  sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock")
541aa8a24d3SStephen Hurd #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx)
542aa8a24d3SStephen Hurd #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx)
543aa8a24d3SStephen Hurd #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx)
5444c7070dbSScott Long 
5457b610b60SSean Bruno 
5467b610b60SSean Bruno #define STATE_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF)
5477b610b60SSean Bruno #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx)
5487b610b60SSean Bruno #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx)
5497b610b60SSean Bruno #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx)
5507b610b60SSean Bruno 
551ab2e3f79SStephen Hurd 
5524c7070dbSScott Long 
5534c7070dbSScott Long #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
5544c7070dbSScott Long #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
5554c7070dbSScott Long 
5564c7070dbSScott Long 
5574c7070dbSScott Long /* Our boot-time initialization hook */
5584c7070dbSScott Long static int	iflib_module_event_handler(module_t, int, void *);
5594c7070dbSScott Long 
5604c7070dbSScott Long static moduledata_t iflib_moduledata = {
5614c7070dbSScott Long 	"iflib",
5624c7070dbSScott Long 	iflib_module_event_handler,
5634c7070dbSScott Long 	NULL
5644c7070dbSScott Long };
5654c7070dbSScott Long 
5664c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
5674c7070dbSScott Long MODULE_VERSION(iflib, 1);
5684c7070dbSScott Long 
5694c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1);
5704c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1);
5714c7070dbSScott Long 
572ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
573ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
574ab2e3f79SStephen Hurd 
5754c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS
5764c7070dbSScott Long #ifdef INVARIANTS
5774c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1
5784c7070dbSScott Long #else
5794c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0
5804c7070dbSScott Long #endif /* !INVARIANTS */
5814c7070dbSScott Long #endif
5824c7070dbSScott Long 
583ab2e3f79SStephen Hurd static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0,
584ab2e3f79SStephen Hurd                    "iflib driver parameters");
585ab2e3f79SStephen Hurd 
5864c7070dbSScott Long /*
5874c7070dbSScott Long  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
5884c7070dbSScott Long  */
5894c7070dbSScott Long static int iflib_min_tx_latency = 0;
5904c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
591da69b8f9SSean Bruno 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
59295246abbSSean Bruno static int iflib_no_tx_batch = 0;
59395246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
59495246abbSSean Bruno 		   &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput");
5954c7070dbSScott Long 
5964c7070dbSScott Long 
5974c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS
5984c7070dbSScott Long 
5994c7070dbSScott Long static int iflib_tx_seen;
6004c7070dbSScott Long static int iflib_tx_sent;
6014c7070dbSScott Long static int iflib_tx_encap;
6024c7070dbSScott Long static int iflib_rx_allocs;
6034c7070dbSScott Long static int iflib_fl_refills;
6044c7070dbSScott Long static int iflib_fl_refills_large;
6054c7070dbSScott Long static int iflib_tx_frees;
6064c7070dbSScott Long 
6074c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
6084c7070dbSScott Long 		   &iflib_tx_seen, 0, "# tx mbufs seen");
6094c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
6104c7070dbSScott Long 		   &iflib_tx_sent, 0, "# tx mbufs sent");
6114c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
6124c7070dbSScott Long 		   &iflib_tx_encap, 0, "# tx mbufs encapped");
6134c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
6144c7070dbSScott Long 		   &iflib_tx_frees, 0, "# tx frees");
6154c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
6164c7070dbSScott Long 		   &iflib_rx_allocs, 0, "# rx allocations");
6174c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
6184c7070dbSScott Long 		   &iflib_fl_refills, 0, "# refills");
6194c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
6204c7070dbSScott Long 		   &iflib_fl_refills_large, 0, "# large refills");
6214c7070dbSScott Long 
6224c7070dbSScott Long 
6234c7070dbSScott Long static int iflib_txq_drain_flushing;
6244c7070dbSScott Long static int iflib_txq_drain_oactive;
6254c7070dbSScott Long static int iflib_txq_drain_notready;
6264c7070dbSScott Long static int iflib_txq_drain_encapfail;
6274c7070dbSScott Long 
6284c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
6294c7070dbSScott Long 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
6304c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
6314c7070dbSScott Long 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
6324c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
6334c7070dbSScott Long 		   &iflib_txq_drain_notready, 0, "# drain notready");
6344c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_encapfail, CTLFLAG_RD,
6354c7070dbSScott Long 		   &iflib_txq_drain_encapfail, 0, "# drain encap fails");
6364c7070dbSScott Long 
6374c7070dbSScott Long 
6384c7070dbSScott Long static int iflib_encap_load_mbuf_fail;
639d14c853bSStephen Hurd static int iflib_encap_pad_mbuf_fail;
6404c7070dbSScott Long static int iflib_encap_txq_avail_fail;
6414c7070dbSScott Long static int iflib_encap_txd_encap_fail;
6424c7070dbSScott Long 
6434c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
6444c7070dbSScott Long 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
645d14c853bSStephen Hurd SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD,
646d14c853bSStephen Hurd 		   &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures");
6474c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
6484c7070dbSScott Long 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
6494c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
6504c7070dbSScott Long 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
6514c7070dbSScott Long 
6524c7070dbSScott Long static int iflib_task_fn_rxs;
6534c7070dbSScott Long static int iflib_rx_intr_enables;
6544c7070dbSScott Long static int iflib_fast_intrs;
6554c7070dbSScott Long static int iflib_intr_link;
6564c7070dbSScott Long static int iflib_intr_msix;
6574c7070dbSScott Long static int iflib_rx_unavail;
6584c7070dbSScott Long static int iflib_rx_ctx_inactive;
6594c7070dbSScott Long static int iflib_rx_zero_len;
6604c7070dbSScott Long static int iflib_rx_if_input;
6614c7070dbSScott Long static int iflib_rx_mbuf_null;
6624c7070dbSScott Long static int iflib_rxd_flush;
6634c7070dbSScott Long 
6644c7070dbSScott Long static int iflib_verbose_debug;
6654c7070dbSScott Long 
6664c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_link, CTLFLAG_RD,
6674c7070dbSScott Long 		   &iflib_intr_link, 0, "# intr link calls");
6684c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, intr_msix, CTLFLAG_RD,
6694c7070dbSScott Long 		   &iflib_intr_msix, 0, "# intr msix calls");
6704c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
6714c7070dbSScott Long 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
6724c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
6734c7070dbSScott Long 		   &iflib_rx_intr_enables, 0, "# rx intr enables");
6744c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
6754c7070dbSScott Long 		   &iflib_fast_intrs, 0, "# fast_intr calls");
6764c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
6774c7070dbSScott Long 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
6784c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
6794c7070dbSScott Long 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
6804c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_zero_len, CTLFLAG_RD,
6814c7070dbSScott Long 		   &iflib_rx_zero_len, 0, "# times rxeof saw zero len mbuf");
6824c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
6834c7070dbSScott Long 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
6844c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD,
6854c7070dbSScott Long 		   &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf");
6864c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
6874c7070dbSScott Long 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
6884c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
6894c7070dbSScott Long 		   &iflib_verbose_debug, 0, "enable verbose debugging");
6904c7070dbSScott Long 
6914c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
692da69b8f9SSean Bruno static void
693da69b8f9SSean Bruno iflib_debug_reset(void)
694da69b8f9SSean Bruno {
695da69b8f9SSean Bruno 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
696da69b8f9SSean Bruno 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
697da69b8f9SSean Bruno 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
698da69b8f9SSean Bruno 		iflib_txq_drain_notready = iflib_txq_drain_encapfail =
699d14c853bSStephen Hurd 		iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail =
700d14c853bSStephen Hurd 		iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail =
701d14c853bSStephen Hurd 		iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs =
702d14c853bSStephen Hurd 		iflib_intr_link = iflib_intr_msix = iflib_rx_unavail =
703da69b8f9SSean Bruno 		iflib_rx_ctx_inactive = iflib_rx_zero_len = iflib_rx_if_input =
704da69b8f9SSean Bruno 		iflib_rx_mbuf_null = iflib_rxd_flush = 0;
705da69b8f9SSean Bruno }
7064c7070dbSScott Long 
7074c7070dbSScott Long #else
7084c7070dbSScott Long #define DBG_COUNTER_INC(name)
709da69b8f9SSean Bruno static void iflib_debug_reset(void) {}
7104c7070dbSScott Long #endif
7114c7070dbSScott Long 
7124c7070dbSScott Long 
7134c7070dbSScott Long 
7144c7070dbSScott Long #define IFLIB_DEBUG 0
7154c7070dbSScott Long 
7164c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx);
7174c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx);
7184c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx);
7194c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
72095246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
7214c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx);
7224c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx);
7234c7070dbSScott Long static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, char *str);
7244c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
7254c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
7264c7070dbSScott Long static int iflib_register(if_ctx_t);
7274c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx);
7284c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
7294c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx);
730da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq);
7311248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx);
73295246abbSSean Bruno static void iflib_stop(if_ctx_t ctx);
73395246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx);
73495246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
73595246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m);
73695246abbSSean Bruno #endif
7374c7070dbSScott Long 
7384c7070dbSScott Long #ifdef DEV_NETMAP
7394c7070dbSScott Long #include <sys/selinfo.h>
7404c7070dbSScott Long #include <net/netmap.h>
7414c7070dbSScott Long #include <dev/netmap/netmap_kern.h>
7424c7070dbSScott Long 
7434c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1);
7444c7070dbSScott Long 
7452d873474SStephen Hurd static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init);
7462d873474SStephen Hurd 
7474c7070dbSScott Long /*
7484c7070dbSScott Long  * device-specific sysctl variables:
7494c7070dbSScott Long  *
75091d546a0SConrad Meyer  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
7514c7070dbSScott Long  *	During regular operations the CRC is stripped, but on some
7524c7070dbSScott Long  *	hardware reception of frames not multiple of 64 is slower,
7534c7070dbSScott Long  *	so using crcstrip=0 helps in benchmarks.
7544c7070dbSScott Long  *
75591d546a0SConrad Meyer  * iflib_rx_miss, iflib_rx_miss_bufs:
7564c7070dbSScott Long  *	count packets that might be missed due to lost interrupts.
7574c7070dbSScott Long  */
7584c7070dbSScott Long SYSCTL_DECL(_dev_netmap);
7594c7070dbSScott Long /*
7604c7070dbSScott Long  * The xl driver by default strips CRCs and we do not override it.
7614c7070dbSScott Long  */
7624c7070dbSScott Long 
7634c7070dbSScott Long int iflib_crcstrip = 1;
7644c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
7654c7070dbSScott Long     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames");
7664c7070dbSScott Long 
7674c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs;
7684c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
7694c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr");
77091d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
7714c7070dbSScott Long     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs");
7724c7070dbSScott Long 
7734c7070dbSScott Long /*
7744c7070dbSScott Long  * Register/unregister. We are already under netmap lock.
7754c7070dbSScott Long  * Only called on the first register or the last unregister.
7764c7070dbSScott Long  */
7774c7070dbSScott Long static int
7784c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff)
7794c7070dbSScott Long {
7804c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
7814c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
78295246abbSSean Bruno 	int status;
7834c7070dbSScott Long 
7844c7070dbSScott Long 	CTX_LOCK(ctx);
7854c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
7864c7070dbSScott Long 
7874c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
7884c7070dbSScott Long 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
7894c7070dbSScott Long 
7904c7070dbSScott Long 	if (!CTX_IS_VF(ctx))
7911248952aSSean Bruno 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
7924c7070dbSScott Long 
7934c7070dbSScott Long 	/* enable or disable flags and callbacks in na and ifp */
7944c7070dbSScott Long 	if (onoff) {
7954c7070dbSScott Long 		nm_set_native_flags(na);
7964c7070dbSScott Long 	} else {
7974c7070dbSScott Long 		nm_clear_native_flags(na);
7984c7070dbSScott Long 	}
79995246abbSSean Bruno 	iflib_stop(ctx);
80095246abbSSean Bruno 	iflib_init_locked(ctx);
8011248952aSSean Bruno 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
80295246abbSSean Bruno 	status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1;
80395246abbSSean Bruno 	if (status)
80495246abbSSean Bruno 		nm_clear_native_flags(na);
8054c7070dbSScott Long 	CTX_UNLOCK(ctx);
80695246abbSSean Bruno 	return (status);
8074c7070dbSScott Long }
8084c7070dbSScott Long 
8092d873474SStephen Hurd static int
8102d873474SStephen Hurd netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init)
8112d873474SStephen Hurd {
8122d873474SStephen Hurd 	struct netmap_adapter *na = kring->na;
8132d873474SStephen Hurd 	u_int const lim = kring->nkr_num_slots - 1;
8142d873474SStephen Hurd 	u_int head = kring->rhead;
8152d873474SStephen Hurd 	struct netmap_ring *ring = kring->ring;
8162d873474SStephen Hurd 	bus_dmamap_t *map;
8172d873474SStephen Hurd 	struct if_rxd_update iru;
8182d873474SStephen Hurd 	if_ctx_t ctx = rxq->ifr_ctx;
8192d873474SStephen Hurd 	iflib_fl_t fl = &rxq->ifr_fl[0];
8202d873474SStephen Hurd 	uint32_t refill_pidx, nic_i;
8212d873474SStephen Hurd 
8222d873474SStephen Hurd 	if (nm_i == head && __predict_true(!init))
8232d873474SStephen Hurd 		return 0;
8242d873474SStephen Hurd 	iru_init(&iru, rxq, 0 /* flid */);
8252d873474SStephen Hurd 	map = fl->ifl_sds.ifsd_map;
8262d873474SStephen Hurd 	refill_pidx = netmap_idx_k2n(kring, nm_i);
8272d873474SStephen Hurd 	/*
8282d873474SStephen Hurd 	 * IMPORTANT: we must leave one free slot in the ring,
8292d873474SStephen Hurd 	 * so move head back by one unit
8302d873474SStephen Hurd 	 */
8312d873474SStephen Hurd 	head = nm_prev(head, lim);
8322d873474SStephen Hurd 	while (nm_i != head) {
8332d873474SStephen Hurd 		for (int tmp_pidx = 0; tmp_pidx < IFLIB_MAX_RX_REFRESH && nm_i != head; tmp_pidx++) {
8342d873474SStephen Hurd 			struct netmap_slot *slot = &ring->slot[nm_i];
8352d873474SStephen Hurd 			void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[tmp_pidx]);
8362d873474SStephen Hurd 			uint32_t nic_i_dma = refill_pidx;
8372d873474SStephen Hurd 			nic_i = netmap_idx_k2n(kring, nm_i);
8382d873474SStephen Hurd 
8392d873474SStephen Hurd 			MPASS(tmp_pidx < IFLIB_MAX_RX_REFRESH);
8402d873474SStephen Hurd 
8412d873474SStephen Hurd 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
8422d873474SStephen Hurd 			        return netmap_ring_reinit(kring);
8432d873474SStephen Hurd 
8442d873474SStephen Hurd 			fl->ifl_vm_addrs[tmp_pidx] = addr;
8452d873474SStephen Hurd 			if (__predict_false(init) && map) {
8462d873474SStephen Hurd 				netmap_load_map(na, fl->ifl_ifdi->idi_tag, map[nic_i], addr);
8472d873474SStephen Hurd 			} else if (map && (slot->flags & NS_BUF_CHANGED)) {
8482d873474SStephen Hurd 				/* buffer has changed, reload map */
8492d873474SStephen Hurd 				netmap_reload_map(na, fl->ifl_ifdi->idi_tag, map[nic_i], addr);
8502d873474SStephen Hurd 			}
8512d873474SStephen Hurd 			slot->flags &= ~NS_BUF_CHANGED;
8522d873474SStephen Hurd 
8532d873474SStephen Hurd 			nm_i = nm_next(nm_i, lim);
8542d873474SStephen Hurd 			fl->ifl_rxd_idxs[tmp_pidx] = nic_i = nm_next(nic_i, lim);
8552d873474SStephen Hurd 			if (nm_i != head && tmp_pidx < IFLIB_MAX_RX_REFRESH-1)
8562d873474SStephen Hurd 				continue;
8572d873474SStephen Hurd 
8582d873474SStephen Hurd 			iru.iru_pidx = refill_pidx;
8592d873474SStephen Hurd 			iru.iru_count = tmp_pidx+1;
8602d873474SStephen Hurd 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
8612d873474SStephen Hurd 
8622d873474SStephen Hurd 			refill_pidx = nic_i;
8632d873474SStephen Hurd 			if (map == NULL)
8642d873474SStephen Hurd 				continue;
8652d873474SStephen Hurd 
8662d873474SStephen Hurd 			for (int n = 0; n < iru.iru_count; n++) {
8672d873474SStephen Hurd 				bus_dmamap_sync(fl->ifl_ifdi->idi_tag, map[nic_i_dma],
8682d873474SStephen Hurd 						BUS_DMASYNC_PREREAD);
8692d873474SStephen Hurd 				/* XXX - change this to not use the netmap func*/
8702d873474SStephen Hurd 				nic_i_dma = nm_next(nic_i_dma, lim);
8712d873474SStephen Hurd 			}
8722d873474SStephen Hurd 		}
8732d873474SStephen Hurd 	}
8742d873474SStephen Hurd 	kring->nr_hwcur = head;
8752d873474SStephen Hurd 
8762d873474SStephen Hurd 	if (map)
8772d873474SStephen Hurd 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
8782d873474SStephen Hurd 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
8792d873474SStephen Hurd 	ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
8802d873474SStephen Hurd 	return (0);
8812d873474SStephen Hurd }
8822d873474SStephen Hurd 
8834c7070dbSScott Long /*
8844c7070dbSScott Long  * Reconcile kernel and user view of the transmit ring.
8854c7070dbSScott Long  *
8864c7070dbSScott Long  * All information is in the kring.
8874c7070dbSScott Long  * Userspace wants to send packets up to the one before kring->rhead,
8884c7070dbSScott Long  * kernel knows kring->nr_hwcur is the first unsent packet.
8894c7070dbSScott Long  *
8904c7070dbSScott Long  * Here we push packets out (as many as possible), and possibly
8914c7070dbSScott Long  * reclaim buffers from previously completed transmission.
8924c7070dbSScott Long  *
8934c7070dbSScott Long  * The caller (netmap) guarantees that there is only one instance
8944c7070dbSScott Long  * running at any time. Any interference with other driver
8954c7070dbSScott Long  * methods should be handled by the individual drivers.
8964c7070dbSScott Long  */
8974c7070dbSScott Long static int
8984c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags)
8994c7070dbSScott Long {
9004c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
9014c7070dbSScott Long 	struct ifnet *ifp = na->ifp;
9024c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
9034c7070dbSScott Long 	u_int nm_i;	/* index into the netmap ring */
9044c7070dbSScott Long 	u_int nic_i;	/* index into the NIC ring */
9054c7070dbSScott Long 	u_int n;
9064c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
9074c7070dbSScott Long 	u_int const head = kring->rhead;
9084c7070dbSScott Long 	struct if_pkt_info pi;
9094c7070dbSScott Long 
9104c7070dbSScott Long 	/*
9114c7070dbSScott Long 	 * interrupts on every tx packet are expensive so request
9124c7070dbSScott Long 	 * them every half ring, or where NS_REPORT is set
9134c7070dbSScott Long 	 */
9144c7070dbSScott Long 	u_int report_frequency = kring->nkr_num_slots >> 1;
9154c7070dbSScott Long 	/* device-specific */
9164c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
9174c7070dbSScott Long 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
9184c7070dbSScott Long 
91995246abbSSean Bruno 	if (txq->ift_sds.ifsd_map)
9204c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
9214c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
9224c7070dbSScott Long 
9234c7070dbSScott Long 
9244c7070dbSScott Long 	/*
9254c7070dbSScott Long 	 * First part: process new packets to send.
9264c7070dbSScott Long 	 * nm_i is the current index in the netmap ring,
9274c7070dbSScott Long 	 * nic_i is the corresponding index in the NIC ring.
9284c7070dbSScott Long 	 *
9294c7070dbSScott Long 	 * If we have packets to send (nm_i != head)
9304c7070dbSScott Long 	 * iterate over the netmap ring, fetch length and update
9314c7070dbSScott Long 	 * the corresponding slot in the NIC ring. Some drivers also
9324c7070dbSScott Long 	 * need to update the buffer's physical address in the NIC slot
9334c7070dbSScott Long 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
9344c7070dbSScott Long 	 *
9354c7070dbSScott Long 	 * The netmap_reload_map() calls is especially expensive,
9364c7070dbSScott Long 	 * even when (as in this case) the tag is 0, so do only
9374c7070dbSScott Long 	 * when the buffer has actually changed.
9384c7070dbSScott Long 	 *
9394c7070dbSScott Long 	 * If possible do not set the report/intr bit on all slots,
9404c7070dbSScott Long 	 * but only a few times per ring or when NS_REPORT is set.
9414c7070dbSScott Long 	 *
9424c7070dbSScott Long 	 * Finally, on 10G and faster drivers, it might be useful
9434c7070dbSScott Long 	 * to prefetch the next slot and txr entry.
9444c7070dbSScott Long 	 */
9454c7070dbSScott Long 
9462d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, kring->nr_hwcur);
94795246abbSSean Bruno 	pkt_info_zero(&pi);
94895246abbSSean Bruno 	pi.ipi_segs = txq->ift_segs;
94995246abbSSean Bruno 	pi.ipi_qsidx = kring->ring_id;
9504c7070dbSScott Long 	if (nm_i != head) {	/* we have new packets to send */
9514c7070dbSScott Long 		nic_i = netmap_idx_k2n(kring, nm_i);
9524c7070dbSScott Long 
9534c7070dbSScott Long 		__builtin_prefetch(&ring->slot[nm_i]);
9544c7070dbSScott Long 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
95595246abbSSean Bruno 		if (txq->ift_sds.ifsd_map)
9564c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
9574c7070dbSScott Long 
9584c7070dbSScott Long 		for (n = 0; nm_i != head; n++) {
9594c7070dbSScott Long 			struct netmap_slot *slot = &ring->slot[nm_i];
9604c7070dbSScott Long 			u_int len = slot->len;
9610a1b74a3SSean Bruno 			uint64_t paddr;
9624c7070dbSScott Long 			void *addr = PNMB(na, slot, &paddr);
9634c7070dbSScott Long 			int flags = (slot->flags & NS_REPORT ||
9644c7070dbSScott Long 				nic_i == 0 || nic_i == report_frequency) ?
9654c7070dbSScott Long 				IPI_TX_INTR : 0;
9664c7070dbSScott Long 
9674c7070dbSScott Long 			/* device-specific */
96895246abbSSean Bruno 			pi.ipi_len = len;
96995246abbSSean Bruno 			pi.ipi_segs[0].ds_addr = paddr;
97095246abbSSean Bruno 			pi.ipi_segs[0].ds_len = len;
97195246abbSSean Bruno 			pi.ipi_nsegs = 1;
97295246abbSSean Bruno 			pi.ipi_ndescs = 0;
9734c7070dbSScott Long 			pi.ipi_pidx = nic_i;
9744c7070dbSScott Long 			pi.ipi_flags = flags;
9754c7070dbSScott Long 
9764c7070dbSScott Long 			/* Fill the slot in the NIC ring. */
9774c7070dbSScott Long 			ctx->isc_txd_encap(ctx->ifc_softc, &pi);
9784c7070dbSScott Long 
9794c7070dbSScott Long 			/* prefetch for next round */
9804c7070dbSScott Long 			__builtin_prefetch(&ring->slot[nm_i + 1]);
9814c7070dbSScott Long 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
98295246abbSSean Bruno 			if (txq->ift_sds.ifsd_map) {
9834c7070dbSScott Long 				__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
9844c7070dbSScott Long 
9854c7070dbSScott Long 				NM_CHECK_ADDR_LEN(na, addr, len);
9864c7070dbSScott Long 
9874c7070dbSScott Long 				if (slot->flags & NS_BUF_CHANGED) {
9884c7070dbSScott Long 					/* buffer has changed, reload map */
9894c7070dbSScott Long 					netmap_reload_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[nic_i], addr);
9904c7070dbSScott Long 				}
9914c7070dbSScott Long 				/* make sure changes to the buffer are synced */
9924c7070dbSScott Long 				bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_sds.ifsd_map[nic_i],
9934c7070dbSScott Long 						BUS_DMASYNC_PREWRITE);
99495246abbSSean Bruno 			}
99595246abbSSean Bruno 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
9964c7070dbSScott Long 			nm_i = nm_next(nm_i, lim);
9974c7070dbSScott Long 			nic_i = nm_next(nic_i, lim);
9984c7070dbSScott Long 		}
9994c7070dbSScott Long 		kring->nr_hwcur = head;
10004c7070dbSScott Long 
10014c7070dbSScott Long 		/* synchronize the NIC ring */
100295246abbSSean Bruno 		if (txq->ift_sds.ifsd_map)
10034c7070dbSScott Long 			bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
10044c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
10054c7070dbSScott Long 
10064c7070dbSScott Long 		/* (re)start the tx unit up to slot nic_i (excluded) */
10074c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
10084c7070dbSScott Long 	}
10094c7070dbSScott Long 
10104c7070dbSScott Long 	/*
10114c7070dbSScott Long 	 * Second part: reclaim buffers for completed transmissions.
10124c7070dbSScott Long 	 */
10134c7070dbSScott Long 	if (iflib_tx_credits_update(ctx, txq)) {
10144c7070dbSScott Long 		/* some tx completed, increment avail */
10154c7070dbSScott Long 		nic_i = txq->ift_cidx_processed;
10164c7070dbSScott Long 		kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
10174c7070dbSScott Long 	}
10184c7070dbSScott Long 	return (0);
10194c7070dbSScott Long }
10204c7070dbSScott Long 
10214c7070dbSScott Long /*
10224c7070dbSScott Long  * Reconcile kernel and user view of the receive ring.
10234c7070dbSScott Long  * Same as for the txsync, this routine must be efficient.
10244c7070dbSScott Long  * The caller guarantees a single invocations, but races against
10254c7070dbSScott Long  * the rest of the driver should be handled here.
10264c7070dbSScott Long  *
10274c7070dbSScott Long  * On call, kring->rhead is the first packet that userspace wants
10284c7070dbSScott Long  * to keep, and kring->rcur is the wakeup point.
10294c7070dbSScott Long  * The kernel has previously reported packets up to kring->rtail.
10304c7070dbSScott Long  *
10314c7070dbSScott Long  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
10324c7070dbSScott Long  * of whether or not we received an interrupt.
10334c7070dbSScott Long  */
10344c7070dbSScott Long static int
10354c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
10364c7070dbSScott Long {
10374c7070dbSScott Long 	struct netmap_adapter *na = kring->na;
10384c7070dbSScott Long 	struct netmap_ring *ring = kring->ring;
103995246abbSSean Bruno 	uint32_t nm_i;	/* index into the netmap ring */
10402d873474SStephen Hurd 	uint32_t nic_i;	/* index into the NIC ring */
10414c7070dbSScott Long 	u_int i, n;
10424c7070dbSScott Long 	u_int const lim = kring->nkr_num_slots - 1;
10432d873474SStephen Hurd 	u_int const head = netmap_idx_n2k(kring, kring->rhead);
10444c7070dbSScott Long 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
1045ab2e3f79SStephen Hurd 	struct if_rxd_info ri;
104695246abbSSean Bruno 
104795246abbSSean Bruno 	struct ifnet *ifp = na->ifp;
10484c7070dbSScott Long 	if_ctx_t ctx = ifp->if_softc;
10494c7070dbSScott Long 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
10504c7070dbSScott Long 	iflib_fl_t fl = rxq->ifr_fl;
10514c7070dbSScott Long 	if (head > lim)
10524c7070dbSScott Long 		return netmap_ring_reinit(kring);
10534c7070dbSScott Long 
10544c7070dbSScott Long 	/* XXX check sync modes */
105595246abbSSean Bruno 	for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) {
105695246abbSSean Bruno 		if (fl->ifl_sds.ifsd_map == NULL)
105795246abbSSean Bruno 			continue;
10584c7070dbSScott Long 		bus_dmamap_sync(rxq->ifr_fl[i].ifl_desc_tag, fl->ifl_ifdi->idi_map,
10594c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
106095246abbSSean Bruno 	}
10614c7070dbSScott Long 	/*
10624c7070dbSScott Long 	 * First part: import newly received packets.
10634c7070dbSScott Long 	 *
10644c7070dbSScott Long 	 * nm_i is the index of the next free slot in the netmap ring,
10654c7070dbSScott Long 	 * nic_i is the index of the next received packet in the NIC ring,
10664c7070dbSScott Long 	 * and they may differ in case if_init() has been called while
10674c7070dbSScott Long 	 * in netmap mode. For the receive ring we have
10684c7070dbSScott Long 	 *
10694c7070dbSScott Long 	 *	nic_i = rxr->next_check;
10704c7070dbSScott Long 	 *	nm_i = kring->nr_hwtail (previous)
10714c7070dbSScott Long 	 * and
10724c7070dbSScott Long 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
10734c7070dbSScott Long 	 *
10744c7070dbSScott Long 	 * rxr->next_check is set to 0 on a ring reinit
10754c7070dbSScott Long 	 */
10764c7070dbSScott Long 	if (netmap_no_pendintr || force_update) {
10774c7070dbSScott Long 		int crclen = iflib_crcstrip ? 0 : 4;
10784c7070dbSScott Long 		int error, avail;
10794c7070dbSScott Long 
10802d873474SStephen Hurd 		for (i = 0; i < rxq->ifr_nfl; i++) {
10812d873474SStephen Hurd 			fl = &rxq->ifr_fl[i];
10824c7070dbSScott Long 			nic_i = fl->ifl_cidx;
10834c7070dbSScott Long 			nm_i = netmap_idx_n2k(kring, nic_i);
108495246abbSSean Bruno 			avail = iflib_rxd_avail(ctx, rxq, nic_i, USHRT_MAX);
10854c7070dbSScott Long 			for (n = 0; avail > 0; n++, avail--) {
1086ab2e3f79SStephen Hurd 				rxd_info_zero(&ri);
1087ab2e3f79SStephen Hurd 				ri.iri_frags = rxq->ifr_frags;
1088ab2e3f79SStephen Hurd 				ri.iri_qsidx = kring->ring_id;
1089ab2e3f79SStephen Hurd 				ri.iri_ifp = ctx->ifc_ifp;
1090ab2e3f79SStephen Hurd 				ri.iri_cidx = nic_i;
109195246abbSSean Bruno 
1092ab2e3f79SStephen Hurd 				error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
1093ab2e3f79SStephen Hurd 				ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen;
10947cb7c6e3SNavdeep Parhar 				ring->slot[nm_i].flags = 0;
109595246abbSSean Bruno 				if (fl->ifl_sds.ifsd_map)
10964c7070dbSScott Long 					bus_dmamap_sync(fl->ifl_ifdi->idi_tag,
1097e035717eSSean Bruno 							fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
10984c7070dbSScott Long 				nm_i = nm_next(nm_i, lim);
10994c7070dbSScott Long 				nic_i = nm_next(nic_i, lim);
11004c7070dbSScott Long 			}
11014c7070dbSScott Long 			if (n) { /* update the state variables */
11024c7070dbSScott Long 				if (netmap_no_pendintr && !force_update) {
11034c7070dbSScott Long 					/* diagnostics */
11044c7070dbSScott Long 					iflib_rx_miss ++;
11054c7070dbSScott Long 					iflib_rx_miss_bufs += n;
11064c7070dbSScott Long 				}
11074c7070dbSScott Long 				fl->ifl_cidx = nic_i;
11082d873474SStephen Hurd 				kring->nr_hwtail = netmap_idx_k2n(kring, nm_i);
11094c7070dbSScott Long 			}
11104c7070dbSScott Long 			kring->nr_kflags &= ~NKR_PENDINTR;
11114c7070dbSScott Long 		}
11124c7070dbSScott Long 	}
11134c7070dbSScott Long 	/*
11144c7070dbSScott Long 	 * Second part: skip past packets that userspace has released.
11154c7070dbSScott Long 	 * (kring->nr_hwcur to head excluded),
11164c7070dbSScott Long 	 * and make the buffers available for reception.
11174c7070dbSScott Long 	 * As usual nm_i is the index in the netmap ring,
11184c7070dbSScott Long 	 * nic_i is the index in the NIC ring, and
11194c7070dbSScott Long 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
11204c7070dbSScott Long 	 */
11214c7070dbSScott Long 	/* XXX not sure how this will work with multiple free lists */
11222d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, kring->nr_hwcur);
112395246abbSSean Bruno 
11242d873474SStephen Hurd 	return (netmap_fl_refill(rxq, kring, nm_i, false));
11254c7070dbSScott Long }
11264c7070dbSScott Long 
112795246abbSSean Bruno static void
112895246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff)
112995246abbSSean Bruno {
113095246abbSSean Bruno 	struct ifnet *ifp = na->ifp;
113195246abbSSean Bruno 	if_ctx_t ctx = ifp->if_softc;
113295246abbSSean Bruno 
1133ab2e3f79SStephen Hurd 	CTX_LOCK(ctx);
113495246abbSSean Bruno 	if (onoff) {
113595246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
113695246abbSSean Bruno 	} else {
113795246abbSSean Bruno 		IFDI_INTR_DISABLE(ctx);
113895246abbSSean Bruno 	}
1139ab2e3f79SStephen Hurd 	CTX_UNLOCK(ctx);
114095246abbSSean Bruno }
114195246abbSSean Bruno 
114295246abbSSean Bruno 
11434c7070dbSScott Long static int
11444c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx)
11454c7070dbSScott Long {
11464c7070dbSScott Long 	struct netmap_adapter na;
114723ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
11484c7070dbSScott Long 
11494c7070dbSScott Long 	bzero(&na, sizeof(na));
11504c7070dbSScott Long 
11514c7070dbSScott Long 	na.ifp = ctx->ifc_ifp;
11524c7070dbSScott Long 	na.na_flags = NAF_BDG_MAYSLEEP;
11534c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
11544c7070dbSScott Long 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
11554c7070dbSScott Long 
115623ac9029SStephen Hurd 	na.num_tx_desc = scctx->isc_ntxd[0];
115723ac9029SStephen Hurd 	na.num_rx_desc = scctx->isc_nrxd[0];
11584c7070dbSScott Long 	na.nm_txsync = iflib_netmap_txsync;
11594c7070dbSScott Long 	na.nm_rxsync = iflib_netmap_rxsync;
11604c7070dbSScott Long 	na.nm_register = iflib_netmap_register;
116195246abbSSean Bruno 	na.nm_intr = iflib_netmap_intr;
11624c7070dbSScott Long 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
11634c7070dbSScott Long 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
11644c7070dbSScott Long 	return (netmap_attach(&na));
11654c7070dbSScott Long }
11664c7070dbSScott Long 
11674c7070dbSScott Long static void
11684c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
11694c7070dbSScott Long {
11704c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
11714c7070dbSScott Long 	struct netmap_slot *slot;
11724c7070dbSScott Long 
11734c7070dbSScott Long 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1174e099b90bSPedro F. Giffuni 	if (slot == NULL)
11754c7070dbSScott Long 		return;
117695246abbSSean Bruno 	if (txq->ift_sds.ifsd_map == NULL)
117795246abbSSean Bruno 		return;
11784c7070dbSScott Long 
117923ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
11804c7070dbSScott Long 
11814c7070dbSScott Long 		/*
11824c7070dbSScott Long 		 * In netmap mode, set the map for the packet buffer.
11834c7070dbSScott Long 		 * NOTE: Some drivers (not this one) also need to set
11844c7070dbSScott Long 		 * the physical buffer address in the NIC ring.
11854c7070dbSScott Long 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
11864c7070dbSScott Long 		 * netmap slot index, si
11874c7070dbSScott Long 		 */
11882ff91c17SVincenzo Maffione 		int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i);
11894c7070dbSScott Long 		netmap_load_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[i], NMB(na, slot + si));
11904c7070dbSScott Long 	}
11914c7070dbSScott Long }
11922d873474SStephen Hurd 
11934c7070dbSScott Long static void
11944c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
11954c7070dbSScott Long {
11964c7070dbSScott Long 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
11972ff91c17SVincenzo Maffione 	struct netmap_kring *kring = na->rx_rings[rxq->ifr_id];
11984c7070dbSScott Long 	struct netmap_slot *slot;
11992d873474SStephen Hurd 	uint32_t nm_i;
12004c7070dbSScott Long 
12014c7070dbSScott Long 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1202e099b90bSPedro F. Giffuni 	if (slot == NULL)
12034c7070dbSScott Long 		return;
12042d873474SStephen Hurd 	nm_i = netmap_idx_n2k(kring, 0);
12052d873474SStephen Hurd 	netmap_fl_refill(rxq, kring, nm_i, true);
12064c7070dbSScott Long }
12074c7070dbSScott Long 
12084c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp)
12094c7070dbSScott Long 
12104c7070dbSScott Long #else
12114c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq)
12124c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq)
12134c7070dbSScott Long #define iflib_netmap_detach(ifp)
12144c7070dbSScott Long 
12154c7070dbSScott Long #define iflib_netmap_attach(ctx) (0)
12164c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0)
121795246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0)
12184c7070dbSScott Long 
12194c7070dbSScott Long #endif
12204c7070dbSScott Long 
12214c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
12224c7070dbSScott Long static __inline void
12234c7070dbSScott Long prefetch(void *x)
12244c7070dbSScott Long {
12254c7070dbSScott Long 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12264c7070dbSScott Long }
12273429c02fSStephen Hurd static __inline void
12283429c02fSStephen Hurd prefetch2cachelines(void *x)
12293429c02fSStephen Hurd {
12303429c02fSStephen Hurd 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
12313429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128)
12323429c02fSStephen Hurd 	__asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long)))));
12333429c02fSStephen Hurd #endif
12343429c02fSStephen Hurd }
12354c7070dbSScott Long #else
12364c7070dbSScott Long #define prefetch(x)
12373429c02fSStephen Hurd #define prefetch2cachelines(x)
12384c7070dbSScott Long #endif
12394c7070dbSScott Long 
12404c7070dbSScott Long static void
124110e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid)
124210e0d938SStephen Hurd {
124310e0d938SStephen Hurd 	iflib_fl_t fl;
124410e0d938SStephen Hurd 
124510e0d938SStephen Hurd 	fl = &rxq->ifr_fl[flid];
124610e0d938SStephen Hurd 	iru->iru_paddrs = fl->ifl_bus_addrs;
124710e0d938SStephen Hurd 	iru->iru_vaddrs = &fl->ifl_vm_addrs[0];
124810e0d938SStephen Hurd 	iru->iru_idxs = fl->ifl_rxd_idxs;
124910e0d938SStephen Hurd 	iru->iru_qsidx = rxq->ifr_id;
125010e0d938SStephen Hurd 	iru->iru_buf_size = fl->ifl_buf_size;
125110e0d938SStephen Hurd 	iru->iru_flidx = fl->ifl_id;
125210e0d938SStephen Hurd }
125310e0d938SStephen Hurd 
125410e0d938SStephen Hurd static void
12554c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
12564c7070dbSScott Long {
12574c7070dbSScott Long 	if (err)
12584c7070dbSScott Long 		return;
12594c7070dbSScott Long 	*(bus_addr_t *) arg = segs[0].ds_addr;
12604c7070dbSScott Long }
12614c7070dbSScott Long 
12624c7070dbSScott Long int
12634c7070dbSScott Long iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
12644c7070dbSScott Long {
12654c7070dbSScott Long 	int err;
12664c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
12674c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
12684c7070dbSScott Long 
12694c7070dbSScott Long 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
12704c7070dbSScott Long 
12714c7070dbSScott Long 	err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
12724c7070dbSScott Long 				sctx->isc_q_align, 0,	/* alignment, bounds */
12734c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* lowaddr */
12744c7070dbSScott Long 				BUS_SPACE_MAXADDR,	/* highaddr */
12754c7070dbSScott Long 				NULL, NULL,		/* filter, filterarg */
12764c7070dbSScott Long 				size,			/* maxsize */
12774c7070dbSScott Long 				1,			/* nsegments */
12784c7070dbSScott Long 				size,			/* maxsegsize */
12794c7070dbSScott Long 				BUS_DMA_ALLOCNOW,	/* flags */
12804c7070dbSScott Long 				NULL,			/* lockfunc */
12814c7070dbSScott Long 				NULL,			/* lockarg */
12824c7070dbSScott Long 				&dma->idi_tag);
12834c7070dbSScott Long 	if (err) {
12844c7070dbSScott Long 		device_printf(dev,
12854c7070dbSScott Long 		    "%s: bus_dma_tag_create failed: %d\n",
12864c7070dbSScott Long 		    __func__, err);
12874c7070dbSScott Long 		goto fail_0;
12884c7070dbSScott Long 	}
12894c7070dbSScott Long 
12904c7070dbSScott Long 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
12914c7070dbSScott Long 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
12924c7070dbSScott Long 	if (err) {
12934c7070dbSScott Long 		device_printf(dev,
12944c7070dbSScott Long 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
12954c7070dbSScott Long 		    __func__, (uintmax_t)size, err);
12964c7070dbSScott Long 		goto fail_1;
12974c7070dbSScott Long 	}
12984c7070dbSScott Long 
12994c7070dbSScott Long 	dma->idi_paddr = IF_BAD_DMA;
13004c7070dbSScott Long 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
13014c7070dbSScott Long 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
13024c7070dbSScott Long 	if (err || dma->idi_paddr == IF_BAD_DMA) {
13034c7070dbSScott Long 		device_printf(dev,
13044c7070dbSScott Long 		    "%s: bus_dmamap_load failed: %d\n",
13054c7070dbSScott Long 		    __func__, err);
13064c7070dbSScott Long 		goto fail_2;
13074c7070dbSScott Long 	}
13084c7070dbSScott Long 
13094c7070dbSScott Long 	dma->idi_size = size;
13104c7070dbSScott Long 	return (0);
13114c7070dbSScott Long 
13124c7070dbSScott Long fail_2:
13134c7070dbSScott Long 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
13144c7070dbSScott Long fail_1:
13154c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
13164c7070dbSScott Long fail_0:
13174c7070dbSScott Long 	dma->idi_tag = NULL;
13184c7070dbSScott Long 
13194c7070dbSScott Long 	return (err);
13204c7070dbSScott Long }
13214c7070dbSScott Long 
13224c7070dbSScott Long int
13234c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
13244c7070dbSScott Long {
13254c7070dbSScott Long 	int i, err;
13264c7070dbSScott Long 	iflib_dma_info_t *dmaiter;
13274c7070dbSScott Long 
13284c7070dbSScott Long 	dmaiter = dmalist;
13294c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++) {
13304c7070dbSScott Long 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
13314c7070dbSScott Long 			break;
13324c7070dbSScott Long 	}
13334c7070dbSScott Long 	if (err)
13344c7070dbSScott Long 		iflib_dma_free_multi(dmalist, i);
13354c7070dbSScott Long 	return (err);
13364c7070dbSScott Long }
13374c7070dbSScott Long 
13384c7070dbSScott Long void
13394c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma)
13404c7070dbSScott Long {
13414c7070dbSScott Long 	if (dma->idi_tag == NULL)
13424c7070dbSScott Long 		return;
13434c7070dbSScott Long 	if (dma->idi_paddr != IF_BAD_DMA) {
13444c7070dbSScott Long 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
13454c7070dbSScott Long 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
13464c7070dbSScott Long 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
13474c7070dbSScott Long 		dma->idi_paddr = IF_BAD_DMA;
13484c7070dbSScott Long 	}
13494c7070dbSScott Long 	if (dma->idi_vaddr != NULL) {
13504c7070dbSScott Long 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
13514c7070dbSScott Long 		dma->idi_vaddr = NULL;
13524c7070dbSScott Long 	}
13534c7070dbSScott Long 	bus_dma_tag_destroy(dma->idi_tag);
13544c7070dbSScott Long 	dma->idi_tag = NULL;
13554c7070dbSScott Long }
13564c7070dbSScott Long 
13574c7070dbSScott Long void
13584c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
13594c7070dbSScott Long {
13604c7070dbSScott Long 	int i;
13614c7070dbSScott Long 	iflib_dma_info_t *dmaiter = dmalist;
13624c7070dbSScott Long 
13634c7070dbSScott Long 	for (i = 0; i < count; i++, dmaiter++)
13644c7070dbSScott Long 		iflib_dma_free(*dmaiter);
13654c7070dbSScott Long }
13664c7070dbSScott Long 
1367bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP
1368bd84f700SSean Bruno static const int iflib_started = 1;
1369bd84f700SSean Bruno #else
1370bd84f700SSean Bruno /*
1371bd84f700SSean Bruno  * We used to abuse the smp_started flag to decide if the queues have been
1372bd84f700SSean Bruno  * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()).
1373bd84f700SSean Bruno  * That gave bad races, since the SYSINIT() runs strictly after smp_started
1374bd84f700SSean Bruno  * is set.  Run a SYSINIT() strictly after that to just set a usable
1375bd84f700SSean Bruno  * completion flag.
1376bd84f700SSean Bruno  */
1377bd84f700SSean Bruno 
1378bd84f700SSean Bruno static int iflib_started;
1379bd84f700SSean Bruno 
1380bd84f700SSean Bruno static void
1381bd84f700SSean Bruno iflib_record_started(void *arg)
1382bd84f700SSean Bruno {
1383bd84f700SSean Bruno 	iflib_started = 1;
1384bd84f700SSean Bruno }
1385bd84f700SSean Bruno 
1386bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST,
1387bd84f700SSean Bruno 	iflib_record_started, NULL);
1388bd84f700SSean Bruno #endif
1389bd84f700SSean Bruno 
13904c7070dbSScott Long static int
13914c7070dbSScott Long iflib_fast_intr(void *arg)
13924c7070dbSScott Long {
13934c7070dbSScott Long 	iflib_filter_info_t info = arg;
13944c7070dbSScott Long 	struct grouptask *gtask = info->ifi_task;
139595246abbSSean Bruno 	if (!iflib_started)
139695246abbSSean Bruno 		return (FILTER_HANDLED);
139795246abbSSean Bruno 
139895246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
139995246abbSSean Bruno 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
140095246abbSSean Bruno 		return (FILTER_HANDLED);
140195246abbSSean Bruno 
140295246abbSSean Bruno 	GROUPTASK_ENQUEUE(gtask);
140395246abbSSean Bruno 	return (FILTER_HANDLED);
140495246abbSSean Bruno }
140595246abbSSean Bruno 
140695246abbSSean Bruno static int
140795246abbSSean Bruno iflib_fast_intr_rxtx(void *arg)
140895246abbSSean Bruno {
140995246abbSSean Bruno 	iflib_filter_info_t info = arg;
141095246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
141195246abbSSean Bruno 	iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
141295246abbSSean Bruno 	if_ctx_t ctx;
141395246abbSSean Bruno 	int i, cidx;
141495246abbSSean Bruno 
141595246abbSSean Bruno 	if (!iflib_started)
141695246abbSSean Bruno 		return (FILTER_HANDLED);
141795246abbSSean Bruno 
141895246abbSSean Bruno 	DBG_COUNTER_INC(fast_intrs);
141995246abbSSean Bruno 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
142095246abbSSean Bruno 		return (FILTER_HANDLED);
142195246abbSSean Bruno 
142295246abbSSean Bruno 	for (i = 0; i < rxq->ifr_ntxqirq; i++) {
142395246abbSSean Bruno 		qidx_t txqid = rxq->ifr_txqid[i];
142495246abbSSean Bruno 
1425ab2e3f79SStephen Hurd 		ctx = rxq->ifr_ctx;
1426ab2e3f79SStephen Hurd 
142795246abbSSean Bruno 		if (!ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false)) {
142895246abbSSean Bruno 			IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
142995246abbSSean Bruno 			continue;
143095246abbSSean Bruno 		}
143195246abbSSean Bruno 		GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
143295246abbSSean Bruno 	}
143395246abbSSean Bruno 	if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
143495246abbSSean Bruno 		cidx = rxq->ifr_cq_cidx;
143595246abbSSean Bruno 	else
143695246abbSSean Bruno 		cidx = rxq->ifr_fl[0].ifl_cidx;
143795246abbSSean Bruno 	if (iflib_rxd_avail(ctx, rxq, cidx, 1))
143895246abbSSean Bruno 		GROUPTASK_ENQUEUE(gtask);
143995246abbSSean Bruno 	else
144095246abbSSean Bruno 		IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
144195246abbSSean Bruno 	return (FILTER_HANDLED);
144295246abbSSean Bruno }
144395246abbSSean Bruno 
144495246abbSSean Bruno 
144595246abbSSean Bruno static int
144695246abbSSean Bruno iflib_fast_intr_ctx(void *arg)
144795246abbSSean Bruno {
144895246abbSSean Bruno 	iflib_filter_info_t info = arg;
144995246abbSSean Bruno 	struct grouptask *gtask = info->ifi_task;
14504c7070dbSScott Long 
1451bd84f700SSean Bruno 	if (!iflib_started)
14521248952aSSean Bruno 		return (FILTER_HANDLED);
14531248952aSSean Bruno 
14544c7070dbSScott Long 	DBG_COUNTER_INC(fast_intrs);
14554c7070dbSScott Long 	if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
14564c7070dbSScott Long 		return (FILTER_HANDLED);
14574c7070dbSScott Long 
14584c7070dbSScott Long 	GROUPTASK_ENQUEUE(gtask);
14594c7070dbSScott Long 	return (FILTER_HANDLED);
14604c7070dbSScott Long }
14614c7070dbSScott Long 
14624c7070dbSScott Long static int
14634c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
14644c7070dbSScott Long 	driver_filter_t filter, driver_intr_t handler, void *arg,
14654c7070dbSScott Long 				 char *name)
14664c7070dbSScott Long {
14672b2fc973SSean Bruno 	int rc, flags;
14684c7070dbSScott Long 	struct resource *res;
14692b2fc973SSean Bruno 	void *tag = NULL;
14704c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
14714c7070dbSScott Long 
14722b2fc973SSean Bruno 	flags = RF_ACTIVE;
14732b2fc973SSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
14742b2fc973SSean Bruno 		flags |= RF_SHAREABLE;
14754c7070dbSScott Long 	MPASS(rid < 512);
14764c7070dbSScott Long 	irq->ii_rid = rid;
14772b2fc973SSean Bruno 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid, flags);
14784c7070dbSScott Long 	if (res == NULL) {
14794c7070dbSScott Long 		device_printf(dev,
14804c7070dbSScott Long 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
14814c7070dbSScott Long 		return (ENOMEM);
14824c7070dbSScott Long 	}
14834c7070dbSScott Long 	irq->ii_res = res;
14844c7070dbSScott Long 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
14854c7070dbSScott Long 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
14864c7070dbSScott Long 						filter, handler, arg, &tag);
14874c7070dbSScott Long 	if (rc != 0) {
14884c7070dbSScott Long 		device_printf(dev,
14894c7070dbSScott Long 		    "failed to setup interrupt for rid %d, name %s: %d\n",
14904c7070dbSScott Long 					  rid, name ? name : "unknown", rc);
14914c7070dbSScott Long 		return (rc);
14924c7070dbSScott Long 	} else if (name)
1493f454e7ebSJohn Baldwin 		bus_describe_intr(dev, res, tag, "%s", name);
14944c7070dbSScott Long 
14954c7070dbSScott Long 	irq->ii_tag = tag;
14964c7070dbSScott Long 	return (0);
14974c7070dbSScott Long }
14984c7070dbSScott Long 
14994c7070dbSScott Long 
15004c7070dbSScott Long /*********************************************************************
15014c7070dbSScott Long  *
15024c7070dbSScott Long  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
15034c7070dbSScott Long  *  the information needed to transmit a packet on the wire. This is
15044c7070dbSScott Long  *  called only once at attach, setup is done every reset.
15054c7070dbSScott Long  *
15064c7070dbSScott Long  **********************************************************************/
15074c7070dbSScott Long 
15084c7070dbSScott Long static int
15094c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq)
15104c7070dbSScott Long {
15114c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
15124c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
15134c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
15144c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
15154c7070dbSScott Long 	int err, nsegments, ntsosegments;
15164c7070dbSScott Long 
15174c7070dbSScott Long 	nsegments = scctx->isc_tx_nsegments;
15184c7070dbSScott Long 	ntsosegments = scctx->isc_tx_tso_segments_max;
151923ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[0] > 0);
152023ac9029SStephen Hurd 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
15214c7070dbSScott Long 	MPASS(nsegments > 0);
15224c7070dbSScott Long 	MPASS(ntsosegments > 0);
15234c7070dbSScott Long 	/*
15244c7070dbSScott Long 	 * Setup DMA descriptor areas.
15254c7070dbSScott Long 	 */
15264c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
15274c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
15284c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
15294c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
15304c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
15314c7070dbSScott Long 			       sctx->isc_tx_maxsize,		/* maxsize */
15324c7070dbSScott Long 			       nsegments,	/* nsegments */
15334c7070dbSScott Long 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
15344c7070dbSScott Long 			       0,			/* flags */
15354c7070dbSScott Long 			       NULL,			/* lockfunc */
15364c7070dbSScott Long 			       NULL,			/* lockfuncarg */
15374c7070dbSScott Long 			       &txq->ift_desc_tag))) {
15384c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
15399d0a88deSDimitry Andric 		device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n",
15409d0a88deSDimitry Andric 		    (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
15414c7070dbSScott Long 		goto fail;
15424c7070dbSScott Long 	}
15434c7070dbSScott Long 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
15444c7070dbSScott Long 			       1, 0,			/* alignment, bounds */
15454c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* lowaddr */
15464c7070dbSScott Long 			       BUS_SPACE_MAXADDR,	/* highaddr */
15474c7070dbSScott Long 			       NULL, NULL,		/* filter, filterarg */
15484c7070dbSScott Long 			       scctx->isc_tx_tso_size_max,		/* maxsize */
15494c7070dbSScott Long 			       ntsosegments,	/* nsegments */
15504c7070dbSScott Long 			       scctx->isc_tx_tso_segsize_max,	/* maxsegsize */
15514c7070dbSScott Long 			       0,			/* flags */
15524c7070dbSScott Long 			       NULL,			/* lockfunc */
15534c7070dbSScott Long 			       NULL,			/* lockfuncarg */
15544c7070dbSScott Long 			       &txq->ift_tso_desc_tag))) {
15554c7070dbSScott Long 		device_printf(dev,"Unable to allocate TX TSO DMA tag: %d\n", err);
15564c7070dbSScott Long 
15574c7070dbSScott Long 		goto fail;
15584c7070dbSScott Long 	}
15594c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_flags =
1560ac2fffa4SPedro F. Giffuni 	    (uint8_t *) malloc(sizeof(uint8_t) *
1561ac2fffa4SPedro F. Giffuni 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
15624c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
15634c7070dbSScott Long 		err = ENOMEM;
15644c7070dbSScott Long 		goto fail;
15654c7070dbSScott Long 	}
15664c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_m =
1567ac2fffa4SPedro F. Giffuni 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
1568ac2fffa4SPedro F. Giffuni 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
15694c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
15704c7070dbSScott Long 		err = ENOMEM;
15714c7070dbSScott Long 		goto fail;
15724c7070dbSScott Long 	}
15734c7070dbSScott Long 
15744c7070dbSScott Long         /* Create the descriptor buffer dma maps */
157595246abbSSean Bruno #if defined(ACPI_DMAR) || (! (defined(__i386__) || defined(__amd64__)))
15764c7070dbSScott Long 	if ((ctx->ifc_flags & IFC_DMAR) == 0)
15774c7070dbSScott Long 		return (0);
15784c7070dbSScott Long 
15794c7070dbSScott Long 	if (!(txq->ift_sds.ifsd_map =
1580ac2fffa4SPedro F. Giffuni 	    (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
15814c7070dbSScott Long 		device_printf(dev, "Unable to allocate tx_buffer map memory\n");
15824c7070dbSScott Long 		err = ENOMEM;
15834c7070dbSScott Long 		goto fail;
15844c7070dbSScott Long 	}
15854c7070dbSScott Long 
158623ac9029SStephen Hurd 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
15874c7070dbSScott Long 		err = bus_dmamap_create(txq->ift_desc_tag, 0, &txq->ift_sds.ifsd_map[i]);
15884c7070dbSScott Long 		if (err != 0) {
15894c7070dbSScott Long 			device_printf(dev, "Unable to create TX DMA map\n");
15904c7070dbSScott Long 			goto fail;
15914c7070dbSScott Long 		}
15924c7070dbSScott Long 	}
15934c7070dbSScott Long #endif
15944c7070dbSScott Long 	return (0);
15954c7070dbSScott Long fail:
15964c7070dbSScott Long 	/* We free all, it handles case where we are in the middle */
15974c7070dbSScott Long 	iflib_tx_structures_free(ctx);
15984c7070dbSScott Long 	return (err);
15994c7070dbSScott Long }
16004c7070dbSScott Long 
16014c7070dbSScott Long static void
16024c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
16034c7070dbSScott Long {
16044c7070dbSScott Long 	bus_dmamap_t map;
16054c7070dbSScott Long 
16064c7070dbSScott Long 	map = NULL;
16074c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL)
16084c7070dbSScott Long 		map = txq->ift_sds.ifsd_map[i];
16094c7070dbSScott Long 	if (map != NULL) {
16104c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag, map);
16114c7070dbSScott Long 		bus_dmamap_destroy(txq->ift_desc_tag, map);
16124c7070dbSScott Long 		txq->ift_sds.ifsd_map[i] = NULL;
16134c7070dbSScott Long 	}
16144c7070dbSScott Long }
16154c7070dbSScott Long 
16164c7070dbSScott Long static void
16174c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq)
16184c7070dbSScott Long {
16194c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
16204c7070dbSScott Long 
162123ac9029SStephen Hurd 	for (int i = 0; i < txq->ift_size; i++)
16224c7070dbSScott Long 		iflib_txsd_destroy(ctx, txq, i);
16234c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
16244c7070dbSScott Long 		free(txq->ift_sds.ifsd_map, M_IFLIB);
16254c7070dbSScott Long 		txq->ift_sds.ifsd_map = NULL;
16264c7070dbSScott Long 	}
16274c7070dbSScott Long 	if (txq->ift_sds.ifsd_m != NULL) {
16284c7070dbSScott Long 		free(txq->ift_sds.ifsd_m, M_IFLIB);
16294c7070dbSScott Long 		txq->ift_sds.ifsd_m = NULL;
16304c7070dbSScott Long 	}
16314c7070dbSScott Long 	if (txq->ift_sds.ifsd_flags != NULL) {
16324c7070dbSScott Long 		free(txq->ift_sds.ifsd_flags, M_IFLIB);
16334c7070dbSScott Long 		txq->ift_sds.ifsd_flags = NULL;
16344c7070dbSScott Long 	}
16354c7070dbSScott Long 	if (txq->ift_desc_tag != NULL) {
16364c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_desc_tag);
16374c7070dbSScott Long 		txq->ift_desc_tag = NULL;
16384c7070dbSScott Long 	}
16394c7070dbSScott Long 	if (txq->ift_tso_desc_tag != NULL) {
16404c7070dbSScott Long 		bus_dma_tag_destroy(txq->ift_tso_desc_tag);
16414c7070dbSScott Long 		txq->ift_tso_desc_tag = NULL;
16424c7070dbSScott Long 	}
16434c7070dbSScott Long }
16444c7070dbSScott Long 
16454c7070dbSScott Long static void
16464c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
16474c7070dbSScott Long {
16484c7070dbSScott Long 	struct mbuf **mp;
16494c7070dbSScott Long 
16504c7070dbSScott Long 	mp = &txq->ift_sds.ifsd_m[i];
16514c7070dbSScott Long 	if (*mp == NULL)
16524c7070dbSScott Long 		return;
16534c7070dbSScott Long 
16544c7070dbSScott Long 	if (txq->ift_sds.ifsd_map != NULL) {
16554c7070dbSScott Long 		bus_dmamap_sync(txq->ift_desc_tag,
16564c7070dbSScott Long 				txq->ift_sds.ifsd_map[i],
16574c7070dbSScott Long 				BUS_DMASYNC_POSTWRITE);
16584c7070dbSScott Long 		bus_dmamap_unload(txq->ift_desc_tag,
16594c7070dbSScott Long 				  txq->ift_sds.ifsd_map[i]);
16604c7070dbSScott Long 	}
166123ac9029SStephen Hurd 	m_free(*mp);
16624c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
16634c7070dbSScott Long 	*mp = NULL;
16644c7070dbSScott Long }
16654c7070dbSScott Long 
16664c7070dbSScott Long static int
16674c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq)
16684c7070dbSScott Long {
16694c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
167023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
16714c7070dbSScott Long 	iflib_dma_info_t di;
16724c7070dbSScott Long 	int i;
16734c7070dbSScott Long 
16744c7070dbSScott Long 	/* Set number of descriptors available */
16754c7070dbSScott Long 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
167695246abbSSean Bruno 	/* XXX make configurable */
167795246abbSSean Bruno 	txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
16784c7070dbSScott Long 
16794c7070dbSScott Long 	/* Reset indices */
168095246abbSSean Bruno 	txq->ift_cidx_processed = 0;
168195246abbSSean Bruno 	txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
168223ac9029SStephen Hurd 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
16834c7070dbSScott Long 
16844c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
16854c7070dbSScott Long 		bzero((void *)di->idi_vaddr, di->idi_size);
16864c7070dbSScott Long 
16874c7070dbSScott Long 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
16884c7070dbSScott Long 	for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
16894c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
16904c7070dbSScott Long 						BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
16914c7070dbSScott Long 	return (0);
16924c7070dbSScott Long }
16934c7070dbSScott Long 
16944c7070dbSScott Long /*********************************************************************
16954c7070dbSScott Long  *
16964c7070dbSScott Long  *  Allocate memory for rx_buffer structures. Since we use one
16974c7070dbSScott Long  *  rx_buffer per received packet, the maximum number of rx_buffer's
16984c7070dbSScott Long  *  that we'll need is equal to the number of receive descriptors
16994c7070dbSScott Long  *  that we've allocated.
17004c7070dbSScott Long  *
17014c7070dbSScott Long  **********************************************************************/
17024c7070dbSScott Long static int
17034c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq)
17044c7070dbSScott Long {
17054c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
17064c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
170723ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
17084c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
17094c7070dbSScott Long 	iflib_fl_t fl;
17104c7070dbSScott Long 	int			err;
17114c7070dbSScott Long 
171223ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[0] > 0);
171323ac9029SStephen Hurd 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
17144c7070dbSScott Long 
17154c7070dbSScott Long 	fl = rxq->ifr_fl;
17164c7070dbSScott Long 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
171723ac9029SStephen Hurd 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
17184c7070dbSScott Long 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
17194c7070dbSScott Long 					 1, 0,			/* alignment, bounds */
17204c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* lowaddr */
17214c7070dbSScott Long 					 BUS_SPACE_MAXADDR,	/* highaddr */
17224c7070dbSScott Long 					 NULL, NULL,		/* filter, filterarg */
17234c7070dbSScott Long 					 sctx->isc_rx_maxsize,	/* maxsize */
17244c7070dbSScott Long 					 sctx->isc_rx_nsegments,	/* nsegments */
17254c7070dbSScott Long 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
17264c7070dbSScott Long 					 0,			/* flags */
17274c7070dbSScott Long 					 NULL,			/* lockfunc */
17284c7070dbSScott Long 					 NULL,			/* lockarg */
17294c7070dbSScott Long 					 &fl->ifl_desc_tag);
17304c7070dbSScott Long 		if (err) {
17314c7070dbSScott Long 			device_printf(dev, "%s: bus_dma_tag_create failed %d\n",
17324c7070dbSScott Long 				__func__, err);
17334c7070dbSScott Long 			goto fail;
17344c7070dbSScott Long 		}
1735e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_flags =
1736ac2fffa4SPedro F. Giffuni 		      (uint8_t *) malloc(sizeof(uint8_t) *
1737ac2fffa4SPedro F. Giffuni 					 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1738e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1739e035717eSSean Bruno 			err = ENOMEM;
1740e035717eSSean Bruno 			goto fail;
1741e035717eSSean Bruno 		}
1742e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_m =
1743ac2fffa4SPedro F. Giffuni 		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
1744ac2fffa4SPedro F. Giffuni 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1745e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1746e035717eSSean Bruno 			err = ENOMEM;
1747e035717eSSean Bruno 			goto fail;
1748e035717eSSean Bruno 		}
1749e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_cl =
1750ac2fffa4SPedro F. Giffuni 		      (caddr_t *) malloc(sizeof(caddr_t) *
1751ac2fffa4SPedro F. Giffuni 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1752e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
1753e035717eSSean Bruno 			err = ENOMEM;
1754e035717eSSean Bruno 			goto fail;
1755e035717eSSean Bruno 		}
17564c7070dbSScott Long 
1757e035717eSSean Bruno 		/* Create the descriptor buffer dma maps */
175895246abbSSean Bruno #if defined(ACPI_DMAR) || (! (defined(__i386__) || defined(__amd64__)))
1759e035717eSSean Bruno 		if ((ctx->ifc_flags & IFC_DMAR) == 0)
1760e035717eSSean Bruno 			continue;
1761e035717eSSean Bruno 
1762e035717eSSean Bruno 		if (!(fl->ifl_sds.ifsd_map =
1763ac2fffa4SPedro F. Giffuni 		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1764e035717eSSean Bruno 			device_printf(dev, "Unable to allocate tx_buffer map memory\n");
1765e035717eSSean Bruno 			err = ENOMEM;
1766e035717eSSean Bruno 			goto fail;
1767e035717eSSean Bruno 		}
1768e035717eSSean Bruno 
1769e035717eSSean Bruno 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1770e035717eSSean Bruno 			err = bus_dmamap_create(fl->ifl_desc_tag, 0, &fl->ifl_sds.ifsd_map[i]);
1771e035717eSSean Bruno 			if (err != 0) {
177295246abbSSean Bruno 				device_printf(dev, "Unable to create RX buffer DMA map\n");
17734c7070dbSScott Long 				goto fail;
17744c7070dbSScott Long 			}
17754c7070dbSScott Long 		}
1776e035717eSSean Bruno #endif
1777835809f9SSean Bruno 	}
17784c7070dbSScott Long 	return (0);
17794c7070dbSScott Long 
17804c7070dbSScott Long fail:
17814c7070dbSScott Long 	iflib_rx_structures_free(ctx);
17824c7070dbSScott Long 	return (err);
17834c7070dbSScott Long }
17844c7070dbSScott Long 
17854c7070dbSScott Long 
17864c7070dbSScott Long /*
17874c7070dbSScott Long  * Internal service routines
17884c7070dbSScott Long  */
17894c7070dbSScott Long 
17904c7070dbSScott Long struct rxq_refill_cb_arg {
17914c7070dbSScott Long 	int               error;
17924c7070dbSScott Long 	bus_dma_segment_t seg;
17934c7070dbSScott Long 	int               nseg;
17944c7070dbSScott Long };
17954c7070dbSScott Long 
17964c7070dbSScott Long static void
17974c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
17984c7070dbSScott Long {
17994c7070dbSScott Long 	struct rxq_refill_cb_arg *cb_arg = arg;
18004c7070dbSScott Long 
18014c7070dbSScott Long 	cb_arg->error = error;
18024c7070dbSScott Long 	cb_arg->seg = segs[0];
18034c7070dbSScott Long 	cb_arg->nseg = nseg;
18044c7070dbSScott Long }
18054c7070dbSScott Long 
18064c7070dbSScott Long 
18074c7070dbSScott Long #ifdef ACPI_DMAR
18084c7070dbSScott Long #define IS_DMAR(ctx) (ctx->ifc_flags & IFC_DMAR)
18094c7070dbSScott Long #else
18104c7070dbSScott Long #define IS_DMAR(ctx) (0)
18114c7070dbSScott Long #endif
18124c7070dbSScott Long 
18134c7070dbSScott Long /**
18144c7070dbSScott Long  *	rxq_refill - refill an rxq  free-buffer list
18154c7070dbSScott Long  *	@ctx: the iflib context
18164c7070dbSScott Long  *	@rxq: the free-list to refill
18174c7070dbSScott Long  *	@n: the number of new buffers to allocate
18184c7070dbSScott Long  *
18194c7070dbSScott Long  *	(Re)populate an rxq free-buffer list with up to @n new packet buffers.
18204c7070dbSScott Long  *	The caller must assure that @n does not exceed the queue's capacity.
18214c7070dbSScott Long  */
18224c7070dbSScott Long static void
18234c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
18244c7070dbSScott Long {
18254c7070dbSScott Long 	struct mbuf *m;
182687890dbaSSean Bruno 	int idx, frag_idx = fl->ifl_fragidx;
182787890dbaSSean Bruno         int pidx = fl->ifl_pidx;
1828e035717eSSean Bruno 	caddr_t cl, *sd_cl;
1829e035717eSSean Bruno 	struct mbuf **sd_m;
1830e035717eSSean Bruno 	uint8_t *sd_flags;
183195246abbSSean Bruno 	struct if_rxd_update iru;
1832e035717eSSean Bruno 	bus_dmamap_t *sd_map;
18334c7070dbSScott Long 	int n, i = 0;
18344c7070dbSScott Long 	uint64_t bus_addr;
18354c7070dbSScott Long 	int err;
1836a1b799caSStephen Hurd 	qidx_t credits;
18374c7070dbSScott Long 
1838e035717eSSean Bruno 	sd_m = fl->ifl_sds.ifsd_m;
1839e035717eSSean Bruno 	sd_map = fl->ifl_sds.ifsd_map;
1840e035717eSSean Bruno 	sd_cl = fl->ifl_sds.ifsd_cl;
1841e035717eSSean Bruno 	sd_flags = fl->ifl_sds.ifsd_flags;
1842e035717eSSean Bruno 	idx = pidx;
1843a1b799caSStephen Hurd 	credits = fl->ifl_credits;
1844e035717eSSean Bruno 
18454c7070dbSScott Long 	n  = count;
18464c7070dbSScott Long 	MPASS(n > 0);
1847a1b799caSStephen Hurd 	MPASS(credits + n <= fl->ifl_size);
18484c7070dbSScott Long 
18494c7070dbSScott Long 	if (pidx < fl->ifl_cidx)
18504c7070dbSScott Long 		MPASS(pidx + n <= fl->ifl_cidx);
1851a1b799caSStephen Hurd 	if (pidx == fl->ifl_cidx && (credits < fl->ifl_size))
18524c7070dbSScott Long 		MPASS(fl->ifl_gen == 0);
18534c7070dbSScott Long 	if (pidx > fl->ifl_cidx)
18544c7070dbSScott Long 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
18554c7070dbSScott Long 
18564c7070dbSScott Long 	DBG_COUNTER_INC(fl_refills);
18574c7070dbSScott Long 	if (n > 8)
18584c7070dbSScott Long 		DBG_COUNTER_INC(fl_refills_large);
18592d873474SStephen Hurd 	iru_init(&iru, fl->ifl_rxq, fl->ifl_id);
18604c7070dbSScott Long 	while (n--) {
18614c7070dbSScott Long 		/*
18624c7070dbSScott Long 		 * We allocate an uninitialized mbuf + cluster, mbuf is
18634c7070dbSScott Long 		 * initialized after rx.
18644c7070dbSScott Long 		 *
18654c7070dbSScott Long 		 * If the cluster is still set then we know a minimum sized packet was received
18664c7070dbSScott Long 		 */
186787890dbaSSean Bruno 		bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,  &frag_idx);
186887890dbaSSean Bruno 		if ((frag_idx < 0) || (frag_idx >= fl->ifl_size))
186987890dbaSSean Bruno                 	bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
187087890dbaSSean Bruno 		if ((cl = sd_cl[frag_idx]) == NULL) {
187187890dbaSSean Bruno                        if ((cl = sd_cl[frag_idx] = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
18724c7070dbSScott Long 				break;
18734c7070dbSScott Long #if MEMORY_LOGGING
18744c7070dbSScott Long 			fl->ifl_cl_enqueued++;
18754c7070dbSScott Long #endif
18764c7070dbSScott Long 		}
18774c7070dbSScott Long 		if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
18784c7070dbSScott Long 			break;
18794c7070dbSScott Long 		}
18804c7070dbSScott Long #if MEMORY_LOGGING
18814c7070dbSScott Long 		fl->ifl_m_enqueued++;
18824c7070dbSScott Long #endif
18834c7070dbSScott Long 
18844c7070dbSScott Long 		DBG_COUNTER_INC(rx_allocs);
18854c7070dbSScott Long #if defined(__i386__) || defined(__amd64__)
18864c7070dbSScott Long 		if (!IS_DMAR(ctx)) {
18874c7070dbSScott Long 			bus_addr = pmap_kextract((vm_offset_t)cl);
18884c7070dbSScott Long 		} else
18894c7070dbSScott Long #endif
18904c7070dbSScott Long 		{
18914c7070dbSScott Long 			struct rxq_refill_cb_arg cb_arg;
18924c7070dbSScott Long 			iflib_rxq_t q;
18934c7070dbSScott Long 
18944c7070dbSScott Long 			cb_arg.error = 0;
18954c7070dbSScott Long 			q = fl->ifl_rxq;
189695246abbSSean Bruno 			MPASS(sd_map != NULL);
189787890dbaSSean Bruno 			MPASS(sd_map[frag_idx] != NULL);
189887890dbaSSean Bruno 			err = bus_dmamap_load(fl->ifl_desc_tag, sd_map[frag_idx],
18994c7070dbSScott Long 		         cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 0);
190087890dbaSSean Bruno 			bus_dmamap_sync(fl->ifl_desc_tag, sd_map[frag_idx],
190187890dbaSSean Bruno 					BUS_DMASYNC_PREREAD);
19024c7070dbSScott Long 
19034c7070dbSScott Long 			if (err != 0 || cb_arg.error) {
19044c7070dbSScott Long 				/*
19054c7070dbSScott Long 				 * !zone_pack ?
19064c7070dbSScott Long 				 */
19074c7070dbSScott Long 				if (fl->ifl_zone == zone_pack)
19084c7070dbSScott Long 					uma_zfree(fl->ifl_zone, cl);
19094c7070dbSScott Long 				m_free(m);
19104c7070dbSScott Long 				n = 0;
19114c7070dbSScott Long 				goto done;
19124c7070dbSScott Long 			}
19134c7070dbSScott Long 			bus_addr = cb_arg.seg.ds_addr;
19144c7070dbSScott Long 		}
191587890dbaSSean Bruno                 bit_set(fl->ifl_rx_bitmap, frag_idx);
191687890dbaSSean Bruno 		sd_flags[frag_idx] |= RX_SW_DESC_INUSE;
19174c7070dbSScott Long 
191887890dbaSSean Bruno 		MPASS(sd_m[frag_idx] == NULL);
191987890dbaSSean Bruno 		sd_cl[frag_idx] = cl;
192087890dbaSSean Bruno 		sd_m[frag_idx] = m;
192187890dbaSSean Bruno 		fl->ifl_rxd_idxs[i] = frag_idx;
19224c7070dbSScott Long 		fl->ifl_bus_addrs[i] = bus_addr;
19234c7070dbSScott Long 		fl->ifl_vm_addrs[i] = cl;
1924a1b799caSStephen Hurd 		credits++;
19254c7070dbSScott Long 		i++;
1926a1b799caSStephen Hurd 		MPASS(credits <= fl->ifl_size);
1927e035717eSSean Bruno 		if (++idx == fl->ifl_size) {
19284c7070dbSScott Long 			fl->ifl_gen = 1;
1929e035717eSSean Bruno 			idx = 0;
19304c7070dbSScott Long 		}
19314c7070dbSScott Long 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
193295246abbSSean Bruno 			iru.iru_pidx = pidx;
193395246abbSSean Bruno 			iru.iru_count = i;
193495246abbSSean Bruno 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
19354c7070dbSScott Long 			i = 0;
1936e035717eSSean Bruno 			pidx = idx;
1937fa5416a8SSean Bruno 			fl->ifl_pidx = idx;
1938a1b799caSStephen Hurd 			fl->ifl_credits = credits;
193987890dbaSSean Bruno 		}
1940e035717eSSean Bruno 
19414c7070dbSScott Long 	}
19424c7070dbSScott Long done:
1943a1b799caSStephen Hurd 	if (i) {
1944a1b799caSStephen Hurd 		iru.iru_pidx = pidx;
1945a1b799caSStephen Hurd 		iru.iru_count = i;
1946a1b799caSStephen Hurd 		ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
1947a1b799caSStephen Hurd 		fl->ifl_pidx = idx;
1948a1b799caSStephen Hurd 		fl->ifl_credits = credits;
1949a1b799caSStephen Hurd 	}
19504c7070dbSScott Long 	DBG_COUNTER_INC(rxd_flush);
19514c7070dbSScott Long 	if (fl->ifl_pidx == 0)
19524c7070dbSScott Long 		pidx = fl->ifl_size - 1;
19534c7070dbSScott Long 	else
19544c7070dbSScott Long 		pidx = fl->ifl_pidx - 1;
195595246abbSSean Bruno 
195695246abbSSean Bruno 	if (sd_map)
195795246abbSSean Bruno 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
195895246abbSSean Bruno 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
19594c7070dbSScott Long 	ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
196087890dbaSSean Bruno 	fl->ifl_fragidx = frag_idx;
19614c7070dbSScott Long }
19624c7070dbSScott Long 
19634c7070dbSScott Long static __inline void
19644c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max)
19654c7070dbSScott Long {
19664c7070dbSScott Long 	/* we avoid allowing pidx to catch up with cidx as it confuses ixl */
19674c7070dbSScott Long 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
19684c7070dbSScott Long #ifdef INVARIANTS
19694c7070dbSScott Long 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
19704c7070dbSScott Long #endif
19714c7070dbSScott Long 
19724c7070dbSScott Long 	MPASS(fl->ifl_credits <= fl->ifl_size);
19734c7070dbSScott Long 	MPASS(reclaimable == delta);
19744c7070dbSScott Long 
19754c7070dbSScott Long 	if (reclaimable > 0)
19764c7070dbSScott Long 		_iflib_fl_refill(ctx, fl, min(max, reclaimable));
19774c7070dbSScott Long }
19784c7070dbSScott Long 
19794c7070dbSScott Long static void
19804c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl)
19814c7070dbSScott Long {
19824c7070dbSScott Long 	iflib_dma_info_t idi = fl->ifl_ifdi;
19834c7070dbSScott Long 	uint32_t i;
19844c7070dbSScott Long 
19854c7070dbSScott Long 	for (i = 0; i < fl->ifl_size; i++) {
1986e035717eSSean Bruno 		struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
1987e035717eSSean Bruno 		uint8_t *sd_flags = &fl->ifl_sds.ifsd_flags[i];
1988e035717eSSean Bruno 		caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
19894c7070dbSScott Long 
1990e035717eSSean Bruno 		if (*sd_flags & RX_SW_DESC_INUSE) {
1991e035717eSSean Bruno 			if (fl->ifl_sds.ifsd_map != NULL) {
1992e035717eSSean Bruno 				bus_dmamap_t sd_map = fl->ifl_sds.ifsd_map[i];
1993e035717eSSean Bruno 				bus_dmamap_unload(fl->ifl_desc_tag, sd_map);
1994a4e59607SStephen Hurd 				if (fl->ifl_rxq->ifr_ctx->ifc_in_detach)
1995e035717eSSean Bruno 					bus_dmamap_destroy(fl->ifl_desc_tag, sd_map);
19964c7070dbSScott Long 			}
1997e035717eSSean Bruno 			if (*sd_m != NULL) {
1998e035717eSSean Bruno 				m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
1999e035717eSSean Bruno 				uma_zfree(zone_mbuf, *sd_m);
2000e035717eSSean Bruno 			}
2001e035717eSSean Bruno 			if (*sd_cl != NULL)
2002e035717eSSean Bruno 				uma_zfree(fl->ifl_zone, *sd_cl);
2003e035717eSSean Bruno 			*sd_flags = 0;
20044c7070dbSScott Long 		} else {
2005e035717eSSean Bruno 			MPASS(*sd_cl == NULL);
2006e035717eSSean Bruno 			MPASS(*sd_m == NULL);
20074c7070dbSScott Long 		}
20084c7070dbSScott Long #if MEMORY_LOGGING
20094c7070dbSScott Long 		fl->ifl_m_dequeued++;
20104c7070dbSScott Long 		fl->ifl_cl_dequeued++;
20114c7070dbSScott Long #endif
2012e035717eSSean Bruno 		*sd_cl = NULL;
2013e035717eSSean Bruno 		*sd_m = NULL;
20144c7070dbSScott Long 	}
201595246abbSSean Bruno #ifdef INVARIANTS
201695246abbSSean Bruno 	for (i = 0; i < fl->ifl_size; i++) {
2017ab2e3f79SStephen Hurd 		MPASS(fl->ifl_sds.ifsd_flags[i] == 0);
201895246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
201995246abbSSean Bruno 		MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
202095246abbSSean Bruno 	}
202195246abbSSean Bruno #endif
20224c7070dbSScott Long 	/*
20234c7070dbSScott Long 	 * Reset free list values
20244c7070dbSScott Long 	 */
202587890dbaSSean Bruno 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
20264c7070dbSScott Long 	bzero(idi->idi_vaddr, idi->idi_size);
20274c7070dbSScott Long }
20284c7070dbSScott Long 
20294c7070dbSScott Long /*********************************************************************
20304c7070dbSScott Long  *
20314c7070dbSScott Long  *  Initialize a receive ring and its buffers.
20324c7070dbSScott Long  *
20334c7070dbSScott Long  **********************************************************************/
20344c7070dbSScott Long static int
20354c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl)
20364c7070dbSScott Long {
20374c7070dbSScott Long 	iflib_rxq_t rxq = fl->ifl_rxq;
20384c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
20394c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
20404c7070dbSScott Long 
20417274b2f6SStephen Hurd 	bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1);
20424c7070dbSScott Long 	/*
20434c7070dbSScott Long 	** Free current RX buffer structs and their mbufs
20444c7070dbSScott Long 	*/
20454c7070dbSScott Long 	iflib_fl_bufs_free(fl);
20464c7070dbSScott Long 	/* Now replenish the mbufs */
20474c7070dbSScott Long 	MPASS(fl->ifl_credits == 0);
20484c7070dbSScott Long 	/*
20494c7070dbSScott Long 	 * XXX don't set the max_frame_size to larger
20504c7070dbSScott Long 	 * than the hardware can handle
20514c7070dbSScott Long 	 */
20524c7070dbSScott Long 	if (sctx->isc_max_frame_size <= 2048)
20534c7070dbSScott Long 		fl->ifl_buf_size = MCLBYTES;
205495246abbSSean Bruno #ifndef CONTIGMALLOC_WORKS
205595246abbSSean Bruno 	else
205695246abbSSean Bruno 		fl->ifl_buf_size = MJUMPAGESIZE;
205795246abbSSean Bruno #else
20584c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 4096)
20594c7070dbSScott Long 		fl->ifl_buf_size = MJUMPAGESIZE;
20604c7070dbSScott Long 	else if (sctx->isc_max_frame_size <= 9216)
20614c7070dbSScott Long 		fl->ifl_buf_size = MJUM9BYTES;
20624c7070dbSScott Long 	else
20634c7070dbSScott Long 		fl->ifl_buf_size = MJUM16BYTES;
206495246abbSSean Bruno #endif
20654c7070dbSScott Long 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
20664c7070dbSScott Long 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
20674c7070dbSScott Long 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
20684c7070dbSScott Long 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
20694c7070dbSScott Long 
20704c7070dbSScott Long 
20714c7070dbSScott Long 	/* avoid pre-allocating zillions of clusters to an idle card
20724c7070dbSScott Long 	 * potentially speeding up attach
20734c7070dbSScott Long 	 */
20744c7070dbSScott Long 	_iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
20754c7070dbSScott Long 	MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
20764c7070dbSScott Long 	if (min(128, fl->ifl_size) != fl->ifl_credits)
20774c7070dbSScott Long 		return (ENOBUFS);
20784c7070dbSScott Long 	/*
20794c7070dbSScott Long 	 * handle failure
20804c7070dbSScott Long 	 */
20814c7070dbSScott Long 	MPASS(rxq != NULL);
20824c7070dbSScott Long 	MPASS(fl->ifl_ifdi != NULL);
20834c7070dbSScott Long 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
20844c7070dbSScott Long 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
20854c7070dbSScott Long 	return (0);
20864c7070dbSScott Long }
20874c7070dbSScott Long 
20884c7070dbSScott Long /*********************************************************************
20894c7070dbSScott Long  *
20904c7070dbSScott Long  *  Free receive ring data structures
20914c7070dbSScott Long  *
20924c7070dbSScott Long  **********************************************************************/
20934c7070dbSScott Long static void
20944c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq)
20954c7070dbSScott Long {
20964c7070dbSScott Long 	iflib_fl_t fl;
20974c7070dbSScott Long 	int i;
20984c7070dbSScott Long 
20994c7070dbSScott Long 	if (rxq->ifr_fl != NULL) {
21004c7070dbSScott Long 		for (i = 0; i < rxq->ifr_nfl; i++) {
21014c7070dbSScott Long 			fl = &rxq->ifr_fl[i];
21024c7070dbSScott Long 			if (fl->ifl_desc_tag != NULL) {
21034c7070dbSScott Long 				bus_dma_tag_destroy(fl->ifl_desc_tag);
21044c7070dbSScott Long 				fl->ifl_desc_tag = NULL;
21054c7070dbSScott Long 			}
2106e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_m, M_IFLIB);
2107e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_cl, M_IFLIB);
2108e035717eSSean Bruno 			/* XXX destroy maps first */
2109e035717eSSean Bruno 			free(fl->ifl_sds.ifsd_map, M_IFLIB);
2110e035717eSSean Bruno 			fl->ifl_sds.ifsd_m = NULL;
2111e035717eSSean Bruno 			fl->ifl_sds.ifsd_cl = NULL;
2112e035717eSSean Bruno 			fl->ifl_sds.ifsd_map = NULL;
21134c7070dbSScott Long 		}
21144c7070dbSScott Long 		free(rxq->ifr_fl, M_IFLIB);
21154c7070dbSScott Long 		rxq->ifr_fl = NULL;
21164c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
21174c7070dbSScott Long 	}
21184c7070dbSScott Long }
21194c7070dbSScott Long 
21204c7070dbSScott Long /*
21214c7070dbSScott Long  * MI independent logic
21224c7070dbSScott Long  *
21234c7070dbSScott Long  */
21244c7070dbSScott Long static void
21254c7070dbSScott Long iflib_timer(void *arg)
21264c7070dbSScott Long {
2127ab2e3f79SStephen Hurd 	iflib_txq_t txq = arg;
21284c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
2129ab2e3f79SStephen Hurd 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
21304c7070dbSScott Long 
21314c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
21324c7070dbSScott Long 		return;
21334c7070dbSScott Long 	/*
21344c7070dbSScott Long 	** Check on the state of the TX queue(s), this
21354c7070dbSScott Long 	** can be done without the lock because its RO
21364c7070dbSScott Long 	** and the HUNG state will be static if set.
21374c7070dbSScott Long 	*/
2138ab2e3f79SStephen Hurd 	IFDI_TIMER(ctx, txq->ift_id);
2139ab2e3f79SStephen Hurd 	if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
2140ab2e3f79SStephen Hurd 	    ((txq->ift_cleaned_prev == txq->ift_cleaned) ||
2141ab2e3f79SStephen Hurd 	     (sctx->isc_pause_frames == 0)))
2142ab2e3f79SStephen Hurd 		goto hung;
2143a9693502SSean Bruno 
2144ab2e3f79SStephen Hurd 	if (ifmp_ring_is_stalled(txq->ift_br))
2145ab2e3f79SStephen Hurd 		txq->ift_qstatus = IFLIB_QUEUE_HUNG;
2146ab2e3f79SStephen Hurd 	txq->ift_cleaned_prev = txq->ift_cleaned;
2147ab2e3f79SStephen Hurd 	/* handle any laggards */
2148ab2e3f79SStephen Hurd 	if (txq->ift_db_pending)
2149ab2e3f79SStephen Hurd 		GROUPTASK_ENQUEUE(&txq->ift_task);
2150a9693502SSean Bruno 
2151ab2e3f79SStephen Hurd 	sctx->isc_pause_frames = 0;
2152d300df01SStephen Hurd 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
2153ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
2154ab2e3f79SStephen Hurd 	return;
2155ab2e3f79SStephen Hurd  hung:
2156ab2e3f79SStephen Hurd 	device_printf(ctx->ifc_dev,  "TX(%d) desc avail = %d, pidx = %d\n",
2157ab2e3f79SStephen Hurd 				  txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
21587b610b60SSean Bruno 	STATE_LOCK(ctx);
21597b610b60SSean Bruno 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
21607b610b60SSean Bruno 	ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET);
2161ab2e3f79SStephen Hurd 	iflib_admin_intr_deferred(ctx);
21627b610b60SSean Bruno 	STATE_UNLOCK(ctx);
21634c7070dbSScott Long }
21644c7070dbSScott Long 
21654c7070dbSScott Long static void
21664c7070dbSScott Long iflib_init_locked(if_ctx_t ctx)
21674c7070dbSScott Long {
21684c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
21691248952aSSean Bruno 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
21704c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
21714c7070dbSScott Long 	iflib_fl_t fl;
21724c7070dbSScott Long 	iflib_txq_t txq;
21734c7070dbSScott Long 	iflib_rxq_t rxq;
2174ab2e3f79SStephen Hurd 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
21754c7070dbSScott Long 
21764c7070dbSScott Long 
21774c7070dbSScott Long 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
21784c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
21794c7070dbSScott Long 
21801248952aSSean Bruno 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
21811248952aSSean Bruno 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
21824c7070dbSScott Long 	/* Set hardware offload abilities */
21834c7070dbSScott Long 	if_clearhwassist(ifp);
21844c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
21851248952aSSean Bruno 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
21864c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
21871248952aSSean Bruno 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
21884c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO4)
21894c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
21904c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_TSO6)
21914c7070dbSScott Long 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
21924c7070dbSScott Long 
21934c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
21944c7070dbSScott Long 		CALLOUT_LOCK(txq);
21954c7070dbSScott Long 		callout_stop(&txq->ift_timer);
21964c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
21974c7070dbSScott Long 		iflib_netmap_txq_init(ctx, txq);
21984c7070dbSScott Long 	}
219923ac9029SStephen Hurd #ifdef INVARIANTS
220023ac9029SStephen Hurd 	i = if_getdrvflags(ifp);
220123ac9029SStephen Hurd #endif
22024c7070dbSScott Long 	IFDI_INIT(ctx);
220323ac9029SStephen Hurd 	MPASS(if_getdrvflags(ifp) == i);
22044c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
220595246abbSSean Bruno 		/* XXX this should really be done on a per-queue basis */
2206d0d0ad0aSStephen Hurd 		if (if_getcapenable(ifp) & IFCAP_NETMAP) {
2207d0d0ad0aSStephen Hurd 			MPASS(rxq->ifr_id == i);
2208d0d0ad0aSStephen Hurd 			iflib_netmap_rxq_init(ctx, rxq);
220995246abbSSean Bruno 			continue;
2210d0d0ad0aSStephen Hurd 		}
22114c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
22124c7070dbSScott Long 			if (iflib_fl_setup(fl)) {
22134c7070dbSScott Long 				device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n");
22144c7070dbSScott Long 				goto done;
22154c7070dbSScott Long 			}
22164c7070dbSScott Long 		}
22174c7070dbSScott Long 	}
22184c7070dbSScott Long 	done:
22194c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
22204c7070dbSScott Long 	IFDI_INTR_ENABLE(ctx);
22214c7070dbSScott Long 	txq = ctx->ifc_txqs;
22224c7070dbSScott Long 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
2223ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
2224ab2e3f79SStephen Hurd 			txq->ift_timer.c_cpu);
22254c7070dbSScott Long }
22264c7070dbSScott Long 
22274c7070dbSScott Long static int
22284c7070dbSScott Long iflib_media_change(if_t ifp)
22294c7070dbSScott Long {
22304c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
22314c7070dbSScott Long 	int err;
22324c7070dbSScott Long 
22334c7070dbSScott Long 	CTX_LOCK(ctx);
22344c7070dbSScott Long 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
22354c7070dbSScott Long 		iflib_init_locked(ctx);
22364c7070dbSScott Long 	CTX_UNLOCK(ctx);
22374c7070dbSScott Long 	return (err);
22384c7070dbSScott Long }
22394c7070dbSScott Long 
22404c7070dbSScott Long static void
22414c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
22424c7070dbSScott Long {
22434c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
22444c7070dbSScott Long 
22454c7070dbSScott Long 	CTX_LOCK(ctx);
2246ab2e3f79SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
22474c7070dbSScott Long 	IFDI_MEDIA_STATUS(ctx, ifmr);
22484c7070dbSScott Long 	CTX_UNLOCK(ctx);
22494c7070dbSScott Long }
22504c7070dbSScott Long 
22514c7070dbSScott Long static void
22524c7070dbSScott Long iflib_stop(if_ctx_t ctx)
22534c7070dbSScott Long {
22544c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
22554c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
22564c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
22574c7070dbSScott Long 	iflib_dma_info_t di;
22584c7070dbSScott Long 	iflib_fl_t fl;
22594c7070dbSScott Long 	int i, j;
22604c7070dbSScott Long 
22614c7070dbSScott Long 	/* Tell the stack that the interface is no longer active */
22624c7070dbSScott Long 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
22634c7070dbSScott Long 
22644c7070dbSScott Long 	IFDI_INTR_DISABLE(ctx);
2265ab2e3f79SStephen Hurd 	DELAY(1000);
2266da69b8f9SSean Bruno 	IFDI_STOP(ctx);
2267ab2e3f79SStephen Hurd 	DELAY(1000);
22684c7070dbSScott Long 
2269da69b8f9SSean Bruno 	iflib_debug_reset();
22704c7070dbSScott Long 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
22714c7070dbSScott Long 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
22724c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
22734c7070dbSScott Long 
2274226fb85dSStephen Hurd 		CALLOUT_LOCK(txq);
2275226fb85dSStephen Hurd 		callout_stop(&txq->ift_timer);
2276226fb85dSStephen Hurd 		CALLOUT_UNLOCK(txq);
2277226fb85dSStephen Hurd 
22784c7070dbSScott Long 		/* clean any enqueued buffers */
2279da69b8f9SSean Bruno 		iflib_ifmp_purge(txq);
22804c7070dbSScott Long 		/* Free any existing tx buffers. */
228123ac9029SStephen Hurd 		for (j = 0; j < txq->ift_size; j++) {
22824c7070dbSScott Long 			iflib_txsd_free(ctx, txq, j);
22834c7070dbSScott Long 		}
2284ab2e3f79SStephen Hurd 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2285ab2e3f79SStephen Hurd 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
22864c7070dbSScott Long 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
22874c7070dbSScott Long 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
2288ab2e3f79SStephen Hurd 		txq->ift_pullups = 0;
228995246abbSSean Bruno 		ifmp_ring_reset_stats(txq->ift_br);
22904c7070dbSScott Long 		for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwtxqs; j++, di++)
22914c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
22924c7070dbSScott Long 	}
22934c7070dbSScott Long 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
22944c7070dbSScott Long 		/* make sure all transmitters have completed before proceeding XXX */
22954c7070dbSScott Long 
229666def526SMateusz Guzik 		for (j = 0, di = rxq->ifr_ifdi; j < rxq->ifr_nfl; j++, di++)
22974c7070dbSScott Long 			bzero((void *)di->idi_vaddr, di->idi_size);
22984c7070dbSScott Long 		/* also resets the free lists pidx/cidx */
22994c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
23004c7070dbSScott Long 			iflib_fl_bufs_free(fl);
23014c7070dbSScott Long 	}
23024c7070dbSScott Long }
23034c7070dbSScott Long 
230495246abbSSean Bruno static inline caddr_t
230595246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx)
230695246abbSSean Bruno {
230795246abbSSean Bruno 	qidx_t size;
230895246abbSSean Bruno 	int nrxd;
230995246abbSSean Bruno 	caddr_t start, end, cur, next;
231095246abbSSean Bruno 
231195246abbSSean Bruno 	nrxd = fl->ifl_size;
231295246abbSSean Bruno 	size = fl->ifl_rxd_size;
231395246abbSSean Bruno 	start = fl->ifl_ifdi->idi_vaddr;
231495246abbSSean Bruno 
231595246abbSSean Bruno 	if (__predict_false(size == 0))
231695246abbSSean Bruno 		return (start);
231795246abbSSean Bruno 	cur = start + size*cidx;
231895246abbSSean Bruno 	end = start + size*nrxd;
231995246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
232095246abbSSean Bruno 	return (next < end ? next : start);
232195246abbSSean Bruno }
232295246abbSSean Bruno 
2323e035717eSSean Bruno static inline void
2324e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx)
2325e035717eSSean Bruno {
2326e035717eSSean Bruno 	int nextptr;
2327e035717eSSean Bruno 	int nrxd = fl->ifl_size;
232895246abbSSean Bruno 	caddr_t next_rxd;
232995246abbSSean Bruno 
2330e035717eSSean Bruno 
2331e035717eSSean Bruno 	nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2332e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2333e035717eSSean Bruno 	prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
233495246abbSSean Bruno 	next_rxd = calc_next_rxd(fl, cidx);
233595246abbSSean Bruno 	prefetch(next_rxd);
2336e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2337e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2338e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2339e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2340e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2341e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2342e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2343e035717eSSean Bruno 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2344e035717eSSean Bruno }
2345e035717eSSean Bruno 
2346e035717eSSean Bruno static void
234795246abbSSean Bruno rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int unload, if_rxsd_t sd)
23484c7070dbSScott Long {
23494c7070dbSScott Long 	int flid, cidx;
2350e035717eSSean Bruno 	bus_dmamap_t map;
23514c7070dbSScott Long 	iflib_fl_t fl;
23524c7070dbSScott Long 	iflib_dma_info_t di;
2353e035717eSSean Bruno 	int next;
23544c7070dbSScott Long 
235595246abbSSean Bruno 	map = NULL;
23564c7070dbSScott Long 	flid = irf->irf_flid;
23574c7070dbSScott Long 	cidx = irf->irf_idx;
23584c7070dbSScott Long 	fl = &rxq->ifr_fl[flid];
235995246abbSSean Bruno 	sd->ifsd_fl = fl;
236095246abbSSean Bruno 	sd->ifsd_cidx = cidx;
236195246abbSSean Bruno 	sd->ifsd_m = &fl->ifl_sds.ifsd_m[cidx];
236295246abbSSean Bruno 	sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
23634c7070dbSScott Long 	fl->ifl_credits--;
23644c7070dbSScott Long #if MEMORY_LOGGING
23654c7070dbSScott Long 	fl->ifl_m_dequeued++;
23664c7070dbSScott Long #endif
236795246abbSSean Bruno 	if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
2368e035717eSSean Bruno 		prefetch_pkts(fl, cidx);
2369e035717eSSean Bruno 	if (fl->ifl_sds.ifsd_map != NULL) {
2370e035717eSSean Bruno 		next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2371e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_map[next]);
2372e035717eSSean Bruno 		map = fl->ifl_sds.ifsd_map[cidx];
23734c7070dbSScott Long 		di = fl->ifl_ifdi;
2374e035717eSSean Bruno 		next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1);
2375e035717eSSean Bruno 		prefetch(&fl->ifl_sds.ifsd_flags[next]);
23764c7070dbSScott Long 		bus_dmamap_sync(di->idi_tag, di->idi_map,
23774c7070dbSScott Long 				BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
23784c7070dbSScott Long 
23794c7070dbSScott Long 	/* not valid assert if bxe really does SGE from non-contiguous elements */
23804c7070dbSScott Long 		MPASS(fl->ifl_cidx == cidx);
23814c7070dbSScott Long 		if (unload)
2382e035717eSSean Bruno 			bus_dmamap_unload(fl->ifl_desc_tag, map);
2383e035717eSSean Bruno 	}
238495246abbSSean Bruno 	fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1);
238595246abbSSean Bruno 	if (__predict_false(fl->ifl_cidx == 0))
23864c7070dbSScott Long 		fl->ifl_gen = 0;
238795246abbSSean Bruno 	if (map != NULL)
238895246abbSSean Bruno 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
238995246abbSSean Bruno 			BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
239087890dbaSSean Bruno         bit_clear(fl->ifl_rx_bitmap, cidx);
23914c7070dbSScott Long }
23924c7070dbSScott Long 
23934c7070dbSScott Long static struct mbuf *
239495246abbSSean Bruno assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd)
23954c7070dbSScott Long {
239695246abbSSean Bruno 	int i, padlen , flags;
239795246abbSSean Bruno 	struct mbuf *m, *mh, *mt;
239895246abbSSean Bruno 	caddr_t cl;
23994c7070dbSScott Long 
24004c7070dbSScott Long 	i = 0;
240123ac9029SStephen Hurd 	mh = NULL;
24024c7070dbSScott Long 	do {
240395246abbSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[i], TRUE, sd);
24044c7070dbSScott Long 
240595246abbSSean Bruno 		MPASS(*sd->ifsd_cl != NULL);
240695246abbSSean Bruno 		MPASS(*sd->ifsd_m != NULL);
240723ac9029SStephen Hurd 
240823ac9029SStephen Hurd 		/* Don't include zero-length frags */
240923ac9029SStephen Hurd 		if (ri->iri_frags[i].irf_len == 0) {
241023ac9029SStephen Hurd 			/* XXX we can save the cluster here, but not the mbuf */
241195246abbSSean Bruno 			m_init(*sd->ifsd_m, M_NOWAIT, MT_DATA, 0);
241295246abbSSean Bruno 			m_free(*sd->ifsd_m);
241395246abbSSean Bruno 			*sd->ifsd_m = NULL;
241423ac9029SStephen Hurd 			continue;
241523ac9029SStephen Hurd 		}
241695246abbSSean Bruno 		m = *sd->ifsd_m;
241795246abbSSean Bruno 		*sd->ifsd_m = NULL;
241823ac9029SStephen Hurd 		if (mh == NULL) {
24194c7070dbSScott Long 			flags = M_PKTHDR|M_EXT;
24204c7070dbSScott Long 			mh = mt = m;
24214c7070dbSScott Long 			padlen = ri->iri_pad;
24224c7070dbSScott Long 		} else {
24234c7070dbSScott Long 			flags = M_EXT;
24244c7070dbSScott Long 			mt->m_next = m;
24254c7070dbSScott Long 			mt = m;
24264c7070dbSScott Long 			/* assuming padding is only on the first fragment */
24274c7070dbSScott Long 			padlen = 0;
24284c7070dbSScott Long 		}
242995246abbSSean Bruno 		cl = *sd->ifsd_cl;
243095246abbSSean Bruno 		*sd->ifsd_cl = NULL;
24314c7070dbSScott Long 
24324c7070dbSScott Long 		/* Can these two be made one ? */
24334c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, flags);
243495246abbSSean Bruno 		m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
24354c7070dbSScott Long 		/*
24364c7070dbSScott Long 		 * These must follow m_init and m_cljset
24374c7070dbSScott Long 		 */
24384c7070dbSScott Long 		m->m_data += padlen;
24394c7070dbSScott Long 		ri->iri_len -= padlen;
244023ac9029SStephen Hurd 		m->m_len = ri->iri_frags[i].irf_len;
24414c7070dbSScott Long 	} while (++i < ri->iri_nfrags);
24424c7070dbSScott Long 
24434c7070dbSScott Long 	return (mh);
24444c7070dbSScott Long }
24454c7070dbSScott Long 
24464c7070dbSScott Long /*
24474c7070dbSScott Long  * Process one software descriptor
24484c7070dbSScott Long  */
24494c7070dbSScott Long static struct mbuf *
24504c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
24514c7070dbSScott Long {
245295246abbSSean Bruno 	struct if_rxsd sd;
24534c7070dbSScott Long 	struct mbuf *m;
24544c7070dbSScott Long 
24554c7070dbSScott Long 	/* should I merge this back in now that the two paths are basically duplicated? */
245623ac9029SStephen Hurd 	if (ri->iri_nfrags == 1 &&
245718628b74SMark Johnston 	    ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) {
245895246abbSSean Bruno 		rxd_frag_to_sd(rxq, &ri->iri_frags[0], FALSE, &sd);
245995246abbSSean Bruno 		m = *sd.ifsd_m;
246095246abbSSean Bruno 		*sd.ifsd_m = NULL;
24614c7070dbSScott Long 		m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
246295246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
246395246abbSSean Bruno 		if (!IP_ALIGNED(m))
246495246abbSSean Bruno 			m->m_data += 2;
246595246abbSSean Bruno #endif
246695246abbSSean Bruno 		memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
246723ac9029SStephen Hurd 		m->m_len = ri->iri_frags[0].irf_len;
24684c7070dbSScott Long        } else {
246995246abbSSean Bruno 		m = assemble_segments(rxq, ri, &sd);
24704c7070dbSScott Long 	}
24714c7070dbSScott Long 	m->m_pkthdr.len = ri->iri_len;
24724c7070dbSScott Long 	m->m_pkthdr.rcvif = ri->iri_ifp;
24734c7070dbSScott Long 	m->m_flags |= ri->iri_flags;
24744c7070dbSScott Long 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
24754c7070dbSScott Long 	m->m_pkthdr.flowid = ri->iri_flowid;
24764c7070dbSScott Long 	M_HASHTYPE_SET(m, ri->iri_rsstype);
24774c7070dbSScott Long 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
24784c7070dbSScott Long 	m->m_pkthdr.csum_data = ri->iri_csum_data;
24794c7070dbSScott Long 	return (m);
24804c7070dbSScott Long }
24814c7070dbSScott Long 
248235e4e998SStephen Hurd #if defined(INET6) || defined(INET)
2483fe1bcadaSStephen Hurd static void
2484fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6)
2485fe1bcadaSStephen Hurd {
2486fe1bcadaSStephen Hurd 	CURVNET_SET(lc->ifp->if_vnet);
2487fe1bcadaSStephen Hurd #if defined(INET6)
2488fe1bcadaSStephen Hurd 	*v6 = VNET(ip6_forwarding);
2489fe1bcadaSStephen Hurd #endif
2490fe1bcadaSStephen Hurd #if defined(INET)
2491fe1bcadaSStephen Hurd 	*v4 = VNET(ipforwarding);
2492fe1bcadaSStephen Hurd #endif
2493fe1bcadaSStephen Hurd 	CURVNET_RESTORE();
2494fe1bcadaSStephen Hurd }
2495fe1bcadaSStephen Hurd 
249635e4e998SStephen Hurd /*
249735e4e998SStephen Hurd  * Returns true if it's possible this packet could be LROed.
249835e4e998SStephen Hurd  * if it returns false, it is guaranteed that tcp_lro_rx()
249935e4e998SStephen Hurd  * would not return zero.
250035e4e998SStephen Hurd  */
250135e4e998SStephen Hurd static bool
2502fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding)
250335e4e998SStephen Hurd {
250435e4e998SStephen Hurd 	struct ether_header *eh;
250535e4e998SStephen Hurd 	uint16_t eh_type;
250635e4e998SStephen Hurd 
250735e4e998SStephen Hurd 	eh = mtod(m, struct ether_header *);
250835e4e998SStephen Hurd 	eh_type = ntohs(eh->ether_type);
250935e4e998SStephen Hurd 	switch (eh_type) {
2510abec4724SSean Bruno #if defined(INET6)
251135e4e998SStephen Hurd 		case ETHERTYPE_IPV6:
2512fe1bcadaSStephen Hurd 			return !v6_forwarding;
2513abec4724SSean Bruno #endif
2514abec4724SSean Bruno #if defined (INET)
251535e4e998SStephen Hurd 		case ETHERTYPE_IP:
2516fe1bcadaSStephen Hurd 			return !v4_forwarding;
2517abec4724SSean Bruno #endif
251835e4e998SStephen Hurd 	}
251935e4e998SStephen Hurd 
252035e4e998SStephen Hurd 	return false;
252135e4e998SStephen Hurd }
2522fe1bcadaSStephen Hurd #else
2523fe1bcadaSStephen Hurd static void
2524fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused)
2525fe1bcadaSStephen Hurd {
2526fe1bcadaSStephen Hurd }
252735e4e998SStephen Hurd #endif
252835e4e998SStephen Hurd 
25294c7070dbSScott Long static bool
253095246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
25314c7070dbSScott Long {
25324c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
25334c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
253423ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
25354c7070dbSScott Long 	int avail, i;
253695246abbSSean Bruno 	qidx_t *cidxp;
25374c7070dbSScott Long 	struct if_rxd_info ri;
25384c7070dbSScott Long 	int err, budget_left, rx_bytes, rx_pkts;
25394c7070dbSScott Long 	iflib_fl_t fl;
25404c7070dbSScott Long 	struct ifnet *ifp;
25414c7070dbSScott Long 	int lro_enabled;
254235e4e998SStephen Hurd 	bool lro_possible = false;
2543fe1bcadaSStephen Hurd 	bool v4_forwarding, v6_forwarding;
254495246abbSSean Bruno 
25454c7070dbSScott Long 	/*
25464c7070dbSScott Long 	 * XXX early demux data packets so that if_input processing only handles
25474c7070dbSScott Long 	 * acks in interrupt context
25484c7070dbSScott Long 	 */
254920f63282SStephen Hurd 	struct mbuf *m, *mh, *mt, *mf;
25504c7070dbSScott Long 
255195246abbSSean Bruno 	ifp = ctx->ifc_ifp;
25524c7070dbSScott Long 	mh = mt = NULL;
25534c7070dbSScott Long 	MPASS(budget > 0);
25544c7070dbSScott Long 	rx_pkts	= rx_bytes = 0;
255523ac9029SStephen Hurd 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
25564c7070dbSScott Long 		cidxp = &rxq->ifr_cq_cidx;
25574c7070dbSScott Long 	else
25584c7070dbSScott Long 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
255923ac9029SStephen Hurd 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
25604c7070dbSScott Long 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
25614c7070dbSScott Long 			__iflib_fl_refill_lt(ctx, fl, budget + 8);
25624c7070dbSScott Long 		DBG_COUNTER_INC(rx_unavail);
25634c7070dbSScott Long 		return (false);
25644c7070dbSScott Long 	}
25654c7070dbSScott Long 
25664c7070dbSScott Long 	for (budget_left = budget; (budget_left > 0) && (avail > 0); budget_left--, avail--) {
25674c7070dbSScott Long 		if (__predict_false(!CTX_ACTIVE(ctx))) {
25684c7070dbSScott Long 			DBG_COUNTER_INC(rx_ctx_inactive);
25694c7070dbSScott Long 			break;
25704c7070dbSScott Long 		}
25714c7070dbSScott Long 		/*
25724c7070dbSScott Long 		 * Reset client set fields to their default values
25734c7070dbSScott Long 		 */
257495246abbSSean Bruno 		rxd_info_zero(&ri);
25754c7070dbSScott Long 		ri.iri_qsidx = rxq->ifr_id;
25764c7070dbSScott Long 		ri.iri_cidx = *cidxp;
257795246abbSSean Bruno 		ri.iri_ifp = ifp;
25784c7070dbSScott Long 		ri.iri_frags = rxq->ifr_frags;
25794c7070dbSScott Long 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
25804c7070dbSScott Long 
258195246abbSSean Bruno 		if (err)
258295246abbSSean Bruno 			goto err;
258323ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
258423ac9029SStephen Hurd 			*cidxp = ri.iri_cidx;
258523ac9029SStephen Hurd 			/* Update our consumer index */
258695246abbSSean Bruno 			/* XXX NB: shurd - check if this is still safe */
258723ac9029SStephen Hurd 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) {
258823ac9029SStephen Hurd 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
25894c7070dbSScott Long 				rxq->ifr_cq_gen = 0;
25904c7070dbSScott Long 			}
25914c7070dbSScott Long 			/* was this only a completion queue message? */
25924c7070dbSScott Long 			if (__predict_false(ri.iri_nfrags == 0))
25934c7070dbSScott Long 				continue;
25944c7070dbSScott Long 		}
25954c7070dbSScott Long 		MPASS(ri.iri_nfrags != 0);
25964c7070dbSScott Long 		MPASS(ri.iri_len != 0);
25974c7070dbSScott Long 
25984c7070dbSScott Long 		/* will advance the cidx on the corresponding free lists */
25994c7070dbSScott Long 		m = iflib_rxd_pkt_get(rxq, &ri);
26004c7070dbSScott Long 		if (avail == 0 && budget_left)
260123ac9029SStephen Hurd 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
26024c7070dbSScott Long 
26034c7070dbSScott Long 		if (__predict_false(m == NULL)) {
26044c7070dbSScott Long 			DBG_COUNTER_INC(rx_mbuf_null);
26054c7070dbSScott Long 			continue;
26064c7070dbSScott Long 		}
26074c7070dbSScott Long 		/* imm_pkt: -- cxgb */
26084c7070dbSScott Long 		if (mh == NULL)
26094c7070dbSScott Long 			mh = mt = m;
26104c7070dbSScott Long 		else {
26114c7070dbSScott Long 			mt->m_nextpkt = m;
26124c7070dbSScott Long 			mt = m;
26134c7070dbSScott Long 		}
26144c7070dbSScott Long 	}
26154c7070dbSScott Long 	/* make sure that we can refill faster than drain */
26164c7070dbSScott Long 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2617ab2e3f79SStephen Hurd 		__iflib_fl_refill_lt(ctx, fl, budget + 8);
26184c7070dbSScott Long 
26194c7070dbSScott Long 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
2620fe1bcadaSStephen Hurd 	if (lro_enabled)
2621fe1bcadaSStephen Hurd 		iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding);
262220f63282SStephen Hurd 	mt = mf = NULL;
26234c7070dbSScott Long 	while (mh != NULL) {
26244c7070dbSScott Long 		m = mh;
26254c7070dbSScott Long 		mh = mh->m_nextpkt;
26264c7070dbSScott Long 		m->m_nextpkt = NULL;
262795246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
262895246abbSSean Bruno 		if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
262995246abbSSean Bruno 			continue;
263095246abbSSean Bruno #endif
26314c7070dbSScott Long 		rx_bytes += m->m_pkthdr.len;
26324c7070dbSScott Long 		rx_pkts++;
2633aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
263435e4e998SStephen Hurd 		if (lro_enabled) {
263535e4e998SStephen Hurd 			if (!lro_possible) {
2636fe1bcadaSStephen Hurd 				lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding);
263735e4e998SStephen Hurd 				if (lro_possible && mf != NULL) {
263835e4e998SStephen Hurd 					ifp->if_input(ifp, mf);
263935e4e998SStephen Hurd 					DBG_COUNTER_INC(rx_if_input);
264035e4e998SStephen Hurd 					mt = mf = NULL;
264135e4e998SStephen Hurd 				}
264235e4e998SStephen Hurd 			}
264325ac1dd5SStephen Hurd 			if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) ==
264425ac1dd5SStephen Hurd 			    (CSUM_L4_CALC|CSUM_L4_VALID)) {
264535e4e998SStephen Hurd 				if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
26464c7070dbSScott Long 					continue;
264720f63282SStephen Hurd 			}
264825ac1dd5SStephen Hurd 		}
2649aaeb188aSBjoern A. Zeeb #endif
265035e4e998SStephen Hurd 		if (lro_possible) {
265135e4e998SStephen Hurd 			ifp->if_input(ifp, m);
265235e4e998SStephen Hurd 			DBG_COUNTER_INC(rx_if_input);
265335e4e998SStephen Hurd 			continue;
265435e4e998SStephen Hurd 		}
265535e4e998SStephen Hurd 
265635e4e998SStephen Hurd 		if (mf == NULL)
265735e4e998SStephen Hurd 			mf = m;
265820f63282SStephen Hurd 		if (mt != NULL)
265920f63282SStephen Hurd 			mt->m_nextpkt = m;
266020f63282SStephen Hurd 		mt = m;
266120f63282SStephen Hurd 	}
266220f63282SStephen Hurd 	if (mf != NULL) {
266320f63282SStephen Hurd 		ifp->if_input(ifp, mf);
26644c7070dbSScott Long 		DBG_COUNTER_INC(rx_if_input);
26654c7070dbSScott Long 	}
266623ac9029SStephen Hurd 
26674c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
26684c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
26694c7070dbSScott Long 
26704c7070dbSScott Long 	/*
26714c7070dbSScott Long 	 * Flush any outstanding LRO work
26724c7070dbSScott Long 	 */
2673aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
267423ac9029SStephen Hurd 	tcp_lro_flush_all(&rxq->ifr_lc);
2675aaeb188aSBjoern A. Zeeb #endif
2676ab2e3f79SStephen Hurd 	if (avail)
2677ab2e3f79SStephen Hurd 		return true;
2678ab2e3f79SStephen Hurd 	return (iflib_rxd_avail(ctx, rxq, *cidxp, 1));
267995246abbSSean Bruno err:
26807b610b60SSean Bruno 	STATE_LOCK(ctx);
2681ab2e3f79SStephen Hurd 	ctx->ifc_flags |= IFC_DO_RESET;
2682ab2e3f79SStephen Hurd 	iflib_admin_intr_deferred(ctx);
26837b610b60SSean Bruno 	STATE_UNLOCK(ctx);
268495246abbSSean Bruno 	return (false);
268595246abbSSean Bruno }
268695246abbSSean Bruno 
268795246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1)
268895246abbSSean Bruno static inline qidx_t
268995246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
269095246abbSSean Bruno {
269195246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
269295246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
269395246abbSSean Bruno 	if (in_use > 4*minthresh)
269495246abbSSean Bruno 		return (notify_count);
269595246abbSSean Bruno 	if (in_use > 2*minthresh)
269695246abbSSean Bruno 		return (notify_count >> 1);
269795246abbSSean Bruno 	if (in_use > minthresh)
269895246abbSSean Bruno 		return (notify_count >> 3);
269995246abbSSean Bruno 	return (0);
270095246abbSSean Bruno }
270195246abbSSean Bruno 
270295246abbSSean Bruno static inline qidx_t
270395246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq)
270495246abbSSean Bruno {
270595246abbSSean Bruno 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
270695246abbSSean Bruno 	qidx_t minthresh = txq->ift_size / 8;
270795246abbSSean Bruno 	if (txq->ift_in_use > 4*minthresh)
270895246abbSSean Bruno 		return (notify_count);
270995246abbSSean Bruno 	if (txq->ift_in_use > 2*minthresh)
271095246abbSSean Bruno 		return (notify_count >> 1);
271195246abbSSean Bruno 	if (txq->ift_in_use > minthresh)
271295246abbSSean Bruno 		return (notify_count >> 2);
27132b2fc973SSean Bruno 	return (2);
27144c7070dbSScott Long }
27154c7070dbSScott Long 
27164c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
27174c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
271895246abbSSean Bruno 
271995246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
272095246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
272123ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
27224c7070dbSScott Long 
272395246abbSSean Bruno /* forward compatibility for cxgb */
272495246abbSSean Bruno #define FIRST_QSET(ctx) 0
272595246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
272695246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
272795246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
272895246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
272995246abbSSean Bruno 
273095246abbSSean Bruno /* XXX we should be setting this to something other than zero */
273195246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
273295246abbSSean Bruno #define MAX_TX_DESC(ctx) ((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max)
273395246abbSSean Bruno 
273495246abbSSean Bruno static inline bool
273595246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use)
27364c7070dbSScott Long {
273795246abbSSean Bruno 	qidx_t dbval, max;
273895246abbSSean Bruno 	bool rang;
27394c7070dbSScott Long 
274095246abbSSean Bruno 	rang = false;
274195246abbSSean Bruno 	max = TXQ_MAX_DB_DEFERRED(txq, in_use);
274295246abbSSean Bruno 	if (ring || txq->ift_db_pending >= max) {
27434c7070dbSScott Long 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
27444c7070dbSScott Long 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
27454c7070dbSScott Long 		txq->ift_db_pending = txq->ift_npending = 0;
274695246abbSSean Bruno 		rang = true;
27474c7070dbSScott Long 	}
274895246abbSSean Bruno 	return (rang);
27494c7070dbSScott Long }
27504c7070dbSScott Long 
27514c7070dbSScott Long #ifdef PKT_DEBUG
27524c7070dbSScott Long static void
27534c7070dbSScott Long print_pkt(if_pkt_info_t pi)
27544c7070dbSScott Long {
27554c7070dbSScott Long 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
27564c7070dbSScott Long 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
27574c7070dbSScott Long 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
27584c7070dbSScott Long 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
27594c7070dbSScott Long 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
27604c7070dbSScott Long 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
27614c7070dbSScott Long }
27624c7070dbSScott Long #endif
27634c7070dbSScott Long 
27644c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
27654c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
27664c7070dbSScott Long 
27674c7070dbSScott Long static int
27684c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
27694c7070dbSScott Long {
2770ab2e3f79SStephen Hurd 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
27714c7070dbSScott Long 	struct ether_vlan_header *eh;
277223ac9029SStephen Hurd 	struct mbuf *m, *n;
27734c7070dbSScott Long 
277423ac9029SStephen Hurd 	n = m = *mp;
2775ab2e3f79SStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
2776ab2e3f79SStephen Hurd 	    M_WRITABLE(m) == 0) {
2777ab2e3f79SStephen Hurd 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
2778ab2e3f79SStephen Hurd 			return (ENOMEM);
2779ab2e3f79SStephen Hurd 		} else {
2780ab2e3f79SStephen Hurd 			m_freem(*mp);
2781ab2e3f79SStephen Hurd 			n = *mp = m;
2782ab2e3f79SStephen Hurd 		}
2783ab2e3f79SStephen Hurd 	}
27841248952aSSean Bruno 
27854c7070dbSScott Long 	/*
27864c7070dbSScott Long 	 * Determine where frame payload starts.
27874c7070dbSScott Long 	 * Jump over vlan headers if already present,
27884c7070dbSScott Long 	 * helpful for QinQ too.
27894c7070dbSScott Long 	 */
27904c7070dbSScott Long 	if (__predict_false(m->m_len < sizeof(*eh))) {
27914c7070dbSScott Long 		txq->ift_pullups++;
27924c7070dbSScott Long 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
27934c7070dbSScott Long 			return (ENOMEM);
27944c7070dbSScott Long 	}
27954c7070dbSScott Long 	eh = mtod(m, struct ether_vlan_header *);
27964c7070dbSScott Long 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
27974c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_proto);
27984c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
27994c7070dbSScott Long 	} else {
28004c7070dbSScott Long 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
28014c7070dbSScott Long 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
28024c7070dbSScott Long 	}
28034c7070dbSScott Long 
28044c7070dbSScott Long 	switch (pi->ipi_etype) {
28054c7070dbSScott Long #ifdef INET
28064c7070dbSScott Long 	case ETHERTYPE_IP:
28074c7070dbSScott Long 	{
28084c7070dbSScott Long 		struct ip *ip = NULL;
28094c7070dbSScott Long 		struct tcphdr *th = NULL;
28104c7070dbSScott Long 		int minthlen;
28114c7070dbSScott Long 
28124c7070dbSScott Long 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
28134c7070dbSScott Long 		if (__predict_false(m->m_len < minthlen)) {
28144c7070dbSScott Long 			/*
28154c7070dbSScott Long 			 * if this code bloat is causing too much of a hit
28164c7070dbSScott Long 			 * move it to a separate function and mark it noinline
28174c7070dbSScott Long 			 */
28184c7070dbSScott Long 			if (m->m_len == pi->ipi_ehdrlen) {
28194c7070dbSScott Long 				n = m->m_next;
28204c7070dbSScott Long 				MPASS(n);
28214c7070dbSScott Long 				if (n->m_len >= sizeof(*ip))  {
28224c7070dbSScott Long 					ip = (struct ip *)n->m_data;
28234c7070dbSScott Long 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
28244c7070dbSScott Long 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
28254c7070dbSScott Long 				} else {
28264c7070dbSScott Long 					txq->ift_pullups++;
28274c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
28284c7070dbSScott Long 						return (ENOMEM);
28294c7070dbSScott Long 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
28304c7070dbSScott Long 				}
28314c7070dbSScott Long 			} else {
28324c7070dbSScott Long 				txq->ift_pullups++;
28334c7070dbSScott Long 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
28344c7070dbSScott Long 					return (ENOMEM);
28354c7070dbSScott Long 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
28364c7070dbSScott Long 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
28374c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
28384c7070dbSScott Long 			}
28394c7070dbSScott Long 		} else {
28404c7070dbSScott Long 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
28414c7070dbSScott Long 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
28424c7070dbSScott Long 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
28434c7070dbSScott Long 		}
28444c7070dbSScott Long 		pi->ipi_ip_hlen = ip->ip_hl << 2;
28454c7070dbSScott Long 		pi->ipi_ipproto = ip->ip_p;
28464c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV4;
28474c7070dbSScott Long 
2848c5cf2172SStephen Hurd 		if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP))
28494c7070dbSScott Long                        ip->ip_sum = 0;
28504c7070dbSScott Long 
2851c5cf2172SStephen Hurd 		if (IS_TSO4(pi)) {
28524c7070dbSScott Long 			if (pi->ipi_ipproto == IPPROTO_TCP) {
28534c7070dbSScott Long 				if (__predict_false(th == NULL)) {
28544c7070dbSScott Long 					txq->ift_pullups++;
28554c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
28564c7070dbSScott Long 						return (ENOMEM);
28574c7070dbSScott Long 					th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
28584c7070dbSScott Long 				}
28594c7070dbSScott Long 				pi->ipi_tcp_hflags = th->th_flags;
28604c7070dbSScott Long 				pi->ipi_tcp_hlen = th->th_off << 2;
28614c7070dbSScott Long 				pi->ipi_tcp_seq = th->th_seq;
28624c7070dbSScott Long 			}
28634c7070dbSScott Long 			if (__predict_false(ip->ip_p != IPPROTO_TCP))
28644c7070dbSScott Long 				return (ENXIO);
28654c7070dbSScott Long 			th->th_sum = in_pseudo(ip->ip_src.s_addr,
28664c7070dbSScott Long 					       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
28674c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
28681248952aSSean Bruno 			if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
28691248952aSSean Bruno 				ip->ip_sum = 0;
28701248952aSSean Bruno 				ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
28711248952aSSean Bruno 			}
28724c7070dbSScott Long 		}
28734c7070dbSScott Long 		break;
28744c7070dbSScott Long 	}
28754c7070dbSScott Long #endif
28764c7070dbSScott Long #ifdef INET6
28774c7070dbSScott Long 	case ETHERTYPE_IPV6:
28784c7070dbSScott Long 	{
28794c7070dbSScott Long 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
28804c7070dbSScott Long 		struct tcphdr *th;
28814c7070dbSScott Long 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
28824c7070dbSScott Long 
28834c7070dbSScott Long 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
28844c7070dbSScott Long 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
28854c7070dbSScott Long 				return (ENOMEM);
28864c7070dbSScott Long 		}
28874c7070dbSScott Long 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
28884c7070dbSScott Long 
28894c7070dbSScott Long 		/* XXX-BZ this will go badly in case of ext hdrs. */
28904c7070dbSScott Long 		pi->ipi_ipproto = ip6->ip6_nxt;
28914c7070dbSScott Long 		pi->ipi_flags |= IPI_TX_IPV6;
28924c7070dbSScott Long 
2893c5cf2172SStephen Hurd 		if (IS_TSO6(pi)) {
28944c7070dbSScott Long 			if (pi->ipi_ipproto == IPPROTO_TCP) {
28954c7070dbSScott Long 				if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
28964c7070dbSScott Long 					if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
28974c7070dbSScott Long 						return (ENOMEM);
28984c7070dbSScott Long 				}
28994c7070dbSScott Long 				pi->ipi_tcp_hflags = th->th_flags;
29004c7070dbSScott Long 				pi->ipi_tcp_hlen = th->th_off << 2;
29014c7070dbSScott Long 			}
29024c7070dbSScott Long 
29034c7070dbSScott Long 			if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
29044c7070dbSScott Long 				return (ENXIO);
29054c7070dbSScott Long 			/*
29064c7070dbSScott Long 			 * The corresponding flag is set by the stack in the IPv4
29074c7070dbSScott Long 			 * TSO case, but not in IPv6 (at least in FreeBSD 10.2).
29084c7070dbSScott Long 			 * So, set it here because the rest of the flow requires it.
29094c7070dbSScott Long 			 */
29104c7070dbSScott Long 			pi->ipi_csum_flags |= CSUM_TCP_IPV6;
29114c7070dbSScott Long 			th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
29124c7070dbSScott Long 			pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
29134c7070dbSScott Long 		}
29144c7070dbSScott Long 		break;
29154c7070dbSScott Long 	}
29164c7070dbSScott Long #endif
29174c7070dbSScott Long 	default:
29184c7070dbSScott Long 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
29194c7070dbSScott Long 		pi->ipi_ip_hlen = 0;
29204c7070dbSScott Long 		break;
29214c7070dbSScott Long 	}
29224c7070dbSScott Long 	*mp = m;
29231248952aSSean Bruno 
29244c7070dbSScott Long 	return (0);
29254c7070dbSScott Long }
29264c7070dbSScott Long 
29274c7070dbSScott Long static  __noinline  struct mbuf *
29284c7070dbSScott Long collapse_pkthdr(struct mbuf *m0)
29294c7070dbSScott Long {
29304c7070dbSScott Long 	struct mbuf *m, *m_next, *tmp;
29314c7070dbSScott Long 
29324c7070dbSScott Long 	m = m0;
29334c7070dbSScott Long 	m_next = m->m_next;
29344c7070dbSScott Long 	while (m_next != NULL && m_next->m_len == 0) {
29354c7070dbSScott Long 		m = m_next;
29364c7070dbSScott Long 		m->m_next = NULL;
29374c7070dbSScott Long 		m_free(m);
29384c7070dbSScott Long 		m_next = m_next->m_next;
29394c7070dbSScott Long 	}
29404c7070dbSScott Long 	m = m0;
29414c7070dbSScott Long 	m->m_next = m_next;
29424c7070dbSScott Long 	if ((m_next->m_flags & M_EXT) == 0) {
29434c7070dbSScott Long 		m = m_defrag(m, M_NOWAIT);
29444c7070dbSScott Long 	} else {
29454c7070dbSScott Long 		tmp = m_next->m_next;
29464c7070dbSScott Long 		memcpy(m_next, m, MPKTHSIZE);
29474c7070dbSScott Long 		m = m_next;
29484c7070dbSScott Long 		m->m_next = tmp;
29494c7070dbSScott Long 	}
29504c7070dbSScott Long 	return (m);
29514c7070dbSScott Long }
29524c7070dbSScott Long 
29534c7070dbSScott Long /*
29544c7070dbSScott Long  * If dodgy hardware rejects the scatter gather chain we've handed it
295523ac9029SStephen Hurd  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
295623ac9029SStephen Hurd  * m_defrag'd mbufs
29574c7070dbSScott Long  */
29584c7070dbSScott Long static __noinline struct mbuf *
295923ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq)
29604c7070dbSScott Long {
296123ac9029SStephen Hurd 	int ntxd, i, pidx;
29624c7070dbSScott Long 	struct mbuf *m, *mh, **ifsd_m;
29634c7070dbSScott Long 
29644c7070dbSScott Long 	pidx = txq->ift_pidx;
29654c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
296623ac9029SStephen Hurd 	ntxd = txq->ift_size;
29674c7070dbSScott Long 	mh = m = ifsd_m[pidx];
29684c7070dbSScott Long 	ifsd_m[pidx] = NULL;
29694c7070dbSScott Long #if MEMORY_LOGGING
29704c7070dbSScott Long 	txq->ift_dequeued++;
29714c7070dbSScott Long #endif
29724c7070dbSScott Long 	i = 1;
29734c7070dbSScott Long 
297423ac9029SStephen Hurd 	while (m) {
29754c7070dbSScott Long 		ifsd_m[(pidx + i) & (ntxd -1)] = NULL;
29764c7070dbSScott Long #if MEMORY_LOGGING
29774c7070dbSScott Long 		txq->ift_dequeued++;
29784c7070dbSScott Long #endif
29794c7070dbSScott Long 		m = m->m_next;
29804c7070dbSScott Long 		i++;
29814c7070dbSScott Long 	}
29824c7070dbSScott Long 	return (mh);
29834c7070dbSScott Long }
29844c7070dbSScott Long 
29854c7070dbSScott Long static int
29864c7070dbSScott Long iflib_busdma_load_mbuf_sg(iflib_txq_t txq, bus_dma_tag_t tag, bus_dmamap_t map,
29874c7070dbSScott Long 			  struct mbuf **m0, bus_dma_segment_t *segs, int *nsegs,
29884c7070dbSScott Long 			  int max_segs, int flags)
29894c7070dbSScott Long {
29904c7070dbSScott Long 	if_ctx_t ctx;
29914c7070dbSScott Long 	if_shared_ctx_t		sctx;
299223ac9029SStephen Hurd 	if_softc_ctx_t		scctx;
299325d52811SSean Bruno 	int i, next, pidx, err, ntxd, count;
2994aa8fa07cSSean Bruno 	struct mbuf *m, *tmp, **ifsd_m;
29954c7070dbSScott Long 
29964c7070dbSScott Long 	m = *m0;
29974c7070dbSScott Long 
29984c7070dbSScott Long 	/*
29994c7070dbSScott Long 	 * Please don't ever do this
30004c7070dbSScott Long 	 */
30014c7070dbSScott Long 	if (__predict_false(m->m_len == 0))
30024c7070dbSScott Long 		*m0 = m = collapse_pkthdr(m);
30034c7070dbSScott Long 
30044c7070dbSScott Long 	ctx = txq->ift_ctx;
30054c7070dbSScott Long 	sctx = ctx->ifc_sctx;
300623ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
30074c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
300823ac9029SStephen Hurd 	ntxd = txq->ift_size;
30094c7070dbSScott Long 	pidx = txq->ift_pidx;
3010ab2e3f79SStephen Hurd 	if (map != NULL) {
30114c7070dbSScott Long 		uint8_t *ifsd_flags = txq->ift_sds.ifsd_flags;
3012ab2e3f79SStephen Hurd 
30134c7070dbSScott Long 		err = bus_dmamap_load_mbuf_sg(tag, map,
30144c7070dbSScott Long 					      *m0, segs, nsegs, BUS_DMA_NOWAIT);
30154c7070dbSScott Long 		if (err)
30164c7070dbSScott Long 			return (err);
30174c7070dbSScott Long 		ifsd_flags[pidx] |= TX_SW_DESC_MAPPED;
3018aa8fa07cSSean Bruno 		count = 0;
30194c7070dbSScott Long 		m = *m0;
30204c7070dbSScott Long 		do {
3021aa8fa07cSSean Bruno 			if (__predict_false(m->m_len <= 0)) {
3022aa8fa07cSSean Bruno 				tmp = m;
30234c7070dbSScott Long 				m = m->m_next;
3024aa8fa07cSSean Bruno 				tmp->m_next = NULL;
3025aa8fa07cSSean Bruno 				m_free(tmp);
3026aa8fa07cSSean Bruno 				continue;
3027aa8fa07cSSean Bruno 			}
3028dcbc025fSSean Bruno 			m = m->m_next;
3029dcbc025fSSean Bruno 			count++;
3030dcbc025fSSean Bruno 		} while (m != NULL);
30315c5ca36cSSean Bruno 		if (count > *nsegs) {
30325c5ca36cSSean Bruno 			ifsd_m[pidx] = *m0;
30335c5ca36cSSean Bruno 			ifsd_m[pidx]->m_flags |= M_TOOBIG;
3034dcbc025fSSean Bruno 			return (0);
30355c5ca36cSSean Bruno 		}
3036dcbc025fSSean Bruno 		m = *m0;
3037dcbc025fSSean Bruno 		count = 0;
3038dcbc025fSSean Bruno 		do {
3039aa8fa07cSSean Bruno 			next = (pidx + count) & (ntxd-1);
3040aa8fa07cSSean Bruno 			MPASS(ifsd_m[next] == NULL);
3041aa8fa07cSSean Bruno 			ifsd_m[next] = m;
3042aa8fa07cSSean Bruno 			count++;
3043aa8fa07cSSean Bruno 			tmp = m;
3044aa8fa07cSSean Bruno 			m = m->m_next;
30454c7070dbSScott Long 		} while (m != NULL);
30464c7070dbSScott Long 	} else {
304725d52811SSean Bruno 		int buflen, sgsize, maxsegsz, max_sgsize;
30484c7070dbSScott Long 		vm_offset_t vaddr;
30494c7070dbSScott Long 		vm_paddr_t curaddr;
30504c7070dbSScott Long 
30514c7070dbSScott Long 		count = i = 0;
30524c7070dbSScott Long 		m = *m0;
305325d52811SSean Bruno 		if (m->m_pkthdr.csum_flags & CSUM_TSO)
305425d52811SSean Bruno 			maxsegsz = scctx->isc_tx_tso_segsize_max;
305525d52811SSean Bruno 		else
305625d52811SSean Bruno 			maxsegsz = sctx->isc_tx_maxsegsize;
305725d52811SSean Bruno 
30584c7070dbSScott Long 		do {
30594c7070dbSScott Long 			if (__predict_false(m->m_len <= 0)) {
30604c7070dbSScott Long 				tmp = m;
30614c7070dbSScott Long 				m = m->m_next;
30624c7070dbSScott Long 				tmp->m_next = NULL;
30634c7070dbSScott Long 				m_free(tmp);
30644c7070dbSScott Long 				continue;
30654c7070dbSScott Long 			}
30664c7070dbSScott Long 			buflen = m->m_len;
30674c7070dbSScott Long 			vaddr = (vm_offset_t)m->m_data;
30684c7070dbSScott Long 			/*
30694c7070dbSScott Long 			 * see if we can't be smarter about physically
30704c7070dbSScott Long 			 * contiguous mappings
30714c7070dbSScott Long 			 */
30724c7070dbSScott Long 			next = (pidx + count) & (ntxd-1);
30734c7070dbSScott Long 			MPASS(ifsd_m[next] == NULL);
30744c7070dbSScott Long #if MEMORY_LOGGING
30754c7070dbSScott Long 			txq->ift_enqueued++;
30764c7070dbSScott Long #endif
30774c7070dbSScott Long 			ifsd_m[next] = m;
30784c7070dbSScott Long 			while (buflen > 0) {
30799d35858fSSean Bruno 				if (i >= max_segs)
30809d35858fSSean Bruno 					goto err;
30814c7070dbSScott Long 				max_sgsize = MIN(buflen, maxsegsz);
30824c7070dbSScott Long 				curaddr = pmap_kextract(vaddr);
30834c7070dbSScott Long 				sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
30844c7070dbSScott Long 				sgsize = MIN(sgsize, max_sgsize);
30854c7070dbSScott Long 				segs[i].ds_addr = curaddr;
30864c7070dbSScott Long 				segs[i].ds_len = sgsize;
30874c7070dbSScott Long 				vaddr += sgsize;
30884c7070dbSScott Long 				buflen -= sgsize;
30894c7070dbSScott Long 				i++;
30904c7070dbSScott Long 			}
30914c7070dbSScott Long 			count++;
30924c7070dbSScott Long 			tmp = m;
30934c7070dbSScott Long 			m = m->m_next;
30944c7070dbSScott Long 		} while (m != NULL);
30954c7070dbSScott Long 		*nsegs = i;
30964c7070dbSScott Long 	}
30974c7070dbSScott Long 	return (0);
30984c7070dbSScott Long err:
309923ac9029SStephen Hurd 	*m0 = iflib_remove_mbuf(txq);
31004c7070dbSScott Long 	return (EFBIG);
31014c7070dbSScott Long }
31024c7070dbSScott Long 
310395246abbSSean Bruno static inline caddr_t
310495246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid)
310595246abbSSean Bruno {
310695246abbSSean Bruno 	qidx_t size;
310795246abbSSean Bruno 	int ntxd;
310895246abbSSean Bruno 	caddr_t start, end, cur, next;
310995246abbSSean Bruno 
311095246abbSSean Bruno 	ntxd = txq->ift_size;
311195246abbSSean Bruno 	size = txq->ift_txd_size[qid];
311295246abbSSean Bruno 	start = txq->ift_ifdi[qid].idi_vaddr;
311395246abbSSean Bruno 
311495246abbSSean Bruno 	if (__predict_false(size == 0))
311595246abbSSean Bruno 		return (start);
311695246abbSSean Bruno 	cur = start + size*cidx;
311795246abbSSean Bruno 	end = start + size*ntxd;
311895246abbSSean Bruno 	next = CACHE_PTR_NEXT(cur);
311995246abbSSean Bruno 	return (next < end ? next : start);
312095246abbSSean Bruno }
312195246abbSSean Bruno 
3122d14c853bSStephen Hurd /*
3123d14c853bSStephen Hurd  * Pad an mbuf to ensure a minimum ethernet frame size.
3124d14c853bSStephen Hurd  * min_frame_size is the frame size (less CRC) to pad the mbuf to
3125d14c853bSStephen Hurd  */
3126d14c853bSStephen Hurd static __noinline int
3127a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size)
3128d14c853bSStephen Hurd {
3129d14c853bSStephen Hurd 	/*
3130d14c853bSStephen Hurd 	 * 18 is enough bytes to pad an ARP packet to 46 bytes, and
3131d14c853bSStephen Hurd 	 * and ARP message is the smallest common payload I can think of
3132d14c853bSStephen Hurd 	 */
3133d14c853bSStephen Hurd 	static char pad[18];	/* just zeros */
3134d14c853bSStephen Hurd 	int n;
3135a15fbbb8SStephen Hurd 	struct mbuf *new_head;
3136d14c853bSStephen Hurd 
3137a15fbbb8SStephen Hurd 	if (!M_WRITABLE(*m_head)) {
3138a15fbbb8SStephen Hurd 		new_head = m_dup(*m_head, M_NOWAIT);
3139a15fbbb8SStephen Hurd 		if (new_head == NULL) {
314004993890SStephen Hurd 			m_freem(*m_head);
3141a15fbbb8SStephen Hurd 			device_printf(dev, "cannot pad short frame, m_dup() failed");
314206c47d48SStephen Hurd 			DBG_COUNTER_INC(encap_pad_mbuf_fail);
3143a15fbbb8SStephen Hurd 			return ENOMEM;
3144a15fbbb8SStephen Hurd 		}
3145a15fbbb8SStephen Hurd 		m_freem(*m_head);
3146a15fbbb8SStephen Hurd 		*m_head = new_head;
3147a15fbbb8SStephen Hurd 	}
3148a15fbbb8SStephen Hurd 
3149a15fbbb8SStephen Hurd 	for (n = min_frame_size - (*m_head)->m_pkthdr.len;
3150d14c853bSStephen Hurd 	     n > 0; n -= sizeof(pad))
3151a15fbbb8SStephen Hurd 		if (!m_append(*m_head, min(n, sizeof(pad)), pad))
3152d14c853bSStephen Hurd 			break;
3153d14c853bSStephen Hurd 
3154d14c853bSStephen Hurd 	if (n > 0) {
3155a15fbbb8SStephen Hurd 		m_freem(*m_head);
3156d14c853bSStephen Hurd 		device_printf(dev, "cannot pad short frame\n");
3157d14c853bSStephen Hurd 		DBG_COUNTER_INC(encap_pad_mbuf_fail);
3158d14c853bSStephen Hurd 		return (ENOBUFS);
3159d14c853bSStephen Hurd 	}
3160d14c853bSStephen Hurd 
3161d14c853bSStephen Hurd 	return 0;
3162d14c853bSStephen Hurd }
3163d14c853bSStephen Hurd 
31644c7070dbSScott Long static int
31654c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
31664c7070dbSScott Long {
31674c7070dbSScott Long 	if_ctx_t		ctx;
31684c7070dbSScott Long 	if_shared_ctx_t		sctx;
31694c7070dbSScott Long 	if_softc_ctx_t		scctx;
31704c7070dbSScott Long 	bus_dma_segment_t	*segs;
31714c7070dbSScott Long 	struct mbuf		*m_head;
317295246abbSSean Bruno 	void			*next_txd;
31734c7070dbSScott Long 	bus_dmamap_t		map;
31744c7070dbSScott Long 	struct if_pkt_info	pi;
31754c7070dbSScott Long 	int remap = 0;
31764c7070dbSScott Long 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
31774c7070dbSScott Long 	bus_dma_tag_t desc_tag;
31784c7070dbSScott Long 
31794c7070dbSScott Long 	segs = txq->ift_segs;
31804c7070dbSScott Long 	ctx = txq->ift_ctx;
31814c7070dbSScott Long 	sctx = ctx->ifc_sctx;
31824c7070dbSScott Long 	scctx = &ctx->ifc_softc_ctx;
31834c7070dbSScott Long 	segs = txq->ift_segs;
318423ac9029SStephen Hurd 	ntxd = txq->ift_size;
31854c7070dbSScott Long 	m_head = *m_headp;
31864c7070dbSScott Long 	map = NULL;
31874c7070dbSScott Long 
31884c7070dbSScott Long 	/*
31894c7070dbSScott Long 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
31904c7070dbSScott Long 	 */
31914c7070dbSScott Long 	cidx = txq->ift_cidx;
31924c7070dbSScott Long 	pidx = txq->ift_pidx;
319395246abbSSean Bruno 	if (ctx->ifc_flags & IFC_PREFETCH) {
31944c7070dbSScott Long 		next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
319595246abbSSean Bruno 		if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) {
319695246abbSSean Bruno 			next_txd = calc_next_txd(txq, cidx, 0);
319795246abbSSean Bruno 			prefetch(next_txd);
319895246abbSSean Bruno 		}
31994c7070dbSScott Long 
32004c7070dbSScott Long 		/* prefetch the next cache line of mbuf pointers and flags */
32014c7070dbSScott Long 		prefetch(&txq->ift_sds.ifsd_m[next]);
32024c7070dbSScott Long 		if (txq->ift_sds.ifsd_map != NULL) {
32034c7070dbSScott Long 			prefetch(&txq->ift_sds.ifsd_map[next]);
32044c7070dbSScott Long 			next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
32054c7070dbSScott Long 			prefetch(&txq->ift_sds.ifsd_flags[next]);
32064c7070dbSScott Long 		}
3207ab2e3f79SStephen Hurd 	} else if (txq->ift_sds.ifsd_map != NULL)
320895246abbSSean Bruno 		map = txq->ift_sds.ifsd_map[pidx];
32094c7070dbSScott Long 
32104c7070dbSScott Long 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
32114c7070dbSScott Long 		desc_tag = txq->ift_tso_desc_tag;
32124c7070dbSScott Long 		max_segs = scctx->isc_tx_tso_segments_max;
32134c7070dbSScott Long 	} else {
32144c7070dbSScott Long 		desc_tag = txq->ift_desc_tag;
32154c7070dbSScott Long 		max_segs = scctx->isc_tx_nsegments;
32164c7070dbSScott Long 	}
3217d14c853bSStephen Hurd 	if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) &&
3218d14c853bSStephen Hurd 	    __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) {
3219a15fbbb8SStephen Hurd 		err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size);
3220d14c853bSStephen Hurd 		if (err)
3221d14c853bSStephen Hurd 			return err;
3222d14c853bSStephen Hurd 	}
3223a15fbbb8SStephen Hurd 	m_head = *m_headp;
322495246abbSSean Bruno 
322595246abbSSean Bruno 	pkt_info_zero(&pi);
3226ab2e3f79SStephen Hurd 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
3227ab2e3f79SStephen Hurd 	pi.ipi_pidx = pidx;
3228ab2e3f79SStephen Hurd 	pi.ipi_qsidx = txq->ift_id;
32293429c02fSStephen Hurd 	pi.ipi_len = m_head->m_pkthdr.len;
32303429c02fSStephen Hurd 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
32313429c02fSStephen Hurd 	pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0;
32324c7070dbSScott Long 
32334c7070dbSScott Long 	/* deliberate bitwise OR to make one condition */
32344c7070dbSScott Long 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
32354c7070dbSScott Long 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0))
32364c7070dbSScott Long 			return (err);
32374c7070dbSScott Long 		m_head = *m_headp;
32384c7070dbSScott Long 	}
32394c7070dbSScott Long 
32404c7070dbSScott Long retry:
32414c7070dbSScott Long 	err = iflib_busdma_load_mbuf_sg(txq, desc_tag, map, m_headp, segs, &nsegs, max_segs, BUS_DMA_NOWAIT);
32424c7070dbSScott Long defrag:
32434c7070dbSScott Long 	if (__predict_false(err)) {
32444c7070dbSScott Long 		switch (err) {
32454c7070dbSScott Long 		case EFBIG:
32464c7070dbSScott Long 			/* try collapse once and defrag once */
3247f7594707SAndrew Gallatin 			if (remap == 0) {
32484c7070dbSScott Long 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
3249f7594707SAndrew Gallatin 				/* try defrag if collapsing fails */
3250f7594707SAndrew Gallatin 				if (m_head == NULL)
3251f7594707SAndrew Gallatin 					remap++;
3252f7594707SAndrew Gallatin 			}
32534c7070dbSScott Long 			if (remap == 1)
32544c7070dbSScott Long 				m_head = m_defrag(*m_headp, M_NOWAIT);
32554c7070dbSScott Long 			remap++;
32564c7070dbSScott Long 			if (__predict_false(m_head == NULL))
32574c7070dbSScott Long 				goto defrag_failed;
32584c7070dbSScott Long 			txq->ift_mbuf_defrag++;
32594c7070dbSScott Long 			*m_headp = m_head;
32604c7070dbSScott Long 			goto retry;
32614c7070dbSScott Long 			break;
32624c7070dbSScott Long 		case ENOMEM:
32634c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
32644c7070dbSScott Long 			break;
32654c7070dbSScott Long 		default:
32664c7070dbSScott Long 			txq->ift_no_tx_dma_setup++;
32674c7070dbSScott Long 			m_freem(*m_headp);
32684c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
32694c7070dbSScott Long 			*m_headp = NULL;
32704c7070dbSScott Long 			break;
32714c7070dbSScott Long 		}
32724c7070dbSScott Long 		txq->ift_map_failed++;
32734c7070dbSScott Long 		DBG_COUNTER_INC(encap_load_mbuf_fail);
32744c7070dbSScott Long 		return (err);
32754c7070dbSScott Long 	}
32764c7070dbSScott Long 
32774c7070dbSScott Long 	/*
32784c7070dbSScott Long 	 * XXX assumes a 1 to 1 relationship between segments and
32794c7070dbSScott Long 	 *        descriptors - this does not hold true on all drivers, e.g.
32804c7070dbSScott Long 	 *        cxgb
32814c7070dbSScott Long 	 */
32824c7070dbSScott Long 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
32834c7070dbSScott Long 		txq->ift_no_desc_avail++;
32844c7070dbSScott Long 		if (map != NULL)
32854c7070dbSScott Long 			bus_dmamap_unload(desc_tag, map);
32864c7070dbSScott Long 		DBG_COUNTER_INC(encap_txq_avail_fail);
328723ac9029SStephen Hurd 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
32884c7070dbSScott Long 			GROUPTASK_ENQUEUE(&txq->ift_task);
32894c7070dbSScott Long 		return (ENOBUFS);
32904c7070dbSScott Long 	}
329195246abbSSean Bruno 	/*
329295246abbSSean Bruno 	 * On Intel cards we can greatly reduce the number of TX interrupts
329395246abbSSean Bruno 	 * we see by only setting report status on every Nth descriptor.
329495246abbSSean Bruno 	 * However, this also means that the driver will need to keep track
329595246abbSSean Bruno 	 * of the descriptors that RS was set on to check them for the DD bit.
329695246abbSSean Bruno 	 */
329795246abbSSean Bruno 	txq->ift_rs_pending += nsegs + 1;
329895246abbSSean Bruno 	if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) ||
329995246abbSSean Bruno 	     iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs - 1) <= MAX_TX_DESC(ctx)) {
330095246abbSSean Bruno 		pi.ipi_flags |= IPI_TX_INTR;
330195246abbSSean Bruno 		txq->ift_rs_pending = 0;
330295246abbSSean Bruno 	}
330395246abbSSean Bruno 
33044c7070dbSScott Long 	pi.ipi_segs = segs;
33054c7070dbSScott Long 	pi.ipi_nsegs = nsegs;
33064c7070dbSScott Long 
330723ac9029SStephen Hurd 	MPASS(pidx >= 0 && pidx < txq->ift_size);
33084c7070dbSScott Long #ifdef PKT_DEBUG
33094c7070dbSScott Long 	print_pkt(&pi);
33104c7070dbSScott Long #endif
331195246abbSSean Bruno 	if (map != NULL)
331295246abbSSean Bruno 		bus_dmamap_sync(desc_tag, map, BUS_DMASYNC_PREWRITE);
33134c7070dbSScott Long 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
331495246abbSSean Bruno 		if (map != NULL)
33154c7070dbSScott Long 			bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
33164c7070dbSScott Long 					BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
33174c7070dbSScott Long 		DBG_COUNTER_INC(tx_encap);
331895246abbSSean Bruno 		MPASS(pi.ipi_new_pidx < txq->ift_size);
33194c7070dbSScott Long 
33204c7070dbSScott Long 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
33214c7070dbSScott Long 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
332223ac9029SStephen Hurd 			ndesc += txq->ift_size;
33234c7070dbSScott Long 			txq->ift_gen = 1;
33244c7070dbSScott Long 		}
33251248952aSSean Bruno 		/*
33261248952aSSean Bruno 		 * drivers can need as many as
33271248952aSSean Bruno 		 * two sentinels
33281248952aSSean Bruno 		 */
33291248952aSSean Bruno 		MPASS(ndesc <= pi.ipi_nsegs + 2);
33304c7070dbSScott Long 		MPASS(pi.ipi_new_pidx != pidx);
33314c7070dbSScott Long 		MPASS(ndesc > 0);
33324c7070dbSScott Long 		txq->ift_in_use += ndesc;
333395246abbSSean Bruno 
33344c7070dbSScott Long 		/*
33354c7070dbSScott Long 		 * We update the last software descriptor again here because there may
33364c7070dbSScott Long 		 * be a sentinel and/or there may be more mbufs than segments
33374c7070dbSScott Long 		 */
33384c7070dbSScott Long 		txq->ift_pidx = pi.ipi_new_pidx;
33394c7070dbSScott Long 		txq->ift_npending += pi.ipi_ndescs;
3340f7594707SAndrew Gallatin 	} else {
334123ac9029SStephen Hurd 		*m_headp = m_head = iflib_remove_mbuf(txq);
3342f7594707SAndrew Gallatin 		if (err == EFBIG) {
33434c7070dbSScott Long 			txq->ift_txd_encap_efbig++;
3344f7594707SAndrew Gallatin 			if (remap < 2) {
3345f7594707SAndrew Gallatin 				remap = 1;
33464c7070dbSScott Long 				goto defrag;
3347f7594707SAndrew Gallatin 			}
3348f7594707SAndrew Gallatin 		}
33494c7070dbSScott Long 		DBG_COUNTER_INC(encap_txd_encap_fail);
3350f7594707SAndrew Gallatin 		goto defrag_failed;
3351f7594707SAndrew Gallatin 	}
33524c7070dbSScott Long 	return (err);
33534c7070dbSScott Long 
33544c7070dbSScott Long defrag_failed:
33554c7070dbSScott Long 	txq->ift_mbuf_defrag_failed++;
33564c7070dbSScott Long 	txq->ift_map_failed++;
33574c7070dbSScott Long 	m_freem(*m_headp);
33584c7070dbSScott Long 	DBG_COUNTER_INC(tx_frees);
33594c7070dbSScott Long 	*m_headp = NULL;
33604c7070dbSScott Long 	return (ENOMEM);
33614c7070dbSScott Long }
33624c7070dbSScott Long 
33634c7070dbSScott Long static void
33644c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n)
33654c7070dbSScott Long {
33664c7070dbSScott Long 	int hasmap;
33674c7070dbSScott Long 	uint32_t qsize, cidx, mask, gen;
33684c7070dbSScott Long 	struct mbuf *m, **ifsd_m;
33694c7070dbSScott Long 	uint8_t *ifsd_flags;
33704c7070dbSScott Long 	bus_dmamap_t *ifsd_map;
337195246abbSSean Bruno 	bool do_prefetch;
33724c7070dbSScott Long 
33734c7070dbSScott Long 	cidx = txq->ift_cidx;
33744c7070dbSScott Long 	gen = txq->ift_gen;
337523ac9029SStephen Hurd 	qsize = txq->ift_size;
33764c7070dbSScott Long 	mask = qsize-1;
33774c7070dbSScott Long 	hasmap = txq->ift_sds.ifsd_map != NULL;
33784c7070dbSScott Long 	ifsd_flags = txq->ift_sds.ifsd_flags;
33794c7070dbSScott Long 	ifsd_m = txq->ift_sds.ifsd_m;
33804c7070dbSScott Long 	ifsd_map = txq->ift_sds.ifsd_map;
338195246abbSSean Bruno 	do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH);
33824c7070dbSScott Long 
33834c7070dbSScott Long 	while (n--) {
338495246abbSSean Bruno 		if (do_prefetch) {
33854c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 3) & mask]);
33864c7070dbSScott Long 			prefetch(ifsd_m[(cidx + 4) & mask]);
338795246abbSSean Bruno 		}
33884c7070dbSScott Long 		if (ifsd_m[cidx] != NULL) {
33894c7070dbSScott Long 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
33904c7070dbSScott Long 			prefetch(&ifsd_flags[(cidx + CACHE_PTR_INCREMENT) & mask]);
33914c7070dbSScott Long 			if (hasmap && (ifsd_flags[cidx] & TX_SW_DESC_MAPPED)) {
33924c7070dbSScott Long 				/*
33934c7070dbSScott Long 				 * does it matter if it's not the TSO tag? If so we'll
33944c7070dbSScott Long 				 * have to add the type to flags
33954c7070dbSScott Long 				 */
33964c7070dbSScott Long 				bus_dmamap_unload(txq->ift_desc_tag, ifsd_map[cidx]);
33974c7070dbSScott Long 				ifsd_flags[cidx] &= ~TX_SW_DESC_MAPPED;
33984c7070dbSScott Long 			}
33994c7070dbSScott Long 			if ((m = ifsd_m[cidx]) != NULL) {
34004c7070dbSScott Long 				/* XXX we don't support any drivers that batch packets yet */
34014c7070dbSScott Long 				MPASS(m->m_nextpkt == NULL);
34025c5ca36cSSean Bruno 				/* if the number of clusters exceeds the number of segments
34035c5ca36cSSean Bruno 				 * there won't be space on the ring to save a pointer to each
34045c5ca36cSSean Bruno 				 * cluster so we simply free the list here
34055c5ca36cSSean Bruno 				 */
34065c5ca36cSSean Bruno 				if (m->m_flags & M_TOOBIG) {
34075c5ca36cSSean Bruno 					m_freem(m);
34085c5ca36cSSean Bruno 				} else {
340923ac9029SStephen Hurd 					m_free(m);
34105c5ca36cSSean Bruno 				}
34114c7070dbSScott Long 				ifsd_m[cidx] = NULL;
34124c7070dbSScott Long #if MEMORY_LOGGING
34134c7070dbSScott Long 				txq->ift_dequeued++;
34144c7070dbSScott Long #endif
34154c7070dbSScott Long 				DBG_COUNTER_INC(tx_frees);
34164c7070dbSScott Long 			}
34174c7070dbSScott Long 		}
34184c7070dbSScott Long 		if (__predict_false(++cidx == qsize)) {
34194c7070dbSScott Long 			cidx = 0;
34204c7070dbSScott Long 			gen = 0;
34214c7070dbSScott Long 		}
34224c7070dbSScott Long 	}
34234c7070dbSScott Long 	txq->ift_cidx = cidx;
34244c7070dbSScott Long 	txq->ift_gen = gen;
34254c7070dbSScott Long }
34264c7070dbSScott Long 
34274c7070dbSScott Long static __inline int
34284c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
34294c7070dbSScott Long {
34304c7070dbSScott Long 	int reclaim;
34314c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
34324c7070dbSScott Long 
34334c7070dbSScott Long 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
34344c7070dbSScott Long 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
34354c7070dbSScott Long 
34364c7070dbSScott Long 	/*
34374c7070dbSScott Long 	 * Need a rate-limiting check so that this isn't called every time
34384c7070dbSScott Long 	 */
34394c7070dbSScott Long 	iflib_tx_credits_update(ctx, txq);
34404c7070dbSScott Long 	reclaim = DESC_RECLAIMABLE(txq);
34414c7070dbSScott Long 
34424c7070dbSScott Long 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
34434c7070dbSScott Long #ifdef INVARIANTS
34444c7070dbSScott Long 		if (iflib_verbose_debug) {
34454c7070dbSScott Long 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
34464c7070dbSScott Long 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
34474c7070dbSScott Long 			       reclaim, thresh);
34484c7070dbSScott Long 
34494c7070dbSScott Long 		}
34504c7070dbSScott Long #endif
34514c7070dbSScott Long 		return (0);
34524c7070dbSScott Long 	}
34534c7070dbSScott Long 	iflib_tx_desc_free(txq, reclaim);
34544c7070dbSScott Long 	txq->ift_cleaned += reclaim;
34554c7070dbSScott Long 	txq->ift_in_use -= reclaim;
34564c7070dbSScott Long 
34574c7070dbSScott Long 	return (reclaim);
34584c7070dbSScott Long }
34594c7070dbSScott Long 
34604c7070dbSScott Long static struct mbuf **
346195246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
34624c7070dbSScott Long {
346395246abbSSean Bruno 	int next, size;
346495246abbSSean Bruno 	struct mbuf **items;
34654c7070dbSScott Long 
346695246abbSSean Bruno 	size = r->size;
346795246abbSSean Bruno 	next = (cidx + CACHE_PTR_INCREMENT) & (size-1);
346895246abbSSean Bruno 	items = __DEVOLATILE(struct mbuf **, &r->items[0]);
346995246abbSSean Bruno 
347095246abbSSean Bruno 	prefetch(items[(cidx + offset) & (size-1)]);
347195246abbSSean Bruno 	if (remaining > 1) {
34723429c02fSStephen Hurd 		prefetch2cachelines(&items[next]);
34733429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]);
34743429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]);
34753429c02fSStephen Hurd 		prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]);
347695246abbSSean Bruno 	}
347795246abbSSean Bruno 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)]));
34784c7070dbSScott Long }
34794c7070dbSScott Long 
34804c7070dbSScott Long static void
34814c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget)
34824c7070dbSScott Long {
34834c7070dbSScott Long 
348495246abbSSean Bruno 	ifmp_ring_check_drainage(txq->ift_br, budget);
34854c7070dbSScott Long }
34864c7070dbSScott Long 
34874c7070dbSScott Long static uint32_t
34884c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r)
34894c7070dbSScott Long {
34904c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
34914c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
34924c7070dbSScott Long 
34931248952aSSean Bruno 	return ((TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) ||
349495246abbSSean Bruno 		ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false));
34954c7070dbSScott Long }
34964c7070dbSScott Long 
34974c7070dbSScott Long static uint32_t
34984c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
34994c7070dbSScott Long {
35004c7070dbSScott Long 	iflib_txq_t txq = r->cookie;
35014c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
350295246abbSSean Bruno 	struct ifnet *ifp = ctx->ifc_ifp;
35034c7070dbSScott Long 	struct mbuf **mp, *m;
350495246abbSSean Bruno 	int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail;
350595246abbSSean Bruno 	int reclaimed, err, in_use_prev, desc_used;
350695246abbSSean Bruno 	bool do_prefetch, ring, rang;
35074c7070dbSScott Long 
35084c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
35094c7070dbSScott Long 			    !LINK_ACTIVE(ctx))) {
35104c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_notready);
35114c7070dbSScott Long 		return (0);
35124c7070dbSScott Long 	}
351395246abbSSean Bruno 	reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
351495246abbSSean Bruno 	rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use);
35154c7070dbSScott Long 	avail = IDXDIFF(pidx, cidx, r->size);
35164c7070dbSScott Long 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
35174c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_flushing);
35184c7070dbSScott Long 		for (i = 0; i < avail; i++) {
351923ac9029SStephen Hurd 			m_free(r->items[(cidx + i) & (r->size-1)]);
35204c7070dbSScott Long 			r->items[(cidx + i) & (r->size-1)] = NULL;
35214c7070dbSScott Long 		}
35224c7070dbSScott Long 		return (avail);
35234c7070dbSScott Long 	}
352495246abbSSean Bruno 
35254c7070dbSScott Long 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
35264c7070dbSScott Long 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
35274c7070dbSScott Long 		CALLOUT_LOCK(txq);
35284c7070dbSScott Long 		callout_stop(&txq->ift_timer);
35294c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
35304c7070dbSScott Long 		DBG_COUNTER_INC(txq_drain_oactive);
35314c7070dbSScott Long 		return (0);
35324c7070dbSScott Long 	}
353395246abbSSean Bruno 	if (reclaimed)
353495246abbSSean Bruno 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
35354c7070dbSScott Long 	consumed = mcast_sent = bytes_sent = pkt_sent = 0;
35364c7070dbSScott Long 	count = MIN(avail, TX_BATCH_SIZE);
3537da69b8f9SSean Bruno #ifdef INVARIANTS
3538da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3539da69b8f9SSean Bruno 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3540da69b8f9SSean Bruno 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3541da69b8f9SSean Bruno #endif
354295246abbSSean Bruno 	do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
354395246abbSSean Bruno 	avail = TXQ_AVAIL(txq);
354495246abbSSean Bruno 	for (desc_used = i = 0; i < count && avail > MAX_TX_DESC(ctx) + 2; i++) {
354595246abbSSean Bruno 		int pidx_prev, rem = do_prefetch ? count - i : 0;
35464c7070dbSScott Long 
354795246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, rem);
3548da69b8f9SSean Bruno 		MPASS(mp != NULL && *mp != NULL);
354995246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq)) {
355095246abbSSean Bruno 			consumed++;
355195246abbSSean Bruno 			reclaimed++;
355295246abbSSean Bruno 			continue;
355395246abbSSean Bruno 		}
35544c7070dbSScott Long 		in_use_prev = txq->ift_in_use;
355595246abbSSean Bruno 		pidx_prev = txq->ift_pidx;
355695246abbSSean Bruno 		err = iflib_encap(txq, mp);
355795246abbSSean Bruno 		if (__predict_false(err)) {
35584c7070dbSScott Long 			DBG_COUNTER_INC(txq_drain_encapfail);
3559da69b8f9SSean Bruno 			/* no room - bail out */
356095246abbSSean Bruno 			if (err == ENOBUFS)
35614c7070dbSScott Long 				break;
35624c7070dbSScott Long 			consumed++;
3563da69b8f9SSean Bruno 			DBG_COUNTER_INC(txq_drain_encapfail);
3564da69b8f9SSean Bruno 			/* we can't send this packet - skip it */
35654c7070dbSScott Long 			continue;
3566da69b8f9SSean Bruno 		}
356795246abbSSean Bruno 		consumed++;
35684c7070dbSScott Long 		pkt_sent++;
35694c7070dbSScott Long 		m = *mp;
35704c7070dbSScott Long 		DBG_COUNTER_INC(tx_sent);
35714c7070dbSScott Long 		bytes_sent += m->m_pkthdr.len;
357295246abbSSean Bruno 		mcast_sent += !!(m->m_flags & M_MCAST);
357395246abbSSean Bruno 		avail = TXQ_AVAIL(txq);
35744c7070dbSScott Long 
35754c7070dbSScott Long 		txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
35764c7070dbSScott Long 		desc_used += (txq->ift_in_use - in_use_prev);
35774c7070dbSScott Long 		ETHER_BPF_MTAP(ifp, m);
357895246abbSSean Bruno 		if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
35794c7070dbSScott Long 			break;
358095246abbSSean Bruno 		rang = iflib_txd_db_check(ctx, txq, false, in_use_prev);
35814c7070dbSScott Long 	}
35824c7070dbSScott Long 
358395246abbSSean Bruno 	/* deliberate use of bitwise or to avoid gratuitous short-circuit */
358495246abbSSean Bruno 	ring = rang ? false  : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx));
358595246abbSSean Bruno 	iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use);
35864c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
35874c7070dbSScott Long 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
35884c7070dbSScott Long 	if (mcast_sent)
35894c7070dbSScott Long 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3590da69b8f9SSean Bruno #ifdef INVARIANTS
3591da69b8f9SSean Bruno 	if (iflib_verbose_debug)
3592da69b8f9SSean Bruno 		printf("consumed=%d\n", consumed);
3593da69b8f9SSean Bruno #endif
35944c7070dbSScott Long 	return (consumed);
35954c7070dbSScott Long }
35964c7070dbSScott Long 
3597da69b8f9SSean Bruno static uint32_t
3598da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r)
3599da69b8f9SSean Bruno {
3600da69b8f9SSean Bruno 	return (1);
3601da69b8f9SSean Bruno }
3602da69b8f9SSean Bruno 
3603da69b8f9SSean Bruno static uint32_t
3604da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3605da69b8f9SSean Bruno {
3606da69b8f9SSean Bruno 	int i, avail;
3607da69b8f9SSean Bruno 	struct mbuf **mp;
3608da69b8f9SSean Bruno 	iflib_txq_t txq;
3609da69b8f9SSean Bruno 
3610da69b8f9SSean Bruno 	txq = r->cookie;
3611da69b8f9SSean Bruno 
3612da69b8f9SSean Bruno 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3613da69b8f9SSean Bruno 	CALLOUT_LOCK(txq);
3614da69b8f9SSean Bruno 	callout_stop(&txq->ift_timer);
3615da69b8f9SSean Bruno 	CALLOUT_UNLOCK(txq);
3616da69b8f9SSean Bruno 
3617da69b8f9SSean Bruno 	avail = IDXDIFF(pidx, cidx, r->size);
3618da69b8f9SSean Bruno 	for (i = 0; i < avail; i++) {
361995246abbSSean Bruno 		mp = _ring_peek_one(r, cidx, i, avail - i);
362095246abbSSean Bruno 		if (__predict_false(*mp == (struct mbuf *)txq))
362195246abbSSean Bruno 			continue;
3622da69b8f9SSean Bruno 		m_freem(*mp);
3623da69b8f9SSean Bruno 	}
3624da69b8f9SSean Bruno 	MPASS(ifmp_ring_is_stalled(r) == 0);
3625da69b8f9SSean Bruno 	return (avail);
3626da69b8f9SSean Bruno }
3627da69b8f9SSean Bruno 
3628da69b8f9SSean Bruno static void
3629da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq)
3630da69b8f9SSean Bruno {
3631da69b8f9SSean Bruno 	struct ifmp_ring *r;
3632da69b8f9SSean Bruno 
363395246abbSSean Bruno 	r = txq->ift_br;
3634da69b8f9SSean Bruno 	r->drain = iflib_txq_drain_free;
3635da69b8f9SSean Bruno 	r->can_drain = iflib_txq_drain_always;
3636da69b8f9SSean Bruno 
3637da69b8f9SSean Bruno 	ifmp_ring_check_drainage(r, r->size);
3638da69b8f9SSean Bruno 
3639da69b8f9SSean Bruno 	r->drain = iflib_txq_drain;
3640da69b8f9SSean Bruno 	r->can_drain = iflib_txq_can_drain;
3641da69b8f9SSean Bruno }
3642da69b8f9SSean Bruno 
36434c7070dbSScott Long static void
364423ac9029SStephen Hurd _task_fn_tx(void *context)
36454c7070dbSScott Long {
36464c7070dbSScott Long 	iflib_txq_t txq = context;
36474c7070dbSScott Long 	if_ctx_t ctx = txq->ift_ctx;
364895246abbSSean Bruno 	struct ifnet *ifp = ctx->ifc_ifp;
364995246abbSSean Bruno 	int rc;
36504c7070dbSScott Long 
36511248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
36521248952aSSean Bruno 	txq->ift_cpu_exec_count[curcpu]++;
36531248952aSSean Bruno #endif
36544c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
36554c7070dbSScott Long 		return;
3656d0d0ad0aSStephen Hurd 	if (if_getcapenable(ifp) & IFCAP_NETMAP) {
365795246abbSSean Bruno 		if (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false))
365895246abbSSean Bruno 			netmap_tx_irq(ifp, txq->ift_id);
365995246abbSSean Bruno 		IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
366095246abbSSean Bruno 		return;
366195246abbSSean Bruno 	}
366295246abbSSean Bruno 	if (txq->ift_db_pending)
366395246abbSSean Bruno 		ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE);
366495246abbSSean Bruno 	ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
366595246abbSSean Bruno 	if (ctx->ifc_flags & IFC_LEGACY)
366695246abbSSean Bruno 		IFDI_INTR_ENABLE(ctx);
366795246abbSSean Bruno 	else {
366895246abbSSean Bruno 		rc = IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
366995246abbSSean Bruno 		KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
367095246abbSSean Bruno 	}
36714c7070dbSScott Long }
36724c7070dbSScott Long 
36734c7070dbSScott Long static void
367423ac9029SStephen Hurd _task_fn_rx(void *context)
36754c7070dbSScott Long {
36764c7070dbSScott Long 	iflib_rxq_t rxq = context;
36774c7070dbSScott Long 	if_ctx_t ctx = rxq->ifr_ctx;
36784c7070dbSScott Long 	bool more;
367923ac9029SStephen Hurd 	int rc;
3680f4d2154eSStephen Hurd 	uint16_t budget;
36814c7070dbSScott Long 
36821248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS
36831248952aSSean Bruno 	rxq->ifr_cpu_exec_count[curcpu]++;
36841248952aSSean Bruno #endif
36854c7070dbSScott Long 	DBG_COUNTER_INC(task_fn_rxs);
36864c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
36874c7070dbSScott Long 		return;
3688d0d0ad0aSStephen Hurd 	more = true;
3689d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP
3690d0d0ad0aSStephen Hurd 	if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) {
3691d0d0ad0aSStephen Hurd 		u_int work = 0;
3692d0d0ad0aSStephen Hurd 		if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work)) {
3693d0d0ad0aSStephen Hurd 			more = false;
3694d0d0ad0aSStephen Hurd 		}
3695d0d0ad0aSStephen Hurd 	}
3696d0d0ad0aSStephen Hurd #endif
3697f4d2154eSStephen Hurd 	budget = ctx->ifc_sysctl_rx_budget;
3698f4d2154eSStephen Hurd 	if (budget == 0)
3699f4d2154eSStephen Hurd 		budget = 16;	/* XXX */
3700f4d2154eSStephen Hurd 	if (more == false || (more = iflib_rxeof(rxq, budget)) == false) {
37014c7070dbSScott Long 		if (ctx->ifc_flags & IFC_LEGACY)
37024c7070dbSScott Long 			IFDI_INTR_ENABLE(ctx);
37034c7070dbSScott Long 		else {
37044c7070dbSScott Long 			DBG_COUNTER_INC(rx_intr_enables);
370595246abbSSean Bruno 			rc = IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
370623ac9029SStephen Hurd 			KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
37074c7070dbSScott Long 		}
37084c7070dbSScott Long 	}
37094c7070dbSScott Long 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
37104c7070dbSScott Long 		return;
37114c7070dbSScott Long 	if (more)
37124c7070dbSScott Long 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
37134c7070dbSScott Long }
37144c7070dbSScott Long 
37154c7070dbSScott Long static void
371623ac9029SStephen Hurd _task_fn_admin(void *context)
37174c7070dbSScott Long {
37184c7070dbSScott Long 	if_ctx_t ctx = context;
37194c7070dbSScott Long 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
37204c7070dbSScott Long 	iflib_txq_t txq;
3721ab2e3f79SStephen Hurd 	int i;
37227b610b60SSean Bruno 	bool oactive, running, do_reset, do_watchdog;
3723ab2e3f79SStephen Hurd 
37247b610b60SSean Bruno 	STATE_LOCK(ctx);
37257b610b60SSean Bruno 	running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING);
37267b610b60SSean Bruno 	oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE);
37277b610b60SSean Bruno 	do_reset = (ctx->ifc_flags & IFC_DO_RESET);
37287b610b60SSean Bruno 	do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG);
37297b610b60SSean Bruno 	ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG);
37307b610b60SSean Bruno 	STATE_UNLOCK(ctx);
37317b610b60SSean Bruno 
37327b610b60SSean Bruno 	if (!running & !oactive)
3733ab2e3f79SStephen Hurd 		return;
37344c7070dbSScott Long 
37354c7070dbSScott Long 	CTX_LOCK(ctx);
37364c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
37374c7070dbSScott Long 		CALLOUT_LOCK(txq);
37384c7070dbSScott Long 		callout_stop(&txq->ift_timer);
37394c7070dbSScott Long 		CALLOUT_UNLOCK(txq);
37404c7070dbSScott Long 	}
37417b610b60SSean Bruno 	if (do_watchdog) {
37427b610b60SSean Bruno 		ctx->ifc_watchdog_events++;
37437b610b60SSean Bruno 		IFDI_WATCHDOG_RESET(ctx);
37447b610b60SSean Bruno 	}
3745d300df01SStephen Hurd 	IFDI_UPDATE_ADMIN_STATUS(ctx);
3746ab2e3f79SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
3747ab2e3f79SStephen Hurd 		callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
3748ab2e3f79SStephen Hurd 	IFDI_LINK_INTR_ENABLE(ctx);
37497b610b60SSean Bruno 	if (do_reset)
3750ab2e3f79SStephen Hurd 		iflib_if_init_locked(ctx);
37514c7070dbSScott Long 	CTX_UNLOCK(ctx);
37524c7070dbSScott Long 
3753ab2e3f79SStephen Hurd 	if (LINK_ACTIVE(ctx) == 0)
37544c7070dbSScott Long 		return;
37554c7070dbSScott Long 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
37564c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
37574c7070dbSScott Long }
37584c7070dbSScott Long 
37594c7070dbSScott Long 
37604c7070dbSScott Long static void
376123ac9029SStephen Hurd _task_fn_iov(void *context)
37624c7070dbSScott Long {
37634c7070dbSScott Long 	if_ctx_t ctx = context;
37644c7070dbSScott Long 
37654c7070dbSScott Long 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
37664c7070dbSScott Long 		return;
37674c7070dbSScott Long 
37684c7070dbSScott Long 	CTX_LOCK(ctx);
37694c7070dbSScott Long 	IFDI_VFLR_HANDLE(ctx);
37704c7070dbSScott Long 	CTX_UNLOCK(ctx);
37714c7070dbSScott Long }
37724c7070dbSScott Long 
37734c7070dbSScott Long static int
37744c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
37754c7070dbSScott Long {
37764c7070dbSScott Long 	int err;
37774c7070dbSScott Long 	if_int_delay_info_t info;
37784c7070dbSScott Long 	if_ctx_t ctx;
37794c7070dbSScott Long 
37804c7070dbSScott Long 	info = (if_int_delay_info_t)arg1;
37814c7070dbSScott Long 	ctx = info->iidi_ctx;
37824c7070dbSScott Long 	info->iidi_req = req;
37834c7070dbSScott Long 	info->iidi_oidp = oidp;
37844c7070dbSScott Long 	CTX_LOCK(ctx);
37854c7070dbSScott Long 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
37864c7070dbSScott Long 	CTX_UNLOCK(ctx);
37874c7070dbSScott Long 	return (err);
37884c7070dbSScott Long }
37894c7070dbSScott Long 
37904c7070dbSScott Long /*********************************************************************
37914c7070dbSScott Long  *
37924c7070dbSScott Long  *  IFNET FUNCTIONS
37934c7070dbSScott Long  *
37944c7070dbSScott Long  **********************************************************************/
37954c7070dbSScott Long 
37964c7070dbSScott Long static void
37974c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx)
37984c7070dbSScott Long {
37994c7070dbSScott Long 	iflib_stop(ctx);
38004c7070dbSScott Long 	iflib_init_locked(ctx);
38014c7070dbSScott Long }
38024c7070dbSScott Long 
38034c7070dbSScott Long 
38044c7070dbSScott Long static void
38054c7070dbSScott Long iflib_if_init(void *arg)
38064c7070dbSScott Long {
38074c7070dbSScott Long 	if_ctx_t ctx = arg;
38084c7070dbSScott Long 
38094c7070dbSScott Long 	CTX_LOCK(ctx);
38104c7070dbSScott Long 	iflib_if_init_locked(ctx);
38114c7070dbSScott Long 	CTX_UNLOCK(ctx);
38124c7070dbSScott Long }
38134c7070dbSScott Long 
38144c7070dbSScott Long static int
38154c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m)
38164c7070dbSScott Long {
38174c7070dbSScott Long 	if_ctx_t	ctx = if_getsoftc(ifp);
38184c7070dbSScott Long 
38194c7070dbSScott Long 	iflib_txq_t txq;
382023ac9029SStephen Hurd 	int err, qidx;
38214c7070dbSScott Long 
38224c7070dbSScott Long 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
38234c7070dbSScott Long 		DBG_COUNTER_INC(tx_frees);
38244c7070dbSScott Long 		m_freem(m);
3825da69b8f9SSean Bruno 		return (ENOBUFS);
38264c7070dbSScott Long 	}
38274c7070dbSScott Long 
382823ac9029SStephen Hurd 	MPASS(m->m_nextpkt == NULL);
38294c7070dbSScott Long 	qidx = 0;
38304c7070dbSScott Long 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
38314c7070dbSScott Long 		qidx = QIDX(ctx, m);
38324c7070dbSScott Long 	/*
38334c7070dbSScott Long 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
38344c7070dbSScott Long 	 */
38354c7070dbSScott Long 	txq = &ctx->ifc_txqs[qidx];
38364c7070dbSScott Long 
38374c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
38384c7070dbSScott Long 	if (txq->ift_closed) {
38394c7070dbSScott Long 		while (m != NULL) {
38404c7070dbSScott Long 			next = m->m_nextpkt;
38414c7070dbSScott Long 			m->m_nextpkt = NULL;
38424c7070dbSScott Long 			m_freem(m);
38434c7070dbSScott Long 			m = next;
38444c7070dbSScott Long 		}
38454c7070dbSScott Long 		return (ENOBUFS);
38464c7070dbSScott Long 	}
38474c7070dbSScott Long #endif
384823ac9029SStephen Hurd #ifdef notyet
38494c7070dbSScott Long 	qidx = count = 0;
38504c7070dbSScott Long 	mp = marr;
38514c7070dbSScott Long 	next = m;
38524c7070dbSScott Long 	do {
38534c7070dbSScott Long 		count++;
38544c7070dbSScott Long 		next = next->m_nextpkt;
38554c7070dbSScott Long 	} while (next != NULL);
38564c7070dbSScott Long 
385716fb86abSConrad Meyer 	if (count > nitems(marr))
38584c7070dbSScott Long 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
38594c7070dbSScott Long 			/* XXX check nextpkt */
38604c7070dbSScott Long 			m_freem(m);
38614c7070dbSScott Long 			/* XXX simplify for now */
38624c7070dbSScott Long 			DBG_COUNTER_INC(tx_frees);
38634c7070dbSScott Long 			return (ENOBUFS);
38644c7070dbSScott Long 		}
38654c7070dbSScott Long 	for (next = m, i = 0; next != NULL; i++) {
38664c7070dbSScott Long 		mp[i] = next;
38674c7070dbSScott Long 		next = next->m_nextpkt;
38684c7070dbSScott Long 		mp[i]->m_nextpkt = NULL;
38694c7070dbSScott Long 	}
387023ac9029SStephen Hurd #endif
38714c7070dbSScott Long 	DBG_COUNTER_INC(tx_seen);
387295246abbSSean Bruno 	err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE);
38734c7070dbSScott Long 
3874ab2e3f79SStephen Hurd 	GROUPTASK_ENQUEUE(&txq->ift_task);
38751225d9daSStephen Hurd 	if (err) {
38764c7070dbSScott Long 		/* support forthcoming later */
38774c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE
38784c7070dbSScott Long 		txq->ift_closed = TRUE;
38794c7070dbSScott Long #endif
388095246abbSSean Bruno 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
388123ac9029SStephen Hurd 		m_freem(m);
38824c7070dbSScott Long 	}
38834c7070dbSScott Long 
38844c7070dbSScott Long 	return (err);
38854c7070dbSScott Long }
38864c7070dbSScott Long 
38874c7070dbSScott Long static void
38884c7070dbSScott Long iflib_if_qflush(if_t ifp)
38894c7070dbSScott Long {
38904c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
38914c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
38924c7070dbSScott Long 	int i;
38934c7070dbSScott Long 
38947b610b60SSean Bruno 	STATE_LOCK(ctx);
38954c7070dbSScott Long 	ctx->ifc_flags |= IFC_QFLUSH;
38967b610b60SSean Bruno 	STATE_UNLOCK(ctx);
38974c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
389895246abbSSean Bruno 		while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
38994c7070dbSScott Long 			iflib_txq_check_drain(txq, 0);
39007b610b60SSean Bruno 	STATE_LOCK(ctx);
39014c7070dbSScott Long 	ctx->ifc_flags &= ~IFC_QFLUSH;
39027b610b60SSean Bruno 	STATE_UNLOCK(ctx);
39034c7070dbSScott Long 
39044c7070dbSScott Long 	if_qflush(ifp);
39054c7070dbSScott Long }
39064c7070dbSScott Long 
39074c7070dbSScott Long 
390823ac9029SStephen Hurd #define IFCAP_FLAGS (IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
390918a660b3SSean Bruno 		     IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
39104c7070dbSScott Long 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO)
39114c7070dbSScott Long 
39124c7070dbSScott Long static int
39134c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
39144c7070dbSScott Long {
39154c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
39164c7070dbSScott Long 	struct ifreq	*ifr = (struct ifreq *)data;
39174c7070dbSScott Long #if defined(INET) || defined(INET6)
39184c7070dbSScott Long 	struct ifaddr	*ifa = (struct ifaddr *)data;
39194c7070dbSScott Long #endif
39204c7070dbSScott Long 	bool		avoid_reset = FALSE;
39214c7070dbSScott Long 	int		err = 0, reinit = 0, bits;
39224c7070dbSScott Long 
39234c7070dbSScott Long 	switch (command) {
39244c7070dbSScott Long 	case SIOCSIFADDR:
39254c7070dbSScott Long #ifdef INET
39264c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET)
39274c7070dbSScott Long 			avoid_reset = TRUE;
39284c7070dbSScott Long #endif
39294c7070dbSScott Long #ifdef INET6
39304c7070dbSScott Long 		if (ifa->ifa_addr->sa_family == AF_INET6)
39314c7070dbSScott Long 			avoid_reset = TRUE;
39324c7070dbSScott Long #endif
39334c7070dbSScott Long 		/*
39344c7070dbSScott Long 		** Calling init results in link renegotiation,
39354c7070dbSScott Long 		** so we avoid doing it when possible.
39364c7070dbSScott Long 		*/
39374c7070dbSScott Long 		if (avoid_reset) {
39384c7070dbSScott Long 			if_setflagbits(ifp, IFF_UP,0);
39394c7070dbSScott Long 			if (!(if_getdrvflags(ifp)& IFF_DRV_RUNNING))
39404c7070dbSScott Long 				reinit = 1;
39414c7070dbSScott Long #ifdef INET
39424c7070dbSScott Long 			if (!(if_getflags(ifp) & IFF_NOARP))
39434c7070dbSScott Long 				arp_ifinit(ifp, ifa);
39444c7070dbSScott Long #endif
39454c7070dbSScott Long 		} else
39464c7070dbSScott Long 			err = ether_ioctl(ifp, command, data);
39474c7070dbSScott Long 		break;
39484c7070dbSScott Long 	case SIOCSIFMTU:
39494c7070dbSScott Long 		CTX_LOCK(ctx);
39504c7070dbSScott Long 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
39514c7070dbSScott Long 			CTX_UNLOCK(ctx);
39524c7070dbSScott Long 			break;
39534c7070dbSScott Long 		}
39544c7070dbSScott Long 		bits = if_getdrvflags(ifp);
39554c7070dbSScott Long 		/* stop the driver and free any clusters before proceeding */
39564c7070dbSScott Long 		iflib_stop(ctx);
39574c7070dbSScott Long 
39584c7070dbSScott Long 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
39597b610b60SSean Bruno 			STATE_LOCK(ctx);
39604c7070dbSScott Long 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
39614c7070dbSScott Long 				ctx->ifc_flags |= IFC_MULTISEG;
39624c7070dbSScott Long 			else
39634c7070dbSScott Long 				ctx->ifc_flags &= ~IFC_MULTISEG;
39647b610b60SSean Bruno 			STATE_UNLOCK(ctx);
39654c7070dbSScott Long 			err = if_setmtu(ifp, ifr->ifr_mtu);
39664c7070dbSScott Long 		}
39674c7070dbSScott Long 		iflib_init_locked(ctx);
39687b610b60SSean Bruno 		STATE_LOCK(ctx);
39694c7070dbSScott Long 		if_setdrvflags(ifp, bits);
39707b610b60SSean Bruno 		STATE_UNLOCK(ctx);
39714c7070dbSScott Long 		CTX_UNLOCK(ctx);
39724c7070dbSScott Long 		break;
39734c7070dbSScott Long 	case SIOCSIFFLAGS:
3974ab2e3f79SStephen Hurd 		CTX_LOCK(ctx);
3975ab2e3f79SStephen Hurd 		if (if_getflags(ifp) & IFF_UP) {
3976ab2e3f79SStephen Hurd 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3977ab2e3f79SStephen Hurd 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
3978ab2e3f79SStephen Hurd 				    (IFF_PROMISC | IFF_ALLMULTI)) {
3979ab2e3f79SStephen Hurd 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
3980ab2e3f79SStephen Hurd 				}
3981ab2e3f79SStephen Hurd 			} else
3982ab2e3f79SStephen Hurd 				reinit = 1;
3983ab2e3f79SStephen Hurd 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3984ab2e3f79SStephen Hurd 			iflib_stop(ctx);
3985ab2e3f79SStephen Hurd 		}
3986ab2e3f79SStephen Hurd 		ctx->ifc_if_flags = if_getflags(ifp);
3987ab2e3f79SStephen Hurd 		CTX_UNLOCK(ctx);
39884c7070dbSScott Long 		break;
39894c7070dbSScott Long 	case SIOCADDMULTI:
39904c7070dbSScott Long 	case SIOCDELMULTI:
39914c7070dbSScott Long 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3992ab2e3f79SStephen Hurd 			CTX_LOCK(ctx);
3993ab2e3f79SStephen Hurd 			IFDI_INTR_DISABLE(ctx);
3994ab2e3f79SStephen Hurd 			IFDI_MULTI_SET(ctx);
3995ab2e3f79SStephen Hurd 			IFDI_INTR_ENABLE(ctx);
3996ab2e3f79SStephen Hurd 			CTX_UNLOCK(ctx);
39974c7070dbSScott Long 		}
39984c7070dbSScott Long 		break;
39994c7070dbSScott Long 	case SIOCSIFMEDIA:
40004c7070dbSScott Long 		CTX_LOCK(ctx);
40014c7070dbSScott Long 		IFDI_MEDIA_SET(ctx);
40024c7070dbSScott Long 		CTX_UNLOCK(ctx);
40034c7070dbSScott Long 		/* falls thru */
40044c7070dbSScott Long 	case SIOCGIFMEDIA:
4005a027c8e9SStephen Hurd 	case SIOCGIFXMEDIA:
40064c7070dbSScott Long 		err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command);
40074c7070dbSScott Long 		break;
40084c7070dbSScott Long 	case SIOCGI2C:
40094c7070dbSScott Long 	{
40104c7070dbSScott Long 		struct ifi2creq i2c;
40114c7070dbSScott Long 
4012541d96aaSBrooks Davis 		err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
40134c7070dbSScott Long 		if (err != 0)
40144c7070dbSScott Long 			break;
40154c7070dbSScott Long 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
40164c7070dbSScott Long 			err = EINVAL;
40174c7070dbSScott Long 			break;
40184c7070dbSScott Long 		}
40194c7070dbSScott Long 		if (i2c.len > sizeof(i2c.data)) {
40204c7070dbSScott Long 			err = EINVAL;
40214c7070dbSScott Long 			break;
40224c7070dbSScott Long 		}
40234c7070dbSScott Long 
40244c7070dbSScott Long 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
4025541d96aaSBrooks Davis 			err = copyout(&i2c, ifr_data_get_ptr(ifr),
4026541d96aaSBrooks Davis 			    sizeof(i2c));
40274c7070dbSScott Long 		break;
40284c7070dbSScott Long 	}
40294c7070dbSScott Long 	case SIOCSIFCAP:
40304c7070dbSScott Long 	{
40314c7070dbSScott Long 		int mask, setmask;
40324c7070dbSScott Long 
40334c7070dbSScott Long 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
40344c7070dbSScott Long 		setmask = 0;
40354c7070dbSScott Long #ifdef TCP_OFFLOAD
40364c7070dbSScott Long 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
40374c7070dbSScott Long #endif
40384c7070dbSScott Long 		setmask |= (mask & IFCAP_FLAGS);
40394c7070dbSScott Long 
40408b2a1db9SSean Bruno 		if (setmask  & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
40418b2a1db9SSean Bruno 			setmask |= (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
40424c7070dbSScott Long 		if ((mask & IFCAP_WOL) &&
40434c7070dbSScott Long 		    (if_getcapabilities(ifp) & IFCAP_WOL) != 0)
40444c7070dbSScott Long 			setmask |= (mask & (IFCAP_WOL_MCAST|IFCAP_WOL_MAGIC));
40454c7070dbSScott Long 		if_vlancap(ifp);
40464c7070dbSScott Long 		/*
40474c7070dbSScott Long 		 * want to ensure that traffic has stopped before we change any of the flags
40484c7070dbSScott Long 		 */
40494c7070dbSScott Long 		if (setmask) {
40504c7070dbSScott Long 			CTX_LOCK(ctx);
40514c7070dbSScott Long 			bits = if_getdrvflags(ifp);
40528b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
40534c7070dbSScott Long 				iflib_stop(ctx);
40547b610b60SSean Bruno 			STATE_LOCK(ctx);
40554c7070dbSScott Long 			if_togglecapenable(ifp, setmask);
40567b610b60SSean Bruno 			STATE_UNLOCK(ctx);
40578b2a1db9SSean Bruno 			if (bits & IFF_DRV_RUNNING)
40584c7070dbSScott Long 				iflib_init_locked(ctx);
40597b610b60SSean Bruno 			STATE_LOCK(ctx);
40604c7070dbSScott Long 			if_setdrvflags(ifp, bits);
40617b610b60SSean Bruno 			STATE_UNLOCK(ctx);
40624c7070dbSScott Long 			CTX_UNLOCK(ctx);
40634c7070dbSScott Long 		}
40644c7070dbSScott Long 		break;
40654c7070dbSScott Long 	    }
40664c7070dbSScott Long 	case SIOCGPRIVATE_0:
40674c7070dbSScott Long 	case SIOCSDRVSPEC:
40684c7070dbSScott Long 	case SIOCGDRVSPEC:
40694c7070dbSScott Long 		CTX_LOCK(ctx);
40704c7070dbSScott Long 		err = IFDI_PRIV_IOCTL(ctx, command, data);
40714c7070dbSScott Long 		CTX_UNLOCK(ctx);
40724c7070dbSScott Long 		break;
40734c7070dbSScott Long 	default:
40744c7070dbSScott Long 		err = ether_ioctl(ifp, command, data);
40754c7070dbSScott Long 		break;
40764c7070dbSScott Long 	}
40774c7070dbSScott Long 	if (reinit)
40784c7070dbSScott Long 		iflib_if_init(ctx);
40794c7070dbSScott Long 	return (err);
40804c7070dbSScott Long }
40814c7070dbSScott Long 
40824c7070dbSScott Long static uint64_t
40834c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt)
40844c7070dbSScott Long {
40854c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
40864c7070dbSScott Long 
40874c7070dbSScott Long 	return (IFDI_GET_COUNTER(ctx, cnt));
40884c7070dbSScott Long }
40894c7070dbSScott Long 
40904c7070dbSScott Long /*********************************************************************
40914c7070dbSScott Long  *
40924c7070dbSScott Long  *  OTHER FUNCTIONS EXPORTED TO THE STACK
40934c7070dbSScott Long  *
40944c7070dbSScott Long  **********************************************************************/
40954c7070dbSScott Long 
40964c7070dbSScott Long static void
40974c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
40984c7070dbSScott Long {
40994c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
41004c7070dbSScott Long 
41014c7070dbSScott Long 	if ((void *)ctx != arg)
41024c7070dbSScott Long 		return;
41034c7070dbSScott Long 
41044c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
41054c7070dbSScott Long 		return;
41064c7070dbSScott Long 
41074c7070dbSScott Long 	CTX_LOCK(ctx);
41084c7070dbSScott Long 	IFDI_VLAN_REGISTER(ctx, vtag);
41094c7070dbSScott Long 	/* Re-init to load the changes */
41104c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
411121e10b16SSean Bruno 		iflib_if_init_locked(ctx);
41124c7070dbSScott Long 	CTX_UNLOCK(ctx);
41134c7070dbSScott Long }
41144c7070dbSScott Long 
41154c7070dbSScott Long static void
41164c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
41174c7070dbSScott Long {
41184c7070dbSScott Long 	if_ctx_t ctx = if_getsoftc(ifp);
41194c7070dbSScott Long 
41204c7070dbSScott Long 	if ((void *)ctx != arg)
41214c7070dbSScott Long 		return;
41224c7070dbSScott Long 
41234c7070dbSScott Long 	if ((vtag == 0) || (vtag > 4095))
41244c7070dbSScott Long 		return;
41254c7070dbSScott Long 
41264c7070dbSScott Long 	CTX_LOCK(ctx);
41274c7070dbSScott Long 	IFDI_VLAN_UNREGISTER(ctx, vtag);
41284c7070dbSScott Long 	/* Re-init to load the changes */
41294c7070dbSScott Long 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
413021e10b16SSean Bruno 		iflib_if_init_locked(ctx);
41314c7070dbSScott Long 	CTX_UNLOCK(ctx);
41324c7070dbSScott Long }
41334c7070dbSScott Long 
41344c7070dbSScott Long static void
41354c7070dbSScott Long iflib_led_func(void *arg, int onoff)
41364c7070dbSScott Long {
41374c7070dbSScott Long 	if_ctx_t ctx = arg;
41384c7070dbSScott Long 
41394c7070dbSScott Long 	CTX_LOCK(ctx);
41404c7070dbSScott Long 	IFDI_LED_FUNC(ctx, onoff);
41414c7070dbSScott Long 	CTX_UNLOCK(ctx);
41424c7070dbSScott Long }
41434c7070dbSScott Long 
41444c7070dbSScott Long /*********************************************************************
41454c7070dbSScott Long  *
41464c7070dbSScott Long  *  BUS FUNCTION DEFINITIONS
41474c7070dbSScott Long  *
41484c7070dbSScott Long  **********************************************************************/
41494c7070dbSScott Long 
41504c7070dbSScott Long int
41514c7070dbSScott Long iflib_device_probe(device_t dev)
41524c7070dbSScott Long {
41534c7070dbSScott Long 	pci_vendor_info_t *ent;
41544c7070dbSScott Long 
41554c7070dbSScott Long 	uint16_t	pci_vendor_id, pci_device_id;
41564c7070dbSScott Long 	uint16_t	pci_subvendor_id, pci_subdevice_id;
41574c7070dbSScott Long 	uint16_t	pci_rev_id;
41584c7070dbSScott Long 	if_shared_ctx_t sctx;
41594c7070dbSScott Long 
41604c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
41614c7070dbSScott Long 		return (ENOTSUP);
41624c7070dbSScott Long 
41634c7070dbSScott Long 	pci_vendor_id = pci_get_vendor(dev);
41644c7070dbSScott Long 	pci_device_id = pci_get_device(dev);
41654c7070dbSScott Long 	pci_subvendor_id = pci_get_subvendor(dev);
41664c7070dbSScott Long 	pci_subdevice_id = pci_get_subdevice(dev);
41674c7070dbSScott Long 	pci_rev_id = pci_get_revid(dev);
41684c7070dbSScott Long 	if (sctx->isc_parse_devinfo != NULL)
41694c7070dbSScott Long 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
41704c7070dbSScott Long 
41714c7070dbSScott Long 	ent = sctx->isc_vendor_info;
41724c7070dbSScott Long 	while (ent->pvi_vendor_id != 0) {
41734c7070dbSScott Long 		if (pci_vendor_id != ent->pvi_vendor_id) {
41744c7070dbSScott Long 			ent++;
41754c7070dbSScott Long 			continue;
41764c7070dbSScott Long 		}
41774c7070dbSScott Long 		if ((pci_device_id == ent->pvi_device_id) &&
41784c7070dbSScott Long 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
41794c7070dbSScott Long 		     (ent->pvi_subvendor_id == 0)) &&
41804c7070dbSScott Long 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
41814c7070dbSScott Long 		     (ent->pvi_subdevice_id == 0)) &&
41824c7070dbSScott Long 		    ((pci_rev_id == ent->pvi_rev_id) ||
41834c7070dbSScott Long 		     (ent->pvi_rev_id == 0))) {
41844c7070dbSScott Long 
41854c7070dbSScott Long 			device_set_desc_copy(dev, ent->pvi_name);
41864c7070dbSScott Long 			/* this needs to be changed to zero if the bus probing code
41874c7070dbSScott Long 			 * ever stops re-probing on best match because the sctx
41884c7070dbSScott Long 			 * may have its values over written by register calls
41894c7070dbSScott Long 			 * in subsequent probes
41904c7070dbSScott Long 			 */
41914c7070dbSScott Long 			return (BUS_PROBE_DEFAULT);
41924c7070dbSScott Long 		}
41934c7070dbSScott Long 		ent++;
41944c7070dbSScott Long 	}
41954c7070dbSScott Long 	return (ENXIO);
41964c7070dbSScott Long }
41974c7070dbSScott Long 
41984c7070dbSScott Long int
41994c7070dbSScott Long iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
42004c7070dbSScott Long {
42014c7070dbSScott Long 	int err, rid, msix, msix_bar;
42024c7070dbSScott Long 	if_ctx_t ctx;
42034c7070dbSScott Long 	if_t ifp;
42044c7070dbSScott Long 	if_softc_ctx_t scctx;
420523ac9029SStephen Hurd 	int i;
420623ac9029SStephen Hurd 	uint16_t main_txq;
420723ac9029SStephen Hurd 	uint16_t main_rxq;
42084c7070dbSScott Long 
42094c7070dbSScott Long 
42104c7070dbSScott Long 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
42114c7070dbSScott Long 
42124c7070dbSScott Long 	if (sc == NULL) {
42134c7070dbSScott Long 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
42144c7070dbSScott Long 		device_set_softc(dev, ctx);
421523ac9029SStephen Hurd 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
42164c7070dbSScott Long 	}
42174c7070dbSScott Long 
42184c7070dbSScott Long 	ctx->ifc_sctx = sctx;
42194c7070dbSScott Long 	ctx->ifc_dev = dev;
42204c7070dbSScott Long 	ctx->ifc_softc = sc;
42214c7070dbSScott Long 
42224c7070dbSScott Long 	if ((err = iflib_register(ctx)) != 0) {
42234c7070dbSScott Long 		device_printf(dev, "iflib_register failed %d\n", err);
42244c7070dbSScott Long 		return (err);
42254c7070dbSScott Long 	}
42264c7070dbSScott Long 	iflib_add_device_sysctl_pre(ctx);
422723ac9029SStephen Hurd 
422823ac9029SStephen Hurd 	scctx = &ctx->ifc_softc_ctx;
42291248952aSSean Bruno 	ifp = ctx->ifc_ifp;
423090d72813SStephen Hurd 	ctx->ifc_nhwtxqs = sctx->isc_ntxqs;
42311248952aSSean Bruno 
423223ac9029SStephen Hurd 	/*
423323ac9029SStephen Hurd 	 * XXX sanity check that ntxd & nrxd are a power of 2
423423ac9029SStephen Hurd 	 */
423523ac9029SStephen Hurd 	if (ctx->ifc_sysctl_ntxqs != 0)
423623ac9029SStephen Hurd 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
423723ac9029SStephen Hurd 	if (ctx->ifc_sysctl_nrxqs != 0)
423823ac9029SStephen Hurd 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
423923ac9029SStephen Hurd 
424023ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
424123ac9029SStephen Hurd 		if (ctx->ifc_sysctl_ntxds[i] != 0)
424223ac9029SStephen Hurd 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
424323ac9029SStephen Hurd 		else
424423ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
424523ac9029SStephen Hurd 	}
424623ac9029SStephen Hurd 
424723ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
424823ac9029SStephen Hurd 		if (ctx->ifc_sysctl_nrxds[i] != 0)
424923ac9029SStephen Hurd 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
425023ac9029SStephen Hurd 		else
425123ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
425223ac9029SStephen Hurd 	}
425323ac9029SStephen Hurd 
425423ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
425523ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
425623ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
425723ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
425823ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
425923ac9029SStephen Hurd 		}
426023ac9029SStephen Hurd 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
426123ac9029SStephen Hurd 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
426223ac9029SStephen Hurd 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
426323ac9029SStephen Hurd 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
426423ac9029SStephen Hurd 		}
426523ac9029SStephen Hurd 	}
426623ac9029SStephen Hurd 
426723ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
426823ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
426923ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
427023ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
427123ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
427223ac9029SStephen Hurd 		}
427323ac9029SStephen Hurd 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
427423ac9029SStephen Hurd 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
427523ac9029SStephen Hurd 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
427623ac9029SStephen Hurd 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
427723ac9029SStephen Hurd 		}
427823ac9029SStephen Hurd 	}
4279ab2e3f79SStephen Hurd 
4280aa8a24d3SStephen Hurd 	CTX_LOCK(ctx);
4281ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
4282aa8a24d3SStephen Hurd 		CTX_UNLOCK(ctx);
42834c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
42844c7070dbSScott Long 		return (err);
42854c7070dbSScott Long 	}
42861248952aSSean Bruno 	_iflib_pre_assert(scctx);
42871248952aSSean Bruno 	ctx->ifc_txrx = *scctx->isc_txrx;
42881248952aSSean Bruno 
42891248952aSSean Bruno #ifdef INVARIANTS
42901248952aSSean Bruno 	MPASS(scctx->isc_capenable);
42911248952aSSean Bruno 	if (scctx->isc_capenable & IFCAP_TXCSUM)
42921248952aSSean Bruno 		MPASS(scctx->isc_tx_csum_flags);
42931248952aSSean Bruno #endif
42941248952aSSean Bruno 
429518a660b3SSean Bruno 	if_setcapabilities(ifp, scctx->isc_capenable | IFCAP_HWSTATS);
429618a660b3SSean Bruno 	if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS);
42971248952aSSean Bruno 
42981248952aSSean Bruno 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
42991248952aSSean Bruno 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
43001248952aSSean Bruno 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
43011248952aSSean Bruno 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
430223ac9029SStephen Hurd 
43034c7070dbSScott Long #ifdef ACPI_DMAR
43044c7070dbSScott Long 	if (dmar_get_dma_tag(device_get_parent(dev), dev) != NULL)
43054c7070dbSScott Long 		ctx->ifc_flags |= IFC_DMAR;
430695246abbSSean Bruno #elif !(defined(__i386__) || defined(__amd64__))
430795246abbSSean Bruno 	/* set unconditionally for !x86 */
430895246abbSSean Bruno 	ctx->ifc_flags |= IFC_DMAR;
43094c7070dbSScott Long #endif
43104c7070dbSScott Long 
43114c7070dbSScott Long 	msix_bar = scctx->isc_msix_bar;
431295246abbSSean Bruno 	main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
431395246abbSSean Bruno 	main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
431423ac9029SStephen Hurd 
431523ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
431623ac9029SStephen Hurd 	device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
431723ac9029SStephen Hurd 		      scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
431823ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_nrxqs; i++) {
431923ac9029SStephen Hurd 		if (!powerof2(scctx->isc_nrxd[i])) {
432023ac9029SStephen Hurd 			/* round down instead? */
432123ac9029SStephen Hurd 			device_printf(dev, "# rx descriptors must be a power of 2\n");
432223ac9029SStephen Hurd 			err = EINVAL;
432323ac9029SStephen Hurd 			goto fail;
432423ac9029SStephen Hurd 		}
432523ac9029SStephen Hurd 	}
432623ac9029SStephen Hurd 	for (i = 0; i < sctx->isc_ntxqs; i++) {
432723ac9029SStephen Hurd 		if (!powerof2(scctx->isc_ntxd[i])) {
432823ac9029SStephen Hurd 			device_printf(dev,
432923ac9029SStephen Hurd 			    "# tx descriptors must be a power of 2");
433023ac9029SStephen Hurd 			err = EINVAL;
433123ac9029SStephen Hurd 			goto fail;
433223ac9029SStephen Hurd 		}
433323ac9029SStephen Hurd 	}
433423ac9029SStephen Hurd 
433523ac9029SStephen Hurd 	if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
433623ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
433723ac9029SStephen Hurd 		scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
433823ac9029SStephen Hurd 		    MAX_SINGLE_PACKET_FRACTION);
433923ac9029SStephen Hurd 	if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
434023ac9029SStephen Hurd 	    MAX_SINGLE_PACKET_FRACTION)
434123ac9029SStephen Hurd 		scctx->isc_tx_tso_segments_max = max(1,
434223ac9029SStephen Hurd 		    scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
43434c7070dbSScott Long 
43444c7070dbSScott Long 	/*
43454c7070dbSScott Long 	 * Protect the stack against modern hardware
43464c7070dbSScott Long 	 */
43474c7070dbSScott Long 	if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
43484c7070dbSScott Long 		scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
43494c7070dbSScott Long 
43504c7070dbSScott Long 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
43514c7070dbSScott Long 	ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
43524c7070dbSScott Long 	ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
43534c7070dbSScott Long 	ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
43544c7070dbSScott Long 	if (scctx->isc_rss_table_size == 0)
43554c7070dbSScott Long 		scctx->isc_rss_table_size = 64;
435623ac9029SStephen Hurd 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
4357da69b8f9SSean Bruno 
4358da69b8f9SSean Bruno 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
4359da69b8f9SSean Bruno 	/* XXX format name */
4360ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
4361e516b535SStephen Hurd 
4362772593dbSStephen Hurd 	/* Set up cpu set.  If it fails, use the set of all CPUs. */
4363e516b535SStephen Hurd 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) {
4364e516b535SStephen Hurd 		device_printf(dev, "Unable to fetch CPU list\n");
4365e516b535SStephen Hurd 		CPU_COPY(&all_cpus, &ctx->ifc_cpus);
4366e516b535SStephen Hurd 	}
4367e516b535SStephen Hurd 	MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0);
4368e516b535SStephen Hurd 
43694c7070dbSScott Long 	/*
43704c7070dbSScott Long 	** Now setup MSI or MSI/X, should
43714c7070dbSScott Long 	** return us the number of supported
43724c7070dbSScott Long 	** vectors. (Will be 1 for MSI)
43734c7070dbSScott Long 	*/
43744c7070dbSScott Long 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
43754c7070dbSScott Long 		msix = scctx->isc_vectors;
43764c7070dbSScott Long 	} else if (scctx->isc_msix_bar != 0)
4377f7ae9a84SSean Bruno 	       /*
4378f7ae9a84SSean Bruno 		* The simple fact that isc_msix_bar is not 0 does not mean we
4379f7ae9a84SSean Bruno 		* we have a good value there that is known to work.
4380f7ae9a84SSean Bruno 		*/
43814c7070dbSScott Long 		msix = iflib_msix_init(ctx);
43824c7070dbSScott Long 	else {
43834c7070dbSScott Long 		scctx->isc_vectors = 1;
43844c7070dbSScott Long 		scctx->isc_ntxqsets = 1;
43854c7070dbSScott Long 		scctx->isc_nrxqsets = 1;
43864c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
43874c7070dbSScott Long 		msix = 0;
43884c7070dbSScott Long 	}
43894c7070dbSScott Long 	/* Get memory for the station queues */
43904c7070dbSScott Long 	if ((err = iflib_queues_alloc(ctx))) {
43914c7070dbSScott Long 		device_printf(dev, "Unable to allocate queue memory\n");
43924c7070dbSScott Long 		goto fail;
43934c7070dbSScott Long 	}
43944c7070dbSScott Long 
43954c7070dbSScott Long 	if ((err = iflib_qset_structures_setup(ctx))) {
43964c7070dbSScott Long 		device_printf(dev, "qset structure setup failed %d\n", err);
43974c7070dbSScott Long 		goto fail_queues;
43984c7070dbSScott Long 	}
439969b7fc3eSSean Bruno 
4400bd84f700SSean Bruno 	/*
4401bd84f700SSean Bruno 	 * Group taskqueues aren't properly set up until SMP is started,
4402bd84f700SSean Bruno 	 * so we disable interrupts until we can handle them post
4403bd84f700SSean Bruno 	 * SI_SUB_SMP.
4404bd84f700SSean Bruno 	 *
4405bd84f700SSean Bruno 	 * XXX: disabling interrupts doesn't actually work, at least for
4406bd84f700SSean Bruno 	 * the non-MSI case.  When they occur before SI_SUB_SMP completes,
4407bd84f700SSean Bruno 	 * we do null handling and depend on this not causing too large an
4408bd84f700SSean Bruno 	 * interrupt storm.
4409bd84f700SSean Bruno 	 */
44101248952aSSean Bruno 	IFDI_INTR_DISABLE(ctx);
44114c7070dbSScott Long 	if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
44124c7070dbSScott Long 		device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
44134c7070dbSScott Long 		goto fail_intr_free;
44144c7070dbSScott Long 	}
44154c7070dbSScott Long 	if (msix <= 1) {
44164c7070dbSScott Long 		rid = 0;
44174c7070dbSScott Long 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
44184c7070dbSScott Long 			MPASS(msix == 1);
44194c7070dbSScott Long 			rid = 1;
44204c7070dbSScott Long 		}
442123ac9029SStephen Hurd 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
44224c7070dbSScott Long 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
44234c7070dbSScott Long 			goto fail_intr_free;
44244c7070dbSScott Long 		}
44254c7070dbSScott Long 	}
44264c7070dbSScott Long 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
4427ab2e3f79SStephen Hurd 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
44284c7070dbSScott Long 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
44294c7070dbSScott Long 		goto fail_detach;
44304c7070dbSScott Long 	}
44314c7070dbSScott Long 	if ((err = iflib_netmap_attach(ctx))) {
44324c7070dbSScott Long 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
44334c7070dbSScott Long 		goto fail_detach;
44344c7070dbSScott Long 	}
44354c7070dbSScott Long 	*ctxp = ctx;
44364c7070dbSScott Long 
443723ac9029SStephen Hurd 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
44384c7070dbSScott Long 	iflib_add_device_sysctl_post(ctx);
44394ecb427aSSean Bruno 	ctx->ifc_flags |= IFC_INIT_DONE;
4440aa8a24d3SStephen Hurd 	CTX_UNLOCK(ctx);
44414c7070dbSScott Long 	return (0);
44424c7070dbSScott Long fail_detach:
44434c7070dbSScott Long 	ether_ifdetach(ctx->ifc_ifp);
44444c7070dbSScott Long fail_intr_free:
44454c7070dbSScott Long 	if (scctx->isc_intr == IFLIB_INTR_MSIX || scctx->isc_intr == IFLIB_INTR_MSI)
44464c7070dbSScott Long 		pci_release_msi(ctx->ifc_dev);
44474c7070dbSScott Long fail_queues:
44484c7070dbSScott Long 	/* XXX free queues */
44494c7070dbSScott Long fail:
44504c7070dbSScott Long 	IFDI_DETACH(ctx);
4451aa8a24d3SStephen Hurd 	CTX_UNLOCK(ctx);
44524c7070dbSScott Long 	return (err);
44534c7070dbSScott Long }
44544c7070dbSScott Long 
44554c7070dbSScott Long int
44564c7070dbSScott Long iflib_device_attach(device_t dev)
44574c7070dbSScott Long {
44584c7070dbSScott Long 	if_ctx_t ctx;
44594c7070dbSScott Long 	if_shared_ctx_t sctx;
44604c7070dbSScott Long 
44614c7070dbSScott Long 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
44624c7070dbSScott Long 		return (ENOTSUP);
44634c7070dbSScott Long 
44644c7070dbSScott Long 	pci_enable_busmaster(dev);
44654c7070dbSScott Long 
44664c7070dbSScott Long 	return (iflib_device_register(dev, NULL, sctx, &ctx));
44674c7070dbSScott Long }
44684c7070dbSScott Long 
44694c7070dbSScott Long int
44704c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx)
44714c7070dbSScott Long {
44724c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
44734c7070dbSScott Long 	iflib_txq_t txq;
44744c7070dbSScott Long 	iflib_rxq_t rxq;
44754c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
447687890dbaSSean Bruno 	int i, j;
44774c7070dbSScott Long 	struct taskqgroup *tqg;
447887890dbaSSean Bruno 	iflib_fl_t fl;
44794c7070dbSScott Long 
44804c7070dbSScott Long 	/* Make sure VLANS are not using driver */
44814c7070dbSScott Long 	if (if_vlantrunkinuse(ifp)) {
44824c7070dbSScott Long 		device_printf(dev,"Vlan in use, detach first\n");
44834c7070dbSScott Long 		return (EBUSY);
44844c7070dbSScott Long 	}
44854c7070dbSScott Long 
44864c7070dbSScott Long 	CTX_LOCK(ctx);
44874c7070dbSScott Long 	ctx->ifc_in_detach = 1;
44884c7070dbSScott Long 	iflib_stop(ctx);
44894c7070dbSScott Long 	CTX_UNLOCK(ctx);
44904c7070dbSScott Long 
44914c7070dbSScott Long 	/* Unregister VLAN events */
44924c7070dbSScott Long 	if (ctx->ifc_vlan_attach_event != NULL)
44934c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
44944c7070dbSScott Long 	if (ctx->ifc_vlan_detach_event != NULL)
44954c7070dbSScott Long 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
44964c7070dbSScott Long 
44974c7070dbSScott Long 	iflib_netmap_detach(ifp);
44984c7070dbSScott Long 	ether_ifdetach(ifp);
4499ab2e3f79SStephen Hurd 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
4500ab2e3f79SStephen Hurd 	CTX_LOCK_DESTROY(ctx);
45014c7070dbSScott Long 	if (ctx->ifc_led_dev != NULL)
45024c7070dbSScott Long 		led_destroy(ctx->ifc_led_dev);
45034c7070dbSScott Long 	/* XXX drain any dependent tasks */
4504ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
450523ac9029SStephen Hurd 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
45064c7070dbSScott Long 		callout_drain(&txq->ift_timer);
45074c7070dbSScott Long 		if (txq->ift_task.gt_uniq != NULL)
45084c7070dbSScott Long 			taskqgroup_detach(tqg, &txq->ift_task);
45094c7070dbSScott Long 	}
45104c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
45114c7070dbSScott Long 		if (rxq->ifr_task.gt_uniq != NULL)
45124c7070dbSScott Long 			taskqgroup_detach(tqg, &rxq->ifr_task);
451387890dbaSSean Bruno 
451487890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
451587890dbaSSean Bruno 			free(fl->ifl_rx_bitmap, M_IFLIB);
451687890dbaSSean Bruno 
45174c7070dbSScott Long 	}
4518ab2e3f79SStephen Hurd 	tqg = qgroup_if_config_tqg;
45194c7070dbSScott Long 	if (ctx->ifc_admin_task.gt_uniq != NULL)
45204c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
45214c7070dbSScott Long 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
45224c7070dbSScott Long 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
45234c7070dbSScott Long 
45244c7070dbSScott Long 	IFDI_DETACH(ctx);
452523ac9029SStephen Hurd 	device_set_softc(ctx->ifc_dev, NULL);
45264c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
45274c7070dbSScott Long 		pci_release_msi(dev);
45284c7070dbSScott Long 	}
45294c7070dbSScott Long 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
45304c7070dbSScott Long 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
45314c7070dbSScott Long 	}
45324c7070dbSScott Long 	if (ctx->ifc_msix_mem != NULL) {
45334c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
45344c7070dbSScott Long 			ctx->ifc_softc_ctx.isc_msix_bar, ctx->ifc_msix_mem);
45354c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
45364c7070dbSScott Long 	}
45374c7070dbSScott Long 
45384c7070dbSScott Long 	bus_generic_detach(dev);
45394c7070dbSScott Long 	if_free(ifp);
45404c7070dbSScott Long 
45414c7070dbSScott Long 	iflib_tx_structures_free(ctx);
45424c7070dbSScott Long 	iflib_rx_structures_free(ctx);
454323ac9029SStephen Hurd 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
454423ac9029SStephen Hurd 		free(ctx->ifc_softc, M_IFLIB);
454523ac9029SStephen Hurd 	free(ctx, M_IFLIB);
45464c7070dbSScott Long 	return (0);
45474c7070dbSScott Long }
45484c7070dbSScott Long 
45494c7070dbSScott Long 
45504c7070dbSScott Long int
45514c7070dbSScott Long iflib_device_detach(device_t dev)
45524c7070dbSScott Long {
45534c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
45544c7070dbSScott Long 
45554c7070dbSScott Long 	return (iflib_device_deregister(ctx));
45564c7070dbSScott Long }
45574c7070dbSScott Long 
45584c7070dbSScott Long int
45594c7070dbSScott Long iflib_device_suspend(device_t dev)
45604c7070dbSScott Long {
45614c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
45624c7070dbSScott Long 
45634c7070dbSScott Long 	CTX_LOCK(ctx);
45644c7070dbSScott Long 	IFDI_SUSPEND(ctx);
45654c7070dbSScott Long 	CTX_UNLOCK(ctx);
45664c7070dbSScott Long 
45674c7070dbSScott Long 	return bus_generic_suspend(dev);
45684c7070dbSScott Long }
45694c7070dbSScott Long int
45704c7070dbSScott Long iflib_device_shutdown(device_t dev)
45714c7070dbSScott Long {
45724c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
45734c7070dbSScott Long 
45744c7070dbSScott Long 	CTX_LOCK(ctx);
45754c7070dbSScott Long 	IFDI_SHUTDOWN(ctx);
45764c7070dbSScott Long 	CTX_UNLOCK(ctx);
45774c7070dbSScott Long 
45784c7070dbSScott Long 	return bus_generic_suspend(dev);
45794c7070dbSScott Long }
45804c7070dbSScott Long 
45814c7070dbSScott Long 
45824c7070dbSScott Long int
45834c7070dbSScott Long iflib_device_resume(device_t dev)
45844c7070dbSScott Long {
45854c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
45864c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
45874c7070dbSScott Long 
45884c7070dbSScott Long 	CTX_LOCK(ctx);
45894c7070dbSScott Long 	IFDI_RESUME(ctx);
45904c7070dbSScott Long 	iflib_init_locked(ctx);
45914c7070dbSScott Long 	CTX_UNLOCK(ctx);
45924c7070dbSScott Long 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
45934c7070dbSScott Long 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
45944c7070dbSScott Long 
45954c7070dbSScott Long 	return (bus_generic_resume(dev));
45964c7070dbSScott Long }
45974c7070dbSScott Long 
45984c7070dbSScott Long int
45994c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
46004c7070dbSScott Long {
46014c7070dbSScott Long 	int error;
46024c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
46034c7070dbSScott Long 
46044c7070dbSScott Long 	CTX_LOCK(ctx);
46054c7070dbSScott Long 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
46064c7070dbSScott Long 	CTX_UNLOCK(ctx);
46074c7070dbSScott Long 
46084c7070dbSScott Long 	return (error);
46094c7070dbSScott Long }
46104c7070dbSScott Long 
46114c7070dbSScott Long void
46124c7070dbSScott Long iflib_device_iov_uninit(device_t dev)
46134c7070dbSScott Long {
46144c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
46154c7070dbSScott Long 
46164c7070dbSScott Long 	CTX_LOCK(ctx);
46174c7070dbSScott Long 	IFDI_IOV_UNINIT(ctx);
46184c7070dbSScott Long 	CTX_UNLOCK(ctx);
46194c7070dbSScott Long }
46204c7070dbSScott Long 
46214c7070dbSScott Long int
46224c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
46234c7070dbSScott Long {
46244c7070dbSScott Long 	int error;
46254c7070dbSScott Long 	if_ctx_t ctx = device_get_softc(dev);
46264c7070dbSScott Long 
46274c7070dbSScott Long 	CTX_LOCK(ctx);
46284c7070dbSScott Long 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
46294c7070dbSScott Long 	CTX_UNLOCK(ctx);
46304c7070dbSScott Long 
46314c7070dbSScott Long 	return (error);
46324c7070dbSScott Long }
46334c7070dbSScott Long 
46344c7070dbSScott Long /*********************************************************************
46354c7070dbSScott Long  *
46364c7070dbSScott Long  *  MODULE FUNCTION DEFINITIONS
46374c7070dbSScott Long  *
46384c7070dbSScott Long  **********************************************************************/
46394c7070dbSScott Long 
4640ab2e3f79SStephen Hurd /*
4641ab2e3f79SStephen Hurd  * - Start a fast taskqueue thread for each core
4642ab2e3f79SStephen Hurd  * - Start a taskqueue for control operations
4643ab2e3f79SStephen Hurd  */
46444c7070dbSScott Long static int
46454c7070dbSScott Long iflib_module_init(void)
46464c7070dbSScott Long {
46474c7070dbSScott Long 	return (0);
46484c7070dbSScott Long }
46494c7070dbSScott Long 
46504c7070dbSScott Long static int
46514c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg)
46524c7070dbSScott Long {
46534c7070dbSScott Long 	int err;
46544c7070dbSScott Long 
46554c7070dbSScott Long 	switch (what) {
46564c7070dbSScott Long 	case MOD_LOAD:
46574c7070dbSScott Long 		if ((err = iflib_module_init()) != 0)
46584c7070dbSScott Long 			return (err);
46594c7070dbSScott Long 		break;
46604c7070dbSScott Long 	case MOD_UNLOAD:
46614c7070dbSScott Long 		return (EBUSY);
46624c7070dbSScott Long 	default:
46634c7070dbSScott Long 		return (EOPNOTSUPP);
46644c7070dbSScott Long 	}
46654c7070dbSScott Long 
46664c7070dbSScott Long 	return (0);
46674c7070dbSScott Long }
46684c7070dbSScott Long 
46694c7070dbSScott Long /*********************************************************************
46704c7070dbSScott Long  *
46714c7070dbSScott Long  *  PUBLIC FUNCTION DEFINITIONS
46724c7070dbSScott Long  *     ordered as in iflib.h
46734c7070dbSScott Long  *
46744c7070dbSScott Long  **********************************************************************/
46754c7070dbSScott Long 
46764c7070dbSScott Long 
46774c7070dbSScott Long static void
46784c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx)
46794c7070dbSScott Long {
46804c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsize);
46814c7070dbSScott Long 	MPASS(sctx->isc_tx_maxsegsize);
46824c7070dbSScott Long 
46834c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsize);
46844c7070dbSScott Long 	MPASS(sctx->isc_rx_nsegments);
46854c7070dbSScott Long 	MPASS(sctx->isc_rx_maxsegsize);
46864c7070dbSScott Long 
468723ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_min[0]);
468823ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_max[0]);
468923ac9029SStephen Hurd 	MPASS(sctx->isc_nrxd_default[0]);
469023ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_min[0]);
469123ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_max[0]);
469223ac9029SStephen Hurd 	MPASS(sctx->isc_ntxd_default[0]);
46934c7070dbSScott Long }
46944c7070dbSScott Long 
46951248952aSSean Bruno static void
46961248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx)
46971248952aSSean Bruno {
46981248952aSSean Bruno 
46991248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_encap);
47001248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_flush);
47011248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
47021248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_available);
47031248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
47041248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_refill);
47051248952aSSean Bruno 	MPASS(scctx->isc_txrx->ift_rxd_flush);
47061248952aSSean Bruno }
47072fe66646SSean Bruno 
47084c7070dbSScott Long static int
47094c7070dbSScott Long iflib_register(if_ctx_t ctx)
47104c7070dbSScott Long {
47114c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
47124c7070dbSScott Long 	driver_t *driver = sctx->isc_driver;
47134c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
47144c7070dbSScott Long 	if_t ifp;
47154c7070dbSScott Long 
47164c7070dbSScott Long 	_iflib_assert(sctx);
47174c7070dbSScott Long 
4718aa8a24d3SStephen Hurd 	CTX_LOCK_INIT(ctx);
47197b610b60SSean Bruno 	STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
47204c7070dbSScott Long 	ifp = ctx->ifc_ifp = if_gethandle(IFT_ETHER);
47214c7070dbSScott Long 	if (ifp == NULL) {
47224c7070dbSScott Long 		device_printf(dev, "can not allocate ifnet structure\n");
47234c7070dbSScott Long 		return (ENOMEM);
47244c7070dbSScott Long 	}
47254c7070dbSScott Long 
47264c7070dbSScott Long 	/*
47274c7070dbSScott Long 	 * Initialize our context's device specific methods
47284c7070dbSScott Long 	 */
47294c7070dbSScott Long 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
47304c7070dbSScott Long 	kobj_class_compile((kobj_class_t) driver);
47314c7070dbSScott Long 	driver->refs++;
47324c7070dbSScott Long 
47334c7070dbSScott Long 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
47344c7070dbSScott Long 	if_setsoftc(ifp, ctx);
47354c7070dbSScott Long 	if_setdev(ifp, dev);
47364c7070dbSScott Long 	if_setinitfn(ifp, iflib_if_init);
47374c7070dbSScott Long 	if_setioctlfn(ifp, iflib_if_ioctl);
47384c7070dbSScott Long 	if_settransmitfn(ifp, iflib_if_transmit);
47394c7070dbSScott Long 	if_setqflushfn(ifp, iflib_if_qflush);
47404c7070dbSScott Long 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
47414c7070dbSScott Long 
47424c7070dbSScott Long 	ctx->ifc_vlan_attach_event =
47434c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
47444c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
47454c7070dbSScott Long 	ctx->ifc_vlan_detach_event =
47464c7070dbSScott Long 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
47474c7070dbSScott Long 							  EVENTHANDLER_PRI_FIRST);
47484c7070dbSScott Long 
47494c7070dbSScott Long 	ifmedia_init(&ctx->ifc_media, IFM_IMASK,
47504c7070dbSScott Long 					 iflib_media_change, iflib_media_status);
47514c7070dbSScott Long 
47524c7070dbSScott Long 	return (0);
47534c7070dbSScott Long }
47544c7070dbSScott Long 
47554c7070dbSScott Long 
47564c7070dbSScott Long static int
47574c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx)
47584c7070dbSScott Long {
47594c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
476023ac9029SStephen Hurd 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
47614c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
476223ac9029SStephen Hurd 	int nrxqsets = scctx->isc_nrxqsets;
476323ac9029SStephen Hurd 	int ntxqsets = scctx->isc_ntxqsets;
47644c7070dbSScott Long 	iflib_txq_t txq;
47654c7070dbSScott Long 	iflib_rxq_t rxq;
47664c7070dbSScott Long 	iflib_fl_t fl = NULL;
476723ac9029SStephen Hurd 	int i, j, cpu, err, txconf, rxconf;
47684c7070dbSScott Long 	iflib_dma_info_t ifdip;
476923ac9029SStephen Hurd 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
477023ac9029SStephen Hurd 	uint32_t *txqsizes = scctx->isc_txqsizes;
47714c7070dbSScott Long 	uint8_t nrxqs = sctx->isc_nrxqs;
47724c7070dbSScott Long 	uint8_t ntxqs = sctx->isc_ntxqs;
47734c7070dbSScott Long 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
47744c7070dbSScott Long 	caddr_t *vaddrs;
47754c7070dbSScott Long 	uint64_t *paddrs;
47764c7070dbSScott Long 
477723ac9029SStephen Hurd 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
477823ac9029SStephen Hurd 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
47794c7070dbSScott Long 
47804c7070dbSScott Long /* Allocate the TX ring struct memory */
4781*b89827a0SStephen Hurd 	if (!(ctx->ifc_txqs =
4782ac2fffa4SPedro F. Giffuni 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
4783ac2fffa4SPedro F. Giffuni 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
47844c7070dbSScott Long 		device_printf(dev, "Unable to allocate TX ring memory\n");
47854c7070dbSScott Long 		err = ENOMEM;
47864c7070dbSScott Long 		goto fail;
47874c7070dbSScott Long 	}
47884c7070dbSScott Long 
47894c7070dbSScott Long 	/* Now allocate the RX */
4790*b89827a0SStephen Hurd 	if (!(ctx->ifc_rxqs =
4791ac2fffa4SPedro F. Giffuni 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
4792ac2fffa4SPedro F. Giffuni 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
47934c7070dbSScott Long 		device_printf(dev, "Unable to allocate RX ring memory\n");
47944c7070dbSScott Long 		err = ENOMEM;
47954c7070dbSScott Long 		goto rx_fail;
47964c7070dbSScott Long 	}
47974c7070dbSScott Long 
4798*b89827a0SStephen Hurd 	txq = ctx->ifc_txqs;
4799*b89827a0SStephen Hurd 	rxq = ctx->ifc_rxqs;
48004c7070dbSScott Long 
48014c7070dbSScott Long 	/*
48024c7070dbSScott Long 	 * XXX handle allocation failure
48034c7070dbSScott Long 	 */
480496c85efbSNathan Whitehorn 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
48054c7070dbSScott Long 		/* Set up some basics */
48064c7070dbSScott Long 
48074c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
48084c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
48094c7070dbSScott Long 			err = ENOMEM;
48100d0338afSConrad Meyer 			goto err_tx_desc;
48114c7070dbSScott Long 		}
48124c7070dbSScott Long 		txq->ift_ifdi = ifdip;
48134c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, ifdip++) {
48144c7070dbSScott Long 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
48154c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
48164c7070dbSScott Long 				err = ENOMEM;
48174c7070dbSScott Long 				goto err_tx_desc;
48184c7070dbSScott Long 			}
481995246abbSSean Bruno 			txq->ift_txd_size[j] = scctx->isc_txd_size[j];
48204c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
48214c7070dbSScott Long 		}
48224c7070dbSScott Long 		txq->ift_ctx = ctx;
48234c7070dbSScott Long 		txq->ift_id = i;
482423ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
482523ac9029SStephen Hurd 			txq->ift_br_offset = 1;
482623ac9029SStephen Hurd 		} else {
482723ac9029SStephen Hurd 			txq->ift_br_offset = 0;
482823ac9029SStephen Hurd 		}
48294c7070dbSScott Long 		/* XXX fix this */
483096c85efbSNathan Whitehorn 		txq->ift_timer.c_cpu = cpu;
48314c7070dbSScott Long 
48324c7070dbSScott Long 		if (iflib_txsd_alloc(txq)) {
48334c7070dbSScott Long 			device_printf(dev, "Critical Failure setting up TX buffers\n");
48344c7070dbSScott Long 			err = ENOMEM;
48354c7070dbSScott Long 			goto err_tx_desc;
48364c7070dbSScott Long 		}
48374c7070dbSScott Long 
48384c7070dbSScott Long 		/* Initialize the TX lock */
48394c7070dbSScott Long 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout",
48404c7070dbSScott Long 		    device_get_nameunit(dev), txq->ift_id);
48414c7070dbSScott Long 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
48424c7070dbSScott Long 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
48434c7070dbSScott Long 
48444c7070dbSScott Long 		snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db",
48454c7070dbSScott Long 			 device_get_nameunit(dev), txq->ift_id);
48464c7070dbSScott Long 
484795246abbSSean Bruno 		err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
48484c7070dbSScott Long 				      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
48494c7070dbSScott Long 		if (err) {
48504c7070dbSScott Long 			/* XXX free any allocated rings */
48514c7070dbSScott Long 			device_printf(dev, "Unable to allocate buf_ring\n");
48520d0338afSConrad Meyer 			goto err_tx_desc;
48534c7070dbSScott Long 		}
48544c7070dbSScott Long 	}
48554c7070dbSScott Long 
48564c7070dbSScott Long 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
48574c7070dbSScott Long 		/* Set up some basics */
48584c7070dbSScott Long 
48594c7070dbSScott Long 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
48604c7070dbSScott Long 			device_printf(dev, "failed to allocate iflib_dma_info\n");
48614c7070dbSScott Long 			err = ENOMEM;
48620d0338afSConrad Meyer 			goto err_tx_desc;
48634c7070dbSScott Long 		}
48644c7070dbSScott Long 
48654c7070dbSScott Long 		rxq->ifr_ifdi = ifdip;
486695246abbSSean Bruno 		/* XXX this needs to be changed if #rx queues != #tx queues */
486795246abbSSean Bruno 		rxq->ifr_ntxqirq = 1;
486895246abbSSean Bruno 		rxq->ifr_txqid[0] = i;
48694c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, ifdip++) {
48704c7070dbSScott Long 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
48714c7070dbSScott Long 				device_printf(dev, "Unable to allocate Descriptor memory\n");
48724c7070dbSScott Long 				err = ENOMEM;
48734c7070dbSScott Long 				goto err_tx_desc;
48744c7070dbSScott Long 			}
48754c7070dbSScott Long 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
48764c7070dbSScott Long 		}
48774c7070dbSScott Long 		rxq->ifr_ctx = ctx;
48784c7070dbSScott Long 		rxq->ifr_id = i;
487923ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
488023ac9029SStephen Hurd 			rxq->ifr_fl_offset = 1;
48814c7070dbSScott Long 		} else {
488223ac9029SStephen Hurd 			rxq->ifr_fl_offset = 0;
48834c7070dbSScott Long 		}
48844c7070dbSScott Long 		rxq->ifr_nfl = nfree_lists;
48854c7070dbSScott Long 		if (!(fl =
4886ac2fffa4SPedro F. Giffuni 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
48874c7070dbSScott Long 			device_printf(dev, "Unable to allocate free list memory\n");
48884c7070dbSScott Long 			err = ENOMEM;
48890d0338afSConrad Meyer 			goto err_tx_desc;
48904c7070dbSScott Long 		}
48914c7070dbSScott Long 		rxq->ifr_fl = fl;
48924c7070dbSScott Long 		for (j = 0; j < nfree_lists; j++) {
489395246abbSSean Bruno 			fl[j].ifl_rxq = rxq;
489495246abbSSean Bruno 			fl[j].ifl_id = j;
489595246abbSSean Bruno 			fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
489695246abbSSean Bruno 			fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
48974c7070dbSScott Long 		}
48984c7070dbSScott Long         /* Allocate receive buffers for the ring*/
48994c7070dbSScott Long 		if (iflib_rxsd_alloc(rxq)) {
49004c7070dbSScott Long 			device_printf(dev,
49014c7070dbSScott Long 			    "Critical Failure setting up receive buffers\n");
49024c7070dbSScott Long 			err = ENOMEM;
49034c7070dbSScott Long 			goto err_rx_desc;
49044c7070dbSScott Long 		}
490587890dbaSSean Bruno 
490687890dbaSSean Bruno 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
490787890dbaSSean Bruno 			fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, M_WAITOK|M_ZERO);
49084c7070dbSScott Long 	}
49094c7070dbSScott Long 
49104c7070dbSScott Long 	/* TXQs */
49114c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
49124c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
49134c7070dbSScott Long 	for (i = 0; i < ntxqsets; i++) {
49144c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
49154c7070dbSScott Long 
49164c7070dbSScott Long 		for (j = 0; j < ntxqs; j++, di++) {
49174c7070dbSScott Long 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
49184c7070dbSScott Long 			paddrs[i*ntxqs + j] = di->idi_paddr;
49194c7070dbSScott Long 		}
49204c7070dbSScott Long 	}
49214c7070dbSScott Long 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
49224c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
49234c7070dbSScott Long 		iflib_tx_structures_free(ctx);
49244c7070dbSScott Long 		free(vaddrs, M_IFLIB);
49254c7070dbSScott Long 		free(paddrs, M_IFLIB);
49264c7070dbSScott Long 		goto err_rx_desc;
49274c7070dbSScott Long 	}
49284c7070dbSScott Long 	free(vaddrs, M_IFLIB);
49294c7070dbSScott Long 	free(paddrs, M_IFLIB);
49304c7070dbSScott Long 
49314c7070dbSScott Long 	/* RXQs */
49324c7070dbSScott Long 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
49334c7070dbSScott Long 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
49344c7070dbSScott Long 	for (i = 0; i < nrxqsets; i++) {
49354c7070dbSScott Long 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
49364c7070dbSScott Long 
49374c7070dbSScott Long 		for (j = 0; j < nrxqs; j++, di++) {
49384c7070dbSScott Long 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
49394c7070dbSScott Long 			paddrs[i*nrxqs + j] = di->idi_paddr;
49404c7070dbSScott Long 		}
49414c7070dbSScott Long 	}
49424c7070dbSScott Long 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
49434c7070dbSScott Long 		device_printf(ctx->ifc_dev, "device queue allocation failed\n");
49444c7070dbSScott Long 		iflib_tx_structures_free(ctx);
49454c7070dbSScott Long 		free(vaddrs, M_IFLIB);
49464c7070dbSScott Long 		free(paddrs, M_IFLIB);
49474c7070dbSScott Long 		goto err_rx_desc;
49484c7070dbSScott Long 	}
49494c7070dbSScott Long 	free(vaddrs, M_IFLIB);
49504c7070dbSScott Long 	free(paddrs, M_IFLIB);
49514c7070dbSScott Long 
49524c7070dbSScott Long 	return (0);
49534c7070dbSScott Long 
49544c7070dbSScott Long /* XXX handle allocation failure changes */
49554c7070dbSScott Long err_rx_desc:
49564c7070dbSScott Long err_tx_desc:
4957*b89827a0SStephen Hurd rx_fail:
49584c7070dbSScott Long 	if (ctx->ifc_rxqs != NULL)
49594c7070dbSScott Long 		free(ctx->ifc_rxqs, M_IFLIB);
49604c7070dbSScott Long 	ctx->ifc_rxqs = NULL;
49614c7070dbSScott Long 	if (ctx->ifc_txqs != NULL)
49624c7070dbSScott Long 		free(ctx->ifc_txqs, M_IFLIB);
49634c7070dbSScott Long 	ctx->ifc_txqs = NULL;
49644c7070dbSScott Long fail:
49654c7070dbSScott Long 	return (err);
49664c7070dbSScott Long }
49674c7070dbSScott Long 
49684c7070dbSScott Long static int
49694c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx)
49704c7070dbSScott Long {
49714c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
49724c7070dbSScott Long 	int i;
49734c7070dbSScott Long 
49744c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
49754c7070dbSScott Long 		iflib_txq_setup(txq);
49764c7070dbSScott Long 
49774c7070dbSScott Long 	return (0);
49784c7070dbSScott Long }
49794c7070dbSScott Long 
49804c7070dbSScott Long static void
49814c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx)
49824c7070dbSScott Long {
49834c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
49844c7070dbSScott Long 	int i, j;
49854c7070dbSScott Long 
49864c7070dbSScott Long 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
49874c7070dbSScott Long 		iflib_txq_destroy(txq);
49884c7070dbSScott Long 		for (j = 0; j < ctx->ifc_nhwtxqs; j++)
49894c7070dbSScott Long 			iflib_dma_free(&txq->ift_ifdi[j]);
49904c7070dbSScott Long 	}
49914c7070dbSScott Long 	free(ctx->ifc_txqs, M_IFLIB);
49924c7070dbSScott Long 	ctx->ifc_txqs = NULL;
49934c7070dbSScott Long 	IFDI_QUEUES_FREE(ctx);
49944c7070dbSScott Long }
49954c7070dbSScott Long 
49964c7070dbSScott Long /*********************************************************************
49974c7070dbSScott Long  *
49984c7070dbSScott Long  *  Initialize all receive rings.
49994c7070dbSScott Long  *
50004c7070dbSScott Long  **********************************************************************/
50014c7070dbSScott Long static int
50024c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx)
50034c7070dbSScott Long {
50044c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
5005aaeb188aSBjoern A. Zeeb 	int q;
5006aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
5007aaeb188aSBjoern A. Zeeb 	int i, err;
5008aaeb188aSBjoern A. Zeeb #endif
50094c7070dbSScott Long 
50104c7070dbSScott Long 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
5011aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
50124c7070dbSScott Long 		tcp_lro_free(&rxq->ifr_lc);
501323ac9029SStephen Hurd 		if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
501423ac9029SStephen Hurd 		    TCP_LRO_ENTRIES, min(1024,
501523ac9029SStephen Hurd 		    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) {
50164c7070dbSScott Long 			device_printf(ctx->ifc_dev, "LRO Initialization failed!\n");
50174c7070dbSScott Long 			goto fail;
50184c7070dbSScott Long 		}
50194c7070dbSScott Long 		rxq->ifr_lro_enabled = TRUE;
5020aaeb188aSBjoern A. Zeeb #endif
50214c7070dbSScott Long 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
50224c7070dbSScott Long 	}
50234c7070dbSScott Long 	return (0);
5024aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET)
50254c7070dbSScott Long fail:
50264c7070dbSScott Long 	/*
50274c7070dbSScott Long 	 * Free RX software descriptors allocated so far, we will only handle
50284c7070dbSScott Long 	 * the rings that completed, the failing case will have
50294c7070dbSScott Long 	 * cleaned up for itself. 'q' failed, so its the terminus.
50304c7070dbSScott Long 	 */
50314c7070dbSScott Long 	rxq = ctx->ifc_rxqs;
50324c7070dbSScott Long 	for (i = 0; i < q; ++i, rxq++) {
50334c7070dbSScott Long 		iflib_rx_sds_free(rxq);
50344c7070dbSScott Long 		rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
50354c7070dbSScott Long 	}
50364c7070dbSScott Long 	return (err);
5037aaeb188aSBjoern A. Zeeb #endif
50384c7070dbSScott Long }
50394c7070dbSScott Long 
50404c7070dbSScott Long /*********************************************************************
50414c7070dbSScott Long  *
50424c7070dbSScott Long  *  Free all receive rings.
50434c7070dbSScott Long  *
50444c7070dbSScott Long  **********************************************************************/
50454c7070dbSScott Long static void
50464c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx)
50474c7070dbSScott Long {
50484c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
50494c7070dbSScott Long 
505023ac9029SStephen Hurd 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
50514c7070dbSScott Long 		iflib_rx_sds_free(rxq);
50524c7070dbSScott Long 	}
50534c7070dbSScott Long }
50544c7070dbSScott Long 
50554c7070dbSScott Long static int
50564c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx)
50574c7070dbSScott Long {
50584c7070dbSScott Long 	int err;
50594c7070dbSScott Long 
50604c7070dbSScott Long 	if ((err = iflib_tx_structures_setup(ctx)) != 0)
50614c7070dbSScott Long 		return (err);
50624c7070dbSScott Long 
50634c7070dbSScott Long 	if ((err = iflib_rx_structures_setup(ctx)) != 0) {
50644c7070dbSScott Long 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
50654c7070dbSScott Long 		iflib_tx_structures_free(ctx);
50664c7070dbSScott Long 		iflib_rx_structures_free(ctx);
50674c7070dbSScott Long 	}
50684c7070dbSScott Long 	return (err);
50694c7070dbSScott Long }
50704c7070dbSScott Long 
50714c7070dbSScott Long int
50724c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
50734c7070dbSScott Long 				driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, char *name)
50744c7070dbSScott Long {
50754c7070dbSScott Long 
50764c7070dbSScott Long 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
50774c7070dbSScott Long }
50784c7070dbSScott Long 
5079b103855eSStephen Hurd #ifdef SMP
5080aa3c5dd8SSean Bruno static int
5081b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid)
50824c7070dbSScott Long {
5083b103855eSStephen Hurd 	cpuset_t cpus;
5084aa3c5dd8SSean Bruno 	int i, cpuid, eqid, count;
50854c7070dbSScott Long 
5086b103855eSStephen Hurd 	CPU_COPY(&ctx->ifc_cpus, &cpus);
5087b103855eSStephen Hurd 	count = CPU_COUNT(&cpus);
5088aa3c5dd8SSean Bruno 	eqid = qid % count;
50894c7070dbSScott Long 	/* clear up to the qid'th bit */
5090aa3c5dd8SSean Bruno 	for (i = 0; i < eqid; i++) {
5091b103855eSStephen Hurd 		cpuid = CPU_FFS(&cpus);
5092aa3c5dd8SSean Bruno 		MPASS(cpuid != 0);
5093b103855eSStephen Hurd 		CPU_CLR(cpuid-1, &cpus);
50944c7070dbSScott Long 	}
5095b103855eSStephen Hurd 	cpuid = CPU_FFS(&cpus);
5096aa3c5dd8SSean Bruno 	MPASS(cpuid != 0);
5097aa3c5dd8SSean Bruno 	return (cpuid-1);
50984c7070dbSScott Long }
50994c7070dbSScott Long 
5100b103855eSStephen Hurd #ifdef SCHED_ULE
5101b103855eSStephen Hurd extern struct cpu_group *cpu_top;              /* CPU topology */
5102b103855eSStephen Hurd 
5103b103855eSStephen Hurd static int
5104b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp)
5105b103855eSStephen Hurd {
5106b103855eSStephen Hurd 	int i;
5107b103855eSStephen Hurd 
5108b103855eSStephen Hurd 	if (grp->cg_children == 0)
5109b103855eSStephen Hurd 		return -1;
5110b103855eSStephen Hurd 
5111b103855eSStephen Hurd 	MPASS(grp->cg_child);
5112b103855eSStephen Hurd 	for (i = 0; i < grp->cg_children; i++) {
5113b103855eSStephen Hurd 		if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask))
5114b103855eSStephen Hurd 			return i;
5115b103855eSStephen Hurd 	}
5116b103855eSStephen Hurd 
5117b103855eSStephen Hurd 	return -1;
5118b103855eSStephen Hurd }
5119b103855eSStephen Hurd 
5120b103855eSStephen Hurd /*
51210b75ac77SStephen Hurd  * Find the nth "close" core to the specified core
51220b75ac77SStephen Hurd  * "close" is defined as the deepest level that shares
51230b75ac77SStephen Hurd  * at least an L2 cache.  With threads, this will be
51240b75ac77SStephen Hurd  * threads on the same core.  If the sahred cache is L3
51250b75ac77SStephen Hurd  * or higher, simply returns the same core.
5126b103855eSStephen Hurd  */
5127b103855eSStephen Hurd static int
51280b75ac77SStephen Hurd find_close_core(int cpu, int core_offset)
5129b103855eSStephen Hurd {
5130b103855eSStephen Hurd 	struct cpu_group *grp;
5131b103855eSStephen Hurd 	int i;
51320b75ac77SStephen Hurd 	int fcpu;
5133b103855eSStephen Hurd 	cpuset_t cs;
5134b103855eSStephen Hurd 
5135b103855eSStephen Hurd 	grp = cpu_top;
5136b103855eSStephen Hurd 	if (grp == NULL)
5137b103855eSStephen Hurd 		return cpu;
5138b103855eSStephen Hurd 	i = 0;
5139b103855eSStephen Hurd 	while ((i = find_child_with_core(cpu, grp)) != -1) {
5140b103855eSStephen Hurd 		/* If the child only has one cpu, don't descend */
5141b103855eSStephen Hurd 		if (grp->cg_child[i].cg_count <= 1)
5142b103855eSStephen Hurd 			break;
5143b103855eSStephen Hurd 		grp = &grp->cg_child[i];
5144b103855eSStephen Hurd 	}
5145b103855eSStephen Hurd 
5146b103855eSStephen Hurd 	/* If they don't share at least an L2 cache, use the same CPU */
5147b103855eSStephen Hurd 	if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE)
5148b103855eSStephen Hurd 		return cpu;
5149b103855eSStephen Hurd 
5150b103855eSStephen Hurd 	/* Now pick one */
5151b103855eSStephen Hurd 	CPU_COPY(&grp->cg_mask, &cs);
51520b75ac77SStephen Hurd 
51530b75ac77SStephen Hurd 	/* Add the selected CPU offset to core offset. */
51540b75ac77SStephen Hurd 	for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) {
51550b75ac77SStephen Hurd 		if (fcpu - 1 == cpu)
51560b75ac77SStephen Hurd 			break;
51570b75ac77SStephen Hurd 		CPU_CLR(fcpu - 1, &cs);
51580b75ac77SStephen Hurd 	}
51590b75ac77SStephen Hurd 	MPASS(fcpu);
51600b75ac77SStephen Hurd 
51610b75ac77SStephen Hurd 	core_offset += i;
51620b75ac77SStephen Hurd 
51630b75ac77SStephen Hurd 	CPU_COPY(&grp->cg_mask, &cs);
51640b75ac77SStephen Hurd 	for (i = core_offset % grp->cg_count; i > 0; i--) {
5165b103855eSStephen Hurd 		MPASS(CPU_FFS(&cs));
5166b103855eSStephen Hurd 		CPU_CLR(CPU_FFS(&cs) - 1, &cs);
5167b103855eSStephen Hurd 	}
5168b103855eSStephen Hurd 	MPASS(CPU_FFS(&cs));
5169b103855eSStephen Hurd 	return CPU_FFS(&cs) - 1;
5170b103855eSStephen Hurd }
5171b103855eSStephen Hurd #else
5172b103855eSStephen Hurd static int
51730b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused)
5174b103855eSStephen Hurd {
517597755e83SKonstantin Belousov 	return cpu;
5176b103855eSStephen Hurd }
5177b103855eSStephen Hurd #endif
5178b103855eSStephen Hurd 
5179b103855eSStephen Hurd static int
51800b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid)
5181b103855eSStephen Hurd {
5182b103855eSStephen Hurd 	switch (type) {
5183b103855eSStephen Hurd 	case IFLIB_INTR_TX:
51840b75ac77SStephen Hurd 		/* TX queues get cores which share at least an L2 cache with the corresponding RX queue */
51850b75ac77SStephen Hurd 		/* XXX handle multiple RX threads per core and more than two core per L2 group */
5186b103855eSStephen Hurd 		return qid / CPU_COUNT(&ctx->ifc_cpus) + 1;
5187b103855eSStephen Hurd 	case IFLIB_INTR_RX:
5188b103855eSStephen Hurd 	case IFLIB_INTR_RXTX:
51890b75ac77SStephen Hurd 		/* RX queues get the specified core */
5190b103855eSStephen Hurd 		return qid / CPU_COUNT(&ctx->ifc_cpus);
5191b103855eSStephen Hurd 	default:
5192b103855eSStephen Hurd 		return -1;
5193b103855eSStephen Hurd 	}
5194b103855eSStephen Hurd }
5195b103855eSStephen Hurd #else
51960b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid)	CPU_FIRST()
51970b75ac77SStephen Hurd #define find_close_core(cpuid, tid)	CPU_FIRST()
5198b103855eSStephen Hurd #define find_nth(ctx, gid)		CPU_FIRST()
5199b103855eSStephen Hurd #endif
5200b103855eSStephen Hurd 
5201b103855eSStephen Hurd /* Just to avoid copy/paste */
5202b103855eSStephen Hurd static inline int
5203b103855eSStephen Hurd iflib_irq_set_affinity(if_ctx_t ctx, int irq, iflib_intr_type_t type, int qid,
5204b103855eSStephen Hurd     struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, char *name)
5205b103855eSStephen Hurd {
5206b103855eSStephen Hurd 	int cpuid;
5207b103855eSStephen Hurd 	int err, tid;
5208b103855eSStephen Hurd 
5209b103855eSStephen Hurd 	cpuid = find_nth(ctx, qid);
52100b75ac77SStephen Hurd 	tid = get_core_offset(ctx, type, qid);
5211b103855eSStephen Hurd 	MPASS(tid >= 0);
52120b75ac77SStephen Hurd 	cpuid = find_close_core(cpuid, tid);
5213b103855eSStephen Hurd 	err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, irq, name);
5214b103855eSStephen Hurd 	if (err) {
5215b103855eSStephen Hurd 		device_printf(ctx->ifc_dev, "taskqgroup_attach_cpu failed %d\n", err);
5216b103855eSStephen Hurd 		return (err);
5217b103855eSStephen Hurd 	}
5218b103855eSStephen Hurd #ifdef notyet
5219b103855eSStephen Hurd 	if (cpuid > ctx->ifc_cpuid_highest)
5220b103855eSStephen Hurd 		ctx->ifc_cpuid_highest = cpuid;
5221b103855eSStephen Hurd #endif
5222b103855eSStephen Hurd 	return 0;
5223b103855eSStephen Hurd }
5224b103855eSStephen Hurd 
52254c7070dbSScott Long int
52264c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
52274c7070dbSScott Long 						iflib_intr_type_t type, driver_filter_t *filter,
52284c7070dbSScott Long 						void *filter_arg, int qid, char *name)
52294c7070dbSScott Long {
52304c7070dbSScott Long 	struct grouptask *gtask;
52314c7070dbSScott Long 	struct taskqgroup *tqg;
52324c7070dbSScott Long 	iflib_filter_info_t info;
523323ac9029SStephen Hurd 	gtask_fn_t *fn;
5234b103855eSStephen Hurd 	int tqrid, err;
523595246abbSSean Bruno 	driver_filter_t *intr_fast;
52364c7070dbSScott Long 	void *q;
52374c7070dbSScott Long 
52384c7070dbSScott Long 	info = &ctx->ifc_filter_info;
5239add6f7d0SSean Bruno 	tqrid = rid;
52404c7070dbSScott Long 
52414c7070dbSScott Long 	switch (type) {
52424c7070dbSScott Long 	/* XXX merge tx/rx for netmap? */
52434c7070dbSScott Long 	case IFLIB_INTR_TX:
52444c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
52454c7070dbSScott Long 		info = &ctx->ifc_txqs[qid].ift_filter_info;
52464c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
5247ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
52484c7070dbSScott Long 		fn = _task_fn_tx;
524995246abbSSean Bruno 		intr_fast = iflib_fast_intr;
5250da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
52514c7070dbSScott Long 		break;
52524c7070dbSScott Long 	case IFLIB_INTR_RX:
52534c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
52544c7070dbSScott Long 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
52554c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5256ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
52574c7070dbSScott Long 		fn = _task_fn_rx;
5258ab2e3f79SStephen Hurd 		intr_fast = iflib_fast_intr;
525995246abbSSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
526095246abbSSean Bruno 		break;
526195246abbSSean Bruno 	case IFLIB_INTR_RXTX:
526295246abbSSean Bruno 		q = &ctx->ifc_rxqs[qid];
526395246abbSSean Bruno 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
526495246abbSSean Bruno 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5265ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
526695246abbSSean Bruno 		fn = _task_fn_rx;
526795246abbSSean Bruno 		intr_fast = iflib_fast_intr_rxtx;
5268da69b8f9SSean Bruno 		GROUPTASK_INIT(gtask, 0, fn, q);
52694c7070dbSScott Long 		break;
52704c7070dbSScott Long 	case IFLIB_INTR_ADMIN:
52714c7070dbSScott Long 		q = ctx;
5272da69b8f9SSean Bruno 		tqrid = -1;
52734c7070dbSScott Long 		info = &ctx->ifc_filter_info;
52744c7070dbSScott Long 		gtask = &ctx->ifc_admin_task;
5275ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
52764c7070dbSScott Long 		fn = _task_fn_admin;
527795246abbSSean Bruno 		intr_fast = iflib_fast_intr_ctx;
52784c7070dbSScott Long 		break;
52794c7070dbSScott Long 	default:
52804c7070dbSScott Long 		panic("unknown net intr type");
52814c7070dbSScott Long 	}
52824c7070dbSScott Long 
52834c7070dbSScott Long 	info->ifi_filter = filter;
52844c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
52854c7070dbSScott Long 	info->ifi_task = gtask;
528695246abbSSean Bruno 	info->ifi_ctx = q;
52874c7070dbSScott Long 
528895246abbSSean Bruno 	err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info,  name);
5289da69b8f9SSean Bruno 	if (err != 0) {
5290da69b8f9SSean Bruno 		device_printf(ctx->ifc_dev, "_iflib_irq_alloc failed %d\n", err);
52914c7070dbSScott Long 		return (err);
5292da69b8f9SSean Bruno 	}
5293da69b8f9SSean Bruno 	if (type == IFLIB_INTR_ADMIN)
5294da69b8f9SSean Bruno 		return (0);
5295da69b8f9SSean Bruno 
52964c7070dbSScott Long 	if (tqrid != -1) {
5297b103855eSStephen Hurd 		err = iflib_irq_set_affinity(ctx, rman_get_start(irq->ii_res), type, qid, gtask, tqg, q, name);
5298b103855eSStephen Hurd 		if (err)
5299b103855eSStephen Hurd 			return (err);
5300aa3c5dd8SSean Bruno 	} else {
53011c0054d2SStephen Hurd 		taskqgroup_attach(tqg, gtask, q, rman_get_start(irq->ii_res), name);
5302aa3c5dd8SSean Bruno 	}
53034c7070dbSScott Long 
53044c7070dbSScott Long 	return (0);
53054c7070dbSScott Long }
53064c7070dbSScott Long 
53074c7070dbSScott Long void
53081c0054d2SStephen Hurd iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,  void *arg, int qid, char *name)
53094c7070dbSScott Long {
53104c7070dbSScott Long 	struct grouptask *gtask;
53114c7070dbSScott Long 	struct taskqgroup *tqg;
531223ac9029SStephen Hurd 	gtask_fn_t *fn;
53134c7070dbSScott Long 	void *q;
53141c0054d2SStephen Hurd 	int irq_num = -1;
5315b103855eSStephen Hurd 	int err;
53164c7070dbSScott Long 
53174c7070dbSScott Long 	switch (type) {
53184c7070dbSScott Long 	case IFLIB_INTR_TX:
53194c7070dbSScott Long 		q = &ctx->ifc_txqs[qid];
53204c7070dbSScott Long 		gtask = &ctx->ifc_txqs[qid].ift_task;
5321ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
53224c7070dbSScott Long 		fn = _task_fn_tx;
53231c0054d2SStephen Hurd 		if (irq != NULL)
53241c0054d2SStephen Hurd 			irq_num = rman_get_start(irq->ii_res);
53254c7070dbSScott Long 		break;
53264c7070dbSScott Long 	case IFLIB_INTR_RX:
53274c7070dbSScott Long 		q = &ctx->ifc_rxqs[qid];
53284c7070dbSScott Long 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
5329ab2e3f79SStephen Hurd 		tqg = qgroup_if_io_tqg;
53304c7070dbSScott Long 		fn = _task_fn_rx;
53311c0054d2SStephen Hurd 		if (irq != NULL)
53321c0054d2SStephen Hurd 			irq_num = rman_get_start(irq->ii_res);
53334c7070dbSScott Long 		break;
53344c7070dbSScott Long 	case IFLIB_INTR_IOV:
53354c7070dbSScott Long 		q = ctx;
53364c7070dbSScott Long 		gtask = &ctx->ifc_vflr_task;
5337ab2e3f79SStephen Hurd 		tqg = qgroup_if_config_tqg;
53384c7070dbSScott Long 		fn = _task_fn_iov;
53394c7070dbSScott Long 		break;
53404c7070dbSScott Long 	default:
53414c7070dbSScott Long 		panic("unknown net intr type");
53424c7070dbSScott Long 	}
53434c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
5344b103855eSStephen Hurd 	if (irq_num != -1) {
5345b103855eSStephen Hurd 		err = iflib_irq_set_affinity(ctx, irq_num, type, qid, gtask, tqg, q, name);
5346b103855eSStephen Hurd 		if (err)
53471c0054d2SStephen Hurd 			taskqgroup_attach(tqg, gtask, q, irq_num, name);
53484c7070dbSScott Long 	}
5349b103855eSStephen Hurd 	else {
5350b103855eSStephen Hurd 		taskqgroup_attach(tqg, gtask, q, irq_num, name);
5351b103855eSStephen Hurd 	}
5352b103855eSStephen Hurd }
53534c7070dbSScott Long 
53544c7070dbSScott Long void
53554c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
53564c7070dbSScott Long {
53574c7070dbSScott Long 	if (irq->ii_tag)
53584c7070dbSScott Long 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
53594c7070dbSScott Long 
53604c7070dbSScott Long 	if (irq->ii_res)
53614c7070dbSScott Long 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, irq->ii_rid, irq->ii_res);
53624c7070dbSScott Long }
53634c7070dbSScott Long 
53644c7070dbSScott Long static int
53654c7070dbSScott Long iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, char *name)
53664c7070dbSScott Long {
53674c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
53684c7070dbSScott Long 	iflib_rxq_t rxq = ctx->ifc_rxqs;
53694c7070dbSScott Long 	if_irq_t irq = &ctx->ifc_legacy_irq;
53704c7070dbSScott Long 	iflib_filter_info_t info;
53714c7070dbSScott Long 	struct grouptask *gtask;
53724c7070dbSScott Long 	struct taskqgroup *tqg;
537323ac9029SStephen Hurd 	gtask_fn_t *fn;
53744c7070dbSScott Long 	int tqrid;
53754c7070dbSScott Long 	void *q;
53764c7070dbSScott Long 	int err;
53774c7070dbSScott Long 
53784c7070dbSScott Long 	q = &ctx->ifc_rxqs[0];
53794c7070dbSScott Long 	info = &rxq[0].ifr_filter_info;
53804c7070dbSScott Long 	gtask = &rxq[0].ifr_task;
5381ab2e3f79SStephen Hurd 	tqg = qgroup_if_io_tqg;
53824c7070dbSScott Long 	tqrid = irq->ii_rid = *rid;
53834c7070dbSScott Long 	fn = _task_fn_rx;
53844c7070dbSScott Long 
53854c7070dbSScott Long 	ctx->ifc_flags |= IFC_LEGACY;
53864c7070dbSScott Long 	info->ifi_filter = filter;
53874c7070dbSScott Long 	info->ifi_filter_arg = filter_arg;
53884c7070dbSScott Long 	info->ifi_task = gtask;
53894ecb427aSSean Bruno 	info->ifi_ctx = ctx;
53904c7070dbSScott Long 
53914c7070dbSScott Long 	/* We allocate a single interrupt resource */
539295246abbSSean Bruno 	if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr_ctx, NULL, info, name)) != 0)
53934c7070dbSScott Long 		return (err);
53944c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, q);
53959c58cafaSStephen Hurd 	taskqgroup_attach(tqg, gtask, q, rman_get_start(irq->ii_res), name);
53964c7070dbSScott Long 
53974c7070dbSScott Long 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
53989c58cafaSStephen Hurd 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, rman_get_start(irq->ii_res), "tx");
53994c7070dbSScott Long 	return (0);
54004c7070dbSScott Long }
54014c7070dbSScott Long 
54024c7070dbSScott Long void
54034c7070dbSScott Long iflib_led_create(if_ctx_t ctx)
54044c7070dbSScott Long {
54054c7070dbSScott Long 
54064c7070dbSScott Long 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
54074c7070dbSScott Long 	    device_get_nameunit(ctx->ifc_dev));
54084c7070dbSScott Long }
54094c7070dbSScott Long 
54104c7070dbSScott Long void
54114c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
54124c7070dbSScott Long {
54134c7070dbSScott Long 
54144c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
54154c7070dbSScott Long }
54164c7070dbSScott Long 
54174c7070dbSScott Long void
54184c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
54194c7070dbSScott Long {
54204c7070dbSScott Long 
54214c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
54224c7070dbSScott Long }
54234c7070dbSScott Long 
54244c7070dbSScott Long void
54254c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx)
54264c7070dbSScott Long {
54271248952aSSean Bruno #ifdef INVARIANTS
54281248952aSSean Bruno 	struct grouptask *gtask;
54291248952aSSean Bruno 
54301248952aSSean Bruno 	gtask = &ctx->ifc_admin_task;
5431d0d0ad0aSStephen Hurd 	MPASS(gtask != NULL && gtask->gt_taskqueue != NULL);
54321248952aSSean Bruno #endif
54334c7070dbSScott Long 
54344c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
54354c7070dbSScott Long }
54364c7070dbSScott Long 
54374c7070dbSScott Long void
54384c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx)
54394c7070dbSScott Long {
54404c7070dbSScott Long 
54414c7070dbSScott Long 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
54424c7070dbSScott Long }
54434c7070dbSScott Long 
54444c7070dbSScott Long void
54454c7070dbSScott Long iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name)
54464c7070dbSScott Long {
54474c7070dbSScott Long 
5448ab2e3f79SStephen Hurd 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, -1, name);
54494c7070dbSScott Long }
54504c7070dbSScott Long 
54514c7070dbSScott Long void
5452aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn,
5453aa8a24d3SStephen Hurd 	const char *name)
54544c7070dbSScott Long {
54554c7070dbSScott Long 
54564c7070dbSScott Long 	GROUPTASK_INIT(gtask, 0, fn, ctx);
5457ab2e3f79SStephen Hurd 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, -1, name);
54584c7070dbSScott Long }
54594c7070dbSScott Long 
54604c7070dbSScott Long void
546123ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask)
546223ac9029SStephen Hurd {
546323ac9029SStephen Hurd 
5464ab2e3f79SStephen Hurd 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
546523ac9029SStephen Hurd }
546623ac9029SStephen Hurd 
546723ac9029SStephen Hurd void
546823ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
54694c7070dbSScott Long {
54704c7070dbSScott Long 	if_t ifp = ctx->ifc_ifp;
54714c7070dbSScott Long 	iflib_txq_t txq = ctx->ifc_txqs;
54724c7070dbSScott Long 
54734c7070dbSScott Long 	if_setbaudrate(ifp, baudrate);
54747b610b60SSean Bruno 	if (baudrate >= IF_Gbps(10)) {
54757b610b60SSean Bruno 		STATE_LOCK(ctx);
547695246abbSSean Bruno 		ctx->ifc_flags |= IFC_PREFETCH;
54777b610b60SSean Bruno 		STATE_UNLOCK(ctx);
54787b610b60SSean Bruno 	}
54794c7070dbSScott Long 	/* If link down, disable watchdog */
54804c7070dbSScott Long 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
54814c7070dbSScott Long 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
54824c7070dbSScott Long 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
54834c7070dbSScott Long 	}
54844c7070dbSScott Long 	ctx->ifc_link_state = link_state;
54854c7070dbSScott Long 	if_link_state_change(ifp, link_state);
54864c7070dbSScott Long }
54874c7070dbSScott Long 
54884c7070dbSScott Long static int
54894c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
54904c7070dbSScott Long {
54914c7070dbSScott Long 	int credits;
54921248952aSSean Bruno #ifdef INVARIANTS
54931248952aSSean Bruno 	int credits_pre = txq->ift_cidx_processed;
54941248952aSSean Bruno #endif
54954c7070dbSScott Long 
54964c7070dbSScott Long 	if (ctx->isc_txd_credits_update == NULL)
54974c7070dbSScott Long 		return (0);
54984c7070dbSScott Long 
549995246abbSSean Bruno 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
55004c7070dbSScott Long 		return (0);
55014c7070dbSScott Long 
55024c7070dbSScott Long 	txq->ift_processed += credits;
55034c7070dbSScott Long 	txq->ift_cidx_processed += credits;
55044c7070dbSScott Long 
55051248952aSSean Bruno 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
55064c7070dbSScott Long 	if (txq->ift_cidx_processed >= txq->ift_size)
55074c7070dbSScott Long 		txq->ift_cidx_processed -= txq->ift_size;
55084c7070dbSScott Long 	return (credits);
55094c7070dbSScott Long }
55104c7070dbSScott Long 
55114c7070dbSScott Long static int
551295246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
55134c7070dbSScott Long {
55144c7070dbSScott Long 
551523ac9029SStephen Hurd 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
551623ac9029SStephen Hurd 	    budget));
55174c7070dbSScott Long }
55184c7070dbSScott Long 
55194c7070dbSScott Long void
55204c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
55214c7070dbSScott Long 	const char *description, if_int_delay_info_t info,
55224c7070dbSScott Long 	int offset, int value)
55234c7070dbSScott Long {
55244c7070dbSScott Long 	info->iidi_ctx = ctx;
55254c7070dbSScott Long 	info->iidi_offset = offset;
55264c7070dbSScott Long 	info->iidi_value = value;
55274c7070dbSScott Long 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
55284c7070dbSScott Long 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
55294c7070dbSScott Long 	    OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
55304c7070dbSScott Long 	    info, 0, iflib_sysctl_int_delay, "I", description);
55314c7070dbSScott Long }
55324c7070dbSScott Long 
5533aa8a24d3SStephen Hurd struct sx *
55344c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx)
55354c7070dbSScott Long {
55364c7070dbSScott Long 
5537aa8a24d3SStephen Hurd 	return (&ctx->ifc_ctx_sx);
55384c7070dbSScott Long }
55394c7070dbSScott Long 
55404c7070dbSScott Long static int
55414c7070dbSScott Long iflib_msix_init(if_ctx_t ctx)
55424c7070dbSScott Long {
55434c7070dbSScott Long 	device_t dev = ctx->ifc_dev;
55444c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
55454c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
55464c7070dbSScott Long 	int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs;
55474c7070dbSScott Long 	int iflib_num_tx_queues, iflib_num_rx_queues;
55484c7070dbSScott Long 	int err, admincnt, bar;
55494c7070dbSScott Long 
5550d2735264SStephen Hurd 	iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs;
5551d2735264SStephen Hurd 	iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs;
555223ac9029SStephen Hurd 
5553d2735264SStephen Hurd 	device_printf(dev, "msix_init qsets capped at %d\n", imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets));
55541248952aSSean Bruno 
55554c7070dbSScott Long 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
55564c7070dbSScott Long 	admincnt = sctx->isc_admin_intrcnt;
55570fdea539SStephen Hurd 	/* Override by global tuneable */
55580fdea539SStephen Hurd 	{
55590fdea539SStephen Hurd 		int i;
55600fdea539SStephen Hurd 		size_t len = sizeof(i);
55610fdea539SStephen Hurd 		err = kernel_sysctlbyname(curthread, "hw.pci.enable_msix", &i, &len, NULL, 0, NULL, 0);
55620fdea539SStephen Hurd 		if (err == 0) {
55630fdea539SStephen Hurd 			if (i == 0)
55640fdea539SStephen Hurd 				goto msi;
55650fdea539SStephen Hurd 		}
55660fdea539SStephen Hurd 		else {
55670fdea539SStephen Hurd 			device_printf(dev, "unable to read hw.pci.enable_msix.");
55680fdea539SStephen Hurd 		}
55690fdea539SStephen Hurd 	}
55704c7070dbSScott Long 	/* Override by tuneable */
5571ea351d3fSSean Bruno 	if (scctx->isc_disable_msix)
55724c7070dbSScott Long 		goto msi;
55734c7070dbSScott Long 
55744c7070dbSScott Long 	/*
55754c7070dbSScott Long 	** When used in a virtualized environment
55764c7070dbSScott Long 	** PCI BUSMASTER capability may not be set
55774c7070dbSScott Long 	** so explicity set it here and rewrite
55784c7070dbSScott Long 	** the ENABLE in the MSIX control register
55794c7070dbSScott Long 	** at this point to cause the host to
55804c7070dbSScott Long 	** successfully initialize us.
55814c7070dbSScott Long 	*/
55824c7070dbSScott Long 	{
55834c7070dbSScott Long 		int msix_ctrl, rid;
55844c7070dbSScott Long 
558596eeabefSSean Bruno  		pci_enable_busmaster(dev);
5586f7ae9a84SSean Bruno 		rid = 0;
5587f7ae9a84SSean Bruno 		if (pci_find_cap(dev, PCIY_MSIX, &rid) == 0 && rid != 0) {
55884c7070dbSScott Long 			rid += PCIR_MSIX_CTRL;
55894c7070dbSScott Long 			msix_ctrl = pci_read_config(dev, rid, 2);
55904c7070dbSScott Long 			msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
55914c7070dbSScott Long 			pci_write_config(dev, rid, msix_ctrl, 2);
5592f7ae9a84SSean Bruno 		} else {
5593f7ae9a84SSean Bruno 			device_printf(dev, "PCIY_MSIX capability not found; "
5594f7ae9a84SSean Bruno 			                   "or rid %d == 0.\n", rid);
5595f7ae9a84SSean Bruno 			goto msi;
5596f7ae9a84SSean Bruno 		}
55974c7070dbSScott Long 	}
55984c7070dbSScott Long 
55994c7070dbSScott Long 	/*
56004c7070dbSScott Long 	 * bar == -1 => "trust me I know what I'm doing"
56014c7070dbSScott Long 	 * Some drivers are for hardware that is so shoddily
56024c7070dbSScott Long 	 * documented that no one knows which bars are which
56034c7070dbSScott Long 	 * so the developer has to map all bars. This hack
56044c7070dbSScott Long 	 * allows shoddy garbage to use msix in this framework.
56054c7070dbSScott Long 	 */
56064c7070dbSScott Long 	if (bar != -1) {
56074c7070dbSScott Long 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
56084c7070dbSScott Long 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
56094c7070dbSScott Long 		if (ctx->ifc_msix_mem == NULL) {
56104c7070dbSScott Long 			/* May not be enabled */
56114c7070dbSScott Long 			device_printf(dev, "Unable to map MSIX table \n");
56124c7070dbSScott Long 			goto msi;
56134c7070dbSScott Long 		}
56144c7070dbSScott Long 	}
56154c7070dbSScott Long 	/* First try MSI/X */
56164c7070dbSScott Long 	if ((msgs = pci_msix_count(dev)) == 0) { /* system has msix disabled */
56174c7070dbSScott Long 		device_printf(dev, "System has MSIX disabled \n");
56184c7070dbSScott Long 		bus_release_resource(dev, SYS_RES_MEMORY,
56194c7070dbSScott Long 		    bar, ctx->ifc_msix_mem);
56204c7070dbSScott Long 		ctx->ifc_msix_mem = NULL;
56214c7070dbSScott Long 		goto msi;
56224c7070dbSScott Long 	}
56234c7070dbSScott Long #if IFLIB_DEBUG
56244c7070dbSScott Long 	/* use only 1 qset in debug mode */
56254c7070dbSScott Long 	queuemsgs = min(msgs - admincnt, 1);
56264c7070dbSScott Long #else
56274c7070dbSScott Long 	queuemsgs = msgs - admincnt;
56284c7070dbSScott Long #endif
56294c7070dbSScott Long #ifdef RSS
56304c7070dbSScott Long 	queues = imin(queuemsgs, rss_getnumbuckets());
56314c7070dbSScott Long #else
56324c7070dbSScott Long 	queues = queuemsgs;
56334c7070dbSScott Long #endif
56344c7070dbSScott Long 	queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
56354c7070dbSScott Long 	device_printf(dev, "pxm cpus: %d queue msgs: %d admincnt: %d\n",
56364c7070dbSScott Long 				  CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
56374c7070dbSScott Long #ifdef  RSS
56384c7070dbSScott Long 	/* If we're doing RSS, clamp at the number of RSS buckets */
56394c7070dbSScott Long 	if (queues > rss_getnumbuckets())
56404c7070dbSScott Long 		queues = rss_getnumbuckets();
56414c7070dbSScott Long #endif
564223ac9029SStephen Hurd 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
564323ac9029SStephen Hurd 		rx_queues = iflib_num_rx_queues;
56444c7070dbSScott Long 	else
56454c7070dbSScott Long 		rx_queues = queues;
5646d2735264SStephen Hurd 
5647d2735264SStephen Hurd 	if (rx_queues > scctx->isc_nrxqsets)
5648d2735264SStephen Hurd 		rx_queues = scctx->isc_nrxqsets;
5649d2735264SStephen Hurd 
565023ac9029SStephen Hurd 	/*
565123ac9029SStephen Hurd 	 * We want this to be all logical CPUs by default
565223ac9029SStephen Hurd 	 */
56534c7070dbSScott Long 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
56544c7070dbSScott Long 		tx_queues = iflib_num_tx_queues;
56554c7070dbSScott Long 	else
565623ac9029SStephen Hurd 		tx_queues = mp_ncpus;
565723ac9029SStephen Hurd 
5658d2735264SStephen Hurd 	if (tx_queues > scctx->isc_ntxqsets)
5659d2735264SStephen Hurd 		tx_queues = scctx->isc_ntxqsets;
5660d2735264SStephen Hurd 
566123ac9029SStephen Hurd 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
566223ac9029SStephen Hurd #ifdef INVARIANTS
566323ac9029SStephen Hurd 		if (tx_queues != rx_queues)
566423ac9029SStephen Hurd 			device_printf(dev, "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
566523ac9029SStephen Hurd 				      min(rx_queues, tx_queues), min(rx_queues, tx_queues));
566623ac9029SStephen Hurd #endif
566723ac9029SStephen Hurd 		tx_queues = min(rx_queues, tx_queues);
566823ac9029SStephen Hurd 		rx_queues = min(rx_queues, tx_queues);
566923ac9029SStephen Hurd 	}
56704c7070dbSScott Long 
5671ab2e3f79SStephen Hurd 	device_printf(dev, "using %d rx queues %d tx queues \n", rx_queues, tx_queues);
56724c7070dbSScott Long 
5673ab2e3f79SStephen Hurd 	vectors = rx_queues + admincnt;
56744c7070dbSScott Long 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
56754c7070dbSScott Long 		device_printf(dev,
56764c7070dbSScott Long 					  "Using MSIX interrupts with %d vectors\n", vectors);
56774c7070dbSScott Long 		scctx->isc_vectors = vectors;
56784c7070dbSScott Long 		scctx->isc_nrxqsets = rx_queues;
56794c7070dbSScott Long 		scctx->isc_ntxqsets = tx_queues;
56804c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSIX;
568123ac9029SStephen Hurd 
56824c7070dbSScott Long 		return (vectors);
56834c7070dbSScott Long 	} else {
56844c7070dbSScott Long 		device_printf(dev, "failed to allocate %d msix vectors, err: %d - using MSI\n", vectors, err);
56854c7070dbSScott Long 	}
56864c7070dbSScott Long msi:
56874c7070dbSScott Long 	vectors = pci_msi_count(dev);
56884c7070dbSScott Long 	scctx->isc_nrxqsets = 1;
56894c7070dbSScott Long 	scctx->isc_ntxqsets = 1;
56904c7070dbSScott Long 	scctx->isc_vectors = vectors;
56914c7070dbSScott Long 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
56924c7070dbSScott Long 		device_printf(dev,"Using an MSI interrupt\n");
56934c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_MSI;
56944c7070dbSScott Long 	} else {
56954c7070dbSScott Long 		device_printf(dev,"Using a Legacy interrupt\n");
56964c7070dbSScott Long 		scctx->isc_intr = IFLIB_INTR_LEGACY;
56974c7070dbSScott Long 	}
56984c7070dbSScott Long 
56994c7070dbSScott Long 	return (vectors);
57004c7070dbSScott Long }
57014c7070dbSScott Long 
57024c7070dbSScott Long char * ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
57034c7070dbSScott Long 
57044c7070dbSScott Long static int
57054c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
57064c7070dbSScott Long {
57074c7070dbSScott Long 	int rc;
57084c7070dbSScott Long 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
57094c7070dbSScott Long 	struct sbuf *sb;
57104c7070dbSScott Long 	char *ring_state = "UNKNOWN";
57114c7070dbSScott Long 
57124c7070dbSScott Long 	/* XXX needed ? */
57134c7070dbSScott Long 	rc = sysctl_wire_old_buffer(req, 0);
57144c7070dbSScott Long 	MPASS(rc == 0);
57154c7070dbSScott Long 	if (rc != 0)
57164c7070dbSScott Long 		return (rc);
57174c7070dbSScott Long 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
57184c7070dbSScott Long 	MPASS(sb != NULL);
57194c7070dbSScott Long 	if (sb == NULL)
57204c7070dbSScott Long 		return (ENOMEM);
57214c7070dbSScott Long 	if (state[3] <= 3)
57224c7070dbSScott Long 		ring_state = ring_states[state[3]];
57234c7070dbSScott Long 
57244c7070dbSScott Long 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
57254c7070dbSScott Long 		    state[0], state[1], state[2], ring_state);
57264c7070dbSScott Long 	rc = sbuf_finish(sb);
57274c7070dbSScott Long 	sbuf_delete(sb);
57284c7070dbSScott Long         return(rc);
57294c7070dbSScott Long }
57304c7070dbSScott Long 
573123ac9029SStephen Hurd enum iflib_ndesc_handler {
573223ac9029SStephen Hurd 	IFLIB_NTXD_HANDLER,
573323ac9029SStephen Hurd 	IFLIB_NRXD_HANDLER,
573423ac9029SStephen Hurd };
57354c7070dbSScott Long 
573623ac9029SStephen Hurd static int
573723ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
573823ac9029SStephen Hurd {
573923ac9029SStephen Hurd 	if_ctx_t ctx = (void *)arg1;
574023ac9029SStephen Hurd 	enum iflib_ndesc_handler type = arg2;
574123ac9029SStephen Hurd 	char buf[256] = {0};
574295246abbSSean Bruno 	qidx_t *ndesc;
574323ac9029SStephen Hurd 	char *p, *next;
574423ac9029SStephen Hurd 	int nqs, rc, i;
574523ac9029SStephen Hurd 
574623ac9029SStephen Hurd 	MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER);
574723ac9029SStephen Hurd 
574823ac9029SStephen Hurd 	nqs = 8;
574923ac9029SStephen Hurd 	switch(type) {
575023ac9029SStephen Hurd 	case IFLIB_NTXD_HANDLER:
575123ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_ntxds;
575223ac9029SStephen Hurd 		if (ctx->ifc_sctx)
575323ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_ntxqs;
575423ac9029SStephen Hurd 		break;
575523ac9029SStephen Hurd 	case IFLIB_NRXD_HANDLER:
575623ac9029SStephen Hurd 		ndesc = ctx->ifc_sysctl_nrxds;
575723ac9029SStephen Hurd 		if (ctx->ifc_sctx)
575823ac9029SStephen Hurd 			nqs = ctx->ifc_sctx->isc_nrxqs;
575923ac9029SStephen Hurd 		break;
576023ac9029SStephen Hurd 	}
576123ac9029SStephen Hurd 	if (nqs == 0)
576223ac9029SStephen Hurd 		nqs = 8;
576323ac9029SStephen Hurd 
576423ac9029SStephen Hurd 	for (i=0; i<8; i++) {
576523ac9029SStephen Hurd 		if (i >= nqs)
576623ac9029SStephen Hurd 			break;
576723ac9029SStephen Hurd 		if (i)
576823ac9029SStephen Hurd 			strcat(buf, ",");
576923ac9029SStephen Hurd 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
577023ac9029SStephen Hurd 	}
577123ac9029SStephen Hurd 
577223ac9029SStephen Hurd 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
577323ac9029SStephen Hurd 	if (rc || req->newptr == NULL)
577423ac9029SStephen Hurd 		return rc;
577523ac9029SStephen Hurd 
577623ac9029SStephen Hurd 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
577723ac9029SStephen Hurd 	    i++, p = strsep(&next, " ,")) {
577823ac9029SStephen Hurd 		ndesc[i] = strtoul(p, NULL, 10);
577923ac9029SStephen Hurd 	}
578023ac9029SStephen Hurd 
578123ac9029SStephen Hurd 	return(rc);
578223ac9029SStephen Hurd }
57834c7070dbSScott Long 
57844c7070dbSScott Long #define NAME_BUFLEN 32
57854c7070dbSScott Long static void
57864c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx)
57874c7070dbSScott Long {
57884c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
57894c7070dbSScott Long 	struct sysctl_oid_list *child, *oid_list;
57904c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
57914c7070dbSScott Long 	struct sysctl_oid *node;
57924c7070dbSScott Long 
57934c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
57944c7070dbSScott Long 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
57954c7070dbSScott Long 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
57964c7070dbSScott Long 						      CTLFLAG_RD, NULL, "IFLIB fields");
57974c7070dbSScott Long 	oid_list = SYSCTL_CHILDREN(node);
57984c7070dbSScott Long 
579923ac9029SStephen Hurd 	SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
580023ac9029SStephen Hurd 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
580123ac9029SStephen Hurd 		       "driver version");
580223ac9029SStephen Hurd 
58034c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
58044c7070dbSScott Long 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
58054c7070dbSScott Long 			"# of txqs to use, 0 => use default #");
58064c7070dbSScott Long 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
580723ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
580823ac9029SStephen Hurd 			"# of rxqs to use, 0 => use default #");
580923ac9029SStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
581023ac9029SStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
581123ac9029SStephen Hurd                        "permit #txq != #rxq");
5812ea351d3fSSean Bruno 	SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix",
5813ea351d3fSSean Bruno                       CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
5814ea351d3fSSean Bruno                       "disable MSIX (default 0)");
5815f4d2154eSStephen Hurd 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget",
5816f4d2154eSStephen Hurd 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0,
5817f4d2154eSStephen Hurd                        "set the rx budget");
58184c7070dbSScott Long 
581923ac9029SStephen Hurd 	/* XXX change for per-queue sizes */
582023ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
582123ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER,
582223ac9029SStephen Hurd                        mp_ndesc_handler, "A",
582323ac9029SStephen Hurd                        "list of # of tx descriptors to use, 0 = use default #");
582423ac9029SStephen Hurd 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
582523ac9029SStephen Hurd 		       CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER,
582623ac9029SStephen Hurd                        mp_ndesc_handler, "A",
582723ac9029SStephen Hurd                        "list of # of rx descriptors to use, 0 = use default #");
58284c7070dbSScott Long }
58294c7070dbSScott Long 
58304c7070dbSScott Long static void
58314c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx)
58324c7070dbSScott Long {
58334c7070dbSScott Long 	if_shared_ctx_t sctx = ctx->ifc_sctx;
58344c7070dbSScott Long 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
58354c7070dbSScott Long         device_t dev = iflib_get_dev(ctx);
58364c7070dbSScott Long 	struct sysctl_oid_list *child;
58374c7070dbSScott Long 	struct sysctl_ctx_list *ctx_list;
58384c7070dbSScott Long 	iflib_fl_t fl;
58394c7070dbSScott Long 	iflib_txq_t txq;
58404c7070dbSScott Long 	iflib_rxq_t rxq;
58414c7070dbSScott Long 	int i, j;
58424c7070dbSScott Long 	char namebuf[NAME_BUFLEN];
58434c7070dbSScott Long 	char *qfmt;
58444c7070dbSScott Long 	struct sysctl_oid *queue_node, *fl_node, *node;
58454c7070dbSScott Long 	struct sysctl_oid_list *queue_list, *fl_list;
58464c7070dbSScott Long 	ctx_list = device_get_sysctl_ctx(dev);
58474c7070dbSScott Long 
58484c7070dbSScott Long 	node = ctx->ifc_sysctl_node;
58494c7070dbSScott Long 	child = SYSCTL_CHILDREN(node);
58504c7070dbSScott Long 
58514c7070dbSScott Long 	if (scctx->isc_ntxqsets > 100)
58524c7070dbSScott Long 		qfmt = "txq%03d";
58534c7070dbSScott Long 	else if (scctx->isc_ntxqsets > 10)
58544c7070dbSScott Long 		qfmt = "txq%02d";
58554c7070dbSScott Long 	else
58564c7070dbSScott Long 		qfmt = "txq%d";
58574c7070dbSScott Long 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
58584c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
58594c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
58604c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
58614c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
58624c7070dbSScott Long #if MEMORY_LOGGING
58634c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
58644c7070dbSScott Long 				CTLFLAG_RD,
58654c7070dbSScott Long 				&txq->ift_dequeued, "total mbufs freed");
58664c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
58674c7070dbSScott Long 				CTLFLAG_RD,
58684c7070dbSScott Long 				&txq->ift_enqueued, "total mbufs enqueued");
58694c7070dbSScott Long #endif
58704c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
58714c7070dbSScott Long 				   CTLFLAG_RD,
58724c7070dbSScott Long 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
58734c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
58744c7070dbSScott Long 				   CTLFLAG_RD,
58754c7070dbSScott Long 				   &txq->ift_pullups, "# of times m_pullup was called");
58764c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
58774c7070dbSScott Long 				   CTLFLAG_RD,
58784c7070dbSScott Long 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
58794c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
58804c7070dbSScott Long 				   CTLFLAG_RD,
588123ac9029SStephen Hurd 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
58824c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
58834c7070dbSScott Long 				   CTLFLAG_RD,
58844c7070dbSScott Long 				   &txq->ift_map_failed, "# of times dma map failed");
58854c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
58864c7070dbSScott Long 				   CTLFLAG_RD,
58874c7070dbSScott Long 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
58884c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
58894c7070dbSScott Long 				   CTLFLAG_RD,
58904c7070dbSScott Long 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
58914c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
58924c7070dbSScott Long 				   CTLFLAG_RD,
58934c7070dbSScott Long 				   &txq->ift_pidx, 1, "Producer Index");
58944c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
58954c7070dbSScott Long 				   CTLFLAG_RD,
58964c7070dbSScott Long 				   &txq->ift_cidx, 1, "Consumer Index");
58974c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
58984c7070dbSScott Long 				   CTLFLAG_RD,
58994c7070dbSScott Long 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
59004c7070dbSScott Long 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
59014c7070dbSScott Long 				   CTLFLAG_RD,
59024c7070dbSScott Long 				   &txq->ift_in_use, 1, "descriptors in use");
59034c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
59044c7070dbSScott Long 				   CTLFLAG_RD,
59054c7070dbSScott Long 				   &txq->ift_processed, "descriptors procesed for clean");
59064c7070dbSScott Long 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
59074c7070dbSScott Long 				   CTLFLAG_RD,
59084c7070dbSScott Long 				   &txq->ift_cleaned, "total cleaned");
59094c7070dbSScott Long 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
591095246abbSSean Bruno 				CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br->state),
59114c7070dbSScott Long 				0, mp_ring_state_handler, "A", "soft ring state");
59124c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
591395246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->enqueues,
59144c7070dbSScott Long 				       "# of enqueues to the mp_ring for this queue");
59154c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
591695246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->drops,
59174c7070dbSScott Long 				       "# of drops in the mp_ring for this queue");
59184c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
591995246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->starts,
59204c7070dbSScott Long 				       "# of normal consumer starts in the mp_ring for this queue");
59214c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
592295246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->stalls,
59234c7070dbSScott Long 					       "# of consumer stalls in the mp_ring for this queue");
59244c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
592595246abbSSean Bruno 			       CTLFLAG_RD, &txq->ift_br->restarts,
59264c7070dbSScott Long 				       "# of consumer restarts in the mp_ring for this queue");
59274c7070dbSScott Long 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
592895246abbSSean Bruno 				       CTLFLAG_RD, &txq->ift_br->abdications,
59294c7070dbSScott Long 				       "# of consumer abdications in the mp_ring for this queue");
59304c7070dbSScott Long 	}
59314c7070dbSScott Long 
59324c7070dbSScott Long 	if (scctx->isc_nrxqsets > 100)
59334c7070dbSScott Long 		qfmt = "rxq%03d";
59344c7070dbSScott Long 	else if (scctx->isc_nrxqsets > 10)
59354c7070dbSScott Long 		qfmt = "rxq%02d";
59364c7070dbSScott Long 	else
59374c7070dbSScott Long 		qfmt = "rxq%d";
59384c7070dbSScott Long 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
59394c7070dbSScott Long 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
59404c7070dbSScott Long 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
59414c7070dbSScott Long 					     CTLFLAG_RD, NULL, "Queue Name");
59424c7070dbSScott Long 		queue_list = SYSCTL_CHILDREN(queue_node);
594323ac9029SStephen Hurd 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
59444c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx",
59454c7070dbSScott Long 				       CTLFLAG_RD,
59464c7070dbSScott Long 				       &rxq->ifr_cq_pidx, 1, "Producer Index");
59474c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
59484c7070dbSScott Long 				       CTLFLAG_RD,
59494c7070dbSScott Long 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
59504c7070dbSScott Long 		}
5951da69b8f9SSean Bruno 
59524c7070dbSScott Long 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
59534c7070dbSScott Long 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
59544c7070dbSScott Long 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
59554c7070dbSScott Long 						     CTLFLAG_RD, NULL, "freelist Name");
59564c7070dbSScott Long 			fl_list = SYSCTL_CHILDREN(fl_node);
59574c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
59584c7070dbSScott Long 				       CTLFLAG_RD,
59594c7070dbSScott Long 				       &fl->ifl_pidx, 1, "Producer Index");
59604c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
59614c7070dbSScott Long 				       CTLFLAG_RD,
59624c7070dbSScott Long 				       &fl->ifl_cidx, 1, "Consumer Index");
59634c7070dbSScott Long 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
59644c7070dbSScott Long 				       CTLFLAG_RD,
59654c7070dbSScott Long 				       &fl->ifl_credits, 1, "credits available");
59664c7070dbSScott Long #if MEMORY_LOGGING
59674c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
59684c7070dbSScott Long 					CTLFLAG_RD,
59694c7070dbSScott Long 					&fl->ifl_m_enqueued, "mbufs allocated");
59704c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
59714c7070dbSScott Long 					CTLFLAG_RD,
59724c7070dbSScott Long 					&fl->ifl_m_dequeued, "mbufs freed");
59734c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
59744c7070dbSScott Long 					CTLFLAG_RD,
59754c7070dbSScott Long 					&fl->ifl_cl_enqueued, "clusters allocated");
59764c7070dbSScott Long 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
59774c7070dbSScott Long 					CTLFLAG_RD,
59784c7070dbSScott Long 					&fl->ifl_cl_dequeued, "clusters freed");
59794c7070dbSScott Long #endif
59804c7070dbSScott Long 
59814c7070dbSScott Long 		}
59824c7070dbSScott Long 	}
59834c7070dbSScott Long 
59844c7070dbSScott Long }
598595246abbSSean Bruno 
598695246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT
598795246abbSSean Bruno static struct mbuf *
598895246abbSSean Bruno iflib_fixup_rx(struct mbuf *m)
598995246abbSSean Bruno {
599095246abbSSean Bruno 	struct mbuf *n;
599195246abbSSean Bruno 
599295246abbSSean Bruno 	if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
599395246abbSSean Bruno 		bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
599495246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
599595246abbSSean Bruno 		n = m;
599695246abbSSean Bruno 	} else {
599795246abbSSean Bruno 		MGETHDR(n, M_NOWAIT, MT_DATA);
599895246abbSSean Bruno 		if (n == NULL) {
599995246abbSSean Bruno 			m_freem(m);
600095246abbSSean Bruno 			return (NULL);
600195246abbSSean Bruno 		}
600295246abbSSean Bruno 		bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
600395246abbSSean Bruno 		m->m_data += ETHER_HDR_LEN;
600495246abbSSean Bruno 		m->m_len -= ETHER_HDR_LEN;
600595246abbSSean Bruno 		n->m_len = ETHER_HDR_LEN;
600695246abbSSean Bruno 		M_MOVE_PKTHDR(n, m);
600795246abbSSean Bruno 		n->m_next = m;
600895246abbSSean Bruno 	}
600995246abbSSean Bruno 	return (n);
601095246abbSSean Bruno }
601195246abbSSean Bruno #endif
6012