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> 626d49b41eSAndrew Gallatin #include <net/pfil.h> 6335e4e998SStephen Hurd #include <net/vnet.h> 644c7070dbSScott Long 654c7070dbSScott Long #include <netinet/in.h> 664c7070dbSScott Long #include <netinet/in_pcb.h> 674c7070dbSScott Long #include <netinet/tcp_lro.h> 684c7070dbSScott Long #include <netinet/in_systm.h> 694c7070dbSScott Long #include <netinet/if_ether.h> 704c7070dbSScott Long #include <netinet/ip.h> 714c7070dbSScott Long #include <netinet/ip6.h> 724c7070dbSScott Long #include <netinet/tcp.h> 7335e4e998SStephen Hurd #include <netinet/ip_var.h> 7494618825SMark Johnston #include <netinet/netdump/netdump.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 1324c7070dbSScott Long struct iflib_txq; 1334c7070dbSScott Long typedef struct iflib_txq *iflib_txq_t; 1344c7070dbSScott Long struct iflib_rxq; 1354c7070dbSScott Long typedef struct iflib_rxq *iflib_rxq_t; 1364c7070dbSScott Long struct iflib_fl; 1374c7070dbSScott Long typedef struct iflib_fl *iflib_fl_t; 1384c7070dbSScott Long 1394ecb427aSSean Bruno struct iflib_ctx; 1404ecb427aSSean Bruno 1412d873474SStephen Hurd static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid); 142dd7fbcf1SStephen Hurd static void iflib_timer(void *arg); 1432d873474SStephen Hurd 1444c7070dbSScott Long typedef struct iflib_filter_info { 1454c7070dbSScott Long driver_filter_t *ifi_filter; 1464c7070dbSScott Long void *ifi_filter_arg; 1474c7070dbSScott Long struct grouptask *ifi_task; 14895246abbSSean Bruno void *ifi_ctx; 1494c7070dbSScott Long } *iflib_filter_info_t; 1504c7070dbSScott Long 1514c7070dbSScott Long struct iflib_ctx { 1524c7070dbSScott Long KOBJ_FIELDS; 1534c7070dbSScott Long /* 1544c7070dbSScott Long * Pointer to hardware driver's softc 1554c7070dbSScott Long */ 1564c7070dbSScott Long void *ifc_softc; 1574c7070dbSScott Long device_t ifc_dev; 1584c7070dbSScott Long if_t ifc_ifp; 1594c7070dbSScott Long 1604c7070dbSScott Long cpuset_t ifc_cpus; 1614c7070dbSScott Long if_shared_ctx_t ifc_sctx; 1624c7070dbSScott Long struct if_softc_ctx ifc_softc_ctx; 1634c7070dbSScott Long 164aa8a24d3SStephen Hurd struct sx ifc_ctx_sx; 1657b610b60SSean Bruno struct mtx ifc_state_mtx; 1664c7070dbSScott Long 1674c7070dbSScott Long iflib_txq_t ifc_txqs; 1684c7070dbSScott Long iflib_rxq_t ifc_rxqs; 1694c7070dbSScott Long uint32_t ifc_if_flags; 1704c7070dbSScott Long uint32_t ifc_flags; 1714c7070dbSScott Long uint32_t ifc_max_fl_buf_size; 1721b9d9394SEric Joyner uint32_t ifc_rx_mbuf_sz; 1734c7070dbSScott Long 1744c7070dbSScott Long int ifc_link_state; 1754c7070dbSScott Long int ifc_watchdog_events; 1764c7070dbSScott Long struct cdev *ifc_led_dev; 1774c7070dbSScott Long struct resource *ifc_msix_mem; 1784c7070dbSScott Long 1794c7070dbSScott Long struct if_irq ifc_legacy_irq; 1804c7070dbSScott Long struct grouptask ifc_admin_task; 1814c7070dbSScott Long struct grouptask ifc_vflr_task; 1824c7070dbSScott Long struct iflib_filter_info ifc_filter_info; 1834c7070dbSScott Long struct ifmedia ifc_media; 184e2621d96SMatt Macy struct ifmedia *ifc_mediap; 1854c7070dbSScott Long 1864c7070dbSScott Long struct sysctl_oid *ifc_sysctl_node; 1874c7070dbSScott Long uint16_t ifc_sysctl_ntxqs; 1884c7070dbSScott Long uint16_t ifc_sysctl_nrxqs; 18923ac9029SStephen Hurd uint16_t ifc_sysctl_qs_eq_override; 190f4d2154eSStephen Hurd uint16_t ifc_sysctl_rx_budget; 191fe51d4cdSStephen Hurd uint16_t ifc_sysctl_tx_abdicate; 192f154ece0SStephen Hurd uint16_t ifc_sysctl_core_offset; 193f154ece0SStephen Hurd #define CORE_OFFSET_UNSPECIFIED 0xffff 194f154ece0SStephen Hurd uint8_t ifc_sysctl_separate_txrx; 19523ac9029SStephen Hurd 19695246abbSSean Bruno qidx_t ifc_sysctl_ntxds[8]; 19795246abbSSean Bruno qidx_t ifc_sysctl_nrxds[8]; 1984c7070dbSScott Long struct if_txrx ifc_txrx; 1994c7070dbSScott Long #define isc_txd_encap ifc_txrx.ift_txd_encap 2004c7070dbSScott Long #define isc_txd_flush ifc_txrx.ift_txd_flush 2014c7070dbSScott Long #define isc_txd_credits_update ifc_txrx.ift_txd_credits_update 2024c7070dbSScott Long #define isc_rxd_available ifc_txrx.ift_rxd_available 2034c7070dbSScott Long #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get 2044c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2054c7070dbSScott Long #define isc_rxd_flush ifc_txrx.ift_rxd_flush 2064c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2074c7070dbSScott Long #define isc_rxd_refill ifc_txrx.ift_rxd_refill 2084c7070dbSScott Long #define isc_legacy_intr ifc_txrx.ift_legacy_intr 2094c7070dbSScott Long eventhandler_tag ifc_vlan_attach_event; 2104c7070dbSScott Long eventhandler_tag ifc_vlan_detach_event; 2111fd8c72cSKyle Evans struct ether_addr ifc_mac; 2124c7070dbSScott Long }; 2134c7070dbSScott Long 2144c7070dbSScott Long void * 2154c7070dbSScott Long iflib_get_softc(if_ctx_t ctx) 2164c7070dbSScott Long { 2174c7070dbSScott Long 2184c7070dbSScott Long return (ctx->ifc_softc); 2194c7070dbSScott Long } 2204c7070dbSScott Long 2214c7070dbSScott Long device_t 2224c7070dbSScott Long iflib_get_dev(if_ctx_t ctx) 2234c7070dbSScott Long { 2244c7070dbSScott Long 2254c7070dbSScott Long return (ctx->ifc_dev); 2264c7070dbSScott Long } 2274c7070dbSScott Long 2284c7070dbSScott Long if_t 2294c7070dbSScott Long iflib_get_ifp(if_ctx_t ctx) 2304c7070dbSScott Long { 2314c7070dbSScott Long 2324c7070dbSScott Long return (ctx->ifc_ifp); 2334c7070dbSScott Long } 2344c7070dbSScott Long 2354c7070dbSScott Long struct ifmedia * 2364c7070dbSScott Long iflib_get_media(if_ctx_t ctx) 2374c7070dbSScott Long { 2384c7070dbSScott Long 239e2621d96SMatt Macy return (ctx->ifc_mediap); 2404c7070dbSScott Long } 2414c7070dbSScott Long 24209f6ff4fSMatt Macy uint32_t 24309f6ff4fSMatt Macy iflib_get_flags(if_ctx_t ctx) 24409f6ff4fSMatt Macy { 24509f6ff4fSMatt Macy return (ctx->ifc_flags); 24609f6ff4fSMatt Macy } 24709f6ff4fSMatt Macy 24809f6ff4fSMatt Macy void 2494c7070dbSScott Long iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]) 2504c7070dbSScott Long { 2514c7070dbSScott Long 2521fd8c72cSKyle Evans bcopy(mac, ctx->ifc_mac.octet, ETHER_ADDR_LEN); 2534c7070dbSScott Long } 2544c7070dbSScott Long 2554c7070dbSScott Long if_softc_ctx_t 2564c7070dbSScott Long iflib_get_softc_ctx(if_ctx_t ctx) 2574c7070dbSScott Long { 2584c7070dbSScott Long 2594c7070dbSScott Long return (&ctx->ifc_softc_ctx); 2604c7070dbSScott Long } 2614c7070dbSScott Long 2624c7070dbSScott Long if_shared_ctx_t 2634c7070dbSScott Long iflib_get_sctx(if_ctx_t ctx) 2644c7070dbSScott Long { 2654c7070dbSScott Long 2664c7070dbSScott Long return (ctx->ifc_sctx); 2674c7070dbSScott Long } 2684c7070dbSScott Long 26995246abbSSean Bruno #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2) 2704c7070dbSScott Long #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*)) 2715e888388SSean Bruno #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1))) 2724c7070dbSScott Long 2734c7070dbSScott Long #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP) 2744c7070dbSScott Long #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF) 2754c7070dbSScott Long 276e035717eSSean Bruno typedef struct iflib_sw_rx_desc_array { 277e035717eSSean Bruno bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */ 278e035717eSSean Bruno struct mbuf **ifsd_m; /* pkthdr mbufs */ 279e035717eSSean Bruno caddr_t *ifsd_cl; /* direct cluster pointer for rx */ 280fbec776dSAndrew Gallatin bus_addr_t *ifsd_ba; /* bus addr of cluster for rx */ 281e035717eSSean Bruno } iflib_rxsd_array_t; 2824c7070dbSScott Long 2834c7070dbSScott Long typedef struct iflib_sw_tx_desc_array { 2844c7070dbSScott Long bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */ 2858a04b53dSKonstantin Belousov bus_dmamap_t *ifsd_tso_map; /* bus_dma maps for TSO packet */ 2864c7070dbSScott Long struct mbuf **ifsd_m; /* pkthdr mbufs */ 28795246abbSSean Bruno } if_txsd_vec_t; 2884c7070dbSScott Long 2894c7070dbSScott Long /* magic number that should be high enough for any hardware */ 2904c7070dbSScott Long #define IFLIB_MAX_TX_SEGS 128 29195246abbSSean Bruno #define IFLIB_RX_COPY_THRESH 128 2924c7070dbSScott Long #define IFLIB_MAX_RX_REFRESH 32 29395246abbSSean Bruno /* The minimum descriptors per second before we start coalescing */ 29495246abbSSean Bruno #define IFLIB_MIN_DESC_SEC 16384 29595246abbSSean Bruno #define IFLIB_DEFAULT_TX_UPDATE_FREQ 16 2964c7070dbSScott Long #define IFLIB_QUEUE_IDLE 0 2974c7070dbSScott Long #define IFLIB_QUEUE_HUNG 1 2984c7070dbSScott Long #define IFLIB_QUEUE_WORKING 2 29995246abbSSean Bruno /* maximum number of txqs that can share an rx interrupt */ 30095246abbSSean Bruno #define IFLIB_MAX_TX_SHARED_INTR 4 3014c7070dbSScott Long 30295246abbSSean Bruno /* this should really scale with ring size - this is a fairly arbitrary value */ 30395246abbSSean Bruno #define TX_BATCH_SIZE 32 3044c7070dbSScott Long 3054c7070dbSScott Long #define IFLIB_RESTART_BUDGET 8 3064c7070dbSScott Long 3074c7070dbSScott Long #define CSUM_OFFLOAD (CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \ 3084c7070dbSScott Long CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \ 3094c7070dbSScott Long CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP) 3101722eeacSMarius Strobl 3114c7070dbSScott Long struct iflib_txq { 31295246abbSSean Bruno qidx_t ift_in_use; 31395246abbSSean Bruno qidx_t ift_cidx; 31495246abbSSean Bruno qidx_t ift_cidx_processed; 31595246abbSSean Bruno qidx_t ift_pidx; 3164c7070dbSScott Long uint8_t ift_gen; 31723ac9029SStephen Hurd uint8_t ift_br_offset; 31895246abbSSean Bruno uint16_t ift_npending; 31995246abbSSean Bruno uint16_t ift_db_pending; 32095246abbSSean Bruno uint16_t ift_rs_pending; 3214c7070dbSScott Long /* implicit pad */ 32295246abbSSean Bruno uint8_t ift_txd_size[8]; 3234c7070dbSScott Long uint64_t ift_processed; 3244c7070dbSScott Long uint64_t ift_cleaned; 32595246abbSSean Bruno uint64_t ift_cleaned_prev; 3264c7070dbSScott Long #if MEMORY_LOGGING 3274c7070dbSScott Long uint64_t ift_enqueued; 3284c7070dbSScott Long uint64_t ift_dequeued; 3294c7070dbSScott Long #endif 3304c7070dbSScott Long uint64_t ift_no_tx_dma_setup; 3314c7070dbSScott Long uint64_t ift_no_desc_avail; 3324c7070dbSScott Long uint64_t ift_mbuf_defrag_failed; 3334c7070dbSScott Long uint64_t ift_mbuf_defrag; 3344c7070dbSScott Long uint64_t ift_map_failed; 3354c7070dbSScott Long uint64_t ift_txd_encap_efbig; 3364c7070dbSScott Long uint64_t ift_pullups; 337dd7fbcf1SStephen Hurd uint64_t ift_last_timer_tick; 3384c7070dbSScott Long 3394c7070dbSScott Long struct mtx ift_mtx; 3404c7070dbSScott Long struct mtx ift_db_mtx; 3414c7070dbSScott Long 3424c7070dbSScott Long /* constant values */ 3434c7070dbSScott Long if_ctx_t ift_ctx; 34495246abbSSean Bruno struct ifmp_ring *ift_br; 3454c7070dbSScott Long struct grouptask ift_task; 34695246abbSSean Bruno qidx_t ift_size; 3474c7070dbSScott Long uint16_t ift_id; 3484c7070dbSScott Long struct callout ift_timer; 3494c7070dbSScott Long 35095246abbSSean Bruno if_txsd_vec_t ift_sds; 3514c7070dbSScott Long uint8_t ift_qstatus; 3524c7070dbSScott Long uint8_t ift_closed; 35395246abbSSean Bruno uint8_t ift_update_freq; 3544c7070dbSScott Long struct iflib_filter_info ift_filter_info; 355bfce461eSMarius Strobl bus_dma_tag_t ift_buf_tag; 356bfce461eSMarius Strobl bus_dma_tag_t ift_tso_buf_tag; 3574c7070dbSScott Long iflib_dma_info_t ift_ifdi; 3584c7070dbSScott Long #define MTX_NAME_LEN 16 3594c7070dbSScott Long char ift_mtx_name[MTX_NAME_LEN]; 3604c7070dbSScott Long bus_dma_segment_t ift_segs[IFLIB_MAX_TX_SEGS] __aligned(CACHE_LINE_SIZE); 3611248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 3621248952aSSean Bruno uint64_t ift_cpu_exec_count[256]; 3631248952aSSean Bruno #endif 3644c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 3654c7070dbSScott Long 3664c7070dbSScott Long struct iflib_fl { 36795246abbSSean Bruno qidx_t ifl_cidx; 36895246abbSSean Bruno qidx_t ifl_pidx; 36995246abbSSean Bruno qidx_t ifl_credits; 3704c7070dbSScott Long uint8_t ifl_gen; 37195246abbSSean Bruno uint8_t ifl_rxd_size; 3724c7070dbSScott Long #if MEMORY_LOGGING 3734c7070dbSScott Long uint64_t ifl_m_enqueued; 3744c7070dbSScott Long uint64_t ifl_m_dequeued; 3754c7070dbSScott Long uint64_t ifl_cl_enqueued; 3764c7070dbSScott Long uint64_t ifl_cl_dequeued; 3774c7070dbSScott Long #endif 3784c7070dbSScott Long /* implicit pad */ 37987890dbaSSean Bruno bitstr_t *ifl_rx_bitmap; 38087890dbaSSean Bruno qidx_t ifl_fragidx; 3814c7070dbSScott Long /* constant */ 38295246abbSSean Bruno qidx_t ifl_size; 3834c7070dbSScott Long uint16_t ifl_buf_size; 3844c7070dbSScott Long uint16_t ifl_cltype; 3854c7070dbSScott Long uma_zone_t ifl_zone; 386e035717eSSean Bruno iflib_rxsd_array_t ifl_sds; 3874c7070dbSScott Long iflib_rxq_t ifl_rxq; 3884c7070dbSScott Long uint8_t ifl_id; 389bfce461eSMarius Strobl bus_dma_tag_t ifl_buf_tag; 3904c7070dbSScott Long iflib_dma_info_t ifl_ifdi; 3914c7070dbSScott Long uint64_t ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE); 3924c7070dbSScott Long caddr_t ifl_vm_addrs[IFLIB_MAX_RX_REFRESH]; 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; 4374c7070dbSScott Long struct iflib_filter_info ifr_filter_info; 4384c7070dbSScott Long iflib_dma_info_t ifr_ifdi; 439ab2e3f79SStephen Hurd 4404c7070dbSScott Long /* dynamically allocate if any drivers need a value substantially larger than this */ 4414c7070dbSScott Long struct if_rxd_frag ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE); 4421248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 4431248952aSSean Bruno uint64_t ifr_cpu_exec_count[256]; 4441248952aSSean Bruno #endif 4454c7070dbSScott Long } __aligned(CACHE_LINE_SIZE); 4464c7070dbSScott Long 44795246abbSSean Bruno typedef struct if_rxsd { 44895246abbSSean Bruno caddr_t *ifsd_cl; 44995246abbSSean Bruno iflib_fl_t ifsd_fl; 45095246abbSSean Bruno qidx_t ifsd_cidx; 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 574ab2e3f79SStephen Hurd static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD, 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); 7054c7070dbSScott Long static void iflib_init_locked(if_ctx_t ctx); 7064c7070dbSScott Long static void iflib_add_device_sysctl_pre(if_ctx_t ctx); 7074c7070dbSScott Long static void iflib_add_device_sysctl_post(if_ctx_t ctx); 708da69b8f9SSean Bruno static void iflib_ifmp_purge(iflib_txq_t txq); 7091248952aSSean Bruno static void _iflib_pre_assert(if_softc_ctx_t scctx); 71095246abbSSean Bruno static void iflib_if_init_locked(if_ctx_t ctx); 71177c1fcecSEric Joyner static void iflib_free_intr_mem(if_ctx_t ctx); 71295246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 71395246abbSSean Bruno static struct mbuf * iflib_fixup_rx(struct mbuf *m); 71495246abbSSean Bruno #endif 7154c7070dbSScott Long 716f154ece0SStephen Hurd static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets = 717f154ece0SStephen Hurd SLIST_HEAD_INITIALIZER(cpu_offsets); 718f154ece0SStephen Hurd struct cpu_offset { 719f154ece0SStephen Hurd SLIST_ENTRY(cpu_offset) entries; 720f154ece0SStephen Hurd cpuset_t set; 721f154ece0SStephen Hurd unsigned int refcount; 722f154ece0SStephen Hurd uint16_t offset; 723f154ece0SStephen Hurd }; 724f154ece0SStephen Hurd static struct mtx cpu_offset_mtx; 725f154ece0SStephen Hurd MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock", 726f154ece0SStephen Hurd MTX_DEF); 727f154ece0SStephen Hurd 72894618825SMark Johnston NETDUMP_DEFINE(iflib); 72994618825SMark Johnston 7304c7070dbSScott Long #ifdef DEV_NETMAP 7314c7070dbSScott Long #include <sys/selinfo.h> 7324c7070dbSScott Long #include <net/netmap.h> 7334c7070dbSScott Long #include <dev/netmap/netmap_kern.h> 7344c7070dbSScott Long 7354c7070dbSScott Long MODULE_DEPEND(iflib, netmap, 1, 1, 1); 7364c7070dbSScott Long 7372d873474SStephen Hurd static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init); 7382d873474SStephen Hurd 7394c7070dbSScott Long /* 7404c7070dbSScott Long * device-specific sysctl variables: 7414c7070dbSScott Long * 74291d546a0SConrad Meyer * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it. 7434c7070dbSScott Long * During regular operations the CRC is stripped, but on some 7444c7070dbSScott Long * hardware reception of frames not multiple of 64 is slower, 7454c7070dbSScott Long * so using crcstrip=0 helps in benchmarks. 7464c7070dbSScott Long * 74791d546a0SConrad Meyer * iflib_rx_miss, iflib_rx_miss_bufs: 7484c7070dbSScott Long * count packets that might be missed due to lost interrupts. 7494c7070dbSScott Long */ 7504c7070dbSScott Long SYSCTL_DECL(_dev_netmap); 7514c7070dbSScott Long /* 7524c7070dbSScott Long * The xl driver by default strips CRCs and we do not override it. 7534c7070dbSScott Long */ 7544c7070dbSScott Long 7554c7070dbSScott Long int iflib_crcstrip = 1; 7564c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip, 7571722eeacSMarius Strobl CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames"); 7584c7070dbSScott Long 7594c7070dbSScott Long int iflib_rx_miss, iflib_rx_miss_bufs; 7604c7070dbSScott Long SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss, 7611722eeacSMarius Strobl CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr"); 76291d546a0SConrad Meyer SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs, 7631722eeacSMarius Strobl CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs"); 7644c7070dbSScott Long 7654c7070dbSScott Long /* 7664c7070dbSScott Long * Register/unregister. We are already under netmap lock. 7674c7070dbSScott Long * Only called on the first register or the last unregister. 7684c7070dbSScott Long */ 7694c7070dbSScott Long static int 7704c7070dbSScott Long iflib_netmap_register(struct netmap_adapter *na, int onoff) 7714c7070dbSScott Long { 7721722eeacSMarius Strobl if_t ifp = na->ifp; 7734c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 77495246abbSSean Bruno int status; 7754c7070dbSScott Long 7764c7070dbSScott Long CTX_LOCK(ctx); 7774c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 7784c7070dbSScott Long 7794c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 7804c7070dbSScott Long ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 7814c7070dbSScott Long 7824c7070dbSScott Long if (!CTX_IS_VF(ctx)) 7831248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); 7844c7070dbSScott Long 7854c7070dbSScott Long /* enable or disable flags and callbacks in na and ifp */ 7864c7070dbSScott Long if (onoff) { 7874c7070dbSScott Long nm_set_native_flags(na); 7884c7070dbSScott Long } else { 7894c7070dbSScott Long nm_clear_native_flags(na); 7904c7070dbSScott Long } 79195246abbSSean Bruno iflib_stop(ctx); 79295246abbSSean Bruno iflib_init_locked(ctx); 7931248952aSSean Bruno IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ? 79495246abbSSean Bruno status = ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1; 79595246abbSSean Bruno if (status) 79695246abbSSean Bruno nm_clear_native_flags(na); 7974c7070dbSScott Long CTX_UNLOCK(ctx); 79895246abbSSean Bruno return (status); 7994c7070dbSScott Long } 8004c7070dbSScott Long 8012d873474SStephen Hurd static int 8022d873474SStephen Hurd netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, uint32_t nm_i, bool init) 8032d873474SStephen Hurd { 8042d873474SStephen Hurd struct netmap_adapter *na = kring->na; 8052d873474SStephen Hurd u_int const lim = kring->nkr_num_slots - 1; 8062d873474SStephen Hurd u_int head = kring->rhead; 8072d873474SStephen Hurd struct netmap_ring *ring = kring->ring; 8082d873474SStephen Hurd bus_dmamap_t *map; 8092d873474SStephen Hurd struct if_rxd_update iru; 8102d873474SStephen Hurd if_ctx_t ctx = rxq->ifr_ctx; 8112d873474SStephen Hurd iflib_fl_t fl = &rxq->ifr_fl[0]; 8122d873474SStephen Hurd uint32_t refill_pidx, nic_i; 81364e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 81464e6fc13SStephen Hurd int rf_count = 0; 81564e6fc13SStephen Hurd #endif 8162d873474SStephen Hurd 8172d873474SStephen Hurd if (nm_i == head && __predict_true(!init)) 8182d873474SStephen Hurd return 0; 8192d873474SStephen Hurd iru_init(&iru, rxq, 0 /* flid */); 8202d873474SStephen Hurd map = fl->ifl_sds.ifsd_map; 8212d873474SStephen Hurd refill_pidx = netmap_idx_k2n(kring, nm_i); 8222d873474SStephen Hurd /* 8232d873474SStephen Hurd * IMPORTANT: we must leave one free slot in the ring, 8242d873474SStephen Hurd * so move head back by one unit 8252d873474SStephen Hurd */ 8262d873474SStephen Hurd head = nm_prev(head, lim); 8271ae4848cSMatt Macy nic_i = UINT_MAX; 82864e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills); 8292d873474SStephen Hurd while (nm_i != head) { 83064e6fc13SStephen Hurd #if IFLIB_DEBUG_COUNTERS 83164e6fc13SStephen Hurd if (++rf_count == 9) 83264e6fc13SStephen Hurd DBG_COUNTER_INC(fl_refills_large); 83364e6fc13SStephen Hurd #endif 8342d873474SStephen Hurd for (int tmp_pidx = 0; tmp_pidx < IFLIB_MAX_RX_REFRESH && nm_i != head; tmp_pidx++) { 8352d873474SStephen Hurd struct netmap_slot *slot = &ring->slot[nm_i]; 8362d873474SStephen Hurd void *addr = PNMB(na, slot, &fl->ifl_bus_addrs[tmp_pidx]); 8372d873474SStephen Hurd uint32_t nic_i_dma = refill_pidx; 8382d873474SStephen Hurd nic_i = netmap_idx_k2n(kring, nm_i); 8392d873474SStephen Hurd 8402d873474SStephen Hurd MPASS(tmp_pidx < IFLIB_MAX_RX_REFRESH); 8412d873474SStephen Hurd 8422d873474SStephen Hurd if (addr == NETMAP_BUF_BASE(na)) /* bad buf */ 8432d873474SStephen Hurd return netmap_ring_reinit(kring); 8442d873474SStephen Hurd 8452d873474SStephen Hurd fl->ifl_vm_addrs[tmp_pidx] = addr; 84695dcf343SMarius Strobl if (__predict_false(init)) { 84795dcf343SMarius Strobl netmap_load_map(na, fl->ifl_buf_tag, 84895dcf343SMarius Strobl map[nic_i], addr); 84995dcf343SMarius Strobl } else if (slot->flags & NS_BUF_CHANGED) { 8502d873474SStephen Hurd /* buffer has changed, reload map */ 85195dcf343SMarius Strobl netmap_reload_map(na, fl->ifl_buf_tag, 85295dcf343SMarius Strobl map[nic_i], addr); 8532d873474SStephen Hurd } 8542d873474SStephen Hurd slot->flags &= ~NS_BUF_CHANGED; 8552d873474SStephen Hurd 8562d873474SStephen Hurd nm_i = nm_next(nm_i, lim); 8572d873474SStephen Hurd fl->ifl_rxd_idxs[tmp_pidx] = nic_i = nm_next(nic_i, lim); 8582d873474SStephen Hurd if (nm_i != head && tmp_pidx < IFLIB_MAX_RX_REFRESH-1) 8592d873474SStephen Hurd continue; 8602d873474SStephen Hurd 8612d873474SStephen Hurd iru.iru_pidx = refill_pidx; 8622d873474SStephen Hurd iru.iru_count = tmp_pidx+1; 8632d873474SStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 8642d873474SStephen Hurd refill_pidx = nic_i; 8652d873474SStephen Hurd for (int n = 0; n < iru.iru_count; n++) { 86695dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i_dma], 8672d873474SStephen Hurd BUS_DMASYNC_PREREAD); 8682d873474SStephen Hurd /* XXX - change this to not use the netmap func*/ 8692d873474SStephen Hurd nic_i_dma = nm_next(nic_i_dma, lim); 8702d873474SStephen Hurd } 8712d873474SStephen Hurd } 8722d873474SStephen Hurd } 8732d873474SStephen Hurd kring->nr_hwcur = head; 8742d873474SStephen Hurd 8752d873474SStephen Hurd bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 8762d873474SStephen Hurd BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 87764e6fc13SStephen Hurd if (__predict_true(nic_i != UINT_MAX)) { 8782d873474SStephen Hurd ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id, nic_i); 87964e6fc13SStephen Hurd DBG_COUNTER_INC(rxd_flush); 88064e6fc13SStephen Hurd } 8812d873474SStephen Hurd return (0); 8822d873474SStephen Hurd } 8832d873474SStephen Hurd 8844c7070dbSScott Long /* 8854c7070dbSScott Long * Reconcile kernel and user view of the transmit ring. 8864c7070dbSScott Long * 8874c7070dbSScott Long * All information is in the kring. 8884c7070dbSScott Long * Userspace wants to send packets up to the one before kring->rhead, 8894c7070dbSScott Long * kernel knows kring->nr_hwcur is the first unsent packet. 8904c7070dbSScott Long * 8914c7070dbSScott Long * Here we push packets out (as many as possible), and possibly 8924c7070dbSScott Long * reclaim buffers from previously completed transmission. 8934c7070dbSScott Long * 8944c7070dbSScott Long * The caller (netmap) guarantees that there is only one instance 8954c7070dbSScott Long * running at any time. Any interference with other driver 8964c7070dbSScott Long * methods should be handled by the individual drivers. 8974c7070dbSScott Long */ 8984c7070dbSScott Long static int 8994c7070dbSScott Long iflib_netmap_txsync(struct netmap_kring *kring, int flags) 9004c7070dbSScott Long { 9014c7070dbSScott Long struct netmap_adapter *na = kring->na; 9021722eeacSMarius Strobl if_t ifp = na->ifp; 9034c7070dbSScott Long struct netmap_ring *ring = kring->ring; 904dd7fbcf1SStephen Hurd u_int nm_i; /* index into the netmap kring */ 9054c7070dbSScott Long u_int nic_i; /* index into the NIC ring */ 9064c7070dbSScott Long u_int n; 9074c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 9084c7070dbSScott Long u_int const head = kring->rhead; 9094c7070dbSScott Long struct if_pkt_info pi; 9104c7070dbSScott Long 9114c7070dbSScott Long /* 9124c7070dbSScott Long * interrupts on every tx packet are expensive so request 9134c7070dbSScott Long * them every half ring, or where NS_REPORT is set 9144c7070dbSScott Long */ 9154c7070dbSScott Long u_int report_frequency = kring->nkr_num_slots >> 1; 9164c7070dbSScott Long /* device-specific */ 9174c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 9184c7070dbSScott Long iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id]; 9194c7070dbSScott Long 92095dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 9214c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 9224c7070dbSScott Long 9234c7070dbSScott Long /* 9244c7070dbSScott Long * First part: process new packets to send. 925dd7fbcf1SStephen Hurd * nm_i is the current index in the netmap kring, 9264c7070dbSScott Long * nic_i is the corresponding index in the NIC ring. 9274c7070dbSScott Long * 9284c7070dbSScott Long * If we have packets to send (nm_i != head) 9294c7070dbSScott Long * iterate over the netmap ring, fetch length and update 9304c7070dbSScott Long * the corresponding slot in the NIC ring. Some drivers also 9314c7070dbSScott Long * need to update the buffer's physical address in the NIC slot 9324c7070dbSScott Long * even NS_BUF_CHANGED is not set (PNMB computes the addresses). 9334c7070dbSScott Long * 9344c7070dbSScott Long * The netmap_reload_map() calls is especially expensive, 9354c7070dbSScott Long * even when (as in this case) the tag is 0, so do only 9364c7070dbSScott Long * when the buffer has actually changed. 9374c7070dbSScott Long * 9384c7070dbSScott Long * If possible do not set the report/intr bit on all slots, 9394c7070dbSScott Long * but only a few times per ring or when NS_REPORT is set. 9404c7070dbSScott Long * 9414c7070dbSScott Long * Finally, on 10G and faster drivers, it might be useful 9424c7070dbSScott Long * to prefetch the next slot and txr entry. 9434c7070dbSScott Long */ 9444c7070dbSScott Long 945dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 9465ee36c68SStephen Hurd if (nm_i != head) { /* we have new packets to send */ 94795246abbSSean Bruno pkt_info_zero(&pi); 94895246abbSSean Bruno pi.ipi_segs = txq->ift_segs; 94995246abbSSean Bruno pi.ipi_qsidx = kring->ring_id; 9504c7070dbSScott Long nic_i = netmap_idx_k2n(kring, nm_i); 9514c7070dbSScott Long 9524c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i]); 9534c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]); 9544c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]); 9554c7070dbSScott Long 9564c7070dbSScott Long for (n = 0; nm_i != head; n++) { 9574c7070dbSScott Long struct netmap_slot *slot = &ring->slot[nm_i]; 9584c7070dbSScott Long u_int len = slot->len; 9590a1b74a3SSean Bruno uint64_t paddr; 9604c7070dbSScott Long void *addr = PNMB(na, slot, &paddr); 9614c7070dbSScott Long int flags = (slot->flags & NS_REPORT || 9624c7070dbSScott Long nic_i == 0 || nic_i == report_frequency) ? 9634c7070dbSScott Long IPI_TX_INTR : 0; 9644c7070dbSScott Long 9654c7070dbSScott Long /* device-specific */ 96695246abbSSean Bruno pi.ipi_len = len; 96795246abbSSean Bruno pi.ipi_segs[0].ds_addr = paddr; 96895246abbSSean Bruno pi.ipi_segs[0].ds_len = len; 96995246abbSSean Bruno pi.ipi_nsegs = 1; 97095246abbSSean Bruno pi.ipi_ndescs = 0; 9714c7070dbSScott Long pi.ipi_pidx = nic_i; 9724c7070dbSScott Long pi.ipi_flags = flags; 9734c7070dbSScott Long 9744c7070dbSScott Long /* Fill the slot in the NIC ring. */ 9754c7070dbSScott Long ctx->isc_txd_encap(ctx->ifc_softc, &pi); 97664e6fc13SStephen Hurd DBG_COUNTER_INC(tx_encap); 9774c7070dbSScott Long 9784c7070dbSScott Long /* prefetch for next round */ 9794c7070dbSScott Long __builtin_prefetch(&ring->slot[nm_i + 1]); 9804c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]); 9814c7070dbSScott Long __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]); 9824c7070dbSScott Long 9834c7070dbSScott Long NM_CHECK_ADDR_LEN(na, addr, len); 9844c7070dbSScott Long 9854c7070dbSScott Long if (slot->flags & NS_BUF_CHANGED) { 9864c7070dbSScott Long /* buffer has changed, reload map */ 987bfce461eSMarius Strobl netmap_reload_map(na, txq->ift_buf_tag, 988bfce461eSMarius Strobl txq->ift_sds.ifsd_map[nic_i], addr); 9894c7070dbSScott Long } 9904c7070dbSScott Long /* make sure changes to the buffer are synced */ 99195dcf343SMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 99295dcf343SMarius Strobl txq->ift_sds.ifsd_map[nic_i], 9934c7070dbSScott Long BUS_DMASYNC_PREWRITE); 99495dcf343SMarius Strobl 99595246abbSSean Bruno slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED); 9964c7070dbSScott Long nm_i = nm_next(nm_i, lim); 9974c7070dbSScott Long nic_i = nm_next(nic_i, lim); 9984c7070dbSScott Long } 999dd7fbcf1SStephen Hurd kring->nr_hwcur = nm_i; 10004c7070dbSScott Long 10014c7070dbSScott Long /* synchronize the NIC ring */ 100295dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 10034c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 10044c7070dbSScott Long 10054c7070dbSScott Long /* (re)start the tx unit up to slot nic_i (excluded) */ 10064c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i); 10074c7070dbSScott Long } 10084c7070dbSScott Long 10094c7070dbSScott Long /* 10104c7070dbSScott Long * Second part: reclaim buffers for completed transmissions. 10115ee36c68SStephen Hurd * 10125ee36c68SStephen Hurd * If there are unclaimed buffers, attempt to reclaim them. 10135ee36c68SStephen Hurd * If none are reclaimed, and TX IRQs are not in use, do an initial 10145ee36c68SStephen Hurd * minimal delay, then trigger the tx handler which will spin in the 10155ee36c68SStephen Hurd * group task queue. 10164c7070dbSScott Long */ 1017dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 10184c7070dbSScott Long if (iflib_tx_credits_update(ctx, txq)) { 10194c7070dbSScott Long /* some tx completed, increment avail */ 10204c7070dbSScott Long nic_i = txq->ift_cidx_processed; 10214c7070dbSScott Long kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim); 10224c7070dbSScott Long } 10235ee36c68SStephen Hurd } 1024dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) 1025dd7fbcf1SStephen Hurd if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) { 1026dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, hz < 2000 ? 1 : hz / 1000, 1027dd7fbcf1SStephen Hurd iflib_timer, txq, txq->ift_timer.c_cpu); 10285ee36c68SStephen Hurd } 10294c7070dbSScott Long return (0); 10304c7070dbSScott Long } 10314c7070dbSScott Long 10324c7070dbSScott Long /* 10334c7070dbSScott Long * Reconcile kernel and user view of the receive ring. 10344c7070dbSScott Long * Same as for the txsync, this routine must be efficient. 10354c7070dbSScott Long * The caller guarantees a single invocations, but races against 10364c7070dbSScott Long * the rest of the driver should be handled here. 10374c7070dbSScott Long * 10384c7070dbSScott Long * On call, kring->rhead is the first packet that userspace wants 10394c7070dbSScott Long * to keep, and kring->rcur is the wakeup point. 10404c7070dbSScott Long * The kernel has previously reported packets up to kring->rtail. 10414c7070dbSScott Long * 10424c7070dbSScott Long * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective 10434c7070dbSScott Long * of whether or not we received an interrupt. 10444c7070dbSScott Long */ 10454c7070dbSScott Long static int 10464c7070dbSScott Long iflib_netmap_rxsync(struct netmap_kring *kring, int flags) 10474c7070dbSScott Long { 10484c7070dbSScott Long struct netmap_adapter *na = kring->na; 10494c7070dbSScott Long struct netmap_ring *ring = kring->ring; 10501722eeacSMarius Strobl if_t ifp = na->ifp; 105195dcf343SMarius Strobl iflib_fl_t fl; 105295246abbSSean Bruno uint32_t nm_i; /* index into the netmap ring */ 10532d873474SStephen Hurd uint32_t nic_i; /* index into the NIC ring */ 10544c7070dbSScott Long u_int i, n; 10554c7070dbSScott Long u_int const lim = kring->nkr_num_slots - 1; 1056dd7fbcf1SStephen Hurd u_int const head = kring->rhead; 10574c7070dbSScott Long int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR; 1058ab2e3f79SStephen Hurd struct if_rxd_info ri; 105995246abbSSean Bruno 10604c7070dbSScott Long if_ctx_t ctx = ifp->if_softc; 10614c7070dbSScott Long iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id]; 10624c7070dbSScott Long if (head > lim) 10634c7070dbSScott Long return netmap_ring_reinit(kring); 10644c7070dbSScott Long 106595dcf343SMarius Strobl /* 106695dcf343SMarius Strobl * XXX netmap_fl_refill() only ever (re)fills free list 0 so far. 106795dcf343SMarius Strobl */ 106895dcf343SMarius Strobl 106995246abbSSean Bruno for (i = 0, fl = rxq->ifr_fl; i < rxq->ifr_nfl; i++, fl++) { 107095dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 10714c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 107295246abbSSean Bruno } 107395dcf343SMarius Strobl 10744c7070dbSScott Long /* 10754c7070dbSScott Long * First part: import newly received packets. 10764c7070dbSScott Long * 10774c7070dbSScott Long * nm_i is the index of the next free slot in the netmap ring, 10784c7070dbSScott Long * nic_i is the index of the next received packet in the NIC ring, 10794c7070dbSScott Long * and they may differ in case if_init() has been called while 10804c7070dbSScott Long * in netmap mode. For the receive ring we have 10814c7070dbSScott Long * 10824c7070dbSScott Long * nic_i = rxr->next_check; 10834c7070dbSScott Long * nm_i = kring->nr_hwtail (previous) 10844c7070dbSScott Long * and 10854c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 10864c7070dbSScott Long * 10874c7070dbSScott Long * rxr->next_check is set to 0 on a ring reinit 10884c7070dbSScott Long */ 10894c7070dbSScott Long if (netmap_no_pendintr || force_update) { 10904c7070dbSScott Long int crclen = iflib_crcstrip ? 0 : 4; 10914c7070dbSScott Long int error, avail; 10924c7070dbSScott Long 10932d873474SStephen Hurd for (i = 0; i < rxq->ifr_nfl; i++) { 10942d873474SStephen Hurd fl = &rxq->ifr_fl[i]; 10954c7070dbSScott Long nic_i = fl->ifl_cidx; 10964c7070dbSScott Long nm_i = netmap_idx_n2k(kring, nic_i); 109795dcf343SMarius Strobl avail = ctx->isc_rxd_available(ctx->ifc_softc, 109895dcf343SMarius Strobl rxq->ifr_id, nic_i, USHRT_MAX); 10994c7070dbSScott Long for (n = 0; avail > 0; n++, avail--) { 1100ab2e3f79SStephen Hurd rxd_info_zero(&ri); 1101ab2e3f79SStephen Hurd ri.iri_frags = rxq->ifr_frags; 1102ab2e3f79SStephen Hurd ri.iri_qsidx = kring->ring_id; 1103ab2e3f79SStephen Hurd ri.iri_ifp = ctx->ifc_ifp; 1104ab2e3f79SStephen Hurd ri.iri_cidx = nic_i; 110595246abbSSean Bruno 1106ab2e3f79SStephen Hurd error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 1107ab2e3f79SStephen Hurd ring->slot[nm_i].len = error ? 0 : ri.iri_len - crclen; 11087cb7c6e3SNavdeep Parhar ring->slot[nm_i].flags = 0; 110995dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, 1110e035717eSSean Bruno fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD); 11114c7070dbSScott Long nm_i = nm_next(nm_i, lim); 11124c7070dbSScott Long nic_i = nm_next(nic_i, lim); 11134c7070dbSScott Long } 11144c7070dbSScott Long if (n) { /* update the state variables */ 11154c7070dbSScott Long if (netmap_no_pendintr && !force_update) { 11164c7070dbSScott Long /* diagnostics */ 11174c7070dbSScott Long iflib_rx_miss ++; 11184c7070dbSScott Long iflib_rx_miss_bufs += n; 11194c7070dbSScott Long } 11204c7070dbSScott Long fl->ifl_cidx = nic_i; 1121dd7fbcf1SStephen Hurd kring->nr_hwtail = nm_i; 11224c7070dbSScott Long } 11234c7070dbSScott Long kring->nr_kflags &= ~NKR_PENDINTR; 11244c7070dbSScott Long } 11254c7070dbSScott Long } 11264c7070dbSScott Long /* 11274c7070dbSScott Long * Second part: skip past packets that userspace has released. 11284c7070dbSScott Long * (kring->nr_hwcur to head excluded), 11294c7070dbSScott Long * and make the buffers available for reception. 11304c7070dbSScott Long * As usual nm_i is the index in the netmap ring, 11314c7070dbSScott Long * nic_i is the index in the NIC ring, and 11324c7070dbSScott Long * nm_i == (nic_i + kring->nkr_hwofs) % ring_size 11334c7070dbSScott Long */ 11344c7070dbSScott Long /* XXX not sure how this will work with multiple free lists */ 1135dd7fbcf1SStephen Hurd nm_i = kring->nr_hwcur; 113695246abbSSean Bruno 11372d873474SStephen Hurd return (netmap_fl_refill(rxq, kring, nm_i, false)); 11384c7070dbSScott Long } 11394c7070dbSScott Long 114095246abbSSean Bruno static void 114195246abbSSean Bruno iflib_netmap_intr(struct netmap_adapter *na, int onoff) 114295246abbSSean Bruno { 11431722eeacSMarius Strobl if_ctx_t ctx = na->ifp->if_softc; 114495246abbSSean Bruno 1145ab2e3f79SStephen Hurd CTX_LOCK(ctx); 114695246abbSSean Bruno if (onoff) { 114795246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 114895246abbSSean Bruno } else { 114995246abbSSean Bruno IFDI_INTR_DISABLE(ctx); 115095246abbSSean Bruno } 1151ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 115295246abbSSean Bruno } 115395246abbSSean Bruno 115495246abbSSean Bruno 11554c7070dbSScott Long static int 11564c7070dbSScott Long iflib_netmap_attach(if_ctx_t ctx) 11574c7070dbSScott Long { 11584c7070dbSScott Long struct netmap_adapter na; 115923ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 11604c7070dbSScott Long 11614c7070dbSScott Long bzero(&na, sizeof(na)); 11624c7070dbSScott Long 11634c7070dbSScott Long na.ifp = ctx->ifc_ifp; 11644c7070dbSScott Long na.na_flags = NAF_BDG_MAYSLEEP; 11654c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_ntxqsets); 11664c7070dbSScott Long MPASS(ctx->ifc_softc_ctx.isc_nrxqsets); 11674c7070dbSScott Long 116823ac9029SStephen Hurd na.num_tx_desc = scctx->isc_ntxd[0]; 116923ac9029SStephen Hurd na.num_rx_desc = scctx->isc_nrxd[0]; 11704c7070dbSScott Long na.nm_txsync = iflib_netmap_txsync; 11714c7070dbSScott Long na.nm_rxsync = iflib_netmap_rxsync; 11724c7070dbSScott Long na.nm_register = iflib_netmap_register; 117395246abbSSean Bruno na.nm_intr = iflib_netmap_intr; 11744c7070dbSScott Long na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets; 11754c7070dbSScott Long na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets; 11764c7070dbSScott Long return (netmap_attach(&na)); 11774c7070dbSScott Long } 11784c7070dbSScott Long 11794c7070dbSScott Long static void 11804c7070dbSScott Long iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq) 11814c7070dbSScott Long { 11824c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 11834c7070dbSScott Long struct netmap_slot *slot; 11844c7070dbSScott Long 11854c7070dbSScott Long slot = netmap_reset(na, NR_TX, txq->ift_id, 0); 1186e099b90bSPedro F. Giffuni if (slot == NULL) 11874c7070dbSScott Long return; 118823ac9029SStephen Hurd for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) { 11894c7070dbSScott Long 11904c7070dbSScott Long /* 11914c7070dbSScott Long * In netmap mode, set the map for the packet buffer. 11924c7070dbSScott Long * NOTE: Some drivers (not this one) also need to set 11934c7070dbSScott Long * the physical buffer address in the NIC ring. 11944c7070dbSScott Long * netmap_idx_n2k() maps a nic index, i, into the corresponding 11954c7070dbSScott Long * netmap slot index, si 11964c7070dbSScott Long */ 11972ff91c17SVincenzo Maffione int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i); 1198bfce461eSMarius Strobl netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i], 1199bfce461eSMarius Strobl NMB(na, slot + si)); 12004c7070dbSScott Long } 12014c7070dbSScott Long } 12022d873474SStephen Hurd 12034c7070dbSScott Long static void 12044c7070dbSScott Long iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq) 12054c7070dbSScott Long { 12064c7070dbSScott Long struct netmap_adapter *na = NA(ctx->ifc_ifp); 12072ff91c17SVincenzo Maffione struct netmap_kring *kring = na->rx_rings[rxq->ifr_id]; 12084c7070dbSScott Long struct netmap_slot *slot; 12092d873474SStephen Hurd uint32_t nm_i; 12104c7070dbSScott Long 12114c7070dbSScott Long slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0); 1212e099b90bSPedro F. Giffuni if (slot == NULL) 12134c7070dbSScott Long return; 12142d873474SStephen Hurd nm_i = netmap_idx_n2k(kring, 0); 12152d873474SStephen Hurd netmap_fl_refill(rxq, kring, nm_i, true); 12164c7070dbSScott Long } 12174c7070dbSScott Long 1218dd7fbcf1SStephen Hurd static void 121995dcf343SMarius Strobl iflib_netmap_timer_adjust(if_ctx_t ctx, iflib_txq_t txq, uint32_t *reset_on) 1220dd7fbcf1SStephen Hurd { 1221dd7fbcf1SStephen Hurd struct netmap_kring *kring; 122295dcf343SMarius Strobl uint16_t txqid; 1223dd7fbcf1SStephen Hurd 122495dcf343SMarius Strobl txqid = txq->ift_id; 1225dd7fbcf1SStephen Hurd kring = NA(ctx->ifc_ifp)->tx_rings[txqid]; 1226dd7fbcf1SStephen Hurd 1227dd7fbcf1SStephen Hurd if (kring->nr_hwcur != nm_next(kring->nr_hwtail, kring->nkr_num_slots - 1)) { 122895dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 122995dcf343SMarius Strobl BUS_DMASYNC_POSTREAD); 1230dd7fbcf1SStephen Hurd if (ctx->isc_txd_credits_update(ctx->ifc_softc, txqid, false)) 1231dd7fbcf1SStephen Hurd netmap_tx_irq(ctx->ifc_ifp, txqid); 1232dd7fbcf1SStephen Hurd if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ)) { 1233dd7fbcf1SStephen Hurd if (hz < 2000) 1234dd7fbcf1SStephen Hurd *reset_on = 1; 1235dd7fbcf1SStephen Hurd else 1236dd7fbcf1SStephen Hurd *reset_on = hz / 1000; 1237dd7fbcf1SStephen Hurd } 1238dd7fbcf1SStephen Hurd } 1239dd7fbcf1SStephen Hurd } 1240dd7fbcf1SStephen Hurd 12414c7070dbSScott Long #define iflib_netmap_detach(ifp) netmap_detach(ifp) 12424c7070dbSScott Long 12434c7070dbSScott Long #else 12444c7070dbSScott Long #define iflib_netmap_txq_init(ctx, txq) 12454c7070dbSScott Long #define iflib_netmap_rxq_init(ctx, rxq) 12464c7070dbSScott Long #define iflib_netmap_detach(ifp) 12474c7070dbSScott Long 12484c7070dbSScott Long #define iflib_netmap_attach(ctx) (0) 12494c7070dbSScott Long #define netmap_rx_irq(ifp, qid, budget) (0) 125095246abbSSean Bruno #define netmap_tx_irq(ifp, qid) do {} while (0) 125195dcf343SMarius Strobl #define iflib_netmap_timer_adjust(ctx, txq, reset_on) 12524c7070dbSScott Long #endif 12534c7070dbSScott Long 12544c7070dbSScott Long #if defined(__i386__) || defined(__amd64__) 12554c7070dbSScott Long static __inline void 12564c7070dbSScott Long prefetch(void *x) 12574c7070dbSScott Long { 12584c7070dbSScott Long __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 12594c7070dbSScott Long } 12603429c02fSStephen Hurd static __inline void 12613429c02fSStephen Hurd prefetch2cachelines(void *x) 12623429c02fSStephen Hurd { 12633429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 12643429c02fSStephen Hurd #if (CACHE_LINE_SIZE < 128) 12653429c02fSStephen Hurd __asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long))))); 12663429c02fSStephen Hurd #endif 12673429c02fSStephen Hurd } 12684c7070dbSScott Long #else 12694c7070dbSScott Long #define prefetch(x) 12703429c02fSStephen Hurd #define prefetch2cachelines(x) 12714c7070dbSScott Long #endif 12724c7070dbSScott Long 12734c7070dbSScott Long static void 127410e0d938SStephen Hurd iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid) 127510e0d938SStephen Hurd { 127610e0d938SStephen Hurd iflib_fl_t fl; 127710e0d938SStephen Hurd 127810e0d938SStephen Hurd fl = &rxq->ifr_fl[flid]; 127910e0d938SStephen Hurd iru->iru_paddrs = fl->ifl_bus_addrs; 128010e0d938SStephen Hurd iru->iru_vaddrs = &fl->ifl_vm_addrs[0]; 128110e0d938SStephen Hurd iru->iru_idxs = fl->ifl_rxd_idxs; 128210e0d938SStephen Hurd iru->iru_qsidx = rxq->ifr_id; 128310e0d938SStephen Hurd iru->iru_buf_size = fl->ifl_buf_size; 128410e0d938SStephen Hurd iru->iru_flidx = fl->ifl_id; 128510e0d938SStephen Hurd } 128610e0d938SStephen Hurd 128710e0d938SStephen Hurd static void 12884c7070dbSScott Long _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err) 12894c7070dbSScott Long { 12904c7070dbSScott Long if (err) 12914c7070dbSScott Long return; 12924c7070dbSScott Long *(bus_addr_t *) arg = segs[0].ds_addr; 12934c7070dbSScott Long } 12944c7070dbSScott Long 12954c7070dbSScott Long int 12968f82136aSPatrick Kelsey iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags) 12974c7070dbSScott Long { 12984c7070dbSScott Long int err; 12994c7070dbSScott Long device_t dev = ctx->ifc_dev; 13004c7070dbSScott Long 13014c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 13028f82136aSPatrick Kelsey align, 0, /* alignment, bounds */ 13034c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 13044c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 13054c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 13064c7070dbSScott Long size, /* maxsize */ 13074c7070dbSScott Long 1, /* nsegments */ 13084c7070dbSScott Long size, /* maxsegsize */ 13094c7070dbSScott Long BUS_DMA_ALLOCNOW, /* flags */ 13104c7070dbSScott Long NULL, /* lockfunc */ 13114c7070dbSScott Long NULL, /* lockarg */ 13124c7070dbSScott Long &dma->idi_tag); 13134c7070dbSScott Long if (err) { 13144c7070dbSScott Long device_printf(dev, 13154c7070dbSScott Long "%s: bus_dma_tag_create failed: %d\n", 13164c7070dbSScott Long __func__, err); 13174c7070dbSScott Long goto fail_0; 13184c7070dbSScott Long } 13194c7070dbSScott Long 13204c7070dbSScott Long err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr, 13214c7070dbSScott Long BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map); 13224c7070dbSScott Long if (err) { 13234c7070dbSScott Long device_printf(dev, 13244c7070dbSScott Long "%s: bus_dmamem_alloc(%ju) failed: %d\n", 13254c7070dbSScott Long __func__, (uintmax_t)size, err); 13264c7070dbSScott Long goto fail_1; 13274c7070dbSScott Long } 13284c7070dbSScott Long 13294c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 13304c7070dbSScott Long err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr, 13314c7070dbSScott Long size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT); 13324c7070dbSScott Long if (err || dma->idi_paddr == IF_BAD_DMA) { 13334c7070dbSScott Long device_printf(dev, 13344c7070dbSScott Long "%s: bus_dmamap_load failed: %d\n", 13354c7070dbSScott Long __func__, err); 13364c7070dbSScott Long goto fail_2; 13374c7070dbSScott Long } 13384c7070dbSScott Long 13394c7070dbSScott Long dma->idi_size = size; 13404c7070dbSScott Long return (0); 13414c7070dbSScott Long 13424c7070dbSScott Long fail_2: 13434c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 13444c7070dbSScott Long fail_1: 13454c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 13464c7070dbSScott Long fail_0: 13474c7070dbSScott Long dma->idi_tag = NULL; 13484c7070dbSScott Long 13494c7070dbSScott Long return (err); 13504c7070dbSScott Long } 13514c7070dbSScott Long 13524c7070dbSScott Long int 13538f82136aSPatrick Kelsey iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags) 13548f82136aSPatrick Kelsey { 13558f82136aSPatrick Kelsey if_shared_ctx_t sctx = ctx->ifc_sctx; 13568f82136aSPatrick Kelsey 13578f82136aSPatrick Kelsey KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized")); 13588f82136aSPatrick Kelsey 13598f82136aSPatrick Kelsey return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags)); 13608f82136aSPatrick Kelsey } 13618f82136aSPatrick Kelsey 13628f82136aSPatrick Kelsey int 13634c7070dbSScott Long iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count) 13644c7070dbSScott Long { 13654c7070dbSScott Long int i, err; 13664c7070dbSScott Long iflib_dma_info_t *dmaiter; 13674c7070dbSScott Long 13684c7070dbSScott Long dmaiter = dmalist; 13694c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) { 13704c7070dbSScott Long if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0) 13714c7070dbSScott Long break; 13724c7070dbSScott Long } 13734c7070dbSScott Long if (err) 13744c7070dbSScott Long iflib_dma_free_multi(dmalist, i); 13754c7070dbSScott Long return (err); 13764c7070dbSScott Long } 13774c7070dbSScott Long 13784c7070dbSScott Long void 13794c7070dbSScott Long iflib_dma_free(iflib_dma_info_t dma) 13804c7070dbSScott Long { 13814c7070dbSScott Long if (dma->idi_tag == NULL) 13824c7070dbSScott Long return; 13834c7070dbSScott Long if (dma->idi_paddr != IF_BAD_DMA) { 13844c7070dbSScott Long bus_dmamap_sync(dma->idi_tag, dma->idi_map, 13854c7070dbSScott Long BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 13864c7070dbSScott Long bus_dmamap_unload(dma->idi_tag, dma->idi_map); 13874c7070dbSScott Long dma->idi_paddr = IF_BAD_DMA; 13884c7070dbSScott Long } 13894c7070dbSScott Long if (dma->idi_vaddr != NULL) { 13904c7070dbSScott Long bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map); 13914c7070dbSScott Long dma->idi_vaddr = NULL; 13924c7070dbSScott Long } 13934c7070dbSScott Long bus_dma_tag_destroy(dma->idi_tag); 13944c7070dbSScott Long dma->idi_tag = NULL; 13954c7070dbSScott Long } 13964c7070dbSScott Long 13974c7070dbSScott Long void 13984c7070dbSScott Long iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count) 13994c7070dbSScott Long { 14004c7070dbSScott Long int i; 14014c7070dbSScott Long iflib_dma_info_t *dmaiter = dmalist; 14024c7070dbSScott Long 14034c7070dbSScott Long for (i = 0; i < count; i++, dmaiter++) 14044c7070dbSScott Long iflib_dma_free(*dmaiter); 14054c7070dbSScott Long } 14064c7070dbSScott Long 1407bd84f700SSean Bruno #ifdef EARLY_AP_STARTUP 1408bd84f700SSean Bruno static const int iflib_started = 1; 1409bd84f700SSean Bruno #else 1410bd84f700SSean Bruno /* 1411bd84f700SSean Bruno * We used to abuse the smp_started flag to decide if the queues have been 1412bd84f700SSean Bruno * fully initialized (by late taskqgroup_adjust() calls in a SYSINIT()). 1413bd84f700SSean Bruno * That gave bad races, since the SYSINIT() runs strictly after smp_started 1414bd84f700SSean Bruno * is set. Run a SYSINIT() strictly after that to just set a usable 1415bd84f700SSean Bruno * completion flag. 1416bd84f700SSean Bruno */ 1417bd84f700SSean Bruno 1418bd84f700SSean Bruno static int iflib_started; 1419bd84f700SSean Bruno 1420bd84f700SSean Bruno static void 1421bd84f700SSean Bruno iflib_record_started(void *arg) 1422bd84f700SSean Bruno { 1423bd84f700SSean Bruno iflib_started = 1; 1424bd84f700SSean Bruno } 1425bd84f700SSean Bruno 1426bd84f700SSean Bruno SYSINIT(iflib_record_started, SI_SUB_SMP + 1, SI_ORDER_FIRST, 1427bd84f700SSean Bruno iflib_record_started, NULL); 1428bd84f700SSean Bruno #endif 1429bd84f700SSean Bruno 14304c7070dbSScott Long static int 14314c7070dbSScott Long iflib_fast_intr(void *arg) 14324c7070dbSScott Long { 14334c7070dbSScott Long iflib_filter_info_t info = arg; 14344c7070dbSScott Long struct grouptask *gtask = info->ifi_task; 1435ca62461bSStephen Hurd int result; 1436ca62461bSStephen Hurd 143795246abbSSean Bruno if (!iflib_started) 1438ca62461bSStephen Hurd return (FILTER_STRAY); 143995246abbSSean Bruno 144095246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1441ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1442ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1443ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1444ca62461bSStephen Hurd return (result); 1445ca62461bSStephen Hurd } 144695246abbSSean Bruno 144795246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 144895246abbSSean Bruno return (FILTER_HANDLED); 144995246abbSSean Bruno } 145095246abbSSean Bruno 145195246abbSSean Bruno static int 145295246abbSSean Bruno iflib_fast_intr_rxtx(void *arg) 145395246abbSSean Bruno { 145495246abbSSean Bruno iflib_filter_info_t info = arg; 145595246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 145695dcf343SMarius Strobl if_ctx_t ctx; 145795246abbSSean Bruno iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx; 145895dcf343SMarius Strobl iflib_txq_t txq; 145995dcf343SMarius Strobl void *sc; 1460ca62461bSStephen Hurd int i, cidx, result; 146195dcf343SMarius Strobl qidx_t txqid; 14623d10e9edSMarius Strobl bool intr_enable, intr_legacy; 146395246abbSSean Bruno 146495246abbSSean Bruno if (!iflib_started) 1465ca62461bSStephen Hurd return (FILTER_STRAY); 146695246abbSSean Bruno 146795246abbSSean Bruno DBG_COUNTER_INC(fast_intrs); 1468ca62461bSStephen Hurd if (info->ifi_filter != NULL) { 1469ca62461bSStephen Hurd result = info->ifi_filter(info->ifi_filter_arg); 1470ca62461bSStephen Hurd if ((result & FILTER_SCHEDULE_THREAD) == 0) 1471ca62461bSStephen Hurd return (result); 1472ca62461bSStephen Hurd } 147395246abbSSean Bruno 147495dcf343SMarius Strobl ctx = rxq->ifr_ctx; 147595dcf343SMarius Strobl sc = ctx->ifc_softc; 14763d10e9edSMarius Strobl intr_enable = false; 14773d10e9edSMarius Strobl intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY); 14781ae4848cSMatt Macy MPASS(rxq->ifr_ntxqirq); 147995246abbSSean Bruno for (i = 0; i < rxq->ifr_ntxqirq; i++) { 148095dcf343SMarius Strobl txqid = rxq->ifr_txqid[i]; 148195dcf343SMarius Strobl txq = &ctx->ifc_txqs[txqid]; 148295dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 14838a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 148495dcf343SMarius Strobl if (!ctx->isc_txd_credits_update(sc, txqid, false)) { 14853d10e9edSMarius Strobl if (intr_legacy) 14863d10e9edSMarius Strobl intr_enable = true; 14873d10e9edSMarius Strobl else 148895246abbSSean Bruno IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid); 148995246abbSSean Bruno continue; 149095246abbSSean Bruno } 149195dcf343SMarius Strobl GROUPTASK_ENQUEUE(&txq->ift_task); 149295246abbSSean Bruno } 149395246abbSSean Bruno if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ) 149495246abbSSean Bruno cidx = rxq->ifr_cq_cidx; 149595246abbSSean Bruno else 149695246abbSSean Bruno cidx = rxq->ifr_fl[0].ifl_cidx; 149795246abbSSean Bruno if (iflib_rxd_avail(ctx, rxq, cidx, 1)) 149895246abbSSean Bruno GROUPTASK_ENQUEUE(gtask); 149964e6fc13SStephen Hurd else { 15003d10e9edSMarius Strobl if (intr_legacy) 15013d10e9edSMarius Strobl intr_enable = true; 15023d10e9edSMarius Strobl else 150395246abbSSean Bruno IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 150464e6fc13SStephen Hurd DBG_COUNTER_INC(rx_intr_enables); 150564e6fc13SStephen Hurd } 15063d10e9edSMarius Strobl if (intr_enable) 15073d10e9edSMarius Strobl IFDI_INTR_ENABLE(ctx); 150895246abbSSean Bruno return (FILTER_HANDLED); 150995246abbSSean Bruno } 151095246abbSSean Bruno 151195246abbSSean Bruno 151295246abbSSean Bruno static int 151395246abbSSean Bruno iflib_fast_intr_ctx(void *arg) 151495246abbSSean Bruno { 151595246abbSSean Bruno iflib_filter_info_t info = arg; 151695246abbSSean Bruno struct grouptask *gtask = info->ifi_task; 1517ca62461bSStephen Hurd int result; 15184c7070dbSScott Long 1519bd84f700SSean Bruno if (!iflib_started) 1520ca62461bSStephen Hurd return (FILTER_STRAY); 15211248952aSSean Bruno 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 16974c7070dbSScott Long map = NULL; 16984c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) 16994c7070dbSScott Long map = txq->ift_sds.ifsd_map[i]; 17004c7070dbSScott Long if (map != NULL) { 1701bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE); 1702bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, map); 1703bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_buf_tag, map); 17044c7070dbSScott Long txq->ift_sds.ifsd_map[i] = NULL; 17054c7070dbSScott Long } 17068a04b53dSKonstantin Belousov 17078a04b53dSKonstantin Belousov map = NULL; 17088a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) 17098a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[i]; 17108a04b53dSKonstantin Belousov if (map != NULL) { 1711bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, map, 17128a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 1713bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, map); 1714bfce461eSMarius Strobl bus_dmamap_destroy(txq->ift_tso_buf_tag, map); 17158a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i] = NULL; 17168a04b53dSKonstantin Belousov } 17174c7070dbSScott Long } 17184c7070dbSScott Long 17194c7070dbSScott Long static void 17204c7070dbSScott Long iflib_txq_destroy(iflib_txq_t txq) 17214c7070dbSScott Long { 17224c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 17234c7070dbSScott Long 172423ac9029SStephen Hurd for (int i = 0; i < txq->ift_size; i++) 17254c7070dbSScott Long iflib_txsd_destroy(ctx, txq, i); 17264c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 17274c7070dbSScott Long free(txq->ift_sds.ifsd_map, M_IFLIB); 17284c7070dbSScott Long txq->ift_sds.ifsd_map = NULL; 17294c7070dbSScott Long } 17308a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 17318a04b53dSKonstantin Belousov free(txq->ift_sds.ifsd_tso_map, M_IFLIB); 17328a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map = NULL; 17338a04b53dSKonstantin Belousov } 17344c7070dbSScott Long if (txq->ift_sds.ifsd_m != NULL) { 17354c7070dbSScott Long free(txq->ift_sds.ifsd_m, M_IFLIB); 17364c7070dbSScott Long txq->ift_sds.ifsd_m = NULL; 17374c7070dbSScott Long } 1738bfce461eSMarius Strobl if (txq->ift_buf_tag != NULL) { 1739bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_buf_tag); 1740bfce461eSMarius Strobl txq->ift_buf_tag = NULL; 17414c7070dbSScott Long } 1742bfce461eSMarius Strobl if (txq->ift_tso_buf_tag != NULL) { 1743bfce461eSMarius Strobl bus_dma_tag_destroy(txq->ift_tso_buf_tag); 1744bfce461eSMarius Strobl txq->ift_tso_buf_tag = NULL; 17454c7070dbSScott Long } 17464c7070dbSScott Long } 17474c7070dbSScott Long 17484c7070dbSScott Long static void 17494c7070dbSScott Long iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i) 17504c7070dbSScott Long { 17514c7070dbSScott Long struct mbuf **mp; 17524c7070dbSScott Long 17534c7070dbSScott Long mp = &txq->ift_sds.ifsd_m[i]; 17544c7070dbSScott Long if (*mp == NULL) 17554c7070dbSScott Long return; 17564c7070dbSScott Long 17574c7070dbSScott Long if (txq->ift_sds.ifsd_map != NULL) { 1758bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 17598a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE); 1760bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]); 17618a04b53dSKonstantin Belousov } 17628a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) { 1763bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 17648a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE); 1765bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 17668a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[i]); 17674c7070dbSScott Long } 176823ac9029SStephen Hurd m_free(*mp); 17694c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 17704c7070dbSScott Long *mp = NULL; 17714c7070dbSScott Long } 17724c7070dbSScott Long 17734c7070dbSScott Long static int 17744c7070dbSScott Long iflib_txq_setup(iflib_txq_t txq) 17754c7070dbSScott Long { 17764c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 177723ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 17784d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 17794c7070dbSScott Long iflib_dma_info_t di; 17804c7070dbSScott Long int i; 17814c7070dbSScott Long 17824c7070dbSScott Long /* Set number of descriptors available */ 17834c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 178495246abbSSean Bruno /* XXX make configurable */ 178595246abbSSean Bruno txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ; 17864c7070dbSScott Long 17874c7070dbSScott Long /* Reset indices */ 178895246abbSSean Bruno txq->ift_cidx_processed = 0; 178995246abbSSean Bruno txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0; 179023ac9029SStephen Hurd txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset]; 17914c7070dbSScott Long 17924d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 17934c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 17944c7070dbSScott Long 17954c7070dbSScott Long IFDI_TXQ_SETUP(ctx, txq->ift_id); 17964d261ce2SStephen Hurd for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++) 17974c7070dbSScott Long bus_dmamap_sync(di->idi_tag, di->idi_map, 17984c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 17994c7070dbSScott Long return (0); 18004c7070dbSScott Long } 18014c7070dbSScott Long 18024c7070dbSScott Long /********************************************************************* 18034c7070dbSScott Long * 1804bfce461eSMarius Strobl * Allocate DMA resources for RX buffers as well as memory for the RX 1805bfce461eSMarius Strobl * mbuf map, direct RX cluster pointer map and RX cluster bus address 1806bfce461eSMarius Strobl * map. RX DMA map, RX mbuf map, direct RX cluster pointer map and 1807bfce461eSMarius Strobl * RX cluster map are kept in a iflib_sw_rx_desc_array structure. 1808bfce461eSMarius Strobl * Since we use use one entry in iflib_sw_rx_desc_array per received 1809bfce461eSMarius Strobl * packet, the maximum number of entries we'll need is equal to the 1810bfce461eSMarius Strobl * number of hardware receive descriptors that we've allocated. 18114c7070dbSScott Long * 18124c7070dbSScott Long **********************************************************************/ 18134c7070dbSScott Long static int 18144c7070dbSScott Long iflib_rxsd_alloc(iflib_rxq_t rxq) 18154c7070dbSScott Long { 18164c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 18174c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 181823ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 18194c7070dbSScott Long device_t dev = ctx->ifc_dev; 18204c7070dbSScott Long iflib_fl_t fl; 18214c7070dbSScott Long int err; 18224c7070dbSScott Long 182323ac9029SStephen Hurd MPASS(scctx->isc_nrxd[0] > 0); 182423ac9029SStephen Hurd MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0); 18254c7070dbSScott Long 18264c7070dbSScott Long fl = rxq->ifr_fl; 18274c7070dbSScott Long for (int i = 0; i < rxq->ifr_nfl; i++, fl++) { 182823ac9029SStephen Hurd fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */ 1829bfce461eSMarius Strobl /* Set up DMA tag for RX buffers. */ 18304c7070dbSScott Long err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 18314c7070dbSScott Long 1, 0, /* alignment, bounds */ 18324c7070dbSScott Long BUS_SPACE_MAXADDR, /* lowaddr */ 18334c7070dbSScott Long BUS_SPACE_MAXADDR, /* highaddr */ 18344c7070dbSScott Long NULL, NULL, /* filter, filterarg */ 18354c7070dbSScott Long sctx->isc_rx_maxsize, /* maxsize */ 18364c7070dbSScott Long sctx->isc_rx_nsegments, /* nsegments */ 18374c7070dbSScott Long sctx->isc_rx_maxsegsize, /* maxsegsize */ 18384c7070dbSScott Long 0, /* flags */ 18394c7070dbSScott Long NULL, /* lockfunc */ 18404c7070dbSScott Long NULL, /* lockarg */ 1841bfce461eSMarius Strobl &fl->ifl_buf_tag); 18424c7070dbSScott Long if (err) { 1843bfce461eSMarius Strobl device_printf(dev, 1844bfce461eSMarius Strobl "Unable to allocate RX DMA tag: %d\n", err); 18454c7070dbSScott Long goto fail; 18464c7070dbSScott Long } 1847bfce461eSMarius Strobl 1848bfce461eSMarius Strobl /* Allocate memory for the RX mbuf map. */ 1849e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_m = 1850ac2fffa4SPedro F. Giffuni (struct mbuf **) malloc(sizeof(struct mbuf *) * 1851ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1852bfce461eSMarius Strobl device_printf(dev, 1853bfce461eSMarius Strobl "Unable to allocate RX mbuf map memory\n"); 1854e035717eSSean Bruno err = ENOMEM; 1855e035717eSSean Bruno goto fail; 1856e035717eSSean Bruno } 1857bfce461eSMarius Strobl 1858bfce461eSMarius Strobl /* Allocate memory for the direct RX cluster pointer map. */ 1859e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_cl = 1860ac2fffa4SPedro F. Giffuni (caddr_t *) malloc(sizeof(caddr_t) * 1861ac2fffa4SPedro F. Giffuni scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1862bfce461eSMarius Strobl device_printf(dev, 1863bfce461eSMarius Strobl "Unable to allocate RX cluster map memory\n"); 1864e035717eSSean Bruno err = ENOMEM; 1865e035717eSSean Bruno goto fail; 1866e035717eSSean Bruno } 18674c7070dbSScott Long 1868bfce461eSMarius Strobl /* Allocate memory for the RX cluster bus address map. */ 1869fbec776dSAndrew Gallatin if (!(fl->ifl_sds.ifsd_ba = 1870fbec776dSAndrew Gallatin (bus_addr_t *) malloc(sizeof(bus_addr_t) * 1871fbec776dSAndrew Gallatin scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1872bfce461eSMarius Strobl device_printf(dev, 1873bfce461eSMarius Strobl "Unable to allocate RX bus address map memory\n"); 1874fbec776dSAndrew Gallatin err = ENOMEM; 1875fbec776dSAndrew Gallatin goto fail; 1876fbec776dSAndrew Gallatin } 1877e035717eSSean Bruno 1878bfce461eSMarius Strobl /* 1879bfce461eSMarius Strobl * Create the DMA maps for RX buffers. 1880bfce461eSMarius Strobl */ 1881e035717eSSean Bruno if (!(fl->ifl_sds.ifsd_map = 1882ac2fffa4SPedro F. Giffuni (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) { 1883bfce461eSMarius Strobl device_printf(dev, 1884bfce461eSMarius Strobl "Unable to allocate RX buffer DMA map memory\n"); 1885e035717eSSean Bruno err = ENOMEM; 1886e035717eSSean Bruno goto fail; 1887e035717eSSean Bruno } 1888e035717eSSean Bruno for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) { 1889bfce461eSMarius Strobl err = bus_dmamap_create(fl->ifl_buf_tag, 0, 1890bfce461eSMarius Strobl &fl->ifl_sds.ifsd_map[i]); 1891e035717eSSean Bruno if (err != 0) { 189295246abbSSean Bruno device_printf(dev, "Unable to create RX buffer DMA map\n"); 18934c7070dbSScott Long goto fail; 18944c7070dbSScott Long } 18954c7070dbSScott Long } 1896835809f9SSean Bruno } 18974c7070dbSScott Long return (0); 18984c7070dbSScott Long 18994c7070dbSScott Long fail: 19004c7070dbSScott Long iflib_rx_structures_free(ctx); 19014c7070dbSScott Long return (err); 19024c7070dbSScott Long } 19034c7070dbSScott Long 19044c7070dbSScott Long 19054c7070dbSScott Long /* 19064c7070dbSScott Long * Internal service routines 19074c7070dbSScott Long */ 19084c7070dbSScott Long 19094c7070dbSScott Long struct rxq_refill_cb_arg { 19104c7070dbSScott Long int error; 19114c7070dbSScott Long bus_dma_segment_t seg; 19124c7070dbSScott Long int nseg; 19134c7070dbSScott Long }; 19144c7070dbSScott Long 19154c7070dbSScott Long static void 19164c7070dbSScott Long _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 19174c7070dbSScott Long { 19184c7070dbSScott Long struct rxq_refill_cb_arg *cb_arg = arg; 19194c7070dbSScott Long 19204c7070dbSScott Long cb_arg->error = error; 19214c7070dbSScott Long cb_arg->seg = segs[0]; 19224c7070dbSScott Long cb_arg->nseg = nseg; 19234c7070dbSScott Long } 19244c7070dbSScott Long 19254c7070dbSScott Long /** 19261722eeacSMarius Strobl * _iflib_fl_refill - refill an rxq free-buffer list 19274c7070dbSScott Long * @ctx: the iflib context 19281722eeacSMarius Strobl * @fl: the free list to refill 19291722eeacSMarius Strobl * @count: the number of new buffers to allocate 19304c7070dbSScott Long * 19311722eeacSMarius Strobl * (Re)populate an rxq free-buffer list with up to @count new packet buffers. 19321722eeacSMarius Strobl * The caller must assure that @count does not exceed the queue's capacity. 19334c7070dbSScott Long */ 19344c7070dbSScott Long static void 19354c7070dbSScott Long _iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count) 19364c7070dbSScott Long { 193795246abbSSean Bruno struct if_rxd_update iru; 1938fbec776dSAndrew Gallatin struct rxq_refill_cb_arg cb_arg; 19393db348b5SMarius Strobl struct mbuf *m; 19403db348b5SMarius Strobl caddr_t cl, *sd_cl; 19413db348b5SMarius Strobl struct mbuf **sd_m; 1942e035717eSSean Bruno bus_dmamap_t *sd_map; 1943fbec776dSAndrew Gallatin bus_addr_t bus_addr, *sd_ba; 19443db348b5SMarius Strobl int err, frag_idx, i, idx, n, pidx; 1945a1b799caSStephen Hurd qidx_t credits; 19464c7070dbSScott Long 1947e035717eSSean Bruno sd_m = fl->ifl_sds.ifsd_m; 1948e035717eSSean Bruno sd_map = fl->ifl_sds.ifsd_map; 1949e035717eSSean Bruno sd_cl = fl->ifl_sds.ifsd_cl; 1950fbec776dSAndrew Gallatin sd_ba = fl->ifl_sds.ifsd_ba; 19513db348b5SMarius Strobl pidx = fl->ifl_pidx; 1952e035717eSSean Bruno idx = pidx; 19533db348b5SMarius Strobl frag_idx = fl->ifl_fragidx; 1954a1b799caSStephen Hurd credits = fl->ifl_credits; 1955e035717eSSean Bruno 19563db348b5SMarius Strobl i = 0; 19574c7070dbSScott Long n = count; 19584c7070dbSScott Long MPASS(n > 0); 1959a1b799caSStephen Hurd MPASS(credits + n <= fl->ifl_size); 19604c7070dbSScott Long 19614c7070dbSScott Long if (pidx < fl->ifl_cidx) 19624c7070dbSScott Long MPASS(pidx + n <= fl->ifl_cidx); 1963a1b799caSStephen Hurd if (pidx == fl->ifl_cidx && (credits < fl->ifl_size)) 19644c7070dbSScott Long MPASS(fl->ifl_gen == 0); 19654c7070dbSScott Long if (pidx > fl->ifl_cidx) 19664c7070dbSScott Long MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx); 19674c7070dbSScott Long 19684c7070dbSScott Long DBG_COUNTER_INC(fl_refills); 19694c7070dbSScott Long if (n > 8) 19704c7070dbSScott Long DBG_COUNTER_INC(fl_refills_large); 19712d873474SStephen Hurd iru_init(&iru, fl->ifl_rxq, fl->ifl_id); 19724c7070dbSScott Long while (n--) { 19734c7070dbSScott Long /* 19744c7070dbSScott Long * We allocate an uninitialized mbuf + cluster, mbuf is 19754c7070dbSScott Long * initialized after rx. 19764c7070dbSScott Long * 19774c7070dbSScott Long * If the cluster is still set then we know a minimum sized packet was received 19784c7070dbSScott Long */ 19793db348b5SMarius Strobl bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size, 19803db348b5SMarius Strobl &frag_idx); 19813db348b5SMarius Strobl if (frag_idx < 0) 198287890dbaSSean Bruno bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx); 19833db348b5SMarius Strobl MPASS(frag_idx >= 0); 198487890dbaSSean Bruno if ((cl = sd_cl[frag_idx]) == NULL) { 1985fbec776dSAndrew Gallatin if ((cl = m_cljget(NULL, M_NOWAIT, fl->ifl_buf_size)) == NULL) 19864c7070dbSScott Long break; 19874c7070dbSScott Long 19884c7070dbSScott Long cb_arg.error = 0; 198995246abbSSean Bruno MPASS(sd_map != NULL); 1990bfce461eSMarius Strobl err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx], 19918a04b53dSKonstantin Belousov cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg, 19928a04b53dSKonstantin Belousov BUS_DMA_NOWAIT); 19934c7070dbSScott Long if (err != 0 || cb_arg.error) { 19944c7070dbSScott Long /* 19954c7070dbSScott Long * !zone_pack ? 19964c7070dbSScott Long */ 19974c7070dbSScott Long if (fl->ifl_zone == zone_pack) 19984c7070dbSScott Long uma_zfree(fl->ifl_zone, cl); 1999fbec776dSAndrew Gallatin break; 20004c7070dbSScott Long } 20014c7070dbSScott Long 2002fbec776dSAndrew Gallatin sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr; 200387890dbaSSean Bruno sd_cl[frag_idx] = cl; 2004fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2005fbec776dSAndrew Gallatin fl->ifl_cl_enqueued++; 2006fbec776dSAndrew Gallatin #endif 2007fbec776dSAndrew Gallatin } else { 2008fbec776dSAndrew Gallatin bus_addr = sd_ba[frag_idx]; 2009fbec776dSAndrew Gallatin } 201095dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx], 201195dcf343SMarius Strobl BUS_DMASYNC_PREREAD); 2012fbec776dSAndrew Gallatin 20136d49b41eSAndrew Gallatin if (sd_m[frag_idx] == NULL) { 2014fbec776dSAndrew Gallatin if ((m = m_gethdr(M_NOWAIT, MT_NOINIT)) == NULL) { 2015fbec776dSAndrew Gallatin break; 2016fbec776dSAndrew Gallatin } 201787890dbaSSean Bruno sd_m[frag_idx] = m; 20186d49b41eSAndrew Gallatin } 20193db348b5SMarius Strobl bit_set(fl->ifl_rx_bitmap, frag_idx); 2020fbec776dSAndrew Gallatin #if MEMORY_LOGGING 2021fbec776dSAndrew Gallatin fl->ifl_m_enqueued++; 2022fbec776dSAndrew Gallatin #endif 2023fbec776dSAndrew Gallatin 2024fbec776dSAndrew Gallatin DBG_COUNTER_INC(rx_allocs); 202587890dbaSSean Bruno fl->ifl_rxd_idxs[i] = frag_idx; 20264c7070dbSScott Long fl->ifl_bus_addrs[i] = bus_addr; 20274c7070dbSScott Long fl->ifl_vm_addrs[i] = cl; 2028a1b799caSStephen Hurd credits++; 20294c7070dbSScott Long i++; 2030a1b799caSStephen Hurd MPASS(credits <= fl->ifl_size); 2031e035717eSSean Bruno if (++idx == fl->ifl_size) { 20324c7070dbSScott Long fl->ifl_gen = 1; 2033e035717eSSean Bruno idx = 0; 20344c7070dbSScott Long } 20354c7070dbSScott Long if (n == 0 || i == IFLIB_MAX_RX_REFRESH) { 203695246abbSSean Bruno iru.iru_pidx = pidx; 203795246abbSSean Bruno iru.iru_count = i; 203895246abbSSean Bruno ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 20394c7070dbSScott Long i = 0; 2040e035717eSSean Bruno pidx = idx; 2041fa5416a8SSean Bruno fl->ifl_pidx = idx; 2042a1b799caSStephen Hurd fl->ifl_credits = credits; 204387890dbaSSean Bruno } 20444c7070dbSScott Long } 2045fbec776dSAndrew Gallatin 2046a1b799caSStephen Hurd if (i) { 2047a1b799caSStephen Hurd iru.iru_pidx = pidx; 2048a1b799caSStephen Hurd iru.iru_count = i; 2049a1b799caSStephen Hurd ctx->isc_rxd_refill(ctx->ifc_softc, &iru); 2050a1b799caSStephen Hurd fl->ifl_pidx = idx; 2051a1b799caSStephen Hurd fl->ifl_credits = credits; 2052a1b799caSStephen Hurd } 20534c7070dbSScott Long DBG_COUNTER_INC(rxd_flush); 20544c7070dbSScott Long if (fl->ifl_pidx == 0) 20554c7070dbSScott Long pidx = fl->ifl_size - 1; 20564c7070dbSScott Long else 20574c7070dbSScott Long pidx = fl->ifl_pidx - 1; 205895246abbSSean Bruno 205995246abbSSean Bruno bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 206095246abbSSean Bruno BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 20614c7070dbSScott Long ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id, fl->ifl_id, pidx); 206287890dbaSSean Bruno fl->ifl_fragidx = frag_idx; 20634c7070dbSScott Long } 20644c7070dbSScott Long 20654c7070dbSScott Long static __inline void 20664c7070dbSScott Long __iflib_fl_refill_lt(if_ctx_t ctx, iflib_fl_t fl, int max) 20674c7070dbSScott Long { 20684c7070dbSScott Long /* we avoid allowing pidx to catch up with cidx as it confuses ixl */ 20694c7070dbSScott Long int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1; 20704c7070dbSScott Long #ifdef INVARIANTS 20714c7070dbSScott Long int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1; 20724c7070dbSScott Long #endif 20734c7070dbSScott Long 20744c7070dbSScott Long MPASS(fl->ifl_credits <= fl->ifl_size); 20754c7070dbSScott Long MPASS(reclaimable == delta); 20764c7070dbSScott Long 20774c7070dbSScott Long if (reclaimable > 0) 20784c7070dbSScott Long _iflib_fl_refill(ctx, fl, min(max, reclaimable)); 20794c7070dbSScott Long } 20804c7070dbSScott Long 208177c1fcecSEric Joyner uint8_t 208277c1fcecSEric Joyner iflib_in_detach(if_ctx_t ctx) 208377c1fcecSEric Joyner { 208477c1fcecSEric Joyner bool in_detach; 20851722eeacSMarius Strobl 208677c1fcecSEric Joyner STATE_LOCK(ctx); 208777c1fcecSEric Joyner in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH); 208877c1fcecSEric Joyner STATE_UNLOCK(ctx); 208977c1fcecSEric Joyner return (in_detach); 209077c1fcecSEric Joyner } 209177c1fcecSEric Joyner 20924c7070dbSScott Long static void 20934c7070dbSScott Long iflib_fl_bufs_free(iflib_fl_t fl) 20944c7070dbSScott Long { 20954c7070dbSScott Long iflib_dma_info_t idi = fl->ifl_ifdi; 20968a04b53dSKonstantin Belousov bus_dmamap_t sd_map; 20974c7070dbSScott Long uint32_t i; 20984c7070dbSScott Long 20994c7070dbSScott Long for (i = 0; i < fl->ifl_size; i++) { 2100e035717eSSean Bruno struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i]; 2101e035717eSSean Bruno caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i]; 21024c7070dbSScott Long 2103fbec776dSAndrew Gallatin if (*sd_cl != NULL) { 21048a04b53dSKonstantin Belousov sd_map = fl->ifl_sds.ifsd_map[i]; 2105bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, sd_map, 21068a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 2107bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, sd_map); 2108fbec776dSAndrew Gallatin if (*sd_cl != NULL) 2109fbec776dSAndrew Gallatin uma_zfree(fl->ifl_zone, *sd_cl); 211077c1fcecSEric Joyner // XXX: Should this get moved out? 211177c1fcecSEric Joyner if (iflib_in_detach(fl->ifl_rxq->ifr_ctx)) 2112bfce461eSMarius Strobl bus_dmamap_destroy(fl->ifl_buf_tag, sd_map); 2113e035717eSSean Bruno if (*sd_m != NULL) { 2114e035717eSSean Bruno m_init(*sd_m, M_NOWAIT, MT_DATA, 0); 2115e035717eSSean Bruno uma_zfree(zone_mbuf, *sd_m); 2116e035717eSSean Bruno } 21174c7070dbSScott Long } else { 2118e035717eSSean Bruno MPASS(*sd_cl == NULL); 2119e035717eSSean Bruno MPASS(*sd_m == NULL); 21204c7070dbSScott Long } 21214c7070dbSScott Long #if MEMORY_LOGGING 21224c7070dbSScott Long fl->ifl_m_dequeued++; 21234c7070dbSScott Long fl->ifl_cl_dequeued++; 21244c7070dbSScott Long #endif 2125e035717eSSean Bruno *sd_cl = NULL; 2126e035717eSSean Bruno *sd_m = NULL; 21274c7070dbSScott Long } 212895246abbSSean Bruno #ifdef INVARIANTS 212995246abbSSean Bruno for (i = 0; i < fl->ifl_size; i++) { 213095246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_cl[i] == NULL); 213195246abbSSean Bruno MPASS(fl->ifl_sds.ifsd_m[i] == NULL); 213295246abbSSean Bruno } 213395246abbSSean Bruno #endif 21344c7070dbSScott Long /* 21354c7070dbSScott Long * Reset free list values 21364c7070dbSScott Long */ 213787890dbaSSean Bruno fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0; 21384c7070dbSScott Long bzero(idi->idi_vaddr, idi->idi_size); 21394c7070dbSScott Long } 21404c7070dbSScott Long 21414c7070dbSScott Long /********************************************************************* 21424c7070dbSScott Long * 21431722eeacSMarius Strobl * Initialize a free list and its buffers. 21444c7070dbSScott Long * 21454c7070dbSScott Long **********************************************************************/ 21464c7070dbSScott Long static int 21474c7070dbSScott Long iflib_fl_setup(iflib_fl_t fl) 21484c7070dbSScott Long { 21494c7070dbSScott Long iflib_rxq_t rxq = fl->ifl_rxq; 21504c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 21514c7070dbSScott Long 21527274b2f6SStephen Hurd bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1); 21534c7070dbSScott Long /* 21544c7070dbSScott Long ** Free current RX buffer structs and their mbufs 21554c7070dbSScott Long */ 21564c7070dbSScott Long iflib_fl_bufs_free(fl); 21574c7070dbSScott Long /* Now replenish the mbufs */ 21584c7070dbSScott Long MPASS(fl->ifl_credits == 0); 21591b9d9394SEric Joyner fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz; 21604c7070dbSScott Long if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size) 21614c7070dbSScott Long ctx->ifc_max_fl_buf_size = fl->ifl_buf_size; 21624c7070dbSScott Long fl->ifl_cltype = m_gettype(fl->ifl_buf_size); 21634c7070dbSScott Long fl->ifl_zone = m_getzone(fl->ifl_buf_size); 21644c7070dbSScott Long 21654c7070dbSScott Long 21664c7070dbSScott Long /* avoid pre-allocating zillions of clusters to an idle card 21674c7070dbSScott Long * potentially speeding up attach 21684c7070dbSScott Long */ 21694c7070dbSScott Long _iflib_fl_refill(ctx, fl, min(128, fl->ifl_size)); 21704c7070dbSScott Long MPASS(min(128, fl->ifl_size) == fl->ifl_credits); 21714c7070dbSScott Long if (min(128, fl->ifl_size) != fl->ifl_credits) 21724c7070dbSScott Long return (ENOBUFS); 21734c7070dbSScott Long /* 21744c7070dbSScott Long * handle failure 21754c7070dbSScott Long */ 21764c7070dbSScott Long MPASS(rxq != NULL); 21774c7070dbSScott Long MPASS(fl->ifl_ifdi != NULL); 21784c7070dbSScott Long bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 21794c7070dbSScott Long BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 21804c7070dbSScott Long return (0); 21814c7070dbSScott Long } 21824c7070dbSScott Long 21834c7070dbSScott Long /********************************************************************* 21844c7070dbSScott Long * 21854c7070dbSScott Long * Free receive ring data structures 21864c7070dbSScott Long * 21874c7070dbSScott Long **********************************************************************/ 21884c7070dbSScott Long static void 21894c7070dbSScott Long iflib_rx_sds_free(iflib_rxq_t rxq) 21904c7070dbSScott Long { 21914c7070dbSScott Long iflib_fl_t fl; 21928a04b53dSKonstantin Belousov int i, j; 21934c7070dbSScott Long 21944c7070dbSScott Long if (rxq->ifr_fl != NULL) { 21954c7070dbSScott Long for (i = 0; i < rxq->ifr_nfl; i++) { 21964c7070dbSScott Long fl = &rxq->ifr_fl[i]; 2197bfce461eSMarius Strobl if (fl->ifl_buf_tag != NULL) { 21988a04b53dSKonstantin Belousov if (fl->ifl_sds.ifsd_map != NULL) { 219977102fd6SAndrew Gallatin for (j = 0; j < fl->ifl_size; j++) { 220077102fd6SAndrew Gallatin if (fl->ifl_sds.ifsd_map[j] == 22018a04b53dSKonstantin Belousov NULL) 22028a04b53dSKonstantin Belousov continue; 22038a04b53dSKonstantin Belousov bus_dmamap_sync( 2204bfce461eSMarius Strobl fl->ifl_buf_tag, 220577102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j], 22068a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 22078a04b53dSKonstantin Belousov bus_dmamap_unload( 2208bfce461eSMarius Strobl fl->ifl_buf_tag, 220977102fd6SAndrew Gallatin fl->ifl_sds.ifsd_map[j]); 22108a04b53dSKonstantin Belousov } 22118a04b53dSKonstantin Belousov } 2212bfce461eSMarius Strobl bus_dma_tag_destroy(fl->ifl_buf_tag); 2213bfce461eSMarius Strobl fl->ifl_buf_tag = NULL; 22144c7070dbSScott Long } 2215e035717eSSean Bruno free(fl->ifl_sds.ifsd_m, M_IFLIB); 2216e035717eSSean Bruno free(fl->ifl_sds.ifsd_cl, M_IFLIB); 2217fbec776dSAndrew Gallatin free(fl->ifl_sds.ifsd_ba, M_IFLIB); 2218e035717eSSean Bruno free(fl->ifl_sds.ifsd_map, M_IFLIB); 2219e035717eSSean Bruno fl->ifl_sds.ifsd_m = NULL; 2220e035717eSSean Bruno fl->ifl_sds.ifsd_cl = NULL; 2221fbec776dSAndrew Gallatin fl->ifl_sds.ifsd_ba = NULL; 2222e035717eSSean Bruno fl->ifl_sds.ifsd_map = NULL; 22234c7070dbSScott Long } 22244c7070dbSScott Long free(rxq->ifr_fl, M_IFLIB); 22254c7070dbSScott Long rxq->ifr_fl = NULL; 22261722eeacSMarius Strobl rxq->ifr_cq_cidx = 0; 22274c7070dbSScott Long } 22284c7070dbSScott Long } 22294c7070dbSScott Long 22304c7070dbSScott Long /* 22311722eeacSMarius Strobl * Timer routine 22324c7070dbSScott Long */ 22334c7070dbSScott Long static void 22344c7070dbSScott Long iflib_timer(void *arg) 22354c7070dbSScott Long { 2236ab2e3f79SStephen Hurd iflib_txq_t txq = arg; 22374c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 2238ab2e3f79SStephen Hurd if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 2239dd7fbcf1SStephen Hurd uint64_t this_tick = ticks; 2240dd7fbcf1SStephen Hurd uint32_t reset_on = hz / 2; 22414c7070dbSScott Long 22424c7070dbSScott Long if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) 22434c7070dbSScott Long return; 22441722eeacSMarius Strobl 22454c7070dbSScott Long /* 22464c7070dbSScott Long ** Check on the state of the TX queue(s), this 22474c7070dbSScott Long ** can be done without the lock because its RO 22484c7070dbSScott Long ** and the HUNG state will be static if set. 22494c7070dbSScott Long */ 2250dd7fbcf1SStephen Hurd if (this_tick - txq->ift_last_timer_tick >= hz / 2) { 2251dd7fbcf1SStephen Hurd txq->ift_last_timer_tick = this_tick; 2252ab2e3f79SStephen Hurd IFDI_TIMER(ctx, txq->ift_id); 2253ab2e3f79SStephen Hurd if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) && 2254ab2e3f79SStephen Hurd ((txq->ift_cleaned_prev == txq->ift_cleaned) || 2255ab2e3f79SStephen Hurd (sctx->isc_pause_frames == 0))) 2256ab2e3f79SStephen Hurd goto hung; 2257a9693502SSean Bruno 2258ab2e3f79SStephen Hurd if (ifmp_ring_is_stalled(txq->ift_br)) 2259ab2e3f79SStephen Hurd txq->ift_qstatus = IFLIB_QUEUE_HUNG; 2260ab2e3f79SStephen Hurd txq->ift_cleaned_prev = txq->ift_cleaned; 2261dd7fbcf1SStephen Hurd } 2262dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP 2263dd7fbcf1SStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) 226495dcf343SMarius Strobl iflib_netmap_timer_adjust(ctx, txq, &reset_on); 2265dd7fbcf1SStephen Hurd #endif 2266ab2e3f79SStephen Hurd /* handle any laggards */ 2267ab2e3f79SStephen Hurd if (txq->ift_db_pending) 2268ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 2269a9693502SSean Bruno 2270ab2e3f79SStephen Hurd sctx->isc_pause_frames = 0; 2271d300df01SStephen Hurd if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) 2272dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu); 2273ab2e3f79SStephen Hurd return; 22741722eeacSMarius Strobl 2275ab2e3f79SStephen Hurd hung: 22761722eeacSMarius Strobl device_printf(ctx->ifc_dev, 22771722eeacSMarius Strobl "Watchdog timeout (TX: %d desc avail: %d pidx: %d) -- resetting\n", 2278ab2e3f79SStephen Hurd txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx); 22797b610b60SSean Bruno STATE_LOCK(ctx); 22807b610b60SSean Bruno if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 22817b610b60SSean Bruno ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET); 2282940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 228346fa0c25SEric Joyner STATE_UNLOCK(ctx); 22844c7070dbSScott Long } 22854c7070dbSScott Long 22864c7070dbSScott Long static void 22871b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(if_ctx_t ctx) 22881b9d9394SEric Joyner { 22891b9d9394SEric Joyner if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 22901b9d9394SEric Joyner 22911b9d9394SEric Joyner /* 22921b9d9394SEric Joyner * XXX don't set the max_frame_size to larger 22931b9d9394SEric Joyner * than the hardware can handle 22941b9d9394SEric Joyner */ 22951b9d9394SEric Joyner if (sctx->isc_max_frame_size <= MCLBYTES) 22961b9d9394SEric Joyner ctx->ifc_rx_mbuf_sz = MCLBYTES; 22971b9d9394SEric Joyner else 22981b9d9394SEric Joyner ctx->ifc_rx_mbuf_sz = MJUMPAGESIZE; 22991b9d9394SEric Joyner } 23001b9d9394SEric Joyner 23011b9d9394SEric Joyner uint32_t 23021b9d9394SEric Joyner iflib_get_rx_mbuf_sz(if_ctx_t ctx) 23031b9d9394SEric Joyner { 23041722eeacSMarius Strobl 23051b9d9394SEric Joyner return (ctx->ifc_rx_mbuf_sz); 23061b9d9394SEric Joyner } 23071b9d9394SEric Joyner 23081b9d9394SEric Joyner static void 23094c7070dbSScott Long iflib_init_locked(if_ctx_t ctx) 23104c7070dbSScott Long { 23114c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 23121248952aSSean Bruno if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 23134c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 23144c7070dbSScott Long iflib_fl_t fl; 23154c7070dbSScott Long iflib_txq_t txq; 23164c7070dbSScott Long iflib_rxq_t rxq; 2317ab2e3f79SStephen Hurd int i, j, tx_ip_csum_flags, tx_ip6_csum_flags; 23184c7070dbSScott Long 23194c7070dbSScott Long if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 23204c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 23214c7070dbSScott Long 23221248952aSSean Bruno tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP); 23231248952aSSean Bruno tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP); 23244c7070dbSScott Long /* Set hardware offload abilities */ 23254c7070dbSScott Long if_clearhwassist(ifp); 23264c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM) 23271248952aSSean Bruno if_sethwassistbits(ifp, tx_ip_csum_flags, 0); 23284c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6) 23291248952aSSean Bruno if_sethwassistbits(ifp, tx_ip6_csum_flags, 0); 23304c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO4) 23314c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP_TSO, 0); 23324c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_TSO6) 23334c7070dbSScott Long if_sethwassistbits(ifp, CSUM_IP6_TSO, 0); 23344c7070dbSScott Long 23354c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < sctx->isc_ntxqsets; i++, txq++) { 23364c7070dbSScott Long CALLOUT_LOCK(txq); 23374c7070dbSScott Long callout_stop(&txq->ift_timer); 23384c7070dbSScott Long CALLOUT_UNLOCK(txq); 23394c7070dbSScott Long iflib_netmap_txq_init(ctx, txq); 23404c7070dbSScott Long } 23411b9d9394SEric Joyner 23421b9d9394SEric Joyner /* 23431b9d9394SEric Joyner * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so 23441b9d9394SEric Joyner * that drivers can use the value when setting up the hardware receive 23451b9d9394SEric Joyner * buffers. 23461b9d9394SEric Joyner */ 23471b9d9394SEric Joyner iflib_calc_rx_mbuf_sz(ctx); 23481b9d9394SEric Joyner 234923ac9029SStephen Hurd #ifdef INVARIANTS 235023ac9029SStephen Hurd i = if_getdrvflags(ifp); 235123ac9029SStephen Hurd #endif 23524c7070dbSScott Long IFDI_INIT(ctx); 235323ac9029SStephen Hurd MPASS(if_getdrvflags(ifp) == i); 23544c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < sctx->isc_nrxqsets; i++, rxq++) { 235595246abbSSean Bruno /* XXX this should really be done on a per-queue basis */ 2356d0d0ad0aSStephen Hurd if (if_getcapenable(ifp) & IFCAP_NETMAP) { 2357d0d0ad0aSStephen Hurd MPASS(rxq->ifr_id == i); 2358d0d0ad0aSStephen Hurd iflib_netmap_rxq_init(ctx, rxq); 235995246abbSSean Bruno continue; 2360d0d0ad0aSStephen Hurd } 23614c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 23624c7070dbSScott Long if (iflib_fl_setup(fl)) { 23633d10e9edSMarius Strobl device_printf(ctx->ifc_dev, 23643d10e9edSMarius Strobl "setting up free list %d failed - " 23653d10e9edSMarius Strobl "check cluster settings\n", j); 23664c7070dbSScott Long goto done; 23674c7070dbSScott Long } 23684c7070dbSScott Long } 23694c7070dbSScott Long } 23704c7070dbSScott Long done: 23714c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 23724c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 23734c7070dbSScott Long txq = ctx->ifc_txqs; 23744c7070dbSScott Long for (i = 0; i < sctx->isc_ntxqsets; i++, txq++) 2375ab2e3f79SStephen Hurd callout_reset_on(&txq->ift_timer, hz/2, iflib_timer, txq, 2376ab2e3f79SStephen Hurd txq->ift_timer.c_cpu); 23774c7070dbSScott Long } 23784c7070dbSScott Long 23794c7070dbSScott Long static int 23804c7070dbSScott Long iflib_media_change(if_t ifp) 23814c7070dbSScott Long { 23824c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 23834c7070dbSScott Long int err; 23844c7070dbSScott Long 23854c7070dbSScott Long CTX_LOCK(ctx); 23864c7070dbSScott Long if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0) 23874c7070dbSScott Long iflib_init_locked(ctx); 23884c7070dbSScott Long CTX_UNLOCK(ctx); 23894c7070dbSScott Long return (err); 23904c7070dbSScott Long } 23914c7070dbSScott Long 23924c7070dbSScott Long static void 23934c7070dbSScott Long iflib_media_status(if_t ifp, struct ifmediareq *ifmr) 23944c7070dbSScott Long { 23954c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 23964c7070dbSScott Long 23974c7070dbSScott Long CTX_LOCK(ctx); 2398ab2e3f79SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 23994c7070dbSScott Long IFDI_MEDIA_STATUS(ctx, ifmr); 24004c7070dbSScott Long CTX_UNLOCK(ctx); 24014c7070dbSScott Long } 24024c7070dbSScott Long 240309f6ff4fSMatt Macy void 24044c7070dbSScott Long iflib_stop(if_ctx_t ctx) 24054c7070dbSScott Long { 24064c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 24074c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 24084c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 24094d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 24104c7070dbSScott Long iflib_dma_info_t di; 24114c7070dbSScott Long iflib_fl_t fl; 24124c7070dbSScott Long int i, j; 24134c7070dbSScott Long 24144c7070dbSScott Long /* Tell the stack that the interface is no longer active */ 24154c7070dbSScott Long if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 24164c7070dbSScott Long 24174c7070dbSScott Long IFDI_INTR_DISABLE(ctx); 2418ab2e3f79SStephen Hurd DELAY(1000); 2419da69b8f9SSean Bruno IFDI_STOP(ctx); 2420ab2e3f79SStephen Hurd DELAY(1000); 24214c7070dbSScott Long 2422da69b8f9SSean Bruno iflib_debug_reset(); 24234c7070dbSScott Long /* Wait for current tx queue users to exit to disarm watchdog timer. */ 24244c7070dbSScott Long for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) { 24254c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24264c7070dbSScott Long 2427226fb85dSStephen Hurd CALLOUT_LOCK(txq); 2428226fb85dSStephen Hurd callout_stop(&txq->ift_timer); 2429226fb85dSStephen Hurd CALLOUT_UNLOCK(txq); 2430226fb85dSStephen Hurd 24314c7070dbSScott Long /* clean any enqueued buffers */ 2432da69b8f9SSean Bruno iflib_ifmp_purge(txq); 24334c7070dbSScott Long /* Free any existing tx buffers. */ 243423ac9029SStephen Hurd for (j = 0; j < txq->ift_size; j++) { 24354c7070dbSScott Long iflib_txsd_free(ctx, txq, j); 24364c7070dbSScott Long } 2437ab2e3f79SStephen Hurd txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0; 2438ab2e3f79SStephen Hurd txq->ift_in_use = txq->ift_gen = txq->ift_cidx = txq->ift_pidx = txq->ift_no_desc_avail = 0; 24394c7070dbSScott Long txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0; 24404c7070dbSScott Long txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0; 2441ab2e3f79SStephen Hurd txq->ift_pullups = 0; 244295246abbSSean Bruno ifmp_ring_reset_stats(txq->ift_br); 24434d261ce2SStephen Hurd for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++) 24444c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24454c7070dbSScott Long } 24464c7070dbSScott Long for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) { 24474c7070dbSScott Long /* make sure all transmitters have completed before proceeding XXX */ 24484c7070dbSScott Long 24491722eeacSMarius Strobl rxq->ifr_cq_cidx = 0; 24504d261ce2SStephen Hurd for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++) 24514c7070dbSScott Long bzero((void *)di->idi_vaddr, di->idi_size); 24524c7070dbSScott Long /* also resets the free lists pidx/cidx */ 24534c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 24544c7070dbSScott Long iflib_fl_bufs_free(fl); 24554c7070dbSScott Long } 24564c7070dbSScott Long } 24574c7070dbSScott Long 245895246abbSSean Bruno static inline caddr_t 245995246abbSSean Bruno calc_next_rxd(iflib_fl_t fl, int cidx) 246095246abbSSean Bruno { 246195246abbSSean Bruno qidx_t size; 246295246abbSSean Bruno int nrxd; 246395246abbSSean Bruno caddr_t start, end, cur, next; 246495246abbSSean Bruno 246595246abbSSean Bruno nrxd = fl->ifl_size; 246695246abbSSean Bruno size = fl->ifl_rxd_size; 246795246abbSSean Bruno start = fl->ifl_ifdi->idi_vaddr; 246895246abbSSean Bruno 246995246abbSSean Bruno if (__predict_false(size == 0)) 247095246abbSSean Bruno return (start); 247195246abbSSean Bruno cur = start + size*cidx; 247295246abbSSean Bruno end = start + size*nrxd; 247395246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 247495246abbSSean Bruno return (next < end ? next : start); 247595246abbSSean Bruno } 247695246abbSSean Bruno 2477e035717eSSean Bruno static inline void 2478e035717eSSean Bruno prefetch_pkts(iflib_fl_t fl, int cidx) 2479e035717eSSean Bruno { 2480e035717eSSean Bruno int nextptr; 2481e035717eSSean Bruno int nrxd = fl->ifl_size; 248295246abbSSean Bruno caddr_t next_rxd; 248395246abbSSean Bruno 2484e035717eSSean Bruno 2485e035717eSSean Bruno nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1); 2486e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_m[nextptr]); 2487e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_cl[nextptr]); 248895246abbSSean Bruno next_rxd = calc_next_rxd(fl, cidx); 248995246abbSSean Bruno prefetch(next_rxd); 2490e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]); 2491e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]); 2492e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]); 2493e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]); 2494e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]); 2495e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]); 2496e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]); 2497e035717eSSean Bruno prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]); 2498e035717eSSean Bruno } 2499e035717eSSean Bruno 25006d49b41eSAndrew Gallatin static struct mbuf * 25016d49b41eSAndrew Gallatin rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd, 25026d49b41eSAndrew Gallatin int *pf_rv, if_rxd_info_t ri) 25034c7070dbSScott Long { 2504e035717eSSean Bruno bus_dmamap_t map; 25054c7070dbSScott Long iflib_fl_t fl; 25066d49b41eSAndrew Gallatin caddr_t payload; 25076d49b41eSAndrew Gallatin struct mbuf *m; 25086d49b41eSAndrew Gallatin int flid, cidx, len, next; 25094c7070dbSScott Long 251095246abbSSean Bruno map = NULL; 25114c7070dbSScott Long flid = irf->irf_flid; 25124c7070dbSScott Long cidx = irf->irf_idx; 25134c7070dbSScott Long fl = &rxq->ifr_fl[flid]; 251495246abbSSean Bruno sd->ifsd_fl = fl; 251595246abbSSean Bruno sd->ifsd_cidx = cidx; 25166d49b41eSAndrew Gallatin m = fl->ifl_sds.ifsd_m[cidx]; 251795246abbSSean Bruno sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx]; 25184c7070dbSScott Long fl->ifl_credits--; 25194c7070dbSScott Long #if MEMORY_LOGGING 25204c7070dbSScott Long fl->ifl_m_dequeued++; 25214c7070dbSScott Long #endif 252295246abbSSean Bruno if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH) 2523e035717eSSean Bruno prefetch_pkts(fl, cidx); 2524e035717eSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1); 2525e035717eSSean Bruno prefetch(&fl->ifl_sds.ifsd_map[next]); 2526e035717eSSean Bruno map = fl->ifl_sds.ifsd_map[cidx]; 2527e035717eSSean Bruno next = (cidx + CACHE_LINE_SIZE) & (fl->ifl_size-1); 25284c7070dbSScott Long 25294c7070dbSScott Long /* not valid assert if bxe really does SGE from non-contiguous elements */ 25304c7070dbSScott Long MPASS(fl->ifl_cidx == cidx); 2531bfce461eSMarius Strobl bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD); 25326d49b41eSAndrew Gallatin 25336d49b41eSAndrew Gallatin if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL) { 25346d49b41eSAndrew Gallatin payload = *sd->ifsd_cl; 25356d49b41eSAndrew Gallatin payload += ri->iri_pad; 25366d49b41eSAndrew Gallatin len = ri->iri_len - ri->iri_pad; 25376d49b41eSAndrew Gallatin *pf_rv = pfil_run_hooks(rxq->pfil, payload, ri->iri_ifp, 25386d49b41eSAndrew Gallatin len | PFIL_MEMPTR | PFIL_IN, NULL); 25396d49b41eSAndrew Gallatin switch (*pf_rv) { 25406d49b41eSAndrew Gallatin case PFIL_DROPPED: 25416d49b41eSAndrew Gallatin case PFIL_CONSUMED: 25426d49b41eSAndrew Gallatin /* 25436d49b41eSAndrew Gallatin * The filter ate it. Everything is recycled. 25446d49b41eSAndrew Gallatin */ 25456d49b41eSAndrew Gallatin m = NULL; 25466d49b41eSAndrew Gallatin unload = 0; 25476d49b41eSAndrew Gallatin break; 25486d49b41eSAndrew Gallatin case PFIL_REALLOCED: 25496d49b41eSAndrew Gallatin /* 25506d49b41eSAndrew Gallatin * The filter copied it. Everything is recycled. 25516d49b41eSAndrew Gallatin */ 25526d49b41eSAndrew Gallatin m = pfil_mem2mbuf(payload); 25536d49b41eSAndrew Gallatin unload = 0; 25546d49b41eSAndrew Gallatin break; 25556d49b41eSAndrew Gallatin case PFIL_PASS: 25566d49b41eSAndrew Gallatin /* 25576d49b41eSAndrew Gallatin * Filter said it was OK, so receive like 25586d49b41eSAndrew Gallatin * normal 25596d49b41eSAndrew Gallatin */ 25606d49b41eSAndrew Gallatin fl->ifl_sds.ifsd_m[cidx] = NULL; 25616d49b41eSAndrew Gallatin break; 25626d49b41eSAndrew Gallatin default: 25636d49b41eSAndrew Gallatin MPASS(0); 25646d49b41eSAndrew Gallatin } 25656d49b41eSAndrew Gallatin } else { 25666d49b41eSAndrew Gallatin fl->ifl_sds.ifsd_m[cidx] = NULL; 25676d49b41eSAndrew Gallatin *pf_rv = PFIL_PASS; 25686d49b41eSAndrew Gallatin } 25696d49b41eSAndrew Gallatin 25704c7070dbSScott Long if (unload) 2571bfce461eSMarius Strobl bus_dmamap_unload(fl->ifl_buf_tag, map); 257295246abbSSean Bruno fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1); 257395246abbSSean Bruno if (__predict_false(fl->ifl_cidx == 0)) 25744c7070dbSScott Long fl->ifl_gen = 0; 257587890dbaSSean Bruno bit_clear(fl->ifl_rx_bitmap, cidx); 25766d49b41eSAndrew Gallatin return (m); 25774c7070dbSScott Long } 25784c7070dbSScott Long 25794c7070dbSScott Long static struct mbuf * 25806d49b41eSAndrew Gallatin assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv) 25814c7070dbSScott Long { 258295246abbSSean Bruno struct mbuf *m, *mh, *mt; 258395246abbSSean Bruno caddr_t cl; 25846d49b41eSAndrew Gallatin int *pf_rv_ptr, flags, i, padlen; 25856d49b41eSAndrew Gallatin bool consumed; 25864c7070dbSScott Long 25874c7070dbSScott Long i = 0; 258823ac9029SStephen Hurd mh = NULL; 25896d49b41eSAndrew Gallatin consumed = false; 25906d49b41eSAndrew Gallatin *pf_rv = PFIL_PASS; 25916d49b41eSAndrew Gallatin pf_rv_ptr = pf_rv; 25924c7070dbSScott Long do { 25936d49b41eSAndrew Gallatin m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd, 25946d49b41eSAndrew Gallatin pf_rv_ptr, ri); 25954c7070dbSScott Long 259695246abbSSean Bruno MPASS(*sd->ifsd_cl != NULL); 259723ac9029SStephen Hurd 25986d49b41eSAndrew Gallatin /* 25996d49b41eSAndrew Gallatin * Exclude zero-length frags & frags from 26006d49b41eSAndrew Gallatin * packets the filter has consumed or dropped 26016d49b41eSAndrew Gallatin */ 26026d49b41eSAndrew Gallatin if (ri->iri_frags[i].irf_len == 0 || consumed || 26036d49b41eSAndrew Gallatin *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) { 26046d49b41eSAndrew Gallatin if (mh == NULL) { 26056d49b41eSAndrew Gallatin /* everything saved here */ 26066d49b41eSAndrew Gallatin consumed = true; 26076d49b41eSAndrew Gallatin pf_rv_ptr = NULL; 260823ac9029SStephen Hurd continue; 260923ac9029SStephen Hurd } 26106d49b41eSAndrew Gallatin /* XXX we can save the cluster here, but not the mbuf */ 26116d49b41eSAndrew Gallatin m_init(m, M_NOWAIT, MT_DATA, 0); 26126d49b41eSAndrew Gallatin m_free(m); 26136d49b41eSAndrew Gallatin continue; 26146d49b41eSAndrew Gallatin } 261523ac9029SStephen Hurd if (mh == NULL) { 26164c7070dbSScott Long flags = M_PKTHDR|M_EXT; 26174c7070dbSScott Long mh = mt = m; 26184c7070dbSScott Long padlen = ri->iri_pad; 26194c7070dbSScott Long } else { 26204c7070dbSScott Long flags = M_EXT; 26214c7070dbSScott Long mt->m_next = m; 26224c7070dbSScott Long mt = m; 26234c7070dbSScott Long /* assuming padding is only on the first fragment */ 26244c7070dbSScott Long padlen = 0; 26254c7070dbSScott Long } 262695246abbSSean Bruno cl = *sd->ifsd_cl; 262795246abbSSean Bruno *sd->ifsd_cl = NULL; 26284c7070dbSScott Long 26294c7070dbSScott Long /* Can these two be made one ? */ 26304c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, flags); 263195246abbSSean Bruno m_cljset(m, cl, sd->ifsd_fl->ifl_cltype); 26324c7070dbSScott Long /* 26334c7070dbSScott Long * These must follow m_init and m_cljset 26344c7070dbSScott Long */ 26354c7070dbSScott Long m->m_data += padlen; 26364c7070dbSScott Long ri->iri_len -= padlen; 263723ac9029SStephen Hurd m->m_len = ri->iri_frags[i].irf_len; 26384c7070dbSScott Long } while (++i < ri->iri_nfrags); 26394c7070dbSScott Long 26404c7070dbSScott Long return (mh); 26414c7070dbSScott Long } 26424c7070dbSScott Long 26434c7070dbSScott Long /* 26444c7070dbSScott Long * Process one software descriptor 26454c7070dbSScott Long */ 26464c7070dbSScott Long static struct mbuf * 26474c7070dbSScott Long iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri) 26484c7070dbSScott Long { 264995246abbSSean Bruno struct if_rxsd sd; 26504c7070dbSScott Long struct mbuf *m; 26516d49b41eSAndrew Gallatin int pf_rv; 26524c7070dbSScott Long 26534c7070dbSScott Long /* should I merge this back in now that the two paths are basically duplicated? */ 265423ac9029SStephen Hurd if (ri->iri_nfrags == 1 && 265518628b74SMark Johnston ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) { 26566d49b41eSAndrew Gallatin m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd, 26576d49b41eSAndrew Gallatin &pf_rv, ri); 26586d49b41eSAndrew Gallatin if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED) 26596d49b41eSAndrew Gallatin return (m); 26606d49b41eSAndrew Gallatin if (pf_rv == PFIL_PASS) { 26614c7070dbSScott Long m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR); 266295246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 266395246abbSSean Bruno if (!IP_ALIGNED(m)) 266495246abbSSean Bruno m->m_data += 2; 266595246abbSSean Bruno #endif 266695246abbSSean Bruno memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len); 266723ac9029SStephen Hurd m->m_len = ri->iri_frags[0].irf_len; 26686d49b41eSAndrew Gallatin } 26694c7070dbSScott Long } else { 26706d49b41eSAndrew Gallatin m = assemble_segments(rxq, ri, &sd, &pf_rv); 26716d49b41eSAndrew Gallatin if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED) 26726d49b41eSAndrew Gallatin return (m); 26734c7070dbSScott Long } 26744c7070dbSScott Long m->m_pkthdr.len = ri->iri_len; 26754c7070dbSScott Long m->m_pkthdr.rcvif = ri->iri_ifp; 26764c7070dbSScott Long m->m_flags |= ri->iri_flags; 26774c7070dbSScott Long m->m_pkthdr.ether_vtag = ri->iri_vtag; 26784c7070dbSScott Long m->m_pkthdr.flowid = ri->iri_flowid; 26794c7070dbSScott Long M_HASHTYPE_SET(m, ri->iri_rsstype); 26804c7070dbSScott Long m->m_pkthdr.csum_flags = ri->iri_csum_flags; 26814c7070dbSScott Long m->m_pkthdr.csum_data = ri->iri_csum_data; 26824c7070dbSScott Long return (m); 26834c7070dbSScott Long } 26844c7070dbSScott Long 268535e4e998SStephen Hurd #if defined(INET6) || defined(INET) 2686fe1bcadaSStephen Hurd static void 2687fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6) 2688fe1bcadaSStephen Hurd { 2689fe1bcadaSStephen Hurd CURVNET_SET(lc->ifp->if_vnet); 2690fe1bcadaSStephen Hurd #if defined(INET6) 2691*188adcb7SMarko Zec *v6 = V_ip6_forwarding; 2692fe1bcadaSStephen Hurd #endif 2693fe1bcadaSStephen Hurd #if defined(INET) 2694*188adcb7SMarko Zec *v4 = V_ipforwarding; 2695fe1bcadaSStephen Hurd #endif 2696fe1bcadaSStephen Hurd CURVNET_RESTORE(); 2697fe1bcadaSStephen Hurd } 2698fe1bcadaSStephen Hurd 269935e4e998SStephen Hurd /* 270035e4e998SStephen Hurd * Returns true if it's possible this packet could be LROed. 270135e4e998SStephen Hurd * if it returns false, it is guaranteed that tcp_lro_rx() 270235e4e998SStephen Hurd * would not return zero. 270335e4e998SStephen Hurd */ 270435e4e998SStephen Hurd static bool 2705fe1bcadaSStephen Hurd iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding) 270635e4e998SStephen Hurd { 270735e4e998SStephen Hurd struct ether_header *eh; 270835e4e998SStephen Hurd 270935e4e998SStephen Hurd eh = mtod(m, struct ether_header *); 27106aee0bfaSMarko Zec switch (eh->ether_type) { 2711abec4724SSean Bruno #if defined(INET6) 27126aee0bfaSMarko Zec case htons(ETHERTYPE_IPV6): 27136aee0bfaSMarko Zec return (!v6_forwarding); 2714abec4724SSean Bruno #endif 2715abec4724SSean Bruno #if defined (INET) 27166aee0bfaSMarko Zec case htons(ETHERTYPE_IP): 27176aee0bfaSMarko Zec return (!v4_forwarding); 2718abec4724SSean Bruno #endif 271935e4e998SStephen Hurd } 272035e4e998SStephen Hurd 272135e4e998SStephen Hurd return false; 272235e4e998SStephen Hurd } 2723fe1bcadaSStephen Hurd #else 2724fe1bcadaSStephen Hurd static void 2725fe1bcadaSStephen Hurd iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused) 2726fe1bcadaSStephen Hurd { 2727fe1bcadaSStephen Hurd } 272835e4e998SStephen Hurd #endif 272935e4e998SStephen Hurd 27304c7070dbSScott Long static bool 273195246abbSSean Bruno iflib_rxeof(iflib_rxq_t rxq, qidx_t budget) 27324c7070dbSScott Long { 27331722eeacSMarius Strobl if_t ifp; 27344c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 27354c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 273623ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 27374c7070dbSScott Long int avail, i; 273895246abbSSean Bruno qidx_t *cidxp; 27394c7070dbSScott Long struct if_rxd_info ri; 27404c7070dbSScott Long int err, budget_left, rx_bytes, rx_pkts; 27414c7070dbSScott Long iflib_fl_t fl; 27424c7070dbSScott Long int lro_enabled; 2743f6cb0deaSMatt Macy bool v4_forwarding, v6_forwarding, lro_possible; 274495246abbSSean Bruno 27454c7070dbSScott Long /* 27464c7070dbSScott Long * XXX early demux data packets so that if_input processing only handles 27474c7070dbSScott Long * acks in interrupt context 27484c7070dbSScott Long */ 274920f63282SStephen Hurd struct mbuf *m, *mh, *mt, *mf; 27504c7070dbSScott Long 2751f6cb0deaSMatt Macy lro_possible = v4_forwarding = v6_forwarding = false; 275295246abbSSean Bruno ifp = ctx->ifc_ifp; 27534c7070dbSScott Long mh = mt = NULL; 27544c7070dbSScott Long MPASS(budget > 0); 27554c7070dbSScott Long rx_pkts = rx_bytes = 0; 275623ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) 27574c7070dbSScott Long cidxp = &rxq->ifr_cq_cidx; 27584c7070dbSScott Long else 27594c7070dbSScott Long cidxp = &rxq->ifr_fl[0].ifl_cidx; 276023ac9029SStephen Hurd if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) { 27614c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 27624c7070dbSScott Long __iflib_fl_refill_lt(ctx, fl, budget + 8); 27634c7070dbSScott Long DBG_COUNTER_INC(rx_unavail); 27644c7070dbSScott Long return (false); 27654c7070dbSScott Long } 27664c7070dbSScott Long 27676d49b41eSAndrew Gallatin /* pfil needs the vnet to be set */ 27686d49b41eSAndrew Gallatin CURVNET_SET_QUIET(ifp->if_vnet); 27698b8d9093SMarius Strobl for (budget_left = budget; budget_left > 0 && avail > 0;) { 27704c7070dbSScott Long if (__predict_false(!CTX_ACTIVE(ctx))) { 27714c7070dbSScott Long DBG_COUNTER_INC(rx_ctx_inactive); 27724c7070dbSScott Long break; 27734c7070dbSScott Long } 27744c7070dbSScott Long /* 27754c7070dbSScott Long * Reset client set fields to their default values 27764c7070dbSScott Long */ 277795246abbSSean Bruno rxd_info_zero(&ri); 27784c7070dbSScott Long ri.iri_qsidx = rxq->ifr_id; 27794c7070dbSScott Long ri.iri_cidx = *cidxp; 278095246abbSSean Bruno ri.iri_ifp = ifp; 27814c7070dbSScott Long ri.iri_frags = rxq->ifr_frags; 27824c7070dbSScott Long err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri); 27834c7070dbSScott Long 278495246abbSSean Bruno if (err) 278595246abbSSean Bruno goto err; 27866d49b41eSAndrew Gallatin rx_pkts += 1; 27876d49b41eSAndrew Gallatin rx_bytes += ri.iri_len; 278823ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 278923ac9029SStephen Hurd *cidxp = ri.iri_cidx; 279023ac9029SStephen Hurd /* Update our consumer index */ 279195246abbSSean Bruno /* XXX NB: shurd - check if this is still safe */ 27921722eeacSMarius Strobl while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0]) 279323ac9029SStephen Hurd rxq->ifr_cq_cidx -= scctx->isc_nrxd[0]; 27944c7070dbSScott Long /* was this only a completion queue message? */ 27954c7070dbSScott Long if (__predict_false(ri.iri_nfrags == 0)) 27964c7070dbSScott Long continue; 27974c7070dbSScott Long } 27984c7070dbSScott Long MPASS(ri.iri_nfrags != 0); 27994c7070dbSScott Long MPASS(ri.iri_len != 0); 28004c7070dbSScott Long 28014c7070dbSScott Long /* will advance the cidx on the corresponding free lists */ 28024c7070dbSScott Long m = iflib_rxd_pkt_get(rxq, &ri); 28038b8d9093SMarius Strobl avail--; 28048b8d9093SMarius Strobl budget_left--; 28054c7070dbSScott Long if (avail == 0 && budget_left) 280623ac9029SStephen Hurd avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left); 28074c7070dbSScott Long 28086d49b41eSAndrew Gallatin if (__predict_false(m == NULL)) 28094c7070dbSScott Long continue; 28106d49b41eSAndrew Gallatin 28114c7070dbSScott Long /* imm_pkt: -- cxgb */ 28124c7070dbSScott Long if (mh == NULL) 28134c7070dbSScott Long mh = mt = m; 28144c7070dbSScott Long else { 28154c7070dbSScott Long mt->m_nextpkt = m; 28164c7070dbSScott Long mt = m; 28174c7070dbSScott Long } 28184c7070dbSScott Long } 28196d49b41eSAndrew Gallatin CURVNET_RESTORE(); 28204c7070dbSScott Long /* make sure that we can refill faster than drain */ 28214c7070dbSScott Long for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++) 2822ab2e3f79SStephen Hurd __iflib_fl_refill_lt(ctx, fl, budget + 8); 28234c7070dbSScott Long 28244c7070dbSScott Long lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO); 2825fe1bcadaSStephen Hurd if (lro_enabled) 2826fe1bcadaSStephen Hurd iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding); 282720f63282SStephen Hurd mt = mf = NULL; 28284c7070dbSScott Long while (mh != NULL) { 28294c7070dbSScott Long m = mh; 28304c7070dbSScott Long mh = mh->m_nextpkt; 28314c7070dbSScott Long m->m_nextpkt = NULL; 283295246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 283395246abbSSean Bruno if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL) 283495246abbSSean Bruno continue; 283595246abbSSean Bruno #endif 28364c7070dbSScott Long rx_bytes += m->m_pkthdr.len; 28374c7070dbSScott Long rx_pkts++; 2838aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 283935e4e998SStephen Hurd if (lro_enabled) { 284035e4e998SStephen Hurd if (!lro_possible) { 2841fe1bcadaSStephen Hurd lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding); 284235e4e998SStephen Hurd if (lro_possible && mf != NULL) { 284335e4e998SStephen Hurd ifp->if_input(ifp, mf); 284435e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 284535e4e998SStephen Hurd mt = mf = NULL; 284635e4e998SStephen Hurd } 284735e4e998SStephen Hurd } 284825ac1dd5SStephen Hurd if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) == 284925ac1dd5SStephen Hurd (CSUM_L4_CALC|CSUM_L4_VALID)) { 285035e4e998SStephen Hurd if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0) 28514c7070dbSScott Long continue; 285220f63282SStephen Hurd } 285325ac1dd5SStephen Hurd } 2854aaeb188aSBjoern A. Zeeb #endif 285535e4e998SStephen Hurd if (lro_possible) { 285635e4e998SStephen Hurd ifp->if_input(ifp, m); 285735e4e998SStephen Hurd DBG_COUNTER_INC(rx_if_input); 285835e4e998SStephen Hurd continue; 285935e4e998SStephen Hurd } 286035e4e998SStephen Hurd 286135e4e998SStephen Hurd if (mf == NULL) 286235e4e998SStephen Hurd mf = m; 286320f63282SStephen Hurd if (mt != NULL) 286420f63282SStephen Hurd mt->m_nextpkt = m; 286520f63282SStephen Hurd mt = m; 286620f63282SStephen Hurd } 286720f63282SStephen Hurd if (mf != NULL) { 286820f63282SStephen Hurd ifp->if_input(ifp, mf); 28694c7070dbSScott Long DBG_COUNTER_INC(rx_if_input); 28704c7070dbSScott Long } 287123ac9029SStephen Hurd 28724c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes); 28734c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts); 28744c7070dbSScott Long 28754c7070dbSScott Long /* 28764c7070dbSScott Long * Flush any outstanding LRO work 28774c7070dbSScott Long */ 2878aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 287923ac9029SStephen Hurd tcp_lro_flush_all(&rxq->ifr_lc); 2880aaeb188aSBjoern A. Zeeb #endif 2881ab2e3f79SStephen Hurd if (avail) 2882ab2e3f79SStephen Hurd return true; 2883ab2e3f79SStephen Hurd return (iflib_rxd_avail(ctx, rxq, *cidxp, 1)); 288495246abbSSean Bruno err: 28857b610b60SSean Bruno STATE_LOCK(ctx); 2886ab2e3f79SStephen Hurd ctx->ifc_flags |= IFC_DO_RESET; 2887940f62d6SEric Joyner iflib_admin_intr_deferred(ctx); 288846fa0c25SEric Joyner STATE_UNLOCK(ctx); 288995246abbSSean Bruno return (false); 289095246abbSSean Bruno } 289195246abbSSean Bruno 289295246abbSSean Bruno #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1) 289395246abbSSean Bruno static inline qidx_t 289495246abbSSean Bruno txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use) 289595246abbSSean Bruno { 289695246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 289795246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 289895246abbSSean Bruno if (in_use > 4*minthresh) 289995246abbSSean Bruno return (notify_count); 290095246abbSSean Bruno if (in_use > 2*minthresh) 290195246abbSSean Bruno return (notify_count >> 1); 290295246abbSSean Bruno if (in_use > minthresh) 290395246abbSSean Bruno return (notify_count >> 3); 290495246abbSSean Bruno return (0); 290595246abbSSean Bruno } 290695246abbSSean Bruno 290795246abbSSean Bruno static inline qidx_t 290895246abbSSean Bruno txq_max_rs_deferred(iflib_txq_t txq) 290995246abbSSean Bruno { 291095246abbSSean Bruno qidx_t notify_count = TXD_NOTIFY_COUNT(txq); 291195246abbSSean Bruno qidx_t minthresh = txq->ift_size / 8; 291295246abbSSean Bruno if (txq->ift_in_use > 4*minthresh) 291395246abbSSean Bruno return (notify_count); 291495246abbSSean Bruno if (txq->ift_in_use > 2*minthresh) 291595246abbSSean Bruno return (notify_count >> 1); 291695246abbSSean Bruno if (txq->ift_in_use > minthresh) 291795246abbSSean Bruno return (notify_count >> 2); 29182b2fc973SSean Bruno return (2); 29194c7070dbSScott Long } 29204c7070dbSScott Long 29214c7070dbSScott Long #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags) 29224c7070dbSScott Long #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG) 292395246abbSSean Bruno 292495246abbSSean Bruno #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use)) 292595246abbSSean Bruno #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq) 292623ac9029SStephen Hurd #define TXQ_MAX_DB_CONSUMED(size) (size >> 4) 29274c7070dbSScott Long 292895246abbSSean Bruno /* forward compatibility for cxgb */ 292995246abbSSean Bruno #define FIRST_QSET(ctx) 0 293095246abbSSean Bruno #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets) 293195246abbSSean Bruno #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets) 293295246abbSSean Bruno #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx)) 293395246abbSSean Bruno #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments)) 293495246abbSSean Bruno 293595246abbSSean Bruno /* XXX we should be setting this to something other than zero */ 293695246abbSSean Bruno #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh) 29377474544bSMarius Strobl #define MAX_TX_DESC(ctx) max((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \ 29387474544bSMarius Strobl (ctx)->ifc_softc_ctx.isc_tx_nsegments) 293995246abbSSean Bruno 294095246abbSSean Bruno static inline bool 294195246abbSSean Bruno iflib_txd_db_check(if_ctx_t ctx, iflib_txq_t txq, int ring, qidx_t in_use) 29424c7070dbSScott Long { 294395246abbSSean Bruno qidx_t dbval, max; 294495246abbSSean Bruno bool rang; 29454c7070dbSScott Long 294695246abbSSean Bruno rang = false; 294795246abbSSean Bruno max = TXQ_MAX_DB_DEFERRED(txq, in_use); 294895246abbSSean Bruno if (ring || txq->ift_db_pending >= max) { 29494c7070dbSScott Long dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx; 295095dcf343SMarius Strobl bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 295195dcf343SMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 29524c7070dbSScott Long ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval); 29534c7070dbSScott Long txq->ift_db_pending = txq->ift_npending = 0; 295495246abbSSean Bruno rang = true; 29554c7070dbSScott Long } 295695246abbSSean Bruno return (rang); 29574c7070dbSScott Long } 29584c7070dbSScott Long 29594c7070dbSScott Long #ifdef PKT_DEBUG 29604c7070dbSScott Long static void 29614c7070dbSScott Long print_pkt(if_pkt_info_t pi) 29624c7070dbSScott Long { 29634c7070dbSScott Long printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n", 29644c7070dbSScott Long pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx); 29654c7070dbSScott Long printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n", 29664c7070dbSScott Long pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag); 29674c7070dbSScott Long printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n", 29684c7070dbSScott Long pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto); 29694c7070dbSScott Long } 29704c7070dbSScott Long #endif 29714c7070dbSScott Long 29724c7070dbSScott Long #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO) 2973a06424ddSEric Joyner #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO)) 29744c7070dbSScott Long #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO) 2975a06424ddSEric Joyner #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO)) 29764c7070dbSScott Long 29774c7070dbSScott Long static int 29784c7070dbSScott Long iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp) 29794c7070dbSScott Long { 2980ab2e3f79SStephen Hurd if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx; 29814c7070dbSScott Long struct ether_vlan_header *eh; 2982c9a49a4fSMarius Strobl struct mbuf *m; 29834c7070dbSScott Long 29848b8d9093SMarius Strobl m = *mp; 2985ab2e3f79SStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) && 2986ab2e3f79SStephen Hurd M_WRITABLE(m) == 0) { 2987ab2e3f79SStephen Hurd if ((m = m_dup(m, M_NOWAIT)) == NULL) { 2988ab2e3f79SStephen Hurd return (ENOMEM); 2989ab2e3f79SStephen Hurd } else { 2990ab2e3f79SStephen Hurd m_freem(*mp); 299164e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 29928b8d9093SMarius Strobl *mp = m; 2993ab2e3f79SStephen Hurd } 2994ab2e3f79SStephen Hurd } 29951248952aSSean Bruno 29964c7070dbSScott Long /* 29974c7070dbSScott Long * Determine where frame payload starts. 29984c7070dbSScott Long * Jump over vlan headers if already present, 29994c7070dbSScott Long * helpful for QinQ too. 30004c7070dbSScott Long */ 30014c7070dbSScott Long if (__predict_false(m->m_len < sizeof(*eh))) { 30024c7070dbSScott Long txq->ift_pullups++; 30034c7070dbSScott Long if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL)) 30044c7070dbSScott Long return (ENOMEM); 30054c7070dbSScott Long } 30064c7070dbSScott Long eh = mtod(m, struct ether_vlan_header *); 30074c7070dbSScott Long if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) { 30084c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_proto); 30094c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 30104c7070dbSScott Long } else { 30114c7070dbSScott Long pi->ipi_etype = ntohs(eh->evl_encap_proto); 30124c7070dbSScott Long pi->ipi_ehdrlen = ETHER_HDR_LEN; 30134c7070dbSScott Long } 30144c7070dbSScott Long 30154c7070dbSScott Long switch (pi->ipi_etype) { 30164c7070dbSScott Long #ifdef INET 30174c7070dbSScott Long case ETHERTYPE_IP: 30184c7070dbSScott Long { 3019c9a49a4fSMarius Strobl struct mbuf *n; 30204c7070dbSScott Long struct ip *ip = NULL; 30214c7070dbSScott Long struct tcphdr *th = NULL; 30224c7070dbSScott Long int minthlen; 30234c7070dbSScott Long 30244c7070dbSScott Long minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th)); 30254c7070dbSScott Long if (__predict_false(m->m_len < minthlen)) { 30264c7070dbSScott Long /* 30274c7070dbSScott Long * if this code bloat is causing too much of a hit 30284c7070dbSScott Long * move it to a separate function and mark it noinline 30294c7070dbSScott Long */ 30304c7070dbSScott Long if (m->m_len == pi->ipi_ehdrlen) { 30314c7070dbSScott Long n = m->m_next; 30324c7070dbSScott Long MPASS(n); 30334c7070dbSScott Long if (n->m_len >= sizeof(*ip)) { 30344c7070dbSScott Long ip = (struct ip *)n->m_data; 30354c7070dbSScott Long if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30364c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30374c7070dbSScott Long } else { 30384c7070dbSScott Long txq->ift_pullups++; 30394c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 30404c7070dbSScott Long return (ENOMEM); 30414c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30424c7070dbSScott Long } 30434c7070dbSScott Long } else { 30444c7070dbSScott Long txq->ift_pullups++; 30454c7070dbSScott Long if (__predict_false((m = m_pullup(m, minthlen)) == NULL)) 30464c7070dbSScott Long return (ENOMEM); 30474c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30484c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30494c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30504c7070dbSScott Long } 30514c7070dbSScott Long } else { 30524c7070dbSScott Long ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen); 30534c7070dbSScott Long if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th)) 30544c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 30554c7070dbSScott Long } 30564c7070dbSScott Long pi->ipi_ip_hlen = ip->ip_hl << 2; 30574c7070dbSScott Long pi->ipi_ipproto = ip->ip_p; 30584c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV4; 30594c7070dbSScott Long 3060a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3061a06424ddSEric Joyner if (IS_TX_OFFLOAD4(pi)) { 3062a06424ddSEric Joyner if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) { 30634c7070dbSScott Long if (__predict_false(th == NULL)) { 30644c7070dbSScott Long txq->ift_pullups++; 30654c7070dbSScott Long if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL)) 30664c7070dbSScott Long return (ENOMEM); 30674c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen); 30684c7070dbSScott Long } 30694c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 30704c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 30714c7070dbSScott Long pi->ipi_tcp_seq = th->th_seq; 30724c7070dbSScott Long } 3073a06424ddSEric Joyner if (IS_TSO4(pi)) { 30744c7070dbSScott Long if (__predict_false(ip->ip_p != IPPROTO_TCP)) 30754c7070dbSScott Long return (ENXIO); 30768d4ceb9cSStephen Hurd /* 30778d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 30788d4ceb9cSStephen Hurd */ 30798d4ceb9cSStephen Hurd pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP); 30804c7070dbSScott Long th->th_sum = in_pseudo(ip->ip_src.s_addr, 30814c7070dbSScott Long ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 30824c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 30831248952aSSean Bruno if (sctx->isc_flags & IFLIB_TSO_INIT_IP) { 30841248952aSSean Bruno ip->ip_sum = 0; 30851248952aSSean Bruno ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz); 30861248952aSSean Bruno } 30874c7070dbSScott Long } 3088a06424ddSEric Joyner } 30898d4ceb9cSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP)) 30908d4ceb9cSStephen Hurd ip->ip_sum = 0; 30918d4ceb9cSStephen Hurd 30924c7070dbSScott Long break; 30934c7070dbSScott Long } 30944c7070dbSScott Long #endif 30954c7070dbSScott Long #ifdef INET6 30964c7070dbSScott Long case ETHERTYPE_IPV6: 30974c7070dbSScott Long { 30984c7070dbSScott Long struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen); 30994c7070dbSScott Long struct tcphdr *th; 31004c7070dbSScott Long pi->ipi_ip_hlen = sizeof(struct ip6_hdr); 31014c7070dbSScott Long 31024c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) { 310364e6fc13SStephen Hurd txq->ift_pullups++; 31044c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL)) 31054c7070dbSScott Long return (ENOMEM); 31064c7070dbSScott Long } 31074c7070dbSScott Long th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen); 31084c7070dbSScott Long 31094c7070dbSScott Long /* XXX-BZ this will go badly in case of ext hdrs. */ 31104c7070dbSScott Long pi->ipi_ipproto = ip6->ip6_nxt; 31114c7070dbSScott Long pi->ipi_flags |= IPI_TX_IPV6; 31124c7070dbSScott Long 3113a06424ddSEric Joyner /* TCP checksum offload may require TCP header length */ 3114a06424ddSEric Joyner if (IS_TX_OFFLOAD6(pi)) { 31154c7070dbSScott Long if (pi->ipi_ipproto == IPPROTO_TCP) { 31164c7070dbSScott Long if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) { 3117a06424ddSEric Joyner txq->ift_pullups++; 31184c7070dbSScott Long if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL)) 31194c7070dbSScott Long return (ENOMEM); 31204c7070dbSScott Long } 31214c7070dbSScott Long pi->ipi_tcp_hflags = th->th_flags; 31224c7070dbSScott Long pi->ipi_tcp_hlen = th->th_off << 2; 3123a06424ddSEric Joyner pi->ipi_tcp_seq = th->th_seq; 31244c7070dbSScott Long } 3125a06424ddSEric Joyner if (IS_TSO6(pi)) { 31264c7070dbSScott Long if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP)) 31274c7070dbSScott Long return (ENXIO); 31284c7070dbSScott Long /* 31298d4ceb9cSStephen Hurd * TSO always requires hardware checksum offload. 31304c7070dbSScott Long */ 3131a06424ddSEric Joyner pi->ipi_csum_flags |= CSUM_IP6_TCP; 31324c7070dbSScott Long th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0); 31334c7070dbSScott Long pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz; 31344c7070dbSScott Long } 3135a06424ddSEric Joyner } 31364c7070dbSScott Long break; 31374c7070dbSScott Long } 31384c7070dbSScott Long #endif 31394c7070dbSScott Long default: 31404c7070dbSScott Long pi->ipi_csum_flags &= ~CSUM_OFFLOAD; 31414c7070dbSScott Long pi->ipi_ip_hlen = 0; 31424c7070dbSScott Long break; 31434c7070dbSScott Long } 31444c7070dbSScott Long *mp = m; 31451248952aSSean Bruno 31464c7070dbSScott Long return (0); 31474c7070dbSScott Long } 31484c7070dbSScott Long 31494c7070dbSScott Long /* 31504c7070dbSScott Long * If dodgy hardware rejects the scatter gather chain we've handed it 315123ac9029SStephen Hurd * we'll need to remove the mbuf chain from ifsg_m[] before we can add the 315223ac9029SStephen Hurd * m_defrag'd mbufs 31534c7070dbSScott Long */ 31544c7070dbSScott Long static __noinline struct mbuf * 315523ac9029SStephen Hurd iflib_remove_mbuf(iflib_txq_t txq) 31564c7070dbSScott Long { 3157fbec776dSAndrew Gallatin int ntxd, pidx; 3158fbec776dSAndrew Gallatin struct mbuf *m, **ifsd_m; 31594c7070dbSScott Long 31604c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 316123ac9029SStephen Hurd ntxd = txq->ift_size; 3162fbec776dSAndrew Gallatin pidx = txq->ift_pidx & (ntxd - 1); 3163fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 3164fbec776dSAndrew Gallatin m = ifsd_m[pidx]; 31654c7070dbSScott Long ifsd_m[pidx] = NULL; 3166bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]); 31678a04b53dSKonstantin Belousov if (txq->ift_sds.ifsd_tso_map != NULL) 3168bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 31698a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[pidx]); 31704c7070dbSScott Long #if MEMORY_LOGGING 31714c7070dbSScott Long txq->ift_dequeued++; 31724c7070dbSScott Long #endif 3173fbec776dSAndrew Gallatin return (m); 31744c7070dbSScott Long } 31754c7070dbSScott Long 317695246abbSSean Bruno static inline caddr_t 317795246abbSSean Bruno calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid) 317895246abbSSean Bruno { 317995246abbSSean Bruno qidx_t size; 318095246abbSSean Bruno int ntxd; 318195246abbSSean Bruno caddr_t start, end, cur, next; 318295246abbSSean Bruno 318395246abbSSean Bruno ntxd = txq->ift_size; 318495246abbSSean Bruno size = txq->ift_txd_size[qid]; 318595246abbSSean Bruno start = txq->ift_ifdi[qid].idi_vaddr; 318695246abbSSean Bruno 318795246abbSSean Bruno if (__predict_false(size == 0)) 318895246abbSSean Bruno return (start); 318995246abbSSean Bruno cur = start + size*cidx; 319095246abbSSean Bruno end = start + size*ntxd; 319195246abbSSean Bruno next = CACHE_PTR_NEXT(cur); 319295246abbSSean Bruno return (next < end ? next : start); 319395246abbSSean Bruno } 319495246abbSSean Bruno 3195d14c853bSStephen Hurd /* 3196d14c853bSStephen Hurd * Pad an mbuf to ensure a minimum ethernet frame size. 3197d14c853bSStephen Hurd * min_frame_size is the frame size (less CRC) to pad the mbuf to 3198d14c853bSStephen Hurd */ 3199d14c853bSStephen Hurd static __noinline int 3200a15fbbb8SStephen Hurd iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size) 3201d14c853bSStephen Hurd { 3202d14c853bSStephen Hurd /* 3203d14c853bSStephen Hurd * 18 is enough bytes to pad an ARP packet to 46 bytes, and 3204d14c853bSStephen Hurd * and ARP message is the smallest common payload I can think of 3205d14c853bSStephen Hurd */ 3206d14c853bSStephen Hurd static char pad[18]; /* just zeros */ 3207d14c853bSStephen Hurd int n; 3208a15fbbb8SStephen Hurd struct mbuf *new_head; 3209d14c853bSStephen Hurd 3210a15fbbb8SStephen Hurd if (!M_WRITABLE(*m_head)) { 3211a15fbbb8SStephen Hurd new_head = m_dup(*m_head, M_NOWAIT); 3212a15fbbb8SStephen Hurd if (new_head == NULL) { 321304993890SStephen Hurd m_freem(*m_head); 3214a15fbbb8SStephen Hurd device_printf(dev, "cannot pad short frame, m_dup() failed"); 321506c47d48SStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 321664e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3217a15fbbb8SStephen Hurd return ENOMEM; 3218a15fbbb8SStephen Hurd } 3219a15fbbb8SStephen Hurd m_freem(*m_head); 3220a15fbbb8SStephen Hurd *m_head = new_head; 3221a15fbbb8SStephen Hurd } 3222a15fbbb8SStephen Hurd 3223a15fbbb8SStephen Hurd for (n = min_frame_size - (*m_head)->m_pkthdr.len; 3224d14c853bSStephen Hurd n > 0; n -= sizeof(pad)) 3225a15fbbb8SStephen Hurd if (!m_append(*m_head, min(n, sizeof(pad)), pad)) 3226d14c853bSStephen Hurd break; 3227d14c853bSStephen Hurd 3228d14c853bSStephen Hurd if (n > 0) { 3229a15fbbb8SStephen Hurd m_freem(*m_head); 3230d14c853bSStephen Hurd device_printf(dev, "cannot pad short frame\n"); 3231d14c853bSStephen Hurd DBG_COUNTER_INC(encap_pad_mbuf_fail); 323264e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3233d14c853bSStephen Hurd return (ENOBUFS); 3234d14c853bSStephen Hurd } 3235d14c853bSStephen Hurd 3236d14c853bSStephen Hurd return 0; 3237d14c853bSStephen Hurd } 3238d14c853bSStephen Hurd 32394c7070dbSScott Long static int 32404c7070dbSScott Long iflib_encap(iflib_txq_t txq, struct mbuf **m_headp) 32414c7070dbSScott Long { 32424c7070dbSScott Long if_ctx_t ctx; 32434c7070dbSScott Long if_shared_ctx_t sctx; 32444c7070dbSScott Long if_softc_ctx_t scctx; 3245bfce461eSMarius Strobl bus_dma_tag_t buf_tag; 32464c7070dbSScott Long bus_dma_segment_t *segs; 3247fbec776dSAndrew Gallatin struct mbuf *m_head, **ifsd_m; 324895246abbSSean Bruno void *next_txd; 32494c7070dbSScott Long bus_dmamap_t map; 32504c7070dbSScott Long struct if_pkt_info pi; 32514c7070dbSScott Long int remap = 0; 32524c7070dbSScott Long int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd; 32534c7070dbSScott Long 32544c7070dbSScott Long ctx = txq->ift_ctx; 32554c7070dbSScott Long sctx = ctx->ifc_sctx; 32564c7070dbSScott Long scctx = &ctx->ifc_softc_ctx; 32574c7070dbSScott Long segs = txq->ift_segs; 325823ac9029SStephen Hurd ntxd = txq->ift_size; 32594c7070dbSScott Long m_head = *m_headp; 32604c7070dbSScott Long map = NULL; 32614c7070dbSScott Long 32624c7070dbSScott Long /* 32634c7070dbSScott Long * If we're doing TSO the next descriptor to clean may be quite far ahead 32644c7070dbSScott Long */ 32654c7070dbSScott Long cidx = txq->ift_cidx; 32664c7070dbSScott Long pidx = txq->ift_pidx; 326795246abbSSean Bruno if (ctx->ifc_flags & IFC_PREFETCH) { 32684c7070dbSScott Long next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1); 326995246abbSSean Bruno if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) { 327095246abbSSean Bruno next_txd = calc_next_txd(txq, cidx, 0); 327195246abbSSean Bruno prefetch(next_txd); 327295246abbSSean Bruno } 32734c7070dbSScott Long 32744c7070dbSScott Long /* prefetch the next cache line of mbuf pointers and flags */ 32754c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_m[next]); 32764c7070dbSScott Long prefetch(&txq->ift_sds.ifsd_map[next]); 32774c7070dbSScott Long next = (cidx + CACHE_LINE_SIZE) & (ntxd-1); 32784c7070dbSScott Long } 327995246abbSSean Bruno map = txq->ift_sds.ifsd_map[pidx]; 3280fbec776dSAndrew Gallatin ifsd_m = txq->ift_sds.ifsd_m; 32814c7070dbSScott Long 32824c7070dbSScott Long if (m_head->m_pkthdr.csum_flags & CSUM_TSO) { 3283bfce461eSMarius Strobl buf_tag = txq->ift_tso_buf_tag; 32844c7070dbSScott Long max_segs = scctx->isc_tx_tso_segments_max; 32858a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_tso_map[pidx]; 3286bfce461eSMarius Strobl MPASS(buf_tag != NULL); 32877f87c040SMarius Strobl MPASS(max_segs > 0); 32884c7070dbSScott Long } else { 3289bfce461eSMarius Strobl buf_tag = txq->ift_buf_tag; 32904c7070dbSScott Long max_segs = scctx->isc_tx_nsegments; 32918a04b53dSKonstantin Belousov map = txq->ift_sds.ifsd_map[pidx]; 32924c7070dbSScott Long } 3293d14c853bSStephen Hurd if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) && 3294d14c853bSStephen Hurd __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) { 3295a15fbbb8SStephen Hurd err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size); 329664e6fc13SStephen Hurd if (err) { 329764e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 3298d14c853bSStephen Hurd return err; 3299d14c853bSStephen Hurd } 330064e6fc13SStephen Hurd } 3301a15fbbb8SStephen Hurd m_head = *m_headp; 330295246abbSSean Bruno 330395246abbSSean Bruno pkt_info_zero(&pi); 3304ab2e3f79SStephen Hurd pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST)); 3305ab2e3f79SStephen Hurd pi.ipi_pidx = pidx; 3306ab2e3f79SStephen Hurd pi.ipi_qsidx = txq->ift_id; 33073429c02fSStephen Hurd pi.ipi_len = m_head->m_pkthdr.len; 33083429c02fSStephen Hurd pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags; 33091722eeacSMarius Strobl pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0; 33104c7070dbSScott Long 33114c7070dbSScott Long /* deliberate bitwise OR to make one condition */ 33124c7070dbSScott Long if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) { 331364e6fc13SStephen Hurd if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) { 331464e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 33154c7070dbSScott Long return (err); 331664e6fc13SStephen Hurd } 33174c7070dbSScott Long m_head = *m_headp; 33184c7070dbSScott Long } 33194c7070dbSScott Long 33204c7070dbSScott Long retry: 3321bfce461eSMarius Strobl err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs, 3322fbec776dSAndrew Gallatin BUS_DMA_NOWAIT); 33234c7070dbSScott Long defrag: 33244c7070dbSScott Long if (__predict_false(err)) { 33254c7070dbSScott Long switch (err) { 33264c7070dbSScott Long case EFBIG: 33274c7070dbSScott Long /* try collapse once and defrag once */ 3328f7594707SAndrew Gallatin if (remap == 0) { 33294c7070dbSScott Long m_head = m_collapse(*m_headp, M_NOWAIT, max_segs); 3330f7594707SAndrew Gallatin /* try defrag if collapsing fails */ 3331f7594707SAndrew Gallatin if (m_head == NULL) 3332f7594707SAndrew Gallatin remap++; 3333f7594707SAndrew Gallatin } 333464e6fc13SStephen Hurd if (remap == 1) { 333564e6fc13SStephen Hurd txq->ift_mbuf_defrag++; 33364c7070dbSScott Long m_head = m_defrag(*m_headp, M_NOWAIT); 333764e6fc13SStephen Hurd } 33383e8d1baeSEric Joyner /* 33393e8d1baeSEric Joyner * remap should never be >1 unless bus_dmamap_load_mbuf_sg 33403e8d1baeSEric Joyner * failed to map an mbuf that was run through m_defrag 33413e8d1baeSEric Joyner */ 33423e8d1baeSEric Joyner MPASS(remap <= 1); 33433e8d1baeSEric Joyner if (__predict_false(m_head == NULL || remap > 1)) 33444c7070dbSScott Long goto defrag_failed; 33453e8d1baeSEric Joyner remap++; 33464c7070dbSScott Long *m_headp = m_head; 33474c7070dbSScott Long goto retry; 33484c7070dbSScott Long break; 33494c7070dbSScott Long case ENOMEM: 33504c7070dbSScott Long txq->ift_no_tx_dma_setup++; 33514c7070dbSScott Long break; 33524c7070dbSScott Long default: 33534c7070dbSScott Long txq->ift_no_tx_dma_setup++; 33544c7070dbSScott Long m_freem(*m_headp); 33554c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 33564c7070dbSScott Long *m_headp = NULL; 33574c7070dbSScott Long break; 33584c7070dbSScott Long } 33594c7070dbSScott Long txq->ift_map_failed++; 33604c7070dbSScott Long DBG_COUNTER_INC(encap_load_mbuf_fail); 336164e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 33624c7070dbSScott Long return (err); 33634c7070dbSScott Long } 3364fbec776dSAndrew Gallatin ifsd_m[pidx] = m_head; 33654c7070dbSScott Long /* 33664c7070dbSScott Long * XXX assumes a 1 to 1 relationship between segments and 33674c7070dbSScott Long * descriptors - this does not hold true on all drivers, e.g. 33684c7070dbSScott Long * cxgb 33694c7070dbSScott Long */ 33704c7070dbSScott Long if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) { 33714c7070dbSScott Long txq->ift_no_desc_avail++; 3372bfce461eSMarius Strobl bus_dmamap_unload(buf_tag, map); 33734c7070dbSScott Long DBG_COUNTER_INC(encap_txq_avail_fail); 337464e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 337523ac9029SStephen Hurd if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0) 33764c7070dbSScott Long GROUPTASK_ENQUEUE(&txq->ift_task); 33774c7070dbSScott Long return (ENOBUFS); 33784c7070dbSScott Long } 337995246abbSSean Bruno /* 338095246abbSSean Bruno * On Intel cards we can greatly reduce the number of TX interrupts 338195246abbSSean Bruno * we see by only setting report status on every Nth descriptor. 338295246abbSSean Bruno * However, this also means that the driver will need to keep track 338395246abbSSean Bruno * of the descriptors that RS was set on to check them for the DD bit. 338495246abbSSean Bruno */ 338595246abbSSean Bruno txq->ift_rs_pending += nsegs + 1; 338695246abbSSean Bruno if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) || 33871f7ce05dSAndrew Gallatin iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) { 338895246abbSSean Bruno pi.ipi_flags |= IPI_TX_INTR; 338995246abbSSean Bruno txq->ift_rs_pending = 0; 339095246abbSSean Bruno } 339195246abbSSean Bruno 33924c7070dbSScott Long pi.ipi_segs = segs; 33934c7070dbSScott Long pi.ipi_nsegs = nsegs; 33944c7070dbSScott Long 339523ac9029SStephen Hurd MPASS(pidx >= 0 && pidx < txq->ift_size); 33964c7070dbSScott Long #ifdef PKT_DEBUG 33974c7070dbSScott Long print_pkt(&pi); 33984c7070dbSScott Long #endif 33994c7070dbSScott Long if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) { 340095dcf343SMarius Strobl bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE); 34014c7070dbSScott Long DBG_COUNTER_INC(tx_encap); 340295246abbSSean Bruno MPASS(pi.ipi_new_pidx < txq->ift_size); 34034c7070dbSScott Long 34044c7070dbSScott Long ndesc = pi.ipi_new_pidx - pi.ipi_pidx; 34054c7070dbSScott Long if (pi.ipi_new_pidx < pi.ipi_pidx) { 340623ac9029SStephen Hurd ndesc += txq->ift_size; 34074c7070dbSScott Long txq->ift_gen = 1; 34084c7070dbSScott Long } 34091248952aSSean Bruno /* 34101248952aSSean Bruno * drivers can need as many as 34111248952aSSean Bruno * two sentinels 34121248952aSSean Bruno */ 34131248952aSSean Bruno MPASS(ndesc <= pi.ipi_nsegs + 2); 34144c7070dbSScott Long MPASS(pi.ipi_new_pidx != pidx); 34154c7070dbSScott Long MPASS(ndesc > 0); 34164c7070dbSScott Long txq->ift_in_use += ndesc; 341795246abbSSean Bruno 34184c7070dbSScott Long /* 34194c7070dbSScott Long * We update the last software descriptor again here because there may 34204c7070dbSScott Long * be a sentinel and/or there may be more mbufs than segments 34214c7070dbSScott Long */ 34224c7070dbSScott Long txq->ift_pidx = pi.ipi_new_pidx; 34234c7070dbSScott Long txq->ift_npending += pi.ipi_ndescs; 3424f7594707SAndrew Gallatin } else { 342523ac9029SStephen Hurd *m_headp = m_head = iflib_remove_mbuf(txq); 3426f7594707SAndrew Gallatin if (err == EFBIG) { 34274c7070dbSScott Long txq->ift_txd_encap_efbig++; 3428f7594707SAndrew Gallatin if (remap < 2) { 3429f7594707SAndrew Gallatin remap = 1; 34304c7070dbSScott Long goto defrag; 3431f7594707SAndrew Gallatin } 3432f7594707SAndrew Gallatin } 3433f7594707SAndrew Gallatin goto defrag_failed; 3434f7594707SAndrew Gallatin } 343564e6fc13SStephen Hurd /* 343664e6fc13SStephen Hurd * err can't possibly be non-zero here, so we don't neet to test it 343764e6fc13SStephen Hurd * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail). 343864e6fc13SStephen Hurd */ 34394c7070dbSScott Long return (err); 34404c7070dbSScott Long 34414c7070dbSScott Long defrag_failed: 34424c7070dbSScott Long txq->ift_mbuf_defrag_failed++; 34434c7070dbSScott Long txq->ift_map_failed++; 34444c7070dbSScott Long m_freem(*m_headp); 34454c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 34464c7070dbSScott Long *m_headp = NULL; 344764e6fc13SStephen Hurd DBG_COUNTER_INC(encap_txd_encap_fail); 34484c7070dbSScott Long return (ENOMEM); 34494c7070dbSScott Long } 34504c7070dbSScott Long 34514c7070dbSScott Long static void 34524c7070dbSScott Long iflib_tx_desc_free(iflib_txq_t txq, int n) 34534c7070dbSScott Long { 34544c7070dbSScott Long uint32_t qsize, cidx, mask, gen; 34554c7070dbSScott Long struct mbuf *m, **ifsd_m; 345695246abbSSean Bruno bool do_prefetch; 34574c7070dbSScott Long 34584c7070dbSScott Long cidx = txq->ift_cidx; 34594c7070dbSScott Long gen = txq->ift_gen; 346023ac9029SStephen Hurd qsize = txq->ift_size; 34614c7070dbSScott Long mask = qsize-1; 34624c7070dbSScott Long ifsd_m = txq->ift_sds.ifsd_m; 346395246abbSSean Bruno do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH); 34644c7070dbSScott Long 346594618825SMark Johnston while (n-- > 0) { 346695246abbSSean Bruno if (do_prefetch) { 34674c7070dbSScott Long prefetch(ifsd_m[(cidx + 3) & mask]); 34684c7070dbSScott Long prefetch(ifsd_m[(cidx + 4) & mask]); 346995246abbSSean Bruno } 34704c7070dbSScott Long if ((m = ifsd_m[cidx]) != NULL) { 3471fbec776dSAndrew Gallatin prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]); 34728a04b53dSKonstantin Belousov if (m->m_pkthdr.csum_flags & CSUM_TSO) { 3473bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_tso_buf_tag, 34748a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx], 34758a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3476bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_tso_buf_tag, 34778a04b53dSKonstantin Belousov txq->ift_sds.ifsd_tso_map[cidx]); 34788a04b53dSKonstantin Belousov } else { 3479bfce461eSMarius Strobl bus_dmamap_sync(txq->ift_buf_tag, 34808a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx], 34818a04b53dSKonstantin Belousov BUS_DMASYNC_POSTWRITE); 3482bfce461eSMarius Strobl bus_dmamap_unload(txq->ift_buf_tag, 34838a04b53dSKonstantin Belousov txq->ift_sds.ifsd_map[cidx]); 34848a04b53dSKonstantin Belousov } 34854c7070dbSScott Long /* XXX we don't support any drivers that batch packets yet */ 34864c7070dbSScott Long MPASS(m->m_nextpkt == NULL); 34875c5ca36cSSean Bruno m_freem(m); 34884c7070dbSScott Long ifsd_m[cidx] = NULL; 34894c7070dbSScott Long #if MEMORY_LOGGING 34904c7070dbSScott Long txq->ift_dequeued++; 34914c7070dbSScott Long #endif 34924c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 34934c7070dbSScott Long } 34944c7070dbSScott Long if (__predict_false(++cidx == qsize)) { 34954c7070dbSScott Long cidx = 0; 34964c7070dbSScott Long gen = 0; 34974c7070dbSScott Long } 34984c7070dbSScott Long } 34994c7070dbSScott Long txq->ift_cidx = cidx; 35004c7070dbSScott Long txq->ift_gen = gen; 35014c7070dbSScott Long } 35024c7070dbSScott Long 35034c7070dbSScott Long static __inline int 35044c7070dbSScott Long iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh) 35054c7070dbSScott Long { 35064c7070dbSScott Long int reclaim; 35074c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 35084c7070dbSScott Long 35094c7070dbSScott Long KASSERT(thresh >= 0, ("invalid threshold to reclaim")); 35104c7070dbSScott Long MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size); 35114c7070dbSScott Long 35124c7070dbSScott Long /* 35134c7070dbSScott Long * Need a rate-limiting check so that this isn't called every time 35144c7070dbSScott Long */ 35154c7070dbSScott Long iflib_tx_credits_update(ctx, txq); 35164c7070dbSScott Long reclaim = DESC_RECLAIMABLE(txq); 35174c7070dbSScott Long 35184c7070dbSScott Long if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) { 35194c7070dbSScott Long #ifdef INVARIANTS 35204c7070dbSScott Long if (iflib_verbose_debug) { 35214c7070dbSScott Long printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__, 35224c7070dbSScott Long txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments, 35234c7070dbSScott Long reclaim, thresh); 35244c7070dbSScott Long 35254c7070dbSScott Long } 35264c7070dbSScott Long #endif 35274c7070dbSScott Long return (0); 35284c7070dbSScott Long } 35294c7070dbSScott Long iflib_tx_desc_free(txq, reclaim); 35304c7070dbSScott Long txq->ift_cleaned += reclaim; 35314c7070dbSScott Long txq->ift_in_use -= reclaim; 35324c7070dbSScott Long 35334c7070dbSScott Long return (reclaim); 35344c7070dbSScott Long } 35354c7070dbSScott Long 35364c7070dbSScott Long static struct mbuf ** 353795246abbSSean Bruno _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining) 35384c7070dbSScott Long { 353995246abbSSean Bruno int next, size; 354095246abbSSean Bruno struct mbuf **items; 35414c7070dbSScott Long 354295246abbSSean Bruno size = r->size; 354395246abbSSean Bruno next = (cidx + CACHE_PTR_INCREMENT) & (size-1); 354495246abbSSean Bruno items = __DEVOLATILE(struct mbuf **, &r->items[0]); 354595246abbSSean Bruno 354695246abbSSean Bruno prefetch(items[(cidx + offset) & (size-1)]); 354795246abbSSean Bruno if (remaining > 1) { 35483429c02fSStephen Hurd prefetch2cachelines(&items[next]); 35493429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]); 35503429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]); 35513429c02fSStephen Hurd prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]); 355295246abbSSean Bruno } 355395246abbSSean Bruno return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)])); 35544c7070dbSScott Long } 35554c7070dbSScott Long 35564c7070dbSScott Long static void 35574c7070dbSScott Long iflib_txq_check_drain(iflib_txq_t txq, int budget) 35584c7070dbSScott Long { 35594c7070dbSScott Long 356095246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, budget); 35614c7070dbSScott Long } 35624c7070dbSScott Long 35634c7070dbSScott Long static uint32_t 35644c7070dbSScott Long iflib_txq_can_drain(struct ifmp_ring *r) 35654c7070dbSScott Long { 35664c7070dbSScott Long iflib_txq_t txq = r->cookie; 35674c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 35684c7070dbSScott Long 356995dcf343SMarius Strobl if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2) 357095dcf343SMarius Strobl return (1); 35718a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 35728a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 357395dcf343SMarius Strobl return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, 357495dcf343SMarius Strobl false)); 35754c7070dbSScott Long } 35764c7070dbSScott Long 35774c7070dbSScott Long static uint32_t 35784c7070dbSScott Long iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 35794c7070dbSScott Long { 35804c7070dbSScott Long iflib_txq_t txq = r->cookie; 35814c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 35821722eeacSMarius Strobl if_t ifp = ctx->ifc_ifp; 35834c7070dbSScott Long struct mbuf **mp, *m; 358495246abbSSean Bruno int i, count, consumed, pkt_sent, bytes_sent, mcast_sent, avail; 358595246abbSSean Bruno int reclaimed, err, in_use_prev, desc_used; 358695246abbSSean Bruno bool do_prefetch, ring, rang; 35874c7070dbSScott Long 35884c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) || 35894c7070dbSScott Long !LINK_ACTIVE(ctx))) { 35904c7070dbSScott Long DBG_COUNTER_INC(txq_drain_notready); 35914c7070dbSScott Long return (0); 35924c7070dbSScott Long } 359395246abbSSean Bruno reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 359495246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, reclaimed, txq->ift_in_use); 35954c7070dbSScott Long avail = IDXDIFF(pidx, cidx, r->size); 35964c7070dbSScott Long if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) { 35974c7070dbSScott Long DBG_COUNTER_INC(txq_drain_flushing); 35984c7070dbSScott Long for (i = 0; i < avail; i++) { 3599bc0e855bSStephen Hurd if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq)) 360023ac9029SStephen Hurd m_free(r->items[(cidx + i) & (r->size-1)]); 36014c7070dbSScott Long r->items[(cidx + i) & (r->size-1)] = NULL; 36024c7070dbSScott Long } 36034c7070dbSScott Long return (avail); 36044c7070dbSScott Long } 360595246abbSSean Bruno 36064c7070dbSScott Long if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) { 36074c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 36084c7070dbSScott Long CALLOUT_LOCK(txq); 36094c7070dbSScott Long callout_stop(&txq->ift_timer); 36104c7070dbSScott Long CALLOUT_UNLOCK(txq); 36114c7070dbSScott Long DBG_COUNTER_INC(txq_drain_oactive); 36124c7070dbSScott Long return (0); 36134c7070dbSScott Long } 361495246abbSSean Bruno if (reclaimed) 361595246abbSSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 36164c7070dbSScott Long consumed = mcast_sent = bytes_sent = pkt_sent = 0; 36174c7070dbSScott Long count = MIN(avail, TX_BATCH_SIZE); 3618da69b8f9SSean Bruno #ifdef INVARIANTS 3619da69b8f9SSean Bruno if (iflib_verbose_debug) 3620da69b8f9SSean Bruno printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__, 3621da69b8f9SSean Bruno avail, ctx->ifc_flags, TXQ_AVAIL(txq)); 3622da69b8f9SSean Bruno #endif 362395246abbSSean Bruno do_prefetch = (ctx->ifc_flags & IFC_PREFETCH); 362495246abbSSean Bruno avail = TXQ_AVAIL(txq); 36251ae4848cSMatt Macy err = 0; 362695246abbSSean Bruno for (desc_used = i = 0; i < count && avail > MAX_TX_DESC(ctx) + 2; i++) { 36271ae4848cSMatt Macy int rem = do_prefetch ? count - i : 0; 36284c7070dbSScott Long 362995246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, rem); 3630da69b8f9SSean Bruno MPASS(mp != NULL && *mp != NULL); 363195246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) { 363295246abbSSean Bruno consumed++; 363395246abbSSean Bruno reclaimed++; 363495246abbSSean Bruno continue; 363595246abbSSean Bruno } 36364c7070dbSScott Long in_use_prev = txq->ift_in_use; 363795246abbSSean Bruno err = iflib_encap(txq, mp); 363895246abbSSean Bruno if (__predict_false(err)) { 3639da69b8f9SSean Bruno /* no room - bail out */ 364095246abbSSean Bruno if (err == ENOBUFS) 36414c7070dbSScott Long break; 36424c7070dbSScott Long consumed++; 3643da69b8f9SSean Bruno /* we can't send this packet - skip it */ 36444c7070dbSScott Long continue; 3645da69b8f9SSean Bruno } 364695246abbSSean Bruno consumed++; 36474c7070dbSScott Long pkt_sent++; 36484c7070dbSScott Long m = *mp; 36494c7070dbSScott Long DBG_COUNTER_INC(tx_sent); 36504c7070dbSScott Long bytes_sent += m->m_pkthdr.len; 365195246abbSSean Bruno mcast_sent += !!(m->m_flags & M_MCAST); 365295246abbSSean Bruno avail = TXQ_AVAIL(txq); 36534c7070dbSScott Long 36544c7070dbSScott Long txq->ift_db_pending += (txq->ift_in_use - in_use_prev); 36554c7070dbSScott Long desc_used += (txq->ift_in_use - in_use_prev); 36564c7070dbSScott Long ETHER_BPF_MTAP(ifp, m); 365795246abbSSean Bruno if (__predict_false(!(ifp->if_drv_flags & IFF_DRV_RUNNING))) 36584c7070dbSScott Long break; 365995246abbSSean Bruno rang = iflib_txd_db_check(ctx, txq, false, in_use_prev); 36604c7070dbSScott Long } 36614c7070dbSScott Long 366295246abbSSean Bruno /* deliberate use of bitwise or to avoid gratuitous short-circuit */ 366395246abbSSean Bruno ring = rang ? false : (iflib_min_tx_latency | err) || (TXQ_AVAIL(txq) < MAX_TX_DESC(ctx)); 366495246abbSSean Bruno iflib_txd_db_check(ctx, txq, ring, txq->ift_in_use); 36654c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent); 36664c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent); 36674c7070dbSScott Long if (mcast_sent) 36684c7070dbSScott Long if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent); 3669da69b8f9SSean Bruno #ifdef INVARIANTS 3670da69b8f9SSean Bruno if (iflib_verbose_debug) 3671da69b8f9SSean Bruno printf("consumed=%d\n", consumed); 3672da69b8f9SSean Bruno #endif 36734c7070dbSScott Long return (consumed); 36744c7070dbSScott Long } 36754c7070dbSScott Long 3676da69b8f9SSean Bruno static uint32_t 3677da69b8f9SSean Bruno iflib_txq_drain_always(struct ifmp_ring *r) 3678da69b8f9SSean Bruno { 3679da69b8f9SSean Bruno return (1); 3680da69b8f9SSean Bruno } 3681da69b8f9SSean Bruno 3682da69b8f9SSean Bruno static uint32_t 3683da69b8f9SSean Bruno iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx) 3684da69b8f9SSean Bruno { 3685da69b8f9SSean Bruno int i, avail; 3686da69b8f9SSean Bruno struct mbuf **mp; 3687da69b8f9SSean Bruno iflib_txq_t txq; 3688da69b8f9SSean Bruno 3689da69b8f9SSean Bruno txq = r->cookie; 3690da69b8f9SSean Bruno 3691da69b8f9SSean Bruno txq->ift_qstatus = IFLIB_QUEUE_IDLE; 3692da69b8f9SSean Bruno CALLOUT_LOCK(txq); 3693da69b8f9SSean Bruno callout_stop(&txq->ift_timer); 3694da69b8f9SSean Bruno CALLOUT_UNLOCK(txq); 3695da69b8f9SSean Bruno 3696da69b8f9SSean Bruno avail = IDXDIFF(pidx, cidx, r->size); 3697da69b8f9SSean Bruno for (i = 0; i < avail; i++) { 369895246abbSSean Bruno mp = _ring_peek_one(r, cidx, i, avail - i); 369995246abbSSean Bruno if (__predict_false(*mp == (struct mbuf *)txq)) 370095246abbSSean Bruno continue; 3701da69b8f9SSean Bruno m_freem(*mp); 370264e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 3703da69b8f9SSean Bruno } 3704da69b8f9SSean Bruno MPASS(ifmp_ring_is_stalled(r) == 0); 3705da69b8f9SSean Bruno return (avail); 3706da69b8f9SSean Bruno } 3707da69b8f9SSean Bruno 3708da69b8f9SSean Bruno static void 3709da69b8f9SSean Bruno iflib_ifmp_purge(iflib_txq_t txq) 3710da69b8f9SSean Bruno { 3711da69b8f9SSean Bruno struct ifmp_ring *r; 3712da69b8f9SSean Bruno 371395246abbSSean Bruno r = txq->ift_br; 3714da69b8f9SSean Bruno r->drain = iflib_txq_drain_free; 3715da69b8f9SSean Bruno r->can_drain = iflib_txq_drain_always; 3716da69b8f9SSean Bruno 3717da69b8f9SSean Bruno ifmp_ring_check_drainage(r, r->size); 3718da69b8f9SSean Bruno 3719da69b8f9SSean Bruno r->drain = iflib_txq_drain; 3720da69b8f9SSean Bruno r->can_drain = iflib_txq_can_drain; 3721da69b8f9SSean Bruno } 3722da69b8f9SSean Bruno 37234c7070dbSScott Long static void 372423ac9029SStephen Hurd _task_fn_tx(void *context) 37254c7070dbSScott Long { 37264c7070dbSScott Long iflib_txq_t txq = context; 37274c7070dbSScott Long if_ctx_t ctx = txq->ift_ctx; 3728a6611c93SMarius Strobl #if defined(ALTQ) || defined(DEV_NETMAP) 3729a6611c93SMarius Strobl if_t ifp = ctx->ifc_ifp; 3730a6611c93SMarius Strobl #endif 3731fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 37324c7070dbSScott Long 37331248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 37341248952aSSean Bruno txq->ift_cpu_exec_count[curcpu]++; 37351248952aSSean Bruno #endif 37364c7070dbSScott Long if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)) 37374c7070dbSScott Long return; 373895dcf343SMarius Strobl #ifdef DEV_NETMAP 3739a6611c93SMarius Strobl if (if_getcapenable(ifp) & IFCAP_NETMAP) { 37408a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 37418a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 374295246abbSSean Bruno if (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, false)) 3743a6611c93SMarius Strobl netmap_tx_irq(ifp, txq->ift_id); 37443d10e9edSMarius Strobl if (ctx->ifc_flags & IFC_LEGACY) 37453d10e9edSMarius Strobl IFDI_INTR_ENABLE(ctx); 37463d10e9edSMarius Strobl else 374795246abbSSean Bruno IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 374895246abbSSean Bruno return; 374995246abbSSean Bruno } 375095dcf343SMarius Strobl #endif 3751b8ca4756SPatrick Kelsey #ifdef ALTQ 3752b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) 3753b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 3754b8ca4756SPatrick Kelsey #endif 375595246abbSSean Bruno if (txq->ift_db_pending) 3756fe51d4cdSStephen Hurd ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate); 3757fe51d4cdSStephen Hurd else if (!abdicate) 3758fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 3759fe51d4cdSStephen Hurd /* 3760fe51d4cdSStephen Hurd * When abdicating, we always need to check drainage, not just when we don't enqueue 3761fe51d4cdSStephen Hurd */ 3762fe51d4cdSStephen Hurd if (abdicate) 3763fe51d4cdSStephen Hurd ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 376495246abbSSean Bruno if (ctx->ifc_flags & IFC_LEGACY) 376595246abbSSean Bruno IFDI_INTR_ENABLE(ctx); 37663d10e9edSMarius Strobl else 37671ae4848cSMatt Macy IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id); 37684c7070dbSScott Long } 37694c7070dbSScott Long 37704c7070dbSScott Long static void 377123ac9029SStephen Hurd _task_fn_rx(void *context) 37724c7070dbSScott Long { 37734c7070dbSScott Long iflib_rxq_t rxq = context; 37744c7070dbSScott Long if_ctx_t ctx = rxq->ifr_ctx; 37754c7070dbSScott Long bool more; 3776f4d2154eSStephen Hurd uint16_t budget; 37774c7070dbSScott Long 37781248952aSSean Bruno #ifdef IFLIB_DIAGNOSTICS 37791248952aSSean Bruno rxq->ifr_cpu_exec_count[curcpu]++; 37801248952aSSean Bruno #endif 37814c7070dbSScott Long DBG_COUNTER_INC(task_fn_rxs); 37824c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 37834c7070dbSScott Long return; 3784d0d0ad0aSStephen Hurd more = true; 3785d0d0ad0aSStephen Hurd #ifdef DEV_NETMAP 3786d0d0ad0aSStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) { 3787d0d0ad0aSStephen Hurd u_int work = 0; 3788d0d0ad0aSStephen Hurd if (netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work)) { 3789d0d0ad0aSStephen Hurd more = false; 3790d0d0ad0aSStephen Hurd } 3791d0d0ad0aSStephen Hurd } 3792d0d0ad0aSStephen Hurd #endif 3793f4d2154eSStephen Hurd budget = ctx->ifc_sysctl_rx_budget; 3794f4d2154eSStephen Hurd if (budget == 0) 3795f4d2154eSStephen Hurd budget = 16; /* XXX */ 3796f4d2154eSStephen Hurd if (more == false || (more = iflib_rxeof(rxq, budget)) == false) { 37974c7070dbSScott Long if (ctx->ifc_flags & IFC_LEGACY) 37984c7070dbSScott Long IFDI_INTR_ENABLE(ctx); 37993d10e9edSMarius Strobl else 38001ae4848cSMatt Macy IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id); 38011ae4848cSMatt Macy DBG_COUNTER_INC(rx_intr_enables); 38024c7070dbSScott Long } 38034c7070dbSScott Long if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))) 38044c7070dbSScott Long return; 38054c7070dbSScott Long if (more) 38064c7070dbSScott Long GROUPTASK_ENQUEUE(&rxq->ifr_task); 38074c7070dbSScott Long } 38084c7070dbSScott Long 38094c7070dbSScott Long static void 381023ac9029SStephen Hurd _task_fn_admin(void *context) 38114c7070dbSScott Long { 38124c7070dbSScott Long if_ctx_t ctx = context; 38134c7070dbSScott Long if_softc_ctx_t sctx = &ctx->ifc_softc_ctx; 38144c7070dbSScott Long iflib_txq_t txq; 3815ab2e3f79SStephen Hurd int i; 381677c1fcecSEric Joyner bool oactive, running, do_reset, do_watchdog, in_detach; 3817dd7fbcf1SStephen Hurd uint32_t reset_on = hz / 2; 3818ab2e3f79SStephen Hurd 38197b610b60SSean Bruno STATE_LOCK(ctx); 38207b610b60SSean Bruno running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING); 38217b610b60SSean Bruno oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE); 38227b610b60SSean Bruno do_reset = (ctx->ifc_flags & IFC_DO_RESET); 38237b610b60SSean Bruno do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG); 382477c1fcecSEric Joyner in_detach = (ctx->ifc_flags & IFC_IN_DETACH); 38257b610b60SSean Bruno ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG); 38267b610b60SSean Bruno STATE_UNLOCK(ctx); 38277b610b60SSean Bruno 382877c1fcecSEric Joyner if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 382977c1fcecSEric Joyner return; 383077c1fcecSEric Joyner if (in_detach) 3831ab2e3f79SStephen Hurd return; 38324c7070dbSScott Long 38334c7070dbSScott Long CTX_LOCK(ctx); 38344c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 38354c7070dbSScott Long CALLOUT_LOCK(txq); 38364c7070dbSScott Long callout_stop(&txq->ift_timer); 38374c7070dbSScott Long CALLOUT_UNLOCK(txq); 38384c7070dbSScott Long } 38397b610b60SSean Bruno if (do_watchdog) { 38407b610b60SSean Bruno ctx->ifc_watchdog_events++; 38417b610b60SSean Bruno IFDI_WATCHDOG_RESET(ctx); 38427b610b60SSean Bruno } 3843d300df01SStephen Hurd IFDI_UPDATE_ADMIN_STATUS(ctx); 3844dd7fbcf1SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) { 3845dd7fbcf1SStephen Hurd #ifdef DEV_NETMAP 3846dd7fbcf1SStephen Hurd reset_on = hz / 2; 3847dd7fbcf1SStephen Hurd if (if_getcapenable(ctx->ifc_ifp) & IFCAP_NETMAP) 384895dcf343SMarius Strobl iflib_netmap_timer_adjust(ctx, txq, &reset_on); 3849dd7fbcf1SStephen Hurd #endif 3850dd7fbcf1SStephen Hurd callout_reset_on(&txq->ift_timer, reset_on, iflib_timer, txq, txq->ift_timer.c_cpu); 3851dd7fbcf1SStephen Hurd } 3852ab2e3f79SStephen Hurd IFDI_LINK_INTR_ENABLE(ctx); 38537b610b60SSean Bruno if (do_reset) 3854ab2e3f79SStephen Hurd iflib_if_init_locked(ctx); 38554c7070dbSScott Long CTX_UNLOCK(ctx); 38564c7070dbSScott Long 3857ab2e3f79SStephen Hurd if (LINK_ACTIVE(ctx) == 0) 38584c7070dbSScott Long return; 38594c7070dbSScott Long for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) 38604c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 38614c7070dbSScott Long } 38624c7070dbSScott Long 38634c7070dbSScott Long 38644c7070dbSScott Long static void 386523ac9029SStephen Hurd _task_fn_iov(void *context) 38664c7070dbSScott Long { 38674c7070dbSScott Long if_ctx_t ctx = context; 38684c7070dbSScott Long 386977c1fcecSEric Joyner if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) && 387077c1fcecSEric Joyner !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN)) 38714c7070dbSScott Long return; 38724c7070dbSScott Long 38734c7070dbSScott Long CTX_LOCK(ctx); 38744c7070dbSScott Long IFDI_VFLR_HANDLE(ctx); 38754c7070dbSScott Long CTX_UNLOCK(ctx); 38764c7070dbSScott Long } 38774c7070dbSScott Long 38784c7070dbSScott Long static int 38794c7070dbSScott Long iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS) 38804c7070dbSScott Long { 38814c7070dbSScott Long int err; 38824c7070dbSScott Long if_int_delay_info_t info; 38834c7070dbSScott Long if_ctx_t ctx; 38844c7070dbSScott Long 38854c7070dbSScott Long info = (if_int_delay_info_t)arg1; 38864c7070dbSScott Long ctx = info->iidi_ctx; 38874c7070dbSScott Long info->iidi_req = req; 38884c7070dbSScott Long info->iidi_oidp = oidp; 38894c7070dbSScott Long CTX_LOCK(ctx); 38904c7070dbSScott Long err = IFDI_SYSCTL_INT_DELAY(ctx, info); 38914c7070dbSScott Long CTX_UNLOCK(ctx); 38924c7070dbSScott Long return (err); 38934c7070dbSScott Long } 38944c7070dbSScott Long 38954c7070dbSScott Long /********************************************************************* 38964c7070dbSScott Long * 38974c7070dbSScott Long * IFNET FUNCTIONS 38984c7070dbSScott Long * 38994c7070dbSScott Long **********************************************************************/ 39004c7070dbSScott Long 39014c7070dbSScott Long static void 39024c7070dbSScott Long iflib_if_init_locked(if_ctx_t ctx) 39034c7070dbSScott Long { 39044c7070dbSScott Long iflib_stop(ctx); 39054c7070dbSScott Long iflib_init_locked(ctx); 39064c7070dbSScott Long } 39074c7070dbSScott Long 39084c7070dbSScott Long 39094c7070dbSScott Long static void 39104c7070dbSScott Long iflib_if_init(void *arg) 39114c7070dbSScott Long { 39124c7070dbSScott Long if_ctx_t ctx = arg; 39134c7070dbSScott Long 39144c7070dbSScott Long CTX_LOCK(ctx); 39154c7070dbSScott Long iflib_if_init_locked(ctx); 39164c7070dbSScott Long CTX_UNLOCK(ctx); 39174c7070dbSScott Long } 39184c7070dbSScott Long 39194c7070dbSScott Long static int 39204c7070dbSScott Long iflib_if_transmit(if_t ifp, struct mbuf *m) 39214c7070dbSScott Long { 39224c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 39234c7070dbSScott Long 39244c7070dbSScott Long iflib_txq_t txq; 392523ac9029SStephen Hurd int err, qidx; 3926fe51d4cdSStephen Hurd int abdicate = ctx->ifc_sysctl_tx_abdicate; 39274c7070dbSScott Long 39284c7070dbSScott Long if (__predict_false((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) { 39294c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 39304c7070dbSScott Long m_freem(m); 3931225eae1bSEric Joyner return (ENETDOWN); 39324c7070dbSScott Long } 39334c7070dbSScott Long 393423ac9029SStephen Hurd MPASS(m->m_nextpkt == NULL); 3935b8ca4756SPatrick Kelsey /* ALTQ-enabled interfaces always use queue 0. */ 39364c7070dbSScott Long qidx = 0; 3937b8ca4756SPatrick Kelsey if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !ALTQ_IS_ENABLED(&ifp->if_snd)) 39384c7070dbSScott Long qidx = QIDX(ctx, m); 39394c7070dbSScott Long /* 39404c7070dbSScott Long * XXX calculate buf_ring based on flowid (divvy up bits?) 39414c7070dbSScott Long */ 39424c7070dbSScott Long txq = &ctx->ifc_txqs[qidx]; 39434c7070dbSScott Long 39444c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 39454c7070dbSScott Long if (txq->ift_closed) { 39464c7070dbSScott Long while (m != NULL) { 39474c7070dbSScott Long next = m->m_nextpkt; 39484c7070dbSScott Long m->m_nextpkt = NULL; 39494c7070dbSScott Long m_freem(m); 395064e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 39514c7070dbSScott Long m = next; 39524c7070dbSScott Long } 39534c7070dbSScott Long return (ENOBUFS); 39544c7070dbSScott Long } 39554c7070dbSScott Long #endif 395623ac9029SStephen Hurd #ifdef notyet 39574c7070dbSScott Long qidx = count = 0; 39584c7070dbSScott Long mp = marr; 39594c7070dbSScott Long next = m; 39604c7070dbSScott Long do { 39614c7070dbSScott Long count++; 39624c7070dbSScott Long next = next->m_nextpkt; 39634c7070dbSScott Long } while (next != NULL); 39644c7070dbSScott Long 396516fb86abSConrad Meyer if (count > nitems(marr)) 39664c7070dbSScott Long if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) { 39674c7070dbSScott Long /* XXX check nextpkt */ 39684c7070dbSScott Long m_freem(m); 39694c7070dbSScott Long /* XXX simplify for now */ 39704c7070dbSScott Long DBG_COUNTER_INC(tx_frees); 39714c7070dbSScott Long return (ENOBUFS); 39724c7070dbSScott Long } 39734c7070dbSScott Long for (next = m, i = 0; next != NULL; i++) { 39744c7070dbSScott Long mp[i] = next; 39754c7070dbSScott Long next = next->m_nextpkt; 39764c7070dbSScott Long mp[i]->m_nextpkt = NULL; 39774c7070dbSScott Long } 397823ac9029SStephen Hurd #endif 39794c7070dbSScott Long DBG_COUNTER_INC(tx_seen); 3980fe51d4cdSStephen Hurd err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate); 39814c7070dbSScott Long 3982fe51d4cdSStephen Hurd if (abdicate) 3983ab2e3f79SStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 39841225d9daSStephen Hurd if (err) { 3985fe51d4cdSStephen Hurd if (!abdicate) 3986fe51d4cdSStephen Hurd GROUPTASK_ENQUEUE(&txq->ift_task); 39874c7070dbSScott Long /* support forthcoming later */ 39884c7070dbSScott Long #ifdef DRIVER_BACKPRESSURE 39894c7070dbSScott Long txq->ift_closed = TRUE; 39904c7070dbSScott Long #endif 399195246abbSSean Bruno ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE); 399223ac9029SStephen Hurd m_freem(m); 399364e6fc13SStephen Hurd DBG_COUNTER_INC(tx_frees); 39944c7070dbSScott Long } 39954c7070dbSScott Long 39964c7070dbSScott Long return (err); 39974c7070dbSScott Long } 39984c7070dbSScott Long 3999b8ca4756SPatrick Kelsey #ifdef ALTQ 4000b8ca4756SPatrick Kelsey /* 4001b8ca4756SPatrick Kelsey * The overall approach to integrating iflib with ALTQ is to continue to use 4002b8ca4756SPatrick Kelsey * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware 4003b8ca4756SPatrick Kelsey * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring 4004b8ca4756SPatrick Kelsey * is redundant/unnecessary, but doing so minimizes the amount of 4005b8ca4756SPatrick Kelsey * ALTQ-specific code required in iflib. It is assumed that the overhead of 4006b8ca4756SPatrick Kelsey * redundantly queueing to an intermediate mp_ring is swamped by the 4007b8ca4756SPatrick Kelsey * performance limitations inherent in using ALTQ. 4008b8ca4756SPatrick Kelsey * 4009b8ca4756SPatrick Kelsey * When ALTQ support is compiled in, all iflib drivers will use a transmit 4010b8ca4756SPatrick Kelsey * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the 4011b8ca4756SPatrick Kelsey * given interface. If ALTQ is enabled for an interface, then all 4012b8ca4756SPatrick Kelsey * transmitted packets for that interface will be submitted to the ALTQ 4013b8ca4756SPatrick Kelsey * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit() 4014b8ca4756SPatrick Kelsey * implementation because it uses IFQ_HANDOFF(), which will duplicatively 4015b8ca4756SPatrick Kelsey * update stats that the iflib machinery handles, and which is sensitve to 4016b8ca4756SPatrick Kelsey * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start() 4017b8ca4756SPatrick Kelsey * will be installed as the start routine for use by ALTQ facilities that 4018b8ca4756SPatrick Kelsey * need to trigger queue drains on a scheduled basis. 4019b8ca4756SPatrick Kelsey * 4020b8ca4756SPatrick Kelsey */ 4021b8ca4756SPatrick Kelsey static void 4022b8ca4756SPatrick Kelsey iflib_altq_if_start(if_t ifp) 4023b8ca4756SPatrick Kelsey { 4024b8ca4756SPatrick Kelsey struct ifaltq *ifq = &ifp->if_snd; 4025b8ca4756SPatrick Kelsey struct mbuf *m; 4026b8ca4756SPatrick Kelsey 4027b8ca4756SPatrick Kelsey IFQ_LOCK(ifq); 4028b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 4029b8ca4756SPatrick Kelsey while (m != NULL) { 4030b8ca4756SPatrick Kelsey iflib_if_transmit(ifp, m); 4031b8ca4756SPatrick Kelsey IFQ_DEQUEUE_NOLOCK(ifq, m); 4032b8ca4756SPatrick Kelsey } 4033b8ca4756SPatrick Kelsey IFQ_UNLOCK(ifq); 4034b8ca4756SPatrick Kelsey } 4035b8ca4756SPatrick Kelsey 4036b8ca4756SPatrick Kelsey static int 4037b8ca4756SPatrick Kelsey iflib_altq_if_transmit(if_t ifp, struct mbuf *m) 4038b8ca4756SPatrick Kelsey { 4039b8ca4756SPatrick Kelsey int err; 4040b8ca4756SPatrick Kelsey 4041b8ca4756SPatrick Kelsey if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 4042b8ca4756SPatrick Kelsey IFQ_ENQUEUE(&ifp->if_snd, m, err); 4043b8ca4756SPatrick Kelsey if (err == 0) 4044b8ca4756SPatrick Kelsey iflib_altq_if_start(ifp); 4045b8ca4756SPatrick Kelsey } else 4046b8ca4756SPatrick Kelsey err = iflib_if_transmit(ifp, m); 4047b8ca4756SPatrick Kelsey 4048b8ca4756SPatrick Kelsey return (err); 4049b8ca4756SPatrick Kelsey } 4050b8ca4756SPatrick Kelsey #endif /* ALTQ */ 4051b8ca4756SPatrick Kelsey 40524c7070dbSScott Long static void 40534c7070dbSScott Long iflib_if_qflush(if_t ifp) 40544c7070dbSScott Long { 40554c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 40564c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 40574c7070dbSScott Long int i; 40584c7070dbSScott Long 40597b610b60SSean Bruno STATE_LOCK(ctx); 40604c7070dbSScott Long ctx->ifc_flags |= IFC_QFLUSH; 40617b610b60SSean Bruno STATE_UNLOCK(ctx); 40624c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 406395246abbSSean Bruno while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br))) 40644c7070dbSScott Long iflib_txq_check_drain(txq, 0); 40657b610b60SSean Bruno STATE_LOCK(ctx); 40664c7070dbSScott Long ctx->ifc_flags &= ~IFC_QFLUSH; 40677b610b60SSean Bruno STATE_UNLOCK(ctx); 40684c7070dbSScott Long 4069b8ca4756SPatrick Kelsey /* 4070b8ca4756SPatrick Kelsey * When ALTQ is enabled, this will also take care of purging the 4071b8ca4756SPatrick Kelsey * ALTQ queue(s). 4072b8ca4756SPatrick Kelsey */ 40734c7070dbSScott Long if_qflush(ifp); 40744c7070dbSScott Long } 40754c7070dbSScott Long 40764c7070dbSScott Long 40770c919c23SStephen Hurd #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \ 40780c919c23SStephen Hurd IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \ 40790c919c23SStephen Hurd IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \ 40800c919c23SStephen Hurd IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM) 40814c7070dbSScott Long 40824c7070dbSScott Long static int 40834c7070dbSScott Long iflib_if_ioctl(if_t ifp, u_long command, caddr_t data) 40844c7070dbSScott Long { 40854c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 40864c7070dbSScott Long struct ifreq *ifr = (struct ifreq *)data; 40874c7070dbSScott Long #if defined(INET) || defined(INET6) 40884c7070dbSScott Long struct ifaddr *ifa = (struct ifaddr *)data; 40894c7070dbSScott Long #endif 40901722eeacSMarius Strobl bool avoid_reset = false; 40914c7070dbSScott Long int err = 0, reinit = 0, bits; 40924c7070dbSScott Long 40934c7070dbSScott Long switch (command) { 40944c7070dbSScott Long case SIOCSIFADDR: 40954c7070dbSScott Long #ifdef INET 40964c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET) 40971722eeacSMarius Strobl avoid_reset = true; 40984c7070dbSScott Long #endif 40994c7070dbSScott Long #ifdef INET6 41004c7070dbSScott Long if (ifa->ifa_addr->sa_family == AF_INET6) 41011722eeacSMarius Strobl avoid_reset = true; 41024c7070dbSScott Long #endif 41034c7070dbSScott Long /* 41044c7070dbSScott Long ** Calling init results in link renegotiation, 41054c7070dbSScott Long ** so we avoid doing it when possible. 41064c7070dbSScott Long */ 41074c7070dbSScott Long if (avoid_reset) { 41084c7070dbSScott Long if_setflagbits(ifp, IFF_UP,0); 41094c7070dbSScott Long if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 41104c7070dbSScott Long reinit = 1; 41114c7070dbSScott Long #ifdef INET 41124c7070dbSScott Long if (!(if_getflags(ifp) & IFF_NOARP)) 41134c7070dbSScott Long arp_ifinit(ifp, ifa); 41144c7070dbSScott Long #endif 41154c7070dbSScott Long } else 41164c7070dbSScott Long err = ether_ioctl(ifp, command, data); 41174c7070dbSScott Long break; 41184c7070dbSScott Long case SIOCSIFMTU: 41194c7070dbSScott Long CTX_LOCK(ctx); 41204c7070dbSScott Long if (ifr->ifr_mtu == if_getmtu(ifp)) { 41214c7070dbSScott Long CTX_UNLOCK(ctx); 41224c7070dbSScott Long break; 41234c7070dbSScott Long } 41244c7070dbSScott Long bits = if_getdrvflags(ifp); 41254c7070dbSScott Long /* stop the driver and free any clusters before proceeding */ 41264c7070dbSScott Long iflib_stop(ctx); 41274c7070dbSScott Long 41284c7070dbSScott Long if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) { 41297b610b60SSean Bruno STATE_LOCK(ctx); 41304c7070dbSScott Long if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size) 41314c7070dbSScott Long ctx->ifc_flags |= IFC_MULTISEG; 41324c7070dbSScott Long else 41334c7070dbSScott Long ctx->ifc_flags &= ~IFC_MULTISEG; 41347b610b60SSean Bruno STATE_UNLOCK(ctx); 41354c7070dbSScott Long err = if_setmtu(ifp, ifr->ifr_mtu); 41364c7070dbSScott Long } 41374c7070dbSScott Long iflib_init_locked(ctx); 41387b610b60SSean Bruno STATE_LOCK(ctx); 41394c7070dbSScott Long if_setdrvflags(ifp, bits); 41407b610b60SSean Bruno STATE_UNLOCK(ctx); 41414c7070dbSScott Long CTX_UNLOCK(ctx); 41424c7070dbSScott Long break; 41434c7070dbSScott Long case SIOCSIFFLAGS: 4144ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4145ab2e3f79SStephen Hurd if (if_getflags(ifp) & IFF_UP) { 4146ab2e3f79SStephen Hurd if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4147ab2e3f79SStephen Hurd if ((if_getflags(ifp) ^ ctx->ifc_if_flags) & 4148ab2e3f79SStephen Hurd (IFF_PROMISC | IFF_ALLMULTI)) { 4149ab2e3f79SStephen Hurd err = IFDI_PROMISC_SET(ctx, if_getflags(ifp)); 4150ab2e3f79SStephen Hurd } 4151ab2e3f79SStephen Hurd } else 4152ab2e3f79SStephen Hurd reinit = 1; 4153ab2e3f79SStephen Hurd } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4154ab2e3f79SStephen Hurd iflib_stop(ctx); 4155ab2e3f79SStephen Hurd } 4156ab2e3f79SStephen Hurd ctx->ifc_if_flags = if_getflags(ifp); 4157ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 41584c7070dbSScott Long break; 41594c7070dbSScott Long case SIOCADDMULTI: 41604c7070dbSScott Long case SIOCDELMULTI: 41614c7070dbSScott Long if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 4162ab2e3f79SStephen Hurd CTX_LOCK(ctx); 4163ab2e3f79SStephen Hurd IFDI_INTR_DISABLE(ctx); 4164ab2e3f79SStephen Hurd IFDI_MULTI_SET(ctx); 4165ab2e3f79SStephen Hurd IFDI_INTR_ENABLE(ctx); 4166ab2e3f79SStephen Hurd CTX_UNLOCK(ctx); 41674c7070dbSScott Long } 41684c7070dbSScott Long break; 41694c7070dbSScott Long case SIOCSIFMEDIA: 41704c7070dbSScott Long CTX_LOCK(ctx); 41714c7070dbSScott Long IFDI_MEDIA_SET(ctx); 41724c7070dbSScott Long CTX_UNLOCK(ctx); 41731722eeacSMarius Strobl /* FALLTHROUGH */ 41744c7070dbSScott Long case SIOCGIFMEDIA: 4175a027c8e9SStephen Hurd case SIOCGIFXMEDIA: 4176e2621d96SMatt Macy err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command); 41774c7070dbSScott Long break; 41784c7070dbSScott Long case SIOCGI2C: 41794c7070dbSScott Long { 41804c7070dbSScott Long struct ifi2creq i2c; 41814c7070dbSScott Long 4182541d96aaSBrooks Davis err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c)); 41834c7070dbSScott Long if (err != 0) 41844c7070dbSScott Long break; 41854c7070dbSScott Long if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) { 41864c7070dbSScott Long err = EINVAL; 41874c7070dbSScott Long break; 41884c7070dbSScott Long } 41894c7070dbSScott Long if (i2c.len > sizeof(i2c.data)) { 41904c7070dbSScott Long err = EINVAL; 41914c7070dbSScott Long break; 41924c7070dbSScott Long } 41934c7070dbSScott Long 41944c7070dbSScott Long if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0) 4195541d96aaSBrooks Davis err = copyout(&i2c, ifr_data_get_ptr(ifr), 4196541d96aaSBrooks Davis sizeof(i2c)); 41974c7070dbSScott Long break; 41984c7070dbSScott Long } 41994c7070dbSScott Long case SIOCSIFCAP: 42004c7070dbSScott Long { 42010c919c23SStephen Hurd int mask, setmask, oldmask; 42024c7070dbSScott Long 42030c919c23SStephen Hurd oldmask = if_getcapenable(ifp); 42040c919c23SStephen Hurd mask = ifr->ifr_reqcap ^ oldmask; 42050c919c23SStephen Hurd mask &= ctx->ifc_softc_ctx.isc_capabilities; 42064c7070dbSScott Long setmask = 0; 42074c7070dbSScott Long #ifdef TCP_OFFLOAD 42084c7070dbSScott Long setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6); 42094c7070dbSScott Long #endif 42104c7070dbSScott Long setmask |= (mask & IFCAP_FLAGS); 42110c919c23SStephen Hurd setmask |= (mask & IFCAP_WOL); 42124c7070dbSScott Long 42130c919c23SStephen Hurd /* 4214a42546dfSStephen Hurd * If any RX csum has changed, change all the ones that 4215a42546dfSStephen Hurd * are supported by the driver. 42160c919c23SStephen Hurd */ 4217a42546dfSStephen Hurd if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 4218a42546dfSStephen Hurd setmask |= ctx->ifc_softc_ctx.isc_capabilities & 4219a42546dfSStephen Hurd (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6); 4220a42546dfSStephen Hurd } 42210c919c23SStephen Hurd 42224c7070dbSScott Long /* 42234c7070dbSScott Long * want to ensure that traffic has stopped before we change any of the flags 42244c7070dbSScott Long */ 42254c7070dbSScott Long if (setmask) { 42264c7070dbSScott Long CTX_LOCK(ctx); 42274c7070dbSScott Long bits = if_getdrvflags(ifp); 42280c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 42294c7070dbSScott Long iflib_stop(ctx); 42307b610b60SSean Bruno STATE_LOCK(ctx); 42314c7070dbSScott Long if_togglecapenable(ifp, setmask); 42327b610b60SSean Bruno STATE_UNLOCK(ctx); 42330c919c23SStephen Hurd if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL) 42344c7070dbSScott Long iflib_init_locked(ctx); 42357b610b60SSean Bruno STATE_LOCK(ctx); 42364c7070dbSScott Long if_setdrvflags(ifp, bits); 42377b610b60SSean Bruno STATE_UNLOCK(ctx); 42384c7070dbSScott Long CTX_UNLOCK(ctx); 42394c7070dbSScott Long } 42400c919c23SStephen Hurd if_vlancap(ifp); 42414c7070dbSScott Long break; 42424c7070dbSScott Long } 42434c7070dbSScott Long case SIOCGPRIVATE_0: 42444c7070dbSScott Long case SIOCSDRVSPEC: 42454c7070dbSScott Long case SIOCGDRVSPEC: 42464c7070dbSScott Long CTX_LOCK(ctx); 42474c7070dbSScott Long err = IFDI_PRIV_IOCTL(ctx, command, data); 42484c7070dbSScott Long CTX_UNLOCK(ctx); 42494c7070dbSScott Long break; 42504c7070dbSScott Long default: 42514c7070dbSScott Long err = ether_ioctl(ifp, command, data); 42524c7070dbSScott Long break; 42534c7070dbSScott Long } 42544c7070dbSScott Long if (reinit) 42554c7070dbSScott Long iflib_if_init(ctx); 42564c7070dbSScott Long return (err); 42574c7070dbSScott Long } 42584c7070dbSScott Long 42594c7070dbSScott Long static uint64_t 42604c7070dbSScott Long iflib_if_get_counter(if_t ifp, ift_counter cnt) 42614c7070dbSScott Long { 42624c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 42634c7070dbSScott Long 42644c7070dbSScott Long return (IFDI_GET_COUNTER(ctx, cnt)); 42654c7070dbSScott Long } 42664c7070dbSScott Long 42674c7070dbSScott Long /********************************************************************* 42684c7070dbSScott Long * 42694c7070dbSScott Long * OTHER FUNCTIONS EXPORTED TO THE STACK 42704c7070dbSScott Long * 42714c7070dbSScott Long **********************************************************************/ 42724c7070dbSScott Long 42734c7070dbSScott Long static void 42744c7070dbSScott Long iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag) 42754c7070dbSScott Long { 42764c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 42774c7070dbSScott Long 42784c7070dbSScott Long if ((void *)ctx != arg) 42794c7070dbSScott Long return; 42804c7070dbSScott Long 42814c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 42824c7070dbSScott Long return; 42834c7070dbSScott Long 42844c7070dbSScott Long CTX_LOCK(ctx); 42854c7070dbSScott Long IFDI_VLAN_REGISTER(ctx, vtag); 42864c7070dbSScott Long /* Re-init to load the changes */ 42874c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) 428821e10b16SSean Bruno iflib_if_init_locked(ctx); 42894c7070dbSScott Long CTX_UNLOCK(ctx); 42904c7070dbSScott Long } 42914c7070dbSScott Long 42924c7070dbSScott Long static void 42934c7070dbSScott Long iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag) 42944c7070dbSScott Long { 42954c7070dbSScott Long if_ctx_t ctx = if_getsoftc(ifp); 42964c7070dbSScott Long 42974c7070dbSScott Long if ((void *)ctx != arg) 42984c7070dbSScott Long return; 42994c7070dbSScott Long 43004c7070dbSScott Long if ((vtag == 0) || (vtag > 4095)) 43014c7070dbSScott Long return; 43024c7070dbSScott Long 43034c7070dbSScott Long CTX_LOCK(ctx); 43044c7070dbSScott Long IFDI_VLAN_UNREGISTER(ctx, vtag); 43054c7070dbSScott Long /* Re-init to load the changes */ 43064c7070dbSScott Long if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER) 430721e10b16SSean Bruno iflib_if_init_locked(ctx); 43084c7070dbSScott Long CTX_UNLOCK(ctx); 43094c7070dbSScott Long } 43104c7070dbSScott Long 43114c7070dbSScott Long static void 43124c7070dbSScott Long iflib_led_func(void *arg, int onoff) 43134c7070dbSScott Long { 43144c7070dbSScott Long if_ctx_t ctx = arg; 43154c7070dbSScott Long 43164c7070dbSScott Long CTX_LOCK(ctx); 43174c7070dbSScott Long IFDI_LED_FUNC(ctx, onoff); 43184c7070dbSScott Long CTX_UNLOCK(ctx); 43194c7070dbSScott Long } 43204c7070dbSScott Long 43214c7070dbSScott Long /********************************************************************* 43224c7070dbSScott Long * 43234c7070dbSScott Long * BUS FUNCTION DEFINITIONS 43244c7070dbSScott Long * 43254c7070dbSScott Long **********************************************************************/ 43264c7070dbSScott Long 43274c7070dbSScott Long int 43284c7070dbSScott Long iflib_device_probe(device_t dev) 43294c7070dbSScott Long { 4330d49e83eaSMarius Strobl const pci_vendor_info_t *ent; 43314c7070dbSScott Long if_shared_ctx_t sctx; 4332d49e83eaSMarius Strobl uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id; 4333d49e83eaSMarius Strobl uint16_t pci_vendor_id; 43344c7070dbSScott Long 43354c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 43364c7070dbSScott Long return (ENOTSUP); 43374c7070dbSScott Long 43384c7070dbSScott Long pci_vendor_id = pci_get_vendor(dev); 43394c7070dbSScott Long pci_device_id = pci_get_device(dev); 43404c7070dbSScott Long pci_subvendor_id = pci_get_subvendor(dev); 43414c7070dbSScott Long pci_subdevice_id = pci_get_subdevice(dev); 43424c7070dbSScott Long pci_rev_id = pci_get_revid(dev); 43434c7070dbSScott Long if (sctx->isc_parse_devinfo != NULL) 43444c7070dbSScott Long sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id); 43454c7070dbSScott Long 43464c7070dbSScott Long ent = sctx->isc_vendor_info; 43474c7070dbSScott Long while (ent->pvi_vendor_id != 0) { 43484c7070dbSScott Long if (pci_vendor_id != ent->pvi_vendor_id) { 43494c7070dbSScott Long ent++; 43504c7070dbSScott Long continue; 43514c7070dbSScott Long } 43524c7070dbSScott Long if ((pci_device_id == ent->pvi_device_id) && 43534c7070dbSScott Long ((pci_subvendor_id == ent->pvi_subvendor_id) || 43544c7070dbSScott Long (ent->pvi_subvendor_id == 0)) && 43554c7070dbSScott Long ((pci_subdevice_id == ent->pvi_subdevice_id) || 43564c7070dbSScott Long (ent->pvi_subdevice_id == 0)) && 43574c7070dbSScott Long ((pci_rev_id == ent->pvi_rev_id) || 43584c7070dbSScott Long (ent->pvi_rev_id == 0))) { 43594c7070dbSScott Long 43604c7070dbSScott Long device_set_desc_copy(dev, ent->pvi_name); 43614c7070dbSScott Long /* this needs to be changed to zero if the bus probing code 43624c7070dbSScott Long * ever stops re-probing on best match because the sctx 43634c7070dbSScott Long * may have its values over written by register calls 43644c7070dbSScott Long * in subsequent probes 43654c7070dbSScott Long */ 43664c7070dbSScott Long return (BUS_PROBE_DEFAULT); 43674c7070dbSScott Long } 43684c7070dbSScott Long ent++; 43694c7070dbSScott Long } 43704c7070dbSScott Long return (ENXIO); 43714c7070dbSScott Long } 43724c7070dbSScott Long 4373668d6dbbSEric Joyner int 4374668d6dbbSEric Joyner iflib_device_probe_vendor(device_t dev) 4375668d6dbbSEric Joyner { 4376668d6dbbSEric Joyner int probe; 4377668d6dbbSEric Joyner 4378668d6dbbSEric Joyner probe = iflib_device_probe(dev); 4379668d6dbbSEric Joyner if (probe == BUS_PROBE_DEFAULT) 4380668d6dbbSEric Joyner return (BUS_PROBE_VENDOR); 4381668d6dbbSEric Joyner else 4382668d6dbbSEric Joyner return (probe); 4383668d6dbbSEric Joyner } 4384668d6dbbSEric Joyner 438509f6ff4fSMatt Macy static void 438609f6ff4fSMatt Macy iflib_reset_qvalues(if_ctx_t ctx) 43874c7070dbSScott Long { 438809f6ff4fSMatt Macy if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 438909f6ff4fSMatt Macy if_shared_ctx_t sctx = ctx->ifc_sctx; 439009f6ff4fSMatt Macy device_t dev = ctx->ifc_dev; 439146d0f824SMatt Macy int i; 43924c7070dbSScott Long 439323ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxqs != 0) 439423ac9029SStephen Hurd scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs; 439523ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxqs != 0) 439623ac9029SStephen Hurd scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs; 439723ac9029SStephen Hurd 439823ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 439923ac9029SStephen Hurd if (ctx->ifc_sysctl_ntxds[i] != 0) 440023ac9029SStephen Hurd scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i]; 440123ac9029SStephen Hurd else 440223ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 440323ac9029SStephen Hurd } 440423ac9029SStephen Hurd 440523ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 440623ac9029SStephen Hurd if (ctx->ifc_sysctl_nrxds[i] != 0) 440723ac9029SStephen Hurd scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i]; 440823ac9029SStephen Hurd else 440923ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 441023ac9029SStephen Hurd } 441123ac9029SStephen Hurd 441223ac9029SStephen Hurd for (i = 0; i < sctx->isc_nrxqs; i++) { 441323ac9029SStephen Hurd if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) { 441423ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n", 441523ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]); 441623ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i]; 441723ac9029SStephen Hurd } 441823ac9029SStephen Hurd if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) { 441923ac9029SStephen Hurd device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n", 442023ac9029SStephen Hurd i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]); 442123ac9029SStephen Hurd scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i]; 442223ac9029SStephen Hurd } 4423afb77372SEric Joyner if (!powerof2(scctx->isc_nrxd[i])) { 4424afb77372SEric Joyner device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n", 4425afb77372SEric Joyner i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]); 4426afb77372SEric Joyner scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i]; 4427afb77372SEric Joyner } 442823ac9029SStephen Hurd } 442923ac9029SStephen Hurd 443023ac9029SStephen Hurd for (i = 0; i < sctx->isc_ntxqs; i++) { 443123ac9029SStephen Hurd if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) { 443223ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n", 443323ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]); 443423ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i]; 443523ac9029SStephen Hurd } 443623ac9029SStephen Hurd if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) { 443723ac9029SStephen Hurd device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n", 443823ac9029SStephen Hurd i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]); 443923ac9029SStephen Hurd scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i]; 444023ac9029SStephen Hurd } 4441afb77372SEric Joyner if (!powerof2(scctx->isc_ntxd[i])) { 4442afb77372SEric Joyner device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n", 4443afb77372SEric Joyner i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]); 4444afb77372SEric Joyner scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i]; 4445afb77372SEric Joyner } 444623ac9029SStephen Hurd } 444709f6ff4fSMatt Macy } 4448ab2e3f79SStephen Hurd 44496d49b41eSAndrew Gallatin static void 44506d49b41eSAndrew Gallatin iflib_add_pfil(if_ctx_t ctx) 44516d49b41eSAndrew Gallatin { 44526d49b41eSAndrew Gallatin struct pfil_head *pfil; 44536d49b41eSAndrew Gallatin struct pfil_head_args pa; 44546d49b41eSAndrew Gallatin iflib_rxq_t rxq; 44556d49b41eSAndrew Gallatin int i; 44566d49b41eSAndrew Gallatin 44576d49b41eSAndrew Gallatin pa.pa_version = PFIL_VERSION; 44586d49b41eSAndrew Gallatin pa.pa_flags = PFIL_IN; 44596d49b41eSAndrew Gallatin pa.pa_type = PFIL_TYPE_ETHERNET; 44606d49b41eSAndrew Gallatin pa.pa_headname = ctx->ifc_ifp->if_xname; 44616d49b41eSAndrew Gallatin pfil = pfil_head_register(&pa); 44626d49b41eSAndrew Gallatin 44636d49b41eSAndrew Gallatin for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 44646d49b41eSAndrew Gallatin rxq->pfil = pfil; 44656d49b41eSAndrew Gallatin } 44666d49b41eSAndrew Gallatin } 44676d49b41eSAndrew Gallatin 44686d49b41eSAndrew Gallatin static void 44696d49b41eSAndrew Gallatin iflib_rem_pfil(if_ctx_t ctx) 44706d49b41eSAndrew Gallatin { 44716d49b41eSAndrew Gallatin struct pfil_head *pfil; 44726d49b41eSAndrew Gallatin iflib_rxq_t rxq; 44736d49b41eSAndrew Gallatin int i; 44746d49b41eSAndrew Gallatin 44756d49b41eSAndrew Gallatin rxq = ctx->ifc_rxqs; 44766d49b41eSAndrew Gallatin pfil = rxq->pfil; 44776d49b41eSAndrew Gallatin for (i = 0; i < NRXQSETS(ctx); i++, rxq++) { 44786d49b41eSAndrew Gallatin rxq->pfil = NULL; 44796d49b41eSAndrew Gallatin } 44806d49b41eSAndrew Gallatin pfil_head_unregister(pfil); 44816d49b41eSAndrew Gallatin } 44826d49b41eSAndrew Gallatin 4483f154ece0SStephen Hurd static uint16_t 4484f154ece0SStephen Hurd get_ctx_core_offset(if_ctx_t ctx) 4485f154ece0SStephen Hurd { 4486f154ece0SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 4487f154ece0SStephen Hurd struct cpu_offset *op; 4488f154ece0SStephen Hurd uint16_t qc; 4489f154ece0SStephen Hurd uint16_t ret = ctx->ifc_sysctl_core_offset; 4490f154ece0SStephen Hurd 4491f154ece0SStephen Hurd if (ret != CORE_OFFSET_UNSPECIFIED) 4492f154ece0SStephen Hurd return (ret); 4493f154ece0SStephen Hurd 4494f154ece0SStephen Hurd if (ctx->ifc_sysctl_separate_txrx) 4495f154ece0SStephen Hurd qc = scctx->isc_ntxqsets + scctx->isc_nrxqsets; 4496f154ece0SStephen Hurd else 4497f154ece0SStephen Hurd qc = max(scctx->isc_ntxqsets, scctx->isc_nrxqsets); 4498f154ece0SStephen Hurd 4499f154ece0SStephen Hurd mtx_lock(&cpu_offset_mtx); 4500f154ece0SStephen Hurd SLIST_FOREACH(op, &cpu_offsets, entries) { 4501f154ece0SStephen Hurd if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 4502f154ece0SStephen Hurd ret = op->offset; 4503f154ece0SStephen Hurd op->offset += qc; 4504f154ece0SStephen Hurd MPASS(op->refcount < UINT_MAX); 4505f154ece0SStephen Hurd op->refcount++; 4506f154ece0SStephen Hurd break; 4507f154ece0SStephen Hurd } 4508f154ece0SStephen Hurd } 4509f154ece0SStephen Hurd if (ret == CORE_OFFSET_UNSPECIFIED) { 4510f154ece0SStephen Hurd ret = 0; 4511f154ece0SStephen Hurd op = malloc(sizeof(struct cpu_offset), M_IFLIB, 4512f154ece0SStephen Hurd M_NOWAIT | M_ZERO); 4513f154ece0SStephen Hurd if (op == NULL) { 4514f154ece0SStephen Hurd device_printf(ctx->ifc_dev, 4515f154ece0SStephen Hurd "allocation for cpu offset failed.\n"); 4516f154ece0SStephen Hurd } else { 4517f154ece0SStephen Hurd op->offset = qc; 4518f154ece0SStephen Hurd op->refcount = 1; 4519f154ece0SStephen Hurd CPU_COPY(&ctx->ifc_cpus, &op->set); 4520f154ece0SStephen Hurd SLIST_INSERT_HEAD(&cpu_offsets, op, entries); 4521f154ece0SStephen Hurd } 4522f154ece0SStephen Hurd } 4523f154ece0SStephen Hurd mtx_unlock(&cpu_offset_mtx); 4524f154ece0SStephen Hurd 4525f154ece0SStephen Hurd return (ret); 4526f154ece0SStephen Hurd } 4527f154ece0SStephen Hurd 4528f154ece0SStephen Hurd static void 4529f154ece0SStephen Hurd unref_ctx_core_offset(if_ctx_t ctx) 4530f154ece0SStephen Hurd { 4531f154ece0SStephen Hurd struct cpu_offset *op, *top; 4532f154ece0SStephen Hurd 4533f154ece0SStephen Hurd mtx_lock(&cpu_offset_mtx); 4534f154ece0SStephen Hurd SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) { 4535f154ece0SStephen Hurd if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) { 4536f154ece0SStephen Hurd MPASS(op->refcount > 0); 4537f154ece0SStephen Hurd op->refcount--; 4538f154ece0SStephen Hurd if (op->refcount == 0) { 4539f154ece0SStephen Hurd SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries); 4540f154ece0SStephen Hurd free(op, M_IFLIB); 4541f154ece0SStephen Hurd } 4542f154ece0SStephen Hurd break; 4543f154ece0SStephen Hurd } 4544f154ece0SStephen Hurd } 4545f154ece0SStephen Hurd mtx_unlock(&cpu_offset_mtx); 4546f154ece0SStephen Hurd } 4547f154ece0SStephen Hurd 454809f6ff4fSMatt Macy int 454909f6ff4fSMatt Macy iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp) 455009f6ff4fSMatt Macy { 455109f6ff4fSMatt Macy if_ctx_t ctx; 455209f6ff4fSMatt Macy if_t ifp; 455309f6ff4fSMatt Macy if_softc_ctx_t scctx; 45543d10e9edSMarius Strobl kobjop_desc_t kobj_desc; 45553d10e9edSMarius Strobl kobj_method_t *kobj_method; 4556afb77372SEric Joyner int err, msix, rid; 45573d10e9edSMarius Strobl uint16_t main_rxq, main_txq; 455809f6ff4fSMatt Macy 455909f6ff4fSMatt Macy ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO); 456009f6ff4fSMatt Macy 456109f6ff4fSMatt Macy if (sc == NULL) { 456209f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 456309f6ff4fSMatt Macy device_set_softc(dev, ctx); 456409f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 456509f6ff4fSMatt Macy } 456609f6ff4fSMatt Macy 456709f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 456809f6ff4fSMatt Macy ctx->ifc_dev = dev; 456909f6ff4fSMatt Macy ctx->ifc_softc = sc; 457009f6ff4fSMatt Macy 457109f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 457209f6ff4fSMatt Macy device_printf(dev, "iflib_register failed %d\n", err); 45737f3eb9daSPatrick Kelsey goto fail_ctx_free; 457409f6ff4fSMatt Macy } 457509f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 457609f6ff4fSMatt Macy 457709f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 457809f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 457909f6ff4fSMatt Macy 458009f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4581aa8a24d3SStephen Hurd CTX_LOCK(ctx); 4582ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 45834c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 45847f3eb9daSPatrick Kelsey goto fail_unlock; 45854c7070dbSScott Long } 45861248952aSSean Bruno _iflib_pre_assert(scctx); 45871248952aSSean Bruno ctx->ifc_txrx = *scctx->isc_txrx; 45881248952aSSean Bruno 4589e2621d96SMatt Macy if (sctx->isc_flags & IFLIB_DRIVER_MEDIA) 4590e2621d96SMatt Macy ctx->ifc_mediap = scctx->isc_media; 4591e2621d96SMatt Macy 45921248952aSSean Bruno #ifdef INVARIANTS 45937f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 45941248952aSSean Bruno MPASS(scctx->isc_tx_csum_flags); 45951248952aSSean Bruno #endif 45961248952aSSean Bruno 45977f87c040SMarius Strobl if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS); 459818a660b3SSean Bruno if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS); 45991248952aSSean Bruno 46001248952aSSean Bruno if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 46011248952aSSean Bruno scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 46021248952aSSean Bruno if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 46031248952aSSean Bruno scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 460423ac9029SStephen Hurd 460595246abbSSean Bruno main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0; 460695246abbSSean Bruno main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0; 460723ac9029SStephen Hurd 460823ac9029SStephen Hurd /* XXX change for per-queue sizes */ 46091722eeacSMarius Strobl device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 461023ac9029SStephen Hurd scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]); 461123ac9029SStephen Hurd 461223ac9029SStephen Hurd if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] / 461323ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION) 461423ac9029SStephen Hurd scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] / 461523ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION); 461623ac9029SStephen Hurd if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] / 461723ac9029SStephen Hurd MAX_SINGLE_PACKET_FRACTION) 461823ac9029SStephen Hurd scctx->isc_tx_tso_segments_max = max(1, 461923ac9029SStephen Hurd scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION); 46204c7070dbSScott Long 46214c7070dbSScott Long /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 46227f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 46237f87c040SMarius Strobl /* 46247f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 46257f87c040SMarius Strobl * but some MACs do. 46267f87c040SMarius Strobl */ 46277f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 46287f87c040SMarius Strobl IP_MAXPACKET)); 46297f87c040SMarius Strobl /* 46307f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 46317f87c040SMarius Strobl * into account. In the worst case, each of these calls will 46327f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 46337f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 46347f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 46357f87c040SMarius Strobl * require defragmentation in iflib_encap(). 46367f87c040SMarius Strobl */ 46377f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 46387f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 46397f87c040SMarius Strobl } 46404c7070dbSScott Long if (scctx->isc_rss_table_size == 0) 46414c7070dbSScott Long scctx->isc_rss_table_size = 64; 464223ac9029SStephen Hurd scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 4643da69b8f9SSean Bruno 4644da69b8f9SSean Bruno GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 4645da69b8f9SSean Bruno /* XXX format name */ 4646f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4647f855ec81SMarius Strobl NULL, NULL, "admin"); 4648e516b535SStephen Hurd 4649772593dbSStephen Hurd /* Set up cpu set. If it fails, use the set of all CPUs. */ 4650e516b535SStephen Hurd if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) { 4651e516b535SStephen Hurd device_printf(dev, "Unable to fetch CPU list\n"); 4652e516b535SStephen Hurd CPU_COPY(&all_cpus, &ctx->ifc_cpus); 4653e516b535SStephen Hurd } 4654e516b535SStephen Hurd MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0); 4655e516b535SStephen Hurd 46564c7070dbSScott Long /* 4657b97de13aSMarius Strobl ** Now set up MSI or MSI-X, should return us the number of supported 4658b97de13aSMarius Strobl ** vectors (will be 1 for a legacy interrupt and MSI). 46594c7070dbSScott Long */ 46604c7070dbSScott Long if (sctx->isc_flags & IFLIB_SKIP_MSIX) { 46614c7070dbSScott Long msix = scctx->isc_vectors; 46624c7070dbSScott Long } else if (scctx->isc_msix_bar != 0) 4663f7ae9a84SSean Bruno /* 4664f7ae9a84SSean Bruno * The simple fact that isc_msix_bar is not 0 does not mean we 4665f7ae9a84SSean Bruno * we have a good value there that is known to work. 4666f7ae9a84SSean Bruno */ 46674c7070dbSScott Long msix = iflib_msix_init(ctx); 46684c7070dbSScott Long else { 46694c7070dbSScott Long scctx->isc_vectors = 1; 46704c7070dbSScott Long scctx->isc_ntxqsets = 1; 46714c7070dbSScott Long scctx->isc_nrxqsets = 1; 46724c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 46734c7070dbSScott Long msix = 0; 46744c7070dbSScott Long } 46754c7070dbSScott Long /* Get memory for the station queues */ 46764c7070dbSScott Long if ((err = iflib_queues_alloc(ctx))) { 46774c7070dbSScott Long device_printf(dev, "Unable to allocate queue memory\n"); 46787f3eb9daSPatrick Kelsey goto fail_intr_free; 46794c7070dbSScott Long } 46804c7070dbSScott Long 4681ac88e6daSStephen Hurd if ((err = iflib_qset_structures_setup(ctx))) 46824c7070dbSScott Long goto fail_queues; 468369b7fc3eSSean Bruno 4684bd84f700SSean Bruno /* 4685f154ece0SStephen Hurd * Now that we know how many queues there are, get the core offset. 4686f154ece0SStephen Hurd */ 4687f154ece0SStephen Hurd ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx); 4688f154ece0SStephen Hurd 4689f154ece0SStephen Hurd /* 4690bd84f700SSean Bruno * Group taskqueues aren't properly set up until SMP is started, 4691bd84f700SSean Bruno * so we disable interrupts until we can handle them post 4692bd84f700SSean Bruno * SI_SUB_SMP. 4693bd84f700SSean Bruno * 4694bd84f700SSean Bruno * XXX: disabling interrupts doesn't actually work, at least for 4695bd84f700SSean Bruno * the non-MSI case. When they occur before SI_SUB_SMP completes, 4696bd84f700SSean Bruno * we do null handling and depend on this not causing too large an 4697bd84f700SSean Bruno * interrupt storm. 4698bd84f700SSean Bruno */ 46991248952aSSean Bruno IFDI_INTR_DISABLE(ctx); 47003d10e9edSMarius Strobl 47013d10e9edSMarius Strobl if (msix > 1) { 47023d10e9edSMarius Strobl /* 47033d10e9edSMarius Strobl * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable 47043d10e9edSMarius Strobl * aren't the default NULL implementation. 47053d10e9edSMarius Strobl */ 47063d10e9edSMarius Strobl kobj_desc = &ifdi_rx_queue_intr_enable_desc; 47073d10e9edSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 47083d10e9edSMarius Strobl kobj_desc); 47093d10e9edSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 47103d10e9edSMarius Strobl device_printf(dev, 47113d10e9edSMarius Strobl "MSI-X requires ifdi_rx_queue_intr_enable method"); 47123d10e9edSMarius Strobl err = EOPNOTSUPP; 47137f3eb9daSPatrick Kelsey goto fail_queues; 47144c7070dbSScott Long } 47153d10e9edSMarius Strobl kobj_desc = &ifdi_tx_queue_intr_enable_desc; 47163d10e9edSMarius Strobl kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL, 47173d10e9edSMarius Strobl kobj_desc); 47183d10e9edSMarius Strobl if (kobj_method == &kobj_desc->deflt) { 47193d10e9edSMarius Strobl device_printf(dev, 47203d10e9edSMarius Strobl "MSI-X requires ifdi_tx_queue_intr_enable method"); 47213d10e9edSMarius Strobl err = EOPNOTSUPP; 47223d10e9edSMarius Strobl goto fail_queues; 47233d10e9edSMarius Strobl } 47243d10e9edSMarius Strobl 47253d10e9edSMarius Strobl /* 47263d10e9edSMarius Strobl * Assign the MSI-X vectors. 47273d10e9edSMarius Strobl * Note that the default NULL ifdi_msix_intr_assign method will 47283d10e9edSMarius Strobl * fail here, too. 47293d10e9edSMarius Strobl */ 47303d10e9edSMarius Strobl err = IFDI_MSIX_INTR_ASSIGN(ctx, msix); 47313d10e9edSMarius Strobl if (err != 0) { 47323d10e9edSMarius Strobl device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n", 47333d10e9edSMarius Strobl err); 47343d10e9edSMarius Strobl goto fail_queues; 47353d10e9edSMarius Strobl } 47363d10e9edSMarius Strobl } else { 47374c7070dbSScott Long rid = 0; 47384c7070dbSScott Long if (scctx->isc_intr == IFLIB_INTR_MSI) { 47394c7070dbSScott Long MPASS(msix == 1); 47404c7070dbSScott Long rid = 1; 47414c7070dbSScott Long } 474223ac9029SStephen Hurd if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) { 47434c7070dbSScott Long device_printf(dev, "iflib_legacy_setup failed %d\n", err); 47447f3eb9daSPatrick Kelsey goto fail_queues; 47454c7070dbSScott Long } 47464c7070dbSScott Long } 47477f87c040SMarius Strobl 47481fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 47497f87c040SMarius Strobl 4750ab2e3f79SStephen Hurd if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 47514c7070dbSScott Long device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 47524c7070dbSScott Long goto fail_detach; 47534c7070dbSScott Long } 47547f87c040SMarius Strobl 47557f87c040SMarius Strobl /* 47567f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 47577f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 47587f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 47597f87c040SMarius Strobl */ 47607f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 47617f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 47627f87c040SMarius Strobl 47634c7070dbSScott Long if ((err = iflib_netmap_attach(ctx))) { 47644c7070dbSScott Long device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err); 47654c7070dbSScott Long goto fail_detach; 47664c7070dbSScott Long } 47674c7070dbSScott Long *ctxp = ctx; 47684c7070dbSScott Long 476994618825SMark Johnston NETDUMP_SET(ctx->ifc_ifp, iflib); 477094618825SMark Johnston 477123ac9029SStephen Hurd if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 47724c7070dbSScott Long iflib_add_device_sysctl_post(ctx); 47736d49b41eSAndrew Gallatin iflib_add_pfil(ctx); 47744ecb427aSSean Bruno ctx->ifc_flags |= IFC_INIT_DONE; 4775aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 47763d10e9edSMarius Strobl 47774c7070dbSScott Long return (0); 477877c1fcecSEric Joyner 47794c7070dbSScott Long fail_detach: 47804c7070dbSScott Long ether_ifdetach(ctx->ifc_ifp); 47814c7070dbSScott Long fail_intr_free: 47827f3eb9daSPatrick Kelsey iflib_free_intr_mem(ctx); 47834c7070dbSScott Long fail_queues: 47846108c013SStephen Hurd iflib_tx_structures_free(ctx); 47856108c013SStephen Hurd iflib_rx_structures_free(ctx); 47864c7070dbSScott Long IFDI_DETACH(ctx); 47877f3eb9daSPatrick Kelsey fail_unlock: 4788aa8a24d3SStephen Hurd CTX_UNLOCK(ctx); 47897f3eb9daSPatrick Kelsey fail_ctx_free: 47907f3eb9daSPatrick Kelsey if (ctx->ifc_flags & IFC_SC_ALLOCATED) 47917f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 47927f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 47934c7070dbSScott Long return (err); 47944c7070dbSScott Long } 47954c7070dbSScott Long 47964c7070dbSScott Long int 479709f6ff4fSMatt Macy iflib_pseudo_register(device_t dev, if_shared_ctx_t sctx, if_ctx_t *ctxp, 479809f6ff4fSMatt Macy struct iflib_cloneattach_ctx *clctx) 479909f6ff4fSMatt Macy { 480009f6ff4fSMatt Macy int err; 480109f6ff4fSMatt Macy if_ctx_t ctx; 480209f6ff4fSMatt Macy if_t ifp; 480309f6ff4fSMatt Macy if_softc_ctx_t scctx; 480409f6ff4fSMatt Macy int i; 480509f6ff4fSMatt Macy void *sc; 480609f6ff4fSMatt Macy uint16_t main_txq; 480709f6ff4fSMatt Macy uint16_t main_rxq; 480809f6ff4fSMatt Macy 480909f6ff4fSMatt Macy ctx = malloc(sizeof(*ctx), M_IFLIB, M_WAITOK|M_ZERO); 481009f6ff4fSMatt Macy sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO); 481109f6ff4fSMatt Macy ctx->ifc_flags |= IFC_SC_ALLOCATED; 481209f6ff4fSMatt Macy if (sctx->isc_flags & (IFLIB_PSEUDO|IFLIB_VIRTUAL)) 481309f6ff4fSMatt Macy ctx->ifc_flags |= IFC_PSEUDO; 481409f6ff4fSMatt Macy 481509f6ff4fSMatt Macy ctx->ifc_sctx = sctx; 481609f6ff4fSMatt Macy ctx->ifc_softc = sc; 481709f6ff4fSMatt Macy ctx->ifc_dev = dev; 481809f6ff4fSMatt Macy 481909f6ff4fSMatt Macy if ((err = iflib_register(ctx)) != 0) { 482009f6ff4fSMatt Macy device_printf(dev, "%s: iflib_register failed %d\n", __func__, err); 48217f3eb9daSPatrick Kelsey goto fail_ctx_free; 482209f6ff4fSMatt Macy } 482309f6ff4fSMatt Macy iflib_add_device_sysctl_pre(ctx); 482409f6ff4fSMatt Macy 482509f6ff4fSMatt Macy scctx = &ctx->ifc_softc_ctx; 482609f6ff4fSMatt Macy ifp = ctx->ifc_ifp; 482709f6ff4fSMatt Macy 482809f6ff4fSMatt Macy iflib_reset_qvalues(ctx); 4829aac9c817SEric Joyner CTX_LOCK(ctx); 483009f6ff4fSMatt Macy if ((err = IFDI_ATTACH_PRE(ctx)) != 0) { 483109f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err); 4832aac9c817SEric Joyner goto fail_unlock; 483309f6ff4fSMatt Macy } 483409f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_GEN_MAC) 48351fd8c72cSKyle Evans ether_gen_addr(ifp, &ctx->ifc_mac); 483609f6ff4fSMatt Macy if ((err = IFDI_CLONEATTACH(ctx, clctx->cc_ifc, clctx->cc_name, 483709f6ff4fSMatt Macy clctx->cc_params)) != 0) { 483809f6ff4fSMatt Macy device_printf(dev, "IFDI_CLONEATTACH failed %d\n", err); 48397f3eb9daSPatrick Kelsey goto fail_ctx_free; 484009f6ff4fSMatt Macy } 4841e2621d96SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL); 4842e2621d96SMatt Macy ifmedia_add(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO, 0, NULL); 4843e2621d96SMatt Macy ifmedia_set(ctx->ifc_mediap, IFM_ETHER | IFM_AUTO); 484409f6ff4fSMatt Macy 484509f6ff4fSMatt Macy #ifdef INVARIANTS 48467f87c040SMarius Strobl if (scctx->isc_capabilities & IFCAP_TXCSUM) 484709f6ff4fSMatt Macy MPASS(scctx->isc_tx_csum_flags); 484809f6ff4fSMatt Macy #endif 484909f6ff4fSMatt Macy 48507f87c040SMarius Strobl if_setcapabilities(ifp, scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_LINKSTATE); 485109f6ff4fSMatt Macy if_setcapenable(ifp, scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_LINKSTATE); 485209f6ff4fSMatt Macy 485309f6ff4fSMatt Macy ifp->if_flags |= IFF_NOGROUP; 485409f6ff4fSMatt Macy if (sctx->isc_flags & IFLIB_PSEUDO) { 48551fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 485609f6ff4fSMatt Macy 485709f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 485809f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 485909f6ff4fSMatt Macy goto fail_detach; 486009f6ff4fSMatt Macy } 486109f6ff4fSMatt Macy *ctxp = ctx; 486209f6ff4fSMatt Macy 48637f87c040SMarius Strobl /* 48647f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 48657f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 48667f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 48677f87c040SMarius Strobl */ 48687f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 48697f87c040SMarius Strobl if_setifheaderlen(ifp, 48707f87c040SMarius Strobl sizeof(struct ether_vlan_header)); 48717f87c040SMarius Strobl 487209f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 487309f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 487409f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 487509f6ff4fSMatt Macy return (0); 487609f6ff4fSMatt Macy } 487709f6ff4fSMatt Macy _iflib_pre_assert(scctx); 487809f6ff4fSMatt Macy ctx->ifc_txrx = *scctx->isc_txrx; 487909f6ff4fSMatt Macy 488009f6ff4fSMatt Macy if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets)) 488109f6ff4fSMatt Macy scctx->isc_ntxqsets = scctx->isc_ntxqsets_max; 488209f6ff4fSMatt Macy if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets)) 488309f6ff4fSMatt Macy scctx->isc_nrxqsets = scctx->isc_nrxqsets_max; 488409f6ff4fSMatt Macy 488509f6ff4fSMatt Macy main_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0; 488609f6ff4fSMatt Macy main_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0; 488709f6ff4fSMatt Macy 488809f6ff4fSMatt Macy /* XXX change for per-queue sizes */ 48891722eeacSMarius Strobl device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n", 489009f6ff4fSMatt Macy scctx->isc_ntxd[main_txq], scctx->isc_nrxd[main_rxq]); 489109f6ff4fSMatt Macy 489209f6ff4fSMatt Macy if (scctx->isc_tx_nsegments > scctx->isc_ntxd[main_txq] / 489309f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION) 489409f6ff4fSMatt Macy scctx->isc_tx_nsegments = max(1, scctx->isc_ntxd[main_txq] / 489509f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION); 489609f6ff4fSMatt Macy if (scctx->isc_tx_tso_segments_max > scctx->isc_ntxd[main_txq] / 489709f6ff4fSMatt Macy MAX_SINGLE_PACKET_FRACTION) 489809f6ff4fSMatt Macy scctx->isc_tx_tso_segments_max = max(1, 489909f6ff4fSMatt Macy scctx->isc_ntxd[main_txq] / MAX_SINGLE_PACKET_FRACTION); 490009f6ff4fSMatt Macy 490109f6ff4fSMatt Macy /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */ 49027f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_TSO) { 49037f87c040SMarius Strobl /* 49047f87c040SMarius Strobl * The stack can't handle a TSO size larger than IP_MAXPACKET, 49057f87c040SMarius Strobl * but some MACs do. 49067f87c040SMarius Strobl */ 49077f87c040SMarius Strobl if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max, 49087f87c040SMarius Strobl IP_MAXPACKET)); 49097f87c040SMarius Strobl /* 49107f87c040SMarius Strobl * Take maximum number of m_pullup(9)'s in iflib_parse_header() 49117f87c040SMarius Strobl * into account. In the worst case, each of these calls will 49127f87c040SMarius Strobl * add another mbuf and, thus, the requirement for another DMA 49137f87c040SMarius Strobl * segment. So for best performance, it doesn't make sense to 49147f87c040SMarius Strobl * advertize a maximum of TSO segments that typically will 49157f87c040SMarius Strobl * require defragmentation in iflib_encap(). 49167f87c040SMarius Strobl */ 49177f87c040SMarius Strobl if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3); 49187f87c040SMarius Strobl if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max); 49197f87c040SMarius Strobl } 492009f6ff4fSMatt Macy if (scctx->isc_rss_table_size == 0) 492109f6ff4fSMatt Macy scctx->isc_rss_table_size = 64; 492209f6ff4fSMatt Macy scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1; 492309f6ff4fSMatt Macy 492409f6ff4fSMatt Macy GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx); 492509f6ff4fSMatt Macy /* XXX format name */ 4926f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx, 4927f855ec81SMarius Strobl NULL, NULL, "admin"); 492809f6ff4fSMatt Macy 492909f6ff4fSMatt Macy /* XXX --- can support > 1 -- but keep it simple for now */ 493009f6ff4fSMatt Macy scctx->isc_intr = IFLIB_INTR_LEGACY; 493109f6ff4fSMatt Macy 493209f6ff4fSMatt Macy /* Get memory for the station queues */ 493309f6ff4fSMatt Macy if ((err = iflib_queues_alloc(ctx))) { 493409f6ff4fSMatt Macy device_printf(dev, "Unable to allocate queue memory\n"); 49357f3eb9daSPatrick Kelsey goto fail_iflib_detach; 493609f6ff4fSMatt Macy } 493709f6ff4fSMatt Macy 493809f6ff4fSMatt Macy if ((err = iflib_qset_structures_setup(ctx))) { 493909f6ff4fSMatt Macy device_printf(dev, "qset structure setup failed %d\n", err); 494009f6ff4fSMatt Macy goto fail_queues; 494109f6ff4fSMatt Macy } 49427f87c040SMarius Strobl 494309f6ff4fSMatt Macy /* 494409f6ff4fSMatt Macy * XXX What if anything do we want to do about interrupts? 494509f6ff4fSMatt Macy */ 49461fd8c72cSKyle Evans ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet); 494709f6ff4fSMatt Macy if ((err = IFDI_ATTACH_POST(ctx)) != 0) { 494809f6ff4fSMatt Macy device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err); 494909f6ff4fSMatt Macy goto fail_detach; 495009f6ff4fSMatt Macy } 49517f87c040SMarius Strobl 49527f87c040SMarius Strobl /* 49537f87c040SMarius Strobl * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported. 49547f87c040SMarius Strobl * This must appear after the call to ether_ifattach() because 49557f87c040SMarius Strobl * ether_ifattach() sets if_hdrlen to the default value. 49567f87c040SMarius Strobl */ 49577f87c040SMarius Strobl if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 49587f87c040SMarius Strobl if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 49597f87c040SMarius Strobl 496009f6ff4fSMatt Macy /* XXX handle more than one queue */ 496109f6ff4fSMatt Macy for (i = 0; i < scctx->isc_nrxqsets; i++) 496209f6ff4fSMatt Macy IFDI_RX_CLSET(ctx, 0, i, ctx->ifc_rxqs[i].ifr_fl[0].ifl_sds.ifsd_cl); 496309f6ff4fSMatt Macy 496409f6ff4fSMatt Macy *ctxp = ctx; 496509f6ff4fSMatt Macy 496609f6ff4fSMatt Macy if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter); 496709f6ff4fSMatt Macy iflib_add_device_sysctl_post(ctx); 496809f6ff4fSMatt Macy ctx->ifc_flags |= IFC_INIT_DONE; 4969aac9c817SEric Joyner CTX_UNLOCK(ctx); 49703d10e9edSMarius Strobl 497109f6ff4fSMatt Macy return (0); 497209f6ff4fSMatt Macy fail_detach: 497309f6ff4fSMatt Macy ether_ifdetach(ctx->ifc_ifp); 497409f6ff4fSMatt Macy fail_queues: 497509f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 497609f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 49777f3eb9daSPatrick Kelsey fail_iflib_detach: 497809f6ff4fSMatt Macy IFDI_DETACH(ctx); 4979aac9c817SEric Joyner fail_unlock: 4980aac9c817SEric Joyner CTX_UNLOCK(ctx); 49817f3eb9daSPatrick Kelsey fail_ctx_free: 49827f3eb9daSPatrick Kelsey free(ctx->ifc_softc, M_IFLIB); 49837f3eb9daSPatrick Kelsey free(ctx, M_IFLIB); 498409f6ff4fSMatt Macy return (err); 498509f6ff4fSMatt Macy } 498609f6ff4fSMatt Macy 498709f6ff4fSMatt Macy int 498809f6ff4fSMatt Macy iflib_pseudo_deregister(if_ctx_t ctx) 498909f6ff4fSMatt Macy { 499009f6ff4fSMatt Macy if_t ifp = ctx->ifc_ifp; 499109f6ff4fSMatt Macy iflib_txq_t txq; 499209f6ff4fSMatt Macy iflib_rxq_t rxq; 499309f6ff4fSMatt Macy int i, j; 499409f6ff4fSMatt Macy struct taskqgroup *tqg; 499509f6ff4fSMatt Macy iflib_fl_t fl; 499609f6ff4fSMatt Macy 499709f6ff4fSMatt Macy /* Unregister VLAN events */ 499809f6ff4fSMatt Macy if (ctx->ifc_vlan_attach_event != NULL) 499909f6ff4fSMatt Macy EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 500009f6ff4fSMatt Macy if (ctx->ifc_vlan_detach_event != NULL) 500109f6ff4fSMatt Macy EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 500209f6ff4fSMatt Macy 500309f6ff4fSMatt Macy ether_ifdetach(ifp); 500409f6ff4fSMatt Macy /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 500509f6ff4fSMatt Macy CTX_LOCK_DESTROY(ctx); 500609f6ff4fSMatt Macy /* XXX drain any dependent tasks */ 500709f6ff4fSMatt Macy tqg = qgroup_if_io_tqg; 500809f6ff4fSMatt Macy for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 500909f6ff4fSMatt Macy callout_drain(&txq->ift_timer); 501009f6ff4fSMatt Macy if (txq->ift_task.gt_uniq != NULL) 501109f6ff4fSMatt Macy taskqgroup_detach(tqg, &txq->ift_task); 501209f6ff4fSMatt Macy } 501309f6ff4fSMatt Macy for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 501409f6ff4fSMatt Macy if (rxq->ifr_task.gt_uniq != NULL) 501509f6ff4fSMatt Macy taskqgroup_detach(tqg, &rxq->ifr_task); 501609f6ff4fSMatt Macy 501709f6ff4fSMatt Macy for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 501809f6ff4fSMatt Macy free(fl->ifl_rx_bitmap, M_IFLIB); 501909f6ff4fSMatt Macy } 502009f6ff4fSMatt Macy tqg = qgroup_if_config_tqg; 502109f6ff4fSMatt Macy if (ctx->ifc_admin_task.gt_uniq != NULL) 502209f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_admin_task); 502309f6ff4fSMatt Macy if (ctx->ifc_vflr_task.gt_uniq != NULL) 502409f6ff4fSMatt Macy taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 502509f6ff4fSMatt Macy 502609f6ff4fSMatt Macy if_free(ifp); 502709f6ff4fSMatt Macy 502809f6ff4fSMatt Macy iflib_tx_structures_free(ctx); 502909f6ff4fSMatt Macy iflib_rx_structures_free(ctx); 503009f6ff4fSMatt Macy if (ctx->ifc_flags & IFC_SC_ALLOCATED) 503109f6ff4fSMatt Macy free(ctx->ifc_softc, M_IFLIB); 503209f6ff4fSMatt Macy free(ctx, M_IFLIB); 503309f6ff4fSMatt Macy return (0); 503409f6ff4fSMatt Macy } 503509f6ff4fSMatt Macy 503609f6ff4fSMatt Macy int 50374c7070dbSScott Long iflib_device_attach(device_t dev) 50384c7070dbSScott Long { 50394c7070dbSScott Long if_ctx_t ctx; 50404c7070dbSScott Long if_shared_ctx_t sctx; 50414c7070dbSScott Long 50424c7070dbSScott Long if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC) 50434c7070dbSScott Long return (ENOTSUP); 50444c7070dbSScott Long 50454c7070dbSScott Long pci_enable_busmaster(dev); 50464c7070dbSScott Long 50474c7070dbSScott Long return (iflib_device_register(dev, NULL, sctx, &ctx)); 50484c7070dbSScott Long } 50494c7070dbSScott Long 50504c7070dbSScott Long int 50514c7070dbSScott Long iflib_device_deregister(if_ctx_t ctx) 50524c7070dbSScott Long { 50534c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 50544c7070dbSScott Long iflib_txq_t txq; 50554c7070dbSScott Long iflib_rxq_t rxq; 50564c7070dbSScott Long device_t dev = ctx->ifc_dev; 505787890dbaSSean Bruno int i, j; 50584c7070dbSScott Long struct taskqgroup *tqg; 505987890dbaSSean Bruno iflib_fl_t fl; 50604c7070dbSScott Long 50614c7070dbSScott Long /* Make sure VLANS are not using driver */ 50624c7070dbSScott Long if (if_vlantrunkinuse(ifp)) { 50634c7070dbSScott Long device_printf(dev, "Vlan in use, detach first\n"); 50644c7070dbSScott Long return (EBUSY); 50654c7070dbSScott Long } 506677c1fcecSEric Joyner #ifdef PCI_IOV 506777c1fcecSEric Joyner if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) { 506877c1fcecSEric Joyner device_printf(dev, "SR-IOV in use; detach first.\n"); 506977c1fcecSEric Joyner return (EBUSY); 507077c1fcecSEric Joyner } 507177c1fcecSEric Joyner #endif 507277c1fcecSEric Joyner 507377c1fcecSEric Joyner STATE_LOCK(ctx); 507477c1fcecSEric Joyner ctx->ifc_flags |= IFC_IN_DETACH; 507577c1fcecSEric Joyner STATE_UNLOCK(ctx); 50764c7070dbSScott Long 50774c7070dbSScott Long CTX_LOCK(ctx); 50784c7070dbSScott Long iflib_stop(ctx); 50794c7070dbSScott Long CTX_UNLOCK(ctx); 50804c7070dbSScott Long 50814c7070dbSScott Long /* Unregister VLAN events */ 50824c7070dbSScott Long if (ctx->ifc_vlan_attach_event != NULL) 50834c7070dbSScott Long EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event); 50844c7070dbSScott Long if (ctx->ifc_vlan_detach_event != NULL) 50854c7070dbSScott Long EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event); 50864c7070dbSScott Long 50874c7070dbSScott Long iflib_netmap_detach(ifp); 50884c7070dbSScott Long ether_ifdetach(ifp); 50896d49b41eSAndrew Gallatin iflib_rem_pfil(ctx); 50904c7070dbSScott Long if (ctx->ifc_led_dev != NULL) 50914c7070dbSScott Long led_destroy(ctx->ifc_led_dev); 50924c7070dbSScott Long /* XXX drain any dependent tasks */ 5093ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 509423ac9029SStephen Hurd for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) { 50954c7070dbSScott Long callout_drain(&txq->ift_timer); 50964c7070dbSScott Long if (txq->ift_task.gt_uniq != NULL) 50974c7070dbSScott Long taskqgroup_detach(tqg, &txq->ift_task); 50984c7070dbSScott Long } 50994c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) { 51004c7070dbSScott Long if (rxq->ifr_task.gt_uniq != NULL) 51014c7070dbSScott Long taskqgroup_detach(tqg, &rxq->ifr_task); 510287890dbaSSean Bruno 510387890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 510487890dbaSSean Bruno free(fl->ifl_rx_bitmap, M_IFLIB); 51054c7070dbSScott Long } 5106ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 51074c7070dbSScott Long if (ctx->ifc_admin_task.gt_uniq != NULL) 51084c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_admin_task); 51094c7070dbSScott Long if (ctx->ifc_vflr_task.gt_uniq != NULL) 51104c7070dbSScott Long taskqgroup_detach(tqg, &ctx->ifc_vflr_task); 51116c3c3194SMatt Macy CTX_LOCK(ctx); 51124c7070dbSScott Long IFDI_DETACH(ctx); 51136c3c3194SMatt Macy CTX_UNLOCK(ctx); 51146c3c3194SMatt Macy 51156c3c3194SMatt Macy /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/ 51166c3c3194SMatt Macy CTX_LOCK_DESTROY(ctx); 511723ac9029SStephen Hurd device_set_softc(ctx->ifc_dev, NULL); 511877c1fcecSEric Joyner iflib_free_intr_mem(ctx); 511977c1fcecSEric Joyner 512077c1fcecSEric Joyner bus_generic_detach(dev); 512177c1fcecSEric Joyner if_free(ifp); 512277c1fcecSEric Joyner 512377c1fcecSEric Joyner iflib_tx_structures_free(ctx); 512477c1fcecSEric Joyner iflib_rx_structures_free(ctx); 512577c1fcecSEric Joyner if (ctx->ifc_flags & IFC_SC_ALLOCATED) 512677c1fcecSEric Joyner free(ctx->ifc_softc, M_IFLIB); 5127f154ece0SStephen Hurd unref_ctx_core_offset(ctx); 512877c1fcecSEric Joyner STATE_LOCK_DESTROY(ctx); 512977c1fcecSEric Joyner free(ctx, M_IFLIB); 513077c1fcecSEric Joyner return (0); 513177c1fcecSEric Joyner } 513277c1fcecSEric Joyner 513377c1fcecSEric Joyner static void 513477c1fcecSEric Joyner iflib_free_intr_mem(if_ctx_t ctx) 513577c1fcecSEric Joyner { 513677c1fcecSEric Joyner 51374c7070dbSScott Long if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) { 51384c7070dbSScott Long iflib_irq_free(ctx, &ctx->ifc_legacy_irq); 51394c7070dbSScott Long } 5140b97de13aSMarius Strobl if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) { 5141b97de13aSMarius Strobl pci_release_msi(ctx->ifc_dev); 5142b97de13aSMarius Strobl } 51434c7070dbSScott Long if (ctx->ifc_msix_mem != NULL) { 51444c7070dbSScott Long bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY, 5145b97de13aSMarius Strobl rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem); 51464c7070dbSScott Long ctx->ifc_msix_mem = NULL; 51474c7070dbSScott Long } 51484c7070dbSScott Long } 51494c7070dbSScott Long 51504c7070dbSScott Long int 51514c7070dbSScott Long iflib_device_detach(device_t dev) 51524c7070dbSScott Long { 51534c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 51544c7070dbSScott Long 51554c7070dbSScott Long return (iflib_device_deregister(ctx)); 51564c7070dbSScott Long } 51574c7070dbSScott Long 51584c7070dbSScott Long int 51594c7070dbSScott Long iflib_device_suspend(device_t dev) 51604c7070dbSScott Long { 51614c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 51624c7070dbSScott Long 51634c7070dbSScott Long CTX_LOCK(ctx); 51644c7070dbSScott Long IFDI_SUSPEND(ctx); 51654c7070dbSScott Long CTX_UNLOCK(ctx); 51664c7070dbSScott Long 51674c7070dbSScott Long return bus_generic_suspend(dev); 51684c7070dbSScott Long } 51694c7070dbSScott Long int 51704c7070dbSScott Long iflib_device_shutdown(device_t dev) 51714c7070dbSScott Long { 51724c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 51734c7070dbSScott Long 51744c7070dbSScott Long CTX_LOCK(ctx); 51754c7070dbSScott Long IFDI_SHUTDOWN(ctx); 51764c7070dbSScott Long CTX_UNLOCK(ctx); 51774c7070dbSScott Long 51784c7070dbSScott Long return bus_generic_suspend(dev); 51794c7070dbSScott Long } 51804c7070dbSScott Long 51814c7070dbSScott Long 51824c7070dbSScott Long int 51834c7070dbSScott Long iflib_device_resume(device_t dev) 51844c7070dbSScott Long { 51854c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 51864c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 51874c7070dbSScott Long 51884c7070dbSScott Long CTX_LOCK(ctx); 51894c7070dbSScott Long IFDI_RESUME(ctx); 5190cd28ea92SStephen Hurd iflib_if_init_locked(ctx); 51914c7070dbSScott Long CTX_UNLOCK(ctx); 51924c7070dbSScott Long for (int i = 0; i < NTXQSETS(ctx); i++, txq++) 51934c7070dbSScott Long iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET); 51944c7070dbSScott Long 51954c7070dbSScott Long return (bus_generic_resume(dev)); 51964c7070dbSScott Long } 51974c7070dbSScott Long 51984c7070dbSScott Long int 51994c7070dbSScott Long iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params) 52004c7070dbSScott Long { 52014c7070dbSScott Long int error; 52024c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52034c7070dbSScott Long 52044c7070dbSScott Long CTX_LOCK(ctx); 52054c7070dbSScott Long error = IFDI_IOV_INIT(ctx, num_vfs, params); 52064c7070dbSScott Long CTX_UNLOCK(ctx); 52074c7070dbSScott Long 52084c7070dbSScott Long return (error); 52094c7070dbSScott Long } 52104c7070dbSScott Long 52114c7070dbSScott Long void 52124c7070dbSScott Long iflib_device_iov_uninit(device_t dev) 52134c7070dbSScott Long { 52144c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52154c7070dbSScott Long 52164c7070dbSScott Long CTX_LOCK(ctx); 52174c7070dbSScott Long IFDI_IOV_UNINIT(ctx); 52184c7070dbSScott Long CTX_UNLOCK(ctx); 52194c7070dbSScott Long } 52204c7070dbSScott Long 52214c7070dbSScott Long int 52224c7070dbSScott Long iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params) 52234c7070dbSScott Long { 52244c7070dbSScott Long int error; 52254c7070dbSScott Long if_ctx_t ctx = device_get_softc(dev); 52264c7070dbSScott Long 52274c7070dbSScott Long CTX_LOCK(ctx); 52284c7070dbSScott Long error = IFDI_IOV_VF_ADD(ctx, vfnum, params); 52294c7070dbSScott Long CTX_UNLOCK(ctx); 52304c7070dbSScott Long 52314c7070dbSScott Long return (error); 52324c7070dbSScott Long } 52334c7070dbSScott Long 52344c7070dbSScott Long /********************************************************************* 52354c7070dbSScott Long * 52364c7070dbSScott Long * MODULE FUNCTION DEFINITIONS 52374c7070dbSScott Long * 52384c7070dbSScott Long **********************************************************************/ 52394c7070dbSScott Long 5240ab2e3f79SStephen Hurd /* 5241ab2e3f79SStephen Hurd * - Start a fast taskqueue thread for each core 5242ab2e3f79SStephen Hurd * - Start a taskqueue for control operations 5243ab2e3f79SStephen Hurd */ 52444c7070dbSScott Long static int 52454c7070dbSScott Long iflib_module_init(void) 52464c7070dbSScott Long { 52474c7070dbSScott Long return (0); 52484c7070dbSScott Long } 52494c7070dbSScott Long 52504c7070dbSScott Long static int 52514c7070dbSScott Long iflib_module_event_handler(module_t mod, int what, void *arg) 52524c7070dbSScott Long { 52534c7070dbSScott Long int err; 52544c7070dbSScott Long 52554c7070dbSScott Long switch (what) { 52564c7070dbSScott Long case MOD_LOAD: 52574c7070dbSScott Long if ((err = iflib_module_init()) != 0) 52584c7070dbSScott Long return (err); 52594c7070dbSScott Long break; 52604c7070dbSScott Long case MOD_UNLOAD: 52614c7070dbSScott Long return (EBUSY); 52624c7070dbSScott Long default: 52634c7070dbSScott Long return (EOPNOTSUPP); 52644c7070dbSScott Long } 52654c7070dbSScott Long 52664c7070dbSScott Long return (0); 52674c7070dbSScott Long } 52684c7070dbSScott Long 52694c7070dbSScott Long /********************************************************************* 52704c7070dbSScott Long * 52714c7070dbSScott Long * PUBLIC FUNCTION DEFINITIONS 52724c7070dbSScott Long * ordered as in iflib.h 52734c7070dbSScott Long * 52744c7070dbSScott Long **********************************************************************/ 52754c7070dbSScott Long 52764c7070dbSScott Long 52774c7070dbSScott Long static void 52784c7070dbSScott Long _iflib_assert(if_shared_ctx_t sctx) 52794c7070dbSScott Long { 5280afb77372SEric Joyner int i; 5281afb77372SEric Joyner 52824c7070dbSScott Long MPASS(sctx->isc_tx_maxsize); 52834c7070dbSScott Long MPASS(sctx->isc_tx_maxsegsize); 52844c7070dbSScott Long 52854c7070dbSScott Long MPASS(sctx->isc_rx_maxsize); 52864c7070dbSScott Long MPASS(sctx->isc_rx_nsegments); 52874c7070dbSScott Long MPASS(sctx->isc_rx_maxsegsize); 52884c7070dbSScott Long 5289afb77372SEric Joyner MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8); 5290afb77372SEric Joyner for (i = 0; i < sctx->isc_nrxqs; i++) { 5291afb77372SEric Joyner MPASS(sctx->isc_nrxd_min[i]); 5292afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_min[i])); 5293afb77372SEric Joyner MPASS(sctx->isc_nrxd_max[i]); 5294afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_max[i])); 5295afb77372SEric Joyner MPASS(sctx->isc_nrxd_default[i]); 5296afb77372SEric Joyner MPASS(powerof2(sctx->isc_nrxd_default[i])); 5297afb77372SEric Joyner } 5298afb77372SEric Joyner 5299afb77372SEric Joyner MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8); 5300afb77372SEric Joyner for (i = 0; i < sctx->isc_ntxqs; i++) { 5301afb77372SEric Joyner MPASS(sctx->isc_ntxd_min[i]); 5302afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_min[i])); 5303afb77372SEric Joyner MPASS(sctx->isc_ntxd_max[i]); 5304afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_max[i])); 5305afb77372SEric Joyner MPASS(sctx->isc_ntxd_default[i]); 5306afb77372SEric Joyner MPASS(powerof2(sctx->isc_ntxd_default[i])); 5307afb77372SEric Joyner } 53084c7070dbSScott Long } 53094c7070dbSScott Long 53101248952aSSean Bruno static void 53111248952aSSean Bruno _iflib_pre_assert(if_softc_ctx_t scctx) 53121248952aSSean Bruno { 53131248952aSSean Bruno 53141248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_encap); 53151248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_flush); 53161248952aSSean Bruno MPASS(scctx->isc_txrx->ift_txd_credits_update); 53171248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_available); 53181248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_pkt_get); 53191248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_refill); 53201248952aSSean Bruno MPASS(scctx->isc_txrx->ift_rxd_flush); 53211248952aSSean Bruno } 53222fe66646SSean Bruno 53234c7070dbSScott Long static int 53244c7070dbSScott Long iflib_register(if_ctx_t ctx) 53254c7070dbSScott Long { 53264c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 53274c7070dbSScott Long driver_t *driver = sctx->isc_driver; 53284c7070dbSScott Long device_t dev = ctx->ifc_dev; 53294c7070dbSScott Long if_t ifp; 53304c7070dbSScott Long 53314c7070dbSScott Long _iflib_assert(sctx); 53324c7070dbSScott Long 5333aa8a24d3SStephen Hurd CTX_LOCK_INIT(ctx); 53347b610b60SSean Bruno STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev)); 533577c1fcecSEric Joyner ifp = ctx->ifc_ifp = if_alloc(IFT_ETHER); 53364c7070dbSScott Long if (ifp == NULL) { 53374c7070dbSScott Long device_printf(dev, "can not allocate ifnet structure\n"); 53384c7070dbSScott Long return (ENOMEM); 53394c7070dbSScott Long } 53404c7070dbSScott Long 53414c7070dbSScott Long /* 53424c7070dbSScott Long * Initialize our context's device specific methods 53434c7070dbSScott Long */ 53444c7070dbSScott Long kobj_init((kobj_t) ctx, (kobj_class_t) driver); 53454c7070dbSScott Long kobj_class_compile((kobj_class_t) driver); 53464c7070dbSScott Long driver->refs++; 53474c7070dbSScott Long 53484c7070dbSScott Long if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 53494c7070dbSScott Long if_setsoftc(ifp, ctx); 53504c7070dbSScott Long if_setdev(ifp, dev); 53514c7070dbSScott Long if_setinitfn(ifp, iflib_if_init); 53524c7070dbSScott Long if_setioctlfn(ifp, iflib_if_ioctl); 5353b8ca4756SPatrick Kelsey #ifdef ALTQ 5354b8ca4756SPatrick Kelsey if_setstartfn(ifp, iflib_altq_if_start); 5355b8ca4756SPatrick Kelsey if_settransmitfn(ifp, iflib_altq_if_transmit); 53568f410865SPatrick Kelsey if_setsendqready(ifp); 5357b8ca4756SPatrick Kelsey #else 53584c7070dbSScott Long if_settransmitfn(ifp, iflib_if_transmit); 5359b8ca4756SPatrick Kelsey #endif 53604c7070dbSScott Long if_setqflushfn(ifp, iflib_if_qflush); 53614c7070dbSScott Long if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 53624c7070dbSScott Long 53634c7070dbSScott Long ctx->ifc_vlan_attach_event = 53644c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx, 53654c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 53664c7070dbSScott Long ctx->ifc_vlan_detach_event = 53674c7070dbSScott Long EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx, 53684c7070dbSScott Long EVENTHANDLER_PRI_FIRST); 53694c7070dbSScott Long 5370e2621d96SMatt Macy if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) { 5371e2621d96SMatt Macy ctx->ifc_mediap = &ctx->ifc_media; 5372e2621d96SMatt Macy ifmedia_init(ctx->ifc_mediap, IFM_IMASK, 53734c7070dbSScott Long iflib_media_change, iflib_media_status); 5374e2621d96SMatt Macy } 53754c7070dbSScott Long return (0); 53764c7070dbSScott Long } 53774c7070dbSScott Long 53784c7070dbSScott Long static int 53794c7070dbSScott Long iflib_queues_alloc(if_ctx_t ctx) 53804c7070dbSScott Long { 53814c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 538223ac9029SStephen Hurd if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 53834c7070dbSScott Long device_t dev = ctx->ifc_dev; 538423ac9029SStephen Hurd int nrxqsets = scctx->isc_nrxqsets; 538523ac9029SStephen Hurd int ntxqsets = scctx->isc_ntxqsets; 53864c7070dbSScott Long iflib_txq_t txq; 53874c7070dbSScott Long iflib_rxq_t rxq; 53884c7070dbSScott Long iflib_fl_t fl = NULL; 538923ac9029SStephen Hurd int i, j, cpu, err, txconf, rxconf; 53904c7070dbSScott Long iflib_dma_info_t ifdip; 539123ac9029SStephen Hurd uint32_t *rxqsizes = scctx->isc_rxqsizes; 539223ac9029SStephen Hurd uint32_t *txqsizes = scctx->isc_txqsizes; 53934c7070dbSScott Long uint8_t nrxqs = sctx->isc_nrxqs; 53944c7070dbSScott Long uint8_t ntxqs = sctx->isc_ntxqs; 53954c7070dbSScott Long int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1; 53964c7070dbSScott Long caddr_t *vaddrs; 53974c7070dbSScott Long uint64_t *paddrs; 53984c7070dbSScott Long 539923ac9029SStephen Hurd KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1")); 540023ac9029SStephen Hurd KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1")); 54014c7070dbSScott Long 54024c7070dbSScott Long /* Allocate the TX ring struct memory */ 5403b89827a0SStephen Hurd if (!(ctx->ifc_txqs = 5404ac2fffa4SPedro F. Giffuni (iflib_txq_t) malloc(sizeof(struct iflib_txq) * 5405ac2fffa4SPedro F. Giffuni ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 54064c7070dbSScott Long device_printf(dev, "Unable to allocate TX ring memory\n"); 54074c7070dbSScott Long err = ENOMEM; 54084c7070dbSScott Long goto fail; 54094c7070dbSScott Long } 54104c7070dbSScott Long 54114c7070dbSScott Long /* Now allocate the RX */ 5412b89827a0SStephen Hurd if (!(ctx->ifc_rxqs = 5413ac2fffa4SPedro F. Giffuni (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) * 5414ac2fffa4SPedro F. Giffuni nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) { 54154c7070dbSScott Long device_printf(dev, "Unable to allocate RX ring memory\n"); 54164c7070dbSScott Long err = ENOMEM; 54174c7070dbSScott Long goto rx_fail; 54184c7070dbSScott Long } 54194c7070dbSScott Long 5420b89827a0SStephen Hurd txq = ctx->ifc_txqs; 5421b89827a0SStephen Hurd rxq = ctx->ifc_rxqs; 54224c7070dbSScott Long 54234c7070dbSScott Long /* 54244c7070dbSScott Long * XXX handle allocation failure 54254c7070dbSScott Long */ 542696c85efbSNathan Whitehorn for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) { 54274c7070dbSScott Long /* Set up some basics */ 54284c7070dbSScott Long 5429bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs, 5430bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5431bfce461eSMarius Strobl device_printf(dev, 5432bfce461eSMarius Strobl "Unable to allocate TX DMA info memory\n"); 54334c7070dbSScott Long err = ENOMEM; 54340d0338afSConrad Meyer goto err_tx_desc; 54354c7070dbSScott Long } 54364c7070dbSScott Long txq->ift_ifdi = ifdip; 54374c7070dbSScott Long for (j = 0; j < ntxqs; j++, ifdip++) { 5438bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) { 5439bfce461eSMarius Strobl device_printf(dev, 5440bfce461eSMarius Strobl "Unable to allocate TX descriptors\n"); 54414c7070dbSScott Long err = ENOMEM; 54424c7070dbSScott Long goto err_tx_desc; 54434c7070dbSScott Long } 544495246abbSSean Bruno txq->ift_txd_size[j] = scctx->isc_txd_size[j]; 54454c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, txqsizes[j]); 54464c7070dbSScott Long } 54474c7070dbSScott Long txq->ift_ctx = ctx; 54484c7070dbSScott Long txq->ift_id = i; 544923ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_TXCQ) { 545023ac9029SStephen Hurd txq->ift_br_offset = 1; 545123ac9029SStephen Hurd } else { 545223ac9029SStephen Hurd txq->ift_br_offset = 0; 545323ac9029SStephen Hurd } 54544c7070dbSScott Long /* XXX fix this */ 545596c85efbSNathan Whitehorn txq->ift_timer.c_cpu = cpu; 54564c7070dbSScott Long 54574c7070dbSScott Long if (iflib_txsd_alloc(txq)) { 54584c7070dbSScott Long device_printf(dev, "Critical Failure setting up TX buffers\n"); 54594c7070dbSScott Long err = ENOMEM; 54604c7070dbSScott Long goto err_tx_desc; 54614c7070dbSScott Long } 54624c7070dbSScott Long 54634c7070dbSScott Long /* Initialize the TX lock */ 54641722eeacSMarius Strobl snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout", 54654c7070dbSScott Long device_get_nameunit(dev), txq->ift_id); 54664c7070dbSScott Long mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF); 54674c7070dbSScott Long callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0); 54684c7070dbSScott Long 546995246abbSSean Bruno err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain, 54704c7070dbSScott Long iflib_txq_can_drain, M_IFLIB, M_WAITOK); 54714c7070dbSScott Long if (err) { 54724c7070dbSScott Long /* XXX free any allocated rings */ 54734c7070dbSScott Long device_printf(dev, "Unable to allocate buf_ring\n"); 54740d0338afSConrad Meyer goto err_tx_desc; 54754c7070dbSScott Long } 54764c7070dbSScott Long } 54774c7070dbSScott Long 54784c7070dbSScott Long for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) { 54794c7070dbSScott Long /* Set up some basics */ 54804c7070dbSScott Long 5481bfce461eSMarius Strobl if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs, 5482bfce461eSMarius Strobl M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) { 5483bfce461eSMarius Strobl device_printf(dev, 5484bfce461eSMarius Strobl "Unable to allocate RX DMA info memory\n"); 54854c7070dbSScott Long err = ENOMEM; 54860d0338afSConrad Meyer goto err_tx_desc; 54874c7070dbSScott Long } 54884c7070dbSScott Long 54894c7070dbSScott Long rxq->ifr_ifdi = ifdip; 549095246abbSSean Bruno /* XXX this needs to be changed if #rx queues != #tx queues */ 549195246abbSSean Bruno rxq->ifr_ntxqirq = 1; 549295246abbSSean Bruno rxq->ifr_txqid[0] = i; 54934c7070dbSScott Long for (j = 0; j < nrxqs; j++, ifdip++) { 5494bfce461eSMarius Strobl if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) { 5495bfce461eSMarius Strobl device_printf(dev, 5496bfce461eSMarius Strobl "Unable to allocate RX descriptors\n"); 54974c7070dbSScott Long err = ENOMEM; 54984c7070dbSScott Long goto err_tx_desc; 54994c7070dbSScott Long } 55004c7070dbSScott Long bzero((void *)ifdip->idi_vaddr, rxqsizes[j]); 55014c7070dbSScott Long } 55024c7070dbSScott Long rxq->ifr_ctx = ctx; 55034c7070dbSScott Long rxq->ifr_id = i; 550423ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 550523ac9029SStephen Hurd rxq->ifr_fl_offset = 1; 55064c7070dbSScott Long } else { 550723ac9029SStephen Hurd rxq->ifr_fl_offset = 0; 55084c7070dbSScott Long } 55094c7070dbSScott Long rxq->ifr_nfl = nfree_lists; 55104c7070dbSScott Long if (!(fl = 5511ac2fffa4SPedro F. Giffuni (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) { 55124c7070dbSScott Long device_printf(dev, "Unable to allocate free list memory\n"); 55134c7070dbSScott Long err = ENOMEM; 55140d0338afSConrad Meyer goto err_tx_desc; 55154c7070dbSScott Long } 55164c7070dbSScott Long rxq->ifr_fl = fl; 55174c7070dbSScott Long for (j = 0; j < nfree_lists; j++) { 551895246abbSSean Bruno fl[j].ifl_rxq = rxq; 551995246abbSSean Bruno fl[j].ifl_id = j; 552095246abbSSean Bruno fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset]; 552195246abbSSean Bruno fl[j].ifl_rxd_size = scctx->isc_rxd_size[j]; 55224c7070dbSScott Long } 55234c7070dbSScott Long /* Allocate receive buffers for the ring */ 55244c7070dbSScott Long if (iflib_rxsd_alloc(rxq)) { 55254c7070dbSScott Long device_printf(dev, 55264c7070dbSScott Long "Critical Failure setting up receive buffers\n"); 55274c7070dbSScott Long err = ENOMEM; 55284c7070dbSScott Long goto err_rx_desc; 55294c7070dbSScott Long } 553087890dbaSSean Bruno 553187890dbaSSean Bruno for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) 55323db348b5SMarius Strobl fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB, 55333db348b5SMarius Strobl M_WAITOK); 55344c7070dbSScott Long } 55354c7070dbSScott Long 55364c7070dbSScott Long /* TXQs */ 55374c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 55384c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK); 55394c7070dbSScott Long for (i = 0; i < ntxqsets; i++) { 55404c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi; 55414c7070dbSScott Long 55424c7070dbSScott Long for (j = 0; j < ntxqs; j++, di++) { 55434c7070dbSScott Long vaddrs[i*ntxqs + j] = di->idi_vaddr; 55444c7070dbSScott Long paddrs[i*ntxqs + j] = di->idi_paddr; 55454c7070dbSScott Long } 55464c7070dbSScott Long } 55474c7070dbSScott Long if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) { 5548bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5549bfce461eSMarius Strobl "Unable to allocate device TX queue\n"); 55504c7070dbSScott Long iflib_tx_structures_free(ctx); 55514c7070dbSScott Long free(vaddrs, M_IFLIB); 55524c7070dbSScott Long free(paddrs, M_IFLIB); 55534c7070dbSScott Long goto err_rx_desc; 55544c7070dbSScott Long } 55554c7070dbSScott Long free(vaddrs, M_IFLIB); 55564c7070dbSScott Long free(paddrs, M_IFLIB); 55574c7070dbSScott Long 55584c7070dbSScott Long /* RXQs */ 55594c7070dbSScott Long vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 55604c7070dbSScott Long paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK); 55614c7070dbSScott Long for (i = 0; i < nrxqsets; i++) { 55624c7070dbSScott Long iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi; 55634c7070dbSScott Long 55644c7070dbSScott Long for (j = 0; j < nrxqs; j++, di++) { 55654c7070dbSScott Long vaddrs[i*nrxqs + j] = di->idi_vaddr; 55664c7070dbSScott Long paddrs[i*nrxqs + j] = di->idi_paddr; 55674c7070dbSScott Long } 55684c7070dbSScott Long } 55694c7070dbSScott Long if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) { 5570bfce461eSMarius Strobl device_printf(ctx->ifc_dev, 5571bfce461eSMarius Strobl "Unable to allocate device RX queue\n"); 55724c7070dbSScott Long iflib_tx_structures_free(ctx); 55734c7070dbSScott Long free(vaddrs, M_IFLIB); 55744c7070dbSScott Long free(paddrs, M_IFLIB); 55754c7070dbSScott Long goto err_rx_desc; 55764c7070dbSScott Long } 55774c7070dbSScott Long free(vaddrs, M_IFLIB); 55784c7070dbSScott Long free(paddrs, M_IFLIB); 55794c7070dbSScott Long 55804c7070dbSScott Long return (0); 55814c7070dbSScott Long 55824c7070dbSScott Long /* XXX handle allocation failure changes */ 55834c7070dbSScott Long err_rx_desc: 55844c7070dbSScott Long err_tx_desc: 5585b89827a0SStephen Hurd rx_fail: 55864c7070dbSScott Long if (ctx->ifc_rxqs != NULL) 55874c7070dbSScott Long free(ctx->ifc_rxqs, M_IFLIB); 55884c7070dbSScott Long ctx->ifc_rxqs = NULL; 55894c7070dbSScott Long if (ctx->ifc_txqs != NULL) 55904c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 55914c7070dbSScott Long ctx->ifc_txqs = NULL; 55924c7070dbSScott Long fail: 55934c7070dbSScott Long return (err); 55944c7070dbSScott Long } 55954c7070dbSScott Long 55964c7070dbSScott Long static int 55974c7070dbSScott Long iflib_tx_structures_setup(if_ctx_t ctx) 55984c7070dbSScott Long { 55994c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 56004c7070dbSScott Long int i; 56014c7070dbSScott Long 56024c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) 56034c7070dbSScott Long iflib_txq_setup(txq); 56044c7070dbSScott Long 56054c7070dbSScott Long return (0); 56064c7070dbSScott Long } 56074c7070dbSScott Long 56084c7070dbSScott Long static void 56094c7070dbSScott Long iflib_tx_structures_free(if_ctx_t ctx) 56104c7070dbSScott Long { 56114c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 56124d261ce2SStephen Hurd if_shared_ctx_t sctx = ctx->ifc_sctx; 56134c7070dbSScott Long int i, j; 56144c7070dbSScott Long 56154c7070dbSScott Long for (i = 0; i < NTXQSETS(ctx); i++, txq++) { 56164c7070dbSScott Long iflib_txq_destroy(txq); 56174d261ce2SStephen Hurd for (j = 0; j < sctx->isc_ntxqs; j++) 56184c7070dbSScott Long iflib_dma_free(&txq->ift_ifdi[j]); 56194c7070dbSScott Long } 56204c7070dbSScott Long free(ctx->ifc_txqs, M_IFLIB); 56214c7070dbSScott Long ctx->ifc_txqs = NULL; 56224c7070dbSScott Long IFDI_QUEUES_FREE(ctx); 56234c7070dbSScott Long } 56244c7070dbSScott Long 56254c7070dbSScott Long /********************************************************************* 56264c7070dbSScott Long * 56274c7070dbSScott Long * Initialize all receive rings. 56284c7070dbSScott Long * 56294c7070dbSScott Long **********************************************************************/ 56304c7070dbSScott Long static int 56314c7070dbSScott Long iflib_rx_structures_setup(if_ctx_t ctx) 56324c7070dbSScott Long { 56334c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 5634aaeb188aSBjoern A. Zeeb int q; 5635aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 56363d10e9edSMarius Strobl int err, i; 5637aaeb188aSBjoern A. Zeeb #endif 56384c7070dbSScott Long 56394c7070dbSScott Long for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) { 5640aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 56413d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) { 56423d10e9edSMarius Strobl err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp, 564323ac9029SStephen Hurd TCP_LRO_ENTRIES, min(1024, 56443d10e9edSMarius Strobl ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset])); 56453d10e9edSMarius Strobl if (err != 0) { 56463d10e9edSMarius Strobl device_printf(ctx->ifc_dev, 56473d10e9edSMarius Strobl "LRO Initialization failed!\n"); 56484c7070dbSScott Long goto fail; 56494c7070dbSScott Long } 56503d10e9edSMarius Strobl } 5651aaeb188aSBjoern A. Zeeb #endif 56524c7070dbSScott Long IFDI_RXQ_SETUP(ctx, rxq->ifr_id); 56534c7070dbSScott Long } 56544c7070dbSScott Long return (0); 5655aaeb188aSBjoern A. Zeeb #if defined(INET6) || defined(INET) 56564c7070dbSScott Long fail: 56574c7070dbSScott Long /* 56583d10e9edSMarius Strobl * Free LRO resources allocated so far, we will only handle 56594c7070dbSScott Long * the rings that completed, the failing case will have 56604c7070dbSScott Long * cleaned up for itself. 'q' failed, so its the terminus. 56614c7070dbSScott Long */ 56624c7070dbSScott Long rxq = ctx->ifc_rxqs; 56634c7070dbSScott Long for (i = 0; i < q; ++i, rxq++) { 56643d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) 56653d10e9edSMarius Strobl tcp_lro_free(&rxq->ifr_lc); 56664c7070dbSScott Long } 56674c7070dbSScott Long return (err); 5668aaeb188aSBjoern A. Zeeb #endif 56694c7070dbSScott Long } 56704c7070dbSScott Long 56714c7070dbSScott Long /********************************************************************* 56724c7070dbSScott Long * 56734c7070dbSScott Long * Free all receive rings. 56744c7070dbSScott Long * 56754c7070dbSScott Long **********************************************************************/ 56764c7070dbSScott Long static void 56774c7070dbSScott Long iflib_rx_structures_free(if_ctx_t ctx) 56784c7070dbSScott Long { 56794c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 56803d10e9edSMarius Strobl int i; 56814c7070dbSScott Long 56823d10e9edSMarius Strobl for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) { 56834c7070dbSScott Long iflib_rx_sds_free(rxq); 5684007b804fSMarius Strobl #if defined(INET6) || defined(INET) 56853d10e9edSMarius Strobl if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_LRO) 56863d10e9edSMarius Strobl tcp_lro_free(&rxq->ifr_lc); 5687007b804fSMarius Strobl #endif 56884c7070dbSScott Long } 568977c1fcecSEric Joyner free(ctx->ifc_rxqs, M_IFLIB); 569077c1fcecSEric Joyner ctx->ifc_rxqs = NULL; 56914c7070dbSScott Long } 56924c7070dbSScott Long 56934c7070dbSScott Long static int 56944c7070dbSScott Long iflib_qset_structures_setup(if_ctx_t ctx) 56954c7070dbSScott Long { 56964c7070dbSScott Long int err; 56974c7070dbSScott Long 56986108c013SStephen Hurd /* 56996108c013SStephen Hurd * It is expected that the caller takes care of freeing queues if this 57006108c013SStephen Hurd * fails. 57016108c013SStephen Hurd */ 5702ac88e6daSStephen Hurd if ((err = iflib_tx_structures_setup(ctx)) != 0) { 5703ac88e6daSStephen Hurd device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err); 57044c7070dbSScott Long return (err); 5705ac88e6daSStephen Hurd } 57064c7070dbSScott Long 57076108c013SStephen Hurd if ((err = iflib_rx_structures_setup(ctx)) != 0) 57084c7070dbSScott Long device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err); 57096108c013SStephen Hurd 57104c7070dbSScott Long return (err); 57114c7070dbSScott Long } 57124c7070dbSScott Long 57134c7070dbSScott Long int 57144c7070dbSScott Long iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid, 57153e0e6330SStephen Hurd driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name) 57164c7070dbSScott Long { 57174c7070dbSScott Long 57184c7070dbSScott Long return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name)); 57194c7070dbSScott Long } 57204c7070dbSScott Long 5721b103855eSStephen Hurd #ifdef SMP 5722aa3c5dd8SSean Bruno static int 5723b103855eSStephen Hurd find_nth(if_ctx_t ctx, int qid) 57244c7070dbSScott Long { 5725b103855eSStephen Hurd cpuset_t cpus; 5726aa3c5dd8SSean Bruno int i, cpuid, eqid, count; 57274c7070dbSScott Long 5728b103855eSStephen Hurd CPU_COPY(&ctx->ifc_cpus, &cpus); 5729b103855eSStephen Hurd count = CPU_COUNT(&cpus); 5730aa3c5dd8SSean Bruno eqid = qid % count; 57314c7070dbSScott Long /* clear up to the qid'th bit */ 5732aa3c5dd8SSean Bruno for (i = 0; i < eqid; i++) { 5733b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5734aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5735b103855eSStephen Hurd CPU_CLR(cpuid-1, &cpus); 57364c7070dbSScott Long } 5737b103855eSStephen Hurd cpuid = CPU_FFS(&cpus); 5738aa3c5dd8SSean Bruno MPASS(cpuid != 0); 5739aa3c5dd8SSean Bruno return (cpuid-1); 57404c7070dbSScott Long } 57414c7070dbSScott Long 5742b103855eSStephen Hurd #ifdef SCHED_ULE 5743b103855eSStephen Hurd extern struct cpu_group *cpu_top; /* CPU topology */ 5744b103855eSStephen Hurd 5745b103855eSStephen Hurd static int 5746b103855eSStephen Hurd find_child_with_core(int cpu, struct cpu_group *grp) 5747b103855eSStephen Hurd { 5748b103855eSStephen Hurd int i; 5749b103855eSStephen Hurd 5750b103855eSStephen Hurd if (grp->cg_children == 0) 5751b103855eSStephen Hurd return -1; 5752b103855eSStephen Hurd 5753b103855eSStephen Hurd MPASS(grp->cg_child); 5754b103855eSStephen Hurd for (i = 0; i < grp->cg_children; i++) { 5755b103855eSStephen Hurd if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask)) 5756b103855eSStephen Hurd return i; 5757b103855eSStephen Hurd } 5758b103855eSStephen Hurd 5759b103855eSStephen Hurd return -1; 5760b103855eSStephen Hurd } 5761b103855eSStephen Hurd 5762b103855eSStephen Hurd /* 57630b75ac77SStephen Hurd * Find the nth "close" core to the specified core 57640b75ac77SStephen Hurd * "close" is defined as the deepest level that shares 57650b75ac77SStephen Hurd * at least an L2 cache. With threads, this will be 5766f154ece0SStephen Hurd * threads on the same core. If the shared cache is L3 57670b75ac77SStephen Hurd * or higher, simply returns the same core. 5768b103855eSStephen Hurd */ 5769b103855eSStephen Hurd static int 57700b75ac77SStephen Hurd find_close_core(int cpu, int core_offset) 5771b103855eSStephen Hurd { 5772b103855eSStephen Hurd struct cpu_group *grp; 5773b103855eSStephen Hurd int i; 57740b75ac77SStephen Hurd int fcpu; 5775b103855eSStephen Hurd cpuset_t cs; 5776b103855eSStephen Hurd 5777b103855eSStephen Hurd grp = cpu_top; 5778b103855eSStephen Hurd if (grp == NULL) 5779b103855eSStephen Hurd return cpu; 5780b103855eSStephen Hurd i = 0; 5781b103855eSStephen Hurd while ((i = find_child_with_core(cpu, grp)) != -1) { 5782b103855eSStephen Hurd /* If the child only has one cpu, don't descend */ 5783b103855eSStephen Hurd if (grp->cg_child[i].cg_count <= 1) 5784b103855eSStephen Hurd break; 5785b103855eSStephen Hurd grp = &grp->cg_child[i]; 5786b103855eSStephen Hurd } 5787b103855eSStephen Hurd 5788b103855eSStephen Hurd /* If they don't share at least an L2 cache, use the same CPU */ 5789b103855eSStephen Hurd if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE) 5790b103855eSStephen Hurd return cpu; 5791b103855eSStephen Hurd 5792b103855eSStephen Hurd /* Now pick one */ 5793b103855eSStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 57940b75ac77SStephen Hurd 57950b75ac77SStephen Hurd /* Add the selected CPU offset to core offset. */ 57960b75ac77SStephen Hurd for (i = 0; (fcpu = CPU_FFS(&cs)) != 0; i++) { 57970b75ac77SStephen Hurd if (fcpu - 1 == cpu) 57980b75ac77SStephen Hurd break; 57990b75ac77SStephen Hurd CPU_CLR(fcpu - 1, &cs); 58000b75ac77SStephen Hurd } 58010b75ac77SStephen Hurd MPASS(fcpu); 58020b75ac77SStephen Hurd 58030b75ac77SStephen Hurd core_offset += i; 58040b75ac77SStephen Hurd 58050b75ac77SStephen Hurd CPU_COPY(&grp->cg_mask, &cs); 58060b75ac77SStephen Hurd for (i = core_offset % grp->cg_count; i > 0; i--) { 5807b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5808b103855eSStephen Hurd CPU_CLR(CPU_FFS(&cs) - 1, &cs); 5809b103855eSStephen Hurd } 5810b103855eSStephen Hurd MPASS(CPU_FFS(&cs)); 5811b103855eSStephen Hurd return CPU_FFS(&cs) - 1; 5812b103855eSStephen Hurd } 5813b103855eSStephen Hurd #else 5814b103855eSStephen Hurd static int 58150b75ac77SStephen Hurd find_close_core(int cpu, int core_offset __unused) 5816b103855eSStephen Hurd { 581797755e83SKonstantin Belousov return cpu; 5818b103855eSStephen Hurd } 5819b103855eSStephen Hurd #endif 5820b103855eSStephen Hurd 5821b103855eSStephen Hurd static int 58220b75ac77SStephen Hurd get_core_offset(if_ctx_t ctx, iflib_intr_type_t type, int qid) 5823b103855eSStephen Hurd { 5824b103855eSStephen Hurd switch (type) { 5825b103855eSStephen Hurd case IFLIB_INTR_TX: 58260b75ac77SStephen Hurd /* TX queues get cores which share at least an L2 cache with the corresponding RX queue */ 58270b75ac77SStephen Hurd /* XXX handle multiple RX threads per core and more than two core per L2 group */ 5828b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus) + 1; 5829b103855eSStephen Hurd case IFLIB_INTR_RX: 5830b103855eSStephen Hurd case IFLIB_INTR_RXTX: 58310b75ac77SStephen Hurd /* RX queues get the specified core */ 5832b103855eSStephen Hurd return qid / CPU_COUNT(&ctx->ifc_cpus); 5833b103855eSStephen Hurd default: 5834b103855eSStephen Hurd return -1; 5835b103855eSStephen Hurd } 5836b103855eSStephen Hurd } 5837b103855eSStephen Hurd #else 58380b75ac77SStephen Hurd #define get_core_offset(ctx, type, qid) CPU_FIRST() 58390b75ac77SStephen Hurd #define find_close_core(cpuid, tid) CPU_FIRST() 5840b103855eSStephen Hurd #define find_nth(ctx, gid) CPU_FIRST() 5841b103855eSStephen Hurd #endif 5842b103855eSStephen Hurd 5843b103855eSStephen Hurd /* Just to avoid copy/paste */ 5844b103855eSStephen Hurd static inline int 5845f855ec81SMarius Strobl iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type, 5846f855ec81SMarius Strobl int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq, 5847f855ec81SMarius Strobl const char *name) 5848b103855eSStephen Hurd { 5849f855ec81SMarius Strobl device_t dev; 5850f154ece0SStephen Hurd int co, cpuid, err, tid; 5851b103855eSStephen Hurd 5852f855ec81SMarius Strobl dev = ctx->ifc_dev; 5853f154ece0SStephen Hurd co = ctx->ifc_sysctl_core_offset; 5854f154ece0SStephen Hurd if (ctx->ifc_sysctl_separate_txrx && type == IFLIB_INTR_TX) 5855f154ece0SStephen Hurd co += ctx->ifc_softc_ctx.isc_nrxqsets; 5856f154ece0SStephen Hurd cpuid = find_nth(ctx, qid + co); 58570b75ac77SStephen Hurd tid = get_core_offset(ctx, type, qid); 58583d10e9edSMarius Strobl if (tid < 0) { 58593d10e9edSMarius Strobl device_printf(dev, "get_core_offset failed\n"); 58603d10e9edSMarius Strobl return (EOPNOTSUPP); 58613d10e9edSMarius Strobl } 58620b75ac77SStephen Hurd cpuid = find_close_core(cpuid, tid); 5863f855ec81SMarius Strobl err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev, irq->ii_res, 5864f855ec81SMarius Strobl name); 5865b103855eSStephen Hurd if (err) { 5866f855ec81SMarius Strobl device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err); 5867b103855eSStephen Hurd return (err); 5868b103855eSStephen Hurd } 5869b103855eSStephen Hurd #ifdef notyet 5870b103855eSStephen Hurd if (cpuid > ctx->ifc_cpuid_highest) 5871b103855eSStephen Hurd ctx->ifc_cpuid_highest = cpuid; 5872b103855eSStephen Hurd #endif 58733d10e9edSMarius Strobl return (0); 5874b103855eSStephen Hurd } 5875b103855eSStephen Hurd 58764c7070dbSScott Long int 58774c7070dbSScott Long iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid, 58784c7070dbSScott Long iflib_intr_type_t type, driver_filter_t *filter, 58793e0e6330SStephen Hurd void *filter_arg, int qid, const char *name) 58804c7070dbSScott Long { 5881f855ec81SMarius Strobl device_t dev; 58824c7070dbSScott Long struct grouptask *gtask; 58834c7070dbSScott Long struct taskqgroup *tqg; 58844c7070dbSScott Long iflib_filter_info_t info; 588523ac9029SStephen Hurd gtask_fn_t *fn; 5886b103855eSStephen Hurd int tqrid, err; 588795246abbSSean Bruno driver_filter_t *intr_fast; 58884c7070dbSScott Long void *q; 58894c7070dbSScott Long 58904c7070dbSScott Long info = &ctx->ifc_filter_info; 5891add6f7d0SSean Bruno tqrid = rid; 58924c7070dbSScott Long 58934c7070dbSScott Long switch (type) { 58944c7070dbSScott Long /* XXX merge tx/rx for netmap? */ 58954c7070dbSScott Long case IFLIB_INTR_TX: 58964c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 58974c7070dbSScott Long info = &ctx->ifc_txqs[qid].ift_filter_info; 58984c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 5899ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 59004c7070dbSScott Long fn = _task_fn_tx; 590195246abbSSean Bruno intr_fast = iflib_fast_intr; 5902da69b8f9SSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 59035ee36c68SStephen Hurd ctx->ifc_flags |= IFC_NETMAP_TX_IRQ; 59044c7070dbSScott Long break; 59054c7070dbSScott Long case IFLIB_INTR_RX: 59064c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 59074c7070dbSScott Long info = &ctx->ifc_rxqs[qid].ifr_filter_info; 59084c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 5909ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 59104c7070dbSScott Long fn = _task_fn_rx; 5911ab2e3f79SStephen Hurd intr_fast = iflib_fast_intr; 591295246abbSSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 591395246abbSSean Bruno break; 591495246abbSSean Bruno case IFLIB_INTR_RXTX: 591595246abbSSean Bruno q = &ctx->ifc_rxqs[qid]; 591695246abbSSean Bruno info = &ctx->ifc_rxqs[qid].ifr_filter_info; 591795246abbSSean Bruno gtask = &ctx->ifc_rxqs[qid].ifr_task; 5918ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 591995246abbSSean Bruno fn = _task_fn_rx; 592095246abbSSean Bruno intr_fast = iflib_fast_intr_rxtx; 5921da69b8f9SSean Bruno GROUPTASK_INIT(gtask, 0, fn, q); 59224c7070dbSScott Long break; 59234c7070dbSScott Long case IFLIB_INTR_ADMIN: 59244c7070dbSScott Long q = ctx; 5925da69b8f9SSean Bruno tqrid = -1; 59264c7070dbSScott Long info = &ctx->ifc_filter_info; 59274c7070dbSScott Long gtask = &ctx->ifc_admin_task; 5928ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 59294c7070dbSScott Long fn = _task_fn_admin; 593095246abbSSean Bruno intr_fast = iflib_fast_intr_ctx; 59314c7070dbSScott Long break; 59324c7070dbSScott Long default: 59333d10e9edSMarius Strobl device_printf(ctx->ifc_dev, "%s: unknown net intr type\n", 59343d10e9edSMarius Strobl __func__); 59353d10e9edSMarius Strobl return (EINVAL); 59364c7070dbSScott Long } 59374c7070dbSScott Long 59384c7070dbSScott Long info->ifi_filter = filter; 59394c7070dbSScott Long info->ifi_filter_arg = filter_arg; 59404c7070dbSScott Long info->ifi_task = gtask; 594195246abbSSean Bruno info->ifi_ctx = q; 59424c7070dbSScott Long 5943f855ec81SMarius Strobl dev = ctx->ifc_dev; 594495246abbSSean Bruno err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name); 5945da69b8f9SSean Bruno if (err != 0) { 5946f855ec81SMarius Strobl device_printf(dev, "_iflib_irq_alloc failed %d\n", err); 59474c7070dbSScott Long return (err); 5948da69b8f9SSean Bruno } 5949da69b8f9SSean Bruno if (type == IFLIB_INTR_ADMIN) 5950da69b8f9SSean Bruno return (0); 5951da69b8f9SSean Bruno 59524c7070dbSScott Long if (tqrid != -1) { 5953f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 5954f855ec81SMarius Strobl q, name); 5955b103855eSStephen Hurd if (err) 5956b103855eSStephen Hurd return (err); 5957aa3c5dd8SSean Bruno } else { 5958f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name); 5959aa3c5dd8SSean Bruno } 59604c7070dbSScott Long 59614c7070dbSScott Long return (0); 59624c7070dbSScott Long } 59634c7070dbSScott Long 59644c7070dbSScott Long void 59653e0e6330SStephen 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) 59664c7070dbSScott Long { 59674c7070dbSScott Long struct grouptask *gtask; 59684c7070dbSScott Long struct taskqgroup *tqg; 596923ac9029SStephen Hurd gtask_fn_t *fn; 59704c7070dbSScott Long void *q; 5971b103855eSStephen Hurd int err; 59724c7070dbSScott Long 59734c7070dbSScott Long switch (type) { 59744c7070dbSScott Long case IFLIB_INTR_TX: 59754c7070dbSScott Long q = &ctx->ifc_txqs[qid]; 59764c7070dbSScott Long gtask = &ctx->ifc_txqs[qid].ift_task; 5977ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 59784c7070dbSScott Long fn = _task_fn_tx; 59794c7070dbSScott Long break; 59804c7070dbSScott Long case IFLIB_INTR_RX: 59814c7070dbSScott Long q = &ctx->ifc_rxqs[qid]; 59824c7070dbSScott Long gtask = &ctx->ifc_rxqs[qid].ifr_task; 5983ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 59844c7070dbSScott Long fn = _task_fn_rx; 59854c7070dbSScott Long break; 59864c7070dbSScott Long case IFLIB_INTR_IOV: 59874c7070dbSScott Long q = ctx; 59884c7070dbSScott Long gtask = &ctx->ifc_vflr_task; 5989ab2e3f79SStephen Hurd tqg = qgroup_if_config_tqg; 59904c7070dbSScott Long fn = _task_fn_iov; 59914c7070dbSScott Long break; 59924c7070dbSScott Long default: 59934c7070dbSScott Long panic("unknown net intr type"); 59944c7070dbSScott Long } 59954c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, q); 5996f855ec81SMarius Strobl if (irq != NULL) { 5997f855ec81SMarius Strobl err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, 5998f855ec81SMarius Strobl q, name); 5999b103855eSStephen Hurd if (err) 6000f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, ctx->ifc_dev, 6001f855ec81SMarius Strobl irq->ii_res, name); 6002f855ec81SMarius Strobl } else { 6003f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, NULL, NULL, name); 6004b103855eSStephen Hurd } 6005b103855eSStephen Hurd } 60064c7070dbSScott Long 60074c7070dbSScott Long void 60084c7070dbSScott Long iflib_irq_free(if_ctx_t ctx, if_irq_t irq) 60094c7070dbSScott Long { 6010b97de13aSMarius Strobl 60114c7070dbSScott Long if (irq->ii_tag) 60124c7070dbSScott Long bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag); 60134c7070dbSScott Long 60144c7070dbSScott Long if (irq->ii_res) 6015b97de13aSMarius Strobl bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ, 6016b97de13aSMarius Strobl rman_get_rid(irq->ii_res), irq->ii_res); 60174c7070dbSScott Long } 60184c7070dbSScott Long 60194c7070dbSScott Long static int 60203e0e6330SStephen Hurd iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name) 60214c7070dbSScott Long { 60224c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 60234c7070dbSScott Long iflib_rxq_t rxq = ctx->ifc_rxqs; 60244c7070dbSScott Long if_irq_t irq = &ctx->ifc_legacy_irq; 60254c7070dbSScott Long iflib_filter_info_t info; 6026f855ec81SMarius Strobl device_t dev; 60274c7070dbSScott Long struct grouptask *gtask; 6028f855ec81SMarius Strobl struct resource *res; 60294c7070dbSScott Long struct taskqgroup *tqg; 603023ac9029SStephen Hurd gtask_fn_t *fn; 60314c7070dbSScott Long void *q; 6032d49e83eaSMarius Strobl int err, tqrid; 60334c7070dbSScott Long 60344c7070dbSScott Long q = &ctx->ifc_rxqs[0]; 60354c7070dbSScott Long info = &rxq[0].ifr_filter_info; 60364c7070dbSScott Long gtask = &rxq[0].ifr_task; 6037ab2e3f79SStephen Hurd tqg = qgroup_if_io_tqg; 6038d49e83eaSMarius Strobl tqrid = *rid; 60394c7070dbSScott Long fn = _task_fn_rx; 60404c7070dbSScott Long 60414c7070dbSScott Long ctx->ifc_flags |= IFC_LEGACY; 60424c7070dbSScott Long info->ifi_filter = filter; 60434c7070dbSScott Long info->ifi_filter_arg = filter_arg; 60444c7070dbSScott Long info->ifi_task = gtask; 60453d10e9edSMarius Strobl info->ifi_ctx = q; 60464c7070dbSScott Long 6047f855ec81SMarius Strobl dev = ctx->ifc_dev; 60484c7070dbSScott Long /* We allocate a single interrupt resource */ 60493d10e9edSMarius Strobl if ((err = _iflib_irq_alloc(ctx, irq, tqrid, iflib_fast_intr_rxtx, 60503d10e9edSMarius Strobl NULL, info, name)) != 0) 60514c7070dbSScott Long return (err); 60524c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, q); 6053f855ec81SMarius Strobl res = irq->ii_res; 6054f855ec81SMarius Strobl taskqgroup_attach(tqg, gtask, q, dev, res, name); 60554c7070dbSScott Long 60564c7070dbSScott Long GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq); 6057f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res, 6058f855ec81SMarius Strobl "tx"); 60594c7070dbSScott Long return (0); 60604c7070dbSScott Long } 60614c7070dbSScott Long 60624c7070dbSScott Long void 60634c7070dbSScott Long iflib_led_create(if_ctx_t ctx) 60644c7070dbSScott Long { 60654c7070dbSScott Long 60664c7070dbSScott Long ctx->ifc_led_dev = led_create(iflib_led_func, ctx, 60674c7070dbSScott Long device_get_nameunit(ctx->ifc_dev)); 60684c7070dbSScott Long } 60694c7070dbSScott Long 60704c7070dbSScott Long void 60714c7070dbSScott Long iflib_tx_intr_deferred(if_ctx_t ctx, int txqid) 60724c7070dbSScott Long { 60734c7070dbSScott Long 60744c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task); 60754c7070dbSScott Long } 60764c7070dbSScott Long 60774c7070dbSScott Long void 60784c7070dbSScott Long iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid) 60794c7070dbSScott Long { 60804c7070dbSScott Long 60814c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task); 60824c7070dbSScott Long } 60834c7070dbSScott Long 60844c7070dbSScott Long void 60854c7070dbSScott Long iflib_admin_intr_deferred(if_ctx_t ctx) 60864c7070dbSScott Long { 60871248952aSSean Bruno #ifdef INVARIANTS 60881248952aSSean Bruno struct grouptask *gtask; 608946fa0c25SEric Joyner 60901248952aSSean Bruno gtask = &ctx->ifc_admin_task; 6091d0d0ad0aSStephen Hurd MPASS(gtask != NULL && gtask->gt_taskqueue != NULL); 60921248952aSSean Bruno #endif 60934c7070dbSScott Long 60944c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_admin_task); 60954c7070dbSScott Long } 60964c7070dbSScott Long 60974c7070dbSScott Long void 60984c7070dbSScott Long iflib_iov_intr_deferred(if_ctx_t ctx) 60994c7070dbSScott Long { 61004c7070dbSScott Long 61014c7070dbSScott Long GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task); 61024c7070dbSScott Long } 61034c7070dbSScott Long 61044c7070dbSScott Long void 6105d49e83eaSMarius Strobl iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name) 61064c7070dbSScott Long { 61074c7070dbSScott Long 6108f855ec81SMarius Strobl taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL, 6109f855ec81SMarius Strobl name); 61104c7070dbSScott Long } 61114c7070dbSScott Long 61124c7070dbSScott Long void 6113aa8a24d3SStephen Hurd iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn, 6114aa8a24d3SStephen Hurd const char *name) 61154c7070dbSScott Long { 61164c7070dbSScott Long 61174c7070dbSScott Long GROUPTASK_INIT(gtask, 0, fn, ctx); 6118f855ec81SMarius Strobl taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL, 6119f855ec81SMarius Strobl name); 61204c7070dbSScott Long } 61214c7070dbSScott Long 61224c7070dbSScott Long void 612323ac9029SStephen Hurd iflib_config_gtask_deinit(struct grouptask *gtask) 612423ac9029SStephen Hurd { 612523ac9029SStephen Hurd 6126ab2e3f79SStephen Hurd taskqgroup_detach(qgroup_if_config_tqg, gtask); 612723ac9029SStephen Hurd } 612823ac9029SStephen Hurd 612923ac9029SStephen Hurd void 613023ac9029SStephen Hurd iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate) 61314c7070dbSScott Long { 61324c7070dbSScott Long if_t ifp = ctx->ifc_ifp; 61334c7070dbSScott Long iflib_txq_t txq = ctx->ifc_txqs; 61344c7070dbSScott Long 61354c7070dbSScott Long if_setbaudrate(ifp, baudrate); 61367b610b60SSean Bruno if (baudrate >= IF_Gbps(10)) { 61377b610b60SSean Bruno STATE_LOCK(ctx); 613895246abbSSean Bruno ctx->ifc_flags |= IFC_PREFETCH; 61397b610b60SSean Bruno STATE_UNLOCK(ctx); 61407b610b60SSean Bruno } 61414c7070dbSScott Long /* If link down, disable watchdog */ 61424c7070dbSScott Long if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) { 61434c7070dbSScott Long for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++) 61444c7070dbSScott Long txq->ift_qstatus = IFLIB_QUEUE_IDLE; 61454c7070dbSScott Long } 61464c7070dbSScott Long ctx->ifc_link_state = link_state; 61474c7070dbSScott Long if_link_state_change(ifp, link_state); 61484c7070dbSScott Long } 61494c7070dbSScott Long 61504c7070dbSScott Long static int 61514c7070dbSScott Long iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq) 61524c7070dbSScott Long { 61534c7070dbSScott Long int credits; 61541248952aSSean Bruno #ifdef INVARIANTS 61551248952aSSean Bruno int credits_pre = txq->ift_cidx_processed; 61561248952aSSean Bruno #endif 61574c7070dbSScott Long 61584c7070dbSScott Long if (ctx->isc_txd_credits_update == NULL) 61594c7070dbSScott Long return (0); 61604c7070dbSScott Long 61618a04b53dSKonstantin Belousov bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map, 61628a04b53dSKonstantin Belousov BUS_DMASYNC_POSTREAD); 616395246abbSSean Bruno if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0) 61644c7070dbSScott Long return (0); 61654c7070dbSScott Long 61664c7070dbSScott Long txq->ift_processed += credits; 61674c7070dbSScott Long txq->ift_cidx_processed += credits; 61684c7070dbSScott Long 61691248952aSSean Bruno MPASS(credits_pre + credits == txq->ift_cidx_processed); 61704c7070dbSScott Long if (txq->ift_cidx_processed >= txq->ift_size) 61714c7070dbSScott Long txq->ift_cidx_processed -= txq->ift_size; 61724c7070dbSScott Long return (credits); 61734c7070dbSScott Long } 61744c7070dbSScott Long 61754c7070dbSScott Long static int 617695246abbSSean Bruno iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget) 61774c7070dbSScott Long { 617895dcf343SMarius Strobl iflib_fl_t fl; 617995dcf343SMarius Strobl u_int i; 61804c7070dbSScott Long 618195dcf343SMarius Strobl for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++) 618295dcf343SMarius Strobl bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map, 618395dcf343SMarius Strobl BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 618423ac9029SStephen Hurd return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx, 618523ac9029SStephen Hurd budget)); 61864c7070dbSScott Long } 61874c7070dbSScott Long 61884c7070dbSScott Long void 61894c7070dbSScott Long iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name, 61904c7070dbSScott Long const char *description, if_int_delay_info_t info, 61914c7070dbSScott Long int offset, int value) 61924c7070dbSScott Long { 61934c7070dbSScott Long info->iidi_ctx = ctx; 61944c7070dbSScott Long info->iidi_offset = offset; 61954c7070dbSScott Long info->iidi_value = value; 61964c7070dbSScott Long SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev), 61974c7070dbSScott Long SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)), 61984c7070dbSScott Long OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW, 61994c7070dbSScott Long info, 0, iflib_sysctl_int_delay, "I", description); 62004c7070dbSScott Long } 62014c7070dbSScott Long 6202aa8a24d3SStephen Hurd struct sx * 62034c7070dbSScott Long iflib_ctx_lock_get(if_ctx_t ctx) 62044c7070dbSScott Long { 62054c7070dbSScott Long 6206aa8a24d3SStephen Hurd return (&ctx->ifc_ctx_sx); 62074c7070dbSScott Long } 62084c7070dbSScott Long 62094c7070dbSScott Long static int 62104c7070dbSScott Long iflib_msix_init(if_ctx_t ctx) 62114c7070dbSScott Long { 62124c7070dbSScott Long device_t dev = ctx->ifc_dev; 62134c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 62144c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 62153d10e9edSMarius Strobl int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues; 62163d10e9edSMarius Strobl int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors; 62174c7070dbSScott Long 6218d2735264SStephen Hurd iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs; 6219d2735264SStephen Hurd iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs; 622023ac9029SStephen Hurd 6221b97de13aSMarius Strobl if (bootverbose) 6222b97de13aSMarius Strobl device_printf(dev, "msix_init qsets capped at %d\n", 6223b97de13aSMarius Strobl imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets)); 62241248952aSSean Bruno 62254c7070dbSScott Long /* Override by tuneable */ 6226ea351d3fSSean Bruno if (scctx->isc_disable_msix) 62274c7070dbSScott Long goto msi; 62284c7070dbSScott Long 6229b97de13aSMarius Strobl /* First try MSI-X */ 6230b97de13aSMarius Strobl if ((msgs = pci_msix_count(dev)) == 0) { 6231b97de13aSMarius Strobl if (bootverbose) 6232b97de13aSMarius Strobl device_printf(dev, "MSI-X not supported or disabled\n"); 6233b97de13aSMarius Strobl goto msi; 6234b97de13aSMarius Strobl } 62353d10e9edSMarius Strobl 62363d10e9edSMarius Strobl bar = ctx->ifc_softc_ctx.isc_msix_bar; 62374c7070dbSScott Long /* 62384c7070dbSScott Long * bar == -1 => "trust me I know what I'm doing" 62394c7070dbSScott Long * Some drivers are for hardware that is so shoddily 62404c7070dbSScott Long * documented that no one knows which bars are which 62414c7070dbSScott Long * so the developer has to map all bars. This hack 6242b97de13aSMarius Strobl * allows shoddy garbage to use MSI-X in this framework. 62434c7070dbSScott Long */ 62444c7070dbSScott Long if (bar != -1) { 62454c7070dbSScott Long ctx->ifc_msix_mem = bus_alloc_resource_any(dev, 62464c7070dbSScott Long SYS_RES_MEMORY, &bar, RF_ACTIVE); 62474c7070dbSScott Long if (ctx->ifc_msix_mem == NULL) { 6248b97de13aSMarius Strobl device_printf(dev, "Unable to map MSI-X table\n"); 62494c7070dbSScott Long goto msi; 62504c7070dbSScott Long } 62514c7070dbSScott Long } 62523d10e9edSMarius Strobl 62533d10e9edSMarius Strobl admincnt = sctx->isc_admin_intrcnt; 62544c7070dbSScott Long #if IFLIB_DEBUG 62554c7070dbSScott Long /* use only 1 qset in debug mode */ 62564c7070dbSScott Long queuemsgs = min(msgs - admincnt, 1); 62574c7070dbSScott Long #else 62584c7070dbSScott Long queuemsgs = msgs - admincnt; 62594c7070dbSScott Long #endif 62604c7070dbSScott Long #ifdef RSS 62614c7070dbSScott Long queues = imin(queuemsgs, rss_getnumbuckets()); 62624c7070dbSScott Long #else 62634c7070dbSScott Long queues = queuemsgs; 62644c7070dbSScott Long #endif 62654c7070dbSScott Long queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues); 6266b97de13aSMarius Strobl if (bootverbose) 6267b97de13aSMarius Strobl device_printf(dev, 6268b97de13aSMarius Strobl "intr CPUs: %d queue msgs: %d admincnt: %d\n", 62694c7070dbSScott Long CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt); 62704c7070dbSScott Long #ifdef RSS 62714c7070dbSScott Long /* If we're doing RSS, clamp at the number of RSS buckets */ 62724c7070dbSScott Long if (queues > rss_getnumbuckets()) 62734c7070dbSScott Long queues = rss_getnumbuckets(); 62744c7070dbSScott Long #endif 627523ac9029SStephen Hurd if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt) 627623ac9029SStephen Hurd rx_queues = iflib_num_rx_queues; 62774c7070dbSScott Long else 62784c7070dbSScott Long rx_queues = queues; 6279d2735264SStephen Hurd 6280d2735264SStephen Hurd if (rx_queues > scctx->isc_nrxqsets) 6281d2735264SStephen Hurd rx_queues = scctx->isc_nrxqsets; 6282d2735264SStephen Hurd 628323ac9029SStephen Hurd /* 628423ac9029SStephen Hurd * We want this to be all logical CPUs by default 628523ac9029SStephen Hurd */ 62864c7070dbSScott Long if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues) 62874c7070dbSScott Long tx_queues = iflib_num_tx_queues; 62884c7070dbSScott Long else 628923ac9029SStephen Hurd tx_queues = mp_ncpus; 629023ac9029SStephen Hurd 6291d2735264SStephen Hurd if (tx_queues > scctx->isc_ntxqsets) 6292d2735264SStephen Hurd tx_queues = scctx->isc_ntxqsets; 6293d2735264SStephen Hurd 629423ac9029SStephen Hurd if (ctx->ifc_sysctl_qs_eq_override == 0) { 629523ac9029SStephen Hurd #ifdef INVARIANTS 629623ac9029SStephen Hurd if (tx_queues != rx_queues) 629777c1fcecSEric Joyner device_printf(dev, 629877c1fcecSEric Joyner "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n", 629923ac9029SStephen Hurd min(rx_queues, tx_queues), min(rx_queues, tx_queues)); 630023ac9029SStephen Hurd #endif 630123ac9029SStephen Hurd tx_queues = min(rx_queues, tx_queues); 630223ac9029SStephen Hurd rx_queues = min(rx_queues, tx_queues); 630323ac9029SStephen Hurd } 63044c7070dbSScott Long 63053d10e9edSMarius Strobl vectors = rx_queues + admincnt; 63063d10e9edSMarius Strobl if (msgs < vectors) { 63073d10e9edSMarius Strobl device_printf(dev, 63083d10e9edSMarius Strobl "insufficient number of MSI-X vectors " 63093d10e9edSMarius Strobl "(supported %d, need %d)\n", msgs, vectors); 63103d10e9edSMarius Strobl goto msi; 63113d10e9edSMarius Strobl } 63123d10e9edSMarius Strobl 63131722eeacSMarius Strobl device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues, 63141722eeacSMarius Strobl tx_queues); 63153d10e9edSMarius Strobl msgs = vectors; 63164c7070dbSScott Long if ((err = pci_alloc_msix(dev, &vectors)) == 0) { 63173d10e9edSMarius Strobl if (vectors != msgs) { 63183d10e9edSMarius Strobl device_printf(dev, 63193d10e9edSMarius Strobl "Unable to allocate sufficient MSI-X vectors " 63203d10e9edSMarius Strobl "(got %d, need %d)\n", vectors, msgs); 63213d10e9edSMarius Strobl pci_release_msi(dev); 63223d10e9edSMarius Strobl if (bar != -1) { 63233d10e9edSMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 63243d10e9edSMarius Strobl ctx->ifc_msix_mem); 63253d10e9edSMarius Strobl ctx->ifc_msix_mem = NULL; 63263d10e9edSMarius Strobl } 63273d10e9edSMarius Strobl goto msi; 63283d10e9edSMarius Strobl } 6329b97de13aSMarius Strobl device_printf(dev, "Using MSI-X interrupts with %d vectors\n", 6330b97de13aSMarius Strobl vectors); 63314c7070dbSScott Long scctx->isc_vectors = vectors; 63324c7070dbSScott Long scctx->isc_nrxqsets = rx_queues; 63334c7070dbSScott Long scctx->isc_ntxqsets = tx_queues; 63344c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSIX; 633523ac9029SStephen Hurd 63364c7070dbSScott Long return (vectors); 63374c7070dbSScott Long } else { 633877c1fcecSEric Joyner device_printf(dev, 63393d10e9edSMarius Strobl "failed to allocate %d MSI-X vectors, err: %d\n", vectors, 63403d10e9edSMarius Strobl err); 63413d10e9edSMarius Strobl if (bar != -1) { 6342e4defe55SMarius Strobl bus_release_resource(dev, SYS_RES_MEMORY, bar, 6343e4defe55SMarius Strobl ctx->ifc_msix_mem); 6344e4defe55SMarius Strobl ctx->ifc_msix_mem = NULL; 63454c7070dbSScott Long } 63463d10e9edSMarius Strobl } 63473d10e9edSMarius Strobl 63484c7070dbSScott Long msi: 63494c7070dbSScott Long vectors = pci_msi_count(dev); 63504c7070dbSScott Long scctx->isc_nrxqsets = 1; 63514c7070dbSScott Long scctx->isc_ntxqsets = 1; 63524c7070dbSScott Long scctx->isc_vectors = vectors; 63534c7070dbSScott Long if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) { 63544c7070dbSScott Long device_printf(dev,"Using an MSI interrupt\n"); 63554c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_MSI; 63564c7070dbSScott Long } else { 6357e4defe55SMarius Strobl scctx->isc_vectors = 1; 63584c7070dbSScott Long device_printf(dev,"Using a Legacy interrupt\n"); 63594c7070dbSScott Long scctx->isc_intr = IFLIB_INTR_LEGACY; 63604c7070dbSScott Long } 63614c7070dbSScott Long 63624c7070dbSScott Long return (vectors); 63634c7070dbSScott Long } 63644c7070dbSScott Long 6365e4defe55SMarius Strobl static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" }; 63664c7070dbSScott Long 63674c7070dbSScott Long static int 63684c7070dbSScott Long mp_ring_state_handler(SYSCTL_HANDLER_ARGS) 63694c7070dbSScott Long { 63704c7070dbSScott Long int rc; 63714c7070dbSScott Long uint16_t *state = ((uint16_t *)oidp->oid_arg1); 63724c7070dbSScott Long struct sbuf *sb; 6373e4defe55SMarius Strobl const char *ring_state = "UNKNOWN"; 63744c7070dbSScott Long 63754c7070dbSScott Long /* XXX needed ? */ 63764c7070dbSScott Long rc = sysctl_wire_old_buffer(req, 0); 63774c7070dbSScott Long MPASS(rc == 0); 63784c7070dbSScott Long if (rc != 0) 63794c7070dbSScott Long return (rc); 63804c7070dbSScott Long sb = sbuf_new_for_sysctl(NULL, NULL, 80, req); 63814c7070dbSScott Long MPASS(sb != NULL); 63824c7070dbSScott Long if (sb == NULL) 63834c7070dbSScott Long return (ENOMEM); 63844c7070dbSScott Long if (state[3] <= 3) 63854c7070dbSScott Long ring_state = ring_states[state[3]]; 63864c7070dbSScott Long 63874c7070dbSScott Long sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s", 63884c7070dbSScott Long state[0], state[1], state[2], ring_state); 63894c7070dbSScott Long rc = sbuf_finish(sb); 63904c7070dbSScott Long sbuf_delete(sb); 63914c7070dbSScott Long return(rc); 63924c7070dbSScott Long } 63934c7070dbSScott Long 639423ac9029SStephen Hurd enum iflib_ndesc_handler { 639523ac9029SStephen Hurd IFLIB_NTXD_HANDLER, 639623ac9029SStephen Hurd IFLIB_NRXD_HANDLER, 639723ac9029SStephen Hurd }; 63984c7070dbSScott Long 639923ac9029SStephen Hurd static int 640023ac9029SStephen Hurd mp_ndesc_handler(SYSCTL_HANDLER_ARGS) 640123ac9029SStephen Hurd { 640223ac9029SStephen Hurd if_ctx_t ctx = (void *)arg1; 640323ac9029SStephen Hurd enum iflib_ndesc_handler type = arg2; 640423ac9029SStephen Hurd char buf[256] = {0}; 640595246abbSSean Bruno qidx_t *ndesc; 640623ac9029SStephen Hurd char *p, *next; 640723ac9029SStephen Hurd int nqs, rc, i; 640823ac9029SStephen Hurd 640923ac9029SStephen Hurd nqs = 8; 641023ac9029SStephen Hurd switch(type) { 641123ac9029SStephen Hurd case IFLIB_NTXD_HANDLER: 641223ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_ntxds; 641323ac9029SStephen Hurd if (ctx->ifc_sctx) 641423ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_ntxqs; 641523ac9029SStephen Hurd break; 641623ac9029SStephen Hurd case IFLIB_NRXD_HANDLER: 641723ac9029SStephen Hurd ndesc = ctx->ifc_sysctl_nrxds; 641823ac9029SStephen Hurd if (ctx->ifc_sctx) 641923ac9029SStephen Hurd nqs = ctx->ifc_sctx->isc_nrxqs; 642023ac9029SStephen Hurd break; 64211ae4848cSMatt Macy default: 64223d10e9edSMarius Strobl printf("%s: unhandled type\n", __func__); 64233d10e9edSMarius Strobl return (EINVAL); 642423ac9029SStephen Hurd } 642523ac9029SStephen Hurd if (nqs == 0) 642623ac9029SStephen Hurd nqs = 8; 642723ac9029SStephen Hurd 642823ac9029SStephen Hurd for (i=0; i<8; i++) { 642923ac9029SStephen Hurd if (i >= nqs) 643023ac9029SStephen Hurd break; 643123ac9029SStephen Hurd if (i) 643223ac9029SStephen Hurd strcat(buf, ","); 643323ac9029SStephen Hurd sprintf(strchr(buf, 0), "%d", ndesc[i]); 643423ac9029SStephen Hurd } 643523ac9029SStephen Hurd 643623ac9029SStephen Hurd rc = sysctl_handle_string(oidp, buf, sizeof(buf), req); 643723ac9029SStephen Hurd if (rc || req->newptr == NULL) 643823ac9029SStephen Hurd return rc; 643923ac9029SStephen Hurd 644023ac9029SStephen Hurd for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p; 644123ac9029SStephen Hurd i++, p = strsep(&next, " ,")) { 644223ac9029SStephen Hurd ndesc[i] = strtoul(p, NULL, 10); 644323ac9029SStephen Hurd } 644423ac9029SStephen Hurd 644523ac9029SStephen Hurd return(rc); 644623ac9029SStephen Hurd } 64474c7070dbSScott Long 64484c7070dbSScott Long #define NAME_BUFLEN 32 64494c7070dbSScott Long static void 64504c7070dbSScott Long iflib_add_device_sysctl_pre(if_ctx_t ctx) 64514c7070dbSScott Long { 64524c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 64534c7070dbSScott Long struct sysctl_oid_list *child, *oid_list; 64544c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 64554c7070dbSScott Long struct sysctl_oid *node; 64564c7070dbSScott Long 64574c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 64584c7070dbSScott Long child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev)); 64594c7070dbSScott Long ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, "iflib", 64604c7070dbSScott Long CTLFLAG_RD, NULL, "IFLIB fields"); 64614c7070dbSScott Long oid_list = SYSCTL_CHILDREN(node); 64624c7070dbSScott Long 646310a1e981SEric Joyner SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version", 646410a1e981SEric Joyner CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, 646523ac9029SStephen Hurd "driver version"); 646623ac9029SStephen Hurd 64674c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs", 64684c7070dbSScott Long CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0, 64694c7070dbSScott Long "# of txqs to use, 0 => use default #"); 64704c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs", 647123ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0, 647223ac9029SStephen Hurd "# of rxqs to use, 0 => use default #"); 647323ac9029SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable", 647423ac9029SStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0, 647523ac9029SStephen Hurd "permit #txq != #rxq"); 6476ea351d3fSSean Bruno SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix", 6477ea351d3fSSean Bruno CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0, 6478b97de13aSMarius Strobl "disable MSI-X (default 0)"); 6479f4d2154eSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget", 6480f4d2154eSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, 64811722eeacSMarius Strobl "set the RX budget"); 6482fe51d4cdSStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate", 6483fe51d4cdSStephen Hurd CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0, 64841722eeacSMarius Strobl "cause TX to abdicate instead of running to completion"); 6485f154ece0SStephen Hurd ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED; 6486f154ece0SStephen Hurd SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "core_offset", 6487f154ece0SStephen Hurd CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0, 6488f154ece0SStephen Hurd "offset to start using cores at"); 6489f154ece0SStephen Hurd SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "separate_txrx", 6490f154ece0SStephen Hurd CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0, 6491f154ece0SStephen Hurd "use separate cores for TX and RX"); 64924c7070dbSScott Long 649323ac9029SStephen Hurd /* XXX change for per-queue sizes */ 649423ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds", 649523ac9029SStephen Hurd CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NTXD_HANDLER, 649623ac9029SStephen Hurd mp_ndesc_handler, "A", 64971722eeacSMarius Strobl "list of # of TX descriptors to use, 0 = use default #"); 649823ac9029SStephen Hurd SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds", 649923ac9029SStephen Hurd CTLTYPE_STRING|CTLFLAG_RWTUN, ctx, IFLIB_NRXD_HANDLER, 650023ac9029SStephen Hurd mp_ndesc_handler, "A", 65011722eeacSMarius Strobl "list of # of RX descriptors to use, 0 = use default #"); 65024c7070dbSScott Long } 65034c7070dbSScott Long 65044c7070dbSScott Long static void 65054c7070dbSScott Long iflib_add_device_sysctl_post(if_ctx_t ctx) 65064c7070dbSScott Long { 65074c7070dbSScott Long if_shared_ctx_t sctx = ctx->ifc_sctx; 65084c7070dbSScott Long if_softc_ctx_t scctx = &ctx->ifc_softc_ctx; 65094c7070dbSScott Long device_t dev = iflib_get_dev(ctx); 65104c7070dbSScott Long struct sysctl_oid_list *child; 65114c7070dbSScott Long struct sysctl_ctx_list *ctx_list; 65124c7070dbSScott Long iflib_fl_t fl; 65134c7070dbSScott Long iflib_txq_t txq; 65144c7070dbSScott Long iflib_rxq_t rxq; 65154c7070dbSScott Long int i, j; 65164c7070dbSScott Long char namebuf[NAME_BUFLEN]; 65174c7070dbSScott Long char *qfmt; 65184c7070dbSScott Long struct sysctl_oid *queue_node, *fl_node, *node; 65194c7070dbSScott Long struct sysctl_oid_list *queue_list, *fl_list; 65204c7070dbSScott Long ctx_list = device_get_sysctl_ctx(dev); 65214c7070dbSScott Long 65224c7070dbSScott Long node = ctx->ifc_sysctl_node; 65234c7070dbSScott Long child = SYSCTL_CHILDREN(node); 65244c7070dbSScott Long 65254c7070dbSScott Long if (scctx->isc_ntxqsets > 100) 65264c7070dbSScott Long qfmt = "txq%03d"; 65274c7070dbSScott Long else if (scctx->isc_ntxqsets > 10) 65284c7070dbSScott Long qfmt = "txq%02d"; 65294c7070dbSScott Long else 65304c7070dbSScott Long qfmt = "txq%d"; 65314c7070dbSScott Long for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) { 65324c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 65334c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 65344c7070dbSScott Long CTLFLAG_RD, NULL, "Queue Name"); 65354c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 65364c7070dbSScott Long #if MEMORY_LOGGING 65374c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued", 65384c7070dbSScott Long CTLFLAG_RD, 65394c7070dbSScott Long &txq->ift_dequeued, "total mbufs freed"); 65404c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued", 65414c7070dbSScott Long CTLFLAG_RD, 65424c7070dbSScott Long &txq->ift_enqueued, "total mbufs enqueued"); 65434c7070dbSScott Long #endif 65444c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag", 65454c7070dbSScott Long CTLFLAG_RD, 65464c7070dbSScott Long &txq->ift_mbuf_defrag, "# of times m_defrag was called"); 65474c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "m_pullups", 65484c7070dbSScott Long CTLFLAG_RD, 65494c7070dbSScott Long &txq->ift_pullups, "# of times m_pullup was called"); 65504c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag_failed", 65514c7070dbSScott Long CTLFLAG_RD, 65524c7070dbSScott Long &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed"); 65534c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_desc_avail", 65544c7070dbSScott Long CTLFLAG_RD, 655523ac9029SStephen Hurd &txq->ift_no_desc_avail, "# of times no descriptors were available"); 65564c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "tx_map_failed", 65574c7070dbSScott Long CTLFLAG_RD, 65581722eeacSMarius Strobl &txq->ift_map_failed, "# of times DMA map failed"); 65594c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txd_encap_efbig", 65604c7070dbSScott Long CTLFLAG_RD, 65614c7070dbSScott Long &txq->ift_txd_encap_efbig, "# of times txd_encap returned EFBIG"); 65624c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "no_tx_dma_setup", 65634c7070dbSScott Long CTLFLAG_RD, 65644c7070dbSScott Long &txq->ift_no_tx_dma_setup, "# of times map failed for other than EFBIG"); 65654c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx", 65664c7070dbSScott Long CTLFLAG_RD, 65674c7070dbSScott Long &txq->ift_pidx, 1, "Producer Index"); 65684c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx", 65694c7070dbSScott Long CTLFLAG_RD, 65704c7070dbSScott Long &txq->ift_cidx, 1, "Consumer Index"); 65714c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx_processed", 65724c7070dbSScott Long CTLFLAG_RD, 65734c7070dbSScott Long &txq->ift_cidx_processed, 1, "Consumer Index seen by credit update"); 65744c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use", 65754c7070dbSScott Long CTLFLAG_RD, 65764c7070dbSScott Long &txq->ift_in_use, 1, "descriptors in use"); 65774c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_processed", 65784c7070dbSScott Long CTLFLAG_RD, 65794c7070dbSScott Long &txq->ift_processed, "descriptors procesed for clean"); 65804c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned", 65814c7070dbSScott Long CTLFLAG_RD, 65824c7070dbSScott Long &txq->ift_cleaned, "total cleaned"); 65834c7070dbSScott Long SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state", 658495246abbSSean Bruno CTLTYPE_STRING | CTLFLAG_RD, __DEVOLATILE(uint64_t *, &txq->ift_br->state), 65854c7070dbSScott Long 0, mp_ring_state_handler, "A", "soft ring state"); 65864c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_enqueues", 658795246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->enqueues, 65884c7070dbSScott Long "# of enqueues to the mp_ring for this queue"); 65894c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_drops", 659095246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->drops, 65914c7070dbSScott Long "# of drops in the mp_ring for this queue"); 65924c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_starts", 659395246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->starts, 65944c7070dbSScott Long "# of normal consumer starts in the mp_ring for this queue"); 65954c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_stalls", 659695246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->stalls, 65974c7070dbSScott Long "# of consumer stalls in the mp_ring for this queue"); 65984c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_restarts", 659995246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->restarts, 66004c7070dbSScott Long "# of consumer restarts in the mp_ring for this queue"); 66014c7070dbSScott Long SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO, "r_abdications", 660295246abbSSean Bruno CTLFLAG_RD, &txq->ift_br->abdications, 66034c7070dbSScott Long "# of consumer abdications in the mp_ring for this queue"); 66044c7070dbSScott Long } 66054c7070dbSScott Long 66064c7070dbSScott Long if (scctx->isc_nrxqsets > 100) 66074c7070dbSScott Long qfmt = "rxq%03d"; 66084c7070dbSScott Long else if (scctx->isc_nrxqsets > 10) 66094c7070dbSScott Long qfmt = "rxq%02d"; 66104c7070dbSScott Long else 66114c7070dbSScott Long qfmt = "rxq%d"; 66124c7070dbSScott Long for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) { 66134c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, qfmt, i); 66144c7070dbSScott Long queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf, 66154c7070dbSScott Long CTLFLAG_RD, NULL, "Queue Name"); 66164c7070dbSScott Long queue_list = SYSCTL_CHILDREN(queue_node); 661723ac9029SStephen Hurd if (sctx->isc_flags & IFLIB_HAS_RXCQ) { 66184c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "rxq_cq_cidx", 66194c7070dbSScott Long CTLFLAG_RD, 66204c7070dbSScott Long &rxq->ifr_cq_cidx, 1, "Consumer Index"); 66214c7070dbSScott Long } 6622da69b8f9SSean Bruno 66234c7070dbSScott Long for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) { 66244c7070dbSScott Long snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j); 66254c7070dbSScott Long fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list, OID_AUTO, namebuf, 66264c7070dbSScott Long CTLFLAG_RD, NULL, "freelist Name"); 66274c7070dbSScott Long fl_list = SYSCTL_CHILDREN(fl_node); 66284c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx", 66294c7070dbSScott Long CTLFLAG_RD, 66304c7070dbSScott Long &fl->ifl_pidx, 1, "Producer Index"); 66314c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx", 66324c7070dbSScott Long CTLFLAG_RD, 66334c7070dbSScott Long &fl->ifl_cidx, 1, "Consumer Index"); 66344c7070dbSScott Long SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits", 66354c7070dbSScott Long CTLFLAG_RD, 66364c7070dbSScott Long &fl->ifl_credits, 1, "credits available"); 66374c7070dbSScott Long #if MEMORY_LOGGING 66384c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_enqueued", 66394c7070dbSScott Long CTLFLAG_RD, 66404c7070dbSScott Long &fl->ifl_m_enqueued, "mbufs allocated"); 66414c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_m_dequeued", 66424c7070dbSScott Long CTLFLAG_RD, 66434c7070dbSScott Long &fl->ifl_m_dequeued, "mbufs freed"); 66444c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_enqueued", 66454c7070dbSScott Long CTLFLAG_RD, 66464c7070dbSScott Long &fl->ifl_cl_enqueued, "clusters allocated"); 66474c7070dbSScott Long SYSCTL_ADD_QUAD(ctx_list, fl_list, OID_AUTO, "fl_cl_dequeued", 66484c7070dbSScott Long CTLFLAG_RD, 66494c7070dbSScott Long &fl->ifl_cl_dequeued, "clusters freed"); 66504c7070dbSScott Long #endif 66514c7070dbSScott Long 66524c7070dbSScott Long } 66534c7070dbSScott Long } 66544c7070dbSScott Long 66554c7070dbSScott Long } 665695246abbSSean Bruno 665777c1fcecSEric Joyner void 665877c1fcecSEric Joyner iflib_request_reset(if_ctx_t ctx) 665977c1fcecSEric Joyner { 666077c1fcecSEric Joyner 666177c1fcecSEric Joyner STATE_LOCK(ctx); 666277c1fcecSEric Joyner ctx->ifc_flags |= IFC_DO_RESET; 666377c1fcecSEric Joyner STATE_UNLOCK(ctx); 666477c1fcecSEric Joyner } 666577c1fcecSEric Joyner 666695246abbSSean Bruno #ifndef __NO_STRICT_ALIGNMENT 666795246abbSSean Bruno static struct mbuf * 666895246abbSSean Bruno iflib_fixup_rx(struct mbuf *m) 666995246abbSSean Bruno { 667095246abbSSean Bruno struct mbuf *n; 667195246abbSSean Bruno 667295246abbSSean Bruno if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) { 667395246abbSSean Bruno bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len); 667495246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 667595246abbSSean Bruno n = m; 667695246abbSSean Bruno } else { 667795246abbSSean Bruno MGETHDR(n, M_NOWAIT, MT_DATA); 667895246abbSSean Bruno if (n == NULL) { 667995246abbSSean Bruno m_freem(m); 668095246abbSSean Bruno return (NULL); 668195246abbSSean Bruno } 668295246abbSSean Bruno bcopy(m->m_data, n->m_data, ETHER_HDR_LEN); 668395246abbSSean Bruno m->m_data += ETHER_HDR_LEN; 668495246abbSSean Bruno m->m_len -= ETHER_HDR_LEN; 668595246abbSSean Bruno n->m_len = ETHER_HDR_LEN; 668695246abbSSean Bruno M_MOVE_PKTHDR(n, m); 668795246abbSSean Bruno n->m_next = m; 668895246abbSSean Bruno } 668995246abbSSean Bruno return (n); 669095246abbSSean Bruno } 669195246abbSSean Bruno #endif 669294618825SMark Johnston 669394618825SMark Johnston #ifdef NETDUMP 669494618825SMark Johnston static void 66951722eeacSMarius Strobl iflib_netdump_init(if_t ifp, int *nrxr, int *ncl, int *clsize) 669694618825SMark Johnston { 669794618825SMark Johnston if_ctx_t ctx; 669894618825SMark Johnston 669994618825SMark Johnston ctx = if_getsoftc(ifp); 670094618825SMark Johnston CTX_LOCK(ctx); 670194618825SMark Johnston *nrxr = NRXQSETS(ctx); 670294618825SMark Johnston *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size; 670394618825SMark Johnston *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size; 670494618825SMark Johnston CTX_UNLOCK(ctx); 670594618825SMark Johnston } 670694618825SMark Johnston 670794618825SMark Johnston static void 67081722eeacSMarius Strobl iflib_netdump_event(if_t ifp, enum netdump_ev event) 670994618825SMark Johnston { 671094618825SMark Johnston if_ctx_t ctx; 671194618825SMark Johnston if_softc_ctx_t scctx; 671294618825SMark Johnston iflib_fl_t fl; 671394618825SMark Johnston iflib_rxq_t rxq; 671494618825SMark Johnston int i, j; 671594618825SMark Johnston 671694618825SMark Johnston ctx = if_getsoftc(ifp); 671794618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 671894618825SMark Johnston 671994618825SMark Johnston switch (event) { 672094618825SMark Johnston case NETDUMP_START: 672194618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) { 672294618825SMark Johnston rxq = &ctx->ifc_rxqs[i]; 672394618825SMark Johnston for (j = 0; j < rxq->ifr_nfl; j++) { 672494618825SMark Johnston fl = rxq->ifr_fl; 672594618825SMark Johnston fl->ifl_zone = m_getzone(fl->ifl_buf_size); 672694618825SMark Johnston } 672794618825SMark Johnston } 672894618825SMark Johnston iflib_no_tx_batch = 1; 672994618825SMark Johnston break; 673094618825SMark Johnston default: 673194618825SMark Johnston break; 673294618825SMark Johnston } 673394618825SMark Johnston } 673494618825SMark Johnston 673594618825SMark Johnston static int 67361722eeacSMarius Strobl iflib_netdump_transmit(if_t ifp, struct mbuf *m) 673794618825SMark Johnston { 673894618825SMark Johnston if_ctx_t ctx; 673994618825SMark Johnston iflib_txq_t txq; 674094618825SMark Johnston int error; 674194618825SMark Johnston 674294618825SMark Johnston ctx = if_getsoftc(ifp); 674394618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 674494618825SMark Johnston IFF_DRV_RUNNING) 674594618825SMark Johnston return (EBUSY); 674694618825SMark Johnston 674794618825SMark Johnston txq = &ctx->ifc_txqs[0]; 674894618825SMark Johnston error = iflib_encap(txq, &m); 674994618825SMark Johnston if (error == 0) 675094618825SMark Johnston (void)iflib_txd_db_check(ctx, txq, true, txq->ift_in_use); 675194618825SMark Johnston return (error); 675294618825SMark Johnston } 675394618825SMark Johnston 675494618825SMark Johnston static int 67551722eeacSMarius Strobl iflib_netdump_poll(if_t ifp, int count) 675694618825SMark Johnston { 675794618825SMark Johnston if_ctx_t ctx; 675894618825SMark Johnston if_softc_ctx_t scctx; 675994618825SMark Johnston iflib_txq_t txq; 676094618825SMark Johnston int i; 676194618825SMark Johnston 676294618825SMark Johnston ctx = if_getsoftc(ifp); 676394618825SMark Johnston scctx = &ctx->ifc_softc_ctx; 676494618825SMark Johnston 676594618825SMark Johnston if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 676694618825SMark Johnston IFF_DRV_RUNNING) 676794618825SMark Johnston return (EBUSY); 676894618825SMark Johnston 676994618825SMark Johnston txq = &ctx->ifc_txqs[0]; 677094618825SMark Johnston (void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx)); 677194618825SMark Johnston 677294618825SMark Johnston for (i = 0; i < scctx->isc_nrxqsets; i++) 677394618825SMark Johnston (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */); 677494618825SMark Johnston return (0); 677594618825SMark Johnston } 677694618825SMark Johnston #endif /* NETDUMP */ 6777