14c7070dbSScott Long /*- 27b610b60SSean Bruno * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io> 34c7070dbSScott Long * All rights reserved. 44c7070dbSScott Long * 54c7070dbSScott Long * Redistribution and use in source and binary forms, with or without 64c7070dbSScott Long * modification, are permitted provided that the following conditions are met: 74c7070dbSScott Long * 84c7070dbSScott Long * 1. Redistributions of source code must retain the above copyright notice, 94c7070dbSScott Long * this list of conditions and the following disclaimer. 104c7070dbSScott Long * 114c7070dbSScott Long * 2. Neither the name of Matthew Macy nor the names of its 124c7070dbSScott Long * contributors may be used to endorse or promote products derived from 134c7070dbSScott Long * this software without specific prior written permission. 144c7070dbSScott Long * 154c7070dbSScott Long * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 164c7070dbSScott Long * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 174c7070dbSScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 184c7070dbSScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 194c7070dbSScott Long * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 204c7070dbSScott Long * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 214c7070dbSScott Long * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 224c7070dbSScott Long * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 234c7070dbSScott Long * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 244c7070dbSScott Long * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 254c7070dbSScott Long * POSSIBILITY OF SUCH DAMAGE. 264c7070dbSScott Long */ 274c7070dbSScott Long 284c7070dbSScott Long #include <sys/cdefs.h> 294c7070dbSScott Long __FBSDID("$FreeBSD$"); 304c7070dbSScott Long 31aaeb188aSBjoern A. Zeeb #include "opt_inet.h" 32aaeb188aSBjoern A. Zeeb #include "opt_inet6.h" 33aaeb188aSBjoern A. Zeeb #include "opt_acpi.h" 34b103855eSStephen Hurd #include "opt_sched.h" 35aaeb188aSBjoern A. Zeeb 364c7070dbSScott Long #include <sys/param.h> 374c7070dbSScott Long #include <sys/types.h> 384c7070dbSScott Long #include <sys/bus.h> 394c7070dbSScott Long #include <sys/eventhandler.h> 404c7070dbSScott Long #include <sys/kernel.h> 414c7070dbSScott Long #include <sys/lock.h> 424c7070dbSScott Long #include <sys/mutex.h> 434c7070dbSScott Long #include <sys/module.h> 444c7070dbSScott Long #include <sys/kobj.h> 454c7070dbSScott Long #include <sys/rman.h> 464c7070dbSScott Long #include <sys/sbuf.h> 474c7070dbSScott Long #include <sys/smp.h> 484c7070dbSScott Long #include <sys/socket.h> 4909f6ff4fSMatt Macy #include <sys/sockio.h> 504c7070dbSScott Long #include <sys/sysctl.h> 514c7070dbSScott Long #include <sys/syslog.h> 524c7070dbSScott Long #include <sys/taskqueue.h> 5323ac9029SStephen Hurd #include <sys/limits.h> 544c7070dbSScott Long 554c7070dbSScott Long #include <net/if.h> 564c7070dbSScott Long #include <net/if_var.h> 574c7070dbSScott Long #include <net/if_types.h> 584c7070dbSScott Long #include <net/if_media.h> 594c7070dbSScott Long #include <net/bpf.h> 604c7070dbSScott Long #include <net/ethernet.h> 614c7070dbSScott Long #include <net/mp_ring.h> 627790c8c1SConrad Meyer #include <net/debugnet.h> 636d49b41eSAndrew Gallatin #include <net/pfil.h> 6435e4e998SStephen Hurd #include <net/vnet.h> 654c7070dbSScott Long 664c7070dbSScott Long #include <netinet/in.h> 674c7070dbSScott Long #include <netinet/in_pcb.h> 684c7070dbSScott Long #include <netinet/tcp_lro.h> 694c7070dbSScott Long #include <netinet/in_systm.h> 704c7070dbSScott Long #include <netinet/if_ether.h> 714c7070dbSScott Long #include <netinet/ip.h> 724c7070dbSScott Long #include <netinet/ip6.h> 734c7070dbSScott Long #include <netinet/tcp.h> 7435e4e998SStephen Hurd #include <netinet/ip_var.h> 7535e4e998SStephen Hurd #include <netinet6/ip6_var.h> 764c7070dbSScott Long 774c7070dbSScott Long #include <machine/bus.h> 784c7070dbSScott Long #include <machine/in_cksum.h> 794c7070dbSScott Long 804c7070dbSScott Long #include <vm/vm.h> 814c7070dbSScott Long #include <vm/pmap.h> 824c7070dbSScott Long 834c7070dbSScott Long #include <dev/led/led.h> 844c7070dbSScott Long #include <dev/pci/pcireg.h> 854c7070dbSScott Long #include <dev/pci/pcivar.h> 864c7070dbSScott Long #include <dev/pci/pci_private.h> 874c7070dbSScott Long 884c7070dbSScott Long #include <net/iflib.h> 8909f6ff4fSMatt Macy #include <net/iflib_private.h> 904c7070dbSScott Long 914c7070dbSScott Long #include "ifdi_if.h" 924c7070dbSScott Long 9377c1fcecSEric Joyner #ifdef PCI_IOV 9477c1fcecSEric Joyner #include <dev/pci/pci_iov.h> 9577c1fcecSEric Joyner #endif 9677c1fcecSEric Joyner 9787890dbaSSean Bruno #include <sys/bitstring.h> 984c7070dbSScott Long /* 9995246abbSSean Bruno * enable accounting of every mbuf as it comes in to and goes out of 10095246abbSSean Bruno * iflib's software descriptor references 1014c7070dbSScott Long */ 1024c7070dbSScott Long #define MEMORY_LOGGING 0 1034c7070dbSScott Long /* 1044c7070dbSScott Long * Enable mbuf vectors for compressing long mbuf chains 1054c7070dbSScott Long */ 1064c7070dbSScott Long 1074c7070dbSScott Long /* 1084c7070dbSScott Long * NB: 1094c7070dbSScott Long * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead 1104c7070dbSScott Long * we prefetch needs to be determined by the time spent in m_free vis a vis 1114c7070dbSScott Long * the cost of a prefetch. This will of course vary based on the workload: 1124c7070dbSScott Long * - NFLX's m_free path is dominated by vm-based M_EXT manipulation which 1134c7070dbSScott Long * is quite expensive, thus suggesting very little prefetch. 1144c7070dbSScott Long * - small packet forwarding which is just returning a single mbuf to 1154c7070dbSScott Long * UMA will typically be very fast vis a vis the cost of a memory 1164c7070dbSScott Long * access. 1174c7070dbSScott Long */ 1184c7070dbSScott Long 1194c7070dbSScott Long 1204c7070dbSScott Long /* 1214c7070dbSScott Long * File organization: 1224c7070dbSScott Long * - private structures 1234c7070dbSScott Long * - iflib private utility functions 1244c7070dbSScott Long * - ifnet functions 1254c7070dbSScott Long * - vlan registry and other exported functions 1264c7070dbSScott Long * - iflib public core functions 1274c7070dbSScott Long * 1284c7070dbSScott Long * 1294c7070dbSScott Long */ 13009f6ff4fSMatt Macy MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library"); 1314c7070dbSScott Long 132fb1a29b4SHans Petter Selasky #define IFLIB_RXEOF_MORE (1U << 0) 133fb1a29b4SHans Petter Selasky #define IFLIB_RXEOF_EMPTY (2U << 0) 134fb1a29b4SHans Petter Selasky 1354c7070dbSScott Long struct iflib_txq; 1364c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t; 1374c7070dbSScott Long struct iflib_rxq; 1384c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t; 1394c7070dbSScott Long struct iflib_fl; 1404c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t; 1414c7070dbSScott Long 1424ecb427aSSean Bruno struct iflib_ctx; 1434ecb427aSSean Bruno 1442d873474SStephen Hurd static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid); 145dd7fbcf1SStephen Hurd static void iflib_timer(void *arg); 1462d873474SStephen Hurd 1474c7070dbSScott Long typedef struct iflib_filter_info { 1484c7070dbSScott Long driver_filter_t *ifi_filter; 1494c7070dbSScott Long void *ifi_filter_arg; 1504c7070dbSScott Long struct grouptask *ifi_task; 15195246abbSSean Bruno void *ifi_ctx; 1524c7070dbSScott Long } *iflib_filter_info_t; 1534c7070dbSScott Long 1544c7070dbSScott Long struct iflib_ctx { 1554c7070dbSScott Long KOBJ_FIELDS; 1564c7070dbSScott Long /* 1574c7070dbSScott Long * Pointer to hardware driver's softc 1584c7070dbSScott Long */ 1594c7070dbSScott Long void *ifc_softc; 1604c7070dbSScott Long device_t ifc_dev; 1614c7070dbSScott Long if_t ifc_ifp; 1624c7070dbSScott Long 1634c7070dbSScott Long cpuset_t ifc_cpus; 1644c7070dbSScott Long if_shared_ctx_t ifc_sctx; 1654c7070dbSScott Long struct if_softc_ctx ifc_softc_ctx; 1664c7070dbSScott Long 167aa8a24d3SStephen Hurd struct sx ifc_ctx_sx; 1687b610b60SSean Bruno struct mtx ifc_state_mtx; 1694c7070dbSScott Long 1704c7070dbSScott Long iflib_txq_t ifc_txqs; 1714c7070dbSScott Long iflib_rxq_t ifc_rxqs; 1724c7070dbSScott Long uint32_t ifc_if_flags; 1734c7070dbSScott Long uint32_t ifc_flags; 1744c7070dbSScott Long uint32_t ifc_max_fl_buf_size; 1751b9d9394SEric Joyner uint32_t ifc_rx_mbuf_sz; 1764c7070dbSScott Long 1774c7070dbSScott Long int ifc_link_state; 1784c7070dbSScott Long int ifc_watchdog_events; 1794c7070dbSScott Long struct cdev *ifc_led_dev; 1804c7070dbSScott Long struct resource *ifc_msix_mem; 1814c7070dbSScott Long 1824c7070dbSScott Long struct if_irq ifc_legacy_irq; 1834c7070dbSScott Long struct grouptask ifc_admin_task; 1844c7070dbSScott Long struct grouptask ifc_vflr_task; 1854c7070dbSScott Long struct iflib_filter_info ifc_filter_info; 1864c7070dbSScott Long struct ifmedia ifc_media; 187e2621d96SMatt Macy struct ifmedia *ifc_mediap; 1884c7070dbSScott Long 1894c7070dbSScott Long struct sysctl_oid *ifc_sysctl_node; 1904c7070dbSScott Long uint16_t ifc_sysctl_ntxqs; 1914c7070dbSScott Long uint16_t ifc_sysctl_nrxqs; 19223ac9029SStephen Hurd uint16_t ifc_sysctl_qs_eq_override; 193f4d2154eSStephen Hurd uint16_t ifc_sysctl_rx_budget; 194fe51d4cdSStephen Hurd uint16_t ifc_sysctl_tx_abdicate; 195f154ece0SStephen Hurd uint16_t ifc_sysctl_core_offset; 196f154ece0SStephen Hurd #define CORE_OFFSET_UNSPECIFIED 0xffff 197f154ece0SStephen Hurd uint8_t ifc_sysctl_separate_txrx; 19823ac9029SStephen Hurd 19995246abbSSean Bruno qidx_t ifc_sysctl_ntxds[8]; 20095246abbSSean Bruno qidx_t ifc_sysctl_nrxds[8]; 2014c7070dbSScott Long struct if_txrx ifc_txrx; 2024c7070dbSScott Long #define isc_txd_encap ifc_txrx.ift_txd_encap 2034c7070dbSScott Long #define isc_txd_flush ifc_txrx.ift_txd_flush 2044c7070dbSScott Long #define isc_txd_credits_update ifc_txrx.ift_txd_credits_update 2054c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available 2064c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get 2074c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2084c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush 2094c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr 2104c7070dbSScott Long eventhandler_tag ifc_vlan_attach_event; 2114c7070dbSScott Long eventhandler_tag ifc_vlan_detach_event; 2121fd8c72cSKyle Evans struct ether_addr ifc_mac; 2134c7070dbSScott Long }; 2144c7070dbSScott Long 2154c7070dbSScott Long void * 2164c7070dbSScott Long iflib_get_softc(if_ctx_t ctx) 2174c7070dbSScott Long { 2184c7070dbSScott Long 2194c7070dbSScott Long return (ctx->ifc_softc); 2204c7070dbSScott Long } 2214c7070dbSScott Long 2224c7070dbSScott Long device_t 2234c7070dbSScott Long iflib_get_dev(if_ctx_t ctx) 2244c7070dbSScott Long { 2254c7070dbSScott Long 2264c7070dbSScott Long return (ctx->ifc_dev); 2274c7070dbSScott Long } 2284c7070dbSScott Long 2294c7070dbSScott Long if_t 2304c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx) 2314c7070dbSScott Long { 2324c7070dbSScott Long 2334c7070dbSScott Long return (ctx->ifc_ifp); 2344c7070dbSScott Long } 2354c7070dbSScott Long 2364c7070dbSScott Long struct ifmedia * 2374c7070dbSScott Long iflib_get_media(if_ctx_t ctx) 2384c7070dbSScott Long { 2394c7070dbSScott Long 240e2621d96SMatt Macy return (ctx->ifc_mediap); 2414c7070dbSScott Long } 2424c7070dbSScott Long 24309f6ff4fSMatt Macy uint32_t 24409f6ff4fSMatt Macy iflib_get_flags(if_ctx_t ctx) 24509f6ff4fSMatt Macy { 24609f6ff4fSMatt Macy return (ctx->ifc_flags); 24709f6ff4fSMatt Macy } 24809f6ff4fSMatt Macy 24909f6ff4fSMatt Macy void 2504c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]) 2514c7070dbSScott Long { 2524c7070dbSScott Long 2531fd8c72cSKyle Evans bcopy(mac, ctx->ifc_mac.octet, ETHER_ADDR_LEN); 2544c7070dbSScott Long } 2554c7070dbSScott Long 2564c7070dbSScott Long if_softc_ctx_t 2574c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx) 2584c7070dbSScott Long { 2594c7070dbSScott Long 2604c7070dbSScott Long return (&ctx->ifc_softc_ctx); 2614c7070dbSScott Long } 2624c7070dbSScott Long 2634c7070dbSScott Long if_shared_ctx_t 2644c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx) 2654c7070dbSScott Long { 2664c7070dbSScott Long 2674c7070dbSScott Long return (ctx->ifc_sctx); 2684c7070dbSScott Long } 2694c7070dbSScott Long 27095246abbSSean Bruno #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2) 2714c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*)) 2725e888388SSean Bruno #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1))) 2734c7070dbSScott Long 2744c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP) 2754c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF) 2764c7070dbSScott Long 277e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array { 278e035717eSSean Bruno bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */ 279e035717eSSean Bruno struct mbuf **ifsd_m; /* pkthdr mbufs */ 280e035717eSSean Bruno caddr_t *ifsd_cl; /* direct cluster pointer for rx */ 281fbec776dSAndrew Gallatin bus_addr_t *ifsd_ba; /* bus addr of cluster for rx */ 282e035717eSSean Bruno } iflib_rxsd_array_t; 2834c7070dbSScott Long 2844c7070dbSScott Long typedef struct iflib_sw_tx_desc_array { 2854c7070dbSScott Long bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */ 2868a04b53dSKonstantin Belousov bus_dmamap_t *ifsd_tso_map; /* bus_dma maps for TSO packet */ 2874c7070dbSScott Long struct mbuf **ifsd_m; /* pkthdr mbufs */ 28895246abbSSean Bruno } if_txsd_vec_t; 2894c7070dbSScott Long 2904c7070dbSScott Long /* magic number that should be high enough for any hardware */ 2914c7070dbSScott Long #define IFLIB_MAX_TX_SEGS 128 29295246abbSSean Bruno #define IFLIB_RX_COPY_THRESH 128 2934c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH 32 29495246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */ 29595246abbSSean Bruno #define IFLIB_MIN_DESC_SEC 16384 29695246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ 16 2974c7070dbSScott Long #define IFLIB_QUEUE_IDLE 0 2984c7070dbSScott Long #define IFLIB_QUEUE_HUNG 1 2994c7070dbSScott Long #define IFLIB_QUEUE_WORKING 2 30095246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */ 30195246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR 4 3024c7070dbSScott Long 30395246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */ 30495246abbSSean Bruno #define TX_BATCH_SIZE 32 3054c7070dbSScott Long 3064c7070dbSScott Long #define IFLIB_RESTART_BUDGET 8 3074c7070dbSScott Long 3084c7070dbSScott Long #define CSUM_OFFLOAD (CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \ 3094c7070dbSScott Long CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \ 3104c7070dbSScott Long CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP) 3111722eeacSMarius Strobl 3124c7070dbSScott Long struct iflib_txq { 31395246abbSSean Bruno qidx_t ift_in_use; 31495246abbSSean Bruno qidx_t ift_cidx; 31595246abbSSean Bruno qidx_t ift_cidx_processed; 31695246abbSSean Bruno qidx_t ift_pidx; 3174c7070dbSScott Long uint8_t ift_gen; 31823ac9029SStephen Hurd uint8_t ift_br_offset; 31995246abbSSean Bruno uint16_t ift_npending; 32095246abbSSean Bruno uint16_t ift_db_pending; 32195246abbSSean Bruno uint16_t ift_rs_pending; 3224c7070dbSScott Long /* implicit pad */ 32395246abbSSean Bruno uint8_t ift_txd_size[8]; 3244c7070dbSScott Long uint64_t ift_processed; 3254c7070dbSScott Long uint64_t ift_cleaned; 32695246abbSSean Bruno uint64_t ift_cleaned_prev; 3274c7070dbSScott Long #if MEMORY_LOGGING 3284c7070dbSScott Long uint64_t ift_enqueued; 3294c7070dbSScott Long uint64_t ift_dequeued; 3304c7070dbSScott Long #endif 3314c7070dbSScott Long uint64_t ift_no_tx_dma_setup; 3324c7070dbSScott Long uint64_t ift_no_desc_avail; 3334c7070dbSScott Long uint64_t ift_mbuf_defrag_failed; 3344c7070dbSScott Long uint64_t ift_mbuf_defrag; 3354c7070dbSScott Long uint64_t ift_map_failed; 3364c7070dbSScott Long uint64_t ift_txd_encap_efbig; 3374c7070dbSScott Long uint64_t ift_pullups; 338dd7fbcf1SStephen Hurd uint64_t ift_last_timer_tick; 3394c7070dbSScott Long 3404c7070dbSScott Long struct mtx ift_mtx; 3414c7070dbSScott Long struct mtx ift_db_mtx; 3424c7070dbSScott Long 3434c7070dbSScott Long /* constant values */ 3444c7070dbSScott Long if_ctx_t ift_ctx; 34595246abbSSean Bruno struct ifmp_ring *ift_br; 3464c7070dbSScott Long struct grouptask ift_task; 34795246abbSSean Bruno qidx_t ift_size; 3484c7070dbSScott Long uint16_t ift_id; 3494c7070dbSScott Long struct callout ift_timer; 3504c7070dbSScott Long 35195246abbSSean Bruno if_txsd_vec_t ift_sds; 3524c7070dbSScott Long uint8_t ift_qstatus; 3534c7070dbSScott Long uint8_t ift_closed; 35495246abbSSean Bruno uint8_t ift_update_freq; 3554c7070dbSScott Long struct iflib_filter_info ift_filter_info; 356bfce461eSMarius Strobl bus_dma_tag_t ift_buf_tag; 357bfce461eSMarius Strobl bus_dma_tag_t ift_tso_buf_tag; 3584c7070dbSScott Long iflib_dma_info_t ift_ifdi; 359814fa34dSMark Johnston #define MTX_NAME_LEN 32 3604c7070dbSScott Long char ift_mtx_name[MTX_NAME_LEN]; 3614c7070dbSScott Long bus_dma_segment_t ift_segs[IFLIB_MAX_TX_SEGS] __aligned(CACHE_LINE_SIZE); 3621248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 3631248952aSSean Bruno uint64_t ift_cpu_exec_count[256]; 3641248952aSSean Bruno #endif 3654c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3664c7070dbSScott Long 3674c7070dbSScott Long struct iflib_fl { 36895246abbSSean Bruno qidx_t ifl_cidx; 36995246abbSSean Bruno qidx_t ifl_pidx; 37095246abbSSean Bruno qidx_t ifl_credits; 3714c7070dbSScott Long uint8_t ifl_gen; 37295246abbSSean Bruno uint8_t ifl_rxd_size; 3734c7070dbSScott Long #if MEMORY_LOGGING 3744c7070dbSScott Long uint64_t ifl_m_enqueued; 3754c7070dbSScott Long uint64_t ifl_m_dequeued; 3764c7070dbSScott Long uint64_t ifl_cl_enqueued; 3774c7070dbSScott Long uint64_t ifl_cl_dequeued; 3784c7070dbSScott Long #endif 3794c7070dbSScott Long /* implicit pad */ 38087890dbaSSean Bruno bitstr_t *ifl_rx_bitmap; 38187890dbaSSean Bruno qidx_t ifl_fragidx; 3824c7070dbSScott Long /* constant */ 38395246abbSSean Bruno qidx_t ifl_size; 3844c7070dbSScott Long uint16_t ifl_buf_size; 3854c7070dbSScott Long uint16_t ifl_cltype; 3864c7070dbSScott Long uma_zone_t ifl_zone; 387e035717eSSean Bruno iflib_rxsd_array_t ifl_sds; 3884c7070dbSScott Long iflib_rxq_t ifl_rxq; 3894c7070dbSScott Long uint8_t ifl_id; 390bfce461eSMarius Strobl bus_dma_tag_t ifl_buf_tag; 3914c7070dbSScott Long iflib_dma_info_t ifl_ifdi; 3924c7070dbSScott Long uint64_t ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE); 39395246abbSSean Bruno qidx_t ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH]; 3944c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3954c7070dbSScott Long 39695246abbSSean Bruno static inline qidx_t 39795246abbSSean Bruno get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen) 3984c7070dbSScott Long { 39995246abbSSean Bruno qidx_t used; 4004c7070dbSScott Long 4014c7070dbSScott Long if (pidx > cidx) 4024c7070dbSScott Long used = pidx - cidx; 4034c7070dbSScott Long else if (pidx < cidx) 4044c7070dbSScott Long used = size - cidx + pidx; 4054c7070dbSScott Long else if (gen == 0 && pidx == cidx) 4064c7070dbSScott Long used = 0; 4074c7070dbSScott Long else if (gen == 1 && pidx == cidx) 4084c7070dbSScott Long used = size; 4094c7070dbSScott Long else 4104c7070dbSScott Long panic("bad state"); 4114c7070dbSScott Long 4124c7070dbSScott Long return (used); 4134c7070dbSScott Long } 4144c7070dbSScott Long 4154c7070dbSScott Long #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen)) 4164c7070dbSScott Long 4174c7070dbSScott Long #define IDXDIFF(head, tail, wrap) \ 4184c7070dbSScott Long ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head)) 4194c7070dbSScott Long 4204c7070dbSScott Long struct iflib_rxq { 4214c7070dbSScott Long if_ctx_t ifr_ctx; 4224c7070dbSScott Long iflib_fl_t ifr_fl; 4234c7070dbSScott Long uint64_t ifr_rx_irq; 4246d49b41eSAndrew Gallatin struct pfil_head *pfil; 4251722eeacSMarius Strobl /* 4261722eeacSMarius Strobl * If there is a separate completion queue (IFLIB_HAS_RXCQ), this is 4271722eeacSMarius Strobl * the command queue consumer index. Otherwise it's unused. 4281722eeacSMarius Strobl */ 4291722eeacSMarius Strobl qidx_t ifr_cq_cidx; 4304c7070dbSScott Long uint16_t ifr_id; 4314c7070dbSScott Long uint8_t ifr_nfl; 43295246abbSSean Bruno uint8_t ifr_ntxqirq; 43395246abbSSean Bruno uint8_t ifr_txqid[IFLIB_MAX_TX_SHARED_INTR]; 4341722eeacSMarius Strobl uint8_t ifr_fl_offset; 4354c7070dbSScott Long struct lro_ctrl ifr_lc; 4364c7070dbSScott Long struct grouptask ifr_task; 437fb1a29b4SHans Petter Selasky struct callout ifr_watchdog; 4384c7070dbSScott Long struct iflib_filter_info ifr_filter_info; 4394c7070dbSScott Long iflib_dma_info_t ifr_ifdi; 440ab2e3f79SStephen Hurd 4414c7070dbSScott Long /* dynamically allocate if any drivers need a value substantially larger than this */ 4424c7070dbSScott Long struct if_rxd_frag ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE); 4431248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 4441248952aSSean Bruno uint64_t ifr_cpu_exec_count[256]; 4451248952aSSean Bruno #endif 4464c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 4474c7070dbSScott Long 44895246abbSSean Bruno typedef struct if_rxsd { 44995246abbSSean Bruno caddr_t *ifsd_cl; 45095246abbSSean Bruno iflib_fl_t ifsd_fl; 45195246abbSSean Bruno } *if_rxsd_t; 45295246abbSSean Bruno 45395246abbSSean Bruno /* multiple of word size */ 45495246abbSSean Bruno #ifdef __LP64__ 455ab2e3f79SStephen Hurd #define PKT_INFO_SIZE 6 45695246abbSSean Bruno #define RXD_INFO_SIZE 5 45795246abbSSean Bruno #define PKT_TYPE uint64_t 45895246abbSSean Bruno #else 459ab2e3f79SStephen Hurd #define PKT_INFO_SIZE 11 46095246abbSSean Bruno #define RXD_INFO_SIZE 8 46195246abbSSean Bruno #define PKT_TYPE uint32_t 46295246abbSSean Bruno #endif 46395246abbSSean Bruno #define PKT_LOOP_BOUND ((PKT_INFO_SIZE/3)*3) 46495246abbSSean Bruno #define RXD_LOOP_BOUND ((RXD_INFO_SIZE/4)*4) 46595246abbSSean Bruno 46695246abbSSean Bruno typedef struct if_pkt_info_pad { 46795246abbSSean Bruno PKT_TYPE pkt_val[PKT_INFO_SIZE]; 46895246abbSSean Bruno } *if_pkt_info_pad_t; 46995246abbSSean Bruno typedef struct if_rxd_info_pad { 47095246abbSSean Bruno PKT_TYPE rxd_val[RXD_INFO_SIZE]; 47195246abbSSean Bruno } *if_rxd_info_pad_t; 47295246abbSSean Bruno 47395246abbSSean Bruno CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info)); 47495246abbSSean Bruno CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info)); 47595246abbSSean Bruno 47695246abbSSean Bruno 47795246abbSSean Bruno static inline void 47895246abbSSean Bruno pkt_info_zero(if_pkt_info_t pi) 47995246abbSSean Bruno { 48095246abbSSean Bruno if_pkt_info_pad_t pi_pad; 48195246abbSSean Bruno 48295246abbSSean Bruno pi_pad = (if_pkt_info_pad_t)pi; 48395246abbSSean Bruno pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0; 48495246abbSSean Bruno pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0; 48595246abbSSean Bruno #ifndef __LP64__ 486ab2e3f79SStephen Hurd pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0; 487ab2e3f79SStephen Hurd pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0; 48895246abbSSean Bruno #endif 48995246abbSSean Bruno } 49095246abbSSean Bruno 49109f6ff4fSMatt Macy static device_method_t iflib_pseudo_methods[] = { 49209f6ff4fSMatt Macy DEVMETHOD(device_attach, noop_attach), 49309f6ff4fSMatt Macy DEVMETHOD(device_detach, iflib_pseudo_detach), 49409f6ff4fSMatt Macy DEVMETHOD_END 49509f6ff4fSMatt Macy }; 49609f6ff4fSMatt Macy 49709f6ff4fSMatt Macy driver_t iflib_pseudodriver = { 49809f6ff4fSMatt Macy "iflib_pseudo", iflib_pseudo_methods, sizeof(struct iflib_ctx), 49909f6ff4fSMatt Macy }; 50009f6ff4fSMatt Macy 50195246abbSSean Bruno static inline void 50295246abbSSean Bruno rxd_info_zero(if_rxd_info_t ri) 50395246abbSSean Bruno { 50495246abbSSean Bruno if_rxd_info_pad_t ri_pad; 50595246abbSSean Bruno int i; 50695246abbSSean Bruno 50795246abbSSean Bruno ri_pad = (if_rxd_info_pad_t)ri; 50895246abbSSean Bruno for (i = 0; i < RXD_LOOP_BOUND; i += 4) { 50995246abbSSean Bruno ri_pad->rxd_val[i] = 0; 51095246abbSSean Bruno ri_pad->rxd_val[i+1] = 0; 51195246abbSSean Bruno ri_pad->rxd_val[i+2] = 0; 51295246abbSSean Bruno ri_pad->rxd_val[i+3] = 0; 51395246abbSSean Bruno } 51495246abbSSean Bruno #ifdef __LP64__ 51595246abbSSean Bruno ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0; 51695246abbSSean Bruno #endif 51795246abbSSean Bruno } 51895246abbSSean Bruno 5194c7070dbSScott Long /* 5204c7070dbSScott Long * Only allow a single packet to take up most 1/nth of the tx ring 5214c7070dbSScott Long */ 5224c7070dbSScott Long #define MAX_SINGLE_PACKET_FRACTION 12 5234c7070dbSScott Long #define IF_BAD_DMA (bus_addr_t)-1 5244c7070dbSScott Long 5254c7070dbSScott Long #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING)) 5264c7070dbSScott Long 527aa8a24d3SStephen Hurd #define CTX_LOCK_INIT(_sc) sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock") 528aa8a24d3SStephen Hurd #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx) 529aa8a24d3SStephen Hurd #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx) 530aa8a24d3SStephen Hurd #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx) 5314c7070dbSScott Long 5327b610b60SSean Bruno #define STATE_LOCK_INIT(_sc, _name) mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF) 5337b610b60SSean Bruno #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx) 5347b610b60SSean Bruno #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx) 5357b610b60SSean Bruno #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx) 5367b610b60SSean Bruno 5374c7070dbSScott Long #define CALLOUT_LOCK(txq) mtx_lock(&txq->ift_mtx) 5384c7070dbSScott Long #define CALLOUT_UNLOCK(txq) mtx_unlock(&txq->ift_mtx) 5394c7070dbSScott Long 54077c1fcecSEric Joyner void 54177c1fcecSEric Joyner iflib_set_detach(if_ctx_t ctx) 54277c1fcecSEric Joyner { 54377c1fcecSEric Joyner STATE_LOCK(ctx); 54477c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 54577c1fcecSEric Joyner STATE_UNLOCK(ctx); 54677c1fcecSEric Joyner } 5474c7070dbSScott Long 5484c7070dbSScott Long /* Our boot-time initialization hook */ 5494c7070dbSScott Long static int iflib_module_event_handler(module_t, int, void *); 5504c7070dbSScott Long 5514c7070dbSScott Long static moduledata_t iflib_moduledata = { 5524c7070dbSScott Long "iflib", 5534c7070dbSScott Long iflib_module_event_handler, 5544c7070dbSScott Long NULL 5554c7070dbSScott Long }; 5564c7070dbSScott Long 5574c7070dbSScott Long DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY); 5584c7070dbSScott Long MODULE_VERSION(iflib, 1); 5594c7070dbSScott Long 5604c7070dbSScott Long MODULE_DEPEND(iflib, pci, 1, 1, 1); 5614c7070dbSScott Long MODULE_DEPEND(iflib, ether, 1, 1, 1); 5624c7070dbSScott Long 563ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1); 564ab2e3f79SStephen Hurd TASKQGROUP_DEFINE(if_config_tqg, 1, 1); 565ab2e3f79SStephen Hurd 5664c7070dbSScott Long #ifndef IFLIB_DEBUG_COUNTERS 5674c7070dbSScott Long #ifdef INVARIANTS 5684c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 1 5694c7070dbSScott Long #else 5704c7070dbSScott Long #define IFLIB_DEBUG_COUNTERS 0 5714c7070dbSScott Long #endif /* !INVARIANTS */ 5724c7070dbSScott Long #endif 5734c7070dbSScott Long 5747029da5cSPawel Biernacki static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 575ab2e3f79SStephen Hurd "iflib driver parameters"); 576ab2e3f79SStephen Hurd 5774c7070dbSScott Long /* 5784c7070dbSScott Long * XXX need to ensure that this can't accidentally cause the head to be moved backwards 5794c7070dbSScott Long */ 5804c7070dbSScott Long static int iflib_min_tx_latency = 0; 5814c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW, 582da69b8f9SSean Bruno &iflib_min_tx_latency, 0, "minimize transmit latency at the possible expense of throughput"); 58395246abbSSean Bruno static int iflib_no_tx_batch = 0; 58495246abbSSean Bruno SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW, 58595246abbSSean Bruno &iflib_no_tx_batch, 0, "minimize transmit latency at the possible expense of throughput"); 5864c7070dbSScott Long 5874c7070dbSScott Long 5884c7070dbSScott Long #if IFLIB_DEBUG_COUNTERS 5894c7070dbSScott Long 5904c7070dbSScott Long static int iflib_tx_seen; 5914c7070dbSScott Long static int iflib_tx_sent; 5924c7070dbSScott Long static int iflib_tx_encap; 5934c7070dbSScott Long static int iflib_rx_allocs; 5944c7070dbSScott Long static int iflib_fl_refills; 5954c7070dbSScott Long static int iflib_fl_refills_large; 5964c7070dbSScott Long static int iflib_tx_frees; 5974c7070dbSScott Long 5984c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD, 5991722eeacSMarius Strobl &iflib_tx_seen, 0, "# TX mbufs seen"); 6004c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD, 6011722eeacSMarius Strobl &iflib_tx_sent, 0, "# TX mbufs sent"); 6024c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD, 6031722eeacSMarius Strobl &iflib_tx_encap, 0, "# TX mbufs encapped"); 6044c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD, 6051722eeacSMarius Strobl &iflib_tx_frees, 0, "# TX frees"); 6064c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD, 6071722eeacSMarius Strobl &iflib_rx_allocs, 0, "# RX allocations"); 6084c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD, 6094c7070dbSScott Long &iflib_fl_refills, 0, "# refills"); 6104c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD, 6114c7070dbSScott Long &iflib_fl_refills_large, 0, "# large refills"); 6124c7070dbSScott Long 6134c7070dbSScott Long 6144c7070dbSScott Long static int iflib_txq_drain_flushing; 6154c7070dbSScott Long static int iflib_txq_drain_oactive; 6164c7070dbSScott Long static int iflib_txq_drain_notready; 6174c7070dbSScott Long 6184c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD, 6194c7070dbSScott Long &iflib_txq_drain_flushing, 0, "# drain flushes"); 6204c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD, 6214c7070dbSScott Long &iflib_txq_drain_oactive, 0, "# drain oactives"); 6224c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD, 6234c7070dbSScott Long &iflib_txq_drain_notready, 0, "# drain notready"); 6244c7070dbSScott Long 6254c7070dbSScott Long 6264c7070dbSScott Long static int iflib_encap_load_mbuf_fail; 627d14c853bSStephen Hurd static int iflib_encap_pad_mbuf_fail; 6284c7070dbSScott Long static int iflib_encap_txq_avail_fail; 6294c7070dbSScott Long static int iflib_encap_txd_encap_fail; 6304c7070dbSScott Long 6314c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD, 6324c7070dbSScott Long &iflib_encap_load_mbuf_fail, 0, "# busdma load failures"); 633d14c853bSStephen Hurd SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD, 634d14c853bSStephen Hurd &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures"); 6354c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD, 6364c7070dbSScott Long &iflib_encap_txq_avail_fail, 0, "# txq avail failures"); 6374c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD, 6384c7070dbSScott Long &iflib_encap_txd_encap_fail, 0, "# driver encap failures"); 6394c7070dbSScott Long 6404c7070dbSScott Long static int iflib_task_fn_rxs; 6414c7070dbSScott Long static int iflib_rx_intr_enables; 6424c7070dbSScott Long static int iflib_fast_intrs; 6434c7070dbSScott Long static int iflib_rx_unavail; 6444c7070dbSScott Long static int iflib_rx_ctx_inactive; 6454c7070dbSScott Long static int iflib_rx_if_input; 6464c7070dbSScott Long static int iflib_rxd_flush; 6474c7070dbSScott Long 6484c7070dbSScott Long static int iflib_verbose_debug; 6494c7070dbSScott Long 6504c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD, 6514c7070dbSScott Long &iflib_task_fn_rxs, 0, "# task_fn_rx calls"); 6524c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD, 6531722eeacSMarius Strobl &iflib_rx_intr_enables, 0, "# RX intr enables"); 6544c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD, 6554c7070dbSScott Long &iflib_fast_intrs, 0, "# fast_intr calls"); 6564c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD, 6574c7070dbSScott Long &iflib_rx_unavail, 0, "# times rxeof called with no available data"); 6584c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD, 6594c7070dbSScott Long &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context"); 6604c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD, 6614c7070dbSScott Long &iflib_rx_if_input, 0, "# times rxeof called if_input"); 6624c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD, 6634c7070dbSScott Long &iflib_rxd_flush, 0, "# times rxd_flush called"); 6644c7070dbSScott Long SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW, 6654c7070dbSScott Long &iflib_verbose_debug, 0, "enable verbose debugging"); 6664c7070dbSScott Long 6674c7070dbSScott Long #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1) 668da69b8f9SSean Bruno static void 669da69b8f9SSean Bruno iflib_debug_reset(void) 670da69b8f9SSean Bruno { 671da69b8f9SSean Bruno iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs = 672da69b8f9SSean Bruno iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees = 673da69b8f9SSean Bruno iflib_txq_drain_flushing = iflib_txq_drain_oactive = 67464e6fc13SStephen Hurd iflib_txq_drain_notready = 675d14c853bSStephen Hurd iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail = 676d14c853bSStephen Hurd iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail = 677d14c853bSStephen Hurd iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs = 67864e6fc13SStephen Hurd iflib_rx_unavail = 67964e6fc13SStephen Hurd iflib_rx_ctx_inactive = iflib_rx_if_input = 6806d49b41eSAndrew Gallatin iflib_rxd_flush = 0; 681da69b8f9SSean Bruno } 6824c7070dbSScott Long 6834c7070dbSScott Long #else 6844c7070dbSScott Long #define DBG_COUNTER_INC(name) 685da69b8f9SSean Bruno static void iflib_debug_reset(void) {} 6864c7070dbSScott Long #endif 6874c7070dbSScott Long 6884c7070dbSScott Long #define IFLIB_DEBUG 0 6894c7070dbSScott Long 6904c7070dbSScott Long static void iflib_tx_structures_free(if_ctx_t ctx); 6914c7070dbSScott Long static void iflib_rx_structures_free(if_ctx_t ctx); 6924c7070dbSScott Long static int iflib_queues_alloc(if_ctx_t ctx); 6934c7070dbSScott Long static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq); 69495246abbSSean Bruno static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget); 6954c7070dbSScott Long static int iflib_qset_structures_setup(if_ctx_t ctx); 6964c7070dbSScott Long static int iflib_msix_init(if_ctx_t ctx); 6973e0e6330SStephen Hurd static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str); 6984c7070dbSScott Long static void iflib_txq_check_drain(iflib_txq_t txq, int budget); 6994c7070dbSScott Long static uint32_t iflib_txq_can_drain(struct ifmp_ring *); 700b8ca4756SPatrick Kelsey #ifdef ALTQ 701b8ca4756SPatrick Kelsey static void iflib_altq_if_start(if_t ifp); 702b8ca4756SPatrick Kelsey static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m); 703b8ca4756SPatrick Kelsey #endif 7044c7070dbSScott Long static int iflib_register(if_ctx_t); 70556614414SEric Joyner static void iflib_deregister(if_ctx_t); 7061558015eSEric Joyner static void iflib_unregister_vlan_handlers(if_ctx_t ctx); 707b3813609SPatrick Kelsey static uint16_t iflib_get_mbuf_size_for(unsigned int size); 7084c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx); 7094c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx); 7104c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx); 711da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq); 7121248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx); 71395246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx); 71477c1fcecSEric Joyner static void iflib_free_intr_mem(if_ctx_t ctx); 71595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 71695246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m); 71795246abbSSean Bruno #endif 7184c7070dbSScott Long 719f154ece0SStephen Hurd static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets = 720f154ece0SStephen Hurd SLIST_HEAD_INITIALIZER(cpu_offsets); 721f154ece0SStephen Hurd struct cpu_offset { 722f154ece0SStephen Hurd SLIST_ENTRY(cpu_offset) entries; 723f154ece0SStephen Hurd cpuset_t set; 724f154ece0SStephen Hurd unsigned int refcount; 725f154ece0SStephen Hurd uint16_t offset; 726f154ece0SStephen Hurd }; 727f154ece0SStephen Hurd static struct mtx cpu_offset_mtx; 728f154ece0SStephen Hurd MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock", 729f154ece0SStephen Hurd MTX_DEF); 730f154ece0SStephen Hurd 7317790c8c1SConrad Meyer DEBUGNET_DEFINE(iflib); 73294618825SMark Johnston 733ac11d857SVincenzo Maffione static int 734ac11d857SVincenzo Maffione iflib_num_rx_descs(if_ctx_t ctx) 735ac11d857SVincenzo Maffione { 736ac11d857SVincenzo Maffione if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 737ac11d857SVincenzo Maffione if_shared_ctx_t sctx = ctx->ifc_sctx; 738ac11d857SVincenzo Maffione uint16_t first_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0; 739ac11d857SVincenzo Maffione 740ac11d857SVincenzo Maffione return scctx->isc_nrxd[first_rxq]; 741ac11d857SVincenzo Maffione } 742ac11d857SVincenzo Maffione 743ac11d857SVincenzo Maffione static int 744ac11d857SVincenzo Maffione iflib_num_tx_descs(if_ctx_t ctx) 745ac11d857SVincenzo Maffione { 746ac11d857SVincenzo Maffione if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 747ac11d857SVincenzo Maffione if_shared_ctx_t sctx = ctx->ifc_sctx; 748ac11d857SVincenzo Maffione uint16_t first_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0; 749ac11d857SVincenzo Maffione 750ac11d857SVincenzo Maffione return scctx->isc_ntxd[first_txq]; 751ac11d857SVincenzo Maffione } 752ac11d857SVincenzo Maffione 7534c7070dbSScott Long #ifdef DEV_NETMAP 7544c7070dbSScott Long #include <sys/selinfo.h> 7554c7070dbSScott Long #include <net/netmap.h> 7564c7070dbSScott Long #include <dev/netmap/netmap_kern.h> 7574c7070dbSScott Long 7584c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1); 7594c7070dbSScott Long 7602d873474SStephen Hurd static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init); 7612d873474SStephen Hurd 7624c7070dbSScott Long /* 7634c7070dbSScott Long * device-specific sysctl variables: 7644c7070dbSScott Long * 76591d546a0SConrad Meyer * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it. 7664c7070dbSScott Long * During regular operations the CRC is stripped, but on some 7674c7070dbSScott Long * hardware reception of frames not multiple of 64 is slower, 7684c7070dbSScott Long * so using crcstrip=0 helps in benchmarks. 7694c7070dbSScott Long * 77091d546a0SConrad Meyer * iflib_rx_miss, iflib_rx_miss_bufs: 7714c7070dbSScott Long * count packets that might be missed due to lost interrupts. 7724c7070dbSScott Long */ 7734c7070dbSScott Long SYSCTL_DECL(_dev_netmap); 7744c7070dbSScott Long /* 7754c7070dbSScott Long * The xl driver by default strips CRCs and we do not override it. 7764c7070dbSScott Long */ 7774c7070dbSScott Long 7784c7070dbSScott Long int iflib_crcstrip = 1; 7794c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip, 7801722eeacSMarius Strobl CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames"); 7814c7070dbSScott Long 7824c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs; 7834c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss, 7841722eeacSMarius Strobl CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr"); 78591d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs, 7861722eeacSMarius Strobl CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs"); 7874c7070dbSScott Long 7884c7070dbSScott Long /* 7894c7070dbSScott Long * Register/unregister. We are already under netmap lock. 7904c7070dbSScott Long * Only called on the first register or the last unregister. 7914c7070dbSScott Long */ 7924c7070dbSScott Long static int 7934c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff) 7944c7070dbSScott Long { 7951722eeacSMarius Strobl if_t ifp = na->ifp; 7964c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 79795246abbSSean Bruno int status; 7984c7070dbSScott Long 7994c7070dbSScott Long CTX_LOCK(ctx); 8004c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 8014c7070dbSScott Long 8024c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 8034c7070dbSScott Long ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 8044c7070dbSScott Long 8054c7070dbSScott Long if (!CTX_IS_VF(ctx)) 8061248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); 8074c7070dbSScott Long 8080a182b4cSVincenzo Maffione iflib_stop(ctx); 8090a182b4cSVincenzo Maffione 8100a182b4cSVincenzo Maffione /* 8110a182b4cSVincenzo Maffione * Enable (or disable) netmap flags, and intercept (or restore) 8120a182b4cSVincenzo Maffione * ifp->if_transmit. This is done once the device has been stopped 8130a182b4cSVincenzo Maffione * to prevent race conditions. 8140a182b4cSVincenzo Maffione */ 8154c7070dbSScott Long if (onoff) { 8164c7070dbSScott Long nm_set_native_flags(na); 8174c7070dbSScott Long } else { 8184c7070dbSScott Long nm_clear_native_flags(na); 8194c7070dbSScott Long } 8200a182b4cSVincenzo Maffione 82195246abbSSean Bruno iflib_init_locked(ctx); 8221248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ? 82395246abbSSean Bruno status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1; 82495246abbSSean Bruno if (status) 82595246abbSSean Bruno nm_clear_native_flags(na); 8264c7070dbSScott Long CTX_UNLOCK(ctx); 82795246abbSSean Bruno return (status); 8284c7070dbSScott Long } 8294c7070dbSScott Long 8302d873474SStephen Hurd static int 8312d873474SStephen Hurd netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init) 8322d873474SStephen Hurd { 8332d873474SStephen Hurd struct netmap_adapter *na = kring->na; 8342d873474SStephen Hurd u_int const lim = kring->nkr_num_slots - 1; 8352d873474SStephen Hurd u_int head = kring->rhead; 8362d873474SStephen Hurd struct netmap_ring *ring = kring->ring; 8372d873474SStephen Hurd bus_dmamap_t *map; 8382d873474SStephen Hurd struct if_rxd_update iru; 8392d873474SStephen Hurd if_ctx_t ctx = rxq->ifr_ctx; 8402d873474SStephen Hurd iflib_fl_t fl = &rxq->ifr_fl[0]; 8412d873474SStephen Hurd uint32_t refill_pidx, nic_i; 84264e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 84364e6fc13SStephen Hurd int rf_count = 0; 84464e6fc13SStephen Hurd #endif 8452d873474SStephen Hurd 8462d873474SStephen Hurd if (nm_i == head && __predict_true(!init)) 8472d873474SStephen Hurd return 0; 8482d873474SStephen Hurd iru_init(&iru, rxq, 0 /* flid */); 8492d873474SStephen Hurd map = fl->ifl_sds.ifsd_map; 8502d873474SStephen Hurd refill_pidx = netmap_idx_k2n(kring, nm_i); 8512d873474SStephen Hurd /* 8522d873474SStephen Hurd * IMPORTANT: we must leave one free slot in the ring, 8532d873474SStephen Hurd * so move head back by one unit 8542d873474SStephen Hurd */ 8552d873474SStephen Hurd head = nm_prev(head, lim); 8561ae4848cSMatt Macy nic_i = UINT_MAX; 85764e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills); 8582d873474SStephen Hurd while (nm_i != head) { 85964e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 86064e6fc13SStephen Hurd if (++rf_count == 9) 86164e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills_large); 86264e6fc13SStephen Hurd #endif 8632d873474SStephen Hurd for (int tmp_pidx = 0; tmp_pidx < IFLIB_MAX_RX_REFRESH && nm_i != head; tmp_pidx++) { 8642d873474SStephen Hurd struct netmap_slot *slot = &ring->slot[nm_i]; 8652d873474SStephen Hurd void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[tmp_pidx]); 8662d873474SStephen Hurd uint32_t nic_i_dma = refill_pidx; 8672d873474SStephen Hurd nic_i = netmap_idx_k2n(kring, nm_i); 8682d873474SStephen Hurd 8692d873474SStephen Hurd MPASS(tmp_pidx < IFLIB_MAX_RX_REFRESH); 8702d873474SStephen Hurd 8712d873474SStephen Hurd if (addr == NETMAP_BUF_BASE(na)) /* bad buf */ 8722d873474SStephen Hurd return netmap_ring_reinit(kring); 8732d873474SStephen Hurd 87495dcf343SMarius Strobl if (__predict_false(init)) { 87595dcf343SMarius Strobl netmap_load_map(na, fl->ifl_buf_tag, 87695dcf343SMarius Strobl map[nic_i], addr); 87795dcf343SMarius Strobl } else if (slot->flags & NS_BUF_CHANGED) { 8782d873474SStephen Hurd /* buffer has changed, reload map */ 87995dcf343SMarius Strobl netmap_reload_map(na, fl->ifl_buf_tag, 88095dcf343SMarius Strobl map[nic_i], addr); 8812d873474SStephen Hurd } 8822d873474SStephen Hurd slot->flags &= ~NS_BUF_CHANGED; 8832d873474SStephen Hurd 8842d873474SStephen Hurd nm_i = nm_next(nm_i, lim); 8852d873474SStephen Hurd fl->ifl_rxd_idxs[tmp_pidx] = nic_i = nm_next(nic_i, lim); 8862d873474SStephen Hurd if (nm_i != head && tmp_pidx < IFLIB_MAX_RX_REFRESH-1) 8872d873474SStephen Hurd continue; 8882d873474SStephen Hurd 8892d873474SStephen Hurd iru.iru_pidx = refill_pidx; 8902d873474SStephen Hurd iru.iru_count = tmp_pidx+1; 8912d873474SStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 8922d873474SStephen Hurd refill_pidx = nic_i; 8932d873474SStephen Hurd for (int n = 0; n < iru.iru_count; n++) { 89495dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i_dma], 8952d873474SStephen Hurd BUS_DMASYNC_PREREAD); 8962d873474SStephen Hurd /* XXX - change this to not use the netmap func*/ 8972d873474SStephen Hurd nic_i_dma = nm_next(nic_i_dma, lim); 8982d873474SStephen Hurd } 8992d873474SStephen Hurd } 9002d873474SStephen Hurd } 9012d873474SStephen Hurd kring->nr_hwcur = head; 9022d873474SStephen Hurd 9032d873474SStephen Hurd bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 9042d873474SStephen Hurd BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 90564e6fc13SStephen Hurd if (__predict_true(nic_i != UINT_MAX)) { 9062d873474SStephen Hurd ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i); 90764e6fc13SStephen Hurd DBG_COUNTER_INC(rxd_flush); 90864e6fc13SStephen Hurd } 9092d873474SStephen Hurd return (0); 9102d873474SStephen Hurd } 9112d873474SStephen Hurd 9124c7070dbSScott Long /* 9134c7070dbSScott Long * Reconcile kernel and user view of the transmit ring. 9144c7070dbSScott Long * 9154c7070dbSScott Long * All information is in the kring. 9164c7070dbSScott Long * Userspace wants to send packets up to the one before kring->rhead, 9174c7070dbSScott Long * kernel knows kring->nr_hwcur is the first unsent packet. 9184c7070dbSScott Long * 9194c7070dbSScott Long * Here we push packets out (as many as possible), and possibly 9204c7070dbSScott Long * reclaim buffers from previously completed transmission. 9214c7070dbSScott Long * 9224c7070dbSScott Long * The caller (netmap) guarantees that there is only one instance 9234c7070dbSScott Long * running at any time. Any interference with other driver 9244c7070dbSScott Long * methods should be handled by the individual drivers. 9254c7070dbSScott Long */ 9264c7070dbSScott Long static int 9274c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags) 9284c7070dbSScott Long { 9294c7070dbSScott Long struct netmap_adapter *na = kring->na; 9301722eeacSMarius Strobl if_t ifp = na->ifp; 9314c7070dbSScott Long struct netmap_ring *ring = kring->ring; 932dd7fbcf1SStephen Hurd u_int nm_i; /* index into the netmap kring */ 9334c7070dbSScott Long u_int nic_i; /* index into the NIC ring */ 9344c7070dbSScott Long u_int n; 9354c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 9364c7070dbSScott Long u_int const head = kring->rhead; 9374c7070dbSScott Long struct if_pkt_info pi; 9384c7070dbSScott Long 9394c7070dbSScott Long /* 9404c7070dbSScott Long * interrupts on every tx packet are expensive so request 9414c7070dbSScott Long * them every half ring, or where NS_REPORT is set 9424c7070dbSScott Long */ 9434c7070dbSScott Long u_int report_frequency = kring->nkr_num_slots >> 1; 9444c7070dbSScott Long /* device-specific */ 9454c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 9464c7070dbSScott Long iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id]; 9474c7070dbSScott Long 94895dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 9494c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 9504c7070dbSScott Long 9514c7070dbSScott Long /* 9524c7070dbSScott Long * First part: process new packets to send. 953dd7fbcf1SStephen Hurd * nm_i is the current index in the netmap kring, 9544c7070dbSScott Long * nic_i is the corresponding index in the NIC ring. 9554c7070dbSScott Long * 9564c7070dbSScott Long * If we have packets to send (nm_i != head) 9574c7070dbSScott Long * iterate over the netmap ring, fetch length and update 9584c7070dbSScott Long * the corresponding slot in the NIC ring. Some drivers also 9594c7070dbSScott Long * need to update the buffer's physical address in the NIC slot 9604c7070dbSScott Long * even NS_BUF_CHANGED is not set (PNMB computes the addresses). 9614c7070dbSScott Long * 9624c7070dbSScott Long * The netmap_reload_map() calls is especially expensive, 9634c7070dbSScott Long * even when (as in this case) the tag is 0, so do only 9644c7070dbSScott Long * when the buffer has actually changed. 9654c7070dbSScott Long * 9664c7070dbSScott Long * If possible do not set the report/intr bit on all slots, 9674c7070dbSScott Long * but only a few times per ring or when NS_REPORT is set. 9684c7070dbSScott Long * 9694c7070dbSScott Long * Finally, on 10G and faster drivers, it might be useful 9704c7070dbSScott Long * to prefetch the next slot and txr entry. 9714c7070dbSScott Long */ 9724c7070dbSScott Long 973dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 9745ee36c68SStephen Hurd if (nm_i != head) { /* we have new packets to send */ 97595246abbSSean Bruno pkt_info_zero(&pi); 97695246abbSSean Bruno pi.ipi_segs = txq->ift_segs; 97795246abbSSean Bruno pi.ipi_qsidx = kring->ring_id; 9784c7070dbSScott Long nic_i = netmap_idx_k2n(kring, nm_i); 9794c7070dbSScott Long 9804c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i]); 9814c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]); 9824c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]); 9834c7070dbSScott Long 9844c7070dbSScott Long for (n = 0; nm_i != head; n++) { 9854c7070dbSScott Long struct netmap_slot *slot = &ring->slot[nm_i]; 9864c7070dbSScott Long u_int len = slot->len; 9870a1b74a3SSean Bruno uint64_t paddr; 9884c7070dbSScott Long void *addr = PNMB(na, slot, &paddr); 9894c7070dbSScott Long int flags = (slot->flags & NS_REPORT || 9904c7070dbSScott Long nic_i == 0 || nic_i == report_frequency) ? 9914c7070dbSScott Long IPI_TX_INTR : 0; 9924c7070dbSScott Long 9934c7070dbSScott Long /* device-specific */ 99495246abbSSean Bruno pi.ipi_len = len; 99595246abbSSean Bruno pi.ipi_segs[0].ds_addr = paddr; 99695246abbSSean Bruno pi.ipi_segs[0].ds_len = len; 99795246abbSSean Bruno pi.ipi_nsegs = 1; 99895246abbSSean Bruno pi.ipi_ndescs = 0; 9994c7070dbSScott Long pi.ipi_pidx = nic_i; 10004c7070dbSScott Long pi.ipi_flags = flags; 10014c7070dbSScott Long 10024c7070dbSScott Long /* Fill the slot in the NIC ring. */ 10034c7070dbSScott Long ctx->isc_txd_encap(ctx->ifc_softc, &pi); 100464e6fc13SStephen Hurd DBG_COUNTER_INC(tx_encap); 10054c7070dbSScott Long 10064c7070dbSScott Long /* prefetch for next round */ 10074c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i + 1]); 10084c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]); 10094c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]); 10104c7070dbSScott Long 10114c7070dbSScott Long NM_CHECK_ADDR_LEN(na, addr, len); 10124c7070dbSScott Long 10134c7070dbSScott Long if (slot->flags & NS_BUF_CHANGED) { 10144c7070dbSScott Long /* buffer has changed, reload map */ 1015bfce461eSMarius Strobl netmap_reload_map(na, txq->ift_buf_tag, 1016bfce461eSMarius Strobl txq->ift_sds.ifsd_map[nic_i], addr); 10174c7070dbSScott Long } 10184c7070dbSScott Long /* make sure changes to the buffer are synced */ 101995dcf343SMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 102095dcf343SMarius Strobl txq->ift_sds.ifsd_map[nic_i], 10214c7070dbSScott Long BUS_DMASYNC_PREWRITE); 102295dcf343SMarius Strobl 102395246abbSSean Bruno slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED); 10244c7070dbSScott Long nm_i = nm_next(nm_i, lim); 10254c7070dbSScott Long nic_i = nm_next(nic_i, lim); 10264c7070dbSScott Long } 1027dd7fbcf1SStephen Hurd kring->nr_hwcur = nm_i; 10284c7070dbSScott Long 10294c7070dbSScott Long /* synchronize the NIC ring */ 103095dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 10314c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 10324c7070dbSScott Long 10334c7070dbSScott Long /* (re)start the tx unit up to slot nic_i (excluded) */ 10344c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i); 10354c7070dbSScott Long } 10364c7070dbSScott Long 10374c7070dbSScott Long /* 10384c7070dbSScott Long * Second part: reclaim buffers for completed transmissions. 10395ee36c68SStephen Hurd * 10405ee36c68SStephen Hurd * If there are unclaimed buffers, attempt to reclaim them. 10415ee36c68SStephen Hurd * If none are reclaimed, and TX IRQs are not in use, do an initial 10425ee36c68SStephen Hurd * minimal delay, then trigger the tx handler which will spin in the 10435ee36c68SStephen Hurd * group task queue. 10444c7070dbSScott Long */ 1045dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 10464c7070dbSScott Long if (iflib_tx_credits_update(ctx, txq)) { 10474c7070dbSScott Long /* some tx completed, increment avail */ 10484c7070dbSScott Long nic_i = txq->ift_cidx_processed; 10494c7070dbSScott Long kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim); 10504c7070dbSScott Long } 10515ee36c68SStephen Hurd } 1052dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) 1053dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 1054dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, hz < 2000 ? 1 : hz / 1000, 1055dd7fbcf1SStephen Hurd iflib_timer, txq, txq->ift_timer.c_cpu); 10565ee36c68SStephen Hurd } 10574c7070dbSScott Long return (0); 10584c7070dbSScott Long } 10594c7070dbSScott Long 10604c7070dbSScott Long /* 10614c7070dbSScott Long * Reconcile kernel and user view of the receive ring. 10624c7070dbSScott Long * Same as for the txsync, this routine must be efficient. 10634c7070dbSScott Long * The caller guarantees a single invocations, but races against 10644c7070dbSScott Long * the rest of the driver should be handled here. 10654c7070dbSScott Long * 10664c7070dbSScott Long * On call, kring->rhead is the first packet that userspace wants 10674c7070dbSScott Long * to keep, and kring->rcur is the wakeup point. 10684c7070dbSScott Long * The kernel has previously reported packets up to kring->rtail. 10694c7070dbSScott Long * 10704c7070dbSScott Long * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective 10714c7070dbSScott Long * of whether or not we received an interrupt. 10724c7070dbSScott Long */ 10734c7070dbSScott Long static int 10744c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags) 10754c7070dbSScott Long { 10764c7070dbSScott Long struct netmap_adapter *na = kring->na; 10774c7070dbSScott Long struct netmap_ring *ring = kring->ring; 10781722eeacSMarius Strobl if_t ifp = na->ifp; 107995dcf343SMarius Strobl iflib_fl_t fl; 108095246abbSSean Bruno uint32_t nm_i; /* index into the netmap ring */ 10812d873474SStephen Hurd uint32_t nic_i; /* index into the NIC ring */ 10824c7070dbSScott Long u_int i, n; 10834c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 1084dd7fbcf1SStephen Hurd u_int const head = kring->rhead; 10854c7070dbSScott Long int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR; 1086ab2e3f79SStephen Hurd struct if_rxd_info ri; 108795246abbSSean Bruno 10884c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 10894c7070dbSScott Long iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id]; 10904c7070dbSScott Long if (head > lim) 10914c7070dbSScott Long return netmap_ring_reinit(kring); 10924c7070dbSScott Long 109395dcf343SMarius Strobl /* 109495dcf343SMarius Strobl * XXX netmap_fl_refill() only ever (re)fills free list 0 so far. 109595dcf343SMarius Strobl */ 109695dcf343SMarius Strobl 109795246abbSSean Bruno for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) { 109895dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 10994c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 110095246abbSSean Bruno } 110195dcf343SMarius Strobl 11024c7070dbSScott Long /* 11034c7070dbSScott Long * First part: import newly received packets. 11044c7070dbSScott Long * 11054c7070dbSScott Long * nm_i is the index of the next free slot in the netmap ring, 11064c7070dbSScott Long * nic_i is the index of the next received packet in the NIC ring, 11074c7070dbSScott Long * and they may differ in case if_init() has been called while 11084c7070dbSScott Long * in netmap mode. For the receive ring we have 11094c7070dbSScott Long * 11104c7070dbSScott Long * nic_i = rxr->next_check; 11114c7070dbSScott Long * nm_i = kring->nr_hwtail (previous) 11124c7070dbSScott Long * and 11134c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 11144c7070dbSScott Long * 11154c7070dbSScott Long * rxr->next_check is set to 0 on a ring reinit 11164c7070dbSScott Long */ 11174c7070dbSScott Long if (netmap_no_pendintr || force_update) { 11180ff21267SVincenzo Maffione uint32_t hwtail_lim = nm_prev(kring->nr_hwcur, lim); 11194c7070dbSScott Long int crclen = iflib_crcstrip ? 0 : 4; 11204c7070dbSScott Long int error, avail; 11214c7070dbSScott Long 11222d873474SStephen Hurd for (i = 0; i < rxq->ifr_nfl; i++) { 11232d873474SStephen Hurd fl = &rxq->ifr_fl[i]; 11244c7070dbSScott Long nic_i = fl->ifl_cidx; 11254c7070dbSScott Long nm_i = netmap_idx_n2k(kring, nic_i); 112695dcf343SMarius Strobl avail = ctx->isc_rxd_available(ctx->ifc_softc, 112795dcf343SMarius Strobl rxq->ifr_id, nic_i, USHRT_MAX); 11280ff21267SVincenzo Maffione for (n = 0; avail > 0 && nm_i != hwtail_lim; n++, avail--) { 1129ab2e3f79SStephen Hurd rxd_info_zero(&ri); 1130ab2e3f79SStephen Hurd ri.iri_frags = rxq->ifr_frags; 1131ab2e3f79SStephen Hurd ri.iri_qsidx = kring->ring_id; 1132ab2e3f79SStephen Hurd ri.iri_ifp = ctx->ifc_ifp; 1133ab2e3f79SStephen Hurd ri.iri_cidx = nic_i; 113495246abbSSean Bruno 1135ab2e3f79SStephen Hurd error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 1136ab2e3f79SStephen Hurd ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen; 11377cb7c6e3SNavdeep Parhar ring->slot[nm_i].flags = 0; 113895dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, 1139e035717eSSean Bruno fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD); 11404c7070dbSScott Long nm_i = nm_next(nm_i, lim); 11414c7070dbSScott Long nic_i = nm_next(nic_i, lim); 11424c7070dbSScott Long } 11434c7070dbSScott Long if (n) { /* update the state variables */ 11444c7070dbSScott Long if (netmap_no_pendintr && !force_update) { 11454c7070dbSScott Long /* diagnostics */ 11464c7070dbSScott Long iflib_rx_miss ++; 11474c7070dbSScott Long iflib_rx_miss_bufs += n; 11484c7070dbSScott Long } 11494c7070dbSScott Long fl->ifl_cidx = nic_i; 1150dd7fbcf1SStephen Hurd kring->nr_hwtail = nm_i; 11514c7070dbSScott Long } 11524c7070dbSScott Long kring->nr_kflags &= ~NKR_PENDINTR; 11534c7070dbSScott Long } 11544c7070dbSScott Long } 11554c7070dbSScott Long /* 11564c7070dbSScott Long * Second part: skip past packets that userspace has released. 11574c7070dbSScott Long * (kring->nr_hwcur to head excluded), 11584c7070dbSScott Long * and make the buffers available for reception. 11594c7070dbSScott Long * As usual nm_i is the index in the netmap ring, 11604c7070dbSScott Long * nic_i is the index in the NIC ring, and 11614c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 11624c7070dbSScott Long */ 11634c7070dbSScott Long /* XXX not sure how this will work with multiple free lists */ 1164dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 116595246abbSSean Bruno 11662d873474SStephen Hurd return (netmap_fl_refill(rxq, kring, nm_i, false)); 11674c7070dbSScott Long } 11684c7070dbSScott Long 116995246abbSSean Bruno static void 117095246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff) 117195246abbSSean Bruno { 11721722eeacSMarius Strobl if_ctx_t ctx = na->ifp->if_softc; 117395246abbSSean Bruno 1174ab2e3f79SStephen Hurd CTX_LOCK(ctx); 117595246abbSSean Bruno if (onoff) { 117695246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 117795246abbSSean Bruno } else { 117895246abbSSean Bruno IFDI_INTR_DISABLE(ctx); 117995246abbSSean Bruno } 1180ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 118195246abbSSean Bruno } 118295246abbSSean Bruno 118395246abbSSean Bruno 11844c7070dbSScott Long static int 11854c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx) 11864c7070dbSScott Long { 11874c7070dbSScott Long struct netmap_adapter na; 11884c7070dbSScott Long 11894c7070dbSScott Long bzero(&na, sizeof(na)); 11904c7070dbSScott Long 11914c7070dbSScott Long na.ifp = ctx->ifc_ifp; 11924c7070dbSScott Long na.na_flags = NAF_BDG_MAYSLEEP; 11934c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_ntxqsets); 11944c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_nrxqsets); 11954c7070dbSScott Long 1196ac11d857SVincenzo Maffione na.num_tx_desc = iflib_num_tx_descs(ctx); 1197ac11d857SVincenzo Maffione na.num_rx_desc = iflib_num_rx_descs(ctx); 11984c7070dbSScott Long na.nm_txsync = iflib_netmap_txsync; 11994c7070dbSScott Long na.nm_rxsync = iflib_netmap_rxsync; 12004c7070dbSScott Long na.nm_register = iflib_netmap_register; 120195246abbSSean Bruno na.nm_intr = iflib_netmap_intr; 12024c7070dbSScott Long na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets; 12034c7070dbSScott Long na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets; 12044c7070dbSScott Long return (netmap_attach(&na)); 12054c7070dbSScott Long } 12064c7070dbSScott Long 1207d8b2d26bSVincenzo Maffione static int 12084c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq) 12094c7070dbSScott Long { 12104c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 12114c7070dbSScott Long struct netmap_slot *slot; 12124c7070dbSScott Long 12134c7070dbSScott Long slot = netmap_reset(na, NR_TX, txq->ift_id, 0); 1214e099b90bSPedro F. Giffuni if (slot == NULL) 1215d8b2d26bSVincenzo Maffione return (0); 121623ac9029SStephen Hurd for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) { 12174c7070dbSScott Long 12184c7070dbSScott Long /* 12194c7070dbSScott Long * In netmap mode, set the map for the packet buffer. 12204c7070dbSScott Long * NOTE: Some drivers (not this one) also need to set 12214c7070dbSScott Long * the physical buffer address in the NIC ring. 12224c7070dbSScott Long * netmap_idx_n2k() maps a nic index, i, into the corresponding 12234c7070dbSScott Long * netmap slot index, si 12244c7070dbSScott Long */ 12252ff91c17SVincenzo Maffione int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i); 1226bfce461eSMarius Strobl netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i], 1227bfce461eSMarius Strobl NMB(na, slot + si)); 12284c7070dbSScott Long } 1229d8b2d26bSVincenzo Maffione return (1); 12304c7070dbSScott Long } 12312d873474SStephen Hurd 1232d8b2d26bSVincenzo Maffione static int 12334c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq) 12344c7070dbSScott Long { 12354c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 1236d8b2d26bSVincenzo Maffione struct netmap_kring *kring; 12374c7070dbSScott Long struct netmap_slot *slot; 12382d873474SStephen Hurd uint32_t nm_i; 12394c7070dbSScott Long 12404c7070dbSScott Long slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0); 1241e099b90bSPedro F. Giffuni if (slot == NULL) 1242d8b2d26bSVincenzo Maffione return (0); 1243d8b2d26bSVincenzo Maffione kring = na->rx_rings[rxq->ifr_id]; 12442d873474SStephen Hurd nm_i = netmap_idx_n2k(kring, 0); 12452d873474SStephen Hurd netmap_fl_refill(rxq, kring, nm_i, true); 1246d8b2d26bSVincenzo Maffione return (1); 12474c7070dbSScott Long } 12484c7070dbSScott Long 1249dd7fbcf1SStephen Hurd static void 125095dcf343SMarius Strobl iflib_netmap_timer_adjust(if_ctx_t ctx, iflib_txq_t txq, uint32_t *reset_on) 1251dd7fbcf1SStephen Hurd { 1252dd7fbcf1SStephen Hurd struct netmap_kring *kring; 125395dcf343SMarius Strobl uint16_t txqid; 1254dd7fbcf1SStephen Hurd 125595dcf343SMarius Strobl txqid = txq->ift_id; 125688a68866SVincenzo Maffione kring = netmap_kring_on(NA(ctx->ifc_ifp), txqid, NR_TX); 125788a68866SVincenzo Maffione if (kring == NULL) 125888a68866SVincenzo Maffione return; 1259dd7fbcf1SStephen Hurd 1260dd7fbcf1SStephen Hurd if (kring->nr_hwcur != nm_next(kring->nr_hwtail, kring->nkr_num_slots - 1)) { 126195dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 126295dcf343SMarius Strobl BUS_DMASYNC_POSTREAD); 1263dd7fbcf1SStephen Hurd if (ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false)) 1264dd7fbcf1SStephen Hurd netmap_tx_irq(ctx->ifc_ifp, txqid); 1265dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) { 1266dd7fbcf1SStephen Hurd if (hz < 2000) 1267dd7fbcf1SStephen Hurd *reset_on = 1; 1268dd7fbcf1SStephen Hurd else 1269dd7fbcf1SStephen Hurd *reset_on = hz / 1000; 1270dd7fbcf1SStephen Hurd } 1271dd7fbcf1SStephen Hurd } 1272dd7fbcf1SStephen Hurd } 1273dd7fbcf1SStephen Hurd 12744c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp) 12754c7070dbSScott Long 12764c7070dbSScott Long #else 1277d8b2d26bSVincenzo Maffione #define iflib_netmap_txq_init(ctx, txq) (0) 1278d8b2d26bSVincenzo Maffione #define iflib_netmap_rxq_init(ctx, rxq) (0) 12794c7070dbSScott Long #define iflib_netmap_detach(ifp) 12804c7070dbSScott Long 12814c7070dbSScott Long #define iflib_netmap_attach(ctx) (0) 12824c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0) 128395246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0) 128495dcf343SMarius Strobl #define iflib_netmap_timer_adjust(ctx, txq, reset_on) 12854c7070dbSScott Long #endif 12864c7070dbSScott Long 12874c7070dbSScott Long #if defined(__i386__) || defined(__amd64__) 12884c7070dbSScott Long static __inline void 12894c7070dbSScott Long prefetch(void *x) 12904c7070dbSScott Long { 12914c7070dbSScott Long __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 12924c7070dbSScott Long } 12933429c02fSStephen Hurd static __inline void 12943429c02fSStephen Hurd prefetch2cachelines(void *x) 12953429c02fSStephen Hurd { 12963429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 12973429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128) 12983429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long))))); 12993429c02fSStephen Hurd #endif 13003429c02fSStephen Hurd } 13014c7070dbSScott Long #else 13024c7070dbSScott Long #define prefetch(x) 13033429c02fSStephen Hurd #define prefetch2cachelines(x) 13044c7070dbSScott Long #endif 13054c7070dbSScott Long 13064c7070dbSScott Long static void 130710e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid) 130810e0d938SStephen Hurd { 130910e0d938SStephen Hurd iflib_fl_t fl; 131010e0d938SStephen Hurd 131110e0d938SStephen Hurd fl = &rxq->ifr_fl[flid]; 131210e0d938SStephen Hurd iru->iru_paddrs = fl->ifl_bus_addrs; 131310e0d938SStephen Hurd iru->iru_idxs = fl->ifl_rxd_idxs; 131410e0d938SStephen Hurd iru->iru_qsidx = rxq->ifr_id; 131510e0d938SStephen Hurd iru->iru_buf_size = fl->ifl_buf_size; 131610e0d938SStephen Hurd iru->iru_flidx = fl->ifl_id; 131710e0d938SStephen Hurd } 131810e0d938SStephen Hurd 131910e0d938SStephen Hurd static void 13204c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err) 13214c7070dbSScott Long { 13224c7070dbSScott Long if (err) 13234c7070dbSScott Long return; 13244c7070dbSScott Long *(bus_addr_t *) arg = segs[0].ds_addr; 13254c7070dbSScott Long } 13264c7070dbSScott Long 13274c7070dbSScott Long int 13288f82136aSPatrick Kelsey iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags) 13294c7070dbSScott Long { 13304c7070dbSScott Long int err; 13314c7070dbSScott Long device_t dev = ctx->ifc_dev; 13324c7070dbSScott Long 13334c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 13348f82136aSPatrick Kelsey align, 0, /* alignment, bounds */ 13354c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 13364c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 13374c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 13384c7070dbSScott Long size, /* maxsize */ 13394c7070dbSScott Long 1, /* nsegments */ 13404c7070dbSScott Long size, /* maxsegsize */ 13414c7070dbSScott Long BUS_DMA_ALLOCNOW, /* flags */ 13424c7070dbSScott Long NULL, /* lockfunc */ 13434c7070dbSScott Long NULL, /* lockarg */ 13444c7070dbSScott Long &dma->idi_tag); 13454c7070dbSScott Long if (err) { 13464c7070dbSScott Long device_printf(dev, 13474c7070dbSScott Long "%s: bus_dma_tag_create failed: %d\n", 13484c7070dbSScott Long __func__, err); 13494c7070dbSScott Long goto fail_0; 13504c7070dbSScott Long } 13514c7070dbSScott Long 13524c7070dbSScott Long err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr, 13534c7070dbSScott Long BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map); 13544c7070dbSScott Long if (err) { 13554c7070dbSScott Long device_printf(dev, 13564c7070dbSScott Long "%s: bus_dmamem_alloc(%ju) failed: %d\n", 13574c7070dbSScott Long __func__, (uintmax_t)size, err); 13584c7070dbSScott Long goto fail_1; 13594c7070dbSScott Long } 13604c7070dbSScott Long 13614c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 13624c7070dbSScott Long err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr, 13634c7070dbSScott Long size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT); 13644c7070dbSScott Long if (err || dma->idi_paddr == IF_BAD_DMA) { 13654c7070dbSScott Long device_printf(dev, 13664c7070dbSScott Long "%s: bus_dmamap_load failed: %d\n", 13674c7070dbSScott Long __func__, err); 13684c7070dbSScott Long goto fail_2; 13694c7070dbSScott Long } 13704c7070dbSScott Long 13714c7070dbSScott Long dma->idi_size = size; 13724c7070dbSScott Long return (0); 13734c7070dbSScott Long 13744c7070dbSScott Long fail_2: 13754c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 13764c7070dbSScott Long fail_1: 13774c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 13784c7070dbSScott Long fail_0: 13794c7070dbSScott Long dma->idi_tag = NULL; 13804c7070dbSScott Long 13814c7070dbSScott Long return (err); 13824c7070dbSScott Long } 13834c7070dbSScott Long 13844c7070dbSScott Long int 13858f82136aSPatrick Kelsey iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags) 13868f82136aSPatrick Kelsey { 13878f82136aSPatrick Kelsey if_shared_ctx_t sctx = ctx->ifc_sctx; 13888f82136aSPatrick Kelsey 13898f82136aSPatrick Kelsey KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized")); 13908f82136aSPatrick Kelsey 13918f82136aSPatrick Kelsey return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags)); 13928f82136aSPatrick Kelsey } 13938f82136aSPatrick Kelsey 13948f82136aSPatrick Kelsey int 13954c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count) 13964c7070dbSScott Long { 13974c7070dbSScott Long int i, err; 13984c7070dbSScott Long iflib_dma_info_t *dmaiter; 13994c7070dbSScott Long 14004c7070dbSScott Long dmaiter = dmalist; 14014c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) { 14024c7070dbSScott Long if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0) 14034c7070dbSScott Long break; 14044c7070dbSScott Long } 14054c7070dbSScott Long if (err) 14064c7070dbSScott Long iflib_dma_free_multi(dmalist, i); 14074c7070dbSScott Long return (err); 14084c7070dbSScott Long } 14094c7070dbSScott Long 14104c7070dbSScott Long void 14114c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma) 14124c7070dbSScott Long { 14134c7070dbSScott Long if (dma->idi_tag == NULL) 14144c7070dbSScott Long return; 14154c7070dbSScott Long if (dma->idi_paddr != IF_BAD_DMA) { 14164c7070dbSScott Long bus_dmamap_sync(dma->idi_tag, dma->idi_map, 14174c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 14184c7070dbSScott Long bus_dmamap_unload(dma->idi_tag, dma->idi_map); 14194c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 14204c7070dbSScott Long } 14214c7070dbSScott Long if (dma->idi_vaddr != NULL) { 14224c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 14234c7070dbSScott Long dma->idi_vaddr = NULL; 14244c7070dbSScott Long } 14254c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 14264c7070dbSScott Long dma->idi_tag = NULL; 14274c7070dbSScott Long } 14284c7070dbSScott Long 14294c7070dbSScott Long void 14304c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count) 14314c7070dbSScott Long { 14324c7070dbSScott Long int i; 14334c7070dbSScott Long iflib_dma_info_t *dmaiter = dmalist; 14344c7070dbSScott Long 14354c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) 14364c7070dbSScott Long iflib_dma_free(*dmaiter); 14374c7070dbSScott Long } 14384c7070dbSScott Long 14394c7070dbSScott Long static int 14404c7070dbSScott Long iflib_fast_intr(void *arg) 14414c7070dbSScott Long { 14424c7070dbSScott Long iflib_filter_info_t info = arg; 14434c7070dbSScott Long struct grouptask *gtask = info->ifi_task; 1444ca62461bSStephen Hurd int result; 1445ca62461bSStephen Hurd 144695246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1447ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1448ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1449ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1450ca62461bSStephen Hurd return (result); 1451ca62461bSStephen Hurd } 145295246abbSSean Bruno 145395246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 145495246abbSSean Bruno return (FILTER_HANDLED); 145595246abbSSean Bruno } 145695246abbSSean Bruno 145795246abbSSean Bruno static int 145895246abbSSean Bruno iflib_fast_intr_rxtx(void *arg) 145995246abbSSean Bruno { 146095246abbSSean Bruno iflib_filter_info_t info = arg; 146195246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 146295dcf343SMarius Strobl if_ctx_t ctx; 146395246abbSSean Bruno iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx; 146495dcf343SMarius Strobl iflib_txq_t txq; 146595dcf343SMarius Strobl void *sc; 1466ca62461bSStephen Hurd int i, cidx, result; 146795dcf343SMarius Strobl qidx_t txqid; 14683d10e9edSMarius Strobl bool intr_enable, intr_legacy; 146995246abbSSean Bruno 147095246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1471ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1472ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1473ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1474ca62461bSStephen Hurd return (result); 1475ca62461bSStephen Hurd } 147695246abbSSean Bruno 147795dcf343SMarius Strobl ctx = rxq->ifr_ctx; 147895dcf343SMarius Strobl sc = ctx->ifc_softc; 14793d10e9edSMarius Strobl intr_enable = false; 14803d10e9edSMarius Strobl intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY); 14811ae4848cSMatt Macy MPASS(rxq->ifr_ntxqirq); 148295246abbSSean Bruno for (i = 0; i < rxq->ifr_ntxqirq; i++) { 148395dcf343SMarius Strobl txqid = rxq->ifr_txqid[i]; 148495dcf343SMarius Strobl txq = &ctx->ifc_txqs[txqid]; 148595dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 14868a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 148795dcf343SMarius Strobl if (!ctx->isc_txd_credits_update(sc, txqid, false)) { 14883d10e9edSMarius Strobl if (intr_legacy) 14893d10e9edSMarius Strobl intr_enable = true; 14903d10e9edSMarius Strobl else 149195246abbSSean Bruno IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid); 149295246abbSSean Bruno continue; 149395246abbSSean Bruno } 149495dcf343SMarius Strobl GROUPTASK_ENQUEUE(&txq->ift_task); 149595246abbSSean Bruno } 149695246abbSSean Bruno if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ) 149795246abbSSean Bruno cidx = rxq->ifr_cq_cidx; 149895246abbSSean Bruno else 149995246abbSSean Bruno cidx = rxq->ifr_fl[0].ifl_cidx; 150095246abbSSean Bruno if (iflib_rxd_avail(ctx, rxq, cidx, 1)) 150195246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 150264e6fc13SStephen Hurd else { 15033d10e9edSMarius Strobl if (intr_legacy) 15043d10e9edSMarius Strobl intr_enable = true; 15053d10e9edSMarius Strobl else 150695246abbSSean Bruno IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 150764e6fc13SStephen Hurd DBG_COUNTER_INC(rx_intr_enables); 150864e6fc13SStephen Hurd } 15093d10e9edSMarius Strobl if (intr_enable) 15103d10e9edSMarius Strobl IFDI_INTR_ENABLE(ctx); 151195246abbSSean Bruno return (FILTER_HANDLED); 151295246abbSSean Bruno } 151395246abbSSean Bruno 151495246abbSSean Bruno 151595246abbSSean Bruno static int 151695246abbSSean Bruno iflib_fast_intr_ctx(void *arg) 151795246abbSSean Bruno { 151895246abbSSean Bruno iflib_filter_info_t info = arg; 151995246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 1520ca62461bSStephen Hurd int result; 15214c7070dbSScott Long 15224c7070dbSScott Long DBG_COUNTER_INC(fast_intrs); 1523ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1524ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1525ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1526ca62461bSStephen Hurd return (result); 1527ca62461bSStephen Hurd } 15284c7070dbSScott Long 15294c7070dbSScott Long GROUPTASK_ENQUEUE(gtask); 15304c7070dbSScott Long return (FILTER_HANDLED); 15314c7070dbSScott Long } 15324c7070dbSScott Long 15334c7070dbSScott Long static int 15344c7070dbSScott Long _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 15354c7070dbSScott Long driver_filter_t filter, driver_intr_t handler, void *arg, 15363e0e6330SStephen Hurd const char *name) 15374c7070dbSScott Long { 15384c7070dbSScott Long struct resource *res; 15392b2fc973SSean Bruno void *tag = NULL; 15404c7070dbSScott Long device_t dev = ctx->ifc_dev; 1541d49e83eaSMarius Strobl int flags, i, rc; 15424c7070dbSScott Long 15432b2fc973SSean Bruno flags = RF_ACTIVE; 15442b2fc973SSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 15452b2fc973SSean Bruno flags |= RF_SHAREABLE; 15464c7070dbSScott Long MPASS(rid < 512); 1547d49e83eaSMarius Strobl i = rid; 1548d49e83eaSMarius Strobl res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, flags); 15494c7070dbSScott Long if (res == NULL) { 15504c7070dbSScott Long device_printf(dev, 15514c7070dbSScott Long "failed to allocate IRQ for rid %d, name %s.\n", rid, name); 15524c7070dbSScott Long return (ENOMEM); 15534c7070dbSScott Long } 15544c7070dbSScott Long irq->ii_res = res; 15554c7070dbSScott Long KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL")); 15564c7070dbSScott Long rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET, 15574c7070dbSScott Long filter, handler, arg, &tag); 15584c7070dbSScott Long if (rc != 0) { 15594c7070dbSScott Long device_printf(dev, 15604c7070dbSScott Long "failed to setup interrupt for rid %d, name %s: %d\n", 15614c7070dbSScott Long rid, name ? name : "unknown", rc); 15624c7070dbSScott Long return (rc); 15634c7070dbSScott Long } else if (name) 1564f454e7ebSJohn Baldwin bus_describe_intr(dev, res, tag, "%s", name); 15654c7070dbSScott Long 15664c7070dbSScott Long irq->ii_tag = tag; 15674c7070dbSScott Long return (0); 15684c7070dbSScott Long } 15694c7070dbSScott Long 15704c7070dbSScott Long /********************************************************************* 15714c7070dbSScott Long * 1572bfce461eSMarius Strobl * Allocate DMA resources for TX buffers as well as memory for the TX 1573bfce461eSMarius Strobl * mbuf map. TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a 1574bfce461eSMarius Strobl * iflib_sw_tx_desc_array structure, storing all the information that 1575bfce461eSMarius Strobl * is needed to transmit a packet on the wire. This is called only 1576bfce461eSMarius Strobl * once at attach, setup is done every reset. 15774c7070dbSScott Long * 15784c7070dbSScott Long **********************************************************************/ 15794c7070dbSScott Long static int 15804c7070dbSScott Long iflib_txsd_alloc(iflib_txq_t txq) 15814c7070dbSScott Long { 15824c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 15834c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 15844c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 15854c7070dbSScott Long device_t dev = ctx->ifc_dev; 15867f87c040SMarius Strobl bus_size_t tsomaxsize; 15874c7070dbSScott Long int err, nsegments, ntsosegments; 15888a04b53dSKonstantin Belousov bool tso; 15894c7070dbSScott Long 15904c7070dbSScott Long nsegments = scctx->isc_tx_nsegments; 15914c7070dbSScott Long ntsosegments = scctx->isc_tx_tso_segments_max; 15927f87c040SMarius Strobl tsomaxsize = scctx->isc_tx_tso_size_max; 15937f87c040SMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU) 15947f87c040SMarius Strobl tsomaxsize += sizeof(struct ether_vlan_header); 159523ac9029SStephen Hurd MPASS(scctx->isc_ntxd[0] > 0); 159623ac9029SStephen Hurd MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0); 15974c7070dbSScott Long MPASS(nsegments > 0); 15987f87c040SMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) { 15994c7070dbSScott Long MPASS(ntsosegments > 0); 16007f87c040SMarius Strobl MPASS(sctx->isc_tso_maxsize >= tsomaxsize); 16017f87c040SMarius Strobl } 16027f87c040SMarius Strobl 16034c7070dbSScott Long /* 1604bfce461eSMarius Strobl * Set up DMA tags for TX buffers. 16054c7070dbSScott Long */ 16064c7070dbSScott Long if ((err = bus_dma_tag_create(bus_get_dma_tag(dev), 16074c7070dbSScott Long 1, 0, /* alignment, bounds */ 16084c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 16094c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 16104c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 16114c7070dbSScott Long sctx->isc_tx_maxsize, /* maxsize */ 16124c7070dbSScott Long nsegments, /* nsegments */ 16134c7070dbSScott Long sctx->isc_tx_maxsegsize, /* maxsegsize */ 16144c7070dbSScott Long 0, /* flags */ 16154c7070dbSScott Long NULL, /* lockfunc */ 16164c7070dbSScott Long NULL, /* lockfuncarg */ 1617bfce461eSMarius Strobl &txq->ift_buf_tag))) { 16184c7070dbSScott Long device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err); 16199d0a88deSDimitry Andric device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n", 16209d0a88deSDimitry Andric (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize); 16214c7070dbSScott Long goto fail; 16224c7070dbSScott Long } 16238a04b53dSKonstantin Belousov tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0; 16248a04b53dSKonstantin Belousov if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev), 16254c7070dbSScott Long 1, 0, /* alignment, bounds */ 16264c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 16274c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 16284c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 16297f87c040SMarius Strobl tsomaxsize, /* maxsize */ 16304c7070dbSScott Long ntsosegments, /* nsegments */ 16317f87c040SMarius Strobl sctx->isc_tso_maxsegsize,/* maxsegsize */ 16324c7070dbSScott Long 0, /* flags */ 16334c7070dbSScott Long NULL, /* lockfunc */ 16344c7070dbSScott Long NULL, /* lockfuncarg */ 1635bfce461eSMarius Strobl &txq->ift_tso_buf_tag))) { 1636bfce461eSMarius Strobl device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n", 1637bfce461eSMarius Strobl err); 16384c7070dbSScott Long goto fail; 16394c7070dbSScott Long } 1640bfce461eSMarius Strobl 1641bfce461eSMarius Strobl /* Allocate memory for the TX mbuf map. */ 16424c7070dbSScott Long if (!(txq->ift_sds.ifsd_m = 1643ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1644ac2fffa4SPedro F. Giffuni scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1645bfce461eSMarius Strobl device_printf(dev, "Unable to allocate TX mbuf map memory\n"); 16464c7070dbSScott Long err = ENOMEM; 16474c7070dbSScott Long goto fail; 16484c7070dbSScott Long } 16494c7070dbSScott Long 1650bfce461eSMarius Strobl /* 1651bfce461eSMarius Strobl * Create the DMA maps for TX buffers. 1652bfce461eSMarius Strobl */ 16538a04b53dSKonstantin Belousov if ((txq->ift_sds.ifsd_map = (bus_dmamap_t *)malloc( 16548a04b53dSKonstantin Belousov sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], 16558a04b53dSKonstantin Belousov M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 1656bfce461eSMarius Strobl device_printf(dev, 1657bfce461eSMarius Strobl "Unable to allocate TX buffer DMA map memory\n"); 16584c7070dbSScott Long err = ENOMEM; 16594c7070dbSScott Long goto fail; 16604c7070dbSScott Long } 16618a04b53dSKonstantin Belousov if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc( 16628a04b53dSKonstantin Belousov sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], 16638a04b53dSKonstantin Belousov M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 1664bfce461eSMarius Strobl device_printf(dev, 1665bfce461eSMarius Strobl "Unable to allocate TSO TX buffer map memory\n"); 16668a04b53dSKonstantin Belousov err = ENOMEM; 16678a04b53dSKonstantin Belousov goto fail; 16688a04b53dSKonstantin Belousov } 166923ac9029SStephen Hurd for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) { 1670bfce461eSMarius Strobl err = bus_dmamap_create(txq->ift_buf_tag, 0, 16718a04b53dSKonstantin Belousov &txq->ift_sds.ifsd_map[i]); 16724c7070dbSScott Long if (err != 0) { 16734c7070dbSScott Long device_printf(dev, "Unable to create TX DMA map\n"); 16744c7070dbSScott Long goto fail; 16754c7070dbSScott Long } 16768a04b53dSKonstantin Belousov if (!tso) 16778a04b53dSKonstantin Belousov continue; 1678bfce461eSMarius Strobl err = bus_dmamap_create(txq->ift_tso_buf_tag, 0, 16798a04b53dSKonstantin Belousov &txq->ift_sds.ifsd_tso_map[i]); 16808a04b53dSKonstantin Belousov if (err != 0) { 16818a04b53dSKonstantin Belousov device_printf(dev, "Unable to create TSO TX DMA map\n"); 16828a04b53dSKonstantin Belousov goto fail; 16838a04b53dSKonstantin Belousov } 16844c7070dbSScott Long } 16854c7070dbSScott Long return (0); 16864c7070dbSScott Long fail: 16874c7070dbSScott Long /* We free all, it handles case where we are in the middle */ 16884c7070dbSScott Long iflib_tx_structures_free(ctx); 16894c7070dbSScott Long return (err); 16904c7070dbSScott Long } 16914c7070dbSScott Long 16924c7070dbSScott Long static void 16934c7070dbSScott Long iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i) 16944c7070dbSScott Long { 16954c7070dbSScott Long bus_dmamap_t map; 16964c7070dbSScott Long 1697db8e8f1eSEric Joyner if (txq->ift_sds.ifsd_map != NULL) { 16984c7070dbSScott Long map = txq->ift_sds.ifsd_map[i]; 1699bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE); 1700bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, map); 1701bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_buf_tag, map); 17024c7070dbSScott Long txq->ift_sds.ifsd_map[i] = NULL; 17034c7070dbSScott Long } 17048a04b53dSKonstantin Belousov 1705db8e8f1eSEric Joyner if (txq->ift_sds.ifsd_tso_map != NULL) { 17068a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[i]; 1707bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, map, 17088a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 1709bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, map); 1710bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_tso_buf_tag, map); 17118a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i] = NULL; 17128a04b53dSKonstantin Belousov } 17134c7070dbSScott Long } 17144c7070dbSScott Long 17154c7070dbSScott Long static void 17164c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq) 17174c7070dbSScott Long { 17184c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 17194c7070dbSScott Long 172023ac9029SStephen Hurd for (int i = 0; i < txq->ift_size; i++) 17214c7070dbSScott Long iflib_txsd_destroy(ctx, txq, i); 1722244e7cffSEric Joyner 1723244e7cffSEric Joyner if (txq->ift_br != NULL) { 1724244e7cffSEric Joyner ifmp_ring_free(txq->ift_br); 1725244e7cffSEric Joyner txq->ift_br = NULL; 1726244e7cffSEric Joyner } 1727244e7cffSEric Joyner 1728244e7cffSEric Joyner mtx_destroy(&txq->ift_mtx); 1729244e7cffSEric Joyner 17304c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 17314c7070dbSScott Long free(txq->ift_sds.ifsd_map, M_IFLIB); 17324c7070dbSScott Long txq->ift_sds.ifsd_map = NULL; 17334c7070dbSScott Long } 17348a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 17358a04b53dSKonstantin Belousov free(txq->ift_sds.ifsd_tso_map, M_IFLIB); 17368a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map = NULL; 17378a04b53dSKonstantin Belousov } 17384c7070dbSScott Long if (txq->ift_sds.ifsd_m != NULL) { 17394c7070dbSScott Long free(txq->ift_sds.ifsd_m, M_IFLIB); 17404c7070dbSScott Long txq->ift_sds.ifsd_m = NULL; 17414c7070dbSScott Long } 1742bfce461eSMarius Strobl if (txq->ift_buf_tag != NULL) { 1743bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_buf_tag); 1744bfce461eSMarius Strobl txq->ift_buf_tag = NULL; 17454c7070dbSScott Long } 1746bfce461eSMarius Strobl if (txq->ift_tso_buf_tag != NULL) { 1747bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_tso_buf_tag); 1748bfce461eSMarius Strobl txq->ift_tso_buf_tag = NULL; 17494c7070dbSScott Long } 1750244e7cffSEric Joyner if (txq->ift_ifdi != NULL) { 1751244e7cffSEric Joyner free(txq->ift_ifdi, M_IFLIB); 1752244e7cffSEric Joyner } 17534c7070dbSScott Long } 17544c7070dbSScott Long 17554c7070dbSScott Long static void 17564c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i) 17574c7070dbSScott Long { 17584c7070dbSScott Long struct mbuf **mp; 17594c7070dbSScott Long 17604c7070dbSScott Long mp = &txq->ift_sds.ifsd_m[i]; 17614c7070dbSScott Long if (*mp == NULL) 17624c7070dbSScott Long return; 17634c7070dbSScott Long 17644c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 1765bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 17668a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE); 1767bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]); 17688a04b53dSKonstantin Belousov } 17698a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 1770bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 17718a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE); 1772bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 17738a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i]); 17744c7070dbSScott Long } 177523ac9029SStephen Hurd m_free(*mp); 17764c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 17774c7070dbSScott Long *mp = NULL; 17784c7070dbSScott Long } 17794c7070dbSScott Long 17804c7070dbSScott Long static int 17814c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq) 17824c7070dbSScott Long { 17834c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 178423ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 17854d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 17864c7070dbSScott Long iflib_dma_info_t di; 17874c7070dbSScott Long int i; 17884c7070dbSScott Long 17894c7070dbSScott Long /* Set number of descriptors available */ 17904c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 179195246abbSSean Bruno /* XXX make configurable */ 179295246abbSSean Bruno txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ; 17934c7070dbSScott Long 17944c7070dbSScott Long /* Reset indices */ 179595246abbSSean Bruno txq->ift_cidx_processed = 0; 179695246abbSSean Bruno txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0; 179723ac9029SStephen Hurd txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset]; 17984c7070dbSScott Long 17994d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 18004c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 18014c7070dbSScott Long 18024c7070dbSScott Long IFDI_TXQ_SETUP(ctx, txq->ift_id); 18034d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 18044c7070dbSScott Long bus_dmamap_sync(di->idi_tag, di->idi_map, 18054c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 18064c7070dbSScott Long return (0); 18074c7070dbSScott Long } 18084c7070dbSScott Long 18094c7070dbSScott Long /********************************************************************* 18104c7070dbSScott Long * 1811bfce461eSMarius Strobl * Allocate DMA resources for RX buffers as well as memory for the RX 1812bfce461eSMarius Strobl * mbuf map, direct RX cluster pointer map and RX cluster bus address 1813bfce461eSMarius Strobl * map. RX DMA map, RX mbuf map, direct RX cluster pointer map and 1814bfce461eSMarius Strobl * RX cluster map are kept in a iflib_sw_rx_desc_array structure. 1815bfce461eSMarius Strobl * Since we use use one entry in iflib_sw_rx_desc_array per received 1816bfce461eSMarius Strobl * packet, the maximum number of entries we'll need is equal to the 1817bfce461eSMarius Strobl * number of hardware receive descriptors that we've allocated. 18184c7070dbSScott Long * 18194c7070dbSScott Long **********************************************************************/ 18204c7070dbSScott Long static int 18214c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq) 18224c7070dbSScott Long { 18234c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 18244c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 182523ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 18264c7070dbSScott Long device_t dev = ctx->ifc_dev; 18274c7070dbSScott Long iflib_fl_t fl; 18284c7070dbSScott Long int err; 18294c7070dbSScott Long 183023ac9029SStephen Hurd MPASS(scctx->isc_nrxd[0] > 0); 183123ac9029SStephen Hurd MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0); 18324c7070dbSScott Long 18334c7070dbSScott Long fl = rxq->ifr_fl; 18344c7070dbSScott Long for (int i = 0; i < rxq->ifr_nfl; i++, fl++) { 183523ac9029SStephen Hurd fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */ 1836bfce461eSMarius Strobl /* Set up DMA tag for RX buffers. */ 18374c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 18384c7070dbSScott Long 1, 0, /* alignment, bounds */ 18394c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 18404c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 18414c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 18424c7070dbSScott Long sctx->isc_rx_maxsize, /* maxsize */ 18434c7070dbSScott Long sctx->isc_rx_nsegments, /* nsegments */ 18444c7070dbSScott Long sctx->isc_rx_maxsegsize, /* maxsegsize */ 18454c7070dbSScott Long 0, /* flags */ 18464c7070dbSScott Long NULL, /* lockfunc */ 18474c7070dbSScott Long NULL, /* lockarg */ 1848bfce461eSMarius Strobl &fl->ifl_buf_tag); 18494c7070dbSScott Long if (err) { 1850bfce461eSMarius Strobl device_printf(dev, 1851bfce461eSMarius Strobl "Unable to allocate RX DMA tag: %d\n", err); 18524c7070dbSScott Long goto fail; 18534c7070dbSScott Long } 1854bfce461eSMarius Strobl 1855bfce461eSMarius Strobl /* Allocate memory for the RX mbuf map. */ 1856e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_m = 1857ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1858ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1859bfce461eSMarius Strobl device_printf(dev, 1860bfce461eSMarius Strobl "Unable to allocate RX mbuf map memory\n"); 1861e035717eSSean Bruno err = ENOMEM; 1862e035717eSSean Bruno goto fail; 1863e035717eSSean Bruno } 1864bfce461eSMarius Strobl 1865bfce461eSMarius Strobl /* Allocate memory for the direct RX cluster pointer map. */ 1866e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_cl = 1867ac2fffa4SPedro F. Giffuni (caddr_t *) malloc(sizeof(caddr_t) * 1868ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1869bfce461eSMarius Strobl device_printf(dev, 1870bfce461eSMarius Strobl "Unable to allocate RX cluster map memory\n"); 1871e035717eSSean Bruno err = ENOMEM; 1872e035717eSSean Bruno goto fail; 1873e035717eSSean Bruno } 18744c7070dbSScott Long 1875bfce461eSMarius Strobl /* Allocate memory for the RX cluster bus address map. */ 1876fbec776dSAndrew Gallatin if (!(fl->ifl_sds.ifsd_ba = 1877fbec776dSAndrew Gallatin (bus_addr_t *) malloc(sizeof(bus_addr_t) * 1878fbec776dSAndrew Gallatin scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1879bfce461eSMarius Strobl device_printf(dev, 1880bfce461eSMarius Strobl "Unable to allocate RX bus address map memory\n"); 1881fbec776dSAndrew Gallatin err = ENOMEM; 1882fbec776dSAndrew Gallatin goto fail; 1883fbec776dSAndrew Gallatin } 1884e035717eSSean Bruno 1885bfce461eSMarius Strobl /* 1886bfce461eSMarius Strobl * Create the DMA maps for RX buffers. 1887bfce461eSMarius Strobl */ 1888e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_map = 1889ac2fffa4SPedro F. Giffuni (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1890bfce461eSMarius Strobl device_printf(dev, 1891bfce461eSMarius Strobl "Unable to allocate RX buffer DMA map memory\n"); 1892e035717eSSean Bruno err = ENOMEM; 1893e035717eSSean Bruno goto fail; 1894e035717eSSean Bruno } 1895e035717eSSean Bruno for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) { 1896bfce461eSMarius Strobl err = bus_dmamap_create(fl->ifl_buf_tag, 0, 1897bfce461eSMarius Strobl &fl->ifl_sds.ifsd_map[i]); 1898e035717eSSean Bruno if (err != 0) { 189995246abbSSean Bruno device_printf(dev, "Unable to create RX buffer DMA map\n"); 19004c7070dbSScott Long goto fail; 19014c7070dbSScott Long } 19024c7070dbSScott Long } 1903835809f9SSean Bruno } 19044c7070dbSScott Long return (0); 19054c7070dbSScott Long 19064c7070dbSScott Long fail: 19074c7070dbSScott Long iflib_rx_structures_free(ctx); 19084c7070dbSScott Long return (err); 19094c7070dbSScott Long } 19104c7070dbSScott Long 19114c7070dbSScott Long 19124c7070dbSScott Long /* 19134c7070dbSScott Long * Internal service routines 19144c7070dbSScott Long */ 19154c7070dbSScott Long 19164c7070dbSScott Long struct rxq_refill_cb_arg { 19174c7070dbSScott Long int error; 19184c7070dbSScott Long bus_dma_segment_t seg; 19194c7070dbSScott Long int nseg; 19204c7070dbSScott Long }; 19214c7070dbSScott Long 19224c7070dbSScott Long static void 19234c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 19244c7070dbSScott Long { 19254c7070dbSScott Long struct rxq_refill_cb_arg *cb_arg = arg; 19264c7070dbSScott Long 19274c7070dbSScott Long cb_arg->error = error; 19284c7070dbSScott Long cb_arg->seg = segs[0]; 19294c7070dbSScott Long cb_arg->nseg = nseg; 19304c7070dbSScott Long } 19314c7070dbSScott Long 19324c7070dbSScott Long /** 1933b256d25cSMark Johnston * iflib_fl_refill - refill an rxq free-buffer list 19344c7070dbSScott Long * @ctx: the iflib context 19351722eeacSMarius Strobl * @fl: the free list to refill 19361722eeacSMarius Strobl * @count: the number of new buffers to allocate 19374c7070dbSScott Long * 19381722eeacSMarius Strobl * (Re)populate an rxq free-buffer list with up to @count new packet buffers. 19391722eeacSMarius Strobl * The caller must assure that @count does not exceed the queue's capacity. 19404c7070dbSScott Long */ 1941fb1a29b4SHans Petter Selasky static uint8_t 1942b256d25cSMark Johnston iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count) 19434c7070dbSScott Long { 194495246abbSSean Bruno struct if_rxd_update iru; 1945fbec776dSAndrew Gallatin struct rxq_refill_cb_arg cb_arg; 19463db348b5SMarius Strobl struct mbuf *m; 19473db348b5SMarius Strobl caddr_t cl, *sd_cl; 19483db348b5SMarius Strobl struct mbuf **sd_m; 1949e035717eSSean Bruno bus_dmamap_t *sd_map; 1950fbec776dSAndrew Gallatin bus_addr_t bus_addr, *sd_ba; 19513db348b5SMarius Strobl int err, frag_idx, i, idx, n, pidx; 1952a1b799caSStephen Hurd qidx_t credits; 19534c7070dbSScott Long 1954e035717eSSean Bruno sd_m = fl->ifl_sds.ifsd_m; 1955e035717eSSean Bruno sd_map = fl->ifl_sds.ifsd_map; 1956e035717eSSean Bruno sd_cl = fl->ifl_sds.ifsd_cl; 1957fbec776dSAndrew Gallatin sd_ba = fl->ifl_sds.ifsd_ba; 19583db348b5SMarius Strobl pidx = fl->ifl_pidx; 1959e035717eSSean Bruno idx = pidx; 19603db348b5SMarius Strobl frag_idx = fl->ifl_fragidx; 1961a1b799caSStephen Hurd credits = fl->ifl_credits; 1962e035717eSSean Bruno 19633db348b5SMarius Strobl i = 0; 19644c7070dbSScott Long n = count; 19654c7070dbSScott Long MPASS(n > 0); 1966a1b799caSStephen Hurd MPASS(credits + n <= fl->ifl_size); 19674c7070dbSScott Long 19684c7070dbSScott Long if (pidx < fl->ifl_cidx) 19694c7070dbSScott Long MPASS(pidx + n <= fl->ifl_cidx); 1970a1b799caSStephen Hurd if (pidx == fl->ifl_cidx && (credits < fl->ifl_size)) 19714c7070dbSScott Long MPASS(fl->ifl_gen == 0); 19724c7070dbSScott Long if (pidx > fl->ifl_cidx) 19734c7070dbSScott Long MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx); 19744c7070dbSScott Long 19754c7070dbSScott Long DBG_COUNTER_INC(fl_refills); 19764c7070dbSScott Long if (n > 8) 19774c7070dbSScott Long DBG_COUNTER_INC(fl_refills_large); 19782d873474SStephen Hurd iru_init(&iru, fl->ifl_rxq, fl->ifl_id); 1979b256d25cSMark Johnston while (n-- > 0) { 19804c7070dbSScott Long /* 19814c7070dbSScott Long * We allocate an uninitialized mbuf + cluster, mbuf is 19824c7070dbSScott Long * initialized after rx. 19834c7070dbSScott Long * 1984b256d25cSMark Johnston * If the cluster is still set then we know a minimum sized 1985b256d25cSMark Johnston * packet was received 19864c7070dbSScott Long */ 19873db348b5SMarius Strobl bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size, 19883db348b5SMarius Strobl &frag_idx); 19893db348b5SMarius Strobl if (frag_idx < 0) 199087890dbaSSean Bruno bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx); 19913db348b5SMarius Strobl MPASS(frag_idx >= 0); 199287890dbaSSean Bruno if ((cl = sd_cl[frag_idx]) == NULL) { 1993b256d25cSMark Johnston cl = uma_zalloc(fl->ifl_zone, M_NOWAIT); 1994a363e1d4SMark Johnston if (__predict_false(cl == NULL)) 19954c7070dbSScott Long break; 19964c7070dbSScott Long 19974c7070dbSScott Long cb_arg.error = 0; 199895246abbSSean Bruno MPASS(sd_map != NULL); 1999bfce461eSMarius Strobl err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx], 20008a04b53dSKonstantin Belousov cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 20018a04b53dSKonstantin Belousov BUS_DMA_NOWAIT); 2002a363e1d4SMark Johnston if (__predict_false(err != 0 || cb_arg.error)) { 20034c7070dbSScott Long uma_zfree(fl->ifl_zone, cl); 2004fbec776dSAndrew Gallatin break; 20054c7070dbSScott Long } 20064c7070dbSScott Long 2007fbec776dSAndrew Gallatin sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr; 200887890dbaSSean Bruno sd_cl[frag_idx] = cl; 2009fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2010fbec776dSAndrew Gallatin fl->ifl_cl_enqueued++; 2011fbec776dSAndrew Gallatin #endif 2012fbec776dSAndrew Gallatin } else { 2013fbec776dSAndrew Gallatin bus_addr = sd_ba[frag_idx]; 2014fbec776dSAndrew Gallatin } 201595dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx], 201695dcf343SMarius Strobl BUS_DMASYNC_PREREAD); 2017fbec776dSAndrew Gallatin 20186d49b41eSAndrew Gallatin if (sd_m[frag_idx] == NULL) { 2019a363e1d4SMark Johnston m = m_gethdr(M_NOWAIT, MT_NOINIT); 2020a363e1d4SMark Johnston if (__predict_false(m == NULL)) 2021fbec776dSAndrew Gallatin break; 202287890dbaSSean Bruno sd_m[frag_idx] = m; 20236d49b41eSAndrew Gallatin } 20243db348b5SMarius Strobl bit_set(fl->ifl_rx_bitmap, frag_idx); 2025fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2026fbec776dSAndrew Gallatin fl->ifl_m_enqueued++; 2027fbec776dSAndrew Gallatin #endif 2028fbec776dSAndrew Gallatin 2029fbec776dSAndrew Gallatin DBG_COUNTER_INC(rx_allocs); 203087890dbaSSean Bruno fl->ifl_rxd_idxs[i] = frag_idx; 20314c7070dbSScott Long fl->ifl_bus_addrs[i] = bus_addr; 2032a1b799caSStephen Hurd credits++; 20334c7070dbSScott Long i++; 2034a1b799caSStephen Hurd MPASS(credits <= fl->ifl_size); 2035e035717eSSean Bruno if (++idx == fl->ifl_size) { 2036b256d25cSMark Johnston #ifdef INVARIANTS 20374c7070dbSScott Long fl->ifl_gen = 1; 2038b256d25cSMark Johnston #endif 2039e035717eSSean Bruno idx = 0; 20404c7070dbSScott Long } 20414c7070dbSScott Long if (n == 0 || i == IFLIB_MAX_RX_REFRESH) { 204295246abbSSean Bruno iru.iru_pidx = pidx; 204395246abbSSean Bruno iru.iru_count = i; 204495246abbSSean Bruno ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 2045fa5416a8SSean Bruno fl->ifl_pidx = idx; 2046a1b799caSStephen Hurd fl->ifl_credits = credits; 2047b256d25cSMark Johnston pidx = idx; 2048b256d25cSMark Johnston i = 0; 204987890dbaSSean Bruno } 20504c7070dbSScott Long } 2051fbec776dSAndrew Gallatin 2052a363e1d4SMark Johnston if (n < count - 1) { 2053a363e1d4SMark Johnston if (i != 0) { 2054a1b799caSStephen Hurd iru.iru_pidx = pidx; 2055a1b799caSStephen Hurd iru.iru_count = i; 2056a1b799caSStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 2057a1b799caSStephen Hurd fl->ifl_pidx = idx; 2058a1b799caSStephen Hurd fl->ifl_credits = credits; 2059a1b799caSStephen Hurd } 20604c7070dbSScott Long DBG_COUNTER_INC(rxd_flush); 20614c7070dbSScott Long if (fl->ifl_pidx == 0) 20624c7070dbSScott Long pidx = fl->ifl_size - 1; 20634c7070dbSScott Long else 20644c7070dbSScott Long pidx = fl->ifl_pidx - 1; 206595246abbSSean Bruno 206695246abbSSean Bruno bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 206795246abbSSean Bruno BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2068a363e1d4SMark Johnston ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, 2069a363e1d4SMark Johnston fl->ifl_id, pidx); 2070a363e1d4SMark Johnston if (__predict_true(bit_test(fl->ifl_rx_bitmap, frag_idx))) { 20719e9b738aSPatrick Kelsey fl->ifl_fragidx = frag_idx + 1; 20729e9b738aSPatrick Kelsey if (fl->ifl_fragidx == fl->ifl_size) 20739e9b738aSPatrick Kelsey fl->ifl_fragidx = 0; 2074a363e1d4SMark Johnston } else { 2075a363e1d4SMark Johnston fl->ifl_fragidx = frag_idx; 2076a363e1d4SMark Johnston } 2077a363e1d4SMark Johnston } 2078fb1a29b4SHans Petter Selasky 2079fb1a29b4SHans Petter Selasky return (n == -1 ? 0 : IFLIB_RXEOF_EMPTY); 20804c7070dbSScott Long } 20814c7070dbSScott Long 2082b256d25cSMark Johnston static inline uint8_t 2083b256d25cSMark Johnston iflib_fl_refill_all(if_ctx_t ctx, iflib_fl_t fl) 20844c7070dbSScott Long { 20854c7070dbSScott Long /* we avoid allowing pidx to catch up with cidx as it confuses ixl */ 20864c7070dbSScott Long int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1; 20874c7070dbSScott Long #ifdef INVARIANTS 20884c7070dbSScott Long int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1; 20894c7070dbSScott Long #endif 20904c7070dbSScott Long 20914c7070dbSScott Long MPASS(fl->ifl_credits <= fl->ifl_size); 20924c7070dbSScott Long MPASS(reclaimable == delta); 20934c7070dbSScott Long 20944c7070dbSScott Long if (reclaimable > 0) 2095b256d25cSMark Johnston return (iflib_fl_refill(ctx, fl, reclaimable)); 2096fb1a29b4SHans Petter Selasky return (0); 20974c7070dbSScott Long } 20984c7070dbSScott Long 209977c1fcecSEric Joyner uint8_t 210077c1fcecSEric Joyner iflib_in_detach(if_ctx_t ctx) 210177c1fcecSEric Joyner { 210277c1fcecSEric Joyner bool in_detach; 21031722eeacSMarius Strobl 210477c1fcecSEric Joyner STATE_LOCK(ctx); 210577c1fcecSEric Joyner in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH); 210677c1fcecSEric Joyner STATE_UNLOCK(ctx); 210777c1fcecSEric Joyner return (in_detach); 210877c1fcecSEric Joyner } 210977c1fcecSEric Joyner 21104c7070dbSScott Long static void 21114c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl) 21124c7070dbSScott Long { 21134c7070dbSScott Long iflib_dma_info_t idi = fl->ifl_ifdi; 21148a04b53dSKonstantin Belousov bus_dmamap_t sd_map; 21154c7070dbSScott Long uint32_t i; 21164c7070dbSScott Long 21174c7070dbSScott Long for (i = 0; i < fl->ifl_size; i++) { 2118e035717eSSean Bruno struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i]; 2119e035717eSSean Bruno caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i]; 21204c7070dbSScott Long 2121fbec776dSAndrew Gallatin if (*sd_cl != NULL) { 21228a04b53dSKonstantin Belousov sd_map = fl->ifl_sds.ifsd_map[i]; 2123bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map, 21248a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 2125bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, sd_map); 2126fbec776dSAndrew Gallatin uma_zfree(fl->ifl_zone, *sd_cl); 2127b256d25cSMark Johnston *sd_cl = NULL; 2128e035717eSSean Bruno if (*sd_m != NULL) { 2129e035717eSSean Bruno m_init(*sd_m, M_NOWAIT, MT_DATA, 0); 2130e035717eSSean Bruno uma_zfree(zone_mbuf, *sd_m); 2131b256d25cSMark Johnston *sd_m = NULL; 2132e035717eSSean Bruno } 21334c7070dbSScott Long } else { 2134e035717eSSean Bruno MPASS(*sd_m == NULL); 21354c7070dbSScott Long } 21364c7070dbSScott Long #if MEMORY_LOGGING 21374c7070dbSScott Long fl->ifl_m_dequeued++; 21384c7070dbSScott Long fl->ifl_cl_dequeued++; 21394c7070dbSScott Long #endif 21404c7070dbSScott Long } 214195246abbSSean Bruno #ifdef INVARIANTS 214295246abbSSean Bruno for (i = 0; i < fl->ifl_size; i++) { 214395246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_cl[i] == NULL); 214495246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_m[i] == NULL); 214595246abbSSean Bruno } 214695246abbSSean Bruno #endif 21474c7070dbSScott Long /* 21484c7070dbSScott Long * Reset free list values 21494c7070dbSScott Long */ 215087890dbaSSean Bruno fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0; 21514c7070dbSScott Long bzero(idi->idi_vaddr, idi->idi_size); 21524c7070dbSScott Long } 21534c7070dbSScott Long 21544c7070dbSScott Long /********************************************************************* 21554c7070dbSScott Long * 21561722eeacSMarius Strobl * Initialize a free list and its buffers. 21574c7070dbSScott Long * 21584c7070dbSScott Long **********************************************************************/ 21594c7070dbSScott Long static int 21604c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl) 21614c7070dbSScott Long { 21624c7070dbSScott Long iflib_rxq_t rxq = fl->ifl_rxq; 21634c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 2164b3813609SPatrick Kelsey if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 2165b3813609SPatrick Kelsey int qidx; 21664c7070dbSScott Long 21677274b2f6SStephen Hurd bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1); 21684c7070dbSScott Long /* 21694c7070dbSScott Long ** Free current RX buffer structs and their mbufs 21704c7070dbSScott Long */ 21714c7070dbSScott Long iflib_fl_bufs_free(fl); 21724c7070dbSScott Long /* Now replenish the mbufs */ 21734c7070dbSScott Long MPASS(fl->ifl_credits == 0); 2174b3813609SPatrick Kelsey qidx = rxq->ifr_fl_offset + fl->ifl_id; 2175b3813609SPatrick Kelsey if (scctx->isc_rxd_buf_size[qidx] != 0) 2176b3813609SPatrick Kelsey fl->ifl_buf_size = scctx->isc_rxd_buf_size[qidx]; 2177b3813609SPatrick Kelsey else 21781b9d9394SEric Joyner fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz; 2179b3813609SPatrick Kelsey /* 2180b3813609SPatrick Kelsey * ifl_buf_size may be a driver-supplied value, so pull it up 2181b3813609SPatrick Kelsey * to the selected mbuf size. 2182b3813609SPatrick Kelsey */ 2183b3813609SPatrick Kelsey fl->ifl_buf_size = iflib_get_mbuf_size_for(fl->ifl_buf_size); 21844c7070dbSScott Long if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size) 21854c7070dbSScott Long ctx->ifc_max_fl_buf_size = fl->ifl_buf_size; 21864c7070dbSScott Long fl->ifl_cltype = m_gettype(fl->ifl_buf_size); 21874c7070dbSScott Long fl->ifl_zone = m_getzone(fl->ifl_buf_size); 21884c7070dbSScott Long 21894c7070dbSScott Long 21904c7070dbSScott Long /* avoid pre-allocating zillions of clusters to an idle card 21914c7070dbSScott Long * potentially speeding up attach 21924c7070dbSScott Long */ 2193b256d25cSMark Johnston (void)iflib_fl_refill(ctx, fl, min(128, fl->ifl_size)); 21944c7070dbSScott Long MPASS(min(128, fl->ifl_size) == fl->ifl_credits); 21954c7070dbSScott Long if (min(128, fl->ifl_size) != fl->ifl_credits) 21964c7070dbSScott Long return (ENOBUFS); 21974c7070dbSScott Long /* 21984c7070dbSScott Long * handle failure 21994c7070dbSScott Long */ 22004c7070dbSScott Long MPASS(rxq != NULL); 22014c7070dbSScott Long MPASS(fl->ifl_ifdi != NULL); 22024c7070dbSScott Long bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 22034c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 22044c7070dbSScott Long return (0); 22054c7070dbSScott Long } 22064c7070dbSScott Long 22074c7070dbSScott Long /********************************************************************* 22084c7070dbSScott Long * 22094c7070dbSScott Long * Free receive ring data structures 22104c7070dbSScott Long * 22114c7070dbSScott Long **********************************************************************/ 22124c7070dbSScott Long static void 22134c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq) 22144c7070dbSScott Long { 22154c7070dbSScott Long iflib_fl_t fl; 22168a04b53dSKonstantin Belousov int i, j; 22174c7070dbSScott Long 22184c7070dbSScott Long if (rxq->ifr_fl != NULL) { 22194c7070dbSScott Long for (i = 0; i < rxq->ifr_nfl; i++) { 22204c7070dbSScott Long fl = &rxq->ifr_fl[i]; 2221bfce461eSMarius Strobl if (fl->ifl_buf_tag != NULL) { 22228a04b53dSKonstantin Belousov if (fl->ifl_sds.ifsd_map != NULL) { 222377102fd6SAndrew Gallatin for (j = 0; j < fl->ifl_size; j++) { 22248a04b53dSKonstantin Belousov bus_dmamap_sync( 2225bfce461eSMarius Strobl fl->ifl_buf_tag, 222677102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j], 22278a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 22288a04b53dSKonstantin Belousov bus_dmamap_unload( 2229bfce461eSMarius Strobl fl->ifl_buf_tag, 223077102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j]); 2231db8e8f1eSEric Joyner bus_dmamap_destroy( 2232db8e8f1eSEric Joyner fl->ifl_buf_tag, 2233db8e8f1eSEric Joyner fl->ifl_sds.ifsd_map[j]); 22348a04b53dSKonstantin Belousov } 22358a04b53dSKonstantin Belousov } 2236bfce461eSMarius Strobl bus_dma_tag_destroy(fl->ifl_buf_tag); 2237bfce461eSMarius Strobl fl->ifl_buf_tag = NULL; 22384c7070dbSScott Long } 2239e035717eSSean Bruno free(fl->ifl_sds.ifsd_m, M_IFLIB); 2240e035717eSSean Bruno free(fl->ifl_sds.ifsd_cl, M_IFLIB); 2241fbec776dSAndrew Gallatin free(fl->ifl_sds.ifsd_ba, M_IFLIB); 2242e035717eSSean Bruno free(fl->ifl_sds.ifsd_map, M_IFLIB); 2243e035717eSSean Bruno fl->ifl_sds.ifsd_m = NULL; 2244e035717eSSean Bruno fl->ifl_sds.ifsd_cl = NULL; 2245fbec776dSAndrew Gallatin fl->ifl_sds.ifsd_ba = NULL; 2246e035717eSSean Bruno fl->ifl_sds.ifsd_map = NULL; 22474c7070dbSScott Long } 22484c7070dbSScott Long free(rxq->ifr_fl, M_IFLIB); 22494c7070dbSScott Long rxq->ifr_fl = NULL; 2250244e7cffSEric Joyner free(rxq->ifr_ifdi, M_IFLIB); 2251244e7cffSEric Joyner rxq->ifr_ifdi = NULL; 22521722eeacSMarius Strobl rxq->ifr_cq_cidx = 0; 22534c7070dbSScott Long } 22544c7070dbSScott Long } 22554c7070dbSScott Long 22564c7070dbSScott Long /* 22571722eeacSMarius Strobl * Timer routine 22584c7070dbSScott Long */ 22594c7070dbSScott Long static void 22604c7070dbSScott Long iflib_timer(void *arg) 22614c7070dbSScott Long { 2262ab2e3f79SStephen Hurd iflib_txq_t txq = arg; 22634c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 2264ab2e3f79SStephen Hurd if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 2265dd7fbcf1SStephen Hurd uint64_t this_tick = ticks; 2266dd7fbcf1SStephen Hurd uint32_t reset_on = hz / 2; 22674c7070dbSScott Long 22684c7070dbSScott Long if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) 22694c7070dbSScott Long return; 22701722eeacSMarius Strobl 22714c7070dbSScott Long /* 22724c7070dbSScott Long ** Check on the state of the TX queue(s), this 22734c7070dbSScott Long ** can be done without the lock because its RO 22744c7070dbSScott Long ** and the HUNG state will be static if set. 22754c7070dbSScott Long */ 2276dd7fbcf1SStephen Hurd if (this_tick - txq->ift_last_timer_tick >= hz / 2) { 2277dd7fbcf1SStephen Hurd txq->ift_last_timer_tick = this_tick; 2278ab2e3f79SStephen Hurd IFDI_TIMER(ctx, txq->ift_id); 2279ab2e3f79SStephen Hurd if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) && 2280ab2e3f79SStephen Hurd ((txq->ift_cleaned_prev == txq->ift_cleaned) || 2281ab2e3f79SStephen Hurd (sctx->isc_pause_frames == 0))) 2282ab2e3f79SStephen Hurd goto hung; 2283a9693502SSean Bruno 2284f6afed72SEric Joyner if (txq->ift_qstatus != IFLIB_QUEUE_IDLE && 2285f6afed72SEric Joyner ifmp_ring_is_stalled(txq->ift_br)) { 2286f6afed72SEric Joyner KASSERT(ctx->ifc_link_state == LINK_STATE_UP, ("queue can't be marked as hung if interface is down")); 2287ab2e3f79SStephen Hurd txq->ift_qstatus = IFLIB_QUEUE_HUNG; 2288f6afed72SEric Joyner } 2289ab2e3f79SStephen Hurd txq->ift_cleaned_prev = txq->ift_cleaned; 2290dd7fbcf1SStephen Hurd } 2291dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP 2292dd7fbcf1SStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) 229395dcf343SMarius Strobl iflib_netmap_timer_adjust(ctx, txq, &reset_on); 2294dd7fbcf1SStephen Hurd #endif 2295ab2e3f79SStephen Hurd /* handle any laggards */ 2296ab2e3f79SStephen Hurd if (txq->ift_db_pending) 2297ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 2298a9693502SSean Bruno 2299ab2e3f79SStephen Hurd sctx->isc_pause_frames = 0; 2300d300df01SStephen Hurd if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) 2301dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu); 2302ab2e3f79SStephen Hurd return; 23031722eeacSMarius Strobl 2304ab2e3f79SStephen Hurd hung: 23051722eeacSMarius Strobl device_printf(ctx->ifc_dev, 23061722eeacSMarius Strobl "Watchdog timeout (TX: %d desc avail: %d pidx: %d) -- resetting\n", 2307ab2e3f79SStephen Hurd txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx); 23087b610b60SSean Bruno STATE_LOCK(ctx); 23097b610b60SSean Bruno if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23107b610b60SSean Bruno ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET); 2311940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 231246fa0c25SEric Joyner STATE_UNLOCK(ctx); 23134c7070dbSScott Long } 23144c7070dbSScott Long 2315b3813609SPatrick Kelsey static uint16_t 2316b3813609SPatrick Kelsey iflib_get_mbuf_size_for(unsigned int size) 2317b3813609SPatrick Kelsey { 2318b3813609SPatrick Kelsey 2319b3813609SPatrick Kelsey if (size <= MCLBYTES) 2320b3813609SPatrick Kelsey return (MCLBYTES); 2321b3813609SPatrick Kelsey else 2322b3813609SPatrick Kelsey return (MJUMPAGESIZE); 2323b3813609SPatrick Kelsey } 2324b3813609SPatrick Kelsey 23254c7070dbSScott Long static void 23261b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(if_ctx_t ctx) 23271b9d9394SEric Joyner { 23281b9d9394SEric Joyner if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 23291b9d9394SEric Joyner 23301b9d9394SEric Joyner /* 23311b9d9394SEric Joyner * XXX don't set the max_frame_size to larger 23321b9d9394SEric Joyner * than the hardware can handle 23331b9d9394SEric Joyner */ 2334b3813609SPatrick Kelsey ctx->ifc_rx_mbuf_sz = 2335b3813609SPatrick Kelsey iflib_get_mbuf_size_for(sctx->isc_max_frame_size); 23361b9d9394SEric Joyner } 23371b9d9394SEric Joyner 23381b9d9394SEric Joyner uint32_t 23391b9d9394SEric Joyner iflib_get_rx_mbuf_sz(if_ctx_t ctx) 23401b9d9394SEric Joyner { 23411722eeacSMarius Strobl 23421b9d9394SEric Joyner return (ctx->ifc_rx_mbuf_sz); 23431b9d9394SEric Joyner } 23441b9d9394SEric Joyner 23451b9d9394SEric Joyner static void 23464c7070dbSScott Long iflib_init_locked(if_ctx_t ctx) 23474c7070dbSScott Long { 23484c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 23491248952aSSean Bruno if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 23504c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 23514c7070dbSScott Long iflib_fl_t fl; 23524c7070dbSScott Long iflib_txq_t txq; 23534c7070dbSScott Long iflib_rxq_t rxq; 2354ab2e3f79SStephen Hurd int i, j, tx_ip_csum_flags, tx_ip6_csum_flags; 23554c7070dbSScott Long 23564c7070dbSScott Long if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23574c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 23584c7070dbSScott Long 23591248952aSSean Bruno tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP); 23601248952aSSean Bruno tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP); 23614c7070dbSScott Long /* Set hardware offload abilities */ 23624c7070dbSScott Long if_clearhwassist(ifp); 23634c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM) 23641248952aSSean Bruno if_sethwassistbits(ifp, tx_ip_csum_flags, 0); 23654c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6) 23661248952aSSean Bruno if_sethwassistbits(ifp, tx_ip6_csum_flags, 0); 23674c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO4) 23684c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP_TSO, 0); 23694c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO6) 23704c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP6_TSO, 0); 23714c7070dbSScott Long 23724c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) { 23734c7070dbSScott Long CALLOUT_LOCK(txq); 23744c7070dbSScott Long callout_stop(&txq->ift_timer); 23754c7070dbSScott Long CALLOUT_UNLOCK(txq); 23764c7070dbSScott Long iflib_netmap_txq_init(ctx, txq); 23774c7070dbSScott Long } 23781b9d9394SEric Joyner 23791b9d9394SEric Joyner /* 23801b9d9394SEric Joyner * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so 23811b9d9394SEric Joyner * that drivers can use the value when setting up the hardware receive 23821b9d9394SEric Joyner * buffers. 23831b9d9394SEric Joyner */ 23841b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(ctx); 23851b9d9394SEric Joyner 238623ac9029SStephen Hurd #ifdef INVARIANTS 238723ac9029SStephen Hurd i = if_getdrvflags(ifp); 238823ac9029SStephen Hurd #endif 23894c7070dbSScott Long IFDI_INIT(ctx); 239023ac9029SStephen Hurd MPASS(if_getdrvflags(ifp) == i); 23914c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) { 2392d8b2d26bSVincenzo Maffione if (iflib_netmap_rxq_init(ctx, rxq) > 0) { 2393d8b2d26bSVincenzo Maffione /* This rxq is in netmap mode. Skip normal init. */ 239495246abbSSean Bruno continue; 2395d0d0ad0aSStephen Hurd } 23964c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 23974c7070dbSScott Long if (iflib_fl_setup(fl)) { 23983d10e9edSMarius Strobl device_printf(ctx->ifc_dev, 23993d10e9edSMarius Strobl "setting up free list %d failed - " 24003d10e9edSMarius Strobl "check cluster settings\n", j); 24014c7070dbSScott Long goto done; 24024c7070dbSScott Long } 24034c7070dbSScott Long } 24044c7070dbSScott Long } 24054c7070dbSScott Long done: 24064c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 24074c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 24084c7070dbSScott Long txq = ctx->ifc_txqs; 24094c7070dbSScott Long for (i = 0; i < sctx->isc_ntxqsets; i++, txq++) 2410ab2e3f79SStephen Hurd callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, 2411ab2e3f79SStephen Hurd txq->ift_timer.c_cpu); 24124c7070dbSScott Long } 24134c7070dbSScott Long 24144c7070dbSScott Long static int 24154c7070dbSScott Long iflib_media_change(if_t ifp) 24164c7070dbSScott Long { 24174c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 24184c7070dbSScott Long int err; 24194c7070dbSScott Long 24204c7070dbSScott Long CTX_LOCK(ctx); 24214c7070dbSScott Long if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0) 24224c7070dbSScott Long iflib_init_locked(ctx); 24234c7070dbSScott Long CTX_UNLOCK(ctx); 24244c7070dbSScott Long return (err); 24254c7070dbSScott Long } 24264c7070dbSScott Long 24274c7070dbSScott Long static void 24284c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr) 24294c7070dbSScott Long { 24304c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 24314c7070dbSScott Long 24324c7070dbSScott Long CTX_LOCK(ctx); 2433ab2e3f79SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 24344c7070dbSScott Long IFDI_MEDIA_STATUS(ctx, ifmr); 24354c7070dbSScott Long CTX_UNLOCK(ctx); 24364c7070dbSScott Long } 24374c7070dbSScott Long 243809f6ff4fSMatt Macy void 24394c7070dbSScott Long iflib_stop(if_ctx_t ctx) 24404c7070dbSScott Long { 24414c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 24424c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 24434c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 24444d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 24454c7070dbSScott Long iflib_dma_info_t di; 24464c7070dbSScott Long iflib_fl_t fl; 24474c7070dbSScott Long int i, j; 24484c7070dbSScott Long 24494c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 24504c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 24514c7070dbSScott Long 24524c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 2453ab2e3f79SStephen Hurd DELAY(1000); 2454da69b8f9SSean Bruno IFDI_STOP(ctx); 2455ab2e3f79SStephen Hurd DELAY(1000); 24564c7070dbSScott Long 2457da69b8f9SSean Bruno iflib_debug_reset(); 24584c7070dbSScott Long /* Wait for current tx queue users to exit to disarm watchdog timer. */ 24594c7070dbSScott Long for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) { 24604c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24614c7070dbSScott Long 2462226fb85dSStephen Hurd CALLOUT_LOCK(txq); 2463226fb85dSStephen Hurd callout_stop(&txq->ift_timer); 2464226fb85dSStephen Hurd CALLOUT_UNLOCK(txq); 2465226fb85dSStephen Hurd 24664c7070dbSScott Long /* clean any enqueued buffers */ 2467da69b8f9SSean Bruno iflib_ifmp_purge(txq); 24684c7070dbSScott Long /* Free any existing tx buffers. */ 246923ac9029SStephen Hurd for (j = 0; j < txq->ift_size; j++) { 24704c7070dbSScott Long iflib_txsd_free(ctx, txq, j); 24714c7070dbSScott Long } 2472ab2e3f79SStephen Hurd txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0; 2473ab2e3f79SStephen Hurd txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0; 24744c7070dbSScott Long txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0; 24754c7070dbSScott Long txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0; 2476ab2e3f79SStephen Hurd txq->ift_pullups = 0; 247795246abbSSean Bruno ifmp_ring_reset_stats(txq->ift_br); 24784d261ce2SStephen Hurd for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++) 24794c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24804c7070dbSScott Long } 24814c7070dbSScott Long for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) { 24824c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24834c7070dbSScott Long 24841722eeacSMarius Strobl rxq->ifr_cq_cidx = 0; 24854d261ce2SStephen Hurd for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++) 24864c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24874c7070dbSScott Long /* also resets the free lists pidx/cidx */ 24884c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 24894c7070dbSScott Long iflib_fl_bufs_free(fl); 24904c7070dbSScott Long } 24914c7070dbSScott Long } 24924c7070dbSScott Long 249395246abbSSean Bruno static inline caddr_t 249495246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx) 249595246abbSSean Bruno { 249695246abbSSean Bruno qidx_t size; 249795246abbSSean Bruno int nrxd; 249895246abbSSean Bruno caddr_t start, end, cur, next; 249995246abbSSean Bruno 250095246abbSSean Bruno nrxd = fl->ifl_size; 250195246abbSSean Bruno size = fl->ifl_rxd_size; 250295246abbSSean Bruno start = fl->ifl_ifdi->idi_vaddr; 250395246abbSSean Bruno 250495246abbSSean Bruno if (__predict_false(size == 0)) 250595246abbSSean Bruno return (start); 250695246abbSSean Bruno cur = start + size*cidx; 250795246abbSSean Bruno end = start + size*nrxd; 250895246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 250995246abbSSean Bruno return (next < end ? next : start); 251095246abbSSean Bruno } 251195246abbSSean Bruno 2512e035717eSSean Bruno static inline void 2513e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx) 2514e035717eSSean Bruno { 2515e035717eSSean Bruno int nextptr; 2516e035717eSSean Bruno int nrxd = fl->ifl_size; 251795246abbSSean Bruno caddr_t next_rxd; 251895246abbSSean Bruno 2519e035717eSSean Bruno 2520e035717eSSean Bruno nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1); 2521e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_m[nextptr]); 2522e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_cl[nextptr]); 252395246abbSSean Bruno next_rxd = calc_next_rxd(fl, cidx); 252495246abbSSean Bruno prefetch(next_rxd); 2525e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]); 2526e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]); 2527e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]); 2528e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]); 2529e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]); 2530e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]); 2531e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]); 2532e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]); 2533e035717eSSean Bruno } 2534e035717eSSean Bruno 25356d49b41eSAndrew Gallatin static struct mbuf * 25366d49b41eSAndrew Gallatin rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd, 25376d49b41eSAndrew Gallatin int *pf_rv, if_rxd_info_t ri) 25384c7070dbSScott Long { 2539e035717eSSean Bruno bus_dmamap_t map; 25404c7070dbSScott Long iflib_fl_t fl; 25416d49b41eSAndrew Gallatin caddr_t payload; 25426d49b41eSAndrew Gallatin struct mbuf *m; 25436d49b41eSAndrew Gallatin int flid, cidx, len, next; 25444c7070dbSScott Long 254595246abbSSean Bruno map = NULL; 25464c7070dbSScott Long flid = irf->irf_flid; 25474c7070dbSScott Long cidx = irf->irf_idx; 25484c7070dbSScott Long fl = &rxq->ifr_fl[flid]; 254995246abbSSean Bruno sd->ifsd_fl = fl; 25506d49b41eSAndrew Gallatin m = fl->ifl_sds.ifsd_m[cidx]; 255195246abbSSean Bruno sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx]; 25524c7070dbSScott Long fl->ifl_credits--; 25534c7070dbSScott Long #if MEMORY_LOGGING 25544c7070dbSScott Long fl->ifl_m_dequeued++; 25554c7070dbSScott Long #endif 255695246abbSSean Bruno if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH) 2557e035717eSSean Bruno prefetch_pkts(fl, cidx); 2558e035717eSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1); 2559e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_map[next]); 2560e035717eSSean Bruno map = fl->ifl_sds.ifsd_map[cidx]; 25614c7070dbSScott Long 2562bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD); 25636d49b41eSAndrew Gallatin 25644f2beb72SPatrick Kelsey if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL && 25654f2beb72SPatrick Kelsey irf->irf_len != 0) { 25666d49b41eSAndrew Gallatin payload = *sd->ifsd_cl; 25676d49b41eSAndrew Gallatin payload += ri->iri_pad; 25686d49b41eSAndrew Gallatin len = ri->iri_len - ri->iri_pad; 25696d49b41eSAndrew Gallatin *pf_rv = pfil_run_hooks(rxq->pfil, payload, ri->iri_ifp, 25706d49b41eSAndrew Gallatin len | PFIL_MEMPTR | PFIL_IN, NULL); 25716d49b41eSAndrew Gallatin switch (*pf_rv) { 25726d49b41eSAndrew Gallatin case PFIL_DROPPED: 25736d49b41eSAndrew Gallatin case PFIL_CONSUMED: 25746d49b41eSAndrew Gallatin /* 25756d49b41eSAndrew Gallatin * The filter ate it. Everything is recycled. 25766d49b41eSAndrew Gallatin */ 25776d49b41eSAndrew Gallatin m = NULL; 25786d49b41eSAndrew Gallatin unload = 0; 25796d49b41eSAndrew Gallatin break; 25806d49b41eSAndrew Gallatin case PFIL_REALLOCED: 25816d49b41eSAndrew Gallatin /* 25826d49b41eSAndrew Gallatin * The filter copied it. Everything is recycled. 25836d49b41eSAndrew Gallatin */ 25846d49b41eSAndrew Gallatin m = pfil_mem2mbuf(payload); 25856d49b41eSAndrew Gallatin unload = 0; 25866d49b41eSAndrew Gallatin break; 25876d49b41eSAndrew Gallatin case PFIL_PASS: 25886d49b41eSAndrew Gallatin /* 25896d49b41eSAndrew Gallatin * Filter said it was OK, so receive like 25906d49b41eSAndrew Gallatin * normal 25916d49b41eSAndrew Gallatin */ 25926d49b41eSAndrew Gallatin fl->ifl_sds.ifsd_m[cidx] = NULL; 25936d49b41eSAndrew Gallatin break; 25946d49b41eSAndrew Gallatin default: 25956d49b41eSAndrew Gallatin MPASS(0); 25966d49b41eSAndrew Gallatin } 25976d49b41eSAndrew Gallatin } else { 25986d49b41eSAndrew Gallatin fl->ifl_sds.ifsd_m[cidx] = NULL; 25996d49b41eSAndrew Gallatin *pf_rv = PFIL_PASS; 26006d49b41eSAndrew Gallatin } 26016d49b41eSAndrew Gallatin 26024f2beb72SPatrick Kelsey if (unload && irf->irf_len != 0) 2603bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, map); 260495246abbSSean Bruno fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1); 260595246abbSSean Bruno if (__predict_false(fl->ifl_cidx == 0)) 26064c7070dbSScott Long fl->ifl_gen = 0; 260787890dbaSSean Bruno bit_clear(fl->ifl_rx_bitmap, cidx); 26086d49b41eSAndrew Gallatin return (m); 26094c7070dbSScott Long } 26104c7070dbSScott Long 26114c7070dbSScott Long static struct mbuf * 26126d49b41eSAndrew Gallatin assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv) 26134c7070dbSScott Long { 261495246abbSSean Bruno struct mbuf *m, *mh, *mt; 261595246abbSSean Bruno caddr_t cl; 26166d49b41eSAndrew Gallatin int *pf_rv_ptr, flags, i, padlen; 26176d49b41eSAndrew Gallatin bool consumed; 26184c7070dbSScott Long 26194c7070dbSScott Long i = 0; 262023ac9029SStephen Hurd mh = NULL; 26216d49b41eSAndrew Gallatin consumed = false; 26226d49b41eSAndrew Gallatin *pf_rv = PFIL_PASS; 26236d49b41eSAndrew Gallatin pf_rv_ptr = pf_rv; 26244c7070dbSScott Long do { 26256d49b41eSAndrew Gallatin m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd, 26266d49b41eSAndrew Gallatin pf_rv_ptr, ri); 26274c7070dbSScott Long 262895246abbSSean Bruno MPASS(*sd->ifsd_cl != NULL); 262923ac9029SStephen Hurd 26306d49b41eSAndrew Gallatin /* 26316d49b41eSAndrew Gallatin * Exclude zero-length frags & frags from 26326d49b41eSAndrew Gallatin * packets the filter has consumed or dropped 26336d49b41eSAndrew Gallatin */ 26346d49b41eSAndrew Gallatin if (ri->iri_frags[i].irf_len == 0 || consumed || 26356d49b41eSAndrew Gallatin *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) { 26366d49b41eSAndrew Gallatin if (mh == NULL) { 26376d49b41eSAndrew Gallatin /* everything saved here */ 26386d49b41eSAndrew Gallatin consumed = true; 26396d49b41eSAndrew Gallatin pf_rv_ptr = NULL; 264023ac9029SStephen Hurd continue; 264123ac9029SStephen Hurd } 26426d49b41eSAndrew Gallatin /* XXX we can save the cluster here, but not the mbuf */ 26436d49b41eSAndrew Gallatin m_init(m, M_NOWAIT, MT_DATA, 0); 26446d49b41eSAndrew Gallatin m_free(m); 26456d49b41eSAndrew Gallatin continue; 26466d49b41eSAndrew Gallatin } 264723ac9029SStephen Hurd if (mh == NULL) { 26484c7070dbSScott Long flags = M_PKTHDR|M_EXT; 26494c7070dbSScott Long mh = mt = m; 26504c7070dbSScott Long padlen = ri->iri_pad; 26514c7070dbSScott Long } else { 26524c7070dbSScott Long flags = M_EXT; 26534c7070dbSScott Long mt->m_next = m; 26544c7070dbSScott Long mt = m; 26554c7070dbSScott Long /* assuming padding is only on the first fragment */ 26564c7070dbSScott Long padlen = 0; 26574c7070dbSScott Long } 265895246abbSSean Bruno cl = *sd->ifsd_cl; 265995246abbSSean Bruno *sd->ifsd_cl = NULL; 26604c7070dbSScott Long 26614c7070dbSScott Long /* Can these two be made one ? */ 26624c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, flags); 266395246abbSSean Bruno m_cljset(m, cl, sd->ifsd_fl->ifl_cltype); 26644c7070dbSScott Long /* 26654c7070dbSScott Long * These must follow m_init and m_cljset 26664c7070dbSScott Long */ 26674c7070dbSScott Long m->m_data += padlen; 26684c7070dbSScott Long ri->iri_len -= padlen; 266923ac9029SStephen Hurd m->m_len = ri->iri_frags[i].irf_len; 26704c7070dbSScott Long } while (++i < ri->iri_nfrags); 26714c7070dbSScott Long 26724c7070dbSScott Long return (mh); 26734c7070dbSScott Long } 26744c7070dbSScott Long 26754c7070dbSScott Long /* 26764c7070dbSScott Long * Process one software descriptor 26774c7070dbSScott Long */ 26784c7070dbSScott Long static struct mbuf * 26794c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri) 26804c7070dbSScott Long { 268195246abbSSean Bruno struct if_rxsd sd; 26824c7070dbSScott Long struct mbuf *m; 26836d49b41eSAndrew Gallatin int pf_rv; 26844c7070dbSScott Long 26854c7070dbSScott Long /* should I merge this back in now that the two paths are basically duplicated? */ 268623ac9029SStephen Hurd if (ri->iri_nfrags == 1 && 26874f2beb72SPatrick Kelsey ri->iri_frags[0].irf_len != 0 && 268818628b74SMark Johnston ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) { 26896d49b41eSAndrew Gallatin m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd, 26906d49b41eSAndrew Gallatin &pf_rv, ri); 26916d49b41eSAndrew Gallatin if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED) 26926d49b41eSAndrew Gallatin return (m); 26936d49b41eSAndrew Gallatin if (pf_rv == PFIL_PASS) { 26944c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR); 269595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 269695246abbSSean Bruno if (!IP_ALIGNED(m)) 269795246abbSSean Bruno m->m_data += 2; 269895246abbSSean Bruno #endif 269995246abbSSean Bruno memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len); 270023ac9029SStephen Hurd m->m_len = ri->iri_frags[0].irf_len; 27016d49b41eSAndrew Gallatin } 27024c7070dbSScott Long } else { 27036d49b41eSAndrew Gallatin m = assemble_segments(rxq, ri, &sd, &pf_rv); 27044f2beb72SPatrick Kelsey if (m == NULL) 27054f2beb72SPatrick Kelsey return (NULL); 27066d49b41eSAndrew Gallatin if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED) 27076d49b41eSAndrew Gallatin return (m); 27084c7070dbSScott Long } 27094c7070dbSScott Long m->m_pkthdr.len = ri->iri_len; 27104c7070dbSScott Long m->m_pkthdr.rcvif = ri->iri_ifp; 27114c7070dbSScott Long m->m_flags |= ri->iri_flags; 27124c7070dbSScott Long m->m_pkthdr.ether_vtag = ri->iri_vtag; 27134c7070dbSScott Long m->m_pkthdr.flowid = ri->iri_flowid; 27144c7070dbSScott Long M_HASHTYPE_SET(m, ri->iri_rsstype); 27154c7070dbSScott Long m->m_pkthdr.csum_flags = ri->iri_csum_flags; 27164c7070dbSScott Long m->m_pkthdr.csum_data = ri->iri_csum_data; 27174c7070dbSScott Long return (m); 27184c7070dbSScott Long } 27194c7070dbSScott Long 272035e4e998SStephen Hurd #if defined(INET6) || defined(INET) 2721fe1bcadaSStephen Hurd static void 2722fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6) 2723fe1bcadaSStephen Hurd { 2724fe1bcadaSStephen Hurd CURVNET_SET(lc->ifp->if_vnet); 2725fe1bcadaSStephen Hurd #if defined(INET6) 2726188adcb7SMarko Zec *v6 = V_ip6_forwarding; 2727fe1bcadaSStephen Hurd #endif 2728fe1bcadaSStephen Hurd #if defined(INET) 2729188adcb7SMarko Zec *v4 = V_ipforwarding; 2730fe1bcadaSStephen Hurd #endif 2731fe1bcadaSStephen Hurd CURVNET_RESTORE(); 2732fe1bcadaSStephen Hurd } 2733fe1bcadaSStephen Hurd 273435e4e998SStephen Hurd /* 273535e4e998SStephen Hurd * Returns true if it's possible this packet could be LROed. 273635e4e998SStephen Hurd * if it returns false, it is guaranteed that tcp_lro_rx() 273735e4e998SStephen Hurd * would not return zero. 273835e4e998SStephen Hurd */ 273935e4e998SStephen Hurd static bool 2740fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding) 274135e4e998SStephen Hurd { 274235e4e998SStephen Hurd struct ether_header *eh; 274335e4e998SStephen Hurd 274435e4e998SStephen Hurd eh = mtod(m, struct ether_header *); 27456aee0bfaSMarko Zec switch (eh->ether_type) { 2746abec4724SSean Bruno #if defined(INET6) 27476aee0bfaSMarko Zec case htons(ETHERTYPE_IPV6): 27486aee0bfaSMarko Zec return (!v6_forwarding); 2749abec4724SSean Bruno #endif 2750abec4724SSean Bruno #if defined (INET) 27516aee0bfaSMarko Zec case htons(ETHERTYPE_IP): 27526aee0bfaSMarko Zec return (!v4_forwarding); 2753abec4724SSean Bruno #endif 275435e4e998SStephen Hurd } 275535e4e998SStephen Hurd 275635e4e998SStephen Hurd return false; 275735e4e998SStephen Hurd } 2758fe1bcadaSStephen Hurd #else 2759fe1bcadaSStephen Hurd static void 2760fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused) 2761fe1bcadaSStephen Hurd { 2762fe1bcadaSStephen Hurd } 276335e4e998SStephen Hurd #endif 276435e4e998SStephen Hurd 2765fb1a29b4SHans Petter Selasky static void 2766fb1a29b4SHans Petter Selasky _task_fn_rx_watchdog(void *context) 2767fb1a29b4SHans Petter Selasky { 2768fb1a29b4SHans Petter Selasky iflib_rxq_t rxq = context; 2769fb1a29b4SHans Petter Selasky 2770fb1a29b4SHans Petter Selasky GROUPTASK_ENQUEUE(&rxq->ifr_task); 2771fb1a29b4SHans Petter Selasky } 2772fb1a29b4SHans Petter Selasky 2773fb1a29b4SHans Petter Selasky static uint8_t 277495246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget) 27754c7070dbSScott Long { 27761722eeacSMarius Strobl if_t ifp; 27774c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 27784c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 277923ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 27804c7070dbSScott Long int avail, i; 278195246abbSSean Bruno qidx_t *cidxp; 27824c7070dbSScott Long struct if_rxd_info ri; 27834c7070dbSScott Long int err, budget_left, rx_bytes, rx_pkts; 27844c7070dbSScott Long iflib_fl_t fl; 27854c7070dbSScott Long int lro_enabled; 2786f6cb0deaSMatt Macy bool v4_forwarding, v6_forwarding, lro_possible; 2787fb1a29b4SHans Petter Selasky uint8_t retval = 0; 278895246abbSSean Bruno 27894c7070dbSScott Long /* 27904c7070dbSScott Long * XXX early demux data packets so that if_input processing only handles 27914c7070dbSScott Long * acks in interrupt context 27924c7070dbSScott Long */ 279320f63282SStephen Hurd struct mbuf *m, *mh, *mt, *mf; 27944c7070dbSScott Long 27950b8df657SGleb Smirnoff NET_EPOCH_ASSERT(); 27960b8df657SGleb Smirnoff 2797f6cb0deaSMatt Macy lro_possible = v4_forwarding = v6_forwarding = false; 279895246abbSSean Bruno ifp = ctx->ifc_ifp; 27994c7070dbSScott Long mh = mt = NULL; 28004c7070dbSScott Long MPASS(budget > 0); 28014c7070dbSScott Long rx_pkts = rx_bytes = 0; 280223ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) 28034c7070dbSScott Long cidxp = &rxq->ifr_cq_cidx; 28044c7070dbSScott Long else 28054c7070dbSScott Long cidxp = &rxq->ifr_fl[0].ifl_cidx; 280623ac9029SStephen Hurd if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) { 28074c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 2808b256d25cSMark Johnston retval |= iflib_fl_refill_all(ctx, fl); 28094c7070dbSScott Long DBG_COUNTER_INC(rx_unavail); 2810fb1a29b4SHans Petter Selasky return (retval); 28114c7070dbSScott Long } 28124c7070dbSScott Long 28136d49b41eSAndrew Gallatin /* pfil needs the vnet to be set */ 28146d49b41eSAndrew Gallatin CURVNET_SET_QUIET(ifp->if_vnet); 28158b8d9093SMarius Strobl for (budget_left = budget; budget_left > 0 && avail > 0;) { 28164c7070dbSScott Long if (__predict_false(!CTX_ACTIVE(ctx))) { 28174c7070dbSScott Long DBG_COUNTER_INC(rx_ctx_inactive); 28184c7070dbSScott Long break; 28194c7070dbSScott Long } 28204c7070dbSScott Long /* 28214c7070dbSScott Long * Reset client set fields to their default values 28224c7070dbSScott Long */ 282395246abbSSean Bruno rxd_info_zero(&ri); 28244c7070dbSScott Long ri.iri_qsidx = rxq->ifr_id; 28254c7070dbSScott Long ri.iri_cidx = *cidxp; 282695246abbSSean Bruno ri.iri_ifp = ifp; 28274c7070dbSScott Long ri.iri_frags = rxq->ifr_frags; 28284c7070dbSScott Long err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 28294c7070dbSScott Long 283095246abbSSean Bruno if (err) 283195246abbSSean Bruno goto err; 28326d49b41eSAndrew Gallatin rx_pkts += 1; 28336d49b41eSAndrew Gallatin rx_bytes += ri.iri_len; 283423ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 283523ac9029SStephen Hurd *cidxp = ri.iri_cidx; 283623ac9029SStephen Hurd /* Update our consumer index */ 283795246abbSSean Bruno /* XXX NB: shurd - check if this is still safe */ 28381722eeacSMarius Strobl while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) 283923ac9029SStephen Hurd rxq->ifr_cq_cidx -= scctx->isc_nrxd[0]; 28404c7070dbSScott Long /* was this only a completion queue message? */ 28414c7070dbSScott Long if (__predict_false(ri.iri_nfrags == 0)) 28424c7070dbSScott Long continue; 28434c7070dbSScott Long } 28444c7070dbSScott Long MPASS(ri.iri_nfrags != 0); 28454c7070dbSScott Long MPASS(ri.iri_len != 0); 28464c7070dbSScott Long 28474c7070dbSScott Long /* will advance the cidx on the corresponding free lists */ 28484c7070dbSScott Long m = iflib_rxd_pkt_get(rxq, &ri); 28498b8d9093SMarius Strobl avail--; 28508b8d9093SMarius Strobl budget_left--; 28514c7070dbSScott Long if (avail == 0 && budget_left) 285223ac9029SStephen Hurd avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left); 28534c7070dbSScott Long 28546d49b41eSAndrew Gallatin if (__predict_false(m == NULL)) 28554c7070dbSScott Long continue; 28566d49b41eSAndrew Gallatin 28574c7070dbSScott Long /* imm_pkt: -- cxgb */ 28584c7070dbSScott Long if (mh == NULL) 28594c7070dbSScott Long mh = mt = m; 28604c7070dbSScott Long else { 28614c7070dbSScott Long mt->m_nextpkt = m; 28624c7070dbSScott Long mt = m; 28634c7070dbSScott Long } 28644c7070dbSScott Long } 28656d49b41eSAndrew Gallatin CURVNET_RESTORE(); 28664c7070dbSScott Long /* make sure that we can refill faster than drain */ 28674c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 2868b256d25cSMark Johnston retval |= iflib_fl_refill_all(ctx, fl); 28694c7070dbSScott Long 28704c7070dbSScott Long lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO); 2871fe1bcadaSStephen Hurd if (lro_enabled) 2872fe1bcadaSStephen Hurd iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding); 287320f63282SStephen Hurd mt = mf = NULL; 28744c7070dbSScott Long while (mh != NULL) { 28754c7070dbSScott Long m = mh; 28764c7070dbSScott Long mh = mh->m_nextpkt; 28774c7070dbSScott Long m->m_nextpkt = NULL; 287895246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 287995246abbSSean Bruno if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL) 288095246abbSSean Bruno continue; 288195246abbSSean Bruno #endif 28824c7070dbSScott Long rx_bytes += m->m_pkthdr.len; 28834c7070dbSScott Long rx_pkts++; 2884aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 288535e4e998SStephen Hurd if (lro_enabled) { 288635e4e998SStephen Hurd if (!lro_possible) { 2887fe1bcadaSStephen Hurd lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding); 288835e4e998SStephen Hurd if (lro_possible && mf != NULL) { 288935e4e998SStephen Hurd ifp->if_input(ifp, mf); 289035e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 289135e4e998SStephen Hurd mt = mf = NULL; 289235e4e998SStephen Hurd } 289335e4e998SStephen Hurd } 289425ac1dd5SStephen Hurd if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) == 289525ac1dd5SStephen Hurd (CSUM_L4_CALC|CSUM_L4_VALID)) { 289635e4e998SStephen Hurd if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0) 28974c7070dbSScott Long continue; 289820f63282SStephen Hurd } 289925ac1dd5SStephen Hurd } 2900aaeb188aSBjoern A. Zeeb #endif 290135e4e998SStephen Hurd if (lro_possible) { 290235e4e998SStephen Hurd ifp->if_input(ifp, m); 290335e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 290435e4e998SStephen Hurd continue; 290535e4e998SStephen Hurd } 290635e4e998SStephen Hurd 290735e4e998SStephen Hurd if (mf == NULL) 290835e4e998SStephen Hurd mf = m; 290920f63282SStephen Hurd if (mt != NULL) 291020f63282SStephen Hurd mt->m_nextpkt = m; 291120f63282SStephen Hurd mt = m; 291220f63282SStephen Hurd } 291320f63282SStephen Hurd if (mf != NULL) { 291420f63282SStephen Hurd ifp->if_input(ifp, mf); 29154c7070dbSScott Long DBG_COUNTER_INC(rx_if_input); 29164c7070dbSScott Long } 291723ac9029SStephen Hurd 29184c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes); 29194c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts); 29204c7070dbSScott Long 29214c7070dbSScott Long /* 29224c7070dbSScott Long * Flush any outstanding LRO work 29234c7070dbSScott Long */ 2924aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 292523ac9029SStephen Hurd tcp_lro_flush_all(&rxq->ifr_lc); 2926aaeb188aSBjoern A. Zeeb #endif 2927fb1a29b4SHans Petter Selasky if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0) 2928fb1a29b4SHans Petter Selasky retval |= IFLIB_RXEOF_MORE; 2929fb1a29b4SHans Petter Selasky return (retval); 293095246abbSSean Bruno err: 29317b610b60SSean Bruno STATE_LOCK(ctx); 2932ab2e3f79SStephen Hurd ctx->ifc_flags |= IFC_DO_RESET; 2933940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 293446fa0c25SEric Joyner STATE_UNLOCK(ctx); 2935fb1a29b4SHans Petter Selasky return (0); 293695246abbSSean Bruno } 293795246abbSSean Bruno 293895246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1) 293995246abbSSean Bruno static inline qidx_t 294095246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use) 294195246abbSSean Bruno { 294295246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 294395246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 294495246abbSSean Bruno if (in_use > 4*minthresh) 294595246abbSSean Bruno return (notify_count); 294695246abbSSean Bruno if (in_use > 2*minthresh) 294795246abbSSean Bruno return (notify_count >> 1); 294895246abbSSean Bruno if (in_use > minthresh) 294995246abbSSean Bruno return (notify_count >> 3); 295095246abbSSean Bruno return (0); 295195246abbSSean Bruno } 295295246abbSSean Bruno 295395246abbSSean Bruno static inline qidx_t 295495246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq) 295595246abbSSean Bruno { 295695246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 295795246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 295895246abbSSean Bruno if (txq->ift_in_use > 4*minthresh) 295995246abbSSean Bruno return (notify_count); 296095246abbSSean Bruno if (txq->ift_in_use > 2*minthresh) 296195246abbSSean Bruno return (notify_count >> 1); 296295246abbSSean Bruno if (txq->ift_in_use > minthresh) 296395246abbSSean Bruno return (notify_count >> 2); 29642b2fc973SSean Bruno return (2); 29654c7070dbSScott Long } 29664c7070dbSScott Long 29674c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags) 29684c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG) 296995246abbSSean Bruno 297095246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use)) 297195246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq) 297223ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4) 29734c7070dbSScott Long 297495246abbSSean Bruno /* forward compatibility for cxgb */ 297595246abbSSean Bruno #define FIRST_QSET(ctx) 0 297695246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets) 297795246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets) 297895246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx)) 297995246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments)) 298095246abbSSean Bruno 298195246abbSSean Bruno /* XXX we should be setting this to something other than zero */ 298295246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh) 29837474544bSMarius Strobl #define MAX_TX_DESC(ctx) max((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \ 29847474544bSMarius Strobl (ctx)->ifc_softc_ctx.isc_tx_nsegments) 298595246abbSSean Bruno 298695246abbSSean Bruno static inline bool 298795246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use) 29884c7070dbSScott Long { 298995246abbSSean Bruno qidx_t dbval, max; 299095246abbSSean Bruno bool rang; 29914c7070dbSScott Long 299295246abbSSean Bruno rang = false; 299395246abbSSean Bruno max = TXQ_MAX_DB_DEFERRED(txq, in_use); 299495246abbSSean Bruno if (ring || txq->ift_db_pending >= max) { 29954c7070dbSScott Long dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx; 299695dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 299795dcf343SMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 29984c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval); 29994c7070dbSScott Long txq->ift_db_pending = txq->ift_npending = 0; 300095246abbSSean Bruno rang = true; 30014c7070dbSScott Long } 300295246abbSSean Bruno return (rang); 30034c7070dbSScott Long } 30044c7070dbSScott Long 30054c7070dbSScott Long #ifdef PKT_DEBUG 30064c7070dbSScott Long static void 30074c7070dbSScott Long print_pkt(if_pkt_info_t pi) 30084c7070dbSScott Long { 30094c7070dbSScott Long printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n", 30104c7070dbSScott Long pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx); 30114c7070dbSScott Long printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n", 30124c7070dbSScott Long pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag); 30134c7070dbSScott Long printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n", 30144c7070dbSScott Long pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto); 30154c7070dbSScott Long } 30164c7070dbSScott Long #endif 30174c7070dbSScott Long 30184c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO) 3019a06424ddSEric Joyner #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO)) 30204c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO) 3021a06424ddSEric Joyner #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO)) 30224c7070dbSScott Long 30234c7070dbSScott Long static int 30244c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp) 30254c7070dbSScott Long { 3026ab2e3f79SStephen Hurd if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx; 30274c7070dbSScott Long struct ether_vlan_header *eh; 3028c9a49a4fSMarius Strobl struct mbuf *m; 30294c7070dbSScott Long 30308b8d9093SMarius Strobl m = *mp; 3031ab2e3f79SStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) && 3032ab2e3f79SStephen Hurd M_WRITABLE(m) == 0) { 3033ab2e3f79SStephen Hurd if ((m = m_dup(m, M_NOWAIT)) == NULL) { 3034ab2e3f79SStephen Hurd return (ENOMEM); 3035ab2e3f79SStephen Hurd } else { 3036ab2e3f79SStephen Hurd m_freem(*mp); 303764e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 30388b8d9093SMarius Strobl *mp = m; 3039ab2e3f79SStephen Hurd } 3040ab2e3f79SStephen Hurd } 30411248952aSSean Bruno 30424c7070dbSScott Long /* 30434c7070dbSScott Long * Determine where frame payload starts. 30444c7070dbSScott Long * Jump over vlan headers if already present, 30454c7070dbSScott Long * helpful for QinQ too. 30464c7070dbSScott Long */ 30474c7070dbSScott Long if (__predict_false(m->m_len < sizeof(*eh))) { 30484c7070dbSScott Long txq->ift_pullups++; 30494c7070dbSScott Long if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL)) 30504c7070dbSScott Long return (ENOMEM); 30514c7070dbSScott Long } 30524c7070dbSScott Long eh = mtod(m, struct ether_vlan_header *); 30534c7070dbSScott Long if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 30544c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_proto); 30554c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 30564c7070dbSScott Long } else { 30574c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_encap_proto); 30584c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN; 30594c7070dbSScott Long } 30604c7070dbSScott Long 30614c7070dbSScott Long switch (pi->ipi_etype) { 30624c7070dbSScott Long #ifdef INET 30634c7070dbSScott Long case ETHERTYPE_IP: 30644c7070dbSScott Long { 3065c9a49a4fSMarius Strobl struct mbuf *n; 30664c7070dbSScott Long struct ip *ip = NULL; 30674c7070dbSScott Long struct tcphdr *th = NULL; 30684c7070dbSScott Long int minthlen; 30694c7070dbSScott Long 30704c7070dbSScott Long minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th)); 30714c7070dbSScott Long if (__predict_false(m->m_len < minthlen)) { 30724c7070dbSScott Long /* 30734c7070dbSScott Long * if this code bloat is causing too much of a hit 30744c7070dbSScott Long * move it to a separate function and mark it noinline 30754c7070dbSScott Long */ 30764c7070dbSScott Long if (m->m_len == pi->ipi_ehdrlen) { 30774c7070dbSScott Long n = m->m_next; 30784c7070dbSScott Long MPASS(n); 30794c7070dbSScott Long if (n->m_len >= sizeof(*ip)) { 30804c7070dbSScott Long ip = (struct ip *)n->m_data; 30814c7070dbSScott Long if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30824c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30834c7070dbSScott Long } else { 30844c7070dbSScott Long txq->ift_pullups++; 30854c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 30864c7070dbSScott Long return (ENOMEM); 30874c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30884c7070dbSScott Long } 30894c7070dbSScott Long } else { 30904c7070dbSScott Long txq->ift_pullups++; 30914c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 30924c7070dbSScott Long return (ENOMEM); 30934c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30944c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30954c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30964c7070dbSScott Long } 30974c7070dbSScott Long } else { 30984c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30994c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 31004c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 31014c7070dbSScott Long } 31024c7070dbSScott Long pi->ipi_ip_hlen = ip->ip_hl << 2; 31034c7070dbSScott Long pi->ipi_ipproto = ip->ip_p; 31044c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV4; 31054c7070dbSScott Long 3106a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3107a06424ddSEric Joyner if (IS_TX_OFFLOAD4(pi)) { 3108a06424ddSEric Joyner if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) { 31094c7070dbSScott Long if (__predict_false(th == NULL)) { 31104c7070dbSScott Long txq->ift_pullups++; 31114c7070dbSScott Long if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL)) 31124c7070dbSScott Long return (ENOMEM); 31134c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen); 31144c7070dbSScott Long } 31154c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 31164c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 31174c7070dbSScott Long pi->ipi_tcp_seq = th->th_seq; 31184c7070dbSScott Long } 3119a06424ddSEric Joyner if (IS_TSO4(pi)) { 31204c7070dbSScott Long if (__predict_false(ip->ip_p != IPPROTO_TCP)) 31214c7070dbSScott Long return (ENXIO); 31228d4ceb9cSStephen Hurd /* 31238d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 31248d4ceb9cSStephen Hurd */ 31258d4ceb9cSStephen Hurd pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP); 31264c7070dbSScott Long th->th_sum = in_pseudo(ip->ip_src.s_addr, 31274c7070dbSScott Long ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 31284c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 31291248952aSSean Bruno if (sctx->isc_flags & IFLIB_TSO_INIT_IP) { 31301248952aSSean Bruno ip->ip_sum = 0; 31311248952aSSean Bruno ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz); 31321248952aSSean Bruno } 31334c7070dbSScott Long } 3134a06424ddSEric Joyner } 31358d4ceb9cSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP)) 31368d4ceb9cSStephen Hurd ip->ip_sum = 0; 31378d4ceb9cSStephen Hurd 31384c7070dbSScott Long break; 31394c7070dbSScott Long } 31404c7070dbSScott Long #endif 31414c7070dbSScott Long #ifdef INET6 31424c7070dbSScott Long case ETHERTYPE_IPV6: 31434c7070dbSScott Long { 31444c7070dbSScott Long struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen); 31454c7070dbSScott Long struct tcphdr *th; 31464c7070dbSScott Long pi->ipi_ip_hlen = sizeof(struct ip6_hdr); 31474c7070dbSScott Long 31484c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) { 314964e6fc13SStephen Hurd txq->ift_pullups++; 31504c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL)) 31514c7070dbSScott Long return (ENOMEM); 31524c7070dbSScott Long } 31534c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen); 31544c7070dbSScott Long 31554c7070dbSScott Long /* XXX-BZ this will go badly in case of ext hdrs. */ 31564c7070dbSScott Long pi->ipi_ipproto = ip6->ip6_nxt; 31574c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV6; 31584c7070dbSScott Long 3159a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3160a06424ddSEric Joyner if (IS_TX_OFFLOAD6(pi)) { 31614c7070dbSScott Long if (pi->ipi_ipproto == IPPROTO_TCP) { 31624c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) { 3163a06424ddSEric Joyner txq->ift_pullups++; 31644c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL)) 31654c7070dbSScott Long return (ENOMEM); 31664c7070dbSScott Long } 31674c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 31684c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 3169a06424ddSEric Joyner pi->ipi_tcp_seq = th->th_seq; 31704c7070dbSScott Long } 3171a06424ddSEric Joyner if (IS_TSO6(pi)) { 31724c7070dbSScott Long if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP)) 31734c7070dbSScott Long return (ENXIO); 31744c7070dbSScott Long /* 31758d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 31764c7070dbSScott Long */ 3177a06424ddSEric Joyner pi->ipi_csum_flags |= CSUM_IP6_TCP; 31784c7070dbSScott Long th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0); 31794c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 31804c7070dbSScott Long } 3181a06424ddSEric Joyner } 31824c7070dbSScott Long break; 31834c7070dbSScott Long } 31844c7070dbSScott Long #endif 31854c7070dbSScott Long default: 31864c7070dbSScott Long pi->ipi_csum_flags &= ~CSUM_OFFLOAD; 31874c7070dbSScott Long pi->ipi_ip_hlen = 0; 31884c7070dbSScott Long break; 31894c7070dbSScott Long } 31904c7070dbSScott Long *mp = m; 31911248952aSSean Bruno 31924c7070dbSScott Long return (0); 31934c7070dbSScott Long } 31944c7070dbSScott Long 31954c7070dbSScott Long /* 31964c7070dbSScott Long * If dodgy hardware rejects the scatter gather chain we've handed it 319723ac9029SStephen Hurd * we'll need to remove the mbuf chain from ifsg_m[] before we can add the 319823ac9029SStephen Hurd * m_defrag'd mbufs 31994c7070dbSScott Long */ 32004c7070dbSScott Long static __noinline struct mbuf * 320123ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq) 32024c7070dbSScott Long { 3203fbec776dSAndrew Gallatin int ntxd, pidx; 3204fbec776dSAndrew Gallatin struct mbuf *m, **ifsd_m; 32054c7070dbSScott Long 32064c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 320723ac9029SStephen Hurd ntxd = txq->ift_size; 3208fbec776dSAndrew Gallatin pidx = txq->ift_pidx & (ntxd - 1); 3209fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 3210fbec776dSAndrew Gallatin m = ifsd_m[pidx]; 32114c7070dbSScott Long ifsd_m[pidx] = NULL; 3212bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]); 32138a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) 3214bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 32158a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[pidx]); 32164c7070dbSScott Long #if MEMORY_LOGGING 32174c7070dbSScott Long txq->ift_dequeued++; 32184c7070dbSScott Long #endif 3219fbec776dSAndrew Gallatin return (m); 32204c7070dbSScott Long } 32214c7070dbSScott Long 322295246abbSSean Bruno static inline caddr_t 322395246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid) 322495246abbSSean Bruno { 322595246abbSSean Bruno qidx_t size; 322695246abbSSean Bruno int ntxd; 322795246abbSSean Bruno caddr_t start, end, cur, next; 322895246abbSSean Bruno 322995246abbSSean Bruno ntxd = txq->ift_size; 323095246abbSSean Bruno size = txq->ift_txd_size[qid]; 323195246abbSSean Bruno start = txq->ift_ifdi[qid].idi_vaddr; 323295246abbSSean Bruno 323395246abbSSean Bruno if (__predict_false(size == 0)) 323495246abbSSean Bruno return (start); 323595246abbSSean Bruno cur = start + size*cidx; 323695246abbSSean Bruno end = start + size*ntxd; 323795246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 323895246abbSSean Bruno return (next < end ? next : start); 323995246abbSSean Bruno } 324095246abbSSean Bruno 3241d14c853bSStephen Hurd /* 3242d14c853bSStephen Hurd * Pad an mbuf to ensure a minimum ethernet frame size. 3243d14c853bSStephen Hurd * min_frame_size is the frame size (less CRC) to pad the mbuf to 3244d14c853bSStephen Hurd */ 3245d14c853bSStephen Hurd static __noinline int 3246a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size) 3247d14c853bSStephen Hurd { 3248d14c853bSStephen Hurd /* 3249d14c853bSStephen Hurd * 18 is enough bytes to pad an ARP packet to 46 bytes, and 3250d14c853bSStephen Hurd * and ARP message is the smallest common payload I can think of 3251d14c853bSStephen Hurd */ 3252d14c853bSStephen Hurd static char pad[18]; /* just zeros */ 3253d14c853bSStephen Hurd int n; 3254a15fbbb8SStephen Hurd struct mbuf *new_head; 3255d14c853bSStephen Hurd 3256a15fbbb8SStephen Hurd if (!M_WRITABLE(*m_head)) { 3257a15fbbb8SStephen Hurd new_head = m_dup(*m_head, M_NOWAIT); 3258a15fbbb8SStephen Hurd if (new_head == NULL) { 325904993890SStephen Hurd m_freem(*m_head); 3260a15fbbb8SStephen Hurd device_printf(dev, "cannot pad short frame, m_dup() failed"); 326106c47d48SStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 326264e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3263a15fbbb8SStephen Hurd return ENOMEM; 3264a15fbbb8SStephen Hurd } 3265a15fbbb8SStephen Hurd m_freem(*m_head); 3266a15fbbb8SStephen Hurd *m_head = new_head; 3267a15fbbb8SStephen Hurd } 3268a15fbbb8SStephen Hurd 3269a15fbbb8SStephen Hurd for (n = min_frame_size - (*m_head)->m_pkthdr.len; 3270d14c853bSStephen Hurd n > 0; n -= sizeof(pad)) 3271a15fbbb8SStephen Hurd if (!m_append(*m_head, min(n, sizeof(pad)), pad)) 3272d14c853bSStephen Hurd break; 3273d14c853bSStephen Hurd 3274d14c853bSStephen Hurd if (n > 0) { 3275a15fbbb8SStephen Hurd m_freem(*m_head); 3276d14c853bSStephen Hurd device_printf(dev, "cannot pad short frame\n"); 3277d14c853bSStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 327864e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3279d14c853bSStephen Hurd return (ENOBUFS); 3280d14c853bSStephen Hurd } 3281d14c853bSStephen Hurd 3282d14c853bSStephen Hurd return 0; 3283d14c853bSStephen Hurd } 3284d14c853bSStephen Hurd 32854c7070dbSScott Long static int 32864c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp) 32874c7070dbSScott Long { 32884c7070dbSScott Long if_ctx_t ctx; 32894c7070dbSScott Long if_shared_ctx_t sctx; 32904c7070dbSScott Long if_softc_ctx_t scctx; 3291bfce461eSMarius Strobl bus_dma_tag_t buf_tag; 32924c7070dbSScott Long bus_dma_segment_t *segs; 3293fbec776dSAndrew Gallatin struct mbuf *m_head, **ifsd_m; 329495246abbSSean Bruno void *next_txd; 32954c7070dbSScott Long bus_dmamap_t map; 32964c7070dbSScott Long struct if_pkt_info pi; 32974c7070dbSScott Long int remap = 0; 32984c7070dbSScott Long int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd; 32994c7070dbSScott Long 33004c7070dbSScott Long ctx = txq->ift_ctx; 33014c7070dbSScott Long sctx = ctx->ifc_sctx; 33024c7070dbSScott Long scctx = &ctx->ifc_softc_ctx; 33034c7070dbSScott Long segs = txq->ift_segs; 330423ac9029SStephen Hurd ntxd = txq->ift_size; 33054c7070dbSScott Long m_head = *m_headp; 33064c7070dbSScott Long map = NULL; 33074c7070dbSScott Long 33084c7070dbSScott Long /* 33094c7070dbSScott Long * If we're doing TSO the next descriptor to clean may be quite far ahead 33104c7070dbSScott Long */ 33114c7070dbSScott Long cidx = txq->ift_cidx; 33124c7070dbSScott Long pidx = txq->ift_pidx; 331395246abbSSean Bruno if (ctx->ifc_flags & IFC_PREFETCH) { 33144c7070dbSScott Long next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1); 331595246abbSSean Bruno if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) { 331695246abbSSean Bruno next_txd = calc_next_txd(txq, cidx, 0); 331795246abbSSean Bruno prefetch(next_txd); 331895246abbSSean Bruno } 33194c7070dbSScott Long 33204c7070dbSScott Long /* prefetch the next cache line of mbuf pointers and flags */ 33214c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_m[next]); 33224c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_map[next]); 33234c7070dbSScott Long next = (cidx + CACHE_LINE_SIZE) & (ntxd-1); 33244c7070dbSScott Long } 332595246abbSSean Bruno map = txq->ift_sds.ifsd_map[pidx]; 3326fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 33274c7070dbSScott Long 33284c7070dbSScott Long if (m_head->m_pkthdr.csum_flags & CSUM_TSO) { 3329bfce461eSMarius Strobl buf_tag = txq->ift_tso_buf_tag; 33304c7070dbSScott Long max_segs = scctx->isc_tx_tso_segments_max; 33318a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[pidx]; 3332bfce461eSMarius Strobl MPASS(buf_tag != NULL); 33337f87c040SMarius Strobl MPASS(max_segs > 0); 33344c7070dbSScott Long } else { 3335bfce461eSMarius Strobl buf_tag = txq->ift_buf_tag; 33364c7070dbSScott Long max_segs = scctx->isc_tx_nsegments; 33378a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_map[pidx]; 33384c7070dbSScott Long } 3339d14c853bSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) && 3340d14c853bSStephen Hurd __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) { 3341a15fbbb8SStephen Hurd err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size); 334264e6fc13SStephen Hurd if (err) { 334364e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 3344d14c853bSStephen Hurd return err; 3345d14c853bSStephen Hurd } 334664e6fc13SStephen Hurd } 3347a15fbbb8SStephen Hurd m_head = *m_headp; 334895246abbSSean Bruno 334995246abbSSean Bruno pkt_info_zero(&pi); 3350ab2e3f79SStephen Hurd pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST)); 3351ab2e3f79SStephen Hurd pi.ipi_pidx = pidx; 3352ab2e3f79SStephen Hurd pi.ipi_qsidx = txq->ift_id; 33533429c02fSStephen Hurd pi.ipi_len = m_head->m_pkthdr.len; 33543429c02fSStephen Hurd pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags; 33551722eeacSMarius Strobl pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0; 33564c7070dbSScott Long 33574c7070dbSScott Long /* deliberate bitwise OR to make one condition */ 33584c7070dbSScott Long if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) { 335964e6fc13SStephen Hurd if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) { 336064e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 33614c7070dbSScott Long return (err); 336264e6fc13SStephen Hurd } 33634c7070dbSScott Long m_head = *m_headp; 33644c7070dbSScott Long } 33654c7070dbSScott Long 33664c7070dbSScott Long retry: 3367bfce461eSMarius Strobl err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs, 3368fbec776dSAndrew Gallatin BUS_DMA_NOWAIT); 33694c7070dbSScott Long defrag: 33704c7070dbSScott Long if (__predict_false(err)) { 33714c7070dbSScott Long switch (err) { 33724c7070dbSScott Long case EFBIG: 33734c7070dbSScott Long /* try collapse once and defrag once */ 3374f7594707SAndrew Gallatin if (remap == 0) { 33754c7070dbSScott Long m_head = m_collapse(*m_headp, M_NOWAIT, max_segs); 3376f7594707SAndrew Gallatin /* try defrag if collapsing fails */ 3377f7594707SAndrew Gallatin if (m_head == NULL) 3378f7594707SAndrew Gallatin remap++; 3379f7594707SAndrew Gallatin } 338064e6fc13SStephen Hurd if (remap == 1) { 338164e6fc13SStephen Hurd txq->ift_mbuf_defrag++; 33824c7070dbSScott Long m_head = m_defrag(*m_headp, M_NOWAIT); 338364e6fc13SStephen Hurd } 33843e8d1baeSEric Joyner /* 33853e8d1baeSEric Joyner * remap should never be >1 unless bus_dmamap_load_mbuf_sg 33863e8d1baeSEric Joyner * failed to map an mbuf that was run through m_defrag 33873e8d1baeSEric Joyner */ 33883e8d1baeSEric Joyner MPASS(remap <= 1); 33893e8d1baeSEric Joyner if (__predict_false(m_head == NULL || remap > 1)) 33904c7070dbSScott Long goto defrag_failed; 33913e8d1baeSEric Joyner remap++; 33924c7070dbSScott Long *m_headp = m_head; 33934c7070dbSScott Long goto retry; 33944c7070dbSScott Long break; 33954c7070dbSScott Long case ENOMEM: 33964c7070dbSScott Long txq->ift_no_tx_dma_setup++; 33974c7070dbSScott Long break; 33984c7070dbSScott Long default: 33994c7070dbSScott Long txq->ift_no_tx_dma_setup++; 34004c7070dbSScott Long m_freem(*m_headp); 34014c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 34024c7070dbSScott Long *m_headp = NULL; 34034c7070dbSScott Long break; 34044c7070dbSScott Long } 34054c7070dbSScott Long txq->ift_map_failed++; 34064c7070dbSScott Long DBG_COUNTER_INC(encap_load_mbuf_fail); 340764e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 34084c7070dbSScott Long return (err); 34094c7070dbSScott Long } 3410fbec776dSAndrew Gallatin ifsd_m[pidx] = m_head; 34114c7070dbSScott Long /* 34124c7070dbSScott Long * XXX assumes a 1 to 1 relationship between segments and 34134c7070dbSScott Long * descriptors - this does not hold true on all drivers, e.g. 34144c7070dbSScott Long * cxgb 34154c7070dbSScott Long */ 34164c7070dbSScott Long if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) { 34174c7070dbSScott Long txq->ift_no_desc_avail++; 3418bfce461eSMarius Strobl bus_dmamap_unload(buf_tag, map); 34194c7070dbSScott Long DBG_COUNTER_INC(encap_txq_avail_fail); 342064e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 342123ac9029SStephen Hurd if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) 34224c7070dbSScott Long GROUPTASK_ENQUEUE(&txq->ift_task); 34234c7070dbSScott Long return (ENOBUFS); 34244c7070dbSScott Long } 342595246abbSSean Bruno /* 342695246abbSSean Bruno * On Intel cards we can greatly reduce the number of TX interrupts 342795246abbSSean Bruno * we see by only setting report status on every Nth descriptor. 342895246abbSSean Bruno * However, this also means that the driver will need to keep track 342995246abbSSean Bruno * of the descriptors that RS was set on to check them for the DD bit. 343095246abbSSean Bruno */ 343195246abbSSean Bruno txq->ift_rs_pending += nsegs + 1; 343295246abbSSean Bruno if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) || 34331f7ce05dSAndrew Gallatin iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) { 343495246abbSSean Bruno pi.ipi_flags |= IPI_TX_INTR; 343595246abbSSean Bruno txq->ift_rs_pending = 0; 343695246abbSSean Bruno } 343795246abbSSean Bruno 34384c7070dbSScott Long pi.ipi_segs = segs; 34394c7070dbSScott Long pi.ipi_nsegs = nsegs; 34404c7070dbSScott Long 344123ac9029SStephen Hurd MPASS(pidx >= 0 && pidx < txq->ift_size); 34424c7070dbSScott Long #ifdef PKT_DEBUG 34434c7070dbSScott Long print_pkt(&pi); 34444c7070dbSScott Long #endif 34454c7070dbSScott Long if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) { 344695dcf343SMarius Strobl bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE); 34474c7070dbSScott Long DBG_COUNTER_INC(tx_encap); 344895246abbSSean Bruno MPASS(pi.ipi_new_pidx < txq->ift_size); 34494c7070dbSScott Long 34504c7070dbSScott Long ndesc = pi.ipi_new_pidx - pi.ipi_pidx; 34514c7070dbSScott Long if (pi.ipi_new_pidx < pi.ipi_pidx) { 345223ac9029SStephen Hurd ndesc += txq->ift_size; 34534c7070dbSScott Long txq->ift_gen = 1; 34544c7070dbSScott Long } 34551248952aSSean Bruno /* 34561248952aSSean Bruno * drivers can need as many as 34571248952aSSean Bruno * two sentinels 34581248952aSSean Bruno */ 34591248952aSSean Bruno MPASS(ndesc <= pi.ipi_nsegs + 2); 34604c7070dbSScott Long MPASS(pi.ipi_new_pidx != pidx); 34614c7070dbSScott Long MPASS(ndesc > 0); 34624c7070dbSScott Long txq->ift_in_use += ndesc; 346395246abbSSean Bruno 34644c7070dbSScott Long /* 34654c7070dbSScott Long * We update the last software descriptor again here because there may 34664c7070dbSScott Long * be a sentinel and/or there may be more mbufs than segments 34674c7070dbSScott Long */ 34684c7070dbSScott Long txq->ift_pidx = pi.ipi_new_pidx; 34694c7070dbSScott Long txq->ift_npending += pi.ipi_ndescs; 3470f7594707SAndrew Gallatin } else { 347123ac9029SStephen Hurd *m_headp = m_head = iflib_remove_mbuf(txq); 3472f7594707SAndrew Gallatin if (err == EFBIG) { 34734c7070dbSScott Long txq->ift_txd_encap_efbig++; 3474f7594707SAndrew Gallatin if (remap < 2) { 3475f7594707SAndrew Gallatin remap = 1; 34764c7070dbSScott Long goto defrag; 3477f7594707SAndrew Gallatin } 3478f7594707SAndrew Gallatin } 3479f7594707SAndrew Gallatin goto defrag_failed; 3480f7594707SAndrew Gallatin } 348164e6fc13SStephen Hurd /* 348264e6fc13SStephen Hurd * err can't possibly be non-zero here, so we don't neet to test it 348364e6fc13SStephen Hurd * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail). 348464e6fc13SStephen Hurd */ 34854c7070dbSScott Long return (err); 34864c7070dbSScott Long 34874c7070dbSScott Long defrag_failed: 34884c7070dbSScott Long txq->ift_mbuf_defrag_failed++; 34894c7070dbSScott Long txq->ift_map_failed++; 34904c7070dbSScott Long m_freem(*m_headp); 34914c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 34924c7070dbSScott Long *m_headp = NULL; 349364e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 34944c7070dbSScott Long return (ENOMEM); 34954c7070dbSScott Long } 34964c7070dbSScott Long 34974c7070dbSScott Long static void 34984c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n) 34994c7070dbSScott Long { 35004c7070dbSScott Long uint32_t qsize, cidx, mask, gen; 35014c7070dbSScott Long struct mbuf *m, **ifsd_m; 350295246abbSSean Bruno bool do_prefetch; 35034c7070dbSScott Long 35044c7070dbSScott Long cidx = txq->ift_cidx; 35054c7070dbSScott Long gen = txq->ift_gen; 350623ac9029SStephen Hurd qsize = txq->ift_size; 35074c7070dbSScott Long mask = qsize-1; 35084c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 350995246abbSSean Bruno do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH); 35104c7070dbSScott Long 351194618825SMark Johnston while (n-- > 0) { 351295246abbSSean Bruno if (do_prefetch) { 35134c7070dbSScott Long prefetch(ifsd_m[(cidx + 3) & mask]); 35144c7070dbSScott Long prefetch(ifsd_m[(cidx + 4) & mask]); 351595246abbSSean Bruno } 35164c7070dbSScott Long if ((m = ifsd_m[cidx]) != NULL) { 3517fbec776dSAndrew Gallatin prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]); 35188a04b53dSKonstantin Belousov if (m->m_pkthdr.csum_flags & CSUM_TSO) { 3519bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 35208a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx], 35218a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3522bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 35238a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx]); 35248a04b53dSKonstantin Belousov } else { 3525bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 35268a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx], 35278a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3528bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, 35298a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx]); 35308a04b53dSKonstantin Belousov } 35314c7070dbSScott Long /* XXX we don't support any drivers that batch packets yet */ 35324c7070dbSScott Long MPASS(m->m_nextpkt == NULL); 35335c5ca36cSSean Bruno m_freem(m); 35344c7070dbSScott Long ifsd_m[cidx] = NULL; 35354c7070dbSScott Long #if MEMORY_LOGGING 35364c7070dbSScott Long txq->ift_dequeued++; 35374c7070dbSScott Long #endif 35384c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 35394c7070dbSScott Long } 35404c7070dbSScott Long if (__predict_false(++cidx == qsize)) { 35414c7070dbSScott Long cidx = 0; 35424c7070dbSScott Long gen = 0; 35434c7070dbSScott Long } 35444c7070dbSScott Long } 35454c7070dbSScott Long txq->ift_cidx = cidx; 35464c7070dbSScott Long txq->ift_gen = gen; 35474c7070dbSScott Long } 35484c7070dbSScott Long 35494c7070dbSScott Long static __inline int 35504c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh) 35514c7070dbSScott Long { 35524c7070dbSScott Long int reclaim; 35534c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 35544c7070dbSScott Long 35554c7070dbSScott Long KASSERT(thresh >= 0, ("invalid threshold to reclaim")); 35564c7070dbSScott Long MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size); 35574c7070dbSScott Long 35584c7070dbSScott Long /* 35594c7070dbSScott Long * Need a rate-limiting check so that this isn't called every time 35604c7070dbSScott Long */ 35614c7070dbSScott Long iflib_tx_credits_update(ctx, txq); 35624c7070dbSScott Long reclaim = DESC_RECLAIMABLE(txq); 35634c7070dbSScott Long 35644c7070dbSScott Long if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) { 35654c7070dbSScott Long #ifdef INVARIANTS 35664c7070dbSScott Long if (iflib_verbose_debug) { 35674c7070dbSScott Long printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__, 35684c7070dbSScott Long txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments, 35694c7070dbSScott Long reclaim, thresh); 35704c7070dbSScott Long 35714c7070dbSScott Long } 35724c7070dbSScott Long #endif 35734c7070dbSScott Long return (0); 35744c7070dbSScott Long } 35754c7070dbSScott Long iflib_tx_desc_free(txq, reclaim); 35764c7070dbSScott Long txq->ift_cleaned += reclaim; 35774c7070dbSScott Long txq->ift_in_use -= reclaim; 35784c7070dbSScott Long 35794c7070dbSScott Long return (reclaim); 35804c7070dbSScott Long } 35814c7070dbSScott Long 35824c7070dbSScott Long static struct mbuf ** 358395246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining) 35844c7070dbSScott Long { 358595246abbSSean Bruno int next, size; 358695246abbSSean Bruno struct mbuf **items; 35874c7070dbSScott Long 358895246abbSSean Bruno size = r->size; 358995246abbSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (size-1); 359095246abbSSean Bruno items = __DEVOLATILE(struct mbuf **, &r->items[0]); 359195246abbSSean Bruno 359295246abbSSean Bruno prefetch(items[(cidx + offset) & (size-1)]); 359395246abbSSean Bruno if (remaining > 1) { 35943429c02fSStephen Hurd prefetch2cachelines(&items[next]); 35953429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]); 35963429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]); 35973429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]); 359895246abbSSean Bruno } 359995246abbSSean Bruno return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)])); 36004c7070dbSScott Long } 36014c7070dbSScott Long 36024c7070dbSScott Long static void 36034c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget) 36044c7070dbSScott Long { 36054c7070dbSScott Long 360695246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, budget); 36074c7070dbSScott Long } 36084c7070dbSScott Long 36094c7070dbSScott Long static uint32_t 36104c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r) 36114c7070dbSScott Long { 36124c7070dbSScott Long iflib_txq_t txq = r->cookie; 36134c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 36144c7070dbSScott Long 361595dcf343SMarius Strobl if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) 361695dcf343SMarius Strobl return (1); 36178a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 36188a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 361995dcf343SMarius Strobl return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, 362095dcf343SMarius Strobl false)); 36214c7070dbSScott Long } 36224c7070dbSScott Long 36234c7070dbSScott Long static uint32_t 36244c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 36254c7070dbSScott Long { 36264c7070dbSScott Long iflib_txq_t txq = r->cookie; 36274c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 36281722eeacSMarius Strobl if_t ifp = ctx->ifc_ifp; 3629c2c5d1e7SMarius Strobl struct mbuf *m, **mp; 3630c2c5d1e7SMarius Strobl int avail, bytes_sent, consumed, count, err, i, in_use_prev; 3631c2c5d1e7SMarius Strobl int mcast_sent, pkt_sent, reclaimed, txq_avail; 3632c2c5d1e7SMarius Strobl bool do_prefetch, rang, ring; 36334c7070dbSScott Long 36344c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) || 36354c7070dbSScott Long !LINK_ACTIVE(ctx))) { 36364c7070dbSScott Long DBG_COUNTER_INC(txq_drain_notready); 36374c7070dbSScott Long return (0); 36384c7070dbSScott Long } 363995246abbSSean Bruno reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 364095246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use); 36414c7070dbSScott Long avail = IDXDIFF(pidx, cidx, r->size); 36424c7070dbSScott Long if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) { 36434c7070dbSScott Long DBG_COUNTER_INC(txq_drain_flushing); 36444c7070dbSScott Long for (i = 0; i < avail; i++) { 3645bc0e855bSStephen Hurd if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq)) 364623ac9029SStephen Hurd m_free(r->items[(cidx + i) & (r->size-1)]); 36474c7070dbSScott Long r->items[(cidx + i) & (r->size-1)] = NULL; 36484c7070dbSScott Long } 36494c7070dbSScott Long return (avail); 36504c7070dbSScott Long } 365195246abbSSean Bruno 36524c7070dbSScott Long if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) { 36534c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 36544c7070dbSScott Long CALLOUT_LOCK(txq); 36554c7070dbSScott Long callout_stop(&txq->ift_timer); 36564c7070dbSScott Long CALLOUT_UNLOCK(txq); 36574c7070dbSScott Long DBG_COUNTER_INC(txq_drain_oactive); 36584c7070dbSScott Long return (0); 36594c7070dbSScott Long } 366095246abbSSean Bruno if (reclaimed) 366195246abbSSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 36624c7070dbSScott Long consumed = mcast_sent = bytes_sent = pkt_sent = 0; 36634c7070dbSScott Long count = MIN(avail, TX_BATCH_SIZE); 3664da69b8f9SSean Bruno #ifdef INVARIANTS 3665da69b8f9SSean Bruno if (iflib_verbose_debug) 3666da69b8f9SSean Bruno printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__, 3667da69b8f9SSean Bruno avail, ctx->ifc_flags, TXQ_AVAIL(txq)); 3668da69b8f9SSean Bruno #endif 366995246abbSSean Bruno do_prefetch = (ctx->ifc_flags & IFC_PREFETCH); 3670c2c5d1e7SMarius Strobl txq_avail = TXQ_AVAIL(txq); 36711ae4848cSMatt Macy err = 0; 3672c2c5d1e7SMarius Strobl for (i = 0; i < count && txq_avail > MAX_TX_DESC(ctx) + 2; i++) { 36731ae4848cSMatt Macy int rem = do_prefetch ? count - i : 0; 36744c7070dbSScott Long 367595246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, rem); 3676da69b8f9SSean Bruno MPASS(mp != NULL && *mp != NULL); 367795246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) { 367895246abbSSean Bruno consumed++; 367995246abbSSean Bruno continue; 368095246abbSSean Bruno } 36814c7070dbSScott Long in_use_prev = txq->ift_in_use; 368295246abbSSean Bruno err = iflib_encap(txq, mp); 368395246abbSSean Bruno if (__predict_false(err)) { 3684da69b8f9SSean Bruno /* no room - bail out */ 368595246abbSSean Bruno if (err == ENOBUFS) 36864c7070dbSScott Long break; 36874c7070dbSScott Long consumed++; 3688da69b8f9SSean Bruno /* we can't send this packet - skip it */ 36894c7070dbSScott Long continue; 3690da69b8f9SSean Bruno } 369195246abbSSean Bruno consumed++; 36924c7070dbSScott Long pkt_sent++; 36934c7070dbSScott Long m = *mp; 36944c7070dbSScott Long DBG_COUNTER_INC(tx_sent); 36954c7070dbSScott Long bytes_sent += m->m_pkthdr.len; 369695246abbSSean Bruno mcast_sent += !!(m->m_flags & M_MCAST); 3697c2c5d1e7SMarius Strobl txq_avail = TXQ_AVAIL(txq); 36984c7070dbSScott Long 36994c7070dbSScott Long txq->ift_db_pending += (txq->ift_in_use - in_use_prev); 37004c7070dbSScott Long ETHER_BPF_MTAP(ifp, m); 370195246abbSSean Bruno if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) 37024c7070dbSScott Long break; 370395246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, false, in_use_prev); 37044c7070dbSScott Long } 37054c7070dbSScott Long 370695246abbSSean Bruno /* deliberate use of bitwise or to avoid gratuitous short-circuit */ 370795246abbSSean Bruno ring = rang ? false : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx)); 370895246abbSSean Bruno iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use); 37094c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); 37104c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); 37114c7070dbSScott Long if (mcast_sent) 37124c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent); 3713da69b8f9SSean Bruno #ifdef INVARIANTS 3714da69b8f9SSean Bruno if (iflib_verbose_debug) 3715da69b8f9SSean Bruno printf("consumed=%d\n", consumed); 3716da69b8f9SSean Bruno #endif 37174c7070dbSScott Long return (consumed); 37184c7070dbSScott Long } 37194c7070dbSScott Long 3720da69b8f9SSean Bruno static uint32_t 3721da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r) 3722da69b8f9SSean Bruno { 3723da69b8f9SSean Bruno return (1); 3724da69b8f9SSean Bruno } 3725da69b8f9SSean Bruno 3726da69b8f9SSean Bruno static uint32_t 3727da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 3728da69b8f9SSean Bruno { 3729da69b8f9SSean Bruno int i, avail; 3730da69b8f9SSean Bruno struct mbuf **mp; 3731da69b8f9SSean Bruno iflib_txq_t txq; 3732da69b8f9SSean Bruno 3733da69b8f9SSean Bruno txq = r->cookie; 3734da69b8f9SSean Bruno 3735da69b8f9SSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3736da69b8f9SSean Bruno CALLOUT_LOCK(txq); 3737da69b8f9SSean Bruno callout_stop(&txq->ift_timer); 3738da69b8f9SSean Bruno CALLOUT_UNLOCK(txq); 3739da69b8f9SSean Bruno 3740da69b8f9SSean Bruno avail = IDXDIFF(pidx, cidx, r->size); 3741da69b8f9SSean Bruno for (i = 0; i < avail; i++) { 374295246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, avail - i); 374395246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) 374495246abbSSean Bruno continue; 3745da69b8f9SSean Bruno m_freem(*mp); 374664e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3747da69b8f9SSean Bruno } 3748da69b8f9SSean Bruno MPASS(ifmp_ring_is_stalled(r) == 0); 3749da69b8f9SSean Bruno return (avail); 3750da69b8f9SSean Bruno } 3751da69b8f9SSean Bruno 3752da69b8f9SSean Bruno static void 3753da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq) 3754da69b8f9SSean Bruno { 3755da69b8f9SSean Bruno struct ifmp_ring *r; 3756da69b8f9SSean Bruno 375795246abbSSean Bruno r = txq->ift_br; 3758da69b8f9SSean Bruno r->drain = iflib_txq_drain_free; 3759da69b8f9SSean Bruno r->can_drain = iflib_txq_drain_always; 3760da69b8f9SSean Bruno 3761da69b8f9SSean Bruno ifmp_ring_check_drainage(r, r->size); 3762da69b8f9SSean Bruno 3763da69b8f9SSean Bruno r->drain = iflib_txq_drain; 3764da69b8f9SSean Bruno r->can_drain = iflib_txq_can_drain; 3765da69b8f9SSean Bruno } 3766da69b8f9SSean Bruno 37674c7070dbSScott Long static void 376823ac9029SStephen Hurd _task_fn_tx(void *context) 37694c7070dbSScott Long { 37704c7070dbSScott Long iflib_txq_t txq = context; 37714c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 3772a6611c93SMarius Strobl if_t ifp = ctx->ifc_ifp; 3773fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 37744c7070dbSScott Long 37751248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 37761248952aSSean Bruno txq->ift_cpu_exec_count[curcpu]++; 37771248952aSSean Bruno #endif 377888a68866SVincenzo Maffione if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 37794c7070dbSScott Long return; 378095dcf343SMarius Strobl #ifdef DEV_NETMAP 378188a68866SVincenzo Maffione if ((if_getcapenable(ifp) & IFCAP_NETMAP) && 378288a68866SVincenzo Maffione netmap_tx_irq(ifp, txq->ift_id)) 378388a68866SVincenzo Maffione goto skip_ifmp; 378495dcf343SMarius Strobl #endif 3785b8ca4756SPatrick Kelsey #ifdef ALTQ 3786b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) 3787b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 3788b8ca4756SPatrick Kelsey #endif 378995246abbSSean Bruno if (txq->ift_db_pending) 3790fe51d4cdSStephen Hurd ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate); 3791fe51d4cdSStephen Hurd else if (!abdicate) 3792fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 3793fe51d4cdSStephen Hurd /* 3794fe51d4cdSStephen Hurd * When abdicating, we always need to check drainage, not just when we don't enqueue 3795fe51d4cdSStephen Hurd */ 3796fe51d4cdSStephen Hurd if (abdicate) 3797fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 379888a68866SVincenzo Maffione #ifdef DEV_NETMAP 379988a68866SVincenzo Maffione skip_ifmp: 380088a68866SVincenzo Maffione #endif 380195246abbSSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 380295246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 38033d10e9edSMarius Strobl else 38041ae4848cSMatt Macy IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 38054c7070dbSScott Long } 38064c7070dbSScott Long 38074c7070dbSScott Long static void 380823ac9029SStephen Hurd _task_fn_rx(void *context) 38094c7070dbSScott Long { 38104c7070dbSScott Long iflib_rxq_t rxq = context; 38114c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 3812fb1a29b4SHans Petter Selasky uint8_t more; 3813f4d2154eSStephen Hurd uint16_t budget; 3814e136e9c8SVincenzo Maffione #ifdef DEV_NETMAP 3815e136e9c8SVincenzo Maffione u_int work = 0; 3816e136e9c8SVincenzo Maffione int nmirq; 3817e136e9c8SVincenzo Maffione #endif 38184c7070dbSScott Long 38191248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 38201248952aSSean Bruno rxq->ifr_cpu_exec_count[curcpu]++; 38211248952aSSean Bruno #endif 38224c7070dbSScott Long DBG_COUNTER_INC(task_fn_rxs); 38234c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 38244c7070dbSScott Long return; 3825d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP 3826e136e9c8SVincenzo Maffione nmirq = netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work); 3827e136e9c8SVincenzo Maffione if (nmirq != NM_IRQ_PASS) { 3828e136e9c8SVincenzo Maffione more = (nmirq == NM_IRQ_RESCHED) ? IFLIB_RXEOF_MORE : 0; 3829fb1a29b4SHans Petter Selasky goto skip_rxeof; 3830d0d0ad0aSStephen Hurd } 3831d0d0ad0aSStephen Hurd #endif 3832f4d2154eSStephen Hurd budget = ctx->ifc_sysctl_rx_budget; 3833f4d2154eSStephen Hurd if (budget == 0) 3834f4d2154eSStephen Hurd budget = 16; /* XXX */ 3835fb1a29b4SHans Petter Selasky more = iflib_rxeof(rxq, budget); 3836fb1a29b4SHans Petter Selasky #ifdef DEV_NETMAP 3837fb1a29b4SHans Petter Selasky skip_rxeof: 3838fb1a29b4SHans Petter Selasky #endif 3839fb1a29b4SHans Petter Selasky if ((more & IFLIB_RXEOF_MORE) == 0) { 38404c7070dbSScott Long if (ctx->ifc_flags & IFC_LEGACY) 38414c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 38423d10e9edSMarius Strobl else 38431ae4848cSMatt Macy IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 38441ae4848cSMatt Macy DBG_COUNTER_INC(rx_intr_enables); 38454c7070dbSScott Long } 38464c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 38474c7070dbSScott Long return; 3848fb1a29b4SHans Petter Selasky 3849fb1a29b4SHans Petter Selasky if (more & IFLIB_RXEOF_MORE) 38504c7070dbSScott Long GROUPTASK_ENQUEUE(&rxq->ifr_task); 3851fb1a29b4SHans Petter Selasky else if (more & IFLIB_RXEOF_EMPTY) 3852fb1a29b4SHans Petter Selasky callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq); 38534c7070dbSScott Long } 38544c7070dbSScott Long 38554c7070dbSScott Long static void 385623ac9029SStephen Hurd _task_fn_admin(void *context) 38574c7070dbSScott Long { 38584c7070dbSScott Long if_ctx_t ctx = context; 38594c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 38604c7070dbSScott Long iflib_txq_t txq; 3861ab2e3f79SStephen Hurd int i; 386277c1fcecSEric Joyner bool oactive, running, do_reset, do_watchdog, in_detach; 3863dd7fbcf1SStephen Hurd uint32_t reset_on = hz / 2; 3864ab2e3f79SStephen Hurd 38657b610b60SSean Bruno STATE_LOCK(ctx); 38667b610b60SSean Bruno running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING); 38677b610b60SSean Bruno oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE); 38687b610b60SSean Bruno do_reset = (ctx->ifc_flags & IFC_DO_RESET); 38697b610b60SSean Bruno do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG); 387077c1fcecSEric Joyner in_detach = (ctx->ifc_flags & IFC_IN_DETACH); 38717b610b60SSean Bruno ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG); 38727b610b60SSean Bruno STATE_UNLOCK(ctx); 38737b610b60SSean Bruno 387477c1fcecSEric Joyner if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 387577c1fcecSEric Joyner return; 387677c1fcecSEric Joyner if (in_detach) 3877ab2e3f79SStephen Hurd return; 38784c7070dbSScott Long 38794c7070dbSScott Long CTX_LOCK(ctx); 38804c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 38814c7070dbSScott Long CALLOUT_LOCK(txq); 38824c7070dbSScott Long callout_stop(&txq->ift_timer); 38834c7070dbSScott Long CALLOUT_UNLOCK(txq); 38844c7070dbSScott Long } 38857b610b60SSean Bruno if (do_watchdog) { 38867b610b60SSean Bruno ctx->ifc_watchdog_events++; 38877b610b60SSean Bruno IFDI_WATCHDOG_RESET(ctx); 38887b610b60SSean Bruno } 3889d300df01SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 3890dd7fbcf1SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 3891dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP 3892dd7fbcf1SStephen Hurd reset_on = hz / 2; 3893dd7fbcf1SStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) 389495dcf343SMarius Strobl iflib_netmap_timer_adjust(ctx, txq, &reset_on); 3895dd7fbcf1SStephen Hurd #endif 3896dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu); 3897dd7fbcf1SStephen Hurd } 3898ab2e3f79SStephen Hurd IFDI_LINK_INTR_ENABLE(ctx); 38997b610b60SSean Bruno if (do_reset) 3900ab2e3f79SStephen Hurd iflib_if_init_locked(ctx); 39014c7070dbSScott Long CTX_UNLOCK(ctx); 39024c7070dbSScott Long 3903ab2e3f79SStephen Hurd if (LINK_ACTIVE(ctx) == 0) 39044c7070dbSScott Long return; 39054c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) 39064c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 39074c7070dbSScott Long } 39084c7070dbSScott Long 39094c7070dbSScott Long 39104c7070dbSScott Long static void 391123ac9029SStephen Hurd _task_fn_iov(void *context) 39124c7070dbSScott Long { 39134c7070dbSScott Long if_ctx_t ctx = context; 39144c7070dbSScott Long 391577c1fcecSEric Joyner if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) && 391677c1fcecSEric Joyner !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 39174c7070dbSScott Long return; 39184c7070dbSScott Long 39194c7070dbSScott Long CTX_LOCK(ctx); 39204c7070dbSScott Long IFDI_VFLR_HANDLE(ctx); 39214c7070dbSScott Long CTX_UNLOCK(ctx); 39224c7070dbSScott Long } 39234c7070dbSScott Long 39244c7070dbSScott Long static int 39254c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS) 39264c7070dbSScott Long { 39274c7070dbSScott Long int err; 39284c7070dbSScott Long if_int_delay_info_t info; 39294c7070dbSScott Long if_ctx_t ctx; 39304c7070dbSScott Long 39314c7070dbSScott Long info = (if_int_delay_info_t)arg1; 39324c7070dbSScott Long ctx = info->iidi_ctx; 39334c7070dbSScott Long info->iidi_req = req; 39344c7070dbSScott Long info->iidi_oidp = oidp; 39354c7070dbSScott Long CTX_LOCK(ctx); 39364c7070dbSScott Long err = IFDI_SYSCTL_INT_DELAY(ctx, info); 39374c7070dbSScott Long CTX_UNLOCK(ctx); 39384c7070dbSScott Long return (err); 39394c7070dbSScott Long } 39404c7070dbSScott Long 39414c7070dbSScott Long /********************************************************************* 39424c7070dbSScott Long * 39434c7070dbSScott Long * IFNET FUNCTIONS 39444c7070dbSScott Long * 39454c7070dbSScott Long **********************************************************************/ 39464c7070dbSScott Long 39474c7070dbSScott Long static void 39484c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx) 39494c7070dbSScott Long { 39504c7070dbSScott Long iflib_stop(ctx); 39514c7070dbSScott Long iflib_init_locked(ctx); 39524c7070dbSScott Long } 39534c7070dbSScott Long 39544c7070dbSScott Long 39554c7070dbSScott Long static void 39564c7070dbSScott Long iflib_if_init(void *arg) 39574c7070dbSScott Long { 39584c7070dbSScott Long if_ctx_t ctx = arg; 39594c7070dbSScott Long 39604c7070dbSScott Long CTX_LOCK(ctx); 39614c7070dbSScott Long iflib_if_init_locked(ctx); 39624c7070dbSScott Long CTX_UNLOCK(ctx); 39634c7070dbSScott Long } 39644c7070dbSScott Long 39654c7070dbSScott Long static int 39664c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m) 39674c7070dbSScott Long { 39684c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 39694c7070dbSScott Long 39704c7070dbSScott Long iflib_txq_t txq; 397123ac9029SStephen Hurd int err, qidx; 3972fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 39734c7070dbSScott Long 39744c7070dbSScott Long if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) { 39754c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 39764c7070dbSScott Long m_freem(m); 3977225eae1bSEric Joyner return (ENETDOWN); 39784c7070dbSScott Long } 39794c7070dbSScott Long 398023ac9029SStephen Hurd MPASS(m->m_nextpkt == NULL); 3981b8ca4756SPatrick Kelsey /* ALTQ-enabled interfaces always use queue 0. */ 39824c7070dbSScott Long qidx = 0; 3983b8ca4756SPatrick Kelsey if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !ALTQ_IS_ENABLED(&ifp->if_snd)) 39844c7070dbSScott Long qidx = QIDX(ctx, m); 39854c7070dbSScott Long /* 39864c7070dbSScott Long * XXX calculate buf_ring based on flowid (divvy up bits?) 39874c7070dbSScott Long */ 39884c7070dbSScott Long txq = &ctx->ifc_txqs[qidx]; 39894c7070dbSScott Long 39904c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 39914c7070dbSScott Long if (txq->ift_closed) { 39924c7070dbSScott Long while (m != NULL) { 39934c7070dbSScott Long next = m->m_nextpkt; 39944c7070dbSScott Long m->m_nextpkt = NULL; 39954c7070dbSScott Long m_freem(m); 399664e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 39974c7070dbSScott Long m = next; 39984c7070dbSScott Long } 39994c7070dbSScott Long return (ENOBUFS); 40004c7070dbSScott Long } 40014c7070dbSScott Long #endif 400223ac9029SStephen Hurd #ifdef notyet 40034c7070dbSScott Long qidx = count = 0; 40044c7070dbSScott Long mp = marr; 40054c7070dbSScott Long next = m; 40064c7070dbSScott Long do { 40074c7070dbSScott Long count++; 40084c7070dbSScott Long next = next->m_nextpkt; 40094c7070dbSScott Long } while (next != NULL); 40104c7070dbSScott Long 401116fb86abSConrad Meyer if (count > nitems(marr)) 40124c7070dbSScott Long if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) { 40134c7070dbSScott Long /* XXX check nextpkt */ 40144c7070dbSScott Long m_freem(m); 40154c7070dbSScott Long /* XXX simplify for now */ 40164c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 40174c7070dbSScott Long return (ENOBUFS); 40184c7070dbSScott Long } 40194c7070dbSScott Long for (next = m, i = 0; next != NULL; i++) { 40204c7070dbSScott Long mp[i] = next; 40214c7070dbSScott Long next = next->m_nextpkt; 40224c7070dbSScott Long mp[i]->m_nextpkt = NULL; 40234c7070dbSScott Long } 402423ac9029SStephen Hurd #endif 40254c7070dbSScott Long DBG_COUNTER_INC(tx_seen); 4026fe51d4cdSStephen Hurd err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate); 40274c7070dbSScott Long 4028fe51d4cdSStephen Hurd if (abdicate) 4029ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 40301225d9daSStephen Hurd if (err) { 4031fe51d4cdSStephen Hurd if (!abdicate) 4032fe51d4cdSStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 40334c7070dbSScott Long /* support forthcoming later */ 40344c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 40354c7070dbSScott Long txq->ift_closed = TRUE; 40364c7070dbSScott Long #endif 403795246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 403823ac9029SStephen Hurd m_freem(m); 403964e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 40404c7070dbSScott Long } 40414c7070dbSScott Long 40424c7070dbSScott Long return (err); 40434c7070dbSScott Long } 40444c7070dbSScott Long 4045b8ca4756SPatrick Kelsey #ifdef ALTQ 4046b8ca4756SPatrick Kelsey /* 4047b8ca4756SPatrick Kelsey * The overall approach to integrating iflib with ALTQ is to continue to use 4048b8ca4756SPatrick Kelsey * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware 4049b8ca4756SPatrick Kelsey * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring 4050b8ca4756SPatrick Kelsey * is redundant/unnecessary, but doing so minimizes the amount of 4051b8ca4756SPatrick Kelsey * ALTQ-specific code required in iflib. It is assumed that the overhead of 4052b8ca4756SPatrick Kelsey * redundantly queueing to an intermediate mp_ring is swamped by the 4053b8ca4756SPatrick Kelsey * performance limitations inherent in using ALTQ. 4054b8ca4756SPatrick Kelsey * 4055b8ca4756SPatrick Kelsey * When ALTQ support is compiled in, all iflib drivers will use a transmit 4056b8ca4756SPatrick Kelsey * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the 4057b8ca4756SPatrick Kelsey * given interface. If ALTQ is enabled for an interface, then all 4058b8ca4756SPatrick Kelsey * transmitted packets for that interface will be submitted to the ALTQ 4059b8ca4756SPatrick Kelsey * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit() 4060b8ca4756SPatrick Kelsey * implementation because it uses IFQ_HANDOFF(), which will duplicatively 4061b8ca4756SPatrick Kelsey * update stats that the iflib machinery handles, and which is sensitve to 4062b8ca4756SPatrick Kelsey * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start() 4063b8ca4756SPatrick Kelsey * will be installed as the start routine for use by ALTQ facilities that 4064b8ca4756SPatrick Kelsey * need to trigger queue drains on a scheduled basis. 4065b8ca4756SPatrick Kelsey * 4066b8ca4756SPatrick Kelsey */ 4067b8ca4756SPatrick Kelsey static void 4068b8ca4756SPatrick Kelsey iflib_altq_if_start(if_t ifp) 4069b8ca4756SPatrick Kelsey { 4070b8ca4756SPatrick Kelsey struct ifaltq *ifq = &ifp->if_snd; 4071b8ca4756SPatrick Kelsey struct mbuf *m; 4072b8ca4756SPatrick Kelsey 4073b8ca4756SPatrick Kelsey IFQ_LOCK(ifq); 4074b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 4075b8ca4756SPatrick Kelsey while (m != NULL) { 4076b8ca4756SPatrick Kelsey iflib_if_transmit(ifp, m); 4077b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 4078b8ca4756SPatrick Kelsey } 4079b8ca4756SPatrick Kelsey IFQ_UNLOCK(ifq); 4080b8ca4756SPatrick Kelsey } 4081b8ca4756SPatrick Kelsey 4082b8ca4756SPatrick Kelsey static int 4083b8ca4756SPatrick Kelsey iflib_altq_if_transmit(if_t ifp, struct mbuf *m) 4084b8ca4756SPatrick Kelsey { 4085b8ca4756SPatrick Kelsey int err; 4086b8ca4756SPatrick Kelsey 4087b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 4088b8ca4756SPatrick Kelsey IFQ_ENQUEUE(&ifp->if_snd, m, err); 4089b8ca4756SPatrick Kelsey if (err == 0) 4090b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 4091b8ca4756SPatrick Kelsey } else 4092b8ca4756SPatrick Kelsey err = iflib_if_transmit(ifp, m); 4093b8ca4756SPatrick Kelsey 4094b8ca4756SPatrick Kelsey return (err); 4095b8ca4756SPatrick Kelsey } 4096b8ca4756SPatrick Kelsey #endif /* ALTQ */ 4097b8ca4756SPatrick Kelsey 40984c7070dbSScott Long static void 40994c7070dbSScott Long iflib_if_qflush(if_t ifp) 41004c7070dbSScott Long { 41014c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 41024c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 41034c7070dbSScott Long int i; 41044c7070dbSScott Long 41057b610b60SSean Bruno STATE_LOCK(ctx); 41064c7070dbSScott Long ctx->ifc_flags |= IFC_QFLUSH; 41077b610b60SSean Bruno STATE_UNLOCK(ctx); 41084c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 410995246abbSSean Bruno while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br))) 41104c7070dbSScott Long iflib_txq_check_drain(txq, 0); 41117b610b60SSean Bruno STATE_LOCK(ctx); 41124c7070dbSScott Long ctx->ifc_flags &= ~IFC_QFLUSH; 41137b610b60SSean Bruno STATE_UNLOCK(ctx); 41144c7070dbSScott Long 4115b8ca4756SPatrick Kelsey /* 4116b8ca4756SPatrick Kelsey * When ALTQ is enabled, this will also take care of purging the 4117b8ca4756SPatrick Kelsey * ALTQ queue(s). 4118b8ca4756SPatrick Kelsey */ 41194c7070dbSScott Long if_qflush(ifp); 41204c7070dbSScott Long } 41214c7070dbSScott Long 41224c7070dbSScott Long 41230c919c23SStephen Hurd #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \ 41240c919c23SStephen Hurd IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \ 41250c919c23SStephen Hurd IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \ 41266554362cSAndrew Gallatin IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_NOMAP) 41274c7070dbSScott Long 41284c7070dbSScott Long static int 41294c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data) 41304c7070dbSScott Long { 41314c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 41324c7070dbSScott Long struct ifreq *ifr = (struct ifreq *)data; 41334c7070dbSScott Long #if defined(INET) || defined(INET6) 41344c7070dbSScott Long struct ifaddr *ifa = (struct ifaddr *)data; 41354c7070dbSScott Long #endif 41361722eeacSMarius Strobl bool avoid_reset = false; 41374c7070dbSScott Long int err = 0, reinit = 0, bits; 41384c7070dbSScott Long 41394c7070dbSScott Long switch (command) { 41404c7070dbSScott Long case SIOCSIFADDR: 41414c7070dbSScott Long #ifdef INET 41424c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET) 41431722eeacSMarius Strobl avoid_reset = true; 41444c7070dbSScott Long #endif 41454c7070dbSScott Long #ifdef INET6 41464c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET6) 41471722eeacSMarius Strobl avoid_reset = true; 41484c7070dbSScott Long #endif 41494c7070dbSScott Long /* 41504c7070dbSScott Long ** Calling init results in link renegotiation, 41514c7070dbSScott Long ** so we avoid doing it when possible. 41524c7070dbSScott Long */ 41534c7070dbSScott Long if (avoid_reset) { 41544c7070dbSScott Long if_setflagbits(ifp, IFF_UP,0); 41554c7070dbSScott Long if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 41564c7070dbSScott Long reinit = 1; 41574c7070dbSScott Long #ifdef INET 41584c7070dbSScott Long if (!(if_getflags(ifp) & IFF_NOARP)) 41594c7070dbSScott Long arp_ifinit(ifp, ifa); 41604c7070dbSScott Long #endif 41614c7070dbSScott Long } else 41624c7070dbSScott Long err = ether_ioctl(ifp, command, data); 41634c7070dbSScott Long break; 41644c7070dbSScott Long case SIOCSIFMTU: 41654c7070dbSScott Long CTX_LOCK(ctx); 41664c7070dbSScott Long if (ifr->ifr_mtu == if_getmtu(ifp)) { 41674c7070dbSScott Long CTX_UNLOCK(ctx); 41684c7070dbSScott Long break; 41694c7070dbSScott Long } 41704c7070dbSScott Long bits = if_getdrvflags(ifp); 41714c7070dbSScott Long /* stop the driver and free any clusters before proceeding */ 41724c7070dbSScott Long iflib_stop(ctx); 41734c7070dbSScott Long 41744c7070dbSScott Long if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) { 41757b610b60SSean Bruno STATE_LOCK(ctx); 41764c7070dbSScott Long if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size) 41774c7070dbSScott Long ctx->ifc_flags |= IFC_MULTISEG; 41784c7070dbSScott Long else 41794c7070dbSScott Long ctx->ifc_flags &= ~IFC_MULTISEG; 41807b610b60SSean Bruno STATE_UNLOCK(ctx); 41814c7070dbSScott Long err = if_setmtu(ifp, ifr->ifr_mtu); 41824c7070dbSScott Long } 41834c7070dbSScott Long iflib_init_locked(ctx); 41847b610b60SSean Bruno STATE_LOCK(ctx); 41854c7070dbSScott Long if_setdrvflags(ifp, bits); 41867b610b60SSean Bruno STATE_UNLOCK(ctx); 41874c7070dbSScott Long CTX_UNLOCK(ctx); 41884c7070dbSScott Long break; 41894c7070dbSScott Long case SIOCSIFFLAGS: 4190ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4191ab2e3f79SStephen Hurd if (if_getflags(ifp) & IFF_UP) { 4192ab2e3f79SStephen Hurd if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4193ab2e3f79SStephen Hurd if ((if_getflags(ifp) ^ ctx->ifc_if_flags) & 4194ab2e3f79SStephen Hurd (IFF_PROMISC | IFF_ALLMULTI)) { 4195*0ae0e8d2SMatt Macy CTX_UNLOCK(ctx); 4196ab2e3f79SStephen Hurd err = IFDI_PROMISC_SET(ctx, if_getflags(ifp)); 4197*0ae0e8d2SMatt Macy CTX_LOCK(ctx); 4198ab2e3f79SStephen Hurd } 4199ab2e3f79SStephen Hurd } else 4200ab2e3f79SStephen Hurd reinit = 1; 4201ab2e3f79SStephen Hurd } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4202ab2e3f79SStephen Hurd iflib_stop(ctx); 4203ab2e3f79SStephen Hurd } 4204ab2e3f79SStephen Hurd ctx->ifc_if_flags = if_getflags(ifp); 4205ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 42064c7070dbSScott Long break; 42074c7070dbSScott Long case SIOCADDMULTI: 42084c7070dbSScott Long case SIOCDELMULTI: 42094c7070dbSScott Long if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4210ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4211ab2e3f79SStephen Hurd IFDI_INTR_DISABLE(ctx); 4212ab2e3f79SStephen Hurd IFDI_MULTI_SET(ctx); 4213ab2e3f79SStephen Hurd IFDI_INTR_ENABLE(ctx); 4214ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 42154c7070dbSScott Long } 42164c7070dbSScott Long break; 42174c7070dbSScott Long case SIOCSIFMEDIA: 42184c7070dbSScott Long CTX_LOCK(ctx); 42194c7070dbSScott Long IFDI_MEDIA_SET(ctx); 42204c7070dbSScott Long CTX_UNLOCK(ctx); 42211722eeacSMarius Strobl /* FALLTHROUGH */ 42224c7070dbSScott Long case SIOCGIFMEDIA: 4223a027c8e9SStephen Hurd case SIOCGIFXMEDIA: 4224e2621d96SMatt Macy err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command); 42254c7070dbSScott Long break; 42264c7070dbSScott Long case SIOCGI2C: 42274c7070dbSScott Long { 42284c7070dbSScott Long struct ifi2creq i2c; 42294c7070dbSScott Long 4230541d96aaSBrooks Davis err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c)); 42314c7070dbSScott Long if (err != 0) 42324c7070dbSScott Long break; 42334c7070dbSScott Long if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) { 42344c7070dbSScott Long err = EINVAL; 42354c7070dbSScott Long break; 42364c7070dbSScott Long } 42374c7070dbSScott Long if (i2c.len > sizeof(i2c.data)) { 42384c7070dbSScott Long err = EINVAL; 42394c7070dbSScott Long break; 42404c7070dbSScott Long } 42414c7070dbSScott Long 42424c7070dbSScott Long if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0) 4243541d96aaSBrooks Davis err = copyout(&i2c, ifr_data_get_ptr(ifr), 4244541d96aaSBrooks Davis sizeof(i2c)); 42454c7070dbSScott Long break; 42464c7070dbSScott Long } 42474c7070dbSScott Long case SIOCSIFCAP: 42484c7070dbSScott Long { 42490c919c23SStephen Hurd int mask, setmask, oldmask; 42504c7070dbSScott Long 42510c919c23SStephen Hurd oldmask = if_getcapenable(ifp); 42520c919c23SStephen Hurd mask = ifr->ifr_reqcap ^ oldmask; 42536554362cSAndrew Gallatin mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_NOMAP; 42544c7070dbSScott Long setmask = 0; 42554c7070dbSScott Long #ifdef TCP_OFFLOAD 42564c7070dbSScott Long setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6); 42574c7070dbSScott Long #endif 42584c7070dbSScott Long setmask |= (mask & IFCAP_FLAGS); 42590c919c23SStephen Hurd setmask |= (mask & IFCAP_WOL); 42604c7070dbSScott Long 42610c919c23SStephen Hurd /* 4262a42546dfSStephen Hurd * If any RX csum has changed, change all the ones that 4263a42546dfSStephen Hurd * are supported by the driver. 42640c919c23SStephen Hurd */ 4265a42546dfSStephen Hurd if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 4266a42546dfSStephen Hurd setmask |= ctx->ifc_softc_ctx.isc_capabilities & 4267a42546dfSStephen Hurd (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 4268a42546dfSStephen Hurd } 42690c919c23SStephen Hurd 42704c7070dbSScott Long /* 42714c7070dbSScott Long * want to ensure that traffic has stopped before we change any of the flags 42724c7070dbSScott Long */ 42734c7070dbSScott Long if (setmask) { 42744c7070dbSScott Long CTX_LOCK(ctx); 42754c7070dbSScott Long bits = if_getdrvflags(ifp); 42760c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 42774c7070dbSScott Long iflib_stop(ctx); 42787b610b60SSean Bruno STATE_LOCK(ctx); 42794c7070dbSScott Long if_togglecapenable(ifp, setmask); 42807b610b60SSean Bruno STATE_UNLOCK(ctx); 42810c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 42824c7070dbSScott Long iflib_init_locked(ctx); 42837b610b60SSean Bruno STATE_LOCK(ctx); 42844c7070dbSScott Long if_setdrvflags(ifp, bits); 42857b610b60SSean Bruno STATE_UNLOCK(ctx); 42864c7070dbSScott Long CTX_UNLOCK(ctx); 42874c7070dbSScott Long } 42880c919c23SStephen Hurd if_vlancap(ifp); 42894c7070dbSScott Long break; 42904c7070dbSScott Long } 42914c7070dbSScott Long case SIOCGPRIVATE_0: 42924c7070dbSScott Long case SIOCSDRVSPEC: 42934c7070dbSScott Long case SIOCGDRVSPEC: 42944c7070dbSScott Long CTX_LOCK(ctx); 42954c7070dbSScott Long err = IFDI_PRIV_IOCTL(ctx, command, data); 42964c7070dbSScott Long CTX_UNLOCK(ctx); 42974c7070dbSScott Long break; 42984c7070dbSScott Long default: 42994c7070dbSScott Long err = ether_ioctl(ifp, command, data); 43004c7070dbSScott Long break; 43014c7070dbSScott Long } 43024c7070dbSScott Long if (reinit) 43034c7070dbSScott Long iflib_if_init(ctx); 43044c7070dbSScott Long return (err); 43054c7070dbSScott Long } 43064c7070dbSScott Long 43074c7070dbSScott Long static uint64_t 43084c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt) 43094c7070dbSScott Long { 43104c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 43114c7070dbSScott Long 43124c7070dbSScott Long return (IFDI_GET_COUNTER(ctx, cnt)); 43134c7070dbSScott Long } 43144c7070dbSScott Long 43154c7070dbSScott Long /********************************************************************* 43164c7070dbSScott Long * 43174c7070dbSScott Long * OTHER FUNCTIONS EXPORTED TO THE STACK 43184c7070dbSScott Long * 43194c7070dbSScott Long **********************************************************************/ 43204c7070dbSScott Long 43214c7070dbSScott Long static void 43224c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag) 43234c7070dbSScott Long { 43244c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 43254c7070dbSScott Long 43264c7070dbSScott Long if ((void *)ctx != arg) 43274c7070dbSScott Long return; 43284c7070dbSScott Long 43294c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 43304c7070dbSScott Long return; 43314c7070dbSScott Long 433253b5b9b0SEric Joyner if (iflib_in_detach(ctx)) 433353b5b9b0SEric Joyner return; 433453b5b9b0SEric Joyner 43354c7070dbSScott Long CTX_LOCK(ctx); 433645818bf1SEric Joyner /* Driver may need all untagged packets to be flushed */ 433745818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 433845818bf1SEric Joyner iflib_stop(ctx); 43394c7070dbSScott Long IFDI_VLAN_REGISTER(ctx, vtag); 434045818bf1SEric Joyner /* Re-init to load the changes, if required */ 434145818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 434245818bf1SEric Joyner iflib_init_locked(ctx); 43434c7070dbSScott Long CTX_UNLOCK(ctx); 43444c7070dbSScott Long } 43454c7070dbSScott Long 43464c7070dbSScott Long static void 43474c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag) 43484c7070dbSScott Long { 43494c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 43504c7070dbSScott Long 43514c7070dbSScott Long if ((void *)ctx != arg) 43524c7070dbSScott Long return; 43534c7070dbSScott Long 43544c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 43554c7070dbSScott Long return; 43564c7070dbSScott Long 43574c7070dbSScott Long CTX_LOCK(ctx); 435845818bf1SEric Joyner /* Driver may need all tagged packets to be flushed */ 435945818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 436045818bf1SEric Joyner iflib_stop(ctx); 43614c7070dbSScott Long IFDI_VLAN_UNREGISTER(ctx, vtag); 436245818bf1SEric Joyner /* Re-init to load the changes, if required */ 436345818bf1SEric Joyner if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG)) 436445818bf1SEric Joyner iflib_init_locked(ctx); 43654c7070dbSScott Long CTX_UNLOCK(ctx); 43664c7070dbSScott Long } 43674c7070dbSScott Long 43684c7070dbSScott Long static void 43694c7070dbSScott Long iflib_led_func(void *arg, int onoff) 43704c7070dbSScott Long { 43714c7070dbSScott Long if_ctx_t ctx = arg; 43724c7070dbSScott Long 43734c7070dbSScott Long CTX_LOCK(ctx); 43744c7070dbSScott Long IFDI_LED_FUNC(ctx, onoff); 43754c7070dbSScott Long CTX_UNLOCK(ctx); 43764c7070dbSScott Long } 43774c7070dbSScott Long 43784c7070dbSScott Long /********************************************************************* 43794c7070dbSScott Long * 43804c7070dbSScott Long * BUS FUNCTION DEFINITIONS 43814c7070dbSScott Long * 43824c7070dbSScott Long **********************************************************************/ 43834c7070dbSScott Long 43844c7070dbSScott Long int 43854c7070dbSScott Long iflib_device_probe(device_t dev) 43864c7070dbSScott Long { 4387d49e83eaSMarius Strobl const pci_vendor_info_t *ent; 43884c7070dbSScott Long if_shared_ctx_t sctx; 4389d49e83eaSMarius Strobl uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id; 4390d49e83eaSMarius Strobl uint16_t pci_vendor_id; 43914c7070dbSScott Long 43924c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 43934c7070dbSScott Long return (ENOTSUP); 43944c7070dbSScott Long 43954c7070dbSScott Long pci_vendor_id = pci_get_vendor(dev); 43964c7070dbSScott Long pci_device_id = pci_get_device(dev); 43974c7070dbSScott Long pci_subvendor_id = pci_get_subvendor(dev); 43984c7070dbSScott Long pci_subdevice_id = pci_get_subdevice(dev); 43994c7070dbSScott Long pci_rev_id = pci_get_revid(dev); 44004c7070dbSScott Long if (sctx->isc_parse_devinfo != NULL) 44014c7070dbSScott Long sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id); 44024c7070dbSScott Long 44034c7070dbSScott Long ent = sctx->isc_vendor_info; 44044c7070dbSScott Long while (ent->pvi_vendor_id != 0) { 44054c7070dbSScott Long if (pci_vendor_id != ent->pvi_vendor_id) { 44064c7070dbSScott Long ent++; 44074c7070dbSScott Long continue; 44084c7070dbSScott Long } 44094c7070dbSScott Long if ((pci_device_id == ent->pvi_device_id) && 44104c7070dbSScott Long ((pci_subvendor_id == ent->pvi_subvendor_id) || 44114c7070dbSScott Long (ent->pvi_subvendor_id == 0)) && 44124c7070dbSScott Long ((pci_subdevice_id == ent->pvi_subdevice_id) || 44134c7070dbSScott Long (ent->pvi_subdevice_id == 0)) && 44144c7070dbSScott Long ((pci_rev_id == ent->pvi_rev_id) || 44154c7070dbSScott Long (ent->pvi_rev_id == 0))) { 44164c7070dbSScott Long 44174c7070dbSScott Long device_set_desc_copy(dev, ent->pvi_name); 44184c7070dbSScott Long /* this needs to be changed to zero if the bus probing code 44194c7070dbSScott Long * ever stops re-probing on best match because the sctx 44204c7070dbSScott Long * may have its values over written by register calls 44214c7070dbSScott Long * in subsequent probes 44224c7070dbSScott Long */ 44234c7070dbSScott Long return (BUS_PROBE_DEFAULT); 44244c7070dbSScott Long } 44254c7070dbSScott Long ent++; 44264c7070dbSScott Long } 44274c7070dbSScott Long return (ENXIO); 44284c7070dbSScott Long } 44294c7070dbSScott Long 4430668d6dbbSEric Joyner int 4431668d6dbbSEric Joyner iflib_device_probe_vendor(device_t dev) 4432668d6dbbSEric Joyner { 4433668d6dbbSEric Joyner int probe; 4434668d6dbbSEric Joyner 4435668d6dbbSEric Joyner probe = iflib_device_probe(dev); 4436668d6dbbSEric Joyner if (probe == BUS_PROBE_DEFAULT) 4437668d6dbbSEric Joyner return (BUS_PROBE_VENDOR); 4438668d6dbbSEric Joyner else 4439668d6dbbSEric Joyner return (probe); 4440668d6dbbSEric Joyner } 4441668d6dbbSEric Joyner 444209f6ff4fSMatt Macy static void 444309f6ff4fSMatt Macy iflib_reset_qvalues(if_ctx_t ctx) 44444c7070dbSScott Long { 444509f6ff4fSMatt Macy if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 444609f6ff4fSMatt Macy if_shared_ctx_t sctx = ctx->ifc_sctx; 444709f6ff4fSMatt Macy device_t dev = ctx->ifc_dev; 444846d0f824SMatt Macy int i; 44494c7070dbSScott Long 445023ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxqs != 0) 445123ac9029SStephen Hurd scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs; 445223ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxqs != 0) 445323ac9029SStephen Hurd scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs; 445423ac9029SStephen Hurd 445523ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 445623ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxds[i] != 0) 445723ac9029SStephen Hurd scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i]; 445823ac9029SStephen Hurd else 445923ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 446023ac9029SStephen Hurd } 446123ac9029SStephen Hurd 446223ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 446323ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxds[i] != 0) 446423ac9029SStephen Hurd scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i]; 446523ac9029SStephen Hurd else 446623ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 446723ac9029SStephen Hurd } 446823ac9029SStephen Hurd 446923ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 447023ac9029SStephen Hurd if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) { 447123ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n", 447223ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]); 447323ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i]; 447423ac9029SStephen Hurd } 447523ac9029SStephen Hurd if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) { 447623ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n", 447723ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]); 447823ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i]; 447923ac9029SStephen Hurd } 4480afb77372SEric Joyner if (!powerof2(scctx->isc_nrxd[i])) { 4481afb77372SEric Joyner device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n", 4482afb77372SEric Joyner i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]); 4483afb77372SEric Joyner scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 4484afb77372SEric Joyner } 448523ac9029SStephen Hurd } 448623ac9029SStephen Hurd 448723ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 448823ac9029SStephen Hurd if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) { 448923ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n", 449023ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]); 449123ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i]; 449223ac9029SStephen Hurd } 449323ac9029SStephen Hurd if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) { 449423ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n", 449523ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]); 449623ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i]; 449723ac9029SStephen Hurd } 4498afb77372SEric Joyner if (!powerof2(scctx->isc_ntxd[i])) { 4499afb77372SEric Joyner device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n", 4500afb77372SEric Joyner i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]); 4501afb77372SEric Joyner scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 4502afb77372SEric Joyner } 450323ac9029SStephen Hurd } 450409f6ff4fSMatt Macy } 4505ab2e3f79SStephen Hurd 45066d49b41eSAndrew Gallatin static void 45076d49b41eSAndrew Gallatin iflib_add_pfil(if_ctx_t ctx) 45086d49b41eSAndrew Gallatin { 45096d49b41eSAndrew Gallatin struct pfil_head *pfil; 45106d49b41eSAndrew Gallatin struct pfil_head_args pa; 45116d49b41eSAndrew Gallatin iflib_rxq_t rxq; 45126d49b41eSAndrew Gallatin int i; 45136d49b41eSAndrew Gallatin 45146d49b41eSAndrew Gallatin pa.pa_version = PFIL_VERSION; 45156d49b41eSAndrew Gallatin pa.pa_flags = PFIL_IN; 45166d49b41eSAndrew Gallatin pa.pa_type = PFIL_TYPE_ETHERNET; 45176d49b41eSAndrew Gallatin pa.pa_headname = ctx->ifc_ifp->if_xname; 45186d49b41eSAndrew Gallatin pfil = pfil_head_register(&pa); 45196d49b41eSAndrew Gallatin 45206d49b41eSAndrew Gallatin for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 45216d49b41eSAndrew Gallatin rxq->pfil = pfil; 45226d49b41eSAndrew Gallatin } 45236d49b41eSAndrew Gallatin } 45246d49b41eSAndrew Gallatin 45256d49b41eSAndrew Gallatin static void 45266d49b41eSAndrew Gallatin iflib_rem_pfil(if_ctx_t ctx) 45276d49b41eSAndrew Gallatin { 45286d49b41eSAndrew Gallatin struct pfil_head *pfil; 45296d49b41eSAndrew Gallatin iflib_rxq_t rxq; 45306d49b41eSAndrew Gallatin int i; 45316d49b41eSAndrew Gallatin 45326d49b41eSAndrew Gallatin rxq = ctx->ifc_rxqs; 45336d49b41eSAndrew Gallatin pfil = rxq->pfil; 45346d49b41eSAndrew Gallatin for (i = 0; i < NRXQSETS(ctx); i++, rxq++) { 45356d49b41eSAndrew Gallatin rxq->pfil = NULL; 45366d49b41eSAndrew Gallatin } 45376d49b41eSAndrew Gallatin pfil_head_unregister(pfil); 45386d49b41eSAndrew Gallatin } 45396d49b41eSAndrew Gallatin 4540f154ece0SStephen Hurd static uint16_t 4541f154ece0SStephen Hurd get_ctx_core_offset(if_ctx_t ctx) 4542f154ece0SStephen Hurd { 4543f154ece0SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 4544f154ece0SStephen Hurd struct cpu_offset *op; 4545f154ece0SStephen Hurd uint16_t qc; 4546f154ece0SStephen Hurd uint16_t ret = ctx->ifc_sysctl_core_offset; 4547f154ece0SStephen Hurd 4548f154ece0SStephen Hurd if (ret != CORE_OFFSET_UNSPECIFIED) 4549f154ece0SStephen Hurd return (ret); 4550f154ece0SStephen Hurd 4551f154ece0SStephen Hurd if (ctx->ifc_sysctl_separate_txrx) 4552f154ece0SStephen Hurd qc = scctx->isc_ntxqsets + scctx->isc_nrxqsets; 4553f154ece0SStephen Hurd else 4554f154ece0SStephen Hurd qc = max(scctx->isc_ntxqsets, scctx->isc_nrxqsets); 4555f154ece0SStephen Hurd 4556f154ece0SStephen Hurd mtx_lock(&cpu_offset_mtx); 4557f154ece0SStephen Hurd SLIST_FOREACH(op, &cpu_offsets, entries) { 4558f154ece0SStephen Hurd if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 4559f154ece0SStephen Hurd ret = op->offset; 4560f154ece0SStephen Hurd op->offset += qc; 4561f154ece0SStephen Hurd MPASS(op->refcount < UINT_MAX); 4562f154ece0SStephen Hurd op->refcount++; 4563f154ece0SStephen Hurd break; 4564f154ece0SStephen Hurd } 4565f154ece0SStephen Hurd } 4566f154ece0SStephen Hurd if (ret == CORE_OFFSET_UNSPECIFIED) { 4567f154ece0SStephen Hurd ret = 0; 4568f154ece0SStephen Hurd op = malloc(sizeof(struct cpu_offset), M_IFLIB, 4569f154ece0SStephen Hurd M_NOWAIT | M_ZERO); 4570f154ece0SStephen Hurd if (op == NULL) { 4571f154ece0SStephen Hurd device_printf(ctx->ifc_dev, 4572f154ece0SStephen Hurd "allocation for cpu offset failed.\n"); 4573f154ece0SStephen Hurd } else { 4574f154ece0SStephen Hurd op->offset = qc; 4575f154ece0SStephen Hurd op->refcount = 1; 4576f154ece0SStephen Hurd CPU_COPY(&ctx->ifc_cpus, &op->set); 4577f154ece0SStephen Hurd SLIST_INSERT_HEAD(&cpu_offsets, op, entries); 4578f154ece0SStephen Hurd } 4579f154ece0SStephen Hurd } 4580f154ece0SStephen Hurd mtx_unlock(&cpu_offset_mtx); 4581f154ece0SStephen Hurd 4582f154ece0SStephen Hurd return (ret); 4583f154ece0SStephen Hurd } 4584f154ece0SStephen Hurd 4585f154ece0SStephen Hurd static void 4586f154ece0SStephen Hurd unref_ctx_core_offset(if_ctx_t ctx) 4587f154ece0SStephen Hurd { 4588f154ece0SStephen Hurd struct cpu_offset *op, *top; 4589f154ece0SStephen Hurd 4590f154ece0SStephen Hurd mtx_lock(&cpu_offset_mtx); 4591f154ece0SStephen Hurd SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) { 4592f154ece0SStephen Hurd if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 4593f154ece0SStephen Hurd MPASS(op->refcount > 0); 4594f154ece0SStephen Hurd op->refcount--; 4595f154ece0SStephen Hurd if (op->refcount == 0) { 4596f154ece0SStephen Hurd SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries); 4597f154ece0SStephen Hurd free(op, M_IFLIB); 4598f154ece0SStephen Hurd } 4599f154ece0SStephen Hurd break; 4600f154ece0SStephen Hurd } 4601f154ece0SStephen Hurd } 4602f154ece0SStephen Hurd mtx_unlock(&cpu_offset_mtx); 4603f154ece0SStephen Hurd } 4604f154ece0SStephen Hurd 460509f6ff4fSMatt Macy int 460609f6ff4fSMatt Macy iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp) 460709f6ff4fSMatt Macy { 460809f6ff4fSMatt Macy if_ctx_t ctx; 460909f6ff4fSMatt Macy if_t ifp; 461009f6ff4fSMatt Macy if_softc_ctx_t scctx; 46113d10e9edSMarius Strobl kobjop_desc_t kobj_desc; 46123d10e9edSMarius Strobl kobj_method_t *kobj_method; 4613afb77372SEric Joyner int err, msix, rid; 4614ac11d857SVincenzo Maffione int num_txd, num_rxd; 461509f6ff4fSMatt Macy 461609f6ff4fSMatt Macy ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO); 461709f6ff4fSMatt Macy 461809f6ff4fSMatt Macy if (sc == NULL) { 461909f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 462009f6ff4fSMatt Macy device_set_softc(dev, ctx); 462109f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 462209f6ff4fSMatt Macy } 462309f6ff4fSMatt Macy 462409f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 462509f6ff4fSMatt Macy ctx->ifc_dev = dev; 462609f6ff4fSMatt Macy ctx->ifc_softc = sc; 462709f6ff4fSMatt Macy 462809f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 462909f6ff4fSMatt Macy device_printf(dev, "iflib_register failed %d\n", err); 46307f3eb9daSPatrick Kelsey goto fail_ctx_free; 463109f6ff4fSMatt Macy } 463209f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 463309f6ff4fSMatt Macy 463409f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 463509f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 463609f6ff4fSMatt Macy 463709f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4638aa8a24d3SStephen Hurd CTX_LOCK(ctx); 4639ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 46404c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 46417f3eb9daSPatrick Kelsey goto fail_unlock; 46424c7070dbSScott Long } 46431248952aSSean Bruno _iflib_pre_assert(scctx); 46441248952aSSean Bruno ctx->ifc_txrx = *scctx->isc_txrx; 46451248952aSSean Bruno 4646e2621d96SMatt Macy if (sctx->isc_flags & IFLIB_DRIVER_MEDIA) 4647e2621d96SMatt Macy ctx->ifc_mediap = scctx->isc_media; 4648e2621d96SMatt Macy 46491248952aSSean Bruno #ifdef INVARIANTS 46507f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 46511248952aSSean Bruno MPASS(scctx->isc_tx_csum_flags); 46521248952aSSean Bruno #endif 46531248952aSSean Bruno 46546554362cSAndrew Gallatin if_setcapabilities(ifp, 46556554362cSAndrew Gallatin scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_NOMAP); 46566554362cSAndrew Gallatin if_setcapenable(ifp, 46576554362cSAndrew Gallatin scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_NOMAP); 46581248952aSSean Bruno 46591248952aSSean Bruno if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 46601248952aSSean Bruno scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 46611248952aSSean Bruno if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 46621248952aSSean Bruno scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 466323ac9029SStephen Hurd 4664ac11d857SVincenzo Maffione num_txd = iflib_num_tx_descs(ctx); 4665ac11d857SVincenzo Maffione num_rxd = iflib_num_rx_descs(ctx); 466623ac9029SStephen Hurd 466723ac9029SStephen Hurd /* XXX change for per-queue sizes */ 46681722eeacSMarius Strobl device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 4669ac11d857SVincenzo Maffione num_txd, num_rxd); 467023ac9029SStephen Hurd 4671ac11d857SVincenzo Maffione if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION) 4672ac11d857SVincenzo Maffione scctx->isc_tx_nsegments = max(1, num_txd / 467323ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION); 4674ac11d857SVincenzo Maffione if (scctx->isc_tx_tso_segments_max > num_txd / 467523ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION) 467623ac9029SStephen Hurd scctx->isc_tx_tso_segments_max = max(1, 4677ac11d857SVincenzo Maffione num_txd / MAX_SINGLE_PACKET_FRACTION); 46784c7070dbSScott Long 46794c7070dbSScott Long /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 46807f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 46817f87c040SMarius Strobl /* 46827f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 46837f87c040SMarius Strobl * but some MACs do. 46847f87c040SMarius Strobl */ 46857f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 46867f87c040SMarius Strobl IP_MAXPACKET)); 46877f87c040SMarius Strobl /* 46887f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 46897f87c040SMarius Strobl * into account. In the worst case, each of these calls will 46907f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 46917f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 46927f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 46937f87c040SMarius Strobl * require defragmentation in iflib_encap(). 46947f87c040SMarius Strobl */ 46957f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 46967f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 46977f87c040SMarius Strobl } 46984c7070dbSScott Long if (scctx->isc_rss_table_size == 0) 46994c7070dbSScott Long scctx->isc_rss_table_size = 64; 470023ac9029SStephen Hurd scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 4701da69b8f9SSean Bruno 4702da69b8f9SSean Bruno GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 4703da69b8f9SSean Bruno /* XXX format name */ 4704f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4705f855ec81SMarius Strobl NULL, NULL, "admin"); 4706e516b535SStephen Hurd 4707772593dbSStephen Hurd /* Set up cpu set. If it fails, use the set of all CPUs. */ 4708e516b535SStephen Hurd if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) { 4709e516b535SStephen Hurd device_printf(dev, "Unable to fetch CPU list\n"); 4710e516b535SStephen Hurd CPU_COPY(&all_cpus, &ctx->ifc_cpus); 4711e516b535SStephen Hurd } 4712e516b535SStephen Hurd MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0); 4713e516b535SStephen Hurd 47144c7070dbSScott Long /* 4715b97de13aSMarius Strobl ** Now set up MSI or MSI-X, should return us the number of supported 4716b97de13aSMarius Strobl ** vectors (will be 1 for a legacy interrupt and MSI). 47174c7070dbSScott Long */ 47184c7070dbSScott Long if (sctx->isc_flags & IFLIB_SKIP_MSIX) { 47194c7070dbSScott Long msix = scctx->isc_vectors; 47204c7070dbSScott Long } else if (scctx->isc_msix_bar != 0) 4721f7ae9a84SSean Bruno /* 4722f7ae9a84SSean Bruno * The simple fact that isc_msix_bar is not 0 does not mean we 4723f7ae9a84SSean Bruno * we have a good value there that is known to work. 4724f7ae9a84SSean Bruno */ 47254c7070dbSScott Long msix = iflib_msix_init(ctx); 47264c7070dbSScott Long else { 47274c7070dbSScott Long scctx->isc_vectors = 1; 47284c7070dbSScott Long scctx->isc_ntxqsets = 1; 47294c7070dbSScott Long scctx->isc_nrxqsets = 1; 47304c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 47314c7070dbSScott Long msix = 0; 47324c7070dbSScott Long } 47334c7070dbSScott Long /* Get memory for the station queues */ 47344c7070dbSScott Long if ((err = iflib_queues_alloc(ctx))) { 47354c7070dbSScott Long device_printf(dev, "Unable to allocate queue memory\n"); 47367f3eb9daSPatrick Kelsey goto fail_intr_free; 47374c7070dbSScott Long } 47384c7070dbSScott Long 4739ac88e6daSStephen Hurd if ((err = iflib_qset_structures_setup(ctx))) 47404c7070dbSScott Long goto fail_queues; 474169b7fc3eSSean Bruno 4742bd84f700SSean Bruno /* 4743f154ece0SStephen Hurd * Now that we know how many queues there are, get the core offset. 4744f154ece0SStephen Hurd */ 4745f154ece0SStephen Hurd ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx); 4746f154ece0SStephen Hurd 47473d10e9edSMarius Strobl if (msix > 1) { 47483d10e9edSMarius Strobl /* 47493d10e9edSMarius Strobl * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable 47503d10e9edSMarius Strobl * aren't the default NULL implementation. 47513d10e9edSMarius Strobl */ 47523d10e9edSMarius Strobl kobj_desc = &ifdi_rx_queue_intr_enable_desc; 47533d10e9edSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 47543d10e9edSMarius Strobl kobj_desc); 47553d10e9edSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 47563d10e9edSMarius Strobl device_printf(dev, 47573d10e9edSMarius Strobl "MSI-X requires ifdi_rx_queue_intr_enable method"); 47583d10e9edSMarius Strobl err = EOPNOTSUPP; 47597f3eb9daSPatrick Kelsey goto fail_queues; 47604c7070dbSScott Long } 47613d10e9edSMarius Strobl kobj_desc = &ifdi_tx_queue_intr_enable_desc; 47623d10e9edSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 47633d10e9edSMarius Strobl kobj_desc); 47643d10e9edSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 47653d10e9edSMarius Strobl device_printf(dev, 47663d10e9edSMarius Strobl "MSI-X requires ifdi_tx_queue_intr_enable method"); 47673d10e9edSMarius Strobl err = EOPNOTSUPP; 47683d10e9edSMarius Strobl goto fail_queues; 47693d10e9edSMarius Strobl } 47703d10e9edSMarius Strobl 47713d10e9edSMarius Strobl /* 47723d10e9edSMarius Strobl * Assign the MSI-X vectors. 47733d10e9edSMarius Strobl * Note that the default NULL ifdi_msix_intr_assign method will 47743d10e9edSMarius Strobl * fail here, too. 47753d10e9edSMarius Strobl */ 47763d10e9edSMarius Strobl err = IFDI_MSIX_INTR_ASSIGN(ctx, msix); 47773d10e9edSMarius Strobl if (err != 0) { 47783d10e9edSMarius Strobl device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", 47793d10e9edSMarius Strobl err); 47803d10e9edSMarius Strobl goto fail_queues; 47813d10e9edSMarius Strobl } 4782197c6798SEric Joyner } else if (scctx->isc_intr != IFLIB_INTR_MSIX) { 47834c7070dbSScott Long rid = 0; 47844c7070dbSScott Long if (scctx->isc_intr == IFLIB_INTR_MSI) { 47854c7070dbSScott Long MPASS(msix == 1); 47864c7070dbSScott Long rid = 1; 47874c7070dbSScott Long } 478823ac9029SStephen Hurd if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) { 47894c7070dbSScott Long device_printf(dev, "iflib_legacy_setup failed %d\n", err); 47907f3eb9daSPatrick Kelsey goto fail_queues; 47914c7070dbSScott Long } 4792197c6798SEric Joyner } else { 4793197c6798SEric Joyner device_printf(dev, 4794197c6798SEric Joyner "Cannot use iflib with only 1 MSI-X interrupt!\n"); 4795197c6798SEric Joyner err = ENODEV; 4796197c6798SEric Joyner goto fail_intr_free; 47974c7070dbSScott Long } 47987f87c040SMarius Strobl 47991fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 48007f87c040SMarius Strobl 4801ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 48024c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 48034c7070dbSScott Long goto fail_detach; 48044c7070dbSScott Long } 48057f87c040SMarius Strobl 48067f87c040SMarius Strobl /* 48077f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 48087f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 48097f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 48107f87c040SMarius Strobl */ 48117f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 48127f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 48137f87c040SMarius Strobl 48144c7070dbSScott Long if ((err = iflib_netmap_attach(ctx))) { 48154c7070dbSScott Long device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err); 48164c7070dbSScott Long goto fail_detach; 48174c7070dbSScott Long } 48184c7070dbSScott Long *ctxp = ctx; 48194c7070dbSScott Long 48207790c8c1SConrad Meyer DEBUGNET_SET(ctx->ifc_ifp, iflib); 482194618825SMark Johnston 482223ac9029SStephen Hurd if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 48234c7070dbSScott Long iflib_add_device_sysctl_post(ctx); 48246d49b41eSAndrew Gallatin iflib_add_pfil(ctx); 48254ecb427aSSean Bruno ctx->ifc_flags |= IFC_INIT_DONE; 4826aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 48273d10e9edSMarius Strobl 48284c7070dbSScott Long return (0); 482977c1fcecSEric Joyner 48304c7070dbSScott Long fail_detach: 48314c7070dbSScott Long ether_ifdetach(ctx->ifc_ifp); 48324c7070dbSScott Long fail_intr_free: 48337f3eb9daSPatrick Kelsey iflib_free_intr_mem(ctx); 48344c7070dbSScott Long fail_queues: 48356108c013SStephen Hurd iflib_tx_structures_free(ctx); 48366108c013SStephen Hurd iflib_rx_structures_free(ctx); 4837197c6798SEric Joyner taskqgroup_detach(qgroup_if_config_tqg, &ctx->ifc_admin_task); 48384c7070dbSScott Long IFDI_DETACH(ctx); 48397f3eb9daSPatrick Kelsey fail_unlock: 4840aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 484156614414SEric Joyner iflib_deregister(ctx); 48427f3eb9daSPatrick Kelsey fail_ctx_free: 48437f3f6aadSEric Joyner device_set_softc(ctx->ifc_dev, NULL); 48447f3eb9daSPatrick Kelsey if (ctx->ifc_flags & IFC_SC_ALLOCATED) 48457f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 48467f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 48474c7070dbSScott Long return (err); 48484c7070dbSScott Long } 48494c7070dbSScott Long 48504c7070dbSScott Long int 485109f6ff4fSMatt Macy iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp, 485209f6ff4fSMatt Macy struct iflib_cloneattach_ctx *clctx) 485309f6ff4fSMatt Macy { 4854ac11d857SVincenzo Maffione int num_txd, num_rxd; 485509f6ff4fSMatt Macy int err; 485609f6ff4fSMatt Macy if_ctx_t ctx; 485709f6ff4fSMatt Macy if_t ifp; 485809f6ff4fSMatt Macy if_softc_ctx_t scctx; 485909f6ff4fSMatt Macy int i; 486009f6ff4fSMatt Macy void *sc; 486109f6ff4fSMatt Macy 486209f6ff4fSMatt Macy ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO); 486309f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 486409f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 486509f6ff4fSMatt Macy if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL)) 486609f6ff4fSMatt Macy ctx->ifc_flags |= IFC_PSEUDO; 486709f6ff4fSMatt Macy 486809f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 486909f6ff4fSMatt Macy ctx->ifc_softc = sc; 487009f6ff4fSMatt Macy ctx->ifc_dev = dev; 487109f6ff4fSMatt Macy 487209f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 487309f6ff4fSMatt Macy device_printf(dev, "%s: iflib_register failed %d\n", __func__, err); 48747f3eb9daSPatrick Kelsey goto fail_ctx_free; 487509f6ff4fSMatt Macy } 487609f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 487709f6ff4fSMatt Macy 487809f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 487909f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 488009f6ff4fSMatt Macy 488109f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4882aac9c817SEric Joyner CTX_LOCK(ctx); 488309f6ff4fSMatt Macy if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 488409f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 4885aac9c817SEric Joyner goto fail_unlock; 488609f6ff4fSMatt Macy } 488709f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_GEN_MAC) 48881fd8c72cSKyle Evans ether_gen_addr(ifp, &ctx->ifc_mac); 488909f6ff4fSMatt Macy if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name, 489009f6ff4fSMatt Macy clctx->cc_params)) != 0) { 489109f6ff4fSMatt Macy device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err); 48929aeca213SMatt Macy goto fail_unlock; 489309f6ff4fSMatt Macy } 489409f6ff4fSMatt Macy #ifdef INVARIANTS 48957f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 489609f6ff4fSMatt Macy MPASS(scctx->isc_tx_csum_flags); 489709f6ff4fSMatt Macy #endif 489809f6ff4fSMatt Macy 48997f87c040SMarius Strobl if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_LINKSTATE); 490009f6ff4fSMatt Macy if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE); 490109f6ff4fSMatt Macy 490209f6ff4fSMatt Macy ifp->if_flags |= IFF_NOGROUP; 490309f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO) { 49049aeca213SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL); 49059aeca213SMatt Macy ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO); 49069aeca213SMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO_ETHER) { 49071fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 49089aeca213SMatt Macy } else { 49099aeca213SMatt Macy if_attach(ctx->ifc_ifp); 49109aeca213SMatt Macy bpfattach(ctx->ifc_ifp, DLT_NULL, sizeof(u_int32_t)); 49119aeca213SMatt Macy } 491209f6ff4fSMatt Macy 491309f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 491409f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 491509f6ff4fSMatt Macy goto fail_detach; 491609f6ff4fSMatt Macy } 491709f6ff4fSMatt Macy *ctxp = ctx; 491809f6ff4fSMatt Macy 49197f87c040SMarius Strobl /* 49207f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 49217f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 49227f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 49237f87c040SMarius Strobl */ 49247f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 49257f87c040SMarius Strobl if_setifheaderlen(ifp, 49267f87c040SMarius Strobl sizeof(struct ether_vlan_header)); 49277f87c040SMarius Strobl 492809f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 492909f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 493009f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 49311f93e931SMatt Macy CTX_UNLOCK(ctx); 493209f6ff4fSMatt Macy return (0); 493309f6ff4fSMatt Macy } 49349aeca213SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL); 49359aeca213SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL); 49369aeca213SMatt Macy ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO); 49379aeca213SMatt Macy 493809f6ff4fSMatt Macy _iflib_pre_assert(scctx); 493909f6ff4fSMatt Macy ctx->ifc_txrx = *scctx->isc_txrx; 494009f6ff4fSMatt Macy 494109f6ff4fSMatt Macy if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 494209f6ff4fSMatt Macy scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 494309f6ff4fSMatt Macy if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 494409f6ff4fSMatt Macy scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 494509f6ff4fSMatt Macy 4946ac11d857SVincenzo Maffione num_txd = iflib_num_tx_descs(ctx); 4947ac11d857SVincenzo Maffione num_rxd = iflib_num_rx_descs(ctx); 494809f6ff4fSMatt Macy 494909f6ff4fSMatt Macy /* XXX change for per-queue sizes */ 49501722eeacSMarius Strobl device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 4951ac11d857SVincenzo Maffione num_txd, num_rxd); 495209f6ff4fSMatt Macy 4953ac11d857SVincenzo Maffione if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION) 4954ac11d857SVincenzo Maffione scctx->isc_tx_nsegments = max(1, num_txd / 495509f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION); 4956ac11d857SVincenzo Maffione if (scctx->isc_tx_tso_segments_max > num_txd / 495709f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION) 495809f6ff4fSMatt Macy scctx->isc_tx_tso_segments_max = max(1, 4959ac11d857SVincenzo Maffione num_txd / MAX_SINGLE_PACKET_FRACTION); 496009f6ff4fSMatt Macy 496109f6ff4fSMatt Macy /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 49627f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 49637f87c040SMarius Strobl /* 49647f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 49657f87c040SMarius Strobl * but some MACs do. 49667f87c040SMarius Strobl */ 49677f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 49687f87c040SMarius Strobl IP_MAXPACKET)); 49697f87c040SMarius Strobl /* 49707f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 49717f87c040SMarius Strobl * into account. In the worst case, each of these calls will 49727f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 49737f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 49747f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 49757f87c040SMarius Strobl * require defragmentation in iflib_encap(). 49767f87c040SMarius Strobl */ 49777f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 49787f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 49797f87c040SMarius Strobl } 498009f6ff4fSMatt Macy if (scctx->isc_rss_table_size == 0) 498109f6ff4fSMatt Macy scctx->isc_rss_table_size = 64; 498209f6ff4fSMatt Macy scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 498309f6ff4fSMatt Macy 498409f6ff4fSMatt Macy GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 498509f6ff4fSMatt Macy /* XXX format name */ 4986f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4987f855ec81SMarius Strobl NULL, NULL, "admin"); 498809f6ff4fSMatt Macy 498909f6ff4fSMatt Macy /* XXX --- can support > 1 -- but keep it simple for now */ 499009f6ff4fSMatt Macy scctx->isc_intr = IFLIB_INTR_LEGACY; 499109f6ff4fSMatt Macy 499209f6ff4fSMatt Macy /* Get memory for the station queues */ 499309f6ff4fSMatt Macy if ((err = iflib_queues_alloc(ctx))) { 499409f6ff4fSMatt Macy device_printf(dev, "Unable to allocate queue memory\n"); 49957f3eb9daSPatrick Kelsey goto fail_iflib_detach; 499609f6ff4fSMatt Macy } 499709f6ff4fSMatt Macy 499809f6ff4fSMatt Macy if ((err = iflib_qset_structures_setup(ctx))) { 499909f6ff4fSMatt Macy device_printf(dev, "qset structure setup failed %d\n", err); 500009f6ff4fSMatt Macy goto fail_queues; 500109f6ff4fSMatt Macy } 50027f87c040SMarius Strobl 500309f6ff4fSMatt Macy /* 500409f6ff4fSMatt Macy * XXX What if anything do we want to do about interrupts? 500509f6ff4fSMatt Macy */ 50061fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 500709f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 500809f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 500909f6ff4fSMatt Macy goto fail_detach; 501009f6ff4fSMatt Macy } 50117f87c040SMarius Strobl 50127f87c040SMarius Strobl /* 50137f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 50147f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 50157f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 50167f87c040SMarius Strobl */ 50177f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 50187f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 50197f87c040SMarius Strobl 502009f6ff4fSMatt Macy /* XXX handle more than one queue */ 502109f6ff4fSMatt Macy for (i = 0; i < scctx->isc_nrxqsets; i++) 502209f6ff4fSMatt Macy IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl); 502309f6ff4fSMatt Macy 502409f6ff4fSMatt Macy *ctxp = ctx; 502509f6ff4fSMatt Macy 502609f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 502709f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 502809f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 5029aac9c817SEric Joyner CTX_UNLOCK(ctx); 50303d10e9edSMarius Strobl 503109f6ff4fSMatt Macy return (0); 503209f6ff4fSMatt Macy fail_detach: 503309f6ff4fSMatt Macy ether_ifdetach(ctx->ifc_ifp); 503409f6ff4fSMatt Macy fail_queues: 503509f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 503609f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 50377f3eb9daSPatrick Kelsey fail_iflib_detach: 503809f6ff4fSMatt Macy IFDI_DETACH(ctx); 5039aac9c817SEric Joyner fail_unlock: 5040aac9c817SEric Joyner CTX_UNLOCK(ctx); 504156614414SEric Joyner iflib_deregister(ctx); 50427f3eb9daSPatrick Kelsey fail_ctx_free: 50437f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 50447f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 504509f6ff4fSMatt Macy return (err); 504609f6ff4fSMatt Macy } 504709f6ff4fSMatt Macy 504809f6ff4fSMatt Macy int 504909f6ff4fSMatt Macy iflib_pseudo_deregister(if_ctx_t ctx) 505009f6ff4fSMatt Macy { 505109f6ff4fSMatt Macy if_t ifp = ctx->ifc_ifp; 50529aeca213SMatt Macy if_shared_ctx_t sctx = ctx->ifc_sctx; 505309f6ff4fSMatt Macy iflib_txq_t txq; 505409f6ff4fSMatt Macy iflib_rxq_t rxq; 505509f6ff4fSMatt Macy int i, j; 505609f6ff4fSMatt Macy struct taskqgroup *tqg; 505709f6ff4fSMatt Macy iflib_fl_t fl; 505809f6ff4fSMatt Macy 50591558015eSEric Joyner /* Unregister VLAN event handlers early */ 50601558015eSEric Joyner iflib_unregister_vlan_handlers(ctx); 50611558015eSEric Joyner 50629aeca213SMatt Macy if ((sctx->isc_flags & IFLIB_PSEUDO) && 50639aeca213SMatt Macy (sctx->isc_flags & IFLIB_PSEUDO_ETHER) == 0) { 50649aeca213SMatt Macy bpfdetach(ifp); 50659aeca213SMatt Macy if_detach(ifp); 50669aeca213SMatt Macy } else { 506709f6ff4fSMatt Macy ether_ifdetach(ifp); 50689aeca213SMatt Macy } 506909f6ff4fSMatt Macy /* XXX drain any dependent tasks */ 507009f6ff4fSMatt Macy tqg = qgroup_if_io_tqg; 507109f6ff4fSMatt Macy for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 507209f6ff4fSMatt Macy callout_drain(&txq->ift_timer); 507309f6ff4fSMatt Macy if (txq->ift_task.gt_uniq != NULL) 507409f6ff4fSMatt Macy taskqgroup_detach(tqg, &txq->ift_task); 507509f6ff4fSMatt Macy } 507609f6ff4fSMatt Macy for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 5077fb1a29b4SHans Petter Selasky callout_drain(&rxq->ifr_watchdog); 507809f6ff4fSMatt Macy if (rxq->ifr_task.gt_uniq != NULL) 507909f6ff4fSMatt Macy taskqgroup_detach(tqg, &rxq->ifr_task); 508009f6ff4fSMatt Macy 508109f6ff4fSMatt Macy for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 508209f6ff4fSMatt Macy free(fl->ifl_rx_bitmap, M_IFLIB); 508309f6ff4fSMatt Macy } 508409f6ff4fSMatt Macy tqg = qgroup_if_config_tqg; 508509f6ff4fSMatt Macy if (ctx->ifc_admin_task.gt_uniq != NULL) 508609f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_admin_task); 508709f6ff4fSMatt Macy if (ctx->ifc_vflr_task.gt_uniq != NULL) 508809f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 508909f6ff4fSMatt Macy 509009f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 509109f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 509256614414SEric Joyner 509356614414SEric Joyner iflib_deregister(ctx); 509456614414SEric Joyner 509509f6ff4fSMatt Macy if (ctx->ifc_flags & IFC_SC_ALLOCATED) 509609f6ff4fSMatt Macy free(ctx->ifc_softc, M_IFLIB); 509709f6ff4fSMatt Macy free(ctx, M_IFLIB); 509809f6ff4fSMatt Macy return (0); 509909f6ff4fSMatt Macy } 510009f6ff4fSMatt Macy 510109f6ff4fSMatt Macy int 51024c7070dbSScott Long iflib_device_attach(device_t dev) 51034c7070dbSScott Long { 51044c7070dbSScott Long if_ctx_t ctx; 51054c7070dbSScott Long if_shared_ctx_t sctx; 51064c7070dbSScott Long 51074c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 51084c7070dbSScott Long return (ENOTSUP); 51094c7070dbSScott Long 51104c7070dbSScott Long pci_enable_busmaster(dev); 51114c7070dbSScott Long 51124c7070dbSScott Long return (iflib_device_register(dev, NULL, sctx, &ctx)); 51134c7070dbSScott Long } 51144c7070dbSScott Long 51154c7070dbSScott Long int 51164c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx) 51174c7070dbSScott Long { 51184c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 51194c7070dbSScott Long iflib_txq_t txq; 51204c7070dbSScott Long iflib_rxq_t rxq; 51214c7070dbSScott Long device_t dev = ctx->ifc_dev; 512287890dbaSSean Bruno int i, j; 51234c7070dbSScott Long struct taskqgroup *tqg; 512487890dbaSSean Bruno iflib_fl_t fl; 51254c7070dbSScott Long 51264c7070dbSScott Long /* Make sure VLANS are not using driver */ 51274c7070dbSScott Long if (if_vlantrunkinuse(ifp)) { 51284c7070dbSScott Long device_printf(dev, "Vlan in use, detach first\n"); 51294c7070dbSScott Long return (EBUSY); 51304c7070dbSScott Long } 513177c1fcecSEric Joyner #ifdef PCI_IOV 513277c1fcecSEric Joyner if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) { 513377c1fcecSEric Joyner device_printf(dev, "SR-IOV in use; detach first.\n"); 513477c1fcecSEric Joyner return (EBUSY); 513577c1fcecSEric Joyner } 513677c1fcecSEric Joyner #endif 513777c1fcecSEric Joyner 513877c1fcecSEric Joyner STATE_LOCK(ctx); 513977c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 514077c1fcecSEric Joyner STATE_UNLOCK(ctx); 51414c7070dbSScott Long 51421558015eSEric Joyner /* Unregister VLAN handlers before calling iflib_stop() */ 51431558015eSEric Joyner iflib_unregister_vlan_handlers(ctx); 51441558015eSEric Joyner 51451558015eSEric Joyner iflib_netmap_detach(ifp); 51461558015eSEric Joyner ether_ifdetach(ifp); 51471558015eSEric Joyner 51484c7070dbSScott Long CTX_LOCK(ctx); 51494c7070dbSScott Long iflib_stop(ctx); 51504c7070dbSScott Long CTX_UNLOCK(ctx); 51514c7070dbSScott Long 51526d49b41eSAndrew Gallatin iflib_rem_pfil(ctx); 51534c7070dbSScott Long if (ctx->ifc_led_dev != NULL) 51544c7070dbSScott Long led_destroy(ctx->ifc_led_dev); 51554c7070dbSScott Long /* XXX drain any dependent tasks */ 5156ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 515723ac9029SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 51584c7070dbSScott Long callout_drain(&txq->ift_timer); 51594c7070dbSScott Long if (txq->ift_task.gt_uniq != NULL) 51604c7070dbSScott Long taskqgroup_detach(tqg, &txq->ift_task); 51614c7070dbSScott Long } 51624c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 51634c7070dbSScott Long if (rxq->ifr_task.gt_uniq != NULL) 51644c7070dbSScott Long taskqgroup_detach(tqg, &rxq->ifr_task); 516587890dbaSSean Bruno 516687890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 516787890dbaSSean Bruno free(fl->ifl_rx_bitmap, M_IFLIB); 51684c7070dbSScott Long } 5169ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 51704c7070dbSScott Long if (ctx->ifc_admin_task.gt_uniq != NULL) 51714c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_admin_task); 51724c7070dbSScott Long if (ctx->ifc_vflr_task.gt_uniq != NULL) 51734c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 51746c3c3194SMatt Macy CTX_LOCK(ctx); 51754c7070dbSScott Long IFDI_DETACH(ctx); 51766c3c3194SMatt Macy CTX_UNLOCK(ctx); 51776c3c3194SMatt Macy 51786c3c3194SMatt Macy /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 517977c1fcecSEric Joyner iflib_free_intr_mem(ctx); 518077c1fcecSEric Joyner 518177c1fcecSEric Joyner bus_generic_detach(dev); 518277c1fcecSEric Joyner 518377c1fcecSEric Joyner iflib_tx_structures_free(ctx); 518477c1fcecSEric Joyner iflib_rx_structures_free(ctx); 518556614414SEric Joyner 518656614414SEric Joyner iflib_deregister(ctx); 518756614414SEric Joyner 518856614414SEric Joyner device_set_softc(ctx->ifc_dev, NULL); 518977c1fcecSEric Joyner if (ctx->ifc_flags & IFC_SC_ALLOCATED) 519077c1fcecSEric Joyner free(ctx->ifc_softc, M_IFLIB); 5191f154ece0SStephen Hurd unref_ctx_core_offset(ctx); 519277c1fcecSEric Joyner free(ctx, M_IFLIB); 519377c1fcecSEric Joyner return (0); 519477c1fcecSEric Joyner } 519577c1fcecSEric Joyner 519677c1fcecSEric Joyner static void 519777c1fcecSEric Joyner iflib_free_intr_mem(if_ctx_t ctx) 519877c1fcecSEric Joyner { 519977c1fcecSEric Joyner 52004c7070dbSScott Long if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) { 52014c7070dbSScott Long iflib_irq_free(ctx, &ctx->ifc_legacy_irq); 52024c7070dbSScott Long } 5203b97de13aSMarius Strobl if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) { 5204b97de13aSMarius Strobl pci_release_msi(ctx->ifc_dev); 5205b97de13aSMarius Strobl } 52064c7070dbSScott Long if (ctx->ifc_msix_mem != NULL) { 52074c7070dbSScott Long bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY, 5208b97de13aSMarius Strobl rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem); 52094c7070dbSScott Long ctx->ifc_msix_mem = NULL; 52104c7070dbSScott Long } 52114c7070dbSScott Long } 52124c7070dbSScott Long 52134c7070dbSScott Long int 52144c7070dbSScott Long iflib_device_detach(device_t dev) 52154c7070dbSScott Long { 52164c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52174c7070dbSScott Long 52184c7070dbSScott Long return (iflib_device_deregister(ctx)); 52194c7070dbSScott Long } 52204c7070dbSScott Long 52214c7070dbSScott Long int 52224c7070dbSScott Long iflib_device_suspend(device_t dev) 52234c7070dbSScott Long { 52244c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52254c7070dbSScott Long 52264c7070dbSScott Long CTX_LOCK(ctx); 52274c7070dbSScott Long IFDI_SUSPEND(ctx); 52284c7070dbSScott Long CTX_UNLOCK(ctx); 52294c7070dbSScott Long 52304c7070dbSScott Long return bus_generic_suspend(dev); 52314c7070dbSScott Long } 52324c7070dbSScott Long int 52334c7070dbSScott Long iflib_device_shutdown(device_t dev) 52344c7070dbSScott Long { 52354c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52364c7070dbSScott Long 52374c7070dbSScott Long CTX_LOCK(ctx); 52384c7070dbSScott Long IFDI_SHUTDOWN(ctx); 52394c7070dbSScott Long CTX_UNLOCK(ctx); 52404c7070dbSScott Long 52414c7070dbSScott Long return bus_generic_suspend(dev); 52424c7070dbSScott Long } 52434c7070dbSScott Long 52444c7070dbSScott Long 52454c7070dbSScott Long int 52464c7070dbSScott Long iflib_device_resume(device_t dev) 52474c7070dbSScott Long { 52484c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52494c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 52504c7070dbSScott Long 52514c7070dbSScott Long CTX_LOCK(ctx); 52524c7070dbSScott Long IFDI_RESUME(ctx); 5253cd28ea92SStephen Hurd iflib_if_init_locked(ctx); 52544c7070dbSScott Long CTX_UNLOCK(ctx); 52554c7070dbSScott Long for (int i = 0; i < NTXQSETS(ctx); i++, txq++) 52564c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 52574c7070dbSScott Long 52584c7070dbSScott Long return (bus_generic_resume(dev)); 52594c7070dbSScott Long } 52604c7070dbSScott Long 52614c7070dbSScott Long int 52624c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params) 52634c7070dbSScott Long { 52644c7070dbSScott Long int error; 52654c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52664c7070dbSScott Long 52674c7070dbSScott Long CTX_LOCK(ctx); 52684c7070dbSScott Long error = IFDI_IOV_INIT(ctx, num_vfs, params); 52694c7070dbSScott Long CTX_UNLOCK(ctx); 52704c7070dbSScott Long 52714c7070dbSScott Long return (error); 52724c7070dbSScott Long } 52734c7070dbSScott Long 52744c7070dbSScott Long void 52754c7070dbSScott Long iflib_device_iov_uninit(device_t dev) 52764c7070dbSScott Long { 52774c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52784c7070dbSScott Long 52794c7070dbSScott Long CTX_LOCK(ctx); 52804c7070dbSScott Long IFDI_IOV_UNINIT(ctx); 52814c7070dbSScott Long CTX_UNLOCK(ctx); 52824c7070dbSScott Long } 52834c7070dbSScott Long 52844c7070dbSScott Long int 52854c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params) 52864c7070dbSScott Long { 52874c7070dbSScott Long int error; 52884c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52894c7070dbSScott Long 52904c7070dbSScott Long CTX_LOCK(ctx); 52914c7070dbSScott Long error = IFDI_IOV_VF_ADD(ctx, vfnum, params); 52924c7070dbSScott Long CTX_UNLOCK(ctx); 52934c7070dbSScott Long 52944c7070dbSScott Long return (error); 52954c7070dbSScott Long } 52964c7070dbSScott Long 52974c7070dbSScott Long /********************************************************************* 52984c7070dbSScott Long * 52994c7070dbSScott Long * MODULE FUNCTION DEFINITIONS 53004c7070dbSScott Long * 53014c7070dbSScott Long **********************************************************************/ 53024c7070dbSScott Long 5303ab2e3f79SStephen Hurd /* 5304ab2e3f79SStephen Hurd * - Start a fast taskqueue thread for each core 5305ab2e3f79SStephen Hurd * - Start a taskqueue for control operations 5306ab2e3f79SStephen Hurd */ 53074c7070dbSScott Long static int 53084c7070dbSScott Long iflib_module_init(void) 53094c7070dbSScott Long { 53104c7070dbSScott Long return (0); 53114c7070dbSScott Long } 53124c7070dbSScott Long 53134c7070dbSScott Long static int 53144c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg) 53154c7070dbSScott Long { 53164c7070dbSScott Long int err; 53174c7070dbSScott Long 53184c7070dbSScott Long switch (what) { 53194c7070dbSScott Long case MOD_LOAD: 53204c7070dbSScott Long if ((err = iflib_module_init()) != 0) 53214c7070dbSScott Long return (err); 53224c7070dbSScott Long break; 53234c7070dbSScott Long case MOD_UNLOAD: 53244c7070dbSScott Long return (EBUSY); 53254c7070dbSScott Long default: 53264c7070dbSScott Long return (EOPNOTSUPP); 53274c7070dbSScott Long } 53284c7070dbSScott Long 53294c7070dbSScott Long return (0); 53304c7070dbSScott Long } 53314c7070dbSScott Long 53324c7070dbSScott Long /********************************************************************* 53334c7070dbSScott Long * 53344c7070dbSScott Long * PUBLIC FUNCTION DEFINITIONS 53354c7070dbSScott Long * ordered as in iflib.h 53364c7070dbSScott Long * 53374c7070dbSScott Long **********************************************************************/ 53384c7070dbSScott Long 53394c7070dbSScott Long 53404c7070dbSScott Long static void 53414c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx) 53424c7070dbSScott Long { 5343afb77372SEric Joyner int i; 5344afb77372SEric Joyner 53454c7070dbSScott Long MPASS(sctx->isc_tx_maxsize); 53464c7070dbSScott Long MPASS(sctx->isc_tx_maxsegsize); 53474c7070dbSScott Long 53484c7070dbSScott Long MPASS(sctx->isc_rx_maxsize); 53494c7070dbSScott Long MPASS(sctx->isc_rx_nsegments); 53504c7070dbSScott Long MPASS(sctx->isc_rx_maxsegsize); 53514c7070dbSScott Long 5352afb77372SEric Joyner MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8); 5353afb77372SEric Joyner for (i = 0; i < sctx->isc_nrxqs; i++) { 5354afb77372SEric Joyner MPASS(sctx->isc_nrxd_min[i]); 5355afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_min[i])); 5356afb77372SEric Joyner MPASS(sctx->isc_nrxd_max[i]); 5357afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_max[i])); 5358afb77372SEric Joyner MPASS(sctx->isc_nrxd_default[i]); 5359afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_default[i])); 5360afb77372SEric Joyner } 5361afb77372SEric Joyner 5362afb77372SEric Joyner MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8); 5363afb77372SEric Joyner for (i = 0; i < sctx->isc_ntxqs; i++) { 5364afb77372SEric Joyner MPASS(sctx->isc_ntxd_min[i]); 5365afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_min[i])); 5366afb77372SEric Joyner MPASS(sctx->isc_ntxd_max[i]); 5367afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_max[i])); 5368afb77372SEric Joyner MPASS(sctx->isc_ntxd_default[i]); 5369afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_default[i])); 5370afb77372SEric Joyner } 53714c7070dbSScott Long } 53724c7070dbSScott Long 53731248952aSSean Bruno static void 53741248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx) 53751248952aSSean Bruno { 53761248952aSSean Bruno 53771248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_encap); 53781248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_flush); 53791248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_credits_update); 53801248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_available); 53811248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_pkt_get); 53821248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_refill); 53831248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_flush); 53841248952aSSean Bruno } 53852fe66646SSean Bruno 53864c7070dbSScott Long static int 53874c7070dbSScott Long iflib_register(if_ctx_t ctx) 53884c7070dbSScott Long { 53894c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 53904c7070dbSScott Long driver_t *driver = sctx->isc_driver; 53914c7070dbSScott Long device_t dev = ctx->ifc_dev; 53924c7070dbSScott Long if_t ifp; 53939aeca213SMatt Macy u_char type; 53949aeca213SMatt Macy int iflags; 53954c7070dbSScott Long 53961f93e931SMatt Macy if ((sctx->isc_flags & IFLIB_PSEUDO) == 0) 53974c7070dbSScott Long _iflib_assert(sctx); 53984c7070dbSScott Long 5399aa8a24d3SStephen Hurd CTX_LOCK_INIT(ctx); 54007b610b60SSean Bruno STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev)); 54019aeca213SMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO) { 54029aeca213SMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO_ETHER) 54039aeca213SMatt Macy type = IFT_ETHER; 54049aeca213SMatt Macy else 54059aeca213SMatt Macy type = IFT_PPP; 54069aeca213SMatt Macy } else 54079aeca213SMatt Macy type = IFT_ETHER; 54089aeca213SMatt Macy ifp = ctx->ifc_ifp = if_alloc(type); 54094c7070dbSScott Long if (ifp == NULL) { 54104c7070dbSScott Long device_printf(dev, "can not allocate ifnet structure\n"); 54114c7070dbSScott Long return (ENOMEM); 54124c7070dbSScott Long } 54134c7070dbSScott Long 54144c7070dbSScott Long /* 54154c7070dbSScott Long * Initialize our context's device specific methods 54164c7070dbSScott Long */ 54174c7070dbSScott Long kobj_init((kobj_t) ctx, (kobj_class_t) driver); 54184c7070dbSScott Long kobj_class_compile((kobj_class_t) driver); 54194c7070dbSScott Long 54204c7070dbSScott Long if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 54214c7070dbSScott Long if_setsoftc(ifp, ctx); 54224c7070dbSScott Long if_setdev(ifp, dev); 54234c7070dbSScott Long if_setinitfn(ifp, iflib_if_init); 54244c7070dbSScott Long if_setioctlfn(ifp, iflib_if_ioctl); 5425b8ca4756SPatrick Kelsey #ifdef ALTQ 5426b8ca4756SPatrick Kelsey if_setstartfn(ifp, iflib_altq_if_start); 5427b8ca4756SPatrick Kelsey if_settransmitfn(ifp, iflib_altq_if_transmit); 54288f410865SPatrick Kelsey if_setsendqready(ifp); 5429b8ca4756SPatrick Kelsey #else 54304c7070dbSScott Long if_settransmitfn(ifp, iflib_if_transmit); 5431b8ca4756SPatrick Kelsey #endif 54324c7070dbSScott Long if_setqflushfn(ifp, iflib_if_qflush); 54339aeca213SMatt Macy iflags = IFF_MULTICAST | IFF_KNOWSEPOCH; 54344c7070dbSScott Long 54359aeca213SMatt Macy if ((sctx->isc_flags & IFLIB_PSEUDO) && 54369aeca213SMatt Macy (sctx->isc_flags & IFLIB_PSEUDO_ETHER) == 0) 54379aeca213SMatt Macy iflags |= IFF_POINTOPOINT; 54389aeca213SMatt Macy else 54399aeca213SMatt Macy iflags |= IFF_BROADCAST | IFF_SIMPLEX; 54409aeca213SMatt Macy if_setflags(ifp, iflags); 54414c7070dbSScott Long ctx->ifc_vlan_attach_event = 54424c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx, 54434c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 54444c7070dbSScott Long ctx->ifc_vlan_detach_event = 54454c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx, 54464c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 54474c7070dbSScott Long 5448e2621d96SMatt Macy if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) { 5449e2621d96SMatt Macy ctx->ifc_mediap = &ctx->ifc_media; 5450e2621d96SMatt Macy ifmedia_init(ctx->ifc_mediap, IFM_IMASK, 54514c7070dbSScott Long iflib_media_change, iflib_media_status); 5452e2621d96SMatt Macy } 54534c7070dbSScott Long return (0); 54544c7070dbSScott Long } 54554c7070dbSScott Long 545656614414SEric Joyner static void 54571558015eSEric Joyner iflib_unregister_vlan_handlers(if_ctx_t ctx) 545856614414SEric Joyner { 545956614414SEric Joyner /* Unregister VLAN events */ 546056614414SEric Joyner if (ctx->ifc_vlan_attach_event != NULL) { 546156614414SEric Joyner EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 546256614414SEric Joyner ctx->ifc_vlan_attach_event = NULL; 546356614414SEric Joyner } 546456614414SEric Joyner if (ctx->ifc_vlan_detach_event != NULL) { 546556614414SEric Joyner EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 546656614414SEric Joyner ctx->ifc_vlan_detach_event = NULL; 546756614414SEric Joyner } 546856614414SEric Joyner 54691558015eSEric Joyner } 54701558015eSEric Joyner 54711558015eSEric Joyner static void 54721558015eSEric Joyner iflib_deregister(if_ctx_t ctx) 54731558015eSEric Joyner { 54741558015eSEric Joyner if_t ifp = ctx->ifc_ifp; 54751558015eSEric Joyner 54761558015eSEric Joyner /* Remove all media */ 54771558015eSEric Joyner ifmedia_removeall(&ctx->ifc_media); 54781558015eSEric Joyner 54791558015eSEric Joyner /* Ensure that VLAN event handlers are unregistered */ 54801558015eSEric Joyner iflib_unregister_vlan_handlers(ctx); 54811558015eSEric Joyner 548256614414SEric Joyner /* Release kobject reference */ 548356614414SEric Joyner kobj_delete((kobj_t) ctx, NULL); 548456614414SEric Joyner 548556614414SEric Joyner /* Free the ifnet structure */ 548656614414SEric Joyner if_free(ifp); 548756614414SEric Joyner 548856614414SEric Joyner STATE_LOCK_DESTROY(ctx); 548956614414SEric Joyner 549056614414SEric Joyner /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 549156614414SEric Joyner CTX_LOCK_DESTROY(ctx); 549256614414SEric Joyner } 549356614414SEric Joyner 54944c7070dbSScott Long static int 54954c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx) 54964c7070dbSScott Long { 54974c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 549823ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 54994c7070dbSScott Long device_t dev = ctx->ifc_dev; 550023ac9029SStephen Hurd int nrxqsets = scctx->isc_nrxqsets; 550123ac9029SStephen Hurd int ntxqsets = scctx->isc_ntxqsets; 55024c7070dbSScott Long iflib_txq_t txq; 55034c7070dbSScott Long iflib_rxq_t rxq; 55044c7070dbSScott Long iflib_fl_t fl = NULL; 550523ac9029SStephen Hurd int i, j, cpu, err, txconf, rxconf; 55064c7070dbSScott Long iflib_dma_info_t ifdip; 550723ac9029SStephen Hurd uint32_t *rxqsizes = scctx->isc_rxqsizes; 550823ac9029SStephen Hurd uint32_t *txqsizes = scctx->isc_txqsizes; 55094c7070dbSScott Long uint8_t nrxqs = sctx->isc_nrxqs; 55104c7070dbSScott Long uint8_t ntxqs = sctx->isc_ntxqs; 55114c7070dbSScott Long int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1; 55124c7070dbSScott Long caddr_t *vaddrs; 55134c7070dbSScott Long uint64_t *paddrs; 55144c7070dbSScott Long 551523ac9029SStephen Hurd KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1")); 551623ac9029SStephen Hurd KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1")); 55174c7070dbSScott Long 55184c7070dbSScott Long /* Allocate the TX ring struct memory */ 5519b89827a0SStephen Hurd if (!(ctx->ifc_txqs = 5520ac2fffa4SPedro F. Giffuni (iflib_txq_t) malloc(sizeof(struct iflib_txq) * 5521ac2fffa4SPedro F. Giffuni ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 55224c7070dbSScott Long device_printf(dev, "Unable to allocate TX ring memory\n"); 55234c7070dbSScott Long err = ENOMEM; 55244c7070dbSScott Long goto fail; 55254c7070dbSScott Long } 55264c7070dbSScott Long 55274c7070dbSScott Long /* Now allocate the RX */ 5528b89827a0SStephen Hurd if (!(ctx->ifc_rxqs = 5529ac2fffa4SPedro F. Giffuni (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) * 5530ac2fffa4SPedro F. Giffuni nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 55314c7070dbSScott Long device_printf(dev, "Unable to allocate RX ring memory\n"); 55324c7070dbSScott Long err = ENOMEM; 55334c7070dbSScott Long goto rx_fail; 55344c7070dbSScott Long } 55354c7070dbSScott Long 5536b89827a0SStephen Hurd txq = ctx->ifc_txqs; 5537b89827a0SStephen Hurd rxq = ctx->ifc_rxqs; 55384c7070dbSScott Long 55394c7070dbSScott Long /* 55404c7070dbSScott Long * XXX handle allocation failure 55414c7070dbSScott Long */ 554296c85efbSNathan Whitehorn for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) { 55434c7070dbSScott Long /* Set up some basics */ 55444c7070dbSScott Long 5545bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, 5546bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5547bfce461eSMarius Strobl device_printf(dev, 5548bfce461eSMarius Strobl "Unable to allocate TX DMA info memory\n"); 55494c7070dbSScott Long err = ENOMEM; 55500d0338afSConrad Meyer goto err_tx_desc; 55514c7070dbSScott Long } 55524c7070dbSScott Long txq->ift_ifdi = ifdip; 55534c7070dbSScott Long for (j = 0; j < ntxqs; j++, ifdip++) { 5554bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) { 5555bfce461eSMarius Strobl device_printf(dev, 5556bfce461eSMarius Strobl "Unable to allocate TX descriptors\n"); 55574c7070dbSScott Long err = ENOMEM; 55584c7070dbSScott Long goto err_tx_desc; 55594c7070dbSScott Long } 556095246abbSSean Bruno txq->ift_txd_size[j] = scctx->isc_txd_size[j]; 55614c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, txqsizes[j]); 55624c7070dbSScott Long } 55634c7070dbSScott Long txq->ift_ctx = ctx; 55644c7070dbSScott Long txq->ift_id = i; 556523ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_TXCQ) { 556623ac9029SStephen Hurd txq->ift_br_offset = 1; 556723ac9029SStephen Hurd } else { 556823ac9029SStephen Hurd txq->ift_br_offset = 0; 556923ac9029SStephen Hurd } 55704c7070dbSScott Long /* XXX fix this */ 557196c85efbSNathan Whitehorn txq->ift_timer.c_cpu = cpu; 55724c7070dbSScott Long 55734c7070dbSScott Long if (iflib_txsd_alloc(txq)) { 55744c7070dbSScott Long device_printf(dev, "Critical Failure setting up TX buffers\n"); 55754c7070dbSScott Long err = ENOMEM; 55764c7070dbSScott Long goto err_tx_desc; 55774c7070dbSScott Long } 55784c7070dbSScott Long 55794c7070dbSScott Long /* Initialize the TX lock */ 55801722eeacSMarius Strobl snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout", 55814c7070dbSScott Long device_get_nameunit(dev), txq->ift_id); 55824c7070dbSScott Long mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF); 55834c7070dbSScott Long callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0); 55844c7070dbSScott Long 558595246abbSSean Bruno err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain, 55864c7070dbSScott Long iflib_txq_can_drain, M_IFLIB, M_WAITOK); 55874c7070dbSScott Long if (err) { 55884c7070dbSScott Long /* XXX free any allocated rings */ 55894c7070dbSScott Long device_printf(dev, "Unable to allocate buf_ring\n"); 55900d0338afSConrad Meyer goto err_tx_desc; 55914c7070dbSScott Long } 55924c7070dbSScott Long } 55934c7070dbSScott Long 55944c7070dbSScott Long for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) { 55954c7070dbSScott Long /* Set up some basics */ 5596fb1a29b4SHans Petter Selasky callout_init(&rxq->ifr_watchdog, 1); 55974c7070dbSScott Long 5598bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, 5599bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5600bfce461eSMarius Strobl device_printf(dev, 5601bfce461eSMarius Strobl "Unable to allocate RX DMA info memory\n"); 56024c7070dbSScott Long err = ENOMEM; 56030d0338afSConrad Meyer goto err_tx_desc; 56044c7070dbSScott Long } 56054c7070dbSScott Long 56064c7070dbSScott Long rxq->ifr_ifdi = ifdip; 560795246abbSSean Bruno /* XXX this needs to be changed if #rx queues != #tx queues */ 560895246abbSSean Bruno rxq->ifr_ntxqirq = 1; 560995246abbSSean Bruno rxq->ifr_txqid[0] = i; 56104c7070dbSScott Long for (j = 0; j < nrxqs; j++, ifdip++) { 5611bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) { 5612bfce461eSMarius Strobl device_printf(dev, 5613bfce461eSMarius Strobl "Unable to allocate RX descriptors\n"); 56144c7070dbSScott Long err = ENOMEM; 56154c7070dbSScott Long goto err_tx_desc; 56164c7070dbSScott Long } 56174c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, rxqsizes[j]); 56184c7070dbSScott Long } 56194c7070dbSScott Long rxq->ifr_ctx = ctx; 56204c7070dbSScott Long rxq->ifr_id = i; 562123ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 562223ac9029SStephen Hurd rxq->ifr_fl_offset = 1; 56234c7070dbSScott Long } else { 562423ac9029SStephen Hurd rxq->ifr_fl_offset = 0; 56254c7070dbSScott Long } 56264c7070dbSScott Long rxq->ifr_nfl = nfree_lists; 56274c7070dbSScott Long if (!(fl = 5628ac2fffa4SPedro F. Giffuni (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) { 56294c7070dbSScott Long device_printf(dev, "Unable to allocate free list memory\n"); 56304c7070dbSScott Long err = ENOMEM; 56310d0338afSConrad Meyer goto err_tx_desc; 56324c7070dbSScott Long } 56334c7070dbSScott Long rxq->ifr_fl = fl; 56344c7070dbSScott Long for (j = 0; j < nfree_lists; j++) { 563595246abbSSean Bruno fl[j].ifl_rxq = rxq; 563695246abbSSean Bruno fl[j].ifl_id = j; 563795246abbSSean Bruno fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset]; 563895246abbSSean Bruno fl[j].ifl_rxd_size = scctx->isc_rxd_size[j]; 56394c7070dbSScott Long } 56404c7070dbSScott Long /* Allocate receive buffers for the ring */ 56414c7070dbSScott Long if (iflib_rxsd_alloc(rxq)) { 56424c7070dbSScott Long device_printf(dev, 56434c7070dbSScott Long "Critical Failure setting up receive buffers\n"); 56444c7070dbSScott Long err = ENOMEM; 56454c7070dbSScott Long goto err_rx_desc; 56464c7070dbSScott Long } 564787890dbaSSean Bruno 564887890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 56493db348b5SMarius Strobl fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, 56503db348b5SMarius Strobl M_WAITOK); 56514c7070dbSScott Long } 56524c7070dbSScott Long 56534c7070dbSScott Long /* TXQs */ 56544c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 56554c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 56564c7070dbSScott Long for (i = 0; i < ntxqsets; i++) { 56574c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi; 56584c7070dbSScott Long 56594c7070dbSScott Long for (j = 0; j < ntxqs; j++, di++) { 56604c7070dbSScott Long vaddrs[i*ntxqs + j] = di->idi_vaddr; 56614c7070dbSScott Long paddrs[i*ntxqs + j] = di->idi_paddr; 56624c7070dbSScott Long } 56634c7070dbSScott Long } 56644c7070dbSScott Long if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) { 5665bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5666bfce461eSMarius Strobl "Unable to allocate device TX queue\n"); 56674c7070dbSScott Long iflib_tx_structures_free(ctx); 56684c7070dbSScott Long free(vaddrs, M_IFLIB); 56694c7070dbSScott Long free(paddrs, M_IFLIB); 56704c7070dbSScott Long goto err_rx_desc; 56714c7070dbSScott Long } 56724c7070dbSScott Long free(vaddrs, M_IFLIB); 56734c7070dbSScott Long free(paddrs, M_IFLIB); 56744c7070dbSScott Long 56754c7070dbSScott Long /* RXQs */ 56764c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 56774c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 56784c7070dbSScott Long for (i = 0; i < nrxqsets; i++) { 56794c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi; 56804c7070dbSScott Long 56814c7070dbSScott Long for (j = 0; j < nrxqs; j++, di++) { 56824c7070dbSScott Long vaddrs[i*nrxqs + j] = di->idi_vaddr; 56834c7070dbSScott Long paddrs[i*nrxqs + j] = di->idi_paddr; 56844c7070dbSScott Long } 56854c7070dbSScott Long } 56864c7070dbSScott Long if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) { 5687bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5688bfce461eSMarius Strobl "Unable to allocate device RX queue\n"); 56894c7070dbSScott Long iflib_tx_structures_free(ctx); 56904c7070dbSScott Long free(vaddrs, M_IFLIB); 56914c7070dbSScott Long free(paddrs, M_IFLIB); 56924c7070dbSScott Long goto err_rx_desc; 56934c7070dbSScott Long } 56944c7070dbSScott Long free(vaddrs, M_IFLIB); 56954c7070dbSScott Long free(paddrs, M_IFLIB); 56964c7070dbSScott Long 56974c7070dbSScott Long return (0); 56984c7070dbSScott Long 56994c7070dbSScott Long /* XXX handle allocation failure changes */ 57004c7070dbSScott Long err_rx_desc: 57014c7070dbSScott Long err_tx_desc: 5702b89827a0SStephen Hurd rx_fail: 57034c7070dbSScott Long if (ctx->ifc_rxqs != NULL) 57044c7070dbSScott Long free(ctx->ifc_rxqs, M_IFLIB); 57054c7070dbSScott Long ctx->ifc_rxqs = NULL; 57064c7070dbSScott Long if (ctx->ifc_txqs != NULL) 57074c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 57084c7070dbSScott Long ctx->ifc_txqs = NULL; 57094c7070dbSScott Long fail: 57104c7070dbSScott Long return (err); 57114c7070dbSScott Long } 57124c7070dbSScott Long 57134c7070dbSScott Long static int 57144c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx) 57154c7070dbSScott Long { 57164c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 57174c7070dbSScott Long int i; 57184c7070dbSScott Long 57194c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 57204c7070dbSScott Long iflib_txq_setup(txq); 57214c7070dbSScott Long 57224c7070dbSScott Long return (0); 57234c7070dbSScott Long } 57244c7070dbSScott Long 57254c7070dbSScott Long static void 57264c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx) 57274c7070dbSScott Long { 57284c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 57294d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 57304c7070dbSScott Long int i, j; 57314c7070dbSScott Long 57324c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 57334d261ce2SStephen Hurd for (j = 0; j < sctx->isc_ntxqs; j++) 57344c7070dbSScott Long iflib_dma_free(&txq->ift_ifdi[j]); 5735244e7cffSEric Joyner iflib_txq_destroy(txq); 57364c7070dbSScott Long } 57374c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 57384c7070dbSScott Long ctx->ifc_txqs = NULL; 57394c7070dbSScott Long IFDI_QUEUES_FREE(ctx); 57404c7070dbSScott Long } 57414c7070dbSScott Long 57424c7070dbSScott Long /********************************************************************* 57434c7070dbSScott Long * 57444c7070dbSScott Long * Initialize all receive rings. 57454c7070dbSScott Long * 57464c7070dbSScott Long **********************************************************************/ 57474c7070dbSScott Long static int 57484c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx) 57494c7070dbSScott Long { 57504c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 5751aaeb188aSBjoern A. Zeeb int q; 5752aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 57533d10e9edSMarius Strobl int err, i; 5754aaeb188aSBjoern A. Zeeb #endif 57554c7070dbSScott Long 57564c7070dbSScott Long for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) { 5757aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 57583d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) { 57593d10e9edSMarius Strobl err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp, 576023ac9029SStephen Hurd TCP_LRO_ENTRIES, min(1024, 57613d10e9edSMarius Strobl ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset])); 57623d10e9edSMarius Strobl if (err != 0) { 57633d10e9edSMarius Strobl device_printf(ctx->ifc_dev, 57643d10e9edSMarius Strobl "LRO Initialization failed!\n"); 57654c7070dbSScott Long goto fail; 57664c7070dbSScott Long } 57673d10e9edSMarius Strobl } 5768aaeb188aSBjoern A. Zeeb #endif 57694c7070dbSScott Long IFDI_RXQ_SETUP(ctx, rxq->ifr_id); 57704c7070dbSScott Long } 57714c7070dbSScott Long return (0); 5772aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 57734c7070dbSScott Long fail: 57744c7070dbSScott Long /* 57753d10e9edSMarius Strobl * Free LRO resources allocated so far, we will only handle 57764c7070dbSScott Long * the rings that completed, the failing case will have 57774c7070dbSScott Long * cleaned up for itself. 'q' failed, so its the terminus. 57784c7070dbSScott Long */ 57794c7070dbSScott Long rxq = ctx->ifc_rxqs; 57804c7070dbSScott Long for (i = 0; i < q; ++i, rxq++) { 57813d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) 57823d10e9edSMarius Strobl tcp_lro_free(&rxq->ifr_lc); 57834c7070dbSScott Long } 57844c7070dbSScott Long return (err); 5785aaeb188aSBjoern A. Zeeb #endif 57864c7070dbSScott Long } 57874c7070dbSScott Long 57884c7070dbSScott Long /********************************************************************* 57894c7070dbSScott Long * 57904c7070dbSScott Long * Free all receive rings. 57914c7070dbSScott Long * 57924c7070dbSScott Long **********************************************************************/ 57934c7070dbSScott Long static void 57944c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx) 57954c7070dbSScott Long { 57964c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 5797db8e8f1eSEric Joyner if_shared_ctx_t sctx = ctx->ifc_sctx; 5798db8e8f1eSEric Joyner int i, j; 57994c7070dbSScott Long 58003d10e9edSMarius Strobl for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) { 5801db8e8f1eSEric Joyner for (j = 0; j < sctx->isc_nrxqs; j++) 5802db8e8f1eSEric Joyner iflib_dma_free(&rxq->ifr_ifdi[j]); 58034c7070dbSScott Long iflib_rx_sds_free(rxq); 5804007b804fSMarius Strobl #if defined(INET6) || defined(INET) 58053d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) 58063d10e9edSMarius Strobl tcp_lro_free(&rxq->ifr_lc); 5807007b804fSMarius Strobl #endif 58084c7070dbSScott Long } 580977c1fcecSEric Joyner free(ctx->ifc_rxqs, M_IFLIB); 581077c1fcecSEric Joyner ctx->ifc_rxqs = NULL; 58114c7070dbSScott Long } 58124c7070dbSScott Long 58134c7070dbSScott Long static int 58144c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx) 58154c7070dbSScott Long { 58164c7070dbSScott Long int err; 58174c7070dbSScott Long 58186108c013SStephen Hurd /* 58196108c013SStephen Hurd * It is expected that the caller takes care of freeing queues if this 58206108c013SStephen Hurd * fails. 58216108c013SStephen Hurd */ 5822ac88e6daSStephen Hurd if ((err = iflib_tx_structures_setup(ctx)) != 0) { 5823ac88e6daSStephen Hurd device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err); 58244c7070dbSScott Long return (err); 5825ac88e6daSStephen Hurd } 58264c7070dbSScott Long 58276108c013SStephen Hurd if ((err = iflib_rx_structures_setup(ctx)) != 0) 58284c7070dbSScott Long device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err); 58296108c013SStephen Hurd 58304c7070dbSScott Long return (err); 58314c7070dbSScott Long } 58324c7070dbSScott Long 58334c7070dbSScott Long int 58344c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 58353e0e6330SStephen Hurd driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name) 58364c7070dbSScott Long { 58374c7070dbSScott Long 58384c7070dbSScott Long return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name)); 58394c7070dbSScott Long } 58404c7070dbSScott Long 5841b103855eSStephen Hurd #ifdef SMP 5842aa3c5dd8SSean Bruno static int 5843b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid) 58444c7070dbSScott Long { 5845b103855eSStephen Hurd cpuset_t cpus; 5846aa3c5dd8SSean Bruno int i, cpuid, eqid, count; 58474c7070dbSScott Long 5848b103855eSStephen Hurd CPU_COPY(&ctx->ifc_cpus, &cpus); 5849b103855eSStephen Hurd count = CPU_COUNT(&cpus); 5850aa3c5dd8SSean Bruno eqid = qid % count; 58514c7070dbSScott Long /* clear up to the qid'th bit */ 5852aa3c5dd8SSean Bruno for (i = 0; i < eqid; i++) { 5853b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5854aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5855b103855eSStephen Hurd CPU_CLR(cpuid-1, &cpus); 58564c7070dbSScott Long } 5857b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5858aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5859aa3c5dd8SSean Bruno return (cpuid-1); 58604c7070dbSScott Long } 58614c7070dbSScott Long 5862b103855eSStephen Hurd #ifdef SCHED_ULE 5863b103855eSStephen Hurd extern struct cpu_group *cpu_top; /* CPU topology */ 5864b103855eSStephen Hurd 5865b103855eSStephen Hurd static int 5866b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp) 5867b103855eSStephen Hurd { 5868b103855eSStephen Hurd int i; 5869b103855eSStephen Hurd 5870b103855eSStephen Hurd if (grp->cg_children == 0) 5871b103855eSStephen Hurd return -1; 5872b103855eSStephen Hurd 5873b103855eSStephen Hurd MPASS(grp->cg_child); 5874b103855eSStephen Hurd for (i = 0; i < grp->cg_children; i++) { 5875b103855eSStephen Hurd if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask)) 5876b103855eSStephen Hurd return i; 5877b103855eSStephen Hurd } 5878b103855eSStephen Hurd 5879b103855eSStephen Hurd return -1; 5880b103855eSStephen Hurd } 5881b103855eSStephen Hurd 5882b103855eSStephen Hurd /* 58830b75ac77SStephen Hurd * Find the nth "close" core to the specified core 58840b75ac77SStephen Hurd * "close" is defined as the deepest level that shares 58850b75ac77SStephen Hurd * at least an L2 cache. With threads, this will be 5886f154ece0SStephen Hurd * threads on the same core. If the shared cache is L3 58870b75ac77SStephen Hurd * or higher, simply returns the same core. 5888b103855eSStephen Hurd */ 5889b103855eSStephen Hurd static int 58900b75ac77SStephen Hurd find_close_core(int cpu, int core_offset) 5891b103855eSStephen Hurd { 5892b103855eSStephen Hurd struct cpu_group *grp; 5893b103855eSStephen Hurd int i; 58940b75ac77SStephen Hurd int fcpu; 5895b103855eSStephen Hurd cpuset_t cs; 5896b103855eSStephen Hurd 5897b103855eSStephen Hurd grp = cpu_top; 5898b103855eSStephen Hurd if (grp == NULL) 5899b103855eSStephen Hurd return cpu; 5900b103855eSStephen Hurd i = 0; 5901b103855eSStephen Hurd while ((i = find_child_with_core(cpu, grp)) != -1) { 5902b103855eSStephen Hurd /* If the child only has one cpu, don't descend */ 5903b103855eSStephen Hurd if (grp->cg_child[i].cg_count <= 1) 5904b103855eSStephen Hurd break; 5905b103855eSStephen Hurd grp = &grp->cg_child[i]; 5906b103855eSStephen Hurd } 5907b103855eSStephen Hurd 5908b103855eSStephen Hurd /* If they don't share at least an L2 cache, use the same CPU */ 5909b103855eSStephen Hurd if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE) 5910b103855eSStephen Hurd return cpu; 5911b103855eSStephen Hurd 5912b103855eSStephen Hurd /* Now pick one */ 5913b103855eSStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 59140b75ac77SStephen Hurd 59150b75ac77SStephen Hurd /* Add the selected CPU offset to core offset. */ 59160b75ac77SStephen Hurd for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) { 59170b75ac77SStephen Hurd if (fcpu - 1 == cpu) 59180b75ac77SStephen Hurd break; 59190b75ac77SStephen Hurd CPU_CLR(fcpu - 1, &cs); 59200b75ac77SStephen Hurd } 59210b75ac77SStephen Hurd MPASS(fcpu); 59220b75ac77SStephen Hurd 59230b75ac77SStephen Hurd core_offset += i; 59240b75ac77SStephen Hurd 59250b75ac77SStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 59260b75ac77SStephen Hurd for (i = core_offset % grp->cg_count; i > 0; i--) { 5927b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5928b103855eSStephen Hurd CPU_CLR(CPU_FFS(&cs) - 1, &cs); 5929b103855eSStephen Hurd } 5930b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5931b103855eSStephen Hurd return CPU_FFS(&cs) - 1; 5932b103855eSStephen Hurd } 5933b103855eSStephen Hurd #else 5934b103855eSStephen Hurd static int 59350b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused) 5936b103855eSStephen Hurd { 593797755e83SKonstantin Belousov return cpu; 5938b103855eSStephen Hurd } 5939b103855eSStephen Hurd #endif 5940b103855eSStephen Hurd 5941b103855eSStephen Hurd static int 59420b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid) 5943b103855eSStephen Hurd { 5944b103855eSStephen Hurd switch (type) { 5945b103855eSStephen Hurd case IFLIB_INTR_TX: 59460b75ac77SStephen Hurd /* TX queues get cores which share at least an L2 cache with the corresponding RX queue */ 59470b75ac77SStephen Hurd /* XXX handle multiple RX threads per core and more than two core per L2 group */ 5948b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus) + 1; 5949b103855eSStephen Hurd case IFLIB_INTR_RX: 5950b103855eSStephen Hurd case IFLIB_INTR_RXTX: 59510b75ac77SStephen Hurd /* RX queues get the specified core */ 5952b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus); 5953b103855eSStephen Hurd default: 5954b103855eSStephen Hurd return -1; 5955b103855eSStephen Hurd } 5956b103855eSStephen Hurd } 5957b103855eSStephen Hurd #else 59580b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid) CPU_FIRST() 59590b75ac77SStephen Hurd #define find_close_core(cpuid, tid) CPU_FIRST() 5960b103855eSStephen Hurd #define find_nth(ctx, gid) CPU_FIRST() 5961b103855eSStephen Hurd #endif 5962b103855eSStephen Hurd 5963b103855eSStephen Hurd /* Just to avoid copy/paste */ 5964b103855eSStephen Hurd static inline int 5965f855ec81SMarius Strobl iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, 5966f855ec81SMarius Strobl int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, 5967f855ec81SMarius Strobl const char *name) 5968b103855eSStephen Hurd { 5969f855ec81SMarius Strobl device_t dev; 5970f154ece0SStephen Hurd int co, cpuid, err, tid; 5971b103855eSStephen Hurd 5972f855ec81SMarius Strobl dev = ctx->ifc_dev; 5973f154ece0SStephen Hurd co = ctx->ifc_sysctl_core_offset; 5974f154ece0SStephen Hurd if (ctx->ifc_sysctl_separate_txrx && type == IFLIB_INTR_TX) 5975f154ece0SStephen Hurd co += ctx->ifc_softc_ctx.isc_nrxqsets; 5976f154ece0SStephen Hurd cpuid = find_nth(ctx, qid + co); 59770b75ac77SStephen Hurd tid = get_core_offset(ctx, type, qid); 59783d10e9edSMarius Strobl if (tid < 0) { 59793d10e9edSMarius Strobl device_printf(dev, "get_core_offset failed\n"); 59803d10e9edSMarius Strobl return (EOPNOTSUPP); 59813d10e9edSMarius Strobl } 59820b75ac77SStephen Hurd cpuid = find_close_core(cpuid, tid); 5983f855ec81SMarius Strobl err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, irq->ii_res, 5984f855ec81SMarius Strobl name); 5985b103855eSStephen Hurd if (err) { 5986f855ec81SMarius Strobl device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err); 5987b103855eSStephen Hurd return (err); 5988b103855eSStephen Hurd } 5989b103855eSStephen Hurd #ifdef notyet 5990b103855eSStephen Hurd if (cpuid > ctx->ifc_cpuid_highest) 5991b103855eSStephen Hurd ctx->ifc_cpuid_highest = cpuid; 5992b103855eSStephen Hurd #endif 59933d10e9edSMarius Strobl return (0); 5994b103855eSStephen Hurd } 5995b103855eSStephen Hurd 59964c7070dbSScott Long int 59974c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid, 59984c7070dbSScott Long iflib_intr_type_t type, driver_filter_t *filter, 59993e0e6330SStephen Hurd void *filter_arg, int qid, const char *name) 60004c7070dbSScott Long { 6001f855ec81SMarius Strobl device_t dev; 60024c7070dbSScott Long struct grouptask *gtask; 60034c7070dbSScott Long struct taskqgroup *tqg; 60044c7070dbSScott Long iflib_filter_info_t info; 600523ac9029SStephen Hurd gtask_fn_t *fn; 6006b103855eSStephen Hurd int tqrid, err; 600795246abbSSean Bruno driver_filter_t *intr_fast; 60084c7070dbSScott Long void *q; 60094c7070dbSScott Long 60104c7070dbSScott Long info = &ctx->ifc_filter_info; 6011add6f7d0SSean Bruno tqrid = rid; 60124c7070dbSScott Long 60134c7070dbSScott Long switch (type) { 60144c7070dbSScott Long /* XXX merge tx/rx for netmap? */ 60154c7070dbSScott Long case IFLIB_INTR_TX: 60164c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 60174c7070dbSScott Long info = &ctx->ifc_txqs[qid].ift_filter_info; 60184c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 6019ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 60204c7070dbSScott Long fn = _task_fn_tx; 602195246abbSSean Bruno intr_fast = iflib_fast_intr; 6022da69b8f9SSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 60235ee36c68SStephen Hurd ctx->ifc_flags |= IFC_NETMAP_TX_IRQ; 60244c7070dbSScott Long break; 60254c7070dbSScott Long case IFLIB_INTR_RX: 60264c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 60274c7070dbSScott Long info = &ctx->ifc_rxqs[qid].ifr_filter_info; 60284c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 6029ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 60304c7070dbSScott Long fn = _task_fn_rx; 6031ab2e3f79SStephen Hurd intr_fast = iflib_fast_intr; 60326c3e93cbSGleb Smirnoff NET_GROUPTASK_INIT(gtask, 0, fn, q); 603395246abbSSean Bruno break; 603495246abbSSean Bruno case IFLIB_INTR_RXTX: 603595246abbSSean Bruno q = &ctx->ifc_rxqs[qid]; 603695246abbSSean Bruno info = &ctx->ifc_rxqs[qid].ifr_filter_info; 603795246abbSSean Bruno gtask = &ctx->ifc_rxqs[qid].ifr_task; 6038ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 603995246abbSSean Bruno fn = _task_fn_rx; 604095246abbSSean Bruno intr_fast = iflib_fast_intr_rxtx; 60416c3e93cbSGleb Smirnoff NET_GROUPTASK_INIT(gtask, 0, fn, q); 60424c7070dbSScott Long break; 60434c7070dbSScott Long case IFLIB_INTR_ADMIN: 60444c7070dbSScott Long q = ctx; 6045da69b8f9SSean Bruno tqrid = -1; 60464c7070dbSScott Long info = &ctx->ifc_filter_info; 60474c7070dbSScott Long gtask = &ctx->ifc_admin_task; 6048ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 60494c7070dbSScott Long fn = _task_fn_admin; 605095246abbSSean Bruno intr_fast = iflib_fast_intr_ctx; 60514c7070dbSScott Long break; 60524c7070dbSScott Long default: 60533d10e9edSMarius Strobl device_printf(ctx->ifc_dev, "%s: unknown net intr type\n", 60543d10e9edSMarius Strobl __func__); 60553d10e9edSMarius Strobl return (EINVAL); 60564c7070dbSScott Long } 60574c7070dbSScott Long 60584c7070dbSScott Long info->ifi_filter = filter; 60594c7070dbSScott Long info->ifi_filter_arg = filter_arg; 60604c7070dbSScott Long info->ifi_task = gtask; 606195246abbSSean Bruno info->ifi_ctx = q; 60624c7070dbSScott Long 6063f855ec81SMarius Strobl dev = ctx->ifc_dev; 606495246abbSSean Bruno err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name); 6065da69b8f9SSean Bruno if (err != 0) { 6066f855ec81SMarius Strobl device_printf(dev, "_iflib_irq_alloc failed %d\n", err); 60674c7070dbSScott Long return (err); 6068da69b8f9SSean Bruno } 6069da69b8f9SSean Bruno if (type == IFLIB_INTR_ADMIN) 6070da69b8f9SSean Bruno return (0); 6071da69b8f9SSean Bruno 60724c7070dbSScott Long if (tqrid != -1) { 6073f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 6074f855ec81SMarius Strobl q, name); 6075b103855eSStephen Hurd if (err) 6076b103855eSStephen Hurd return (err); 6077aa3c5dd8SSean Bruno } else { 6078f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name); 6079aa3c5dd8SSean Bruno } 60804c7070dbSScott Long 60814c7070dbSScott Long return (0); 60824c7070dbSScott Long } 60834c7070dbSScott Long 60844c7070dbSScott Long void 60853e0e6330SStephen Hurd iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, void *arg, int qid, const char *name) 60864c7070dbSScott Long { 60874c7070dbSScott Long struct grouptask *gtask; 60884c7070dbSScott Long struct taskqgroup *tqg; 608923ac9029SStephen Hurd gtask_fn_t *fn; 60904c7070dbSScott Long void *q; 6091b103855eSStephen Hurd int err; 60924c7070dbSScott Long 60934c7070dbSScott Long switch (type) { 60944c7070dbSScott Long case IFLIB_INTR_TX: 60954c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 60964c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 6097ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 60984c7070dbSScott Long fn = _task_fn_tx; 6099f98977b5SHans Petter Selasky GROUPTASK_INIT(gtask, 0, fn, q); 61004c7070dbSScott Long break; 61014c7070dbSScott Long case IFLIB_INTR_RX: 61024c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 61034c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 6104ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 61054c7070dbSScott Long fn = _task_fn_rx; 6106f98977b5SHans Petter Selasky NET_GROUPTASK_INIT(gtask, 0, fn, q); 61074c7070dbSScott Long break; 61084c7070dbSScott Long case IFLIB_INTR_IOV: 61094c7070dbSScott Long q = ctx; 61104c7070dbSScott Long gtask = &ctx->ifc_vflr_task; 6111ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 61124c7070dbSScott Long fn = _task_fn_iov; 6113f98977b5SHans Petter Selasky GROUPTASK_INIT(gtask, 0, fn, q); 61144c7070dbSScott Long break; 61154c7070dbSScott Long default: 61164c7070dbSScott Long panic("unknown net intr type"); 61174c7070dbSScott Long } 6118f855ec81SMarius Strobl if (irq != NULL) { 6119f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 6120f855ec81SMarius Strobl q, name); 6121b103855eSStephen Hurd if (err) 6122f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, ctx->ifc_dev, 6123f855ec81SMarius Strobl irq->ii_res, name); 6124f855ec81SMarius Strobl } else { 6125f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, NULL, NULL, name); 6126b103855eSStephen Hurd } 6127b103855eSStephen Hurd } 61284c7070dbSScott Long 61294c7070dbSScott Long void 61304c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq) 61314c7070dbSScott Long { 6132b97de13aSMarius Strobl 61334c7070dbSScott Long if (irq->ii_tag) 61344c7070dbSScott Long bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag); 61354c7070dbSScott Long 61364c7070dbSScott Long if (irq->ii_res) 6137b97de13aSMarius Strobl bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, 6138b97de13aSMarius Strobl rman_get_rid(irq->ii_res), irq->ii_res); 61394c7070dbSScott Long } 61404c7070dbSScott Long 61414c7070dbSScott Long static int 61423e0e6330SStephen Hurd iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name) 61434c7070dbSScott Long { 61444c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 61454c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 61464c7070dbSScott Long if_irq_t irq = &ctx->ifc_legacy_irq; 61474c7070dbSScott Long iflib_filter_info_t info; 6148f855ec81SMarius Strobl device_t dev; 61494c7070dbSScott Long struct grouptask *gtask; 6150f855ec81SMarius Strobl struct resource *res; 61514c7070dbSScott Long struct taskqgroup *tqg; 61524c7070dbSScott Long void *q; 6153d49e83eaSMarius Strobl int err, tqrid; 615441669133SMark Johnston bool rx_only; 61554c7070dbSScott Long 61564c7070dbSScott Long q = &ctx->ifc_rxqs[0]; 61574c7070dbSScott Long info = &rxq[0].ifr_filter_info; 61584c7070dbSScott Long gtask = &rxq[0].ifr_task; 6159ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 6160d49e83eaSMarius Strobl tqrid = *rid; 616141669133SMark Johnston rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0; 61624c7070dbSScott Long 61634c7070dbSScott Long ctx->ifc_flags |= IFC_LEGACY; 61644c7070dbSScott Long info->ifi_filter = filter; 61654c7070dbSScott Long info->ifi_filter_arg = filter_arg; 61664c7070dbSScott Long info->ifi_task = gtask; 616741669133SMark Johnston info->ifi_ctx = rx_only ? ctx : q; 61684c7070dbSScott Long 6169f855ec81SMarius Strobl dev = ctx->ifc_dev; 61704c7070dbSScott Long /* We allocate a single interrupt resource */ 617141669133SMark Johnston err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr_ctx : 617241669133SMark Johnston iflib_fast_intr_rxtx, NULL, info, name); 617341669133SMark Johnston if (err != 0) 61744c7070dbSScott Long return (err); 6175f98977b5SHans Petter Selasky NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, q); 6176f855ec81SMarius Strobl res = irq->ii_res; 6177f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, res, name); 61784c7070dbSScott Long 61794c7070dbSScott Long GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq); 6180f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res, 6181f855ec81SMarius Strobl "tx"); 61824c7070dbSScott Long return (0); 61834c7070dbSScott Long } 61844c7070dbSScott Long 61854c7070dbSScott Long void 61864c7070dbSScott Long iflib_led_create(if_ctx_t ctx) 61874c7070dbSScott Long { 61884c7070dbSScott Long 61894c7070dbSScott Long ctx->ifc_led_dev = led_create(iflib_led_func, ctx, 61904c7070dbSScott Long device_get_nameunit(ctx->ifc_dev)); 61914c7070dbSScott Long } 61924c7070dbSScott Long 61934c7070dbSScott Long void 61944c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid) 61954c7070dbSScott Long { 61964c7070dbSScott Long 61974c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task); 61984c7070dbSScott Long } 61994c7070dbSScott Long 62004c7070dbSScott Long void 62014c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid) 62024c7070dbSScott Long { 62034c7070dbSScott Long 62044c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task); 62054c7070dbSScott Long } 62064c7070dbSScott Long 62074c7070dbSScott Long void 62084c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx) 62094c7070dbSScott Long { 621046fa0c25SEric Joyner 6211ed6611ccSEd Maste MPASS(ctx->ifc_admin_task.gt_taskqueue != NULL); 62124c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_admin_task); 62134c7070dbSScott Long } 62144c7070dbSScott Long 62154c7070dbSScott Long void 62164c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx) 62174c7070dbSScott Long { 62184c7070dbSScott Long 62194c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task); 62204c7070dbSScott Long } 62214c7070dbSScott Long 62224c7070dbSScott Long void 6223d49e83eaSMarius Strobl iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name) 62244c7070dbSScott Long { 62254c7070dbSScott Long 6226f855ec81SMarius Strobl taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL, 6227f855ec81SMarius Strobl name); 62284c7070dbSScott Long } 62294c7070dbSScott Long 62304c7070dbSScott Long void 6231aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn, 6232aa8a24d3SStephen Hurd const char *name) 62334c7070dbSScott Long { 62344c7070dbSScott Long 62354c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, ctx); 6236f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL, 6237f855ec81SMarius Strobl name); 62384c7070dbSScott Long } 62394c7070dbSScott Long 62404c7070dbSScott Long void 624123ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask) 624223ac9029SStephen Hurd { 624323ac9029SStephen Hurd 6244ab2e3f79SStephen Hurd taskqgroup_detach(qgroup_if_config_tqg, gtask); 624523ac9029SStephen Hurd } 624623ac9029SStephen Hurd 624723ac9029SStephen Hurd void 624823ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate) 62494c7070dbSScott Long { 62504c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 62514c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 62524c7070dbSScott Long 62534c7070dbSScott Long if_setbaudrate(ifp, baudrate); 62547b610b60SSean Bruno if (baudrate >= IF_Gbps(10)) { 62557b610b60SSean Bruno STATE_LOCK(ctx); 625695246abbSSean Bruno ctx->ifc_flags |= IFC_PREFETCH; 62577b610b60SSean Bruno STATE_UNLOCK(ctx); 62587b610b60SSean Bruno } 62594c7070dbSScott Long /* If link down, disable watchdog */ 62604c7070dbSScott Long if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) { 62614c7070dbSScott Long for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++) 62624c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 62634c7070dbSScott Long } 62644c7070dbSScott Long ctx->ifc_link_state = link_state; 62654c7070dbSScott Long if_link_state_change(ifp, link_state); 62664c7070dbSScott Long } 62674c7070dbSScott Long 62684c7070dbSScott Long static int 62694c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq) 62704c7070dbSScott Long { 62714c7070dbSScott Long int credits; 62721248952aSSean Bruno #ifdef INVARIANTS 62731248952aSSean Bruno int credits_pre = txq->ift_cidx_processed; 62741248952aSSean Bruno #endif 62754c7070dbSScott Long 62768a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 62778a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 627895246abbSSean Bruno if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0) 62794c7070dbSScott Long return (0); 62804c7070dbSScott Long 62814c7070dbSScott Long txq->ift_processed += credits; 62824c7070dbSScott Long txq->ift_cidx_processed += credits; 62834c7070dbSScott Long 62841248952aSSean Bruno MPASS(credits_pre + credits == txq->ift_cidx_processed); 62854c7070dbSScott Long if (txq->ift_cidx_processed >= txq->ift_size) 62864c7070dbSScott Long txq->ift_cidx_processed -= txq->ift_size; 62874c7070dbSScott Long return (credits); 62884c7070dbSScott Long } 62894c7070dbSScott Long 62904c7070dbSScott Long static int 629195246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget) 62924c7070dbSScott Long { 629395dcf343SMarius Strobl iflib_fl_t fl; 629495dcf343SMarius Strobl u_int i; 62954c7070dbSScott Long 629695dcf343SMarius Strobl for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++) 629795dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 629895dcf343SMarius Strobl BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 629923ac9029SStephen Hurd return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx, 630023ac9029SStephen Hurd budget)); 63014c7070dbSScott Long } 63024c7070dbSScott Long 63034c7070dbSScott Long void 63044c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name, 63054c7070dbSScott Long const char *description, if_int_delay_info_t info, 63064c7070dbSScott Long int offset, int value) 63074c7070dbSScott Long { 63084c7070dbSScott Long info->iidi_ctx = ctx; 63094c7070dbSScott Long info->iidi_offset = offset; 63104c7070dbSScott Long info->iidi_value = value; 63114c7070dbSScott Long SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev), 63124c7070dbSScott Long SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)), 63137029da5cSPawel Biernacki OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 63144c7070dbSScott Long info, 0, iflib_sysctl_int_delay, "I", description); 63154c7070dbSScott Long } 63164c7070dbSScott Long 6317aa8a24d3SStephen Hurd struct sx * 63184c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx) 63194c7070dbSScott Long { 63204c7070dbSScott Long 6321aa8a24d3SStephen Hurd return (&ctx->ifc_ctx_sx); 63224c7070dbSScott Long } 63234c7070dbSScott Long 63244c7070dbSScott Long static int 63254c7070dbSScott Long iflib_msix_init(if_ctx_t ctx) 63264c7070dbSScott Long { 63274c7070dbSScott Long device_t dev = ctx->ifc_dev; 63284c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 63294c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 63303d10e9edSMarius Strobl int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues; 63313d10e9edSMarius Strobl int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors; 63324c7070dbSScott Long 6333d2735264SStephen Hurd iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs; 6334d2735264SStephen Hurd iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs; 633523ac9029SStephen Hurd 6336b97de13aSMarius Strobl if (bootverbose) 6337b97de13aSMarius Strobl device_printf(dev, "msix_init qsets capped at %d\n", 6338b97de13aSMarius Strobl imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets)); 63391248952aSSean Bruno 63404c7070dbSScott Long /* Override by tuneable */ 6341ea351d3fSSean Bruno if (scctx->isc_disable_msix) 63424c7070dbSScott Long goto msi; 63434c7070dbSScott Long 6344b97de13aSMarius Strobl /* First try MSI-X */ 6345b97de13aSMarius Strobl if ((msgs = pci_msix_count(dev)) == 0) { 6346b97de13aSMarius Strobl if (bootverbose) 6347b97de13aSMarius Strobl device_printf(dev, "MSI-X not supported or disabled\n"); 6348b97de13aSMarius Strobl goto msi; 6349b97de13aSMarius Strobl } 63503d10e9edSMarius Strobl 63513d10e9edSMarius Strobl bar = ctx->ifc_softc_ctx.isc_msix_bar; 63524c7070dbSScott Long /* 63534c7070dbSScott Long * bar == -1 => "trust me I know what I'm doing" 63544c7070dbSScott Long * Some drivers are for hardware that is so shoddily 63554c7070dbSScott Long * documented that no one knows which bars are which 63564c7070dbSScott Long * so the developer has to map all bars. This hack 6357b97de13aSMarius Strobl * allows shoddy garbage to use MSI-X in this framework. 63584c7070dbSScott Long */ 63594c7070dbSScott Long if (bar != -1) { 63604c7070dbSScott Long ctx->ifc_msix_mem = bus_alloc_resource_any(dev, 63614c7070dbSScott Long SYS_RES_MEMORY, &bar, RF_ACTIVE); 63624c7070dbSScott Long if (ctx->ifc_msix_mem == NULL) { 6363b97de13aSMarius Strobl device_printf(dev, "Unable to map MSI-X table\n"); 63644c7070dbSScott Long goto msi; 63654c7070dbSScott Long } 63664c7070dbSScott Long } 63673d10e9edSMarius Strobl 63683d10e9edSMarius Strobl admincnt = sctx->isc_admin_intrcnt; 63694c7070dbSScott Long #if IFLIB_DEBUG 63704c7070dbSScott Long /* use only 1 qset in debug mode */ 63714c7070dbSScott Long queuemsgs = min(msgs - admincnt, 1); 63724c7070dbSScott Long #else 63734c7070dbSScott Long queuemsgs = msgs - admincnt; 63744c7070dbSScott Long #endif 63754c7070dbSScott Long #ifdef RSS 63764c7070dbSScott Long queues = imin(queuemsgs, rss_getnumbuckets()); 63774c7070dbSScott Long #else 63784c7070dbSScott Long queues = queuemsgs; 63794c7070dbSScott Long #endif 63804c7070dbSScott Long queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues); 6381b97de13aSMarius Strobl if (bootverbose) 6382b97de13aSMarius Strobl device_printf(dev, 6383b97de13aSMarius Strobl "intr CPUs: %d queue msgs: %d admincnt: %d\n", 63844c7070dbSScott Long CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt); 63854c7070dbSScott Long #ifdef RSS 63864c7070dbSScott Long /* If we're doing RSS, clamp at the number of RSS buckets */ 63874c7070dbSScott Long if (queues > rss_getnumbuckets()) 63884c7070dbSScott Long queues = rss_getnumbuckets(); 63894c7070dbSScott Long #endif 639023ac9029SStephen Hurd if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt) 639123ac9029SStephen Hurd rx_queues = iflib_num_rx_queues; 63924c7070dbSScott Long else 63934c7070dbSScott Long rx_queues = queues; 6394d2735264SStephen Hurd 6395d2735264SStephen Hurd if (rx_queues > scctx->isc_nrxqsets) 6396d2735264SStephen Hurd rx_queues = scctx->isc_nrxqsets; 6397d2735264SStephen Hurd 639823ac9029SStephen Hurd /* 639923ac9029SStephen Hurd * We want this to be all logical CPUs by default 640023ac9029SStephen Hurd */ 64014c7070dbSScott Long if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues) 64024c7070dbSScott Long tx_queues = iflib_num_tx_queues; 64034c7070dbSScott Long else 640423ac9029SStephen Hurd tx_queues = mp_ncpus; 640523ac9029SStephen Hurd 6406d2735264SStephen Hurd if (tx_queues > scctx->isc_ntxqsets) 6407d2735264SStephen Hurd tx_queues = scctx->isc_ntxqsets; 6408d2735264SStephen Hurd 640923ac9029SStephen Hurd if (ctx->ifc_sysctl_qs_eq_override == 0) { 641023ac9029SStephen Hurd #ifdef INVARIANTS 641123ac9029SStephen Hurd if (tx_queues != rx_queues) 641277c1fcecSEric Joyner device_printf(dev, 641377c1fcecSEric Joyner "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n", 641423ac9029SStephen Hurd min(rx_queues, tx_queues), min(rx_queues, tx_queues)); 641523ac9029SStephen Hurd #endif 641623ac9029SStephen Hurd tx_queues = min(rx_queues, tx_queues); 641723ac9029SStephen Hurd rx_queues = min(rx_queues, tx_queues); 641823ac9029SStephen Hurd } 64194c7070dbSScott Long 64203d10e9edSMarius Strobl vectors = rx_queues + admincnt; 64213d10e9edSMarius Strobl if (msgs < vectors) { 64223d10e9edSMarius Strobl device_printf(dev, 64233d10e9edSMarius Strobl "insufficient number of MSI-X vectors " 64243d10e9edSMarius Strobl "(supported %d, need %d)\n", msgs, vectors); 64253d10e9edSMarius Strobl goto msi; 64263d10e9edSMarius Strobl } 64273d10e9edSMarius Strobl 64281722eeacSMarius Strobl device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues, 64291722eeacSMarius Strobl tx_queues); 64303d10e9edSMarius Strobl msgs = vectors; 64314c7070dbSScott Long if ((err = pci_alloc_msix(dev, &vectors)) == 0) { 64323d10e9edSMarius Strobl if (vectors != msgs) { 64333d10e9edSMarius Strobl device_printf(dev, 64343d10e9edSMarius Strobl "Unable to allocate sufficient MSI-X vectors " 64353d10e9edSMarius Strobl "(got %d, need %d)\n", vectors, msgs); 64363d10e9edSMarius Strobl pci_release_msi(dev); 64373d10e9edSMarius Strobl if (bar != -1) { 64383d10e9edSMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 64393d10e9edSMarius Strobl ctx->ifc_msix_mem); 64403d10e9edSMarius Strobl ctx->ifc_msix_mem = NULL; 64413d10e9edSMarius Strobl } 64423d10e9edSMarius Strobl goto msi; 64433d10e9edSMarius Strobl } 6444b97de13aSMarius Strobl device_printf(dev, "Using MSI-X interrupts with %d vectors\n", 6445b97de13aSMarius Strobl vectors); 64464c7070dbSScott Long scctx->isc_vectors = vectors; 64474c7070dbSScott Long scctx->isc_nrxqsets = rx_queues; 64484c7070dbSScott Long scctx->isc_ntxqsets = tx_queues; 64494c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSIX; 645023ac9029SStephen Hurd 64514c7070dbSScott Long return (vectors); 64524c7070dbSScott Long } else { 645377c1fcecSEric Joyner device_printf(dev, 64543d10e9edSMarius Strobl "failed to allocate %d MSI-X vectors, err: %d\n", vectors, 64553d10e9edSMarius Strobl err); 64563d10e9edSMarius Strobl if (bar != -1) { 6457e4defe55SMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 6458e4defe55SMarius Strobl ctx->ifc_msix_mem); 6459e4defe55SMarius Strobl ctx->ifc_msix_mem = NULL; 64604c7070dbSScott Long } 64613d10e9edSMarius Strobl } 64623d10e9edSMarius Strobl 64634c7070dbSScott Long msi: 64644c7070dbSScott Long vectors = pci_msi_count(dev); 64654c7070dbSScott Long scctx->isc_nrxqsets = 1; 64664c7070dbSScott Long scctx->isc_ntxqsets = 1; 64674c7070dbSScott Long scctx->isc_vectors = vectors; 64684c7070dbSScott Long if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) { 64694c7070dbSScott Long device_printf(dev,"Using an MSI interrupt\n"); 64704c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSI; 64714c7070dbSScott Long } else { 6472e4defe55SMarius Strobl scctx->isc_vectors = 1; 64734c7070dbSScott Long device_printf(dev,"Using a Legacy interrupt\n"); 64744c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 64754c7070dbSScott Long } 64764c7070dbSScott Long 64774c7070dbSScott Long return (vectors); 64784c7070dbSScott Long } 64794c7070dbSScott Long 6480e4defe55SMarius Strobl static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" }; 64814c7070dbSScott Long 64824c7070dbSScott Long static int 64834c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS) 64844c7070dbSScott Long { 64854c7070dbSScott Long int rc; 64864c7070dbSScott Long uint16_t *state = ((uint16_t *)oidp->oid_arg1); 64874c7070dbSScott Long struct sbuf *sb; 6488e4defe55SMarius Strobl const char *ring_state = "UNKNOWN"; 64894c7070dbSScott Long 64904c7070dbSScott Long /* XXX needed ? */ 64914c7070dbSScott Long rc = sysctl_wire_old_buffer(req, 0); 64924c7070dbSScott Long MPASS(rc == 0); 64934c7070dbSScott Long if (rc != 0) 64944c7070dbSScott Long return (rc); 64954c7070dbSScott Long sb = sbuf_new_for_sysctl(NULL, NULL, 80, req); 64964c7070dbSScott Long MPASS(sb != NULL); 64974c7070dbSScott Long if (sb == NULL) 64984c7070dbSScott Long return (ENOMEM); 64994c7070dbSScott Long if (state[3] <= 3) 65004c7070dbSScott Long ring_state = ring_states[state[3]]; 65014c7070dbSScott Long 65024c7070dbSScott Long sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s", 65034c7070dbSScott Long state[0], state[1], state[2], ring_state); 65044c7070dbSScott Long rc = sbuf_finish(sb); 65054c7070dbSScott Long sbuf_delete(sb); 65064c7070dbSScott Long return(rc); 65074c7070dbSScott Long } 65084c7070dbSScott Long 650923ac9029SStephen Hurd enum iflib_ndesc_handler { 651023ac9029SStephen Hurd IFLIB_NTXD_HANDLER, 651123ac9029SStephen Hurd IFLIB_NRXD_HANDLER, 651223ac9029SStephen Hurd }; 65134c7070dbSScott Long 651423ac9029SStephen Hurd static int 651523ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS) 651623ac9029SStephen Hurd { 651723ac9029SStephen Hurd if_ctx_t ctx = (void *)arg1; 651823ac9029SStephen Hurd enum iflib_ndesc_handler type = arg2; 651923ac9029SStephen Hurd char buf[256] = {0}; 652095246abbSSean Bruno qidx_t *ndesc; 652123ac9029SStephen Hurd char *p, *next; 652223ac9029SStephen Hurd int nqs, rc, i; 652323ac9029SStephen Hurd 652423ac9029SStephen Hurd nqs = 8; 652523ac9029SStephen Hurd switch(type) { 652623ac9029SStephen Hurd case IFLIB_NTXD_HANDLER: 652723ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_ntxds; 652823ac9029SStephen Hurd if (ctx->ifc_sctx) 652923ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_ntxqs; 653023ac9029SStephen Hurd break; 653123ac9029SStephen Hurd case IFLIB_NRXD_HANDLER: 653223ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_nrxds; 653323ac9029SStephen Hurd if (ctx->ifc_sctx) 653423ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_nrxqs; 653523ac9029SStephen Hurd break; 65361ae4848cSMatt Macy default: 65373d10e9edSMarius Strobl printf("%s: unhandled type\n", __func__); 65383d10e9edSMarius Strobl return (EINVAL); 653923ac9029SStephen Hurd } 654023ac9029SStephen Hurd if (nqs == 0) 654123ac9029SStephen Hurd nqs = 8; 654223ac9029SStephen Hurd 654323ac9029SStephen Hurd for (i=0; i<8; i++) { 654423ac9029SStephen Hurd if (i >= nqs) 654523ac9029SStephen Hurd break; 654623ac9029SStephen Hurd if (i) 654723ac9029SStephen Hurd strcat(buf, ","); 654823ac9029SStephen Hurd sprintf(strchr(buf, 0), "%d", ndesc[i]); 654923ac9029SStephen Hurd } 655023ac9029SStephen Hurd 655123ac9029SStephen Hurd rc = sysctl_handle_string(oidp, buf, sizeof(buf), req); 655223ac9029SStephen Hurd if (rc || req->newptr == NULL) 655323ac9029SStephen Hurd return rc; 655423ac9029SStephen Hurd 655523ac9029SStephen Hurd for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p; 655623ac9029SStephen Hurd i++, p = strsep(&next, " ,")) { 655723ac9029SStephen Hurd ndesc[i] = strtoul(p, NULL, 10); 655823ac9029SStephen Hurd } 655923ac9029SStephen Hurd 656023ac9029SStephen Hurd return(rc); 656123ac9029SStephen Hurd } 65624c7070dbSScott Long 65634c7070dbSScott Long #define NAME_BUFLEN 32 65644c7070dbSScott Long static void 65654c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx) 65664c7070dbSScott Long { 65674c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 65684c7070dbSScott Long struct sysctl_oid_list *child, *oid_list; 65694c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 65704c7070dbSScott Long struct sysctl_oid *node; 65714c7070dbSScott Long 65724c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 65734c7070dbSScott Long child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 65744c7070dbSScott Long ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib", 65757029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "IFLIB fields"); 65764c7070dbSScott Long oid_list = SYSCTL_CHILDREN(node); 65774c7070dbSScott Long 657810a1e981SEric Joyner SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version", 657910a1e981SEric Joyner CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 658023ac9029SStephen Hurd "driver version"); 658123ac9029SStephen Hurd 65824c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs", 65834c7070dbSScott Long CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0, 65844c7070dbSScott Long "# of txqs to use, 0 => use default #"); 65854c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs", 658623ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0, 658723ac9029SStephen Hurd "# of rxqs to use, 0 => use default #"); 658823ac9029SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable", 658923ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0, 659023ac9029SStephen Hurd "permit #txq != #rxq"); 6591ea351d3fSSean Bruno SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix", 6592ea351d3fSSean Bruno CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0, 6593b97de13aSMarius Strobl "disable MSI-X (default 0)"); 6594f4d2154eSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget", 6595f4d2154eSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, 65961722eeacSMarius Strobl "set the RX budget"); 6597fe51d4cdSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate", 6598fe51d4cdSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0, 65991722eeacSMarius Strobl "cause TX to abdicate instead of running to completion"); 6600f154ece0SStephen Hurd ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED; 6601f154ece0SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "core_offset", 6602f154ece0SStephen Hurd CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0, 6603f154ece0SStephen Hurd "offset to start using cores at"); 6604f154ece0SStephen Hurd SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "separate_txrx", 6605f154ece0SStephen Hurd CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0, 6606f154ece0SStephen Hurd "use separate cores for TX and RX"); 66074c7070dbSScott Long 660823ac9029SStephen Hurd /* XXX change for per-queue sizes */ 660923ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds", 66107029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx, 66117029da5cSPawel Biernacki IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A", 66121722eeacSMarius Strobl "list of # of TX descriptors to use, 0 = use default #"); 661323ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds", 66147029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx, 66157029da5cSPawel Biernacki IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A", 66161722eeacSMarius Strobl "list of # of RX descriptors to use, 0 = use default #"); 66174c7070dbSScott Long } 66184c7070dbSScott Long 66194c7070dbSScott Long static void 66204c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx) 66214c7070dbSScott Long { 66224c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 66234c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 66244c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 66254c7070dbSScott Long struct sysctl_oid_list *child; 66264c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 66274c7070dbSScott Long iflib_fl_t fl; 66284c7070dbSScott Long iflib_txq_t txq; 66294c7070dbSScott Long iflib_rxq_t rxq; 66304c7070dbSScott Long int i, j; 66314c7070dbSScott Long char namebuf[NAME_BUFLEN]; 66324c7070dbSScott Long char *qfmt; 66334c7070dbSScott Long struct sysctl_oid *queue_node, *fl_node, *node; 66344c7070dbSScott Long struct sysctl_oid_list *queue_list, *fl_list; 66354c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 66364c7070dbSScott Long 66374c7070dbSScott Long node = ctx->ifc_sysctl_node; 66384c7070dbSScott Long child = SYSCTL_CHILDREN(node); 66394c7070dbSScott Long 66404c7070dbSScott Long if (scctx->isc_ntxqsets > 100) 66414c7070dbSScott Long qfmt = "txq%03d"; 66424c7070dbSScott Long else if (scctx->isc_ntxqsets > 10) 66434c7070dbSScott Long qfmt = "txq%02d"; 66444c7070dbSScott Long else 66454c7070dbSScott Long qfmt = "txq%d"; 66464c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) { 66474c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 66484c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 66497029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name"); 66504c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 66514c7070dbSScott Long #if MEMORY_LOGGING 66524c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued", 66534c7070dbSScott Long CTLFLAG_RD, 66544c7070dbSScott Long &txq->ift_dequeued, "total mbufs freed"); 66554c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued", 66564c7070dbSScott Long CTLFLAG_RD, 66574c7070dbSScott Long &txq->ift_enqueued, "total mbufs enqueued"); 66584c7070dbSScott Long #endif 66594c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag", 66604c7070dbSScott Long CTLFLAG_RD, 66614c7070dbSScott Long &txq->ift_mbuf_defrag, "# of times m_defrag was called"); 66624c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups", 66634c7070dbSScott Long CTLFLAG_RD, 66644c7070dbSScott Long &txq->ift_pullups, "# of times m_pullup was called"); 66654c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed", 66664c7070dbSScott Long CTLFLAG_RD, 66674c7070dbSScott Long &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed"); 66684c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail", 66694c7070dbSScott Long CTLFLAG_RD, 667023ac9029SStephen Hurd &txq->ift_no_desc_avail, "# of times no descriptors were available"); 66714c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed", 66724c7070dbSScott Long CTLFLAG_RD, 66731722eeacSMarius Strobl &txq->ift_map_failed, "# of times DMA map failed"); 66744c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig", 66754c7070dbSScott Long CTLFLAG_RD, 66764c7070dbSScott Long &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG"); 66774c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup", 66784c7070dbSScott Long CTLFLAG_RD, 66794c7070dbSScott Long &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG"); 66804c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx", 66814c7070dbSScott Long CTLFLAG_RD, 66824c7070dbSScott Long &txq->ift_pidx, 1, "Producer Index"); 66834c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx", 66844c7070dbSScott Long CTLFLAG_RD, 66854c7070dbSScott Long &txq->ift_cidx, 1, "Consumer Index"); 66864c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed", 66874c7070dbSScott Long CTLFLAG_RD, 66884c7070dbSScott Long &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update"); 66894c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use", 66904c7070dbSScott Long CTLFLAG_RD, 66914c7070dbSScott Long &txq->ift_in_use, 1, "descriptors in use"); 66924c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed", 66934c7070dbSScott Long CTLFLAG_RD, 66944c7070dbSScott Long &txq->ift_processed, "descriptors procesed for clean"); 66954c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned", 66964c7070dbSScott Long CTLFLAG_RD, 66974c7070dbSScott Long &txq->ift_cleaned, "total cleaned"); 66984c7070dbSScott Long SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state", 66997029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 67007029da5cSPawel Biernacki __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0, 67017029da5cSPawel Biernacki mp_ring_state_handler, "A", "soft ring state"); 67024c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues", 670395246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->enqueues, 67044c7070dbSScott Long "# of enqueues to the mp_ring for this queue"); 67054c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops", 670695246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->drops, 67074c7070dbSScott Long "# of drops in the mp_ring for this queue"); 67084c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts", 670995246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->starts, 67104c7070dbSScott Long "# of normal consumer starts in the mp_ring for this queue"); 67114c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls", 671295246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->stalls, 67134c7070dbSScott Long "# of consumer stalls in the mp_ring for this queue"); 67144c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts", 671595246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->restarts, 67164c7070dbSScott Long "# of consumer restarts in the mp_ring for this queue"); 67174c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications", 671895246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->abdications, 67194c7070dbSScott Long "# of consumer abdications in the mp_ring for this queue"); 67204c7070dbSScott Long } 67214c7070dbSScott Long 67224c7070dbSScott Long if (scctx->isc_nrxqsets > 100) 67234c7070dbSScott Long qfmt = "rxq%03d"; 67244c7070dbSScott Long else if (scctx->isc_nrxqsets > 10) 67254c7070dbSScott Long qfmt = "rxq%02d"; 67264c7070dbSScott Long else 67274c7070dbSScott Long qfmt = "rxq%d"; 67284c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) { 67294c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 67304c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 67317029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name"); 67324c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 673323ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 67344c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx", 67354c7070dbSScott Long CTLFLAG_RD, 67364c7070dbSScott Long &rxq->ifr_cq_cidx, 1, "Consumer Index"); 67374c7070dbSScott Long } 6738da69b8f9SSean Bruno 67394c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 67404c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j); 67414c7070dbSScott Long fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf, 67427029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "freelist Name"); 67434c7070dbSScott Long fl_list = SYSCTL_CHILDREN(fl_node); 67444c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx", 67454c7070dbSScott Long CTLFLAG_RD, 67464c7070dbSScott Long &fl->ifl_pidx, 1, "Producer Index"); 67474c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx", 67484c7070dbSScott Long CTLFLAG_RD, 67494c7070dbSScott Long &fl->ifl_cidx, 1, "Consumer Index"); 67504c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits", 67514c7070dbSScott Long CTLFLAG_RD, 67524c7070dbSScott Long &fl->ifl_credits, 1, "credits available"); 6753b3813609SPatrick Kelsey SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size", 6754b3813609SPatrick Kelsey CTLFLAG_RD, 6755b3813609SPatrick Kelsey &fl->ifl_buf_size, 1, "buffer size"); 67564c7070dbSScott Long #if MEMORY_LOGGING 67574c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued", 67584c7070dbSScott Long CTLFLAG_RD, 67594c7070dbSScott Long &fl->ifl_m_enqueued, "mbufs allocated"); 67604c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued", 67614c7070dbSScott Long CTLFLAG_RD, 67624c7070dbSScott Long &fl->ifl_m_dequeued, "mbufs freed"); 67634c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued", 67644c7070dbSScott Long CTLFLAG_RD, 67654c7070dbSScott Long &fl->ifl_cl_enqueued, "clusters allocated"); 67664c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued", 67674c7070dbSScott Long CTLFLAG_RD, 67684c7070dbSScott Long &fl->ifl_cl_dequeued, "clusters freed"); 67694c7070dbSScott Long #endif 67704c7070dbSScott Long 67714c7070dbSScott Long } 67724c7070dbSScott Long } 67734c7070dbSScott Long 67744c7070dbSScott Long } 677595246abbSSean Bruno 677677c1fcecSEric Joyner void 677777c1fcecSEric Joyner iflib_request_reset(if_ctx_t ctx) 677877c1fcecSEric Joyner { 677977c1fcecSEric Joyner 678077c1fcecSEric Joyner STATE_LOCK(ctx); 678177c1fcecSEric Joyner ctx->ifc_flags |= IFC_DO_RESET; 678277c1fcecSEric Joyner STATE_UNLOCK(ctx); 678377c1fcecSEric Joyner } 678477c1fcecSEric Joyner 678595246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 678695246abbSSean Bruno static struct mbuf * 678795246abbSSean Bruno iflib_fixup_rx(struct mbuf *m) 678895246abbSSean Bruno { 678995246abbSSean Bruno struct mbuf *n; 679095246abbSSean Bruno 679195246abbSSean Bruno if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) { 679295246abbSSean Bruno bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len); 679395246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 679495246abbSSean Bruno n = m; 679595246abbSSean Bruno } else { 679695246abbSSean Bruno MGETHDR(n, M_NOWAIT, MT_DATA); 679795246abbSSean Bruno if (n == NULL) { 679895246abbSSean Bruno m_freem(m); 679995246abbSSean Bruno return (NULL); 680095246abbSSean Bruno } 680195246abbSSean Bruno bcopy(m->m_data, n->m_data, ETHER_HDR_LEN); 680295246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 680395246abbSSean Bruno m->m_len -= ETHER_HDR_LEN; 680495246abbSSean Bruno n->m_len = ETHER_HDR_LEN; 680595246abbSSean Bruno M_MOVE_PKTHDR(n, m); 680695246abbSSean Bruno n->m_next = m; 680795246abbSSean Bruno } 680895246abbSSean Bruno return (n); 680995246abbSSean Bruno } 681095246abbSSean Bruno #endif 681194618825SMark Johnston 68127790c8c1SConrad Meyer #ifdef DEBUGNET 681394618825SMark Johnston static void 68147790c8c1SConrad Meyer iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize) 681594618825SMark Johnston { 681694618825SMark Johnston if_ctx_t ctx; 681794618825SMark Johnston 681894618825SMark Johnston ctx = if_getsoftc(ifp); 681994618825SMark Johnston CTX_LOCK(ctx); 682094618825SMark Johnston *nrxr = NRXQSETS(ctx); 682194618825SMark Johnston *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size; 682294618825SMark Johnston *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size; 682394618825SMark Johnston CTX_UNLOCK(ctx); 682494618825SMark Johnston } 682594618825SMark Johnston 682694618825SMark Johnston static void 68277790c8c1SConrad Meyer iflib_debugnet_event(if_t ifp, enum debugnet_ev event) 682894618825SMark Johnston { 682994618825SMark Johnston if_ctx_t ctx; 683094618825SMark Johnston if_softc_ctx_t scctx; 683194618825SMark Johnston iflib_fl_t fl; 683294618825SMark Johnston iflib_rxq_t rxq; 683394618825SMark Johnston int i, j; 683494618825SMark Johnston 683594618825SMark Johnston ctx = if_getsoftc(ifp); 683694618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 683794618825SMark Johnston 683894618825SMark Johnston switch (event) { 68397790c8c1SConrad Meyer case DEBUGNET_START: 684094618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) { 684194618825SMark Johnston rxq = &ctx->ifc_rxqs[i]; 684294618825SMark Johnston for (j = 0; j < rxq->ifr_nfl; j++) { 684394618825SMark Johnston fl = rxq->ifr_fl; 684494618825SMark Johnston fl->ifl_zone = m_getzone(fl->ifl_buf_size); 684594618825SMark Johnston } 684694618825SMark Johnston } 684794618825SMark Johnston iflib_no_tx_batch = 1; 684894618825SMark Johnston break; 684994618825SMark Johnston default: 685094618825SMark Johnston break; 685194618825SMark Johnston } 685294618825SMark Johnston } 685394618825SMark Johnston 685494618825SMark Johnston static int 68557790c8c1SConrad Meyer iflib_debugnet_transmit(if_t ifp, struct mbuf *m) 685694618825SMark Johnston { 685794618825SMark Johnston if_ctx_t ctx; 685894618825SMark Johnston iflib_txq_t txq; 685994618825SMark Johnston int error; 686094618825SMark Johnston 686194618825SMark Johnston ctx = if_getsoftc(ifp); 686294618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 686394618825SMark Johnston IFF_DRV_RUNNING) 686494618825SMark Johnston return (EBUSY); 686594618825SMark Johnston 686694618825SMark Johnston txq = &ctx->ifc_txqs[0]; 686794618825SMark Johnston error = iflib_encap(txq, &m); 686894618825SMark Johnston if (error == 0) 686994618825SMark Johnston (void)iflib_txd_db_check(ctx, txq, true, txq->ift_in_use); 687094618825SMark Johnston return (error); 687194618825SMark Johnston } 687294618825SMark Johnston 687394618825SMark Johnston static int 68747790c8c1SConrad Meyer iflib_debugnet_poll(if_t ifp, int count) 687594618825SMark Johnston { 68760b8df657SGleb Smirnoff struct epoch_tracker et; 687794618825SMark Johnston if_ctx_t ctx; 687894618825SMark Johnston if_softc_ctx_t scctx; 687994618825SMark Johnston iflib_txq_t txq; 688094618825SMark Johnston int i; 688194618825SMark Johnston 688294618825SMark Johnston ctx = if_getsoftc(ifp); 688394618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 688494618825SMark Johnston 688594618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 688694618825SMark Johnston IFF_DRV_RUNNING) 688794618825SMark Johnston return (EBUSY); 688894618825SMark Johnston 688994618825SMark Johnston txq = &ctx->ifc_txqs[0]; 689094618825SMark Johnston (void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 689194618825SMark Johnston 68920b8df657SGleb Smirnoff NET_EPOCH_ENTER(et); 689394618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) 689494618825SMark Johnston (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */); 68950b8df657SGleb Smirnoff NET_EPOCH_EXIT(et); 689694618825SMark Johnston return (0); 689794618825SMark Johnston } 68987790c8c1SConrad Meyer #endif /* DEBUGNET */ 6899