1 /*-
2 * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Neither the name of Matthew Macy nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 #include "opt_acpi.h"
32
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/bus.h>
36 #include <sys/counter.h>
37 #include <sys/eventhandler.h>
38 #include <sys/fail.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/module.h>
43 #include <sys/kobj.h>
44 #include <sys/pcpu.h>
45 #include <sys/proc.h>
46 #include <sys/rman.h>
47 #include <sys/sbuf.h>
48 #include <sys/sched.h>
49 #include <sys/smp.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/taskqueue.h>
55 #include <sys/limits.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_private.h>
60 #include <net/if_types.h>
61 #include <net/if_media.h>
62 #include <net/bpf.h>
63 #include <net/ethernet.h>
64 #include <net/mp_ring.h>
65 #include <net/debugnet.h>
66 #include <net/pfil.h>
67 #include <net/vnet.h>
68
69 #include <netinet/in.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/tcp_lro.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip6.h>
76 #include <netinet/tcp.h>
77 #include <netinet/udp.h>
78 #include <netinet/ip_var.h>
79 #include <netinet6/ip6_var.h>
80
81 #include <machine/bus.h>
82 #include <machine/in_cksum.h>
83
84 #include <vm/vm.h>
85 #include <vm/pmap.h>
86
87 #include <dev/led/led.h>
88 #include <dev/pci/pcireg.h>
89 #include <dev/pci/pcivar.h>
90 #include <dev/pci/pci_private.h>
91
92 #include <net/iflib.h>
93 #include <net/if_vf_status.h>
94
95 #include "ifdi_if.h"
96
97 #ifdef PCI_IOV
98 #include <dev/pci/pci_iov.h>
99 #endif
100
101 #include <sys/bitstring.h>
102 /*
103 * enable accounting of every mbuf as it comes in to and goes out of
104 * iflib's software descriptor references
105 */
106 #define MEMORY_LOGGING 0
107 /*
108 * Enable mbuf vectors for compressing long mbuf chains
109 */
110
111 /*
112 * NB:
113 * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
114 * we prefetch needs to be determined by the time spent in m_free vis a vis
115 * the cost of a prefetch. This will of course vary based on the workload:
116 * - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
117 * is quite expensive, thus suggesting very little prefetch.
118 * - small packet forwarding which is just returning a single mbuf to
119 * UMA will typically be very fast vis a vis the cost of a memory
120 * access.
121 */
122
123 /*
124 * File organization:
125 * - private structures
126 * - iflib private utility functions
127 * - ifnet functions
128 * - vlan registry and other exported functions
129 * - iflib public core functions
130 *
131 *
132 */
133 static MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
134
135 #define IFLIB_RXEOF_MORE (1U << 0)
136 #define IFLIB_RXEOF_EMPTY (2U << 0)
137 #define IFLIB_TXQ_QUIESCING (1U << 31)
138 #define IFLIB_TXQ_PRODUCER_LLC_SHIFT 20
139 #define IFLIB_TXQ_PRODUCER_MAX \
140 ((1U << IFLIB_TXQ_PRODUCER_LLC_SHIFT) - 1)
141 #define IFLIB_TXQ_PRODUCER(_state) \
142 ((_state) & IFLIB_TXQ_PRODUCER_MAX)
143 #define IFLIB_TXQ_PRODUCER_LLC_MASK \
144 (~(IFLIB_TXQ_QUIESCING | IFLIB_TXQ_PRODUCER_MAX))
145 #define IFLIB_TXQ_PRODUCER_LLC(_state) \
146 (((_state) & IFLIB_TXQ_PRODUCER_LLC_MASK) >> \
147 IFLIB_TXQ_PRODUCER_LLC_SHIFT)
148
149 CTASSERT(MAXCPU - 1 <= (IFLIB_TXQ_PRODUCER_LLC_MASK >>
150 IFLIB_TXQ_PRODUCER_LLC_SHIFT));
151 struct iflib_txq;
152 typedef struct iflib_txq *iflib_txq_t;
153 struct iflib_rxq;
154 typedef struct iflib_rxq *iflib_rxq_t;
155 struct iflib_fl;
156 typedef struct iflib_fl *iflib_fl_t;
157
158 struct iflib_ctx;
159
160 /*
161 * This state describes access to queue mappings owned by iflib. It does not
162 * describe driver-owned administrative DMA or the PCI function's power state.
163 * Normal transitions are serialized by ifc_ctx_sx.
164 *
165 * Only STOPPED establishes that the device can no longer access the mappings;
166 * a failed initialization can leave queues active. IFF_UP separately records
167 * administrative intent. ifc_running separately gates software traffic,
168 * but clearing it does not establish quiescence: the watchdog closes that
169 * gate before the admin task stops the hardware. Use this state for lifecycle
170 * decisions under ifc_ctx_sx, not as an unlocked datapath admission check.
171 */
172 enum iflib_datapath_state {
173 IFLIB_DP_UNKNOWN = 0,
174 IFLIB_DP_STOPPED,
175 IFLIB_DP_FAILED,
176 IFLIB_DP_STARTING,
177 IFLIB_DP_RUNNING,
178 IFLIB_DP_STOPPING,
179 };
180
181 /*
182 * Power-transition state is separate from datapath ownership. It gates
183 * configuration callbacks while the device is entering or remains in low
184 * power without making any claim about driver-owned firmware or admin DMA.
185 */
186 enum iflib_pm_state {
187 IFLIB_PM_ACTIVE = 0,
188 IFLIB_PM_SUSPENDING,
189 IFLIB_PM_SUSPENDED,
190 };
191
192 static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid);
193 static void iflib_timer(void *arg);
194 static void iflib_tqg_detach(if_ctx_t ctx);
195 static int iflib_simple_transmit(if_t ifp, struct mbuf *m);
196 static int iflib_simple_transmit_txq_select(if_t ifp, struct mbuf *m);
197 static int iflib_simple_transmit_txq_select_v2(if_t ifp, struct mbuf *m);
198 static void iflib_simple_if_start(if_t ifp);
199 static void iflib_simple_txq_drain(iflib_txq_t txq);
200
201 typedef struct iflib_filter_info {
202 driver_filter_t *ifi_filter;
203 void *ifi_filter_arg;
204 struct grouptask *ifi_task;
205 void *ifi_ctx;
206 } *iflib_filter_info_t;
207
208 struct iflib_ctx {
209 KOBJ_FIELDS;
210 /*
211 * Pointer to hardware driver's softc
212 */
213 void *ifc_softc;
214 device_t ifc_dev;
215 if_t ifc_ifp;
216
217 cpuset_t ifc_cpus;
218 if_shared_ctx_t ifc_sctx;
219 struct if_softc_ctx ifc_softc_ctx;
220
221 struct sx ifc_ctx_sx;
222 struct mtx ifc_state_mtx;
223
224 iflib_txq_t ifc_txqs;
225 iflib_rxq_t ifc_rxqs;
226 uint32_t ifc_if_flags;
227 uint32_t ifc_flags;
228 enum iflib_datapath_state ifc_datapath_state;
229 enum iflib_pm_state ifc_pm_state;
230 /* Atomic software admission snapshot, not proof of DMA quiescence. */
231 u_int ifc_running;
232 uint32_t ifc_max_fl_buf_size;
233 uint32_t ifc_rx_mbuf_sz;
234
235 int ifc_link_state;
236 uint32_t ifc_tx_watchdog_events;
237 struct cdev *ifc_led_dev;
238 int ifc_led_state;
239 struct resource *ifc_msix_mem;
240
241 struct if_irq ifc_legacy_irq;
242 struct task ifc_admin_task;
243 struct task ifc_led_task;
244 struct task ifc_vflr_task;
245 struct taskqueue *ifc_tq;
246 struct iflib_filter_info ifc_filter_info;
247 struct ifmedia ifc_media;
248 struct ifmedia *ifc_mediap;
249
250 struct sysctl_ctx_list ifc_sysctl_ctx;
251 struct sysctl_oid *ifc_sysctl_node;
252 uint16_t ifc_sysctl_ntxqs;
253 uint16_t ifc_sysctl_nrxqs;
254 uint16_t ifc_sysctl_qs_eq_override;
255 uint16_t ifc_sysctl_rx_budget;
256 uint16_t ifc_sysctl_tx_abdicate;
257 uint16_t ifc_sysctl_core_offset;
258 #define CORE_OFFSET_UNSPECIFIED 0xffff
259 uint8_t ifc_sysctl_separate_txrx;
260 uint8_t ifc_sysctl_use_logical_cores;
261 uint16_t ifc_sysctl_extra_msix_vectors;
262 bool ifc_cpus_are_physical_cores;
263 bool ifc_core_offset_ref;
264 bool ifc_sysctl_simple_tx;
265 bool ifc_sysctl_tx_defer_mfree;
266 uint16_t ifc_sysctl_tx_reclaim_thresh;
267 uint16_t ifc_sysctl_tx_reclaim_ticks;
268
269 qidx_t ifc_sysctl_ntxds[8];
270 qidx_t ifc_sysctl_nrxds[8];
271 struct if_txrx ifc_txrx;
272 #define isc_txd_encap ifc_txrx.ift_txd_encap
273 #define isc_txd_flush ifc_txrx.ift_txd_flush
274 #define isc_txd_credits_update ifc_txrx.ift_txd_credits_update
275 #define isc_rxd_available ifc_txrx.ift_rxd_available
276 #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
277 #define isc_rxd_refill ifc_txrx.ift_rxd_refill
278 #define isc_rxd_flush ifc_txrx.ift_rxd_flush
279 #define isc_legacy_intr ifc_txrx.ift_legacy_intr
280 #define isc_txq_select ifc_txrx.ift_txq_select
281 #define isc_txq_select_v2 ifc_txrx.ift_txq_select_v2
282
283 eventhandler_tag ifc_vlan_attach_event;
284 eventhandler_tag ifc_vlan_detach_event;
285 struct ether_addr ifc_mac;
286 };
287
288 void *
iflib_get_softc(if_ctx_t ctx)289 iflib_get_softc(if_ctx_t ctx)
290 {
291
292 return (ctx->ifc_softc);
293 }
294
295 device_t
iflib_get_dev(if_ctx_t ctx)296 iflib_get_dev(if_ctx_t ctx)
297 {
298
299 return (ctx->ifc_dev);
300 }
301
302 if_t
iflib_get_ifp(if_ctx_t ctx)303 iflib_get_ifp(if_ctx_t ctx)
304 {
305
306 return (ctx->ifc_ifp);
307 }
308
309 bool
iflib_is_running(if_ctx_t ctx)310 iflib_is_running(if_ctx_t ctx)
311 {
312
313 return (atomic_load_acq_int(&ctx->ifc_running) != 0);
314 }
315
316 struct ifmedia *
iflib_get_media(if_ctx_t ctx)317 iflib_get_media(if_ctx_t ctx)
318 {
319
320 return (ctx->ifc_mediap);
321 }
322
323 void
iflib_set_mac(if_ctx_t ctx,uint8_t mac[ETHER_ADDR_LEN])324 iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
325 {
326
327 bcopy(mac, ctx->ifc_mac.octet, ETHER_ADDR_LEN);
328 }
329
330 if_softc_ctx_t
iflib_get_softc_ctx(if_ctx_t ctx)331 iflib_get_softc_ctx(if_ctx_t ctx)
332 {
333
334 return (&ctx->ifc_softc_ctx);
335 }
336
337 if_shared_ctx_t
iflib_get_sctx(if_ctx_t ctx)338 iflib_get_sctx(if_ctx_t ctx)
339 {
340
341 return (ctx->ifc_sctx);
342 }
343
344 uint16_t
iflib_get_extra_msix_vectors_sysctl(if_ctx_t ctx)345 iflib_get_extra_msix_vectors_sysctl(if_ctx_t ctx)
346 {
347
348 return (ctx->ifc_sysctl_extra_msix_vectors);
349 }
350
351 #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
352 #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE / sizeof(void *))
353 #define CACHE_PTR_NEXT(ptr) ((void *)(roundup2(ptr, CACHE_LINE_SIZE)))
354
355 #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
356 #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
357
358 typedef struct iflib_sw_rx_desc_array {
359 bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */
360 struct mbuf **ifsd_m; /* pkthdr mbufs */
361 caddr_t *ifsd_cl; /* direct cluster pointer for rx */
362 bus_addr_t *ifsd_ba; /* bus addr of cluster for rx */
363 } iflib_rxsd_array_t;
364
365 typedef struct iflib_sw_tx_desc_array {
366 bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */
367 bus_dmamap_t *ifsd_tso_map; /* bus_dma maps for TSO packet */
368 struct mbuf **ifsd_m; /* pkthdr mbufs */
369 struct mbuf **ifsd_m_defer; /* deferred mbuf ptr */
370 struct mbuf **ifsd_m_deferb;/* deferred mbuf backing ptr */
371 } if_txsd_vec_t;
372
373 /* magic number that should be high enough for any hardware */
374 #define IFLIB_MAX_TX_SEGS 128
375 #define IFLIB_RX_COPY_THRESH 128
376 #define IFLIB_MAX_RX_REFRESH 32
377 /* The minimum descriptors per second before we start coalescing */
378 #define IFLIB_MIN_DESC_SEC 16384
379 #define IFLIB_DEFAULT_TX_UPDATE_FREQ 16
380 /* maximum number of txqs that can share an rx interrupt */
381 #define IFLIB_MAX_TX_SHARED_INTR 4
382
383 /* this should really scale with ring size - this is a fairly arbitrary value */
384 #define TX_BATCH_SIZE 32
385
386 #define IFLIB_RESTART_BUDGET 8
387
388
389 /*
390 * Encode TSO or !TSO in the low bits of the tx ifsd_m pointer so as
391 * to avoid defref'ing the mbuf to determine the correct busdma resources
392 * to release
393 */
394 #define IFLIB_TSO (1ULL << 0)
395 #define IFLIB_NO_TSO (2ULL << 0)
396 #define IFLIB_FLAGS_MASK (0x3ULL)
397 #define IFLIB_SAVE_MBUF(mbuf, flags) ((void *)(((uintptr_t)mbuf) | flags))
398 #define IFLIB_GET_FLAGS(a) ((uintptr_t)a & IFLIB_FLAGS_MASK)
399 #define IFLIB_GET_MBUF(a) ((struct mbuf *)((uintptr_t)a & ~IFLIB_FLAGS_MASK))
400
401
402 #define IFC_LEGACY 0x001
403 #define IFC_QFLUSH 0x002
404 #define IFC_MULTISEG 0x004
405 #define IFC_INIT_FAILED 0x008
406 #define IFC_SC_ALLOCATED 0x010
407 #define IFC_INIT_DONE 0x020
408 #define IFC_PREFETCH 0x040
409 #define IFC_DO_RESET 0x080
410 #define IFC_DO_WATCHDOG 0x100
411 #define IFC_DO_RESET_IF_UP 0x200
412 #define IFC_SPARE2 0x400
413 #define IFC_IN_DETACH 0x800
414
415 #define IFC_NETMAP_TX_IRQ 0x80000000
416
417 #define CSUM_OFFLOAD (CSUM_IP_TSO | CSUM_IP6_TSO | CSUM_IP | \
418 CSUM_IP_UDP | CSUM_IP_TCP | CSUM_IP_SCTP | \
419 CSUM_IP6_UDP | CSUM_IP6_TCP | CSUM_IP6_SCTP)
420
421 struct iflib_txq {
422 qidx_t ift_in_use;
423 qidx_t ift_cidx;
424 qidx_t ift_cidx_processed;
425 qidx_t ift_pidx;
426 uint8_t ift_gen;
427 uint8_t ift_br_offset:1,
428 ift_defer_mfree:1,
429 ift_spare_bits0:6;
430 uint16_t ift_npending;
431 uint16_t ift_db_pending;
432 uint16_t ift_rs_pending;
433 uint32_t ift_last_reclaim;
434 uint16_t ift_reclaim_thresh;
435 uint16_t ift_reclaim_ticks;
436 uint8_t ift_txd_size[8];
437 uint64_t ift_processed;
438 uint64_t ift_cleaned;
439 uint64_t ift_processed_prev;
440 #if MEMORY_LOGGING
441 uint64_t ift_enqueued;
442 uint64_t ift_dequeued;
443 #endif
444 uint64_t ift_no_tx_dma_setup;
445 uint64_t ift_no_desc_avail;
446 uint64_t ift_mbuf_defrag_failed;
447 uint64_t ift_mbuf_defrag;
448 uint64_t ift_map_failed;
449 uint64_t ift_txd_encap_efbig;
450 uint64_t ift_pullups;
451 uint64_t ift_last_timer_tick;
452 uint64_t ift_drbr_direct;
453 uint64_t ift_drbr_stall;
454 counter_u64_t ift_drbr_deferred;
455 counter_u64_t ift_drbr_drops;
456 counter_u64_t ift_drbr_blocked;
457 counter_u64_t ift_drbr_remote;
458
459 /* Lockless producer count and current llc. */
460 volatile u_int ift_producers __aligned(CACHE_LINE_SIZE);
461
462 struct mtx ift_mtx;
463 struct mtx ift_db_mtx;
464
465 /* constant values */
466 if_ctx_t ift_ctx;
467 struct ifmp_ring *ift_br;
468 struct buf_ring *ift_drbr;
469 struct grouptask ift_task;
470 qidx_t ift_size;
471 qidx_t ift_pad;
472 uint16_t ift_id;
473 struct callout ift_timer;
474 #ifdef DEV_NETMAP
475 struct callout ift_netmap_timer;
476 #endif /* DEV_NETMAP */
477
478 if_txsd_vec_t ift_sds;
479 /*
480 * TX watchdog state, updated once per iflib_timer period. The
481 * period count saturates instead of wrapping, and is 16 bits
482 * wide so that it still reaches any value
483 * net.iflib.tx_watchdog_periods is plausibly set to; an 8-bit
484 * counter would silently disable the check for a threshold
485 * above 255.
486 */
487 qidx_t ift_outstanding_prev;
488 uint16_t ift_wdog_armed;
489 uint8_t ift_closed;
490 uint8_t ift_update_freq;
491 uint8_t ift_spare0[2]; /* pad to the next pointer boundary */
492 struct iflib_filter_info ift_filter_info;
493 bus_dma_tag_t ift_buf_tag;
494 bus_dma_tag_t ift_tso_buf_tag;
495 iflib_dma_info_t ift_ifdi;
496 #define MTX_NAME_LEN 32
497 char ift_mtx_name[MTX_NAME_LEN];
498 bus_dma_segment_t ift_segs[IFLIB_MAX_TX_SEGS] __aligned(CACHE_LINE_SIZE);
499 #ifdef IFLIB_DIAGNOSTICS
500 uint64_t ift_cpu_exec_count[256];
501 #endif
502 } __aligned(CACHE_LINE_SIZE);
503
504 struct iflib_fl {
505 qidx_t ifl_cidx;
506 qidx_t ifl_pidx;
507 qidx_t ifl_credits;
508 uint8_t ifl_gen;
509 uint8_t ifl_rxd_size;
510 #if MEMORY_LOGGING
511 uint64_t ifl_m_enqueued;
512 uint64_t ifl_m_dequeued;
513 uint64_t ifl_cl_enqueued;
514 uint64_t ifl_cl_dequeued;
515 #endif
516 /* implicit pad */
517 bitstr_t *ifl_rx_bitmap;
518 qidx_t ifl_fragidx;
519 /* constant */
520 qidx_t ifl_size;
521 uint16_t ifl_buf_size;
522 uint16_t ifl_cltype;
523 uma_zone_t ifl_zone;
524 iflib_rxsd_array_t ifl_sds;
525 iflib_rxq_t ifl_rxq;
526 uint8_t ifl_id;
527 bus_dma_tag_t ifl_buf_tag;
528 iflib_dma_info_t ifl_ifdi;
529 uint64_t ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
530 qidx_t ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
531 } __aligned(CACHE_LINE_SIZE);
532
533 static inline qidx_t
get_inuse(int size,qidx_t cidx,qidx_t pidx,uint8_t gen)534 get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
535 {
536 qidx_t used;
537
538 if (pidx > cidx)
539 used = pidx - cidx;
540 else if (pidx < cidx)
541 used = size - cidx + pidx;
542 else if (gen == 0 && pidx == cidx)
543 used = 0;
544 else if (gen == 1 && pidx == cidx)
545 used = size;
546 else
547 panic("bad state");
548
549 return (used);
550 }
551
552 #define TXQ_AVAIL(txq) ((txq->ift_size - txq->ift_pad) -\
553 get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
554
555 #define MAX_TX_DESC(ctx) MAX((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \
556 (ctx)->ifc_softc_ctx.isc_tx_nsegments)
557
558 #define IDXDIFF(head, tail, wrap) \
559 ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
560
561 struct iflib_rxq {
562 if_ctx_t ifr_ctx;
563 iflib_fl_t ifr_fl;
564 struct pfil_head *pfil;
565 /*
566 * If there is a separate completion queue (IFLIB_HAS_RXCQ), this is
567 * the completion queue consumer index. Otherwise it's unused.
568 */
569 qidx_t ifr_cq_cidx;
570 uint16_t ifr_id;
571 uint8_t ifr_nfl;
572 uint8_t ifr_ntxqirq;
573 uint8_t ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
574 uint8_t ifr_fl_offset;
575 struct lro_ctrl ifr_lc;
576 struct grouptask ifr_task;
577 struct callout ifr_watchdog;
578 struct iflib_filter_info ifr_filter_info;
579 iflib_dma_info_t ifr_ifdi;
580
581 /* dynamically allocate if any drivers need a value substantially larger than this */
582 struct if_rxd_frag ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
583 #ifdef IFLIB_DIAGNOSTICS
584 uint64_t ifr_cpu_exec_count[256];
585 #endif
586 } __aligned(CACHE_LINE_SIZE);
587
588 typedef struct if_rxsd {
589 caddr_t *ifsd_cl;
590 iflib_fl_t ifsd_fl;
591 } *if_rxsd_t;
592
593 /*
594 * Only allow a single packet to take up most 1/nth of the tx ring
595 */
596 #define MAX_SINGLE_PACKET_FRACTION 12
597 #define IF_BAD_DMA ((bus_addr_t)-1)
598
599 #define CTX_ACTIVE(ctx) iflib_is_running(ctx)
600
601 #define CTX_LOCK_INIT(_sc) sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock")
602 #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx)
603 #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx)
604 #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx)
605
606 #define STATE_LOCK_INIT(_sc, _name) mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF)
607 #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx)
608 #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx)
609 #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx)
610
611 #define CALLOUT_LOCK(txq) mtx_lock(&txq->ift_mtx)
612 #define CALLOUT_UNLOCK(txq) mtx_unlock(&txq->ift_mtx)
613
614 static bool
iflib_admin_enabled(if_ctx_t ctx)615 iflib_admin_enabled(if_ctx_t ctx)
616 {
617
618 sx_assert(&ctx->ifc_ctx_sx, SA_XLOCKED);
619 /*
620 * Before the first stop/init, only drivers with an always-running
621 * admin path need deferred updates. Stopped and failed datapaths
622 * still need link, mailbox and recovery work after that point.
623 */
624 return (ctx->ifc_datapath_state != IFLIB_DP_UNKNOWN ||
625 (ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN) != 0);
626 }
627
628 /* Our boot-time initialization hook */
629 static int iflib_module_event_handler(module_t, int, void *);
630
631 static moduledata_t iflib_moduledata = {
632 "iflib",
633 iflib_module_event_handler,
634 NULL
635 };
636
637 DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
638 MODULE_VERSION(iflib, 1);
639
640 MODULE_DEPEND(iflib, pci, 1, 1, 1);
641 MODULE_DEPEND(iflib, ether, 1, 1, 1);
642
643 TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
644 TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
645
646 #ifndef IFLIB_DEBUG_COUNTERS
647 #ifdef INVARIANTS
648 #define IFLIB_DEBUG_COUNTERS 1
649 #else
650 #define IFLIB_DEBUG_COUNTERS 0
651 #endif /* !INVARIANTS */
652 #endif
653
654 static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
655 "iflib driver parameters");
656
657 static SYSCTL_NODE(_debug_fail_point, OID_AUTO, iflib,
658 CTLFLAG_RW | CTLFLAG_MPSAFE, 0, "iflib fail points");
659
660 static char iflib_register_fail_device[32];
661 SYSCTL_STRING(_debug_fail_point_iflib, OID_AUTO, register_device,
662 CTLFLAG_RW | CTLFLAG_MPSAFE,
663 iflib_register_fail_device, sizeof(iflib_register_fail_device),
664 "device name eligible for registration fail points");
665
666 static char iflib_admin_task_fail_device[32];
667 SYSCTL_STRING(_debug_fail_point_iflib, OID_AUTO, admin_task_device,
668 CTLFLAG_RW | CTLFLAG_MPSAFE,
669 iflib_admin_task_fail_device, sizeof(iflib_admin_task_fail_device),
670 "device name eligible for admin task fail points");
671
672 /*
673 * XXX need to ensure that this can't accidentally cause the head to be moved backwards
674 */
675 static int iflib_min_tx_latency = 0;
676 SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
677 &iflib_min_tx_latency, 0,
678 "minimize transmit latency at the possible expense of throughput");
679 static int iflib_no_tx_batch = 0;
680 SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
681 &iflib_no_tx_batch, 0,
682 "minimize transmit latency at the possible expense of throughput");
683 static int iflib_timer_default = 1000;
684 SYSCTL_INT(_net_iflib, OID_AUTO, timer_default, CTLFLAG_RW,
685 &iflib_timer_default, 0, "number of ticks between iflib_timer calls");
686 /*
687 * Consecutive timer periods a TX queue must stay frozen while demand
688 * persists - see iflib_timer(), which defines those states - before the
689 * hardware is asked whether it has completions pending. Four periods is
690 * roughly two seconds with the default timer interval: a healthy queue on
691 * hardware that coalesces completion reports (e.g. 8254x, TXDCTL.WTHRESH)
692 * stays frozen for at most two (measured on 82541PI), a wedged one until it
693 * is reset.
694 */
695 static int iflib_tx_watchdog_periods = 4;
696 SYSCTL_INT(_net_iflib, OID_AUTO, tx_watchdog_periods, CTLFLAG_RWTUN,
697 &iflib_tx_watchdog_periods, 0,
698 "consecutive frozen timer periods under demand before a TX queue is "
699 "checked for a hang (0 disables the check)");
700
701 #define IFLIB_SIMPLE_TXBR_MIN 64
702 #define IFLIB_SIMPLE_TXBR_SIZE 1024
703 static int iflib_simple_txbr_size = IFLIB_SIMPLE_TXBR_SIZE;
704 SYSCTL_INT(_net_iflib, OID_AUTO, simple_txbr_size, CTLFLAG_RDTUN,
705 &iflib_simple_txbr_size, 0,
706 "number of entries in the simple tx deferral ring");
707 static bool iflib_prefer_mpring = true;
708 SYSCTL_BOOL(_net_iflib, OID_AUTO, prefer_mpring, CTLFLAG_RDTUN,
709 &iflib_prefer_mpring, 0,
710 "prefer the mp_ring transmit path by default");
711 static u_int iflib_simple_drain_quota = 8;
712 SYSCTL_UINT(_net_iflib, OID_AUTO, simple_drain_quota, CTLFLAG_RWTUN,
713 &iflib_simple_drain_quota, 0,
714 "maximum packets sent per simple tx deferral ring drain from the tx task");
715 static u_int iflib_simple_drain_quota_thread = 65536;
716 SYSCTL_UINT(_net_iflib, OID_AUTO, simple_drain_quota_thread, CTLFLAG_RWTUN,
717 &iflib_simple_drain_quota_thread, 0,
718 "maximum packets sent per simple tx deferral ring drain from another thread");
719 static u_int iflib_max_producers = 8;
720 static bool iflib_single_llc __read_mostly;
721 static bool iflib_producer_gate __read_mostly;
722
723 static int
iflib_sysctl_max_producers(SYSCTL_HANDLER_ARGS)724 iflib_sysctl_max_producers(SYSCTL_HANDLER_ARGS)
725 {
726 u_int max_producers;
727 int error;
728
729 max_producers = iflib_max_producers;
730 error = sysctl_handle_int(oidp, &max_producers, 0, req);
731 if (error != 0 || req->newptr == NULL)
732 return (error);
733
734 max_producers = MIN(max_producers, IFLIB_TXQ_PRODUCER_MAX);
735 iflib_max_producers = max_producers;
736 if (iflib_single_llc)
737 iflib_producer_gate = mp_ncpus > max_producers;
738 return (0);
739 }
740 SYSCTL_PROC(_net_iflib, OID_AUTO, max_producers,
741 CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, &iflib_max_producers, 0,
742 iflib_sysctl_max_producers, "IU",
743 "maximum concurrent lockless producers per transmit queue");
744
745 /* Encoded llc for each CPU. */
746 static uint16_t iflib_cpu_llc[MAXCPU] __read_mostly;
747
748 #if IFLIB_DEBUG_COUNTERS
749
750 static int iflib_tx_seen;
751 static int iflib_tx_sent;
752 static int iflib_tx_encap;
753 static int iflib_rx_allocs;
754 static int iflib_fl_refills;
755 static int iflib_fl_refills_large;
756 static int iflib_tx_frees;
757
758 SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD, &iflib_tx_seen, 0,
759 "# TX mbufs seen");
760 SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD, &iflib_tx_sent, 0,
761 "# TX mbufs sent");
762 SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD, &iflib_tx_encap, 0,
763 "# TX mbufs encapped");
764 SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD, &iflib_tx_frees, 0,
765 "# TX frees");
766 SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD, &iflib_rx_allocs, 0,
767 "# RX allocations");
768 SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD, &iflib_fl_refills, 0,
769 "# refills");
770 SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
771 &iflib_fl_refills_large, 0, "# large refills");
772
773 static int iflib_txq_drain_flushing;
774 static int iflib_txq_drain_stopped;
775 static int iflib_txq_drain_notready;
776
777 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
778 &iflib_txq_drain_flushing, 0, "# drain flushes");
779 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_stopped, CTLFLAG_RD,
780 &iflib_txq_drain_stopped, 0, "# drains interrupted by stop");
781 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
782 &iflib_txq_drain_notready, 0, "# drain notready");
783
784 static int iflib_encap_load_mbuf_fail;
785 static int iflib_encap_pad_mbuf_fail;
786 static int iflib_encap_txq_avail_fail;
787 static int iflib_encap_txd_encap_fail;
788
789 SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
790 &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
791 SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD,
792 &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures");
793 SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
794 &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
795 SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
796 &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
797
798 static int iflib_task_fn_rxs;
799 static int iflib_rx_intr_enables;
800 static int iflib_fast_intrs;
801 static int iflib_rx_unavail;
802 static int iflib_rx_ctx_inactive;
803 static int iflib_rx_if_input;
804 static int iflib_rxd_flush;
805
806 static int iflib_verbose_debug;
807
808 SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD, &iflib_task_fn_rxs, 0,
809 "# task_fn_rx calls");
810 SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
811 &iflib_rx_intr_enables, 0, "# RX intr enables");
812 SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD, &iflib_fast_intrs, 0,
813 "# fast_intr calls");
814 SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD, &iflib_rx_unavail, 0,
815 "# times rxeof called with no available data");
816 SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
817 &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
818 SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD, &iflib_rx_if_input,
819 0, "# times rxeof called if_input");
820 SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD, &iflib_rxd_flush, 0,
821 "# times rxd_flush called");
822 SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
823 &iflib_verbose_debug, 0, "enable verbose debugging");
824
825 #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
826 static void
iflib_debug_reset(void)827 iflib_debug_reset(void)
828 {
829 iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
830 iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
831 iflib_txq_drain_flushing = iflib_txq_drain_stopped =
832 iflib_txq_drain_notready =
833 iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail =
834 iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail =
835 iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs =
836 iflib_rx_unavail =
837 iflib_rx_ctx_inactive = iflib_rx_if_input =
838 iflib_rxd_flush = 0;
839 }
840
841 #else
842 #define DBG_COUNTER_INC(name)
iflib_debug_reset(void)843 static void iflib_debug_reset(void) {}
844 #endif
845
846 #define IFLIB_DEBUG 0
847
848 static void iflib_tx_structures_free(if_ctx_t ctx);
849 static void iflib_rx_structures_free(if_ctx_t ctx);
850 static int iflib_queues_alloc(if_ctx_t ctx);
851 static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
852 static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
853 static int iflib_qset_structures_setup(if_ctx_t ctx);
854 static int iflib_msix_init(if_ctx_t ctx);
855 static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str);
856 static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
857 static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
858 #ifdef ALTQ
859 static void iflib_altq_if_start(if_t ifp);
860 static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m);
861 #endif
862 static void iflib_register(if_ctx_t);
863 static void iflib_deregister(if_ctx_t);
864 static void iflib_unregister_vlan_handlers(if_ctx_t ctx);
865 static uint16_t iflib_get_mbuf_size_for(unsigned int size);
866 static void iflib_init_locked(if_ctx_t ctx);
867 static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
868 static void iflib_add_device_sysctl_post(if_ctx_t ctx);
869 static void iflib_ifmp_purge(iflib_txq_t txq);
870 static void _iflib_pre_assert(if_softc_ctx_t scctx);
871 static void iflib_stop(if_ctx_t ctx);
872 static void iflib_if_init_locked(if_ctx_t ctx);
873 static void iflib_free_intr_mem(if_ctx_t ctx);
874 #ifndef __NO_STRICT_ALIGNMENT
875 static struct mbuf *iflib_fixup_rx(struct mbuf *m);
876 #endif
877 static __inline int iflib_completed_tx_reclaim(iflib_txq_t txq,
878 struct mbuf **m_defer);
879 static __inline void iflib_completed_tx_reclaim_force(iflib_txq_t txq);
880
881 static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets =
882 SLIST_HEAD_INITIALIZER(cpu_offsets);
883 struct cpu_offset {
884 SLIST_ENTRY(cpu_offset) entries;
885 cpuset_t set;
886 unsigned int refcount;
887 uint16_t next_cpuid;
888 };
889 static struct mtx cpu_offset_mtx;
890 MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock",
891 MTX_DEF);
892
893 DEBUGNET_DEFINE(iflib);
894
895 static int
iflib_num_rx_descs(if_ctx_t ctx)896 iflib_num_rx_descs(if_ctx_t ctx)
897 {
898 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
899 if_shared_ctx_t sctx = ctx->ifc_sctx;
900 uint16_t first_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
901
902 return (scctx->isc_nrxd[first_rxq]);
903 }
904
905 static int
iflib_num_tx_descs(if_ctx_t ctx)906 iflib_num_tx_descs(if_ctx_t ctx)
907 {
908 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
909 if_shared_ctx_t sctx = ctx->ifc_sctx;
910 uint16_t first_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
911
912 return (scctx->isc_ntxd[first_txq]);
913 }
914
915 #ifdef DEV_NETMAP
916 #include <sys/selinfo.h>
917 #include <net/netmap.h>
918 #include <dev/netmap/netmap_kern.h>
919
920 MODULE_DEPEND(iflib, netmap, 1, 1, 1);
921
922 static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init);
923 static void iflib_netmap_timer(void *arg);
924
925 /*
926 * device-specific sysctl variables:
927 *
928 * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
929 * During regular operations the CRC is stripped, but on some
930 * hardware reception of frames not multiple of 64 is slower,
931 * so using crcstrip=0 helps in benchmarks.
932 *
933 * iflib_rx_miss, iflib_rx_miss_bufs:
934 * count packets that might be missed due to lost interrupts.
935 */
936 SYSCTL_DECL(_dev_netmap);
937 /*
938 * The xl driver by default strips CRCs and we do not override it.
939 */
940
941 int iflib_crcstrip = 1;
942 SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
943 CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames");
944
945 int iflib_rx_miss, iflib_rx_miss_bufs;
946 SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
947 CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr");
948 SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
949 CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs");
950
951 /*
952 * Register/unregister. We are already under netmap lock.
953 * Only called on the first register or the last unregister.
954 */
955 static int
iflib_netmap_register(struct netmap_adapter * na,int onoff)956 iflib_netmap_register(struct netmap_adapter *na, int onoff)
957 {
958 if_t ifp = na->ifp;
959 if_ctx_t ctx = if_getsoftc(ifp);
960 int status;
961
962 CTX_LOCK(ctx);
963 if (!CTX_IS_VF(ctx))
964 IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
965
966 iflib_stop(ctx);
967
968 /*
969 * Enable (or disable) netmap flags, and intercept (or restore)
970 * ifp->if_transmit. This is done once the device has been stopped
971 * to prevent race conditions. Also, this must be done after
972 * calling netmap_disable_all_rings() and before calling
973 * netmap_enable_all_rings(), so that these two functions see the
974 * updated state of the NAF_NETMAP_ON bit.
975 */
976 if (onoff) {
977 nm_set_native_flags(na);
978 } else {
979 nm_clear_native_flags(na);
980 }
981
982 iflib_init_locked(ctx);
983 IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
984 status = ctx->ifc_datapath_state == IFLIB_DP_RUNNING ? 0 : 1;
985 if (status)
986 nm_clear_native_flags(na);
987 CTX_UNLOCK(ctx);
988 return (status);
989 }
990
991 static int
iflib_netmap_config(struct netmap_adapter * na,struct nm_config_info * info)992 iflib_netmap_config(struct netmap_adapter *na, struct nm_config_info *info)
993 {
994 if_t ifp = na->ifp;
995 if_ctx_t ctx = if_getsoftc(ifp);
996 iflib_rxq_t rxq = &ctx->ifc_rxqs[0];
997 iflib_fl_t fl = &rxq->ifr_fl[0];
998
999 info->num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
1000 info->num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
1001 info->num_tx_descs = iflib_num_tx_descs(ctx);
1002 info->num_rx_descs = iflib_num_rx_descs(ctx);
1003 info->rx_buf_maxsize = fl->ifl_buf_size;
1004 nm_prinf("txr %u rxr %u txd %u rxd %u rbufsz %u",
1005 info->num_tx_rings, info->num_rx_rings, info->num_tx_descs,
1006 info->num_rx_descs, info->rx_buf_maxsize);
1007
1008 return (0);
1009 }
1010
1011 static int
netmap_fl_refill(iflib_rxq_t rxq,struct netmap_kring * kring,bool init)1012 netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init)
1013 {
1014 struct netmap_adapter *na = kring->na;
1015 u_int const lim = kring->nkr_num_slots - 1;
1016 struct netmap_ring *ring = kring->ring;
1017 bus_dmamap_t *map;
1018 struct if_rxd_update iru;
1019 if_ctx_t ctx = rxq->ifr_ctx;
1020 iflib_fl_t fl = &rxq->ifr_fl[0];
1021 u_int nic_i_first, nic_i;
1022 u_int nm_i;
1023 int i, n;
1024 #if IFLIB_DEBUG_COUNTERS
1025 int rf_count = 0;
1026 #endif
1027
1028 /*
1029 * This function is used both at initialization and in rxsync.
1030 * At initialization we need to prepare (with isc_rxd_refill())
1031 * all the netmap buffers currently owned by the kernel, in
1032 * such a way to keep fl->ifl_pidx and kring->nr_hwcur in sync
1033 * (except for kring->nkr_hwofs). These may be less than
1034 * kring->nkr_num_slots if netmap_reset() was called while
1035 * an application using the kring that still owned some
1036 * buffers.
1037 * At rxsync time, both indexes point to the next buffer to be
1038 * refilled.
1039 * In any case we publish (with isc_rxd_flush()) up to
1040 * (fl->ifl_pidx - 1) % N (included), to avoid the NIC tail/prod
1041 * pointer to overrun the head/cons pointer, although this is
1042 * not necessary for some NICs (e.g. vmx).
1043 */
1044 if (__predict_false(init)) {
1045 n = kring->nkr_num_slots - nm_kr_rxspace(kring);
1046 } else {
1047 n = kring->rhead - kring->nr_hwcur;
1048 if (n == 0)
1049 return (0); /* Nothing to do. */
1050 if (n < 0)
1051 n += kring->nkr_num_slots;
1052 }
1053
1054 iru_init(&iru, rxq, 0 /* flid */);
1055 map = fl->ifl_sds.ifsd_map;
1056 nic_i = fl->ifl_pidx;
1057 nm_i = netmap_idx_n2k(kring, nic_i);
1058 if (__predict_false(init)) {
1059 /*
1060 * On init/reset, nic_i must be 0, and we must
1061 * start to refill from hwtail (see netmap_reset()).
1062 */
1063 MPASS(nic_i == 0);
1064 MPASS(nm_i == kring->nr_hwtail);
1065 } else
1066 MPASS(nm_i == kring->nr_hwcur);
1067 DBG_COUNTER_INC(fl_refills);
1068 while (n > 0) {
1069 #if IFLIB_DEBUG_COUNTERS
1070 if (++rf_count == 9)
1071 DBG_COUNTER_INC(fl_refills_large);
1072 #endif
1073 nic_i_first = nic_i;
1074 for (i = 0; n > 0 && i < IFLIB_MAX_RX_REFRESH; n--, i++) {
1075 struct netmap_slot *slot = &ring->slot[nm_i];
1076 uint64_t paddr;
1077 void *addr = PNMB(na, slot, &paddr);
1078
1079 MPASS(i < IFLIB_MAX_RX_REFRESH);
1080
1081 if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
1082 return (netmap_ring_reinit(kring));
1083
1084 fl->ifl_bus_addrs[i] = paddr +
1085 nm_get_offset(kring, slot);
1086 fl->ifl_rxd_idxs[i] = nic_i;
1087
1088 if (__predict_false(init)) {
1089 netmap_load_map(na, fl->ifl_buf_tag,
1090 map[nic_i], addr);
1091 } else if (slot->flags & NS_BUF_CHANGED) {
1092 /* buffer has changed, reload map */
1093 netmap_reload_map(na, fl->ifl_buf_tag,
1094 map[nic_i], addr);
1095 }
1096 bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i],
1097 BUS_DMASYNC_PREREAD);
1098 slot->flags &= ~NS_BUF_CHANGED;
1099
1100 nm_i = nm_next(nm_i, lim);
1101 nic_i = nm_next(nic_i, lim);
1102 }
1103
1104 iru.iru_pidx = nic_i_first;
1105 iru.iru_count = i;
1106 ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
1107 }
1108 fl->ifl_pidx = nic_i;
1109 /*
1110 * At the end of the loop we must have refilled everything
1111 * we could possibly refill.
1112 */
1113 MPASS(nm_i == kring->rhead);
1114 kring->nr_hwcur = nm_i;
1115
1116 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
1117 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1118 ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id,
1119 nm_prev(nic_i, lim));
1120 DBG_COUNTER_INC(rxd_flush);
1121
1122 return (0);
1123 }
1124
1125 #define NETMAP_TX_TIMER_US 90
1126
1127 /*
1128 * Reconcile kernel and user view of the transmit ring.
1129 *
1130 * All information is in the kring.
1131 * Userspace wants to send packets up to the one before kring->rhead,
1132 * kernel knows kring->nr_hwcur is the first unsent packet.
1133 *
1134 * Here we push packets out (as many as possible), and possibly
1135 * reclaim buffers from previously completed transmission.
1136 *
1137 * The caller (netmap) guarantees that there is only one instance
1138 * running at any time. Any interference with other driver
1139 * methods should be handled by the individual drivers.
1140 */
1141 static int
iflib_netmap_txsync(struct netmap_kring * kring,int flags)1142 iflib_netmap_txsync(struct netmap_kring *kring, int flags)
1143 {
1144 struct netmap_adapter *na = kring->na;
1145 if_t ifp = na->ifp;
1146 struct netmap_ring *ring = kring->ring;
1147 u_int nm_i; /* index into the netmap kring */
1148 u_int nic_i; /* index into the NIC ring */
1149 u_int const lim = kring->nkr_num_slots - 1;
1150 u_int const head = kring->rhead;
1151 struct if_pkt_info pi;
1152 int tx_pkts = 0, tx_bytes = 0;
1153
1154 /*
1155 * interrupts on every tx packet are expensive so request
1156 * them every half ring, or where NS_REPORT is set
1157 */
1158 u_int report_frequency = kring->nkr_num_slots >> 1;
1159 /* device-specific */
1160 if_ctx_t ctx = if_getsoftc(ifp);
1161 iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
1162
1163 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
1164 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1165
1166 /*
1167 * First part: process new packets to send.
1168 * nm_i is the current index in the netmap kring,
1169 * nic_i is the corresponding index in the NIC ring.
1170 *
1171 * If we have packets to send (nm_i != head)
1172 * iterate over the netmap ring, fetch length and update
1173 * the corresponding slot in the NIC ring. Some drivers also
1174 * need to update the buffer's physical address in the NIC slot
1175 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
1176 *
1177 * The netmap_reload_map() calls is especially expensive,
1178 * even when (as in this case) the tag is 0, so do only
1179 * when the buffer has actually changed.
1180 *
1181 * If possible do not set the report/intr bit on all slots,
1182 * but only a few times per ring or when NS_REPORT is set.
1183 *
1184 * Finally, on 10G and faster drivers, it might be useful
1185 * to prefetch the next slot and txr entry.
1186 */
1187
1188 nm_i = kring->nr_hwcur;
1189 if (nm_i != head) { /* we have new packets to send */
1190 uint32_t pkt_len = 0, seg_idx = 0;
1191 int nic_i_start = -1, flags = 0;
1192 memset(&pi, 0, sizeof(pi));
1193 pi.ipi_segs = txq->ift_segs;
1194 pi.ipi_qsidx = kring->ring_id;
1195 nic_i = netmap_idx_k2n(kring, nm_i);
1196
1197 __builtin_prefetch(&ring->slot[nm_i]);
1198 __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
1199 __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
1200
1201 while (nm_i != head) {
1202 struct netmap_slot *slot = &ring->slot[nm_i];
1203 uint64_t offset = nm_get_offset(kring, slot);
1204 u_int len = slot->len;
1205 uint64_t paddr;
1206 void *addr = PNMB(na, slot, &paddr);
1207
1208 flags |= (slot->flags & NS_REPORT ||
1209 nic_i == 0 || nic_i == report_frequency) ?
1210 IPI_TX_INTR : 0;
1211
1212 /*
1213 * If this is the first packet fragment, save the
1214 * index of the first NIC slot for later.
1215 */
1216 if (nic_i_start < 0)
1217 nic_i_start = nic_i;
1218
1219 pi.ipi_segs[seg_idx].ds_addr = paddr + offset;
1220 pi.ipi_segs[seg_idx].ds_len = len;
1221 if (len) {
1222 pkt_len += len;
1223 seg_idx++;
1224 }
1225
1226 if (!(slot->flags & NS_MOREFRAG)) {
1227 pi.ipi_len = pkt_len;
1228 pi.ipi_nsegs = seg_idx;
1229 pi.ipi_pidx = nic_i_start;
1230 pi.ipi_ndescs = 0;
1231 pi.ipi_flags = flags;
1232
1233 /* Prepare the NIC TX ring. */
1234 ctx->isc_txd_encap(ctx->ifc_softc, &pi);
1235 DBG_COUNTER_INC(tx_encap);
1236
1237 /* Update transmit counters */
1238 tx_bytes += pi.ipi_len;
1239 tx_pkts++;
1240
1241 /* Reinit per-packet info for the next one. */
1242 flags = seg_idx = pkt_len = 0;
1243 nic_i_start = -1;
1244 }
1245
1246 /* prefetch for next round */
1247 __builtin_prefetch(&ring->slot[nm_i + 1]);
1248 __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
1249 __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
1250
1251 NM_CHECK_ADDR_LEN_OFF(na, len, offset);
1252
1253 if (slot->flags & NS_BUF_CHANGED) {
1254 /* buffer has changed, reload map */
1255 netmap_reload_map(na, txq->ift_buf_tag,
1256 txq->ift_sds.ifsd_map[nic_i], addr);
1257 }
1258 /* make sure changes to the buffer are synced */
1259 bus_dmamap_sync(txq->ift_buf_tag,
1260 txq->ift_sds.ifsd_map[nic_i],
1261 BUS_DMASYNC_PREWRITE);
1262
1263 slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED | NS_MOREFRAG);
1264 nm_i = nm_next(nm_i, lim);
1265 nic_i = nm_next(nic_i, lim);
1266 }
1267 kring->nr_hwcur = nm_i;
1268
1269 /* synchronize the NIC ring */
1270 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
1271 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1272
1273 /* (re)start the tx unit up to slot nic_i (excluded) */
1274 ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
1275 }
1276
1277 /*
1278 * Second part: reclaim buffers for completed transmissions.
1279 *
1280 * If there are unclaimed buffers, attempt to reclaim them.
1281 * If we don't manage to reclaim them all, and TX IRQs are not in use,
1282 * trigger a per-tx-queue timer to try again later.
1283 */
1284 if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
1285 if (iflib_tx_credits_update(ctx, txq)) {
1286 /* some tx completed, increment avail */
1287 nic_i = txq->ift_cidx_processed;
1288 kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
1289 }
1290 }
1291
1292 if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ))
1293 if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
1294 callout_reset_sbt_on(&txq->ift_netmap_timer,
1295 NETMAP_TX_TIMER_US * SBT_1US, SBT_1US,
1296 iflib_netmap_timer, txq,
1297 txq->ift_netmap_timer.c_cpu, 0);
1298 }
1299
1300 if_inc_counter(ifp, IFCOUNTER_OBYTES, tx_bytes);
1301 if_inc_counter(ifp, IFCOUNTER_OPACKETS, tx_pkts);
1302
1303 return (0);
1304 }
1305
1306 /*
1307 * Reconcile kernel and user view of the receive ring.
1308 * Same as for the txsync, this routine must be efficient.
1309 * The caller guarantees a single invocations, but races against
1310 * the rest of the driver should be handled here.
1311 *
1312 * On call, kring->rhead is the first packet that userspace wants
1313 * to keep, and kring->rcur is the wakeup point.
1314 * The kernel has previously reported packets up to kring->rtail.
1315 *
1316 * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
1317 * of whether or not we received an interrupt.
1318 */
1319 static int
iflib_netmap_rxsync(struct netmap_kring * kring,int flags)1320 iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
1321 {
1322 struct netmap_adapter *na = kring->na;
1323 struct netmap_ring *ring = kring->ring;
1324 if_t ifp = na->ifp;
1325 uint32_t nm_i; /* index into the netmap ring */
1326 uint32_t nic_i; /* index into the NIC ring */
1327 u_int n;
1328 u_int const lim = kring->nkr_num_slots - 1;
1329 int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
1330 int i = 0, rx_bytes = 0, rx_pkts = 0;
1331
1332 if_ctx_t ctx = if_getsoftc(ifp);
1333 if_shared_ctx_t sctx = ctx->ifc_sctx;
1334 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1335 iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
1336 iflib_fl_t fl = &rxq->ifr_fl[0];
1337 struct if_rxd_info ri;
1338 qidx_t *cidxp;
1339
1340 /*
1341 * netmap only uses free list 0, to avoid out of order consumption
1342 * of receive buffers
1343 */
1344
1345 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
1346 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1347
1348 /*
1349 * First part: import newly received packets.
1350 *
1351 * nm_i is the index of the next free slot in the netmap ring,
1352 * nic_i is the index of the next received packet in the NIC ring
1353 * (or in the free list 0 if IFLIB_HAS_RXCQ is set), and they may
1354 * differ in case if_init() has been called while
1355 * in netmap mode. For the receive ring we have
1356 *
1357 * nic_i = fl->ifl_cidx;
1358 * nm_i = kring->nr_hwtail (previous)
1359 * and
1360 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
1361 *
1362 * fl->ifl_cidx is set to 0 on a ring reinit
1363 */
1364 if (netmap_no_pendintr || force_update) {
1365 uint32_t hwtail_lim = nm_prev(kring->nr_hwcur, lim);
1366 bool have_rxcq = sctx->isc_flags & IFLIB_HAS_RXCQ;
1367 int crclen = iflib_crcstrip ? 0 : 4;
1368 int error, avail;
1369
1370 /*
1371 * For the free list consumer index, we use the same
1372 * logic as in iflib_rxeof().
1373 */
1374 if (have_rxcq)
1375 cidxp = &rxq->ifr_cq_cidx;
1376 else
1377 cidxp = &fl->ifl_cidx;
1378 avail = ctx->isc_rxd_available(ctx->ifc_softc,
1379 rxq->ifr_id, *cidxp, USHRT_MAX);
1380
1381 nic_i = fl->ifl_cidx;
1382 nm_i = netmap_idx_n2k(kring, nic_i);
1383 MPASS(nm_i == kring->nr_hwtail);
1384 for (n = 0; avail > 0 && nm_i != hwtail_lim; n++, avail--) {
1385 memset(&ri, 0, sizeof(ri));
1386 ri.iri_frags = rxq->ifr_frags;
1387 ri.iri_qsidx = kring->ring_id;
1388 ri.iri_ifp = ctx->ifc_ifp;
1389 ri.iri_cidx = *cidxp;
1390
1391 error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
1392 for (i = 0; i < ri.iri_nfrags; i++) {
1393 if (error) {
1394 ring->slot[nm_i].len = 0;
1395 ring->slot[nm_i].flags = 0;
1396 } else {
1397 ring->slot[nm_i].len = ri.iri_frags[i].irf_len;
1398 if (i == (ri.iri_nfrags - 1)) {
1399 ring->slot[nm_i].len -= crclen;
1400 ring->slot[nm_i].flags = 0;
1401
1402 /* Update receive counters */
1403 rx_bytes += ri.iri_len;
1404 rx_pkts++;
1405 } else
1406 ring->slot[nm_i].flags = NS_MOREFRAG;
1407 }
1408
1409 bus_dmamap_sync(fl->ifl_buf_tag,
1410 fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
1411 nm_i = nm_next(nm_i, lim);
1412 fl->ifl_cidx = nic_i = nm_next(nic_i, lim);
1413 }
1414
1415 if (have_rxcq) {
1416 *cidxp = ri.iri_cidx;
1417 while (*cidxp >= scctx->isc_nrxd[0])
1418 *cidxp -= scctx->isc_nrxd[0];
1419 }
1420
1421 }
1422 if (n) { /* update the state variables */
1423 if (netmap_no_pendintr && !force_update) {
1424 /* diagnostics */
1425 iflib_rx_miss++;
1426 iflib_rx_miss_bufs += n;
1427 }
1428 kring->nr_hwtail = nm_i;
1429 }
1430 kring->nr_kflags &= ~NKR_PENDINTR;
1431 }
1432 /*
1433 * Second part: skip past packets that userspace has released.
1434 * (kring->nr_hwcur to head excluded),
1435 * and make the buffers available for reception.
1436 * As usual nm_i is the index in the netmap ring,
1437 * nic_i is the index in the NIC ring, and
1438 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
1439 */
1440 netmap_fl_refill(rxq, kring, false);
1441
1442 if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
1443 if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
1444
1445 return (0);
1446 }
1447
1448 static void
iflib_netmap_intr(struct netmap_adapter * na,int onoff)1449 iflib_netmap_intr(struct netmap_adapter *na, int onoff)
1450 {
1451 if_ctx_t ctx = if_getsoftc(na->ifp);
1452
1453 CTX_LOCK(ctx);
1454 if (onoff) {
1455 IFDI_INTR_ENABLE(ctx);
1456 } else {
1457 IFDI_INTR_DISABLE(ctx);
1458 }
1459 CTX_UNLOCK(ctx);
1460 }
1461
1462 static int
iflib_netmap_attach(if_ctx_t ctx)1463 iflib_netmap_attach(if_ctx_t ctx)
1464 {
1465 struct netmap_adapter na;
1466
1467 bzero(&na, sizeof(na));
1468
1469 na.ifp = ctx->ifc_ifp;
1470 na.na_flags = NAF_BDG_MAYSLEEP | NAF_MOREFRAG | NAF_OFFSETS;
1471 MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
1472 MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
1473
1474 na.num_tx_desc = iflib_num_tx_descs(ctx);
1475 na.num_rx_desc = iflib_num_rx_descs(ctx);
1476 na.nm_txsync = iflib_netmap_txsync;
1477 na.nm_rxsync = iflib_netmap_rxsync;
1478 na.nm_register = iflib_netmap_register;
1479 na.nm_intr = iflib_netmap_intr;
1480 na.nm_config = iflib_netmap_config;
1481 na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
1482 na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
1483 return (netmap_attach(&na));
1484 }
1485
1486 static int
iflib_netmap_txq_init(if_ctx_t ctx,iflib_txq_t txq)1487 iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
1488 {
1489 struct netmap_adapter *na = NA(ctx->ifc_ifp);
1490 struct netmap_slot *slot;
1491
1492 slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1493 if (slot == NULL)
1494 return (0);
1495 for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
1496 /*
1497 * In netmap mode, set the map for the packet buffer.
1498 * NOTE: Some drivers (not this one) also need to set
1499 * the physical buffer address in the NIC ring.
1500 * netmap_idx_n2k() maps a nic index, i, into the corresponding
1501 * netmap slot index, si
1502 */
1503 int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i);
1504 netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i],
1505 NMB(na, slot + si));
1506 }
1507 return (1);
1508 }
1509
1510 static int
iflib_netmap_rxq_init(if_ctx_t ctx,iflib_rxq_t rxq)1511 iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
1512 {
1513 struct netmap_adapter *na = NA(ctx->ifc_ifp);
1514 struct netmap_kring *kring;
1515 struct netmap_slot *slot;
1516
1517 slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1518 if (slot == NULL)
1519 return (0);
1520 kring = na->rx_rings[rxq->ifr_id];
1521 netmap_fl_refill(rxq, kring, true);
1522 return (1);
1523 }
1524
1525 static void
iflib_netmap_timer(void * arg)1526 iflib_netmap_timer(void *arg)
1527 {
1528 iflib_txq_t txq = arg;
1529 if_ctx_t ctx = txq->ift_ctx;
1530
1531 /*
1532 * Wake up the netmap application, to give it a chance to
1533 * call txsync and reclaim more completed TX buffers.
1534 */
1535 netmap_tx_irq(ctx->ifc_ifp, txq->ift_id);
1536 }
1537
1538 #define iflib_netmap_detach(ifp) netmap_detach(ifp)
1539
1540 #else
1541 #define iflib_netmap_txq_init(ctx, txq) (0)
1542 #define iflib_netmap_rxq_init(ctx, rxq) (0)
1543 #define iflib_netmap_detach(ifp)
1544 #define netmap_enable_all_rings(ifp)
1545 #define netmap_disable_all_rings(ifp)
1546
1547 #define iflib_netmap_attach(ctx) (0)
1548 #define netmap_rx_irq(ifp, qid, budget) (0)
1549 #endif
1550
1551 #if defined(__i386__) || defined(__amd64__)
1552 static __inline void
prefetch(void * x)1553 prefetch(void *x)
1554 {
1555 __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1556 }
1557
1558 static __inline void
prefetch2cachelines(void * x)1559 prefetch2cachelines(void *x)
1560 {
1561 __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1562 #if (CACHE_LINE_SIZE < 128)
1563 __asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x) + CACHE_LINE_SIZE / (sizeof(unsigned long)))));
1564 #endif
1565 }
1566 #else
1567 static __inline void
prefetch(void * x)1568 prefetch(void *x)
1569 {
1570 }
1571
1572 static __inline void
prefetch2cachelines(void * x)1573 prefetch2cachelines(void *x)
1574 {
1575 }
1576 #endif
1577
1578 static void
iru_init(if_rxd_update_t iru,iflib_rxq_t rxq,uint8_t flid)1579 iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid)
1580 {
1581 iflib_fl_t fl;
1582
1583 fl = &rxq->ifr_fl[flid];
1584 iru->iru_paddrs = fl->ifl_bus_addrs;
1585 iru->iru_idxs = fl->ifl_rxd_idxs;
1586 iru->iru_qsidx = rxq->ifr_id;
1587 iru->iru_buf_size = fl->ifl_buf_size;
1588 iru->iru_flidx = fl->ifl_id;
1589 }
1590
1591 static void
_iflib_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nseg,int err)1592 _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
1593 {
1594 if (err)
1595 return;
1596 *(bus_addr_t *) arg = segs[0].ds_addr;
1597 }
1598
1599 #define DMA_WIDTH_TO_BUS_LOWADDR(width) \
1600 (((width) == 0) || (width) == flsll(BUS_SPACE_MAXADDR) ? \
1601 BUS_SPACE_MAXADDR : (1ULL << (width)) - 1ULL)
1602
1603 int
iflib_dma_alloc_align(if_ctx_t ctx,int size,int align,iflib_dma_info_t dma,int mapflags)1604 iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags)
1605 {
1606 int err;
1607 device_t dev = ctx->ifc_dev;
1608 bus_addr_t lowaddr;
1609
1610 lowaddr = DMA_WIDTH_TO_BUS_LOWADDR(ctx->ifc_softc_ctx.isc_dma_width);
1611
1612 err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
1613 align, 0, /* alignment, bounds */
1614 lowaddr, /* lowaddr */
1615 BUS_SPACE_MAXADDR, /* highaddr */
1616 NULL, NULL, /* filter, filterarg */
1617 size, /* maxsize */
1618 1, /* nsegments */
1619 size, /* maxsegsize */
1620 BUS_DMA_ALLOCNOW, /* flags */
1621 NULL, /* lockfunc */
1622 NULL, /* lockarg */
1623 &dma->idi_tag);
1624 if (err) {
1625 device_printf(dev,
1626 "%s: bus_dma_tag_create failed: %d (size=%d, align=%d)\n",
1627 __func__, err, size, align);
1628 goto fail_0;
1629 }
1630
1631 err = bus_dmamem_alloc(dma->idi_tag, (void **)&dma->idi_vaddr,
1632 BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
1633 if (err) {
1634 device_printf(dev,
1635 "%s: bus_dmamem_alloc(%ju) failed: %d\n",
1636 __func__, (uintmax_t)size, err);
1637 goto fail_1;
1638 }
1639
1640 dma->idi_paddr = IF_BAD_DMA;
1641 err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
1642 size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
1643 if (err || dma->idi_paddr == IF_BAD_DMA) {
1644 device_printf(dev,
1645 "%s: bus_dmamap_load failed: %d\n",
1646 __func__, err);
1647 goto fail_2;
1648 }
1649
1650 dma->idi_size = size;
1651 return (0);
1652
1653 fail_2:
1654 bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
1655 fail_1:
1656 bus_dma_tag_destroy(dma->idi_tag);
1657 fail_0:
1658 dma->idi_tag = NULL;
1659
1660 return (err);
1661 }
1662
1663 int
iflib_dma_alloc(if_ctx_t ctx,int size,iflib_dma_info_t dma,int mapflags)1664 iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
1665 {
1666 if_shared_ctx_t sctx = ctx->ifc_sctx;
1667
1668 KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
1669
1670 return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags));
1671 }
1672
1673 int
iflib_dma_alloc_multi(if_ctx_t ctx,int * sizes,iflib_dma_info_t * dmalist,int mapflags,int count)1674 iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
1675 {
1676 int i, err;
1677 iflib_dma_info_t *dmaiter;
1678
1679 dmaiter = dmalist;
1680 for (i = 0; i < count; i++, dmaiter++) {
1681 if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
1682 break;
1683 }
1684 if (err)
1685 iflib_dma_free_multi(dmalist, i);
1686 return (err);
1687 }
1688
1689 void
iflib_dma_free(iflib_dma_info_t dma)1690 iflib_dma_free(iflib_dma_info_t dma)
1691 {
1692 if (dma->idi_tag == NULL)
1693 return;
1694 if (dma->idi_paddr != IF_BAD_DMA) {
1695 bus_dmamap_sync(dma->idi_tag, dma->idi_map,
1696 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1697 bus_dmamap_unload(dma->idi_tag, dma->idi_map);
1698 dma->idi_paddr = IF_BAD_DMA;
1699 }
1700 if (dma->idi_vaddr != NULL) {
1701 bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
1702 dma->idi_vaddr = NULL;
1703 }
1704 bus_dma_tag_destroy(dma->idi_tag);
1705 dma->idi_tag = NULL;
1706 }
1707
1708 void
iflib_dma_free_multi(iflib_dma_info_t * dmalist,int count)1709 iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
1710 {
1711 int i;
1712 iflib_dma_info_t *dmaiter = dmalist;
1713
1714 for (i = 0; i < count; i++, dmaiter++)
1715 iflib_dma_free(*dmaiter);
1716 }
1717
1718 static int
iflib_fast_intr(void * arg)1719 iflib_fast_intr(void *arg)
1720 {
1721 iflib_filter_info_t info = arg;
1722 struct grouptask *gtask = info->ifi_task;
1723 int result;
1724
1725 DBG_COUNTER_INC(fast_intrs);
1726 if (info->ifi_filter != NULL) {
1727 result = info->ifi_filter(info->ifi_filter_arg);
1728 if ((result & FILTER_SCHEDULE_THREAD) == 0)
1729 return (result);
1730 }
1731
1732 GROUPTASK_ENQUEUE(gtask);
1733 return (FILTER_HANDLED);
1734 }
1735
1736 static int
iflib_fast_intr_rxtx(void * arg)1737 iflib_fast_intr_rxtx(void *arg)
1738 {
1739 iflib_filter_info_t info = arg;
1740 struct grouptask *gtask = info->ifi_task;
1741 if_ctx_t ctx;
1742 iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
1743 iflib_txq_t txq;
1744 void *sc;
1745 int i, cidx, result;
1746 qidx_t txqid;
1747 bool intr_enable, intr_legacy;
1748
1749 DBG_COUNTER_INC(fast_intrs);
1750 if (info->ifi_filter != NULL) {
1751 result = info->ifi_filter(info->ifi_filter_arg);
1752 if ((result & FILTER_SCHEDULE_THREAD) == 0)
1753 return (result);
1754 }
1755
1756 ctx = rxq->ifr_ctx;
1757 sc = ctx->ifc_softc;
1758 intr_enable = false;
1759 intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY);
1760 MPASS(rxq->ifr_ntxqirq);
1761 for (i = 0; i < rxq->ifr_ntxqirq; i++) {
1762 txqid = rxq->ifr_txqid[i];
1763 txq = &ctx->ifc_txqs[txqid];
1764 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
1765 BUS_DMASYNC_POSTREAD);
1766 if (!ctx->isc_txd_credits_update(sc, txqid, false)) {
1767 if (intr_legacy)
1768 intr_enable = true;
1769 else
1770 IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
1771 continue;
1772 }
1773 GROUPTASK_ENQUEUE(&txq->ift_task);
1774 }
1775 if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
1776 cidx = rxq->ifr_cq_cidx;
1777 else
1778 cidx = rxq->ifr_fl[0].ifl_cidx;
1779 if (iflib_rxd_avail(ctx, rxq, cidx, 1))
1780 GROUPTASK_ENQUEUE(gtask);
1781 else {
1782 if (intr_legacy)
1783 intr_enable = true;
1784 else
1785 IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
1786 DBG_COUNTER_INC(rx_intr_enables);
1787 }
1788 if (intr_enable)
1789 IFDI_INTR_ENABLE(ctx);
1790 return (FILTER_HANDLED);
1791 }
1792
1793 static int
iflib_fast_intr_ctx(void * arg)1794 iflib_fast_intr_ctx(void *arg)
1795 {
1796 iflib_filter_info_t info = arg;
1797 if_ctx_t ctx = info->ifi_ctx;
1798 int result;
1799
1800 DBG_COUNTER_INC(fast_intrs);
1801 if (info->ifi_filter != NULL) {
1802 result = info->ifi_filter(info->ifi_filter_arg);
1803 if ((result & FILTER_SCHEDULE_THREAD) == 0)
1804 return (result);
1805 }
1806
1807 taskqueue_enqueue(ctx->ifc_tq, &ctx->ifc_admin_task);
1808 return (FILTER_HANDLED);
1809 }
1810
1811 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)1812 _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
1813 driver_filter_t filter, driver_intr_t handler, void *arg,
1814 const char *name)
1815 {
1816 struct resource *res;
1817 void *tag = NULL;
1818 device_t dev = ctx->ifc_dev;
1819 int flags, i, rc;
1820
1821 flags = RF_ACTIVE;
1822 if (ctx->ifc_flags & IFC_LEGACY)
1823 flags |= RF_SHAREABLE;
1824 MPASS(rid < 512);
1825 i = rid;
1826 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, flags);
1827 if (res == NULL) {
1828 device_printf(dev,
1829 "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
1830 return (ENOMEM);
1831 }
1832 irq->ii_res = res;
1833 KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
1834 rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
1835 filter, handler, arg, &tag);
1836 if (rc != 0) {
1837 device_printf(dev,
1838 "failed to setup interrupt for rid %d, name %s: %d\n",
1839 rid, name ? name : "unknown", rc);
1840 return (rc);
1841 } else if (name)
1842 bus_describe_intr(dev, res, tag, "%s", name);
1843
1844 irq->ii_tag = tag;
1845 return (0);
1846 }
1847
1848 /*********************************************************************
1849 *
1850 * Allocate DMA resources for TX buffers as well as memory for the TX
1851 * mbuf map. TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a
1852 * iflib_sw_tx_desc_array structure, storing all the information that
1853 * is needed to transmit a packet on the wire. This is called only
1854 * once at attach, setup is done every reset.
1855 *
1856 **********************************************************************/
1857 static int
iflib_txsd_alloc(iflib_txq_t txq)1858 iflib_txsd_alloc(iflib_txq_t txq)
1859 {
1860 if_ctx_t ctx = txq->ift_ctx;
1861 if_shared_ctx_t sctx = ctx->ifc_sctx;
1862 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1863 device_t dev = ctx->ifc_dev;
1864 bus_size_t tsomaxsize;
1865 bus_addr_t lowaddr;
1866 int err, nsegments, ntsosegments;
1867 bool tso;
1868
1869 nsegments = scctx->isc_tx_nsegments;
1870 ntsosegments = scctx->isc_tx_tso_segments_max;
1871 tsomaxsize = scctx->isc_tx_tso_size_max;
1872 if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU)
1873 tsomaxsize += sizeof(struct ether_vlan_header);
1874 MPASS(scctx->isc_ntxd[0] > 0);
1875 MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
1876 MPASS(nsegments > 0);
1877 if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) {
1878 MPASS(ntsosegments > 0);
1879 MPASS(sctx->isc_tso_maxsize >= tsomaxsize);
1880 }
1881
1882 lowaddr = DMA_WIDTH_TO_BUS_LOWADDR(scctx->isc_dma_width);
1883
1884 /*
1885 * Set up DMA tags for TX buffers.
1886 */
1887 if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
1888 1, 0, /* alignment, bounds */
1889 lowaddr, /* lowaddr */
1890 BUS_SPACE_MAXADDR, /* highaddr */
1891 NULL, NULL, /* filter, filterarg */
1892 sctx->isc_tx_maxsize, /* maxsize */
1893 nsegments, /* nsegments */
1894 sctx->isc_tx_maxsegsize, /* maxsegsize */
1895 0, /* flags */
1896 NULL, /* lockfunc */
1897 NULL, /* lockfuncarg */
1898 &txq->ift_buf_tag))) {
1899 device_printf(dev, "Unable to allocate TX DMA tag: %d\n", err);
1900 device_printf(dev, "maxsize: %ju nsegments: %d maxsegsize: %ju\n",
1901 (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
1902 goto fail;
1903 }
1904 tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0;
1905 if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev),
1906 1, 0, /* alignment, bounds */
1907 lowaddr, /* lowaddr */
1908 BUS_SPACE_MAXADDR, /* highaddr */
1909 NULL, NULL, /* filter, filterarg */
1910 tsomaxsize, /* maxsize */
1911 ntsosegments, /* nsegments */
1912 sctx->isc_tso_maxsegsize, /* maxsegsize */
1913 0, /* flags */
1914 NULL, /* lockfunc */
1915 NULL, /* lockfuncarg */
1916 &txq->ift_tso_buf_tag))) {
1917 device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n",
1918 err);
1919 goto fail;
1920 }
1921
1922 /* Allocate memory for the TX mbuf map. */
1923 if (!(txq->ift_sds.ifsd_m =
1924 (struct mbuf **) malloc(sizeof(struct mbuf *) *
1925 scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1926 device_printf(dev, "Unable to allocate TX mbuf map memory\n");
1927 err = ENOMEM;
1928 goto fail;
1929 }
1930 if (ctx->ifc_sysctl_simple_tx) {
1931 if (!(txq->ift_sds.ifsd_m_defer =
1932 (struct mbuf **) malloc(sizeof(struct mbuf *) *
1933 scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1934 device_printf(dev, "Unable to allocate TX mbuf map memory\n");
1935 err = ENOMEM;
1936 goto fail;
1937 }
1938 }
1939 txq->ift_sds.ifsd_m_deferb = txq->ift_sds.ifsd_m_defer;
1940 /*
1941 * Create the DMA maps for TX buffers.
1942 */
1943 if ((txq->ift_sds.ifsd_map = (bus_dmamap_t *)malloc(
1944 sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
1945 M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
1946 device_printf(dev,
1947 "Unable to allocate TX buffer DMA map memory\n");
1948 err = ENOMEM;
1949 goto fail;
1950 }
1951 if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc(
1952 sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
1953 M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
1954 device_printf(dev,
1955 "Unable to allocate TSO TX buffer map memory\n");
1956 err = ENOMEM;
1957 goto fail;
1958 }
1959 for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
1960 err = bus_dmamap_create(txq->ift_buf_tag, 0,
1961 &txq->ift_sds.ifsd_map[i]);
1962 if (err != 0) {
1963 device_printf(dev, "Unable to create TX DMA map\n");
1964 goto fail;
1965 }
1966 if (!tso)
1967 continue;
1968 err = bus_dmamap_create(txq->ift_tso_buf_tag, 0,
1969 &txq->ift_sds.ifsd_tso_map[i]);
1970 if (err != 0) {
1971 device_printf(dev, "Unable to create TSO TX DMA map\n");
1972 goto fail;
1973 }
1974 }
1975 return (0);
1976 fail:
1977 /* We free all, it handles case where we are in the middle */
1978 iflib_tx_structures_free(ctx);
1979 return (err);
1980 }
1981
1982 static void
iflib_txsd_destroy(if_ctx_t ctx,iflib_txq_t txq,int i)1983 iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
1984 {
1985 bus_dmamap_t map;
1986
1987 if (txq->ift_sds.ifsd_map != NULL) {
1988 map = txq->ift_sds.ifsd_map[i];
1989 bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE);
1990 bus_dmamap_unload(txq->ift_buf_tag, map);
1991 bus_dmamap_destroy(txq->ift_buf_tag, map);
1992 txq->ift_sds.ifsd_map[i] = NULL;
1993 }
1994
1995 if (txq->ift_sds.ifsd_tso_map != NULL) {
1996 map = txq->ift_sds.ifsd_tso_map[i];
1997 bus_dmamap_sync(txq->ift_tso_buf_tag, map,
1998 BUS_DMASYNC_POSTWRITE);
1999 bus_dmamap_unload(txq->ift_tso_buf_tag, map);
2000 bus_dmamap_destroy(txq->ift_tso_buf_tag, map);
2001 txq->ift_sds.ifsd_tso_map[i] = NULL;
2002 }
2003 }
2004
2005 static void
iflib_txq_destroy(iflib_txq_t txq)2006 iflib_txq_destroy(iflib_txq_t txq)
2007 {
2008 if_ctx_t ctx = txq->ift_ctx;
2009
2010 for (int i = 0; i < txq->ift_size; i++)
2011 iflib_txsd_destroy(ctx, txq, i);
2012
2013 if (txq->ift_br != NULL) {
2014 ifmp_ring_free(txq->ift_br);
2015 txq->ift_br = NULL;
2016 }
2017
2018 /* Free any mbufs stranded in the deferral ring */
2019 if (txq->ift_drbr != NULL) {
2020 mtx_lock(&txq->ift_mtx);
2021 drbr_flush(NULL, txq->ift_drbr);
2022 mtx_unlock(&txq->ift_mtx);
2023 buf_ring_free(txq->ift_drbr, M_IFLIB);
2024 txq->ift_drbr = NULL;
2025 }
2026 if (txq->ift_drbr_deferred != NULL) {
2027 counter_u64_free(txq->ift_drbr_deferred);
2028 txq->ift_drbr_deferred = NULL;
2029 }
2030 if (txq->ift_drbr_blocked != NULL) {
2031 counter_u64_free(txq->ift_drbr_blocked);
2032 txq->ift_drbr_blocked = NULL;
2033 }
2034 if (txq->ift_drbr_remote != NULL) {
2035 counter_u64_free(txq->ift_drbr_remote);
2036 txq->ift_drbr_remote = NULL;
2037 }
2038 if (txq->ift_drbr_drops != NULL) {
2039 counter_u64_free(txq->ift_drbr_drops);
2040 txq->ift_drbr_drops = NULL;
2041 }
2042
2043 mtx_destroy(&txq->ift_mtx);
2044
2045 if (txq->ift_sds.ifsd_map != NULL) {
2046 free(txq->ift_sds.ifsd_map, M_IFLIB);
2047 txq->ift_sds.ifsd_map = NULL;
2048 }
2049 if (txq->ift_sds.ifsd_tso_map != NULL) {
2050 free(txq->ift_sds.ifsd_tso_map, M_IFLIB);
2051 txq->ift_sds.ifsd_tso_map = NULL;
2052 }
2053 if (txq->ift_sds.ifsd_m != NULL) {
2054 free(txq->ift_sds.ifsd_m, M_IFLIB);
2055 txq->ift_sds.ifsd_m = NULL;
2056 }
2057 if (txq->ift_sds.ifsd_m_defer != NULL) {
2058 free(txq->ift_sds.ifsd_m_defer, M_IFLIB);
2059 txq->ift_sds.ifsd_m_defer = NULL;
2060 }
2061 if (txq->ift_buf_tag != NULL) {
2062 bus_dma_tag_destroy(txq->ift_buf_tag);
2063 txq->ift_buf_tag = NULL;
2064 }
2065 if (txq->ift_tso_buf_tag != NULL) {
2066 bus_dma_tag_destroy(txq->ift_tso_buf_tag);
2067 txq->ift_tso_buf_tag = NULL;
2068 }
2069 if (txq->ift_ifdi != NULL) {
2070 free(txq->ift_ifdi, M_IFLIB);
2071 }
2072 }
2073
2074 static void
iflib_txsd_free(if_ctx_t ctx,iflib_txq_t txq,int i)2075 iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
2076 {
2077 struct mbuf *m;
2078
2079 m = IFLIB_GET_MBUF(txq->ift_sds.ifsd_m[i]);
2080 if (m == NULL)
2081 return;
2082
2083 if (txq->ift_sds.ifsd_map != NULL) {
2084 bus_dmamap_sync(txq->ift_buf_tag,
2085 txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE);
2086 bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]);
2087 }
2088 if (txq->ift_sds.ifsd_tso_map != NULL) {
2089 bus_dmamap_sync(txq->ift_tso_buf_tag,
2090 txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE);
2091 bus_dmamap_unload(txq->ift_tso_buf_tag,
2092 txq->ift_sds.ifsd_tso_map[i]);
2093 }
2094 txq->ift_sds.ifsd_m[i] = NULL;
2095 m_freem(m);
2096 DBG_COUNTER_INC(tx_frees);
2097 }
2098
2099 static int
iflib_txq_setup(iflib_txq_t txq)2100 iflib_txq_setup(iflib_txq_t txq)
2101 {
2102 if_ctx_t ctx = txq->ift_ctx;
2103 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2104 if_shared_ctx_t sctx = ctx->ifc_sctx;
2105 iflib_dma_info_t di;
2106 int i;
2107
2108 /* XXX make configurable */
2109 txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
2110
2111 /* Reset indices */
2112 txq->ift_cidx_processed = 0;
2113 txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
2114 txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
2115 txq->ift_pad = scctx->isc_tx_pad;
2116
2117 for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
2118 bzero((void *)di->idi_vaddr, di->idi_size);
2119
2120 IFDI_TXQ_SETUP(ctx, txq->ift_id);
2121 for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
2122 bus_dmamap_sync(di->idi_tag, di->idi_map,
2123 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2124 return (0);
2125 }
2126
2127 /*********************************************************************
2128 *
2129 * Allocate DMA resources for RX buffers as well as memory for the RX
2130 * mbuf map, direct RX cluster pointer map and RX cluster bus address
2131 * map. RX DMA map, RX mbuf map, direct RX cluster pointer map and
2132 * RX cluster map are kept in a iflib_sw_rx_desc_array structure.
2133 * Since we use use one entry in iflib_sw_rx_desc_array per received
2134 * packet, the maximum number of entries we'll need is equal to the
2135 * number of hardware receive descriptors that we've allocated.
2136 *
2137 **********************************************************************/
2138 static int
iflib_rxsd_alloc(iflib_rxq_t rxq)2139 iflib_rxsd_alloc(iflib_rxq_t rxq)
2140 {
2141 if_ctx_t ctx = rxq->ifr_ctx;
2142 if_shared_ctx_t sctx = ctx->ifc_sctx;
2143 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2144 device_t dev = ctx->ifc_dev;
2145 iflib_fl_t fl;
2146 bus_addr_t lowaddr;
2147 int err;
2148
2149 MPASS(scctx->isc_nrxd[0] > 0);
2150 MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
2151
2152 lowaddr = DMA_WIDTH_TO_BUS_LOWADDR(scctx->isc_dma_width);
2153
2154 fl = rxq->ifr_fl;
2155 for (int i = 0; i < rxq->ifr_nfl; i++, fl++) {
2156 fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
2157 /* Set up DMA tag for RX buffers. */
2158 err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
2159 1, 0, /* alignment, bounds */
2160 lowaddr, /* lowaddr */
2161 BUS_SPACE_MAXADDR, /* highaddr */
2162 NULL, NULL, /* filter, filterarg */
2163 sctx->isc_rx_maxsize, /* maxsize */
2164 sctx->isc_rx_nsegments, /* nsegments */
2165 sctx->isc_rx_maxsegsize, /* maxsegsize */
2166 0, /* flags */
2167 NULL, /* lockfunc */
2168 NULL, /* lockarg */
2169 &fl->ifl_buf_tag);
2170 if (err) {
2171 device_printf(dev,
2172 "Unable to allocate RX DMA tag: %d\n", err);
2173 goto fail;
2174 }
2175
2176 /* Allocate memory for the RX mbuf map. */
2177 if (!(fl->ifl_sds.ifsd_m =
2178 (struct mbuf **) malloc(sizeof(struct mbuf *) *
2179 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
2180 device_printf(dev,
2181 "Unable to allocate RX mbuf map memory\n");
2182 err = ENOMEM;
2183 goto fail;
2184 }
2185
2186 /* Allocate memory for the direct RX cluster pointer map. */
2187 if (!(fl->ifl_sds.ifsd_cl =
2188 (caddr_t *) malloc(sizeof(caddr_t) *
2189 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
2190 device_printf(dev,
2191 "Unable to allocate RX cluster map memory\n");
2192 err = ENOMEM;
2193 goto fail;
2194 }
2195
2196 /* Allocate memory for the RX cluster bus address map. */
2197 if (!(fl->ifl_sds.ifsd_ba =
2198 (bus_addr_t *) malloc(sizeof(bus_addr_t) *
2199 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
2200 device_printf(dev,
2201 "Unable to allocate RX bus address map memory\n");
2202 err = ENOMEM;
2203 goto fail;
2204 }
2205
2206 /*
2207 * Create the DMA maps for RX buffers.
2208 */
2209 if (!(fl->ifl_sds.ifsd_map =
2210 (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
2211 device_printf(dev,
2212 "Unable to allocate RX buffer DMA map memory\n");
2213 err = ENOMEM;
2214 goto fail;
2215 }
2216 for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
2217 err = bus_dmamap_create(fl->ifl_buf_tag, 0,
2218 &fl->ifl_sds.ifsd_map[i]);
2219 if (err != 0) {
2220 device_printf(dev, "Unable to create RX buffer DMA map\n");
2221 goto fail;
2222 }
2223 }
2224 }
2225 return (0);
2226
2227 fail:
2228 iflib_rx_structures_free(ctx);
2229 return (err);
2230 }
2231
2232 /*
2233 * Internal service routines
2234 */
2235
2236 struct rxq_refill_cb_arg {
2237 int error;
2238 bus_dma_segment_t seg;
2239 int nseg;
2240 };
2241
2242 static void
_rxq_refill_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)2243 _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2244 {
2245 struct rxq_refill_cb_arg *cb_arg = arg;
2246
2247 cb_arg->error = error;
2248 cb_arg->seg = segs[0];
2249 cb_arg->nseg = nseg;
2250 }
2251
2252 /**
2253 * iflib_fl_refill - refill an rxq free-buffer list
2254 * @ctx: the iflib context
2255 * @fl: the free list to refill
2256 * @count: the number of new buffers to allocate
2257 *
2258 * (Re)populate an rxq free-buffer list with up to @count new packet buffers.
2259 * The caller must assure that @count does not exceed the queue's capacity
2260 * minus one (since we always leave a descriptor unavailable).
2261 */
2262 static uint8_t
iflib_fl_refill(if_ctx_t ctx,iflib_fl_t fl,int count)2263 iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
2264 {
2265 struct if_rxd_update iru;
2266 struct rxq_refill_cb_arg cb_arg;
2267 struct mbuf *m;
2268 caddr_t cl, *sd_cl;
2269 struct mbuf **sd_m;
2270 bus_dmamap_t *sd_map;
2271 bus_addr_t bus_addr, *sd_ba;
2272 int err, frag_idx, i, idx, n, pidx;
2273 qidx_t credits;
2274
2275 MPASS(count <= fl->ifl_size - fl->ifl_credits - 1);
2276
2277 sd_m = fl->ifl_sds.ifsd_m;
2278 sd_map = fl->ifl_sds.ifsd_map;
2279 sd_cl = fl->ifl_sds.ifsd_cl;
2280 sd_ba = fl->ifl_sds.ifsd_ba;
2281 pidx = fl->ifl_pidx;
2282 idx = pidx;
2283 frag_idx = fl->ifl_fragidx;
2284 credits = fl->ifl_credits;
2285
2286 i = 0;
2287 n = count;
2288 MPASS(n > 0);
2289 MPASS(credits + n <= fl->ifl_size);
2290
2291 if (pidx < fl->ifl_cidx)
2292 MPASS(pidx + n <= fl->ifl_cidx);
2293 if (pidx == fl->ifl_cidx && (credits < fl->ifl_size))
2294 MPASS(fl->ifl_gen == 0);
2295 if (pidx > fl->ifl_cidx)
2296 MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
2297
2298 DBG_COUNTER_INC(fl_refills);
2299 if (n > 8)
2300 DBG_COUNTER_INC(fl_refills_large);
2301 iru_init(&iru, fl->ifl_rxq, fl->ifl_id);
2302 while (n-- > 0) {
2303 /*
2304 * We allocate an uninitialized mbuf + cluster, mbuf is
2305 * initialized after rx.
2306 *
2307 * If the cluster is still set then we know a minimum sized
2308 * packet was received
2309 */
2310 bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,
2311 &frag_idx);
2312 if (frag_idx < 0)
2313 bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
2314 MPASS(frag_idx >= 0);
2315 if ((cl = sd_cl[frag_idx]) == NULL) {
2316 cl = uma_zalloc(fl->ifl_zone, M_NOWAIT);
2317 if (__predict_false(cl == NULL))
2318 break;
2319
2320 cb_arg.error = 0;
2321 MPASS(sd_map != NULL);
2322 err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx],
2323 cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg,
2324 BUS_DMA_NOWAIT);
2325 if (__predict_false(err != 0 || cb_arg.error)) {
2326 uma_zfree(fl->ifl_zone, cl);
2327 break;
2328 }
2329
2330 sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr;
2331 sd_cl[frag_idx] = cl;
2332 #if MEMORY_LOGGING
2333 fl->ifl_cl_enqueued++;
2334 #endif
2335 } else {
2336 bus_addr = sd_ba[frag_idx];
2337 }
2338 bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx],
2339 BUS_DMASYNC_PREREAD);
2340
2341 if (sd_m[frag_idx] == NULL) {
2342 m = m_gethdr_raw(M_NOWAIT, 0);
2343 if (__predict_false(m == NULL))
2344 break;
2345 sd_m[frag_idx] = m;
2346 }
2347 bit_set(fl->ifl_rx_bitmap, frag_idx);
2348 #if MEMORY_LOGGING
2349 fl->ifl_m_enqueued++;
2350 #endif
2351
2352 DBG_COUNTER_INC(rx_allocs);
2353 fl->ifl_rxd_idxs[i] = frag_idx;
2354 fl->ifl_bus_addrs[i] = bus_addr;
2355 credits++;
2356 i++;
2357 MPASS(credits <= fl->ifl_size);
2358 if (++idx == fl->ifl_size) {
2359 #ifdef INVARIANTS
2360 fl->ifl_gen = 1;
2361 #endif
2362 idx = 0;
2363 }
2364 if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
2365 iru.iru_pidx = pidx;
2366 iru.iru_count = i;
2367 ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
2368 fl->ifl_pidx = idx;
2369 fl->ifl_credits = credits;
2370 pidx = idx;
2371 i = 0;
2372 }
2373 }
2374
2375 if (n < count - 1) {
2376 if (i != 0) {
2377 iru.iru_pidx = pidx;
2378 iru.iru_count = i;
2379 ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
2380 fl->ifl_pidx = idx;
2381 fl->ifl_credits = credits;
2382 }
2383 DBG_COUNTER_INC(rxd_flush);
2384 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
2385 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2386 ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id,
2387 fl->ifl_id, fl->ifl_pidx);
2388 if (__predict_true(bit_test(fl->ifl_rx_bitmap, frag_idx))) {
2389 fl->ifl_fragidx = frag_idx + 1;
2390 if (fl->ifl_fragidx == fl->ifl_size)
2391 fl->ifl_fragidx = 0;
2392 } else {
2393 fl->ifl_fragidx = frag_idx;
2394 }
2395 }
2396
2397 return (n == -1 ? 0 : IFLIB_RXEOF_EMPTY);
2398 }
2399
2400 static inline uint8_t
iflib_fl_refill_all(if_ctx_t ctx,iflib_fl_t fl)2401 iflib_fl_refill_all(if_ctx_t ctx, iflib_fl_t fl)
2402 {
2403 /*
2404 * We leave an unused descriptor to avoid pidx to catch up with cidx.
2405 * This is important as it confuses most NICs. For instance,
2406 * Intel NICs have (per receive ring) RDH and RDT registers, where
2407 * RDH points to the next receive descriptor to be used by the NIC,
2408 * and RDT for the next receive descriptor to be published by the
2409 * driver to the NIC (RDT - 1 is thus the last valid one).
2410 * The condition RDH == RDT means no descriptors are available to
2411 * the NIC, and thus it would be ambiguous if it also meant that
2412 * all the descriptors are available to the NIC.
2413 */
2414 int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
2415 #ifdef INVARIANTS
2416 int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
2417 #endif
2418
2419 MPASS(fl->ifl_credits <= fl->ifl_size);
2420 MPASS(reclaimable == delta);
2421
2422 if (reclaimable > 0)
2423 return (iflib_fl_refill(ctx, fl, reclaimable));
2424 return (0);
2425 }
2426
2427 uint8_t
iflib_in_detach(if_ctx_t ctx)2428 iflib_in_detach(if_ctx_t ctx)
2429 {
2430 bool in_detach;
2431
2432 STATE_LOCK(ctx);
2433 in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH);
2434 STATE_UNLOCK(ctx);
2435 return (in_detach);
2436 }
2437
2438 static void
iflib_fl_bufs_free(iflib_fl_t fl)2439 iflib_fl_bufs_free(iflib_fl_t fl)
2440 {
2441 iflib_dma_info_t idi = fl->ifl_ifdi;
2442 bus_dmamap_t sd_map;
2443 uint32_t i;
2444
2445 for (i = 0; i < fl->ifl_size; i++) {
2446 struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
2447 caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
2448
2449 if (*sd_cl != NULL) {
2450 sd_map = fl->ifl_sds.ifsd_map[i];
2451 bus_dmamap_sync(fl->ifl_buf_tag, sd_map,
2452 BUS_DMASYNC_POSTREAD);
2453 bus_dmamap_unload(fl->ifl_buf_tag, sd_map);
2454 uma_zfree(fl->ifl_zone, *sd_cl);
2455 *sd_cl = NULL;
2456 if (*sd_m != NULL) {
2457 m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
2458 m_free_raw(*sd_m);
2459 *sd_m = NULL;
2460 }
2461 } else {
2462 MPASS(*sd_m == NULL);
2463 }
2464 #if MEMORY_LOGGING
2465 fl->ifl_m_dequeued++;
2466 fl->ifl_cl_dequeued++;
2467 #endif
2468 }
2469 #ifdef INVARIANTS
2470 for (i = 0; i < fl->ifl_size; i++) {
2471 MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
2472 MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
2473 }
2474 #endif
2475 /*
2476 * Reset free list values
2477 */
2478 fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
2479 bzero(idi->idi_vaddr, idi->idi_size);
2480 }
2481
2482 /*********************************************************************
2483 *
2484 * Initialize a free list and its buffers.
2485 *
2486 **********************************************************************/
2487 static int
iflib_fl_setup(iflib_fl_t fl)2488 iflib_fl_setup(iflib_fl_t fl)
2489 {
2490 iflib_rxq_t rxq = fl->ifl_rxq;
2491 if_ctx_t ctx = rxq->ifr_ctx;
2492 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2493 int qidx;
2494
2495 bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1);
2496 /*
2497 * Free current RX buffer structs and their mbufs
2498 */
2499 iflib_fl_bufs_free(fl);
2500 /* Now replenish the mbufs */
2501 MPASS(fl->ifl_credits == 0);
2502 qidx = rxq->ifr_fl_offset + fl->ifl_id;
2503 if (scctx->isc_rxd_buf_size[qidx] != 0)
2504 fl->ifl_buf_size = scctx->isc_rxd_buf_size[qidx];
2505 else
2506 fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz;
2507 /*
2508 * ifl_buf_size may be a driver-supplied value, so pull it up
2509 * to the selected mbuf size.
2510 */
2511 fl->ifl_buf_size = iflib_get_mbuf_size_for(fl->ifl_buf_size);
2512 if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
2513 ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
2514 fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
2515 fl->ifl_zone = m_getzone(fl->ifl_buf_size);
2516
2517 /*
2518 * Avoid pre-allocating zillions of clusters to an idle card
2519 * potentially speeding up attach. In any case make sure
2520 * to leave a descriptor unavailable. See the comment in
2521 * iflib_fl_refill_all().
2522 */
2523 MPASS(fl->ifl_size > 0);
2524 (void)iflib_fl_refill(ctx, fl, min(128, fl->ifl_size - 1));
2525 if (min(128, fl->ifl_size - 1) != fl->ifl_credits)
2526 return (ENOBUFS);
2527 /*
2528 * handle failure
2529 */
2530 MPASS(rxq != NULL);
2531 MPASS(fl->ifl_ifdi != NULL);
2532 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
2533 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2534 return (0);
2535 }
2536
2537 /*********************************************************************
2538 *
2539 * Free receive ring data structures
2540 *
2541 **********************************************************************/
2542 static void
iflib_rx_sds_free(iflib_rxq_t rxq)2543 iflib_rx_sds_free(iflib_rxq_t rxq)
2544 {
2545 iflib_fl_t fl;
2546 int i, j;
2547
2548 if (rxq->ifr_fl != NULL) {
2549 for (i = 0; i < rxq->ifr_nfl; i++) {
2550 fl = &rxq->ifr_fl[i];
2551 if (fl->ifl_buf_tag != NULL) {
2552 if (fl->ifl_sds.ifsd_map != NULL) {
2553 for (j = 0; j < fl->ifl_size; j++) {
2554 bus_dmamap_sync(
2555 fl->ifl_buf_tag,
2556 fl->ifl_sds.ifsd_map[j],
2557 BUS_DMASYNC_POSTREAD);
2558 bus_dmamap_unload(
2559 fl->ifl_buf_tag,
2560 fl->ifl_sds.ifsd_map[j]);
2561 bus_dmamap_destroy(
2562 fl->ifl_buf_tag,
2563 fl->ifl_sds.ifsd_map[j]);
2564 }
2565 }
2566 bus_dma_tag_destroy(fl->ifl_buf_tag);
2567 fl->ifl_buf_tag = NULL;
2568 }
2569 free(fl->ifl_sds.ifsd_m, M_IFLIB);
2570 free(fl->ifl_sds.ifsd_cl, M_IFLIB);
2571 free(fl->ifl_sds.ifsd_ba, M_IFLIB);
2572 free(fl->ifl_sds.ifsd_map, M_IFLIB);
2573 free(fl->ifl_rx_bitmap, M_IFLIB);
2574 fl->ifl_sds.ifsd_m = NULL;
2575 fl->ifl_sds.ifsd_cl = NULL;
2576 fl->ifl_sds.ifsd_ba = NULL;
2577 fl->ifl_sds.ifsd_map = NULL;
2578 fl->ifl_rx_bitmap = NULL;
2579 }
2580 free(rxq->ifr_fl, M_IFLIB);
2581 rxq->ifr_fl = NULL;
2582 free(rxq->ifr_ifdi, M_IFLIB);
2583 rxq->ifr_ifdi = NULL;
2584 rxq->ifr_cq_cidx = 0;
2585 }
2586 }
2587
2588 /*
2589 * Serialize admission changes and publication of the legacy driver flags.
2590 * Opening admission publishes queue setup to lockless readers before device
2591 * interrupts are enabled. Closing it does not wait for existing users:
2592 * queue locks, task drains and the driver stop contract still apply.
2593 */
2594 static void
iflib_set_running(if_ctx_t ctx,bool running)2595 iflib_set_running(if_ctx_t ctx, bool running)
2596 {
2597
2598 mtx_assert(&ctx->ifc_state_mtx, MA_OWNED);
2599 if (running) {
2600 if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING,
2601 IFF_DRV_OACTIVE);
2602 atomic_store_rel_int(&ctx->ifc_running, 1);
2603 } else {
2604 atomic_store_rel_int(&ctx->ifc_running, 0);
2605 if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE,
2606 IFF_DRV_RUNNING);
2607 }
2608 }
2609
2610 /*
2611 * Timer routine
2612 */
2613 static void
iflib_timer(void * arg)2614 iflib_timer(void *arg)
2615 {
2616 iflib_txq_t txq = arg;
2617 if_ctx_t ctx = txq->ift_ctx;
2618 if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
2619 uint64_t this_tick = ticks;
2620
2621 if (!iflib_is_running(ctx))
2622 return;
2623
2624 /*
2625 * Check on the state of the TX queue(s); this can be done
2626 * without the lock: the counters the check reads are only
2627 * advanced by the queue's tx task and a stale read just
2628 * delays the verdict by one timer period.
2629 */
2630 if (this_tick - txq->ift_last_timer_tick >= iflib_timer_default) {
2631 qidx_t in_use, outstanding;
2632 bool demand, frozen;
2633
2634 txq->ift_last_timer_tick = this_tick;
2635 IFDI_TIMER(ctx, txq->ift_id);
2636
2637 /*
2638 * Descriptors the hardware has not reported as
2639 * completed: neither harvested as credits
2640 * (ift_processed) nor reclaimed (ift_cleaned accounts
2641 * the difference to ift_in_use). The tail whose
2642 * report-status request is still deferred is never
2643 * reported and must not count.
2644 */
2645 in_use = txq->ift_in_use;
2646 outstanding = in_use -
2647 (qidx_t)(txq->ift_processed - txq->ift_cleaned);
2648
2649 /*
2650 * The queue is frozen while it has descriptors the
2651 * hardware has not reported as completed and none
2652 * were reclaimed over the period; the link must be
2653 * up, with no pause frames and no pending doorbell
2654 * (the laggard check below rings it).
2655 *
2656 * Being frozen is not a fault - the hardware may defer
2657 * marking descriptors as completed indefinitely, and
2658 * 8254x hardware does so for a quiet queue. Continue
2659 * arming only while demand persists: the outstanding
2660 * count grows, the software ring is stalled, or the
2661 * hardware ring has reached iflib's backpressure
2662 * threshold. The last condition covers simple-TX, which
2663 * does not use the software ring. This also prevents one
2664 * mixed lockless counter sample from arming a quiet queue
2665 * until the verdict. Act only once it has stayed frozen
2666 * under demand for
2667 * net.iflib.tx_watchdog_periods consecutive periods.
2668 */
2669 frozen = outstanding > txq->ift_rs_pending &&
2670 txq->ift_processed == txq->ift_processed_prev &&
2671 txq->ift_db_pending == 0 &&
2672 sctx->isc_pause_frames == 0 &&
2673 ctx->ifc_link_state == LINK_STATE_UP;
2674 demand = outstanding > txq->ift_outstanding_prev ||
2675 ifmp_ring_is_stalled(txq->ift_br) ||
2676 in_use + MAX_TX_DESC(ctx) >= txq->ift_size - txq->ift_pad;
2677 if (!frozen || !demand)
2678 txq->ift_wdog_armed = 0;
2679 else {
2680 if (txq->ift_wdog_armed < UINT16_MAX)
2681 txq->ift_wdog_armed++;
2682 }
2683
2684 /*
2685 * Frozen long enough: ask the hardware. Completions
2686 * ready but unharvested for this long mean the
2687 * completion interrupt went missing - kick the
2688 * queue's task. Nothing ready while demand persisted
2689 * means it is hung.
2690 */
2691 if (iflib_tx_watchdog_periods > 0 &&
2692 txq->ift_wdog_armed >= iflib_tx_watchdog_periods) {
2693 bus_dmamap_sync(txq->ift_ifdi->idi_tag,
2694 txq->ift_ifdi->idi_map, BUS_DMASYNC_POSTREAD);
2695 if (ctx->isc_txd_credits_update(ctx->ifc_softc,
2696 txq->ift_id, false) == 0) {
2697 device_printf(ctx->ifc_dev,
2698 "Watchdog timeout (TX: %d desc "
2699 "avail: %d pidx: %d) -- resetting\n",
2700 txq->ift_id, TXQ_AVAIL(txq),
2701 txq->ift_pidx);
2702 STATE_LOCK(ctx);
2703 iflib_set_running(ctx, false);
2704 ctx->ifc_flags |=
2705 (IFC_DO_WATCHDOG | IFC_DO_RESET);
2706 iflib_admin_intr_deferred(ctx);
2707 STATE_UNLOCK(ctx);
2708 return;
2709 }
2710 GROUPTASK_ENQUEUE(&txq->ift_task);
2711 }
2712 txq->ift_outstanding_prev = outstanding;
2713 txq->ift_processed_prev = txq->ift_processed;
2714 }
2715 /* Handle any laggards */
2716 if (txq->ift_db_pending ||
2717 (txq->ift_drbr != NULL &&
2718 (!if_altq_is_enabled(ctx->ifc_ifp) || txq->ift_id == 0) &&
2719 !drbr_empty(ctx->ifc_ifp, txq->ift_drbr)))
2720 GROUPTASK_ENQUEUE(&txq->ift_task);
2721
2722 sctx->isc_pause_frames = 0;
2723 if (iflib_is_running(ctx))
2724 callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer,
2725 txq, txq->ift_timer.c_cpu);
2726 }
2727
2728 static uint16_t
iflib_get_mbuf_size_for(unsigned int size)2729 iflib_get_mbuf_size_for(unsigned int size)
2730 {
2731
2732 if (size <= MCLBYTES)
2733 return (MCLBYTES);
2734 else
2735 return (MJUMPAGESIZE);
2736 }
2737
2738 static void
iflib_calc_rx_mbuf_sz(if_ctx_t ctx)2739 iflib_calc_rx_mbuf_sz(if_ctx_t ctx)
2740 {
2741 if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
2742
2743 /*
2744 * XXX don't set the max_frame_size to larger
2745 * than the hardware can handle
2746 */
2747 ctx->ifc_rx_mbuf_sz =
2748 iflib_get_mbuf_size_for(sctx->isc_max_frame_size);
2749 }
2750
2751 uint32_t
iflib_get_rx_mbuf_sz(if_ctx_t ctx)2752 iflib_get_rx_mbuf_sz(if_ctx_t ctx)
2753 {
2754
2755 return (ctx->ifc_rx_mbuf_sz);
2756 }
2757
2758 static void
iflib_init_locked(if_ctx_t ctx)2759 iflib_init_locked(if_ctx_t ctx)
2760 {
2761 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2762 if_t ifp = ctx->ifc_ifp;
2763 iflib_fl_t fl;
2764 iflib_txq_t txq;
2765 iflib_rxq_t rxq;
2766 int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
2767 bool init_failed;
2768
2769 sx_assert(&ctx->ifc_ctx_sx, SA_XLOCKED);
2770 /* Configuration changes made during suspend take effect on resume. */
2771 if (ctx->ifc_pm_state != IFLIB_PM_ACTIVE)
2772 return;
2773 KASSERT(ctx->ifc_datapath_state == IFLIB_DP_STOPPED,
2774 ("iflib init from datapath state %d", ctx->ifc_datapath_state));
2775 ctx->ifc_datapath_state = IFLIB_DP_STARTING;
2776
2777 STATE_LOCK(ctx);
2778 iflib_set_running(ctx, false);
2779 STATE_UNLOCK(ctx);
2780 IFDI_INTR_DISABLE(ctx);
2781
2782 /*
2783 * See iflib_stop(). Useful in case iflib_init_locked() is
2784 * called without first calling iflib_stop().
2785 */
2786 netmap_disable_all_rings(ifp);
2787
2788 tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
2789 tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
2790 /* Set hardware offload abilities */
2791 if_clearhwassist(ifp);
2792 if (if_getcapenable(ifp) & IFCAP_TXCSUM)
2793 if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
2794 if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
2795 if_sethwassistbits(ifp, tx_ip6_csum_flags, 0);
2796 if (if_getcapenable(ifp) & IFCAP_TSO4)
2797 if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
2798 if (if_getcapenable(ifp) & IFCAP_TSO6)
2799 if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
2800
2801 for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
2802 CALLOUT_LOCK(txq);
2803 callout_stop(&txq->ift_timer);
2804 #ifdef DEV_NETMAP
2805 callout_stop(&txq->ift_netmap_timer);
2806 #endif /* DEV_NETMAP */
2807 CALLOUT_UNLOCK(txq);
2808 (void)iflib_netmap_txq_init(ctx, txq);
2809 }
2810 /*
2811 * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so
2812 * that drivers can use the value when setting up the hardware receive
2813 * buffers.
2814 */
2815 iflib_calc_rx_mbuf_sz(ctx);
2816
2817 #ifdef INVARIANTS
2818 i = if_getdrvflags(ifp);
2819 #endif
2820 STATE_LOCK(ctx);
2821 ctx->ifc_flags &= ~IFC_INIT_FAILED;
2822 STATE_UNLOCK(ctx);
2823 IFDI_INIT(ctx);
2824 MPASS(if_getdrvflags(ifp) == i);
2825 STATE_LOCK(ctx);
2826 init_failed = (ctx->ifc_flags & IFC_INIT_FAILED) != 0;
2827 STATE_UNLOCK(ctx);
2828 if (init_failed) {
2829 /*
2830 * IFDI_INIT failed, but that alone does not prove that the
2831 * driver stopped every queue or fenced DMA. Force the next
2832 * lifecycle transition through the driver's stop method.
2833 */
2834 ctx->ifc_datapath_state = IFLIB_DP_FAILED;
2835 return;
2836 }
2837 for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
2838 if (iflib_netmap_rxq_init(ctx, rxq) > 0) {
2839 /* This rxq is in netmap mode. Skip normal init. */
2840 continue;
2841 }
2842 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
2843 if (iflib_fl_setup(fl)) {
2844 device_printf(ctx->ifc_dev,
2845 "setting up free list %d failed - "
2846 "check cluster settings\n", j);
2847 /*
2848 * IFDI_INIT has started the hardware. Stop it before
2849 * releasing partially populated receive mappings.
2850 */
2851 iflib_init_failed(ctx);
2852 iflib_stop(ctx);
2853 return;
2854 }
2855 }
2856 }
2857 /* Open every TX queue before publishing admission to transmitters. */
2858 if (ctx->ifc_sysctl_simple_tx) {
2859 for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets;
2860 i++, txq++)
2861 atomic_clear_rel_int(&txq->ift_producers,
2862 IFLIB_TXQ_QUIESCING);
2863 }
2864 STATE_LOCK(ctx);
2865 iflib_set_running(ctx, true);
2866 STATE_UNLOCK(ctx);
2867 IFDI_INTR_ENABLE(ctx);
2868 txq = ctx->ifc_txqs;
2869 for (i = 0; i < scctx->isc_ntxqsets; i++, txq++)
2870 callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq,
2871 txq->ift_timer.c_cpu);
2872
2873 /* Re-enable txsync/rxsync. */
2874 netmap_enable_all_rings(ifp);
2875 ctx->ifc_datapath_state = IFLIB_DP_RUNNING;
2876 }
2877
2878 static int
iflib_media_change(if_t ifp)2879 iflib_media_change(if_t ifp)
2880 {
2881 if_ctx_t ctx = if_getsoftc(ifp);
2882 bool restart;
2883 int err;
2884
2885 CTX_LOCK(ctx);
2886 if (ctx->ifc_pm_state != IFLIB_PM_ACTIVE) {
2887 CTX_UNLOCK(ctx);
2888 return (EBUSY);
2889 }
2890 restart = (if_getflags(ifp) & IFF_UP) != 0 ||
2891 ctx->ifc_datapath_state == IFLIB_DP_RUNNING;
2892 if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0 && restart)
2893 iflib_if_init_locked(ctx);
2894 CTX_UNLOCK(ctx);
2895 return (err);
2896 }
2897
2898 static void
iflib_media_status(if_t ifp,struct ifmediareq * ifmr)2899 iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
2900 {
2901 if_ctx_t ctx = if_getsoftc(ifp);
2902
2903 CTX_LOCK(ctx);
2904 if (ctx->ifc_pm_state != IFLIB_PM_ACTIVE) {
2905 ifmr->ifm_status = IFM_AVALID;
2906 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
2907 CTX_UNLOCK(ctx);
2908 return;
2909 }
2910 /*
2911 * There is no need to update the admin status when it is done regularly by
2912 * _task_fn_admin(), so only do it if that's not running. That can be quite
2913 * expensive on some drivers.
2914 */
2915 if (!iflib_admin_enabled(ctx)) {
2916 IFDI_UPDATE_ADMIN_STATUS(ctx);
2917 }
2918 IFDI_MEDIA_STATUS(ctx, ifmr);
2919 CTX_UNLOCK(ctx);
2920 }
2921
2922 static void
iflib_stop(if_ctx_t ctx)2923 iflib_stop(if_ctx_t ctx)
2924 {
2925 iflib_txq_t txq = ctx->ifc_txqs;
2926 iflib_rxq_t rxq = ctx->ifc_rxqs;
2927 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2928 if_shared_ctx_t sctx = ctx->ifc_sctx;
2929 iflib_dma_info_t di;
2930 iflib_fl_t fl;
2931 bool stop_hardware;
2932 int i, j;
2933
2934 sx_assert(&ctx->ifc_ctx_sx, SA_XLOCKED);
2935 KASSERT(ctx->ifc_datapath_state != IFLIB_DP_STOPPING,
2936 ("recursive iflib stop"));
2937 stop_hardware = ctx->ifc_datapath_state != IFLIB_DP_STOPPED;
2938
2939 if (ctx->ifc_sysctl_simple_tx && stop_hardware) {
2940 /* close deferral rings to new traffic */
2941 for (i = 0; i < scctx->isc_ntxqsets; i++) {
2942 atomic_set_int(&txq[i].ift_producers,
2943 IFLIB_TXQ_QUIESCING);
2944 }
2945 }
2946
2947 /* Tell the stack that the interface is no longer active */
2948 STATE_LOCK(ctx);
2949 iflib_set_running(ctx, false);
2950 STATE_UNLOCK(ctx);
2951
2952 if (stop_hardware) {
2953 ctx->ifc_datapath_state = IFLIB_DP_STOPPING;
2954 IFDI_INTR_DISABLE(ctx);
2955 DELAY(1000);
2956 IFDI_STOP(ctx);
2957 DELAY(1000);
2958 ctx->ifc_datapath_state = IFLIB_DP_STOPPED;
2959 }
2960
2961 /*
2962 * Stop any pending txsync/rxsync and prevent new ones
2963 * form starting. Processes blocked in poll() will get
2964 * POLLERR.
2965 */
2966 netmap_disable_all_rings(ctx->ifc_ifp);
2967
2968 iflib_debug_reset();
2969 /* Wait for current tx queue users to exit to disarm watchdog timer. */
2970 for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
2971 /* make sure all transmitters have completed before proceeding XXX */
2972
2973 CALLOUT_LOCK(txq);
2974 callout_stop(&txq->ift_timer);
2975 #ifdef DEV_NETMAP
2976 callout_stop(&txq->ift_netmap_timer);
2977 #endif /* DEV_NETMAP */
2978 CALLOUT_UNLOCK(txq);
2979
2980 /* clean any enqueued buffers */
2981 if (!ctx->ifc_sysctl_simple_tx) {
2982 iflib_ifmp_purge(txq);
2983 } else {
2984 mtx_lock(&txq->ift_mtx);
2985 drbr_flush(ctx->ifc_ifp, txq->ift_drbr);
2986 mtx_unlock(&txq->ift_mtx);
2987 }
2988 /* Free any existing tx buffers. */
2989 for (j = 0; j < txq->ift_size; j++) {
2990 iflib_txsd_free(ctx, txq, j);
2991 }
2992 txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2993 txq->ift_processed_prev = 0;
2994 txq->ift_outstanding_prev = 0;
2995 txq->ift_wdog_armed = 0;
2996 txq->ift_in_use = txq->ift_gen = txq->ift_no_desc_avail = 0;
2997 txq->ift_npending = txq->ift_db_pending = 0;
2998 txq->ift_rs_pending = 0;
2999 if (sctx->isc_flags & IFLIB_PRESERVE_TX_INDICES)
3000 txq->ift_cidx = txq->ift_pidx;
3001 else
3002 txq->ift_cidx = txq->ift_pidx = 0;
3003
3004 txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
3005 txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
3006 txq->ift_pullups = 0;
3007 txq->ift_drbr_direct = txq->ift_drbr_stall = 0;
3008 if (ctx->ifc_sysctl_simple_tx) {
3009 counter_u64_zero(txq->ift_drbr_deferred);
3010 counter_u64_zero(txq->ift_drbr_drops);
3011 counter_u64_zero(txq->ift_drbr_blocked);
3012 counter_u64_zero(txq->ift_drbr_remote);
3013 }
3014 ifmp_ring_reset_stats(txq->ift_br);
3015 for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++)
3016 bzero((void *)di->idi_vaddr, di->idi_size);
3017 }
3018 for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
3019 if (rxq->ifr_task.gt_taskqueue != NULL)
3020 gtaskqueue_drain(rxq->ifr_task.gt_taskqueue,
3021 &rxq->ifr_task.gt_task);
3022
3023 rxq->ifr_cq_cidx = 0;
3024 for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++)
3025 bzero((void *)di->idi_vaddr, di->idi_size);
3026 /* also resets the free lists pidx/cidx */
3027 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
3028 iflib_fl_bufs_free(fl);
3029 }
3030 }
3031
3032 static inline caddr_t
calc_next_rxd(iflib_fl_t fl,int cidx)3033 calc_next_rxd(iflib_fl_t fl, int cidx)
3034 {
3035 qidx_t size;
3036 int nrxd;
3037 caddr_t start, end, cur, next;
3038
3039 nrxd = fl->ifl_size;
3040 size = fl->ifl_rxd_size;
3041 start = fl->ifl_ifdi->idi_vaddr;
3042
3043 if (__predict_false(size == 0))
3044 return (start);
3045 cur = start + size * cidx;
3046 end = start + size * nrxd;
3047 next = CACHE_PTR_NEXT(cur);
3048 return (next < end ? next : start);
3049 }
3050
3051 static inline void
prefetch_pkts(iflib_fl_t fl,int cidx)3052 prefetch_pkts(iflib_fl_t fl, int cidx)
3053 {
3054 int nextptr;
3055 int nrxd = fl->ifl_size;
3056 caddr_t next_rxd;
3057
3058 nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd - 1);
3059 prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
3060 prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
3061 next_rxd = calc_next_rxd(fl, cidx);
3062 prefetch(next_rxd);
3063 prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd - 1)]);
3064 prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd - 1)]);
3065 prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd - 1)]);
3066 prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd - 1)]);
3067 prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd - 1)]);
3068 prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd - 1)]);
3069 prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd - 1)]);
3070 prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd - 1)]);
3071 }
3072
3073 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)3074 rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd,
3075 int *pf_rv, if_rxd_info_t ri)
3076 {
3077 bus_dmamap_t map;
3078 iflib_fl_t fl;
3079 caddr_t payload;
3080 struct mbuf *m;
3081 int flid, cidx, len, next;
3082
3083 map = NULL;
3084 flid = irf->irf_flid;
3085 cidx = irf->irf_idx;
3086 fl = &rxq->ifr_fl[flid];
3087 sd->ifsd_fl = fl;
3088 sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
3089 fl->ifl_credits--;
3090 #if MEMORY_LOGGING
3091 fl->ifl_m_dequeued++;
3092 #endif
3093 if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
3094 prefetch_pkts(fl, cidx);
3095 next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size - 1);
3096 prefetch(&fl->ifl_sds.ifsd_map[next]);
3097 map = fl->ifl_sds.ifsd_map[cidx];
3098
3099 bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD);
3100
3101 if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL &&
3102 irf->irf_len != 0) {
3103 payload = *sd->ifsd_cl;
3104 payload += ri->iri_pad;
3105 len = ri->iri_len - ri->iri_pad;
3106 *pf_rv = pfil_mem_in(rxq->pfil, payload, len, ri->iri_ifp, &m);
3107 switch (*pf_rv) {
3108 case PFIL_DROPPED:
3109 case PFIL_CONSUMED:
3110 /*
3111 * The filter ate it. Everything is recycled.
3112 */
3113 m = NULL;
3114 unload = 0;
3115 break;
3116 case PFIL_REALLOCED:
3117 /*
3118 * The filter copied it. Everything is recycled.
3119 * 'm' points at new mbuf.
3120 */
3121 unload = 0;
3122 break;
3123 case PFIL_PASS:
3124 /*
3125 * Filter said it was OK, so receive like
3126 * normal
3127 */
3128 m = fl->ifl_sds.ifsd_m[cidx];
3129 fl->ifl_sds.ifsd_m[cidx] = NULL;
3130 break;
3131 default:
3132 MPASS(0);
3133 }
3134 } else {
3135 m = fl->ifl_sds.ifsd_m[cidx];
3136 fl->ifl_sds.ifsd_m[cidx] = NULL;
3137 if (pf_rv != NULL)
3138 *pf_rv = PFIL_PASS;
3139 }
3140
3141 if (unload && irf->irf_len != 0)
3142 bus_dmamap_unload(fl->ifl_buf_tag, map);
3143 fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size - 1);
3144 if (__predict_false(fl->ifl_cidx == 0))
3145 fl->ifl_gen = 0;
3146 bit_clear(fl->ifl_rx_bitmap, cidx);
3147 return (m);
3148 }
3149
3150 static struct mbuf *
assemble_segments(iflib_rxq_t rxq,if_rxd_info_t ri,if_rxsd_t sd,int * pf_rv)3151 assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv)
3152 {
3153 struct mbuf *m, *mh, *mt;
3154 caddr_t cl;
3155 int *pf_rv_ptr, flags, i, padlen;
3156 bool consumed;
3157
3158 i = 0;
3159 mh = NULL;
3160 consumed = false;
3161 *pf_rv = PFIL_PASS;
3162 pf_rv_ptr = pf_rv;
3163 do {
3164 m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd,
3165 pf_rv_ptr, ri);
3166
3167 MPASS(*sd->ifsd_cl != NULL);
3168
3169 /*
3170 * Exclude zero-length frags & frags from
3171 * packets the filter has consumed or dropped
3172 */
3173 if (ri->iri_frags[i].irf_len == 0 || consumed ||
3174 *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) {
3175 if (mh == NULL) {
3176 consumed = true;
3177 pf_rv_ptr = NULL;
3178 }
3179 /* XXX we can save the cluster here, but not the mbuf */
3180 if (m != NULL) {
3181 m_init(m, M_NOWAIT, MT_DATA, 0);
3182 m_free(m);
3183 }
3184 continue;
3185 }
3186 if (mh == NULL) {
3187 flags = M_PKTHDR | M_EXT;
3188 mh = mt = m;
3189 padlen = ri->iri_pad;
3190 } else {
3191 flags = M_EXT;
3192 mt->m_next = m;
3193 mt = m;
3194 /* assuming padding is only on the first fragment */
3195 padlen = 0;
3196 }
3197 cl = *sd->ifsd_cl;
3198 *sd->ifsd_cl = NULL;
3199
3200 /* Can these two be made one ? */
3201 m_init(m, M_NOWAIT, MT_DATA, flags);
3202 m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
3203 /*
3204 * These must follow m_init and m_cljset
3205 */
3206 m->m_data += padlen;
3207 ri->iri_len -= padlen;
3208 m->m_len = ri->iri_frags[i].irf_len;
3209 } while (++i < ri->iri_nfrags);
3210
3211 return (mh);
3212 }
3213
3214 /*
3215 * Process one software descriptor
3216 */
3217 static struct mbuf *
iflib_rxd_pkt_get(iflib_rxq_t rxq,if_rxd_info_t ri)3218 iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
3219 {
3220 struct if_rxsd sd;
3221 struct mbuf *m;
3222 int pf_rv;
3223
3224 /* should I merge this back in now that the two paths are basically duplicated? */
3225 if (ri->iri_nfrags == 1 &&
3226 ri->iri_frags[0].irf_len != 0 &&
3227 ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) {
3228 m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd,
3229 &pf_rv, ri);
3230 if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
3231 return (m);
3232 if (pf_rv == PFIL_PASS) {
3233 m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
3234 #ifndef __NO_STRICT_ALIGNMENT
3235 if (!IP_ALIGNED(m) && ri->iri_pad == 0)
3236 m->m_data += 2;
3237 #endif
3238 memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
3239 m->m_len = ri->iri_frags[0].irf_len;
3240 m->m_data += ri->iri_pad;
3241 ri->iri_len -= ri->iri_pad;
3242 }
3243 } else {
3244 m = assemble_segments(rxq, ri, &sd, &pf_rv);
3245 if (m == NULL)
3246 return (NULL);
3247 if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
3248 return (m);
3249 }
3250 m->m_pkthdr.len = ri->iri_len;
3251 m->m_pkthdr.rcvif = ri->iri_ifp;
3252 m->m_flags |= ri->iri_flags & IFLIB_IRI_VALID_FLAGS;
3253 m->m_pkthdr.ether_vtag = ri->iri_vtag;
3254 m->m_pkthdr.flowid = ri->iri_flowid;
3255 #ifdef NUMA
3256 m->m_pkthdr.numa_domain = if_getnumadomain(ri->iri_ifp);
3257 #endif
3258 M_HASHTYPE_SET(m, ri->iri_rsstype);
3259 m->m_pkthdr.csum_flags = ri->iri_csum_flags;
3260 m->m_pkthdr.csum_data = ri->iri_csum_data;
3261 m->m_pkthdr.rcv_tstmp = ri->iri_rcv_tstmp;
3262 return (m);
3263 }
3264
3265 static void
_task_fn_rx_watchdog(void * context)3266 _task_fn_rx_watchdog(void *context)
3267 {
3268 iflib_rxq_t rxq = context;
3269
3270 GROUPTASK_ENQUEUE(&rxq->ifr_task);
3271 }
3272
3273 static uint8_t
iflib_rxeof(iflib_rxq_t rxq,qidx_t budget)3274 iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
3275 {
3276 if_t ifp;
3277 if_ctx_t ctx = rxq->ifr_ctx;
3278 if_shared_ctx_t sctx = ctx->ifc_sctx;
3279 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
3280 int avail, i;
3281 qidx_t *cidxp;
3282 struct if_rxd_info ri;
3283 int err, budget_left, rx_bytes, rx_pkts;
3284 iflib_fl_t fl;
3285 #if defined(INET6) || defined(INET)
3286 int lro_enabled;
3287 #endif
3288 uint8_t retval = 0;
3289
3290 /*
3291 * XXX early demux data packets so that if_input processing only handles
3292 * acks in interrupt context
3293 */
3294 struct mbuf *m, *mh, *mt;
3295
3296 NET_EPOCH_ASSERT();
3297
3298 ifp = ctx->ifc_ifp;
3299 mh = mt = NULL;
3300 MPASS(budget > 0);
3301 rx_pkts = rx_bytes = 0;
3302 if (sctx->isc_flags & IFLIB_HAS_RXCQ)
3303 cidxp = &rxq->ifr_cq_cidx;
3304 else
3305 cidxp = &rxq->ifr_fl[0].ifl_cidx;
3306 if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
3307 for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
3308 retval |= iflib_fl_refill_all(ctx, fl);
3309 DBG_COUNTER_INC(rx_unavail);
3310 return (retval);
3311 }
3312
3313 #if defined(INET6) || defined(INET)
3314 lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
3315 #endif
3316
3317 /* pfil needs the vnet to be set */
3318 CURVNET_SET_QUIET(if_getvnet(ifp));
3319 for (budget_left = budget; budget_left > 0 && avail > 0;) {
3320 if (__predict_false(!CTX_ACTIVE(ctx))) {
3321 DBG_COUNTER_INC(rx_ctx_inactive);
3322 break;
3323 }
3324 /*
3325 * Reset client set fields to their default values
3326 */
3327 memset(&ri, 0, sizeof(ri));
3328 ri.iri_qsidx = rxq->ifr_id;
3329 ri.iri_cidx = *cidxp;
3330 ri.iri_ifp = ifp;
3331 ri.iri_frags = rxq->ifr_frags;
3332 err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
3333
3334 if (err) {
3335 CURVNET_RESTORE();
3336 goto err;
3337 }
3338 rx_pkts += 1;
3339 rx_bytes += ri.iri_len;
3340 if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
3341 *cidxp = ri.iri_cidx;
3342 /* Update our consumer index */
3343 /* XXX NB: shurd - check if this is still safe */
3344 while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0])
3345 rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
3346 /* was this only a completion queue message? */
3347 if (__predict_false(ri.iri_nfrags == 0))
3348 continue;
3349 }
3350 MPASS(ri.iri_nfrags != 0);
3351 MPASS(ri.iri_len != 0);
3352
3353 /* will advance the cidx on the corresponding free lists */
3354 m = iflib_rxd_pkt_get(rxq, &ri);
3355 avail--;
3356 budget_left--;
3357 if (avail == 0 && budget_left)
3358 avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
3359
3360 if (__predict_false(m == NULL))
3361 continue;
3362
3363 #ifndef __NO_STRICT_ALIGNMENT
3364 if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
3365 continue;
3366 #endif
3367 #if defined(INET6) || defined(INET)
3368 if (lro_enabled) {
3369 tcp_lro_queue_mbuf(&rxq->ifr_lc, m);
3370 continue;
3371 }
3372 #endif
3373
3374 if (mh == NULL)
3375 mh = mt = m;
3376 else {
3377 mt->m_nextpkt = m;
3378 mt = m;
3379 }
3380 }
3381 CURVNET_RESTORE();
3382 /* make sure that we can refill faster than drain */
3383 for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
3384 retval |= iflib_fl_refill_all(ctx, fl);
3385
3386 if (mh != NULL) {
3387 if_input(ifp, mh);
3388 DBG_COUNTER_INC(rx_if_input);
3389 }
3390
3391 if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
3392 if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
3393
3394 /*
3395 * Flush any outstanding LRO work
3396 */
3397 #if defined(INET6) || defined(INET)
3398 tcp_lro_flush_all(&rxq->ifr_lc);
3399 #endif
3400 if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0)
3401 retval |= IFLIB_RXEOF_MORE;
3402 return (retval);
3403 err:
3404 STATE_LOCK(ctx);
3405 ctx->ifc_flags |= IFC_DO_RESET;
3406 iflib_admin_intr_deferred(ctx);
3407 STATE_UNLOCK(ctx);
3408 return (0);
3409 }
3410
3411 #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq) - 1)
3412 static inline qidx_t
txq_max_db_deferred(iflib_txq_t txq,qidx_t in_use)3413 txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
3414 {
3415 qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
3416 qidx_t minthresh = txq->ift_size / 8;
3417 if (in_use > 4 * minthresh)
3418 return (notify_count);
3419 if (in_use > 2 * minthresh)
3420 return (notify_count >> 1);
3421 if (in_use > minthresh)
3422 return (notify_count >> 3);
3423 return (0);
3424 }
3425
3426 static inline qidx_t
txq_max_rs_deferred(iflib_txq_t txq)3427 txq_max_rs_deferred(iflib_txq_t txq)
3428 {
3429 qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
3430 qidx_t minthresh = txq->ift_size / 8;
3431 if (txq->ift_in_use > 4 * minthresh)
3432 return (notify_count);
3433 if (txq->ift_in_use > 2 * minthresh)
3434 return (notify_count >> 1);
3435 if (txq->ift_in_use > minthresh)
3436 return (notify_count >> 2);
3437 return (2);
3438 }
3439
3440 #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
3441 #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
3442
3443 #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
3444 #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
3445 #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
3446
3447 /* forward compatibility for cxgb */
3448 #define FIRST_QSET(ctx) 0
3449 #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
3450 #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
3451 #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
3452 #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
3453
3454 static inline bool
iflib_txd_db_check(iflib_txq_t txq,int ring)3455 iflib_txd_db_check(iflib_txq_t txq, int ring)
3456 {
3457 if_ctx_t ctx = txq->ift_ctx;
3458 qidx_t dbval, max;
3459
3460 max = TXQ_MAX_DB_DEFERRED(txq, txq->ift_in_use);
3461
3462 /* force || threshold exceeded || at the edge of the ring */
3463 if (ring || (txq->ift_db_pending >= max) || (TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx))) {
3464
3465 /*
3466 * 'npending' is used if the card's doorbell is in terms of the number of descriptors
3467 * pending flush (BRCM). 'pidx' is used in cases where the card's doorbeel uses the
3468 * producer index explicitly (INTC).
3469 */
3470 dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
3471 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
3472 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3473 ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
3474
3475 /*
3476 * Absent bugs there are zero packets pending so reset pending counts to zero.
3477 */
3478 txq->ift_db_pending = txq->ift_npending = 0;
3479 return (true);
3480 }
3481 return (false);
3482 }
3483
3484 #ifdef PKT_DEBUG
3485 static void
print_pkt(if_pkt_info_t pi)3486 print_pkt(if_pkt_info_t pi)
3487 {
3488 printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
3489 pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
3490 printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
3491 pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
3492 printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
3493 pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
3494 }
3495 #endif
3496
3497 #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
3498 #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO))
3499 #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
3500 #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO))
3501
3502 /**
3503 * Parses out ethernet header information in the given mbuf.
3504 * Returns in pi: ipi_etype (EtherType) and ipi_ehdrlen (Ethernet header length)
3505 *
3506 * This will account for the VLAN header if present.
3507 *
3508 * XXX: This doesn't handle QinQ, which could prevent TX offloads for those
3509 * types of packets.
3510 */
3511 static int
iflib_parse_ether_header(if_pkt_info_t pi,struct mbuf ** mp,uint64_t * pullups)3512 iflib_parse_ether_header(if_pkt_info_t pi, struct mbuf **mp, uint64_t *pullups)
3513 {
3514 struct ether_vlan_header *eh;
3515 struct mbuf *m;
3516
3517 m = *mp;
3518 if (__predict_false(m->m_len < sizeof(*eh))) {
3519 (*pullups)++;
3520 if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
3521 return (ENOMEM);
3522 }
3523 eh = mtod(m, struct ether_vlan_header *);
3524 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
3525 pi->ipi_etype = ntohs(eh->evl_proto);
3526 pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3527 } else {
3528 pi->ipi_etype = ntohs(eh->evl_encap_proto);
3529 pi->ipi_ehdrlen = ETHER_HDR_LEN;
3530 }
3531 *mp = m;
3532
3533 return (0);
3534 }
3535
3536 /**
3537 * Parse up to the L3 header and extract IPv4/IPv6 header information into pi.
3538 * Currently this information includes: IP ToS value, IP header version/presence
3539 *
3540 * This is missing some checks and doesn't edit the packet content as it goes,
3541 * unlike iflib_parse_header(), in order to keep the amount of code here minimal.
3542 */
3543 static int
iflib_parse_header_partial(if_pkt_info_t pi,struct mbuf ** mp,uint64_t * pullups)3544 iflib_parse_header_partial(if_pkt_info_t pi, struct mbuf **mp, uint64_t *pullups)
3545 {
3546 struct mbuf *m;
3547 int err;
3548
3549 *pullups = 0;
3550 m = *mp;
3551 if (!M_WRITABLE(m)) {
3552 m = m_dup(m, M_NOWAIT);
3553 m_freem(*mp);
3554 DBG_COUNTER_INC(tx_frees);
3555 *mp = m;
3556 if (m == NULL)
3557 return (ENOMEM);
3558 }
3559
3560 /* Fills out pi->ipi_etype */
3561 err = iflib_parse_ether_header(pi, mp, pullups);
3562 if (err)
3563 return (err);
3564 m = *mp;
3565
3566 switch (pi->ipi_etype) {
3567 #ifdef INET
3568 case ETHERTYPE_IP:
3569 {
3570 struct mbuf *n;
3571 struct ip *ip = NULL;
3572 int miniplen;
3573
3574 miniplen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip));
3575 if (__predict_false(m->m_len < miniplen)) {
3576 /*
3577 * Check for common case where the first mbuf only contains
3578 * the Ethernet header
3579 */
3580 if (m->m_len == pi->ipi_ehdrlen) {
3581 n = m->m_next;
3582 MPASS(n);
3583 /* If next mbuf contains at least the minimal IP header, then stop */
3584 if (n->m_len >= sizeof(*ip)) {
3585 ip = (struct ip *)n->m_data;
3586 } else {
3587 (*pullups)++;
3588 if (__predict_false((m = m_pullup(m, miniplen)) == NULL))
3589 return (ENOMEM);
3590 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3591 }
3592 } else {
3593 (*pullups)++;
3594 if (__predict_false((m = m_pullup(m, miniplen)) == NULL))
3595 return (ENOMEM);
3596 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3597 }
3598 } else {
3599 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3600 }
3601
3602 /* Have the IPv4 header w/ no options here */
3603 pi->ipi_ip_hlen = ip->ip_hl << 2;
3604 pi->ipi_ipproto = ip->ip_p;
3605 pi->ipi_ip_tos = ip->ip_tos;
3606 pi->ipi_flags |= IPI_TX_IPV4;
3607
3608 break;
3609 }
3610 #endif
3611 #ifdef INET6
3612 case ETHERTYPE_IPV6:
3613 {
3614 struct ip6_hdr *ip6;
3615
3616 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
3617 (*pullups)++;
3618 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
3619 return (ENOMEM);
3620 }
3621 ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
3622
3623 /* Have the IPv6 fixed header here */
3624 pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
3625 pi->ipi_ipproto = ip6->ip6_nxt;
3626 pi->ipi_ip_tos = IPV6_TRAFFIC_CLASS(ip6);
3627 pi->ipi_flags |= IPI_TX_IPV6;
3628
3629 break;
3630 }
3631 #endif
3632 default:
3633 pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
3634 pi->ipi_ip_hlen = 0;
3635 break;
3636 }
3637 *mp = m;
3638
3639 return (0);
3640
3641 }
3642
3643 static int
iflib_parse_header(iflib_txq_t txq,if_pkt_info_t pi,struct mbuf ** mp)3644 iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
3645 {
3646 if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
3647 struct mbuf *m;
3648 int err;
3649
3650 m = *mp;
3651 if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
3652 M_WRITABLE(m) == 0) {
3653 m = m_dup(m, M_NOWAIT);
3654 m_freem(*mp);
3655 DBG_COUNTER_INC(tx_frees);
3656 *mp = m;
3657 if (m == NULL)
3658 return (ENOMEM);
3659 }
3660
3661 /* Fills out pi->ipi_etype */
3662 err = iflib_parse_ether_header(pi, mp, &txq->ift_pullups);
3663 if (__predict_false(err))
3664 return (err);
3665 m = *mp;
3666
3667 switch (pi->ipi_etype) {
3668 #ifdef INET
3669 case ETHERTYPE_IP:
3670 {
3671 struct ip *ip;
3672 struct tcphdr *th;
3673 uint8_t hlen;
3674
3675 hlen = pi->ipi_ehdrlen + sizeof(*ip);
3676 if (__predict_false(m->m_len < hlen)) {
3677 txq->ift_pullups++;
3678 if (__predict_false((m = m_pullup(m, hlen)) == NULL))
3679 return (ENOMEM);
3680 }
3681 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3682 hlen = pi->ipi_ehdrlen + (ip->ip_hl << 2);
3683 if (ip->ip_p == IPPROTO_TCP) {
3684 hlen += sizeof(*th);
3685 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
3686 } else if (ip->ip_p == IPPROTO_UDP) {
3687 hlen += sizeof(struct udphdr);
3688 }
3689 if (__predict_false(m->m_len < hlen)) {
3690 txq->ift_pullups++;
3691 if ((m = m_pullup(m, hlen)) == NULL)
3692 return (ENOMEM);
3693 /* reset pointers after pullup */
3694 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
3695 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2));
3696 }
3697 pi->ipi_ip_hlen = ip->ip_hl << 2;
3698 pi->ipi_ipproto = ip->ip_p;
3699 pi->ipi_ip_tos = ip->ip_tos;
3700 pi->ipi_flags |= IPI_TX_IPV4;
3701
3702 /* TCP checksum offload may require TCP header length */
3703 if (IS_TX_OFFLOAD4(pi)) {
3704 if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) {
3705 pi->ipi_tcp_hflags = tcp_get_flags(th);
3706 pi->ipi_tcp_hlen = th->th_off << 2;
3707 pi->ipi_tcp_seq = th->th_seq;
3708 }
3709 if (IS_TSO4(pi)) {
3710 MPASS(ip->ip_p == IPPROTO_TCP);
3711 /*
3712 * TSO always requires hardware checksum offload.
3713 */
3714 pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP);
3715 th->th_sum = in_pseudo(ip->ip_src.s_addr,
3716 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
3717 pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
3718 if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
3719 ip->ip_sum = 0;
3720 ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
3721 }
3722 }
3723 }
3724 if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP))
3725 ip->ip_sum = 0;
3726
3727 break;
3728 }
3729 #endif
3730 #ifdef INET6
3731 case ETHERTYPE_IPV6:
3732 {
3733 struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
3734 struct tcphdr *th;
3735 pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
3736
3737 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
3738 txq->ift_pullups++;
3739 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
3740 return (ENOMEM);
3741 /* reset pointers after pullup */
3742 ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
3743 }
3744 th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
3745
3746 /* XXX-BZ this will go badly in case of ext hdrs. */
3747 pi->ipi_ipproto = ip6->ip6_nxt;
3748 pi->ipi_ip_tos = IPV6_TRAFFIC_CLASS(ip6);
3749 pi->ipi_flags |= IPI_TX_IPV6;
3750
3751 /* TCP checksum offload may require TCP header length */
3752 if (IS_TX_OFFLOAD6(pi)) {
3753 if (pi->ipi_ipproto == IPPROTO_TCP) {
3754 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
3755 txq->ift_pullups++;
3756 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
3757 return (ENOMEM);
3758 /* reset pointers after pullup */
3759 ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
3760 th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
3761 }
3762 pi->ipi_tcp_hflags = tcp_get_flags(th);
3763 pi->ipi_tcp_hlen = th->th_off << 2;
3764 pi->ipi_tcp_seq = th->th_seq;
3765 }
3766 if (IS_TSO6(pi)) {
3767 MPASS(ip6->ip6_nxt == IPPROTO_TCP);
3768 /*
3769 * TSO always requires hardware checksum offload.
3770 */
3771 pi->ipi_csum_flags |= CSUM_IP6_TCP;
3772 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
3773 pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
3774 }
3775 }
3776 break;
3777 }
3778 #endif
3779 default:
3780 pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
3781 pi->ipi_ip_hlen = 0;
3782 break;
3783 }
3784 *mp = m;
3785
3786 return (0);
3787 }
3788
3789 /*
3790 * If dodgy hardware rejects the scatter gather chain we've handed it
3791 * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
3792 * m_defrag'd mbufs
3793 */
3794 static __noinline struct mbuf *
iflib_remove_mbuf(iflib_txq_t txq)3795 iflib_remove_mbuf(iflib_txq_t txq)
3796 {
3797 int ntxd, pidx;
3798 struct mbuf *m, **ifsd_m;
3799
3800 ifsd_m = txq->ift_sds.ifsd_m;
3801 ntxd = txq->ift_size;
3802 pidx = txq->ift_pidx & (ntxd - 1);
3803 ifsd_m = txq->ift_sds.ifsd_m;
3804 m = IFLIB_GET_MBUF(ifsd_m[pidx]);
3805 ifsd_m[pidx] = NULL;
3806 bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]);
3807 if (txq->ift_sds.ifsd_tso_map != NULL)
3808 bus_dmamap_unload(txq->ift_tso_buf_tag,
3809 txq->ift_sds.ifsd_tso_map[pidx]);
3810 #if MEMORY_LOGGING
3811 txq->ift_dequeued++;
3812 #endif
3813 return (m);
3814 }
3815
3816 /*
3817 * Pad an mbuf to ensure a minimum ethernet frame size.
3818 * min_frame_size is the frame size (less CRC) to pad the mbuf to
3819 */
3820 static __noinline int
iflib_ether_pad(device_t dev,struct mbuf ** m_head,uint16_t min_frame_size)3821 iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size)
3822 {
3823 /*
3824 * 18 is enough bytes to pad an ARP packet to 46 bytes, and
3825 * and ARP message is the smallest common payload I can think of
3826 */
3827 static char pad[18]; /* just zeros */
3828 int n;
3829 struct mbuf *new_head;
3830
3831 if (!M_WRITABLE(*m_head)) {
3832 new_head = m_dup(*m_head, M_NOWAIT);
3833 m_freem(*m_head);
3834 *m_head = new_head;
3835 if (new_head == NULL) {
3836 device_printf(dev, "cannot pad short frame, m_dup() failed");
3837 DBG_COUNTER_INC(encap_pad_mbuf_fail);
3838 DBG_COUNTER_INC(tx_frees);
3839 return (ENOMEM);
3840 }
3841 }
3842
3843 for (n = min_frame_size - (*m_head)->m_pkthdr.len;
3844 n > 0; n -= sizeof(pad))
3845 if (!m_append(*m_head, min(n, sizeof(pad)), pad))
3846 break;
3847
3848 if (n > 0) {
3849 m_freem(*m_head);
3850 *m_head = NULL;
3851 device_printf(dev, "cannot pad short frame\n");
3852 DBG_COUNTER_INC(encap_pad_mbuf_fail);
3853 DBG_COUNTER_INC(tx_frees);
3854 return (ENOMEM);
3855 }
3856
3857 return (0);
3858 }
3859
3860 static int
iflib_encap(iflib_txq_t txq,struct mbuf ** m_headp,int * obytes,int * opkts)3861 iflib_encap(iflib_txq_t txq, struct mbuf **m_headp, int *obytes, int *opkts)
3862 {
3863 if_ctx_t ctx;
3864 if_shared_ctx_t sctx;
3865 if_softc_ctx_t scctx;
3866 bus_dma_tag_t buf_tag;
3867 bus_dma_segment_t *segs;
3868 struct mbuf *m_head, **ifsd_m;
3869 bus_dmamap_t map;
3870 struct if_pkt_info pi;
3871 uintptr_t flags;
3872 int remap = 0;
3873 int err, nsegs, ndesc, max_segs, pidx;
3874
3875 ctx = txq->ift_ctx;
3876 sctx = ctx->ifc_sctx;
3877 scctx = &ctx->ifc_softc_ctx;
3878 segs = txq->ift_segs;
3879 m_head = *m_headp;
3880 map = NULL;
3881
3882 /*
3883 * If we're doing TSO the next descriptor to clean may be quite far ahead
3884 */
3885 pidx = txq->ift_pidx;
3886 map = txq->ift_sds.ifsd_map[pidx];
3887 ifsd_m = txq->ift_sds.ifsd_m;
3888
3889 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
3890 buf_tag = txq->ift_tso_buf_tag;
3891 max_segs = scctx->isc_tx_tso_segments_max;
3892 map = txq->ift_sds.ifsd_tso_map[pidx];
3893 MPASS(buf_tag != NULL);
3894 MPASS(max_segs > 0);
3895 flags = IFLIB_TSO;
3896 } else {
3897 buf_tag = txq->ift_buf_tag;
3898 max_segs = scctx->isc_tx_nsegments;
3899 map = txq->ift_sds.ifsd_map[pidx];
3900 flags = IFLIB_NO_TSO;
3901 }
3902 if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) &&
3903 __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) {
3904 err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size);
3905 if (err) {
3906 DBG_COUNTER_INC(encap_txd_encap_fail);
3907 return (err);
3908 }
3909 }
3910 m_head = *m_headp;
3911
3912 memset(&pi, 0, sizeof(pi));
3913 pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG | M_BCAST | M_MCAST));
3914 pi.ipi_pidx = pidx;
3915 pi.ipi_qsidx = txq->ift_id;
3916 pi.ipi_len = m_head->m_pkthdr.len;
3917 pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
3918 pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0;
3919
3920 /* deliberate bitwise OR to make one condition */
3921 if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
3922 if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) {
3923 DBG_COUNTER_INC(encap_txd_encap_fail);
3924 return (err);
3925 }
3926 m_head = *m_headp;
3927 }
3928
3929 retry:
3930 err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs,
3931 BUS_DMA_NOWAIT);
3932 defrag:
3933 if (__predict_false(err)) {
3934 switch (err) {
3935 case EFBIG:
3936 /* try collapse once and defrag once */
3937 if (remap == 0) {
3938 m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
3939 /* try defrag if collapsing fails */
3940 if (m_head == NULL)
3941 remap++;
3942 }
3943 if (remap == 1) {
3944 txq->ift_mbuf_defrag++;
3945 m_head = m_defrag(*m_headp, M_NOWAIT);
3946 }
3947 /*
3948 * remap should never be >1 unless bus_dmamap_load_mbuf_sg
3949 * failed to map an mbuf that was run through m_defrag
3950 */
3951 MPASS(remap <= 1);
3952 if (__predict_false(m_head == NULL || remap > 1))
3953 goto defrag_failed;
3954 remap++;
3955 *m_headp = m_head;
3956 goto retry;
3957 break;
3958 case ENOMEM:
3959 /* FALLTHROUGH */
3960 default:
3961 txq->ift_no_tx_dma_setup++;
3962 m_freem(*m_headp);
3963 DBG_COUNTER_INC(tx_frees);
3964 *m_headp = NULL;
3965 break;
3966 }
3967 txq->ift_map_failed++;
3968 DBG_COUNTER_INC(encap_load_mbuf_fail);
3969 DBG_COUNTER_INC(encap_txd_encap_fail);
3970 return (err);
3971 }
3972 ifsd_m[pidx] = IFLIB_SAVE_MBUF(m_head, flags);
3973 if (m_head->m_pkthdr.csum_flags & CSUM_SND_TAG)
3974 pi.ipi_mbuf = m_head;
3975 else
3976 pi.ipi_mbuf = NULL;
3977 /*
3978 * XXX assumes a 1 to 1 relationship between segments and
3979 * descriptors - this does not hold true on all drivers, e.g.
3980 * cxgb
3981 */
3982 if (__predict_false(nsegs > TXQ_AVAIL(txq))) {
3983 iflib_completed_tx_reclaim_force(txq);
3984 if (__predict_false(nsegs > TXQ_AVAIL(txq))) {
3985 txq->ift_no_desc_avail++;
3986 bus_dmamap_unload(buf_tag, map);
3987 DBG_COUNTER_INC(encap_txq_avail_fail);
3988 DBG_COUNTER_INC(encap_txd_encap_fail);
3989 if (ctx->ifc_sysctl_simple_tx) {
3990 *m_headp = m_head = iflib_remove_mbuf(txq);
3991 m_freem(*m_headp);
3992 DBG_COUNTER_INC(tx_frees);
3993 *m_headp = NULL;
3994 }
3995 if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
3996 GROUPTASK_ENQUEUE(&txq->ift_task);
3997 return (ENOBUFS);
3998 }
3999 }
4000 /*
4001 * On Intel cards we can greatly reduce the number of TX interrupts
4002 * we see by only setting report status on every Nth descriptor.
4003 * However, this also means that the driver will need to keep track
4004 * of the descriptors that RS was set on to check them for the DD bit.
4005 */
4006 if (txq->ift_rs_pending + nsegs + 1 > TXQ_MAX_RS_DEFERRED(txq) ||
4007 iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx)) {
4008 pi.ipi_flags |= IPI_TX_INTR;
4009 }
4010
4011 pi.ipi_segs = segs;
4012 pi.ipi_nsegs = nsegs;
4013
4014 MPASS(pidx >= 0 && pidx < txq->ift_size);
4015 #ifdef PKT_DEBUG
4016 print_pkt(&pi);
4017 #endif
4018 if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
4019 bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE);
4020 DBG_COUNTER_INC(tx_encap);
4021 MPASS(pi.ipi_new_pidx < txq->ift_size);
4022
4023 ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
4024 if (pi.ipi_new_pidx < pi.ipi_pidx) {
4025 ndesc += txq->ift_size;
4026 txq->ift_gen = 1;
4027 }
4028
4029 if (pi.ipi_flags & IPI_TX_INTR)
4030 txq->ift_rs_pending = 0;
4031 else
4032 txq->ift_rs_pending += ndesc;
4033 /*
4034 * drivers can need up to ift_pad sentinels
4035 */
4036 MPASS(ndesc <= pi.ipi_nsegs + txq->ift_pad);
4037 MPASS(pi.ipi_new_pidx != pidx);
4038 MPASS(ndesc > 0);
4039 txq->ift_in_use += ndesc;
4040 txq->ift_db_pending += ndesc;
4041
4042 /*
4043 * We update the last software descriptor again here because there may
4044 * be a sentinel and/or there may be more mbufs than segments
4045 */
4046 txq->ift_pidx = pi.ipi_new_pidx;
4047 txq->ift_npending += pi.ipi_ndescs;
4048
4049 /*
4050 * Update packets / bytes sent
4051 */
4052 if (flags & IFLIB_TSO) {
4053 int hlen = pi.ipi_ehdrlen + pi.ipi_ip_hlen + pi.ipi_tcp_hlen;
4054 int tsolen = pi.ipi_len - hlen;
4055 int nsegs = (tsolen + pi.ipi_tso_segsz - 1) / pi.ipi_tso_segsz;
4056 *obytes += tsolen + nsegs * hlen;
4057 *opkts += nsegs;
4058 } else {
4059 *obytes += pi.ipi_len;
4060 *opkts += 1;
4061 }
4062 } else {
4063 *m_headp = m_head = iflib_remove_mbuf(txq);
4064 if (err == EFBIG) {
4065 txq->ift_txd_encap_efbig++;
4066 if (remap < 2) {
4067 remap = 1;
4068 goto defrag;
4069 }
4070 goto defrag_failed;
4071 }
4072 /* mp_ring assumes ENOBUFS means we didn't consume the mbuf */
4073 if (err == ENOBUFS && !ctx->ifc_sysctl_simple_tx)
4074 err = ENOMEM;
4075 goto out_with_error;
4076 }
4077 /*
4078 * err can't possibly be non-zero here, so we don't neet to test it
4079 * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail).
4080 */
4081 return (err);
4082
4083 defrag_failed:
4084 err = ENOMEM;
4085 txq->ift_mbuf_defrag_failed++;
4086 out_with_error:
4087 txq->ift_map_failed++;
4088 m_freem(*m_headp);
4089 DBG_COUNTER_INC(tx_frees);
4090 *m_headp = NULL;
4091 DBG_COUNTER_INC(encap_txd_encap_fail);
4092 return (err);
4093 }
4094
4095 static void
iflib_tx_desc_free(iflib_txq_t txq,int n,struct mbuf ** m_defer)4096 iflib_tx_desc_free(iflib_txq_t txq, int n, struct mbuf **m_defer)
4097 {
4098 uint32_t qsize, cidx, gen;
4099 struct mbuf *m, **ifsd_m;
4100 uintptr_t flags;
4101
4102 cidx = txq->ift_cidx;
4103 gen = txq->ift_gen;
4104 qsize = txq->ift_size;
4105 ifsd_m =txq->ift_sds.ifsd_m;
4106
4107 while (n-- > 0) {
4108 if ((m = IFLIB_GET_MBUF(ifsd_m[cidx])) != NULL) {
4109 flags = IFLIB_GET_FLAGS(ifsd_m[cidx]);
4110 MPASS(flags != 0);
4111 if (flags & IFLIB_TSO) {
4112 bus_dmamap_sync(txq->ift_tso_buf_tag,
4113 txq->ift_sds.ifsd_tso_map[cidx],
4114 BUS_DMASYNC_POSTWRITE);
4115 bus_dmamap_unload(txq->ift_tso_buf_tag,
4116 txq->ift_sds.ifsd_tso_map[cidx]);
4117 } else {
4118 bus_dmamap_sync(txq->ift_buf_tag,
4119 txq->ift_sds.ifsd_map[cidx],
4120 BUS_DMASYNC_POSTWRITE);
4121 bus_dmamap_unload(txq->ift_buf_tag,
4122 txq->ift_sds.ifsd_map[cidx]);
4123 }
4124 /* XXX we don't support any drivers that batch packets yet */
4125 MPASS(m->m_nextpkt == NULL);
4126 if (m_defer == NULL) {
4127 m_freem(m);
4128 } else if (m != NULL) {
4129 *m_defer = m;
4130 m_defer++;
4131 }
4132 ifsd_m[cidx] = NULL;
4133 #if MEMORY_LOGGING
4134 txq->ift_dequeued++;
4135 #endif
4136 DBG_COUNTER_INC(tx_frees);
4137 }
4138 if (__predict_false(++cidx == qsize)) {
4139 cidx = 0;
4140 gen = 0;
4141 }
4142 }
4143 txq->ift_cidx = cidx;
4144 txq->ift_gen = gen;
4145 }
4146
4147 static __inline int
iflib_txq_can_reclaim(iflib_txq_t txq)4148 iflib_txq_can_reclaim(iflib_txq_t txq)
4149 {
4150 int reclaim, thresh;
4151
4152 thresh = txq->ift_reclaim_thresh;
4153 KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
4154 MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
4155
4156 if (ticks <= (txq->ift_last_reclaim + txq->ift_reclaim_ticks) &&
4157 txq->ift_in_use < thresh)
4158 return (false);
4159 iflib_tx_credits_update(txq->ift_ctx, txq);
4160 reclaim = DESC_RECLAIMABLE(txq);
4161 if (reclaim <= thresh) {
4162 #ifdef INVARIANTS
4163 if (iflib_verbose_debug) {
4164 printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __func__,
4165 txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
4166 reclaim, thresh);
4167 }
4168 #endif
4169 return (0);
4170 }
4171 return (reclaim);
4172 }
4173
4174 static __inline void
_iflib_completed_tx_reclaim(iflib_txq_t txq,struct mbuf ** m_defer,int reclaim)4175 _iflib_completed_tx_reclaim(iflib_txq_t txq, struct mbuf **m_defer, int reclaim)
4176 {
4177 txq->ift_last_reclaim = ticks;
4178 iflib_tx_desc_free(txq, reclaim, m_defer);
4179 txq->ift_cleaned += reclaim;
4180 txq->ift_in_use -= reclaim;
4181 }
4182
4183 static __inline int
iflib_completed_tx_reclaim(iflib_txq_t txq,struct mbuf ** m_defer)4184 iflib_completed_tx_reclaim(iflib_txq_t txq, struct mbuf **m_defer)
4185 {
4186 int reclaim;
4187
4188 reclaim = iflib_txq_can_reclaim(txq);
4189 if (reclaim <= 0)
4190 return (0);
4191 _iflib_completed_tx_reclaim(txq, m_defer, reclaim);
4192 return (reclaim);
4193 }
4194
4195 /*
4196 * Reclaim any transmit descriptors possible, ignoring coalescing
4197 */
4198 static __inline void
iflib_completed_tx_reclaim_force(iflib_txq_t txq)4199 iflib_completed_tx_reclaim_force(iflib_txq_t txq)
4200 {
4201 int reclaim;
4202
4203 iflib_tx_credits_update(txq->ift_ctx, txq);
4204 reclaim = DESC_RECLAIMABLE(txq);
4205 if (reclaim != 0)
4206 _iflib_completed_tx_reclaim(txq, NULL, reclaim);
4207 }
4208
4209 static struct mbuf **
_ring_peek_one(struct ifmp_ring * r,int cidx,int offset,int remaining)4210 _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
4211 {
4212 int next, size;
4213 struct mbuf **items;
4214
4215 size = r->size;
4216 next = (cidx + CACHE_PTR_INCREMENT) & (size - 1);
4217 items = __DEVOLATILE(struct mbuf **, &r->items[0]);
4218
4219 prefetch(items[(cidx + offset) & (size - 1)]);
4220 if (remaining > 1) {
4221 prefetch2cachelines(&items[next]);
4222 prefetch2cachelines(items[(cidx + offset + 1) & (size - 1)]);
4223 prefetch2cachelines(items[(cidx + offset + 2) & (size - 1)]);
4224 prefetch2cachelines(items[(cidx + offset + 3) & (size - 1)]);
4225 }
4226 return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size - 1)]));
4227 }
4228
4229 static void
iflib_txq_check_drain(iflib_txq_t txq,int budget)4230 iflib_txq_check_drain(iflib_txq_t txq, int budget)
4231 {
4232
4233 ifmp_ring_check_drainage(txq->ift_br, budget);
4234 }
4235
4236 static uint32_t
iflib_txq_can_drain(struct ifmp_ring * r)4237 iflib_txq_can_drain(struct ifmp_ring *r)
4238 {
4239 iflib_txq_t txq = r->cookie;
4240 if_ctx_t ctx = txq->ift_ctx;
4241
4242 if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx))
4243 return (1);
4244 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
4245 BUS_DMASYNC_POSTREAD);
4246 return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id,
4247 false));
4248 }
4249
4250 static uint32_t
iflib_txq_drain(struct ifmp_ring * r,uint32_t cidx,uint32_t pidx)4251 iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
4252 {
4253 iflib_txq_t txq = r->cookie;
4254 if_ctx_t ctx = txq->ift_ctx;
4255 if_t ifp = ctx->ifc_ifp;
4256 struct mbuf *m, **mp;
4257 int avail, bytes_sent, consumed, count, err, i;
4258 int mcast_sent, pkt_sent, reclaimed;
4259 bool do_prefetch, rang, ring;
4260
4261 if (__predict_false(!iflib_is_running(ctx) || !LINK_ACTIVE(ctx))) {
4262 DBG_COUNTER_INC(txq_drain_notready);
4263 return (0);
4264 }
4265 reclaimed = iflib_completed_tx_reclaim(txq, NULL);
4266 rang = iflib_txd_db_check(txq, reclaimed && txq->ift_db_pending);
4267 avail = IDXDIFF(pidx, cidx, r->size);
4268
4269 if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
4270 /*
4271 * The driver is unloading so we need to free all pending packets.
4272 */
4273 DBG_COUNTER_INC(txq_drain_flushing);
4274 for (i = 0; i < avail; i++) {
4275 if (__predict_true(r->items[(cidx + i) & (r->size - 1)] != (void *)txq))
4276 m_freem(r->items[(cidx + i) & (r->size - 1)]);
4277 r->items[(cidx + i) & (r->size - 1)] = NULL;
4278 }
4279 return (avail);
4280 }
4281
4282 if (__predict_false(!iflib_is_running(ctx))) {
4283 CALLOUT_LOCK(txq);
4284 callout_stop(&txq->ift_timer);
4285 CALLOUT_UNLOCK(txq);
4286 DBG_COUNTER_INC(txq_drain_stopped);
4287 return (0);
4288 }
4289
4290 consumed = mcast_sent = bytes_sent = pkt_sent = 0;
4291 count = MIN(avail, TX_BATCH_SIZE);
4292 #ifdef INVARIANTS
4293 if (iflib_verbose_debug)
4294 printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __func__,
4295 avail, ctx->ifc_flags, TXQ_AVAIL(txq));
4296 #endif
4297 do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
4298 err = 0;
4299 for (i = 0; i < count && TXQ_AVAIL(txq) >= MAX_TX_DESC(ctx); i++) {
4300 int rem = do_prefetch ? count - i : 0;
4301
4302 mp = _ring_peek_one(r, cidx, i, rem);
4303 MPASS(mp != NULL && *mp != NULL);
4304
4305 /*
4306 * Completion interrupts will use the address of the txq
4307 * as a sentinel to enqueue _something_ in order to acquire
4308 * the lock on the mp_ring (there's no direct lock call).
4309 * We obviously whave to check for these sentinel cases
4310 * and skip them.
4311 */
4312 if (__predict_false(*mp == (struct mbuf *)txq)) {
4313 consumed++;
4314 continue;
4315 }
4316 err = iflib_encap(txq, mp, &bytes_sent, &pkt_sent);
4317 if (__predict_false(err)) {
4318 /* no room - bail out */
4319 if (err == ENOBUFS)
4320 break;
4321 consumed++;
4322 /* we can't send this packet - skip it */
4323 continue;
4324 }
4325 consumed++;
4326 m = *mp;
4327 DBG_COUNTER_INC(tx_sent);
4328 mcast_sent += !!(m->m_flags & M_MCAST);
4329
4330 if (__predict_false(!iflib_is_running(ctx)))
4331 break;
4332 ETHER_BPF_MTAP(ifp, m);
4333 rang = iflib_txd_db_check(txq, false);
4334 }
4335
4336 /* deliberate use of bitwise or to avoid gratuitous short-circuit */
4337 ring = rang ? false : (iflib_min_tx_latency | err | (!!txq->ift_reclaim_thresh));
4338 iflib_txd_db_check(txq, ring);
4339 if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
4340 if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
4341 if (mcast_sent)
4342 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
4343 #ifdef INVARIANTS
4344 if (iflib_verbose_debug)
4345 printf("consumed=%d\n", consumed);
4346 #endif
4347 return (consumed);
4348 }
4349
4350 static uint32_t
iflib_txq_drain_always(struct ifmp_ring * r)4351 iflib_txq_drain_always(struct ifmp_ring *r)
4352 {
4353 return (1);
4354 }
4355
4356 static uint32_t
iflib_txq_drain_free(struct ifmp_ring * r,uint32_t cidx,uint32_t pidx)4357 iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
4358 {
4359 int i, avail;
4360 struct mbuf **mp;
4361 iflib_txq_t txq;
4362
4363 txq = r->cookie;
4364
4365 CALLOUT_LOCK(txq);
4366 callout_stop(&txq->ift_timer);
4367 CALLOUT_UNLOCK(txq);
4368
4369 avail = IDXDIFF(pidx, cidx, r->size);
4370 for (i = 0; i < avail; i++) {
4371 mp = _ring_peek_one(r, cidx, i, avail - i);
4372 if (__predict_false(*mp == (struct mbuf *)txq))
4373 continue;
4374 m_freem(*mp);
4375 DBG_COUNTER_INC(tx_frees);
4376 }
4377 MPASS(ifmp_ring_is_stalled(r) == 0);
4378 return (avail);
4379 }
4380
4381 static void
iflib_ifmp_purge(iflib_txq_t txq)4382 iflib_ifmp_purge(iflib_txq_t txq)
4383 {
4384 struct ifmp_ring *r;
4385
4386 r = txq->ift_br;
4387 r->drain = iflib_txq_drain_free;
4388 r->can_drain = iflib_txq_drain_always;
4389
4390 ifmp_ring_check_drainage(r, r->size);
4391
4392 r->drain = iflib_txq_drain;
4393 r->can_drain = iflib_txq_can_drain;
4394 }
4395
4396 static void
_task_fn_tx(void * context)4397 _task_fn_tx(void *context)
4398 {
4399 iflib_txq_t txq = context;
4400 if_ctx_t ctx = txq->ift_ctx;
4401 #if defined(DEV_NETMAP) || defined(ALTQ)
4402 if_t ifp = ctx->ifc_ifp;
4403 #endif
4404 int abdicate = ctx->ifc_sysctl_tx_abdicate;
4405
4406 #ifdef IFLIB_DIAGNOSTICS
4407 txq->ift_cpu_exec_count[curcpu]++;
4408 #endif
4409 if (!iflib_is_running(ctx))
4410 return;
4411 #ifdef DEV_NETMAP
4412 if ((if_getcapenable(ifp) & IFCAP_NETMAP) &&
4413 netmap_tx_irq(ifp, txq->ift_id))
4414 goto skip_ifmp;
4415 #endif
4416 if (ctx->ifc_sysctl_simple_tx) {
4417 iflib_simple_txq_drain(txq);
4418 goto skip_ifmp;
4419 }
4420 #ifdef ALTQ
4421 if (if_altq_is_enabled(ifp))
4422 iflib_altq_if_start(ifp);
4423 #endif
4424 if (txq->ift_db_pending)
4425 ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate);
4426 else if (!abdicate)
4427 ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
4428 /*
4429 * When abdicating, we always need to check drainage, not just when we don't enqueue
4430 */
4431 if (abdicate)
4432 ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
4433
4434 skip_ifmp:
4435 if (ctx->ifc_flags & IFC_LEGACY)
4436 IFDI_INTR_ENABLE(ctx);
4437 else
4438 IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
4439 }
4440
4441 static void
_task_fn_rx(void * context)4442 _task_fn_rx(void *context)
4443 {
4444 iflib_rxq_t rxq = context;
4445 if_ctx_t ctx = rxq->ifr_ctx;
4446 uint8_t more;
4447 uint16_t budget;
4448 #ifdef DEV_NETMAP
4449 u_int work = 0;
4450 int nmirq;
4451 #endif
4452
4453 #ifdef IFLIB_DIAGNOSTICS
4454 rxq->ifr_cpu_exec_count[curcpu]++;
4455 #endif
4456 DBG_COUNTER_INC(task_fn_rxs);
4457 if (__predict_false(!iflib_is_running(ctx)))
4458 return;
4459 #ifdef DEV_NETMAP
4460 nmirq = netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work);
4461 if (nmirq != NM_IRQ_PASS) {
4462 more = (nmirq == NM_IRQ_RESCHED) ? IFLIB_RXEOF_MORE : 0;
4463 goto skip_rxeof;
4464 }
4465 #endif
4466 budget = ctx->ifc_sysctl_rx_budget;
4467 if (budget == 0)
4468 budget = 16; /* XXX */
4469 more = iflib_rxeof(rxq, budget);
4470 #ifdef DEV_NETMAP
4471 skip_rxeof:
4472 #endif
4473 if ((more & IFLIB_RXEOF_MORE) == 0) {
4474 if (ctx->ifc_flags & IFC_LEGACY)
4475 IFDI_INTR_ENABLE(ctx);
4476 else
4477 IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
4478 DBG_COUNTER_INC(rx_intr_enables);
4479 }
4480 if (__predict_false(!iflib_is_running(ctx)))
4481 return;
4482
4483 if (more & IFLIB_RXEOF_MORE)
4484 GROUPTASK_ENQUEUE(&rxq->ifr_task);
4485 else if (more & IFLIB_RXEOF_EMPTY)
4486 callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq);
4487 }
4488
4489 static void
_task_fn_admin(void * context,int pending)4490 _task_fn_admin(void *context, int pending)
4491 {
4492 if_ctx_t ctx = context;
4493 if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
4494 iflib_txq_t txq;
4495 int i;
4496 bool do_reset, do_reset_if_up, do_watchdog;
4497 bool in_detach;
4498
4499 STATE_LOCK(ctx);
4500 do_reset = (ctx->ifc_flags & IFC_DO_RESET);
4501 do_reset_if_up = (ctx->ifc_flags & IFC_DO_RESET_IF_UP);
4502 do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG);
4503 in_detach = (ctx->ifc_flags & IFC_IN_DETACH);
4504 ctx->ifc_flags &= ~(IFC_DO_RESET | IFC_DO_RESET_IF_UP |
4505 IFC_DO_WATCHDOG);
4506 STATE_UNLOCK(ctx);
4507
4508 if (in_detach)
4509 return;
4510 KFAIL_POINT_CODE_COND(_debug_fail_point_iflib,
4511 admin_task_after_detach_check,
4512 iflib_admin_task_fail_device[0] != '\0' &&
4513 strcmp(device_get_nameunit(ctx->ifc_dev),
4514 iflib_admin_task_fail_device) == 0, FAIL_POINT_NONSLEEPABLE, {});
4515
4516 CTX_LOCK(ctx);
4517 if (ctx->ifc_pm_state != IFLIB_PM_ACTIVE ||
4518 !iflib_admin_enabled(ctx)) {
4519 CTX_UNLOCK(ctx);
4520 return;
4521 }
4522 if (!do_reset && do_reset_if_up &&
4523 (if_getflags(ctx->ifc_ifp) & IFF_UP) != 0)
4524 do_reset = true;
4525 for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
4526 CALLOUT_LOCK(txq);
4527 callout_stop(&txq->ift_timer);
4528 CALLOUT_UNLOCK(txq);
4529 }
4530 if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_ADMINCQ)
4531 IFDI_ADMIN_COMPLETION_HANDLE(ctx);
4532 if (do_watchdog) {
4533 ctx->ifc_tx_watchdog_events++;
4534 IFDI_WATCHDOG_RESET(ctx);
4535 }
4536 IFDI_UPDATE_ADMIN_STATUS(ctx);
4537 for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
4538 callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq,
4539 txq->ift_timer.c_cpu);
4540 }
4541 IFDI_LINK_INTR_ENABLE(ctx);
4542 if (do_reset)
4543 iflib_if_init_locked(ctx);
4544 CTX_UNLOCK(ctx);
4545
4546 if (LINK_ACTIVE(ctx) == 0)
4547 return;
4548 for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
4549 iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
4550 }
4551
4552 static void
_task_fn_iov(void * context,int pending)4553 _task_fn_iov(void *context, int pending)
4554 {
4555 if_ctx_t ctx = context;
4556
4557 if (iflib_in_detach(ctx))
4558 return;
4559 CTX_LOCK(ctx);
4560 if (ctx->ifc_pm_state != IFLIB_PM_ACTIVE ||
4561 (!iflib_is_running(ctx) &&
4562 !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))) {
4563 CTX_UNLOCK(ctx);
4564 return;
4565 }
4566 IFDI_VFLR_HANDLE(ctx);
4567 CTX_UNLOCK(ctx);
4568 }
4569
4570 static int
iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)4571 iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
4572 {
4573 int err;
4574 if_int_delay_info_t info;
4575 if_ctx_t ctx;
4576
4577 info = (if_int_delay_info_t)arg1;
4578 ctx = info->iidi_ctx;
4579 info->iidi_req = req;
4580 info->iidi_oidp = oidp;
4581 CTX_LOCK(ctx);
4582 err = IFDI_SYSCTL_INT_DELAY(ctx, info);
4583 CTX_UNLOCK(ctx);
4584 return (err);
4585 }
4586
4587 /*********************************************************************
4588 *
4589 * IFNET FUNCTIONS
4590 *
4591 **********************************************************************/
4592
4593 static void
iflib_if_init_locked(if_ctx_t ctx)4594 iflib_if_init_locked(if_ctx_t ctx)
4595 {
4596 if (ctx->ifc_pm_state != IFLIB_PM_ACTIVE)
4597 return;
4598 if (ctx->ifc_datapath_state != IFLIB_DP_STOPPED)
4599 iflib_stop(ctx);
4600 iflib_init_locked(ctx);
4601 }
4602
4603 static void
iflib_if_init(void * arg)4604 iflib_if_init(void *arg)
4605 {
4606 if_ctx_t ctx = arg;
4607
4608 CTX_LOCK(ctx);
4609 iflib_if_init_locked(ctx);
4610 CTX_UNLOCK(ctx);
4611 }
4612
4613 static int
iflib_if_transmit(if_t ifp,struct mbuf * m)4614 iflib_if_transmit(if_t ifp, struct mbuf *m)
4615 {
4616 if_ctx_t ctx = if_getsoftc(ifp);
4617 iflib_txq_t txq;
4618 int err, qidx;
4619 int abdicate;
4620
4621 if (__predict_false(!iflib_is_running(ctx) || !LINK_ACTIVE(ctx))) {
4622 DBG_COUNTER_INC(tx_frees);
4623 m_freem(m);
4624 return (ENETDOWN);
4625 }
4626
4627 MPASS(m->m_nextpkt == NULL);
4628 /* ALTQ-enabled interfaces always use queue 0. */
4629 qidx = 0;
4630 /* Use driver-supplied queue selection method if it exists */
4631 if (ctx->isc_txq_select_v2) {
4632 struct if_pkt_info pi;
4633 uint64_t early_pullups = 0;
4634 memset(&pi, 0, sizeof(pi));
4635
4636 err = iflib_parse_header_partial(&pi, &m, &early_pullups);
4637 if (__predict_false(err != 0)) {
4638 /* Assign pullups for bad pkts to default queue */
4639 ctx->ifc_txqs[0].ift_pullups += early_pullups;
4640 DBG_COUNTER_INC(encap_txd_encap_fail);
4641 return (err);
4642 }
4643 /* Let driver make queueing decision */
4644 qidx = ctx->isc_txq_select_v2(ctx->ifc_softc, m, &pi);
4645 ctx->ifc_txqs[qidx].ift_pullups += early_pullups;
4646 }
4647 /* Backwards compatibility w/ simpler queue select */
4648 else if (ctx->isc_txq_select)
4649 qidx = ctx->isc_txq_select(ctx->ifc_softc, m);
4650 /* If not, use iflib's standard method */
4651 else if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !if_altq_is_enabled(ifp))
4652 qidx = QIDX(ctx, m);
4653
4654 /* Set TX queue */
4655 txq = &ctx->ifc_txqs[qidx];
4656
4657 #ifdef DRIVER_BACKPRESSURE
4658 if (txq->ift_closed) {
4659 while (m != NULL) {
4660 next = m->m_nextpkt;
4661 m->m_nextpkt = NULL;
4662 m_freem(m);
4663 DBG_COUNTER_INC(tx_frees);
4664 m = next;
4665 }
4666 return (ENOBUFS);
4667 }
4668 #endif
4669 #ifdef notyet
4670 qidx = count = 0;
4671 mp = marr;
4672 next = m;
4673 do {
4674 count++;
4675 next = next->m_nextpkt;
4676 } while (next != NULL);
4677
4678 if (count > nitems(marr))
4679 if ((mp = malloc(count * sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
4680 /* XXX check nextpkt */
4681 m_freem(m);
4682 /* XXX simplify for now */
4683 DBG_COUNTER_INC(tx_frees);
4684 return (ENOBUFS);
4685 }
4686 for (next = m, i = 0; next != NULL; i++) {
4687 mp[i] = next;
4688 next = next->m_nextpkt;
4689 mp[i]->m_nextpkt = NULL;
4690 }
4691 #endif
4692 DBG_COUNTER_INC(tx_seen);
4693 abdicate = ctx->ifc_sysctl_tx_abdicate;
4694
4695 err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate);
4696
4697 if (abdicate)
4698 GROUPTASK_ENQUEUE(&txq->ift_task);
4699 if (err) {
4700 if (!abdicate)
4701 GROUPTASK_ENQUEUE(&txq->ift_task);
4702 /* support forthcoming later */
4703 #ifdef DRIVER_BACKPRESSURE
4704 txq->ift_closed = TRUE;
4705 #endif
4706 ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
4707 m_freem(m);
4708 DBG_COUNTER_INC(tx_frees);
4709 if (err == ENOBUFS)
4710 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
4711 else
4712 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4713 }
4714
4715 return (err);
4716 }
4717
4718 #ifdef ALTQ
4719 /*
4720 * The overall approach to integrating iflib with ALTQ is to continue to use
4721 * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware
4722 * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring
4723 * is redundant/unnecessary, but doing so minimizes the amount of
4724 * ALTQ-specific code required in iflib. It is assumed that the overhead of
4725 * redundantly queueing to an intermediate mp_ring is swamped by the
4726 * performance limitations inherent in using ALTQ.
4727 *
4728 * When ALTQ support is compiled in, all iflib drivers will use a transmit
4729 * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the
4730 * given interface. If ALTQ is enabled for an interface, then all
4731 * transmitted packets for that interface will be submitted to the ALTQ
4732 * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit()
4733 * implementation because it uses IFQ_HANDOFF(), which will duplicatively
4734 * update stats that the iflib machinery handles, and which is sensitve to
4735 * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start()
4736 * will be installed as the start routine for use by ALTQ facilities that
4737 * need to trigger queue drains on a scheduled basis.
4738 *
4739 */
4740 static void
iflib_altq_if_start(if_t ifp)4741 iflib_altq_if_start(if_t ifp)
4742 {
4743 struct ifaltq *ifq = &ifp->if_snd; /* XXX - DRVAPI */
4744 struct mbuf *m;
4745
4746 IFQ_LOCK(ifq);
4747 IFQ_DEQUEUE_NOLOCK(ifq, m);
4748 while (m != NULL) {
4749 iflib_if_transmit(ifp, m);
4750 IFQ_DEQUEUE_NOLOCK(ifq, m);
4751 }
4752 IFQ_UNLOCK(ifq);
4753 }
4754
4755 static int
iflib_altq_if_transmit(if_t ifp,struct mbuf * m)4756 iflib_altq_if_transmit(if_t ifp, struct mbuf *m)
4757 {
4758 int err;
4759
4760 if (if_altq_is_enabled(ifp)) {
4761 IFQ_ENQUEUE(&ifp->if_snd, m, err); /* XXX - DRVAPI */
4762 if (err == 0)
4763 if_start(ifp);
4764 return (err);
4765 }
4766 return (iflib_if_transmit(ifp, m));
4767 }
4768 #endif /* ALTQ */
4769
4770 static void
iflib_if_qflush(if_t ifp)4771 iflib_if_qflush(if_t ifp)
4772 {
4773 if_ctx_t ctx = if_getsoftc(ifp);
4774 iflib_txq_t txq = ctx->ifc_txqs;
4775 int i;
4776
4777 STATE_LOCK(ctx);
4778 ctx->ifc_flags |= IFC_QFLUSH;
4779 STATE_UNLOCK(ctx);
4780 for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
4781 if (txq->ift_drbr != NULL) {
4782 mtx_lock(&txq->ift_mtx);
4783 drbr_flush(ifp, txq->ift_drbr);
4784 mtx_unlock(&txq->ift_mtx);
4785 continue;
4786 }
4787 while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
4788 iflib_txq_check_drain(txq, 0);
4789 }
4790 STATE_LOCK(ctx);
4791 ctx->ifc_flags &= ~IFC_QFLUSH;
4792 STATE_UNLOCK(ctx);
4793
4794 /*
4795 * When ALTQ is enabled, this will also take care of purging the
4796 * ALTQ queue(s).
4797 */
4798 if_qflush(ifp);
4799 }
4800
4801 #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
4802 IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
4803 IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \
4804 IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_MEXTPG)
4805
4806 static int
iflib_if_ioctl(if_t ifp,u_long command,caddr_t data)4807 iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
4808 {
4809 if_ctx_t ctx = if_getsoftc(ifp);
4810 struct ifreq *ifr = (struct ifreq *)data;
4811 #if defined(INET) || defined(INET6)
4812 struct ifaddr *ifa = (struct ifaddr *)data;
4813 #endif
4814 bool avoid_reset = false, restart;
4815 int err = 0, reinit = 0;
4816
4817 switch (command) {
4818 case SIOCSIFADDR:
4819 #ifdef INET
4820 if (ifa->ifa_addr->sa_family == AF_INET)
4821 avoid_reset = true;
4822 #endif
4823 #ifdef INET6
4824 if (ifa->ifa_addr->sa_family == AF_INET6)
4825 avoid_reset = true;
4826 #endif
4827 /*
4828 * Calling init results in link renegotiation,
4829 * so we avoid doing it when possible.
4830 */
4831 if (avoid_reset) {
4832 if_setflagbits(ifp, IFF_UP, 0);
4833 if (!iflib_is_running(ctx))
4834 reinit = 1;
4835 #ifdef INET
4836 if (!(if_getflags(ifp) & IFF_NOARP))
4837 arp_ifinit(ifp, ifa);
4838 #endif
4839 } else
4840 err = ether_ioctl(ifp, command, data);
4841 break;
4842 case SIOCSIFMTU:
4843 CTX_LOCK(ctx);
4844 if (ifr->ifr_mtu == if_getmtu(ifp)) {
4845 CTX_UNLOCK(ctx);
4846 break;
4847 }
4848 restart = ctx->ifc_datapath_state == IFLIB_DP_RUNNING ||
4849 (if_getflags(ifp) & IFF_UP) != 0;
4850 /* Quiesce a datapath whose stopped state is not established. */
4851 if (ctx->ifc_datapath_state != IFLIB_DP_STOPPED)
4852 iflib_stop(ctx);
4853
4854 if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
4855 STATE_LOCK(ctx);
4856 if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
4857 ctx->ifc_flags |= IFC_MULTISEG;
4858 else
4859 ctx->ifc_flags &= ~IFC_MULTISEG;
4860 STATE_UNLOCK(ctx);
4861 err = if_setmtu(ifp, ifr->ifr_mtu);
4862 }
4863 if (restart)
4864 iflib_init_locked(ctx);
4865 CTX_UNLOCK(ctx);
4866 break;
4867 case SIOCSIFFLAGS:
4868 CTX_LOCK(ctx);
4869 if (if_getflags(ifp) & IFF_UP) {
4870 if (iflib_is_running(ctx)) {
4871 if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
4872 (IFF_PROMISC | IFF_ALLMULTI)) {
4873 CTX_UNLOCK(ctx);
4874 err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
4875 CTX_LOCK(ctx);
4876 }
4877 } else
4878 reinit = 1;
4879 } else if (ctx->ifc_datapath_state != IFLIB_DP_STOPPED) {
4880 /* Stop partially initialized hardware as well as running queues. */
4881 iflib_stop(ctx);
4882 }
4883 ctx->ifc_if_flags = if_getflags(ifp);
4884 CTX_UNLOCK(ctx);
4885 break;
4886 case SIOCADDMULTI:
4887 case SIOCDELMULTI:
4888 CTX_LOCK(ctx);
4889 if (iflib_is_running(ctx)) {
4890 IFDI_INTR_DISABLE(ctx);
4891 IFDI_MULTI_SET(ctx);
4892 IFDI_INTR_ENABLE(ctx);
4893 }
4894 CTX_UNLOCK(ctx);
4895 break;
4896 case SIOCSIFMEDIA:
4897 CTX_LOCK(ctx);
4898 IFDI_MEDIA_SET(ctx);
4899 CTX_UNLOCK(ctx);
4900 /* FALLTHROUGH */
4901 case SIOCGIFMEDIA:
4902 case SIOCGIFXMEDIA:
4903 err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command);
4904 break;
4905 case SIOCGI2C:
4906 /* FALLTHROUGH */
4907 case SIOCGI2CPB:
4908 {
4909 struct ifi2creq i2c;
4910 if_shared_ctx_t sctx = ctx->ifc_sctx;
4911
4912 err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
4913 if (err != 0)
4914 break;
4915 if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
4916 err = EINVAL;
4917 break;
4918 }
4919 if (i2c.len > sizeof(i2c.data)) {
4920 err = EINVAL;
4921 break;
4922 }
4923 if (command == SIOCGI2C) {
4924 i2c.page = i2c.bank = 0;
4925 } else if ((sctx->isc_flags & IFLIB_I2C_PAGE_BANK) == 0) {
4926 err = EINVAL;
4927 break;
4928 }
4929
4930 if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
4931 err = copyout(&i2c, ifr_data_get_ptr(ifr),
4932 sizeof(i2c));
4933 break;
4934 }
4935 case SIOCSIFCAP:
4936 {
4937 int mask, setmask, oldmask;
4938
4939 oldmask = if_getcapenable(ifp);
4940 mask = ifr->ifr_reqcap ^ oldmask;
4941 mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_MEXTPG;
4942 setmask = 0;
4943 #ifdef TCP_OFFLOAD
4944 setmask |= mask & (IFCAP_TOE4 | IFCAP_TOE6);
4945 #endif
4946 setmask |= (mask & IFCAP_FLAGS);
4947 setmask |= (mask & IFCAP_WOL);
4948
4949 /*
4950 * If any RX csum has changed, change all the ones that
4951 * are supported by the driver.
4952 */
4953 if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
4954 setmask |= ctx->ifc_softc_ctx.isc_capabilities &
4955 (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
4956 }
4957
4958 /*
4959 * want to ensure that traffic has stopped before we change any of the flags
4960 */
4961 if (setmask) {
4962 CTX_LOCK(ctx);
4963 restart = (setmask & ~IFCAP_WOL) != 0 &&
4964 (ctx->ifc_datapath_state == IFLIB_DP_RUNNING ||
4965 (if_getflags(ifp) & IFF_UP) != 0);
4966 if (restart)
4967 iflib_stop(ctx);
4968 STATE_LOCK(ctx);
4969 if_togglecapenable(ifp, setmask);
4970 ctx->ifc_softc_ctx.isc_capenable ^= setmask;
4971 STATE_UNLOCK(ctx);
4972 if (restart)
4973 iflib_init_locked(ctx);
4974 CTX_UNLOCK(ctx);
4975 }
4976 if_vlancap(ifp);
4977 break;
4978 }
4979 case SIOCGIFRSSKEY:
4980 CTX_LOCK(ctx);
4981 err = IFDI_GET_RSS_KEY(ctx, (struct ifrsskey *)data);
4982 CTX_UNLOCK(ctx);
4983 break;
4984 case SIOCGIFRSSHASH:
4985 CTX_LOCK(ctx);
4986 err = IFDI_GET_RSS_HASH(ctx, (struct ifrsshash *)data);
4987 CTX_UNLOCK(ctx);
4988 break;
4989 case SIOCGPRIVATE_0:
4990 case SIOCSDRVSPEC:
4991 case SIOCGDRVSPEC:
4992 CTX_LOCK(ctx);
4993 err = IFDI_PRIV_IOCTL(ctx, command, data);
4994 CTX_UNLOCK(ctx);
4995 break;
4996 case SIOCGIFDOWNREASON:
4997 CTX_LOCK(ctx);
4998 err = IFDI_GET_DOWNREASON(ctx, (struct ifdownreason *)data);
4999 CTX_UNLOCK(ctx);
5000 break;
5001 default:
5002 err = ether_ioctl(ifp, command, data);
5003 break;
5004 }
5005 if (reinit)
5006 iflib_if_init(ctx);
5007 return (err);
5008 }
5009
5010 static int
iflib_if_vf_status(if_t ifp,struct if_vf_status ** statusp)5011 iflib_if_vf_status(if_t ifp, struct if_vf_status **statusp)
5012 {
5013 if_ctx_t ctx;
5014 int error;
5015
5016 ctx = if_getsoftc(ifp);
5017 CTX_LOCK(ctx);
5018 error = IFDI_VF_STATUS(ctx, statusp);
5019 CTX_UNLOCK(ctx);
5020 return (error);
5021 }
5022
5023 static uint64_t
iflib_if_get_counter(if_t ifp,ift_counter cnt)5024 iflib_if_get_counter(if_t ifp, ift_counter cnt)
5025 {
5026 if_ctx_t ctx = if_getsoftc(ifp);
5027
5028 return (IFDI_GET_COUNTER(ctx, cnt));
5029 }
5030
5031 /*********************************************************************
5032 *
5033 * OTHER FUNCTIONS EXPORTED TO THE STACK
5034 *
5035 **********************************************************************/
5036
5037 static void
iflib_vlan_register(void * arg,if_t ifp,uint16_t vtag)5038 iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
5039 {
5040 if_ctx_t ctx = if_getsoftc(ifp);
5041 bool restart;
5042
5043 if ((void *)ctx != arg)
5044 return;
5045
5046 if ((vtag == 0) || (vtag > 4095))
5047 return;
5048
5049 if (iflib_in_detach(ctx))
5050 return;
5051
5052 CTX_LOCK(ctx);
5053 restart = IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG) &&
5054 ((if_getflags(ifp) & IFF_UP) != 0 ||
5055 ctx->ifc_datapath_state == IFLIB_DP_RUNNING);
5056 /* Driver may need all untagged packets to be flushed */
5057 if (restart)
5058 iflib_stop(ctx);
5059 IFDI_VLAN_REGISTER(ctx, vtag);
5060 /* Re-init to load the changes, if required */
5061 if (restart)
5062 iflib_init_locked(ctx);
5063 CTX_UNLOCK(ctx);
5064 }
5065
5066 static void
iflib_vlan_unregister(void * arg,if_t ifp,uint16_t vtag)5067 iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
5068 {
5069 if_ctx_t ctx = if_getsoftc(ifp);
5070 bool restart;
5071
5072 if ((void *)ctx != arg)
5073 return;
5074
5075 if ((vtag == 0) || (vtag > 4095))
5076 return;
5077
5078 CTX_LOCK(ctx);
5079 restart = IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG) &&
5080 ((if_getflags(ifp) & IFF_UP) != 0 ||
5081 ctx->ifc_datapath_state == IFLIB_DP_RUNNING);
5082 /* Driver may need all tagged packets to be flushed */
5083 if (restart)
5084 iflib_stop(ctx);
5085 IFDI_VLAN_UNREGISTER(ctx, vtag);
5086 /* Re-init to load the changes, if required */
5087 if (restart)
5088 iflib_init_locked(ctx);
5089 CTX_UNLOCK(ctx);
5090 }
5091
5092 static void
_task_fn_led(void * context,int pending __unused)5093 _task_fn_led(void *context, int pending __unused)
5094 {
5095 if_ctx_t ctx = context;
5096 bool in_detach;
5097 int onoff;
5098
5099 STATE_LOCK(ctx);
5100 in_detach = (ctx->ifc_flags & IFC_IN_DETACH) != 0;
5101 onoff = ctx->ifc_led_state;
5102 STATE_UNLOCK(ctx);
5103 if (in_detach)
5104 return;
5105
5106 CTX_LOCK(ctx);
5107 if (ctx->ifc_pm_state == IFLIB_PM_ACTIVE)
5108 IFDI_LED_FUNC(ctx, onoff);
5109 CTX_UNLOCK(ctx);
5110 }
5111
5112 static void
iflib_led_func(void * arg,int onoff)5113 iflib_led_func(void *arg, int onoff)
5114 {
5115 if_ctx_t ctx = arg;
5116 bool in_detach;
5117
5118 /* led(4) may invoke this callback from a non-sleepable callout. */
5119 STATE_LOCK(ctx);
5120 ctx->ifc_led_state = onoff;
5121 in_detach = (ctx->ifc_flags & IFC_IN_DETACH) != 0;
5122 STATE_UNLOCK(ctx);
5123 if (!in_detach)
5124 taskqueue_enqueue(ctx->ifc_tq, &ctx->ifc_led_task);
5125 }
5126
5127 /*********************************************************************
5128 *
5129 * BUS FUNCTION DEFINITIONS
5130 *
5131 **********************************************************************/
5132
5133 int
iflib_device_probe(device_t dev)5134 iflib_device_probe(device_t dev)
5135 {
5136 const pci_vendor_info_t *ent;
5137 if_shared_ctx_t sctx;
5138 uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id;
5139 uint16_t pci_vendor_id;
5140
5141 if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
5142 return (ENOTSUP);
5143
5144 pci_vendor_id = pci_get_vendor(dev);
5145 pci_device_id = pci_get_device(dev);
5146 pci_subvendor_id = pci_get_subvendor(dev);
5147 pci_subdevice_id = pci_get_subdevice(dev);
5148 pci_rev_id = pci_get_revid(dev);
5149 if (sctx->isc_parse_devinfo != NULL)
5150 sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
5151
5152 ent = sctx->isc_vendor_info;
5153 while (ent->pvi_vendor_id != 0) {
5154 if (pci_vendor_id != ent->pvi_vendor_id) {
5155 ent++;
5156 continue;
5157 }
5158 if ((pci_device_id == ent->pvi_device_id) &&
5159 ((pci_subvendor_id == ent->pvi_subvendor_id) ||
5160 (ent->pvi_subvendor_id == 0)) &&
5161 ((pci_subdevice_id == ent->pvi_subdevice_id) ||
5162 (ent->pvi_subdevice_id == 0)) &&
5163 ((pci_rev_id == ent->pvi_rev_id) ||
5164 (ent->pvi_rev_id == 0))) {
5165 device_set_desc_copy(dev, ent->pvi_name);
5166 /* this needs to be changed to zero if the bus probing code
5167 * ever stops re-probing on best match because the sctx
5168 * may have its values over written by register calls
5169 * in subsequent probes
5170 */
5171 return (BUS_PROBE_DEFAULT);
5172 }
5173 ent++;
5174 }
5175 return (ENXIO);
5176 }
5177
5178 int
iflib_device_probe_vendor(device_t dev)5179 iflib_device_probe_vendor(device_t dev)
5180 {
5181 int probe;
5182
5183 probe = iflib_device_probe(dev);
5184 if (probe == BUS_PROBE_DEFAULT)
5185 return (BUS_PROBE_VENDOR);
5186 else
5187 return (probe);
5188 }
5189
5190 static void
iflib_reset_qvalues(if_ctx_t ctx)5191 iflib_reset_qvalues(if_ctx_t ctx)
5192 {
5193 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
5194 if_shared_ctx_t sctx = ctx->ifc_sctx;
5195 device_t dev = ctx->ifc_dev;
5196 int i;
5197
5198 if (ctx->ifc_sysctl_ntxqs != 0)
5199 scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
5200 if (ctx->ifc_sysctl_nrxqs != 0)
5201 scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
5202
5203 for (i = 0; i < sctx->isc_ntxqs; i++) {
5204 if (ctx->ifc_sysctl_ntxds[i] != 0)
5205 scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
5206 else
5207 scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
5208 }
5209
5210 for (i = 0; i < sctx->isc_nrxqs; i++) {
5211 if (ctx->ifc_sysctl_nrxds[i] != 0)
5212 scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
5213 else
5214 scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
5215 }
5216
5217 for (i = 0; i < sctx->isc_nrxqs; i++) {
5218 if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
5219 device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
5220 i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
5221 scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
5222 }
5223 if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
5224 device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
5225 i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
5226 scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
5227 }
5228 if (!powerof2(scctx->isc_nrxd[i])) {
5229 device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n",
5230 i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]);
5231 scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
5232 }
5233 }
5234
5235 for (i = 0; i < sctx->isc_ntxqs; i++) {
5236 if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
5237 device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
5238 i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
5239 scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
5240 }
5241 if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
5242 device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
5243 i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
5244 scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
5245 }
5246 if (!powerof2(scctx->isc_ntxd[i])) {
5247 device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n",
5248 i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]);
5249 scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
5250 }
5251 }
5252 scctx->isc_tx_pad = 2;
5253 }
5254
5255 static void
iflib_add_pfil(if_ctx_t ctx)5256 iflib_add_pfil(if_ctx_t ctx)
5257 {
5258 struct pfil_head *pfil;
5259 struct pfil_head_args pa;
5260 iflib_rxq_t rxq;
5261 int i;
5262
5263 pa.pa_version = PFIL_VERSION;
5264 pa.pa_flags = PFIL_IN;
5265 pa.pa_type = PFIL_TYPE_ETHERNET;
5266 pa.pa_headname = if_name(ctx->ifc_ifp);
5267 pfil = pfil_head_register(&pa);
5268
5269 for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
5270 rxq->pfil = pfil;
5271 }
5272 }
5273
5274 static void
iflib_rem_pfil(if_ctx_t ctx)5275 iflib_rem_pfil(if_ctx_t ctx)
5276 {
5277 struct pfil_head *pfil;
5278 iflib_rxq_t rxq;
5279 int i;
5280
5281 rxq = ctx->ifc_rxqs;
5282 pfil = rxq->pfil;
5283 for (i = 0; i < NRXQSETS(ctx); i++, rxq++) {
5284 rxq->pfil = NULL;
5285 }
5286 pfil_head_unregister(pfil);
5287 }
5288
5289
5290 /*
5291 * Advance forward by n members of the cpuset ctx->ifc_cpus starting from
5292 * cpuid and wrapping as necessary.
5293 */
5294 static unsigned int
cpuid_advance(if_ctx_t ctx,unsigned int cpuid,unsigned int n)5295 cpuid_advance(if_ctx_t ctx, unsigned int cpuid, unsigned int n)
5296 {
5297 unsigned int first_valid;
5298 unsigned int last_valid;
5299
5300 /* cpuid should always be in the valid set */
5301 MPASS(CPU_ISSET(cpuid, &ctx->ifc_cpus));
5302
5303 /* valid set should never be empty */
5304 MPASS(!CPU_EMPTY(&ctx->ifc_cpus));
5305
5306 first_valid = CPU_FFS(&ctx->ifc_cpus) - 1;
5307 last_valid = CPU_FLS(&ctx->ifc_cpus) - 1;
5308 n = n % CPU_COUNT(&ctx->ifc_cpus);
5309 while (n > 0) {
5310 do {
5311 cpuid++;
5312 if (cpuid > last_valid)
5313 cpuid = first_valid;
5314 } while (!CPU_ISSET(cpuid, &ctx->ifc_cpus));
5315 n--;
5316 }
5317
5318 return (cpuid);
5319 }
5320
5321 /*
5322 * CPU mapping behaviors
5323 * ---------------------
5324 * 'separate txrx' refers to the separate_txrx sysctl
5325 * 'use logical' refers to the use_logical_cores sysctl
5326 * 'INTR CPUS' indicates whether bus_get_cpus(INTR_CPUS) succeeded
5327 *
5328 * separate use INTR
5329 * txrx logical CPUS result
5330 * ---------- --------- ------ ------------------------------------------------
5331 * - - X RX and TX queues mapped to consecutive physical
5332 * cores with RX/TX pairs on same core and excess
5333 * of either following
5334 * - X X RX and TX queues mapped to consecutive cores
5335 * of any type with RX/TX pairs on same core and
5336 * excess of either following
5337 * X - X RX and TX queues mapped to consecutive physical
5338 * cores; all RX then all TX
5339 * X X X RX queues mapped to consecutive physical cores
5340 * first, then TX queues mapped to L2 neighbor of
5341 * the corresponding RX queue if one exists,
5342 * otherwise to consecutive physical cores
5343 * - n/a - RX and TX queues mapped to consecutive cores of
5344 * any type with RX/TX pairs on same core and excess
5345 * of either following
5346 * X n/a - RX and TX queues mapped to consecutive cores of
5347 * any type; all RX then all TX
5348 */
5349 static unsigned int
get_cpuid_for_queue(if_ctx_t ctx,unsigned int base_cpuid,unsigned int qid,bool is_tx)5350 get_cpuid_for_queue(if_ctx_t ctx, unsigned int base_cpuid, unsigned int qid,
5351 bool is_tx)
5352 {
5353 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
5354 unsigned int core_index;
5355
5356 if (ctx->ifc_sysctl_separate_txrx) {
5357 /*
5358 * When using separate CPUs for TX and RX, the assignment
5359 * will always be of a consecutive CPU out of the set of
5360 * context CPUs, except for the specific case where the
5361 * context CPUs are phsyical cores, the use of logical cores
5362 * has been enabled, the assignment is for TX, the TX qid
5363 * corresponds to an RX qid, and the CPU assigned to the
5364 * corresponding RX queue has an L2 neighbor.
5365 */
5366 if (ctx->ifc_sysctl_use_logical_cores &&
5367 ctx->ifc_cpus_are_physical_cores &&
5368 is_tx && qid < scctx->isc_nrxqsets) {
5369 int l2_neighbor;
5370 unsigned int rx_cpuid;
5371
5372 rx_cpuid = cpuid_advance(ctx, base_cpuid, qid);
5373 l2_neighbor = sched_find_l2_neighbor(rx_cpuid);
5374 if (l2_neighbor != -1) {
5375 return (l2_neighbor);
5376 }
5377 /*
5378 * ... else fall through to the normal
5379 * consecutive-after-RX assignment scheme.
5380 *
5381 * Note that we are assuming that all RX queue CPUs
5382 * have an L2 neighbor, or all do not. If a mixed
5383 * scenario is possible, we will have to keep track
5384 * separately of how many queues prior to this one
5385 * were not able to be assigned to an L2 neighbor.
5386 */
5387 }
5388 if (is_tx)
5389 core_index = scctx->isc_nrxqsets + qid;
5390 else
5391 core_index = qid;
5392 } else {
5393 core_index = qid;
5394 }
5395
5396 return (cpuid_advance(ctx, base_cpuid, core_index));
5397 }
5398
5399 static uint16_t
get_ctx_core_offset(if_ctx_t ctx)5400 get_ctx_core_offset(if_ctx_t ctx)
5401 {
5402 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
5403 struct cpu_offset *op;
5404 cpuset_t assigned_cpus;
5405 unsigned int cores_consumed;
5406 unsigned int base_cpuid = ctx->ifc_sysctl_core_offset;
5407 unsigned int first_valid;
5408 unsigned int last_valid;
5409 unsigned int i;
5410
5411 MPASS(!ctx->ifc_core_offset_ref);
5412 first_valid = CPU_FFS(&ctx->ifc_cpus) - 1;
5413 last_valid = CPU_FLS(&ctx->ifc_cpus) - 1;
5414
5415 if (base_cpuid != CORE_OFFSET_UNSPECIFIED) {
5416 /*
5417 * Align the user-chosen base CPU ID to the next valid CPU
5418 * for this device. If the chosen base CPU ID is smaller
5419 * than the first valid CPU or larger than the last valid
5420 * CPU, we assume the user does not know what the valid
5421 * range is for this device and is thinking in terms of a
5422 * zero-based reference frame, and so we shift the given
5423 * value into the valid range (and wrap accordingly) so the
5424 * intent is translated to the proper frame of reference.
5425 * If the base CPU ID is within the valid first/last, but
5426 * does not correspond to a valid CPU, it is advanced to the
5427 * next valid CPU (wrapping if necessary).
5428 */
5429 if (base_cpuid < first_valid || base_cpuid > last_valid) {
5430 /* shift from zero-based to first_valid-based */
5431 base_cpuid += first_valid;
5432 /* wrap to range [first_valid, last_valid] */
5433 base_cpuid = (base_cpuid - first_valid) %
5434 (last_valid - first_valid + 1);
5435 }
5436 if (!CPU_ISSET(base_cpuid, &ctx->ifc_cpus)) {
5437 /*
5438 * base_cpuid is in [first_valid, last_valid], but
5439 * not a member of the valid set. In this case,
5440 * there will always be a member of the valid set
5441 * with a CPU ID that is greater than base_cpuid,
5442 * and we simply advance to it.
5443 */
5444 while (!CPU_ISSET(base_cpuid, &ctx->ifc_cpus))
5445 base_cpuid++;
5446 }
5447 return (base_cpuid);
5448 }
5449
5450 /*
5451 * Determine how many cores will be consumed by performing the CPU
5452 * assignments and counting how many of the assigned CPUs correspond
5453 * to CPUs in the set of context CPUs. This is done using the CPU
5454 * ID first_valid as the base CPU ID, as the base CPU must be within
5455 * the set of context CPUs.
5456 *
5457 * Note not all assigned CPUs will be in the set of context CPUs
5458 * when separate CPUs are being allocated to TX and RX queues,
5459 * assignment to logical cores has been enabled, the set of context
5460 * CPUs contains only physical CPUs, and TX queues are mapped to L2
5461 * neighbors of CPUs that RX queues have been mapped to - in this
5462 * case we do only want to count how many CPUs in the set of context
5463 * CPUs have been consumed, as that determines the next CPU in that
5464 * set to start allocating at for the next device for which
5465 * core_offset is not set.
5466 */
5467 CPU_ZERO(&assigned_cpus);
5468 for (i = 0; i < scctx->isc_ntxqsets; i++)
5469 CPU_SET(get_cpuid_for_queue(ctx, first_valid, i, true),
5470 &assigned_cpus);
5471 for (i = 0; i < scctx->isc_nrxqsets; i++)
5472 CPU_SET(get_cpuid_for_queue(ctx, first_valid, i, false),
5473 &assigned_cpus);
5474 CPU_AND(&assigned_cpus, &assigned_cpus, &ctx->ifc_cpus);
5475 cores_consumed = CPU_COUNT(&assigned_cpus);
5476
5477 mtx_lock(&cpu_offset_mtx);
5478 SLIST_FOREACH(op, &cpu_offsets, entries) {
5479 if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
5480 base_cpuid = op->next_cpuid;
5481 op->next_cpuid = cpuid_advance(ctx, op->next_cpuid,
5482 cores_consumed);
5483 MPASS(op->refcount < UINT_MAX);
5484 op->refcount++;
5485 ctx->ifc_core_offset_ref = true;
5486 break;
5487 }
5488 }
5489 if (base_cpuid == CORE_OFFSET_UNSPECIFIED) {
5490 base_cpuid = first_valid;
5491 op = malloc(sizeof(struct cpu_offset), M_IFLIB,
5492 M_NOWAIT | M_ZERO);
5493 if (op == NULL) {
5494 device_printf(ctx->ifc_dev,
5495 "allocation for cpu offset failed.\n");
5496 } else {
5497 op->next_cpuid = cpuid_advance(ctx, base_cpuid,
5498 cores_consumed);
5499 op->refcount = 1;
5500 CPU_COPY(&ctx->ifc_cpus, &op->set);
5501 SLIST_INSERT_HEAD(&cpu_offsets, op, entries);
5502 ctx->ifc_core_offset_ref = true;
5503 }
5504 }
5505 mtx_unlock(&cpu_offset_mtx);
5506
5507 return (base_cpuid);
5508 }
5509
5510 static void
unref_ctx_core_offset(if_ctx_t ctx)5511 unref_ctx_core_offset(if_ctx_t ctx)
5512 {
5513 struct cpu_offset *op, *top;
5514
5515 if (!ctx->ifc_core_offset_ref)
5516 return;
5517
5518 mtx_lock(&cpu_offset_mtx);
5519 SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) {
5520 if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
5521 MPASS(op->refcount > 0);
5522 op->refcount--;
5523 if (op->refcount == 0) {
5524 SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries);
5525 free(op, M_IFLIB);
5526 }
5527 ctx->ifc_core_offset_ref = false;
5528 break;
5529 }
5530 }
5531 mtx_unlock(&cpu_offset_mtx);
5532 MPASS(!ctx->ifc_core_offset_ref);
5533 }
5534
5535 static bool
iflib_register_fail_device_matches(device_t dev)5536 iflib_register_fail_device_matches(device_t dev)
5537 {
5538 const char *nameunit;
5539
5540 nameunit = device_get_nameunit(dev);
5541 return (iflib_register_fail_device[0] != '\0' && nameunit != NULL &&
5542 strcmp(nameunit, iflib_register_fail_device) == 0);
5543 }
5544
5545 #define IFLIB_REGISTER_FAIL_POINT(_dev, _name, _error, _label) do { \
5546 KFAIL_POINT_CODE_COND(_debug_fail_point_iflib, _name, \
5547 iflib_register_fail_device_matches((_dev)), \
5548 FAIL_POINT_NONSLEEPABLE, { \
5549 (_error) = RETURN_VALUE; \
5550 if ((_error) <= 0) \
5551 (_error) = EIO; \
5552 device_printf((_dev), \
5553 "injecting iflib registration failure at %s: %d\n", \
5554 #_name, (_error)); \
5555 goto _label; \
5556 }); \
5557 } while (0)
5558
5559 int
iflib_device_register(device_t dev,void * sc,if_shared_ctx_t sctx,if_ctx_t * ctxp)5560 iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
5561 {
5562 if_ctx_t ctx;
5563 if_t ifp;
5564 if_softc_ctx_t scctx;
5565 kobjop_desc_t kobj_desc;
5566 kobj_method_t *kobj_method;
5567 bool attach_pre_succeeded, intr_allocated, queues_allocated;
5568 int err, msix, rid;
5569 #ifdef PCI_IOV
5570 int iov_error;
5571 #endif
5572 int num_txd, num_rxd;
5573 char namebuf[TASKQUEUE_NAMELEN];
5574
5575 attach_pre_succeeded = false;
5576 intr_allocated = false;
5577 queues_allocated = false;
5578 ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK | M_ZERO);
5579 ctx->ifc_datapath_state = IFLIB_DP_UNKNOWN;
5580 ctx->ifc_pm_state = IFLIB_PM_ACTIVE;
5581
5582 if (sc == NULL) {
5583 sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK | M_ZERO);
5584 device_set_softc(dev, ctx);
5585 ctx->ifc_flags |= IFC_SC_ALLOCATED;
5586 }
5587
5588 ctx->ifc_sctx = sctx;
5589 ctx->ifc_dev = dev;
5590 ctx->ifc_softc = sc;
5591
5592 iflib_register(ctx);
5593 iflib_add_device_sysctl_pre(ctx);
5594
5595 scctx = &ctx->ifc_softc_ctx;
5596 ifp = ctx->ifc_ifp;
5597 if (ctx->ifc_sysctl_simple_tx) {
5598 /* if_start drives the same drbr drain when ALTQ is active. */
5599 if_settransmitfn(ifp, iflib_simple_transmit);
5600 if_setstartfn(ifp, iflib_simple_if_start);
5601 device_printf(dev, "using simple transmit\n");
5602 }
5603 iflib_reset_qvalues(ctx);
5604 CTX_LOCK(ctx);
5605 IFLIB_REGISTER_FAIL_POINT(dev, register_before_attach_pre, err,
5606 fail_cleanup);
5607 if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
5608 device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
5609 goto fail_cleanup;
5610 }
5611 attach_pre_succeeded = true;
5612 IFLIB_REGISTER_FAIL_POINT(dev, register_after_attach_pre, err,
5613 fail_cleanup);
5614 _iflib_pre_assert(scctx);
5615 ctx->ifc_txrx = *scctx->isc_txrx;
5616 /*
5617 * Bind optional queue selection to a transmit entry point once, rather
5618 * than testing for optional methods for every packet. Prefer v2 when
5619 * the driver provides both methods.
5620 */
5621 if (ctx->ifc_sysctl_simple_tx) {
5622 if (ctx->isc_txq_select_v2 != NULL)
5623 if_settransmitfn(ifp,
5624 iflib_simple_transmit_txq_select_v2);
5625 else if (ctx->isc_txq_select != NULL)
5626 if_settransmitfn(ifp,
5627 iflib_simple_transmit_txq_select);
5628 }
5629
5630 MPASS(scctx->isc_dma_width <= flsll(BUS_SPACE_MAXADDR));
5631
5632 if (sctx->isc_flags & IFLIB_DRIVER_MEDIA)
5633 ctx->ifc_mediap = scctx->isc_media;
5634
5635 #ifdef INVARIANTS
5636 if (scctx->isc_capabilities & IFCAP_TXCSUM)
5637 MPASS(scctx->isc_tx_csum_flags);
5638 #endif
5639
5640 if_setcapabilities(ifp,
5641 scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_MEXTPG);
5642 if_setcapenable(ifp,
5643 scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_MEXTPG);
5644
5645 if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
5646 scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
5647 if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
5648 scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
5649
5650 num_txd = iflib_num_tx_descs(ctx);
5651 num_rxd = iflib_num_rx_descs(ctx);
5652
5653 /* XXX change for per-queue sizes */
5654 device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n",
5655 num_txd, num_rxd);
5656
5657 if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION)
5658 scctx->isc_tx_nsegments = max(1, num_txd /
5659 MAX_SINGLE_PACKET_FRACTION);
5660 if (scctx->isc_tx_tso_segments_max > num_txd /
5661 MAX_SINGLE_PACKET_FRACTION)
5662 scctx->isc_tx_tso_segments_max = max(1,
5663 num_txd / MAX_SINGLE_PACKET_FRACTION);
5664
5665 /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
5666 if (if_getcapabilities(ifp) & IFCAP_TSO) {
5667 /*
5668 * The stack can't handle a TSO size larger than IP_MAXPACKET,
5669 * but some MACs do.
5670 */
5671 if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max,
5672 IP_MAXPACKET));
5673 /*
5674 * Take maximum number of m_pullup(9)'s in iflib_parse_header()
5675 * into account. In the worst case, each of these calls will
5676 * add another mbuf and, thus, the requirement for another DMA
5677 * segment. So for best performance, it doesn't make sense to
5678 * advertize a maximum of TSO segments that typically will
5679 * require defragmentation in iflib_encap().
5680 */
5681 if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3);
5682 if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max);
5683 }
5684 if (scctx->isc_rss_table_size == 0)
5685 scctx->isc_rss_table_size = 64;
5686 scctx->isc_rss_table_mask = scctx->isc_rss_table_size - 1;
5687
5688 /* Create and start admin taskqueue */
5689 snprintf(namebuf, TASKQUEUE_NAMELEN, "if_%s_tq", device_get_nameunit(dev));
5690 ctx->ifc_tq = taskqueue_create_fast(namebuf, M_NOWAIT,
5691 taskqueue_thread_enqueue, &ctx->ifc_tq);
5692 if (ctx->ifc_tq == NULL) {
5693 device_printf(dev, "Unable to create admin taskqueue\n");
5694 err = ENOMEM;
5695 goto fail_cleanup;
5696 }
5697
5698 err = taskqueue_start_threads(&ctx->ifc_tq, 1, PI_NET, "%s", namebuf);
5699 if (err) {
5700 device_printf(dev,
5701 "Unable to start admin taskqueue threads error: %d\n",
5702 err);
5703 taskqueue_free(ctx->ifc_tq);
5704 ctx->ifc_tq = NULL;
5705 goto fail_cleanup;
5706 }
5707
5708 TASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
5709 TASK_INIT(&ctx->ifc_led_task, 0, _task_fn_led, ctx);
5710 TASK_INIT(&ctx->ifc_vflr_task, 0, _task_fn_iov, ctx);
5711 IFLIB_REGISTER_FAIL_POINT(dev, register_after_taskqueue, err,
5712 fail_cleanup);
5713
5714 /* Set up cpu set. If it fails, use the set of all CPUs. */
5715 if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) {
5716 device_printf(dev, "Unable to fetch CPU list\n");
5717 CPU_COPY(&all_cpus, &ctx->ifc_cpus);
5718 ctx->ifc_cpus_are_physical_cores = false;
5719 } else
5720 ctx->ifc_cpus_are_physical_cores = true;
5721 MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0);
5722
5723 /*
5724 * Now set up MSI or MSI-X, should return us the number of supported
5725 * vectors (will be 1 for a legacy interrupt and MSI).
5726 */
5727 if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
5728 msix = scctx->isc_vectors;
5729 } else if (scctx->isc_msix_bar != 0)
5730 /*
5731 * The simple fact that isc_msix_bar is not 0 does not mean we
5732 * we have a good value there that is known to work.
5733 */
5734 msix = iflib_msix_init(ctx);
5735 else {
5736 scctx->isc_vectors = 1;
5737 scctx->isc_ntxqsets = 1;
5738 scctx->isc_nrxqsets = 1;
5739 scctx->isc_intr = IFLIB_INTR_LEGACY;
5740 msix = 0;
5741 }
5742 intr_allocated = true;
5743 IFLIB_REGISTER_FAIL_POINT(dev, register_after_interrupts, err,
5744 fail_cleanup);
5745 /* Get memory for the station queues */
5746 if ((err = iflib_queues_alloc(ctx))) {
5747 device_printf(dev, "Unable to allocate queue memory\n");
5748 goto fail_cleanup;
5749 }
5750 queues_allocated = true;
5751
5752 if ((err = iflib_qset_structures_setup(ctx)))
5753 goto fail_cleanup;
5754
5755 /*
5756 * Now that we know how many queues there are, get the core offset.
5757 */
5758 ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx);
5759 IFLIB_REGISTER_FAIL_POINT(dev, register_after_queues, err,
5760 fail_cleanup);
5761
5762 if (msix > 1) {
5763 /*
5764 * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable
5765 * aren't the default NULL implementation.
5766 */
5767 kobj_desc = &ifdi_rx_queue_intr_enable_desc;
5768 kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
5769 kobj_desc);
5770 if (kobj_method == &kobj_desc->deflt) {
5771 device_printf(dev,
5772 "MSI-X requires ifdi_rx_queue_intr_enable method");
5773 err = EOPNOTSUPP;
5774 goto fail_cleanup;
5775 }
5776 kobj_desc = &ifdi_tx_queue_intr_enable_desc;
5777 kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
5778 kobj_desc);
5779 if (kobj_method == &kobj_desc->deflt) {
5780 device_printf(dev,
5781 "MSI-X requires ifdi_tx_queue_intr_enable method");
5782 err = EOPNOTSUPP;
5783 goto fail_cleanup;
5784 }
5785
5786 /*
5787 * Assign the MSI-X vectors.
5788 * Note that the default NULL ifdi_msix_intr_assign method will
5789 * fail here, too.
5790 */
5791 err = IFDI_MSIX_INTR_ASSIGN(ctx, msix);
5792 if (err != 0) {
5793 device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n",
5794 err);
5795 goto fail_cleanup;
5796 }
5797 } else if (scctx->isc_intr != IFLIB_INTR_MSIX) {
5798 rid = 0;
5799 if (scctx->isc_intr == IFLIB_INTR_MSI) {
5800 MPASS(msix == 1);
5801 rid = 1;
5802 }
5803 if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
5804 device_printf(dev, "iflib_legacy_setup failed %d\n", err);
5805 goto fail_cleanup;
5806 }
5807 } else {
5808 device_printf(dev,
5809 "Cannot use iflib with only 1 MSI-X interrupt!\n");
5810 err = ENODEV;
5811 goto fail_cleanup;
5812 }
5813
5814 /*
5815 * It prevents a double-locking panic with iflib_media_status when
5816 * the driver loads.
5817 */
5818 CTX_UNLOCK(ctx);
5819 ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
5820 CTX_LOCK(ctx);
5821
5822 if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
5823 device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
5824 goto fail_detach;
5825 }
5826 IFLIB_REGISTER_FAIL_POINT(dev, register_after_attach_post, err,
5827 fail_detach);
5828
5829 /*
5830 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
5831 * This must appear after the call to ether_ifattach() because
5832 * ether_ifattach() sets if_hdrlen to the default value.
5833 */
5834 if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
5835 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
5836
5837 if ((err = iflib_netmap_attach(ctx))) {
5838 device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
5839 goto fail_detach;
5840 }
5841 *ctxp = ctx;
5842
5843 DEBUGNET_SET(ctx->ifc_ifp, iflib);
5844
5845 iflib_add_device_sysctl_post(ctx);
5846 iflib_add_pfil(ctx);
5847 ctx->ifc_flags |= IFC_INIT_DONE;
5848 CTX_UNLOCK(ctx);
5849
5850 /* Create led(4) devices if the driver defined the method */
5851 kobj_desc = &ifdi_led_func_desc;
5852 kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
5853 kobj_desc);
5854 if (kobj_method != &kobj_desc->deflt && IFDI_LED_SUPPORTED(ctx))
5855 iflib_led_create(ctx);
5856
5857 return (0);
5858
5859 fail_detach:
5860 STATE_LOCK(ctx);
5861 ctx->ifc_flags |= IFC_IN_DETACH;
5862 STATE_UNLOCK(ctx);
5863 /* Tasks may need the context lock; ether_ifdetach() may sleep. */
5864 CTX_UNLOCK(ctx);
5865 taskqueue_drain_all(ctx->ifc_tq);
5866 #ifdef PCI_IOV
5867 /*
5868 * IFDI_ATTACH_POST may have registered an SR-IOV schema. Match the
5869 * normal deregistration order so a failed attach cannot leave a stale
5870 * /dev/iov node behind. device_attach() holds Giant throughout this
5871 * path, so an IOV configuration cannot race the detach.
5872 */
5873 if (!CTX_IS_VF(ctx)) {
5874 iov_error = pci_iov_detach(dev);
5875 if (iov_error != 0)
5876 device_printf(dev, "Could not detach SR-IOV after "
5877 "attach failure: %d\n", iov_error);
5878 }
5879 #endif
5880 ether_ifdetach(ctx->ifc_ifp);
5881 CTX_LOCK(ctx);
5882 goto fail_cleanup_detaching;
5883
5884 fail_cleanup:
5885 STATE_LOCK(ctx);
5886 ctx->ifc_flags |= IFC_IN_DETACH;
5887 STATE_UNLOCK(ctx);
5888
5889 fail_cleanup_detaching:
5890 /*
5891 * The pre-attach sysctls contain pointers into ctx. Remove them on
5892 * every registration failure before iflib_deregister() frees ctx.
5893 */
5894 if (ctx->ifc_sysctl_node != NULL) {
5895 sysctl_ctx_free(&ctx->ifc_sysctl_ctx);
5896 ctx->ifc_sysctl_node = NULL;
5897 }
5898
5899 if (ctx->ifc_tq != NULL) {
5900 /*
5901 * Drain without holding the context lock so configuration tasks can
5902 * run to completion. On fail_detach a second drain also catches
5903 * tasks queued during the first drain.
5904 */
5905 CTX_UNLOCK(ctx);
5906 taskqueue_drain_all(ctx->ifc_tq);
5907 CTX_LOCK(ctx);
5908 }
5909
5910 if (queues_allocated) {
5911 iflib_tqg_detach(ctx);
5912 iflib_tx_structures_free(ctx);
5913 iflib_rx_structures_free(ctx);
5914 }
5915
5916 /*
5917 * A successful IFDI_ATTACH_PRE must be matched by IFDI_DETACH, even
5918 * when registration fails before queue allocation. Match
5919 * iflib_device_deregister by detaching before taskqueue_free.
5920 */
5921 if (attach_pre_succeeded) {
5922 IFDI_DETACH(ctx);
5923 if (queues_allocated)
5924 IFDI_QUEUES_FREE(ctx);
5925 }
5926 if (ctx->ifc_tq != NULL) {
5927 taskqueue_free(ctx->ifc_tq);
5928 ctx->ifc_tq = NULL;
5929 }
5930 if (intr_allocated)
5931 iflib_free_intr_mem(ctx);
5932
5933 CTX_UNLOCK(ctx);
5934 iflib_deregister(ctx);
5935 device_set_softc(ctx->ifc_dev, NULL);
5936 if (ctx->ifc_flags & IFC_SC_ALLOCATED)
5937 free(ctx->ifc_softc, M_IFLIB);
5938 unref_ctx_core_offset(ctx);
5939 free(ctx, M_IFLIB);
5940 return (err);
5941 }
5942
5943 int
iflib_device_attach(device_t dev)5944 iflib_device_attach(device_t dev)
5945 {
5946 if_ctx_t ctx;
5947 if_shared_ctx_t sctx;
5948
5949 if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
5950 return (ENOTSUP);
5951
5952 pci_enable_busmaster(dev);
5953
5954 return (iflib_device_register(dev, NULL, sctx, &ctx));
5955 }
5956
5957 int
iflib_device_deregister(if_ctx_t ctx)5958 iflib_device_deregister(if_ctx_t ctx)
5959 {
5960 if_t ifp = ctx->ifc_ifp;
5961 device_t dev = ctx->ifc_dev;
5962 int error;
5963
5964 /* Make sure VLANS are not using driver */
5965 if (if_vlantrunkinuse(ifp)) {
5966 device_printf(dev, "Vlan in use, detach first\n");
5967 return (EBUSY);
5968 }
5969 #ifdef PCI_IOV
5970 if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) {
5971 device_printf(dev, "SR-IOV in use; detach first.\n");
5972 return (EBUSY);
5973 }
5974 #endif
5975
5976 /*
5977 * Establish any ordering required by the terminal stop while the
5978 * interface is still intact. Once this succeeds, mark the context
5979 * inactive before releasing the lock so configuration tasks cannot
5980 * consume partially applied policy.
5981 */
5982 CTX_LOCK(ctx);
5983 error = IFDI_POWER_PREPARE(ctx, IFLIB_POWER_DETACH);
5984 if (error != 0) {
5985 CTX_UNLOCK(ctx);
5986 return (error);
5987 }
5988 STATE_LOCK(ctx);
5989 ctx->ifc_flags |= IFC_IN_DETACH;
5990 STATE_UNLOCK(ctx);
5991 ctx->ifc_pm_state = IFLIB_PM_SUSPENDING;
5992 CTX_UNLOCK(ctx);
5993
5994 sysctl_ctx_free(&ctx->ifc_sysctl_ctx);
5995 ctx->ifc_sysctl_node = NULL;
5996
5997 /* Unregister VLAN handlers before calling iflib_stop() */
5998 iflib_unregister_vlan_handlers(ctx);
5999
6000 iflib_netmap_detach(ifp);
6001 /*
6002 * A task that passed its IFC_IN_DETACH check before the flag was set
6003 * can still report a link change. Drain every private task before
6004 * ether_ifdetach() performs the final if_linktask drain. Drivers may
6005 * register their own link-related tasks on this taskqueue.
6006 */
6007 taskqueue_drain_all(ctx->ifc_tq);
6008 ether_ifdetach(ifp);
6009
6010 CTX_LOCK(ctx);
6011 iflib_stop(ctx);
6012 CTX_UNLOCK(ctx);
6013
6014 iflib_rem_pfil(ctx);
6015 if (ctx->ifc_led_dev != NULL) {
6016 led_destroy(ctx->ifc_led_dev);
6017 taskqueue_drain(ctx->ifc_tq, &ctx->ifc_led_task);
6018 }
6019
6020 iflib_tqg_detach(ctx);
6021 iflib_tx_structures_free(ctx);
6022 iflib_rx_structures_free(ctx);
6023
6024 CTX_LOCK(ctx);
6025 IFDI_DETACH(ctx);
6026 IFDI_QUEUES_FREE(ctx);
6027 CTX_UNLOCK(ctx);
6028
6029 taskqueue_free(ctx->ifc_tq);
6030 ctx->ifc_tq = NULL;
6031
6032 /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
6033 iflib_free_intr_mem(ctx);
6034
6035 bus_generic_detach(dev);
6036
6037 iflib_deregister(ctx);
6038
6039 device_set_softc(ctx->ifc_dev, NULL);
6040 if (ctx->ifc_flags & IFC_SC_ALLOCATED)
6041 free(ctx->ifc_softc, M_IFLIB);
6042 unref_ctx_core_offset(ctx);
6043 free(ctx, M_IFLIB);
6044 return (0);
6045 }
6046
6047 static void
iflib_tqg_detach(if_ctx_t ctx)6048 iflib_tqg_detach(if_ctx_t ctx)
6049 {
6050 iflib_txq_t txq;
6051 iflib_rxq_t rxq;
6052 int i;
6053 struct taskqgroup *tqg;
6054
6055 /* XXX drain any dependent tasks */
6056 tqg = qgroup_if_io_tqg;
6057 for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
6058 callout_drain(&txq->ift_timer);
6059 #ifdef DEV_NETMAP
6060 callout_drain(&txq->ift_netmap_timer);
6061 #endif /* DEV_NETMAP */
6062 if (txq->ift_task.gt_uniq != NULL)
6063 taskqgroup_detach(tqg, &txq->ift_task);
6064 }
6065 for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
6066 if (rxq->ifr_task.gt_uniq != NULL)
6067 taskqgroup_detach(tqg, &rxq->ifr_task);
6068 }
6069 }
6070
6071 static void
iflib_free_intr_mem(if_ctx_t ctx)6072 iflib_free_intr_mem(if_ctx_t ctx)
6073 {
6074
6075 if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
6076 iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
6077 }
6078 if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
6079 pci_release_msi(ctx->ifc_dev);
6080 }
6081 if (ctx->ifc_msix_mem != NULL) {
6082 bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
6083 rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem);
6084 ctx->ifc_msix_mem = NULL;
6085 }
6086 }
6087
6088 int
iflib_device_detach(device_t dev)6089 iflib_device_detach(device_t dev)
6090 {
6091 if_ctx_t ctx = device_get_softc(dev);
6092
6093 return (iflib_device_deregister(ctx));
6094 }
6095
6096 static int
iflib_device_resume_locked(if_ctx_t ctx)6097 iflib_device_resume_locked(if_ctx_t ctx)
6098 {
6099 if_t ifp;
6100 int error;
6101
6102 sx_assert(&ctx->ifc_ctx_sx, SA_XLOCKED);
6103 KASSERT(ctx->ifc_datapath_state == IFLIB_DP_STOPPED,
6104 ("iflib resume with active datapath state %d",
6105 ctx->ifc_datapath_state));
6106 KASSERT(ctx->ifc_pm_state == IFLIB_PM_SUSPENDING ||
6107 ctx->ifc_pm_state == IFLIB_PM_SUSPENDED,
6108 ("iflib resume from power state %d", ctx->ifc_pm_state));
6109
6110 ifp = ctx->ifc_ifp;
6111 error = IFDI_RESUME(ctx);
6112 if (error != 0)
6113 return (error);
6114 ctx->ifc_pm_state = IFLIB_PM_ACTIVE;
6115
6116 if ((if_getflags(ifp) & IFF_UP) == 0) {
6117 STATE_LOCK(ctx);
6118 iflib_set_running(ctx, false);
6119 STATE_UNLOCK(ctx);
6120 return (0);
6121 }
6122
6123 iflib_init_locked(ctx);
6124 return (0);
6125 }
6126
6127 int
iflib_device_suspend(device_t dev)6128 iflib_device_suspend(device_t dev)
6129 {
6130 if_ctx_t ctx = device_get_softc(dev);
6131 int error, resume_error;
6132
6133 CTX_LOCK(ctx);
6134 error = IFDI_POWER_PREPARE(ctx, IFLIB_POWER_SUSPEND);
6135 if (error == 0) {
6136 iflib_stop(ctx);
6137 ctx->ifc_pm_state = IFLIB_PM_SUSPENDING;
6138 }
6139 CTX_UNLOCK(ctx);
6140 if (error != 0)
6141 return (error);
6142
6143 /* Driver configuration tasks must finish before entering low power. */
6144 taskqueue_drain_all(ctx->ifc_tq);
6145
6146 CTX_LOCK(ctx);
6147 error = IFDI_SUSPEND(ctx);
6148 if (error == 0)
6149 ctx->ifc_pm_state = IFLIB_PM_SUSPENDED;
6150 else {
6151 resume_error = iflib_device_resume_locked(ctx);
6152 if (resume_error != 0)
6153 device_printf(dev,
6154 "failed to resume after suspend error: %d\n",
6155 resume_error);
6156 }
6157 CTX_UNLOCK(ctx);
6158 if (error != 0)
6159 return (error);
6160
6161 error = bus_generic_suspend(dev);
6162 if (error != 0) {
6163 CTX_LOCK(ctx);
6164 resume_error = iflib_device_resume_locked(ctx);
6165 CTX_UNLOCK(ctx);
6166 if (resume_error != 0)
6167 device_printf(dev,
6168 "failed to resume after child suspend error: %d\n",
6169 resume_error);
6170 }
6171
6172 return (error);
6173 }
6174
6175 int
iflib_device_shutdown(device_t dev)6176 iflib_device_shutdown(device_t dev)
6177 {
6178 if_ctx_t ctx = device_get_softc(dev);
6179 int error;
6180
6181 CTX_LOCK(ctx);
6182 error = IFDI_POWER_PREPARE(ctx, IFLIB_POWER_SHUTDOWN);
6183 if (error == 0) {
6184 iflib_stop(ctx);
6185 ctx->ifc_pm_state = IFLIB_PM_SUSPENDING;
6186 }
6187 CTX_UNLOCK(ctx);
6188 if (error != 0)
6189 return (error);
6190
6191 taskqueue_drain_all(ctx->ifc_tq);
6192
6193 CTX_LOCK(ctx);
6194 error = IFDI_SHUTDOWN(ctx);
6195 if (error == 0)
6196 ctx->ifc_pm_state = IFLIB_PM_SUSPENDED;
6197 CTX_UNLOCK(ctx);
6198 if (error != 0)
6199 return (error);
6200
6201 return (bus_generic_suspend(dev));
6202 }
6203
6204 int
iflib_device_resume(device_t dev)6205 iflib_device_resume(device_t dev)
6206 {
6207 if_ctx_t ctx = device_get_softc(dev);
6208 iflib_txq_t txq = ctx->ifc_txqs;
6209 bool running;
6210 int error, child_error;
6211
6212 CTX_LOCK(ctx);
6213 error = iflib_device_resume_locked(ctx);
6214 running = ctx->ifc_datapath_state == IFLIB_DP_RUNNING;
6215 CTX_UNLOCK(ctx);
6216 if (running) {
6217 for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
6218 iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
6219 }
6220
6221 child_error = bus_generic_resume(dev);
6222 return (error != 0 ? error : child_error);
6223 }
6224
6225 int
iflib_device_iov_init(device_t dev,uint16_t num_vfs,const nvlist_t * params)6226 iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
6227 {
6228 int error;
6229 if_ctx_t ctx = device_get_softc(dev);
6230
6231 CTX_LOCK(ctx);
6232 error = IFDI_IOV_INIT(ctx, num_vfs, params);
6233 CTX_UNLOCK(ctx);
6234
6235 return (error);
6236 }
6237
6238 int
iflib_device_iov_init_restart(device_t dev,uint16_t num_vfs,const nvlist_t * params)6239 iflib_device_iov_init_restart(device_t dev, uint16_t num_vfs,
6240 const nvlist_t *params)
6241 {
6242 if_ctx_t ctx;
6243 if_t ifp;
6244 bool restart;
6245 int error;
6246
6247 ctx = device_get_softc(dev);
6248 ifp = ctx->ifc_ifp;
6249
6250 CTX_LOCK(ctx);
6251 /*
6252 * Drivers which change the PF queue layout need the complete iflib
6253 * stop/init sequence around their IOV callback. Administrative state
6254 * and software admission do not establish DMA quiescence:
6255 * failed initialization or a pending watchdog reset can leave DMA
6256 * active. Let iflib_stop() decide whether hardware needs quiescing,
6257 * and preserve administrative intent across the layout change.
6258 */
6259 restart = (if_getflags(ifp) & IFF_UP) != 0;
6260 iflib_stop(ctx);
6261 error = IFDI_IOV_INIT(ctx, num_vfs, params);
6262 if (restart)
6263 iflib_init_locked(ctx);
6264 CTX_UNLOCK(ctx);
6265 return (error);
6266 }
6267
6268 void
iflib_device_iov_uninit(device_t dev)6269 iflib_device_iov_uninit(device_t dev)
6270 {
6271 if_ctx_t ctx = device_get_softc(dev);
6272
6273 CTX_LOCK(ctx);
6274 IFDI_IOV_UNINIT(ctx);
6275 CTX_UNLOCK(ctx);
6276 }
6277
6278 void
iflib_device_iov_uninit_restart(device_t dev)6279 iflib_device_iov_uninit_restart(device_t dev)
6280 {
6281 if_ctx_t ctx;
6282 bool restart;
6283
6284 ctx = device_get_softc(dev);
6285
6286 CTX_LOCK(ctx);
6287 /*
6288 * Software admission can be closed while a watchdog reset is pending
6289 * but the hardware is still live. Always stop before the driver changes
6290 * its queue layout, and use IFF_UP only to preserve administrative intent.
6291 */
6292 restart = (if_getflags(ctx->ifc_ifp) & IFF_UP) != 0;
6293 iflib_stop(ctx);
6294 IFDI_IOV_UNINIT(ctx);
6295 if (restart)
6296 iflib_init_locked(ctx);
6297 CTX_UNLOCK(ctx);
6298 }
6299
6300 int
iflib_device_iov_add_vf(device_t dev,uint16_t vfnum,const nvlist_t * params)6301 iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
6302 {
6303 int error;
6304 if_ctx_t ctx = device_get_softc(dev);
6305
6306 CTX_LOCK(ctx);
6307 error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
6308 CTX_UNLOCK(ctx);
6309
6310 return (error);
6311 }
6312
6313 /*********************************************************************
6314 *
6315 * MODULE FUNCTION DEFINITIONS
6316 *
6317 **********************************************************************/
6318
6319 static void
iflib_cpu_llc_init(void)6320 iflib_cpu_llc_init(void)
6321 {
6322 #ifdef SMP
6323 struct cpu_group *cg, *llc;
6324 uint16_t llc_id, first_llc_id;
6325 int cpu;
6326 #endif
6327
6328 iflib_single_llc = true;
6329 #ifdef SMP
6330 first_llc_id = USHRT_MAX;
6331 for (cpu = 0; cpu <= mp_maxid; cpu++) {
6332 if (CPU_ABSENT(cpu))
6333 continue;
6334
6335 /*
6336 * Select the outermost shared-cache group containing this
6337 * CPU. On AMD systems this is the L3/CCX group. This also
6338 * gives sensible behavior when the llc is not L3.
6339 */
6340 llc = NULL;
6341 for (cg = smp_topo_find(cpu_top, cpu); cg != NULL;
6342 cg = cg->cg_parent) {
6343 if (cg->cg_level != CG_SHARE_NONE)
6344 llc = cg;
6345 }
6346
6347 /* cg_first is a stable llc identifier. */
6348 llc_id = llc != NULL ? llc->cg_first : cpu;
6349 iflib_cpu_llc[cpu] = llc_id;
6350 if (first_llc_id == USHRT_MAX)
6351 first_llc_id = llc_id;
6352 else if (llc_id != first_llc_id)
6353 iflib_single_llc = false;
6354 }
6355 #else
6356 iflib_cpu_llc[0] = 0;
6357 #endif
6358 iflib_producer_gate = !iflib_single_llc ||
6359 mp_ncpus > iflib_max_producers;
6360 }
6361
6362 /*
6363 * - Start a fast taskqueue thread for each core
6364 * - Start a taskqueue for control operations
6365 */
6366 static int
iflib_module_init(void)6367 iflib_module_init(void)
6368 {
6369 iflib_timer_default = hz / 2;
6370 iflib_cpu_llc_init();
6371
6372 if (iflib_simple_txbr_size < IFLIB_SIMPLE_TXBR_MIN ||
6373 !powerof2(iflib_simple_txbr_size)) {
6374 printf("iflib: simple_txbr_size %d is not a power of 2 >= %d "
6375 "- using default value of %d\n", iflib_simple_txbr_size,
6376 IFLIB_SIMPLE_TXBR_MIN, IFLIB_SIMPLE_TXBR_SIZE);
6377 iflib_simple_txbr_size = IFLIB_SIMPLE_TXBR_SIZE;
6378 }
6379 return (0);
6380 }
6381
6382 static int
iflib_module_event_handler(module_t mod,int what,void * arg)6383 iflib_module_event_handler(module_t mod, int what, void *arg)
6384 {
6385 int err;
6386
6387 switch (what) {
6388 case MOD_LOAD:
6389 if ((err = iflib_module_init()) != 0)
6390 return (err);
6391 break;
6392 case MOD_UNLOAD:
6393 return (EBUSY);
6394 default:
6395 return (EOPNOTSUPP);
6396 }
6397
6398 return (0);
6399 }
6400
6401 /*********************************************************************
6402 *
6403 * PUBLIC FUNCTION DEFINITIONS
6404 * ordered as in iflib.h
6405 *
6406 **********************************************************************/
6407
6408 static void
_iflib_assert(if_shared_ctx_t sctx)6409 _iflib_assert(if_shared_ctx_t sctx)
6410 {
6411 int i;
6412
6413 MPASS(sctx->isc_tx_maxsize);
6414 MPASS(sctx->isc_tx_maxsegsize);
6415
6416 MPASS(sctx->isc_rx_maxsize);
6417 MPASS(sctx->isc_rx_nsegments);
6418 MPASS(sctx->isc_rx_maxsegsize);
6419
6420 MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8);
6421 for (i = 0; i < sctx->isc_nrxqs; i++) {
6422 MPASS(sctx->isc_nrxd_min[i]);
6423 MPASS(powerof2(sctx->isc_nrxd_min[i]));
6424 MPASS(sctx->isc_nrxd_max[i]);
6425 MPASS(powerof2(sctx->isc_nrxd_max[i]));
6426 MPASS(sctx->isc_nrxd_default[i]);
6427 MPASS(powerof2(sctx->isc_nrxd_default[i]));
6428 }
6429
6430 MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8);
6431 for (i = 0; i < sctx->isc_ntxqs; i++) {
6432 MPASS(sctx->isc_ntxd_min[i]);
6433 MPASS(powerof2(sctx->isc_ntxd_min[i]));
6434 MPASS(sctx->isc_ntxd_max[i]);
6435 MPASS(powerof2(sctx->isc_ntxd_max[i]));
6436 MPASS(sctx->isc_ntxd_default[i]);
6437 MPASS(powerof2(sctx->isc_ntxd_default[i]));
6438 }
6439 }
6440
6441 static void
_iflib_pre_assert(if_softc_ctx_t scctx)6442 _iflib_pre_assert(if_softc_ctx_t scctx)
6443 {
6444
6445 MPASS(scctx->isc_txrx->ift_txd_encap);
6446 MPASS(scctx->isc_txrx->ift_txd_flush);
6447 MPASS(scctx->isc_txrx->ift_txd_credits_update);
6448 MPASS(scctx->isc_txrx->ift_rxd_available);
6449 MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
6450 MPASS(scctx->isc_txrx->ift_rxd_refill);
6451 MPASS(scctx->isc_txrx->ift_rxd_flush);
6452 }
6453
6454 static void
iflib_register(if_ctx_t ctx)6455 iflib_register(if_ctx_t ctx)
6456 {
6457 if_shared_ctx_t sctx = ctx->ifc_sctx;
6458 driver_t *driver = sctx->isc_driver;
6459 device_t dev = ctx->ifc_dev;
6460 if_t ifp;
6461
6462 _iflib_assert(sctx);
6463
6464 CTX_LOCK_INIT(ctx);
6465 STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
6466 ifp = ctx->ifc_ifp = if_alloc_dev(IFT_ETHER, dev);
6467
6468 /*
6469 * Initialize our context's device specific methods
6470 */
6471 kobj_init((kobj_t) ctx, (kobj_class_t) driver);
6472 kobj_class_compile((kobj_class_t) driver);
6473
6474 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
6475 if_setsoftc(ifp, ctx);
6476 if_setdev(ifp, dev);
6477 if_setinitfn(ifp, iflib_if_init);
6478 if_setioctlfn(ifp, iflib_if_ioctl);
6479 /* VF status describes children of an SR-IOV PF. */
6480 if (!CTX_IS_VF(ctx))
6481 if_setvfstatusfn(ifp, iflib_if_vf_status);
6482 #ifdef ALTQ
6483 if_setstartfn(ifp, iflib_altq_if_start);
6484 if_settransmitfn(ifp, iflib_altq_if_transmit);
6485 if_setsendqready(ifp);
6486 #else
6487 if_settransmitfn(ifp, iflib_if_transmit);
6488 #endif
6489 if_setqflushfn(ifp, iflib_if_qflush);
6490 if_setgetcounterfn(ifp, iflib_if_get_counter);
6491 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
6492 ctx->ifc_vlan_attach_event =
6493 EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
6494 EVENTHANDLER_PRI_FIRST);
6495 ctx->ifc_vlan_detach_event =
6496 EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
6497 EVENTHANDLER_PRI_FIRST);
6498
6499 if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) {
6500 ctx->ifc_mediap = &ctx->ifc_media;
6501 ifmedia_init(ctx->ifc_mediap, IFM_IMASK,
6502 iflib_media_change, iflib_media_status);
6503 }
6504 }
6505
6506 static void
iflib_unregister_vlan_handlers(if_ctx_t ctx)6507 iflib_unregister_vlan_handlers(if_ctx_t ctx)
6508 {
6509 /* Unregister VLAN events */
6510 if (ctx->ifc_vlan_attach_event != NULL) {
6511 EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
6512 ctx->ifc_vlan_attach_event = NULL;
6513 }
6514 if (ctx->ifc_vlan_detach_event != NULL) {
6515 EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
6516 ctx->ifc_vlan_detach_event = NULL;
6517 }
6518
6519 }
6520
6521 static void
iflib_deregister(if_ctx_t ctx)6522 iflib_deregister(if_ctx_t ctx)
6523 {
6524 if_t ifp = ctx->ifc_ifp;
6525
6526 /* Remove all media */
6527 ifmedia_removeall(&ctx->ifc_media);
6528
6529 /* Ensure that VLAN event handlers are unregistered */
6530 iflib_unregister_vlan_handlers(ctx);
6531
6532 /* Release kobject reference */
6533 kobj_delete((kobj_t) ctx, NULL);
6534
6535 /* Free the ifnet structure */
6536 if_free(ifp);
6537
6538 STATE_LOCK_DESTROY(ctx);
6539
6540 /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
6541 CTX_LOCK_DESTROY(ctx);
6542 }
6543
6544 static int
iflib_queues_alloc(if_ctx_t ctx)6545 iflib_queues_alloc(if_ctx_t ctx)
6546 {
6547 if_shared_ctx_t sctx = ctx->ifc_sctx;
6548 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
6549 device_t dev = ctx->ifc_dev;
6550 int nrxqsets = scctx->isc_nrxqsets;
6551 int ntxqsets = scctx->isc_ntxqsets;
6552 iflib_txq_t txq;
6553 iflib_rxq_t rxq;
6554 iflib_fl_t fl = NULL;
6555 int i, j, cpu, err;
6556 iflib_dma_info_t ifdip;
6557 uint32_t *rxqsizes = scctx->isc_rxqsizes;
6558 uint32_t *txqsizes = scctx->isc_txqsizes;
6559 uint8_t nrxqs = sctx->isc_nrxqs;
6560 uint8_t ntxqs = sctx->isc_ntxqs;
6561 int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
6562 int fl_offset = (sctx->isc_flags & IFLIB_HAS_RXCQ ? 1 : 0);
6563 caddr_t *vaddrs;
6564 uint64_t *paddrs;
6565
6566 KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
6567 KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
6568 KASSERT(nrxqs >= fl_offset + nfree_lists,
6569 ("there must be at least a rxq for each free list"));
6570
6571 /* Allocate the TX ring struct memory */
6572 if (!(ctx->ifc_txqs =
6573 (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
6574 ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
6575 device_printf(dev, "Unable to allocate TX ring memory\n");
6576 err = ENOMEM;
6577 goto fail;
6578 }
6579
6580 /* Now allocate the RX */
6581 if (!(ctx->ifc_rxqs =
6582 (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
6583 nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
6584 device_printf(dev, "Unable to allocate RX ring memory\n");
6585 err = ENOMEM;
6586 goto rx_fail;
6587 }
6588
6589 txq = ctx->ifc_txqs;
6590 rxq = ctx->ifc_rxqs;
6591
6592 /*
6593 * XXX handle allocation failure
6594 */
6595 for (i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txq++, cpu = CPU_NEXT(cpu)) {
6596 /* Set up some basics */
6597
6598 if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs,
6599 M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
6600 device_printf(dev,
6601 "Unable to allocate TX DMA info memory\n");
6602 err = ENOMEM;
6603 goto err_tx_desc;
6604 }
6605 txq->ift_ifdi = ifdip;
6606 for (j = 0; j < ntxqs; j++, ifdip++) {
6607 if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) {
6608 device_printf(dev,
6609 "Unable to allocate TX descriptors\n");
6610 err = ENOMEM;
6611 goto err_tx_desc;
6612 }
6613 txq->ift_txd_size[j] = scctx->isc_txd_size[j];
6614 bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
6615 }
6616 txq->ift_ctx = ctx;
6617 txq->ift_id = i;
6618 if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
6619 txq->ift_br_offset = 1;
6620 } else {
6621 txq->ift_br_offset = 0;
6622 }
6623
6624 if (iflib_txsd_alloc(txq)) {
6625 device_printf(dev, "Critical Failure setting up TX buffers\n");
6626 err = ENOMEM;
6627 goto err_tx_desc;
6628 }
6629
6630 /* Initialize the TX lock */
6631 snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout",
6632 device_get_nameunit(dev), txq->ift_id);
6633 mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
6634 callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
6635 txq->ift_timer.c_cpu = cpu;
6636 #ifdef DEV_NETMAP
6637 callout_init_mtx(&txq->ift_netmap_timer, &txq->ift_mtx, 0);
6638 txq->ift_netmap_timer.c_cpu = cpu;
6639 #endif /* DEV_NETMAP */
6640
6641 err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
6642 iflib_txq_can_drain, M_IFLIB, M_WAITOK);
6643 if (err) {
6644 /* XXX free any allocated rings */
6645 device_printf(dev, "Unable to allocate buf_ring\n");
6646 goto err_tx_desc;
6647 }
6648 if (ctx->ifc_sysctl_simple_tx) {
6649 txq->ift_drbr = buf_ring_alloc(iflib_simple_txbr_size,
6650 M_IFLIB, M_WAITOK, &txq->ift_mtx);
6651 txq->ift_drbr_deferred = counter_u64_alloc(M_WAITOK);
6652 txq->ift_drbr_drops = counter_u64_alloc(M_WAITOK);
6653 txq->ift_drbr_blocked = counter_u64_alloc(M_WAITOK);
6654 txq->ift_drbr_remote = counter_u64_alloc(M_WAITOK);
6655 }
6656 txq->ift_reclaim_thresh = ctx->ifc_sysctl_tx_reclaim_thresh;
6657 }
6658
6659 for (i = 0; i < nrxqsets; i++, rxq++) {
6660 /* Set up some basics */
6661 callout_init(&rxq->ifr_watchdog, 1);
6662
6663 if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs,
6664 M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
6665 device_printf(dev,
6666 "Unable to allocate RX DMA info memory\n");
6667 err = ENOMEM;
6668 goto err_tx_desc;
6669 }
6670
6671 rxq->ifr_ifdi = ifdip;
6672 /* XXX this needs to be changed if #rx queues != #tx queues */
6673 rxq->ifr_ntxqirq = 1;
6674 rxq->ifr_txqid[0] = i;
6675 for (j = 0; j < nrxqs; j++, ifdip++) {
6676 if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) {
6677 device_printf(dev,
6678 "Unable to allocate RX descriptors\n");
6679 err = ENOMEM;
6680 goto err_tx_desc;
6681 }
6682 bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
6683 }
6684 rxq->ifr_ctx = ctx;
6685 rxq->ifr_id = i;
6686 rxq->ifr_fl_offset = fl_offset;
6687 rxq->ifr_nfl = nfree_lists;
6688 if (!(fl =
6689 (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
6690 device_printf(dev, "Unable to allocate free list memory\n");
6691 err = ENOMEM;
6692 goto err_tx_desc;
6693 }
6694 rxq->ifr_fl = fl;
6695 for (j = 0; j < nfree_lists; j++) {
6696 fl[j].ifl_rxq = rxq;
6697 fl[j].ifl_id = j;
6698 fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
6699 fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
6700 }
6701 /* Allocate receive buffers for the ring */
6702 if (iflib_rxsd_alloc(rxq)) {
6703 device_printf(dev,
6704 "Critical Failure setting up receive buffers\n");
6705 err = ENOMEM;
6706 goto err_rx_desc;
6707 }
6708
6709 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
6710 fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB,
6711 M_WAITOK);
6712 }
6713
6714 /* TXQs */
6715 vaddrs = malloc(sizeof(caddr_t) * ntxqsets * ntxqs, M_IFLIB, M_WAITOK);
6716 paddrs = malloc(sizeof(uint64_t) * ntxqsets * ntxqs, M_IFLIB, M_WAITOK);
6717 for (i = 0; i < ntxqsets; i++) {
6718 iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
6719
6720 for (j = 0; j < ntxqs; j++, di++) {
6721 vaddrs[i * ntxqs + j] = di->idi_vaddr;
6722 paddrs[i * ntxqs + j] = di->idi_paddr;
6723 }
6724 }
6725 if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
6726 device_printf(ctx->ifc_dev,
6727 "Unable to allocate device TX queue\n");
6728 iflib_tx_structures_free(ctx);
6729 free(vaddrs, M_IFLIB);
6730 free(paddrs, M_IFLIB);
6731 goto err_rx_desc;
6732 }
6733 free(vaddrs, M_IFLIB);
6734 free(paddrs, M_IFLIB);
6735
6736 /* RXQs */
6737 vaddrs = malloc(sizeof(caddr_t) * nrxqsets * nrxqs, M_IFLIB, M_WAITOK);
6738 paddrs = malloc(sizeof(uint64_t) * nrxqsets * nrxqs, M_IFLIB, M_WAITOK);
6739 for (i = 0; i < nrxqsets; i++) {
6740 iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
6741
6742 for (j = 0; j < nrxqs; j++, di++) {
6743 vaddrs[i * nrxqs + j] = di->idi_vaddr;
6744 paddrs[i * nrxqs + j] = di->idi_paddr;
6745 }
6746 }
6747 if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
6748 device_printf(ctx->ifc_dev,
6749 "Unable to allocate device RX queue\n");
6750 iflib_tx_structures_free(ctx);
6751 free(vaddrs, M_IFLIB);
6752 free(paddrs, M_IFLIB);
6753 goto err_rx_desc;
6754 }
6755 free(vaddrs, M_IFLIB);
6756 free(paddrs, M_IFLIB);
6757
6758 return (0);
6759
6760 /* XXX handle allocation failure changes */
6761 err_rx_desc:
6762 err_tx_desc:
6763 rx_fail:
6764 if (ctx->ifc_rxqs != NULL)
6765 free(ctx->ifc_rxqs, M_IFLIB);
6766 ctx->ifc_rxqs = NULL;
6767 if (ctx->ifc_txqs != NULL)
6768 free(ctx->ifc_txqs, M_IFLIB);
6769 ctx->ifc_txqs = NULL;
6770 fail:
6771 return (err);
6772 }
6773
6774 static int
iflib_tx_structures_setup(if_ctx_t ctx)6775 iflib_tx_structures_setup(if_ctx_t ctx)
6776 {
6777 iflib_txq_t txq = ctx->ifc_txqs;
6778 int i;
6779
6780 for (i = 0; i < NTXQSETS(ctx); i++, txq++)
6781 iflib_txq_setup(txq);
6782
6783 return (0);
6784 }
6785
6786 static void
iflib_tx_structures_free(if_ctx_t ctx)6787 iflib_tx_structures_free(if_ctx_t ctx)
6788 {
6789 iflib_txq_t txq = ctx->ifc_txqs;
6790 if_shared_ctx_t sctx = ctx->ifc_sctx;
6791 int i, j;
6792
6793 for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
6794 for (j = 0; j < sctx->isc_ntxqs; j++)
6795 iflib_dma_free(&txq->ift_ifdi[j]);
6796 iflib_txq_destroy(txq);
6797 }
6798 free(ctx->ifc_txqs, M_IFLIB);
6799 ctx->ifc_txqs = NULL;
6800 }
6801
6802 /*********************************************************************
6803 *
6804 * Initialize all receive rings.
6805 *
6806 **********************************************************************/
6807 static int
iflib_rx_structures_setup(if_ctx_t ctx)6808 iflib_rx_structures_setup(if_ctx_t ctx)
6809 {
6810 iflib_rxq_t rxq = ctx->ifc_rxqs;
6811 int q;
6812 #if defined(INET6) || defined(INET)
6813 int err, i;
6814 #endif
6815
6816 for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
6817 #if defined(INET6) || defined(INET)
6818 err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
6819 TCP_LRO_ENTRIES, min(1024,
6820 ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]));
6821 if (err != 0) {
6822 device_printf(ctx->ifc_dev,
6823 "LRO Initialization failed!\n");
6824 goto fail;
6825 }
6826 #endif
6827 IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
6828 }
6829 return (0);
6830 #if defined(INET6) || defined(INET)
6831 fail:
6832 /*
6833 * Free LRO resources allocated so far, we will only handle
6834 * the rings that completed, the failing case will have
6835 * cleaned up for itself. 'q' failed, so its the terminus.
6836 */
6837 rxq = ctx->ifc_rxqs;
6838 for (i = 0; i < q; ++i, rxq++) {
6839 tcp_lro_free(&rxq->ifr_lc);
6840 }
6841 return (err);
6842 #endif
6843 }
6844
6845 /*********************************************************************
6846 *
6847 * Free all receive rings.
6848 *
6849 **********************************************************************/
6850 static void
iflib_rx_structures_free(if_ctx_t ctx)6851 iflib_rx_structures_free(if_ctx_t ctx)
6852 {
6853 iflib_rxq_t rxq = ctx->ifc_rxqs;
6854 if_shared_ctx_t sctx = ctx->ifc_sctx;
6855 int i, j;
6856
6857 for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
6858 for (j = 0; j < sctx->isc_nrxqs; j++)
6859 iflib_dma_free(&rxq->ifr_ifdi[j]);
6860 iflib_rx_sds_free(rxq);
6861 #if defined(INET6) || defined(INET)
6862 tcp_lro_free(&rxq->ifr_lc);
6863 #endif
6864 }
6865 free(ctx->ifc_rxqs, M_IFLIB);
6866 ctx->ifc_rxqs = NULL;
6867 }
6868
6869 static int
iflib_qset_structures_setup(if_ctx_t ctx)6870 iflib_qset_structures_setup(if_ctx_t ctx)
6871 {
6872 int err;
6873
6874 /*
6875 * It is expected that the caller takes care of freeing queues if this
6876 * fails.
6877 */
6878 if ((err = iflib_tx_structures_setup(ctx)) != 0) {
6879 device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err);
6880 return (err);
6881 }
6882
6883 if ((err = iflib_rx_structures_setup(ctx)) != 0)
6884 device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
6885
6886 return (err);
6887 }
6888
6889 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)6890 iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
6891 driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name)
6892 {
6893
6894 return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
6895 }
6896
6897 /* Just to avoid copy/paste */
6898 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)6899 iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,
6900 int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq,
6901 const char *name)
6902 {
6903 device_t dev;
6904 unsigned int base_cpuid, cpuid;
6905 int err;
6906
6907 dev = ctx->ifc_dev;
6908 base_cpuid = ctx->ifc_sysctl_core_offset;
6909 cpuid = get_cpuid_for_queue(ctx, base_cpuid, qid, type == IFLIB_INTR_TX);
6910 err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev,
6911 irq ? irq->ii_res : NULL, name);
6912 if (err) {
6913 device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err);
6914 return (err);
6915 }
6916 #ifdef notyet
6917 if (cpuid > ctx->ifc_cpuid_highest)
6918 ctx->ifc_cpuid_highest = cpuid;
6919 #endif
6920 return (0);
6921 }
6922
6923 /*
6924 * Allocate a hardware interrupt for subctx using the parent (ctx)'s hardware
6925 * resources.
6926 *
6927 * Similar to iflib_irq_alloc_generic(), but for interrupt type IFLIB_INTR_RXTX
6928 * only.
6929 *
6930 * XXX: Could be removed if subctx's dev has its intr resource allocation
6931 * methods replaced with custom ones?
6932 */
6933 int
iflib_irq_alloc_generic_subctx(if_ctx_t ctx,if_ctx_t subctx,if_irq_t irq,int rid,iflib_intr_type_t type,driver_filter_t * filter,void * filter_arg,int qid,const char * name)6934 iflib_irq_alloc_generic_subctx(if_ctx_t ctx, if_ctx_t subctx, if_irq_t irq,
6935 int rid, iflib_intr_type_t type,
6936 driver_filter_t *filter, void *filter_arg,
6937 int qid, const char *name)
6938 {
6939 device_t dev, subdev;
6940 struct grouptask *gtask;
6941 struct taskqgroup *tqg;
6942 iflib_filter_info_t info;
6943 gtask_fn_t *fn;
6944 int tqrid, err;
6945 driver_filter_t *intr_fast;
6946 void *q;
6947
6948 MPASS(ctx != NULL);
6949 MPASS(subctx != NULL);
6950
6951 tqrid = rid;
6952 dev = ctx->ifc_dev;
6953 subdev = subctx->ifc_dev;
6954
6955 switch (type) {
6956 case IFLIB_INTR_RXTX:
6957 q = &subctx->ifc_rxqs[qid];
6958 info = &subctx->ifc_rxqs[qid].ifr_filter_info;
6959 gtask = &subctx->ifc_rxqs[qid].ifr_task;
6960 tqg = qgroup_if_io_tqg;
6961 fn = _task_fn_rx;
6962 intr_fast = iflib_fast_intr_rxtx;
6963 NET_GROUPTASK_INIT(gtask, 0, fn, q);
6964 break;
6965 default:
6966 device_printf(dev, "%s: unknown net intr type for subctx %s (%d)\n",
6967 __func__, device_get_nameunit(subdev), type);
6968 return (EINVAL);
6969 }
6970
6971 info->ifi_filter = filter;
6972 info->ifi_filter_arg = filter_arg;
6973 info->ifi_task = gtask;
6974 info->ifi_ctx = q;
6975
6976 NET_GROUPTASK_INIT(gtask, 0, fn, q);
6977
6978 /* Allocate interrupts from hardware using parent context */
6979 err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name);
6980 if (err != 0) {
6981 device_printf(dev, "_iflib_irq_alloc failed for subctx %s: %d\n",
6982 device_get_nameunit(subdev), err);
6983 return (err);
6984 }
6985
6986 if (tqrid != -1) {
6987 err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q,
6988 name);
6989 if (err)
6990 return (err);
6991 } else {
6992 taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name);
6993 }
6994
6995 return (0);
6996 }
6997
6998 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)6999 iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
7000 iflib_intr_type_t type, driver_filter_t *filter,
7001 void *filter_arg, int qid, const char *name)
7002 {
7003 device_t dev;
7004 struct grouptask *gtask;
7005 struct taskqgroup *tqg;
7006 iflib_filter_info_t info;
7007 gtask_fn_t *fn;
7008 int tqrid, err;
7009 driver_filter_t *intr_fast;
7010 void *q;
7011
7012 info = &ctx->ifc_filter_info;
7013 tqrid = rid;
7014
7015 switch (type) {
7016 /* XXX merge tx/rx for netmap? */
7017 case IFLIB_INTR_TX:
7018 q = &ctx->ifc_txqs[qid];
7019 info = &ctx->ifc_txqs[qid].ift_filter_info;
7020 gtask = &ctx->ifc_txqs[qid].ift_task;
7021 tqg = qgroup_if_io_tqg;
7022 fn = _task_fn_tx;
7023 intr_fast = iflib_fast_intr;
7024 GROUPTASK_INIT(gtask, 0, fn, q);
7025 ctx->ifc_flags |= IFC_NETMAP_TX_IRQ;
7026 break;
7027 case IFLIB_INTR_RX:
7028 q = &ctx->ifc_rxqs[qid];
7029 info = &ctx->ifc_rxqs[qid].ifr_filter_info;
7030 gtask = &ctx->ifc_rxqs[qid].ifr_task;
7031 tqg = qgroup_if_io_tqg;
7032 fn = _task_fn_rx;
7033 intr_fast = iflib_fast_intr;
7034 NET_GROUPTASK_INIT(gtask, 0, fn, q);
7035 break;
7036 case IFLIB_INTR_RXTX:
7037 q = &ctx->ifc_rxqs[qid];
7038 info = &ctx->ifc_rxqs[qid].ifr_filter_info;
7039 gtask = &ctx->ifc_rxqs[qid].ifr_task;
7040 tqg = qgroup_if_io_tqg;
7041 fn = _task_fn_rx;
7042 intr_fast = iflib_fast_intr_rxtx;
7043 NET_GROUPTASK_INIT(gtask, 0, fn, q);
7044 break;
7045 case IFLIB_INTR_ADMIN:
7046 q = ctx;
7047 tqrid = -1;
7048 info = &ctx->ifc_filter_info;
7049 gtask = NULL;
7050 intr_fast = iflib_fast_intr_ctx;
7051 break;
7052 default:
7053 device_printf(ctx->ifc_dev, "%s: unknown net intr type\n",
7054 __func__);
7055 return (EINVAL);
7056 }
7057
7058 info->ifi_filter = filter;
7059 info->ifi_filter_arg = filter_arg;
7060 info->ifi_task = gtask;
7061 info->ifi_ctx = q;
7062
7063 dev = ctx->ifc_dev;
7064 err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name);
7065 if (err != 0) {
7066 device_printf(dev, "_iflib_irq_alloc failed %d\n", err);
7067 return (err);
7068 }
7069 if (type == IFLIB_INTR_ADMIN)
7070 return (0);
7071
7072 if (tqrid != -1) {
7073 err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q,
7074 name);
7075 if (err)
7076 return (err);
7077 } else {
7078 taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name);
7079 }
7080
7081 return (0);
7082 }
7083
7084 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)7085 iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,
7086 void *arg, int qid, const char *name)
7087 {
7088 device_t dev;
7089 struct grouptask *gtask;
7090 struct taskqgroup *tqg;
7091 gtask_fn_t *fn;
7092 void *q;
7093 int err;
7094
7095 switch (type) {
7096 case IFLIB_INTR_TX:
7097 q = &ctx->ifc_txqs[qid];
7098 gtask = &ctx->ifc_txqs[qid].ift_task;
7099 tqg = qgroup_if_io_tqg;
7100 fn = _task_fn_tx;
7101 GROUPTASK_INIT(gtask, 0, fn, q);
7102 break;
7103 case IFLIB_INTR_RX:
7104 q = &ctx->ifc_rxqs[qid];
7105 gtask = &ctx->ifc_rxqs[qid].ifr_task;
7106 tqg = qgroup_if_io_tqg;
7107 fn = _task_fn_rx;
7108 NET_GROUPTASK_INIT(gtask, 0, fn, q);
7109 break;
7110 case IFLIB_INTR_IOV:
7111 return;
7112 default:
7113 panic("unknown net intr type");
7114 }
7115 err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q, name);
7116 if (err) {
7117 dev = ctx->ifc_dev;
7118 taskqgroup_attach(tqg, gtask, q, dev, irq ? irq->ii_res : NULL,
7119 name);
7120 }
7121 }
7122
7123 void
iflib_irq_free(if_ctx_t ctx,if_irq_t irq)7124 iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
7125 {
7126
7127 if (irq->ii_tag)
7128 bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
7129
7130 if (irq->ii_res)
7131 bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ,
7132 rman_get_rid(irq->ii_res), irq->ii_res);
7133 }
7134
7135 static int
iflib_legacy_setup(if_ctx_t ctx,driver_filter_t filter,void * filter_arg,int * rid,const char * name)7136 iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name)
7137 {
7138 iflib_txq_t txq = ctx->ifc_txqs;
7139 iflib_rxq_t rxq = ctx->ifc_rxqs;
7140 if_irq_t irq = &ctx->ifc_legacy_irq;
7141 iflib_filter_info_t info;
7142 device_t dev;
7143 struct grouptask *gtask;
7144 struct resource *res;
7145 int err, tqrid;
7146 bool rx_only;
7147
7148 info = &rxq->ifr_filter_info;
7149 gtask = &rxq->ifr_task;
7150 tqrid = *rid;
7151 rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0;
7152
7153 ctx->ifc_flags |= IFC_LEGACY;
7154 info->ifi_filter = filter;
7155 info->ifi_filter_arg = filter_arg;
7156 info->ifi_task = gtask;
7157 info->ifi_ctx = rxq;
7158
7159 dev = ctx->ifc_dev;
7160 /* We allocate a single interrupt resource */
7161 err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr :
7162 iflib_fast_intr_rxtx, NULL, info, name);
7163 if (err != 0)
7164 return (err);
7165 NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, rxq);
7166 res = irq->ii_res;
7167 taskqgroup_attach(qgroup_if_io_tqg, gtask, rxq, dev, res, name);
7168
7169 GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
7170 taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res,
7171 "tx");
7172 return (0);
7173 }
7174
7175 void
iflib_led_create(if_ctx_t ctx)7176 iflib_led_create(if_ctx_t ctx)
7177 {
7178
7179 ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
7180 device_get_nameunit(ctx->ifc_dev));
7181 }
7182
7183 void
iflib_tx_intr_deferred(if_ctx_t ctx,int txqid)7184 iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
7185 {
7186
7187 GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
7188 }
7189
7190 void
iflib_rx_intr_deferred(if_ctx_t ctx,int rxqid)7191 iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
7192 {
7193
7194 GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
7195 }
7196
7197 void
iflib_admin_intr_deferred(if_ctx_t ctx)7198 iflib_admin_intr_deferred(if_ctx_t ctx)
7199 {
7200
7201 taskqueue_enqueue(ctx->ifc_tq, &ctx->ifc_admin_task);
7202 }
7203
7204 void
iflib_iov_intr_deferred(if_ctx_t ctx)7205 iflib_iov_intr_deferred(if_ctx_t ctx)
7206 {
7207
7208 taskqueue_enqueue(ctx->ifc_tq, &ctx->ifc_vflr_task);
7209 }
7210
7211 void
iflib_io_tqg_attach(struct grouptask * gt,void * uniq,int cpu,const char * name)7212 iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name)
7213 {
7214
7215 taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL,
7216 name);
7217 }
7218
7219 void
iflib_config_task_init(if_ctx_t ctx,struct task * config_task,task_fn_t * fn)7220 iflib_config_task_init(if_ctx_t ctx, struct task *config_task, task_fn_t *fn)
7221 {
7222 TASK_INIT(config_task, 0, fn, ctx);
7223 }
7224
7225 void
iflib_config_task_enqueue(if_ctx_t ctx,struct task * config_task)7226 iflib_config_task_enqueue(if_ctx_t ctx, struct task *config_task)
7227 {
7228 taskqueue_enqueue(ctx->ifc_tq, config_task);
7229 }
7230
7231 void
iflib_link_state_change(if_ctx_t ctx,int link_state,uint64_t baudrate)7232 iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
7233 {
7234 if_t ifp = ctx->ifc_ifp;
7235
7236 if_setbaudrate(ifp, baudrate);
7237 if (baudrate >= IF_Gbps(10)) {
7238 STATE_LOCK(ctx);
7239 ctx->ifc_flags |= IFC_PREFETCH;
7240 STATE_UNLOCK(ctx);
7241 }
7242 ctx->ifc_link_state = link_state;
7243 if_link_state_change(ifp, link_state);
7244 }
7245
7246 static int
iflib_tx_credits_update(if_ctx_t ctx,iflib_txq_t txq)7247 iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
7248 {
7249 int credits;
7250 #ifdef INVARIANTS
7251 int credits_pre = txq->ift_cidx_processed;
7252 #endif
7253
7254 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
7255 BUS_DMASYNC_POSTREAD);
7256 if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
7257 return (0);
7258
7259 txq->ift_processed += credits;
7260 txq->ift_cidx_processed += credits;
7261
7262 MPASS(credits_pre + credits == txq->ift_cidx_processed);
7263 if (txq->ift_cidx_processed >= txq->ift_size)
7264 txq->ift_cidx_processed -= txq->ift_size;
7265 return (credits);
7266 }
7267
7268 static int
iflib_rxd_avail(if_ctx_t ctx,iflib_rxq_t rxq,qidx_t cidx,qidx_t budget)7269 iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
7270 {
7271 iflib_fl_t fl;
7272 u_int i;
7273
7274 for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++)
7275 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
7276 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
7277 return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
7278 budget));
7279 }
7280
7281 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)7282 iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
7283 const char *description, if_int_delay_info_t info,
7284 int offset, int value)
7285 {
7286 info->iidi_ctx = ctx;
7287 info->iidi_offset = offset;
7288 info->iidi_value = value;
7289 SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
7290 SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
7291 OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
7292 info, 0, iflib_sysctl_int_delay, "I", description);
7293 }
7294
7295 struct sx *
iflib_ctx_lock_get(if_ctx_t ctx)7296 iflib_ctx_lock_get(if_ctx_t ctx)
7297 {
7298
7299 return (&ctx->ifc_ctx_sx);
7300 }
7301
7302 static int
iflib_msix_init(if_ctx_t ctx)7303 iflib_msix_init(if_ctx_t ctx)
7304 {
7305 device_t dev = ctx->ifc_dev;
7306 if_shared_ctx_t sctx = ctx->ifc_sctx;
7307 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
7308 int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues;
7309 int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors;
7310
7311 iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs;
7312 iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs;
7313
7314 if (bootverbose)
7315 device_printf(dev, "msix_init qsets capped at %d\n",
7316 imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets));
7317
7318 /* Override by tuneable */
7319 if (scctx->isc_disable_msix)
7320 goto msi;
7321
7322 /* First try MSI-X */
7323 if ((msgs = pci_msix_count(dev)) == 0) {
7324 if (bootverbose)
7325 device_printf(dev, "MSI-X not supported or disabled\n");
7326 goto msi;
7327 }
7328
7329 bar = ctx->ifc_softc_ctx.isc_msix_bar;
7330 /*
7331 * bar == -1 => "trust me I know what I'm doing"
7332 * Some drivers are for hardware that is so shoddily
7333 * documented that no one knows which bars are which
7334 * so the developer has to map all bars. This hack
7335 * allows shoddy garbage to use MSI-X in this framework.
7336 */
7337 if (bar != -1) {
7338 ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
7339 SYS_RES_MEMORY, &bar, RF_ACTIVE);
7340 if (ctx->ifc_msix_mem == NULL) {
7341 device_printf(dev, "Unable to map MSI-X table\n");
7342 goto msi;
7343 }
7344 }
7345
7346 admincnt = sctx->isc_admin_intrcnt;
7347 #if IFLIB_DEBUG
7348 /* use only 1 qset in debug mode */
7349 queuemsgs = min(msgs - admincnt, 1);
7350 #else
7351 queuemsgs = msgs - admincnt;
7352 #endif
7353 #ifdef RSS
7354 queues = imin(queuemsgs, rss_getnumbuckets());
7355 #else
7356 queues = queuemsgs;
7357 #endif
7358 queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
7359 if (bootverbose)
7360 device_printf(dev,
7361 "intr CPUs: %d queue msgs: %d admincnt: %d\n",
7362 CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
7363 #ifdef RSS
7364 /* If we're doing RSS, clamp at the number of RSS buckets */
7365 if (queues > rss_getnumbuckets())
7366 queues = rss_getnumbuckets();
7367 #endif
7368 if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
7369 rx_queues = iflib_num_rx_queues;
7370 else
7371 rx_queues = queues;
7372
7373 if (rx_queues > scctx->isc_nrxqsets)
7374 rx_queues = scctx->isc_nrxqsets;
7375
7376 /*
7377 * We want this to be all logical CPUs by default
7378 */
7379 if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
7380 tx_queues = iflib_num_tx_queues;
7381 else
7382 tx_queues = mp_ncpus;
7383
7384 if (tx_queues > scctx->isc_ntxqsets)
7385 tx_queues = scctx->isc_ntxqsets;
7386
7387 if (ctx->ifc_sysctl_qs_eq_override == 0) {
7388 #ifdef INVARIANTS
7389 if (tx_queues != rx_queues)
7390 device_printf(dev,
7391 "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
7392 min(rx_queues, tx_queues), min(rx_queues, tx_queues));
7393 #endif
7394 tx_queues = min(rx_queues, tx_queues);
7395 rx_queues = min(rx_queues, tx_queues);
7396 }
7397
7398 vectors = rx_queues + admincnt;
7399 if (msgs < vectors) {
7400 device_printf(dev,
7401 "insufficient number of MSI-X vectors "
7402 "(supported %d, need %d)\n", msgs, vectors);
7403 goto msi;
7404 }
7405
7406 device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues,
7407 tx_queues);
7408 msgs = vectors;
7409 if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
7410 if (vectors != msgs) {
7411 device_printf(dev,
7412 "Unable to allocate sufficient MSI-X vectors "
7413 "(got %d, need %d)\n", vectors, msgs);
7414 pci_release_msi(dev);
7415 if (bar != -1) {
7416 bus_release_resource(dev, SYS_RES_MEMORY, bar,
7417 ctx->ifc_msix_mem);
7418 ctx->ifc_msix_mem = NULL;
7419 }
7420 goto msi;
7421 }
7422 device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
7423 vectors);
7424 scctx->isc_vectors = vectors;
7425 scctx->isc_nrxqsets = rx_queues;
7426 scctx->isc_ntxqsets = tx_queues;
7427 scctx->isc_intr = IFLIB_INTR_MSIX;
7428
7429 return (vectors);
7430 } else {
7431 device_printf(dev,
7432 "failed to allocate %d MSI-X vectors, err: %d\n", vectors,
7433 err);
7434 if (bar != -1) {
7435 bus_release_resource(dev, SYS_RES_MEMORY, bar,
7436 ctx->ifc_msix_mem);
7437 ctx->ifc_msix_mem = NULL;
7438 }
7439 }
7440
7441 msi:
7442 vectors = pci_msi_count(dev);
7443 scctx->isc_nrxqsets = 1;
7444 scctx->isc_ntxqsets = 1;
7445 scctx->isc_vectors = vectors;
7446 if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
7447 device_printf(dev, "Using an MSI interrupt\n");
7448 scctx->isc_intr = IFLIB_INTR_MSI;
7449 } else {
7450 scctx->isc_vectors = 1;
7451 device_printf(dev, "Using a Legacy interrupt\n");
7452 scctx->isc_intr = IFLIB_INTR_LEGACY;
7453 }
7454
7455 return (vectors);
7456 }
7457
7458 static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
7459
7460 static int
mp_ring_state_handler(SYSCTL_HANDLER_ARGS)7461 mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
7462 {
7463 int rc;
7464 uint16_t *state = ((uint16_t *)oidp->oid_arg1);
7465 struct sbuf *sb;
7466 const char *ring_state = "UNKNOWN";
7467
7468 /* XXX needed ? */
7469 rc = sysctl_wire_old_buffer(req, 0);
7470 MPASS(rc == 0);
7471 if (rc != 0)
7472 return (rc);
7473 sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
7474 MPASS(sb != NULL);
7475 if (sb == NULL)
7476 return (ENOMEM);
7477 if (state[3] <= 3)
7478 ring_state = ring_states[state[3]];
7479
7480 sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
7481 state[0], state[1], state[2], ring_state);
7482 rc = sbuf_finish(sb);
7483 sbuf_delete(sb);
7484 return (rc);
7485 }
7486
7487 enum iflib_ndesc_handler {
7488 IFLIB_NTXD_HANDLER,
7489 IFLIB_NRXD_HANDLER,
7490 };
7491
7492 static int
mp_ndesc_handler(SYSCTL_HANDLER_ARGS)7493 mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
7494 {
7495 if_ctx_t ctx = (void *)arg1;
7496 enum iflib_ndesc_handler type = arg2;
7497 char buf[256] = {0};
7498 qidx_t *ndesc;
7499 char *p, *next;
7500 int nqs, rc, i;
7501
7502 nqs = 8;
7503 switch (type) {
7504 case IFLIB_NTXD_HANDLER:
7505 ndesc = ctx->ifc_sysctl_ntxds;
7506 if (ctx->ifc_sctx)
7507 nqs = ctx->ifc_sctx->isc_ntxqs;
7508 break;
7509 case IFLIB_NRXD_HANDLER:
7510 ndesc = ctx->ifc_sysctl_nrxds;
7511 if (ctx->ifc_sctx)
7512 nqs = ctx->ifc_sctx->isc_nrxqs;
7513 break;
7514 default:
7515 printf("%s: unhandled type\n", __func__);
7516 return (EINVAL);
7517 }
7518 if (nqs == 0)
7519 nqs = 8;
7520
7521 for (i = 0; i < 8; i++) {
7522 if (i >= nqs)
7523 break;
7524 if (i)
7525 strcat(buf, ",");
7526 sprintf(strchr(buf, 0), "%d", ndesc[i]);
7527 }
7528
7529 rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
7530 if (rc || req->newptr == NULL)
7531 return (rc);
7532
7533 for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
7534 i++, p = strsep(&next, " ,")) {
7535 ndesc[i] = strtoul(p, NULL, 10);
7536 }
7537
7538 return (rc);
7539 }
7540
7541 static int
iflib_handle_tx_reclaim_thresh(SYSCTL_HANDLER_ARGS)7542 iflib_handle_tx_reclaim_thresh(SYSCTL_HANDLER_ARGS)
7543 {
7544 if_ctx_t ctx = (void *)arg1;
7545 iflib_txq_t txq;
7546 int i, err;
7547 int thresh;
7548
7549 thresh = ctx->ifc_sysctl_tx_reclaim_thresh;
7550 err = sysctl_handle_int(oidp, &thresh, arg2, req);
7551 if (err != 0) {
7552 return err;
7553 }
7554
7555 if (thresh == ctx->ifc_sysctl_tx_reclaim_thresh)
7556 return 0;
7557
7558 if (thresh > ctx->ifc_softc_ctx.isc_ntxd[0] / 2) {
7559 device_printf(ctx->ifc_dev, "TX Reclaim thresh must be <= %d\n",
7560 ctx->ifc_softc_ctx.isc_ntxd[0] / 2);
7561 return (EINVAL);
7562 }
7563
7564 ctx->ifc_sysctl_tx_reclaim_thresh = thresh;
7565 if (ctx->ifc_txqs == NULL)
7566 return (err);
7567
7568 txq = &ctx->ifc_txqs[0];
7569 for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
7570 txq->ift_reclaim_thresh = thresh;
7571 }
7572 return (err);
7573 }
7574
7575 static int
iflib_handle_tx_reclaim_ticks(SYSCTL_HANDLER_ARGS)7576 iflib_handle_tx_reclaim_ticks(SYSCTL_HANDLER_ARGS)
7577 {
7578 if_ctx_t ctx = (void *)arg1;
7579 iflib_txq_t txq;
7580 int i, err;
7581 int ticks;
7582
7583 ticks = ctx->ifc_sysctl_tx_reclaim_ticks;
7584 err = sysctl_handle_int(oidp, &ticks, arg2, req);
7585 if (err != 0) {
7586 return err;
7587 }
7588
7589 if (ticks == ctx->ifc_sysctl_tx_reclaim_ticks)
7590 return 0;
7591
7592 if (ticks > hz) {
7593 device_printf(ctx->ifc_dev,
7594 "TX Reclaim ticks must be <= hz (%d)\n", hz);
7595 return (EINVAL);
7596 }
7597
7598 ctx->ifc_sysctl_tx_reclaim_ticks = ticks;
7599 if (ctx->ifc_txqs == NULL)
7600 return (err);
7601
7602 txq = &ctx->ifc_txqs[0];
7603 for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
7604 txq->ift_reclaim_ticks = ticks;
7605 }
7606 return (err);
7607 }
7608
7609 static int
iflib_handle_tx_defer_mfree(SYSCTL_HANDLER_ARGS)7610 iflib_handle_tx_defer_mfree(SYSCTL_HANDLER_ARGS)
7611 {
7612 if_ctx_t ctx = (void *)arg1;
7613 iflib_txq_t txq;
7614 int i, err;
7615 int defer;
7616
7617 defer = ctx->ifc_sysctl_tx_defer_mfree;
7618 err = sysctl_handle_int(oidp, &defer, arg2, req);
7619 if (err != 0) {
7620 return err;
7621 }
7622
7623 if (defer == ctx->ifc_sysctl_tx_defer_mfree)
7624 return 0;
7625
7626 ctx->ifc_sysctl_tx_defer_mfree = defer;
7627 if (ctx->ifc_txqs == NULL)
7628 return (err);
7629
7630 txq = &ctx->ifc_txqs[0];
7631 for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
7632 txq->ift_defer_mfree = defer;
7633 }
7634 return (err);
7635 }
7636
7637 #define NAME_BUFLEN 32
7638 static void
iflib_add_device_sysctl_pre(if_ctx_t ctx)7639 iflib_add_device_sysctl_pre(if_ctx_t ctx)
7640 {
7641 device_t dev = iflib_get_dev(ctx);
7642 struct sysctl_oid_list *child, *oid_list;
7643 struct sysctl_oid *node;
7644
7645 sysctl_ctx_init(&ctx->ifc_sysctl_ctx);
7646 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
7647 ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(&ctx->ifc_sysctl_ctx, child,
7648 OID_AUTO, "iflib", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7649 "IFLIB fields");
7650 oid_list = SYSCTL_CHILDREN(node);
7651
7652 SYSCTL_ADD_CONST_STRING(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "driver_version",
7653 CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, "driver version");
7654 SYSCTL_ADD_U32(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO,
7655 "tx_watchdog_events", CTLFLAG_RD, &ctx->ifc_tx_watchdog_events, 0,
7656 "TX watchdog resets initiated by iflib");
7657
7658 ctx->ifc_sysctl_simple_tx = !iflib_prefer_mpring;
7659 SYSCTL_ADD_BOOL(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "simple_tx",
7660 CTLFLAG_RDTUN, &ctx->ifc_sysctl_simple_tx, 0,
7661 "use simple tx ring");
7662 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_ntxqs",
7663 CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
7664 "# of txqs to use, 0 => use default #");
7665 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_nrxqs",
7666 CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
7667 "# of rxqs to use, 0 => use default #");
7668 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_qs_enable",
7669 CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
7670 "permit #txq != #rxq");
7671 SYSCTL_ADD_INT(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "disable_msix",
7672 CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
7673 "disable MSI-X (default 0)");
7674 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "rx_budget",
7675 CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, "set the RX budget");
7676 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "tx_abdicate",
7677 CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0,
7678 "cause TX to abdicate instead of running to completion");
7679 ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED;
7680 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "core_offset",
7681 CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0,
7682 "offset to start using cores at");
7683 SYSCTL_ADD_U8(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "separate_txrx",
7684 CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0,
7685 "use separate cores for TX and RX");
7686 SYSCTL_ADD_U8(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "use_logical_cores",
7687 CTLFLAG_RDTUN, &ctx->ifc_sysctl_use_logical_cores, 0,
7688 "try to make use of logical cores for TX and RX");
7689 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "use_extra_msix_vectors",
7690 CTLFLAG_RDTUN, &ctx->ifc_sysctl_extra_msix_vectors, 0,
7691 "attempt to reserve the given number of extra MSI-X vectors during driver load for the creation of additional interfaces later");
7692 SYSCTL_ADD_INT(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "allocated_msix_vectors",
7693 CTLFLAG_RDTUN, &ctx->ifc_softc_ctx.isc_vectors, 0,
7694 "total # of MSI-X vectors allocated by driver");
7695
7696 /* XXX change for per-queue sizes */
7697 SYSCTL_ADD_PROC(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_ntxds",
7698 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
7699 IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A",
7700 "list of # of TX descriptors to use, 0 = use default #");
7701 SYSCTL_ADD_PROC(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_nrxds",
7702 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
7703 IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A",
7704 "list of # of RX descriptors to use, 0 = use default #");
7705 }
7706
7707 static void
iflib_add_device_sysctl_post(if_ctx_t ctx)7708 iflib_add_device_sysctl_post(if_ctx_t ctx)
7709 {
7710 if_shared_ctx_t sctx = ctx->ifc_sctx;
7711 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
7712 struct sysctl_oid_list *child;
7713 struct sysctl_ctx_list *ctx_list = &ctx->ifc_sysctl_ctx;
7714 iflib_fl_t fl;
7715 iflib_txq_t txq;
7716 iflib_rxq_t rxq;
7717 int i, j;
7718 char namebuf[NAME_BUFLEN];
7719 char *qfmt;
7720 struct sysctl_oid *queue_node, *fl_node, *node;
7721 struct sysctl_oid_list *queue_list, *fl_list;
7722
7723 node = ctx->ifc_sysctl_node;
7724 child = SYSCTL_CHILDREN(node);
7725
7726 SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "tx_reclaim_thresh",
7727 CTLTYPE_INT | CTLFLAG_RWTUN, ctx,
7728 0, iflib_handle_tx_reclaim_thresh, "I",
7729 "Number of TX descs outstanding before reclaim is called");
7730
7731 SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "tx_reclaim_ticks",
7732 CTLTYPE_INT | CTLFLAG_RWTUN, ctx,
7733 0, iflib_handle_tx_reclaim_ticks, "I",
7734 "Number of ticks before a TX reclaim is forced");
7735
7736 SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "tx_defer_mfree",
7737 CTLTYPE_INT | CTLFLAG_RWTUN, ctx,
7738 0, iflib_handle_tx_defer_mfree, "I",
7739 "Free completed transmits outside of TX ring lock");
7740
7741 if (scctx->isc_ntxqsets > 100)
7742 qfmt = "txq%03d";
7743 else if (scctx->isc_ntxqsets > 10)
7744 qfmt = "txq%02d";
7745 else
7746 qfmt = "txq%d";
7747 for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
7748 snprintf(namebuf, NAME_BUFLEN, qfmt, i);
7749 queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
7750 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
7751 queue_list = SYSCTL_CHILDREN(queue_node);
7752 SYSCTL_ADD_INT(ctx_list, queue_list, OID_AUTO, "cpu",
7753 CTLFLAG_RD, &txq->ift_task.gt_cpu, 0,
7754 "cpu this queue is bound to");
7755 #if MEMORY_LOGGING
7756 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
7757 CTLFLAG_RD, &txq->ift_dequeued, "total mbufs freed");
7758 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
7759 CTLFLAG_RD, &txq->ift_enqueued, "total mbufs enqueued");
7760 #endif
7761 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
7762 CTLFLAG_RD, &txq->ift_mbuf_defrag,
7763 "# of times m_defrag was called");
7764 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
7765 CTLFLAG_RD, &txq->ift_pullups,
7766 "# of times m_pullup was called");
7767 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7768 "mbuf_defrag_failed", CTLFLAG_RD,
7769 &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
7770 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7771 "no_desc_avail", CTLFLAG_RD, &txq->ift_no_desc_avail,
7772 "# of times no descriptors were available");
7773 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7774 "tx_map_failed", CTLFLAG_RD, &txq->ift_map_failed,
7775 "# of times DMA map failed");
7776 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7777 "txd_encap_efbig", CTLFLAG_RD, &txq->ift_txd_encap_efbig,
7778 "# of times txd_encap returned EFBIG");
7779 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7780 "no_tx_dma_setup", CTLFLAG_RD, &txq->ift_no_tx_dma_setup,
7781 "# of times map failed for other than EFBIG");
7782 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
7783 CTLFLAG_RD, &txq->ift_pidx, 1, "Producer Index");
7784 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
7785 CTLFLAG_RD, &txq->ift_cidx, 1, "Consumer Index");
7786 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO,
7787 "txq_cidx_processed", CTLFLAG_RD, &txq->ift_cidx_processed,
7788 1, "Consumer Index seen by credit update");
7789 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
7790 CTLFLAG_RD, &txq->ift_in_use, 1, "descriptors in use");
7791 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7792 "txq_processed", CTLFLAG_RD, &txq->ift_processed,
7793 "descriptors procesed for clean");
7794 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
7795 CTLFLAG_RD, &txq->ift_cleaned, "total cleaned");
7796 SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
7797 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
7798 __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0,
7799 mp_ring_state_handler, "A", "soft ring state");
7800 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7801 "r_enqueues", CTLFLAG_RD, &txq->ift_br->enqueues,
7802 "# of enqueues to the mp_ring for this queue");
7803 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7804 "r_drops", CTLFLAG_RD, &txq->ift_br->drops,
7805 "# of drops in the mp_ring for this queue");
7806 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7807 "r_starts", CTLFLAG_RD, &txq->ift_br->starts,
7808 "# of normal consumer starts in mp_ring for this queue");
7809 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7810 "r_stalls", CTLFLAG_RD, &txq->ift_br->stalls,
7811 "# of consumer stalls in the mp_ring for this queue");
7812 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7813 "r_restarts", CTLFLAG_RD, &txq->ift_br->restarts,
7814 "# of consumer restarts in the mp_ring for this queue");
7815 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7816 "r_abdications", CTLFLAG_RD, &txq->ift_br->abdications,
7817 "# of consumer abdications in the mp_ring for this queue");
7818 if (txq->ift_drbr == NULL)
7819 continue;
7820 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7821 "drbr_direct", CTLFLAG_RD, &txq->ift_drbr_direct,
7822 "# of packets sent without touching the deferral ring");
7823 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
7824 "drbr_stall", CTLFLAG_RD, &txq->ift_drbr_stall,
7825 "# of times the drain stopped with no descriptors free");
7826 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7827 "drbr_deferred", CTLFLAG_RD, &txq->ift_drbr_deferred,
7828 "# of packets deferred after losing the tx trylock");
7829 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7830 "drbr_drops", CTLFLAG_RD, &txq->ift_drbr_drops,
7831 "# of packets dropped because the deferral ring was full");
7832 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7833 "drbr_blocked", CTLFLAG_RD, &txq->ift_drbr_blocked,
7834 "# of times a full deferral ring forced a tx lock wait");
7835 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
7836 "drbr_remote", CTLFLAG_RD, &txq->ift_drbr_remote,
7837 "# of times a different llc or producer limit forced a tx "
7838 "lock wait");
7839 }
7840
7841 if (scctx->isc_nrxqsets > 100)
7842 qfmt = "rxq%03d";
7843 else if (scctx->isc_nrxqsets > 10)
7844 qfmt = "rxq%02d";
7845 else
7846 qfmt = "rxq%d";
7847 for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
7848 snprintf(namebuf, NAME_BUFLEN, qfmt, i);
7849 queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
7850 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
7851 queue_list = SYSCTL_CHILDREN(queue_node);
7852 SYSCTL_ADD_INT(ctx_list, queue_list, OID_AUTO, "cpu",
7853 CTLFLAG_RD, &rxq->ifr_task.gt_cpu, 0,
7854 "cpu this queue is bound to");
7855 if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
7856 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO,
7857 "rxq_cq_cidx", CTLFLAG_RD, &rxq->ifr_cq_cidx, 1,
7858 "Consumer Index");
7859 }
7860
7861 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
7862 snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
7863 fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list,
7864 OID_AUTO, namebuf, CTLFLAG_RD | CTLFLAG_MPSAFE,
7865 NULL, "freelist Name");
7866 fl_list = SYSCTL_CHILDREN(fl_node);
7867 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
7868 CTLFLAG_RD, &fl->ifl_pidx, 1, "Producer Index");
7869 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
7870 CTLFLAG_RD, &fl->ifl_cidx, 1, "Consumer Index");
7871 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
7872 CTLFLAG_RD, &fl->ifl_credits, 1,
7873 "credits available");
7874 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size",
7875 CTLFLAG_RD, &fl->ifl_buf_size, 1, "buffer size");
7876 #if MEMORY_LOGGING
7877 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
7878 "fl_m_enqueued", CTLFLAG_RD, &fl->ifl_m_enqueued,
7879 "mbufs allocated");
7880 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
7881 "fl_m_dequeued", CTLFLAG_RD, &fl->ifl_m_dequeued,
7882 "mbufs freed");
7883 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
7884 "fl_cl_enqueued", CTLFLAG_RD, &fl->ifl_cl_enqueued,
7885 "clusters allocated");
7886 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
7887 "fl_cl_dequeued", CTLFLAG_RD, &fl->ifl_cl_dequeued,
7888 "clusters freed");
7889 #endif
7890 }
7891 }
7892
7893 }
7894
7895 void
iflib_request_reset(if_ctx_t ctx)7896 iflib_request_reset(if_ctx_t ctx)
7897 {
7898
7899 STATE_LOCK(ctx);
7900 ctx->ifc_flags |= IFC_DO_RESET;
7901 STATE_UNLOCK(ctx);
7902 }
7903
7904 void
iflib_request_reset_if_up(if_ctx_t ctx)7905 iflib_request_reset_if_up(if_ctx_t ctx)
7906 {
7907
7908 STATE_LOCK(ctx);
7909 ctx->ifc_flags |= IFC_DO_RESET_IF_UP;
7910 STATE_UNLOCK(ctx);
7911 }
7912
7913 void
iflib_init_failed(if_ctx_t ctx)7914 iflib_init_failed(if_ctx_t ctx)
7915 {
7916
7917 sx_assert(&ctx->ifc_ctx_sx, SA_XLOCKED);
7918 KASSERT(ctx->ifc_datapath_state == IFLIB_DP_STARTING,
7919 ("iflib_init_failed outside IFDI_INIT, state %d",
7920 ctx->ifc_datapath_state));
7921 STATE_LOCK(ctx);
7922 ctx->ifc_flags |= IFC_INIT_FAILED;
7923 STATE_UNLOCK(ctx);
7924 }
7925
7926 #ifndef __NO_STRICT_ALIGNMENT
7927 static struct mbuf *
iflib_fixup_rx(struct mbuf * m)7928 iflib_fixup_rx(struct mbuf *m)
7929 {
7930 struct mbuf *n;
7931
7932 if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
7933 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
7934 m->m_data += ETHER_HDR_LEN;
7935 n = m;
7936 } else {
7937 MGETHDR(n, M_NOWAIT, MT_DATA);
7938 if (n == NULL) {
7939 m_freem(m);
7940 return (NULL);
7941 }
7942 bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
7943 m->m_data += ETHER_HDR_LEN;
7944 m->m_len -= ETHER_HDR_LEN;
7945 n->m_len = ETHER_HDR_LEN;
7946 M_MOVE_PKTHDR(n, m);
7947 n->m_next = m;
7948 }
7949 return (n);
7950 }
7951 #endif
7952
7953 #ifdef DEBUGNET
7954 static void
iflib_debugnet_init(if_t ifp,int * nrxr,int * ncl,int * clsize)7955 iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
7956 {
7957 if_ctx_t ctx;
7958
7959 ctx = if_getsoftc(ifp);
7960 CTX_LOCK(ctx);
7961 *nrxr = NRXQSETS(ctx);
7962 *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size;
7963 *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size;
7964 CTX_UNLOCK(ctx);
7965 }
7966
7967 static void
iflib_debugnet_event(if_t ifp,enum debugnet_ev event)7968 iflib_debugnet_event(if_t ifp, enum debugnet_ev event)
7969 {
7970 if_ctx_t ctx;
7971 if_softc_ctx_t scctx;
7972 iflib_fl_t fl;
7973 iflib_rxq_t rxq;
7974 int i, j;
7975
7976 ctx = if_getsoftc(ifp);
7977 scctx = &ctx->ifc_softc_ctx;
7978
7979 switch (event) {
7980 case DEBUGNET_START:
7981 for (i = 0; i < scctx->isc_nrxqsets; i++) {
7982 rxq = &ctx->ifc_rxqs[i];
7983 for (j = 0; j < rxq->ifr_nfl; j++) {
7984 fl = rxq->ifr_fl;
7985 fl->ifl_zone = m_getzone(fl->ifl_buf_size);
7986 }
7987 }
7988 iflib_no_tx_batch = 1;
7989 break;
7990 default:
7991 break;
7992 }
7993 }
7994
7995 static int
iflib_debugnet_transmit(if_t ifp,struct mbuf * m)7996 iflib_debugnet_transmit(if_t ifp, struct mbuf *m)
7997 {
7998 if_ctx_t ctx;
7999 iflib_txq_t txq;
8000 int error;
8001 int bytes_sent = 0;
8002 int pkt_sent = 0;
8003
8004 ctx = if_getsoftc(ifp);
8005 if (!iflib_is_running(ctx))
8006 return (EBUSY);
8007
8008 txq = &ctx->ifc_txqs[0];
8009 error = iflib_encap(txq, &m, &bytes_sent, &pkt_sent);
8010 if (error == 0)
8011 (void)iflib_txd_db_check(txq, true);
8012 return (error);
8013 }
8014
8015 static int
iflib_debugnet_poll(if_t ifp,int count)8016 iflib_debugnet_poll(if_t ifp, int count)
8017 {
8018 struct epoch_tracker et;
8019 if_ctx_t ctx;
8020 if_softc_ctx_t scctx;
8021 iflib_txq_t txq;
8022 int i;
8023
8024 ctx = if_getsoftc(ifp);
8025 scctx = &ctx->ifc_softc_ctx;
8026
8027 if (!iflib_is_running(ctx))
8028 return (EBUSY);
8029
8030 txq = &ctx->ifc_txqs[0];
8031 (void)iflib_completed_tx_reclaim(txq, NULL);
8032
8033 NET_EPOCH_ENTER(et);
8034 for (i = 0; i < scctx->isc_nrxqsets; i++)
8035 (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */);
8036 NET_EPOCH_EXIT(et);
8037 return (0);
8038 }
8039 #endif /* DEBUGNET */
8040
8041 enum iflib_txq_producer_status {
8042 IFLIB_TXQ_PRODUCER_ENTERED,
8043 IFLIB_TXQ_PRODUCER_QUIESCING,
8044 IFLIB_TXQ_PRODUCER_REMOTE,
8045 };
8046
8047 /*
8048 * Keep the most recent producer llc when the count reaches zero. Producers
8049 * in that llc can use fetchadd without contending on a compare-and-swap.
8050 * Another llc can take ownership only while the producer count is zero.
8051 */
8052 static __inline enum iflib_txq_producer_status
iflib_txq_producer_enter(iflib_txq_t txq,bool * pinned)8053 iflib_txq_producer_enter(iflib_txq_t txq, bool *pinned)
8054 {
8055 u_int count, llc_id, max_producers, newstate, old, state;
8056
8057 if (!iflib_producer_gate) {
8058 state = atomic_fetchadd_int(&txq->ift_producers, 1);
8059 if (__predict_false((state & IFLIB_TXQ_QUIESCING) != 0)) {
8060 atomic_subtract_int(&txq->ift_producers, 1);
8061 return (IFLIB_TXQ_PRODUCER_QUIESCING);
8062 }
8063 *pinned = false;
8064 return (IFLIB_TXQ_PRODUCER_ENTERED);
8065 }
8066
8067 max_producers = iflib_max_producers;
8068 if (iflib_single_llc) {
8069 state = atomic_fetchadd_int(&txq->ift_producers, 1);
8070 if (__predict_false((state & IFLIB_TXQ_QUIESCING) != 0)) {
8071 atomic_subtract_int(&txq->ift_producers, 1);
8072 return (IFLIB_TXQ_PRODUCER_QUIESCING);
8073 }
8074 count = IFLIB_TXQ_PRODUCER(state);
8075 if (count >= max_producers ||
8076 count == IFLIB_TXQ_PRODUCER_MAX) {
8077 atomic_subtract_int(&txq->ift_producers, 1);
8078 return (IFLIB_TXQ_PRODUCER_REMOTE);
8079 }
8080 *pinned = false;
8081 return (IFLIB_TXQ_PRODUCER_ENTERED);
8082 }
8083
8084 sched_pin();
8085 llc_id = iflib_cpu_llc[curcpu];
8086 state = atomic_load_acq_int(&txq->ift_producers);
8087 for (;;) {
8088 if ((state & IFLIB_TXQ_QUIESCING) != 0) {
8089 sched_unpin();
8090 return (IFLIB_TXQ_PRODUCER_QUIESCING);
8091 }
8092
8093 count = IFLIB_TXQ_PRODUCER(state);
8094 if (count >= max_producers ||
8095 count == IFLIB_TXQ_PRODUCER_MAX) {
8096 sched_unpin();
8097 return (IFLIB_TXQ_PRODUCER_REMOTE);
8098 }
8099 if (IFLIB_TXQ_PRODUCER_LLC(state) == llc_id) {
8100 /*
8101 * The llc can change between the load and fetchadd only
8102 * if the count was zero. Validate the returned
8103 * state and undo the increment if ownership changed.
8104 */
8105 old = atomic_fetchadd_int(&txq->ift_producers, 1);
8106 if ((old & IFLIB_TXQ_QUIESCING) != 0) {
8107 atomic_subtract_int(&txq->ift_producers, 1);
8108 sched_unpin();
8109 return (IFLIB_TXQ_PRODUCER_QUIESCING);
8110 }
8111 count = IFLIB_TXQ_PRODUCER(old);
8112 if (IFLIB_TXQ_PRODUCER_LLC(old) == llc_id &&
8113 count < max_producers &&
8114 count != IFLIB_TXQ_PRODUCER_MAX) {
8115 *pinned = true;
8116 return (IFLIB_TXQ_PRODUCER_ENTERED);
8117 }
8118
8119 atomic_subtract_int(&txq->ift_producers, 1);
8120 sched_unpin();
8121 return (IFLIB_TXQ_PRODUCER_REMOTE);
8122 }
8123
8124 if (count != 0) {
8125 sched_unpin();
8126 return (IFLIB_TXQ_PRODUCER_REMOTE);
8127 }
8128
8129 newstate = llc_id << IFLIB_TXQ_PRODUCER_LLC_SHIFT;
8130 newstate |= 1;
8131 if (atomic_fcmpset_acq_int(&txq->ift_producers, &state,
8132 newstate)) {
8133 *pinned = true;
8134 return (IFLIB_TXQ_PRODUCER_ENTERED);
8135 }
8136 }
8137 }
8138
8139 static __inline void
iflib_txq_producer_exit(iflib_txq_t txq,bool pinned)8140 iflib_txq_producer_exit(iflib_txq_t txq, bool pinned)
8141 {
8142 u_int state __diagused;
8143
8144 if (!pinned) {
8145 atomic_subtract_rel_int(&txq->ift_producers, 1);
8146 return;
8147 }
8148
8149 atomic_thread_fence_rel();
8150 state = atomic_fetchadd_int(&txq->ift_producers, -1);
8151 KASSERT(IFLIB_TXQ_PRODUCER(state) != 0,
8152 ("%s: producer count underflow", __func__));
8153 KASSERT(IFLIB_TXQ_PRODUCER_LLC(state) == iflib_cpu_llc[curcpu],
8154 ("%s: producer llc changed", __func__));
8155 sched_unpin();
8156 }
8157
8158 /* Consumes the mbuf in all cases */
8159 static int
iflib_simple_encap(iflib_txq_t txq,struct mbuf * m,int * bytes,int * pkts,int * mcasts)8160 iflib_simple_encap(iflib_txq_t txq, struct mbuf *m, int *bytes, int *pkts,
8161 int *mcasts)
8162 {
8163 if_t ifp;
8164 int error;
8165
8166 mtx_assert(&txq->ift_mtx, MA_OWNED);
8167 ifp = txq->ift_ctx->ifc_ifp;
8168
8169 error = iflib_encap(txq, &m, bytes, pkts);
8170 if (__predict_false(error != 0)) {
8171 /* iflib_encap() always frees the mbuf on failures */
8172 if (error == ENOBUFS)
8173 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
8174 else
8175 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
8176 return (error);
8177 }
8178 *mcasts += !!(m->m_flags & M_MCAST);
8179 DBG_COUNTER_INC(tx_sent);
8180 ETHER_BPF_MTAP(ifp, m);
8181 (void)iflib_txd_db_check(txq, false);
8182 return (0);
8183 }
8184
8185 /* Drain the deferral ring into the hardware. */
8186 static void
iflib_simple_drbr_drain(iflib_txq_t txq,u_int quota,int * bytes,int * pkts,int * mcasts)8187 iflib_simple_drbr_drain(iflib_txq_t txq, u_int quota, int *bytes, int *pkts,
8188 int *mcasts)
8189 {
8190 if_ctx_t ctx;
8191 struct mbuf *m;
8192 if_t ifp;
8193 u_int i;
8194
8195 mtx_assert(&txq->ift_mtx, MA_OWNED);
8196 ctx = txq->ift_ctx;
8197 ifp = ctx->ifc_ifp;
8198 if (__predict_false(!iflib_is_running(ctx) || !LINK_ACTIVE(ctx)))
8199 return;
8200
8201 #ifdef ALTQ
8202 /* We only drain from txq0 when altq is enabled. */
8203 if (__predict_false(if_altq_is_enabled(ifp) && txq->ift_id != 0))
8204 return;
8205 #endif
8206 for (i = 0; TXQ_AVAIL(txq) >= MAX_TX_DESC(ctx); i++) {
8207 if (i == quota)
8208 return;
8209 m = drbr_dequeue(ifp, txq->ift_drbr);
8210 if (m == NULL)
8211 return;
8212 (void)iflib_simple_encap(txq, m, bytes, pkts, mcasts);
8213 }
8214 if (!drbr_empty(ifp, txq->ift_drbr)) {
8215 txq->ift_drbr_stall++;
8216 if (quota != iflib_simple_drain_quota &&
8217 (txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
8218 GROUPTASK_ENQUEUE(&txq->ift_task);
8219 }
8220 }
8221
8222 /*
8223 * Reclaim completed descriptors and push out anything that was deferred while
8224 * the tx lock was held. Called from tx completion and from the timer.
8225 * A thread already holding the lock is draining, and will re-arm us if it
8226 * cannot finish, so never wait for it here.
8227 */
8228 static void
iflib_simple_txq_drain(iflib_txq_t txq)8229 iflib_simple_txq_drain(iflib_txq_t txq)
8230 {
8231 if_t ifp;
8232 int bytes_sent = 0, pkt_sent = 0, mcast_sent = 0;
8233
8234 ifp = txq->ift_ctx->ifc_ifp;
8235
8236 if (!mtx_trylock(&txq->ift_mtx))
8237 return;
8238
8239 if ((atomic_load_acq_int(&txq->ift_producers) & IFLIB_TXQ_QUIESCING)
8240 != 0) {
8241 mtx_unlock(&txq->ift_mtx);
8242 return;
8243 }
8244
8245 (void)iflib_completed_tx_reclaim(txq, NULL);
8246 if (!drbr_empty(ifp, txq->ift_drbr))
8247 iflib_simple_drbr_drain(txq, iflib_simple_drain_quota,
8248 &bytes_sent, &pkt_sent, &mcast_sent);
8249 if (txq->ift_db_pending != 0)
8250 (void)iflib_txd_db_check(txq, true);
8251 mtx_unlock(&txq->ift_mtx);
8252
8253 if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
8254 if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
8255 if (mcast_sent)
8256 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
8257 }
8258
8259 /*
8260 * When nothing is queued ahead of us there is no ordering constraint,
8261 * so the mbuf goes straight to the hardware and the deferral ring is
8262 * never touched.
8263 */
8264 static int
iflib_simple_transmit_locked(iflib_txq_t txq,struct mbuf * m,int * bytes,int * pkts,int * mcasts)8265 iflib_simple_transmit_locked(iflib_txq_t txq, struct mbuf *m, int *bytes,
8266 int *pkts, int *mcasts)
8267 {
8268 if_ctx_t ctx;
8269 if_t ifp;
8270 int error;
8271
8272 mtx_assert(&txq->ift_mtx, MA_OWNED);
8273 ctx = txq->ift_ctx;
8274 ifp = ctx->ifc_ifp;
8275
8276 if (__predict_true(!drbr_needs_enqueue(ifp, txq->ift_drbr) &&
8277 TXQ_AVAIL(txq) >= MAX_TX_DESC(ctx))) {
8278 txq->ift_drbr_direct++;
8279 error = iflib_simple_encap(txq, m, bytes, pkts, mcasts);
8280 } else {
8281 error = buf_ring_enqueue(txq->ift_drbr, m);
8282 if (__predict_false(error != 0)) {
8283 m_freem(m);
8284 DBG_COUNTER_INC(tx_frees);
8285 counter_u64_add(txq->ift_drbr_drops, 1);
8286 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1);
8287 }
8288 }
8289
8290 /*
8291 * Other transmitters may have deferred to the ring while we were in
8292 * iflib_encap(), so always check again before dropping the lock. We
8293 * are the only thread that can drain it.
8294 */
8295 if (!drbr_empty(ifp, txq->ift_drbr))
8296 iflib_simple_drbr_drain(txq, iflib_simple_drain_quota_thread,
8297 bytes, pkts, mcasts);
8298 return (error);
8299 }
8300
8301 /*
8302 * Always inline the common transmit path so queue selection does not add a
8303 * function call to the default simple transmit path.
8304 */
8305 static __always_inline int
iflib_simple_transmit_impl(if_ctx_t ctx,if_t ifp,struct mbuf * m,iflib_txq_t txq)8306 iflib_simple_transmit_impl(if_ctx_t ctx, if_t ifp, struct mbuf *m,
8307 iflib_txq_t txq)
8308 {
8309 struct mbuf **m_defer;
8310 enum iflib_txq_producer_status producer_status;
8311 bool pinned;
8312 int error, i, reclaimable;
8313 int bytes_sent = 0, pkt_sent = 0, mcast_sent = 0;
8314
8315
8316 #ifdef ALTQ
8317 if (if_altq_is_enabled(ifp)) {
8318 IFQ_ENQUEUE(&ifp->if_snd, m, error); /* XXX - DRVAPI */
8319 if (error == 0)
8320 if_start(ifp);
8321 return (error);
8322 }
8323 #endif
8324
8325 ctx = if_getsoftc(ifp);
8326 if (__predict_false(!iflib_is_running(ctx) || !LINK_ACTIVE(ctx)))
8327 goto net_down;
8328
8329 /*
8330 * Avoid blocking behind another transmitter; the ring is drained by
8331 * whoever holds ift_mtx, by tx completion, or by the watchdog timer.
8332 */
8333 if (__predict_false(!mtx_trylock(&txq->ift_mtx))) {
8334 producer_status = iflib_txq_producer_enter(txq, &pinned);
8335 if (producer_status == IFLIB_TXQ_PRODUCER_QUIESCING)
8336 goto net_down;
8337
8338 if (producer_status == IFLIB_TXQ_PRODUCER_ENTERED) {
8339 error = buf_ring_enqueue(txq->ift_drbr, m);
8340 iflib_txq_producer_exit(txq, pinned);
8341 if (__predict_true(error == 0)) {
8342 counter_u64_add(txq->ift_drbr_deferred, 1);
8343 return (0);
8344 }
8345 counter_u64_add(txq->ift_drbr_blocked, 1);
8346 } else {
8347 counter_u64_add(txq->ift_drbr_remote, 1);
8348 }
8349 mtx_lock(&txq->ift_mtx);
8350 }
8351
8352 if (__predict_false(atomic_load_acq_int(&txq->ift_producers) &
8353 IFLIB_TXQ_QUIESCING)) {
8354 mtx_unlock(&txq->ift_mtx);
8355 goto net_down;
8356 }
8357
8358 error = iflib_simple_transmit_locked(txq, m, &bytes_sent, &pkt_sent,
8359 &mcast_sent);
8360 if (txq->ift_db_pending != 0)
8361 (void)iflib_txd_db_check(txq, true);
8362 m_defer = NULL;
8363 reclaimable = iflib_txq_can_reclaim(txq);
8364 if (reclaimable != 0) {
8365 /*
8366 * Try to set m_defer to the deferred mbuf reclaim array. If
8367 * we can, the frees will happen outside the tx lock. If we
8368 * can't, it means another thread is still proccessing frees.
8369 */
8370 if (txq->ift_defer_mfree &&
8371 atomic_cmpset_acq_ptr((uintptr_t *)&txq->ift_sds.ifsd_m_defer,
8372 (uintptr_t )txq->ift_sds.ifsd_m_deferb, 0)) {
8373 m_defer = txq->ift_sds.ifsd_m_deferb;
8374 }
8375 _iflib_completed_tx_reclaim(txq, m_defer, reclaimable);
8376 }
8377 mtx_unlock(&txq->ift_mtx);
8378
8379 /*
8380 * Process mbuf frees outside the tx lock
8381 */
8382 if (m_defer != NULL) {
8383 for (i = 0; m_defer[i] != NULL; i++) {
8384 m_freem(m_defer[i]);
8385 m_defer[i] = NULL;
8386 }
8387 atomic_store_rel_ptr((uintptr_t *)&txq->ift_sds.ifsd_m_defer,
8388 (uintptr_t)m_defer);
8389 }
8390 if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
8391 if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
8392 if (mcast_sent)
8393 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
8394
8395 return (error);
8396
8397 net_down:
8398 m_freem(m);
8399 DBG_COUNTER_INC(tx_frees);
8400 return (ENETDOWN);
8401 }
8402
8403 static int
iflib_simple_transmit(if_t ifp,struct mbuf * m)8404 iflib_simple_transmit(if_t ifp, struct mbuf *m)
8405 {
8406 if_ctx_t ctx;
8407 iflib_txq_t txq;
8408 int qidx;
8409
8410 ctx = if_getsoftc(ifp);
8411 if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
8412 qidx = QIDX(ctx, m);
8413 else
8414 qidx = NTXQSETS(ctx) + FIRST_QSET(ctx) - 1;
8415 MPASS(qidx < NTXQSETS(ctx));
8416 txq = &ctx->ifc_txqs[qidx];
8417 return (iflib_simple_transmit_impl(ctx, ifp, m, txq));
8418 }
8419
8420 static int
iflib_simple_transmit_txq_select(if_t ifp,struct mbuf * m)8421 iflib_simple_transmit_txq_select(if_t ifp, struct mbuf *m)
8422 {
8423 if_ctx_t ctx;
8424 int qidx;
8425
8426 ctx = if_getsoftc(ifp);
8427 qidx = ctx->isc_txq_select(ctx->ifc_softc, m);
8428 MPASS(qidx < NTXQSETS(ctx));
8429 return (iflib_simple_transmit_impl(ctx, ifp, m,
8430 &ctx->ifc_txqs[qidx]));
8431 }
8432
8433 static int
iflib_simple_transmit_txq_select_v2(if_t ifp,struct mbuf * m)8434 iflib_simple_transmit_txq_select_v2(if_t ifp, struct mbuf *m)
8435 {
8436 struct if_pkt_info pi;
8437 if_ctx_t ctx;
8438 uint64_t early_pullups = 0;
8439 int error, qidx;
8440
8441 ctx = if_getsoftc(ifp);
8442 memset(&pi, 0, sizeof(pi));
8443 error = iflib_parse_header_partial(&pi, &m, &early_pullups);
8444 if (error != 0) {
8445 /* Assign pullups for bad packets to the default queue. */
8446 ctx->ifc_txqs[0].ift_pullups += early_pullups;
8447 DBG_COUNTER_INC(encap_txd_encap_fail);
8448 return (error);
8449 }
8450 qidx = ctx->isc_txq_select_v2(ctx->ifc_softc, m, &pi);
8451 MPASS(qidx < NTXQSETS(ctx));
8452 ctx->ifc_txqs[qidx].ift_pullups += early_pullups;
8453 return (iflib_simple_transmit_impl(ctx, ifp, m,
8454 &ctx->ifc_txqs[qidx]));
8455 }
8456
8457 /*
8458 * ALTQ entry point. drbr_dequeue() pulls from ifp->if_snd when a discipline
8459 * is attached, so the drain loop is shared with the if_transmit path. Only
8460 * queue zero is used, matching the queue ALTQ itself selects.
8461 */
8462 static void
iflib_simple_if_start(if_t ifp)8463 iflib_simple_if_start(if_t ifp)
8464 {
8465 if_ctx_t ctx;
8466 iflib_txq_t txq;
8467 bool retry;
8468 int bytes_sent = 0, pkt_sent = 0, mcast_sent = 0;
8469
8470 ctx = if_getsoftc(ifp);
8471 txq = &ctx->ifc_txqs[0];
8472
8473 mtx_lock(&txq->ift_mtx);
8474 (void)iflib_completed_tx_reclaim(txq, NULL);
8475 iflib_simple_drbr_drain(txq, UINT_MAX, &bytes_sent, &pkt_sent,
8476 &mcast_sent);
8477 if (txq->ift_db_pending != 0)
8478 (void)iflib_txd_db_check(txq, true);
8479 retry = !drbr_empty(ifp, txq->ift_drbr);
8480 mtx_unlock(&txq->ift_mtx);
8481
8482 if (retry)
8483 GROUPTASK_ENQUEUE(&txq->ift_task);
8484
8485 if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
8486 if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
8487 if (mcast_sent)
8488 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
8489 }
8490