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