xref: /f-stack/freebsd/net/iflib.c (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * Copyright (c) 2014-2018, Matthew Macy <[email protected]>
3a9643ea8Slogwang  * All rights reserved.
4a9643ea8Slogwang  *
5a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
6a9643ea8Slogwang  * modification, are permitted provided that the following conditions are met:
7a9643ea8Slogwang  *
8a9643ea8Slogwang  *  1. Redistributions of source code must retain the above copyright notice,
9a9643ea8Slogwang  *     this list of conditions and the following disclaimer.
10a9643ea8Slogwang  *
11a9643ea8Slogwang  *  2. Neither the name of Matthew Macy nor the names of its
12a9643ea8Slogwang  *     contributors may be used to endorse or promote products derived from
13a9643ea8Slogwang  *     this software without specific prior written permission.
14a9643ea8Slogwang  *
15a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16a9643ea8Slogwang  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a9643ea8Slogwang  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19a9643ea8Slogwang  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20a9643ea8Slogwang  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21a9643ea8Slogwang  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22a9643ea8Slogwang  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23a9643ea8Slogwang  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24a9643ea8Slogwang  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25a9643ea8Slogwang  * POSSIBILITY OF SUCH DAMAGE.
26a9643ea8Slogwang  */
27a9643ea8Slogwang 
28a9643ea8Slogwang #include <sys/cdefs.h>
29a9643ea8Slogwang __FBSDID("$FreeBSD$");
30a9643ea8Slogwang 
31a9643ea8Slogwang #include "opt_inet.h"
32a9643ea8Slogwang #include "opt_inet6.h"
33a9643ea8Slogwang #include "opt_acpi.h"
34*22ce4affSfengbojiang #include "opt_sched.h"
35a9643ea8Slogwang 
36a9643ea8Slogwang #include <sys/param.h>
37a9643ea8Slogwang #include <sys/types.h>
38a9643ea8Slogwang #include <sys/bus.h>
39a9643ea8Slogwang #include <sys/eventhandler.h>
40a9643ea8Slogwang #include <sys/kernel.h>
41a9643ea8Slogwang #include <sys/lock.h>
42a9643ea8Slogwang #include <sys/mutex.h>
43a9643ea8Slogwang #include <sys/module.h>
44a9643ea8Slogwang #include <sys/kobj.h>
45a9643ea8Slogwang #include <sys/rman.h>
46a9643ea8Slogwang #include <sys/sbuf.h>
47a9643ea8Slogwang #include <sys/smp.h>
48a9643ea8Slogwang #include <sys/socket.h>
49*22ce4affSfengbojiang #include <sys/sockio.h>
50a9643ea8Slogwang #include <sys/sysctl.h>
51a9643ea8Slogwang #include <sys/syslog.h>
52a9643ea8Slogwang #include <sys/taskqueue.h>
53a9643ea8Slogwang #include <sys/limits.h>
54a9643ea8Slogwang 
55a9643ea8Slogwang #include <net/if.h>
56a9643ea8Slogwang #include <net/if_var.h>
57a9643ea8Slogwang #include <net/if_types.h>
58a9643ea8Slogwang #include <net/if_media.h>
59a9643ea8Slogwang #include <net/bpf.h>
60a9643ea8Slogwang #include <net/ethernet.h>
61a9643ea8Slogwang #include <net/mp_ring.h>
62*22ce4affSfengbojiang #include <net/debugnet.h>
63*22ce4affSfengbojiang #include <net/pfil.h>
64*22ce4affSfengbojiang #include <net/vnet.h>
65a9643ea8Slogwang 
66a9643ea8Slogwang #include <netinet/in.h>
67a9643ea8Slogwang #include <netinet/in_pcb.h>
68a9643ea8Slogwang #include <netinet/tcp_lro.h>
69a9643ea8Slogwang #include <netinet/in_systm.h>
70a9643ea8Slogwang #include <netinet/if_ether.h>
71a9643ea8Slogwang #include <netinet/ip.h>
72a9643ea8Slogwang #include <netinet/ip6.h>
73a9643ea8Slogwang #include <netinet/tcp.h>
74*22ce4affSfengbojiang #include <netinet/ip_var.h>
75*22ce4affSfengbojiang #include <netinet6/ip6_var.h>
76a9643ea8Slogwang 
77a9643ea8Slogwang #include <machine/bus.h>
78a9643ea8Slogwang #include <machine/in_cksum.h>
79a9643ea8Slogwang 
80a9643ea8Slogwang #include <vm/vm.h>
81a9643ea8Slogwang #include <vm/pmap.h>
82a9643ea8Slogwang 
83a9643ea8Slogwang #include <dev/led/led.h>
84a9643ea8Slogwang #include <dev/pci/pcireg.h>
85a9643ea8Slogwang #include <dev/pci/pcivar.h>
86a9643ea8Slogwang #include <dev/pci/pci_private.h>
87a9643ea8Slogwang 
88a9643ea8Slogwang #include <net/iflib.h>
89*22ce4affSfengbojiang #include <net/iflib_private.h>
90a9643ea8Slogwang 
91a9643ea8Slogwang #include "ifdi_if.h"
92a9643ea8Slogwang 
93*22ce4affSfengbojiang #ifdef PCI_IOV
94*22ce4affSfengbojiang #include <dev/pci/pci_iov.h>
95a9643ea8Slogwang #endif
96a9643ea8Slogwang 
97*22ce4affSfengbojiang #include <sys/bitstring.h>
98a9643ea8Slogwang /*
99*22ce4affSfengbojiang  * enable accounting of every mbuf as it comes in to and goes out of
100*22ce4affSfengbojiang  * iflib's software descriptor references
101a9643ea8Slogwang  */
102a9643ea8Slogwang #define MEMORY_LOGGING 0
103a9643ea8Slogwang /*
104a9643ea8Slogwang  * Enable mbuf vectors for compressing long mbuf chains
105a9643ea8Slogwang  */
106a9643ea8Slogwang 
107a9643ea8Slogwang /*
108a9643ea8Slogwang  * NB:
109a9643ea8Slogwang  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
110a9643ea8Slogwang  *   we prefetch needs to be determined by the time spent in m_free vis a vis
111a9643ea8Slogwang  *   the cost of a prefetch. This will of course vary based on the workload:
112a9643ea8Slogwang  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
113a9643ea8Slogwang  *        is quite expensive, thus suggesting very little prefetch.
114a9643ea8Slogwang  *      - small packet forwarding which is just returning a single mbuf to
115a9643ea8Slogwang  *        UMA will typically be very fast vis a vis the cost of a memory
116a9643ea8Slogwang  *        access.
117a9643ea8Slogwang  */
118a9643ea8Slogwang 
119a9643ea8Slogwang /*
120a9643ea8Slogwang  * File organization:
121a9643ea8Slogwang  *  - private structures
122a9643ea8Slogwang  *  - iflib private utility functions
123a9643ea8Slogwang  *  - ifnet functions
124a9643ea8Slogwang  *  - vlan registry and other exported functions
125a9643ea8Slogwang  *  - iflib public core functions
126a9643ea8Slogwang  *
127a9643ea8Slogwang  *
128a9643ea8Slogwang  */
129*22ce4affSfengbojiang MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
130*22ce4affSfengbojiang 
131*22ce4affSfengbojiang #define	IFLIB_RXEOF_MORE (1U << 0)
132*22ce4affSfengbojiang #define	IFLIB_RXEOF_EMPTY (2U << 0)
133a9643ea8Slogwang 
134a9643ea8Slogwang struct iflib_txq;
135a9643ea8Slogwang typedef struct iflib_txq *iflib_txq_t;
136a9643ea8Slogwang struct iflib_rxq;
137a9643ea8Slogwang typedef struct iflib_rxq *iflib_rxq_t;
138a9643ea8Slogwang struct iflib_fl;
139a9643ea8Slogwang typedef struct iflib_fl *iflib_fl_t;
140a9643ea8Slogwang 
141*22ce4affSfengbojiang struct iflib_ctx;
142*22ce4affSfengbojiang 
143*22ce4affSfengbojiang static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid);
144*22ce4affSfengbojiang static void iflib_timer(void *arg);
145*22ce4affSfengbojiang static void iflib_tqg_detach(if_ctx_t ctx);
146*22ce4affSfengbojiang 
147a9643ea8Slogwang typedef struct iflib_filter_info {
148a9643ea8Slogwang 	driver_filter_t *ifi_filter;
149a9643ea8Slogwang 	void *ifi_filter_arg;
150a9643ea8Slogwang 	struct grouptask *ifi_task;
151*22ce4affSfengbojiang 	void *ifi_ctx;
152a9643ea8Slogwang } *iflib_filter_info_t;
153a9643ea8Slogwang 
154a9643ea8Slogwang struct iflib_ctx {
155a9643ea8Slogwang 	KOBJ_FIELDS;
156a9643ea8Slogwang 	/*
157a9643ea8Slogwang 	 * Pointer to hardware driver's softc
158a9643ea8Slogwang 	 */
159a9643ea8Slogwang 	void *ifc_softc;
160a9643ea8Slogwang 	device_t ifc_dev;
161a9643ea8Slogwang 	if_t ifc_ifp;
162a9643ea8Slogwang 
163a9643ea8Slogwang 	cpuset_t ifc_cpus;
164a9643ea8Slogwang 	if_shared_ctx_t ifc_sctx;
165a9643ea8Slogwang 	struct if_softc_ctx ifc_softc_ctx;
166a9643ea8Slogwang 
167*22ce4affSfengbojiang 	struct sx ifc_ctx_sx;
168*22ce4affSfengbojiang 	struct mtx ifc_state_mtx;
169a9643ea8Slogwang 
170a9643ea8Slogwang 	iflib_txq_t ifc_txqs;
171a9643ea8Slogwang 	iflib_rxq_t ifc_rxqs;
172a9643ea8Slogwang 	uint32_t ifc_if_flags;
173a9643ea8Slogwang 	uint32_t ifc_flags;
174a9643ea8Slogwang 	uint32_t ifc_max_fl_buf_size;
175*22ce4affSfengbojiang 	uint32_t ifc_rx_mbuf_sz;
176a9643ea8Slogwang 
177a9643ea8Slogwang 	int ifc_link_state;
178a9643ea8Slogwang 	int ifc_watchdog_events;
179a9643ea8Slogwang 	struct cdev *ifc_led_dev;
180a9643ea8Slogwang 	struct resource *ifc_msix_mem;
181a9643ea8Slogwang 
182a9643ea8Slogwang 	struct if_irq ifc_legacy_irq;
183a9643ea8Slogwang 	struct grouptask ifc_admin_task;
184a9643ea8Slogwang 	struct grouptask ifc_vflr_task;
185a9643ea8Slogwang 	struct iflib_filter_info ifc_filter_info;
186a9643ea8Slogwang 	struct ifmedia	ifc_media;
187*22ce4affSfengbojiang 	struct ifmedia	*ifc_mediap;
188a9643ea8Slogwang 
189a9643ea8Slogwang 	struct sysctl_oid *ifc_sysctl_node;
190a9643ea8Slogwang 	uint16_t ifc_sysctl_ntxqs;
191a9643ea8Slogwang 	uint16_t ifc_sysctl_nrxqs;
192a9643ea8Slogwang 	uint16_t ifc_sysctl_qs_eq_override;
193*22ce4affSfengbojiang 	uint16_t ifc_sysctl_rx_budget;
194*22ce4affSfengbojiang 	uint16_t ifc_sysctl_tx_abdicate;
195*22ce4affSfengbojiang 	uint16_t ifc_sysctl_core_offset;
196*22ce4affSfengbojiang #define	CORE_OFFSET_UNSPECIFIED	0xffff
197*22ce4affSfengbojiang 	uint8_t  ifc_sysctl_separate_txrx;
198a9643ea8Slogwang 
199*22ce4affSfengbojiang 	qidx_t ifc_sysctl_ntxds[8];
200*22ce4affSfengbojiang 	qidx_t ifc_sysctl_nrxds[8];
201a9643ea8Slogwang 	struct if_txrx ifc_txrx;
202a9643ea8Slogwang #define isc_txd_encap  ifc_txrx.ift_txd_encap
203a9643ea8Slogwang #define isc_txd_flush  ifc_txrx.ift_txd_flush
204a9643ea8Slogwang #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
205a9643ea8Slogwang #define isc_rxd_available ifc_txrx.ift_rxd_available
206a9643ea8Slogwang #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
207a9643ea8Slogwang #define isc_rxd_refill ifc_txrx.ift_rxd_refill
208a9643ea8Slogwang #define isc_rxd_flush ifc_txrx.ift_rxd_flush
209a9643ea8Slogwang #define isc_legacy_intr ifc_txrx.ift_legacy_intr
210a9643ea8Slogwang 	eventhandler_tag ifc_vlan_attach_event;
211a9643ea8Slogwang 	eventhandler_tag ifc_vlan_detach_event;
212*22ce4affSfengbojiang 	struct ether_addr ifc_mac;
213a9643ea8Slogwang };
214a9643ea8Slogwang 
215a9643ea8Slogwang void *
iflib_get_softc(if_ctx_t ctx)216a9643ea8Slogwang iflib_get_softc(if_ctx_t ctx)
217a9643ea8Slogwang {
218a9643ea8Slogwang 
219a9643ea8Slogwang 	return (ctx->ifc_softc);
220a9643ea8Slogwang }
221a9643ea8Slogwang 
222a9643ea8Slogwang device_t
iflib_get_dev(if_ctx_t ctx)223a9643ea8Slogwang iflib_get_dev(if_ctx_t ctx)
224a9643ea8Slogwang {
225a9643ea8Slogwang 
226a9643ea8Slogwang 	return (ctx->ifc_dev);
227a9643ea8Slogwang }
228a9643ea8Slogwang 
229a9643ea8Slogwang if_t
iflib_get_ifp(if_ctx_t ctx)230a9643ea8Slogwang iflib_get_ifp(if_ctx_t ctx)
231a9643ea8Slogwang {
232a9643ea8Slogwang 
233a9643ea8Slogwang 	return (ctx->ifc_ifp);
234a9643ea8Slogwang }
235a9643ea8Slogwang 
236a9643ea8Slogwang struct ifmedia *
iflib_get_media(if_ctx_t ctx)237a9643ea8Slogwang iflib_get_media(if_ctx_t ctx)
238a9643ea8Slogwang {
239a9643ea8Slogwang 
240*22ce4affSfengbojiang 	return (ctx->ifc_mediap);
241*22ce4affSfengbojiang }
242*22ce4affSfengbojiang 
243*22ce4affSfengbojiang uint32_t
iflib_get_flags(if_ctx_t ctx)244*22ce4affSfengbojiang iflib_get_flags(if_ctx_t ctx)
245*22ce4affSfengbojiang {
246*22ce4affSfengbojiang 	return (ctx->ifc_flags);
247a9643ea8Slogwang }
248a9643ea8Slogwang 
249a9643ea8Slogwang void
iflib_set_mac(if_ctx_t ctx,uint8_t mac[ETHER_ADDR_LEN])250a9643ea8Slogwang iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
251a9643ea8Slogwang {
252a9643ea8Slogwang 
253*22ce4affSfengbojiang 	bcopy(mac, ctx->ifc_mac.octet, ETHER_ADDR_LEN);
254a9643ea8Slogwang }
255a9643ea8Slogwang 
256a9643ea8Slogwang if_softc_ctx_t
iflib_get_softc_ctx(if_ctx_t ctx)257a9643ea8Slogwang iflib_get_softc_ctx(if_ctx_t ctx)
258a9643ea8Slogwang {
259a9643ea8Slogwang 
260a9643ea8Slogwang 	return (&ctx->ifc_softc_ctx);
261a9643ea8Slogwang }
262a9643ea8Slogwang 
263a9643ea8Slogwang if_shared_ctx_t
iflib_get_sctx(if_ctx_t ctx)264a9643ea8Slogwang iflib_get_sctx(if_ctx_t ctx)
265a9643ea8Slogwang {
266a9643ea8Slogwang 
267a9643ea8Slogwang 	return (ctx->ifc_sctx);
268a9643ea8Slogwang }
269a9643ea8Slogwang 
270*22ce4affSfengbojiang #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
271a9643ea8Slogwang #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
272*22ce4affSfengbojiang #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1)))
273a9643ea8Slogwang 
274a9643ea8Slogwang #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
275a9643ea8Slogwang #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
276a9643ea8Slogwang 
277*22ce4affSfengbojiang typedef struct iflib_sw_rx_desc_array {
278*22ce4affSfengbojiang 	bus_dmamap_t	*ifsd_map;         /* bus_dma maps for packet */
279*22ce4affSfengbojiang 	struct mbuf	**ifsd_m;           /* pkthdr mbufs */
280*22ce4affSfengbojiang 	caddr_t		*ifsd_cl;          /* direct cluster pointer for rx */
281*22ce4affSfengbojiang 	bus_addr_t	*ifsd_ba;          /* bus addr of cluster for rx */
282*22ce4affSfengbojiang } iflib_rxsd_array_t;
283a9643ea8Slogwang 
284a9643ea8Slogwang typedef struct iflib_sw_tx_desc_array {
285a9643ea8Slogwang 	bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
286*22ce4affSfengbojiang 	bus_dmamap_t	*ifsd_tso_map;     /* bus_dma maps for TSO packet */
287a9643ea8Slogwang 	struct mbuf    **ifsd_m;           /* pkthdr mbufs */
288*22ce4affSfengbojiang } if_txsd_vec_t;
289a9643ea8Slogwang 
290a9643ea8Slogwang /* magic number that should be high enough for any hardware */
291a9643ea8Slogwang #define IFLIB_MAX_TX_SEGS		128
292a9643ea8Slogwang #define IFLIB_RX_COPY_THRESH		128
293a9643ea8Slogwang #define IFLIB_MAX_RX_REFRESH		32
294*22ce4affSfengbojiang /* The minimum descriptors per second before we start coalescing */
295*22ce4affSfengbojiang #define IFLIB_MIN_DESC_SEC		16384
296*22ce4affSfengbojiang #define IFLIB_DEFAULT_TX_UPDATE_FREQ	16
297a9643ea8Slogwang #define IFLIB_QUEUE_IDLE		0
298a9643ea8Slogwang #define IFLIB_QUEUE_HUNG		1
299a9643ea8Slogwang #define IFLIB_QUEUE_WORKING		2
300*22ce4affSfengbojiang /* maximum number of txqs that can share an rx interrupt */
301*22ce4affSfengbojiang #define IFLIB_MAX_TX_SHARED_INTR	4
302a9643ea8Slogwang 
303*22ce4affSfengbojiang /* this should really scale with ring size - this is a fairly arbitrary value */
304*22ce4affSfengbojiang #define TX_BATCH_SIZE			32
305a9643ea8Slogwang 
306a9643ea8Slogwang #define IFLIB_RESTART_BUDGET		8
307a9643ea8Slogwang 
308a9643ea8Slogwang #define CSUM_OFFLOAD		(CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
309a9643ea8Slogwang 				 CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
310a9643ea8Slogwang 				 CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
311*22ce4affSfengbojiang 
312a9643ea8Slogwang struct iflib_txq {
313*22ce4affSfengbojiang 	qidx_t		ift_in_use;
314*22ce4affSfengbojiang 	qidx_t		ift_cidx;
315*22ce4affSfengbojiang 	qidx_t		ift_cidx_processed;
316*22ce4affSfengbojiang 	qidx_t		ift_pidx;
317a9643ea8Slogwang 	uint8_t		ift_gen;
318a9643ea8Slogwang 	uint8_t		ift_br_offset;
319*22ce4affSfengbojiang 	uint16_t	ift_npending;
320*22ce4affSfengbojiang 	uint16_t	ift_db_pending;
321*22ce4affSfengbojiang 	uint16_t	ift_rs_pending;
322a9643ea8Slogwang 	/* implicit pad */
323*22ce4affSfengbojiang 	uint8_t		ift_txd_size[8];
324a9643ea8Slogwang 	uint64_t	ift_processed;
325a9643ea8Slogwang 	uint64_t	ift_cleaned;
326*22ce4affSfengbojiang 	uint64_t	ift_cleaned_prev;
327a9643ea8Slogwang #if MEMORY_LOGGING
328a9643ea8Slogwang 	uint64_t	ift_enqueued;
329a9643ea8Slogwang 	uint64_t	ift_dequeued;
330a9643ea8Slogwang #endif
331a9643ea8Slogwang 	uint64_t	ift_no_tx_dma_setup;
332a9643ea8Slogwang 	uint64_t	ift_no_desc_avail;
333a9643ea8Slogwang 	uint64_t	ift_mbuf_defrag_failed;
334a9643ea8Slogwang 	uint64_t	ift_mbuf_defrag;
335a9643ea8Slogwang 	uint64_t	ift_map_failed;
336a9643ea8Slogwang 	uint64_t	ift_txd_encap_efbig;
337a9643ea8Slogwang 	uint64_t	ift_pullups;
338*22ce4affSfengbojiang 	uint64_t	ift_last_timer_tick;
339a9643ea8Slogwang 
340a9643ea8Slogwang 	struct mtx	ift_mtx;
341a9643ea8Slogwang 	struct mtx	ift_db_mtx;
342a9643ea8Slogwang 
343a9643ea8Slogwang 	/* constant values */
344a9643ea8Slogwang 	if_ctx_t	ift_ctx;
345*22ce4affSfengbojiang 	struct ifmp_ring        *ift_br;
346a9643ea8Slogwang 	struct grouptask	ift_task;
347*22ce4affSfengbojiang 	qidx_t		ift_size;
348a9643ea8Slogwang 	uint16_t	ift_id;
349a9643ea8Slogwang 	struct callout	ift_timer;
350*22ce4affSfengbojiang #ifdef DEV_NETMAP
351*22ce4affSfengbojiang 	struct callout	ift_netmap_timer;
352*22ce4affSfengbojiang #endif /* DEV_NETMAP */
353a9643ea8Slogwang 
354*22ce4affSfengbojiang 	if_txsd_vec_t	ift_sds;
355a9643ea8Slogwang 	uint8_t		ift_qstatus;
356a9643ea8Slogwang 	uint8_t		ift_closed;
357*22ce4affSfengbojiang 	uint8_t		ift_update_freq;
358a9643ea8Slogwang 	struct iflib_filter_info ift_filter_info;
359*22ce4affSfengbojiang 	bus_dma_tag_t	ift_buf_tag;
360*22ce4affSfengbojiang 	bus_dma_tag_t	ift_tso_buf_tag;
361a9643ea8Slogwang 	iflib_dma_info_t	ift_ifdi;
362*22ce4affSfengbojiang #define	MTX_NAME_LEN	32
363a9643ea8Slogwang 	char                    ift_mtx_name[MTX_NAME_LEN];
364a9643ea8Slogwang 	bus_dma_segment_t	ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
365*22ce4affSfengbojiang #ifdef IFLIB_DIAGNOSTICS
366*22ce4affSfengbojiang 	uint64_t ift_cpu_exec_count[256];
367*22ce4affSfengbojiang #endif
368a9643ea8Slogwang } __aligned(CACHE_LINE_SIZE);
369a9643ea8Slogwang 
370a9643ea8Slogwang struct iflib_fl {
371*22ce4affSfengbojiang 	qidx_t		ifl_cidx;
372*22ce4affSfengbojiang 	qidx_t		ifl_pidx;
373*22ce4affSfengbojiang 	qidx_t		ifl_credits;
374a9643ea8Slogwang 	uint8_t		ifl_gen;
375*22ce4affSfengbojiang 	uint8_t		ifl_rxd_size;
376a9643ea8Slogwang #if MEMORY_LOGGING
377a9643ea8Slogwang 	uint64_t	ifl_m_enqueued;
378a9643ea8Slogwang 	uint64_t	ifl_m_dequeued;
379a9643ea8Slogwang 	uint64_t	ifl_cl_enqueued;
380a9643ea8Slogwang 	uint64_t	ifl_cl_dequeued;
381a9643ea8Slogwang #endif
382a9643ea8Slogwang 	/* implicit pad */
383*22ce4affSfengbojiang 	bitstr_t 	*ifl_rx_bitmap;
384*22ce4affSfengbojiang 	qidx_t		ifl_fragidx;
385a9643ea8Slogwang 	/* constant */
386*22ce4affSfengbojiang 	qidx_t		ifl_size;
387a9643ea8Slogwang 	uint16_t	ifl_buf_size;
388a9643ea8Slogwang 	uint16_t	ifl_cltype;
389a9643ea8Slogwang 	uma_zone_t	ifl_zone;
390*22ce4affSfengbojiang 	iflib_rxsd_array_t	ifl_sds;
391a9643ea8Slogwang 	iflib_rxq_t	ifl_rxq;
392a9643ea8Slogwang 	uint8_t		ifl_id;
393*22ce4affSfengbojiang 	bus_dma_tag_t	ifl_buf_tag;
394a9643ea8Slogwang 	iflib_dma_info_t	ifl_ifdi;
395a9643ea8Slogwang 	uint64_t	ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
396*22ce4affSfengbojiang 	qidx_t		ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
397a9643ea8Slogwang }  __aligned(CACHE_LINE_SIZE);
398a9643ea8Slogwang 
399*22ce4affSfengbojiang static inline qidx_t
get_inuse(int size,qidx_t cidx,qidx_t pidx,uint8_t gen)400*22ce4affSfengbojiang get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
401a9643ea8Slogwang {
402*22ce4affSfengbojiang 	qidx_t used;
403a9643ea8Slogwang 
404a9643ea8Slogwang 	if (pidx > cidx)
405a9643ea8Slogwang 		used = pidx - cidx;
406a9643ea8Slogwang 	else if (pidx < cidx)
407a9643ea8Slogwang 		used = size - cidx + pidx;
408a9643ea8Slogwang 	else if (gen == 0 && pidx == cidx)
409a9643ea8Slogwang 		used = 0;
410a9643ea8Slogwang 	else if (gen == 1 && pidx == cidx)
411a9643ea8Slogwang 		used = size;
412a9643ea8Slogwang 	else
413a9643ea8Slogwang 		panic("bad state");
414a9643ea8Slogwang 
415a9643ea8Slogwang 	return (used);
416a9643ea8Slogwang }
417a9643ea8Slogwang 
418a9643ea8Slogwang #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
419a9643ea8Slogwang 
420a9643ea8Slogwang #define IDXDIFF(head, tail, wrap) \
421a9643ea8Slogwang 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
422a9643ea8Slogwang 
423a9643ea8Slogwang struct iflib_rxq {
424a9643ea8Slogwang 	if_ctx_t	ifr_ctx;
425a9643ea8Slogwang 	iflib_fl_t	ifr_fl;
426a9643ea8Slogwang 	uint64_t	ifr_rx_irq;
427*22ce4affSfengbojiang 	struct pfil_head	*pfil;
428*22ce4affSfengbojiang 	/*
429*22ce4affSfengbojiang 	 * If there is a separate completion queue (IFLIB_HAS_RXCQ), this is
430*22ce4affSfengbojiang 	 * the completion queue consumer index.  Otherwise it's unused.
431*22ce4affSfengbojiang 	 */
432*22ce4affSfengbojiang 	qidx_t		ifr_cq_cidx;
433a9643ea8Slogwang 	uint16_t	ifr_id;
434a9643ea8Slogwang 	uint8_t		ifr_nfl;
435*22ce4affSfengbojiang 	uint8_t		ifr_ntxqirq;
436*22ce4affSfengbojiang 	uint8_t		ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
437*22ce4affSfengbojiang 	uint8_t		ifr_fl_offset;
438a9643ea8Slogwang 	struct lro_ctrl			ifr_lc;
439a9643ea8Slogwang 	struct grouptask        ifr_task;
440*22ce4affSfengbojiang 	struct callout		ifr_watchdog;
441a9643ea8Slogwang 	struct iflib_filter_info ifr_filter_info;
442a9643ea8Slogwang 	iflib_dma_info_t		ifr_ifdi;
443*22ce4affSfengbojiang 
444a9643ea8Slogwang 	/* dynamically allocate if any drivers need a value substantially larger than this */
445a9643ea8Slogwang 	struct if_rxd_frag	ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
446*22ce4affSfengbojiang #ifdef IFLIB_DIAGNOSTICS
447*22ce4affSfengbojiang 	uint64_t ifr_cpu_exec_count[256];
448*22ce4affSfengbojiang #endif
449a9643ea8Slogwang }  __aligned(CACHE_LINE_SIZE);
450a9643ea8Slogwang 
451*22ce4affSfengbojiang typedef struct if_rxsd {
452*22ce4affSfengbojiang 	caddr_t *ifsd_cl;
453*22ce4affSfengbojiang 	iflib_fl_t ifsd_fl;
454*22ce4affSfengbojiang } *if_rxsd_t;
455*22ce4affSfengbojiang 
456*22ce4affSfengbojiang /* multiple of word size */
457*22ce4affSfengbojiang #ifdef __LP64__
458*22ce4affSfengbojiang #define PKT_INFO_SIZE	6
459*22ce4affSfengbojiang #define RXD_INFO_SIZE	5
460*22ce4affSfengbojiang #define PKT_TYPE uint64_t
461*22ce4affSfengbojiang #else
462*22ce4affSfengbojiang #define PKT_INFO_SIZE	11
463*22ce4affSfengbojiang #define RXD_INFO_SIZE	8
464*22ce4affSfengbojiang #define PKT_TYPE uint32_t
465*22ce4affSfengbojiang #endif
466*22ce4affSfengbojiang #define PKT_LOOP_BOUND  ((PKT_INFO_SIZE/3)*3)
467*22ce4affSfengbojiang #define RXD_LOOP_BOUND  ((RXD_INFO_SIZE/4)*4)
468*22ce4affSfengbojiang 
469*22ce4affSfengbojiang typedef struct if_pkt_info_pad {
470*22ce4affSfengbojiang 	PKT_TYPE pkt_val[PKT_INFO_SIZE];
471*22ce4affSfengbojiang } *if_pkt_info_pad_t;
472*22ce4affSfengbojiang typedef struct if_rxd_info_pad {
473*22ce4affSfengbojiang 	PKT_TYPE rxd_val[RXD_INFO_SIZE];
474*22ce4affSfengbojiang } *if_rxd_info_pad_t;
475*22ce4affSfengbojiang 
476*22ce4affSfengbojiang CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info));
477*22ce4affSfengbojiang CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info));
478*22ce4affSfengbojiang 
479*22ce4affSfengbojiang static inline void
pkt_info_zero(if_pkt_info_t pi)480*22ce4affSfengbojiang pkt_info_zero(if_pkt_info_t pi)
481*22ce4affSfengbojiang {
482*22ce4affSfengbojiang 	if_pkt_info_pad_t pi_pad;
483*22ce4affSfengbojiang 
484*22ce4affSfengbojiang 	pi_pad = (if_pkt_info_pad_t)pi;
485*22ce4affSfengbojiang 	pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0;
486*22ce4affSfengbojiang 	pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0;
487*22ce4affSfengbojiang #ifndef __LP64__
488*22ce4affSfengbojiang 	pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0;
489*22ce4affSfengbojiang 	pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0;
490*22ce4affSfengbojiang #endif
491*22ce4affSfengbojiang }
492*22ce4affSfengbojiang 
493*22ce4affSfengbojiang static device_method_t iflib_pseudo_methods[] = {
494*22ce4affSfengbojiang 	DEVMETHOD(device_attach, noop_attach),
495*22ce4affSfengbojiang 	DEVMETHOD(device_detach, iflib_pseudo_detach),
496*22ce4affSfengbojiang 	DEVMETHOD_END
497*22ce4affSfengbojiang };
498*22ce4affSfengbojiang 
499*22ce4affSfengbojiang driver_t iflib_pseudodriver = {
500*22ce4affSfengbojiang 	"iflib_pseudo", iflib_pseudo_methods, sizeof(struct iflib_ctx),
501*22ce4affSfengbojiang };
502*22ce4affSfengbojiang 
503*22ce4affSfengbojiang static inline void
rxd_info_zero(if_rxd_info_t ri)504*22ce4affSfengbojiang rxd_info_zero(if_rxd_info_t ri)
505*22ce4affSfengbojiang {
506*22ce4affSfengbojiang 	if_rxd_info_pad_t ri_pad;
507*22ce4affSfengbojiang 	int i;
508*22ce4affSfengbojiang 
509*22ce4affSfengbojiang 	ri_pad = (if_rxd_info_pad_t)ri;
510*22ce4affSfengbojiang 	for (i = 0; i < RXD_LOOP_BOUND; i += 4) {
511*22ce4affSfengbojiang 		ri_pad->rxd_val[i] = 0;
512*22ce4affSfengbojiang 		ri_pad->rxd_val[i+1] = 0;
513*22ce4affSfengbojiang 		ri_pad->rxd_val[i+2] = 0;
514*22ce4affSfengbojiang 		ri_pad->rxd_val[i+3] = 0;
515*22ce4affSfengbojiang 	}
516*22ce4affSfengbojiang #ifdef __LP64__
517*22ce4affSfengbojiang 	ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0;
518*22ce4affSfengbojiang #endif
519*22ce4affSfengbojiang }
520*22ce4affSfengbojiang 
521a9643ea8Slogwang /*
522a9643ea8Slogwang  * Only allow a single packet to take up most 1/nth of the tx ring
523a9643ea8Slogwang  */
524a9643ea8Slogwang #define MAX_SINGLE_PACKET_FRACTION 12
525a9643ea8Slogwang #define IF_BAD_DMA (bus_addr_t)-1
526a9643ea8Slogwang 
527a9643ea8Slogwang #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
528a9643ea8Slogwang 
529*22ce4affSfengbojiang #define CTX_LOCK_INIT(_sc)  sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock")
530*22ce4affSfengbojiang #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx)
531*22ce4affSfengbojiang #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx)
532*22ce4affSfengbojiang #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx)
533a9643ea8Slogwang 
534*22ce4affSfengbojiang #define STATE_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF)
535*22ce4affSfengbojiang #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx)
536*22ce4affSfengbojiang #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx)
537*22ce4affSfengbojiang #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx)
538a9643ea8Slogwang 
539a9643ea8Slogwang #define CALLOUT_LOCK(txq)	mtx_lock(&txq->ift_mtx)
540a9643ea8Slogwang #define CALLOUT_UNLOCK(txq) 	mtx_unlock(&txq->ift_mtx)
541a9643ea8Slogwang 
542*22ce4affSfengbojiang void
iflib_set_detach(if_ctx_t ctx)543*22ce4affSfengbojiang iflib_set_detach(if_ctx_t ctx)
544*22ce4affSfengbojiang {
545*22ce4affSfengbojiang 	STATE_LOCK(ctx);
546*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_IN_DETACH;
547*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
548*22ce4affSfengbojiang }
549a9643ea8Slogwang 
550a9643ea8Slogwang /* Our boot-time initialization hook */
551a9643ea8Slogwang static int	iflib_module_event_handler(module_t, int, void *);
552a9643ea8Slogwang 
553a9643ea8Slogwang static moduledata_t iflib_moduledata = {
554a9643ea8Slogwang 	"iflib",
555a9643ea8Slogwang 	iflib_module_event_handler,
556a9643ea8Slogwang 	NULL
557a9643ea8Slogwang };
558a9643ea8Slogwang 
559a9643ea8Slogwang DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
560a9643ea8Slogwang MODULE_VERSION(iflib, 1);
561a9643ea8Slogwang 
562a9643ea8Slogwang MODULE_DEPEND(iflib, pci, 1, 1, 1);
563a9643ea8Slogwang MODULE_DEPEND(iflib, ether, 1, 1, 1);
564a9643ea8Slogwang 
565a9643ea8Slogwang TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
566a9643ea8Slogwang TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
567a9643ea8Slogwang 
568a9643ea8Slogwang #ifndef IFLIB_DEBUG_COUNTERS
569a9643ea8Slogwang #ifdef INVARIANTS
570a9643ea8Slogwang #define IFLIB_DEBUG_COUNTERS 1
571a9643ea8Slogwang #else
572a9643ea8Slogwang #define IFLIB_DEBUG_COUNTERS 0
573a9643ea8Slogwang #endif /* !INVARIANTS */
574a9643ea8Slogwang #endif
575a9643ea8Slogwang 
576*22ce4affSfengbojiang static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
577a9643ea8Slogwang     "iflib driver parameters");
578a9643ea8Slogwang 
579a9643ea8Slogwang /*
580a9643ea8Slogwang  * XXX need to ensure that this can't accidentally cause the head to be moved backwards
581a9643ea8Slogwang  */
582a9643ea8Slogwang static int iflib_min_tx_latency = 0;
583a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
584*22ce4affSfengbojiang 		   &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
585*22ce4affSfengbojiang static int iflib_no_tx_batch = 0;
586*22ce4affSfengbojiang SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
587*22ce4affSfengbojiang 		   &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput");
588*22ce4affSfengbojiang static int iflib_timer_default = 1000;
589*22ce4affSfengbojiang SYSCTL_INT(_net_iflib, OID_AUTO, timer_default, CTLFLAG_RW,
590*22ce4affSfengbojiang 		   &iflib_timer_default, 0, "number of ticks between iflib_timer calls");
591a9643ea8Slogwang 
592a9643ea8Slogwang 
593a9643ea8Slogwang #if IFLIB_DEBUG_COUNTERS
594a9643ea8Slogwang 
595a9643ea8Slogwang static int iflib_tx_seen;
596a9643ea8Slogwang static int iflib_tx_sent;
597a9643ea8Slogwang static int iflib_tx_encap;
598a9643ea8Slogwang static int iflib_rx_allocs;
599a9643ea8Slogwang static int iflib_fl_refills;
600a9643ea8Slogwang static int iflib_fl_refills_large;
601a9643ea8Slogwang static int iflib_tx_frees;
602a9643ea8Slogwang 
603a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
604*22ce4affSfengbojiang 		   &iflib_tx_seen, 0, "# TX mbufs seen");
605a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
606*22ce4affSfengbojiang 		   &iflib_tx_sent, 0, "# TX mbufs sent");
607a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
608*22ce4affSfengbojiang 		   &iflib_tx_encap, 0, "# TX mbufs encapped");
609a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
610*22ce4affSfengbojiang 		   &iflib_tx_frees, 0, "# TX frees");
611a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
612*22ce4affSfengbojiang 		   &iflib_rx_allocs, 0, "# RX allocations");
613a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
614a9643ea8Slogwang 		   &iflib_fl_refills, 0, "# refills");
615a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
616a9643ea8Slogwang 		   &iflib_fl_refills_large, 0, "# large refills");
617a9643ea8Slogwang 
618a9643ea8Slogwang static int iflib_txq_drain_flushing;
619a9643ea8Slogwang static int iflib_txq_drain_oactive;
620a9643ea8Slogwang static int iflib_txq_drain_notready;
621a9643ea8Slogwang 
622a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
623a9643ea8Slogwang 		   &iflib_txq_drain_flushing, 0, "# drain flushes");
624a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
625a9643ea8Slogwang 		   &iflib_txq_drain_oactive, 0, "# drain oactives");
626a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
627a9643ea8Slogwang 		   &iflib_txq_drain_notready, 0, "# drain notready");
628a9643ea8Slogwang 
629a9643ea8Slogwang static int iflib_encap_load_mbuf_fail;
630*22ce4affSfengbojiang static int iflib_encap_pad_mbuf_fail;
631a9643ea8Slogwang static int iflib_encap_txq_avail_fail;
632a9643ea8Slogwang static int iflib_encap_txd_encap_fail;
633a9643ea8Slogwang 
634a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
635a9643ea8Slogwang 		   &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
636*22ce4affSfengbojiang SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD,
637*22ce4affSfengbojiang 		   &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures");
638a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
639a9643ea8Slogwang 		   &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
640a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
641a9643ea8Slogwang 		   &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
642a9643ea8Slogwang 
643a9643ea8Slogwang static int iflib_task_fn_rxs;
644a9643ea8Slogwang static int iflib_rx_intr_enables;
645a9643ea8Slogwang static int iflib_fast_intrs;
646a9643ea8Slogwang static int iflib_rx_unavail;
647a9643ea8Slogwang static int iflib_rx_ctx_inactive;
648a9643ea8Slogwang static int iflib_rx_if_input;
649a9643ea8Slogwang static int iflib_rxd_flush;
650a9643ea8Slogwang 
651a9643ea8Slogwang static int iflib_verbose_debug;
652a9643ea8Slogwang 
653a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
654a9643ea8Slogwang 		   &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
655a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
656*22ce4affSfengbojiang 		   &iflib_rx_intr_enables, 0, "# RX intr enables");
657a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
658a9643ea8Slogwang 		   &iflib_fast_intrs, 0, "# fast_intr calls");
659a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
660a9643ea8Slogwang 		   &iflib_rx_unavail, 0, "# times rxeof called with no available data");
661a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
662a9643ea8Slogwang 		   &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
663a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
664a9643ea8Slogwang 		   &iflib_rx_if_input, 0, "# times rxeof called if_input");
665a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
666a9643ea8Slogwang 	         &iflib_rxd_flush, 0, "# times rxd_flush called");
667a9643ea8Slogwang SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
668a9643ea8Slogwang 		   &iflib_verbose_debug, 0, "enable verbose debugging");
669a9643ea8Slogwang 
670a9643ea8Slogwang #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
671*22ce4affSfengbojiang static void
iflib_debug_reset(void)672*22ce4affSfengbojiang iflib_debug_reset(void)
673*22ce4affSfengbojiang {
674*22ce4affSfengbojiang 	iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
675*22ce4affSfengbojiang 		iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
676*22ce4affSfengbojiang 		iflib_txq_drain_flushing = iflib_txq_drain_oactive =
677*22ce4affSfengbojiang 		iflib_txq_drain_notready =
678*22ce4affSfengbojiang 		iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail =
679*22ce4affSfengbojiang 		iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail =
680*22ce4affSfengbojiang 		iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs =
681*22ce4affSfengbojiang 		iflib_rx_unavail =
682*22ce4affSfengbojiang 		iflib_rx_ctx_inactive = iflib_rx_if_input =
683*22ce4affSfengbojiang 		iflib_rxd_flush = 0;
684*22ce4affSfengbojiang }
685a9643ea8Slogwang 
686a9643ea8Slogwang #else
687a9643ea8Slogwang #define DBG_COUNTER_INC(name)
iflib_debug_reset(void)688*22ce4affSfengbojiang static void iflib_debug_reset(void) {}
689a9643ea8Slogwang #endif
690a9643ea8Slogwang 
691a9643ea8Slogwang #define IFLIB_DEBUG 0
692a9643ea8Slogwang 
693a9643ea8Slogwang static void iflib_tx_structures_free(if_ctx_t ctx);
694a9643ea8Slogwang static void iflib_rx_structures_free(if_ctx_t ctx);
695a9643ea8Slogwang static int iflib_queues_alloc(if_ctx_t ctx);
696a9643ea8Slogwang static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
697*22ce4affSfengbojiang static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
698a9643ea8Slogwang static int iflib_qset_structures_setup(if_ctx_t ctx);
699a9643ea8Slogwang static int iflib_msix_init(if_ctx_t ctx);
700*22ce4affSfengbojiang static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str);
701a9643ea8Slogwang static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
702a9643ea8Slogwang static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
703*22ce4affSfengbojiang #ifdef ALTQ
704*22ce4affSfengbojiang static void iflib_altq_if_start(if_t ifp);
705*22ce4affSfengbojiang static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m);
706*22ce4affSfengbojiang #endif
707a9643ea8Slogwang static int iflib_register(if_ctx_t);
708*22ce4affSfengbojiang static void iflib_deregister(if_ctx_t);
709*22ce4affSfengbojiang static void iflib_unregister_vlan_handlers(if_ctx_t ctx);
710*22ce4affSfengbojiang static uint16_t iflib_get_mbuf_size_for(unsigned int size);
711a9643ea8Slogwang static void iflib_init_locked(if_ctx_t ctx);
712a9643ea8Slogwang static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
713a9643ea8Slogwang static void iflib_add_device_sysctl_post(if_ctx_t ctx);
714*22ce4affSfengbojiang static void iflib_ifmp_purge(iflib_txq_t txq);
715*22ce4affSfengbojiang static void _iflib_pre_assert(if_softc_ctx_t scctx);
716*22ce4affSfengbojiang static void iflib_if_init_locked(if_ctx_t ctx);
717*22ce4affSfengbojiang static void iflib_free_intr_mem(if_ctx_t ctx);
718*22ce4affSfengbojiang #ifndef __NO_STRICT_ALIGNMENT
719*22ce4affSfengbojiang static struct mbuf * iflib_fixup_rx(struct mbuf *m);
720*22ce4affSfengbojiang #endif
721a9643ea8Slogwang 
722*22ce4affSfengbojiang static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets =
723*22ce4affSfengbojiang     SLIST_HEAD_INITIALIZER(cpu_offsets);
724*22ce4affSfengbojiang struct cpu_offset {
725*22ce4affSfengbojiang 	SLIST_ENTRY(cpu_offset) entries;
726*22ce4affSfengbojiang 	cpuset_t	set;
727*22ce4affSfengbojiang 	unsigned int	refcount;
728*22ce4affSfengbojiang 	uint16_t	offset;
729*22ce4affSfengbojiang };
730*22ce4affSfengbojiang static struct mtx cpu_offset_mtx;
731*22ce4affSfengbojiang MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock",
732*22ce4affSfengbojiang     MTX_DEF);
733*22ce4affSfengbojiang 
734*22ce4affSfengbojiang DEBUGNET_DEFINE(iflib);
735*22ce4affSfengbojiang 
736*22ce4affSfengbojiang static int
iflib_num_rx_descs(if_ctx_t ctx)737*22ce4affSfengbojiang iflib_num_rx_descs(if_ctx_t ctx)
738*22ce4affSfengbojiang {
739*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
740*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
741*22ce4affSfengbojiang 	uint16_t first_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
742*22ce4affSfengbojiang 
743*22ce4affSfengbojiang 	return scctx->isc_nrxd[first_rxq];
744*22ce4affSfengbojiang }
745*22ce4affSfengbojiang 
746*22ce4affSfengbojiang static int
iflib_num_tx_descs(if_ctx_t ctx)747*22ce4affSfengbojiang iflib_num_tx_descs(if_ctx_t ctx)
748*22ce4affSfengbojiang {
749*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
750*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
751*22ce4affSfengbojiang 	uint16_t first_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
752*22ce4affSfengbojiang 
753*22ce4affSfengbojiang 	return scctx->isc_ntxd[first_txq];
754*22ce4affSfengbojiang }
755a9643ea8Slogwang 
756a9643ea8Slogwang #ifdef DEV_NETMAP
757a9643ea8Slogwang #include <sys/selinfo.h>
758a9643ea8Slogwang #include <net/netmap.h>
759a9643ea8Slogwang #include <dev/netmap/netmap_kern.h>
760a9643ea8Slogwang 
761a9643ea8Slogwang MODULE_DEPEND(iflib, netmap, 1, 1, 1);
762a9643ea8Slogwang 
763*22ce4affSfengbojiang static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init);
764*22ce4affSfengbojiang static void iflib_netmap_timer(void *arg);
765*22ce4affSfengbojiang 
766a9643ea8Slogwang /*
767a9643ea8Slogwang  * device-specific sysctl variables:
768a9643ea8Slogwang  *
769a9643ea8Slogwang  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
770a9643ea8Slogwang  *	During regular operations the CRC is stripped, but on some
771a9643ea8Slogwang  *	hardware reception of frames not multiple of 64 is slower,
772a9643ea8Slogwang  *	so using crcstrip=0 helps in benchmarks.
773a9643ea8Slogwang  *
774a9643ea8Slogwang  * iflib_rx_miss, iflib_rx_miss_bufs:
775a9643ea8Slogwang  *	count packets that might be missed due to lost interrupts.
776a9643ea8Slogwang  */
777a9643ea8Slogwang SYSCTL_DECL(_dev_netmap);
778a9643ea8Slogwang /*
779a9643ea8Slogwang  * The xl driver by default strips CRCs and we do not override it.
780a9643ea8Slogwang  */
781a9643ea8Slogwang 
782a9643ea8Slogwang int iflib_crcstrip = 1;
783a9643ea8Slogwang SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
784*22ce4affSfengbojiang     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames");
785a9643ea8Slogwang 
786a9643ea8Slogwang int iflib_rx_miss, iflib_rx_miss_bufs;
787a9643ea8Slogwang SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
788*22ce4affSfengbojiang     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr");
789a9643ea8Slogwang SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
790*22ce4affSfengbojiang     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs");
791a9643ea8Slogwang 
792a9643ea8Slogwang /*
793a9643ea8Slogwang  * Register/unregister. We are already under netmap lock.
794a9643ea8Slogwang  * Only called on the first register or the last unregister.
795a9643ea8Slogwang  */
796a9643ea8Slogwang static int
iflib_netmap_register(struct netmap_adapter * na,int onoff)797a9643ea8Slogwang iflib_netmap_register(struct netmap_adapter *na, int onoff)
798a9643ea8Slogwang {
799*22ce4affSfengbojiang 	if_t ifp = na->ifp;
800a9643ea8Slogwang 	if_ctx_t ctx = ifp->if_softc;
801*22ce4affSfengbojiang 	int status;
802a9643ea8Slogwang 
803a9643ea8Slogwang 	CTX_LOCK(ctx);
804a9643ea8Slogwang 	if (!CTX_IS_VF(ctx))
805*22ce4affSfengbojiang 		IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
806a9643ea8Slogwang 
807*22ce4affSfengbojiang 	iflib_stop(ctx);
808*22ce4affSfengbojiang 
809*22ce4affSfengbojiang 	/*
810*22ce4affSfengbojiang 	 * Enable (or disable) netmap flags, and intercept (or restore)
811*22ce4affSfengbojiang 	 * ifp->if_transmit. This is done once the device has been stopped
812*22ce4affSfengbojiang 	 * to prevent race conditions. Also, this must be done after
813*22ce4affSfengbojiang 	 * calling netmap_disable_all_rings() and before calling
814*22ce4affSfengbojiang 	 * netmap_enable_all_rings(), so that these two functions see the
815*22ce4affSfengbojiang 	 * updated state of the NAF_NETMAP_ON bit.
816*22ce4affSfengbojiang 	 */
817a9643ea8Slogwang 	if (onoff) {
818a9643ea8Slogwang 		nm_set_native_flags(na);
819a9643ea8Slogwang 	} else {
820a9643ea8Slogwang 		nm_clear_native_flags(na);
821a9643ea8Slogwang 	}
822*22ce4affSfengbojiang 
823*22ce4affSfengbojiang 	iflib_init_locked(ctx);
824*22ce4affSfengbojiang 	IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
825*22ce4affSfengbojiang 	status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1;
826*22ce4affSfengbojiang 	if (status)
827*22ce4affSfengbojiang 		nm_clear_native_flags(na);
828a9643ea8Slogwang 	CTX_UNLOCK(ctx);
829*22ce4affSfengbojiang 	return (status);
830a9643ea8Slogwang }
831a9643ea8Slogwang 
832*22ce4affSfengbojiang static int
netmap_fl_refill(iflib_rxq_t rxq,struct netmap_kring * kring,bool init)833*22ce4affSfengbojiang netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init)
834*22ce4affSfengbojiang {
835*22ce4affSfengbojiang 	struct netmap_adapter *na = kring->na;
836*22ce4affSfengbojiang 	u_int const lim = kring->nkr_num_slots - 1;
837*22ce4affSfengbojiang 	struct netmap_ring *ring = kring->ring;
838*22ce4affSfengbojiang 	bus_dmamap_t *map;
839*22ce4affSfengbojiang 	struct if_rxd_update iru;
840*22ce4affSfengbojiang 	if_ctx_t ctx = rxq->ifr_ctx;
841*22ce4affSfengbojiang 	iflib_fl_t fl = &rxq->ifr_fl[0];
842*22ce4affSfengbojiang 	u_int nic_i_first, nic_i;
843*22ce4affSfengbojiang 	u_int nm_i;
844*22ce4affSfengbojiang 	int i, n;
845*22ce4affSfengbojiang #if IFLIB_DEBUG_COUNTERS
846*22ce4affSfengbojiang 	int rf_count = 0;
847*22ce4affSfengbojiang #endif
848*22ce4affSfengbojiang 
849*22ce4affSfengbojiang 	/*
850*22ce4affSfengbojiang 	 * This function is used both at initialization and in rxsync.
851*22ce4affSfengbojiang 	 * At initialization we need to prepare (with isc_rxd_refill())
852*22ce4affSfengbojiang 	 * all the netmap buffers currently owned by the kernel, in
853*22ce4affSfengbojiang 	 * such a way to keep fl->ifl_pidx and kring->nr_hwcur in sync
854*22ce4affSfengbojiang 	 * (except for kring->nkr_hwofs). These may be less than
855*22ce4affSfengbojiang 	 * kring->nkr_num_slots if netmap_reset() was called while
856*22ce4affSfengbojiang 	 * an application using the kring that still owned some
857*22ce4affSfengbojiang 	 * buffers.
858*22ce4affSfengbojiang 	 * At rxsync time, both indexes point to the next buffer to be
859*22ce4affSfengbojiang 	 * refilled.
860*22ce4affSfengbojiang 	 * In any case we publish (with isc_rxd_flush()) up to
861*22ce4affSfengbojiang 	 * (fl->ifl_pidx - 1) % N (included), to avoid the NIC tail/prod
862*22ce4affSfengbojiang 	 * pointer to overrun the head/cons pointer, although this is
863*22ce4affSfengbojiang 	 * not necessary for some NICs (e.g. vmx).
864*22ce4affSfengbojiang 	 */
865*22ce4affSfengbojiang 	if (__predict_false(init)) {
866*22ce4affSfengbojiang 		n = kring->nkr_num_slots - nm_kr_rxspace(kring);
867*22ce4affSfengbojiang 	} else {
868*22ce4affSfengbojiang 		n = kring->rhead - kring->nr_hwcur;
869*22ce4affSfengbojiang 		if (n == 0)
870*22ce4affSfengbojiang 			return (0); /* Nothing to do. */
871*22ce4affSfengbojiang 		if (n < 0)
872*22ce4affSfengbojiang 			n += kring->nkr_num_slots;
873*22ce4affSfengbojiang 	}
874*22ce4affSfengbojiang 
875*22ce4affSfengbojiang 	iru_init(&iru, rxq, 0 /* flid */);
876*22ce4affSfengbojiang 	map = fl->ifl_sds.ifsd_map;
877*22ce4affSfengbojiang 	nic_i = fl->ifl_pidx;
878*22ce4affSfengbojiang 	nm_i = netmap_idx_n2k(kring, nic_i);
879*22ce4affSfengbojiang 	if (__predict_false(init)) {
880*22ce4affSfengbojiang 		/*
881*22ce4affSfengbojiang 		 * On init/reset, nic_i must be 0, and we must
882*22ce4affSfengbojiang 		 * start to refill from hwtail (see netmap_reset()).
883*22ce4affSfengbojiang 		 */
884*22ce4affSfengbojiang 		MPASS(nic_i == 0);
885*22ce4affSfengbojiang 		MPASS(nm_i == kring->nr_hwtail);
886*22ce4affSfengbojiang 	} else
887*22ce4affSfengbojiang 		MPASS(nm_i == kring->nr_hwcur);
888*22ce4affSfengbojiang 	DBG_COUNTER_INC(fl_refills);
889*22ce4affSfengbojiang 	while (n > 0) {
890*22ce4affSfengbojiang #if IFLIB_DEBUG_COUNTERS
891*22ce4affSfengbojiang 		if (++rf_count == 9)
892*22ce4affSfengbojiang 			DBG_COUNTER_INC(fl_refills_large);
893*22ce4affSfengbojiang #endif
894*22ce4affSfengbojiang 		nic_i_first = nic_i;
895*22ce4affSfengbojiang 		for (i = 0; n > 0 && i < IFLIB_MAX_RX_REFRESH; n--, i++) {
896*22ce4affSfengbojiang 			struct netmap_slot *slot = &ring->slot[nm_i];
897*22ce4affSfengbojiang 			void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[i]);
898*22ce4affSfengbojiang 
899*22ce4affSfengbojiang 			MPASS(i < IFLIB_MAX_RX_REFRESH);
900*22ce4affSfengbojiang 
901*22ce4affSfengbojiang 			if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
902*22ce4affSfengbojiang 			        return netmap_ring_reinit(kring);
903*22ce4affSfengbojiang 
904*22ce4affSfengbojiang 			fl->ifl_rxd_idxs[i] = nic_i;
905*22ce4affSfengbojiang 
906*22ce4affSfengbojiang 			if (__predict_false(init)) {
907*22ce4affSfengbojiang 				netmap_load_map(na, fl->ifl_buf_tag,
908*22ce4affSfengbojiang 				    map[nic_i], addr);
909*22ce4affSfengbojiang 			} else if (slot->flags & NS_BUF_CHANGED) {
910*22ce4affSfengbojiang 				/* buffer has changed, reload map */
911*22ce4affSfengbojiang 				netmap_reload_map(na, fl->ifl_buf_tag,
912*22ce4affSfengbojiang 				    map[nic_i], addr);
913*22ce4affSfengbojiang 			}
914*22ce4affSfengbojiang 			bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i],
915*22ce4affSfengbojiang 			    BUS_DMASYNC_PREREAD);
916*22ce4affSfengbojiang 			slot->flags &= ~NS_BUF_CHANGED;
917*22ce4affSfengbojiang 
918*22ce4affSfengbojiang 			nm_i = nm_next(nm_i, lim);
919*22ce4affSfengbojiang 			nic_i = nm_next(nic_i, lim);
920*22ce4affSfengbojiang 		}
921*22ce4affSfengbojiang 
922*22ce4affSfengbojiang 		iru.iru_pidx = nic_i_first;
923*22ce4affSfengbojiang 		iru.iru_count = i;
924*22ce4affSfengbojiang 		ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
925*22ce4affSfengbojiang 	}
926*22ce4affSfengbojiang 	fl->ifl_pidx = nic_i;
927*22ce4affSfengbojiang 	/*
928*22ce4affSfengbojiang 	 * At the end of the loop we must have refilled everything
929*22ce4affSfengbojiang 	 * we could possibly refill.
930*22ce4affSfengbojiang 	 */
931*22ce4affSfengbojiang 	MPASS(nm_i == kring->rhead);
932*22ce4affSfengbojiang 	kring->nr_hwcur = nm_i;
933*22ce4affSfengbojiang 
934*22ce4affSfengbojiang 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
935*22ce4affSfengbojiang 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
936*22ce4affSfengbojiang 	ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id,
937*22ce4affSfengbojiang 	    nm_prev(nic_i, lim));
938*22ce4affSfengbojiang 	DBG_COUNTER_INC(rxd_flush);
939*22ce4affSfengbojiang 
940*22ce4affSfengbojiang 	return (0);
941*22ce4affSfengbojiang }
942*22ce4affSfengbojiang 
943*22ce4affSfengbojiang #define NETMAP_TX_TIMER_US	90
944*22ce4affSfengbojiang 
945a9643ea8Slogwang /*
946a9643ea8Slogwang  * Reconcile kernel and user view of the transmit ring.
947a9643ea8Slogwang  *
948a9643ea8Slogwang  * All information is in the kring.
949a9643ea8Slogwang  * Userspace wants to send packets up to the one before kring->rhead,
950a9643ea8Slogwang  * kernel knows kring->nr_hwcur is the first unsent packet.
951a9643ea8Slogwang  *
952a9643ea8Slogwang  * Here we push packets out (as many as possible), and possibly
953a9643ea8Slogwang  * reclaim buffers from previously completed transmission.
954a9643ea8Slogwang  *
955a9643ea8Slogwang  * The caller (netmap) guarantees that there is only one instance
956a9643ea8Slogwang  * running at any time. Any interference with other driver
957a9643ea8Slogwang  * methods should be handled by the individual drivers.
958a9643ea8Slogwang  */
959a9643ea8Slogwang static int
iflib_netmap_txsync(struct netmap_kring * kring,int flags)960a9643ea8Slogwang iflib_netmap_txsync(struct netmap_kring *kring, int flags)
961a9643ea8Slogwang {
962a9643ea8Slogwang 	struct netmap_adapter *na = kring->na;
963*22ce4affSfengbojiang 	if_t ifp = na->ifp;
964a9643ea8Slogwang 	struct netmap_ring *ring = kring->ring;
965*22ce4affSfengbojiang 	u_int nm_i;	/* index into the netmap kring */
966a9643ea8Slogwang 	u_int nic_i;	/* index into the NIC ring */
967a9643ea8Slogwang 	u_int n;
968a9643ea8Slogwang 	u_int const lim = kring->nkr_num_slots - 1;
969a9643ea8Slogwang 	u_int const head = kring->rhead;
970a9643ea8Slogwang 	struct if_pkt_info pi;
971a9643ea8Slogwang 
972a9643ea8Slogwang 	/*
973a9643ea8Slogwang 	 * interrupts on every tx packet are expensive so request
974a9643ea8Slogwang 	 * them every half ring, or where NS_REPORT is set
975a9643ea8Slogwang 	 */
976a9643ea8Slogwang 	u_int report_frequency = kring->nkr_num_slots >> 1;
977a9643ea8Slogwang 	/* device-specific */
978a9643ea8Slogwang 	if_ctx_t ctx = ifp->if_softc;
979a9643ea8Slogwang 	iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
980a9643ea8Slogwang 
981*22ce4affSfengbojiang 	bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
982a9643ea8Slogwang 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
983a9643ea8Slogwang 
984a9643ea8Slogwang 	/*
985a9643ea8Slogwang 	 * First part: process new packets to send.
986*22ce4affSfengbojiang 	 * nm_i is the current index in the netmap kring,
987a9643ea8Slogwang 	 * nic_i is the corresponding index in the NIC ring.
988a9643ea8Slogwang 	 *
989a9643ea8Slogwang 	 * If we have packets to send (nm_i != head)
990a9643ea8Slogwang 	 * iterate over the netmap ring, fetch length and update
991a9643ea8Slogwang 	 * the corresponding slot in the NIC ring. Some drivers also
992a9643ea8Slogwang 	 * need to update the buffer's physical address in the NIC slot
993a9643ea8Slogwang 	 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
994a9643ea8Slogwang 	 *
995a9643ea8Slogwang 	 * The netmap_reload_map() calls is especially expensive,
996a9643ea8Slogwang 	 * even when (as in this case) the tag is 0, so do only
997a9643ea8Slogwang 	 * when the buffer has actually changed.
998a9643ea8Slogwang 	 *
999a9643ea8Slogwang 	 * If possible do not set the report/intr bit on all slots,
1000a9643ea8Slogwang 	 * but only a few times per ring or when NS_REPORT is set.
1001a9643ea8Slogwang 	 *
1002a9643ea8Slogwang 	 * Finally, on 10G and faster drivers, it might be useful
1003a9643ea8Slogwang 	 * to prefetch the next slot and txr entry.
1004a9643ea8Slogwang 	 */
1005a9643ea8Slogwang 
1006a9643ea8Slogwang 	nm_i = kring->nr_hwcur;
1007a9643ea8Slogwang 	if (nm_i != head) {	/* we have new packets to send */
1008*22ce4affSfengbojiang 		uint32_t pkt_len = 0, seg_idx = 0;
1009*22ce4affSfengbojiang 		int nic_i_start = -1, flags = 0;
1010*22ce4affSfengbojiang 		pkt_info_zero(&pi);
1011*22ce4affSfengbojiang 		pi.ipi_segs = txq->ift_segs;
1012*22ce4affSfengbojiang 		pi.ipi_qsidx = kring->ring_id;
1013a9643ea8Slogwang 		nic_i = netmap_idx_k2n(kring, nm_i);
1014a9643ea8Slogwang 
1015a9643ea8Slogwang 		__builtin_prefetch(&ring->slot[nm_i]);
1016a9643ea8Slogwang 		__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
1017a9643ea8Slogwang 		__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
1018a9643ea8Slogwang 
1019a9643ea8Slogwang 		for (n = 0; nm_i != head; n++) {
1020a9643ea8Slogwang 			struct netmap_slot *slot = &ring->slot[nm_i];
1021a9643ea8Slogwang 			u_int len = slot->len;
1022a9643ea8Slogwang 			uint64_t paddr;
1023a9643ea8Slogwang 			void *addr = PNMB(na, slot, &paddr);
1024*22ce4affSfengbojiang 
1025*22ce4affSfengbojiang 			flags |= (slot->flags & NS_REPORT ||
1026a9643ea8Slogwang 				nic_i == 0 || nic_i == report_frequency) ?
1027a9643ea8Slogwang 				IPI_TX_INTR : 0;
1028a9643ea8Slogwang 
1029*22ce4affSfengbojiang 			/*
1030*22ce4affSfengbojiang 			 * If this is the first packet fragment, save the
1031*22ce4affSfengbojiang 			 * index of the first NIC slot for later.
1032*22ce4affSfengbojiang 			 */
1033*22ce4affSfengbojiang 			if (nic_i_start < 0)
1034*22ce4affSfengbojiang 				nic_i_start = nic_i;
1035*22ce4affSfengbojiang 
1036*22ce4affSfengbojiang 			pi.ipi_segs[seg_idx].ds_addr = paddr;
1037*22ce4affSfengbojiang 			pi.ipi_segs[seg_idx].ds_len = len;
1038*22ce4affSfengbojiang 			if (len) {
1039*22ce4affSfengbojiang 				pkt_len += len;
1040*22ce4affSfengbojiang 				seg_idx++;
1041*22ce4affSfengbojiang 			}
1042*22ce4affSfengbojiang 
1043*22ce4affSfengbojiang 			if (!(slot->flags & NS_MOREFRAG)) {
1044*22ce4affSfengbojiang 				pi.ipi_len = pkt_len;
1045*22ce4affSfengbojiang 				pi.ipi_nsegs = seg_idx;
1046*22ce4affSfengbojiang 				pi.ipi_pidx = nic_i_start;
1047*22ce4affSfengbojiang 				pi.ipi_ndescs = 0;
1048a9643ea8Slogwang 				pi.ipi_flags = flags;
1049a9643ea8Slogwang 
1050*22ce4affSfengbojiang 				/* Prepare the NIC TX ring. */
1051a9643ea8Slogwang 				ctx->isc_txd_encap(ctx->ifc_softc, &pi);
1052*22ce4affSfengbojiang 				DBG_COUNTER_INC(tx_encap);
1053*22ce4affSfengbojiang 
1054*22ce4affSfengbojiang 				/* Reinit per-packet info for the next one. */
1055*22ce4affSfengbojiang 				flags = seg_idx = pkt_len = 0;
1056*22ce4affSfengbojiang 				nic_i_start = -1;
1057*22ce4affSfengbojiang 			}
1058a9643ea8Slogwang 
1059a9643ea8Slogwang 			/* prefetch for next round */
1060a9643ea8Slogwang 			__builtin_prefetch(&ring->slot[nm_i + 1]);
1061a9643ea8Slogwang 			__builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
1062a9643ea8Slogwang 			__builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
1063a9643ea8Slogwang 
1064a9643ea8Slogwang 			NM_CHECK_ADDR_LEN(na, addr, len);
1065a9643ea8Slogwang 
1066a9643ea8Slogwang 			if (slot->flags & NS_BUF_CHANGED) {
1067a9643ea8Slogwang 				/* buffer has changed, reload map */
1068*22ce4affSfengbojiang 				netmap_reload_map(na, txq->ift_buf_tag,
1069*22ce4affSfengbojiang 				    txq->ift_sds.ifsd_map[nic_i], addr);
1070a9643ea8Slogwang 			}
1071a9643ea8Slogwang 			/* make sure changes to the buffer are synced */
1072*22ce4affSfengbojiang 			bus_dmamap_sync(txq->ift_buf_tag,
1073*22ce4affSfengbojiang 			    txq->ift_sds.ifsd_map[nic_i],
1074a9643ea8Slogwang 			    BUS_DMASYNC_PREWRITE);
1075a9643ea8Slogwang 
1076*22ce4affSfengbojiang 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED | NS_MOREFRAG);
1077a9643ea8Slogwang 			nm_i = nm_next(nm_i, lim);
1078a9643ea8Slogwang 			nic_i = nm_next(nic_i, lim);
1079a9643ea8Slogwang 		}
1080*22ce4affSfengbojiang 		kring->nr_hwcur = nm_i;
1081a9643ea8Slogwang 
1082a9643ea8Slogwang 		/* synchronize the NIC ring */
1083*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
1084a9643ea8Slogwang 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1085a9643ea8Slogwang 
1086a9643ea8Slogwang 		/* (re)start the tx unit up to slot nic_i (excluded) */
1087a9643ea8Slogwang 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
1088a9643ea8Slogwang 	}
1089a9643ea8Slogwang 
1090a9643ea8Slogwang 	/*
1091a9643ea8Slogwang 	 * Second part: reclaim buffers for completed transmissions.
1092*22ce4affSfengbojiang 	 *
1093*22ce4affSfengbojiang 	 * If there are unclaimed buffers, attempt to reclaim them.
1094*22ce4affSfengbojiang 	 * If we don't manage to reclaim them all, and TX IRQs are not in use,
1095*22ce4affSfengbojiang 	 * trigger a per-tx-queue timer to try again later.
1096a9643ea8Slogwang 	 */
1097*22ce4affSfengbojiang 	if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
1098a9643ea8Slogwang 		if (iflib_tx_credits_update(ctx, txq)) {
1099a9643ea8Slogwang 			/* some tx completed, increment avail */
1100a9643ea8Slogwang 			nic_i = txq->ift_cidx_processed;
1101a9643ea8Slogwang 			kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
1102a9643ea8Slogwang 		}
1103*22ce4affSfengbojiang 	}
1104*22ce4affSfengbojiang 
1105*22ce4affSfengbojiang 	if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ))
1106*22ce4affSfengbojiang 		if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
1107*22ce4affSfengbojiang 			callout_reset_sbt_on(&txq->ift_netmap_timer,
1108*22ce4affSfengbojiang 			    NETMAP_TX_TIMER_US * SBT_1US, SBT_1US,
1109*22ce4affSfengbojiang 			    iflib_netmap_timer, txq,
1110*22ce4affSfengbojiang 			    txq->ift_netmap_timer.c_cpu, 0);
1111*22ce4affSfengbojiang 		}
1112a9643ea8Slogwang 	return (0);
1113a9643ea8Slogwang }
1114a9643ea8Slogwang 
1115a9643ea8Slogwang /*
1116a9643ea8Slogwang  * Reconcile kernel and user view of the receive ring.
1117a9643ea8Slogwang  * Same as for the txsync, this routine must be efficient.
1118a9643ea8Slogwang  * The caller guarantees a single invocations, but races against
1119a9643ea8Slogwang  * the rest of the driver should be handled here.
1120a9643ea8Slogwang  *
1121a9643ea8Slogwang  * On call, kring->rhead is the first packet that userspace wants
1122a9643ea8Slogwang  * to keep, and kring->rcur is the wakeup point.
1123a9643ea8Slogwang  * The kernel has previously reported packets up to kring->rtail.
1124a9643ea8Slogwang  *
1125a9643ea8Slogwang  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
1126a9643ea8Slogwang  * of whether or not we received an interrupt.
1127a9643ea8Slogwang  */
1128a9643ea8Slogwang static int
iflib_netmap_rxsync(struct netmap_kring * kring,int flags)1129a9643ea8Slogwang iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
1130a9643ea8Slogwang {
1131a9643ea8Slogwang 	struct netmap_adapter *na = kring->na;
1132a9643ea8Slogwang 	struct netmap_ring *ring = kring->ring;
1133*22ce4affSfengbojiang 	if_t ifp = na->ifp;
1134*22ce4affSfengbojiang 	uint32_t nm_i;	/* index into the netmap ring */
1135*22ce4affSfengbojiang 	uint32_t nic_i;	/* index into the NIC ring */
1136*22ce4affSfengbojiang 	u_int n;
1137a9643ea8Slogwang 	u_int const lim = kring->nkr_num_slots - 1;
1138a9643ea8Slogwang 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
1139*22ce4affSfengbojiang 	int i = 0;
1140a9643ea8Slogwang 
1141*22ce4affSfengbojiang 	if_ctx_t ctx = ifp->if_softc;
1142*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
1143*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1144*22ce4affSfengbojiang 	iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
1145*22ce4affSfengbojiang 	iflib_fl_t fl = &rxq->ifr_fl[0];
1146*22ce4affSfengbojiang 	struct if_rxd_info ri;
1147*22ce4affSfengbojiang 	qidx_t *cidxp;
1148*22ce4affSfengbojiang 
1149*22ce4affSfengbojiang 	/*
1150*22ce4affSfengbojiang 	 * netmap only uses free list 0, to avoid out of order consumption
1151*22ce4affSfengbojiang 	 * of receive buffers
1152*22ce4affSfengbojiang 	 */
1153*22ce4affSfengbojiang 
1154*22ce4affSfengbojiang 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
1155a9643ea8Slogwang 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1156a9643ea8Slogwang 
1157a9643ea8Slogwang 	/*
1158a9643ea8Slogwang 	 * First part: import newly received packets.
1159a9643ea8Slogwang 	 *
1160a9643ea8Slogwang 	 * nm_i is the index of the next free slot in the netmap ring,
1161*22ce4affSfengbojiang 	 * nic_i is the index of the next received packet in the NIC ring
1162*22ce4affSfengbojiang 	 * (or in the free list 0 if IFLIB_HAS_RXCQ is set), and they may
1163*22ce4affSfengbojiang 	 * differ in case if_init() has been called while
1164a9643ea8Slogwang 	 * in netmap mode. For the receive ring we have
1165a9643ea8Slogwang 	 *
1166*22ce4affSfengbojiang 	 *	nic_i = fl->ifl_cidx;
1167a9643ea8Slogwang 	 *	nm_i = kring->nr_hwtail (previous)
1168a9643ea8Slogwang 	 * and
1169a9643ea8Slogwang 	 *	nm_i == (nic_i + kring->nkr_hwofs) % ring_size
1170a9643ea8Slogwang 	 *
1171*22ce4affSfengbojiang 	 * fl->ifl_cidx is set to 0 on a ring reinit
1172a9643ea8Slogwang 	 */
1173a9643ea8Slogwang 	if (netmap_no_pendintr || force_update) {
1174*22ce4affSfengbojiang 		uint32_t hwtail_lim = nm_prev(kring->nr_hwcur, lim);
1175*22ce4affSfengbojiang 		bool have_rxcq = sctx->isc_flags & IFLIB_HAS_RXCQ;
1176a9643ea8Slogwang 		int crclen = iflib_crcstrip ? 0 : 4;
1177a9643ea8Slogwang 		int error, avail;
1178a9643ea8Slogwang 
1179*22ce4affSfengbojiang 		/*
1180*22ce4affSfengbojiang 		 * For the free list consumer index, we use the same
1181*22ce4affSfengbojiang 		 * logic as in iflib_rxeof().
1182*22ce4affSfengbojiang 		 */
1183*22ce4affSfengbojiang 		if (have_rxcq)
1184*22ce4affSfengbojiang 			cidxp = &rxq->ifr_cq_cidx;
1185*22ce4affSfengbojiang 		else
1186*22ce4affSfengbojiang 			cidxp = &fl->ifl_cidx;
1187*22ce4affSfengbojiang 		avail = ctx->isc_rxd_available(ctx->ifc_softc,
1188*22ce4affSfengbojiang 		    rxq->ifr_id, *cidxp, USHRT_MAX);
1189*22ce4affSfengbojiang 
1190a9643ea8Slogwang 		nic_i = fl->ifl_cidx;
1191a9643ea8Slogwang 		nm_i = netmap_idx_n2k(kring, nic_i);
1192*22ce4affSfengbojiang 		MPASS(nm_i == kring->nr_hwtail);
1193*22ce4affSfengbojiang 		for (n = 0; avail > 0 && nm_i != hwtail_lim; n++, avail--) {
1194*22ce4affSfengbojiang 			rxd_info_zero(&ri);
1195*22ce4affSfengbojiang 			ri.iri_frags = rxq->ifr_frags;
1196*22ce4affSfengbojiang 			ri.iri_qsidx = kring->ring_id;
1197*22ce4affSfengbojiang 			ri.iri_ifp = ctx->ifc_ifp;
1198*22ce4affSfengbojiang 			ri.iri_cidx = *cidxp;
1199*22ce4affSfengbojiang 
1200a9643ea8Slogwang 			error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
1201*22ce4affSfengbojiang 			for (i = 0; i < ri.iri_nfrags; i++) {
1202*22ce4affSfengbojiang 				if (error) {
1203a9643ea8Slogwang 					ring->slot[nm_i].len = 0;
1204*22ce4affSfengbojiang 					ring->slot[nm_i].flags = 0;
1205*22ce4affSfengbojiang 				} else {
1206*22ce4affSfengbojiang 					ring->slot[nm_i].len = ri.iri_frags[i].irf_len;
1207*22ce4affSfengbojiang 					if (i == (ri.iri_nfrags - 1)) {
1208*22ce4affSfengbojiang 						ring->slot[nm_i].len -= crclen;
1209*22ce4affSfengbojiang 						ring->slot[nm_i].flags = 0;
1210*22ce4affSfengbojiang 					} else
1211*22ce4affSfengbojiang 						ring->slot[nm_i].flags = NS_MOREFRAG;
1212*22ce4affSfengbojiang 				}
1213*22ce4affSfengbojiang 
1214*22ce4affSfengbojiang 				bus_dmamap_sync(fl->ifl_buf_tag,
1215*22ce4affSfengbojiang 				    fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
1216a9643ea8Slogwang 				nm_i = nm_next(nm_i, lim);
1217*22ce4affSfengbojiang 				fl->ifl_cidx = nic_i = nm_next(nic_i, lim);
1218*22ce4affSfengbojiang 			}
1219*22ce4affSfengbojiang 
1220*22ce4affSfengbojiang 			if (have_rxcq) {
1221*22ce4affSfengbojiang 				*cidxp = ri.iri_cidx;
1222*22ce4affSfengbojiang 				while (*cidxp >= scctx->isc_nrxd[0])
1223*22ce4affSfengbojiang 					*cidxp -= scctx->isc_nrxd[0];
1224*22ce4affSfengbojiang 			}
1225*22ce4affSfengbojiang 
1226a9643ea8Slogwang 		}
1227a9643ea8Slogwang 		if (n) { /* update the state variables */
1228a9643ea8Slogwang 			if (netmap_no_pendintr && !force_update) {
1229a9643ea8Slogwang 				/* diagnostics */
1230a9643ea8Slogwang 				iflib_rx_miss ++;
1231a9643ea8Slogwang 				iflib_rx_miss_bufs += n;
1232a9643ea8Slogwang 			}
1233a9643ea8Slogwang 			kring->nr_hwtail = nm_i;
1234a9643ea8Slogwang 		}
1235a9643ea8Slogwang 		kring->nr_kflags &= ~NKR_PENDINTR;
1236a9643ea8Slogwang 	}
1237a9643ea8Slogwang 	/*
1238a9643ea8Slogwang 	 * Second part: skip past packets that userspace has released.
1239a9643ea8Slogwang 	 * (kring->nr_hwcur to head excluded),
1240a9643ea8Slogwang 	 * and make the buffers available for reception.
1241a9643ea8Slogwang 	 * As usual nm_i is the index in the netmap ring,
1242a9643ea8Slogwang 	 * nic_i is the index in the NIC ring, and
1243a9643ea8Slogwang 	 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
1244a9643ea8Slogwang 	 */
1245*22ce4affSfengbojiang 	netmap_fl_refill(rxq, kring, false);
1246a9643ea8Slogwang 
1247*22ce4affSfengbojiang 	return (0);
1248a9643ea8Slogwang }
1249a9643ea8Slogwang 
1250*22ce4affSfengbojiang static void
iflib_netmap_intr(struct netmap_adapter * na,int onoff)1251*22ce4affSfengbojiang iflib_netmap_intr(struct netmap_adapter *na, int onoff)
1252*22ce4affSfengbojiang {
1253*22ce4affSfengbojiang 	if_ctx_t ctx = na->ifp->if_softc;
1254a9643ea8Slogwang 
1255*22ce4affSfengbojiang 	CTX_LOCK(ctx);
1256*22ce4affSfengbojiang 	if (onoff) {
1257*22ce4affSfengbojiang 		IFDI_INTR_ENABLE(ctx);
1258*22ce4affSfengbojiang 	} else {
1259*22ce4affSfengbojiang 		IFDI_INTR_DISABLE(ctx);
1260*22ce4affSfengbojiang 	}
1261*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
1262a9643ea8Slogwang }
1263a9643ea8Slogwang 
1264a9643ea8Slogwang static int
iflib_netmap_attach(if_ctx_t ctx)1265a9643ea8Slogwang iflib_netmap_attach(if_ctx_t ctx)
1266a9643ea8Slogwang {
1267a9643ea8Slogwang 	struct netmap_adapter na;
1268a9643ea8Slogwang 
1269a9643ea8Slogwang 	bzero(&na, sizeof(na));
1270a9643ea8Slogwang 
1271a9643ea8Slogwang 	na.ifp = ctx->ifc_ifp;
1272*22ce4affSfengbojiang 	na.na_flags = NAF_BDG_MAYSLEEP | NAF_MOREFRAG;
1273a9643ea8Slogwang 	MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
1274a9643ea8Slogwang 	MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
1275a9643ea8Slogwang 
1276*22ce4affSfengbojiang 	na.num_tx_desc = iflib_num_tx_descs(ctx);
1277*22ce4affSfengbojiang 	na.num_rx_desc = iflib_num_rx_descs(ctx);
1278a9643ea8Slogwang 	na.nm_txsync = iflib_netmap_txsync;
1279a9643ea8Slogwang 	na.nm_rxsync = iflib_netmap_rxsync;
1280a9643ea8Slogwang 	na.nm_register = iflib_netmap_register;
1281*22ce4affSfengbojiang 	na.nm_intr = iflib_netmap_intr;
1282a9643ea8Slogwang 	na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
1283a9643ea8Slogwang 	na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
1284a9643ea8Slogwang 	return (netmap_attach(&na));
1285a9643ea8Slogwang }
1286a9643ea8Slogwang 
1287*22ce4affSfengbojiang static int
iflib_netmap_txq_init(if_ctx_t ctx,iflib_txq_t txq)1288a9643ea8Slogwang iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
1289a9643ea8Slogwang {
1290a9643ea8Slogwang 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
1291a9643ea8Slogwang 	struct netmap_slot *slot;
1292a9643ea8Slogwang 
1293a9643ea8Slogwang 	slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1294*22ce4affSfengbojiang 	if (slot == NULL)
1295*22ce4affSfengbojiang 		return (0);
1296a9643ea8Slogwang 	for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
1297a9643ea8Slogwang 		/*
1298a9643ea8Slogwang 		 * In netmap mode, set the map for the packet buffer.
1299a9643ea8Slogwang 		 * NOTE: Some drivers (not this one) also need to set
1300a9643ea8Slogwang 		 * the physical buffer address in the NIC ring.
1301a9643ea8Slogwang 		 * netmap_idx_n2k() maps a nic index, i, into the corresponding
1302a9643ea8Slogwang 		 * netmap slot index, si
1303a9643ea8Slogwang 		 */
1304*22ce4affSfengbojiang 		int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i);
1305*22ce4affSfengbojiang 		netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i],
1306*22ce4affSfengbojiang 		    NMB(na, slot + si));
1307a9643ea8Slogwang 	}
1308*22ce4affSfengbojiang 	return (1);
1309a9643ea8Slogwang }
1310*22ce4affSfengbojiang 
1311*22ce4affSfengbojiang static int
iflib_netmap_rxq_init(if_ctx_t ctx,iflib_rxq_t rxq)1312a9643ea8Slogwang iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
1313a9643ea8Slogwang {
1314a9643ea8Slogwang 	struct netmap_adapter *na = NA(ctx->ifc_ifp);
1315*22ce4affSfengbojiang 	struct netmap_kring *kring;
1316a9643ea8Slogwang 	struct netmap_slot *slot;
1317a9643ea8Slogwang 
1318a9643ea8Slogwang 	slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1319*22ce4affSfengbojiang 	if (slot == NULL)
1320*22ce4affSfengbojiang 		return (0);
1321*22ce4affSfengbojiang 	kring = na->rx_rings[rxq->ifr_id];
1322*22ce4affSfengbojiang 	netmap_fl_refill(rxq, kring, true);
1323*22ce4affSfengbojiang 	return (1);
1324a9643ea8Slogwang }
1325*22ce4affSfengbojiang 
1326*22ce4affSfengbojiang static void
iflib_netmap_timer(void * arg)1327*22ce4affSfengbojiang iflib_netmap_timer(void *arg)
1328*22ce4affSfengbojiang {
1329*22ce4affSfengbojiang 	iflib_txq_t txq = arg;
1330*22ce4affSfengbojiang 	if_ctx_t ctx = txq->ift_ctx;
1331*22ce4affSfengbojiang 
1332*22ce4affSfengbojiang 	/*
1333*22ce4affSfengbojiang 	 * Wake up the netmap application, to give it a chance to
1334*22ce4affSfengbojiang 	 * call txsync and reclaim more completed TX buffers.
1335*22ce4affSfengbojiang 	 */
1336*22ce4affSfengbojiang 	netmap_tx_irq(ctx->ifc_ifp, txq->ift_id);
1337a9643ea8Slogwang }
1338a9643ea8Slogwang 
1339a9643ea8Slogwang #define iflib_netmap_detach(ifp) netmap_detach(ifp)
1340a9643ea8Slogwang 
1341a9643ea8Slogwang #else
1342*22ce4affSfengbojiang #define iflib_netmap_txq_init(ctx, txq) (0)
1343*22ce4affSfengbojiang #define iflib_netmap_rxq_init(ctx, rxq) (0)
1344a9643ea8Slogwang #define iflib_netmap_detach(ifp)
1345*22ce4affSfengbojiang #define netmap_enable_all_rings(ifp)
1346*22ce4affSfengbojiang #define netmap_disable_all_rings(ifp)
1347a9643ea8Slogwang 
1348a9643ea8Slogwang #define iflib_netmap_attach(ctx) (0)
1349a9643ea8Slogwang #define netmap_rx_irq(ifp, qid, budget) (0)
1350a9643ea8Slogwang #endif
1351a9643ea8Slogwang 
1352a9643ea8Slogwang #if defined(__i386__) || defined(__amd64__)
1353a9643ea8Slogwang static __inline void
prefetch(void * x)1354a9643ea8Slogwang prefetch(void *x)
1355a9643ea8Slogwang {
1356a9643ea8Slogwang 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1357a9643ea8Slogwang }
1358*22ce4affSfengbojiang static __inline void
prefetch2cachelines(void * x)1359*22ce4affSfengbojiang prefetch2cachelines(void *x)
1360*22ce4affSfengbojiang {
1361*22ce4affSfengbojiang 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1362*22ce4affSfengbojiang #if (CACHE_LINE_SIZE < 128)
1363*22ce4affSfengbojiang 	__asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long)))));
1364*22ce4affSfengbojiang #endif
1365*22ce4affSfengbojiang }
1366a9643ea8Slogwang #else
1367a9643ea8Slogwang #define prefetch(x)
1368*22ce4affSfengbojiang #define prefetch2cachelines(x)
1369a9643ea8Slogwang #endif
1370a9643ea8Slogwang 
1371a9643ea8Slogwang static void
iru_init(if_rxd_update_t iru,iflib_rxq_t rxq,uint8_t flid)1372*22ce4affSfengbojiang iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid)
1373*22ce4affSfengbojiang {
1374*22ce4affSfengbojiang 	iflib_fl_t fl;
1375*22ce4affSfengbojiang 
1376*22ce4affSfengbojiang 	fl = &rxq->ifr_fl[flid];
1377*22ce4affSfengbojiang 	iru->iru_paddrs = fl->ifl_bus_addrs;
1378*22ce4affSfengbojiang 	iru->iru_idxs = fl->ifl_rxd_idxs;
1379*22ce4affSfengbojiang 	iru->iru_qsidx = rxq->ifr_id;
1380*22ce4affSfengbojiang 	iru->iru_buf_size = fl->ifl_buf_size;
1381*22ce4affSfengbojiang 	iru->iru_flidx = fl->ifl_id;
1382*22ce4affSfengbojiang }
1383*22ce4affSfengbojiang 
1384*22ce4affSfengbojiang static void
_iflib_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int err)1385a9643ea8Slogwang _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
1386a9643ea8Slogwang {
1387a9643ea8Slogwang 	if (err)
1388a9643ea8Slogwang 		return;
1389a9643ea8Slogwang 	*(bus_addr_t *) arg = segs[0].ds_addr;
1390a9643ea8Slogwang }
1391a9643ea8Slogwang 
1392a9643ea8Slogwang int
iflib_dma_alloc_align(if_ctx_t ctx,int size,int align,iflib_dma_info_t dma,int mapflags)1393*22ce4affSfengbojiang iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags)
1394a9643ea8Slogwang {
1395a9643ea8Slogwang 	int err;
1396a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
1397a9643ea8Slogwang 
1398a9643ea8Slogwang 	err = bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
1399*22ce4affSfengbojiang 				align, 0,		/* alignment, bounds */
1400a9643ea8Slogwang 				BUS_SPACE_MAXADDR,	/* lowaddr */
1401a9643ea8Slogwang 				BUS_SPACE_MAXADDR,	/* highaddr */
1402a9643ea8Slogwang 				NULL, NULL,		/* filter, filterarg */
1403a9643ea8Slogwang 				size,			/* maxsize */
1404a9643ea8Slogwang 				1,			/* nsegments */
1405a9643ea8Slogwang 				size,			/* maxsegsize */
1406a9643ea8Slogwang 				BUS_DMA_ALLOCNOW,	/* flags */
1407a9643ea8Slogwang 				NULL,			/* lockfunc */
1408a9643ea8Slogwang 				NULL,			/* lockarg */
1409a9643ea8Slogwang 				&dma->idi_tag);
1410a9643ea8Slogwang 	if (err) {
1411a9643ea8Slogwang 		device_printf(dev,
1412a9643ea8Slogwang 		    "%s: bus_dma_tag_create failed: %d\n",
1413a9643ea8Slogwang 		    __func__, err);
1414a9643ea8Slogwang 		goto fail_0;
1415a9643ea8Slogwang 	}
1416a9643ea8Slogwang 
1417a9643ea8Slogwang 	err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
1418a9643ea8Slogwang 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
1419a9643ea8Slogwang 	if (err) {
1420a9643ea8Slogwang 		device_printf(dev,
1421a9643ea8Slogwang 		    "%s: bus_dmamem_alloc(%ju) failed: %d\n",
1422a9643ea8Slogwang 		    __func__, (uintmax_t)size, err);
1423a9643ea8Slogwang 		goto fail_1;
1424a9643ea8Slogwang 	}
1425a9643ea8Slogwang 
1426a9643ea8Slogwang 	dma->idi_paddr = IF_BAD_DMA;
1427a9643ea8Slogwang 	err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
1428a9643ea8Slogwang 	    size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
1429a9643ea8Slogwang 	if (err || dma->idi_paddr == IF_BAD_DMA) {
1430a9643ea8Slogwang 		device_printf(dev,
1431a9643ea8Slogwang 		    "%s: bus_dmamap_load failed: %d\n",
1432a9643ea8Slogwang 		    __func__, err);
1433a9643ea8Slogwang 		goto fail_2;
1434a9643ea8Slogwang 	}
1435a9643ea8Slogwang 
1436a9643ea8Slogwang 	dma->idi_size = size;
1437a9643ea8Slogwang 	return (0);
1438a9643ea8Slogwang 
1439a9643ea8Slogwang fail_2:
1440a9643ea8Slogwang 	bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
1441a9643ea8Slogwang fail_1:
1442a9643ea8Slogwang 	bus_dma_tag_destroy(dma->idi_tag);
1443a9643ea8Slogwang fail_0:
1444a9643ea8Slogwang 	dma->idi_tag = NULL;
1445a9643ea8Slogwang 
1446a9643ea8Slogwang 	return (err);
1447a9643ea8Slogwang }
1448a9643ea8Slogwang 
1449a9643ea8Slogwang int
iflib_dma_alloc(if_ctx_t ctx,int size,iflib_dma_info_t dma,int mapflags)1450*22ce4affSfengbojiang iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
1451*22ce4affSfengbojiang {
1452*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
1453*22ce4affSfengbojiang 
1454*22ce4affSfengbojiang 	KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
1455*22ce4affSfengbojiang 
1456*22ce4affSfengbojiang 	return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags));
1457*22ce4affSfengbojiang }
1458*22ce4affSfengbojiang 
1459*22ce4affSfengbojiang int
iflib_dma_alloc_multi(if_ctx_t ctx,int * sizes,iflib_dma_info_t * dmalist,int mapflags,int count)1460a9643ea8Slogwang iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
1461a9643ea8Slogwang {
1462a9643ea8Slogwang 	int i, err;
1463a9643ea8Slogwang 	iflib_dma_info_t *dmaiter;
1464a9643ea8Slogwang 
1465a9643ea8Slogwang 	dmaiter = dmalist;
1466a9643ea8Slogwang 	for (i = 0; i < count; i++, dmaiter++) {
1467a9643ea8Slogwang 		if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
1468a9643ea8Slogwang 			break;
1469a9643ea8Slogwang 	}
1470a9643ea8Slogwang 	if (err)
1471a9643ea8Slogwang 		iflib_dma_free_multi(dmalist, i);
1472a9643ea8Slogwang 	return (err);
1473a9643ea8Slogwang }
1474a9643ea8Slogwang 
1475a9643ea8Slogwang void
iflib_dma_free(iflib_dma_info_t dma)1476a9643ea8Slogwang iflib_dma_free(iflib_dma_info_t dma)
1477a9643ea8Slogwang {
1478a9643ea8Slogwang 	if (dma->idi_tag == NULL)
1479a9643ea8Slogwang 		return;
1480a9643ea8Slogwang 	if (dma->idi_paddr != IF_BAD_DMA) {
1481a9643ea8Slogwang 		bus_dmamap_sync(dma->idi_tag, dma->idi_map,
1482a9643ea8Slogwang 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1483a9643ea8Slogwang 		bus_dmamap_unload(dma->idi_tag, dma->idi_map);
1484a9643ea8Slogwang 		dma->idi_paddr = IF_BAD_DMA;
1485a9643ea8Slogwang 	}
1486a9643ea8Slogwang 	if (dma->idi_vaddr != NULL) {
1487a9643ea8Slogwang 		bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
1488a9643ea8Slogwang 		dma->idi_vaddr = NULL;
1489a9643ea8Slogwang 	}
1490a9643ea8Slogwang 	bus_dma_tag_destroy(dma->idi_tag);
1491a9643ea8Slogwang 	dma->idi_tag = NULL;
1492a9643ea8Slogwang }
1493a9643ea8Slogwang 
1494a9643ea8Slogwang void
iflib_dma_free_multi(iflib_dma_info_t * dmalist,int count)1495a9643ea8Slogwang iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
1496a9643ea8Slogwang {
1497a9643ea8Slogwang 	int i;
1498a9643ea8Slogwang 	iflib_dma_info_t *dmaiter = dmalist;
1499a9643ea8Slogwang 
1500a9643ea8Slogwang 	for (i = 0; i < count; i++, dmaiter++)
1501a9643ea8Slogwang 		iflib_dma_free(*dmaiter);
1502a9643ea8Slogwang }
1503a9643ea8Slogwang 
1504a9643ea8Slogwang static int
iflib_fast_intr(void * arg)1505a9643ea8Slogwang iflib_fast_intr(void *arg)
1506a9643ea8Slogwang {
1507a9643ea8Slogwang 	iflib_filter_info_t info = arg;
1508a9643ea8Slogwang 	struct grouptask *gtask = info->ifi_task;
1509*22ce4affSfengbojiang 	int result;
1510a9643ea8Slogwang 
1511a9643ea8Slogwang 	DBG_COUNTER_INC(fast_intrs);
1512*22ce4affSfengbojiang 	if (info->ifi_filter != NULL) {
1513*22ce4affSfengbojiang 		result = info->ifi_filter(info->ifi_filter_arg);
1514*22ce4affSfengbojiang 		if ((result & FILTER_SCHEDULE_THREAD) == 0)
1515*22ce4affSfengbojiang 			return (result);
1516*22ce4affSfengbojiang 	}
1517*22ce4affSfengbojiang 
1518*22ce4affSfengbojiang 	GROUPTASK_ENQUEUE(gtask);
1519a9643ea8Slogwang 	return (FILTER_HANDLED);
1520*22ce4affSfengbojiang }
1521*22ce4affSfengbojiang 
1522*22ce4affSfengbojiang static int
iflib_fast_intr_rxtx(void * arg)1523*22ce4affSfengbojiang iflib_fast_intr_rxtx(void *arg)
1524*22ce4affSfengbojiang {
1525*22ce4affSfengbojiang 	iflib_filter_info_t info = arg;
1526*22ce4affSfengbojiang 	struct grouptask *gtask = info->ifi_task;
1527*22ce4affSfengbojiang 	if_ctx_t ctx;
1528*22ce4affSfengbojiang 	iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
1529*22ce4affSfengbojiang 	iflib_txq_t txq;
1530*22ce4affSfengbojiang 	void *sc;
1531*22ce4affSfengbojiang 	int i, cidx, result;
1532*22ce4affSfengbojiang 	qidx_t txqid;
1533*22ce4affSfengbojiang 	bool intr_enable, intr_legacy;
1534*22ce4affSfengbojiang 
1535*22ce4affSfengbojiang 	DBG_COUNTER_INC(fast_intrs);
1536*22ce4affSfengbojiang 	if (info->ifi_filter != NULL) {
1537*22ce4affSfengbojiang 		result = info->ifi_filter(info->ifi_filter_arg);
1538*22ce4affSfengbojiang 		if ((result & FILTER_SCHEDULE_THREAD) == 0)
1539*22ce4affSfengbojiang 			return (result);
1540*22ce4affSfengbojiang 	}
1541*22ce4affSfengbojiang 
1542*22ce4affSfengbojiang 	ctx = rxq->ifr_ctx;
1543*22ce4affSfengbojiang 	sc = ctx->ifc_softc;
1544*22ce4affSfengbojiang 	intr_enable = false;
1545*22ce4affSfengbojiang 	intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY);
1546*22ce4affSfengbojiang 	MPASS(rxq->ifr_ntxqirq);
1547*22ce4affSfengbojiang 	for (i = 0; i < rxq->ifr_ntxqirq; i++) {
1548*22ce4affSfengbojiang 		txqid = rxq->ifr_txqid[i];
1549*22ce4affSfengbojiang 		txq = &ctx->ifc_txqs[txqid];
1550*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
1551*22ce4affSfengbojiang 		    BUS_DMASYNC_POSTREAD);
1552*22ce4affSfengbojiang 		if (!ctx->isc_txd_credits_update(sc, txqid, false)) {
1553*22ce4affSfengbojiang 			if (intr_legacy)
1554*22ce4affSfengbojiang 				intr_enable = true;
1555*22ce4affSfengbojiang 			else
1556*22ce4affSfengbojiang 				IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
1557*22ce4affSfengbojiang 			continue;
1558*22ce4affSfengbojiang 		}
1559*22ce4affSfengbojiang 		GROUPTASK_ENQUEUE(&txq->ift_task);
1560*22ce4affSfengbojiang 	}
1561*22ce4affSfengbojiang 	if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
1562*22ce4affSfengbojiang 		cidx = rxq->ifr_cq_cidx;
1563*22ce4affSfengbojiang 	else
1564*22ce4affSfengbojiang 		cidx = rxq->ifr_fl[0].ifl_cidx;
1565*22ce4affSfengbojiang 	if (iflib_rxd_avail(ctx, rxq, cidx, 1))
1566*22ce4affSfengbojiang 		GROUPTASK_ENQUEUE(gtask);
1567*22ce4affSfengbojiang 	else {
1568*22ce4affSfengbojiang 		if (intr_legacy)
1569*22ce4affSfengbojiang 			intr_enable = true;
1570*22ce4affSfengbojiang 		else
1571*22ce4affSfengbojiang 			IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
1572*22ce4affSfengbojiang 		DBG_COUNTER_INC(rx_intr_enables);
1573*22ce4affSfengbojiang 	}
1574*22ce4affSfengbojiang 	if (intr_enable)
1575*22ce4affSfengbojiang 		IFDI_INTR_ENABLE(ctx);
1576*22ce4affSfengbojiang 	return (FILTER_HANDLED);
1577*22ce4affSfengbojiang }
1578*22ce4affSfengbojiang 
1579*22ce4affSfengbojiang static int
iflib_fast_intr_ctx(void * arg)1580*22ce4affSfengbojiang iflib_fast_intr_ctx(void *arg)
1581*22ce4affSfengbojiang {
1582*22ce4affSfengbojiang 	iflib_filter_info_t info = arg;
1583*22ce4affSfengbojiang 	struct grouptask *gtask = info->ifi_task;
1584*22ce4affSfengbojiang 	int result;
1585*22ce4affSfengbojiang 
1586*22ce4affSfengbojiang 	DBG_COUNTER_INC(fast_intrs);
1587*22ce4affSfengbojiang 	if (info->ifi_filter != NULL) {
1588*22ce4affSfengbojiang 		result = info->ifi_filter(info->ifi_filter_arg);
1589*22ce4affSfengbojiang 		if ((result & FILTER_SCHEDULE_THREAD) == 0)
1590*22ce4affSfengbojiang 			return (result);
1591*22ce4affSfengbojiang 	}
1592a9643ea8Slogwang 
1593a9643ea8Slogwang 	GROUPTASK_ENQUEUE(gtask);
1594a9643ea8Slogwang 	return (FILTER_HANDLED);
1595a9643ea8Slogwang }
1596a9643ea8Slogwang 
1597a9643ea8Slogwang static int
_iflib_irq_alloc(if_ctx_t ctx,if_irq_t irq,int rid,driver_filter_t filter,driver_intr_t handler,void * arg,const char * name)1598a9643ea8Slogwang _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
1599a9643ea8Slogwang 		 driver_filter_t filter, driver_intr_t handler, void *arg,
1600*22ce4affSfengbojiang 		 const char *name)
1601a9643ea8Slogwang {
1602a9643ea8Slogwang 	struct resource *res;
1603*22ce4affSfengbojiang 	void *tag = NULL;
1604a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
1605*22ce4affSfengbojiang 	int flags, i, rc;
1606a9643ea8Slogwang 
1607*22ce4affSfengbojiang 	flags = RF_ACTIVE;
1608*22ce4affSfengbojiang 	if (ctx->ifc_flags & IFC_LEGACY)
1609*22ce4affSfengbojiang 		flags |= RF_SHAREABLE;
1610a9643ea8Slogwang 	MPASS(rid < 512);
1611*22ce4affSfengbojiang 	i = rid;
1612*22ce4affSfengbojiang 	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, flags);
1613a9643ea8Slogwang 	if (res == NULL) {
1614a9643ea8Slogwang 		device_printf(dev,
1615a9643ea8Slogwang 		    "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
1616a9643ea8Slogwang 		return (ENOMEM);
1617a9643ea8Slogwang 	}
1618a9643ea8Slogwang 	irq->ii_res = res;
1619a9643ea8Slogwang 	KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
1620a9643ea8Slogwang 	rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
1621a9643ea8Slogwang 						filter, handler, arg, &tag);
1622a9643ea8Slogwang 	if (rc != 0) {
1623a9643ea8Slogwang 		device_printf(dev,
1624a9643ea8Slogwang 		    "failed to setup interrupt for rid %d, name %s: %d\n",
1625a9643ea8Slogwang 					  rid, name ? name : "unknown", rc);
1626a9643ea8Slogwang 		return (rc);
1627a9643ea8Slogwang 	} else if (name)
1628*22ce4affSfengbojiang 		bus_describe_intr(dev, res, tag, "%s", name);
1629a9643ea8Slogwang 
1630a9643ea8Slogwang 	irq->ii_tag = tag;
1631a9643ea8Slogwang 	return (0);
1632a9643ea8Slogwang }
1633a9643ea8Slogwang 
1634a9643ea8Slogwang /*********************************************************************
1635a9643ea8Slogwang  *
1636*22ce4affSfengbojiang  *  Allocate DMA resources for TX buffers as well as memory for the TX
1637*22ce4affSfengbojiang  *  mbuf map.  TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a
1638*22ce4affSfengbojiang  *  iflib_sw_tx_desc_array structure, storing all the information that
1639*22ce4affSfengbojiang  *  is needed to transmit a packet on the wire.  This is called only
1640*22ce4affSfengbojiang  *  once at attach, setup is done every reset.
1641a9643ea8Slogwang  *
1642a9643ea8Slogwang  **********************************************************************/
1643a9643ea8Slogwang static int
iflib_txsd_alloc(iflib_txq_t txq)1644a9643ea8Slogwang iflib_txsd_alloc(iflib_txq_t txq)
1645a9643ea8Slogwang {
1646a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
1647a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
1648a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1649a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
1650*22ce4affSfengbojiang 	bus_size_t tsomaxsize;
1651a9643ea8Slogwang 	int err, nsegments, ntsosegments;
1652*22ce4affSfengbojiang 	bool tso;
1653a9643ea8Slogwang 
1654a9643ea8Slogwang 	nsegments = scctx->isc_tx_nsegments;
1655a9643ea8Slogwang 	ntsosegments = scctx->isc_tx_tso_segments_max;
1656*22ce4affSfengbojiang 	tsomaxsize = scctx->isc_tx_tso_size_max;
1657*22ce4affSfengbojiang 	if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU)
1658*22ce4affSfengbojiang 		tsomaxsize += sizeof(struct ether_vlan_header);
1659a9643ea8Slogwang 	MPASS(scctx->isc_ntxd[0] > 0);
1660a9643ea8Slogwang 	MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
1661a9643ea8Slogwang 	MPASS(nsegments > 0);
1662*22ce4affSfengbojiang 	if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) {
1663a9643ea8Slogwang 		MPASS(ntsosegments > 0);
1664*22ce4affSfengbojiang 		MPASS(sctx->isc_tso_maxsize >= tsomaxsize);
1665*22ce4affSfengbojiang 	}
1666*22ce4affSfengbojiang 
1667a9643ea8Slogwang 	/*
1668*22ce4affSfengbojiang 	 * Set up DMA tags for TX buffers.
1669a9643ea8Slogwang 	 */
1670a9643ea8Slogwang 	if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
1671a9643ea8Slogwang 			       1, 0,			/* alignment, bounds */
1672a9643ea8Slogwang 			       BUS_SPACE_MAXADDR,	/* lowaddr */
1673a9643ea8Slogwang 			       BUS_SPACE_MAXADDR,	/* highaddr */
1674a9643ea8Slogwang 			       NULL, NULL,		/* filter, filterarg */
1675a9643ea8Slogwang 			       sctx->isc_tx_maxsize,		/* maxsize */
1676a9643ea8Slogwang 			       nsegments,	/* nsegments */
1677a9643ea8Slogwang 			       sctx->isc_tx_maxsegsize,	/* maxsegsize */
1678a9643ea8Slogwang 			       0,			/* flags */
1679a9643ea8Slogwang 			       NULL,			/* lockfunc */
1680a9643ea8Slogwang 			       NULL,			/* lockfuncarg */
1681*22ce4affSfengbojiang 			       &txq->ift_buf_tag))) {
1682a9643ea8Slogwang 		device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
1683*22ce4affSfengbojiang 		device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n",
1684*22ce4affSfengbojiang 		    (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
1685a9643ea8Slogwang 		goto fail;
1686a9643ea8Slogwang 	}
1687*22ce4affSfengbojiang 	tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0;
1688*22ce4affSfengbojiang 	if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev),
1689a9643ea8Slogwang 			       1, 0,			/* alignment, bounds */
1690a9643ea8Slogwang 			       BUS_SPACE_MAXADDR,	/* lowaddr */
1691a9643ea8Slogwang 			       BUS_SPACE_MAXADDR,	/* highaddr */
1692a9643ea8Slogwang 			       NULL, NULL,		/* filter, filterarg */
1693*22ce4affSfengbojiang 			       tsomaxsize,		/* maxsize */
1694a9643ea8Slogwang 			       ntsosegments,	/* nsegments */
1695*22ce4affSfengbojiang 			       sctx->isc_tso_maxsegsize,/* maxsegsize */
1696a9643ea8Slogwang 			       0,			/* flags */
1697a9643ea8Slogwang 			       NULL,			/* lockfunc */
1698a9643ea8Slogwang 			       NULL,			/* lockfuncarg */
1699*22ce4affSfengbojiang 			       &txq->ift_tso_buf_tag))) {
1700*22ce4affSfengbojiang 		device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n",
1701*22ce4affSfengbojiang 		    err);
1702*22ce4affSfengbojiang 		goto fail;
1703*22ce4affSfengbojiang 	}
1704a9643ea8Slogwang 
1705*22ce4affSfengbojiang 	/* Allocate memory for the TX mbuf map. */
1706a9643ea8Slogwang 	if (!(txq->ift_sds.ifsd_m =
1707a9643ea8Slogwang 	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
1708a9643ea8Slogwang 	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1709*22ce4affSfengbojiang 		device_printf(dev, "Unable to allocate TX mbuf map memory\n");
1710a9643ea8Slogwang 		err = ENOMEM;
1711a9643ea8Slogwang 		goto fail;
1712a9643ea8Slogwang 	}
1713a9643ea8Slogwang 
1714*22ce4affSfengbojiang 	/*
1715*22ce4affSfengbojiang 	 * Create the DMA maps for TX buffers.
1716*22ce4affSfengbojiang 	 */
1717*22ce4affSfengbojiang 	if ((txq->ift_sds.ifsd_map = (bus_dmamap_t *)malloc(
1718*22ce4affSfengbojiang 	    sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
1719*22ce4affSfengbojiang 	    M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
1720*22ce4affSfengbojiang 		device_printf(dev,
1721*22ce4affSfengbojiang 		    "Unable to allocate TX buffer DMA map memory\n");
1722a9643ea8Slogwang 		err = ENOMEM;
1723a9643ea8Slogwang 		goto fail;
1724a9643ea8Slogwang 	}
1725*22ce4affSfengbojiang 	if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc(
1726*22ce4affSfengbojiang 	    sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
1727*22ce4affSfengbojiang 	    M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
1728*22ce4affSfengbojiang 		device_printf(dev,
1729*22ce4affSfengbojiang 		    "Unable to allocate TSO TX buffer map memory\n");
1730*22ce4affSfengbojiang 		err = ENOMEM;
1731*22ce4affSfengbojiang 		goto fail;
1732*22ce4affSfengbojiang 	}
1733a9643ea8Slogwang 	for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
1734*22ce4affSfengbojiang 		err = bus_dmamap_create(txq->ift_buf_tag, 0,
1735*22ce4affSfengbojiang 		    &txq->ift_sds.ifsd_map[i]);
1736a9643ea8Slogwang 		if (err != 0) {
1737a9643ea8Slogwang 			device_printf(dev, "Unable to create TX DMA map\n");
1738a9643ea8Slogwang 			goto fail;
1739a9643ea8Slogwang 		}
1740*22ce4affSfengbojiang 		if (!tso)
1741*22ce4affSfengbojiang 			continue;
1742*22ce4affSfengbojiang 		err = bus_dmamap_create(txq->ift_tso_buf_tag, 0,
1743*22ce4affSfengbojiang 		    &txq->ift_sds.ifsd_tso_map[i]);
1744*22ce4affSfengbojiang 		if (err != 0) {
1745*22ce4affSfengbojiang 			device_printf(dev, "Unable to create TSO TX DMA map\n");
1746*22ce4affSfengbojiang 			goto fail;
1747a9643ea8Slogwang 		}
1748*22ce4affSfengbojiang 	}
1749a9643ea8Slogwang 	return (0);
1750a9643ea8Slogwang fail:
1751a9643ea8Slogwang 	/* We free all, it handles case where we are in the middle */
1752a9643ea8Slogwang 	iflib_tx_structures_free(ctx);
1753a9643ea8Slogwang 	return (err);
1754a9643ea8Slogwang }
1755a9643ea8Slogwang 
1756a9643ea8Slogwang static void
iflib_txsd_destroy(if_ctx_t ctx,iflib_txq_t txq,int i)1757a9643ea8Slogwang iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
1758a9643ea8Slogwang {
1759a9643ea8Slogwang 	bus_dmamap_t map;
1760a9643ea8Slogwang 
1761*22ce4affSfengbojiang 	if (txq->ift_sds.ifsd_map != NULL) {
1762a9643ea8Slogwang 		map = txq->ift_sds.ifsd_map[i];
1763*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE);
1764*22ce4affSfengbojiang 		bus_dmamap_unload(txq->ift_buf_tag, map);
1765*22ce4affSfengbojiang 		bus_dmamap_destroy(txq->ift_buf_tag, map);
1766a9643ea8Slogwang 		txq->ift_sds.ifsd_map[i] = NULL;
1767a9643ea8Slogwang 	}
1768*22ce4affSfengbojiang 
1769*22ce4affSfengbojiang 	if (txq->ift_sds.ifsd_tso_map != NULL) {
1770*22ce4affSfengbojiang 		map = txq->ift_sds.ifsd_tso_map[i];
1771*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_tso_buf_tag, map,
1772*22ce4affSfengbojiang 		    BUS_DMASYNC_POSTWRITE);
1773*22ce4affSfengbojiang 		bus_dmamap_unload(txq->ift_tso_buf_tag, map);
1774*22ce4affSfengbojiang 		bus_dmamap_destroy(txq->ift_tso_buf_tag, map);
1775*22ce4affSfengbojiang 		txq->ift_sds.ifsd_tso_map[i] = NULL;
1776*22ce4affSfengbojiang 	}
1777a9643ea8Slogwang }
1778a9643ea8Slogwang 
1779a9643ea8Slogwang static void
iflib_txq_destroy(iflib_txq_t txq)1780a9643ea8Slogwang iflib_txq_destroy(iflib_txq_t txq)
1781a9643ea8Slogwang {
1782a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
1783a9643ea8Slogwang 
1784a9643ea8Slogwang 	for (int i = 0; i < txq->ift_size; i++)
1785a9643ea8Slogwang 		iflib_txsd_destroy(ctx, txq, i);
1786*22ce4affSfengbojiang 
1787*22ce4affSfengbojiang 	if (txq->ift_br != NULL) {
1788*22ce4affSfengbojiang 		ifmp_ring_free(txq->ift_br);
1789*22ce4affSfengbojiang 		txq->ift_br = NULL;
1790*22ce4affSfengbojiang 	}
1791*22ce4affSfengbojiang 
1792*22ce4affSfengbojiang 	mtx_destroy(&txq->ift_mtx);
1793*22ce4affSfengbojiang 
1794a9643ea8Slogwang 	if (txq->ift_sds.ifsd_map != NULL) {
1795a9643ea8Slogwang 		free(txq->ift_sds.ifsd_map, M_IFLIB);
1796a9643ea8Slogwang 		txq->ift_sds.ifsd_map = NULL;
1797a9643ea8Slogwang 	}
1798*22ce4affSfengbojiang 	if (txq->ift_sds.ifsd_tso_map != NULL) {
1799*22ce4affSfengbojiang 		free(txq->ift_sds.ifsd_tso_map, M_IFLIB);
1800*22ce4affSfengbojiang 		txq->ift_sds.ifsd_tso_map = NULL;
1801*22ce4affSfengbojiang 	}
1802a9643ea8Slogwang 	if (txq->ift_sds.ifsd_m != NULL) {
1803a9643ea8Slogwang 		free(txq->ift_sds.ifsd_m, M_IFLIB);
1804a9643ea8Slogwang 		txq->ift_sds.ifsd_m = NULL;
1805a9643ea8Slogwang 	}
1806*22ce4affSfengbojiang 	if (txq->ift_buf_tag != NULL) {
1807*22ce4affSfengbojiang 		bus_dma_tag_destroy(txq->ift_buf_tag);
1808*22ce4affSfengbojiang 		txq->ift_buf_tag = NULL;
1809a9643ea8Slogwang 	}
1810*22ce4affSfengbojiang 	if (txq->ift_tso_buf_tag != NULL) {
1811*22ce4affSfengbojiang 		bus_dma_tag_destroy(txq->ift_tso_buf_tag);
1812*22ce4affSfengbojiang 		txq->ift_tso_buf_tag = NULL;
1813a9643ea8Slogwang 	}
1814*22ce4affSfengbojiang 	if (txq->ift_ifdi != NULL) {
1815*22ce4affSfengbojiang 		free(txq->ift_ifdi, M_IFLIB);
1816a9643ea8Slogwang 	}
1817a9643ea8Slogwang }
1818a9643ea8Slogwang 
1819a9643ea8Slogwang static void
iflib_txsd_free(if_ctx_t ctx,iflib_txq_t txq,int i)1820a9643ea8Slogwang iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
1821a9643ea8Slogwang {
1822a9643ea8Slogwang 	struct mbuf **mp;
1823a9643ea8Slogwang 
1824a9643ea8Slogwang 	mp = &txq->ift_sds.ifsd_m[i];
1825a9643ea8Slogwang 	if (*mp == NULL)
1826a9643ea8Slogwang 		return;
1827a9643ea8Slogwang 
1828a9643ea8Slogwang 	if (txq->ift_sds.ifsd_map != NULL) {
1829*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_buf_tag,
1830*22ce4affSfengbojiang 		    txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE);
1831*22ce4affSfengbojiang 		bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]);
1832a9643ea8Slogwang 	}
1833*22ce4affSfengbojiang 	if (txq->ift_sds.ifsd_tso_map != NULL) {
1834*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_tso_buf_tag,
1835*22ce4affSfengbojiang 		    txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE);
1836*22ce4affSfengbojiang 		bus_dmamap_unload(txq->ift_tso_buf_tag,
1837*22ce4affSfengbojiang 		    txq->ift_sds.ifsd_tso_map[i]);
1838*22ce4affSfengbojiang 	}
1839*22ce4affSfengbojiang 	m_freem(*mp);
1840a9643ea8Slogwang 	DBG_COUNTER_INC(tx_frees);
1841a9643ea8Slogwang 	*mp = NULL;
1842a9643ea8Slogwang }
1843a9643ea8Slogwang 
1844a9643ea8Slogwang static int
iflib_txq_setup(iflib_txq_t txq)1845a9643ea8Slogwang iflib_txq_setup(iflib_txq_t txq)
1846a9643ea8Slogwang {
1847a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
1848a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1849*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
1850a9643ea8Slogwang 	iflib_dma_info_t di;
1851a9643ea8Slogwang 	int i;
1852a9643ea8Slogwang 
1853a9643ea8Slogwang 	/* Set number of descriptors available */
1854a9643ea8Slogwang 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
1855*22ce4affSfengbojiang 	/* XXX make configurable */
1856*22ce4affSfengbojiang 	txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
1857a9643ea8Slogwang 
1858a9643ea8Slogwang 	/* Reset indices */
1859*22ce4affSfengbojiang 	txq->ift_cidx_processed = 0;
1860*22ce4affSfengbojiang 	txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
1861a9643ea8Slogwang 	txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
1862a9643ea8Slogwang 
1863*22ce4affSfengbojiang 	for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
1864a9643ea8Slogwang 		bzero((void *)di->idi_vaddr, di->idi_size);
1865a9643ea8Slogwang 
1866a9643ea8Slogwang 	IFDI_TXQ_SETUP(ctx, txq->ift_id);
1867*22ce4affSfengbojiang 	for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
1868a9643ea8Slogwang 		bus_dmamap_sync(di->idi_tag, di->idi_map,
1869a9643ea8Slogwang 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1870a9643ea8Slogwang 	return (0);
1871a9643ea8Slogwang }
1872a9643ea8Slogwang 
1873a9643ea8Slogwang /*********************************************************************
1874a9643ea8Slogwang  *
1875*22ce4affSfengbojiang  *  Allocate DMA resources for RX buffers as well as memory for the RX
1876*22ce4affSfengbojiang  *  mbuf map, direct RX cluster pointer map and RX cluster bus address
1877*22ce4affSfengbojiang  *  map.  RX DMA map, RX mbuf map, direct RX cluster pointer map and
1878*22ce4affSfengbojiang  *  RX cluster map are kept in a iflib_sw_rx_desc_array structure.
1879*22ce4affSfengbojiang  *  Since we use use one entry in iflib_sw_rx_desc_array per received
1880*22ce4affSfengbojiang  *  packet, the maximum number of entries we'll need is equal to the
1881*22ce4affSfengbojiang  *  number of hardware receive descriptors that we've allocated.
1882a9643ea8Slogwang  *
1883a9643ea8Slogwang  **********************************************************************/
1884a9643ea8Slogwang static int
iflib_rxsd_alloc(iflib_rxq_t rxq)1885a9643ea8Slogwang iflib_rxsd_alloc(iflib_rxq_t rxq)
1886a9643ea8Slogwang {
1887a9643ea8Slogwang 	if_ctx_t ctx = rxq->ifr_ctx;
1888a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
1889a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1890a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
1891a9643ea8Slogwang 	iflib_fl_t fl;
1892a9643ea8Slogwang 	int			err;
1893a9643ea8Slogwang 
1894a9643ea8Slogwang 	MPASS(scctx->isc_nrxd[0] > 0);
1895a9643ea8Slogwang 	MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
1896a9643ea8Slogwang 
1897a9643ea8Slogwang 	fl = rxq->ifr_fl;
1898a9643ea8Slogwang 	for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
1899a9643ea8Slogwang 		fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
1900*22ce4affSfengbojiang 		/* Set up DMA tag for RX buffers. */
1901a9643ea8Slogwang 		err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
1902a9643ea8Slogwang 					 1, 0,			/* alignment, bounds */
1903a9643ea8Slogwang 					 BUS_SPACE_MAXADDR,	/* lowaddr */
1904a9643ea8Slogwang 					 BUS_SPACE_MAXADDR,	/* highaddr */
1905a9643ea8Slogwang 					 NULL, NULL,		/* filter, filterarg */
1906a9643ea8Slogwang 					 sctx->isc_rx_maxsize,	/* maxsize */
1907a9643ea8Slogwang 					 sctx->isc_rx_nsegments,	/* nsegments */
1908a9643ea8Slogwang 					 sctx->isc_rx_maxsegsize,	/* maxsegsize */
1909a9643ea8Slogwang 					 0,			/* flags */
1910a9643ea8Slogwang 					 NULL,			/* lockfunc */
1911a9643ea8Slogwang 					 NULL,			/* lockarg */
1912*22ce4affSfengbojiang 					 &fl->ifl_buf_tag);
1913a9643ea8Slogwang 		if (err) {
1914*22ce4affSfengbojiang 			device_printf(dev,
1915*22ce4affSfengbojiang 			    "Unable to allocate RX DMA tag: %d\n", err);
1916a9643ea8Slogwang 			goto fail;
1917a9643ea8Slogwang 		}
1918a9643ea8Slogwang 
1919*22ce4affSfengbojiang 		/* Allocate memory for the RX mbuf map. */
1920*22ce4affSfengbojiang 		if (!(fl->ifl_sds.ifsd_m =
1921*22ce4affSfengbojiang 		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
1922*22ce4affSfengbojiang 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1923*22ce4affSfengbojiang 			device_printf(dev,
1924*22ce4affSfengbojiang 			    "Unable to allocate RX mbuf map memory\n");
1925*22ce4affSfengbojiang 			err = ENOMEM;
1926*22ce4affSfengbojiang 			goto fail;
1927*22ce4affSfengbojiang 		}
1928*22ce4affSfengbojiang 
1929*22ce4affSfengbojiang 		/* Allocate memory for the direct RX cluster pointer map. */
1930*22ce4affSfengbojiang 		if (!(fl->ifl_sds.ifsd_cl =
1931*22ce4affSfengbojiang 		      (caddr_t *) malloc(sizeof(caddr_t) *
1932*22ce4affSfengbojiang 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1933*22ce4affSfengbojiang 			device_printf(dev,
1934*22ce4affSfengbojiang 			    "Unable to allocate RX cluster map memory\n");
1935*22ce4affSfengbojiang 			err = ENOMEM;
1936*22ce4affSfengbojiang 			goto fail;
1937*22ce4affSfengbojiang 		}
1938*22ce4affSfengbojiang 
1939*22ce4affSfengbojiang 		/* Allocate memory for the RX cluster bus address map. */
1940*22ce4affSfengbojiang 		if (!(fl->ifl_sds.ifsd_ba =
1941*22ce4affSfengbojiang 		      (bus_addr_t *) malloc(sizeof(bus_addr_t) *
1942*22ce4affSfengbojiang 					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1943*22ce4affSfengbojiang 			device_printf(dev,
1944*22ce4affSfengbojiang 			    "Unable to allocate RX bus address map memory\n");
1945*22ce4affSfengbojiang 			err = ENOMEM;
1946*22ce4affSfengbojiang 			goto fail;
1947*22ce4affSfengbojiang 		}
1948*22ce4affSfengbojiang 
1949*22ce4affSfengbojiang 		/*
1950*22ce4affSfengbojiang 		 * Create the DMA maps for RX buffers.
1951*22ce4affSfengbojiang 		 */
1952*22ce4affSfengbojiang 		if (!(fl->ifl_sds.ifsd_map =
1953*22ce4affSfengbojiang 		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1954*22ce4affSfengbojiang 			device_printf(dev,
1955*22ce4affSfengbojiang 			    "Unable to allocate RX buffer DMA map memory\n");
1956*22ce4affSfengbojiang 			err = ENOMEM;
1957*22ce4affSfengbojiang 			goto fail;
1958*22ce4affSfengbojiang 		}
1959*22ce4affSfengbojiang 		for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1960*22ce4affSfengbojiang 			err = bus_dmamap_create(fl->ifl_buf_tag, 0,
1961*22ce4affSfengbojiang 			    &fl->ifl_sds.ifsd_map[i]);
1962*22ce4affSfengbojiang 			if (err != 0) {
1963*22ce4affSfengbojiang 				device_printf(dev, "Unable to create RX buffer DMA map\n");
1964a9643ea8Slogwang 				goto fail;
1965a9643ea8Slogwang 			}
1966a9643ea8Slogwang 		}
1967a9643ea8Slogwang 	}
1968a9643ea8Slogwang 	return (0);
1969a9643ea8Slogwang 
1970a9643ea8Slogwang fail:
1971a9643ea8Slogwang 	iflib_rx_structures_free(ctx);
1972a9643ea8Slogwang 	return (err);
1973a9643ea8Slogwang }
1974a9643ea8Slogwang 
1975a9643ea8Slogwang /*
1976a9643ea8Slogwang  * Internal service routines
1977a9643ea8Slogwang  */
1978a9643ea8Slogwang 
1979a9643ea8Slogwang struct rxq_refill_cb_arg {
1980a9643ea8Slogwang 	int               error;
1981a9643ea8Slogwang 	bus_dma_segment_t seg;
1982a9643ea8Slogwang 	int               nseg;
1983a9643ea8Slogwang };
1984a9643ea8Slogwang 
1985a9643ea8Slogwang static void
_rxq_refill_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)1986a9643ea8Slogwang _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1987a9643ea8Slogwang {
1988a9643ea8Slogwang 	struct rxq_refill_cb_arg *cb_arg = arg;
1989a9643ea8Slogwang 
1990a9643ea8Slogwang 	cb_arg->error = error;
1991a9643ea8Slogwang 	cb_arg->seg = segs[0];
1992a9643ea8Slogwang 	cb_arg->nseg = nseg;
1993a9643ea8Slogwang }
1994a9643ea8Slogwang 
1995a9643ea8Slogwang /**
1996*22ce4affSfengbojiang  * iflib_fl_refill - refill an rxq free-buffer list
1997a9643ea8Slogwang  * @ctx: the iflib context
1998*22ce4affSfengbojiang  * @fl: the free list to refill
1999*22ce4affSfengbojiang  * @count: the number of new buffers to allocate
2000a9643ea8Slogwang  *
2001*22ce4affSfengbojiang  * (Re)populate an rxq free-buffer list with up to @count new packet buffers.
2002*22ce4affSfengbojiang  * The caller must assure that @count does not exceed the queue's capacity
2003*22ce4affSfengbojiang  * minus one (since we always leave a descriptor unavailable).
2004a9643ea8Slogwang  */
2005*22ce4affSfengbojiang static uint8_t
iflib_fl_refill(if_ctx_t ctx,iflib_fl_t fl,int count)2006*22ce4affSfengbojiang iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
2007a9643ea8Slogwang {
2008*22ce4affSfengbojiang 	struct if_rxd_update iru;
2009*22ce4affSfengbojiang 	struct rxq_refill_cb_arg cb_arg;
2010a9643ea8Slogwang 	struct mbuf *m;
2011*22ce4affSfengbojiang 	caddr_t cl, *sd_cl;
2012*22ce4affSfengbojiang 	struct mbuf **sd_m;
2013*22ce4affSfengbojiang 	bus_dmamap_t *sd_map;
2014*22ce4affSfengbojiang 	bus_addr_t bus_addr, *sd_ba;
2015*22ce4affSfengbojiang 	int err, frag_idx, i, idx, n, pidx;
2016*22ce4affSfengbojiang 	qidx_t credits;
2017a9643ea8Slogwang 
2018*22ce4affSfengbojiang 	MPASS(count <= fl->ifl_size - fl->ifl_credits - 1);
2019*22ce4affSfengbojiang 
2020*22ce4affSfengbojiang 	sd_m = fl->ifl_sds.ifsd_m;
2021*22ce4affSfengbojiang 	sd_map = fl->ifl_sds.ifsd_map;
2022*22ce4affSfengbojiang 	sd_cl = fl->ifl_sds.ifsd_cl;
2023*22ce4affSfengbojiang 	sd_ba = fl->ifl_sds.ifsd_ba;
2024*22ce4affSfengbojiang 	pidx = fl->ifl_pidx;
2025*22ce4affSfengbojiang 	idx = pidx;
2026*22ce4affSfengbojiang 	frag_idx = fl->ifl_fragidx;
2027*22ce4affSfengbojiang 	credits = fl->ifl_credits;
2028*22ce4affSfengbojiang 
2029*22ce4affSfengbojiang 	i = 0;
2030a9643ea8Slogwang 	n = count;
2031a9643ea8Slogwang 	MPASS(n > 0);
2032*22ce4affSfengbojiang 	MPASS(credits + n <= fl->ifl_size);
2033a9643ea8Slogwang 
2034a9643ea8Slogwang 	if (pidx < fl->ifl_cidx)
2035a9643ea8Slogwang 		MPASS(pidx + n <= fl->ifl_cidx);
2036*22ce4affSfengbojiang 	if (pidx == fl->ifl_cidx && (credits < fl->ifl_size))
2037a9643ea8Slogwang 		MPASS(fl->ifl_gen == 0);
2038a9643ea8Slogwang 	if (pidx > fl->ifl_cidx)
2039a9643ea8Slogwang 		MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
2040a9643ea8Slogwang 
2041a9643ea8Slogwang 	DBG_COUNTER_INC(fl_refills);
2042a9643ea8Slogwang 	if (n > 8)
2043a9643ea8Slogwang 		DBG_COUNTER_INC(fl_refills_large);
2044*22ce4affSfengbojiang 	iru_init(&iru, fl->ifl_rxq, fl->ifl_id);
2045*22ce4affSfengbojiang 	while (n-- > 0) {
2046a9643ea8Slogwang 		/*
2047a9643ea8Slogwang 		 * We allocate an uninitialized mbuf + cluster, mbuf is
2048a9643ea8Slogwang 		 * initialized after rx.
2049a9643ea8Slogwang 		 *
2050*22ce4affSfengbojiang 		 * If the cluster is still set then we know a minimum sized
2051*22ce4affSfengbojiang 		 * packet was received
2052a9643ea8Slogwang 		 */
2053*22ce4affSfengbojiang 		bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,
2054*22ce4affSfengbojiang 		    &frag_idx);
2055*22ce4affSfengbojiang 		if (frag_idx < 0)
2056*22ce4affSfengbojiang 			bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
2057*22ce4affSfengbojiang 		MPASS(frag_idx >= 0);
2058*22ce4affSfengbojiang 		if ((cl = sd_cl[frag_idx]) == NULL) {
2059*22ce4affSfengbojiang 			cl = uma_zalloc(fl->ifl_zone, M_NOWAIT);
2060*22ce4affSfengbojiang 			if (__predict_false(cl == NULL))
2061a9643ea8Slogwang 				break;
2062*22ce4affSfengbojiang 
2063*22ce4affSfengbojiang 			cb_arg.error = 0;
2064*22ce4affSfengbojiang 			MPASS(sd_map != NULL);
2065*22ce4affSfengbojiang 			err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx],
2066*22ce4affSfengbojiang 			    cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg,
2067*22ce4affSfengbojiang 			    BUS_DMA_NOWAIT);
2068*22ce4affSfengbojiang 			if (__predict_false(err != 0 || cb_arg.error)) {
2069*22ce4affSfengbojiang 				uma_zfree(fl->ifl_zone, cl);
2070*22ce4affSfengbojiang 				break;
2071*22ce4affSfengbojiang 			}
2072*22ce4affSfengbojiang 
2073*22ce4affSfengbojiang 			sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr;
2074*22ce4affSfengbojiang 			sd_cl[frag_idx] = cl;
2075a9643ea8Slogwang #if MEMORY_LOGGING
2076a9643ea8Slogwang 			fl->ifl_cl_enqueued++;
2077a9643ea8Slogwang #endif
2078*22ce4affSfengbojiang 		} else {
2079*22ce4affSfengbojiang 			bus_addr = sd_ba[frag_idx];
2080a9643ea8Slogwang 		}
2081*22ce4affSfengbojiang 		bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx],
2082*22ce4affSfengbojiang 		    BUS_DMASYNC_PREREAD);
2083*22ce4affSfengbojiang 
2084*22ce4affSfengbojiang 		if (sd_m[frag_idx] == NULL) {
2085*22ce4affSfengbojiang 			m = m_gethdr(M_NOWAIT, MT_NOINIT);
2086*22ce4affSfengbojiang 			if (__predict_false(m == NULL))
2087a9643ea8Slogwang 				break;
2088*22ce4affSfengbojiang 			sd_m[frag_idx] = m;
2089a9643ea8Slogwang 		}
2090*22ce4affSfengbojiang 		bit_set(fl->ifl_rx_bitmap, frag_idx);
2091a9643ea8Slogwang #if MEMORY_LOGGING
2092a9643ea8Slogwang 		fl->ifl_m_enqueued++;
2093a9643ea8Slogwang #endif
2094a9643ea8Slogwang 
2095a9643ea8Slogwang 		DBG_COUNTER_INC(rx_allocs);
2096*22ce4affSfengbojiang 		fl->ifl_rxd_idxs[i] = frag_idx;
2097a9643ea8Slogwang 		fl->ifl_bus_addrs[i] = bus_addr;
2098*22ce4affSfengbojiang 		credits++;
2099a9643ea8Slogwang 		i++;
2100*22ce4affSfengbojiang 		MPASS(credits <= fl->ifl_size);
2101*22ce4affSfengbojiang 		if (++idx == fl->ifl_size) {
2102*22ce4affSfengbojiang #ifdef INVARIANTS
2103a9643ea8Slogwang 			fl->ifl_gen = 1;
2104*22ce4affSfengbojiang #endif
2105*22ce4affSfengbojiang 			idx = 0;
2106a9643ea8Slogwang 		}
2107a9643ea8Slogwang 		if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
2108*22ce4affSfengbojiang 			iru.iru_pidx = pidx;
2109*22ce4affSfengbojiang 			iru.iru_count = i;
2110*22ce4affSfengbojiang 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
2111*22ce4affSfengbojiang 			fl->ifl_pidx = idx;
2112*22ce4affSfengbojiang 			fl->ifl_credits = credits;
2113*22ce4affSfengbojiang 			pidx = idx;
2114a9643ea8Slogwang 			i = 0;
2115a9643ea8Slogwang 		}
2116a9643ea8Slogwang 	}
2117a9643ea8Slogwang 
2118*22ce4affSfengbojiang 	if (n < count - 1) {
2119*22ce4affSfengbojiang 		if (i != 0) {
2120*22ce4affSfengbojiang 			iru.iru_pidx = pidx;
2121*22ce4affSfengbojiang 			iru.iru_count = i;
2122*22ce4affSfengbojiang 			ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
2123*22ce4affSfengbojiang 			fl->ifl_pidx = idx;
2124*22ce4affSfengbojiang 			fl->ifl_credits = credits;
2125*22ce4affSfengbojiang 		}
2126*22ce4affSfengbojiang 		DBG_COUNTER_INC(rxd_flush);
2127*22ce4affSfengbojiang 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
2128*22ce4affSfengbojiang 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2129*22ce4affSfengbojiang 		ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id,
2130*22ce4affSfengbojiang 		    fl->ifl_id, fl->ifl_pidx);
2131*22ce4affSfengbojiang 		if (__predict_true(bit_test(fl->ifl_rx_bitmap, frag_idx))) {
2132*22ce4affSfengbojiang 			fl->ifl_fragidx = frag_idx + 1;
2133*22ce4affSfengbojiang 			if (fl->ifl_fragidx == fl->ifl_size)
2134*22ce4affSfengbojiang 				fl->ifl_fragidx = 0;
2135*22ce4affSfengbojiang 		} else {
2136*22ce4affSfengbojiang 			fl->ifl_fragidx = frag_idx;
2137*22ce4affSfengbojiang 		}
2138*22ce4affSfengbojiang 	}
2139*22ce4affSfengbojiang 
2140*22ce4affSfengbojiang 	return (n == -1 ? 0 : IFLIB_RXEOF_EMPTY);
2141*22ce4affSfengbojiang }
2142*22ce4affSfengbojiang 
2143*22ce4affSfengbojiang static inline uint8_t
iflib_fl_refill_all(if_ctx_t ctx,iflib_fl_t fl)2144*22ce4affSfengbojiang iflib_fl_refill_all(if_ctx_t ctx, iflib_fl_t fl)
2145a9643ea8Slogwang {
2146*22ce4affSfengbojiang 	/*
2147*22ce4affSfengbojiang 	 * We leave an unused descriptor to avoid pidx to catch up with cidx.
2148*22ce4affSfengbojiang 	 * This is important as it confuses most NICs. For instance,
2149*22ce4affSfengbojiang 	 * Intel NICs have (per receive ring) RDH and RDT registers, where
2150*22ce4affSfengbojiang 	 * RDH points to the next receive descriptor to be used by the NIC,
2151*22ce4affSfengbojiang 	 * and RDT for the next receive descriptor to be published by the
2152*22ce4affSfengbojiang 	 * driver to the NIC (RDT - 1 is thus the last valid one).
2153*22ce4affSfengbojiang 	 * The condition RDH == RDT means no descriptors are available to
2154*22ce4affSfengbojiang 	 * the NIC, and thus it would be ambiguous if it also meant that
2155*22ce4affSfengbojiang 	 * all the descriptors are available to the NIC.
2156*22ce4affSfengbojiang 	 */
2157a9643ea8Slogwang 	int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
2158a9643ea8Slogwang #ifdef INVARIANTS
2159a9643ea8Slogwang 	int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
2160a9643ea8Slogwang #endif
2161a9643ea8Slogwang 
2162a9643ea8Slogwang 	MPASS(fl->ifl_credits <= fl->ifl_size);
2163a9643ea8Slogwang 	MPASS(reclaimable == delta);
2164a9643ea8Slogwang 
2165a9643ea8Slogwang 	if (reclaimable > 0)
2166*22ce4affSfengbojiang 		return (iflib_fl_refill(ctx, fl, reclaimable));
2167*22ce4affSfengbojiang 	return (0);
2168*22ce4affSfengbojiang }
2169*22ce4affSfengbojiang 
2170*22ce4affSfengbojiang uint8_t
iflib_in_detach(if_ctx_t ctx)2171*22ce4affSfengbojiang iflib_in_detach(if_ctx_t ctx)
2172*22ce4affSfengbojiang {
2173*22ce4affSfengbojiang 	bool in_detach;
2174*22ce4affSfengbojiang 
2175*22ce4affSfengbojiang 	STATE_LOCK(ctx);
2176*22ce4affSfengbojiang 	in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH);
2177*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
2178*22ce4affSfengbojiang 	return (in_detach);
2179a9643ea8Slogwang }
2180a9643ea8Slogwang 
2181a9643ea8Slogwang static void
iflib_fl_bufs_free(iflib_fl_t fl)2182a9643ea8Slogwang iflib_fl_bufs_free(iflib_fl_t fl)
2183a9643ea8Slogwang {
2184a9643ea8Slogwang 	iflib_dma_info_t idi = fl->ifl_ifdi;
2185*22ce4affSfengbojiang 	bus_dmamap_t sd_map;
2186a9643ea8Slogwang 	uint32_t i;
2187a9643ea8Slogwang 
2188a9643ea8Slogwang 	for (i = 0; i < fl->ifl_size; i++) {
2189*22ce4affSfengbojiang 		struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
2190*22ce4affSfengbojiang 		caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
2191a9643ea8Slogwang 
2192*22ce4affSfengbojiang 		if (*sd_cl != NULL) {
2193*22ce4affSfengbojiang 			sd_map = fl->ifl_sds.ifsd_map[i];
2194*22ce4affSfengbojiang 			bus_dmamap_sync(fl->ifl_buf_tag, sd_map,
2195*22ce4affSfengbojiang 			    BUS_DMASYNC_POSTREAD);
2196*22ce4affSfengbojiang 			bus_dmamap_unload(fl->ifl_buf_tag, sd_map);
2197*22ce4affSfengbojiang 			uma_zfree(fl->ifl_zone, *sd_cl);
2198*22ce4affSfengbojiang 			*sd_cl = NULL;
2199*22ce4affSfengbojiang 			if (*sd_m != NULL) {
2200*22ce4affSfengbojiang 				m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
2201*22ce4affSfengbojiang 				uma_zfree(zone_mbuf, *sd_m);
2202*22ce4affSfengbojiang 				*sd_m = NULL;
2203a9643ea8Slogwang 			}
2204a9643ea8Slogwang 		} else {
2205*22ce4affSfengbojiang 			MPASS(*sd_m == NULL);
2206a9643ea8Slogwang 		}
2207a9643ea8Slogwang #if MEMORY_LOGGING
2208a9643ea8Slogwang 		fl->ifl_m_dequeued++;
2209a9643ea8Slogwang 		fl->ifl_cl_dequeued++;
2210a9643ea8Slogwang #endif
2211a9643ea8Slogwang 	}
2212*22ce4affSfengbojiang #ifdef INVARIANTS
2213*22ce4affSfengbojiang 	for (i = 0; i < fl->ifl_size; i++) {
2214*22ce4affSfengbojiang 		MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
2215*22ce4affSfengbojiang 		MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
2216*22ce4affSfengbojiang 	}
2217*22ce4affSfengbojiang #endif
2218a9643ea8Slogwang 	/*
2219a9643ea8Slogwang 	 * Reset free list values
2220a9643ea8Slogwang 	 */
2221*22ce4affSfengbojiang 	fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
2222a9643ea8Slogwang 	bzero(idi->idi_vaddr, idi->idi_size);
2223a9643ea8Slogwang }
2224a9643ea8Slogwang 
2225a9643ea8Slogwang /*********************************************************************
2226a9643ea8Slogwang  *
2227*22ce4affSfengbojiang  *  Initialize a free list and its buffers.
2228a9643ea8Slogwang  *
2229a9643ea8Slogwang  **********************************************************************/
2230a9643ea8Slogwang static int
iflib_fl_setup(iflib_fl_t fl)2231a9643ea8Slogwang iflib_fl_setup(iflib_fl_t fl)
2232a9643ea8Slogwang {
2233a9643ea8Slogwang 	iflib_rxq_t rxq = fl->ifl_rxq;
2234a9643ea8Slogwang 	if_ctx_t ctx = rxq->ifr_ctx;
2235*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2236*22ce4affSfengbojiang 	int qidx;
2237a9643ea8Slogwang 
2238*22ce4affSfengbojiang 	bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1);
2239a9643ea8Slogwang 	/*
2240a9643ea8Slogwang 	** Free current RX buffer structs and their mbufs
2241a9643ea8Slogwang 	*/
2242a9643ea8Slogwang 	iflib_fl_bufs_free(fl);
2243a9643ea8Slogwang 	/* Now replenish the mbufs */
2244a9643ea8Slogwang 	MPASS(fl->ifl_credits == 0);
2245*22ce4affSfengbojiang 	qidx = rxq->ifr_fl_offset + fl->ifl_id;
2246*22ce4affSfengbojiang 	if (scctx->isc_rxd_buf_size[qidx] != 0)
2247*22ce4affSfengbojiang 		fl->ifl_buf_size = scctx->isc_rxd_buf_size[qidx];
2248a9643ea8Slogwang 	else
2249*22ce4affSfengbojiang 		fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz;
2250*22ce4affSfengbojiang 	/*
2251*22ce4affSfengbojiang 	 * ifl_buf_size may be a driver-supplied value, so pull it up
2252*22ce4affSfengbojiang 	 * to the selected mbuf size.
2253*22ce4affSfengbojiang 	 */
2254*22ce4affSfengbojiang 	fl->ifl_buf_size = iflib_get_mbuf_size_for(fl->ifl_buf_size);
2255a9643ea8Slogwang 	if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
2256a9643ea8Slogwang 		ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
2257a9643ea8Slogwang 	fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
2258a9643ea8Slogwang 	fl->ifl_zone = m_getzone(fl->ifl_buf_size);
2259a9643ea8Slogwang 
2260*22ce4affSfengbojiang 	/*
2261*22ce4affSfengbojiang 	 * Avoid pre-allocating zillions of clusters to an idle card
2262*22ce4affSfengbojiang 	 * potentially speeding up attach. In any case make sure
2263*22ce4affSfengbojiang 	 * to leave a descriptor unavailable. See the comment in
2264*22ce4affSfengbojiang 	 * iflib_fl_refill_all().
2265a9643ea8Slogwang 	 */
2266*22ce4affSfengbojiang 	MPASS(fl->ifl_size > 0);
2267*22ce4affSfengbojiang 	(void)iflib_fl_refill(ctx, fl, min(128, fl->ifl_size - 1));
2268*22ce4affSfengbojiang 	if (min(128, fl->ifl_size - 1) != fl->ifl_credits)
2269a9643ea8Slogwang 		return (ENOBUFS);
2270a9643ea8Slogwang 	/*
2271a9643ea8Slogwang 	 * handle failure
2272a9643ea8Slogwang 	 */
2273a9643ea8Slogwang 	MPASS(rxq != NULL);
2274a9643ea8Slogwang 	MPASS(fl->ifl_ifdi != NULL);
2275a9643ea8Slogwang 	bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
2276a9643ea8Slogwang 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2277a9643ea8Slogwang 	return (0);
2278a9643ea8Slogwang }
2279a9643ea8Slogwang 
2280a9643ea8Slogwang /*********************************************************************
2281a9643ea8Slogwang  *
2282a9643ea8Slogwang  *  Free receive ring data structures
2283a9643ea8Slogwang  *
2284a9643ea8Slogwang  **********************************************************************/
2285a9643ea8Slogwang static void
iflib_rx_sds_free(iflib_rxq_t rxq)2286a9643ea8Slogwang iflib_rx_sds_free(iflib_rxq_t rxq)
2287a9643ea8Slogwang {
2288a9643ea8Slogwang 	iflib_fl_t fl;
2289*22ce4affSfengbojiang 	int i, j;
2290a9643ea8Slogwang 
2291a9643ea8Slogwang 	if (rxq->ifr_fl != NULL) {
2292a9643ea8Slogwang 		for (i = 0; i < rxq->ifr_nfl; i++) {
2293a9643ea8Slogwang 			fl = &rxq->ifr_fl[i];
2294*22ce4affSfengbojiang 			if (fl->ifl_buf_tag != NULL) {
2295*22ce4affSfengbojiang 				if (fl->ifl_sds.ifsd_map != NULL) {
2296*22ce4affSfengbojiang 					for (j = 0; j < fl->ifl_size; j++) {
2297*22ce4affSfengbojiang 						bus_dmamap_sync(
2298*22ce4affSfengbojiang 						    fl->ifl_buf_tag,
2299*22ce4affSfengbojiang 						    fl->ifl_sds.ifsd_map[j],
2300*22ce4affSfengbojiang 						    BUS_DMASYNC_POSTREAD);
2301*22ce4affSfengbojiang 						bus_dmamap_unload(
2302*22ce4affSfengbojiang 						    fl->ifl_buf_tag,
2303*22ce4affSfengbojiang 						    fl->ifl_sds.ifsd_map[j]);
2304*22ce4affSfengbojiang 						bus_dmamap_destroy(
2305*22ce4affSfengbojiang 						    fl->ifl_buf_tag,
2306*22ce4affSfengbojiang 						    fl->ifl_sds.ifsd_map[j]);
2307a9643ea8Slogwang 					}
2308a9643ea8Slogwang 				}
2309*22ce4affSfengbojiang 				bus_dma_tag_destroy(fl->ifl_buf_tag);
2310*22ce4affSfengbojiang 				fl->ifl_buf_tag = NULL;
2311*22ce4affSfengbojiang 			}
2312*22ce4affSfengbojiang 			free(fl->ifl_sds.ifsd_m, M_IFLIB);
2313*22ce4affSfengbojiang 			free(fl->ifl_sds.ifsd_cl, M_IFLIB);
2314*22ce4affSfengbojiang 			free(fl->ifl_sds.ifsd_ba, M_IFLIB);
2315*22ce4affSfengbojiang 			free(fl->ifl_sds.ifsd_map, M_IFLIB);
2316*22ce4affSfengbojiang 			free(fl->ifl_rx_bitmap, M_IFLIB);
2317*22ce4affSfengbojiang 			fl->ifl_sds.ifsd_m = NULL;
2318*22ce4affSfengbojiang 			fl->ifl_sds.ifsd_cl = NULL;
2319*22ce4affSfengbojiang 			fl->ifl_sds.ifsd_ba = NULL;
2320*22ce4affSfengbojiang 			fl->ifl_sds.ifsd_map = NULL;
2321*22ce4affSfengbojiang 			fl->ifl_rx_bitmap = NULL;
2322*22ce4affSfengbojiang 		}
2323a9643ea8Slogwang 		free(rxq->ifr_fl, M_IFLIB);
2324a9643ea8Slogwang 		rxq->ifr_fl = NULL;
2325*22ce4affSfengbojiang 		free(rxq->ifr_ifdi, M_IFLIB);
2326*22ce4affSfengbojiang 		rxq->ifr_ifdi = NULL;
2327*22ce4affSfengbojiang 		rxq->ifr_cq_cidx = 0;
2328a9643ea8Slogwang 	}
2329a9643ea8Slogwang }
2330a9643ea8Slogwang 
2331a9643ea8Slogwang /*
2332*22ce4affSfengbojiang  * Timer routine
2333a9643ea8Slogwang  */
2334a9643ea8Slogwang static void
iflib_timer(void * arg)2335a9643ea8Slogwang iflib_timer(void *arg)
2336a9643ea8Slogwang {
2337a9643ea8Slogwang 	iflib_txq_t txq = arg;
2338a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
2339*22ce4affSfengbojiang 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
2340*22ce4affSfengbojiang 	uint64_t this_tick = ticks;
2341a9643ea8Slogwang 
2342a9643ea8Slogwang 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
2343a9643ea8Slogwang 		return;
2344*22ce4affSfengbojiang 
2345a9643ea8Slogwang 	/*
2346a9643ea8Slogwang 	** Check on the state of the TX queue(s), this
2347a9643ea8Slogwang 	** can be done without the lock because its RO
2348a9643ea8Slogwang 	** and the HUNG state will be static if set.
2349a9643ea8Slogwang 	*/
2350*22ce4affSfengbojiang 	if (this_tick - txq->ift_last_timer_tick >= iflib_timer_default) {
2351*22ce4affSfengbojiang 		txq->ift_last_timer_tick = this_tick;
2352a9643ea8Slogwang 		IFDI_TIMER(ctx, txq->ift_id);
2353a9643ea8Slogwang 		if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
2354*22ce4affSfengbojiang 		    ((txq->ift_cleaned_prev == txq->ift_cleaned) ||
2355*22ce4affSfengbojiang 		     (sctx->isc_pause_frames == 0)))
2356a9643ea8Slogwang 			goto hung;
2357a9643ea8Slogwang 
2358*22ce4affSfengbojiang 		if (txq->ift_qstatus != IFLIB_QUEUE_IDLE &&
2359*22ce4affSfengbojiang 		    ifmp_ring_is_stalled(txq->ift_br)) {
2360*22ce4affSfengbojiang 			KASSERT(ctx->ifc_link_state == LINK_STATE_UP,
2361*22ce4affSfengbojiang 			    ("queue can't be marked as hung if interface is down"));
2362*22ce4affSfengbojiang 			txq->ift_qstatus = IFLIB_QUEUE_HUNG;
2363*22ce4affSfengbojiang 		}
2364*22ce4affSfengbojiang 		txq->ift_cleaned_prev = txq->ift_cleaned;
2365*22ce4affSfengbojiang 	}
2366*22ce4affSfengbojiang 	/* handle any laggards */
2367*22ce4affSfengbojiang 	if (txq->ift_db_pending)
2368a9643ea8Slogwang 		GROUPTASK_ENQUEUE(&txq->ift_task);
2369a9643ea8Slogwang 
2370*22ce4affSfengbojiang 	sctx->isc_pause_frames = 0;
2371a9643ea8Slogwang 	if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
2372*22ce4affSfengbojiang 		callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer,
2373*22ce4affSfengbojiang 		    txq, txq->ift_timer.c_cpu);
2374a9643ea8Slogwang 	return;
2375*22ce4affSfengbojiang 
2376a9643ea8Slogwang  hung:
2377*22ce4affSfengbojiang 	device_printf(ctx->ifc_dev,
2378*22ce4affSfengbojiang 	    "Watchdog timeout (TX: %d desc avail: %d pidx: %d) -- resetting\n",
2379a9643ea8Slogwang 	    txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
2380*22ce4affSfengbojiang 	STATE_LOCK(ctx);
2381*22ce4affSfengbojiang 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
2382*22ce4affSfengbojiang 	ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET);
2383*22ce4affSfengbojiang 	iflib_admin_intr_deferred(ctx);
2384*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
2385*22ce4affSfengbojiang }
2386a9643ea8Slogwang 
2387*22ce4affSfengbojiang static uint16_t
iflib_get_mbuf_size_for(unsigned int size)2388*22ce4affSfengbojiang iflib_get_mbuf_size_for(unsigned int size)
2389*22ce4affSfengbojiang {
2390a9643ea8Slogwang 
2391*22ce4affSfengbojiang 	if (size <= MCLBYTES)
2392*22ce4affSfengbojiang 		return (MCLBYTES);
2393*22ce4affSfengbojiang 	else
2394*22ce4affSfengbojiang 		return (MJUMPAGESIZE);
2395*22ce4affSfengbojiang }
2396*22ce4affSfengbojiang 
2397*22ce4affSfengbojiang static void
iflib_calc_rx_mbuf_sz(if_ctx_t ctx)2398*22ce4affSfengbojiang iflib_calc_rx_mbuf_sz(if_ctx_t ctx)
2399*22ce4affSfengbojiang {
2400*22ce4affSfengbojiang 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
2401*22ce4affSfengbojiang 
2402*22ce4affSfengbojiang 	/*
2403*22ce4affSfengbojiang 	 * XXX don't set the max_frame_size to larger
2404*22ce4affSfengbojiang 	 * than the hardware can handle
2405*22ce4affSfengbojiang 	 */
2406*22ce4affSfengbojiang 	ctx->ifc_rx_mbuf_sz =
2407*22ce4affSfengbojiang 	    iflib_get_mbuf_size_for(sctx->isc_max_frame_size);
2408*22ce4affSfengbojiang }
2409*22ce4affSfengbojiang 
2410*22ce4affSfengbojiang uint32_t
iflib_get_rx_mbuf_sz(if_ctx_t ctx)2411*22ce4affSfengbojiang iflib_get_rx_mbuf_sz(if_ctx_t ctx)
2412*22ce4affSfengbojiang {
2413*22ce4affSfengbojiang 
2414*22ce4affSfengbojiang 	return (ctx->ifc_rx_mbuf_sz);
2415a9643ea8Slogwang }
2416a9643ea8Slogwang 
2417a9643ea8Slogwang static void
iflib_init_locked(if_ctx_t ctx)2418a9643ea8Slogwang iflib_init_locked(if_ctx_t ctx)
2419a9643ea8Slogwang {
2420a9643ea8Slogwang 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
2421*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2422a9643ea8Slogwang 	if_t ifp = ctx->ifc_ifp;
2423a9643ea8Slogwang 	iflib_fl_t fl;
2424a9643ea8Slogwang 	iflib_txq_t txq;
2425a9643ea8Slogwang 	iflib_rxq_t rxq;
2426*22ce4affSfengbojiang 	int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
2427a9643ea8Slogwang 
2428a9643ea8Slogwang 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
2429a9643ea8Slogwang 	IFDI_INTR_DISABLE(ctx);
2430a9643ea8Slogwang 
2431*22ce4affSfengbojiang 	/*
2432*22ce4affSfengbojiang 	 * See iflib_stop(). Useful in case iflib_init_locked() is
2433*22ce4affSfengbojiang 	 * called without first calling iflib_stop().
2434*22ce4affSfengbojiang 	 */
2435*22ce4affSfengbojiang 	netmap_disable_all_rings(ifp);
2436*22ce4affSfengbojiang 
2437*22ce4affSfengbojiang 	tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
2438*22ce4affSfengbojiang 	tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
2439a9643ea8Slogwang 	/* Set hardware offload abilities */
2440a9643ea8Slogwang 	if_clearhwassist(ifp);
2441a9643ea8Slogwang 	if (if_getcapenable(ifp) & IFCAP_TXCSUM)
2442*22ce4affSfengbojiang 		if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
2443a9643ea8Slogwang 	if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
2444*22ce4affSfengbojiang 		if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
2445a9643ea8Slogwang 	if (if_getcapenable(ifp) & IFCAP_TSO4)
2446a9643ea8Slogwang 		if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
2447a9643ea8Slogwang 	if (if_getcapenable(ifp) & IFCAP_TSO6)
2448a9643ea8Slogwang 		if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
2449a9643ea8Slogwang 
2450a9643ea8Slogwang 	for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
2451a9643ea8Slogwang 		CALLOUT_LOCK(txq);
2452a9643ea8Slogwang 		callout_stop(&txq->ift_timer);
2453*22ce4affSfengbojiang #ifdef DEV_NETMAP
2454*22ce4affSfengbojiang 		callout_stop(&txq->ift_netmap_timer);
2455*22ce4affSfengbojiang #endif /* DEV_NETMAP */
2456a9643ea8Slogwang 		CALLOUT_UNLOCK(txq);
2457a9643ea8Slogwang 		iflib_netmap_txq_init(ctx, txq);
2458a9643ea8Slogwang 	}
2459*22ce4affSfengbojiang 
2460*22ce4affSfengbojiang 	/*
2461*22ce4affSfengbojiang 	 * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so
2462*22ce4affSfengbojiang 	 * that drivers can use the value when setting up the hardware receive
2463*22ce4affSfengbojiang 	 * buffers.
2464*22ce4affSfengbojiang 	 */
2465*22ce4affSfengbojiang 	iflib_calc_rx_mbuf_sz(ctx);
2466*22ce4affSfengbojiang 
2467a9643ea8Slogwang #ifdef INVARIANTS
2468a9643ea8Slogwang 	i = if_getdrvflags(ifp);
2469a9643ea8Slogwang #endif
2470a9643ea8Slogwang 	IFDI_INIT(ctx);
2471a9643ea8Slogwang 	MPASS(if_getdrvflags(ifp) == i);
2472a9643ea8Slogwang 	for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
2473*22ce4affSfengbojiang 		if (iflib_netmap_rxq_init(ctx, rxq) > 0) {
2474*22ce4affSfengbojiang 			/* This rxq is in netmap mode. Skip normal init. */
2475*22ce4affSfengbojiang 			continue;
2476*22ce4affSfengbojiang 		}
2477a9643ea8Slogwang 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
2478a9643ea8Slogwang 			if (iflib_fl_setup(fl)) {
2479*22ce4affSfengbojiang 				device_printf(ctx->ifc_dev,
2480*22ce4affSfengbojiang 				    "setting up free list %d failed - "
2481*22ce4affSfengbojiang 				    "check cluster settings\n", j);
2482a9643ea8Slogwang 				goto done;
2483a9643ea8Slogwang 			}
2484a9643ea8Slogwang 		}
2485a9643ea8Slogwang 	}
2486a9643ea8Slogwang done:
2487a9643ea8Slogwang 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
2488a9643ea8Slogwang 	IFDI_INTR_ENABLE(ctx);
2489a9643ea8Slogwang 	txq = ctx->ifc_txqs;
2490a9643ea8Slogwang 	for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
2491*22ce4affSfengbojiang 		callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq,
2492a9643ea8Slogwang 			txq->ift_timer.c_cpu);
2493*22ce4affSfengbojiang 
2494*22ce4affSfengbojiang         /* Re-enable txsync/rxsync. */
2495*22ce4affSfengbojiang 	netmap_enable_all_rings(ifp);
2496a9643ea8Slogwang }
2497a9643ea8Slogwang 
2498a9643ea8Slogwang static int
iflib_media_change(if_t ifp)2499a9643ea8Slogwang iflib_media_change(if_t ifp)
2500a9643ea8Slogwang {
2501a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
2502a9643ea8Slogwang 	int err;
2503a9643ea8Slogwang 
2504a9643ea8Slogwang 	CTX_LOCK(ctx);
2505a9643ea8Slogwang 	if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
2506*22ce4affSfengbojiang 		iflib_if_init_locked(ctx);
2507a9643ea8Slogwang 	CTX_UNLOCK(ctx);
2508a9643ea8Slogwang 	return (err);
2509a9643ea8Slogwang }
2510a9643ea8Slogwang 
2511a9643ea8Slogwang static void
iflib_media_status(if_t ifp,struct ifmediareq * ifmr)2512a9643ea8Slogwang iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
2513a9643ea8Slogwang {
2514a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
2515a9643ea8Slogwang 
2516a9643ea8Slogwang 	CTX_LOCK(ctx);
2517a9643ea8Slogwang 	IFDI_UPDATE_ADMIN_STATUS(ctx);
2518a9643ea8Slogwang 	IFDI_MEDIA_STATUS(ctx, ifmr);
2519a9643ea8Slogwang 	CTX_UNLOCK(ctx);
2520a9643ea8Slogwang }
2521a9643ea8Slogwang 
2522*22ce4affSfengbojiang void
iflib_stop(if_ctx_t ctx)2523a9643ea8Slogwang iflib_stop(if_ctx_t ctx)
2524a9643ea8Slogwang {
2525a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
2526a9643ea8Slogwang 	iflib_rxq_t rxq = ctx->ifc_rxqs;
2527a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2528*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
2529a9643ea8Slogwang 	iflib_dma_info_t di;
2530a9643ea8Slogwang 	iflib_fl_t fl;
2531a9643ea8Slogwang 	int i, j;
2532a9643ea8Slogwang 
2533a9643ea8Slogwang 	/* Tell the stack that the interface is no longer active */
2534a9643ea8Slogwang 	if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
2535a9643ea8Slogwang 
2536a9643ea8Slogwang 	IFDI_INTR_DISABLE(ctx);
2537*22ce4affSfengbojiang 	DELAY(1000);
2538*22ce4affSfengbojiang 	IFDI_STOP(ctx);
2539*22ce4affSfengbojiang 	DELAY(1000);
2540a9643ea8Slogwang 
2541*22ce4affSfengbojiang 	/*
2542*22ce4affSfengbojiang 	 * Stop any pending txsync/rxsync and prevent new ones
2543*22ce4affSfengbojiang 	 * form starting. Processes blocked in poll() will get
2544*22ce4affSfengbojiang 	 * POLLERR.
2545*22ce4affSfengbojiang 	 */
2546*22ce4affSfengbojiang 	netmap_disable_all_rings(ctx->ifc_ifp);
2547*22ce4affSfengbojiang 
2548*22ce4affSfengbojiang 	iflib_debug_reset();
2549a9643ea8Slogwang 	/* Wait for current tx queue users to exit to disarm watchdog timer. */
2550a9643ea8Slogwang 	for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
2551a9643ea8Slogwang 		/* make sure all transmitters have completed before proceeding XXX */
2552a9643ea8Slogwang 
2553*22ce4affSfengbojiang 		CALLOUT_LOCK(txq);
2554*22ce4affSfengbojiang 		callout_stop(&txq->ift_timer);
2555*22ce4affSfengbojiang #ifdef DEV_NETMAP
2556*22ce4affSfengbojiang 		callout_stop(&txq->ift_netmap_timer);
2557*22ce4affSfengbojiang #endif /* DEV_NETMAP */
2558*22ce4affSfengbojiang 		CALLOUT_UNLOCK(txq);
2559*22ce4affSfengbojiang 
2560a9643ea8Slogwang 		/* clean any enqueued buffers */
2561*22ce4affSfengbojiang 		iflib_ifmp_purge(txq);
2562a9643ea8Slogwang 		/* Free any existing tx buffers. */
2563a9643ea8Slogwang 		for (j = 0; j < txq->ift_size; j++) {
2564a9643ea8Slogwang 			iflib_txsd_free(ctx, txq, j);
2565a9643ea8Slogwang 		}
2566a9643ea8Slogwang 		txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2567*22ce4affSfengbojiang 		txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
2568a9643ea8Slogwang 		txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
2569a9643ea8Slogwang 		txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
2570a9643ea8Slogwang 		txq->ift_pullups = 0;
2571*22ce4affSfengbojiang 		ifmp_ring_reset_stats(txq->ift_br);
2572*22ce4affSfengbojiang 		for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++)
2573a9643ea8Slogwang 			bzero((void *)di->idi_vaddr, di->idi_size);
2574a9643ea8Slogwang 	}
2575a9643ea8Slogwang 	for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
2576a9643ea8Slogwang 		/* make sure all transmitters have completed before proceeding XXX */
2577a9643ea8Slogwang 
2578*22ce4affSfengbojiang 		rxq->ifr_cq_cidx = 0;
2579*22ce4affSfengbojiang 		for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++)
2580a9643ea8Slogwang 			bzero((void *)di->idi_vaddr, di->idi_size);
2581a9643ea8Slogwang 		/* also resets the free lists pidx/cidx */
2582a9643ea8Slogwang 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
2583a9643ea8Slogwang 			iflib_fl_bufs_free(fl);
2584a9643ea8Slogwang 	}
2585a9643ea8Slogwang }
2586a9643ea8Slogwang 
2587*22ce4affSfengbojiang static inline caddr_t
calc_next_rxd(iflib_fl_t fl,int cidx)2588*22ce4affSfengbojiang calc_next_rxd(iflib_fl_t fl, int cidx)
2589a9643ea8Slogwang {
2590*22ce4affSfengbojiang 	qidx_t size;
2591*22ce4affSfengbojiang 	int nrxd;
2592*22ce4affSfengbojiang 	caddr_t start, end, cur, next;
2593a9643ea8Slogwang 
2594*22ce4affSfengbojiang 	nrxd = fl->ifl_size;
2595*22ce4affSfengbojiang 	size = fl->ifl_rxd_size;
2596*22ce4affSfengbojiang 	start = fl->ifl_ifdi->idi_vaddr;
2597a9643ea8Slogwang 
2598*22ce4affSfengbojiang 	if (__predict_false(size == 0))
2599*22ce4affSfengbojiang 		return (start);
2600*22ce4affSfengbojiang 	cur = start + size*cidx;
2601*22ce4affSfengbojiang 	end = start + size*nrxd;
2602*22ce4affSfengbojiang 	next = CACHE_PTR_NEXT(cur);
2603*22ce4affSfengbojiang 	return (next < end ? next : start);
2604a9643ea8Slogwang }
2605*22ce4affSfengbojiang 
2606*22ce4affSfengbojiang static inline void
prefetch_pkts(iflib_fl_t fl,int cidx)2607*22ce4affSfengbojiang prefetch_pkts(iflib_fl_t fl, int cidx)
2608*22ce4affSfengbojiang {
2609*22ce4affSfengbojiang 	int nextptr;
2610*22ce4affSfengbojiang 	int nrxd = fl->ifl_size;
2611*22ce4affSfengbojiang 	caddr_t next_rxd;
2612*22ce4affSfengbojiang 
2613*22ce4affSfengbojiang 	nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2614*22ce4affSfengbojiang 	prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2615*22ce4affSfengbojiang 	prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
2616*22ce4affSfengbojiang 	next_rxd = calc_next_rxd(fl, cidx);
2617*22ce4affSfengbojiang 	prefetch(next_rxd);
2618*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2619*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2620*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2621*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2622*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2623*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2624*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2625*22ce4affSfengbojiang 	prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2626a9643ea8Slogwang }
2627a9643ea8Slogwang 
2628a9643ea8Slogwang static struct mbuf *
rxd_frag_to_sd(iflib_rxq_t rxq,if_rxd_frag_t irf,bool unload,if_rxsd_t sd,int * pf_rv,if_rxd_info_t ri)2629*22ce4affSfengbojiang rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd,
2630*22ce4affSfengbojiang     int *pf_rv, if_rxd_info_t ri)
2631a9643ea8Slogwang {
2632*22ce4affSfengbojiang 	bus_dmamap_t map;
2633*22ce4affSfengbojiang 	iflib_fl_t fl;
2634*22ce4affSfengbojiang 	caddr_t payload;
2635*22ce4affSfengbojiang 	struct mbuf *m;
2636*22ce4affSfengbojiang 	int flid, cidx, len, next;
2637*22ce4affSfengbojiang 
2638*22ce4affSfengbojiang 	map = NULL;
2639*22ce4affSfengbojiang 	flid = irf->irf_flid;
2640*22ce4affSfengbojiang 	cidx = irf->irf_idx;
2641*22ce4affSfengbojiang 	fl = &rxq->ifr_fl[flid];
2642*22ce4affSfengbojiang 	sd->ifsd_fl = fl;
2643*22ce4affSfengbojiang 	m = fl->ifl_sds.ifsd_m[cidx];
2644*22ce4affSfengbojiang 	sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
2645*22ce4affSfengbojiang 	fl->ifl_credits--;
2646*22ce4affSfengbojiang #if MEMORY_LOGGING
2647*22ce4affSfengbojiang 	fl->ifl_m_dequeued++;
2648*22ce4affSfengbojiang #endif
2649*22ce4affSfengbojiang 	if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
2650*22ce4affSfengbojiang 		prefetch_pkts(fl, cidx);
2651*22ce4affSfengbojiang 	next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2652*22ce4affSfengbojiang 	prefetch(&fl->ifl_sds.ifsd_map[next]);
2653*22ce4affSfengbojiang 	map = fl->ifl_sds.ifsd_map[cidx];
2654*22ce4affSfengbojiang 
2655*22ce4affSfengbojiang 	bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD);
2656*22ce4affSfengbojiang 
2657*22ce4affSfengbojiang 	if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL &&
2658*22ce4affSfengbojiang 	    irf->irf_len != 0) {
2659*22ce4affSfengbojiang 		payload  = *sd->ifsd_cl;
2660*22ce4affSfengbojiang 		payload +=  ri->iri_pad;
2661*22ce4affSfengbojiang 		len = ri->iri_len - ri->iri_pad;
2662*22ce4affSfengbojiang 		*pf_rv = pfil_run_hooks(rxq->pfil, payload, ri->iri_ifp,
2663*22ce4affSfengbojiang 		    len | PFIL_MEMPTR | PFIL_IN, NULL);
2664*22ce4affSfengbojiang 		switch (*pf_rv) {
2665*22ce4affSfengbojiang 		case PFIL_DROPPED:
2666*22ce4affSfengbojiang 		case PFIL_CONSUMED:
2667*22ce4affSfengbojiang 			/*
2668*22ce4affSfengbojiang 			 * The filter ate it.  Everything is recycled.
2669*22ce4affSfengbojiang 			 */
2670*22ce4affSfengbojiang 			m = NULL;
2671*22ce4affSfengbojiang 			unload = 0;
2672*22ce4affSfengbojiang 			break;
2673*22ce4affSfengbojiang 		case PFIL_REALLOCED:
2674*22ce4affSfengbojiang 			/*
2675*22ce4affSfengbojiang 			 * The filter copied it.  Everything is recycled.
2676*22ce4affSfengbojiang 			 */
2677*22ce4affSfengbojiang 			m = pfil_mem2mbuf(payload);
2678*22ce4affSfengbojiang 			unload = 0;
2679*22ce4affSfengbojiang 			break;
2680*22ce4affSfengbojiang 		case PFIL_PASS:
2681*22ce4affSfengbojiang 			/*
2682*22ce4affSfengbojiang 			 * Filter said it was OK, so receive like
2683*22ce4affSfengbojiang 			 * normal
2684*22ce4affSfengbojiang 			 */
2685*22ce4affSfengbojiang 			fl->ifl_sds.ifsd_m[cidx] = NULL;
2686*22ce4affSfengbojiang 			break;
2687*22ce4affSfengbojiang 		default:
2688*22ce4affSfengbojiang 			MPASS(0);
2689*22ce4affSfengbojiang 		}
2690*22ce4affSfengbojiang 	} else {
2691*22ce4affSfengbojiang 		fl->ifl_sds.ifsd_m[cidx] = NULL;
2692*22ce4affSfengbojiang 		if (pf_rv != NULL)
2693*22ce4affSfengbojiang 			*pf_rv = PFIL_PASS;
2694*22ce4affSfengbojiang 	}
2695*22ce4affSfengbojiang 
2696*22ce4affSfengbojiang 	if (unload && irf->irf_len != 0)
2697*22ce4affSfengbojiang 		bus_dmamap_unload(fl->ifl_buf_tag, map);
2698*22ce4affSfengbojiang 	fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1);
2699*22ce4affSfengbojiang 	if (__predict_false(fl->ifl_cidx == 0))
2700*22ce4affSfengbojiang 		fl->ifl_gen = 0;
2701*22ce4affSfengbojiang 	bit_clear(fl->ifl_rx_bitmap, cidx);
2702*22ce4affSfengbojiang 	return (m);
2703*22ce4affSfengbojiang }
2704*22ce4affSfengbojiang 
2705*22ce4affSfengbojiang static struct mbuf *
assemble_segments(iflib_rxq_t rxq,if_rxd_info_t ri,if_rxsd_t sd,int * pf_rv)2706*22ce4affSfengbojiang assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv)
2707*22ce4affSfengbojiang {
2708a9643ea8Slogwang 	struct mbuf *m, *mh, *mt;
2709a9643ea8Slogwang 	caddr_t cl;
2710*22ce4affSfengbojiang 	int  *pf_rv_ptr, flags, i, padlen;
2711*22ce4affSfengbojiang 	bool consumed;
2712a9643ea8Slogwang 
2713a9643ea8Slogwang 	i = 0;
2714a9643ea8Slogwang 	mh = NULL;
2715*22ce4affSfengbojiang 	consumed = false;
2716*22ce4affSfengbojiang 	*pf_rv = PFIL_PASS;
2717*22ce4affSfengbojiang 	pf_rv_ptr = pf_rv;
2718a9643ea8Slogwang 	do {
2719*22ce4affSfengbojiang 		m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd,
2720*22ce4affSfengbojiang 		    pf_rv_ptr, ri);
2721a9643ea8Slogwang 
2722*22ce4affSfengbojiang 		MPASS(*sd->ifsd_cl != NULL);
2723a9643ea8Slogwang 
2724*22ce4affSfengbojiang 		/*
2725*22ce4affSfengbojiang 		 * Exclude zero-length frags & frags from
2726*22ce4affSfengbojiang 		 * packets the filter has consumed or dropped
2727*22ce4affSfengbojiang 		 */
2728*22ce4affSfengbojiang 		if (ri->iri_frags[i].irf_len == 0 || consumed ||
2729*22ce4affSfengbojiang 		    *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) {
2730*22ce4affSfengbojiang 			if (mh == NULL) {
2731*22ce4affSfengbojiang 				/* everything saved here */
2732*22ce4affSfengbojiang 				consumed = true;
2733*22ce4affSfengbojiang 				pf_rv_ptr = NULL;
2734a9643ea8Slogwang 				continue;
2735a9643ea8Slogwang 			}
2736*22ce4affSfengbojiang 			/* XXX we can save the cluster here, but not the mbuf */
2737*22ce4affSfengbojiang 			m_init(m, M_NOWAIT, MT_DATA, 0);
2738*22ce4affSfengbojiang 			m_free(m);
2739*22ce4affSfengbojiang 			continue;
2740*22ce4affSfengbojiang 		}
2741a9643ea8Slogwang 		if (mh == NULL) {
2742a9643ea8Slogwang 			flags = M_PKTHDR|M_EXT;
2743a9643ea8Slogwang 			mh = mt = m;
2744a9643ea8Slogwang 			padlen = ri->iri_pad;
2745a9643ea8Slogwang 		} else {
2746a9643ea8Slogwang 			flags = M_EXT;
2747a9643ea8Slogwang 			mt->m_next = m;
2748a9643ea8Slogwang 			mt = m;
2749a9643ea8Slogwang 			/* assuming padding is only on the first fragment */
2750a9643ea8Slogwang 			padlen = 0;
2751a9643ea8Slogwang 		}
2752*22ce4affSfengbojiang 		cl = *sd->ifsd_cl;
2753*22ce4affSfengbojiang 		*sd->ifsd_cl = NULL;
2754a9643ea8Slogwang 
2755a9643ea8Slogwang 		/* Can these two be made one ? */
2756a9643ea8Slogwang 		m_init(m, M_NOWAIT, MT_DATA, flags);
2757*22ce4affSfengbojiang 		m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
2758a9643ea8Slogwang 		/*
2759a9643ea8Slogwang 		 * These must follow m_init and m_cljset
2760a9643ea8Slogwang 		 */
2761a9643ea8Slogwang 		m->m_data += padlen;
2762a9643ea8Slogwang 		ri->iri_len -= padlen;
2763a9643ea8Slogwang 		m->m_len = ri->iri_frags[i].irf_len;
2764a9643ea8Slogwang 	} while (++i < ri->iri_nfrags);
2765a9643ea8Slogwang 
2766a9643ea8Slogwang 	return (mh);
2767a9643ea8Slogwang }
2768a9643ea8Slogwang 
2769a9643ea8Slogwang /*
2770a9643ea8Slogwang  * Process one software descriptor
2771a9643ea8Slogwang  */
2772a9643ea8Slogwang static struct mbuf *
iflib_rxd_pkt_get(iflib_rxq_t rxq,if_rxd_info_t ri)2773a9643ea8Slogwang iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
2774a9643ea8Slogwang {
2775*22ce4affSfengbojiang 	struct if_rxsd sd;
2776a9643ea8Slogwang 	struct mbuf *m;
2777*22ce4affSfengbojiang 	int pf_rv;
2778a9643ea8Slogwang 
2779a9643ea8Slogwang 	/* should I merge this back in now that the two paths are basically duplicated? */
2780a9643ea8Slogwang 	if (ri->iri_nfrags == 1 &&
2781*22ce4affSfengbojiang 	    ri->iri_frags[0].irf_len != 0 &&
2782*22ce4affSfengbojiang 	    ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) {
2783*22ce4affSfengbojiang 		m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd,
2784*22ce4affSfengbojiang 		    &pf_rv, ri);
2785*22ce4affSfengbojiang 		if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
2786*22ce4affSfengbojiang 			return (m);
2787*22ce4affSfengbojiang 		if (pf_rv == PFIL_PASS) {
2788a9643ea8Slogwang 			m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
2789*22ce4affSfengbojiang #ifndef __NO_STRICT_ALIGNMENT
2790*22ce4affSfengbojiang 			if (!IP_ALIGNED(m))
2791*22ce4affSfengbojiang 				m->m_data += 2;
2792*22ce4affSfengbojiang #endif
2793*22ce4affSfengbojiang 			memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
2794a9643ea8Slogwang 			m->m_len = ri->iri_frags[0].irf_len;
2795*22ce4affSfengbojiang 		}
2796a9643ea8Slogwang 	} else {
2797*22ce4affSfengbojiang 		m = assemble_segments(rxq, ri, &sd, &pf_rv);
2798*22ce4affSfengbojiang 		if (m == NULL)
2799*22ce4affSfengbojiang 			return (NULL);
2800*22ce4affSfengbojiang 		if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
2801*22ce4affSfengbojiang 			return (m);
2802a9643ea8Slogwang 	}
2803a9643ea8Slogwang 	m->m_pkthdr.len = ri->iri_len;
2804a9643ea8Slogwang 	m->m_pkthdr.rcvif = ri->iri_ifp;
2805a9643ea8Slogwang 	m->m_flags |= ri->iri_flags;
2806a9643ea8Slogwang 	m->m_pkthdr.ether_vtag = ri->iri_vtag;
2807a9643ea8Slogwang 	m->m_pkthdr.flowid = ri->iri_flowid;
2808a9643ea8Slogwang 	M_HASHTYPE_SET(m, ri->iri_rsstype);
2809a9643ea8Slogwang 	m->m_pkthdr.csum_flags = ri->iri_csum_flags;
2810a9643ea8Slogwang 	m->m_pkthdr.csum_data = ri->iri_csum_data;
2811a9643ea8Slogwang 	return (m);
2812a9643ea8Slogwang }
2813a9643ea8Slogwang 
2814*22ce4affSfengbojiang #if defined(INET6) || defined(INET)
2815*22ce4affSfengbojiang static void
iflib_get_ip_forwarding(struct lro_ctrl * lc,bool * v4,bool * v6)2816*22ce4affSfengbojiang iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6)
2817a9643ea8Slogwang {
2818*22ce4affSfengbojiang 	CURVNET_SET(lc->ifp->if_vnet);
2819*22ce4affSfengbojiang #if defined(INET6)
2820*22ce4affSfengbojiang 	*v6 = V_ip6_forwarding;
2821*22ce4affSfengbojiang #endif
2822*22ce4affSfengbojiang #if defined(INET)
2823*22ce4affSfengbojiang 	*v4 = V_ipforwarding;
2824*22ce4affSfengbojiang #endif
2825*22ce4affSfengbojiang 	CURVNET_RESTORE();
2826*22ce4affSfengbojiang }
2827*22ce4affSfengbojiang 
2828*22ce4affSfengbojiang /*
2829*22ce4affSfengbojiang  * Returns true if it's possible this packet could be LROed.
2830*22ce4affSfengbojiang  * if it returns false, it is guaranteed that tcp_lro_rx()
2831*22ce4affSfengbojiang  * would not return zero.
2832*22ce4affSfengbojiang  */
2833*22ce4affSfengbojiang static bool
iflib_check_lro_possible(struct mbuf * m,bool v4_forwarding,bool v6_forwarding)2834*22ce4affSfengbojiang iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding)
2835*22ce4affSfengbojiang {
2836*22ce4affSfengbojiang 	struct ether_header *eh;
2837*22ce4affSfengbojiang 
2838*22ce4affSfengbojiang 	eh = mtod(m, struct ether_header *);
2839*22ce4affSfengbojiang 	switch (eh->ether_type) {
2840*22ce4affSfengbojiang #if defined(INET6)
2841*22ce4affSfengbojiang 		case htons(ETHERTYPE_IPV6):
2842*22ce4affSfengbojiang 			return (!v6_forwarding);
2843*22ce4affSfengbojiang #endif
2844*22ce4affSfengbojiang #if defined (INET)
2845*22ce4affSfengbojiang 		case htons(ETHERTYPE_IP):
2846*22ce4affSfengbojiang 			return (!v4_forwarding);
2847*22ce4affSfengbojiang #endif
2848*22ce4affSfengbojiang 	}
2849*22ce4affSfengbojiang 
2850*22ce4affSfengbojiang 	return false;
2851*22ce4affSfengbojiang }
2852*22ce4affSfengbojiang #else
2853*22ce4affSfengbojiang static void
iflib_get_ip_forwarding(struct lro_ctrl * lc __unused,bool * v4 __unused,bool * v6 __unused)2854*22ce4affSfengbojiang iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused)
2855*22ce4affSfengbojiang {
2856*22ce4affSfengbojiang }
2857*22ce4affSfengbojiang #endif
2858*22ce4affSfengbojiang 
2859*22ce4affSfengbojiang static void
_task_fn_rx_watchdog(void * context)2860*22ce4affSfengbojiang _task_fn_rx_watchdog(void *context)
2861*22ce4affSfengbojiang {
2862*22ce4affSfengbojiang 	iflib_rxq_t rxq = context;
2863*22ce4affSfengbojiang 
2864*22ce4affSfengbojiang 	GROUPTASK_ENQUEUE(&rxq->ifr_task);
2865*22ce4affSfengbojiang }
2866*22ce4affSfengbojiang 
2867*22ce4affSfengbojiang static uint8_t
iflib_rxeof(iflib_rxq_t rxq,qidx_t budget)2868*22ce4affSfengbojiang iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
2869*22ce4affSfengbojiang {
2870*22ce4affSfengbojiang 	if_t ifp;
2871a9643ea8Slogwang 	if_ctx_t ctx = rxq->ifr_ctx;
2872a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
2873a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2874a9643ea8Slogwang 	int avail, i;
2875*22ce4affSfengbojiang 	qidx_t *cidxp;
2876a9643ea8Slogwang 	struct if_rxd_info ri;
2877a9643ea8Slogwang 	int err, budget_left, rx_bytes, rx_pkts;
2878a9643ea8Slogwang 	iflib_fl_t fl;
2879a9643ea8Slogwang 	int lro_enabled;
2880*22ce4affSfengbojiang 	bool v4_forwarding, v6_forwarding, lro_possible;
2881*22ce4affSfengbojiang 	uint8_t retval = 0;
2882*22ce4affSfengbojiang 
2883a9643ea8Slogwang 	/*
2884a9643ea8Slogwang 	 * XXX early demux data packets so that if_input processing only handles
2885a9643ea8Slogwang 	 * acks in interrupt context
2886a9643ea8Slogwang 	 */
2887*22ce4affSfengbojiang 	struct mbuf *m, *mh, *mt, *mf;
2888a9643ea8Slogwang 
2889*22ce4affSfengbojiang 	NET_EPOCH_ASSERT();
2890a9643ea8Slogwang 
2891*22ce4affSfengbojiang 	lro_possible = v4_forwarding = v6_forwarding = false;
2892*22ce4affSfengbojiang 	ifp = ctx->ifc_ifp;
2893a9643ea8Slogwang 	mh = mt = NULL;
2894a9643ea8Slogwang 	MPASS(budget > 0);
2895a9643ea8Slogwang 	rx_pkts	= rx_bytes = 0;
2896a9643ea8Slogwang 	if (sctx->isc_flags & IFLIB_HAS_RXCQ)
2897a9643ea8Slogwang 		cidxp = &rxq->ifr_cq_cidx;
2898a9643ea8Slogwang 	else
2899a9643ea8Slogwang 		cidxp = &rxq->ifr_fl[0].ifl_cidx;
2900a9643ea8Slogwang 	if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
2901a9643ea8Slogwang 		for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2902*22ce4affSfengbojiang 			retval |= iflib_fl_refill_all(ctx, fl);
2903a9643ea8Slogwang 		DBG_COUNTER_INC(rx_unavail);
2904*22ce4affSfengbojiang 		return (retval);
2905a9643ea8Slogwang 	}
2906a9643ea8Slogwang 
2907*22ce4affSfengbojiang 	/* pfil needs the vnet to be set */
2908*22ce4affSfengbojiang 	CURVNET_SET_QUIET(ifp->if_vnet);
2909*22ce4affSfengbojiang 	for (budget_left = budget; budget_left > 0 && avail > 0;) {
2910a9643ea8Slogwang 		if (__predict_false(!CTX_ACTIVE(ctx))) {
2911a9643ea8Slogwang 			DBG_COUNTER_INC(rx_ctx_inactive);
2912a9643ea8Slogwang 			break;
2913a9643ea8Slogwang 		}
2914a9643ea8Slogwang 		/*
2915a9643ea8Slogwang 		 * Reset client set fields to their default values
2916a9643ea8Slogwang 		 */
2917*22ce4affSfengbojiang 		rxd_info_zero(&ri);
2918a9643ea8Slogwang 		ri.iri_qsidx = rxq->ifr_id;
2919a9643ea8Slogwang 		ri.iri_cidx = *cidxp;
2920*22ce4affSfengbojiang 		ri.iri_ifp = ifp;
2921a9643ea8Slogwang 		ri.iri_frags = rxq->ifr_frags;
2922a9643ea8Slogwang 		err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
2923a9643ea8Slogwang 
2924*22ce4affSfengbojiang 		if (err)
2925*22ce4affSfengbojiang 			goto err;
2926*22ce4affSfengbojiang 		rx_pkts += 1;
2927*22ce4affSfengbojiang 		rx_bytes += ri.iri_len;
2928a9643ea8Slogwang 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
2929a9643ea8Slogwang 			*cidxp = ri.iri_cidx;
2930a9643ea8Slogwang 			/* Update our consumer index */
2931*22ce4affSfengbojiang 			/* XXX NB: shurd - check if this is still safe */
2932*22ce4affSfengbojiang 			while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0])
2933a9643ea8Slogwang 				rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
2934a9643ea8Slogwang 			/* was this only a completion queue message? */
2935a9643ea8Slogwang 			if (__predict_false(ri.iri_nfrags == 0))
2936a9643ea8Slogwang 				continue;
2937a9643ea8Slogwang 		}
2938a9643ea8Slogwang 		MPASS(ri.iri_nfrags != 0);
2939a9643ea8Slogwang 		MPASS(ri.iri_len != 0);
2940a9643ea8Slogwang 
2941a9643ea8Slogwang 		/* will advance the cidx on the corresponding free lists */
2942a9643ea8Slogwang 		m = iflib_rxd_pkt_get(rxq, &ri);
2943*22ce4affSfengbojiang 		avail--;
2944*22ce4affSfengbojiang 		budget_left--;
2945a9643ea8Slogwang 		if (avail == 0 && budget_left)
2946a9643ea8Slogwang 			avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
2947a9643ea8Slogwang 
2948*22ce4affSfengbojiang 		if (__predict_false(m == NULL))
2949a9643ea8Slogwang 			continue;
2950*22ce4affSfengbojiang 
2951a9643ea8Slogwang 		/* imm_pkt: -- cxgb */
2952a9643ea8Slogwang 		if (mh == NULL)
2953a9643ea8Slogwang 			mh = mt = m;
2954a9643ea8Slogwang 		else {
2955a9643ea8Slogwang 			mt->m_nextpkt = m;
2956a9643ea8Slogwang 			mt = m;
2957a9643ea8Slogwang 		}
2958a9643ea8Slogwang 	}
2959*22ce4affSfengbojiang 	CURVNET_RESTORE();
2960a9643ea8Slogwang 	/* make sure that we can refill faster than drain */
2961a9643ea8Slogwang 	for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2962*22ce4affSfengbojiang 		retval |= iflib_fl_refill_all(ctx, fl);
2963a9643ea8Slogwang 
2964a9643ea8Slogwang 	lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
2965*22ce4affSfengbojiang 	if (lro_enabled)
2966*22ce4affSfengbojiang 		iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding);
2967*22ce4affSfengbojiang 	mt = mf = NULL;
2968a9643ea8Slogwang 	while (mh != NULL) {
2969a9643ea8Slogwang 		m = mh;
2970a9643ea8Slogwang 		mh = mh->m_nextpkt;
2971a9643ea8Slogwang 		m->m_nextpkt = NULL;
2972*22ce4affSfengbojiang #ifndef __NO_STRICT_ALIGNMENT
2973*22ce4affSfengbojiang 		if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
2974a9643ea8Slogwang 			continue;
2975a9643ea8Slogwang #endif
2976*22ce4affSfengbojiang #if defined(INET6) || defined(INET)
2977*22ce4affSfengbojiang 		if (lro_enabled) {
2978*22ce4affSfengbojiang 			if (!lro_possible) {
2979*22ce4affSfengbojiang 				lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding);
2980*22ce4affSfengbojiang 				if (lro_possible && mf != NULL) {
2981*22ce4affSfengbojiang 					ifp->if_input(ifp, mf);
2982a9643ea8Slogwang 					DBG_COUNTER_INC(rx_if_input);
2983*22ce4affSfengbojiang 					mt = mf = NULL;
2984*22ce4affSfengbojiang 				}
2985*22ce4affSfengbojiang 			}
2986*22ce4affSfengbojiang 			if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) ==
2987*22ce4affSfengbojiang 			    (CSUM_L4_CALC|CSUM_L4_VALID)) {
2988*22ce4affSfengbojiang 				if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
2989*22ce4affSfengbojiang 					continue;
2990*22ce4affSfengbojiang 			}
2991*22ce4affSfengbojiang 		}
2992*22ce4affSfengbojiang #endif
2993*22ce4affSfengbojiang 		if (lro_possible) {
2994a9643ea8Slogwang 			ifp->if_input(ifp, m);
2995*22ce4affSfengbojiang 			DBG_COUNTER_INC(rx_if_input);
2996*22ce4affSfengbojiang 			continue;
2997*22ce4affSfengbojiang 		}
2998*22ce4affSfengbojiang 
2999*22ce4affSfengbojiang 		if (mf == NULL)
3000*22ce4affSfengbojiang 			mf = m;
3001*22ce4affSfengbojiang 		if (mt != NULL)
3002*22ce4affSfengbojiang 			mt->m_nextpkt = m;
3003*22ce4affSfengbojiang 		mt = m;
3004*22ce4affSfengbojiang 	}
3005*22ce4affSfengbojiang 	if (mf != NULL) {
3006*22ce4affSfengbojiang 		ifp->if_input(ifp, mf);
3007*22ce4affSfengbojiang 		DBG_COUNTER_INC(rx_if_input);
3008a9643ea8Slogwang 	}
3009a9643ea8Slogwang 
3010a9643ea8Slogwang 	if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
3011a9643ea8Slogwang 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
3012a9643ea8Slogwang 
3013a9643ea8Slogwang 	/*
3014a9643ea8Slogwang 	 * Flush any outstanding LRO work
3015a9643ea8Slogwang 	 */
3016a9643ea8Slogwang #if defined(INET6) || defined(INET)
3017a9643ea8Slogwang 	tcp_lro_flush_all(&rxq->ifr_lc);
3018a9643ea8Slogwang #endif
3019*22ce4affSfengbojiang 	if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0)
3020*22ce4affSfengbojiang 		retval |= IFLIB_RXEOF_MORE;
3021*22ce4affSfengbojiang 	return (retval);
3022*22ce4affSfengbojiang err:
3023*22ce4affSfengbojiang 	STATE_LOCK(ctx);
3024*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_DO_RESET;
3025*22ce4affSfengbojiang 	iflib_admin_intr_deferred(ctx);
3026*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
3027*22ce4affSfengbojiang 	return (0);
3028*22ce4affSfengbojiang }
3029*22ce4affSfengbojiang 
3030*22ce4affSfengbojiang #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1)
3031*22ce4affSfengbojiang static inline qidx_t
txq_max_db_deferred(iflib_txq_t txq,qidx_t in_use)3032*22ce4affSfengbojiang txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
3033*22ce4affSfengbojiang {
3034*22ce4affSfengbojiang 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
3035*22ce4affSfengbojiang 	qidx_t minthresh = txq->ift_size / 8;
3036*22ce4affSfengbojiang 	if (in_use > 4*minthresh)
3037*22ce4affSfengbojiang 		return (notify_count);
3038*22ce4affSfengbojiang 	if (in_use > 2*minthresh)
3039*22ce4affSfengbojiang 		return (notify_count >> 1);
3040*22ce4affSfengbojiang 	if (in_use > minthresh)
3041*22ce4affSfengbojiang 		return (notify_count >> 3);
3042*22ce4affSfengbojiang 	return (0);
3043*22ce4affSfengbojiang }
3044*22ce4affSfengbojiang 
3045*22ce4affSfengbojiang static inline qidx_t
txq_max_rs_deferred(iflib_txq_t txq)3046*22ce4affSfengbojiang txq_max_rs_deferred(iflib_txq_t txq)
3047*22ce4affSfengbojiang {
3048*22ce4affSfengbojiang 	qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
3049*22ce4affSfengbojiang 	qidx_t minthresh = txq->ift_size / 8;
3050*22ce4affSfengbojiang 	if (txq->ift_in_use > 4*minthresh)
3051*22ce4affSfengbojiang 		return (notify_count);
3052*22ce4affSfengbojiang 	if (txq->ift_in_use > 2*minthresh)
3053*22ce4affSfengbojiang 		return (notify_count >> 1);
3054*22ce4affSfengbojiang 	if (txq->ift_in_use > minthresh)
3055*22ce4affSfengbojiang 		return (notify_count >> 2);
3056*22ce4affSfengbojiang 	return (2);
3057a9643ea8Slogwang }
3058a9643ea8Slogwang 
3059a9643ea8Slogwang #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
3060a9643ea8Slogwang #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
3061*22ce4affSfengbojiang 
3062*22ce4affSfengbojiang #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
3063*22ce4affSfengbojiang #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
3064a9643ea8Slogwang #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
3065a9643ea8Slogwang 
3066*22ce4affSfengbojiang /* forward compatibility for cxgb */
3067*22ce4affSfengbojiang #define FIRST_QSET(ctx) 0
3068*22ce4affSfengbojiang #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
3069*22ce4affSfengbojiang #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
3070*22ce4affSfengbojiang #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
3071*22ce4affSfengbojiang #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
3072*22ce4affSfengbojiang 
3073*22ce4affSfengbojiang /* XXX we should be setting this to something other than zero */
3074*22ce4affSfengbojiang #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
3075*22ce4affSfengbojiang #define	MAX_TX_DESC(ctx) MAX((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \
3076*22ce4affSfengbojiang     (ctx)->ifc_softc_ctx.isc_tx_nsegments)
3077*22ce4affSfengbojiang 
3078*22ce4affSfengbojiang static inline bool
iflib_txd_db_check(iflib_txq_t txq,int ring)3079*22ce4affSfengbojiang iflib_txd_db_check(iflib_txq_t txq, int ring)
3080a9643ea8Slogwang {
3081*22ce4affSfengbojiang 	if_ctx_t ctx = txq->ift_ctx;
3082*22ce4affSfengbojiang 	qidx_t dbval, max;
3083a9643ea8Slogwang 
3084*22ce4affSfengbojiang 	max = TXQ_MAX_DB_DEFERRED(txq, txq->ift_in_use);
3085a9643ea8Slogwang 
3086*22ce4affSfengbojiang 	/* force || threshold exceeded || at the edge of the ring */
3087*22ce4affSfengbojiang 	if (ring || (txq->ift_db_pending >= max) || (TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx) + 2)) {
3088*22ce4affSfengbojiang 
3089*22ce4affSfengbojiang 		/*
3090*22ce4affSfengbojiang 		 * 'npending' is used if the card's doorbell is in terms of the number of descriptors
3091*22ce4affSfengbojiang 		 * pending flush (BRCM). 'pidx' is used in cases where the card's doorbeel uses the
3092*22ce4affSfengbojiang 		 * producer index explicitly (INTC).
3093*22ce4affSfengbojiang 		 */
3094a9643ea8Slogwang 		dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
3095*22ce4affSfengbojiang 		bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
3096*22ce4affSfengbojiang 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3097a9643ea8Slogwang 		ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
3098*22ce4affSfengbojiang 
3099*22ce4affSfengbojiang 		/*
3100*22ce4affSfengbojiang 		 * Absent bugs there are zero packets pending so reset pending counts to zero.
3101*22ce4affSfengbojiang 		 */
3102a9643ea8Slogwang 		txq->ift_db_pending = txq->ift_npending = 0;
3103*22ce4affSfengbojiang 		return (true);
3104a9643ea8Slogwang 	}
3105*22ce4affSfengbojiang 	return (false);
3106a9643ea8Slogwang }
3107a9643ea8Slogwang 
3108a9643ea8Slogwang #ifdef PKT_DEBUG
3109a9643ea8Slogwang static void
print_pkt(if_pkt_info_t pi)3110a9643ea8Slogwang print_pkt(if_pkt_info_t pi)
3111a9643ea8Slogwang {
3112a9643ea8Slogwang 	printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
3113a9643ea8Slogwang 	       pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
3114a9643ea8Slogwang 	printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
3115a9643ea8Slogwang 	       pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
3116a9643ea8Slogwang 	printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
3117a9643ea8Slogwang 	       pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
3118a9643ea8Slogwang }
3119a9643ea8Slogwang #endif
3120a9643ea8Slogwang 
3121a9643ea8Slogwang #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
3122*22ce4affSfengbojiang #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO))
3123a9643ea8Slogwang #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
3124*22ce4affSfengbojiang #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO))
3125a9643ea8Slogwang 
3126a9643ea8Slogwang static int
iflib_parse_header(iflib_txq_t txq,if_pkt_info_t pi,struct mbuf ** mp)3127a9643ea8Slogwang iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
3128a9643ea8Slogwang {
3129*22ce4affSfengbojiang 	if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
3130a9643ea8Slogwang 	struct ether_vlan_header *eh;
3131*22ce4affSfengbojiang 	struct mbuf *m;
3132a9643ea8Slogwang 
3133*22ce4affSfengbojiang 	m = *mp;
3134*22ce4affSfengbojiang 	if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
3135*22ce4affSfengbojiang 	    M_WRITABLE(m) == 0) {
3136*22ce4affSfengbojiang 		if ((m = m_dup(m, M_NOWAIT)) == NULL) {
3137*22ce4affSfengbojiang 			return (ENOMEM);
3138*22ce4affSfengbojiang 		} else {
3139*22ce4affSfengbojiang 			m_freem(*mp);
3140*22ce4affSfengbojiang 			DBG_COUNTER_INC(tx_frees);
3141*22ce4affSfengbojiang 			*mp = m;
3142*22ce4affSfengbojiang 		}
3143*22ce4affSfengbojiang 	}
3144*22ce4affSfengbojiang 
3145a9643ea8Slogwang 	/*
3146a9643ea8Slogwang 	 * Determine where frame payload starts.
3147a9643ea8Slogwang 	 * Jump over vlan headers if already present,
3148a9643ea8Slogwang 	 * helpful for QinQ too.
3149a9643ea8Slogwang 	 */
3150a9643ea8Slogwang 	if (__predict_false(m->m_len < sizeof(*eh))) {
3151a9643ea8Slogwang 		txq->ift_pullups++;
3152a9643ea8Slogwang 		if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
3153a9643ea8Slogwang 			return (ENOMEM);
3154a9643ea8Slogwang 	}
3155a9643ea8Slogwang 	eh = mtod(m, struct ether_vlan_header *);
3156a9643ea8Slogwang 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
3157a9643ea8Slogwang 		pi->ipi_etype = ntohs(eh->evl_proto);
3158a9643ea8Slogwang 		pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3159a9643ea8Slogwang 	} else {
3160a9643ea8Slogwang 		pi->ipi_etype = ntohs(eh->evl_encap_proto);
3161a9643ea8Slogwang 		pi->ipi_ehdrlen = ETHER_HDR_LEN;
3162a9643ea8Slogwang 	}
3163a9643ea8Slogwang 
3164a9643ea8Slogwang 	switch (pi->ipi_etype) {
3165a9643ea8Slogwang #ifdef INET
3166a9643ea8Slogwang 	case ETHERTYPE_IP:
3167a9643ea8Slogwang 	{
3168*22ce4affSfengbojiang 		struct mbuf *n;
3169a9643ea8Slogwang 		struct ip *ip = NULL;
3170a9643ea8Slogwang 		struct tcphdr *th = NULL;
3171a9643ea8Slogwang 		int minthlen;
3172a9643ea8Slogwang 
3173a9643ea8Slogwang 		minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
3174a9643ea8Slogwang 		if (__predict_false(m->m_len < minthlen)) {
3175a9643ea8Slogwang 			/*
3176a9643ea8Slogwang 			 * if this code bloat is causing too much of a hit
3177a9643ea8Slogwang 			 * move it to a separate function and mark it noinline
3178a9643ea8Slogwang 			 */
3179a9643ea8Slogwang 			if (m->m_len == pi->ipi_ehdrlen) {
3180a9643ea8Slogwang 				n = m->m_next;
3181a9643ea8Slogwang 				MPASS(n);
3182a9643ea8Slogwang 				if (n->m_len >= sizeof(*ip))  {
3183a9643ea8Slogwang 					ip = (struct ip *)n->m_data;
3184a9643ea8Slogwang 					if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
3185a9643ea8Slogwang 						th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
3186a9643ea8Slogwang 				} else {
3187a9643ea8Slogwang 					txq->ift_pullups++;
3188a9643ea8Slogwang 					if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
3189a9643ea8Slogwang 						return (ENOMEM);
3190a9643ea8Slogwang 					ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3191a9643ea8Slogwang 				}
3192a9643ea8Slogwang 			} else {
3193a9643ea8Slogwang 				txq->ift_pullups++;
3194a9643ea8Slogwang 				if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
3195a9643ea8Slogwang 					return (ENOMEM);
3196a9643ea8Slogwang 				ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3197a9643ea8Slogwang 				if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
3198a9643ea8Slogwang 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
3199a9643ea8Slogwang 			}
3200a9643ea8Slogwang 		} else {
3201a9643ea8Slogwang 			ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3202a9643ea8Slogwang 			if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
3203a9643ea8Slogwang 				th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
3204a9643ea8Slogwang 		}
3205a9643ea8Slogwang 		pi->ipi_ip_hlen = ip->ip_hl << 2;
3206a9643ea8Slogwang 		pi->ipi_ipproto = ip->ip_p;
3207a9643ea8Slogwang 		pi->ipi_flags |= IPI_TX_IPV4;
3208a9643ea8Slogwang 
3209*22ce4affSfengbojiang 		/* TCP checksum offload may require TCP header length */
3210*22ce4affSfengbojiang 		if (IS_TX_OFFLOAD4(pi)) {
3211*22ce4affSfengbojiang 			if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) {
3212a9643ea8Slogwang 				if (__predict_false(th == NULL)) {
3213a9643ea8Slogwang 					txq->ift_pullups++;
3214a9643ea8Slogwang 					if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
3215a9643ea8Slogwang 						return (ENOMEM);
3216a9643ea8Slogwang 					th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
3217a9643ea8Slogwang 				}
3218a9643ea8Slogwang 				pi->ipi_tcp_hflags = th->th_flags;
3219a9643ea8Slogwang 				pi->ipi_tcp_hlen = th->th_off << 2;
3220a9643ea8Slogwang 				pi->ipi_tcp_seq = th->th_seq;
3221a9643ea8Slogwang 			}
3222a9643ea8Slogwang 			if (IS_TSO4(pi)) {
3223a9643ea8Slogwang 				if (__predict_false(ip->ip_p != IPPROTO_TCP))
3224a9643ea8Slogwang 					return (ENXIO);
3225*22ce4affSfengbojiang 				/*
3226*22ce4affSfengbojiang 				 * TSO always requires hardware checksum offload.
3227*22ce4affSfengbojiang 				 */
3228*22ce4affSfengbojiang 				pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP);
3229a9643ea8Slogwang 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
3230a9643ea8Slogwang 						       ip->ip_dst.s_addr, htons(IPPROTO_TCP));
3231a9643ea8Slogwang 				pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
3232*22ce4affSfengbojiang 				if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
3233*22ce4affSfengbojiang 					ip->ip_sum = 0;
3234*22ce4affSfengbojiang 					ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
3235a9643ea8Slogwang 				}
3236*22ce4affSfengbojiang 			}
3237*22ce4affSfengbojiang 		}
3238*22ce4affSfengbojiang 		if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP))
3239*22ce4affSfengbojiang                        ip->ip_sum = 0;
3240*22ce4affSfengbojiang 
3241a9643ea8Slogwang 		break;
3242a9643ea8Slogwang 	}
3243a9643ea8Slogwang #endif
3244a9643ea8Slogwang #ifdef INET6
3245a9643ea8Slogwang 	case ETHERTYPE_IPV6:
3246a9643ea8Slogwang 	{
3247a9643ea8Slogwang 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
3248a9643ea8Slogwang 		struct tcphdr *th;
3249a9643ea8Slogwang 		pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
3250a9643ea8Slogwang 
3251a9643ea8Slogwang 		if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
3252*22ce4affSfengbojiang 			txq->ift_pullups++;
3253a9643ea8Slogwang 			if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
3254a9643ea8Slogwang 				return (ENOMEM);
3255a9643ea8Slogwang 		}
3256a9643ea8Slogwang 		th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
3257a9643ea8Slogwang 
3258a9643ea8Slogwang 		/* XXX-BZ this will go badly in case of ext hdrs. */
3259a9643ea8Slogwang 		pi->ipi_ipproto = ip6->ip6_nxt;
3260a9643ea8Slogwang 		pi->ipi_flags |= IPI_TX_IPV6;
3261a9643ea8Slogwang 
3262*22ce4affSfengbojiang 		/* TCP checksum offload may require TCP header length */
3263*22ce4affSfengbojiang 		if (IS_TX_OFFLOAD6(pi)) {
3264a9643ea8Slogwang 			if (pi->ipi_ipproto == IPPROTO_TCP) {
3265a9643ea8Slogwang 				if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
3266*22ce4affSfengbojiang 					txq->ift_pullups++;
3267a9643ea8Slogwang 					if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
3268a9643ea8Slogwang 						return (ENOMEM);
3269a9643ea8Slogwang 				}
3270a9643ea8Slogwang 				pi->ipi_tcp_hflags = th->th_flags;
3271a9643ea8Slogwang 				pi->ipi_tcp_hlen = th->th_off << 2;
3272*22ce4affSfengbojiang 				pi->ipi_tcp_seq = th->th_seq;
3273a9643ea8Slogwang 			}
3274a9643ea8Slogwang 			if (IS_TSO6(pi)) {
3275a9643ea8Slogwang 				if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
3276a9643ea8Slogwang 					return (ENXIO);
3277a9643ea8Slogwang 				/*
3278*22ce4affSfengbojiang 				 * TSO always requires hardware checksum offload.
3279a9643ea8Slogwang 				 */
3280*22ce4affSfengbojiang 				pi->ipi_csum_flags |= CSUM_IP6_TCP;
3281a9643ea8Slogwang 				th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
3282a9643ea8Slogwang 				pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
3283a9643ea8Slogwang 			}
3284*22ce4affSfengbojiang 		}
3285a9643ea8Slogwang 		break;
3286a9643ea8Slogwang 	}
3287a9643ea8Slogwang #endif
3288a9643ea8Slogwang 	default:
3289a9643ea8Slogwang 		pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
3290a9643ea8Slogwang 		pi->ipi_ip_hlen = 0;
3291a9643ea8Slogwang 		break;
3292a9643ea8Slogwang 	}
3293a9643ea8Slogwang 	*mp = m;
3294*22ce4affSfengbojiang 
3295a9643ea8Slogwang 	return (0);
3296a9643ea8Slogwang }
3297a9643ea8Slogwang 
3298a9643ea8Slogwang /*
3299a9643ea8Slogwang  * If dodgy hardware rejects the scatter gather chain we've handed it
3300a9643ea8Slogwang  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
3301a9643ea8Slogwang  * m_defrag'd mbufs
3302a9643ea8Slogwang  */
3303a9643ea8Slogwang static __noinline struct mbuf *
iflib_remove_mbuf(iflib_txq_t txq)3304a9643ea8Slogwang iflib_remove_mbuf(iflib_txq_t txq)
3305a9643ea8Slogwang {
3306*22ce4affSfengbojiang 	int ntxd, pidx;
3307*22ce4affSfengbojiang 	struct mbuf *m, **ifsd_m;
3308a9643ea8Slogwang 
3309a9643ea8Slogwang 	ifsd_m = txq->ift_sds.ifsd_m;
3310a9643ea8Slogwang 	ntxd = txq->ift_size;
3311*22ce4affSfengbojiang 	pidx = txq->ift_pidx & (ntxd - 1);
3312*22ce4affSfengbojiang 	ifsd_m = txq->ift_sds.ifsd_m;
3313*22ce4affSfengbojiang 	m = ifsd_m[pidx];
3314a9643ea8Slogwang 	ifsd_m[pidx] = NULL;
3315*22ce4affSfengbojiang 	bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]);
3316*22ce4affSfengbojiang 	if (txq->ift_sds.ifsd_tso_map != NULL)
3317*22ce4affSfengbojiang 		bus_dmamap_unload(txq->ift_tso_buf_tag,
3318*22ce4affSfengbojiang 		    txq->ift_sds.ifsd_tso_map[pidx]);
3319a9643ea8Slogwang #if MEMORY_LOGGING
3320a9643ea8Slogwang 	txq->ift_dequeued++;
3321a9643ea8Slogwang #endif
3322*22ce4affSfengbojiang 	return (m);
3323a9643ea8Slogwang }
3324a9643ea8Slogwang 
3325*22ce4affSfengbojiang static inline caddr_t
calc_next_txd(iflib_txq_t txq,int cidx,uint8_t qid)3326*22ce4affSfengbojiang calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid)
3327a9643ea8Slogwang {
3328*22ce4affSfengbojiang 	qidx_t size;
3329*22ce4affSfengbojiang 	int ntxd;
3330*22ce4affSfengbojiang 	caddr_t start, end, cur, next;
3331a9643ea8Slogwang 
3332a9643ea8Slogwang 	ntxd = txq->ift_size;
3333*22ce4affSfengbojiang 	size = txq->ift_txd_size[qid];
3334*22ce4affSfengbojiang 	start = txq->ift_ifdi[qid].idi_vaddr;
3335a9643ea8Slogwang 
3336*22ce4affSfengbojiang 	if (__predict_false(size == 0))
3337*22ce4affSfengbojiang 		return (start);
3338*22ce4affSfengbojiang 	cur = start + size*cidx;
3339*22ce4affSfengbojiang 	end = start + size*ntxd;
3340*22ce4affSfengbojiang 	next = CACHE_PTR_NEXT(cur);
3341*22ce4affSfengbojiang 	return (next < end ? next : start);
3342a9643ea8Slogwang }
3343*22ce4affSfengbojiang 
3344a9643ea8Slogwang /*
3345*22ce4affSfengbojiang  * Pad an mbuf to ensure a minimum ethernet frame size.
3346*22ce4affSfengbojiang  * min_frame_size is the frame size (less CRC) to pad the mbuf to
3347a9643ea8Slogwang  */
3348*22ce4affSfengbojiang static __noinline int
iflib_ether_pad(device_t dev,struct mbuf ** m_head,uint16_t min_frame_size)3349*22ce4affSfengbojiang iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size)
3350*22ce4affSfengbojiang {
3351*22ce4affSfengbojiang 	/*
3352*22ce4affSfengbojiang 	 * 18 is enough bytes to pad an ARP packet to 46 bytes, and
3353*22ce4affSfengbojiang 	 * and ARP message is the smallest common payload I can think of
3354*22ce4affSfengbojiang 	 */
3355*22ce4affSfengbojiang 	static char pad[18];	/* just zeros */
3356*22ce4affSfengbojiang 	int n;
3357*22ce4affSfengbojiang 	struct mbuf *new_head;
3358*22ce4affSfengbojiang 
3359*22ce4affSfengbojiang 	if (!M_WRITABLE(*m_head)) {
3360*22ce4affSfengbojiang 		new_head = m_dup(*m_head, M_NOWAIT);
3361*22ce4affSfengbojiang 		if (new_head == NULL) {
3362*22ce4affSfengbojiang 			m_freem(*m_head);
3363*22ce4affSfengbojiang 			device_printf(dev, "cannot pad short frame, m_dup() failed");
3364*22ce4affSfengbojiang 			DBG_COUNTER_INC(encap_pad_mbuf_fail);
3365*22ce4affSfengbojiang 			DBG_COUNTER_INC(tx_frees);
3366*22ce4affSfengbojiang 			return ENOMEM;
3367a9643ea8Slogwang 		}
3368*22ce4affSfengbojiang 		m_freem(*m_head);
3369*22ce4affSfengbojiang 		*m_head = new_head;
3370a9643ea8Slogwang 	}
3371*22ce4affSfengbojiang 
3372*22ce4affSfengbojiang 	for (n = min_frame_size - (*m_head)->m_pkthdr.len;
3373*22ce4affSfengbojiang 	     n > 0; n -= sizeof(pad))
3374*22ce4affSfengbojiang 		if (!m_append(*m_head, min(n, sizeof(pad)), pad))
3375*22ce4affSfengbojiang 			break;
3376*22ce4affSfengbojiang 
3377*22ce4affSfengbojiang 	if (n > 0) {
3378*22ce4affSfengbojiang 		m_freem(*m_head);
3379*22ce4affSfengbojiang 		device_printf(dev, "cannot pad short frame\n");
3380*22ce4affSfengbojiang 		DBG_COUNTER_INC(encap_pad_mbuf_fail);
3381*22ce4affSfengbojiang 		DBG_COUNTER_INC(tx_frees);
3382*22ce4affSfengbojiang 		return (ENOBUFS);
3383*22ce4affSfengbojiang 	}
3384*22ce4affSfengbojiang 
3385*22ce4affSfengbojiang 	return 0;
3386a9643ea8Slogwang }
3387a9643ea8Slogwang 
3388a9643ea8Slogwang static int
iflib_encap(iflib_txq_t txq,struct mbuf ** m_headp)3389a9643ea8Slogwang iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
3390a9643ea8Slogwang {
3391a9643ea8Slogwang 	if_ctx_t		ctx;
3392a9643ea8Slogwang 	if_shared_ctx_t		sctx;
3393a9643ea8Slogwang 	if_softc_ctx_t		scctx;
3394*22ce4affSfengbojiang 	bus_dma_tag_t		buf_tag;
3395a9643ea8Slogwang 	bus_dma_segment_t	*segs;
3396*22ce4affSfengbojiang 	struct mbuf		*m_head, **ifsd_m;
3397*22ce4affSfengbojiang 	void			*next_txd;
3398a9643ea8Slogwang 	bus_dmamap_t		map;
3399a9643ea8Slogwang 	struct if_pkt_info	pi;
3400a9643ea8Slogwang 	int remap = 0;
3401a9643ea8Slogwang 	int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
3402a9643ea8Slogwang 
3403a9643ea8Slogwang 	ctx = txq->ift_ctx;
3404a9643ea8Slogwang 	sctx = ctx->ifc_sctx;
3405a9643ea8Slogwang 	scctx = &ctx->ifc_softc_ctx;
3406a9643ea8Slogwang 	segs = txq->ift_segs;
3407a9643ea8Slogwang 	ntxd = txq->ift_size;
3408a9643ea8Slogwang 	m_head = *m_headp;
3409a9643ea8Slogwang 	map = NULL;
3410a9643ea8Slogwang 
3411a9643ea8Slogwang 	/*
3412a9643ea8Slogwang 	 * If we're doing TSO the next descriptor to clean may be quite far ahead
3413a9643ea8Slogwang 	 */
3414a9643ea8Slogwang 	cidx = txq->ift_cidx;
3415a9643ea8Slogwang 	pidx = txq->ift_pidx;
3416*22ce4affSfengbojiang 	if (ctx->ifc_flags & IFC_PREFETCH) {
3417a9643ea8Slogwang 		next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
3418*22ce4affSfengbojiang 		if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) {
3419*22ce4affSfengbojiang 			next_txd = calc_next_txd(txq, cidx, 0);
3420*22ce4affSfengbojiang 			prefetch(next_txd);
3421*22ce4affSfengbojiang 		}
3422a9643ea8Slogwang 
3423a9643ea8Slogwang 		/* prefetch the next cache line of mbuf pointers and flags */
3424a9643ea8Slogwang 		prefetch(&txq->ift_sds.ifsd_m[next]);
3425a9643ea8Slogwang 		prefetch(&txq->ift_sds.ifsd_map[next]);
3426a9643ea8Slogwang 		next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
3427a9643ea8Slogwang 	}
3428*22ce4affSfengbojiang 	map = txq->ift_sds.ifsd_map[pidx];
3429*22ce4affSfengbojiang 	ifsd_m = txq->ift_sds.ifsd_m;
3430a9643ea8Slogwang 
3431a9643ea8Slogwang 	if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
3432*22ce4affSfengbojiang 		buf_tag = txq->ift_tso_buf_tag;
3433a9643ea8Slogwang 		max_segs = scctx->isc_tx_tso_segments_max;
3434*22ce4affSfengbojiang 		map = txq->ift_sds.ifsd_tso_map[pidx];
3435*22ce4affSfengbojiang 		MPASS(buf_tag != NULL);
3436*22ce4affSfengbojiang 		MPASS(max_segs > 0);
3437a9643ea8Slogwang 	} else {
3438*22ce4affSfengbojiang 		buf_tag = txq->ift_buf_tag;
3439a9643ea8Slogwang 		max_segs = scctx->isc_tx_nsegments;
3440*22ce4affSfengbojiang 		map = txq->ift_sds.ifsd_map[pidx];
3441*22ce4affSfengbojiang 	}
3442*22ce4affSfengbojiang 	if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) &&
3443*22ce4affSfengbojiang 	    __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) {
3444*22ce4affSfengbojiang 		err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size);
3445*22ce4affSfengbojiang 		if (err) {
3446*22ce4affSfengbojiang 			DBG_COUNTER_INC(encap_txd_encap_fail);
3447*22ce4affSfengbojiang 			return err;
3448*22ce4affSfengbojiang 		}
3449a9643ea8Slogwang 	}
3450a9643ea8Slogwang 	m_head = *m_headp;
3451*22ce4affSfengbojiang 
3452*22ce4affSfengbojiang 	pkt_info_zero(&pi);
3453a9643ea8Slogwang 	pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
3454a9643ea8Slogwang 	pi.ipi_pidx = pidx;
3455a9643ea8Slogwang 	pi.ipi_qsidx = txq->ift_id;
3456*22ce4affSfengbojiang 	pi.ipi_len = m_head->m_pkthdr.len;
3457*22ce4affSfengbojiang 	pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
3458*22ce4affSfengbojiang 	pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0;
3459a9643ea8Slogwang 
3460a9643ea8Slogwang 	/* deliberate bitwise OR to make one condition */
3461a9643ea8Slogwang 	if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
3462*22ce4affSfengbojiang 		if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) {
3463*22ce4affSfengbojiang 			DBG_COUNTER_INC(encap_txd_encap_fail);
3464a9643ea8Slogwang 			return (err);
3465*22ce4affSfengbojiang 		}
3466a9643ea8Slogwang 		m_head = *m_headp;
3467a9643ea8Slogwang 	}
3468a9643ea8Slogwang 
3469a9643ea8Slogwang retry:
3470*22ce4affSfengbojiang 	err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs,
3471*22ce4affSfengbojiang 	    BUS_DMA_NOWAIT);
3472a9643ea8Slogwang defrag:
3473a9643ea8Slogwang 	if (__predict_false(err)) {
3474a9643ea8Slogwang 		switch (err) {
3475a9643ea8Slogwang 		case EFBIG:
3476a9643ea8Slogwang 			/* try collapse once and defrag once */
3477*22ce4affSfengbojiang 			if (remap == 0) {
3478a9643ea8Slogwang 				m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
3479*22ce4affSfengbojiang 				/* try defrag if collapsing fails */
3480*22ce4affSfengbojiang 				if (m_head == NULL)
3481a9643ea8Slogwang 					remap++;
3482*22ce4affSfengbojiang 			}
3483*22ce4affSfengbojiang 			if (remap == 1) {
3484a9643ea8Slogwang 				txq->ift_mbuf_defrag++;
3485*22ce4affSfengbojiang 				m_head = m_defrag(*m_headp, M_NOWAIT);
3486*22ce4affSfengbojiang 			}
3487*22ce4affSfengbojiang 			/*
3488*22ce4affSfengbojiang 			 * remap should never be >1 unless bus_dmamap_load_mbuf_sg
3489*22ce4affSfengbojiang 			 * failed to map an mbuf that was run through m_defrag
3490*22ce4affSfengbojiang 			 */
3491*22ce4affSfengbojiang 			MPASS(remap <= 1);
3492*22ce4affSfengbojiang 			if (__predict_false(m_head == NULL || remap > 1))
3493*22ce4affSfengbojiang 				goto defrag_failed;
3494*22ce4affSfengbojiang 			remap++;
3495a9643ea8Slogwang 			*m_headp = m_head;
3496a9643ea8Slogwang 			goto retry;
3497a9643ea8Slogwang 			break;
3498a9643ea8Slogwang 		case ENOMEM:
3499a9643ea8Slogwang 			txq->ift_no_tx_dma_setup++;
3500a9643ea8Slogwang 			break;
3501a9643ea8Slogwang 		default:
3502a9643ea8Slogwang 			txq->ift_no_tx_dma_setup++;
3503a9643ea8Slogwang 			m_freem(*m_headp);
3504a9643ea8Slogwang 			DBG_COUNTER_INC(tx_frees);
3505a9643ea8Slogwang 			*m_headp = NULL;
3506a9643ea8Slogwang 			break;
3507a9643ea8Slogwang 		}
3508a9643ea8Slogwang 		txq->ift_map_failed++;
3509a9643ea8Slogwang 		DBG_COUNTER_INC(encap_load_mbuf_fail);
3510*22ce4affSfengbojiang 		DBG_COUNTER_INC(encap_txd_encap_fail);
3511a9643ea8Slogwang 		return (err);
3512a9643ea8Slogwang 	}
3513*22ce4affSfengbojiang 	ifsd_m[pidx] = m_head;
3514a9643ea8Slogwang 	/*
3515a9643ea8Slogwang 	 * XXX assumes a 1 to 1 relationship between segments and
3516a9643ea8Slogwang 	 *        descriptors - this does not hold true on all drivers, e.g.
3517a9643ea8Slogwang 	 *        cxgb
3518a9643ea8Slogwang 	 */
3519a9643ea8Slogwang 	if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
3520a9643ea8Slogwang 		txq->ift_no_desc_avail++;
3521*22ce4affSfengbojiang 		bus_dmamap_unload(buf_tag, map);
3522a9643ea8Slogwang 		DBG_COUNTER_INC(encap_txq_avail_fail);
3523*22ce4affSfengbojiang 		DBG_COUNTER_INC(encap_txd_encap_fail);
3524a9643ea8Slogwang 		if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
3525a9643ea8Slogwang 			GROUPTASK_ENQUEUE(&txq->ift_task);
3526a9643ea8Slogwang 		return (ENOBUFS);
3527a9643ea8Slogwang 	}
3528*22ce4affSfengbojiang 	/*
3529*22ce4affSfengbojiang 	 * On Intel cards we can greatly reduce the number of TX interrupts
3530*22ce4affSfengbojiang 	 * we see by only setting report status on every Nth descriptor.
3531*22ce4affSfengbojiang 	 * However, this also means that the driver will need to keep track
3532*22ce4affSfengbojiang 	 * of the descriptors that RS was set on to check them for the DD bit.
3533*22ce4affSfengbojiang 	 */
3534*22ce4affSfengbojiang 	txq->ift_rs_pending += nsegs + 1;
3535*22ce4affSfengbojiang 	if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) ||
3536*22ce4affSfengbojiang 	     iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) {
3537*22ce4affSfengbojiang 		pi.ipi_flags |= IPI_TX_INTR;
3538*22ce4affSfengbojiang 		txq->ift_rs_pending = 0;
3539*22ce4affSfengbojiang 	}
3540*22ce4affSfengbojiang 
3541a9643ea8Slogwang 	pi.ipi_segs = segs;
3542a9643ea8Slogwang 	pi.ipi_nsegs = nsegs;
3543a9643ea8Slogwang 
3544a9643ea8Slogwang 	MPASS(pidx >= 0 && pidx < txq->ift_size);
3545a9643ea8Slogwang #ifdef PKT_DEBUG
3546a9643ea8Slogwang 	print_pkt(&pi);
3547a9643ea8Slogwang #endif
3548a9643ea8Slogwang 	if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
3549*22ce4affSfengbojiang 		bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE);
3550a9643ea8Slogwang 		DBG_COUNTER_INC(tx_encap);
3551*22ce4affSfengbojiang 		MPASS(pi.ipi_new_pidx < txq->ift_size);
3552a9643ea8Slogwang 
3553a9643ea8Slogwang 		ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
3554a9643ea8Slogwang 		if (pi.ipi_new_pidx < pi.ipi_pidx) {
3555a9643ea8Slogwang 			ndesc += txq->ift_size;
3556a9643ea8Slogwang 			txq->ift_gen = 1;
3557a9643ea8Slogwang 		}
3558*22ce4affSfengbojiang 		/*
3559*22ce4affSfengbojiang 		 * drivers can need as many as
3560*22ce4affSfengbojiang 		 * two sentinels
3561*22ce4affSfengbojiang 		 */
3562*22ce4affSfengbojiang 		MPASS(ndesc <= pi.ipi_nsegs + 2);
3563a9643ea8Slogwang 		MPASS(pi.ipi_new_pidx != pidx);
3564a9643ea8Slogwang 		MPASS(ndesc > 0);
3565a9643ea8Slogwang 		txq->ift_in_use += ndesc;
3566*22ce4affSfengbojiang 		txq->ift_db_pending += ndesc;
3567*22ce4affSfengbojiang 
3568a9643ea8Slogwang 		/*
3569a9643ea8Slogwang 		 * We update the last software descriptor again here because there may
3570a9643ea8Slogwang 		 * be a sentinel and/or there may be more mbufs than segments
3571a9643ea8Slogwang 		 */
3572a9643ea8Slogwang 		txq->ift_pidx = pi.ipi_new_pidx;
3573a9643ea8Slogwang 		txq->ift_npending += pi.ipi_ndescs;
3574*22ce4affSfengbojiang 	} else {
3575a9643ea8Slogwang 		*m_headp = m_head = iflib_remove_mbuf(txq);
3576*22ce4affSfengbojiang 		if (err == EFBIG) {
3577a9643ea8Slogwang 			txq->ift_txd_encap_efbig++;
3578*22ce4affSfengbojiang 			if (remap < 2) {
3579*22ce4affSfengbojiang 				remap = 1;
3580a9643ea8Slogwang 				goto defrag;
3581*22ce4affSfengbojiang 			}
3582*22ce4affSfengbojiang 		}
3583*22ce4affSfengbojiang 		goto defrag_failed;
3584*22ce4affSfengbojiang 	}
3585*22ce4affSfengbojiang 	/*
3586*22ce4affSfengbojiang 	 * err can't possibly be non-zero here, so we don't neet to test it
3587*22ce4affSfengbojiang 	 * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail).
3588*22ce4affSfengbojiang 	 */
3589a9643ea8Slogwang 	return (err);
3590a9643ea8Slogwang 
3591a9643ea8Slogwang defrag_failed:
3592a9643ea8Slogwang 	txq->ift_mbuf_defrag_failed++;
3593a9643ea8Slogwang 	txq->ift_map_failed++;
3594a9643ea8Slogwang 	m_freem(*m_headp);
3595a9643ea8Slogwang 	DBG_COUNTER_INC(tx_frees);
3596a9643ea8Slogwang 	*m_headp = NULL;
3597*22ce4affSfengbojiang 	DBG_COUNTER_INC(encap_txd_encap_fail);
3598a9643ea8Slogwang 	return (ENOMEM);
3599a9643ea8Slogwang }
3600a9643ea8Slogwang 
3601a9643ea8Slogwang static void
iflib_tx_desc_free(iflib_txq_t txq,int n)3602a9643ea8Slogwang iflib_tx_desc_free(iflib_txq_t txq, int n)
3603a9643ea8Slogwang {
3604a9643ea8Slogwang 	uint32_t qsize, cidx, mask, gen;
3605a9643ea8Slogwang 	struct mbuf *m, **ifsd_m;
3606*22ce4affSfengbojiang 	bool do_prefetch;
3607a9643ea8Slogwang 
3608a9643ea8Slogwang 	cidx = txq->ift_cidx;
3609a9643ea8Slogwang 	gen = txq->ift_gen;
3610a9643ea8Slogwang 	qsize = txq->ift_size;
3611a9643ea8Slogwang 	mask = qsize-1;
3612a9643ea8Slogwang 	ifsd_m = txq->ift_sds.ifsd_m;
3613*22ce4affSfengbojiang 	do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH);
3614a9643ea8Slogwang 
3615*22ce4affSfengbojiang 	while (n-- > 0) {
3616*22ce4affSfengbojiang 		if (do_prefetch) {
3617a9643ea8Slogwang 			prefetch(ifsd_m[(cidx + 3) & mask]);
3618a9643ea8Slogwang 			prefetch(ifsd_m[(cidx + 4) & mask]);
3619a9643ea8Slogwang 		}
3620a9643ea8Slogwang 		if ((m = ifsd_m[cidx]) != NULL) {
3621*22ce4affSfengbojiang 			prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
3622*22ce4affSfengbojiang 			if (m->m_pkthdr.csum_flags & CSUM_TSO) {
3623*22ce4affSfengbojiang 				bus_dmamap_sync(txq->ift_tso_buf_tag,
3624*22ce4affSfengbojiang 				    txq->ift_sds.ifsd_tso_map[cidx],
3625*22ce4affSfengbojiang 				    BUS_DMASYNC_POSTWRITE);
3626*22ce4affSfengbojiang 				bus_dmamap_unload(txq->ift_tso_buf_tag,
3627*22ce4affSfengbojiang 				    txq->ift_sds.ifsd_tso_map[cidx]);
3628*22ce4affSfengbojiang 			} else {
3629*22ce4affSfengbojiang 				bus_dmamap_sync(txq->ift_buf_tag,
3630*22ce4affSfengbojiang 				    txq->ift_sds.ifsd_map[cidx],
3631*22ce4affSfengbojiang 				    BUS_DMASYNC_POSTWRITE);
3632*22ce4affSfengbojiang 				bus_dmamap_unload(txq->ift_buf_tag,
3633*22ce4affSfengbojiang 				    txq->ift_sds.ifsd_map[cidx]);
3634*22ce4affSfengbojiang 			}
3635a9643ea8Slogwang 			/* XXX we don't support any drivers that batch packets yet */
3636a9643ea8Slogwang 			MPASS(m->m_nextpkt == NULL);
3637*22ce4affSfengbojiang 			m_freem(m);
3638a9643ea8Slogwang 			ifsd_m[cidx] = NULL;
3639a9643ea8Slogwang #if MEMORY_LOGGING
3640a9643ea8Slogwang 			txq->ift_dequeued++;
3641a9643ea8Slogwang #endif
3642a9643ea8Slogwang 			DBG_COUNTER_INC(tx_frees);
3643a9643ea8Slogwang 		}
3644a9643ea8Slogwang 		if (__predict_false(++cidx == qsize)) {
3645a9643ea8Slogwang 			cidx = 0;
3646a9643ea8Slogwang 			gen = 0;
3647a9643ea8Slogwang 		}
3648a9643ea8Slogwang 	}
3649a9643ea8Slogwang 	txq->ift_cidx = cidx;
3650a9643ea8Slogwang 	txq->ift_gen = gen;
3651a9643ea8Slogwang }
3652a9643ea8Slogwang 
3653a9643ea8Slogwang static __inline int
iflib_completed_tx_reclaim(iflib_txq_t txq,int thresh)3654a9643ea8Slogwang iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
3655a9643ea8Slogwang {
3656a9643ea8Slogwang 	int reclaim;
3657a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
3658a9643ea8Slogwang 
3659a9643ea8Slogwang 	KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
3660a9643ea8Slogwang 	MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
3661a9643ea8Slogwang 
3662a9643ea8Slogwang 	/*
3663a9643ea8Slogwang 	 * Need a rate-limiting check so that this isn't called every time
3664a9643ea8Slogwang 	 */
3665a9643ea8Slogwang 	iflib_tx_credits_update(ctx, txq);
3666a9643ea8Slogwang 	reclaim = DESC_RECLAIMABLE(txq);
3667a9643ea8Slogwang 
3668a9643ea8Slogwang 	if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
3669a9643ea8Slogwang #ifdef INVARIANTS
3670a9643ea8Slogwang 		if (iflib_verbose_debug) {
3671a9643ea8Slogwang 			printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
3672a9643ea8Slogwang 			       txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
3673a9643ea8Slogwang 			       reclaim, thresh);
3674a9643ea8Slogwang 		}
3675a9643ea8Slogwang #endif
3676a9643ea8Slogwang 		return (0);
3677a9643ea8Slogwang 	}
3678a9643ea8Slogwang 	iflib_tx_desc_free(txq, reclaim);
3679a9643ea8Slogwang 	txq->ift_cleaned += reclaim;
3680a9643ea8Slogwang 	txq->ift_in_use -= reclaim;
3681a9643ea8Slogwang 
3682a9643ea8Slogwang 	return (reclaim);
3683a9643ea8Slogwang }
3684a9643ea8Slogwang 
3685a9643ea8Slogwang static struct mbuf **
_ring_peek_one(struct ifmp_ring * r,int cidx,int offset,int remaining)3686*22ce4affSfengbojiang _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
3687a9643ea8Slogwang {
3688*22ce4affSfengbojiang 	int next, size;
3689*22ce4affSfengbojiang 	struct mbuf **items;
3690a9643ea8Slogwang 
3691*22ce4affSfengbojiang 	size = r->size;
3692*22ce4affSfengbojiang 	next = (cidx + CACHE_PTR_INCREMENT) & (size-1);
3693*22ce4affSfengbojiang 	items = __DEVOLATILE(struct mbuf **, &r->items[0]);
3694*22ce4affSfengbojiang 
3695*22ce4affSfengbojiang 	prefetch(items[(cidx + offset) & (size-1)]);
3696*22ce4affSfengbojiang 	if (remaining > 1) {
3697*22ce4affSfengbojiang 		prefetch2cachelines(&items[next]);
3698*22ce4affSfengbojiang 		prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]);
3699*22ce4affSfengbojiang 		prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]);
3700*22ce4affSfengbojiang 		prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]);
3701*22ce4affSfengbojiang 	}
3702*22ce4affSfengbojiang 	return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)]));
3703a9643ea8Slogwang }
3704a9643ea8Slogwang 
3705a9643ea8Slogwang static void
iflib_txq_check_drain(iflib_txq_t txq,int budget)3706a9643ea8Slogwang iflib_txq_check_drain(iflib_txq_t txq, int budget)
3707a9643ea8Slogwang {
3708a9643ea8Slogwang 
3709*22ce4affSfengbojiang 	ifmp_ring_check_drainage(txq->ift_br, budget);
3710a9643ea8Slogwang }
3711a9643ea8Slogwang 
3712a9643ea8Slogwang static uint32_t
iflib_txq_can_drain(struct ifmp_ring * r)3713a9643ea8Slogwang iflib_txq_can_drain(struct ifmp_ring *r)
3714a9643ea8Slogwang {
3715a9643ea8Slogwang 	iflib_txq_t txq = r->cookie;
3716a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
3717a9643ea8Slogwang 
3718*22ce4affSfengbojiang 	if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2)
3719*22ce4affSfengbojiang 		return (1);
3720*22ce4affSfengbojiang 	bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
3721*22ce4affSfengbojiang 	    BUS_DMASYNC_POSTREAD);
3722*22ce4affSfengbojiang 	return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id,
3723*22ce4affSfengbojiang 	    false));
3724a9643ea8Slogwang }
3725a9643ea8Slogwang 
3726a9643ea8Slogwang static uint32_t
iflib_txq_drain(struct ifmp_ring * r,uint32_t cidx,uint32_t pidx)3727a9643ea8Slogwang iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3728a9643ea8Slogwang {
3729a9643ea8Slogwang 	iflib_txq_t txq = r->cookie;
3730a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
3731a9643ea8Slogwang 	if_t ifp = ctx->ifc_ifp;
3732*22ce4affSfengbojiang 	struct mbuf *m, **mp;
3733*22ce4affSfengbojiang 	int avail, bytes_sent, skipped, count, err, i;
3734*22ce4affSfengbojiang 	int mcast_sent, pkt_sent, reclaimed;
3735*22ce4affSfengbojiang 	bool do_prefetch, rang, ring;
3736a9643ea8Slogwang 
3737a9643ea8Slogwang 	if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
3738a9643ea8Slogwang 			    !LINK_ACTIVE(ctx))) {
3739a9643ea8Slogwang 		DBG_COUNTER_INC(txq_drain_notready);
3740a9643ea8Slogwang 		return (0);
3741a9643ea8Slogwang 	}
3742*22ce4affSfengbojiang 	reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
3743*22ce4affSfengbojiang 	rang = iflib_txd_db_check(txq, reclaimed && txq->ift_db_pending);
3744a9643ea8Slogwang 	avail = IDXDIFF(pidx, cidx, r->size);
3745*22ce4affSfengbojiang 
3746a9643ea8Slogwang 	if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
3747*22ce4affSfengbojiang 		/*
3748*22ce4affSfengbojiang 		 * The driver is unloading so we need to free all pending packets.
3749*22ce4affSfengbojiang 		 */
3750a9643ea8Slogwang 		DBG_COUNTER_INC(txq_drain_flushing);
3751a9643ea8Slogwang 		for (i = 0; i < avail; i++) {
3752*22ce4affSfengbojiang 			if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq))
3753*22ce4affSfengbojiang 				m_freem(r->items[(cidx + i) & (r->size-1)]);
3754a9643ea8Slogwang 			r->items[(cidx + i) & (r->size-1)] = NULL;
3755a9643ea8Slogwang 		}
3756a9643ea8Slogwang 		return (avail);
3757a9643ea8Slogwang 	}
3758*22ce4affSfengbojiang 
3759a9643ea8Slogwang 	if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
3760a9643ea8Slogwang 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3761a9643ea8Slogwang 		CALLOUT_LOCK(txq);
3762a9643ea8Slogwang 		callout_stop(&txq->ift_timer);
3763a9643ea8Slogwang 		CALLOUT_UNLOCK(txq);
3764a9643ea8Slogwang 		DBG_COUNTER_INC(txq_drain_oactive);
3765a9643ea8Slogwang 		return (0);
3766a9643ea8Slogwang 	}
3767a9643ea8Slogwang 
3768a9643ea8Slogwang 	/*
3769*22ce4affSfengbojiang 	 * If we've reclaimed any packets this queue cannot be hung.
3770a9643ea8Slogwang 	 */
3771*22ce4affSfengbojiang 	if (reclaimed)
3772*22ce4affSfengbojiang 		txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3773*22ce4affSfengbojiang 	skipped = mcast_sent = bytes_sent = pkt_sent = 0;
3774*22ce4affSfengbojiang 	count = MIN(avail, TX_BATCH_SIZE);
3775*22ce4affSfengbojiang #ifdef INVARIANTS
3776*22ce4affSfengbojiang 	if (iflib_verbose_debug)
3777*22ce4affSfengbojiang 		printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3778*22ce4affSfengbojiang 		       avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3779*22ce4affSfengbojiang #endif
3780*22ce4affSfengbojiang 	do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
3781*22ce4affSfengbojiang 	err = 0;
3782*22ce4affSfengbojiang 	for (i = 0; i < count && TXQ_AVAIL(txq) >= MAX_TX_DESC(ctx) + 2; i++) {
3783*22ce4affSfengbojiang 		int rem = do_prefetch ? count - i : 0;
3784a9643ea8Slogwang 
3785*22ce4affSfengbojiang 		mp = _ring_peek_one(r, cidx, i, rem);
3786*22ce4affSfengbojiang 		MPASS(mp != NULL && *mp != NULL);
3787*22ce4affSfengbojiang 
3788*22ce4affSfengbojiang 		/*
3789*22ce4affSfengbojiang 		 * Completion interrupts will use the address of the txq
3790*22ce4affSfengbojiang 		 * as a sentinel to enqueue _something_ in order to acquire
3791*22ce4affSfengbojiang 		 * the lock on the mp_ring (there's no direct lock call).
3792*22ce4affSfengbojiang 		 * We obviously whave to check for these sentinel cases
3793*22ce4affSfengbojiang 		 * and skip them.
3794*22ce4affSfengbojiang 		 */
3795*22ce4affSfengbojiang 		if (__predict_false(*mp == (struct mbuf *)txq)) {
3796*22ce4affSfengbojiang 			skipped++;
3797*22ce4affSfengbojiang 			continue;
3798*22ce4affSfengbojiang 		}
3799*22ce4affSfengbojiang 		err = iflib_encap(txq, mp);
3800*22ce4affSfengbojiang 		if (__predict_false(err)) {
3801*22ce4affSfengbojiang 			/* no room - bail out */
3802*22ce4affSfengbojiang 			if (err == ENOBUFS)
3803*22ce4affSfengbojiang 				break;
3804*22ce4affSfengbojiang 			skipped++;
3805*22ce4affSfengbojiang 			/* we can't send this packet - skip it */
3806*22ce4affSfengbojiang 			continue;
3807*22ce4affSfengbojiang 		}
3808a9643ea8Slogwang 		pkt_sent++;
3809a9643ea8Slogwang 		m = *mp;
3810a9643ea8Slogwang 		DBG_COUNTER_INC(tx_sent);
3811a9643ea8Slogwang 		bytes_sent += m->m_pkthdr.len;
3812*22ce4affSfengbojiang 		mcast_sent += !!(m->m_flags & M_MCAST);
3813a9643ea8Slogwang 
3814*22ce4affSfengbojiang 		if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING)))
3815*22ce4affSfengbojiang 			break;
3816a9643ea8Slogwang 		ETHER_BPF_MTAP(ifp, m);
3817*22ce4affSfengbojiang 		rang = iflib_txd_db_check(txq, false);
3818a9643ea8Slogwang 	}
3819a9643ea8Slogwang 
3820*22ce4affSfengbojiang 	/* deliberate use of bitwise or to avoid gratuitous short-circuit */
3821*22ce4affSfengbojiang 	ring = rang ? false  : (iflib_min_tx_latency | err);
3822*22ce4affSfengbojiang 	iflib_txd_db_check(txq, ring);
3823a9643ea8Slogwang 	if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
3824a9643ea8Slogwang 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
3825a9643ea8Slogwang 	if (mcast_sent)
3826a9643ea8Slogwang 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3827*22ce4affSfengbojiang #ifdef INVARIANTS
3828*22ce4affSfengbojiang 	if (iflib_verbose_debug)
3829*22ce4affSfengbojiang 		printf("consumed=%d\n", skipped + pkt_sent);
3830*22ce4affSfengbojiang #endif
3831*22ce4affSfengbojiang 	return (skipped + pkt_sent);
3832*22ce4affSfengbojiang }
3833a9643ea8Slogwang 
3834*22ce4affSfengbojiang static uint32_t
iflib_txq_drain_always(struct ifmp_ring * r)3835*22ce4affSfengbojiang iflib_txq_drain_always(struct ifmp_ring *r)
3836*22ce4affSfengbojiang {
3837*22ce4affSfengbojiang 	return (1);
3838*22ce4affSfengbojiang }
3839*22ce4affSfengbojiang 
3840*22ce4affSfengbojiang static uint32_t
iflib_txq_drain_free(struct ifmp_ring * r,uint32_t cidx,uint32_t pidx)3841*22ce4affSfengbojiang iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3842*22ce4affSfengbojiang {
3843*22ce4affSfengbojiang 	int i, avail;
3844*22ce4affSfengbojiang 	struct mbuf **mp;
3845*22ce4affSfengbojiang 	iflib_txq_t txq;
3846*22ce4affSfengbojiang 
3847*22ce4affSfengbojiang 	txq = r->cookie;
3848*22ce4affSfengbojiang 
3849*22ce4affSfengbojiang 	txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3850*22ce4affSfengbojiang 	CALLOUT_LOCK(txq);
3851*22ce4affSfengbojiang 	callout_stop(&txq->ift_timer);
3852*22ce4affSfengbojiang 	CALLOUT_UNLOCK(txq);
3853*22ce4affSfengbojiang 
3854*22ce4affSfengbojiang 	avail = IDXDIFF(pidx, cidx, r->size);
3855*22ce4affSfengbojiang 	for (i = 0; i < avail; i++) {
3856*22ce4affSfengbojiang 		mp = _ring_peek_one(r, cidx, i, avail - i);
3857*22ce4affSfengbojiang 		if (__predict_false(*mp == (struct mbuf *)txq))
3858*22ce4affSfengbojiang 			continue;
3859*22ce4affSfengbojiang 		m_freem(*mp);
3860*22ce4affSfengbojiang 		DBG_COUNTER_INC(tx_frees);
3861*22ce4affSfengbojiang 	}
3862*22ce4affSfengbojiang 	MPASS(ifmp_ring_is_stalled(r) == 0);
3863*22ce4affSfengbojiang 	return (avail);
3864*22ce4affSfengbojiang }
3865*22ce4affSfengbojiang 
3866*22ce4affSfengbojiang static void
iflib_ifmp_purge(iflib_txq_t txq)3867*22ce4affSfengbojiang iflib_ifmp_purge(iflib_txq_t txq)
3868*22ce4affSfengbojiang {
3869*22ce4affSfengbojiang 	struct ifmp_ring *r;
3870*22ce4affSfengbojiang 
3871*22ce4affSfengbojiang 	r = txq->ift_br;
3872*22ce4affSfengbojiang 	r->drain = iflib_txq_drain_free;
3873*22ce4affSfengbojiang 	r->can_drain = iflib_txq_drain_always;
3874*22ce4affSfengbojiang 
3875*22ce4affSfengbojiang 	ifmp_ring_check_drainage(r, r->size);
3876*22ce4affSfengbojiang 
3877*22ce4affSfengbojiang 	r->drain = iflib_txq_drain;
3878*22ce4affSfengbojiang 	r->can_drain = iflib_txq_can_drain;
3879a9643ea8Slogwang }
3880a9643ea8Slogwang 
3881a9643ea8Slogwang static void
_task_fn_tx(void * context)3882a9643ea8Slogwang _task_fn_tx(void *context)
3883a9643ea8Slogwang {
3884a9643ea8Slogwang 	iflib_txq_t txq = context;
3885a9643ea8Slogwang 	if_ctx_t ctx = txq->ift_ctx;
3886*22ce4affSfengbojiang 	if_t ifp = ctx->ifc_ifp;
3887*22ce4affSfengbojiang 	int abdicate = ctx->ifc_sysctl_tx_abdicate;
3888a9643ea8Slogwang 
3889*22ce4affSfengbojiang #ifdef IFLIB_DIAGNOSTICS
3890*22ce4affSfengbojiang 	txq->ift_cpu_exec_count[curcpu]++;
3891*22ce4affSfengbojiang #endif
3892*22ce4affSfengbojiang 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
3893a9643ea8Slogwang 		return;
3894*22ce4affSfengbojiang #ifdef DEV_NETMAP
3895*22ce4affSfengbojiang 	if ((if_getcapenable(ifp) & IFCAP_NETMAP) &&
3896*22ce4affSfengbojiang 	    netmap_tx_irq(ifp, txq->ift_id))
3897*22ce4affSfengbojiang 		goto skip_ifmp;
3898*22ce4affSfengbojiang #endif
3899*22ce4affSfengbojiang #ifdef ALTQ
3900*22ce4affSfengbojiang 	if (ALTQ_IS_ENABLED(&ifp->if_snd))
3901*22ce4affSfengbojiang 		iflib_altq_if_start(ifp);
3902*22ce4affSfengbojiang #endif
3903*22ce4affSfengbojiang 	if (txq->ift_db_pending)
3904*22ce4affSfengbojiang 		ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate);
3905*22ce4affSfengbojiang 	else if (!abdicate)
3906*22ce4affSfengbojiang 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
3907*22ce4affSfengbojiang 	/*
3908*22ce4affSfengbojiang 	 * When abdicating, we always need to check drainage, not just when we don't enqueue
3909*22ce4affSfengbojiang 	 */
3910*22ce4affSfengbojiang 	if (abdicate)
3911*22ce4affSfengbojiang 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
3912*22ce4affSfengbojiang #ifdef DEV_NETMAP
3913*22ce4affSfengbojiang skip_ifmp:
3914*22ce4affSfengbojiang #endif
3915*22ce4affSfengbojiang 	if (ctx->ifc_flags & IFC_LEGACY)
3916*22ce4affSfengbojiang 		IFDI_INTR_ENABLE(ctx);
3917*22ce4affSfengbojiang 	else
3918*22ce4affSfengbojiang 		IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
3919a9643ea8Slogwang }
3920a9643ea8Slogwang 
3921a9643ea8Slogwang static void
_task_fn_rx(void * context)3922a9643ea8Slogwang _task_fn_rx(void *context)
3923a9643ea8Slogwang {
3924a9643ea8Slogwang 	iflib_rxq_t rxq = context;
3925a9643ea8Slogwang 	if_ctx_t ctx = rxq->ifr_ctx;
3926*22ce4affSfengbojiang 	uint8_t more;
3927*22ce4affSfengbojiang 	uint16_t budget;
3928*22ce4affSfengbojiang #ifdef DEV_NETMAP
3929*22ce4affSfengbojiang 	u_int work = 0;
3930*22ce4affSfengbojiang 	int nmirq;
3931*22ce4affSfengbojiang #endif
3932a9643ea8Slogwang 
3933*22ce4affSfengbojiang #ifdef IFLIB_DIAGNOSTICS
3934*22ce4affSfengbojiang 	rxq->ifr_cpu_exec_count[curcpu]++;
3935*22ce4affSfengbojiang #endif
3936a9643ea8Slogwang 	DBG_COUNTER_INC(task_fn_rxs);
3937a9643ea8Slogwang 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
3938a9643ea8Slogwang 		return;
3939*22ce4affSfengbojiang #ifdef DEV_NETMAP
3940*22ce4affSfengbojiang 	nmirq = netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work);
3941*22ce4affSfengbojiang 	if (nmirq != NM_IRQ_PASS) {
3942*22ce4affSfengbojiang 		more = (nmirq == NM_IRQ_RESCHED) ? IFLIB_RXEOF_MORE : 0;
3943*22ce4affSfengbojiang 		goto skip_rxeof;
3944*22ce4affSfengbojiang 	}
3945*22ce4affSfengbojiang #endif
3946*22ce4affSfengbojiang 	budget = ctx->ifc_sysctl_rx_budget;
3947*22ce4affSfengbojiang 	if (budget == 0)
3948*22ce4affSfengbojiang 		budget = 16;	/* XXX */
3949*22ce4affSfengbojiang 	more = iflib_rxeof(rxq, budget);
3950*22ce4affSfengbojiang #ifdef DEV_NETMAP
3951*22ce4affSfengbojiang skip_rxeof:
3952*22ce4affSfengbojiang #endif
3953*22ce4affSfengbojiang 	if ((more & IFLIB_RXEOF_MORE) == 0) {
3954a9643ea8Slogwang 		if (ctx->ifc_flags & IFC_LEGACY)
3955a9643ea8Slogwang 			IFDI_INTR_ENABLE(ctx);
3956*22ce4affSfengbojiang 		else
3957*22ce4affSfengbojiang 			IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
3958a9643ea8Slogwang 		DBG_COUNTER_INC(rx_intr_enables);
3959a9643ea8Slogwang 	}
3960a9643ea8Slogwang 	if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
3961a9643ea8Slogwang 		return;
3962*22ce4affSfengbojiang 
3963*22ce4affSfengbojiang 	if (more & IFLIB_RXEOF_MORE)
3964a9643ea8Slogwang 		GROUPTASK_ENQUEUE(&rxq->ifr_task);
3965*22ce4affSfengbojiang 	else if (more & IFLIB_RXEOF_EMPTY)
3966*22ce4affSfengbojiang 		callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq);
3967a9643ea8Slogwang }
3968a9643ea8Slogwang 
3969a9643ea8Slogwang static void
_task_fn_admin(void * context)3970a9643ea8Slogwang _task_fn_admin(void *context)
3971a9643ea8Slogwang {
3972a9643ea8Slogwang 	if_ctx_t ctx = context;
3973a9643ea8Slogwang 	if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
3974a9643ea8Slogwang 	iflib_txq_t txq;
3975a9643ea8Slogwang 	int i;
3976*22ce4affSfengbojiang 	bool oactive, running, do_reset, do_watchdog, in_detach;
3977a9643ea8Slogwang 
3978*22ce4affSfengbojiang 	STATE_LOCK(ctx);
3979*22ce4affSfengbojiang 	running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING);
3980*22ce4affSfengbojiang 	oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE);
3981*22ce4affSfengbojiang 	do_reset = (ctx->ifc_flags & IFC_DO_RESET);
3982*22ce4affSfengbojiang 	do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG);
3983*22ce4affSfengbojiang 	in_detach = (ctx->ifc_flags & IFC_IN_DETACH);
3984*22ce4affSfengbojiang 	ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG);
3985*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
3986*22ce4affSfengbojiang 
3987*22ce4affSfengbojiang 	if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))
3988*22ce4affSfengbojiang 		return;
3989*22ce4affSfengbojiang 	if (in_detach)
3990a9643ea8Slogwang 		return;
3991a9643ea8Slogwang 
3992a9643ea8Slogwang 	CTX_LOCK(ctx);
3993a9643ea8Slogwang 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
3994a9643ea8Slogwang 		CALLOUT_LOCK(txq);
3995a9643ea8Slogwang 		callout_stop(&txq->ift_timer);
3996a9643ea8Slogwang 		CALLOUT_UNLOCK(txq);
3997a9643ea8Slogwang 	}
3998*22ce4affSfengbojiang 	if (do_watchdog) {
3999*22ce4affSfengbojiang 		ctx->ifc_watchdog_events++;
4000*22ce4affSfengbojiang 		IFDI_WATCHDOG_RESET(ctx);
4001*22ce4affSfengbojiang 	}
4002a9643ea8Slogwang 	IFDI_UPDATE_ADMIN_STATUS(ctx);
4003*22ce4affSfengbojiang 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
4004*22ce4affSfengbojiang 		callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq,
4005*22ce4affSfengbojiang 		    txq->ift_timer.c_cpu);
4006*22ce4affSfengbojiang 	}
4007a9643ea8Slogwang 	IFDI_LINK_INTR_ENABLE(ctx);
4008*22ce4affSfengbojiang 	if (do_reset)
4009*22ce4affSfengbojiang 		iflib_if_init_locked(ctx);
4010a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4011a9643ea8Slogwang 
4012a9643ea8Slogwang 	if (LINK_ACTIVE(ctx) == 0)
4013a9643ea8Slogwang 		return;
4014a9643ea8Slogwang 	for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
4015a9643ea8Slogwang 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
4016a9643ea8Slogwang }
4017a9643ea8Slogwang 
4018a9643ea8Slogwang static void
_task_fn_iov(void * context)4019a9643ea8Slogwang _task_fn_iov(void *context)
4020a9643ea8Slogwang {
4021a9643ea8Slogwang 	if_ctx_t ctx = context;
4022a9643ea8Slogwang 
4023*22ce4affSfengbojiang 	if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) &&
4024*22ce4affSfengbojiang 	    !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))
4025a9643ea8Slogwang 		return;
4026a9643ea8Slogwang 
4027a9643ea8Slogwang 	CTX_LOCK(ctx);
4028a9643ea8Slogwang 	IFDI_VFLR_HANDLE(ctx);
4029a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4030a9643ea8Slogwang }
4031a9643ea8Slogwang 
4032a9643ea8Slogwang static int
iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)4033a9643ea8Slogwang iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
4034a9643ea8Slogwang {
4035a9643ea8Slogwang 	int err;
4036a9643ea8Slogwang 	if_int_delay_info_t info;
4037a9643ea8Slogwang 	if_ctx_t ctx;
4038a9643ea8Slogwang 
4039a9643ea8Slogwang 	info = (if_int_delay_info_t)arg1;
4040a9643ea8Slogwang 	ctx = info->iidi_ctx;
4041a9643ea8Slogwang 	info->iidi_req = req;
4042a9643ea8Slogwang 	info->iidi_oidp = oidp;
4043a9643ea8Slogwang 	CTX_LOCK(ctx);
4044a9643ea8Slogwang 	err = IFDI_SYSCTL_INT_DELAY(ctx, info);
4045a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4046a9643ea8Slogwang 	return (err);
4047a9643ea8Slogwang }
4048a9643ea8Slogwang 
4049a9643ea8Slogwang /*********************************************************************
4050a9643ea8Slogwang  *
4051a9643ea8Slogwang  *  IFNET FUNCTIONS
4052a9643ea8Slogwang  *
4053a9643ea8Slogwang  **********************************************************************/
4054a9643ea8Slogwang 
4055a9643ea8Slogwang static void
iflib_if_init_locked(if_ctx_t ctx)4056a9643ea8Slogwang iflib_if_init_locked(if_ctx_t ctx)
4057a9643ea8Slogwang {
4058a9643ea8Slogwang 	iflib_stop(ctx);
4059a9643ea8Slogwang 	iflib_init_locked(ctx);
4060a9643ea8Slogwang }
4061a9643ea8Slogwang 
4062a9643ea8Slogwang static void
iflib_if_init(void * arg)4063a9643ea8Slogwang iflib_if_init(void *arg)
4064a9643ea8Slogwang {
4065a9643ea8Slogwang 	if_ctx_t ctx = arg;
4066a9643ea8Slogwang 
4067a9643ea8Slogwang 	CTX_LOCK(ctx);
4068a9643ea8Slogwang 	iflib_if_init_locked(ctx);
4069a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4070a9643ea8Slogwang }
4071a9643ea8Slogwang 
4072a9643ea8Slogwang static int
iflib_if_transmit(if_t ifp,struct mbuf * m)4073a9643ea8Slogwang iflib_if_transmit(if_t ifp, struct mbuf *m)
4074a9643ea8Slogwang {
4075a9643ea8Slogwang 	if_ctx_t	ctx = if_getsoftc(ifp);
4076a9643ea8Slogwang 
4077a9643ea8Slogwang 	iflib_txq_t txq;
4078a9643ea8Slogwang 	int err, qidx;
4079*22ce4affSfengbojiang 	int abdicate = ctx->ifc_sysctl_tx_abdicate;
4080a9643ea8Slogwang 
4081a9643ea8Slogwang 	if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
4082a9643ea8Slogwang 		DBG_COUNTER_INC(tx_frees);
4083a9643ea8Slogwang 		m_freem(m);
4084*22ce4affSfengbojiang 		return (ENETDOWN);
4085a9643ea8Slogwang 	}
4086a9643ea8Slogwang 
4087a9643ea8Slogwang 	MPASS(m->m_nextpkt == NULL);
4088*22ce4affSfengbojiang 	/* ALTQ-enabled interfaces always use queue 0. */
4089a9643ea8Slogwang 	qidx = 0;
4090*22ce4affSfengbojiang 	if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !ALTQ_IS_ENABLED(&ifp->if_snd))
4091a9643ea8Slogwang 		qidx = QIDX(ctx, m);
4092a9643ea8Slogwang 	/*
4093a9643ea8Slogwang 	 * XXX calculate buf_ring based on flowid (divvy up bits?)
4094a9643ea8Slogwang 	 */
4095a9643ea8Slogwang 	txq = &ctx->ifc_txqs[qidx];
4096a9643ea8Slogwang 
4097a9643ea8Slogwang #ifdef DRIVER_BACKPRESSURE
4098a9643ea8Slogwang 	if (txq->ift_closed) {
4099a9643ea8Slogwang 		while (m != NULL) {
4100a9643ea8Slogwang 			next = m->m_nextpkt;
4101a9643ea8Slogwang 			m->m_nextpkt = NULL;
4102a9643ea8Slogwang 			m_freem(m);
4103*22ce4affSfengbojiang 			DBG_COUNTER_INC(tx_frees);
4104a9643ea8Slogwang 			m = next;
4105a9643ea8Slogwang 		}
4106a9643ea8Slogwang 		return (ENOBUFS);
4107a9643ea8Slogwang 	}
4108a9643ea8Slogwang #endif
4109a9643ea8Slogwang #ifdef notyet
4110a9643ea8Slogwang 	qidx = count = 0;
4111a9643ea8Slogwang 	mp = marr;
4112a9643ea8Slogwang 	next = m;
4113a9643ea8Slogwang 	do {
4114a9643ea8Slogwang 		count++;
4115a9643ea8Slogwang 		next = next->m_nextpkt;
4116a9643ea8Slogwang 	} while (next != NULL);
4117a9643ea8Slogwang 
4118a9643ea8Slogwang 	if (count > nitems(marr))
4119a9643ea8Slogwang 		if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
4120a9643ea8Slogwang 			/* XXX check nextpkt */
4121a9643ea8Slogwang 			m_freem(m);
4122a9643ea8Slogwang 			/* XXX simplify for now */
4123a9643ea8Slogwang 			DBG_COUNTER_INC(tx_frees);
4124a9643ea8Slogwang 			return (ENOBUFS);
4125a9643ea8Slogwang 		}
4126a9643ea8Slogwang 	for (next = m, i = 0; next != NULL; i++) {
4127a9643ea8Slogwang 		mp[i] = next;
4128a9643ea8Slogwang 		next = next->m_nextpkt;
4129a9643ea8Slogwang 		mp[i]->m_nextpkt = NULL;
4130a9643ea8Slogwang 	}
4131a9643ea8Slogwang #endif
4132a9643ea8Slogwang 	DBG_COUNTER_INC(tx_seen);
4133*22ce4affSfengbojiang 	err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate);
4134a9643ea8Slogwang 
4135*22ce4affSfengbojiang 	if (abdicate)
4136*22ce4affSfengbojiang 		GROUPTASK_ENQUEUE(&txq->ift_task);
4137a9643ea8Slogwang  	if (err) {
4138*22ce4affSfengbojiang 		if (!abdicate)
4139a9643ea8Slogwang 			GROUPTASK_ENQUEUE(&txq->ift_task);
4140a9643ea8Slogwang 		/* support forthcoming later */
4141a9643ea8Slogwang #ifdef DRIVER_BACKPRESSURE
4142a9643ea8Slogwang 		txq->ift_closed = TRUE;
4143a9643ea8Slogwang #endif
4144*22ce4affSfengbojiang 		ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
4145a9643ea8Slogwang 		m_freem(m);
4146*22ce4affSfengbojiang 		DBG_COUNTER_INC(tx_frees);
4147a9643ea8Slogwang 	}
4148a9643ea8Slogwang 
4149a9643ea8Slogwang 	return (err);
4150a9643ea8Slogwang }
4151a9643ea8Slogwang 
4152*22ce4affSfengbojiang #ifdef ALTQ
4153*22ce4affSfengbojiang /*
4154*22ce4affSfengbojiang  * The overall approach to integrating iflib with ALTQ is to continue to use
4155*22ce4affSfengbojiang  * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware
4156*22ce4affSfengbojiang  * ring.  Technically, when using ALTQ, queueing to an intermediate mp_ring
4157*22ce4affSfengbojiang  * is redundant/unnecessary, but doing so minimizes the amount of
4158*22ce4affSfengbojiang  * ALTQ-specific code required in iflib.  It is assumed that the overhead of
4159*22ce4affSfengbojiang  * redundantly queueing to an intermediate mp_ring is swamped by the
4160*22ce4affSfengbojiang  * performance limitations inherent in using ALTQ.
4161*22ce4affSfengbojiang  *
4162*22ce4affSfengbojiang  * When ALTQ support is compiled in, all iflib drivers will use a transmit
4163*22ce4affSfengbojiang  * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the
4164*22ce4affSfengbojiang  * given interface.  If ALTQ is enabled for an interface, then all
4165*22ce4affSfengbojiang  * transmitted packets for that interface will be submitted to the ALTQ
4166*22ce4affSfengbojiang  * subsystem via IFQ_ENQUEUE().  We don't use the legacy if_transmit()
4167*22ce4affSfengbojiang  * implementation because it uses IFQ_HANDOFF(), which will duplicatively
4168*22ce4affSfengbojiang  * update stats that the iflib machinery handles, and which is sensitve to
4169*22ce4affSfengbojiang  * the disused IFF_DRV_OACTIVE flag.  Additionally, iflib_altq_if_start()
4170*22ce4affSfengbojiang  * will be installed as the start routine for use by ALTQ facilities that
4171*22ce4affSfengbojiang  * need to trigger queue drains on a scheduled basis.
4172*22ce4affSfengbojiang  *
4173*22ce4affSfengbojiang  */
4174*22ce4affSfengbojiang static void
iflib_altq_if_start(if_t ifp)4175*22ce4affSfengbojiang iflib_altq_if_start(if_t ifp)
4176*22ce4affSfengbojiang {
4177*22ce4affSfengbojiang 	struct ifaltq *ifq = &ifp->if_snd;
4178*22ce4affSfengbojiang 	struct mbuf *m;
4179*22ce4affSfengbojiang 
4180*22ce4affSfengbojiang 	IFQ_LOCK(ifq);
4181*22ce4affSfengbojiang 	IFQ_DEQUEUE_NOLOCK(ifq, m);
4182*22ce4affSfengbojiang 	while (m != NULL) {
4183*22ce4affSfengbojiang 		iflib_if_transmit(ifp, m);
4184*22ce4affSfengbojiang 		IFQ_DEQUEUE_NOLOCK(ifq, m);
4185*22ce4affSfengbojiang 	}
4186*22ce4affSfengbojiang 	IFQ_UNLOCK(ifq);
4187*22ce4affSfengbojiang }
4188*22ce4affSfengbojiang 
4189*22ce4affSfengbojiang static int
iflib_altq_if_transmit(if_t ifp,struct mbuf * m)4190*22ce4affSfengbojiang iflib_altq_if_transmit(if_t ifp, struct mbuf *m)
4191*22ce4affSfengbojiang {
4192*22ce4affSfengbojiang 	int err;
4193*22ce4affSfengbojiang 
4194*22ce4affSfengbojiang 	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
4195*22ce4affSfengbojiang 		IFQ_ENQUEUE(&ifp->if_snd, m, err);
4196*22ce4affSfengbojiang 		if (err == 0)
4197*22ce4affSfengbojiang 			iflib_altq_if_start(ifp);
4198*22ce4affSfengbojiang 	} else
4199*22ce4affSfengbojiang 		err = iflib_if_transmit(ifp, m);
4200*22ce4affSfengbojiang 
4201*22ce4affSfengbojiang 	return (err);
4202*22ce4affSfengbojiang }
4203*22ce4affSfengbojiang #endif /* ALTQ */
4204*22ce4affSfengbojiang 
4205a9643ea8Slogwang static void
iflib_if_qflush(if_t ifp)4206a9643ea8Slogwang iflib_if_qflush(if_t ifp)
4207a9643ea8Slogwang {
4208a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
4209a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
4210a9643ea8Slogwang 	int i;
4211a9643ea8Slogwang 
4212*22ce4affSfengbojiang 	STATE_LOCK(ctx);
4213a9643ea8Slogwang 	ctx->ifc_flags |= IFC_QFLUSH;
4214*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
4215a9643ea8Slogwang 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
4216*22ce4affSfengbojiang 		while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
4217a9643ea8Slogwang 			iflib_txq_check_drain(txq, 0);
4218*22ce4affSfengbojiang 	STATE_LOCK(ctx);
4219a9643ea8Slogwang 	ctx->ifc_flags &= ~IFC_QFLUSH;
4220*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
4221a9643ea8Slogwang 
4222*22ce4affSfengbojiang 	/*
4223*22ce4affSfengbojiang 	 * When ALTQ is enabled, this will also take care of purging the
4224*22ce4affSfengbojiang 	 * ALTQ queue(s).
4225*22ce4affSfengbojiang 	 */
4226a9643ea8Slogwang 	if_qflush(ifp);
4227a9643ea8Slogwang }
4228a9643ea8Slogwang 
4229*22ce4affSfengbojiang #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
4230*22ce4affSfengbojiang 		     IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
4231*22ce4affSfengbojiang 		     IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \
4232*22ce4affSfengbojiang 		     IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_NOMAP)
4233a9643ea8Slogwang 
4234a9643ea8Slogwang static int
iflib_if_ioctl(if_t ifp,u_long command,caddr_t data)4235a9643ea8Slogwang iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
4236a9643ea8Slogwang {
4237a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
4238a9643ea8Slogwang 	struct ifreq	*ifr = (struct ifreq *)data;
4239a9643ea8Slogwang #if defined(INET) || defined(INET6)
4240a9643ea8Slogwang 	struct ifaddr	*ifa = (struct ifaddr *)data;
4241a9643ea8Slogwang #endif
4242*22ce4affSfengbojiang 	bool		avoid_reset = false;
4243a9643ea8Slogwang 	int		err = 0, reinit = 0, bits;
4244a9643ea8Slogwang 
4245a9643ea8Slogwang 	switch (command) {
4246a9643ea8Slogwang 	case SIOCSIFADDR:
4247a9643ea8Slogwang #ifdef INET
4248a9643ea8Slogwang 		if (ifa->ifa_addr->sa_family == AF_INET)
4249*22ce4affSfengbojiang 			avoid_reset = true;
4250a9643ea8Slogwang #endif
4251a9643ea8Slogwang #ifdef INET6
4252a9643ea8Slogwang 		if (ifa->ifa_addr->sa_family == AF_INET6)
4253*22ce4affSfengbojiang 			avoid_reset = true;
4254a9643ea8Slogwang #endif
4255a9643ea8Slogwang 		/*
4256a9643ea8Slogwang 		** Calling init results in link renegotiation,
4257a9643ea8Slogwang 		** so we avoid doing it when possible.
4258a9643ea8Slogwang 		*/
4259a9643ea8Slogwang 		if (avoid_reset) {
4260a9643ea8Slogwang 			if_setflagbits(ifp, IFF_UP,0);
4261a9643ea8Slogwang 			if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
4262a9643ea8Slogwang 				reinit = 1;
4263a9643ea8Slogwang #ifdef INET
4264a9643ea8Slogwang 			if (!(if_getflags(ifp) & IFF_NOARP))
4265a9643ea8Slogwang 				arp_ifinit(ifp, ifa);
4266a9643ea8Slogwang #endif
4267a9643ea8Slogwang 		} else
4268a9643ea8Slogwang 			err = ether_ioctl(ifp, command, data);
4269a9643ea8Slogwang 		break;
4270a9643ea8Slogwang 	case SIOCSIFMTU:
4271a9643ea8Slogwang 		CTX_LOCK(ctx);
4272a9643ea8Slogwang 		if (ifr->ifr_mtu == if_getmtu(ifp)) {
4273a9643ea8Slogwang 			CTX_UNLOCK(ctx);
4274a9643ea8Slogwang 			break;
4275a9643ea8Slogwang 		}
4276a9643ea8Slogwang 		bits = if_getdrvflags(ifp);
4277a9643ea8Slogwang 		/* stop the driver and free any clusters before proceeding */
4278a9643ea8Slogwang 		iflib_stop(ctx);
4279a9643ea8Slogwang 
4280a9643ea8Slogwang 		if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
4281*22ce4affSfengbojiang 			STATE_LOCK(ctx);
4282a9643ea8Slogwang 			if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
4283a9643ea8Slogwang 				ctx->ifc_flags |= IFC_MULTISEG;
4284a9643ea8Slogwang 			else
4285a9643ea8Slogwang 				ctx->ifc_flags &= ~IFC_MULTISEG;
4286*22ce4affSfengbojiang 			STATE_UNLOCK(ctx);
4287a9643ea8Slogwang 			err = if_setmtu(ifp, ifr->ifr_mtu);
4288a9643ea8Slogwang 		}
4289a9643ea8Slogwang 		iflib_init_locked(ctx);
4290*22ce4affSfengbojiang 		STATE_LOCK(ctx);
4291a9643ea8Slogwang 		if_setdrvflags(ifp, bits);
4292*22ce4affSfengbojiang 		STATE_UNLOCK(ctx);
4293a9643ea8Slogwang 		CTX_UNLOCK(ctx);
4294a9643ea8Slogwang 		break;
4295a9643ea8Slogwang 	case SIOCSIFFLAGS:
4296a9643ea8Slogwang 		CTX_LOCK(ctx);
4297a9643ea8Slogwang 		if (if_getflags(ifp) & IFF_UP) {
4298a9643ea8Slogwang 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4299a9643ea8Slogwang 				if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
4300a9643ea8Slogwang 				    (IFF_PROMISC | IFF_ALLMULTI)) {
4301*22ce4affSfengbojiang 					CTX_UNLOCK(ctx);
4302a9643ea8Slogwang 					err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
4303*22ce4affSfengbojiang 					CTX_LOCK(ctx);
4304a9643ea8Slogwang 				}
4305a9643ea8Slogwang 			} else
4306a9643ea8Slogwang 				reinit = 1;
4307a9643ea8Slogwang 		} else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4308a9643ea8Slogwang 			iflib_stop(ctx);
4309a9643ea8Slogwang 		}
4310a9643ea8Slogwang 		ctx->ifc_if_flags = if_getflags(ifp);
4311a9643ea8Slogwang 		CTX_UNLOCK(ctx);
4312a9643ea8Slogwang 		break;
4313a9643ea8Slogwang 	case SIOCADDMULTI:
4314a9643ea8Slogwang 	case SIOCDELMULTI:
4315a9643ea8Slogwang 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4316a9643ea8Slogwang 			CTX_LOCK(ctx);
4317a9643ea8Slogwang 			IFDI_INTR_DISABLE(ctx);
4318a9643ea8Slogwang 			IFDI_MULTI_SET(ctx);
4319a9643ea8Slogwang 			IFDI_INTR_ENABLE(ctx);
4320a9643ea8Slogwang 			CTX_UNLOCK(ctx);
4321a9643ea8Slogwang 		}
4322a9643ea8Slogwang 		break;
4323a9643ea8Slogwang 	case SIOCSIFMEDIA:
4324a9643ea8Slogwang 		CTX_LOCK(ctx);
4325a9643ea8Slogwang 		IFDI_MEDIA_SET(ctx);
4326a9643ea8Slogwang 		CTX_UNLOCK(ctx);
4327*22ce4affSfengbojiang 		/* FALLTHROUGH */
4328a9643ea8Slogwang 	case SIOCGIFMEDIA:
4329*22ce4affSfengbojiang 	case SIOCGIFXMEDIA:
4330*22ce4affSfengbojiang 		err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command);
4331a9643ea8Slogwang 		break;
4332a9643ea8Slogwang 	case SIOCGI2C:
4333a9643ea8Slogwang 	{
4334a9643ea8Slogwang 		struct ifi2creq i2c;
4335a9643ea8Slogwang 
4336*22ce4affSfengbojiang 		err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
4337a9643ea8Slogwang 		if (err != 0)
4338a9643ea8Slogwang 			break;
4339a9643ea8Slogwang 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
4340a9643ea8Slogwang 			err = EINVAL;
4341a9643ea8Slogwang 			break;
4342a9643ea8Slogwang 		}
4343a9643ea8Slogwang 		if (i2c.len > sizeof(i2c.data)) {
4344a9643ea8Slogwang 			err = EINVAL;
4345a9643ea8Slogwang 			break;
4346a9643ea8Slogwang 		}
4347a9643ea8Slogwang 
4348a9643ea8Slogwang 		if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
4349*22ce4affSfengbojiang 			err = copyout(&i2c, ifr_data_get_ptr(ifr),
4350*22ce4affSfengbojiang 			    sizeof(i2c));
4351a9643ea8Slogwang 		break;
4352a9643ea8Slogwang 	}
4353a9643ea8Slogwang 	case SIOCSIFCAP:
4354a9643ea8Slogwang 	{
4355*22ce4affSfengbojiang 		int mask, setmask, oldmask;
4356a9643ea8Slogwang 
4357*22ce4affSfengbojiang 		oldmask = if_getcapenable(ifp);
4358*22ce4affSfengbojiang 		mask = ifr->ifr_reqcap ^ oldmask;
4359*22ce4affSfengbojiang 		mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_NOMAP;
4360a9643ea8Slogwang 		setmask = 0;
4361a9643ea8Slogwang #ifdef TCP_OFFLOAD
4362a9643ea8Slogwang 		setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
4363a9643ea8Slogwang #endif
4364a9643ea8Slogwang 		setmask |= (mask & IFCAP_FLAGS);
4365*22ce4affSfengbojiang 		setmask |= (mask & IFCAP_WOL);
4366a9643ea8Slogwang 
4367*22ce4affSfengbojiang 		/*
4368*22ce4affSfengbojiang 		 * If any RX csum has changed, change all the ones that
4369*22ce4affSfengbojiang 		 * are supported by the driver.
4370*22ce4affSfengbojiang 		 */
4371*22ce4affSfengbojiang 		if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
4372*22ce4affSfengbojiang 			setmask |= ctx->ifc_softc_ctx.isc_capabilities &
4373*22ce4affSfengbojiang 			    (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
4374*22ce4affSfengbojiang 		}
4375*22ce4affSfengbojiang 
4376a9643ea8Slogwang 		/*
4377a9643ea8Slogwang 		 * want to ensure that traffic has stopped before we change any of the flags
4378a9643ea8Slogwang 		 */
4379a9643ea8Slogwang 		if (setmask) {
4380a9643ea8Slogwang 			CTX_LOCK(ctx);
4381a9643ea8Slogwang 			bits = if_getdrvflags(ifp);
4382*22ce4affSfengbojiang 			if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL)
4383a9643ea8Slogwang 				iflib_stop(ctx);
4384*22ce4affSfengbojiang 			STATE_LOCK(ctx);
4385a9643ea8Slogwang 			if_togglecapenable(ifp, setmask);
4386*22ce4affSfengbojiang 			STATE_UNLOCK(ctx);
4387*22ce4affSfengbojiang 			if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL)
4388a9643ea8Slogwang 				iflib_init_locked(ctx);
4389*22ce4affSfengbojiang 			STATE_LOCK(ctx);
4390a9643ea8Slogwang 			if_setdrvflags(ifp, bits);
4391*22ce4affSfengbojiang 			STATE_UNLOCK(ctx);
4392a9643ea8Slogwang 			CTX_UNLOCK(ctx);
4393a9643ea8Slogwang 		}
4394*22ce4affSfengbojiang 		if_vlancap(ifp);
4395a9643ea8Slogwang 		break;
4396a9643ea8Slogwang 	}
4397a9643ea8Slogwang 	case SIOCGPRIVATE_0:
4398a9643ea8Slogwang 	case SIOCSDRVSPEC:
4399a9643ea8Slogwang 	case SIOCGDRVSPEC:
4400a9643ea8Slogwang 		CTX_LOCK(ctx);
4401a9643ea8Slogwang 		err = IFDI_PRIV_IOCTL(ctx, command, data);
4402a9643ea8Slogwang 		CTX_UNLOCK(ctx);
4403a9643ea8Slogwang 		break;
4404a9643ea8Slogwang 	default:
4405a9643ea8Slogwang 		err = ether_ioctl(ifp, command, data);
4406a9643ea8Slogwang 		break;
4407a9643ea8Slogwang 	}
4408a9643ea8Slogwang 	if (reinit)
4409a9643ea8Slogwang 		iflib_if_init(ctx);
4410a9643ea8Slogwang 	return (err);
4411a9643ea8Slogwang }
4412a9643ea8Slogwang 
4413a9643ea8Slogwang static uint64_t
iflib_if_get_counter(if_t ifp,ift_counter cnt)4414a9643ea8Slogwang iflib_if_get_counter(if_t ifp, ift_counter cnt)
4415a9643ea8Slogwang {
4416a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
4417a9643ea8Slogwang 
4418a9643ea8Slogwang 	return (IFDI_GET_COUNTER(ctx, cnt));
4419a9643ea8Slogwang }
4420a9643ea8Slogwang 
4421a9643ea8Slogwang /*********************************************************************
4422a9643ea8Slogwang  *
4423a9643ea8Slogwang  *  OTHER FUNCTIONS EXPORTED TO THE STACK
4424a9643ea8Slogwang  *
4425a9643ea8Slogwang  **********************************************************************/
4426a9643ea8Slogwang 
4427a9643ea8Slogwang static void
iflib_vlan_register(void * arg,if_t ifp,uint16_t vtag)4428a9643ea8Slogwang iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
4429a9643ea8Slogwang {
4430a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
4431a9643ea8Slogwang 
4432a9643ea8Slogwang 	if ((void *)ctx != arg)
4433a9643ea8Slogwang 		return;
4434a9643ea8Slogwang 
4435a9643ea8Slogwang 	if ((vtag == 0) || (vtag > 4095))
4436a9643ea8Slogwang 		return;
4437a9643ea8Slogwang 
4438*22ce4affSfengbojiang 	if (iflib_in_detach(ctx))
4439*22ce4affSfengbojiang 		return;
4440*22ce4affSfengbojiang 
4441a9643ea8Slogwang 	CTX_LOCK(ctx);
4442*22ce4affSfengbojiang 	/* Driver may need all untagged packets to be flushed */
4443*22ce4affSfengbojiang 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
4444*22ce4affSfengbojiang 		iflib_stop(ctx);
4445a9643ea8Slogwang 	IFDI_VLAN_REGISTER(ctx, vtag);
4446*22ce4affSfengbojiang 	/* Re-init to load the changes, if required */
4447*22ce4affSfengbojiang 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
4448a9643ea8Slogwang 		iflib_init_locked(ctx);
4449a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4450a9643ea8Slogwang }
4451a9643ea8Slogwang 
4452a9643ea8Slogwang static void
iflib_vlan_unregister(void * arg,if_t ifp,uint16_t vtag)4453a9643ea8Slogwang iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
4454a9643ea8Slogwang {
4455a9643ea8Slogwang 	if_ctx_t ctx = if_getsoftc(ifp);
4456a9643ea8Slogwang 
4457a9643ea8Slogwang 	if ((void *)ctx != arg)
4458a9643ea8Slogwang 		return;
4459a9643ea8Slogwang 
4460a9643ea8Slogwang 	if ((vtag == 0) || (vtag > 4095))
4461a9643ea8Slogwang 		return;
4462a9643ea8Slogwang 
4463a9643ea8Slogwang 	CTX_LOCK(ctx);
4464*22ce4affSfengbojiang 	/* Driver may need all tagged packets to be flushed */
4465*22ce4affSfengbojiang 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
4466*22ce4affSfengbojiang 		iflib_stop(ctx);
4467a9643ea8Slogwang 	IFDI_VLAN_UNREGISTER(ctx, vtag);
4468*22ce4affSfengbojiang 	/* Re-init to load the changes, if required */
4469*22ce4affSfengbojiang 	if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
4470a9643ea8Slogwang 		iflib_init_locked(ctx);
4471a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4472a9643ea8Slogwang }
4473a9643ea8Slogwang 
4474a9643ea8Slogwang static void
iflib_led_func(void * arg,int onoff)4475a9643ea8Slogwang iflib_led_func(void *arg, int onoff)
4476a9643ea8Slogwang {
4477a9643ea8Slogwang 	if_ctx_t ctx = arg;
4478a9643ea8Slogwang 
4479a9643ea8Slogwang 	CTX_LOCK(ctx);
4480a9643ea8Slogwang 	IFDI_LED_FUNC(ctx, onoff);
4481a9643ea8Slogwang 	CTX_UNLOCK(ctx);
4482a9643ea8Slogwang }
4483a9643ea8Slogwang 
4484a9643ea8Slogwang /*********************************************************************
4485a9643ea8Slogwang  *
4486a9643ea8Slogwang  *  BUS FUNCTION DEFINITIONS
4487a9643ea8Slogwang  *
4488a9643ea8Slogwang  **********************************************************************/
4489a9643ea8Slogwang 
4490a9643ea8Slogwang int
iflib_device_probe(device_t dev)4491a9643ea8Slogwang iflib_device_probe(device_t dev)
4492a9643ea8Slogwang {
4493*22ce4affSfengbojiang 	const pci_vendor_info_t *ent;
4494a9643ea8Slogwang 	if_shared_ctx_t sctx;
4495*22ce4affSfengbojiang 	uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id;
4496*22ce4affSfengbojiang 	uint16_t pci_vendor_id;
4497a9643ea8Slogwang 
4498a9643ea8Slogwang 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
4499a9643ea8Slogwang 		return (ENOTSUP);
4500a9643ea8Slogwang 
4501a9643ea8Slogwang 	pci_vendor_id = pci_get_vendor(dev);
4502a9643ea8Slogwang 	pci_device_id = pci_get_device(dev);
4503a9643ea8Slogwang 	pci_subvendor_id = pci_get_subvendor(dev);
4504a9643ea8Slogwang 	pci_subdevice_id = pci_get_subdevice(dev);
4505a9643ea8Slogwang 	pci_rev_id = pci_get_revid(dev);
4506a9643ea8Slogwang 	if (sctx->isc_parse_devinfo != NULL)
4507a9643ea8Slogwang 		sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
4508a9643ea8Slogwang 
4509a9643ea8Slogwang 	ent = sctx->isc_vendor_info;
4510a9643ea8Slogwang 	while (ent->pvi_vendor_id != 0) {
4511a9643ea8Slogwang 		if (pci_vendor_id != ent->pvi_vendor_id) {
4512a9643ea8Slogwang 			ent++;
4513a9643ea8Slogwang 			continue;
4514a9643ea8Slogwang 		}
4515a9643ea8Slogwang 		if ((pci_device_id == ent->pvi_device_id) &&
4516a9643ea8Slogwang 		    ((pci_subvendor_id == ent->pvi_subvendor_id) ||
4517a9643ea8Slogwang 		     (ent->pvi_subvendor_id == 0)) &&
4518a9643ea8Slogwang 		    ((pci_subdevice_id == ent->pvi_subdevice_id) ||
4519a9643ea8Slogwang 		     (ent->pvi_subdevice_id == 0)) &&
4520a9643ea8Slogwang 		    ((pci_rev_id == ent->pvi_rev_id) ||
4521a9643ea8Slogwang 		     (ent->pvi_rev_id == 0))) {
4522a9643ea8Slogwang 			device_set_desc_copy(dev, ent->pvi_name);
4523a9643ea8Slogwang 			/* this needs to be changed to zero if the bus probing code
4524a9643ea8Slogwang 			 * ever stops re-probing on best match because the sctx
4525a9643ea8Slogwang 			 * may have its values over written by register calls
4526a9643ea8Slogwang 			 * in subsequent probes
4527a9643ea8Slogwang 			 */
4528a9643ea8Slogwang 			return (BUS_PROBE_DEFAULT);
4529a9643ea8Slogwang 		}
4530a9643ea8Slogwang 		ent++;
4531a9643ea8Slogwang 	}
4532a9643ea8Slogwang 	return (ENXIO);
4533a9643ea8Slogwang }
4534a9643ea8Slogwang 
4535a9643ea8Slogwang int
iflib_device_probe_vendor(device_t dev)4536*22ce4affSfengbojiang iflib_device_probe_vendor(device_t dev)
4537a9643ea8Slogwang {
4538*22ce4affSfengbojiang 	int probe;
4539*22ce4affSfengbojiang 
4540*22ce4affSfengbojiang 	probe = iflib_device_probe(dev);
4541*22ce4affSfengbojiang 	if (probe == BUS_PROBE_DEFAULT)
4542*22ce4affSfengbojiang 		return (BUS_PROBE_VENDOR);
4543*22ce4affSfengbojiang 	else
4544*22ce4affSfengbojiang 		return (probe);
4545*22ce4affSfengbojiang }
4546*22ce4affSfengbojiang 
4547*22ce4affSfengbojiang static void
iflib_reset_qvalues(if_ctx_t ctx)4548*22ce4affSfengbojiang iflib_reset_qvalues(if_ctx_t ctx)
4549*22ce4affSfengbojiang {
4550*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
4551*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
4552*22ce4affSfengbojiang 	device_t dev = ctx->ifc_dev;
4553a9643ea8Slogwang 	int i;
4554a9643ea8Slogwang 
4555a9643ea8Slogwang 	if (ctx->ifc_sysctl_ntxqs != 0)
4556a9643ea8Slogwang 		scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
4557a9643ea8Slogwang 	if (ctx->ifc_sysctl_nrxqs != 0)
4558a9643ea8Slogwang 		scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
4559a9643ea8Slogwang 
4560a9643ea8Slogwang 	for (i = 0; i < sctx->isc_ntxqs; i++) {
4561a9643ea8Slogwang 		if (ctx->ifc_sysctl_ntxds[i] != 0)
4562a9643ea8Slogwang 			scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
4563a9643ea8Slogwang 		else
4564a9643ea8Slogwang 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
4565a9643ea8Slogwang 	}
4566a9643ea8Slogwang 
4567a9643ea8Slogwang 	for (i = 0; i < sctx->isc_nrxqs; i++) {
4568a9643ea8Slogwang 		if (ctx->ifc_sysctl_nrxds[i] != 0)
4569a9643ea8Slogwang 			scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
4570a9643ea8Slogwang 		else
4571a9643ea8Slogwang 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
4572a9643ea8Slogwang 	}
4573a9643ea8Slogwang 
4574a9643ea8Slogwang 	for (i = 0; i < sctx->isc_nrxqs; i++) {
4575a9643ea8Slogwang 		if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
4576a9643ea8Slogwang 			device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
4577a9643ea8Slogwang 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
4578a9643ea8Slogwang 			scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
4579a9643ea8Slogwang 		}
4580a9643ea8Slogwang 		if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
4581a9643ea8Slogwang 			device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
4582a9643ea8Slogwang 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
4583a9643ea8Slogwang 			scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
4584a9643ea8Slogwang 		}
4585*22ce4affSfengbojiang 		if (!powerof2(scctx->isc_nrxd[i])) {
4586*22ce4affSfengbojiang 			device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n",
4587*22ce4affSfengbojiang 				      i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]);
4588*22ce4affSfengbojiang 			scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
4589*22ce4affSfengbojiang 		}
4590a9643ea8Slogwang 	}
4591a9643ea8Slogwang 
4592a9643ea8Slogwang 	for (i = 0; i < sctx->isc_ntxqs; i++) {
4593a9643ea8Slogwang 		if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
4594a9643ea8Slogwang 			device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
4595a9643ea8Slogwang 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
4596a9643ea8Slogwang 			scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
4597a9643ea8Slogwang 		}
4598a9643ea8Slogwang 		if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
4599a9643ea8Slogwang 			device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
4600a9643ea8Slogwang 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
4601a9643ea8Slogwang 			scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
4602a9643ea8Slogwang 		}
4603*22ce4affSfengbojiang 		if (!powerof2(scctx->isc_ntxd[i])) {
4604*22ce4affSfengbojiang 			device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n",
4605*22ce4affSfengbojiang 				      i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]);
4606*22ce4affSfengbojiang 			scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
4607*22ce4affSfengbojiang 		}
4608*22ce4affSfengbojiang 	}
4609a9643ea8Slogwang }
4610a9643ea8Slogwang 
4611*22ce4affSfengbojiang static void
iflib_add_pfil(if_ctx_t ctx)4612*22ce4affSfengbojiang iflib_add_pfil(if_ctx_t ctx)
4613*22ce4affSfengbojiang {
4614*22ce4affSfengbojiang 	struct pfil_head *pfil;
4615*22ce4affSfengbojiang 	struct pfil_head_args pa;
4616*22ce4affSfengbojiang 	iflib_rxq_t rxq;
4617*22ce4affSfengbojiang 	int i;
4618*22ce4affSfengbojiang 
4619*22ce4affSfengbojiang 	pa.pa_version = PFIL_VERSION;
4620*22ce4affSfengbojiang 	pa.pa_flags = PFIL_IN;
4621*22ce4affSfengbojiang 	pa.pa_type = PFIL_TYPE_ETHERNET;
4622*22ce4affSfengbojiang 	pa.pa_headname = ctx->ifc_ifp->if_xname;
4623*22ce4affSfengbojiang 	pfil = pfil_head_register(&pa);
4624*22ce4affSfengbojiang 
4625*22ce4affSfengbojiang 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
4626*22ce4affSfengbojiang 		rxq->pfil = pfil;
4627a9643ea8Slogwang 	}
4628*22ce4affSfengbojiang }
4629a9643ea8Slogwang 
4630*22ce4affSfengbojiang static void
iflib_rem_pfil(if_ctx_t ctx)4631*22ce4affSfengbojiang iflib_rem_pfil(if_ctx_t ctx)
4632*22ce4affSfengbojiang {
4633*22ce4affSfengbojiang 	struct pfil_head *pfil;
4634*22ce4affSfengbojiang 	iflib_rxq_t rxq;
4635*22ce4affSfengbojiang 	int i;
4636a9643ea8Slogwang 
4637*22ce4affSfengbojiang 	rxq = ctx->ifc_rxqs;
4638*22ce4affSfengbojiang 	pfil = rxq->pfil;
4639*22ce4affSfengbojiang 	for (i = 0; i < NRXQSETS(ctx); i++, rxq++) {
4640*22ce4affSfengbojiang 		rxq->pfil = NULL;
4641*22ce4affSfengbojiang 	}
4642*22ce4affSfengbojiang 	pfil_head_unregister(pfil);
4643*22ce4affSfengbojiang }
4644a9643ea8Slogwang 
4645*22ce4affSfengbojiang static uint16_t
get_ctx_core_offset(if_ctx_t ctx)4646*22ce4affSfengbojiang get_ctx_core_offset(if_ctx_t ctx)
4647*22ce4affSfengbojiang {
4648*22ce4affSfengbojiang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
4649*22ce4affSfengbojiang 	struct cpu_offset *op;
4650*22ce4affSfengbojiang 	uint16_t qc;
4651*22ce4affSfengbojiang 	uint16_t ret = ctx->ifc_sysctl_core_offset;
4652*22ce4affSfengbojiang 
4653*22ce4affSfengbojiang 	if (ret != CORE_OFFSET_UNSPECIFIED)
4654*22ce4affSfengbojiang 		return (ret);
4655*22ce4affSfengbojiang 
4656*22ce4affSfengbojiang 	if (ctx->ifc_sysctl_separate_txrx)
4657*22ce4affSfengbojiang 		qc = scctx->isc_ntxqsets + scctx->isc_nrxqsets;
4658*22ce4affSfengbojiang 	else
4659*22ce4affSfengbojiang 		qc = max(scctx->isc_ntxqsets, scctx->isc_nrxqsets);
4660*22ce4affSfengbojiang 
4661*22ce4affSfengbojiang 	mtx_lock(&cpu_offset_mtx);
4662*22ce4affSfengbojiang 	SLIST_FOREACH(op, &cpu_offsets, entries) {
4663*22ce4affSfengbojiang 		if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
4664*22ce4affSfengbojiang 			ret = op->offset;
4665*22ce4affSfengbojiang 			op->offset += qc;
4666*22ce4affSfengbojiang 			MPASS(op->refcount < UINT_MAX);
4667*22ce4affSfengbojiang 			op->refcount++;
4668*22ce4affSfengbojiang 			break;
4669*22ce4affSfengbojiang 		}
4670*22ce4affSfengbojiang 	}
4671*22ce4affSfengbojiang 	if (ret == CORE_OFFSET_UNSPECIFIED) {
4672*22ce4affSfengbojiang 		ret = 0;
4673*22ce4affSfengbojiang 		op = malloc(sizeof(struct cpu_offset), M_IFLIB,
4674*22ce4affSfengbojiang 		    M_NOWAIT | M_ZERO);
4675*22ce4affSfengbojiang 		if (op == NULL) {
4676*22ce4affSfengbojiang 			device_printf(ctx->ifc_dev,
4677*22ce4affSfengbojiang 			    "allocation for cpu offset failed.\n");
4678*22ce4affSfengbojiang 		} else {
4679*22ce4affSfengbojiang 			op->offset = qc;
4680*22ce4affSfengbojiang 			op->refcount = 1;
4681*22ce4affSfengbojiang 			CPU_COPY(&ctx->ifc_cpus, &op->set);
4682*22ce4affSfengbojiang 			SLIST_INSERT_HEAD(&cpu_offsets, op, entries);
4683*22ce4affSfengbojiang 		}
4684*22ce4affSfengbojiang 	}
4685*22ce4affSfengbojiang 	mtx_unlock(&cpu_offset_mtx);
4686*22ce4affSfengbojiang 
4687*22ce4affSfengbojiang 	return (ret);
4688*22ce4affSfengbojiang }
4689*22ce4affSfengbojiang 
4690*22ce4affSfengbojiang static void
unref_ctx_core_offset(if_ctx_t ctx)4691*22ce4affSfengbojiang unref_ctx_core_offset(if_ctx_t ctx)
4692*22ce4affSfengbojiang {
4693*22ce4affSfengbojiang 	struct cpu_offset *op, *top;
4694*22ce4affSfengbojiang 
4695*22ce4affSfengbojiang 	mtx_lock(&cpu_offset_mtx);
4696*22ce4affSfengbojiang 	SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) {
4697*22ce4affSfengbojiang 		if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
4698*22ce4affSfengbojiang 			MPASS(op->refcount > 0);
4699*22ce4affSfengbojiang 			op->refcount--;
4700*22ce4affSfengbojiang 			if (op->refcount == 0) {
4701*22ce4affSfengbojiang 				SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries);
4702*22ce4affSfengbojiang 				free(op, M_IFLIB);
4703*22ce4affSfengbojiang 			}
4704*22ce4affSfengbojiang 			break;
4705*22ce4affSfengbojiang 		}
4706*22ce4affSfengbojiang 	}
4707*22ce4affSfengbojiang 	mtx_unlock(&cpu_offset_mtx);
4708*22ce4affSfengbojiang }
4709*22ce4affSfengbojiang 
4710*22ce4affSfengbojiang int
iflib_device_register(device_t dev,void * sc,if_shared_ctx_t sctx,if_ctx_t * ctxp)4711*22ce4affSfengbojiang iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
4712*22ce4affSfengbojiang {
4713*22ce4affSfengbojiang 	if_ctx_t ctx;
4714*22ce4affSfengbojiang 	if_t ifp;
4715*22ce4affSfengbojiang 	if_softc_ctx_t scctx;
4716*22ce4affSfengbojiang 	kobjop_desc_t kobj_desc;
4717*22ce4affSfengbojiang 	kobj_method_t *kobj_method;
4718*22ce4affSfengbojiang 	int err, msix, rid;
4719*22ce4affSfengbojiang 	int num_txd, num_rxd;
4720*22ce4affSfengbojiang 
4721*22ce4affSfengbojiang 	ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
4722*22ce4affSfengbojiang 
4723*22ce4affSfengbojiang 	if (sc == NULL) {
4724*22ce4affSfengbojiang 		sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
4725*22ce4affSfengbojiang 		device_set_softc(dev, ctx);
4726*22ce4affSfengbojiang 		ctx->ifc_flags |= IFC_SC_ALLOCATED;
4727*22ce4affSfengbojiang 	}
4728*22ce4affSfengbojiang 
4729*22ce4affSfengbojiang 	ctx->ifc_sctx = sctx;
4730*22ce4affSfengbojiang 	ctx->ifc_dev = dev;
4731*22ce4affSfengbojiang 	ctx->ifc_softc = sc;
4732*22ce4affSfengbojiang 
4733*22ce4affSfengbojiang 	if ((err = iflib_register(ctx)) != 0) {
4734*22ce4affSfengbojiang 		device_printf(dev, "iflib_register failed %d\n", err);
4735*22ce4affSfengbojiang 		goto fail_ctx_free;
4736*22ce4affSfengbojiang 	}
4737*22ce4affSfengbojiang 	iflib_add_device_sysctl_pre(ctx);
4738*22ce4affSfengbojiang 
4739*22ce4affSfengbojiang 	scctx = &ctx->ifc_softc_ctx;
4740a9643ea8Slogwang 	ifp = ctx->ifc_ifp;
4741a9643ea8Slogwang 
4742*22ce4affSfengbojiang 	iflib_reset_qvalues(ctx);
4743*22ce4affSfengbojiang 	CTX_LOCK(ctx);
4744*22ce4affSfengbojiang 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
4745*22ce4affSfengbojiang 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
4746*22ce4affSfengbojiang 		goto fail_unlock;
4747*22ce4affSfengbojiang 	}
4748*22ce4affSfengbojiang 	_iflib_pre_assert(scctx);
4749*22ce4affSfengbojiang 	ctx->ifc_txrx = *scctx->isc_txrx;
4750a9643ea8Slogwang 
4751*22ce4affSfengbojiang 	if (sctx->isc_flags & IFLIB_DRIVER_MEDIA)
4752*22ce4affSfengbojiang 		ctx->ifc_mediap = scctx->isc_media;
4753*22ce4affSfengbojiang 
4754*22ce4affSfengbojiang #ifdef INVARIANTS
4755*22ce4affSfengbojiang 	if (scctx->isc_capabilities & IFCAP_TXCSUM)
4756*22ce4affSfengbojiang 		MPASS(scctx->isc_tx_csum_flags);
4757*22ce4affSfengbojiang #endif
4758*22ce4affSfengbojiang 
4759*22ce4affSfengbojiang 	if_setcapabilities(ifp,
4760*22ce4affSfengbojiang 	    scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_NOMAP);
4761*22ce4affSfengbojiang 	if_setcapenable(ifp,
4762*22ce4affSfengbojiang 	    scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_NOMAP);
4763*22ce4affSfengbojiang 
4764*22ce4affSfengbojiang 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
4765*22ce4affSfengbojiang 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
4766*22ce4affSfengbojiang 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
4767*22ce4affSfengbojiang 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
4768*22ce4affSfengbojiang 
4769*22ce4affSfengbojiang 	num_txd = iflib_num_tx_descs(ctx);
4770*22ce4affSfengbojiang 	num_rxd = iflib_num_rx_descs(ctx);
4771a9643ea8Slogwang 
4772a9643ea8Slogwang 	/* XXX change for per-queue sizes */
4773*22ce4affSfengbojiang 	device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n",
4774*22ce4affSfengbojiang 	    num_txd, num_rxd);
4775a9643ea8Slogwang 
4776*22ce4affSfengbojiang 	if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION)
4777*22ce4affSfengbojiang 		scctx->isc_tx_nsegments = max(1, num_txd /
4778a9643ea8Slogwang 		    MAX_SINGLE_PACKET_FRACTION);
4779*22ce4affSfengbojiang 	if (scctx->isc_tx_tso_segments_max > num_txd /
4780a9643ea8Slogwang 	    MAX_SINGLE_PACKET_FRACTION)
4781a9643ea8Slogwang 		scctx->isc_tx_tso_segments_max = max(1,
4782*22ce4affSfengbojiang 		    num_txd / MAX_SINGLE_PACKET_FRACTION);
4783a9643ea8Slogwang 
4784a9643ea8Slogwang 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
4785*22ce4affSfengbojiang 	if (if_getcapabilities(ifp) & IFCAP_TSO) {
4786*22ce4affSfengbojiang 		/*
4787*22ce4affSfengbojiang 		 * The stack can't handle a TSO size larger than IP_MAXPACKET,
4788*22ce4affSfengbojiang 		 * but some MACs do.
4789*22ce4affSfengbojiang 		 */
4790*22ce4affSfengbojiang 		if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max,
4791*22ce4affSfengbojiang 		    IP_MAXPACKET));
4792*22ce4affSfengbojiang 		/*
4793*22ce4affSfengbojiang 		 * Take maximum number of m_pullup(9)'s in iflib_parse_header()
4794*22ce4affSfengbojiang 		 * into account.  In the worst case, each of these calls will
4795*22ce4affSfengbojiang 		 * add another mbuf and, thus, the requirement for another DMA
4796*22ce4affSfengbojiang 		 * segment.  So for best performance, it doesn't make sense to
4797*22ce4affSfengbojiang 		 * advertize a maximum of TSO segments that typically will
4798*22ce4affSfengbojiang 		 * require defragmentation in iflib_encap().
4799*22ce4affSfengbojiang 		 */
4800*22ce4affSfengbojiang 		if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3);
4801*22ce4affSfengbojiang 		if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max);
4802*22ce4affSfengbojiang 	}
4803a9643ea8Slogwang 	if (scctx->isc_rss_table_size == 0)
4804a9643ea8Slogwang 		scctx->isc_rss_table_size = 64;
4805a9643ea8Slogwang 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
4806*22ce4affSfengbojiang 
4807*22ce4affSfengbojiang 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
4808*22ce4affSfengbojiang 	/* XXX format name */
4809*22ce4affSfengbojiang 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx,
4810*22ce4affSfengbojiang 	    NULL, NULL, "admin");
4811*22ce4affSfengbojiang 
4812*22ce4affSfengbojiang 	/* Set up cpu set.  If it fails, use the set of all CPUs. */
4813*22ce4affSfengbojiang 	if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) {
4814*22ce4affSfengbojiang 		device_printf(dev, "Unable to fetch CPU list\n");
4815*22ce4affSfengbojiang 		CPU_COPY(&all_cpus, &ctx->ifc_cpus);
4816*22ce4affSfengbojiang 	}
4817*22ce4affSfengbojiang 	MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0);
4818*22ce4affSfengbojiang 
4819a9643ea8Slogwang 	/*
4820*22ce4affSfengbojiang 	** Now set up MSI or MSI-X, should return us the number of supported
4821*22ce4affSfengbojiang 	** vectors (will be 1 for a legacy interrupt and MSI).
4822a9643ea8Slogwang 	*/
4823a9643ea8Slogwang 	if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
4824a9643ea8Slogwang 		msix = scctx->isc_vectors;
4825a9643ea8Slogwang 	} else if (scctx->isc_msix_bar != 0)
4826*22ce4affSfengbojiang 	       /*
4827*22ce4affSfengbojiang 		* The simple fact that isc_msix_bar is not 0 does not mean we
4828*22ce4affSfengbojiang 		* we have a good value there that is known to work.
4829*22ce4affSfengbojiang 		*/
4830a9643ea8Slogwang 		msix = iflib_msix_init(ctx);
4831a9643ea8Slogwang 	else {
4832a9643ea8Slogwang 		scctx->isc_vectors = 1;
4833a9643ea8Slogwang 		scctx->isc_ntxqsets = 1;
4834a9643ea8Slogwang 		scctx->isc_nrxqsets = 1;
4835a9643ea8Slogwang 		scctx->isc_intr = IFLIB_INTR_LEGACY;
4836a9643ea8Slogwang 		msix = 0;
4837a9643ea8Slogwang 	}
4838a9643ea8Slogwang 	/* Get memory for the station queues */
4839a9643ea8Slogwang 	if ((err = iflib_queues_alloc(ctx))) {
4840a9643ea8Slogwang 		device_printf(dev, "Unable to allocate queue memory\n");
4841*22ce4affSfengbojiang 		goto fail_intr_free;
4842a9643ea8Slogwang 	}
4843a9643ea8Slogwang 
4844*22ce4affSfengbojiang 	if ((err = iflib_qset_structures_setup(ctx)))
4845*22ce4affSfengbojiang 		goto fail_queues;
4846*22ce4affSfengbojiang 
4847*22ce4affSfengbojiang 	/*
4848*22ce4affSfengbojiang 	 * Now that we know how many queues there are, get the core offset.
4849*22ce4affSfengbojiang 	 */
4850*22ce4affSfengbojiang 	ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx);
4851*22ce4affSfengbojiang 
4852*22ce4affSfengbojiang 	if (msix > 1) {
4853*22ce4affSfengbojiang 		/*
4854*22ce4affSfengbojiang 		 * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable
4855*22ce4affSfengbojiang 		 * aren't the default NULL implementation.
4856*22ce4affSfengbojiang 		 */
4857*22ce4affSfengbojiang 		kobj_desc = &ifdi_rx_queue_intr_enable_desc;
4858*22ce4affSfengbojiang 		kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
4859*22ce4affSfengbojiang 		    kobj_desc);
4860*22ce4affSfengbojiang 		if (kobj_method == &kobj_desc->deflt) {
4861*22ce4affSfengbojiang 			device_printf(dev,
4862*22ce4affSfengbojiang 			    "MSI-X requires ifdi_rx_queue_intr_enable method");
4863*22ce4affSfengbojiang 			err = EOPNOTSUPP;
4864*22ce4affSfengbojiang 			goto fail_queues;
4865*22ce4affSfengbojiang 		}
4866*22ce4affSfengbojiang 		kobj_desc = &ifdi_tx_queue_intr_enable_desc;
4867*22ce4affSfengbojiang 		kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
4868*22ce4affSfengbojiang 		    kobj_desc);
4869*22ce4affSfengbojiang 		if (kobj_method == &kobj_desc->deflt) {
4870*22ce4affSfengbojiang 			device_printf(dev,
4871*22ce4affSfengbojiang 			    "MSI-X requires ifdi_tx_queue_intr_enable method");
4872*22ce4affSfengbojiang 			err = EOPNOTSUPP;
4873a9643ea8Slogwang 			goto fail_queues;
4874a9643ea8Slogwang 		}
4875a9643ea8Slogwang 
4876*22ce4affSfengbojiang 		/*
4877*22ce4affSfengbojiang 		 * Assign the MSI-X vectors.
4878*22ce4affSfengbojiang 		 * Note that the default NULL ifdi_msix_intr_assign method will
4879*22ce4affSfengbojiang 		 * fail here, too.
4880*22ce4affSfengbojiang 		 */
4881*22ce4affSfengbojiang 		err = IFDI_MSIX_INTR_ASSIGN(ctx, msix);
4882*22ce4affSfengbojiang 		if (err != 0) {
4883*22ce4affSfengbojiang 			device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n",
4884*22ce4affSfengbojiang 			    err);
4885*22ce4affSfengbojiang 			goto fail_queues;
4886a9643ea8Slogwang 		}
4887*22ce4affSfengbojiang 	} else if (scctx->isc_intr != IFLIB_INTR_MSIX) {
4888a9643ea8Slogwang 		rid = 0;
4889a9643ea8Slogwang 		if (scctx->isc_intr == IFLIB_INTR_MSI) {
4890a9643ea8Slogwang 			MPASS(msix == 1);
4891a9643ea8Slogwang 			rid = 1;
4892a9643ea8Slogwang 		}
4893a9643ea8Slogwang 		if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
4894a9643ea8Slogwang 			device_printf(dev, "iflib_legacy_setup failed %d\n", err);
4895*22ce4affSfengbojiang 			goto fail_queues;
4896*22ce4affSfengbojiang 		}
4897*22ce4affSfengbojiang 	} else {
4898*22ce4affSfengbojiang 		device_printf(dev,
4899*22ce4affSfengbojiang 		    "Cannot use iflib with only 1 MSI-X interrupt!\n");
4900*22ce4affSfengbojiang 		err = ENODEV;
4901a9643ea8Slogwang 		goto fail_intr_free;
4902a9643ea8Slogwang 	}
4903*22ce4affSfengbojiang 
4904*22ce4affSfengbojiang 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
4905*22ce4affSfengbojiang 
4906a9643ea8Slogwang 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
4907a9643ea8Slogwang 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
4908a9643ea8Slogwang 		goto fail_detach;
4909a9643ea8Slogwang 	}
4910*22ce4affSfengbojiang 
4911*22ce4affSfengbojiang 	/*
4912*22ce4affSfengbojiang 	 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
4913*22ce4affSfengbojiang 	 * This must appear after the call to ether_ifattach() because
4914*22ce4affSfengbojiang 	 * ether_ifattach() sets if_hdrlen to the default value.
4915*22ce4affSfengbojiang 	 */
4916*22ce4affSfengbojiang 	if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
4917*22ce4affSfengbojiang 		if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
4918*22ce4affSfengbojiang 
4919a9643ea8Slogwang 	if ((err = iflib_netmap_attach(ctx))) {
4920a9643ea8Slogwang 		device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
4921a9643ea8Slogwang 		goto fail_detach;
4922a9643ea8Slogwang 	}
4923a9643ea8Slogwang 	*ctxp = ctx;
4924a9643ea8Slogwang 
4925*22ce4affSfengbojiang 	DEBUGNET_SET(ctx->ifc_ifp, iflib);
4926*22ce4affSfengbojiang 
4927a9643ea8Slogwang 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
4928a9643ea8Slogwang 	iflib_add_device_sysctl_post(ctx);
4929*22ce4affSfengbojiang 	iflib_add_pfil(ctx);
4930*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_INIT_DONE;
4931*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
4932*22ce4affSfengbojiang 
4933a9643ea8Slogwang 	return (0);
4934*22ce4affSfengbojiang 
4935a9643ea8Slogwang fail_detach:
4936a9643ea8Slogwang 	ether_ifdetach(ctx->ifc_ifp);
4937a9643ea8Slogwang fail_intr_free:
4938*22ce4affSfengbojiang 	iflib_free_intr_mem(ctx);
4939a9643ea8Slogwang fail_queues:
4940*22ce4affSfengbojiang 	iflib_tx_structures_free(ctx);
4941*22ce4affSfengbojiang 	iflib_rx_structures_free(ctx);
4942*22ce4affSfengbojiang 	iflib_tqg_detach(ctx);
4943a9643ea8Slogwang 	IFDI_DETACH(ctx);
4944*22ce4affSfengbojiang fail_unlock:
4945*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
4946*22ce4affSfengbojiang 	iflib_deregister(ctx);
4947*22ce4affSfengbojiang fail_ctx_free:
4948*22ce4affSfengbojiang 	device_set_softc(ctx->ifc_dev, NULL);
4949*22ce4affSfengbojiang         if (ctx->ifc_flags & IFC_SC_ALLOCATED)
4950*22ce4affSfengbojiang                 free(ctx->ifc_softc, M_IFLIB);
4951*22ce4affSfengbojiang         free(ctx, M_IFLIB);
4952a9643ea8Slogwang 	return (err);
4953a9643ea8Slogwang }
4954a9643ea8Slogwang 
4955a9643ea8Slogwang int
iflib_pseudo_register(device_t dev,if_shared_ctx_t sctx,if_ctx_t * ctxp,struct iflib_cloneattach_ctx * clctx)4956*22ce4affSfengbojiang iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp,
4957*22ce4affSfengbojiang 					  struct iflib_cloneattach_ctx *clctx)
4958*22ce4affSfengbojiang {
4959*22ce4affSfengbojiang 	int num_txd, num_rxd;
4960*22ce4affSfengbojiang 	int err;
4961*22ce4affSfengbojiang 	if_ctx_t ctx;
4962*22ce4affSfengbojiang 	if_t ifp;
4963*22ce4affSfengbojiang 	if_softc_ctx_t scctx;
4964*22ce4affSfengbojiang 	int i;
4965*22ce4affSfengbojiang 	void *sc;
4966*22ce4affSfengbojiang 
4967*22ce4affSfengbojiang 	ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO);
4968*22ce4affSfengbojiang 	sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
4969*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_SC_ALLOCATED;
4970*22ce4affSfengbojiang 	if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL))
4971*22ce4affSfengbojiang 		ctx->ifc_flags |= IFC_PSEUDO;
4972*22ce4affSfengbojiang 
4973*22ce4affSfengbojiang 	ctx->ifc_sctx = sctx;
4974*22ce4affSfengbojiang 	ctx->ifc_softc = sc;
4975*22ce4affSfengbojiang 	ctx->ifc_dev = dev;
4976*22ce4affSfengbojiang 
4977*22ce4affSfengbojiang 	if ((err = iflib_register(ctx)) != 0) {
4978*22ce4affSfengbojiang 		device_printf(dev, "%s: iflib_register failed %d\n", __func__, err);
4979*22ce4affSfengbojiang 		goto fail_ctx_free;
4980*22ce4affSfengbojiang 	}
4981*22ce4affSfengbojiang 	iflib_add_device_sysctl_pre(ctx);
4982*22ce4affSfengbojiang 
4983*22ce4affSfengbojiang 	scctx = &ctx->ifc_softc_ctx;
4984*22ce4affSfengbojiang 	ifp = ctx->ifc_ifp;
4985*22ce4affSfengbojiang 
4986*22ce4affSfengbojiang 	iflib_reset_qvalues(ctx);
4987*22ce4affSfengbojiang 	CTX_LOCK(ctx);
4988*22ce4affSfengbojiang 	if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
4989*22ce4affSfengbojiang 		device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
4990*22ce4affSfengbojiang 		goto fail_unlock;
4991*22ce4affSfengbojiang 	}
4992*22ce4affSfengbojiang 	if (sctx->isc_flags & IFLIB_GEN_MAC)
4993*22ce4affSfengbojiang 		ether_gen_addr(ifp, &ctx->ifc_mac);
4994*22ce4affSfengbojiang 	if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name,
4995*22ce4affSfengbojiang 								clctx->cc_params)) != 0) {
4996*22ce4affSfengbojiang 		device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err);
4997*22ce4affSfengbojiang 		goto fail_unlock;
4998*22ce4affSfengbojiang 	}
4999*22ce4affSfengbojiang #ifdef INVARIANTS
5000*22ce4affSfengbojiang 	if (scctx->isc_capabilities & IFCAP_TXCSUM)
5001*22ce4affSfengbojiang 		MPASS(scctx->isc_tx_csum_flags);
5002*22ce4affSfengbojiang #endif
5003*22ce4affSfengbojiang 
5004*22ce4affSfengbojiang 	if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_LINKSTATE);
5005*22ce4affSfengbojiang 	if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE);
5006*22ce4affSfengbojiang 
5007*22ce4affSfengbojiang 	ifp->if_flags |= IFF_NOGROUP;
5008*22ce4affSfengbojiang 	if (sctx->isc_flags & IFLIB_PSEUDO) {
5009*22ce4affSfengbojiang 		ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL);
5010*22ce4affSfengbojiang 		ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO);
5011*22ce4affSfengbojiang 		if (sctx->isc_flags & IFLIB_PSEUDO_ETHER) {
5012*22ce4affSfengbojiang 			ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
5013*22ce4affSfengbojiang 		} else {
5014*22ce4affSfengbojiang 			if_attach(ctx->ifc_ifp);
5015*22ce4affSfengbojiang 			bpfattach(ctx->ifc_ifp, DLT_NULL, sizeof(u_int32_t));
5016*22ce4affSfengbojiang 		}
5017*22ce4affSfengbojiang 
5018*22ce4affSfengbojiang 		if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
5019*22ce4affSfengbojiang 			device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
5020*22ce4affSfengbojiang 			goto fail_detach;
5021*22ce4affSfengbojiang 		}
5022*22ce4affSfengbojiang 		*ctxp = ctx;
5023*22ce4affSfengbojiang 
5024*22ce4affSfengbojiang 		/*
5025*22ce4affSfengbojiang 		 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
5026*22ce4affSfengbojiang 		 * This must appear after the call to ether_ifattach() because
5027*22ce4affSfengbojiang 		 * ether_ifattach() sets if_hdrlen to the default value.
5028*22ce4affSfengbojiang 		 */
5029*22ce4affSfengbojiang 		if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
5030*22ce4affSfengbojiang 			if_setifheaderlen(ifp,
5031*22ce4affSfengbojiang 			    sizeof(struct ether_vlan_header));
5032*22ce4affSfengbojiang 
5033*22ce4affSfengbojiang 		if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
5034*22ce4affSfengbojiang 		iflib_add_device_sysctl_post(ctx);
5035*22ce4affSfengbojiang 		ctx->ifc_flags |= IFC_INIT_DONE;
5036*22ce4affSfengbojiang 		CTX_UNLOCK(ctx);
5037*22ce4affSfengbojiang 		return (0);
5038*22ce4affSfengbojiang 	}
5039*22ce4affSfengbojiang 	ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
5040*22ce4affSfengbojiang 	ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL);
5041*22ce4affSfengbojiang 	ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO);
5042*22ce4affSfengbojiang 
5043*22ce4affSfengbojiang 	_iflib_pre_assert(scctx);
5044*22ce4affSfengbojiang 	ctx->ifc_txrx = *scctx->isc_txrx;
5045*22ce4affSfengbojiang 
5046*22ce4affSfengbojiang 	if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
5047*22ce4affSfengbojiang 		scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
5048*22ce4affSfengbojiang 	if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
5049*22ce4affSfengbojiang 		scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
5050*22ce4affSfengbojiang 
5051*22ce4affSfengbojiang 	num_txd = iflib_num_tx_descs(ctx);
5052*22ce4affSfengbojiang 	num_rxd = iflib_num_rx_descs(ctx);
5053*22ce4affSfengbojiang 
5054*22ce4affSfengbojiang 	/* XXX change for per-queue sizes */
5055*22ce4affSfengbojiang 	device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n",
5056*22ce4affSfengbojiang 	    num_txd, num_rxd);
5057*22ce4affSfengbojiang 
5058*22ce4affSfengbojiang 	if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION)
5059*22ce4affSfengbojiang 		scctx->isc_tx_nsegments = max(1, num_txd /
5060*22ce4affSfengbojiang 		    MAX_SINGLE_PACKET_FRACTION);
5061*22ce4affSfengbojiang 	if (scctx->isc_tx_tso_segments_max > num_txd /
5062*22ce4affSfengbojiang 	    MAX_SINGLE_PACKET_FRACTION)
5063*22ce4affSfengbojiang 		scctx->isc_tx_tso_segments_max = max(1,
5064*22ce4affSfengbojiang 		    num_txd / MAX_SINGLE_PACKET_FRACTION);
5065*22ce4affSfengbojiang 
5066*22ce4affSfengbojiang 	/* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
5067*22ce4affSfengbojiang 	if (if_getcapabilities(ifp) & IFCAP_TSO) {
5068*22ce4affSfengbojiang 		/*
5069*22ce4affSfengbojiang 		 * The stack can't handle a TSO size larger than IP_MAXPACKET,
5070*22ce4affSfengbojiang 		 * but some MACs do.
5071*22ce4affSfengbojiang 		 */
5072*22ce4affSfengbojiang 		if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max,
5073*22ce4affSfengbojiang 		    IP_MAXPACKET));
5074*22ce4affSfengbojiang 		/*
5075*22ce4affSfengbojiang 		 * Take maximum number of m_pullup(9)'s in iflib_parse_header()
5076*22ce4affSfengbojiang 		 * into account.  In the worst case, each of these calls will
5077*22ce4affSfengbojiang 		 * add another mbuf and, thus, the requirement for another DMA
5078*22ce4affSfengbojiang 		 * segment.  So for best performance, it doesn't make sense to
5079*22ce4affSfengbojiang 		 * advertize a maximum of TSO segments that typically will
5080*22ce4affSfengbojiang 		 * require defragmentation in iflib_encap().
5081*22ce4affSfengbojiang 		 */
5082*22ce4affSfengbojiang 		if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3);
5083*22ce4affSfengbojiang 		if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max);
5084*22ce4affSfengbojiang 	}
5085*22ce4affSfengbojiang 	if (scctx->isc_rss_table_size == 0)
5086*22ce4affSfengbojiang 		scctx->isc_rss_table_size = 64;
5087*22ce4affSfengbojiang 	scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
5088*22ce4affSfengbojiang 
5089*22ce4affSfengbojiang 	GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
5090*22ce4affSfengbojiang 	/* XXX format name */
5091*22ce4affSfengbojiang 	taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx,
5092*22ce4affSfengbojiang 	    NULL, NULL, "admin");
5093*22ce4affSfengbojiang 
5094*22ce4affSfengbojiang 	/* XXX --- can support > 1 -- but keep it simple for now */
5095*22ce4affSfengbojiang 	scctx->isc_intr = IFLIB_INTR_LEGACY;
5096*22ce4affSfengbojiang 
5097*22ce4affSfengbojiang 	/* Get memory for the station queues */
5098*22ce4affSfengbojiang 	if ((err = iflib_queues_alloc(ctx))) {
5099*22ce4affSfengbojiang 		device_printf(dev, "Unable to allocate queue memory\n");
5100*22ce4affSfengbojiang 		goto fail_iflib_detach;
5101*22ce4affSfengbojiang 	}
5102*22ce4affSfengbojiang 
5103*22ce4affSfengbojiang 	if ((err = iflib_qset_structures_setup(ctx))) {
5104*22ce4affSfengbojiang 		device_printf(dev, "qset structure setup failed %d\n", err);
5105*22ce4affSfengbojiang 		goto fail_queues;
5106*22ce4affSfengbojiang 	}
5107*22ce4affSfengbojiang 
5108*22ce4affSfengbojiang 	/*
5109*22ce4affSfengbojiang 	 * XXX What if anything do we want to do about interrupts?
5110*22ce4affSfengbojiang 	 */
5111*22ce4affSfengbojiang 	ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
5112*22ce4affSfengbojiang 	if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
5113*22ce4affSfengbojiang 		device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
5114*22ce4affSfengbojiang 		goto fail_detach;
5115*22ce4affSfengbojiang 	}
5116*22ce4affSfengbojiang 
5117*22ce4affSfengbojiang 	/*
5118*22ce4affSfengbojiang 	 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
5119*22ce4affSfengbojiang 	 * This must appear after the call to ether_ifattach() because
5120*22ce4affSfengbojiang 	 * ether_ifattach() sets if_hdrlen to the default value.
5121*22ce4affSfengbojiang 	 */
5122*22ce4affSfengbojiang 	if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
5123*22ce4affSfengbojiang 		if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
5124*22ce4affSfengbojiang 
5125*22ce4affSfengbojiang 	/* XXX handle more than one queue */
5126*22ce4affSfengbojiang 	for (i = 0; i < scctx->isc_nrxqsets; i++)
5127*22ce4affSfengbojiang 		IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl);
5128*22ce4affSfengbojiang 
5129*22ce4affSfengbojiang 	*ctxp = ctx;
5130*22ce4affSfengbojiang 
5131*22ce4affSfengbojiang 	if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
5132*22ce4affSfengbojiang 	iflib_add_device_sysctl_post(ctx);
5133*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_INIT_DONE;
5134*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
5135*22ce4affSfengbojiang 
5136*22ce4affSfengbojiang 	return (0);
5137*22ce4affSfengbojiang fail_detach:
5138*22ce4affSfengbojiang 	ether_ifdetach(ctx->ifc_ifp);
5139*22ce4affSfengbojiang fail_queues:
5140*22ce4affSfengbojiang 	iflib_tx_structures_free(ctx);
5141*22ce4affSfengbojiang 	iflib_rx_structures_free(ctx);
5142*22ce4affSfengbojiang 	iflib_tqg_detach(ctx);
5143*22ce4affSfengbojiang fail_iflib_detach:
5144*22ce4affSfengbojiang 	IFDI_DETACH(ctx);
5145*22ce4affSfengbojiang fail_unlock:
5146*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
5147*22ce4affSfengbojiang 	iflib_deregister(ctx);
5148*22ce4affSfengbojiang fail_ctx_free:
5149*22ce4affSfengbojiang 	free(ctx->ifc_softc, M_IFLIB);
5150*22ce4affSfengbojiang 	free(ctx, M_IFLIB);
5151*22ce4affSfengbojiang 	return (err);
5152*22ce4affSfengbojiang }
5153*22ce4affSfengbojiang 
5154*22ce4affSfengbojiang int
iflib_pseudo_deregister(if_ctx_t ctx)5155*22ce4affSfengbojiang iflib_pseudo_deregister(if_ctx_t ctx)
5156*22ce4affSfengbojiang {
5157*22ce4affSfengbojiang 	if_t ifp = ctx->ifc_ifp;
5158*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
5159*22ce4affSfengbojiang 
5160*22ce4affSfengbojiang 	/* Unregister VLAN event handlers early */
5161*22ce4affSfengbojiang 	iflib_unregister_vlan_handlers(ctx);
5162*22ce4affSfengbojiang 
5163*22ce4affSfengbojiang 	if ((sctx->isc_flags & IFLIB_PSEUDO)  &&
5164*22ce4affSfengbojiang 		(sctx->isc_flags & IFLIB_PSEUDO_ETHER) == 0) {
5165*22ce4affSfengbojiang 		bpfdetach(ifp);
5166*22ce4affSfengbojiang 		if_detach(ifp);
5167*22ce4affSfengbojiang 	} else {
5168*22ce4affSfengbojiang 		ether_ifdetach(ifp);
5169*22ce4affSfengbojiang 	}
5170*22ce4affSfengbojiang 
5171*22ce4affSfengbojiang 	iflib_tqg_detach(ctx);
5172*22ce4affSfengbojiang 	iflib_tx_structures_free(ctx);
5173*22ce4affSfengbojiang 	iflib_rx_structures_free(ctx);
5174*22ce4affSfengbojiang 
5175*22ce4affSfengbojiang 	iflib_deregister(ctx);
5176*22ce4affSfengbojiang 
5177*22ce4affSfengbojiang 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
5178*22ce4affSfengbojiang 		free(ctx->ifc_softc, M_IFLIB);
5179*22ce4affSfengbojiang 	free(ctx, M_IFLIB);
5180*22ce4affSfengbojiang 	return (0);
5181*22ce4affSfengbojiang }
5182*22ce4affSfengbojiang 
5183*22ce4affSfengbojiang int
iflib_device_attach(device_t dev)5184a9643ea8Slogwang iflib_device_attach(device_t dev)
5185a9643ea8Slogwang {
5186a9643ea8Slogwang 	if_ctx_t ctx;
5187a9643ea8Slogwang 	if_shared_ctx_t sctx;
5188a9643ea8Slogwang 
5189a9643ea8Slogwang 	if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
5190a9643ea8Slogwang 		return (ENOTSUP);
5191a9643ea8Slogwang 
5192a9643ea8Slogwang 	pci_enable_busmaster(dev);
5193a9643ea8Slogwang 
5194a9643ea8Slogwang 	return (iflib_device_register(dev, NULL, sctx, &ctx));
5195a9643ea8Slogwang }
5196a9643ea8Slogwang 
5197a9643ea8Slogwang int
iflib_device_deregister(if_ctx_t ctx)5198a9643ea8Slogwang iflib_device_deregister(if_ctx_t ctx)
5199a9643ea8Slogwang {
5200a9643ea8Slogwang 	if_t ifp = ctx->ifc_ifp;
5201a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
5202a9643ea8Slogwang 
5203a9643ea8Slogwang 	/* Make sure VLANS are not using driver */
5204a9643ea8Slogwang 	if (if_vlantrunkinuse(ifp)) {
5205a9643ea8Slogwang 		device_printf(dev, "Vlan in use, detach first\n");
5206a9643ea8Slogwang 		return (EBUSY);
5207a9643ea8Slogwang 	}
5208*22ce4affSfengbojiang #ifdef PCI_IOV
5209*22ce4affSfengbojiang 	if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) {
5210*22ce4affSfengbojiang 		device_printf(dev, "SR-IOV in use; detach first.\n");
5211*22ce4affSfengbojiang 		return (EBUSY);
5212*22ce4affSfengbojiang 	}
5213*22ce4affSfengbojiang #endif
5214a9643ea8Slogwang 
5215*22ce4affSfengbojiang 	STATE_LOCK(ctx);
5216*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_IN_DETACH;
5217*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
5218a9643ea8Slogwang 
5219*22ce4affSfengbojiang 	/* Unregister VLAN handlers before calling iflib_stop() */
5220*22ce4affSfengbojiang 	iflib_unregister_vlan_handlers(ctx);
5221a9643ea8Slogwang 
5222a9643ea8Slogwang 	iflib_netmap_detach(ifp);
5223a9643ea8Slogwang 	ether_ifdetach(ifp);
5224*22ce4affSfengbojiang 
5225*22ce4affSfengbojiang 	CTX_LOCK(ctx);
5226*22ce4affSfengbojiang 	iflib_stop(ctx);
5227*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
5228*22ce4affSfengbojiang 
5229*22ce4affSfengbojiang 	iflib_rem_pfil(ctx);
5230a9643ea8Slogwang 	if (ctx->ifc_led_dev != NULL)
5231a9643ea8Slogwang 		led_destroy(ctx->ifc_led_dev);
5232*22ce4affSfengbojiang 
5233*22ce4affSfengbojiang 	iflib_tqg_detach(ctx);
5234*22ce4affSfengbojiang 	CTX_LOCK(ctx);
5235*22ce4affSfengbojiang 	IFDI_DETACH(ctx);
5236*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
5237*22ce4affSfengbojiang 
5238*22ce4affSfengbojiang 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
5239*22ce4affSfengbojiang 	iflib_free_intr_mem(ctx);
5240*22ce4affSfengbojiang 
5241*22ce4affSfengbojiang 	bus_generic_detach(dev);
5242*22ce4affSfengbojiang 
5243*22ce4affSfengbojiang 	iflib_tx_structures_free(ctx);
5244*22ce4affSfengbojiang 	iflib_rx_structures_free(ctx);
5245*22ce4affSfengbojiang 
5246*22ce4affSfengbojiang 	iflib_deregister(ctx);
5247*22ce4affSfengbojiang 
5248*22ce4affSfengbojiang 	device_set_softc(ctx->ifc_dev, NULL);
5249*22ce4affSfengbojiang 	if (ctx->ifc_flags & IFC_SC_ALLOCATED)
5250*22ce4affSfengbojiang 		free(ctx->ifc_softc, M_IFLIB);
5251*22ce4affSfengbojiang 	unref_ctx_core_offset(ctx);
5252*22ce4affSfengbojiang 	free(ctx, M_IFLIB);
5253*22ce4affSfengbojiang 	return (0);
5254*22ce4affSfengbojiang }
5255*22ce4affSfengbojiang 
5256*22ce4affSfengbojiang static void
iflib_tqg_detach(if_ctx_t ctx)5257*22ce4affSfengbojiang iflib_tqg_detach(if_ctx_t ctx)
5258*22ce4affSfengbojiang {
5259*22ce4affSfengbojiang 	iflib_txq_t txq;
5260*22ce4affSfengbojiang 	iflib_rxq_t rxq;
5261*22ce4affSfengbojiang 	int i;
5262*22ce4affSfengbojiang 	struct taskqgroup *tqg;
5263*22ce4affSfengbojiang 
5264a9643ea8Slogwang 	/* XXX drain any dependent tasks */
5265a9643ea8Slogwang 	tqg = qgroup_if_io_tqg;
5266a9643ea8Slogwang 	for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
5267a9643ea8Slogwang 		callout_drain(&txq->ift_timer);
5268*22ce4affSfengbojiang #ifdef DEV_NETMAP
5269*22ce4affSfengbojiang 		callout_drain(&txq->ift_netmap_timer);
5270*22ce4affSfengbojiang #endif /* DEV_NETMAP */
5271a9643ea8Slogwang 		if (txq->ift_task.gt_uniq != NULL)
5272a9643ea8Slogwang 			taskqgroup_detach(tqg, &txq->ift_task);
5273a9643ea8Slogwang 	}
5274a9643ea8Slogwang 	for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
5275a9643ea8Slogwang 		if (rxq->ifr_task.gt_uniq != NULL)
5276a9643ea8Slogwang 			taskqgroup_detach(tqg, &rxq->ifr_task);
5277a9643ea8Slogwang 	}
5278a9643ea8Slogwang 	tqg = qgroup_if_config_tqg;
5279a9643ea8Slogwang 	if (ctx->ifc_admin_task.gt_uniq != NULL)
5280a9643ea8Slogwang 		taskqgroup_detach(tqg, &ctx->ifc_admin_task);
5281a9643ea8Slogwang 	if (ctx->ifc_vflr_task.gt_uniq != NULL)
5282a9643ea8Slogwang 		taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
5283a9643ea8Slogwang }
5284*22ce4affSfengbojiang 
5285*22ce4affSfengbojiang static void
iflib_free_intr_mem(if_ctx_t ctx)5286*22ce4affSfengbojiang iflib_free_intr_mem(if_ctx_t ctx)
5287*22ce4affSfengbojiang {
5288*22ce4affSfengbojiang 
5289a9643ea8Slogwang 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
5290a9643ea8Slogwang 		iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
5291a9643ea8Slogwang 	}
5292*22ce4affSfengbojiang 	if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
5293*22ce4affSfengbojiang 		pci_release_msi(ctx->ifc_dev);
5294*22ce4affSfengbojiang 	}
5295a9643ea8Slogwang 	if (ctx->ifc_msix_mem != NULL) {
5296a9643ea8Slogwang 		bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
5297*22ce4affSfengbojiang 		    rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem);
5298a9643ea8Slogwang 		ctx->ifc_msix_mem = NULL;
5299a9643ea8Slogwang 	}
5300a9643ea8Slogwang }
5301a9643ea8Slogwang 
5302a9643ea8Slogwang int
iflib_device_detach(device_t dev)5303a9643ea8Slogwang iflib_device_detach(device_t dev)
5304a9643ea8Slogwang {
5305a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5306a9643ea8Slogwang 
5307a9643ea8Slogwang 	return (iflib_device_deregister(ctx));
5308a9643ea8Slogwang }
5309a9643ea8Slogwang 
5310a9643ea8Slogwang int
iflib_device_suspend(device_t dev)5311a9643ea8Slogwang iflib_device_suspend(device_t dev)
5312a9643ea8Slogwang {
5313a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5314a9643ea8Slogwang 
5315a9643ea8Slogwang 	CTX_LOCK(ctx);
5316a9643ea8Slogwang 	IFDI_SUSPEND(ctx);
5317a9643ea8Slogwang 	CTX_UNLOCK(ctx);
5318a9643ea8Slogwang 
5319a9643ea8Slogwang 	return bus_generic_suspend(dev);
5320a9643ea8Slogwang }
5321a9643ea8Slogwang int
iflib_device_shutdown(device_t dev)5322a9643ea8Slogwang iflib_device_shutdown(device_t dev)
5323a9643ea8Slogwang {
5324a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5325a9643ea8Slogwang 
5326a9643ea8Slogwang 	CTX_LOCK(ctx);
5327a9643ea8Slogwang 	IFDI_SHUTDOWN(ctx);
5328a9643ea8Slogwang 	CTX_UNLOCK(ctx);
5329a9643ea8Slogwang 
5330a9643ea8Slogwang 	return bus_generic_suspend(dev);
5331a9643ea8Slogwang }
5332a9643ea8Slogwang 
5333a9643ea8Slogwang int
iflib_device_resume(device_t dev)5334a9643ea8Slogwang iflib_device_resume(device_t dev)
5335a9643ea8Slogwang {
5336a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5337a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
5338a9643ea8Slogwang 
5339a9643ea8Slogwang 	CTX_LOCK(ctx);
5340a9643ea8Slogwang 	IFDI_RESUME(ctx);
5341*22ce4affSfengbojiang 	iflib_if_init_locked(ctx);
5342a9643ea8Slogwang 	CTX_UNLOCK(ctx);
5343a9643ea8Slogwang 	for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
5344a9643ea8Slogwang 		iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
5345a9643ea8Slogwang 
5346a9643ea8Slogwang 	return (bus_generic_resume(dev));
5347a9643ea8Slogwang }
5348a9643ea8Slogwang 
5349a9643ea8Slogwang int
iflib_device_iov_init(device_t dev,uint16_t num_vfs,const nvlist_t * params)5350a9643ea8Slogwang iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
5351a9643ea8Slogwang {
5352a9643ea8Slogwang 	int error;
5353a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5354a9643ea8Slogwang 
5355a9643ea8Slogwang 	CTX_LOCK(ctx);
5356a9643ea8Slogwang 	error = IFDI_IOV_INIT(ctx, num_vfs, params);
5357a9643ea8Slogwang 	CTX_UNLOCK(ctx);
5358a9643ea8Slogwang 
5359a9643ea8Slogwang 	return (error);
5360a9643ea8Slogwang }
5361a9643ea8Slogwang 
5362a9643ea8Slogwang void
iflib_device_iov_uninit(device_t dev)5363a9643ea8Slogwang iflib_device_iov_uninit(device_t dev)
5364a9643ea8Slogwang {
5365a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5366a9643ea8Slogwang 
5367a9643ea8Slogwang 	CTX_LOCK(ctx);
5368a9643ea8Slogwang 	IFDI_IOV_UNINIT(ctx);
5369a9643ea8Slogwang 	CTX_UNLOCK(ctx);
5370a9643ea8Slogwang }
5371a9643ea8Slogwang 
5372a9643ea8Slogwang int
iflib_device_iov_add_vf(device_t dev,uint16_t vfnum,const nvlist_t * params)5373a9643ea8Slogwang iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
5374a9643ea8Slogwang {
5375a9643ea8Slogwang 	int error;
5376a9643ea8Slogwang 	if_ctx_t ctx = device_get_softc(dev);
5377a9643ea8Slogwang 
5378a9643ea8Slogwang 	CTX_LOCK(ctx);
5379a9643ea8Slogwang 	error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
5380a9643ea8Slogwang 	CTX_UNLOCK(ctx);
5381a9643ea8Slogwang 
5382a9643ea8Slogwang 	return (error);
5383a9643ea8Slogwang }
5384a9643ea8Slogwang 
5385a9643ea8Slogwang /*********************************************************************
5386a9643ea8Slogwang  *
5387a9643ea8Slogwang  *  MODULE FUNCTION DEFINITIONS
5388a9643ea8Slogwang  *
5389a9643ea8Slogwang  **********************************************************************/
5390a9643ea8Slogwang 
5391a9643ea8Slogwang /*
5392a9643ea8Slogwang  * - Start a fast taskqueue thread for each core
5393a9643ea8Slogwang  * - Start a taskqueue for control operations
5394a9643ea8Slogwang  */
5395a9643ea8Slogwang static int
iflib_module_init(void)5396a9643ea8Slogwang iflib_module_init(void)
5397a9643ea8Slogwang {
5398*22ce4affSfengbojiang 	iflib_timer_default = hz / 2;
5399a9643ea8Slogwang 	return (0);
5400a9643ea8Slogwang }
5401a9643ea8Slogwang 
5402a9643ea8Slogwang static int
iflib_module_event_handler(module_t mod,int what,void * arg)5403a9643ea8Slogwang iflib_module_event_handler(module_t mod, int what, void *arg)
5404a9643ea8Slogwang {
5405a9643ea8Slogwang 	int err;
5406a9643ea8Slogwang 
5407a9643ea8Slogwang 	switch (what) {
5408a9643ea8Slogwang 	case MOD_LOAD:
5409a9643ea8Slogwang 		if ((err = iflib_module_init()) != 0)
5410a9643ea8Slogwang 			return (err);
5411a9643ea8Slogwang 		break;
5412a9643ea8Slogwang 	case MOD_UNLOAD:
5413a9643ea8Slogwang 		return (EBUSY);
5414a9643ea8Slogwang 	default:
5415a9643ea8Slogwang 		return (EOPNOTSUPP);
5416a9643ea8Slogwang 	}
5417a9643ea8Slogwang 
5418a9643ea8Slogwang 	return (0);
5419a9643ea8Slogwang }
5420a9643ea8Slogwang 
5421a9643ea8Slogwang /*********************************************************************
5422a9643ea8Slogwang  *
5423a9643ea8Slogwang  *  PUBLIC FUNCTION DEFINITIONS
5424a9643ea8Slogwang  *     ordered as in iflib.h
5425a9643ea8Slogwang  *
5426a9643ea8Slogwang  **********************************************************************/
5427a9643ea8Slogwang 
5428a9643ea8Slogwang static void
_iflib_assert(if_shared_ctx_t sctx)5429a9643ea8Slogwang _iflib_assert(if_shared_ctx_t sctx)
5430a9643ea8Slogwang {
5431*22ce4affSfengbojiang 	int i;
5432*22ce4affSfengbojiang 
5433a9643ea8Slogwang 	MPASS(sctx->isc_tx_maxsize);
5434a9643ea8Slogwang 	MPASS(sctx->isc_tx_maxsegsize);
5435a9643ea8Slogwang 
5436a9643ea8Slogwang 	MPASS(sctx->isc_rx_maxsize);
5437a9643ea8Slogwang 	MPASS(sctx->isc_rx_nsegments);
5438a9643ea8Slogwang 	MPASS(sctx->isc_rx_maxsegsize);
5439a9643ea8Slogwang 
5440*22ce4affSfengbojiang 	MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8);
5441*22ce4affSfengbojiang 	for (i = 0; i < sctx->isc_nrxqs; i++) {
5442*22ce4affSfengbojiang 		MPASS(sctx->isc_nrxd_min[i]);
5443*22ce4affSfengbojiang 		MPASS(powerof2(sctx->isc_nrxd_min[i]));
5444*22ce4affSfengbojiang 		MPASS(sctx->isc_nrxd_max[i]);
5445*22ce4affSfengbojiang 		MPASS(powerof2(sctx->isc_nrxd_max[i]));
5446*22ce4affSfengbojiang 		MPASS(sctx->isc_nrxd_default[i]);
5447*22ce4affSfengbojiang 		MPASS(powerof2(sctx->isc_nrxd_default[i]));
5448*22ce4affSfengbojiang 	}
5449a9643ea8Slogwang 
5450*22ce4affSfengbojiang 	MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8);
5451*22ce4affSfengbojiang 	for (i = 0; i < sctx->isc_ntxqs; i++) {
5452*22ce4affSfengbojiang 		MPASS(sctx->isc_ntxd_min[i]);
5453*22ce4affSfengbojiang 		MPASS(powerof2(sctx->isc_ntxd_min[i]));
5454*22ce4affSfengbojiang 		MPASS(sctx->isc_ntxd_max[i]);
5455*22ce4affSfengbojiang 		MPASS(powerof2(sctx->isc_ntxd_max[i]));
5456*22ce4affSfengbojiang 		MPASS(sctx->isc_ntxd_default[i]);
5457*22ce4affSfengbojiang 		MPASS(powerof2(sctx->isc_ntxd_default[i]));
5458*22ce4affSfengbojiang 	}
5459*22ce4affSfengbojiang }
5460a9643ea8Slogwang 
5461*22ce4affSfengbojiang static void
_iflib_pre_assert(if_softc_ctx_t scctx)5462*22ce4affSfengbojiang _iflib_pre_assert(if_softc_ctx_t scctx)
5463*22ce4affSfengbojiang {
5464*22ce4affSfengbojiang 
5465*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_txd_encap);
5466*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_txd_flush);
5467*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_txd_credits_update);
5468*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_rxd_available);
5469*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
5470*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_rxd_refill);
5471*22ce4affSfengbojiang 	MPASS(scctx->isc_txrx->ift_rxd_flush);
5472a9643ea8Slogwang }
5473a9643ea8Slogwang 
5474a9643ea8Slogwang static int
iflib_register(if_ctx_t ctx)5475a9643ea8Slogwang iflib_register(if_ctx_t ctx)
5476a9643ea8Slogwang {
5477a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
5478a9643ea8Slogwang 	driver_t *driver = sctx->isc_driver;
5479a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
5480a9643ea8Slogwang 	if_t ifp;
5481*22ce4affSfengbojiang 	u_char type;
5482*22ce4affSfengbojiang 	int iflags;
5483a9643ea8Slogwang 
5484*22ce4affSfengbojiang 	if ((sctx->isc_flags & IFLIB_PSEUDO) == 0)
5485a9643ea8Slogwang 		_iflib_assert(sctx);
5486a9643ea8Slogwang 
5487*22ce4affSfengbojiang 	CTX_LOCK_INIT(ctx);
5488*22ce4affSfengbojiang 	STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
5489*22ce4affSfengbojiang 	if (sctx->isc_flags & IFLIB_PSEUDO) {
5490*22ce4affSfengbojiang 		if (sctx->isc_flags & IFLIB_PSEUDO_ETHER)
5491*22ce4affSfengbojiang 			type = IFT_ETHER;
5492*22ce4affSfengbojiang 		else
5493*22ce4affSfengbojiang 			type = IFT_PPP;
5494*22ce4affSfengbojiang 	} else
5495*22ce4affSfengbojiang 		type = IFT_ETHER;
5496*22ce4affSfengbojiang 	ifp = ctx->ifc_ifp = if_alloc(type);
5497a9643ea8Slogwang 	if (ifp == NULL) {
5498a9643ea8Slogwang 		device_printf(dev, "can not allocate ifnet structure\n");
5499a9643ea8Slogwang 		return (ENOMEM);
5500a9643ea8Slogwang 	}
5501a9643ea8Slogwang 
5502a9643ea8Slogwang 	/*
5503a9643ea8Slogwang 	 * Initialize our context's device specific methods
5504a9643ea8Slogwang 	 */
5505a9643ea8Slogwang 	kobj_init((kobj_t) ctx, (kobj_class_t) driver);
5506a9643ea8Slogwang 	kobj_class_compile((kobj_class_t) driver);
5507a9643ea8Slogwang 
5508a9643ea8Slogwang 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
5509a9643ea8Slogwang 	if_setsoftc(ifp, ctx);
5510a9643ea8Slogwang 	if_setdev(ifp, dev);
5511a9643ea8Slogwang 	if_setinitfn(ifp, iflib_if_init);
5512a9643ea8Slogwang 	if_setioctlfn(ifp, iflib_if_ioctl);
5513*22ce4affSfengbojiang #ifdef ALTQ
5514*22ce4affSfengbojiang 	if_setstartfn(ifp, iflib_altq_if_start);
5515*22ce4affSfengbojiang 	if_settransmitfn(ifp, iflib_altq_if_transmit);
5516*22ce4affSfengbojiang 	if_setsendqready(ifp);
5517*22ce4affSfengbojiang #else
5518a9643ea8Slogwang 	if_settransmitfn(ifp, iflib_if_transmit);
5519*22ce4affSfengbojiang #endif
5520a9643ea8Slogwang 	if_setqflushfn(ifp, iflib_if_qflush);
5521*22ce4affSfengbojiang 	iflags = IFF_MULTICAST | IFF_KNOWSEPOCH;
5522a9643ea8Slogwang 
5523*22ce4affSfengbojiang 	if ((sctx->isc_flags & IFLIB_PSEUDO) &&
5524*22ce4affSfengbojiang 		(sctx->isc_flags & IFLIB_PSEUDO_ETHER) == 0)
5525*22ce4affSfengbojiang 		iflags |= IFF_POINTOPOINT;
5526*22ce4affSfengbojiang 	else
5527*22ce4affSfengbojiang 		iflags |= IFF_BROADCAST | IFF_SIMPLEX;
5528*22ce4affSfengbojiang 	if_setflags(ifp, iflags);
5529a9643ea8Slogwang 	ctx->ifc_vlan_attach_event =
5530a9643ea8Slogwang 		EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
5531a9643ea8Slogwang 							  EVENTHANDLER_PRI_FIRST);
5532a9643ea8Slogwang 	ctx->ifc_vlan_detach_event =
5533a9643ea8Slogwang 		EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
5534a9643ea8Slogwang 							  EVENTHANDLER_PRI_FIRST);
5535a9643ea8Slogwang 
5536*22ce4affSfengbojiang 	if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) {
5537*22ce4affSfengbojiang 		ctx->ifc_mediap = &ctx->ifc_media;
5538*22ce4affSfengbojiang 		ifmedia_init(ctx->ifc_mediap, IFM_IMASK,
5539a9643ea8Slogwang 		    iflib_media_change, iflib_media_status);
5540*22ce4affSfengbojiang 	}
5541a9643ea8Slogwang 	return (0);
5542a9643ea8Slogwang }
5543a9643ea8Slogwang 
5544*22ce4affSfengbojiang static void
iflib_unregister_vlan_handlers(if_ctx_t ctx)5545*22ce4affSfengbojiang iflib_unregister_vlan_handlers(if_ctx_t ctx)
5546*22ce4affSfengbojiang {
5547*22ce4affSfengbojiang 	/* Unregister VLAN events */
5548*22ce4affSfengbojiang 	if (ctx->ifc_vlan_attach_event != NULL) {
5549*22ce4affSfengbojiang 		EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
5550*22ce4affSfengbojiang 		ctx->ifc_vlan_attach_event = NULL;
5551*22ce4affSfengbojiang 	}
5552*22ce4affSfengbojiang 	if (ctx->ifc_vlan_detach_event != NULL) {
5553*22ce4affSfengbojiang 		EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
5554*22ce4affSfengbojiang 		ctx->ifc_vlan_detach_event = NULL;
5555*22ce4affSfengbojiang 	}
5556*22ce4affSfengbojiang 
5557*22ce4affSfengbojiang }
5558*22ce4affSfengbojiang 
5559*22ce4affSfengbojiang static void
iflib_deregister(if_ctx_t ctx)5560*22ce4affSfengbojiang iflib_deregister(if_ctx_t ctx)
5561*22ce4affSfengbojiang {
5562*22ce4affSfengbojiang 	if_t ifp = ctx->ifc_ifp;
5563*22ce4affSfengbojiang 
5564*22ce4affSfengbojiang 	/* Remove all media */
5565*22ce4affSfengbojiang 	ifmedia_removeall(&ctx->ifc_media);
5566*22ce4affSfengbojiang 
5567*22ce4affSfengbojiang 	/* Ensure that VLAN event handlers are unregistered */
5568*22ce4affSfengbojiang 	iflib_unregister_vlan_handlers(ctx);
5569*22ce4affSfengbojiang 
5570*22ce4affSfengbojiang 	/* Release kobject reference */
5571*22ce4affSfengbojiang 	kobj_delete((kobj_t) ctx, NULL);
5572*22ce4affSfengbojiang 
5573*22ce4affSfengbojiang 	/* Free the ifnet structure */
5574*22ce4affSfengbojiang 	if_free(ifp);
5575*22ce4affSfengbojiang 
5576*22ce4affSfengbojiang 	STATE_LOCK_DESTROY(ctx);
5577*22ce4affSfengbojiang 
5578*22ce4affSfengbojiang 	/* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
5579*22ce4affSfengbojiang 	CTX_LOCK_DESTROY(ctx);
5580*22ce4affSfengbojiang }
5581a9643ea8Slogwang 
5582a9643ea8Slogwang static int
iflib_queues_alloc(if_ctx_t ctx)5583a9643ea8Slogwang iflib_queues_alloc(if_ctx_t ctx)
5584a9643ea8Slogwang {
5585a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
5586a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
5587a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
5588a9643ea8Slogwang 	int nrxqsets = scctx->isc_nrxqsets;
5589a9643ea8Slogwang 	int ntxqsets = scctx->isc_ntxqsets;
5590a9643ea8Slogwang 	iflib_txq_t txq;
5591a9643ea8Slogwang 	iflib_rxq_t rxq;
5592a9643ea8Slogwang 	iflib_fl_t fl = NULL;
5593a9643ea8Slogwang 	int i, j, cpu, err, txconf, rxconf;
5594a9643ea8Slogwang 	iflib_dma_info_t ifdip;
5595a9643ea8Slogwang 	uint32_t *rxqsizes = scctx->isc_rxqsizes;
5596a9643ea8Slogwang 	uint32_t *txqsizes = scctx->isc_txqsizes;
5597a9643ea8Slogwang 	uint8_t nrxqs = sctx->isc_nrxqs;
5598a9643ea8Slogwang 	uint8_t ntxqs = sctx->isc_ntxqs;
5599a9643ea8Slogwang 	int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
5600*22ce4affSfengbojiang 	int fl_offset = (sctx->isc_flags & IFLIB_HAS_RXCQ ? 1 : 0);
5601a9643ea8Slogwang 	caddr_t *vaddrs;
5602a9643ea8Slogwang 	uint64_t *paddrs;
5603a9643ea8Slogwang 
5604a9643ea8Slogwang 	KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
5605a9643ea8Slogwang 	KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
5606*22ce4affSfengbojiang 	KASSERT(nrxqs >= fl_offset + nfree_lists,
5607*22ce4affSfengbojiang            ("there must be at least a rxq for each free list"));
5608a9643ea8Slogwang 
5609a9643ea8Slogwang 	/* Allocate the TX ring struct memory */
5610*22ce4affSfengbojiang 	if (!(ctx->ifc_txqs =
5611a9643ea8Slogwang 	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
5612a9643ea8Slogwang 	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
5613a9643ea8Slogwang 		device_printf(dev, "Unable to allocate TX ring memory\n");
5614a9643ea8Slogwang 		err = ENOMEM;
5615a9643ea8Slogwang 		goto fail;
5616a9643ea8Slogwang 	}
5617a9643ea8Slogwang 
5618a9643ea8Slogwang 	/* Now allocate the RX */
5619*22ce4affSfengbojiang 	if (!(ctx->ifc_rxqs =
5620a9643ea8Slogwang 	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
5621a9643ea8Slogwang 	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
5622a9643ea8Slogwang 		device_printf(dev, "Unable to allocate RX ring memory\n");
5623a9643ea8Slogwang 		err = ENOMEM;
5624a9643ea8Slogwang 		goto rx_fail;
5625a9643ea8Slogwang 	}
5626a9643ea8Slogwang 
5627*22ce4affSfengbojiang 	txq = ctx->ifc_txqs;
5628*22ce4affSfengbojiang 	rxq = ctx->ifc_rxqs;
5629a9643ea8Slogwang 
5630a9643ea8Slogwang 	/*
5631a9643ea8Slogwang 	 * XXX handle allocation failure
5632a9643ea8Slogwang 	 */
5633a9643ea8Slogwang 	for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
5634a9643ea8Slogwang 		/* Set up some basics */
5635a9643ea8Slogwang 
5636*22ce4affSfengbojiang 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs,
5637*22ce4affSfengbojiang 		    M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
5638*22ce4affSfengbojiang 			device_printf(dev,
5639*22ce4affSfengbojiang 			    "Unable to allocate TX DMA info memory\n");
5640a9643ea8Slogwang 			err = ENOMEM;
5641a9643ea8Slogwang 			goto err_tx_desc;
5642a9643ea8Slogwang 		}
5643a9643ea8Slogwang 		txq->ift_ifdi = ifdip;
5644a9643ea8Slogwang 		for (j = 0; j < ntxqs; j++, ifdip++) {
5645*22ce4affSfengbojiang 			if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) {
5646*22ce4affSfengbojiang 				device_printf(dev,
5647*22ce4affSfengbojiang 				    "Unable to allocate TX descriptors\n");
5648a9643ea8Slogwang 				err = ENOMEM;
5649a9643ea8Slogwang 				goto err_tx_desc;
5650a9643ea8Slogwang 			}
5651*22ce4affSfengbojiang 			txq->ift_txd_size[j] = scctx->isc_txd_size[j];
5652a9643ea8Slogwang 			bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
5653a9643ea8Slogwang 		}
5654a9643ea8Slogwang 		txq->ift_ctx = ctx;
5655a9643ea8Slogwang 		txq->ift_id = i;
5656a9643ea8Slogwang 		if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
5657a9643ea8Slogwang 			txq->ift_br_offset = 1;
5658a9643ea8Slogwang 		} else {
5659a9643ea8Slogwang 			txq->ift_br_offset = 0;
5660a9643ea8Slogwang 		}
5661a9643ea8Slogwang 
5662a9643ea8Slogwang 		if (iflib_txsd_alloc(txq)) {
5663a9643ea8Slogwang 			device_printf(dev, "Critical Failure setting up TX buffers\n");
5664a9643ea8Slogwang 			err = ENOMEM;
5665a9643ea8Slogwang 			goto err_tx_desc;
5666a9643ea8Slogwang 		}
5667a9643ea8Slogwang 
5668a9643ea8Slogwang 		/* Initialize the TX lock */
5669*22ce4affSfengbojiang 		snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout",
5670a9643ea8Slogwang 		    device_get_nameunit(dev), txq->ift_id);
5671a9643ea8Slogwang 		mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
5672a9643ea8Slogwang 		callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
5673*22ce4affSfengbojiang 		txq->ift_timer.c_cpu = cpu;
5674*22ce4affSfengbojiang #ifdef DEV_NETMAP
5675*22ce4affSfengbojiang 		callout_init_mtx(&txq->ift_netmap_timer, &txq->ift_mtx, 0);
5676*22ce4affSfengbojiang 		txq->ift_netmap_timer.c_cpu = cpu;
5677*22ce4affSfengbojiang #endif /* DEV_NETMAP */
5678a9643ea8Slogwang 
5679*22ce4affSfengbojiang 		err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
5680a9643ea8Slogwang 				      iflib_txq_can_drain, M_IFLIB, M_WAITOK);
5681a9643ea8Slogwang 		if (err) {
5682a9643ea8Slogwang 			/* XXX free any allocated rings */
5683a9643ea8Slogwang 			device_printf(dev, "Unable to allocate buf_ring\n");
5684a9643ea8Slogwang 			goto err_tx_desc;
5685a9643ea8Slogwang 		}
5686a9643ea8Slogwang 	}
5687a9643ea8Slogwang 
5688a9643ea8Slogwang 	for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
5689a9643ea8Slogwang 		/* Set up some basics */
5690*22ce4affSfengbojiang 		callout_init(&rxq->ifr_watchdog, 1);
5691a9643ea8Slogwang 
5692*22ce4affSfengbojiang 		if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs,
5693*22ce4affSfengbojiang 		   M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
5694*22ce4affSfengbojiang 			device_printf(dev,
5695*22ce4affSfengbojiang 			    "Unable to allocate RX DMA info memory\n");
5696a9643ea8Slogwang 			err = ENOMEM;
5697a9643ea8Slogwang 			goto err_tx_desc;
5698a9643ea8Slogwang 		}
5699a9643ea8Slogwang 
5700a9643ea8Slogwang 		rxq->ifr_ifdi = ifdip;
5701*22ce4affSfengbojiang 		/* XXX this needs to be changed if #rx queues != #tx queues */
5702*22ce4affSfengbojiang 		rxq->ifr_ntxqirq = 1;
5703*22ce4affSfengbojiang 		rxq->ifr_txqid[0] = i;
5704a9643ea8Slogwang 		for (j = 0; j < nrxqs; j++, ifdip++) {
5705*22ce4affSfengbojiang 			if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) {
5706*22ce4affSfengbojiang 				device_printf(dev,
5707*22ce4affSfengbojiang 				    "Unable to allocate RX descriptors\n");
5708a9643ea8Slogwang 				err = ENOMEM;
5709a9643ea8Slogwang 				goto err_tx_desc;
5710a9643ea8Slogwang 			}
5711a9643ea8Slogwang 			bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
5712a9643ea8Slogwang 		}
5713a9643ea8Slogwang 		rxq->ifr_ctx = ctx;
5714a9643ea8Slogwang 		rxq->ifr_id = i;
5715*22ce4affSfengbojiang 		rxq->ifr_fl_offset = fl_offset;
5716a9643ea8Slogwang 		rxq->ifr_nfl = nfree_lists;
5717a9643ea8Slogwang 		if (!(fl =
5718a9643ea8Slogwang 			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
5719a9643ea8Slogwang 			device_printf(dev, "Unable to allocate free list memory\n");
5720a9643ea8Slogwang 			err = ENOMEM;
5721a9643ea8Slogwang 			goto err_tx_desc;
5722a9643ea8Slogwang 		}
5723a9643ea8Slogwang 		rxq->ifr_fl = fl;
5724a9643ea8Slogwang 		for (j = 0; j < nfree_lists; j++) {
5725*22ce4affSfengbojiang 			fl[j].ifl_rxq = rxq;
5726*22ce4affSfengbojiang 			fl[j].ifl_id = j;
5727*22ce4affSfengbojiang 			fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
5728*22ce4affSfengbojiang 			fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
5729a9643ea8Slogwang 		}
5730a9643ea8Slogwang 		/* Allocate receive buffers for the ring */
5731a9643ea8Slogwang 		if (iflib_rxsd_alloc(rxq)) {
5732a9643ea8Slogwang 			device_printf(dev,
5733a9643ea8Slogwang 			    "Critical Failure setting up receive buffers\n");
5734a9643ea8Slogwang 			err = ENOMEM;
5735a9643ea8Slogwang 			goto err_rx_desc;
5736a9643ea8Slogwang 		}
5737*22ce4affSfengbojiang 
5738*22ce4affSfengbojiang 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
5739*22ce4affSfengbojiang 			fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB,
5740*22ce4affSfengbojiang 			    M_WAITOK);
5741a9643ea8Slogwang 	}
5742a9643ea8Slogwang 
5743a9643ea8Slogwang 	/* TXQs */
5744a9643ea8Slogwang 	vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
5745a9643ea8Slogwang 	paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
5746a9643ea8Slogwang 	for (i = 0; i < ntxqsets; i++) {
5747a9643ea8Slogwang 		iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
5748a9643ea8Slogwang 
5749a9643ea8Slogwang 		for (j = 0; j < ntxqs; j++, di++) {
5750a9643ea8Slogwang 			vaddrs[i*ntxqs + j] = di->idi_vaddr;
5751a9643ea8Slogwang 			paddrs[i*ntxqs + j] = di->idi_paddr;
5752a9643ea8Slogwang 		}
5753a9643ea8Slogwang 	}
5754a9643ea8Slogwang 	if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
5755*22ce4affSfengbojiang 		device_printf(ctx->ifc_dev,
5756*22ce4affSfengbojiang 		    "Unable to allocate device TX queue\n");
5757a9643ea8Slogwang 		iflib_tx_structures_free(ctx);
5758a9643ea8Slogwang 		free(vaddrs, M_IFLIB);
5759a9643ea8Slogwang 		free(paddrs, M_IFLIB);
5760a9643ea8Slogwang 		goto err_rx_desc;
5761a9643ea8Slogwang 	}
5762a9643ea8Slogwang 	free(vaddrs, M_IFLIB);
5763a9643ea8Slogwang 	free(paddrs, M_IFLIB);
5764a9643ea8Slogwang 
5765a9643ea8Slogwang 	/* RXQs */
5766a9643ea8Slogwang 	vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
5767a9643ea8Slogwang 	paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
5768a9643ea8Slogwang 	for (i = 0; i < nrxqsets; i++) {
5769a9643ea8Slogwang 		iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
5770a9643ea8Slogwang 
5771a9643ea8Slogwang 		for (j = 0; j < nrxqs; j++, di++) {
5772a9643ea8Slogwang 			vaddrs[i*nrxqs + j] = di->idi_vaddr;
5773a9643ea8Slogwang 			paddrs[i*nrxqs + j] = di->idi_paddr;
5774a9643ea8Slogwang 		}
5775a9643ea8Slogwang 	}
5776a9643ea8Slogwang 	if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
5777*22ce4affSfengbojiang 		device_printf(ctx->ifc_dev,
5778*22ce4affSfengbojiang 		    "Unable to allocate device RX queue\n");
5779a9643ea8Slogwang 		iflib_tx_structures_free(ctx);
5780a9643ea8Slogwang 		free(vaddrs, M_IFLIB);
5781a9643ea8Slogwang 		free(paddrs, M_IFLIB);
5782a9643ea8Slogwang 		goto err_rx_desc;
5783a9643ea8Slogwang 	}
5784a9643ea8Slogwang 	free(vaddrs, M_IFLIB);
5785a9643ea8Slogwang 	free(paddrs, M_IFLIB);
5786a9643ea8Slogwang 
5787a9643ea8Slogwang 	return (0);
5788a9643ea8Slogwang 
5789a9643ea8Slogwang /* XXX handle allocation failure changes */
5790a9643ea8Slogwang err_rx_desc:
5791a9643ea8Slogwang err_tx_desc:
5792*22ce4affSfengbojiang rx_fail:
5793a9643ea8Slogwang 	if (ctx->ifc_rxqs != NULL)
5794a9643ea8Slogwang 		free(ctx->ifc_rxqs, M_IFLIB);
5795a9643ea8Slogwang 	ctx->ifc_rxqs = NULL;
5796a9643ea8Slogwang 	if (ctx->ifc_txqs != NULL)
5797a9643ea8Slogwang 		free(ctx->ifc_txqs, M_IFLIB);
5798a9643ea8Slogwang 	ctx->ifc_txqs = NULL;
5799a9643ea8Slogwang fail:
5800a9643ea8Slogwang 	return (err);
5801a9643ea8Slogwang }
5802a9643ea8Slogwang 
5803a9643ea8Slogwang static int
iflib_tx_structures_setup(if_ctx_t ctx)5804a9643ea8Slogwang iflib_tx_structures_setup(if_ctx_t ctx)
5805a9643ea8Slogwang {
5806a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
5807a9643ea8Slogwang 	int i;
5808a9643ea8Slogwang 
5809a9643ea8Slogwang 	for (i = 0; i < NTXQSETS(ctx); i++, txq++)
5810a9643ea8Slogwang 		iflib_txq_setup(txq);
5811a9643ea8Slogwang 
5812a9643ea8Slogwang 	return (0);
5813a9643ea8Slogwang }
5814a9643ea8Slogwang 
5815a9643ea8Slogwang static void
iflib_tx_structures_free(if_ctx_t ctx)5816a9643ea8Slogwang iflib_tx_structures_free(if_ctx_t ctx)
5817a9643ea8Slogwang {
5818a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
5819*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
5820a9643ea8Slogwang 	int i, j;
5821a9643ea8Slogwang 
5822a9643ea8Slogwang 	for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
5823*22ce4affSfengbojiang 		for (j = 0; j < sctx->isc_ntxqs; j++)
5824a9643ea8Slogwang 			iflib_dma_free(&txq->ift_ifdi[j]);
5825*22ce4affSfengbojiang 		iflib_txq_destroy(txq);
5826a9643ea8Slogwang 	}
5827a9643ea8Slogwang 	free(ctx->ifc_txqs, M_IFLIB);
5828a9643ea8Slogwang 	ctx->ifc_txqs = NULL;
5829a9643ea8Slogwang 	IFDI_QUEUES_FREE(ctx);
5830a9643ea8Slogwang }
5831a9643ea8Slogwang 
5832a9643ea8Slogwang /*********************************************************************
5833a9643ea8Slogwang  *
5834a9643ea8Slogwang  *  Initialize all receive rings.
5835a9643ea8Slogwang  *
5836a9643ea8Slogwang  **********************************************************************/
5837a9643ea8Slogwang static int
iflib_rx_structures_setup(if_ctx_t ctx)5838a9643ea8Slogwang iflib_rx_structures_setup(if_ctx_t ctx)
5839a9643ea8Slogwang {
5840a9643ea8Slogwang 	iflib_rxq_t rxq = ctx->ifc_rxqs;
5841a9643ea8Slogwang 	int q;
5842a9643ea8Slogwang #if defined(INET6) || defined(INET)
5843*22ce4affSfengbojiang 	int err, i;
5844a9643ea8Slogwang #endif
5845a9643ea8Slogwang 
5846a9643ea8Slogwang 	for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
5847a9643ea8Slogwang #if defined(INET6) || defined(INET)
5848*22ce4affSfengbojiang 		if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) {
5849*22ce4affSfengbojiang 			err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
5850a9643ea8Slogwang 			    TCP_LRO_ENTRIES, min(1024,
5851*22ce4affSfengbojiang 			    ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]));
5852*22ce4affSfengbojiang 			if (err != 0) {
5853*22ce4affSfengbojiang 				device_printf(ctx->ifc_dev,
5854*22ce4affSfengbojiang 				    "LRO Initialization failed!\n");
5855a9643ea8Slogwang 				goto fail;
5856a9643ea8Slogwang 			}
5857*22ce4affSfengbojiang 		}
5858a9643ea8Slogwang #endif
5859a9643ea8Slogwang 		IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
5860a9643ea8Slogwang 	}
5861a9643ea8Slogwang 	return (0);
5862a9643ea8Slogwang #if defined(INET6) || defined(INET)
5863a9643ea8Slogwang fail:
5864a9643ea8Slogwang 	/*
5865*22ce4affSfengbojiang 	 * Free LRO resources allocated so far, we will only handle
5866a9643ea8Slogwang 	 * the rings that completed, the failing case will have
5867a9643ea8Slogwang 	 * cleaned up for itself.  'q' failed, so its the terminus.
5868a9643ea8Slogwang 	 */
5869a9643ea8Slogwang 	rxq = ctx->ifc_rxqs;
5870a9643ea8Slogwang 	for (i = 0; i < q; ++i, rxq++) {
5871*22ce4affSfengbojiang 		if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO)
5872*22ce4affSfengbojiang 			tcp_lro_free(&rxq->ifr_lc);
5873a9643ea8Slogwang 	}
5874a9643ea8Slogwang 	return (err);
5875a9643ea8Slogwang #endif
5876a9643ea8Slogwang }
5877a9643ea8Slogwang 
5878a9643ea8Slogwang /*********************************************************************
5879a9643ea8Slogwang  *
5880a9643ea8Slogwang  *  Free all receive rings.
5881a9643ea8Slogwang  *
5882a9643ea8Slogwang  **********************************************************************/
5883a9643ea8Slogwang static void
iflib_rx_structures_free(if_ctx_t ctx)5884a9643ea8Slogwang iflib_rx_structures_free(if_ctx_t ctx)
5885a9643ea8Slogwang {
5886a9643ea8Slogwang 	iflib_rxq_t rxq = ctx->ifc_rxqs;
5887*22ce4affSfengbojiang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
5888*22ce4affSfengbojiang 	int i, j;
5889a9643ea8Slogwang 
5890*22ce4affSfengbojiang 	for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
5891*22ce4affSfengbojiang 		for (j = 0; j < sctx->isc_nrxqs; j++)
5892*22ce4affSfengbojiang 			iflib_dma_free(&rxq->ifr_ifdi[j]);
5893a9643ea8Slogwang 		iflib_rx_sds_free(rxq);
5894*22ce4affSfengbojiang #if defined(INET6) || defined(INET)
5895*22ce4affSfengbojiang 		if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO)
5896*22ce4affSfengbojiang 			tcp_lro_free(&rxq->ifr_lc);
5897*22ce4affSfengbojiang #endif
5898a9643ea8Slogwang 	}
5899*22ce4affSfengbojiang 	free(ctx->ifc_rxqs, M_IFLIB);
5900*22ce4affSfengbojiang 	ctx->ifc_rxqs = NULL;
5901a9643ea8Slogwang }
5902a9643ea8Slogwang 
5903a9643ea8Slogwang static int
iflib_qset_structures_setup(if_ctx_t ctx)5904a9643ea8Slogwang iflib_qset_structures_setup(if_ctx_t ctx)
5905a9643ea8Slogwang {
5906a9643ea8Slogwang 	int err;
5907a9643ea8Slogwang 
5908*22ce4affSfengbojiang 	/*
5909*22ce4affSfengbojiang 	 * It is expected that the caller takes care of freeing queues if this
5910*22ce4affSfengbojiang 	 * fails.
5911*22ce4affSfengbojiang 	 */
5912*22ce4affSfengbojiang 	if ((err = iflib_tx_structures_setup(ctx)) != 0) {
5913*22ce4affSfengbojiang 		device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err);
5914a9643ea8Slogwang 		return (err);
5915a9643ea8Slogwang 	}
5916*22ce4affSfengbojiang 
5917*22ce4affSfengbojiang 	if ((err = iflib_rx_structures_setup(ctx)) != 0)
5918*22ce4affSfengbojiang 		device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
5919*22ce4affSfengbojiang 
5920a9643ea8Slogwang 	return (err);
5921a9643ea8Slogwang }
5922a9643ea8Slogwang 
5923a9643ea8Slogwang int
iflib_irq_alloc(if_ctx_t ctx,if_irq_t irq,int rid,driver_filter_t filter,void * filter_arg,driver_intr_t handler,void * arg,const char * name)5924a9643ea8Slogwang iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
5925*22ce4affSfengbojiang 		driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name)
5926a9643ea8Slogwang {
5927a9643ea8Slogwang 
5928a9643ea8Slogwang 	return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
5929a9643ea8Slogwang }
5930a9643ea8Slogwang 
5931*22ce4affSfengbojiang #ifdef SMP
5932*22ce4affSfengbojiang static int
find_nth(if_ctx_t ctx,int qid)5933*22ce4affSfengbojiang find_nth(if_ctx_t ctx, int qid)
5934a9643ea8Slogwang {
5935*22ce4affSfengbojiang 	cpuset_t cpus;
5936*22ce4affSfengbojiang 	int i, cpuid, eqid, count;
5937a9643ea8Slogwang 
5938*22ce4affSfengbojiang 	CPU_COPY(&ctx->ifc_cpus, &cpus);
5939*22ce4affSfengbojiang 	count = CPU_COUNT(&cpus);
5940*22ce4affSfengbojiang 	eqid = qid % count;
5941a9643ea8Slogwang 	/* clear up to the qid'th bit */
5942*22ce4affSfengbojiang 	for (i = 0; i < eqid; i++) {
5943*22ce4affSfengbojiang 		cpuid = CPU_FFS(&cpus);
5944*22ce4affSfengbojiang 		MPASS(cpuid != 0);
5945*22ce4affSfengbojiang 		CPU_CLR(cpuid-1, &cpus);
5946a9643ea8Slogwang 	}
5947*22ce4affSfengbojiang 	cpuid = CPU_FFS(&cpus);
5948*22ce4affSfengbojiang 	MPASS(cpuid != 0);
5949*22ce4affSfengbojiang 	return (cpuid-1);
5950*22ce4affSfengbojiang }
5951*22ce4affSfengbojiang 
5952*22ce4affSfengbojiang #ifdef SCHED_ULE
5953*22ce4affSfengbojiang extern struct cpu_group *cpu_top;              /* CPU topology */
5954*22ce4affSfengbojiang 
5955*22ce4affSfengbojiang static int
find_child_with_core(int cpu,struct cpu_group * grp)5956*22ce4affSfengbojiang find_child_with_core(int cpu, struct cpu_group *grp)
5957*22ce4affSfengbojiang {
5958*22ce4affSfengbojiang 	int i;
5959*22ce4affSfengbojiang 
5960*22ce4affSfengbojiang 	if (grp->cg_children == 0)
5961*22ce4affSfengbojiang 		return -1;
5962*22ce4affSfengbojiang 
5963*22ce4affSfengbojiang 	MPASS(grp->cg_child);
5964*22ce4affSfengbojiang 	for (i = 0; i < grp->cg_children; i++) {
5965*22ce4affSfengbojiang 		if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask))
5966*22ce4affSfengbojiang 			return i;
5967*22ce4affSfengbojiang 	}
5968*22ce4affSfengbojiang 
5969*22ce4affSfengbojiang 	return -1;
5970*22ce4affSfengbojiang }
5971*22ce4affSfengbojiang 
5972*22ce4affSfengbojiang /*
5973*22ce4affSfengbojiang  * Find the nth "close" core to the specified core
5974*22ce4affSfengbojiang  * "close" is defined as the deepest level that shares
5975*22ce4affSfengbojiang  * at least an L2 cache.  With threads, this will be
5976*22ce4affSfengbojiang  * threads on the same core.  If the shared cache is L3
5977*22ce4affSfengbojiang  * or higher, simply returns the same core.
5978*22ce4affSfengbojiang  */
5979*22ce4affSfengbojiang static int
find_close_core(int cpu,int core_offset)5980*22ce4affSfengbojiang find_close_core(int cpu, int core_offset)
5981*22ce4affSfengbojiang {
5982*22ce4affSfengbojiang 	struct cpu_group *grp;
5983*22ce4affSfengbojiang 	int i;
5984*22ce4affSfengbojiang 	int fcpu;
5985*22ce4affSfengbojiang 	cpuset_t cs;
5986*22ce4affSfengbojiang 
5987*22ce4affSfengbojiang 	grp = cpu_top;
5988*22ce4affSfengbojiang 	if (grp == NULL)
5989*22ce4affSfengbojiang 		return cpu;
5990*22ce4affSfengbojiang 	i = 0;
5991*22ce4affSfengbojiang 	while ((i = find_child_with_core(cpu, grp)) != -1) {
5992*22ce4affSfengbojiang 		/* If the child only has one cpu, don't descend */
5993*22ce4affSfengbojiang 		if (grp->cg_child[i].cg_count <= 1)
5994*22ce4affSfengbojiang 			break;
5995*22ce4affSfengbojiang 		grp = &grp->cg_child[i];
5996*22ce4affSfengbojiang 	}
5997*22ce4affSfengbojiang 
5998*22ce4affSfengbojiang 	/* If they don't share at least an L2 cache, use the same CPU */
5999*22ce4affSfengbojiang 	if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE)
6000*22ce4affSfengbojiang 		return cpu;
6001*22ce4affSfengbojiang 
6002*22ce4affSfengbojiang 	/* Now pick one */
6003*22ce4affSfengbojiang 	CPU_COPY(&grp->cg_mask, &cs);
6004*22ce4affSfengbojiang 
6005*22ce4affSfengbojiang 	/* Add the selected CPU offset to core offset. */
6006*22ce4affSfengbojiang 	for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) {
6007*22ce4affSfengbojiang 		if (fcpu - 1 == cpu)
6008*22ce4affSfengbojiang 			break;
6009*22ce4affSfengbojiang 		CPU_CLR(fcpu - 1, &cs);
6010*22ce4affSfengbojiang 	}
6011*22ce4affSfengbojiang 	MPASS(fcpu);
6012*22ce4affSfengbojiang 
6013*22ce4affSfengbojiang 	core_offset += i;
6014*22ce4affSfengbojiang 
6015*22ce4affSfengbojiang 	CPU_COPY(&grp->cg_mask, &cs);
6016*22ce4affSfengbojiang 	for (i = core_offset % grp->cg_count; i > 0; i--) {
6017*22ce4affSfengbojiang 		MPASS(CPU_FFS(&cs));
6018*22ce4affSfengbojiang 		CPU_CLR(CPU_FFS(&cs) - 1, &cs);
6019*22ce4affSfengbojiang 	}
6020*22ce4affSfengbojiang 	MPASS(CPU_FFS(&cs));
6021*22ce4affSfengbojiang 	return CPU_FFS(&cs) - 1;
6022*22ce4affSfengbojiang }
6023*22ce4affSfengbojiang #else
6024*22ce4affSfengbojiang static int
find_close_core(int cpu,int core_offset __unused)6025*22ce4affSfengbojiang find_close_core(int cpu, int core_offset __unused)
6026*22ce4affSfengbojiang {
6027*22ce4affSfengbojiang 	return cpu;
6028*22ce4affSfengbojiang }
6029*22ce4affSfengbojiang #endif
6030*22ce4affSfengbojiang 
6031*22ce4affSfengbojiang static int
get_core_offset(if_ctx_t ctx,iflib_intr_type_t type,int qid)6032*22ce4affSfengbojiang get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid)
6033*22ce4affSfengbojiang {
6034*22ce4affSfengbojiang 	switch (type) {
6035*22ce4affSfengbojiang 	case IFLIB_INTR_TX:
6036*22ce4affSfengbojiang 		/* TX queues get cores which share at least an L2 cache with the corresponding RX queue */
6037*22ce4affSfengbojiang 		/* XXX handle multiple RX threads per core and more than two core per L2 group */
6038*22ce4affSfengbojiang 		return qid / CPU_COUNT(&ctx->ifc_cpus) + 1;
6039*22ce4affSfengbojiang 	case IFLIB_INTR_RX:
6040*22ce4affSfengbojiang 	case IFLIB_INTR_RXTX:
6041*22ce4affSfengbojiang 		/* RX queues get the specified core */
6042*22ce4affSfengbojiang 		return qid / CPU_COUNT(&ctx->ifc_cpus);
6043*22ce4affSfengbojiang 	default:
6044*22ce4affSfengbojiang 		return -1;
6045*22ce4affSfengbojiang 	}
6046*22ce4affSfengbojiang }
6047*22ce4affSfengbojiang #else
6048*22ce4affSfengbojiang #define get_core_offset(ctx, type, qid)	CPU_FIRST()
6049*22ce4affSfengbojiang #define find_close_core(cpuid, tid)	CPU_FIRST()
6050*22ce4affSfengbojiang #define find_nth(ctx, gid)		CPU_FIRST()
6051*22ce4affSfengbojiang #endif
6052*22ce4affSfengbojiang 
6053*22ce4affSfengbojiang /* Just to avoid copy/paste */
6054*22ce4affSfengbojiang static inline int
iflib_irq_set_affinity(if_ctx_t ctx,if_irq_t irq,iflib_intr_type_t type,int qid,struct grouptask * gtask,struct taskqgroup * tqg,void * uniq,const char * name)6055*22ce4affSfengbojiang iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,
6056*22ce4affSfengbojiang     int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq,
6057*22ce4affSfengbojiang     const char *name)
6058*22ce4affSfengbojiang {
6059*22ce4affSfengbojiang 	device_t dev;
6060*22ce4affSfengbojiang 	int co, cpuid, err, tid;
6061*22ce4affSfengbojiang 
6062*22ce4affSfengbojiang 	dev = ctx->ifc_dev;
6063*22ce4affSfengbojiang 	co = ctx->ifc_sysctl_core_offset;
6064*22ce4affSfengbojiang 	if (ctx->ifc_sysctl_separate_txrx && type == IFLIB_INTR_TX)
6065*22ce4affSfengbojiang 		co += ctx->ifc_softc_ctx.isc_nrxqsets;
6066*22ce4affSfengbojiang 	cpuid = find_nth(ctx, qid + co);
6067*22ce4affSfengbojiang 	tid = get_core_offset(ctx, type, qid);
6068*22ce4affSfengbojiang 	if (tid < 0) {
6069*22ce4affSfengbojiang 		device_printf(dev, "get_core_offset failed\n");
6070*22ce4affSfengbojiang 		return (EOPNOTSUPP);
6071*22ce4affSfengbojiang 	}
6072*22ce4affSfengbojiang 	cpuid = find_close_core(cpuid, tid);
6073*22ce4affSfengbojiang 	err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, irq->ii_res,
6074*22ce4affSfengbojiang 	    name);
6075*22ce4affSfengbojiang 	if (err) {
6076*22ce4affSfengbojiang 		device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err);
6077*22ce4affSfengbojiang 		return (err);
6078*22ce4affSfengbojiang 	}
6079*22ce4affSfengbojiang #ifdef notyet
6080*22ce4affSfengbojiang 	if (cpuid > ctx->ifc_cpuid_highest)
6081*22ce4affSfengbojiang 		ctx->ifc_cpuid_highest = cpuid;
6082*22ce4affSfengbojiang #endif
6083*22ce4affSfengbojiang 	return (0);
6084a9643ea8Slogwang }
6085a9643ea8Slogwang 
6086a9643ea8Slogwang int
iflib_irq_alloc_generic(if_ctx_t ctx,if_irq_t irq,int rid,iflib_intr_type_t type,driver_filter_t * filter,void * filter_arg,int qid,const char * name)6087a9643ea8Slogwang iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
6088a9643ea8Slogwang 			iflib_intr_type_t type, driver_filter_t *filter,
6089*22ce4affSfengbojiang 			void *filter_arg, int qid, const char *name)
6090a9643ea8Slogwang {
6091*22ce4affSfengbojiang 	device_t dev;
6092a9643ea8Slogwang 	struct grouptask *gtask;
6093a9643ea8Slogwang 	struct taskqgroup *tqg;
6094a9643ea8Slogwang 	iflib_filter_info_t info;
6095a9643ea8Slogwang 	gtask_fn_t *fn;
6096a9643ea8Slogwang 	int tqrid, err;
6097*22ce4affSfengbojiang 	driver_filter_t *intr_fast;
6098a9643ea8Slogwang 	void *q;
6099a9643ea8Slogwang 
6100a9643ea8Slogwang 	info = &ctx->ifc_filter_info;
6101*22ce4affSfengbojiang 	tqrid = rid;
6102a9643ea8Slogwang 
6103a9643ea8Slogwang 	switch (type) {
6104a9643ea8Slogwang 	/* XXX merge tx/rx for netmap? */
6105a9643ea8Slogwang 	case IFLIB_INTR_TX:
6106a9643ea8Slogwang 		q = &ctx->ifc_txqs[qid];
6107a9643ea8Slogwang 		info = &ctx->ifc_txqs[qid].ift_filter_info;
6108a9643ea8Slogwang 		gtask = &ctx->ifc_txqs[qid].ift_task;
6109a9643ea8Slogwang 		tqg = qgroup_if_io_tqg;
6110a9643ea8Slogwang 		fn = _task_fn_tx;
6111*22ce4affSfengbojiang 		intr_fast = iflib_fast_intr;
6112*22ce4affSfengbojiang 		GROUPTASK_INIT(gtask, 0, fn, q);
6113*22ce4affSfengbojiang 		ctx->ifc_flags |= IFC_NETMAP_TX_IRQ;
6114a9643ea8Slogwang 		break;
6115a9643ea8Slogwang 	case IFLIB_INTR_RX:
6116a9643ea8Slogwang 		q = &ctx->ifc_rxqs[qid];
6117a9643ea8Slogwang 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
6118a9643ea8Slogwang 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
6119a9643ea8Slogwang 		tqg = qgroup_if_io_tqg;
6120a9643ea8Slogwang 		fn = _task_fn_rx;
6121*22ce4affSfengbojiang 		intr_fast = iflib_fast_intr;
6122*22ce4affSfengbojiang 		NET_GROUPTASK_INIT(gtask, 0, fn, q);
6123*22ce4affSfengbojiang 		break;
6124*22ce4affSfengbojiang 	case IFLIB_INTR_RXTX:
6125*22ce4affSfengbojiang 		q = &ctx->ifc_rxqs[qid];
6126*22ce4affSfengbojiang 		info = &ctx->ifc_rxqs[qid].ifr_filter_info;
6127*22ce4affSfengbojiang 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
6128*22ce4affSfengbojiang 		tqg = qgroup_if_io_tqg;
6129*22ce4affSfengbojiang 		fn = _task_fn_rx;
6130*22ce4affSfengbojiang 		intr_fast = iflib_fast_intr_rxtx;
6131*22ce4affSfengbojiang 		NET_GROUPTASK_INIT(gtask, 0, fn, q);
6132a9643ea8Slogwang 		break;
6133a9643ea8Slogwang 	case IFLIB_INTR_ADMIN:
6134a9643ea8Slogwang 		q = ctx;
6135*22ce4affSfengbojiang 		tqrid = -1;
6136a9643ea8Slogwang 		info = &ctx->ifc_filter_info;
6137a9643ea8Slogwang 		gtask = &ctx->ifc_admin_task;
6138a9643ea8Slogwang 		tqg = qgroup_if_config_tqg;
6139a9643ea8Slogwang 		fn = _task_fn_admin;
6140*22ce4affSfengbojiang 		intr_fast = iflib_fast_intr_ctx;
6141a9643ea8Slogwang 		break;
6142a9643ea8Slogwang 	default:
6143*22ce4affSfengbojiang 		device_printf(ctx->ifc_dev, "%s: unknown net intr type\n",
6144*22ce4affSfengbojiang 		    __func__);
6145*22ce4affSfengbojiang 		return (EINVAL);
6146a9643ea8Slogwang 	}
6147a9643ea8Slogwang 
6148a9643ea8Slogwang 	info->ifi_filter = filter;
6149a9643ea8Slogwang 	info->ifi_filter_arg = filter_arg;
6150a9643ea8Slogwang 	info->ifi_task = gtask;
6151*22ce4affSfengbojiang 	info->ifi_ctx = q;
6152a9643ea8Slogwang 
6153*22ce4affSfengbojiang 	dev = ctx->ifc_dev;
6154*22ce4affSfengbojiang 	err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info,  name);
6155*22ce4affSfengbojiang 	if (err != 0) {
6156*22ce4affSfengbojiang 		device_printf(dev, "_iflib_irq_alloc failed %d\n", err);
6157a9643ea8Slogwang 		return (err);
6158*22ce4affSfengbojiang 	}
6159*22ce4affSfengbojiang 	if (type == IFLIB_INTR_ADMIN)
6160*22ce4affSfengbojiang 		return (0);
6161a9643ea8Slogwang 
6162*22ce4affSfengbojiang 	if (tqrid != -1) {
6163*22ce4affSfengbojiang 		err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg,
6164*22ce4affSfengbojiang 		    q, name);
6165*22ce4affSfengbojiang 		if (err)
6166*22ce4affSfengbojiang 			return (err);
6167*22ce4affSfengbojiang 	} else {
6168*22ce4affSfengbojiang 		taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name);
6169*22ce4affSfengbojiang 	}
6170a9643ea8Slogwang 
6171a9643ea8Slogwang 	return (0);
6172a9643ea8Slogwang }
6173a9643ea8Slogwang 
6174a9643ea8Slogwang void
iflib_softirq_alloc_generic(if_ctx_t ctx,if_irq_t irq,iflib_intr_type_t type,void * arg,int qid,const char * name)6175*22ce4affSfengbojiang iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, void *arg, int qid, const char *name)
6176a9643ea8Slogwang {
6177a9643ea8Slogwang 	struct grouptask *gtask;
6178a9643ea8Slogwang 	struct taskqgroup *tqg;
6179a9643ea8Slogwang 	gtask_fn_t *fn;
6180a9643ea8Slogwang 	void *q;
6181*22ce4affSfengbojiang 	int err;
6182a9643ea8Slogwang 
6183a9643ea8Slogwang 	switch (type) {
6184a9643ea8Slogwang 	case IFLIB_INTR_TX:
6185a9643ea8Slogwang 		q = &ctx->ifc_txqs[qid];
6186a9643ea8Slogwang 		gtask = &ctx->ifc_txqs[qid].ift_task;
6187a9643ea8Slogwang 		tqg = qgroup_if_io_tqg;
6188a9643ea8Slogwang 		fn = _task_fn_tx;
6189*22ce4affSfengbojiang 		GROUPTASK_INIT(gtask, 0, fn, q);
6190a9643ea8Slogwang 		break;
6191a9643ea8Slogwang 	case IFLIB_INTR_RX:
6192a9643ea8Slogwang 		q = &ctx->ifc_rxqs[qid];
6193a9643ea8Slogwang 		gtask = &ctx->ifc_rxqs[qid].ifr_task;
6194a9643ea8Slogwang 		tqg = qgroup_if_io_tqg;
6195a9643ea8Slogwang 		fn = _task_fn_rx;
6196*22ce4affSfengbojiang 		NET_GROUPTASK_INIT(gtask, 0, fn, q);
6197a9643ea8Slogwang 		break;
6198a9643ea8Slogwang 	case IFLIB_INTR_IOV:
6199a9643ea8Slogwang 		q = ctx;
6200a9643ea8Slogwang 		gtask = &ctx->ifc_vflr_task;
6201a9643ea8Slogwang 		tqg = qgroup_if_config_tqg;
6202a9643ea8Slogwang 		fn = _task_fn_iov;
6203*22ce4affSfengbojiang 		GROUPTASK_INIT(gtask, 0, fn, q);
6204a9643ea8Slogwang 		break;
6205a9643ea8Slogwang 	default:
6206a9643ea8Slogwang 		panic("unknown net intr type");
6207a9643ea8Slogwang 	}
6208*22ce4affSfengbojiang 	if (irq != NULL) {
6209*22ce4affSfengbojiang 		err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg,
6210*22ce4affSfengbojiang 		    q, name);
6211*22ce4affSfengbojiang 		if (err)
6212*22ce4affSfengbojiang 			taskqgroup_attach(tqg, gtask, q, ctx->ifc_dev,
6213*22ce4affSfengbojiang 			    irq->ii_res, name);
6214*22ce4affSfengbojiang 	} else {
6215*22ce4affSfengbojiang 		taskqgroup_attach(tqg, gtask, q, NULL, NULL, name);
6216*22ce4affSfengbojiang 	}
6217a9643ea8Slogwang }
6218a9643ea8Slogwang 
6219a9643ea8Slogwang void
iflib_irq_free(if_ctx_t ctx,if_irq_t irq)6220a9643ea8Slogwang iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
6221a9643ea8Slogwang {
6222*22ce4affSfengbojiang 
6223a9643ea8Slogwang 	if (irq->ii_tag)
6224a9643ea8Slogwang 		bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
6225a9643ea8Slogwang 
6226a9643ea8Slogwang 	if (irq->ii_res)
6227*22ce4affSfengbojiang 		bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ,
6228*22ce4affSfengbojiang 		    rman_get_rid(irq->ii_res), irq->ii_res);
6229a9643ea8Slogwang }
6230a9643ea8Slogwang 
6231a9643ea8Slogwang static int
iflib_legacy_setup(if_ctx_t ctx,driver_filter_t filter,void * filter_arg,int * rid,const char * name)6232*22ce4affSfengbojiang iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name)
6233a9643ea8Slogwang {
6234a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
6235a9643ea8Slogwang 	iflib_rxq_t rxq = ctx->ifc_rxqs;
6236a9643ea8Slogwang 	if_irq_t irq = &ctx->ifc_legacy_irq;
6237a9643ea8Slogwang 	iflib_filter_info_t info;
6238*22ce4affSfengbojiang 	device_t dev;
6239a9643ea8Slogwang 	struct grouptask *gtask;
6240*22ce4affSfengbojiang 	struct resource *res;
6241a9643ea8Slogwang 	struct taskqgroup *tqg;
6242a9643ea8Slogwang 	void *q;
6243*22ce4affSfengbojiang 	int err, tqrid;
6244*22ce4affSfengbojiang 	bool rx_only;
6245a9643ea8Slogwang 
6246a9643ea8Slogwang 	q = &ctx->ifc_rxqs[0];
6247a9643ea8Slogwang 	info = &rxq[0].ifr_filter_info;
6248a9643ea8Slogwang 	gtask = &rxq[0].ifr_task;
6249a9643ea8Slogwang 	tqg = qgroup_if_io_tqg;
6250*22ce4affSfengbojiang 	tqrid = *rid;
6251*22ce4affSfengbojiang 	rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0;
6252a9643ea8Slogwang 
6253a9643ea8Slogwang 	ctx->ifc_flags |= IFC_LEGACY;
6254a9643ea8Slogwang 	info->ifi_filter = filter;
6255a9643ea8Slogwang 	info->ifi_filter_arg = filter_arg;
6256a9643ea8Slogwang 	info->ifi_task = gtask;
6257*22ce4affSfengbojiang 	info->ifi_ctx = rx_only ? ctx : q;
6258a9643ea8Slogwang 
6259*22ce4affSfengbojiang 	dev = ctx->ifc_dev;
6260a9643ea8Slogwang 	/* We allocate a single interrupt resource */
6261*22ce4affSfengbojiang 	err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr_ctx :
6262*22ce4affSfengbojiang 	    iflib_fast_intr_rxtx, NULL, info, name);
6263*22ce4affSfengbojiang 	if (err != 0)
6264a9643ea8Slogwang 		return (err);
6265*22ce4affSfengbojiang 	NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, q);
6266*22ce4affSfengbojiang 	res = irq->ii_res;
6267*22ce4affSfengbojiang 	taskqgroup_attach(tqg, gtask, q, dev, res, name);
6268a9643ea8Slogwang 
6269a9643ea8Slogwang 	GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
6270*22ce4affSfengbojiang 	taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res,
6271*22ce4affSfengbojiang 	    "tx");
6272a9643ea8Slogwang 	return (0);
6273a9643ea8Slogwang }
6274a9643ea8Slogwang 
6275a9643ea8Slogwang void
iflib_led_create(if_ctx_t ctx)6276a9643ea8Slogwang iflib_led_create(if_ctx_t ctx)
6277a9643ea8Slogwang {
6278a9643ea8Slogwang 
6279a9643ea8Slogwang 	ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
6280a9643ea8Slogwang 	    device_get_nameunit(ctx->ifc_dev));
6281a9643ea8Slogwang }
6282a9643ea8Slogwang 
6283a9643ea8Slogwang void
iflib_tx_intr_deferred(if_ctx_t ctx,int txqid)6284a9643ea8Slogwang iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
6285a9643ea8Slogwang {
6286a9643ea8Slogwang 
6287a9643ea8Slogwang 	GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
6288a9643ea8Slogwang }
6289a9643ea8Slogwang 
6290a9643ea8Slogwang void
iflib_rx_intr_deferred(if_ctx_t ctx,int rxqid)6291a9643ea8Slogwang iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
6292a9643ea8Slogwang {
6293a9643ea8Slogwang 
6294a9643ea8Slogwang 	GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
6295a9643ea8Slogwang }
6296a9643ea8Slogwang 
6297a9643ea8Slogwang void
iflib_admin_intr_deferred(if_ctx_t ctx)6298a9643ea8Slogwang iflib_admin_intr_deferred(if_ctx_t ctx)
6299a9643ea8Slogwang {
6300a9643ea8Slogwang 
6301*22ce4affSfengbojiang 	MPASS(ctx->ifc_admin_task.gt_taskqueue != NULL);
6302a9643ea8Slogwang 	GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
6303a9643ea8Slogwang }
6304a9643ea8Slogwang 
6305a9643ea8Slogwang void
iflib_iov_intr_deferred(if_ctx_t ctx)6306a9643ea8Slogwang iflib_iov_intr_deferred(if_ctx_t ctx)
6307a9643ea8Slogwang {
6308a9643ea8Slogwang 
6309a9643ea8Slogwang 	GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
6310a9643ea8Slogwang }
6311a9643ea8Slogwang 
6312a9643ea8Slogwang void
iflib_io_tqg_attach(struct grouptask * gt,void * uniq,int cpu,const char * name)6313*22ce4affSfengbojiang iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name)
6314a9643ea8Slogwang {
6315a9643ea8Slogwang 
6316*22ce4affSfengbojiang 	taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL,
6317*22ce4affSfengbojiang 	    name);
6318a9643ea8Slogwang }
6319a9643ea8Slogwang 
6320a9643ea8Slogwang void
iflib_config_gtask_init(void * ctx,struct grouptask * gtask,gtask_fn_t * fn,const char * name)6321*22ce4affSfengbojiang iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn,
6322*22ce4affSfengbojiang 	const char *name)
6323a9643ea8Slogwang {
6324a9643ea8Slogwang 
6325a9643ea8Slogwang 	GROUPTASK_INIT(gtask, 0, fn, ctx);
6326*22ce4affSfengbojiang 	taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL,
6327*22ce4affSfengbojiang 	    name);
6328a9643ea8Slogwang }
6329a9643ea8Slogwang 
6330a9643ea8Slogwang void
iflib_config_gtask_deinit(struct grouptask * gtask)6331a9643ea8Slogwang iflib_config_gtask_deinit(struct grouptask *gtask)
6332a9643ea8Slogwang {
6333a9643ea8Slogwang 
6334a9643ea8Slogwang 	taskqgroup_detach(qgroup_if_config_tqg, gtask);
6335a9643ea8Slogwang }
6336a9643ea8Slogwang 
6337a9643ea8Slogwang void
iflib_link_state_change(if_ctx_t ctx,int link_state,uint64_t baudrate)6338a9643ea8Slogwang iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
6339a9643ea8Slogwang {
6340a9643ea8Slogwang 	if_t ifp = ctx->ifc_ifp;
6341a9643ea8Slogwang 	iflib_txq_t txq = ctx->ifc_txqs;
6342a9643ea8Slogwang 
6343a9643ea8Slogwang 	if_setbaudrate(ifp, baudrate);
6344*22ce4affSfengbojiang 	if (baudrate >= IF_Gbps(10)) {
6345*22ce4affSfengbojiang 		STATE_LOCK(ctx);
6346*22ce4affSfengbojiang 		ctx->ifc_flags |= IFC_PREFETCH;
6347*22ce4affSfengbojiang 		STATE_UNLOCK(ctx);
6348*22ce4affSfengbojiang 	}
6349a9643ea8Slogwang 	/* If link down, disable watchdog */
6350a9643ea8Slogwang 	if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
6351a9643ea8Slogwang 		for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
6352a9643ea8Slogwang 			txq->ift_qstatus = IFLIB_QUEUE_IDLE;
6353a9643ea8Slogwang 	}
6354a9643ea8Slogwang 	ctx->ifc_link_state = link_state;
6355a9643ea8Slogwang 	if_link_state_change(ifp, link_state);
6356a9643ea8Slogwang }
6357a9643ea8Slogwang 
6358a9643ea8Slogwang static int
iflib_tx_credits_update(if_ctx_t ctx,iflib_txq_t txq)6359a9643ea8Slogwang iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
6360a9643ea8Slogwang {
6361a9643ea8Slogwang 	int credits;
6362*22ce4affSfengbojiang #ifdef INVARIANTS
6363*22ce4affSfengbojiang 	int credits_pre = txq->ift_cidx_processed;
6364*22ce4affSfengbojiang #endif
6365a9643ea8Slogwang 
6366*22ce4affSfengbojiang 	bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
6367*22ce4affSfengbojiang 	    BUS_DMASYNC_POSTREAD);
6368*22ce4affSfengbojiang 	if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
6369a9643ea8Slogwang 		return (0);
6370a9643ea8Slogwang 
6371a9643ea8Slogwang 	txq->ift_processed += credits;
6372a9643ea8Slogwang 	txq->ift_cidx_processed += credits;
6373a9643ea8Slogwang 
6374*22ce4affSfengbojiang 	MPASS(credits_pre + credits == txq->ift_cidx_processed);
6375a9643ea8Slogwang 	if (txq->ift_cidx_processed >= txq->ift_size)
6376a9643ea8Slogwang 		txq->ift_cidx_processed -= txq->ift_size;
6377a9643ea8Slogwang 	return (credits);
6378a9643ea8Slogwang }
6379a9643ea8Slogwang 
6380a9643ea8Slogwang static int
iflib_rxd_avail(if_ctx_t ctx,iflib_rxq_t rxq,qidx_t cidx,qidx_t budget)6381*22ce4affSfengbojiang iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
6382a9643ea8Slogwang {
6383*22ce4affSfengbojiang 	iflib_fl_t fl;
6384*22ce4affSfengbojiang 	u_int i;
6385a9643ea8Slogwang 
6386*22ce4affSfengbojiang 	for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++)
6387*22ce4affSfengbojiang 		bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
6388*22ce4affSfengbojiang 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6389a9643ea8Slogwang 	return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
6390a9643ea8Slogwang 	    budget));
6391a9643ea8Slogwang }
6392a9643ea8Slogwang 
6393a9643ea8Slogwang void
iflib_add_int_delay_sysctl(if_ctx_t ctx,const char * name,const char * description,if_int_delay_info_t info,int offset,int value)6394a9643ea8Slogwang iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
6395a9643ea8Slogwang 	const char *description, if_int_delay_info_t info,
6396a9643ea8Slogwang 	int offset, int value)
6397a9643ea8Slogwang {
6398a9643ea8Slogwang 	info->iidi_ctx = ctx;
6399a9643ea8Slogwang 	info->iidi_offset = offset;
6400a9643ea8Slogwang 	info->iidi_value = value;
6401a9643ea8Slogwang 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
6402a9643ea8Slogwang 	    SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
6403*22ce4affSfengbojiang 	    OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
6404a9643ea8Slogwang 	    info, 0, iflib_sysctl_int_delay, "I", description);
6405a9643ea8Slogwang }
6406a9643ea8Slogwang 
6407*22ce4affSfengbojiang struct sx *
iflib_ctx_lock_get(if_ctx_t ctx)6408a9643ea8Slogwang iflib_ctx_lock_get(if_ctx_t ctx)
6409a9643ea8Slogwang {
6410a9643ea8Slogwang 
6411*22ce4affSfengbojiang 	return (&ctx->ifc_ctx_sx);
6412a9643ea8Slogwang }
6413a9643ea8Slogwang 
6414a9643ea8Slogwang static int
iflib_msix_init(if_ctx_t ctx)6415a9643ea8Slogwang iflib_msix_init(if_ctx_t ctx)
6416a9643ea8Slogwang {
6417a9643ea8Slogwang 	device_t dev = ctx->ifc_dev;
6418a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
6419a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
6420*22ce4affSfengbojiang 	int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues;
6421*22ce4affSfengbojiang 	int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors;
6422a9643ea8Slogwang 
6423*22ce4affSfengbojiang 	iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs;
6424*22ce4affSfengbojiang 	iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs;
6425a9643ea8Slogwang 
6426*22ce4affSfengbojiang 	if (bootverbose)
6427*22ce4affSfengbojiang 		device_printf(dev, "msix_init qsets capped at %d\n",
6428*22ce4affSfengbojiang 		    imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets));
6429*22ce4affSfengbojiang 
6430a9643ea8Slogwang 	/* Override by tuneable */
6431*22ce4affSfengbojiang 	if (scctx->isc_disable_msix)
6432a9643ea8Slogwang 		goto msi;
6433a9643ea8Slogwang 
6434*22ce4affSfengbojiang 	/* First try MSI-X */
6435*22ce4affSfengbojiang 	if ((msgs = pci_msix_count(dev)) == 0) {
6436*22ce4affSfengbojiang 		if (bootverbose)
6437*22ce4affSfengbojiang 			device_printf(dev, "MSI-X not supported or disabled\n");
6438*22ce4affSfengbojiang 		goto msi;
6439a9643ea8Slogwang 	}
6440a9643ea8Slogwang 
6441*22ce4affSfengbojiang 	bar = ctx->ifc_softc_ctx.isc_msix_bar;
6442a9643ea8Slogwang 	/*
6443a9643ea8Slogwang 	 * bar == -1 => "trust me I know what I'm doing"
6444a9643ea8Slogwang 	 * Some drivers are for hardware that is so shoddily
6445a9643ea8Slogwang 	 * documented that no one knows which bars are which
6446a9643ea8Slogwang 	 * so the developer has to map all bars. This hack
6447*22ce4affSfengbojiang 	 * allows shoddy garbage to use MSI-X in this framework.
6448a9643ea8Slogwang 	 */
6449a9643ea8Slogwang 	if (bar != -1) {
6450a9643ea8Slogwang 		ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
6451a9643ea8Slogwang 	            SYS_RES_MEMORY, &bar, RF_ACTIVE);
6452a9643ea8Slogwang 		if (ctx->ifc_msix_mem == NULL) {
6453*22ce4affSfengbojiang 			device_printf(dev, "Unable to map MSI-X table\n");
6454a9643ea8Slogwang 			goto msi;
6455a9643ea8Slogwang 		}
6456a9643ea8Slogwang 	}
6457*22ce4affSfengbojiang 
6458*22ce4affSfengbojiang 	admincnt = sctx->isc_admin_intrcnt;
6459a9643ea8Slogwang #if IFLIB_DEBUG
6460a9643ea8Slogwang 	/* use only 1 qset in debug mode */
6461a9643ea8Slogwang 	queuemsgs = min(msgs - admincnt, 1);
6462a9643ea8Slogwang #else
6463a9643ea8Slogwang 	queuemsgs = msgs - admincnt;
6464a9643ea8Slogwang #endif
6465a9643ea8Slogwang #ifdef RSS
6466a9643ea8Slogwang 	queues = imin(queuemsgs, rss_getnumbuckets());
6467a9643ea8Slogwang #else
6468a9643ea8Slogwang 	queues = queuemsgs;
6469a9643ea8Slogwang #endif
6470a9643ea8Slogwang 	queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
6471*22ce4affSfengbojiang 	if (bootverbose)
6472*22ce4affSfengbojiang 		device_printf(dev,
6473*22ce4affSfengbojiang 		    "intr CPUs: %d queue msgs: %d admincnt: %d\n",
6474a9643ea8Slogwang 		    CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
6475a9643ea8Slogwang #ifdef  RSS
6476a9643ea8Slogwang 	/* If we're doing RSS, clamp at the number of RSS buckets */
6477a9643ea8Slogwang 	if (queues > rss_getnumbuckets())
6478a9643ea8Slogwang 		queues = rss_getnumbuckets();
6479a9643ea8Slogwang #endif
6480a9643ea8Slogwang 	if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
6481a9643ea8Slogwang 		rx_queues = iflib_num_rx_queues;
6482a9643ea8Slogwang 	else
6483a9643ea8Slogwang 		rx_queues = queues;
6484*22ce4affSfengbojiang 
6485*22ce4affSfengbojiang 	if (rx_queues > scctx->isc_nrxqsets)
6486*22ce4affSfengbojiang 		rx_queues = scctx->isc_nrxqsets;
6487*22ce4affSfengbojiang 
6488a9643ea8Slogwang 	/*
6489a9643ea8Slogwang 	 * We want this to be all logical CPUs by default
6490a9643ea8Slogwang 	 */
6491a9643ea8Slogwang 	if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
6492a9643ea8Slogwang 		tx_queues = iflib_num_tx_queues;
6493a9643ea8Slogwang 	else
6494a9643ea8Slogwang 		tx_queues = mp_ncpus;
6495a9643ea8Slogwang 
6496*22ce4affSfengbojiang 	if (tx_queues > scctx->isc_ntxqsets)
6497*22ce4affSfengbojiang 		tx_queues = scctx->isc_ntxqsets;
6498*22ce4affSfengbojiang 
6499a9643ea8Slogwang 	if (ctx->ifc_sysctl_qs_eq_override == 0) {
6500a9643ea8Slogwang #ifdef INVARIANTS
6501a9643ea8Slogwang 		if (tx_queues != rx_queues)
6502*22ce4affSfengbojiang 			device_printf(dev,
6503*22ce4affSfengbojiang 			    "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
6504a9643ea8Slogwang 			    min(rx_queues, tx_queues), min(rx_queues, tx_queues));
6505a9643ea8Slogwang #endif
6506a9643ea8Slogwang 		tx_queues = min(rx_queues, tx_queues);
6507a9643ea8Slogwang 		rx_queues = min(rx_queues, tx_queues);
6508a9643ea8Slogwang 	}
6509a9643ea8Slogwang 
6510a9643ea8Slogwang 	vectors = rx_queues + admincnt;
6511*22ce4affSfengbojiang 	if (msgs < vectors) {
6512a9643ea8Slogwang 		device_printf(dev,
6513*22ce4affSfengbojiang 		    "insufficient number of MSI-X vectors "
6514*22ce4affSfengbojiang 		    "(supported %d, need %d)\n", msgs, vectors);
6515*22ce4affSfengbojiang 		goto msi;
6516*22ce4affSfengbojiang 	}
6517*22ce4affSfengbojiang 
6518*22ce4affSfengbojiang 	device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues,
6519*22ce4affSfengbojiang 	    tx_queues);
6520*22ce4affSfengbojiang 	msgs = vectors;
6521*22ce4affSfengbojiang 	if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
6522*22ce4affSfengbojiang 		if (vectors != msgs) {
6523*22ce4affSfengbojiang 			device_printf(dev,
6524*22ce4affSfengbojiang 			    "Unable to allocate sufficient MSI-X vectors "
6525*22ce4affSfengbojiang 			    "(got %d, need %d)\n", vectors, msgs);
6526*22ce4affSfengbojiang 			pci_release_msi(dev);
6527*22ce4affSfengbojiang 			if (bar != -1) {
6528*22ce4affSfengbojiang 				bus_release_resource(dev, SYS_RES_MEMORY, bar,
6529*22ce4affSfengbojiang 				    ctx->ifc_msix_mem);
6530*22ce4affSfengbojiang 				ctx->ifc_msix_mem = NULL;
6531*22ce4affSfengbojiang 			}
6532*22ce4affSfengbojiang 			goto msi;
6533*22ce4affSfengbojiang 		}
6534*22ce4affSfengbojiang 		device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
6535*22ce4affSfengbojiang 		    vectors);
6536a9643ea8Slogwang 		scctx->isc_vectors = vectors;
6537a9643ea8Slogwang 		scctx->isc_nrxqsets = rx_queues;
6538a9643ea8Slogwang 		scctx->isc_ntxqsets = tx_queues;
6539a9643ea8Slogwang 		scctx->isc_intr = IFLIB_INTR_MSIX;
6540a9643ea8Slogwang 
6541a9643ea8Slogwang 		return (vectors);
6542a9643ea8Slogwang 	} else {
6543*22ce4affSfengbojiang 		device_printf(dev,
6544*22ce4affSfengbojiang 		    "failed to allocate %d MSI-X vectors, err: %d\n", vectors,
6545*22ce4affSfengbojiang 		    err);
6546*22ce4affSfengbojiang 		if (bar != -1) {
6547*22ce4affSfengbojiang 			bus_release_resource(dev, SYS_RES_MEMORY, bar,
6548*22ce4affSfengbojiang 			    ctx->ifc_msix_mem);
6549*22ce4affSfengbojiang 			ctx->ifc_msix_mem = NULL;
6550a9643ea8Slogwang 		}
6551*22ce4affSfengbojiang 	}
6552*22ce4affSfengbojiang 
6553a9643ea8Slogwang msi:
6554a9643ea8Slogwang 	vectors = pci_msi_count(dev);
6555a9643ea8Slogwang 	scctx->isc_nrxqsets = 1;
6556a9643ea8Slogwang 	scctx->isc_ntxqsets = 1;
6557a9643ea8Slogwang 	scctx->isc_vectors = vectors;
6558a9643ea8Slogwang 	if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
6559a9643ea8Slogwang 		device_printf(dev,"Using an MSI interrupt\n");
6560a9643ea8Slogwang 		scctx->isc_intr = IFLIB_INTR_MSI;
6561a9643ea8Slogwang 	} else {
6562*22ce4affSfengbojiang 		scctx->isc_vectors = 1;
6563a9643ea8Slogwang 		device_printf(dev,"Using a Legacy interrupt\n");
6564a9643ea8Slogwang 		scctx->isc_intr = IFLIB_INTR_LEGACY;
6565a9643ea8Slogwang 	}
6566a9643ea8Slogwang 
6567a9643ea8Slogwang 	return (vectors);
6568a9643ea8Slogwang }
6569a9643ea8Slogwang 
6570*22ce4affSfengbojiang static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
6571a9643ea8Slogwang 
6572a9643ea8Slogwang static int
mp_ring_state_handler(SYSCTL_HANDLER_ARGS)6573a9643ea8Slogwang mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
6574a9643ea8Slogwang {
6575a9643ea8Slogwang 	int rc;
6576a9643ea8Slogwang 	uint16_t *state = ((uint16_t *)oidp->oid_arg1);
6577a9643ea8Slogwang 	struct sbuf *sb;
6578*22ce4affSfengbojiang 	const char *ring_state = "UNKNOWN";
6579a9643ea8Slogwang 
6580a9643ea8Slogwang 	/* XXX needed ? */
6581a9643ea8Slogwang 	rc = sysctl_wire_old_buffer(req, 0);
6582a9643ea8Slogwang 	MPASS(rc == 0);
6583a9643ea8Slogwang 	if (rc != 0)
6584a9643ea8Slogwang 		return (rc);
6585a9643ea8Slogwang 	sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
6586a9643ea8Slogwang 	MPASS(sb != NULL);
6587a9643ea8Slogwang 	if (sb == NULL)
6588a9643ea8Slogwang 		return (ENOMEM);
6589a9643ea8Slogwang 	if (state[3] <= 3)
6590a9643ea8Slogwang 		ring_state = ring_states[state[3]];
6591a9643ea8Slogwang 
6592a9643ea8Slogwang 	sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
6593a9643ea8Slogwang 		    state[0], state[1], state[2], ring_state);
6594a9643ea8Slogwang 	rc = sbuf_finish(sb);
6595a9643ea8Slogwang 	sbuf_delete(sb);
6596a9643ea8Slogwang         return(rc);
6597a9643ea8Slogwang }
6598a9643ea8Slogwang 
6599a9643ea8Slogwang enum iflib_ndesc_handler {
6600a9643ea8Slogwang 	IFLIB_NTXD_HANDLER,
6601a9643ea8Slogwang 	IFLIB_NRXD_HANDLER,
6602a9643ea8Slogwang };
6603a9643ea8Slogwang 
6604a9643ea8Slogwang static int
mp_ndesc_handler(SYSCTL_HANDLER_ARGS)6605a9643ea8Slogwang mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
6606a9643ea8Slogwang {
6607a9643ea8Slogwang 	if_ctx_t ctx = (void *)arg1;
6608a9643ea8Slogwang 	enum iflib_ndesc_handler type = arg2;
6609a9643ea8Slogwang 	char buf[256] = {0};
6610*22ce4affSfengbojiang 	qidx_t *ndesc;
6611a9643ea8Slogwang 	char *p, *next;
6612a9643ea8Slogwang 	int nqs, rc, i;
6613a9643ea8Slogwang 
6614a9643ea8Slogwang 	nqs = 8;
6615a9643ea8Slogwang 	switch(type) {
6616a9643ea8Slogwang 	case IFLIB_NTXD_HANDLER:
6617a9643ea8Slogwang 		ndesc = ctx->ifc_sysctl_ntxds;
6618a9643ea8Slogwang 		if (ctx->ifc_sctx)
6619a9643ea8Slogwang 			nqs = ctx->ifc_sctx->isc_ntxqs;
6620a9643ea8Slogwang 		break;
6621a9643ea8Slogwang 	case IFLIB_NRXD_HANDLER:
6622a9643ea8Slogwang 		ndesc = ctx->ifc_sysctl_nrxds;
6623a9643ea8Slogwang 		if (ctx->ifc_sctx)
6624a9643ea8Slogwang 			nqs = ctx->ifc_sctx->isc_nrxqs;
6625a9643ea8Slogwang 		break;
6626*22ce4affSfengbojiang 	default:
6627*22ce4affSfengbojiang 		printf("%s: unhandled type\n", __func__);
6628*22ce4affSfengbojiang 		return (EINVAL);
6629a9643ea8Slogwang 	}
6630a9643ea8Slogwang 	if (nqs == 0)
6631a9643ea8Slogwang 		nqs = 8;
6632a9643ea8Slogwang 
6633a9643ea8Slogwang 	for (i=0; i<8; i++) {
6634a9643ea8Slogwang 		if (i >= nqs)
6635a9643ea8Slogwang 			break;
6636a9643ea8Slogwang 		if (i)
6637a9643ea8Slogwang 			strcat(buf, ",");
6638a9643ea8Slogwang 		sprintf(strchr(buf, 0), "%d", ndesc[i]);
6639a9643ea8Slogwang 	}
6640a9643ea8Slogwang 
6641a9643ea8Slogwang 	rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
6642a9643ea8Slogwang 	if (rc || req->newptr == NULL)
6643a9643ea8Slogwang 		return rc;
6644a9643ea8Slogwang 
6645a9643ea8Slogwang 	for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
6646a9643ea8Slogwang 	    i++, p = strsep(&next, " ,")) {
6647a9643ea8Slogwang 		ndesc[i] = strtoul(p, NULL, 10);
6648a9643ea8Slogwang 	}
6649a9643ea8Slogwang 
6650a9643ea8Slogwang 	return(rc);
6651a9643ea8Slogwang }
6652a9643ea8Slogwang 
6653a9643ea8Slogwang #define NAME_BUFLEN 32
6654a9643ea8Slogwang static void
iflib_add_device_sysctl_pre(if_ctx_t ctx)6655a9643ea8Slogwang iflib_add_device_sysctl_pre(if_ctx_t ctx)
6656a9643ea8Slogwang {
6657a9643ea8Slogwang         device_t dev = iflib_get_dev(ctx);
6658a9643ea8Slogwang 	struct sysctl_oid_list *child, *oid_list;
6659a9643ea8Slogwang 	struct sysctl_ctx_list *ctx_list;
6660a9643ea8Slogwang 	struct sysctl_oid *node;
6661a9643ea8Slogwang 
6662a9643ea8Slogwang 	ctx_list = device_get_sysctl_ctx(dev);
6663a9643ea8Slogwang 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
6664a9643ea8Slogwang 	ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
6665*22ce4affSfengbojiang 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IFLIB fields");
6666a9643ea8Slogwang 	oid_list = SYSCTL_CHILDREN(node);
6667a9643ea8Slogwang 
6668*22ce4affSfengbojiang 	SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
6669*22ce4affSfengbojiang 		       CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version,
6670a9643ea8Slogwang 		       "driver version");
6671a9643ea8Slogwang 
6672a9643ea8Slogwang 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
6673a9643ea8Slogwang 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
6674a9643ea8Slogwang 			"# of txqs to use, 0 => use default #");
6675a9643ea8Slogwang 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
6676a9643ea8Slogwang 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
6677a9643ea8Slogwang 			"# of rxqs to use, 0 => use default #");
6678a9643ea8Slogwang 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
6679a9643ea8Slogwang 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
6680a9643ea8Slogwang                        "permit #txq != #rxq");
6681*22ce4affSfengbojiang 	SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix",
6682*22ce4affSfengbojiang                       CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
6683*22ce4affSfengbojiang                       "disable MSI-X (default 0)");
6684*22ce4affSfengbojiang 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget",
6685*22ce4affSfengbojiang 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0,
6686*22ce4affSfengbojiang 		       "set the RX budget");
6687*22ce4affSfengbojiang 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate",
6688*22ce4affSfengbojiang 		       CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0,
6689*22ce4affSfengbojiang 		       "cause TX to abdicate instead of running to completion");
6690*22ce4affSfengbojiang 	ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED;
6691*22ce4affSfengbojiang 	SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "core_offset",
6692*22ce4affSfengbojiang 		       CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0,
6693*22ce4affSfengbojiang 		       "offset to start using cores at");
6694*22ce4affSfengbojiang 	SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "separate_txrx",
6695*22ce4affSfengbojiang 		       CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0,
6696*22ce4affSfengbojiang 		       "use separate cores for TX and RX");
6697a9643ea8Slogwang 
6698a9643ea8Slogwang 	/* XXX change for per-queue sizes */
6699a9643ea8Slogwang 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
6700*22ce4affSfengbojiang 	    CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
6701*22ce4affSfengbojiang 	    IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A",
6702*22ce4affSfengbojiang 	    "list of # of TX descriptors to use, 0 = use default #");
6703a9643ea8Slogwang 	SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
6704*22ce4affSfengbojiang 	    CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
6705*22ce4affSfengbojiang 	    IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A",
6706*22ce4affSfengbojiang 	    "list of # of RX descriptors to use, 0 = use default #");
6707a9643ea8Slogwang }
6708a9643ea8Slogwang 
6709a9643ea8Slogwang static void
iflib_add_device_sysctl_post(if_ctx_t ctx)6710a9643ea8Slogwang iflib_add_device_sysctl_post(if_ctx_t ctx)
6711a9643ea8Slogwang {
6712a9643ea8Slogwang 	if_shared_ctx_t sctx = ctx->ifc_sctx;
6713a9643ea8Slogwang 	if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
6714a9643ea8Slogwang         device_t dev = iflib_get_dev(ctx);
6715a9643ea8Slogwang 	struct sysctl_oid_list *child;
6716a9643ea8Slogwang 	struct sysctl_ctx_list *ctx_list;
6717a9643ea8Slogwang 	iflib_fl_t fl;
6718a9643ea8Slogwang 	iflib_txq_t txq;
6719a9643ea8Slogwang 	iflib_rxq_t rxq;
6720a9643ea8Slogwang 	int i, j;
6721a9643ea8Slogwang 	char namebuf[NAME_BUFLEN];
6722a9643ea8Slogwang 	char *qfmt;
6723a9643ea8Slogwang 	struct sysctl_oid *queue_node, *fl_node, *node;
6724a9643ea8Slogwang 	struct sysctl_oid_list *queue_list, *fl_list;
6725a9643ea8Slogwang 	ctx_list = device_get_sysctl_ctx(dev);
6726a9643ea8Slogwang 
6727a9643ea8Slogwang 	node = ctx->ifc_sysctl_node;
6728a9643ea8Slogwang 	child = SYSCTL_CHILDREN(node);
6729a9643ea8Slogwang 
6730a9643ea8Slogwang 	if (scctx->isc_ntxqsets > 100)
6731a9643ea8Slogwang 		qfmt = "txq%03d";
6732a9643ea8Slogwang 	else if (scctx->isc_ntxqsets > 10)
6733a9643ea8Slogwang 		qfmt = "txq%02d";
6734a9643ea8Slogwang 	else
6735a9643ea8Slogwang 		qfmt = "txq%d";
6736a9643ea8Slogwang 	for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
6737a9643ea8Slogwang 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
6738a9643ea8Slogwang 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
6739*22ce4affSfengbojiang 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
6740a9643ea8Slogwang 		queue_list = SYSCTL_CHILDREN(queue_node);
6741a9643ea8Slogwang #if MEMORY_LOGGING
6742a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
6743a9643ea8Slogwang 				CTLFLAG_RD,
6744a9643ea8Slogwang 				&txq->ift_dequeued, "total mbufs freed");
6745a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
6746a9643ea8Slogwang 				CTLFLAG_RD,
6747a9643ea8Slogwang 				&txq->ift_enqueued, "total mbufs enqueued");
6748a9643ea8Slogwang #endif
6749a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
6750a9643ea8Slogwang 				   CTLFLAG_RD,
6751a9643ea8Slogwang 				   &txq->ift_mbuf_defrag, "# of times m_defrag was called");
6752a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
6753a9643ea8Slogwang 				   CTLFLAG_RD,
6754a9643ea8Slogwang 				   &txq->ift_pullups, "# of times m_pullup was called");
6755a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
6756a9643ea8Slogwang 				   CTLFLAG_RD,
6757a9643ea8Slogwang 				   &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
6758a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
6759a9643ea8Slogwang 				   CTLFLAG_RD,
6760a9643ea8Slogwang 				   &txq->ift_no_desc_avail, "# of times no descriptors were available");
6761a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
6762a9643ea8Slogwang 				   CTLFLAG_RD,
6763*22ce4affSfengbojiang 				   &txq->ift_map_failed, "# of times DMA map failed");
6764a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
6765a9643ea8Slogwang 				   CTLFLAG_RD,
6766a9643ea8Slogwang 				   &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
6767a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
6768a9643ea8Slogwang 				   CTLFLAG_RD,
6769a9643ea8Slogwang 				   &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
6770a9643ea8Slogwang 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
6771a9643ea8Slogwang 				   CTLFLAG_RD,
6772a9643ea8Slogwang 				   &txq->ift_pidx, 1, "Producer Index");
6773a9643ea8Slogwang 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
6774a9643ea8Slogwang 				   CTLFLAG_RD,
6775a9643ea8Slogwang 				   &txq->ift_cidx, 1, "Consumer Index");
6776a9643ea8Slogwang 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
6777a9643ea8Slogwang 				   CTLFLAG_RD,
6778a9643ea8Slogwang 				   &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
6779a9643ea8Slogwang 		SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
6780a9643ea8Slogwang 				   CTLFLAG_RD,
6781a9643ea8Slogwang 				   &txq->ift_in_use, 1, "descriptors in use");
6782a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
6783a9643ea8Slogwang 				   CTLFLAG_RD,
6784a9643ea8Slogwang 				   &txq->ift_processed, "descriptors procesed for clean");
6785a9643ea8Slogwang 		SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
6786a9643ea8Slogwang 				   CTLFLAG_RD,
6787a9643ea8Slogwang 				   &txq->ift_cleaned, "total cleaned");
6788a9643ea8Slogwang 		SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
6789*22ce4affSfengbojiang 		    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
6790*22ce4affSfengbojiang 		    __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0,
6791*22ce4affSfengbojiang 		    mp_ring_state_handler, "A", "soft ring state");
6792a9643ea8Slogwang 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
6793*22ce4affSfengbojiang 				       CTLFLAG_RD, &txq->ift_br->enqueues,
6794a9643ea8Slogwang 				       "# of enqueues to the mp_ring for this queue");
6795a9643ea8Slogwang 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
6796*22ce4affSfengbojiang 				       CTLFLAG_RD, &txq->ift_br->drops,
6797a9643ea8Slogwang 				       "# of drops in the mp_ring for this queue");
6798a9643ea8Slogwang 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
6799*22ce4affSfengbojiang 				       CTLFLAG_RD, &txq->ift_br->starts,
6800a9643ea8Slogwang 				       "# of normal consumer starts in the mp_ring for this queue");
6801a9643ea8Slogwang 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
6802*22ce4affSfengbojiang 				       CTLFLAG_RD, &txq->ift_br->stalls,
6803a9643ea8Slogwang 					       "# of consumer stalls in the mp_ring for this queue");
6804a9643ea8Slogwang 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
6805*22ce4affSfengbojiang 			       CTLFLAG_RD, &txq->ift_br->restarts,
6806a9643ea8Slogwang 				       "# of consumer restarts in the mp_ring for this queue");
6807a9643ea8Slogwang 		SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
6808*22ce4affSfengbojiang 				       CTLFLAG_RD, &txq->ift_br->abdications,
6809a9643ea8Slogwang 				       "# of consumer abdications in the mp_ring for this queue");
6810a9643ea8Slogwang 	}
6811a9643ea8Slogwang 
6812a9643ea8Slogwang 	if (scctx->isc_nrxqsets > 100)
6813a9643ea8Slogwang 		qfmt = "rxq%03d";
6814a9643ea8Slogwang 	else if (scctx->isc_nrxqsets > 10)
6815a9643ea8Slogwang 		qfmt = "rxq%02d";
6816a9643ea8Slogwang 	else
6817a9643ea8Slogwang 		qfmt = "rxq%d";
6818a9643ea8Slogwang 	for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
6819a9643ea8Slogwang 		snprintf(namebuf, NAME_BUFLEN, qfmt, i);
6820a9643ea8Slogwang 		queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
6821*22ce4affSfengbojiang 		    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
6822a9643ea8Slogwang 		queue_list = SYSCTL_CHILDREN(queue_node);
6823a9643ea8Slogwang 		if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
6824a9643ea8Slogwang 			SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
6825a9643ea8Slogwang 				       CTLFLAG_RD,
6826a9643ea8Slogwang 				       &rxq->ifr_cq_cidx, 1, "Consumer Index");
6827a9643ea8Slogwang 		}
6828*22ce4affSfengbojiang 
6829a9643ea8Slogwang 		for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
6830a9643ea8Slogwang 			snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
6831a9643ea8Slogwang 			fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
6832*22ce4affSfengbojiang 			    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "freelist Name");
6833a9643ea8Slogwang 			fl_list = SYSCTL_CHILDREN(fl_node);
6834a9643ea8Slogwang 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
6835a9643ea8Slogwang 				       CTLFLAG_RD,
6836a9643ea8Slogwang 				       &fl->ifl_pidx, 1, "Producer Index");
6837a9643ea8Slogwang 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
6838a9643ea8Slogwang 				       CTLFLAG_RD,
6839a9643ea8Slogwang 				       &fl->ifl_cidx, 1, "Consumer Index");
6840a9643ea8Slogwang 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
6841a9643ea8Slogwang 				       CTLFLAG_RD,
6842a9643ea8Slogwang 				       &fl->ifl_credits, 1, "credits available");
6843*22ce4affSfengbojiang 			SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size",
6844*22ce4affSfengbojiang 				       CTLFLAG_RD,
6845*22ce4affSfengbojiang 				       &fl->ifl_buf_size, 1, "buffer size");
6846a9643ea8Slogwang #if MEMORY_LOGGING
6847a9643ea8Slogwang 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
6848a9643ea8Slogwang 					CTLFLAG_RD,
6849a9643ea8Slogwang 					&fl->ifl_m_enqueued, "mbufs allocated");
6850a9643ea8Slogwang 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
6851a9643ea8Slogwang 					CTLFLAG_RD,
6852a9643ea8Slogwang 					&fl->ifl_m_dequeued, "mbufs freed");
6853a9643ea8Slogwang 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
6854a9643ea8Slogwang 					CTLFLAG_RD,
6855a9643ea8Slogwang 					&fl->ifl_cl_enqueued, "clusters allocated");
6856a9643ea8Slogwang 			SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
6857a9643ea8Slogwang 					CTLFLAG_RD,
6858a9643ea8Slogwang 					&fl->ifl_cl_dequeued, "clusters freed");
6859a9643ea8Slogwang #endif
6860a9643ea8Slogwang 		}
6861a9643ea8Slogwang 	}
6862a9643ea8Slogwang 
6863a9643ea8Slogwang }
6864*22ce4affSfengbojiang 
6865*22ce4affSfengbojiang void
iflib_request_reset(if_ctx_t ctx)6866*22ce4affSfengbojiang iflib_request_reset(if_ctx_t ctx)
6867*22ce4affSfengbojiang {
6868*22ce4affSfengbojiang 
6869*22ce4affSfengbojiang 	STATE_LOCK(ctx);
6870*22ce4affSfengbojiang 	ctx->ifc_flags |= IFC_DO_RESET;
6871*22ce4affSfengbojiang 	STATE_UNLOCK(ctx);
6872*22ce4affSfengbojiang }
6873*22ce4affSfengbojiang 
6874*22ce4affSfengbojiang #ifndef __NO_STRICT_ALIGNMENT
6875*22ce4affSfengbojiang static struct mbuf *
iflib_fixup_rx(struct mbuf * m)6876*22ce4affSfengbojiang iflib_fixup_rx(struct mbuf *m)
6877*22ce4affSfengbojiang {
6878*22ce4affSfengbojiang 	struct mbuf *n;
6879*22ce4affSfengbojiang 
6880*22ce4affSfengbojiang 	if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
6881*22ce4affSfengbojiang 		bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
6882*22ce4affSfengbojiang 		m->m_data += ETHER_HDR_LEN;
6883*22ce4affSfengbojiang 		n = m;
6884*22ce4affSfengbojiang 	} else {
6885*22ce4affSfengbojiang 		MGETHDR(n, M_NOWAIT, MT_DATA);
6886*22ce4affSfengbojiang 		if (n == NULL) {
6887*22ce4affSfengbojiang 			m_freem(m);
6888*22ce4affSfengbojiang 			return (NULL);
6889*22ce4affSfengbojiang 		}
6890*22ce4affSfengbojiang 		bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
6891*22ce4affSfengbojiang 		m->m_data += ETHER_HDR_LEN;
6892*22ce4affSfengbojiang 		m->m_len -= ETHER_HDR_LEN;
6893*22ce4affSfengbojiang 		n->m_len = ETHER_HDR_LEN;
6894*22ce4affSfengbojiang 		M_MOVE_PKTHDR(n, m);
6895*22ce4affSfengbojiang 		n->m_next = m;
6896*22ce4affSfengbojiang 	}
6897*22ce4affSfengbojiang 	return (n);
6898*22ce4affSfengbojiang }
6899*22ce4affSfengbojiang #endif
6900*22ce4affSfengbojiang 
6901*22ce4affSfengbojiang #ifdef DEBUGNET
6902*22ce4affSfengbojiang static void
iflib_debugnet_init(if_t ifp,int * nrxr,int * ncl,int * clsize)6903*22ce4affSfengbojiang iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
6904*22ce4affSfengbojiang {
6905*22ce4affSfengbojiang 	if_ctx_t ctx;
6906*22ce4affSfengbojiang 
6907*22ce4affSfengbojiang 	ctx = if_getsoftc(ifp);
6908*22ce4affSfengbojiang 	CTX_LOCK(ctx);
6909*22ce4affSfengbojiang 	*nrxr = NRXQSETS(ctx);
6910*22ce4affSfengbojiang 	*ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size;
6911*22ce4affSfengbojiang 	*clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size;
6912*22ce4affSfengbojiang 	CTX_UNLOCK(ctx);
6913*22ce4affSfengbojiang }
6914*22ce4affSfengbojiang 
6915*22ce4affSfengbojiang static void
iflib_debugnet_event(if_t ifp,enum debugnet_ev event)6916*22ce4affSfengbojiang iflib_debugnet_event(if_t ifp, enum debugnet_ev event)
6917*22ce4affSfengbojiang {
6918*22ce4affSfengbojiang 	if_ctx_t ctx;
6919*22ce4affSfengbojiang 	if_softc_ctx_t scctx;
6920*22ce4affSfengbojiang 	iflib_fl_t fl;
6921*22ce4affSfengbojiang 	iflib_rxq_t rxq;
6922*22ce4affSfengbojiang 	int i, j;
6923*22ce4affSfengbojiang 
6924*22ce4affSfengbojiang 	ctx = if_getsoftc(ifp);
6925*22ce4affSfengbojiang 	scctx = &ctx->ifc_softc_ctx;
6926*22ce4affSfengbojiang 
6927*22ce4affSfengbojiang 	switch (event) {
6928*22ce4affSfengbojiang 	case DEBUGNET_START:
6929*22ce4affSfengbojiang 		for (i = 0; i < scctx->isc_nrxqsets; i++) {
6930*22ce4affSfengbojiang 			rxq = &ctx->ifc_rxqs[i];
6931*22ce4affSfengbojiang 			for (j = 0; j < rxq->ifr_nfl; j++) {
6932*22ce4affSfengbojiang 				fl = rxq->ifr_fl;
6933*22ce4affSfengbojiang 				fl->ifl_zone = m_getzone(fl->ifl_buf_size);
6934*22ce4affSfengbojiang 			}
6935*22ce4affSfengbojiang 		}
6936*22ce4affSfengbojiang 		iflib_no_tx_batch = 1;
6937*22ce4affSfengbojiang 		break;
6938*22ce4affSfengbojiang 	default:
6939*22ce4affSfengbojiang 		break;
6940*22ce4affSfengbojiang 	}
6941*22ce4affSfengbojiang }
6942*22ce4affSfengbojiang 
6943*22ce4affSfengbojiang static int
iflib_debugnet_transmit(if_t ifp,struct mbuf * m)6944*22ce4affSfengbojiang iflib_debugnet_transmit(if_t ifp, struct mbuf *m)
6945*22ce4affSfengbojiang {
6946*22ce4affSfengbojiang 	if_ctx_t ctx;
6947*22ce4affSfengbojiang 	iflib_txq_t txq;
6948*22ce4affSfengbojiang 	int error;
6949*22ce4affSfengbojiang 
6950*22ce4affSfengbojiang 	ctx = if_getsoftc(ifp);
6951*22ce4affSfengbojiang 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6952*22ce4affSfengbojiang 	    IFF_DRV_RUNNING)
6953*22ce4affSfengbojiang 		return (EBUSY);
6954*22ce4affSfengbojiang 
6955*22ce4affSfengbojiang 	txq = &ctx->ifc_txqs[0];
6956*22ce4affSfengbojiang 	error = iflib_encap(txq, &m);
6957*22ce4affSfengbojiang 	if (error == 0)
6958*22ce4affSfengbojiang 		(void)iflib_txd_db_check(txq, true);
6959*22ce4affSfengbojiang 	return (error);
6960*22ce4affSfengbojiang }
6961*22ce4affSfengbojiang 
6962*22ce4affSfengbojiang static int
iflib_debugnet_poll(if_t ifp,int count)6963*22ce4affSfengbojiang iflib_debugnet_poll(if_t ifp, int count)
6964*22ce4affSfengbojiang {
6965*22ce4affSfengbojiang 	struct epoch_tracker et;
6966*22ce4affSfengbojiang 	if_ctx_t ctx;
6967*22ce4affSfengbojiang 	if_softc_ctx_t scctx;
6968*22ce4affSfengbojiang 	iflib_txq_t txq;
6969*22ce4affSfengbojiang 	int i;
6970*22ce4affSfengbojiang 
6971*22ce4affSfengbojiang 	ctx = if_getsoftc(ifp);
6972*22ce4affSfengbojiang 	scctx = &ctx->ifc_softc_ctx;
6973*22ce4affSfengbojiang 
6974*22ce4affSfengbojiang 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6975*22ce4affSfengbojiang 	    IFF_DRV_RUNNING)
6976*22ce4affSfengbojiang 		return (EBUSY);
6977*22ce4affSfengbojiang 
6978*22ce4affSfengbojiang 	txq = &ctx->ifc_txqs[0];
6979*22ce4affSfengbojiang 	(void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
6980*22ce4affSfengbojiang 
6981*22ce4affSfengbojiang 	NET_EPOCH_ENTER(et);
6982*22ce4affSfengbojiang 	for (i = 0; i < scctx->isc_nrxqsets; i++)
6983*22ce4affSfengbojiang 		(void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */);
6984*22ce4affSfengbojiang 	NET_EPOCH_EXIT(et);
6985*22ce4affSfengbojiang 	return (0);
6986*22ce4affSfengbojiang }
6987*22ce4affSfengbojiang #endif /* DEBUGNET */
6988