]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/iflib.c
MFV: r313101
[FreeBSD/FreeBSD.git] / sys / net / iflib.c
1 /*-
2  * Copyright (c) 2014-2017, Matthew Macy <mmacy@nextbsd.org>
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 __FBSDID("$FreeBSD$");
30
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_acpi.h"
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/bus.h>
38 #include <sys/eventhandler.h>
39 #include <sys/sockio.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/module.h>
44 #include <sys/kobj.h>
45 #include <sys/rman.h>
46 #include <sys/sbuf.h>
47 #include <sys/smp.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/syslog.h>
51 #include <sys/taskqueue.h>
52 #include <sys/limits.h>
53
54
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 #include <net/mp_ring.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/tcp_lro.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip6.h>
70 #include <netinet/tcp.h>
71
72 #include <machine/bus.h>
73 #include <machine/in_cksum.h>
74
75 #include <vm/vm.h>
76 #include <vm/pmap.h>
77
78 #include <dev/led/led.h>
79 #include <dev/pci/pcireg.h>
80 #include <dev/pci/pcivar.h>
81 #include <dev/pci/pci_private.h>
82
83 #include <net/iflib.h>
84
85 #include "ifdi_if.h"
86
87 #if defined(__i386__) || defined(__amd64__)
88 #include <sys/memdesc.h>
89 #include <machine/bus.h>
90 #include <machine/md_var.h>
91 #include <machine/specialreg.h>
92 #include <x86/include/busdma_impl.h>
93 #include <x86/iommu/busdma_dmar.h>
94 #endif
95
96 /*
97  * enable accounting of every mbuf as it comes in to and goes out of iflib's software descriptor references
98  */
99 #define MEMORY_LOGGING 0
100 /*
101  * Enable mbuf vectors for compressing long mbuf chains
102  */
103
104 /*
105  * NB:
106  * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
107  *   we prefetch needs to be determined by the time spent in m_free vis a vis
108  *   the cost of a prefetch. This will of course vary based on the workload:
109  *      - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
110  *        is quite expensive, thus suggesting very little prefetch.
111  *      - small packet forwarding which is just returning a single mbuf to
112  *        UMA will typically be very fast vis a vis the cost of a memory
113  *        access.
114  */
115
116
117 /*
118  * File organization:
119  *  - private structures
120  *  - iflib private utility functions
121  *  - ifnet functions
122  *  - vlan registry and other exported functions
123  *  - iflib public core functions
124  *
125  *
126  */
127 static MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
128
129 struct iflib_txq;
130 typedef struct iflib_txq *iflib_txq_t;
131 struct iflib_rxq;
132 typedef struct iflib_rxq *iflib_rxq_t;
133 struct iflib_fl;
134 typedef struct iflib_fl *iflib_fl_t;
135
136 struct iflib_ctx;
137
138 typedef struct iflib_filter_info {
139         driver_filter_t *ifi_filter;
140         void *ifi_filter_arg;
141         struct grouptask *ifi_task;
142         struct iflib_ctx *ifi_ctx;
143 } *iflib_filter_info_t;
144
145 struct iflib_ctx {
146         KOBJ_FIELDS;
147    /*
148    * Pointer to hardware driver's softc
149    */
150         void *ifc_softc;
151         device_t ifc_dev;
152         if_t ifc_ifp;
153
154         cpuset_t ifc_cpus;
155         if_shared_ctx_t ifc_sctx;
156         struct if_softc_ctx ifc_softc_ctx;
157
158         struct mtx ifc_mtx;
159
160         uint16_t ifc_nhwtxqs;
161         uint16_t ifc_nhwrxqs;
162
163         iflib_txq_t ifc_txqs;
164         iflib_rxq_t ifc_rxqs;
165         uint32_t ifc_if_flags;
166         uint32_t ifc_flags;
167         uint32_t ifc_max_fl_buf_size;
168         int ifc_in_detach;
169
170         int ifc_link_state;
171         int ifc_link_irq;
172         int ifc_pause_frames;
173         int ifc_watchdog_events;
174         struct cdev *ifc_led_dev;
175         struct resource *ifc_msix_mem;
176
177         struct if_irq ifc_legacy_irq;
178         struct grouptask ifc_admin_task;
179         struct grouptask ifc_vflr_task;
180         struct iflib_filter_info ifc_filter_info;
181         struct ifmedia  ifc_media;
182
183         struct sysctl_oid *ifc_sysctl_node;
184         uint16_t ifc_sysctl_ntxqs;
185         uint16_t ifc_sysctl_nrxqs;
186         uint16_t ifc_sysctl_qs_eq_override;
187
188         uint16_t ifc_sysctl_ntxds[8];
189         uint16_t ifc_sysctl_nrxds[8];
190         struct if_txrx ifc_txrx;
191 #define isc_txd_encap  ifc_txrx.ift_txd_encap
192 #define isc_txd_flush  ifc_txrx.ift_txd_flush
193 #define isc_txd_credits_update  ifc_txrx.ift_txd_credits_update
194 #define isc_rxd_available ifc_txrx.ift_rxd_available
195 #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
196 #define isc_rxd_refill ifc_txrx.ift_rxd_refill
197 #define isc_rxd_flush ifc_txrx.ift_rxd_flush
198 #define isc_rxd_refill ifc_txrx.ift_rxd_refill
199 #define isc_rxd_refill ifc_txrx.ift_rxd_refill
200 #define isc_legacy_intr ifc_txrx.ift_legacy_intr
201         eventhandler_tag ifc_vlan_attach_event;
202         eventhandler_tag ifc_vlan_detach_event;
203         uint8_t ifc_mac[ETHER_ADDR_LEN];
204         char ifc_mtx_name[16];
205 };
206
207
208 void *
209 iflib_get_softc(if_ctx_t ctx)
210 {
211
212         return (ctx->ifc_softc);
213 }
214
215 device_t
216 iflib_get_dev(if_ctx_t ctx)
217 {
218
219         return (ctx->ifc_dev);
220 }
221
222 if_t
223 iflib_get_ifp(if_ctx_t ctx)
224 {
225
226         return (ctx->ifc_ifp);
227 }
228
229 struct ifmedia *
230 iflib_get_media(if_ctx_t ctx)
231 {
232
233         return (&ctx->ifc_media);
234 }
235
236 void
237 iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
238 {
239
240         bcopy(mac, ctx->ifc_mac, ETHER_ADDR_LEN);
241 }
242
243 if_softc_ctx_t
244 iflib_get_softc_ctx(if_ctx_t ctx)
245 {
246
247         return (&ctx->ifc_softc_ctx);
248 }
249
250 if_shared_ctx_t
251 iflib_get_sctx(if_ctx_t ctx)
252 {
253
254         return (ctx->ifc_sctx);
255 }
256
257 #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
258
259 #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
260 #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
261
262 #define RX_SW_DESC_MAP_CREATED  (1 << 0)
263 #define TX_SW_DESC_MAP_CREATED  (1 << 1)
264 #define RX_SW_DESC_INUSE        (1 << 3)
265 #define TX_SW_DESC_MAPPED       (1 << 4)
266
267 typedef struct iflib_sw_rx_desc_array {
268         bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
269         struct mbuf     **ifsd_m;           /* pkthdr mbufs */
270         caddr_t         *ifsd_cl;          /* direct cluster pointer for rx */
271         uint8_t         *ifsd_flags;
272 } iflib_rxsd_array_t;
273
274 typedef struct iflib_sw_tx_desc_array {
275         bus_dmamap_t    *ifsd_map;         /* bus_dma maps for packet */
276         struct mbuf    **ifsd_m;           /* pkthdr mbufs */
277         uint8_t         *ifsd_flags;
278 } iflib_txsd_array_t;
279
280
281 /* magic number that should be high enough for any hardware */
282 #define IFLIB_MAX_TX_SEGS               128
283 #define IFLIB_MAX_RX_SEGS               32
284 #define IFLIB_RX_COPY_THRESH            63
285 #define IFLIB_MAX_RX_REFRESH            32
286 #define IFLIB_QUEUE_IDLE                0
287 #define IFLIB_QUEUE_HUNG                1
288 #define IFLIB_QUEUE_WORKING             2
289
290 /* this should really scale with ring size - 32 is a fairly arbitrary value for this */
291 #define TX_BATCH_SIZE                   16
292
293 #define IFLIB_RESTART_BUDGET            8
294
295 #define IFC_LEGACY              0x01
296 #define IFC_QFLUSH              0x02
297 #define IFC_MULTISEG            0x04
298 #define IFC_DMAR                0x08
299 #define IFC_SC_ALLOCATED        0x10
300 #define IFC_INIT_DONE           0x20
301
302
303 #define CSUM_OFFLOAD            (CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
304                                  CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
305                                  CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
306 struct iflib_txq {
307         uint16_t        ift_in_use;
308         uint16_t        ift_cidx;
309         uint16_t        ift_cidx_processed;
310         uint16_t        ift_pidx;
311         uint8_t         ift_gen;
312         uint8_t         ift_db_pending;
313         uint8_t         ift_db_pending_queued;
314         uint8_t         ift_npending;
315         uint8_t         ift_br_offset;
316         /* implicit pad */
317         uint64_t        ift_processed;
318         uint64_t        ift_cleaned;
319 #if MEMORY_LOGGING
320         uint64_t        ift_enqueued;
321         uint64_t        ift_dequeued;
322 #endif
323         uint64_t        ift_no_tx_dma_setup;
324         uint64_t        ift_no_desc_avail;
325         uint64_t        ift_mbuf_defrag_failed;
326         uint64_t        ift_mbuf_defrag;
327         uint64_t        ift_map_failed;
328         uint64_t        ift_txd_encap_efbig;
329         uint64_t        ift_pullups;
330
331         struct mtx      ift_mtx;
332         struct mtx      ift_db_mtx;
333
334         /* constant values */
335         if_ctx_t        ift_ctx;
336         struct ifmp_ring        **ift_br;
337         struct grouptask        ift_task;
338         uint16_t        ift_size;
339         uint16_t        ift_id;
340         struct callout  ift_timer;
341         struct callout  ift_db_check;
342
343         iflib_txsd_array_t      ift_sds;
344         uint8_t                 ift_nbr;
345         uint8_t                 ift_qstatus;
346         uint8_t                 ift_active;
347         uint8_t                 ift_closed;
348         int                     ift_watchdog_time;
349         struct iflib_filter_info ift_filter_info;
350         bus_dma_tag_t           ift_desc_tag;
351         bus_dma_tag_t           ift_tso_desc_tag;
352         iflib_dma_info_t        ift_ifdi;
353 #define MTX_NAME_LEN 16
354         char                    ift_mtx_name[MTX_NAME_LEN];
355         char                    ift_db_mtx_name[MTX_NAME_LEN];
356         bus_dma_segment_t       ift_segs[IFLIB_MAX_TX_SEGS]  __aligned(CACHE_LINE_SIZE);
357 #ifdef IFLIB_DIAGNOSTICS
358         uint64_t ift_cpu_exec_count[256];
359 #endif
360 } __aligned(CACHE_LINE_SIZE);
361
362 struct iflib_fl {
363         uint16_t        ifl_cidx;
364         uint16_t        ifl_pidx;
365         uint16_t        ifl_credits;
366         uint8_t         ifl_gen;
367 #if MEMORY_LOGGING
368         uint64_t        ifl_m_enqueued;
369         uint64_t        ifl_m_dequeued;
370         uint64_t        ifl_cl_enqueued;
371         uint64_t        ifl_cl_dequeued;
372 #endif
373         /* implicit pad */
374
375         /* constant */
376         uint16_t        ifl_size;
377         uint16_t        ifl_buf_size;
378         uint16_t        ifl_cltype;
379         uma_zone_t      ifl_zone;
380         iflib_rxsd_array_t      ifl_sds;
381         iflib_rxq_t     ifl_rxq;
382         uint8_t         ifl_id;
383         bus_dma_tag_t           ifl_desc_tag;
384         iflib_dma_info_t        ifl_ifdi;
385         uint64_t        ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
386         caddr_t         ifl_vm_addrs[IFLIB_MAX_RX_REFRESH];
387 }  __aligned(CACHE_LINE_SIZE);
388
389 static inline int
390 get_inuse(int size, int cidx, int pidx, int gen)
391 {
392         int used;
393
394         if (pidx > cidx)
395                 used = pidx - cidx;
396         else if (pidx < cidx)
397                 used = size - cidx + pidx;
398         else if (gen == 0 && pidx == cidx)
399                 used = 0;
400         else if (gen == 1 && pidx == cidx)
401                 used = size;
402         else
403                 panic("bad state");
404
405         return (used);
406 }
407
408 #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
409
410 #define IDXDIFF(head, tail, wrap) \
411         ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
412
413 struct iflib_rxq {
414         /* If there is a separate completion queue -
415          * these are the cq cidx and pidx. Otherwise
416          * these are unused.
417          */
418         uint16_t        ifr_size;
419         uint16_t        ifr_cq_cidx;
420         uint16_t        ifr_cq_pidx;
421         uint8_t         ifr_cq_gen;
422         uint8_t         ifr_fl_offset;
423
424         if_ctx_t        ifr_ctx;
425         iflib_fl_t      ifr_fl;
426         uint64_t        ifr_rx_irq;
427         uint16_t        ifr_id;
428         uint8_t         ifr_lro_enabled;
429         uint8_t         ifr_nfl;
430         struct lro_ctrl                 ifr_lc;
431         struct grouptask        ifr_task;
432         struct iflib_filter_info ifr_filter_info;
433         iflib_dma_info_t                ifr_ifdi;
434         /* dynamically allocate if any drivers need a value substantially larger than this */
435         struct if_rxd_frag      ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
436 #ifdef IFLIB_DIAGNOSTICS
437         uint64_t ifr_cpu_exec_count[256];
438 #endif
439 }  __aligned(CACHE_LINE_SIZE);
440
441 /*
442  * Only allow a single packet to take up most 1/nth of the tx ring
443  */
444 #define MAX_SINGLE_PACKET_FRACTION 12
445 #define IF_BAD_DMA (bus_addr_t)-1
446
447 static int enable_msix = 1;
448
449 #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
450
451 #define CTX_LOCK_INIT(_sc, _name)  mtx_init(&(_sc)->ifc_mtx, _name, "iflib ctx lock", MTX_DEF)
452
453 #define CTX_LOCK(ctx) mtx_lock(&(ctx)->ifc_mtx)
454 #define CTX_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_mtx)
455 #define CTX_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_mtx)
456
457
458 #define TXDB_LOCK_INIT(txq)  mtx_init(&(txq)->ift_db_mtx, (txq)->ift_db_mtx_name, NULL, MTX_DEF)
459 #define TXDB_TRYLOCK(txq) mtx_trylock(&(txq)->ift_db_mtx)
460 #define TXDB_LOCK(txq) mtx_lock(&(txq)->ift_db_mtx)
461 #define TXDB_UNLOCK(txq) mtx_unlock(&(txq)->ift_db_mtx)
462 #define TXDB_LOCK_DESTROY(txq) mtx_destroy(&(txq)->ift_db_mtx)
463
464 #define CALLOUT_LOCK(txq)       mtx_lock(&txq->ift_mtx)
465 #define CALLOUT_UNLOCK(txq)     mtx_unlock(&txq->ift_mtx)
466
467
468 /* Our boot-time initialization hook */
469 static int      iflib_module_event_handler(module_t, int, void *);
470
471 static moduledata_t iflib_moduledata = {
472         "iflib",
473         iflib_module_event_handler,
474         NULL
475 };
476
477 DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
478 MODULE_VERSION(iflib, 1);
479
480 MODULE_DEPEND(iflib, pci, 1, 1, 1);
481 MODULE_DEPEND(iflib, ether, 1, 1, 1);
482
483 TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
484 TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
485
486 #ifndef IFLIB_DEBUG_COUNTERS
487 #ifdef INVARIANTS
488 #define IFLIB_DEBUG_COUNTERS 1
489 #else
490 #define IFLIB_DEBUG_COUNTERS 0
491 #endif /* !INVARIANTS */
492 #endif
493
494 static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 0,
495                    "iflib driver parameters");
496
497 /*
498  * XXX need to ensure that this can't accidentally cause the head to be moved backwards 
499  */
500 static int iflib_min_tx_latency = 0;
501
502 SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
503                    &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput");
504
505
506 #if IFLIB_DEBUG_COUNTERS
507
508 static int iflib_tx_seen;
509 static int iflib_tx_sent;
510 static int iflib_tx_encap;
511 static int iflib_rx_allocs;
512 static int iflib_fl_refills;
513 static int iflib_fl_refills_large;
514 static int iflib_tx_frees;
515
516 SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD,
517                    &iflib_tx_seen, 0, "# tx mbufs seen");
518 SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD,
519                    &iflib_tx_sent, 0, "# tx mbufs sent");
520 SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD,
521                    &iflib_tx_encap, 0, "# tx mbufs encapped");
522 SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD,
523                    &iflib_tx_frees, 0, "# tx frees");
524 SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD,
525                    &iflib_rx_allocs, 0, "# rx allocations");
526 SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD,
527                    &iflib_fl_refills, 0, "# refills");
528 SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
529                    &iflib_fl_refills_large, 0, "# large refills");
530
531
532 static int iflib_txq_drain_flushing;
533 static int iflib_txq_drain_oactive;
534 static int iflib_txq_drain_notready;
535 static int iflib_txq_drain_encapfail;
536
537 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
538                    &iflib_txq_drain_flushing, 0, "# drain flushes");
539 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
540                    &iflib_txq_drain_oactive, 0, "# drain oactives");
541 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
542                    &iflib_txq_drain_notready, 0, "# drain notready");
543 SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_encapfail, CTLFLAG_RD,
544                    &iflib_txq_drain_encapfail, 0, "# drain encap fails");
545
546
547 static int iflib_encap_load_mbuf_fail;
548 static int iflib_encap_txq_avail_fail;
549 static int iflib_encap_txd_encap_fail;
550
551 SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
552                    &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
553 SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
554                    &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
555 SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
556                    &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
557
558 static int iflib_task_fn_rxs;
559 static int iflib_rx_intr_enables;
560 static int iflib_fast_intrs;
561 static int iflib_intr_link;
562 static int iflib_intr_msix; 
563 static int iflib_rx_unavail;
564 static int iflib_rx_ctx_inactive;
565 static int iflib_rx_zero_len;
566 static int iflib_rx_if_input;
567 static int iflib_rx_mbuf_null;
568 static int iflib_rxd_flush;
569
570 static int iflib_verbose_debug;
571
572 SYSCTL_INT(_net_iflib, OID_AUTO, intr_link, CTLFLAG_RD,
573                    &iflib_intr_link, 0, "# intr link calls");
574 SYSCTL_INT(_net_iflib, OID_AUTO, intr_msix, CTLFLAG_RD,
575                    &iflib_intr_msix, 0, "# intr msix calls");
576 SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD,
577                    &iflib_task_fn_rxs, 0, "# task_fn_rx calls");
578 SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
579                    &iflib_rx_intr_enables, 0, "# rx intr enables");
580 SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD,
581                    &iflib_fast_intrs, 0, "# fast_intr calls");
582 SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD,
583                    &iflib_rx_unavail, 0, "# times rxeof called with no available data");
584 SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
585                    &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
586 SYSCTL_INT(_net_iflib, OID_AUTO, rx_zero_len, CTLFLAG_RD,
587                    &iflib_rx_zero_len, 0, "# times rxeof saw zero len mbuf");
588 SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD,
589                    &iflib_rx_if_input, 0, "# times rxeof called if_input");
590 SYSCTL_INT(_net_iflib, OID_AUTO, rx_mbuf_null, CTLFLAG_RD,
591                    &iflib_rx_mbuf_null, 0, "# times rxeof got null mbuf");
592 SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD,
593                  &iflib_rxd_flush, 0, "# times rxd_flush called");
594 SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
595                    &iflib_verbose_debug, 0, "enable verbose debugging");
596
597 #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
598 static void
599 iflib_debug_reset(void)
600 {
601         iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
602                 iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
603                 iflib_txq_drain_flushing = iflib_txq_drain_oactive =
604                 iflib_txq_drain_notready = iflib_txq_drain_encapfail =
605                 iflib_encap_load_mbuf_fail = iflib_encap_txq_avail_fail =
606                 iflib_encap_txd_encap_fail = iflib_task_fn_rxs = iflib_rx_intr_enables =
607                 iflib_fast_intrs = iflib_intr_link = iflib_intr_msix = iflib_rx_unavail =
608                 iflib_rx_ctx_inactive = iflib_rx_zero_len = iflib_rx_if_input =
609                 iflib_rx_mbuf_null = iflib_rxd_flush = 0;
610 }
611
612 #else
613 #define DBG_COUNTER_INC(name)
614 static void iflib_debug_reset(void) {}
615 #endif
616
617
618
619 #define IFLIB_DEBUG 0
620
621 static void iflib_tx_structures_free(if_ctx_t ctx);
622 static void iflib_rx_structures_free(if_ctx_t ctx);
623 static int iflib_queues_alloc(if_ctx_t ctx);
624 static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
625 static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, int cidx, int budget);
626 static int iflib_qset_structures_setup(if_ctx_t ctx);
627 static int iflib_msix_init(if_ctx_t ctx);
628 static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, char *str);
629 static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
630 static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
631 static int iflib_register(if_ctx_t);
632 static void iflib_init_locked(if_ctx_t ctx);
633 static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
634 static void iflib_add_device_sysctl_post(if_ctx_t ctx);
635 static void iflib_ifmp_purge(iflib_txq_t txq);
636 static void _iflib_pre_assert(if_softc_ctx_t scctx);
637
638 #ifdef DEV_NETMAP
639 #include <sys/selinfo.h>
640 #include <net/netmap.h>
641 #include <dev/netmap/netmap_kern.h>
642
643 MODULE_DEPEND(iflib, netmap, 1, 1, 1);
644
645 /*
646  * device-specific sysctl variables:
647  *
648  * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
649  *      During regular operations the CRC is stripped, but on some
650  *      hardware reception of frames not multiple of 64 is slower,
651  *      so using crcstrip=0 helps in benchmarks.
652  *
653  * iflib_rx_miss, iflib_rx_miss_bufs:
654  *      count packets that might be missed due to lost interrupts.
655  */
656 SYSCTL_DECL(_dev_netmap);
657 /*
658  * The xl driver by default strips CRCs and we do not override it.
659  */
660
661 int iflib_crcstrip = 1;
662 SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
663     CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on rx frames");
664
665 int iflib_rx_miss, iflib_rx_miss_bufs;
666 SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
667     CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed rx intr");
668 SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
669     CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed rx intr bufs");
670
671 /*
672  * Register/unregister. We are already under netmap lock.
673  * Only called on the first register or the last unregister.
674  */
675 static int
676 iflib_netmap_register(struct netmap_adapter *na, int onoff)
677 {
678         struct ifnet *ifp = na->ifp;
679         if_ctx_t ctx = ifp->if_softc;
680
681         CTX_LOCK(ctx);
682         IFDI_INTR_DISABLE(ctx);
683
684         /* Tell the stack that the interface is no longer active */
685         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
686
687         if (!CTX_IS_VF(ctx))
688                 IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
689
690         /* enable or disable flags and callbacks in na and ifp */
691         if (onoff) {
692                 nm_set_native_flags(na);
693         } else {
694                 nm_clear_native_flags(na);
695         }
696         IFDI_INIT(ctx);
697         IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
698         CTX_UNLOCK(ctx);
699         return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
700 }
701
702 /*
703  * Reconcile kernel and user view of the transmit ring.
704  *
705  * All information is in the kring.
706  * Userspace wants to send packets up to the one before kring->rhead,
707  * kernel knows kring->nr_hwcur is the first unsent packet.
708  *
709  * Here we push packets out (as many as possible), and possibly
710  * reclaim buffers from previously completed transmission.
711  *
712  * The caller (netmap) guarantees that there is only one instance
713  * running at any time. Any interference with other driver
714  * methods should be handled by the individual drivers.
715  */
716 static int
717 iflib_netmap_txsync(struct netmap_kring *kring, int flags)
718 {
719         struct netmap_adapter *na = kring->na;
720         struct ifnet *ifp = na->ifp;
721         struct netmap_ring *ring = kring->ring;
722         u_int nm_i;     /* index into the netmap ring */
723         u_int nic_i;    /* index into the NIC ring */
724         u_int n;
725         u_int const lim = kring->nkr_num_slots - 1;
726         u_int const head = kring->rhead;
727         struct if_pkt_info pi;
728
729         /*
730          * interrupts on every tx packet are expensive so request
731          * them every half ring, or where NS_REPORT is set
732          */
733         u_int report_frequency = kring->nkr_num_slots >> 1;
734         /* device-specific */
735         if_ctx_t ctx = ifp->if_softc;
736         iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
737
738         pi.ipi_segs = txq->ift_segs;
739         pi.ipi_qsidx = kring->ring_id;
740         pi.ipi_ndescs = 0;
741
742         bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
743                                         BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
744
745
746         /*
747          * First part: process new packets to send.
748          * nm_i is the current index in the netmap ring,
749          * nic_i is the corresponding index in the NIC ring.
750          *
751          * If we have packets to send (nm_i != head)
752          * iterate over the netmap ring, fetch length and update
753          * the corresponding slot in the NIC ring. Some drivers also
754          * need to update the buffer's physical address in the NIC slot
755          * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
756          *
757          * The netmap_reload_map() calls is especially expensive,
758          * even when (as in this case) the tag is 0, so do only
759          * when the buffer has actually changed.
760          *
761          * If possible do not set the report/intr bit on all slots,
762          * but only a few times per ring or when NS_REPORT is set.
763          *
764          * Finally, on 10G and faster drivers, it might be useful
765          * to prefetch the next slot and txr entry.
766          */
767
768         nm_i = kring->nr_hwcur;
769         if (nm_i != head) {     /* we have new packets to send */
770                 nic_i = netmap_idx_k2n(kring, nm_i);
771
772                 __builtin_prefetch(&ring->slot[nm_i]);
773                 __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
774                 __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
775
776                 for (n = 0; nm_i != head; n++) {
777                         struct netmap_slot *slot = &ring->slot[nm_i];
778                         u_int len = slot->len;
779                         uint64_t paddr;
780                         void *addr = PNMB(na, slot, &paddr);
781                         int flags = (slot->flags & NS_REPORT ||
782                                 nic_i == 0 || nic_i == report_frequency) ?
783                                 IPI_TX_INTR : 0;
784
785                         /* device-specific */
786                         pi.ipi_pidx = nic_i;
787                         pi.ipi_flags = flags;
788
789                         /* Fill the slot in the NIC ring. */
790                         ctx->isc_txd_encap(ctx->ifc_softc, &pi);
791
792                         /* prefetch for next round */
793                         __builtin_prefetch(&ring->slot[nm_i + 1]);
794                         __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
795                         __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
796
797                         NM_CHECK_ADDR_LEN(na, addr, len);
798
799                         if (slot->flags & NS_BUF_CHANGED) {
800                                 /* buffer has changed, reload map */
801                                 netmap_reload_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[nic_i], addr);
802                         }
803                         slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
804
805                         /* make sure changes to the buffer are synced */
806                         bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_sds.ifsd_map[nic_i],
807                                                         BUS_DMASYNC_PREWRITE);
808
809                         nm_i = nm_next(nm_i, lim);
810                         nic_i = nm_next(nic_i, lim);
811                 }
812                 kring->nr_hwcur = head;
813
814                 /* synchronize the NIC ring */
815                 bus_dmamap_sync(txq->ift_desc_tag, txq->ift_ifdi->idi_map,
816                                                 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
817
818                 /* (re)start the tx unit up to slot nic_i (excluded) */
819                 ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
820         }
821
822         /*
823          * Second part: reclaim buffers for completed transmissions.
824          */
825         if (iflib_tx_credits_update(ctx, txq)) {
826                 /* some tx completed, increment avail */
827                 nic_i = txq->ift_cidx_processed;
828                 kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
829         }
830         return (0);
831 }
832
833 /*
834  * Reconcile kernel and user view of the receive ring.
835  * Same as for the txsync, this routine must be efficient.
836  * The caller guarantees a single invocations, but races against
837  * the rest of the driver should be handled here.
838  *
839  * On call, kring->rhead is the first packet that userspace wants
840  * to keep, and kring->rcur is the wakeup point.
841  * The kernel has previously reported packets up to kring->rtail.
842  *
843  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
844  * of whether or not we received an interrupt.
845  */
846 static int
847 iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
848 {
849         struct netmap_adapter *na = kring->na;
850         struct ifnet *ifp = na->ifp;
851         struct netmap_ring *ring = kring->ring;
852         u_int nm_i;     /* index into the netmap ring */
853         u_int nic_i;    /* index into the NIC ring */
854         u_int i, n;
855         u_int const lim = kring->nkr_num_slots - 1;
856         u_int const head = kring->rhead;
857         int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
858         struct if_rxd_info ri;
859         /* device-specific */
860         if_ctx_t ctx = ifp->if_softc;
861         iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
862         iflib_fl_t fl = rxq->ifr_fl;
863         if (head > lim)
864                 return netmap_ring_reinit(kring);
865
866         bzero(&ri, sizeof(ri));
867         ri.iri_qsidx = kring->ring_id;
868         ri.iri_ifp = ctx->ifc_ifp;
869         /* XXX check sync modes */
870         for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++)
871                 bus_dmamap_sync(rxq->ifr_fl[i].ifl_desc_tag, fl->ifl_ifdi->idi_map,
872                                 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
873
874         /*
875          * First part: import newly received packets.
876          *
877          * nm_i is the index of the next free slot in the netmap ring,
878          * nic_i is the index of the next received packet in the NIC ring,
879          * and they may differ in case if_init() has been called while
880          * in netmap mode. For the receive ring we have
881          *
882          *      nic_i = rxr->next_check;
883          *      nm_i = kring->nr_hwtail (previous)
884          * and
885          *      nm_i == (nic_i + kring->nkr_hwofs) % ring_size
886          *
887          * rxr->next_check is set to 0 on a ring reinit
888          */
889         if (netmap_no_pendintr || force_update) {
890                 int crclen = iflib_crcstrip ? 0 : 4;
891                 int error, avail;
892                 uint16_t slot_flags = kring->nkr_slot_flags;
893
894                 for (fl = rxq->ifr_fl, i = 0; i < rxq->ifr_nfl; i++, fl++) {
895                         nic_i = fl->ifl_cidx;
896                         nm_i = netmap_idx_n2k(kring, nic_i);
897                         avail = ctx->isc_rxd_available(ctx->ifc_softc, kring->ring_id, nic_i, INT_MAX);
898                         for (n = 0; avail > 0; n++, avail--) {
899                                 error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
900                                 if (error)
901                                         ring->slot[nm_i].len = 0;
902                                 else
903                                         ring->slot[nm_i].len = ri.iri_len - crclen;
904                                 ring->slot[nm_i].flags = slot_flags;
905                                 bus_dmamap_sync(fl->ifl_ifdi->idi_tag,
906                                                                 fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
907                                 nm_i = nm_next(nm_i, lim);
908                                 nic_i = nm_next(nic_i, lim);
909                         }
910                         if (n) { /* update the state variables */
911                                 if (netmap_no_pendintr && !force_update) {
912                                         /* diagnostics */
913                                         iflib_rx_miss ++;
914                                         iflib_rx_miss_bufs += n;
915                                 }
916                                 fl->ifl_cidx = nic_i;
917                                 kring->nr_hwtail = nm_i;
918                         }
919                         kring->nr_kflags &= ~NKR_PENDINTR;
920                 }
921         }
922         /*
923          * Second part: skip past packets that userspace has released.
924          * (kring->nr_hwcur to head excluded),
925          * and make the buffers available for reception.
926          * As usual nm_i is the index in the netmap ring,
927          * nic_i is the index in the NIC ring, and
928          * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
929          */
930         /* XXX not sure how this will work with multiple free lists */
931         nm_i = kring->nr_hwcur;
932         if (nm_i != head) {
933                 nic_i = netmap_idx_k2n(kring, nm_i);
934                 for (n = 0; nm_i != head; n++) {
935                         struct netmap_slot *slot = &ring->slot[nm_i];
936                         uint64_t paddr;
937                         caddr_t vaddr;
938                         void *addr = PNMB(na, slot, &paddr);
939
940                         if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
941                                 goto ring_reset;
942
943                         vaddr = addr;
944                         if (slot->flags & NS_BUF_CHANGED) {
945                                 /* buffer has changed, reload map */
946                                 netmap_reload_map(na, fl->ifl_ifdi->idi_tag, fl->ifl_sds.ifsd_map[nic_i], addr);
947                                 slot->flags &= ~NS_BUF_CHANGED;
948                         }
949                         /*
950                          * XXX we should be batching this operation - TODO
951                          */
952                         ctx->isc_rxd_refill(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i, &paddr, &vaddr, 1, fl->ifl_buf_size);
953                         bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_sds.ifsd_map[nic_i],
954                             BUS_DMASYNC_PREREAD);
955                         nm_i = nm_next(nm_i, lim);
956                         nic_i = nm_next(nic_i, lim);
957                 }
958                 kring->nr_hwcur = head;
959
960                 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
961                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
962                 /*
963                  * IMPORTANT: we must leave one free slot in the ring,
964                  * so move nic_i back by one unit
965                  */
966                 nic_i = nm_prev(nic_i, lim);
967                 ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i);
968         }
969
970         return 0;
971
972 ring_reset:
973         return netmap_ring_reinit(kring);
974 }
975
976 static int
977 iflib_netmap_attach(if_ctx_t ctx)
978 {
979         struct netmap_adapter na;
980         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
981
982         bzero(&na, sizeof(na));
983
984         na.ifp = ctx->ifc_ifp;
985         na.na_flags = NAF_BDG_MAYSLEEP;
986         MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
987         MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
988
989         na.num_tx_desc = scctx->isc_ntxd[0];
990         na.num_rx_desc = scctx->isc_nrxd[0];
991         na.nm_txsync = iflib_netmap_txsync;
992         na.nm_rxsync = iflib_netmap_rxsync;
993         na.nm_register = iflib_netmap_register;
994         na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
995         na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
996         return (netmap_attach(&na));
997 }
998
999 static void
1000 iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
1001 {
1002         struct netmap_adapter *na = NA(ctx->ifc_ifp);
1003         struct netmap_slot *slot;
1004
1005         slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
1006         if (slot == 0)
1007                 return;
1008
1009         for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
1010
1011                 /*
1012                  * In netmap mode, set the map for the packet buffer.
1013                  * NOTE: Some drivers (not this one) also need to set
1014                  * the physical buffer address in the NIC ring.
1015                  * netmap_idx_n2k() maps a nic index, i, into the corresponding
1016                  * netmap slot index, si
1017                  */
1018                 int si = netmap_idx_n2k(&na->tx_rings[txq->ift_id], i);
1019                 netmap_load_map(na, txq->ift_desc_tag, txq->ift_sds.ifsd_map[i], NMB(na, slot + si));
1020         }
1021 }
1022 static void
1023 iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
1024 {
1025         struct netmap_adapter *na = NA(ctx->ifc_ifp);
1026         struct netmap_slot *slot;
1027         bus_dmamap_t *map;
1028         int nrxd;
1029
1030         slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
1031         if (slot == 0)
1032                 return;
1033         map = rxq->ifr_fl[0].ifl_sds.ifsd_map;
1034         nrxd = ctx->ifc_softc_ctx.isc_nrxd[0];
1035         for (int i = 0; i < nrxd; i++, map++) {
1036                         int sj = netmap_idx_n2k(&na->rx_rings[rxq->ifr_id], i);
1037                         uint64_t paddr;
1038                         void *addr;
1039                         caddr_t vaddr;
1040
1041                         vaddr = addr = PNMB(na, slot + sj, &paddr);
1042                         netmap_load_map(na, rxq->ifr_fl[0].ifl_ifdi->idi_tag, *map, addr);
1043                         /* Update descriptor and the cached value */
1044                         ctx->isc_rxd_refill(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, i, &paddr, &vaddr, 1, rxq->ifr_fl[0].ifl_buf_size);
1045         }
1046         /* preserve queue */
1047         if (ctx->ifc_ifp->if_capenable & IFCAP_NETMAP) {
1048                 struct netmap_kring *kring = &na->rx_rings[rxq->ifr_id];
1049                 int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
1050                 ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, t);
1051         } else
1052                 ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, 0 /* fl_id */, nrxd-1);
1053 }
1054
1055 #define iflib_netmap_detach(ifp) netmap_detach(ifp)
1056
1057 #else
1058 #define iflib_netmap_txq_init(ctx, txq)
1059 #define iflib_netmap_rxq_init(ctx, rxq)
1060 #define iflib_netmap_detach(ifp)
1061
1062 #define iflib_netmap_attach(ctx) (0)
1063 #define netmap_rx_irq(ifp, qid, budget) (0)
1064
1065 #endif
1066
1067 #if defined(__i386__) || defined(__amd64__)
1068 static __inline void
1069 prefetch(void *x)
1070 {
1071         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1072 }
1073 #else
1074 #define prefetch(x)
1075 #endif
1076
1077 static void
1078 _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
1079 {
1080         if (err)
1081                 return;
1082         *(bus_addr_t *) arg = segs[0].ds_addr;
1083 }
1084
1085 int
1086 iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
1087 {
1088         int err;
1089         if_shared_ctx_t sctx = ctx->ifc_sctx;
1090         device_t dev = ctx->ifc_dev;
1091
1092         KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
1093
1094         err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
1095                                 sctx->isc_q_align, 0,   /* alignment, bounds */
1096                                 BUS_SPACE_MAXADDR,      /* lowaddr */
1097                                 BUS_SPACE_MAXADDR,      /* highaddr */
1098                                 NULL, NULL,             /* filter, filterarg */
1099                                 size,                   /* maxsize */
1100                                 1,                      /* nsegments */
1101                                 size,                   /* maxsegsize */
1102                                 BUS_DMA_ALLOCNOW,       /* flags */
1103                                 NULL,                   /* lockfunc */
1104                                 NULL,                   /* lockarg */
1105                                 &dma->idi_tag);
1106         if (err) {
1107                 device_printf(dev,
1108                     "%s: bus_dma_tag_create failed: %d\n",
1109                     __func__, err);
1110                 goto fail_0;
1111         }
1112
1113         err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
1114             BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
1115         if (err) {
1116                 device_printf(dev,
1117                     "%s: bus_dmamem_alloc(%ju) failed: %d\n",
1118                     __func__, (uintmax_t)size, err);
1119                 goto fail_1;
1120         }
1121
1122         dma->idi_paddr = IF_BAD_DMA;
1123         err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
1124             size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
1125         if (err || dma->idi_paddr == IF_BAD_DMA) {
1126                 device_printf(dev,
1127                     "%s: bus_dmamap_load failed: %d\n",
1128                     __func__, err);
1129                 goto fail_2;
1130         }
1131
1132         dma->idi_size = size;
1133         return (0);
1134
1135 fail_2:
1136         bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
1137 fail_1:
1138         bus_dma_tag_destroy(dma->idi_tag);
1139 fail_0:
1140         dma->idi_tag = NULL;
1141
1142         return (err);
1143 }
1144
1145 int
1146 iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
1147 {
1148         int i, err;
1149         iflib_dma_info_t *dmaiter;
1150
1151         dmaiter = dmalist;
1152         for (i = 0; i < count; i++, dmaiter++) {
1153                 if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
1154                         break;
1155         }
1156         if (err)
1157                 iflib_dma_free_multi(dmalist, i);
1158         return (err);
1159 }
1160
1161 void
1162 iflib_dma_free(iflib_dma_info_t dma)
1163 {
1164         if (dma->idi_tag == NULL)
1165                 return;
1166         if (dma->idi_paddr != IF_BAD_DMA) {
1167                 bus_dmamap_sync(dma->idi_tag, dma->idi_map,
1168                     BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1169                 bus_dmamap_unload(dma->idi_tag, dma->idi_map);
1170                 dma->idi_paddr = IF_BAD_DMA;
1171         }
1172         if (dma->idi_vaddr != NULL) {
1173                 bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
1174                 dma->idi_vaddr = NULL;
1175         }
1176         bus_dma_tag_destroy(dma->idi_tag);
1177         dma->idi_tag = NULL;
1178 }
1179
1180 void
1181 iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
1182 {
1183         int i;
1184         iflib_dma_info_t *dmaiter = dmalist;
1185
1186         for (i = 0; i < count; i++, dmaiter++)
1187                 iflib_dma_free(*dmaiter);
1188 }
1189
1190 #ifdef EARLY_AP_STARTUP
1191 static const int iflib_started = 1;
1192 #else
1193 /*
1194  * We used to abuse the smp_started flag to decide if the queues have been
1195  * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()).
1196  * That gave bad races, since the SYSINIT() runs strictly after smp_started
1197  * is set.  Run a SYSINIT() strictly after that to just set a usable
1198  * completion flag.
1199  */
1200
1201 static int iflib_started;
1202
1203 static void
1204 iflib_record_started(void *arg)
1205 {
1206         iflib_started = 1;
1207 }
1208
1209 SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST,
1210         iflib_record_started, NULL);
1211 #endif
1212
1213 static int
1214 iflib_fast_intr(void *arg)
1215 {
1216         iflib_filter_info_t info = arg;
1217         struct grouptask *gtask = info->ifi_task;
1218
1219         if (!iflib_started)
1220                 return (FILTER_HANDLED);
1221
1222         DBG_COUNTER_INC(fast_intrs);
1223         if (info->ifi_filter != NULL && info->ifi_filter(info->ifi_filter_arg) == FILTER_HANDLED)
1224                 return (FILTER_HANDLED);
1225
1226         GROUPTASK_ENQUEUE(gtask);
1227         return (FILTER_HANDLED);
1228 }
1229
1230 static int
1231 _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
1232         driver_filter_t filter, driver_intr_t handler, void *arg,
1233                                  char *name)
1234 {
1235         int rc;
1236         struct resource *res;
1237         void *tag;
1238         device_t dev = ctx->ifc_dev;
1239
1240         MPASS(rid < 512);
1241         irq->ii_rid = rid;
1242         res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irq->ii_rid,
1243                                      RF_SHAREABLE | RF_ACTIVE);
1244         if (res == NULL) {
1245                 device_printf(dev,
1246                     "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
1247                 return (ENOMEM);
1248         }
1249         irq->ii_res = res;
1250         KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
1251         rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
1252                                                 filter, handler, arg, &tag);
1253         if (rc != 0) {
1254                 device_printf(dev,
1255                     "failed to setup interrupt for rid %d, name %s: %d\n",
1256                                           rid, name ? name : "unknown", rc);
1257                 return (rc);
1258         } else if (name)
1259                 bus_describe_intr(dev, res, tag, "%s", name);
1260
1261         irq->ii_tag = tag;
1262         return (0);
1263 }
1264
1265
1266 /*********************************************************************
1267  *
1268  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
1269  *  the information needed to transmit a packet on the wire. This is
1270  *  called only once at attach, setup is done every reset.
1271  *
1272  **********************************************************************/
1273
1274 static int
1275 iflib_txsd_alloc(iflib_txq_t txq)
1276 {
1277         if_ctx_t ctx = txq->ift_ctx;
1278         if_shared_ctx_t sctx = ctx->ifc_sctx;
1279         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1280         device_t dev = ctx->ifc_dev;
1281         int err, nsegments, ntsosegments;
1282
1283         nsegments = scctx->isc_tx_nsegments;
1284         ntsosegments = scctx->isc_tx_tso_segments_max;
1285         MPASS(scctx->isc_ntxd[0] > 0);
1286         MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
1287         MPASS(nsegments > 0);
1288         MPASS(ntsosegments > 0);
1289         /*
1290          * Setup DMA descriptor areas.
1291          */
1292         if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
1293                                1, 0,                    /* alignment, bounds */
1294                                BUS_SPACE_MAXADDR,       /* lowaddr */
1295                                BUS_SPACE_MAXADDR,       /* highaddr */
1296                                NULL, NULL,              /* filter, filterarg */
1297                                sctx->isc_tx_maxsize,            /* maxsize */
1298                                nsegments,       /* nsegments */
1299                                sctx->isc_tx_maxsegsize, /* maxsegsize */
1300                                0,                       /* flags */
1301                                NULL,                    /* lockfunc */
1302                                NULL,                    /* lockfuncarg */
1303                                &txq->ift_desc_tag))) {
1304                 device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
1305                 device_printf(dev,"maxsize: %zd nsegments: %d maxsegsize: %zd\n",
1306                                           sctx->isc_tx_maxsize, nsegments, sctx->isc_tx_maxsegsize);
1307                 goto fail;
1308         }
1309         if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
1310                                1, 0,                    /* alignment, bounds */
1311                                BUS_SPACE_MAXADDR,       /* lowaddr */
1312                                BUS_SPACE_MAXADDR,       /* highaddr */
1313                                NULL, NULL,              /* filter, filterarg */
1314                                scctx->isc_tx_tso_size_max,              /* maxsize */
1315                                ntsosegments,    /* nsegments */
1316                                scctx->isc_tx_tso_segsize_max,   /* maxsegsize */
1317                                0,                       /* flags */
1318                                NULL,                    /* lockfunc */
1319                                NULL,                    /* lockfuncarg */
1320                                &txq->ift_tso_desc_tag))) {
1321                 device_printf(dev,"Unable to allocate TX TSO DMA tag: %d\n", err);
1322
1323                 goto fail;
1324         }
1325         if (!(txq->ift_sds.ifsd_flags =
1326             (uint8_t *) malloc(sizeof(uint8_t) *
1327             scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1328                 device_printf(dev, "Unable to allocate tx_buffer memory\n");
1329                 err = ENOMEM;
1330                 goto fail;
1331         }
1332         if (!(txq->ift_sds.ifsd_m =
1333             (struct mbuf **) malloc(sizeof(struct mbuf *) *
1334             scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1335                 device_printf(dev, "Unable to allocate tx_buffer memory\n");
1336                 err = ENOMEM;
1337                 goto fail;
1338         }
1339
1340         /* Create the descriptor buffer dma maps */
1341 #if defined(ACPI_DMAR) || (!(defined(__i386__) && !defined(__amd64__)))
1342         if ((ctx->ifc_flags & IFC_DMAR) == 0)
1343                 return (0);
1344
1345         if (!(txq->ift_sds.ifsd_map =
1346             (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1347                 device_printf(dev, "Unable to allocate tx_buffer map memory\n");
1348                 err = ENOMEM;
1349                 goto fail;
1350         }
1351
1352         for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
1353                 err = bus_dmamap_create(txq->ift_desc_tag, 0, &txq->ift_sds.ifsd_map[i]);
1354                 if (err != 0) {
1355                         device_printf(dev, "Unable to create TX DMA map\n");
1356                         goto fail;
1357                 }
1358         }
1359 #endif
1360         return (0);
1361 fail:
1362         /* We free all, it handles case where we are in the middle */
1363         iflib_tx_structures_free(ctx);
1364         return (err);
1365 }
1366
1367 static void
1368 iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
1369 {
1370         bus_dmamap_t map;
1371
1372         map = NULL;
1373         if (txq->ift_sds.ifsd_map != NULL)
1374                 map = txq->ift_sds.ifsd_map[i];
1375         if (map != NULL) {
1376                 bus_dmamap_unload(txq->ift_desc_tag, map);
1377                 bus_dmamap_destroy(txq->ift_desc_tag, map);
1378                 txq->ift_sds.ifsd_map[i] = NULL;
1379         }
1380 }
1381
1382 static void
1383 iflib_txq_destroy(iflib_txq_t txq)
1384 {
1385         if_ctx_t ctx = txq->ift_ctx;
1386
1387         for (int i = 0; i < txq->ift_size; i++)
1388                 iflib_txsd_destroy(ctx, txq, i);
1389         if (txq->ift_sds.ifsd_map != NULL) {
1390                 free(txq->ift_sds.ifsd_map, M_IFLIB);
1391                 txq->ift_sds.ifsd_map = NULL;
1392         }
1393         if (txq->ift_sds.ifsd_m != NULL) {
1394                 free(txq->ift_sds.ifsd_m, M_IFLIB);
1395                 txq->ift_sds.ifsd_m = NULL;
1396         }
1397         if (txq->ift_sds.ifsd_flags != NULL) {
1398                 free(txq->ift_sds.ifsd_flags, M_IFLIB);
1399                 txq->ift_sds.ifsd_flags = NULL;
1400         }
1401         if (txq->ift_desc_tag != NULL) {
1402                 bus_dma_tag_destroy(txq->ift_desc_tag);
1403                 txq->ift_desc_tag = NULL;
1404         }
1405         if (txq->ift_tso_desc_tag != NULL) {
1406                 bus_dma_tag_destroy(txq->ift_tso_desc_tag);
1407                 txq->ift_tso_desc_tag = NULL;
1408         }
1409 }
1410
1411 static void
1412 iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
1413 {
1414         struct mbuf **mp;
1415
1416         mp = &txq->ift_sds.ifsd_m[i];
1417         if (*mp == NULL)
1418                 return;
1419
1420         if (txq->ift_sds.ifsd_map != NULL) {
1421                 bus_dmamap_sync(txq->ift_desc_tag,
1422                                 txq->ift_sds.ifsd_map[i],
1423                                 BUS_DMASYNC_POSTWRITE);
1424                 bus_dmamap_unload(txq->ift_desc_tag,
1425                                   txq->ift_sds.ifsd_map[i]);
1426         }
1427         m_free(*mp);
1428         DBG_COUNTER_INC(tx_frees);
1429         *mp = NULL;
1430 }
1431
1432 static int
1433 iflib_txq_setup(iflib_txq_t txq)
1434 {
1435         if_ctx_t ctx = txq->ift_ctx;
1436         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1437         iflib_dma_info_t di;
1438         int i;
1439
1440         /* Set number of descriptors available */
1441         txq->ift_qstatus = IFLIB_QUEUE_IDLE;
1442
1443         /* Reset indices */
1444         txq->ift_cidx_processed = txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
1445         txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
1446
1447         for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
1448                 bzero((void *)di->idi_vaddr, di->idi_size);
1449
1450         IFDI_TXQ_SETUP(ctx, txq->ift_id);
1451         for (i = 0, di = txq->ift_ifdi; i < ctx->ifc_nhwtxqs; i++, di++)
1452                 bus_dmamap_sync(di->idi_tag, di->idi_map,
1453                                                 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1454         return (0);
1455 }
1456
1457 /*********************************************************************
1458  *
1459  *  Allocate memory for rx_buffer structures. Since we use one
1460  *  rx_buffer per received packet, the maximum number of rx_buffer's
1461  *  that we'll need is equal to the number of receive descriptors
1462  *  that we've allocated.
1463  *
1464  **********************************************************************/
1465 static int
1466 iflib_rxsd_alloc(iflib_rxq_t rxq)
1467 {
1468         if_ctx_t ctx = rxq->ifr_ctx;
1469         if_shared_ctx_t sctx = ctx->ifc_sctx;
1470         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1471         device_t dev = ctx->ifc_dev;
1472         iflib_fl_t fl;
1473         int                     err;
1474
1475         MPASS(scctx->isc_nrxd[0] > 0);
1476         MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
1477
1478         fl = rxq->ifr_fl;
1479         for (int i = 0; i <  rxq->ifr_nfl; i++, fl++) {
1480                 fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
1481                 err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
1482                                          1, 0,                  /* alignment, bounds */
1483                                          BUS_SPACE_MAXADDR,     /* lowaddr */
1484                                          BUS_SPACE_MAXADDR,     /* highaddr */
1485                                          NULL, NULL,            /* filter, filterarg */
1486                                          sctx->isc_rx_maxsize,  /* maxsize */
1487                                          sctx->isc_rx_nsegments,        /* nsegments */
1488                                          sctx->isc_rx_maxsegsize,       /* maxsegsize */
1489                                          0,                     /* flags */
1490                                          NULL,                  /* lockfunc */
1491                                          NULL,                  /* lockarg */
1492                                          &fl->ifl_desc_tag);
1493                 if (err) {
1494                         device_printf(dev, "%s: bus_dma_tag_create failed %d\n",
1495                                 __func__, err);
1496                         goto fail;
1497                 }
1498                 if (!(fl->ifl_sds.ifsd_flags =
1499                       (uint8_t *) malloc(sizeof(uint8_t) *
1500                                          scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1501                         device_printf(dev, "Unable to allocate tx_buffer memory\n");
1502                         err = ENOMEM;
1503                         goto fail;
1504                 }
1505                 if (!(fl->ifl_sds.ifsd_m =
1506                       (struct mbuf **) malloc(sizeof(struct mbuf *) *
1507                                               scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1508                         device_printf(dev, "Unable to allocate tx_buffer memory\n");
1509                         err = ENOMEM;
1510                         goto fail;
1511                 }
1512                 if (!(fl->ifl_sds.ifsd_cl =
1513                       (caddr_t *) malloc(sizeof(caddr_t) *
1514                                               scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1515                         device_printf(dev, "Unable to allocate tx_buffer memory\n");
1516                         err = ENOMEM;
1517                         goto fail;
1518                 }
1519
1520                 /* Create the descriptor buffer dma maps */
1521 #if defined(ACPI_DMAR) || (!(defined(__i386__) && !defined(__amd64__)))
1522                 if ((ctx->ifc_flags & IFC_DMAR) == 0)
1523                         continue;
1524
1525                 if (!(fl->ifl_sds.ifsd_map =
1526                       (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
1527                         device_printf(dev, "Unable to allocate tx_buffer map memory\n");
1528                         err = ENOMEM;
1529                         goto fail;
1530                 }
1531
1532                 for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
1533                         err = bus_dmamap_create(fl->ifl_desc_tag, 0, &fl->ifl_sds.ifsd_map[i]);
1534                         if (err != 0) {
1535                                 device_printf(dev, "Unable to create TX DMA map\n");
1536                                 goto fail;
1537                         }
1538                 }
1539 #endif
1540         }
1541         return (0);
1542
1543 fail:
1544         iflib_rx_structures_free(ctx);
1545         return (err);
1546 }
1547
1548
1549 /*
1550  * Internal service routines
1551  */
1552
1553 struct rxq_refill_cb_arg {
1554         int               error;
1555         bus_dma_segment_t seg;
1556         int               nseg;
1557 };
1558
1559 static void
1560 _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1561 {
1562         struct rxq_refill_cb_arg *cb_arg = arg;
1563
1564         cb_arg->error = error;
1565         cb_arg->seg = segs[0];
1566         cb_arg->nseg = nseg;
1567 }
1568
1569
1570 #ifdef ACPI_DMAR
1571 #define IS_DMAR(ctx) (ctx->ifc_flags & IFC_DMAR)
1572 #else
1573 #define IS_DMAR(ctx) (0)
1574 #endif
1575
1576 /**
1577  *      rxq_refill - refill an rxq  free-buffer list
1578  *      @ctx: the iflib context
1579  *      @rxq: the free-list to refill
1580  *      @n: the number of new buffers to allocate
1581  *
1582  *      (Re)populate an rxq free-buffer list with up to @n new packet buffers.
1583  *      The caller must assure that @n does not exceed the queue's capacity.
1584  */
1585 static void
1586 _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
1587 {
1588         struct mbuf *m;
1589         int idx, pidx = fl->ifl_pidx;
1590         caddr_t cl, *sd_cl;
1591         struct mbuf **sd_m;
1592         uint8_t *sd_flags;
1593         bus_dmamap_t *sd_map;
1594         int n, i = 0;
1595         uint64_t bus_addr;
1596         int err;
1597
1598         sd_m = fl->ifl_sds.ifsd_m;
1599         sd_map = fl->ifl_sds.ifsd_map;
1600         sd_cl = fl->ifl_sds.ifsd_cl;
1601         sd_flags = fl->ifl_sds.ifsd_flags;
1602         idx = pidx;
1603
1604         n  = count;
1605         MPASS(n > 0);
1606         MPASS(fl->ifl_credits + n <= fl->ifl_size);
1607
1608         if (pidx < fl->ifl_cidx)
1609                 MPASS(pidx + n <= fl->ifl_cidx);
1610         if (pidx == fl->ifl_cidx && (fl->ifl_credits < fl->ifl_size))
1611                 MPASS(fl->ifl_gen == 0);
1612         if (pidx > fl->ifl_cidx)
1613                 MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
1614
1615         DBG_COUNTER_INC(fl_refills);
1616         if (n > 8)
1617                 DBG_COUNTER_INC(fl_refills_large);
1618
1619         while (n--) {
1620                 /*
1621                  * We allocate an uninitialized mbuf + cluster, mbuf is
1622                  * initialized after rx.
1623                  *
1624                  * If the cluster is still set then we know a minimum sized packet was received
1625                  */
1626                 if ((cl = sd_cl[idx]) == NULL) {
1627                         if ((cl = sd_cl[idx] = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL)
1628                                 break;
1629 #if MEMORY_LOGGING
1630                         fl->ifl_cl_enqueued++;
1631 #endif
1632                 }
1633                 if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) {
1634                         break;
1635                 }
1636 #if MEMORY_LOGGING
1637                 fl->ifl_m_enqueued++;
1638 #endif
1639
1640                 DBG_COUNTER_INC(rx_allocs);
1641 #ifdef notyet
1642                 if ((sd_flags[pidx] & RX_SW_DESC_MAP_CREATED) == 0) {
1643                         int err;
1644
1645                         if ((err = bus_dmamap_create(fl->ifl_ifdi->idi_tag, 0, &sd_map[idx]))) {
1646                                 log(LOG_WARNING, "bus_dmamap_create failed %d\n", err);
1647                                 uma_zfree(fl->ifl_zone, cl);
1648                                 n = 0;
1649                                 goto done;
1650                         }
1651                         sd_flags[idx] |= RX_SW_DESC_MAP_CREATED;
1652                 }
1653 #endif
1654 #if defined(__i386__) || defined(__amd64__)
1655                 if (!IS_DMAR(ctx)) {
1656                         bus_addr = pmap_kextract((vm_offset_t)cl);
1657                 } else
1658 #endif
1659                 {
1660                         struct rxq_refill_cb_arg cb_arg;
1661                         iflib_rxq_t q;
1662
1663                         cb_arg.error = 0;
1664                         q = fl->ifl_rxq;
1665                         err = bus_dmamap_load(fl->ifl_desc_tag, sd_map[idx],
1666                          cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 0);
1667
1668                         if (err != 0 || cb_arg.error) {
1669                                 /*
1670                                  * !zone_pack ?
1671                                  */
1672                                 if (fl->ifl_zone == zone_pack)
1673                                         uma_zfree(fl->ifl_zone, cl);
1674                                 m_free(m);
1675                                 n = 0;
1676                                 goto done;
1677                         }
1678                         bus_addr = cb_arg.seg.ds_addr;
1679                 }
1680                 sd_flags[idx] |= RX_SW_DESC_INUSE;
1681
1682                 MPASS(sd_m[idx] == NULL);
1683                 sd_cl[idx] = cl;
1684                 sd_m[idx] = m;
1685                 fl->ifl_bus_addrs[i] = bus_addr;
1686                 fl->ifl_vm_addrs[i] = cl;
1687                 fl->ifl_credits++;
1688                 i++;
1689                 MPASS(fl->ifl_credits <= fl->ifl_size);
1690                 if (++idx == fl->ifl_size) {
1691                         fl->ifl_gen = 1;
1692                         idx = 0;
1693                 }
1694                 if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
1695                         ctx->isc_rxd_refill(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx,
1696                                                                  fl->ifl_bus_addrs, fl->ifl_vm_addrs, i, fl->ifl_buf_size);
1697                         i = 0;
1698                         pidx = idx;
1699                 }
1700                 fl->ifl_pidx = idx;
1701
1702         }
1703 done:
1704         DBG_COUNTER_INC(rxd_flush);
1705         if (fl->ifl_pidx == 0)
1706                 pidx = fl->ifl_size - 1;
1707         else
1708                 pidx = fl->ifl_pidx - 1;
1709         ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx);
1710 }
1711
1712 static __inline void
1713 __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max)
1714 {
1715         /* we avoid allowing pidx to catch up with cidx as it confuses ixl */
1716         int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
1717 #ifdef INVARIANTS
1718         int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
1719 #endif
1720
1721         MPASS(fl->ifl_credits <= fl->ifl_size);
1722         MPASS(reclaimable == delta);
1723
1724         if (reclaimable > 0)
1725                 _iflib_fl_refill(ctx, fl, min(max, reclaimable));
1726 }
1727
1728 static void
1729 iflib_fl_bufs_free(iflib_fl_t fl)
1730 {
1731         iflib_dma_info_t idi = fl->ifl_ifdi;
1732         uint32_t i;
1733
1734         for (i = 0; i < fl->ifl_size; i++) {
1735                 struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
1736                 uint8_t *sd_flags = &fl->ifl_sds.ifsd_flags[i];
1737                 caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
1738
1739                 if (*sd_flags & RX_SW_DESC_INUSE) {
1740                         if (fl->ifl_sds.ifsd_map != NULL) {
1741                                 bus_dmamap_t sd_map = fl->ifl_sds.ifsd_map[i];
1742                                 bus_dmamap_unload(fl->ifl_desc_tag, sd_map);
1743                                 bus_dmamap_destroy(fl->ifl_desc_tag, sd_map);
1744                         }
1745                         if (*sd_m != NULL) {
1746                                 m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
1747                                 uma_zfree(zone_mbuf, *sd_m);
1748                         }
1749                         if (*sd_cl != NULL)
1750                                 uma_zfree(fl->ifl_zone, *sd_cl);
1751                         *sd_flags = 0;
1752                 } else {
1753                         MPASS(*sd_cl == NULL);
1754                         MPASS(*sd_m == NULL);
1755                 }
1756 #if MEMORY_LOGGING
1757                 fl->ifl_m_dequeued++;
1758                 fl->ifl_cl_dequeued++;
1759 #endif
1760                 *sd_cl = NULL;
1761                 *sd_m = NULL;
1762         }
1763         /*
1764          * Reset free list values
1765          */
1766         fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = 0;;
1767         bzero(idi->idi_vaddr, idi->idi_size);
1768 }
1769
1770 /*********************************************************************
1771  *
1772  *  Initialize a receive ring and its buffers.
1773  *
1774  **********************************************************************/
1775 static int
1776 iflib_fl_setup(iflib_fl_t fl)
1777 {
1778         iflib_rxq_t rxq = fl->ifl_rxq;
1779         if_ctx_t ctx = rxq->ifr_ctx;
1780         if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
1781
1782         /*
1783         ** Free current RX buffer structs and their mbufs
1784         */
1785         iflib_fl_bufs_free(fl);
1786         /* Now replenish the mbufs */
1787         MPASS(fl->ifl_credits == 0);
1788         /*
1789          * XXX don't set the max_frame_size to larger
1790          * than the hardware can handle
1791          */
1792         if (sctx->isc_max_frame_size <= 2048)
1793                 fl->ifl_buf_size = MCLBYTES;
1794         else if (sctx->isc_max_frame_size <= 4096)
1795                 fl->ifl_buf_size = MJUMPAGESIZE;
1796         else if (sctx->isc_max_frame_size <= 9216)
1797                 fl->ifl_buf_size = MJUM9BYTES;
1798         else
1799                 fl->ifl_buf_size = MJUM16BYTES;
1800         if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
1801                 ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
1802         fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
1803         fl->ifl_zone = m_getzone(fl->ifl_buf_size);
1804
1805
1806         /* avoid pre-allocating zillions of clusters to an idle card
1807          * potentially speeding up attach
1808          */
1809         _iflib_fl_refill(ctx, fl, min(128, fl->ifl_size));
1810         MPASS(min(128, fl->ifl_size) == fl->ifl_credits);
1811         if (min(128, fl->ifl_size) != fl->ifl_credits)
1812                 return (ENOBUFS);
1813         /*
1814          * handle failure
1815          */
1816         MPASS(rxq != NULL);
1817         MPASS(fl->ifl_ifdi != NULL);
1818         bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
1819             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1820         return (0);
1821 }
1822
1823 /*********************************************************************
1824  *
1825  *  Free receive ring data structures
1826  *
1827  **********************************************************************/
1828 static void
1829 iflib_rx_sds_free(iflib_rxq_t rxq)
1830 {
1831         iflib_fl_t fl;
1832         int i;
1833
1834         if (rxq->ifr_fl != NULL) {
1835                 for (i = 0; i < rxq->ifr_nfl; i++) {
1836                         fl = &rxq->ifr_fl[i];
1837                         if (fl->ifl_desc_tag != NULL) {
1838                                 bus_dma_tag_destroy(fl->ifl_desc_tag);
1839                                 fl->ifl_desc_tag = NULL;
1840                         }
1841                         free(fl->ifl_sds.ifsd_m, M_IFLIB);
1842                         free(fl->ifl_sds.ifsd_cl, M_IFLIB);
1843                         /* XXX destroy maps first */
1844                         free(fl->ifl_sds.ifsd_map, M_IFLIB);
1845                         fl->ifl_sds.ifsd_m = NULL;
1846                         fl->ifl_sds.ifsd_cl = NULL;
1847                         fl->ifl_sds.ifsd_map = NULL;
1848                 }
1849                 free(rxq->ifr_fl, M_IFLIB);
1850                 rxq->ifr_fl = NULL;
1851                 rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
1852         }
1853 }
1854
1855 /*
1856  * MI independent logic
1857  *
1858  */
1859 static void
1860 iflib_timer(void *arg)
1861 {
1862         iflib_txq_t txq = arg;
1863         if_ctx_t ctx = txq->ift_ctx;
1864         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1865
1866         if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
1867                 return;
1868         /*
1869         ** Check on the state of the TX queue(s), this
1870         ** can be done without the lock because its RO
1871         ** and the HUNG state will be static if set.
1872         */
1873         IFDI_TIMER(ctx, txq->ift_id);
1874         if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
1875                 (ctx->ifc_pause_frames == 0))
1876                 goto hung;
1877
1878         if (TXQ_AVAIL(txq) <= 2*scctx->isc_tx_nsegments ||
1879             ifmp_ring_is_stalled(txq->ift_br[0]))
1880                 GROUPTASK_ENQUEUE(&txq->ift_task);
1881
1882         ctx->ifc_pause_frames = 0;
1883         if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) 
1884                 callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
1885         return;
1886 hung:
1887         CTX_LOCK(ctx);
1888         if_setdrvflagbits(ctx->ifc_ifp, 0, IFF_DRV_RUNNING);
1889         device_printf(ctx->ifc_dev,  "TX(%d) desc avail = %d, pidx = %d\n",
1890                                   txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
1891
1892         IFDI_WATCHDOG_RESET(ctx);
1893         ctx->ifc_watchdog_events++;
1894         ctx->ifc_pause_frames = 0;
1895
1896         iflib_init_locked(ctx);
1897         CTX_UNLOCK(ctx);
1898 }
1899
1900 static void
1901 iflib_init_locked(if_ctx_t ctx)
1902 {
1903         if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
1904         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1905         if_t ifp = ctx->ifc_ifp;
1906         iflib_fl_t fl;
1907         iflib_txq_t txq;
1908         iflib_rxq_t rxq;
1909         int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
1910
1911
1912         if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
1913         IFDI_INTR_DISABLE(ctx);
1914
1915         tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
1916         tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
1917         /* Set hardware offload abilities */
1918         if_clearhwassist(ifp);
1919         if (if_getcapenable(ifp) & IFCAP_TXCSUM)
1920                 if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
1921         if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
1922                 if_sethwassistbits(ifp,  tx_ip6_csum_flags, 0);
1923         if (if_getcapenable(ifp) & IFCAP_TSO4)
1924                 if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
1925         if (if_getcapenable(ifp) & IFCAP_TSO6)
1926                 if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
1927
1928         for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) {
1929                 CALLOUT_LOCK(txq);
1930                 callout_stop(&txq->ift_timer);
1931                 callout_stop(&txq->ift_db_check);
1932                 CALLOUT_UNLOCK(txq);
1933                 iflib_netmap_txq_init(ctx, txq);
1934         }
1935         for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
1936                 iflib_netmap_rxq_init(ctx, rxq);
1937         }
1938 #ifdef INVARIANTS
1939         i = if_getdrvflags(ifp);
1940 #endif
1941         IFDI_INIT(ctx);
1942         MPASS(if_getdrvflags(ifp) == i);
1943         for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) {
1944                 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
1945                         if (iflib_fl_setup(fl)) {
1946                                 device_printf(ctx->ifc_dev, "freelist setup failed - check cluster settings\n");
1947                                 goto done;
1948                         }
1949                 }
1950         }
1951         done:
1952         if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
1953         IFDI_INTR_ENABLE(ctx);
1954         txq = ctx->ifc_txqs;
1955         for (i = 0; i < sctx->isc_ntxqsets; i++, txq++)
1956                 callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq,
1957                         txq->ift_timer.c_cpu);
1958 }
1959
1960 static int
1961 iflib_media_change(if_t ifp)
1962 {
1963         if_ctx_t ctx = if_getsoftc(ifp);
1964         int err;
1965
1966         CTX_LOCK(ctx);
1967         if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
1968                 iflib_init_locked(ctx);
1969         CTX_UNLOCK(ctx);
1970         return (err);
1971 }
1972
1973 static void
1974 iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
1975 {
1976         if_ctx_t ctx = if_getsoftc(ifp);
1977
1978         CTX_LOCK(ctx);
1979         IFDI_UPDATE_ADMIN_STATUS(ctx);
1980         IFDI_MEDIA_STATUS(ctx, ifmr);
1981         CTX_UNLOCK(ctx);
1982 }
1983
1984 static void
1985 iflib_stop(if_ctx_t ctx)
1986 {
1987         iflib_txq_t txq = ctx->ifc_txqs;
1988         iflib_rxq_t rxq = ctx->ifc_rxqs;
1989         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
1990         iflib_dma_info_t di;
1991         iflib_fl_t fl;
1992         int i, j;
1993
1994         /* Tell the stack that the interface is no longer active */
1995         if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
1996
1997         IFDI_INTR_DISABLE(ctx);
1998         DELAY(100000);
1999         IFDI_STOP(ctx);
2000         DELAY(100000);
2001
2002         iflib_debug_reset();
2003         /* Wait for current tx queue users to exit to disarm watchdog timer. */
2004         for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
2005                 /* make sure all transmitters have completed before proceeding XXX */
2006
2007                 /* clean any enqueued buffers */
2008                 iflib_ifmp_purge(txq);
2009                 /* Free any existing tx buffers. */
2010                 for (j = 0; j < txq->ift_size; j++) {
2011                         iflib_txsd_free(ctx, txq, j);
2012                 }
2013                 txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
2014                 txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0;
2015                 txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
2016                 txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
2017                 txq->ift_pullups = 0;
2018                 ifmp_ring_reset_stats(txq->ift_br[0]);
2019                 for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwtxqs; j++, di++)
2020                         bzero((void *)di->idi_vaddr, di->idi_size);
2021         }
2022         for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
2023                 /* make sure all transmitters have completed before proceeding XXX */
2024
2025                 for (j = 0, di = txq->ift_ifdi; j < ctx->ifc_nhwrxqs; j++, di++)
2026                         bzero((void *)di->idi_vaddr, di->idi_size);
2027                 /* also resets the free lists pidx/cidx */
2028                 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
2029                         iflib_fl_bufs_free(fl);
2030         }
2031 }
2032
2033 static inline void
2034 prefetch_pkts(iflib_fl_t fl, int cidx)
2035 {
2036         int nextptr;
2037         int nrxd = fl->ifl_size;
2038
2039         nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
2040         prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
2041         prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
2042         prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
2043         prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
2044         prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
2045         prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
2046         prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
2047         prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
2048         prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
2049         prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
2050 }
2051
2052 static void
2053 rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, int *cltype, int unload, iflib_fl_t *pfl, int *pcidx)
2054 {
2055         int flid, cidx;
2056         bus_dmamap_t map;
2057         iflib_fl_t fl;
2058         iflib_dma_info_t di;
2059         int next;
2060
2061         flid = irf->irf_flid;
2062         cidx = irf->irf_idx;
2063         fl = &rxq->ifr_fl[flid];
2064         fl->ifl_credits--;
2065 #if MEMORY_LOGGING
2066         fl->ifl_m_dequeued++;
2067         if (cltype)
2068                 fl->ifl_cl_dequeued++;
2069 #endif
2070         prefetch_pkts(fl, cidx);
2071         if (fl->ifl_sds.ifsd_map != NULL) {
2072                 next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
2073                 prefetch(&fl->ifl_sds.ifsd_map[next]);
2074                 map = fl->ifl_sds.ifsd_map[cidx];
2075                 di = fl->ifl_ifdi;
2076                 next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1);
2077                 prefetch(&fl->ifl_sds.ifsd_flags[next]);
2078                 bus_dmamap_sync(di->idi_tag, di->idi_map,
2079                                 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2080
2081         /* not valid assert if bxe really does SGE from non-contiguous elements */
2082                 MPASS(fl->ifl_cidx == cidx);
2083                 if (unload)
2084                         bus_dmamap_unload(fl->ifl_desc_tag, map);
2085         }
2086         if (__predict_false(++fl->ifl_cidx == fl->ifl_size)) {
2087                 fl->ifl_cidx = 0;
2088                 fl->ifl_gen = 0;
2089         }
2090         /* YES ick */
2091         if (cltype)
2092                 *cltype = fl->ifl_cltype;
2093         *pfl = fl;
2094         *pcidx = cidx;
2095 }
2096
2097 static struct mbuf *
2098 assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri)
2099 {
2100         int i, padlen , flags, cltype;
2101         struct mbuf *m, *mh, *mt, *sd_m;
2102         iflib_fl_t fl;
2103         int cidx;
2104         caddr_t cl, sd_cl;
2105
2106         i = 0;
2107         mh = NULL;
2108         do {
2109                 rxd_frag_to_sd(rxq, &ri->iri_frags[i], &cltype, TRUE, &fl, &cidx);
2110                 sd_m = fl->ifl_sds.ifsd_m[cidx];
2111                 sd_cl = fl->ifl_sds.ifsd_cl[cidx];
2112
2113                 MPASS(sd_cl != NULL);
2114                 MPASS(sd_m != NULL);
2115
2116                 /* Don't include zero-length frags */
2117                 if (ri->iri_frags[i].irf_len == 0) {
2118                         /* XXX we can save the cluster here, but not the mbuf */
2119                         m_init(sd_m, M_NOWAIT, MT_DATA, 0);
2120                         m_free(sd_m);
2121                         fl->ifl_sds.ifsd_m[cidx] = NULL;
2122                         continue;
2123                 }
2124                 m = sd_m;
2125                 if (mh == NULL) {
2126                         flags = M_PKTHDR|M_EXT;
2127                         mh = mt = m;
2128                         padlen = ri->iri_pad;
2129                 } else {
2130                         flags = M_EXT;
2131                         mt->m_next = m;
2132                         mt = m;
2133                         /* assuming padding is only on the first fragment */
2134                         padlen = 0;
2135                 }
2136                 fl->ifl_sds.ifsd_m[cidx] = NULL;
2137                 cl = fl->ifl_sds.ifsd_cl[cidx];
2138                 fl->ifl_sds.ifsd_cl[cidx] = NULL;
2139
2140                 /* Can these two be made one ? */
2141                 m_init(m, M_NOWAIT, MT_DATA, flags);
2142                 m_cljset(m, cl, cltype);
2143                 /*
2144                  * These must follow m_init and m_cljset
2145                  */
2146                 m->m_data += padlen;
2147                 ri->iri_len -= padlen;
2148                 m->m_len = ri->iri_frags[i].irf_len;
2149         } while (++i < ri->iri_nfrags);
2150
2151         return (mh);
2152 }
2153
2154 /*
2155  * Process one software descriptor
2156  */
2157 static struct mbuf *
2158 iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
2159 {
2160         struct mbuf *m;
2161         iflib_fl_t fl;
2162         caddr_t sd_cl;
2163         int cidx;
2164
2165         /* should I merge this back in now that the two paths are basically duplicated? */
2166         if (ri->iri_nfrags == 1 &&
2167             ri->iri_frags[0].irf_len <= IFLIB_RX_COPY_THRESH) {
2168                 rxd_frag_to_sd(rxq, &ri->iri_frags[0], NULL, FALSE, &fl, &cidx);
2169                 m = fl->ifl_sds.ifsd_m[cidx];
2170                 fl->ifl_sds.ifsd_m[cidx] = NULL;
2171                 sd_cl = fl->ifl_sds.ifsd_cl[cidx];
2172                 m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
2173                 memcpy(m->m_data, sd_cl, ri->iri_len);
2174                 m->m_len = ri->iri_frags[0].irf_len;
2175        } else {
2176                 m = assemble_segments(rxq, ri);
2177         }
2178         m->m_pkthdr.len = ri->iri_len;
2179         m->m_pkthdr.rcvif = ri->iri_ifp;
2180         m->m_flags |= ri->iri_flags;
2181         m->m_pkthdr.ether_vtag = ri->iri_vtag;
2182         m->m_pkthdr.flowid = ri->iri_flowid;
2183         M_HASHTYPE_SET(m, ri->iri_rsstype);
2184         m->m_pkthdr.csum_flags = ri->iri_csum_flags;
2185         m->m_pkthdr.csum_data = ri->iri_csum_data;
2186         return (m);
2187 }
2188
2189 static bool
2190 iflib_rxeof(iflib_rxq_t rxq, int budget)
2191 {
2192         if_ctx_t ctx = rxq->ifr_ctx;
2193         if_shared_ctx_t sctx = ctx->ifc_sctx;
2194         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
2195         int avail, i;
2196         uint16_t *cidxp;
2197         struct if_rxd_info ri;
2198         int err, budget_left, rx_bytes, rx_pkts;
2199         iflib_fl_t fl;
2200         struct ifnet *ifp;
2201         int lro_enabled;
2202         /*
2203          * XXX early demux data packets so that if_input processing only handles
2204          * acks in interrupt context
2205          */
2206         struct mbuf *m, *mh, *mt;
2207
2208         if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &budget)) {
2209                 return (FALSE);
2210         }
2211
2212         mh = mt = NULL;
2213         MPASS(budget > 0);
2214         rx_pkts = rx_bytes = 0;
2215         if (sctx->isc_flags & IFLIB_HAS_RXCQ)
2216                 cidxp = &rxq->ifr_cq_cidx;
2217         else
2218                 cidxp = &rxq->ifr_fl[0].ifl_cidx;
2219         if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
2220                 for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2221                         __iflib_fl_refill_lt(ctx, fl, budget + 8);
2222                 DBG_COUNTER_INC(rx_unavail);
2223                 return (false);
2224         }
2225
2226         for (budget_left = budget; (budget_left > 0) && (avail > 0); budget_left--, avail--) {
2227                 if (__predict_false(!CTX_ACTIVE(ctx))) {
2228                         DBG_COUNTER_INC(rx_ctx_inactive);
2229                         break;
2230                 }
2231                 /*
2232                  * Reset client set fields to their default values
2233                  */
2234                 bzero(&ri, sizeof(ri));
2235                 ri.iri_qsidx = rxq->ifr_id;
2236                 ri.iri_cidx = *cidxp;
2237                 ri.iri_ifp = ctx->ifc_ifp;
2238                 ri.iri_frags = rxq->ifr_frags;
2239                 err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
2240
2241                 /* in lieu of handling correctly - make sure it isn't being unhandled */
2242                 MPASS(err == 0);
2243                 if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
2244                         *cidxp = ri.iri_cidx;
2245                         /* Update our consumer index */
2246                         while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) {
2247                                 rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
2248                                 rxq->ifr_cq_gen = 0;
2249                         }
2250                         /* was this only a completion queue message? */
2251                         if (__predict_false(ri.iri_nfrags == 0))
2252                                 continue;
2253                 }
2254                 MPASS(ri.iri_nfrags != 0);
2255                 MPASS(ri.iri_len != 0);
2256
2257                 /* will advance the cidx on the corresponding free lists */
2258                 m = iflib_rxd_pkt_get(rxq, &ri);
2259                 if (avail == 0 && budget_left)
2260                         avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
2261
2262                 if (__predict_false(m == NULL)) {
2263                         DBG_COUNTER_INC(rx_mbuf_null);
2264                         continue;
2265                 }
2266                 /* imm_pkt: -- cxgb */
2267                 if (mh == NULL)
2268                         mh = mt = m;
2269                 else {
2270                         mt->m_nextpkt = m;
2271                         mt = m;
2272                 }
2273         }
2274         /* make sure that we can refill faster than drain */
2275         for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
2276                 __iflib_fl_refill_lt(ctx, fl, budget + 8);
2277
2278         ifp = ctx->ifc_ifp;
2279         lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
2280         while (mh != NULL) {
2281                 m = mh;
2282                 mh = mh->m_nextpkt;
2283                 m->m_nextpkt = NULL;
2284                 rx_bytes += m->m_pkthdr.len;
2285                 rx_pkts++;
2286 #if defined(INET6) || defined(INET)
2287                 if (lro_enabled && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
2288                         continue;
2289 #endif
2290                 DBG_COUNTER_INC(rx_if_input);
2291                 ifp->if_input(ifp, m);
2292         }
2293
2294         if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
2295         if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
2296
2297         /*
2298          * Flush any outstanding LRO work
2299          */
2300 #if defined(INET6) || defined(INET)
2301         tcp_lro_flush_all(&rxq->ifr_lc);
2302 #endif
2303         if (avail)
2304                 return true;
2305         return (iflib_rxd_avail(ctx, rxq, *cidxp, 1));
2306 }
2307
2308 #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
2309 #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
2310 #define TXQ_MAX_DB_DEFERRED(size) (size >> 5)
2311 #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
2312
2313 static __inline void
2314 iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring)
2315 {
2316         uint32_t dbval;
2317
2318         if (ring || txq->ift_db_pending >=
2319             TXQ_MAX_DB_DEFERRED(txq->ift_size)) {
2320
2321                 /* the lock will only ever be contended in the !min_latency case */
2322                 if (!TXDB_TRYLOCK(txq))
2323                         return;
2324                 dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
2325                 ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
2326                 txq->ift_db_pending = txq->ift_npending = 0;
2327                 TXDB_UNLOCK(txq);
2328         }
2329 }
2330
2331 static void
2332 iflib_txd_deferred_db_check(void * arg)
2333 {
2334         iflib_txq_t txq = arg;
2335
2336         /* simple non-zero boolean so use bitwise OR */
2337         if ((txq->ift_db_pending | txq->ift_npending) &&
2338             txq->ift_db_pending >= txq->ift_db_pending_queued)
2339                 iflib_txd_db_check(txq->ift_ctx, txq, TRUE);
2340         txq->ift_db_pending_queued = 0;
2341         if (ifmp_ring_is_stalled(txq->ift_br[0]))
2342                 iflib_txq_check_drain(txq, 4);
2343 }
2344
2345 #ifdef PKT_DEBUG
2346 static void
2347 print_pkt(if_pkt_info_t pi)
2348 {
2349         printf("pi len:  %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
2350                pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
2351         printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
2352                pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
2353         printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
2354                pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
2355 }
2356 #endif
2357
2358 #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
2359 #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
2360
2361 static int
2362 iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
2363 {
2364         if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
2365         struct ether_vlan_header *eh;
2366         struct mbuf *m, *n;
2367
2368         n = m = *mp;
2369         if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
2370             M_WRITABLE(m) == 0) {
2371                 if ((m = m_dup(m, M_NOWAIT)) == NULL) {
2372                         return (ENOMEM);
2373                 } else {
2374                         m_freem(*mp);
2375                         n = *mp = m;
2376                 }
2377         }
2378
2379         /*
2380          * Determine where frame payload starts.
2381          * Jump over vlan headers if already present,
2382          * helpful for QinQ too.
2383          */
2384         if (__predict_false(m->m_len < sizeof(*eh))) {
2385                 txq->ift_pullups++;
2386                 if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
2387                         return (ENOMEM);
2388         }
2389         eh = mtod(m, struct ether_vlan_header *);
2390         if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
2391                 pi->ipi_etype = ntohs(eh->evl_proto);
2392                 pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
2393         } else {
2394                 pi->ipi_etype = ntohs(eh->evl_encap_proto);
2395                 pi->ipi_ehdrlen = ETHER_HDR_LEN;
2396         }
2397
2398         switch (pi->ipi_etype) {
2399 #ifdef INET
2400         case ETHERTYPE_IP:
2401         {
2402                 struct ip *ip = NULL;
2403                 struct tcphdr *th = NULL;
2404                 int minthlen;
2405
2406                 minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
2407                 if (__predict_false(m->m_len < minthlen)) {
2408                         /*
2409                          * if this code bloat is causing too much of a hit
2410                          * move it to a separate function and mark it noinline
2411                          */
2412                         if (m->m_len == pi->ipi_ehdrlen) {
2413                                 n = m->m_next;
2414                                 MPASS(n);
2415                                 if (n->m_len >= sizeof(*ip))  {
2416                                         ip = (struct ip *)n->m_data;
2417                                         if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
2418                                                 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
2419                                 } else {
2420                                         txq->ift_pullups++;
2421                                         if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
2422                                                 return (ENOMEM);
2423                                         ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
2424                                 }
2425                         } else {
2426                                 txq->ift_pullups++;
2427                                 if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
2428                                         return (ENOMEM);
2429                                 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
2430                                 if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
2431                                         th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
2432                         }
2433                 } else {
2434                         ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
2435                         if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
2436                                 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
2437                 }
2438                 pi->ipi_ip_hlen = ip->ip_hl << 2;
2439                 pi->ipi_ipproto = ip->ip_p;
2440                 pi->ipi_flags |= IPI_TX_IPV4;
2441
2442                 if (pi->ipi_csum_flags & CSUM_IP)
2443                        ip->ip_sum = 0;
2444
2445                 if (pi->ipi_ipproto == IPPROTO_TCP) {
2446                         if (__predict_false(th == NULL)) {
2447                                 txq->ift_pullups++;
2448                                 if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
2449                                         return (ENOMEM);
2450                                 th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
2451                         }
2452                         pi->ipi_tcp_hflags = th->th_flags;
2453                         pi->ipi_tcp_hlen = th->th_off << 2;
2454                         pi->ipi_tcp_seq = th->th_seq;
2455                 }
2456                 if (IS_TSO4(pi)) {
2457                         if (__predict_false(ip->ip_p != IPPROTO_TCP))
2458                                 return (ENXIO);
2459                         th->th_sum = in_pseudo(ip->ip_src.s_addr,
2460                                                ip->ip_dst.s_addr, htons(IPPROTO_TCP));
2461                         pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
2462                         if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
2463                                 ip->ip_sum = 0;
2464                                 ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
2465                         }
2466                 }
2467                 break;
2468         }
2469 #endif
2470 #ifdef INET6
2471         case ETHERTYPE_IPV6:
2472         {
2473                 struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
2474                 struct tcphdr *th;
2475                 pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
2476
2477                 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
2478                         if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
2479                                 return (ENOMEM);
2480                 }
2481                 th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
2482
2483                 /* XXX-BZ this will go badly in case of ext hdrs. */
2484                 pi->ipi_ipproto = ip6->ip6_nxt;
2485                 pi->ipi_flags |= IPI_TX_IPV6;
2486
2487                 if (pi->ipi_ipproto == IPPROTO_TCP) {
2488                         if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
2489                                 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
2490                                         return (ENOMEM);
2491                         }
2492                         pi->ipi_tcp_hflags = th->th_flags;
2493                         pi->ipi_tcp_hlen = th->th_off << 2;
2494                 }
2495                 if (IS_TSO6(pi)) {
2496
2497                         if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
2498                                 return (ENXIO);
2499                         /*
2500                          * The corresponding flag is set by the stack in the IPv4
2501                          * TSO case, but not in IPv6 (at least in FreeBSD 10.2).
2502                          * So, set it here because the rest of the flow requires it.
2503                          */
2504                         pi->ipi_csum_flags |= CSUM_TCP_IPV6;
2505                         th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
2506                         pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
2507                 }
2508                 break;
2509         }
2510 #endif
2511         default:
2512                 pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
2513                 pi->ipi_ip_hlen = 0;
2514                 break;
2515         }
2516         *mp = m;
2517
2518         return (0);
2519 }
2520
2521 static  __noinline  struct mbuf *
2522 collapse_pkthdr(struct mbuf *m0)
2523 {
2524         struct mbuf *m, *m_next, *tmp;
2525
2526         m = m0;
2527         m_next = m->m_next;
2528         while (m_next != NULL && m_next->m_len == 0) {
2529                 m = m_next;
2530                 m->m_next = NULL;
2531                 m_free(m);
2532                 m_next = m_next->m_next;
2533         }
2534         m = m0;
2535         m->m_next = m_next;
2536         if ((m_next->m_flags & M_EXT) == 0) {
2537                 m = m_defrag(m, M_NOWAIT);
2538         } else {
2539                 tmp = m_next->m_next;
2540                 memcpy(m_next, m, MPKTHSIZE);
2541                 m = m_next;
2542                 m->m_next = tmp;
2543         }
2544         return (m);
2545 }
2546
2547 /*
2548  * If dodgy hardware rejects the scatter gather chain we've handed it
2549  * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
2550  * m_defrag'd mbufs
2551  */
2552 static __noinline struct mbuf *
2553 iflib_remove_mbuf(iflib_txq_t txq)
2554 {
2555         int ntxd, i, pidx;
2556         struct mbuf *m, *mh, **ifsd_m;
2557
2558         pidx = txq->ift_pidx;
2559         ifsd_m = txq->ift_sds.ifsd_m;
2560         ntxd = txq->ift_size;
2561         mh = m = ifsd_m[pidx];
2562         ifsd_m[pidx] = NULL;
2563 #if MEMORY_LOGGING
2564         txq->ift_dequeued++;
2565 #endif
2566         i = 1;
2567
2568         while (m) {
2569                 ifsd_m[(pidx + i) & (ntxd -1)] = NULL;
2570 #if MEMORY_LOGGING
2571                 txq->ift_dequeued++;
2572 #endif
2573                 m = m->m_next;
2574                 i++;
2575         }
2576         return (mh);
2577 }
2578
2579 static int
2580 iflib_busdma_load_mbuf_sg(iflib_txq_t txq, bus_dma_tag_t tag, bus_dmamap_t map,
2581                           struct mbuf **m0, bus_dma_segment_t *segs, int *nsegs,
2582                           int max_segs, int flags)
2583 {
2584         if_ctx_t ctx;
2585         if_shared_ctx_t         sctx;
2586         if_softc_ctx_t          scctx;
2587         int i, next, pidx, mask, err, maxsegsz, ntxd, count;
2588         struct mbuf *m, *tmp, **ifsd_m, **mp;
2589
2590         m = *m0;
2591
2592         /*
2593          * Please don't ever do this
2594          */
2595         if (__predict_false(m->m_len == 0))
2596                 *m0 = m = collapse_pkthdr(m);
2597
2598         ctx = txq->ift_ctx;
2599         sctx = ctx->ifc_sctx;
2600         scctx = &ctx->ifc_softc_ctx;
2601         ifsd_m = txq->ift_sds.ifsd_m;
2602         ntxd = txq->ift_size;
2603         pidx = txq->ift_pidx;
2604         if (map != NULL) {
2605                 uint8_t *ifsd_flags = txq->ift_sds.ifsd_flags;
2606
2607                 err = bus_dmamap_load_mbuf_sg(tag, map,
2608                                               *m0, segs, nsegs, BUS_DMA_NOWAIT);
2609                 if (err)
2610                         return (err);
2611                 ifsd_flags[pidx] |= TX_SW_DESC_MAPPED;
2612                 i = 0;
2613                 next = pidx;
2614                 mask = (txq->ift_size-1);
2615                 m = *m0;
2616                 do {
2617                         mp = &ifsd_m[next];
2618                         *mp = m;
2619                         m = m->m_next;
2620                         if (__predict_false((*mp)->m_len == 0)) {
2621                                 m_free(*mp);
2622                                 *mp = NULL;
2623                         } else
2624                                 next = (pidx + i) & (ntxd-1);
2625                 } while (m != NULL);
2626         } else {
2627                 int buflen, sgsize, max_sgsize;
2628                 vm_offset_t vaddr;
2629                 vm_paddr_t curaddr;
2630
2631                 count = i = 0;
2632                 maxsegsz = sctx->isc_tx_maxsize;
2633                 m = *m0;
2634                 do {
2635                         if (__predict_false(m->m_len <= 0)) {
2636                                 tmp = m;
2637                                 m = m->m_next;
2638                                 tmp->m_next = NULL;
2639                                 m_free(tmp);
2640                                 continue;
2641                         }
2642                         buflen = m->m_len;
2643                         vaddr = (vm_offset_t)m->m_data;
2644                         /*
2645                          * see if we can't be smarter about physically
2646                          * contiguous mappings
2647                          */
2648                         next = (pidx + count) & (ntxd-1);
2649                         MPASS(ifsd_m[next] == NULL);
2650 #if MEMORY_LOGGING
2651                         txq->ift_enqueued++;
2652 #endif
2653                         ifsd_m[next] = m;
2654                         while (buflen > 0) {
2655                                 max_sgsize = MIN(buflen, maxsegsz);
2656                                 curaddr = pmap_kextract(vaddr);
2657                                 sgsize = PAGE_SIZE - (curaddr & PAGE_MASK);
2658                                 sgsize = MIN(sgsize, max_sgsize);
2659                                 segs[i].ds_addr = curaddr;
2660                                 segs[i].ds_len = sgsize;
2661                                 vaddr += sgsize;
2662                                 buflen -= sgsize;
2663                                 i++;
2664                                 if (i >= max_segs)
2665                                         goto err;
2666                         }
2667                         count++;
2668                         tmp = m;
2669                         m = m->m_next;
2670                 } while (m != NULL);
2671                 *nsegs = i;
2672         }
2673         return (0);
2674 err:
2675         *m0 = iflib_remove_mbuf(txq);
2676         return (EFBIG);
2677 }
2678
2679 static int
2680 iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
2681 {
2682         if_ctx_t                ctx;
2683         if_shared_ctx_t         sctx;
2684         if_softc_ctx_t          scctx;
2685         bus_dma_segment_t       *segs;
2686         struct mbuf             *m_head;
2687         bus_dmamap_t            map;
2688         struct if_pkt_info      pi;
2689         int remap = 0;
2690         int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
2691         bus_dma_tag_t desc_tag;
2692
2693         segs = txq->ift_segs;
2694         ctx = txq->ift_ctx;
2695         sctx = ctx->ifc_sctx;
2696         scctx = &ctx->ifc_softc_ctx;
2697         segs = txq->ift_segs;
2698         ntxd = txq->ift_size;
2699         m_head = *m_headp;
2700         map = NULL;
2701
2702         /*
2703          * If we're doing TSO the next descriptor to clean may be quite far ahead
2704          */
2705         cidx = txq->ift_cidx;
2706         pidx = txq->ift_pidx;
2707         next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
2708
2709         /* prefetch the next cache line of mbuf pointers and flags */
2710         prefetch(&txq->ift_sds.ifsd_m[next]);
2711         if (txq->ift_sds.ifsd_map != NULL) {
2712                 prefetch(&txq->ift_sds.ifsd_map[next]);
2713                 map = txq->ift_sds.ifsd_map[pidx];
2714                 next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
2715                 prefetch(&txq->ift_sds.ifsd_flags[next]);
2716         }
2717
2718
2719         if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
2720                 desc_tag = txq->ift_tso_desc_tag;
2721                 max_segs = scctx->isc_tx_tso_segments_max;
2722         } else {
2723                 desc_tag = txq->ift_desc_tag;
2724                 max_segs = scctx->isc_tx_nsegments;
2725         }
2726         m_head = *m_headp;
2727         bzero(&pi, sizeof(pi));
2728         pi.ipi_len = m_head->m_pkthdr.len;
2729         pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
2730         pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
2731         pi.ipi_vtag = (m_head->m_flags & M_VLANTAG) ? m_head->m_pkthdr.ether_vtag : 0;
2732         pi.ipi_pidx = pidx;
2733         pi.ipi_qsidx = txq->ift_id;
2734
2735         /* deliberate bitwise OR to make one condition */
2736         if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
2737                 if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0))
2738                         return (err);
2739                 m_head = *m_headp;
2740         }
2741
2742 retry:
2743         err = iflib_busdma_load_mbuf_sg(txq, desc_tag, map, m_headp, segs, &nsegs, max_segs, BUS_DMA_NOWAIT);
2744 defrag:
2745         if (__predict_false(err)) {
2746                 switch (err) {
2747                 case EFBIG:
2748                         /* try collapse once and defrag once */
2749                         if (remap == 0)
2750                                 m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
2751                         if (remap == 1)
2752                                 m_head = m_defrag(*m_headp, M_NOWAIT);
2753                         remap++;
2754                         if (__predict_false(m_head == NULL))
2755                                 goto defrag_failed;
2756                         txq->ift_mbuf_defrag++;
2757                         *m_headp = m_head;
2758                         goto retry;
2759                         break;
2760                 case ENOMEM:
2761                         txq->ift_no_tx_dma_setup++;
2762                         break;
2763                 default:
2764                         txq->ift_no_tx_dma_setup++;
2765                         m_freem(*m_headp);
2766                         DBG_COUNTER_INC(tx_frees);
2767                         *m_headp = NULL;
2768                         break;
2769                 }
2770                 txq->ift_map_failed++;
2771                 DBG_COUNTER_INC(encap_load_mbuf_fail);
2772                 return (err);
2773         }
2774
2775         /*
2776          * XXX assumes a 1 to 1 relationship between segments and
2777          *        descriptors - this does not hold true on all drivers, e.g.
2778          *        cxgb
2779          */
2780         if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
2781                 txq->ift_no_desc_avail++;
2782                 if (map != NULL)
2783                         bus_dmamap_unload(desc_tag, map);
2784                 DBG_COUNTER_INC(encap_txq_avail_fail);
2785                 if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
2786                         GROUPTASK_ENQUEUE(&txq->ift_task);
2787                 return (ENOBUFS);
2788         }
2789         pi.ipi_segs = segs;
2790         pi.ipi_nsegs = nsegs;
2791
2792         MPASS(pidx >= 0 && pidx < txq->ift_size);
2793 #ifdef PKT_DEBUG
2794         print_pkt(&pi);
2795 #endif
2796         if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
2797                 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
2798                                                 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2799
2800                 DBG_COUNTER_INC(tx_encap);
2801                 MPASS(pi.ipi_new_pidx >= 0 &&
2802                     pi.ipi_new_pidx < txq->ift_size);
2803
2804                 ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
2805                 if (pi.ipi_new_pidx < pi.ipi_pidx) {
2806                         ndesc += txq->ift_size;
2807                         txq->ift_gen = 1;
2808                 }
2809                 /*
2810                  * drivers can need as many as 
2811                  * two sentinels
2812                  */
2813                 MPASS(ndesc <= pi.ipi_nsegs + 2);
2814                 MPASS(pi.ipi_new_pidx != pidx);
2815                 MPASS(ndesc > 0);
2816                 txq->ift_in_use += ndesc;
2817                 /*
2818                  * We update the last software descriptor again here because there may
2819                  * be a sentinel and/or there may be more mbufs than segments
2820                  */
2821                 txq->ift_pidx = pi.ipi_new_pidx;
2822                 txq->ift_npending += pi.ipi_ndescs;
2823         } else if (__predict_false(err == EFBIG && remap < 2)) {
2824                 *m_headp = m_head = iflib_remove_mbuf(txq);
2825                 remap = 1;
2826                 txq->ift_txd_encap_efbig++;
2827                 goto defrag;
2828         } else
2829                 DBG_COUNTER_INC(encap_txd_encap_fail);
2830         return (err);
2831
2832 defrag_failed:
2833         txq->ift_mbuf_defrag_failed++;
2834         txq->ift_map_failed++;
2835         m_freem(*m_headp);
2836         DBG_COUNTER_INC(tx_frees);
2837         *m_headp = NULL;
2838         return (ENOMEM);
2839 }
2840
2841 /* forward compatibility for cxgb */
2842 #define FIRST_QSET(ctx) 0
2843
2844 #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
2845 #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
2846 #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
2847 #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
2848 #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
2849 #define MAX_TX_DESC(ctx) ((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max)
2850
2851
2852
2853 /* if there are more than TXQ_MIN_OCCUPANCY packets pending we consider deferring
2854  * doorbell writes
2855  *
2856  * ORing with 2 assures that min occupancy is never less than 2 without any conditional logic
2857  */
2858 #define TXQ_MIN_OCCUPANCY(size) ((size >> 6)| 0x2)
2859
2860 static inline int
2861 iflib_txq_min_occupancy(iflib_txq_t txq)
2862 {
2863         if_ctx_t ctx;
2864
2865         ctx = txq->ift_ctx;
2866         return (get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx,
2867             txq->ift_gen) < TXQ_MIN_OCCUPANCY(txq->ift_size) +
2868             MAX_TX_DESC(ctx));
2869 }
2870
2871 static void
2872 iflib_tx_desc_free(iflib_txq_t txq, int n)
2873 {
2874         int hasmap;
2875         uint32_t qsize, cidx, mask, gen;
2876         struct mbuf *m, **ifsd_m;
2877         uint8_t *ifsd_flags;
2878         bus_dmamap_t *ifsd_map;
2879
2880         cidx = txq->ift_cidx;
2881         gen = txq->ift_gen;
2882         qsize = txq->ift_size;
2883         mask = qsize-1;
2884         hasmap = txq->ift_sds.ifsd_map != NULL;
2885         ifsd_flags = txq->ift_sds.ifsd_flags;
2886         ifsd_m = txq->ift_sds.ifsd_m;
2887         ifsd_map = txq->ift_sds.ifsd_map;
2888
2889         while (n--) {
2890                 prefetch(ifsd_m[(cidx + 3) & mask]);
2891                 prefetch(ifsd_m[(cidx + 4) & mask]);
2892
2893                 if (ifsd_m[cidx] != NULL) {
2894                         prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
2895                         prefetch(&ifsd_flags[(cidx + CACHE_PTR_INCREMENT) & mask]);
2896                         if (hasmap && (ifsd_flags[cidx] & TX_SW_DESC_MAPPED)) {
2897                                 /*
2898                                  * does it matter if it's not the TSO tag? If so we'll
2899                                  * have to add the type to flags
2900                                  */
2901                                 bus_dmamap_unload(txq->ift_desc_tag, ifsd_map[cidx]);
2902                                 ifsd_flags[cidx] &= ~TX_SW_DESC_MAPPED;
2903                         }
2904                         if ((m = ifsd_m[cidx]) != NULL) {
2905                                 /* XXX we don't support any drivers that batch packets yet */
2906                                 MPASS(m->m_nextpkt == NULL);
2907
2908                                 m_free(m);
2909                                 ifsd_m[cidx] = NULL;
2910 #if MEMORY_LOGGING
2911                                 txq->ift_dequeued++;
2912 #endif
2913                                 DBG_COUNTER_INC(tx_frees);
2914                         }
2915                 }
2916                 if (__predict_false(++cidx == qsize)) {
2917                         cidx = 0;
2918                         gen = 0;
2919                 }
2920         }
2921         txq->ift_cidx = cidx;
2922         txq->ift_gen = gen;
2923 }
2924
2925 static __inline int
2926 iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
2927 {
2928         int reclaim;
2929         if_ctx_t ctx = txq->ift_ctx;
2930
2931         KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
2932         MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
2933
2934         /*
2935          * Need a rate-limiting check so that this isn't called every time
2936          */
2937         iflib_tx_credits_update(ctx, txq);
2938         reclaim = DESC_RECLAIMABLE(txq);
2939
2940         if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
2941 #ifdef INVARIANTS
2942                 if (iflib_verbose_debug) {
2943                         printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
2944                                txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
2945                                reclaim, thresh);
2946
2947                 }
2948 #endif
2949                 return (0);
2950         }
2951         iflib_tx_desc_free(txq, reclaim);
2952         txq->ift_cleaned += reclaim;
2953         txq->ift_in_use -= reclaim;
2954
2955         if (txq->ift_active == FALSE)
2956                 txq->ift_active = TRUE;
2957
2958         return (reclaim);
2959 }
2960
2961 static struct mbuf **
2962 _ring_peek_one(struct ifmp_ring *r, int cidx, int offset)
2963 {
2964
2965         return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (r->size-1)]));
2966 }
2967
2968 static void
2969 iflib_txq_check_drain(iflib_txq_t txq, int budget)
2970 {
2971
2972         ifmp_ring_check_drainage(txq->ift_br[0], budget);
2973 }
2974
2975 static uint32_t
2976 iflib_txq_can_drain(struct ifmp_ring *r)
2977 {
2978         iflib_txq_t txq = r->cookie;
2979         if_ctx_t ctx = txq->ift_ctx;
2980
2981         return ((TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) ||
2982                 ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, txq->ift_cidx_processed, false));
2983 }
2984
2985 static uint32_t
2986 iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
2987 {
2988         iflib_txq_t txq = r->cookie;
2989         if_ctx_t ctx = txq->ift_ctx;
2990         if_t ifp = ctx->ifc_ifp;
2991         struct mbuf **mp, *m;
2992         int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail, err, in_use_prev, desc_used;
2993
2994         if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
2995                             !LINK_ACTIVE(ctx))) {
2996                 DBG_COUNTER_INC(txq_drain_notready);
2997                 return (0);
2998         }
2999
3000         avail = IDXDIFF(pidx, cidx, r->size);
3001         if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
3002                 DBG_COUNTER_INC(txq_drain_flushing);
3003                 for (i = 0; i < avail; i++) {
3004                         m_free(r->items[(cidx + i) & (r->size-1)]);
3005                         r->items[(cidx + i) & (r->size-1)] = NULL;
3006                 }
3007                 return (avail);
3008         }
3009         iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
3010         if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
3011                 txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3012                 CALLOUT_LOCK(txq);
3013                 callout_stop(&txq->ift_timer);
3014                 callout_stop(&txq->ift_db_check);
3015                 CALLOUT_UNLOCK(txq);
3016                 DBG_COUNTER_INC(txq_drain_oactive);
3017                 return (0);
3018         }
3019         consumed = mcast_sent = bytes_sent = pkt_sent = 0;
3020         count = MIN(avail, TX_BATCH_SIZE);
3021 #ifdef INVARIANTS
3022         if (iflib_verbose_debug)
3023                 printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
3024                        avail, ctx->ifc_flags, TXQ_AVAIL(txq));
3025 #endif
3026
3027         for (desc_used = i = 0; i < count && TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2; i++) {
3028                 mp = _ring_peek_one(r, cidx, i);
3029                 MPASS(mp != NULL && *mp != NULL);
3030                 in_use_prev = txq->ift_in_use;
3031                 if ((err = iflib_encap(txq, mp)) == ENOBUFS) {
3032                         DBG_COUNTER_INC(txq_drain_encapfail);
3033                         /* no room - bail out */
3034                         break;
3035                 }
3036                 consumed++;
3037                 if (err) {
3038                         DBG_COUNTER_INC(txq_drain_encapfail);
3039                         /* we can't send this packet - skip it */
3040                         continue;
3041                 }
3042                 pkt_sent++;
3043                 m = *mp;
3044                 DBG_COUNTER_INC(tx_sent);
3045                 bytes_sent += m->m_pkthdr.len;
3046                 if (m->m_flags & M_MCAST)
3047                         mcast_sent++;
3048
3049                 txq->ift_db_pending += (txq->ift_in_use - in_use_prev);
3050                 desc_used += (txq->ift_in_use - in_use_prev);
3051                 iflib_txd_db_check(ctx, txq, FALSE);
3052                 ETHER_BPF_MTAP(ifp, m);
3053                 if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
3054                         break;
3055
3056                 if (desc_used >= TXQ_MAX_DB_CONSUMED(txq->ift_size))
3057                         break;
3058         }
3059
3060         if ((iflib_min_tx_latency || iflib_txq_min_occupancy(txq)) && txq->ift_db_pending)
3061                 iflib_txd_db_check(ctx, txq, TRUE);
3062         else if ((txq->ift_db_pending || TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx) + 2) &&
3063                  (callout_pending(&txq->ift_db_check) == 0)) {
3064                 txq->ift_db_pending_queued = txq->ift_db_pending;
3065                 callout_reset_on(&txq->ift_db_check, 1, iflib_txd_deferred_db_check,
3066                                  txq, txq->ift_db_check.c_cpu);
3067         }
3068         if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
3069         if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
3070         if (mcast_sent)
3071                 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
3072 #ifdef INVARIANTS
3073         if (iflib_verbose_debug)
3074                 printf("consumed=%d\n", consumed);
3075 #endif
3076         return (consumed);
3077 }
3078
3079 static uint32_t
3080 iflib_txq_drain_always(struct ifmp_ring *r)
3081 {
3082         return (1);
3083 }
3084
3085 static uint32_t
3086 iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
3087 {
3088         int i, avail;
3089         struct mbuf **mp;
3090         iflib_txq_t txq;
3091
3092         txq = r->cookie;
3093
3094         txq->ift_qstatus = IFLIB_QUEUE_IDLE;
3095         CALLOUT_LOCK(txq);
3096         callout_stop(&txq->ift_timer);
3097         callout_stop(&txq->ift_db_check);
3098         CALLOUT_UNLOCK(txq);
3099
3100         avail = IDXDIFF(pidx, cidx, r->size);
3101         for (i = 0; i < avail; i++) {
3102                 mp = _ring_peek_one(r, cidx, i);
3103                 m_freem(*mp);
3104         }
3105         MPASS(ifmp_ring_is_stalled(r) == 0);
3106         return (avail);
3107 }
3108
3109 static void
3110 iflib_ifmp_purge(iflib_txq_t txq)
3111 {
3112         struct ifmp_ring *r;
3113
3114         r = txq->ift_br[0];
3115         r->drain = iflib_txq_drain_free;
3116         r->can_drain = iflib_txq_drain_always;
3117
3118         ifmp_ring_check_drainage(r, r->size);
3119
3120         r->drain = iflib_txq_drain;
3121         r->can_drain = iflib_txq_can_drain;
3122 }
3123
3124 static void
3125 _task_fn_tx(void *context)
3126 {
3127         iflib_txq_t txq = context;
3128         if_ctx_t ctx = txq->ift_ctx;
3129
3130 #ifdef IFLIB_DIAGNOSTICS
3131         txq->ift_cpu_exec_count[curcpu]++;
3132 #endif
3133         if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
3134                 return;
3135         ifmp_ring_check_drainage(txq->ift_br[0], TX_BATCH_SIZE);
3136 }
3137
3138 static void
3139 _task_fn_rx(void *context)
3140 {
3141         iflib_rxq_t rxq = context;
3142         if_ctx_t ctx = rxq->ifr_ctx;
3143         bool more;
3144         int rc;
3145
3146 #ifdef IFLIB_DIAGNOSTICS
3147         rxq->ifr_cpu_exec_count[curcpu]++;
3148 #endif
3149         DBG_COUNTER_INC(task_fn_rxs);
3150         if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
3151                 return;
3152
3153         if ((more = iflib_rxeof(rxq, 16 /* XXX */)) == false) {
3154                 if (ctx->ifc_flags & IFC_LEGACY)
3155                         IFDI_INTR_ENABLE(ctx);
3156                 else {
3157                         DBG_COUNTER_INC(rx_intr_enables);
3158                         rc = IFDI_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
3159                         KASSERT(rc != ENOTSUP, ("MSI-X support requires queue_intr_enable, but not implemented in driver"));
3160                 }
3161         }
3162         if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
3163                 return;
3164         if (more)
3165                 GROUPTASK_ENQUEUE(&rxq->ifr_task);
3166 }
3167
3168 static void
3169 _task_fn_admin(void *context)
3170 {
3171         if_ctx_t ctx = context;
3172         if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
3173         iflib_txq_t txq;
3174         int i;
3175
3176         if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
3177                 return;
3178
3179         CTX_LOCK(ctx);
3180         for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
3181                 CALLOUT_LOCK(txq);
3182                 callout_stop(&txq->ift_timer);
3183                 CALLOUT_UNLOCK(txq);
3184         }
3185         IFDI_UPDATE_ADMIN_STATUS(ctx);
3186         for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
3187                 callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, txq->ift_timer.c_cpu);
3188         IFDI_LINK_INTR_ENABLE(ctx);
3189         CTX_UNLOCK(ctx);
3190
3191         if (LINK_ACTIVE(ctx) == 0)
3192                 return;
3193         for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
3194                 iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
3195 }
3196
3197
3198 static void
3199 _task_fn_iov(void *context)
3200 {
3201         if_ctx_t ctx = context;
3202
3203         if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
3204                 return;
3205
3206         CTX_LOCK(ctx);
3207         IFDI_VFLR_HANDLE(ctx);
3208         CTX_UNLOCK(ctx);
3209 }
3210
3211 static int
3212 iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
3213 {
3214         int err;
3215         if_int_delay_info_t info;
3216         if_ctx_t ctx;
3217
3218         info = (if_int_delay_info_t)arg1;
3219         ctx = info->iidi_ctx;
3220         info->iidi_req = req;
3221         info->iidi_oidp = oidp;
3222         CTX_LOCK(ctx);
3223         err = IFDI_SYSCTL_INT_DELAY(ctx, info);
3224         CTX_UNLOCK(ctx);
3225         return (err);
3226 }
3227
3228 /*********************************************************************
3229  *
3230  *  IFNET FUNCTIONS
3231  *
3232  **********************************************************************/
3233
3234 static void
3235 iflib_if_init_locked(if_ctx_t ctx)
3236 {
3237         iflib_stop(ctx);
3238         iflib_init_locked(ctx);
3239 }
3240
3241
3242 static void
3243 iflib_if_init(void *arg)
3244 {
3245         if_ctx_t ctx = arg;
3246
3247         CTX_LOCK(ctx);
3248         iflib_if_init_locked(ctx);
3249         CTX_UNLOCK(ctx);
3250 }
3251
3252 static int
3253 iflib_if_transmit(if_t ifp, struct mbuf *m)
3254 {
3255         if_ctx_t        ctx = if_getsoftc(ifp);
3256
3257         iflib_txq_t txq;
3258         int err, qidx;
3259
3260         if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
3261                 DBG_COUNTER_INC(tx_frees);
3262                 m_freem(m);
3263                 return (ENOBUFS);
3264         }
3265
3266         MPASS(m->m_nextpkt == NULL);
3267         qidx = 0;
3268         if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m))
3269                 qidx = QIDX(ctx, m);
3270         /*
3271          * XXX calculate buf_ring based on flowid (divvy up bits?)
3272          */
3273         txq = &ctx->ifc_txqs[qidx];
3274
3275 #ifdef DRIVER_BACKPRESSURE
3276         if (txq->ift_closed) {
3277                 while (m != NULL) {
3278                         next = m->m_nextpkt;
3279                         m->m_nextpkt = NULL;
3280                         m_freem(m);
3281                         m = next;
3282                 }
3283                 return (ENOBUFS);
3284         }
3285 #endif
3286 #ifdef notyet
3287         qidx = count = 0;
3288         mp = marr;
3289         next = m;
3290         do {
3291                 count++;
3292                 next = next->m_nextpkt;
3293         } while (next != NULL);
3294
3295         if (count > nitems(marr))
3296                 if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
3297                         /* XXX check nextpkt */
3298                         m_freem(m);
3299                         /* XXX simplify for now */
3300                         DBG_COUNTER_INC(tx_frees);
3301                         return (ENOBUFS);
3302                 }
3303         for (next = m, i = 0; next != NULL; i++) {
3304                 mp[i] = next;
3305                 next = next->m_nextpkt;
3306                 mp[i]->m_nextpkt = NULL;
3307         }
3308 #endif
3309         DBG_COUNTER_INC(tx_seen);
3310         err = ifmp_ring_enqueue(txq->ift_br[0], (void **)&m, 1, TX_BATCH_SIZE);
3311
3312         if (err) {
3313                 GROUPTASK_ENQUEUE(&txq->ift_task);
3314                 /* support forthcoming later */
3315 #ifdef DRIVER_BACKPRESSURE
3316                 txq->ift_closed = TRUE;
3317 #endif
3318                 ifmp_ring_check_drainage(txq->ift_br[0], TX_BATCH_SIZE);
3319                 m_freem(m);
3320         } else if (TXQ_AVAIL(txq) < (txq->ift_size >> 1)) {
3321                 GROUPTASK_ENQUEUE(&txq->ift_task);
3322         }
3323
3324         return (err);
3325 }
3326
3327 static void
3328 iflib_if_qflush(if_t ifp)
3329 {
3330         if_ctx_t ctx = if_getsoftc(ifp);
3331         iflib_txq_t txq = ctx->ifc_txqs;
3332         int i;
3333
3334         CTX_LOCK(ctx);
3335         ctx->ifc_flags |= IFC_QFLUSH;
3336         CTX_UNLOCK(ctx);
3337         for (i = 0; i < NTXQSETS(ctx); i++, txq++)
3338                 while (!(ifmp_ring_is_idle(txq->ift_br[0]) || ifmp_ring_is_stalled(txq->ift_br[0])))
3339                         iflib_txq_check_drain(txq, 0);
3340         CTX_LOCK(ctx);
3341         ctx->ifc_flags &= ~IFC_QFLUSH;
3342         CTX_UNLOCK(ctx);
3343
3344         if_qflush(ifp);
3345 }
3346
3347
3348 #define IFCAP_FLAGS (IFCAP_TXCSUM_IPV6 | IFCAP_RXCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
3349                      IFCAP_TSO4 | IFCAP_TSO6 | IFCAP_VLAN_HWTAGGING |   \
3350                      IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO)
3351
3352 static int
3353 iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
3354 {
3355         if_ctx_t ctx = if_getsoftc(ifp);
3356         struct ifreq    *ifr = (struct ifreq *)data;
3357 #if defined(INET) || defined(INET6)
3358         struct ifaddr   *ifa = (struct ifaddr *)data;
3359 #endif
3360         bool            avoid_reset = FALSE;
3361         int             err = 0, reinit = 0, bits;
3362
3363         switch (command) {
3364         case SIOCSIFADDR:
3365 #ifdef INET
3366                 if (ifa->ifa_addr->sa_family == AF_INET)
3367                         avoid_reset = TRUE;
3368 #endif
3369 #ifdef INET6
3370                 if (ifa->ifa_addr->sa_family == AF_INET6)
3371                         avoid_reset = TRUE;
3372 #endif
3373                 /*
3374                 ** Calling init results in link renegotiation,
3375                 ** so we avoid doing it when possible.
3376                 */
3377                 if (avoid_reset) {
3378                         if_setflagbits(ifp, IFF_UP,0);
3379                         if (!(if_getdrvflags(ifp)& IFF_DRV_RUNNING))
3380                                 reinit = 1;
3381 #ifdef INET
3382                         if (!(if_getflags(ifp) & IFF_NOARP))
3383                                 arp_ifinit(ifp, ifa);
3384 #endif
3385                 } else
3386                         err = ether_ioctl(ifp, command, data);
3387                 break;
3388         case SIOCSIFMTU:
3389                 CTX_LOCK(ctx);
3390                 if (ifr->ifr_mtu == if_getmtu(ifp)) {
3391                         CTX_UNLOCK(ctx);
3392                         break;
3393                 }
3394                 bits = if_getdrvflags(ifp);
3395                 /* stop the driver and free any clusters before proceeding */
3396                 iflib_stop(ctx);
3397
3398                 if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
3399                         if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
3400                                 ctx->ifc_flags |= IFC_MULTISEG;
3401                         else
3402                                 ctx->ifc_flags &= ~IFC_MULTISEG;
3403                         err = if_setmtu(ifp, ifr->ifr_mtu);
3404                 }
3405                 iflib_init_locked(ctx);
3406                 if_setdrvflags(ifp, bits);
3407                 CTX_UNLOCK(ctx);
3408                 break;
3409         case SIOCSIFFLAGS:
3410                 CTX_LOCK(ctx);
3411                 if (if_getflags(ifp) & IFF_UP) {
3412                         if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3413                                 if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
3414                                     (IFF_PROMISC | IFF_ALLMULTI)) {
3415                                         err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
3416                                 }
3417                         } else
3418                                 reinit = 1;
3419                 } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3420                         iflib_stop(ctx);
3421                 }
3422                 ctx->ifc_if_flags = if_getflags(ifp);
3423                 CTX_UNLOCK(ctx);
3424                 break;
3425
3426                 break;
3427         case SIOCADDMULTI:
3428         case SIOCDELMULTI:
3429                 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
3430                         CTX_LOCK(ctx);
3431                         IFDI_INTR_DISABLE(ctx);
3432                         IFDI_MULTI_SET(ctx);
3433                         IFDI_INTR_ENABLE(ctx);
3434                         CTX_UNLOCK(ctx);
3435                 }
3436                 break;
3437         case SIOCSIFMEDIA:
3438                 CTX_LOCK(ctx);
3439                 IFDI_MEDIA_SET(ctx);
3440                 CTX_UNLOCK(ctx);
3441                 /* falls thru */
3442         case SIOCGIFMEDIA:
3443                 err = ifmedia_ioctl(ifp, ifr, &ctx->ifc_media, command);
3444                 break;
3445         case SIOCGI2C:
3446         {
3447                 struct ifi2creq i2c;
3448
3449                 err = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
3450                 if (err != 0)
3451                         break;
3452                 if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
3453                         err = EINVAL;
3454                         break;
3455                 }
3456                 if (i2c.len > sizeof(i2c.data)) {
3457                         err = EINVAL;
3458                         break;
3459                 }
3460
3461                 if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
3462                         err = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
3463                 break;
3464         }
3465         case SIOCSIFCAP:
3466         {
3467                 int mask, setmask;
3468
3469                 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3470                 setmask = 0;
3471 #ifdef TCP_OFFLOAD
3472                 setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
3473 #endif
3474                 setmask |= (mask & IFCAP_FLAGS);
3475
3476                 if (setmask  & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
3477                         setmask |= (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
3478                 if ((mask & IFCAP_WOL) &&
3479                     (if_getcapabilities(ifp) & IFCAP_WOL) != 0)
3480                         setmask |= (mask & (IFCAP_WOL_MCAST|IFCAP_WOL_MAGIC));
3481                 if_vlancap(ifp);
3482                 /*
3483                  * want to ensure that traffic has stopped before we change any of the flags
3484                  */
3485                 if (setmask) {
3486                         CTX_LOCK(ctx);
3487                         bits = if_getdrvflags(ifp);
3488                         if (bits & IFF_DRV_RUNNING)
3489                                 iflib_stop(ctx);
3490                         if_togglecapenable(ifp, setmask);
3491                         if (bits & IFF_DRV_RUNNING)
3492                                 iflib_init_locked(ctx);
3493                         if_setdrvflags(ifp, bits);
3494                         CTX_UNLOCK(ctx);
3495                 }
3496                 break;
3497             }
3498         case SIOCGPRIVATE_0:
3499         case SIOCSDRVSPEC:
3500         case SIOCGDRVSPEC:
3501                 CTX_LOCK(ctx);
3502                 err = IFDI_PRIV_IOCTL(ctx, command, data);
3503                 CTX_UNLOCK(ctx);
3504                 break;
3505         default:
3506                 err = ether_ioctl(ifp, command, data);
3507                 break;
3508         }
3509         if (reinit)
3510                 iflib_if_init(ctx);
3511         return (err);
3512 }
3513
3514 static uint64_t
3515 iflib_if_get_counter(if_t ifp, ift_counter cnt)
3516 {
3517         if_ctx_t ctx = if_getsoftc(ifp);
3518
3519         return (IFDI_GET_COUNTER(ctx, cnt));
3520 }
3521
3522 /*********************************************************************
3523  *
3524  *  OTHER FUNCTIONS EXPORTED TO THE STACK
3525  *
3526  **********************************************************************/
3527
3528 static void
3529 iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
3530 {
3531         if_ctx_t ctx = if_getsoftc(ifp);
3532
3533         if ((void *)ctx != arg)
3534                 return;
3535
3536         if ((vtag == 0) || (vtag > 4095))
3537                 return;
3538
3539         CTX_LOCK(ctx);
3540         IFDI_VLAN_REGISTER(ctx, vtag);
3541         /* Re-init to load the changes */
3542         if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
3543                 iflib_init_locked(ctx);
3544         CTX_UNLOCK(ctx);
3545 }
3546
3547 static void
3548 iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
3549 {
3550         if_ctx_t ctx = if_getsoftc(ifp);
3551
3552         if ((void *)ctx != arg)
3553                 return;
3554
3555         if ((vtag == 0) || (vtag > 4095))
3556                 return;
3557
3558         CTX_LOCK(ctx);
3559         IFDI_VLAN_UNREGISTER(ctx, vtag);
3560         /* Re-init to load the changes */
3561         if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
3562                 iflib_init_locked(ctx);
3563         CTX_UNLOCK(ctx);
3564 }
3565
3566 static void
3567 iflib_led_func(void *arg, int onoff)
3568 {
3569         if_ctx_t ctx = arg;
3570
3571         CTX_LOCK(ctx);
3572         IFDI_LED_FUNC(ctx, onoff);
3573         CTX_UNLOCK(ctx);
3574 }
3575
3576 /*********************************************************************
3577  *
3578  *  BUS FUNCTION DEFINITIONS
3579  *
3580  **********************************************************************/
3581
3582 int
3583 iflib_device_probe(device_t dev)
3584 {
3585         pci_vendor_info_t *ent;
3586
3587         uint16_t        pci_vendor_id, pci_device_id;
3588         uint16_t        pci_subvendor_id, pci_subdevice_id;
3589         uint16_t        pci_rev_id;
3590         if_shared_ctx_t sctx;
3591
3592         if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
3593                 return (ENOTSUP);
3594
3595         pci_vendor_id = pci_get_vendor(dev);
3596         pci_device_id = pci_get_device(dev);
3597         pci_subvendor_id = pci_get_subvendor(dev);
3598         pci_subdevice_id = pci_get_subdevice(dev);
3599         pci_rev_id = pci_get_revid(dev);
3600         if (sctx->isc_parse_devinfo != NULL)
3601                 sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
3602
3603         ent = sctx->isc_vendor_info;
3604         while (ent->pvi_vendor_id != 0) {
3605                 if (pci_vendor_id != ent->pvi_vendor_id) {
3606                         ent++;
3607                         continue;
3608                 }
3609                 if ((pci_device_id == ent->pvi_device_id) &&
3610                     ((pci_subvendor_id == ent->pvi_subvendor_id) ||
3611                      (ent->pvi_subvendor_id == 0)) &&
3612                     ((pci_subdevice_id == ent->pvi_subdevice_id) ||
3613                      (ent->pvi_subdevice_id == 0)) &&
3614                     ((pci_rev_id == ent->pvi_rev_id) ||
3615                      (ent->pvi_rev_id == 0))) {
3616
3617                         device_set_desc_copy(dev, ent->pvi_name);
3618                         /* this needs to be changed to zero if the bus probing code
3619                          * ever stops re-probing on best match because the sctx
3620                          * may have its values over written by register calls
3621                          * in subsequent probes
3622                          */
3623                         return (BUS_PROBE_DEFAULT);
3624                 }
3625                 ent++;
3626         }
3627         return (ENXIO);
3628 }
3629
3630 int
3631 iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
3632 {
3633         int err, rid, msix, msix_bar;
3634         if_ctx_t ctx;
3635         if_t ifp;
3636         if_softc_ctx_t scctx;
3637         int i;
3638         uint16_t main_txq;
3639         uint16_t main_rxq;
3640
3641
3642         ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
3643
3644         if (sc == NULL) {
3645                 sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
3646                 device_set_softc(dev, ctx);
3647                 ctx->ifc_flags |= IFC_SC_ALLOCATED;
3648         }
3649
3650         ctx->ifc_sctx = sctx;
3651         ctx->ifc_dev = dev;
3652         ctx->ifc_softc = sc;
3653
3654         if ((err = iflib_register(ctx)) != 0) {
3655                 device_printf(dev, "iflib_register failed %d\n", err);
3656                 return (err);
3657         }
3658         iflib_add_device_sysctl_pre(ctx);
3659
3660         scctx = &ctx->ifc_softc_ctx;
3661         ifp = ctx->ifc_ifp;
3662
3663         /*
3664          * XXX sanity check that ntxd & nrxd are a power of 2
3665          */
3666         if (ctx->ifc_sysctl_ntxqs != 0)
3667                 scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
3668         if (ctx->ifc_sysctl_nrxqs != 0)
3669                 scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
3670
3671         for (i = 0; i < sctx->isc_ntxqs; i++) {
3672                 if (ctx->ifc_sysctl_ntxds[i] != 0)
3673                         scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
3674                 else
3675                         scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
3676         }
3677
3678         for (i = 0; i < sctx->isc_nrxqs; i++) {
3679                 if (ctx->ifc_sysctl_nrxds[i] != 0)
3680                         scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
3681                 else
3682                         scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
3683         }
3684
3685         for (i = 0; i < sctx->isc_nrxqs; i++) {
3686                 if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
3687                         device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
3688                                       i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
3689                         scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
3690                 }
3691                 if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
3692                         device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
3693                                       i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
3694                         scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
3695                 }
3696         }
3697
3698         for (i = 0; i < sctx->isc_ntxqs; i++) {
3699                 if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
3700                         device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
3701                                       i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
3702                         scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
3703                 }
3704                 if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
3705                         device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
3706                                       i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
3707                         scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
3708                 }
3709         }
3710
3711         if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
3712                 device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
3713                 return (err);
3714         }
3715         _iflib_pre_assert(scctx);
3716         ctx->ifc_txrx = *scctx->isc_txrx;
3717
3718 #ifdef INVARIANTS
3719         MPASS(scctx->isc_capenable);
3720         if (scctx->isc_capenable & IFCAP_TXCSUM)
3721                 MPASS(scctx->isc_tx_csum_flags);
3722 #endif
3723
3724         if_setcapabilities(ifp, scctx->isc_capenable);
3725         if_setcapenable(ifp, scctx->isc_capenable);
3726
3727         if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
3728                 scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
3729         if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
3730                 scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
3731
3732 #ifdef ACPI_DMAR
3733         if (dmar_get_dma_tag(device_get_parent(dev), dev) != NULL)
3734                 ctx->ifc_flags |= IFC_DMAR;
3735 #endif
3736
3737         msix_bar = scctx->isc_msix_bar;
3738
3739         if(sctx->isc_flags & IFLIB_HAS_TXCQ)
3740                 main_txq = 1;
3741         else
3742                 main_txq = 0;
3743
3744         if(sctx->isc_flags & IFLIB_HAS_RXCQ)
3745                 main_rxq = 1;
3746         else
3747                 main_rxq = 0;
3748
3749         /* XXX change for per-queue sizes */
3750         device_printf(dev, "using %d tx descriptors and %d rx descriptors\n",
3751                       scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]);
3752         for (i = 0; i < sctx->isc_nrxqs; i++) {
3753                 if (!powerof2(scctx->isc_nrxd[i])) {
3754                         /* round down instead? */
3755                         device_printf(dev, "# rx descriptors must be a power of 2\n");
3756                         err = EINVAL;
3757                         goto fail;
3758                 }
3759         }
3760         for (i = 0; i < sctx->isc_ntxqs; i++) {
3761                 if (!powerof2(scctx->isc_ntxd[i])) {
3762                         device_printf(dev,
3763                             "# tx descriptors must be a power of 2");
3764                         err = EINVAL;
3765                         goto fail;
3766                 }
3767         }
3768
3769         if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] /
3770             MAX_SINGLE_PACKET_FRACTION)
3771                 scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] /
3772                     MAX_SINGLE_PACKET_FRACTION);
3773         if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] /
3774             MAX_SINGLE_PACKET_FRACTION)
3775                 scctx->isc_tx_tso_segments_max = max(1,
3776                     scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION);
3777
3778         /*
3779          * Protect the stack against modern hardware
3780          */
3781         if (scctx->isc_tx_tso_size_max > FREEBSD_TSO_SIZE_MAX)
3782                 scctx->isc_tx_tso_size_max = FREEBSD_TSO_SIZE_MAX;
3783
3784         /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
3785         ifp->if_hw_tsomaxsegcount = scctx->isc_tx_tso_segments_max;
3786         ifp->if_hw_tsomax = scctx->isc_tx_tso_size_max;
3787         ifp->if_hw_tsomaxsegsize = scctx->isc_tx_tso_segsize_max;
3788         if (scctx->isc_rss_table_size == 0)
3789                 scctx->isc_rss_table_size = 64;
3790         scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
3791
3792         GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
3793         /* XXX format name */
3794         taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, -1, "admin");
3795         /*
3796         ** Now setup MSI or MSI/X, should
3797         ** return us the number of supported
3798         ** vectors. (Will be 1 for MSI)
3799         */
3800         if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
3801                 msix = scctx->isc_vectors;
3802         } else if (scctx->isc_msix_bar != 0)
3803                /*
3804                 * The simple fact that isc_msix_bar is not 0 does not mean we
3805                 * we have a good value there that is known to work.
3806                 */
3807                 msix = iflib_msix_init(ctx);
3808         else {
3809                 scctx->isc_vectors = 1;
3810                 scctx->isc_ntxqsets = 1;
3811                 scctx->isc_nrxqsets = 1;
3812                 scctx->isc_intr = IFLIB_INTR_LEGACY;
3813                 msix = 0;
3814         }
3815         /* Get memory for the station queues */
3816         if ((err = iflib_queues_alloc(ctx))) {
3817                 device_printf(dev, "Unable to allocate queue memory\n");
3818                 goto fail;
3819         }
3820
3821         if ((err = iflib_qset_structures_setup(ctx))) {
3822                 device_printf(dev, "qset structure setup failed %d\n", err);
3823                 goto fail_queues;
3824         }
3825
3826         /*
3827          * Group taskqueues aren't properly set up until SMP is started,
3828          * so we disable interrupts until we can handle them post
3829          * SI_SUB_SMP.
3830          *
3831          * XXX: disabling interrupts doesn't actually work, at least for
3832          * the non-MSI case.  When they occur before SI_SUB_SMP completes,
3833          * we do null handling and depend on this not causing too large an
3834          * interrupt storm.
3835          */
3836         IFDI_INTR_DISABLE(ctx);
3837         if (msix > 1 && (err = IFDI_MSIX_INTR_ASSIGN(ctx, msix)) != 0) {
3838                 device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", err);
3839                 goto fail_intr_free;
3840         }
3841         if (msix <= 1) {
3842                 rid = 0;
3843                 if (scctx->isc_intr == IFLIB_INTR_MSI) {
3844                         MPASS(msix == 1);
3845                         rid = 1;
3846                 }
3847                 if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
3848                         device_printf(dev, "iflib_legacy_setup failed %d\n", err);
3849                         goto fail_intr_free;
3850                 }
3851         }
3852         ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac);
3853         if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
3854                 device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
3855                 goto fail_detach;
3856         }
3857         if ((err = iflib_netmap_attach(ctx))) {
3858                 device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
3859                 goto fail_detach;
3860         }
3861         *ctxp = ctx;
3862
3863         if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
3864         iflib_add_device_sysctl_post(ctx);
3865         ctx->ifc_flags |= IFC_INIT_DONE;
3866         return (0);
3867 fail_detach:
3868         ether_ifdetach(ctx->ifc_ifp);
3869 fail_intr_free:
3870         if (scctx->isc_intr == IFLIB_INTR_MSIX || scctx->isc_intr == IFLIB_INTR_MSI)
3871                 pci_release_msi(ctx->ifc_dev);
3872 fail_queues:
3873         /* XXX free queues */
3874 fail:
3875         IFDI_DETACH(ctx);
3876         return (err);
3877 }
3878
3879 int
3880 iflib_device_attach(device_t dev)
3881 {
3882         if_ctx_t ctx;
3883         if_shared_ctx_t sctx;
3884
3885         if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
3886                 return (ENOTSUP);
3887
3888         pci_enable_busmaster(dev);
3889
3890         return (iflib_device_register(dev, NULL, sctx, &ctx));
3891 }
3892
3893 int
3894 iflib_device_deregister(if_ctx_t ctx)
3895 {
3896         if_t ifp = ctx->ifc_ifp;
3897         iflib_txq_t txq;
3898         iflib_rxq_t rxq;
3899         device_t dev = ctx->ifc_dev;
3900         int i;
3901         struct taskqgroup *tqg;
3902
3903         /* Make sure VLANS are not using driver */
3904         if (if_vlantrunkinuse(ifp)) {
3905                 device_printf(dev,"Vlan in use, detach first\n");
3906                 return (EBUSY);
3907         }
3908
3909         CTX_LOCK(ctx);
3910         ctx->ifc_in_detach = 1;
3911         iflib_stop(ctx);
3912         CTX_UNLOCK(ctx);
3913
3914         /* Unregister VLAN events */
3915         if (ctx->ifc_vlan_attach_event != NULL)
3916                 EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
3917         if (ctx->ifc_vlan_detach_event != NULL)
3918                 EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
3919
3920         iflib_netmap_detach(ifp);
3921         ether_ifdetach(ifp);
3922         /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
3923         CTX_LOCK_DESTROY(ctx);
3924         if (ctx->ifc_led_dev != NULL)
3925                 led_destroy(ctx->ifc_led_dev);
3926         /* XXX drain any dependent tasks */
3927         tqg = qgroup_if_io_tqg;
3928         for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
3929                 callout_drain(&txq->ift_timer);
3930                 callout_drain(&txq->ift_db_check);
3931                 if (txq->ift_task.gt_uniq != NULL)
3932                         taskqgroup_detach(tqg, &txq->ift_task);
3933         }
3934         for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
3935                 if (rxq->ifr_task.gt_uniq != NULL)
3936                         taskqgroup_detach(tqg, &rxq->ifr_task);
3937         }
3938         tqg = qgroup_if_config_tqg;
3939         if (ctx->ifc_admin_task.gt_uniq != NULL)
3940                 taskqgroup_detach(tqg, &ctx->ifc_admin_task);
3941         if (ctx->ifc_vflr_task.gt_uniq != NULL)
3942                 taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
3943
3944         IFDI_DETACH(ctx);
3945         device_set_softc(ctx->ifc_dev, NULL);
3946         if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
3947                 pci_release_msi(dev);
3948         }
3949         if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
3950                 iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
3951         }
3952         if (ctx->ifc_msix_mem != NULL) {
3953                 bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
3954                         ctx->ifc_softc_ctx.isc_msix_bar, ctx->ifc_msix_mem);
3955                 ctx->ifc_msix_mem = NULL;
3956         }
3957
3958         bus_generic_detach(dev);
3959         if_free(ifp);
3960
3961         iflib_tx_structures_free(ctx);
3962         iflib_rx_structures_free(ctx);
3963         if (ctx->ifc_flags & IFC_SC_ALLOCATED)
3964                 free(ctx->ifc_softc, M_IFLIB);
3965         free(ctx, M_IFLIB);
3966         return (0);
3967 }
3968
3969
3970 int
3971 iflib_device_detach(device_t dev)
3972 {
3973         if_ctx_t ctx = device_get_softc(dev);
3974
3975         return (iflib_device_deregister(ctx));
3976 }
3977
3978 int
3979 iflib_device_suspend(device_t dev)
3980 {
3981         if_ctx_t ctx = device_get_softc(dev);
3982
3983         CTX_LOCK(ctx);
3984         IFDI_SUSPEND(ctx);
3985         CTX_UNLOCK(ctx);
3986
3987         return bus_generic_suspend(dev);
3988 }
3989 int
3990 iflib_device_shutdown(device_t dev)
3991 {
3992         if_ctx_t ctx = device_get_softc(dev);
3993
3994         CTX_LOCK(ctx);
3995         IFDI_SHUTDOWN(ctx);
3996         CTX_UNLOCK(ctx);
3997
3998         return bus_generic_suspend(dev);
3999 }
4000
4001
4002 int
4003 iflib_device_resume(device_t dev)
4004 {
4005         if_ctx_t ctx = device_get_softc(dev);
4006         iflib_txq_t txq = ctx->ifc_txqs;
4007
4008         CTX_LOCK(ctx);
4009         IFDI_RESUME(ctx);
4010         iflib_init_locked(ctx);
4011         CTX_UNLOCK(ctx);
4012         for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
4013                 iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
4014
4015         return (bus_generic_resume(dev));
4016 }
4017
4018 int
4019 iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
4020 {
4021         int error;
4022         if_ctx_t ctx = device_get_softc(dev);
4023
4024         CTX_LOCK(ctx);
4025         error = IFDI_IOV_INIT(ctx, num_vfs, params);
4026         CTX_UNLOCK(ctx);
4027
4028         return (error);
4029 }
4030
4031 void
4032 iflib_device_iov_uninit(device_t dev)
4033 {
4034         if_ctx_t ctx = device_get_softc(dev);
4035
4036         CTX_LOCK(ctx);
4037         IFDI_IOV_UNINIT(ctx);
4038         CTX_UNLOCK(ctx);
4039 }
4040
4041 int
4042 iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
4043 {
4044         int error;
4045         if_ctx_t ctx = device_get_softc(dev);
4046
4047         CTX_LOCK(ctx);
4048         error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
4049         CTX_UNLOCK(ctx);
4050
4051         return (error);
4052 }
4053
4054 /*********************************************************************
4055  *
4056  *  MODULE FUNCTION DEFINITIONS
4057  *
4058  **********************************************************************/
4059
4060 /*
4061  * - Start a fast taskqueue thread for each core
4062  * - Start a taskqueue for control operations
4063  */
4064 static int
4065 iflib_module_init(void)
4066 {
4067         return (0);
4068 }
4069
4070 static int
4071 iflib_module_event_handler(module_t mod, int what, void *arg)
4072 {
4073         int err;
4074
4075         switch (what) {
4076         case MOD_LOAD:
4077                 if ((err = iflib_module_init()) != 0)
4078                         return (err);
4079                 break;
4080         case MOD_UNLOAD:
4081                 return (EBUSY);
4082         default:
4083                 return (EOPNOTSUPP);
4084         }
4085
4086         return (0);
4087 }
4088
4089 /*********************************************************************
4090  *
4091  *  PUBLIC FUNCTION DEFINITIONS
4092  *     ordered as in iflib.h
4093  *
4094  **********************************************************************/
4095
4096
4097 static void
4098 _iflib_assert(if_shared_ctx_t sctx)
4099 {
4100         MPASS(sctx->isc_tx_maxsize);
4101         MPASS(sctx->isc_tx_maxsegsize);
4102
4103         MPASS(sctx->isc_rx_maxsize);
4104         MPASS(sctx->isc_rx_nsegments);
4105         MPASS(sctx->isc_rx_maxsegsize);
4106
4107         MPASS(sctx->isc_nrxd_min[0]);
4108         MPASS(sctx->isc_nrxd_max[0]);
4109         MPASS(sctx->isc_nrxd_default[0]);
4110         MPASS(sctx->isc_ntxd_min[0]);
4111         MPASS(sctx->isc_ntxd_max[0]);
4112         MPASS(sctx->isc_ntxd_default[0]);
4113 }
4114
4115 static void
4116 _iflib_pre_assert(if_softc_ctx_t scctx)
4117 {
4118
4119         MPASS(scctx->isc_txrx->ift_txd_encap);
4120         MPASS(scctx->isc_txrx->ift_txd_flush);
4121         MPASS(scctx->isc_txrx->ift_txd_credits_update);
4122         MPASS(scctx->isc_txrx->ift_rxd_available);
4123         MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
4124         MPASS(scctx->isc_txrx->ift_rxd_refill);
4125         MPASS(scctx->isc_txrx->ift_rxd_flush);
4126 }
4127
4128 static int
4129 iflib_register(if_ctx_t ctx)
4130 {
4131         if_shared_ctx_t sctx = ctx->ifc_sctx;
4132         driver_t *driver = sctx->isc_driver;
4133         device_t dev = ctx->ifc_dev;
4134         if_t ifp;
4135
4136         _iflib_assert(sctx);
4137
4138         CTX_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
4139
4140         ifp = ctx->ifc_ifp = if_gethandle(IFT_ETHER);
4141         if (ifp == NULL) {
4142                 device_printf(dev, "can not allocate ifnet structure\n");
4143                 return (ENOMEM);
4144         }
4145
4146         /*
4147          * Initialize our context's device specific methods
4148          */
4149         kobj_init((kobj_t) ctx, (kobj_class_t) driver);
4150         kobj_class_compile((kobj_class_t) driver);
4151         driver->refs++;
4152
4153         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
4154         if_setsoftc(ifp, ctx);
4155         if_setdev(ifp, dev);
4156         if_setinitfn(ifp, iflib_if_init);
4157         if_setioctlfn(ifp, iflib_if_ioctl);
4158         if_settransmitfn(ifp, iflib_if_transmit);
4159         if_setqflushfn(ifp, iflib_if_qflush);
4160         if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
4161
4162         ctx->ifc_vlan_attach_event =
4163                 EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
4164                                                           EVENTHANDLER_PRI_FIRST);
4165         ctx->ifc_vlan_detach_event =
4166                 EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
4167                                                           EVENTHANDLER_PRI_FIRST);
4168
4169         ifmedia_init(&ctx->ifc_media, IFM_IMASK,
4170                                          iflib_media_change, iflib_media_status);
4171
4172         return (0);
4173 }
4174
4175
4176 static int
4177 iflib_queues_alloc(if_ctx_t ctx)
4178 {
4179         if_shared_ctx_t sctx = ctx->ifc_sctx;
4180         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
4181         device_t dev = ctx->ifc_dev;
4182         int nrxqsets = scctx->isc_nrxqsets;
4183         int ntxqsets = scctx->isc_ntxqsets;
4184         iflib_txq_t txq;
4185         iflib_rxq_t rxq;
4186         iflib_fl_t fl = NULL;
4187         int i, j, cpu, err, txconf, rxconf;
4188         iflib_dma_info_t ifdip;
4189         uint32_t *rxqsizes = scctx->isc_rxqsizes;
4190         uint32_t *txqsizes = scctx->isc_txqsizes;
4191         uint8_t nrxqs = sctx->isc_nrxqs;
4192         uint8_t ntxqs = sctx->isc_ntxqs;
4193         int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
4194         caddr_t *vaddrs;
4195         uint64_t *paddrs;
4196         struct ifmp_ring **brscp;
4197         int nbuf_rings = 1; /* XXX determine dynamically */
4198
4199         KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
4200         KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
4201
4202         brscp = NULL;
4203         txq = NULL;
4204         rxq = NULL;
4205
4206 /* Allocate the TX ring struct memory */
4207         if (!(txq =
4208             (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
4209             ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
4210                 device_printf(dev, "Unable to allocate TX ring memory\n");
4211                 err = ENOMEM;
4212                 goto fail;
4213         }
4214
4215         /* Now allocate the RX */
4216         if (!(rxq =
4217             (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
4218             nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
4219                 device_printf(dev, "Unable to allocate RX ring memory\n");
4220                 err = ENOMEM;
4221                 goto rx_fail;
4222         }
4223         if (!(brscp = malloc(sizeof(void *) * nbuf_rings * nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
4224                 device_printf(dev, "Unable to buf_ring_sc * memory\n");
4225                 err = ENOMEM;
4226                 goto rx_fail;
4227         }
4228
4229         ctx->ifc_txqs = txq;
4230         ctx->ifc_rxqs = rxq;
4231
4232         /*
4233          * XXX handle allocation failure
4234          */
4235         for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
4236                 /* Set up some basics */
4237
4238                 if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
4239                         device_printf(dev, "failed to allocate iflib_dma_info\n");
4240                         err = ENOMEM;
4241                         goto err_tx_desc;
4242                 }
4243                 txq->ift_ifdi = ifdip;
4244                 for (j = 0; j < ntxqs; j++, ifdip++) {
4245                         if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
4246                                 device_printf(dev, "Unable to allocate Descriptor memory\n");
4247                                 err = ENOMEM;
4248                                 goto err_tx_desc;
4249                         }
4250                         bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
4251                 }
4252                 txq->ift_ctx = ctx;
4253                 txq->ift_id = i;
4254                 if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
4255                         txq->ift_br_offset = 1;
4256                 } else {
4257                         txq->ift_br_offset = 0;
4258                 }
4259                 /* XXX fix this */
4260                 txq->ift_timer.c_cpu = cpu;
4261                 txq->ift_db_check.c_cpu = cpu;
4262                 txq->ift_nbr = nbuf_rings;
4263
4264                 if (iflib_txsd_alloc(txq)) {
4265                         device_printf(dev, "Critical Failure setting up TX buffers\n");
4266                         err = ENOMEM;
4267                         goto err_tx_desc;
4268                 }
4269
4270                 /* Initialize the TX lock */
4271                 snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:tx(%d):callout",
4272                     device_get_nameunit(dev), txq->ift_id);
4273                 mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
4274                 callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
4275                 callout_init_mtx(&txq->ift_db_check, &txq->ift_mtx, 0);
4276
4277                 snprintf(txq->ift_db_mtx_name, MTX_NAME_LEN, "%s:tx(%d):db",
4278                          device_get_nameunit(dev), txq->ift_id);
4279                 TXDB_LOCK_INIT(txq);
4280
4281                 txq->ift_br = brscp + i*nbuf_rings;
4282                 for (j = 0; j < nbuf_rings; j++) {
4283                         err = ifmp_ring_alloc(&txq->ift_br[j], 2048, txq, iflib_txq_drain,
4284                                               iflib_txq_can_drain, M_IFLIB, M_WAITOK);
4285                         if (err) {
4286                                 /* XXX free any allocated rings */
4287                                 device_printf(dev, "Unable to allocate buf_ring\n");
4288                                 goto err_tx_desc;
4289                         }
4290                 }
4291         }
4292
4293         for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
4294                 /* Set up some basics */
4295
4296                 if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, M_IFLIB, M_WAITOK|M_ZERO)) == NULL) {
4297                         device_printf(dev, "failed to allocate iflib_dma_info\n");
4298                         err = ENOMEM;
4299                         goto err_tx_desc;
4300                 }
4301
4302                 rxq->ifr_ifdi = ifdip;
4303                 for (j = 0; j < nrxqs; j++, ifdip++) {
4304                         if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, BUS_DMA_NOWAIT)) {
4305                                 device_printf(dev, "Unable to allocate Descriptor memory\n");
4306                                 err = ENOMEM;
4307                                 goto err_tx_desc;
4308                         }
4309                         bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
4310                 }
4311                 rxq->ifr_ctx = ctx;
4312                 rxq->ifr_id = i;
4313                 if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
4314                         rxq->ifr_fl_offset = 1;
4315                 } else {
4316                         rxq->ifr_fl_offset = 0;
4317                 }
4318                 rxq->ifr_nfl = nfree_lists;
4319                 if (!(fl =
4320                           (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
4321                         device_printf(dev, "Unable to allocate free list memory\n");
4322                         err = ENOMEM;
4323                         goto err_tx_desc;
4324                 }
4325                 rxq->ifr_fl = fl;
4326                 for (j = 0; j < nfree_lists; j++) {
4327                         rxq->ifr_fl[j].ifl_rxq = rxq;
4328                         rxq->ifr_fl[j].ifl_id = j;
4329                         rxq->ifr_fl[j].ifl_ifdi =
4330                             &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
4331                 }
4332         /* Allocate receive buffers for the ring*/
4333                 if (iflib_rxsd_alloc(rxq)) {
4334                         device_printf(dev,
4335                             "Critical Failure setting up receive buffers\n");
4336                         err = ENOMEM;
4337                         goto err_rx_desc;
4338                 }
4339         }
4340
4341         /* TXQs */
4342         vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
4343         paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
4344         for (i = 0; i < ntxqsets; i++) {
4345                 iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
4346
4347                 for (j = 0; j < ntxqs; j++, di++) {
4348                         vaddrs[i*ntxqs + j] = di->idi_vaddr;
4349                         paddrs[i*ntxqs + j] = di->idi_paddr;
4350                 }
4351         }
4352         if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
4353                 device_printf(ctx->ifc_dev, "device queue allocation failed\n");
4354                 iflib_tx_structures_free(ctx);
4355                 free(vaddrs, M_IFLIB);
4356                 free(paddrs, M_IFLIB);
4357                 goto err_rx_desc;
4358         }
4359         free(vaddrs, M_IFLIB);
4360         free(paddrs, M_IFLIB);
4361
4362         /* RXQs */
4363         vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
4364         paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
4365         for (i = 0; i < nrxqsets; i++) {
4366                 iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
4367
4368                 for (j = 0; j < nrxqs; j++, di++) {
4369                         vaddrs[i*nrxqs + j] = di->idi_vaddr;
4370                         paddrs[i*nrxqs + j] = di->idi_paddr;
4371                 }
4372         }
4373         if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
4374                 device_printf(ctx->ifc_dev, "device queue allocation failed\n");
4375                 iflib_tx_structures_free(ctx);
4376                 free(vaddrs, M_IFLIB);
4377                 free(paddrs, M_IFLIB);
4378                 goto err_rx_desc;
4379         }
4380         free(vaddrs, M_IFLIB);
4381         free(paddrs, M_IFLIB);
4382
4383         return (0);
4384
4385 /* XXX handle allocation failure changes */
4386 err_rx_desc:
4387 err_tx_desc:
4388         if (ctx->ifc_rxqs != NULL)
4389                 free(ctx->ifc_rxqs, M_IFLIB);
4390         ctx->ifc_rxqs = NULL;
4391         if (ctx->ifc_txqs != NULL)
4392                 free(ctx->ifc_txqs, M_IFLIB);
4393         ctx->ifc_txqs = NULL;
4394 rx_fail:
4395         if (brscp != NULL)
4396                 free(brscp, M_IFLIB);
4397         if (rxq != NULL)
4398                 free(rxq, M_IFLIB);
4399         if (txq != NULL)
4400                 free(txq, M_IFLIB);
4401 fail:
4402         return (err);
4403 }
4404
4405 static int
4406 iflib_tx_structures_setup(if_ctx_t ctx)
4407 {
4408         iflib_txq_t txq = ctx->ifc_txqs;
4409         int i;
4410
4411         for (i = 0; i < NTXQSETS(ctx); i++, txq++)
4412                 iflib_txq_setup(txq);
4413
4414         return (0);
4415 }
4416
4417 static void
4418 iflib_tx_structures_free(if_ctx_t ctx)
4419 {
4420         iflib_txq_t txq = ctx->ifc_txqs;
4421         int i, j;
4422
4423         for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
4424                 iflib_txq_destroy(txq);
4425                 for (j = 0; j < ctx->ifc_nhwtxqs; j++)
4426                         iflib_dma_free(&txq->ift_ifdi[j]);
4427         }
4428         free(ctx->ifc_txqs, M_IFLIB);
4429         ctx->ifc_txqs = NULL;
4430         IFDI_QUEUES_FREE(ctx);
4431 }
4432
4433 /*********************************************************************
4434  *
4435  *  Initialize all receive rings.
4436  *
4437  **********************************************************************/
4438 static int
4439 iflib_rx_structures_setup(if_ctx_t ctx)
4440 {
4441         iflib_rxq_t rxq = ctx->ifc_rxqs;
4442         int q;
4443 #if defined(INET6) || defined(INET)
4444         int i, err;
4445 #endif
4446
4447         for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
4448 #if defined(INET6) || defined(INET)
4449                 tcp_lro_free(&rxq->ifr_lc);
4450                 if ((err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
4451                     TCP_LRO_ENTRIES, min(1024,
4452                     ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]))) != 0) {
4453                         device_printf(ctx->ifc_dev, "LRO Initialization failed!\n");
4454                         goto fail;
4455                 }
4456                 rxq->ifr_lro_enabled = TRUE;
4457 #endif
4458                 IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
4459         }
4460         return (0);
4461 #if defined(INET6) || defined(INET)
4462 fail:
4463         /*
4464          * Free RX software descriptors allocated so far, we will only handle
4465          * the rings that completed, the failing case will have
4466          * cleaned up for itself. 'q' failed, so its the terminus.
4467          */
4468         rxq = ctx->ifc_rxqs;
4469         for (i = 0; i < q; ++i, rxq++) {
4470                 iflib_rx_sds_free(rxq);
4471                 rxq->ifr_cq_gen = rxq->ifr_cq_cidx = rxq->ifr_cq_pidx = 0;
4472         }
4473         return (err);
4474 #endif
4475 }
4476
4477 /*********************************************************************
4478  *
4479  *  Free all receive rings.
4480  *
4481  **********************************************************************/
4482 static void
4483 iflib_rx_structures_free(if_ctx_t ctx)
4484 {
4485         iflib_rxq_t rxq = ctx->ifc_rxqs;
4486
4487         for (int i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
4488                 iflib_rx_sds_free(rxq);
4489         }
4490 }
4491
4492 static int
4493 iflib_qset_structures_setup(if_ctx_t ctx)
4494 {
4495         int err;
4496
4497         if ((err = iflib_tx_structures_setup(ctx)) != 0)
4498                 return (err);
4499
4500         if ((err = iflib_rx_structures_setup(ctx)) != 0) {
4501                 device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
4502                 iflib_tx_structures_free(ctx);
4503                 iflib_rx_structures_free(ctx);
4504         }
4505         return (err);
4506 }
4507
4508 int
4509 iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
4510                                 driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, char *name)
4511 {
4512
4513         return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
4514 }
4515
4516 static int
4517 find_nth(if_ctx_t ctx, cpuset_t *cpus, int qid)
4518 {
4519         int i, cpuid, eqid, count;
4520
4521         CPU_COPY(&ctx->ifc_cpus, cpus);
4522         count = CPU_COUNT(&ctx->ifc_cpus);
4523         eqid = qid % count;
4524         /* clear up to the qid'th bit */
4525         for (i = 0; i < eqid; i++) {
4526                 cpuid = CPU_FFS(cpus);
4527                 MPASS(cpuid != 0);
4528                 CPU_CLR(cpuid-1, cpus);
4529         }
4530         cpuid = CPU_FFS(cpus);
4531         MPASS(cpuid != 0);
4532         return (cpuid-1);
4533 }
4534
4535 int
4536 iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
4537                                                 iflib_intr_type_t type, driver_filter_t *filter,
4538                                                 void *filter_arg, int qid, char *name)
4539 {
4540         struct grouptask *gtask;
4541         struct taskqgroup *tqg;
4542         iflib_filter_info_t info;
4543         cpuset_t cpus;
4544         gtask_fn_t *fn;
4545         int tqrid, err, cpuid;
4546         void *q;
4547
4548         info = &ctx->ifc_filter_info;
4549         tqrid = rid;
4550
4551         switch (type) {
4552         /* XXX merge tx/rx for netmap? */
4553         case IFLIB_INTR_TX:
4554                 q = &ctx->ifc_txqs[qid];
4555                 info = &ctx->ifc_txqs[qid].ift_filter_info;
4556                 gtask = &ctx->ifc_txqs[qid].ift_task;
4557                 tqg = qgroup_if_io_tqg;
4558                 fn = _task_fn_tx;
4559                 GROUPTASK_INIT(gtask, 0, fn, q);
4560                 break;
4561         case IFLIB_INTR_RX:
4562                 q = &ctx->ifc_rxqs[qid];
4563                 info = &ctx->ifc_rxqs[qid].ifr_filter_info;
4564                 gtask = &ctx->ifc_rxqs[qid].ifr_task;
4565                 tqg = qgroup_if_io_tqg;
4566                 fn = _task_fn_rx;
4567                 GROUPTASK_INIT(gtask, 0, fn, q);
4568                 break;
4569         case IFLIB_INTR_ADMIN:
4570                 q = ctx;
4571                 tqrid = -1;
4572                 info = &ctx->ifc_filter_info;
4573                 gtask = &ctx->ifc_admin_task;
4574                 tqg = qgroup_if_config_tqg;
4575                 fn = _task_fn_admin;
4576                 break;
4577         default:
4578                 panic("unknown net intr type");
4579         }
4580
4581         info->ifi_filter = filter;
4582         info->ifi_filter_arg = filter_arg;
4583         info->ifi_task = gtask;
4584         info->ifi_ctx = ctx;
4585
4586         err = _iflib_irq_alloc(ctx, irq, rid, iflib_fast_intr, NULL, info,  name);
4587         if (err != 0) {
4588                 device_printf(ctx->ifc_dev, "_iflib_irq_alloc failed %d\n", err);
4589                 return (err);
4590         }
4591         if (type == IFLIB_INTR_ADMIN)
4592                 return (0);
4593
4594         if (tqrid != -1) {
4595                 cpuid = find_nth(ctx, &cpus, qid);
4596                 taskqgroup_attach_cpu(tqg, gtask, q, cpuid, irq->ii_rid, name);
4597         } else {
4598                 taskqgroup_attach(tqg, gtask, q, tqrid, name);
4599         }
4600
4601         return (0);
4602 }
4603
4604 void
4605 iflib_softirq_alloc_generic(if_ctx_t ctx, int rid, iflib_intr_type_t type,  void *arg, int qid, char *name)
4606 {
4607         struct grouptask *gtask;
4608         struct taskqgroup *tqg;
4609         gtask_fn_t *fn;
4610         void *q;
4611
4612         switch (type) {
4613         case IFLIB_INTR_TX:
4614                 q = &ctx->ifc_txqs[qid];
4615                 gtask = &ctx->ifc_txqs[qid].ift_task;
4616                 tqg = qgroup_if_io_tqg;
4617                 fn = _task_fn_tx;
4618                 break;
4619         case IFLIB_INTR_RX:
4620                 q = &ctx->ifc_rxqs[qid];
4621                 gtask = &ctx->ifc_rxqs[qid].ifr_task;
4622                 tqg = qgroup_if_io_tqg;
4623                 fn = _task_fn_rx;
4624                 break;
4625         case IFLIB_INTR_IOV:
4626                 q = ctx;
4627                 gtask = &ctx->ifc_vflr_task;
4628                 tqg = qgroup_if_config_tqg;
4629                 rid = -1;
4630                 fn = _task_fn_iov;
4631                 break;
4632         default:
4633                 panic("unknown net intr type");
4634         }
4635         GROUPTASK_INIT(gtask, 0, fn, q);
4636         taskqgroup_attach(tqg, gtask, q, rid, name);
4637 }
4638
4639 void
4640 iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
4641 {
4642         if (irq->ii_tag)
4643                 bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
4644
4645         if (irq->ii_res)
4646                 bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, irq->ii_rid, irq->ii_res);
4647 }
4648
4649 static int
4650 iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, char *name)
4651 {
4652         iflib_txq_t txq = ctx->ifc_txqs;
4653         iflib_rxq_t rxq = ctx->ifc_rxqs;
4654         if_irq_t irq = &ctx->ifc_legacy_irq;
4655         iflib_filter_info_t info;
4656         struct grouptask *gtask;
4657         struct taskqgroup *tqg;
4658         gtask_fn_t *fn;
4659         int tqrid;
4660         void *q;
4661         int err;
4662
4663         q = &ctx->ifc_rxqs[0];
4664         info = &rxq[0].ifr_filter_info;
4665         gtask = &rxq[0].ifr_task;
4666         tqg = qgroup_if_io_tqg;
4667         tqrid = irq->ii_rid = *rid;
4668         fn = _task_fn_rx;
4669
4670         ctx->ifc_flags |= IFC_LEGACY;
4671         info->ifi_filter = filter;
4672         info->ifi_filter_arg = filter_arg;
4673         info->ifi_task = gtask;
4674         info->ifi_ctx = ctx;
4675
4676         /* We allocate a single interrupt resource */
4677         if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr, NULL, info, name)) != 0)
4678                 return (err);
4679         GROUPTASK_INIT(gtask, 0, fn, q);
4680         taskqgroup_attach(tqg, gtask, q, tqrid, name);
4681
4682         GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
4683         taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, tqrid, "tx");
4684         return (0);
4685 }
4686
4687 void
4688 iflib_led_create(if_ctx_t ctx)
4689 {
4690
4691         ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
4692                                                                   device_get_nameunit(ctx->ifc_dev));
4693 }
4694
4695 void
4696 iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
4697 {
4698
4699         GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
4700 }
4701
4702 void
4703 iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
4704 {
4705
4706         GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
4707 }
4708
4709 void
4710 iflib_admin_intr_deferred(if_ctx_t ctx)
4711 {
4712 #ifdef INVARIANTS
4713         struct grouptask *gtask;
4714
4715         gtask = &ctx->ifc_admin_task;
4716         MPASS(gtask->gt_taskqueue != NULL);
4717 #endif
4718
4719         GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
4720 }
4721
4722 void
4723 iflib_iov_intr_deferred(if_ctx_t ctx)
4724 {
4725
4726         GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
4727 }
4728
4729 void
4730 iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, char *name)
4731 {
4732
4733         taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, -1, name);
4734 }
4735
4736 void
4737 iflib_config_gtask_init(if_ctx_t ctx, struct grouptask *gtask, gtask_fn_t *fn,
4738         char *name)
4739 {
4740
4741         GROUPTASK_INIT(gtask, 0, fn, ctx);
4742         taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, -1, name);
4743 }
4744
4745 void
4746 iflib_config_gtask_deinit(struct grouptask *gtask)
4747 {
4748
4749         taskqgroup_detach(qgroup_if_config_tqg, gtask); 
4750 }
4751
4752 void
4753 iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
4754 {
4755         if_t ifp = ctx->ifc_ifp;
4756         iflib_txq_t txq = ctx->ifc_txqs;
4757
4758         if_setbaudrate(ifp, baudrate);
4759
4760         /* If link down, disable watchdog */
4761         if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
4762                 for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
4763                         txq->ift_qstatus = IFLIB_QUEUE_IDLE;
4764         }
4765         ctx->ifc_link_state = link_state;
4766         if_link_state_change(ifp, link_state);
4767 }
4768
4769 static int
4770 iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
4771 {
4772         int credits;
4773 #ifdef INVARIANTS
4774         int credits_pre = txq->ift_cidx_processed;
4775 #endif  
4776
4777         if (ctx->isc_txd_credits_update == NULL)
4778                 return (0);
4779
4780         if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, txq->ift_cidx_processed, true)) == 0)
4781                 return (0);
4782
4783         txq->ift_processed += credits;
4784         txq->ift_cidx_processed += credits;
4785
4786         MPASS(credits_pre + credits == txq->ift_cidx_processed);
4787         if (txq->ift_cidx_processed >= txq->ift_size)
4788                 txq->ift_cidx_processed -= txq->ift_size;
4789         return (credits);
4790 }
4791
4792 static int
4793 iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, int cidx, int budget)
4794 {
4795
4796         return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
4797             budget));
4798 }
4799
4800 void
4801 iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
4802         const char *description, if_int_delay_info_t info,
4803         int offset, int value)
4804 {
4805         info->iidi_ctx = ctx;
4806         info->iidi_offset = offset;
4807         info->iidi_value = value;
4808         SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
4809             SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
4810             OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW,
4811             info, 0, iflib_sysctl_int_delay, "I", description);
4812 }
4813
4814 struct mtx *
4815 iflib_ctx_lock_get(if_ctx_t ctx)
4816 {
4817
4818         return (&ctx->ifc_mtx);
4819 }
4820
4821 static int
4822 iflib_msix_init(if_ctx_t ctx)
4823 {
4824         device_t dev = ctx->ifc_dev;
4825         if_shared_ctx_t sctx = ctx->ifc_sctx;
4826         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
4827         int vectors, queues, rx_queues, tx_queues, queuemsgs, msgs;
4828         int iflib_num_tx_queues, iflib_num_rx_queues;
4829         int err, admincnt, bar;
4830
4831         iflib_num_tx_queues = scctx->isc_ntxqsets;
4832         iflib_num_rx_queues = scctx->isc_nrxqsets;
4833
4834         device_printf(dev, "msix_init qsets capped at %d\n", iflib_num_tx_queues);
4835         
4836         bar = ctx->ifc_softc_ctx.isc_msix_bar;
4837         admincnt = sctx->isc_admin_intrcnt;
4838         /* Override by tuneable */
4839         if (enable_msix == 0)
4840                 goto msi;
4841
4842         /*
4843         ** When used in a virtualized environment
4844         ** PCI BUSMASTER capability may not be set
4845         ** so explicity set it here and rewrite
4846         ** the ENABLE in the MSIX control register
4847         ** at this point to cause the host to
4848         ** successfully initialize us.
4849         */
4850         {
4851                 int msix_ctrl, rid;
4852
4853                 pci_enable_busmaster(dev);
4854                 rid = 0;
4855                 if (pci_find_cap(dev, PCIY_MSIX, &rid) == 0 && rid != 0) {
4856                         rid += PCIR_MSIX_CTRL;
4857                         msix_ctrl = pci_read_config(dev, rid, 2);
4858                         msix_ctrl |= PCIM_MSIXCTRL_MSIX_ENABLE;
4859                         pci_write_config(dev, rid, msix_ctrl, 2);
4860                 } else {
4861                         device_printf(dev, "PCIY_MSIX capability not found; "
4862                                            "or rid %d == 0.\n", rid);
4863                         goto msi;
4864                 }
4865         }
4866
4867         /*
4868          * bar == -1 => "trust me I know what I'm doing"
4869          * https://www.youtube.com/watch?v=nnwWKkNau4I
4870          * Some drivers are for hardware that is so shoddily
4871          * documented that no one knows which bars are which
4872          * so the developer has to map all bars. This hack
4873          * allows shoddy garbage to use msix in this framework.
4874          */
4875         if (bar != -1) {
4876                 ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
4877                     SYS_RES_MEMORY, &bar, RF_ACTIVE);
4878                 if (ctx->ifc_msix_mem == NULL) {
4879                         /* May not be enabled */
4880                         device_printf(dev, "Unable to map MSIX table \n");
4881                         goto msi;
4882                 }
4883         }
4884         /* First try MSI/X */
4885         if ((msgs = pci_msix_count(dev)) == 0) { /* system has msix disabled */
4886                 device_printf(dev, "System has MSIX disabled \n");
4887                 bus_release_resource(dev, SYS_RES_MEMORY,
4888                     bar, ctx->ifc_msix_mem);
4889                 ctx->ifc_msix_mem = NULL;
4890                 goto msi;
4891         }
4892 #if IFLIB_DEBUG
4893         /* use only 1 qset in debug mode */
4894         queuemsgs = min(msgs - admincnt, 1);
4895 #else
4896         queuemsgs = msgs - admincnt;
4897 #endif
4898         if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) == 0) {
4899 #ifdef RSS
4900                 queues = imin(queuemsgs, rss_getnumbuckets());
4901 #else
4902                 queues = queuemsgs;
4903 #endif
4904                 queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
4905                 device_printf(dev, "pxm cpus: %d queue msgs: %d admincnt: %d\n",
4906                                           CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
4907         } else {
4908                 device_printf(dev, "Unable to fetch CPU list\n");
4909                 /* Figure out a reasonable auto config value */
4910                 queues = min(queuemsgs, mp_ncpus);
4911         }
4912 #ifdef  RSS
4913         /* If we're doing RSS, clamp at the number of RSS buckets */
4914         if (queues > rss_getnumbuckets())
4915                 queues = rss_getnumbuckets();
4916 #endif
4917         if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
4918                 rx_queues = iflib_num_rx_queues;
4919         else
4920                 rx_queues = queues;
4921         /*
4922          * We want this to be all logical CPUs by default
4923          */
4924         if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
4925                 tx_queues = iflib_num_tx_queues;
4926         else
4927                 tx_queues = mp_ncpus;
4928
4929         if (ctx->ifc_sysctl_qs_eq_override == 0) {
4930 #ifdef INVARIANTS
4931                 if (tx_queues != rx_queues)
4932                         device_printf(dev, "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
4933                                       min(rx_queues, tx_queues), min(rx_queues, tx_queues));
4934 #endif
4935                 tx_queues = min(rx_queues, tx_queues);
4936                 rx_queues = min(rx_queues, tx_queues);
4937         }
4938
4939         device_printf(dev, "using %d rx queues %d tx queues \n", rx_queues, tx_queues);
4940
4941         vectors = rx_queues + admincnt;
4942         if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
4943                 device_printf(dev,
4944                                           "Using MSIX interrupts with %d vectors\n", vectors);
4945                 scctx->isc_vectors = vectors;
4946                 scctx->isc_nrxqsets = rx_queues;
4947                 scctx->isc_ntxqsets = tx_queues;
4948                 scctx->isc_intr = IFLIB_INTR_MSIX;
4949
4950                 return (vectors);
4951         } else {
4952                 device_printf(dev, "failed to allocate %d msix vectors, err: %d - using MSI\n", vectors, err);
4953         }
4954 msi:
4955         vectors = pci_msi_count(dev);
4956         scctx->isc_nrxqsets = 1;
4957         scctx->isc_ntxqsets = 1;
4958         scctx->isc_vectors = vectors;
4959         if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
4960                 device_printf(dev,"Using an MSI interrupt\n");
4961                 scctx->isc_intr = IFLIB_INTR_MSI;
4962         } else {
4963                 device_printf(dev,"Using a Legacy interrupt\n");
4964                 scctx->isc_intr = IFLIB_INTR_LEGACY;
4965         }
4966
4967         return (vectors);
4968 }
4969
4970 char * ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
4971
4972 static int
4973 mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
4974 {
4975         int rc;
4976         uint16_t *state = ((uint16_t *)oidp->oid_arg1);
4977         struct sbuf *sb;
4978         char *ring_state = "UNKNOWN";
4979
4980         /* XXX needed ? */
4981         rc = sysctl_wire_old_buffer(req, 0);
4982         MPASS(rc == 0);
4983         if (rc != 0)
4984                 return (rc);
4985         sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
4986         MPASS(sb != NULL);
4987         if (sb == NULL)
4988                 return (ENOMEM);
4989         if (state[3] <= 3)
4990                 ring_state = ring_states[state[3]];
4991
4992         sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
4993                     state[0], state[1], state[2], ring_state);
4994         rc = sbuf_finish(sb);
4995         sbuf_delete(sb);
4996         return(rc);
4997 }
4998
4999 enum iflib_ndesc_handler {
5000         IFLIB_NTXD_HANDLER,
5001         IFLIB_NRXD_HANDLER,
5002 };
5003
5004 static int
5005 mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
5006 {
5007         if_ctx_t ctx = (void *)arg1;
5008         enum iflib_ndesc_handler type = arg2;
5009         char buf[256] = {0};
5010         uint16_t *ndesc;
5011         char *p, *next;
5012         int nqs, rc, i;
5013
5014         MPASS(type == IFLIB_NTXD_HANDLER || type == IFLIB_NRXD_HANDLER);
5015
5016         nqs = 8;
5017         switch(type) {
5018         case IFLIB_NTXD_HANDLER:
5019                 ndesc = ctx->ifc_sysctl_ntxds;
5020                 if (ctx->ifc_sctx)
5021                         nqs = ctx->ifc_sctx->isc_ntxqs;
5022                 break;
5023         case IFLIB_NRXD_HANDLER:
5024                 ndesc = ctx->ifc_sysctl_nrxds;
5025                 if (ctx->ifc_sctx)
5026                         nqs = ctx->ifc_sctx->isc_nrxqs;
5027                 break;
5028         }
5029         if (nqs == 0)
5030                 nqs = 8;
5031
5032         for (i=0; i<8; i++) {
5033                 if (i >= nqs)
5034                         break;
5035                 if (i)
5036                         strcat(buf, ",");
5037                 sprintf(strchr(buf, 0), "%d", ndesc[i]);
5038         }
5039
5040         rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
5041         if (rc || req->newptr == NULL)
5042                 return rc;
5043
5044         for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
5045             i++, p = strsep(&next, " ,")) {
5046                 ndesc[i] = strtoul(p, NULL, 10);
5047         }
5048
5049         return(rc);
5050 }
5051
5052 #define NAME_BUFLEN 32
5053 static void
5054 iflib_add_device_sysctl_pre(if_ctx_t ctx)
5055 {
5056         device_t dev = iflib_get_dev(ctx);
5057         struct sysctl_oid_list *child, *oid_list;
5058         struct sysctl_ctx_list *ctx_list;
5059         struct sysctl_oid *node;
5060
5061         ctx_list = device_get_sysctl_ctx(dev);
5062         child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
5063         ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib",
5064                                                       CTLFLAG_RD, NULL, "IFLIB fields");
5065         oid_list = SYSCTL_CHILDREN(node);
5066
5067         SYSCTL_ADD_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
5068                        CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 0,
5069                        "driver version");
5070
5071         SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
5072                        CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
5073                         "# of txqs to use, 0 => use default #");
5074         SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
5075                        CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
5076                         "# of rxqs to use, 0 => use default #");
5077         SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
5078                        CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
5079                        "permit #txq != #rxq");
5080
5081         /* XXX change for per-queue sizes */
5082         SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
5083                        CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER,
5084                        mp_ndesc_handler, "A",
5085                        "list of # of tx descriptors to use, 0 = use default #");
5086         SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
5087                        CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER,
5088                        mp_ndesc_handler, "A",
5089                        "list of # of rx descriptors to use, 0 = use default #");
5090 }
5091
5092 static void
5093 iflib_add_device_sysctl_post(if_ctx_t ctx)
5094 {
5095         if_shared_ctx_t sctx = ctx->ifc_sctx;
5096         if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
5097         device_t dev = iflib_get_dev(ctx);
5098         struct sysctl_oid_list *child;
5099         struct sysctl_ctx_list *ctx_list;
5100         iflib_fl_t fl;
5101         iflib_txq_t txq;
5102         iflib_rxq_t rxq;
5103         int i, j;
5104         char namebuf[NAME_BUFLEN];
5105         char *qfmt;
5106         struct sysctl_oid *queue_node, *fl_node, *node;
5107         struct sysctl_oid_list *queue_list, *fl_list;
5108         ctx_list = device_get_sysctl_ctx(dev);
5109
5110         node = ctx->ifc_sysctl_node;
5111         child = SYSCTL_CHILDREN(node);
5112
5113         if (scctx->isc_ntxqsets > 100)
5114                 qfmt = "txq%03d";
5115         else if (scctx->isc_ntxqsets > 10)
5116                 qfmt = "txq%02d";
5117         else
5118                 qfmt = "txq%d";
5119         for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
5120                 snprintf(namebuf, NAME_BUFLEN, qfmt, i);
5121                 queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
5122                                              CTLFLAG_RD, NULL, "Queue Name");
5123                 queue_list = SYSCTL_CHILDREN(queue_node);
5124 #if MEMORY_LOGGING
5125                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
5126                                 CTLFLAG_RD,
5127                                 &txq->ift_dequeued, "total mbufs freed");
5128                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
5129                                 CTLFLAG_RD,
5130                                 &txq->ift_enqueued, "total mbufs enqueued");
5131 #endif
5132                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
5133                                    CTLFLAG_RD,
5134                                    &txq->ift_mbuf_defrag, "# of times m_defrag was called");
5135                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
5136                                    CTLFLAG_RD,
5137                                    &txq->ift_pullups, "# of times m_pullup was called");
5138                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed",
5139                                    CTLFLAG_RD,
5140                                    &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
5141                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail",
5142                                    CTLFLAG_RD,
5143                                    &txq->ift_no_desc_avail, "# of times no descriptors were available");
5144                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed",
5145                                    CTLFLAG_RD,
5146                                    &txq->ift_map_failed, "# of times dma map failed");
5147                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig",
5148                                    CTLFLAG_RD,
5149                                    &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG");
5150                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup",
5151                                    CTLFLAG_RD,
5152                                    &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG");
5153                 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
5154                                    CTLFLAG_RD,
5155                                    &txq->ift_pidx, 1, "Producer Index");
5156                 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
5157                                    CTLFLAG_RD,
5158                                    &txq->ift_cidx, 1, "Consumer Index");
5159                 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed",
5160                                    CTLFLAG_RD,
5161                                    &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update");
5162                 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
5163                                    CTLFLAG_RD,
5164                                    &txq->ift_in_use, 1, "descriptors in use");
5165                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed",
5166                                    CTLFLAG_RD,
5167                                    &txq->ift_processed, "descriptors procesed for clean");
5168                 SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
5169                                    CTLFLAG_RD,
5170                                    &txq->ift_cleaned, "total cleaned");
5171                 SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
5172                                 CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br[0]->state),
5173                                 0, mp_ring_state_handler, "A", "soft ring state");
5174                 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues",
5175                                        CTLFLAG_RD, &txq->ift_br[0]->enqueues,
5176                                        "# of enqueues to the mp_ring for this queue");
5177                 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops",
5178                                        CTLFLAG_RD, &txq->ift_br[0]->drops,
5179                                        "# of drops in the mp_ring for this queue");
5180                 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts",
5181                                        CTLFLAG_RD, &txq->ift_br[0]->starts,
5182                                        "# of normal consumer starts in the mp_ring for this queue");
5183                 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls",
5184                                        CTLFLAG_RD, &txq->ift_br[0]->stalls,
5185                                                "# of consumer stalls in the mp_ring for this queue");
5186                 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts",
5187                                CTLFLAG_RD, &txq->ift_br[0]->restarts,
5188                                        "# of consumer restarts in the mp_ring for this queue");
5189                 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications",
5190                                        CTLFLAG_RD, &txq->ift_br[0]->abdications,
5191                                        "# of consumer abdications in the mp_ring for this queue");
5192         }
5193
5194         if (scctx->isc_nrxqsets > 100)
5195                 qfmt = "rxq%03d";
5196         else if (scctx->isc_nrxqsets > 10)
5197                 qfmt = "rxq%02d";
5198         else
5199                 qfmt = "rxq%d";
5200         for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
5201                 snprintf(namebuf, NAME_BUFLEN, qfmt, i);
5202                 queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
5203                                              CTLFLAG_RD, NULL, "Queue Name");
5204                 queue_list = SYSCTL_CHILDREN(queue_node);
5205                 if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
5206                         SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_pidx",
5207                                        CTLFLAG_RD,
5208                                        &rxq->ifr_cq_pidx, 1, "Producer Index");
5209                         SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx",
5210                                        CTLFLAG_RD,
5211                                        &rxq->ifr_cq_cidx, 1, "Consumer Index");
5212                 }
5213
5214                 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
5215                         snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
5216                         fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf,
5217                                                      CTLFLAG_RD, NULL, "freelist Name");
5218                         fl_list = SYSCTL_CHILDREN(fl_node);
5219                         SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
5220                                        CTLFLAG_RD,
5221                                        &fl->ifl_pidx, 1, "Producer Index");
5222                         SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
5223                                        CTLFLAG_RD,
5224                                        &fl->ifl_cidx, 1, "Consumer Index");
5225                         SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
5226                                        CTLFLAG_RD,
5227                                        &fl->ifl_credits, 1, "credits available");
5228 #if MEMORY_LOGGING
5229                         SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued",
5230                                         CTLFLAG_RD,
5231                                         &fl->ifl_m_enqueued, "mbufs allocated");
5232                         SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued",
5233                                         CTLFLAG_RD,
5234                                         &fl->ifl_m_dequeued, "mbufs freed");
5235                         SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued",
5236                                         CTLFLAG_RD,
5237                                         &fl->ifl_cl_enqueued, "clusters allocated");
5238                         SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued",
5239                                         CTLFLAG_RD,
5240                                         &fl->ifl_cl_dequeued, "clusters freed");
5241 #endif
5242
5243                 }
5244         }
5245
5246 }