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