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