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 goto err; 2959 rx_pkts += 1; 2960 rx_bytes += ri.iri_len; 2961 if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 2962 *cidxp = ri.iri_cidx; 2963 /* Update our consumer index */ 2964 /* XXX NB: shurd - check if this is still safe */ 2965 while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) 2966 rxq->ifr_cq_cidx -= scctx->isc_nrxd[0]; 2967 /* was this only a completion queue message? */ 2968 if (__predict_false(ri.iri_nfrags == 0)) 2969 continue; 2970 } 2971 MPASS(ri.iri_nfrags != 0); 2972 MPASS(ri.iri_len != 0); 2973 2974 /* will advance the cidx on the corresponding free lists */ 2975 m = iflib_rxd_pkt_get(rxq, &ri); 2976 avail--; 2977 budget_left--; 2978 if (avail == 0 && budget_left) 2979 avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left); 2980 2981 if (__predict_false(m == NULL)) 2982 continue; 2983 2984 #ifndef __NO_STRICT_ALIGNMENT 2985 if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL) 2986 continue; 2987 #endif 2988 #if defined(INET6) || defined(INET) 2989 if (lro_enabled) { 2990 tcp_lro_queue_mbuf(&rxq->ifr_lc, m); 2991 continue; 2992 } 2993 #endif 2994 2995 if (mh == NULL) 2996 mh = mt = m; 2997 else { 2998 mt->m_nextpkt = m; 2999 mt = m; 3000 } 3001 } 3002 CURVNET_RESTORE(); 3003 /* make sure that we can refill faster than drain */ 3004 for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 3005 retval |= iflib_fl_refill_all(ctx, fl); 3006 3007 if (mh != NULL) { 3008 if_input(ifp, mh); 3009 DBG_COUNTER_INC(rx_if_input); 3010 } 3011 3012 if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes); 3013 if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts); 3014 3015 /* 3016 * Flush any outstanding LRO work 3017 */ 3018 #if defined(INET6) || defined(INET) 3019 tcp_lro_flush_all(&rxq->ifr_lc); 3020 #endif 3021 if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0) 3022 retval |= IFLIB_RXEOF_MORE; 3023 return (retval); 3024 err: 3025 STATE_LOCK(ctx); 3026 ctx->ifc_flags |= IFC_DO_RESET; 3027 iflib_admin_intr_deferred(ctx); 3028 STATE_UNLOCK(ctx); 3029 return (0); 3030 } 3031 3032 #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq) - 1) 3033 static inline qidx_t 3034 txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use) 3035 { 3036 qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 3037 qidx_t minthresh = txq->ift_size / 8; 3038 if (in_use > 4 * minthresh) 3039 return (notify_count); 3040 if (in_use > 2 * minthresh) 3041 return (notify_count >> 1); 3042 if (in_use > minthresh) 3043 return (notify_count >> 3); 3044 return (0); 3045 } 3046 3047 static inline qidx_t 3048 txq_max_rs_deferred(iflib_txq_t txq) 3049 { 3050 qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 3051 qidx_t minthresh = txq->ift_size / 8; 3052 if (txq->ift_in_use > 4 * minthresh) 3053 return (notify_count); 3054 if (txq->ift_in_use > 2 * minthresh) 3055 return (notify_count >> 1); 3056 if (txq->ift_in_use > minthresh) 3057 return (notify_count >> 2); 3058 return (2); 3059 } 3060 3061 #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags) 3062 #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG) 3063 3064 #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use)) 3065 #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq) 3066 #define TXQ_MAX_DB_CONSUMED(size) (size >> 4) 3067 3068 /* forward compatibility for cxgb */ 3069 #define FIRST_QSET(ctx) 0 3070 #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets) 3071 #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets) 3072 #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx)) 3073 #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments)) 3074 3075 #define MAX_TX_DESC(ctx) MAX((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \ 3076 (ctx)->ifc_softc_ctx.isc_tx_nsegments) 3077 3078 static inline bool 3079 iflib_txd_db_check(iflib_txq_t txq, int ring) 3080 { 3081 if_ctx_t ctx = txq->ift_ctx; 3082 qidx_t dbval, max; 3083 3084 max = TXQ_MAX_DB_DEFERRED(txq, txq->ift_in_use); 3085 3086 /* force || threshold exceeded || at the edge of the ring */ 3087 if (ring || (txq->ift_db_pending >= max) || (TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx))) { 3088 3089 /* 3090 * 'npending' is used if the card's doorbell is in terms of the number of descriptors 3091 * pending flush (BRCM). 'pidx' is used in cases where the card's doorbeel uses the 3092 * producer index explicitly (INTC). 3093 */ 3094 dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx; 3095 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 3096 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 3097 ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval); 3098 3099 /* 3100 * Absent bugs there are zero packets pending so reset pending counts to zero. 3101 */ 3102 txq->ift_db_pending = txq->ift_npending = 0; 3103 return (true); 3104 } 3105 return (false); 3106 } 3107 3108 #ifdef PKT_DEBUG 3109 static void 3110 print_pkt(if_pkt_info_t pi) 3111 { 3112 printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n", 3113 pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx); 3114 printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n", 3115 pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag); 3116 printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n", 3117 pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto); 3118 } 3119 #endif 3120 3121 #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO) 3122 #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO)) 3123 #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO) 3124 #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO)) 3125 3126 /** 3127 * Parses out ethernet header information in the given mbuf. 3128 * Returns in pi: ipi_etype (EtherType) and ipi_ehdrlen (Ethernet header length) 3129 * 3130 * This will account for the VLAN header if present. 3131 * 3132 * XXX: This doesn't handle QinQ, which could prevent TX offloads for those 3133 * types of packets. 3134 */ 3135 static int 3136 iflib_parse_ether_header(if_pkt_info_t pi, struct mbuf **mp, uint64_t *pullups) 3137 { 3138 struct ether_vlan_header *eh; 3139 struct mbuf *m; 3140 3141 m = *mp; 3142 if (__predict_false(m->m_len < sizeof(*eh))) { 3143 (*pullups)++; 3144 if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL)) 3145 return (ENOMEM); 3146 } 3147 eh = mtod(m, struct ether_vlan_header *); 3148 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 3149 pi->ipi_etype = ntohs(eh->evl_proto); 3150 pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 3151 } else { 3152 pi->ipi_etype = ntohs(eh->evl_encap_proto); 3153 pi->ipi_ehdrlen = ETHER_HDR_LEN; 3154 } 3155 *mp = m; 3156 3157 return (0); 3158 } 3159 3160 /** 3161 * Parse up to the L3 header and extract IPv4/IPv6 header information into pi. 3162 * Currently this information includes: IP ToS value, IP header version/presence 3163 * 3164 * This is missing some checks and doesn't edit the packet content as it goes, 3165 * unlike iflib_parse_header(), in order to keep the amount of code here minimal. 3166 */ 3167 static int 3168 iflib_parse_header_partial(if_pkt_info_t pi, struct mbuf **mp, uint64_t *pullups) 3169 { 3170 struct mbuf *m; 3171 int err; 3172 3173 *pullups = 0; 3174 m = *mp; 3175 if (!M_WRITABLE(m)) { 3176 if ((m = m_dup(m, M_NOWAIT)) == NULL) { 3177 return (ENOMEM); 3178 } else { 3179 m_freem(*mp); 3180 DBG_COUNTER_INC(tx_frees); 3181 *mp = m; 3182 } 3183 } 3184 3185 /* Fills out pi->ipi_etype */ 3186 err = iflib_parse_ether_header(pi, mp, pullups); 3187 if (err) 3188 return (err); 3189 m = *mp; 3190 3191 switch (pi->ipi_etype) { 3192 #ifdef INET 3193 case ETHERTYPE_IP: 3194 { 3195 struct mbuf *n; 3196 struct ip *ip = NULL; 3197 int miniplen; 3198 3199 miniplen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip)); 3200 if (__predict_false(m->m_len < miniplen)) { 3201 /* 3202 * Check for common case where the first mbuf only contains 3203 * the Ethernet header 3204 */ 3205 if (m->m_len == pi->ipi_ehdrlen) { 3206 n = m->m_next; 3207 MPASS(n); 3208 /* If next mbuf contains at least the minimal IP header, then stop */ 3209 if (n->m_len >= sizeof(*ip)) { 3210 ip = (struct ip *)n->m_data; 3211 } else { 3212 (*pullups)++; 3213 if (__predict_false((m = m_pullup(m, miniplen)) == NULL)) 3214 return (ENOMEM); 3215 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 3216 } 3217 } else { 3218 (*pullups)++; 3219 if (__predict_false((m = m_pullup(m, miniplen)) == NULL)) 3220 return (ENOMEM); 3221 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 3222 } 3223 } else { 3224 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 3225 } 3226 3227 /* Have the IPv4 header w/ no options here */ 3228 pi->ipi_ip_hlen = ip->ip_hl << 2; 3229 pi->ipi_ipproto = ip->ip_p; 3230 pi->ipi_ip_tos = ip->ip_tos; 3231 pi->ipi_flags |= IPI_TX_IPV4; 3232 3233 break; 3234 } 3235 #endif 3236 #ifdef INET6 3237 case ETHERTYPE_IPV6: 3238 { 3239 struct ip6_hdr *ip6; 3240 3241 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) { 3242 (*pullups)++; 3243 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL)) 3244 return (ENOMEM); 3245 } 3246 ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen); 3247 3248 /* Have the IPv6 fixed header here */ 3249 pi->ipi_ip_hlen = sizeof(struct ip6_hdr); 3250 pi->ipi_ipproto = ip6->ip6_nxt; 3251 pi->ipi_ip_tos = IPV6_TRAFFIC_CLASS(ip6); 3252 pi->ipi_flags |= IPI_TX_IPV6; 3253 3254 break; 3255 } 3256 #endif 3257 default: 3258 pi->ipi_csum_flags &= ~CSUM_OFFLOAD; 3259 pi->ipi_ip_hlen = 0; 3260 break; 3261 } 3262 *mp = m; 3263 3264 return (0); 3265 3266 } 3267 3268 static int 3269 iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp) 3270 { 3271 if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx; 3272 struct mbuf *m; 3273 int err; 3274 3275 m = *mp; 3276 if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) && 3277 M_WRITABLE(m) == 0) { 3278 if ((m = m_dup(m, M_NOWAIT)) == NULL) { 3279 return (ENOMEM); 3280 } else { 3281 m_freem(*mp); 3282 DBG_COUNTER_INC(tx_frees); 3283 *mp = m; 3284 } 3285 } 3286 3287 /* Fills out pi->ipi_etype */ 3288 err = iflib_parse_ether_header(pi, mp, &txq->ift_pullups); 3289 if (__predict_false(err)) 3290 return (err); 3291 m = *mp; 3292 3293 switch (pi->ipi_etype) { 3294 #ifdef INET 3295 case ETHERTYPE_IP: 3296 { 3297 struct ip *ip; 3298 struct tcphdr *th; 3299 uint8_t hlen; 3300 3301 hlen = pi->ipi_ehdrlen + sizeof(*ip); 3302 if (__predict_false(m->m_len < hlen)) { 3303 txq->ift_pullups++; 3304 if (__predict_false((m = m_pullup(m, hlen)) == NULL)) 3305 return (ENOMEM); 3306 } 3307 ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 3308 hlen = pi->ipi_ehdrlen + (ip->ip_hl << 2); 3309 if (ip->ip_p == IPPROTO_TCP) { 3310 hlen += sizeof(*th); 3311 th = (struct tcphdr *)((char *)ip + (ip->ip_hl << 2)); 3312 } else if (ip->ip_p == IPPROTO_UDP) { 3313 hlen += sizeof(struct udphdr); 3314 } 3315 if (__predict_false(m->m_len < hlen)) { 3316 txq->ift_pullups++; 3317 if ((m = m_pullup(m, hlen)) == NULL) 3318 return (ENOMEM); 3319 } 3320 pi->ipi_ip_hlen = ip->ip_hl << 2; 3321 pi->ipi_ipproto = ip->ip_p; 3322 pi->ipi_ip_tos = ip->ip_tos; 3323 pi->ipi_flags |= IPI_TX_IPV4; 3324 3325 /* TCP checksum offload may require TCP header length */ 3326 if (IS_TX_OFFLOAD4(pi)) { 3327 if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) { 3328 pi->ipi_tcp_hflags = tcp_get_flags(th); 3329 pi->ipi_tcp_hlen = th->th_off << 2; 3330 pi->ipi_tcp_seq = th->th_seq; 3331 } 3332 if (IS_TSO4(pi)) { 3333 if (__predict_false(ip->ip_p != IPPROTO_TCP)) 3334 return (ENXIO); 3335 /* 3336 * TSO always requires hardware checksum offload. 3337 */ 3338 pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP); 3339 th->th_sum = in_pseudo(ip->ip_src.s_addr, 3340 ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 3341 pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 3342 if (sctx->isc_flags & IFLIB_TSO_INIT_IP) { 3343 ip->ip_sum = 0; 3344 ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz); 3345 } 3346 } 3347 } 3348 if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP)) 3349 ip->ip_sum = 0; 3350 3351 break; 3352 } 3353 #endif 3354 #ifdef INET6 3355 case ETHERTYPE_IPV6: 3356 { 3357 struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen); 3358 struct tcphdr *th; 3359 pi->ipi_ip_hlen = sizeof(struct ip6_hdr); 3360 3361 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) { 3362 txq->ift_pullups++; 3363 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL)) 3364 return (ENOMEM); 3365 } 3366 th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen); 3367 3368 /* XXX-BZ this will go badly in case of ext hdrs. */ 3369 pi->ipi_ipproto = ip6->ip6_nxt; 3370 pi->ipi_ip_tos = IPV6_TRAFFIC_CLASS(ip6); 3371 pi->ipi_flags |= IPI_TX_IPV6; 3372 3373 /* TCP checksum offload may require TCP header length */ 3374 if (IS_TX_OFFLOAD6(pi)) { 3375 if (pi->ipi_ipproto == IPPROTO_TCP) { 3376 if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) { 3377 txq->ift_pullups++; 3378 if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL)) 3379 return (ENOMEM); 3380 } 3381 pi->ipi_tcp_hflags = tcp_get_flags(th); 3382 pi->ipi_tcp_hlen = th->th_off << 2; 3383 pi->ipi_tcp_seq = th->th_seq; 3384 } 3385 if (IS_TSO6(pi)) { 3386 if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP)) 3387 return (ENXIO); 3388 /* 3389 * TSO always requires hardware checksum offload. 3390 */ 3391 pi->ipi_csum_flags |= CSUM_IP6_TCP; 3392 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0); 3393 pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 3394 } 3395 } 3396 break; 3397 } 3398 #endif 3399 default: 3400 pi->ipi_csum_flags &= ~CSUM_OFFLOAD; 3401 pi->ipi_ip_hlen = 0; 3402 break; 3403 } 3404 *mp = m; 3405 3406 return (0); 3407 } 3408 3409 /* 3410 * If dodgy hardware rejects the scatter gather chain we've handed it 3411 * we'll need to remove the mbuf chain from ifsg_m[] before we can add the 3412 * m_defrag'd mbufs 3413 */ 3414 static __noinline struct mbuf * 3415 iflib_remove_mbuf(iflib_txq_t txq) 3416 { 3417 int ntxd, pidx; 3418 struct mbuf *m, **ifsd_m; 3419 3420 ifsd_m = txq->ift_sds.ifsd_m; 3421 ntxd = txq->ift_size; 3422 pidx = txq->ift_pidx & (ntxd - 1); 3423 ifsd_m = txq->ift_sds.ifsd_m; 3424 m = IFLIB_GET_MBUF(ifsd_m[pidx]); 3425 ifsd_m[pidx] = NULL; 3426 bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]); 3427 if (txq->ift_sds.ifsd_tso_map != NULL) 3428 bus_dmamap_unload(txq->ift_tso_buf_tag, 3429 txq->ift_sds.ifsd_tso_map[pidx]); 3430 #if MEMORY_LOGGING 3431 txq->ift_dequeued++; 3432 #endif 3433 return (m); 3434 } 3435 3436 /* 3437 * Pad an mbuf to ensure a minimum ethernet frame size. 3438 * min_frame_size is the frame size (less CRC) to pad the mbuf to 3439 */ 3440 static __noinline int 3441 iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size) 3442 { 3443 /* 3444 * 18 is enough bytes to pad an ARP packet to 46 bytes, and 3445 * and ARP message is the smallest common payload I can think of 3446 */ 3447 static char pad[18]; /* just zeros */ 3448 int n; 3449 struct mbuf *new_head; 3450 3451 if (!M_WRITABLE(*m_head)) { 3452 new_head = m_dup(*m_head, M_NOWAIT); 3453 if (new_head == NULL) { 3454 m_freem(*m_head); 3455 device_printf(dev, "cannot pad short frame, m_dup() failed"); 3456 DBG_COUNTER_INC(encap_pad_mbuf_fail); 3457 DBG_COUNTER_INC(tx_frees); 3458 return (ENOMEM); 3459 } 3460 m_freem(*m_head); 3461 *m_head = new_head; 3462 } 3463 3464 for (n = min_frame_size - (*m_head)->m_pkthdr.len; 3465 n > 0; n -= sizeof(pad)) 3466 if (!m_append(*m_head, min(n, sizeof(pad)), pad)) 3467 break; 3468 3469 if (n > 0) { 3470 m_freem(*m_head); 3471 device_printf(dev, "cannot pad short frame\n"); 3472 DBG_COUNTER_INC(encap_pad_mbuf_fail); 3473 DBG_COUNTER_INC(tx_frees); 3474 return (ENOBUFS); 3475 } 3476 3477 return (0); 3478 } 3479 3480 static int 3481 iflib_encap(iflib_txq_t txq, struct mbuf **m_headp, int *obytes, int *opkts) 3482 { 3483 if_ctx_t ctx; 3484 if_shared_ctx_t sctx; 3485 if_softc_ctx_t scctx; 3486 bus_dma_tag_t buf_tag; 3487 bus_dma_segment_t *segs; 3488 struct mbuf *m_head, **ifsd_m; 3489 bus_dmamap_t map; 3490 struct if_pkt_info pi; 3491 uintptr_t flags; 3492 int remap = 0; 3493 int err, nsegs, ndesc, max_segs, pidx; 3494 3495 ctx = txq->ift_ctx; 3496 sctx = ctx->ifc_sctx; 3497 scctx = &ctx->ifc_softc_ctx; 3498 segs = txq->ift_segs; 3499 m_head = *m_headp; 3500 map = NULL; 3501 3502 /* 3503 * If we're doing TSO the next descriptor to clean may be quite far ahead 3504 */ 3505 pidx = txq->ift_pidx; 3506 map = txq->ift_sds.ifsd_map[pidx]; 3507 ifsd_m = txq->ift_sds.ifsd_m; 3508 3509 if (m_head->m_pkthdr.csum_flags & CSUM_TSO) { 3510 buf_tag = txq->ift_tso_buf_tag; 3511 max_segs = scctx->isc_tx_tso_segments_max; 3512 map = txq->ift_sds.ifsd_tso_map[pidx]; 3513 MPASS(buf_tag != NULL); 3514 MPASS(max_segs > 0); 3515 flags = IFLIB_TSO; 3516 } else { 3517 buf_tag = txq->ift_buf_tag; 3518 max_segs = scctx->isc_tx_nsegments; 3519 map = txq->ift_sds.ifsd_map[pidx]; 3520 flags = IFLIB_NO_TSO; 3521 } 3522 if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) && 3523 __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) { 3524 err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size); 3525 if (err) { 3526 DBG_COUNTER_INC(encap_txd_encap_fail); 3527 return (err); 3528 } 3529 } 3530 m_head = *m_headp; 3531 3532 memset(&pi, 0, sizeof(pi)); 3533 pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG | M_BCAST | M_MCAST)); 3534 pi.ipi_pidx = pidx; 3535 pi.ipi_qsidx = txq->ift_id; 3536 pi.ipi_len = m_head->m_pkthdr.len; 3537 pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags; 3538 pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0; 3539 3540 /* deliberate bitwise OR to make one condition */ 3541 if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) { 3542 if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) { 3543 DBG_COUNTER_INC(encap_txd_encap_fail); 3544 return (err); 3545 } 3546 m_head = *m_headp; 3547 } 3548 3549 retry: 3550 err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs, 3551 BUS_DMA_NOWAIT); 3552 defrag: 3553 if (__predict_false(err)) { 3554 switch (err) { 3555 case EFBIG: 3556 /* try collapse once and defrag once */ 3557 if (remap == 0) { 3558 m_head = m_collapse(*m_headp, M_NOWAIT, max_segs); 3559 /* try defrag if collapsing fails */ 3560 if (m_head == NULL) 3561 remap++; 3562 } 3563 if (remap == 1) { 3564 txq->ift_mbuf_defrag++; 3565 m_head = m_defrag(*m_headp, M_NOWAIT); 3566 } 3567 /* 3568 * remap should never be >1 unless bus_dmamap_load_mbuf_sg 3569 * failed to map an mbuf that was run through m_defrag 3570 */ 3571 MPASS(remap <= 1); 3572 if (__predict_false(m_head == NULL || remap > 1)) 3573 goto defrag_failed; 3574 remap++; 3575 *m_headp = m_head; 3576 goto retry; 3577 break; 3578 case ENOMEM: 3579 txq->ift_no_tx_dma_setup++; 3580 break; 3581 default: 3582 txq->ift_no_tx_dma_setup++; 3583 m_freem(*m_headp); 3584 DBG_COUNTER_INC(tx_frees); 3585 *m_headp = NULL; 3586 break; 3587 } 3588 txq->ift_map_failed++; 3589 DBG_COUNTER_INC(encap_load_mbuf_fail); 3590 DBG_COUNTER_INC(encap_txd_encap_fail); 3591 return (err); 3592 } 3593 ifsd_m[pidx] = IFLIB_SAVE_MBUF(m_head, flags); 3594 if (m_head->m_pkthdr.csum_flags & CSUM_SND_TAG) 3595 pi.ipi_mbuf = m_head; 3596 else 3597 pi.ipi_mbuf = NULL; 3598 /* 3599 * XXX assumes a 1 to 1 relationship between segments and 3600 * descriptors - this does not hold true on all drivers, e.g. 3601 * cxgb 3602 */ 3603 if (__predict_false(nsegs > TXQ_AVAIL(txq))) { 3604 iflib_completed_tx_reclaim_force(txq); 3605 if (__predict_false(nsegs > TXQ_AVAIL(txq))) { 3606 txq->ift_no_desc_avail++; 3607 bus_dmamap_unload(buf_tag, map); 3608 DBG_COUNTER_INC(encap_txq_avail_fail); 3609 DBG_COUNTER_INC(encap_txd_encap_fail); 3610 if (ctx->ifc_sysctl_simple_tx) { 3611 *m_headp = m_head = iflib_remove_mbuf(txq); 3612 m_freem(*m_headp); 3613 DBG_COUNTER_INC(tx_frees); 3614 *m_headp = NULL; 3615 } 3616 if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) 3617 GROUPTASK_ENQUEUE(&txq->ift_task); 3618 return (ENOBUFS); 3619 } 3620 } 3621 /* 3622 * On Intel cards we can greatly reduce the number of TX interrupts 3623 * we see by only setting report status on every Nth descriptor. 3624 * However, this also means that the driver will need to keep track 3625 * of the descriptors that RS was set on to check them for the DD bit. 3626 */ 3627 txq->ift_rs_pending += nsegs + 1; 3628 if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) || 3629 iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx)) { 3630 pi.ipi_flags |= IPI_TX_INTR; 3631 txq->ift_rs_pending = 0; 3632 } 3633 3634 pi.ipi_segs = segs; 3635 pi.ipi_nsegs = nsegs; 3636 3637 MPASS(pidx >= 0 && pidx < txq->ift_size); 3638 #ifdef PKT_DEBUG 3639 print_pkt(&pi); 3640 #endif 3641 if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) { 3642 bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE); 3643 DBG_COUNTER_INC(tx_encap); 3644 MPASS(pi.ipi_new_pidx < txq->ift_size); 3645 3646 ndesc = pi.ipi_new_pidx - pi.ipi_pidx; 3647 if (pi.ipi_new_pidx < pi.ipi_pidx) { 3648 ndesc += txq->ift_size; 3649 txq->ift_gen = 1; 3650 } 3651 /* 3652 * drivers can need up to ift_pad sentinels 3653 */ 3654 MPASS(ndesc <= pi.ipi_nsegs + txq->ift_pad); 3655 MPASS(pi.ipi_new_pidx != pidx); 3656 MPASS(ndesc > 0); 3657 txq->ift_in_use += ndesc; 3658 txq->ift_db_pending += ndesc; 3659 3660 /* 3661 * We update the last software descriptor again here because there may 3662 * be a sentinel and/or there may be more mbufs than segments 3663 */ 3664 txq->ift_pidx = pi.ipi_new_pidx; 3665 txq->ift_npending += pi.ipi_ndescs; 3666 3667 /* 3668 * Update packets / bytes sent 3669 */ 3670 if (flags & IFLIB_TSO) { 3671 int hlen = pi.ipi_ehdrlen + pi.ipi_ip_hlen + pi.ipi_tcp_hlen; 3672 int tsolen = pi.ipi_len - hlen; 3673 int nsegs = (tsolen + pi.ipi_tso_segsz - 1) / pi.ipi_tso_segsz; 3674 *obytes += tsolen + nsegs * hlen; 3675 *opkts += nsegs; 3676 } else { 3677 *obytes += pi.ipi_len; 3678 *opkts += 1; 3679 } 3680 } else { 3681 *m_headp = m_head = iflib_remove_mbuf(txq); 3682 if (err == EFBIG) { 3683 txq->ift_txd_encap_efbig++; 3684 if (remap < 2) { 3685 remap = 1; 3686 goto defrag; 3687 } 3688 } 3689 goto defrag_failed; 3690 } 3691 /* 3692 * err can't possibly be non-zero here, so we don't neet to test it 3693 * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail). 3694 */ 3695 return (err); 3696 3697 defrag_failed: 3698 txq->ift_mbuf_defrag_failed++; 3699 txq->ift_map_failed++; 3700 m_freem(*m_headp); 3701 DBG_COUNTER_INC(tx_frees); 3702 *m_headp = NULL; 3703 DBG_COUNTER_INC(encap_txd_encap_fail); 3704 return (ENOMEM); 3705 } 3706 3707 static void 3708 iflib_tx_desc_free(iflib_txq_t txq, int n, struct mbuf **m_defer) 3709 { 3710 uint32_t qsize, cidx, gen; 3711 struct mbuf *m, **ifsd_m; 3712 uintptr_t flags; 3713 3714 cidx = txq->ift_cidx; 3715 gen = txq->ift_gen; 3716 qsize = txq->ift_size; 3717 ifsd_m =txq->ift_sds.ifsd_m; 3718 3719 while (n-- > 0) { 3720 if ((m = IFLIB_GET_MBUF(ifsd_m[cidx])) != NULL) { 3721 flags = IFLIB_GET_FLAGS(ifsd_m[cidx]); 3722 MPASS(flags != 0); 3723 if (flags & IFLIB_TSO) { 3724 bus_dmamap_sync(txq->ift_tso_buf_tag, 3725 txq->ift_sds.ifsd_tso_map[cidx], 3726 BUS_DMASYNC_POSTWRITE); 3727 bus_dmamap_unload(txq->ift_tso_buf_tag, 3728 txq->ift_sds.ifsd_tso_map[cidx]); 3729 } else { 3730 bus_dmamap_sync(txq->ift_buf_tag, 3731 txq->ift_sds.ifsd_map[cidx], 3732 BUS_DMASYNC_POSTWRITE); 3733 bus_dmamap_unload(txq->ift_buf_tag, 3734 txq->ift_sds.ifsd_map[cidx]); 3735 } 3736 /* XXX we don't support any drivers that batch packets yet */ 3737 MPASS(m->m_nextpkt == NULL); 3738 if (m_defer == NULL) { 3739 m_freem(m); 3740 } else if (m != NULL) { 3741 *m_defer = m; 3742 m_defer++; 3743 } 3744 ifsd_m[cidx] = NULL; 3745 #if MEMORY_LOGGING 3746 txq->ift_dequeued++; 3747 #endif 3748 DBG_COUNTER_INC(tx_frees); 3749 } 3750 if (__predict_false(++cidx == qsize)) { 3751 cidx = 0; 3752 gen = 0; 3753 } 3754 } 3755 txq->ift_cidx = cidx; 3756 txq->ift_gen = gen; 3757 } 3758 3759 static __inline int 3760 iflib_txq_can_reclaim(iflib_txq_t txq) 3761 { 3762 int reclaim, thresh; 3763 3764 thresh = txq->ift_reclaim_thresh; 3765 KASSERT(thresh >= 0, ("invalid threshold to reclaim")); 3766 MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size); 3767 3768 if (ticks <= (txq->ift_last_reclaim + txq->ift_reclaim_ticks) && 3769 txq->ift_in_use < thresh) 3770 return (false); 3771 iflib_tx_credits_update(txq->ift_ctx, txq); 3772 reclaim = DESC_RECLAIMABLE(txq); 3773 if (reclaim <= thresh) { 3774 #ifdef INVARIANTS 3775 if (iflib_verbose_debug) { 3776 printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __func__, 3777 txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments, 3778 reclaim, thresh); 3779 } 3780 #endif 3781 return (0); 3782 } 3783 return (reclaim); 3784 } 3785 3786 static __inline void 3787 _iflib_completed_tx_reclaim(iflib_txq_t txq, struct mbuf **m_defer, int reclaim) 3788 { 3789 txq->ift_last_reclaim = ticks; 3790 iflib_tx_desc_free(txq, reclaim, m_defer); 3791 txq->ift_cleaned += reclaim; 3792 txq->ift_in_use -= reclaim; 3793 } 3794 3795 static __inline int 3796 iflib_completed_tx_reclaim(iflib_txq_t txq, struct mbuf **m_defer) 3797 { 3798 int reclaim; 3799 3800 reclaim = iflib_txq_can_reclaim(txq); 3801 if (reclaim == 0) 3802 return (0); 3803 _iflib_completed_tx_reclaim(txq, m_defer, reclaim); 3804 return (reclaim); 3805 } 3806 3807 /* 3808 * Reclaim any transmit descriptors possible, ignoring coalescing 3809 */ 3810 static __inline void 3811 iflib_completed_tx_reclaim_force(iflib_txq_t txq) 3812 { 3813 int reclaim; 3814 3815 iflib_tx_credits_update(txq->ift_ctx, txq); 3816 reclaim = DESC_RECLAIMABLE(txq); 3817 if (reclaim != 0) 3818 _iflib_completed_tx_reclaim(txq, NULL, reclaim); 3819 } 3820 3821 static struct mbuf ** 3822 _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining) 3823 { 3824 int next, size; 3825 struct mbuf **items; 3826 3827 size = r->size; 3828 next = (cidx + CACHE_PTR_INCREMENT) & (size - 1); 3829 items = __DEVOLATILE(struct mbuf **, &r->items[0]); 3830 3831 prefetch(items[(cidx + offset) & (size - 1)]); 3832 if (remaining > 1) { 3833 prefetch2cachelines(&items[next]); 3834 prefetch2cachelines(items[(cidx + offset + 1) & (size - 1)]); 3835 prefetch2cachelines(items[(cidx + offset + 2) & (size - 1)]); 3836 prefetch2cachelines(items[(cidx + offset + 3) & (size - 1)]); 3837 } 3838 return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size - 1)])); 3839 } 3840 3841 static void 3842 iflib_txq_check_drain(iflib_txq_t txq, int budget) 3843 { 3844 3845 ifmp_ring_check_drainage(txq->ift_br, budget); 3846 } 3847 3848 static uint32_t 3849 iflib_txq_can_drain(struct ifmp_ring *r) 3850 { 3851 iflib_txq_t txq = r->cookie; 3852 if_ctx_t ctx = txq->ift_ctx; 3853 3854 if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx)) 3855 return (1); 3856 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 3857 BUS_DMASYNC_POSTREAD); 3858 return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, 3859 false)); 3860 } 3861 3862 static uint32_t 3863 iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 3864 { 3865 iflib_txq_t txq = r->cookie; 3866 if_ctx_t ctx = txq->ift_ctx; 3867 if_t ifp = ctx->ifc_ifp; 3868 struct mbuf *m, **mp; 3869 int avail, bytes_sent, consumed, count, err, i; 3870 int mcast_sent, pkt_sent, reclaimed; 3871 bool do_prefetch, rang, ring; 3872 3873 if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) || 3874 !LINK_ACTIVE(ctx))) { 3875 DBG_COUNTER_INC(txq_drain_notready); 3876 return (0); 3877 } 3878 reclaimed = iflib_completed_tx_reclaim(txq, NULL); 3879 rang = iflib_txd_db_check(txq, reclaimed && txq->ift_db_pending); 3880 avail = IDXDIFF(pidx, cidx, r->size); 3881 3882 if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) { 3883 /* 3884 * The driver is unloading so we need to free all pending packets. 3885 */ 3886 DBG_COUNTER_INC(txq_drain_flushing); 3887 for (i = 0; i < avail; i++) { 3888 if (__predict_true(r->items[(cidx + i) & (r->size - 1)] != (void *)txq)) 3889 m_freem(r->items[(cidx + i) & (r->size - 1)]); 3890 r->items[(cidx + i) & (r->size - 1)] = NULL; 3891 } 3892 return (avail); 3893 } 3894 3895 if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) { 3896 txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3897 CALLOUT_LOCK(txq); 3898 callout_stop(&txq->ift_timer); 3899 CALLOUT_UNLOCK(txq); 3900 DBG_COUNTER_INC(txq_drain_oactive); 3901 return (0); 3902 } 3903 3904 /* 3905 * If we've reclaimed any packets this queue cannot be hung. 3906 */ 3907 if (reclaimed) 3908 txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3909 consumed = mcast_sent = bytes_sent = pkt_sent = 0; 3910 count = MIN(avail, TX_BATCH_SIZE); 3911 #ifdef INVARIANTS 3912 if (iflib_verbose_debug) 3913 printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __func__, 3914 avail, ctx->ifc_flags, TXQ_AVAIL(txq)); 3915 #endif 3916 do_prefetch = (ctx->ifc_flags & IFC_PREFETCH); 3917 err = 0; 3918 for (i = 0; i < count && TXQ_AVAIL(txq) >= MAX_TX_DESC(ctx); i++) { 3919 int rem = do_prefetch ? count - i : 0; 3920 3921 mp = _ring_peek_one(r, cidx, i, rem); 3922 MPASS(mp != NULL && *mp != NULL); 3923 3924 /* 3925 * Completion interrupts will use the address of the txq 3926 * as a sentinel to enqueue _something_ in order to acquire 3927 * the lock on the mp_ring (there's no direct lock call). 3928 * We obviously whave to check for these sentinel cases 3929 * and skip them. 3930 */ 3931 if (__predict_false(*mp == (struct mbuf *)txq)) { 3932 consumed++; 3933 continue; 3934 } 3935 err = iflib_encap(txq, mp, &bytes_sent, &pkt_sent); 3936 if (__predict_false(err)) { 3937 /* no room - bail out */ 3938 if (err == ENOBUFS) 3939 break; 3940 consumed++; 3941 /* we can't send this packet - skip it */ 3942 continue; 3943 } 3944 consumed++; 3945 m = *mp; 3946 DBG_COUNTER_INC(tx_sent); 3947 mcast_sent += !!(m->m_flags & M_MCAST); 3948 3949 if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))) 3950 break; 3951 ETHER_BPF_MTAP(ifp, m); 3952 rang = iflib_txd_db_check(txq, false); 3953 } 3954 3955 /* deliberate use of bitwise or to avoid gratuitous short-circuit */ 3956 ring = rang ? false : (iflib_min_tx_latency | err | (!!txq->ift_reclaim_thresh)); 3957 iflib_txd_db_check(txq, ring); 3958 if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); 3959 if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); 3960 if (mcast_sent) 3961 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent); 3962 #ifdef INVARIANTS 3963 if (iflib_verbose_debug) 3964 printf("consumed=%d\n", consumed); 3965 #endif 3966 return (consumed); 3967 } 3968 3969 static uint32_t 3970 iflib_txq_drain_always(struct ifmp_ring *r) 3971 { 3972 return (1); 3973 } 3974 3975 static uint32_t 3976 iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 3977 { 3978 int i, avail; 3979 struct mbuf **mp; 3980 iflib_txq_t txq; 3981 3982 txq = r->cookie; 3983 3984 txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3985 CALLOUT_LOCK(txq); 3986 callout_stop(&txq->ift_timer); 3987 CALLOUT_UNLOCK(txq); 3988 3989 avail = IDXDIFF(pidx, cidx, r->size); 3990 for (i = 0; i < avail; i++) { 3991 mp = _ring_peek_one(r, cidx, i, avail - i); 3992 if (__predict_false(*mp == (struct mbuf *)txq)) 3993 continue; 3994 m_freem(*mp); 3995 DBG_COUNTER_INC(tx_frees); 3996 } 3997 MPASS(ifmp_ring_is_stalled(r) == 0); 3998 return (avail); 3999 } 4000 4001 static void 4002 iflib_ifmp_purge(iflib_txq_t txq) 4003 { 4004 struct ifmp_ring *r; 4005 4006 r = txq->ift_br; 4007 r->drain = iflib_txq_drain_free; 4008 r->can_drain = iflib_txq_drain_always; 4009 4010 ifmp_ring_check_drainage(r, r->size); 4011 4012 r->drain = iflib_txq_drain; 4013 r->can_drain = iflib_txq_can_drain; 4014 } 4015 4016 static void 4017 _task_fn_tx(void *context) 4018 { 4019 iflib_txq_t txq = context; 4020 if_ctx_t ctx = txq->ift_ctx; 4021 if_t ifp = ctx->ifc_ifp; 4022 int abdicate = ctx->ifc_sysctl_tx_abdicate; 4023 4024 #ifdef IFLIB_DIAGNOSTICS 4025 txq->ift_cpu_exec_count[curcpu]++; 4026 #endif 4027 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 4028 return; 4029 #ifdef DEV_NETMAP 4030 if ((if_getcapenable(ifp) & IFCAP_NETMAP) && 4031 netmap_tx_irq(ifp, txq->ift_id)) 4032 goto skip_ifmp; 4033 #endif 4034 if (ctx->ifc_sysctl_simple_tx) { 4035 mtx_lock(&txq->ift_mtx); 4036 (void)iflib_completed_tx_reclaim(txq, NULL); 4037 mtx_unlock(&txq->ift_mtx); 4038 goto skip_ifmp; 4039 } 4040 #ifdef ALTQ 4041 if (if_altq_is_enabled(ifp)) 4042 iflib_altq_if_start(ifp); 4043 #endif 4044 if (txq->ift_db_pending) 4045 ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate); 4046 else if (!abdicate) 4047 ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 4048 /* 4049 * When abdicating, we always need to check drainage, not just when we don't enqueue 4050 */ 4051 if (abdicate) 4052 ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 4053 4054 skip_ifmp: 4055 if (ctx->ifc_flags & IFC_LEGACY) 4056 IFDI_INTR_ENABLE(ctx); 4057 else 4058 IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 4059 } 4060 4061 static void 4062 _task_fn_rx(void *context) 4063 { 4064 iflib_rxq_t rxq = context; 4065 if_ctx_t ctx = rxq->ifr_ctx; 4066 uint8_t more; 4067 uint16_t budget; 4068 #ifdef DEV_NETMAP 4069 u_int work = 0; 4070 int nmirq; 4071 #endif 4072 4073 #ifdef IFLIB_DIAGNOSTICS 4074 rxq->ifr_cpu_exec_count[curcpu]++; 4075 #endif 4076 DBG_COUNTER_INC(task_fn_rxs); 4077 if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 4078 return; 4079 #ifdef DEV_NETMAP 4080 nmirq = netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work); 4081 if (nmirq != NM_IRQ_PASS) { 4082 more = (nmirq == NM_IRQ_RESCHED) ? IFLIB_RXEOF_MORE : 0; 4083 goto skip_rxeof; 4084 } 4085 #endif 4086 budget = ctx->ifc_sysctl_rx_budget; 4087 if (budget == 0) 4088 budget = 16; /* XXX */ 4089 more = iflib_rxeof(rxq, budget); 4090 #ifdef DEV_NETMAP 4091 skip_rxeof: 4092 #endif 4093 if ((more & IFLIB_RXEOF_MORE) == 0) { 4094 if (ctx->ifc_flags & IFC_LEGACY) 4095 IFDI_INTR_ENABLE(ctx); 4096 else 4097 IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 4098 DBG_COUNTER_INC(rx_intr_enables); 4099 } 4100 if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 4101 return; 4102 4103 if (more & IFLIB_RXEOF_MORE) 4104 GROUPTASK_ENQUEUE(&rxq->ifr_task); 4105 else if (more & IFLIB_RXEOF_EMPTY) 4106 callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq); 4107 } 4108 4109 static void 4110 _task_fn_admin(void *context, int pending) 4111 { 4112 if_ctx_t ctx = context; 4113 if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 4114 iflib_txq_t txq; 4115 int i; 4116 bool oactive, running, do_reset, do_watchdog, in_detach; 4117 4118 STATE_LOCK(ctx); 4119 running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING); 4120 oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE); 4121 do_reset = (ctx->ifc_flags & IFC_DO_RESET); 4122 do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG); 4123 in_detach = (ctx->ifc_flags & IFC_IN_DETACH); 4124 ctx->ifc_flags &= ~(IFC_DO_RESET | IFC_DO_WATCHDOG); 4125 STATE_UNLOCK(ctx); 4126 4127 if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 4128 return; 4129 if (in_detach) 4130 return; 4131 4132 CTX_LOCK(ctx); 4133 for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 4134 CALLOUT_LOCK(txq); 4135 callout_stop(&txq->ift_timer); 4136 CALLOUT_UNLOCK(txq); 4137 } 4138 if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_ADMINCQ) 4139 IFDI_ADMIN_COMPLETION_HANDLE(ctx); 4140 if (do_watchdog) { 4141 ctx->ifc_watchdog_events++; 4142 IFDI_WATCHDOG_RESET(ctx); 4143 } 4144 IFDI_UPDATE_ADMIN_STATUS(ctx); 4145 for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 4146 callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq, 4147 txq->ift_timer.c_cpu); 4148 } 4149 IFDI_LINK_INTR_ENABLE(ctx); 4150 if (do_reset) 4151 iflib_if_init_locked(ctx); 4152 CTX_UNLOCK(ctx); 4153 4154 if (LINK_ACTIVE(ctx) == 0) 4155 return; 4156 for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) 4157 iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 4158 } 4159 4160 static void 4161 _task_fn_iov(void *context, int pending) 4162 { 4163 if_ctx_t ctx = context; 4164 4165 if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) && 4166 !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 4167 return; 4168 4169 CTX_LOCK(ctx); 4170 IFDI_VFLR_HANDLE(ctx); 4171 CTX_UNLOCK(ctx); 4172 } 4173 4174 static int 4175 iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS) 4176 { 4177 int err; 4178 if_int_delay_info_t info; 4179 if_ctx_t ctx; 4180 4181 info = (if_int_delay_info_t)arg1; 4182 ctx = info->iidi_ctx; 4183 info->iidi_req = req; 4184 info->iidi_oidp = oidp; 4185 CTX_LOCK(ctx); 4186 err = IFDI_SYSCTL_INT_DELAY(ctx, info); 4187 CTX_UNLOCK(ctx); 4188 return (err); 4189 } 4190 4191 /********************************************************************* 4192 * 4193 * IFNET FUNCTIONS 4194 * 4195 **********************************************************************/ 4196 4197 static void 4198 iflib_if_init_locked(if_ctx_t ctx) 4199 { 4200 iflib_stop(ctx); 4201 iflib_init_locked(ctx); 4202 } 4203 4204 static void 4205 iflib_if_init(void *arg) 4206 { 4207 if_ctx_t ctx = arg; 4208 4209 CTX_LOCK(ctx); 4210 iflib_if_init_locked(ctx); 4211 CTX_UNLOCK(ctx); 4212 } 4213 4214 static int 4215 iflib_if_transmit(if_t ifp, struct mbuf *m) 4216 { 4217 if_ctx_t ctx = if_getsoftc(ifp); 4218 iflib_txq_t txq; 4219 int err, qidx; 4220 int abdicate; 4221 4222 if (__predict_false((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) { 4223 DBG_COUNTER_INC(tx_frees); 4224 m_freem(m); 4225 return (ENETDOWN); 4226 } 4227 4228 MPASS(m->m_nextpkt == NULL); 4229 /* ALTQ-enabled interfaces always use queue 0. */ 4230 qidx = 0; 4231 /* Use driver-supplied queue selection method if it exists */ 4232 if (ctx->isc_txq_select_v2) { 4233 struct if_pkt_info pi; 4234 uint64_t early_pullups = 0; 4235 memset(&pi, 0, sizeof(pi)); 4236 4237 err = iflib_parse_header_partial(&pi, &m, &early_pullups); 4238 if (__predict_false(err != 0)) { 4239 /* Assign pullups for bad pkts to default queue */ 4240 ctx->ifc_txqs[0].ift_pullups += early_pullups; 4241 DBG_COUNTER_INC(encap_txd_encap_fail); 4242 return (err); 4243 } 4244 /* Let driver make queueing decision */ 4245 qidx = ctx->isc_txq_select_v2(ctx->ifc_softc, m, &pi); 4246 ctx->ifc_txqs[qidx].ift_pullups += early_pullups; 4247 } 4248 /* Backwards compatibility w/ simpler queue select */ 4249 else if (ctx->isc_txq_select) 4250 qidx = ctx->isc_txq_select(ctx->ifc_softc, m); 4251 /* If not, use iflib's standard method */ 4252 else if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !if_altq_is_enabled(ifp)) 4253 qidx = QIDX(ctx, m); 4254 4255 /* Set TX queue */ 4256 txq = &ctx->ifc_txqs[qidx]; 4257 4258 #ifdef DRIVER_BACKPRESSURE 4259 if (txq->ift_closed) { 4260 while (m != NULL) { 4261 next = m->m_nextpkt; 4262 m->m_nextpkt = NULL; 4263 m_freem(m); 4264 DBG_COUNTER_INC(tx_frees); 4265 m = next; 4266 } 4267 return (ENOBUFS); 4268 } 4269 #endif 4270 #ifdef notyet 4271 qidx = count = 0; 4272 mp = marr; 4273 next = m; 4274 do { 4275 count++; 4276 next = next->m_nextpkt; 4277 } while (next != NULL); 4278 4279 if (count > nitems(marr)) 4280 if ((mp = malloc(count * sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) { 4281 /* XXX check nextpkt */ 4282 m_freem(m); 4283 /* XXX simplify for now */ 4284 DBG_COUNTER_INC(tx_frees); 4285 return (ENOBUFS); 4286 } 4287 for (next = m, i = 0; next != NULL; i++) { 4288 mp[i] = next; 4289 next = next->m_nextpkt; 4290 mp[i]->m_nextpkt = NULL; 4291 } 4292 #endif 4293 DBG_COUNTER_INC(tx_seen); 4294 abdicate = ctx->ifc_sysctl_tx_abdicate; 4295 4296 err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate); 4297 4298 if (abdicate) 4299 GROUPTASK_ENQUEUE(&txq->ift_task); 4300 if (err) { 4301 if (!abdicate) 4302 GROUPTASK_ENQUEUE(&txq->ift_task); 4303 /* support forthcoming later */ 4304 #ifdef DRIVER_BACKPRESSURE 4305 txq->ift_closed = TRUE; 4306 #endif 4307 ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 4308 m_freem(m); 4309 DBG_COUNTER_INC(tx_frees); 4310 if (err == ENOBUFS) 4311 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 4312 else 4313 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 4314 } 4315 4316 return (err); 4317 } 4318 4319 #ifdef ALTQ 4320 /* 4321 * The overall approach to integrating iflib with ALTQ is to continue to use 4322 * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware 4323 * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring 4324 * is redundant/unnecessary, but doing so minimizes the amount of 4325 * ALTQ-specific code required in iflib. It is assumed that the overhead of 4326 * redundantly queueing to an intermediate mp_ring is swamped by the 4327 * performance limitations inherent in using ALTQ. 4328 * 4329 * When ALTQ support is compiled in, all iflib drivers will use a transmit 4330 * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the 4331 * given interface. If ALTQ is enabled for an interface, then all 4332 * transmitted packets for that interface will be submitted to the ALTQ 4333 * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit() 4334 * implementation because it uses IFQ_HANDOFF(), which will duplicatively 4335 * update stats that the iflib machinery handles, and which is sensitve to 4336 * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start() 4337 * will be installed as the start routine for use by ALTQ facilities that 4338 * need to trigger queue drains on a scheduled basis. 4339 * 4340 */ 4341 static void 4342 iflib_altq_if_start(if_t ifp) 4343 { 4344 struct ifaltq *ifq = &ifp->if_snd; /* XXX - DRVAPI */ 4345 struct mbuf *m; 4346 4347 IFQ_LOCK(ifq); 4348 IFQ_DEQUEUE_NOLOCK(ifq, m); 4349 while (m != NULL) { 4350 iflib_if_transmit(ifp, m); 4351 IFQ_DEQUEUE_NOLOCK(ifq, m); 4352 } 4353 IFQ_UNLOCK(ifq); 4354 } 4355 4356 static int 4357 iflib_altq_if_transmit(if_t ifp, struct mbuf *m) 4358 { 4359 int err; 4360 4361 if (if_altq_is_enabled(ifp)) { 4362 IFQ_ENQUEUE(&ifp->if_snd, m, err); /* XXX - DRVAPI */ 4363 if (err == 0) 4364 iflib_altq_if_start(ifp); 4365 } else 4366 err = iflib_if_transmit(ifp, m); 4367 4368 return (err); 4369 } 4370 #endif /* ALTQ */ 4371 4372 static void 4373 iflib_if_qflush(if_t ifp) 4374 { 4375 if_ctx_t ctx = if_getsoftc(ifp); 4376 iflib_txq_t txq = ctx->ifc_txqs; 4377 int i; 4378 4379 STATE_LOCK(ctx); 4380 ctx->ifc_flags |= IFC_QFLUSH; 4381 STATE_UNLOCK(ctx); 4382 for (i = 0; i < NTXQSETS(ctx); i++, txq++) 4383 while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br))) 4384 iflib_txq_check_drain(txq, 0); 4385 STATE_LOCK(ctx); 4386 ctx->ifc_flags &= ~IFC_QFLUSH; 4387 STATE_UNLOCK(ctx); 4388 4389 /* 4390 * When ALTQ is enabled, this will also take care of purging the 4391 * ALTQ queue(s). 4392 */ 4393 if_qflush(ifp); 4394 } 4395 4396 #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \ 4397 IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \ 4398 IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \ 4399 IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_MEXTPG) 4400 4401 static int 4402 iflib_if_ioctl(if_t ifp, u_long command, caddr_t data) 4403 { 4404 if_ctx_t ctx = if_getsoftc(ifp); 4405 struct ifreq *ifr = (struct ifreq *)data; 4406 #if defined(INET) || defined(INET6) 4407 struct ifaddr *ifa = (struct ifaddr *)data; 4408 #endif 4409 bool avoid_reset = false; 4410 int err = 0, reinit = 0, bits; 4411 4412 switch (command) { 4413 case SIOCSIFADDR: 4414 #ifdef INET 4415 if (ifa->ifa_addr->sa_family == AF_INET) 4416 avoid_reset = true; 4417 #endif 4418 #ifdef INET6 4419 if (ifa->ifa_addr->sa_family == AF_INET6) 4420 avoid_reset = true; 4421 #endif 4422 /* 4423 * Calling init results in link renegotiation, 4424 * so we avoid doing it when possible. 4425 */ 4426 if (avoid_reset) { 4427 if_setflagbits(ifp, IFF_UP, 0); 4428 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 4429 reinit = 1; 4430 #ifdef INET 4431 if (!(if_getflags(ifp) & IFF_NOARP)) 4432 arp_ifinit(ifp, ifa); 4433 #endif 4434 } else 4435 err = ether_ioctl(ifp, command, data); 4436 break; 4437 case SIOCSIFMTU: 4438 CTX_LOCK(ctx); 4439 if (ifr->ifr_mtu == if_getmtu(ifp)) { 4440 CTX_UNLOCK(ctx); 4441 break; 4442 } 4443 bits = if_getdrvflags(ifp); 4444 /* stop the driver and free any clusters before proceeding */ 4445 iflib_stop(ctx); 4446 4447 if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) { 4448 STATE_LOCK(ctx); 4449 if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size) 4450 ctx->ifc_flags |= IFC_MULTISEG; 4451 else 4452 ctx->ifc_flags &= ~IFC_MULTISEG; 4453 STATE_UNLOCK(ctx); 4454 err = if_setmtu(ifp, ifr->ifr_mtu); 4455 } 4456 iflib_init_locked(ctx); 4457 STATE_LOCK(ctx); 4458 if_setdrvflags(ifp, bits); 4459 STATE_UNLOCK(ctx); 4460 CTX_UNLOCK(ctx); 4461 break; 4462 case SIOCSIFFLAGS: 4463 CTX_LOCK(ctx); 4464 if (if_getflags(ifp) & IFF_UP) { 4465 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4466 if ((if_getflags(ifp) ^ ctx->ifc_if_flags) & 4467 (IFF_PROMISC | IFF_ALLMULTI)) { 4468 CTX_UNLOCK(ctx); 4469 err = IFDI_PROMISC_SET(ctx, if_getflags(ifp)); 4470 CTX_LOCK(ctx); 4471 } 4472 } else 4473 reinit = 1; 4474 } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4475 iflib_stop(ctx); 4476 } 4477 ctx->ifc_if_flags = if_getflags(ifp); 4478 CTX_UNLOCK(ctx); 4479 break; 4480 case SIOCADDMULTI: 4481 case SIOCDELMULTI: 4482 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4483 CTX_LOCK(ctx); 4484 IFDI_INTR_DISABLE(ctx); 4485 IFDI_MULTI_SET(ctx); 4486 IFDI_INTR_ENABLE(ctx); 4487 CTX_UNLOCK(ctx); 4488 } 4489 break; 4490 case SIOCSIFMEDIA: 4491 CTX_LOCK(ctx); 4492 IFDI_MEDIA_SET(ctx); 4493 CTX_UNLOCK(ctx); 4494 /* FALLTHROUGH */ 4495 case SIOCGIFMEDIA: 4496 case SIOCGIFXMEDIA: 4497 err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command); 4498 break; 4499 case SIOCGI2C: 4500 /* FALLTHROUGH */ 4501 case SIOCGI2CPB: 4502 { 4503 struct ifi2creq i2c; 4504 if_shared_ctx_t sctx = ctx->ifc_sctx; 4505 4506 err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c)); 4507 if (err != 0) 4508 break; 4509 if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) { 4510 err = EINVAL; 4511 break; 4512 } 4513 if (i2c.len > sizeof(i2c.data)) { 4514 err = EINVAL; 4515 break; 4516 } 4517 if (command == SIOCGI2C) { 4518 i2c.page = i2c.bank = 0; 4519 } else if ((sctx->isc_flags & IFLIB_I2C_PAGE_BANK) == 0) { 4520 err = EINVAL; 4521 break; 4522 } 4523 4524 if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0) 4525 err = copyout(&i2c, ifr_data_get_ptr(ifr), 4526 sizeof(i2c)); 4527 break; 4528 } 4529 case SIOCSIFCAP: 4530 { 4531 int mask, setmask, oldmask; 4532 4533 oldmask = if_getcapenable(ifp); 4534 mask = ifr->ifr_reqcap ^ oldmask; 4535 mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_MEXTPG; 4536 setmask = 0; 4537 #ifdef TCP_OFFLOAD 4538 setmask |= mask & (IFCAP_TOE4 | IFCAP_TOE6); 4539 #endif 4540 setmask |= (mask & IFCAP_FLAGS); 4541 setmask |= (mask & IFCAP_WOL); 4542 4543 /* 4544 * If any RX csum has changed, change all the ones that 4545 * are supported by the driver. 4546 */ 4547 if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 4548 setmask |= ctx->ifc_softc_ctx.isc_capabilities & 4549 (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 4550 } 4551 4552 /* 4553 * want to ensure that traffic has stopped before we change any of the flags 4554 */ 4555 if (setmask) { 4556 CTX_LOCK(ctx); 4557 bits = if_getdrvflags(ifp); 4558 if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 4559 iflib_stop(ctx); 4560 STATE_LOCK(ctx); 4561 if_togglecapenable(ifp, setmask); 4562 ctx->ifc_softc_ctx.isc_capenable ^= setmask; 4563 STATE_UNLOCK(ctx); 4564 if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 4565 iflib_init_locked(ctx); 4566 STATE_LOCK(ctx); 4567 if_setdrvflags(ifp, bits); 4568 STATE_UNLOCK(ctx); 4569 CTX_UNLOCK(ctx); 4570 } 4571 if_vlancap(ifp); 4572 break; 4573 } 4574 case SIOCGPRIVATE_0: 4575 case SIOCSDRVSPEC: 4576 case SIOCGDRVSPEC: 4577 CTX_LOCK(ctx); 4578 err = IFDI_PRIV_IOCTL(ctx, command, data); 4579 CTX_UNLOCK(ctx); 4580 break; 4581 case SIOCGIFDOWNREASON: 4582 CTX_LOCK(ctx); 4583 err = IFDI_GET_DOWNREASON(ctx, (struct ifdownreason *)data); 4584 CTX_UNLOCK(ctx); 4585 break; 4586 default: 4587 err = ether_ioctl(ifp, command, data); 4588 break; 4589 } 4590 if (reinit) 4591 iflib_if_init(ctx); 4592 return (err); 4593 } 4594 4595 static uint64_t 4596 iflib_if_get_counter(if_t ifp, ift_counter cnt) 4597 { 4598 if_ctx_t ctx = if_getsoftc(ifp); 4599 4600 return (IFDI_GET_COUNTER(ctx, cnt)); 4601 } 4602 4603 /********************************************************************* 4604 * 4605 * OTHER FUNCTIONS EXPORTED TO THE STACK 4606 * 4607 **********************************************************************/ 4608 4609 static void 4610 iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag) 4611 { 4612 if_ctx_t ctx = if_getsoftc(ifp); 4613 4614 if ((void *)ctx != arg) 4615 return; 4616 4617 if ((vtag == 0) || (vtag > 4095)) 4618 return; 4619 4620 if (iflib_in_detach(ctx)) 4621 return; 4622 4623 CTX_LOCK(ctx); 4624 /* Driver may need all untagged packets to be flushed */ 4625 if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 4626 iflib_stop(ctx); 4627 IFDI_VLAN_REGISTER(ctx, vtag); 4628 /* Re-init to load the changes, if required */ 4629 if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 4630 iflib_init_locked(ctx); 4631 CTX_UNLOCK(ctx); 4632 } 4633 4634 static void 4635 iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag) 4636 { 4637 if_ctx_t ctx = if_getsoftc(ifp); 4638 4639 if ((void *)ctx != arg) 4640 return; 4641 4642 if ((vtag == 0) || (vtag > 4095)) 4643 return; 4644 4645 CTX_LOCK(ctx); 4646 /* Driver may need all tagged packets to be flushed */ 4647 if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 4648 iflib_stop(ctx); 4649 IFDI_VLAN_UNREGISTER(ctx, vtag); 4650 /* Re-init to load the changes, if required */ 4651 if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 4652 iflib_init_locked(ctx); 4653 CTX_UNLOCK(ctx); 4654 } 4655 4656 static void 4657 iflib_led_func(void *arg, int onoff) 4658 { 4659 if_ctx_t ctx = arg; 4660 4661 CTX_LOCK(ctx); 4662 IFDI_LED_FUNC(ctx, onoff); 4663 CTX_UNLOCK(ctx); 4664 } 4665 4666 /********************************************************************* 4667 * 4668 * BUS FUNCTION DEFINITIONS 4669 * 4670 **********************************************************************/ 4671 4672 int 4673 iflib_device_probe(device_t dev) 4674 { 4675 const pci_vendor_info_t *ent; 4676 if_shared_ctx_t sctx; 4677 uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id; 4678 uint16_t pci_vendor_id; 4679 4680 if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 4681 return (ENOTSUP); 4682 4683 pci_vendor_id = pci_get_vendor(dev); 4684 pci_device_id = pci_get_device(dev); 4685 pci_subvendor_id = pci_get_subvendor(dev); 4686 pci_subdevice_id = pci_get_subdevice(dev); 4687 pci_rev_id = pci_get_revid(dev); 4688 if (sctx->isc_parse_devinfo != NULL) 4689 sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id); 4690 4691 ent = sctx->isc_vendor_info; 4692 while (ent->pvi_vendor_id != 0) { 4693 if (pci_vendor_id != ent->pvi_vendor_id) { 4694 ent++; 4695 continue; 4696 } 4697 if ((pci_device_id == ent->pvi_device_id) && 4698 ((pci_subvendor_id == ent->pvi_subvendor_id) || 4699 (ent->pvi_subvendor_id == 0)) && 4700 ((pci_subdevice_id == ent->pvi_subdevice_id) || 4701 (ent->pvi_subdevice_id == 0)) && 4702 ((pci_rev_id == ent->pvi_rev_id) || 4703 (ent->pvi_rev_id == 0))) { 4704 device_set_desc_copy(dev, ent->pvi_name); 4705 /* this needs to be changed to zero if the bus probing code 4706 * ever stops re-probing on best match because the sctx 4707 * may have its values over written by register calls 4708 * in subsequent probes 4709 */ 4710 return (BUS_PROBE_DEFAULT); 4711 } 4712 ent++; 4713 } 4714 return (ENXIO); 4715 } 4716 4717 int 4718 iflib_device_probe_vendor(device_t dev) 4719 { 4720 int probe; 4721 4722 probe = iflib_device_probe(dev); 4723 if (probe == BUS_PROBE_DEFAULT) 4724 return (BUS_PROBE_VENDOR); 4725 else 4726 return (probe); 4727 } 4728 4729 static void 4730 iflib_reset_qvalues(if_ctx_t ctx) 4731 { 4732 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 4733 if_shared_ctx_t sctx = ctx->ifc_sctx; 4734 device_t dev = ctx->ifc_dev; 4735 int i; 4736 4737 if (ctx->ifc_sysctl_ntxqs != 0) 4738 scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs; 4739 if (ctx->ifc_sysctl_nrxqs != 0) 4740 scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs; 4741 4742 for (i = 0; i < sctx->isc_ntxqs; i++) { 4743 if (ctx->ifc_sysctl_ntxds[i] != 0) 4744 scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i]; 4745 else 4746 scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 4747 } 4748 4749 for (i = 0; i < sctx->isc_nrxqs; i++) { 4750 if (ctx->ifc_sysctl_nrxds[i] != 0) 4751 scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i]; 4752 else 4753 scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 4754 } 4755 4756 for (i = 0; i < sctx->isc_nrxqs; i++) { 4757 if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) { 4758 device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n", 4759 i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]); 4760 scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i]; 4761 } 4762 if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) { 4763 device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n", 4764 i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]); 4765 scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i]; 4766 } 4767 if (!powerof2(scctx->isc_nrxd[i])) { 4768 device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n", 4769 i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]); 4770 scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 4771 } 4772 } 4773 4774 for (i = 0; i < sctx->isc_ntxqs; i++) { 4775 if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) { 4776 device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n", 4777 i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]); 4778 scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i]; 4779 } 4780 if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) { 4781 device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n", 4782 i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]); 4783 scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i]; 4784 } 4785 if (!powerof2(scctx->isc_ntxd[i])) { 4786 device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n", 4787 i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]); 4788 scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 4789 } 4790 } 4791 scctx->isc_tx_pad = 2; 4792 } 4793 4794 static void 4795 iflib_add_pfil(if_ctx_t ctx) 4796 { 4797 struct pfil_head *pfil; 4798 struct pfil_head_args pa; 4799 iflib_rxq_t rxq; 4800 int i; 4801 4802 pa.pa_version = PFIL_VERSION; 4803 pa.pa_flags = PFIL_IN; 4804 pa.pa_type = PFIL_TYPE_ETHERNET; 4805 pa.pa_headname = if_name(ctx->ifc_ifp); 4806 pfil = pfil_head_register(&pa); 4807 4808 for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 4809 rxq->pfil = pfil; 4810 } 4811 } 4812 4813 static void 4814 iflib_rem_pfil(if_ctx_t ctx) 4815 { 4816 struct pfil_head *pfil; 4817 iflib_rxq_t rxq; 4818 int i; 4819 4820 rxq = ctx->ifc_rxqs; 4821 pfil = rxq->pfil; 4822 for (i = 0; i < NRXQSETS(ctx); i++, rxq++) { 4823 rxq->pfil = NULL; 4824 } 4825 pfil_head_unregister(pfil); 4826 } 4827 4828 4829 /* 4830 * Advance forward by n members of the cpuset ctx->ifc_cpus starting from 4831 * cpuid and wrapping as necessary. 4832 */ 4833 static unsigned int 4834 cpuid_advance(if_ctx_t ctx, unsigned int cpuid, unsigned int n) 4835 { 4836 unsigned int first_valid; 4837 unsigned int last_valid; 4838 4839 /* cpuid should always be in the valid set */ 4840 MPASS(CPU_ISSET(cpuid, &ctx->ifc_cpus)); 4841 4842 /* valid set should never be empty */ 4843 MPASS(!CPU_EMPTY(&ctx->ifc_cpus)); 4844 4845 first_valid = CPU_FFS(&ctx->ifc_cpus) - 1; 4846 last_valid = CPU_FLS(&ctx->ifc_cpus) - 1; 4847 n = n % CPU_COUNT(&ctx->ifc_cpus); 4848 while (n > 0) { 4849 do { 4850 cpuid++; 4851 if (cpuid > last_valid) 4852 cpuid = first_valid; 4853 } while (!CPU_ISSET(cpuid, &ctx->ifc_cpus)); 4854 n--; 4855 } 4856 4857 return (cpuid); 4858 } 4859 4860 /* 4861 * CPU mapping behaviors 4862 * --------------------- 4863 * 'separate txrx' refers to the separate_txrx sysctl 4864 * 'use logical' refers to the use_logical_cores sysctl 4865 * 'INTR CPUS' indicates whether bus_get_cpus(INTR_CPUS) succeeded 4866 * 4867 * separate use INTR 4868 * txrx logical CPUS result 4869 * ---------- --------- ------ ------------------------------------------------ 4870 * - - X RX and TX queues mapped to consecutive physical 4871 * cores with RX/TX pairs on same core and excess 4872 * of either following 4873 * - X X RX and TX queues mapped to consecutive cores 4874 * of any type with RX/TX pairs on same core and 4875 * excess of either following 4876 * X - X RX and TX queues mapped to consecutive physical 4877 * cores; all RX then all TX 4878 * X X X RX queues mapped to consecutive physical cores 4879 * first, then TX queues mapped to L2 neighbor of 4880 * the corresponding RX queue if one exists, 4881 * otherwise to consecutive physical cores 4882 * - n/a - RX and TX queues mapped to consecutive cores of 4883 * any type with RX/TX pairs on same core and excess 4884 * of either following 4885 * X n/a - RX and TX queues mapped to consecutive cores of 4886 * any type; all RX then all TX 4887 */ 4888 static unsigned int 4889 get_cpuid_for_queue(if_ctx_t ctx, unsigned int base_cpuid, unsigned int qid, 4890 bool is_tx) 4891 { 4892 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 4893 unsigned int core_index; 4894 4895 if (ctx->ifc_sysctl_separate_txrx) { 4896 /* 4897 * When using separate CPUs for TX and RX, the assignment 4898 * will always be of a consecutive CPU out of the set of 4899 * context CPUs, except for the specific case where the 4900 * context CPUs are phsyical cores, the use of logical cores 4901 * has been enabled, the assignment is for TX, the TX qid 4902 * corresponds to an RX qid, and the CPU assigned to the 4903 * corresponding RX queue has an L2 neighbor. 4904 */ 4905 if (ctx->ifc_sysctl_use_logical_cores && 4906 ctx->ifc_cpus_are_physical_cores && 4907 is_tx && qid < scctx->isc_nrxqsets) { 4908 int l2_neighbor; 4909 unsigned int rx_cpuid; 4910 4911 rx_cpuid = cpuid_advance(ctx, base_cpuid, qid); 4912 l2_neighbor = sched_find_l2_neighbor(rx_cpuid); 4913 if (l2_neighbor != -1) { 4914 return (l2_neighbor); 4915 } 4916 /* 4917 * ... else fall through to the normal 4918 * consecutive-after-RX assignment scheme. 4919 * 4920 * Note that we are assuming that all RX queue CPUs 4921 * have an L2 neighbor, or all do not. If a mixed 4922 * scenario is possible, we will have to keep track 4923 * separately of how many queues prior to this one 4924 * were not able to be assigned to an L2 neighbor. 4925 */ 4926 } 4927 if (is_tx) 4928 core_index = scctx->isc_nrxqsets + qid; 4929 else 4930 core_index = qid; 4931 } else { 4932 core_index = qid; 4933 } 4934 4935 return (cpuid_advance(ctx, base_cpuid, core_index)); 4936 } 4937 4938 static uint16_t 4939 get_ctx_core_offset(if_ctx_t ctx) 4940 { 4941 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 4942 struct cpu_offset *op; 4943 cpuset_t assigned_cpus; 4944 unsigned int cores_consumed; 4945 unsigned int base_cpuid = ctx->ifc_sysctl_core_offset; 4946 unsigned int first_valid; 4947 unsigned int last_valid; 4948 unsigned int i; 4949 4950 first_valid = CPU_FFS(&ctx->ifc_cpus) - 1; 4951 last_valid = CPU_FLS(&ctx->ifc_cpus) - 1; 4952 4953 if (base_cpuid != CORE_OFFSET_UNSPECIFIED) { 4954 /* 4955 * Align the user-chosen base CPU ID to the next valid CPU 4956 * for this device. If the chosen base CPU ID is smaller 4957 * than the first valid CPU or larger than the last valid 4958 * CPU, we assume the user does not know what the valid 4959 * range is for this device and is thinking in terms of a 4960 * zero-based reference frame, and so we shift the given 4961 * value into the valid range (and wrap accordingly) so the 4962 * intent is translated to the proper frame of reference. 4963 * If the base CPU ID is within the valid first/last, but 4964 * does not correspond to a valid CPU, it is advanced to the 4965 * next valid CPU (wrapping if necessary). 4966 */ 4967 if (base_cpuid < first_valid || base_cpuid > last_valid) { 4968 /* shift from zero-based to first_valid-based */ 4969 base_cpuid += first_valid; 4970 /* wrap to range [first_valid, last_valid] */ 4971 base_cpuid = (base_cpuid - first_valid) % 4972 (last_valid - first_valid + 1); 4973 } 4974 if (!CPU_ISSET(base_cpuid, &ctx->ifc_cpus)) { 4975 /* 4976 * base_cpuid is in [first_valid, last_valid], but 4977 * not a member of the valid set. In this case, 4978 * there will always be a member of the valid set 4979 * with a CPU ID that is greater than base_cpuid, 4980 * and we simply advance to it. 4981 */ 4982 while (!CPU_ISSET(base_cpuid, &ctx->ifc_cpus)) 4983 base_cpuid++; 4984 } 4985 return (base_cpuid); 4986 } 4987 4988 /* 4989 * Determine how many cores will be consumed by performing the CPU 4990 * assignments and counting how many of the assigned CPUs correspond 4991 * to CPUs in the set of context CPUs. This is done using the CPU 4992 * ID first_valid as the base CPU ID, as the base CPU must be within 4993 * the set of context CPUs. 4994 * 4995 * Note not all assigned CPUs will be in the set of context CPUs 4996 * when separate CPUs are being allocated to TX and RX queues, 4997 * assignment to logical cores has been enabled, the set of context 4998 * CPUs contains only physical CPUs, and TX queues are mapped to L2 4999 * neighbors of CPUs that RX queues have been mapped to - in this 5000 * case we do only want to count how many CPUs in the set of context 5001 * CPUs have been consumed, as that determines the next CPU in that 5002 * set to start allocating at for the next device for which 5003 * core_offset is not set. 5004 */ 5005 CPU_ZERO(&assigned_cpus); 5006 for (i = 0; i < scctx->isc_ntxqsets; i++) 5007 CPU_SET(get_cpuid_for_queue(ctx, first_valid, i, true), 5008 &assigned_cpus); 5009 for (i = 0; i < scctx->isc_nrxqsets; i++) 5010 CPU_SET(get_cpuid_for_queue(ctx, first_valid, i, false), 5011 &assigned_cpus); 5012 CPU_AND(&assigned_cpus, &assigned_cpus, &ctx->ifc_cpus); 5013 cores_consumed = CPU_COUNT(&assigned_cpus); 5014 5015 mtx_lock(&cpu_offset_mtx); 5016 SLIST_FOREACH(op, &cpu_offsets, entries) { 5017 if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 5018 base_cpuid = op->next_cpuid; 5019 op->next_cpuid = cpuid_advance(ctx, op->next_cpuid, 5020 cores_consumed); 5021 MPASS(op->refcount < UINT_MAX); 5022 op->refcount++; 5023 break; 5024 } 5025 } 5026 if (base_cpuid == CORE_OFFSET_UNSPECIFIED) { 5027 base_cpuid = first_valid; 5028 op = malloc(sizeof(struct cpu_offset), M_IFLIB, 5029 M_NOWAIT | M_ZERO); 5030 if (op == NULL) { 5031 device_printf(ctx->ifc_dev, 5032 "allocation for cpu offset failed.\n"); 5033 } else { 5034 op->next_cpuid = cpuid_advance(ctx, base_cpuid, 5035 cores_consumed); 5036 op->refcount = 1; 5037 CPU_COPY(&ctx->ifc_cpus, &op->set); 5038 SLIST_INSERT_HEAD(&cpu_offsets, op, entries); 5039 } 5040 } 5041 mtx_unlock(&cpu_offset_mtx); 5042 5043 return (base_cpuid); 5044 } 5045 5046 static void 5047 unref_ctx_core_offset(if_ctx_t ctx) 5048 { 5049 struct cpu_offset *op, *top; 5050 5051 mtx_lock(&cpu_offset_mtx); 5052 SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) { 5053 if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 5054 MPASS(op->refcount > 0); 5055 op->refcount--; 5056 if (op->refcount == 0) { 5057 SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries); 5058 free(op, M_IFLIB); 5059 } 5060 break; 5061 } 5062 } 5063 mtx_unlock(&cpu_offset_mtx); 5064 } 5065 5066 int 5067 iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp) 5068 { 5069 if_ctx_t ctx; 5070 if_t ifp; 5071 if_softc_ctx_t scctx; 5072 kobjop_desc_t kobj_desc; 5073 kobj_method_t *kobj_method; 5074 int err, msix, rid; 5075 int num_txd, num_rxd; 5076 char namebuf[TASKQUEUE_NAMELEN]; 5077 5078 ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK | M_ZERO); 5079 5080 if (sc == NULL) { 5081 sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK | M_ZERO); 5082 device_set_softc(dev, ctx); 5083 ctx->ifc_flags |= IFC_SC_ALLOCATED; 5084 } 5085 5086 ctx->ifc_sctx = sctx; 5087 ctx->ifc_dev = dev; 5088 ctx->ifc_softc = sc; 5089 5090 iflib_register(ctx); 5091 iflib_add_device_sysctl_pre(ctx); 5092 5093 scctx = &ctx->ifc_softc_ctx; 5094 ifp = ctx->ifc_ifp; 5095 if (ctx->ifc_sysctl_simple_tx) { 5096 #ifndef ALTQ 5097 if_settransmitfn(ifp, iflib_simple_transmit); 5098 device_printf(dev, "using simple if_transmit\n"); 5099 #else 5100 device_printf(dev, "ALTQ prevents using simple if_transmit\n"); 5101 #endif 5102 } 5103 iflib_reset_qvalues(ctx); 5104 IFNET_WLOCK(); 5105 CTX_LOCK(ctx); 5106 if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 5107 device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 5108 goto fail_unlock; 5109 } 5110 _iflib_pre_assert(scctx); 5111 ctx->ifc_txrx = *scctx->isc_txrx; 5112 5113 MPASS(scctx->isc_dma_width <= flsll(BUS_SPACE_MAXADDR)); 5114 5115 if (sctx->isc_flags & IFLIB_DRIVER_MEDIA) 5116 ctx->ifc_mediap = scctx->isc_media; 5117 5118 #ifdef INVARIANTS 5119 if (scctx->isc_capabilities & IFCAP_TXCSUM) 5120 MPASS(scctx->isc_tx_csum_flags); 5121 #endif 5122 5123 if_setcapabilities(ifp, 5124 scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_MEXTPG); 5125 if_setcapenable(ifp, 5126 scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_MEXTPG); 5127 5128 if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 5129 scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 5130 if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 5131 scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 5132 5133 num_txd = iflib_num_tx_descs(ctx); 5134 num_rxd = iflib_num_rx_descs(ctx); 5135 5136 /* XXX change for per-queue sizes */ 5137 device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 5138 num_txd, num_rxd); 5139 5140 if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION) 5141 scctx->isc_tx_nsegments = max(1, num_txd / 5142 MAX_SINGLE_PACKET_FRACTION); 5143 if (scctx->isc_tx_tso_segments_max > num_txd / 5144 MAX_SINGLE_PACKET_FRACTION) 5145 scctx->isc_tx_tso_segments_max = max(1, 5146 num_txd / MAX_SINGLE_PACKET_FRACTION); 5147 5148 /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 5149 if (if_getcapabilities(ifp) & IFCAP_TSO) { 5150 /* 5151 * The stack can't handle a TSO size larger than IP_MAXPACKET, 5152 * but some MACs do. 5153 */ 5154 if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 5155 IP_MAXPACKET)); 5156 /* 5157 * Take maximum number of m_pullup(9)'s in iflib_parse_header() 5158 * into account. In the worst case, each of these calls will 5159 * add another mbuf and, thus, the requirement for another DMA 5160 * segment. So for best performance, it doesn't make sense to 5161 * advertize a maximum of TSO segments that typically will 5162 * require defragmentation in iflib_encap(). 5163 */ 5164 if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 5165 if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 5166 } 5167 if (scctx->isc_rss_table_size == 0) 5168 scctx->isc_rss_table_size = 64; 5169 scctx->isc_rss_table_mask = scctx->isc_rss_table_size - 1; 5170 5171 /* Create and start admin taskqueue */ 5172 snprintf(namebuf, TASKQUEUE_NAMELEN, "if_%s_tq", device_get_nameunit(dev)); 5173 ctx->ifc_tq = taskqueue_create_fast(namebuf, M_NOWAIT, 5174 taskqueue_thread_enqueue, &ctx->ifc_tq); 5175 if (ctx->ifc_tq == NULL) { 5176 device_printf(dev, "Unable to create admin taskqueue\n"); 5177 return (ENOMEM); 5178 } 5179 5180 err = taskqueue_start_threads(&ctx->ifc_tq, 1, PI_NET, "%s", namebuf); 5181 if (err) { 5182 device_printf(dev, 5183 "Unable to start admin taskqueue threads error: %d\n", 5184 err); 5185 taskqueue_free(ctx->ifc_tq); 5186 return (err); 5187 } 5188 5189 TASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 5190 5191 /* Set up cpu set. If it fails, use the set of all CPUs. */ 5192 if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) { 5193 device_printf(dev, "Unable to fetch CPU list\n"); 5194 CPU_COPY(&all_cpus, &ctx->ifc_cpus); 5195 ctx->ifc_cpus_are_physical_cores = false; 5196 } else 5197 ctx->ifc_cpus_are_physical_cores = true; 5198 MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0); 5199 5200 /* 5201 * Now set up MSI or MSI-X, should return us the number of supported 5202 * vectors (will be 1 for a legacy interrupt and MSI). 5203 */ 5204 if (sctx->isc_flags & IFLIB_SKIP_MSIX) { 5205 msix = scctx->isc_vectors; 5206 } else if (scctx->isc_msix_bar != 0) 5207 /* 5208 * The simple fact that isc_msix_bar is not 0 does not mean we 5209 * we have a good value there that is known to work. 5210 */ 5211 msix = iflib_msix_init(ctx); 5212 else { 5213 scctx->isc_vectors = 1; 5214 scctx->isc_ntxqsets = 1; 5215 scctx->isc_nrxqsets = 1; 5216 scctx->isc_intr = IFLIB_INTR_LEGACY; 5217 msix = 0; 5218 } 5219 /* Get memory for the station queues */ 5220 if ((err = iflib_queues_alloc(ctx))) { 5221 device_printf(dev, "Unable to allocate queue memory\n"); 5222 goto fail_intr_free; 5223 } 5224 5225 if ((err = iflib_qset_structures_setup(ctx))) 5226 goto fail_queues; 5227 5228 /* 5229 * Now that we know how many queues there are, get the core offset. 5230 */ 5231 ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx); 5232 5233 if (msix > 1) { 5234 /* 5235 * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable 5236 * aren't the default NULL implementation. 5237 */ 5238 kobj_desc = &ifdi_rx_queue_intr_enable_desc; 5239 kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 5240 kobj_desc); 5241 if (kobj_method == &kobj_desc->deflt) { 5242 device_printf(dev, 5243 "MSI-X requires ifdi_rx_queue_intr_enable method"); 5244 err = EOPNOTSUPP; 5245 goto fail_queues; 5246 } 5247 kobj_desc = &ifdi_tx_queue_intr_enable_desc; 5248 kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 5249 kobj_desc); 5250 if (kobj_method == &kobj_desc->deflt) { 5251 device_printf(dev, 5252 "MSI-X requires ifdi_tx_queue_intr_enable method"); 5253 err = EOPNOTSUPP; 5254 goto fail_queues; 5255 } 5256 5257 /* 5258 * Assign the MSI-X vectors. 5259 * Note that the default NULL ifdi_msix_intr_assign method will 5260 * fail here, too. 5261 */ 5262 err = IFDI_MSIX_INTR_ASSIGN(ctx, msix); 5263 if (err != 0) { 5264 device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", 5265 err); 5266 goto fail_queues; 5267 } 5268 } else if (scctx->isc_intr != IFLIB_INTR_MSIX) { 5269 rid = 0; 5270 if (scctx->isc_intr == IFLIB_INTR_MSI) { 5271 MPASS(msix == 1); 5272 rid = 1; 5273 } 5274 if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) { 5275 device_printf(dev, "iflib_legacy_setup failed %d\n", err); 5276 goto fail_queues; 5277 } 5278 } else { 5279 device_printf(dev, 5280 "Cannot use iflib with only 1 MSI-X interrupt!\n"); 5281 err = ENODEV; 5282 goto fail_queues; 5283 } 5284 5285 /* 5286 * It prevents a double-locking panic with iflib_media_status when 5287 * the driver loads. 5288 */ 5289 CTX_UNLOCK(ctx); 5290 ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 5291 CTX_LOCK(ctx); 5292 5293 if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 5294 device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 5295 goto fail_detach; 5296 } 5297 5298 /* 5299 * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 5300 * This must appear after the call to ether_ifattach() because 5301 * ether_ifattach() sets if_hdrlen to the default value. 5302 */ 5303 if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 5304 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 5305 5306 if ((err = iflib_netmap_attach(ctx))) { 5307 device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err); 5308 goto fail_detach; 5309 } 5310 *ctxp = ctx; 5311 5312 DEBUGNET_SET(ctx->ifc_ifp, iflib); 5313 5314 iflib_add_device_sysctl_post(ctx); 5315 iflib_add_pfil(ctx); 5316 ctx->ifc_flags |= IFC_INIT_DONE; 5317 CTX_UNLOCK(ctx); 5318 IFNET_WUNLOCK(); 5319 5320 return (0); 5321 5322 fail_detach: 5323 CTX_UNLOCK(ctx); 5324 taskqueue_drain(ctx->ifc_tq, &ctx->ifc_admin_task); 5325 ether_ifdetach(ctx->ifc_ifp); 5326 CTX_LOCK(ctx); 5327 fail_queues: 5328 sysctl_ctx_free(&ctx->ifc_sysctl_ctx); 5329 ctx->ifc_sysctl_node = NULL; 5330 /* 5331 * Drain without holding CTX_LOCK so _task_fn_admin can run to 5332 * completion if it needs the context lock. On fail_detach we already 5333 * drained above; a second drain is a no-op when the queue is empty. 5334 */ 5335 CTX_UNLOCK(ctx); 5336 taskqueue_drain(ctx->ifc_tq, &ctx->ifc_admin_task); 5337 CTX_LOCK(ctx); 5338 iflib_tqg_detach(ctx); 5339 iflib_tx_structures_free(ctx); 5340 iflib_rx_structures_free(ctx); 5341 /* 5342 * Match iflib_device_deregister: IFDI_DETACH before taskqueue_free. 5343 * Avoid IFNET_WLOCK across driver detach (LinuxKPI workqueue drain). 5344 */ 5345 IFNET_WUNLOCK(); 5346 IFDI_DETACH(ctx); 5347 IFDI_QUEUES_FREE(ctx); 5348 IFNET_WLOCK(); 5349 taskqueue_free(ctx->ifc_tq); 5350 fail_intr_free: 5351 iflib_free_intr_mem(ctx); 5352 fail_unlock: 5353 CTX_UNLOCK(ctx); 5354 IFNET_WUNLOCK(); 5355 iflib_deregister(ctx); 5356 device_set_softc(ctx->ifc_dev, NULL); 5357 if (ctx->ifc_flags & IFC_SC_ALLOCATED) 5358 free(ctx->ifc_softc, M_IFLIB); 5359 free(ctx, M_IFLIB); 5360 return (err); 5361 } 5362 5363 int 5364 iflib_device_attach(device_t dev) 5365 { 5366 if_ctx_t ctx; 5367 if_shared_ctx_t sctx; 5368 5369 if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 5370 return (ENOTSUP); 5371 5372 pci_enable_busmaster(dev); 5373 5374 return (iflib_device_register(dev, NULL, sctx, &ctx)); 5375 } 5376 5377 int 5378 iflib_device_deregister(if_ctx_t ctx) 5379 { 5380 if_t ifp = ctx->ifc_ifp; 5381 device_t dev = ctx->ifc_dev; 5382 5383 sysctl_ctx_free(&ctx->ifc_sysctl_ctx); 5384 ctx->ifc_sysctl_node = NULL; 5385 5386 /* Make sure VLANS are not using driver */ 5387 if (if_vlantrunkinuse(ifp)) { 5388 device_printf(dev, "Vlan in use, detach first\n"); 5389 return (EBUSY); 5390 } 5391 #ifdef PCI_IOV 5392 if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) { 5393 device_printf(dev, "SR-IOV in use; detach first.\n"); 5394 return (EBUSY); 5395 } 5396 #endif 5397 5398 STATE_LOCK(ctx); 5399 ctx->ifc_flags |= IFC_IN_DETACH; 5400 STATE_UNLOCK(ctx); 5401 5402 /* Unregister VLAN handlers before calling iflib_stop() */ 5403 iflib_unregister_vlan_handlers(ctx); 5404 5405 iflib_netmap_detach(ifp); 5406 ether_ifdetach(ifp); 5407 5408 CTX_LOCK(ctx); 5409 iflib_stop(ctx); 5410 CTX_UNLOCK(ctx); 5411 5412 iflib_rem_pfil(ctx); 5413 if (ctx->ifc_led_dev != NULL) 5414 led_destroy(ctx->ifc_led_dev); 5415 5416 iflib_tqg_detach(ctx); 5417 iflib_tx_structures_free(ctx); 5418 iflib_rx_structures_free(ctx); 5419 5420 CTX_LOCK(ctx); 5421 IFDI_DETACH(ctx); 5422 IFDI_QUEUES_FREE(ctx); 5423 CTX_UNLOCK(ctx); 5424 5425 taskqueue_free(ctx->ifc_tq); 5426 ctx->ifc_tq = NULL; 5427 5428 /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 5429 iflib_free_intr_mem(ctx); 5430 5431 bus_generic_detach(dev); 5432 5433 iflib_deregister(ctx); 5434 5435 device_set_softc(ctx->ifc_dev, NULL); 5436 if (ctx->ifc_flags & IFC_SC_ALLOCATED) 5437 free(ctx->ifc_softc, M_IFLIB); 5438 unref_ctx_core_offset(ctx); 5439 free(ctx, M_IFLIB); 5440 return (0); 5441 } 5442 5443 static void 5444 iflib_tqg_detach(if_ctx_t ctx) 5445 { 5446 iflib_txq_t txq; 5447 iflib_rxq_t rxq; 5448 int i; 5449 struct taskqgroup *tqg; 5450 5451 /* XXX drain any dependent tasks */ 5452 tqg = qgroup_if_io_tqg; 5453 for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 5454 callout_drain(&txq->ift_timer); 5455 #ifdef DEV_NETMAP 5456 callout_drain(&txq->ift_netmap_timer); 5457 #endif /* DEV_NETMAP */ 5458 if (txq->ift_task.gt_uniq != NULL) 5459 taskqgroup_detach(tqg, &txq->ift_task); 5460 } 5461 for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 5462 if (rxq->ifr_task.gt_uniq != NULL) 5463 taskqgroup_detach(tqg, &rxq->ifr_task); 5464 } 5465 } 5466 5467 static void 5468 iflib_free_intr_mem(if_ctx_t ctx) 5469 { 5470 5471 if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) { 5472 iflib_irq_free(ctx, &ctx->ifc_legacy_irq); 5473 } 5474 if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) { 5475 pci_release_msi(ctx->ifc_dev); 5476 } 5477 if (ctx->ifc_msix_mem != NULL) { 5478 bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY, 5479 rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem); 5480 ctx->ifc_msix_mem = NULL; 5481 } 5482 } 5483 5484 int 5485 iflib_device_detach(device_t dev) 5486 { 5487 if_ctx_t ctx = device_get_softc(dev); 5488 5489 return (iflib_device_deregister(ctx)); 5490 } 5491 5492 int 5493 iflib_device_suspend(device_t dev) 5494 { 5495 if_ctx_t ctx = device_get_softc(dev); 5496 5497 CTX_LOCK(ctx); 5498 IFDI_SUSPEND(ctx); 5499 CTX_UNLOCK(ctx); 5500 5501 return (bus_generic_suspend(dev)); 5502 } 5503 int 5504 iflib_device_shutdown(device_t dev) 5505 { 5506 if_ctx_t ctx = device_get_softc(dev); 5507 5508 CTX_LOCK(ctx); 5509 IFDI_SHUTDOWN(ctx); 5510 CTX_UNLOCK(ctx); 5511 5512 return (bus_generic_suspend(dev)); 5513 } 5514 5515 int 5516 iflib_device_resume(device_t dev) 5517 { 5518 if_ctx_t ctx = device_get_softc(dev); 5519 iflib_txq_t txq = ctx->ifc_txqs; 5520 5521 CTX_LOCK(ctx); 5522 IFDI_RESUME(ctx); 5523 iflib_if_init_locked(ctx); 5524 CTX_UNLOCK(ctx); 5525 for (int i = 0; i < NTXQSETS(ctx); i++, txq++) 5526 iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 5527 5528 return (bus_generic_resume(dev)); 5529 } 5530 5531 int 5532 iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params) 5533 { 5534 int error; 5535 if_ctx_t ctx = device_get_softc(dev); 5536 5537 CTX_LOCK(ctx); 5538 error = IFDI_IOV_INIT(ctx, num_vfs, params); 5539 CTX_UNLOCK(ctx); 5540 5541 return (error); 5542 } 5543 5544 void 5545 iflib_device_iov_uninit(device_t dev) 5546 { 5547 if_ctx_t ctx = device_get_softc(dev); 5548 5549 CTX_LOCK(ctx); 5550 IFDI_IOV_UNINIT(ctx); 5551 CTX_UNLOCK(ctx); 5552 } 5553 5554 int 5555 iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params) 5556 { 5557 int error; 5558 if_ctx_t ctx = device_get_softc(dev); 5559 5560 CTX_LOCK(ctx); 5561 error = IFDI_IOV_VF_ADD(ctx, vfnum, params); 5562 CTX_UNLOCK(ctx); 5563 5564 return (error); 5565 } 5566 5567 /********************************************************************* 5568 * 5569 * MODULE FUNCTION DEFINITIONS 5570 * 5571 **********************************************************************/ 5572 5573 /* 5574 * - Start a fast taskqueue thread for each core 5575 * - Start a taskqueue for control operations 5576 */ 5577 static int 5578 iflib_module_init(void) 5579 { 5580 iflib_timer_default = hz / 2; 5581 return (0); 5582 } 5583 5584 static int 5585 iflib_module_event_handler(module_t mod, int what, void *arg) 5586 { 5587 int err; 5588 5589 switch (what) { 5590 case MOD_LOAD: 5591 if ((err = iflib_module_init()) != 0) 5592 return (err); 5593 break; 5594 case MOD_UNLOAD: 5595 return (EBUSY); 5596 default: 5597 return (EOPNOTSUPP); 5598 } 5599 5600 return (0); 5601 } 5602 5603 /********************************************************************* 5604 * 5605 * PUBLIC FUNCTION DEFINITIONS 5606 * ordered as in iflib.h 5607 * 5608 **********************************************************************/ 5609 5610 static void 5611 _iflib_assert(if_shared_ctx_t sctx) 5612 { 5613 int i; 5614 5615 MPASS(sctx->isc_tx_maxsize); 5616 MPASS(sctx->isc_tx_maxsegsize); 5617 5618 MPASS(sctx->isc_rx_maxsize); 5619 MPASS(sctx->isc_rx_nsegments); 5620 MPASS(sctx->isc_rx_maxsegsize); 5621 5622 MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8); 5623 for (i = 0; i < sctx->isc_nrxqs; i++) { 5624 MPASS(sctx->isc_nrxd_min[i]); 5625 MPASS(powerof2(sctx->isc_nrxd_min[i])); 5626 MPASS(sctx->isc_nrxd_max[i]); 5627 MPASS(powerof2(sctx->isc_nrxd_max[i])); 5628 MPASS(sctx->isc_nrxd_default[i]); 5629 MPASS(powerof2(sctx->isc_nrxd_default[i])); 5630 } 5631 5632 MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8); 5633 for (i = 0; i < sctx->isc_ntxqs; i++) { 5634 MPASS(sctx->isc_ntxd_min[i]); 5635 MPASS(powerof2(sctx->isc_ntxd_min[i])); 5636 MPASS(sctx->isc_ntxd_max[i]); 5637 MPASS(powerof2(sctx->isc_ntxd_max[i])); 5638 MPASS(sctx->isc_ntxd_default[i]); 5639 MPASS(powerof2(sctx->isc_ntxd_default[i])); 5640 } 5641 } 5642 5643 static void 5644 _iflib_pre_assert(if_softc_ctx_t scctx) 5645 { 5646 5647 MPASS(scctx->isc_txrx->ift_txd_encap); 5648 MPASS(scctx->isc_txrx->ift_txd_flush); 5649 MPASS(scctx->isc_txrx->ift_txd_credits_update); 5650 MPASS(scctx->isc_txrx->ift_rxd_available); 5651 MPASS(scctx->isc_txrx->ift_rxd_pkt_get); 5652 MPASS(scctx->isc_txrx->ift_rxd_refill); 5653 MPASS(scctx->isc_txrx->ift_rxd_flush); 5654 } 5655 5656 static void 5657 iflib_register(if_ctx_t ctx) 5658 { 5659 if_shared_ctx_t sctx = ctx->ifc_sctx; 5660 driver_t *driver = sctx->isc_driver; 5661 device_t dev = ctx->ifc_dev; 5662 if_t ifp; 5663 5664 _iflib_assert(sctx); 5665 5666 CTX_LOCK_INIT(ctx); 5667 STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev)); 5668 ifp = ctx->ifc_ifp = if_alloc_dev(IFT_ETHER, dev); 5669 5670 /* 5671 * Initialize our context's device specific methods 5672 */ 5673 kobj_init((kobj_t) ctx, (kobj_class_t) driver); 5674 kobj_class_compile((kobj_class_t) driver); 5675 5676 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 5677 if_setsoftc(ifp, ctx); 5678 if_setdev(ifp, dev); 5679 if_setinitfn(ifp, iflib_if_init); 5680 if_setioctlfn(ifp, iflib_if_ioctl); 5681 #ifdef ALTQ 5682 if_setstartfn(ifp, iflib_altq_if_start); 5683 if_settransmitfn(ifp, iflib_altq_if_transmit); 5684 if_setsendqready(ifp); 5685 #else 5686 if_settransmitfn(ifp, iflib_if_transmit); 5687 #endif 5688 if_setqflushfn(ifp, iflib_if_qflush); 5689 if_setgetcounterfn(ifp, iflib_if_get_counter); 5690 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 5691 ctx->ifc_vlan_attach_event = 5692 EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx, 5693 EVENTHANDLER_PRI_FIRST); 5694 ctx->ifc_vlan_detach_event = 5695 EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx, 5696 EVENTHANDLER_PRI_FIRST); 5697 5698 if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) { 5699 ctx->ifc_mediap = &ctx->ifc_media; 5700 ifmedia_init(ctx->ifc_mediap, IFM_IMASK, 5701 iflib_media_change, iflib_media_status); 5702 } 5703 } 5704 5705 static void 5706 iflib_unregister_vlan_handlers(if_ctx_t ctx) 5707 { 5708 /* Unregister VLAN events */ 5709 if (ctx->ifc_vlan_attach_event != NULL) { 5710 EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 5711 ctx->ifc_vlan_attach_event = NULL; 5712 } 5713 if (ctx->ifc_vlan_detach_event != NULL) { 5714 EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 5715 ctx->ifc_vlan_detach_event = NULL; 5716 } 5717 5718 } 5719 5720 static void 5721 iflib_deregister(if_ctx_t ctx) 5722 { 5723 if_t ifp = ctx->ifc_ifp; 5724 5725 /* Remove all media */ 5726 ifmedia_removeall(&ctx->ifc_media); 5727 5728 /* Ensure that VLAN event handlers are unregistered */ 5729 iflib_unregister_vlan_handlers(ctx); 5730 5731 /* Release kobject reference */ 5732 kobj_delete((kobj_t) ctx, NULL); 5733 5734 /* Free the ifnet structure */ 5735 if_free(ifp); 5736 5737 STATE_LOCK_DESTROY(ctx); 5738 5739 /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 5740 CTX_LOCK_DESTROY(ctx); 5741 } 5742 5743 static int 5744 iflib_queues_alloc(if_ctx_t ctx) 5745 { 5746 if_shared_ctx_t sctx = ctx->ifc_sctx; 5747 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 5748 device_t dev = ctx->ifc_dev; 5749 int nrxqsets = scctx->isc_nrxqsets; 5750 int ntxqsets = scctx->isc_ntxqsets; 5751 iflib_txq_t txq; 5752 iflib_rxq_t rxq; 5753 iflib_fl_t fl = NULL; 5754 int i, j, cpu, err, txconf, rxconf; 5755 iflib_dma_info_t ifdip; 5756 uint32_t *rxqsizes = scctx->isc_rxqsizes; 5757 uint32_t *txqsizes = scctx->isc_txqsizes; 5758 uint8_t nrxqs = sctx->isc_nrxqs; 5759 uint8_t ntxqs = sctx->isc_ntxqs; 5760 int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1; 5761 int fl_offset = (sctx->isc_flags & IFLIB_HAS_RXCQ ? 1 : 0); 5762 caddr_t *vaddrs; 5763 uint64_t *paddrs; 5764 5765 KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1")); 5766 KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1")); 5767 KASSERT(nrxqs >= fl_offset + nfree_lists, 5768 ("there must be at least a rxq for each free list")); 5769 5770 /* Allocate the TX ring struct memory */ 5771 if (!(ctx->ifc_txqs = 5772 (iflib_txq_t) malloc(sizeof(struct iflib_txq) * 5773 ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 5774 device_printf(dev, "Unable to allocate TX ring memory\n"); 5775 err = ENOMEM; 5776 goto fail; 5777 } 5778 5779 /* Now allocate the RX */ 5780 if (!(ctx->ifc_rxqs = 5781 (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) * 5782 nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 5783 device_printf(dev, "Unable to allocate RX ring memory\n"); 5784 err = ENOMEM; 5785 goto rx_fail; 5786 } 5787 5788 txq = ctx->ifc_txqs; 5789 rxq = ctx->ifc_rxqs; 5790 5791 /* 5792 * XXX handle allocation failure 5793 */ 5794 for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) { 5795 /* Set up some basics */ 5796 5797 if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, 5798 M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5799 device_printf(dev, 5800 "Unable to allocate TX DMA info memory\n"); 5801 err = ENOMEM; 5802 goto err_tx_desc; 5803 } 5804 txq->ift_ifdi = ifdip; 5805 for (j = 0; j < ntxqs; j++, ifdip++) { 5806 if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) { 5807 device_printf(dev, 5808 "Unable to allocate TX descriptors\n"); 5809 err = ENOMEM; 5810 goto err_tx_desc; 5811 } 5812 txq->ift_txd_size[j] = scctx->isc_txd_size[j]; 5813 bzero((void *)ifdip->idi_vaddr, txqsizes[j]); 5814 } 5815 txq->ift_ctx = ctx; 5816 txq->ift_id = i; 5817 if (sctx->isc_flags & IFLIB_HAS_TXCQ) { 5818 txq->ift_br_offset = 1; 5819 } else { 5820 txq->ift_br_offset = 0; 5821 } 5822 5823 if (iflib_txsd_alloc(txq)) { 5824 device_printf(dev, "Critical Failure setting up TX buffers\n"); 5825 err = ENOMEM; 5826 goto err_tx_desc; 5827 } 5828 5829 /* Initialize the TX lock */ 5830 snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout", 5831 device_get_nameunit(dev), txq->ift_id); 5832 mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF); 5833 callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0); 5834 txq->ift_timer.c_cpu = cpu; 5835 #ifdef DEV_NETMAP 5836 callout_init_mtx(&txq->ift_netmap_timer, &txq->ift_mtx, 0); 5837 txq->ift_netmap_timer.c_cpu = cpu; 5838 #endif /* DEV_NETMAP */ 5839 5840 err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain, 5841 iflib_txq_can_drain, M_IFLIB, M_WAITOK); 5842 if (err) { 5843 /* XXX free any allocated rings */ 5844 device_printf(dev, "Unable to allocate buf_ring\n"); 5845 goto err_tx_desc; 5846 } 5847 txq->ift_reclaim_thresh = ctx->ifc_sysctl_tx_reclaim_thresh; 5848 } 5849 5850 for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) { 5851 /* Set up some basics */ 5852 callout_init(&rxq->ifr_watchdog, 1); 5853 5854 if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, 5855 M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5856 device_printf(dev, 5857 "Unable to allocate RX DMA info memory\n"); 5858 err = ENOMEM; 5859 goto err_tx_desc; 5860 } 5861 5862 rxq->ifr_ifdi = ifdip; 5863 /* XXX this needs to be changed if #rx queues != #tx queues */ 5864 rxq->ifr_ntxqirq = 1; 5865 rxq->ifr_txqid[0] = i; 5866 for (j = 0; j < nrxqs; j++, ifdip++) { 5867 if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) { 5868 device_printf(dev, 5869 "Unable to allocate RX descriptors\n"); 5870 err = ENOMEM; 5871 goto err_tx_desc; 5872 } 5873 bzero((void *)ifdip->idi_vaddr, rxqsizes[j]); 5874 } 5875 rxq->ifr_ctx = ctx; 5876 rxq->ifr_id = i; 5877 rxq->ifr_fl_offset = fl_offset; 5878 rxq->ifr_nfl = nfree_lists; 5879 if (!(fl = 5880 (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) { 5881 device_printf(dev, "Unable to allocate free list memory\n"); 5882 err = ENOMEM; 5883 goto err_tx_desc; 5884 } 5885 rxq->ifr_fl = fl; 5886 for (j = 0; j < nfree_lists; j++) { 5887 fl[j].ifl_rxq = rxq; 5888 fl[j].ifl_id = j; 5889 fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset]; 5890 fl[j].ifl_rxd_size = scctx->isc_rxd_size[j]; 5891 } 5892 /* Allocate receive buffers for the ring */ 5893 if (iflib_rxsd_alloc(rxq)) { 5894 device_printf(dev, 5895 "Critical Failure setting up receive buffers\n"); 5896 err = ENOMEM; 5897 goto err_rx_desc; 5898 } 5899 5900 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 5901 fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, 5902 M_WAITOK); 5903 } 5904 5905 /* TXQs */ 5906 vaddrs = malloc(sizeof(caddr_t) * ntxqsets * ntxqs, M_IFLIB, M_WAITOK); 5907 paddrs = malloc(sizeof(uint64_t) * ntxqsets * ntxqs, M_IFLIB, M_WAITOK); 5908 for (i = 0; i < ntxqsets; i++) { 5909 iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi; 5910 5911 for (j = 0; j < ntxqs; j++, di++) { 5912 vaddrs[i * ntxqs + j] = di->idi_vaddr; 5913 paddrs[i * ntxqs + j] = di->idi_paddr; 5914 } 5915 } 5916 if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) { 5917 device_printf(ctx->ifc_dev, 5918 "Unable to allocate device TX queue\n"); 5919 iflib_tx_structures_free(ctx); 5920 free(vaddrs, M_IFLIB); 5921 free(paddrs, M_IFLIB); 5922 goto err_rx_desc; 5923 } 5924 free(vaddrs, M_IFLIB); 5925 free(paddrs, M_IFLIB); 5926 5927 /* RXQs */ 5928 vaddrs = malloc(sizeof(caddr_t) * nrxqsets * nrxqs, M_IFLIB, M_WAITOK); 5929 paddrs = malloc(sizeof(uint64_t) * nrxqsets * nrxqs, M_IFLIB, M_WAITOK); 5930 for (i = 0; i < nrxqsets; i++) { 5931 iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi; 5932 5933 for (j = 0; j < nrxqs; j++, di++) { 5934 vaddrs[i * nrxqs + j] = di->idi_vaddr; 5935 paddrs[i * nrxqs + j] = di->idi_paddr; 5936 } 5937 } 5938 if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) { 5939 device_printf(ctx->ifc_dev, 5940 "Unable to allocate device RX queue\n"); 5941 iflib_tx_structures_free(ctx); 5942 free(vaddrs, M_IFLIB); 5943 free(paddrs, M_IFLIB); 5944 goto err_rx_desc; 5945 } 5946 free(vaddrs, M_IFLIB); 5947 free(paddrs, M_IFLIB); 5948 5949 return (0); 5950 5951 /* XXX handle allocation failure changes */ 5952 err_rx_desc: 5953 err_tx_desc: 5954 rx_fail: 5955 if (ctx->ifc_rxqs != NULL) 5956 free(ctx->ifc_rxqs, M_IFLIB); 5957 ctx->ifc_rxqs = NULL; 5958 if (ctx->ifc_txqs != NULL) 5959 free(ctx->ifc_txqs, M_IFLIB); 5960 ctx->ifc_txqs = NULL; 5961 fail: 5962 return (err); 5963 } 5964 5965 static int 5966 iflib_tx_structures_setup(if_ctx_t ctx) 5967 { 5968 iflib_txq_t txq = ctx->ifc_txqs; 5969 int i; 5970 5971 for (i = 0; i < NTXQSETS(ctx); i++, txq++) 5972 iflib_txq_setup(txq); 5973 5974 return (0); 5975 } 5976 5977 static void 5978 iflib_tx_structures_free(if_ctx_t ctx) 5979 { 5980 iflib_txq_t txq = ctx->ifc_txqs; 5981 if_shared_ctx_t sctx = ctx->ifc_sctx; 5982 int i, j; 5983 5984 for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 5985 for (j = 0; j < sctx->isc_ntxqs; j++) 5986 iflib_dma_free(&txq->ift_ifdi[j]); 5987 iflib_txq_destroy(txq); 5988 } 5989 free(ctx->ifc_txqs, M_IFLIB); 5990 ctx->ifc_txqs = NULL; 5991 } 5992 5993 /********************************************************************* 5994 * 5995 * Initialize all receive rings. 5996 * 5997 **********************************************************************/ 5998 static int 5999 iflib_rx_structures_setup(if_ctx_t ctx) 6000 { 6001 iflib_rxq_t rxq = ctx->ifc_rxqs; 6002 int q; 6003 #if defined(INET6) || defined(INET) 6004 int err, i; 6005 #endif 6006 6007 for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) { 6008 #if defined(INET6) || defined(INET) 6009 err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp, 6010 TCP_LRO_ENTRIES, min(1024, 6011 ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset])); 6012 if (err != 0) { 6013 device_printf(ctx->ifc_dev, 6014 "LRO Initialization failed!\n"); 6015 goto fail; 6016 } 6017 #endif 6018 IFDI_RXQ_SETUP(ctx, rxq->ifr_id); 6019 } 6020 return (0); 6021 #if defined(INET6) || defined(INET) 6022 fail: 6023 /* 6024 * Free LRO resources allocated so far, we will only handle 6025 * the rings that completed, the failing case will have 6026 * cleaned up for itself. 'q' failed, so its the terminus. 6027 */ 6028 rxq = ctx->ifc_rxqs; 6029 for (i = 0; i < q; ++i, rxq++) { 6030 tcp_lro_free(&rxq->ifr_lc); 6031 } 6032 return (err); 6033 #endif 6034 } 6035 6036 /********************************************************************* 6037 * 6038 * Free all receive rings. 6039 * 6040 **********************************************************************/ 6041 static void 6042 iflib_rx_structures_free(if_ctx_t ctx) 6043 { 6044 iflib_rxq_t rxq = ctx->ifc_rxqs; 6045 if_shared_ctx_t sctx = ctx->ifc_sctx; 6046 int i, j; 6047 6048 for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) { 6049 for (j = 0; j < sctx->isc_nrxqs; j++) 6050 iflib_dma_free(&rxq->ifr_ifdi[j]); 6051 iflib_rx_sds_free(rxq); 6052 #if defined(INET6) || defined(INET) 6053 tcp_lro_free(&rxq->ifr_lc); 6054 #endif 6055 } 6056 free(ctx->ifc_rxqs, M_IFLIB); 6057 ctx->ifc_rxqs = NULL; 6058 } 6059 6060 static int 6061 iflib_qset_structures_setup(if_ctx_t ctx) 6062 { 6063 int err; 6064 6065 /* 6066 * It is expected that the caller takes care of freeing queues if this 6067 * fails. 6068 */ 6069 if ((err = iflib_tx_structures_setup(ctx)) != 0) { 6070 device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err); 6071 return (err); 6072 } 6073 6074 if ((err = iflib_rx_structures_setup(ctx)) != 0) 6075 device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err); 6076 6077 return (err); 6078 } 6079 6080 int 6081 iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 6082 driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name) 6083 { 6084 6085 return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name)); 6086 } 6087 6088 /* Just to avoid copy/paste */ 6089 static inline int 6090 iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, 6091 int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, 6092 const char *name) 6093 { 6094 device_t dev; 6095 unsigned int base_cpuid, cpuid; 6096 int err; 6097 6098 dev = ctx->ifc_dev; 6099 base_cpuid = ctx->ifc_sysctl_core_offset; 6100 cpuid = get_cpuid_for_queue(ctx, base_cpuid, qid, type == IFLIB_INTR_TX); 6101 err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, 6102 irq ? irq->ii_res : NULL, name); 6103 if (err) { 6104 device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err); 6105 return (err); 6106 } 6107 #ifdef notyet 6108 if (cpuid > ctx->ifc_cpuid_highest) 6109 ctx->ifc_cpuid_highest = cpuid; 6110 #endif 6111 return (0); 6112 } 6113 6114 /* 6115 * Allocate a hardware interrupt for subctx using the parent (ctx)'s hardware 6116 * resources. 6117 * 6118 * Similar to iflib_irq_alloc_generic(), but for interrupt type IFLIB_INTR_RXTX 6119 * only. 6120 * 6121 * XXX: Could be removed if subctx's dev has its intr resource allocation 6122 * methods replaced with custom ones? 6123 */ 6124 int 6125 iflib_irq_alloc_generic_subctx(if_ctx_t ctx, if_ctx_t subctx, if_irq_t irq, 6126 int rid, iflib_intr_type_t type, 6127 driver_filter_t *filter, void *filter_arg, 6128 int qid, const char *name) 6129 { 6130 device_t dev, subdev; 6131 struct grouptask *gtask; 6132 struct taskqgroup *tqg; 6133 iflib_filter_info_t info; 6134 gtask_fn_t *fn; 6135 int tqrid, err; 6136 driver_filter_t *intr_fast; 6137 void *q; 6138 6139 MPASS(ctx != NULL); 6140 MPASS(subctx != NULL); 6141 6142 tqrid = rid; 6143 dev = ctx->ifc_dev; 6144 subdev = subctx->ifc_dev; 6145 6146 switch (type) { 6147 case IFLIB_INTR_RXTX: 6148 q = &subctx->ifc_rxqs[qid]; 6149 info = &subctx->ifc_rxqs[qid].ifr_filter_info; 6150 gtask = &subctx->ifc_rxqs[qid].ifr_task; 6151 tqg = qgroup_if_io_tqg; 6152 fn = _task_fn_rx; 6153 intr_fast = iflib_fast_intr_rxtx; 6154 NET_GROUPTASK_INIT(gtask, 0, fn, q); 6155 break; 6156 default: 6157 device_printf(dev, "%s: unknown net intr type for subctx %s (%d)\n", 6158 __func__, device_get_nameunit(subdev), type); 6159 return (EINVAL); 6160 } 6161 6162 info->ifi_filter = filter; 6163 info->ifi_filter_arg = filter_arg; 6164 info->ifi_task = gtask; 6165 info->ifi_ctx = q; 6166 6167 NET_GROUPTASK_INIT(gtask, 0, fn, q); 6168 6169 /* Allocate interrupts from hardware using parent context */ 6170 err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name); 6171 if (err != 0) { 6172 device_printf(dev, "_iflib_irq_alloc failed for subctx %s: %d\n", 6173 device_get_nameunit(subdev), err); 6174 return (err); 6175 } 6176 6177 if (tqrid != -1) { 6178 err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q, 6179 name); 6180 if (err) 6181 return (err); 6182 } else { 6183 taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name); 6184 } 6185 6186 return (0); 6187 } 6188 6189 int 6190 iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid, 6191 iflib_intr_type_t type, driver_filter_t *filter, 6192 void *filter_arg, int qid, const char *name) 6193 { 6194 device_t dev; 6195 struct grouptask *gtask; 6196 struct taskqgroup *tqg; 6197 iflib_filter_info_t info; 6198 gtask_fn_t *fn; 6199 int tqrid, err; 6200 driver_filter_t *intr_fast; 6201 void *q; 6202 6203 info = &ctx->ifc_filter_info; 6204 tqrid = rid; 6205 6206 switch (type) { 6207 /* XXX merge tx/rx for netmap? */ 6208 case IFLIB_INTR_TX: 6209 q = &ctx->ifc_txqs[qid]; 6210 info = &ctx->ifc_txqs[qid].ift_filter_info; 6211 gtask = &ctx->ifc_txqs[qid].ift_task; 6212 tqg = qgroup_if_io_tqg; 6213 fn = _task_fn_tx; 6214 intr_fast = iflib_fast_intr; 6215 GROUPTASK_INIT(gtask, 0, fn, q); 6216 ctx->ifc_flags |= IFC_NETMAP_TX_IRQ; 6217 break; 6218 case IFLIB_INTR_RX: 6219 q = &ctx->ifc_rxqs[qid]; 6220 info = &ctx->ifc_rxqs[qid].ifr_filter_info; 6221 gtask = &ctx->ifc_rxqs[qid].ifr_task; 6222 tqg = qgroup_if_io_tqg; 6223 fn = _task_fn_rx; 6224 intr_fast = iflib_fast_intr; 6225 NET_GROUPTASK_INIT(gtask, 0, fn, q); 6226 break; 6227 case IFLIB_INTR_RXTX: 6228 q = &ctx->ifc_rxqs[qid]; 6229 info = &ctx->ifc_rxqs[qid].ifr_filter_info; 6230 gtask = &ctx->ifc_rxqs[qid].ifr_task; 6231 tqg = qgroup_if_io_tqg; 6232 fn = _task_fn_rx; 6233 intr_fast = iflib_fast_intr_rxtx; 6234 NET_GROUPTASK_INIT(gtask, 0, fn, q); 6235 break; 6236 case IFLIB_INTR_ADMIN: 6237 q = ctx; 6238 tqrid = -1; 6239 info = &ctx->ifc_filter_info; 6240 gtask = NULL; 6241 intr_fast = iflib_fast_intr_ctx; 6242 break; 6243 default: 6244 device_printf(ctx->ifc_dev, "%s: unknown net intr type\n", 6245 __func__); 6246 return (EINVAL); 6247 } 6248 6249 info->ifi_filter = filter; 6250 info->ifi_filter_arg = filter_arg; 6251 info->ifi_task = gtask; 6252 info->ifi_ctx = q; 6253 6254 dev = ctx->ifc_dev; 6255 err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name); 6256 if (err != 0) { 6257 device_printf(dev, "_iflib_irq_alloc failed %d\n", err); 6258 return (err); 6259 } 6260 if (type == IFLIB_INTR_ADMIN) 6261 return (0); 6262 6263 if (tqrid != -1) { 6264 err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q, 6265 name); 6266 if (err) 6267 return (err); 6268 } else { 6269 taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name); 6270 } 6271 6272 return (0); 6273 } 6274 6275 void 6276 iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, 6277 void *arg, int qid, const char *name) 6278 { 6279 device_t dev; 6280 struct grouptask *gtask; 6281 struct taskqgroup *tqg; 6282 gtask_fn_t *fn; 6283 void *q; 6284 int err; 6285 6286 switch (type) { 6287 case IFLIB_INTR_TX: 6288 q = &ctx->ifc_txqs[qid]; 6289 gtask = &ctx->ifc_txqs[qid].ift_task; 6290 tqg = qgroup_if_io_tqg; 6291 fn = _task_fn_tx; 6292 GROUPTASK_INIT(gtask, 0, fn, q); 6293 break; 6294 case IFLIB_INTR_RX: 6295 q = &ctx->ifc_rxqs[qid]; 6296 gtask = &ctx->ifc_rxqs[qid].ifr_task; 6297 tqg = qgroup_if_io_tqg; 6298 fn = _task_fn_rx; 6299 NET_GROUPTASK_INIT(gtask, 0, fn, q); 6300 break; 6301 case IFLIB_INTR_IOV: 6302 TASK_INIT(&ctx->ifc_vflr_task, 0, _task_fn_iov, ctx); 6303 return; 6304 default: 6305 panic("unknown net intr type"); 6306 } 6307 err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q, name); 6308 if (err) { 6309 dev = ctx->ifc_dev; 6310 taskqgroup_attach(tqg, gtask, q, dev, irq ? irq->ii_res : NULL, 6311 name); 6312 } 6313 } 6314 6315 void 6316 iflib_irq_free(if_ctx_t ctx, if_irq_t irq) 6317 { 6318 6319 if (irq->ii_tag) 6320 bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag); 6321 6322 if (irq->ii_res) 6323 bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, 6324 rman_get_rid(irq->ii_res), irq->ii_res); 6325 } 6326 6327 static int 6328 iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name) 6329 { 6330 iflib_txq_t txq = ctx->ifc_txqs; 6331 iflib_rxq_t rxq = ctx->ifc_rxqs; 6332 if_irq_t irq = &ctx->ifc_legacy_irq; 6333 iflib_filter_info_t info; 6334 device_t dev; 6335 struct grouptask *gtask; 6336 struct resource *res; 6337 int err, tqrid; 6338 bool rx_only; 6339 6340 info = &rxq->ifr_filter_info; 6341 gtask = &rxq->ifr_task; 6342 tqrid = *rid; 6343 rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0; 6344 6345 ctx->ifc_flags |= IFC_LEGACY; 6346 info->ifi_filter = filter; 6347 info->ifi_filter_arg = filter_arg; 6348 info->ifi_task = gtask; 6349 info->ifi_ctx = rxq; 6350 6351 dev = ctx->ifc_dev; 6352 /* We allocate a single interrupt resource */ 6353 err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr : 6354 iflib_fast_intr_rxtx, NULL, info, name); 6355 if (err != 0) 6356 return (err); 6357 NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, rxq); 6358 res = irq->ii_res; 6359 taskqgroup_attach(qgroup_if_io_tqg, gtask, rxq, dev, res, name); 6360 6361 GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq); 6362 taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res, 6363 "tx"); 6364 return (0); 6365 } 6366 6367 void 6368 iflib_led_create(if_ctx_t ctx) 6369 { 6370 6371 ctx->ifc_led_dev = led_create(iflib_led_func, ctx, 6372 device_get_nameunit(ctx->ifc_dev)); 6373 } 6374 6375 void 6376 iflib_tx_intr_deferred(if_ctx_t ctx, int txqid) 6377 { 6378 6379 GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task); 6380 } 6381 6382 void 6383 iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid) 6384 { 6385 6386 GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task); 6387 } 6388 6389 void 6390 iflib_admin_intr_deferred(if_ctx_t ctx) 6391 { 6392 6393 taskqueue_enqueue(ctx->ifc_tq, &ctx->ifc_admin_task); 6394 } 6395 6396 void 6397 iflib_iov_intr_deferred(if_ctx_t ctx) 6398 { 6399 6400 taskqueue_enqueue(ctx->ifc_tq, &ctx->ifc_vflr_task); 6401 } 6402 6403 void 6404 iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name) 6405 { 6406 6407 taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL, 6408 name); 6409 } 6410 6411 void 6412 iflib_config_task_init(if_ctx_t ctx, struct task *config_task, task_fn_t *fn) 6413 { 6414 TASK_INIT(config_task, 0, fn, ctx); 6415 } 6416 6417 void 6418 iflib_config_task_enqueue(if_ctx_t ctx, struct task *config_task) 6419 { 6420 taskqueue_enqueue(ctx->ifc_tq, config_task); 6421 } 6422 6423 void 6424 iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate) 6425 { 6426 if_t ifp = ctx->ifc_ifp; 6427 iflib_txq_t txq = ctx->ifc_txqs; 6428 6429 if_setbaudrate(ifp, baudrate); 6430 if (baudrate >= IF_Gbps(10)) { 6431 STATE_LOCK(ctx); 6432 ctx->ifc_flags |= IFC_PREFETCH; 6433 STATE_UNLOCK(ctx); 6434 } 6435 /* If link down, disable watchdog */ 6436 if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) { 6437 for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++) 6438 txq->ift_qstatus = IFLIB_QUEUE_IDLE; 6439 } 6440 ctx->ifc_link_state = link_state; 6441 if_link_state_change(ifp, link_state); 6442 } 6443 6444 static int 6445 iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq) 6446 { 6447 int credits; 6448 #ifdef INVARIANTS 6449 int credits_pre = txq->ift_cidx_processed; 6450 #endif 6451 6452 bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 6453 BUS_DMASYNC_POSTREAD); 6454 if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0) 6455 return (0); 6456 6457 txq->ift_processed += credits; 6458 txq->ift_cidx_processed += credits; 6459 6460 MPASS(credits_pre + credits == txq->ift_cidx_processed); 6461 if (txq->ift_cidx_processed >= txq->ift_size) 6462 txq->ift_cidx_processed -= txq->ift_size; 6463 return (credits); 6464 } 6465 6466 static int 6467 iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget) 6468 { 6469 iflib_fl_t fl; 6470 u_int i; 6471 6472 for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++) 6473 bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 6474 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 6475 return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx, 6476 budget)); 6477 } 6478 6479 void 6480 iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name, 6481 const char *description, if_int_delay_info_t info, 6482 int offset, int value) 6483 { 6484 info->iidi_ctx = ctx; 6485 info->iidi_offset = offset; 6486 info->iidi_value = value; 6487 SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev), 6488 SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)), 6489 OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 6490 info, 0, iflib_sysctl_int_delay, "I", description); 6491 } 6492 6493 struct sx * 6494 iflib_ctx_lock_get(if_ctx_t ctx) 6495 { 6496 6497 return (&ctx->ifc_ctx_sx); 6498 } 6499 6500 static int 6501 iflib_msix_init(if_ctx_t ctx) 6502 { 6503 device_t dev = ctx->ifc_dev; 6504 if_shared_ctx_t sctx = ctx->ifc_sctx; 6505 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 6506 int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues; 6507 int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors; 6508 6509 iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs; 6510 iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs; 6511 6512 if (bootverbose) 6513 device_printf(dev, "msix_init qsets capped at %d\n", 6514 imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets)); 6515 6516 /* Override by tuneable */ 6517 if (scctx->isc_disable_msix) 6518 goto msi; 6519 6520 /* First try MSI-X */ 6521 if ((msgs = pci_msix_count(dev)) == 0) { 6522 if (bootverbose) 6523 device_printf(dev, "MSI-X not supported or disabled\n"); 6524 goto msi; 6525 } 6526 6527 bar = ctx->ifc_softc_ctx.isc_msix_bar; 6528 /* 6529 * bar == -1 => "trust me I know what I'm doing" 6530 * Some drivers are for hardware that is so shoddily 6531 * documented that no one knows which bars are which 6532 * so the developer has to map all bars. This hack 6533 * allows shoddy garbage to use MSI-X in this framework. 6534 */ 6535 if (bar != -1) { 6536 ctx->ifc_msix_mem = bus_alloc_resource_any(dev, 6537 SYS_RES_MEMORY, &bar, RF_ACTIVE); 6538 if (ctx->ifc_msix_mem == NULL) { 6539 device_printf(dev, "Unable to map MSI-X table\n"); 6540 goto msi; 6541 } 6542 } 6543 6544 admincnt = sctx->isc_admin_intrcnt; 6545 #if IFLIB_DEBUG 6546 /* use only 1 qset in debug mode */ 6547 queuemsgs = min(msgs - admincnt, 1); 6548 #else 6549 queuemsgs = msgs - admincnt; 6550 #endif 6551 #ifdef RSS 6552 queues = imin(queuemsgs, rss_getnumbuckets()); 6553 #else 6554 queues = queuemsgs; 6555 #endif 6556 queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues); 6557 if (bootverbose) 6558 device_printf(dev, 6559 "intr CPUs: %d queue msgs: %d admincnt: %d\n", 6560 CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt); 6561 #ifdef RSS 6562 /* If we're doing RSS, clamp at the number of RSS buckets */ 6563 if (queues > rss_getnumbuckets()) 6564 queues = rss_getnumbuckets(); 6565 #endif 6566 if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt) 6567 rx_queues = iflib_num_rx_queues; 6568 else 6569 rx_queues = queues; 6570 6571 if (rx_queues > scctx->isc_nrxqsets) 6572 rx_queues = scctx->isc_nrxqsets; 6573 6574 /* 6575 * We want this to be all logical CPUs by default 6576 */ 6577 if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues) 6578 tx_queues = iflib_num_tx_queues; 6579 else 6580 tx_queues = mp_ncpus; 6581 6582 if (tx_queues > scctx->isc_ntxqsets) 6583 tx_queues = scctx->isc_ntxqsets; 6584 6585 if (ctx->ifc_sysctl_qs_eq_override == 0) { 6586 #ifdef INVARIANTS 6587 if (tx_queues != rx_queues) 6588 device_printf(dev, 6589 "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n", 6590 min(rx_queues, tx_queues), min(rx_queues, tx_queues)); 6591 #endif 6592 tx_queues = min(rx_queues, tx_queues); 6593 rx_queues = min(rx_queues, tx_queues); 6594 } 6595 6596 vectors = rx_queues + admincnt; 6597 if (msgs < vectors) { 6598 device_printf(dev, 6599 "insufficient number of MSI-X vectors " 6600 "(supported %d, need %d)\n", msgs, vectors); 6601 goto msi; 6602 } 6603 6604 device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues, 6605 tx_queues); 6606 msgs = vectors; 6607 if ((err = pci_alloc_msix(dev, &vectors)) == 0) { 6608 if (vectors != msgs) { 6609 device_printf(dev, 6610 "Unable to allocate sufficient MSI-X vectors " 6611 "(got %d, need %d)\n", vectors, msgs); 6612 pci_release_msi(dev); 6613 if (bar != -1) { 6614 bus_release_resource(dev, SYS_RES_MEMORY, bar, 6615 ctx->ifc_msix_mem); 6616 ctx->ifc_msix_mem = NULL; 6617 } 6618 goto msi; 6619 } 6620 device_printf(dev, "Using MSI-X interrupts with %d vectors\n", 6621 vectors); 6622 scctx->isc_vectors = vectors; 6623 scctx->isc_nrxqsets = rx_queues; 6624 scctx->isc_ntxqsets = tx_queues; 6625 scctx->isc_intr = IFLIB_INTR_MSIX; 6626 6627 return (vectors); 6628 } else { 6629 device_printf(dev, 6630 "failed to allocate %d MSI-X vectors, err: %d\n", vectors, 6631 err); 6632 if (bar != -1) { 6633 bus_release_resource(dev, SYS_RES_MEMORY, bar, 6634 ctx->ifc_msix_mem); 6635 ctx->ifc_msix_mem = NULL; 6636 } 6637 } 6638 6639 msi: 6640 vectors = pci_msi_count(dev); 6641 scctx->isc_nrxqsets = 1; 6642 scctx->isc_ntxqsets = 1; 6643 scctx->isc_vectors = vectors; 6644 if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) { 6645 device_printf(dev, "Using an MSI interrupt\n"); 6646 scctx->isc_intr = IFLIB_INTR_MSI; 6647 } else { 6648 scctx->isc_vectors = 1; 6649 device_printf(dev, "Using a Legacy interrupt\n"); 6650 scctx->isc_intr = IFLIB_INTR_LEGACY; 6651 } 6652 6653 return (vectors); 6654 } 6655 6656 static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" }; 6657 6658 static int 6659 mp_ring_state_handler(SYSCTL_HANDLER_ARGS) 6660 { 6661 int rc; 6662 uint16_t *state = ((uint16_t *)oidp->oid_arg1); 6663 struct sbuf *sb; 6664 const char *ring_state = "UNKNOWN"; 6665 6666 /* XXX needed ? */ 6667 rc = sysctl_wire_old_buffer(req, 0); 6668 MPASS(rc == 0); 6669 if (rc != 0) 6670 return (rc); 6671 sb = sbuf_new_for_sysctl(NULL, NULL, 80, req); 6672 MPASS(sb != NULL); 6673 if (sb == NULL) 6674 return (ENOMEM); 6675 if (state[3] <= 3) 6676 ring_state = ring_states[state[3]]; 6677 6678 sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s", 6679 state[0], state[1], state[2], ring_state); 6680 rc = sbuf_finish(sb); 6681 sbuf_delete(sb); 6682 return (rc); 6683 } 6684 6685 enum iflib_ndesc_handler { 6686 IFLIB_NTXD_HANDLER, 6687 IFLIB_NRXD_HANDLER, 6688 }; 6689 6690 static int 6691 mp_ndesc_handler(SYSCTL_HANDLER_ARGS) 6692 { 6693 if_ctx_t ctx = (void *)arg1; 6694 enum iflib_ndesc_handler type = arg2; 6695 char buf[256] = {0}; 6696 qidx_t *ndesc; 6697 char *p, *next; 6698 int nqs, rc, i; 6699 6700 nqs = 8; 6701 switch (type) { 6702 case IFLIB_NTXD_HANDLER: 6703 ndesc = ctx->ifc_sysctl_ntxds; 6704 if (ctx->ifc_sctx) 6705 nqs = ctx->ifc_sctx->isc_ntxqs; 6706 break; 6707 case IFLIB_NRXD_HANDLER: 6708 ndesc = ctx->ifc_sysctl_nrxds; 6709 if (ctx->ifc_sctx) 6710 nqs = ctx->ifc_sctx->isc_nrxqs; 6711 break; 6712 default: 6713 printf("%s: unhandled type\n", __func__); 6714 return (EINVAL); 6715 } 6716 if (nqs == 0) 6717 nqs = 8; 6718 6719 for (i = 0; i < 8; i++) { 6720 if (i >= nqs) 6721 break; 6722 if (i) 6723 strcat(buf, ","); 6724 sprintf(strchr(buf, 0), "%d", ndesc[i]); 6725 } 6726 6727 rc = sysctl_handle_string(oidp, buf, sizeof(buf), req); 6728 if (rc || req->newptr == NULL) 6729 return (rc); 6730 6731 for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p; 6732 i++, p = strsep(&next, " ,")) { 6733 ndesc[i] = strtoul(p, NULL, 10); 6734 } 6735 6736 return (rc); 6737 } 6738 6739 static int 6740 iflib_handle_tx_reclaim_thresh(SYSCTL_HANDLER_ARGS) 6741 { 6742 if_ctx_t ctx = (void *)arg1; 6743 iflib_txq_t txq; 6744 int i, err; 6745 int thresh; 6746 6747 thresh = ctx->ifc_sysctl_tx_reclaim_thresh; 6748 err = sysctl_handle_int(oidp, &thresh, arg2, req); 6749 if (err != 0) { 6750 return err; 6751 } 6752 6753 if (thresh == ctx->ifc_sysctl_tx_reclaim_thresh) 6754 return 0; 6755 6756 if (thresh > ctx->ifc_softc_ctx.isc_ntxd[0] / 2) { 6757 device_printf(ctx->ifc_dev, "TX Reclaim thresh must be <= %d\n", 6758 ctx->ifc_softc_ctx.isc_ntxd[0] / 2); 6759 return (EINVAL); 6760 } 6761 6762 ctx->ifc_sysctl_tx_reclaim_thresh = thresh; 6763 if (ctx->ifc_txqs == NULL) 6764 return (err); 6765 6766 txq = &ctx->ifc_txqs[0]; 6767 for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 6768 txq->ift_reclaim_thresh = thresh; 6769 } 6770 return (err); 6771 } 6772 6773 static int 6774 iflib_handle_tx_reclaim_ticks(SYSCTL_HANDLER_ARGS) 6775 { 6776 if_ctx_t ctx = (void *)arg1; 6777 iflib_txq_t txq; 6778 int i, err; 6779 int ticks; 6780 6781 ticks = ctx->ifc_sysctl_tx_reclaim_ticks; 6782 err = sysctl_handle_int(oidp, &ticks, arg2, req); 6783 if (err != 0) { 6784 return err; 6785 } 6786 6787 if (ticks == ctx->ifc_sysctl_tx_reclaim_ticks) 6788 return 0; 6789 6790 if (ticks > hz) { 6791 device_printf(ctx->ifc_dev, 6792 "TX Reclaim ticks must be <= hz (%d)\n", hz); 6793 return (EINVAL); 6794 } 6795 6796 ctx->ifc_sysctl_tx_reclaim_ticks = ticks; 6797 if (ctx->ifc_txqs == NULL) 6798 return (err); 6799 6800 txq = &ctx->ifc_txqs[0]; 6801 for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 6802 txq->ift_reclaim_ticks = ticks; 6803 } 6804 return (err); 6805 } 6806 6807 static int 6808 iflib_handle_tx_defer_mfree(SYSCTL_HANDLER_ARGS) 6809 { 6810 if_ctx_t ctx = (void *)arg1; 6811 iflib_txq_t txq; 6812 int i, err; 6813 int defer; 6814 6815 defer = ctx->ifc_sysctl_tx_defer_mfree; 6816 err = sysctl_handle_int(oidp, &defer, arg2, req); 6817 if (err != 0) { 6818 return err; 6819 } 6820 6821 if (defer == ctx->ifc_sysctl_tx_defer_mfree) 6822 return 0; 6823 6824 ctx->ifc_sysctl_tx_defer_mfree = defer; 6825 if (ctx->ifc_txqs == NULL) 6826 return (err); 6827 6828 txq = &ctx->ifc_txqs[0]; 6829 for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 6830 txq->ift_defer_mfree = defer; 6831 } 6832 return (err); 6833 } 6834 6835 #define NAME_BUFLEN 32 6836 static void 6837 iflib_add_device_sysctl_pre(if_ctx_t ctx) 6838 { 6839 device_t dev = iflib_get_dev(ctx); 6840 struct sysctl_oid_list *child, *oid_list; 6841 struct sysctl_oid *node; 6842 6843 sysctl_ctx_init(&ctx->ifc_sysctl_ctx); 6844 child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 6845 ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(&ctx->ifc_sysctl_ctx, child, 6846 OID_AUTO, "iflib", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 6847 "IFLIB fields"); 6848 oid_list = SYSCTL_CHILDREN(node); 6849 6850 SYSCTL_ADD_CONST_STRING(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "driver_version", 6851 CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, "driver version"); 6852 6853 SYSCTL_ADD_BOOL(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "simple_tx", 6854 CTLFLAG_RDTUN, &ctx->ifc_sysctl_simple_tx, 0, 6855 "use simple tx ring"); 6856 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_ntxqs", 6857 CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0, 6858 "# of txqs to use, 0 => use default #"); 6859 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_nrxqs", 6860 CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0, 6861 "# of rxqs to use, 0 => use default #"); 6862 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_qs_enable", 6863 CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0, 6864 "permit #txq != #rxq"); 6865 SYSCTL_ADD_INT(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "disable_msix", 6866 CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0, 6867 "disable MSI-X (default 0)"); 6868 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "rx_budget", 6869 CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, "set the RX budget"); 6870 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "tx_abdicate", 6871 CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0, 6872 "cause TX to abdicate instead of running to completion"); 6873 ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED; 6874 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "core_offset", 6875 CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0, 6876 "offset to start using cores at"); 6877 SYSCTL_ADD_U8(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "separate_txrx", 6878 CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0, 6879 "use separate cores for TX and RX"); 6880 SYSCTL_ADD_U8(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "use_logical_cores", 6881 CTLFLAG_RDTUN, &ctx->ifc_sysctl_use_logical_cores, 0, 6882 "try to make use of logical cores for TX and RX"); 6883 SYSCTL_ADD_U16(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "use_extra_msix_vectors", 6884 CTLFLAG_RDTUN, &ctx->ifc_sysctl_extra_msix_vectors, 0, 6885 "attempt to reserve the given number of extra MSI-X vectors during driver load for the creation of additional interfaces later"); 6886 SYSCTL_ADD_INT(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "allocated_msix_vectors", 6887 CTLFLAG_RDTUN, &ctx->ifc_softc_ctx.isc_vectors, 0, 6888 "total # of MSI-X vectors allocated by driver"); 6889 6890 /* XXX change for per-queue sizes */ 6891 SYSCTL_ADD_PROC(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_ntxds", 6892 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx, 6893 IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A", 6894 "list of # of TX descriptors to use, 0 = use default #"); 6895 SYSCTL_ADD_PROC(&ctx->ifc_sysctl_ctx, oid_list, OID_AUTO, "override_nrxds", 6896 CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx, 6897 IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A", 6898 "list of # of RX descriptors to use, 0 = use default #"); 6899 } 6900 6901 static void 6902 iflib_add_device_sysctl_post(if_ctx_t ctx) 6903 { 6904 if_shared_ctx_t sctx = ctx->ifc_sctx; 6905 if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 6906 struct sysctl_oid_list *child; 6907 struct sysctl_ctx_list *ctx_list = &ctx->ifc_sysctl_ctx; 6908 iflib_fl_t fl; 6909 iflib_txq_t txq; 6910 iflib_rxq_t rxq; 6911 int i, j; 6912 char namebuf[NAME_BUFLEN]; 6913 char *qfmt; 6914 struct sysctl_oid *queue_node, *fl_node, *node; 6915 struct sysctl_oid_list *queue_list, *fl_list; 6916 6917 node = ctx->ifc_sysctl_node; 6918 child = SYSCTL_CHILDREN(node); 6919 6920 SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "tx_reclaim_thresh", 6921 CTLTYPE_INT | CTLFLAG_RWTUN, ctx, 6922 0, iflib_handle_tx_reclaim_thresh, "I", 6923 "Number of TX descs outstanding before reclaim is called"); 6924 6925 SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "tx_reclaim_ticks", 6926 CTLTYPE_INT | CTLFLAG_RWTUN, ctx, 6927 0, iflib_handle_tx_reclaim_ticks, "I", 6928 "Number of ticks before a TX reclaim is forced"); 6929 6930 SYSCTL_ADD_PROC(ctx_list, child, OID_AUTO, "tx_defer_mfree", 6931 CTLTYPE_INT | CTLFLAG_RWTUN, ctx, 6932 0, iflib_handle_tx_defer_mfree, "I", 6933 "Free completed transmits outside of TX ring lock"); 6934 6935 if (scctx->isc_ntxqsets > 100) 6936 qfmt = "txq%03d"; 6937 else if (scctx->isc_ntxqsets > 10) 6938 qfmt = "txq%02d"; 6939 else 6940 qfmt = "txq%d"; 6941 for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) { 6942 snprintf(namebuf, NAME_BUFLEN, qfmt, i); 6943 queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 6944 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name"); 6945 queue_list = SYSCTL_CHILDREN(queue_node); 6946 SYSCTL_ADD_INT(ctx_list, queue_list, OID_AUTO, "cpu", 6947 CTLFLAG_RD, &txq->ift_task.gt_cpu, 0, 6948 "cpu this queue is bound to"); 6949 #if MEMORY_LOGGING 6950 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued", 6951 CTLFLAG_RD, &txq->ift_dequeued, "total mbufs freed"); 6952 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued", 6953 CTLFLAG_RD, &txq->ift_enqueued, "total mbufs enqueued"); 6954 #endif 6955 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag", 6956 CTLFLAG_RD, &txq->ift_mbuf_defrag, 6957 "# of times m_defrag was called"); 6958 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "m_pullups", 6959 CTLFLAG_RD, &txq->ift_pullups, 6960 "# of times m_pullup was called"); 6961 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, 6962 "mbuf_defrag_failed", CTLFLAG_RD, 6963 &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed"); 6964 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, 6965 "no_desc_avail", CTLFLAG_RD, &txq->ift_no_desc_avail, 6966 "# of times no descriptors were available"); 6967 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, 6968 "tx_map_failed", CTLFLAG_RD, &txq->ift_map_failed, 6969 "# of times DMA map failed"); 6970 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, 6971 "txd_encap_efbig", CTLFLAG_RD, &txq->ift_txd_encap_efbig, 6972 "# of times txd_encap returned EFBIG"); 6973 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, 6974 "no_tx_dma_setup", CTLFLAG_RD, &txq->ift_no_tx_dma_setup, 6975 "# of times map failed for other than EFBIG"); 6976 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx", 6977 CTLFLAG_RD, &txq->ift_pidx, 1, "Producer Index"); 6978 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx", 6979 CTLFLAG_RD, &txq->ift_cidx, 1, "Consumer Index"); 6980 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, 6981 "txq_cidx_processed", CTLFLAG_RD, &txq->ift_cidx_processed, 6982 1, "Consumer Index seen by credit update"); 6983 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use", 6984 CTLFLAG_RD, &txq->ift_in_use, 1, "descriptors in use"); 6985 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, 6986 "txq_processed", CTLFLAG_RD, &txq->ift_processed, 6987 "descriptors procesed for clean"); 6988 SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned", 6989 CTLFLAG_RD, &txq->ift_cleaned, "total cleaned"); 6990 SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state", 6991 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 6992 __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0, 6993 mp_ring_state_handler, "A", "soft ring state"); 6994 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, 6995 "r_enqueues", CTLFLAG_RD, &txq->ift_br->enqueues, 6996 "# of enqueues to the mp_ring for this queue"); 6997 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, 6998 "r_drops", CTLFLAG_RD, &txq->ift_br->drops, 6999 "# of drops in the mp_ring for this queue"); 7000 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, 7001 "r_starts", CTLFLAG_RD, &txq->ift_br->starts, 7002 "# of normal consumer starts in mp_ring for this queue"); 7003 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, 7004 "r_stalls", CTLFLAG_RD, &txq->ift_br->stalls, 7005 "# of consumer stalls in the mp_ring for this queue"); 7006 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, 7007 "r_restarts", CTLFLAG_RD, &txq->ift_br->restarts, 7008 "# of consumer restarts in the mp_ring for this queue"); 7009 SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, 7010 "r_abdications", CTLFLAG_RD, &txq->ift_br->abdications, 7011 "# of consumer abdications in the mp_ring for this queue"); 7012 } 7013 7014 if (scctx->isc_nrxqsets > 100) 7015 qfmt = "rxq%03d"; 7016 else if (scctx->isc_nrxqsets > 10) 7017 qfmt = "rxq%02d"; 7018 else 7019 qfmt = "rxq%d"; 7020 for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) { 7021 snprintf(namebuf, NAME_BUFLEN, qfmt, i); 7022 queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 7023 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name"); 7024 queue_list = SYSCTL_CHILDREN(queue_node); 7025 SYSCTL_ADD_INT(ctx_list, queue_list, OID_AUTO, "cpu", 7026 CTLFLAG_RD, &rxq->ifr_task.gt_cpu, 0, 7027 "cpu this queue is bound to"); 7028 if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 7029 SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, 7030 "rxq_cq_cidx", CTLFLAG_RD, &rxq->ifr_cq_cidx, 1, 7031 "Consumer Index"); 7032 } 7033 7034 for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 7035 snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j); 7036 fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, 7037 OID_AUTO, namebuf, CTLFLAG_RD | CTLFLAG_MPSAFE, 7038 NULL, "freelist Name"); 7039 fl_list = SYSCTL_CHILDREN(fl_node); 7040 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx", 7041 CTLFLAG_RD, &fl->ifl_pidx, 1, "Producer Index"); 7042 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx", 7043 CTLFLAG_RD, &fl->ifl_cidx, 1, "Consumer Index"); 7044 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits", 7045 CTLFLAG_RD, &fl->ifl_credits, 1, 7046 "credits available"); 7047 SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size", 7048 CTLFLAG_RD, &fl->ifl_buf_size, 1, "buffer size"); 7049 #if MEMORY_LOGGING 7050 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO, 7051 "fl_m_enqueued", CTLFLAG_RD, &fl->ifl_m_enqueued, 7052 "mbufs allocated"); 7053 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO, 7054 "fl_m_dequeued", CTLFLAG_RD, &fl->ifl_m_dequeued, 7055 "mbufs freed"); 7056 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO, 7057 "fl_cl_enqueued", CTLFLAG_RD, &fl->ifl_cl_enqueued, 7058 "clusters allocated"); 7059 SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO, 7060 "fl_cl_dequeued", CTLFLAG_RD, &fl->ifl_cl_dequeued, 7061 "clusters freed"); 7062 #endif 7063 } 7064 } 7065 7066 } 7067 7068 void 7069 iflib_request_reset(if_ctx_t ctx) 7070 { 7071 7072 STATE_LOCK(ctx); 7073 ctx->ifc_flags |= IFC_DO_RESET; 7074 STATE_UNLOCK(ctx); 7075 } 7076 7077 #ifndef __NO_STRICT_ALIGNMENT 7078 static struct mbuf * 7079 iflib_fixup_rx(struct mbuf *m) 7080 { 7081 struct mbuf *n; 7082 7083 if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) { 7084 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len); 7085 m->m_data += ETHER_HDR_LEN; 7086 n = m; 7087 } else { 7088 MGETHDR(n, M_NOWAIT, MT_DATA); 7089 if (n == NULL) { 7090 m_freem(m); 7091 return (NULL); 7092 } 7093 bcopy(m->m_data, n->m_data, ETHER_HDR_LEN); 7094 m->m_data += ETHER_HDR_LEN; 7095 m->m_len -= ETHER_HDR_LEN; 7096 n->m_len = ETHER_HDR_LEN; 7097 M_MOVE_PKTHDR(n, m); 7098 n->m_next = m; 7099 } 7100 return (n); 7101 } 7102 #endif 7103 7104 #ifdef DEBUGNET 7105 static void 7106 iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize) 7107 { 7108 if_ctx_t ctx; 7109 7110 ctx = if_getsoftc(ifp); 7111 CTX_LOCK(ctx); 7112 *nrxr = NRXQSETS(ctx); 7113 *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size; 7114 *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size; 7115 CTX_UNLOCK(ctx); 7116 } 7117 7118 static void 7119 iflib_debugnet_event(if_t ifp, enum debugnet_ev event) 7120 { 7121 if_ctx_t ctx; 7122 if_softc_ctx_t scctx; 7123 iflib_fl_t fl; 7124 iflib_rxq_t rxq; 7125 int i, j; 7126 7127 ctx = if_getsoftc(ifp); 7128 scctx = &ctx->ifc_softc_ctx; 7129 7130 switch (event) { 7131 case DEBUGNET_START: 7132 for (i = 0; i < scctx->isc_nrxqsets; i++) { 7133 rxq = &ctx->ifc_rxqs[i]; 7134 for (j = 0; j < rxq->ifr_nfl; j++) { 7135 fl = rxq->ifr_fl; 7136 fl->ifl_zone = m_getzone(fl->ifl_buf_size); 7137 } 7138 } 7139 iflib_no_tx_batch = 1; 7140 break; 7141 default: 7142 break; 7143 } 7144 } 7145 7146 static int 7147 iflib_debugnet_transmit(if_t ifp, struct mbuf *m) 7148 { 7149 if_ctx_t ctx; 7150 iflib_txq_t txq; 7151 int error; 7152 int bytes_sent = 0; 7153 int pkt_sent = 0; 7154 7155 ctx = if_getsoftc(ifp); 7156 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 7157 IFF_DRV_RUNNING) 7158 return (EBUSY); 7159 7160 txq = &ctx->ifc_txqs[0]; 7161 error = iflib_encap(txq, &m, &bytes_sent, &pkt_sent); 7162 if (error == 0) 7163 (void)iflib_txd_db_check(txq, true); 7164 return (error); 7165 } 7166 7167 static int 7168 iflib_debugnet_poll(if_t ifp, int count) 7169 { 7170 struct epoch_tracker et; 7171 if_ctx_t ctx; 7172 if_softc_ctx_t scctx; 7173 iflib_txq_t txq; 7174 int i; 7175 7176 ctx = if_getsoftc(ifp); 7177 scctx = &ctx->ifc_softc_ctx; 7178 7179 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 7180 IFF_DRV_RUNNING) 7181 return (EBUSY); 7182 7183 txq = &ctx->ifc_txqs[0]; 7184 (void)iflib_completed_tx_reclaim(txq, NULL); 7185 7186 NET_EPOCH_ENTER(et); 7187 for (i = 0; i < scctx->isc_nrxqsets; i++) 7188 (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */); 7189 NET_EPOCH_EXIT(et); 7190 return (0); 7191 } 7192 #endif /* DEBUGNET */ 7193 7194 #ifndef ALTQ 7195 static inline iflib_txq_t 7196 iflib_simple_select_queue(if_ctx_t ctx, struct mbuf *m) 7197 { 7198 int qidx; 7199 7200 if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m)) 7201 qidx = QIDX(ctx, m); 7202 else 7203 qidx = NTXQSETS(ctx) + FIRST_QSET(ctx) - 1; 7204 return (&ctx->ifc_txqs[qidx]); 7205 } 7206 7207 static int 7208 iflib_simple_transmit(if_t ifp, struct mbuf *m) 7209 { 7210 if_ctx_t ctx; 7211 iflib_txq_t txq; 7212 struct mbuf **m_defer; 7213 int error, i, reclaimable; 7214 int bytes_sent = 0, pkt_sent = 0, mcast_sent = 0; 7215 7216 7217 ctx = if_getsoftc(ifp); 7218 if (__predict_false((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 7219 || !LINK_ACTIVE(ctx))) { 7220 DBG_COUNTER_INC(tx_frees); 7221 m_freem(m); 7222 return (ENETDOWN); 7223 } 7224 7225 txq = iflib_simple_select_queue(ctx, m); 7226 mtx_lock(&txq->ift_mtx); 7227 error = iflib_encap(txq, &m, &bytes_sent, &pkt_sent); 7228 if (error == 0) { 7229 mcast_sent += !!(m->m_flags & M_MCAST); 7230 (void)iflib_txd_db_check(txq, true); 7231 } else { 7232 if (error == ENOBUFS) 7233 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 7234 else 7235 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 7236 } 7237 m_defer = NULL; 7238 reclaimable = iflib_txq_can_reclaim(txq); 7239 if (reclaimable != 0) { 7240 /* 7241 * Try to set m_defer to the deferred mbuf reclaim array. If 7242 * we can, the frees will happen outside the tx lock. If we 7243 * can't, it means another thread is still proccessing frees. 7244 */ 7245 if (txq->ift_defer_mfree && 7246 atomic_cmpset_acq_ptr((uintptr_t *)&txq->ift_sds.ifsd_m_defer, 7247 (uintptr_t )txq->ift_sds.ifsd_m_deferb, 0)) { 7248 m_defer = txq->ift_sds.ifsd_m_deferb; 7249 } 7250 _iflib_completed_tx_reclaim(txq, m_defer, reclaimable); 7251 } 7252 mtx_unlock(&txq->ift_mtx); 7253 7254 /* 7255 * Process mbuf frees outside the tx lock 7256 */ 7257 if (m_defer != NULL) { 7258 for (i = 0; m_defer[i] != NULL; i++) { 7259 m_freem(m_defer[i]); 7260 m_defer[i] = NULL; 7261 } 7262 atomic_store_rel_ptr((uintptr_t *)&txq->ift_sds.ifsd_m_defer, 7263 (uintptr_t)m_defer); 7264 } 7265 if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); 7266 if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); 7267 if (mcast_sent) 7268 if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent); 7269 7270 return (error); 7271 } 7272 #endif 7273